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"
40 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
46 #include <sys/callout.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
54 #include <sys/kernel.h>
55 #include <sys/sysctl.h>
64 #include <net/if_types.h>
65 #include <net/route.h>
68 #if defined(INET) || defined(INET6)
69 #include <netinet/in.h>
70 #include <netinet/in_pcb.h>
71 #include <netinet/ip_var.h>
72 #include <netinet/tcp_var.h>
73 #include <netinet/udp.h>
74 #include <netinet/udp_var.h>
77 #include <netinet/in_var.h>
80 #include <netinet/ip6.h>
81 #include <netinet6/in6_pcb.h>
82 #include <netinet6/in6_var.h>
83 #include <netinet6/ip6_var.h>
88 #include <netipsec/ipsec.h>
89 #include <netipsec/key.h>
92 #include <security/mac/mac_framework.h>
94 static struct callout ipport_tick_callout;
97 * These configure the range of local port addresses assigned to
98 * "unspecified" outgoing connections/packets/whatever.
100 VNET_DEFINE(int, ipport_lowfirstauto) = IPPORT_RESERVED - 1; /* 1023 */
101 VNET_DEFINE(int, ipport_lowlastauto) = IPPORT_RESERVEDSTART; /* 600 */
102 VNET_DEFINE(int, ipport_firstauto) = IPPORT_EPHEMERALFIRST; /* 10000 */
103 VNET_DEFINE(int, ipport_lastauto) = IPPORT_EPHEMERALLAST; /* 65535 */
104 VNET_DEFINE(int, ipport_hifirstauto) = IPPORT_HIFIRSTAUTO; /* 49152 */
105 VNET_DEFINE(int, ipport_hilastauto) = IPPORT_HILASTAUTO; /* 65535 */
108 * Reserved ports accessible only to root. There are significant
109 * security considerations that must be accounted for when changing these,
110 * but the security benefits can be great. Please be careful.
112 VNET_DEFINE(int, ipport_reservedhigh) = IPPORT_RESERVED - 1; /* 1023 */
113 VNET_DEFINE(int, ipport_reservedlow);
115 /* Variables dealing with random ephemeral port allocation. */
116 VNET_DEFINE(int, ipport_randomized) = 1; /* user controlled via sysctl */
117 VNET_DEFINE(int, ipport_randomcps) = 10; /* user controlled via sysctl */
118 VNET_DEFINE(int, ipport_randomtime) = 45; /* user controlled via sysctl */
119 VNET_DEFINE(int, ipport_stoprandom); /* toggled by ipport_tick */
120 VNET_DEFINE(int, ipport_tcpallocs);
121 static VNET_DEFINE(int, ipport_tcplastcount);
123 #define V_ipport_tcplastcount VNET(ipport_tcplastcount)
125 static void in_pcbremlists(struct inpcb *inp);
128 #define RANGECHK(var, min, max) \
129 if ((var) < (min)) { (var) = (min); } \
130 else if ((var) > (max)) { (var) = (max); }
133 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
138 error = vnet_sysctl_handle_int(oidp, arg1, arg2, req);
140 error = sysctl_handle_int(oidp, arg1, arg2, req);
143 RANGECHK(V_ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
144 RANGECHK(V_ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
145 RANGECHK(V_ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
146 RANGECHK(V_ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
147 RANGECHK(V_ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
148 RANGECHK(V_ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
155 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
157 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst,
158 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowfirstauto), 0,
159 &sysctl_net_ipport_check, "I", "");
160 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast,
161 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lowlastauto), 0,
162 &sysctl_net_ipport_check, "I", "");
163 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, first,
164 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_firstauto), 0,
165 &sysctl_net_ipport_check, "I", "");
166 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, last,
167 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_lastauto), 0,
168 &sysctl_net_ipport_check, "I", "");
169 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst,
170 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hifirstauto), 0,
171 &sysctl_net_ipport_check, "I", "");
172 SYSCTL_VNET_PROC(_net_inet_ip_portrange, OID_AUTO, hilast,
173 CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(ipport_hilastauto), 0,
174 &sysctl_net_ipport_check, "I", "");
175 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
176 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedhigh), 0, "");
177 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
178 CTLFLAG_RW|CTLFLAG_SECURE, &VNET_NAME(ipport_reservedlow), 0, "");
179 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
180 &VNET_NAME(ipport_randomized), 0, "Enable random port allocation");
181 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
182 &VNET_NAME(ipport_randomcps), 0, "Maximum number of random port "
183 "allocations before switching to a sequental one");
184 SYSCTL_VNET_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
185 &VNET_NAME(ipport_randomtime), 0,
186 "Minimum time to keep sequental port "
187 "allocation before switching to a random one");
191 * in_pcb.c: manage the Protocol Control Blocks.
193 * NOTE: It is assumed that most of these functions will be called with
194 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
195 * functions often modify hash chains or addresses in pcbs.
199 * Initialize an inpcbinfo -- we should be able to reduce the number of
203 in_pcbinfo_init(struct inpcbinfo *pcbinfo, const char *name,
204 struct inpcbhead *listhead, int hash_nelements, int porthash_nelements,
205 char *inpcbzone_name, uma_init inpcbzone_init, uma_fini inpcbzone_fini,
206 uint32_t inpcbzone_flags)
209 INP_INFO_LOCK_INIT(pcbinfo, name);
211 pcbinfo->ipi_vnet = curvnet;
213 pcbinfo->ipi_listhead = listhead;
214 LIST_INIT(pcbinfo->ipi_listhead);
215 pcbinfo->ipi_hashbase = hashinit(hash_nelements, M_PCB,
216 &pcbinfo->ipi_hashmask);
217 pcbinfo->ipi_porthashbase = hashinit(porthash_nelements, M_PCB,
218 &pcbinfo->ipi_porthashmask);
219 pcbinfo->ipi_zone = uma_zcreate(inpcbzone_name, sizeof(struct inpcb),
220 NULL, NULL, inpcbzone_init, inpcbzone_fini, UMA_ALIGN_PTR,
222 uma_zone_set_max(pcbinfo->ipi_zone, maxsockets);
226 * Destroy an inpcbinfo.
229 in_pcbinfo_destroy(struct inpcbinfo *pcbinfo)
232 hashdestroy(pcbinfo->ipi_hashbase, M_PCB, pcbinfo->ipi_hashmask);
233 hashdestroy(pcbinfo->ipi_porthashbase, M_PCB,
234 pcbinfo->ipi_porthashmask);
235 uma_zdestroy(pcbinfo->ipi_zone);
236 INP_INFO_LOCK_DESTROY(pcbinfo);
240 * Allocate a PCB and associate it with the socket.
241 * On success return with the PCB locked.
244 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
249 INP_INFO_WLOCK_ASSERT(pcbinfo);
251 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
254 bzero(inp, inp_zero_size);
255 inp->inp_pcbinfo = pcbinfo;
256 inp->inp_socket = so;
257 inp->inp_cred = crhold(so->so_cred);
258 inp->inp_inc.inc_fibnum = so->so_fibnum;
260 error = mac_inpcb_init(inp, M_NOWAIT);
263 mac_inpcb_create(so, inp);
266 error = ipsec_init_policy(so, &inp->inp_sp);
269 mac_inpcb_destroy(inp);
275 if (INP_SOCKAF(so) == AF_INET6) {
276 inp->inp_vflag |= INP_IPV6PROTO;
278 inp->inp_flags |= IN6P_IPV6_V6ONLY;
281 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
282 pcbinfo->ipi_count++;
283 so->so_pcb = (caddr_t)inp;
285 if (V_ip6_auto_flowlabel)
286 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
289 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
290 inp->inp_refcount = 1; /* Reference from the inpcbinfo */
291 #if defined(IPSEC) || defined(MAC)
294 crfree(inp->inp_cred);
295 uma_zfree(pcbinfo->ipi_zone, inp);
303 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
307 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
308 INP_WLOCK_ASSERT(inp);
310 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
312 anonport = inp->inp_lport == 0 && (nam == NULL ||
313 ((struct sockaddr_in *)nam)->sin_port == 0);
314 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
315 &inp->inp_lport, cred);
318 if (in_pcbinshash(inp) != 0) {
319 inp->inp_laddr.s_addr = INADDR_ANY;
324 inp->inp_flags |= INP_ANONPORT;
329 #if defined(INET) || defined(INET6)
331 in_pcb_lport(struct inpcb *inp, struct in_addr *laddrp, u_short *lportp,
332 struct ucred *cred, int lookupflags)
334 struct inpcbinfo *pcbinfo;
335 struct inpcb *tmpinp;
336 unsigned short *lastport;
337 int count, dorandom, error;
338 u_short aux, first, last, lport;
340 struct in_addr laddr;
343 pcbinfo = inp->inp_pcbinfo;
346 * Because no actual state changes occur here, a global write lock on
347 * the pcbinfo isn't required.
349 INP_INFO_LOCK_ASSERT(pcbinfo);
350 INP_LOCK_ASSERT(inp);
352 if (inp->inp_flags & INP_HIGHPORT) {
353 first = V_ipport_hifirstauto; /* sysctl */
354 last = V_ipport_hilastauto;
355 lastport = &pcbinfo->ipi_lasthi;
356 } else if (inp->inp_flags & INP_LOWPORT) {
357 error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0);
360 first = V_ipport_lowfirstauto; /* 1023 */
361 last = V_ipport_lowlastauto; /* 600 */
362 lastport = &pcbinfo->ipi_lastlow;
364 first = V_ipport_firstauto; /* sysctl */
365 last = V_ipport_lastauto;
366 lastport = &pcbinfo->ipi_lastport;
369 * For UDP, use random port allocation as long as the user
370 * allows it. For TCP (and as of yet unknown) connections,
371 * use random port allocation only if the user allows it AND
372 * ipport_tick() allows it.
374 if (V_ipport_randomized &&
375 (!V_ipport_stoprandom || pcbinfo == &V_udbinfo))
380 * It makes no sense to do random port allocation if
381 * we have the only port available.
385 /* Make sure to not include UDP packets in the count. */
386 if (pcbinfo != &V_udbinfo)
387 V_ipport_tcpallocs++;
389 * Instead of having two loops further down counting up or down
390 * make sure that first is always <= last and go with only one
391 * code path implementing all logic.
400 /* Make the compiler happy. */
402 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4) {
403 KASSERT(laddrp != NULL, ("%s: laddrp NULL for v4 inp %p",
408 tmpinp = NULL; /* Make compiler happy. */
412 *lastport = first + (arc4random() % (last - first));
414 count = last - first;
417 if (count-- < 0) /* completely used? */
418 return (EADDRNOTAVAIL);
420 if (*lastport < first || *lastport > last)
422 lport = htons(*lastport);
425 if ((inp->inp_vflag & INP_IPV6) != 0)
426 tmpinp = in6_pcblookup_local(pcbinfo,
427 &inp->in6p_laddr, lport, lookupflags, cred);
429 #if defined(INET) && defined(INET6)
433 tmpinp = in_pcblookup_local(pcbinfo, laddr,
434 lport, lookupflags, cred);
436 } while (tmpinp != NULL);
439 if ((inp->inp_vflag & (INP_IPV4|INP_IPV6)) == INP_IPV4)
440 laddrp->s_addr = laddr.s_addr;
446 #endif /* INET || INET6 */
450 * Set up a bind operation on a PCB, performing port allocation
451 * as required, but do not actually modify the PCB. Callers can
452 * either complete the bind by setting inp_laddr/inp_lport and
453 * calling in_pcbinshash(), or they can just use the resulting
454 * port and address to authorise the sending of a once-off packet.
456 * On error, the values of *laddrp and *lportp are not changed.
459 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
460 u_short *lportp, struct ucred *cred)
462 struct socket *so = inp->inp_socket;
463 struct sockaddr_in *sin;
464 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
465 struct in_addr laddr;
467 int lookupflags = 0, reuseport = (so->so_options & SO_REUSEPORT);
471 * Because no actual state changes occur here, a global write lock on
472 * the pcbinfo isn't required.
474 INP_INFO_LOCK_ASSERT(pcbinfo);
475 INP_LOCK_ASSERT(inp);
477 if (TAILQ_EMPTY(&V_in_ifaddrhead)) /* XXX broken! */
478 return (EADDRNOTAVAIL);
479 laddr.s_addr = *laddrp;
480 if (nam != NULL && laddr.s_addr != INADDR_ANY)
482 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
483 lookupflags = INPLOOKUP_WILDCARD;
485 if ((error = prison_local_ip4(cred, &laddr)) != 0)
488 sin = (struct sockaddr_in *)nam;
489 if (nam->sa_len != sizeof (*sin))
493 * We should check the family, but old programs
494 * incorrectly fail to initialize it.
496 if (sin->sin_family != AF_INET)
497 return (EAFNOSUPPORT);
499 error = prison_local_ip4(cred, &sin->sin_addr);
502 if (sin->sin_port != *lportp) {
503 /* Don't allow the port to change. */
506 lport = sin->sin_port;
508 /* NB: lport is left as 0 if the port isn't being changed. */
509 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
511 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
512 * allow complete duplication of binding if
513 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
514 * and a multicast address is bound on both
515 * new and duplicated sockets.
517 if (so->so_options & SO_REUSEADDR)
518 reuseport = SO_REUSEADDR|SO_REUSEPORT;
519 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
520 sin->sin_port = 0; /* yech... */
521 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
523 * Is the address a local IP address?
524 * If INP_BINDANY is set, then the socket may be bound
525 * to any endpoint address, local or not.
527 if ((inp->inp_flags & INP_BINDANY) == 0 &&
528 ifa_ifwithaddr_check((struct sockaddr *)sin) == 0)
529 return (EADDRNOTAVAIL);
531 laddr = sin->sin_addr;
537 if (ntohs(lport) <= V_ipport_reservedhigh &&
538 ntohs(lport) >= V_ipport_reservedlow &&
539 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
542 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
543 priv_check_cred(inp->inp_cred,
544 PRIV_NETINET_REUSEPORT, 0) != 0) {
545 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
546 lport, INPLOOKUP_WILDCARD, cred);
549 * This entire block sorely needs a rewrite.
552 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
553 (so->so_type != SOCK_STREAM ||
554 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
555 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
556 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
557 (t->inp_socket->so_options &
558 SO_REUSEPORT) == 0) &&
559 (inp->inp_cred->cr_uid !=
560 t->inp_cred->cr_uid))
563 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
564 lport, lookupflags, cred);
565 if (t && (t->inp_flags & INP_TIMEWAIT)) {
567 * XXXRW: If an incpb has had its timewait
568 * state recycled, we treat the address as
569 * being in use (for now). This is better
570 * than a panic, but not desirable.
574 (reuseport & tw->tw_so_options) == 0)
577 (reuseport & t->inp_socket->so_options) == 0) {
579 if (ntohl(sin->sin_addr.s_addr) !=
581 ntohl(t->inp_laddr.s_addr) !=
584 INP_SOCKAF(t->inp_socket))
593 error = in_pcb_lport(inp, &laddr, &lport, cred, lookupflags);
598 *laddrp = laddr.s_addr;
604 * Connect from a socket to a specified address.
605 * Both address and port must be specified in argument sin.
606 * If don't have a local address for this socket yet,
610 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
612 u_short lport, fport;
613 in_addr_t laddr, faddr;
616 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
617 INP_WLOCK_ASSERT(inp);
619 lport = inp->inp_lport;
620 laddr = inp->inp_laddr.s_addr;
621 anonport = (lport == 0);
622 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
627 /* Do the initial binding of the local address if required. */
628 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
629 inp->inp_lport = lport;
630 inp->inp_laddr.s_addr = laddr;
631 if (in_pcbinshash(inp) != 0) {
632 inp->inp_laddr.s_addr = INADDR_ANY;
638 /* Commit the remaining changes. */
639 inp->inp_lport = lport;
640 inp->inp_laddr.s_addr = laddr;
641 inp->inp_faddr.s_addr = faddr;
642 inp->inp_fport = fport;
646 inp->inp_flags |= INP_ANONPORT;
651 * Do proper source address selection on an unbound socket in case
652 * of connect. Take jails into account as well.
655 in_pcbladdr(struct inpcb *inp, struct in_addr *faddr, struct in_addr *laddr,
660 struct sockaddr_in *sin;
664 KASSERT(laddr != NULL, ("%s: laddr NULL", __func__));
667 * Bypass source address selection and use the primary jail IP
670 if (cred != NULL && !prison_saddrsel_ip4(cred, laddr))
674 bzero(&sro, sizeof(sro));
676 sin = (struct sockaddr_in *)&sro.ro_dst;
677 sin->sin_family = AF_INET;
678 sin->sin_len = sizeof(struct sockaddr_in);
679 sin->sin_addr.s_addr = faddr->s_addr;
682 * If route is known our src addr is taken from the i/f,
685 * Find out route to destination.
687 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
688 in_rtalloc_ign(&sro, 0, inp->inp_inc.inc_fibnum);
691 * If we found a route, use the address corresponding to
692 * the outgoing interface.
694 * Otherwise assume faddr is reachable on a directly connected
695 * network and try to find a corresponding interface to take
696 * the source address from.
698 if (sro.ro_rt == NULL || sro.ro_rt->rt_ifp == NULL) {
699 struct in_ifaddr *ia;
702 ia = ifatoia(ifa_ifwithdstaddr((struct sockaddr *)sin));
704 ia = ifatoia(ifa_ifwithnet((struct sockaddr *)sin, 0));
710 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
711 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
712 ifa_free(&ia->ia_ifa);
717 ifa_free(&ia->ia_ifa);
720 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
723 if (sa->sa_family != AF_INET)
725 sin = (struct sockaddr_in *)sa;
726 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
727 ia = (struct in_ifaddr *)ifa;
732 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
738 /* 3. As a last resort return the 'default' jail address. */
739 error = prison_get_ip4(cred, laddr);
744 * If the outgoing interface on the route found is not
745 * a loopback interface, use the address from that interface.
746 * In case of jails do those three steps:
747 * 1. check if the interface address belongs to the jail. If so use it.
748 * 2. check if we have any address on the outgoing interface
749 * belonging to this jail. If so use it.
750 * 3. as a last resort return the 'default' jail address.
752 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
753 struct in_ifaddr *ia;
756 /* If not jailed, use the default returned. */
757 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
758 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
759 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
764 /* 1. Check if the iface address belongs to the jail. */
765 sin = (struct sockaddr_in *)sro.ro_rt->rt_ifa->ifa_addr;
766 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
767 ia = (struct in_ifaddr *)sro.ro_rt->rt_ifa;
768 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
773 * 2. Check if we have any address on the outgoing interface
774 * belonging to this jail.
777 ifp = sro.ro_rt->rt_ifp;
779 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
781 if (sa->sa_family != AF_INET)
783 sin = (struct sockaddr_in *)sa;
784 if (prison_check_ip4(cred, &sin->sin_addr) == 0) {
785 ia = (struct in_ifaddr *)ifa;
790 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
796 /* 3. As a last resort return the 'default' jail address. */
797 error = prison_get_ip4(cred, laddr);
802 * The outgoing interface is marked with 'loopback net', so a route
803 * to ourselves is here.
804 * Try to find the interface of the destination address and then
805 * take the address from there. That interface is not necessarily
806 * a loopback interface.
807 * In case of jails, check that it is an address of the jail
808 * and if we cannot find, fall back to the 'default' jail address.
810 if ((sro.ro_rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) {
811 struct sockaddr_in sain;
812 struct in_ifaddr *ia;
814 bzero(&sain, sizeof(struct sockaddr_in));
815 sain.sin_family = AF_INET;
816 sain.sin_len = sizeof(struct sockaddr_in);
817 sain.sin_addr.s_addr = faddr->s_addr;
819 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sain)));
821 ia = ifatoia(ifa_ifwithnet(sintosa(&sain), 0));
823 ia = ifatoia(ifa_ifwithaddr(sintosa(&sain)));
825 if (cred == NULL || !prison_flag(cred, PR_IP4)) {
830 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
831 ifa_free(&ia->ia_ifa);
840 ifa_free(&ia->ia_ifa);
843 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
846 if (sa->sa_family != AF_INET)
848 sin = (struct sockaddr_in *)sa;
849 if (prison_check_ip4(cred,
850 &sin->sin_addr) == 0) {
851 ia = (struct in_ifaddr *)ifa;
856 laddr->s_addr = ia->ia_addr.sin_addr.s_addr;
863 /* 3. As a last resort return the 'default' jail address. */
864 error = prison_get_ip4(cred, laddr);
869 if (sro.ro_rt != NULL)
875 * Set up for a connect from a socket to the specified address.
876 * On entry, *laddrp and *lportp should contain the current local
877 * address and port for the PCB; these are updated to the values
878 * that should be placed in inp_laddr and inp_lport to complete
881 * On success, *faddrp and *fportp will be set to the remote address
882 * and port. These are not updated in the error case.
884 * If the operation fails because the connection already exists,
885 * *oinpp will be set to the PCB of that connection so that the
886 * caller can decide to override it. In all other cases, *oinpp
890 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
891 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
892 struct inpcb **oinpp, struct ucred *cred)
894 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
895 struct in_ifaddr *ia;
897 struct in_addr laddr, faddr;
898 u_short lport, fport;
902 * Because a global state change doesn't actually occur here, a read
903 * lock is sufficient.
905 INP_INFO_LOCK_ASSERT(inp->inp_pcbinfo);
906 INP_LOCK_ASSERT(inp);
910 if (nam->sa_len != sizeof (*sin))
912 if (sin->sin_family != AF_INET)
913 return (EAFNOSUPPORT);
914 if (sin->sin_port == 0)
915 return (EADDRNOTAVAIL);
916 laddr.s_addr = *laddrp;
918 faddr = sin->sin_addr;
919 fport = sin->sin_port;
921 if (!TAILQ_EMPTY(&V_in_ifaddrhead)) {
923 * If the destination address is INADDR_ANY,
924 * use the primary local address.
925 * If the supplied address is INADDR_BROADCAST,
926 * and the primary interface supports broadcast,
927 * choose the broadcast address for that interface.
929 if (faddr.s_addr == INADDR_ANY) {
932 IA_SIN(TAILQ_FIRST(&V_in_ifaddrhead))->sin_addr;
935 (error = prison_get_ip4(cred, &faddr)) != 0)
937 } else if (faddr.s_addr == (u_long)INADDR_BROADCAST) {
939 if (TAILQ_FIRST(&V_in_ifaddrhead)->ia_ifp->if_flags &
941 faddr = satosin(&TAILQ_FIRST(
942 &V_in_ifaddrhead)->ia_broadaddr)->sin_addr;
946 if (laddr.s_addr == INADDR_ANY) {
947 error = in_pcbladdr(inp, &faddr, &laddr, cred);
949 * If the destination address is multicast and an outgoing
950 * interface has been set as a multicast option, prefer the
951 * address of that interface as our source address.
953 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
954 inp->inp_moptions != NULL) {
955 struct ip_moptions *imo;
958 imo = inp->inp_moptions;
959 if (imo->imo_multicast_ifp != NULL) {
960 ifp = imo->imo_multicast_ifp;
962 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
963 if ((ia->ia_ifp == ifp) &&
965 prison_check_ip4(cred,
966 &ia->ia_addr.sin_addr) == 0))
970 error = EADDRNOTAVAIL;
972 laddr = ia->ia_addr.sin_addr;
981 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
989 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
994 *laddrp = laddr.s_addr;
996 *faddrp = faddr.s_addr;
1002 in_pcbdisconnect(struct inpcb *inp)
1005 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
1006 INP_WLOCK_ASSERT(inp);
1008 inp->inp_faddr.s_addr = INADDR_ANY;
1015 * in_pcbdetach() is responsibe for disassociating a socket from an inpcb.
1016 * For most protocols, this will be invoked immediately prior to calling
1017 * in_pcbfree(). However, with TCP the inpcb may significantly outlive the
1018 * socket, in which case in_pcbfree() is deferred.
1021 in_pcbdetach(struct inpcb *inp)
1024 KASSERT(inp->inp_socket != NULL, ("%s: inp_socket == NULL", __func__));
1026 inp->inp_socket->so_pcb = NULL;
1027 inp->inp_socket = NULL;
1031 * in_pcbfree_internal() frees an inpcb that has been detached from its
1032 * socket, and whose reference count has reached 0. It will also remove the
1033 * inpcb from any global lists it might remain on.
1036 in_pcbfree_internal(struct inpcb *inp)
1038 struct inpcbinfo *ipi = inp->inp_pcbinfo;
1040 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL", __func__));
1041 KASSERT(inp->inp_refcount == 0, ("%s: refcount !0", __func__));
1043 INP_INFO_WLOCK_ASSERT(ipi);
1044 INP_WLOCK_ASSERT(inp);
1047 if (inp->inp_sp != NULL)
1048 ipsec_delete_pcbpolicy(inp);
1050 inp->inp_gencnt = ++ipi->ipi_gencnt;
1051 in_pcbremlists(inp);
1053 if (inp->inp_vflag & INP_IPV6PROTO) {
1054 ip6_freepcbopts(inp->in6p_outputopts);
1055 if (inp->in6p_moptions != NULL)
1056 ip6_freemoptions(inp->in6p_moptions);
1059 if (inp->inp_options)
1060 (void)m_free(inp->inp_options);
1062 if (inp->inp_moptions != NULL)
1063 inp_freemoptions(inp->inp_moptions);
1066 crfree(inp->inp_cred);
1069 mac_inpcb_destroy(inp);
1072 uma_zfree(ipi->ipi_zone, inp);
1076 * in_pcbref() bumps the reference count on an inpcb in order to maintain
1077 * stability of an inpcb pointer despite the inpcb lock being released. This
1078 * is used in TCP when the inpcbinfo lock needs to be acquired or upgraded,
1079 * but where the inpcb lock is already held.
1081 * While the inpcb will not be freed, releasing the inpcb lock means that the
1082 * connection's state may change, so the caller should be careful to
1083 * revalidate any cached state on reacquiring the lock. Drop the reference
1084 * using in_pcbrele().
1087 in_pcbref(struct inpcb *inp)
1090 INP_WLOCK_ASSERT(inp);
1092 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1094 inp->inp_refcount++;
1098 * Drop a refcount on an inpcb elevated using in_pcbref(); because a call to
1099 * in_pcbfree() may have been made between in_pcbref() and in_pcbrele(), we
1100 * return a flag indicating whether or not the inpcb remains valid. If it is
1101 * valid, we return with the inpcb lock held.
1104 in_pcbrele(struct inpcb *inp)
1107 struct inpcbinfo *ipi = inp->inp_pcbinfo;
1110 KASSERT(inp->inp_refcount > 0, ("%s: refcount 0", __func__));
1112 INP_INFO_WLOCK_ASSERT(ipi);
1113 INP_WLOCK_ASSERT(inp);
1115 inp->inp_refcount--;
1116 if (inp->inp_refcount > 0)
1118 in_pcbfree_internal(inp);
1123 * Unconditionally schedule an inpcb to be freed by decrementing its
1124 * reference count, which should occur only after the inpcb has been detached
1125 * from its socket. If another thread holds a temporary reference (acquired
1126 * using in_pcbref()) then the free is deferred until that reference is
1127 * released using in_pcbrele(), but the inpcb is still unlocked.
1130 in_pcbfree(struct inpcb *inp)
1133 struct inpcbinfo *ipi = inp->inp_pcbinfo;
1136 KASSERT(inp->inp_socket == NULL, ("%s: inp_socket != NULL",
1139 INP_INFO_WLOCK_ASSERT(ipi);
1140 INP_WLOCK_ASSERT(inp);
1142 if (!in_pcbrele(inp))
1147 * in_pcbdrop() removes an inpcb from hashed lists, releasing its address and
1148 * port reservation, and preventing it from being returned by inpcb lookups.
1150 * It is used by TCP to mark an inpcb as unused and avoid future packet
1151 * delivery or event notification when a socket remains open but TCP has
1152 * closed. This might occur as a result of a shutdown()-initiated TCP close
1153 * or a RST on the wire, and allows the port binding to be reused while still
1154 * maintaining the invariant that so_pcb always points to a valid inpcb until
1157 * XXXRW: Possibly in_pcbdrop() should also prevent future notifications by
1158 * in_pcbnotifyall() and in_pcbpurgeif0()?
1161 in_pcbdrop(struct inpcb *inp)
1164 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
1165 INP_WLOCK_ASSERT(inp);
1167 inp->inp_flags |= INP_DROPPED;
1168 if (inp->inp_flags & INP_INHASHLIST) {
1169 struct inpcbport *phd = inp->inp_phd;
1171 LIST_REMOVE(inp, inp_hash);
1172 LIST_REMOVE(inp, inp_portlist);
1173 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1174 LIST_REMOVE(phd, phd_hash);
1177 inp->inp_flags &= ~INP_INHASHLIST;
1183 * Common routines to return the socket addresses associated with inpcbs.
1186 in_sockaddr(in_port_t port, struct in_addr *addr_p)
1188 struct sockaddr_in *sin;
1190 sin = malloc(sizeof *sin, M_SONAME,
1192 sin->sin_family = AF_INET;
1193 sin->sin_len = sizeof(*sin);
1194 sin->sin_addr = *addr_p;
1195 sin->sin_port = port;
1197 return (struct sockaddr *)sin;
1201 in_getsockaddr(struct socket *so, struct sockaddr **nam)
1204 struct in_addr addr;
1207 inp = sotoinpcb(so);
1208 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
1211 port = inp->inp_lport;
1212 addr = inp->inp_laddr;
1215 *nam = in_sockaddr(port, &addr);
1220 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
1223 struct in_addr addr;
1226 inp = sotoinpcb(so);
1227 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
1230 port = inp->inp_fport;
1231 addr = inp->inp_faddr;
1234 *nam = in_sockaddr(port, &addr);
1239 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
1240 struct inpcb *(*notify)(struct inpcb *, int))
1242 struct inpcb *inp, *inp_temp;
1244 INP_INFO_WLOCK(pcbinfo);
1245 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
1248 if ((inp->inp_vflag & INP_IPV4) == 0) {
1253 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1254 inp->inp_socket == NULL) {
1258 if ((*notify)(inp, errno))
1261 INP_INFO_WUNLOCK(pcbinfo);
1265 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1268 struct ip_moptions *imo;
1271 INP_INFO_RLOCK(pcbinfo);
1272 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
1274 imo = inp->inp_moptions;
1275 if ((inp->inp_vflag & INP_IPV4) &&
1278 * Unselect the outgoing interface if it is being
1281 if (imo->imo_multicast_ifp == ifp)
1282 imo->imo_multicast_ifp = NULL;
1285 * Drop multicast group membership if we joined
1286 * through the interface being detached.
1288 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1290 if (imo->imo_membership[i]->inm_ifp == ifp) {
1291 in_delmulti(imo->imo_membership[i]);
1293 } else if (gap != 0)
1294 imo->imo_membership[i - gap] =
1295 imo->imo_membership[i];
1297 imo->imo_num_memberships -= gap;
1301 INP_INFO_RUNLOCK(pcbinfo);
1305 * Lookup a PCB based on the local address and port.
1307 #define INP_LOOKUP_MAPPED_PCB_COST 3
1309 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1310 u_short lport, int lookupflags, struct ucred *cred)
1314 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
1320 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1321 ("%s: invalid lookup flags %d", __func__, lookupflags));
1323 INP_INFO_LOCK_ASSERT(pcbinfo);
1325 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
1326 struct inpcbhead *head;
1328 * Look for an unconnected (wildcard foreign addr) PCB that
1329 * matches the local address and port we're looking for.
1331 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1332 0, pcbinfo->ipi_hashmask)];
1333 LIST_FOREACH(inp, head, inp_hash) {
1335 /* XXX inp locking */
1336 if ((inp->inp_vflag & INP_IPV4) == 0)
1339 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1340 inp->inp_laddr.s_addr == laddr.s_addr &&
1341 inp->inp_lport == lport) {
1346 prison_equal_ip4(cred->cr_prison,
1347 inp->inp_cred->cr_prison))
1356 struct inpcbporthead *porthash;
1357 struct inpcbport *phd;
1358 struct inpcb *match = NULL;
1360 * Best fit PCB lookup.
1362 * First see if this local port is in use by looking on the
1365 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
1366 pcbinfo->ipi_porthashmask)];
1367 LIST_FOREACH(phd, porthash, phd_hash) {
1368 if (phd->phd_port == lport)
1373 * Port is in use by one or more PCBs. Look for best
1376 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1379 !prison_equal_ip4(inp->inp_cred->cr_prison,
1383 /* XXX inp locking */
1384 if ((inp->inp_vflag & INP_IPV4) == 0)
1387 * We never select the PCB that has
1388 * INP_IPV6 flag and is bound to :: if
1389 * we have another PCB which is bound
1390 * to 0.0.0.0. If a PCB has the
1391 * INP_IPV6 flag, then we set its cost
1392 * higher than IPv4 only PCBs.
1394 * Note that the case only happens
1395 * when a socket is bound to ::, under
1396 * the condition that the use of the
1397 * mapped address is allowed.
1399 if ((inp->inp_vflag & INP_IPV6) != 0)
1400 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
1402 if (inp->inp_faddr.s_addr != INADDR_ANY)
1404 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1405 if (laddr.s_addr == INADDR_ANY)
1407 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1410 if (laddr.s_addr != INADDR_ANY)
1413 if (wildcard < matchwild) {
1415 matchwild = wildcard;
1424 #undef INP_LOOKUP_MAPPED_PCB_COST
1427 * Lookup PCB in hash list.
1430 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1431 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int lookupflags,
1434 struct inpcbhead *head;
1435 struct inpcb *inp, *tmpinp;
1436 u_short fport = fport_arg, lport = lport_arg;
1438 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1439 ("%s: invalid lookup flags %d", __func__, lookupflags));
1441 INP_INFO_LOCK_ASSERT(pcbinfo);
1444 * First look for an exact match.
1447 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1448 pcbinfo->ipi_hashmask)];
1449 LIST_FOREACH(inp, head, inp_hash) {
1451 /* XXX inp locking */
1452 if ((inp->inp_vflag & INP_IPV4) == 0)
1455 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1456 inp->inp_laddr.s_addr == laddr.s_addr &&
1457 inp->inp_fport == fport &&
1458 inp->inp_lport == lport) {
1460 * XXX We should be able to directly return
1461 * the inp here, without any checks.
1462 * Well unless both bound with SO_REUSEPORT?
1464 if (prison_flag(inp->inp_cred, PR_IP4))
1474 * Then look for a wildcard match, if requested.
1476 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1477 struct inpcb *local_wild = NULL, *local_exact = NULL;
1479 struct inpcb *local_wild_mapped = NULL;
1481 struct inpcb *jail_wild = NULL;
1485 * Order of socket selection - we always prefer jails.
1486 * 1. jailed, non-wild.
1488 * 3. non-jailed, non-wild.
1489 * 4. non-jailed, wild.
1492 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1493 0, pcbinfo->ipi_hashmask)];
1494 LIST_FOREACH(inp, head, inp_hash) {
1496 /* XXX inp locking */
1497 if ((inp->inp_vflag & INP_IPV4) == 0)
1500 if (inp->inp_faddr.s_addr != INADDR_ANY ||
1501 inp->inp_lport != lport)
1504 /* XXX inp locking */
1505 if (ifp && ifp->if_type == IFT_FAITH &&
1506 (inp->inp_flags & INP_FAITH) == 0)
1509 injail = prison_flag(inp->inp_cred, PR_IP4);
1511 if (prison_check_ip4(inp->inp_cred,
1515 if (local_exact != NULL)
1519 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1524 } else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1526 /* XXX inp locking, NULL check */
1527 if (inp->inp_vflag & INP_IPV6PROTO)
1528 local_wild_mapped = inp;
1536 } /* LIST_FOREACH */
1537 if (jail_wild != NULL)
1539 if (local_exact != NULL)
1540 return (local_exact);
1541 if (local_wild != NULL)
1542 return (local_wild);
1544 if (local_wild_mapped != NULL)
1545 return (local_wild_mapped);
1546 #endif /* defined(INET6) */
1547 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1554 * Insert PCB onto various hash lists.
1557 in_pcbinshash(struct inpcb *inp)
1559 struct inpcbhead *pcbhash;
1560 struct inpcbporthead *pcbporthash;
1561 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1562 struct inpcbport *phd;
1563 u_int32_t hashkey_faddr;
1565 INP_INFO_WLOCK_ASSERT(pcbinfo);
1566 INP_WLOCK_ASSERT(inp);
1567 KASSERT((inp->inp_flags & INP_INHASHLIST) == 0,
1568 ("in_pcbinshash: INP_INHASHLIST"));
1571 if (inp->inp_vflag & INP_IPV6)
1572 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1575 hashkey_faddr = inp->inp_faddr.s_addr;
1577 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1578 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1580 pcbporthash = &pcbinfo->ipi_porthashbase[
1581 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1584 * Go through port list and look for a head for this lport.
1586 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1587 if (phd->phd_port == inp->inp_lport)
1591 * If none exists, malloc one and tack it on.
1594 phd = malloc(sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1596 return (ENOBUFS); /* XXX */
1598 phd->phd_port = inp->inp_lport;
1599 LIST_INIT(&phd->phd_pcblist);
1600 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1603 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1604 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1605 inp->inp_flags |= INP_INHASHLIST;
1610 * Move PCB to the proper hash bucket when { faddr, fport } have been
1611 * changed. NOTE: This does not handle the case of the lport changing (the
1612 * hashed port list would have to be updated as well), so the lport must
1613 * not change after in_pcbinshash() has been called.
1616 in_pcbrehash(struct inpcb *inp)
1618 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1619 struct inpcbhead *head;
1620 u_int32_t hashkey_faddr;
1622 INP_INFO_WLOCK_ASSERT(pcbinfo);
1623 INP_WLOCK_ASSERT(inp);
1624 KASSERT(inp->inp_flags & INP_INHASHLIST,
1625 ("in_pcbrehash: !INP_INHASHLIST"));
1628 if (inp->inp_vflag & INP_IPV6)
1629 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1632 hashkey_faddr = inp->inp_faddr.s_addr;
1634 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1635 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1637 LIST_REMOVE(inp, inp_hash);
1638 LIST_INSERT_HEAD(head, inp, inp_hash);
1642 * Remove PCB from various lists.
1645 in_pcbremlists(struct inpcb *inp)
1647 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1649 INP_INFO_WLOCK_ASSERT(pcbinfo);
1650 INP_WLOCK_ASSERT(inp);
1652 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1653 if (inp->inp_flags & INP_INHASHLIST) {
1654 struct inpcbport *phd = inp->inp_phd;
1656 LIST_REMOVE(inp, inp_hash);
1657 LIST_REMOVE(inp, inp_portlist);
1658 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1659 LIST_REMOVE(phd, phd_hash);
1662 inp->inp_flags &= ~INP_INHASHLIST;
1664 LIST_REMOVE(inp, inp_list);
1665 pcbinfo->ipi_count--;
1669 * A set label operation has occurred at the socket layer, propagate the
1670 * label change into the in_pcb for the socket.
1673 in_pcbsosetlabel(struct socket *so)
1678 inp = sotoinpcb(so);
1679 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1683 mac_inpcb_sosetlabel(so, inp);
1690 * ipport_tick runs once per second, determining if random port allocation
1691 * should be continued. If more than ipport_randomcps ports have been
1692 * allocated in the last second, then we return to sequential port
1693 * allocation. We return to random allocation only once we drop below
1694 * ipport_randomcps for at least ipport_randomtime seconds.
1697 ipport_tick(void *xtp)
1699 VNET_ITERATOR_DECL(vnet_iter);
1701 VNET_LIST_RLOCK_NOSLEEP();
1702 VNET_FOREACH(vnet_iter) {
1703 CURVNET_SET(vnet_iter); /* XXX appease INVARIANTS here */
1704 if (V_ipport_tcpallocs <=
1705 V_ipport_tcplastcount + V_ipport_randomcps) {
1706 if (V_ipport_stoprandom > 0)
1707 V_ipport_stoprandom--;
1709 V_ipport_stoprandom = V_ipport_randomtime;
1710 V_ipport_tcplastcount = V_ipport_tcpallocs;
1713 VNET_LIST_RUNLOCK_NOSLEEP();
1714 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1721 callout_stop(&ipport_tick_callout);
1725 * The ipport_callout should start running at about the time we attach the
1726 * inet or inet6 domains.
1729 ipport_tick_init(const void *unused __unused)
1732 /* Start ipport_tick. */
1733 callout_init(&ipport_tick_callout, CALLOUT_MPSAFE);
1734 callout_reset(&ipport_tick_callout, 1, ipport_tick, NULL);
1735 EVENTHANDLER_REGISTER(shutdown_pre_sync, ip_fini, NULL,
1736 SHUTDOWN_PRI_DEFAULT);
1738 SYSINIT(ipport_tick_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
1739 ipport_tick_init, NULL);
1742 inp_wlock(struct inpcb *inp)
1749 inp_wunlock(struct inpcb *inp)
1756 inp_rlock(struct inpcb *inp)
1763 inp_runlock(struct inpcb *inp)
1771 inp_lock_assert(struct inpcb *inp)
1774 INP_WLOCK_ASSERT(inp);
1778 inp_unlock_assert(struct inpcb *inp)
1781 INP_UNLOCK_ASSERT(inp);
1786 inp_apply_all(void (*func)(struct inpcb *, void *), void *arg)
1790 INP_INFO_RLOCK(&V_tcbinfo);
1791 LIST_FOREACH(inp, V_tcbinfo.ipi_listhead, inp_list) {
1796 INP_INFO_RUNLOCK(&V_tcbinfo);
1800 inp_inpcbtosocket(struct inpcb *inp)
1803 INP_WLOCK_ASSERT(inp);
1804 return (inp->inp_socket);
1808 inp_inpcbtotcpcb(struct inpcb *inp)
1811 INP_WLOCK_ASSERT(inp);
1812 return ((struct tcpcb *)inp->inp_ppcb);
1816 inp_ip_tos_get(const struct inpcb *inp)
1819 return (inp->inp_ip_tos);
1823 inp_ip_tos_set(struct inpcb *inp, int val)
1826 inp->inp_ip_tos = val;
1830 inp_4tuple_get(struct inpcb *inp, uint32_t *laddr, uint16_t *lp,
1831 uint32_t *faddr, uint16_t *fp)
1834 INP_LOCK_ASSERT(inp);
1835 *laddr = inp->inp_laddr.s_addr;
1836 *faddr = inp->inp_faddr.s_addr;
1837 *lp = inp->inp_lport;
1838 *fp = inp->inp_fport;
1842 so_sotoinpcb(struct socket *so)
1845 return (sotoinpcb(so));
1849 so_sototcpcb(struct socket *so)
1852 return (sototcpcb(so));
1857 db_print_indent(int indent)
1861 for (i = 0; i < indent; i++)
1866 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1868 char faddr_str[48], laddr_str[48];
1870 db_print_indent(indent);
1871 db_printf("%s at %p\n", name, inc);
1876 if (inc->inc_flags & INC_ISIPV6) {
1878 ip6_sprintf(laddr_str, &inc->inc6_laddr);
1879 ip6_sprintf(faddr_str, &inc->inc6_faddr);
1883 inet_ntoa_r(inc->inc_laddr, laddr_str);
1884 inet_ntoa_r(inc->inc_faddr, faddr_str);
1888 db_print_indent(indent);
1889 db_printf("inc_laddr %s inc_lport %u\n", laddr_str,
1890 ntohs(inc->inc_lport));
1891 db_print_indent(indent);
1892 db_printf("inc_faddr %s inc_fport %u\n", faddr_str,
1893 ntohs(inc->inc_fport));
1897 db_print_inpflags(int inp_flags)
1902 if (inp_flags & INP_RECVOPTS) {
1903 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1906 if (inp_flags & INP_RECVRETOPTS) {
1907 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1910 if (inp_flags & INP_RECVDSTADDR) {
1911 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1914 if (inp_flags & INP_HDRINCL) {
1915 db_printf("%sINP_HDRINCL", comma ? ", " : "");
1918 if (inp_flags & INP_HIGHPORT) {
1919 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1922 if (inp_flags & INP_LOWPORT) {
1923 db_printf("%sINP_LOWPORT", comma ? ", " : "");
1926 if (inp_flags & INP_ANONPORT) {
1927 db_printf("%sINP_ANONPORT", comma ? ", " : "");
1930 if (inp_flags & INP_RECVIF) {
1931 db_printf("%sINP_RECVIF", comma ? ", " : "");
1934 if (inp_flags & INP_MTUDISC) {
1935 db_printf("%sINP_MTUDISC", comma ? ", " : "");
1938 if (inp_flags & INP_FAITH) {
1939 db_printf("%sINP_FAITH", comma ? ", " : "");
1942 if (inp_flags & INP_RECVTTL) {
1943 db_printf("%sINP_RECVTTL", comma ? ", " : "");
1946 if (inp_flags & INP_DONTFRAG) {
1947 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1950 if (inp_flags & IN6P_IPV6_V6ONLY) {
1951 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1954 if (inp_flags & IN6P_PKTINFO) {
1955 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1958 if (inp_flags & IN6P_HOPLIMIT) {
1959 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1962 if (inp_flags & IN6P_HOPOPTS) {
1963 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1966 if (inp_flags & IN6P_DSTOPTS) {
1967 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1970 if (inp_flags & IN6P_RTHDR) {
1971 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
1974 if (inp_flags & IN6P_RTHDRDSTOPTS) {
1975 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
1978 if (inp_flags & IN6P_TCLASS) {
1979 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
1982 if (inp_flags & IN6P_AUTOFLOWLABEL) {
1983 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
1986 if (inp_flags & INP_TIMEWAIT) {
1987 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
1990 if (inp_flags & INP_ONESBCAST) {
1991 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
1994 if (inp_flags & INP_DROPPED) {
1995 db_printf("%sINP_DROPPED", comma ? ", " : "");
1998 if (inp_flags & INP_SOCKREF) {
1999 db_printf("%sINP_SOCKREF", comma ? ", " : "");
2002 if (inp_flags & IN6P_RFC2292) {
2003 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
2006 if (inp_flags & IN6P_MTU) {
2007 db_printf("IN6P_MTU%s", comma ? ", " : "");
2013 db_print_inpvflag(u_char inp_vflag)
2018 if (inp_vflag & INP_IPV4) {
2019 db_printf("%sINP_IPV4", comma ? ", " : "");
2022 if (inp_vflag & INP_IPV6) {
2023 db_printf("%sINP_IPV6", comma ? ", " : "");
2026 if (inp_vflag & INP_IPV6PROTO) {
2027 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
2033 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
2036 db_print_indent(indent);
2037 db_printf("%s at %p\n", name, inp);
2041 db_print_indent(indent);
2042 db_printf("inp_flow: 0x%x\n", inp->inp_flow);
2044 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
2046 db_print_indent(indent);
2047 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n",
2048 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
2050 db_print_indent(indent);
2051 db_printf("inp_label: %p inp_flags: 0x%x (",
2052 inp->inp_label, inp->inp_flags);
2053 db_print_inpflags(inp->inp_flags);
2056 db_print_indent(indent);
2057 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp,
2059 db_print_inpvflag(inp->inp_vflag);
2062 db_print_indent(indent);
2063 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n",
2064 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
2066 db_print_indent(indent);
2068 if (inp->inp_vflag & INP_IPV6) {
2069 db_printf("in6p_options: %p in6p_outputopts: %p "
2070 "in6p_moptions: %p\n", inp->in6p_options,
2071 inp->in6p_outputopts, inp->in6p_moptions);
2072 db_printf("in6p_icmp6filt: %p in6p_cksum %d "
2073 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
2078 db_printf("inp_ip_tos: %d inp_ip_options: %p "
2079 "inp_ip_moptions: %p\n", inp->inp_ip_tos,
2080 inp->inp_options, inp->inp_moptions);
2083 db_print_indent(indent);
2084 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd,
2085 (uintmax_t)inp->inp_gencnt);
2088 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
2093 db_printf("usage: show inpcb <addr>\n");
2096 inp = (struct inpcb *)addr;
2098 db_print_inpcb(inp, "inpcb", 0);