xref: /freebsd/sys/netinet/in_pcb.c (revision a1a4f1a0d87b594d3f17a97dc0127eec1417e6f6)
1 /*
2  * Copyright (c) 1982, 1986, 1991, 1993, 1995
3  *	The Regents of the University of California.  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  *
33  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
34  * $FreeBSD$
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/proc.h>
45 #include <sys/jail.h>
46 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 
49 #include <machine/limits.h>
50 
51 #include <vm/vm_zone.h>
52 
53 #include <net/if.h>
54 #include <net/route.h>
55 
56 #include <netinet/in.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip_var.h>
60 
61 struct	in_addr zeroin_addr;
62 
63 static void	in_pcbremlists __P((struct inpcb *));
64 static void	in_rtchange __P((struct inpcb *, int));
65 
66 /*
67  * These configure the range of local port addresses assigned to
68  * "unspecified" outgoing connections/packets/whatever.
69  */
70 static int ipport_lowfirstauto  = IPPORT_RESERVED - 1;	/* 1023 */
71 static int ipport_lowlastauto = IPPORT_RESERVEDSTART;	/* 600 */
72 static int ipport_firstauto = IPPORT_RESERVED;		/* 1024 */
73 static int ipport_lastauto  = IPPORT_USERRESERVED;	/* 5000 */
74 static int ipport_hifirstauto = IPPORT_HIFIRSTAUTO;	/* 49152 */
75 static int ipport_hilastauto  = IPPORT_HILASTAUTO;	/* 65535 */
76 
77 #define RANGECHK(var, min, max) \
78 	if ((var) < (min)) { (var) = (min); } \
79 	else if ((var) > (max)) { (var) = (max); }
80 
81 static int
82 sysctl_net_ipport_check SYSCTL_HANDLER_ARGS
83 {
84 	int error = sysctl_handle_int(oidp,
85 		oidp->oid_arg1, oidp->oid_arg2, req);
86 	if (!error) {
87 		RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
88 		RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
89 		RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
90 		RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
91 		RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
92 		RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
93 	}
94 	return error;
95 }
96 
97 #undef RANGECHK
98 
99 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
100 
101 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
102 	   &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
103 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
104 	   &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
105 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
106 	   &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
107 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
108 	   &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
109 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
110 	   &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
111 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
112 	   &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
113 
114 /*
115  * in_pcb.c: manage the Protocol Control Blocks.
116  *
117  * NOTE: It is assumed that most of these functions will be called at
118  * splnet(). XXX - There are, unfortunately, a few exceptions to this
119  * rule that should be fixed.
120  */
121 
122 /*
123  * Allocate a PCB and associate it with the socket.
124  */
125 int
126 in_pcballoc(so, pcbinfo, p)
127 	struct socket *so;
128 	struct inpcbinfo *pcbinfo;
129 	struct proc *p;
130 {
131 	register struct inpcb *inp;
132 
133 	inp = zalloci(pcbinfo->ipi_zone);
134 	if (inp == NULL)
135 		return (ENOBUFS);
136 	bzero((caddr_t)inp, sizeof(*inp));
137 	inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
138 	inp->inp_pcbinfo = pcbinfo;
139 	inp->inp_socket = so;
140 	LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
141 	pcbinfo->ipi_count++;
142 	so->so_pcb = (caddr_t)inp;
143 	return (0);
144 }
145 
146 int
147 in_pcbbind(inp, nam, p)
148 	register struct inpcb *inp;
149 	struct sockaddr *nam;
150 	struct proc *p;
151 {
152 	register struct socket *so = inp->inp_socket;
153 	unsigned short *lastport;
154 	struct sockaddr_in *sin;
155 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
156 	u_short lport = 0;
157 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
158 	int error, prison = 0;
159 
160 	if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
161 		return (EADDRNOTAVAIL);
162 	if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
163 		return (EINVAL);
164 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
165 		wild = 1;
166 	if (nam) {
167 		sin = (struct sockaddr_in *)nam;
168 		if (nam->sa_len != sizeof (*sin))
169 			return (EINVAL);
170 #ifdef notdef
171 		/*
172 		 * We should check the family, but old programs
173 		 * incorrectly fail to initialize it.
174 		 */
175 		if (sin->sin_family != AF_INET)
176 			return (EAFNOSUPPORT);
177 #endif
178 		if (prison_ip(p, 0, &sin->sin_addr.s_addr))
179 			return(EINVAL);
180 		lport = sin->sin_port;
181 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
182 			/*
183 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
184 			 * allow complete duplication of binding if
185 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
186 			 * and a multicast address is bound on both
187 			 * new and duplicated sockets.
188 			 */
189 			if (so->so_options & SO_REUSEADDR)
190 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
191 		} else if (sin->sin_addr.s_addr != INADDR_ANY) {
192 			sin->sin_port = 0;		/* yech... */
193 			if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
194 				return (EADDRNOTAVAIL);
195 		}
196 		if (lport) {
197 			struct inpcb *t;
198 
199 			/* GROSS */
200 			if (ntohs(lport) < IPPORT_RESERVED && p &&
201 			    suser_xxx(0, p, PRISON_ROOT))
202 				return (EACCES);
203 			if (p && p->p_prison)
204 				prison = 1;
205 			if (so->so_cred && so->so_cred->p_ruid != 0 &&
206 			    !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
207 				t = in_pcblookup_local(inp->inp_pcbinfo,
208 				    sin->sin_addr, lport,
209 				    prison ? 0 :  INPLOOKUP_WILDCARD);
210 				if (t &&
211 				    (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
212 				     ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
213 				     (t->inp_socket->so_options &
214 					 SO_REUSEPORT) == 0) &&
215 				    (t->inp_socket->so_cred) &&
216 				    (so->so_cred->p_ruid !=
217 					t->inp_socket->so_cred->p_ruid))
218 					return (EADDRINUSE);
219 			}
220 			t = in_pcblookup_local(pcbinfo, sin->sin_addr,
221 			    lport, prison ? 0 : wild);
222 			if (t && (reuseport & t->inp_socket->so_options) == 0)
223 				return (EADDRINUSE);
224 		}
225 		inp->inp_laddr = sin->sin_addr;
226 	}
227 	if (lport == 0) {
228 		ushort first, last;
229 		int count;
230 
231 		if (prison_ip(p, 0, &inp->inp_laddr.s_addr ))
232 			return (EINVAL);
233 		inp->inp_flags |= INP_ANONPORT;
234 
235 		if (inp->inp_flags & INP_HIGHPORT) {
236 			first = ipport_hifirstauto;	/* sysctl */
237 			last  = ipport_hilastauto;
238 			lastport = &pcbinfo->lasthi;
239 		} else if (inp->inp_flags & INP_LOWPORT) {
240 			if (p && (error = suser_xxx(0, p, PRISON_ROOT)))
241 				return error;
242 			first = ipport_lowfirstauto;	/* 1023 */
243 			last  = ipport_lowlastauto;	/* 600 */
244 			lastport = &pcbinfo->lastlow;
245 		} else {
246 			first = ipport_firstauto;	/* sysctl */
247 			last  = ipport_lastauto;
248 			lastport = &pcbinfo->lastport;
249 		}
250 		/*
251 		 * Simple check to ensure all ports are not used up causing
252 		 * a deadlock here.
253 		 *
254 		 * We split the two cases (up and down) so that the direction
255 		 * is not being tested on each round of the loop.
256 		 */
257 		if (first > last) {
258 			/*
259 			 * counting down
260 			 */
261 			count = first - last;
262 
263 			do {
264 				if (count-- < 0) {	/* completely used? */
265 					/*
266 					 * Undo any address bind that may have
267 					 * occurred above.
268 					 */
269 					inp->inp_laddr.s_addr = INADDR_ANY;
270 					return (EAGAIN);
271 				}
272 				--*lastport;
273 				if (*lastport > first || *lastport < last)
274 					*lastport = first;
275 				lport = htons(*lastport);
276 			} while (in_pcblookup_local(pcbinfo,
277 				 inp->inp_laddr, lport, wild));
278 		} else {
279 			/*
280 			 * counting up
281 			 */
282 			count = last - first;
283 
284 			do {
285 				if (count-- < 0) {	/* completely used? */
286 					/*
287 					 * Undo any address bind that may have
288 					 * occurred above.
289 					 */
290 					inp->inp_laddr.s_addr = INADDR_ANY;
291 					return (EAGAIN);
292 				}
293 				++*lastport;
294 				if (*lastport < first || *lastport > last)
295 					*lastport = first;
296 				lport = htons(*lastport);
297 			} while (in_pcblookup_local(pcbinfo,
298 				 inp->inp_laddr, lport, wild));
299 		}
300 	}
301 	inp->inp_lport = lport;
302 	if (in_pcbinshash(inp) != 0) {
303 		inp->inp_laddr.s_addr = INADDR_ANY;
304 		inp->inp_lport = 0;
305 		return (EAGAIN);
306 	}
307 	return (0);
308 }
309 
310 /*
311  *   Transform old in_pcbconnect() into an inner subroutine for new
312  *   in_pcbconnect(): Do some validity-checking on the remote
313  *   address (in mbuf 'nam') and then determine local host address
314  *   (i.e., which interface) to use to access that remote host.
315  *
316  *   This preserves definition of in_pcbconnect(), while supporting a
317  *   slightly different version for T/TCP.  (This is more than
318  *   a bit of a kludge, but cleaning up the internal interfaces would
319  *   have forced minor changes in every protocol).
320  */
321 
322 int
323 in_pcbladdr(inp, nam, plocal_sin)
324 	register struct inpcb *inp;
325 	struct sockaddr *nam;
326 	struct sockaddr_in **plocal_sin;
327 {
328 	struct in_ifaddr *ia;
329 	register struct sockaddr_in *sin = (struct sockaddr_in *)nam;
330 
331 	if (nam->sa_len != sizeof (*sin))
332 		return (EINVAL);
333 	if (sin->sin_family != AF_INET)
334 		return (EAFNOSUPPORT);
335 	if (sin->sin_port == 0)
336 		return (EADDRNOTAVAIL);
337 	if (!TAILQ_EMPTY(&in_ifaddrhead)) {
338 		/*
339 		 * If the destination address is INADDR_ANY,
340 		 * use the primary local address.
341 		 * If the supplied address is INADDR_BROADCAST,
342 		 * and the primary interface supports broadcast,
343 		 * choose the broadcast address for that interface.
344 		 */
345 #define	satosin(sa)	((struct sockaddr_in *)(sa))
346 #define sintosa(sin)	((struct sockaddr *)(sin))
347 #define ifatoia(ifa)	((struct in_ifaddr *)(ifa))
348 		if (sin->sin_addr.s_addr == INADDR_ANY)
349 		    sin->sin_addr = IA_SIN(in_ifaddrhead.tqh_first)->sin_addr;
350 		else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
351 		  (in_ifaddrhead.tqh_first->ia_ifp->if_flags & IFF_BROADCAST))
352 		    sin->sin_addr = satosin(&in_ifaddrhead.tqh_first->ia_broadaddr)->sin_addr;
353 	}
354 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
355 		register struct route *ro;
356 
357 		ia = (struct in_ifaddr *)0;
358 		/*
359 		 * If route is known or can be allocated now,
360 		 * our src addr is taken from the i/f, else punt.
361 		 */
362 		ro = &inp->inp_route;
363 		if (ro->ro_rt &&
364 		    (satosin(&ro->ro_dst)->sin_addr.s_addr !=
365 			sin->sin_addr.s_addr ||
366 		    inp->inp_socket->so_options & SO_DONTROUTE)) {
367 			RTFREE(ro->ro_rt);
368 			ro->ro_rt = (struct rtentry *)0;
369 		}
370 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
371 		    (ro->ro_rt == (struct rtentry *)0 ||
372 		    ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
373 			/* No route yet, so try to acquire one */
374 			ro->ro_dst.sa_family = AF_INET;
375 			ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
376 			((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
377 				sin->sin_addr;
378 			rtalloc(ro);
379 		}
380 		/*
381 		 * If we found a route, use the address
382 		 * corresponding to the outgoing interface
383 		 * unless it is the loopback (in case a route
384 		 * to our address on another net goes to loopback).
385 		 */
386 		if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
387 			ia = ifatoia(ro->ro_rt->rt_ifa);
388 		if (ia == 0) {
389 			u_short fport = sin->sin_port;
390 
391 			sin->sin_port = 0;
392 			ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
393 			if (ia == 0)
394 				ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
395 			sin->sin_port = fport;
396 			if (ia == 0)
397 				ia = in_ifaddrhead.tqh_first;
398 			if (ia == 0)
399 				return (EADDRNOTAVAIL);
400 		}
401 		/*
402 		 * If the destination address is multicast and an outgoing
403 		 * interface has been set as a multicast option, use the
404 		 * address of that interface as our source address.
405 		 */
406 		if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
407 		    inp->inp_moptions != NULL) {
408 			struct ip_moptions *imo;
409 			struct ifnet *ifp;
410 
411 			imo = inp->inp_moptions;
412 			if (imo->imo_multicast_ifp != NULL) {
413 				ifp = imo->imo_multicast_ifp;
414 				for (ia = in_ifaddrhead.tqh_first; ia;
415 				     ia = ia->ia_link.tqe_next)
416 					if (ia->ia_ifp == ifp)
417 						break;
418 				if (ia == 0)
419 					return (EADDRNOTAVAIL);
420 			}
421 		}
422 	/*
423 	 * Don't do pcblookup call here; return interface in plocal_sin
424 	 * and exit to caller, that will do the lookup.
425 	 */
426 		*plocal_sin = &ia->ia_addr;
427 
428 	}
429 	return(0);
430 }
431 
432 /*
433  * Outer subroutine:
434  * Connect from a socket to a specified address.
435  * Both address and port must be specified in argument sin.
436  * If don't have a local address for this socket yet,
437  * then pick one.
438  */
439 int
440 in_pcbconnect(inp, nam, p)
441 	register struct inpcb *inp;
442 	struct sockaddr *nam;
443 	struct proc *p;
444 {
445 	struct sockaddr_in *ifaddr;
446 	register struct sockaddr_in *sin = (struct sockaddr_in *)nam;
447 	int error;
448 
449 	/*
450 	 *   Call inner routine, to assign local interface address.
451 	 */
452 	if ((error = in_pcbladdr(inp, nam, &ifaddr)) != 0)
453 		return(error);
454 
455 	if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
456 	    inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
457 	    inp->inp_lport, 0) != NULL) {
458 		return (EADDRINUSE);
459 	}
460 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
461 		if (inp->inp_lport == 0) {
462 			error = in_pcbbind(inp, (struct sockaddr *)0, p);
463 			if (error)
464 			    return (error);
465 		}
466 		inp->inp_laddr = ifaddr->sin_addr;
467 	}
468 	inp->inp_faddr = sin->sin_addr;
469 	inp->inp_fport = sin->sin_port;
470 	in_pcbrehash(inp);
471 	return (0);
472 }
473 
474 void
475 in_pcbdisconnect(inp)
476 	struct inpcb *inp;
477 {
478 
479 	inp->inp_faddr.s_addr = INADDR_ANY;
480 	inp->inp_fport = 0;
481 	in_pcbrehash(inp);
482 	if (inp->inp_socket->so_state & SS_NOFDREF)
483 		in_pcbdetach(inp);
484 }
485 
486 void
487 in_pcbdetach(inp)
488 	struct inpcb *inp;
489 {
490 	struct socket *so = inp->inp_socket;
491 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
492 
493 	inp->inp_gencnt = ++ipi->ipi_gencnt;
494 	in_pcbremlists(inp);
495 	so->so_pcb = 0;
496 	sofree(so);
497 	if (inp->inp_options)
498 		(void)m_free(inp->inp_options);
499 	if (inp->inp_route.ro_rt)
500 		rtfree(inp->inp_route.ro_rt);
501 	ip_freemoptions(inp->inp_moptions);
502 	zfreei(ipi->ipi_zone, inp);
503 }
504 
505 /*
506  * The calling convention of in_setsockaddr() and in_setpeeraddr() was
507  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
508  * in struct pr_usrreqs, so that protocols can just reference then directly
509  * without the need for a wrapper function.  The socket must have a valid
510  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
511  * except through a kernel programming error, so it is acceptable to panic
512  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
513  * because there actually /is/ a programming error somewhere... XXX)
514  */
515 int
516 in_setsockaddr(so, nam)
517 	struct socket *so;
518 	struct sockaddr **nam;
519 {
520 	int s;
521 	register struct inpcb *inp;
522 	register struct sockaddr_in *sin;
523 
524 	/*
525 	 * Do the malloc first in case it blocks.
526 	 */
527 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, M_WAITOK);
528 	bzero(sin, sizeof *sin);
529 	sin->sin_family = AF_INET;
530 	sin->sin_len = sizeof(*sin);
531 
532 	s = splnet();
533 	inp = sotoinpcb(so);
534 	if (!inp) {
535 		splx(s);
536 		free(sin, M_SONAME);
537 		return EINVAL;
538 	}
539 	sin->sin_port = inp->inp_lport;
540 	sin->sin_addr = inp->inp_laddr;
541 	splx(s);
542 
543 	*nam = (struct sockaddr *)sin;
544 	return 0;
545 }
546 
547 int
548 in_setpeeraddr(so, nam)
549 	struct socket *so;
550 	struct sockaddr **nam;
551 {
552 	int s;
553 	struct inpcb *inp;
554 	register struct sockaddr_in *sin;
555 
556 	/*
557 	 * Do the malloc first in case it blocks.
558 	 */
559 	MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, M_WAITOK);
560 	bzero((caddr_t)sin, sizeof (*sin));
561 	sin->sin_family = AF_INET;
562 	sin->sin_len = sizeof(*sin);
563 
564 	s = splnet();
565 	inp = sotoinpcb(so);
566 	if (!inp) {
567 		splx(s);
568 		free(sin, M_SONAME);
569 		return EINVAL;
570 	}
571 	sin->sin_port = inp->inp_fport;
572 	sin->sin_addr = inp->inp_faddr;
573 	splx(s);
574 
575 	*nam = (struct sockaddr *)sin;
576 	return 0;
577 }
578 
579 /*
580  * Pass some notification to all connections of a protocol
581  * associated with address dst.  The local address and/or port numbers
582  * may be specified to limit the search.  The "usual action" will be
583  * taken, depending on the ctlinput cmd.  The caller must filter any
584  * cmds that are uninteresting (e.g., no error in the map).
585  * Call the protocol specific routine (if any) to report
586  * any errors for each matching socket.
587  */
588 void
589 in_pcbnotify(head, dst, fport_arg, laddr, lport_arg, cmd, notify)
590 	struct inpcbhead *head;
591 	struct sockaddr *dst;
592 	u_int fport_arg, lport_arg;
593 	struct in_addr laddr;
594 	int cmd;
595 	void (*notify) __P((struct inpcb *, int));
596 {
597 	register struct inpcb *inp, *oinp;
598 	struct in_addr faddr;
599 	u_short fport = fport_arg, lport = lport_arg;
600 	int errno, s;
601 
602 	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET)
603 		return;
604 	faddr = ((struct sockaddr_in *)dst)->sin_addr;
605 	if (faddr.s_addr == INADDR_ANY)
606 		return;
607 
608 	/*
609 	 * Redirects go to all references to the destination,
610 	 * and use in_rtchange to invalidate the route cache.
611 	 * Dead host indications: notify all references to the destination.
612 	 * Otherwise, if we have knowledge of the local port and address,
613 	 * deliver only to that socket.
614 	 */
615 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
616 		fport = 0;
617 		lport = 0;
618 		laddr.s_addr = 0;
619 		if (cmd != PRC_HOSTDEAD)
620 			notify = in_rtchange;
621 	}
622 	errno = inetctlerrmap[cmd];
623 	s = splnet();
624 	for (inp = head->lh_first; inp != NULL;) {
625 		if (inp->inp_faddr.s_addr != faddr.s_addr ||
626 		    inp->inp_socket == 0 ||
627 		    (lport && inp->inp_lport != lport) ||
628 		    (laddr.s_addr && inp->inp_laddr.s_addr != laddr.s_addr) ||
629 		    (fport && inp->inp_fport != fport)) {
630 			inp = inp->inp_list.le_next;
631 			continue;
632 		}
633 		oinp = inp;
634 		inp = inp->inp_list.le_next;
635 		if (notify)
636 			(*notify)(oinp, errno);
637 	}
638 	splx(s);
639 }
640 
641 /*
642  * Check for alternatives when higher level complains
643  * about service problems.  For now, invalidate cached
644  * routing information.  If the route was created dynamically
645  * (by a redirect), time to try a default gateway again.
646  */
647 void
648 in_losing(inp)
649 	struct inpcb *inp;
650 {
651 	register struct rtentry *rt;
652 	struct rt_addrinfo info;
653 
654 	if ((rt = inp->inp_route.ro_rt)) {
655 		inp->inp_route.ro_rt = 0;
656 		bzero((caddr_t)&info, sizeof(info));
657 		info.rti_info[RTAX_DST] =
658 			(struct sockaddr *)&inp->inp_route.ro_dst;
659 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
660 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
661 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
662 		if (rt->rt_flags & RTF_DYNAMIC)
663 			(void) rtrequest(RTM_DELETE, rt_key(rt),
664 				rt->rt_gateway, rt_mask(rt), rt->rt_flags,
665 				(struct rtentry **)0);
666 		else
667 		/*
668 		 * A new route can be allocated
669 		 * the next time output is attempted.
670 		 */
671 			rtfree(rt);
672 	}
673 }
674 
675 /*
676  * After a routing change, flush old routing
677  * and allocate a (hopefully) better one.
678  */
679 static void
680 in_rtchange(inp, errno)
681 	register struct inpcb *inp;
682 	int errno;
683 {
684 	if (inp->inp_route.ro_rt) {
685 		rtfree(inp->inp_route.ro_rt);
686 		inp->inp_route.ro_rt = 0;
687 		/*
688 		 * A new route can be allocated the next time
689 		 * output is attempted.
690 		 */
691 	}
692 }
693 
694 /*
695  * Lookup a PCB based on the local address and port.
696  */
697 struct inpcb *
698 in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
699 	struct inpcbinfo *pcbinfo;
700 	struct in_addr laddr;
701 	u_int lport_arg;
702 	int wild_okay;
703 {
704 	register struct inpcb *inp;
705 	int matchwild = 3, wildcard;
706 	u_short lport = lport_arg;
707 
708 	if (!wild_okay) {
709 		struct inpcbhead *head;
710 		/*
711 		 * Look for an unconnected (wildcard foreign addr) PCB that
712 		 * matches the local address and port we're looking for.
713 		 */
714 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
715 		for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
716 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
717 			    inp->inp_laddr.s_addr == laddr.s_addr &&
718 			    inp->inp_lport == lport) {
719 				/*
720 				 * Found.
721 				 */
722 				return (inp);
723 			}
724 		}
725 		/*
726 		 * Not found.
727 		 */
728 		return (NULL);
729 	} else {
730 		struct inpcbporthead *porthash;
731 		struct inpcbport *phd;
732 		struct inpcb *match = NULL;
733 		/*
734 		 * Best fit PCB lookup.
735 		 *
736 		 * First see if this local port is in use by looking on the
737 		 * port hash list.
738 		 */
739 		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
740 		    pcbinfo->porthashmask)];
741 		for (phd = porthash->lh_first; phd != NULL; phd = phd->phd_hash.le_next) {
742 			if (phd->phd_port == lport)
743 				break;
744 		}
745 		if (phd != NULL) {
746 			/*
747 			 * Port is in use by one or more PCBs. Look for best
748 			 * fit.
749 			 */
750 			for (inp = phd->phd_pcblist.lh_first; inp != NULL;
751 			    inp = inp->inp_portlist.le_next) {
752 				wildcard = 0;
753 				if (inp->inp_faddr.s_addr != INADDR_ANY)
754 					wildcard++;
755 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
756 					if (laddr.s_addr == INADDR_ANY)
757 						wildcard++;
758 					else if (inp->inp_laddr.s_addr != laddr.s_addr)
759 						continue;
760 				} else {
761 					if (laddr.s_addr != INADDR_ANY)
762 						wildcard++;
763 				}
764 				if (wildcard < matchwild) {
765 					match = inp;
766 					matchwild = wildcard;
767 					if (matchwild == 0) {
768 						break;
769 					}
770 				}
771 			}
772 		}
773 		return (match);
774 	}
775 }
776 
777 /*
778  * Lookup PCB in hash list.
779  */
780 struct inpcb *
781 in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard)
782 	struct inpcbinfo *pcbinfo;
783 	struct in_addr faddr, laddr;
784 	u_int fport_arg, lport_arg;
785 	int wildcard;
786 {
787 	struct inpcbhead *head;
788 	register struct inpcb *inp;
789 	u_short fport = fport_arg, lport = lport_arg;
790 
791 	/*
792 	 * First look for an exact match.
793 	 */
794 	head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
795 	for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
796 		if (inp->inp_faddr.s_addr == faddr.s_addr &&
797 		    inp->inp_laddr.s_addr == laddr.s_addr &&
798 		    inp->inp_fport == fport &&
799 		    inp->inp_lport == lport) {
800 			/*
801 			 * Found.
802 			 */
803 			return (inp);
804 		}
805 	}
806 	if (wildcard) {
807 		struct inpcb *local_wild = NULL;
808 
809 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
810 		for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
811 			if (inp->inp_faddr.s_addr == INADDR_ANY &&
812 			    inp->inp_lport == lport) {
813 				if (inp->inp_laddr.s_addr == laddr.s_addr)
814 					return (inp);
815 				else if (inp->inp_laddr.s_addr == INADDR_ANY)
816 					local_wild = inp;
817 			}
818 		}
819 		return (local_wild);
820 	}
821 
822 	/*
823 	 * Not found.
824 	 */
825 	return (NULL);
826 }
827 
828 /*
829  * Insert PCB onto various hash lists.
830  */
831 int
832 in_pcbinshash(inp)
833 	struct inpcb *inp;
834 {
835 	struct inpcbhead *pcbhash;
836 	struct inpcbporthead *pcbporthash;
837 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
838 	struct inpcbport *phd;
839 
840 	pcbhash = &pcbinfo->hashbase[INP_PCBHASH(inp->inp_faddr.s_addr,
841 		 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
842 
843 	pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
844 	    pcbinfo->porthashmask)];
845 
846 	/*
847 	 * Go through port list and look for a head for this lport.
848 	 */
849 	for (phd = pcbporthash->lh_first; phd != NULL; phd = phd->phd_hash.le_next) {
850 		if (phd->phd_port == inp->inp_lport)
851 			break;
852 	}
853 	/*
854 	 * If none exists, malloc one and tack it on.
855 	 */
856 	if (phd == NULL) {
857 		MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
858 		if (phd == NULL) {
859 			return (ENOBUFS); /* XXX */
860 		}
861 		phd->phd_port = inp->inp_lport;
862 		LIST_INIT(&phd->phd_pcblist);
863 		LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
864 	}
865 	inp->inp_phd = phd;
866 	LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
867 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
868 	return (0);
869 }
870 
871 /*
872  * Move PCB to the proper hash bucket when { faddr, fport } have  been
873  * changed. NOTE: This does not handle the case of the lport changing (the
874  * hashed port list would have to be updated as well), so the lport must
875  * not change after in_pcbinshash() has been called.
876  */
877 void
878 in_pcbrehash(inp)
879 	struct inpcb *inp;
880 {
881 	struct inpcbhead *head;
882 
883 	head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(inp->inp_faddr.s_addr,
884 		inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)];
885 
886 	LIST_REMOVE(inp, inp_hash);
887 	LIST_INSERT_HEAD(head, inp, inp_hash);
888 }
889 
890 /*
891  * Remove PCB from various lists.
892  */
893 static void
894 in_pcbremlists(inp)
895 	struct inpcb *inp;
896 {
897 	inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
898 	if (inp->inp_lport) {
899 		struct inpcbport *phd = inp->inp_phd;
900 
901 		LIST_REMOVE(inp, inp_hash);
902 		LIST_REMOVE(inp, inp_portlist);
903 		if (phd->phd_pcblist.lh_first == NULL) {
904 			LIST_REMOVE(phd, phd_hash);
905 			free(phd, M_PCB);
906 		}
907 	}
908 	LIST_REMOVE(inp, inp_list);
909 	inp->inp_pcbinfo->ipi_count--;
910 }
911 
912 int
913 prison_xinpcb(struct proc *p, struct inpcb *inp)
914 {
915 	if (!p->p_prison)
916 		return (0);
917 	if (ntohl(inp->inp_laddr.s_addr) == p->p_prison->pr_ip)
918 		return (0);
919 	return (1);
920 }
921