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 will be non-NULL.
338 if (portinfo->porttoken)
339 lwkt_gettoken(portinfo->porttoken);
342 * Simple check to ensure all ports are not used up causing
345 * We split the two cases (up and down) so that the direction
346 * is not being tested on each round of the loop.
352 in_pcbportrange(&first, &last, portinfo->offset, step);
353 count = (first - last) / step;
356 if (count-- < 0) { /* completely used? */
357 error = EADDRNOTAVAIL;
361 if (*lastport > first || *lastport < last)
363 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
365 lport = htons(*lastport);
366 } while (in_pcblookup_local(portinfo, inp->inp_laddr, lport,
372 in_pcbportrange(&last, &first, portinfo->offset, step);
373 count = (last - first) / step;
376 if (count-- < 0) { /* completely used? */
377 error = EADDRNOTAVAIL;
381 if (*lastport < first || *lastport > last)
383 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
385 lport = htons(*lastport);
386 } while (in_pcblookup_local(portinfo, inp->inp_laddr, lport,
389 inp->inp_lport = lport;
390 in_pcbinsporthash(portinfo, inp);
393 if (portinfo->porttoken)
394 lwkt_reltoken(portinfo->porttoken);
397 /* Try next portinfo */
399 portinfo_idx &= pcbinfo->portinfo_mask;
400 if (portinfo_idx != portinfo_first)
402 inp->inp_laddr.s_addr = INADDR_ANY;
408 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
410 struct socket *so = inp->inp_socket;
411 struct sockaddr_in jsin;
412 struct ucred *cred = NULL;
415 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
416 return (EADDRNOTAVAIL);
417 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
418 return (EINVAL); /* already bound */
420 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
421 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
423 cred = td->td_proc->p_ucred;
426 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
427 struct inpcbinfo *pcbinfo;
428 struct inpcbportinfo *portinfo;
430 u_short lport, lport_ho;
431 int reuseport = (so->so_options & SO_REUSEPORT);
434 if (nam->sa_len != sizeof *sin)
438 * We should check the family, but old programs
439 * incorrectly fail to initialize it.
441 if (sin->sin_family != AF_INET)
442 return (EAFNOSUPPORT);
444 if (!prison_replace_wildcards(td, nam))
447 lport = sin->sin_port;
448 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
450 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
451 * allow complete duplication of binding if
452 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
453 * and a multicast address is bound on both
454 * new and duplicated sockets.
456 if (so->so_options & SO_REUSEADDR)
457 reuseport = SO_REUSEADDR | SO_REUSEPORT;
458 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
459 sin->sin_port = 0; /* yech... */
460 bzero(&sin->sin_zero, sizeof sin->sin_zero);
461 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
462 return (EADDRNOTAVAIL);
465 inp->inp_laddr = sin->sin_addr;
467 jsin.sin_family = AF_INET;
468 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
469 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
470 inp->inp_laddr.s_addr = INADDR_ANY;
473 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
476 /* Auto-select local port */
477 return in_pcbsetlport(inp, wild, cred);
479 lport_ho = ntohs(lport);
482 if (lport_ho < IPPORT_RESERVED && cred &&
484 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
485 inp->inp_laddr.s_addr = INADDR_ANY;
490 * Locate the proper portinfo based on lport
492 pcbinfo = inp->inp_pcbinfo;
494 &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
495 KKASSERT((lport_ho & pcbinfo->portinfo_mask) ==
499 * This has to be atomic. If the porthash is shared across
500 * multiple protocol threads (aka tcp) then the token will
503 if (portinfo->porttoken)
504 lwkt_gettoken(portinfo->porttoken);
506 if (so->so_cred->cr_uid != 0 &&
507 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
508 t = in_pcblookup_local(portinfo, sin->sin_addr, lport,
509 INPLOOKUP_WILDCARD, cred);
511 (!in_nullhost(sin->sin_addr) ||
512 !in_nullhost(t->inp_laddr) ||
513 (t->inp_socket->so_options & SO_REUSEPORT) == 0) &&
514 (so->so_cred->cr_uid !=
515 t->inp_socket->so_cred->cr_uid)) {
517 if (!in_nullhost(sin->sin_addr) ||
518 !in_nullhost(t->inp_laddr) ||
519 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
522 inp->inp_laddr.s_addr = INADDR_ANY;
528 if (cred && !prison_replace_wildcards(td, nam)) {
529 inp->inp_laddr.s_addr = INADDR_ANY;
530 error = EADDRNOTAVAIL;
533 t = in_pcblookup_local(portinfo, sin->sin_addr, lport,
535 if (t && !(reuseport & t->inp_socket->so_options)) {
537 if (!in_nullhost(sin->sin_addr) ||
538 !in_nullhost(t->inp_laddr) ||
539 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
542 inp->inp_laddr.s_addr = INADDR_ANY;
547 inp->inp_lport = lport;
548 in_pcbinsporthash(portinfo, inp);
551 if (portinfo->porttoken)
552 lwkt_reltoken(portinfo->porttoken);
555 jsin.sin_family = AF_INET;
556 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
557 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
558 inp->inp_laddr.s_addr = INADDR_ANY;
561 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
563 return in_pcbsetlport(inp, wild, cred);
567 static struct inpcb *
568 in_pcblookup_localremote(struct inpcbportinfo *portinfo, struct in_addr laddr,
569 u_short lport, struct in_addr faddr, u_short fport, struct ucred *cred)
572 struct inpcbporthead *porthash;
573 struct inpcbport *phd;
574 struct inpcb *match = NULL;
577 * If the porthashbase is shared across several cpus, it must
580 if (portinfo->porttoken)
581 ASSERT_LWKT_TOKEN_HELD(portinfo->porttoken);
584 * Best fit PCB lookup.
586 * First see if this local port is in use by looking on the
589 porthash = &portinfo->porthashbase[
590 INP_PCBPORTHASH(lport, portinfo->porthashmask)];
591 LIST_FOREACH(phd, porthash, phd_hash) {
592 if (phd->phd_port == lport)
596 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
598 if ((inp->inp_vflag & INP_IPV4) == 0)
601 if (inp->inp_laddr.s_addr != INADDR_ANY &&
602 inp->inp_laddr.s_addr != laddr.s_addr)
605 if (inp->inp_faddr.s_addr != INADDR_ANY &&
606 inp->inp_faddr.s_addr != faddr.s_addr)
609 if (inp->inp_fport != 0 && inp->inp_fport != fport)
614 inp->inp_socket->so_cred->cr_prison) {
624 in_pcbsetlport_remote(struct inpcb *inp, const struct sockaddr *remote,
627 struct proc *p = td->td_proc;
628 unsigned short *lastport;
629 const struct sockaddr_in *sin = (const struct sockaddr_in *)remote;
630 struct sockaddr_in jsin;
631 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
632 struct inpcbportinfo *portinfo;
633 struct ucred *cred = NULL;
634 u_short first, last, lport, step;
635 int count, error, dup;
636 int portinfo_first, portinfo_idx;
638 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
639 return (EADDRNOTAVAIL);
641 KKASSERT(inp->inp_laddr.s_addr != INADDR_ANY);
642 if (inp->inp_lport != 0)
643 return (EINVAL); /* already bound */
648 jsin.sin_family = AF_INET;
649 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
650 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
651 inp->inp_laddr.s_addr = INADDR_ANY;
654 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
656 inp->inp_flags |= INP_ANONPORT;
658 step = pcbinfo->portinfo_mask + 1;
659 portinfo_first = mycpuid & pcbinfo->portinfo_mask;
660 portinfo_idx = portinfo_first;
662 portinfo = &pcbinfo->portinfo[portinfo_idx];
665 if (inp->inp_flags & INP_HIGHPORT) {
666 first = ipport_hifirstauto; /* sysctl */
667 last = ipport_hilastauto;
668 lastport = &portinfo->lasthi;
669 } else if (inp->inp_flags & INP_LOWPORT) {
672 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
673 inp->inp_laddr.s_addr = INADDR_ANY;
676 first = ipport_lowfirstauto; /* 1023 */
677 last = ipport_lowlastauto; /* 600 */
678 lastport = &portinfo->lastlow;
680 first = ipport_firstauto; /* sysctl */
681 last = ipport_lastauto;
682 lastport = &portinfo->lastport;
686 * This has to be atomic. If the porthash is shared across multiple
687 * protocol threads (aka tcp) then the token will be non-NULL.
689 if (portinfo->porttoken)
690 lwkt_gettoken(portinfo->porttoken);
694 * Simple check to ensure all ports are not used up causing
697 * We split the two cases (up and down) so that the direction
698 * is not being tested on each round of the loop.
704 in_pcbportrange(&first, &last, portinfo->offset, step);
705 count = (first - last) / step;
708 if (count-- < 0) { /* completely used? */
709 error = EADDRNOTAVAIL;
713 if (*lastport > first || *lastport < last)
715 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
717 lport = htons(*lastport);
718 } while (in_pcblookup_localremote(portinfo, inp->inp_laddr,
719 lport, sin->sin_addr, sin->sin_port, cred));
724 in_pcbportrange(&last, &first, portinfo->offset, step);
725 count = (last - first) / step;
728 if (count-- < 0) { /* completely used? */
729 error = EADDRNOTAVAIL;
733 if (*lastport < first || *lastport > last)
735 KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
737 lport = htons(*lastport);
738 } while (in_pcblookup_localremote(portinfo, inp->inp_laddr,
739 lport, sin->sin_addr, sin->sin_port, cred));
742 /* This could happen on loopback interface */
743 if (sin->sin_port == lport &&
744 sin->sin_addr.s_addr == inp->inp_laddr.s_addr) {
747 * Duplicate again; give up
749 error = EADDRNOTAVAIL;
755 inp->inp_lport = lport;
756 in_pcbinsporthash(portinfo, inp);
759 if (portinfo->porttoken)
760 lwkt_reltoken(portinfo->porttoken);
763 /* Try next portinfo */
765 portinfo_idx &= pcbinfo->portinfo_mask;
766 if (portinfo_idx != portinfo_first)
768 inp->inp_laddr.s_addr = INADDR_ANY;
774 * Transform old in_pcbconnect() into an inner subroutine for new
775 * in_pcbconnect(): Do some validity-checking on the remote
776 * address (in mbuf 'nam') and then determine local host address
777 * (i.e., which interface) to use to access that remote host.
779 * This preserves definition of in_pcbconnect(), while supporting a
780 * slightly different version for T/TCP. (This is more than
781 * a bit of a kludge, but cleaning up the internal interfaces would
782 * have forced minor changes in every protocol).
785 in_pcbladdr_find(struct inpcb *inp, struct sockaddr *nam,
786 struct sockaddr_in **plocal_sin, struct thread *td, int find)
788 struct in_ifaddr *ia;
789 struct ucred *cred = NULL;
790 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
791 struct sockaddr *jsin;
792 int jailed = 0, alloc_route = 0;
794 if (nam->sa_len != sizeof *sin)
796 if (sin->sin_family != AF_INET)
797 return (EAFNOSUPPORT);
798 if (sin->sin_port == 0)
799 return (EADDRNOTAVAIL);
800 if (td && td->td_proc && td->td_proc->p_ucred)
801 cred = td->td_proc->p_ucred;
802 if (cred && cred->cr_prison)
804 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
805 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
807 * If the destination address is INADDR_ANY,
808 * use the primary local address.
809 * If the supplied address is INADDR_BROADCAST,
810 * and the primary interface supports broadcast,
811 * choose the broadcast address for that interface.
813 if (sin->sin_addr.s_addr == INADDR_ANY)
814 sin->sin_addr = IA_SIN(ia)->sin_addr;
815 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
816 (ia->ia_ifp->if_flags & IFF_BROADCAST))
817 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
824 * If route is known or can be allocated now,
825 * our src addr is taken from the i/f, else punt.
826 * Note that we should check the address family of the cached
827 * destination, in case of sharing the cache with IPv6.
829 ro = &inp->inp_route;
831 (!(ro->ro_rt->rt_flags & RTF_UP) ||
832 ro->ro_dst.sa_family != AF_INET ||
833 satosin(&ro->ro_dst)->sin_addr.s_addr !=
834 sin->sin_addr.s_addr ||
835 inp->inp_socket->so_options & SO_DONTROUTE)) {
839 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
840 (ro->ro_rt == NULL ||
841 ro->ro_rt->rt_ifp == NULL)) {
842 /* No route yet, so try to acquire one */
843 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
844 ro->ro_dst.sa_family = AF_INET;
845 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
846 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
852 * If we found a route, use the address
853 * corresponding to the outgoing interface
854 * unless it is the loopback (in case a route
855 * to our address on another net goes to loopback).
857 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
859 if (jailed_ip(cred->cr_prison,
860 ro->ro_rt->rt_ifa->ifa_addr)) {
861 ia = ifatoia(ro->ro_rt->rt_ifa);
864 ia = ifatoia(ro->ro_rt->rt_ifa);
868 u_short fport = sin->sin_port;
871 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
872 if (ia && jailed && !jailed_ip(cred->cr_prison,
873 sintosa(&ia->ia_addr)))
876 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
877 if (ia && jailed && !jailed_ip(cred->cr_prison,
878 sintosa(&ia->ia_addr)))
880 sin->sin_port = fport;
882 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
883 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
884 if (ia && jailed && !jailed_ip(cred->cr_prison,
885 sintosa(&ia->ia_addr)))
888 if (!jailed && ia == NULL)
892 * If the destination address is multicast and an outgoing
893 * interface has been set as a multicast option, use the
894 * address of that interface as our source address.
896 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
897 inp->inp_moptions != NULL) {
898 struct ip_moptions *imo;
901 imo = inp->inp_moptions;
902 if (imo->imo_multicast_ifp != NULL) {
903 struct in_ifaddr_container *iac;
905 ifp = imo->imo_multicast_ifp;
908 &in_ifaddrheads[mycpuid], ia_link) {
909 if (iac->ia->ia_ifp == ifp) {
919 * Don't do pcblookup call here; return interface in plocal_sin
920 * and exit to caller, that will do the lookup.
922 if (ia == NULL && jailed) {
923 if ((jsin = prison_get_nonlocal(cred->cr_prison, AF_INET, NULL)) != NULL ||
924 (jsin = prison_get_local(cred->cr_prison, AF_INET, NULL)) != NULL) {
925 *plocal_sin = satosin(jsin);
931 *plocal_sin = &ia->ia_addr;
937 struct route *ro = &inp->inp_route;
939 if (ro->ro_rt != NULL)
941 bzero(ro, sizeof(*ro));
943 return (EADDRNOTAVAIL);
947 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
948 struct sockaddr_in **plocal_sin, struct thread *td)
950 return in_pcbladdr_find(inp, nam, plocal_sin, td,
951 (inp->inp_laddr.s_addr == INADDR_ANY));
956 * Connect from a socket to a specified address.
957 * Both address and port must be specified in argument sin.
958 * If don't have a local address for this socket yet,
962 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
964 struct sockaddr_in *if_sin;
965 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
968 /* Call inner routine to assign local interface address. */
969 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
972 if (in_pcblookup_hash(inp->inp_cpcbinfo, sin->sin_addr, sin->sin_port,
973 inp->inp_laddr.s_addr ?
974 inp->inp_laddr : if_sin->sin_addr,
975 inp->inp_lport, FALSE, NULL) != NULL) {
978 if (inp->inp_laddr.s_addr == INADDR_ANY) {
979 if (inp->inp_lport == 0) {
980 error = in_pcbbind(inp, NULL, td);
984 inp->inp_laddr = if_sin->sin_addr;
986 inp->inp_faddr = sin->sin_addr;
987 inp->inp_fport = sin->sin_port;
988 in_pcbinsconnhash(inp);
993 in_pcbdisconnect(struct inpcb *inp)
996 inp->inp_faddr.s_addr = INADDR_ANY;
998 in_pcbremconnhash(inp);
999 if (inp->inp_socket->so_state & SS_NOFDREF)
1004 in_pcbdetach(struct inpcb *inp)
1006 struct socket *so = inp->inp_socket;
1007 struct inpcbinfo *ipi = inp->inp_pcbinfo;
1010 ipsec4_delete_pcbpolicy(inp);
1012 inp->inp_gencnt = ++ipi->ipi_gencnt;
1013 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
1014 in_pcbremlists(inp);
1016 sofree(so); /* remove pcb ref */
1017 if (inp->inp_options)
1018 m_free(inp->inp_options);
1019 if (inp->inp_route.ro_rt)
1020 rtfree(inp->inp_route.ro_rt);
1021 ip_freemoptions(inp->inp_moptions);
1027 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
1028 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
1029 * in struct pr_usrreqs, so that protocols can just reference then directly
1030 * without the need for a wrapper function. The socket must have a valid
1031 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
1032 * except through a kernel programming error, so it is acceptable to panic
1033 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
1034 * because there actually /is/ a programming error somewhere... XXX)
1037 in_setsockaddr(struct socket *so, struct sockaddr **nam)
1040 struct sockaddr_in *sin;
1043 * Do the malloc first in case it blocks.
1045 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1046 sin->sin_family = AF_INET;
1047 sin->sin_len = sizeof *sin;
1053 kfree(sin, M_SONAME);
1054 return (ECONNRESET);
1056 sin->sin_port = inp->inp_lport;
1057 sin->sin_addr = inp->inp_laddr;
1060 *nam = (struct sockaddr *)sin;
1065 in_setsockaddr_dispatch(netmsg_t msg)
1069 error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1070 lwkt_replymsg(&msg->lmsg, error);
1074 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
1077 struct sockaddr_in *sin;
1080 * Do the malloc first in case it blocks.
1082 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1083 sin->sin_family = AF_INET;
1084 sin->sin_len = sizeof *sin;
1090 kfree(sin, M_SONAME);
1091 return (ECONNRESET);
1093 sin->sin_port = inp->inp_fport;
1094 sin->sin_addr = inp->inp_faddr;
1097 *nam = (struct sockaddr *)sin;
1102 in_setpeeraddr_dispatch(netmsg_t msg)
1106 error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1107 lwkt_replymsg(&msg->lmsg, error);
1111 in_pcbnotifyall(struct inpcbhead *head, struct in_addr faddr, int err,
1112 void (*notify)(struct inpcb *, int))
1114 struct inpcb *inp, *ninp;
1117 * note: if INP_PLACEMARKER is set we must ignore the rest of
1118 * the structure and skip it.
1121 LIST_FOREACH_MUTABLE(inp, head, inp_list, ninp) {
1122 if (inp->inp_flags & INP_PLACEMARKER)
1125 if (!(inp->inp_vflag & INP_IPV4))
1128 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1129 inp->inp_socket == NULL)
1131 (*notify)(inp, err); /* can remove inp from list! */
1137 in_pcbpurgeif0(struct inpcb *head, struct ifnet *ifp)
1140 struct ip_moptions *imo;
1143 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
1144 if (inp->inp_flags & INP_PLACEMARKER)
1146 imo = inp->inp_moptions;
1147 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
1149 * Unselect the outgoing interface if it is being
1152 if (imo->imo_multicast_ifp == ifp)
1153 imo->imo_multicast_ifp = NULL;
1156 * Drop multicast group membership if we joined
1157 * through the interface being detached.
1159 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1161 if (imo->imo_membership[i]->inm_ifp == ifp) {
1162 in_delmulti(imo->imo_membership[i]);
1164 } else if (gap != 0)
1165 imo->imo_membership[i - gap] =
1166 imo->imo_membership[i];
1168 imo->imo_num_memberships -= gap;
1174 * Check for alternatives when higher level complains
1175 * about service problems. For now, invalidate cached
1176 * routing information. If the route was created dynamically
1177 * (by a redirect), time to try a default gateway again.
1180 in_losing(struct inpcb *inp)
1183 struct rt_addrinfo rtinfo;
1185 if ((rt = inp->inp_route.ro_rt)) {
1186 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1187 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1188 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1189 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1190 rtinfo.rti_flags = rt->rt_flags;
1191 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1192 if (rt->rt_flags & RTF_DYNAMIC) {
1193 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1194 rt_mask(rt), rt->rt_flags, NULL);
1196 inp->inp_route.ro_rt = NULL;
1199 * A new route can be allocated
1200 * the next time output is attempted.
1206 * After a routing change, flush old routing
1207 * and allocate a (hopefully) better one.
1210 in_rtchange(struct inpcb *inp, int err)
1212 if (inp->inp_route.ro_rt) {
1213 rtfree(inp->inp_route.ro_rt);
1214 inp->inp_route.ro_rt = NULL;
1216 * A new route can be allocated the next time
1217 * output is attempted.
1223 * Lookup a PCB based on the local address and port.
1226 in_pcblookup_local(struct inpcbportinfo *portinfo, struct in_addr laddr,
1227 u_int lport_arg, int wild_okay, struct ucred *cred)
1230 int matchwild = 3, wildcard;
1231 u_short lport = lport_arg;
1232 struct inpcbporthead *porthash;
1233 struct inpcbport *phd;
1234 struct inpcb *match = NULL;
1237 * If the porthashbase is shared across several cpus, it must
1240 if (portinfo->porttoken)
1241 ASSERT_LWKT_TOKEN_HELD(portinfo->porttoken);
1244 * Best fit PCB lookup.
1246 * First see if this local port is in use by looking on the
1249 porthash = &portinfo->porthashbase[
1250 INP_PCBPORTHASH(lport, portinfo->porthashmask)];
1251 LIST_FOREACH(phd, porthash, phd_hash) {
1252 if (phd->phd_port == lport)
1257 * Port is in use by one or more PCBs. Look for best
1260 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1263 if ((inp->inp_vflag & INP_IPV4) == 0)
1266 if (inp->inp_faddr.s_addr != INADDR_ANY)
1268 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1269 if (laddr.s_addr == INADDR_ANY)
1271 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1274 if (laddr.s_addr != INADDR_ANY)
1277 if (wildcard && !wild_okay)
1279 if (wildcard < matchwild &&
1282 inp->inp_socket->so_cred->cr_prison)) {
1284 matchwild = wildcard;
1285 if (matchwild == 0) {
1295 in_pcblocalgroup_last(const struct inpcbinfo *pcbinfo,
1296 const struct inpcb *inp)
1298 const struct inp_localgrphead *hdr;
1299 const struct inp_localgroup *grp;
1302 if (pcbinfo->localgrphashbase == NULL)
1305 hdr = &pcbinfo->localgrphashbase[
1306 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1308 LIST_FOREACH(grp, hdr, il_list) {
1309 if (grp->il_vflag == inp->inp_vflag &&
1310 grp->il_lport == inp->inp_lport &&
1311 memcmp(&grp->il_dependladdr,
1312 &inp->inp_inc.inc_ie.ie_dependladdr,
1313 sizeof(grp->il_dependladdr)) == 0) {
1317 if (grp == NULL || grp->il_inpcnt == 1)
1320 KASSERT(grp->il_inpcnt >= 2,
1321 ("invalid localgroup inp count %d", grp->il_inpcnt));
1322 for (i = 0; i < grp->il_inpcnt; ++i) {
1323 if (grp->il_inp[i] == inp) {
1324 int last = grp->il_inpcnt - 1;
1327 last = grp->il_inpcnt - 2;
1328 return grp->il_inp[last];
1334 static struct inpcb *
1335 inp_localgroup_lookup(const struct inpcbinfo *pcbinfo,
1336 struct in_addr laddr, uint16_t lport, uint32_t pkt_hash)
1338 struct inpcb *local_wild = NULL;
1339 const struct inp_localgrphead *hdr;
1340 const struct inp_localgroup *grp;
1342 hdr = &pcbinfo->localgrphashbase[
1343 INP_PCBLOCALGRPHASH(lport, pcbinfo->localgrphashmask)];
1344 #ifdef INP_LOCALGROUP_HASHTHR
1345 pkt_hash >>= ncpus2_shift;
1349 * Order of socket selection:
1354 * - Local group does not contain jailed sockets
1355 * - Local group does not contain IPv4 mapped INET6 wild sockets
1357 LIST_FOREACH(grp, hdr, il_list) {
1359 if (!(grp->il_vflag & INP_IPV4))
1362 if (grp->il_lport == lport) {
1365 #ifdef INP_LOCALGROUP_HASHTHR
1366 idx = pkt_hash / grp->il_factor;
1367 KASSERT(idx < grp->il_inpcnt && idx >= 0,
1368 ("invalid hash %04x, cnt %d or fact %d",
1369 pkt_hash, grp->il_inpcnt, grp->il_factor));
1372 * Modulo-N is used here, which greatly reduces
1373 * completion queue token contention, thus more
1374 * cpu time is saved.
1376 idx = pkt_hash % grp->il_inpcnt;
1379 if (grp->il_laddr.s_addr == laddr.s_addr)
1380 return grp->il_inp[idx];
1381 else if (grp->il_laddr.s_addr == INADDR_ANY)
1382 local_wild = grp->il_inp[idx];
1385 if (local_wild != NULL)
1391 * Lookup PCB in hash list.
1394 in_pcblookup_pkthash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1395 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1396 boolean_t wildcard, struct ifnet *ifp, const struct mbuf *m)
1398 struct inpcbhead *head;
1399 struct inpcb *inp, *jinp=NULL;
1400 u_short fport = fport_arg, lport = lport_arg;
1403 * First look for an exact match.
1405 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1406 laddr.s_addr, lport, pcbinfo->hashmask)];
1407 LIST_FOREACH(inp, head, inp_hash) {
1409 if (!(inp->inp_vflag & INP_IPV4))
1412 if (in_hosteq(inp->inp_faddr, faddr) &&
1413 in_hosteq(inp->inp_laddr, laddr) &&
1414 inp->inp_fport == fport && inp->inp_lport == lport) {
1416 if (inp->inp_socket == NULL ||
1417 inp->inp_socket->so_cred->cr_prison == NULL) {
1428 struct inpcb *local_wild = NULL;
1429 struct inpcb *jinp_wild = NULL;
1431 struct inpcb *local_wild_mapped = NULL;
1433 struct inpcontainer *ic;
1434 struct inpcontainerhead *chead;
1435 struct sockaddr_in jsin;
1439 * Check local group first
1441 if (pcbinfo->localgrphashbase != NULL &&
1442 m != NULL && (m->m_flags & M_HASH) &&
1443 !(ifp && ifp->if_type == IFT_FAITH)) {
1444 inp = inp_localgroup_lookup(pcbinfo,
1445 laddr, lport, m->m_pkthdr.hash);
1451 * Order of socket selection:
1452 * 1. non-jailed, non-wild.
1453 * 2. non-jailed, wild.
1454 * 3. jailed, non-wild.
1457 jsin.sin_family = AF_INET;
1458 chead = &pcbinfo->wildcardhashbase[
1459 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1460 LIST_FOREACH(ic, chead, ic_list) {
1462 jsin.sin_addr.s_addr = laddr.s_addr;
1464 if (!(inp->inp_vflag & INP_IPV4))
1467 if (inp->inp_socket != NULL)
1468 cred = inp->inp_socket->so_cred;
1471 if (cred != NULL && jailed(cred)) {
1475 if (!jailed_ip(cred->cr_prison,
1476 (struct sockaddr *)&jsin))
1479 if (inp->inp_lport == lport) {
1480 if (ifp && ifp->if_type == IFT_FAITH &&
1481 !(inp->inp_flags & INP_FAITH))
1483 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1484 if (cred != NULL && jailed(cred))
1489 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1491 if (INP_CHECK_SOCKAF(inp->inp_socket,
1493 local_wild_mapped = inp;
1504 if (local_wild != NULL)
1505 return (local_wild);
1507 if (local_wild_mapped != NULL)
1508 return (local_wild_mapped);
1522 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1523 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1524 boolean_t wildcard, struct ifnet *ifp)
1526 return in_pcblookup_pkthash(pcbinfo, faddr, fport_arg,
1527 laddr, lport_arg, wildcard, ifp, NULL);
1531 * Insert PCB into connection hash table.
1534 in_pcbinsconnhash(struct inpcb *inp)
1536 struct inpcbinfo *pcbinfo = inp->inp_cpcbinfo;
1537 struct inpcbhead *bucket;
1538 u_int32_t hashkey_faddr, hashkey_laddr;
1541 if (inp->inp_vflag & INP_IPV6) {
1542 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1543 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1546 hashkey_faddr = inp->inp_faddr.s_addr;
1547 hashkey_laddr = inp->inp_laddr.s_addr;
1552 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1553 ("already on wildcardhash"));
1554 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1555 ("already on connhash"));
1556 inp->inp_flags |= INP_CONNECTED;
1559 * Insert into the connection hash table.
1561 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1562 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1563 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1567 * Remove PCB from connection hash table.
1570 in_pcbremconnhash(struct inpcb *inp)
1572 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1573 LIST_REMOVE(inp, inp_hash);
1574 inp->inp_flags &= ~INP_CONNECTED;
1578 * Insert PCB into port hash table.
1581 in_pcbinsporthash(struct inpcbportinfo *portinfo, struct inpcb *inp)
1583 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1584 struct inpcbporthead *pcbporthash;
1585 struct inpcbport *phd;
1588 * If the porthashbase is shared across several cpus, it must
1591 if (portinfo->porttoken)
1592 ASSERT_LWKT_TOKEN_HELD(portinfo->porttoken);
1595 * Insert into the port hash table.
1597 pcbporthash = &portinfo->porthashbase[
1598 INP_PCBPORTHASH(inp->inp_lport, portinfo->porthashmask)];
1600 /* Go through port list and look for a head for this lport. */
1601 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1602 if (phd->phd_port == inp->inp_lport)
1606 /* If none exists, malloc one and tack it on. */
1608 KKASSERT(pcbinfo->portsave != NULL);
1609 phd = pcbinfo->portsave;
1610 pcbinfo->portsave = NULL;
1611 phd->phd_port = inp->inp_lport;
1612 LIST_INIT(&phd->phd_pcblist);
1613 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1616 inp->inp_portinfo = portinfo;
1618 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1620 if (pcbinfo->portsave == NULL) {
1621 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1622 M_PCB, M_INTWAIT | M_ZERO);
1627 in_pcbinsporthash_lport(struct inpcb *inp)
1629 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1630 struct inpcbportinfo *portinfo;
1633 /* Locate the proper portinfo based on lport */
1634 lport_ho = ntohs(inp->inp_lport);
1635 portinfo = &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
1636 KKASSERT((lport_ho & pcbinfo->portinfo_mask) == portinfo->offset);
1638 if (portinfo->porttoken)
1639 lwkt_gettoken(portinfo->porttoken);
1641 in_pcbinsporthash(portinfo, inp);
1643 if (portinfo->porttoken)
1644 lwkt_reltoken(portinfo->porttoken);
1647 static struct inp_localgroup *
1648 inp_localgroup_alloc(struct inp_localgrphead *hdr, u_char vflag,
1649 uint16_t port, const union in_dependaddr *addr, int size)
1651 struct inp_localgroup *grp;
1653 grp = kmalloc(__offsetof(struct inp_localgroup, il_inp[size]),
1654 M_TEMP, M_INTWAIT | M_ZERO);
1655 grp->il_vflag = vflag;
1656 grp->il_lport = port;
1657 grp->il_dependladdr = *addr;
1658 grp->il_inpsiz = size;
1660 LIST_INSERT_HEAD(hdr, grp, il_list);
1666 inp_localgroup_free(struct inp_localgroup *grp)
1668 LIST_REMOVE(grp, il_list);
1672 static struct inp_localgroup *
1673 inp_localgroup_resize(struct inp_localgrphead *hdr,
1674 struct inp_localgroup *old_grp, int size)
1676 struct inp_localgroup *grp;
1679 grp = inp_localgroup_alloc(hdr, old_grp->il_vflag,
1680 old_grp->il_lport, &old_grp->il_dependladdr, size);
1682 KASSERT(old_grp->il_inpcnt < grp->il_inpsiz,
1683 ("invalid new local group size %d and old local group count %d",
1684 grp->il_inpsiz, old_grp->il_inpcnt));
1685 for (i = 0; i < old_grp->il_inpcnt; ++i)
1686 grp->il_inp[i] = old_grp->il_inp[i];
1687 grp->il_inpcnt = old_grp->il_inpcnt;
1688 grp->il_factor = old_grp->il_factor;
1690 inp_localgroup_free(old_grp);
1696 inp_localgroup_factor(struct inp_localgroup *grp)
1699 ((uint32_t)(0xffff >> ncpus2_shift) / grp->il_inpcnt) + 1;
1700 KASSERT(grp->il_factor != 0, ("invalid local group factor, "
1701 "ncpus2_shift %d, inpcnt %d", ncpus2_shift, grp->il_inpcnt));
1705 in_pcbinslocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1707 struct inp_localgrphead *hdr;
1708 struct inp_localgroup *grp;
1711 if (pcbinfo->localgrphashbase == NULL)
1715 * XXX don't allow jailed socket to join local group
1717 if (inp->inp_socket != NULL)
1718 cred = inp->inp_socket->so_cred;
1721 if (cred != NULL && jailed(cred))
1726 * XXX don't allow IPv4 mapped INET6 wild socket
1728 if ((inp->inp_vflag & INP_IPV4) &&
1729 inp->inp_laddr.s_addr == INADDR_ANY &&
1730 INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6))
1734 hdr = &pcbinfo->localgrphashbase[
1735 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1737 LIST_FOREACH(grp, hdr, il_list) {
1738 if (grp->il_vflag == inp->inp_vflag &&
1739 grp->il_lport == inp->inp_lport &&
1740 memcmp(&grp->il_dependladdr,
1741 &inp->inp_inc.inc_ie.ie_dependladdr,
1742 sizeof(grp->il_dependladdr)) == 0) {
1747 /* Create new local group */
1748 grp = inp_localgroup_alloc(hdr, inp->inp_vflag,
1749 inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr,
1750 INP_LOCALGROUP_SIZMIN);
1751 } else if (grp->il_inpcnt == grp->il_inpsiz) {
1752 if (grp->il_inpsiz >= INP_LOCALGROUP_SIZMAX) {
1753 static int limit_logged = 0;
1755 if (!limit_logged) {
1757 kprintf("local group port %d, "
1758 "limit reached\n", ntohs(grp->il_lport));
1763 /* Expand this local group */
1764 grp = inp_localgroup_resize(hdr, grp, grp->il_inpsiz * 2);
1767 KASSERT(grp->il_inpcnt < grp->il_inpsiz,
1768 ("invalid local group size %d and count %d",
1769 grp->il_inpsiz, grp->il_inpcnt));
1770 grp->il_inp[grp->il_inpcnt] = inp;
1772 inp_localgroup_factor(grp);
1776 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1778 struct inpcontainer *ic;
1779 struct inpcontainerhead *bucket;
1781 in_pcbinslocalgrphash_oncpu(inp, pcbinfo);
1783 bucket = &pcbinfo->wildcardhashbase[
1784 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1786 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1788 LIST_INSERT_HEAD(bucket, ic, ic_list);
1792 * Insert PCB into wildcard hash table.
1795 in_pcbinswildcardhash(struct inpcb *inp)
1797 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1799 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1800 ("already on connhash"));
1801 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1802 ("already on wildcardhash"));
1803 inp->inp_flags |= INP_WILDCARD;
1805 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1809 in_pcbremlocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1811 struct inp_localgrphead *hdr;
1812 struct inp_localgroup *grp;
1814 if (pcbinfo->localgrphashbase == NULL)
1817 hdr = &pcbinfo->localgrphashbase[
1818 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1820 LIST_FOREACH(grp, hdr, il_list) {
1823 for (i = 0; i < grp->il_inpcnt; ++i) {
1824 if (grp->il_inp[i] != inp)
1827 if (grp->il_inpcnt == 1) {
1828 /* Free this local group */
1829 inp_localgroup_free(grp);
1831 /* Pull up inpcbs */
1832 for (; i + 1 < grp->il_inpcnt; ++i)
1833 grp->il_inp[i] = grp->il_inp[i + 1];
1835 inp_localgroup_factor(grp);
1837 if (grp->il_inpsiz > INP_LOCALGROUP_SIZMIN &&
1838 grp->il_inpcnt <= (grp->il_inpsiz / 4)) {
1839 /* Shrink this local group */
1840 grp = inp_localgroup_resize(hdr, grp,
1841 grp->il_inpsiz / 2);
1850 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1852 struct inpcontainer *ic;
1853 struct inpcontainerhead *head;
1855 in_pcbremlocalgrphash_oncpu(inp, pcbinfo);
1858 head = &pcbinfo->wildcardhashbase[
1859 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1861 LIST_FOREACH(ic, head, ic_list) {
1862 if (ic->ic_inp == inp)
1865 return; /* not found! */
1868 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
1869 kfree(ic, M_TEMP); /* deallocate container */
1873 * Remove PCB from wildcard hash table.
1876 in_pcbremwildcardhash(struct inpcb *inp)
1878 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1880 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1881 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
1882 inp->inp_flags &= ~INP_WILDCARD;
1886 * Remove PCB from various lists.
1889 in_pcbremlists(struct inpcb *inp)
1891 if (inp->inp_lport) {
1892 struct inpcbportinfo *portinfo;
1893 struct inpcbport *phd;
1897 * inp->inp_portinfo is _not_ necessary same as
1898 * inp->inp_pcbinfo->portinfo.
1900 portinfo = inp->inp_portinfo;
1901 if (portinfo->porttoken)
1902 lwkt_gettoken(portinfo->porttoken);
1905 LIST_REMOVE(inp, inp_portlist);
1906 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1907 LIST_REMOVE(phd, phd_hash);
1910 if (portinfo->porttoken)
1911 lwkt_reltoken(portinfo->porttoken);
1913 if (inp->inp_flags & INP_WILDCARD) {
1914 in_pcbremwildcardhash(inp);
1915 } else if (inp->inp_flags & INP_CONNECTED) {
1916 in_pcbremconnhash(inp);
1918 LIST_REMOVE(inp, inp_list);
1919 inp->inp_pcbinfo->ipi_count--;
1923 prison_xinpcb(struct thread *td, struct inpcb *inp)
1927 if (td->td_proc == NULL)
1929 cr = td->td_proc->p_ucred;
1930 if (cr->cr_prison == NULL)
1932 if (inp->inp_socket && inp->inp_socket->so_cred &&
1933 inp->inp_socket->so_cred->cr_prison &&
1934 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
1940 in_pcblist_global(SYSCTL_HANDLER_ARGS)
1942 struct inpcbinfo *pcbinfo = arg1;
1943 struct inpcb *inp, *marker;
1948 * The process of preparing the TCB list is too time-consuming and
1949 * resource-intensive to repeat twice on every request.
1951 if (req->oldptr == NULL) {
1952 n = pcbinfo->ipi_count;
1953 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1957 if (req->newptr != NULL)
1961 * OK, now we're committed to doing something. Re-fetch ipi_count
1962 * after obtaining the generation count.
1964 n = pcbinfo->ipi_count;
1966 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
1967 marker->inp_flags |= INP_PLACEMARKER;
1968 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1973 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
1974 LIST_REMOVE(marker, inp_list);
1975 LIST_INSERT_AFTER(inp, marker, inp_list);
1977 if (inp->inp_flags & INP_PLACEMARKER)
1979 if (prison_xinpcb(req->td, inp))
1981 bzero(&xi, sizeof xi);
1982 xi.xi_len = sizeof xi;
1983 bcopy(inp, &xi.xi_inp, sizeof *inp);
1984 if (inp->inp_socket)
1985 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1986 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
1990 LIST_REMOVE(marker, inp_list);
1991 if (error == 0 && i < n) {
1992 bzero(&xi, sizeof xi);
1993 xi.xi_len = sizeof xi;
1995 error = SYSCTL_OUT(req, &xi, sizeof xi);
1999 kfree(marker, M_TEMP);
2004 in_pcblist_global_nomarker(SYSCTL_HANDLER_ARGS, struct xinpcb **xi0, int *nxi0)
2006 struct inpcbinfo *pcbinfo = arg1;
2015 * The process of preparing the PCB list is too time-consuming and
2016 * resource-intensive to repeat twice on every request.
2018 if (req->oldptr == NULL) {
2019 int n = pcbinfo->ipi_count;
2021 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
2025 if (req->newptr != NULL)
2028 if (pcbinfo->ipi_count == 0)
2032 xi = kmalloc(pcbinfo->ipi_count * sizeof(*xi), M_TEMP,
2033 M_WAITOK | M_ZERO | M_NULLOK);
2037 LIST_FOREACH(inp, &pcbinfo->pcblisthead, inp_list) {
2038 struct xinpcb *xi_ptr = &xi[nxi];
2040 if (prison_xinpcb(req->td, inp))
2043 xi_ptr->xi_len = sizeof(*xi_ptr);
2044 bcopy(inp, &xi_ptr->xi_inp, sizeof(*inp));
2045 if (inp->inp_socket)
2046 sotoxsocket(inp->inp_socket, &xi_ptr->xi_socket);
2062 in_savefaddr(struct socket *so, const struct sockaddr *faddr)
2064 struct sockaddr_in *sin;
2066 KASSERT(faddr->sa_family == AF_INET,
2067 ("not AF_INET faddr %d", faddr->sa_family));
2069 sin = kmalloc(sizeof(*sin), M_SONAME, M_WAITOK | M_ZERO);
2070 sin->sin_family = AF_INET;
2071 sin->sin_len = sizeof(*sin);
2072 sin->sin_port = ((const struct sockaddr_in *)faddr)->sin_port;
2073 sin->sin_addr = ((const struct sockaddr_in *)faddr)->sin_addr;
2075 so->so_faddr = (struct sockaddr *)sin;
2079 in_pcbportinfo_init(struct inpcbportinfo *portinfo, int hashsize,
2080 boolean_t shared, u_short offset)
2082 memset(portinfo, 0, sizeof(*portinfo));
2084 portinfo->offset = offset;
2085 portinfo->lastport = offset;
2086 portinfo->lastlow = offset;
2087 portinfo->lasthi = offset;
2089 portinfo->porthashbase = hashinit(hashsize, M_PCB,
2090 &portinfo->porthashmask);
2093 portinfo->porttoken = kmalloc(sizeof(struct lwkt_token),
2095 lwkt_token_init(portinfo->porttoken, "porttoken");
2100 in_pcbportrange(u_short *hi0, u_short *lo0, u_short ofs, u_short step)
2110 hi = rounddown2(hi, step);
2115 lo = roundup2(lo, step);