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