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. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/sys/netipx/ipx_pcb.c,v 1.18.2.1 2001/02/22 09:44:18 bp Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/thread2.h>
45 #include <net/route.h>
52 static struct ipx_addr zeroipx_addr;
53 static uint16_t ipxpcb_lport_cache;
56 ipx_pcballoc(struct socket *so, struct ipxpcbhead *head)
60 ipxp = kmalloc(sizeof *ipxp, M_PCB, M_WAITOK | M_ZERO | M_NULLOK);
63 ipxp->ipxp_socket = so;
65 ipxp->ipxp_flags |= IPXP_CHECKSUM;
66 LIST_INSERT_HEAD(head, ipxp, ipxp_list);
67 so->so_pcb = (caddr_t)ipxp;
72 ipx_pcbbind(struct ipxpcb *ipxp, struct sockaddr *nam, struct thread *td)
74 struct sockaddr_ipx *sipx;
77 if (ipxp->ipxp_lport || !ipx_nullhost(ipxp->ipxp_laddr))
81 sipx = (struct sockaddr_ipx *)nam;
82 if (!ipx_nullhost(sipx->sipx_addr)) {
83 int tport = sipx->sipx_port;
85 sipx->sipx_port = 0; /* yech... */
86 if (ifa_ifwithaddr((struct sockaddr *)sipx) == NULL)
87 return (EADDRNOTAVAIL);
88 sipx->sipx_port = tport;
90 lport = sipx->sipx_port;
92 u_short aport = ntohs(lport);
95 if (aport < IPXPORT_RESERVED &&
96 td != NULL && (error = priv_check(td, PRIV_ROOT)) != 0)
98 if (ipx_pcblookup(&zeroipx_addr, lport, 0))
101 ipxp->ipxp_laddr = sipx->sipx_addr;
105 ipxpcb_lport_cache++;
106 if ((ipxpcb_lport_cache < IPXPORT_RESERVED) ||
107 (ipxpcb_lport_cache >= IPXPORT_WELLKNOWN))
108 ipxpcb_lport_cache = IPXPORT_RESERVED;
109 lport = htons(ipxpcb_lport_cache);
110 } while (ipx_pcblookup(&zeroipx_addr, lport, 0));
111 ipxp->ipxp_lport = lport;
116 * Connect from a socket to a specified address.
117 * Both address and port must be specified in argument sipx.
118 * If don't have a local address for this socket yet,
122 ipx_pcbconnect(struct ipxpcb *ipxp, struct sockaddr *nam, struct thread *td)
124 struct ipx_ifaddr *ia;
125 struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)nam;
126 struct ipx_addr *dst;
132 if (sipx->sipx_family != AF_IPX)
133 return (EAFNOSUPPORT);
134 if (sipx->sipx_port == 0 || ipx_nullhost(sipx->sipx_addr))
135 return (EADDRNOTAVAIL);
137 * If we haven't bound which network number to use as ours,
138 * we will use the number of the outgoing interface.
139 * This depends on having done a routing lookup, which
140 * we will probably have to do anyway, so we might
141 * as well do it now. On the other hand if we are
142 * sending to multiple destinations we may have already
143 * done the lookup, so see if we can use the route
144 * from before. In any case, we only
145 * chose a port number once, even if sending to multiple
148 ro = &ipxp->ipxp_route;
149 dst = &satoipx_addr(ro->ro_dst);
150 if (ipxp->ipxp_socket->so_options & SO_DONTROUTE)
152 if (!ipx_neteq(ipxp->ipxp_lastdst, sipx->sipx_addr))
154 if (!ipx_hosteq(ipxp->ipxp_lastdst, sipx->sipx_addr)) {
155 if (ro->ro_rt != NULL && !(ro->ro_rt->rt_flags & RTF_HOST)) {
156 /* can patch route to avoid rtalloc */
157 *dst = sipx->sipx_addr;
160 if (ro->ro_rt != NULL)
164 }/* else cached route is ok; do nothing */
165 ipxp->ipxp_lastdst = sipx->sipx_addr;
166 if ((ipxp->ipxp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
167 (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL)) {
168 /* No route yet, so try to acquire one */
169 ro->ro_dst.sa_family = AF_IPX;
170 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
171 *dst = sipx->sipx_addr;
175 if (ipx_neteqnn(ipxp->ipxp_laddr.x_net, ipx_zeronet)) {
177 * If route is known or can be allocated now,
178 * our src addr is taken from the i/f, else punt.
182 * If we found a route, use the address
183 * corresponding to the outgoing interface
185 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL)
186 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
187 if (ia->ia_ifp == ifp)
190 u_short fport = sipx->sipx_addr.x_port;
191 sipx->sipx_addr.x_port = 0;
192 ia = (struct ipx_ifaddr *)
193 ifa_ifwithdstaddr((struct sockaddr *)sipx);
194 sipx->sipx_addr.x_port = fport;
196 ia = ipx_iaonnetof(&sipx->sipx_addr);
200 return (EADDRNOTAVAIL);
202 ipxp->ipxp_laddr.x_net = satoipx_addr(ia->ia_addr).x_net;
204 if (ipx_nullhost(ipxp->ipxp_laddr)) {
206 * If route is known or can be allocated now,
207 * our src addr is taken from the i/f, else punt.
211 * If we found a route, use the address
212 * corresponding to the outgoing interface
214 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL)
215 for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
216 if (ia->ia_ifp == ifp)
219 u_short fport = sipx->sipx_addr.x_port;
220 sipx->sipx_addr.x_port = 0;
221 ia = (struct ipx_ifaddr *)
222 ifa_ifwithdstaddr((struct sockaddr *)sipx);
223 sipx->sipx_addr.x_port = fport;
225 ia = ipx_iaonnetof(&sipx->sipx_addr);
229 return (EADDRNOTAVAIL);
231 ipxp->ipxp_laddr.x_host = satoipx_addr(ia->ia_addr).x_host;
233 if (ipx_pcblookup(&sipx->sipx_addr, ipxp->ipxp_lport, 0))
235 if (ipxp->ipxp_lport == 0)
236 ipx_pcbbind(ipxp, NULL, td);
238 /* XXX just leave it zero if we can't find a route */
240 ipxp->ipxp_faddr = sipx->sipx_addr;
241 /* Includes ipxp->ipxp_fport = sipx->sipx_port; */
246 ipx_pcbdisconnect(struct ipxpcb *ipxp)
249 ipxp->ipxp_faddr = zeroipx_addr;
250 if (ipxp->ipxp_socket->so_state & SS_NOFDREF)
255 ipx_pcbdetach(struct ipxpcb *ipxp)
257 struct socket *so = ipxp->ipxp_socket;
262 if (ipxp->ipxp_route.ro_rt != NULL)
263 rtfree(ipxp->ipxp_route.ro_rt);
264 LIST_REMOVE(ipxp, ipxp_list);
269 ipx_setsockaddr(struct ipxpcb *ipxp, struct sockaddr **nam)
271 struct sockaddr_ipx *sipx, ssipx;
274 bzero((caddr_t)sipx, sizeof(*sipx));
275 sipx->sipx_len = sizeof(*sipx);
276 sipx->sipx_family = AF_IPX;
277 sipx->sipx_addr = ipxp->ipxp_laddr;
278 *nam = dup_sockaddr((struct sockaddr *)sipx);
282 ipx_setpeeraddr(struct ipxpcb *ipxp, 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_faddr;
291 *nam = dup_sockaddr((struct sockaddr *)sipx);
295 ipx_pcblookup(struct ipx_addr *faddr, int lport, int wildp)
297 struct ipxpcb *ipxp, *match = NULL;
298 int matchwild = 3, wildcard;
301 fport = faddr->x_port;
302 LIST_FOREACH(ipxp, &ipxpcb_list, ipxp_list) {
303 if (ipxp->ipxp_lport != lport)
306 if (ipx_nullhost(ipxp->ipxp_faddr)) {
307 if (!ipx_nullhost(*faddr))
310 if (ipx_nullhost(*faddr))
313 if (!ipx_hosteq(ipxp->ipxp_faddr, *faddr))
315 if (ipxp->ipxp_fport != fport) {
316 if (ipxp->ipxp_fport != 0)
323 if (wildcard && wildp == 0)
325 if (wildcard < matchwild) {
327 matchwild = wildcard;