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
36 * $FreeBSD: src/sys/netipx/ipx_pcb.c,v 1.18.2.1 2001/02/22 09:44:18 bp Exp $
37 * $DragonFly: src/sys/netproto/ipx/ipx_pcb.c,v 1.2 2003/06/17 04:28:53 dillon Exp $
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #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;
58 ipx_pcballoc(so, head, p)
63 register struct ipxpcb *ipxp;
65 MALLOC(ipxp, struct ipxpcb *, sizeof *ipxp, M_PCB, M_NOWAIT | M_ZERO);
68 ipxp->ipxp_socket = so;
70 ipxp->ipxp_flags |= IPXP_CHECKSUM;
72 so->so_pcb = (caddr_t)ipxp;
77 ipx_pcbbind(ipxp, nam, p)
78 register struct ipxpcb *ipxp;
82 register struct sockaddr_ipx *sipx;
85 if (ipxp->ipxp_lport || !ipx_nullhost(ipxp->ipxp_laddr))
89 sipx = (struct sockaddr_ipx *)nam;
90 if (!ipx_nullhost(sipx->sipx_addr)) {
91 int tport = sipx->sipx_port;
93 sipx->sipx_port = 0; /* yech... */
94 if (ifa_ifwithaddr((struct sockaddr *)sipx) == 0)
95 return (EADDRNOTAVAIL);
96 sipx->sipx_port = tport;
98 lport = sipx->sipx_port;
100 u_short aport = ntohs(lport);
103 if (aport < IPXPORT_RESERVED &&
104 p != NULL && (error = suser(p)) != 0)
106 if (ipx_pcblookup(&zeroipx_addr, lport, 0))
109 ipxp->ipxp_laddr = sipx->sipx_addr;
114 if ((ipxpcb.ipxp_lport < IPXPORT_RESERVED) ||
115 (ipxpcb.ipxp_lport >= IPXPORT_WELLKNOWN))
116 ipxpcb.ipxp_lport = IPXPORT_RESERVED;
117 lport = htons(ipxpcb.ipxp_lport);
118 } while (ipx_pcblookup(&zeroipx_addr, lport, 0));
119 ipxp->ipxp_lport = lport;
124 * Connect from a socket to a specified address.
125 * Both address and port must be specified in argument sipx.
126 * If don't have a local address for this socket yet,
130 ipx_pcbconnect(ipxp, nam, p)
132 struct sockaddr *nam;
135 struct ipx_ifaddr *ia;
136 register struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)nam;
137 register struct ipx_addr *dst;
138 register struct route *ro;
143 if (sipx->sipx_family != AF_IPX)
144 return (EAFNOSUPPORT);
145 if (sipx->sipx_port == 0 || ipx_nullhost(sipx->sipx_addr))
146 return (EADDRNOTAVAIL);
148 * If we haven't bound which network number to use as ours,
149 * we will use the number of the outgoing interface.
150 * This depends on having done a routing lookup, which
151 * we will probably have to do anyway, so we might
152 * as well do it now. On the other hand if we are
153 * sending to multiple destinations we may have already
154 * done the lookup, so see if we can use the route
155 * from before. In any case, we only
156 * chose a port number once, even if sending to multiple
159 ro = &ipxp->ipxp_route;
160 dst = &satoipx_addr(ro->ro_dst);
161 if (ipxp->ipxp_socket->so_options & SO_DONTROUTE)
163 if (!ipx_neteq(ipxp->ipxp_lastdst, sipx->sipx_addr))
165 if (!ipx_hosteq(ipxp->ipxp_lastdst, sipx->sipx_addr)) {
166 if (ro->ro_rt != NULL && !(ro->ro_rt->rt_flags & RTF_HOST)) {
167 /* can patch route to avoid rtalloc */
168 *dst = sipx->sipx_addr;
171 if (ro->ro_rt != NULL)
175 }/* else cached route is ok; do nothing */
176 ipxp->ipxp_lastdst = sipx->sipx_addr;
177 if ((ipxp->ipxp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
178 (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL)) {
179 /* No route yet, so try to acquire one */
180 ro->ro_dst.sa_family = AF_IPX;
181 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
182 *dst = sipx->sipx_addr;
186 if (ipx_neteqnn(ipxp->ipxp_laddr.x_net, ipx_zeronet)) {
188 * If route is known or can be allocated now,
189 * our src addr is taken from the i/f, else punt.
193 * If we found a route, use the address
194 * corresponding to the outgoing interface
196 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL)
197 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
198 if (ia->ia_ifp == ifp)
201 u_short fport = sipx->sipx_addr.x_port;
202 sipx->sipx_addr.x_port = 0;
203 ia = (struct ipx_ifaddr *)
204 ifa_ifwithdstaddr((struct sockaddr *)sipx);
205 sipx->sipx_addr.x_port = fport;
207 ia = ipx_iaonnetof(&sipx->sipx_addr);
211 return (EADDRNOTAVAIL);
213 ipxp->ipxp_laddr.x_net = satoipx_addr(ia->ia_addr).x_net;
215 if (ipx_nullhost(ipxp->ipxp_laddr)) {
217 * If route is known or can be allocated now,
218 * our src addr is taken from the i/f, else punt.
222 * If we found a route, use the address
223 * corresponding to the outgoing interface
225 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL)
226 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
227 if (ia->ia_ifp == ifp)
230 u_short fport = sipx->sipx_addr.x_port;
231 sipx->sipx_addr.x_port = 0;
232 ia = (struct ipx_ifaddr *)
233 ifa_ifwithdstaddr((struct sockaddr *)sipx);
234 sipx->sipx_addr.x_port = fport;
236 ia = ipx_iaonnetof(&sipx->sipx_addr);
240 return (EADDRNOTAVAIL);
242 ipxp->ipxp_laddr.x_host = satoipx_addr(ia->ia_addr).x_host;
244 if (ipx_pcblookup(&sipx->sipx_addr, ipxp->ipxp_lport, 0))
246 if (ipxp->ipxp_lport == 0)
247 ipx_pcbbind(ipxp, (struct sockaddr *)NULL, p);
249 /* XXX just leave it zero if we can't find a route */
251 ipxp->ipxp_faddr = sipx->sipx_addr;
252 /* Includes ipxp->ipxp_fport = sipx->sipx_port; */
257 ipx_pcbdisconnect(ipxp)
261 ipxp->ipxp_faddr = zeroipx_addr;
262 if (ipxp->ipxp_socket->so_state & SS_NOFDREF)
270 struct socket *so = ipxp->ipxp_socket;
274 if (ipxp->ipxp_route.ro_rt != NULL)
275 rtfree(ipxp->ipxp_route.ro_rt);
281 ipx_setsockaddr(ipxp, nam)
282 register struct ipxpcb *ipxp;
283 struct sockaddr **nam;
285 struct sockaddr_ipx *sipx, ssipx;
288 bzero((caddr_t)sipx, sizeof(*sipx));
289 sipx->sipx_len = sizeof(*sipx);
290 sipx->sipx_family = AF_IPX;
291 sipx->sipx_addr = ipxp->ipxp_laddr;
292 *nam = dup_sockaddr((struct sockaddr *)sipx, 0);
296 ipx_setpeeraddr(ipxp, nam)
297 register struct ipxpcb *ipxp;
298 struct sockaddr **nam;
300 struct sockaddr_ipx *sipx, ssipx;
303 bzero((caddr_t)sipx, sizeof(*sipx));
304 sipx->sipx_len = sizeof(*sipx);
305 sipx->sipx_family = AF_IPX;
306 sipx->sipx_addr = ipxp->ipxp_faddr;
307 *nam = dup_sockaddr((struct sockaddr *)sipx, 0);
311 * Pass some notification to all connections of a protocol
312 * associated with address dst. Call the
313 * protocol specific routine to handle each connection.
314 * Also pass an extra paramter via the ipxpcb. (which may in fact
315 * be a parameter list!)
318 ipx_pcbnotify(dst, errno, notify, param)
319 register struct ipx_addr *dst;
321 void (*notify)(struct ipxpcb *);
324 register struct ipxpcb *ipxp, *oinp;
327 for (ipxp = (&ipxpcb)->ipxp_next; ipxp != (&ipxpcb);) {
328 if (!ipx_hosteq(*dst,ipxp->ipxp_faddr)) {
330 ipxp = ipxp->ipxp_next;
333 if (ipxp->ipxp_socket == 0)
336 ipxp->ipxp_socket->so_error = errno;
338 ipxp = ipxp->ipxp_next;
339 oinp->ipxp_notify_param = param;
347 * After a routing change, flush old routing
348 * and allocate a (hopefully) better one.
353 if (ipxp->ipxp_route.ro_rt != NULL) {
354 rtfree(ipxp->ipxp_route.ro_rt);
355 ipxp->ipxp_route.ro_rt = NULL;
357 * A new route can be allocated the next time
358 * output is attempted.
361 /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
366 ipx_pcblookup(faddr, lport, wildp)
367 struct ipx_addr *faddr;
371 register struct ipxpcb *ipxp, *match = 0;
372 int matchwild = 3, wildcard;
375 fport = faddr->x_port;
376 for (ipxp = (&ipxpcb)->ipxp_next; ipxp != (&ipxpcb); ipxp = ipxp->ipxp_next) {
377 if (ipxp->ipxp_lport != lport)
380 if (ipx_nullhost(ipxp->ipxp_faddr)) {
381 if (!ipx_nullhost(*faddr))
384 if (ipx_nullhost(*faddr))
387 if (!ipx_hosteq(ipxp->ipxp_faddr, *faddr))
389 if (ipxp->ipxp_fport != fport) {
390 if (ipxp->ipxp_fport != 0)
397 if (wildcard && wildp == 0)
399 if (wildcard < matchwild) {
401 matchwild = wildcard;