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