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>
93 #include <netinet/in.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip_var.h>
98 #include <netinet/ip6.h>
99 #include <netinet6/ip6_var.h>
103 #include <netinet6/ipsec.h>
104 #include <netproto/key/key.h>
105 #include <netproto/ipsec/esp_var.h>
109 #if defined(IPSEC) || defined(IPSEC_ESP)
110 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
113 #include <netproto/ipsec/ipsec.h>
114 #include <netproto/ipsec/key.h>
116 #endif /* FAST_IPSEC */
118 #define INP_LOCALGROUP_SIZMIN 8
119 #define INP_LOCALGROUP_SIZMAX 256
121 struct in_addr zeroin_addr;
124 * These configure the range of local port addresses assigned to
125 * "unspecified" outgoing connections/packets/whatever.
127 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
128 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
130 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
131 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
133 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
134 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
136 #define RANGECHK(var, min, max) \
137 if ((var) < (min)) { (var) = (min); } \
138 else if ((var) > (max)) { (var) = (max); }
140 int udpencap_enable = 1; /* enabled by default */
141 int udpencap_port = 4500; /* triggers decapsulation */
144 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
148 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
150 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
151 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
153 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
154 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
156 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
157 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
164 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
166 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
167 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
168 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
169 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
170 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
171 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
172 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
173 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
174 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
175 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
176 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
177 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
180 * in_pcb.c: manage the Protocol Control Blocks.
182 * NOTE: It is assumed that most of these functions will be called from
183 * a critical section. XXX - There are, unfortunately, a few exceptions
184 * to this rule that should be fixed.
186 * NOTE: The caller should initialize the cpu field to the cpu running the
187 * protocol stack associated with this inpcbinfo.
191 in_pcbinfo_init(struct inpcbinfo *pcbinfo)
193 LIST_INIT(&pcbinfo->pcblisthead);
195 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave), M_PCB,
199 struct baddynamicports baddynamicports;
202 * Check if the specified port is invalid for dynamic allocation.
205 in_baddynamic(u_int16_t port, u_int16_t proto)
209 return (DP_ISSET(baddynamicports.tcp, port));
212 /* Cannot preset this as it is a sysctl */
213 if (port == udpencap_port)
216 return (DP_ISSET(baddynamicports.udp, port));
224 * Allocate a PCB and associate it with the socket.
227 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
234 inp = kmalloc(pcbinfo->ipi_size, M_PCB, M_WAITOK|M_ZERO|M_NULLOK);
237 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
238 inp->inp_pcbinfo = inp->inp_cpcbinfo = pcbinfo;
239 inp->inp_socket = so;
241 error = ipsec_init_policy(so, &inp->inp_sp);
248 if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
249 inp->inp_flags |= IN6P_IPV6_V6ONLY;
250 if (ip6_auto_flowlabel)
251 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
255 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
256 pcbinfo->ipi_count++;
261 * Unlink a pcb with the intention of moving it to another cpu with a
262 * different pcbinfo. While unlinked nothing should attempt to dereference
263 * inp_pcbinfo, NULL it out so we assert if it does.
266 in_pcbunlink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
268 KASSERT(inp->inp_pcbinfo == pcbinfo, ("pcbinfo mismatch"));
269 KASSERT(inp->inp_cpcbinfo == pcbinfo, ("cpcbinfo mismatch"));
270 KASSERT((inp->inp_flags & (INP_WILDCARD | INP_CONNECTED)) == 0,
273 LIST_REMOVE(inp, inp_list);
274 pcbinfo->ipi_count--;
275 inp->inp_pcbinfo = NULL;
276 inp->inp_cpcbinfo = NULL;
280 * Relink a pcb into a new pcbinfo.
283 in_pcblink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
285 KASSERT(inp->inp_pcbinfo == NULL, ("has pcbinfo"));
286 KASSERT(inp->inp_cpcbinfo == NULL, ("has cpcbinfo"));
287 KASSERT((inp->inp_flags & (INP_WILDCARD | INP_CONNECTED)) == 0,
290 inp->inp_cpcbinfo = pcbinfo;
291 inp->inp_pcbinfo = pcbinfo;
292 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
293 pcbinfo->ipi_count++;
297 in_pcbsetlport(struct inpcb *inp, int wild, struct ucred *cred)
299 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
300 struct inpcbportinfo *portinfo;
301 u_short first, last, lport, step;
304 int portinfo_first, portinfo_idx;
306 inp->inp_flags |= INP_ANONPORT;
308 step = pcbinfo->portinfo_mask + 1;
309 portinfo_first = mycpuid & pcbinfo->portinfo_mask;
310 portinfo_idx = portinfo_first;
312 portinfo = &pcbinfo->portinfo[portinfo_idx];
314 if (inp->inp_flags & INP_HIGHPORT) {
315 first = ipport_hifirstauto; /* sysctl */
316 last = ipport_hilastauto;
317 lastport = &portinfo->lasthi;
318 } else if (inp->inp_flags & INP_LOWPORT) {
321 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
322 inp->inp_laddr.s_addr = INADDR_ANY;
325 first = ipport_lowfirstauto; /* 1023 */
326 last = ipport_lowlastauto; /* 600 */
327 lastport = &portinfo->lastlow;
329 first = ipport_firstauto; /* sysctl */
330 last = ipport_lastauto;
331 lastport = &portinfo->lastport;
335 * This has to be atomic. If the porthash is shared across multiple
336 * protocol threads (aka tcp) then the token must be held.
338 GET_PORT_TOKEN(portinfo);
341 * Simple check to ensure all ports are not used up causing
344 * We split the two cases (up and down) so that the direction
345 * is not being tested on each round of the loop.
351 in_pcbportrange(&first, &last, portinfo->offset, step);
352 count = (first - last) / step;
355 if (count-- < 0) { /* completely used? */
356 error = EADDRNOTAVAIL;
360 if (*lastport > first || *lastport < last)
362 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
364 lport = htons(*lastport);
365 } while (in_pcblookup_local(portinfo, inp->inp_laddr, lport,
371 in_pcbportrange(&last, &first, portinfo->offset, step);
372 count = (last - first) / step;
375 if (count-- < 0) { /* completely used? */
376 error = EADDRNOTAVAIL;
380 if (*lastport < first || *lastport > last)
382 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
384 lport = htons(*lastport);
385 } while (in_pcblookup_local(portinfo, inp->inp_laddr, lport,
388 inp->inp_lport = lport;
389 in_pcbinsporthash(portinfo, inp);
392 REL_PORT_TOKEN(portinfo);
395 /* Try next portinfo */
397 portinfo_idx &= pcbinfo->portinfo_mask;
398 if (portinfo_idx != portinfo_first)
400 inp->inp_laddr.s_addr = INADDR_ANY;
406 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
408 struct socket *so = inp->inp_socket;
409 struct sockaddr_in jsin;
410 struct ucred *cred = NULL;
413 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
414 return (EADDRNOTAVAIL);
415 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
416 return (EINVAL); /* already bound */
418 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
419 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
421 cred = td->td_proc->p_ucred;
424 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
425 struct inpcbinfo *pcbinfo;
426 struct inpcbportinfo *portinfo;
428 u_short lport, lport_ho;
429 int reuseport = (so->so_options & SO_REUSEPORT);
432 if (nam->sa_len != sizeof *sin)
436 * We should check the family, but old programs
437 * incorrectly fail to initialize it.
439 if (sin->sin_family != AF_INET)
440 return (EAFNOSUPPORT);
442 if (!prison_replace_wildcards(td, nam))
445 lport = sin->sin_port;
446 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
448 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
449 * allow complete duplication of binding if
450 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
451 * and a multicast address is bound on both
452 * new and duplicated sockets.
454 if (so->so_options & SO_REUSEADDR)
455 reuseport = SO_REUSEADDR | SO_REUSEPORT;
456 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
457 sin->sin_port = 0; /* yech... */
458 bzero(&sin->sin_zero, sizeof sin->sin_zero);
459 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
460 return (EADDRNOTAVAIL);
463 inp->inp_laddr = sin->sin_addr;
465 jsin.sin_family = AF_INET;
466 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
467 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
468 inp->inp_laddr.s_addr = INADDR_ANY;
471 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
474 /* Auto-select local port */
475 return in_pcbsetlport(inp, wild, cred);
477 lport_ho = ntohs(lport);
480 if (lport_ho < IPPORT_RESERVED && cred &&
482 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
483 inp->inp_laddr.s_addr = INADDR_ANY;
488 * Locate the proper portinfo based on lport
490 pcbinfo = inp->inp_pcbinfo;
492 &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
493 KKASSERT((lport_ho & pcbinfo->portinfo_mask) ==
497 * This has to be atomic. If the porthash is shared across
498 * multiple protocol threads (aka tcp) then the token must
501 GET_PORT_TOKEN(portinfo);
503 if (so->so_cred->cr_uid != 0 &&
504 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
505 t = in_pcblookup_local(portinfo, sin->sin_addr, lport,
506 INPLOOKUP_WILDCARD, cred);
508 (!in_nullhost(sin->sin_addr) ||
509 !in_nullhost(t->inp_laddr) ||
510 (t->inp_socket->so_options & SO_REUSEPORT) == 0) &&
511 (so->so_cred->cr_uid !=
512 t->inp_socket->so_cred->cr_uid)) {
514 if (!in_nullhost(sin->sin_addr) ||
515 !in_nullhost(t->inp_laddr) ||
516 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
519 inp->inp_laddr.s_addr = INADDR_ANY;
525 if (cred && !prison_replace_wildcards(td, nam)) {
526 inp->inp_laddr.s_addr = INADDR_ANY;
527 error = EADDRNOTAVAIL;
530 t = in_pcblookup_local(portinfo, sin->sin_addr, lport,
532 if (t && !(reuseport & t->inp_socket->so_options)) {
534 if (!in_nullhost(sin->sin_addr) ||
535 !in_nullhost(t->inp_laddr) ||
536 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
539 inp->inp_laddr.s_addr = INADDR_ANY;
544 inp->inp_lport = lport;
545 in_pcbinsporthash(portinfo, inp);
548 REL_PORT_TOKEN(portinfo);
551 jsin.sin_family = AF_INET;
552 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
553 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
554 inp->inp_laddr.s_addr = INADDR_ANY;
557 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
559 return in_pcbsetlport(inp, wild, cred);
563 static struct inpcb *
564 in_pcblookup_localremote(struct inpcbportinfo *portinfo, struct in_addr laddr,
565 u_short lport, struct in_addr faddr, u_short fport, struct ucred *cred)
568 struct inpcbporthead *porthash;
569 struct inpcbport *phd;
570 struct inpcb *match = NULL;
573 * If the porthashbase is shared across several cpus, it must
576 ASSERT_PORT_TOKEN_HELD(portinfo);
579 * Best fit PCB lookup.
581 * First see if this local port is in use by looking on the
584 porthash = &portinfo->porthashbase[
585 INP_PCBPORTHASH(lport, portinfo->porthashmask)];
586 LIST_FOREACH(phd, porthash, phd_hash) {
587 if (phd->phd_port == lport)
591 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
593 if ((inp->inp_vflag & INP_IPV4) == 0)
596 if (inp->inp_laddr.s_addr != INADDR_ANY &&
597 inp->inp_laddr.s_addr != laddr.s_addr)
600 if (inp->inp_faddr.s_addr != INADDR_ANY &&
601 inp->inp_faddr.s_addr != faddr.s_addr)
604 if (inp->inp_fport != 0 && inp->inp_fport != fport)
609 inp->inp_socket->so_cred->cr_prison) {
619 in_pcbsetlport_remote(struct inpcb *inp, const struct sockaddr *remote,
622 struct proc *p = td->td_proc;
623 unsigned short *lastport;
624 const struct sockaddr_in *sin = (const struct sockaddr_in *)remote;
625 struct sockaddr_in jsin;
626 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
627 struct inpcbportinfo *portinfo;
628 struct ucred *cred = NULL;
629 u_short first, last, lport, step;
630 int count, error, dup;
631 int portinfo_first, portinfo_idx;
633 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
634 return (EADDRNOTAVAIL);
636 KKASSERT(inp->inp_laddr.s_addr != INADDR_ANY);
637 if (inp->inp_lport != 0)
638 return (EINVAL); /* already bound */
643 jsin.sin_family = AF_INET;
644 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
645 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
646 inp->inp_laddr.s_addr = INADDR_ANY;
649 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
651 inp->inp_flags |= INP_ANONPORT;
653 step = pcbinfo->portinfo_mask + 1;
654 portinfo_first = mycpuid & pcbinfo->portinfo_mask;
655 portinfo_idx = portinfo_first;
657 portinfo = &pcbinfo->portinfo[portinfo_idx];
660 if (inp->inp_flags & INP_HIGHPORT) {
661 first = ipport_hifirstauto; /* sysctl */
662 last = ipport_hilastauto;
663 lastport = &portinfo->lasthi;
664 } else if (inp->inp_flags & INP_LOWPORT) {
667 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
668 inp->inp_laddr.s_addr = INADDR_ANY;
671 first = ipport_lowfirstauto; /* 1023 */
672 last = ipport_lowlastauto; /* 600 */
673 lastport = &portinfo->lastlow;
675 first = ipport_firstauto; /* sysctl */
676 last = ipport_lastauto;
677 lastport = &portinfo->lastport;
681 * This has to be atomic. If the porthash is shared across multiple
682 * protocol threads (aka tcp) then the token must be held.
684 GET_PORT_TOKEN(portinfo);
688 * Simple check to ensure all ports are not used up causing
691 * We split the two cases (up and down) so that the direction
692 * is not being tested on each round of the loop.
698 in_pcbportrange(&first, &last, portinfo->offset, step);
699 count = (first - last) / step;
702 if (count-- < 0) { /* completely used? */
703 error = EADDRNOTAVAIL;
707 if (*lastport > first || *lastport < last)
709 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
711 lport = htons(*lastport);
712 } while (in_pcblookup_localremote(portinfo, inp->inp_laddr,
713 lport, sin->sin_addr, sin->sin_port, cred));
718 in_pcbportrange(&last, &first, portinfo->offset, step);
719 count = (last - first) / step;
722 if (count-- < 0) { /* completely used? */
723 error = EADDRNOTAVAIL;
727 if (*lastport < first || *lastport > last)
729 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
731 lport = htons(*lastport);
732 } while (in_pcblookup_localremote(portinfo, inp->inp_laddr,
733 lport, sin->sin_addr, sin->sin_port, cred));
736 /* This could happen on loopback interface */
737 if (sin->sin_port == lport &&
738 sin->sin_addr.s_addr == inp->inp_laddr.s_addr) {
741 * Duplicate again; give up
743 error = EADDRNOTAVAIL;
749 inp->inp_lport = lport;
750 in_pcbinsporthash(portinfo, inp);
753 REL_PORT_TOKEN(portinfo);
756 /* Try next portinfo */
758 portinfo_idx &= pcbinfo->portinfo_mask;
759 if (portinfo_idx != portinfo_first)
761 inp->inp_laddr.s_addr = INADDR_ANY;
767 * Transform old in_pcbconnect() into an inner subroutine for new
768 * in_pcbconnect(): Do some validity-checking on the remote
769 * address (in mbuf 'nam') and then determine local host address
770 * (i.e., which interface) to use to access that remote host.
772 * This preserves definition of in_pcbconnect(), while supporting a
773 * slightly different version for T/TCP. (This is more than
774 * a bit of a kludge, but cleaning up the internal interfaces would
775 * have forced minor changes in every protocol).
778 in_pcbladdr_find(struct inpcb *inp, struct sockaddr *nam,
779 struct sockaddr_in **plocal_sin, struct thread *td, int find)
781 struct in_ifaddr *ia;
782 struct ucred *cred = NULL;
783 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
784 struct sockaddr *jsin;
785 int jailed = 0, alloc_route = 0;
787 if (nam->sa_len != sizeof *sin)
789 if (sin->sin_family != AF_INET)
790 return (EAFNOSUPPORT);
791 if (sin->sin_port == 0)
792 return (EADDRNOTAVAIL);
793 if (td && td->td_proc && td->td_proc->p_ucred)
794 cred = td->td_proc->p_ucred;
795 if (cred && cred->cr_prison)
797 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
798 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
800 * If the destination address is INADDR_ANY,
801 * use the primary local address.
802 * If the supplied address is INADDR_BROADCAST,
803 * and the primary interface supports broadcast,
804 * choose the broadcast address for that interface.
806 if (sin->sin_addr.s_addr == INADDR_ANY)
807 sin->sin_addr = IA_SIN(ia)->sin_addr;
808 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
809 (ia->ia_ifp->if_flags & IFF_BROADCAST))
810 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
817 * If route is known or can be allocated now,
818 * our src addr is taken from the i/f, else punt.
819 * Note that we should check the address family of the cached
820 * destination, in case of sharing the cache with IPv6.
822 ro = &inp->inp_route;
824 (!(ro->ro_rt->rt_flags & RTF_UP) ||
825 ro->ro_dst.sa_family != AF_INET ||
826 satosin(&ro->ro_dst)->sin_addr.s_addr !=
827 sin->sin_addr.s_addr ||
828 inp->inp_socket->so_options & SO_DONTROUTE)) {
832 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
833 (ro->ro_rt == NULL ||
834 ro->ro_rt->rt_ifp == NULL)) {
835 /* No route yet, so try to acquire one */
836 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
837 ro->ro_dst.sa_family = AF_INET;
838 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
839 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
845 * If we found a route, use the address
846 * corresponding to the outgoing interface
847 * unless it is the loopback (in case a route
848 * to our address on another net goes to loopback).
851 !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
853 if (jailed_ip(cred->cr_prison,
854 ro->ro_rt->rt_ifa->ifa_addr)) {
855 ia = ifatoia(ro->ro_rt->rt_ifa);
858 ia = ifatoia(ro->ro_rt->rt_ifa);
862 u_short fport = sin->sin_port;
865 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
866 if (ia && jailed && !jailed_ip(cred->cr_prison,
867 sintosa(&ia->ia_addr)))
870 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
871 if (ia && jailed && !jailed_ip(cred->cr_prison,
872 sintosa(&ia->ia_addr)))
874 sin->sin_port = fport;
876 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
877 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
878 if (ia && jailed && !jailed_ip(cred->cr_prison,
879 sintosa(&ia->ia_addr)))
882 if (!jailed && ia == NULL)
886 * If the destination address is multicast and an outgoing
887 * interface has been set as a multicast option, use the
888 * address of that interface as our source address.
890 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
891 inp->inp_moptions != NULL) {
892 struct ip_moptions *imo;
895 imo = inp->inp_moptions;
896 if (imo->imo_multicast_ifp != NULL) {
897 struct in_ifaddr_container *iac;
899 ifp = imo->imo_multicast_ifp;
902 &in_ifaddrheads[mycpuid], ia_link) {
903 if (iac->ia->ia_ifp == ifp) {
913 * Don't do pcblookup call here; return interface in plocal_sin
914 * and exit to caller, that will do the lookup.
916 if (ia == NULL && jailed) {
917 if ((jsin = prison_get_nonlocal(
918 cred->cr_prison, AF_INET, NULL)) != NULL ||
919 (jsin = prison_get_local(
920 cred->cr_prison, AF_INET, NULL)) != NULL) {
921 *plocal_sin = satosin(jsin);
927 *plocal_sin = &ia->ia_addr;
933 struct route *ro = &inp->inp_route;
935 if (ro->ro_rt != NULL)
937 bzero(ro, sizeof(*ro));
939 return (EADDRNOTAVAIL);
943 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
944 struct sockaddr_in **plocal_sin, struct thread *td)
946 return in_pcbladdr_find(inp, nam, plocal_sin, td,
947 (inp->inp_laddr.s_addr == INADDR_ANY));
952 * Connect from a socket to a specified address.
953 * Both address and port must be specified in argument sin.
954 * If don't have a local address for this socket yet,
958 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
960 struct sockaddr_in *if_sin;
961 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
964 /* Call inner routine to assign local interface address. */
965 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
968 if (in_pcblookup_hash(inp->inp_cpcbinfo, sin->sin_addr, sin->sin_port,
969 inp->inp_laddr.s_addr ?
970 inp->inp_laddr : if_sin->sin_addr,
971 inp->inp_lport, FALSE, NULL) != NULL) {
974 if (inp->inp_laddr.s_addr == INADDR_ANY) {
975 if (inp->inp_lport == 0) {
976 error = in_pcbbind(inp, NULL, td);
980 inp->inp_laddr = if_sin->sin_addr;
982 inp->inp_faddr = sin->sin_addr;
983 inp->inp_fport = sin->sin_port;
984 in_pcbinsconnhash(inp);
989 in_pcbdisconnect(struct inpcb *inp)
992 inp->inp_faddr.s_addr = INADDR_ANY;
994 in_pcbremconnhash(inp);
998 in_pcbdetach(struct inpcb *inp)
1000 struct socket *so = inp->inp_socket;
1001 struct inpcbinfo *ipi = inp->inp_pcbinfo;
1004 ipsec4_delete_pcbpolicy(inp);
1006 inp->inp_gencnt = ++ipi->ipi_gencnt;
1007 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
1008 in_pcbremlists(inp);
1010 sofree(so); /* remove pcb ref */
1011 if (inp->inp_options)
1012 m_free(inp->inp_options);
1013 if (inp->inp_route.ro_rt)
1014 rtfree(inp->inp_route.ro_rt);
1015 ip_freemoptions(inp->inp_moptions);
1021 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
1022 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
1023 * in struct pr_usrreqs, so that protocols can just reference then directly
1024 * without the need for a wrapper function. The socket must have a valid
1025 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
1026 * except through a kernel programming error, so it is acceptable to panic
1027 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
1028 * because there actually /is/ a programming error somewhere... XXX)
1031 in_setsockaddr(struct socket *so, struct sockaddr **nam)
1034 struct sockaddr_in *sin;
1037 * Do the malloc first in case it blocks.
1039 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1040 sin->sin_family = AF_INET;
1041 sin->sin_len = sizeof *sin;
1047 kfree(sin, M_SONAME);
1048 return (ECONNRESET);
1050 sin->sin_port = inp->inp_lport;
1051 sin->sin_addr = inp->inp_laddr;
1054 *nam = (struct sockaddr *)sin;
1059 in_setsockaddr_dispatch(netmsg_t msg)
1063 error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1064 lwkt_replymsg(&msg->lmsg, error);
1068 in_setpeeraddr(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_fport;
1088 sin->sin_addr = inp->inp_faddr;
1091 *nam = (struct sockaddr *)sin;
1096 in_setpeeraddr_dispatch(netmsg_t msg)
1100 error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1101 lwkt_replymsg(&msg->lmsg, error);
1105 in_pcbnotifyall(struct inpcbhead *head, struct in_addr faddr, int err,
1106 void (*notify)(struct inpcb *, int))
1108 struct inpcb *inp, *ninp;
1111 * note: if INP_PLACEMARKER is set we must ignore the rest of
1112 * the structure and skip it.
1115 LIST_FOREACH_MUTABLE(inp, head, inp_list, ninp) {
1116 if (inp->inp_flags & INP_PLACEMARKER)
1119 if (!(inp->inp_vflag & INP_IPV4))
1122 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1123 inp->inp_socket == NULL)
1125 (*notify)(inp, err); /* can remove inp from list! */
1131 in_pcbpurgeif0(struct inpcb *head, struct ifnet *ifp)
1134 struct ip_moptions *imo;
1137 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
1138 if (inp->inp_flags & INP_PLACEMARKER)
1140 imo = inp->inp_moptions;
1141 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
1143 * Unselect the outgoing interface if it is being
1146 if (imo->imo_multicast_ifp == ifp)
1147 imo->imo_multicast_ifp = NULL;
1150 * Drop multicast group membership if we joined
1151 * through the interface being detached.
1153 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1155 if (imo->imo_membership[i]->inm_ifp == ifp) {
1156 in_delmulti(imo->imo_membership[i]);
1158 } else if (gap != 0)
1159 imo->imo_membership[i - gap] =
1160 imo->imo_membership[i];
1162 imo->imo_num_memberships -= gap;
1168 * Check for alternatives when higher level complains
1169 * about service problems. For now, invalidate cached
1170 * routing information. If the route was created dynamically
1171 * (by a redirect), time to try a default gateway again.
1174 in_losing(struct inpcb *inp)
1177 struct rt_addrinfo rtinfo;
1179 if ((rt = inp->inp_route.ro_rt)) {
1180 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1181 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1182 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1183 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1184 rtinfo.rti_flags = rt->rt_flags;
1185 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1186 if (rt->rt_flags & RTF_DYNAMIC) {
1187 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1188 rt_mask(rt), rt->rt_flags, NULL);
1190 inp->inp_route.ro_rt = NULL;
1193 * A new route can be allocated
1194 * the next time output is attempted.
1200 * After a routing change, flush old routing
1201 * and allocate a (hopefully) better one.
1204 in_rtchange(struct inpcb *inp, int err)
1206 if (inp->inp_route.ro_rt) {
1207 rtfree(inp->inp_route.ro_rt);
1208 inp->inp_route.ro_rt = NULL;
1210 * A new route can be allocated the next time
1211 * output is attempted.
1217 * Lookup a PCB based on the local address and port.
1220 in_pcblookup_local(struct inpcbportinfo *portinfo, struct in_addr laddr,
1221 u_int lport_arg, int wild_okay, struct ucred *cred)
1224 int matchwild = 3, wildcard;
1225 u_short lport = lport_arg;
1226 struct inpcbporthead *porthash;
1227 struct inpcbport *phd;
1228 struct inpcb *match = NULL;
1231 * If the porthashbase is shared across several cpus, it must
1234 ASSERT_PORT_TOKEN_HELD(portinfo);
1237 * Best fit PCB lookup.
1239 * First see if this local port is in use by looking on the
1242 porthash = &portinfo->porthashbase[
1243 INP_PCBPORTHASH(lport, portinfo->porthashmask)];
1244 LIST_FOREACH(phd, porthash, phd_hash) {
1245 if (phd->phd_port == lport)
1250 * Port is in use by one or more PCBs. Look for best
1253 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1256 if ((inp->inp_vflag & INP_IPV4) == 0)
1259 if (inp->inp_faddr.s_addr != INADDR_ANY)
1261 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1262 if (laddr.s_addr == INADDR_ANY)
1264 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1267 if (laddr.s_addr != INADDR_ANY)
1270 if (wildcard && !wild_okay)
1272 if (wildcard < matchwild &&
1275 inp->inp_socket->so_cred->cr_prison)) {
1277 matchwild = wildcard;
1278 if (matchwild == 0) {
1288 in_pcblocalgroup_last(const struct inpcbinfo *pcbinfo,
1289 const struct inpcb *inp)
1291 const struct inp_localgrphead *hdr;
1292 const struct inp_localgroup *grp;
1295 if (pcbinfo->localgrphashbase == NULL)
1298 hdr = &pcbinfo->localgrphashbase[
1299 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1301 LIST_FOREACH(grp, hdr, il_list) {
1302 if (grp->il_vflag == inp->inp_vflag &&
1303 grp->il_lport == inp->inp_lport &&
1304 memcmp(&grp->il_dependladdr,
1305 &inp->inp_inc.inc_ie.ie_dependladdr,
1306 sizeof(grp->il_dependladdr)) == 0) {
1310 if (grp == NULL || grp->il_inpcnt == 1)
1313 KASSERT(grp->il_inpcnt >= 2,
1314 ("invalid localgroup inp count %d", grp->il_inpcnt));
1315 for (i = 0; i < grp->il_inpcnt; ++i) {
1316 if (grp->il_inp[i] == inp) {
1317 int last = grp->il_inpcnt - 1;
1320 last = grp->il_inpcnt - 2;
1321 return grp->il_inp[last];
1327 static struct inpcb *
1328 inp_localgroup_lookup(const struct inpcbinfo *pcbinfo,
1329 struct in_addr laddr, uint16_t lport, uint32_t pkt_hash)
1331 struct inpcb *local_wild = NULL;
1332 const struct inp_localgrphead *hdr;
1333 const struct inp_localgroup *grp;
1335 hdr = &pcbinfo->localgrphashbase[
1336 INP_PCBLOCALGRPHASH(lport, pcbinfo->localgrphashmask)];
1337 #ifdef INP_LOCALGROUP_HASHTHR
1338 pkt_hash >>= ncpus2_shift;
1342 * Order of socket selection:
1347 * - Local group does not contain jailed sockets
1348 * - Local group does not contain IPv4 mapped INET6 wild sockets
1350 LIST_FOREACH(grp, hdr, il_list) {
1352 if (!(grp->il_vflag & INP_IPV4))
1355 if (grp->il_lport == lport) {
1358 #ifdef INP_LOCALGROUP_HASHTHR
1359 idx = pkt_hash / grp->il_factor;
1360 KASSERT(idx < grp->il_inpcnt && idx >= 0,
1361 ("invalid hash %04x, cnt %d or fact %d",
1362 pkt_hash, grp->il_inpcnt, grp->il_factor));
1365 * Modulo-N is used here, which greatly reduces
1366 * completion queue token contention, thus more
1367 * cpu time is saved.
1369 idx = pkt_hash % grp->il_inpcnt;
1372 if (grp->il_laddr.s_addr == laddr.s_addr)
1373 return grp->il_inp[idx];
1374 else if (grp->il_laddr.s_addr == INADDR_ANY)
1375 local_wild = grp->il_inp[idx];
1378 if (local_wild != NULL)
1384 * Lookup PCB in hash list.
1387 in_pcblookup_pkthash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1388 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1389 boolean_t wildcard, struct ifnet *ifp, const struct mbuf *m)
1391 struct inpcbhead *head;
1392 struct inpcb *inp, *jinp=NULL;
1393 u_short fport = fport_arg, lport = lport_arg;
1396 * First look for an exact match.
1398 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1399 laddr.s_addr, lport, pcbinfo->hashmask)];
1400 LIST_FOREACH(inp, head, inp_hash) {
1402 if (!(inp->inp_vflag & INP_IPV4))
1405 if (in_hosteq(inp->inp_faddr, faddr) &&
1406 in_hosteq(inp->inp_laddr, laddr) &&
1407 inp->inp_fport == fport && inp->inp_lport == lport) {
1409 if (inp->inp_socket == NULL ||
1410 inp->inp_socket->so_cred->cr_prison == NULL) {
1421 struct inpcb *local_wild = NULL;
1422 struct inpcb *jinp_wild = NULL;
1424 struct inpcb *local_wild_mapped = NULL;
1426 struct inpcontainer *ic;
1427 struct inpcontainerhead *chead;
1428 struct sockaddr_in jsin;
1432 * Check local group first
1434 if (pcbinfo->localgrphashbase != NULL &&
1435 m != NULL && (m->m_flags & M_HASH) &&
1436 !(ifp && ifp->if_type == IFT_FAITH)) {
1437 inp = inp_localgroup_lookup(pcbinfo,
1438 laddr, lport, m->m_pkthdr.hash);
1444 * Order of socket selection:
1445 * 1. non-jailed, non-wild.
1446 * 2. non-jailed, wild.
1447 * 3. jailed, non-wild.
1450 jsin.sin_family = AF_INET;
1451 chead = &pcbinfo->wildcardhashbase[
1452 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1453 LIST_FOREACH(ic, chead, ic_list) {
1455 jsin.sin_addr.s_addr = laddr.s_addr;
1457 if (!(inp->inp_vflag & INP_IPV4))
1460 if (inp->inp_socket != NULL)
1461 cred = inp->inp_socket->so_cred;
1464 if (cred != NULL && jailed(cred)) {
1468 if (!jailed_ip(cred->cr_prison,
1469 (struct sockaddr *)&jsin))
1472 if (inp->inp_lport == lport) {
1473 if (ifp && ifp->if_type == IFT_FAITH &&
1474 !(inp->inp_flags & INP_FAITH))
1476 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1477 if (cred != NULL && jailed(cred))
1482 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1484 if (INP_CHECK_SOCKAF(inp->inp_socket,
1486 local_wild_mapped = inp;
1497 if (local_wild != NULL)
1498 return (local_wild);
1500 if (local_wild_mapped != NULL)
1501 return (local_wild_mapped);
1515 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1516 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1517 boolean_t wildcard, struct ifnet *ifp)
1519 return in_pcblookup_pkthash(pcbinfo, faddr, fport_arg,
1520 laddr, lport_arg, wildcard, ifp, NULL);
1524 * Insert PCB into connection hash table.
1527 in_pcbinsconnhash(struct inpcb *inp)
1529 struct inpcbinfo *pcbinfo = inp->inp_cpcbinfo;
1530 struct inpcbhead *bucket;
1531 u_int32_t hashkey_faddr, hashkey_laddr;
1534 if (inp->inp_vflag & INP_IPV6) {
1535 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1536 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1539 hashkey_faddr = inp->inp_faddr.s_addr;
1540 hashkey_laddr = inp->inp_laddr.s_addr;
1545 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1546 ("already on wildcardhash"));
1547 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1548 ("already on connhash"));
1549 inp->inp_flags |= INP_CONNECTED;
1552 * Insert into the connection hash table.
1554 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1555 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1556 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1560 * Remove PCB from connection hash table.
1563 in_pcbremconnhash(struct inpcb *inp)
1565 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1566 LIST_REMOVE(inp, inp_hash);
1567 inp->inp_flags &= ~INP_CONNECTED;
1571 * Insert PCB into port hash table.
1574 in_pcbinsporthash(struct inpcbportinfo *portinfo, struct inpcb *inp)
1576 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1577 struct inpcbporthead *pcbporthash;
1578 struct inpcbport *phd;
1581 * If the porthashbase is shared across several cpus, it must
1584 ASSERT_PORT_TOKEN_HELD(portinfo);
1587 * Insert into the port hash table.
1589 pcbporthash = &portinfo->porthashbase[
1590 INP_PCBPORTHASH(inp->inp_lport, portinfo->porthashmask)];
1592 /* Go through port list and look for a head for this lport. */
1593 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1594 if (phd->phd_port == inp->inp_lport)
1598 /* If none exists, use saved one and tack it on. */
1600 KKASSERT(pcbinfo->portsave != NULL);
1601 phd = pcbinfo->portsave;
1602 pcbinfo->portsave = NULL;
1603 phd->phd_port = inp->inp_lport;
1604 LIST_INIT(&phd->phd_pcblist);
1605 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1608 inp->inp_portinfo = portinfo;
1610 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1613 * Malloc one inpcbport for later use. It is safe to use
1614 * "wait" malloc here (port token would be released, if
1615 * malloc ever blocked), since all changes to the porthash
1618 if (pcbinfo->portsave == NULL) {
1619 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1620 M_PCB, M_INTWAIT | M_ZERO);
1625 in_pcbinsporthash_lport(struct inpcb *inp)
1627 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1628 struct inpcbportinfo *portinfo;
1631 /* Locate the proper portinfo based on lport */
1632 lport_ho = ntohs(inp->inp_lport);
1633 portinfo = &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
1634 KKASSERT((lport_ho & pcbinfo->portinfo_mask) == portinfo->offset);
1636 GET_PORT_TOKEN(portinfo);
1637 in_pcbinsporthash(portinfo, inp);
1638 REL_PORT_TOKEN(portinfo);
1641 static struct inp_localgroup *
1642 inp_localgroup_alloc(struct inp_localgrphead *hdr, u_char vflag,
1643 uint16_t port, const union in_dependaddr *addr, int size)
1645 struct inp_localgroup *grp;
1647 grp = kmalloc(__offsetof(struct inp_localgroup, il_inp[size]),
1648 M_TEMP, M_INTWAIT | M_ZERO);
1649 grp->il_vflag = vflag;
1650 grp->il_lport = port;
1651 grp->il_dependladdr = *addr;
1652 grp->il_inpsiz = size;
1654 LIST_INSERT_HEAD(hdr, grp, il_list);
1660 inp_localgroup_free(struct inp_localgroup *grp)
1662 LIST_REMOVE(grp, il_list);
1666 static struct inp_localgroup *
1667 inp_localgroup_resize(struct inp_localgrphead *hdr,
1668 struct inp_localgroup *old_grp, int size)
1670 struct inp_localgroup *grp;
1673 grp = inp_localgroup_alloc(hdr, old_grp->il_vflag,
1674 old_grp->il_lport, &old_grp->il_dependladdr, size);
1676 KASSERT(old_grp->il_inpcnt < grp->il_inpsiz,
1677 ("invalid new local group size %d and old local group count %d",
1678 grp->il_inpsiz, old_grp->il_inpcnt));
1679 for (i = 0; i < old_grp->il_inpcnt; ++i)
1680 grp->il_inp[i] = old_grp->il_inp[i];
1681 grp->il_inpcnt = old_grp->il_inpcnt;
1682 grp->il_factor = old_grp->il_factor;
1684 inp_localgroup_free(old_grp);
1690 inp_localgroup_factor(struct inp_localgroup *grp)
1693 ((uint32_t)(0xffff >> ncpus2_shift) / grp->il_inpcnt) + 1;
1694 KASSERT(grp->il_factor != 0, ("invalid local group factor, "
1695 "ncpus2_shift %d, inpcnt %d", ncpus2_shift, grp->il_inpcnt));
1699 in_pcbinslocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1701 struct inp_localgrphead *hdr;
1702 struct inp_localgroup *grp;
1705 if (pcbinfo->localgrphashbase == NULL)
1709 * XXX don't allow jailed socket to join local group
1711 if (inp->inp_socket != NULL)
1712 cred = inp->inp_socket->so_cred;
1715 if (cred != NULL && jailed(cred))
1720 * XXX don't allow IPv4 mapped INET6 wild socket
1722 if ((inp->inp_vflag & INP_IPV4) &&
1723 inp->inp_laddr.s_addr == INADDR_ANY &&
1724 INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6))
1728 hdr = &pcbinfo->localgrphashbase[
1729 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1731 LIST_FOREACH(grp, hdr, il_list) {
1732 if (grp->il_vflag == inp->inp_vflag &&
1733 grp->il_lport == inp->inp_lport &&
1734 memcmp(&grp->il_dependladdr,
1735 &inp->inp_inc.inc_ie.ie_dependladdr,
1736 sizeof(grp->il_dependladdr)) == 0) {
1741 /* Create new local group */
1742 grp = inp_localgroup_alloc(hdr, inp->inp_vflag,
1743 inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr,
1744 INP_LOCALGROUP_SIZMIN);
1745 } else if (grp->il_inpcnt == grp->il_inpsiz) {
1746 if (grp->il_inpsiz >= INP_LOCALGROUP_SIZMAX) {
1747 static int limit_logged = 0;
1749 if (!limit_logged) {
1751 kprintf("local group port %d, "
1752 "limit reached\n", ntohs(grp->il_lport));
1757 /* Expand this local group */
1758 grp = inp_localgroup_resize(hdr, grp, grp->il_inpsiz * 2);
1761 KASSERT(grp->il_inpcnt < grp->il_inpsiz,
1762 ("invalid local group size %d and count %d",
1763 grp->il_inpsiz, grp->il_inpcnt));
1764 grp->il_inp[grp->il_inpcnt] = inp;
1766 inp_localgroup_factor(grp);
1770 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1772 struct inpcontainer *ic;
1773 struct inpcontainerhead *bucket;
1775 in_pcbinslocalgrphash_oncpu(inp, pcbinfo);
1777 bucket = &pcbinfo->wildcardhashbase[
1778 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1780 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1782 LIST_INSERT_HEAD(bucket, ic, ic_list);
1786 * Insert PCB into wildcard hash table.
1789 in_pcbinswildcardhash(struct inpcb *inp)
1791 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1793 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1794 ("already on connhash"));
1795 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1796 ("already on wildcardhash"));
1797 inp->inp_flags |= INP_WILDCARD;
1799 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1803 in_pcbremlocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1805 struct inp_localgrphead *hdr;
1806 struct inp_localgroup *grp;
1808 if (pcbinfo->localgrphashbase == NULL)
1811 hdr = &pcbinfo->localgrphashbase[
1812 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1814 LIST_FOREACH(grp, hdr, il_list) {
1817 for (i = 0; i < grp->il_inpcnt; ++i) {
1818 if (grp->il_inp[i] != inp)
1821 if (grp->il_inpcnt == 1) {
1822 /* Free this local group */
1823 inp_localgroup_free(grp);
1825 /* Pull up inpcbs */
1826 for (; i + 1 < grp->il_inpcnt; ++i)
1827 grp->il_inp[i] = grp->il_inp[i + 1];
1829 inp_localgroup_factor(grp);
1831 if (grp->il_inpsiz > INP_LOCALGROUP_SIZMIN &&
1832 grp->il_inpcnt <= (grp->il_inpsiz / 4)) {
1833 /* Shrink this local group */
1834 grp = inp_localgroup_resize(hdr, grp,
1835 grp->il_inpsiz / 2);
1844 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1846 struct inpcontainer *ic;
1847 struct inpcontainerhead *head;
1849 in_pcbremlocalgrphash_oncpu(inp, pcbinfo);
1852 head = &pcbinfo->wildcardhashbase[
1853 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1855 LIST_FOREACH(ic, head, ic_list) {
1856 if (ic->ic_inp == inp)
1859 return; /* not found! */
1862 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
1863 kfree(ic, M_TEMP); /* deallocate container */
1867 * Remove PCB from wildcard hash table.
1870 in_pcbremwildcardhash(struct inpcb *inp)
1872 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1874 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1875 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
1876 inp->inp_flags &= ~INP_WILDCARD;
1880 * Remove PCB from various lists.
1883 in_pcbremlists(struct inpcb *inp)
1885 if (inp->inp_lport) {
1886 struct inpcbportinfo *portinfo;
1887 struct inpcbport *phd;
1891 * inp->inp_portinfo is _not_ necessary same as
1892 * inp->inp_pcbinfo->portinfo.
1894 portinfo = inp->inp_portinfo;
1895 GET_PORT_TOKEN(portinfo);
1898 LIST_REMOVE(inp, inp_portlist);
1899 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1900 LIST_REMOVE(phd, phd_hash);
1904 REL_PORT_TOKEN(portinfo);
1906 if (inp->inp_flags & INP_WILDCARD) {
1907 in_pcbremwildcardhash(inp);
1908 } else if (inp->inp_flags & INP_CONNECTED) {
1909 in_pcbremconnhash(inp);
1911 LIST_REMOVE(inp, inp_list);
1912 inp->inp_pcbinfo->ipi_count--;
1916 prison_xinpcb(struct thread *td, struct inpcb *inp)
1920 if (td->td_proc == NULL)
1922 cr = td->td_proc->p_ucred;
1923 if (cr->cr_prison == NULL)
1925 if (inp->inp_socket && inp->inp_socket->so_cred &&
1926 inp->inp_socket->so_cred->cr_prison &&
1927 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
1933 in_pcblist_global(SYSCTL_HANDLER_ARGS)
1935 struct inpcbinfo *pcbinfo = arg1;
1936 struct inpcb *inp, *marker;
1941 * The process of preparing the TCB list is too time-consuming and
1942 * resource-intensive to repeat twice on every request.
1944 if (req->oldptr == NULL) {
1945 n = pcbinfo->ipi_count;
1946 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1950 if (req->newptr != NULL)
1954 * OK, now we're committed to doing something. Re-fetch ipi_count
1955 * after obtaining the generation count.
1957 n = pcbinfo->ipi_count;
1959 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
1960 marker->inp_flags |= INP_PLACEMARKER;
1961 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1966 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
1967 LIST_REMOVE(marker, inp_list);
1968 LIST_INSERT_AFTER(inp, marker, inp_list);
1970 if (inp->inp_flags & INP_PLACEMARKER)
1972 if (prison_xinpcb(req->td, inp))
1974 bzero(&xi, sizeof xi);
1975 xi.xi_len = sizeof xi;
1976 bcopy(inp, &xi.xi_inp, sizeof *inp);
1977 if (inp->inp_socket)
1978 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1979 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
1983 LIST_REMOVE(marker, inp_list);
1984 if (error == 0 && i < n) {
1985 bzero(&xi, sizeof xi);
1986 xi.xi_len = sizeof xi;
1988 error = SYSCTL_OUT(req, &xi, sizeof xi);
1992 kfree(marker, M_TEMP);
1997 in_pcblist_global_cpu0(SYSCTL_HANDLER_ARGS)
1999 boolean_t migrate = FALSE;
2000 int origcpu = mycpuid;
2008 error = in_pcblist_global(oidp, arg1, arg2, req);
2011 lwkt_migratecpu(origcpu);
2016 in_pcblist_global_nomarker(SYSCTL_HANDLER_ARGS, struct xinpcb **xi0, int *nxi0)
2018 struct inpcbinfo *pcbinfo = arg1;
2027 * The process of preparing the PCB list is too time-consuming and
2028 * resource-intensive to repeat twice on every request.
2030 if (req->oldptr == NULL) {
2031 int n = pcbinfo->ipi_count;
2033 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
2037 if (req->newptr != NULL)
2040 if (pcbinfo->ipi_count == 0)
2044 xi = kmalloc(pcbinfo->ipi_count * sizeof(*xi), M_TEMP,
2045 M_WAITOK | M_ZERO | M_NULLOK);
2049 LIST_FOREACH(inp, &pcbinfo->pcblisthead, inp_list) {
2050 struct xinpcb *xi_ptr = &xi[nxi];
2052 if (prison_xinpcb(req->td, inp))
2055 xi_ptr->xi_len = sizeof(*xi_ptr);
2056 bcopy(inp, &xi_ptr->xi_inp, sizeof(*inp));
2057 if (inp->inp_socket)
2058 sotoxsocket(inp->inp_socket, &xi_ptr->xi_socket);
2074 in_savefaddr(struct socket *so, const struct sockaddr *faddr)
2076 struct sockaddr_in *sin;
2078 KASSERT(faddr->sa_family == AF_INET,
2079 ("not AF_INET faddr %d", faddr->sa_family));
2081 sin = kmalloc(sizeof(*sin), M_SONAME, M_WAITOK | M_ZERO);
2082 sin->sin_family = AF_INET;
2083 sin->sin_len = sizeof(*sin);
2084 sin->sin_port = ((const struct sockaddr_in *)faddr)->sin_port;
2085 sin->sin_addr = ((const struct sockaddr_in *)faddr)->sin_addr;
2087 so->so_faddr = (struct sockaddr *)sin;
2091 in_pcbportinfo_init(struct inpcbportinfo *portinfo, int hashsize,
2092 boolean_t shared, u_short offset)
2094 memset(portinfo, 0, sizeof(*portinfo));
2096 portinfo->offset = offset;
2097 portinfo->lastport = offset;
2098 portinfo->lastlow = offset;
2099 portinfo->lasthi = offset;
2101 portinfo->porthashbase = hashinit(hashsize, M_PCB,
2102 &portinfo->porthashmask);
2105 portinfo->porttoken = kmalloc(sizeof(struct lwkt_token),
2107 lwkt_token_init(portinfo->porttoken, "porttoken");
2112 in_pcbportrange(u_short *hi0, u_short *lo0, u_short ofs, u_short step)
2122 hi = rounddown2(hi, step);
2127 lo = roundup2(lo, step);