Remove !_KERNEL parts.
[dragonfly.git] / sys / netproto / ns / ns_pcb.c
1 /*
2  * Copyright (c) 1984, 1985, 1986, 1987, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)ns_pcb.c    8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/netns/ns_pcb.c,v 1.9 1999/08/28 00:49:51 peter Exp $
35  * $DragonFly: src/sys/netproto/ns/ns_pcb.c,v 1.12 2004/06/07 07:04:33 dillon Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/mbuf.h>
42 #include <sys/errno.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/protosw.h>
46 #include <sys/module.h>
47 #include <sys/malloc.h>
48
49 #include <net/if.h>
50 #include <net/route.h>
51
52 #include "ns.h"
53 #include "ns_if.h"
54 #include "ns_pcb.h"
55
56 struct  ns_addr zerons_addr;
57 struct nspcb nspcb;             /* head of list */
58
59 static MALLOC_DEFINE(M_NSPCB, "nspcb", "NS PCB Management");
60
61 int
62 ns_pcballoc(struct socket *so, struct nspcb *head)
63 {
64         struct nspcb *nsp;
65
66         nsp = malloc(sizeof(struct nspcb), M_NSPCB, M_WAITOK|M_ZERO);
67         nsp->nsp_socket = so;
68         insque(nsp, head);
69         so->so_pcb = (caddr_t)nsp;
70         return (0);
71 }
72
73 int
74 ns_pcbbind(struct nspcb *nsp, struct sockaddr *nam)
75 {
76         struct sockaddr_ns *sns;
77         u_short lport = 0;
78
79         if (nsp->nsp_lport || !ns_nullhost(nsp->nsp_laddr))
80                 return (EINVAL);
81         if (nam == NULL)
82                 goto noname;
83         sns = (struct sockaddr_ns *)nam;
84         if (nam->sa_len != sizeof (*sns))
85                 return (EINVAL);
86         if (!ns_nullhost(sns->sns_addr)) {
87                 int tport = sns->sns_port;
88
89                 sns->sns_port = 0;              /* yech... */
90                 if (ifa_ifwithaddr((struct sockaddr *)sns) == 0)
91                         return (EADDRNOTAVAIL);
92                 sns->sns_port = tport;
93         }
94         lport = sns->sns_port;
95         if (lport) {
96 #ifdef  NS_PRIV_SOCKETS
97                 u_short aport = ntohs(lport);
98
99                 if (aport < NSPORT_RESERVED &&
100                     (nsp->nsp_socket->so_state & SS_PRIV) == 0)
101                         return (EACCES);
102 #endif  /* NS_PRIV_SOCKETS */
103
104                 if (ns_pcblookup(&zerons_addr, lport, 0))
105                         return (EADDRINUSE);
106         }
107         nsp->nsp_laddr = sns->sns_addr;
108 noname:
109         if (lport == 0)
110                 do {
111                         if (nspcb.nsp_lport++ < NSPORT_RESERVED)
112                                 nspcb.nsp_lport = NSPORT_RESERVED;
113                         lport = htons(nspcb.nsp_lport);
114                 } while (ns_pcblookup(&zerons_addr, lport, 0));
115         nsp->nsp_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 sns.
122  * If don't have a local address for this socket yet,
123  * then pick one.
124  */
125 int
126 ns_pcbconnect(struct nspcb *nsp, struct sockaddr *nam)
127 {
128         struct ns_ifaddr *ia;
129         struct sockaddr_ns *sns = (struct sockaddr_ns *)nam;
130         struct ns_addr *dst;
131         struct route *ro;
132         struct ifnet *ifp;
133
134         if (nam->sa_len != sizeof (*sns))
135                 return (EINVAL);
136         if (sns->sns_family != AF_NS)
137                 return (EAFNOSUPPORT);
138         if (sns->sns_port==0 || ns_nullhost(sns->sns_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 = &nsp->nsp_route;
153         dst = &satons_addr(ro->ro_dst);
154         if (nsp->nsp_socket->so_options & SO_DONTROUTE)
155                 goto flush;
156         if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
157                 goto flush;
158         if (!ns_hosteq(nsp->nsp_lastdst, sns->sns_addr)) {
159                 if (ro->ro_rt && ! (ro->ro_rt->rt_flags & RTF_HOST)) {
160                         /* can patch route to avoid rtalloc */
161                         *dst = sns->sns_addr;
162                 } else {
163         flush:
164                         if (ro->ro_rt)
165                                 RTFREE(ro->ro_rt);
166                         ro->ro_rt = (struct rtentry *)0;
167                         nsp->nsp_laddr.x_net = ns_zeronet;
168                 }
169         }/* else cached route is ok; do nothing */
170         nsp->nsp_lastdst = sns->sns_addr;
171         if ((nsp->nsp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
172             (ro->ro_rt == (struct rtentry *)0 ||
173              ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
174                     /* No route yet, so try to acquire one */
175                     ro->ro_dst.sa_family = AF_NS;
176                     ro->ro_dst.sa_len = sizeof(ro->ro_dst);
177                     *dst = sns->sns_addr;
178                     dst->x_port = 0;
179                     rtalloc(ro);
180         }
181         if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_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                 ia = (struct ns_ifaddr *)0;
188                 /*
189                  * If we found a route, use the address
190                  * corresponding to the outgoing interface
191                  */
192                 if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp))
193                         for (ia = ns_ifaddr; ia; ia = ia->ia_next)
194                                 if (ia->ia_ifp == ifp)
195                                         break;
196                 if (ia == 0) {
197                         u_short fport = sns->sns_addr.x_port;
198                         sns->sns_addr.x_port = 0;
199                         ia = (struct ns_ifaddr *)
200                                 ifa_ifwithdstaddr((struct sockaddr *)sns);
201                         sns->sns_addr.x_port = fport;
202                         if (ia == 0)
203                                 ia = ns_iaonnetof(&sns->sns_addr);
204                         if (ia == 0)
205                                 ia = ns_ifaddr;
206                         if (ia == 0)
207                                 return (EADDRNOTAVAIL);
208                 }
209                 nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
210         }
211         if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
212                 return (EADDRINUSE);
213         if (ns_nullhost(nsp->nsp_laddr)) {
214                 if (nsp->nsp_lport == 0)
215                         (void) ns_pcbbind(nsp, NULL);
216                 nsp->nsp_laddr.x_host = ns_thishost;
217         }
218         nsp->nsp_faddr = sns->sns_addr;
219         /* Includes nsp->nsp_fport = sns->sns_port; */
220         return (0);
221 }
222
223 void
224 ns_pcbdisconnect(struct nspcb *nsp)
225 {
226         nsp->nsp_faddr = zerons_addr;
227         if (nsp->nsp_socket->so_state & SS_NOFDREF)
228                 ns_pcbdetach(nsp);
229 }
230
231 void
232 ns_pcbdetach(struct nspcb *nsp)
233 {
234         struct socket *so = nsp->nsp_socket;
235
236         so->so_pcb = 0;
237         sofree(so);
238         if (nsp->nsp_route.ro_rt)
239                 rtfree(nsp->nsp_route.ro_rt);
240         remque(nsp);
241         free(nsp, M_NSPCB);
242 }
243
244 void
245 ns_setsockaddr(struct nspcb *nsp, struct sockaddr **pnam)
246 {
247         struct sockaddr_ns sns;
248
249         bzero(&sns, sizeof(sns));
250         sns.sns_len = sizeof(sns);
251         sns.sns_family = AF_NS;
252         sns.sns_addr = nsp->nsp_laddr;
253         *pnam = dup_sockaddr((struct sockaddr *)&sns);
254 }
255
256 void
257 ns_setpeeraddr(struct nspcb *nsp, struct sockaddr **pnam)
258 {
259         struct sockaddr_ns sns;
260
261         bzero(&sns, sizeof(sns));
262         sns.sns_len = sizeof(sns);
263         sns.sns_family = AF_NS;
264         sns.sns_addr  = nsp->nsp_faddr;
265         *pnam = dup_sockaddr((struct sockaddr *)&sns);
266 }
267
268 /*
269  * Pass some notification to all connections of a protocol
270  * associated with address dst.  Call the
271  * protocol specific routine to handle each connection.
272  * Also pass an extra paramter via the nspcb. (which may in fact
273  * be a parameter list!)
274  */
275 void
276 ns_pcbnotify(struct ns_addr *dst, int errno,
277             void (*notify)(struct nspcb *), long param)
278 {
279         struct nspcb *nsp, *oinp;
280         int s = splimp();
281
282         for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb);) {
283                 if (!ns_hosteq(*dst,nsp->nsp_faddr)) {
284         next:
285                         nsp = nsp->nsp_next;
286                         continue;
287                 }
288                 if (nsp->nsp_socket == 0)
289                         goto next;
290                 if (errno)
291                         nsp->nsp_socket->so_error = errno;
292                 oinp = nsp;
293                 nsp = nsp->nsp_next;
294                 oinp->nsp_notify_param = param;
295                 (*notify)(oinp);
296         }
297         splx(s);
298 }
299
300 #ifdef notdef
301 /*
302  * After a routing change, flush old routing
303  * and allocate a (hopefully) better one.
304  */
305 ns_rtchange(struct nspcb *nsp)
306 {
307         if (nsp->nsp_route.ro_rt) {
308                 rtfree(nsp->nsp_route.ro_rt);
309                 nsp->nsp_route.ro_rt = 0;
310                 /*
311                  * A new route can be allocated the next time
312                  * output is attempted.
313                  */
314         }
315         /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
316 }
317 #endif
318
319 struct nspcb *
320 ns_pcblookup(struct ns_addr *faddr, u_short lport, int wildp)
321 {
322         struct nspcb *nsp, *match = 0;
323         int matchwild = 3, wildcard;
324         u_short fport;
325
326         fport = faddr->x_port;
327         for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb); nsp = nsp->nsp_next) {
328                 if (nsp->nsp_lport != lport)
329                         continue;
330                 wildcard = 0;
331                 if (ns_nullhost(nsp->nsp_faddr)) {
332                         if (!ns_nullhost(*faddr))
333                                 wildcard++;
334                 } else {
335                         if (ns_nullhost(*faddr))
336                                 wildcard++;
337                         else {
338                                 if (!ns_hosteq(nsp->nsp_faddr, *faddr))
339                                         continue;
340                                 if (nsp->nsp_fport != fport) {
341                                         if (nsp->nsp_fport != 0)
342                                                 continue;
343                                         else
344                                                 wildcard++;
345                                 }
346                         }
347                 }
348                 if (wildcard && wildp==0)
349                         continue;
350                 if (wildcard < matchwild) {
351                         match = nsp;
352                         matchwild = wildcard;
353                         if (wildcard == 0)
354                                 break;
355                 }
356         }
357         return (match);
358 }