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
5 * Copyright (c) 2010-2011 Juniper Networks, Inc.
8 * Portions of this software were developed by Robert N. M. Watson under
9 * contract to Juniper Networks, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 #include "opt_ipsec.h"
44 #include "opt_inet6.h"
45 #include "opt_pcbgroup.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
51 #include <sys/callout.h>
52 #include <sys/domain.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
58 #include <sys/refcount.h>
60 #include <sys/kernel.h>
61 #include <sys/sysctl.h>
70 #include <net/if_types.h>
71 #include <net/route.h>
74 #if defined(INET) || defined(INET6)
75 #include <netinet/in.h>
76 #include <netinet/in_pcb.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/tcp_var.h>
79 #include <netinet/udp.h>
80 #include <netinet/udp_var.h>
83 #include <netinet/in_var.h>
86 #include <netinet/ip6.h>
87 #include <netinet6/in6_pcb.h>
88 #include <netinet6/in6_var.h>
89 #include <netinet6/ip6_var.h>
94 #include <netipsec/ipsec.h>
95 #include <netipsec/key.h>
98 #include <security/mac/mac_framework.h>
100 static struct callout ipport_tick_callout;
103 * These configure the range of local port addresses assigned to
104 * "unspecified" outgoing connections/packets/whatever.
106 VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */
107 VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */
108 VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */
109 VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */
110 VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */
111 VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */
114 * Reserved ports accessible only to root. There are significant
115 * security considerations that must be accounted for when changing these,
116 * but the security benefits can be great. Please be careful.
118 VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */
119 VNET_DEFINE(int, ipport_reservedlow);
121 /* Variables dealing with random ephemeral port allocation. */
122 VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */
123 VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */
124 VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */
125 VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */
126 VNET_DEFINE(int, ipport_tcpallocs);
127 static VNET_DEFINE(int, ipport_tcplastcount);
129 #define V_ipport_tcplastcount VNET(ipport_tcplastcount)
131 static void in_pcbremlists(struct inpcb *inp);
133 static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
134 struct in_addr faddr, u_int fport_arg,
135 struct in_addr laddr, u_int lport_arg,
136 int lookupflags, struct ifnet *ifp);
138 #define RANGECHK(var, min, max) \
139 if ((var) < (min)) { (var) = (min); } \
140 else if ((var) > (max)) { (var) = (max); }
143 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
147 error = sysctl_handle_int(oidp, arg1, arg2, req);
149 RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
150 RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
151 RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
152 RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
153 RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
154 RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
161 static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
164 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
165 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0,
166 &sysctl_net_ipport_check, "I", "");
167 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
168 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0,
169 &sysctl_net_ipport_check, "I", "");
170 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first,
171 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0,
172 &sysctl_net_ipport_check, "I", "");
173 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last,
174 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0,
175 &sysctl_net_ipport_check, "I", "");
176 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
177 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0,
178 &sysctl_net_ipport_check, "I", "");
179 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
180 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0,
181 &sysctl_net_ipport_check, "I", "");
182 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
183 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, "");
184 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
185 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
186 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
187 &VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
188 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
189 &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
190 "allocations before switching to a sequental one");
191 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
192 &VNET_NAME(ipport_randomtime), 0,
193 "Minimum time to keep sequental port "
194 "allocation before switching to a random one");
198 * in_pcb.c: manage the Protocol Control Blocks.
200 * NOTE: It is assumed that most of these functions will be called with
201 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
202 * functions often modify hash chains or addresses in pcbs.
206 * Initialize an inpcbinfo -- we should be able to reduce the number of
210 in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
211 struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
212 char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
213 uint32_t inpcbzone_flags, u_int hashfields)
216 INP_INFO_LOCK_INIT(pcbinfo, name);
217 INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */
219 pcbinfo->ipi_vnet = curvnet;
221 pcbinfo->ipi_listhead = listhead;
222 LIST_INIT(pcbinfo->ipi_listhead);
223 pcbinfo->ipi_count = 0;
224 pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
225 &pcbinfo->ipi_hashmask);
226 pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
227 &pcbinfo->ipi_porthashmask);
229 in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
231 pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
232 NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
234 uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
235 uma_zone_set_warning(pcbinfo->ipi_zone,
236 "kern.ipc.maxsockets limit reached");
240 * Destroy an inpcbinfo.
243 in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
246 KASSERT(pcbinfo->ipi_count == 0,
247 ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
249 hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
250 hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
251 pcbinfo->ipi_porthashmask);
253 in_pcbgroup_destroy(pcbinfo);
255 uma_zdestroy(pcbinfo->ipi_zone);
256 INP_HASH_LOCK_DESTROY(pcbinfo);
257 INP_INFO_LOCK_DESTROY(pcbinfo);
261 * Allocate a PCB and associate it with the socket.
262 * On success return with the PCB locked.
265 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
270 INP_INFO_WLOCK_ASSERT(pcbinfo);
272 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
275 bzero(inp, inp_zero_size);
276 inp->inp_pcbinfo = pcbinfo;
277 inp->inp_socket = so;
278 inp->inp_cred = crhold(so->so_cred);
279 inp->inp_inc.inc_fibnum = so->so_fibnum;
281 error = mac_inpcb_init(inp, M_NOWAIT);
284 mac_inpcb_create(so, inp);
287 error = ipsec_init_policy(so, &inp->inp_sp);
290 mac_inpcb_destroy(inp);
296 if (INP_SOCKAF(so) == AF_INET6) {
297 inp->inp_vflag |= INP_IPV6PROTO;
299 inp->inp_flags |= IN6P_IPV6_V6ONLY;
302 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
303 pcbinfo->ipi_count++;
304 so->so_pcb = (caddr_t)inp;
306 if (V_ip6_auto_flowlabel)
307 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
310 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
311 refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */
312 #if defined(IPSEC) || defined(MAC)
315 crfree(inp->inp_cred);
316 uma_zfree(pcbinfo->ipi_zone, inp);
324 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
328 INP_WLOCK_ASSERT(inp);
329 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
331 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
333 anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
334 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
335 &inp->inp_lport, cred);
338 if (in_pcbinshash(inp) != 0) {
339 inp->inp_laddr.s_addr = INADDR_ANY;
344 inp->inp_flags |= INP_ANONPORT;
349 #if defined(INET) || defined(INET6)
351 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
352 struct ucred *cred, int lookupflags)
354 struct inpcbinfo *pcbinfo;
355 struct inpcb *tmpinp;
356 unsigned short *lastport;
357 int count, dorandom, error;
358 u_short aux, first, last, lport;
360 struct in_addr laddr;
363 pcbinfo = inp->inp_pcbinfo;
366 * Because no actual state changes occur here, a global write lock on
367 * the pcbinfo isn't required.
369 INP_LOCK_ASSERT(inp);
370 INP_HASH_LOCK_ASSERT(pcbinfo);
372 if (inp->inp_flags & INP_HIGHPORT) {
373 first = V_ipport_hifirstauto; /* sysctl */
374 last = V_ipport_hilastauto;
375 lastport = &pcbinfo->ipi_lasthi;
376 } else if (inp->inp_flags & INP_LOWPORT) {
377 error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
380 first = V_ipport_lowfirstauto; /* 1023 */
381 last = V_ipport_lowlastauto; /* 600 */
382 lastport = &pcbinfo->ipi_lastlow;
384 first = V_ipport_firstauto; /* sysctl */
385 last = V_ipport_lastauto;
386 lastport = &pcbinfo->ipi_lastport;
389 * For UDP(-Lite), use random port allocation as long as the user
390 * allows it. For TCP (and as of yet unknown) connections,
391 * use random port allocation only if the user allows it AND
392 * ipport_tick() allows it.
394 if (V_ipport_randomized &&
395 (!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
396 pcbinfo == &V_ulitecbinfo))
401 * It makes no sense to do random port allocation if
402 * we have the only port available.
406 /* Make sure to not include UDP(-Lite) packets in the count. */
407 if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
408 V_ipport_tcpallocs++;
410 * Instead of having two loops further down counting up or down
411 * make sure that first is always <= last and go with only one
412 * code path implementing all logic.
421 /* Make the compiler happy. */
423 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
424 KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
429 tmpinp = NULL; /* Make compiler happy. */
433 *lastport = first + (arc4random() % (last - first));
435 count = last - first;
438 if (count-- < 0) /* completely used? */
439 return (EADDRNOTAVAIL);
441 if (*lastport < first || *lastport > last)
443 lport = htons(*lastport);
446 if ((inp->inp_vflag & INP_IPV6) != 0)
447 tmpinp = in6_pcblookup_local(pcbinfo,
448 &inp->in6p_laddr, lport, lookupflags, cred);
450 #if defined(INET) && defined(INET6)
454 tmpinp = in_pcblookup_local(pcbinfo, laddr,
455 lport, lookupflags, cred);
457 } while (tmpinp != NULL);
460 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
461 laddrp->s_addr = laddr.s_addr;
469 * Return cached socket options.
472 inp_so_options(const struct inpcb *inp)
478 if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
479 so_options |= SO_REUSEPORT;
480 if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
481 so_options |= SO_REUSEADDR;
484 #endif /* INET || INET6 */
488 * Set up a bind operation on a PCB, performing port allocation
489 * as required, but do not actually modify the PCB. Callers can
490 * either complete the bind by setting inp_laddr/inp_lport and
491 * calling in_pcbinshash(), or they can just use the resulting
492 * port and address to authorise the sending of a once-off packet.
494 * On error, the values of *laddrp and *lportp are not changed.
497 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
498 u_short *lportp, struct ucred *cred)
500 struct socket *so = inp->inp_socket;
501 struct sockaddr_in *sin;
502 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
503 struct in_addr laddr;
505 int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
509 * No state changes, so read locks are sufficient here.
511 INP_LOCK_ASSERT(inp);
512 INP_HASH_LOCK_ASSERT(pcbinfo);
514 if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
515 return (EADDRNOTAVAIL);
516 laddr.s_addr = *laddrp;
517 if (nam != NULL && laddr.s_addr != INADDR_ANY)
519 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
520 lookupflags = INPLOOKUP_WILDCARD;
522 if ((error = prison_local_ip4(cred, &laddr)) != 0)
525 sin = (struct sockaddr_in *)nam;
526 if (nam->sa_len != sizeof (*sin))
530 * We should check the family, but old programs
531 * incorrectly fail to initialize it.
533 if (sin->sin_family != AF_INET)
534 return (EAFNOSUPPORT);
536 error = prison_local_ip4(cred, &sin->sin_addr);
539 if (sin->sin_port != *lportp) {
540 /* Don't allow the port to change. */
543 lport = sin->sin_port;
545 /* NB: lport is left as 0 if the port isn't being changed. */
546 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
548 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
549 * allow complete duplication of binding if
550 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
551 * and a multicast address is bound on both
552 * new and duplicated sockets.
554 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
555 reuseport = SO_REUSEADDR|SO_REUSEPORT;
556 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
557 sin->sin_port = 0; /* yech... */
558 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
560 * Is the address a local IP address?
561 * If INP_BINDANY is set, then the socket may be bound
562 * to any endpoint address, local or not.
564 if ((inp->inp_flags & INP_BINDANY) == 0 &&
565 ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
566 return (EADDRNOTAVAIL);
568 laddr = sin->sin_addr;
574 if (ntohs(lport) <= V_ipport_reservedhigh &&
575 ntohs(lport) >= V_ipport_reservedlow &&
576 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
579 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
580 priv_check_cred(inp->inp_cred,
581 PRIV_NETINET_REUSEPORT, 0) != 0) {
582 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
583 lport, INPLOOKUP_WILDCARD, cred);
586 * This entire block sorely needs a rewrite.
589 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
590 (so->so_type != SOCK_STREAM ||
591 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
592 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
593 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
594 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
595 (inp->inp_cred->cr_uid !=
596 t->inp_cred->cr_uid))
599 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
600 lport, lookupflags, cred);
601 if (t && (t->inp_flags & INP_TIMEWAIT)) {
603 * XXXRW: If an incpb has had its timewait
604 * state recycled, we treat the address as
605 * being in use (for now). This is better
606 * than a panic, but not desirable.
610 (reuseport & tw->tw_so_options) == 0)
612 } else if (t && (reuseport & inp_so_options(t)) == 0) {
614 if (ntohl(sin->sin_addr.s_addr) !=
616 ntohl(t->inp_laddr.s_addr) !=
618 (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
619 (t->inp_vflag & INP_IPV6PROTO) == 0)
628 error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
633 *laddrp = laddr.s_addr;
639 * Connect from a socket to a specified address.
640 * Both address and port must be specified in argument sin.
641 * If don't have a local address for this socket yet,
645 in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
646 struct ucred *cred, struct mbuf *m)
648 u_short lport, fport;
649 in_addr_t laddr, faddr;
652 INP_WLOCK_ASSERT(inp);
653 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
655 lport = inp->inp_lport;
656 laddr = inp->inp_laddr.s_addr;
657 anonport = (lport == 0);
658 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
663 /* Do the initial binding of the local address if required. */
664 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
665 inp->inp_lport = lport;
666 inp->inp_laddr.s_addr = laddr;
667 if (in_pcbinshash(inp) != 0) {
668 inp->inp_laddr.s_addr = INADDR_ANY;
674 /* Commit the remaining changes. */
675 inp->inp_lport = lport;
676 inp->inp_laddr.s_addr = laddr;
677 inp->inp_faddr.s_addr = faddr;
678 inp->inp_fport = fport;
679 in_pcbrehash_mbuf(inp, m);
682 inp->inp_flags |= INP_ANONPORT;
687 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
690 return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
694 * Do proper source address selection on an unbound socket in case
695 * of connect. Take jails into account as well.
698 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
703 struct sockaddr_in *sin;
707 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
710 * Bypass source address selection and use the primary jail IP
713 if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
717 bzero(&sro, sizeof(sro));
719 sin = (struct sockaddr_in *)&sro.ro_dst;
720 sin->sin_family = AF_INET;
721 sin->sin_len = sizeof(struct sockaddr_in);
722 sin->sin_addr.s_addr = faddr->s_addr;
725 * If route is known our src addr is taken from the i/f,
728 * Find out route to destination.
730 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
731 in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
734 * If we found a route, use the address corresponding to
735 * the outgoing interface.
737 * Otherwise assume faddr is reachable on a directly connected
738 * network and try to find a corresponding interface to take
739 * the source address from.
741 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
742 struct in_ifaddr *ia;
745 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
747 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0));
753 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
754 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
755 ifa_free(&ia->ia_ifa);
760 ifa_free(&ia->ia_ifa);
763 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
766 if (sa->sa_family != AF_INET)
768 sin = (struct sockaddr_in *)sa;
769 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
770 ia = (struct in_ifaddr *)ifa;
775 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
776 IF_ADDR_RUNLOCK(ifp);
779 IF_ADDR_RUNLOCK(ifp);
781 /* 3. As a last resort return the 'default' jail address. */
782 error = prison_get_ip4(cred, laddr);
787 * If the outgoing interface on the route found is not
788 * a loopback interface, use the address from that interface.
789 * In case of jails do those three steps:
790 * 1. check if the interface address belongs to the jail. If so use it.
791 * 2. check if we have any address on the outgoing interface
792 * belonging to this jail. If so use it.
793 * 3. as a last resort return the 'default' jail address.
795 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
796 struct in_ifaddr *ia;
799 /* If not jailed, use the default returned. */
800 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
801 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
802 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
807 /* 1. Check if the iface address belongs to the jail. */
808 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
809 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
810 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
811 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
816 * 2. Check if we have any address on the outgoing interface
817 * belonging to this jail.
820 ifp = sro.ro_rt->rt_ifp;
822 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
824 if (sa->sa_family != AF_INET)
826 sin = (struct sockaddr_in *)sa;
827 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
828 ia = (struct in_ifaddr *)ifa;
833 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
834 IF_ADDR_RUNLOCK(ifp);
837 IF_ADDR_RUNLOCK(ifp);
839 /* 3. As a last resort return the 'default' jail address. */
840 error = prison_get_ip4(cred, laddr);
845 * The outgoing interface is marked with 'loopback net', so a route
846 * to ourselves is here.
847 * Try to find the interface of the destination address and then
848 * take the address from there. That interface is not necessarily
849 * a loopback interface.
850 * In case of jails, check that it is an address of the jail
851 * and if we cannot find, fall back to the 'default' jail address.
853 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
854 struct sockaddr_in sain;
855 struct in_ifaddr *ia;
857 bzero(&sain, sizeof(struct sockaddr_in));
858 sain.sin_family = AF_INET;
859 sain.sin_len = sizeof(struct sockaddr_in);
860 sain.sin_addr.s_addr = faddr->s_addr;
862 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
864 ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0));
866 ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
868 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
873 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
874 ifa_free(&ia->ia_ifa);
883 ifa_free(&ia->ia_ifa);
886 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
889 if (sa->sa_family != AF_INET)
891 sin = (struct sockaddr_in *)sa;
892 if (prison_check_ip4(cred,
893 &sin->sin_addr) == 0) {
894 ia = (struct in_ifaddr *)ifa;
899 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
900 IF_ADDR_RUNLOCK(ifp);
903 IF_ADDR_RUNLOCK(ifp);
906 /* 3. As a last resort return the 'default' jail address. */
907 error = prison_get_ip4(cred, laddr);
912 if (sro.ro_rt != NULL)
918 * Set up for a connect from a socket to the specified address.
919 * On entry, *laddrp and *lportp should contain the current local
920 * address and port for the PCB; these are updated to the values
921 * that should be placed in inp_laddr and inp_lport to complete
924 * On success, *faddrp and *fportp will be set to the remote address
925 * and port. These are not updated in the error case.
927 * If the operation fails because the connection already exists,
928 * *oinpp will be set to the PCB of that connection so that the
929 * caller can decide to override it. In all other cases, *oinpp
933 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
934 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
935 struct inpcb **oinpp, struct ucred *cred)
937 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
938 struct in_ifaddr *ia;
940 struct in_addr laddr, faddr;
941 u_short lport, fport;
945 * Because a global state change doesn't actually occur here, a read
946 * lock is sufficient.
948 INP_LOCK_ASSERT(inp);
949 INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
953 if (nam->sa_len != sizeof (*sin))
955 if (sin->sin_family != AF_INET)
956 return (EAFNOSUPPORT);
957 if (sin->sin_port == 0)
958 return (EADDRNOTAVAIL);
959 laddr.s_addr = *laddrp;
961 faddr = sin->sin_addr;
962 fport = sin->sin_port;
964 if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
966 * If the destination address is INADDR_ANY,
967 * use the primary local address.
968 * If the supplied address is INADDR_BROADCAST,
969 * and the primary interface supports broadcast,
970 * choose the broadcast address for that interface.
972 if (faddr.s_addr == INADDR_ANY) {
975 IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
978 (error = prison_get_ip4(cred, &faddr)) != 0)
980 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
982 if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
984 faddr = satosin(&TAILQ_FIRST(
985 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
989 if (laddr.s_addr == INADDR_ANY) {
990 error = in_pcbladdr(inp, &faddr, &laddr, cred);
992 * If the destination address is multicast and an outgoing
993 * interface has been set as a multicast option, prefer the
994 * address of that interface as our source address.
996 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
997 inp->inp_moptions != NULL) {
998 struct ip_moptions *imo;
1001 imo = inp->inp_moptions;
1002 if (imo->imo_multicast_ifp != NULL) {
1003 ifp = imo->imo_multicast_ifp;
1005 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1006 if ((ia->ia_ifp == ifp) &&
1008 prison_check_ip4(cred,
1009 &ia->ia_addr.sin_addr) == 0))
1013 error = EADDRNOTAVAIL;
1015 laddr = ia->ia_addr.sin_addr;
1018 IN_IFADDR_RUNLOCK();
1024 oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1025 laddr, lport, 0, NULL);
1029 return (EADDRINUSE);
1032 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1037 *laddrp = laddr.s_addr;
1039 *faddrp = faddr.s_addr;
1045 in_pcbdisconnect(struct inpcb *inp)
1048 INP_WLOCK_ASSERT(inp);
1049 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1051 inp->inp_faddr.s_addr = INADDR_ANY;
1058 * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1059 * For most protocols, this will be invoked immediately prior to calling
1060 * in_pcbfree(). However, with TCP the inpcb may significantly outlive the
1061 * socket, in which case in_pcbfree() is deferred.
1064 in_pcbdetach(struct inpcb *inp)
1067 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1069 inp->inp_socket->so_pcb = NULL;
1070 inp->inp_socket = NULL;
1074 * in_pcbref() bumps the reference count on an inpcb in order to maintain
1075 * stability of an inpcb pointer despite the inpcb lock being released. This
1076 * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
1077 * but where the inpcb lock may already held, or when acquiring a reference
1080 * in_pcbref() should be used only to provide brief memory stability, and
1081 * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
1082 * garbage collect the inpcb if it has been in_pcbfree()'d from another
1083 * context. Until in_pcbrele() has returned that the inpcb is still valid,
1084 * lock and rele are the *only* safe operations that may be performed on the
1087 * While the inpcb will not be freed, releasing the inpcb lock means that the
1088 * connection's state may change, so the caller should be careful to
1089 * revalidate any cached state on reacquiring the lock. Drop the reference
1090 * using in_pcbrele().
1093 in_pcbref(struct inpcb *inp)
1096 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1098 refcount_acquire(&inp->inp_refcount);
1102 * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
1103 * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
1104 * return a flag indicating whether or not the inpcb remains valid. If it is
1105 * valid, we return with the inpcb lock held.
1107 * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
1108 * reference on an inpcb. Historically more work was done here (actually, in
1109 * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
1110 * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely
1111 * about memory stability (and continued use of the write lock).
1114 in_pcbrele_rlocked(struct inpcb *inp)
1116 struct inpcbinfo *pcbinfo;
1118 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1120 INP_RLOCK_ASSERT(inp);
1122 if (refcount_release(&inp->inp_refcount) == 0) {
1124 * If the inpcb has been freed, let the caller know, even if
1125 * this isn't the last reference.
1127 if (inp->inp_flags2 & INP_FREED) {
1134 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1137 pcbinfo = inp->inp_pcbinfo;
1138 uma_zfree(pcbinfo->ipi_zone, inp);
1143 in_pcbrele_wlocked(struct inpcb *inp)
1145 struct inpcbinfo *pcbinfo;
1147 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1149 INP_WLOCK_ASSERT(inp);
1151 if (refcount_release(&inp->inp_refcount) == 0) {
1153 * If the inpcb has been freed, let the caller know, even if
1154 * this isn't the last reference.
1156 if (inp->inp_flags2 & INP_FREED) {
1163 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1166 pcbinfo = inp->inp_pcbinfo;
1167 uma_zfree(pcbinfo->ipi_zone, inp);
1172 * Temporary wrapper.
1175 in_pcbrele(struct inpcb *inp)
1178 return (in_pcbrele_wlocked(inp));
1182 * Unconditionally schedule an inpcb to be freed by decrementing its
1183 * reference count, which should occur only after the inpcb has been detached
1184 * from its socket. If another thread holds a temporary reference (acquired
1185 * using in_pcbref()) then the free is deferred until that reference is
1186 * released using in_pcbrele(), but the inpcb is still unlocked. Almost all
1187 * work, including removal from global lists, is done in this context, where
1188 * the pcbinfo lock is held.
1191 in_pcbfree(struct inpcb *inp)
1193 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1195 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1197 INP_INFO_WLOCK_ASSERT(pcbinfo);
1198 INP_WLOCK_ASSERT(inp);
1200 /* XXXRW: Do as much as possible here. */
1202 if (inp->inp_sp != NULL)
1203 ipsec_delete_pcbpolicy(inp);
1205 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1206 in_pcbremlists(inp);
1208 if (inp->inp_vflag & INP_IPV6PROTO) {
1209 ip6_freepcbopts(inp->in6p_outputopts);
1210 if (inp->in6p_moptions != NULL)
1211 ip6_freemoptions(inp->in6p_moptions);
1214 if (inp->inp_options)
1215 (void)m_free(inp->inp_options);
1217 if (inp->inp_moptions != NULL)
1218 inp_freemoptions(inp->inp_moptions);
1221 inp->inp_flags2 |= INP_FREED;
1222 crfree(inp->inp_cred);
1224 mac_inpcb_destroy(inp);
1226 if (!in_pcbrele_wlocked(inp))
1231 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1232 * port reservation, and preventing it from being returned by inpcb lookups.
1234 * It is used by TCP to mark an inpcb as unused and avoid future packet
1235 * delivery or event notification when a socket remains open but TCP has
1236 * closed. This might occur as a result of a shutdown()-initiated TCP close
1237 * or a RST on the wire, and allows the port binding to be reused while still
1238 * maintaining the invariant that so_pcb always points to a valid inpcb until
1241 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1242 * in_pcbnotifyall() and in_pcbpurgeif0()?
1245 in_pcbdrop(struct inpcb *inp)
1248 INP_WLOCK_ASSERT(inp);
1251 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1254 inp->inp_flags |= INP_DROPPED;
1255 if (inp->inp_flags & INP_INHASHLIST) {
1256 struct inpcbport *phd = inp->inp_phd;
1258 INP_HASH_WLOCK(inp->inp_pcbinfo);
1259 LIST_REMOVE(inp, inp_hash);
1260 LIST_REMOVE(inp, inp_portlist);
1261 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1262 LIST_REMOVE(phd, phd_hash);
1265 INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1266 inp->inp_flags &= ~INP_INHASHLIST;
1268 in_pcbgroup_remove(inp);
1275 * Common routines to return the socket addresses associated with inpcbs.
1278 in_sockaddr(in_port_t port, struct in_addr *addr_p)
1280 struct sockaddr_in *sin;
1282 sin = malloc(sizeof *sin, M_SONAME,
1284 sin->sin_family = AF_INET;
1285 sin->sin_len = sizeof(*sin);
1286 sin->sin_addr = *addr_p;
1287 sin->sin_port = port;
1289 return (struct sockaddr *)sin;
1293 in_getsockaddr(struct socket *so, struct sockaddr **nam)
1296 struct in_addr addr;
1299 inp = sotoinpcb(so);
1300 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1303 port = inp->inp_lport;
1304 addr = inp->inp_laddr;
1307 *nam = in_sockaddr(port, &addr);
1312 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1315 struct in_addr addr;
1318 inp = sotoinpcb(so);
1319 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1322 port = inp->inp_fport;
1323 addr = inp->inp_faddr;
1326 *nam = in_sockaddr(port, &addr);
1331 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1332 struct inpcb *(*notify)(struct inpcb *, int))
1334 struct inpcb *inp, *inp_temp;
1336 INP_INFO_WLOCK(pcbinfo);
1337 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1340 if ((inp->inp_vflag & INP_IPV4) == 0) {
1345 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1346 inp->inp_socket == NULL) {
1350 if ((*notify)(inp, errno))
1353 INP_INFO_WUNLOCK(pcbinfo);
1357 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1360 struct ip_moptions *imo;
1363 INP_INFO_RLOCK(pcbinfo);
1364 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1366 imo = inp->inp_moptions;
1367 if ((inp->inp_vflag & INP_IPV4) &&
1370 * Unselect the outgoing interface if it is being
1373 if (imo->imo_multicast_ifp == ifp)
1374 imo->imo_multicast_ifp = NULL;
1377 * Drop multicast group membership if we joined
1378 * through the interface being detached.
1380 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1382 if (imo->imo_membership[i]->inm_ifp == ifp) {
1383 in_delmulti(imo->imo_membership[i]);
1385 } else if (gap != 0)
1386 imo->imo_membership[i - gap] =
1387 imo->imo_membership[i];
1389 imo->imo_num_memberships -= gap;
1393 INP_INFO_RUNLOCK(pcbinfo);
1397 * Lookup a PCB based on the local address and port. Caller must hold the
1398 * hash lock. No inpcb locks or references are acquired.
1400 #define INP_LOOKUP_MAPPED_PCB_COST 3
1402 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1403 u_short lport, int lookupflags, struct ucred *cred)
1407 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1413 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1414 ("%s: invalid lookup flags %d", __func__, lookupflags));
1416 INP_HASH_LOCK_ASSERT(pcbinfo);
1418 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1419 struct inpcbhead *head;
1421 * Look for an unconnected (wildcard foreign addr) PCB that
1422 * matches the local address and port we're looking for.
1424 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1425 0, pcbinfo->ipi_hashmask)];
1426 LIST_FOREACH(inp, head, inp_hash) {
1428 /* XXX inp locking */
1429 if ((inp->inp_vflag & INP_IPV4) == 0)
1432 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1433 inp->inp_laddr.s_addr == laddr.s_addr &&
1434 inp->inp_lport == lport) {
1439 prison_equal_ip4(cred->cr_prison,
1440 inp->inp_cred->cr_prison))
1449 struct inpcbporthead *porthash;
1450 struct inpcbport *phd;
1451 struct inpcb *match = NULL;
1453 * Best fit PCB lookup.
1455 * First see if this local port is in use by looking on the
1458 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1459 pcbinfo->ipi_porthashmask)];
1460 LIST_FOREACH(phd, porthash, phd_hash) {
1461 if (phd->phd_port == lport)
1466 * Port is in use by one or more PCBs. Look for best
1469 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1472 !prison_equal_ip4(inp->inp_cred->cr_prison,
1476 /* XXX inp locking */
1477 if ((inp->inp_vflag & INP_IPV4) == 0)
1480 * We never select the PCB that has
1481 * INP_IPV6 flag and is bound to :: if
1482 * we have another PCB which is bound
1483 * to 0.0.0.0. If a PCB has the
1484 * INP_IPV6 flag, then we set its cost
1485 * higher than IPv4 only PCBs.
1487 * Note that the case only happens
1488 * when a socket is bound to ::, under
1489 * the condition that the use of the
1490 * mapped address is allowed.
1492 if ((inp->inp_vflag & INP_IPV6) != 0)
1493 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1495 if (inp->inp_faddr.s_addr != INADDR_ANY)
1497 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1498 if (laddr.s_addr == INADDR_ANY)
1500 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1503 if (laddr.s_addr != INADDR_ANY)
1506 if (wildcard < matchwild) {
1508 matchwild = wildcard;
1517 #undef INP_LOOKUP_MAPPED_PCB_COST
1521 * Lookup PCB in hash list, using pcbgroup tables.
1523 static struct inpcb *
1524 in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
1525 struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
1526 u_int lport_arg, int lookupflags, struct ifnet *ifp)
1528 struct inpcbhead *head;
1529 struct inpcb *inp, *tmpinp;
1530 u_short fport = fport_arg, lport = lport_arg;
1533 * First look for an exact match.
1536 INP_GROUP_LOCK(pcbgroup);
1537 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1538 pcbgroup->ipg_hashmask)];
1539 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1541 /* XXX inp locking */
1542 if ((inp->inp_vflag & INP_IPV4) == 0)
1545 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1546 inp->inp_laddr.s_addr == laddr.s_addr &&
1547 inp->inp_fport == fport &&
1548 inp->inp_lport == lport) {
1550 * XXX We should be able to directly return
1551 * the inp here, without any checks.
1552 * Well unless both bound with SO_REUSEPORT?
1554 if (prison_flag(inp->inp_cred, PR_IP4))
1560 if (tmpinp != NULL) {
1566 * Then look for a wildcard match, if requested.
1568 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1569 struct inpcb *local_wild = NULL, *local_exact = NULL;
1571 struct inpcb *local_wild_mapped = NULL;
1573 struct inpcb *jail_wild = NULL;
1574 struct inpcbhead *head;
1578 * Order of socket selection - we always prefer jails.
1579 * 1. jailed, non-wild.
1581 * 3. non-jailed, non-wild.
1582 * 4. non-jailed, wild.
1584 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
1585 0, pcbinfo->ipi_wildmask)];
1586 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1588 /* XXX inp locking */
1589 if ((inp->inp_vflag & INP_IPV4) == 0)
1592 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1593 inp->inp_lport != lport)
1596 /* XXX inp locking */
1597 if (ifp && ifp->if_type == IFT_FAITH &&
1598 (inp->inp_flags & INP_FAITH) == 0)
1601 injail = prison_flag(inp->inp_cred, PR_IP4);
1603 if (prison_check_ip4(inp->inp_cred,
1607 if (local_exact != NULL)
1611 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1616 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1618 /* XXX inp locking, NULL check */
1619 if (inp->inp_vflag & INP_IPV6PROTO)
1620 local_wild_mapped = inp;
1628 } /* LIST_FOREACH */
1636 inp = local_wild_mapped;
1640 } /* if (lookupflags & INPLOOKUP_WILDCARD) */
1641 INP_GROUP_UNLOCK(pcbgroup);
1646 INP_GROUP_UNLOCK(pcbgroup);
1647 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1649 if (in_pcbrele_wlocked(inp))
1651 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1653 if (in_pcbrele_rlocked(inp))
1656 panic("%s: locking bug", __func__);
1659 #endif /* PCBGROUP */
1662 * Lookup PCB in hash list, using pcbinfo tables. This variation assumes
1663 * that the caller has locked the hash list, and will not perform any further
1664 * locking or reference operations on either the hash list or the connection.
1666 static struct inpcb *
1667 in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1668 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1671 struct inpcbhead *head;
1672 struct inpcb *inp, *tmpinp;
1673 u_short fport = fport_arg, lport = lport_arg;
1675 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1676 ("%s: invalid lookup flags %d", __func__, lookupflags));
1678 INP_HASH_LOCK_ASSERT(pcbinfo);
1681 * First look for an exact match.
1684 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1685 pcbinfo->ipi_hashmask)];
1686 LIST_FOREACH(inp, head, inp_hash) {
1688 /* XXX inp locking */
1689 if ((inp->inp_vflag & INP_IPV4) == 0)
1692 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1693 inp->inp_laddr.s_addr == laddr.s_addr &&
1694 inp->inp_fport == fport &&
1695 inp->inp_lport == lport) {
1697 * XXX We should be able to directly return
1698 * the inp here, without any checks.
1699 * Well unless both bound with SO_REUSEPORT?
1701 if (prison_flag(inp->inp_cred, PR_IP4))
1711 * Then look for a wildcard match, if requested.
1713 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1714 struct inpcb *local_wild = NULL, *local_exact = NULL;
1716 struct inpcb *local_wild_mapped = NULL;
1718 struct inpcb *jail_wild = NULL;
1722 * Order of socket selection - we always prefer jails.
1723 * 1. jailed, non-wild.
1725 * 3. non-jailed, non-wild.
1726 * 4. non-jailed, wild.
1729 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1730 0, pcbinfo->ipi_hashmask)];
1731 LIST_FOREACH(inp, head, inp_hash) {
1733 /* XXX inp locking */
1734 if ((inp->inp_vflag & INP_IPV4) == 0)
1737 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1738 inp->inp_lport != lport)
1741 /* XXX inp locking */
1742 if (ifp && ifp->if_type == IFT_FAITH &&
1743 (inp->inp_flags & INP_FAITH) == 0)
1746 injail = prison_flag(inp->inp_cred, PR_IP4);
1748 if (prison_check_ip4(inp->inp_cred,
1752 if (local_exact != NULL)
1756 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1761 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1763 /* XXX inp locking, NULL check */
1764 if (inp->inp_vflag & INP_IPV6PROTO)
1765 local_wild_mapped = inp;
1773 } /* LIST_FOREACH */
1774 if (jail_wild != NULL)
1776 if (local_exact != NULL)
1777 return (local_exact);
1778 if (local_wild != NULL)
1779 return (local_wild);
1781 if (local_wild_mapped != NULL)
1782 return (local_wild_mapped);
1784 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1790 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1791 * hash list lock, and will return the inpcb locked (i.e., requires
1792 * INPLOOKUP_LOCKPCB).
1794 static struct inpcb *
1795 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1796 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1801 INP_HASH_RLOCK(pcbinfo);
1802 inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1803 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1806 INP_HASH_RUNLOCK(pcbinfo);
1807 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1809 if (in_pcbrele_wlocked(inp))
1811 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1813 if (in_pcbrele_rlocked(inp))
1816 panic("%s: locking bug", __func__);
1818 INP_HASH_RUNLOCK(pcbinfo);
1823 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1824 * from which a pre-calculated hash value may be extracted.
1826 * Possibly more of this logic should be in in_pcbgroup.c.
1829 in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1830 struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1832 #if defined(PCBGROUP)
1833 struct inpcbgroup *pcbgroup;
1836 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1837 ("%s: invalid lookup flags %d", __func__, lookupflags));
1838 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1839 ("%s: LOCKPCB not set", __func__));
1841 #if defined(PCBGROUP)
1842 if (in_pcbgroup_enabled(pcbinfo)) {
1843 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1845 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1846 laddr, lport, lookupflags, ifp));
1849 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1854 in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1855 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1856 struct ifnet *ifp, struct mbuf *m)
1859 struct inpcbgroup *pcbgroup;
1862 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1863 ("%s: invalid lookup flags %d", __func__, lookupflags));
1864 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1865 ("%s: LOCKPCB not set", __func__));
1868 if (in_pcbgroup_enabled(pcbinfo)) {
1869 pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1870 m->m_pkthdr.flowid);
1871 if (pcbgroup != NULL)
1872 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
1873 fport, laddr, lport, lookupflags, ifp));
1874 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1876 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1877 laddr, lport, lookupflags, ifp));
1880 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1886 * Insert PCB onto various hash lists.
1889 in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
1891 struct inpcbhead *pcbhash;
1892 struct inpcbporthead *pcbporthash;
1893 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1894 struct inpcbport *phd;
1895 u_int32_t hashkey_faddr;
1897 INP_WLOCK_ASSERT(inp);
1898 INP_HASH_WLOCK_ASSERT(pcbinfo);
1900 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1901 ("in_pcbinshash: INP_INHASHLIST"));
1904 if (inp->inp_vflag & INP_IPV6)
1905 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1908 hashkey_faddr = inp->inp_faddr.s_addr;
1910 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1911 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1913 pcbporthash = &pcbinfo->ipi_porthashbase[
1914 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1917 * Go through port list and look for a head for this lport.
1919 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1920 if (phd->phd_port == inp->inp_lport)
1924 * If none exists, malloc one and tack it on.
1927 phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1929 return (ENOBUFS); /* XXX */
1931 phd->phd_port = inp->inp_lport;
1932 LIST_INIT(&phd->phd_pcblist);
1933 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1936 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1937 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1938 inp->inp_flags |= INP_INHASHLIST;
1940 if (do_pcbgroup_update)
1941 in_pcbgroup_update(inp);
1947 * For now, there are two public interfaces to insert an inpcb into the hash
1948 * lists -- one that does update pcbgroups, and one that doesn't. The latter
1949 * is used only in the TCP syncache, where in_pcbinshash is called before the
1950 * full 4-tuple is set for the inpcb, and we don't want to install in the
1951 * pcbgroup until later.
1953 * XXXRW: This seems like a misfeature. in_pcbinshash should always update
1954 * connection groups, and partially initialised inpcbs should not be exposed
1955 * to either reservation hash tables or pcbgroups.
1958 in_pcbinshash(struct inpcb *inp)
1961 return (in_pcbinshash_internal(inp, 1));
1965 in_pcbinshash_nopcbgroup(struct inpcb *inp)
1968 return (in_pcbinshash_internal(inp, 0));
1972 * Move PCB to the proper hash bucket when { faddr, fport } have been
1973 * changed. NOTE: This does not handle the case of the lport changing (the
1974 * hashed port list would have to be updated as well), so the lport must
1975 * not change after in_pcbinshash() has been called.
1978 in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
1980 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1981 struct inpcbhead *head;
1982 u_int32_t hashkey_faddr;
1984 INP_WLOCK_ASSERT(inp);
1985 INP_HASH_WLOCK_ASSERT(pcbinfo);
1987 KASSERT(inp->inp_flags & INP_INHASHLIST,
1988 ("in_pcbrehash: !INP_INHASHLIST"));
1991 if (inp->inp_vflag & INP_IPV6)
1992 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1995 hashkey_faddr = inp->inp_faddr.s_addr;
1997 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1998 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
2000 LIST_REMOVE(inp, inp_hash);
2001 LIST_INSERT_HEAD(head, inp, inp_hash);
2005 in_pcbgroup_update_mbuf(inp, m);
2007 in_pcbgroup_update(inp);
2012 in_pcbrehash(struct inpcb *inp)
2015 in_pcbrehash_mbuf(inp, NULL);
2019 * Remove PCB from various lists.
2022 in_pcbremlists(struct inpcb *inp)
2024 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2026 INP_INFO_WLOCK_ASSERT(pcbinfo);
2027 INP_WLOCK_ASSERT(inp);
2029 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2030 if (inp->inp_flags & INP_INHASHLIST) {
2031 struct inpcbport *phd = inp->inp_phd;
2033 INP_HASH_WLOCK(pcbinfo);
2034 LIST_REMOVE(inp, inp_hash);
2035 LIST_REMOVE(inp, inp_portlist);
2036 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2037 LIST_REMOVE(phd, phd_hash);
2040 INP_HASH_WUNLOCK(pcbinfo);
2041 inp->inp_flags &= ~INP_INHASHLIST;
2043 LIST_REMOVE(inp, inp_list);
2044 pcbinfo->ipi_count--;
2046 in_pcbgroup_remove(inp);
2051 * A set label operation has occurred at the socket layer, propagate the
2052 * label change into the in_pcb for the socket.
2055 in_pcbsosetlabel(struct socket *so)
2060 inp = sotoinpcb(so);
2061 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2065 mac_inpcb_sosetlabel(so, inp);
2072 * ipport_tick runs once per second, determining if random port allocation
2073 * should be continued. If more than ipport_randomcps ports have been
2074 * allocated in the last second, then we return to sequential port
2075 * allocation. We return to random allocation only once we drop below
2076 * ipport_randomcps for at least ipport_randomtime seconds.
2079 ipport_tick(void *xtp)
2081 VNET_ITERATOR_DECL(vnet_iter);
2083 VNET_LIST_RLOCK_NOSLEEP();
2084 VNET_FOREACH(vnet_iter) {
2085 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */
2086 if (V_ipport_tcpallocs <=
2087 V_ipport_tcplastcount + V_ipport_randomcps) {
2088 if (V_ipport_stoprandom > 0)
2089 V_ipport_stoprandom--;
2091 V_ipport_stoprandom = V_ipport_randomtime;
2092 V_ipport_tcplastcount = V_ipport_tcpallocs;
2095 VNET_LIST_RUNLOCK_NOSLEEP();
2096 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
2103 callout_stop(&ipport_tick_callout);
2107 * The ipport_callout should start running at about the time we attach the
2108 * inet or inet6 domains.
2111 ipport_tick_init(const void *unused __unused)
2114 /* Start ipport_tick. */
2115 callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
2116 callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2117 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2118 SHUTDOWN_PRI_DEFAULT);
2120 SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2121 ipport_tick_init, NULL);
2124 inp_wlock(struct inpcb *inp)
2131 inp_wunlock(struct inpcb *inp)
2138 inp_rlock(struct inpcb *inp)
2145 inp_runlock(struct inpcb *inp)
2153 inp_lock_assert(struct inpcb *inp)
2156 INP_WLOCK_ASSERT(inp);
2160 inp_unlock_assert(struct inpcb *inp)
2163 INP_UNLOCK_ASSERT(inp);
2168 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
2172 INP_INFO_RLOCK(&V_tcbinfo);
2173 LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
2178 INP_INFO_RUNLOCK(&V_tcbinfo);
2182 inp_inpcbtosocket(struct inpcb *inp)
2185 INP_WLOCK_ASSERT(inp);
2186 return (inp->inp_socket);
2190 inp_inpcbtotcpcb(struct inpcb *inp)
2193 INP_WLOCK_ASSERT(inp);
2194 return ((struct tcpcb *)inp->inp_ppcb);
2198 inp_ip_tos_get(const struct inpcb *inp)
2201 return (inp->inp_ip_tos);
2205 inp_ip_tos_set(struct inpcb *inp, int val)
2208 inp->inp_ip_tos = val;
2212 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
2213 uint32_t *faddr, uint16_t *fp)
2216 INP_LOCK_ASSERT(inp);
2217 *laddr = inp->inp_laddr.s_addr;
2218 *faddr = inp->inp_faddr.s_addr;
2219 *lp = inp->inp_lport;
2220 *fp = inp->inp_fport;
2224 so_sotoinpcb(struct socket *so)
2227 return (sotoinpcb(so));
2231 so_sototcpcb(struct socket *so)
2234 return (sototcpcb(so));
2239 db_print_indent(int indent)
2243 for (i = 0; i < indent; i++)
2248 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2250 char faddr_str[48], laddr_str[48];
2252 db_print_indent(indent);
2253 db_printf("%s at %p\n", name, inc);
2258 if (inc->inc_flags & INC_ISIPV6) {
2260 ip6_sprintf(laddr_str, &inc->inc6_laddr);
2261 ip6_sprintf(faddr_str, &inc->inc6_faddr);
2266 inet_ntoa_r(inc->inc_laddr, laddr_str);
2267 inet_ntoa_r(inc->inc_faddr, faddr_str);
2269 db_print_indent(indent);
2270 db_printf("inc_laddr %s inc_lport %u\n", laddr_str,
2271 ntohs(inc->inc_lport));
2272 db_print_indent(indent);
2273 db_printf("inc_faddr %s inc_fport %u\n", faddr_str,
2274 ntohs(inc->inc_fport));
2278 db_print_inpflags(int inp_flags)
2283 if (inp_flags & INP_RECVOPTS) {
2284 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2287 if (inp_flags & INP_RECVRETOPTS) {
2288 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2291 if (inp_flags & INP_RECVDSTADDR) {
2292 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2295 if (inp_flags & INP_HDRINCL) {
2296 db_printf("%sINP_HDRINCL", comma ? ", " : "");
2299 if (inp_flags & INP_HIGHPORT) {
2300 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2303 if (inp_flags & INP_LOWPORT) {
2304 db_printf("%sINP_LOWPORT", comma ? ", " : "");
2307 if (inp_flags & INP_ANONPORT) {
2308 db_printf("%sINP_ANONPORT", comma ? ", " : "");
2311 if (inp_flags & INP_RECVIF) {
2312 db_printf("%sINP_RECVIF", comma ? ", " : "");
2315 if (inp_flags & INP_MTUDISC) {
2316 db_printf("%sINP_MTUDISC", comma ? ", " : "");
2319 if (inp_flags & INP_FAITH) {
2320 db_printf("%sINP_FAITH", comma ? ", " : "");
2323 if (inp_flags & INP_RECVTTL) {
2324 db_printf("%sINP_RECVTTL", comma ? ", " : "");
2327 if (inp_flags & INP_DONTFRAG) {
2328 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2331 if (inp_flags & INP_RECVTOS) {
2332 db_printf("%sINP_RECVTOS", comma ? ", " : "");
2335 if (inp_flags & IN6P_IPV6_V6ONLY) {
2336 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2339 if (inp_flags & IN6P_PKTINFO) {
2340 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2343 if (inp_flags & IN6P_HOPLIMIT) {
2344 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2347 if (inp_flags & IN6P_HOPOPTS) {
2348 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2351 if (inp_flags & IN6P_DSTOPTS) {
2352 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2355 if (inp_flags & IN6P_RTHDR) {
2356 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2359 if (inp_flags & IN6P_RTHDRDSTOPTS) {
2360 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2363 if (inp_flags & IN6P_TCLASS) {
2364 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2367 if (inp_flags & IN6P_AUTOFLOWLABEL) {
2368 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2371 if (inp_flags & INP_TIMEWAIT) {
2372 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2375 if (inp_flags & INP_ONESBCAST) {
2376 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2379 if (inp_flags & INP_DROPPED) {
2380 db_printf("%sINP_DROPPED", comma ? ", " : "");
2383 if (inp_flags & INP_SOCKREF) {
2384 db_printf("%sINP_SOCKREF", comma ? ", " : "");
2387 if (inp_flags & IN6P_RFC2292) {
2388 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2391 if (inp_flags & IN6P_MTU) {
2392 db_printf("IN6P_MTU%s", comma ? ", " : "");
2398 db_print_inpvflag(u_char inp_vflag)
2403 if (inp_vflag & INP_IPV4) {
2404 db_printf("%sINP_IPV4", comma ? ", " : "");
2407 if (inp_vflag & INP_IPV6) {
2408 db_printf("%sINP_IPV6", comma ? ", " : "");
2411 if (inp_vflag & INP_IPV6PROTO) {
2412 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2418 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2421 db_print_indent(indent);
2422 db_printf("%s at %p\n", name, inp);
2426 db_print_indent(indent);
2427 db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2429 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2431 db_print_indent(indent);
2432 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n",
2433 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2435 db_print_indent(indent);
2436 db_printf("inp_label: %p inp_flags: 0x%x (",
2437 inp->inp_label, inp->inp_flags);
2438 db_print_inpflags(inp->inp_flags);
2441 db_print_indent(indent);
2442 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp,
2444 db_print_inpvflag(inp->inp_vflag);
2447 db_print_indent(indent);
2448 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n",
2449 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2451 db_print_indent(indent);
2453 if (inp->inp_vflag & INP_IPV6) {
2454 db_printf("in6p_options: %p in6p_outputopts: %p "
2455 "in6p_moptions: %p\n", inp->in6p_options,
2456 inp->in6p_outputopts, inp->in6p_moptions);
2457 db_printf("in6p_icmp6filt: %p in6p_cksum %d "
2458 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2463 db_printf("inp_ip_tos: %d inp_ip_options: %p "
2464 "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2465 inp->inp_options, inp->inp_moptions);
2468 db_print_indent(indent);
2469 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd,
2470 (uintmax_t)inp->inp_gencnt);
2473 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2478 db_printf("usage: show inpcb <addr>\n");
2481 inp = (struct inpcb *)addr;
2483 db_print_inpcb(inp, "inpcb", 0);