2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1991, 1993, 1995
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
63 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.27 2004/01/02 04:06:42 ambrisko Exp $
66 #include "opt_ipsec.h"
67 #include "opt_inet6.h"
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
73 #include <sys/domain.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
80 #include <sys/kernel.h>
81 #include <sys/sysctl.h>
83 #include <sys/thread2.h>
84 #include <sys/socketvar2.h>
85 #include <sys/msgport2.h>
87 #include <machine/limits.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
92 #include <net/netisr2.h>
94 #include <netinet/in.h>
95 #include <netinet/in_pcb.h>
96 #include <netinet/in_var.h>
97 #include <netinet/ip_var.h>
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
104 #include <netinet6/ipsec.h>
105 #include <netproto/key/key.h>
106 #include <netproto/ipsec/esp_var.h>
110 #if defined(IPSEC) || defined(IPSEC_ESP)
111 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
114 #include <netproto/ipsec/ipsec.h>
115 #include <netproto/ipsec/key.h>
117 #endif /* FAST_IPSEC */
119 #define INP_LOCALGROUP_SIZMIN 8
120 #define INP_LOCALGROUP_SIZMAX 256
122 struct in_addr zeroin_addr;
125 * These configure the range of local port addresses assigned to
126 * "unspecified" outgoing connections/packets/whatever.
128 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
129 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
131 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
132 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
134 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
135 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
137 #define RANGECHK(var, min, max) \
138 if ((var) < (min)) { (var) = (min); } \
139 else if ((var) > (max)) { (var) = (max); }
141 int udpencap_enable = 1; /* enabled by default */
142 int udpencap_port = 4500; /* triggers decapsulation */
145 * Per-netisr inpcb markers.
146 * NOTE: they should only be used in netisrs.
148 static struct inpcb *in_pcbmarkers;
149 static struct inpcontainer *in_pcbcontainer_markers;
152 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
156 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
158 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
159 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
161 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
162 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
164 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
165 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
172 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
174 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
175 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
176 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
177 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
178 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
179 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
180 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
181 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
182 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
183 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
184 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
185 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
188 * in_pcb.c: manage the Protocol Control Blocks.
190 * NOTE: It is assumed that most of these functions will be called from
191 * a critical section. XXX - There are, unfortunately, a few exceptions
192 * to this rule that should be fixed.
194 * NOTE: The caller should initialize the cpu field to the cpu running the
195 * protocol stack associated with this inpcbinfo.
199 in_pcbinfo_init(struct inpcbinfo *pcbinfo, int cpu, boolean_t shared)
201 KASSERT(cpu >= 0 && cpu < ncpus, ("invalid cpu%d", cpu));
204 LIST_INIT(&pcbinfo->pcblisthead);
205 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave), M_PCB,
209 pcbinfo->infotoken = kmalloc(sizeof(struct lwkt_token),
211 lwkt_token_init(pcbinfo->infotoken, "infotoken");
213 pcbinfo->infotoken = NULL;
217 struct baddynamicports baddynamicports;
220 * Check if the specified port is invalid for dynamic allocation.
223 in_baddynamic(u_int16_t port, u_int16_t proto)
227 return (DP_ISSET(baddynamicports.tcp, port));
230 /* Cannot preset this as it is a sysctl */
231 if (port == udpencap_port)
234 return (DP_ISSET(baddynamicports.udp, port));
241 in_pcbonlist(struct inpcb *inp)
243 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
245 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
246 ("not in the correct netisr"));
247 KASSERT((inp->inp_flags & INP_ONLIST) == 0, ("already on pcblist"));
248 inp->inp_flags |= INP_ONLIST;
250 GET_PCBINFO_TOKEN(pcbinfo);
251 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
252 pcbinfo->ipi_count++;
253 REL_PCBINFO_TOKEN(pcbinfo);
257 in_pcbofflist(struct inpcb *inp)
259 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
261 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
262 ("not in the correct netisr"));
263 KASSERT(inp->inp_flags & INP_ONLIST, ("not on pcblist"));
264 inp->inp_flags &= ~INP_ONLIST;
266 GET_PCBINFO_TOKEN(pcbinfo);
267 LIST_REMOVE(inp, inp_list);
268 KASSERT(pcbinfo->ipi_count > 0,
269 ("invalid inpcb count %d", pcbinfo->ipi_count));
270 pcbinfo->ipi_count--;
271 REL_PCBINFO_TOKEN(pcbinfo);
275 * Allocate a PCB and associate it with the socket.
278 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
285 inp = kmalloc(pcbinfo->ipi_size, M_PCB, M_WAITOK|M_ZERO|M_NULLOK);
288 inp->inp_lgrpindex = -1;
289 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
290 inp->inp_pcbinfo = pcbinfo;
291 inp->inp_socket = so;
293 error = ipsec_init_policy(so, &inp->inp_sp);
300 if (INP_CHECK_SOCKAF(so, AF_INET6)) {
301 if (ip6_auto_flowlabel)
302 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
303 inp->inp_af = AF_INET6;
306 inp->inp_af = AF_INET;
315 * Unlink a pcb with the intention of moving it to another cpu with a
316 * different pcbinfo. While unlinked nothing should attempt to dereference
317 * inp_pcbinfo, NULL it out so we assert if it does.
320 in_pcbunlink_flags(struct inpcb *inp, struct inpcbinfo *pcbinfo, int flags)
322 KASSERT(inp->inp_pcbinfo == pcbinfo, ("pcbinfo mismatch"));
323 KASSERT((inp->inp_flags & (flags | INP_CONNECTED)) == 0,
327 inp->inp_pcbinfo = NULL;
331 in_pcbunlink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
333 in_pcbunlink_flags(inp, pcbinfo, INP_WILDCARD);
337 * Relink a pcb into a new pcbinfo.
340 in_pcblink_flags(struct inpcb *inp, struct inpcbinfo *pcbinfo, int flags)
342 KASSERT(inp->inp_pcbinfo == NULL, ("has pcbinfo"));
343 KASSERT((inp->inp_flags & (flags | INP_CONNECTED)) == 0,
346 inp->inp_pcbinfo = pcbinfo;
351 in_pcblink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
353 return in_pcblink_flags(inp, pcbinfo, INP_WILDCARD);
357 in_pcbsetlport(struct inpcb *inp, int wild, struct ucred *cred)
359 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
360 struct inpcbportinfo *portinfo;
361 u_short first, last, lport, step;
364 int portinfo_first, portinfo_idx;
366 inp->inp_flags |= INP_ANONPORT;
368 step = pcbinfo->portinfo_mask + 1;
369 portinfo_first = mycpuid & pcbinfo->portinfo_mask;
370 portinfo_idx = portinfo_first;
372 portinfo = &pcbinfo->portinfo[portinfo_idx];
374 if (inp->inp_flags & INP_HIGHPORT) {
375 first = ipport_hifirstauto; /* sysctl */
376 last = ipport_hilastauto;
377 lastport = &portinfo->lasthi;
378 } else if (inp->inp_flags & INP_LOWPORT) {
381 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
382 inp->inp_laddr.s_addr = INADDR_ANY;
385 first = ipport_lowfirstauto; /* 1023 */
386 last = ipport_lowlastauto; /* 600 */
387 lastport = &portinfo->lastlow;
389 first = ipport_firstauto; /* sysctl */
390 last = ipport_lastauto;
391 lastport = &portinfo->lastport;
395 * This has to be atomic. If the porthash is shared across multiple
396 * protocol threads (aka tcp) then the token must be held.
398 GET_PORT_TOKEN(portinfo);
401 * Simple check to ensure all ports are not used up causing
404 * We split the two cases (up and down) so that the direction
405 * is not being tested on each round of the loop.
411 in_pcbportrange(&first, &last, portinfo->offset, step);
412 count = (first - last) / step;
415 if (count-- < 0) { /* completely used? */
416 error = EADDRNOTAVAIL;
420 if (*lastport > first || *lastport < last)
422 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
424 lport = htons(*lastport);
425 } while (in_pcblookup_local(portinfo, inp->inp_laddr, lport,
431 in_pcbportrange(&last, &first, portinfo->offset, step);
432 count = (last - first) / step;
435 if (count-- < 0) { /* completely used? */
436 error = EADDRNOTAVAIL;
440 if (*lastport < first || *lastport > last)
442 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
444 lport = htons(*lastport);
445 } while (in_pcblookup_local(portinfo, inp->inp_laddr, lport,
448 inp->inp_lport = lport;
449 in_pcbinsporthash(portinfo, inp);
452 REL_PORT_TOKEN(portinfo);
455 /* Try next portinfo */
457 portinfo_idx &= pcbinfo->portinfo_mask;
458 if (portinfo_idx != portinfo_first)
460 inp->inp_laddr.s_addr = INADDR_ANY;
466 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
468 struct socket *so = inp->inp_socket;
469 struct sockaddr_in jsin;
470 struct ucred *cred = NULL;
473 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
474 return (EADDRNOTAVAIL);
475 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
476 return (EINVAL); /* already bound */
478 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
479 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
481 cred = td->td_proc->p_ucred;
484 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
485 struct inpcbinfo *pcbinfo;
486 struct inpcbportinfo *portinfo;
488 u_short lport, lport_ho;
489 int reuseport = (so->so_options & SO_REUSEPORT);
492 if (nam->sa_len != sizeof *sin)
496 * We should check the family, but old programs
497 * incorrectly fail to initialize it.
499 if (sin->sin_family != AF_INET)
500 return (EAFNOSUPPORT);
502 if (!prison_replace_wildcards(td, nam))
505 lport = sin->sin_port;
506 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
508 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
509 * allow complete duplication of binding if
510 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
511 * and a multicast address is bound on both
512 * new and duplicated sockets.
514 if (so->so_options & SO_REUSEADDR)
515 reuseport = SO_REUSEADDR | SO_REUSEPORT;
516 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
517 sin->sin_port = 0; /* yech... */
518 bzero(&sin->sin_zero, sizeof sin->sin_zero);
519 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
520 return (EADDRNOTAVAIL);
523 inp->inp_laddr = sin->sin_addr;
525 jsin.sin_family = AF_INET;
526 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
527 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
528 inp->inp_laddr.s_addr = INADDR_ANY;
531 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
534 /* Auto-select local port */
535 return in_pcbsetlport(inp, wild, cred);
537 lport_ho = ntohs(lport);
540 if (lport_ho < IPPORT_RESERVED && cred &&
542 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
543 inp->inp_laddr.s_addr = INADDR_ANY;
548 * Locate the proper portinfo based on lport
550 pcbinfo = inp->inp_pcbinfo;
552 &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
553 KKASSERT((lport_ho & pcbinfo->portinfo_mask) ==
557 * This has to be atomic. If the porthash is shared across
558 * multiple protocol threads (aka tcp) then the token must
561 GET_PORT_TOKEN(portinfo);
563 if (so->so_cred->cr_uid != 0 &&
564 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
565 t = in_pcblookup_local(portinfo, sin->sin_addr, lport,
566 INPLOOKUP_WILDCARD, cred);
568 (so->so_cred->cr_uid !=
569 t->inp_socket->so_cred->cr_uid)) {
570 inp->inp_laddr.s_addr = INADDR_ANY;
575 if (cred && !prison_replace_wildcards(td, nam)) {
576 inp->inp_laddr.s_addr = INADDR_ANY;
577 error = EADDRNOTAVAIL;
580 t = in_pcblookup_local(portinfo, sin->sin_addr, lport,
582 if (t && !(reuseport & t->inp_socket->so_options)) {
583 inp->inp_laddr.s_addr = INADDR_ANY;
587 inp->inp_lport = lport;
588 in_pcbinsporthash(portinfo, inp);
591 REL_PORT_TOKEN(portinfo);
594 jsin.sin_family = AF_INET;
595 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
596 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
597 inp->inp_laddr.s_addr = INADDR_ANY;
600 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
602 return in_pcbsetlport(inp, wild, cred);
606 static struct inpcb *
607 in_pcblookup_localremote(struct inpcbportinfo *portinfo, struct in_addr laddr,
608 u_short lport, struct in_addr faddr, u_short fport, struct ucred *cred)
611 struct inpcbporthead *porthash;
612 struct inpcbport *phd;
613 struct inpcb *match = NULL;
616 * If the porthashbase is shared across several cpus, it must
619 ASSERT_PORT_TOKEN_HELD(portinfo);
622 * Best fit PCB lookup.
624 * First see if this local port is in use by looking on the
627 porthash = &portinfo->porthashbase[
628 INP_PCBPORTHASH(lport, portinfo->porthashmask)];
629 LIST_FOREACH(phd, porthash, phd_hash) {
630 if (phd->phd_port == lport)
634 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
636 if (!INP_ISIPV4(inp))
639 if (inp->inp_laddr.s_addr != INADDR_ANY &&
640 inp->inp_laddr.s_addr != laddr.s_addr)
643 if (inp->inp_faddr.s_addr != INADDR_ANY &&
644 inp->inp_faddr.s_addr != faddr.s_addr)
647 if (inp->inp_fport != 0 && inp->inp_fport != fport)
652 inp->inp_socket->so_cred->cr_prison) {
662 in_pcbbind_remote(struct inpcb *inp, const struct sockaddr *remote,
665 struct proc *p = td->td_proc;
666 unsigned short *lastport;
667 const struct sockaddr_in *sin = (const struct sockaddr_in *)remote;
668 struct sockaddr_in jsin;
669 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
670 struct inpcbportinfo *portinfo;
671 struct ucred *cred = NULL;
672 u_short first, last, lport, step;
673 int count, error, dup;
674 int portinfo_first, portinfo_idx;
676 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
677 return (EADDRNOTAVAIL);
679 KKASSERT(inp->inp_laddr.s_addr != INADDR_ANY);
680 if (inp->inp_lport != 0)
681 return (EINVAL); /* already bound */
686 jsin.sin_family = AF_INET;
687 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
688 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
689 inp->inp_laddr.s_addr = INADDR_ANY;
692 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
694 inp->inp_flags |= INP_ANONPORT;
696 step = pcbinfo->portinfo_mask + 1;
697 portinfo_first = mycpuid & pcbinfo->portinfo_mask;
698 portinfo_idx = portinfo_first;
700 portinfo = &pcbinfo->portinfo[portinfo_idx];
703 if (inp->inp_flags & INP_HIGHPORT) {
704 first = ipport_hifirstauto; /* sysctl */
705 last = ipport_hilastauto;
706 lastport = &portinfo->lasthi;
707 } else if (inp->inp_flags & INP_LOWPORT) {
710 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
711 inp->inp_laddr.s_addr = INADDR_ANY;
714 first = ipport_lowfirstauto; /* 1023 */
715 last = ipport_lowlastauto; /* 600 */
716 lastport = &portinfo->lastlow;
718 first = ipport_firstauto; /* sysctl */
719 last = ipport_lastauto;
720 lastport = &portinfo->lastport;
724 * This has to be atomic. If the porthash is shared across multiple
725 * protocol threads (aka tcp) then the token must be held.
727 GET_PORT_TOKEN(portinfo);
731 * Simple check to ensure all ports are not used up causing
734 * We split the two cases (up and down) so that the direction
735 * is not being tested on each round of the loop.
741 in_pcbportrange(&first, &last, portinfo->offset, step);
742 count = (first - last) / step;
745 if (count-- < 0) { /* completely used? */
746 error = EADDRNOTAVAIL;
750 if (*lastport > first || *lastport < last)
752 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
754 lport = htons(*lastport);
755 } while (in_pcblookup_localremote(portinfo, inp->inp_laddr,
756 lport, sin->sin_addr, sin->sin_port, cred));
761 in_pcbportrange(&last, &first, portinfo->offset, step);
762 count = (last - first) / step;
765 if (count-- < 0) { /* completely used? */
766 error = EADDRNOTAVAIL;
770 if (*lastport < first || *lastport > last)
772 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
774 lport = htons(*lastport);
775 } while (in_pcblookup_localremote(portinfo, inp->inp_laddr,
776 lport, sin->sin_addr, sin->sin_port, cred));
779 /* This could happen on loopback interface */
780 if (sin->sin_port == lport &&
781 sin->sin_addr.s_addr == inp->inp_laddr.s_addr) {
784 * Duplicate again; give up
786 error = EADDRNOTAVAIL;
792 inp->inp_lport = lport;
793 in_pcbinsporthash(portinfo, inp);
796 REL_PORT_TOKEN(portinfo);
799 /* Try next portinfo */
801 portinfo_idx &= pcbinfo->portinfo_mask;
802 if (portinfo_idx != portinfo_first)
804 inp->inp_laddr.s_addr = INADDR_ANY;
810 * Transform old in_pcbconnect() into an inner subroutine for new
811 * in_pcbconnect(): Do some validity-checking on the remote
812 * address (in mbuf 'nam') and then determine local host address
813 * (i.e., which interface) to use to access that remote host.
815 * This preserves definition of in_pcbconnect(), while supporting a
816 * slightly different version for T/TCP. (This is more than
817 * a bit of a kludge, but cleaning up the internal interfaces would
818 * have forced minor changes in every protocol).
821 in_pcbladdr_find(struct inpcb *inp, struct sockaddr *nam,
822 struct sockaddr_in **plocal_sin, struct thread *td, int find)
824 struct in_ifaddr *ia;
825 struct ucred *cred = NULL;
826 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
827 struct sockaddr *jsin;
828 int jailed = 0, alloc_route = 0;
830 if (nam->sa_len != sizeof *sin)
832 if (sin->sin_family != AF_INET)
833 return (EAFNOSUPPORT);
834 if (sin->sin_port == 0)
835 return (EADDRNOTAVAIL);
836 if (td && td->td_proc && td->td_proc->p_ucred)
837 cred = td->td_proc->p_ucred;
838 if (cred && cred->cr_prison)
840 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
841 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
843 * If the destination address is INADDR_ANY,
844 * use the primary local address.
845 * If the supplied address is INADDR_BROADCAST,
846 * and the primary interface supports broadcast,
847 * choose the broadcast address for that interface.
849 if (sin->sin_addr.s_addr == INADDR_ANY)
850 sin->sin_addr = IA_SIN(ia)->sin_addr;
851 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
852 (ia->ia_ifp->if_flags & IFF_BROADCAST))
853 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
860 * If route is known or can be allocated now,
861 * our src addr is taken from the i/f, else punt.
862 * Note that we should check the address family of the cached
863 * destination, in case of sharing the cache with IPv6.
865 ro = &inp->inp_route;
867 (!(ro->ro_rt->rt_flags & RTF_UP) ||
868 ro->ro_dst.sa_family != AF_INET ||
869 satosin(&ro->ro_dst)->sin_addr.s_addr !=
870 sin->sin_addr.s_addr ||
871 inp->inp_socket->so_options & SO_DONTROUTE)) {
875 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
876 (ro->ro_rt == NULL ||
877 ro->ro_rt->rt_ifp == NULL)) {
878 /* No route yet, so try to acquire one */
879 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
880 ro->ro_dst.sa_family = AF_INET;
881 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
882 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
888 * If we found a route, use the address
889 * corresponding to the outgoing interface
890 * unless it is the loopback (in case a route
891 * to our address on another net goes to loopback).
894 !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
896 if (jailed_ip(cred->cr_prison,
897 ro->ro_rt->rt_ifa->ifa_addr)) {
898 ia = ifatoia(ro->ro_rt->rt_ifa);
901 ia = ifatoia(ro->ro_rt->rt_ifa);
905 u_short fport = sin->sin_port;
908 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
909 if (ia && jailed && !jailed_ip(cred->cr_prison,
910 sintosa(&ia->ia_addr)))
913 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
914 if (ia && jailed && !jailed_ip(cred->cr_prison,
915 sintosa(&ia->ia_addr)))
917 sin->sin_port = fport;
919 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
920 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
921 if (ia && jailed && !jailed_ip(cred->cr_prison,
922 sintosa(&ia->ia_addr)))
925 if (!jailed && ia == NULL)
929 * If the destination address is multicast and an outgoing
930 * interface has been set as a multicast option, use the
931 * address of that interface as our source address.
933 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
934 inp->inp_moptions != NULL) {
935 struct ip_moptions *imo;
938 imo = inp->inp_moptions;
939 if (imo->imo_multicast_ifp != NULL) {
940 struct in_ifaddr_container *iac;
942 ifp = imo->imo_multicast_ifp;
945 &in_ifaddrheads[mycpuid], ia_link) {
946 if (iac->ia->ia_ifp == ifp) {
956 * Don't do pcblookup call here; return interface in plocal_sin
957 * and exit to caller, that will do the lookup.
959 if (ia == NULL && jailed) {
960 if ((jsin = prison_get_nonlocal(
961 cred->cr_prison, AF_INET, NULL)) != NULL ||
962 (jsin = prison_get_local(
963 cred->cr_prison, AF_INET, NULL)) != NULL) {
964 *plocal_sin = satosin(jsin);
970 *plocal_sin = &ia->ia_addr;
976 in_pcbresetroute(inp);
977 return (EADDRNOTAVAIL);
981 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
982 struct sockaddr_in **plocal_sin, struct thread *td)
984 return in_pcbladdr_find(inp, nam, plocal_sin, td,
985 (inp->inp_laddr.s_addr == INADDR_ANY));
990 * Connect from a socket to a specified address.
991 * Both address and port must be specified in argument sin.
992 * If don't have a local address for this socket yet,
996 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
998 struct sockaddr_in *if_sin;
999 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1002 /* Call inner routine to assign local interface address. */
1003 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
1006 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
1007 inp->inp_laddr.s_addr ?
1008 inp->inp_laddr : if_sin->sin_addr,
1009 inp->inp_lport, FALSE, NULL) != NULL) {
1010 return (EADDRINUSE);
1012 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1013 if (inp->inp_lport == 0) {
1014 error = in_pcbbind(inp, NULL, td);
1018 inp->inp_laddr = if_sin->sin_addr;
1020 inp->inp_faddr = sin->sin_addr;
1021 inp->inp_fport = sin->sin_port;
1022 in_pcbinsconnhash(inp);
1027 in_pcbdisconnect(struct inpcb *inp)
1030 in_pcbremconnhash(inp);
1031 inp->inp_faddr.s_addr = INADDR_ANY;
1036 in_pcbdetach(struct inpcb *inp)
1038 struct socket *so = inp->inp_socket;
1039 struct inpcbinfo *ipi = inp->inp_pcbinfo;
1042 ipsec4_delete_pcbpolicy(inp);
1044 inp->inp_gencnt = ++ipi->ipi_gencnt;
1045 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
1046 in_pcbremlists(inp);
1048 sofree(so); /* remove pcb ref */
1049 if (inp->inp_options)
1050 m_free(inp->inp_options);
1051 if (inp->inp_route.ro_rt)
1052 rtfree(inp->inp_route.ro_rt);
1053 ip_freemoptions(inp->inp_moptions);
1058 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
1059 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
1060 * in struct pr_usrreqs, so that protocols can just reference then directly
1061 * without the need for a wrapper function. The socket must have a valid
1062 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
1063 * except through a kernel programming error, so it is acceptable to panic
1064 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
1065 * because there actually /is/ a programming error somewhere... XXX)
1068 in_setsockaddr(struct socket *so, struct sockaddr **nam)
1071 struct sockaddr_in *sin;
1074 * Do the malloc first in case it blocks.
1076 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1077 sin->sin_family = AF_INET;
1078 sin->sin_len = sizeof *sin;
1084 kfree(sin, M_SONAME);
1085 return (ECONNRESET);
1087 sin->sin_port = inp->inp_lport;
1088 sin->sin_addr = inp->inp_laddr;
1091 *nam = (struct sockaddr *)sin;
1096 in_setsockaddr_dispatch(netmsg_t msg)
1100 error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1101 lwkt_replymsg(&msg->lmsg, error);
1105 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
1108 struct sockaddr_in *sin;
1111 * Do the malloc first in case it blocks.
1113 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1114 sin->sin_family = AF_INET;
1115 sin->sin_len = sizeof *sin;
1121 kfree(sin, M_SONAME);
1122 return (ECONNRESET);
1124 sin->sin_port = inp->inp_fport;
1125 sin->sin_addr = inp->inp_faddr;
1128 *nam = (struct sockaddr *)sin;
1133 in_setpeeraddr_dispatch(netmsg_t msg)
1137 error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1138 lwkt_replymsg(&msg->lmsg, error);
1142 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int err,
1143 inp_notify_t notify)
1145 struct inpcb *inp, *marker;
1147 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1148 ("not in the correct netisr"));
1149 marker = &in_pcbmarkers[mycpuid];
1153 * - If INP_PLACEMARKER is set we must ignore the rest of the
1154 * structure and skip it.
1155 * - It is safe to nuke inpcbs here, since we are in their own
1158 GET_PCBINFO_TOKEN(pcbinfo);
1160 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1161 while ((inp = LIST_NEXT(marker, inp_list)) != NULL) {
1162 LIST_REMOVE(marker, inp_list);
1163 LIST_INSERT_AFTER(inp, marker, inp_list);
1165 if (inp->inp_flags & INP_PLACEMARKER)
1168 if (!INP_ISIPV4(inp))
1171 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1172 inp->inp_socket == NULL)
1174 (*notify)(inp, err); /* can remove inp from list! */
1176 LIST_REMOVE(marker, inp_list);
1178 REL_PCBINFO_TOKEN(pcbinfo);
1182 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1184 struct inpcb *inp, *marker;
1187 * We only need to make sure that we are in netisr0, where all
1188 * multicast operation happen. We could check inpcbinfo which
1189 * does not belong to netisr0 by holding the inpcbinfo's token.
1190 * In this case, the pcbinfo must be able to be shared, i.e.
1191 * pcbinfo->infotoken is not NULL.
1193 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1194 ("not in netisr0"));
1195 KASSERT(pcbinfo->cpu == 0 || pcbinfo->infotoken != NULL,
1196 ("pcbinfo could not be shared"));
1199 * Get a marker for the current netisr (netisr0).
1201 * It is possible that the multicast address deletion blocks,
1202 * which could cause temporary token releasing. So we use
1203 * inpcb marker here to get a coherent view of the inpcb list.
1205 * While, on the other hand, moptions are only added and deleted
1206 * in netisr0, so we would not see staled moption or miss moption
1207 * even if the token was released due to the blocking multicast
1210 marker = &in_pcbmarkers[mycpuid];
1212 GET_PCBINFO_TOKEN(pcbinfo);
1214 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1215 while ((inp = LIST_NEXT(marker, inp_list)) != NULL) {
1216 struct ip_moptions *imo;
1218 LIST_REMOVE(marker, inp_list);
1219 LIST_INSERT_AFTER(inp, marker, inp_list);
1221 if (inp->inp_flags & INP_PLACEMARKER)
1223 imo = inp->inp_moptions;
1224 if (INP_ISIPV4(inp) && imo != NULL) {
1228 * Unselect the outgoing interface if it is being
1231 if (imo->imo_multicast_ifp == ifp)
1232 imo->imo_multicast_ifp = NULL;
1235 * Drop multicast group membership if we joined
1236 * through the interface being detached.
1238 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1240 if (imo->imo_membership[i]->inm_ifp == ifp) {
1243 * This could block and the pcbinfo
1244 * token could be passively released.
1246 in_delmulti(imo->imo_membership[i]);
1248 } else if (gap != 0)
1249 imo->imo_membership[i - gap] =
1250 imo->imo_membership[i];
1252 imo->imo_num_memberships -= gap;
1255 LIST_REMOVE(marker, inp_list);
1257 REL_PCBINFO_TOKEN(pcbinfo);
1261 * Check for alternatives when higher level complains
1262 * about service problems. For now, invalidate cached
1263 * routing information. If the route was created dynamically
1264 * (by a redirect), time to try a default gateway again.
1267 in_losing(struct inpcb *inp)
1270 struct rt_addrinfo rtinfo;
1272 if ((rt = inp->inp_route.ro_rt)) {
1273 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1274 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1275 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1276 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1277 rtinfo.rti_flags = rt->rt_flags;
1278 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1279 if (rt->rt_flags & RTF_DYNAMIC) {
1280 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1281 rt_mask(rt), rt->rt_flags, NULL);
1283 inp->inp_route.ro_rt = NULL;
1286 * A new route can be allocated
1287 * the next time output is attempted.
1293 * After a routing change, flush old routing
1294 * and allocate a (hopefully) better one.
1297 in_rtchange(struct inpcb *inp, int err)
1299 if (inp->inp_route.ro_rt) {
1300 rtfree(inp->inp_route.ro_rt);
1301 inp->inp_route.ro_rt = NULL;
1303 * A new route can be allocated the next time
1304 * output is attempted.
1310 * Lookup a PCB based on the local address and port.
1313 in_pcblookup_local(struct inpcbportinfo *portinfo, struct in_addr laddr,
1314 u_int lport_arg, int wild_okay, struct ucred *cred)
1317 int matchwild = 3, wildcard;
1318 u_short lport = lport_arg;
1319 struct inpcbporthead *porthash;
1320 struct inpcbport *phd;
1321 struct inpcb *match = NULL;
1324 * If the porthashbase is shared across several cpus, it must
1327 ASSERT_PORT_TOKEN_HELD(portinfo);
1330 * Best fit PCB lookup.
1332 * First see if this local port is in use by looking on the
1335 porthash = &portinfo->porthashbase[
1336 INP_PCBPORTHASH(lport, portinfo->porthashmask)];
1337 LIST_FOREACH(phd, porthash, phd_hash) {
1338 if (phd->phd_port == lport)
1343 * Port is in use by one or more PCBs. Look for best
1346 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1349 if (!INP_ISIPV4(inp))
1352 if (inp->inp_faddr.s_addr != INADDR_ANY)
1354 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1355 if (laddr.s_addr == INADDR_ANY)
1357 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1360 if (laddr.s_addr != INADDR_ANY)
1363 if (wildcard && !wild_okay)
1365 if (wildcard < matchwild &&
1368 inp->inp_socket->so_cred->cr_prison)) {
1370 matchwild = wildcard;
1371 if (matchwild == 0) {
1381 in_pcblocalgroup_last(const struct inpcbinfo *pcbinfo,
1382 const struct inpcb *inp)
1384 const struct inp_localgrphead *hdr;
1385 const struct inp_localgroup *grp;
1388 if (pcbinfo->localgrphashbase == NULL)
1391 GET_PCBINFO_TOKEN(pcbinfo);
1393 hdr = &pcbinfo->localgrphashbase[
1394 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1396 LIST_FOREACH(grp, hdr, il_list) {
1397 if (grp->il_af == inp->inp_af &&
1398 grp->il_lport == inp->inp_lport &&
1399 memcmp(&grp->il_dependladdr,
1400 &inp->inp_inc.inc_ie.ie_dependladdr,
1401 sizeof(grp->il_dependladdr)) == 0) {
1405 if (grp == NULL || grp->il_inpcnt == 1) {
1406 REL_PCBINFO_TOKEN(pcbinfo);
1410 KASSERT(grp->il_inpcnt >= 2,
1411 ("invalid localgroup inp count %d", grp->il_inpcnt));
1412 for (i = 0; i < grp->il_inpcnt; ++i) {
1413 if (grp->il_inp[i] == inp) {
1414 int last = grp->il_inpcnt - 1;
1417 last = grp->il_inpcnt - 2;
1418 REL_PCBINFO_TOKEN(pcbinfo);
1419 return grp->il_inp[last];
1422 REL_PCBINFO_TOKEN(pcbinfo);
1426 static struct inpcb *
1427 inp_localgroup_lookup(const struct inpcbinfo *pcbinfo,
1428 struct in_addr laddr, uint16_t lport, uint32_t pkt_hash)
1430 struct inpcb *local_wild = NULL;
1431 const struct inp_localgrphead *hdr;
1432 const struct inp_localgroup *grp;
1434 ASSERT_PCBINFO_TOKEN_HELD(pcbinfo);
1436 hdr = &pcbinfo->localgrphashbase[
1437 INP_PCBLOCALGRPHASH(lport, pcbinfo->localgrphashmask)];
1440 * Order of socket selection:
1444 * NOTE: Local group does not contain jailed sockets
1446 LIST_FOREACH(grp, hdr, il_list) {
1448 if (grp->il_af != AF_INET)
1451 if (grp->il_lport == lport) {
1455 * Modulo-N is used here, which greatly reduces
1456 * completion queue token contention, thus more
1457 * cpu time is saved.
1459 idx = pkt_hash % grp->il_inpcnt;
1460 if (grp->il_laddr.s_addr == laddr.s_addr)
1461 return grp->il_inp[idx];
1462 else if (grp->il_laddr.s_addr == INADDR_ANY)
1463 local_wild = grp->il_inp[idx];
1466 if (local_wild != NULL)
1472 * Lookup PCB in hash list.
1475 in_pcblookup_pkthash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1476 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1477 boolean_t wildcard, struct ifnet *ifp, const struct mbuf *m)
1479 struct inpcbhead *head;
1480 struct inpcb *inp, *jinp=NULL;
1481 u_short fport = fport_arg, lport = lport_arg;
1484 * First look for an exact match.
1486 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1487 laddr.s_addr, lport, pcbinfo->hashmask)];
1488 LIST_FOREACH(inp, head, inp_hash) {
1490 if (!INP_ISIPV4(inp))
1493 if (in_hosteq(inp->inp_faddr, faddr) &&
1494 in_hosteq(inp->inp_laddr, laddr) &&
1495 inp->inp_fport == fport && inp->inp_lport == lport) {
1497 if (inp->inp_socket == NULL ||
1498 inp->inp_socket->so_cred->cr_prison == NULL) {
1510 struct inpcb *local_wild = NULL;
1511 struct inpcb *jinp_wild = NULL;
1512 struct inpcontainer *ic;
1513 struct inpcontainerhead *chead;
1514 struct sockaddr_in jsin;
1517 GET_PCBINFO_TOKEN(pcbinfo);
1520 * Check local group first
1522 if (pcbinfo->localgrphashbase != NULL &&
1523 m != NULL && (m->m_flags & M_HASH) &&
1524 !(ifp && ifp->if_type == IFT_FAITH)) {
1525 inp = inp_localgroup_lookup(pcbinfo,
1526 laddr, lport, m->m_pkthdr.hash);
1528 REL_PCBINFO_TOKEN(pcbinfo);
1534 * Order of socket selection:
1535 * 1. non-jailed, non-wild.
1536 * 2. non-jailed, wild.
1537 * 3. jailed, non-wild.
1540 jsin.sin_family = AF_INET;
1541 chead = &pcbinfo->wildcardhashbase[
1542 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1543 LIST_FOREACH(ic, chead, ic_list) {
1545 if (inp->inp_flags & INP_PLACEMARKER)
1548 jsin.sin_addr.s_addr = laddr.s_addr;
1550 if (!INP_ISIPV4(inp))
1553 if (inp->inp_socket != NULL)
1554 cred = inp->inp_socket->so_cred;
1557 if (cred != NULL && jailed(cred)) {
1561 if (!jailed_ip(cred->cr_prison,
1562 (struct sockaddr *)&jsin))
1565 if (inp->inp_lport == lport) {
1566 if (ifp && ifp->if_type == IFT_FAITH &&
1567 !(inp->inp_flags & INP_FAITH))
1569 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1570 if (cred != NULL && jailed(cred)) {
1573 REL_PCBINFO_TOKEN(pcbinfo);
1577 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1578 if (cred != NULL && jailed(cred))
1586 REL_PCBINFO_TOKEN(pcbinfo);
1588 if (local_wild != NULL)
1589 return (local_wild);
1602 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1603 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1604 boolean_t wildcard, struct ifnet *ifp)
1606 return in_pcblookup_pkthash(pcbinfo, faddr, fport_arg,
1607 laddr, lport_arg, wildcard, ifp, NULL);
1611 * Insert PCB into connection hash table.
1614 in_pcbinsconnhash(struct inpcb *inp)
1616 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1617 struct inpcbhead *bucket;
1618 u_int32_t hashkey_faddr, hashkey_laddr;
1621 if (INP_ISIPV6(inp)) {
1622 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1623 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1626 hashkey_faddr = inp->inp_faddr.s_addr;
1627 hashkey_laddr = inp->inp_laddr.s_addr;
1632 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1633 ("not in the correct netisr"));
1634 ASSERT_INP_NOTINHASH(inp);
1635 inp->inp_flags |= INP_CONNECTED;
1638 * Insert into the connection hash table.
1640 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1641 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1642 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1646 * Remove PCB from connection hash table.
1649 in_pcbremconnhash(struct inpcb *inp)
1651 struct inpcbinfo *pcbinfo __debugvar = inp->inp_pcbinfo;
1653 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1654 ("not in the correct netisr"));
1655 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1657 LIST_REMOVE(inp, inp_hash);
1658 inp->inp_flags &= ~INP_CONNECTED;
1662 * Insert PCB into port hash table.
1665 in_pcbinsporthash(struct inpcbportinfo *portinfo, struct inpcb *inp)
1667 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1668 struct inpcbporthead *pcbporthash;
1669 struct inpcbport *phd;
1672 * If the porthashbase is shared across several cpus, it must
1675 ASSERT_PORT_TOKEN_HELD(portinfo);
1678 * Insert into the port hash table.
1680 pcbporthash = &portinfo->porthashbase[
1681 INP_PCBPORTHASH(inp->inp_lport, portinfo->porthashmask)];
1683 /* Go through port list and look for a head for this lport. */
1684 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1685 if (phd->phd_port == inp->inp_lport)
1689 /* If none exists, use saved one and tack it on. */
1691 KKASSERT(pcbinfo->portsave != NULL);
1692 phd = pcbinfo->portsave;
1693 pcbinfo->portsave = NULL;
1694 phd->phd_port = inp->inp_lport;
1695 LIST_INIT(&phd->phd_pcblist);
1696 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1699 inp->inp_portinfo = portinfo;
1701 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1704 * Malloc one inpcbport for later use. It is safe to use
1705 * "wait" malloc here (port token would be released, if
1706 * malloc ever blocked), since all changes to the porthash
1709 if (pcbinfo->portsave == NULL) {
1710 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1711 M_PCB, M_INTWAIT | M_ZERO);
1716 in_pcbinsporthash_lport(struct inpcb *inp)
1718 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1719 struct inpcbportinfo *portinfo;
1722 /* Locate the proper portinfo based on lport */
1723 lport_ho = ntohs(inp->inp_lport);
1724 portinfo = &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
1725 KKASSERT((lport_ho & pcbinfo->portinfo_mask) == portinfo->offset);
1727 GET_PORT_TOKEN(portinfo);
1728 in_pcbinsporthash(portinfo, inp);
1729 REL_PORT_TOKEN(portinfo);
1732 static struct inp_localgroup *
1733 inp_localgroup_alloc(u_char af, uint16_t port,
1734 const union in_dependaddr *addr, int size)
1736 struct inp_localgroup *grp;
1738 grp = kmalloc(__offsetof(struct inp_localgroup, il_inp[size]),
1739 M_TEMP, M_INTWAIT | M_ZERO);
1741 grp->il_lport = port;
1742 grp->il_dependladdr = *addr;
1743 grp->il_inpsiz = size;
1749 inp_localgroup_free(struct inp_localgroup *grp)
1755 inp_localgroup_destroy(struct inp_localgroup *grp)
1757 LIST_REMOVE(grp, il_list);
1758 inp_localgroup_free(grp);
1762 inp_localgroup_copy(struct inp_localgroup *grp,
1763 const struct inp_localgroup *old_grp)
1767 KASSERT(old_grp->il_inpcnt < grp->il_inpsiz,
1768 ("invalid new local group size %d and old local group count %d",
1769 grp->il_inpsiz, old_grp->il_inpcnt));
1770 for (i = 0; i < old_grp->il_inpcnt; ++i)
1771 grp->il_inp[i] = old_grp->il_inp[i];
1772 grp->il_inpcnt = old_grp->il_inpcnt;
1776 in_pcbinslocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1778 struct inp_localgrphead *hdr;
1779 struct inp_localgroup *grp, *grp_alloc = NULL;
1783 ASSERT_PCBINFO_TOKEN_HELD(pcbinfo);
1785 if (pcbinfo->localgrphashbase == NULL)
1789 * XXX don't allow jailed socket to join local group
1791 if (inp->inp_socket != NULL)
1792 cred = inp->inp_socket->so_cred;
1795 if (cred != NULL && jailed(cred))
1798 hdr = &pcbinfo->localgrphashbase[
1799 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1802 LIST_FOREACH(grp, hdr, il_list) {
1803 if (grp->il_af == inp->inp_af &&
1804 grp->il_lport == inp->inp_lport &&
1805 memcmp(&grp->il_dependladdr,
1806 &inp->inp_inc.inc_ie.ie_dependladdr,
1807 sizeof(grp->il_dependladdr)) == 0) {
1813 * Create a new local group
1815 if (grp_alloc == NULL) {
1816 grp_alloc = inp_localgroup_alloc(inp->inp_af,
1817 inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr,
1818 INP_LOCALGROUP_SIZMIN);
1820 * Local group allocation could block and the
1821 * local group w/ the same property might have
1822 * been added by others when we were blocked;
1827 /* Local group has been allocated; link it */
1830 LIST_INSERT_HEAD(hdr, grp, il_list);
1832 } else if (grp->il_inpcnt == grp->il_inpsiz) {
1833 if (grp->il_inpsiz >= INP_LOCALGROUP_SIZMAX) {
1834 static int limit_logged = 0;
1836 if (!limit_logged) {
1838 kprintf("local group port %d, "
1839 "limit reached\n", ntohs(grp->il_lport));
1841 if (grp_alloc != NULL) {
1843 * This would happen if the local group
1844 * w/ the same property was expanded when
1845 * our local group allocation blocked.
1847 inp_localgroup_free(grp_alloc);
1853 * Expand this local group
1855 if (grp_alloc == NULL ||
1856 grp->il_inpcnt >= grp_alloc->il_inpsiz) {
1857 if (grp_alloc != NULL)
1858 inp_localgroup_free(grp_alloc);
1859 grp_alloc = inp_localgroup_alloc(grp->il_af,
1860 grp->il_lport, &grp->il_dependladdr,
1861 grp->il_inpsiz * 2);
1863 * Local group allocation could block and the
1864 * local group w/ the same property might have
1865 * been expanded by others when we were blocked;
1872 * Save the old local group, link the new one, and then
1873 * destroy the old local group
1875 inp_localgroup_copy(grp_alloc, grp);
1876 LIST_INSERT_HEAD(hdr, grp_alloc, il_list);
1877 inp_localgroup_destroy(grp);
1883 * Found the local group
1885 if (grp_alloc != NULL) {
1887 * This would happen if the local group w/ the
1888 * same property was added or expanded when our
1889 * local group allocation blocked.
1891 inp_localgroup_free(grp_alloc);
1896 KASSERT(grp->il_inpcnt < grp->il_inpsiz,
1897 ("invalid local group size %d and count %d",
1898 grp->il_inpsiz, grp->il_inpcnt));
1901 * Keep the local group sorted by the inpcb local group index
1902 * in ascending order.
1904 * This eases the multi-process userland application which uses
1905 * SO_REUSEPORT sockets and binds process to the owner cpu of
1906 * the SO_REUSEPORT socket:
1907 * If we didn't sort the local group by the inpcb local group
1908 * index and one of the process owning an inpcb in this local
1909 * group restarted, e.g. crashed and restarted by watchdog,
1910 * other processes owning a inpcb in this local group would have
1911 * to detect that event, refetch its socket's owner cpu, and
1914 idx = grp->il_inpcnt;
1915 for (i = 0; i < idx; ++i) {
1916 struct inpcb *oinp = grp->il_inp[i];
1918 if (oinp->inp_lgrpindex > i) {
1919 if (inp->inp_lgrpindex < 0) {
1920 inp->inp_lgrpindex = i;
1921 } else if (inp->inp_lgrpindex != i) {
1923 kprintf("inp %p: grpidx %d, "
1924 "assigned to %d, cpu%d\n",
1925 inp, inp->inp_lgrpindex, i,
1929 grp->il_inp[i] = inp;
1931 /* Pull down inpcbs */
1932 for (; i < grp->il_inpcnt; ++i) {
1933 struct inpcb *oinp1 = grp->il_inp[i + 1];
1935 grp->il_inp[i + 1] = oinp;
1943 if (inp->inp_lgrpindex < 0) {
1944 inp->inp_lgrpindex = idx;
1945 } else if (inp->inp_lgrpindex != idx) {
1947 kprintf("inp %p: grpidx %d, assigned to %d, cpu%d\n",
1948 inp, inp->inp_lgrpindex, idx, mycpuid);
1951 grp->il_inp[idx] = inp;
1956 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1958 struct inpcontainer *ic;
1959 struct inpcontainerhead *bucket;
1961 GET_PCBINFO_TOKEN(pcbinfo);
1963 in_pcbinslocalgrphash_oncpu(inp, pcbinfo);
1965 bucket = &pcbinfo->wildcardhashbase[
1966 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1968 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1970 LIST_INSERT_HEAD(bucket, ic, ic_list);
1972 REL_PCBINFO_TOKEN(pcbinfo);
1976 * Insert PCB into wildcard hash table.
1979 in_pcbinswildcardhash(struct inpcb *inp)
1981 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1983 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1984 ("not in correct netisr"));
1985 ASSERT_INP_NOTINHASH(inp);
1986 inp->inp_flags |= INP_WILDCARD;
1988 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1992 in_pcbremlocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1994 struct inp_localgrphead *hdr;
1995 struct inp_localgroup *grp;
1997 ASSERT_PCBINFO_TOKEN_HELD(pcbinfo);
1999 if (pcbinfo->localgrphashbase == NULL)
2002 hdr = &pcbinfo->localgrphashbase[
2003 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
2005 LIST_FOREACH(grp, hdr, il_list) {
2008 for (i = 0; i < grp->il_inpcnt; ++i) {
2009 if (grp->il_inp[i] != inp)
2012 if (grp->il_inpcnt == 1) {
2013 /* Destroy this local group */
2014 inp_localgroup_destroy(grp);
2016 /* Pull up inpcbs */
2017 for (; i + 1 < grp->il_inpcnt; ++i)
2018 grp->il_inp[i] = grp->il_inp[i + 1];
2027 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
2029 struct inpcontainer *ic;
2030 struct inpcontainerhead *head;
2032 GET_PCBINFO_TOKEN(pcbinfo);
2034 in_pcbremlocalgrphash_oncpu(inp, pcbinfo);
2037 head = &pcbinfo->wildcardhashbase[
2038 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
2040 LIST_FOREACH(ic, head, ic_list) {
2041 if (ic->ic_inp == inp)
2044 REL_PCBINFO_TOKEN(pcbinfo);
2045 return; /* not found! */
2048 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
2049 REL_PCBINFO_TOKEN(pcbinfo);
2050 kfree(ic, M_TEMP); /* deallocate container */
2054 * Remove PCB from wildcard hash table.
2057 in_pcbremwildcardhash(struct inpcb *inp)
2059 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2061 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
2062 ("not in correct netisr"));
2063 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
2065 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
2066 inp->inp_lgrpindex = -1;
2067 inp->inp_flags &= ~INP_WILDCARD;
2071 * Remove PCB from various lists.
2074 in_pcbremlists(struct inpcb *inp)
2076 if (inp->inp_lport) {
2077 struct inpcbportinfo *portinfo;
2078 struct inpcbport *phd;
2082 * inp->inp_portinfo is _not_ necessary same as
2083 * inp->inp_pcbinfo->portinfo.
2085 portinfo = inp->inp_portinfo;
2086 GET_PORT_TOKEN(portinfo);
2089 LIST_REMOVE(inp, inp_portlist);
2090 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2091 LIST_REMOVE(phd, phd_hash);
2095 REL_PORT_TOKEN(portinfo);
2097 if (inp->inp_flags & INP_WILDCARD) {
2098 in_pcbremwildcardhash(inp);
2099 } else if (inp->inp_flags & INP_CONNECTED) {
2100 in_pcbremconnhash(inp);
2103 if (inp->inp_flags & INP_ONLIST)
2108 prison_xinpcb(struct thread *td, struct inpcb *inp)
2112 if (td->td_proc == NULL)
2114 cr = td->td_proc->p_ucred;
2115 if (cr->cr_prison == NULL)
2117 if (inp->inp_socket && inp->inp_socket->so_cred &&
2118 inp->inp_socket->so_cred->cr_prison &&
2119 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
2125 in_pcblist_global(SYSCTL_HANDLER_ARGS)
2127 struct inpcbinfo *pcbinfo_arr = arg1;
2128 int pcbinfo_arrlen = arg2;
2129 struct inpcb *marker;
2133 KASSERT(pcbinfo_arrlen <= ncpus && pcbinfo_arrlen >= 1,
2134 ("invalid pcbinfo count %d", pcbinfo_arrlen));
2137 * The process of preparing the TCB list is too time-consuming and
2138 * resource-intensive to repeat twice on every request.
2141 if (req->oldptr == NULL) {
2142 for (cpu = 0; cpu < pcbinfo_arrlen; ++cpu)
2143 n += pcbinfo_arr[cpu].ipi_count;
2144 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
2148 if (req->newptr != NULL)
2151 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
2152 marker->inp_flags |= INP_PLACEMARKER;
2155 * OK, now we're committed to doing something. Re-fetch ipi_count
2156 * after obtaining the generation count.
2160 for (cpu = 0; cpu < pcbinfo_arrlen && error == 0; ++cpu) {
2161 struct inpcbinfo *pcbinfo = &pcbinfo_arr[cpu];
2166 lwkt_migratecpu(cpu);
2168 GET_PCBINFO_TOKEN(pcbinfo);
2170 n = pcbinfo->ipi_count;
2172 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
2174 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
2175 LIST_REMOVE(marker, inp_list);
2176 LIST_INSERT_AFTER(inp, marker, inp_list);
2178 if (inp->inp_flags & INP_PLACEMARKER)
2180 if (prison_xinpcb(req->td, inp))
2183 bzero(&xi, sizeof xi);
2184 xi.xi_len = sizeof xi;
2185 bcopy(inp, &xi.xi_inp, sizeof *inp);
2186 if (inp->inp_socket)
2187 sotoxsocket(inp->inp_socket, &xi.xi_socket);
2188 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
2192 LIST_REMOVE(marker, inp_list);
2194 REL_PCBINFO_TOKEN(pcbinfo);
2196 if (error == 0 && i < n) {
2197 bzero(&xi, sizeof xi);
2198 xi.xi_len = sizeof xi;
2200 error = SYSCTL_OUT(req, &xi, sizeof xi);
2208 lwkt_migratecpu(origcpu);
2209 kfree(marker, M_TEMP);
2214 in_pcblist_global_ncpus2(SYSCTL_HANDLER_ARGS)
2216 return in_pcblist_global(oidp, arg1, ncpus2, req);
2220 in_savefaddr(struct socket *so, const struct sockaddr *faddr)
2222 struct sockaddr_in *sin;
2224 KASSERT(faddr->sa_family == AF_INET,
2225 ("not AF_INET faddr %d", faddr->sa_family));
2227 sin = kmalloc(sizeof(*sin), M_SONAME, M_WAITOK | M_ZERO);
2228 sin->sin_family = AF_INET;
2229 sin->sin_len = sizeof(*sin);
2230 sin->sin_port = ((const struct sockaddr_in *)faddr)->sin_port;
2231 sin->sin_addr = ((const struct sockaddr_in *)faddr)->sin_addr;
2233 so->so_faddr = (struct sockaddr *)sin;
2237 in_pcbportinfo_init(struct inpcbportinfo *portinfo, int hashsize,
2238 boolean_t shared, u_short offset)
2240 memset(portinfo, 0, sizeof(*portinfo));
2242 portinfo->offset = offset;
2243 portinfo->lastport = offset;
2244 portinfo->lastlow = offset;
2245 portinfo->lasthi = offset;
2247 portinfo->porthashbase = hashinit(hashsize, M_PCB,
2248 &portinfo->porthashmask);
2251 portinfo->porttoken = kmalloc(sizeof(struct lwkt_token),
2253 lwkt_token_init(portinfo->porttoken, "porttoken");
2258 in_pcbportrange(u_short *hi0, u_short *lo0, u_short ofs, u_short step)
2268 hi = rounddown2(hi, step);
2273 lo = roundup2(lo, step);
2283 in_pcbglobalinit(void)
2287 in_pcbmarkers = kmalloc(ncpus * sizeof(struct inpcb), M_PCB,
2289 in_pcbcontainer_markers = kmalloc(ncpus * sizeof(struct inpcontainer),
2290 M_PCB, M_WAITOK | M_ZERO);
2292 for (cpu = 0; cpu < ncpus; ++cpu) {
2293 struct inpcontainer *ic = &in_pcbcontainer_markers[cpu];
2294 struct inpcb *marker = &in_pcbmarkers[cpu];
2296 marker->inp_flags |= INP_PLACEMARKER;
2297 ic->ic_inp = marker;
2302 in_pcbmarker(int cpuid)
2304 KASSERT(cpuid >= 0 && cpuid < ncpus, ("invalid cpuid %d", cpuid));
2305 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
2307 return &in_pcbmarkers[cpuid];
2310 struct inpcontainer *
2311 in_pcbcontainer_marker(int cpuid)
2313 KASSERT(cpuid >= 0 && cpuid < ncpus, ("invalid cpuid %d", cpuid));
2314 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
2316 return &in_pcbcontainer_markers[cpuid];
2320 in_pcbresetroute(struct inpcb *inp)
2322 struct route *ro = &inp->inp_route;
2324 if (ro->ro_rt != NULL)
2326 bzero(ro, sizeof(*ro));