remove bad semicolon
[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. 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.
21  *
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
32  * SUCH DAMAGE.
33  *
34  *      @(#)ipx_pcb.c
35  *
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.10 2004/06/06 19:16:09 dillon Exp $
38  */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/proc.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46
47 #include <net/if.h>
48 #include <net/route.h>
49
50 #include "ipx.h"
51 #include "ipx_if.h"
52 #include "ipx_pcb.h"
53 #include "ipx_var.h"
54
55 static struct   ipx_addr zeroipx_addr;
56
57 int
58 ipx_pcballoc(struct socket *so, struct ipxpcb *head)
59 {
60         struct ipxpcb *ipxp;
61
62         MALLOC(ipxp, struct ipxpcb *, sizeof *ipxp, M_PCB, 
63                 M_WAITOK | M_ZERO | M_NULLOK);
64         if (ipxp == NULL)
65                 return (ENOBUFS);
66         ipxp->ipxp_socket = so;
67         if (ipxcksum)
68                 ipxp->ipxp_flags |= IPXP_CHECKSUM;
69         insque(ipxp, head);
70         so->so_pcb = (caddr_t)ipxp;
71         return (0);
72 }
73         
74 int
75 ipx_pcbbind(struct ipxpcb *ipxp, struct sockaddr *nam, struct thread *td)
76 {
77         struct sockaddr_ipx *sipx;
78         u_short lport = 0;
79
80         if (ipxp->ipxp_lport || !ipx_nullhost(ipxp->ipxp_laddr))
81                 return (EINVAL);
82         if (nam == NULL)
83                 goto noname;
84         sipx = (struct sockaddr_ipx *)nam;
85         if (!ipx_nullhost(sipx->sipx_addr)) {
86                 int tport = sipx->sipx_port;
87
88                 sipx->sipx_port = 0;            /* yech... */
89                 if (ifa_ifwithaddr((struct sockaddr *)sipx) == 0)
90                         return (EADDRNOTAVAIL);
91                 sipx->sipx_port = tport;
92         }
93         lport = sipx->sipx_port;
94         if (lport) {
95                 u_short aport = ntohs(lport);
96                 int error;
97
98                 if (aport < IPXPORT_RESERVED &&
99                     td != NULL && (error = suser(td)) != 0)
100                         return (error);
101                 if (ipx_pcblookup(&zeroipx_addr, lport, 0))
102                         return (EADDRINUSE);
103         }
104         ipxp->ipxp_laddr = sipx->sipx_addr;
105 noname:
106         if (lport == 0)
107                 do {
108                         ipxpcb.ipxp_lport++;
109                         if ((ipxpcb.ipxp_lport < IPXPORT_RESERVED) ||
110                             (ipxpcb.ipxp_lport >= IPXPORT_WELLKNOWN))
111                                 ipxpcb.ipxp_lport = IPXPORT_RESERVED;
112                         lport = htons(ipxpcb.ipxp_lport);
113                 } while (ipx_pcblookup(&zeroipx_addr, lport, 0));
114         ipxp->ipxp_lport = lport;
115         return (0);
116 }
117
118 /*
119  * Connect from a socket to a specified address.
120  * Both address and port must be specified in argument sipx.
121  * If don't have a local address for this socket yet,
122  * then pick one.
123  */
124 int
125 ipx_pcbconnect(struct ipxpcb *ipxp, struct sockaddr *nam, struct thread *td)
126 {
127         struct ipx_ifaddr *ia;
128         struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)nam;
129         struct ipx_addr *dst;
130         struct route *ro;
131         struct ifnet *ifp;
132
133         ia = NULL;
134
135         if (sipx->sipx_family != AF_IPX)
136                 return (EAFNOSUPPORT);
137         if (sipx->sipx_port == 0 || ipx_nullhost(sipx->sipx_addr))
138                 return (EADDRNOTAVAIL);
139         /*
140          * If we haven't bound which network number to use as ours,
141          * we will use the number of the outgoing interface.
142          * This depends on having done a routing lookup, which
143          * we will probably have to do anyway, so we might
144          * as well do it now.  On the other hand if we are
145          * sending to multiple destinations we may have already
146          * done the lookup, so see if we can use the route
147          * from before.  In any case, we only
148          * chose a port number once, even if sending to multiple
149          * destinations.
150          */
151         ro = &ipxp->ipxp_route;
152         dst = &satoipx_addr(ro->ro_dst);
153         if (ipxp->ipxp_socket->so_options & SO_DONTROUTE)
154                 goto flush;
155         if (!ipx_neteq(ipxp->ipxp_lastdst, sipx->sipx_addr))
156                 goto flush;
157         if (!ipx_hosteq(ipxp->ipxp_lastdst, sipx->sipx_addr)) {
158                 if (ro->ro_rt != NULL && !(ro->ro_rt->rt_flags & RTF_HOST)) {
159                         /* can patch route to avoid rtalloc */
160                         *dst = sipx->sipx_addr;
161                 } else {
162         flush:
163                         if (ro->ro_rt != NULL)
164                                 RTFREE(ro->ro_rt);
165                         ro->ro_rt = NULL;
166                 }
167         }/* else cached route is ok; do nothing */
168         ipxp->ipxp_lastdst = sipx->sipx_addr;
169         if ((ipxp->ipxp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
170             (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL)) {
171                     /* No route yet, so try to acquire one */
172                     ro->ro_dst.sa_family = AF_IPX;
173                     ro->ro_dst.sa_len = sizeof(ro->ro_dst);
174                     *dst = sipx->sipx_addr;
175                     dst->x_port = 0;
176                     rtalloc(ro);
177         }
178         if (ipx_neteqnn(ipxp->ipxp_laddr.x_net, ipx_zeronet)) {
179                 /* 
180                  * If route is known or can be allocated now,
181                  * our src addr is taken from the i/f, else punt.
182                  */
183
184                 /*
185                  * If we found a route, use the address
186                  * corresponding to the outgoing interface
187                  */
188                 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL)
189                         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
190                                 if (ia->ia_ifp == ifp)
191                                         break;
192                 if (ia == NULL) {
193                         u_short fport = sipx->sipx_addr.x_port;
194                         sipx->sipx_addr.x_port = 0;
195                         ia = (struct ipx_ifaddr *)
196                                 ifa_ifwithdstaddr((struct sockaddr *)sipx);
197                         sipx->sipx_addr.x_port = fport;
198                         if (ia == NULL)
199                                 ia = ipx_iaonnetof(&sipx->sipx_addr);
200                         if (ia == NULL)
201                                 ia = ipx_ifaddr;
202                         if (ia == NULL)
203                                 return (EADDRNOTAVAIL);
204                 }
205                 ipxp->ipxp_laddr.x_net = satoipx_addr(ia->ia_addr).x_net;
206         }
207         if (ipx_nullhost(ipxp->ipxp_laddr)) {
208                 /* 
209                  * If route is known or can be allocated now,
210                  * our src addr is taken from the i/f, else punt.
211                  */
212
213                 /*
214                  * If we found a route, use the address
215                  * corresponding to the outgoing interface
216                  */
217                 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL)
218                         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
219                                 if (ia->ia_ifp == ifp)
220                                         break;
221                 if (ia == NULL) {
222                         u_short fport = sipx->sipx_addr.x_port;
223                         sipx->sipx_addr.x_port = 0;
224                         ia = (struct ipx_ifaddr *)
225                                 ifa_ifwithdstaddr((struct sockaddr *)sipx);
226                         sipx->sipx_addr.x_port = fport;
227                         if (ia == NULL)
228                                 ia = ipx_iaonnetof(&sipx->sipx_addr);
229                         if (ia == NULL)
230                                 ia = ipx_ifaddr;
231                         if (ia == NULL)
232                                 return (EADDRNOTAVAIL);
233                 }
234                 ipxp->ipxp_laddr.x_host = satoipx_addr(ia->ia_addr).x_host;
235         }
236         if (ipx_pcblookup(&sipx->sipx_addr, ipxp->ipxp_lport, 0))
237                 return (EADDRINUSE);
238         if (ipxp->ipxp_lport == 0)
239                 ipx_pcbbind(ipxp, (struct sockaddr *)NULL, td);
240
241         /* XXX just leave it zero if we can't find a route */
242
243         ipxp->ipxp_faddr = sipx->sipx_addr;
244         /* Includes ipxp->ipxp_fport = sipx->sipx_port; */
245         return (0);
246 }
247
248 void
249 ipx_pcbdisconnect(ipxp)
250         struct ipxpcb *ipxp;
251 {
252
253         ipxp->ipxp_faddr = zeroipx_addr;
254         if (ipxp->ipxp_socket->so_state & SS_NOFDREF)
255                 ipx_pcbdetach(ipxp);
256 }
257
258 void
259 ipx_pcbdetach(ipxp)
260         struct ipxpcb *ipxp;
261 {
262         struct socket *so = ipxp->ipxp_socket;
263
264         so->so_pcb = 0;
265         sofree(so);
266         if (ipxp->ipxp_route.ro_rt != NULL)
267                 rtfree(ipxp->ipxp_route.ro_rt);
268         remque(ipxp);
269         FREE(ipxp, M_PCB);
270 }
271
272 void
273 ipx_setsockaddr(struct ipxpcb *ipxp, struct sockaddr **nam)
274 {
275         struct sockaddr_ipx *sipx, ssipx;
276         
277         sipx = &ssipx;
278         bzero((caddr_t)sipx, sizeof(*sipx));
279         sipx->sipx_len = sizeof(*sipx);
280         sipx->sipx_family = AF_IPX;
281         sipx->sipx_addr = ipxp->ipxp_laddr;
282         *nam = dup_sockaddr((struct sockaddr *)sipx);
283 }
284
285 void
286 ipx_setpeeraddr(ipxp, nam)
287         struct ipxpcb *ipxp;
288         struct sockaddr **nam;
289 {
290         struct sockaddr_ipx *sipx, ssipx;
291         
292         sipx = &ssipx;
293         bzero((caddr_t)sipx, sizeof(*sipx));
294         sipx->sipx_len = sizeof(*sipx);
295         sipx->sipx_family = AF_IPX;
296         sipx->sipx_addr = ipxp->ipxp_faddr;
297         *nam = dup_sockaddr((struct sockaddr *)sipx);
298 }
299
300 /*
301  * Pass some notification to all connections of a protocol
302  * associated with address dst.  Call the
303  * protocol specific routine to handle each connection.
304  * Also pass an extra paramter via the ipxpcb. (which may in fact
305  * be a parameter list!)
306  */
307 void
308 ipx_pcbnotify(dst, errno, notify, param)
309         struct ipx_addr *dst;
310         int errno;
311         void (*notify)(struct ipxpcb *);
312         long param;
313 {
314         struct ipxpcb *ipxp, *oinp;
315         int s = splimp();
316
317         for (ipxp = (&ipxpcb)->ipxp_next; ipxp != (&ipxpcb);) {
318                 if (!ipx_hosteq(*dst,ipxp->ipxp_faddr)) {
319         next:
320                         ipxp = ipxp->ipxp_next;
321                         continue;
322                 }
323                 if (ipxp->ipxp_socket == 0)
324                         goto next;
325                 if (errno) 
326                         ipxp->ipxp_socket->so_error = errno;
327                 oinp = ipxp;
328                 ipxp = ipxp->ipxp_next;
329                 oinp->ipxp_notify_param = param;
330                 (*notify)(oinp);
331         }
332         splx(s);
333 }
334
335 #ifdef notdef
336 /*
337  * After a routing change, flush old routing
338  * and allocate a (hopefully) better one.
339  */
340 ipx_rtchange(ipxp)
341         struct ipxpcb *ipxp;
342 {
343         if (ipxp->ipxp_route.ro_rt != NULL) {
344                 rtfree(ipxp->ipxp_route.ro_rt);
345                 ipxp->ipxp_route.ro_rt = NULL;
346                 /*
347                  * A new route can be allocated the next time
348                  * output is attempted.
349                  */
350         }
351         /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
352 }
353 #endif
354
355 struct ipxpcb *
356 ipx_pcblookup(faddr, lport, wildp)
357         struct ipx_addr *faddr;
358         u_short lport;
359         int wildp;
360 {
361         struct ipxpcb *ipxp, *match = 0;
362         int matchwild = 3, wildcard;
363         u_short fport;
364
365         fport = faddr->x_port;
366         for (ipxp = (&ipxpcb)->ipxp_next; ipxp != (&ipxpcb); ipxp = ipxp->ipxp_next) {
367                 if (ipxp->ipxp_lport != lport)
368                         continue;
369                 wildcard = 0;
370                 if (ipx_nullhost(ipxp->ipxp_faddr)) {
371                         if (!ipx_nullhost(*faddr))
372                                 wildcard++;
373                 } else {
374                         if (ipx_nullhost(*faddr))
375                                 wildcard++;
376                         else {
377                                 if (!ipx_hosteq(ipxp->ipxp_faddr, *faddr))
378                                         continue;
379                                 if (ipxp->ipxp_fport != fport) {
380                                         if (ipxp->ipxp_fport != 0)
381                                                 continue;
382                                         else
383                                                 wildcard++;
384                                 }
385                         }
386                 }
387                 if (wildcard && wildp == 0)
388                         continue;
389                 if (wildcard < matchwild) {
390                         match = ipxp;
391                         matchwild = wildcard;
392                         if (wildcard == 0)
393                                 break;
394                 }
395         }
396         return (match);
397 }