xref: /original-bsd/sys/netinet/in_pcb.c (revision f66f3413)
1 /*
2  * Copyright (c) 1982, 1986, 1991 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)in_pcb.c	7.25 (Berkeley) 03/02/93
8  */
9 
10 #include <sys/param.h>
11 #include <sys/systm.h>
12 #include <sys/malloc.h>
13 #include <sys/mbuf.h>
14 #include <sys/protosw.h>
15 #include <sys/socket.h>
16 #include <sys/socketvar.h>
17 #include <sys/ioctl.h>
18 #include <sys/errno.h>
19 
20 #include <net/if.h>
21 #include <net/route.h>
22 
23 #include <netinet/in.h>
24 #include <netinet/in_systm.h>
25 #include <netinet/ip.h>
26 #include <netinet/in_pcb.h>
27 #include <netinet/in_var.h>
28 
29 #include <netinet/ip_var.h>
30 
31 struct	in_addr zeroin_addr;
32 
33 in_pcballoc(so, head)
34 	struct socket *so;
35 	struct inpcb *head;
36 {
37 	struct mbuf *m;
38 	register struct inpcb *inp;
39 
40 	MALLOC(inp, struct inpcb *, sizeof(*inp), M_PCB, M_WAITOK);
41 	if (inp == NULL)
42 		return (ENOBUFS);
43 	bzero((caddr_t)inp, sizeof(*inp));
44 	inp->inp_head = head;
45 	inp->inp_socket = so;
46 	insque(inp, head);
47 	so->so_pcb = (caddr_t)inp;
48 	return (0);
49 }
50 
51 in_pcbbind(inp, nam)
52 	register struct inpcb *inp;
53 	struct mbuf *nam;
54 {
55 	register struct socket *so = inp->inp_socket;
56 	register struct inpcb *head = inp->inp_head;
57 	register struct sockaddr_in *sin;
58 	u_short lport = 0;
59 	int wild = 0;
60 
61 	if (in_ifaddr == 0)
62 		return (EADDRNOTAVAIL);
63 	if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
64 		return (EINVAL);
65 	if ((so->so_options & SO_REUSEADDR) == 0 &&
66 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
67 	     (so->so_options & SO_ACCEPTCONN) == 0))
68 		wild = INPLOOKUP_WILDCARD;
69 	if (nam == 0)
70 		goto noname;
71 	sin = mtod(nam, struct sockaddr_in *);
72 	if (nam->m_len != sizeof (*sin))
73 		return (EINVAL);
74 	if (sin->sin_addr.s_addr != INADDR_ANY) {
75 		int tport = sin->sin_port;
76 
77 		sin->sin_port = 0;		/* yech... */
78 		if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
79 			return (EADDRNOTAVAIL);
80 		sin->sin_port = tport;
81 	}
82 	lport = sin->sin_port;
83 	if (lport) {
84 		struct inpcb *t;
85 		u_short aport = ntohs(lport);
86 
87 		/* GROSS */
88 		if (aport < IPPORT_RESERVED && (so->so_state & SS_PRIV) == 0)
89 			return (EACCES);
90 		t = in_pcblookup(head, zeroin_addr, 0,
91 				sin->sin_addr, lport, wild);
92 		if (t && !((so->so_options & t->inp_socket->so_options) &
93 		    SO_REUSEPORT))
94 			return (EADDRINUSE);
95 	}
96 	inp->inp_laddr = sin->sin_addr;
97 noname:
98 	if (lport == 0)
99 		do {
100 			if (head->inp_lport++ < IPPORT_RESERVED ||
101 			    head->inp_lport > IPPORT_USERRESERVED)
102 				head->inp_lport = IPPORT_RESERVED;
103 			lport = htons(head->inp_lport);
104 		} while (in_pcblookup(head,
105 			    zeroin_addr, 0, inp->inp_laddr, lport, wild));
106 	inp->inp_lport = lport;
107 	return (0);
108 }
109 
110 /*
111  * Connect from a socket to a specified address.
112  * Both address and port must be specified in argument sin.
113  * If don't have a local address for this socket yet,
114  * then pick one.
115  */
116 in_pcbconnect(inp, nam)
117 	register struct inpcb *inp;
118 	struct mbuf *nam;
119 {
120 	struct in_ifaddr *ia;
121 	struct sockaddr_in *ifaddr;
122 	register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
123 
124 	if (nam->m_len != sizeof (*sin))
125 		return (EINVAL);
126 	if (sin->sin_family != AF_INET)
127 		return (EAFNOSUPPORT);
128 	if (sin->sin_port == 0)
129 		return (EADDRNOTAVAIL);
130 	if (in_ifaddr) {
131 		/*
132 		 * If the destination address is INADDR_ANY,
133 		 * use the primary local address.
134 		 * If the supplied address is INADDR_BROADCAST,
135 		 * and the primary interface supports broadcast,
136 		 * choose the broadcast address for that interface.
137 		 */
138 #define	satosin(sa)	((struct sockaddr_in *)(sa))
139 		if (sin->sin_addr.s_addr == INADDR_ANY)
140 		    sin->sin_addr = IA_SIN(in_ifaddr)->sin_addr;
141 		else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
142 		  (in_ifaddr->ia_ifp->if_flags & IFF_BROADCAST))
143 		    sin->sin_addr = satosin(&in_ifaddr->ia_broadaddr)->sin_addr;
144 	}
145 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
146 		register struct route *ro;
147 		struct ifnet *ifp;
148 
149 		ia = (struct in_ifaddr *)0;
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 		ro = &inp->inp_route;
155 		if (ro->ro_rt &&
156 		    (satosin(&ro->ro_dst)->sin_addr.s_addr !=
157 			sin->sin_addr.s_addr ||
158 		    inp->inp_socket->so_options & SO_DONTROUTE)) {
159 			RTFREE(ro->ro_rt);
160 			ro->ro_rt = (struct rtentry *)0;
161 		}
162 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
163 		    (ro->ro_rt == (struct rtentry *)0 ||
164 		    ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
165 			/* No route yet, so try to acquire one */
166 			ro->ro_dst.sa_family = AF_INET;
167 			ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
168 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
169 				sin->sin_addr;
170 			rtalloc(ro);
171 		}
172 		/*
173 		 * If we found a route, use the address
174 		 * corresponding to the outgoing interface
175 		 * unless it is the loopback (in case a route
176 		 * to our address on another net goes to loopback).
177 		 */
178 		if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp) &&
179 		    (ifp->if_flags & IFF_LOOPBACK) == 0 &&
180 		    (ia = (struct in_ifaddr *)ro->ro_rt->rt_ifa) == 0)
181 			for (ia = in_ifaddr; ia; ia = ia->ia_next)
182 				if (ia->ia_ifp == ifp)
183 					break;
184 		if (ia == 0) {
185 			int fport = sin->sin_port;
186 
187 			sin->sin_port = 0;
188 			ia = (struct in_ifaddr *)
189 			    ifa_ifwithdstaddr((struct sockaddr *)sin);
190 			sin->sin_port = fport;
191 			if (ia == 0)
192 				ia = in_iaonnetof(in_netof(sin->sin_addr));
193 			if (ia == 0)
194 				ia = in_ifaddr;
195 			if (ia == 0)
196 				return (EADDRNOTAVAIL);
197 		}
198 		/*
199 		 * If the destination address is multicast and an outgoing
200 		 * interface has been set as a multicast option, use the
201 		 * address of that interface as our source address.
202 		 */
203 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
204 		    inp->inp_moptions != NULL) {
205 			struct ip_moptions *imo;
206 
207 			imo = inp->inp_moptions;
208 			if (imo->imo_multicast_ifp != NULL) {
209 				ifp = imo->imo_multicast_ifp;
210 				for (ia = in_ifaddr; ia; ia = ia->ia_next)
211 					if (ia->ia_ifp == ifp)
212 						break;
213 				if (ia == 0)
214 					return (EADDRNOTAVAIL);
215 			}
216 		}
217 		ifaddr = (struct sockaddr_in *)&ia->ia_addr;
218 	}
219 	if (in_pcblookup(inp->inp_head,
220 	    sin->sin_addr,
221 	    sin->sin_port,
222 	    inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
223 	    inp->inp_lport,
224 	    0))
225 		return (EADDRINUSE);
226 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
227 		if (inp->inp_lport == 0)
228 			(void)in_pcbbind(inp, (struct mbuf *)0);
229 		inp->inp_laddr = ifaddr->sin_addr;
230 	}
231 	inp->inp_faddr = sin->sin_addr;
232 	inp->inp_fport = sin->sin_port;
233 	return (0);
234 }
235 
236 in_pcbdisconnect(inp)
237 	struct inpcb *inp;
238 {
239 
240 	inp->inp_faddr.s_addr = INADDR_ANY;
241 	inp->inp_fport = 0;
242 	if (inp->inp_socket->so_state & SS_NOFDREF)
243 		in_pcbdetach(inp);
244 }
245 
246 in_pcbdetach(inp)
247 	struct inpcb *inp;
248 {
249 	struct socket *so = inp->inp_socket;
250 
251 	so->so_pcb = 0;
252 	sofree(so);
253 	if (inp->inp_options)
254 		(void)m_free(inp->inp_options);
255 	if (inp->inp_route.ro_rt)
256 		rtfree(inp->inp_route.ro_rt);
257 	ip_freemoptions(inp->inp_moptions);
258 	remque(inp);
259 	FREE(inp, M_PCB);
260 }
261 
262 in_setsockaddr(inp, nam)
263 	register struct inpcb *inp;
264 	struct mbuf *nam;
265 {
266 	register struct sockaddr_in *sin;
267 
268 	nam->m_len = sizeof (*sin);
269 	sin = mtod(nam, struct sockaddr_in *);
270 	bzero((caddr_t)sin, sizeof (*sin));
271 	sin->sin_family = AF_INET;
272 	sin->sin_len = sizeof(*sin);
273 	sin->sin_port = inp->inp_lport;
274 	sin->sin_addr = inp->inp_laddr;
275 }
276 
277 in_setpeeraddr(inp, nam)
278 	struct inpcb *inp;
279 	struct mbuf *nam;
280 {
281 	register struct sockaddr_in *sin;
282 
283 	nam->m_len = sizeof (*sin);
284 	sin = mtod(nam, struct sockaddr_in *);
285 	bzero((caddr_t)sin, sizeof (*sin));
286 	sin->sin_family = AF_INET;
287 	sin->sin_len = sizeof(*sin);
288 	sin->sin_port = inp->inp_fport;
289 	sin->sin_addr = inp->inp_faddr;
290 }
291 
292 /*
293  * Pass some notification to all connections of a protocol
294  * associated with address dst.  The local address and/or port numbers
295  * may be specified to limit the search.  The "usual action" will be
296  * taken, depending on the ctlinput cmd.  The caller must filter any
297  * cmds that are uninteresting (e.g., no error in the map).
298  * Call the protocol specific routine (if any) to report
299  * any errors for each matching socket.
300  *
301  * Must be called at splnet.
302  */
303 in_pcbnotify(head, dst, fport, laddr, lport, cmd, notify)
304 	struct inpcb *head;
305 	struct sockaddr *dst;
306 	u_short fport, lport;
307 	struct in_addr laddr;
308 	int cmd, (*notify)();
309 {
310 	register struct inpcb *inp, *oinp;
311 	struct in_addr faddr;
312 	int errno;
313 	int in_rtchange();
314 	extern u_char inetctlerrmap[];
315 
316 	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET)
317 		return;
318 	faddr = ((struct sockaddr_in *)dst)->sin_addr;
319 	if (faddr.s_addr == INADDR_ANY)
320 		return;
321 
322 	/*
323 	 * Redirects go to all references to the destination,
324 	 * and use in_rtchange to invalidate the route cache.
325 	 * Dead host indications: notify all references to the destination.
326 	 * Otherwise, if we have knowledge of the local port and address,
327 	 * deliver only to that socket.
328 	 */
329 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
330 		fport = 0;
331 		lport = 0;
332 		laddr.s_addr = 0;
333 		if (cmd != PRC_HOSTDEAD)
334 			notify = in_rtchange;
335 	}
336 	errno = inetctlerrmap[cmd];
337 	for (inp = head->inp_next; inp != head;) {
338 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
339 		    inp->inp_socket == 0 ||
340 		    (lport && inp->inp_lport != lport) ||
341 		    (laddr.s_addr && inp->inp_laddr.s_addr != laddr.s_addr) ||
342 		    (fport && inp->inp_fport != fport)) {
343 			inp = inp->inp_next;
344 			continue;
345 		}
346 		oinp = inp;
347 		inp = inp->inp_next;
348 		if (notify)
349 			(*notify)(oinp, errno);
350 	}
351 }
352 
353 /*
354  * Check for alternatives when higher level complains
355  * about service problems.  For now, invalidate cached
356  * routing information.  If the route was created dynamically
357  * (by a redirect), time to try a default gateway again.
358  */
359 in_losing(inp)
360 	struct inpcb *inp;
361 {
362 	register struct rtentry *rt;
363 	struct rt_addrinfo info;
364 
365 	if ((rt = inp->inp_route.ro_rt)) {
366 		inp->inp_route.ro_rt = 0;
367 		bzero((caddr_t)&info, sizeof(info));
368 		info.rti_info[RTAX_DST] =
369 			(struct sockaddr *)&inp->inp_route.ro_dst;
370 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
371 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
372 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
373 		if (rt->rt_flags & RTF_DYNAMIC)
374 			(void) rtrequest(RTM_DELETE, rt_key(rt),
375 				rt->rt_gateway, rt_mask(rt), rt->rt_flags,
376 				(struct rtentry **)0);
377 		else
378 		/*
379 		 * A new route can be allocated
380 		 * the next time output is attempted.
381 		 */
382 			rtfree(rt);
383 	}
384 }
385 
386 /*
387  * After a routing change, flush old routing
388  * and allocate a (hopefully) better one.
389  */
390 in_rtchange(inp)
391 	register struct inpcb *inp;
392 {
393 	if (inp->inp_route.ro_rt) {
394 		rtfree(inp->inp_route.ro_rt);
395 		inp->inp_route.ro_rt = 0;
396 		/*
397 		 * A new route can be allocated the next time
398 		 * output is attempted.
399 		 */
400 	}
401 }
402 
403 struct inpcb *
404 in_pcblookup(head, faddr, fport, laddr, lport, flags)
405 	struct inpcb *head;
406 	struct in_addr faddr, laddr;
407 	u_short fport, lport;
408 	int flags;
409 {
410 	register struct inpcb *inp, *match = 0;
411 	int matchwild = 3, wildcard;
412 
413 	for (inp = head->inp_next; inp != head; inp = inp->inp_next) {
414 		if (inp->inp_lport != lport)
415 			continue;
416 		wildcard = 0;
417 		if (inp->inp_laddr.s_addr != INADDR_ANY) {
418 			if (laddr.s_addr == INADDR_ANY)
419 				wildcard++;
420 			else if (inp->inp_laddr.s_addr != laddr.s_addr)
421 				continue;
422 		} else {
423 			if (laddr.s_addr != INADDR_ANY)
424 				wildcard++;
425 		}
426 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
427 			if (faddr.s_addr == INADDR_ANY)
428 				wildcard++;
429 			else if (inp->inp_faddr.s_addr != faddr.s_addr ||
430 			    inp->inp_fport != fport)
431 				continue;
432 		} else {
433 			if (faddr.s_addr != INADDR_ANY)
434 				wildcard++;
435 		}
436 		if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
437 			continue;
438 		if (wildcard < matchwild) {
439 			match = inp;
440 			matchwild = wildcard;
441 			if (matchwild == 0)
442 				break;
443 		}
444 	}
445 	return (match);
446 }
447