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_gencnt = ++pcbinfo->ipi_gencnt;
289 inp->inp_pcbinfo = pcbinfo;
290 inp->inp_socket = so;
292 error = ipsec_init_policy(so, &inp->inp_sp);
299 if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
300 inp->inp_flags |= IN6P_IPV6_V6ONLY;
301 if (ip6_auto_flowlabel)
302 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
312 * Unlink a pcb with the intention of moving it to another cpu with a
313 * different pcbinfo. While unlinked nothing should attempt to dereference
314 * inp_pcbinfo, NULL it out so we assert if it does.
317 in_pcbunlink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
319 KASSERT(inp->inp_pcbinfo == pcbinfo, ("pcbinfo mismatch"));
320 KASSERT((inp->inp_flags & (INP_WILDCARD | INP_CONNECTED)) == 0,
324 inp->inp_pcbinfo = NULL;
328 * Relink a pcb into a new pcbinfo.
331 in_pcblink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
333 KASSERT(inp->inp_pcbinfo == NULL, ("has pcbinfo"));
334 KASSERT((inp->inp_flags & (INP_WILDCARD | INP_CONNECTED)) == 0,
337 inp->inp_pcbinfo = pcbinfo;
342 in_pcbsetlport(struct inpcb *inp, int wild, struct ucred *cred)
344 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
345 struct inpcbportinfo *portinfo;
346 u_short first, last, lport, step;
349 int portinfo_first, portinfo_idx;
351 inp->inp_flags |= INP_ANONPORT;
353 step = pcbinfo->portinfo_mask + 1;
354 portinfo_first = mycpuid & pcbinfo->portinfo_mask;
355 portinfo_idx = portinfo_first;
357 portinfo = &pcbinfo->portinfo[portinfo_idx];
359 if (inp->inp_flags & INP_HIGHPORT) {
360 first = ipport_hifirstauto; /* sysctl */
361 last = ipport_hilastauto;
362 lastport = &portinfo->lasthi;
363 } else if (inp->inp_flags & INP_LOWPORT) {
366 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
367 inp->inp_laddr.s_addr = INADDR_ANY;
370 first = ipport_lowfirstauto; /* 1023 */
371 last = ipport_lowlastauto; /* 600 */
372 lastport = &portinfo->lastlow;
374 first = ipport_firstauto; /* sysctl */
375 last = ipport_lastauto;
376 lastport = &portinfo->lastport;
380 * This has to be atomic. If the porthash is shared across multiple
381 * protocol threads (aka tcp) then the token must be held.
383 GET_PORT_TOKEN(portinfo);
386 * Simple check to ensure all ports are not used up causing
389 * We split the two cases (up and down) so that the direction
390 * is not being tested on each round of the loop.
396 in_pcbportrange(&first, &last, portinfo->offset, step);
397 count = (first - last) / step;
400 if (count-- < 0) { /* completely used? */
401 error = EADDRNOTAVAIL;
405 if (*lastport > first || *lastport < last)
407 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
409 lport = htons(*lastport);
410 } while (in_pcblookup_local(portinfo, inp->inp_laddr, lport,
416 in_pcbportrange(&last, &first, portinfo->offset, step);
417 count = (last - first) / step;
420 if (count-- < 0) { /* completely used? */
421 error = EADDRNOTAVAIL;
425 if (*lastport < first || *lastport > last)
427 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
429 lport = htons(*lastport);
430 } while (in_pcblookup_local(portinfo, inp->inp_laddr, lport,
433 inp->inp_lport = lport;
434 in_pcbinsporthash(portinfo, inp);
437 REL_PORT_TOKEN(portinfo);
440 /* Try next portinfo */
442 portinfo_idx &= pcbinfo->portinfo_mask;
443 if (portinfo_idx != portinfo_first)
445 inp->inp_laddr.s_addr = INADDR_ANY;
451 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
453 struct socket *so = inp->inp_socket;
454 struct sockaddr_in jsin;
455 struct ucred *cred = NULL;
458 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
459 return (EADDRNOTAVAIL);
460 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
461 return (EINVAL); /* already bound */
463 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
464 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
466 cred = td->td_proc->p_ucred;
469 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
470 struct inpcbinfo *pcbinfo;
471 struct inpcbportinfo *portinfo;
473 u_short lport, lport_ho;
474 int reuseport = (so->so_options & SO_REUSEPORT);
477 if (nam->sa_len != sizeof *sin)
481 * We should check the family, but old programs
482 * incorrectly fail to initialize it.
484 if (sin->sin_family != AF_INET)
485 return (EAFNOSUPPORT);
487 if (!prison_replace_wildcards(td, nam))
490 lport = sin->sin_port;
491 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
493 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
494 * allow complete duplication of binding if
495 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
496 * and a multicast address is bound on both
497 * new and duplicated sockets.
499 if (so->so_options & SO_REUSEADDR)
500 reuseport = SO_REUSEADDR | SO_REUSEPORT;
501 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
502 sin->sin_port = 0; /* yech... */
503 bzero(&sin->sin_zero, sizeof sin->sin_zero);
504 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
505 return (EADDRNOTAVAIL);
508 inp->inp_laddr = sin->sin_addr;
510 jsin.sin_family = AF_INET;
511 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
512 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
513 inp->inp_laddr.s_addr = INADDR_ANY;
516 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
519 /* Auto-select local port */
520 return in_pcbsetlport(inp, wild, cred);
522 lport_ho = ntohs(lport);
525 if (lport_ho < IPPORT_RESERVED && cred &&
527 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
528 inp->inp_laddr.s_addr = INADDR_ANY;
533 * Locate the proper portinfo based on lport
535 pcbinfo = inp->inp_pcbinfo;
537 &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
538 KKASSERT((lport_ho & pcbinfo->portinfo_mask) ==
542 * This has to be atomic. If the porthash is shared across
543 * multiple protocol threads (aka tcp) then the token must
546 GET_PORT_TOKEN(portinfo);
548 if (so->so_cred->cr_uid != 0 &&
549 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
550 t = in_pcblookup_local(portinfo, sin->sin_addr, lport,
551 INPLOOKUP_WILDCARD, cred);
553 (!in_nullhost(sin->sin_addr) ||
554 !in_nullhost(t->inp_laddr) ||
555 (t->inp_socket->so_options & SO_REUSEPORT) == 0) &&
556 (so->so_cred->cr_uid !=
557 t->inp_socket->so_cred->cr_uid)) {
559 if (!in_nullhost(sin->sin_addr) ||
560 !in_nullhost(t->inp_laddr) ||
561 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
564 inp->inp_laddr.s_addr = INADDR_ANY;
570 if (cred && !prison_replace_wildcards(td, nam)) {
571 inp->inp_laddr.s_addr = INADDR_ANY;
572 error = EADDRNOTAVAIL;
575 t = in_pcblookup_local(portinfo, sin->sin_addr, lport,
577 if (t && !(reuseport & t->inp_socket->so_options)) {
579 if (!in_nullhost(sin->sin_addr) ||
580 !in_nullhost(t->inp_laddr) ||
581 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
584 inp->inp_laddr.s_addr = INADDR_ANY;
589 inp->inp_lport = lport;
590 in_pcbinsporthash(portinfo, inp);
593 REL_PORT_TOKEN(portinfo);
596 jsin.sin_family = AF_INET;
597 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
598 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
599 inp->inp_laddr.s_addr = INADDR_ANY;
602 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
604 return in_pcbsetlport(inp, wild, cred);
608 static struct inpcb *
609 in_pcblookup_localremote(struct inpcbportinfo *portinfo, struct in_addr laddr,
610 u_short lport, struct in_addr faddr, u_short fport, struct ucred *cred)
613 struct inpcbporthead *porthash;
614 struct inpcbport *phd;
615 struct inpcb *match = NULL;
618 * If the porthashbase is shared across several cpus, it must
621 ASSERT_PORT_TOKEN_HELD(portinfo);
624 * Best fit PCB lookup.
626 * First see if this local port is in use by looking on the
629 porthash = &portinfo->porthashbase[
630 INP_PCBPORTHASH(lport, portinfo->porthashmask)];
631 LIST_FOREACH(phd, porthash, phd_hash) {
632 if (phd->phd_port == lport)
636 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
638 if ((inp->inp_vflag & INP_IPV4) == 0)
641 if (inp->inp_laddr.s_addr != INADDR_ANY &&
642 inp->inp_laddr.s_addr != laddr.s_addr)
645 if (inp->inp_faddr.s_addr != INADDR_ANY &&
646 inp->inp_faddr.s_addr != faddr.s_addr)
649 if (inp->inp_fport != 0 && inp->inp_fport != fport)
654 inp->inp_socket->so_cred->cr_prison) {
664 in_pcbbind_remote(struct inpcb *inp, const struct sockaddr *remote,
667 struct proc *p = td->td_proc;
668 unsigned short *lastport;
669 const struct sockaddr_in *sin = (const struct sockaddr_in *)remote;
670 struct sockaddr_in jsin;
671 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
672 struct inpcbportinfo *portinfo;
673 struct ucred *cred = NULL;
674 u_short first, last, lport, step;
675 int count, error, dup;
676 int portinfo_first, portinfo_idx;
678 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
679 return (EADDRNOTAVAIL);
681 KKASSERT(inp->inp_laddr.s_addr != INADDR_ANY);
682 if (inp->inp_lport != 0)
683 return (EINVAL); /* already bound */
688 jsin.sin_family = AF_INET;
689 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
690 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
691 inp->inp_laddr.s_addr = INADDR_ANY;
694 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
696 inp->inp_flags |= INP_ANONPORT;
698 step = pcbinfo->portinfo_mask + 1;
699 portinfo_first = mycpuid & pcbinfo->portinfo_mask;
700 portinfo_idx = portinfo_first;
702 portinfo = &pcbinfo->portinfo[portinfo_idx];
705 if (inp->inp_flags & INP_HIGHPORT) {
706 first = ipport_hifirstauto; /* sysctl */
707 last = ipport_hilastauto;
708 lastport = &portinfo->lasthi;
709 } else if (inp->inp_flags & INP_LOWPORT) {
712 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
713 inp->inp_laddr.s_addr = INADDR_ANY;
716 first = ipport_lowfirstauto; /* 1023 */
717 last = ipport_lowlastauto; /* 600 */
718 lastport = &portinfo->lastlow;
720 first = ipport_firstauto; /* sysctl */
721 last = ipport_lastauto;
722 lastport = &portinfo->lastport;
726 * This has to be atomic. If the porthash is shared across multiple
727 * protocol threads (aka tcp) then the token must be held.
729 GET_PORT_TOKEN(portinfo);
733 * Simple check to ensure all ports are not used up causing
736 * We split the two cases (up and down) so that the direction
737 * is not being tested on each round of the loop.
743 in_pcbportrange(&first, &last, portinfo->offset, step);
744 count = (first - last) / step;
747 if (count-- < 0) { /* completely used? */
748 error = EADDRNOTAVAIL;
752 if (*lastport > first || *lastport < last)
754 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
756 lport = htons(*lastport);
757 } while (in_pcblookup_localremote(portinfo, inp->inp_laddr,
758 lport, sin->sin_addr, sin->sin_port, cred));
763 in_pcbportrange(&last, &first, portinfo->offset, step);
764 count = (last - first) / step;
767 if (count-- < 0) { /* completely used? */
768 error = EADDRNOTAVAIL;
772 if (*lastport < first || *lastport > last)
774 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
776 lport = htons(*lastport);
777 } while (in_pcblookup_localremote(portinfo, inp->inp_laddr,
778 lport, sin->sin_addr, sin->sin_port, cred));
781 /* This could happen on loopback interface */
782 if (sin->sin_port == lport &&
783 sin->sin_addr.s_addr == inp->inp_laddr.s_addr) {
786 * Duplicate again; give up
788 error = EADDRNOTAVAIL;
794 inp->inp_lport = lport;
795 in_pcbinsporthash(portinfo, inp);
798 REL_PORT_TOKEN(portinfo);
801 /* Try next portinfo */
803 portinfo_idx &= pcbinfo->portinfo_mask;
804 if (portinfo_idx != portinfo_first)
806 inp->inp_laddr.s_addr = INADDR_ANY;
812 * Transform old in_pcbconnect() into an inner subroutine for new
813 * in_pcbconnect(): Do some validity-checking on the remote
814 * address (in mbuf 'nam') and then determine local host address
815 * (i.e., which interface) to use to access that remote host.
817 * This preserves definition of in_pcbconnect(), while supporting a
818 * slightly different version for T/TCP. (This is more than
819 * a bit of a kludge, but cleaning up the internal interfaces would
820 * have forced minor changes in every protocol).
823 in_pcbladdr_find(struct inpcb *inp, struct sockaddr *nam,
824 struct sockaddr_in **plocal_sin, struct thread *td, int find)
826 struct in_ifaddr *ia;
827 struct ucred *cred = NULL;
828 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
829 struct sockaddr *jsin;
830 int jailed = 0, alloc_route = 0;
832 if (nam->sa_len != sizeof *sin)
834 if (sin->sin_family != AF_INET)
835 return (EAFNOSUPPORT);
836 if (sin->sin_port == 0)
837 return (EADDRNOTAVAIL);
838 if (td && td->td_proc && td->td_proc->p_ucred)
839 cred = td->td_proc->p_ucred;
840 if (cred && cred->cr_prison)
842 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
843 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
845 * If the destination address is INADDR_ANY,
846 * use the primary local address.
847 * If the supplied address is INADDR_BROADCAST,
848 * and the primary interface supports broadcast,
849 * choose the broadcast address for that interface.
851 if (sin->sin_addr.s_addr == INADDR_ANY)
852 sin->sin_addr = IA_SIN(ia)->sin_addr;
853 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
854 (ia->ia_ifp->if_flags & IFF_BROADCAST))
855 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
862 * If route is known or can be allocated now,
863 * our src addr is taken from the i/f, else punt.
864 * Note that we should check the address family of the cached
865 * destination, in case of sharing the cache with IPv6.
867 ro = &inp->inp_route;
869 (!(ro->ro_rt->rt_flags & RTF_UP) ||
870 ro->ro_dst.sa_family != AF_INET ||
871 satosin(&ro->ro_dst)->sin_addr.s_addr !=
872 sin->sin_addr.s_addr ||
873 inp->inp_socket->so_options & SO_DONTROUTE)) {
877 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
878 (ro->ro_rt == NULL ||
879 ro->ro_rt->rt_ifp == NULL)) {
880 /* No route yet, so try to acquire one */
881 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
882 ro->ro_dst.sa_family = AF_INET;
883 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
884 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
890 * If we found a route, use the address
891 * corresponding to the outgoing interface
892 * unless it is the loopback (in case a route
893 * to our address on another net goes to loopback).
896 !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
898 if (jailed_ip(cred->cr_prison,
899 ro->ro_rt->rt_ifa->ifa_addr)) {
900 ia = ifatoia(ro->ro_rt->rt_ifa);
903 ia = ifatoia(ro->ro_rt->rt_ifa);
907 u_short fport = sin->sin_port;
910 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
911 if (ia && jailed && !jailed_ip(cred->cr_prison,
912 sintosa(&ia->ia_addr)))
915 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
916 if (ia && jailed && !jailed_ip(cred->cr_prison,
917 sintosa(&ia->ia_addr)))
919 sin->sin_port = fport;
921 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
922 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
923 if (ia && jailed && !jailed_ip(cred->cr_prison,
924 sintosa(&ia->ia_addr)))
927 if (!jailed && ia == NULL)
931 * If the destination address is multicast and an outgoing
932 * interface has been set as a multicast option, use the
933 * address of that interface as our source address.
935 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
936 inp->inp_moptions != NULL) {
937 struct ip_moptions *imo;
940 imo = inp->inp_moptions;
941 if (imo->imo_multicast_ifp != NULL) {
942 struct in_ifaddr_container *iac;
944 ifp = imo->imo_multicast_ifp;
947 &in_ifaddrheads[mycpuid], ia_link) {
948 if (iac->ia->ia_ifp == ifp) {
958 * Don't do pcblookup call here; return interface in plocal_sin
959 * and exit to caller, that will do the lookup.
961 if (ia == NULL && jailed) {
962 if ((jsin = prison_get_nonlocal(
963 cred->cr_prison, AF_INET, NULL)) != NULL ||
964 (jsin = prison_get_local(
965 cred->cr_prison, AF_INET, NULL)) != NULL) {
966 *plocal_sin = satosin(jsin);
972 *plocal_sin = &ia->ia_addr;
978 struct route *ro = &inp->inp_route;
980 if (ro->ro_rt != NULL)
982 bzero(ro, sizeof(*ro));
984 return (EADDRNOTAVAIL);
988 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
989 struct sockaddr_in **plocal_sin, struct thread *td)
991 return in_pcbladdr_find(inp, nam, plocal_sin, td,
992 (inp->inp_laddr.s_addr == INADDR_ANY));
997 * Connect from a socket to a specified address.
998 * Both address and port must be specified in argument sin.
999 * If don't have a local address for this socket yet,
1003 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
1005 struct sockaddr_in *if_sin;
1006 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1009 /* Call inner routine to assign local interface address. */
1010 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
1013 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
1014 inp->inp_laddr.s_addr ?
1015 inp->inp_laddr : if_sin->sin_addr,
1016 inp->inp_lport, FALSE, NULL) != NULL) {
1017 return (EADDRINUSE);
1019 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1020 if (inp->inp_lport == 0) {
1021 error = in_pcbbind(inp, NULL, td);
1025 inp->inp_laddr = if_sin->sin_addr;
1027 inp->inp_faddr = sin->sin_addr;
1028 inp->inp_fport = sin->sin_port;
1029 in_pcbinsconnhash(inp);
1034 in_pcbdisconnect(struct inpcb *inp)
1037 in_pcbremconnhash(inp);
1038 inp->inp_faddr.s_addr = INADDR_ANY;
1043 in_pcbdetach(struct inpcb *inp)
1045 struct socket *so = inp->inp_socket;
1046 struct inpcbinfo *ipi = inp->inp_pcbinfo;
1049 ipsec4_delete_pcbpolicy(inp);
1051 inp->inp_gencnt = ++ipi->ipi_gencnt;
1052 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
1053 in_pcbremlists(inp);
1055 sofree(so); /* remove pcb ref */
1056 if (inp->inp_options)
1057 m_free(inp->inp_options);
1058 if (inp->inp_route.ro_rt)
1059 rtfree(inp->inp_route.ro_rt);
1060 ip_freemoptions(inp->inp_moptions);
1066 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
1067 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
1068 * in struct pr_usrreqs, so that protocols can just reference then directly
1069 * without the need for a wrapper function. The socket must have a valid
1070 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
1071 * except through a kernel programming error, so it is acceptable to panic
1072 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
1073 * because there actually /is/ a programming error somewhere... XXX)
1076 in_setsockaddr(struct socket *so, struct sockaddr **nam)
1079 struct sockaddr_in *sin;
1082 * Do the malloc first in case it blocks.
1084 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1085 sin->sin_family = AF_INET;
1086 sin->sin_len = sizeof *sin;
1092 kfree(sin, M_SONAME);
1093 return (ECONNRESET);
1095 sin->sin_port = inp->inp_lport;
1096 sin->sin_addr = inp->inp_laddr;
1099 *nam = (struct sockaddr *)sin;
1104 in_setsockaddr_dispatch(netmsg_t msg)
1108 error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1109 lwkt_replymsg(&msg->lmsg, error);
1113 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
1116 struct sockaddr_in *sin;
1119 * Do the malloc first in case it blocks.
1121 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1122 sin->sin_family = AF_INET;
1123 sin->sin_len = sizeof *sin;
1129 kfree(sin, M_SONAME);
1130 return (ECONNRESET);
1132 sin->sin_port = inp->inp_fport;
1133 sin->sin_addr = inp->inp_faddr;
1136 *nam = (struct sockaddr *)sin;
1141 in_setpeeraddr_dispatch(netmsg_t msg)
1145 error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1146 lwkt_replymsg(&msg->lmsg, error);
1150 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int err,
1151 void (*notify)(struct inpcb *, int))
1153 struct inpcb *inp, *marker;
1155 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1156 ("not in the correct netisr"));
1157 marker = &in_pcbmarkers[mycpuid];
1161 * - If INP_PLACEMARKER is set we must ignore the rest of the
1162 * structure and skip it.
1163 * - It is safe to nuke inpcbs here, since we are in their own
1166 GET_PCBINFO_TOKEN(pcbinfo);
1168 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1169 while ((inp = LIST_NEXT(marker, inp_list)) != NULL) {
1170 LIST_REMOVE(marker, inp_list);
1171 LIST_INSERT_AFTER(inp, marker, inp_list);
1173 if (inp->inp_flags & INP_PLACEMARKER)
1176 if (!(inp->inp_vflag & INP_IPV4))
1179 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1180 inp->inp_socket == NULL)
1182 (*notify)(inp, err); /* can remove inp from list! */
1184 LIST_REMOVE(marker, inp_list);
1186 REL_PCBINFO_TOKEN(pcbinfo);
1190 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1192 struct inpcb *inp, *marker;
1195 * We only need to make sure that we are in netisr0, where all
1196 * multicast operation happen. We could check inpcbinfo which
1197 * does not belong to netisr0 by holding the inpcbinfo's token.
1198 * In this case, the pcbinfo must be able to be shared, i.e.
1199 * pcbinfo->infotoken is not NULL.
1201 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1202 ("not in netisr0"));
1203 KASSERT(pcbinfo->cpu == 0 || pcbinfo->infotoken != NULL,
1204 ("pcbinfo could not be shared"));
1207 * Get a marker for the current netisr (netisr0).
1209 * It is possible that the multicast address deletion blocks,
1210 * which could cause temporary token releasing. So we use
1211 * inpcb marker here to get a coherent view of the inpcb list.
1213 * While, on the other hand, moptions are only added and deleted
1214 * in netisr0, so we would not see staled moption or miss moption
1215 * even if the token was released due to the blocking multicast
1218 marker = &in_pcbmarkers[mycpuid];
1220 GET_PCBINFO_TOKEN(pcbinfo);
1222 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1223 while ((inp = LIST_NEXT(marker, inp_list)) != NULL) {
1224 struct ip_moptions *imo;
1226 LIST_REMOVE(marker, inp_list);
1227 LIST_INSERT_AFTER(inp, marker, inp_list);
1229 if (inp->inp_flags & INP_PLACEMARKER)
1231 imo = inp->inp_moptions;
1232 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
1236 * Unselect the outgoing interface if it is being
1239 if (imo->imo_multicast_ifp == ifp)
1240 imo->imo_multicast_ifp = NULL;
1243 * Drop multicast group membership if we joined
1244 * through the interface being detached.
1246 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1248 if (imo->imo_membership[i]->inm_ifp == ifp) {
1251 * This could block and the pcbinfo
1252 * token could be passively released.
1254 in_delmulti(imo->imo_membership[i]);
1256 } else if (gap != 0)
1257 imo->imo_membership[i - gap] =
1258 imo->imo_membership[i];
1260 imo->imo_num_memberships -= gap;
1263 LIST_REMOVE(marker, inp_list);
1265 REL_PCBINFO_TOKEN(pcbinfo);
1269 * Check for alternatives when higher level complains
1270 * about service problems. For now, invalidate cached
1271 * routing information. If the route was created dynamically
1272 * (by a redirect), time to try a default gateway again.
1275 in_losing(struct inpcb *inp)
1278 struct rt_addrinfo rtinfo;
1280 if ((rt = inp->inp_route.ro_rt)) {
1281 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1282 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1283 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1284 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1285 rtinfo.rti_flags = rt->rt_flags;
1286 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1287 if (rt->rt_flags & RTF_DYNAMIC) {
1288 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1289 rt_mask(rt), rt->rt_flags, NULL);
1291 inp->inp_route.ro_rt = NULL;
1294 * A new route can be allocated
1295 * the next time output is attempted.
1301 * After a routing change, flush old routing
1302 * and allocate a (hopefully) better one.
1305 in_rtchange(struct inpcb *inp, int err)
1307 if (inp->inp_route.ro_rt) {
1308 rtfree(inp->inp_route.ro_rt);
1309 inp->inp_route.ro_rt = NULL;
1311 * A new route can be allocated the next time
1312 * output is attempted.
1318 * Lookup a PCB based on the local address and port.
1321 in_pcblookup_local(struct inpcbportinfo *portinfo, struct in_addr laddr,
1322 u_int lport_arg, int wild_okay, struct ucred *cred)
1325 int matchwild = 3, wildcard;
1326 u_short lport = lport_arg;
1327 struct inpcbporthead *porthash;
1328 struct inpcbport *phd;
1329 struct inpcb *match = NULL;
1332 * If the porthashbase is shared across several cpus, it must
1335 ASSERT_PORT_TOKEN_HELD(portinfo);
1338 * Best fit PCB lookup.
1340 * First see if this local port is in use by looking on the
1343 porthash = &portinfo->porthashbase[
1344 INP_PCBPORTHASH(lport, portinfo->porthashmask)];
1345 LIST_FOREACH(phd, porthash, phd_hash) {
1346 if (phd->phd_port == lport)
1351 * Port is in use by one or more PCBs. Look for best
1354 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1357 if ((inp->inp_vflag & INP_IPV4) == 0)
1360 if (inp->inp_faddr.s_addr != INADDR_ANY)
1362 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1363 if (laddr.s_addr == INADDR_ANY)
1365 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1368 if (laddr.s_addr != INADDR_ANY)
1371 if (wildcard && !wild_okay)
1373 if (wildcard < matchwild &&
1376 inp->inp_socket->so_cred->cr_prison)) {
1378 matchwild = wildcard;
1379 if (matchwild == 0) {
1389 in_pcblocalgroup_last(const struct inpcbinfo *pcbinfo,
1390 const struct inpcb *inp)
1392 const struct inp_localgrphead *hdr;
1393 const struct inp_localgroup *grp;
1396 if (pcbinfo->localgrphashbase == NULL)
1399 GET_PCBINFO_TOKEN(pcbinfo);
1401 hdr = &pcbinfo->localgrphashbase[
1402 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1404 LIST_FOREACH(grp, hdr, il_list) {
1405 if (grp->il_vflag == inp->inp_vflag &&
1406 grp->il_lport == inp->inp_lport &&
1407 memcmp(&grp->il_dependladdr,
1408 &inp->inp_inc.inc_ie.ie_dependladdr,
1409 sizeof(grp->il_dependladdr)) == 0) {
1413 if (grp == NULL || grp->il_inpcnt == 1) {
1414 REL_PCBINFO_TOKEN(pcbinfo);
1418 KASSERT(grp->il_inpcnt >= 2,
1419 ("invalid localgroup inp count %d", grp->il_inpcnt));
1420 for (i = 0; i < grp->il_inpcnt; ++i) {
1421 if (grp->il_inp[i] == inp) {
1422 int last = grp->il_inpcnt - 1;
1425 last = grp->il_inpcnt - 2;
1426 REL_PCBINFO_TOKEN(pcbinfo);
1427 return grp->il_inp[last];
1430 REL_PCBINFO_TOKEN(pcbinfo);
1434 static struct inpcb *
1435 inp_localgroup_lookup(const struct inpcbinfo *pcbinfo,
1436 struct in_addr laddr, uint16_t lport, uint32_t pkt_hash)
1438 struct inpcb *local_wild = NULL;
1439 const struct inp_localgrphead *hdr;
1440 const struct inp_localgroup *grp;
1442 ASSERT_PCBINFO_TOKEN_HELD(pcbinfo);
1444 hdr = &pcbinfo->localgrphashbase[
1445 INP_PCBLOCALGRPHASH(lport, pcbinfo->localgrphashmask)];
1448 * Order of socket selection:
1453 * - Local group does not contain jailed sockets
1454 * - Local group does not contain IPv4 mapped INET6 wild sockets
1456 LIST_FOREACH(grp, hdr, il_list) {
1458 if (!(grp->il_vflag & INP_IPV4))
1461 if (grp->il_lport == lport) {
1465 * Modulo-N is used here, which greatly reduces
1466 * completion queue token contention, thus more
1467 * cpu time is saved.
1469 idx = pkt_hash % grp->il_inpcnt;
1470 if (grp->il_laddr.s_addr == laddr.s_addr)
1471 return grp->il_inp[idx];
1472 else if (grp->il_laddr.s_addr == INADDR_ANY)
1473 local_wild = grp->il_inp[idx];
1476 if (local_wild != NULL)
1482 * Lookup PCB in hash list.
1485 in_pcblookup_pkthash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1486 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1487 boolean_t wildcard, struct ifnet *ifp, const struct mbuf *m)
1489 struct inpcbhead *head;
1490 struct inpcb *inp, *jinp=NULL;
1491 u_short fport = fport_arg, lport = lport_arg;
1494 * First look for an exact match.
1496 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1497 laddr.s_addr, lport, pcbinfo->hashmask)];
1498 LIST_FOREACH(inp, head, inp_hash) {
1500 if (!(inp->inp_vflag & INP_IPV4))
1503 if (in_hosteq(inp->inp_faddr, faddr) &&
1504 in_hosteq(inp->inp_laddr, laddr) &&
1505 inp->inp_fport == fport && inp->inp_lport == lport) {
1507 if (inp->inp_socket == NULL ||
1508 inp->inp_socket->so_cred->cr_prison == NULL) {
1520 struct inpcb *local_wild = NULL;
1521 struct inpcb *jinp_wild = NULL;
1523 struct inpcb *local_wild_mapped = NULL;
1525 struct inpcontainer *ic;
1526 struct inpcontainerhead *chead;
1527 struct sockaddr_in jsin;
1530 GET_PCBINFO_TOKEN(pcbinfo);
1533 * Check local group first
1535 if (pcbinfo->localgrphashbase != NULL &&
1536 m != NULL && (m->m_flags & M_HASH) &&
1537 !(ifp && ifp->if_type == IFT_FAITH)) {
1538 inp = inp_localgroup_lookup(pcbinfo,
1539 laddr, lport, m->m_pkthdr.hash);
1541 REL_PCBINFO_TOKEN(pcbinfo);
1547 * Order of socket selection:
1548 * 1. non-jailed, non-wild.
1549 * 2. non-jailed, wild.
1550 * 3. jailed, non-wild.
1553 jsin.sin_family = AF_INET;
1554 chead = &pcbinfo->wildcardhashbase[
1555 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1556 LIST_FOREACH(ic, chead, ic_list) {
1558 if (inp->inp_flags & INP_PLACEMARKER)
1561 jsin.sin_addr.s_addr = laddr.s_addr;
1563 if (!(inp->inp_vflag & INP_IPV4))
1566 if (inp->inp_socket != NULL)
1567 cred = inp->inp_socket->so_cred;
1570 if (cred != NULL && jailed(cred)) {
1574 if (!jailed_ip(cred->cr_prison,
1575 (struct sockaddr *)&jsin))
1578 if (inp->inp_lport == lport) {
1579 if (ifp && ifp->if_type == IFT_FAITH &&
1580 !(inp->inp_flags & INP_FAITH))
1582 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1583 if (cred != NULL && jailed(cred)) {
1586 REL_PCBINFO_TOKEN(pcbinfo);
1590 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1592 if (INP_CHECK_SOCKAF(inp->inp_socket,
1594 local_wild_mapped = inp;
1606 REL_PCBINFO_TOKEN(pcbinfo);
1608 if (local_wild != NULL)
1609 return (local_wild);
1611 if (local_wild_mapped != NULL)
1612 return (local_wild_mapped);
1626 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1627 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1628 boolean_t wildcard, struct ifnet *ifp)
1630 return in_pcblookup_pkthash(pcbinfo, faddr, fport_arg,
1631 laddr, lport_arg, wildcard, ifp, NULL);
1635 * Insert PCB into connection hash table.
1638 in_pcbinsconnhash(struct inpcb *inp)
1640 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1641 struct inpcbhead *bucket;
1642 u_int32_t hashkey_faddr, hashkey_laddr;
1645 if (inp->inp_vflag & INP_IPV6) {
1646 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1647 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1650 hashkey_faddr = inp->inp_faddr.s_addr;
1651 hashkey_laddr = inp->inp_laddr.s_addr;
1656 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1657 ("not in the correct netisr"));
1658 KASSERT(!(inp->inp_flags & INP_WILDCARD), ("already on wildcardhash"));
1659 KASSERT(!(inp->inp_flags & INP_CONNECTED), ("already on connhash"));
1660 inp->inp_flags |= INP_CONNECTED;
1663 * Insert into the connection hash table.
1665 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1666 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1667 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1671 * Remove PCB from connection hash table.
1674 in_pcbremconnhash(struct inpcb *inp)
1676 struct inpcbinfo *pcbinfo __debugvar = inp->inp_pcbinfo;
1678 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1679 ("not in the correct netisr"));
1680 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1682 LIST_REMOVE(inp, inp_hash);
1683 inp->inp_flags &= ~INP_CONNECTED;
1687 * Insert PCB into port hash table.
1690 in_pcbinsporthash(struct inpcbportinfo *portinfo, struct inpcb *inp)
1692 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1693 struct inpcbporthead *pcbporthash;
1694 struct inpcbport *phd;
1697 * If the porthashbase is shared across several cpus, it must
1700 ASSERT_PORT_TOKEN_HELD(portinfo);
1703 * Insert into the port hash table.
1705 pcbporthash = &portinfo->porthashbase[
1706 INP_PCBPORTHASH(inp->inp_lport, portinfo->porthashmask)];
1708 /* Go through port list and look for a head for this lport. */
1709 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1710 if (phd->phd_port == inp->inp_lport)
1714 /* If none exists, use saved one and tack it on. */
1716 KKASSERT(pcbinfo->portsave != NULL);
1717 phd = pcbinfo->portsave;
1718 pcbinfo->portsave = NULL;
1719 phd->phd_port = inp->inp_lport;
1720 LIST_INIT(&phd->phd_pcblist);
1721 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1724 inp->inp_portinfo = portinfo;
1726 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1729 * Malloc one inpcbport for later use. It is safe to use
1730 * "wait" malloc here (port token would be released, if
1731 * malloc ever blocked), since all changes to the porthash
1734 if (pcbinfo->portsave == NULL) {
1735 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1736 M_PCB, M_INTWAIT | M_ZERO);
1741 in_pcbinsporthash_lport(struct inpcb *inp)
1743 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1744 struct inpcbportinfo *portinfo;
1747 /* Locate the proper portinfo based on lport */
1748 lport_ho = ntohs(inp->inp_lport);
1749 portinfo = &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
1750 KKASSERT((lport_ho & pcbinfo->portinfo_mask) == portinfo->offset);
1752 GET_PORT_TOKEN(portinfo);
1753 in_pcbinsporthash(portinfo, inp);
1754 REL_PORT_TOKEN(portinfo);
1757 static struct inp_localgroup *
1758 inp_localgroup_alloc(u_char vflag,
1759 uint16_t port, const union in_dependaddr *addr, int size)
1761 struct inp_localgroup *grp;
1763 grp = kmalloc(__offsetof(struct inp_localgroup, il_inp[size]),
1764 M_TEMP, M_INTWAIT | M_ZERO);
1765 grp->il_vflag = vflag;
1766 grp->il_lport = port;
1767 grp->il_dependladdr = *addr;
1768 grp->il_inpsiz = size;
1774 inp_localgroup_free(struct inp_localgroup *grp)
1780 inp_localgroup_destroy(struct inp_localgroup *grp)
1782 LIST_REMOVE(grp, il_list);
1783 inp_localgroup_free(grp);
1787 inp_localgroup_copy(struct inp_localgroup *grp,
1788 const struct inp_localgroup *old_grp)
1792 KASSERT(old_grp->il_inpcnt < grp->il_inpsiz,
1793 ("invalid new local group size %d and old local group count %d",
1794 grp->il_inpsiz, old_grp->il_inpcnt));
1795 for (i = 0; i < old_grp->il_inpcnt; ++i)
1796 grp->il_inp[i] = old_grp->il_inp[i];
1797 grp->il_inpcnt = old_grp->il_inpcnt;
1801 in_pcbinslocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1803 struct inp_localgrphead *hdr;
1804 struct inp_localgroup *grp, *grp_alloc = NULL;
1807 ASSERT_PCBINFO_TOKEN_HELD(pcbinfo);
1809 if (pcbinfo->localgrphashbase == NULL)
1813 * XXX don't allow jailed socket to join local group
1815 if (inp->inp_socket != NULL)
1816 cred = inp->inp_socket->so_cred;
1819 if (cred != NULL && jailed(cred))
1824 * XXX don't allow IPv4 mapped INET6 wild socket
1826 if ((inp->inp_vflag & INP_IPV4) &&
1827 inp->inp_laddr.s_addr == INADDR_ANY &&
1828 INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6))
1832 hdr = &pcbinfo->localgrphashbase[
1833 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1836 LIST_FOREACH(grp, hdr, il_list) {
1837 if (grp->il_vflag == inp->inp_vflag &&
1838 grp->il_lport == inp->inp_lport &&
1839 memcmp(&grp->il_dependladdr,
1840 &inp->inp_inc.inc_ie.ie_dependladdr,
1841 sizeof(grp->il_dependladdr)) == 0) {
1847 * Create a new local group
1849 if (grp_alloc == NULL) {
1850 grp_alloc = inp_localgroup_alloc(inp->inp_vflag,
1851 inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr,
1852 INP_LOCALGROUP_SIZMIN);
1854 * Local group allocation could block and the
1855 * local group w/ the same property might have
1856 * been added by others when we were blocked;
1861 /* Local group has been allocated; link it */
1864 LIST_INSERT_HEAD(hdr, grp, il_list);
1866 } else if (grp->il_inpcnt == grp->il_inpsiz) {
1867 if (grp->il_inpsiz >= INP_LOCALGROUP_SIZMAX) {
1868 static int limit_logged = 0;
1870 if (!limit_logged) {
1872 kprintf("local group port %d, "
1873 "limit reached\n", ntohs(grp->il_lport));
1875 if (grp_alloc != NULL) {
1877 * This would happen if the local group
1878 * w/ the same property was expanded when
1879 * our local group allocation blocked.
1881 inp_localgroup_free(grp_alloc);
1887 * Expand this local group
1889 if (grp_alloc == NULL ||
1890 grp->il_inpcnt >= grp_alloc->il_inpsiz) {
1891 if (grp_alloc != NULL)
1892 inp_localgroup_free(grp_alloc);
1893 grp_alloc = inp_localgroup_alloc(grp->il_vflag,
1894 grp->il_lport, &grp->il_dependladdr,
1895 grp->il_inpsiz * 2);
1897 * Local group allocation could block and the
1898 * local group w/ the same property might have
1899 * been expanded by others when we were blocked;
1906 * Save the old local group, link the new one, and then
1907 * destroy the old local group
1909 inp_localgroup_copy(grp_alloc, grp);
1910 LIST_INSERT_HEAD(hdr, grp_alloc, il_list);
1911 inp_localgroup_destroy(grp);
1917 * Found the local group
1919 if (grp_alloc != NULL) {
1921 * This would happen if the local group w/ the
1922 * same property was added or expanded when our
1923 * local group allocation blocked.
1925 inp_localgroup_free(grp_alloc);
1930 KASSERT(grp->il_inpcnt < grp->il_inpsiz,
1931 ("invalid local group size %d and count %d",
1932 grp->il_inpsiz, grp->il_inpcnt));
1933 grp->il_inp[grp->il_inpcnt] = inp;
1938 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1940 struct inpcontainer *ic;
1941 struct inpcontainerhead *bucket;
1943 GET_PCBINFO_TOKEN(pcbinfo);
1945 in_pcbinslocalgrphash_oncpu(inp, pcbinfo);
1947 bucket = &pcbinfo->wildcardhashbase[
1948 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1950 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1952 LIST_INSERT_HEAD(bucket, ic, ic_list);
1954 REL_PCBINFO_TOKEN(pcbinfo);
1958 * Insert PCB into wildcard hash table.
1961 in_pcbinswildcardhash(struct inpcb *inp)
1963 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1965 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1966 ("not in correct netisr"));
1967 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1968 ("already on connhash"));
1969 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1970 ("already on wildcardhash"));
1971 inp->inp_flags |= INP_WILDCARD;
1973 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1977 in_pcbremlocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1979 struct inp_localgrphead *hdr;
1980 struct inp_localgroup *grp;
1982 ASSERT_PCBINFO_TOKEN_HELD(pcbinfo);
1984 if (pcbinfo->localgrphashbase == NULL)
1987 hdr = &pcbinfo->localgrphashbase[
1988 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1990 LIST_FOREACH(grp, hdr, il_list) {
1993 for (i = 0; i < grp->il_inpcnt; ++i) {
1994 if (grp->il_inp[i] != inp)
1997 if (grp->il_inpcnt == 1) {
1998 /* Destroy this local group */
1999 inp_localgroup_destroy(grp);
2001 /* Pull up inpcbs */
2002 for (; i + 1 < grp->il_inpcnt; ++i)
2003 grp->il_inp[i] = grp->il_inp[i + 1];
2012 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
2014 struct inpcontainer *ic;
2015 struct inpcontainerhead *head;
2017 GET_PCBINFO_TOKEN(pcbinfo);
2019 in_pcbremlocalgrphash_oncpu(inp, pcbinfo);
2022 head = &pcbinfo->wildcardhashbase[
2023 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
2025 LIST_FOREACH(ic, head, ic_list) {
2026 if (ic->ic_inp == inp)
2029 REL_PCBINFO_TOKEN(pcbinfo);
2030 return; /* not found! */
2033 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
2034 REL_PCBINFO_TOKEN(pcbinfo);
2035 kfree(ic, M_TEMP); /* deallocate container */
2039 * Remove PCB from wildcard hash table.
2042 in_pcbremwildcardhash(struct inpcb *inp)
2044 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2046 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
2047 ("not in correct netisr"));
2048 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
2050 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
2051 inp->inp_flags &= ~INP_WILDCARD;
2055 * Remove PCB from various lists.
2058 in_pcbremlists(struct inpcb *inp)
2060 if (inp->inp_lport) {
2061 struct inpcbportinfo *portinfo;
2062 struct inpcbport *phd;
2066 * inp->inp_portinfo is _not_ necessary same as
2067 * inp->inp_pcbinfo->portinfo.
2069 portinfo = inp->inp_portinfo;
2070 GET_PORT_TOKEN(portinfo);
2073 LIST_REMOVE(inp, inp_portlist);
2074 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2075 LIST_REMOVE(phd, phd_hash);
2079 REL_PORT_TOKEN(portinfo);
2081 if (inp->inp_flags & INP_WILDCARD) {
2082 in_pcbremwildcardhash(inp);
2083 } else if (inp->inp_flags & INP_CONNECTED) {
2084 in_pcbremconnhash(inp);
2087 if (inp->inp_flags & INP_ONLIST)
2092 prison_xinpcb(struct thread *td, struct inpcb *inp)
2096 if (td->td_proc == NULL)
2098 cr = td->td_proc->p_ucred;
2099 if (cr->cr_prison == NULL)
2101 if (inp->inp_socket && inp->inp_socket->so_cred &&
2102 inp->inp_socket->so_cred->cr_prison &&
2103 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
2109 in_pcblist_global(SYSCTL_HANDLER_ARGS)
2111 struct inpcbinfo *pcbinfo_arr = arg1;
2112 int pcbinfo_arrlen = arg2;
2113 struct inpcb *marker;
2117 KASSERT(pcbinfo_arrlen <= ncpus && pcbinfo_arrlen >= 1,
2118 ("invalid pcbinfo count %d", pcbinfo_arrlen));
2121 * The process of preparing the TCB list is too time-consuming and
2122 * resource-intensive to repeat twice on every request.
2125 if (req->oldptr == NULL) {
2126 for (cpu = 0; cpu < pcbinfo_arrlen; ++cpu)
2127 n += pcbinfo_arr[cpu].ipi_count;
2128 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
2132 if (req->newptr != NULL)
2135 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
2136 marker->inp_flags |= INP_PLACEMARKER;
2139 * OK, now we're committed to doing something. Re-fetch ipi_count
2140 * after obtaining the generation count.
2144 for (cpu = 0; cpu < pcbinfo_arrlen && error == 0; ++cpu) {
2145 struct inpcbinfo *pcbinfo = &pcbinfo_arr[cpu];
2150 lwkt_migratecpu(cpu);
2152 GET_PCBINFO_TOKEN(pcbinfo);
2154 n = pcbinfo->ipi_count;
2156 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
2158 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
2159 LIST_REMOVE(marker, inp_list);
2160 LIST_INSERT_AFTER(inp, marker, inp_list);
2162 if (inp->inp_flags & INP_PLACEMARKER)
2164 if (prison_xinpcb(req->td, inp))
2167 bzero(&xi, sizeof xi);
2168 xi.xi_len = sizeof xi;
2169 bcopy(inp, &xi.xi_inp, sizeof *inp);
2170 if (inp->inp_socket)
2171 sotoxsocket(inp->inp_socket, &xi.xi_socket);
2172 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
2176 LIST_REMOVE(marker, inp_list);
2178 REL_PCBINFO_TOKEN(pcbinfo);
2180 if (error == 0 && i < n) {
2181 bzero(&xi, sizeof xi);
2182 xi.xi_len = sizeof xi;
2184 error = SYSCTL_OUT(req, &xi, sizeof xi);
2192 lwkt_migratecpu(origcpu);
2193 kfree(marker, M_TEMP);
2198 in_pcblist_global_ncpus2(SYSCTL_HANDLER_ARGS)
2200 return in_pcblist_global(oidp, arg1, ncpus2, req);
2204 in_savefaddr(struct socket *so, const struct sockaddr *faddr)
2206 struct sockaddr_in *sin;
2208 KASSERT(faddr->sa_family == AF_INET,
2209 ("not AF_INET faddr %d", faddr->sa_family));
2211 sin = kmalloc(sizeof(*sin), M_SONAME, M_WAITOK | M_ZERO);
2212 sin->sin_family = AF_INET;
2213 sin->sin_len = sizeof(*sin);
2214 sin->sin_port = ((const struct sockaddr_in *)faddr)->sin_port;
2215 sin->sin_addr = ((const struct sockaddr_in *)faddr)->sin_addr;
2217 so->so_faddr = (struct sockaddr *)sin;
2221 in_pcbportinfo_init(struct inpcbportinfo *portinfo, int hashsize,
2222 boolean_t shared, u_short offset)
2224 memset(portinfo, 0, sizeof(*portinfo));
2226 portinfo->offset = offset;
2227 portinfo->lastport = offset;
2228 portinfo->lastlow = offset;
2229 portinfo->lasthi = offset;
2231 portinfo->porthashbase = hashinit(hashsize, M_PCB,
2232 &portinfo->porthashmask);
2235 portinfo->porttoken = kmalloc(sizeof(struct lwkt_token),
2237 lwkt_token_init(portinfo->porttoken, "porttoken");
2242 in_pcbportrange(u_short *hi0, u_short *lo0, u_short ofs, u_short step)
2252 hi = rounddown2(hi, step);
2257 lo = roundup2(lo, step);
2267 in_pcbglobalinit(void)
2271 in_pcbmarkers = kmalloc(ncpus * sizeof(struct inpcb), M_PCB,
2273 in_pcbcontainer_markers = kmalloc(ncpus * sizeof(struct inpcontainer),
2274 M_PCB, M_WAITOK | M_ZERO);
2276 for (cpu = 0; cpu < ncpus; ++cpu) {
2277 struct inpcontainer *ic = &in_pcbcontainer_markers[cpu];
2278 struct inpcb *marker = &in_pcbmarkers[cpu];
2280 marker->inp_flags |= INP_PLACEMARKER;
2281 ic->ic_inp = marker;
2286 in_pcbmarker(int cpuid)
2288 KASSERT(cpuid >= 0 && cpuid < ncpus, ("invalid cpuid %d", cpuid));
2289 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
2291 return &in_pcbmarkers[cpuid];
2294 struct inpcontainer *
2295 in_pcbcontainer_marker(int cpuid)
2297 KASSERT(cpuid >= 0 && cpuid < ncpus, ("invalid cpuid %d", cpuid));
2298 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
2300 return &in_pcbcontainer_markers[cpuid];