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_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
299 struct socket *so = inp->inp_socket;
300 unsigned short *lastport;
301 struct sockaddr_in *sin;
302 struct sockaddr_in jsin;
303 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
304 struct ucred *cred = NULL;
306 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
309 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
310 return (EADDRNOTAVAIL);
311 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
312 return (EINVAL); /* already bound */
314 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
315 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
317 cred = td->td_proc->p_ucred;
320 * This has to be atomic. If the porthash is shared across multiple
321 * protocol threads (aka tcp) then the token will be non-NULL.
323 if (pcbinfo->porttoken)
324 lwkt_gettoken(pcbinfo->porttoken);
327 sin = (struct sockaddr_in *)nam;
328 if (nam->sa_len != sizeof *sin) {
334 * We should check the family, but old programs
335 * incorrectly fail to initialize it.
337 if (sin->sin_family != AF_INET) {
338 error = EAFNOSUPPORT;
342 if (!prison_replace_wildcards(td, nam)) {
346 lport = sin->sin_port;
347 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
349 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
350 * allow complete duplication of binding if
351 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
352 * and a multicast address is bound on both
353 * new and duplicated sockets.
355 if (so->so_options & SO_REUSEADDR)
356 reuseport = SO_REUSEADDR | SO_REUSEPORT;
357 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
358 sin->sin_port = 0; /* yech... */
359 bzero(&sin->sin_zero, sizeof sin->sin_zero);
360 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL) {
361 error = EADDRNOTAVAIL;
369 if (ntohs(lport) < IPPORT_RESERVED &&
371 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0)) {
375 if (so->so_cred->cr_uid != 0 &&
376 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
377 t = in_pcblookup_local(pcbinfo,
383 (!in_nullhost(sin->sin_addr) ||
384 !in_nullhost(t->inp_laddr) ||
385 (t->inp_socket->so_options &
386 SO_REUSEPORT) == 0) &&
387 (so->so_cred->cr_uid !=
388 t->inp_socket->so_cred->cr_uid)) {
390 if (!in_nullhost(sin->sin_addr) ||
391 !in_nullhost(t->inp_laddr) ||
393 INP_SOCKAF(t->inp_socket))
401 if (cred && !prison_replace_wildcards(td, nam)) {
402 error = EADDRNOTAVAIL;
405 t = in_pcblookup_local(pcbinfo, sin->sin_addr, lport,
407 if (t && !(reuseport & t->inp_socket->so_options)) {
409 if (!in_nullhost(sin->sin_addr) ||
410 !in_nullhost(t->inp_laddr) ||
411 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
419 inp->inp_laddr = sin->sin_addr;
422 jsin.sin_family = AF_INET;
423 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
424 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
425 inp->inp_laddr.s_addr = INADDR_ANY;
429 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
435 inp->inp_flags |= INP_ANONPORT;
437 if (inp->inp_flags & INP_HIGHPORT) {
438 first = ipport_hifirstauto; /* sysctl */
439 last = ipport_hilastauto;
440 lastport = &pcbinfo->lasthi;
441 } else if (inp->inp_flags & INP_LOWPORT) {
443 (error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
444 inp->inp_laddr.s_addr = INADDR_ANY;
447 first = ipport_lowfirstauto; /* 1023 */
448 last = ipport_lowlastauto; /* 600 */
449 lastport = &pcbinfo->lastlow;
451 first = ipport_firstauto; /* sysctl */
452 last = ipport_lastauto;
453 lastport = &pcbinfo->lastport;
456 * Simple check to ensure all ports are not used up causing
459 * We split the two cases (up and down) so that the direction
460 * is not being tested on each round of the loop.
466 count = first - last;
469 if (count-- < 0) { /* completely used? */
470 inp->inp_laddr.s_addr = INADDR_ANY;
471 error = EADDRNOTAVAIL;
475 if (*lastport > first || *lastport < last)
477 lport = htons(*lastport);
478 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
484 count = last - first;
487 if (count-- < 0) { /* completely used? */
488 inp->inp_laddr.s_addr = INADDR_ANY;
489 error = EADDRNOTAVAIL;
493 if (*lastport < first || *lastport > last)
495 lport = htons(*lastport);
496 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
500 inp->inp_lport = lport;
502 if (in_pcbinsporthash(inp) != 0) {
503 inp->inp_laddr.s_addr = INADDR_ANY;
510 if (pcbinfo->porttoken)
511 lwkt_reltoken(pcbinfo->porttoken);
515 static struct inpcb *
516 in_pcblookup_addrport(struct inpcbinfo *pcbinfo, struct in_addr laddr,
517 u_short lport, struct in_addr faddr, u_short fport, struct ucred *cred)
520 struct inpcbporthead *porthash;
521 struct inpcbport *phd;
522 struct inpcb *match = NULL;
525 * If the porthashbase is shared across several cpus we need
528 if (pcbinfo->porttoken)
529 lwkt_gettoken(pcbinfo->porttoken);
532 * Best fit PCB lookup.
534 * First see if this local port is in use by looking on the
537 porthash = &pcbinfo->porthashbase[
538 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
539 LIST_FOREACH(phd, porthash, phd_hash) {
540 if (phd->phd_port == lport)
544 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
546 if ((inp->inp_vflag & INP_IPV4) == 0)
549 if (inp->inp_laddr.s_addr != INADDR_ANY &&
550 inp->inp_laddr.s_addr != laddr.s_addr)
553 if (inp->inp_faddr.s_addr != INADDR_ANY &&
554 inp->inp_faddr.s_addr != faddr.s_addr)
557 if (inp->inp_fport != 0 && inp->inp_fport != fport)
562 inp->inp_socket->so_cred->cr_prison) {
568 if (pcbinfo->porttoken)
569 lwkt_reltoken(pcbinfo->porttoken);
574 in_pcbconn_bind(struct inpcb *inp, const struct sockaddr *nam,
577 struct proc *p = td->td_proc;
578 unsigned short *lastport;
579 const struct sockaddr_in *sin = (const struct sockaddr_in *)nam;
580 struct sockaddr_in jsin;
581 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
582 struct ucred *cred = NULL;
585 int count, error, dup = 0;
587 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
588 return (EADDRNOTAVAIL);
590 KKASSERT(inp->inp_laddr.s_addr != INADDR_ANY);
591 if (inp->inp_lport != 0)
592 return (EINVAL); /* already bound */
598 * This has to be atomic. If the porthash is shared across multiple
599 * protocol threads (aka tcp) then the token will be non-NULL.
601 if (pcbinfo->porttoken)
602 lwkt_gettoken(pcbinfo->porttoken);
604 jsin.sin_family = AF_INET;
605 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
606 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
607 inp->inp_laddr.s_addr = INADDR_ANY;
611 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
613 inp->inp_flags |= INP_ANONPORT;
615 if (inp->inp_flags & INP_HIGHPORT) {
616 first = ipport_hifirstauto; /* sysctl */
617 last = ipport_hilastauto;
618 lastport = &pcbinfo->lasthi;
619 } else if (inp->inp_flags & INP_LOWPORT) {
621 (error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
622 inp->inp_laddr.s_addr = INADDR_ANY;
625 first = ipport_lowfirstauto; /* 1023 */
626 last = ipport_lowlastauto; /* 600 */
627 lastport = &pcbinfo->lastlow;
629 first = ipport_firstauto; /* sysctl */
630 last = ipport_lastauto;
631 lastport = &pcbinfo->lastport;
636 * Simple check to ensure all ports are not used up causing
639 * We split the two cases (up and down) so that the direction
640 * is not being tested on each round of the loop.
646 count = first - last;
649 if (count-- < 0) { /* completely used? */
650 inp->inp_laddr.s_addr = INADDR_ANY;
651 error = EADDRNOTAVAIL;
655 if (*lastport > first || *lastport < last)
657 lport = htons(*lastport);
658 } while (in_pcblookup_addrport(pcbinfo, inp->inp_laddr, lport,
659 sin->sin_addr, sin->sin_port, cred));
664 count = last - first;
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_addrport(pcbinfo, inp->inp_laddr, lport,
677 sin->sin_addr, sin->sin_port, cred));
680 /* This could happen on loopback interface */
681 if (sin->sin_port == lport &&
682 sin->sin_addr.s_addr == inp->inp_laddr.s_addr) {
685 * Duplicate again; give up
687 inp->inp_laddr.s_addr = INADDR_ANY;
688 error = EADDRNOTAVAIL;
694 inp->inp_lport = lport;
696 if (in_pcbinsporthash(inp) != 0) {
697 inp->inp_laddr.s_addr = INADDR_ANY;
704 if (pcbinfo->porttoken)
705 lwkt_reltoken(pcbinfo->porttoken);
710 * Transform old in_pcbconnect() into an inner subroutine for new
711 * in_pcbconnect(): Do some validity-checking on the remote
712 * address (in mbuf 'nam') and then determine local host address
713 * (i.e., which interface) to use to access that remote host.
715 * This preserves definition of in_pcbconnect(), while supporting a
716 * slightly different version for T/TCP. (This is more than
717 * a bit of a kludge, but cleaning up the internal interfaces would
718 * have forced minor changes in every protocol).
721 in_pcbladdr_find(struct inpcb *inp, struct sockaddr *nam,
722 struct sockaddr_in **plocal_sin, struct thread *td, int find)
724 struct in_ifaddr *ia;
725 struct ucred *cred = NULL;
726 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
727 struct sockaddr *jsin;
728 int jailed = 0, alloc_route = 0;
730 if (nam->sa_len != sizeof *sin)
732 if (sin->sin_family != AF_INET)
733 return (EAFNOSUPPORT);
734 if (sin->sin_port == 0)
735 return (EADDRNOTAVAIL);
736 if (td && td->td_proc && td->td_proc->p_ucred)
737 cred = td->td_proc->p_ucred;
738 if (cred && cred->cr_prison)
740 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
741 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
743 * If the destination address is INADDR_ANY,
744 * use the primary local address.
745 * If the supplied address is INADDR_BROADCAST,
746 * and the primary interface supports broadcast,
747 * choose the broadcast address for that interface.
749 if (sin->sin_addr.s_addr == INADDR_ANY)
750 sin->sin_addr = IA_SIN(ia)->sin_addr;
751 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
752 (ia->ia_ifp->if_flags & IFF_BROADCAST))
753 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
760 * If route is known or can be allocated now,
761 * our src addr is taken from the i/f, else punt.
762 * Note that we should check the address family of the cached
763 * destination, in case of sharing the cache with IPv6.
765 ro = &inp->inp_route;
767 (!(ro->ro_rt->rt_flags & RTF_UP) ||
768 ro->ro_dst.sa_family != AF_INET ||
769 satosin(&ro->ro_dst)->sin_addr.s_addr !=
770 sin->sin_addr.s_addr ||
771 inp->inp_socket->so_options & SO_DONTROUTE)) {
775 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
776 (ro->ro_rt == NULL ||
777 ro->ro_rt->rt_ifp == NULL)) {
778 /* No route yet, so try to acquire one */
779 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
780 ro->ro_dst.sa_family = AF_INET;
781 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
782 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
788 * If we found a route, use the address
789 * corresponding to the outgoing interface
790 * unless it is the loopback (in case a route
791 * to our address on another net goes to loopback).
793 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
795 if (jailed_ip(cred->cr_prison,
796 ro->ro_rt->rt_ifa->ifa_addr)) {
797 ia = ifatoia(ro->ro_rt->rt_ifa);
800 ia = ifatoia(ro->ro_rt->rt_ifa);
804 u_short fport = sin->sin_port;
807 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
808 if (ia && jailed && !jailed_ip(cred->cr_prison,
809 sintosa(&ia->ia_addr)))
812 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
813 if (ia && jailed && !jailed_ip(cred->cr_prison,
814 sintosa(&ia->ia_addr)))
816 sin->sin_port = fport;
818 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
819 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
820 if (ia && jailed && !jailed_ip(cred->cr_prison,
821 sintosa(&ia->ia_addr)))
824 if (!jailed && ia == NULL)
828 * If the destination address is multicast and an outgoing
829 * interface has been set as a multicast option, use the
830 * address of that interface as our source address.
832 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
833 inp->inp_moptions != NULL) {
834 struct ip_moptions *imo;
837 imo = inp->inp_moptions;
838 if (imo->imo_multicast_ifp != NULL) {
839 struct in_ifaddr_container *iac;
841 ifp = imo->imo_multicast_ifp;
844 &in_ifaddrheads[mycpuid], ia_link) {
845 if (iac->ia->ia_ifp == ifp) {
855 * Don't do pcblookup call here; return interface in plocal_sin
856 * and exit to caller, that will do the lookup.
858 if (ia == NULL && jailed) {
859 if ((jsin = prison_get_nonlocal(cred->cr_prison, AF_INET, NULL)) != NULL ||
860 (jsin = prison_get_local(cred->cr_prison, AF_INET, NULL)) != NULL) {
861 *plocal_sin = satosin(jsin);
867 *plocal_sin = &ia->ia_addr;
873 struct route *ro = &inp->inp_route;
875 if (ro->ro_rt != NULL)
877 bzero(ro, sizeof(*ro));
879 return (EADDRNOTAVAIL);
883 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
884 struct sockaddr_in **plocal_sin, struct thread *td)
886 return in_pcbladdr_find(inp, nam, plocal_sin, td,
887 (inp->inp_laddr.s_addr == INADDR_ANY));
892 * Connect from a socket to a specified address.
893 * Both address and port must be specified in argument sin.
894 * If don't have a local address for this socket yet,
898 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
900 struct sockaddr_in *if_sin;
901 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
904 /* Call inner routine to assign local interface address. */
905 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
908 if (in_pcblookup_hash(inp->inp_cpcbinfo, sin->sin_addr, sin->sin_port,
909 inp->inp_laddr.s_addr ?
910 inp->inp_laddr : if_sin->sin_addr,
911 inp->inp_lport, FALSE, NULL) != NULL) {
914 if (inp->inp_laddr.s_addr == INADDR_ANY) {
915 if (inp->inp_lport == 0) {
916 error = in_pcbbind(inp, NULL, td);
920 inp->inp_laddr = if_sin->sin_addr;
922 inp->inp_faddr = sin->sin_addr;
923 inp->inp_fport = sin->sin_port;
924 in_pcbinsconnhash(inp);
929 in_pcbdisconnect(struct inpcb *inp)
932 inp->inp_faddr.s_addr = INADDR_ANY;
934 in_pcbremconnhash(inp);
935 if (inp->inp_socket->so_state & SS_NOFDREF)
940 in_pcbdetach(struct inpcb *inp)
942 struct socket *so = inp->inp_socket;
943 struct inpcbinfo *ipi = inp->inp_pcbinfo;
946 ipsec4_delete_pcbpolicy(inp);
948 inp->inp_gencnt = ++ipi->ipi_gencnt;
949 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
952 sofree(so); /* remove pcb ref */
953 if (inp->inp_options)
954 m_free(inp->inp_options);
955 if (inp->inp_route.ro_rt)
956 rtfree(inp->inp_route.ro_rt);
957 ip_freemoptions(inp->inp_moptions);
963 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
964 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
965 * in struct pr_usrreqs, so that protocols can just reference then directly
966 * without the need for a wrapper function. The socket must have a valid
967 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
968 * except through a kernel programming error, so it is acceptable to panic
969 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
970 * because there actually /is/ a programming error somewhere... XXX)
973 in_setsockaddr(struct socket *so, struct sockaddr **nam)
976 struct sockaddr_in *sin;
979 * Do the malloc first in case it blocks.
981 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
982 sin->sin_family = AF_INET;
983 sin->sin_len = sizeof *sin;
989 kfree(sin, M_SONAME);
992 sin->sin_port = inp->inp_lport;
993 sin->sin_addr = inp->inp_laddr;
996 *nam = (struct sockaddr *)sin;
1001 in_setsockaddr_dispatch(netmsg_t msg)
1005 error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1006 lwkt_replymsg(&msg->lmsg, error);
1010 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
1013 struct sockaddr_in *sin;
1016 * Do the malloc first in case it blocks.
1018 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1019 sin->sin_family = AF_INET;
1020 sin->sin_len = sizeof *sin;
1026 kfree(sin, M_SONAME);
1027 return (ECONNRESET);
1029 sin->sin_port = inp->inp_fport;
1030 sin->sin_addr = inp->inp_faddr;
1033 *nam = (struct sockaddr *)sin;
1038 in_setpeeraddr_dispatch(netmsg_t msg)
1042 error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1043 lwkt_replymsg(&msg->lmsg, error);
1047 in_pcbnotifyall(struct inpcbhead *head, struct in_addr faddr, int err,
1048 void (*notify)(struct inpcb *, int))
1050 struct inpcb *inp, *ninp;
1053 * note: if INP_PLACEMARKER is set we must ignore the rest of
1054 * the structure and skip it.
1057 LIST_FOREACH_MUTABLE(inp, head, inp_list, ninp) {
1058 if (inp->inp_flags & INP_PLACEMARKER)
1061 if (!(inp->inp_vflag & INP_IPV4))
1064 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1065 inp->inp_socket == NULL)
1067 (*notify)(inp, err); /* can remove inp from list! */
1073 in_pcbpurgeif0(struct inpcb *head, struct ifnet *ifp)
1076 struct ip_moptions *imo;
1079 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
1080 if (inp->inp_flags & INP_PLACEMARKER)
1082 imo = inp->inp_moptions;
1083 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
1085 * Unselect the outgoing interface if it is being
1088 if (imo->imo_multicast_ifp == ifp)
1089 imo->imo_multicast_ifp = NULL;
1092 * Drop multicast group membership if we joined
1093 * through the interface being detached.
1095 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1097 if (imo->imo_membership[i]->inm_ifp == ifp) {
1098 in_delmulti(imo->imo_membership[i]);
1100 } else if (gap != 0)
1101 imo->imo_membership[i - gap] =
1102 imo->imo_membership[i];
1104 imo->imo_num_memberships -= gap;
1110 * Check for alternatives when higher level complains
1111 * about service problems. For now, invalidate cached
1112 * routing information. If the route was created dynamically
1113 * (by a redirect), time to try a default gateway again.
1116 in_losing(struct inpcb *inp)
1119 struct rt_addrinfo rtinfo;
1121 if ((rt = inp->inp_route.ro_rt)) {
1122 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1123 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1124 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1125 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1126 rtinfo.rti_flags = rt->rt_flags;
1127 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1128 if (rt->rt_flags & RTF_DYNAMIC) {
1129 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1130 rt_mask(rt), rt->rt_flags, NULL);
1132 inp->inp_route.ro_rt = NULL;
1135 * A new route can be allocated
1136 * the next time output is attempted.
1142 * After a routing change, flush old routing
1143 * and allocate a (hopefully) better one.
1146 in_rtchange(struct inpcb *inp, int err)
1148 if (inp->inp_route.ro_rt) {
1149 rtfree(inp->inp_route.ro_rt);
1150 inp->inp_route.ro_rt = NULL;
1152 * A new route can be allocated the next time
1153 * output is attempted.
1159 * Lookup a PCB based on the local address and port.
1162 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1163 u_int lport_arg, int wild_okay, struct ucred *cred)
1166 int matchwild = 3, wildcard;
1167 u_short lport = lport_arg;
1168 struct inpcbporthead *porthash;
1169 struct inpcbport *phd;
1170 struct inpcb *match = NULL;
1173 * If the porthashbase is shared across several cpus we need
1176 if (pcbinfo->porttoken)
1177 lwkt_gettoken(pcbinfo->porttoken);
1180 * Best fit PCB lookup.
1182 * First see if this local port is in use by looking on the
1185 porthash = &pcbinfo->porthashbase[
1186 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
1187 LIST_FOREACH(phd, porthash, phd_hash) {
1188 if (phd->phd_port == lport)
1193 * Port is in use by one or more PCBs. Look for best
1196 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1199 if ((inp->inp_vflag & INP_IPV4) == 0)
1202 if (inp->inp_faddr.s_addr != INADDR_ANY)
1204 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1205 if (laddr.s_addr == INADDR_ANY)
1207 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1210 if (laddr.s_addr != INADDR_ANY)
1213 if (wildcard && !wild_okay)
1215 if (wildcard < matchwild &&
1218 inp->inp_socket->so_cred->cr_prison)) {
1220 matchwild = wildcard;
1221 if (matchwild == 0) {
1227 if (pcbinfo->porttoken)
1228 lwkt_reltoken(pcbinfo->porttoken);
1233 in_pcblocalgroup_last(const struct inpcbinfo *pcbinfo,
1234 const struct inpcb *inp)
1236 const struct inp_localgrphead *hdr;
1237 const struct inp_localgroup *grp;
1240 if (pcbinfo->localgrphashbase == NULL)
1243 hdr = &pcbinfo->localgrphashbase[
1244 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1246 LIST_FOREACH(grp, hdr, il_list) {
1247 if (grp->il_vflag == inp->inp_vflag &&
1248 grp->il_lport == inp->inp_lport &&
1249 memcmp(&grp->il_dependladdr,
1250 &inp->inp_inc.inc_ie.ie_dependladdr,
1251 sizeof(grp->il_dependladdr)) == 0) {
1255 if (grp == NULL || grp->il_inpcnt == 1)
1258 KASSERT(grp->il_inpcnt >= 2,
1259 ("invalid localgroup inp count %d", grp->il_inpcnt));
1260 for (i = 0; i < grp->il_inpcnt; ++i) {
1261 if (grp->il_inp[i] == inp) {
1262 int last = grp->il_inpcnt - 1;
1265 last = grp->il_inpcnt - 2;
1266 return grp->il_inp[last];
1272 static struct inpcb *
1273 inp_localgroup_lookup(const struct inpcbinfo *pcbinfo,
1274 struct in_addr laddr, uint16_t lport, uint32_t pkt_hash)
1276 struct inpcb *local_wild = NULL;
1277 const struct inp_localgrphead *hdr;
1278 const struct inp_localgroup *grp;
1280 hdr = &pcbinfo->localgrphashbase[
1281 INP_PCBLOCALGRPHASH(lport, pcbinfo->localgrphashmask)];
1282 #ifdef INP_LOCALGROUP_HASHTHR
1283 pkt_hash >>= ncpus2_shift;
1287 * Order of socket selection:
1292 * - Local group does not contain jailed sockets
1293 * - Local group does not contain IPv4 mapped INET6 wild sockets
1295 LIST_FOREACH(grp, hdr, il_list) {
1297 if (!(grp->il_vflag & INP_IPV4))
1300 if (grp->il_lport == lport) {
1303 #ifdef INP_LOCALGROUP_HASHTHR
1304 idx = pkt_hash / grp->il_factor;
1305 KASSERT(idx < grp->il_inpcnt && idx >= 0,
1306 ("invalid hash %04x, cnt %d or fact %d",
1307 pkt_hash, grp->il_inpcnt, grp->il_factor));
1310 * Modulo-N is used here, which greatly reduces
1311 * completion queue token contention, thus more
1312 * cpu time is saved.
1314 idx = pkt_hash % grp->il_inpcnt;
1317 if (grp->il_laddr.s_addr == laddr.s_addr)
1318 return grp->il_inp[idx];
1319 else if (grp->il_laddr.s_addr == INADDR_ANY)
1320 local_wild = grp->il_inp[idx];
1323 if (local_wild != NULL)
1329 * Lookup PCB in hash list.
1332 in_pcblookup_pkthash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1333 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1334 boolean_t wildcard, struct ifnet *ifp, const struct mbuf *m)
1336 struct inpcbhead *head;
1337 struct inpcb *inp, *jinp=NULL;
1338 u_short fport = fport_arg, lport = lport_arg;
1341 * First look for an exact match.
1343 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1344 laddr.s_addr, lport, pcbinfo->hashmask)];
1345 LIST_FOREACH(inp, head, inp_hash) {
1347 if (!(inp->inp_vflag & INP_IPV4))
1350 if (in_hosteq(inp->inp_faddr, faddr) &&
1351 in_hosteq(inp->inp_laddr, laddr) &&
1352 inp->inp_fport == fport && inp->inp_lport == lport) {
1354 if (inp->inp_socket == NULL ||
1355 inp->inp_socket->so_cred->cr_prison == NULL) {
1366 struct inpcb *local_wild = NULL;
1367 struct inpcb *jinp_wild = NULL;
1369 struct inpcb *local_wild_mapped = NULL;
1371 struct inpcontainer *ic;
1372 struct inpcontainerhead *chead;
1373 struct sockaddr_in jsin;
1377 * Check local group first
1379 if (pcbinfo->localgrphashbase != NULL &&
1380 m != NULL && (m->m_flags & M_HASH) &&
1381 !(ifp && ifp->if_type == IFT_FAITH)) {
1382 inp = inp_localgroup_lookup(pcbinfo,
1383 laddr, lport, m->m_pkthdr.hash);
1389 * Order of socket selection:
1390 * 1. non-jailed, non-wild.
1391 * 2. non-jailed, wild.
1392 * 3. jailed, non-wild.
1395 jsin.sin_family = AF_INET;
1396 chead = &pcbinfo->wildcardhashbase[
1397 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1398 LIST_FOREACH(ic, chead, ic_list) {
1400 jsin.sin_addr.s_addr = laddr.s_addr;
1402 if (!(inp->inp_vflag & INP_IPV4))
1405 if (inp->inp_socket != NULL)
1406 cred = inp->inp_socket->so_cred;
1409 if (cred != NULL && jailed(cred)) {
1413 if (!jailed_ip(cred->cr_prison,
1414 (struct sockaddr *)&jsin))
1417 if (inp->inp_lport == lport) {
1418 if (ifp && ifp->if_type == IFT_FAITH &&
1419 !(inp->inp_flags & INP_FAITH))
1421 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1422 if (cred != NULL && jailed(cred))
1427 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1429 if (INP_CHECK_SOCKAF(inp->inp_socket,
1431 local_wild_mapped = inp;
1442 if (local_wild != NULL)
1443 return (local_wild);
1445 if (local_wild_mapped != NULL)
1446 return (local_wild_mapped);
1460 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1461 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1462 boolean_t wildcard, struct ifnet *ifp)
1464 return in_pcblookup_pkthash(pcbinfo, faddr, fport_arg,
1465 laddr, lport_arg, wildcard, ifp, NULL);
1469 * Insert PCB into connection hash table.
1472 in_pcbinsconnhash(struct inpcb *inp)
1474 struct inpcbinfo *pcbinfo = inp->inp_cpcbinfo;
1475 struct inpcbhead *bucket;
1476 u_int32_t hashkey_faddr, hashkey_laddr;
1479 if (inp->inp_vflag & INP_IPV6) {
1480 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1481 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1484 hashkey_faddr = inp->inp_faddr.s_addr;
1485 hashkey_laddr = inp->inp_laddr.s_addr;
1490 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1491 ("already on wildcardhash"));
1492 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1493 ("already on connhash"));
1494 inp->inp_flags |= INP_CONNECTED;
1497 * Insert into the connection hash table.
1499 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1500 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1501 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1505 * Remove PCB from connection hash table.
1508 in_pcbremconnhash(struct inpcb *inp)
1510 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1511 LIST_REMOVE(inp, inp_hash);
1512 inp->inp_flags &= ~INP_CONNECTED;
1516 * Insert PCB into port hash table.
1519 in_pcbinsporthash(struct inpcb *inp)
1521 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1522 struct inpcbporthead *pcbporthash;
1523 struct inpcbport *phd;
1526 * If the porthashbase is shared across several cpus we need
1529 if (pcbinfo->porttoken)
1530 lwkt_gettoken(pcbinfo->porttoken);
1533 * Insert into the port hash table.
1535 pcbporthash = &pcbinfo->porthashbase[
1536 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->porthashmask)];
1538 /* Go through port list and look for a head for this lport. */
1539 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1540 if (phd->phd_port == inp->inp_lport)
1544 /* If none exists, malloc one and tack it on. */
1546 KKASSERT(pcbinfo->portsave != NULL);
1547 phd = pcbinfo->portsave;
1548 pcbinfo->portsave = NULL;
1549 phd->phd_port = inp->inp_lport;
1550 LIST_INIT(&phd->phd_pcblist);
1551 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1555 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1557 if (pcbinfo->porttoken)
1558 lwkt_reltoken(pcbinfo->porttoken);
1559 if (pcbinfo->portsave == NULL) {
1560 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1561 M_PCB, M_INTWAIT | M_ZERO);
1566 static struct inp_localgroup *
1567 inp_localgroup_alloc(struct inp_localgrphead *hdr, u_char vflag,
1568 uint16_t port, const union in_dependaddr *addr, int size)
1570 struct inp_localgroup *grp;
1572 grp = kmalloc(__offsetof(struct inp_localgroup, il_inp[size]),
1573 M_TEMP, M_INTWAIT | M_ZERO);
1574 grp->il_vflag = vflag;
1575 grp->il_lport = port;
1576 grp->il_dependladdr = *addr;
1577 grp->il_inpsiz = size;
1579 LIST_INSERT_HEAD(hdr, grp, il_list);
1585 inp_localgroup_free(struct inp_localgroup *grp)
1587 LIST_REMOVE(grp, il_list);
1591 static struct inp_localgroup *
1592 inp_localgroup_resize(struct inp_localgrphead *hdr,
1593 struct inp_localgroup *old_grp, int size)
1595 struct inp_localgroup *grp;
1598 grp = inp_localgroup_alloc(hdr, old_grp->il_vflag,
1599 old_grp->il_lport, &old_grp->il_dependladdr, size);
1601 KASSERT(old_grp->il_inpcnt < grp->il_inpsiz,
1602 ("invalid new local group size %d and old local group count %d",
1603 grp->il_inpsiz, old_grp->il_inpcnt));
1604 for (i = 0; i < old_grp->il_inpcnt; ++i)
1605 grp->il_inp[i] = old_grp->il_inp[i];
1606 grp->il_inpcnt = old_grp->il_inpcnt;
1607 grp->il_factor = old_grp->il_factor;
1609 inp_localgroup_free(old_grp);
1615 inp_localgroup_factor(struct inp_localgroup *grp)
1618 ((uint32_t)(0xffff >> ncpus2_shift) / grp->il_inpcnt) + 1;
1619 KASSERT(grp->il_factor != 0, ("invalid local group factor, "
1620 "ncpus2_shift %d, inpcnt %d", ncpus2_shift, grp->il_inpcnt));
1624 in_pcbinslocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1626 struct inp_localgrphead *hdr;
1627 struct inp_localgroup *grp;
1630 if (pcbinfo->localgrphashbase == NULL)
1634 * XXX don't allow jailed socket to join local group
1636 if (inp->inp_socket != NULL)
1637 cred = inp->inp_socket->so_cred;
1640 if (cred != NULL && jailed(cred))
1645 * XXX don't allow IPv4 mapped INET6 wild socket
1647 if ((inp->inp_vflag & INP_IPV4) &&
1648 inp->inp_laddr.s_addr == INADDR_ANY &&
1649 INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6))
1653 hdr = &pcbinfo->localgrphashbase[
1654 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1656 LIST_FOREACH(grp, hdr, il_list) {
1657 if (grp->il_vflag == inp->inp_vflag &&
1658 grp->il_lport == inp->inp_lport &&
1659 memcmp(&grp->il_dependladdr,
1660 &inp->inp_inc.inc_ie.ie_dependladdr,
1661 sizeof(grp->il_dependladdr)) == 0) {
1666 /* Create new local group */
1667 grp = inp_localgroup_alloc(hdr, inp->inp_vflag,
1668 inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr,
1669 INP_LOCALGROUP_SIZMIN);
1670 } else if (grp->il_inpcnt == grp->il_inpsiz) {
1671 if (grp->il_inpsiz >= INP_LOCALGROUP_SIZMAX) {
1672 static int limit_logged = 0;
1674 if (!limit_logged) {
1676 kprintf("local group port %d, "
1677 "limit reached\n", ntohs(grp->il_lport));
1682 /* Expand this local group */
1683 grp = inp_localgroup_resize(hdr, grp, grp->il_inpsiz * 2);
1686 KASSERT(grp->il_inpcnt < grp->il_inpsiz,
1687 ("invalid local group size %d and count %d",
1688 grp->il_inpsiz, grp->il_inpcnt));
1689 grp->il_inp[grp->il_inpcnt] = inp;
1691 inp_localgroup_factor(grp);
1695 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1697 struct inpcontainer *ic;
1698 struct inpcontainerhead *bucket;
1700 in_pcbinslocalgrphash_oncpu(inp, pcbinfo);
1702 bucket = &pcbinfo->wildcardhashbase[
1703 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1705 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1707 LIST_INSERT_HEAD(bucket, ic, ic_list);
1711 * Insert PCB into wildcard hash table.
1714 in_pcbinswildcardhash(struct inpcb *inp)
1716 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1718 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1719 ("already on connhash"));
1720 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1721 ("already on wildcardhash"));
1722 inp->inp_flags |= INP_WILDCARD;
1724 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1728 in_pcbremlocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1730 struct inp_localgrphead *hdr;
1731 struct inp_localgroup *grp;
1733 if (pcbinfo->localgrphashbase == NULL)
1736 hdr = &pcbinfo->localgrphashbase[
1737 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1739 LIST_FOREACH(grp, hdr, il_list) {
1742 for (i = 0; i < grp->il_inpcnt; ++i) {
1743 if (grp->il_inp[i] != inp)
1746 if (grp->il_inpcnt == 1) {
1747 /* Free this local group */
1748 inp_localgroup_free(grp);
1750 /* Pull up inpcbs */
1751 for (; i + 1 < grp->il_inpcnt; ++i)
1752 grp->il_inp[i] = grp->il_inp[i + 1];
1754 inp_localgroup_factor(grp);
1756 if (grp->il_inpsiz > INP_LOCALGROUP_SIZMIN &&
1757 grp->il_inpcnt <= (grp->il_inpsiz / 4)) {
1758 /* Shrink this local group */
1759 grp = inp_localgroup_resize(hdr, grp,
1760 grp->il_inpsiz / 2);
1769 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1771 struct inpcontainer *ic;
1772 struct inpcontainerhead *head;
1774 in_pcbremlocalgrphash_oncpu(inp, pcbinfo);
1777 head = &pcbinfo->wildcardhashbase[
1778 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1780 LIST_FOREACH(ic, head, ic_list) {
1781 if (ic->ic_inp == inp)
1784 return; /* not found! */
1787 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
1788 kfree(ic, M_TEMP); /* deallocate container */
1792 * Remove PCB from wildcard hash table.
1795 in_pcbremwildcardhash(struct inpcb *inp)
1797 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1799 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1800 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
1801 inp->inp_flags &= ~INP_WILDCARD;
1805 * Remove PCB from various lists.
1808 in_pcbremlists(struct inpcb *inp)
1810 struct inpcbinfo *pcbinfo;
1812 if (inp->inp_lport) {
1813 struct inpcbport *phd;
1815 pcbinfo = inp->inp_pcbinfo;
1816 if (pcbinfo->porttoken)
1817 lwkt_gettoken(pcbinfo->porttoken);
1820 LIST_REMOVE(inp, inp_portlist);
1821 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1822 LIST_REMOVE(phd, phd_hash);
1825 if (pcbinfo->porttoken)
1826 lwkt_reltoken(pcbinfo->porttoken);
1828 if (inp->inp_flags & INP_WILDCARD) {
1829 in_pcbremwildcardhash(inp);
1830 } else if (inp->inp_flags & INP_CONNECTED) {
1831 in_pcbremconnhash(inp);
1833 LIST_REMOVE(inp, inp_list);
1834 inp->inp_pcbinfo->ipi_count--;
1838 prison_xinpcb(struct thread *td, struct inpcb *inp)
1842 if (td->td_proc == NULL)
1844 cr = td->td_proc->p_ucred;
1845 if (cr->cr_prison == NULL)
1847 if (inp->inp_socket && inp->inp_socket->so_cred &&
1848 inp->inp_socket->so_cred->cr_prison &&
1849 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
1855 in_pcblist_global(SYSCTL_HANDLER_ARGS)
1857 struct inpcbinfo *pcbinfo = arg1;
1858 struct inpcb *inp, *marker;
1863 * The process of preparing the TCB list is too time-consuming and
1864 * resource-intensive to repeat twice on every request.
1866 if (req->oldptr == NULL) {
1867 n = pcbinfo->ipi_count;
1868 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1872 if (req->newptr != NULL)
1876 * OK, now we're committed to doing something. Re-fetch ipi_count
1877 * after obtaining the generation count.
1879 n = pcbinfo->ipi_count;
1881 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
1882 marker->inp_flags |= INP_PLACEMARKER;
1883 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1888 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
1889 LIST_REMOVE(marker, inp_list);
1890 LIST_INSERT_AFTER(inp, marker, inp_list);
1892 if (inp->inp_flags & INP_PLACEMARKER)
1894 if (prison_xinpcb(req->td, inp))
1896 bzero(&xi, sizeof xi);
1897 xi.xi_len = sizeof xi;
1898 bcopy(inp, &xi.xi_inp, sizeof *inp);
1899 if (inp->inp_socket)
1900 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1901 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
1905 LIST_REMOVE(marker, inp_list);
1906 if (error == 0 && i < n) {
1907 bzero(&xi, sizeof xi);
1908 xi.xi_len = sizeof xi;
1910 error = SYSCTL_OUT(req, &xi, sizeof xi);
1914 kfree(marker, M_TEMP);
1919 in_pcblist_global_nomarker(SYSCTL_HANDLER_ARGS, struct xinpcb **xi0, int *nxi0)
1921 struct inpcbinfo *pcbinfo = arg1;
1930 * The process of preparing the PCB list is too time-consuming and
1931 * resource-intensive to repeat twice on every request.
1933 if (req->oldptr == NULL) {
1934 int n = pcbinfo->ipi_count;
1936 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1940 if (req->newptr != NULL)
1943 if (pcbinfo->ipi_count == 0)
1947 xi = kmalloc(pcbinfo->ipi_count * sizeof(*xi), M_TEMP,
1948 M_WAITOK | M_ZERO | M_NULLOK);
1952 LIST_FOREACH(inp, &pcbinfo->pcblisthead, inp_list) {
1953 struct xinpcb *xi_ptr = &xi[nxi];
1955 if (prison_xinpcb(req->td, inp))
1958 xi_ptr->xi_len = sizeof(*xi_ptr);
1959 bcopy(inp, &xi_ptr->xi_inp, sizeof(*inp));
1960 if (inp->inp_socket)
1961 sotoxsocket(inp->inp_socket, &xi_ptr->xi_socket);
1977 in_savefaddr(struct socket *so, const struct sockaddr *faddr)
1979 struct sockaddr_in *sin;
1981 KASSERT(faddr->sa_family == AF_INET,
1982 ("not AF_INET faddr %d", faddr->sa_family));
1984 sin = kmalloc(sizeof(*sin), M_SONAME, M_WAITOK | M_ZERO);
1985 sin->sin_family = AF_INET;
1986 sin->sin_len = sizeof(*sin);
1987 sin->sin_port = ((const struct sockaddr_in *)faddr)->sin_port;
1988 sin->sin_addr = ((const struct sockaddr_in *)faddr)->sin_addr;
1990 so->so_faddr = (struct sockaddr *)sin;