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
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_ipsec.h"
39 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
53 #include <sys/kernel.h>
54 #include <sys/sysctl.h>
63 #include <net/if_types.h>
64 #include <net/route.h>
66 #include <netinet/in.h>
67 #include <netinet/in_pcb.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet/udp.h>
72 #include <netinet/udp_var.h>
74 #include <netinet/ip6.h>
75 #include <netinet6/ip6_var.h>
80 #include <netipsec/ipsec.h>
81 #include <netipsec/key.h>
84 #include <security/mac/mac_framework.h>
87 * These configure the range of local port addresses assigned to
88 * "unspecified" outgoing connections/packets/whatever.
90 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
91 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
92 int ipport_firstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
93 int ipport_lastauto = IPPORT_HILASTAUTO; /* 65535 */
94 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
95 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
98 * Reserved ports accessible only to root. There are significant
99 * security considerations that must be accounted for when changing these,
100 * but the security benefits can be great. Please be careful.
102 int ipport_reservedhigh = IPPORT_RESERVED - 1; /* 1023 */
103 int ipport_reservedlow = 0;
105 /* Variables dealing with random ephemeral port allocation. */
106 int ipport_randomized = 1; /* user controlled via sysctl */
107 int ipport_randomcps = 10; /* user controlled via sysctl */
108 int ipport_randomtime = 45; /* user controlled via sysctl */
109 int ipport_stoprandom = 0; /* toggled by ipport_tick */
110 int ipport_tcpallocs;
111 int ipport_tcplastcount;
113 #define RANGECHK(var, min, max) \
114 if ((var) < (min)) { (var) = (min); } \
115 else if ((var) > (max)) { (var) = (max); }
118 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
122 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
124 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
125 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
126 RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
127 RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
128 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
129 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
136 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
138 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
139 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
140 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
141 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
142 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
143 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
144 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
145 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
146 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
147 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
148 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
149 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
150 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
151 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
152 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
153 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
154 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
155 &ipport_randomized, 0, "Enable random port allocation");
156 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
157 &ipport_randomcps, 0, "Maximum number of random port "
158 "allocations before switching to a sequental one");
159 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
160 &ipport_randomtime, 0, "Minimum time to keep sequental port "
161 "allocation before switching to a random one");
164 * in_pcb.c: manage the Protocol Control Blocks.
166 * NOTE: It is assumed that most of these functions will be called with
167 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
168 * functions often modify hash chains or addresses in pcbs.
172 * Allocate a PCB and associate it with the socket.
173 * On success return with the PCB locked.
176 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
181 INP_INFO_WLOCK_ASSERT(pcbinfo);
183 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
186 bzero(inp, inp_zero_size);
187 inp->inp_pcbinfo = pcbinfo;
188 inp->inp_socket = so;
189 inp->inp_cred = crhold(so->so_cred);
190 inp->inp_inc.inc_fibnum = so->so_fibnum;
192 error = mac_init_inpcb(inp, M_NOWAIT);
196 mac_create_inpcb_from_socket(so, inp);
200 error = ipsec_init_policy(so, &inp->inp_sp);
203 mac_destroy_inpcb(inp);
209 if (INP_SOCKAF(so) == AF_INET6) {
210 inp->inp_vflag |= INP_IPV6PROTO;
212 inp->inp_flags |= IN6P_IPV6_V6ONLY;
215 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
216 pcbinfo->ipi_count++;
217 so->so_pcb = (caddr_t)inp;
219 if (ip6_auto_flowlabel)
220 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
223 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
224 #if defined(IPSEC) || defined(MAC)
227 crfree(inp->inp_cred);
228 uma_zfree(pcbinfo->ipi_zone, inp);
235 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
239 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
240 INP_WLOCK_ASSERT(inp);
242 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
244 anonport = inp->inp_lport == 0 && (nam == NULL ||
245 ((struct sockaddr_in *)nam)->sin_port == 0);
246 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
247 &inp->inp_lport, cred);
250 if (in_pcbinshash(inp) != 0) {
251 inp->inp_laddr.s_addr = INADDR_ANY;
256 inp->inp_flags |= INP_ANONPORT;
261 * Set up a bind operation on a PCB, performing port allocation
262 * as required, but do not actually modify the PCB. Callers can
263 * either complete the bind by setting inp_laddr/inp_lport and
264 * calling in_pcbinshash(), or they can just use the resulting
265 * port and address to authorise the sending of a once-off packet.
267 * On error, the values of *laddrp and *lportp are not changed.
270 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
271 u_short *lportp, struct ucred *cred)
273 struct socket *so = inp->inp_socket;
274 unsigned short *lastport;
275 struct sockaddr_in *sin;
276 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
277 struct in_addr laddr;
279 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
284 * Because no actual state changes occur here, a global write lock on
285 * the pcbinfo isn't required.
287 INP_INFO_LOCK_ASSERT(pcbinfo);
288 INP_LOCK_ASSERT(inp);
290 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
291 return (EADDRNOTAVAIL);
292 laddr.s_addr = *laddrp;
293 if (nam != NULL && laddr.s_addr != INADDR_ANY)
295 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
296 wild = INPLOOKUP_WILDCARD;
298 if ((error = prison_local_ip4(cred, &laddr)) != 0)
301 sin = (struct sockaddr_in *)nam;
302 if (nam->sa_len != sizeof (*sin))
306 * We should check the family, but old programs
307 * incorrectly fail to initialize it.
309 if (sin->sin_family != AF_INET)
310 return (EAFNOSUPPORT);
312 error = prison_local_ip4(cred, &sin->sin_addr);
315 if (sin->sin_port != *lportp) {
316 /* Don't allow the port to change. */
319 lport = sin->sin_port;
321 /* NB: lport is left as 0 if the port isn't being changed. */
322 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
324 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
325 * allow complete duplication of binding if
326 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
327 * and a multicast address is bound on both
328 * new and duplicated sockets.
330 if (so->so_options & SO_REUSEADDR)
331 reuseport = SO_REUSEADDR|SO_REUSEPORT;
332 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
333 sin->sin_port = 0; /* yech... */
334 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
335 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
336 return (EADDRNOTAVAIL);
338 laddr = sin->sin_addr;
344 if (ntohs(lport) <= ipport_reservedhigh &&
345 ntohs(lport) >= ipport_reservedlow &&
346 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
349 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
350 priv_check_cred(inp->inp_cred,
351 PRIV_NETINET_REUSEPORT, 0) != 0) {
352 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
353 lport, INPLOOKUP_WILDCARD, cred);
356 * This entire block sorely needs a rewrite.
359 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
360 (so->so_type != SOCK_STREAM ||
361 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
362 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
363 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
364 (t->inp_socket->so_options &
365 SO_REUSEPORT) == 0) &&
366 (inp->inp_cred->cr_uid !=
367 t->inp_cred->cr_uid))
370 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
372 if (t && (t->inp_flags & INP_TIMEWAIT)) {
374 * XXXRW: If an incpb has had its timewait
375 * state recycled, we treat the address as
376 * being in use (for now). This is better
377 * than a panic, but not desirable.
381 (reuseport & tw->tw_so_options) == 0)
384 (reuseport & t->inp_socket->so_options) == 0) {
386 if (ntohl(sin->sin_addr.s_addr) !=
388 ntohl(t->inp_laddr.s_addr) !=
391 INP_SOCKAF(t->inp_socket))
403 if (inp->inp_flags & INP_HIGHPORT) {
404 first = ipport_hifirstauto; /* sysctl */
405 last = ipport_hilastauto;
406 lastport = &pcbinfo->ipi_lasthi;
407 } else if (inp->inp_flags & INP_LOWPORT) {
408 error = priv_check_cred(cred,
409 PRIV_NETINET_RESERVEDPORT, 0);
412 first = ipport_lowfirstauto; /* 1023 */
413 last = ipport_lowlastauto; /* 600 */
414 lastport = &pcbinfo->ipi_lastlow;
416 first = ipport_firstauto; /* sysctl */
417 last = ipport_lastauto;
418 lastport = &pcbinfo->ipi_lastport;
421 * For UDP, use random port allocation as long as the user
422 * allows it. For TCP (and as of yet unknown) connections,
423 * use random port allocation only if the user allows it AND
424 * ipport_tick() allows it.
426 if (ipport_randomized &&
427 (!ipport_stoprandom || pcbinfo == &udbinfo))
432 * It makes no sense to do random port allocation if
433 * we have the only port available.
437 /* Make sure to not include UDP packets in the count. */
438 if (pcbinfo != &udbinfo)
441 * Instead of having two loops further down counting up or down
442 * make sure that first is always <= last and go with only one
443 * code path implementing all logic.
445 * We split the two cases (up and down) so that the direction
446 * is not being tested on each round of the loop.
454 (arc4random() % (first - last));
455 count = first - last;
458 if (count-- < 0) /* completely used? */
459 return (EADDRNOTAVAIL);
461 if (*lastport > first || *lastport < last)
463 lport = htons(*lastport);
464 } while (in_pcblookup_local(pcbinfo, laddr, lport,
472 (arc4random() % (last - first));
473 count = last - first;
476 if (count-- < 0) /* completely used? */
477 return (EADDRNOTAVAIL);
479 if (*lastport < first || *lastport > last)
481 lport = htons(*lastport);
482 } while (in_pcblookup_local(pcbinfo, laddr, lport,
486 *laddrp = laddr.s_addr;
492 * Connect from a socket to a specified address.
493 * Both address and port must be specified in argument sin.
494 * If don't have a local address for this socket yet,
498 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
500 u_short lport, fport;
501 in_addr_t laddr, faddr;
504 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
505 INP_WLOCK_ASSERT(inp);
507 lport = inp->inp_lport;
508 laddr = inp->inp_laddr.s_addr;
509 anonport = (lport == 0);
510 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
515 /* Do the initial binding of the local address if required. */
516 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
517 inp->inp_lport = lport;
518 inp->inp_laddr.s_addr = laddr;
519 if (in_pcbinshash(inp) != 0) {
520 inp->inp_laddr.s_addr = INADDR_ANY;
526 /* Commit the remaining changes. */
527 inp->inp_lport = lport;
528 inp->inp_laddr.s_addr = laddr;
529 inp->inp_faddr.s_addr = faddr;
530 inp->inp_fport = fport;
534 inp->inp_flags |= INP_ANONPORT;
539 * Do proper source address selection on an unbound socket in case
540 * of connect. Take jails into account as well.
543 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
546 struct in_ifaddr *ia;
549 struct sockaddr_in *sin;
553 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
557 bzero(&sro, sizeof(sro));
559 sin = (struct sockaddr_in *)&sro.ro_dst;
560 sin->sin_family = AF_INET;
561 sin->sin_len = sizeof(struct sockaddr_in);
562 sin->sin_addr.s_addr = faddr->s_addr;
565 * If route is known our src addr is taken from the i/f,
568 * Find out route to destination.
570 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
571 in_rtalloc_ign(&sro, RTF_CLONING, inp->inp_inc.inc_fibnum);
574 * If we found a route, use the address corresponding to
575 * the outgoing interface.
577 * Otherwise assume faddr is reachable on a directly connected
578 * network and try to find a corresponding interface to take
579 * the source address from.
581 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
584 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
586 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin));
592 if (cred == NULL || !jailed(cred)) {
593 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
599 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
602 if (sa->sa_family != AF_INET)
604 sin = (struct sockaddr_in *)sa;
605 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
606 ia = (struct in_ifaddr *)ifa;
611 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
615 /* 3. As a last resort return the 'default' jail address. */
616 error = prison_get_ip4(cred, laddr);
621 * If the outgoing interface on the route found is not
622 * a loopback interface, use the address from that interface.
623 * In case of jails do those three steps:
624 * 1. check if the interface address belongs to the jail. If so use it.
625 * 2. check if we have any address on the outgoing interface
626 * belonging to this jail. If so use it.
627 * 3. as a last resort return the 'default' jail address.
629 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
631 /* If not jailed, use the default returned. */
632 if (cred == NULL || !jailed(cred)) {
633 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
634 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
639 /* 1. Check if the iface address belongs to the jail. */
640 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
641 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
642 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
643 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
648 * 2. Check if we have any address on the outgoing interface
649 * belonging to this jail.
651 TAILQ_FOREACH(ifa, &sro.ro_rt->rt_ifp->if_addrhead, ifa_link) {
654 if (sa->sa_family != AF_INET)
656 sin = (struct sockaddr_in *)sa;
657 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
658 ia = (struct in_ifaddr *)ifa;
663 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
667 /* 3. As a last resort return the 'default' jail address. */
668 error = prison_get_ip4(cred, laddr);
673 * The outgoing interface is marked with 'loopback net', so a route
674 * to ourselves is here.
675 * Try to find the interface of the destination address and then
676 * take the address from there. That interface is not necessarily
677 * a loopback interface.
678 * In case of jails, check that it is an address of the jail
679 * and if we cannot find, fall back to the 'default' jail address.
681 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
682 struct sockaddr_in sain;
684 bzero(&sain, sizeof(struct sockaddr_in));
685 sain.sin_family = AF_INET;
686 sain.sin_len = sizeof(struct sockaddr_in);
687 sain.sin_addr.s_addr = faddr->s_addr;
689 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
691 ia = ifatoia(ifa_ifwithnet(sintosa(&sain)));
693 if (cred == NULL || !jailed(cred)) {
694 #if __FreeBSD_version < 800000
696 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
702 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
712 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
715 if (sa->sa_family != AF_INET)
717 sin = (struct sockaddr_in *)sa;
718 if (prison_check_ip4(cred,
719 &sin->sin_addr) == 0) {
720 ia = (struct in_ifaddr *)ifa;
725 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
730 /* 3. As a last resort return the 'default' jail address. */
731 error = prison_get_ip4(cred, laddr);
736 if (sro.ro_rt != NULL)
742 * Set up for a connect from a socket to the specified address.
743 * On entry, *laddrp and *lportp should contain the current local
744 * address and port for the PCB; these are updated to the values
745 * that should be placed in inp_laddr and inp_lport to complete
748 * On success, *faddrp and *fportp will be set to the remote address
749 * and port. These are not updated in the error case.
751 * If the operation fails because the connection already exists,
752 * *oinpp will be set to the PCB of that connection so that the
753 * caller can decide to override it. In all other cases, *oinpp
757 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
758 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
759 struct inpcb **oinpp, struct ucred *cred)
761 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
762 struct in_ifaddr *ia;
764 struct in_addr laddr, faddr;
765 u_short lport, fport;
769 * Because a global state change doesn't actually occur here, a read
770 * lock is sufficient.
772 INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo);
773 INP_LOCK_ASSERT(inp);
777 if (nam->sa_len != sizeof (*sin))
779 if (sin->sin_family != AF_INET)
780 return (EAFNOSUPPORT);
781 if (sin->sin_port == 0)
782 return (EADDRNOTAVAIL);
783 laddr.s_addr = *laddrp;
785 faddr = sin->sin_addr;
786 fport = sin->sin_port;
788 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
790 * If the destination address is INADDR_ANY,
791 * use the primary local address.
792 * If the supplied address is INADDR_BROADCAST,
793 * and the primary interface supports broadcast,
794 * choose the broadcast address for that interface.
796 if (faddr.s_addr == INADDR_ANY) {
798 IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
800 (error = prison_get_ip4(cred, &faddr)) != 0)
802 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
803 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
805 faddr = satosin(&TAILQ_FIRST(
806 &in_ifaddrhead)->ia_broadaddr)->sin_addr;
808 if (laddr.s_addr == INADDR_ANY) {
809 error = in_pcbladdr(inp, &faddr, &laddr, cred);
814 * If the destination address is multicast and an outgoing
815 * interface has been set as a multicast option, use the
816 * address of that interface as our source address.
818 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
819 inp->inp_moptions != NULL) {
820 struct ip_moptions *imo;
823 imo = inp->inp_moptions;
824 if (imo->imo_multicast_ifp != NULL) {
825 ifp = imo->imo_multicast_ifp;
826 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
827 if (ia->ia_ifp == ifp)
830 return (EADDRNOTAVAIL);
831 laddr = ia->ia_addr.sin_addr;
836 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
844 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
849 *laddrp = laddr.s_addr;
851 *faddrp = faddr.s_addr;
857 in_pcbdisconnect(struct inpcb *inp)
860 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
861 INP_WLOCK_ASSERT(inp);
863 inp->inp_faddr.s_addr = INADDR_ANY;
869 * Historically, in_pcbdetach() included the functionality now found in
870 * in_pcbfree() and in_pcbdrop(). They are now broken out to reflect the
871 * more complex life cycle of TCP.
873 * in_pcbdetach() is responsibe for disconnecting the socket from an inpcb.
874 * For most protocols, this will be invoked immediately prior to calling
875 * in_pcbfree(). However, for TCP the inpcb may significantly outlive the
876 * socket, in which case in_pcbfree() may be deferred.
879 in_pcbdetach(struct inpcb *inp)
882 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
884 inp->inp_socket->so_pcb = NULL;
885 inp->inp_socket = NULL;
889 * in_pcbfree() is responsible for freeing an already-detached inpcb, as well
890 * as removing it from any global inpcb lists it might be on.
893 in_pcbfree(struct inpcb *inp)
895 struct inpcbinfo *ipi = inp->inp_pcbinfo;
897 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
899 INP_INFO_WLOCK_ASSERT(ipi);
900 INP_WLOCK_ASSERT(inp);
903 if (inp->inp_sp != NULL)
904 ipsec_delete_pcbpolicy(inp);
906 inp->inp_gencnt = ++ipi->ipi_gencnt;
909 if (inp->inp_vflag & INP_IPV6PROTO) {
910 ip6_freepcbopts(inp->in6p_outputopts);
911 ip6_freemoptions(inp->in6p_moptions);
914 if (inp->inp_options)
915 (void)m_free(inp->inp_options);
916 if (inp->inp_moptions != NULL)
917 inp_freemoptions(inp->inp_moptions);
919 crfree(inp->inp_cred);
922 mac_destroy_inpcb(inp);
925 uma_zfree(ipi->ipi_zone, inp);
929 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
930 * port reservation, and preventing it from being returned by inpcb lookups.
932 * It is used by TCP to mark an inpcb as unused and avoid future packet
933 * delivery or event notification when a socket remains open but TCP has
934 * closed. This might occur as a result of a shutdown()-initiated TCP close
935 * or a RST on the wire, and allows the port binding to be reused while still
936 * maintaining the invariant that so_pcb always points to a valid inpcb until
939 * XXXRW: An inp_lport of 0 is used to indicate that the inpcb is not on hash
940 * lists, but can lead to confusing netstat output, as open sockets with
941 * closed TCP connections will no longer appear to have their bound port
942 * number. An explicit flag would be better, as it would allow us to leave
943 * the port number intact after the connection is dropped.
945 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
946 * in_pcbnotifyall() and in_pcbpurgeif0()?
949 in_pcbdrop(struct inpcb *inp)
952 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
953 INP_WLOCK_ASSERT(inp);
955 inp->inp_flags |= INP_DROPPED;
956 if (inp->inp_flags & INP_INHASHLIST) {
957 struct inpcbport *phd = inp->inp_phd;
959 LIST_REMOVE(inp, inp_hash);
960 LIST_REMOVE(inp, inp_portlist);
961 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
962 LIST_REMOVE(phd, phd_hash);
965 inp->inp_flags &= ~INP_INHASHLIST;
970 * Common routines to return the socket addresses associated with inpcbs.
973 in_sockaddr(in_port_t port, struct in_addr *addr_p)
975 struct sockaddr_in *sin;
977 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
979 sin->sin_family = AF_INET;
980 sin->sin_len = sizeof(*sin);
981 sin->sin_addr = *addr_p;
982 sin->sin_port = port;
984 return (struct sockaddr *)sin;
988 in_getsockaddr(struct socket *so, struct sockaddr **nam)
995 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
998 port = inp->inp_lport;
999 addr = inp->inp_laddr;
1002 *nam = in_sockaddr(port, &addr);
1007 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1010 struct in_addr addr;
1013 inp = sotoinpcb(so);
1014 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1017 port = inp->inp_fport;
1018 addr = inp->inp_faddr;
1021 *nam = in_sockaddr(port, &addr);
1026 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1027 struct inpcb *(*notify)(struct inpcb *, int))
1029 struct inpcb *inp, *inp_temp;
1031 INP_INFO_WLOCK(pcbinfo);
1032 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1035 if ((inp->inp_vflag & INP_IPV4) == 0) {
1040 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1041 inp->inp_socket == NULL) {
1045 if ((*notify)(inp, errno))
1048 INP_INFO_WUNLOCK(pcbinfo);
1052 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1055 struct ip_moptions *imo;
1058 INP_INFO_RLOCK(pcbinfo);
1059 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1061 imo = inp->inp_moptions;
1062 if ((inp->inp_vflag & INP_IPV4) &&
1065 * Unselect the outgoing interface if it is being
1068 if (imo->imo_multicast_ifp == ifp)
1069 imo->imo_multicast_ifp = NULL;
1072 * Drop multicast group membership if we joined
1073 * through the interface being detached.
1075 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1077 if (imo->imo_membership[i]->inm_ifp == ifp) {
1078 in_delmulti(imo->imo_membership[i]);
1080 } else if (gap != 0)
1081 imo->imo_membership[i - gap] =
1082 imo->imo_membership[i];
1084 imo->imo_num_memberships -= gap;
1088 INP_INFO_RUNLOCK(pcbinfo);
1092 * Lookup a PCB based on the local address and port.
1094 #define INP_LOOKUP_MAPPED_PCB_COST 3
1096 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1097 u_short lport, int wild_okay, struct ucred *cred)
1101 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1107 INP_INFO_LOCK_ASSERT(pcbinfo);
1110 struct inpcbhead *head;
1112 * Look for an unconnected (wildcard foreign addr) PCB that
1113 * matches the local address and port we're looking for.
1115 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1116 0, pcbinfo->ipi_hashmask)];
1117 LIST_FOREACH(inp, head, inp_hash) {
1119 /* XXX inp locking */
1120 if ((inp->inp_vflag & INP_IPV4) == 0)
1123 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1124 inp->inp_laddr.s_addr == laddr.s_addr &&
1125 inp->inp_lport == lport) {
1130 inp->inp_cred->cr_prison == cred->cr_prison)
1139 struct inpcbporthead *porthash;
1140 struct inpcbport *phd;
1141 struct inpcb *match = NULL;
1143 * Best fit PCB lookup.
1145 * First see if this local port is in use by looking on the
1148 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1149 pcbinfo->ipi_porthashmask)];
1150 LIST_FOREACH(phd, porthash, phd_hash) {
1151 if (phd->phd_port == lport)
1156 * Port is in use by one or more PCBs. Look for best
1159 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1162 inp->inp_cred->cr_prison != cred->cr_prison)
1165 /* XXX inp locking */
1166 if ((inp->inp_vflag & INP_IPV4) == 0)
1169 * We never select the PCB that has
1170 * INP_IPV6 flag and is bound to :: if
1171 * we have another PCB which is bound
1172 * to 0.0.0.0. If a PCB has the
1173 * INP_IPV6 flag, then we set its cost
1174 * higher than IPv4 only PCBs.
1176 * Note that the case only happens
1177 * when a socket is bound to ::, under
1178 * the condition that the use of the
1179 * mapped address is allowed.
1181 if ((inp->inp_vflag & INP_IPV6) != 0)
1182 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1184 if (inp->inp_faddr.s_addr != INADDR_ANY)
1186 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1187 if (laddr.s_addr == INADDR_ANY)
1189 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1192 if (laddr.s_addr != INADDR_ANY)
1195 if (wildcard < matchwild) {
1197 matchwild = wildcard;
1206 #undef INP_LOOKUP_MAPPED_PCB_COST
1209 * Lookup PCB in hash list.
1212 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1213 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1216 struct inpcbhead *head;
1217 struct inpcb *inp, *tmpinp;
1218 u_short fport = fport_arg, lport = lport_arg;
1220 INP_INFO_LOCK_ASSERT(pcbinfo);
1223 * First look for an exact match.
1226 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1227 pcbinfo->ipi_hashmask)];
1228 LIST_FOREACH(inp, head, inp_hash) {
1230 /* XXX inp locking */
1231 if ((inp->inp_vflag & INP_IPV4) == 0)
1234 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1235 inp->inp_laddr.s_addr == laddr.s_addr &&
1236 inp->inp_fport == fport &&
1237 inp->inp_lport == lport) {
1239 * XXX We should be able to directly return
1240 * the inp here, without any checks.
1241 * Well unless both bound with SO_REUSEPORT?
1243 if (jailed(inp->inp_cred))
1253 * Then look for a wildcard match, if requested.
1255 if (wildcard == INPLOOKUP_WILDCARD) {
1256 struct inpcb *local_wild = NULL, *local_exact = NULL;
1258 struct inpcb *local_wild_mapped = NULL;
1260 struct inpcb *jail_wild = NULL;
1264 * Order of socket selection - we always prefer jails.
1265 * 1. jailed, non-wild.
1267 * 3. non-jailed, non-wild.
1268 * 4. non-jailed, wild.
1271 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1272 0, pcbinfo->ipi_hashmask)];
1273 LIST_FOREACH(inp, head, inp_hash) {
1275 /* XXX inp locking */
1276 if ((inp->inp_vflag & INP_IPV4) == 0)
1279 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1280 inp->inp_lport != lport)
1283 /* XXX inp locking */
1284 if (ifp && ifp->if_type == IFT_FAITH &&
1285 (inp->inp_flags & INP_FAITH) == 0)
1288 injail = jailed(inp->inp_cred);
1290 if (prison_check_ip4(inp->inp_cred,
1294 if (local_exact != NULL)
1298 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1303 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1305 /* XXX inp locking, NULL check */
1306 if (inp->inp_vflag & INP_IPV6PROTO)
1307 local_wild_mapped = inp;
1315 } /* LIST_FOREACH */
1316 if (jail_wild != NULL)
1318 if (local_exact != NULL)
1319 return (local_exact);
1320 if (local_wild != NULL)
1321 return (local_wild);
1323 if (local_wild_mapped != NULL)
1324 return (local_wild_mapped);
1325 #endif /* defined(INET6) */
1326 } /* if (wildcard == INPLOOKUP_WILDCARD) */
1332 * Insert PCB onto various hash lists.
1335 in_pcbinshash(struct inpcb *inp)
1337 struct inpcbhead *pcbhash;
1338 struct inpcbporthead *pcbporthash;
1339 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1340 struct inpcbport *phd;
1341 u_int32_t hashkey_faddr;
1343 INP_INFO_WLOCK_ASSERT(pcbinfo);
1344 INP_WLOCK_ASSERT(inp);
1345 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1346 ("in_pcbinshash: INP_INHASHLIST"));
1349 if (inp->inp_vflag & INP_IPV6)
1350 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1353 hashkey_faddr = inp->inp_faddr.s_addr;
1355 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1356 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1358 pcbporthash = &pcbinfo->ipi_porthashbase[
1359 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1362 * Go through port list and look for a head for this lport.
1364 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1365 if (phd->phd_port == inp->inp_lport)
1369 * If none exists, malloc one and tack it on.
1372 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1374 return (ENOBUFS); /* XXX */
1376 phd->phd_port = inp->inp_lport;
1377 LIST_INIT(&phd->phd_pcblist);
1378 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1381 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1382 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1383 inp->inp_flags |= INP_INHASHLIST;
1388 * Move PCB to the proper hash bucket when { faddr, fport } have been
1389 * changed. NOTE: This does not handle the case of the lport changing (the
1390 * hashed port list would have to be updated as well), so the lport must
1391 * not change after in_pcbinshash() has been called.
1394 in_pcbrehash(struct inpcb *inp)
1396 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1397 struct inpcbhead *head;
1398 u_int32_t hashkey_faddr;
1400 INP_INFO_WLOCK_ASSERT(pcbinfo);
1401 INP_WLOCK_ASSERT(inp);
1402 KASSERT(inp->inp_flags & INP_INHASHLIST,
1403 ("in_pcbrehash: !INP_INHASHLIST"));
1406 if (inp->inp_vflag & INP_IPV6)
1407 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1410 hashkey_faddr = inp->inp_faddr.s_addr;
1412 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1413 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1415 LIST_REMOVE(inp, inp_hash);
1416 LIST_INSERT_HEAD(head, inp, inp_hash);
1420 * Remove PCB from various lists.
1423 in_pcbremlists(struct inpcb *inp)
1425 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1427 INP_INFO_WLOCK_ASSERT(pcbinfo);
1428 INP_WLOCK_ASSERT(inp);
1430 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1431 if (inp->inp_flags & INP_INHASHLIST) {
1432 struct inpcbport *phd = inp->inp_phd;
1434 LIST_REMOVE(inp, inp_hash);
1435 LIST_REMOVE(inp, inp_portlist);
1436 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1437 LIST_REMOVE(phd, phd_hash);
1440 inp->inp_flags &= ~INP_INHASHLIST;
1442 LIST_REMOVE(inp, inp_list);
1443 pcbinfo->ipi_count--;
1447 * A set label operation has occurred at the socket layer, propagate the
1448 * label change into the in_pcb for the socket.
1451 in_pcbsosetlabel(struct socket *so)
1456 inp = sotoinpcb(so);
1457 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1461 mac_inpcb_sosetlabel(so, inp);
1468 * ipport_tick runs once per second, determining if random port allocation
1469 * should be continued. If more than ipport_randomcps ports have been
1470 * allocated in the last second, then we return to sequential port
1471 * allocation. We return to random allocation only once we drop below
1472 * ipport_randomcps for at least ipport_randomtime seconds.
1475 ipport_tick(void *xtp)
1478 if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) {
1479 if (ipport_stoprandom > 0)
1480 ipport_stoprandom--;
1482 ipport_stoprandom = ipport_randomtime;
1483 ipport_tcplastcount = ipport_tcpallocs;
1484 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1488 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
1492 INP_INFO_RLOCK(&tcbinfo);
1493 LIST_FOREACH(inp, tcbinfo.ipi_listhead, inp_list) {
1498 INP_INFO_RUNLOCK(&tcbinfo);
1502 inp_inpcbtosocket(struct inpcb *inp)
1505 INP_WLOCK_ASSERT(inp);
1506 return (inp->inp_socket);
1510 inp_inpcbtotcpcb(struct inpcb *inp)
1513 INP_WLOCK_ASSERT(inp);
1514 return ((struct tcpcb *)inp->inp_ppcb);
1518 inp_ip_tos_get(const struct inpcb *inp)
1521 return (inp->inp_ip_tos);
1525 inp_ip_tos_set(struct inpcb *inp, int val)
1528 inp->inp_ip_tos = val;
1532 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
1533 uint32_t *faddr, uint16_t *fp)
1536 INP_LOCK_ASSERT(inp);
1537 *laddr = inp->inp_laddr.s_addr;
1538 *faddr = inp->inp_faddr.s_addr;
1539 *lp = inp->inp_lport;
1540 *fp = inp->inp_fport;
1544 so_sotoinpcb(struct socket *so)
1547 return (sotoinpcb(so));
1551 so_sototcpcb(struct socket *so)
1554 return (sototcpcb(so));
1558 inp_wlock(struct inpcb *inp)
1565 inp_wunlock(struct inpcb *inp)
1572 inp_rlock(struct inpcb *inp)
1579 inp_runlock(struct inpcb *inp)
1587 inp_wlock_assert(struct inpcb *inp)
1590 INP_WLOCK_ASSERT(inp);
1594 inp_rlock_assert(struct inpcb *inp)
1597 INP_RLOCK_ASSERT(inp);
1601 inp_lock_assert(struct inpcb *inp)
1604 INP_LOCK_ASSERT(inp);
1608 inp_unlock_assert(struct inpcb *inp)
1611 INP_UNLOCK_ASSERT(inp);
1618 db_print_indent(int indent)
1622 for (i = 0; i < indent; i++)
1627 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1629 char faddr_str[48], laddr_str[48];
1631 db_print_indent(indent);
1632 db_printf("%s at %p\n", name, inc);
1637 if (inc->inc_flags & INC_ISIPV6) {
1639 ip6_sprintf(laddr_str, &inc->inc6_laddr);
1640 ip6_sprintf(faddr_str, &inc->inc6_faddr);
1644 inet_ntoa_r(inc->inc_laddr, laddr_str);
1645 inet_ntoa_r(inc->inc_faddr, faddr_str);
1649 db_print_indent(indent);
1650 db_printf("inc_laddr %s inc_lport %u\n", laddr_str,
1651 ntohs(inc->inc_lport));
1652 db_print_indent(indent);
1653 db_printf("inc_faddr %s inc_fport %u\n", faddr_str,
1654 ntohs(inc->inc_fport));
1658 db_print_inpflags(int inp_flags)
1663 if (inp_flags & INP_RECVOPTS) {
1664 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1667 if (inp_flags & INP_RECVRETOPTS) {
1668 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1671 if (inp_flags & INP_RECVDSTADDR) {
1672 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1675 if (inp_flags & INP_HDRINCL) {
1676 db_printf("%sINP_HDRINCL", comma ? ", " : "");
1679 if (inp_flags & INP_HIGHPORT) {
1680 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1683 if (inp_flags & INP_LOWPORT) {
1684 db_printf("%sINP_LOWPORT", comma ? ", " : "");
1687 if (inp_flags & INP_ANONPORT) {
1688 db_printf("%sINP_ANONPORT", comma ? ", " : "");
1691 if (inp_flags & INP_RECVIF) {
1692 db_printf("%sINP_RECVIF", comma ? ", " : "");
1695 if (inp_flags & INP_MTUDISC) {
1696 db_printf("%sINP_MTUDISC", comma ? ", " : "");
1699 if (inp_flags & INP_FAITH) {
1700 db_printf("%sINP_FAITH", comma ? ", " : "");
1703 if (inp_flags & INP_RECVTTL) {
1704 db_printf("%sINP_RECVTTL", comma ? ", " : "");
1707 if (inp_flags & INP_DONTFRAG) {
1708 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1711 if (inp_flags & IN6P_IPV6_V6ONLY) {
1712 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1715 if (inp_flags & IN6P_PKTINFO) {
1716 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1719 if (inp_flags & IN6P_HOPLIMIT) {
1720 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1723 if (inp_flags & IN6P_HOPOPTS) {
1724 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1727 if (inp_flags & IN6P_DSTOPTS) {
1728 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1731 if (inp_flags & IN6P_RTHDR) {
1732 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
1735 if (inp_flags & IN6P_RTHDRDSTOPTS) {
1736 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
1739 if (inp_flags & IN6P_TCLASS) {
1740 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
1743 if (inp_flags & IN6P_AUTOFLOWLABEL) {
1744 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
1747 if (inp_flags & INP_TIMEWAIT) {
1748 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
1751 if (inp_flags & INP_ONESBCAST) {
1752 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
1755 if (inp_flags & INP_DROPPED) {
1756 db_printf("%sINP_DROPPED", comma ? ", " : "");
1759 if (inp_flags & INP_SOCKREF) {
1760 db_printf("%sINP_SOCKREF", comma ? ", " : "");
1763 if (inp_flags & IN6P_RFC2292) {
1764 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
1767 if (inp_flags & IN6P_MTU) {
1768 db_printf("IN6P_MTU%s", comma ? ", " : "");
1774 db_print_inpvflag(u_char inp_vflag)
1779 if (inp_vflag & INP_IPV4) {
1780 db_printf("%sINP_IPV4", comma ? ", " : "");
1783 if (inp_vflag & INP_IPV6) {
1784 db_printf("%sINP_IPV6", comma ? ", " : "");
1787 if (inp_vflag & INP_IPV6PROTO) {
1788 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
1794 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
1797 db_print_indent(indent);
1798 db_printf("%s at %p\n", name, inp);
1802 db_print_indent(indent);
1803 db_printf("inp_flow: 0x%x\n", inp->inp_flow);
1805 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
1807 db_print_indent(indent);
1808 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n",
1809 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
1811 db_print_indent(indent);
1812 db_printf("inp_label: %p inp_flags: 0x%x (",
1813 inp->inp_label, inp->inp_flags);
1814 db_print_inpflags(inp->inp_flags);
1817 db_print_indent(indent);
1818 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp,
1820 db_print_inpvflag(inp->inp_vflag);
1823 db_print_indent(indent);
1824 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n",
1825 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
1827 db_print_indent(indent);
1829 if (inp->inp_vflag & INP_IPV6) {
1830 db_printf("in6p_options: %p in6p_outputopts: %p "
1831 "in6p_moptions: %p\n", inp->in6p_options,
1832 inp->in6p_outputopts, inp->in6p_moptions);
1833 db_printf("in6p_icmp6filt: %p in6p_cksum %d "
1834 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
1839 db_printf("inp_ip_tos: %d inp_ip_options: %p "
1840 "inp_ip_moptions: %p\n", inp->inp_ip_tos,
1841 inp->inp_options, inp->inp_moptions);
1844 db_print_indent(indent);
1845 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd,
1846 (uintmax_t)inp->inp_gencnt);
1849 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
1854 db_printf("usage: show inpcb <addr>\n");
1857 inp = (struct inpcb *)addr;
1859 db_print_inpcb(inp, "inpcb", 0);