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.6 2004/02/16 20:37:20 dillon Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/protosw.h>
47 #include <net/route.h>
53 struct ns_addr zerons_addr;
54 struct nspcb nspcb; /* head of list */
64 m = m_getclr(M_DONTWAIT, MT_CONTROL); /*protocol private PCB */
67 nsp = mtod(m, struct nspcb *);
70 so->so_pcb = (caddr_t)nsp;
79 struct sockaddr_ns *sns;
82 if (nsp->nsp_lport || !ns_nullhost(nsp->nsp_laddr))
86 sns = mtod(nam, struct sockaddr_ns *);
87 if (nam->m_len != sizeof (*sns))
89 if (!ns_nullhost(sns->sns_addr)) {
90 int tport = sns->sns_port;
92 sns->sns_port = 0; /* yech... */
93 if (ifa_ifwithaddr((struct sockaddr *)sns) == 0)
94 return (EADDRNOTAVAIL);
95 sns->sns_port = tport;
97 lport = sns->sns_port;
99 u_short aport = ntohs(lport);
101 #ifdef NS_PRIV_SOCKETS
102 if (aport < NSPORT_RESERVED &&
103 (nsp->nsp_socket->so_state & SS_PRIV) == 0)
105 #endif /* NS_PRIV_SOCKETS */
107 if (ns_pcblookup(&zerons_addr, lport, 0))
110 nsp->nsp_laddr = sns->sns_addr;
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;
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,
129 ns_pcbconnect(nsp, nam)
133 struct ns_ifaddr *ia;
134 struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
139 if (nam->m_len != sizeof (*sns))
141 if (sns->sns_family != AF_NS)
142 return (EAFNOSUPPORT);
143 if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
144 return (EADDRNOTAVAIL);
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
157 ro = &nsp->nsp_route;
158 dst = &satons_addr(ro->ro_dst);
159 if (nsp->nsp_socket->so_options & SO_DONTROUTE)
161 if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
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;
171 ro->ro_rt = (struct rtentry *)0;
172 nsp->nsp_laddr.x_net = ns_zeronet;
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;
186 if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_zeronet)) {
188 * If route is known or can be allocated now,
189 * our src addr is taken from the i/f, else punt.
192 ia = (struct ns_ifaddr *)0;
194 * If we found a route, use the address
195 * corresponding to the outgoing interface
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)
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;
208 ia = ns_iaonnetof(&sns->sns_addr);
212 return (EADDRNOTAVAIL);
214 nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
216 if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
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;
223 nsp->nsp_faddr = sns->sns_addr;
224 /* Includes nsp->nsp_fport = sns->sns_port; */
229 ns_pcbdisconnect(nsp)
233 nsp->nsp_faddr = zerons_addr;
234 if (nsp->nsp_socket->so_state & SS_NOFDREF)
242 struct socket *so = nsp->nsp_socket;
246 if (nsp->nsp_route.ro_rt)
247 rtfree(nsp->nsp_route.ro_rt);
249 (void) m_free(dtom(nsp));
253 ns_setsockaddr(nsp, nam)
257 struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
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;
268 ns_setpeeraddr(nsp, nam)
272 struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
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;
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!)
290 ns_pcbnotify(dst, errno, notify, param)
293 void (*notify)(struct nspcb *);
296 struct nspcb *nsp, *oinp;
299 for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb);) {
300 if (!ns_hosteq(*dst,nsp->nsp_faddr)) {
305 if (nsp->nsp_socket == 0)
308 nsp->nsp_socket->so_error = errno;
311 oinp->nsp_notify_param = param;
319 * After a routing change, flush old routing
320 * and allocate a (hopefully) better one.
325 if (nsp->nsp_route.ro_rt) {
326 rtfree(nsp->nsp_route.ro_rt);
327 nsp->nsp_route.ro_rt = 0;
329 * A new route can be allocated the next time
330 * output is attempted.
333 /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
338 ns_pcblookup(faddr, lport, wildp)
339 struct ns_addr *faddr;
342 struct nspcb *nsp, *match = 0;
343 int matchwild = 3, wildcard;
346 fport = faddr->x_port;
347 for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb); nsp = nsp->nsp_next) {
348 if (nsp->nsp_lport != lport)
351 if (ns_nullhost(nsp->nsp_faddr)) {
352 if (!ns_nullhost(*faddr))
355 if (ns_nullhost(*faddr))
358 if (!ns_hosteq(nsp->nsp_faddr, *faddr))
360 if (nsp->nsp_fport != fport) {
361 if (nsp->nsp_fport != 0)
368 if (wildcard && wildp==0)
370 if (wildcard < matchwild) {
372 matchwild = wildcard;