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 u_short first, last, lport;
304 inp->inp_flags |= INP_ANONPORT;
306 if (inp->inp_flags & INP_HIGHPORT) {
307 first = ipport_hifirstauto; /* sysctl */
308 last = ipport_hilastauto;
309 lastport = &pcbinfo->lasthi;
310 } else if (inp->inp_flags & INP_LOWPORT) {
313 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
314 inp->inp_laddr.s_addr = INADDR_ANY;
317 first = ipport_lowfirstauto; /* 1023 */
318 last = ipport_lowlastauto; /* 600 */
319 lastport = &pcbinfo->lastlow;
321 first = ipport_firstauto; /* sysctl */
322 last = ipport_lastauto;
323 lastport = &pcbinfo->lastport;
327 * This has to be atomic. If the porthash is shared across multiple
328 * protocol threads (aka tcp) then the token will be non-NULL.
330 if (pcbinfo->porttoken)
331 lwkt_gettoken(pcbinfo->porttoken);
334 * Simple check to ensure all ports are not used up causing
337 * We split the two cases (up and down) so that the direction
338 * is not being tested on each round of the loop.
344 count = first - last;
347 if (count-- < 0) { /* completely used? */
348 inp->inp_laddr.s_addr = INADDR_ANY;
349 error = EADDRNOTAVAIL;
353 if (*lastport > first || *lastport < last)
355 lport = htons(*lastport);
356 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr, lport,
362 count = last - first;
365 if (count-- < 0) { /* completely used? */
366 inp->inp_laddr.s_addr = INADDR_ANY;
367 error = EADDRNOTAVAIL;
371 if (*lastport < first || *lastport > last)
373 lport = htons(*lastport);
374 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr, lport,
377 inp->inp_lport = lport;
378 in_pcbinsporthash(inp);
381 if (pcbinfo->porttoken)
382 lwkt_reltoken(pcbinfo->porttoken);
387 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
389 struct socket *so = inp->inp_socket;
390 struct sockaddr_in jsin;
391 struct ucred *cred = NULL;
394 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
395 return (EADDRNOTAVAIL);
396 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
397 return (EINVAL); /* already bound */
399 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
400 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
402 cred = td->td_proc->p_ucred;
405 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
406 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
409 int reuseport = (so->so_options & SO_REUSEPORT);
412 if (nam->sa_len != sizeof *sin)
416 * We should check the family, but old programs
417 * incorrectly fail to initialize it.
419 if (sin->sin_family != AF_INET)
420 return (EAFNOSUPPORT);
422 if (!prison_replace_wildcards(td, nam))
425 lport = sin->sin_port;
426 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
428 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
429 * allow complete duplication of binding if
430 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
431 * and a multicast address is bound on both
432 * new and duplicated sockets.
434 if (so->so_options & SO_REUSEADDR)
435 reuseport = SO_REUSEADDR | SO_REUSEPORT;
436 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
437 sin->sin_port = 0; /* yech... */
438 bzero(&sin->sin_zero, sizeof sin->sin_zero);
439 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
440 return (EADDRNOTAVAIL);
443 inp->inp_laddr = sin->sin_addr;
445 jsin.sin_family = AF_INET;
446 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
447 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
448 inp->inp_laddr.s_addr = INADDR_ANY;
451 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
454 /* Auto-select local port */
455 return in_pcbsetlport(inp, wild, cred);
459 if (ntohs(lport) < IPPORT_RESERVED && cred &&
461 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
462 inp->inp_laddr.s_addr = INADDR_ANY;
467 * This has to be atomic. If the porthash is shared across
468 * multiple protocol threads (aka tcp) then the token will
471 if (pcbinfo->porttoken)
472 lwkt_gettoken(pcbinfo->porttoken);
474 if (so->so_cred->cr_uid != 0 &&
475 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
476 t = in_pcblookup_local(pcbinfo, sin->sin_addr, lport,
477 INPLOOKUP_WILDCARD, cred);
479 (!in_nullhost(sin->sin_addr) ||
480 !in_nullhost(t->inp_laddr) ||
481 (t->inp_socket->so_options & SO_REUSEPORT) == 0) &&
482 (so->so_cred->cr_uid !=
483 t->inp_socket->so_cred->cr_uid)) {
485 if (!in_nullhost(sin->sin_addr) ||
486 !in_nullhost(t->inp_laddr) ||
487 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
490 inp->inp_laddr.s_addr = INADDR_ANY;
496 if (cred && !prison_replace_wildcards(td, nam)) {
497 inp->inp_laddr.s_addr = INADDR_ANY;
498 error = EADDRNOTAVAIL;
501 t = in_pcblookup_local(pcbinfo, sin->sin_addr, lport,
503 if (t && !(reuseport & t->inp_socket->so_options)) {
505 if (!in_nullhost(sin->sin_addr) ||
506 !in_nullhost(t->inp_laddr) ||
507 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
510 inp->inp_laddr.s_addr = INADDR_ANY;
515 inp->inp_lport = lport;
516 in_pcbinsporthash(inp);
519 if (pcbinfo->porttoken)
520 lwkt_reltoken(pcbinfo->porttoken);
523 jsin.sin_family = AF_INET;
524 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
525 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
526 inp->inp_laddr.s_addr = INADDR_ANY;
529 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
531 return in_pcbsetlport(inp, wild, cred);
535 static struct inpcb *
536 in_pcblookup_localremote(struct inpcbinfo *pcbinfo, struct in_addr laddr,
537 u_short lport, struct in_addr faddr, u_short fport, struct ucred *cred)
540 struct inpcbporthead *porthash;
541 struct inpcbport *phd;
542 struct inpcb *match = NULL;
545 * If the porthashbase is shared across several cpus, it must
548 if (pcbinfo->porttoken)
549 ASSERT_LWKT_TOKEN_HELD(pcbinfo->porttoken);
552 * Best fit PCB lookup.
554 * First see if this local port is in use by looking on the
557 porthash = &pcbinfo->porthashbase[
558 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
559 LIST_FOREACH(phd, porthash, phd_hash) {
560 if (phd->phd_port == lport)
564 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
566 if ((inp->inp_vflag & INP_IPV4) == 0)
569 if (inp->inp_laddr.s_addr != INADDR_ANY &&
570 inp->inp_laddr.s_addr != laddr.s_addr)
573 if (inp->inp_faddr.s_addr != INADDR_ANY &&
574 inp->inp_faddr.s_addr != faddr.s_addr)
577 if (inp->inp_fport != 0 && inp->inp_fport != fport)
582 inp->inp_socket->so_cred->cr_prison) {
592 in_pcbsetlport_remote(struct inpcb *inp, const struct sockaddr *remote,
595 struct proc *p = td->td_proc;
596 unsigned short *lastport;
597 const struct sockaddr_in *sin = (const struct sockaddr_in *)remote;
598 struct sockaddr_in jsin;
599 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
600 struct ucred *cred = NULL;
603 int count, error, dup = 0;
605 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
606 return (EADDRNOTAVAIL);
608 KKASSERT(inp->inp_laddr.s_addr != INADDR_ANY);
609 if (inp->inp_lport != 0)
610 return (EINVAL); /* already bound */
615 jsin.sin_family = AF_INET;
616 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
617 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
618 inp->inp_laddr.s_addr = INADDR_ANY;
621 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
623 inp->inp_flags |= INP_ANONPORT;
625 if (inp->inp_flags & INP_HIGHPORT) {
626 first = ipport_hifirstauto; /* sysctl */
627 last = ipport_hilastauto;
628 lastport = &pcbinfo->lasthi;
629 } else if (inp->inp_flags & INP_LOWPORT) {
632 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
633 inp->inp_laddr.s_addr = INADDR_ANY;
636 first = ipport_lowfirstauto; /* 1023 */
637 last = ipport_lowlastauto; /* 600 */
638 lastport = &pcbinfo->lastlow;
640 first = ipport_firstauto; /* sysctl */
641 last = ipport_lastauto;
642 lastport = &pcbinfo->lastport;
646 * This has to be atomic. If the porthash is shared across multiple
647 * protocol threads (aka tcp) then the token will be non-NULL.
649 if (pcbinfo->porttoken)
650 lwkt_gettoken(pcbinfo->porttoken);
654 * Simple check to ensure all ports are not used up causing
657 * We split the two cases (up and down) so that the direction
658 * is not being tested on each round of the loop.
664 count = first - last;
667 if (count-- < 0) { /* completely used? */
668 inp->inp_laddr.s_addr = INADDR_ANY;
669 error = EADDRNOTAVAIL;
673 if (*lastport > first || *lastport < last)
675 lport = htons(*lastport);
676 } while (in_pcblookup_localremote(pcbinfo, inp->inp_laddr,
677 lport, sin->sin_addr, sin->sin_port, cred));
682 count = last - first;
685 if (count-- < 0) { /* completely used? */
686 inp->inp_laddr.s_addr = INADDR_ANY;
687 error = EADDRNOTAVAIL;
691 if (*lastport < first || *lastport > last)
693 lport = htons(*lastport);
694 } while (in_pcblookup_localremote(pcbinfo, inp->inp_laddr,
695 lport, sin->sin_addr, sin->sin_port, cred));
698 /* This could happen on loopback interface */
699 if (sin->sin_port == lport &&
700 sin->sin_addr.s_addr == inp->inp_laddr.s_addr) {
703 * Duplicate again; give up
705 inp->inp_laddr.s_addr = INADDR_ANY;
706 error = EADDRNOTAVAIL;
712 inp->inp_lport = lport;
713 in_pcbinsporthash(inp);
716 if (pcbinfo->porttoken)
717 lwkt_reltoken(pcbinfo->porttoken);
722 * Transform old in_pcbconnect() into an inner subroutine for new
723 * in_pcbconnect(): Do some validity-checking on the remote
724 * address (in mbuf 'nam') and then determine local host address
725 * (i.e., which interface) to use to access that remote host.
727 * This preserves definition of in_pcbconnect(), while supporting a
728 * slightly different version for T/TCP. (This is more than
729 * a bit of a kludge, but cleaning up the internal interfaces would
730 * have forced minor changes in every protocol).
733 in_pcbladdr_find(struct inpcb *inp, struct sockaddr *nam,
734 struct sockaddr_in **plocal_sin, struct thread *td, int find)
736 struct in_ifaddr *ia;
737 struct ucred *cred = NULL;
738 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
739 struct sockaddr *jsin;
740 int jailed = 0, alloc_route = 0;
742 if (nam->sa_len != sizeof *sin)
744 if (sin->sin_family != AF_INET)
745 return (EAFNOSUPPORT);
746 if (sin->sin_port == 0)
747 return (EADDRNOTAVAIL);
748 if (td && td->td_proc && td->td_proc->p_ucred)
749 cred = td->td_proc->p_ucred;
750 if (cred && cred->cr_prison)
752 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
753 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
755 * If the destination address is INADDR_ANY,
756 * use the primary local address.
757 * If the supplied address is INADDR_BROADCAST,
758 * and the primary interface supports broadcast,
759 * choose the broadcast address for that interface.
761 if (sin->sin_addr.s_addr == INADDR_ANY)
762 sin->sin_addr = IA_SIN(ia)->sin_addr;
763 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
764 (ia->ia_ifp->if_flags & IFF_BROADCAST))
765 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
772 * If route is known or can be allocated now,
773 * our src addr is taken from the i/f, else punt.
774 * Note that we should check the address family of the cached
775 * destination, in case of sharing the cache with IPv6.
777 ro = &inp->inp_route;
779 (!(ro->ro_rt->rt_flags & RTF_UP) ||
780 ro->ro_dst.sa_family != AF_INET ||
781 satosin(&ro->ro_dst)->sin_addr.s_addr !=
782 sin->sin_addr.s_addr ||
783 inp->inp_socket->so_options & SO_DONTROUTE)) {
787 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
788 (ro->ro_rt == NULL ||
789 ro->ro_rt->rt_ifp == NULL)) {
790 /* No route yet, so try to acquire one */
791 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
792 ro->ro_dst.sa_family = AF_INET;
793 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
794 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
800 * If we found a route, use the address
801 * corresponding to the outgoing interface
802 * unless it is the loopback (in case a route
803 * to our address on another net goes to loopback).
805 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
807 if (jailed_ip(cred->cr_prison,
808 ro->ro_rt->rt_ifa->ifa_addr)) {
809 ia = ifatoia(ro->ro_rt->rt_ifa);
812 ia = ifatoia(ro->ro_rt->rt_ifa);
816 u_short fport = sin->sin_port;
819 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
820 if (ia && jailed && !jailed_ip(cred->cr_prison,
821 sintosa(&ia->ia_addr)))
824 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
825 if (ia && jailed && !jailed_ip(cred->cr_prison,
826 sintosa(&ia->ia_addr)))
828 sin->sin_port = fport;
830 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
831 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
832 if (ia && jailed && !jailed_ip(cred->cr_prison,
833 sintosa(&ia->ia_addr)))
836 if (!jailed && ia == NULL)
840 * If the destination address is multicast and an outgoing
841 * interface has been set as a multicast option, use the
842 * address of that interface as our source address.
844 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
845 inp->inp_moptions != NULL) {
846 struct ip_moptions *imo;
849 imo = inp->inp_moptions;
850 if (imo->imo_multicast_ifp != NULL) {
851 struct in_ifaddr_container *iac;
853 ifp = imo->imo_multicast_ifp;
856 &in_ifaddrheads[mycpuid], ia_link) {
857 if (iac->ia->ia_ifp == ifp) {
867 * Don't do pcblookup call here; return interface in plocal_sin
868 * and exit to caller, that will do the lookup.
870 if (ia == NULL && jailed) {
871 if ((jsin = prison_get_nonlocal(cred->cr_prison, AF_INET, NULL)) != NULL ||
872 (jsin = prison_get_local(cred->cr_prison, AF_INET, NULL)) != NULL) {
873 *plocal_sin = satosin(jsin);
879 *plocal_sin = &ia->ia_addr;
885 struct route *ro = &inp->inp_route;
887 if (ro->ro_rt != NULL)
889 bzero(ro, sizeof(*ro));
891 return (EADDRNOTAVAIL);
895 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
896 struct sockaddr_in **plocal_sin, struct thread *td)
898 return in_pcbladdr_find(inp, nam, plocal_sin, td,
899 (inp->inp_laddr.s_addr == INADDR_ANY));
904 * Connect from a socket to a specified address.
905 * Both address and port must be specified in argument sin.
906 * If don't have a local address for this socket yet,
910 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
912 struct sockaddr_in *if_sin;
913 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
916 /* Call inner routine to assign local interface address. */
917 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
920 if (in_pcblookup_hash(inp->inp_cpcbinfo, sin->sin_addr, sin->sin_port,
921 inp->inp_laddr.s_addr ?
922 inp->inp_laddr : if_sin->sin_addr,
923 inp->inp_lport, FALSE, NULL) != NULL) {
926 if (inp->inp_laddr.s_addr == INADDR_ANY) {
927 if (inp->inp_lport == 0) {
928 error = in_pcbbind(inp, NULL, td);
932 inp->inp_laddr = if_sin->sin_addr;
934 inp->inp_faddr = sin->sin_addr;
935 inp->inp_fport = sin->sin_port;
936 in_pcbinsconnhash(inp);
941 in_pcbdisconnect(struct inpcb *inp)
944 inp->inp_faddr.s_addr = INADDR_ANY;
946 in_pcbremconnhash(inp);
947 if (inp->inp_socket->so_state & SS_NOFDREF)
952 in_pcbdetach(struct inpcb *inp)
954 struct socket *so = inp->inp_socket;
955 struct inpcbinfo *ipi = inp->inp_pcbinfo;
958 ipsec4_delete_pcbpolicy(inp);
960 inp->inp_gencnt = ++ipi->ipi_gencnt;
961 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
964 sofree(so); /* remove pcb ref */
965 if (inp->inp_options)
966 m_free(inp->inp_options);
967 if (inp->inp_route.ro_rt)
968 rtfree(inp->inp_route.ro_rt);
969 ip_freemoptions(inp->inp_moptions);
975 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
976 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
977 * in struct pr_usrreqs, so that protocols can just reference then directly
978 * without the need for a wrapper function. The socket must have a valid
979 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
980 * except through a kernel programming error, so it is acceptable to panic
981 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
982 * because there actually /is/ a programming error somewhere... XXX)
985 in_setsockaddr(struct socket *so, struct sockaddr **nam)
988 struct sockaddr_in *sin;
991 * Do the malloc first in case it blocks.
993 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
994 sin->sin_family = AF_INET;
995 sin->sin_len = sizeof *sin;
1001 kfree(sin, M_SONAME);
1002 return (ECONNRESET);
1004 sin->sin_port = inp->inp_lport;
1005 sin->sin_addr = inp->inp_laddr;
1008 *nam = (struct sockaddr *)sin;
1013 in_setsockaddr_dispatch(netmsg_t msg)
1017 error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1018 lwkt_replymsg(&msg->lmsg, error);
1022 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
1025 struct sockaddr_in *sin;
1028 * Do the malloc first in case it blocks.
1030 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1031 sin->sin_family = AF_INET;
1032 sin->sin_len = sizeof *sin;
1038 kfree(sin, M_SONAME);
1039 return (ECONNRESET);
1041 sin->sin_port = inp->inp_fport;
1042 sin->sin_addr = inp->inp_faddr;
1045 *nam = (struct sockaddr *)sin;
1050 in_setpeeraddr_dispatch(netmsg_t msg)
1054 error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1055 lwkt_replymsg(&msg->lmsg, error);
1059 in_pcbnotifyall(struct inpcbhead *head, struct in_addr faddr, int err,
1060 void (*notify)(struct inpcb *, int))
1062 struct inpcb *inp, *ninp;
1065 * note: if INP_PLACEMARKER is set we must ignore the rest of
1066 * the structure and skip it.
1069 LIST_FOREACH_MUTABLE(inp, head, inp_list, ninp) {
1070 if (inp->inp_flags & INP_PLACEMARKER)
1073 if (!(inp->inp_vflag & INP_IPV4))
1076 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1077 inp->inp_socket == NULL)
1079 (*notify)(inp, err); /* can remove inp from list! */
1085 in_pcbpurgeif0(struct inpcb *head, struct ifnet *ifp)
1088 struct ip_moptions *imo;
1091 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
1092 if (inp->inp_flags & INP_PLACEMARKER)
1094 imo = inp->inp_moptions;
1095 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
1097 * Unselect the outgoing interface if it is being
1100 if (imo->imo_multicast_ifp == ifp)
1101 imo->imo_multicast_ifp = NULL;
1104 * Drop multicast group membership if we joined
1105 * through the interface being detached.
1107 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1109 if (imo->imo_membership[i]->inm_ifp == ifp) {
1110 in_delmulti(imo->imo_membership[i]);
1112 } else if (gap != 0)
1113 imo->imo_membership[i - gap] =
1114 imo->imo_membership[i];
1116 imo->imo_num_memberships -= gap;
1122 * Check for alternatives when higher level complains
1123 * about service problems. For now, invalidate cached
1124 * routing information. If the route was created dynamically
1125 * (by a redirect), time to try a default gateway again.
1128 in_losing(struct inpcb *inp)
1131 struct rt_addrinfo rtinfo;
1133 if ((rt = inp->inp_route.ro_rt)) {
1134 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1135 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1136 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1137 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1138 rtinfo.rti_flags = rt->rt_flags;
1139 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1140 if (rt->rt_flags & RTF_DYNAMIC) {
1141 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1142 rt_mask(rt), rt->rt_flags, NULL);
1144 inp->inp_route.ro_rt = NULL;
1147 * A new route can be allocated
1148 * the next time output is attempted.
1154 * After a routing change, flush old routing
1155 * and allocate a (hopefully) better one.
1158 in_rtchange(struct inpcb *inp, int err)
1160 if (inp->inp_route.ro_rt) {
1161 rtfree(inp->inp_route.ro_rt);
1162 inp->inp_route.ro_rt = NULL;
1164 * A new route can be allocated the next time
1165 * output is attempted.
1171 * Lookup a PCB based on the local address and port.
1174 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1175 u_int lport_arg, int wild_okay, struct ucred *cred)
1178 int matchwild = 3, wildcard;
1179 u_short lport = lport_arg;
1180 struct inpcbporthead *porthash;
1181 struct inpcbport *phd;
1182 struct inpcb *match = NULL;
1185 * If the porthashbase is shared across several cpus, it must
1188 if (pcbinfo->porttoken)
1189 ASSERT_LWKT_TOKEN_HELD(pcbinfo->porttoken);
1192 * Best fit PCB lookup.
1194 * First see if this local port is in use by looking on the
1197 porthash = &pcbinfo->porthashbase[
1198 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
1199 LIST_FOREACH(phd, porthash, phd_hash) {
1200 if (phd->phd_port == lport)
1205 * Port is in use by one or more PCBs. Look for best
1208 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1211 if ((inp->inp_vflag & INP_IPV4) == 0)
1214 if (inp->inp_faddr.s_addr != INADDR_ANY)
1216 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1217 if (laddr.s_addr == INADDR_ANY)
1219 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1222 if (laddr.s_addr != INADDR_ANY)
1225 if (wildcard && !wild_okay)
1227 if (wildcard < matchwild &&
1230 inp->inp_socket->so_cred->cr_prison)) {
1232 matchwild = wildcard;
1233 if (matchwild == 0) {
1243 in_pcblocalgroup_last(const struct inpcbinfo *pcbinfo,
1244 const struct inpcb *inp)
1246 const struct inp_localgrphead *hdr;
1247 const struct inp_localgroup *grp;
1250 if (pcbinfo->localgrphashbase == NULL)
1253 hdr = &pcbinfo->localgrphashbase[
1254 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1256 LIST_FOREACH(grp, hdr, il_list) {
1257 if (grp->il_vflag == inp->inp_vflag &&
1258 grp->il_lport == inp->inp_lport &&
1259 memcmp(&grp->il_dependladdr,
1260 &inp->inp_inc.inc_ie.ie_dependladdr,
1261 sizeof(grp->il_dependladdr)) == 0) {
1265 if (grp == NULL || grp->il_inpcnt == 1)
1268 KASSERT(grp->il_inpcnt >= 2,
1269 ("invalid localgroup inp count %d", grp->il_inpcnt));
1270 for (i = 0; i < grp->il_inpcnt; ++i) {
1271 if (grp->il_inp[i] == inp) {
1272 int last = grp->il_inpcnt - 1;
1275 last = grp->il_inpcnt - 2;
1276 return grp->il_inp[last];
1282 static struct inpcb *
1283 inp_localgroup_lookup(const struct inpcbinfo *pcbinfo,
1284 struct in_addr laddr, uint16_t lport, uint32_t pkt_hash)
1286 struct inpcb *local_wild = NULL;
1287 const struct inp_localgrphead *hdr;
1288 const struct inp_localgroup *grp;
1290 hdr = &pcbinfo->localgrphashbase[
1291 INP_PCBLOCALGRPHASH(lport, pcbinfo->localgrphashmask)];
1292 #ifdef INP_LOCALGROUP_HASHTHR
1293 pkt_hash >>= ncpus2_shift;
1297 * Order of socket selection:
1302 * - Local group does not contain jailed sockets
1303 * - Local group does not contain IPv4 mapped INET6 wild sockets
1305 LIST_FOREACH(grp, hdr, il_list) {
1307 if (!(grp->il_vflag & INP_IPV4))
1310 if (grp->il_lport == lport) {
1313 #ifdef INP_LOCALGROUP_HASHTHR
1314 idx = pkt_hash / grp->il_factor;
1315 KASSERT(idx < grp->il_inpcnt && idx >= 0,
1316 ("invalid hash %04x, cnt %d or fact %d",
1317 pkt_hash, grp->il_inpcnt, grp->il_factor));
1320 * Modulo-N is used here, which greatly reduces
1321 * completion queue token contention, thus more
1322 * cpu time is saved.
1324 idx = pkt_hash % grp->il_inpcnt;
1327 if (grp->il_laddr.s_addr == laddr.s_addr)
1328 return grp->il_inp[idx];
1329 else if (grp->il_laddr.s_addr == INADDR_ANY)
1330 local_wild = grp->il_inp[idx];
1333 if (local_wild != NULL)
1339 * Lookup PCB in hash list.
1342 in_pcblookup_pkthash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1343 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1344 boolean_t wildcard, struct ifnet *ifp, const struct mbuf *m)
1346 struct inpcbhead *head;
1347 struct inpcb *inp, *jinp=NULL;
1348 u_short fport = fport_arg, lport = lport_arg;
1351 * First look for an exact match.
1353 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1354 laddr.s_addr, lport, pcbinfo->hashmask)];
1355 LIST_FOREACH(inp, head, inp_hash) {
1357 if (!(inp->inp_vflag & INP_IPV4))
1360 if (in_hosteq(inp->inp_faddr, faddr) &&
1361 in_hosteq(inp->inp_laddr, laddr) &&
1362 inp->inp_fport == fport && inp->inp_lport == lport) {
1364 if (inp->inp_socket == NULL ||
1365 inp->inp_socket->so_cred->cr_prison == NULL) {
1376 struct inpcb *local_wild = NULL;
1377 struct inpcb *jinp_wild = NULL;
1379 struct inpcb *local_wild_mapped = NULL;
1381 struct inpcontainer *ic;
1382 struct inpcontainerhead *chead;
1383 struct sockaddr_in jsin;
1387 * Check local group first
1389 if (pcbinfo->localgrphashbase != NULL &&
1390 m != NULL && (m->m_flags & M_HASH) &&
1391 !(ifp && ifp->if_type == IFT_FAITH)) {
1392 inp = inp_localgroup_lookup(pcbinfo,
1393 laddr, lport, m->m_pkthdr.hash);
1399 * Order of socket selection:
1400 * 1. non-jailed, non-wild.
1401 * 2. non-jailed, wild.
1402 * 3. jailed, non-wild.
1405 jsin.sin_family = AF_INET;
1406 chead = &pcbinfo->wildcardhashbase[
1407 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1408 LIST_FOREACH(ic, chead, ic_list) {
1410 jsin.sin_addr.s_addr = laddr.s_addr;
1412 if (!(inp->inp_vflag & INP_IPV4))
1415 if (inp->inp_socket != NULL)
1416 cred = inp->inp_socket->so_cred;
1419 if (cred != NULL && jailed(cred)) {
1423 if (!jailed_ip(cred->cr_prison,
1424 (struct sockaddr *)&jsin))
1427 if (inp->inp_lport == lport) {
1428 if (ifp && ifp->if_type == IFT_FAITH &&
1429 !(inp->inp_flags & INP_FAITH))
1431 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1432 if (cred != NULL && jailed(cred))
1437 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1439 if (INP_CHECK_SOCKAF(inp->inp_socket,
1441 local_wild_mapped = inp;
1452 if (local_wild != NULL)
1453 return (local_wild);
1455 if (local_wild_mapped != NULL)
1456 return (local_wild_mapped);
1470 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1471 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1472 boolean_t wildcard, struct ifnet *ifp)
1474 return in_pcblookup_pkthash(pcbinfo, faddr, fport_arg,
1475 laddr, lport_arg, wildcard, ifp, NULL);
1479 * Insert PCB into connection hash table.
1482 in_pcbinsconnhash(struct inpcb *inp)
1484 struct inpcbinfo *pcbinfo = inp->inp_cpcbinfo;
1485 struct inpcbhead *bucket;
1486 u_int32_t hashkey_faddr, hashkey_laddr;
1489 if (inp->inp_vflag & INP_IPV6) {
1490 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1491 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1494 hashkey_faddr = inp->inp_faddr.s_addr;
1495 hashkey_laddr = inp->inp_laddr.s_addr;
1500 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1501 ("already on wildcardhash"));
1502 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1503 ("already on connhash"));
1504 inp->inp_flags |= INP_CONNECTED;
1507 * Insert into the connection hash table.
1509 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1510 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1511 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1515 * Remove PCB from connection hash table.
1518 in_pcbremconnhash(struct inpcb *inp)
1520 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1521 LIST_REMOVE(inp, inp_hash);
1522 inp->inp_flags &= ~INP_CONNECTED;
1526 * Insert PCB into port hash table.
1529 in_pcbinsporthash(struct inpcb *inp)
1531 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1532 struct inpcbporthead *pcbporthash;
1533 struct inpcbport *phd;
1536 * If the porthashbase is shared across several cpus we need
1539 if (pcbinfo->porttoken)
1540 lwkt_gettoken(pcbinfo->porttoken);
1543 * Insert into the port hash table.
1545 pcbporthash = &pcbinfo->porthashbase[
1546 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->porthashmask)];
1548 /* Go through port list and look for a head for this lport. */
1549 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1550 if (phd->phd_port == inp->inp_lport)
1554 /* If none exists, malloc one and tack it on. */
1556 KKASSERT(pcbinfo->portsave != NULL);
1557 phd = pcbinfo->portsave;
1558 pcbinfo->portsave = NULL;
1559 phd->phd_port = inp->inp_lport;
1560 LIST_INIT(&phd->phd_pcblist);
1561 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1565 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1567 if (pcbinfo->porttoken)
1568 lwkt_reltoken(pcbinfo->porttoken);
1569 if (pcbinfo->portsave == NULL) {
1570 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1571 M_PCB, M_INTWAIT | M_ZERO);
1575 static struct inp_localgroup *
1576 inp_localgroup_alloc(struct inp_localgrphead *hdr, u_char vflag,
1577 uint16_t port, const union in_dependaddr *addr, int size)
1579 struct inp_localgroup *grp;
1581 grp = kmalloc(__offsetof(struct inp_localgroup, il_inp[size]),
1582 M_TEMP, M_INTWAIT | M_ZERO);
1583 grp->il_vflag = vflag;
1584 grp->il_lport = port;
1585 grp->il_dependladdr = *addr;
1586 grp->il_inpsiz = size;
1588 LIST_INSERT_HEAD(hdr, grp, il_list);
1594 inp_localgroup_free(struct inp_localgroup *grp)
1596 LIST_REMOVE(grp, il_list);
1600 static struct inp_localgroup *
1601 inp_localgroup_resize(struct inp_localgrphead *hdr,
1602 struct inp_localgroup *old_grp, int size)
1604 struct inp_localgroup *grp;
1607 grp = inp_localgroup_alloc(hdr, old_grp->il_vflag,
1608 old_grp->il_lport, &old_grp->il_dependladdr, size);
1610 KASSERT(old_grp->il_inpcnt < grp->il_inpsiz,
1611 ("invalid new local group size %d and old local group count %d",
1612 grp->il_inpsiz, old_grp->il_inpcnt));
1613 for (i = 0; i < old_grp->il_inpcnt; ++i)
1614 grp->il_inp[i] = old_grp->il_inp[i];
1615 grp->il_inpcnt = old_grp->il_inpcnt;
1616 grp->il_factor = old_grp->il_factor;
1618 inp_localgroup_free(old_grp);
1624 inp_localgroup_factor(struct inp_localgroup *grp)
1627 ((uint32_t)(0xffff >> ncpus2_shift) / grp->il_inpcnt) + 1;
1628 KASSERT(grp->il_factor != 0, ("invalid local group factor, "
1629 "ncpus2_shift %d, inpcnt %d", ncpus2_shift, grp->il_inpcnt));
1633 in_pcbinslocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1635 struct inp_localgrphead *hdr;
1636 struct inp_localgroup *grp;
1639 if (pcbinfo->localgrphashbase == NULL)
1643 * XXX don't allow jailed socket to join local group
1645 if (inp->inp_socket != NULL)
1646 cred = inp->inp_socket->so_cred;
1649 if (cred != NULL && jailed(cred))
1654 * XXX don't allow IPv4 mapped INET6 wild socket
1656 if ((inp->inp_vflag & INP_IPV4) &&
1657 inp->inp_laddr.s_addr == INADDR_ANY &&
1658 INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6))
1662 hdr = &pcbinfo->localgrphashbase[
1663 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1665 LIST_FOREACH(grp, hdr, il_list) {
1666 if (grp->il_vflag == inp->inp_vflag &&
1667 grp->il_lport == inp->inp_lport &&
1668 memcmp(&grp->il_dependladdr,
1669 &inp->inp_inc.inc_ie.ie_dependladdr,
1670 sizeof(grp->il_dependladdr)) == 0) {
1675 /* Create new local group */
1676 grp = inp_localgroup_alloc(hdr, inp->inp_vflag,
1677 inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr,
1678 INP_LOCALGROUP_SIZMIN);
1679 } else if (grp->il_inpcnt == grp->il_inpsiz) {
1680 if (grp->il_inpsiz >= INP_LOCALGROUP_SIZMAX) {
1681 static int limit_logged = 0;
1683 if (!limit_logged) {
1685 kprintf("local group port %d, "
1686 "limit reached\n", ntohs(grp->il_lport));
1691 /* Expand this local group */
1692 grp = inp_localgroup_resize(hdr, grp, grp->il_inpsiz * 2);
1695 KASSERT(grp->il_inpcnt < grp->il_inpsiz,
1696 ("invalid local group size %d and count %d",
1697 grp->il_inpsiz, grp->il_inpcnt));
1698 grp->il_inp[grp->il_inpcnt] = inp;
1700 inp_localgroup_factor(grp);
1704 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1706 struct inpcontainer *ic;
1707 struct inpcontainerhead *bucket;
1709 in_pcbinslocalgrphash_oncpu(inp, pcbinfo);
1711 bucket = &pcbinfo->wildcardhashbase[
1712 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1714 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1716 LIST_INSERT_HEAD(bucket, ic, ic_list);
1720 * Insert PCB into wildcard hash table.
1723 in_pcbinswildcardhash(struct inpcb *inp)
1725 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1727 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1728 ("already on connhash"));
1729 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1730 ("already on wildcardhash"));
1731 inp->inp_flags |= INP_WILDCARD;
1733 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1737 in_pcbremlocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1739 struct inp_localgrphead *hdr;
1740 struct inp_localgroup *grp;
1742 if (pcbinfo->localgrphashbase == NULL)
1745 hdr = &pcbinfo->localgrphashbase[
1746 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1748 LIST_FOREACH(grp, hdr, il_list) {
1751 for (i = 0; i < grp->il_inpcnt; ++i) {
1752 if (grp->il_inp[i] != inp)
1755 if (grp->il_inpcnt == 1) {
1756 /* Free this local group */
1757 inp_localgroup_free(grp);
1759 /* Pull up inpcbs */
1760 for (; i + 1 < grp->il_inpcnt; ++i)
1761 grp->il_inp[i] = grp->il_inp[i + 1];
1763 inp_localgroup_factor(grp);
1765 if (grp->il_inpsiz > INP_LOCALGROUP_SIZMIN &&
1766 grp->il_inpcnt <= (grp->il_inpsiz / 4)) {
1767 /* Shrink this local group */
1768 grp = inp_localgroup_resize(hdr, grp,
1769 grp->il_inpsiz / 2);
1778 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1780 struct inpcontainer *ic;
1781 struct inpcontainerhead *head;
1783 in_pcbremlocalgrphash_oncpu(inp, pcbinfo);
1786 head = &pcbinfo->wildcardhashbase[
1787 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1789 LIST_FOREACH(ic, head, ic_list) {
1790 if (ic->ic_inp == inp)
1793 return; /* not found! */
1796 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
1797 kfree(ic, M_TEMP); /* deallocate container */
1801 * Remove PCB from wildcard hash table.
1804 in_pcbremwildcardhash(struct inpcb *inp)
1806 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1808 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1809 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
1810 inp->inp_flags &= ~INP_WILDCARD;
1814 * Remove PCB from various lists.
1817 in_pcbremlists(struct inpcb *inp)
1819 struct inpcbinfo *pcbinfo;
1821 if (inp->inp_lport) {
1822 struct inpcbport *phd;
1824 pcbinfo = inp->inp_pcbinfo;
1825 if (pcbinfo->porttoken)
1826 lwkt_gettoken(pcbinfo->porttoken);
1829 LIST_REMOVE(inp, inp_portlist);
1830 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1831 LIST_REMOVE(phd, phd_hash);
1834 if (pcbinfo->porttoken)
1835 lwkt_reltoken(pcbinfo->porttoken);
1837 if (inp->inp_flags & INP_WILDCARD) {
1838 in_pcbremwildcardhash(inp);
1839 } else if (inp->inp_flags & INP_CONNECTED) {
1840 in_pcbremconnhash(inp);
1842 LIST_REMOVE(inp, inp_list);
1843 inp->inp_pcbinfo->ipi_count--;
1847 prison_xinpcb(struct thread *td, struct inpcb *inp)
1851 if (td->td_proc == NULL)
1853 cr = td->td_proc->p_ucred;
1854 if (cr->cr_prison == NULL)
1856 if (inp->inp_socket && inp->inp_socket->so_cred &&
1857 inp->inp_socket->so_cred->cr_prison &&
1858 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
1864 in_pcblist_global(SYSCTL_HANDLER_ARGS)
1866 struct inpcbinfo *pcbinfo = arg1;
1867 struct inpcb *inp, *marker;
1872 * The process of preparing the TCB list is too time-consuming and
1873 * resource-intensive to repeat twice on every request.
1875 if (req->oldptr == NULL) {
1876 n = pcbinfo->ipi_count;
1877 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1881 if (req->newptr != NULL)
1885 * OK, now we're committed to doing something. Re-fetch ipi_count
1886 * after obtaining the generation count.
1888 n = pcbinfo->ipi_count;
1890 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
1891 marker->inp_flags |= INP_PLACEMARKER;
1892 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1897 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
1898 LIST_REMOVE(marker, inp_list);
1899 LIST_INSERT_AFTER(inp, marker, inp_list);
1901 if (inp->inp_flags & INP_PLACEMARKER)
1903 if (prison_xinpcb(req->td, inp))
1905 bzero(&xi, sizeof xi);
1906 xi.xi_len = sizeof xi;
1907 bcopy(inp, &xi.xi_inp, sizeof *inp);
1908 if (inp->inp_socket)
1909 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1910 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
1914 LIST_REMOVE(marker, inp_list);
1915 if (error == 0 && i < n) {
1916 bzero(&xi, sizeof xi);
1917 xi.xi_len = sizeof xi;
1919 error = SYSCTL_OUT(req, &xi, sizeof xi);
1923 kfree(marker, M_TEMP);
1928 in_pcblist_global_nomarker(SYSCTL_HANDLER_ARGS, struct xinpcb **xi0, int *nxi0)
1930 struct inpcbinfo *pcbinfo = arg1;
1939 * The process of preparing the PCB list is too time-consuming and
1940 * resource-intensive to repeat twice on every request.
1942 if (req->oldptr == NULL) {
1943 int n = pcbinfo->ipi_count;
1945 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1949 if (req->newptr != NULL)
1952 if (pcbinfo->ipi_count == 0)
1956 xi = kmalloc(pcbinfo->ipi_count * sizeof(*xi), M_TEMP,
1957 M_WAITOK | M_ZERO | M_NULLOK);
1961 LIST_FOREACH(inp, &pcbinfo->pcblisthead, inp_list) {
1962 struct xinpcb *xi_ptr = &xi[nxi];
1964 if (prison_xinpcb(req->td, inp))
1967 xi_ptr->xi_len = sizeof(*xi_ptr);
1968 bcopy(inp, &xi_ptr->xi_inp, sizeof(*inp));
1969 if (inp->inp_socket)
1970 sotoxsocket(inp->inp_socket, &xi_ptr->xi_socket);
1986 in_savefaddr(struct socket *so, const struct sockaddr *faddr)
1988 struct sockaddr_in *sin;
1990 KASSERT(faddr->sa_family == AF_INET,
1991 ("not AF_INET faddr %d", faddr->sa_family));
1993 sin = kmalloc(sizeof(*sin), M_SONAME, M_WAITOK | M_ZERO);
1994 sin->sin_family = AF_INET;
1995 sin->sin_len = sizeof(*sin);
1996 sin->sin_port = ((const struct sockaddr_in *)faddr)->sin_port;
1997 sin->sin_addr = ((const struct sockaddr_in *)faddr)->sin_addr;
1999 so->so_faddr = (struct sockaddr *)sin;