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 = 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_flags & (INP_WILDCARD | INP_CONNECTED)) == 0,
272 LIST_REMOVE(inp, inp_list);
273 pcbinfo->ipi_count--;
274 inp->inp_pcbinfo = NULL;
278 * Relink a pcb into a new pcbinfo.
281 in_pcblink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
283 KASSERT(inp->inp_pcbinfo == NULL, ("has pcbinfo"));
284 KASSERT((inp->inp_flags & (INP_WILDCARD | INP_CONNECTED)) == 0,
287 inp->inp_pcbinfo = pcbinfo;
288 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
289 pcbinfo->ipi_count++;
293 in_pcbsetlport(struct inpcb *inp, int wild, struct ucred *cred)
295 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
296 struct inpcbportinfo *portinfo;
297 u_short first, last, lport, step;
300 int portinfo_first, portinfo_idx;
302 inp->inp_flags |= INP_ANONPORT;
304 step = pcbinfo->portinfo_mask + 1;
305 portinfo_first = mycpuid & pcbinfo->portinfo_mask;
306 portinfo_idx = portinfo_first;
308 portinfo = &pcbinfo->portinfo[portinfo_idx];
310 if (inp->inp_flags & INP_HIGHPORT) {
311 first = ipport_hifirstauto; /* sysctl */
312 last = ipport_hilastauto;
313 lastport = &portinfo->lasthi;
314 } else if (inp->inp_flags & INP_LOWPORT) {
317 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
318 inp->inp_laddr.s_addr = INADDR_ANY;
321 first = ipport_lowfirstauto; /* 1023 */
322 last = ipport_lowlastauto; /* 600 */
323 lastport = &portinfo->lastlow;
325 first = ipport_firstauto; /* sysctl */
326 last = ipport_lastauto;
327 lastport = &portinfo->lastport;
331 * This has to be atomic. If the porthash is shared across multiple
332 * protocol threads (aka tcp) then the token must be held.
334 GET_PORT_TOKEN(portinfo);
337 * Simple check to ensure all ports are not used up causing
340 * We split the two cases (up and down) so that the direction
341 * is not being tested on each round of the loop.
347 in_pcbportrange(&first, &last, portinfo->offset, step);
348 count = (first - last) / step;
351 if (count-- < 0) { /* completely used? */
352 error = EADDRNOTAVAIL;
356 if (*lastport > first || *lastport < last)
358 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
360 lport = htons(*lastport);
361 } while (in_pcblookup_local(portinfo, inp->inp_laddr, lport,
367 in_pcbportrange(&last, &first, portinfo->offset, step);
368 count = (last - first) / step;
371 if (count-- < 0) { /* completely used? */
372 error = EADDRNOTAVAIL;
376 if (*lastport < first || *lastport > last)
378 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
380 lport = htons(*lastport);
381 } while (in_pcblookup_local(portinfo, inp->inp_laddr, lport,
384 inp->inp_lport = lport;
385 in_pcbinsporthash(portinfo, inp);
388 REL_PORT_TOKEN(portinfo);
391 /* Try next portinfo */
393 portinfo_idx &= pcbinfo->portinfo_mask;
394 if (portinfo_idx != portinfo_first)
396 inp->inp_laddr.s_addr = INADDR_ANY;
402 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
404 struct socket *so = inp->inp_socket;
405 struct sockaddr_in jsin;
406 struct ucred *cred = NULL;
409 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
410 return (EADDRNOTAVAIL);
411 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
412 return (EINVAL); /* already bound */
414 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
415 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
417 cred = td->td_proc->p_ucred;
420 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
421 struct inpcbinfo *pcbinfo;
422 struct inpcbportinfo *portinfo;
424 u_short lport, lport_ho;
425 int reuseport = (so->so_options & SO_REUSEPORT);
428 if (nam->sa_len != sizeof *sin)
432 * We should check the family, but old programs
433 * incorrectly fail to initialize it.
435 if (sin->sin_family != AF_INET)
436 return (EAFNOSUPPORT);
438 if (!prison_replace_wildcards(td, nam))
441 lport = sin->sin_port;
442 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
444 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
445 * allow complete duplication of binding if
446 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
447 * and a multicast address is bound on both
448 * new and duplicated sockets.
450 if (so->so_options & SO_REUSEADDR)
451 reuseport = SO_REUSEADDR | SO_REUSEPORT;
452 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
453 sin->sin_port = 0; /* yech... */
454 bzero(&sin->sin_zero, sizeof sin->sin_zero);
455 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
456 return (EADDRNOTAVAIL);
459 inp->inp_laddr = sin->sin_addr;
461 jsin.sin_family = AF_INET;
462 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
463 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
464 inp->inp_laddr.s_addr = INADDR_ANY;
467 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
470 /* Auto-select local port */
471 return in_pcbsetlport(inp, wild, cred);
473 lport_ho = ntohs(lport);
476 if (lport_ho < IPPORT_RESERVED && cred &&
478 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
479 inp->inp_laddr.s_addr = INADDR_ANY;
484 * Locate the proper portinfo based on lport
486 pcbinfo = inp->inp_pcbinfo;
488 &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
489 KKASSERT((lport_ho & pcbinfo->portinfo_mask) ==
493 * This has to be atomic. If the porthash is shared across
494 * multiple protocol threads (aka tcp) then the token must
497 GET_PORT_TOKEN(portinfo);
499 if (so->so_cred->cr_uid != 0 &&
500 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
501 t = in_pcblookup_local(portinfo, sin->sin_addr, lport,
502 INPLOOKUP_WILDCARD, cred);
504 (!in_nullhost(sin->sin_addr) ||
505 !in_nullhost(t->inp_laddr) ||
506 (t->inp_socket->so_options & SO_REUSEPORT) == 0) &&
507 (so->so_cred->cr_uid !=
508 t->inp_socket->so_cred->cr_uid)) {
510 if (!in_nullhost(sin->sin_addr) ||
511 !in_nullhost(t->inp_laddr) ||
512 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
515 inp->inp_laddr.s_addr = INADDR_ANY;
521 if (cred && !prison_replace_wildcards(td, nam)) {
522 inp->inp_laddr.s_addr = INADDR_ANY;
523 error = EADDRNOTAVAIL;
526 t = in_pcblookup_local(portinfo, sin->sin_addr, lport,
528 if (t && !(reuseport & t->inp_socket->so_options)) {
530 if (!in_nullhost(sin->sin_addr) ||
531 !in_nullhost(t->inp_laddr) ||
532 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
535 inp->inp_laddr.s_addr = INADDR_ANY;
540 inp->inp_lport = lport;
541 in_pcbinsporthash(portinfo, inp);
544 REL_PORT_TOKEN(portinfo);
547 jsin.sin_family = AF_INET;
548 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
549 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
550 inp->inp_laddr.s_addr = INADDR_ANY;
553 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
555 return in_pcbsetlport(inp, wild, cred);
559 static struct inpcb *
560 in_pcblookup_localremote(struct inpcbportinfo *portinfo, struct in_addr laddr,
561 u_short lport, struct in_addr faddr, u_short fport, struct ucred *cred)
564 struct inpcbporthead *porthash;
565 struct inpcbport *phd;
566 struct inpcb *match = NULL;
569 * If the porthashbase is shared across several cpus, it must
572 ASSERT_PORT_TOKEN_HELD(portinfo);
575 * Best fit PCB lookup.
577 * First see if this local port is in use by looking on the
580 porthash = &portinfo->porthashbase[
581 INP_PCBPORTHASH(lport, portinfo->porthashmask)];
582 LIST_FOREACH(phd, porthash, phd_hash) {
583 if (phd->phd_port == lport)
587 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
589 if ((inp->inp_vflag & INP_IPV4) == 0)
592 if (inp->inp_laddr.s_addr != INADDR_ANY &&
593 inp->inp_laddr.s_addr != laddr.s_addr)
596 if (inp->inp_faddr.s_addr != INADDR_ANY &&
597 inp->inp_faddr.s_addr != faddr.s_addr)
600 if (inp->inp_fport != 0 && inp->inp_fport != fport)
605 inp->inp_socket->so_cred->cr_prison) {
615 in_pcbbind_remote(struct inpcb *inp, const struct sockaddr *remote,
618 struct proc *p = td->td_proc;
619 unsigned short *lastport;
620 const struct sockaddr_in *sin = (const struct sockaddr_in *)remote;
621 struct sockaddr_in jsin;
622 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
623 struct inpcbportinfo *portinfo;
624 struct ucred *cred = NULL;
625 u_short first, last, lport, step;
626 int count, error, dup;
627 int portinfo_first, portinfo_idx;
629 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
630 return (EADDRNOTAVAIL);
632 KKASSERT(inp->inp_laddr.s_addr != INADDR_ANY);
633 if (inp->inp_lport != 0)
634 return (EINVAL); /* already bound */
639 jsin.sin_family = AF_INET;
640 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
641 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
642 inp->inp_laddr.s_addr = INADDR_ANY;
645 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
647 inp->inp_flags |= INP_ANONPORT;
649 step = pcbinfo->portinfo_mask + 1;
650 portinfo_first = mycpuid & pcbinfo->portinfo_mask;
651 portinfo_idx = portinfo_first;
653 portinfo = &pcbinfo->portinfo[portinfo_idx];
656 if (inp->inp_flags & INP_HIGHPORT) {
657 first = ipport_hifirstauto; /* sysctl */
658 last = ipport_hilastauto;
659 lastport = &portinfo->lasthi;
660 } else if (inp->inp_flags & INP_LOWPORT) {
663 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
664 inp->inp_laddr.s_addr = INADDR_ANY;
667 first = ipport_lowfirstauto; /* 1023 */
668 last = ipport_lowlastauto; /* 600 */
669 lastport = &portinfo->lastlow;
671 first = ipport_firstauto; /* sysctl */
672 last = ipport_lastauto;
673 lastport = &portinfo->lastport;
677 * This has to be atomic. If the porthash is shared across multiple
678 * protocol threads (aka tcp) then the token must be held.
680 GET_PORT_TOKEN(portinfo);
684 * Simple check to ensure all ports are not used up causing
687 * We split the two cases (up and down) so that the direction
688 * is not being tested on each round of the loop.
694 in_pcbportrange(&first, &last, portinfo->offset, step);
695 count = (first - last) / step;
698 if (count-- < 0) { /* completely used? */
699 error = EADDRNOTAVAIL;
703 if (*lastport > first || *lastport < last)
705 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
707 lport = htons(*lastport);
708 } while (in_pcblookup_localremote(portinfo, inp->inp_laddr,
709 lport, sin->sin_addr, sin->sin_port, cred));
714 in_pcbportrange(&last, &first, portinfo->offset, step);
715 count = (last - first) / step;
718 if (count-- < 0) { /* completely used? */
719 error = EADDRNOTAVAIL;
723 if (*lastport < first || *lastport > last)
725 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
727 lport = htons(*lastport);
728 } while (in_pcblookup_localremote(portinfo, inp->inp_laddr,
729 lport, sin->sin_addr, sin->sin_port, cred));
732 /* This could happen on loopback interface */
733 if (sin->sin_port == lport &&
734 sin->sin_addr.s_addr == inp->inp_laddr.s_addr) {
737 * Duplicate again; give up
739 error = EADDRNOTAVAIL;
745 inp->inp_lport = lport;
746 in_pcbinsporthash(portinfo, inp);
749 REL_PORT_TOKEN(portinfo);
752 /* Try next portinfo */
754 portinfo_idx &= pcbinfo->portinfo_mask;
755 if (portinfo_idx != portinfo_first)
757 inp->inp_laddr.s_addr = INADDR_ANY;
763 * Transform old in_pcbconnect() into an inner subroutine for new
764 * in_pcbconnect(): Do some validity-checking on the remote
765 * address (in mbuf 'nam') and then determine local host address
766 * (i.e., which interface) to use to access that remote host.
768 * This preserves definition of in_pcbconnect(), while supporting a
769 * slightly different version for T/TCP. (This is more than
770 * a bit of a kludge, but cleaning up the internal interfaces would
771 * have forced minor changes in every protocol).
774 in_pcbladdr_find(struct inpcb *inp, struct sockaddr *nam,
775 struct sockaddr_in **plocal_sin, struct thread *td, int find)
777 struct in_ifaddr *ia;
778 struct ucred *cred = NULL;
779 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
780 struct sockaddr *jsin;
781 int jailed = 0, alloc_route = 0;
783 if (nam->sa_len != sizeof *sin)
785 if (sin->sin_family != AF_INET)
786 return (EAFNOSUPPORT);
787 if (sin->sin_port == 0)
788 return (EADDRNOTAVAIL);
789 if (td && td->td_proc && td->td_proc->p_ucred)
790 cred = td->td_proc->p_ucred;
791 if (cred && cred->cr_prison)
793 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
794 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
796 * If the destination address is INADDR_ANY,
797 * use the primary local address.
798 * If the supplied address is INADDR_BROADCAST,
799 * and the primary interface supports broadcast,
800 * choose the broadcast address for that interface.
802 if (sin->sin_addr.s_addr == INADDR_ANY)
803 sin->sin_addr = IA_SIN(ia)->sin_addr;
804 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
805 (ia->ia_ifp->if_flags & IFF_BROADCAST))
806 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
813 * If route is known or can be allocated now,
814 * our src addr is taken from the i/f, else punt.
815 * Note that we should check the address family of the cached
816 * destination, in case of sharing the cache with IPv6.
818 ro = &inp->inp_route;
820 (!(ro->ro_rt->rt_flags & RTF_UP) ||
821 ro->ro_dst.sa_family != AF_INET ||
822 satosin(&ro->ro_dst)->sin_addr.s_addr !=
823 sin->sin_addr.s_addr ||
824 inp->inp_socket->so_options & SO_DONTROUTE)) {
828 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
829 (ro->ro_rt == NULL ||
830 ro->ro_rt->rt_ifp == NULL)) {
831 /* No route yet, so try to acquire one */
832 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
833 ro->ro_dst.sa_family = AF_INET;
834 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
835 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
841 * If we found a route, use the address
842 * corresponding to the outgoing interface
843 * unless it is the loopback (in case a route
844 * to our address on another net goes to loopback).
847 !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
849 if (jailed_ip(cred->cr_prison,
850 ro->ro_rt->rt_ifa->ifa_addr)) {
851 ia = ifatoia(ro->ro_rt->rt_ifa);
854 ia = ifatoia(ro->ro_rt->rt_ifa);
858 u_short fport = sin->sin_port;
861 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
862 if (ia && jailed && !jailed_ip(cred->cr_prison,
863 sintosa(&ia->ia_addr)))
866 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
867 if (ia && jailed && !jailed_ip(cred->cr_prison,
868 sintosa(&ia->ia_addr)))
870 sin->sin_port = fport;
872 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
873 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
874 if (ia && jailed && !jailed_ip(cred->cr_prison,
875 sintosa(&ia->ia_addr)))
878 if (!jailed && ia == NULL)
882 * If the destination address is multicast and an outgoing
883 * interface has been set as a multicast option, use the
884 * address of that interface as our source address.
886 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
887 inp->inp_moptions != NULL) {
888 struct ip_moptions *imo;
891 imo = inp->inp_moptions;
892 if (imo->imo_multicast_ifp != NULL) {
893 struct in_ifaddr_container *iac;
895 ifp = imo->imo_multicast_ifp;
898 &in_ifaddrheads[mycpuid], ia_link) {
899 if (iac->ia->ia_ifp == ifp) {
909 * Don't do pcblookup call here; return interface in plocal_sin
910 * and exit to caller, that will do the lookup.
912 if (ia == NULL && jailed) {
913 if ((jsin = prison_get_nonlocal(
914 cred->cr_prison, AF_INET, NULL)) != NULL ||
915 (jsin = prison_get_local(
916 cred->cr_prison, AF_INET, NULL)) != NULL) {
917 *plocal_sin = satosin(jsin);
923 *plocal_sin = &ia->ia_addr;
929 struct route *ro = &inp->inp_route;
931 if (ro->ro_rt != NULL)
933 bzero(ro, sizeof(*ro));
935 return (EADDRNOTAVAIL);
939 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
940 struct sockaddr_in **plocal_sin, struct thread *td)
942 return in_pcbladdr_find(inp, nam, plocal_sin, td,
943 (inp->inp_laddr.s_addr == INADDR_ANY));
948 * Connect from a socket to a specified address.
949 * Both address and port must be specified in argument sin.
950 * If don't have a local address for this socket yet,
954 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
956 struct sockaddr_in *if_sin;
957 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
960 /* Call inner routine to assign local interface address. */
961 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
964 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
965 inp->inp_laddr.s_addr ?
966 inp->inp_laddr : if_sin->sin_addr,
967 inp->inp_lport, FALSE, NULL) != NULL) {
970 if (inp->inp_laddr.s_addr == INADDR_ANY) {
971 if (inp->inp_lport == 0) {
972 error = in_pcbbind(inp, NULL, td);
976 inp->inp_laddr = if_sin->sin_addr;
978 inp->inp_faddr = sin->sin_addr;
979 inp->inp_fport = sin->sin_port;
980 in_pcbinsconnhash(inp);
985 in_pcbdisconnect(struct inpcb *inp)
988 inp->inp_faddr.s_addr = INADDR_ANY;
990 in_pcbremconnhash(inp);
994 in_pcbdetach(struct inpcb *inp)
996 struct socket *so = inp->inp_socket;
997 struct inpcbinfo *ipi = inp->inp_pcbinfo;
1000 ipsec4_delete_pcbpolicy(inp);
1002 inp->inp_gencnt = ++ipi->ipi_gencnt;
1003 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
1004 in_pcbremlists(inp);
1006 sofree(so); /* remove pcb ref */
1007 if (inp->inp_options)
1008 m_free(inp->inp_options);
1009 if (inp->inp_route.ro_rt)
1010 rtfree(inp->inp_route.ro_rt);
1011 ip_freemoptions(inp->inp_moptions);
1017 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
1018 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
1019 * in struct pr_usrreqs, so that protocols can just reference then directly
1020 * without the need for a wrapper function. The socket must have a valid
1021 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
1022 * except through a kernel programming error, so it is acceptable to panic
1023 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
1024 * because there actually /is/ a programming error somewhere... XXX)
1027 in_setsockaddr(struct socket *so, struct sockaddr **nam)
1030 struct sockaddr_in *sin;
1033 * Do the malloc first in case it blocks.
1035 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1036 sin->sin_family = AF_INET;
1037 sin->sin_len = sizeof *sin;
1043 kfree(sin, M_SONAME);
1044 return (ECONNRESET);
1046 sin->sin_port = inp->inp_lport;
1047 sin->sin_addr = inp->inp_laddr;
1050 *nam = (struct sockaddr *)sin;
1055 in_setsockaddr_dispatch(netmsg_t msg)
1059 error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1060 lwkt_replymsg(&msg->lmsg, error);
1064 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
1067 struct sockaddr_in *sin;
1070 * Do the malloc first in case it blocks.
1072 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1073 sin->sin_family = AF_INET;
1074 sin->sin_len = sizeof *sin;
1080 kfree(sin, M_SONAME);
1081 return (ECONNRESET);
1083 sin->sin_port = inp->inp_fport;
1084 sin->sin_addr = inp->inp_faddr;
1087 *nam = (struct sockaddr *)sin;
1092 in_setpeeraddr_dispatch(netmsg_t msg)
1096 error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1097 lwkt_replymsg(&msg->lmsg, error);
1101 in_pcbnotifyall(struct inpcbhead *head, struct in_addr faddr, int err,
1102 void (*notify)(struct inpcb *, int))
1104 struct inpcb *inp, *ninp;
1107 * note: if INP_PLACEMARKER is set we must ignore the rest of
1108 * the structure and skip it.
1111 LIST_FOREACH_MUTABLE(inp, head, inp_list, ninp) {
1112 if (inp->inp_flags & INP_PLACEMARKER)
1115 if (!(inp->inp_vflag & INP_IPV4))
1118 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1119 inp->inp_socket == NULL)
1121 (*notify)(inp, err); /* can remove inp from list! */
1127 in_pcbpurgeif0(struct inpcb *head, struct ifnet *ifp)
1130 struct ip_moptions *imo;
1133 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
1134 if (inp->inp_flags & INP_PLACEMARKER)
1136 imo = inp->inp_moptions;
1137 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
1139 * Unselect the outgoing interface if it is being
1142 if (imo->imo_multicast_ifp == ifp)
1143 imo->imo_multicast_ifp = NULL;
1146 * Drop multicast group membership if we joined
1147 * through the interface being detached.
1149 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1151 if (imo->imo_membership[i]->inm_ifp == ifp) {
1152 in_delmulti(imo->imo_membership[i]);
1154 } else if (gap != 0)
1155 imo->imo_membership[i - gap] =
1156 imo->imo_membership[i];
1158 imo->imo_num_memberships -= gap;
1164 * Check for alternatives when higher level complains
1165 * about service problems. For now, invalidate cached
1166 * routing information. If the route was created dynamically
1167 * (by a redirect), time to try a default gateway again.
1170 in_losing(struct inpcb *inp)
1173 struct rt_addrinfo rtinfo;
1175 if ((rt = inp->inp_route.ro_rt)) {
1176 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1177 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1178 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1179 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1180 rtinfo.rti_flags = rt->rt_flags;
1181 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1182 if (rt->rt_flags & RTF_DYNAMIC) {
1183 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1184 rt_mask(rt), rt->rt_flags, NULL);
1186 inp->inp_route.ro_rt = NULL;
1189 * A new route can be allocated
1190 * the next time output is attempted.
1196 * After a routing change, flush old routing
1197 * and allocate a (hopefully) better one.
1200 in_rtchange(struct inpcb *inp, int err)
1202 if (inp->inp_route.ro_rt) {
1203 rtfree(inp->inp_route.ro_rt);
1204 inp->inp_route.ro_rt = NULL;
1206 * A new route can be allocated the next time
1207 * output is attempted.
1213 * Lookup a PCB based on the local address and port.
1216 in_pcblookup_local(struct inpcbportinfo *portinfo, struct in_addr laddr,
1217 u_int lport_arg, int wild_okay, struct ucred *cred)
1220 int matchwild = 3, wildcard;
1221 u_short lport = lport_arg;
1222 struct inpcbporthead *porthash;
1223 struct inpcbport *phd;
1224 struct inpcb *match = NULL;
1227 * If the porthashbase is shared across several cpus, it must
1230 ASSERT_PORT_TOKEN_HELD(portinfo);
1233 * Best fit PCB lookup.
1235 * First see if this local port is in use by looking on the
1238 porthash = &portinfo->porthashbase[
1239 INP_PCBPORTHASH(lport, portinfo->porthashmask)];
1240 LIST_FOREACH(phd, porthash, phd_hash) {
1241 if (phd->phd_port == lport)
1246 * Port is in use by one or more PCBs. Look for best
1249 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1252 if ((inp->inp_vflag & INP_IPV4) == 0)
1255 if (inp->inp_faddr.s_addr != INADDR_ANY)
1257 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1258 if (laddr.s_addr == INADDR_ANY)
1260 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1263 if (laddr.s_addr != INADDR_ANY)
1266 if (wildcard && !wild_okay)
1268 if (wildcard < matchwild &&
1271 inp->inp_socket->so_cred->cr_prison)) {
1273 matchwild = wildcard;
1274 if (matchwild == 0) {
1284 in_pcblocalgroup_last(const struct inpcbinfo *pcbinfo,
1285 const struct inpcb *inp)
1287 const struct inp_localgrphead *hdr;
1288 const struct inp_localgroup *grp;
1291 if (pcbinfo->localgrphashbase == NULL)
1294 hdr = &pcbinfo->localgrphashbase[
1295 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1297 LIST_FOREACH(grp, hdr, il_list) {
1298 if (grp->il_vflag == inp->inp_vflag &&
1299 grp->il_lport == inp->inp_lport &&
1300 memcmp(&grp->il_dependladdr,
1301 &inp->inp_inc.inc_ie.ie_dependladdr,
1302 sizeof(grp->il_dependladdr)) == 0) {
1306 if (grp == NULL || grp->il_inpcnt == 1)
1309 KASSERT(grp->il_inpcnt >= 2,
1310 ("invalid localgroup inp count %d", grp->il_inpcnt));
1311 for (i = 0; i < grp->il_inpcnt; ++i) {
1312 if (grp->il_inp[i] == inp) {
1313 int last = grp->il_inpcnt - 1;
1316 last = grp->il_inpcnt - 2;
1317 return grp->il_inp[last];
1323 static struct inpcb *
1324 inp_localgroup_lookup(const struct inpcbinfo *pcbinfo,
1325 struct in_addr laddr, uint16_t lport, uint32_t pkt_hash)
1327 struct inpcb *local_wild = NULL;
1328 const struct inp_localgrphead *hdr;
1329 const struct inp_localgroup *grp;
1331 hdr = &pcbinfo->localgrphashbase[
1332 INP_PCBLOCALGRPHASH(lport, pcbinfo->localgrphashmask)];
1333 #ifdef INP_LOCALGROUP_HASHTHR
1334 pkt_hash >>= ncpus2_shift;
1338 * Order of socket selection:
1343 * - Local group does not contain jailed sockets
1344 * - Local group does not contain IPv4 mapped INET6 wild sockets
1346 LIST_FOREACH(grp, hdr, il_list) {
1348 if (!(grp->il_vflag & INP_IPV4))
1351 if (grp->il_lport == lport) {
1354 #ifdef INP_LOCALGROUP_HASHTHR
1355 idx = pkt_hash / grp->il_factor;
1356 KASSERT(idx < grp->il_inpcnt && idx >= 0,
1357 ("invalid hash %04x, cnt %d or fact %d",
1358 pkt_hash, grp->il_inpcnt, grp->il_factor));
1361 * Modulo-N is used here, which greatly reduces
1362 * completion queue token contention, thus more
1363 * cpu time is saved.
1365 idx = pkt_hash % grp->il_inpcnt;
1368 if (grp->il_laddr.s_addr == laddr.s_addr)
1369 return grp->il_inp[idx];
1370 else if (grp->il_laddr.s_addr == INADDR_ANY)
1371 local_wild = grp->il_inp[idx];
1374 if (local_wild != NULL)
1380 * Lookup PCB in hash list.
1383 in_pcblookup_pkthash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1384 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1385 boolean_t wildcard, struct ifnet *ifp, const struct mbuf *m)
1387 struct inpcbhead *head;
1388 struct inpcb *inp, *jinp=NULL;
1389 u_short fport = fport_arg, lport = lport_arg;
1392 * First look for an exact match.
1394 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1395 laddr.s_addr, lport, pcbinfo->hashmask)];
1396 LIST_FOREACH(inp, head, inp_hash) {
1398 if (!(inp->inp_vflag & INP_IPV4))
1401 if (in_hosteq(inp->inp_faddr, faddr) &&
1402 in_hosteq(inp->inp_laddr, laddr) &&
1403 inp->inp_fport == fport && inp->inp_lport == lport) {
1405 if (inp->inp_socket == NULL ||
1406 inp->inp_socket->so_cred->cr_prison == NULL) {
1417 struct inpcb *local_wild = NULL;
1418 struct inpcb *jinp_wild = NULL;
1420 struct inpcb *local_wild_mapped = NULL;
1422 struct inpcontainer *ic;
1423 struct inpcontainerhead *chead;
1424 struct sockaddr_in jsin;
1428 * Check local group first
1430 if (pcbinfo->localgrphashbase != NULL &&
1431 m != NULL && (m->m_flags & M_HASH) &&
1432 !(ifp && ifp->if_type == IFT_FAITH)) {
1433 inp = inp_localgroup_lookup(pcbinfo,
1434 laddr, lport, m->m_pkthdr.hash);
1440 * Order of socket selection:
1441 * 1. non-jailed, non-wild.
1442 * 2. non-jailed, wild.
1443 * 3. jailed, non-wild.
1446 jsin.sin_family = AF_INET;
1447 chead = &pcbinfo->wildcardhashbase[
1448 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1449 LIST_FOREACH(ic, chead, ic_list) {
1451 jsin.sin_addr.s_addr = laddr.s_addr;
1453 if (!(inp->inp_vflag & INP_IPV4))
1456 if (inp->inp_socket != NULL)
1457 cred = inp->inp_socket->so_cred;
1460 if (cred != NULL && jailed(cred)) {
1464 if (!jailed_ip(cred->cr_prison,
1465 (struct sockaddr *)&jsin))
1468 if (inp->inp_lport == lport) {
1469 if (ifp && ifp->if_type == IFT_FAITH &&
1470 !(inp->inp_flags & INP_FAITH))
1472 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1473 if (cred != NULL && jailed(cred))
1478 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1480 if (INP_CHECK_SOCKAF(inp->inp_socket,
1482 local_wild_mapped = inp;
1493 if (local_wild != NULL)
1494 return (local_wild);
1496 if (local_wild_mapped != NULL)
1497 return (local_wild_mapped);
1511 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1512 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1513 boolean_t wildcard, struct ifnet *ifp)
1515 return in_pcblookup_pkthash(pcbinfo, faddr, fport_arg,
1516 laddr, lport_arg, wildcard, ifp, NULL);
1520 * Insert PCB into connection hash table.
1523 in_pcbinsconnhash(struct inpcb *inp)
1525 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1526 struct inpcbhead *bucket;
1527 u_int32_t hashkey_faddr, hashkey_laddr;
1530 if (inp->inp_vflag & INP_IPV6) {
1531 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1532 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1535 hashkey_faddr = inp->inp_faddr.s_addr;
1536 hashkey_laddr = inp->inp_laddr.s_addr;
1541 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1542 ("already on wildcardhash"));
1543 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1544 ("already on connhash"));
1545 inp->inp_flags |= INP_CONNECTED;
1548 * Insert into the connection hash table.
1550 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1551 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1552 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1556 * Remove PCB from connection hash table.
1559 in_pcbremconnhash(struct inpcb *inp)
1561 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1562 LIST_REMOVE(inp, inp_hash);
1563 inp->inp_flags &= ~INP_CONNECTED;
1567 * Insert PCB into port hash table.
1570 in_pcbinsporthash(struct inpcbportinfo *portinfo, struct inpcb *inp)
1572 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1573 struct inpcbporthead *pcbporthash;
1574 struct inpcbport *phd;
1577 * If the porthashbase is shared across several cpus, it must
1580 ASSERT_PORT_TOKEN_HELD(portinfo);
1583 * Insert into the port hash table.
1585 pcbporthash = &portinfo->porthashbase[
1586 INP_PCBPORTHASH(inp->inp_lport, portinfo->porthashmask)];
1588 /* Go through port list and look for a head for this lport. */
1589 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1590 if (phd->phd_port == inp->inp_lport)
1594 /* If none exists, use saved one and tack it on. */
1596 KKASSERT(pcbinfo->portsave != NULL);
1597 phd = pcbinfo->portsave;
1598 pcbinfo->portsave = NULL;
1599 phd->phd_port = inp->inp_lport;
1600 LIST_INIT(&phd->phd_pcblist);
1601 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1604 inp->inp_portinfo = portinfo;
1606 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1609 * Malloc one inpcbport for later use. It is safe to use
1610 * "wait" malloc here (port token would be released, if
1611 * malloc ever blocked), since all changes to the porthash
1614 if (pcbinfo->portsave == NULL) {
1615 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1616 M_PCB, M_INTWAIT | M_ZERO);
1621 in_pcbinsporthash_lport(struct inpcb *inp)
1623 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1624 struct inpcbportinfo *portinfo;
1627 /* Locate the proper portinfo based on lport */
1628 lport_ho = ntohs(inp->inp_lport);
1629 portinfo = &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
1630 KKASSERT((lport_ho & pcbinfo->portinfo_mask) == portinfo->offset);
1632 GET_PORT_TOKEN(portinfo);
1633 in_pcbinsporthash(portinfo, inp);
1634 REL_PORT_TOKEN(portinfo);
1637 static struct inp_localgroup *
1638 inp_localgroup_alloc(u_char vflag,
1639 uint16_t port, const union in_dependaddr *addr, int size)
1641 struct inp_localgroup *grp;
1643 grp = kmalloc(__offsetof(struct inp_localgroup, il_inp[size]),
1644 M_TEMP, M_INTWAIT | M_ZERO);
1645 grp->il_vflag = vflag;
1646 grp->il_lport = port;
1647 grp->il_dependladdr = *addr;
1648 grp->il_inpsiz = size;
1654 inp_localgroup_free(struct inp_localgroup *grp)
1660 inp_localgroup_destroy(struct inp_localgroup *grp)
1662 LIST_REMOVE(grp, il_list);
1663 inp_localgroup_free(grp);
1667 inp_localgroup_copy(struct inp_localgroup *grp,
1668 const struct inp_localgroup *old_grp)
1672 KASSERT(old_grp->il_inpcnt < grp->il_inpsiz,
1673 ("invalid new local group size %d and old local group count %d",
1674 grp->il_inpsiz, old_grp->il_inpcnt));
1675 for (i = 0; i < old_grp->il_inpcnt; ++i)
1676 grp->il_inp[i] = old_grp->il_inp[i];
1677 grp->il_inpcnt = old_grp->il_inpcnt;
1678 grp->il_factor = old_grp->il_factor;
1682 inp_localgroup_factor(struct inp_localgroup *grp)
1685 ((uint32_t)(0xffff >> ncpus2_shift) / grp->il_inpcnt) + 1;
1686 KASSERT(grp->il_factor != 0, ("invalid local group factor, "
1687 "ncpus2_shift %d, inpcnt %d", ncpus2_shift, grp->il_inpcnt));
1691 in_pcbinslocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1693 struct inp_localgrphead *hdr;
1694 struct inp_localgroup *grp, *grp_alloc = NULL;
1697 if (pcbinfo->localgrphashbase == NULL)
1701 * XXX don't allow jailed socket to join local group
1703 if (inp->inp_socket != NULL)
1704 cred = inp->inp_socket->so_cred;
1707 if (cred != NULL && jailed(cred))
1712 * XXX don't allow IPv4 mapped INET6 wild socket
1714 if ((inp->inp_vflag & INP_IPV4) &&
1715 inp->inp_laddr.s_addr == INADDR_ANY &&
1716 INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6))
1720 hdr = &pcbinfo->localgrphashbase[
1721 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1724 LIST_FOREACH(grp, hdr, il_list) {
1725 if (grp->il_vflag == inp->inp_vflag &&
1726 grp->il_lport == inp->inp_lport &&
1727 memcmp(&grp->il_dependladdr,
1728 &inp->inp_inc.inc_ie.ie_dependladdr,
1729 sizeof(grp->il_dependladdr)) == 0) {
1735 * Create a new local group
1737 if (grp_alloc == NULL) {
1738 grp_alloc = inp_localgroup_alloc(inp->inp_vflag,
1739 inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr,
1740 INP_LOCALGROUP_SIZMIN);
1742 * Local group allocation could block and the
1743 * local group w/ the same property might have
1744 * been added by others when we were blocked;
1749 /* Local group has been allocated; link it */
1752 LIST_INSERT_HEAD(hdr, grp, il_list);
1754 } else if (grp->il_inpcnt == grp->il_inpsiz) {
1755 if (grp->il_inpsiz >= INP_LOCALGROUP_SIZMAX) {
1756 static int limit_logged = 0;
1758 if (!limit_logged) {
1760 kprintf("local group port %d, "
1761 "limit reached\n", ntohs(grp->il_lport));
1763 if (grp_alloc != NULL) {
1765 * This would happen if the local group
1766 * w/ the same property was expanded when
1767 * our local group allocation blocked.
1769 inp_localgroup_free(grp_alloc);
1775 * Expand this local group
1777 if (grp_alloc == NULL ||
1778 grp->il_inpcnt >= grp_alloc->il_inpsiz) {
1779 if (grp_alloc != NULL)
1780 inp_localgroup_free(grp_alloc);
1781 grp_alloc = inp_localgroup_alloc(grp->il_vflag,
1782 grp->il_lport, &grp->il_dependladdr,
1783 grp->il_inpsiz * 2);
1785 * Local group allocation could block and the
1786 * local group w/ the same property might have
1787 * been expanded by others when we were blocked;
1794 * Save the old local group, link the new one, and then
1795 * destroy the old local group
1797 inp_localgroup_copy(grp_alloc, grp);
1798 LIST_INSERT_HEAD(hdr, grp_alloc, il_list);
1799 inp_localgroup_destroy(grp);
1805 * Found the local group
1807 if (grp_alloc != NULL) {
1809 * This would happen if the local group w/ the
1810 * same property was added or expanded when our
1811 * local group allocation blocked.
1813 inp_localgroup_free(grp_alloc);
1818 KASSERT(grp->il_inpcnt < grp->il_inpsiz,
1819 ("invalid local group size %d and count %d",
1820 grp->il_inpsiz, grp->il_inpcnt));
1821 grp->il_inp[grp->il_inpcnt] = inp;
1823 inp_localgroup_factor(grp);
1827 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1829 struct inpcontainer *ic;
1830 struct inpcontainerhead *bucket;
1832 in_pcbinslocalgrphash_oncpu(inp, pcbinfo);
1834 bucket = &pcbinfo->wildcardhashbase[
1835 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1837 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1839 LIST_INSERT_HEAD(bucket, ic, ic_list);
1843 * Insert PCB into wildcard hash table.
1846 in_pcbinswildcardhash(struct inpcb *inp)
1848 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1850 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1851 ("already on connhash"));
1852 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1853 ("already on wildcardhash"));
1854 inp->inp_flags |= INP_WILDCARD;
1856 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1860 in_pcbremlocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1862 struct inp_localgrphead *hdr;
1863 struct inp_localgroup *grp;
1865 if (pcbinfo->localgrphashbase == NULL)
1868 hdr = &pcbinfo->localgrphashbase[
1869 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1871 LIST_FOREACH(grp, hdr, il_list) {
1874 for (i = 0; i < grp->il_inpcnt; ++i) {
1875 if (grp->il_inp[i] != inp)
1878 if (grp->il_inpcnt == 1) {
1879 /* Destroy this local group */
1880 inp_localgroup_destroy(grp);
1882 /* Pull up inpcbs */
1883 for (; i + 1 < grp->il_inpcnt; ++i)
1884 grp->il_inp[i] = grp->il_inp[i + 1];
1886 inp_localgroup_factor(grp);
1894 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1896 struct inpcontainer *ic;
1897 struct inpcontainerhead *head;
1899 in_pcbremlocalgrphash_oncpu(inp, pcbinfo);
1902 head = &pcbinfo->wildcardhashbase[
1903 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1905 LIST_FOREACH(ic, head, ic_list) {
1906 if (ic->ic_inp == inp)
1909 return; /* not found! */
1912 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
1913 kfree(ic, M_TEMP); /* deallocate container */
1917 * Remove PCB from wildcard hash table.
1920 in_pcbremwildcardhash(struct inpcb *inp)
1922 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1924 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1925 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
1926 inp->inp_flags &= ~INP_WILDCARD;
1930 * Remove PCB from various lists.
1933 in_pcbremlists(struct inpcb *inp)
1935 if (inp->inp_lport) {
1936 struct inpcbportinfo *portinfo;
1937 struct inpcbport *phd;
1941 * inp->inp_portinfo is _not_ necessary same as
1942 * inp->inp_pcbinfo->portinfo.
1944 portinfo = inp->inp_portinfo;
1945 GET_PORT_TOKEN(portinfo);
1948 LIST_REMOVE(inp, inp_portlist);
1949 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1950 LIST_REMOVE(phd, phd_hash);
1954 REL_PORT_TOKEN(portinfo);
1956 if (inp->inp_flags & INP_WILDCARD) {
1957 in_pcbremwildcardhash(inp);
1958 } else if (inp->inp_flags & INP_CONNECTED) {
1959 in_pcbremconnhash(inp);
1961 LIST_REMOVE(inp, inp_list);
1962 inp->inp_pcbinfo->ipi_count--;
1966 prison_xinpcb(struct thread *td, struct inpcb *inp)
1970 if (td->td_proc == NULL)
1972 cr = td->td_proc->p_ucred;
1973 if (cr->cr_prison == NULL)
1975 if (inp->inp_socket && inp->inp_socket->so_cred &&
1976 inp->inp_socket->so_cred->cr_prison &&
1977 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
1983 in_pcblist_global(SYSCTL_HANDLER_ARGS)
1985 struct inpcbinfo *pcbinfo = arg1;
1986 struct inpcb *inp, *marker;
1991 * The process of preparing the TCB list is too time-consuming and
1992 * resource-intensive to repeat twice on every request.
1994 if (req->oldptr == NULL) {
1995 n = pcbinfo->ipi_count;
1996 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
2000 if (req->newptr != NULL)
2004 * OK, now we're committed to doing something. Re-fetch ipi_count
2005 * after obtaining the generation count.
2007 n = pcbinfo->ipi_count;
2009 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
2010 marker->inp_flags |= INP_PLACEMARKER;
2011 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
2016 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
2017 LIST_REMOVE(marker, inp_list);
2018 LIST_INSERT_AFTER(inp, marker, inp_list);
2020 if (inp->inp_flags & INP_PLACEMARKER)
2022 if (prison_xinpcb(req->td, inp))
2024 bzero(&xi, sizeof xi);
2025 xi.xi_len = sizeof xi;
2026 bcopy(inp, &xi.xi_inp, sizeof *inp);
2027 if (inp->inp_socket)
2028 sotoxsocket(inp->inp_socket, &xi.xi_socket);
2029 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
2033 LIST_REMOVE(marker, inp_list);
2034 if (error == 0 && i < n) {
2035 bzero(&xi, sizeof xi);
2036 xi.xi_len = sizeof xi;
2038 error = SYSCTL_OUT(req, &xi, sizeof xi);
2042 kfree(marker, M_TEMP);
2047 in_pcblist_global_cpu0(SYSCTL_HANDLER_ARGS)
2049 boolean_t migrate = FALSE;
2050 int origcpu = mycpuid;
2058 error = in_pcblist_global(oidp, arg1, arg2, req);
2061 lwkt_migratecpu(origcpu);
2066 in_pcblist_global_nomarker(SYSCTL_HANDLER_ARGS, struct xinpcb **xi0, int *nxi0)
2068 struct inpcbinfo *pcbinfo = arg1;
2077 * The process of preparing the PCB list is too time-consuming and
2078 * resource-intensive to repeat twice on every request.
2080 if (req->oldptr == NULL) {
2081 int n = pcbinfo->ipi_count;
2083 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
2087 if (req->newptr != NULL)
2090 if (pcbinfo->ipi_count == 0)
2094 xi = kmalloc(pcbinfo->ipi_count * sizeof(*xi), M_TEMP,
2095 M_WAITOK | M_ZERO | M_NULLOK);
2099 LIST_FOREACH(inp, &pcbinfo->pcblisthead, inp_list) {
2100 struct xinpcb *xi_ptr = &xi[nxi];
2102 if (prison_xinpcb(req->td, inp))
2105 xi_ptr->xi_len = sizeof(*xi_ptr);
2106 bcopy(inp, &xi_ptr->xi_inp, sizeof(*inp));
2107 if (inp->inp_socket)
2108 sotoxsocket(inp->inp_socket, &xi_ptr->xi_socket);
2124 in_savefaddr(struct socket *so, const struct sockaddr *faddr)
2126 struct sockaddr_in *sin;
2128 KASSERT(faddr->sa_family == AF_INET,
2129 ("not AF_INET faddr %d", faddr->sa_family));
2131 sin = kmalloc(sizeof(*sin), M_SONAME, M_WAITOK | M_ZERO);
2132 sin->sin_family = AF_INET;
2133 sin->sin_len = sizeof(*sin);
2134 sin->sin_port = ((const struct sockaddr_in *)faddr)->sin_port;
2135 sin->sin_addr = ((const struct sockaddr_in *)faddr)->sin_addr;
2137 so->so_faddr = (struct sockaddr *)sin;
2141 in_pcbportinfo_init(struct inpcbportinfo *portinfo, int hashsize,
2142 boolean_t shared, u_short offset)
2144 memset(portinfo, 0, sizeof(*portinfo));
2146 portinfo->offset = offset;
2147 portinfo->lastport = offset;
2148 portinfo->lastlow = offset;
2149 portinfo->lasthi = offset;
2151 portinfo->porthashbase = hashinit(hashsize, M_PCB,
2152 &portinfo->porthashmask);
2155 portinfo->porttoken = kmalloc(sizeof(struct lwkt_token),
2157 lwkt_token_init(portinfo->porttoken, "porttoken");
2162 in_pcbportrange(u_short *hi0, u_short *lo0, u_short ofs, u_short step)
2172 hi = rounddown2(hi, step);
2177 lo = roundup2(lo, step);