Initial import of binutils 2.22 on the new vendor branch
[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 static uint16_t ipxpcb_lport_cache;
59
60 int
61 ipx_pcballoc(struct socket *so, struct ipxpcbhead *head)
62 {
63         struct ipxpcb *ipxp;
64
65         MALLOC(ipxp, struct ipxpcb *, sizeof *ipxp, M_PCB, 
66                 M_WAITOK | M_ZERO | M_NULLOK);
67         if (ipxp == NULL)
68                 return (ENOBUFS);
69         ipxp->ipxp_socket = so;
70         if (ipxcksum)
71                 ipxp->ipxp_flags |= IPXP_CHECKSUM;
72         LIST_INSERT_HEAD(head, ipxp, ipxp_list);
73         so->so_pcb = (caddr_t)ipxp;
74         return (0);
75 }
76         
77 int
78 ipx_pcbbind(struct ipxpcb *ipxp, struct sockaddr *nam, struct thread *td)
79 {
80         struct sockaddr_ipx *sipx;
81         u_short lport = 0;
82
83         if (ipxp->ipxp_lport || !ipx_nullhost(ipxp->ipxp_laddr))
84                 return (EINVAL);
85         if (nam == NULL)
86                 goto noname;
87         sipx = (struct sockaddr_ipx *)nam;
88         if (!ipx_nullhost(sipx->sipx_addr)) {
89                 int tport = sipx->sipx_port;
90
91                 sipx->sipx_port = 0;            /* yech... */
92                 if (ifa_ifwithaddr((struct sockaddr *)sipx) == 0)
93                         return (EADDRNOTAVAIL);
94                 sipx->sipx_port = tport;
95         }
96         lport = sipx->sipx_port;
97         if (lport) {
98                 u_short aport = ntohs(lport);
99                 int error;
100
101                 if (aport < IPXPORT_RESERVED &&
102                     td != NULL && (error = priv_check(td, PRIV_ROOT)) != 0)
103                         return (error);
104                 if (ipx_pcblookup(&zeroipx_addr, lport, 0))
105                         return (EADDRINUSE);
106         }
107         ipxp->ipxp_laddr = sipx->sipx_addr;
108 noname:
109         if (lport == 0)
110                 do {
111                         ipxpcb_lport_cache++;
112                         if ((ipxpcb_lport_cache < IPXPORT_RESERVED) ||
113                             (ipxpcb_lport_cache >= IPXPORT_WELLKNOWN))
114                                 ipxpcb_lport_cache = IPXPORT_RESERVED;
115                         lport = htons(ipxpcb_lport_cache);
116                 } while (ipx_pcblookup(&zeroipx_addr, lport, 0));
117         ipxp->ipxp_lport = lport;
118         return (0);
119 }
120
121 /*
122  * Connect from a socket to a specified address.
123  * Both address and port must be specified in argument sipx.
124  * If don't have a local address for this socket yet,
125  * then pick one.
126  */
127 int
128 ipx_pcbconnect(struct ipxpcb *ipxp, struct sockaddr *nam, struct thread *td)
129 {
130         struct ipx_ifaddr *ia;
131         struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)nam;
132         struct ipx_addr *dst;
133         struct route *ro;
134         struct ifnet *ifp;
135
136         ia = NULL;
137
138         if (sipx->sipx_family != AF_IPX)
139                 return (EAFNOSUPPORT);
140         if (sipx->sipx_port == 0 || ipx_nullhost(sipx->sipx_addr))
141                 return (EADDRNOTAVAIL);
142         /*
143          * If we haven't bound which network number to use as ours,
144          * we will use the number of the outgoing interface.
145          * This depends on having done a routing lookup, which
146          * we will probably have to do anyway, so we might
147          * as well do it now.  On the other hand if we are
148          * sending to multiple destinations we may have already
149          * done the lookup, so see if we can use the route
150          * from before.  In any case, we only
151          * chose a port number once, even if sending to multiple
152          * destinations.
153          */
154         ro = &ipxp->ipxp_route;
155         dst = &satoipx_addr(ro->ro_dst);
156         if (ipxp->ipxp_socket->so_options & SO_DONTROUTE)
157                 goto flush;
158         if (!ipx_neteq(ipxp->ipxp_lastdst, sipx->sipx_addr))
159                 goto flush;
160         if (!ipx_hosteq(ipxp->ipxp_lastdst, sipx->sipx_addr)) {
161                 if (ro->ro_rt != NULL && !(ro->ro_rt->rt_flags & RTF_HOST)) {
162                         /* can patch route to avoid rtalloc */
163                         *dst = sipx->sipx_addr;
164                 } else {
165         flush:
166                         if (ro->ro_rt != NULL)
167                                 RTFREE(ro->ro_rt);
168                         ro->ro_rt = NULL;
169                 }
170         }/* else cached route is ok; do nothing */
171         ipxp->ipxp_lastdst = sipx->sipx_addr;
172         if ((ipxp->ipxp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
173             (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL)) {
174                     /* No route yet, so try to acquire one */
175                     ro->ro_dst.sa_family = AF_IPX;
176                     ro->ro_dst.sa_len = sizeof(ro->ro_dst);
177                     *dst = sipx->sipx_addr;
178                     dst->x_port = 0;
179                     rtalloc(ro);
180         }
181         if (ipx_neteqnn(ipxp->ipxp_laddr.x_net, ipx_zeronet)) {
182                 /* 
183                  * If route is known or can be allocated now,
184                  * our src addr is taken from the i/f, else punt.
185                  */
186
187                 /*
188                  * If we found a route, use the address
189                  * corresponding to the outgoing interface
190                  */
191                 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL)
192                         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
193                                 if (ia->ia_ifp == ifp)
194                                         break;
195                 if (ia == NULL) {
196                         u_short fport = sipx->sipx_addr.x_port;
197                         sipx->sipx_addr.x_port = 0;
198                         ia = (struct ipx_ifaddr *)
199                                 ifa_ifwithdstaddr((struct sockaddr *)sipx);
200                         sipx->sipx_addr.x_port = fport;
201                         if (ia == NULL)
202                                 ia = ipx_iaonnetof(&sipx->sipx_addr);
203                         if (ia == NULL)
204                                 ia = ipx_ifaddr;
205                         if (ia == NULL)
206                                 return (EADDRNOTAVAIL);
207                 }
208                 ipxp->ipxp_laddr.x_net = satoipx_addr(ia->ia_addr).x_net;
209         }
210         if (ipx_nullhost(ipxp->ipxp_laddr)) {
211                 /* 
212                  * If route is known or can be allocated now,
213                  * our src addr is taken from the i/f, else punt.
214                  */
215
216                 /*
217                  * If we found a route, use the address
218                  * corresponding to the outgoing interface
219                  */
220                 if (ro->ro_rt != NULL && (ifp = ro->ro_rt->rt_ifp) != NULL)
221                         for (ia = ipx_ifaddr; ia != NULL; ia = ia->ia_next)
222                                 if (ia->ia_ifp == ifp)
223                                         break;
224                 if (ia == NULL) {
225                         u_short fport = sipx->sipx_addr.x_port;
226                         sipx->sipx_addr.x_port = 0;
227                         ia = (struct ipx_ifaddr *)
228                                 ifa_ifwithdstaddr((struct sockaddr *)sipx);
229                         sipx->sipx_addr.x_port = fport;
230                         if (ia == NULL)
231                                 ia = ipx_iaonnetof(&sipx->sipx_addr);
232                         if (ia == NULL)
233                                 ia = ipx_ifaddr;
234                         if (ia == NULL)
235                                 return (EADDRNOTAVAIL);
236                 }
237                 ipxp->ipxp_laddr.x_host = satoipx_addr(ia->ia_addr).x_host;
238         }
239         if (ipx_pcblookup(&sipx->sipx_addr, ipxp->ipxp_lport, 0))
240                 return (EADDRINUSE);
241         if (ipxp->ipxp_lport == 0)
242                 ipx_pcbbind(ipxp, NULL, td);
243
244         /* XXX just leave it zero if we can't find a route */
245
246         ipxp->ipxp_faddr = sipx->sipx_addr;
247         /* Includes ipxp->ipxp_fport = sipx->sipx_port; */
248         return (0);
249 }
250
251 void
252 ipx_pcbdisconnect(struct ipxpcb *ipxp)
253 {
254
255         ipxp->ipxp_faddr = zeroipx_addr;
256         if (ipxp->ipxp_socket->so_state & SS_NOFDREF)
257                 ipx_pcbdetach(ipxp);
258 }
259
260 void
261 ipx_pcbdetach(struct ipxpcb *ipxp)
262 {
263         struct socket *so = ipxp->ipxp_socket;
264
265         so->so_pcb = NULL;
266         sofree(so);
267
268         if (ipxp->ipxp_route.ro_rt != NULL)
269                 rtfree(ipxp->ipxp_route.ro_rt);
270         LIST_REMOVE(ipxp, ipxp_list);
271         FREE(ipxp, M_PCB);
272 }
273
274 void
275 ipx_setsockaddr(struct ipxpcb *ipxp, struct sockaddr **nam)
276 {
277         struct sockaddr_ipx *sipx, ssipx;
278         
279         sipx = &ssipx;
280         bzero((caddr_t)sipx, sizeof(*sipx));
281         sipx->sipx_len = sizeof(*sipx);
282         sipx->sipx_family = AF_IPX;
283         sipx->sipx_addr = ipxp->ipxp_laddr;
284         *nam = dup_sockaddr((struct sockaddr *)sipx);
285 }
286
287 void
288 ipx_setpeeraddr(struct ipxpcb *ipxp, 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 struct ipxpcb *
301 ipx_pcblookup(struct ipx_addr *faddr, int lport, int wildp)
302 {
303         struct ipxpcb *ipxp, *match = 0;
304         int matchwild = 3, wildcard;
305         u_short fport;
306
307         fport = faddr->x_port;
308         LIST_FOREACH(ipxp, &ipxpcb_list, ipxp_list) {
309                 if (ipxp->ipxp_lport != lport)
310                         continue;
311                 wildcard = 0;
312                 if (ipx_nullhost(ipxp->ipxp_faddr)) {
313                         if (!ipx_nullhost(*faddr))
314                                 wildcard++;
315                 } else {
316                         if (ipx_nullhost(*faddr))
317                                 wildcard++;
318                         else {
319                                 if (!ipx_hosteq(ipxp->ipxp_faddr, *faddr))
320                                         continue;
321                                 if (ipxp->ipxp_fport != fport) {
322                                         if (ipxp->ipxp_fport != 0)
323                                                 continue;
324                                         else
325                                                 wildcard++;
326                                 }
327                         }
328                 }
329                 if (wildcard && wildp == 0)
330                         continue;
331                 if (wildcard < matchwild) {
332                         match = ipxp;
333                         matchwild = wildcard;
334                         if (wildcard == 0)
335                                 break;
336                 }
337         }
338         return (match);
339 }