2 * Copyright (c) 1982, 1986, 1991, 1993, 1995
3 * The Regents of the University of California.
4 * Copyright (c) 2007-2009 Robert N. M. Watson
5 * Copyright (c) 2010-2011 Juniper Networks, Inc.
8 * Portions of this software were developed by Robert N. M. Watson under
9 * contract to Juniper Networks, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 #include "opt_ipsec.h"
44 #include "opt_inet6.h"
45 #include "opt_pcbgroup.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
51 #include <sys/callout.h>
52 #include <sys/domain.h>
53 #include <sys/protosw.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
58 #include <sys/refcount.h>
60 #include <sys/kernel.h>
61 #include <sys/sysctl.h>
70 #include <net/if_types.h>
71 #include <net/route.h>
74 #if defined(INET) || defined(INET6)
75 #include <netinet/in.h>
76 #include <netinet/in_pcb.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/tcp_var.h>
79 #include <netinet/udp.h>
80 #include <netinet/udp_var.h>
83 #include <netinet/in_var.h>
86 #include <netinet/ip6.h>
87 #include <netinet6/in6_pcb.h>
88 #include <netinet6/in6_var.h>
89 #include <netinet6/ip6_var.h>
94 #include <netipsec/ipsec.h>
95 #include <netipsec/key.h>
98 #include <security/mac/mac_framework.h>
100 static struct callout ipport_tick_callout;
103 * These configure the range of local port addresses assigned to
104 * "unspecified" outgoing connections/packets/whatever.
106 VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */
107 VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */
108 VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */
109 VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */
110 VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */
111 VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */
114 * Reserved ports accessible only to root. There are significant
115 * security considerations that must be accounted for when changing these,
116 * but the security benefits can be great. Please be careful.
118 VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */
119 VNET_DEFINE(int, ipport_reservedlow);
121 /* Variables dealing with random ephemeral port allocation. */
122 VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */
123 VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */
124 VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */
125 VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */
126 VNET_DEFINE(int, ipport_tcpallocs);
127 static VNET_DEFINE(int, ipport_tcplastcount);
129 #define V_ipport_tcplastcount VNET(ipport_tcplastcount)
131 static void in_pcbremlists(struct inpcb *inp);
133 static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
134 struct in_addr faddr, u_int fport_arg,
135 struct in_addr laddr, u_int lport_arg,
136 int lookupflags, struct ifnet *ifp);
138 #define RANGECHK(var, min, max) \
139 if ((var) < (min)) { (var) = (min); } \
140 else if ((var) > (max)) { (var) = (max); }
143 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
147 error = sysctl_handle_int(oidp, arg1, arg2, req);
149 RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
150 RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
151 RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
152 RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
153 RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
154 RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
161 static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
164 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
165 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0,
166 &sysctl_net_ipport_check, "I", "");
167 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
168 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0,
169 &sysctl_net_ipport_check, "I", "");
170 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first,
171 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0,
172 &sysctl_net_ipport_check, "I", "");
173 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last,
174 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0,
175 &sysctl_net_ipport_check, "I", "");
176 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
177 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0,
178 &sysctl_net_ipport_check, "I", "");
179 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
180 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0,
181 &sysctl_net_ipport_check, "I", "");
182 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
183 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, "");
184 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
185 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
186 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
187 &VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
188 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
189 &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
190 "allocations before switching to a sequental one");
191 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
192 &VNET_NAME(ipport_randomtime), 0,
193 "Minimum time to keep sequental port "
194 "allocation before switching to a random one");
198 * in_pcb.c: manage the Protocol Control Blocks.
200 * NOTE: It is assumed that most of these functions will be called with
201 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
202 * functions often modify hash chains or addresses in pcbs.
206 * Initialize an inpcbinfo -- we should be able to reduce the number of
210 in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
211 struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
212 char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
213 uint32_t inpcbzone_flags, u_int hashfields)
216 INP_INFO_LOCK_INIT(pcbinfo, name);
217 INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */
219 pcbinfo->ipi_vnet = curvnet;
221 pcbinfo->ipi_listhead = listhead;
222 LIST_INIT(pcbinfo->ipi_listhead);
223 pcbinfo->ipi_count = 0;
224 pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
225 &pcbinfo->ipi_hashmask);
226 pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
227 &pcbinfo->ipi_porthashmask);
229 in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
231 pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
232 NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
234 uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
235 uma_zone_set_warning(pcbinfo->ipi_zone,
236 "kern.ipc.maxsockets limit reached");
240 * Destroy an inpcbinfo.
243 in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
246 KASSERT(pcbinfo->ipi_count == 0,
247 ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
249 hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
250 hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
251 pcbinfo->ipi_porthashmask);
253 in_pcbgroup_destroy(pcbinfo);
255 uma_zdestroy(pcbinfo->ipi_zone);
256 INP_HASH_LOCK_DESTROY(pcbinfo);
257 INP_INFO_LOCK_DESTROY(pcbinfo);
261 * Allocate a PCB and associate it with the socket.
262 * On success return with the PCB locked.
265 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
270 INP_INFO_WLOCK_ASSERT(pcbinfo);
272 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
275 bzero(inp, inp_zero_size);
276 inp->inp_pcbinfo = pcbinfo;
277 inp->inp_socket = so;
278 inp->inp_cred = crhold(so->so_cred);
279 inp->inp_inc.inc_fibnum = so->so_fibnum;
281 error = mac_inpcb_init(inp, M_NOWAIT);
284 mac_inpcb_create(so, inp);
287 error = ipsec_init_policy(so, &inp->inp_sp);
290 mac_inpcb_destroy(inp);
296 if (INP_SOCKAF(so) == AF_INET6) {
297 inp->inp_vflag |= INP_IPV6PROTO;
299 inp->inp_flags |= IN6P_IPV6_V6ONLY;
302 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
303 pcbinfo->ipi_count++;
304 so->so_pcb = (caddr_t)inp;
306 if (V_ip6_auto_flowlabel)
307 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
310 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
311 refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */
312 #if defined(IPSEC) || defined(MAC)
315 crfree(inp->inp_cred);
316 uma_zfree(pcbinfo->ipi_zone, inp);
324 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
328 INP_WLOCK_ASSERT(inp);
329 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
331 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
333 anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
334 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
335 &inp->inp_lport, cred);
338 if (in_pcbinshash(inp) != 0) {
339 inp->inp_laddr.s_addr = INADDR_ANY;
344 inp->inp_flags |= INP_ANONPORT;
349 #if defined(INET) || defined(INET6)
351 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
352 struct ucred *cred, int lookupflags)
354 struct inpcbinfo *pcbinfo;
355 struct inpcb *tmpinp;
356 unsigned short *lastport;
357 int count, dorandom, error;
358 u_short aux, first, last, lport;
360 struct in_addr laddr;
363 pcbinfo = inp->inp_pcbinfo;
366 * Because no actual state changes occur here, a global write lock on
367 * the pcbinfo isn't required.
369 INP_LOCK_ASSERT(inp);
370 INP_HASH_LOCK_ASSERT(pcbinfo);
372 if (inp->inp_flags & INP_HIGHPORT) {
373 first = V_ipport_hifirstauto; /* sysctl */
374 last = V_ipport_hilastauto;
375 lastport = &pcbinfo->ipi_lasthi;
376 } else if (inp->inp_flags & INP_LOWPORT) {
377 error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
380 first = V_ipport_lowfirstauto; /* 1023 */
381 last = V_ipport_lowlastauto; /* 600 */
382 lastport = &pcbinfo->ipi_lastlow;
384 first = V_ipport_firstauto; /* sysctl */
385 last = V_ipport_lastauto;
386 lastport = &pcbinfo->ipi_lastport;
389 * For UDP(-Lite), use random port allocation as long as the user
390 * allows it. For TCP (and as of yet unknown) connections,
391 * use random port allocation only if the user allows it AND
392 * ipport_tick() allows it.
394 if (V_ipport_randomized &&
395 (!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
396 pcbinfo == &V_ulitecbinfo))
401 * It makes no sense to do random port allocation if
402 * we have the only port available.
406 /* Make sure to not include UDP(-Lite) packets in the count. */
407 if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
408 V_ipport_tcpallocs++;
410 * Instead of having two loops further down counting up or down
411 * make sure that first is always <= last and go with only one
412 * code path implementing all logic.
421 /* Make the compiler happy. */
423 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
424 KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
429 tmpinp = NULL; /* Make compiler happy. */
433 *lastport = first + (arc4random() % (last - first));
435 count = last - first;
438 if (count-- < 0) /* completely used? */
439 return (EADDRNOTAVAIL);
441 if (*lastport < first || *lastport > last)
443 lport = htons(*lastport);
446 if ((inp->inp_vflag & INP_IPV6) != 0)
447 tmpinp = in6_pcblookup_local(pcbinfo,
448 &inp->in6p_laddr, lport, lookupflags, cred);
450 #if defined(INET) && defined(INET6)
454 tmpinp = in_pcblookup_local(pcbinfo, laddr,
455 lport, lookupflags, cred);
457 } while (tmpinp != NULL);
460 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
461 laddrp->s_addr = laddr.s_addr;
469 * Return cached socket options.
472 inp_so_options(const struct inpcb *inp)
478 if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
479 so_options |= SO_REUSEPORT;
480 if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
481 so_options |= SO_REUSEADDR;
484 #endif /* INET || INET6 */
488 * Set up a bind operation on a PCB, performing port allocation
489 * as required, but do not actually modify the PCB. Callers can
490 * either complete the bind by setting inp_laddr/inp_lport and
491 * calling in_pcbinshash(), or they can just use the resulting
492 * port and address to authorise the sending of a once-off packet.
494 * On error, the values of *laddrp and *lportp are not changed.
497 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
498 u_short *lportp, struct ucred *cred)
500 struct socket *so = inp->inp_socket;
501 struct sockaddr_in *sin;
502 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
503 struct in_addr laddr;
505 int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
509 * No state changes, so read locks are sufficient here.
511 INP_LOCK_ASSERT(inp);
512 INP_HASH_LOCK_ASSERT(pcbinfo);
514 if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
515 return (EADDRNOTAVAIL);
516 laddr.s_addr = *laddrp;
517 if (nam != NULL && laddr.s_addr != INADDR_ANY)
519 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
520 lookupflags = INPLOOKUP_WILDCARD;
522 if ((error = prison_local_ip4(cred, &laddr)) != 0)
525 sin = (struct sockaddr_in *)nam;
526 if (nam->sa_len != sizeof (*sin))
530 * We should check the family, but old programs
531 * incorrectly fail to initialize it.
533 if (sin->sin_family != AF_INET)
534 return (EAFNOSUPPORT);
536 error = prison_local_ip4(cred, &sin->sin_addr);
539 if (sin->sin_port != *lportp) {
540 /* Don't allow the port to change. */
543 lport = sin->sin_port;
545 /* NB: lport is left as 0 if the port isn't being changed. */
546 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
548 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
549 * allow complete duplication of binding if
550 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
551 * and a multicast address is bound on both
552 * new and duplicated sockets.
554 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
555 reuseport = SO_REUSEADDR|SO_REUSEPORT;
556 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
557 sin->sin_port = 0; /* yech... */
558 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
560 * Is the address a local IP address?
561 * If INP_BINDANY is set, then the socket may be bound
562 * to any endpoint address, local or not.
564 if ((inp->inp_flags & INP_BINDANY) == 0 &&
565 ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
566 return (EADDRNOTAVAIL);
568 laddr = sin->sin_addr;
574 if (ntohs(lport) <= V_ipport_reservedhigh &&
575 ntohs(lport) >= V_ipport_reservedlow &&
576 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
579 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
580 priv_check_cred(inp->inp_cred,
581 PRIV_NETINET_REUSEPORT, 0) != 0) {
582 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
583 lport, INPLOOKUP_WILDCARD, cred);
586 * This entire block sorely needs a rewrite.
589 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
590 (so->so_type != SOCK_STREAM ||
591 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
592 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
593 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
594 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
595 (inp->inp_cred->cr_uid !=
596 t->inp_cred->cr_uid))
599 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
600 lport, lookupflags, cred);
601 if (t && (t->inp_flags & INP_TIMEWAIT)) {
603 * XXXRW: If an incpb has had its timewait
604 * state recycled, we treat the address as
605 * being in use (for now). This is better
606 * than a panic, but not desirable.
610 (reuseport & tw->tw_so_options) == 0)
612 } else if (t && (reuseport & inp_so_options(t)) == 0) {
614 if (ntohl(sin->sin_addr.s_addr) !=
616 ntohl(t->inp_laddr.s_addr) !=
618 (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
619 (t->inp_vflag & INP_IPV6PROTO) == 0)
628 error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
633 *laddrp = laddr.s_addr;
639 * Connect from a socket to a specified address.
640 * Both address and port must be specified in argument sin.
641 * If don't have a local address for this socket yet,
645 in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
646 struct ucred *cred, struct mbuf *m)
648 u_short lport, fport;
649 in_addr_t laddr, faddr;
652 INP_WLOCK_ASSERT(inp);
653 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
655 lport = inp->inp_lport;
656 laddr = inp->inp_laddr.s_addr;
657 anonport = (lport == 0);
658 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
663 /* Do the initial binding of the local address if required. */
664 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
665 inp->inp_lport = lport;
666 inp->inp_laddr.s_addr = laddr;
667 if (in_pcbinshash(inp) != 0) {
668 inp->inp_laddr.s_addr = INADDR_ANY;
674 /* Commit the remaining changes. */
675 inp->inp_lport = lport;
676 inp->inp_laddr.s_addr = laddr;
677 inp->inp_faddr.s_addr = faddr;
678 inp->inp_fport = fport;
679 in_pcbrehash_mbuf(inp, m);
682 inp->inp_flags |= INP_ANONPORT;
687 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
690 return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
694 * Do proper source address selection on an unbound socket in case
695 * of connect. Take jails into account as well.
698 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
703 struct sockaddr_in *sin;
707 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
710 * Bypass source address selection and use the primary jail IP
713 if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
717 bzero(&sro, sizeof(sro));
719 sin = (struct sockaddr_in *)&sro.ro_dst;
720 sin->sin_family = AF_INET;
721 sin->sin_len = sizeof(struct sockaddr_in);
722 sin->sin_addr.s_addr = faddr->s_addr;
725 * If route is known our src addr is taken from the i/f,
728 * Find out route to destination.
730 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
731 in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
734 * If we found a route, use the address corresponding to
735 * the outgoing interface.
737 * Otherwise assume faddr is reachable on a directly connected
738 * network and try to find a corresponding interface to take
739 * the source address from.
741 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
742 struct in_ifaddr *ia;
745 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
747 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0));
753 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
754 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
755 ifa_free(&ia->ia_ifa);
760 ifa_free(&ia->ia_ifa);
763 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
766 if (sa->sa_family != AF_INET)
768 sin = (struct sockaddr_in *)sa;
769 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
770 ia = (struct in_ifaddr *)ifa;
775 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
776 IF_ADDR_RUNLOCK(ifp);
779 IF_ADDR_RUNLOCK(ifp);
781 /* 3. As a last resort return the 'default' jail address. */
782 error = prison_get_ip4(cred, laddr);
787 * If the outgoing interface on the route found is not
788 * a loopback interface, use the address from that interface.
789 * In case of jails do those three steps:
790 * 1. check if the interface address belongs to the jail. If so use it.
791 * 2. check if we have any address on the outgoing interface
792 * belonging to this jail. If so use it.
793 * 3. as a last resort return the 'default' jail address.
795 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
796 struct in_ifaddr *ia;
799 /* If not jailed, use the default returned. */
800 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
801 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
802 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
807 /* 1. Check if the iface address belongs to the jail. */
808 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
809 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
810 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
811 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
816 * 2. Check if we have any address on the outgoing interface
817 * belonging to this jail.
820 ifp = sro.ro_rt->rt_ifp;
822 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
824 if (sa->sa_family != AF_INET)
826 sin = (struct sockaddr_in *)sa;
827 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
828 ia = (struct in_ifaddr *)ifa;
833 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
834 IF_ADDR_RUNLOCK(ifp);
837 IF_ADDR_RUNLOCK(ifp);
839 /* 3. As a last resort return the 'default' jail address. */
840 error = prison_get_ip4(cred, laddr);
845 * The outgoing interface is marked with 'loopback net', so a route
846 * to ourselves is here.
847 * Try to find the interface of the destination address and then
848 * take the address from there. That interface is not necessarily
849 * a loopback interface.
850 * In case of jails, check that it is an address of the jail
851 * and if we cannot find, fall back to the 'default' jail address.
853 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
854 struct sockaddr_in sain;
855 struct in_ifaddr *ia;
857 bzero(&sain, sizeof(struct sockaddr_in));
858 sain.sin_family = AF_INET;
859 sain.sin_len = sizeof(struct sockaddr_in);
860 sain.sin_addr.s_addr = faddr->s_addr;
862 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
864 ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0));
866 ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
868 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
873 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
874 ifa_free(&ia->ia_ifa);
883 ifa_free(&ia->ia_ifa);
886 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
889 if (sa->sa_family != AF_INET)
891 sin = (struct sockaddr_in *)sa;
892 if (prison_check_ip4(cred,
893 &sin->sin_addr) == 0) {
894 ia = (struct in_ifaddr *)ifa;
899 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
900 IF_ADDR_RUNLOCK(ifp);
903 IF_ADDR_RUNLOCK(ifp);
906 /* 3. As a last resort return the 'default' jail address. */
907 error = prison_get_ip4(cred, laddr);
912 if (sro.ro_rt != NULL)
918 * Set up for a connect from a socket to the specified address.
919 * On entry, *laddrp and *lportp should contain the current local
920 * address and port for the PCB; these are updated to the values
921 * that should be placed in inp_laddr and inp_lport to complete
924 * On success, *faddrp and *fportp will be set to the remote address
925 * and port. These are not updated in the error case.
927 * If the operation fails because the connection already exists,
928 * *oinpp will be set to the PCB of that connection so that the
929 * caller can decide to override it. In all other cases, *oinpp
933 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
934 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
935 struct inpcb **oinpp, struct ucred *cred)
937 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
938 struct in_ifaddr *ia;
940 struct in_addr laddr, faddr;
941 u_short lport, fport;
945 * Because a global state change doesn't actually occur here, a read
946 * lock is sufficient.
948 INP_LOCK_ASSERT(inp);
949 INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
953 if (nam->sa_len != sizeof (*sin))
955 if (sin->sin_family != AF_INET)
956 return (EAFNOSUPPORT);
957 if (sin->sin_port == 0)
958 return (EADDRNOTAVAIL);
959 laddr.s_addr = *laddrp;
961 faddr = sin->sin_addr;
962 fport = sin->sin_port;
964 if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
966 * If the destination address is INADDR_ANY,
967 * use the primary local address.
968 * If the supplied address is INADDR_BROADCAST,
969 * and the primary interface supports broadcast,
970 * choose the broadcast address for that interface.
972 if (faddr.s_addr == INADDR_ANY) {
975 IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
978 (error = prison_get_ip4(cred, &faddr)) != 0)
980 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
982 if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
984 faddr = satosin(&TAILQ_FIRST(
985 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
989 if (laddr.s_addr == INADDR_ANY) {
990 error = in_pcbladdr(inp, &faddr, &laddr, cred);
992 * If the destination address is multicast and an outgoing
993 * interface has been set as a multicast option, prefer the
994 * address of that interface as our source address.
996 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
997 inp->inp_moptions != NULL) {
998 struct ip_moptions *imo;
1001 imo = inp->inp_moptions;
1002 if (imo->imo_multicast_ifp != NULL) {
1003 ifp = imo->imo_multicast_ifp;
1005 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1006 if ((ia->ia_ifp == ifp) &&
1008 prison_check_ip4(cred,
1009 &ia->ia_addr.sin_addr) == 0))
1013 error = EADDRNOTAVAIL;
1015 laddr = ia->ia_addr.sin_addr;
1018 IN_IFADDR_RUNLOCK();
1024 oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1025 laddr, lport, 0, NULL);
1029 return (EADDRINUSE);
1032 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1037 *laddrp = laddr.s_addr;
1039 *faddrp = faddr.s_addr;
1045 in_pcbdisconnect(struct inpcb *inp)
1048 INP_WLOCK_ASSERT(inp);
1049 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1051 inp->inp_faddr.s_addr = INADDR_ANY;
1058 * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1059 * For most protocols, this will be invoked immediately prior to calling
1060 * in_pcbfree(). However, with TCP the inpcb may significantly outlive the
1061 * socket, in which case in_pcbfree() is deferred.
1064 in_pcbdetach(struct inpcb *inp)
1067 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1069 inp->inp_socket->so_pcb = NULL;
1070 inp->inp_socket = NULL;
1074 * in_pcbref() bumps the reference count on an inpcb in order to maintain
1075 * stability of an inpcb pointer despite the inpcb lock being released. This
1076 * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
1077 * but where the inpcb lock may already held, or when acquiring a reference
1080 * in_pcbref() should be used only to provide brief memory stability, and
1081 * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
1082 * garbage collect the inpcb if it has been in_pcbfree()'d from another
1083 * context. Until in_pcbrele() has returned that the inpcb is still valid,
1084 * lock and rele are the *only* safe operations that may be performed on the
1087 * While the inpcb will not be freed, releasing the inpcb lock means that the
1088 * connection's state may change, so the caller should be careful to
1089 * revalidate any cached state on reacquiring the lock. Drop the reference
1090 * using in_pcbrele().
1093 in_pcbref(struct inpcb *inp)
1096 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1098 refcount_acquire(&inp->inp_refcount);
1102 * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
1103 * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
1104 * return a flag indicating whether or not the inpcb remains valid. If it is
1105 * valid, we return with the inpcb lock held.
1107 * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
1108 * reference on an inpcb. Historically more work was done here (actually, in
1109 * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
1110 * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely
1111 * about memory stability (and continued use of the write lock).
1114 in_pcbrele_rlocked(struct inpcb *inp)
1116 struct inpcbinfo *pcbinfo;
1118 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1120 INP_RLOCK_ASSERT(inp);
1122 if (refcount_release(&inp->inp_refcount) == 0) {
1124 * If the inpcb has been freed, let the caller know, even if
1125 * this isn't the last reference.
1127 if (inp->inp_flags2 & INP_FREED) {
1134 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1137 pcbinfo = inp->inp_pcbinfo;
1138 uma_zfree(pcbinfo->ipi_zone, inp);
1143 in_pcbrele_wlocked(struct inpcb *inp)
1145 struct inpcbinfo *pcbinfo;
1147 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1149 INP_WLOCK_ASSERT(inp);
1151 if (refcount_release(&inp->inp_refcount) == 0)
1154 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1157 pcbinfo = inp->inp_pcbinfo;
1158 uma_zfree(pcbinfo->ipi_zone, inp);
1163 * Temporary wrapper.
1166 in_pcbrele(struct inpcb *inp)
1169 return (in_pcbrele_wlocked(inp));
1173 * Unconditionally schedule an inpcb to be freed by decrementing its
1174 * reference count, which should occur only after the inpcb has been detached
1175 * from its socket. If another thread holds a temporary reference (acquired
1176 * using in_pcbref()) then the free is deferred until that reference is
1177 * released using in_pcbrele(), but the inpcb is still unlocked. Almost all
1178 * work, including removal from global lists, is done in this context, where
1179 * the pcbinfo lock is held.
1182 in_pcbfree(struct inpcb *inp)
1184 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1186 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1188 INP_INFO_WLOCK_ASSERT(pcbinfo);
1189 INP_WLOCK_ASSERT(inp);
1191 /* XXXRW: Do as much as possible here. */
1193 if (inp->inp_sp != NULL)
1194 ipsec_delete_pcbpolicy(inp);
1196 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1197 in_pcbremlists(inp);
1199 if (inp->inp_vflag & INP_IPV6PROTO) {
1200 ip6_freepcbopts(inp->in6p_outputopts);
1201 if (inp->in6p_moptions != NULL)
1202 ip6_freemoptions(inp->in6p_moptions);
1205 if (inp->inp_options)
1206 (void)m_free(inp->inp_options);
1208 if (inp->inp_moptions != NULL)
1209 inp_freemoptions(inp->inp_moptions);
1212 inp->inp_flags2 |= INP_FREED;
1213 crfree(inp->inp_cred);
1215 mac_inpcb_destroy(inp);
1217 if (!in_pcbrele_wlocked(inp))
1222 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1223 * port reservation, and preventing it from being returned by inpcb lookups.
1225 * It is used by TCP to mark an inpcb as unused and avoid future packet
1226 * delivery or event notification when a socket remains open but TCP has
1227 * closed. This might occur as a result of a shutdown()-initiated TCP close
1228 * or a RST on the wire, and allows the port binding to be reused while still
1229 * maintaining the invariant that so_pcb always points to a valid inpcb until
1232 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1233 * in_pcbnotifyall() and in_pcbpurgeif0()?
1236 in_pcbdrop(struct inpcb *inp)
1239 INP_WLOCK_ASSERT(inp);
1242 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1245 inp->inp_flags |= INP_DROPPED;
1246 if (inp->inp_flags & INP_INHASHLIST) {
1247 struct inpcbport *phd = inp->inp_phd;
1249 INP_HASH_WLOCK(inp->inp_pcbinfo);
1250 LIST_REMOVE(inp, inp_hash);
1251 LIST_REMOVE(inp, inp_portlist);
1252 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1253 LIST_REMOVE(phd, phd_hash);
1256 INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1257 inp->inp_flags &= ~INP_INHASHLIST;
1259 in_pcbgroup_remove(inp);
1266 * Common routines to return the socket addresses associated with inpcbs.
1269 in_sockaddr(in_port_t port, struct in_addr *addr_p)
1271 struct sockaddr_in *sin;
1273 sin = malloc(sizeof *sin, M_SONAME,
1275 sin->sin_family = AF_INET;
1276 sin->sin_len = sizeof(*sin);
1277 sin->sin_addr = *addr_p;
1278 sin->sin_port = port;
1280 return (struct sockaddr *)sin;
1284 in_getsockaddr(struct socket *so, struct sockaddr **nam)
1287 struct in_addr addr;
1290 inp = sotoinpcb(so);
1291 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1294 port = inp->inp_lport;
1295 addr = inp->inp_laddr;
1298 *nam = in_sockaddr(port, &addr);
1303 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1306 struct in_addr addr;
1309 inp = sotoinpcb(so);
1310 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1313 port = inp->inp_fport;
1314 addr = inp->inp_faddr;
1317 *nam = in_sockaddr(port, &addr);
1322 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1323 struct inpcb *(*notify)(struct inpcb *, int))
1325 struct inpcb *inp, *inp_temp;
1327 INP_INFO_WLOCK(pcbinfo);
1328 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1331 if ((inp->inp_vflag & INP_IPV4) == 0) {
1336 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1337 inp->inp_socket == NULL) {
1341 if ((*notify)(inp, errno))
1344 INP_INFO_WUNLOCK(pcbinfo);
1348 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1351 struct ip_moptions *imo;
1354 INP_INFO_RLOCK(pcbinfo);
1355 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1357 imo = inp->inp_moptions;
1358 if ((inp->inp_vflag & INP_IPV4) &&
1361 * Unselect the outgoing interface if it is being
1364 if (imo->imo_multicast_ifp == ifp)
1365 imo->imo_multicast_ifp = NULL;
1368 * Drop multicast group membership if we joined
1369 * through the interface being detached.
1371 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1373 if (imo->imo_membership[i]->inm_ifp == ifp) {
1374 in_delmulti(imo->imo_membership[i]);
1376 } else if (gap != 0)
1377 imo->imo_membership[i - gap] =
1378 imo->imo_membership[i];
1380 imo->imo_num_memberships -= gap;
1384 INP_INFO_RUNLOCK(pcbinfo);
1388 * Lookup a PCB based on the local address and port. Caller must hold the
1389 * hash lock. No inpcb locks or references are acquired.
1391 #define INP_LOOKUP_MAPPED_PCB_COST 3
1393 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1394 u_short lport, int lookupflags, struct ucred *cred)
1398 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1404 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1405 ("%s: invalid lookup flags %d", __func__, lookupflags));
1407 INP_HASH_LOCK_ASSERT(pcbinfo);
1409 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1410 struct inpcbhead *head;
1412 * Look for an unconnected (wildcard foreign addr) PCB that
1413 * matches the local address and port we're looking for.
1415 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1416 0, pcbinfo->ipi_hashmask)];
1417 LIST_FOREACH(inp, head, inp_hash) {
1419 /* XXX inp locking */
1420 if ((inp->inp_vflag & INP_IPV4) == 0)
1423 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1424 inp->inp_laddr.s_addr == laddr.s_addr &&
1425 inp->inp_lport == lport) {
1430 prison_equal_ip4(cred->cr_prison,
1431 inp->inp_cred->cr_prison))
1440 struct inpcbporthead *porthash;
1441 struct inpcbport *phd;
1442 struct inpcb *match = NULL;
1444 * Best fit PCB lookup.
1446 * First see if this local port is in use by looking on the
1449 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1450 pcbinfo->ipi_porthashmask)];
1451 LIST_FOREACH(phd, porthash, phd_hash) {
1452 if (phd->phd_port == lport)
1457 * Port is in use by one or more PCBs. Look for best
1460 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1463 !prison_equal_ip4(inp->inp_cred->cr_prison,
1467 /* XXX inp locking */
1468 if ((inp->inp_vflag & INP_IPV4) == 0)
1471 * We never select the PCB that has
1472 * INP_IPV6 flag and is bound to :: if
1473 * we have another PCB which is bound
1474 * to 0.0.0.0. If a PCB has the
1475 * INP_IPV6 flag, then we set its cost
1476 * higher than IPv4 only PCBs.
1478 * Note that the case only happens
1479 * when a socket is bound to ::, under
1480 * the condition that the use of the
1481 * mapped address is allowed.
1483 if ((inp->inp_vflag & INP_IPV6) != 0)
1484 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1486 if (inp->inp_faddr.s_addr != INADDR_ANY)
1488 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1489 if (laddr.s_addr == INADDR_ANY)
1491 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1494 if (laddr.s_addr != INADDR_ANY)
1497 if (wildcard < matchwild) {
1499 matchwild = wildcard;
1508 #undef INP_LOOKUP_MAPPED_PCB_COST
1512 * Lookup PCB in hash list, using pcbgroup tables.
1514 static struct inpcb *
1515 in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
1516 struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
1517 u_int lport_arg, int lookupflags, struct ifnet *ifp)
1519 struct inpcbhead *head;
1520 struct inpcb *inp, *tmpinp;
1521 u_short fport = fport_arg, lport = lport_arg;
1524 * First look for an exact match.
1527 INP_GROUP_LOCK(pcbgroup);
1528 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1529 pcbgroup->ipg_hashmask)];
1530 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1532 /* XXX inp locking */
1533 if ((inp->inp_vflag & INP_IPV4) == 0)
1536 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1537 inp->inp_laddr.s_addr == laddr.s_addr &&
1538 inp->inp_fport == fport &&
1539 inp->inp_lport == lport) {
1541 * XXX We should be able to directly return
1542 * the inp here, without any checks.
1543 * Well unless both bound with SO_REUSEPORT?
1545 if (prison_flag(inp->inp_cred, PR_IP4))
1551 if (tmpinp != NULL) {
1557 * Then look for a wildcard match, if requested.
1559 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1560 struct inpcb *local_wild = NULL, *local_exact = NULL;
1562 struct inpcb *local_wild_mapped = NULL;
1564 struct inpcb *jail_wild = NULL;
1565 struct inpcbhead *head;
1569 * Order of socket selection - we always prefer jails.
1570 * 1. jailed, non-wild.
1572 * 3. non-jailed, non-wild.
1573 * 4. non-jailed, wild.
1575 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
1576 0, pcbinfo->ipi_wildmask)];
1577 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1579 /* XXX inp locking */
1580 if ((inp->inp_vflag & INP_IPV4) == 0)
1583 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1584 inp->inp_lport != lport)
1587 /* XXX inp locking */
1588 if (ifp && ifp->if_type == IFT_FAITH &&
1589 (inp->inp_flags & INP_FAITH) == 0)
1592 injail = prison_flag(inp->inp_cred, PR_IP4);
1594 if (prison_check_ip4(inp->inp_cred,
1598 if (local_exact != NULL)
1602 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1607 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1609 /* XXX inp locking, NULL check */
1610 if (inp->inp_vflag & INP_IPV6PROTO)
1611 local_wild_mapped = inp;
1619 } /* LIST_FOREACH */
1627 inp = local_wild_mapped;
1631 } /* if (lookupflags & INPLOOKUP_WILDCARD) */
1632 INP_GROUP_UNLOCK(pcbgroup);
1637 INP_GROUP_UNLOCK(pcbgroup);
1638 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1640 if (in_pcbrele_wlocked(inp))
1642 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1644 if (in_pcbrele_rlocked(inp))
1647 panic("%s: locking bug", __func__);
1650 #endif /* PCBGROUP */
1653 * Lookup PCB in hash list, using pcbinfo tables. This variation assumes
1654 * that the caller has locked the hash list, and will not perform any further
1655 * locking or reference operations on either the hash list or the connection.
1657 static struct inpcb *
1658 in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1659 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1662 struct inpcbhead *head;
1663 struct inpcb *inp, *tmpinp;
1664 u_short fport = fport_arg, lport = lport_arg;
1666 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1667 ("%s: invalid lookup flags %d", __func__, lookupflags));
1669 INP_HASH_LOCK_ASSERT(pcbinfo);
1672 * First look for an exact match.
1675 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1676 pcbinfo->ipi_hashmask)];
1677 LIST_FOREACH(inp, head, inp_hash) {
1679 /* XXX inp locking */
1680 if ((inp->inp_vflag & INP_IPV4) == 0)
1683 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1684 inp->inp_laddr.s_addr == laddr.s_addr &&
1685 inp->inp_fport == fport &&
1686 inp->inp_lport == lport) {
1688 * XXX We should be able to directly return
1689 * the inp here, without any checks.
1690 * Well unless both bound with SO_REUSEPORT?
1692 if (prison_flag(inp->inp_cred, PR_IP4))
1702 * Then look for a wildcard match, if requested.
1704 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1705 struct inpcb *local_wild = NULL, *local_exact = NULL;
1707 struct inpcb *local_wild_mapped = NULL;
1709 struct inpcb *jail_wild = NULL;
1713 * Order of socket selection - we always prefer jails.
1714 * 1. jailed, non-wild.
1716 * 3. non-jailed, non-wild.
1717 * 4. non-jailed, wild.
1720 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1721 0, pcbinfo->ipi_hashmask)];
1722 LIST_FOREACH(inp, head, inp_hash) {
1724 /* XXX inp locking */
1725 if ((inp->inp_vflag & INP_IPV4) == 0)
1728 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1729 inp->inp_lport != lport)
1732 /* XXX inp locking */
1733 if (ifp && ifp->if_type == IFT_FAITH &&
1734 (inp->inp_flags & INP_FAITH) == 0)
1737 injail = prison_flag(inp->inp_cred, PR_IP4);
1739 if (prison_check_ip4(inp->inp_cred,
1743 if (local_exact != NULL)
1747 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1752 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1754 /* XXX inp locking, NULL check */
1755 if (inp->inp_vflag & INP_IPV6PROTO)
1756 local_wild_mapped = inp;
1764 } /* LIST_FOREACH */
1765 if (jail_wild != NULL)
1767 if (local_exact != NULL)
1768 return (local_exact);
1769 if (local_wild != NULL)
1770 return (local_wild);
1772 if (local_wild_mapped != NULL)
1773 return (local_wild_mapped);
1775 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1781 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1782 * hash list lock, and will return the inpcb locked (i.e., requires
1783 * INPLOOKUP_LOCKPCB).
1785 static struct inpcb *
1786 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1787 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1792 INP_HASH_RLOCK(pcbinfo);
1793 inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1794 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1797 INP_HASH_RUNLOCK(pcbinfo);
1798 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1800 if (in_pcbrele_wlocked(inp))
1802 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1804 if (in_pcbrele_rlocked(inp))
1807 panic("%s: locking bug", __func__);
1809 INP_HASH_RUNLOCK(pcbinfo);
1814 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1815 * from which a pre-calculated hash value may be extracted.
1817 * Possibly more of this logic should be in in_pcbgroup.c.
1820 in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1821 struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1823 #if defined(PCBGROUP)
1824 struct inpcbgroup *pcbgroup;
1827 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1828 ("%s: invalid lookup flags %d", __func__, lookupflags));
1829 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1830 ("%s: LOCKPCB not set", __func__));
1832 #if defined(PCBGROUP)
1833 if (in_pcbgroup_enabled(pcbinfo)) {
1834 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1836 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1837 laddr, lport, lookupflags, ifp));
1840 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1845 in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1846 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1847 struct ifnet *ifp, struct mbuf *m)
1850 struct inpcbgroup *pcbgroup;
1853 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1854 ("%s: invalid lookup flags %d", __func__, lookupflags));
1855 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1856 ("%s: LOCKPCB not set", __func__));
1859 if (in_pcbgroup_enabled(pcbinfo)) {
1860 pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1861 m->m_pkthdr.flowid);
1862 if (pcbgroup != NULL)
1863 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
1864 fport, laddr, lport, lookupflags, ifp));
1865 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1867 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1868 laddr, lport, lookupflags, ifp));
1871 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1877 * Insert PCB onto various hash lists.
1880 in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
1882 struct inpcbhead *pcbhash;
1883 struct inpcbporthead *pcbporthash;
1884 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1885 struct inpcbport *phd;
1886 u_int32_t hashkey_faddr;
1888 INP_WLOCK_ASSERT(inp);
1889 INP_HASH_WLOCK_ASSERT(pcbinfo);
1891 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1892 ("in_pcbinshash: INP_INHASHLIST"));
1895 if (inp->inp_vflag & INP_IPV6)
1896 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1899 hashkey_faddr = inp->inp_faddr.s_addr;
1901 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1902 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1904 pcbporthash = &pcbinfo->ipi_porthashbase[
1905 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1908 * Go through port list and look for a head for this lport.
1910 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1911 if (phd->phd_port == inp->inp_lport)
1915 * If none exists, malloc one and tack it on.
1918 phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1920 return (ENOBUFS); /* XXX */
1922 phd->phd_port = inp->inp_lport;
1923 LIST_INIT(&phd->phd_pcblist);
1924 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1927 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1928 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1929 inp->inp_flags |= INP_INHASHLIST;
1931 if (do_pcbgroup_update)
1932 in_pcbgroup_update(inp);
1938 * For now, there are two public interfaces to insert an inpcb into the hash
1939 * lists -- one that does update pcbgroups, and one that doesn't. The latter
1940 * is used only in the TCP syncache, where in_pcbinshash is called before the
1941 * full 4-tuple is set for the inpcb, and we don't want to install in the
1942 * pcbgroup until later.
1944 * XXXRW: This seems like a misfeature. in_pcbinshash should always update
1945 * connection groups, and partially initialised inpcbs should not be exposed
1946 * to either reservation hash tables or pcbgroups.
1949 in_pcbinshash(struct inpcb *inp)
1952 return (in_pcbinshash_internal(inp, 1));
1956 in_pcbinshash_nopcbgroup(struct inpcb *inp)
1959 return (in_pcbinshash_internal(inp, 0));
1963 * Move PCB to the proper hash bucket when { faddr, fport } have been
1964 * changed. NOTE: This does not handle the case of the lport changing (the
1965 * hashed port list would have to be updated as well), so the lport must
1966 * not change after in_pcbinshash() has been called.
1969 in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
1971 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1972 struct inpcbhead *head;
1973 u_int32_t hashkey_faddr;
1975 INP_WLOCK_ASSERT(inp);
1976 INP_HASH_WLOCK_ASSERT(pcbinfo);
1978 KASSERT(inp->inp_flags & INP_INHASHLIST,
1979 ("in_pcbrehash: !INP_INHASHLIST"));
1982 if (inp->inp_vflag & INP_IPV6)
1983 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1986 hashkey_faddr = inp->inp_faddr.s_addr;
1988 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1989 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1991 LIST_REMOVE(inp, inp_hash);
1992 LIST_INSERT_HEAD(head, inp, inp_hash);
1996 in_pcbgroup_update_mbuf(inp, m);
1998 in_pcbgroup_update(inp);
2003 in_pcbrehash(struct inpcb *inp)
2006 in_pcbrehash_mbuf(inp, NULL);
2010 * Remove PCB from various lists.
2013 in_pcbremlists(struct inpcb *inp)
2015 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2017 INP_INFO_WLOCK_ASSERT(pcbinfo);
2018 INP_WLOCK_ASSERT(inp);
2020 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2021 if (inp->inp_flags & INP_INHASHLIST) {
2022 struct inpcbport *phd = inp->inp_phd;
2024 INP_HASH_WLOCK(pcbinfo);
2025 LIST_REMOVE(inp, inp_hash);
2026 LIST_REMOVE(inp, inp_portlist);
2027 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2028 LIST_REMOVE(phd, phd_hash);
2031 INP_HASH_WUNLOCK(pcbinfo);
2032 inp->inp_flags &= ~INP_INHASHLIST;
2034 LIST_REMOVE(inp, inp_list);
2035 pcbinfo->ipi_count--;
2037 in_pcbgroup_remove(inp);
2042 * A set label operation has occurred at the socket layer, propagate the
2043 * label change into the in_pcb for the socket.
2046 in_pcbsosetlabel(struct socket *so)
2051 inp = sotoinpcb(so);
2052 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2056 mac_inpcb_sosetlabel(so, inp);
2063 * ipport_tick runs once per second, determining if random port allocation
2064 * should be continued. If more than ipport_randomcps ports have been
2065 * allocated in the last second, then we return to sequential port
2066 * allocation. We return to random allocation only once we drop below
2067 * ipport_randomcps for at least ipport_randomtime seconds.
2070 ipport_tick(void *xtp)
2072 VNET_ITERATOR_DECL(vnet_iter);
2074 VNET_LIST_RLOCK_NOSLEEP();
2075 VNET_FOREACH(vnet_iter) {
2076 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */
2077 if (V_ipport_tcpallocs <=
2078 V_ipport_tcplastcount + V_ipport_randomcps) {
2079 if (V_ipport_stoprandom > 0)
2080 V_ipport_stoprandom--;
2082 V_ipport_stoprandom = V_ipport_randomtime;
2083 V_ipport_tcplastcount = V_ipport_tcpallocs;
2086 VNET_LIST_RUNLOCK_NOSLEEP();
2087 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
2094 callout_stop(&ipport_tick_callout);
2098 * The ipport_callout should start running at about the time we attach the
2099 * inet or inet6 domains.
2102 ipport_tick_init(const void *unused __unused)
2105 /* Start ipport_tick. */
2106 callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
2107 callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2108 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2109 SHUTDOWN_PRI_DEFAULT);
2111 SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2112 ipport_tick_init, NULL);
2115 inp_wlock(struct inpcb *inp)
2122 inp_wunlock(struct inpcb *inp)
2129 inp_rlock(struct inpcb *inp)
2136 inp_runlock(struct inpcb *inp)
2144 inp_lock_assert(struct inpcb *inp)
2147 INP_WLOCK_ASSERT(inp);
2151 inp_unlock_assert(struct inpcb *inp)
2154 INP_UNLOCK_ASSERT(inp);
2159 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
2163 INP_INFO_RLOCK(&V_tcbinfo);
2164 LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
2169 INP_INFO_RUNLOCK(&V_tcbinfo);
2173 inp_inpcbtosocket(struct inpcb *inp)
2176 INP_WLOCK_ASSERT(inp);
2177 return (inp->inp_socket);
2181 inp_inpcbtotcpcb(struct inpcb *inp)
2184 INP_WLOCK_ASSERT(inp);
2185 return ((struct tcpcb *)inp->inp_ppcb);
2189 inp_ip_tos_get(const struct inpcb *inp)
2192 return (inp->inp_ip_tos);
2196 inp_ip_tos_set(struct inpcb *inp, int val)
2199 inp->inp_ip_tos = val;
2203 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
2204 uint32_t *faddr, uint16_t *fp)
2207 INP_LOCK_ASSERT(inp);
2208 *laddr = inp->inp_laddr.s_addr;
2209 *faddr = inp->inp_faddr.s_addr;
2210 *lp = inp->inp_lport;
2211 *fp = inp->inp_fport;
2215 so_sotoinpcb(struct socket *so)
2218 return (sotoinpcb(so));
2222 so_sototcpcb(struct socket *so)
2225 return (sototcpcb(so));
2230 db_print_indent(int indent)
2234 for (i = 0; i < indent; i++)
2239 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2241 char faddr_str[48], laddr_str[48];
2243 db_print_indent(indent);
2244 db_printf("%s at %p\n", name, inc);
2249 if (inc->inc_flags & INC_ISIPV6) {
2251 ip6_sprintf(laddr_str, &inc->inc6_laddr);
2252 ip6_sprintf(faddr_str, &inc->inc6_faddr);
2257 inet_ntoa_r(inc->inc_laddr, laddr_str);
2258 inet_ntoa_r(inc->inc_faddr, faddr_str);
2260 db_print_indent(indent);
2261 db_printf("inc_laddr %s inc_lport %u\n", laddr_str,
2262 ntohs(inc->inc_lport));
2263 db_print_indent(indent);
2264 db_printf("inc_faddr %s inc_fport %u\n", faddr_str,
2265 ntohs(inc->inc_fport));
2269 db_print_inpflags(int inp_flags)
2274 if (inp_flags & INP_RECVOPTS) {
2275 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2278 if (inp_flags & INP_RECVRETOPTS) {
2279 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2282 if (inp_flags & INP_RECVDSTADDR) {
2283 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2286 if (inp_flags & INP_HDRINCL) {
2287 db_printf("%sINP_HDRINCL", comma ? ", " : "");
2290 if (inp_flags & INP_HIGHPORT) {
2291 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2294 if (inp_flags & INP_LOWPORT) {
2295 db_printf("%sINP_LOWPORT", comma ? ", " : "");
2298 if (inp_flags & INP_ANONPORT) {
2299 db_printf("%sINP_ANONPORT", comma ? ", " : "");
2302 if (inp_flags & INP_RECVIF) {
2303 db_printf("%sINP_RECVIF", comma ? ", " : "");
2306 if (inp_flags & INP_MTUDISC) {
2307 db_printf("%sINP_MTUDISC", comma ? ", " : "");
2310 if (inp_flags & INP_FAITH) {
2311 db_printf("%sINP_FAITH", comma ? ", " : "");
2314 if (inp_flags & INP_RECVTTL) {
2315 db_printf("%sINP_RECVTTL", comma ? ", " : "");
2318 if (inp_flags & INP_DONTFRAG) {
2319 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2322 if (inp_flags & INP_RECVTOS) {
2323 db_printf("%sINP_RECVTOS", comma ? ", " : "");
2326 if (inp_flags & IN6P_IPV6_V6ONLY) {
2327 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2330 if (inp_flags & IN6P_PKTINFO) {
2331 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2334 if (inp_flags & IN6P_HOPLIMIT) {
2335 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2338 if (inp_flags & IN6P_HOPOPTS) {
2339 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2342 if (inp_flags & IN6P_DSTOPTS) {
2343 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2346 if (inp_flags & IN6P_RTHDR) {
2347 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2350 if (inp_flags & IN6P_RTHDRDSTOPTS) {
2351 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2354 if (inp_flags & IN6P_TCLASS) {
2355 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2358 if (inp_flags & IN6P_AUTOFLOWLABEL) {
2359 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2362 if (inp_flags & INP_TIMEWAIT) {
2363 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2366 if (inp_flags & INP_ONESBCAST) {
2367 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2370 if (inp_flags & INP_DROPPED) {
2371 db_printf("%sINP_DROPPED", comma ? ", " : "");
2374 if (inp_flags & INP_SOCKREF) {
2375 db_printf("%sINP_SOCKREF", comma ? ", " : "");
2378 if (inp_flags & IN6P_RFC2292) {
2379 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2382 if (inp_flags & IN6P_MTU) {
2383 db_printf("IN6P_MTU%s", comma ? ", " : "");
2389 db_print_inpvflag(u_char inp_vflag)
2394 if (inp_vflag & INP_IPV4) {
2395 db_printf("%sINP_IPV4", comma ? ", " : "");
2398 if (inp_vflag & INP_IPV6) {
2399 db_printf("%sINP_IPV6", comma ? ", " : "");
2402 if (inp_vflag & INP_IPV6PROTO) {
2403 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2409 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2412 db_print_indent(indent);
2413 db_printf("%s at %p\n", name, inp);
2417 db_print_indent(indent);
2418 db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2420 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2422 db_print_indent(indent);
2423 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n",
2424 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2426 db_print_indent(indent);
2427 db_printf("inp_label: %p inp_flags: 0x%x (",
2428 inp->inp_label, inp->inp_flags);
2429 db_print_inpflags(inp->inp_flags);
2432 db_print_indent(indent);
2433 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp,
2435 db_print_inpvflag(inp->inp_vflag);
2438 db_print_indent(indent);
2439 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n",
2440 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2442 db_print_indent(indent);
2444 if (inp->inp_vflag & INP_IPV6) {
2445 db_printf("in6p_options: %p in6p_outputopts: %p "
2446 "in6p_moptions: %p\n", inp->in6p_options,
2447 inp->in6p_outputopts, inp->in6p_moptions);
2448 db_printf("in6p_icmp6filt: %p in6p_cksum %d "
2449 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2454 db_printf("inp_ip_tos: %d inp_ip_options: %p "
2455 "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2456 inp->inp_options, inp->inp_moptions);
2459 db_print_indent(indent);
2460 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd,
2461 (uintmax_t)inp->inp_gencnt);
2464 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2469 db_printf("usage: show inpcb <addr>\n");
2472 inp = (struct inpcb *)addr;
2474 db_print_inpcb(inp, "inpcb", 0);