xref: /original-bsd/sys/netns/ns_pcb.c (revision 6b3572dd)
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.9 (Berkeley) 06/28/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 (ro->ro_rt) {
123 		if (nsp->nsp_socket->so_options & SO_DONTROUTE)
124 			goto flush;
125 		if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
126 			goto flush;
127 		if (!ns_hosteq(nsp->nsp_lastdst, sns->sns_addr)) {
128 			if (((ro->ro_rt->rt_flags & (RTF_GATEWAY|RTF_HOST))
129 			     == RTF_GATEWAY)
130 			    || ((ifp = ro->ro_rt->rt_ifp) &&
131 				 !(ifp->if_flags & IFF_POINTOPOINT))) {
132 				/* can patch route to avoid rtalloc */
133 				*dst = sns->sns_addr;
134 			} else {
135 		flush:
136 				RTFREE(ro->ro_rt);
137 				ro->ro_rt = (struct rtentry *)0;
138 			}
139 		}/* else cached route is ok; do nothing */
140 	}
141 	nsp->nsp_lastdst = sns->sns_addr;
142 	if ((nsp->nsp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
143 	    (ro->ro_rt == (struct rtentry *)0 ||
144 	     ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
145 		    /* No route yet, so try to acquire one */
146 		    ro->ro_dst.sa_family = AF_NS;
147 		    ro->ro_dst.sa_len = sizeof(ro->ro_dst);
148 		    *dst = sns->sns_addr;
149 		    dst->x_port = 0;
150 		    rtalloc(ro);
151 	}
152 	if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_zeronet)) {
153 		/*
154 		 * If route is known or can be allocated now,
155 		 * our src addr is taken from the i/f, else punt.
156 		 */
157 
158 		ia = (struct ns_ifaddr *)0;
159 		/*
160 		 * If we found a route, use the address
161 		 * corresponding to the outgoing interface
162 		 */
163 		if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp))
164 			for (ia = ns_ifaddr; ia; ia = ia->ia_next)
165 				if (ia->ia_ifp == ifp)
166 					break;
167 		if (ia == 0) {
168 			u_short fport = sns->sns_addr.x_port;
169 			sns->sns_addr.x_port = 0;
170 			ia = (struct ns_ifaddr *)
171 				ifa_ifwithdstaddr((struct sockaddr *)sns);
172 			sns->sns_addr.x_port = fport;
173 			if (ia == 0)
174 				ia = ns_iaonnetof(&sns->sns_addr);
175 			if (ia == 0)
176 				ia = ns_ifaddr;
177 			if (ia == 0)
178 				return (EADDRNOTAVAIL);
179 		}
180 		nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
181 	}
182 	if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
183 		return (EADDRINUSE);
184 	if (ns_nullhost(nsp->nsp_laddr)) {
185 		if (nsp->nsp_lport == 0)
186 			(void) ns_pcbbind(nsp, (struct mbuf *)0);
187 		nsp->nsp_laddr.x_host = ns_thishost;
188 	}
189 	nsp->nsp_faddr = sns->sns_addr;
190 	/* Includes nsp->nsp_fport = sns->sns_port; */
191 	return (0);
192 }
193 
194 ns_pcbdisconnect(nsp)
195 	struct nspcb *nsp;
196 {
197 
198 	nsp->nsp_faddr = zerons_addr;
199 	if (nsp->nsp_socket->so_state & SS_NOFDREF)
200 		ns_pcbdetach(nsp);
201 }
202 
203 ns_pcbdetach(nsp)
204 	struct nspcb *nsp;
205 {
206 	struct socket *so = nsp->nsp_socket;
207 
208 	so->so_pcb = 0;
209 	sofree(so);
210 	if (nsp->nsp_route.ro_rt)
211 		rtfree(nsp->nsp_route.ro_rt);
212 	remque(nsp);
213 	(void) m_free(dtom(nsp));
214 }
215 
216 ns_setsockaddr(nsp, nam)
217 	register struct nspcb *nsp;
218 	struct mbuf *nam;
219 {
220 	register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
221 
222 	nam->m_len = sizeof (*sns);
223 	sns = mtod(nam, struct sockaddr_ns *);
224 	bzero((caddr_t)sns, sizeof (*sns));
225 	sns->sns_len = sizeof(*sns);
226 	sns->sns_family = AF_NS;
227 	sns->sns_addr = nsp->nsp_laddr;
228 }
229 
230 ns_setpeeraddr(nsp, nam)
231 	register struct nspcb *nsp;
232 	struct mbuf *nam;
233 {
234 	register struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
235 
236 	nam->m_len = sizeof (*sns);
237 	sns = mtod(nam, struct sockaddr_ns *);
238 	bzero((caddr_t)sns, sizeof (*sns));
239 	sns->sns_len = sizeof(*sns);
240 	sns->sns_family = AF_NS;
241 	sns->sns_addr  = nsp->nsp_faddr;
242 }
243 
244 /*
245  * Pass some notification to all connections of a protocol
246  * associated with address dst.  Call the
247  * protocol specific routine to handle each connection.
248  * Also pass an extra paramter via the nspcb. (which may in fact
249  * be a parameter list!)
250  */
251 ns_pcbnotify(dst, errno, notify, param)
252 	register struct ns_addr *dst;
253 	long param;
254 	int errno, (*notify)();
255 {
256 	register struct nspcb *nsp, *oinp;
257 	int s = splimp();
258 
259 	for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb);) {
260 		if (!ns_hosteq(*dst,nsp->nsp_faddr)) {
261 	next:
262 			nsp = nsp->nsp_next;
263 			continue;
264 		}
265 		if (nsp->nsp_socket == 0)
266 			goto next;
267 		if (errno)
268 			nsp->nsp_socket->so_error = errno;
269 		oinp = nsp;
270 		nsp = nsp->nsp_next;
271 		oinp->nsp_notify_param = param;
272 		(*notify)(oinp);
273 	}
274 	splx(s);
275 }
276 
277 #ifdef notdef
278 /*
279  * After a routing change, flush old routing
280  * and allocate a (hopefully) better one.
281  */
282 ns_rtchange(nsp)
283 	struct nspcb *nsp;
284 {
285 	if (nsp->nsp_route.ro_rt) {
286 		rtfree(nsp->nsp_route.ro_rt);
287 		nsp->nsp_route.ro_rt = 0;
288 		/*
289 		 * A new route can be allocated the next time
290 		 * output is attempted.
291 		 */
292 	}
293 	/* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
294 }
295 #endif
296 
297 struct nspcb *
298 ns_pcblookup(faddr, lport, wildp)
299 	struct ns_addr *faddr;
300 	u_short lport;
301 {
302 	register struct nspcb *nsp, *match = 0;
303 	int matchwild = 3, wildcard;
304 	u_short fport;
305 
306 	fport = faddr->x_port;
307 	for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb); nsp = nsp->nsp_next) {
308 		if (nsp->nsp_lport != lport)
309 			continue;
310 		wildcard = 0;
311 		if (ns_nullhost(nsp->nsp_faddr)) {
312 			if (!ns_nullhost(*faddr))
313 				wildcard++;
314 		} else {
315 			if (ns_nullhost(*faddr))
316 				wildcard++;
317 			else {
318 				if (!ns_hosteq(nsp->nsp_faddr, *faddr))
319 					continue;
320 				if (nsp->nsp_fport != fport) {
321 					if (nsp->nsp_fport != 0)
322 						continue;
323 					else
324 						wildcard++;
325 				}
326 			}
327 		}
328 		if (wildcard && wildp==0)
329 			continue;
330 		if (wildcard < matchwild) {
331 			match = nsp;
332 			matchwild = wildcard;
333 			if (wildcard == 0)
334 				break;
335 		}
336 	}
337 	return (match);
338 }
339