2 * Copyright (c) 1982, 1986, 1991, 1993, 1995
3 * The Regents of the University of California.
4 * Copyright (c) 2007-2009 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_ipsec.h"
40 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
53 #include <sys/kernel.h>
54 #include <sys/sysctl.h>
63 #include <net/if_types.h>
64 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/in_var.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/tcp_var.h>
72 #include <netinet/udp.h>
73 #include <netinet/udp_var.h>
75 #include <netinet/ip6.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/in6_pcb.h>
82 #include <netipsec/ipsec.h>
83 #include <netipsec/key.h>
86 #include <security/mac/mac_framework.h>
89 * These configure the range of local port addresses assigned to
90 * "unspecified" outgoing connections/packets/whatever.
92 VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */
93 VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */
94 VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */
95 VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */
96 VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */
97 VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */
100 * Reserved ports accessible only to root. There are significant
101 * security considerations that must be accounted for when changing these,
102 * but the security benefits can be great. Please be careful.
104 VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */
105 VNET_DEFINE(int, ipport_reservedlow);
107 /* Variables dealing with random ephemeral port allocation. */
108 VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */
109 VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */
110 VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */
111 VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */
112 VNET_DEFINE(int, ipport_tcpallocs);
113 static VNET_DEFINE(int, ipport_tcplastcount);
115 #define V_ipport_tcplastcount VNET(ipport_tcplastcount)
117 #define RANGECHK(var, min, max) \
118 if ((var) < (min)) { (var) = (min); } \
119 else if ((var) > (max)) { (var) = (max); }
121 static void in_pcbremlists(struct inpcb *inp);
124 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
129 error = vnet_sysctl_handle_int(oidp, arg1, arg2, req);
131 error = sysctl_handle_int(oidp, arg1, arg2, req);
134 RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
135 RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
136 RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
137 RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
138 RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
139 RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
146 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
148 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
149 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0,
150 &sysctl_net_ipport_check, "I", "");
151 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
152 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0,
153 &sysctl_net_ipport_check, "I", "");
154 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first,
155 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0,
156 &sysctl_net_ipport_check, "I", "");
157 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last,
158 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0,
159 &sysctl_net_ipport_check, "I", "");
160 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
161 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0,
162 &sysctl_net_ipport_check, "I", "");
163 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
164 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0,
165 &sysctl_net_ipport_check, "I", "");
166 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
167 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, "");
168 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
169 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
170 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
171 &VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
172 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
173 &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
174 "allocations before switching to a sequental one");
175 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
176 &VNET_NAME(ipport_randomtime), 0,
177 "Minimum time to keep sequental port "
178 "allocation before switching to a random one");
181 * in_pcb.c: manage the Protocol Control Blocks.
183 * NOTE: It is assumed that most of these functions will be called with
184 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
185 * functions often modify hash chains or addresses in pcbs.
189 * Allocate a PCB and associate it with the socket.
190 * On success return with the PCB locked.
193 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
198 INP_INFO_WLOCK_ASSERT(pcbinfo);
200 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
203 bzero(inp, inp_zero_size);
204 inp->inp_pcbinfo = pcbinfo;
205 inp->inp_socket = so;
206 inp->inp_cred = crhold(so->so_cred);
207 inp->inp_inc.inc_fibnum = so->so_fibnum;
209 error = mac_inpcb_init(inp, M_NOWAIT);
212 mac_inpcb_create(so, inp);
215 error = ipsec_init_policy(so, &inp->inp_sp);
218 mac_inpcb_destroy(inp);
224 if (INP_SOCKAF(so) == AF_INET6) {
225 inp->inp_vflag |= INP_IPV6PROTO;
227 inp->inp_flags |= IN6P_IPV6_V6ONLY;
230 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
231 pcbinfo->ipi_count++;
232 so->so_pcb = (caddr_t)inp;
234 if (V_ip6_auto_flowlabel)
235 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
238 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
239 inp->inp_refcount = 1; /* Reference from the inpcbinfo */
240 #if defined(IPSEC) || defined(MAC)
243 crfree(inp->inp_cred);
244 uma_zfree(pcbinfo->ipi_zone, inp);
251 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
255 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
256 INP_WLOCK_ASSERT(inp);
258 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
260 anonport = inp->inp_lport == 0 && (nam == NULL ||
261 ((struct sockaddr_in *)nam)->sin_port == 0);
262 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
263 &inp->inp_lport, cred);
266 if (in_pcbinshash(inp) != 0) {
267 inp->inp_laddr.s_addr = INADDR_ANY;
272 inp->inp_flags |= INP_ANONPORT;
276 #if defined(INET) || defined(INET6)
278 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
279 struct ucred *cred, int wild)
281 struct inpcbinfo *pcbinfo;
282 struct inpcb *tmpinp;
283 unsigned short *lastport;
284 int count, dorandom, error;
285 u_short aux, first, last, lport;
287 struct in_addr laddr;
290 pcbinfo = inp->inp_pcbinfo;
293 * Because no actual state changes occur here, a global write lock on
294 * the pcbinfo isn't required.
296 INP_INFO_LOCK_ASSERT(pcbinfo);
297 INP_LOCK_ASSERT(inp);
299 if (inp->inp_flags & INP_HIGHPORT) {
300 first = V_ipport_hifirstauto; /* sysctl */
301 last = V_ipport_hilastauto;
302 lastport = &pcbinfo->ipi_lasthi;
303 } else if (inp->inp_flags & INP_LOWPORT) {
304 error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
307 first = V_ipport_lowfirstauto; /* 1023 */
308 last = V_ipport_lowlastauto; /* 600 */
309 lastport = &pcbinfo->ipi_lastlow;
311 first = V_ipport_firstauto; /* sysctl */
312 last = V_ipport_lastauto;
313 lastport = &pcbinfo->ipi_lastport;
316 * For UDP, use random port allocation as long as the user
317 * allows it. For TCP (and as of yet unknown) connections,
318 * use random port allocation only if the user allows it AND
319 * ipport_tick() allows it.
321 if (V_ipport_randomized &&
322 (!V_ipport_stoprandom || pcbinfo == &V_udbinfo))
327 * It makes no sense to do random port allocation if
328 * we have the only port available.
332 /* Make sure to not include UDP packets in the count. */
333 if (pcbinfo != &V_udbinfo)
334 V_ipport_tcpallocs++;
336 * Instead of having two loops further down counting up or down
337 * make sure that first is always <= last and go with only one
338 * code path implementing all logic.
347 /* Make the compiler happy. */
349 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
350 KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
358 *lastport = first + (arc4random() % (last - first));
360 count = last - first;
363 if (count-- < 0) /* completely used? */
364 return (EADDRNOTAVAIL);
366 if (*lastport < first || *lastport > last)
368 lport = htons(*lastport);
371 if ((inp->inp_vflag & INP_IPV6) != 0)
372 tmpinp = in6_pcblookup_local(pcbinfo,
373 &inp->in6p_laddr, lport, wild, cred);
375 #if defined(INET) && defined(INET6)
379 tmpinp = in_pcblookup_local(pcbinfo, laddr,
382 } while (tmpinp != NULL);
385 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
386 laddrp->s_addr = laddr.s_addr;
392 #endif /* INET || INET6 */
395 * Set up a bind operation on a PCB, performing port allocation
396 * as required, but do not actually modify the PCB. Callers can
397 * either complete the bind by setting inp_laddr/inp_lport and
398 * calling in_pcbinshash(), or they can just use the resulting
399 * port and address to authorise the sending of a once-off packet.
401 * On error, the values of *laddrp and *lportp are not changed.
404 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
405 u_short *lportp, struct ucred *cred)
407 struct socket *so = inp->inp_socket;
408 struct sockaddr_in *sin;
409 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
410 struct in_addr laddr;
412 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
416 * Because no actual state changes occur here, a global write lock on
417 * the pcbinfo isn't required.
419 INP_INFO_LOCK_ASSERT(pcbinfo);
420 INP_LOCK_ASSERT(inp);
422 if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
423 return (EADDRNOTAVAIL);
424 laddr.s_addr = *laddrp;
425 if (nam != NULL && laddr.s_addr != INADDR_ANY)
427 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
428 wild = INPLOOKUP_WILDCARD;
430 if ((error = prison_local_ip4(cred, &laddr)) != 0)
433 sin = (struct sockaddr_in *)nam;
434 if (nam->sa_len != sizeof (*sin))
438 * We should check the family, but old programs
439 * incorrectly fail to initialize it.
441 if (sin->sin_family != AF_INET)
442 return (EAFNOSUPPORT);
444 error = prison_local_ip4(cred, &sin->sin_addr);
447 if (sin->sin_port != *lportp) {
448 /* Don't allow the port to change. */
451 lport = sin->sin_port;
453 /* NB: lport is left as 0 if the port isn't being changed. */
454 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
456 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
457 * allow complete duplication of binding if
458 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
459 * and a multicast address is bound on both
460 * new and duplicated sockets.
462 if (so->so_options & SO_REUSEADDR)
463 reuseport = SO_REUSEADDR|SO_REUSEPORT;
464 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
465 sin->sin_port = 0; /* yech... */
466 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
468 * Is the address a local IP address?
469 * If INP_BINDANY is set, then the socket may be bound
470 * to any endpoint address, local or not.
472 if ((inp->inp_flags & INP_BINDANY) == 0 &&
473 ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
474 return (EADDRNOTAVAIL);
476 laddr = sin->sin_addr;
482 if (ntohs(lport) <= V_ipport_reservedhigh &&
483 ntohs(lport) >= V_ipport_reservedlow &&
484 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
487 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
488 priv_check_cred(inp->inp_cred,
489 PRIV_NETINET_REUSEPORT, 0) != 0) {
490 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
491 lport, INPLOOKUP_WILDCARD, cred);
494 * This entire block sorely needs a rewrite.
497 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
498 (so->so_type != SOCK_STREAM ||
499 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
500 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
501 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
502 (t->inp_socket->so_options &
503 SO_REUSEPORT) == 0) &&
504 (inp->inp_cred->cr_uid !=
505 t->inp_cred->cr_uid))
508 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
510 if (t && (t->inp_flags & INP_TIMEWAIT)) {
512 * XXXRW: If an incpb has had its timewait
513 * state recycled, we treat the address as
514 * being in use (for now). This is better
515 * than a panic, but not desirable.
519 (reuseport & tw->tw_so_options) == 0)
522 (reuseport & t->inp_socket->so_options) == 0) {
524 if (ntohl(sin->sin_addr.s_addr) !=
526 ntohl(t->inp_laddr.s_addr) !=
529 INP_SOCKAF(t->inp_socket))
538 error = in_pcb_lport(inp, &laddr, &lport, cred, wild);
543 *laddrp = laddr.s_addr;
549 * Connect from a socket to a specified address.
550 * Both address and port must be specified in argument sin.
551 * If don't have a local address for this socket yet,
555 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
557 u_short lport, fport;
558 in_addr_t laddr, faddr;
561 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
562 INP_WLOCK_ASSERT(inp);
564 lport = inp->inp_lport;
565 laddr = inp->inp_laddr.s_addr;
566 anonport = (lport == 0);
567 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
572 /* Do the initial binding of the local address if required. */
573 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
574 inp->inp_lport = lport;
575 inp->inp_laddr.s_addr = laddr;
576 if (in_pcbinshash(inp) != 0) {
577 inp->inp_laddr.s_addr = INADDR_ANY;
583 /* Commit the remaining changes. */
584 inp->inp_lport = lport;
585 inp->inp_laddr.s_addr = laddr;
586 inp->inp_faddr.s_addr = faddr;
587 inp->inp_fport = fport;
591 inp->inp_flags |= INP_ANONPORT;
596 * Do proper source address selection on an unbound socket in case
597 * of connect. Take jails into account as well.
600 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
605 struct sockaddr_in *sin;
609 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
612 * Bypass source address selection and use the primary jail IP
615 if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
619 bzero(&sro, sizeof(sro));
621 sin = (struct sockaddr_in *)&sro.ro_dst;
622 sin->sin_family = AF_INET;
623 sin->sin_len = sizeof(struct sockaddr_in);
624 sin->sin_addr.s_addr = faddr->s_addr;
627 * If route is known our src addr is taken from the i/f,
630 * Find out route to destination.
632 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
633 in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
636 * If we found a route, use the address corresponding to
637 * the outgoing interface.
639 * Otherwise assume faddr is reachable on a directly connected
640 * network and try to find a corresponding interface to take
641 * the source address from.
643 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
644 struct in_ifaddr *ia;
647 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
649 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0));
655 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
656 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
657 ifa_free(&ia->ia_ifa);
662 ifa_free(&ia->ia_ifa);
665 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
668 if (sa->sa_family != AF_INET)
670 sin = (struct sockaddr_in *)sa;
671 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
672 ia = (struct in_ifaddr *)ifa;
677 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
678 IF_ADDR_RUNLOCK(ifp);
681 IF_ADDR_RUNLOCK(ifp);
683 /* 3. As a last resort return the 'default' jail address. */
684 error = prison_get_ip4(cred, laddr);
689 * If the outgoing interface on the route found is not
690 * a loopback interface, use the address from that interface.
691 * In case of jails do those three steps:
692 * 1. check if the interface address belongs to the jail. If so use it.
693 * 2. check if we have any address on the outgoing interface
694 * belonging to this jail. If so use it.
695 * 3. as a last resort return the 'default' jail address.
697 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
698 struct in_ifaddr *ia;
701 /* If not jailed, use the default returned. */
702 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
703 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
704 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
709 /* 1. Check if the iface address belongs to the jail. */
710 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
711 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
712 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
713 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
718 * 2. Check if we have any address on the outgoing interface
719 * belonging to this jail.
722 ifp = sro.ro_rt->rt_ifp;
724 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
726 if (sa->sa_family != AF_INET)
728 sin = (struct sockaddr_in *)sa;
729 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
730 ia = (struct in_ifaddr *)ifa;
735 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
736 IF_ADDR_RUNLOCK(ifp);
739 IF_ADDR_RUNLOCK(ifp);
741 /* 3. As a last resort return the 'default' jail address. */
742 error = prison_get_ip4(cred, laddr);
747 * The outgoing interface is marked with 'loopback net', so a route
748 * to ourselves is here.
749 * Try to find the interface of the destination address and then
750 * take the address from there. That interface is not necessarily
751 * a loopback interface.
752 * In case of jails, check that it is an address of the jail
753 * and if we cannot find, fall back to the 'default' jail address.
755 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
756 struct sockaddr_in sain;
757 struct in_ifaddr *ia;
759 bzero(&sain, sizeof(struct sockaddr_in));
760 sain.sin_family = AF_INET;
761 sain.sin_len = sizeof(struct sockaddr_in);
762 sain.sin_addr.s_addr = faddr->s_addr;
764 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
766 ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0));
768 ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
770 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
775 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
776 ifa_free(&ia->ia_ifa);
785 ifa_free(&ia->ia_ifa);
788 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
791 if (sa->sa_family != AF_INET)
793 sin = (struct sockaddr_in *)sa;
794 if (prison_check_ip4(cred,
795 &sin->sin_addr) == 0) {
796 ia = (struct in_ifaddr *)ifa;
801 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
802 IF_ADDR_RUNLOCK(ifp);
805 IF_ADDR_RUNLOCK(ifp);
808 /* 3. As a last resort return the 'default' jail address. */
809 error = prison_get_ip4(cred, laddr);
814 if (sro.ro_rt != NULL)
820 * Set up for a connect from a socket to the specified address.
821 * On entry, *laddrp and *lportp should contain the current local
822 * address and port for the PCB; these are updated to the values
823 * that should be placed in inp_laddr and inp_lport to complete
826 * On success, *faddrp and *fportp will be set to the remote address
827 * and port. These are not updated in the error case.
829 * If the operation fails because the connection already exists,
830 * *oinpp will be set to the PCB of that connection so that the
831 * caller can decide to override it. In all other cases, *oinpp
835 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
836 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
837 struct inpcb **oinpp, struct ucred *cred)
839 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
840 struct in_ifaddr *ia;
842 struct in_addr laddr, faddr;
843 u_short lport, fport;
847 * Because a global state change doesn't actually occur here, a read
848 * lock is sufficient.
850 INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo);
851 INP_LOCK_ASSERT(inp);
855 if (nam->sa_len != sizeof (*sin))
857 if (sin->sin_family != AF_INET)
858 return (EAFNOSUPPORT);
859 if (sin->sin_port == 0)
860 return (EADDRNOTAVAIL);
861 laddr.s_addr = *laddrp;
863 faddr = sin->sin_addr;
864 fport = sin->sin_port;
866 if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
868 * If the destination address is INADDR_ANY,
869 * use the primary local address.
870 * If the supplied address is INADDR_BROADCAST,
871 * and the primary interface supports broadcast,
872 * choose the broadcast address for that interface.
874 if (faddr.s_addr == INADDR_ANY) {
877 IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
880 (error = prison_get_ip4(cred, &faddr)) != 0)
882 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
884 if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
886 faddr = satosin(&TAILQ_FIRST(
887 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
891 if (laddr.s_addr == INADDR_ANY) {
892 error = in_pcbladdr(inp, &faddr, &laddr, cred);
894 * If the destination address is multicast and an outgoing
895 * interface has been set as a multicast option, prefer the
896 * address of that interface as our source address.
898 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
899 inp->inp_moptions != NULL) {
900 struct ip_moptions *imo;
903 imo = inp->inp_moptions;
904 if (imo->imo_multicast_ifp != NULL) {
905 ifp = imo->imo_multicast_ifp;
907 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
908 if ((ia->ia_ifp == ifp) &&
910 prison_check_ip4(cred,
911 &ia->ia_addr.sin_addr) == 0))
915 error = EADDRNOTAVAIL;
917 laddr = ia->ia_addr.sin_addr;
926 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
934 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
939 *laddrp = laddr.s_addr;
941 *faddrp = faddr.s_addr;
947 in_pcbdisconnect(struct inpcb *inp)
950 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
951 INP_WLOCK_ASSERT(inp);
953 inp->inp_faddr.s_addr = INADDR_ANY;
959 * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
960 * For most protocols, this will be invoked immediately prior to calling
961 * in_pcbfree(). However, with TCP the inpcb may significantly outlive the
962 * socket, in which case in_pcbfree() is deferred.
965 in_pcbdetach(struct inpcb *inp)
968 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
970 inp->inp_socket->so_pcb = NULL;
971 inp->inp_socket = NULL;
975 * in_pcbfree_internal() frees an inpcb that has been detached from its
976 * socket, and whose reference count has reached 0. It will also remove the
977 * inpcb from any global lists it might remain on.
980 in_pcbfree_internal(struct inpcb *inp)
982 struct inpcbinfo *ipi = inp->inp_pcbinfo;
984 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
985 KASSERT(inp->inp_refcount == 0, ("%s: refcount !0", __func__));
987 INP_INFO_WLOCK_ASSERT(ipi);
988 INP_WLOCK_ASSERT(inp);
991 if (inp->inp_sp != NULL)
992 ipsec_delete_pcbpolicy(inp);
994 inp->inp_gencnt = ++ipi->ipi_gencnt;
997 if (inp->inp_vflag & INP_IPV6PROTO) {
998 ip6_freepcbopts(inp->in6p_outputopts);
999 if (inp->in6p_moptions != NULL)
1000 ip6_freemoptions(inp->in6p_moptions);
1003 if (inp->inp_options)
1004 (void)m_free(inp->inp_options);
1005 if (inp->inp_moptions != NULL)
1006 inp_freemoptions(inp->inp_moptions);
1008 crfree(inp->inp_cred);
1011 mac_inpcb_destroy(inp);
1014 uma_zfree(ipi->ipi_zone, inp);
1018 * in_pcbref() bumps the reference count on an inpcb in order to maintain
1019 * stability of an inpcb pointer despite the inpcb lock being released. This
1020 * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
1021 * but where the inpcb lock is already held.
1023 * While the inpcb will not be freed, releasing the inpcb lock means that the
1024 * connection's state may change, so the caller should be careful to
1025 * revalidate any cached state on reacquiring the lock. Drop the reference
1026 * using in_pcbrele().
1029 in_pcbref(struct inpcb *inp)
1032 INP_WLOCK_ASSERT(inp);
1034 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1036 inp->inp_refcount++;
1040 * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
1041 * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
1042 * return a flag indicating whether or not the inpcb remains valid. If it is
1043 * valid, we return with the inpcb lock held.
1046 in_pcbrele(struct inpcb *inp)
1049 struct inpcbinfo *ipi = inp->inp_pcbinfo;
1052 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1054 INP_INFO_WLOCK_ASSERT(ipi);
1055 INP_WLOCK_ASSERT(inp);
1057 inp->inp_refcount--;
1058 if (inp->inp_refcount > 0)
1060 in_pcbfree_internal(inp);
1065 * Unconditionally schedule an inpcb to be freed by decrementing its
1066 * reference count, which should occur only after the inpcb has been detached
1067 * from its socket. If another thread holds a temporary reference (acquired
1068 * using in_pcbref()) then the free is deferred until that reference is
1069 * released using in_pcbrele(), but the inpcb is still unlocked.
1072 in_pcbfree(struct inpcb *inp)
1075 struct inpcbinfo *ipi = inp->inp_pcbinfo;
1078 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL",
1081 INP_INFO_WLOCK_ASSERT(ipi);
1082 INP_WLOCK_ASSERT(inp);
1084 if (!in_pcbrele(inp))
1089 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1090 * port reservation, and preventing it from being returned by inpcb lookups.
1092 * It is used by TCP to mark an inpcb as unused and avoid future packet
1093 * delivery or event notification when a socket remains open but TCP has
1094 * closed. This might occur as a result of a shutdown()-initiated TCP close
1095 * or a RST on the wire, and allows the port binding to be reused while still
1096 * maintaining the invariant that so_pcb always points to a valid inpcb until
1099 * XXXRW: An inp_lport of 0 is used to indicate that the inpcb is not on hash
1100 * lists, but can lead to confusing netstat output, as open sockets with
1101 * closed TCP connections will no longer appear to have their bound port
1102 * number. An explicit flag would be better, as it would allow us to leave
1103 * the port number intact after the connection is dropped.
1105 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1106 * in_pcbnotifyall() and in_pcbpurgeif0()?
1109 in_pcbdrop(struct inpcb *inp)
1112 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
1113 INP_WLOCK_ASSERT(inp);
1115 inp->inp_flags |= INP_DROPPED;
1116 if (inp->inp_flags & INP_INHASHLIST) {
1117 struct inpcbport *phd = inp->inp_phd;
1119 LIST_REMOVE(inp, inp_hash);
1120 LIST_REMOVE(inp, inp_portlist);
1121 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1122 LIST_REMOVE(phd, phd_hash);
1125 inp->inp_flags &= ~INP_INHASHLIST;
1130 * Common routines to return the socket addresses associated with inpcbs.
1133 in_sockaddr(in_port_t port, struct in_addr *addr_p)
1135 struct sockaddr_in *sin;
1137 sin = malloc(sizeof *sin, M_SONAME,
1139 sin->sin_family = AF_INET;
1140 sin->sin_len = sizeof(*sin);
1141 sin->sin_addr = *addr_p;
1142 sin->sin_port = port;
1144 return (struct sockaddr *)sin;
1148 in_getsockaddr(struct socket *so, struct sockaddr **nam)
1151 struct in_addr addr;
1154 inp = sotoinpcb(so);
1155 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1158 port = inp->inp_lport;
1159 addr = inp->inp_laddr;
1162 *nam = in_sockaddr(port, &addr);
1167 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1170 struct in_addr addr;
1173 inp = sotoinpcb(so);
1174 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1177 port = inp->inp_fport;
1178 addr = inp->inp_faddr;
1181 *nam = in_sockaddr(port, &addr);
1186 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1187 struct inpcb *(*notify)(struct inpcb *, int))
1189 struct inpcb *inp, *inp_temp;
1191 INP_INFO_WLOCK(pcbinfo);
1192 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1195 if ((inp->inp_vflag & INP_IPV4) == 0) {
1200 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1201 inp->inp_socket == NULL) {
1205 if ((*notify)(inp, errno))
1208 INP_INFO_WUNLOCK(pcbinfo);
1212 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1215 struct ip_moptions *imo;
1218 INP_INFO_RLOCK(pcbinfo);
1219 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1221 imo = inp->inp_moptions;
1222 if ((inp->inp_vflag & INP_IPV4) &&
1225 * Unselect the outgoing interface if it is being
1228 if (imo->imo_multicast_ifp == ifp)
1229 imo->imo_multicast_ifp = NULL;
1232 * Drop multicast group membership if we joined
1233 * through the interface being detached.
1235 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1237 if (imo->imo_membership[i]->inm_ifp == ifp) {
1238 in_delmulti(imo->imo_membership[i]);
1240 } else if (gap != 0)
1241 imo->imo_membership[i - gap] =
1242 imo->imo_membership[i];
1244 imo->imo_num_memberships -= gap;
1248 INP_INFO_RUNLOCK(pcbinfo);
1252 * Lookup a PCB based on the local address and port.
1254 #define INP_LOOKUP_MAPPED_PCB_COST 3
1256 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1257 u_short lport, int wild_okay, struct ucred *cred)
1261 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1267 INP_INFO_LOCK_ASSERT(pcbinfo);
1270 struct inpcbhead *head;
1272 * Look for an unconnected (wildcard foreign addr) PCB that
1273 * matches the local address and port we're looking for.
1275 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1276 0, pcbinfo->ipi_hashmask)];
1277 LIST_FOREACH(inp, head, inp_hash) {
1279 /* XXX inp locking */
1280 if ((inp->inp_vflag & INP_IPV4) == 0)
1283 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1284 inp->inp_laddr.s_addr == laddr.s_addr &&
1285 inp->inp_lport == lport) {
1290 prison_equal_ip4(cred->cr_prison,
1291 inp->inp_cred->cr_prison))
1300 struct inpcbporthead *porthash;
1301 struct inpcbport *phd;
1302 struct inpcb *match = NULL;
1304 * Best fit PCB lookup.
1306 * First see if this local port is in use by looking on the
1309 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1310 pcbinfo->ipi_porthashmask)];
1311 LIST_FOREACH(phd, porthash, phd_hash) {
1312 if (phd->phd_port == lport)
1317 * Port is in use by one or more PCBs. Look for best
1320 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1323 !prison_equal_ip4(inp->inp_cred->cr_prison,
1327 /* XXX inp locking */
1328 if ((inp->inp_vflag & INP_IPV4) == 0)
1331 * We never select the PCB that has
1332 * INP_IPV6 flag and is bound to :: if
1333 * we have another PCB which is bound
1334 * to 0.0.0.0. If a PCB has the
1335 * INP_IPV6 flag, then we set its cost
1336 * higher than IPv4 only PCBs.
1338 * Note that the case only happens
1339 * when a socket is bound to ::, under
1340 * the condition that the use of the
1341 * mapped address is allowed.
1343 if ((inp->inp_vflag & INP_IPV6) != 0)
1344 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1346 if (inp->inp_faddr.s_addr != INADDR_ANY)
1348 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1349 if (laddr.s_addr == INADDR_ANY)
1351 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1354 if (laddr.s_addr != INADDR_ANY)
1357 if (wildcard < matchwild) {
1359 matchwild = wildcard;
1368 #undef INP_LOOKUP_MAPPED_PCB_COST
1371 * Lookup PCB in hash list.
1374 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1375 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1378 struct inpcbhead *head;
1379 struct inpcb *inp, *tmpinp;
1380 u_short fport = fport_arg, lport = lport_arg;
1382 INP_INFO_LOCK_ASSERT(pcbinfo);
1385 * First look for an exact match.
1388 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1389 pcbinfo->ipi_hashmask)];
1390 LIST_FOREACH(inp, head, inp_hash) {
1392 /* XXX inp locking */
1393 if ((inp->inp_vflag & INP_IPV4) == 0)
1396 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1397 inp->inp_laddr.s_addr == laddr.s_addr &&
1398 inp->inp_fport == fport &&
1399 inp->inp_lport == lport) {
1401 * XXX We should be able to directly return
1402 * the inp here, without any checks.
1403 * Well unless both bound with SO_REUSEPORT?
1405 if (prison_flag(inp->inp_cred, PR_IP4))
1415 * Then look for a wildcard match, if requested.
1417 if (wildcard == INPLOOKUP_WILDCARD) {
1418 struct inpcb *local_wild = NULL, *local_exact = NULL;
1420 struct inpcb *local_wild_mapped = NULL;
1422 struct inpcb *jail_wild = NULL;
1426 * Order of socket selection - we always prefer jails.
1427 * 1. jailed, non-wild.
1429 * 3. non-jailed, non-wild.
1430 * 4. non-jailed, wild.
1433 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1434 0, pcbinfo->ipi_hashmask)];
1435 LIST_FOREACH(inp, head, inp_hash) {
1437 /* XXX inp locking */
1438 if ((inp->inp_vflag & INP_IPV4) == 0)
1441 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1442 inp->inp_lport != lport)
1445 /* XXX inp locking */
1446 if (ifp && ifp->if_type == IFT_FAITH &&
1447 (inp->inp_flags & INP_FAITH) == 0)
1450 injail = prison_flag(inp->inp_cred, PR_IP4);
1452 if (prison_check_ip4(inp->inp_cred,
1456 if (local_exact != NULL)
1460 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1465 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1467 /* XXX inp locking, NULL check */
1468 if (inp->inp_vflag & INP_IPV6PROTO)
1469 local_wild_mapped = inp;
1477 } /* LIST_FOREACH */
1478 if (jail_wild != NULL)
1480 if (local_exact != NULL)
1481 return (local_exact);
1482 if (local_wild != NULL)
1483 return (local_wild);
1485 if (local_wild_mapped != NULL)
1486 return (local_wild_mapped);
1487 #endif /* defined(INET6) */
1488 } /* if (wildcard == INPLOOKUP_WILDCARD) */
1494 * Insert PCB onto various hash lists.
1497 in_pcbinshash(struct inpcb *inp)
1499 struct inpcbhead *pcbhash;
1500 struct inpcbporthead *pcbporthash;
1501 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1502 struct inpcbport *phd;
1503 u_int32_t hashkey_faddr;
1505 INP_INFO_WLOCK_ASSERT(pcbinfo);
1506 INP_WLOCK_ASSERT(inp);
1507 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1508 ("in_pcbinshash: INP_INHASHLIST"));
1511 if (inp->inp_vflag & INP_IPV6)
1512 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1515 hashkey_faddr = inp->inp_faddr.s_addr;
1517 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1518 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1520 pcbporthash = &pcbinfo->ipi_porthashbase[
1521 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1524 * Go through port list and look for a head for this lport.
1526 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1527 if (phd->phd_port == inp->inp_lport)
1531 * If none exists, malloc one and tack it on.
1534 phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1536 return (ENOBUFS); /* XXX */
1538 phd->phd_port = inp->inp_lport;
1539 LIST_INIT(&phd->phd_pcblist);
1540 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1543 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1544 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1545 inp->inp_flags |= INP_INHASHLIST;
1550 * Move PCB to the proper hash bucket when { faddr, fport } have been
1551 * changed. NOTE: This does not handle the case of the lport changing (the
1552 * hashed port list would have to be updated as well), so the lport must
1553 * not change after in_pcbinshash() has been called.
1556 in_pcbrehash(struct inpcb *inp)
1558 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1559 struct inpcbhead *head;
1560 u_int32_t hashkey_faddr;
1562 INP_INFO_WLOCK_ASSERT(pcbinfo);
1563 INP_WLOCK_ASSERT(inp);
1564 KASSERT(inp->inp_flags & INP_INHASHLIST,
1565 ("in_pcbrehash: !INP_INHASHLIST"));
1568 if (inp->inp_vflag & INP_IPV6)
1569 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1572 hashkey_faddr = inp->inp_faddr.s_addr;
1574 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1575 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1577 LIST_REMOVE(inp, inp_hash);
1578 LIST_INSERT_HEAD(head, inp, inp_hash);
1582 * Remove PCB from various lists.
1585 in_pcbremlists(struct inpcb *inp)
1587 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1589 INP_INFO_WLOCK_ASSERT(pcbinfo);
1590 INP_WLOCK_ASSERT(inp);
1592 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1593 if (inp->inp_flags & INP_INHASHLIST) {
1594 struct inpcbport *phd = inp->inp_phd;
1596 LIST_REMOVE(inp, inp_hash);
1597 LIST_REMOVE(inp, inp_portlist);
1598 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1599 LIST_REMOVE(phd, phd_hash);
1602 inp->inp_flags &= ~INP_INHASHLIST;
1604 LIST_REMOVE(inp, inp_list);
1605 pcbinfo->ipi_count--;
1609 * A set label operation has occurred at the socket layer, propagate the
1610 * label change into the in_pcb for the socket.
1613 in_pcbsosetlabel(struct socket *so)
1618 inp = sotoinpcb(so);
1619 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1623 mac_inpcb_sosetlabel(so, inp);
1630 * ipport_tick runs once per second, determining if random port allocation
1631 * should be continued. If more than ipport_randomcps ports have been
1632 * allocated in the last second, then we return to sequential port
1633 * allocation. We return to random allocation only once we drop below
1634 * ipport_randomcps for at least ipport_randomtime seconds.
1637 ipport_tick(void *xtp)
1639 VNET_ITERATOR_DECL(vnet_iter);
1641 VNET_LIST_RLOCK_NOSLEEP();
1642 VNET_FOREACH(vnet_iter) {
1643 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */
1644 if (V_ipport_tcpallocs <=
1645 V_ipport_tcplastcount + V_ipport_randomcps) {
1646 if (V_ipport_stoprandom > 0)
1647 V_ipport_stoprandom--;
1649 V_ipport_stoprandom = V_ipport_randomtime;
1650 V_ipport_tcplastcount = V_ipport_tcpallocs;
1653 VNET_LIST_RUNLOCK_NOSLEEP();
1654 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1658 inp_wlock(struct inpcb *inp)
1665 inp_wunlock(struct inpcb *inp)
1672 inp_rlock(struct inpcb *inp)
1679 inp_runlock(struct inpcb *inp)
1687 inp_lock_assert(struct inpcb *inp)
1690 INP_WLOCK_ASSERT(inp);
1694 inp_unlock_assert(struct inpcb *inp)
1697 INP_UNLOCK_ASSERT(inp);
1702 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
1706 INP_INFO_RLOCK(&V_tcbinfo);
1707 LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
1712 INP_INFO_RUNLOCK(&V_tcbinfo);
1716 inp_inpcbtosocket(struct inpcb *inp)
1719 INP_WLOCK_ASSERT(inp);
1720 return (inp->inp_socket);
1724 inp_inpcbtotcpcb(struct inpcb *inp)
1727 INP_WLOCK_ASSERT(inp);
1728 return ((struct tcpcb *)inp->inp_ppcb);
1732 inp_ip_tos_get(const struct inpcb *inp)
1735 return (inp->inp_ip_tos);
1739 inp_ip_tos_set(struct inpcb *inp, int val)
1742 inp->inp_ip_tos = val;
1746 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
1747 uint32_t *faddr, uint16_t *fp)
1750 INP_LOCK_ASSERT(inp);
1751 *laddr = inp->inp_laddr.s_addr;
1752 *faddr = inp->inp_faddr.s_addr;
1753 *lp = inp->inp_lport;
1754 *fp = inp->inp_fport;
1758 so_sotoinpcb(struct socket *so)
1761 return (sotoinpcb(so));
1765 so_sototcpcb(struct socket *so)
1768 return (sototcpcb(so));
1773 db_print_indent(int indent)
1777 for (i = 0; i < indent; i++)
1782 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1784 char faddr_str[48], laddr_str[48];
1786 db_print_indent(indent);
1787 db_printf("%s at %p\n", name, inc);
1792 if (inc->inc_flags & INC_ISIPV6) {
1794 ip6_sprintf(laddr_str, &inc->inc6_laddr);
1795 ip6_sprintf(faddr_str, &inc->inc6_faddr);
1799 inet_ntoa_r(inc->inc_laddr, laddr_str);
1800 inet_ntoa_r(inc->inc_faddr, faddr_str);
1804 db_print_indent(indent);
1805 db_printf("inc_laddr %s inc_lport %u\n", laddr_str,
1806 ntohs(inc->inc_lport));
1807 db_print_indent(indent);
1808 db_printf("inc_faddr %s inc_fport %u\n", faddr_str,
1809 ntohs(inc->inc_fport));
1813 db_print_inpflags(int inp_flags)
1818 if (inp_flags & INP_RECVOPTS) {
1819 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1822 if (inp_flags & INP_RECVRETOPTS) {
1823 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1826 if (inp_flags & INP_RECVDSTADDR) {
1827 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1830 if (inp_flags & INP_HDRINCL) {
1831 db_printf("%sINP_HDRINCL", comma ? ", " : "");
1834 if (inp_flags & INP_HIGHPORT) {
1835 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1838 if (inp_flags & INP_LOWPORT) {
1839 db_printf("%sINP_LOWPORT", comma ? ", " : "");
1842 if (inp_flags & INP_ANONPORT) {
1843 db_printf("%sINP_ANONPORT", comma ? ", " : "");
1846 if (inp_flags & INP_RECVIF) {
1847 db_printf("%sINP_RECVIF", comma ? ", " : "");
1850 if (inp_flags & INP_MTUDISC) {
1851 db_printf("%sINP_MTUDISC", comma ? ", " : "");
1854 if (inp_flags & INP_FAITH) {
1855 db_printf("%sINP_FAITH", comma ? ", " : "");
1858 if (inp_flags & INP_RECVTTL) {
1859 db_printf("%sINP_RECVTTL", comma ? ", " : "");
1862 if (inp_flags & INP_DONTFRAG) {
1863 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1866 if (inp_flags & INP_RECVTOS) {
1867 db_printf("%sINP_RECVTOS", comma ? ", " : "");
1870 if (inp_flags & IN6P_IPV6_V6ONLY) {
1871 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1874 if (inp_flags & IN6P_PKTINFO) {
1875 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1878 if (inp_flags & IN6P_HOPLIMIT) {
1879 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1882 if (inp_flags & IN6P_HOPOPTS) {
1883 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1886 if (inp_flags & IN6P_DSTOPTS) {
1887 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1890 if (inp_flags & IN6P_RTHDR) {
1891 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
1894 if (inp_flags & IN6P_RTHDRDSTOPTS) {
1895 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
1898 if (inp_flags & IN6P_TCLASS) {
1899 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
1902 if (inp_flags & IN6P_AUTOFLOWLABEL) {
1903 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
1906 if (inp_flags & INP_TIMEWAIT) {
1907 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
1910 if (inp_flags & INP_ONESBCAST) {
1911 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
1914 if (inp_flags & INP_DROPPED) {
1915 db_printf("%sINP_DROPPED", comma ? ", " : "");
1918 if (inp_flags & INP_SOCKREF) {
1919 db_printf("%sINP_SOCKREF", comma ? ", " : "");
1922 if (inp_flags & IN6P_RFC2292) {
1923 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
1926 if (inp_flags & IN6P_MTU) {
1927 db_printf("IN6P_MTU%s", comma ? ", " : "");
1933 db_print_inpvflag(u_char inp_vflag)
1938 if (inp_vflag & INP_IPV4) {
1939 db_printf("%sINP_IPV4", comma ? ", " : "");
1942 if (inp_vflag & INP_IPV6) {
1943 db_printf("%sINP_IPV6", comma ? ", " : "");
1946 if (inp_vflag & INP_IPV6PROTO) {
1947 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
1953 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
1956 db_print_indent(indent);
1957 db_printf("%s at %p\n", name, inp);
1961 db_print_indent(indent);
1962 db_printf("inp_flow: 0x%x\n", inp->inp_flow);
1964 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
1966 db_print_indent(indent);
1967 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n",
1968 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
1970 db_print_indent(indent);
1971 db_printf("inp_label: %p inp_flags: 0x%x (",
1972 inp->inp_label, inp->inp_flags);
1973 db_print_inpflags(inp->inp_flags);
1976 db_print_indent(indent);
1977 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp,
1979 db_print_inpvflag(inp->inp_vflag);
1982 db_print_indent(indent);
1983 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n",
1984 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
1986 db_print_indent(indent);
1988 if (inp->inp_vflag & INP_IPV6) {
1989 db_printf("in6p_options: %p in6p_outputopts: %p "
1990 "in6p_moptions: %p\n", inp->in6p_options,
1991 inp->in6p_outputopts, inp->in6p_moptions);
1992 db_printf("in6p_icmp6filt: %p in6p_cksum %d "
1993 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
1998 db_printf("inp_ip_tos: %d inp_ip_options: %p "
1999 "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2000 inp->inp_options, inp->inp_moptions);
2003 db_print_indent(indent);
2004 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd,
2005 (uintmax_t)inp->inp_gencnt);
2008 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2013 db_printf("usage: show inpcb <addr>\n");
2016 inp = (struct inpcb *)addr;
2018 db_print_inpcb(inp, "inpcb", 0);