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/eventhandler.h>
55 #include <sys/domain.h>
56 #include <sys/protosw.h>
57 #include <sys/rmlock.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
62 #include <sys/refcount.h>
64 #include <sys/kernel.h>
65 #include <sys/sysctl.h>
74 #include <net/if_var.h>
75 #include <net/if_types.h>
76 #include <net/if_llatbl.h>
77 #include <net/route.h>
78 #include <net/rss_config.h>
81 #if defined(INET) || defined(INET6)
82 #include <netinet/in.h>
83 #include <netinet/in_pcb.h>
84 #include <netinet/ip_var.h>
85 #include <netinet/tcp_var.h>
86 #include <netinet/udp.h>
87 #include <netinet/udp_var.h>
90 #include <netinet/in_var.h>
93 #include <netinet/ip6.h>
94 #include <netinet6/in6_pcb.h>
95 #include <netinet6/in6_var.h>
96 #include <netinet6/ip6_var.h>
99 #include <netipsec/ipsec_support.h>
101 #include <security/mac/mac_framework.h>
103 static struct callout ipport_tick_callout;
106 * These configure the range of local port addresses assigned to
107 * "unspecified" outgoing connections/packets/whatever.
109 VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */
110 VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */
111 VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */
112 VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */
113 VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */
114 VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */
117 * Reserved ports accessible only to root. There are significant
118 * security considerations that must be accounted for when changing these,
119 * but the security benefits can be great. Please be careful.
121 VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */
122 VNET_DEFINE(int, ipport_reservedlow);
124 /* Variables dealing with random ephemeral port allocation. */
125 VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */
126 VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */
127 VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */
128 VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */
129 VNET_DEFINE(int, ipport_tcpallocs);
130 static VNET_DEFINE(int, ipport_tcplastcount);
132 #define V_ipport_tcplastcount VNET(ipport_tcplastcount)
134 static void in_pcbremlists(struct inpcb *inp);
136 static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
137 struct in_addr faddr, u_int fport_arg,
138 struct in_addr laddr, u_int lport_arg,
139 int lookupflags, struct ifnet *ifp);
141 #define RANGECHK(var, min, max) \
142 if ((var) < (min)) { (var) = (min); } \
143 else if ((var) > (max)) { (var) = (max); }
146 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
150 error = sysctl_handle_int(oidp, arg1, arg2, req);
152 RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
153 RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
154 RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
155 RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
156 RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
157 RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
164 static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
167 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
168 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
169 &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", "");
170 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
171 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
172 &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", "");
173 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first,
174 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
175 &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", "");
176 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last,
177 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
178 &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", "");
179 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
180 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
181 &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", "");
182 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
183 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
184 &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", "");
185 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
186 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
187 &VNET_NAME(ipport_reservedhigh), 0, "");
188 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
189 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
190 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized,
191 CTLFLAG_VNET | CTLFLAG_RW,
192 &VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
193 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps,
194 CTLFLAG_VNET | CTLFLAG_RW,
195 &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
196 "allocations before switching to a sequental one");
197 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime,
198 CTLFLAG_VNET | CTLFLAG_RW,
199 &VNET_NAME(ipport_randomtime), 0,
200 "Minimum time to keep sequental port "
201 "allocation before switching to a random one");
205 * in_pcb.c: manage the Protocol Control Blocks.
207 * NOTE: It is assumed that most of these functions will be called with
208 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
209 * functions often modify hash chains or addresses in pcbs.
213 * Initialize an inpcbinfo -- we should be able to reduce the number of
217 in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
218 struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
219 char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
220 uint32_t inpcbzone_flags, u_int hashfields)
223 INP_INFO_LOCK_INIT(pcbinfo, name);
224 INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */
225 INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist");
227 pcbinfo->ipi_vnet = curvnet;
229 pcbinfo->ipi_listhead = listhead;
230 LIST_INIT(pcbinfo->ipi_listhead);
231 pcbinfo->ipi_count = 0;
232 pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
233 &pcbinfo->ipi_hashmask);
234 pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
235 &pcbinfo->ipi_porthashmask);
237 in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
239 pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
240 NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
242 uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
243 uma_zone_set_warning(pcbinfo->ipi_zone,
244 "kern.ipc.maxsockets limit reached");
248 * Destroy an inpcbinfo.
251 in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
254 KASSERT(pcbinfo->ipi_count == 0,
255 ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
257 hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
258 hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
259 pcbinfo->ipi_porthashmask);
261 in_pcbgroup_destroy(pcbinfo);
263 uma_zdestroy(pcbinfo->ipi_zone);
264 INP_LIST_LOCK_DESTROY(pcbinfo);
265 INP_HASH_LOCK_DESTROY(pcbinfo);
266 INP_INFO_LOCK_DESTROY(pcbinfo);
270 * Allocate a PCB and associate it with the socket.
271 * On success return with the PCB locked.
274 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
280 if (pcbinfo == &V_tcbinfo) {
281 INP_INFO_RLOCK_ASSERT(pcbinfo);
283 INP_INFO_WLOCK_ASSERT(pcbinfo);
288 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
291 bzero(inp, inp_zero_size);
292 inp->inp_pcbinfo = pcbinfo;
293 inp->inp_socket = so;
294 inp->inp_cred = crhold(so->so_cred);
295 inp->inp_inc.inc_fibnum = so->so_fibnum;
297 error = mac_inpcb_init(inp, M_NOWAIT);
300 mac_inpcb_create(so, inp);
302 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
303 error = ipsec_init_pcbpolicy(inp);
306 mac_inpcb_destroy(inp);
312 if (INP_SOCKAF(so) == AF_INET6) {
313 inp->inp_vflag |= INP_IPV6PROTO;
315 inp->inp_flags |= IN6P_IPV6_V6ONLY;
319 INP_LIST_WLOCK(pcbinfo);
320 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
321 pcbinfo->ipi_count++;
322 so->so_pcb = (caddr_t)inp;
324 if (V_ip6_auto_flowlabel)
325 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
327 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
328 refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */
329 INP_LIST_WUNLOCK(pcbinfo);
330 #if defined(IPSEC) || defined(IPSEC_SUPPORT) || defined(MAC)
333 crfree(inp->inp_cred);
334 uma_zfree(pcbinfo->ipi_zone, inp);
342 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
346 INP_WLOCK_ASSERT(inp);
347 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
349 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
351 anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
352 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
353 &inp->inp_lport, cred);
356 if (in_pcbinshash(inp) != 0) {
357 inp->inp_laddr.s_addr = INADDR_ANY;
362 inp->inp_flags |= INP_ANONPORT;
368 * Select a local port (number) to use.
370 #if defined(INET) || defined(INET6)
372 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
373 struct ucred *cred, int lookupflags)
375 struct inpcbinfo *pcbinfo;
376 struct inpcb *tmpinp;
377 unsigned short *lastport;
378 int count, dorandom, error;
379 u_short aux, first, last, lport;
381 struct in_addr laddr;
384 pcbinfo = inp->inp_pcbinfo;
387 * Because no actual state changes occur here, a global write lock on
388 * the pcbinfo isn't required.
390 INP_LOCK_ASSERT(inp);
391 INP_HASH_LOCK_ASSERT(pcbinfo);
393 if (inp->inp_flags & INP_HIGHPORT) {
394 first = V_ipport_hifirstauto; /* sysctl */
395 last = V_ipport_hilastauto;
396 lastport = &pcbinfo->ipi_lasthi;
397 } else if (inp->inp_flags & INP_LOWPORT) {
398 error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
401 first = V_ipport_lowfirstauto; /* 1023 */
402 last = V_ipport_lowlastauto; /* 600 */
403 lastport = &pcbinfo->ipi_lastlow;
405 first = V_ipport_firstauto; /* sysctl */
406 last = V_ipport_lastauto;
407 lastport = &pcbinfo->ipi_lastport;
410 * For UDP(-Lite), use random port allocation as long as the user
411 * allows it. For TCP (and as of yet unknown) connections,
412 * use random port allocation only if the user allows it AND
413 * ipport_tick() allows it.
415 if (V_ipport_randomized &&
416 (!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
417 pcbinfo == &V_ulitecbinfo))
422 * It makes no sense to do random port allocation if
423 * we have the only port available.
427 /* Make sure to not include UDP(-Lite) packets in the count. */
428 if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
429 V_ipport_tcpallocs++;
431 * Instead of having two loops further down counting up or down
432 * make sure that first is always <= last and go with only one
433 * code path implementing all logic.
442 /* Make the compiler happy. */
444 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
445 KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
450 tmpinp = NULL; /* Make compiler happy. */
454 *lastport = first + (arc4random() % (last - first));
456 count = last - first;
459 if (count-- < 0) /* completely used? */
460 return (EADDRNOTAVAIL);
462 if (*lastport < first || *lastport > last)
464 lport = htons(*lastport);
467 if ((inp->inp_vflag & INP_IPV6) != 0)
468 tmpinp = in6_pcblookup_local(pcbinfo,
469 &inp->in6p_laddr, lport, lookupflags, cred);
471 #if defined(INET) && defined(INET6)
475 tmpinp = in_pcblookup_local(pcbinfo, laddr,
476 lport, lookupflags, cred);
478 } while (tmpinp != NULL);
481 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
482 laddrp->s_addr = laddr.s_addr;
490 * Return cached socket options.
493 inp_so_options(const struct inpcb *inp)
499 if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
500 so_options |= SO_REUSEPORT;
501 if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
502 so_options |= SO_REUSEADDR;
505 #endif /* INET || INET6 */
508 * Check if a new BINDMULTI socket is allowed to be created.
510 * ni points to the new inp.
511 * oi points to the exisitng inp.
513 * This checks whether the existing inp also has BINDMULTI and
514 * whether the credentials match.
517 in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi)
519 /* Check permissions match */
520 if ((ni->inp_flags2 & INP_BINDMULTI) &&
521 (ni->inp_cred->cr_uid !=
522 oi->inp_cred->cr_uid))
525 /* Check the existing inp has BINDMULTI set */
526 if ((ni->inp_flags2 & INP_BINDMULTI) &&
527 ((oi->inp_flags2 & INP_BINDMULTI) == 0))
531 * We're okay - either INP_BINDMULTI isn't set on ni, or
532 * it is and it matches the checks.
539 * Set up a bind operation on a PCB, performing port allocation
540 * as required, but do not actually modify the PCB. Callers can
541 * either complete the bind by setting inp_laddr/inp_lport and
542 * calling in_pcbinshash(), or they can just use the resulting
543 * port and address to authorise the sending of a once-off packet.
545 * On error, the values of *laddrp and *lportp are not changed.
548 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
549 u_short *lportp, struct ucred *cred)
551 struct socket *so = inp->inp_socket;
552 struct sockaddr_in *sin;
553 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
554 struct in_addr laddr;
556 int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
560 * No state changes, so read locks are sufficient here.
562 INP_LOCK_ASSERT(inp);
563 INP_HASH_LOCK_ASSERT(pcbinfo);
565 if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
566 return (EADDRNOTAVAIL);
567 laddr.s_addr = *laddrp;
568 if (nam != NULL && laddr.s_addr != INADDR_ANY)
570 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
571 lookupflags = INPLOOKUP_WILDCARD;
573 if ((error = prison_local_ip4(cred, &laddr)) != 0)
576 sin = (struct sockaddr_in *)nam;
577 if (nam->sa_len != sizeof (*sin))
581 * We should check the family, but old programs
582 * incorrectly fail to initialize it.
584 if (sin->sin_family != AF_INET)
585 return (EAFNOSUPPORT);
587 error = prison_local_ip4(cred, &sin->sin_addr);
590 if (sin->sin_port != *lportp) {
591 /* Don't allow the port to change. */
594 lport = sin->sin_port;
596 /* NB: lport is left as 0 if the port isn't being changed. */
597 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
599 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
600 * allow complete duplication of binding if
601 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
602 * and a multicast address is bound on both
603 * new and duplicated sockets.
605 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
606 reuseport = SO_REUSEADDR|SO_REUSEPORT;
607 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
608 sin->sin_port = 0; /* yech... */
609 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
611 * Is the address a local IP address?
612 * If INP_BINDANY is set, then the socket may be bound
613 * to any endpoint address, local or not.
615 if ((inp->inp_flags & INP_BINDANY) == 0 &&
616 ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
617 return (EADDRNOTAVAIL);
619 laddr = sin->sin_addr;
625 if (ntohs(lport) <= V_ipport_reservedhigh &&
626 ntohs(lport) >= V_ipport_reservedlow &&
627 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
630 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
631 priv_check_cred(inp->inp_cred,
632 PRIV_NETINET_REUSEPORT, 0) != 0) {
633 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
634 lport, INPLOOKUP_WILDCARD, cred);
637 * This entire block sorely needs a rewrite.
640 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
641 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
642 (so->so_type != SOCK_STREAM ||
643 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
644 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
645 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
646 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
647 (inp->inp_cred->cr_uid !=
648 t->inp_cred->cr_uid))
652 * If the socket is a BINDMULTI socket, then
653 * the credentials need to match and the
654 * original socket also has to have been bound
657 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
660 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
661 lport, lookupflags, cred);
662 if (t && (t->inp_flags & INP_TIMEWAIT)) {
664 * XXXRW: If an incpb has had its timewait
665 * state recycled, we treat the address as
666 * being in use (for now). This is better
667 * than a panic, but not desirable.
671 (reuseport & tw->tw_so_options) == 0)
674 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
675 (reuseport & inp_so_options(t)) == 0) {
677 if (ntohl(sin->sin_addr.s_addr) !=
679 ntohl(t->inp_laddr.s_addr) !=
681 (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
682 (t->inp_vflag & INP_IPV6PROTO) == 0)
685 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
693 error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
698 *laddrp = laddr.s_addr;
704 * Connect from a socket to a specified address.
705 * Both address and port must be specified in argument sin.
706 * If don't have a local address for this socket yet,
710 in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
711 struct ucred *cred, struct mbuf *m)
713 u_short lport, fport;
714 in_addr_t laddr, faddr;
717 INP_WLOCK_ASSERT(inp);
718 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
720 lport = inp->inp_lport;
721 laddr = inp->inp_laddr.s_addr;
722 anonport = (lport == 0);
723 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
728 /* Do the initial binding of the local address if required. */
729 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
730 inp->inp_lport = lport;
731 inp->inp_laddr.s_addr = laddr;
732 if (in_pcbinshash(inp) != 0) {
733 inp->inp_laddr.s_addr = INADDR_ANY;
739 /* Commit the remaining changes. */
740 inp->inp_lport = lport;
741 inp->inp_laddr.s_addr = laddr;
742 inp->inp_faddr.s_addr = faddr;
743 inp->inp_fport = fport;
744 in_pcbrehash_mbuf(inp, m);
747 inp->inp_flags |= INP_ANONPORT;
752 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
755 return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
759 * Do proper source address selection on an unbound socket in case
760 * of connect. Take jails into account as well.
763 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
768 struct sockaddr_in *sin;
772 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
775 * Bypass source address selection and use the primary jail IP
778 if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
782 bzero(&sro, sizeof(sro));
784 sin = (struct sockaddr_in *)&sro.ro_dst;
785 sin->sin_family = AF_INET;
786 sin->sin_len = sizeof(struct sockaddr_in);
787 sin->sin_addr.s_addr = faddr->s_addr;
790 * If route is known our src addr is taken from the i/f,
793 * Find out route to destination.
795 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
796 in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
799 * If we found a route, use the address corresponding to
800 * the outgoing interface.
802 * Otherwise assume faddr is reachable on a directly connected
803 * network and try to find a corresponding interface to take
804 * the source address from.
806 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
807 struct in_ifaddr *ia;
810 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin,
811 inp->inp_socket->so_fibnum));
813 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0,
814 inp->inp_socket->so_fibnum));
820 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
821 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
822 ifa_free(&ia->ia_ifa);
827 ifa_free(&ia->ia_ifa);
830 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
833 if (sa->sa_family != AF_INET)
835 sin = (struct sockaddr_in *)sa;
836 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
837 ia = (struct in_ifaddr *)ifa;
842 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
843 IF_ADDR_RUNLOCK(ifp);
846 IF_ADDR_RUNLOCK(ifp);
848 /* 3. As a last resort return the 'default' jail address. */
849 error = prison_get_ip4(cred, laddr);
854 * If the outgoing interface on the route found is not
855 * a loopback interface, use the address from that interface.
856 * In case of jails do those three steps:
857 * 1. check if the interface address belongs to the jail. If so use it.
858 * 2. check if we have any address on the outgoing interface
859 * belonging to this jail. If so use it.
860 * 3. as a last resort return the 'default' jail address.
862 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
863 struct in_ifaddr *ia;
866 /* If not jailed, use the default returned. */
867 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
868 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
869 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
874 /* 1. Check if the iface address belongs to the jail. */
875 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
876 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
877 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
878 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
883 * 2. Check if we have any address on the outgoing interface
884 * belonging to this jail.
887 ifp = sro.ro_rt->rt_ifp;
889 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
891 if (sa->sa_family != AF_INET)
893 sin = (struct sockaddr_in *)sa;
894 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
895 ia = (struct in_ifaddr *)ifa;
900 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
901 IF_ADDR_RUNLOCK(ifp);
904 IF_ADDR_RUNLOCK(ifp);
906 /* 3. As a last resort return the 'default' jail address. */
907 error = prison_get_ip4(cred, laddr);
912 * The outgoing interface is marked with 'loopback net', so a route
913 * to ourselves is here.
914 * Try to find the interface of the destination address and then
915 * take the address from there. That interface is not necessarily
916 * a loopback interface.
917 * In case of jails, check that it is an address of the jail
918 * and if we cannot find, fall back to the 'default' jail address.
920 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
921 struct sockaddr_in sain;
922 struct in_ifaddr *ia;
924 bzero(&sain, sizeof(struct sockaddr_in));
925 sain.sin_family = AF_INET;
926 sain.sin_len = sizeof(struct sockaddr_in);
927 sain.sin_addr.s_addr = faddr->s_addr;
929 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain),
930 inp->inp_socket->so_fibnum));
932 ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0,
933 inp->inp_socket->so_fibnum));
935 ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
937 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
942 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
943 ifa_free(&ia->ia_ifa);
952 ifa_free(&ia->ia_ifa);
955 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
958 if (sa->sa_family != AF_INET)
960 sin = (struct sockaddr_in *)sa;
961 if (prison_check_ip4(cred,
962 &sin->sin_addr) == 0) {
963 ia = (struct in_ifaddr *)ifa;
968 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
969 IF_ADDR_RUNLOCK(ifp);
972 IF_ADDR_RUNLOCK(ifp);
975 /* 3. As a last resort return the 'default' jail address. */
976 error = prison_get_ip4(cred, laddr);
981 if (sro.ro_rt != NULL)
987 * Set up for a connect from a socket to the specified address.
988 * On entry, *laddrp and *lportp should contain the current local
989 * address and port for the PCB; these are updated to the values
990 * that should be placed in inp_laddr and inp_lport to complete
993 * On success, *faddrp and *fportp will be set to the remote address
994 * and port. These are not updated in the error case.
996 * If the operation fails because the connection already exists,
997 * *oinpp will be set to the PCB of that connection so that the
998 * caller can decide to override it. In all other cases, *oinpp
1002 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
1003 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
1004 struct inpcb **oinpp, struct ucred *cred)
1006 struct rm_priotracker in_ifa_tracker;
1007 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1008 struct in_ifaddr *ia;
1010 struct in_addr laddr, faddr;
1011 u_short lport, fport;
1015 * Because a global state change doesn't actually occur here, a read
1016 * lock is sufficient.
1018 INP_LOCK_ASSERT(inp);
1019 INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
1023 if (nam->sa_len != sizeof (*sin))
1025 if (sin->sin_family != AF_INET)
1026 return (EAFNOSUPPORT);
1027 if (sin->sin_port == 0)
1028 return (EADDRNOTAVAIL);
1029 laddr.s_addr = *laddrp;
1031 faddr = sin->sin_addr;
1032 fport = sin->sin_port;
1034 if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
1036 * If the destination address is INADDR_ANY,
1037 * use the primary local address.
1038 * If the supplied address is INADDR_BROADCAST,
1039 * and the primary interface supports broadcast,
1040 * choose the broadcast address for that interface.
1042 if (faddr.s_addr == INADDR_ANY) {
1043 IN_IFADDR_RLOCK(&in_ifa_tracker);
1045 IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
1046 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1048 (error = prison_get_ip4(cred, &faddr)) != 0)
1050 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
1051 IN_IFADDR_RLOCK(&in_ifa_tracker);
1052 if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
1054 faddr = satosin(&TAILQ_FIRST(
1055 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
1056 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1059 if (laddr.s_addr == INADDR_ANY) {
1060 error = in_pcbladdr(inp, &faddr, &laddr, cred);
1062 * If the destination address is multicast and an outgoing
1063 * interface has been set as a multicast option, prefer the
1064 * address of that interface as our source address.
1066 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
1067 inp->inp_moptions != NULL) {
1068 struct ip_moptions *imo;
1071 imo = inp->inp_moptions;
1072 if (imo->imo_multicast_ifp != NULL) {
1073 ifp = imo->imo_multicast_ifp;
1074 IN_IFADDR_RLOCK(&in_ifa_tracker);
1075 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1076 if ((ia->ia_ifp == ifp) &&
1078 prison_check_ip4(cred,
1079 &ia->ia_addr.sin_addr) == 0))
1083 error = EADDRNOTAVAIL;
1085 laddr = ia->ia_addr.sin_addr;
1088 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1094 oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1095 laddr, lport, 0, NULL);
1099 return (EADDRINUSE);
1102 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1107 *laddrp = laddr.s_addr;
1109 *faddrp = faddr.s_addr;
1115 in_pcbdisconnect(struct inpcb *inp)
1118 INP_WLOCK_ASSERT(inp);
1119 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1121 inp->inp_faddr.s_addr = INADDR_ANY;
1128 * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1129 * For most protocols, this will be invoked immediately prior to calling
1130 * in_pcbfree(). However, with TCP the inpcb may significantly outlive the
1131 * socket, in which case in_pcbfree() is deferred.
1134 in_pcbdetach(struct inpcb *inp)
1137 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1139 inp->inp_socket->so_pcb = NULL;
1140 inp->inp_socket = NULL;
1144 * in_pcbref() bumps the reference count on an inpcb in order to maintain
1145 * stability of an inpcb pointer despite the inpcb lock being released. This
1146 * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
1147 * but where the inpcb lock may already held, or when acquiring a reference
1150 * in_pcbref() should be used only to provide brief memory stability, and
1151 * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
1152 * garbage collect the inpcb if it has been in_pcbfree()'d from another
1153 * context. Until in_pcbrele() has returned that the inpcb is still valid,
1154 * lock and rele are the *only* safe operations that may be performed on the
1157 * While the inpcb will not be freed, releasing the inpcb lock means that the
1158 * connection's state may change, so the caller should be careful to
1159 * revalidate any cached state on reacquiring the lock. Drop the reference
1160 * using in_pcbrele().
1163 in_pcbref(struct inpcb *inp)
1166 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1168 refcount_acquire(&inp->inp_refcount);
1172 * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
1173 * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
1174 * return a flag indicating whether or not the inpcb remains valid. If it is
1175 * valid, we return with the inpcb lock held.
1177 * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
1178 * reference on an inpcb. Historically more work was done here (actually, in
1179 * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
1180 * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely
1181 * about memory stability (and continued use of the write lock).
1184 in_pcbrele_rlocked(struct inpcb *inp)
1186 struct inpcbinfo *pcbinfo;
1188 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1190 INP_RLOCK_ASSERT(inp);
1192 if (refcount_release(&inp->inp_refcount) == 0) {
1194 * If the inpcb has been freed, let the caller know, even if
1195 * this isn't the last reference.
1197 if (inp->inp_flags2 & INP_FREED) {
1204 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1207 pcbinfo = inp->inp_pcbinfo;
1208 uma_zfree(pcbinfo->ipi_zone, inp);
1213 in_pcbrele_wlocked(struct inpcb *inp)
1215 struct inpcbinfo *pcbinfo;
1217 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1219 INP_WLOCK_ASSERT(inp);
1221 if (refcount_release(&inp->inp_refcount) == 0) {
1223 * If the inpcb has been freed, let the caller know, even if
1224 * this isn't the last reference.
1226 if (inp->inp_flags2 & INP_FREED) {
1233 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1236 pcbinfo = inp->inp_pcbinfo;
1237 uma_zfree(pcbinfo->ipi_zone, inp);
1242 * Temporary wrapper.
1245 in_pcbrele(struct inpcb *inp)
1248 return (in_pcbrele_wlocked(inp));
1252 * Unconditionally schedule an inpcb to be freed by decrementing its
1253 * reference count, which should occur only after the inpcb has been detached
1254 * from its socket. If another thread holds a temporary reference (acquired
1255 * using in_pcbref()) then the free is deferred until that reference is
1256 * released using in_pcbrele(), but the inpcb is still unlocked. Almost all
1257 * work, including removal from global lists, is done in this context, where
1258 * the pcbinfo lock is held.
1261 in_pcbfree(struct inpcb *inp)
1263 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1265 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1268 if (pcbinfo == &V_tcbinfo) {
1269 INP_INFO_LOCK_ASSERT(pcbinfo);
1271 INP_INFO_WLOCK_ASSERT(pcbinfo);
1274 INP_WLOCK_ASSERT(inp);
1276 /* XXXRW: Do as much as possible here. */
1277 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1278 if (inp->inp_sp != NULL)
1279 ipsec_delete_pcbpolicy(inp);
1281 INP_LIST_WLOCK(pcbinfo);
1282 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1283 in_pcbremlists(inp);
1284 INP_LIST_WUNLOCK(pcbinfo);
1286 if (inp->inp_vflag & INP_IPV6PROTO) {
1287 ip6_freepcbopts(inp->in6p_outputopts);
1288 if (inp->in6p_moptions != NULL)
1289 ip6_freemoptions(inp->in6p_moptions);
1292 if (inp->inp_options)
1293 (void)m_free(inp->inp_options);
1295 if (inp->inp_moptions != NULL)
1296 inp_freemoptions(inp->inp_moptions);
1298 if (inp->inp_route.ro_rt) {
1299 RTFREE(inp->inp_route.ro_rt);
1300 inp->inp_route.ro_rt = (struct rtentry *)NULL;
1302 if (inp->inp_route.ro_lle)
1303 LLE_FREE(inp->inp_route.ro_lle); /* zeros ro_lle */
1306 inp->inp_flags2 |= INP_FREED;
1307 crfree(inp->inp_cred);
1309 mac_inpcb_destroy(inp);
1311 if (!in_pcbrele_wlocked(inp))
1316 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1317 * port reservation, and preventing it from being returned by inpcb lookups.
1319 * It is used by TCP to mark an inpcb as unused and avoid future packet
1320 * delivery or event notification when a socket remains open but TCP has
1321 * closed. This might occur as a result of a shutdown()-initiated TCP close
1322 * or a RST on the wire, and allows the port binding to be reused while still
1323 * maintaining the invariant that so_pcb always points to a valid inpcb until
1326 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1327 * in_pcbnotifyall() and in_pcbpurgeif0()?
1330 in_pcbdrop(struct inpcb *inp)
1333 INP_WLOCK_ASSERT(inp);
1336 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1339 inp->inp_flags |= INP_DROPPED;
1340 if (inp->inp_flags & INP_INHASHLIST) {
1341 struct inpcbport *phd = inp->inp_phd;
1343 INP_HASH_WLOCK(inp->inp_pcbinfo);
1344 LIST_REMOVE(inp, inp_hash);
1345 LIST_REMOVE(inp, inp_portlist);
1346 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1347 LIST_REMOVE(phd, phd_hash);
1350 INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1351 inp->inp_flags &= ~INP_INHASHLIST;
1353 in_pcbgroup_remove(inp);
1360 * Common routines to return the socket addresses associated with inpcbs.
1363 in_sockaddr(in_port_t port, struct in_addr *addr_p)
1365 struct sockaddr_in *sin;
1367 sin = malloc(sizeof *sin, M_SONAME,
1369 sin->sin_family = AF_INET;
1370 sin->sin_len = sizeof(*sin);
1371 sin->sin_addr = *addr_p;
1372 sin->sin_port = port;
1374 return (struct sockaddr *)sin;
1378 in_getsockaddr(struct socket *so, struct sockaddr **nam)
1381 struct in_addr addr;
1384 inp = sotoinpcb(so);
1385 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1388 port = inp->inp_lport;
1389 addr = inp->inp_laddr;
1392 *nam = in_sockaddr(port, &addr);
1397 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1400 struct in_addr addr;
1403 inp = sotoinpcb(so);
1404 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1407 port = inp->inp_fport;
1408 addr = inp->inp_faddr;
1411 *nam = in_sockaddr(port, &addr);
1416 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1417 struct inpcb *(*notify)(struct inpcb *, int))
1419 struct inpcb *inp, *inp_temp;
1421 INP_INFO_WLOCK(pcbinfo);
1422 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1425 if ((inp->inp_vflag & INP_IPV4) == 0) {
1430 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1431 inp->inp_socket == NULL) {
1435 if ((*notify)(inp, errno))
1438 INP_INFO_WUNLOCK(pcbinfo);
1442 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1445 struct ip_moptions *imo;
1448 INP_INFO_WLOCK(pcbinfo);
1449 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1451 imo = inp->inp_moptions;
1452 if ((inp->inp_vflag & INP_IPV4) &&
1455 * Unselect the outgoing interface if it is being
1458 if (imo->imo_multicast_ifp == ifp)
1459 imo->imo_multicast_ifp = NULL;
1462 * Drop multicast group membership if we joined
1463 * through the interface being detached.
1465 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1467 if (imo->imo_membership[i]->inm_ifp == ifp) {
1468 in_delmulti(imo->imo_membership[i]);
1470 } else if (gap != 0)
1471 imo->imo_membership[i - gap] =
1472 imo->imo_membership[i];
1474 imo->imo_num_memberships -= gap;
1478 INP_INFO_WUNLOCK(pcbinfo);
1482 * Lookup a PCB based on the local address and port. Caller must hold the
1483 * hash lock. No inpcb locks or references are acquired.
1485 #define INP_LOOKUP_MAPPED_PCB_COST 3
1487 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1488 u_short lport, int lookupflags, struct ucred *cred)
1492 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1498 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1499 ("%s: invalid lookup flags %d", __func__, lookupflags));
1501 INP_HASH_LOCK_ASSERT(pcbinfo);
1503 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1504 struct inpcbhead *head;
1506 * Look for an unconnected (wildcard foreign addr) PCB that
1507 * matches the local address and port we're looking for.
1509 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1510 0, pcbinfo->ipi_hashmask)];
1511 LIST_FOREACH(inp, head, inp_hash) {
1513 /* XXX inp locking */
1514 if ((inp->inp_vflag & INP_IPV4) == 0)
1517 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1518 inp->inp_laddr.s_addr == laddr.s_addr &&
1519 inp->inp_lport == lport) {
1524 prison_equal_ip4(cred->cr_prison,
1525 inp->inp_cred->cr_prison))
1534 struct inpcbporthead *porthash;
1535 struct inpcbport *phd;
1536 struct inpcb *match = NULL;
1538 * Best fit PCB lookup.
1540 * First see if this local port is in use by looking on the
1543 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1544 pcbinfo->ipi_porthashmask)];
1545 LIST_FOREACH(phd, porthash, phd_hash) {
1546 if (phd->phd_port == lport)
1551 * Port is in use by one or more PCBs. Look for best
1554 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1557 !prison_equal_ip4(inp->inp_cred->cr_prison,
1561 /* XXX inp locking */
1562 if ((inp->inp_vflag & INP_IPV4) == 0)
1565 * We never select the PCB that has
1566 * INP_IPV6 flag and is bound to :: if
1567 * we have another PCB which is bound
1568 * to 0.0.0.0. If a PCB has the
1569 * INP_IPV6 flag, then we set its cost
1570 * higher than IPv4 only PCBs.
1572 * Note that the case only happens
1573 * when a socket is bound to ::, under
1574 * the condition that the use of the
1575 * mapped address is allowed.
1577 if ((inp->inp_vflag & INP_IPV6) != 0)
1578 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1580 if (inp->inp_faddr.s_addr != INADDR_ANY)
1582 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1583 if (laddr.s_addr == INADDR_ANY)
1585 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1588 if (laddr.s_addr != INADDR_ANY)
1591 if (wildcard < matchwild) {
1593 matchwild = wildcard;
1602 #undef INP_LOOKUP_MAPPED_PCB_COST
1606 * Lookup PCB in hash list, using pcbgroup tables.
1608 static struct inpcb *
1609 in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
1610 struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
1611 u_int lport_arg, int lookupflags, struct ifnet *ifp)
1613 struct inpcbhead *head;
1614 struct inpcb *inp, *tmpinp;
1615 u_short fport = fport_arg, lport = lport_arg;
1618 * First look for an exact match.
1621 INP_GROUP_LOCK(pcbgroup);
1622 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1623 pcbgroup->ipg_hashmask)];
1624 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1626 /* XXX inp locking */
1627 if ((inp->inp_vflag & INP_IPV4) == 0)
1630 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1631 inp->inp_laddr.s_addr == laddr.s_addr &&
1632 inp->inp_fport == fport &&
1633 inp->inp_lport == lport) {
1635 * XXX We should be able to directly return
1636 * the inp here, without any checks.
1637 * Well unless both bound with SO_REUSEPORT?
1639 if (prison_flag(inp->inp_cred, PR_IP4))
1645 if (tmpinp != NULL) {
1652 * For incoming connections, we may wish to do a wildcard
1653 * match for an RSS-local socket.
1655 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1656 struct inpcb *local_wild = NULL, *local_exact = NULL;
1658 struct inpcb *local_wild_mapped = NULL;
1660 struct inpcb *jail_wild = NULL;
1661 struct inpcbhead *head;
1665 * Order of socket selection - we always prefer jails.
1666 * 1. jailed, non-wild.
1668 * 3. non-jailed, non-wild.
1669 * 4. non-jailed, wild.
1672 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY,
1673 lport, 0, pcbgroup->ipg_hashmask)];
1674 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1676 /* XXX inp locking */
1677 if ((inp->inp_vflag & INP_IPV4) == 0)
1680 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1681 inp->inp_lport != lport)
1684 injail = prison_flag(inp->inp_cred, PR_IP4);
1686 if (prison_check_ip4(inp->inp_cred,
1690 if (local_exact != NULL)
1694 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1699 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1701 /* XXX inp locking, NULL check */
1702 if (inp->inp_vflag & INP_IPV6PROTO)
1703 local_wild_mapped = inp;
1711 } /* LIST_FOREACH */
1720 inp = local_wild_mapped;
1728 * Then look for a wildcard match, if requested.
1730 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1731 struct inpcb *local_wild = NULL, *local_exact = NULL;
1733 struct inpcb *local_wild_mapped = NULL;
1735 struct inpcb *jail_wild = NULL;
1736 struct inpcbhead *head;
1740 * Order of socket selection - we always prefer jails.
1741 * 1. jailed, non-wild.
1743 * 3. non-jailed, non-wild.
1744 * 4. non-jailed, wild.
1746 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
1747 0, pcbinfo->ipi_wildmask)];
1748 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1750 /* XXX inp locking */
1751 if ((inp->inp_vflag & INP_IPV4) == 0)
1754 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1755 inp->inp_lport != lport)
1758 injail = prison_flag(inp->inp_cred, PR_IP4);
1760 if (prison_check_ip4(inp->inp_cred,
1764 if (local_exact != NULL)
1768 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1773 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1775 /* XXX inp locking, NULL check */
1776 if (inp->inp_vflag & INP_IPV6PROTO)
1777 local_wild_mapped = inp;
1785 } /* LIST_FOREACH */
1793 inp = local_wild_mapped;
1797 } /* if (lookupflags & INPLOOKUP_WILDCARD) */
1798 INP_GROUP_UNLOCK(pcbgroup);
1803 INP_GROUP_UNLOCK(pcbgroup);
1804 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1806 if (in_pcbrele_wlocked(inp))
1808 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1810 if (in_pcbrele_rlocked(inp))
1813 panic("%s: locking bug", __func__);
1816 #endif /* PCBGROUP */
1819 * Lookup PCB in hash list, using pcbinfo tables. This variation assumes
1820 * that the caller has locked the hash list, and will not perform any further
1821 * locking or reference operations on either the hash list or the connection.
1823 static struct inpcb *
1824 in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1825 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1828 struct inpcbhead *head;
1829 struct inpcb *inp, *tmpinp;
1830 u_short fport = fport_arg, lport = lport_arg;
1832 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1833 ("%s: invalid lookup flags %d", __func__, lookupflags));
1835 INP_HASH_LOCK_ASSERT(pcbinfo);
1838 * First look for an exact match.
1841 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1842 pcbinfo->ipi_hashmask)];
1843 LIST_FOREACH(inp, head, inp_hash) {
1845 /* XXX inp locking */
1846 if ((inp->inp_vflag & INP_IPV4) == 0)
1849 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1850 inp->inp_laddr.s_addr == laddr.s_addr &&
1851 inp->inp_fport == fport &&
1852 inp->inp_lport == lport) {
1854 * XXX We should be able to directly return
1855 * the inp here, without any checks.
1856 * Well unless both bound with SO_REUSEPORT?
1858 if (prison_flag(inp->inp_cred, PR_IP4))
1868 * Then look for a wildcard match, if requested.
1870 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1871 struct inpcb *local_wild = NULL, *local_exact = NULL;
1873 struct inpcb *local_wild_mapped = NULL;
1875 struct inpcb *jail_wild = NULL;
1879 * Order of socket selection - we always prefer jails.
1880 * 1. jailed, non-wild.
1882 * 3. non-jailed, non-wild.
1883 * 4. non-jailed, wild.
1886 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1887 0, pcbinfo->ipi_hashmask)];
1888 LIST_FOREACH(inp, head, inp_hash) {
1890 /* XXX inp locking */
1891 if ((inp->inp_vflag & INP_IPV4) == 0)
1894 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1895 inp->inp_lport != lport)
1898 injail = prison_flag(inp->inp_cred, PR_IP4);
1900 if (prison_check_ip4(inp->inp_cred,
1904 if (local_exact != NULL)
1908 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1913 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1915 /* XXX inp locking, NULL check */
1916 if (inp->inp_vflag & INP_IPV6PROTO)
1917 local_wild_mapped = inp;
1925 } /* LIST_FOREACH */
1926 if (jail_wild != NULL)
1928 if (local_exact != NULL)
1929 return (local_exact);
1930 if (local_wild != NULL)
1931 return (local_wild);
1933 if (local_wild_mapped != NULL)
1934 return (local_wild_mapped);
1936 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1942 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1943 * hash list lock, and will return the inpcb locked (i.e., requires
1944 * INPLOOKUP_LOCKPCB).
1946 static struct inpcb *
1947 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1948 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1953 INP_HASH_RLOCK(pcbinfo);
1954 inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1955 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1958 INP_HASH_RUNLOCK(pcbinfo);
1959 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1961 if (in_pcbrele_wlocked(inp))
1963 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1965 if (in_pcbrele_rlocked(inp))
1968 panic("%s: locking bug", __func__);
1970 INP_HASH_RUNLOCK(pcbinfo);
1975 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1976 * from which a pre-calculated hash value may be extracted.
1978 * Possibly more of this logic should be in in_pcbgroup.c.
1981 in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1982 struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1984 #if defined(PCBGROUP) && !defined(RSS)
1985 struct inpcbgroup *pcbgroup;
1988 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1989 ("%s: invalid lookup flags %d", __func__, lookupflags));
1990 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1991 ("%s: LOCKPCB not set", __func__));
1994 * When not using RSS, use connection groups in preference to the
1995 * reservation table when looking up 4-tuples. When using RSS, just
1996 * use the reservation table, due to the cost of the Toeplitz hash
1999 * XXXRW: This policy belongs in the pcbgroup code, as in principle
2000 * we could be doing RSS with a non-Toeplitz hash that is affordable
2003 #if defined(PCBGROUP) && !defined(RSS)
2004 if (in_pcbgroup_enabled(pcbinfo)) {
2005 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
2007 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
2008 laddr, lport, lookupflags, ifp));
2011 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2016 in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2017 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2018 struct ifnet *ifp, struct mbuf *m)
2021 struct inpcbgroup *pcbgroup;
2024 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2025 ("%s: invalid lookup flags %d", __func__, lookupflags));
2026 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2027 ("%s: LOCKPCB not set", __func__));
2031 * If we can use a hardware-generated hash to look up the connection
2032 * group, use that connection group to find the inpcb. Otherwise
2033 * fall back on a software hash -- or the reservation table if we're
2036 * XXXRW: As above, that policy belongs in the pcbgroup code.
2038 if (in_pcbgroup_enabled(pcbinfo) &&
2039 !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
2040 pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
2041 m->m_pkthdr.flowid);
2042 if (pcbgroup != NULL)
2043 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
2044 fport, laddr, lport, lookupflags, ifp));
2046 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
2048 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
2049 laddr, lport, lookupflags, ifp));
2053 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2059 * Insert PCB onto various hash lists.
2062 in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
2064 struct inpcbhead *pcbhash;
2065 struct inpcbporthead *pcbporthash;
2066 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2067 struct inpcbport *phd;
2068 u_int32_t hashkey_faddr;
2070 INP_WLOCK_ASSERT(inp);
2071 INP_HASH_WLOCK_ASSERT(pcbinfo);
2073 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2074 ("in_pcbinshash: INP_INHASHLIST"));
2077 if (inp->inp_vflag & INP_IPV6)
2078 hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2081 hashkey_faddr = inp->inp_faddr.s_addr;
2083 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2084 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
2086 pcbporthash = &pcbinfo->ipi_porthashbase[
2087 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2090 * Go through port list and look for a head for this lport.
2092 LIST_FOREACH(phd, pcbporthash, phd_hash) {
2093 if (phd->phd_port == inp->inp_lport)
2097 * If none exists, malloc one and tack it on.
2100 phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
2102 return (ENOBUFS); /* XXX */
2104 phd->phd_port = inp->inp_lport;
2105 LIST_INIT(&phd->phd_pcblist);
2106 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
2109 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
2110 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
2111 inp->inp_flags |= INP_INHASHLIST;
2113 if (do_pcbgroup_update)
2114 in_pcbgroup_update(inp);
2120 * For now, there are two public interfaces to insert an inpcb into the hash
2121 * lists -- one that does update pcbgroups, and one that doesn't. The latter
2122 * is used only in the TCP syncache, where in_pcbinshash is called before the
2123 * full 4-tuple is set for the inpcb, and we don't want to install in the
2124 * pcbgroup until later.
2126 * XXXRW: This seems like a misfeature. in_pcbinshash should always update
2127 * connection groups, and partially initialised inpcbs should not be exposed
2128 * to either reservation hash tables or pcbgroups.
2131 in_pcbinshash(struct inpcb *inp)
2134 return (in_pcbinshash_internal(inp, 1));
2138 in_pcbinshash_nopcbgroup(struct inpcb *inp)
2141 return (in_pcbinshash_internal(inp, 0));
2145 * Move PCB to the proper hash bucket when { faddr, fport } have been
2146 * changed. NOTE: This does not handle the case of the lport changing (the
2147 * hashed port list would have to be updated as well), so the lport must
2148 * not change after in_pcbinshash() has been called.
2151 in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
2153 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2154 struct inpcbhead *head;
2155 u_int32_t hashkey_faddr;
2157 INP_WLOCK_ASSERT(inp);
2158 INP_HASH_WLOCK_ASSERT(pcbinfo);
2160 KASSERT(inp->inp_flags & INP_INHASHLIST,
2161 ("in_pcbrehash: !INP_INHASHLIST"));
2164 if (inp->inp_vflag & INP_IPV6)
2165 hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2168 hashkey_faddr = inp->inp_faddr.s_addr;
2170 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2171 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
2173 LIST_REMOVE(inp, inp_hash);
2174 LIST_INSERT_HEAD(head, inp, inp_hash);
2178 in_pcbgroup_update_mbuf(inp, m);
2180 in_pcbgroup_update(inp);
2185 in_pcbrehash(struct inpcb *inp)
2188 in_pcbrehash_mbuf(inp, NULL);
2192 * Remove PCB from various lists.
2195 in_pcbremlists(struct inpcb *inp)
2197 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2200 if (pcbinfo == &V_tcbinfo) {
2201 INP_INFO_RLOCK_ASSERT(pcbinfo);
2203 INP_INFO_WLOCK_ASSERT(pcbinfo);
2207 INP_WLOCK_ASSERT(inp);
2208 INP_LIST_WLOCK_ASSERT(pcbinfo);
2210 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2211 if (inp->inp_flags & INP_INHASHLIST) {
2212 struct inpcbport *phd = inp->inp_phd;
2214 INP_HASH_WLOCK(pcbinfo);
2215 LIST_REMOVE(inp, inp_hash);
2216 LIST_REMOVE(inp, inp_portlist);
2217 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2218 LIST_REMOVE(phd, phd_hash);
2221 INP_HASH_WUNLOCK(pcbinfo);
2222 inp->inp_flags &= ~INP_INHASHLIST;
2224 LIST_REMOVE(inp, inp_list);
2225 pcbinfo->ipi_count--;
2227 in_pcbgroup_remove(inp);
2232 * Check for alternatives when higher level complains
2233 * about service problems. For now, invalidate cached
2234 * routing information. If the route was created dynamically
2235 * (by a redirect), time to try a default gateway again.
2238 in_losing(struct inpcb *inp)
2241 if (inp->inp_route.ro_rt) {
2242 RTFREE(inp->inp_route.ro_rt);
2243 inp->inp_route.ro_rt = (struct rtentry *)NULL;
2245 if (inp->inp_route.ro_lle)
2246 LLE_FREE(inp->inp_route.ro_lle); /* zeros ro_lle */
2251 * A set label operation has occurred at the socket layer, propagate the
2252 * label change into the in_pcb for the socket.
2255 in_pcbsosetlabel(struct socket *so)
2260 inp = sotoinpcb(so);
2261 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2265 mac_inpcb_sosetlabel(so, inp);
2272 * ipport_tick runs once per second, determining if random port allocation
2273 * should be continued. If more than ipport_randomcps ports have been
2274 * allocated in the last second, then we return to sequential port
2275 * allocation. We return to random allocation only once we drop below
2276 * ipport_randomcps for at least ipport_randomtime seconds.
2279 ipport_tick(void *xtp)
2281 VNET_ITERATOR_DECL(vnet_iter);
2283 VNET_LIST_RLOCK_NOSLEEP();
2284 VNET_FOREACH(vnet_iter) {
2285 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */
2286 if (V_ipport_tcpallocs <=
2287 V_ipport_tcplastcount + V_ipport_randomcps) {
2288 if (V_ipport_stoprandom > 0)
2289 V_ipport_stoprandom--;
2291 V_ipport_stoprandom = V_ipport_randomtime;
2292 V_ipport_tcplastcount = V_ipport_tcpallocs;
2295 VNET_LIST_RUNLOCK_NOSLEEP();
2296 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
2303 callout_stop(&ipport_tick_callout);
2307 * The ipport_callout should start running at about the time we attach the
2308 * inet or inet6 domains.
2311 ipport_tick_init(const void *unused __unused)
2314 /* Start ipport_tick. */
2315 callout_init(&ipport_tick_callout, 1);
2316 callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2317 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2318 SHUTDOWN_PRI_DEFAULT);
2320 SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2321 ipport_tick_init, NULL);
2324 inp_wlock(struct inpcb *inp)
2331 inp_wunlock(struct inpcb *inp)
2338 inp_rlock(struct inpcb *inp)
2345 inp_runlock(struct inpcb *inp)
2353 inp_lock_assert(struct inpcb *inp)
2356 INP_WLOCK_ASSERT(inp);
2360 inp_unlock_assert(struct inpcb *inp)
2363 INP_UNLOCK_ASSERT(inp);
2368 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
2372 INP_INFO_WLOCK(&V_tcbinfo);
2373 LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
2378 INP_INFO_WUNLOCK(&V_tcbinfo);
2382 inp_inpcbtosocket(struct inpcb *inp)
2385 INP_WLOCK_ASSERT(inp);
2386 return (inp->inp_socket);
2390 inp_inpcbtotcpcb(struct inpcb *inp)
2393 INP_WLOCK_ASSERT(inp);
2394 return ((struct tcpcb *)inp->inp_ppcb);
2398 inp_ip_tos_get(const struct inpcb *inp)
2401 return (inp->inp_ip_tos);
2405 inp_ip_tos_set(struct inpcb *inp, int val)
2408 inp->inp_ip_tos = val;
2412 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
2413 uint32_t *faddr, uint16_t *fp)
2416 INP_LOCK_ASSERT(inp);
2417 *laddr = inp->inp_laddr.s_addr;
2418 *faddr = inp->inp_faddr.s_addr;
2419 *lp = inp->inp_lport;
2420 *fp = inp->inp_fport;
2424 so_sotoinpcb(struct socket *so)
2427 return (sotoinpcb(so));
2431 so_sototcpcb(struct socket *so)
2434 return (sototcpcb(so));
2439 db_print_indent(int indent)
2443 for (i = 0; i < indent; i++)
2448 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2450 char faddr_str[48], laddr_str[48];
2452 db_print_indent(indent);
2453 db_printf("%s at %p\n", name, inc);
2458 if (inc->inc_flags & INC_ISIPV6) {
2460 ip6_sprintf(laddr_str, &inc->inc6_laddr);
2461 ip6_sprintf(faddr_str, &inc->inc6_faddr);
2466 inet_ntoa_r(inc->inc_laddr, laddr_str);
2467 inet_ntoa_r(inc->inc_faddr, faddr_str);
2469 db_print_indent(indent);
2470 db_printf("inc_laddr %s inc_lport %u\n", laddr_str,
2471 ntohs(inc->inc_lport));
2472 db_print_indent(indent);
2473 db_printf("inc_faddr %s inc_fport %u\n", faddr_str,
2474 ntohs(inc->inc_fport));
2478 db_print_inpflags(int inp_flags)
2483 if (inp_flags & INP_RECVOPTS) {
2484 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2487 if (inp_flags & INP_RECVRETOPTS) {
2488 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2491 if (inp_flags & INP_RECVDSTADDR) {
2492 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2495 if (inp_flags & INP_HDRINCL) {
2496 db_printf("%sINP_HDRINCL", comma ? ", " : "");
2499 if (inp_flags & INP_HIGHPORT) {
2500 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2503 if (inp_flags & INP_LOWPORT) {
2504 db_printf("%sINP_LOWPORT", comma ? ", " : "");
2507 if (inp_flags & INP_ANONPORT) {
2508 db_printf("%sINP_ANONPORT", comma ? ", " : "");
2511 if (inp_flags & INP_RECVIF) {
2512 db_printf("%sINP_RECVIF", comma ? ", " : "");
2515 if (inp_flags & INP_MTUDISC) {
2516 db_printf("%sINP_MTUDISC", comma ? ", " : "");
2519 if (inp_flags & INP_RECVTTL) {
2520 db_printf("%sINP_RECVTTL", comma ? ", " : "");
2523 if (inp_flags & INP_DONTFRAG) {
2524 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2527 if (inp_flags & INP_RECVTOS) {
2528 db_printf("%sINP_RECVTOS", comma ? ", " : "");
2531 if (inp_flags & IN6P_IPV6_V6ONLY) {
2532 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2535 if (inp_flags & IN6P_PKTINFO) {
2536 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2539 if (inp_flags & IN6P_HOPLIMIT) {
2540 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2543 if (inp_flags & IN6P_HOPOPTS) {
2544 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2547 if (inp_flags & IN6P_DSTOPTS) {
2548 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2551 if (inp_flags & IN6P_RTHDR) {
2552 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2555 if (inp_flags & IN6P_RTHDRDSTOPTS) {
2556 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2559 if (inp_flags & IN6P_TCLASS) {
2560 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2563 if (inp_flags & IN6P_AUTOFLOWLABEL) {
2564 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2567 if (inp_flags & INP_TIMEWAIT) {
2568 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2571 if (inp_flags & INP_ONESBCAST) {
2572 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2575 if (inp_flags & INP_DROPPED) {
2576 db_printf("%sINP_DROPPED", comma ? ", " : "");
2579 if (inp_flags & INP_SOCKREF) {
2580 db_printf("%sINP_SOCKREF", comma ? ", " : "");
2583 if (inp_flags & IN6P_RFC2292) {
2584 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2587 if (inp_flags & IN6P_MTU) {
2588 db_printf("IN6P_MTU%s", comma ? ", " : "");
2594 db_print_inpvflag(u_char inp_vflag)
2599 if (inp_vflag & INP_IPV4) {
2600 db_printf("%sINP_IPV4", comma ? ", " : "");
2603 if (inp_vflag & INP_IPV6) {
2604 db_printf("%sINP_IPV6", comma ? ", " : "");
2607 if (inp_vflag & INP_IPV6PROTO) {
2608 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2614 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2617 db_print_indent(indent);
2618 db_printf("%s at %p\n", name, inp);
2622 db_print_indent(indent);
2623 db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2625 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2627 db_print_indent(indent);
2628 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n",
2629 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2631 db_print_indent(indent);
2632 db_printf("inp_label: %p inp_flags: 0x%x (",
2633 inp->inp_label, inp->inp_flags);
2634 db_print_inpflags(inp->inp_flags);
2637 db_print_indent(indent);
2638 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp,
2640 db_print_inpvflag(inp->inp_vflag);
2643 db_print_indent(indent);
2644 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n",
2645 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2647 db_print_indent(indent);
2649 if (inp->inp_vflag & INP_IPV6) {
2650 db_printf("in6p_options: %p in6p_outputopts: %p "
2651 "in6p_moptions: %p\n", inp->in6p_options,
2652 inp->in6p_outputopts, inp->in6p_moptions);
2653 db_printf("in6p_icmp6filt: %p in6p_cksum %d "
2654 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2659 db_printf("inp_ip_tos: %d inp_ip_options: %p "
2660 "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2661 inp->inp_options, inp->inp_moptions);
2664 db_print_indent(indent);
2665 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd,
2666 (uintmax_t)inp->inp_gencnt);
2669 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2674 db_printf("usage: show inpcb <addr>\n");
2677 inp = (struct inpcb *)addr;
2679 db_print_inpcb(inp, "inpcb", 0);