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.12 2004/06/07 07:04:33 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>
50 #include <net/route.h>
56 struct ns_addr zerons_addr;
57 struct nspcb nspcb; /* head of list */
59 static MALLOC_DEFINE(M_NSPCB, "nspcb", "NS PCB Management");
62 ns_pcballoc(struct socket *so, struct nspcb *head)
66 nsp = malloc(sizeof(struct nspcb), M_NSPCB, M_WAITOK|M_ZERO);
69 so->so_pcb = (caddr_t)nsp;
74 ns_pcbbind(struct nspcb *nsp, struct sockaddr *nam)
76 struct sockaddr_ns *sns;
79 if (nsp->nsp_lport || !ns_nullhost(nsp->nsp_laddr))
83 sns = (struct sockaddr_ns *)nam;
84 if (nam->sa_len != sizeof (*sns))
86 if (!ns_nullhost(sns->sns_addr)) {
87 int tport = sns->sns_port;
89 sns->sns_port = 0; /* yech... */
90 if (ifa_ifwithaddr((struct sockaddr *)sns) == 0)
91 return (EADDRNOTAVAIL);
92 sns->sns_port = tport;
94 lport = sns->sns_port;
96 #ifdef NS_PRIV_SOCKETS
97 u_short aport = ntohs(lport);
99 if (aport < NSPORT_RESERVED &&
100 (nsp->nsp_socket->so_state & SS_PRIV) == 0)
102 #endif /* NS_PRIV_SOCKETS */
104 if (ns_pcblookup(&zerons_addr, lport, 0))
107 nsp->nsp_laddr = sns->sns_addr;
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;
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,
126 ns_pcbconnect(struct nspcb *nsp, struct sockaddr *nam)
128 struct ns_ifaddr *ia;
129 struct sockaddr_ns *sns = (struct sockaddr_ns *)nam;
134 if (nam->sa_len != sizeof (*sns))
136 if (sns->sns_family != AF_NS)
137 return (EAFNOSUPPORT);
138 if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
139 return (EADDRNOTAVAIL);
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
152 ro = &nsp->nsp_route;
153 dst = &satons_addr(ro->ro_dst);
154 if (nsp->nsp_socket->so_options & SO_DONTROUTE)
156 if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
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;
166 ro->ro_rt = (struct rtentry *)0;
167 nsp->nsp_laddr.x_net = ns_zeronet;
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;
181 if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_zeronet)) {
183 * If route is known or can be allocated now,
184 * our src addr is taken from the i/f, else punt.
187 ia = (struct ns_ifaddr *)0;
189 * If we found a route, use the address
190 * corresponding to the outgoing interface
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)
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;
203 ia = ns_iaonnetof(&sns->sns_addr);
207 return (EADDRNOTAVAIL);
209 nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
211 if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
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;
218 nsp->nsp_faddr = sns->sns_addr;
219 /* Includes nsp->nsp_fport = sns->sns_port; */
224 ns_pcbdisconnect(struct nspcb *nsp)
226 nsp->nsp_faddr = zerons_addr;
227 if (nsp->nsp_socket->so_state & SS_NOFDREF)
232 ns_pcbdetach(struct nspcb *nsp)
234 struct socket *so = nsp->nsp_socket;
238 if (nsp->nsp_route.ro_rt)
239 rtfree(nsp->nsp_route.ro_rt);
245 ns_setsockaddr(struct nspcb *nsp, struct sockaddr **pnam)
247 struct sockaddr_ns sns;
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);
257 ns_setpeeraddr(struct nspcb *nsp, struct sockaddr **pnam)
259 struct sockaddr_ns sns;
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);
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!)
276 ns_pcbnotify(struct ns_addr *dst, int errno,
277 void (*notify)(struct nspcb *), long param)
279 struct nspcb *nsp, *oinp;
282 for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb);) {
283 if (!ns_hosteq(*dst,nsp->nsp_faddr)) {
288 if (nsp->nsp_socket == 0)
291 nsp->nsp_socket->so_error = errno;
294 oinp->nsp_notify_param = param;
302 * After a routing change, flush old routing
303 * and allocate a (hopefully) better one.
305 ns_rtchange(struct nspcb *nsp)
307 if (nsp->nsp_route.ro_rt) {
308 rtfree(nsp->nsp_route.ro_rt);
309 nsp->nsp_route.ro_rt = 0;
311 * A new route can be allocated the next time
312 * output is attempted.
315 /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
320 ns_pcblookup(struct ns_addr *faddr, u_short lport, int wildp)
322 struct nspcb *nsp, *match = 0;
323 int matchwild = 3, wildcard;
326 fport = faddr->x_port;
327 for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb); nsp = nsp->nsp_next) {
328 if (nsp->nsp_lport != lport)
331 if (ns_nullhost(nsp->nsp_faddr)) {
332 if (!ns_nullhost(*faddr))
335 if (ns_nullhost(*faddr))
338 if (!ns_hosteq(nsp->nsp_faddr, *faddr))
340 if (nsp->nsp_fport != fport) {
341 if (nsp->nsp_fport != 0)
348 if (wildcard && wildp==0)
350 if (wildcard < matchwild) {
352 matchwild = wildcard;