2 * Copyright (c) 1995, Mike Mitchell
3 * Copyright (c) 1984, 1985, 1986, 1987, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the University of
17 * California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
47 #include <net/route.h>
49 #include <netipx/ipx.h>
50 #include <netipx/ipx_if.h>
51 #include <netipx/ipx_pcb.h>
52 #include <netipx/ipx_var.h>
54 static struct ipx_addr zeroipx_addr;
57 ipx_pcballoc(so, head, td)
62 register struct ipxpcb *ipxp;
64 MALLOC(ipxp, struct ipxpcb *, sizeof *ipxp, M_PCB, M_NOWAIT | M_ZERO);
67 ipxp->ipxp_socket = so;
69 ipxp->ipxp_flags |= IPXP_CHECKSUM;
71 so->so_pcb = (caddr_t)ipxp;
76 ipx_pcbbind(ipxp, nam, td)
77 register struct ipxpcb *ipxp;
81 register struct sockaddr_ipx *sipx;
84 if (ipxp->ipxp_lport || !ipx_nullhost(ipxp->ipxp_laddr))
88 sipx = (struct sockaddr_ipx *)nam;
89 if (!ipx_nullhost(sipx->sipx_addr)) {
90 int tport = sipx->sipx_port;
92 sipx->sipx_port = 0; /* yech... */
93 if (ifa_ifwithaddr((struct sockaddr *)sipx) == 0)
94 return (EADDRNOTAVAIL);
95 sipx->sipx_port = tport;
97 lport = sipx->sipx_port;
99 u_short aport = ntohs(lport);
102 if (aport < IPXPORT_RESERVED &&
103 td != NULL && (error = suser(td)) != 0)
105 if (ipx_pcblookup(&zeroipx_addr, lport, 0))
108 ipxp->ipxp_laddr = sipx->sipx_addr;
113 if ((ipxpcb.ipxp_lport < IPXPORT_RESERVED) ||
114 (ipxpcb.ipxp_lport >= IPXPORT_WELLKNOWN))
115 ipxpcb.ipxp_lport = IPXPORT_RESERVED;
116 lport = htons(ipxpcb.ipxp_lport);
117 } while (ipx_pcblookup(&zeroipx_addr, lport, 0));
118 ipxp->ipxp_lport = lport;
123 * Connect from a socket to a specified address.
124 * Both address and port must be specified in argument sipx.
125 * If don't have a local address for this socket yet,
129 ipx_pcbconnect(ipxp, nam, td)
131 struct sockaddr *nam;
134 struct ipx_ifaddr *ia;
135 register struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)nam;
136 register struct ipx_addr *dst;
137 register struct route *ro;
142 if (sipx->sipx_family != AF_IPX)
143 return (EAFNOSUPPORT);
144 if (sipx->sipx_port == 0 || ipx_nullhost(sipx->sipx_addr))
145 return (EADDRNOTAVAIL);
147 * If we haven't bound which network number to use as ours,
148 * we will use the number of the outgoing interface.
149 * This depends on having done a routing lookup, which
150 * we will probably have to do anyway, so we might
151 * as well do it now. On the other hand if we are
152 * sending to multiple destinations we may have already
153 * done the lookup, so see if we can use the route
154 * from before. In any case, we only
155 * chose a port number once, even if sending to multiple
158 ro = &ipxp->ipxp_route;
159 dst = &satoipx_addr(ro->ro_dst);
160 if (ipxp->ipxp_socket->so_options & SO_DONTROUTE)
162 if (!ipx_neteq(ipxp->ipxp_lastdst, sipx->sipx_addr))
164 if (!ipx_hosteq(ipxp->ipxp_lastdst, sipx->sipx_addr)) {
165 if (ro->ro_rt != NULL && !(ro->ro_rt->rt_flags & RTF_HOST)) {
166 /* can patch route to avoid rtalloc */
167 *dst = sipx->sipx_addr;
170 if (ro->ro_rt != NULL)
174 }/* else cached route is ok; do nothing */
175 ipxp->ipxp_lastdst = sipx->sipx_addr;
176 if ((ipxp->ipxp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
177 (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL)) {
178 /* No route yet, so try to acquire one */
179 ro->ro_dst.sa_family = AF_IPX;
180 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
181 *dst = sipx->sipx_addr;
185 if (ipx_neteqnn(ipxp->ipxp_laddr.x_net, ipx_zeronet)) {
187 * If route is known or can be allocated now,
188 * our src addr is taken from the i/f, else punt.
192 * If we found a route, use the address
193 * corresponding to the outgoing interface
195 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL)
196 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
197 if (ia->ia_ifp == ifp)
200 u_short fport = sipx->sipx_addr.x_port;
201 sipx->sipx_addr.x_port = 0;
202 ia = (struct ipx_ifaddr *)
203 ifa_ifwithdstaddr((struct sockaddr *)sipx);
204 sipx->sipx_addr.x_port = fport;
206 ia = ipx_iaonnetof(&sipx->sipx_addr);
210 return (EADDRNOTAVAIL);
212 ipxp->ipxp_laddr.x_net = satoipx_addr(ia->ia_addr).x_net;
214 if (ipx_nullhost(ipxp->ipxp_laddr)) {
216 * If route is known or can be allocated now,
217 * our src addr is taken from the i/f, else punt.
221 * If we found a route, use the address
222 * corresponding to the outgoing interface
224 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL)
225 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
226 if (ia->ia_ifp == ifp)
229 u_short fport = sipx->sipx_addr.x_port;
230 sipx->sipx_addr.x_port = 0;
231 ia = (struct ipx_ifaddr *)
232 ifa_ifwithdstaddr((struct sockaddr *)sipx);
233 sipx->sipx_addr.x_port = fport;
235 ia = ipx_iaonnetof(&sipx->sipx_addr);
239 return (EADDRNOTAVAIL);
241 ipxp->ipxp_laddr.x_host = satoipx_addr(ia->ia_addr).x_host;
243 if (ipx_pcblookup(&sipx->sipx_addr, ipxp->ipxp_lport, 0))
245 if (ipxp->ipxp_lport == 0)
246 ipx_pcbbind(ipxp, (struct sockaddr *)NULL, td);
248 /* XXX just leave it zero if we can't find a route */
250 ipxp->ipxp_faddr = sipx->sipx_addr;
251 /* Includes ipxp->ipxp_fport = sipx->sipx_port; */
256 ipx_pcbdisconnect(ipxp)
260 ipxp->ipxp_faddr = zeroipx_addr;
261 if (ipxp->ipxp_socket->so_state & SS_NOFDREF)
269 struct socket *so = ipxp->ipxp_socket;
273 if (ipxp->ipxp_route.ro_rt != NULL)
274 rtfree(ipxp->ipxp_route.ro_rt);
280 ipx_setsockaddr(ipxp, nam)
281 register struct ipxpcb *ipxp;
282 struct sockaddr **nam;
284 struct sockaddr_ipx *sipx, ssipx;
287 bzero((caddr_t)sipx, sizeof(*sipx));
288 sipx->sipx_len = sizeof(*sipx);
289 sipx->sipx_family = AF_IPX;
290 sipx->sipx_addr = ipxp->ipxp_laddr;
291 *nam = dup_sockaddr((struct sockaddr *)sipx, 0);
295 ipx_setpeeraddr(ipxp, nam)
296 register struct ipxpcb *ipxp;
297 struct sockaddr **nam;
299 struct sockaddr_ipx *sipx, ssipx;
302 bzero((caddr_t)sipx, sizeof(*sipx));
303 sipx->sipx_len = sizeof(*sipx);
304 sipx->sipx_family = AF_IPX;
305 sipx->sipx_addr = ipxp->ipxp_faddr;
306 *nam = dup_sockaddr((struct sockaddr *)sipx, 0);
310 * Pass some notification to all connections of a protocol
311 * associated with address dst. Call the
312 * protocol specific routine to handle each connection.
313 * Also pass an extra paramter via the ipxpcb. (which may in fact
314 * be a parameter list!)
317 ipx_pcbnotify(dst, errno, notify, param)
318 register struct ipx_addr *dst;
320 void (*notify)(struct ipxpcb *);
323 register struct ipxpcb *ipxp, *oinp;
326 for (ipxp = (&ipxpcb)->ipxp_next; ipxp != (&ipxpcb);) {
327 if (!ipx_hosteq(*dst,ipxp->ipxp_faddr)) {
329 ipxp = ipxp->ipxp_next;
332 if (ipxp->ipxp_socket == 0)
335 ipxp->ipxp_socket->so_error = errno;
337 ipxp = ipxp->ipxp_next;
338 oinp->ipxp_notify_param = param;
346 * After a routing change, flush old routing
347 * and allocate a (hopefully) better one.
352 if (ipxp->ipxp_route.ro_rt != NULL) {
353 rtfree(ipxp->ipxp_route.ro_rt);
354 ipxp->ipxp_route.ro_rt = NULL;
356 * A new route can be allocated the next time
357 * output is attempted.
360 /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
365 ipx_pcblookup(faddr, lport, wildp)
366 struct ipx_addr *faddr;
370 register struct ipxpcb *ipxp, *match = 0;
371 int matchwild = 3, wildcard;
374 fport = faddr->x_port;
375 for (ipxp = (&ipxpcb)->ipxp_next; ipxp != (&ipxpcb); ipxp = ipxp->ipxp_next) {
376 if (ipxp->ipxp_lport != lport)
379 if (ipx_nullhost(ipxp->ipxp_faddr)) {
380 if (!ipx_nullhost(*faddr))
383 if (ipx_nullhost(*faddr))
386 if (!ipx_hosteq(ipxp->ipxp_faddr, *faddr))
388 if (ipxp->ipxp_fport != fport) {
389 if (ipxp->ipxp_fport != 0)
396 if (wildcard && wildp == 0)
398 if (wildcard < matchwild) {
400 matchwild = wildcard;