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)
148 error = vnet_sysctl_handle_int(oidp, arg1, arg2, req);
150 error = sysctl_handle_int(oidp, arg1, arg2, req);
153 RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
154 RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
155 RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
156 RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
157 RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
158 RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
165 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
167 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
168 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0,
169 &sysctl_net_ipport_check, "I", "");
170 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
171 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0,
172 &sysctl_net_ipport_check, "I", "");
173 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first,
174 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0,
175 &sysctl_net_ipport_check, "I", "");
176 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last,
177 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0,
178 &sysctl_net_ipport_check, "I", "");
179 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
180 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0,
181 &sysctl_net_ipport_check, "I", "");
182 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
183 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0,
184 &sysctl_net_ipport_check, "I", "");
185 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
186 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, "");
187 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
188 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
189 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
190 &VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
191 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
192 &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
193 "allocations before switching to a sequental one");
194 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
195 &VNET_NAME(ipport_randomtime), 0,
196 "Minimum time to keep sequental port "
197 "allocation before switching to a random one");
201 * in_pcb.c: manage the Protocol Control Blocks.
203 * NOTE: It is assumed that most of these functions will be called with
204 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
205 * functions often modify hash chains or addresses in pcbs.
209 * Initialize an inpcbinfo -- we should be able to reduce the number of
213 in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
214 struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
215 char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
216 uint32_t inpcbzone_flags, u_int hashfields)
219 INP_INFO_LOCK_INIT(pcbinfo, name);
220 INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */
222 pcbinfo->ipi_vnet = curvnet;
224 pcbinfo->ipi_listhead = listhead;
225 LIST_INIT(pcbinfo->ipi_listhead);
226 pcbinfo->ipi_count = 0;
227 pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
228 &pcbinfo->ipi_hashmask);
229 pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
230 &pcbinfo->ipi_porthashmask);
232 in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
234 pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
235 NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
237 uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
241 * Destroy an inpcbinfo.
244 in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
247 KASSERT(pcbinfo->ipi_count == 0,
248 ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
250 hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
251 hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
252 pcbinfo->ipi_porthashmask);
254 in_pcbgroup_destroy(pcbinfo);
256 uma_zdestroy(pcbinfo->ipi_zone);
257 INP_HASH_LOCK_DESTROY(pcbinfo);
258 INP_INFO_LOCK_DESTROY(pcbinfo);
262 * Allocate a PCB and associate it with the socket.
263 * On success return with the PCB locked.
266 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
271 INP_INFO_WLOCK_ASSERT(pcbinfo);
273 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
276 bzero(inp, inp_zero_size);
277 inp->inp_pcbinfo = pcbinfo;
278 inp->inp_socket = so;
279 inp->inp_cred = crhold(so->so_cred);
280 inp->inp_inc.inc_fibnum = so->so_fibnum;
282 error = mac_inpcb_init(inp, M_NOWAIT);
285 mac_inpcb_create(so, inp);
288 error = ipsec_init_policy(so, &inp->inp_sp);
291 mac_inpcb_destroy(inp);
297 if (INP_SOCKAF(so) == AF_INET6) {
298 inp->inp_vflag |= INP_IPV6PROTO;
300 inp->inp_flags |= IN6P_IPV6_V6ONLY;
303 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
304 pcbinfo->ipi_count++;
305 so->so_pcb = (caddr_t)inp;
307 if (V_ip6_auto_flowlabel)
308 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
311 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
312 refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */
313 #if defined(IPSEC) || defined(MAC)
316 crfree(inp->inp_cred);
317 uma_zfree(pcbinfo->ipi_zone, inp);
325 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
329 INP_WLOCK_ASSERT(inp);
330 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
332 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
334 anonport = inp->inp_lport == 0 && (nam == NULL ||
335 ((struct sockaddr_in *)nam)->sin_port == 0);
336 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
337 &inp->inp_lport, cred);
340 if (in_pcbinshash(inp) != 0) {
341 inp->inp_laddr.s_addr = INADDR_ANY;
346 inp->inp_flags |= INP_ANONPORT;
351 #if defined(INET) || defined(INET6)
353 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
354 struct ucred *cred, int lookupflags)
356 struct inpcbinfo *pcbinfo;
357 struct inpcb *tmpinp;
358 unsigned short *lastport;
359 int count, dorandom, error;
360 u_short aux, first, last, lport;
362 struct in_addr laddr;
365 pcbinfo = inp->inp_pcbinfo;
368 * Because no actual state changes occur here, a global write lock on
369 * the pcbinfo isn't required.
371 INP_LOCK_ASSERT(inp);
372 INP_HASH_LOCK_ASSERT(pcbinfo);
374 if (inp->inp_flags & INP_HIGHPORT) {
375 first = V_ipport_hifirstauto; /* sysctl */
376 last = V_ipport_hilastauto;
377 lastport = &pcbinfo->ipi_lasthi;
378 } else if (inp->inp_flags & INP_LOWPORT) {
379 error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
382 first = V_ipport_lowfirstauto; /* 1023 */
383 last = V_ipport_lowlastauto; /* 600 */
384 lastport = &pcbinfo->ipi_lastlow;
386 first = V_ipport_firstauto; /* sysctl */
387 last = V_ipport_lastauto;
388 lastport = &pcbinfo->ipi_lastport;
391 * For UDP, use random port allocation as long as the user
392 * allows it. For TCP (and as of yet unknown) connections,
393 * use random port allocation only if the user allows it AND
394 * ipport_tick() allows it.
396 if (V_ipport_randomized &&
397 (!V_ipport_stoprandom || pcbinfo == &V_udbinfo))
402 * It makes no sense to do random port allocation if
403 * we have the only port available.
407 /* Make sure to not include UDP packets in the count. */
408 if (pcbinfo != &V_udbinfo)
409 V_ipport_tcpallocs++;
411 * Instead of having two loops further down counting up or down
412 * make sure that first is always <= last and go with only one
413 * code path implementing all logic.
422 /* Make the compiler happy. */
424 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
425 KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
430 tmpinp = NULL; /* Make compiler happy. */
434 *lastport = first + (arc4random() % (last - first));
436 count = last - first;
439 if (count-- < 0) /* completely used? */
440 return (EADDRNOTAVAIL);
442 if (*lastport < first || *lastport > last)
444 lport = htons(*lastport);
447 if ((inp->inp_vflag & INP_IPV6) != 0)
448 tmpinp = in6_pcblookup_local(pcbinfo,
449 &inp->in6p_laddr, lport, lookupflags, cred);
451 #if defined(INET) && defined(INET6)
455 tmpinp = in_pcblookup_local(pcbinfo, laddr,
456 lport, lookupflags, cred);
458 } while (tmpinp != NULL);
461 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
462 laddrp->s_addr = laddr.s_addr;
468 #endif /* INET || INET6 */
472 * Set up a bind operation on a PCB, performing port allocation
473 * as required, but do not actually modify the PCB. Callers can
474 * either complete the bind by setting inp_laddr/inp_lport and
475 * calling in_pcbinshash(), or they can just use the resulting
476 * port and address to authorise the sending of a once-off packet.
478 * On error, the values of *laddrp and *lportp are not changed.
481 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
482 u_short *lportp, struct ucred *cred)
484 struct socket *so = inp->inp_socket;
485 struct sockaddr_in *sin;
486 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
487 struct in_addr laddr;
489 int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
493 * No state changes, so read locks are sufficient here.
495 INP_LOCK_ASSERT(inp);
496 INP_HASH_LOCK_ASSERT(pcbinfo);
498 if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
499 return (EADDRNOTAVAIL);
500 laddr.s_addr = *laddrp;
501 if (nam != NULL && laddr.s_addr != INADDR_ANY)
503 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
504 lookupflags = INPLOOKUP_WILDCARD;
506 if ((error = prison_local_ip4(cred, &laddr)) != 0)
509 sin = (struct sockaddr_in *)nam;
510 if (nam->sa_len != sizeof (*sin))
514 * We should check the family, but old programs
515 * incorrectly fail to initialize it.
517 if (sin->sin_family != AF_INET)
518 return (EAFNOSUPPORT);
520 error = prison_local_ip4(cred, &sin->sin_addr);
523 if (sin->sin_port != *lportp) {
524 /* Don't allow the port to change. */
527 lport = sin->sin_port;
529 /* NB: lport is left as 0 if the port isn't being changed. */
530 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
532 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
533 * allow complete duplication of binding if
534 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
535 * and a multicast address is bound on both
536 * new and duplicated sockets.
538 if (so->so_options & SO_REUSEADDR)
539 reuseport = SO_REUSEADDR|SO_REUSEPORT;
540 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
541 sin->sin_port = 0; /* yech... */
542 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
544 * Is the address a local IP address?
545 * If INP_BINDANY is set, then the socket may be bound
546 * to any endpoint address, local or not.
548 if ((inp->inp_flags & INP_BINDANY) == 0 &&
549 ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
550 return (EADDRNOTAVAIL);
552 laddr = sin->sin_addr;
558 if (ntohs(lport) <= V_ipport_reservedhigh &&
559 ntohs(lport) >= V_ipport_reservedlow &&
560 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
563 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
564 priv_check_cred(inp->inp_cred,
565 PRIV_NETINET_REUSEPORT, 0) != 0) {
566 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
567 lport, INPLOOKUP_WILDCARD, cred);
570 * This entire block sorely needs a rewrite.
573 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
574 (so->so_type != SOCK_STREAM ||
575 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
576 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
577 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
578 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
579 (inp->inp_cred->cr_uid !=
580 t->inp_cred->cr_uid))
583 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
584 lport, lookupflags, cred);
585 if (t && (t->inp_flags & INP_TIMEWAIT)) {
587 * XXXRW: If an incpb has had its timewait
588 * state recycled, we treat the address as
589 * being in use (for now). This is better
590 * than a panic, but not desirable.
594 (reuseport & tw->tw_so_options) == 0)
596 } else if (t && (reuseport == 0 ||
597 (t->inp_flags2 & INP_REUSEPORT) == 0)) {
599 if (ntohl(sin->sin_addr.s_addr) !=
601 ntohl(t->inp_laddr.s_addr) !=
603 (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
604 (t->inp_vflag & INP_IPV6PROTO) == 0)
613 error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
618 *laddrp = laddr.s_addr;
624 * Connect from a socket to a specified address.
625 * Both address and port must be specified in argument sin.
626 * If don't have a local address for this socket yet,
630 in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
631 struct ucred *cred, struct mbuf *m)
633 u_short lport, fport;
634 in_addr_t laddr, faddr;
637 INP_WLOCK_ASSERT(inp);
638 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
640 lport = inp->inp_lport;
641 laddr = inp->inp_laddr.s_addr;
642 anonport = (lport == 0);
643 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
648 /* Do the initial binding of the local address if required. */
649 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
650 inp->inp_lport = lport;
651 inp->inp_laddr.s_addr = laddr;
652 if (in_pcbinshash(inp) != 0) {
653 inp->inp_laddr.s_addr = INADDR_ANY;
659 /* Commit the remaining changes. */
660 inp->inp_lport = lport;
661 inp->inp_laddr.s_addr = laddr;
662 inp->inp_faddr.s_addr = faddr;
663 inp->inp_fport = fport;
664 in_pcbrehash_mbuf(inp, m);
667 inp->inp_flags |= INP_ANONPORT;
672 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
675 return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
679 * Do proper source address selection on an unbound socket in case
680 * of connect. Take jails into account as well.
683 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
688 struct sockaddr_in *sin;
692 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
695 * Bypass source address selection and use the primary jail IP
698 if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
702 bzero(&sro, sizeof(sro));
704 sin = (struct sockaddr_in *)&sro.ro_dst;
705 sin->sin_family = AF_INET;
706 sin->sin_len = sizeof(struct sockaddr_in);
707 sin->sin_addr.s_addr = faddr->s_addr;
710 * If route is known our src addr is taken from the i/f,
713 * Find out route to destination.
715 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
716 in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
719 * If we found a route, use the address corresponding to
720 * the outgoing interface.
722 * Otherwise assume faddr is reachable on a directly connected
723 * network and try to find a corresponding interface to take
724 * the source address from.
726 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
727 struct in_ifaddr *ia;
730 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
732 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0));
738 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
739 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
740 ifa_free(&ia->ia_ifa);
745 ifa_free(&ia->ia_ifa);
748 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
751 if (sa->sa_family != AF_INET)
753 sin = (struct sockaddr_in *)sa;
754 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
755 ia = (struct in_ifaddr *)ifa;
760 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
766 /* 3. As a last resort return the 'default' jail address. */
767 error = prison_get_ip4(cred, laddr);
772 * If the outgoing interface on the route found is not
773 * a loopback interface, use the address from that interface.
774 * In case of jails do those three steps:
775 * 1. check if the interface address belongs to the jail. If so use it.
776 * 2. check if we have any address on the outgoing interface
777 * belonging to this jail. If so use it.
778 * 3. as a last resort return the 'default' jail address.
780 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
781 struct in_ifaddr *ia;
784 /* If not jailed, use the default returned. */
785 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
786 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
787 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
792 /* 1. Check if the iface address belongs to the jail. */
793 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
794 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
795 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
796 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
801 * 2. Check if we have any address on the outgoing interface
802 * belonging to this jail.
805 ifp = sro.ro_rt->rt_ifp;
807 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
809 if (sa->sa_family != AF_INET)
811 sin = (struct sockaddr_in *)sa;
812 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
813 ia = (struct in_ifaddr *)ifa;
818 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
824 /* 3. As a last resort return the 'default' jail address. */
825 error = prison_get_ip4(cred, laddr);
830 * The outgoing interface is marked with 'loopback net', so a route
831 * to ourselves is here.
832 * Try to find the interface of the destination address and then
833 * take the address from there. That interface is not necessarily
834 * a loopback interface.
835 * In case of jails, check that it is an address of the jail
836 * and if we cannot find, fall back to the 'default' jail address.
838 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
839 struct sockaddr_in sain;
840 struct in_ifaddr *ia;
842 bzero(&sain, sizeof(struct sockaddr_in));
843 sain.sin_family = AF_INET;
844 sain.sin_len = sizeof(struct sockaddr_in);
845 sain.sin_addr.s_addr = faddr->s_addr;
847 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
849 ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0));
851 ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
853 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
858 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
859 ifa_free(&ia->ia_ifa);
868 ifa_free(&ia->ia_ifa);
871 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
874 if (sa->sa_family != AF_INET)
876 sin = (struct sockaddr_in *)sa;
877 if (prison_check_ip4(cred,
878 &sin->sin_addr) == 0) {
879 ia = (struct in_ifaddr *)ifa;
884 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
891 /* 3. As a last resort return the 'default' jail address. */
892 error = prison_get_ip4(cred, laddr);
897 if (sro.ro_rt != NULL)
903 * Set up for a connect from a socket to the specified address.
904 * On entry, *laddrp and *lportp should contain the current local
905 * address and port for the PCB; these are updated to the values
906 * that should be placed in inp_laddr and inp_lport to complete
909 * On success, *faddrp and *fportp will be set to the remote address
910 * and port. These are not updated in the error case.
912 * If the operation fails because the connection already exists,
913 * *oinpp will be set to the PCB of that connection so that the
914 * caller can decide to override it. In all other cases, *oinpp
918 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
919 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
920 struct inpcb **oinpp, struct ucred *cred)
922 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
923 struct in_ifaddr *ia;
925 struct in_addr laddr, faddr;
926 u_short lport, fport;
930 * Because a global state change doesn't actually occur here, a read
931 * lock is sufficient.
933 INP_LOCK_ASSERT(inp);
934 INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
938 if (nam->sa_len != sizeof (*sin))
940 if (sin->sin_family != AF_INET)
941 return (EAFNOSUPPORT);
942 if (sin->sin_port == 0)
943 return (EADDRNOTAVAIL);
944 laddr.s_addr = *laddrp;
946 faddr = sin->sin_addr;
947 fport = sin->sin_port;
949 if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
951 * If the destination address is INADDR_ANY,
952 * use the primary local address.
953 * If the supplied address is INADDR_BROADCAST,
954 * and the primary interface supports broadcast,
955 * choose the broadcast address for that interface.
957 if (faddr.s_addr == INADDR_ANY) {
960 IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
963 (error = prison_get_ip4(cred, &faddr)) != 0)
965 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
967 if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
969 faddr = satosin(&TAILQ_FIRST(
970 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
974 if (laddr.s_addr == INADDR_ANY) {
975 error = in_pcbladdr(inp, &faddr, &laddr, cred);
977 * If the destination address is multicast and an outgoing
978 * interface has been set as a multicast option, prefer the
979 * address of that interface as our source address.
981 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
982 inp->inp_moptions != NULL) {
983 struct ip_moptions *imo;
986 imo = inp->inp_moptions;
987 if (imo->imo_multicast_ifp != NULL) {
988 ifp = imo->imo_multicast_ifp;
990 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
991 if ((ia->ia_ifp == ifp) &&
993 prison_check_ip4(cred,
994 &ia->ia_addr.sin_addr) == 0))
998 error = EADDRNOTAVAIL;
1000 laddr = ia->ia_addr.sin_addr;
1003 IN_IFADDR_RUNLOCK();
1009 oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1010 laddr, lport, 0, NULL);
1014 return (EADDRINUSE);
1017 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1022 *laddrp = laddr.s_addr;
1024 *faddrp = faddr.s_addr;
1030 in_pcbdisconnect(struct inpcb *inp)
1033 INP_WLOCK_ASSERT(inp);
1034 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1036 inp->inp_faddr.s_addr = INADDR_ANY;
1043 * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1044 * For most protocols, this will be invoked immediately prior to calling
1045 * in_pcbfree(). However, with TCP the inpcb may significantly outlive the
1046 * socket, in which case in_pcbfree() is deferred.
1049 in_pcbdetach(struct inpcb *inp)
1052 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1054 inp->inp_socket->so_pcb = NULL;
1055 inp->inp_socket = NULL;
1059 * in_pcbref() bumps the reference count on an inpcb in order to maintain
1060 * stability of an inpcb pointer despite the inpcb lock being released. This
1061 * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
1062 * but where the inpcb lock may already held, or when acquiring a reference
1065 * in_pcbref() should be used only to provide brief memory stability, and
1066 * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
1067 * garbage collect the inpcb if it has been in_pcbfree()'d from another
1068 * context. Until in_pcbrele() has returned that the inpcb is still valid,
1069 * lock and rele are the *only* safe operations that may be performed on the
1072 * While the inpcb will not be freed, releasing the inpcb lock means that the
1073 * connection's state may change, so the caller should be careful to
1074 * revalidate any cached state on reacquiring the lock. Drop the reference
1075 * using in_pcbrele().
1078 in_pcbref(struct inpcb *inp)
1081 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1083 refcount_acquire(&inp->inp_refcount);
1087 * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
1088 * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
1089 * return a flag indicating whether or not the inpcb remains valid. If it is
1090 * valid, we return with the inpcb lock held.
1092 * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
1093 * reference on an inpcb. Historically more work was done here (actually, in
1094 * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
1095 * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely
1096 * about memory stability (and continued use of the write lock).
1099 in_pcbrele_rlocked(struct inpcb *inp)
1101 struct inpcbinfo *pcbinfo;
1103 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1105 INP_RLOCK_ASSERT(inp);
1107 if (refcount_release(&inp->inp_refcount) == 0)
1110 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1113 pcbinfo = inp->inp_pcbinfo;
1114 uma_zfree(pcbinfo->ipi_zone, inp);
1119 in_pcbrele_wlocked(struct inpcb *inp)
1121 struct inpcbinfo *pcbinfo;
1123 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1125 INP_WLOCK_ASSERT(inp);
1127 if (refcount_release(&inp->inp_refcount) == 0)
1130 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1133 pcbinfo = inp->inp_pcbinfo;
1134 uma_zfree(pcbinfo->ipi_zone, inp);
1139 * Temporary wrapper.
1142 in_pcbrele(struct inpcb *inp)
1145 return (in_pcbrele_wlocked(inp));
1149 * Unconditionally schedule an inpcb to be freed by decrementing its
1150 * reference count, which should occur only after the inpcb has been detached
1151 * from its socket. If another thread holds a temporary reference (acquired
1152 * using in_pcbref()) then the free is deferred until that reference is
1153 * released using in_pcbrele(), but the inpcb is still unlocked. Almost all
1154 * work, including removal from global lists, is done in this context, where
1155 * the pcbinfo lock is held.
1158 in_pcbfree(struct inpcb *inp)
1160 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1162 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1164 INP_INFO_WLOCK_ASSERT(pcbinfo);
1165 INP_WLOCK_ASSERT(inp);
1167 /* XXXRW: Do as much as possible here. */
1169 if (inp->inp_sp != NULL)
1170 ipsec_delete_pcbpolicy(inp);
1172 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1173 in_pcbremlists(inp);
1175 if (inp->inp_vflag & INP_IPV6PROTO) {
1176 ip6_freepcbopts(inp->in6p_outputopts);
1177 if (inp->in6p_moptions != NULL)
1178 ip6_freemoptions(inp->in6p_moptions);
1181 if (inp->inp_options)
1182 (void)m_free(inp->inp_options);
1184 if (inp->inp_moptions != NULL)
1185 inp_freemoptions(inp->inp_moptions);
1188 crfree(inp->inp_cred);
1190 mac_inpcb_destroy(inp);
1192 if (!in_pcbrele_wlocked(inp))
1197 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1198 * port reservation, and preventing it from being returned by inpcb lookups.
1200 * It is used by TCP to mark an inpcb as unused and avoid future packet
1201 * delivery or event notification when a socket remains open but TCP has
1202 * closed. This might occur as a result of a shutdown()-initiated TCP close
1203 * or a RST on the wire, and allows the port binding to be reused while still
1204 * maintaining the invariant that so_pcb always points to a valid inpcb until
1207 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1208 * in_pcbnotifyall() and in_pcbpurgeif0()?
1211 in_pcbdrop(struct inpcb *inp)
1214 INP_WLOCK_ASSERT(inp);
1217 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1220 inp->inp_flags |= INP_DROPPED;
1221 if (inp->inp_flags & INP_INHASHLIST) {
1222 struct inpcbport *phd = inp->inp_phd;
1224 INP_HASH_WLOCK(inp->inp_pcbinfo);
1225 LIST_REMOVE(inp, inp_hash);
1226 LIST_REMOVE(inp, inp_portlist);
1227 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1228 LIST_REMOVE(phd, phd_hash);
1231 INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1232 inp->inp_flags &= ~INP_INHASHLIST;
1234 in_pcbgroup_remove(inp);
1241 * Common routines to return the socket addresses associated with inpcbs.
1244 in_sockaddr(in_port_t port, struct in_addr *addr_p)
1246 struct sockaddr_in *sin;
1248 sin = malloc(sizeof *sin, M_SONAME,
1250 sin->sin_family = AF_INET;
1251 sin->sin_len = sizeof(*sin);
1252 sin->sin_addr = *addr_p;
1253 sin->sin_port = port;
1255 return (struct sockaddr *)sin;
1259 in_getsockaddr(struct socket *so, struct sockaddr **nam)
1262 struct in_addr addr;
1265 inp = sotoinpcb(so);
1266 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1269 port = inp->inp_lport;
1270 addr = inp->inp_laddr;
1273 *nam = in_sockaddr(port, &addr);
1278 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1281 struct in_addr addr;
1284 inp = sotoinpcb(so);
1285 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1288 port = inp->inp_fport;
1289 addr = inp->inp_faddr;
1292 *nam = in_sockaddr(port, &addr);
1297 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1298 struct inpcb *(*notify)(struct inpcb *, int))
1300 struct inpcb *inp, *inp_temp;
1302 INP_INFO_WLOCK(pcbinfo);
1303 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1306 if ((inp->inp_vflag & INP_IPV4) == 0) {
1311 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1312 inp->inp_socket == NULL) {
1316 if ((*notify)(inp, errno))
1319 INP_INFO_WUNLOCK(pcbinfo);
1323 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1326 struct ip_moptions *imo;
1329 INP_INFO_RLOCK(pcbinfo);
1330 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1332 imo = inp->inp_moptions;
1333 if ((inp->inp_vflag & INP_IPV4) &&
1336 * Unselect the outgoing interface if it is being
1339 if (imo->imo_multicast_ifp == ifp)
1340 imo->imo_multicast_ifp = NULL;
1343 * Drop multicast group membership if we joined
1344 * through the interface being detached.
1346 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1348 if (imo->imo_membership[i]->inm_ifp == ifp) {
1349 in_delmulti(imo->imo_membership[i]);
1351 } else if (gap != 0)
1352 imo->imo_membership[i - gap] =
1353 imo->imo_membership[i];
1355 imo->imo_num_memberships -= gap;
1359 INP_INFO_RUNLOCK(pcbinfo);
1363 * Lookup a PCB based on the local address and port. Caller must hold the
1364 * hash lock. No inpcb locks or references are acquired.
1366 #define INP_LOOKUP_MAPPED_PCB_COST 3
1368 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1369 u_short lport, int lookupflags, struct ucred *cred)
1373 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1379 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1380 ("%s: invalid lookup flags %d", __func__, lookupflags));
1382 INP_HASH_LOCK_ASSERT(pcbinfo);
1384 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1385 struct inpcbhead *head;
1387 * Look for an unconnected (wildcard foreign addr) PCB that
1388 * matches the local address and port we're looking for.
1390 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1391 0, pcbinfo->ipi_hashmask)];
1392 LIST_FOREACH(inp, head, inp_hash) {
1394 /* XXX inp locking */
1395 if ((inp->inp_vflag & INP_IPV4) == 0)
1398 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1399 inp->inp_laddr.s_addr == laddr.s_addr &&
1400 inp->inp_lport == lport) {
1405 prison_equal_ip4(cred->cr_prison,
1406 inp->inp_cred->cr_prison))
1415 struct inpcbporthead *porthash;
1416 struct inpcbport *phd;
1417 struct inpcb *match = NULL;
1419 * Best fit PCB lookup.
1421 * First see if this local port is in use by looking on the
1424 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1425 pcbinfo->ipi_porthashmask)];
1426 LIST_FOREACH(phd, porthash, phd_hash) {
1427 if (phd->phd_port == lport)
1432 * Port is in use by one or more PCBs. Look for best
1435 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1438 !prison_equal_ip4(inp->inp_cred->cr_prison,
1442 /* XXX inp locking */
1443 if ((inp->inp_vflag & INP_IPV4) == 0)
1446 * We never select the PCB that has
1447 * INP_IPV6 flag and is bound to :: if
1448 * we have another PCB which is bound
1449 * to 0.0.0.0. If a PCB has the
1450 * INP_IPV6 flag, then we set its cost
1451 * higher than IPv4 only PCBs.
1453 * Note that the case only happens
1454 * when a socket is bound to ::, under
1455 * the condition that the use of the
1456 * mapped address is allowed.
1458 if ((inp->inp_vflag & INP_IPV6) != 0)
1459 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1461 if (inp->inp_faddr.s_addr != INADDR_ANY)
1463 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1464 if (laddr.s_addr == INADDR_ANY)
1466 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1469 if (laddr.s_addr != INADDR_ANY)
1472 if (wildcard < matchwild) {
1474 matchwild = wildcard;
1483 #undef INP_LOOKUP_MAPPED_PCB_COST
1487 * Lookup PCB in hash list, using pcbgroup tables.
1489 static struct inpcb *
1490 in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
1491 struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
1492 u_int lport_arg, int lookupflags, struct ifnet *ifp)
1494 struct inpcbhead *head;
1495 struct inpcb *inp, *tmpinp;
1496 u_short fport = fport_arg, lport = lport_arg;
1499 * First look for an exact match.
1502 INP_GROUP_LOCK(pcbgroup);
1503 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1504 pcbgroup->ipg_hashmask)];
1505 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1507 /* XXX inp locking */
1508 if ((inp->inp_vflag & INP_IPV4) == 0)
1511 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1512 inp->inp_laddr.s_addr == laddr.s_addr &&
1513 inp->inp_fport == fport &&
1514 inp->inp_lport == lport) {
1516 * XXX We should be able to directly return
1517 * the inp here, without any checks.
1518 * Well unless both bound with SO_REUSEPORT?
1520 if (prison_flag(inp->inp_cred, PR_IP4))
1526 if (tmpinp != NULL) {
1532 * Then look for a wildcard match, if requested.
1534 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1535 struct inpcb *local_wild = NULL, *local_exact = NULL;
1537 struct inpcb *local_wild_mapped = NULL;
1539 struct inpcb *jail_wild = NULL;
1540 struct inpcbhead *head;
1544 * Order of socket selection - we always prefer jails.
1545 * 1. jailed, non-wild.
1547 * 3. non-jailed, non-wild.
1548 * 4. non-jailed, wild.
1550 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
1551 0, pcbinfo->ipi_wildmask)];
1552 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1554 /* XXX inp locking */
1555 if ((inp->inp_vflag & INP_IPV4) == 0)
1558 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1559 inp->inp_lport != lport)
1562 /* XXX inp locking */
1563 if (ifp && ifp->if_type == IFT_FAITH &&
1564 (inp->inp_flags & INP_FAITH) == 0)
1567 injail = prison_flag(inp->inp_cred, PR_IP4);
1569 if (prison_check_ip4(inp->inp_cred,
1573 if (local_exact != NULL)
1577 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1582 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1584 /* XXX inp locking, NULL check */
1585 if (inp->inp_vflag & INP_IPV6PROTO)
1586 local_wild_mapped = inp;
1594 } /* LIST_FOREACH */
1602 inp = local_wild_mapped;
1603 #endif /* defined(INET6) */
1606 } /* if (lookupflags & INPLOOKUP_WILDCARD) */
1607 INP_GROUP_UNLOCK(pcbgroup);
1612 INP_GROUP_UNLOCK(pcbgroup);
1613 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1615 if (in_pcbrele_wlocked(inp))
1617 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1619 if (in_pcbrele_rlocked(inp))
1622 panic("%s: locking bug", __func__);
1625 #endif /* PCBGROUP */
1628 * Lookup PCB in hash list, using pcbinfo tables. This variation assumes
1629 * that the caller has locked the hash list, and will not perform any further
1630 * locking or reference operations on either the hash list or the connection.
1632 static struct inpcb *
1633 in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1634 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1637 struct inpcbhead *head;
1638 struct inpcb *inp, *tmpinp;
1639 u_short fport = fport_arg, lport = lport_arg;
1641 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1642 ("%s: invalid lookup flags %d", __func__, lookupflags));
1644 INP_HASH_LOCK_ASSERT(pcbinfo);
1647 * First look for an exact match.
1650 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1651 pcbinfo->ipi_hashmask)];
1652 LIST_FOREACH(inp, head, inp_hash) {
1654 /* XXX inp locking */
1655 if ((inp->inp_vflag & INP_IPV4) == 0)
1658 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1659 inp->inp_laddr.s_addr == laddr.s_addr &&
1660 inp->inp_fport == fport &&
1661 inp->inp_lport == lport) {
1663 * XXX We should be able to directly return
1664 * the inp here, without any checks.
1665 * Well unless both bound with SO_REUSEPORT?
1667 if (prison_flag(inp->inp_cred, PR_IP4))
1677 * Then look for a wildcard match, if requested.
1679 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1680 struct inpcb *local_wild = NULL, *local_exact = NULL;
1682 struct inpcb *local_wild_mapped = NULL;
1684 struct inpcb *jail_wild = NULL;
1688 * Order of socket selection - we always prefer jails.
1689 * 1. jailed, non-wild.
1691 * 3. non-jailed, non-wild.
1692 * 4. non-jailed, wild.
1695 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1696 0, pcbinfo->ipi_hashmask)];
1697 LIST_FOREACH(inp, head, inp_hash) {
1699 /* XXX inp locking */
1700 if ((inp->inp_vflag & INP_IPV4) == 0)
1703 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1704 inp->inp_lport != lport)
1707 /* XXX inp locking */
1708 if (ifp && ifp->if_type == IFT_FAITH &&
1709 (inp->inp_flags & INP_FAITH) == 0)
1712 injail = prison_flag(inp->inp_cred, PR_IP4);
1714 if (prison_check_ip4(inp->inp_cred,
1718 if (local_exact != NULL)
1722 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1727 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1729 /* XXX inp locking, NULL check */
1730 if (inp->inp_vflag & INP_IPV6PROTO)
1731 local_wild_mapped = inp;
1739 } /* LIST_FOREACH */
1740 if (jail_wild != NULL)
1742 if (local_exact != NULL)
1743 return (local_exact);
1744 if (local_wild != NULL)
1745 return (local_wild);
1747 if (local_wild_mapped != NULL)
1748 return (local_wild_mapped);
1749 #endif /* defined(INET6) */
1750 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1756 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1757 * hash list lock, and will return the inpcb locked (i.e., requires
1758 * INPLOOKUP_LOCKPCB).
1760 static struct inpcb *
1761 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1762 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1767 INP_HASH_RLOCK(pcbinfo);
1768 inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1769 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1772 INP_HASH_RUNLOCK(pcbinfo);
1773 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1775 if (in_pcbrele_wlocked(inp))
1777 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1779 if (in_pcbrele_rlocked(inp))
1782 panic("%s: locking bug", __func__);
1784 INP_HASH_RUNLOCK(pcbinfo);
1789 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1790 * from which a pre-calculated hash value may be extracted.
1792 * Possibly more of this logic should be in in_pcbgroup.c.
1795 in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1796 struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1798 #if defined(PCBGROUP)
1799 struct inpcbgroup *pcbgroup;
1802 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1803 ("%s: invalid lookup flags %d", __func__, lookupflags));
1804 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1805 ("%s: LOCKPCB not set", __func__));
1807 #if defined(PCBGROUP)
1808 if (in_pcbgroup_enabled(pcbinfo)) {
1809 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1811 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1812 laddr, lport, lookupflags, ifp));
1815 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1820 in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1821 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1822 struct ifnet *ifp, struct mbuf *m)
1825 struct inpcbgroup *pcbgroup;
1828 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1829 ("%s: invalid lookup flags %d", __func__, lookupflags));
1830 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1831 ("%s: LOCKPCB not set", __func__));
1834 if (in_pcbgroup_enabled(pcbinfo)) {
1835 pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1836 m->m_pkthdr.flowid);
1837 if (pcbgroup != NULL)
1838 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
1839 fport, laddr, lport, lookupflags, ifp));
1840 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1842 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1843 laddr, lport, lookupflags, ifp));
1846 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1852 * Insert PCB onto various hash lists.
1855 in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
1857 struct inpcbhead *pcbhash;
1858 struct inpcbporthead *pcbporthash;
1859 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1860 struct inpcbport *phd;
1861 u_int32_t hashkey_faddr;
1863 INP_WLOCK_ASSERT(inp);
1864 INP_HASH_WLOCK_ASSERT(pcbinfo);
1866 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1867 ("in_pcbinshash: INP_INHASHLIST"));
1870 if (inp->inp_vflag & INP_IPV6)
1871 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1874 hashkey_faddr = inp->inp_faddr.s_addr;
1876 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1877 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1879 pcbporthash = &pcbinfo->ipi_porthashbase[
1880 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1883 * Go through port list and look for a head for this lport.
1885 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1886 if (phd->phd_port == inp->inp_lport)
1890 * If none exists, malloc one and tack it on.
1893 phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1895 return (ENOBUFS); /* XXX */
1897 phd->phd_port = inp->inp_lport;
1898 LIST_INIT(&phd->phd_pcblist);
1899 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1902 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1903 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1904 inp->inp_flags |= INP_INHASHLIST;
1906 if (do_pcbgroup_update)
1907 in_pcbgroup_update(inp);
1913 * For now, there are two public interfaces to insert an inpcb into the hash
1914 * lists -- one that does update pcbgroups, and one that doesn't. The latter
1915 * is used only in the TCP syncache, where in_pcbinshash is called before the
1916 * full 4-tuple is set for the inpcb, and we don't want to install in the
1917 * pcbgroup until later.
1919 * XXXRW: This seems like a misfeature. in_pcbinshash should always update
1920 * connection groups, and partially initialised inpcbs should not be exposed
1921 * to either reservation hash tables or pcbgroups.
1924 in_pcbinshash(struct inpcb *inp)
1927 return (in_pcbinshash_internal(inp, 1));
1931 in_pcbinshash_nopcbgroup(struct inpcb *inp)
1934 return (in_pcbinshash_internal(inp, 0));
1938 * Move PCB to the proper hash bucket when { faddr, fport } have been
1939 * changed. NOTE: This does not handle the case of the lport changing (the
1940 * hashed port list would have to be updated as well), so the lport must
1941 * not change after in_pcbinshash() has been called.
1944 in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
1946 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1947 struct inpcbhead *head;
1948 u_int32_t hashkey_faddr;
1950 INP_WLOCK_ASSERT(inp);
1951 INP_HASH_WLOCK_ASSERT(pcbinfo);
1953 KASSERT(inp->inp_flags & INP_INHASHLIST,
1954 ("in_pcbrehash: !INP_INHASHLIST"));
1957 if (inp->inp_vflag & INP_IPV6)
1958 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1961 hashkey_faddr = inp->inp_faddr.s_addr;
1963 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1964 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1966 LIST_REMOVE(inp, inp_hash);
1967 LIST_INSERT_HEAD(head, inp, inp_hash);
1971 in_pcbgroup_update_mbuf(inp, m);
1973 in_pcbgroup_update(inp);
1978 in_pcbrehash(struct inpcb *inp)
1981 in_pcbrehash_mbuf(inp, NULL);
1985 * Remove PCB from various lists.
1988 in_pcbremlists(struct inpcb *inp)
1990 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1992 INP_INFO_WLOCK_ASSERT(pcbinfo);
1993 INP_WLOCK_ASSERT(inp);
1995 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1996 if (inp->inp_flags & INP_INHASHLIST) {
1997 struct inpcbport *phd = inp->inp_phd;
1999 INP_HASH_WLOCK(pcbinfo);
2000 LIST_REMOVE(inp, inp_hash);
2001 LIST_REMOVE(inp, inp_portlist);
2002 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2003 LIST_REMOVE(phd, phd_hash);
2006 INP_HASH_WUNLOCK(pcbinfo);
2007 inp->inp_flags &= ~INP_INHASHLIST;
2009 LIST_REMOVE(inp, inp_list);
2010 pcbinfo->ipi_count--;
2012 in_pcbgroup_remove(inp);
2017 * A set label operation has occurred at the socket layer, propagate the
2018 * label change into the in_pcb for the socket.
2021 in_pcbsosetlabel(struct socket *so)
2026 inp = sotoinpcb(so);
2027 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2031 mac_inpcb_sosetlabel(so, inp);
2038 * ipport_tick runs once per second, determining if random port allocation
2039 * should be continued. If more than ipport_randomcps ports have been
2040 * allocated in the last second, then we return to sequential port
2041 * allocation. We return to random allocation only once we drop below
2042 * ipport_randomcps for at least ipport_randomtime seconds.
2045 ipport_tick(void *xtp)
2047 VNET_ITERATOR_DECL(vnet_iter);
2049 VNET_LIST_RLOCK_NOSLEEP();
2050 VNET_FOREACH(vnet_iter) {
2051 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */
2052 if (V_ipport_tcpallocs <=
2053 V_ipport_tcplastcount + V_ipport_randomcps) {
2054 if (V_ipport_stoprandom > 0)
2055 V_ipport_stoprandom--;
2057 V_ipport_stoprandom = V_ipport_randomtime;
2058 V_ipport_tcplastcount = V_ipport_tcpallocs;
2061 VNET_LIST_RUNLOCK_NOSLEEP();
2062 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
2069 callout_stop(&ipport_tick_callout);
2073 * The ipport_callout should start running at about the time we attach the
2074 * inet or inet6 domains.
2077 ipport_tick_init(const void *unused __unused)
2080 /* Start ipport_tick. */
2081 callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
2082 callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2083 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2084 SHUTDOWN_PRI_DEFAULT);
2086 SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2087 ipport_tick_init, NULL);
2090 inp_wlock(struct inpcb *inp)
2097 inp_wunlock(struct inpcb *inp)
2104 inp_rlock(struct inpcb *inp)
2111 inp_runlock(struct inpcb *inp)
2119 inp_lock_assert(struct inpcb *inp)
2122 INP_WLOCK_ASSERT(inp);
2126 inp_unlock_assert(struct inpcb *inp)
2129 INP_UNLOCK_ASSERT(inp);
2134 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
2138 INP_INFO_RLOCK(&V_tcbinfo);
2139 LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
2144 INP_INFO_RUNLOCK(&V_tcbinfo);
2148 inp_inpcbtosocket(struct inpcb *inp)
2151 INP_WLOCK_ASSERT(inp);
2152 return (inp->inp_socket);
2156 inp_inpcbtotcpcb(struct inpcb *inp)
2159 INP_WLOCK_ASSERT(inp);
2160 return ((struct tcpcb *)inp->inp_ppcb);
2164 inp_ip_tos_get(const struct inpcb *inp)
2167 return (inp->inp_ip_tos);
2171 inp_ip_tos_set(struct inpcb *inp, int val)
2174 inp->inp_ip_tos = val;
2178 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
2179 uint32_t *faddr, uint16_t *fp)
2182 INP_LOCK_ASSERT(inp);
2183 *laddr = inp->inp_laddr.s_addr;
2184 *faddr = inp->inp_faddr.s_addr;
2185 *lp = inp->inp_lport;
2186 *fp = inp->inp_fport;
2190 so_sotoinpcb(struct socket *so)
2193 return (sotoinpcb(so));
2197 so_sototcpcb(struct socket *so)
2200 return (sototcpcb(so));
2205 db_print_indent(int indent)
2209 for (i = 0; i < indent; i++)
2214 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2216 char faddr_str[48], laddr_str[48];
2218 db_print_indent(indent);
2219 db_printf("%s at %p\n", name, inc);
2224 if (inc->inc_flags & INC_ISIPV6) {
2226 ip6_sprintf(laddr_str, &inc->inc6_laddr);
2227 ip6_sprintf(faddr_str, &inc->inc6_faddr);
2231 inet_ntoa_r(inc->inc_laddr, laddr_str);
2232 inet_ntoa_r(inc->inc_faddr, faddr_str);
2236 db_print_indent(indent);
2237 db_printf("inc_laddr %s inc_lport %u\n", laddr_str,
2238 ntohs(inc->inc_lport));
2239 db_print_indent(indent);
2240 db_printf("inc_faddr %s inc_fport %u\n", faddr_str,
2241 ntohs(inc->inc_fport));
2245 db_print_inpflags(int inp_flags)
2250 if (inp_flags & INP_RECVOPTS) {
2251 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2254 if (inp_flags & INP_RECVRETOPTS) {
2255 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2258 if (inp_flags & INP_RECVDSTADDR) {
2259 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2262 if (inp_flags & INP_HDRINCL) {
2263 db_printf("%sINP_HDRINCL", comma ? ", " : "");
2266 if (inp_flags & INP_HIGHPORT) {
2267 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2270 if (inp_flags & INP_LOWPORT) {
2271 db_printf("%sINP_LOWPORT", comma ? ", " : "");
2274 if (inp_flags & INP_ANONPORT) {
2275 db_printf("%sINP_ANONPORT", comma ? ", " : "");
2278 if (inp_flags & INP_RECVIF) {
2279 db_printf("%sINP_RECVIF", comma ? ", " : "");
2282 if (inp_flags & INP_MTUDISC) {
2283 db_printf("%sINP_MTUDISC", comma ? ", " : "");
2286 if (inp_flags & INP_FAITH) {
2287 db_printf("%sINP_FAITH", comma ? ", " : "");
2290 if (inp_flags & INP_RECVTTL) {
2291 db_printf("%sINP_RECVTTL", comma ? ", " : "");
2294 if (inp_flags & INP_DONTFRAG) {
2295 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2298 if (inp_flags & IN6P_IPV6_V6ONLY) {
2299 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2302 if (inp_flags & IN6P_PKTINFO) {
2303 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2306 if (inp_flags & IN6P_HOPLIMIT) {
2307 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2310 if (inp_flags & IN6P_HOPOPTS) {
2311 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2314 if (inp_flags & IN6P_DSTOPTS) {
2315 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2318 if (inp_flags & IN6P_RTHDR) {
2319 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2322 if (inp_flags & IN6P_RTHDRDSTOPTS) {
2323 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2326 if (inp_flags & IN6P_TCLASS) {
2327 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2330 if (inp_flags & IN6P_AUTOFLOWLABEL) {
2331 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2334 if (inp_flags & INP_TIMEWAIT) {
2335 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2338 if (inp_flags & INP_ONESBCAST) {
2339 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2342 if (inp_flags & INP_DROPPED) {
2343 db_printf("%sINP_DROPPED", comma ? ", " : "");
2346 if (inp_flags & INP_SOCKREF) {
2347 db_printf("%sINP_SOCKREF", comma ? ", " : "");
2350 if (inp_flags & IN6P_RFC2292) {
2351 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2354 if (inp_flags & IN6P_MTU) {
2355 db_printf("IN6P_MTU%s", comma ? ", " : "");
2361 db_print_inpvflag(u_char inp_vflag)
2366 if (inp_vflag & INP_IPV4) {
2367 db_printf("%sINP_IPV4", comma ? ", " : "");
2370 if (inp_vflag & INP_IPV6) {
2371 db_printf("%sINP_IPV6", comma ? ", " : "");
2374 if (inp_vflag & INP_IPV6PROTO) {
2375 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2381 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2384 db_print_indent(indent);
2385 db_printf("%s at %p\n", name, inp);
2389 db_print_indent(indent);
2390 db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2392 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2394 db_print_indent(indent);
2395 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n",
2396 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2398 db_print_indent(indent);
2399 db_printf("inp_label: %p inp_flags: 0x%x (",
2400 inp->inp_label, inp->inp_flags);
2401 db_print_inpflags(inp->inp_flags);
2404 db_print_indent(indent);
2405 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp,
2407 db_print_inpvflag(inp->inp_vflag);
2410 db_print_indent(indent);
2411 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n",
2412 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2414 db_print_indent(indent);
2416 if (inp->inp_vflag & INP_IPV6) {
2417 db_printf("in6p_options: %p in6p_outputopts: %p "
2418 "in6p_moptions: %p\n", inp->in6p_options,
2419 inp->in6p_outputopts, inp->in6p_moptions);
2420 db_printf("in6p_icmp6filt: %p in6p_cksum %d "
2421 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2426 db_printf("inp_ip_tos: %d inp_ip_options: %p "
2427 "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2428 inp->inp_options, inp->inp_moptions);
2431 db_print_indent(indent);
2432 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd,
2433 (uintmax_t)inp->inp_gencnt);
2436 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2441 db_printf("usage: show inpcb <addr>\n");
2444 inp = (struct inpcb *)addr;
2446 db_print_inpcb(inp, "inpcb", 0);