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