]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netipx/ipx_pcb.c
MFV r262617: ncurses 5.9.
[FreeBSD/FreeBSD.git] / sys / netipx / ipx_pcb.c
1 /*-
2  * Copyright (c) 1984, 1985, 1986, 1987, 1993
3  *      The Regents of the University of California.
4  * Copyright (c) 2004-2009 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * Copyright (c) 1995, Mike Mitchell
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  * 3. All advertising materials mentioning features or use of this software
43  *    must display the following acknowledgement:
44  *      This product includes software developed by the University of
45  *      California, Berkeley and its contributors.
46  * 4. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  *      @(#)ipx_pcb.c
63  */
64
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/malloc.h>
71 #include <sys/priv.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74
75 #include <net/if.h>
76 #include <net/if_var.h>
77 #include <net/route.h>
78
79 #include <netipx/ipx.h>
80 #include <netipx/ipx_if.h>
81 #include <netipx/ipx_pcb.h>
82 #include <netipx/ipx_var.h>
83
84 static struct   ipx_addr zeroipx_addr;
85 static u_short  ipxpcb_lport_cache;
86
87 int
88 ipx_pcballoc(struct socket *so, struct ipxpcbhead *head, struct thread *td)
89 {
90         struct ipxpcb *ipxp;
91
92         KASSERT(so->so_pcb == NULL, ("ipx_pcballoc: so_pcb != NULL"));
93         IPX_LIST_LOCK_ASSERT();
94
95         ipxp = malloc(sizeof *ipxp, M_PCB, M_NOWAIT | M_ZERO);
96         if (ipxp == NULL)
97                 return (ENOBUFS);
98         IPX_LOCK_INIT(ipxp);
99         ipxp->ipxp_socket = so;
100         if (ipxcksum)
101                 ipxp->ipxp_flags |= IPXP_CHECKSUM;
102         LIST_INSERT_HEAD(head, ipxp, ipxp_list);
103         so->so_pcb = (caddr_t)ipxp;
104         return (0);
105 }
106
107 int
108 ipx_pcbbind(struct ipxpcb *ipxp, struct sockaddr *nam, struct thread *td)
109 {
110         struct sockaddr_ipx *sipx;
111         u_short lport = 0;
112
113         IPX_LIST_LOCK_ASSERT();
114         IPX_LOCK_ASSERT(ipxp);
115
116         if (ipxp->ipxp_lport || !ipx_nullhost(ipxp->ipxp_laddr))
117                 return (EINVAL);
118         if (nam == NULL)
119                 goto noname;
120         sipx = (struct sockaddr_ipx *)nam;
121         if (!ipx_nullhost(sipx->sipx_addr)) {
122                 int tport = sipx->sipx_port;
123
124                 sipx->sipx_port = 0;            /* yech... */
125                 if (ifa_ifwithaddr_check((struct sockaddr *)sipx) == 0)
126                         return (EADDRNOTAVAIL);
127                 sipx->sipx_port = tport;
128         }
129         lport = sipx->sipx_port;
130         if (lport) {
131                 u_short aport = ntohs(lport);
132
133                 if (aport < IPXPORT_RESERVED && td != NULL &&
134                     priv_check(td, PRIV_NETIPX_RESERVEDPORT))
135                         return (EACCES);
136                 if (ipx_pcblookup(&zeroipx_addr, lport, 0))
137                         return (EADDRINUSE);
138         }
139         ipxp->ipxp_laddr = sipx->sipx_addr;
140 noname:
141         if (lport == 0)
142                 do {
143                         ipxpcb_lport_cache++;
144                         if ((ipxpcb_lport_cache < IPXPORT_RESERVED) ||
145                             (ipxpcb_lport_cache >= IPXPORT_WELLKNOWN))
146                                 ipxpcb_lport_cache = IPXPORT_RESERVED;
147                         lport = htons(ipxpcb_lport_cache);
148                 } while (ipx_pcblookup(&zeroipx_addr, lport, 0));
149         ipxp->ipxp_lport = lport;
150         return (0);
151 }
152
153 /*
154  * Connect from a socket to a specified address.
155  * Both address and port must be specified in argument sipx.
156  * If don't have a local address for this socket yet,
157  * then pick one.
158  */
159 int
160 ipx_pcbconnect(struct ipxpcb *ipxp, struct sockaddr *nam, struct thread *td)
161 {
162         struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)nam;
163         struct ipx_addr *dst;
164         struct route *ro;
165         struct ifnet *ifp;
166
167         IPX_LIST_LOCK_ASSERT();
168         IPX_LOCK_ASSERT(ipxp);
169
170         if (sipx->sipx_family != AF_IPX)
171                 return (EAFNOSUPPORT);
172         if (sipx->sipx_port == 0 || ipx_nullhost(sipx->sipx_addr))
173                 return (EADDRNOTAVAIL);
174         /*
175          * If we haven't bound which network number to use as ours,
176          * we will use the number of the outgoing interface.
177          * This depends on having done a routing lookup, which
178          * we will probably have to do anyway, so we might
179          * as well do it now.  On the other hand if we are
180          * sending to multiple destinations we may have already
181          * done the lookup, so see if we can use the route
182          * from before.  In any case, we only
183          * chose a port number once, even if sending to multiple
184          * destinations.
185          */
186         ro = &ipxp->ipxp_route;
187         dst = &satoipx_addr(ro->ro_dst);
188         if (ipxp->ipxp_socket->so_options & SO_DONTROUTE)
189                 goto flush;
190         if (!ipx_neteq(ipxp->ipxp_lastdst, sipx->sipx_addr))
191                 goto flush;
192         if (!ipx_hosteq(ipxp->ipxp_lastdst, sipx->sipx_addr)) {
193                 if (ro->ro_rt != NULL && !(ro->ro_rt->rt_flags & RTF_HOST)) {
194                         /* can patch route to avoid rtalloc */
195                         *dst = sipx->sipx_addr;
196                 } else {
197         flush:
198                         if (ro->ro_rt != NULL)
199                                 RTFREE(ro->ro_rt);
200                         ro->ro_rt = NULL;
201                 }
202         }/* else cached route is ok; do nothing */
203         ipxp->ipxp_lastdst = sipx->sipx_addr;
204         if ((ipxp->ipxp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
205             (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL)) {
206                     /* No route yet, so try to acquire one */
207                     ro->ro_dst.sa_family = AF_IPX;
208                     ro->ro_dst.sa_len = sizeof(ro->ro_dst);
209                     *dst = sipx->sipx_addr;
210                     dst->x_port = 0;
211                     rtalloc_ign(ro, 0);
212         }
213         if (ipx_neteqnn(ipxp->ipxp_laddr.x_net, ipx_zeronet)) {
214                 struct ipx_ifaddr *ia = NULL;
215
216                 /*
217                  * If route is known or can be allocated now,
218                  * our src addr is taken from the i/f, else punt.
219                  */
220
221                 /*
222                  * If we found a route, use the address
223                  * corresponding to the outgoing interface
224                  */
225                 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL) {
226                         IPX_IFADDR_RLOCK();
227                         TAILQ_FOREACH(ia, &ipx_ifaddrhead, ia_link) {
228                                 if (ia->ia_ifp == ifp) {
229                                         ifa_ref(&ia->ia_ifa);
230                                         break;
231                                 }
232                         }
233                         IPX_IFADDR_RUNLOCK();
234                 }
235                 if (ia == NULL) {
236                         u_short fport = sipx->sipx_addr.x_port;
237                         sipx->sipx_addr.x_port = 0;
238                         ia = (struct ipx_ifaddr *)
239                                 ifa_ifwithdstaddr((struct sockaddr *)sipx);
240                         sipx->sipx_addr.x_port = fport;
241                         if (ia == NULL) {
242                                 IPX_IFADDR_RLOCK();
243                                 ia = ipx_iaonnetof(&sipx->sipx_addr);
244                                 if (ia != NULL)
245                                         ifa_ref(&ia->ia_ifa);
246                                 IPX_IFADDR_RUNLOCK();
247                         }
248                         if (ia == NULL) {
249                                 IPX_IFADDR_RLOCK();
250                                 ia = TAILQ_FIRST(&ipx_ifaddrhead);
251                                 if (ia != NULL)
252                                         ifa_ref(&ia->ia_ifa);
253                                 IPX_IFADDR_RUNLOCK();
254                         }
255                         if (ia == NULL)
256                                 return (EADDRNOTAVAIL);
257                 }
258                 ipxp->ipxp_laddr.x_net = satoipx_addr(ia->ia_addr).x_net;
259                 ifa_free(&ia->ia_ifa);
260         }
261         if (ipx_nullhost(ipxp->ipxp_laddr)) {
262                 struct ipx_ifaddr *ia = NULL;
263                 /*
264                  * If route is known or can be allocated now,
265                  * our src addr is taken from the i/f, else punt.
266                  */
267
268                 /*
269                  * If we found a route, use the address
270                  * corresponding to the outgoing interface
271                  */
272                 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL) {
273                         IPX_IFADDR_RLOCK();
274                         TAILQ_FOREACH(ia, &ipx_ifaddrhead, ia_link) {
275                                 if (ia->ia_ifp == ifp) {
276                                         ifa_ref(&ia->ia_ifa);
277                                         break;
278                                 }
279                         }
280                         IPX_IFADDR_RUNLOCK();
281                 }
282                 if (ia == NULL) {
283                         u_short fport = sipx->sipx_addr.x_port;
284                         sipx->sipx_addr.x_port = 0;
285                         ia = (struct ipx_ifaddr *)
286                                 ifa_ifwithdstaddr((struct sockaddr *)sipx);
287                         sipx->sipx_addr.x_port = fport;
288                         if (ia == NULL) {
289                                 IPX_IFADDR_RLOCK();
290                                 ia = ipx_iaonnetof(&sipx->sipx_addr);
291                                 if (ia != NULL)
292                                         ifa_ref(&ia->ia_ifa);
293                                 IPX_IFADDR_RUNLOCK();
294                         }
295                         if (ia == NULL) {
296                                 IPX_IFADDR_RLOCK();
297                                 ia = TAILQ_FIRST(&ipx_ifaddrhead);
298                                 if (ia != NULL)
299                                         ifa_ref(&ia->ia_ifa);
300                                 IPX_IFADDR_RUNLOCK();
301                         }
302                         if (ia == NULL)
303                                 return (EADDRNOTAVAIL);
304                 }
305                 ipxp->ipxp_laddr.x_host = satoipx_addr(ia->ia_addr).x_host;
306                 ifa_free(&ia->ia_ifa);
307         }
308         if (ipx_pcblookup(&sipx->sipx_addr, ipxp->ipxp_lport, 0))
309                 return (EADDRINUSE);
310         if (ipxp->ipxp_lport == 0)
311                 ipx_pcbbind(ipxp, (struct sockaddr *)NULL, td);
312
313         /* XXX just leave it zero if we can't find a route */
314
315         ipxp->ipxp_faddr = sipx->sipx_addr;
316         /* Includes ipxp->ipxp_fport = sipx->sipx_port; */
317         return (0);
318 }
319
320 void
321 ipx_pcbdisconnect(struct ipxpcb *ipxp)
322 {
323
324         IPX_LIST_LOCK_ASSERT();
325         IPX_LOCK_ASSERT(ipxp);
326
327         ipxp->ipxp_faddr = zeroipx_addr;
328 }
329
330 void
331 ipx_pcbdetach(struct ipxpcb *ipxp)
332 {
333         struct socket *so = ipxp->ipxp_socket;
334
335         IPX_LIST_LOCK_ASSERT();
336         IPX_LOCK_ASSERT(ipxp);
337
338         so->so_pcb = NULL;
339         ipxp->ipxp_socket = NULL;
340 }
341
342 void
343 ipx_pcbfree(struct ipxpcb *ipxp)
344 {
345
346         KASSERT(ipxp->ipxp_socket == NULL,
347             ("ipx_pcbfree: ipxp_socket != NULL"));
348         IPX_LIST_LOCK_ASSERT();
349         IPX_LOCK_ASSERT(ipxp);
350
351         if (ipxp->ipxp_route.ro_rt != NULL)
352                 RTFREE(ipxp->ipxp_route.ro_rt);
353         LIST_REMOVE(ipxp, ipxp_list);
354         IPX_LOCK_DESTROY(ipxp);
355         free(ipxp, M_PCB);
356 }
357
358 void
359 ipx_getsockaddr(struct ipxpcb *ipxp, struct sockaddr **nam)
360 {
361         struct sockaddr_ipx *sipx, ssipx;
362
363         sipx = &ssipx;
364         bzero((caddr_t)sipx, sizeof(*sipx));
365         sipx->sipx_len = sizeof(*sipx);
366         sipx->sipx_family = AF_IPX;
367         IPX_LOCK(ipxp);
368         sipx->sipx_addr = ipxp->ipxp_laddr;
369         IPX_UNLOCK(ipxp);
370         *nam = sodupsockaddr((struct sockaddr *)sipx, M_WAITOK);
371 }
372
373 void
374 ipx_getpeeraddr(struct ipxpcb *ipxp, struct sockaddr **nam)
375 {
376         struct sockaddr_ipx *sipx, ssipx;
377
378         sipx = &ssipx;
379         bzero(sipx, sizeof(*sipx));
380         sipx->sipx_len = sizeof(*sipx);
381         sipx->sipx_family = AF_IPX;
382         IPX_LOCK(ipxp);
383         sipx->sipx_addr = ipxp->ipxp_faddr;
384         IPX_UNLOCK(ipxp);
385         *nam = sodupsockaddr((struct sockaddr *)sipx, M_WAITOK);
386 }
387
388 struct ipxpcb *
389 ipx_pcblookup(struct ipx_addr *faddr, u_short lport, int wildp)
390 {
391         struct ipxpcb *ipxp, *match = NULL;
392         int matchwild = 3, wildcard;
393         u_short fport;
394
395         IPX_LIST_LOCK_ASSERT();
396
397         fport = faddr->x_port;
398         LIST_FOREACH(ipxp, &ipxpcb_list, ipxp_list) {
399                 if (ipxp->ipxp_lport != lport)
400                         continue;
401                 wildcard = 0;
402                 if (ipx_nullhost(ipxp->ipxp_faddr)) {
403                         if (!ipx_nullhost(*faddr))
404                                 wildcard++;
405                 } else {
406                         if (ipx_nullhost(*faddr))
407                                 wildcard++;
408                         else {
409                                 if (!ipx_hosteq(ipxp->ipxp_faddr, *faddr))
410                                         continue;
411                                 if (ipxp->ipxp_fport != fport) {
412                                         if (ipxp->ipxp_fport != 0)
413                                                 continue;
414                                         else
415                                                 wildcard++;
416                                 }
417                         }
418                 }
419                 if (wildcard && wildp == 0)
420                         continue;
421                 if (wildcard < matchwild) {
422                         match = ipxp;
423                         matchwild = wildcard;
424                         if (wildcard == 0)
425                                 break;
426                 }
427         }
428         return (match);
429 }