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.5 2003/09/06 21:51:12 drhodus 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;
63 m = m_getclr(M_DONTWAIT, MT_CONTROL); /*protocol private PCB */
66 nsp = mtod(m, struct nspcb *);
69 so->so_pcb = (caddr_t)nsp;
78 struct sockaddr_ns *sns;
81 if (nsp->nsp_lport || !ns_nullhost(nsp->nsp_laddr))
85 sns = mtod(nam, struct sockaddr_ns *);
86 if (nam->m_len != sizeof (*sns))
88 if (!ns_nullhost(sns->sns_addr)) {
89 int tport = sns->sns_port;
91 sns->sns_port = 0; /* yech... */
92 if (ifa_ifwithaddr((struct sockaddr *)sns) == 0)
93 return (EADDRNOTAVAIL);
94 sns->sns_port = tport;
96 lport = sns->sns_port;
98 u_short aport = ntohs(lport);
100 #ifdef NS_PRIV_SOCKETS
101 if (aport < NSPORT_RESERVED &&
102 (nsp->nsp_socket->so_state & SS_PRIV) == 0)
104 #endif /* NS_PRIV_SOCKETS */
106 if (ns_pcblookup(&zerons_addr, lport, 0))
109 nsp->nsp_laddr = sns->sns_addr;
113 if (nspcb.nsp_lport++ < NSPORT_RESERVED)
114 nspcb.nsp_lport = NSPORT_RESERVED;
115 lport = htons(nspcb.nsp_lport);
116 } while (ns_pcblookup(&zerons_addr, lport, 0));
117 nsp->nsp_lport = lport;
122 * Connect from a socket to a specified address.
123 * Both address and port must be specified in argument sns.
124 * If don't have a local address for this socket yet,
128 ns_pcbconnect(nsp, nam)
132 struct ns_ifaddr *ia;
133 struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
138 if (nam->m_len != sizeof (*sns))
140 if (sns->sns_family != AF_NS)
141 return (EAFNOSUPPORT);
142 if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
143 return (EADDRNOTAVAIL);
145 * If we haven't bound which network number to use as ours,
146 * we will use the number of the outgoing interface.
147 * This depends on having done a routing lookup, which
148 * we will probably have to do anyway, so we might
149 * as well do it now. On the other hand if we are
150 * sending to multiple destinations we may have already
151 * done the lookup, so see if we can use the route
152 * from before. In any case, we only
153 * chose a port number once, even if sending to multiple
156 ro = &nsp->nsp_route;
157 dst = &satons_addr(ro->ro_dst);
158 if (nsp->nsp_socket->so_options & SO_DONTROUTE)
160 if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
162 if (!ns_hosteq(nsp->nsp_lastdst, sns->sns_addr)) {
163 if (ro->ro_rt && ! (ro->ro_rt->rt_flags & RTF_HOST)) {
164 /* can patch route to avoid rtalloc */
165 *dst = sns->sns_addr;
170 ro->ro_rt = (struct rtentry *)0;
171 nsp->nsp_laddr.x_net = ns_zeronet;
173 }/* else cached route is ok; do nothing */
174 nsp->nsp_lastdst = sns->sns_addr;
175 if ((nsp->nsp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
176 (ro->ro_rt == (struct rtentry *)0 ||
177 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
178 /* No route yet, so try to acquire one */
179 ro->ro_dst.sa_family = AF_NS;
180 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
181 *dst = sns->sns_addr;
185 if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_zeronet)) {
187 * If route is known or can be allocated now,
188 * our src addr is taken from the i/f, else punt.
191 ia = (struct ns_ifaddr *)0;
193 * If we found a route, use the address
194 * corresponding to the outgoing interface
196 if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp))
197 for (ia = ns_ifaddr; ia; ia = ia->ia_next)
198 if (ia->ia_ifp == ifp)
201 u_short fport = sns->sns_addr.x_port;
202 sns->sns_addr.x_port = 0;
203 ia = (struct ns_ifaddr *)
204 ifa_ifwithdstaddr((struct sockaddr *)sns);
205 sns->sns_addr.x_port = fport;
207 ia = ns_iaonnetof(&sns->sns_addr);
211 return (EADDRNOTAVAIL);
213 nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
215 if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
217 if (ns_nullhost(nsp->nsp_laddr)) {
218 if (nsp->nsp_lport == 0)
219 (void) ns_pcbbind(nsp, (struct mbuf *)0);
220 nsp->nsp_laddr.x_host = ns_thishost;
222 nsp->nsp_faddr = sns->sns_addr;
223 /* Includes nsp->nsp_fport = sns->sns_port; */
228 ns_pcbdisconnect(nsp)
232 nsp->nsp_faddr = zerons_addr;
233 if (nsp->nsp_socket->so_state & SS_NOFDREF)
241 struct socket *so = nsp->nsp_socket;
245 if (nsp->nsp_route.ro_rt)
246 rtfree(nsp->nsp_route.ro_rt);
248 (void) m_free(dtom(nsp));
252 ns_setsockaddr(nsp, nam)
256 struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
258 nam->m_len = sizeof (*sns);
259 sns = mtod(nam, struct sockaddr_ns *);
260 bzero((caddr_t)sns, sizeof (*sns));
261 sns->sns_len = sizeof(*sns);
262 sns->sns_family = AF_NS;
263 sns->sns_addr = nsp->nsp_laddr;
267 ns_setpeeraddr(nsp, nam)
271 struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
273 nam->m_len = sizeof (*sns);
274 sns = mtod(nam, struct sockaddr_ns *);
275 bzero((caddr_t)sns, sizeof (*sns));
276 sns->sns_len = sizeof(*sns);
277 sns->sns_family = AF_NS;
278 sns->sns_addr = nsp->nsp_faddr;
282 * Pass some notification to all connections of a protocol
283 * associated with address dst. Call the
284 * protocol specific routine to handle each connection.
285 * Also pass an extra paramter via the nspcb. (which may in fact
286 * be a parameter list!)
289 ns_pcbnotify(dst, errno, notify, param)
292 void (*notify)(struct nspcb *);
295 struct nspcb *nsp, *oinp;
298 for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb);) {
299 if (!ns_hosteq(*dst,nsp->nsp_faddr)) {
304 if (nsp->nsp_socket == 0)
307 nsp->nsp_socket->so_error = errno;
310 oinp->nsp_notify_param = param;
318 * After a routing change, flush old routing
319 * and allocate a (hopefully) better one.
324 if (nsp->nsp_route.ro_rt) {
325 rtfree(nsp->nsp_route.ro_rt);
326 nsp->nsp_route.ro_rt = 0;
328 * A new route can be allocated the next time
329 * output is attempted.
332 /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
337 ns_pcblookup(faddr, lport, wildp)
338 struct ns_addr *faddr;
341 struct nspcb *nsp, *match = 0;
342 int matchwild = 3, wildcard;
345 fport = faddr->x_port;
346 for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb); nsp = nsp->nsp_next) {
347 if (nsp->nsp_lport != lport)
350 if (ns_nullhost(nsp->nsp_faddr)) {
351 if (!ns_nullhost(*faddr))
354 if (ns_nullhost(*faddr))
357 if (!ns_hosteq(nsp->nsp_faddr, *faddr))
359 if (nsp->nsp_fport != fport) {
360 if (nsp->nsp_fport != 0)
367 if (wildcard && wildp==0)
369 if (wildcard < matchwild) {
371 matchwild = wildcard;