Added
[unix-history] / sys / netns / ns_pcb.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1984, 1985, 1986, 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
136b31f1 33 * from: @(#)ns_pcb.c 7.11 (Berkeley) 6/27/91
fde1aeb2 34 * $Id: ns_pcb.c,v 1.4 1993/11/25 01:36:33 wollman Exp $
15637ed4
RG
35 */
36
37#include "param.h"
38#include "systm.h"
39#include "mbuf.h"
40#include "errno.h"
41#include "socket.h"
42#include "socketvar.h"
43#include "protosw.h"
44
45#include "../net/if.h"
46#include "../net/route.h"
47
48#include "ns.h"
49#include "ns_if.h"
50#include "ns_pcb.h"
51
52struct ns_addr zerons_addr;
8ace4366
GW
53struct nspcb nspcb;
54
4c45483e 55int
15637ed4
RG
56ns_pcballoc(so, head)
57 struct socket *so;
58 struct nspcb *head;
59{
60 struct mbuf *m;
61 register struct nspcb *nsp;
62
63 m = m_getclr(M_DONTWAIT, MT_PCB);
64 if (m == NULL)
65 return (ENOBUFS);
66 nsp = mtod(m, struct nspcb *);
67 nsp->nsp_socket = so;
68 insque(nsp, head);
69 so->so_pcb = (caddr_t)nsp;
70 return (0);
71}
72
4c45483e 73int
15637ed4
RG
74ns_pcbbind(nsp, nam)
75 register struct nspcb *nsp;
76 struct mbuf *nam;
77{
78 register struct sockaddr_ns *sns;
79 u_short lport = 0;
80
81 if (nsp->nsp_lport || !ns_nullhost(nsp->nsp_laddr))
82 return (EINVAL);
83 if (nam == 0)
84 goto noname;
85 sns = mtod(nam, struct sockaddr_ns *);
86 if (nam->m_len != sizeof (*sns))
87 return (EINVAL);
88 if (!ns_nullhost(sns->sns_addr)) {
89 int tport = sns->sns_port;
90
91 sns->sns_port = 0; /* yech... */
92 if (ifa_ifwithaddr((struct sockaddr *)sns) == 0)
93 return (EADDRNOTAVAIL);
94 sns->sns_port = tport;
95 }
96 lport = sns->sns_port;
97 if (lport) {
98 u_short aport = ntohs(lport);
99
100 if (aport < NSPORT_RESERVED &&
101 (nsp->nsp_socket->so_state & SS_PRIV) == 0)
102 return (EACCES);
103 if (ns_pcblookup(&zerons_addr, lport, 0))
104 return (EADDRINUSE);
105 }
106 nsp->nsp_laddr = sns->sns_addr;
107noname:
108 if (lport == 0)
109 do {
110 if (nspcb.nsp_lport++ < NSPORT_RESERVED)
111 nspcb.nsp_lport = NSPORT_RESERVED;
112 lport = htons(nspcb.nsp_lport);
113 } while (ns_pcblookup(&zerons_addr, lport, 0));
114 nsp->nsp_lport = lport;
115 return (0);
116}
117
118/*
119 * Connect from a socket to a specified address.
120 * Both address and port must be specified in argument sns.
121 * If don't have a local address for this socket yet,
122 * then pick one.
123 */
4c45483e 124int
15637ed4
RG
125ns_pcbconnect(nsp, nam)
126 struct nspcb *nsp;
127 struct mbuf *nam;
128{
129 struct ns_ifaddr *ia;
130 register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
131 register struct ns_addr *dst;
132 register struct route *ro;
133 struct ifnet *ifp;
134
135 if (nam->m_len != sizeof (*sns))
136 return (EINVAL);
137 if (sns->sns_family != AF_NS)
138 return (EAFNOSUPPORT);
139 if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
140 return (EADDRNOTAVAIL);
141 /*
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
151 * destinations.
152 */
153 ro = &nsp->nsp_route;
154 dst = &satons_addr(ro->ro_dst);
155 if (nsp->nsp_socket->so_options & SO_DONTROUTE)
156 goto flush;
157 if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
158 goto flush;
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;
163 } else {
164 flush:
165 if (ro->ro_rt)
166 RTFREE(ro->ro_rt);
167 ro->ro_rt = (struct rtentry *)0;
168 nsp->nsp_laddr.x_net = ns_zeronet;
169 }
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 == (struct rtentry *)0 ||
174 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
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;
179 dst->x_port = 0;
180 rtalloc(ro);
181 }
182 if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_zeronet)) {
183 /*
184 * If route is known or can be allocated now,
185 * our src addr is taken from the i/f, else punt.
186 */
187
188 ia = (struct ns_ifaddr *)0;
189 /*
190 * If we found a route, use the address
191 * corresponding to the outgoing interface
192 */
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)
196 break;
197 if (ia == 0) {
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;
203 if (ia == 0)
204 ia = ns_iaonnetof(&sns->sns_addr);
205 if (ia == 0)
206 ia = ns_ifaddr;
207 if (ia == 0)
208 return (EADDRNOTAVAIL);
209 }
210 nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
211 }
212 if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
213 return (EADDRINUSE);
214 if (ns_nullhost(nsp->nsp_laddr)) {
215 if (nsp->nsp_lport == 0)
216 (void) ns_pcbbind(nsp, (struct mbuf *)0);
217 nsp->nsp_laddr.x_host = ns_thishost;
218 }
219 nsp->nsp_faddr = sns->sns_addr;
220 /* Includes nsp->nsp_fport = sns->sns_port; */
221 return (0);
222}
223
4c45483e 224void
15637ed4
RG
225ns_pcbdisconnect(nsp)
226 struct nspcb *nsp;
227{
228
229 nsp->nsp_faddr = zerons_addr;
230 if (nsp->nsp_socket->so_state & SS_NOFDREF)
231 ns_pcbdetach(nsp);
232}
233
4c45483e 234void
15637ed4
RG
235ns_pcbdetach(nsp)
236 struct nspcb *nsp;
237{
238 struct socket *so = nsp->nsp_socket;
239
240 so->so_pcb = 0;
241 sofree(so);
242 if (nsp->nsp_route.ro_rt)
243 rtfree(nsp->nsp_route.ro_rt);
244 remque(nsp);
245 (void) m_free(dtom(nsp));
246}
247
4c45483e 248void
15637ed4
RG
249ns_setsockaddr(nsp, nam)
250 register struct nspcb *nsp;
251 struct mbuf *nam;
252{
253 register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
254
255 nam->m_len = sizeof (*sns);
256 sns = mtod(nam, struct sockaddr_ns *);
257 bzero((caddr_t)sns, sizeof (*sns));
258 sns->sns_len = sizeof(*sns);
259 sns->sns_family = AF_NS;
260 sns->sns_addr = nsp->nsp_laddr;
261}
262
4c45483e 263void
15637ed4
RG
264ns_setpeeraddr(nsp, nam)
265 register struct nspcb *nsp;
266 struct mbuf *nam;
267{
268 register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
269
270 nam->m_len = sizeof (*sns);
271 sns = mtod(nam, struct sockaddr_ns *);
272 bzero((caddr_t)sns, sizeof (*sns));
273 sns->sns_len = sizeof(*sns);
274 sns->sns_family = AF_NS;
275 sns->sns_addr = nsp->nsp_faddr;
276}
277
278/*
279 * Pass some notification to all connections of a protocol
280 * associated with address dst. Call the
281 * protocol specific routine to handle each connection.
282 * Also pass an extra paramter via the nspcb. (which may in fact
283 * be a parameter list!)
284 */
4c45483e 285void
15637ed4
RG
286ns_pcbnotify(dst, errno, notify, param)
287 register struct ns_addr *dst;
fde1aeb2
GW
288 int errno;
289 ns_notify_func_t notify;
15637ed4 290 long param;
15637ed4
RG
291{
292 register struct nspcb *nsp, *oinp;
293 int s = splimp();
294
295 for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb);) {
296 if (!ns_hosteq(*dst,nsp->nsp_faddr)) {
297 next:
298 nsp = nsp->nsp_next;
299 continue;
300 }
301 if (nsp->nsp_socket == 0)
302 goto next;
303 if (errno)
304 nsp->nsp_socket->so_error = errno;
305 oinp = nsp;
306 nsp = nsp->nsp_next;
307 oinp->nsp_notify_param = param;
fde1aeb2 308 (*notify)(oinp, errno);
15637ed4
RG
309 }
310 splx(s);
311}
312
313#ifdef notdef
314/*
315 * After a routing change, flush old routing
316 * and allocate a (hopefully) better one.
317 */
318ns_rtchange(nsp)
319 struct nspcb *nsp;
320{
321 if (nsp->nsp_route.ro_rt) {
322 rtfree(nsp->nsp_route.ro_rt);
323 nsp->nsp_route.ro_rt = 0;
324 /*
325 * A new route can be allocated the next time
326 * output is attempted.
327 */
328 }
329 /* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
330}
331#endif
332
333struct nspcb *
334ns_pcblookup(faddr, lport, wildp)
335 struct ns_addr *faddr;
336 u_short lport;
4c45483e 337 int wildp;
15637ed4
RG
338{
339 register struct nspcb *nsp, *match = 0;
340 int matchwild = 3, wildcard;
341 u_short fport;
342
343 fport = faddr->x_port;
344 for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb); nsp = nsp->nsp_next) {
345 if (nsp->nsp_lport != lport)
346 continue;
347 wildcard = 0;
348 if (ns_nullhost(nsp->nsp_faddr)) {
349 if (!ns_nullhost(*faddr))
350 wildcard++;
351 } else {
352 if (ns_nullhost(*faddr))
353 wildcard++;
354 else {
355 if (!ns_hosteq(nsp->nsp_faddr, *faddr))
356 continue;
357 if (nsp->nsp_fport != fport) {
358 if (nsp->nsp_fport != 0)
359 continue;
360 else
361 wildcard++;
362 }
363 }
364 }
365 if (wildcard && wildp==0)
366 continue;
367 if (wildcard < matchwild) {
368 match = nsp;
369 matchwild = wildcard;
370 if (wildcard == 0)
371 break;
372 }
373 }
374 return (match);
375}