Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[dragonfly.git] / sys / netproto / ipx / ipx_pcb.c
1 /*
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.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
17  *
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
28  * SUCH DAMAGE.
29  *
30  *      @(#)ipx_pcb.c
31  *
32  * $FreeBSD: src/sys/netipx/ipx_pcb.c,v 1.18.2.1 2001/02/22 09:44:18 bp Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/proc.h>
39 #include <sys/priv.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/thread2.h>
43
44 #include <net/if.h>
45 #include <net/route.h>
46
47 #include "ipx.h"
48 #include "ipx_if.h"
49 #include "ipx_pcb.h"
50 #include "ipx_var.h"
51
52 static struct   ipx_addr zeroipx_addr;
53 static uint16_t ipxpcb_lport_cache;
54
55 int
56 ipx_pcballoc(struct socket *so, struct ipxpcbhead *head)
57 {
58         struct ipxpcb *ipxp;
59
60         ipxp = kmalloc(sizeof *ipxp, M_PCB, M_WAITOK | M_ZERO | M_NULLOK);
61         if (ipxp == NULL)
62                 return (ENOBUFS);
63         ipxp->ipxp_socket = so;
64         if (ipxcksum)
65                 ipxp->ipxp_flags |= IPXP_CHECKSUM;
66         LIST_INSERT_HEAD(head, ipxp, ipxp_list);
67         so->so_pcb = (caddr_t)ipxp;
68         return (0);
69 }
70         
71 int
72 ipx_pcbbind(struct ipxpcb *ipxp, struct sockaddr *nam, struct thread *td)
73 {
74         struct sockaddr_ipx *sipx;
75         u_short lport = 0;
76
77         if (ipxp->ipxp_lport || !ipx_nullhost(ipxp->ipxp_laddr))
78                 return (EINVAL);
79         if (nam == NULL)
80                 goto noname;
81         sipx = (struct sockaddr_ipx *)nam;
82         if (!ipx_nullhost(sipx->sipx_addr)) {
83                 int tport = sipx->sipx_port;
84
85                 sipx->sipx_port = 0;            /* yech... */
86                 if (ifa_ifwithaddr((struct sockaddr *)sipx) == NULL)
87                         return (EADDRNOTAVAIL);
88                 sipx->sipx_port = tport;
89         }
90         lport = sipx->sipx_port;
91         if (lport) {
92                 u_short aport = ntohs(lport);
93                 int error;
94
95                 if (aport < IPXPORT_RESERVED &&
96                     td != NULL && (error = priv_check(td, PRIV_ROOT)) != 0)
97                         return (error);
98                 if (ipx_pcblookup(&zeroipx_addr, lport, 0))
99                         return (EADDRINUSE);
100         }
101         ipxp->ipxp_laddr = sipx->sipx_addr;
102 noname:
103         if (lport == 0)
104                 do {
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;
112         return (0);
113 }
114
115 /*
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,
119  * then pick one.
120  */
121 int
122 ipx_pcbconnect(struct ipxpcb *ipxp, struct sockaddr *nam, struct thread *td)
123 {
124         struct ipx_ifaddr *ia;
125         struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)nam;
126         struct ipx_addr *dst;
127         struct route *ro;
128         struct ifnet *ifp;
129
130         ia = NULL;
131
132         if (sipx->sipx_family != AF_IPX)
133                 return (EAFNOSUPPORT);
134         if (sipx->sipx_port == 0 || ipx_nullhost(sipx->sipx_addr))
135                 return (EADDRNOTAVAIL);
136         /*
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
146          * destinations.
147          */
148         ro = &ipxp->ipxp_route;
149         dst = &satoipx_addr(ro->ro_dst);
150         if (ipxp->ipxp_socket->so_options & SO_DONTROUTE)
151                 goto flush;
152         if (!ipx_neteq(ipxp->ipxp_lastdst, sipx->sipx_addr))
153                 goto flush;
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;
158                 } else {
159         flush:
160                         if (ro->ro_rt != NULL)
161                                 RTFREE(ro->ro_rt);
162                         ro->ro_rt = NULL;
163                 }
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;
172                     dst->x_port = 0;
173                     rtalloc(ro);
174         }
175         if (ipx_neteqnn(ipxp->ipxp_laddr.x_net, ipx_zeronet)) {
176                 /* 
177                  * If route is known or can be allocated now,
178                  * our src addr is taken from the i/f, else punt.
179                  */
180
181                 /*
182                  * If we found a route, use the address
183                  * corresponding to the outgoing interface
184                  */
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)
188                                         break;
189                 if (ia == NULL) {
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;
195                         if (ia == NULL)
196                                 ia = ipx_iaonnetof(&sipx->sipx_addr);
197                         if (ia == NULL)
198                                 ia = ipx_ifaddr;
199                         if (ia == NULL)
200                                 return (EADDRNOTAVAIL);
201                 }
202                 ipxp->ipxp_laddr.x_net = satoipx_addr(ia->ia_addr).x_net;
203         }
204         if (ipx_nullhost(ipxp->ipxp_laddr)) {
205                 /* 
206                  * If route is known or can be allocated now,
207                  * our src addr is taken from the i/f, else punt.
208                  */
209
210                 /*
211                  * If we found a route, use the address
212                  * corresponding to the outgoing interface
213                  */
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)
217                                         break;
218                 if (ia == NULL) {
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;
224                         if (ia == NULL)
225                                 ia = ipx_iaonnetof(&sipx->sipx_addr);
226                         if (ia == NULL)
227                                 ia = ipx_ifaddr;
228                         if (ia == NULL)
229                                 return (EADDRNOTAVAIL);
230                 }
231                 ipxp->ipxp_laddr.x_host = satoipx_addr(ia->ia_addr).x_host;
232         }
233         if (ipx_pcblookup(&sipx->sipx_addr, ipxp->ipxp_lport, 0))
234                 return (EADDRINUSE);
235         if (ipxp->ipxp_lport == 0)
236                 ipx_pcbbind(ipxp, NULL, td);
237
238         /* XXX just leave it zero if we can't find a route */
239
240         ipxp->ipxp_faddr = sipx->sipx_addr;
241         /* Includes ipxp->ipxp_fport = sipx->sipx_port; */
242         return (0);
243 }
244
245 void
246 ipx_pcbdisconnect(struct ipxpcb *ipxp)
247 {
248
249         ipxp->ipxp_faddr = zeroipx_addr;
250         if (ipxp->ipxp_socket->so_state & SS_NOFDREF)
251                 ipx_pcbdetach(ipxp);
252 }
253
254 void
255 ipx_pcbdetach(struct ipxpcb *ipxp)
256 {
257         struct socket *so = ipxp->ipxp_socket;
258
259         so->so_pcb = NULL;
260         sofree(so);
261
262         if (ipxp->ipxp_route.ro_rt != NULL)
263                 rtfree(ipxp->ipxp_route.ro_rt);
264         LIST_REMOVE(ipxp, ipxp_list);
265         kfree(ipxp, M_PCB);
266 }
267
268 void
269 ipx_setsockaddr(struct ipxpcb *ipxp, struct sockaddr **nam)
270 {
271         struct sockaddr_ipx *sipx, ssipx;
272         
273         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);
279 }
280
281 void
282 ipx_setpeeraddr(struct ipxpcb *ipxp, struct sockaddr **nam)
283 {
284         struct sockaddr_ipx *sipx, ssipx;
285         
286         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);
292 }
293
294 struct ipxpcb *
295 ipx_pcblookup(struct ipx_addr *faddr, int lport, int wildp)
296 {
297         struct ipxpcb *ipxp, *match = NULL;
298         int matchwild = 3, wildcard;
299         u_short fport;
300
301         fport = faddr->x_port;
302         LIST_FOREACH(ipxp, &ipxpcb_list, ipxp_list) {
303                 if (ipxp->ipxp_lport != lport)
304                         continue;
305                 wildcard = 0;
306                 if (ipx_nullhost(ipxp->ipxp_faddr)) {
307                         if (!ipx_nullhost(*faddr))
308                                 wildcard++;
309                 } else {
310                         if (ipx_nullhost(*faddr))
311                                 wildcard++;
312                         else {
313                                 if (!ipx_hosteq(ipxp->ipxp_faddr, *faddr))
314                                         continue;
315                                 if (ipxp->ipxp_fport != fport) {
316                                         if (ipxp->ipxp_fport != 0)
317                                                 continue;
318                                         else
319                                                 wildcard++;
320                                 }
321                         }
322                 }
323                 if (wildcard && wildp == 0)
324                         continue;
325                 if (wildcard < matchwild) {
326                         match = ipxp;
327                         matchwild = wildcard;
328                         if (wildcard == 0)
329                                 break;
330                 }
331         }
332         return (match);
333 }