2 * Copyright (c) 2004-2005 Robert N. M. Watson
3 * Copyright (c) 1995, Mike Mitchell
4 * Copyright (c) 1984, 1985, 1986, 1987, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
48 #include <net/route.h>
50 #include <netipx/ipx.h>
51 #include <netipx/ipx_if.h>
52 #include <netipx/ipx_pcb.h>
53 #include <netipx/ipx_var.h>
55 static struct ipx_addr zeroipx_addr;
56 static u_short ipxpcb_lport_cache;
59 ipx_pcballoc(so, head, td)
61 struct ipxpcbhead *head;
64 register struct ipxpcb *ipxp;
66 IPX_LIST_LOCK_ASSERT();
68 MALLOC(ipxp, struct ipxpcb *, sizeof *ipxp, M_PCB, M_NOWAIT | M_ZERO);
72 ipxp->ipxp_socket = so;
74 ipxp->ipxp_flags |= IPXP_CHECKSUM;
75 LIST_INSERT_HEAD(head, ipxp, ipxp_list);
76 so->so_pcb = (caddr_t)ipxp;
81 ipx_pcbbind(ipxp, nam, td)
82 register struct ipxpcb *ipxp;
86 register struct sockaddr_ipx *sipx;
89 IPX_LIST_LOCK_ASSERT();
90 IPX_LOCK_ASSERT(ipxp);
92 if (ipxp->ipxp_lport || !ipx_nullhost(ipxp->ipxp_laddr))
96 sipx = (struct sockaddr_ipx *)nam;
97 if (!ipx_nullhost(sipx->sipx_addr)) {
98 int tport = sipx->sipx_port;
100 sipx->sipx_port = 0; /* yech... */
101 if (ifa_ifwithaddr((struct sockaddr *)sipx) == NULL)
102 return (EADDRNOTAVAIL);
103 sipx->sipx_port = tport;
105 lport = sipx->sipx_port;
107 u_short aport = ntohs(lport);
110 if (aport < IPXPORT_RESERVED &&
111 td != NULL && (error = suser(td)) != 0)
113 if (ipx_pcblookup(&zeroipx_addr, lport, 0))
116 ipxp->ipxp_laddr = sipx->sipx_addr;
120 ipxpcb_lport_cache++;
121 if ((ipxpcb_lport_cache < IPXPORT_RESERVED) ||
122 (ipxpcb_lport_cache >= IPXPORT_WELLKNOWN))
123 ipxpcb_lport_cache = IPXPORT_RESERVED;
124 lport = htons(ipxpcb_lport_cache);
125 } while (ipx_pcblookup(&zeroipx_addr, lport, 0));
126 ipxp->ipxp_lport = lport;
131 * Connect from a socket to a specified address.
132 * Both address and port must be specified in argument sipx.
133 * If don't have a local address for this socket yet,
137 ipx_pcbconnect(ipxp, nam, td)
139 struct sockaddr *nam;
142 struct ipx_ifaddr *ia;
143 register struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)nam;
144 register struct ipx_addr *dst;
145 register struct route *ro;
148 IPX_LIST_LOCK_ASSERT();
149 IPX_LOCK_ASSERT(ipxp);
153 if (sipx->sipx_family != AF_IPX)
154 return (EAFNOSUPPORT);
155 if (sipx->sipx_port == 0 || ipx_nullhost(sipx->sipx_addr))
156 return (EADDRNOTAVAIL);
158 * If we haven't bound which network number to use as ours,
159 * we will use the number of the outgoing interface.
160 * This depends on having done a routing lookup, which
161 * we will probably have to do anyway, so we might
162 * as well do it now. On the other hand if we are
163 * sending to multiple destinations we may have already
164 * done the lookup, so see if we can use the route
165 * from before. In any case, we only
166 * chose a port number once, even if sending to multiple
169 ro = &ipxp->ipxp_route;
170 dst = &satoipx_addr(ro->ro_dst);
171 if (ipxp->ipxp_socket->so_options & SO_DONTROUTE)
173 if (!ipx_neteq(ipxp->ipxp_lastdst, sipx->sipx_addr))
175 if (!ipx_hosteq(ipxp->ipxp_lastdst, sipx->sipx_addr)) {
176 if (ro->ro_rt != NULL && !(ro->ro_rt->rt_flags & RTF_HOST)) {
177 /* can patch route to avoid rtalloc */
178 *dst = sipx->sipx_addr;
181 if (ro->ro_rt != NULL)
185 }/* else cached route is ok; do nothing */
186 ipxp->ipxp_lastdst = sipx->sipx_addr;
187 if ((ipxp->ipxp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
188 (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL)) {
189 /* No route yet, so try to acquire one */
190 ro->ro_dst.sa_family = AF_IPX;
191 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
192 *dst = sipx->sipx_addr;
196 if (ipx_neteqnn(ipxp->ipxp_laddr.x_net, ipx_zeronet)) {
198 * If route is known or can be allocated now,
199 * our src addr is taken from the i/f, else punt.
203 * If we found a route, use the address
204 * corresponding to the outgoing interface
206 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL)
207 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
208 if (ia->ia_ifp == ifp)
211 u_short fport = sipx->sipx_addr.x_port;
212 sipx->sipx_addr.x_port = 0;
213 ia = (struct ipx_ifaddr *)
214 ifa_ifwithdstaddr((struct sockaddr *)sipx);
215 sipx->sipx_addr.x_port = fport;
217 ia = ipx_iaonnetof(&sipx->sipx_addr);
221 return (EADDRNOTAVAIL);
223 ipxp->ipxp_laddr.x_net = satoipx_addr(ia->ia_addr).x_net;
225 if (ipx_nullhost(ipxp->ipxp_laddr)) {
227 * If route is known or can be allocated now,
228 * our src addr is taken from the i/f, else punt.
232 * If we found a route, use the address
233 * corresponding to the outgoing interface
235 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL)
236 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
237 if (ia->ia_ifp == ifp)
240 u_short fport = sipx->sipx_addr.x_port;
241 sipx->sipx_addr.x_port = 0;
242 ia = (struct ipx_ifaddr *)
243 ifa_ifwithdstaddr((struct sockaddr *)sipx);
244 sipx->sipx_addr.x_port = fport;
246 ia = ipx_iaonnetof(&sipx->sipx_addr);
250 return (EADDRNOTAVAIL);
252 ipxp->ipxp_laddr.x_host = satoipx_addr(ia->ia_addr).x_host;
254 if (ipx_pcblookup(&sipx->sipx_addr, ipxp->ipxp_lport, 0))
256 if (ipxp->ipxp_lport == 0)
257 ipx_pcbbind(ipxp, (struct sockaddr *)NULL, td);
259 /* XXX just leave it zero if we can't find a route */
261 ipxp->ipxp_faddr = sipx->sipx_addr;
262 /* Includes ipxp->ipxp_fport = sipx->sipx_port; */
267 ipx_pcbdisconnect(ipxp)
271 IPX_LIST_LOCK_ASSERT();
272 IPX_LOCK_ASSERT(ipxp);
274 ipxp->ipxp_faddr = zeroipx_addr;
275 if (ipxp->ipxp_socket->so_state & SS_NOFDREF)
283 struct socket *so = ipxp->ipxp_socket;
285 IPX_LIST_LOCK_ASSERT();
286 IPX_LOCK_ASSERT(ipxp);
292 if (ipxp->ipxp_route.ro_rt != NULL)
293 RTFREE(ipxp->ipxp_route.ro_rt);
294 LIST_REMOVE(ipxp, ipxp_list);
295 IPX_LOCK_DESTROY(ipxp);
300 ipx_setsockaddr(ipxp, nam)
301 register struct ipxpcb *ipxp;
302 struct sockaddr **nam;
304 struct sockaddr_ipx *sipx, ssipx;
307 bzero((caddr_t)sipx, sizeof(*sipx));
308 sipx->sipx_len = sizeof(*sipx);
309 sipx->sipx_family = AF_IPX;
311 sipx->sipx_addr = ipxp->ipxp_laddr;
313 *nam = sodupsockaddr((struct sockaddr *)sipx, M_WAITOK);
317 ipx_setpeeraddr(ipxp, nam)
318 register struct ipxpcb *ipxp;
319 struct sockaddr **nam;
321 struct sockaddr_ipx *sipx, ssipx;
324 bzero(sipx, sizeof(*sipx));
325 sipx->sipx_len = sizeof(*sipx);
326 sipx->sipx_family = AF_IPX;
328 sipx->sipx_addr = ipxp->ipxp_faddr;
330 *nam = sodupsockaddr((struct sockaddr *)sipx, M_WAITOK);
334 ipx_pcblookup(faddr, lport, wildp)
335 struct ipx_addr *faddr;
339 register struct ipxpcb *ipxp, *match = NULL;
340 int matchwild = 3, wildcard;
343 IPX_LIST_LOCK_ASSERT();
345 fport = faddr->x_port;
346 LIST_FOREACH(ipxp, &ipxpcb_list, ipxp_list) {
347 if (ipxp->ipxp_lport != lport)
350 if (ipx_nullhost(ipxp->ipxp_faddr)) {
351 if (!ipx_nullhost(*faddr))
354 if (ipx_nullhost(*faddr))
357 if (!ipx_hosteq(ipxp->ipxp_faddr, *faddr))
359 if (ipxp->ipxp_fport != fport) {
360 if (ipxp->ipxp_fport != 0)
367 if (wildcard && wildp == 0)
369 if (wildcard < matchwild) {
371 matchwild = wildcard;