2 * Copyright (c) 1984, 1985, 1986, 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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 $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.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>
51 #include <net/route.h>
57 struct ns_addr zerons_addr;
58 struct nspcb nspcb; /* head of list */
60 static MALLOC_DEFINE(M_NSPCB, "nspcb", "NS PCB Management");
63 ns_pcballoc(struct socket *so, struct nspcb *head)
67 nsp = kmalloc(sizeof(struct nspcb), M_NSPCB, M_WAITOK|M_ZERO);
70 so->so_pcb = (caddr_t)nsp;
75 ns_pcbbind(struct nspcb *nsp, struct sockaddr *nam)
77 struct sockaddr_ns *sns;
80 if (nsp->nsp_lport || !ns_nullhost(nsp->nsp_laddr))
84 sns = (struct sockaddr_ns *)nam;
85 if (nam->sa_len != sizeof (*sns))
87 if (!ns_nullhost(sns->sns_addr)) {
88 int tport = sns->sns_port;
90 sns->sns_port = 0; /* yech... */
91 if (ifa_ifwithaddr((struct sockaddr *)sns) == 0)
92 return (EADDRNOTAVAIL);
93 sns->sns_port = tport;
95 lport = sns->sns_port;
97 #ifdef NS_PRIV_SOCKETS
98 u_short aport = ntohs(lport);
100 if (aport < NSPORT_RESERVED &&
101 (nsp->nsp_socket->so_state & SS_PRIV) == 0)
103 #endif /* NS_PRIV_SOCKETS */
105 if (ns_pcblookup(&zerons_addr, lport, 0))
108 nsp->nsp_laddr = sns->sns_addr;
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;
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,
127 ns_pcbconnect(struct nspcb *nsp, struct sockaddr *nam)
129 struct ns_ifaddr *ia;
130 struct sockaddr_ns *sns = (struct sockaddr_ns *)nam;
135 if (nam->sa_len != sizeof (*sns))
137 if (sns->sns_family != AF_NS)
138 return (EAFNOSUPPORT);
139 if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
140 return (EADDRNOTAVAIL);
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
153 ro = &nsp->nsp_route;
154 dst = &satons_addr(ro->ro_dst);
155 if (nsp->nsp_socket->so_options & SO_DONTROUTE)
157 if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
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;
168 nsp->nsp_laddr.x_net = ns_zeronet;
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;
182 if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_zeronet)) {
184 * If route is known or can be allocated now,
185 * our src addr is taken from the i/f, else punt.
190 * If we found a route, use the address
191 * corresponding to the outgoing interface
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)
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;
204 ia = ns_iaonnetof(&sns->sns_addr);
208 return (EADDRNOTAVAIL);
210 nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
212 if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
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;
219 nsp->nsp_faddr = sns->sns_addr;
220 /* Includes nsp->nsp_fport = sns->sns_port; */
225 ns_pcbdisconnect(struct nspcb *nsp)
227 nsp->nsp_faddr = zerons_addr;
228 if (nsp->nsp_socket->so_state & SS_NOFDREF)
233 ns_pcbdetach(struct nspcb *nsp)
235 struct socket *so = nsp->nsp_socket;
239 if (nsp->nsp_route.ro_rt)
240 rtfree(nsp->nsp_route.ro_rt);
246 ns_setsockaddr(struct nspcb *nsp, struct sockaddr **pnam)
248 struct sockaddr_ns sns;
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);
258 ns_setpeeraddr(struct nspcb *nsp, struct sockaddr **pnam)
260 struct sockaddr_ns sns;
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);
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!)
277 ns_pcbnotify(struct ns_addr *dst, int errno,
278 void (*notify)(struct nspcb *), long param)
280 struct nspcb *nsp, *oinp;
284 for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb);) {
285 if (!ns_hosteq(*dst,nsp->nsp_faddr)) {
290 if (nsp->nsp_socket == 0)
293 nsp->nsp_socket->so_error = errno;
296 oinp->nsp_notify_param = param;
304 * After a routing change, flush old routing
305 * and allocate a (hopefully) better one.
308 ns_rtchange(struct nspcb *nsp)
310 if (nsp->nsp_route.ro_rt) {
311 rtfree(nsp->nsp_route.ro_rt);
312 nsp->nsp_route.ro_rt = 0;
314 * A new route can be allocated the next time
315 * output is attempted.
318 /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
323 ns_pcblookup(struct ns_addr *faddr, u_short lport, int wildp)
325 struct nspcb *nsp, *match = 0;
326 int matchwild = 3, wildcard;
329 fport = faddr->x_port;
330 for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb); nsp = nsp->nsp_next) {
331 if (nsp->nsp_lport != lport)
334 if (ns_nullhost(nsp->nsp_faddr)) {
335 if (!ns_nullhost(*faddr))
338 if (ns_nullhost(*faddr))
341 if (!ns_hosteq(nsp->nsp_faddr, *faddr))
343 if (nsp->nsp_fport != fport) {
344 if (nsp->nsp_fport != 0)
351 if (wildcard && wildp==0)
353 if (wildcard < matchwild) {
355 matchwild = wildcard;