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/route.h>
77 #include <net/rss_config.h>
80 #if defined(INET) || defined(INET6)
81 #include <netinet/in.h>
82 #include <netinet/in_pcb.h>
83 #include <netinet/ip_var.h>
84 #include <netinet/tcp_var.h>
85 #include <netinet/udp.h>
86 #include <netinet/udp_var.h>
89 #include <netinet/in_var.h>
92 #include <netinet/ip6.h>
93 #include <netinet6/in6_pcb.h>
94 #include <netinet6/in6_var.h>
95 #include <netinet6/ip6_var.h>
100 #include <netipsec/ipsec.h>
101 #include <netipsec/key.h>
104 #include <security/mac/mac_framework.h>
106 static struct callout ipport_tick_callout;
109 * These configure the range of local port addresses assigned to
110 * "unspecified" outgoing connections/packets/whatever.
112 VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */
113 VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */
114 VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */
115 VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */
116 VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */
117 VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */
120 * Reserved ports accessible only to root. There are significant
121 * security considerations that must be accounted for when changing these,
122 * but the security benefits can be great. Please be careful.
124 VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */
125 VNET_DEFINE(int, ipport_reservedlow);
127 /* Variables dealing with random ephemeral port allocation. */
128 VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */
129 VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */
130 VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */
131 VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */
132 VNET_DEFINE(int, ipport_tcpallocs);
133 static VNET_DEFINE(int, ipport_tcplastcount);
135 #define V_ipport_tcplastcount VNET(ipport_tcplastcount)
137 static void in_pcbremlists(struct inpcb *inp);
139 static struct inpcb *in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
140 struct in_addr faddr, u_int fport_arg,
141 struct in_addr laddr, u_int lport_arg,
142 int lookupflags, struct ifnet *ifp);
144 #define RANGECHK(var, min, max) \
145 if ((var) < (min)) { (var) = (min); } \
146 else if ((var) > (max)) { (var) = (max); }
149 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
153 error = sysctl_handle_int(oidp, arg1, arg2, req);
155 RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
156 RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
157 RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
158 RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
159 RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
160 RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
167 static SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0,
170 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
171 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
172 &VNET_NAME(ipport_lowfirstauto), 0, &sysctl_net_ipport_check, "I", "");
173 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
174 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
175 &VNET_NAME(ipport_lowlastauto), 0, &sysctl_net_ipport_check, "I", "");
176 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first,
177 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
178 &VNET_NAME(ipport_firstauto), 0, &sysctl_net_ipport_check, "I", "");
179 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last,
180 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
181 &VNET_NAME(ipport_lastauto), 0, &sysctl_net_ipport_check, "I", "");
182 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
183 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
184 &VNET_NAME(ipport_hifirstauto), 0, &sysctl_net_ipport_check, "I", "");
185 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
186 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
187 &VNET_NAME(ipport_hilastauto), 0, &sysctl_net_ipport_check, "I", "");
188 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
189 CTLFLAG_VNET | CTLFLAG_RW | CTLFLAG_SECURE,
190 &VNET_NAME(ipport_reservedhigh), 0, "");
191 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
192 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
193 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized,
194 CTLFLAG_VNET | CTLFLAG_RW,
195 &VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
196 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps,
197 CTLFLAG_VNET | CTLFLAG_RW,
198 &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
199 "allocations before switching to a sequental one");
200 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime,
201 CTLFLAG_VNET | CTLFLAG_RW,
202 &VNET_NAME(ipport_randomtime), 0,
203 "Minimum time to keep sequental port "
204 "allocation before switching to a random one");
208 * in_pcb.c: manage the Protocol Control Blocks.
210 * NOTE: It is assumed that most of these functions will be called with
211 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
212 * functions often modify hash chains or addresses in pcbs.
216 * Initialize an inpcbinfo -- we should be able to reduce the number of
220 in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
221 struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
222 char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
223 uint32_t inpcbzone_flags, u_int hashfields)
226 INP_INFO_LOCK_INIT(pcbinfo, name);
227 INP_HASH_LOCK_INIT(pcbinfo, "pcbinfohash"); /* XXXRW: argument? */
228 INP_LIST_LOCK_INIT(pcbinfo, "pcbinfolist");
230 pcbinfo->ipi_vnet = curvnet;
232 pcbinfo->ipi_listhead = listhead;
233 LIST_INIT(pcbinfo->ipi_listhead);
234 pcbinfo->ipi_count = 0;
235 pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
236 &pcbinfo->ipi_hashmask);
237 pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
238 &pcbinfo->ipi_porthashmask);
240 in_pcbgroup_init(pcbinfo, hashfields, hash_nelements);
242 pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
243 NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
245 uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
246 uma_zone_set_warning(pcbinfo->ipi_zone,
247 "kern.ipc.maxsockets limit reached");
251 * Destroy an inpcbinfo.
254 in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
257 KASSERT(pcbinfo->ipi_count == 0,
258 ("%s: ipi_count = %u", __func__, pcbinfo->ipi_count));
260 hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
261 hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
262 pcbinfo->ipi_porthashmask);
264 in_pcbgroup_destroy(pcbinfo);
266 uma_zdestroy(pcbinfo->ipi_zone);
267 INP_LIST_LOCK_DESTROY(pcbinfo);
268 INP_HASH_LOCK_DESTROY(pcbinfo);
269 INP_INFO_LOCK_DESTROY(pcbinfo);
273 * Allocate a PCB and associate it with the socket.
274 * On success return with the PCB locked.
277 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
283 if (pcbinfo == &V_tcbinfo) {
284 INP_INFO_RLOCK_ASSERT(pcbinfo);
286 INP_INFO_WLOCK_ASSERT(pcbinfo);
291 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
294 bzero(inp, inp_zero_size);
295 inp->inp_pcbinfo = pcbinfo;
296 inp->inp_socket = so;
297 inp->inp_cred = crhold(so->so_cred);
298 inp->inp_inc.inc_fibnum = so->so_fibnum;
300 error = mac_inpcb_init(inp, M_NOWAIT);
303 mac_inpcb_create(so, inp);
306 error = ipsec_init_policy(so, &inp->inp_sp);
309 mac_inpcb_destroy(inp);
315 if (INP_SOCKAF(so) == AF_INET6) {
316 inp->inp_vflag |= INP_IPV6PROTO;
318 inp->inp_flags |= IN6P_IPV6_V6ONLY;
322 INP_LIST_WLOCK(pcbinfo);
323 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
324 pcbinfo->ipi_count++;
325 so->so_pcb = (caddr_t)inp;
327 if (V_ip6_auto_flowlabel)
328 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
330 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
331 refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */
332 INP_LIST_WUNLOCK(pcbinfo);
333 #if defined(IPSEC) || defined(MAC)
336 crfree(inp->inp_cred);
337 uma_zfree(pcbinfo->ipi_zone, inp);
345 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
349 INP_WLOCK_ASSERT(inp);
350 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
352 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
354 anonport = nam == NULL || ((struct sockaddr_in *)nam)->sin_port == 0;
355 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
356 &inp->inp_lport, cred);
359 if (in_pcbinshash(inp) != 0) {
360 inp->inp_laddr.s_addr = INADDR_ANY;
365 inp->inp_flags |= INP_ANONPORT;
371 * Select a local port (number) to use.
373 #if defined(INET) || defined(INET6)
375 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
376 struct ucred *cred, int lookupflags)
378 struct inpcbinfo *pcbinfo;
379 struct inpcb *tmpinp;
380 unsigned short *lastport;
381 int count, dorandom, error;
382 u_short aux, first, last, lport;
384 struct in_addr laddr;
387 pcbinfo = inp->inp_pcbinfo;
390 * Because no actual state changes occur here, a global write lock on
391 * the pcbinfo isn't required.
393 INP_LOCK_ASSERT(inp);
394 INP_HASH_LOCK_ASSERT(pcbinfo);
396 if (inp->inp_flags & INP_HIGHPORT) {
397 first = V_ipport_hifirstauto; /* sysctl */
398 last = V_ipport_hilastauto;
399 lastport = &pcbinfo->ipi_lasthi;
400 } else if (inp->inp_flags & INP_LOWPORT) {
401 error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
404 first = V_ipport_lowfirstauto; /* 1023 */
405 last = V_ipport_lowlastauto; /* 600 */
406 lastport = &pcbinfo->ipi_lastlow;
408 first = V_ipport_firstauto; /* sysctl */
409 last = V_ipport_lastauto;
410 lastport = &pcbinfo->ipi_lastport;
413 * For UDP(-Lite), use random port allocation as long as the user
414 * allows it. For TCP (and as of yet unknown) connections,
415 * use random port allocation only if the user allows it AND
416 * ipport_tick() allows it.
418 if (V_ipport_randomized &&
419 (!V_ipport_stoprandom || pcbinfo == &V_udbinfo ||
420 pcbinfo == &V_ulitecbinfo))
425 * It makes no sense to do random port allocation if
426 * we have the only port available.
430 /* Make sure to not include UDP(-Lite) packets in the count. */
431 if (pcbinfo != &V_udbinfo || pcbinfo != &V_ulitecbinfo)
432 V_ipport_tcpallocs++;
434 * Instead of having two loops further down counting up or down
435 * make sure that first is always <= last and go with only one
436 * code path implementing all logic.
445 /* Make the compiler happy. */
447 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
448 KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
453 tmpinp = NULL; /* Make compiler happy. */
457 *lastport = first + (arc4random() % (last - first));
459 count = last - first;
462 if (count-- < 0) /* completely used? */
463 return (EADDRNOTAVAIL);
465 if (*lastport < first || *lastport > last)
467 lport = htons(*lastport);
470 if ((inp->inp_vflag & INP_IPV6) != 0)
471 tmpinp = in6_pcblookup_local(pcbinfo,
472 &inp->in6p_laddr, lport, lookupflags, cred);
474 #if defined(INET) && defined(INET6)
478 tmpinp = in_pcblookup_local(pcbinfo, laddr,
479 lport, lookupflags, cred);
481 } while (tmpinp != NULL);
484 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
485 laddrp->s_addr = laddr.s_addr;
493 * Return cached socket options.
496 inp_so_options(const struct inpcb *inp)
502 if ((inp->inp_flags2 & INP_REUSEPORT) != 0)
503 so_options |= SO_REUSEPORT;
504 if ((inp->inp_flags2 & INP_REUSEADDR) != 0)
505 so_options |= SO_REUSEADDR;
508 #endif /* INET || INET6 */
511 * Check if a new BINDMULTI socket is allowed to be created.
513 * ni points to the new inp.
514 * oi points to the exisitng inp.
516 * This checks whether the existing inp also has BINDMULTI and
517 * whether the credentials match.
520 in_pcbbind_check_bindmulti(const struct inpcb *ni, const struct inpcb *oi)
522 /* Check permissions match */
523 if ((ni->inp_flags2 & INP_BINDMULTI) &&
524 (ni->inp_cred->cr_uid !=
525 oi->inp_cred->cr_uid))
528 /* Check the existing inp has BINDMULTI set */
529 if ((ni->inp_flags2 & INP_BINDMULTI) &&
530 ((oi->inp_flags2 & INP_BINDMULTI) == 0))
534 * We're okay - either INP_BINDMULTI isn't set on ni, or
535 * it is and it matches the checks.
542 * Set up a bind operation on a PCB, performing port allocation
543 * as required, but do not actually modify the PCB. Callers can
544 * either complete the bind by setting inp_laddr/inp_lport and
545 * calling in_pcbinshash(), or they can just use the resulting
546 * port and address to authorise the sending of a once-off packet.
548 * On error, the values of *laddrp and *lportp are not changed.
551 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
552 u_short *lportp, struct ucred *cred)
554 struct socket *so = inp->inp_socket;
555 struct sockaddr_in *sin;
556 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
557 struct in_addr laddr;
559 int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
563 * No state changes, so read locks are sufficient here.
565 INP_LOCK_ASSERT(inp);
566 INP_HASH_LOCK_ASSERT(pcbinfo);
568 if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
569 return (EADDRNOTAVAIL);
570 laddr.s_addr = *laddrp;
571 if (nam != NULL && laddr.s_addr != INADDR_ANY)
573 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
574 lookupflags = INPLOOKUP_WILDCARD;
576 if ((error = prison_local_ip4(cred, &laddr)) != 0)
579 sin = (struct sockaddr_in *)nam;
580 if (nam->sa_len != sizeof (*sin))
584 * We should check the family, but old programs
585 * incorrectly fail to initialize it.
587 if (sin->sin_family != AF_INET)
588 return (EAFNOSUPPORT);
590 error = prison_local_ip4(cred, &sin->sin_addr);
593 if (sin->sin_port != *lportp) {
594 /* Don't allow the port to change. */
597 lport = sin->sin_port;
599 /* NB: lport is left as 0 if the port isn't being changed. */
600 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
602 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
603 * allow complete duplication of binding if
604 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
605 * and a multicast address is bound on both
606 * new and duplicated sockets.
608 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
609 reuseport = SO_REUSEADDR|SO_REUSEPORT;
610 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
611 sin->sin_port = 0; /* yech... */
612 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
614 * Is the address a local IP address?
615 * If INP_BINDANY is set, then the socket may be bound
616 * to any endpoint address, local or not.
618 if ((inp->inp_flags & INP_BINDANY) == 0 &&
619 ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
620 return (EADDRNOTAVAIL);
622 laddr = sin->sin_addr;
628 if (ntohs(lport) <= V_ipport_reservedhigh &&
629 ntohs(lport) >= V_ipport_reservedlow &&
630 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
633 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
634 priv_check_cred(inp->inp_cred,
635 PRIV_NETINET_REUSEPORT, 0) != 0) {
636 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
637 lport, INPLOOKUP_WILDCARD, cred);
640 * This entire block sorely needs a rewrite.
643 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
644 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
645 (so->so_type != SOCK_STREAM ||
646 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
647 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
648 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
649 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
650 (inp->inp_cred->cr_uid !=
651 t->inp_cred->cr_uid))
655 * If the socket is a BINDMULTI socket, then
656 * the credentials need to match and the
657 * original socket also has to have been bound
660 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
663 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
664 lport, lookupflags, cred);
665 if (t && (t->inp_flags & INP_TIMEWAIT)) {
667 * XXXRW: If an incpb has had its timewait
668 * state recycled, we treat the address as
669 * being in use (for now). This is better
670 * than a panic, but not desirable.
674 (reuseport & tw->tw_so_options) == 0)
677 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
678 (reuseport & inp_so_options(t)) == 0) {
680 if (ntohl(sin->sin_addr.s_addr) !=
682 ntohl(t->inp_laddr.s_addr) !=
684 (inp->inp_vflag & INP_IPV6PROTO) == 0 ||
685 (t->inp_vflag & INP_IPV6PROTO) == 0)
688 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
696 error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
701 *laddrp = laddr.s_addr;
707 * Connect from a socket to a specified address.
708 * Both address and port must be specified in argument sin.
709 * If don't have a local address for this socket yet,
713 in_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
714 struct ucred *cred, struct mbuf *m)
716 u_short lport, fport;
717 in_addr_t laddr, faddr;
720 INP_WLOCK_ASSERT(inp);
721 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
723 lport = inp->inp_lport;
724 laddr = inp->inp_laddr.s_addr;
725 anonport = (lport == 0);
726 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
731 /* Do the initial binding of the local address if required. */
732 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
733 inp->inp_lport = lport;
734 inp->inp_laddr.s_addr = laddr;
735 if (in_pcbinshash(inp) != 0) {
736 inp->inp_laddr.s_addr = INADDR_ANY;
742 /* Commit the remaining changes. */
743 inp->inp_lport = lport;
744 inp->inp_laddr.s_addr = laddr;
745 inp->inp_faddr.s_addr = faddr;
746 inp->inp_fport = fport;
747 in_pcbrehash_mbuf(inp, m);
750 inp->inp_flags |= INP_ANONPORT;
755 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
758 return (in_pcbconnect_mbuf(inp, nam, cred, NULL));
762 * Do proper source address selection on an unbound socket in case
763 * of connect. Take jails into account as well.
766 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
771 struct sockaddr_in *sin;
775 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
778 * Bypass source address selection and use the primary jail IP
781 if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
785 bzero(&sro, sizeof(sro));
787 sin = (struct sockaddr_in *)&sro.ro_dst;
788 sin->sin_family = AF_INET;
789 sin->sin_len = sizeof(struct sockaddr_in);
790 sin->sin_addr.s_addr = faddr->s_addr;
793 * If route is known our src addr is taken from the i/f,
796 * Find out route to destination.
798 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
799 in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
802 * If we found a route, use the address corresponding to
803 * the outgoing interface.
805 * Otherwise assume faddr is reachable on a directly connected
806 * network and try to find a corresponding interface to take
807 * the source address from.
809 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
810 struct in_ifaddr *ia;
813 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin,
814 inp->inp_socket->so_fibnum));
816 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0,
817 inp->inp_socket->so_fibnum));
823 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
824 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
825 ifa_free(&ia->ia_ifa);
830 ifa_free(&ia->ia_ifa);
833 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
836 if (sa->sa_family != AF_INET)
838 sin = (struct sockaddr_in *)sa;
839 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
840 ia = (struct in_ifaddr *)ifa;
845 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
846 IF_ADDR_RUNLOCK(ifp);
849 IF_ADDR_RUNLOCK(ifp);
851 /* 3. As a last resort return the 'default' jail address. */
852 error = prison_get_ip4(cred, laddr);
857 * If the outgoing interface on the route found is not
858 * a loopback interface, use the address from that interface.
859 * In case of jails do those three steps:
860 * 1. check if the interface address belongs to the jail. If so use it.
861 * 2. check if we have any address on the outgoing interface
862 * belonging to this jail. If so use it.
863 * 3. as a last resort return the 'default' jail address.
865 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
866 struct in_ifaddr *ia;
869 /* If not jailed, use the default returned. */
870 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
871 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
872 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
877 /* 1. Check if the iface address belongs to the jail. */
878 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
879 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
880 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
881 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
886 * 2. Check if we have any address on the outgoing interface
887 * belonging to this jail.
890 ifp = sro.ro_rt->rt_ifp;
892 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
894 if (sa->sa_family != AF_INET)
896 sin = (struct sockaddr_in *)sa;
897 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
898 ia = (struct in_ifaddr *)ifa;
903 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
904 IF_ADDR_RUNLOCK(ifp);
907 IF_ADDR_RUNLOCK(ifp);
909 /* 3. As a last resort return the 'default' jail address. */
910 error = prison_get_ip4(cred, laddr);
915 * The outgoing interface is marked with 'loopback net', so a route
916 * to ourselves is here.
917 * Try to find the interface of the destination address and then
918 * take the address from there. That interface is not necessarily
919 * a loopback interface.
920 * In case of jails, check that it is an address of the jail
921 * and if we cannot find, fall back to the 'default' jail address.
923 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
924 struct sockaddr_in sain;
925 struct in_ifaddr *ia;
927 bzero(&sain, sizeof(struct sockaddr_in));
928 sain.sin_family = AF_INET;
929 sain.sin_len = sizeof(struct sockaddr_in);
930 sain.sin_addr.s_addr = faddr->s_addr;
932 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain),
933 inp->inp_socket->so_fibnum));
935 ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0,
936 inp->inp_socket->so_fibnum));
938 ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
940 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
945 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
946 ifa_free(&ia->ia_ifa);
955 ifa_free(&ia->ia_ifa);
958 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
961 if (sa->sa_family != AF_INET)
963 sin = (struct sockaddr_in *)sa;
964 if (prison_check_ip4(cred,
965 &sin->sin_addr) == 0) {
966 ia = (struct in_ifaddr *)ifa;
971 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
972 IF_ADDR_RUNLOCK(ifp);
975 IF_ADDR_RUNLOCK(ifp);
978 /* 3. As a last resort return the 'default' jail address. */
979 error = prison_get_ip4(cred, laddr);
984 if (sro.ro_rt != NULL)
990 * Set up for a connect from a socket to the specified address.
991 * On entry, *laddrp and *lportp should contain the current local
992 * address and port for the PCB; these are updated to the values
993 * that should be placed in inp_laddr and inp_lport to complete
996 * On success, *faddrp and *fportp will be set to the remote address
997 * and port. These are not updated in the error case.
999 * If the operation fails because the connection already exists,
1000 * *oinpp will be set to the PCB of that connection so that the
1001 * caller can decide to override it. In all other cases, *oinpp
1005 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
1006 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
1007 struct inpcb **oinpp, struct ucred *cred)
1009 struct rm_priotracker in_ifa_tracker;
1010 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1011 struct in_ifaddr *ia;
1013 struct in_addr laddr, faddr;
1014 u_short lport, fport;
1018 * Because a global state change doesn't actually occur here, a read
1019 * lock is sufficient.
1021 INP_LOCK_ASSERT(inp);
1022 INP_HASH_LOCK_ASSERT(inp->inp_pcbinfo);
1026 if (nam->sa_len != sizeof (*sin))
1028 if (sin->sin_family != AF_INET)
1029 return (EAFNOSUPPORT);
1030 if (sin->sin_port == 0)
1031 return (EADDRNOTAVAIL);
1032 laddr.s_addr = *laddrp;
1034 faddr = sin->sin_addr;
1035 fport = sin->sin_port;
1037 if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
1039 * If the destination address is INADDR_ANY,
1040 * use the primary local address.
1041 * If the supplied address is INADDR_BROADCAST,
1042 * and the primary interface supports broadcast,
1043 * choose the broadcast address for that interface.
1045 if (faddr.s_addr == INADDR_ANY) {
1046 IN_IFADDR_RLOCK(&in_ifa_tracker);
1048 IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
1049 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1051 (error = prison_get_ip4(cred, &faddr)) != 0)
1053 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
1054 IN_IFADDR_RLOCK(&in_ifa_tracker);
1055 if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
1057 faddr = satosin(&TAILQ_FIRST(
1058 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
1059 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1062 if (laddr.s_addr == INADDR_ANY) {
1063 error = in_pcbladdr(inp, &faddr, &laddr, cred);
1065 * If the destination address is multicast and an outgoing
1066 * interface has been set as a multicast option, prefer the
1067 * address of that interface as our source address.
1069 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
1070 inp->inp_moptions != NULL) {
1071 struct ip_moptions *imo;
1074 imo = inp->inp_moptions;
1075 if (imo->imo_multicast_ifp != NULL) {
1076 ifp = imo->imo_multicast_ifp;
1077 IN_IFADDR_RLOCK(&in_ifa_tracker);
1078 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1079 if ((ia->ia_ifp == ifp) &&
1081 prison_check_ip4(cred,
1082 &ia->ia_addr.sin_addr) == 0))
1086 error = EADDRNOTAVAIL;
1088 laddr = ia->ia_addr.sin_addr;
1091 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1097 oinp = in_pcblookup_hash_locked(inp->inp_pcbinfo, faddr, fport,
1098 laddr, lport, 0, NULL);
1102 return (EADDRINUSE);
1105 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
1110 *laddrp = laddr.s_addr;
1112 *faddrp = faddr.s_addr;
1118 in_pcbdisconnect(struct inpcb *inp)
1121 INP_WLOCK_ASSERT(inp);
1122 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
1124 inp->inp_faddr.s_addr = INADDR_ANY;
1131 * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1132 * For most protocols, this will be invoked immediately prior to calling
1133 * in_pcbfree(). However, with TCP the inpcb may significantly outlive the
1134 * socket, in which case in_pcbfree() is deferred.
1137 in_pcbdetach(struct inpcb *inp)
1140 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1142 inp->inp_socket->so_pcb = NULL;
1143 inp->inp_socket = NULL;
1147 * in_pcbref() bumps the reference count on an inpcb in order to maintain
1148 * stability of an inpcb pointer despite the inpcb lock being released. This
1149 * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
1150 * but where the inpcb lock may already held, or when acquiring a reference
1153 * in_pcbref() should be used only to provide brief memory stability, and
1154 * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
1155 * garbage collect the inpcb if it has been in_pcbfree()'d from another
1156 * context. Until in_pcbrele() has returned that the inpcb is still valid,
1157 * lock and rele are the *only* safe operations that may be performed on the
1160 * While the inpcb will not be freed, releasing the inpcb lock means that the
1161 * connection's state may change, so the caller should be careful to
1162 * revalidate any cached state on reacquiring the lock. Drop the reference
1163 * using in_pcbrele().
1166 in_pcbref(struct inpcb *inp)
1169 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1171 refcount_acquire(&inp->inp_refcount);
1175 * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
1176 * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
1177 * return a flag indicating whether or not the inpcb remains valid. If it is
1178 * valid, we return with the inpcb lock held.
1180 * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
1181 * reference on an inpcb. Historically more work was done here (actually, in
1182 * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
1183 * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely
1184 * about memory stability (and continued use of the write lock).
1187 in_pcbrele_rlocked(struct inpcb *inp)
1189 struct inpcbinfo *pcbinfo;
1191 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1193 INP_RLOCK_ASSERT(inp);
1195 if (refcount_release(&inp->inp_refcount) == 0) {
1197 * If the inpcb has been freed, let the caller know, even if
1198 * this isn't the last reference.
1200 if (inp->inp_flags2 & INP_FREED) {
1207 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1210 pcbinfo = inp->inp_pcbinfo;
1211 uma_zfree(pcbinfo->ipi_zone, inp);
1216 in_pcbrele_wlocked(struct inpcb *inp)
1218 struct inpcbinfo *pcbinfo;
1220 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1222 INP_WLOCK_ASSERT(inp);
1224 if (refcount_release(&inp->inp_refcount) == 0) {
1226 * If the inpcb has been freed, let the caller know, even if
1227 * this isn't the last reference.
1229 if (inp->inp_flags2 & INP_FREED) {
1236 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1239 pcbinfo = inp->inp_pcbinfo;
1240 uma_zfree(pcbinfo->ipi_zone, inp);
1245 * Temporary wrapper.
1248 in_pcbrele(struct inpcb *inp)
1251 return (in_pcbrele_wlocked(inp));
1255 * Unconditionally schedule an inpcb to be freed by decrementing its
1256 * reference count, which should occur only after the inpcb has been detached
1257 * from its socket. If another thread holds a temporary reference (acquired
1258 * using in_pcbref()) then the free is deferred until that reference is
1259 * released using in_pcbrele(), but the inpcb is still unlocked. Almost all
1260 * work, including removal from global lists, is done in this context, where
1261 * the pcbinfo lock is held.
1264 in_pcbfree(struct inpcb *inp)
1266 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1268 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1271 if (pcbinfo == &V_tcbinfo) {
1272 INP_INFO_LOCK_ASSERT(pcbinfo);
1274 INP_INFO_WLOCK_ASSERT(pcbinfo);
1277 INP_WLOCK_ASSERT(inp);
1279 /* XXXRW: Do as much as possible here. */
1281 if (inp->inp_sp != NULL)
1282 ipsec_delete_pcbpolicy(inp);
1284 INP_LIST_WLOCK(pcbinfo);
1285 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1286 in_pcbremlists(inp);
1287 INP_LIST_WUNLOCK(pcbinfo);
1289 if (inp->inp_vflag & INP_IPV6PROTO) {
1290 ip6_freepcbopts(inp->in6p_outputopts);
1291 if (inp->in6p_moptions != NULL)
1292 ip6_freemoptions(inp->in6p_moptions);
1295 if (inp->inp_options)
1296 (void)m_free(inp->inp_options);
1298 if (inp->inp_moptions != NULL)
1299 inp_freemoptions(inp->inp_moptions);
1301 if (inp->inp_route.ro_rt) {
1302 RTFREE(inp->inp_route.ro_rt);
1303 inp->inp_route.ro_rt = (struct rtentry *)NULL;
1307 inp->inp_flags2 |= INP_FREED;
1308 crfree(inp->inp_cred);
1310 mac_inpcb_destroy(inp);
1312 if (!in_pcbrele_wlocked(inp))
1317 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1318 * port reservation, and preventing it from being returned by inpcb lookups.
1320 * It is used by TCP to mark an inpcb as unused and avoid future packet
1321 * delivery or event notification when a socket remains open but TCP has
1322 * closed. This might occur as a result of a shutdown()-initiated TCP close
1323 * or a RST on the wire, and allows the port binding to be reused while still
1324 * maintaining the invariant that so_pcb always points to a valid inpcb until
1327 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1328 * in_pcbnotifyall() and in_pcbpurgeif0()?
1331 in_pcbdrop(struct inpcb *inp)
1334 INP_WLOCK_ASSERT(inp);
1337 * XXXRW: Possibly we should protect the setting of INP_DROPPED with
1340 inp->inp_flags |= INP_DROPPED;
1341 if (inp->inp_flags & INP_INHASHLIST) {
1342 struct inpcbport *phd = inp->inp_phd;
1344 INP_HASH_WLOCK(inp->inp_pcbinfo);
1345 LIST_REMOVE(inp, inp_hash);
1346 LIST_REMOVE(inp, inp_portlist);
1347 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1348 LIST_REMOVE(phd, phd_hash);
1351 INP_HASH_WUNLOCK(inp->inp_pcbinfo);
1352 inp->inp_flags &= ~INP_INHASHLIST;
1354 in_pcbgroup_remove(inp);
1361 * Common routines to return the socket addresses associated with inpcbs.
1364 in_sockaddr(in_port_t port, struct in_addr *addr_p)
1366 struct sockaddr_in *sin;
1368 sin = malloc(sizeof *sin, M_SONAME,
1370 sin->sin_family = AF_INET;
1371 sin->sin_len = sizeof(*sin);
1372 sin->sin_addr = *addr_p;
1373 sin->sin_port = port;
1375 return (struct sockaddr *)sin;
1379 in_getsockaddr(struct socket *so, struct sockaddr **nam)
1382 struct in_addr addr;
1385 inp = sotoinpcb(so);
1386 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1389 port = inp->inp_lport;
1390 addr = inp->inp_laddr;
1393 *nam = in_sockaddr(port, &addr);
1398 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1401 struct in_addr addr;
1404 inp = sotoinpcb(so);
1405 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1408 port = inp->inp_fport;
1409 addr = inp->inp_faddr;
1412 *nam = in_sockaddr(port, &addr);
1417 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1418 struct inpcb *(*notify)(struct inpcb *, int))
1420 struct inpcb *inp, *inp_temp;
1422 INP_INFO_WLOCK(pcbinfo);
1423 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1426 if ((inp->inp_vflag & INP_IPV4) == 0) {
1431 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1432 inp->inp_socket == NULL) {
1436 if ((*notify)(inp, errno))
1439 INP_INFO_WUNLOCK(pcbinfo);
1443 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1446 struct ip_moptions *imo;
1449 INP_INFO_WLOCK(pcbinfo);
1450 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1452 imo = inp->inp_moptions;
1453 if ((inp->inp_vflag & INP_IPV4) &&
1456 * Unselect the outgoing interface if it is being
1459 if (imo->imo_multicast_ifp == ifp)
1460 imo->imo_multicast_ifp = NULL;
1463 * Drop multicast group membership if we joined
1464 * through the interface being detached.
1466 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1468 if (imo->imo_membership[i]->inm_ifp == ifp) {
1469 in_delmulti(imo->imo_membership[i]);
1471 } else if (gap != 0)
1472 imo->imo_membership[i - gap] =
1473 imo->imo_membership[i];
1475 imo->imo_num_memberships -= gap;
1479 INP_INFO_WUNLOCK(pcbinfo);
1483 * Lookup a PCB based on the local address and port. Caller must hold the
1484 * hash lock. No inpcb locks or references are acquired.
1486 #define INP_LOOKUP_MAPPED_PCB_COST 3
1488 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1489 u_short lport, int lookupflags, struct ucred *cred)
1493 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1499 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1500 ("%s: invalid lookup flags %d", __func__, lookupflags));
1502 INP_HASH_LOCK_ASSERT(pcbinfo);
1504 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1505 struct inpcbhead *head;
1507 * Look for an unconnected (wildcard foreign addr) PCB that
1508 * matches the local address and port we're looking for.
1510 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1511 0, pcbinfo->ipi_hashmask)];
1512 LIST_FOREACH(inp, head, inp_hash) {
1514 /* XXX inp locking */
1515 if ((inp->inp_vflag & INP_IPV4) == 0)
1518 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1519 inp->inp_laddr.s_addr == laddr.s_addr &&
1520 inp->inp_lport == lport) {
1525 prison_equal_ip4(cred->cr_prison,
1526 inp->inp_cred->cr_prison))
1535 struct inpcbporthead *porthash;
1536 struct inpcbport *phd;
1537 struct inpcb *match = NULL;
1539 * Best fit PCB lookup.
1541 * First see if this local port is in use by looking on the
1544 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1545 pcbinfo->ipi_porthashmask)];
1546 LIST_FOREACH(phd, porthash, phd_hash) {
1547 if (phd->phd_port == lport)
1552 * Port is in use by one or more PCBs. Look for best
1555 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1558 !prison_equal_ip4(inp->inp_cred->cr_prison,
1562 /* XXX inp locking */
1563 if ((inp->inp_vflag & INP_IPV4) == 0)
1566 * We never select the PCB that has
1567 * INP_IPV6 flag and is bound to :: if
1568 * we have another PCB which is bound
1569 * to 0.0.0.0. If a PCB has the
1570 * INP_IPV6 flag, then we set its cost
1571 * higher than IPv4 only PCBs.
1573 * Note that the case only happens
1574 * when a socket is bound to ::, under
1575 * the condition that the use of the
1576 * mapped address is allowed.
1578 if ((inp->inp_vflag & INP_IPV6) != 0)
1579 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1581 if (inp->inp_faddr.s_addr != INADDR_ANY)
1583 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1584 if (laddr.s_addr == INADDR_ANY)
1586 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1589 if (laddr.s_addr != INADDR_ANY)
1592 if (wildcard < matchwild) {
1594 matchwild = wildcard;
1603 #undef INP_LOOKUP_MAPPED_PCB_COST
1607 * Lookup PCB in hash list, using pcbgroup tables.
1609 static struct inpcb *
1610 in_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
1611 struct in_addr faddr, u_int fport_arg, struct in_addr laddr,
1612 u_int lport_arg, int lookupflags, struct ifnet *ifp)
1614 struct inpcbhead *head;
1615 struct inpcb *inp, *tmpinp;
1616 u_short fport = fport_arg, lport = lport_arg;
1619 * First look for an exact match.
1622 INP_GROUP_LOCK(pcbgroup);
1623 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1624 pcbgroup->ipg_hashmask)];
1625 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1627 /* XXX inp locking */
1628 if ((inp->inp_vflag & INP_IPV4) == 0)
1631 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1632 inp->inp_laddr.s_addr == laddr.s_addr &&
1633 inp->inp_fport == fport &&
1634 inp->inp_lport == lport) {
1636 * XXX We should be able to directly return
1637 * the inp here, without any checks.
1638 * Well unless both bound with SO_REUSEPORT?
1640 if (prison_flag(inp->inp_cred, PR_IP4))
1646 if (tmpinp != NULL) {
1653 * For incoming connections, we may wish to do a wildcard
1654 * match for an RSS-local socket.
1656 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1657 struct inpcb *local_wild = NULL, *local_exact = NULL;
1659 struct inpcb *local_wild_mapped = NULL;
1661 struct inpcb *jail_wild = NULL;
1662 struct inpcbhead *head;
1666 * Order of socket selection - we always prefer jails.
1667 * 1. jailed, non-wild.
1669 * 3. non-jailed, non-wild.
1670 * 4. non-jailed, wild.
1673 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(INADDR_ANY,
1674 lport, 0, pcbgroup->ipg_hashmask)];
1675 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1677 /* XXX inp locking */
1678 if ((inp->inp_vflag & INP_IPV4) == 0)
1681 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1682 inp->inp_lport != lport)
1685 injail = prison_flag(inp->inp_cred, PR_IP4);
1687 if (prison_check_ip4(inp->inp_cred,
1691 if (local_exact != NULL)
1695 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1700 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1702 /* XXX inp locking, NULL check */
1703 if (inp->inp_vflag & INP_IPV6PROTO)
1704 local_wild_mapped = inp;
1712 } /* LIST_FOREACH */
1721 inp = local_wild_mapped;
1729 * Then look for a wildcard match, if requested.
1731 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1732 struct inpcb *local_wild = NULL, *local_exact = NULL;
1734 struct inpcb *local_wild_mapped = NULL;
1736 struct inpcb *jail_wild = NULL;
1737 struct inpcbhead *head;
1741 * Order of socket selection - we always prefer jails.
1742 * 1. jailed, non-wild.
1744 * 3. non-jailed, non-wild.
1745 * 4. non-jailed, wild.
1747 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
1748 0, pcbinfo->ipi_wildmask)];
1749 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1751 /* XXX inp locking */
1752 if ((inp->inp_vflag & INP_IPV4) == 0)
1755 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1756 inp->inp_lport != lport)
1759 injail = prison_flag(inp->inp_cred, PR_IP4);
1761 if (prison_check_ip4(inp->inp_cred,
1765 if (local_exact != NULL)
1769 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1774 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1776 /* XXX inp locking, NULL check */
1777 if (inp->inp_vflag & INP_IPV6PROTO)
1778 local_wild_mapped = inp;
1786 } /* LIST_FOREACH */
1794 inp = local_wild_mapped;
1798 } /* if (lookupflags & INPLOOKUP_WILDCARD) */
1799 INP_GROUP_UNLOCK(pcbgroup);
1804 INP_GROUP_UNLOCK(pcbgroup);
1805 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1807 if (in_pcbrele_wlocked(inp))
1809 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1811 if (in_pcbrele_rlocked(inp))
1814 panic("%s: locking bug", __func__);
1817 #endif /* PCBGROUP */
1820 * Lookup PCB in hash list, using pcbinfo tables. This variation assumes
1821 * that the caller has locked the hash list, and will not perform any further
1822 * locking or reference operations on either the hash list or the connection.
1824 static struct inpcb *
1825 in_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1826 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1829 struct inpcbhead *head;
1830 struct inpcb *inp, *tmpinp;
1831 u_short fport = fport_arg, lport = lport_arg;
1833 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1834 ("%s: invalid lookup flags %d", __func__, lookupflags));
1836 INP_HASH_LOCK_ASSERT(pcbinfo);
1839 * First look for an exact match.
1842 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1843 pcbinfo->ipi_hashmask)];
1844 LIST_FOREACH(inp, head, inp_hash) {
1846 /* XXX inp locking */
1847 if ((inp->inp_vflag & INP_IPV4) == 0)
1850 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1851 inp->inp_laddr.s_addr == laddr.s_addr &&
1852 inp->inp_fport == fport &&
1853 inp->inp_lport == lport) {
1855 * XXX We should be able to directly return
1856 * the inp here, without any checks.
1857 * Well unless both bound with SO_REUSEPORT?
1859 if (prison_flag(inp->inp_cred, PR_IP4))
1869 * Then look for a wildcard match, if requested.
1871 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1872 struct inpcb *local_wild = NULL, *local_exact = NULL;
1874 struct inpcb *local_wild_mapped = NULL;
1876 struct inpcb *jail_wild = NULL;
1880 * Order of socket selection - we always prefer jails.
1881 * 1. jailed, non-wild.
1883 * 3. non-jailed, non-wild.
1884 * 4. non-jailed, wild.
1887 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1888 0, pcbinfo->ipi_hashmask)];
1889 LIST_FOREACH(inp, head, inp_hash) {
1891 /* XXX inp locking */
1892 if ((inp->inp_vflag & INP_IPV4) == 0)
1895 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1896 inp->inp_lport != lport)
1899 injail = prison_flag(inp->inp_cred, PR_IP4);
1901 if (prison_check_ip4(inp->inp_cred,
1905 if (local_exact != NULL)
1909 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1914 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1916 /* XXX inp locking, NULL check */
1917 if (inp->inp_vflag & INP_IPV6PROTO)
1918 local_wild_mapped = inp;
1926 } /* LIST_FOREACH */
1927 if (jail_wild != NULL)
1929 if (local_exact != NULL)
1930 return (local_exact);
1931 if (local_wild != NULL)
1932 return (local_wild);
1934 if (local_wild_mapped != NULL)
1935 return (local_wild_mapped);
1937 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1943 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1944 * hash list lock, and will return the inpcb locked (i.e., requires
1945 * INPLOOKUP_LOCKPCB).
1947 static struct inpcb *
1948 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1949 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
1954 INP_HASH_RLOCK(pcbinfo);
1955 inp = in_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1956 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1959 INP_HASH_RUNLOCK(pcbinfo);
1960 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1962 if (in_pcbrele_wlocked(inp))
1964 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1966 if (in_pcbrele_rlocked(inp))
1969 panic("%s: locking bug", __func__);
1971 INP_HASH_RUNLOCK(pcbinfo);
1976 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1977 * from which a pre-calculated hash value may be extracted.
1979 * Possibly more of this logic should be in in_pcbgroup.c.
1982 in_pcblookup(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport,
1983 struct in_addr laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1985 #if defined(PCBGROUP) && !defined(RSS)
1986 struct inpcbgroup *pcbgroup;
1989 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1990 ("%s: invalid lookup flags %d", __func__, lookupflags));
1991 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1992 ("%s: LOCKPCB not set", __func__));
1995 * When not using RSS, use connection groups in preference to the
1996 * reservation table when looking up 4-tuples. When using RSS, just
1997 * use the reservation table, due to the cost of the Toeplitz hash
2000 * XXXRW: This policy belongs in the pcbgroup code, as in principle
2001 * we could be doing RSS with a non-Toeplitz hash that is affordable
2004 #if defined(PCBGROUP) && !defined(RSS)
2005 if (in_pcbgroup_enabled(pcbinfo)) {
2006 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
2008 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
2009 laddr, lport, lookupflags, ifp));
2012 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2017 in_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in_addr faddr,
2018 u_int fport, struct in_addr laddr, u_int lport, int lookupflags,
2019 struct ifnet *ifp, struct mbuf *m)
2022 struct inpcbgroup *pcbgroup;
2025 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
2026 ("%s: invalid lookup flags %d", __func__, lookupflags));
2027 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
2028 ("%s: LOCKPCB not set", __func__));
2032 * If we can use a hardware-generated hash to look up the connection
2033 * group, use that connection group to find the inpcb. Otherwise
2034 * fall back on a software hash -- or the reservation table if we're
2037 * XXXRW: As above, that policy belongs in the pcbgroup code.
2039 if (in_pcbgroup_enabled(pcbinfo) &&
2040 !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
2041 pcbgroup = in_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
2042 m->m_pkthdr.flowid);
2043 if (pcbgroup != NULL)
2044 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr,
2045 fport, laddr, lport, lookupflags, ifp));
2047 pcbgroup = in_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
2049 return (in_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
2050 laddr, lport, lookupflags, ifp));
2054 return (in_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
2060 * Insert PCB onto various hash lists.
2063 in_pcbinshash_internal(struct inpcb *inp, int do_pcbgroup_update)
2065 struct inpcbhead *pcbhash;
2066 struct inpcbporthead *pcbporthash;
2067 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2068 struct inpcbport *phd;
2069 u_int32_t hashkey_faddr;
2071 INP_WLOCK_ASSERT(inp);
2072 INP_HASH_WLOCK_ASSERT(pcbinfo);
2074 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
2075 ("in_pcbinshash: INP_INHASHLIST"));
2078 if (inp->inp_vflag & INP_IPV6)
2079 hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2082 hashkey_faddr = inp->inp_faddr.s_addr;
2084 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2085 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
2087 pcbporthash = &pcbinfo->ipi_porthashbase[
2088 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
2091 * Go through port list and look for a head for this lport.
2093 LIST_FOREACH(phd, pcbporthash, phd_hash) {
2094 if (phd->phd_port == inp->inp_lport)
2098 * If none exists, malloc one and tack it on.
2101 phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
2103 return (ENOBUFS); /* XXX */
2105 phd->phd_port = inp->inp_lport;
2106 LIST_INIT(&phd->phd_pcblist);
2107 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
2110 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
2111 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
2112 inp->inp_flags |= INP_INHASHLIST;
2114 if (do_pcbgroup_update)
2115 in_pcbgroup_update(inp);
2121 * For now, there are two public interfaces to insert an inpcb into the hash
2122 * lists -- one that does update pcbgroups, and one that doesn't. The latter
2123 * is used only in the TCP syncache, where in_pcbinshash is called before the
2124 * full 4-tuple is set for the inpcb, and we don't want to install in the
2125 * pcbgroup until later.
2127 * XXXRW: This seems like a misfeature. in_pcbinshash should always update
2128 * connection groups, and partially initialised inpcbs should not be exposed
2129 * to either reservation hash tables or pcbgroups.
2132 in_pcbinshash(struct inpcb *inp)
2135 return (in_pcbinshash_internal(inp, 1));
2139 in_pcbinshash_nopcbgroup(struct inpcb *inp)
2142 return (in_pcbinshash_internal(inp, 0));
2146 * Move PCB to the proper hash bucket when { faddr, fport } have been
2147 * changed. NOTE: This does not handle the case of the lport changing (the
2148 * hashed port list would have to be updated as well), so the lport must
2149 * not change after in_pcbinshash() has been called.
2152 in_pcbrehash_mbuf(struct inpcb *inp, struct mbuf *m)
2154 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2155 struct inpcbhead *head;
2156 u_int32_t hashkey_faddr;
2158 INP_WLOCK_ASSERT(inp);
2159 INP_HASH_WLOCK_ASSERT(pcbinfo);
2161 KASSERT(inp->inp_flags & INP_INHASHLIST,
2162 ("in_pcbrehash: !INP_INHASHLIST"));
2165 if (inp->inp_vflag & INP_IPV6)
2166 hashkey_faddr = INP6_PCBHASHKEY(&inp->in6p_faddr);
2169 hashkey_faddr = inp->inp_faddr.s_addr;
2171 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
2172 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
2174 LIST_REMOVE(inp, inp_hash);
2175 LIST_INSERT_HEAD(head, inp, inp_hash);
2179 in_pcbgroup_update_mbuf(inp, m);
2181 in_pcbgroup_update(inp);
2186 in_pcbrehash(struct inpcb *inp)
2189 in_pcbrehash_mbuf(inp, NULL);
2193 * Remove PCB from various lists.
2196 in_pcbremlists(struct inpcb *inp)
2198 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2201 if (pcbinfo == &V_tcbinfo) {
2202 INP_INFO_RLOCK_ASSERT(pcbinfo);
2204 INP_INFO_WLOCK_ASSERT(pcbinfo);
2208 INP_WLOCK_ASSERT(inp);
2209 INP_LIST_WLOCK_ASSERT(pcbinfo);
2211 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
2212 if (inp->inp_flags & INP_INHASHLIST) {
2213 struct inpcbport *phd = inp->inp_phd;
2215 INP_HASH_WLOCK(pcbinfo);
2216 LIST_REMOVE(inp, inp_hash);
2217 LIST_REMOVE(inp, inp_portlist);
2218 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2219 LIST_REMOVE(phd, phd_hash);
2222 INP_HASH_WUNLOCK(pcbinfo);
2223 inp->inp_flags &= ~INP_INHASHLIST;
2225 LIST_REMOVE(inp, inp_list);
2226 pcbinfo->ipi_count--;
2228 in_pcbgroup_remove(inp);
2233 * Check for alternatives when higher level complains
2234 * about service problems. For now, invalidate cached
2235 * routing information. If the route was created dynamically
2236 * (by a redirect), time to try a default gateway again.
2239 in_losing(struct inpcb *inp)
2242 if (inp->inp_route.ro_rt) {
2243 RTFREE(inp->inp_route.ro_rt);
2244 inp->inp_route.ro_rt = (struct rtentry *)NULL;
2250 * A set label operation has occurred at the socket layer, propagate the
2251 * label change into the in_pcb for the socket.
2254 in_pcbsosetlabel(struct socket *so)
2259 inp = sotoinpcb(so);
2260 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
2264 mac_inpcb_sosetlabel(so, inp);
2271 * ipport_tick runs once per second, determining if random port allocation
2272 * should be continued. If more than ipport_randomcps ports have been
2273 * allocated in the last second, then we return to sequential port
2274 * allocation. We return to random allocation only once we drop below
2275 * ipport_randomcps for at least ipport_randomtime seconds.
2278 ipport_tick(void *xtp)
2280 VNET_ITERATOR_DECL(vnet_iter);
2282 VNET_LIST_RLOCK_NOSLEEP();
2283 VNET_FOREACH(vnet_iter) {
2284 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */
2285 if (V_ipport_tcpallocs <=
2286 V_ipport_tcplastcount + V_ipport_randomcps) {
2287 if (V_ipport_stoprandom > 0)
2288 V_ipport_stoprandom--;
2290 V_ipport_stoprandom = V_ipport_randomtime;
2291 V_ipport_tcplastcount = V_ipport_tcpallocs;
2294 VNET_LIST_RUNLOCK_NOSLEEP();
2295 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
2302 callout_stop(&ipport_tick_callout);
2306 * The ipport_callout should start running at about the time we attach the
2307 * inet or inet6 domains.
2310 ipport_tick_init(const void *unused __unused)
2313 /* Start ipport_tick. */
2314 callout_init(&ipport_tick_callout, 1);
2315 callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
2316 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
2317 SHUTDOWN_PRI_DEFAULT);
2319 SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
2320 ipport_tick_init, NULL);
2323 inp_wlock(struct inpcb *inp)
2330 inp_wunlock(struct inpcb *inp)
2337 inp_rlock(struct inpcb *inp)
2344 inp_runlock(struct inpcb *inp)
2352 inp_lock_assert(struct inpcb *inp)
2355 INP_WLOCK_ASSERT(inp);
2359 inp_unlock_assert(struct inpcb *inp)
2362 INP_UNLOCK_ASSERT(inp);
2367 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
2371 INP_INFO_WLOCK(&V_tcbinfo);
2372 LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
2377 INP_INFO_WUNLOCK(&V_tcbinfo);
2381 inp_inpcbtosocket(struct inpcb *inp)
2384 INP_WLOCK_ASSERT(inp);
2385 return (inp->inp_socket);
2389 inp_inpcbtotcpcb(struct inpcb *inp)
2392 INP_WLOCK_ASSERT(inp);
2393 return ((struct tcpcb *)inp->inp_ppcb);
2397 inp_ip_tos_get(const struct inpcb *inp)
2400 return (inp->inp_ip_tos);
2404 inp_ip_tos_set(struct inpcb *inp, int val)
2407 inp->inp_ip_tos = val;
2411 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
2412 uint32_t *faddr, uint16_t *fp)
2415 INP_LOCK_ASSERT(inp);
2416 *laddr = inp->inp_laddr.s_addr;
2417 *faddr = inp->inp_faddr.s_addr;
2418 *lp = inp->inp_lport;
2419 *fp = inp->inp_fport;
2423 so_sotoinpcb(struct socket *so)
2426 return (sotoinpcb(so));
2430 so_sototcpcb(struct socket *so)
2433 return (sototcpcb(so));
2438 db_print_indent(int indent)
2442 for (i = 0; i < indent; i++)
2447 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
2449 char faddr_str[48], laddr_str[48];
2451 db_print_indent(indent);
2452 db_printf("%s at %p\n", name, inc);
2457 if (inc->inc_flags & INC_ISIPV6) {
2459 ip6_sprintf(laddr_str, &inc->inc6_laddr);
2460 ip6_sprintf(faddr_str, &inc->inc6_faddr);
2465 inet_ntoa_r(inc->inc_laddr, laddr_str);
2466 inet_ntoa_r(inc->inc_faddr, faddr_str);
2468 db_print_indent(indent);
2469 db_printf("inc_laddr %s inc_lport %u\n", laddr_str,
2470 ntohs(inc->inc_lport));
2471 db_print_indent(indent);
2472 db_printf("inc_faddr %s inc_fport %u\n", faddr_str,
2473 ntohs(inc->inc_fport));
2477 db_print_inpflags(int inp_flags)
2482 if (inp_flags & INP_RECVOPTS) {
2483 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
2486 if (inp_flags & INP_RECVRETOPTS) {
2487 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
2490 if (inp_flags & INP_RECVDSTADDR) {
2491 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
2494 if (inp_flags & INP_HDRINCL) {
2495 db_printf("%sINP_HDRINCL", comma ? ", " : "");
2498 if (inp_flags & INP_HIGHPORT) {
2499 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
2502 if (inp_flags & INP_LOWPORT) {
2503 db_printf("%sINP_LOWPORT", comma ? ", " : "");
2506 if (inp_flags & INP_ANONPORT) {
2507 db_printf("%sINP_ANONPORT", comma ? ", " : "");
2510 if (inp_flags & INP_RECVIF) {
2511 db_printf("%sINP_RECVIF", comma ? ", " : "");
2514 if (inp_flags & INP_MTUDISC) {
2515 db_printf("%sINP_MTUDISC", comma ? ", " : "");
2518 if (inp_flags & INP_RECVTTL) {
2519 db_printf("%sINP_RECVTTL", comma ? ", " : "");
2522 if (inp_flags & INP_DONTFRAG) {
2523 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
2526 if (inp_flags & INP_RECVTOS) {
2527 db_printf("%sINP_RECVTOS", comma ? ", " : "");
2530 if (inp_flags & IN6P_IPV6_V6ONLY) {
2531 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
2534 if (inp_flags & IN6P_PKTINFO) {
2535 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
2538 if (inp_flags & IN6P_HOPLIMIT) {
2539 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
2542 if (inp_flags & IN6P_HOPOPTS) {
2543 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
2546 if (inp_flags & IN6P_DSTOPTS) {
2547 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
2550 if (inp_flags & IN6P_RTHDR) {
2551 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2554 if (inp_flags & IN6P_RTHDRDSTOPTS) {
2555 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2558 if (inp_flags & IN6P_TCLASS) {
2559 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2562 if (inp_flags & IN6P_AUTOFLOWLABEL) {
2563 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2566 if (inp_flags & INP_TIMEWAIT) {
2567 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2570 if (inp_flags & INP_ONESBCAST) {
2571 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2574 if (inp_flags & INP_DROPPED) {
2575 db_printf("%sINP_DROPPED", comma ? ", " : "");
2578 if (inp_flags & INP_SOCKREF) {
2579 db_printf("%sINP_SOCKREF", comma ? ", " : "");
2582 if (inp_flags & IN6P_RFC2292) {
2583 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2586 if (inp_flags & IN6P_MTU) {
2587 db_printf("IN6P_MTU%s", comma ? ", " : "");
2593 db_print_inpvflag(u_char inp_vflag)
2598 if (inp_vflag & INP_IPV4) {
2599 db_printf("%sINP_IPV4", comma ? ", " : "");
2602 if (inp_vflag & INP_IPV6) {
2603 db_printf("%sINP_IPV6", comma ? ", " : "");
2606 if (inp_vflag & INP_IPV6PROTO) {
2607 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2613 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2616 db_print_indent(indent);
2617 db_printf("%s at %p\n", name, inp);
2621 db_print_indent(indent);
2622 db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2624 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2626 db_print_indent(indent);
2627 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n",
2628 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2630 db_print_indent(indent);
2631 db_printf("inp_label: %p inp_flags: 0x%x (",
2632 inp->inp_label, inp->inp_flags);
2633 db_print_inpflags(inp->inp_flags);
2636 db_print_indent(indent);
2637 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp,
2639 db_print_inpvflag(inp->inp_vflag);
2642 db_print_indent(indent);
2643 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n",
2644 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2646 db_print_indent(indent);
2648 if (inp->inp_vflag & INP_IPV6) {
2649 db_printf("in6p_options: %p in6p_outputopts: %p "
2650 "in6p_moptions: %p\n", inp->in6p_options,
2651 inp->in6p_outputopts, inp->in6p_moptions);
2652 db_printf("in6p_icmp6filt: %p in6p_cksum %d "
2653 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2658 db_printf("inp_ip_tos: %d inp_ip_options: %p "
2659 "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2660 inp->inp_options, inp->inp_moptions);
2663 db_print_indent(indent);
2664 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd,
2665 (uintmax_t)inp->inp_gencnt);
2668 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2673 db_printf("usage: show inpcb <addr>\n");
2676 inp = (struct inpcb *)addr;
2678 db_print_inpcb(inp, "inpcb", 0);