MP Implementation 1/2: Get the APIC code working again, sweetly integrate the
[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.2 2003/06/17 04:28:53 dillon Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/errno.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/protosw.h>
45
46 #include <net/if.h>
47 #include <net/route.h>
48
49 #include <netns/ns.h>
50 #include <netns/ns_if.h>
51 #include <netns/ns_pcb.h>
52
53 struct  ns_addr zerons_addr;
54
55 ns_pcballoc(so, head)
56         struct socket *so;
57         struct nspcb *head;
58 {
59         struct mbuf *m;
60         register struct nspcb *nsp;
61
62         m = m_getclr(M_DONTWAIT, MT_PCB);
63         if (m == NULL)
64                 return (ENOBUFS);
65         nsp = mtod(m, struct nspcb *);
66         nsp->nsp_socket = so;
67         insque(nsp, head);
68         so->so_pcb = (caddr_t)nsp;
69         return (0);
70 }
71
72 ns_pcbbind(nsp, nam)
73         register struct nspcb *nsp;
74         struct mbuf *nam;
75 {
76         register 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 == 0)
82                 goto noname;
83         sns = mtod(nam, struct sockaddr_ns *);
84         if (nam->m_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                 u_short aport = ntohs(lport);
97
98                 if (aport < NSPORT_RESERVED &&
99                     (nsp->nsp_socket->so_state & SS_PRIV) == 0)
100                         return (EACCES);
101                 if (ns_pcblookup(&zerons_addr, lport, 0))
102                         return (EADDRINUSE);
103         }
104         nsp->nsp_laddr = sns->sns_addr;
105 noname:
106         if (lport == 0)
107                 do {
108                         if (nspcb.nsp_lport++ < NSPORT_RESERVED)
109                                 nspcb.nsp_lport = NSPORT_RESERVED;
110                         lport = htons(nspcb.nsp_lport);
111                 } while (ns_pcblookup(&zerons_addr, lport, 0));
112         nsp->nsp_lport = lport;
113         return (0);
114 }
115
116 /*
117  * Connect from a socket to a specified address.
118  * Both address and port must be specified in argument sns.
119  * If don't have a local address for this socket yet,
120  * then pick one.
121  */
122 ns_pcbconnect(nsp, nam)
123         struct nspcb *nsp;
124         struct mbuf *nam;
125 {
126         struct ns_ifaddr *ia;
127         register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
128         register struct ns_addr *dst;
129         register struct route *ro;
130         struct ifnet *ifp;
131
132         if (nam->m_len != sizeof (*sns))
133                 return (EINVAL);
134         if (sns->sns_family != AF_NS)
135                 return (EAFNOSUPPORT);
136         if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
137                 return (EADDRNOTAVAIL);
138         /*
139          * If we haven't bound which network number to use as ours,
140          * we will use the number of the outgoing interface.
141          * This depends on having done a routing lookup, which
142          * we will probably have to do anyway, so we might
143          * as well do it now.  On the other hand if we are
144          * sending to multiple destinations we may have already
145          * done the lookup, so see if we can use the route
146          * from before.  In any case, we only
147          * chose a port number once, even if sending to multiple
148          * destinations.
149          */
150         ro = &nsp->nsp_route;
151         dst = &satons_addr(ro->ro_dst);
152         if (nsp->nsp_socket->so_options & SO_DONTROUTE)
153                 goto flush;
154         if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
155                 goto flush;
156         if (!ns_hosteq(nsp->nsp_lastdst, sns->sns_addr)) {
157                 if (ro->ro_rt && ! (ro->ro_rt->rt_flags & RTF_HOST)) {
158                         /* can patch route to avoid rtalloc */
159                         *dst = sns->sns_addr;
160                 } else {
161         flush:
162                         if (ro->ro_rt)
163                                 RTFREE(ro->ro_rt);
164                         ro->ro_rt = (struct rtentry *)0;
165                         nsp->nsp_laddr.x_net = ns_zeronet;
166                 }
167         }/* else cached route is ok; do nothing */
168         nsp->nsp_lastdst = sns->sns_addr;
169         if ((nsp->nsp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
170             (ro->ro_rt == (struct rtentry *)0 ||
171              ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
172                     /* No route yet, so try to acquire one */
173                     ro->ro_dst.sa_family = AF_NS;
174                     ro->ro_dst.sa_len = sizeof(ro->ro_dst);
175                     *dst = sns->sns_addr;
176                     dst->x_port = 0;
177                     rtalloc(ro);
178         }
179         if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_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                 ia = (struct ns_ifaddr *)0;
186                 /*
187                  * If we found a route, use the address
188                  * corresponding to the outgoing interface
189                  */
190                 if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp))
191                         for (ia = ns_ifaddr; ia; ia = ia->ia_next)
192                                 if (ia->ia_ifp == ifp)
193                                         break;
194                 if (ia == 0) {
195                         u_short fport = sns->sns_addr.x_port;
196                         sns->sns_addr.x_port = 0;
197                         ia = (struct ns_ifaddr *)
198                                 ifa_ifwithdstaddr((struct sockaddr *)sns);
199                         sns->sns_addr.x_port = fport;
200                         if (ia == 0)
201                                 ia = ns_iaonnetof(&sns->sns_addr);
202                         if (ia == 0)
203                                 ia = ns_ifaddr;
204                         if (ia == 0)
205                                 return (EADDRNOTAVAIL);
206                 }
207                 nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
208         }
209         if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
210                 return (EADDRINUSE);
211         if (ns_nullhost(nsp->nsp_laddr)) {
212                 if (nsp->nsp_lport == 0)
213                         (void) ns_pcbbind(nsp, (struct mbuf *)0);
214                 nsp->nsp_laddr.x_host = ns_thishost;
215         }
216         nsp->nsp_faddr = sns->sns_addr;
217         /* Includes nsp->nsp_fport = sns->sns_port; */
218         return (0);
219 }
220
221 ns_pcbdisconnect(nsp)
222         struct nspcb *nsp;
223 {
224
225         nsp->nsp_faddr = zerons_addr;
226         if (nsp->nsp_socket->so_state & SS_NOFDREF)
227                 ns_pcbdetach(nsp);
228 }
229
230 ns_pcbdetach(nsp)
231         struct nspcb *nsp;
232 {
233         struct socket *so = nsp->nsp_socket;
234
235         so->so_pcb = 0;
236         sofree(so);
237         if (nsp->nsp_route.ro_rt)
238                 rtfree(nsp->nsp_route.ro_rt);
239         remque(nsp);
240         (void) m_free(dtom(nsp));
241 }
242
243 ns_setsockaddr(nsp, nam)
244         register struct nspcb *nsp;
245         struct mbuf *nam;
246 {
247         register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
248
249         nam->m_len = sizeof (*sns);
250         sns = mtod(nam, struct sockaddr_ns *);
251         bzero((caddr_t)sns, sizeof (*sns));
252         sns->sns_len = sizeof(*sns);
253         sns->sns_family = AF_NS;
254         sns->sns_addr = nsp->nsp_laddr;
255 }
256
257 ns_setpeeraddr(nsp, nam)
258         register struct nspcb *nsp;
259         struct mbuf *nam;
260 {
261         register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
262
263         nam->m_len = sizeof (*sns);
264         sns = mtod(nam, struct sockaddr_ns *);
265         bzero((caddr_t)sns, sizeof (*sns));
266         sns->sns_len = sizeof(*sns);
267         sns->sns_family = AF_NS;
268         sns->sns_addr  = nsp->nsp_faddr;
269 }
270
271 /*
272  * Pass some notification to all connections of a protocol
273  * associated with address dst.  Call the
274  * protocol specific routine to handle each connection.
275  * Also pass an extra paramter via the nspcb. (which may in fact
276  * be a parameter list!)
277  */
278 ns_pcbnotify(dst, errno, notify, param)
279         register struct ns_addr *dst;
280         long param;
281         int errno, (*notify)();
282 {
283         register struct nspcb *nsp, *oinp;
284         int s = splimp();
285
286         for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb);) {
287                 if (!ns_hosteq(*dst,nsp->nsp_faddr)) {
288         next:
289                         nsp = nsp->nsp_next;
290                         continue;
291                 }
292                 if (nsp->nsp_socket == 0)
293                         goto next;
294                 if (errno)
295                         nsp->nsp_socket->so_error = errno;
296                 oinp = nsp;
297                 nsp = nsp->nsp_next;
298                 oinp->nsp_notify_param = param;
299                 (*notify)(oinp);
300         }
301         splx(s);
302 }
303
304 #ifdef notdef
305 /*
306  * After a routing change, flush old routing
307  * and allocate a (hopefully) better one.
308  */
309 ns_rtchange(nsp)
310         struct nspcb *nsp;
311 {
312         if (nsp->nsp_route.ro_rt) {
313                 rtfree(nsp->nsp_route.ro_rt);
314                 nsp->nsp_route.ro_rt = 0;
315                 /*
316                  * A new route can be allocated the next time
317                  * output is attempted.
318                  */
319         }
320         /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
321 }
322 #endif
323
324 struct nspcb *
325 ns_pcblookup(faddr, lport, wildp)
326         struct ns_addr *faddr;
327         u_short lport;
328 {
329         register struct nspcb *nsp, *match = 0;
330         int matchwild = 3, wildcard;
331         u_short fport;
332
333         fport = faddr->x_port;
334         for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb); nsp = nsp->nsp_next) {
335                 if (nsp->nsp_lport != lport)
336                         continue;
337                 wildcard = 0;
338                 if (ns_nullhost(nsp->nsp_faddr)) {
339                         if (!ns_nullhost(*faddr))
340                                 wildcard++;
341                 } else {
342                         if (ns_nullhost(*faddr))
343                                 wildcard++;
344                         else {
345                                 if (!ns_hosteq(nsp->nsp_faddr, *faddr))
346                                         continue;
347                                 if (nsp->nsp_fport != fport) {
348                                         if (nsp->nsp_fport != 0)
349                                                 continue;
350                                         else
351                                                 wildcard++;
352                                 }
353                         }
354                 }
355                 if (wildcard && wildp==0)
356                         continue;
357                 if (wildcard < matchwild) {
358                         match = nsp;
359                         matchwild = wildcard;
360                         if (wildcard == 0)
361                                 break;
362                 }
363         }
364         return (match);
365 }