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 static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
168 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
169 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0,
170 &sysctl_net_ipport_check, "I", "");
171 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
172 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0,
173 &sysctl_net_ipport_check, "I", "");
174 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first,
175 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0,
176 &sysctl_net_ipport_check, "I", "");
177 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last,
178 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0,
179 &sysctl_net_ipport_check, "I", "");
180 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
181 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0,
182 &sysctl_net_ipport_check, "I", "");
183 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
184 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0,
185 &sysctl_net_ipport_check, "I", "");
186 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
187 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, "");
188 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
189 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
190 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
191 &VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
192 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
193 &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
194 "allocations before switching to a sequental one");
195 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
196 &VNET_NAME(ipport_randomtime), 0,
197 "Minimum time to keep sequental port "
198 "allocation before switching to a random one");
202 * in_pcb.c: manage the Protocol Control Blocks.
204 * NOTE: It is assumed that most of these functions will be called with
205 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
206 * functions often modify hash chains or addresses in pcbs.
210 * Initialize an inpcbinfo -- we should be able to reduce the number of
214 in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
215 struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
216 char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
217 uint32_t inpcbzone_flags, u_int hashfields)
220 INP_INFO_LOCK_INIT(pcbinfo, name);
221 INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */
223 pcbinfo->ipi_vnet = curvnet;
225 pcbinfo->ipi_listhead = listhead;
226 LIST_INIT(pcbinfo->ipi_listhead);
227 pcbinfo->ipi_count = 0;
228 pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
229 &pcbinfo->ipi_hashmask);
230 pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
231 &pcbinfo->ipi_porthashmask);
233 in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
235 pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
236 NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
238 uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
242 * Destroy an inpcbinfo.
245 in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
248 KASSERT(pcbinfo->ipi_count == 0,
249 ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
251 hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
252 hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
253 pcbinfo->ipi_porthashmask);
255 in_pcbgroup_destroy(pcbinfo);
257 uma_zdestroy(pcbinfo->ipi_zone);
258 INP_HASH_LOCK_DESTROY(pcbinfo);
259 INP_INFO_LOCK_DESTROY(pcbinfo);
263 * Allocate a PCB and associate it with the socket.
264 * On success return with the PCB locked.
267 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
272 INP_INFO_WLOCK_ASSERT(pcbinfo);
274 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
277 bzero(inp, inp_zero_size);
278 inp->inp_pcbinfo = pcbinfo;
279 inp->inp_socket = so;
280 inp->inp_cred = crhold(so->so_cred);
281 inp->inp_inc.inc_fibnum = so->so_fibnum;
283 error = mac_inpcb_init(inp, M_NOWAIT);
286 mac_inpcb_create(so, inp);
289 error = ipsec_init_policy(so, &inp->inp_sp);
292 mac_inpcb_destroy(inp);
298 if (INP_SOCKAF(so) == AF_INET6) {
299 inp->inp_vflag |= INP_IPV6PROTO;
301 inp->inp_flags |= IN6P_IPV6_V6ONLY;
304 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
305 pcbinfo->ipi_count++;
306 so->so_pcb = (caddr_t)inp;
308 if (V_ip6_auto_flowlabel)
309 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
312 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
313 refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */
314 #if defined(IPSEC) || defined(MAC)
317 crfree(inp->inp_cred);
318 uma_zfree(pcbinfo->ipi_zone, inp);
326 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
330 INP_WLOCK_ASSERT(inp);
331 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
333 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
335 anonport = inp->inp_lport == 0 && (nam == NULL ||
336 ((struct sockaddr_in *)nam)->sin_port == 0);
337 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
338 &inp->inp_lport, cred);
341 if (in_pcbinshash(inp) != 0) {
342 inp->inp_laddr.s_addr = INADDR_ANY;
347 inp->inp_flags |= INP_ANONPORT;
352 #if defined(INET) || defined(INET6)
354 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
355 struct ucred *cred, int lookupflags)
357 struct inpcbinfo *pcbinfo;
358 struct inpcb *tmpinp;
359 unsigned short *lastport;
360 int count, dorandom, error;
361 u_short aux, first, last, lport;
363 struct in_addr laddr;
366 pcbinfo = inp->inp_pcbinfo;
369 * Because no actual state changes occur here, a global write lock on
370 * the pcbinfo isn't required.
372 INP_LOCK_ASSERT(inp);
373 INP_HASH_LOCK_ASSERT(pcbinfo);
375 if (inp->inp_flags & INP_HIGHPORT) {
376 first = V_ipport_hifirstauto; /* sysctl */
377 last = V_ipport_hilastauto;
378 lastport = &pcbinfo->ipi_lasthi;
379 } else if (inp->inp_flags & INP_LOWPORT) {
380 error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
383 first = V_ipport_lowfirstauto; /* 1023 */
384 last = V_ipport_lowlastauto; /* 600 */
385 lastport = &pcbinfo->ipi_lastlow;
387 first = V_ipport_firstauto; /* sysctl */
388 last = V_ipport_lastauto;
389 lastport = &pcbinfo->ipi_lastport;
392 * For UDP, use random port allocation as long as the user
393 * allows it. For TCP (and as of yet unknown) connections,
394 * use random port allocation only if the user allows it AND
395 * ipport_tick() allows it.
397 if (V_ipport_randomized &&
398 (!V_ipport_stoprandom || pcbinfo == &V_udbinfo))
403 * It makes no sense to do random port allocation if
404 * we have the only port available.
408 /* Make sure to not include UDP packets in the count. */
409 if (pcbinfo != &V_udbinfo)
410 V_ipport_tcpallocs++;
412 * Instead of having two loops further down counting up or down
413 * make sure that first is always <= last and go with only one
414 * code path implementing all logic.
423 /* Make the compiler happy. */
425 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
426 KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
431 tmpinp = NULL; /* Make compiler happy. */
435 *lastport = first + (arc4random() % (last - first));
437 count = last - first;
440 if (count-- < 0) /* completely used? */
441 return (EADDRNOTAVAIL);
443 if (*lastport < first || *lastport > last)
445 lport = htons(*lastport);
448 if ((inp->inp_vflag & INP_IPV6) != 0)
449 tmpinp = in6_pcblookup_local(pcbinfo,
450 &inp->in6p_laddr, lport, lookupflags, cred);
452 #if defined(INET) && defined(INET6)
456 tmpinp = in_pcblookup_local(pcbinfo, laddr,
457 lport, lookupflags, cred);
459 } while (tmpinp != NULL);
462 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
463 laddrp->s_addr = laddr.s_addr;
469 #endif /* INET || INET6 */
473 * Set up a bind operation on a PCB, performing port allocation
474 * as required, but do not actually modify the PCB. Callers can
475 * either complete the bind by setting inp_laddr/inp_lport and
476 * calling in_pcbinshash(), or they can just use the resulting
477 * port and address to authorise the sending of a once-off packet.
479 * On error, the values of *laddrp and *lportp are not changed.
482 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
483 u_short *lportp, struct ucred *cred)
485 struct socket *so = inp->inp_socket;
486 struct sockaddr_in *sin;
487 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
488 struct in_addr laddr;
490 int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
494 * No state changes, so read locks are sufficient here.
496 INP_LOCK_ASSERT(inp);
497 INP_HASH_LOCK_ASSERT(pcbinfo);
499 if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
500 return (EADDRNOTAVAIL);
501 laddr.s_addr = *laddrp;
502 if (nam != NULL && laddr.s_addr != INADDR_ANY)
504 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
505 lookupflags = INPLOOKUP_WILDCARD;
507 if ((error = prison_local_ip4(cred, &laddr)) != 0)
510 sin = (struct sockaddr_in *)nam;
511 if (nam->sa_len != sizeof (*sin))
515 * We should check the family, but old programs
516 * incorrectly fail to initialize it.
518 if (sin->sin_family != AF_INET)
519 return (EAFNOSUPPORT);
521 error = prison_local_ip4(cred, &sin->sin_addr);
524 if (sin->sin_port != *lportp) {
525 /* Don't allow the port to change. */
528 lport = sin->sin_port;
530 /* NB: lport is left as 0 if the port isn't being changed. */
531 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
533 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
534 * allow complete duplication of binding if
535 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
536 * and a multicast address is bound on both
537 * new and duplicated sockets.
539 if (so->so_options & SO_REUSEADDR)
540 reuseport = SO_REUSEADDR|SO_REUSEPORT;
541 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
542 sin->sin_port = 0; /* yech... */
543 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
545 * Is the address a local IP address?
546 * If INP_BINDANY is set, then the socket may be bound
547 * to any endpoint address, local or not.
549 if ((inp->inp_flags & INP_BINDANY) == 0 &&
550 ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
551 return (EADDRNOTAVAIL);
553 laddr = sin->sin_addr;
559 if (ntohs(lport) <= V_ipport_reservedhigh &&
560 ntohs(lport) >= V_ipport_reservedlow &&
561 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
564 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
565 priv_check_cred(inp->inp_cred,
566 PRIV_NETINET_REUSEPORT, 0) != 0) {
567 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
568 lport, INPLOOKUP_WILDCARD, cred);
571 * This entire block sorely needs a rewrite.
574 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
575 (so->so_type != SOCK_STREAM ||
576 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
577 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
578 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
579 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
580 (inp->inp_cred->cr_uid !=
581 t->inp_cred->cr_uid))
584 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
585 lport, lookupflags, cred);
586 if (t && (t->inp_flags & INP_TIMEWAIT)) {
588 * XXXRW: If an incpb has had its timewait
589 * state recycled, we treat the address as
590 * being in use (for now). This is better
591 * than a panic, but not desirable.
595 (reuseport & tw->tw_so_options) == 0)
597 } else if (t && (reuseport == 0 ||
598 (t->inp_flags2 & INP_REUSEPORT) == 0)) {
600 if (ntohl(sin->sin_addr.s_addr) !=
602 ntohl(t->inp_laddr.s_addr) !=
604 (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
605 (t->inp_vflag & INP_IPV6PROTO) == 0)
614 error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
619 *laddrp = laddr.s_addr;
625 * Connect from a socket to a specified address.
626 * Both address and port must be specified in argument sin.
627 * If don't have a local address for this socket yet,
631 in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
632 struct ucred *cred, struct mbuf *m)
634 u_short lport, fport;
635 in_addr_t laddr, faddr;
638 INP_WLOCK_ASSERT(inp);
639 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
641 lport = inp->inp_lport;
642 laddr = inp->inp_laddr.s_addr;
643 anonport = (lport == 0);
644 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
649 /* Do the initial binding of the local address if required. */
650 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
651 inp->inp_lport = lport;
652 inp->inp_laddr.s_addr = laddr;
653 if (in_pcbinshash(inp) != 0) {
654 inp->inp_laddr.s_addr = INADDR_ANY;
660 /* Commit the remaining changes. */
661 inp->inp_lport = lport;
662 inp->inp_laddr.s_addr = laddr;
663 inp->inp_faddr.s_addr = faddr;
664 inp->inp_fport = fport;
665 in_pcbrehash_mbuf(inp, m);
668 inp->inp_flags |= INP_ANONPORT;
673 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
676 return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
680 * Do proper source address selection on an unbound socket in case
681 * of connect. Take jails into account as well.
684 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
689 struct sockaddr_in *sin;
693 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
696 * Bypass source address selection and use the primary jail IP
699 if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
703 bzero(&sro, sizeof(sro));
705 sin = (struct sockaddr_in *)&sro.ro_dst;
706 sin->sin_family = AF_INET;
707 sin->sin_len = sizeof(struct sockaddr_in);
708 sin->sin_addr.s_addr = faddr->s_addr;
711 * If route is known our src addr is taken from the i/f,
714 * Find out route to destination.
716 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
717 in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
720 * If we found a route, use the address corresponding to
721 * the outgoing interface.
723 * Otherwise assume faddr is reachable on a directly connected
724 * network and try to find a corresponding interface to take
725 * the source address from.
727 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
728 struct in_ifaddr *ia;
731 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
733 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0));
739 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
740 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
741 ifa_free(&ia->ia_ifa);
746 ifa_free(&ia->ia_ifa);
749 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
752 if (sa->sa_family != AF_INET)
754 sin = (struct sockaddr_in *)sa;
755 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
756 ia = (struct in_ifaddr *)ifa;
761 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
762 IF_ADDR_RUNLOCK(ifp);
765 IF_ADDR_RUNLOCK(ifp);
767 /* 3. As a last resort return the 'default' jail address. */
768 error = prison_get_ip4(cred, laddr);
773 * If the outgoing interface on the route found is not
774 * a loopback interface, use the address from that interface.
775 * In case of jails do those three steps:
776 * 1. check if the interface address belongs to the jail. If so use it.
777 * 2. check if we have any address on the outgoing interface
778 * belonging to this jail. If so use it.
779 * 3. as a last resort return the 'default' jail address.
781 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
782 struct in_ifaddr *ia;
785 /* If not jailed, use the default returned. */
786 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
787 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
788 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
793 /* 1. Check if the iface address belongs to the jail. */
794 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
795 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
796 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
797 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
802 * 2. Check if we have any address on the outgoing interface
803 * belonging to this jail.
806 ifp = sro.ro_rt->rt_ifp;
808 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
810 if (sa->sa_family != AF_INET)
812 sin = (struct sockaddr_in *)sa;
813 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
814 ia = (struct in_ifaddr *)ifa;
819 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
820 IF_ADDR_RUNLOCK(ifp);
823 IF_ADDR_RUNLOCK(ifp);
825 /* 3. As a last resort return the 'default' jail address. */
826 error = prison_get_ip4(cred, laddr);
831 * The outgoing interface is marked with 'loopback net', so a route
832 * to ourselves is here.
833 * Try to find the interface of the destination address and then
834 * take the address from there. That interface is not necessarily
835 * a loopback interface.
836 * In case of jails, check that it is an address of the jail
837 * and if we cannot find, fall back to the 'default' jail address.
839 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
840 struct sockaddr_in sain;
841 struct in_ifaddr *ia;
843 bzero(&sain, sizeof(struct sockaddr_in));
844 sain.sin_family = AF_INET;
845 sain.sin_len = sizeof(struct sockaddr_in);
846 sain.sin_addr.s_addr = faddr->s_addr;
848 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
850 ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0));
852 ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
854 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
859 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
860 ifa_free(&ia->ia_ifa);
869 ifa_free(&ia->ia_ifa);
872 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
875 if (sa->sa_family != AF_INET)
877 sin = (struct sockaddr_in *)sa;
878 if (prison_check_ip4(cred,
879 &sin->sin_addr) == 0) {
880 ia = (struct in_ifaddr *)ifa;
885 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
886 IF_ADDR_RUNLOCK(ifp);
889 IF_ADDR_RUNLOCK(ifp);
892 /* 3. As a last resort return the 'default' jail address. */
893 error = prison_get_ip4(cred, laddr);
898 if (sro.ro_rt != NULL)
904 * Set up for a connect from a socket to the specified address.
905 * On entry, *laddrp and *lportp should contain the current local
906 * address and port for the PCB; these are updated to the values
907 * that should be placed in inp_laddr and inp_lport to complete
910 * On success, *faddrp and *fportp will be set to the remote address
911 * and port. These are not updated in the error case.
913 * If the operation fails because the connection already exists,
914 * *oinpp will be set to the PCB of that connection so that the
915 * caller can decide to override it. In all other cases, *oinpp
919 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
920 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
921 struct inpcb **oinpp, struct ucred *cred)
923 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
924 struct in_ifaddr *ia;
926 struct in_addr laddr, faddr;
927 u_short lport, fport;
931 * Because a global state change doesn't actually occur here, a read
932 * lock is sufficient.
934 INP_LOCK_ASSERT(inp);
935 INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
939 if (nam->sa_len != sizeof (*sin))
941 if (sin->sin_family != AF_INET)
942 return (EAFNOSUPPORT);
943 if (sin->sin_port == 0)
944 return (EADDRNOTAVAIL);
945 laddr.s_addr = *laddrp;
947 faddr = sin->sin_addr;
948 fport = sin->sin_port;
950 if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
952 * If the destination address is INADDR_ANY,
953 * use the primary local address.
954 * If the supplied address is INADDR_BROADCAST,
955 * and the primary interface supports broadcast,
956 * choose the broadcast address for that interface.
958 if (faddr.s_addr == INADDR_ANY) {
961 IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
964 (error = prison_get_ip4(cred, &faddr)) != 0)
966 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
968 if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
970 faddr = satosin(&TAILQ_FIRST(
971 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
975 if (laddr.s_addr == INADDR_ANY) {
976 error = in_pcbladdr(inp, &faddr, &laddr, cred);
978 * If the destination address is multicast and an outgoing
979 * interface has been set as a multicast option, prefer the
980 * address of that interface as our source address.
982 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
983 inp->inp_moptions != NULL) {
984 struct ip_moptions *imo;
987 imo = inp->inp_moptions;
988 if (imo->imo_multicast_ifp != NULL) {
989 ifp = imo->imo_multicast_ifp;
991 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
992 if ((ia->ia_ifp == ifp) &&
994 prison_check_ip4(cred,
995 &ia->ia_addr.sin_addr) == 0))
999 error = EADDRNOTAVAIL;
1001 laddr = ia->ia_addr.sin_addr;
1004 IN_IFADDR_RUNLOCK();
1010 oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1011 laddr, lport, 0, NULL);
1015 return (EADDRINUSE);
1018 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1023 *laddrp = laddr.s_addr;
1025 *faddrp = faddr.s_addr;
1031 in_pcbdisconnect(struct inpcb *inp)
1034 INP_WLOCK_ASSERT(inp);
1035 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1037 inp->inp_faddr.s_addr = INADDR_ANY;
1044 * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1045 * For most protocols, this will be invoked immediately prior to calling
1046 * in_pcbfree(). However, with TCP the inpcb may significantly outlive the
1047 * socket, in which case in_pcbfree() is deferred.
1050 in_pcbdetach(struct inpcb *inp)
1053 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1055 inp->inp_socket->so_pcb = NULL;
1056 inp->inp_socket = NULL;
1060 * in_pcbref() bumps the reference count on an inpcb in order to maintain
1061 * stability of an inpcb pointer despite the inpcb lock being released. This
1062 * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
1063 * but where the inpcb lock may already held, or when acquiring a reference
1066 * in_pcbref() should be used only to provide brief memory stability, and
1067 * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
1068 * garbage collect the inpcb if it has been in_pcbfree()'d from another
1069 * context. Until in_pcbrele() has returned that the inpcb is still valid,
1070 * lock and rele are the *only* safe operations that may be performed on the
1073 * While the inpcb will not be freed, releasing the inpcb lock means that the
1074 * connection's state may change, so the caller should be careful to
1075 * revalidate any cached state on reacquiring the lock. Drop the reference
1076 * using in_pcbrele().
1079 in_pcbref(struct inpcb *inp)
1082 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1084 refcount_acquire(&inp->inp_refcount);
1088 * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
1089 * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
1090 * return a flag indicating whether or not the inpcb remains valid. If it is
1091 * valid, we return with the inpcb lock held.
1093 * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
1094 * reference on an inpcb. Historically more work was done here (actually, in
1095 * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
1096 * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely
1097 * about memory stability (and continued use of the write lock).
1100 in_pcbrele_rlocked(struct inpcb *inp)
1102 struct inpcbinfo *pcbinfo;
1104 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1106 INP_RLOCK_ASSERT(inp);
1108 if (refcount_release(&inp->inp_refcount) == 0)
1111 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1114 pcbinfo = inp->inp_pcbinfo;
1115 uma_zfree(pcbinfo->ipi_zone, inp);
1120 in_pcbrele_wlocked(struct inpcb *inp)
1122 struct inpcbinfo *pcbinfo;
1124 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1126 INP_WLOCK_ASSERT(inp);
1128 if (refcount_release(&inp->inp_refcount) == 0)
1131 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1134 pcbinfo = inp->inp_pcbinfo;
1135 uma_zfree(pcbinfo->ipi_zone, inp);
1140 * Temporary wrapper.
1143 in_pcbrele(struct inpcb *inp)
1146 return (in_pcbrele_wlocked(inp));
1150 * Unconditionally schedule an inpcb to be freed by decrementing its
1151 * reference count, which should occur only after the inpcb has been detached
1152 * from its socket. If another thread holds a temporary reference (acquired
1153 * using in_pcbref()) then the free is deferred until that reference is
1154 * released using in_pcbrele(), but the inpcb is still unlocked. Almost all
1155 * work, including removal from global lists, is done in this context, where
1156 * the pcbinfo lock is held.
1159 in_pcbfree(struct inpcb *inp)
1161 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1163 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1165 INP_INFO_WLOCK_ASSERT(pcbinfo);
1166 INP_WLOCK_ASSERT(inp);
1168 /* XXXRW: Do as much as possible here. */
1170 if (inp->inp_sp != NULL)
1171 ipsec_delete_pcbpolicy(inp);
1173 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1174 in_pcbremlists(inp);
1176 if (inp->inp_vflag & INP_IPV6PROTO) {
1177 ip6_freepcbopts(inp->in6p_outputopts);
1178 if (inp->in6p_moptions != NULL)
1179 ip6_freemoptions(inp->in6p_moptions);
1182 if (inp->inp_options)
1183 (void)m_free(inp->inp_options);
1185 if (inp->inp_moptions != NULL)
1186 inp_freemoptions(inp->inp_moptions);
1189 crfree(inp->inp_cred);
1191 mac_inpcb_destroy(inp);
1193 if (!in_pcbrele_wlocked(inp))
1198 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1199 * port reservation, and preventing it from being returned by inpcb lookups.
1201 * It is used by TCP to mark an inpcb as unused and avoid future packet
1202 * delivery or event notification when a socket remains open but TCP has
1203 * closed. This might occur as a result of a shutdown()-initiated TCP close
1204 * or a RST on the wire, and allows the port binding to be reused while still
1205 * maintaining the invariant that so_pcb always points to a valid inpcb until
1208 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1209 * in_pcbnotifyall() and in_pcbpurgeif0()?
1212 in_pcbdrop(struct inpcb *inp)
1215 INP_WLOCK_ASSERT(inp);
1218 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1221 inp->inp_flags |= INP_DROPPED;
1222 if (inp->inp_flags & INP_INHASHLIST) {
1223 struct inpcbport *phd = inp->inp_phd;
1225 INP_HASH_WLOCK(inp->inp_pcbinfo);
1226 LIST_REMOVE(inp, inp_hash);
1227 LIST_REMOVE(inp, inp_portlist);
1228 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1229 LIST_REMOVE(phd, phd_hash);
1232 INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1233 inp->inp_flags &= ~INP_INHASHLIST;
1235 in_pcbgroup_remove(inp);
1242 * Common routines to return the socket addresses associated with inpcbs.
1245 in_sockaddr(in_port_t port, struct in_addr *addr_p)
1247 struct sockaddr_in *sin;
1249 sin = malloc(sizeof *sin, M_SONAME,
1251 sin->sin_family = AF_INET;
1252 sin->sin_len = sizeof(*sin);
1253 sin->sin_addr = *addr_p;
1254 sin->sin_port = port;
1256 return (struct sockaddr *)sin;
1260 in_getsockaddr(struct socket *so, struct sockaddr **nam)
1263 struct in_addr addr;
1266 inp = sotoinpcb(so);
1267 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1270 port = inp->inp_lport;
1271 addr = inp->inp_laddr;
1274 *nam = in_sockaddr(port, &addr);
1279 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1282 struct in_addr addr;
1285 inp = sotoinpcb(so);
1286 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1289 port = inp->inp_fport;
1290 addr = inp->inp_faddr;
1293 *nam = in_sockaddr(port, &addr);
1298 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1299 struct inpcb *(*notify)(struct inpcb *, int))
1301 struct inpcb *inp, *inp_temp;
1303 INP_INFO_WLOCK(pcbinfo);
1304 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1307 if ((inp->inp_vflag & INP_IPV4) == 0) {
1312 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1313 inp->inp_socket == NULL) {
1317 if ((*notify)(inp, errno))
1320 INP_INFO_WUNLOCK(pcbinfo);
1324 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1327 struct ip_moptions *imo;
1330 INP_INFO_RLOCK(pcbinfo);
1331 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1333 imo = inp->inp_moptions;
1334 if ((inp->inp_vflag & INP_IPV4) &&
1337 * Unselect the outgoing interface if it is being
1340 if (imo->imo_multicast_ifp == ifp)
1341 imo->imo_multicast_ifp = NULL;
1344 * Drop multicast group membership if we joined
1345 * through the interface being detached.
1347 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1349 if (imo->imo_membership[i]->inm_ifp == ifp) {
1350 in_delmulti(imo->imo_membership[i]);
1352 } else if (gap != 0)
1353 imo->imo_membership[i - gap] =
1354 imo->imo_membership[i];
1356 imo->imo_num_memberships -= gap;
1360 INP_INFO_RUNLOCK(pcbinfo);
1364 * Lookup a PCB based on the local address and port. Caller must hold the
1365 * hash lock. No inpcb locks or references are acquired.
1367 #define INP_LOOKUP_MAPPED_PCB_COST 3
1369 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1370 u_short lport, int lookupflags, struct ucred *cred)
1374 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1380 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1381 ("%s: invalid lookup flags %d", __func__, lookupflags));
1383 INP_HASH_LOCK_ASSERT(pcbinfo);
1385 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1386 struct inpcbhead *head;
1388 * Look for an unconnected (wildcard foreign addr) PCB that
1389 * matches the local address and port we're looking for.
1391 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1392 0, pcbinfo->ipi_hashmask)];
1393 LIST_FOREACH(inp, head, inp_hash) {
1395 /* XXX inp locking */
1396 if ((inp->inp_vflag & INP_IPV4) == 0)
1399 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1400 inp->inp_laddr.s_addr == laddr.s_addr &&
1401 inp->inp_lport == lport) {
1406 prison_equal_ip4(cred->cr_prison,
1407 inp->inp_cred->cr_prison))
1416 struct inpcbporthead *porthash;
1417 struct inpcbport *phd;
1418 struct inpcb *match = NULL;
1420 * Best fit PCB lookup.
1422 * First see if this local port is in use by looking on the
1425 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1426 pcbinfo->ipi_porthashmask)];
1427 LIST_FOREACH(phd, porthash, phd_hash) {
1428 if (phd->phd_port == lport)
1433 * Port is in use by one or more PCBs. Look for best
1436 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1439 !prison_equal_ip4(inp->inp_cred->cr_prison,
1443 /* XXX inp locking */
1444 if ((inp->inp_vflag & INP_IPV4) == 0)
1447 * We never select the PCB that has
1448 * INP_IPV6 flag and is bound to :: if
1449 * we have another PCB which is bound
1450 * to 0.0.0.0. If a PCB has the
1451 * INP_IPV6 flag, then we set its cost
1452 * higher than IPv4 only PCBs.
1454 * Note that the case only happens
1455 * when a socket is bound to ::, under
1456 * the condition that the use of the
1457 * mapped address is allowed.
1459 if ((inp->inp_vflag & INP_IPV6) != 0)
1460 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1462 if (inp->inp_faddr.s_addr != INADDR_ANY)
1464 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1465 if (laddr.s_addr == INADDR_ANY)
1467 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1470 if (laddr.s_addr != INADDR_ANY)
1473 if (wildcard < matchwild) {
1475 matchwild = wildcard;
1484 #undef INP_LOOKUP_MAPPED_PCB_COST
1488 * Lookup PCB in hash list, using pcbgroup tables.
1490 static struct inpcb *
1491 in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
1492 struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
1493 u_int lport_arg, int lookupflags, struct ifnet *ifp)
1495 struct inpcbhead *head;
1496 struct inpcb *inp, *tmpinp;
1497 u_short fport = fport_arg, lport = lport_arg;
1500 * First look for an exact match.
1503 INP_GROUP_LOCK(pcbgroup);
1504 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1505 pcbgroup->ipg_hashmask)];
1506 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1508 /* XXX inp locking */
1509 if ((inp->inp_vflag & INP_IPV4) == 0)
1512 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1513 inp->inp_laddr.s_addr == laddr.s_addr &&
1514 inp->inp_fport == fport &&
1515 inp->inp_lport == lport) {
1517 * XXX We should be able to directly return
1518 * the inp here, without any checks.
1519 * Well unless both bound with SO_REUSEPORT?
1521 if (prison_flag(inp->inp_cred, PR_IP4))
1527 if (tmpinp != NULL) {
1533 * Then look for a wildcard match, if requested.
1535 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1536 struct inpcb *local_wild = NULL, *local_exact = NULL;
1538 struct inpcb *local_wild_mapped = NULL;
1540 struct inpcb *jail_wild = NULL;
1541 struct inpcbhead *head;
1545 * Order of socket selection - we always prefer jails.
1546 * 1. jailed, non-wild.
1548 * 3. non-jailed, non-wild.
1549 * 4. non-jailed, wild.
1551 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
1552 0, pcbinfo->ipi_wildmask)];
1553 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1555 /* XXX inp locking */
1556 if ((inp->inp_vflag & INP_IPV4) == 0)
1559 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1560 inp->inp_lport != lport)
1563 /* XXX inp locking */
1564 if (ifp && ifp->if_type == IFT_FAITH &&
1565 (inp->inp_flags & INP_FAITH) == 0)
1568 injail = prison_flag(inp->inp_cred, PR_IP4);
1570 if (prison_check_ip4(inp->inp_cred,
1574 if (local_exact != NULL)
1578 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1583 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1585 /* XXX inp locking, NULL check */
1586 if (inp->inp_vflag & INP_IPV6PROTO)
1587 local_wild_mapped = inp;
1595 } /* LIST_FOREACH */
1603 inp = local_wild_mapped;
1607 } /* if (lookupflags & INPLOOKUP_WILDCARD) */
1608 INP_GROUP_UNLOCK(pcbgroup);
1613 INP_GROUP_UNLOCK(pcbgroup);
1614 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1616 if (in_pcbrele_wlocked(inp))
1618 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1620 if (in_pcbrele_rlocked(inp))
1623 panic("%s: locking bug", __func__);
1626 #endif /* PCBGROUP */
1629 * Lookup PCB in hash list, using pcbinfo tables. This variation assumes
1630 * that the caller has locked the hash list, and will not perform any further
1631 * locking or reference operations on either the hash list or the connection.
1633 static struct inpcb *
1634 in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1635 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1638 struct inpcbhead *head;
1639 struct inpcb *inp, *tmpinp;
1640 u_short fport = fport_arg, lport = lport_arg;
1642 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1643 ("%s: invalid lookup flags %d", __func__, lookupflags));
1645 INP_HASH_LOCK_ASSERT(pcbinfo);
1648 * First look for an exact match.
1651 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1652 pcbinfo->ipi_hashmask)];
1653 LIST_FOREACH(inp, head, inp_hash) {
1655 /* XXX inp locking */
1656 if ((inp->inp_vflag & INP_IPV4) == 0)
1659 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1660 inp->inp_laddr.s_addr == laddr.s_addr &&
1661 inp->inp_fport == fport &&
1662 inp->inp_lport == lport) {
1664 * XXX We should be able to directly return
1665 * the inp here, without any checks.
1666 * Well unless both bound with SO_REUSEPORT?
1668 if (prison_flag(inp->inp_cred, PR_IP4))
1678 * Then look for a wildcard match, if requested.
1680 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1681 struct inpcb *local_wild = NULL, *local_exact = NULL;
1683 struct inpcb *local_wild_mapped = NULL;
1685 struct inpcb *jail_wild = NULL;
1689 * Order of socket selection - we always prefer jails.
1690 * 1. jailed, non-wild.
1692 * 3. non-jailed, non-wild.
1693 * 4. non-jailed, wild.
1696 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1697 0, pcbinfo->ipi_hashmask)];
1698 LIST_FOREACH(inp, head, inp_hash) {
1700 /* XXX inp locking */
1701 if ((inp->inp_vflag & INP_IPV4) == 0)
1704 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1705 inp->inp_lport != lport)
1708 /* XXX inp locking */
1709 if (ifp && ifp->if_type == IFT_FAITH &&
1710 (inp->inp_flags & INP_FAITH) == 0)
1713 injail = prison_flag(inp->inp_cred, PR_IP4);
1715 if (prison_check_ip4(inp->inp_cred,
1719 if (local_exact != NULL)
1723 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1728 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1730 /* XXX inp locking, NULL check */
1731 if (inp->inp_vflag & INP_IPV6PROTO)
1732 local_wild_mapped = inp;
1740 } /* LIST_FOREACH */
1741 if (jail_wild != NULL)
1743 if (local_exact != NULL)
1744 return (local_exact);
1745 if (local_wild != NULL)
1746 return (local_wild);
1748 if (local_wild_mapped != NULL)
1749 return (local_wild_mapped);
1751 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1757 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1758 * hash list lock, and will return the inpcb locked (i.e., requires
1759 * INPLOOKUP_LOCKPCB).
1761 static struct inpcb *
1762 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1763 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1768 INP_HASH_RLOCK(pcbinfo);
1769 inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1770 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1773 INP_HASH_RUNLOCK(pcbinfo);
1774 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1776 if (in_pcbrele_wlocked(inp))
1778 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1780 if (in_pcbrele_rlocked(inp))
1783 panic("%s: locking bug", __func__);
1785 INP_HASH_RUNLOCK(pcbinfo);
1790 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1791 * from which a pre-calculated hash value may be extracted.
1793 * Possibly more of this logic should be in in_pcbgroup.c.
1796 in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1797 struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1799 #if defined(PCBGROUP)
1800 struct inpcbgroup *pcbgroup;
1803 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1804 ("%s: invalid lookup flags %d", __func__, lookupflags));
1805 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1806 ("%s: LOCKPCB not set", __func__));
1808 #if defined(PCBGROUP)
1809 if (in_pcbgroup_enabled(pcbinfo)) {
1810 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1812 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1813 laddr, lport, lookupflags, ifp));
1816 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1821 in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1822 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1823 struct ifnet *ifp, struct mbuf *m)
1826 struct inpcbgroup *pcbgroup;
1829 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1830 ("%s: invalid lookup flags %d", __func__, lookupflags));
1831 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1832 ("%s: LOCKPCB not set", __func__));
1835 if (in_pcbgroup_enabled(pcbinfo)) {
1836 pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1837 m->m_pkthdr.flowid);
1838 if (pcbgroup != NULL)
1839 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
1840 fport, laddr, lport, lookupflags, ifp));
1841 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1843 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1844 laddr, lport, lookupflags, ifp));
1847 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1853 * Insert PCB onto various hash lists.
1856 in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
1858 struct inpcbhead *pcbhash;
1859 struct inpcbporthead *pcbporthash;
1860 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1861 struct inpcbport *phd;
1862 u_int32_t hashkey_faddr;
1864 INP_WLOCK_ASSERT(inp);
1865 INP_HASH_WLOCK_ASSERT(pcbinfo);
1867 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1868 ("in_pcbinshash: INP_INHASHLIST"));
1871 if (inp->inp_vflag & INP_IPV6)
1872 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1875 hashkey_faddr = inp->inp_faddr.s_addr;
1877 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1878 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1880 pcbporthash = &pcbinfo->ipi_porthashbase[
1881 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1884 * Go through port list and look for a head for this lport.
1886 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1887 if (phd->phd_port == inp->inp_lport)
1891 * If none exists, malloc one and tack it on.
1894 phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1896 return (ENOBUFS); /* XXX */
1898 phd->phd_port = inp->inp_lport;
1899 LIST_INIT(&phd->phd_pcblist);
1900 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1903 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1904 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1905 inp->inp_flags |= INP_INHASHLIST;
1907 if (do_pcbgroup_update)
1908 in_pcbgroup_update(inp);
1914 * For now, there are two public interfaces to insert an inpcb into the hash
1915 * lists -- one that does update pcbgroups, and one that doesn't. The latter
1916 * is used only in the TCP syncache, where in_pcbinshash is called before the
1917 * full 4-tuple is set for the inpcb, and we don't want to install in the
1918 * pcbgroup until later.
1920 * XXXRW: This seems like a misfeature. in_pcbinshash should always update
1921 * connection groups, and partially initialised inpcbs should not be exposed
1922 * to either reservation hash tables or pcbgroups.
1925 in_pcbinshash(struct inpcb *inp)
1928 return (in_pcbinshash_internal(inp, 1));
1932 in_pcbinshash_nopcbgroup(struct inpcb *inp)
1935 return (in_pcbinshash_internal(inp, 0));
1939 * Move PCB to the proper hash bucket when { faddr, fport } have been
1940 * changed. NOTE: This does not handle the case of the lport changing (the
1941 * hashed port list would have to be updated as well), so the lport must
1942 * not change after in_pcbinshash() has been called.
1945 in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
1947 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1948 struct inpcbhead *head;
1949 u_int32_t hashkey_faddr;
1951 INP_WLOCK_ASSERT(inp);
1952 INP_HASH_WLOCK_ASSERT(pcbinfo);
1954 KASSERT(inp->inp_flags & INP_INHASHLIST,
1955 ("in_pcbrehash: !INP_INHASHLIST"));
1958 if (inp->inp_vflag & INP_IPV6)
1959 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1962 hashkey_faddr = inp->inp_faddr.s_addr;
1964 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1965 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1967 LIST_REMOVE(inp, inp_hash);
1968 LIST_INSERT_HEAD(head, inp, inp_hash);
1972 in_pcbgroup_update_mbuf(inp, m);
1974 in_pcbgroup_update(inp);
1979 in_pcbrehash(struct inpcb *inp)
1982 in_pcbrehash_mbuf(inp, NULL);
1986 * Remove PCB from various lists.
1989 in_pcbremlists(struct inpcb *inp)
1991 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1993 INP_INFO_WLOCK_ASSERT(pcbinfo);
1994 INP_WLOCK_ASSERT(inp);
1996 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1997 if (inp->inp_flags & INP_INHASHLIST) {
1998 struct inpcbport *phd = inp->inp_phd;
2000 INP_HASH_WLOCK(pcbinfo);
2001 LIST_REMOVE(inp, inp_hash);
2002 LIST_REMOVE(inp, inp_portlist);
2003 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2004 LIST_REMOVE(phd, phd_hash);
2007 INP_HASH_WUNLOCK(pcbinfo);
2008 inp->inp_flags &= ~INP_INHASHLIST;
2010 LIST_REMOVE(inp, inp_list);
2011 pcbinfo->ipi_count--;
2013 in_pcbgroup_remove(inp);
2018 * A set label operation has occurred at the socket layer, propagate the
2019 * label change into the in_pcb for the socket.
2022 in_pcbsosetlabel(struct socket *so)
2027 inp = sotoinpcb(so);
2028 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2032 mac_inpcb_sosetlabel(so, inp);
2039 * ipport_tick runs once per second, determining if random port allocation
2040 * should be continued. If more than ipport_randomcps ports have been
2041 * allocated in the last second, then we return to sequential port
2042 * allocation. We return to random allocation only once we drop below
2043 * ipport_randomcps for at least ipport_randomtime seconds.
2046 ipport_tick(void *xtp)
2048 VNET_ITERATOR_DECL(vnet_iter);
2050 VNET_LIST_RLOCK_NOSLEEP();
2051 VNET_FOREACH(vnet_iter) {
2052 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */
2053 if (V_ipport_tcpallocs <=
2054 V_ipport_tcplastcount + V_ipport_randomcps) {
2055 if (V_ipport_stoprandom > 0)
2056 V_ipport_stoprandom--;
2058 V_ipport_stoprandom = V_ipport_randomtime;
2059 V_ipport_tcplastcount = V_ipport_tcpallocs;
2062 VNET_LIST_RUNLOCK_NOSLEEP();
2063 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
2070 callout_stop(&ipport_tick_callout);
2074 * The ipport_callout should start running at about the time we attach the
2075 * inet or inet6 domains.
2078 ipport_tick_init(const void *unused __unused)
2081 /* Start ipport_tick. */
2082 callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
2083 callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2084 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2085 SHUTDOWN_PRI_DEFAULT);
2087 SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2088 ipport_tick_init, NULL);
2091 inp_wlock(struct inpcb *inp)
2098 inp_wunlock(struct inpcb *inp)
2105 inp_rlock(struct inpcb *inp)
2112 inp_runlock(struct inpcb *inp)
2120 inp_lock_assert(struct inpcb *inp)
2123 INP_WLOCK_ASSERT(inp);
2127 inp_unlock_assert(struct inpcb *inp)
2130 INP_UNLOCK_ASSERT(inp);
2135 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
2139 INP_INFO_RLOCK(&V_tcbinfo);
2140 LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
2145 INP_INFO_RUNLOCK(&V_tcbinfo);
2149 inp_inpcbtosocket(struct inpcb *inp)
2152 INP_WLOCK_ASSERT(inp);
2153 return (inp->inp_socket);
2157 inp_inpcbtotcpcb(struct inpcb *inp)
2160 INP_WLOCK_ASSERT(inp);
2161 return ((struct tcpcb *)inp->inp_ppcb);
2165 inp_ip_tos_get(const struct inpcb *inp)
2168 return (inp->inp_ip_tos);
2172 inp_ip_tos_set(struct inpcb *inp, int val)
2175 inp->inp_ip_tos = val;
2179 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
2180 uint32_t *faddr, uint16_t *fp)
2183 INP_LOCK_ASSERT(inp);
2184 *laddr = inp->inp_laddr.s_addr;
2185 *faddr = inp->inp_faddr.s_addr;
2186 *lp = inp->inp_lport;
2187 *fp = inp->inp_fport;
2191 so_sotoinpcb(struct socket *so)
2194 return (sotoinpcb(so));
2198 so_sototcpcb(struct socket *so)
2201 return (sototcpcb(so));
2206 db_print_indent(int indent)
2210 for (i = 0; i < indent; i++)
2215 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2217 char faddr_str[48], laddr_str[48];
2219 db_print_indent(indent);
2220 db_printf("%s at %p\n", name, inc);
2225 if (inc->inc_flags & INC_ISIPV6) {
2227 ip6_sprintf(laddr_str, &inc->inc6_laddr);
2228 ip6_sprintf(faddr_str, &inc->inc6_faddr);
2233 inet_ntoa_r(inc->inc_laddr, laddr_str);
2234 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);