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 $
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/protosw.h>
46 #include <net/route.h>
49 #include <netns/ns_if.h>
50 #include <netns/ns_pcb.h>
52 struct ns_addr zerons_addr;
59 register struct nspcb *nsp;
61 m = m_getclr(M_DONTWAIT, MT_PCB);
64 nsp = mtod(m, struct nspcb *);
67 so->so_pcb = (caddr_t)nsp;
72 register struct nspcb *nsp;
75 register struct sockaddr_ns *sns;
78 if (nsp->nsp_lport || !ns_nullhost(nsp->nsp_laddr))
82 sns = mtod(nam, struct sockaddr_ns *);
83 if (nam->m_len != sizeof (*sns))
85 if (!ns_nullhost(sns->sns_addr)) {
86 int tport = sns->sns_port;
88 sns->sns_port = 0; /* yech... */
89 if (ifa_ifwithaddr((struct sockaddr *)sns) == 0)
90 return (EADDRNOTAVAIL);
91 sns->sns_port = tport;
93 lport = sns->sns_port;
95 u_short aport = ntohs(lport);
97 if (aport < NSPORT_RESERVED &&
98 (nsp->nsp_socket->so_state & SS_PRIV) == 0)
100 if (ns_pcblookup(&zerons_addr, lport, 0))
103 nsp->nsp_laddr = sns->sns_addr;
107 if (nspcb.nsp_lport++ < NSPORT_RESERVED)
108 nspcb.nsp_lport = NSPORT_RESERVED;
109 lport = htons(nspcb.nsp_lport);
110 } while (ns_pcblookup(&zerons_addr, lport, 0));
111 nsp->nsp_lport = lport;
116 * Connect from a socket to a specified address.
117 * Both address and port must be specified in argument sns.
118 * If don't have a local address for this socket yet,
121 ns_pcbconnect(nsp, nam)
125 struct ns_ifaddr *ia;
126 register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
127 register struct ns_addr *dst;
128 register struct route *ro;
131 if (nam->m_len != sizeof (*sns))
133 if (sns->sns_family != AF_NS)
134 return (EAFNOSUPPORT);
135 if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
136 return (EADDRNOTAVAIL);
138 * If we haven't bound which network number to use as ours,
139 * we will use the number of the outgoing interface.
140 * This depends on having done a routing lookup, which
141 * we will probably have to do anyway, so we might
142 * as well do it now. On the other hand if we are
143 * sending to multiple destinations we may have already
144 * done the lookup, so see if we can use the route
145 * from before. In any case, we only
146 * chose a port number once, even if sending to multiple
149 ro = &nsp->nsp_route;
150 dst = &satons_addr(ro->ro_dst);
151 if (nsp->nsp_socket->so_options & SO_DONTROUTE)
153 if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
155 if (!ns_hosteq(nsp->nsp_lastdst, sns->sns_addr)) {
156 if (ro->ro_rt && ! (ro->ro_rt->rt_flags & RTF_HOST)) {
157 /* can patch route to avoid rtalloc */
158 *dst = sns->sns_addr;
163 ro->ro_rt = (struct rtentry *)0;
164 nsp->nsp_laddr.x_net = ns_zeronet;
166 }/* else cached route is ok; do nothing */
167 nsp->nsp_lastdst = sns->sns_addr;
168 if ((nsp->nsp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
169 (ro->ro_rt == (struct rtentry *)0 ||
170 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
171 /* No route yet, so try to acquire one */
172 ro->ro_dst.sa_family = AF_NS;
173 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
174 *dst = sns->sns_addr;
178 if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_zeronet)) {
180 * If route is known or can be allocated now,
181 * our src addr is taken from the i/f, else punt.
184 ia = (struct ns_ifaddr *)0;
186 * If we found a route, use the address
187 * corresponding to the outgoing interface
189 if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp))
190 for (ia = ns_ifaddr; ia; ia = ia->ia_next)
191 if (ia->ia_ifp == ifp)
194 u_short fport = sns->sns_addr.x_port;
195 sns->sns_addr.x_port = 0;
196 ia = (struct ns_ifaddr *)
197 ifa_ifwithdstaddr((struct sockaddr *)sns);
198 sns->sns_addr.x_port = fport;
200 ia = ns_iaonnetof(&sns->sns_addr);
204 return (EADDRNOTAVAIL);
206 nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
208 if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
210 if (ns_nullhost(nsp->nsp_laddr)) {
211 if (nsp->nsp_lport == 0)
212 (void) ns_pcbbind(nsp, (struct mbuf *)0);
213 nsp->nsp_laddr.x_host = ns_thishost;
215 nsp->nsp_faddr = sns->sns_addr;
216 /* Includes nsp->nsp_fport = sns->sns_port; */
220 ns_pcbdisconnect(nsp)
224 nsp->nsp_faddr = zerons_addr;
225 if (nsp->nsp_socket->so_state & SS_NOFDREF)
232 struct socket *so = nsp->nsp_socket;
236 if (nsp->nsp_route.ro_rt)
237 rtfree(nsp->nsp_route.ro_rt);
239 (void) m_free(dtom(nsp));
242 ns_setsockaddr(nsp, nam)
243 register struct nspcb *nsp;
246 register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
248 nam->m_len = sizeof (*sns);
249 sns = mtod(nam, struct sockaddr_ns *);
250 bzero((caddr_t)sns, sizeof (*sns));
251 sns->sns_len = sizeof(*sns);
252 sns->sns_family = AF_NS;
253 sns->sns_addr = nsp->nsp_laddr;
256 ns_setpeeraddr(nsp, nam)
257 register struct nspcb *nsp;
260 register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
262 nam->m_len = sizeof (*sns);
263 sns = mtod(nam, struct sockaddr_ns *);
264 bzero((caddr_t)sns, sizeof (*sns));
265 sns->sns_len = sizeof(*sns);
266 sns->sns_family = AF_NS;
267 sns->sns_addr = nsp->nsp_faddr;
271 * Pass some notification to all connections of a protocol
272 * associated with address dst. Call the
273 * protocol specific routine to handle each connection.
274 * Also pass an extra paramter via the nspcb. (which may in fact
275 * be a parameter list!)
277 ns_pcbnotify(dst, errno, notify, param)
278 register struct ns_addr *dst;
280 int errno, (*notify)();
282 register struct nspcb *nsp, *oinp;
285 for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb);) {
286 if (!ns_hosteq(*dst,nsp->nsp_faddr)) {
291 if (nsp->nsp_socket == 0)
294 nsp->nsp_socket->so_error = errno;
297 oinp->nsp_notify_param = param;
305 * After a routing change, flush old routing
306 * and allocate a (hopefully) better one.
311 if (nsp->nsp_route.ro_rt) {
312 rtfree(nsp->nsp_route.ro_rt);
313 nsp->nsp_route.ro_rt = 0;
315 * A new route can be allocated the next time
316 * output is attempted.
319 /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
324 ns_pcblookup(faddr, lport, wildp)
325 struct ns_addr *faddr;
328 register struct nspcb *nsp, *match = 0;
329 int matchwild = 3, wildcard;
332 fport = faddr->x_port;
333 for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb); nsp = nsp->nsp_next) {
334 if (nsp->nsp_lport != lport)
337 if (ns_nullhost(nsp->nsp_faddr)) {
338 if (!ns_nullhost(*faddr))
341 if (ns_nullhost(*faddr))
344 if (!ns_hosteq(nsp->nsp_faddr, *faddr))
346 if (nsp->nsp_fport != fport) {
347 if (nsp->nsp_fport != 0)
354 if (wildcard && wildp==0)
356 if (wildcard < matchwild) {
358 matchwild = wildcard;