Generally use NULL instead of explicitly casting 0 to some pointer type.
[games.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.15 2006/09/05 00:55:49 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 #include <sys/thread2.h>
49
50 #include <net/if.h>
51 #include <net/route.h>
52
53 #include "ns.h"
54 #include "ns_if.h"
55 #include "ns_pcb.h"
56
57 struct  ns_addr zerons_addr;
58 struct nspcb nspcb;             /* head of list */
59
60 static MALLOC_DEFINE(M_NSPCB, "nspcb", "NS PCB Management");
61
62 int
63 ns_pcballoc(struct socket *so, struct nspcb *head)
64 {
65         struct nspcb *nsp;
66
67         nsp = kmalloc(sizeof(struct nspcb), M_NSPCB, M_WAITOK|M_ZERO);
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(struct nspcb *nsp, struct sockaddr *nam)
76 {
77         struct sockaddr_ns *sns;
78         u_short lport = 0;
79
80         if (nsp->nsp_lport || !ns_nullhost(nsp->nsp_laddr))
81                 return (EINVAL);
82         if (nam == NULL)
83                 goto noname;
84         sns = (struct sockaddr_ns *)nam;
85         if (nam->sa_len != sizeof (*sns))
86                 return (EINVAL);
87         if (!ns_nullhost(sns->sns_addr)) {
88                 int tport = sns->sns_port;
89
90                 sns->sns_port = 0;              /* yech... */
91                 if (ifa_ifwithaddr((struct sockaddr *)sns) == 0)
92                         return (EADDRNOTAVAIL);
93                 sns->sns_port = tport;
94         }
95         lport = sns->sns_port;
96         if (lport) {
97 #ifdef  NS_PRIV_SOCKETS
98                 u_short aport = ntohs(lport);
99
100                 if (aport < NSPORT_RESERVED &&
101                     (nsp->nsp_socket->so_state & SS_PRIV) == 0)
102                         return (EACCES);
103 #endif  /* NS_PRIV_SOCKETS */
104
105                 if (ns_pcblookup(&zerons_addr, lport, 0))
106                         return (EADDRINUSE);
107         }
108         nsp->nsp_laddr = sns->sns_addr;
109 noname:
110         if (lport == 0)
111                 do {
112                         if (nspcb.nsp_lport++ < NSPORT_RESERVED)
113                                 nspcb.nsp_lport = NSPORT_RESERVED;
114                         lport = htons(nspcb.nsp_lport);
115                 } while (ns_pcblookup(&zerons_addr, lport, 0));
116         nsp->nsp_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 sns.
123  * If don't have a local address for this socket yet,
124  * then pick one.
125  */
126 int
127 ns_pcbconnect(struct nspcb *nsp, struct sockaddr *nam)
128 {
129         struct ns_ifaddr *ia;
130         struct sockaddr_ns *sns = (struct sockaddr_ns *)nam;
131         struct ns_addr *dst;
132         struct route *ro;
133         struct ifnet *ifp;
134
135         if (nam->sa_len != sizeof (*sns))
136                 return (EINVAL);
137         if (sns->sns_family != AF_NS)
138                 return (EAFNOSUPPORT);
139         if (sns->sns_port==0 || ns_nullhost(sns->sns_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 = &nsp->nsp_route;
154         dst = &satons_addr(ro->ro_dst);
155         if (nsp->nsp_socket->so_options & SO_DONTROUTE)
156                 goto flush;
157         if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
158                 goto flush;
159         if (!ns_hosteq(nsp->nsp_lastdst, sns->sns_addr)) {
160                 if (ro->ro_rt && ! (ro->ro_rt->rt_flags & RTF_HOST)) {
161                         /* can patch route to avoid rtalloc */
162                         *dst = sns->sns_addr;
163                 } else {
164         flush:
165                         if (ro->ro_rt)
166                                 RTFREE(ro->ro_rt);
167                         ro->ro_rt = NULL;
168                         nsp->nsp_laddr.x_net = ns_zeronet;
169                 }
170         }/* else cached route is ok; do nothing */
171         nsp->nsp_lastdst = sns->sns_addr;
172         if ((nsp->nsp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
173             (ro->ro_rt == NULL ||
174              ro->ro_rt->rt_ifp == NULL)) {
175                     /* No route yet, so try to acquire one */
176                     ro->ro_dst.sa_family = AF_NS;
177                     ro->ro_dst.sa_len = sizeof(ro->ro_dst);
178                     *dst = sns->sns_addr;
179                     dst->x_port = 0;
180                     rtalloc(ro);
181         }
182         if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_zeronet)) {
183                 /*
184                  * If route is known or can be allocated now,
185                  * our src addr is taken from the i/f, else punt.
186                  */
187
188                 ia = NULL;
189                 /*
190                  * If we found a route, use the address
191                  * corresponding to the outgoing interface
192                  */
193                 if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp))
194                         for (ia = ns_ifaddr; ia; ia = ia->ia_next)
195                                 if (ia->ia_ifp == ifp)
196                                         break;
197                 if (ia == 0) {
198                         u_short fport = sns->sns_addr.x_port;
199                         sns->sns_addr.x_port = 0;
200                         ia = (struct ns_ifaddr *)
201                                 ifa_ifwithdstaddr((struct sockaddr *)sns);
202                         sns->sns_addr.x_port = fport;
203                         if (ia == 0)
204                                 ia = ns_iaonnetof(&sns->sns_addr);
205                         if (ia == 0)
206                                 ia = ns_ifaddr;
207                         if (ia == 0)
208                                 return (EADDRNOTAVAIL);
209                 }
210                 nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
211         }
212         if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
213                 return (EADDRINUSE);
214         if (ns_nullhost(nsp->nsp_laddr)) {
215                 if (nsp->nsp_lport == 0)
216                         ns_pcbbind(nsp, NULL);
217                 nsp->nsp_laddr.x_host = ns_thishost;
218         }
219         nsp->nsp_faddr = sns->sns_addr;
220         /* Includes nsp->nsp_fport = sns->sns_port; */
221         return (0);
222 }
223
224 void
225 ns_pcbdisconnect(struct nspcb *nsp)
226 {
227         nsp->nsp_faddr = zerons_addr;
228         if (nsp->nsp_socket->so_state & SS_NOFDREF)
229                 ns_pcbdetach(nsp);
230 }
231
232 void
233 ns_pcbdetach(struct nspcb *nsp)
234 {
235         struct socket *so = nsp->nsp_socket;
236
237         so->so_pcb = 0;
238         sofree(so);
239         if (nsp->nsp_route.ro_rt)
240                 rtfree(nsp->nsp_route.ro_rt);
241         remque(nsp);
242         kfree(nsp, M_NSPCB);
243 }
244
245 void
246 ns_setsockaddr(struct nspcb *nsp, struct sockaddr **pnam)
247 {
248         struct sockaddr_ns sns;
249
250         bzero(&sns, sizeof(sns));
251         sns.sns_len = sizeof(sns);
252         sns.sns_family = AF_NS;
253         sns.sns_addr = nsp->nsp_laddr;
254         *pnam = dup_sockaddr((struct sockaddr *)&sns);
255 }
256
257 void
258 ns_setpeeraddr(struct nspcb *nsp, struct sockaddr **pnam)
259 {
260         struct sockaddr_ns sns;
261
262         bzero(&sns, sizeof(sns));
263         sns.sns_len = sizeof(sns);
264         sns.sns_family = AF_NS;
265         sns.sns_addr  = nsp->nsp_faddr;
266         *pnam = dup_sockaddr((struct sockaddr *)&sns);
267 }
268
269 /*
270  * Pass some notification to all connections of a protocol
271  * associated with address dst.  Call the
272  * protocol specific routine to handle each connection.
273  * Also pass an extra paramter via the nspcb. (which may in fact
274  * be a parameter list!)
275  */
276 void
277 ns_pcbnotify(struct ns_addr *dst, int errno,
278             void (*notify)(struct nspcb *), long param)
279 {
280         struct nspcb *nsp, *oinp;
281
282         crit_enter();
283
284         for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb);) {
285                 if (!ns_hosteq(*dst,nsp->nsp_faddr)) {
286         next:
287                         nsp = nsp->nsp_next;
288                         continue;
289                 }
290                 if (nsp->nsp_socket == 0)
291                         goto next;
292                 if (errno)
293                         nsp->nsp_socket->so_error = errno;
294                 oinp = nsp;
295                 nsp = nsp->nsp_next;
296                 oinp->nsp_notify_param = param;
297                 (*notify)(oinp);
298         }
299         crit_exit();
300 }
301
302 #ifdef notdef
303 /*
304  * After a routing change, flush old routing
305  * and allocate a (hopefully) better one.
306  */
307 void
308 ns_rtchange(struct nspcb *nsp)
309 {
310         if (nsp->nsp_route.ro_rt) {
311                 rtfree(nsp->nsp_route.ro_rt);
312                 nsp->nsp_route.ro_rt = 0;
313                 /*
314                  * A new route can be allocated the next time
315                  * output is attempted.
316                  */
317         }
318         /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
319 }
320 #endif
321
322 struct nspcb *
323 ns_pcblookup(struct ns_addr *faddr, u_short lport, int wildp)
324 {
325         struct nspcb *nsp, *match = 0;
326         int matchwild = 3, wildcard;
327         u_short fport;
328
329         fport = faddr->x_port;
330         for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb); nsp = nsp->nsp_next) {
331                 if (nsp->nsp_lport != lport)
332                         continue;
333                 wildcard = 0;
334                 if (ns_nullhost(nsp->nsp_faddr)) {
335                         if (!ns_nullhost(*faddr))
336                                 wildcard++;
337                 } else {
338                         if (ns_nullhost(*faddr))
339                                 wildcard++;
340                         else {
341                                 if (!ns_hosteq(nsp->nsp_faddr, *faddr))
342                                         continue;
343                                 if (nsp->nsp_fport != fport) {
344                                         if (nsp->nsp_fport != 0)
345                                                 continue;
346                                         else
347                                                 wildcard++;
348                                 }
349                         }
350                 }
351                 if (wildcard && wildp==0)
352                         continue;
353                 if (wildcard < matchwild) {
354                         match = nsp;
355                         matchwild = wildcard;
356                         if (wildcard == 0)
357                                 break;
358                 }
359         }
360         return (match);
361 }