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"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/malloc.h>
50 #include <sys/callout.h>
51 #include <sys/domain.h>
52 #include <sys/protosw.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
57 #include <sys/refcount.h>
59 #include <sys/kernel.h>
60 #include <sys/sysctl.h>
69 #include <net/if_types.h>
70 #include <net/route.h>
73 #if defined(INET) || defined(INET6)
74 #include <netinet/in.h>
75 #include <netinet/in_pcb.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/tcp_var.h>
78 #include <netinet/udp.h>
79 #include <netinet/udp_var.h>
82 #include <netinet/in_var.h>
85 #include <netinet/ip6.h>
86 #include <netinet6/in6_pcb.h>
87 #include <netinet6/in6_var.h>
88 #include <netinet6/ip6_var.h>
93 #include <netipsec/ipsec.h>
94 #include <netipsec/key.h>
97 #include <security/mac/mac_framework.h>
99 static struct callout ipport_tick_callout;
102 * These configure the range of local port addresses assigned to
103 * "unspecified" outgoing connections/packets/whatever.
105 VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */
106 VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */
107 VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */
108 VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */
109 VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */
110 VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */
113 * Reserved ports accessible only to root. There are significant
114 * security considerations that must be accounted for when changing these,
115 * but the security benefits can be great. Please be careful.
117 VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */
118 VNET_DEFINE(int, ipport_reservedlow);
120 /* Variables dealing with random ephemeral port allocation. */
121 VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */
122 VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */
123 VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */
124 VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */
125 VNET_DEFINE(int, ipport_tcpallocs);
126 static VNET_DEFINE(int, ipport_tcplastcount);
128 #define V_ipport_tcplastcount VNET(ipport_tcplastcount)
130 static void in_pcbremlists(struct inpcb *inp);
133 #define RANGECHK(var, min, max) \
134 if ((var) < (min)) { (var) = (min); } \
135 else if ((var) > (max)) { (var) = (max); }
138 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
143 error = vnet_sysctl_handle_int(oidp, arg1, arg2, req);
145 error = sysctl_handle_int(oidp, arg1, arg2, req);
148 RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
149 RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
150 RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
151 RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
152 RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
153 RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
160 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
162 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
163 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0,
164 &sysctl_net_ipport_check, "I", "");
165 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
166 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0,
167 &sysctl_net_ipport_check, "I", "");
168 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first,
169 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0,
170 &sysctl_net_ipport_check, "I", "");
171 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last,
172 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0,
173 &sysctl_net_ipport_check, "I", "");
174 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
175 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0,
176 &sysctl_net_ipport_check, "I", "");
177 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
178 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0,
179 &sysctl_net_ipport_check, "I", "");
180 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
181 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, "");
182 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
183 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
184 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
185 &VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
186 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
187 &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
188 "allocations before switching to a sequental one");
189 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
190 &VNET_NAME(ipport_randomtime), 0,
191 "Minimum time to keep sequental port "
192 "allocation before switching to a random one");
196 * in_pcb.c: manage the Protocol Control Blocks.
198 * NOTE: It is assumed that most of these functions will be called with
199 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
200 * functions often modify hash chains or addresses in pcbs.
204 * Initialize an inpcbinfo -- we should be able to reduce the number of
208 in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
209 struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
210 char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
211 uint32_t inpcbzone_flags)
214 INP_INFO_LOCK_INIT(pcbinfo, name);
216 pcbinfo->ipi_vnet = curvnet;
218 pcbinfo->ipi_listhead = listhead;
219 LIST_INIT(pcbinfo->ipi_listhead);
220 pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
221 &pcbinfo->ipi_hashmask);
222 pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
223 &pcbinfo->ipi_porthashmask);
224 pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
225 NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
227 uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
231 * Destroy an inpcbinfo.
234 in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
237 hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
238 hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
239 pcbinfo->ipi_porthashmask);
240 uma_zdestroy(pcbinfo->ipi_zone);
241 INP_INFO_LOCK_DESTROY(pcbinfo);
245 * Allocate a PCB and associate it with the socket.
246 * On success return with the PCB locked.
249 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
254 INP_INFO_WLOCK_ASSERT(pcbinfo);
256 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
259 bzero(inp, inp_zero_size);
260 inp->inp_pcbinfo = pcbinfo;
261 inp->inp_socket = so;
262 inp->inp_cred = crhold(so->so_cred);
263 inp->inp_inc.inc_fibnum = so->so_fibnum;
265 error = mac_inpcb_init(inp, M_NOWAIT);
268 mac_inpcb_create(so, inp);
271 error = ipsec_init_policy(so, &inp->inp_sp);
274 mac_inpcb_destroy(inp);
280 if (INP_SOCKAF(so) == AF_INET6) {
281 inp->inp_vflag |= INP_IPV6PROTO;
283 inp->inp_flags |= IN6P_IPV6_V6ONLY;
286 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
287 pcbinfo->ipi_count++;
288 so->so_pcb = (caddr_t)inp;
290 if (V_ip6_auto_flowlabel)
291 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
294 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
295 refcount_init(&inp->inp_refcount, 1); /* Reference from inpcbinfo */
296 #if defined(IPSEC) || defined(MAC)
299 crfree(inp->inp_cred);
300 uma_zfree(pcbinfo->ipi_zone, inp);
308 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
312 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
313 INP_WLOCK_ASSERT(inp);
315 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
317 anonport = inp->inp_lport == 0 && (nam == NULL ||
318 ((struct sockaddr_in *)nam)->sin_port == 0);
319 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
320 &inp->inp_lport, cred);
323 if (in_pcbinshash(inp) != 0) {
324 inp->inp_laddr.s_addr = INADDR_ANY;
329 inp->inp_flags |= INP_ANONPORT;
334 #if defined(INET) || defined(INET6)
336 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
337 struct ucred *cred, int lookupflags)
339 struct inpcbinfo *pcbinfo;
340 struct inpcb *tmpinp;
341 unsigned short *lastport;
342 int count, dorandom, error;
343 u_short aux, first, last, lport;
345 struct in_addr laddr;
348 pcbinfo = inp->inp_pcbinfo;
351 * Because no actual state changes occur here, a global write lock on
352 * the pcbinfo isn't required.
354 INP_INFO_LOCK_ASSERT(pcbinfo);
355 INP_LOCK_ASSERT(inp);
357 if (inp->inp_flags & INP_HIGHPORT) {
358 first = V_ipport_hifirstauto; /* sysctl */
359 last = V_ipport_hilastauto;
360 lastport = &pcbinfo->ipi_lasthi;
361 } else if (inp->inp_flags & INP_LOWPORT) {
362 error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
365 first = V_ipport_lowfirstauto; /* 1023 */
366 last = V_ipport_lowlastauto; /* 600 */
367 lastport = &pcbinfo->ipi_lastlow;
369 first = V_ipport_firstauto; /* sysctl */
370 last = V_ipport_lastauto;
371 lastport = &pcbinfo->ipi_lastport;
374 * For UDP, use random port allocation as long as the user
375 * allows it. For TCP (and as of yet unknown) connections,
376 * use random port allocation only if the user allows it AND
377 * ipport_tick() allows it.
379 if (V_ipport_randomized &&
380 (!V_ipport_stoprandom || pcbinfo == &V_udbinfo))
385 * It makes no sense to do random port allocation if
386 * we have the only port available.
390 /* Make sure to not include UDP packets in the count. */
391 if (pcbinfo != &V_udbinfo)
392 V_ipport_tcpallocs++;
394 * Instead of having two loops further down counting up or down
395 * make sure that first is always <= last and go with only one
396 * code path implementing all logic.
405 /* Make the compiler happy. */
407 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
408 KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
413 tmpinp = NULL; /* Make compiler happy. */
417 *lastport = first + (arc4random() % (last - first));
419 count = last - first;
422 if (count-- < 0) /* completely used? */
423 return (EADDRNOTAVAIL);
425 if (*lastport < first || *lastport > last)
427 lport = htons(*lastport);
430 if ((inp->inp_vflag & INP_IPV6) != 0)
431 tmpinp = in6_pcblookup_local(pcbinfo,
432 &inp->in6p_laddr, lport, lookupflags, cred);
434 #if defined(INET) && defined(INET6)
438 tmpinp = in_pcblookup_local(pcbinfo, laddr,
439 lport, lookupflags, cred);
441 } while (tmpinp != NULL);
444 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
445 laddrp->s_addr = laddr.s_addr;
451 #endif /* INET || INET6 */
455 * Set up a bind operation on a PCB, performing port allocation
456 * as required, but do not actually modify the PCB. Callers can
457 * either complete the bind by setting inp_laddr/inp_lport and
458 * calling in_pcbinshash(), or they can just use the resulting
459 * port and address to authorise the sending of a once-off packet.
461 * On error, the values of *laddrp and *lportp are not changed.
464 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
465 u_short *lportp, struct ucred *cred)
467 struct socket *so = inp->inp_socket;
468 struct sockaddr_in *sin;
469 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
470 struct in_addr laddr;
472 int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
476 * Because no actual state changes occur here, a global write lock on
477 * the pcbinfo isn't required.
479 INP_INFO_LOCK_ASSERT(pcbinfo);
480 INP_LOCK_ASSERT(inp);
482 if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
483 return (EADDRNOTAVAIL);
484 laddr.s_addr = *laddrp;
485 if (nam != NULL && laddr.s_addr != INADDR_ANY)
487 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
488 lookupflags = INPLOOKUP_WILDCARD;
490 if ((error = prison_local_ip4(cred, &laddr)) != 0)
493 sin = (struct sockaddr_in *)nam;
494 if (nam->sa_len != sizeof (*sin))
498 * We should check the family, but old programs
499 * incorrectly fail to initialize it.
501 if (sin->sin_family != AF_INET)
502 return (EAFNOSUPPORT);
504 error = prison_local_ip4(cred, &sin->sin_addr);
507 if (sin->sin_port != *lportp) {
508 /* Don't allow the port to change. */
511 lport = sin->sin_port;
513 /* NB: lport is left as 0 if the port isn't being changed. */
514 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
516 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
517 * allow complete duplication of binding if
518 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
519 * and a multicast address is bound on both
520 * new and duplicated sockets.
522 if (so->so_options & SO_REUSEADDR)
523 reuseport = SO_REUSEADDR|SO_REUSEPORT;
524 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
525 sin->sin_port = 0; /* yech... */
526 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
528 * Is the address a local IP address?
529 * If INP_BINDANY is set, then the socket may be bound
530 * to any endpoint address, local or not.
532 if ((inp->inp_flags & INP_BINDANY) == 0 &&
533 ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
534 return (EADDRNOTAVAIL);
536 laddr = sin->sin_addr;
542 if (ntohs(lport) <= V_ipport_reservedhigh &&
543 ntohs(lport) >= V_ipport_reservedlow &&
544 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
547 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
548 priv_check_cred(inp->inp_cred,
549 PRIV_NETINET_REUSEPORT, 0) != 0) {
550 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
551 lport, INPLOOKUP_WILDCARD, cred);
554 * This entire block sorely needs a rewrite.
557 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
558 (so->so_type != SOCK_STREAM ||
559 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
560 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
561 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
562 (t->inp_socket->so_options &
563 SO_REUSEPORT) == 0) &&
564 (inp->inp_cred->cr_uid !=
565 t->inp_cred->cr_uid))
568 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
569 lport, lookupflags, cred);
570 if (t && (t->inp_flags & INP_TIMEWAIT)) {
572 * XXXRW: If an incpb has had its timewait
573 * state recycled, we treat the address as
574 * being in use (for now). This is better
575 * than a panic, but not desirable.
579 (reuseport & tw->tw_so_options) == 0)
582 (reuseport & t->inp_socket->so_options) == 0) {
584 if (ntohl(sin->sin_addr.s_addr) !=
586 ntohl(t->inp_laddr.s_addr) !=
589 INP_SOCKAF(t->inp_socket))
598 error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
603 *laddrp = laddr.s_addr;
609 * Connect from a socket to a specified address.
610 * Both address and port must be specified in argument sin.
611 * If don't have a local address for this socket yet,
615 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
617 u_short lport, fport;
618 in_addr_t laddr, faddr;
621 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
622 INP_WLOCK_ASSERT(inp);
624 lport = inp->inp_lport;
625 laddr = inp->inp_laddr.s_addr;
626 anonport = (lport == 0);
627 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
632 /* Do the initial binding of the local address if required. */
633 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
634 inp->inp_lport = lport;
635 inp->inp_laddr.s_addr = laddr;
636 if (in_pcbinshash(inp) != 0) {
637 inp->inp_laddr.s_addr = INADDR_ANY;
643 /* Commit the remaining changes. */
644 inp->inp_lport = lport;
645 inp->inp_laddr.s_addr = laddr;
646 inp->inp_faddr.s_addr = faddr;
647 inp->inp_fport = fport;
651 inp->inp_flags |= INP_ANONPORT;
656 * Do proper source address selection on an unbound socket in case
657 * of connect. Take jails into account as well.
660 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
665 struct sockaddr_in *sin;
669 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
672 * Bypass source address selection and use the primary jail IP
675 if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
679 bzero(&sro, sizeof(sro));
681 sin = (struct sockaddr_in *)&sro.ro_dst;
682 sin->sin_family = AF_INET;
683 sin->sin_len = sizeof(struct sockaddr_in);
684 sin->sin_addr.s_addr = faddr->s_addr;
687 * If route is known our src addr is taken from the i/f,
690 * Find out route to destination.
692 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
693 in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
696 * If we found a route, use the address corresponding to
697 * the outgoing interface.
699 * Otherwise assume faddr is reachable on a directly connected
700 * network and try to find a corresponding interface to take
701 * the source address from.
703 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
704 struct in_ifaddr *ia;
707 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
709 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0));
715 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
716 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
717 ifa_free(&ia->ia_ifa);
722 ifa_free(&ia->ia_ifa);
725 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
728 if (sa->sa_family != AF_INET)
730 sin = (struct sockaddr_in *)sa;
731 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
732 ia = (struct in_ifaddr *)ifa;
737 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
743 /* 3. As a last resort return the 'default' jail address. */
744 error = prison_get_ip4(cred, laddr);
749 * If the outgoing interface on the route found is not
750 * a loopback interface, use the address from that interface.
751 * In case of jails do those three steps:
752 * 1. check if the interface address belongs to the jail. If so use it.
753 * 2. check if we have any address on the outgoing interface
754 * belonging to this jail. If so use it.
755 * 3. as a last resort return the 'default' jail address.
757 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
758 struct in_ifaddr *ia;
761 /* If not jailed, use the default returned. */
762 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
763 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
764 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
769 /* 1. Check if the iface address belongs to the jail. */
770 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
771 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
772 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
773 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
778 * 2. Check if we have any address on the outgoing interface
779 * belonging to this jail.
782 ifp = sro.ro_rt->rt_ifp;
784 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
786 if (sa->sa_family != AF_INET)
788 sin = (struct sockaddr_in *)sa;
789 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
790 ia = (struct in_ifaddr *)ifa;
795 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
801 /* 3. As a last resort return the 'default' jail address. */
802 error = prison_get_ip4(cred, laddr);
807 * The outgoing interface is marked with 'loopback net', so a route
808 * to ourselves is here.
809 * Try to find the interface of the destination address and then
810 * take the address from there. That interface is not necessarily
811 * a loopback interface.
812 * In case of jails, check that it is an address of the jail
813 * and if we cannot find, fall back to the 'default' jail address.
815 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
816 struct sockaddr_in sain;
817 struct in_ifaddr *ia;
819 bzero(&sain, sizeof(struct sockaddr_in));
820 sain.sin_family = AF_INET;
821 sain.sin_len = sizeof(struct sockaddr_in);
822 sain.sin_addr.s_addr = faddr->s_addr;
824 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
826 ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0));
828 ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
830 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
835 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
836 ifa_free(&ia->ia_ifa);
845 ifa_free(&ia->ia_ifa);
848 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
851 if (sa->sa_family != AF_INET)
853 sin = (struct sockaddr_in *)sa;
854 if (prison_check_ip4(cred,
855 &sin->sin_addr) == 0) {
856 ia = (struct in_ifaddr *)ifa;
861 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
868 /* 3. As a last resort return the 'default' jail address. */
869 error = prison_get_ip4(cred, laddr);
874 if (sro.ro_rt != NULL)
880 * Set up for a connect from a socket to the specified address.
881 * On entry, *laddrp and *lportp should contain the current local
882 * address and port for the PCB; these are updated to the values
883 * that should be placed in inp_laddr and inp_lport to complete
886 * On success, *faddrp and *fportp will be set to the remote address
887 * and port. These are not updated in the error case.
889 * If the operation fails because the connection already exists,
890 * *oinpp will be set to the PCB of that connection so that the
891 * caller can decide to override it. In all other cases, *oinpp
895 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
896 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
897 struct inpcb **oinpp, struct ucred *cred)
899 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
900 struct in_ifaddr *ia;
902 struct in_addr laddr, faddr;
903 u_short lport, fport;
907 * Because a global state change doesn't actually occur here, a read
908 * lock is sufficient.
910 INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo);
911 INP_LOCK_ASSERT(inp);
915 if (nam->sa_len != sizeof (*sin))
917 if (sin->sin_family != AF_INET)
918 return (EAFNOSUPPORT);
919 if (sin->sin_port == 0)
920 return (EADDRNOTAVAIL);
921 laddr.s_addr = *laddrp;
923 faddr = sin->sin_addr;
924 fport = sin->sin_port;
926 if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
928 * If the destination address is INADDR_ANY,
929 * use the primary local address.
930 * If the supplied address is INADDR_BROADCAST,
931 * and the primary interface supports broadcast,
932 * choose the broadcast address for that interface.
934 if (faddr.s_addr == INADDR_ANY) {
937 IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
940 (error = prison_get_ip4(cred, &faddr)) != 0)
942 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
944 if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
946 faddr = satosin(&TAILQ_FIRST(
947 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
951 if (laddr.s_addr == INADDR_ANY) {
952 error = in_pcbladdr(inp, &faddr, &laddr, cred);
954 * If the destination address is multicast and an outgoing
955 * interface has been set as a multicast option, prefer the
956 * address of that interface as our source address.
958 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
959 inp->inp_moptions != NULL) {
960 struct ip_moptions *imo;
963 imo = inp->inp_moptions;
964 if (imo->imo_multicast_ifp != NULL) {
965 ifp = imo->imo_multicast_ifp;
967 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
968 if ((ia->ia_ifp == ifp) &&
970 prison_check_ip4(cred,
971 &ia->ia_addr.sin_addr) == 0))
975 error = EADDRNOTAVAIL;
977 laddr = ia->ia_addr.sin_addr;
986 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
994 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
999 *laddrp = laddr.s_addr;
1001 *faddrp = faddr.s_addr;
1007 in_pcbdisconnect(struct inpcb *inp)
1010 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
1011 INP_WLOCK_ASSERT(inp);
1013 inp->inp_faddr.s_addr = INADDR_ANY;
1020 * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1021 * For most protocols, this will be invoked immediately prior to calling
1022 * in_pcbfree(). However, with TCP the inpcb may significantly outlive the
1023 * socket, in which case in_pcbfree() is deferred.
1026 in_pcbdetach(struct inpcb *inp)
1029 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1031 inp->inp_socket->so_pcb = NULL;
1032 inp->inp_socket = NULL;
1036 * in_pcbref() bumps the reference count on an inpcb in order to maintain
1037 * stability of an inpcb pointer despite the inpcb lock being released. This
1038 * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
1039 * but where the inpcb lock is already held.
1041 * in_pcbref() should be used only to provide brief memory stability, and
1042 * must always be followed by a call to INP_WLOCK() and in_pcbrele() to
1043 * garbage collect the inpcb if it has been in_pcbfree()'d from another
1044 * context. Until in_pcbrele() has returned that the inpcb is still valid,
1045 * lock and rele are the *only* safe operations that may be performed on the
1048 * While the inpcb will not be freed, releasing the inpcb lock means that the
1049 * connection's state may change, so the caller should be careful to
1050 * revalidate any cached state on reacquiring the lock. Drop the reference
1051 * using in_pcbrele().
1054 in_pcbref(struct inpcb *inp)
1057 INP_WLOCK_ASSERT(inp);
1059 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1061 refcount_acquire(&inp->inp_refcount);
1065 * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
1066 * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
1067 * return a flag indicating whether or not the inpcb remains valid. If it is
1068 * valid, we return with the inpcb lock held.
1070 * Notice that, unlike in_pcbref(), the inpcb lock must be held to drop a
1071 * reference on an inpcb. Historically more work was done here (actually, in
1072 * in_pcbfree_internal()) but has been moved to in_pcbfree() to avoid the
1073 * need for the pcbinfo lock in in_pcbrele(). Deferring the free is entirely
1074 * about memory stability (and continued use of the write lock).
1077 in_pcbrele_rlocked(struct inpcb *inp)
1079 struct inpcbinfo *pcbinfo;
1081 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1083 INP_RLOCK_ASSERT(inp);
1085 if (refcount_release(&inp->inp_refcount) == 0)
1088 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1091 pcbinfo = inp->inp_pcbinfo;
1092 uma_zfree(pcbinfo->ipi_zone, inp);
1097 in_pcbrele_wlocked(struct inpcb *inp)
1099 struct inpcbinfo *pcbinfo;
1101 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1103 INP_WLOCK_ASSERT(inp);
1105 if (refcount_release(&inp->inp_refcount) == 0)
1108 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1111 pcbinfo = inp->inp_pcbinfo;
1112 uma_zfree(pcbinfo->ipi_zone, inp);
1117 * Temporary wrapper.
1120 in_pcbrele(struct inpcb *inp)
1123 return (in_pcbrele_wlocked(inp));
1127 * Unconditionally schedule an inpcb to be freed by decrementing its
1128 * reference count, which should occur only after the inpcb has been detached
1129 * from its socket. If another thread holds a temporary reference (acquired
1130 * using in_pcbref()) then the free is deferred until that reference is
1131 * released using in_pcbrele(), but the inpcb is still unlocked. Almost all
1132 * work, including removal from global lists, is done in this context, where
1133 * the pcbinfo lock is held.
1136 in_pcbfree(struct inpcb *inp)
1138 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1140 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1142 INP_INFO_WLOCK_ASSERT(pcbinfo);
1143 INP_WLOCK_ASSERT(inp);
1145 /* XXXRW: Do as much as possible here. */
1147 if (inp->inp_sp != NULL)
1148 ipsec_delete_pcbpolicy(inp);
1150 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1151 in_pcbremlists(inp);
1153 if (inp->inp_vflag & INP_IPV6PROTO) {
1154 ip6_freepcbopts(inp->in6p_outputopts);
1155 if (inp->in6p_moptions != NULL)
1156 ip6_freemoptions(inp->in6p_moptions);
1159 if (inp->inp_options)
1160 (void)m_free(inp->inp_options);
1162 if (inp->inp_moptions != NULL)
1163 inp_freemoptions(inp->inp_moptions);
1166 crfree(inp->inp_cred);
1168 mac_inpcb_destroy(inp);
1170 if (!in_pcbrele_wlocked(inp))
1175 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1176 * port reservation, and preventing it from being returned by inpcb lookups.
1178 * It is used by TCP to mark an inpcb as unused and avoid future packet
1179 * delivery or event notification when a socket remains open but TCP has
1180 * closed. This might occur as a result of a shutdown()-initiated TCP close
1181 * or a RST on the wire, and allows the port binding to be reused while still
1182 * maintaining the invariant that so_pcb always points to a valid inpcb until
1185 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1186 * in_pcbnotifyall() and in_pcbpurgeif0()?
1189 in_pcbdrop(struct inpcb *inp)
1192 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
1193 INP_WLOCK_ASSERT(inp);
1195 inp->inp_flags |= INP_DROPPED;
1196 if (inp->inp_flags & INP_INHASHLIST) {
1197 struct inpcbport *phd = inp->inp_phd;
1199 LIST_REMOVE(inp, inp_hash);
1200 LIST_REMOVE(inp, inp_portlist);
1201 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1202 LIST_REMOVE(phd, phd_hash);
1205 inp->inp_flags &= ~INP_INHASHLIST;
1211 * Common routines to return the socket addresses associated with inpcbs.
1214 in_sockaddr(in_port_t port, struct in_addr *addr_p)
1216 struct sockaddr_in *sin;
1218 sin = malloc(sizeof *sin, M_SONAME,
1220 sin->sin_family = AF_INET;
1221 sin->sin_len = sizeof(*sin);
1222 sin->sin_addr = *addr_p;
1223 sin->sin_port = port;
1225 return (struct sockaddr *)sin;
1229 in_getsockaddr(struct socket *so, struct sockaddr **nam)
1232 struct in_addr addr;
1235 inp = sotoinpcb(so);
1236 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1239 port = inp->inp_lport;
1240 addr = inp->inp_laddr;
1243 *nam = in_sockaddr(port, &addr);
1248 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1251 struct in_addr addr;
1254 inp = sotoinpcb(so);
1255 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1258 port = inp->inp_fport;
1259 addr = inp->inp_faddr;
1262 *nam = in_sockaddr(port, &addr);
1267 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1268 struct inpcb *(*notify)(struct inpcb *, int))
1270 struct inpcb *inp, *inp_temp;
1272 INP_INFO_WLOCK(pcbinfo);
1273 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1276 if ((inp->inp_vflag & INP_IPV4) == 0) {
1281 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1282 inp->inp_socket == NULL) {
1286 if ((*notify)(inp, errno))
1289 INP_INFO_WUNLOCK(pcbinfo);
1293 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1296 struct ip_moptions *imo;
1299 INP_INFO_RLOCK(pcbinfo);
1300 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1302 imo = inp->inp_moptions;
1303 if ((inp->inp_vflag & INP_IPV4) &&
1306 * Unselect the outgoing interface if it is being
1309 if (imo->imo_multicast_ifp == ifp)
1310 imo->imo_multicast_ifp = NULL;
1313 * Drop multicast group membership if we joined
1314 * through the interface being detached.
1316 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1318 if (imo->imo_membership[i]->inm_ifp == ifp) {
1319 in_delmulti(imo->imo_membership[i]);
1321 } else if (gap != 0)
1322 imo->imo_membership[i - gap] =
1323 imo->imo_membership[i];
1325 imo->imo_num_memberships -= gap;
1329 INP_INFO_RUNLOCK(pcbinfo);
1333 * Lookup a PCB based on the local address and port.
1335 #define INP_LOOKUP_MAPPED_PCB_COST 3
1337 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1338 u_short lport, int lookupflags, struct ucred *cred)
1342 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1348 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1349 ("%s: invalid lookup flags %d", __func__, lookupflags));
1351 INP_INFO_LOCK_ASSERT(pcbinfo);
1353 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1354 struct inpcbhead *head;
1356 * Look for an unconnected (wildcard foreign addr) PCB that
1357 * matches the local address and port we're looking for.
1359 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1360 0, pcbinfo->ipi_hashmask)];
1361 LIST_FOREACH(inp, head, inp_hash) {
1363 /* XXX inp locking */
1364 if ((inp->inp_vflag & INP_IPV4) == 0)
1367 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1368 inp->inp_laddr.s_addr == laddr.s_addr &&
1369 inp->inp_lport == lport) {
1374 prison_equal_ip4(cred->cr_prison,
1375 inp->inp_cred->cr_prison))
1384 struct inpcbporthead *porthash;
1385 struct inpcbport *phd;
1386 struct inpcb *match = NULL;
1388 * Best fit PCB lookup.
1390 * First see if this local port is in use by looking on the
1393 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1394 pcbinfo->ipi_porthashmask)];
1395 LIST_FOREACH(phd, porthash, phd_hash) {
1396 if (phd->phd_port == lport)
1401 * Port is in use by one or more PCBs. Look for best
1404 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1407 !prison_equal_ip4(inp->inp_cred->cr_prison,
1411 /* XXX inp locking */
1412 if ((inp->inp_vflag & INP_IPV4) == 0)
1415 * We never select the PCB that has
1416 * INP_IPV6 flag and is bound to :: if
1417 * we have another PCB which is bound
1418 * to 0.0.0.0. If a PCB has the
1419 * INP_IPV6 flag, then we set its cost
1420 * higher than IPv4 only PCBs.
1422 * Note that the case only happens
1423 * when a socket is bound to ::, under
1424 * the condition that the use of the
1425 * mapped address is allowed.
1427 if ((inp->inp_vflag & INP_IPV6) != 0)
1428 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1430 if (inp->inp_faddr.s_addr != INADDR_ANY)
1432 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1433 if (laddr.s_addr == INADDR_ANY)
1435 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1438 if (laddr.s_addr != INADDR_ANY)
1441 if (wildcard < matchwild) {
1443 matchwild = wildcard;
1452 #undef INP_LOOKUP_MAPPED_PCB_COST
1455 * Lookup PCB in hash list.
1458 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1459 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1462 struct inpcbhead *head;
1463 struct inpcb *inp, *tmpinp;
1464 u_short fport = fport_arg, lport = lport_arg;
1466 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1467 ("%s: invalid lookup flags %d", __func__, lookupflags));
1469 INP_INFO_LOCK_ASSERT(pcbinfo);
1472 * First look for an exact match.
1475 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1476 pcbinfo->ipi_hashmask)];
1477 LIST_FOREACH(inp, head, inp_hash) {
1479 /* XXX inp locking */
1480 if ((inp->inp_vflag & INP_IPV4) == 0)
1483 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1484 inp->inp_laddr.s_addr == laddr.s_addr &&
1485 inp->inp_fport == fport &&
1486 inp->inp_lport == lport) {
1488 * XXX We should be able to directly return
1489 * the inp here, without any checks.
1490 * Well unless both bound with SO_REUSEPORT?
1492 if (prison_flag(inp->inp_cred, PR_IP4))
1502 * Then look for a wildcard match, if requested.
1504 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1505 struct inpcb *local_wild = NULL, *local_exact = NULL;
1507 struct inpcb *local_wild_mapped = NULL;
1509 struct inpcb *jail_wild = NULL;
1513 * Order of socket selection - we always prefer jails.
1514 * 1. jailed, non-wild.
1516 * 3. non-jailed, non-wild.
1517 * 4. non-jailed, wild.
1520 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1521 0, pcbinfo->ipi_hashmask)];
1522 LIST_FOREACH(inp, head, inp_hash) {
1524 /* XXX inp locking */
1525 if ((inp->inp_vflag & INP_IPV4) == 0)
1528 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1529 inp->inp_lport != lport)
1532 /* XXX inp locking */
1533 if (ifp && ifp->if_type == IFT_FAITH &&
1534 (inp->inp_flags & INP_FAITH) == 0)
1537 injail = prison_flag(inp->inp_cred, PR_IP4);
1539 if (prison_check_ip4(inp->inp_cred,
1543 if (local_exact != NULL)
1547 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1552 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1554 /* XXX inp locking, NULL check */
1555 if (inp->inp_vflag & INP_IPV6PROTO)
1556 local_wild_mapped = inp;
1564 } /* LIST_FOREACH */
1565 if (jail_wild != NULL)
1567 if (local_exact != NULL)
1568 return (local_exact);
1569 if (local_wild != NULL)
1570 return (local_wild);
1572 if (local_wild_mapped != NULL)
1573 return (local_wild_mapped);
1574 #endif /* defined(INET6) */
1575 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1582 * Insert PCB onto various hash lists.
1585 in_pcbinshash(struct inpcb *inp)
1587 struct inpcbhead *pcbhash;
1588 struct inpcbporthead *pcbporthash;
1589 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1590 struct inpcbport *phd;
1591 u_int32_t hashkey_faddr;
1593 INP_INFO_WLOCK_ASSERT(pcbinfo);
1594 INP_WLOCK_ASSERT(inp);
1595 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1596 ("in_pcbinshash: INP_INHASHLIST"));
1599 if (inp->inp_vflag & INP_IPV6)
1600 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1603 hashkey_faddr = inp->inp_faddr.s_addr;
1605 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1606 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1608 pcbporthash = &pcbinfo->ipi_porthashbase[
1609 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1612 * Go through port list and look for a head for this lport.
1614 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1615 if (phd->phd_port == inp->inp_lport)
1619 * If none exists, malloc one and tack it on.
1622 phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1624 return (ENOBUFS); /* XXX */
1626 phd->phd_port = inp->inp_lport;
1627 LIST_INIT(&phd->phd_pcblist);
1628 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1631 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1632 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1633 inp->inp_flags |= INP_INHASHLIST;
1638 * Move PCB to the proper hash bucket when { faddr, fport } have been
1639 * changed. NOTE: This does not handle the case of the lport changing (the
1640 * hashed port list would have to be updated as well), so the lport must
1641 * not change after in_pcbinshash() has been called.
1644 in_pcbrehash(struct inpcb *inp)
1646 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1647 struct inpcbhead *head;
1648 u_int32_t hashkey_faddr;
1650 INP_INFO_WLOCK_ASSERT(pcbinfo);
1651 INP_WLOCK_ASSERT(inp);
1652 KASSERT(inp->inp_flags & INP_INHASHLIST,
1653 ("in_pcbrehash: !INP_INHASHLIST"));
1656 if (inp->inp_vflag & INP_IPV6)
1657 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1660 hashkey_faddr = inp->inp_faddr.s_addr;
1662 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1663 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1665 LIST_REMOVE(inp, inp_hash);
1666 LIST_INSERT_HEAD(head, inp, inp_hash);
1670 * Remove PCB from various lists.
1673 in_pcbremlists(struct inpcb *inp)
1675 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1677 INP_INFO_WLOCK_ASSERT(pcbinfo);
1678 INP_WLOCK_ASSERT(inp);
1680 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1681 if (inp->inp_flags & INP_INHASHLIST) {
1682 struct inpcbport *phd = inp->inp_phd;
1684 LIST_REMOVE(inp, inp_hash);
1685 LIST_REMOVE(inp, inp_portlist);
1686 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1687 LIST_REMOVE(phd, phd_hash);
1690 inp->inp_flags &= ~INP_INHASHLIST;
1692 LIST_REMOVE(inp, inp_list);
1693 pcbinfo->ipi_count--;
1697 * A set label operation has occurred at the socket layer, propagate the
1698 * label change into the in_pcb for the socket.
1701 in_pcbsosetlabel(struct socket *so)
1706 inp = sotoinpcb(so);
1707 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1711 mac_inpcb_sosetlabel(so, inp);
1718 * ipport_tick runs once per second, determining if random port allocation
1719 * should be continued. If more than ipport_randomcps ports have been
1720 * allocated in the last second, then we return to sequential port
1721 * allocation. We return to random allocation only once we drop below
1722 * ipport_randomcps for at least ipport_randomtime seconds.
1725 ipport_tick(void *xtp)
1727 VNET_ITERATOR_DECL(vnet_iter);
1729 VNET_LIST_RLOCK_NOSLEEP();
1730 VNET_FOREACH(vnet_iter) {
1731 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */
1732 if (V_ipport_tcpallocs <=
1733 V_ipport_tcplastcount + V_ipport_randomcps) {
1734 if (V_ipport_stoprandom > 0)
1735 V_ipport_stoprandom--;
1737 V_ipport_stoprandom = V_ipport_randomtime;
1738 V_ipport_tcplastcount = V_ipport_tcpallocs;
1741 VNET_LIST_RUNLOCK_NOSLEEP();
1742 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1749 callout_stop(&ipport_tick_callout);
1753 * The ipport_callout should start running at about the time we attach the
1754 * inet or inet6 domains.
1757 ipport_tick_init(const void *unused __unused)
1760 /* Start ipport_tick. */
1761 callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
1762 callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
1763 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
1764 SHUTDOWN_PRI_DEFAULT);
1766 SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
1767 ipport_tick_init, NULL);
1770 inp_wlock(struct inpcb *inp)
1777 inp_wunlock(struct inpcb *inp)
1784 inp_rlock(struct inpcb *inp)
1791 inp_runlock(struct inpcb *inp)
1799 inp_lock_assert(struct inpcb *inp)
1802 INP_WLOCK_ASSERT(inp);
1806 inp_unlock_assert(struct inpcb *inp)
1809 INP_UNLOCK_ASSERT(inp);
1814 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
1818 INP_INFO_RLOCK(&V_tcbinfo);
1819 LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
1824 INP_INFO_RUNLOCK(&V_tcbinfo);
1828 inp_inpcbtosocket(struct inpcb *inp)
1831 INP_WLOCK_ASSERT(inp);
1832 return (inp->inp_socket);
1836 inp_inpcbtotcpcb(struct inpcb *inp)
1839 INP_WLOCK_ASSERT(inp);
1840 return ((struct tcpcb *)inp->inp_ppcb);
1844 inp_ip_tos_get(const struct inpcb *inp)
1847 return (inp->inp_ip_tos);
1851 inp_ip_tos_set(struct inpcb *inp, int val)
1854 inp->inp_ip_tos = val;
1858 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
1859 uint32_t *faddr, uint16_t *fp)
1862 INP_LOCK_ASSERT(inp);
1863 *laddr = inp->inp_laddr.s_addr;
1864 *faddr = inp->inp_faddr.s_addr;
1865 *lp = inp->inp_lport;
1866 *fp = inp->inp_fport;
1870 so_sotoinpcb(struct socket *so)
1873 return (sotoinpcb(so));
1877 so_sototcpcb(struct socket *so)
1880 return (sototcpcb(so));
1885 db_print_indent(int indent)
1889 for (i = 0; i < indent; i++)
1894 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1896 char faddr_str[48], laddr_str[48];
1898 db_print_indent(indent);
1899 db_printf("%s at %p\n", name, inc);
1904 if (inc->inc_flags & INC_ISIPV6) {
1906 ip6_sprintf(laddr_str, &inc->inc6_laddr);
1907 ip6_sprintf(faddr_str, &inc->inc6_faddr);
1911 inet_ntoa_r(inc->inc_laddr, laddr_str);
1912 inet_ntoa_r(inc->inc_faddr, faddr_str);
1916 db_print_indent(indent);
1917 db_printf("inc_laddr %s inc_lport %u\n", laddr_str,
1918 ntohs(inc->inc_lport));
1919 db_print_indent(indent);
1920 db_printf("inc_faddr %s inc_fport %u\n", faddr_str,
1921 ntohs(inc->inc_fport));
1925 db_print_inpflags(int inp_flags)
1930 if (inp_flags & INP_RECVOPTS) {
1931 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1934 if (inp_flags & INP_RECVRETOPTS) {
1935 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1938 if (inp_flags & INP_RECVDSTADDR) {
1939 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1942 if (inp_flags & INP_HDRINCL) {
1943 db_printf("%sINP_HDRINCL", comma ? ", " : "");
1946 if (inp_flags & INP_HIGHPORT) {
1947 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1950 if (inp_flags & INP_LOWPORT) {
1951 db_printf("%sINP_LOWPORT", comma ? ", " : "");
1954 if (inp_flags & INP_ANONPORT) {
1955 db_printf("%sINP_ANONPORT", comma ? ", " : "");
1958 if (inp_flags & INP_RECVIF) {
1959 db_printf("%sINP_RECVIF", comma ? ", " : "");
1962 if (inp_flags & INP_MTUDISC) {
1963 db_printf("%sINP_MTUDISC", comma ? ", " : "");
1966 if (inp_flags & INP_FAITH) {
1967 db_printf("%sINP_FAITH", comma ? ", " : "");
1970 if (inp_flags & INP_RECVTTL) {
1971 db_printf("%sINP_RECVTTL", comma ? ", " : "");
1974 if (inp_flags & INP_DONTFRAG) {
1975 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1978 if (inp_flags & IN6P_IPV6_V6ONLY) {
1979 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1982 if (inp_flags & IN6P_PKTINFO) {
1983 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1986 if (inp_flags & IN6P_HOPLIMIT) {
1987 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1990 if (inp_flags & IN6P_HOPOPTS) {
1991 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1994 if (inp_flags & IN6P_DSTOPTS) {
1995 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1998 if (inp_flags & IN6P_RTHDR) {
1999 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
2002 if (inp_flags & IN6P_RTHDRDSTOPTS) {
2003 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
2006 if (inp_flags & IN6P_TCLASS) {
2007 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
2010 if (inp_flags & IN6P_AUTOFLOWLABEL) {
2011 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
2014 if (inp_flags & INP_TIMEWAIT) {
2015 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
2018 if (inp_flags & INP_ONESBCAST) {
2019 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
2022 if (inp_flags & INP_DROPPED) {
2023 db_printf("%sINP_DROPPED", comma ? ", " : "");
2026 if (inp_flags & INP_SOCKREF) {
2027 db_printf("%sINP_SOCKREF", comma ? ", " : "");
2030 if (inp_flags & IN6P_RFC2292) {
2031 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2034 if (inp_flags & IN6P_MTU) {
2035 db_printf("IN6P_MTU%s", comma ? ", " : "");
2041 db_print_inpvflag(u_char inp_vflag)
2046 if (inp_vflag & INP_IPV4) {
2047 db_printf("%sINP_IPV4", comma ? ", " : "");
2050 if (inp_vflag & INP_IPV6) {
2051 db_printf("%sINP_IPV6", comma ? ", " : "");
2054 if (inp_vflag & INP_IPV6PROTO) {
2055 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2061 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2064 db_print_indent(indent);
2065 db_printf("%s at %p\n", name, inp);
2069 db_print_indent(indent);
2070 db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2072 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2074 db_print_indent(indent);
2075 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n",
2076 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2078 db_print_indent(indent);
2079 db_printf("inp_label: %p inp_flags: 0x%x (",
2080 inp->inp_label, inp->inp_flags);
2081 db_print_inpflags(inp->inp_flags);
2084 db_print_indent(indent);
2085 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp,
2087 db_print_inpvflag(inp->inp_vflag);
2090 db_print_indent(indent);
2091 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n",
2092 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2094 db_print_indent(indent);
2096 if (inp->inp_vflag & INP_IPV6) {
2097 db_printf("in6p_options: %p in6p_outputopts: %p "
2098 "in6p_moptions: %p\n", inp->in6p_options,
2099 inp->in6p_outputopts, inp->in6p_moptions);
2100 db_printf("in6p_icmp6filt: %p in6p_cksum %d "
2101 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2106 db_printf("inp_ip_tos: %d inp_ip_options: %p "
2107 "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2108 inp->inp_options, inp->inp_moptions);
2111 db_print_indent(indent);
2112 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd,
2113 (uintmax_t)inp->inp_gencnt);
2116 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2121 db_printf("usage: show inpcb <addr>\n");
2124 inp = (struct inpcb *)addr;
2126 db_print_inpcb(inp, "inpcb", 0);