xref: /original-bsd/sys/netns/ns_pcb.c (revision a64d8d4e)
1 /*
2  * Copyright (c) 1984, 1985, 1986, 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)ns_pcb.c	7.10 (Berkeley) 11/20/90
8  */
9 
10 #include "param.h"
11 #include "systm.h"
12 #include "user.h"
13 #include "mbuf.h"
14 #include "socket.h"
15 #include "socketvar.h"
16 #include "../net/if.h"
17 #include "../net/route.h"
18 #include "protosw.h"
19 
20 #include "ns.h"
21 #include "ns_if.h"
22 #include "ns_pcb.h"
23 
24 struct	ns_addr zerons_addr;
25 
26 ns_pcballoc(so, head)
27 	struct socket *so;
28 	struct nspcb *head;
29 {
30 	struct mbuf *m;
31 	register struct nspcb *nsp;
32 
33 	m = m_getclr(M_DONTWAIT, MT_PCB);
34 	if (m == NULL)
35 		return (ENOBUFS);
36 	nsp = mtod(m, struct nspcb *);
37 	nsp->nsp_socket = so;
38 	insque(nsp, head);
39 	so->so_pcb = (caddr_t)nsp;
40 	return (0);
41 }
42 
43 ns_pcbbind(nsp, nam)
44 	register struct nspcb *nsp;
45 	struct mbuf *nam;
46 {
47 	register struct sockaddr_ns *sns;
48 	u_short lport = 0;
49 
50 	if (nsp->nsp_lport || !ns_nullhost(nsp->nsp_laddr))
51 		return (EINVAL);
52 	if (nam == 0)
53 		goto noname;
54 	sns = mtod(nam, struct sockaddr_ns *);
55 	if (nam->m_len != sizeof (*sns))
56 		return (EINVAL);
57 	if (!ns_nullhost(sns->sns_addr)) {
58 		int tport = sns->sns_port;
59 
60 		sns->sns_port = 0;		/* yech... */
61 		if (ifa_ifwithaddr((struct sockaddr *)sns) == 0)
62 			return (EADDRNOTAVAIL);
63 		sns->sns_port = tport;
64 	}
65 	lport = sns->sns_port;
66 	if (lport) {
67 		u_short aport = ntohs(lport);
68 
69 		if (aport < NSPORT_RESERVED && u.u_uid != 0)
70 			return (EACCES);
71 		if (ns_pcblookup(&zerons_addr, lport, 0))
72 			return (EADDRINUSE);
73 	}
74 	nsp->nsp_laddr = sns->sns_addr;
75 noname:
76 	if (lport == 0)
77 		do {
78 			if (nspcb.nsp_lport++ < NSPORT_RESERVED)
79 				nspcb.nsp_lport = NSPORT_RESERVED;
80 			lport = htons(nspcb.nsp_lport);
81 		} while (ns_pcblookup(&zerons_addr, lport, 0));
82 	nsp->nsp_lport = lport;
83 	return (0);
84 }
85 
86 /*
87  * Connect from a socket to a specified address.
88  * Both address and port must be specified in argument sns.
89  * If don't have a local address for this socket yet,
90  * then pick one.
91  */
92 ns_pcbconnect(nsp, nam)
93 	struct nspcb *nsp;
94 	struct mbuf *nam;
95 {
96 	struct ns_ifaddr *ia;
97 	register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
98 	register struct ns_addr *dst;
99 	register struct route *ro;
100 	struct ifnet *ifp;
101 
102 	if (nam->m_len != sizeof (*sns))
103 		return (EINVAL);
104 	if (sns->sns_family != AF_NS)
105 		return (EAFNOSUPPORT);
106 	if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
107 		return (EADDRNOTAVAIL);
108 	/*
109 	 * If we haven't bound which network number to use as ours,
110 	 * we will use the number of the outgoing interface.
111 	 * This depends on having done a routing lookup, which
112 	 * we will probably have to do anyway, so we might
113 	 * as well do it now.  On the other hand if we are
114 	 * sending to multiple destinations we may have already
115 	 * done the lookup, so see if we can use the route
116 	 * from before.  In any case, we only
117 	 * chose a port number once, even if sending to multiple
118 	 * destinations.
119 	 */
120 	ro = &nsp->nsp_route;
121 	dst = &satons_addr(ro->ro_dst);
122 	if (nsp->nsp_socket->so_options & SO_DONTROUTE)
123 		goto flush;
124 	if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
125 		goto flush;
126 	if (!ns_hosteq(nsp->nsp_lastdst, sns->sns_addr)) {
127 		if (ro->ro_rt && ! (ro->ro_rt->rt_flags & RTF_HOST)) {
128 			/* can patch route to avoid rtalloc */
129 			*dst = sns->sns_addr;
130 		} else {
131 	flush:
132 			if (ro->ro_rt)
133 				RTFREE(ro->ro_rt);
134 			ro->ro_rt = (struct rtentry *)0;
135 			nsp->nsp_laddr.x_net = ns_zeronet;
136 		}
137 	}/* else cached route is ok; do nothing */
138 	nsp->nsp_lastdst = sns->sns_addr;
139 	if ((nsp->nsp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
140 	    (ro->ro_rt == (struct rtentry *)0 ||
141 	     ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
142 		    /* No route yet, so try to acquire one */
143 		    ro->ro_dst.sa_family = AF_NS;
144 		    ro->ro_dst.sa_len = sizeof(ro->ro_dst);
145 		    *dst = sns->sns_addr;
146 		    dst->x_port = 0;
147 		    rtalloc(ro);
148 	}
149 	if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_zeronet)) {
150 		/*
151 		 * If route is known or can be allocated now,
152 		 * our src addr is taken from the i/f, else punt.
153 		 */
154 
155 		ia = (struct ns_ifaddr *)0;
156 		/*
157 		 * If we found a route, use the address
158 		 * corresponding to the outgoing interface
159 		 */
160 		if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp))
161 			for (ia = ns_ifaddr; ia; ia = ia->ia_next)
162 				if (ia->ia_ifp == ifp)
163 					break;
164 		if (ia == 0) {
165 			u_short fport = sns->sns_addr.x_port;
166 			sns->sns_addr.x_port = 0;
167 			ia = (struct ns_ifaddr *)
168 				ifa_ifwithdstaddr((struct sockaddr *)sns);
169 			sns->sns_addr.x_port = fport;
170 			if (ia == 0)
171 				ia = ns_iaonnetof(&sns->sns_addr);
172 			if (ia == 0)
173 				ia = ns_ifaddr;
174 			if (ia == 0)
175 				return (EADDRNOTAVAIL);
176 		}
177 		nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
178 	}
179 	if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
180 		return (EADDRINUSE);
181 	if (ns_nullhost(nsp->nsp_laddr)) {
182 		if (nsp->nsp_lport == 0)
183 			(void) ns_pcbbind(nsp, (struct mbuf *)0);
184 		nsp->nsp_laddr.x_host = ns_thishost;
185 	}
186 	nsp->nsp_faddr = sns->sns_addr;
187 	/* Includes nsp->nsp_fport = sns->sns_port; */
188 	return (0);
189 }
190 
191 ns_pcbdisconnect(nsp)
192 	struct nspcb *nsp;
193 {
194 
195 	nsp->nsp_faddr = zerons_addr;
196 	if (nsp->nsp_socket->so_state & SS_NOFDREF)
197 		ns_pcbdetach(nsp);
198 }
199 
200 ns_pcbdetach(nsp)
201 	struct nspcb *nsp;
202 {
203 	struct socket *so = nsp->nsp_socket;
204 
205 	so->so_pcb = 0;
206 	sofree(so);
207 	if (nsp->nsp_route.ro_rt)
208 		rtfree(nsp->nsp_route.ro_rt);
209 	remque(nsp);
210 	(void) m_free(dtom(nsp));
211 }
212 
213 ns_setsockaddr(nsp, nam)
214 	register struct nspcb *nsp;
215 	struct mbuf *nam;
216 {
217 	register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
218 
219 	nam->m_len = sizeof (*sns);
220 	sns = mtod(nam, struct sockaddr_ns *);
221 	bzero((caddr_t)sns, sizeof (*sns));
222 	sns->sns_len = sizeof(*sns);
223 	sns->sns_family = AF_NS;
224 	sns->sns_addr = nsp->nsp_laddr;
225 }
226 
227 ns_setpeeraddr(nsp, nam)
228 	register struct nspcb *nsp;
229 	struct mbuf *nam;
230 {
231 	register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
232 
233 	nam->m_len = sizeof (*sns);
234 	sns = mtod(nam, struct sockaddr_ns *);
235 	bzero((caddr_t)sns, sizeof (*sns));
236 	sns->sns_len = sizeof(*sns);
237 	sns->sns_family = AF_NS;
238 	sns->sns_addr  = nsp->nsp_faddr;
239 }
240 
241 /*
242  * Pass some notification to all connections of a protocol
243  * associated with address dst.  Call the
244  * protocol specific routine to handle each connection.
245  * Also pass an extra paramter via the nspcb. (which may in fact
246  * be a parameter list!)
247  */
248 ns_pcbnotify(dst, errno, notify, param)
249 	register struct ns_addr *dst;
250 	long param;
251 	int errno, (*notify)();
252 {
253 	register struct nspcb *nsp, *oinp;
254 	int s = splimp();
255 
256 	for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb);) {
257 		if (!ns_hosteq(*dst,nsp->nsp_faddr)) {
258 	next:
259 			nsp = nsp->nsp_next;
260 			continue;
261 		}
262 		if (nsp->nsp_socket == 0)
263 			goto next;
264 		if (errno)
265 			nsp->nsp_socket->so_error = errno;
266 		oinp = nsp;
267 		nsp = nsp->nsp_next;
268 		oinp->nsp_notify_param = param;
269 		(*notify)(oinp);
270 	}
271 	splx(s);
272 }
273 
274 #ifdef notdef
275 /*
276  * After a routing change, flush old routing
277  * and allocate a (hopefully) better one.
278  */
279 ns_rtchange(nsp)
280 	struct nspcb *nsp;
281 {
282 	if (nsp->nsp_route.ro_rt) {
283 		rtfree(nsp->nsp_route.ro_rt);
284 		nsp->nsp_route.ro_rt = 0;
285 		/*
286 		 * A new route can be allocated the next time
287 		 * output is attempted.
288 		 */
289 	}
290 	/* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
291 }
292 #endif
293 
294 struct nspcb *
295 ns_pcblookup(faddr, lport, wildp)
296 	struct ns_addr *faddr;
297 	u_short lport;
298 {
299 	register struct nspcb *nsp, *match = 0;
300 	int matchwild = 3, wildcard;
301 	u_short fport;
302 
303 	fport = faddr->x_port;
304 	for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb); nsp = nsp->nsp_next) {
305 		if (nsp->nsp_lport != lport)
306 			continue;
307 		wildcard = 0;
308 		if (ns_nullhost(nsp->nsp_faddr)) {
309 			if (!ns_nullhost(*faddr))
310 				wildcard++;
311 		} else {
312 			if (ns_nullhost(*faddr))
313 				wildcard++;
314 			else {
315 				if (!ns_hosteq(nsp->nsp_faddr, *faddr))
316 					continue;
317 				if (nsp->nsp_fport != fport) {
318 					if (nsp->nsp_fport != 0)
319 						continue;
320 					else
321 						wildcard++;
322 				}
323 			}
324 		}
325 		if (wildcard && wildp==0)
326 			continue;
327 		if (wildcard < matchwild) {
328 			match = nsp;
329 			matchwild = wildcard;
330 			if (wildcard == 0)
331 				break;
332 		}
333 	}
334 	return (match);
335 }
336