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