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"
48 #include <sys/param.h>
49 #include <sys/systm.h>
51 #include <sys/malloc.h>
53 #include <sys/callout.h>
54 #include <sys/domain.h>
55 #include <sys/protosw.h>
56 #include <sys/rmlock.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
61 #include <sys/refcount.h>
63 #include <sys/kernel.h>
64 #include <sys/sysctl.h>
73 #include <net/if_var.h>
74 #include <net/if_types.h>
75 #include <net/route.h>
76 #include <net/rss_config.h>
79 #if defined(INET) || defined(INET6)
80 #include <netinet/in.h>
81 #include <netinet/in_pcb.h>
82 #include <netinet/ip_var.h>
83 #include <netinet/tcp_var.h>
84 #include <netinet/udp.h>
85 #include <netinet/udp_var.h>
88 #include <netinet/in_var.h>
91 #include <netinet/ip6.h>
92 #include <netinet6/in6_pcb.h>
93 #include <netinet6/in6_var.h>
94 #include <netinet6/ip6_var.h>
99 #include <netipsec/ipsec.h>
100 #include <netipsec/key.h>
103 #include <security/mac/mac_framework.h>
105 static struct callout ipport_tick_callout;
108 * These configure the range of local port addresses assigned to
109 * "unspecified" outgoing connections/packets/whatever.
111 VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */
112 VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */
113 VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */
114 VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */
115 VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */
116 VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */
119 * Reserved ports accessible only to root. There are significant
120 * security considerations that must be accounted for when changing these,
121 * but the security benefits can be great. Please be careful.
123 VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */
124 VNET_DEFINE(int, ipport_reservedlow);
126 /* Variables dealing with random ephemeral port allocation. */
127 VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */
128 VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */
129 VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */
130 VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */
131 VNET_DEFINE(int, ipport_tcpallocs);
132 static VNET_DEFINE(int, ipport_tcplastcount);
134 #define V_ipport_tcplastcount VNET(ipport_tcplastcount)
136 static void in_pcbremlists(struct inpcb *inp);
138 static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
139 struct in_addr faddr, u_int fport_arg,
140 struct in_addr laddr, u_int lport_arg,
141 int lookupflags, struct ifnet *ifp);
143 #define RANGECHK(var, min, max) \
144 if ((var) < (min)) { (var) = (min); } \
145 else if ((var) > (max)) { (var) = (max); }
148 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
152 error = sysctl_handle_int(oidp, arg1, arg2, req);
154 RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
155 RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
156 RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
157 RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
158 RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
159 RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
166 static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
169 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
170 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
171 &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", "");
172 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
173 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
174 &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", "");
175 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first,
176 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
177 &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", "");
178 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last,
179 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
180 &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", "");
181 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
182 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
183 &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", "");
184 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
185 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
186 &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", "");
187 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
188 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
189 &VNET_NAME(ipport_reservedhigh), 0, "");
190 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
191 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
192 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized,
193 CTLFLAG_VNET | CTLFLAG_RW,
194 &VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
195 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps,
196 CTLFLAG_VNET | CTLFLAG_RW,
197 &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
198 "allocations before switching to a sequental one");
199 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime,
200 CTLFLAG_VNET | CTLFLAG_RW,
201 &VNET_NAME(ipport_randomtime), 0,
202 "Minimum time to keep sequental port "
203 "allocation before switching to a random one");
207 * in_pcb.c: manage the Protocol Control Blocks.
209 * NOTE: It is assumed that most of these functions will be called with
210 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
211 * functions often modify hash chains or addresses in pcbs.
215 * Initialize an inpcbinfo -- we should be able to reduce the number of
219 in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
220 struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
221 char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
222 uint32_t inpcbzone_flags, u_int hashfields)
225 INP_INFO_LOCK_INIT(pcbinfo, name);
226 INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */
227 INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist");
229 pcbinfo->ipi_vnet = curvnet;
231 pcbinfo->ipi_listhead = listhead;
232 LIST_INIT(pcbinfo->ipi_listhead);
233 pcbinfo->ipi_count = 0;
234 pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
235 &pcbinfo->ipi_hashmask);
236 pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
237 &pcbinfo->ipi_porthashmask);
239 in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
241 pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
242 NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
244 uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
245 uma_zone_set_warning(pcbinfo->ipi_zone,
246 "kern.ipc.maxsockets limit reached");
250 * Destroy an inpcbinfo.
253 in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
256 KASSERT(pcbinfo->ipi_count == 0,
257 ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
259 hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
260 hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
261 pcbinfo->ipi_porthashmask);
263 in_pcbgroup_destroy(pcbinfo);
265 uma_zdestroy(pcbinfo->ipi_zone);
266 INP_LIST_LOCK_DESTROY(pcbinfo);
267 INP_HASH_LOCK_DESTROY(pcbinfo);
268 INP_INFO_LOCK_DESTROY(pcbinfo);
272 * Allocate a PCB and associate it with the socket.
273 * On success return with the PCB locked.
276 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
282 if (pcbinfo == &V_tcbinfo) {
283 INP_INFO_RLOCK_ASSERT(pcbinfo);
285 INP_INFO_WLOCK_ASSERT(pcbinfo);
290 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
293 bzero(inp, inp_zero_size);
294 inp->inp_pcbinfo = pcbinfo;
295 inp->inp_socket = so;
296 inp->inp_cred = crhold(so->so_cred);
297 inp->inp_inc.inc_fibnum = so->so_fibnum;
299 error = mac_inpcb_init(inp, M_NOWAIT);
302 mac_inpcb_create(so, inp);
305 error = ipsec_init_policy(so, &inp->inp_sp);
308 mac_inpcb_destroy(inp);
314 if (INP_SOCKAF(so) == AF_INET6) {
315 inp->inp_vflag |= INP_IPV6PROTO;
317 inp->inp_flags |= IN6P_IPV6_V6ONLY;
321 INP_LIST_WLOCK(pcbinfo);
322 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
323 pcbinfo->ipi_count++;
324 so->so_pcb = (caddr_t)inp;
326 if (V_ip6_auto_flowlabel)
327 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
329 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
330 refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */
331 INP_LIST_WUNLOCK(pcbinfo);
332 #if defined(IPSEC) || defined(MAC)
335 crfree(inp->inp_cred);
336 uma_zfree(pcbinfo->ipi_zone, inp);
344 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
348 INP_WLOCK_ASSERT(inp);
349 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
351 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
353 anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
354 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
355 &inp->inp_lport, cred);
358 if (in_pcbinshash(inp) != 0) {
359 inp->inp_laddr.s_addr = INADDR_ANY;
364 inp->inp_flags |= INP_ANONPORT;
370 * Select a local port (number) to use.
372 #if defined(INET) || defined(INET6)
374 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
375 struct ucred *cred, int lookupflags)
377 struct inpcbinfo *pcbinfo;
378 struct inpcb *tmpinp;
379 unsigned short *lastport;
380 int count, dorandom, error;
381 u_short aux, first, last, lport;
383 struct in_addr laddr;
386 pcbinfo = inp->inp_pcbinfo;
389 * Because no actual state changes occur here, a global write lock on
390 * the pcbinfo isn't required.
392 INP_LOCK_ASSERT(inp);
393 INP_HASH_LOCK_ASSERT(pcbinfo);
395 if (inp->inp_flags & INP_HIGHPORT) {
396 first = V_ipport_hifirstauto; /* sysctl */
397 last = V_ipport_hilastauto;
398 lastport = &pcbinfo->ipi_lasthi;
399 } else if (inp->inp_flags & INP_LOWPORT) {
400 error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
403 first = V_ipport_lowfirstauto; /* 1023 */
404 last = V_ipport_lowlastauto; /* 600 */
405 lastport = &pcbinfo->ipi_lastlow;
407 first = V_ipport_firstauto; /* sysctl */
408 last = V_ipport_lastauto;
409 lastport = &pcbinfo->ipi_lastport;
412 * For UDP(-Lite), use random port allocation as long as the user
413 * allows it. For TCP (and as of yet unknown) connections,
414 * use random port allocation only if the user allows it AND
415 * ipport_tick() allows it.
417 if (V_ipport_randomized &&
418 (!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
419 pcbinfo == &V_ulitecbinfo))
424 * It makes no sense to do random port allocation if
425 * we have the only port available.
429 /* Make sure to not include UDP(-Lite) packets in the count. */
430 if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
431 V_ipport_tcpallocs++;
433 * Instead of having two loops further down counting up or down
434 * make sure that first is always <= last and go with only one
435 * code path implementing all logic.
444 /* Make the compiler happy. */
446 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
447 KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
452 tmpinp = NULL; /* Make compiler happy. */
456 *lastport = first + (arc4random() % (last - first));
458 count = last - first;
461 if (count-- < 0) /* completely used? */
462 return (EADDRNOTAVAIL);
464 if (*lastport < first || *lastport > last)
466 lport = htons(*lastport);
469 if ((inp->inp_vflag & INP_IPV6) != 0)
470 tmpinp = in6_pcblookup_local(pcbinfo,
471 &inp->in6p_laddr, lport, lookupflags, cred);
473 #if defined(INET) && defined(INET6)
477 tmpinp = in_pcblookup_local(pcbinfo, laddr,
478 lport, lookupflags, cred);
480 } while (tmpinp != NULL);
483 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
484 laddrp->s_addr = laddr.s_addr;
492 * Return cached socket options.
495 inp_so_options(const struct inpcb *inp)
501 if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
502 so_options |= SO_REUSEPORT;
503 if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
504 so_options |= SO_REUSEADDR;
507 #endif /* INET || INET6 */
510 * Check if a new BINDMULTI socket is allowed to be created.
512 * ni points to the new inp.
513 * oi points to the exisitng inp.
515 * This checks whether the existing inp also has BINDMULTI and
516 * whether the credentials match.
519 in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi)
521 /* Check permissions match */
522 if ((ni->inp_flags2 & INP_BINDMULTI) &&
523 (ni->inp_cred->cr_uid !=
524 oi->inp_cred->cr_uid))
527 /* Check the existing inp has BINDMULTI set */
528 if ((ni->inp_flags2 & INP_BINDMULTI) &&
529 ((oi->inp_flags2 & INP_BINDMULTI) == 0))
533 * We're okay - either INP_BINDMULTI isn't set on ni, or
534 * it is and it matches the checks.
541 * Set up a bind operation on a PCB, performing port allocation
542 * as required, but do not actually modify the PCB. Callers can
543 * either complete the bind by setting inp_laddr/inp_lport and
544 * calling in_pcbinshash(), or they can just use the resulting
545 * port and address to authorise the sending of a once-off packet.
547 * On error, the values of *laddrp and *lportp are not changed.
550 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
551 u_short *lportp, struct ucred *cred)
553 struct socket *so = inp->inp_socket;
554 struct sockaddr_in *sin;
555 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
556 struct in_addr laddr;
558 int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
562 * No state changes, so read locks are sufficient here.
564 INP_LOCK_ASSERT(inp);
565 INP_HASH_LOCK_ASSERT(pcbinfo);
567 if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
568 return (EADDRNOTAVAIL);
569 laddr.s_addr = *laddrp;
570 if (nam != NULL && laddr.s_addr != INADDR_ANY)
572 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
573 lookupflags = INPLOOKUP_WILDCARD;
575 if ((error = prison_local_ip4(cred, &laddr)) != 0)
578 sin = (struct sockaddr_in *)nam;
579 if (nam->sa_len != sizeof (*sin))
583 * We should check the family, but old programs
584 * incorrectly fail to initialize it.
586 if (sin->sin_family != AF_INET)
587 return (EAFNOSUPPORT);
589 error = prison_local_ip4(cred, &sin->sin_addr);
592 if (sin->sin_port != *lportp) {
593 /* Don't allow the port to change. */
596 lport = sin->sin_port;
598 /* NB: lport is left as 0 if the port isn't being changed. */
599 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
601 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
602 * allow complete duplication of binding if
603 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
604 * and a multicast address is bound on both
605 * new and duplicated sockets.
607 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
608 reuseport = SO_REUSEADDR|SO_REUSEPORT;
609 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
610 sin->sin_port = 0; /* yech... */
611 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
613 * Is the address a local IP address?
614 * If INP_BINDANY is set, then the socket may be bound
615 * to any endpoint address, local or not.
617 if ((inp->inp_flags & INP_BINDANY) == 0 &&
618 ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
619 return (EADDRNOTAVAIL);
621 laddr = sin->sin_addr;
627 if (ntohs(lport) <= V_ipport_reservedhigh &&
628 ntohs(lport) >= V_ipport_reservedlow &&
629 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
632 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
633 priv_check_cred(inp->inp_cred,
634 PRIV_NETINET_REUSEPORT, 0) != 0) {
635 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
636 lport, INPLOOKUP_WILDCARD, cred);
639 * This entire block sorely needs a rewrite.
642 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
643 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
644 (so->so_type != SOCK_STREAM ||
645 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
646 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
647 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
648 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
649 (inp->inp_cred->cr_uid !=
650 t->inp_cred->cr_uid))
654 * If the socket is a BINDMULTI socket, then
655 * the credentials need to match and the
656 * original socket also has to have been bound
659 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
662 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
663 lport, lookupflags, cred);
664 if (t && (t->inp_flags & INP_TIMEWAIT)) {
666 * XXXRW: If an incpb has had its timewait
667 * state recycled, we treat the address as
668 * being in use (for now). This is better
669 * than a panic, but not desirable.
673 (reuseport & tw->tw_so_options) == 0)
676 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
677 (reuseport & inp_so_options(t)) == 0) {
679 if (ntohl(sin->sin_addr.s_addr) !=
681 ntohl(t->inp_laddr.s_addr) !=
683 (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
684 (t->inp_vflag & INP_IPV6PROTO) == 0)
687 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
695 error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
700 *laddrp = laddr.s_addr;
706 * Connect from a socket to a specified address.
707 * Both address and port must be specified in argument sin.
708 * If don't have a local address for this socket yet,
712 in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
713 struct ucred *cred, struct mbuf *m)
715 u_short lport, fport;
716 in_addr_t laddr, faddr;
719 INP_WLOCK_ASSERT(inp);
720 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
722 lport = inp->inp_lport;
723 laddr = inp->inp_laddr.s_addr;
724 anonport = (lport == 0);
725 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
730 /* Do the initial binding of the local address if required. */
731 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
732 inp->inp_lport = lport;
733 inp->inp_laddr.s_addr = laddr;
734 if (in_pcbinshash(inp) != 0) {
735 inp->inp_laddr.s_addr = INADDR_ANY;
741 /* Commit the remaining changes. */
742 inp->inp_lport = lport;
743 inp->inp_laddr.s_addr = laddr;
744 inp->inp_faddr.s_addr = faddr;
745 inp->inp_fport = fport;
746 in_pcbrehash_mbuf(inp, m);
749 inp->inp_flags |= INP_ANONPORT;
754 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
757 return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
761 * Do proper source address selection on an unbound socket in case
762 * of connect. Take jails into account as well.
765 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
770 struct sockaddr_in *sin;
774 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
777 * Bypass source address selection and use the primary jail IP
780 if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
784 bzero(&sro, sizeof(sro));
786 sin = (struct sockaddr_in *)&sro.ro_dst;
787 sin->sin_family = AF_INET;
788 sin->sin_len = sizeof(struct sockaddr_in);
789 sin->sin_addr.s_addr = faddr->s_addr;
792 * If route is known our src addr is taken from the i/f,
795 * Find out route to destination.
797 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
798 in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
801 * If we found a route, use the address corresponding to
802 * the outgoing interface.
804 * Otherwise assume faddr is reachable on a directly connected
805 * network and try to find a corresponding interface to take
806 * the source address from.
808 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
809 struct in_ifaddr *ia;
812 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin,
813 inp->inp_socket->so_fibnum));
815 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0,
816 inp->inp_socket->so_fibnum));
822 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
823 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
824 ifa_free(&ia->ia_ifa);
829 ifa_free(&ia->ia_ifa);
832 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
835 if (sa->sa_family != AF_INET)
837 sin = (struct sockaddr_in *)sa;
838 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
839 ia = (struct in_ifaddr *)ifa;
844 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
845 IF_ADDR_RUNLOCK(ifp);
848 IF_ADDR_RUNLOCK(ifp);
850 /* 3. As a last resort return the 'default' jail address. */
851 error = prison_get_ip4(cred, laddr);
856 * If the outgoing interface on the route found is not
857 * a loopback interface, use the address from that interface.
858 * In case of jails do those three steps:
859 * 1. check if the interface address belongs to the jail. If so use it.
860 * 2. check if we have any address on the outgoing interface
861 * belonging to this jail. If so use it.
862 * 3. as a last resort return the 'default' jail address.
864 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
865 struct in_ifaddr *ia;
868 /* If not jailed, use the default returned. */
869 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
870 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
871 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
876 /* 1. Check if the iface address belongs to the jail. */
877 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
878 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
879 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
880 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
885 * 2. Check if we have any address on the outgoing interface
886 * belonging to this jail.
889 ifp = sro.ro_rt->rt_ifp;
891 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
893 if (sa->sa_family != AF_INET)
895 sin = (struct sockaddr_in *)sa;
896 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
897 ia = (struct in_ifaddr *)ifa;
902 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
903 IF_ADDR_RUNLOCK(ifp);
906 IF_ADDR_RUNLOCK(ifp);
908 /* 3. As a last resort return the 'default' jail address. */
909 error = prison_get_ip4(cred, laddr);
914 * The outgoing interface is marked with 'loopback net', so a route
915 * to ourselves is here.
916 * Try to find the interface of the destination address and then
917 * take the address from there. That interface is not necessarily
918 * a loopback interface.
919 * In case of jails, check that it is an address of the jail
920 * and if we cannot find, fall back to the 'default' jail address.
922 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
923 struct sockaddr_in sain;
924 struct in_ifaddr *ia;
926 bzero(&sain, sizeof(struct sockaddr_in));
927 sain.sin_family = AF_INET;
928 sain.sin_len = sizeof(struct sockaddr_in);
929 sain.sin_addr.s_addr = faddr->s_addr;
931 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain),
932 inp->inp_socket->so_fibnum));
934 ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0,
935 inp->inp_socket->so_fibnum));
937 ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
939 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
944 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
945 ifa_free(&ia->ia_ifa);
954 ifa_free(&ia->ia_ifa);
957 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
960 if (sa->sa_family != AF_INET)
962 sin = (struct sockaddr_in *)sa;
963 if (prison_check_ip4(cred,
964 &sin->sin_addr) == 0) {
965 ia = (struct in_ifaddr *)ifa;
970 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
971 IF_ADDR_RUNLOCK(ifp);
974 IF_ADDR_RUNLOCK(ifp);
977 /* 3. As a last resort return the 'default' jail address. */
978 error = prison_get_ip4(cred, laddr);
983 if (sro.ro_rt != NULL)
989 * Set up for a connect from a socket to the specified address.
990 * On entry, *laddrp and *lportp should contain the current local
991 * address and port for the PCB; these are updated to the values
992 * that should be placed in inp_laddr and inp_lport to complete
995 * On success, *faddrp and *fportp will be set to the remote address
996 * and port. These are not updated in the error case.
998 * If the operation fails because the connection already exists,
999 * *oinpp will be set to the PCB of that connection so that the
1000 * caller can decide to override it. In all other cases, *oinpp
1004 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
1005 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
1006 struct inpcb **oinpp, struct ucred *cred)
1008 struct rm_priotracker in_ifa_tracker;
1009 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1010 struct in_ifaddr *ia;
1012 struct in_addr laddr, faddr;
1013 u_short lport, fport;
1017 * Because a global state change doesn't actually occur here, a read
1018 * lock is sufficient.
1020 INP_LOCK_ASSERT(inp);
1021 INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
1025 if (nam->sa_len != sizeof (*sin))
1027 if (sin->sin_family != AF_INET)
1028 return (EAFNOSUPPORT);
1029 if (sin->sin_port == 0)
1030 return (EADDRNOTAVAIL);
1031 laddr.s_addr = *laddrp;
1033 faddr = sin->sin_addr;
1034 fport = sin->sin_port;
1036 if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
1038 * If the destination address is INADDR_ANY,
1039 * use the primary local address.
1040 * If the supplied address is INADDR_BROADCAST,
1041 * and the primary interface supports broadcast,
1042 * choose the broadcast address for that interface.
1044 if (faddr.s_addr == INADDR_ANY) {
1045 IN_IFADDR_RLOCK(&in_ifa_tracker);
1047 IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
1048 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1050 (error = prison_get_ip4(cred, &faddr)) != 0)
1052 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
1053 IN_IFADDR_RLOCK(&in_ifa_tracker);
1054 if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
1056 faddr = satosin(&TAILQ_FIRST(
1057 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
1058 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1061 if (laddr.s_addr == INADDR_ANY) {
1062 error = in_pcbladdr(inp, &faddr, &laddr, cred);
1064 * If the destination address is multicast and an outgoing
1065 * interface has been set as a multicast option, prefer the
1066 * address of that interface as our source address.
1068 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
1069 inp->inp_moptions != NULL) {
1070 struct ip_moptions *imo;
1073 imo = inp->inp_moptions;
1074 if (imo->imo_multicast_ifp != NULL) {
1075 ifp = imo->imo_multicast_ifp;
1076 IN_IFADDR_RLOCK(&in_ifa_tracker);
1077 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1078 if ((ia->ia_ifp == ifp) &&
1080 prison_check_ip4(cred,
1081 &ia->ia_addr.sin_addr) == 0))
1085 error = EADDRNOTAVAIL;
1087 laddr = ia->ia_addr.sin_addr;
1090 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1096 oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1097 laddr, lport, 0, NULL);
1101 return (EADDRINUSE);
1104 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1109 *laddrp = laddr.s_addr;
1111 *faddrp = faddr.s_addr;
1117 in_pcbdisconnect(struct inpcb *inp)
1120 INP_WLOCK_ASSERT(inp);
1121 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1123 inp->inp_faddr.s_addr = INADDR_ANY;
1130 * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1131 * For most protocols, this will be invoked immediately prior to calling
1132 * in_pcbfree(). However, with TCP the inpcb may significantly outlive the
1133 * socket, in which case in_pcbfree() is deferred.
1136 in_pcbdetach(struct inpcb *inp)
1139 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1141 inp->inp_socket->so_pcb = NULL;
1142 inp->inp_socket = NULL;
1146 * in_pcbref() bumps the reference count on an inpcb in order to maintain
1147 * stability of an inpcb pointer despite the inpcb lock being released. This
1148 * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
1149 * but where the inpcb lock may already held, or when acquiring a reference
1152 * in_pcbref() should be used only to provide brief memory stability, and
1153 * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
1154 * garbage collect the inpcb if it has been in_pcbfree()'d from another
1155 * context. Until in_pcbrele() has returned that the inpcb is still valid,
1156 * lock and rele are the *only* safe operations that may be performed on the
1159 * While the inpcb will not be freed, releasing the inpcb lock means that the
1160 * connection's state may change, so the caller should be careful to
1161 * revalidate any cached state on reacquiring the lock. Drop the reference
1162 * using in_pcbrele().
1165 in_pcbref(struct inpcb *inp)
1168 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1170 refcount_acquire(&inp->inp_refcount);
1174 * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
1175 * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
1176 * return a flag indicating whether or not the inpcb remains valid. If it is
1177 * valid, we return with the inpcb lock held.
1179 * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
1180 * reference on an inpcb. Historically more work was done here (actually, in
1181 * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
1182 * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely
1183 * about memory stability (and continued use of the write lock).
1186 in_pcbrele_rlocked(struct inpcb *inp)
1188 struct inpcbinfo *pcbinfo;
1190 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1192 INP_RLOCK_ASSERT(inp);
1194 if (refcount_release(&inp->inp_refcount) == 0) {
1196 * If the inpcb has been freed, let the caller know, even if
1197 * this isn't the last reference.
1199 if (inp->inp_flags2 & INP_FREED) {
1206 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1209 pcbinfo = inp->inp_pcbinfo;
1210 uma_zfree(pcbinfo->ipi_zone, inp);
1215 in_pcbrele_wlocked(struct inpcb *inp)
1217 struct inpcbinfo *pcbinfo;
1219 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1221 INP_WLOCK_ASSERT(inp);
1223 if (refcount_release(&inp->inp_refcount) == 0) {
1225 * If the inpcb has been freed, let the caller know, even if
1226 * this isn't the last reference.
1228 if (inp->inp_flags2 & INP_FREED) {
1235 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1238 pcbinfo = inp->inp_pcbinfo;
1239 uma_zfree(pcbinfo->ipi_zone, inp);
1244 * Temporary wrapper.
1247 in_pcbrele(struct inpcb *inp)
1250 return (in_pcbrele_wlocked(inp));
1254 * Unconditionally schedule an inpcb to be freed by decrementing its
1255 * reference count, which should occur only after the inpcb has been detached
1256 * from its socket. If another thread holds a temporary reference (acquired
1257 * using in_pcbref()) then the free is deferred until that reference is
1258 * released using in_pcbrele(), but the inpcb is still unlocked. Almost all
1259 * work, including removal from global lists, is done in this context, where
1260 * the pcbinfo lock is held.
1263 in_pcbfree(struct inpcb *inp)
1265 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1267 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1270 if (pcbinfo == &V_tcbinfo) {
1271 INP_INFO_LOCK_ASSERT(pcbinfo);
1273 INP_INFO_WLOCK_ASSERT(pcbinfo);
1276 INP_WLOCK_ASSERT(inp);
1278 /* XXXRW: Do as much as possible here. */
1280 if (inp->inp_sp != NULL)
1281 ipsec_delete_pcbpolicy(inp);
1283 INP_LIST_WLOCK(pcbinfo);
1284 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1285 in_pcbremlists(inp);
1286 INP_LIST_WUNLOCK(pcbinfo);
1288 if (inp->inp_vflag & INP_IPV6PROTO) {
1289 ip6_freepcbopts(inp->in6p_outputopts);
1290 if (inp->in6p_moptions != NULL)
1291 ip6_freemoptions(inp->in6p_moptions);
1294 if (inp->inp_options)
1295 (void)m_free(inp->inp_options);
1297 if (inp->inp_moptions != NULL)
1298 inp_freemoptions(inp->inp_moptions);
1301 inp->inp_flags2 |= INP_FREED;
1302 crfree(inp->inp_cred);
1304 mac_inpcb_destroy(inp);
1306 if (!in_pcbrele_wlocked(inp))
1311 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1312 * port reservation, and preventing it from being returned by inpcb lookups.
1314 * It is used by TCP to mark an inpcb as unused and avoid future packet
1315 * delivery or event notification when a socket remains open but TCP has
1316 * closed. This might occur as a result of a shutdown()-initiated TCP close
1317 * or a RST on the wire, and allows the port binding to be reused while still
1318 * maintaining the invariant that so_pcb always points to a valid inpcb until
1321 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1322 * in_pcbnotifyall() and in_pcbpurgeif0()?
1325 in_pcbdrop(struct inpcb *inp)
1328 INP_WLOCK_ASSERT(inp);
1331 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1334 inp->inp_flags |= INP_DROPPED;
1335 if (inp->inp_flags & INP_INHASHLIST) {
1336 struct inpcbport *phd = inp->inp_phd;
1338 INP_HASH_WLOCK(inp->inp_pcbinfo);
1339 LIST_REMOVE(inp, inp_hash);
1340 LIST_REMOVE(inp, inp_portlist);
1341 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1342 LIST_REMOVE(phd, phd_hash);
1345 INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1346 inp->inp_flags &= ~INP_INHASHLIST;
1348 in_pcbgroup_remove(inp);
1355 * Common routines to return the socket addresses associated with inpcbs.
1358 in_sockaddr(in_port_t port, struct in_addr *addr_p)
1360 struct sockaddr_in *sin;
1362 sin = malloc(sizeof *sin, M_SONAME,
1364 sin->sin_family = AF_INET;
1365 sin->sin_len = sizeof(*sin);
1366 sin->sin_addr = *addr_p;
1367 sin->sin_port = port;
1369 return (struct sockaddr *)sin;
1373 in_getsockaddr(struct socket *so, struct sockaddr **nam)
1376 struct in_addr addr;
1379 inp = sotoinpcb(so);
1380 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1383 port = inp->inp_lport;
1384 addr = inp->inp_laddr;
1387 *nam = in_sockaddr(port, &addr);
1392 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1395 struct in_addr addr;
1398 inp = sotoinpcb(so);
1399 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1402 port = inp->inp_fport;
1403 addr = inp->inp_faddr;
1406 *nam = in_sockaddr(port, &addr);
1411 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1412 struct inpcb *(*notify)(struct inpcb *, int))
1414 struct inpcb *inp, *inp_temp;
1416 INP_INFO_WLOCK(pcbinfo);
1417 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1420 if ((inp->inp_vflag & INP_IPV4) == 0) {
1425 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1426 inp->inp_socket == NULL) {
1430 if ((*notify)(inp, errno))
1433 INP_INFO_WUNLOCK(pcbinfo);
1437 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1440 struct ip_moptions *imo;
1443 INP_INFO_WLOCK(pcbinfo);
1444 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1446 imo = inp->inp_moptions;
1447 if ((inp->inp_vflag & INP_IPV4) &&
1450 * Unselect the outgoing interface if it is being
1453 if (imo->imo_multicast_ifp == ifp)
1454 imo->imo_multicast_ifp = NULL;
1457 * Drop multicast group membership if we joined
1458 * through the interface being detached.
1460 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1462 if (imo->imo_membership[i]->inm_ifp == ifp) {
1463 in_delmulti(imo->imo_membership[i]);
1465 } else if (gap != 0)
1466 imo->imo_membership[i - gap] =
1467 imo->imo_membership[i];
1469 imo->imo_num_memberships -= gap;
1473 INP_INFO_WUNLOCK(pcbinfo);
1477 * Lookup a PCB based on the local address and port. Caller must hold the
1478 * hash lock. No inpcb locks or references are acquired.
1480 #define INP_LOOKUP_MAPPED_PCB_COST 3
1482 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1483 u_short lport, int lookupflags, struct ucred *cred)
1487 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1493 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1494 ("%s: invalid lookup flags %d", __func__, lookupflags));
1496 INP_HASH_LOCK_ASSERT(pcbinfo);
1498 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1499 struct inpcbhead *head;
1501 * Look for an unconnected (wildcard foreign addr) PCB that
1502 * matches the local address and port we're looking for.
1504 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1505 0, pcbinfo->ipi_hashmask)];
1506 LIST_FOREACH(inp, head, inp_hash) {
1508 /* XXX inp locking */
1509 if ((inp->inp_vflag & INP_IPV4) == 0)
1512 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1513 inp->inp_laddr.s_addr == laddr.s_addr &&
1514 inp->inp_lport == lport) {
1519 prison_equal_ip4(cred->cr_prison,
1520 inp->inp_cred->cr_prison))
1529 struct inpcbporthead *porthash;
1530 struct inpcbport *phd;
1531 struct inpcb *match = NULL;
1533 * Best fit PCB lookup.
1535 * First see if this local port is in use by looking on the
1538 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1539 pcbinfo->ipi_porthashmask)];
1540 LIST_FOREACH(phd, porthash, phd_hash) {
1541 if (phd->phd_port == lport)
1546 * Port is in use by one or more PCBs. Look for best
1549 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1552 !prison_equal_ip4(inp->inp_cred->cr_prison,
1556 /* XXX inp locking */
1557 if ((inp->inp_vflag & INP_IPV4) == 0)
1560 * We never select the PCB that has
1561 * INP_IPV6 flag and is bound to :: if
1562 * we have another PCB which is bound
1563 * to 0.0.0.0. If a PCB has the
1564 * INP_IPV6 flag, then we set its cost
1565 * higher than IPv4 only PCBs.
1567 * Note that the case only happens
1568 * when a socket is bound to ::, under
1569 * the condition that the use of the
1570 * mapped address is allowed.
1572 if ((inp->inp_vflag & INP_IPV6) != 0)
1573 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1575 if (inp->inp_faddr.s_addr != INADDR_ANY)
1577 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1578 if (laddr.s_addr == INADDR_ANY)
1580 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1583 if (laddr.s_addr != INADDR_ANY)
1586 if (wildcard < matchwild) {
1588 matchwild = wildcard;
1597 #undef INP_LOOKUP_MAPPED_PCB_COST
1601 * Lookup PCB in hash list, using pcbgroup tables.
1603 static struct inpcb *
1604 in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
1605 struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
1606 u_int lport_arg, int lookupflags, struct ifnet *ifp)
1608 struct inpcbhead *head;
1609 struct inpcb *inp, *tmpinp;
1610 u_short fport = fport_arg, lport = lport_arg;
1613 * First look for an exact match.
1616 INP_GROUP_LOCK(pcbgroup);
1617 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1618 pcbgroup->ipg_hashmask)];
1619 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1621 /* XXX inp locking */
1622 if ((inp->inp_vflag & INP_IPV4) == 0)
1625 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1626 inp->inp_laddr.s_addr == laddr.s_addr &&
1627 inp->inp_fport == fport &&
1628 inp->inp_lport == lport) {
1630 * XXX We should be able to directly return
1631 * the inp here, without any checks.
1632 * Well unless both bound with SO_REUSEPORT?
1634 if (prison_flag(inp->inp_cred, PR_IP4))
1640 if (tmpinp != NULL) {
1647 * For incoming connections, we may wish to do a wildcard
1648 * match for an RSS-local socket.
1650 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1651 struct inpcb *local_wild = NULL, *local_exact = NULL;
1653 struct inpcb *local_wild_mapped = NULL;
1655 struct inpcb *jail_wild = NULL;
1656 struct inpcbhead *head;
1660 * Order of socket selection - we always prefer jails.
1661 * 1. jailed, non-wild.
1663 * 3. non-jailed, non-wild.
1664 * 4. non-jailed, wild.
1667 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY,
1668 lport, 0, pcbgroup->ipg_hashmask)];
1669 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1671 /* XXX inp locking */
1672 if ((inp->inp_vflag & INP_IPV4) == 0)
1675 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1676 inp->inp_lport != lport)
1679 injail = prison_flag(inp->inp_cred, PR_IP4);
1681 if (prison_check_ip4(inp->inp_cred,
1685 if (local_exact != NULL)
1689 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1694 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1696 /* XXX inp locking, NULL check */
1697 if (inp->inp_vflag & INP_IPV6PROTO)
1698 local_wild_mapped = inp;
1706 } /* LIST_FOREACH */
1715 inp = local_wild_mapped;
1723 * Then look for a wildcard match, if requested.
1725 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1726 struct inpcb *local_wild = NULL, *local_exact = NULL;
1728 struct inpcb *local_wild_mapped = NULL;
1730 struct inpcb *jail_wild = NULL;
1731 struct inpcbhead *head;
1735 * Order of socket selection - we always prefer jails.
1736 * 1. jailed, non-wild.
1738 * 3. non-jailed, non-wild.
1739 * 4. non-jailed, wild.
1741 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
1742 0, pcbinfo->ipi_wildmask)];
1743 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1745 /* XXX inp locking */
1746 if ((inp->inp_vflag & INP_IPV4) == 0)
1749 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1750 inp->inp_lport != lport)
1753 injail = prison_flag(inp->inp_cred, PR_IP4);
1755 if (prison_check_ip4(inp->inp_cred,
1759 if (local_exact != NULL)
1763 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1768 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1770 /* XXX inp locking, NULL check */
1771 if (inp->inp_vflag & INP_IPV6PROTO)
1772 local_wild_mapped = inp;
1780 } /* LIST_FOREACH */
1788 inp = local_wild_mapped;
1792 } /* if (lookupflags & INPLOOKUP_WILDCARD) */
1793 INP_GROUP_UNLOCK(pcbgroup);
1798 INP_GROUP_UNLOCK(pcbgroup);
1799 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1801 if (in_pcbrele_wlocked(inp))
1803 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1805 if (in_pcbrele_rlocked(inp))
1808 panic("%s: locking bug", __func__);
1811 #endif /* PCBGROUP */
1814 * Lookup PCB in hash list, using pcbinfo tables. This variation assumes
1815 * that the caller has locked the hash list, and will not perform any further
1816 * locking or reference operations on either the hash list or the connection.
1818 static struct inpcb *
1819 in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1820 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1823 struct inpcbhead *head;
1824 struct inpcb *inp, *tmpinp;
1825 u_short fport = fport_arg, lport = lport_arg;
1827 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1828 ("%s: invalid lookup flags %d", __func__, lookupflags));
1830 INP_HASH_LOCK_ASSERT(pcbinfo);
1833 * First look for an exact match.
1836 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1837 pcbinfo->ipi_hashmask)];
1838 LIST_FOREACH(inp, head, inp_hash) {
1840 /* XXX inp locking */
1841 if ((inp->inp_vflag & INP_IPV4) == 0)
1844 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1845 inp->inp_laddr.s_addr == laddr.s_addr &&
1846 inp->inp_fport == fport &&
1847 inp->inp_lport == lport) {
1849 * XXX We should be able to directly return
1850 * the inp here, without any checks.
1851 * Well unless both bound with SO_REUSEPORT?
1853 if (prison_flag(inp->inp_cred, PR_IP4))
1863 * Then look for a wildcard match, if requested.
1865 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1866 struct inpcb *local_wild = NULL, *local_exact = NULL;
1868 struct inpcb *local_wild_mapped = NULL;
1870 struct inpcb *jail_wild = NULL;
1874 * Order of socket selection - we always prefer jails.
1875 * 1. jailed, non-wild.
1877 * 3. non-jailed, non-wild.
1878 * 4. non-jailed, wild.
1881 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1882 0, pcbinfo->ipi_hashmask)];
1883 LIST_FOREACH(inp, head, inp_hash) {
1885 /* XXX inp locking */
1886 if ((inp->inp_vflag & INP_IPV4) == 0)
1889 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1890 inp->inp_lport != lport)
1893 injail = prison_flag(inp->inp_cred, PR_IP4);
1895 if (prison_check_ip4(inp->inp_cred,
1899 if (local_exact != NULL)
1903 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1908 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1910 /* XXX inp locking, NULL check */
1911 if (inp->inp_vflag & INP_IPV6PROTO)
1912 local_wild_mapped = inp;
1920 } /* LIST_FOREACH */
1921 if (jail_wild != NULL)
1923 if (local_exact != NULL)
1924 return (local_exact);
1925 if (local_wild != NULL)
1926 return (local_wild);
1928 if (local_wild_mapped != NULL)
1929 return (local_wild_mapped);
1931 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1937 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1938 * hash list lock, and will return the inpcb locked (i.e., requires
1939 * INPLOOKUP_LOCKPCB).
1941 static struct inpcb *
1942 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1943 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1948 INP_HASH_RLOCK(pcbinfo);
1949 inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1950 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1953 INP_HASH_RUNLOCK(pcbinfo);
1954 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1956 if (in_pcbrele_wlocked(inp))
1958 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1960 if (in_pcbrele_rlocked(inp))
1963 panic("%s: locking bug", __func__);
1965 INP_HASH_RUNLOCK(pcbinfo);
1970 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1971 * from which a pre-calculated hash value may be extracted.
1973 * Possibly more of this logic should be in in_pcbgroup.c.
1976 in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1977 struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1979 #if defined(PCBGROUP) && !defined(RSS)
1980 struct inpcbgroup *pcbgroup;
1983 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1984 ("%s: invalid lookup flags %d", __func__, lookupflags));
1985 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1986 ("%s: LOCKPCB not set", __func__));
1989 * When not using RSS, use connection groups in preference to the
1990 * reservation table when looking up 4-tuples. When using RSS, just
1991 * use the reservation table, due to the cost of the Toeplitz hash
1994 * XXXRW: This policy belongs in the pcbgroup code, as in principle
1995 * we could be doing RSS with a non-Toeplitz hash that is affordable
1998 #if defined(PCBGROUP) && !defined(RSS)
1999 if (in_pcbgroup_enabled(pcbinfo)) {
2000 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
2002 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
2003 laddr, lport, lookupflags, ifp));
2006 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2011 in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2012 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2013 struct ifnet *ifp, struct mbuf *m)
2016 struct inpcbgroup *pcbgroup;
2019 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2020 ("%s: invalid lookup flags %d", __func__, lookupflags));
2021 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2022 ("%s: LOCKPCB not set", __func__));
2026 * If we can use a hardware-generated hash to look up the connection
2027 * group, use that connection group to find the inpcb. Otherwise
2028 * fall back on a software hash -- or the reservation table if we're
2031 * XXXRW: As above, that policy belongs in the pcbgroup code.
2033 if (in_pcbgroup_enabled(pcbinfo) &&
2034 !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
2035 pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
2036 m->m_pkthdr.flowid);
2037 if (pcbgroup != NULL)
2038 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
2039 fport, laddr, lport, lookupflags, ifp));
2041 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
2043 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
2044 laddr, lport, lookupflags, ifp));
2048 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2054 * Insert PCB onto various hash lists.
2057 in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
2059 struct inpcbhead *pcbhash;
2060 struct inpcbporthead *pcbporthash;
2061 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2062 struct inpcbport *phd;
2063 u_int32_t hashkey_faddr;
2065 INP_WLOCK_ASSERT(inp);
2066 INP_HASH_WLOCK_ASSERT(pcbinfo);
2068 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2069 ("in_pcbinshash: INP_INHASHLIST"));
2072 if (inp->inp_vflag & INP_IPV6)
2073 hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2076 hashkey_faddr = inp->inp_faddr.s_addr;
2078 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2079 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
2081 pcbporthash = &pcbinfo->ipi_porthashbase[
2082 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2085 * Go through port list and look for a head for this lport.
2087 LIST_FOREACH(phd, pcbporthash, phd_hash) {
2088 if (phd->phd_port == inp->inp_lport)
2092 * If none exists, malloc one and tack it on.
2095 phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
2097 return (ENOBUFS); /* XXX */
2099 phd->phd_port = inp->inp_lport;
2100 LIST_INIT(&phd->phd_pcblist);
2101 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
2104 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
2105 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
2106 inp->inp_flags |= INP_INHASHLIST;
2108 if (do_pcbgroup_update)
2109 in_pcbgroup_update(inp);
2115 * For now, there are two public interfaces to insert an inpcb into the hash
2116 * lists -- one that does update pcbgroups, and one that doesn't. The latter
2117 * is used only in the TCP syncache, where in_pcbinshash is called before the
2118 * full 4-tuple is set for the inpcb, and we don't want to install in the
2119 * pcbgroup until later.
2121 * XXXRW: This seems like a misfeature. in_pcbinshash should always update
2122 * connection groups, and partially initialised inpcbs should not be exposed
2123 * to either reservation hash tables or pcbgroups.
2126 in_pcbinshash(struct inpcb *inp)
2129 return (in_pcbinshash_internal(inp, 1));
2133 in_pcbinshash_nopcbgroup(struct inpcb *inp)
2136 return (in_pcbinshash_internal(inp, 0));
2140 * Move PCB to the proper hash bucket when { faddr, fport } have been
2141 * changed. NOTE: This does not handle the case of the lport changing (the
2142 * hashed port list would have to be updated as well), so the lport must
2143 * not change after in_pcbinshash() has been called.
2146 in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
2148 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2149 struct inpcbhead *head;
2150 u_int32_t hashkey_faddr;
2152 INP_WLOCK_ASSERT(inp);
2153 INP_HASH_WLOCK_ASSERT(pcbinfo);
2155 KASSERT(inp->inp_flags & INP_INHASHLIST,
2156 ("in_pcbrehash: !INP_INHASHLIST"));
2159 if (inp->inp_vflag & INP_IPV6)
2160 hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2163 hashkey_faddr = inp->inp_faddr.s_addr;
2165 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2166 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
2168 LIST_REMOVE(inp, inp_hash);
2169 LIST_INSERT_HEAD(head, inp, inp_hash);
2173 in_pcbgroup_update_mbuf(inp, m);
2175 in_pcbgroup_update(inp);
2180 in_pcbrehash(struct inpcb *inp)
2183 in_pcbrehash_mbuf(inp, NULL);
2187 * Remove PCB from various lists.
2190 in_pcbremlists(struct inpcb *inp)
2192 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2195 if (pcbinfo == &V_tcbinfo) {
2196 INP_INFO_RLOCK_ASSERT(pcbinfo);
2198 INP_INFO_WLOCK_ASSERT(pcbinfo);
2202 INP_WLOCK_ASSERT(inp);
2203 INP_LIST_WLOCK_ASSERT(pcbinfo);
2205 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2206 if (inp->inp_flags & INP_INHASHLIST) {
2207 struct inpcbport *phd = inp->inp_phd;
2209 INP_HASH_WLOCK(pcbinfo);
2210 LIST_REMOVE(inp, inp_hash);
2211 LIST_REMOVE(inp, inp_portlist);
2212 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2213 LIST_REMOVE(phd, phd_hash);
2216 INP_HASH_WUNLOCK(pcbinfo);
2217 inp->inp_flags &= ~INP_INHASHLIST;
2219 LIST_REMOVE(inp, inp_list);
2220 pcbinfo->ipi_count--;
2222 in_pcbgroup_remove(inp);
2227 * A set label operation has occurred at the socket layer, propagate the
2228 * label change into the in_pcb for the socket.
2231 in_pcbsosetlabel(struct socket *so)
2236 inp = sotoinpcb(so);
2237 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2241 mac_inpcb_sosetlabel(so, inp);
2248 * ipport_tick runs once per second, determining if random port allocation
2249 * should be continued. If more than ipport_randomcps ports have been
2250 * allocated in the last second, then we return to sequential port
2251 * allocation. We return to random allocation only once we drop below
2252 * ipport_randomcps for at least ipport_randomtime seconds.
2255 ipport_tick(void *xtp)
2257 VNET_ITERATOR_DECL(vnet_iter);
2259 VNET_LIST_RLOCK_NOSLEEP();
2260 VNET_FOREACH(vnet_iter) {
2261 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */
2262 if (V_ipport_tcpallocs <=
2263 V_ipport_tcplastcount + V_ipport_randomcps) {
2264 if (V_ipport_stoprandom > 0)
2265 V_ipport_stoprandom--;
2267 V_ipport_stoprandom = V_ipport_randomtime;
2268 V_ipport_tcplastcount = V_ipport_tcpallocs;
2271 VNET_LIST_RUNLOCK_NOSLEEP();
2272 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
2279 callout_stop(&ipport_tick_callout);
2283 * The ipport_callout should start running at about the time we attach the
2284 * inet or inet6 domains.
2287 ipport_tick_init(const void *unused __unused)
2290 /* Start ipport_tick. */
2291 callout_init(&ipport_tick_callout, 1);
2292 callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2293 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2294 SHUTDOWN_PRI_DEFAULT);
2296 SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2297 ipport_tick_init, NULL);
2300 inp_wlock(struct inpcb *inp)
2307 inp_wunlock(struct inpcb *inp)
2314 inp_rlock(struct inpcb *inp)
2321 inp_runlock(struct inpcb *inp)
2329 inp_lock_assert(struct inpcb *inp)
2332 INP_WLOCK_ASSERT(inp);
2336 inp_unlock_assert(struct inpcb *inp)
2339 INP_UNLOCK_ASSERT(inp);
2344 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
2348 INP_INFO_WLOCK(&V_tcbinfo);
2349 LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
2354 INP_INFO_WUNLOCK(&V_tcbinfo);
2358 inp_inpcbtosocket(struct inpcb *inp)
2361 INP_WLOCK_ASSERT(inp);
2362 return (inp->inp_socket);
2366 inp_inpcbtotcpcb(struct inpcb *inp)
2369 INP_WLOCK_ASSERT(inp);
2370 return ((struct tcpcb *)inp->inp_ppcb);
2374 inp_ip_tos_get(const struct inpcb *inp)
2377 return (inp->inp_ip_tos);
2381 inp_ip_tos_set(struct inpcb *inp, int val)
2384 inp->inp_ip_tos = val;
2388 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
2389 uint32_t *faddr, uint16_t *fp)
2392 INP_LOCK_ASSERT(inp);
2393 *laddr = inp->inp_laddr.s_addr;
2394 *faddr = inp->inp_faddr.s_addr;
2395 *lp = inp->inp_lport;
2396 *fp = inp->inp_fport;
2400 so_sotoinpcb(struct socket *so)
2403 return (sotoinpcb(so));
2407 so_sototcpcb(struct socket *so)
2410 return (sototcpcb(so));
2415 db_print_indent(int indent)
2419 for (i = 0; i < indent; i++)
2424 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2426 char faddr_str[48], laddr_str[48];
2428 db_print_indent(indent);
2429 db_printf("%s at %p\n", name, inc);
2434 if (inc->inc_flags & INC_ISIPV6) {
2436 ip6_sprintf(laddr_str, &inc->inc6_laddr);
2437 ip6_sprintf(faddr_str, &inc->inc6_faddr);
2442 inet_ntoa_r(inc->inc_laddr, laddr_str);
2443 inet_ntoa_r(inc->inc_faddr, faddr_str);
2445 db_print_indent(indent);
2446 db_printf("inc_laddr %s inc_lport %u\n", laddr_str,
2447 ntohs(inc->inc_lport));
2448 db_print_indent(indent);
2449 db_printf("inc_faddr %s inc_fport %u\n", faddr_str,
2450 ntohs(inc->inc_fport));
2454 db_print_inpflags(int inp_flags)
2459 if (inp_flags & INP_RECVOPTS) {
2460 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2463 if (inp_flags & INP_RECVRETOPTS) {
2464 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2467 if (inp_flags & INP_RECVDSTADDR) {
2468 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2471 if (inp_flags & INP_HDRINCL) {
2472 db_printf("%sINP_HDRINCL", comma ? ", " : "");
2475 if (inp_flags & INP_HIGHPORT) {
2476 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2479 if (inp_flags & INP_LOWPORT) {
2480 db_printf("%sINP_LOWPORT", comma ? ", " : "");
2483 if (inp_flags & INP_ANONPORT) {
2484 db_printf("%sINP_ANONPORT", comma ? ", " : "");
2487 if (inp_flags & INP_RECVIF) {
2488 db_printf("%sINP_RECVIF", comma ? ", " : "");
2491 if (inp_flags & INP_MTUDISC) {
2492 db_printf("%sINP_MTUDISC", comma ? ", " : "");
2495 if (inp_flags & INP_RECVTTL) {
2496 db_printf("%sINP_RECVTTL", comma ? ", " : "");
2499 if (inp_flags & INP_DONTFRAG) {
2500 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2503 if (inp_flags & INP_RECVTOS) {
2504 db_printf("%sINP_RECVTOS", comma ? ", " : "");
2507 if (inp_flags & IN6P_IPV6_V6ONLY) {
2508 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2511 if (inp_flags & IN6P_PKTINFO) {
2512 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2515 if (inp_flags & IN6P_HOPLIMIT) {
2516 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2519 if (inp_flags & IN6P_HOPOPTS) {
2520 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2523 if (inp_flags & IN6P_DSTOPTS) {
2524 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2527 if (inp_flags & IN6P_RTHDR) {
2528 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2531 if (inp_flags & IN6P_RTHDRDSTOPTS) {
2532 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2535 if (inp_flags & IN6P_TCLASS) {
2536 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2539 if (inp_flags & IN6P_AUTOFLOWLABEL) {
2540 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2543 if (inp_flags & INP_TIMEWAIT) {
2544 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2547 if (inp_flags & INP_ONESBCAST) {
2548 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2551 if (inp_flags & INP_DROPPED) {
2552 db_printf("%sINP_DROPPED", comma ? ", " : "");
2555 if (inp_flags & INP_SOCKREF) {
2556 db_printf("%sINP_SOCKREF", comma ? ", " : "");
2559 if (inp_flags & IN6P_RFC2292) {
2560 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2563 if (inp_flags & IN6P_MTU) {
2564 db_printf("IN6P_MTU%s", comma ? ", " : "");
2570 db_print_inpvflag(u_char inp_vflag)
2575 if (inp_vflag & INP_IPV4) {
2576 db_printf("%sINP_IPV4", comma ? ", " : "");
2579 if (inp_vflag & INP_IPV6) {
2580 db_printf("%sINP_IPV6", comma ? ", " : "");
2583 if (inp_vflag & INP_IPV6PROTO) {
2584 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2590 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2593 db_print_indent(indent);
2594 db_printf("%s at %p\n", name, inp);
2598 db_print_indent(indent);
2599 db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2601 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2603 db_print_indent(indent);
2604 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n",
2605 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2607 db_print_indent(indent);
2608 db_printf("inp_label: %p inp_flags: 0x%x (",
2609 inp->inp_label, inp->inp_flags);
2610 db_print_inpflags(inp->inp_flags);
2613 db_print_indent(indent);
2614 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp,
2616 db_print_inpvflag(inp->inp_vflag);
2619 db_print_indent(indent);
2620 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n",
2621 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2623 db_print_indent(indent);
2625 if (inp->inp_vflag & INP_IPV6) {
2626 db_printf("in6p_options: %p in6p_outputopts: %p "
2627 "in6p_moptions: %p\n", inp->in6p_options,
2628 inp->in6p_outputopts, inp->in6p_moptions);
2629 db_printf("in6p_icmp6filt: %p in6p_cksum %d "
2630 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2635 db_printf("inp_ip_tos: %d inp_ip_options: %p "
2636 "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2637 inp->inp_options, inp->inp_moptions);
2640 db_print_indent(indent);
2641 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd,
2642 (uintmax_t)inp->inp_gencnt);
2645 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2650 db_printf("usage: show inpcb <addr>\n");
2653 inp = (struct inpcb *)addr;
2655 db_print_inpcb(inp, "inpcb", 0);