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 * 4. 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 struct in_addr zeroin_addr;
121 * These configure the range of local port addresses assigned to
122 * "unspecified" outgoing connections/packets/whatever.
124 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
125 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
127 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
128 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
130 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
131 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
133 #define RANGECHK(var, min, max) \
134 if ((var) < (min)) { (var) = (min); } \
135 else if ((var) > (max)) { (var) = (max); }
137 int udpencap_enable = 1; /* enabled by default */
138 int udpencap_port = 4500; /* triggers decapsulation */
141 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
145 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
147 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
148 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
150 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
151 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
153 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
154 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
161 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
163 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
164 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
165 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
166 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
167 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
168 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
169 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
170 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
171 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
172 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
173 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
174 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
177 * in_pcb.c: manage the Protocol Control Blocks.
179 * NOTE: It is assumed that most of these functions will be called from
180 * a critical section. XXX - There are, unfortunately, a few exceptions
181 * to this rule that should be fixed.
183 * NOTE: The caller should initialize the cpu field to the cpu running the
184 * protocol stack associated with this inpcbinfo.
188 in_pcbinfo_init(struct inpcbinfo *pcbinfo)
190 LIST_INIT(&pcbinfo->pcblisthead);
192 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave), M_PCB,
196 struct baddynamicports baddynamicports;
199 * Check if the specified port is invalid for dynamic allocation.
202 in_baddynamic(u_int16_t port, u_int16_t proto)
206 return (DP_ISSET(baddynamicports.tcp, port));
209 /* Cannot preset this as it is a sysctl */
210 if (port == udpencap_port)
213 return (DP_ISSET(baddynamicports.udp, port));
221 * Allocate a PCB and associate it with the socket.
224 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
231 inp = kmalloc(pcbinfo->ipi_size, M_PCB, M_WAITOK|M_ZERO);
232 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
233 inp->inp_pcbinfo = inp->inp_cpcbinfo = pcbinfo;
234 inp->inp_socket = so;
236 error = ipsec_init_policy(so, &inp->inp_sp);
243 if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
244 inp->inp_flags |= IN6P_IPV6_V6ONLY;
245 if (ip6_auto_flowlabel)
246 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
250 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
251 pcbinfo->ipi_count++;
256 * Unlink a pcb with the intention of moving it to another cpu with a
257 * different pcbinfo. While unlinked nothing should attempt to dereference
258 * inp_pcbinfo, NULL it out so we assert if it does.
261 in_pcbunlink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
263 KKASSERT(inp->inp_pcbinfo == pcbinfo);
265 LIST_REMOVE(inp, inp_list);
266 pcbinfo->ipi_count--;
267 inp->inp_pcbinfo = NULL;
271 * Relink a pcb into a new pcbinfo.
274 in_pcblink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
276 KKASSERT(inp->inp_pcbinfo == NULL);
277 inp->inp_pcbinfo = pcbinfo;
278 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
279 pcbinfo->ipi_count++;
283 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
285 struct socket *so = inp->inp_socket;
286 unsigned short *lastport;
287 struct sockaddr_in *sin;
288 struct sockaddr_in jsin;
289 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
290 struct ucred *cred = NULL;
292 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
295 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
296 return (EADDRNOTAVAIL);
297 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
298 return (EINVAL); /* already bound */
300 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
301 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
303 cred = td->td_proc->p_ucred;
306 * This has to be atomic. If the porthash is shared across multiple
307 * protocol threads (aka tcp) then the token will be non-NULL.
309 if (pcbinfo->porttoken)
310 lwkt_gettoken(pcbinfo->porttoken);
313 sin = (struct sockaddr_in *)nam;
314 if (nam->sa_len != sizeof *sin) {
320 * We should check the family, but old programs
321 * incorrectly fail to initialize it.
323 if (sin->sin_family != AF_INET) {
324 error = EAFNOSUPPORT;
328 if (!prison_replace_wildcards(td, nam)) {
332 lport = sin->sin_port;
333 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
335 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
336 * allow complete duplication of binding if
337 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
338 * and a multicast address is bound on both
339 * new and duplicated sockets.
341 if (so->so_options & SO_REUSEADDR)
342 reuseport = SO_REUSEADDR | SO_REUSEPORT;
343 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
344 sin->sin_port = 0; /* yech... */
345 bzero(&sin->sin_zero, sizeof sin->sin_zero);
346 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL) {
347 error = EADDRNOTAVAIL;
355 if (ntohs(lport) < IPPORT_RESERVED &&
357 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0)) {
361 if (so->so_cred->cr_uid != 0 &&
362 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
363 t = in_pcblookup_local(pcbinfo,
369 (!in_nullhost(sin->sin_addr) ||
370 !in_nullhost(t->inp_laddr) ||
371 (t->inp_socket->so_options &
372 SO_REUSEPORT) == 0) &&
373 (so->so_cred->cr_uid !=
374 t->inp_socket->so_cred->cr_uid)) {
376 if (!in_nullhost(sin->sin_addr) ||
377 !in_nullhost(t->inp_laddr) ||
379 INP_SOCKAF(t->inp_socket))
387 if (cred && !prison_replace_wildcards(td, nam)) {
388 error = EADDRNOTAVAIL;
391 t = in_pcblookup_local(pcbinfo, sin->sin_addr, lport,
393 if (t && !(reuseport & t->inp_socket->so_options)) {
395 if (!in_nullhost(sin->sin_addr) ||
396 !in_nullhost(t->inp_laddr) ||
397 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
405 inp->inp_laddr = sin->sin_addr;
411 jsin.sin_family = AF_INET;
412 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
413 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
414 inp->inp_laddr.s_addr = INADDR_ANY;
418 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
420 inp->inp_flags |= INP_ANONPORT;
422 if (inp->inp_flags & INP_HIGHPORT) {
423 first = ipport_hifirstauto; /* sysctl */
424 last = ipport_hilastauto;
425 lastport = &pcbinfo->lasthi;
426 } else if (inp->inp_flags & INP_LOWPORT) {
428 (error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
429 inp->inp_laddr.s_addr = INADDR_ANY;
432 first = ipport_lowfirstauto; /* 1023 */
433 last = ipport_lowlastauto; /* 600 */
434 lastport = &pcbinfo->lastlow;
436 first = ipport_firstauto; /* sysctl */
437 last = ipport_lastauto;
438 lastport = &pcbinfo->lastport;
441 * Simple check to ensure all ports are not used up causing
444 * We split the two cases (up and down) so that the direction
445 * is not being tested on each round of the loop.
451 count = first - last;
454 if (count-- < 0) { /* completely used? */
455 inp->inp_laddr.s_addr = INADDR_ANY;
456 error = EADDRNOTAVAIL;
460 if (*lastport > first || *lastport < last)
462 lport = htons(*lastport);
463 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
469 count = last - first;
472 if (count-- < 0) { /* completely used? */
473 inp->inp_laddr.s_addr = INADDR_ANY;
474 error = EADDRNOTAVAIL;
478 if (*lastport < first || *lastport > last)
480 lport = htons(*lastport);
481 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
485 inp->inp_lport = lport;
487 jsin.sin_family = AF_INET;
488 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
489 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin)) {
490 inp->inp_laddr.s_addr = INADDR_ANY;
495 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
497 if (in_pcbinsporthash(inp) != 0) {
498 inp->inp_laddr.s_addr = INADDR_ANY;
505 if (pcbinfo->porttoken)
506 lwkt_reltoken(pcbinfo->porttoken);
510 static struct inpcb *
511 in_pcblookup_addrport(struct inpcbinfo *pcbinfo, struct in_addr laddr,
512 u_short lport, struct in_addr faddr, u_short fport, struct ucred *cred)
515 struct inpcbporthead *porthash;
516 struct inpcbport *phd;
517 struct inpcb *match = NULL;
520 * If the porthashbase is shared across several cpus we need
523 if (pcbinfo->porttoken)
524 lwkt_gettoken(pcbinfo->porttoken);
527 * Best fit PCB lookup.
529 * First see if this local port is in use by looking on the
532 porthash = &pcbinfo->porthashbase[
533 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
534 LIST_FOREACH(phd, porthash, phd_hash) {
535 if (phd->phd_port == lport)
539 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
541 if ((inp->inp_vflag & INP_IPV4) == 0)
544 if (inp->inp_laddr.s_addr != INADDR_ANY &&
545 inp->inp_laddr.s_addr != laddr.s_addr)
548 if (inp->inp_faddr.s_addr != INADDR_ANY &&
549 inp->inp_faddr.s_addr != faddr.s_addr)
552 if (inp->inp_fport != 0 && inp->inp_fport != fport)
557 inp->inp_socket->so_cred->cr_prison) {
563 if (pcbinfo->porttoken)
564 lwkt_reltoken(pcbinfo->porttoken);
569 in_pcbconn_bind(struct inpcb *inp, const struct sockaddr *nam,
572 struct proc *p = td->td_proc;
573 unsigned short *lastport;
574 const struct sockaddr_in *sin = (const struct sockaddr_in *)nam;
575 struct sockaddr_in jsin;
576 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
577 struct ucred *cred = NULL;
580 int count, error, dup = 0;
582 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
583 return (EADDRNOTAVAIL);
585 KKASSERT(inp->inp_laddr.s_addr != INADDR_ANY);
586 if (inp->inp_lport != 0)
587 return (EINVAL); /* already bound */
593 * This has to be atomic. If the porthash is shared across multiple
594 * protocol threads (aka tcp) then the token will be non-NULL.
596 if (pcbinfo->porttoken)
597 lwkt_gettoken(pcbinfo->porttoken);
599 jsin.sin_family = AF_INET;
600 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
601 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
602 inp->inp_laddr.s_addr = INADDR_ANY;
606 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
608 inp->inp_flags |= INP_ANONPORT;
610 if (inp->inp_flags & INP_HIGHPORT) {
611 first = ipport_hifirstauto; /* sysctl */
612 last = ipport_hilastauto;
613 lastport = &pcbinfo->lasthi;
614 } else if (inp->inp_flags & INP_LOWPORT) {
616 (error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
617 inp->inp_laddr.s_addr = INADDR_ANY;
620 first = ipport_lowfirstauto; /* 1023 */
621 last = ipport_lowlastauto; /* 600 */
622 lastport = &pcbinfo->lastlow;
624 first = ipport_firstauto; /* sysctl */
625 last = ipport_lastauto;
626 lastport = &pcbinfo->lastport;
631 * Simple check to ensure all ports are not used up causing
634 * We split the two cases (up and down) so that the direction
635 * is not being tested on each round of the loop.
641 count = first - last;
644 if (count-- < 0) { /* completely used? */
645 inp->inp_laddr.s_addr = INADDR_ANY;
646 error = EADDRNOTAVAIL;
650 if (*lastport > first || *lastport < last)
652 lport = htons(*lastport);
653 } while (in_pcblookup_addrport(pcbinfo, inp->inp_laddr, lport,
654 sin->sin_addr, sin->sin_port, cred));
659 count = last - first;
662 if (count-- < 0) { /* completely used? */
663 inp->inp_laddr.s_addr = INADDR_ANY;
664 error = EADDRNOTAVAIL;
668 if (*lastport < first || *lastport > last)
670 lport = htons(*lastport);
671 } while (in_pcblookup_addrport(pcbinfo, inp->inp_laddr, lport,
672 sin->sin_addr, sin->sin_port, cred));
675 /* This could happen on loopback interface */
676 if (sin->sin_port == lport &&
677 sin->sin_addr.s_addr == inp->inp_laddr.s_addr) {
680 * Duplicate again; give up
682 inp->inp_laddr.s_addr = INADDR_ANY;
683 error = EADDRNOTAVAIL;
689 inp->inp_lport = lport;
691 jsin.sin_family = AF_INET;
692 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
693 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin)) {
694 inp->inp_laddr.s_addr = INADDR_ANY;
699 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
701 if (in_pcbinsporthash(inp) != 0) {
702 inp->inp_laddr.s_addr = INADDR_ANY;
709 if (pcbinfo->porttoken)
710 lwkt_reltoken(pcbinfo->porttoken);
715 * Transform old in_pcbconnect() into an inner subroutine for new
716 * in_pcbconnect(): Do some validity-checking on the remote
717 * address (in mbuf 'nam') and then determine local host address
718 * (i.e., which interface) to use to access that remote host.
720 * This preserves definition of in_pcbconnect(), while supporting a
721 * slightly different version for T/TCP. (This is more than
722 * a bit of a kludge, but cleaning up the internal interfaces would
723 * have forced minor changes in every protocol).
726 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
727 struct sockaddr_in **plocal_sin, struct thread *td)
729 struct in_ifaddr *ia;
730 struct ucred *cred = NULL;
731 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
732 struct sockaddr *jsin;
733 int jailed = 0, alloc_route = 0;
735 if (nam->sa_len != sizeof *sin)
737 if (sin->sin_family != AF_INET)
738 return (EAFNOSUPPORT);
739 if (sin->sin_port == 0)
740 return (EADDRNOTAVAIL);
741 if (td && td->td_proc && td->td_proc->p_ucred)
742 cred = td->td_proc->p_ucred;
743 if (cred && cred->cr_prison)
745 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
746 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
748 * If the destination address is INADDR_ANY,
749 * use the primary local address.
750 * If the supplied address is INADDR_BROADCAST,
751 * and the primary interface supports broadcast,
752 * choose the broadcast address for that interface.
754 if (sin->sin_addr.s_addr == INADDR_ANY)
755 sin->sin_addr = IA_SIN(ia)->sin_addr;
756 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
757 (ia->ia_ifp->if_flags & IFF_BROADCAST))
758 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
760 if (inp->inp_laddr.s_addr == INADDR_ANY) {
765 * If route is known or can be allocated now,
766 * our src addr is taken from the i/f, else punt.
767 * Note that we should check the address family of the cached
768 * destination, in case of sharing the cache with IPv6.
770 ro = &inp->inp_route;
772 (!(ro->ro_rt->rt_flags & RTF_UP) ||
773 ro->ro_dst.sa_family != AF_INET ||
774 satosin(&ro->ro_dst)->sin_addr.s_addr !=
775 sin->sin_addr.s_addr ||
776 inp->inp_socket->so_options & SO_DONTROUTE)) {
780 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
781 (ro->ro_rt == NULL ||
782 ro->ro_rt->rt_ifp == NULL)) {
783 /* No route yet, so try to acquire one */
784 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
785 ro->ro_dst.sa_family = AF_INET;
786 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
787 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
793 * If we found a route, use the address
794 * corresponding to the outgoing interface
795 * unless it is the loopback (in case a route
796 * to our address on another net goes to loopback).
798 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
800 if (jailed_ip(cred->cr_prison,
801 ro->ro_rt->rt_ifa->ifa_addr)) {
802 ia = ifatoia(ro->ro_rt->rt_ifa);
805 ia = ifatoia(ro->ro_rt->rt_ifa);
809 u_short fport = sin->sin_port;
812 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
813 if (ia && jailed && !jailed_ip(cred->cr_prison,
814 sintosa(&ia->ia_addr)))
817 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
818 if (ia && jailed && !jailed_ip(cred->cr_prison,
819 sintosa(&ia->ia_addr)))
821 sin->sin_port = fport;
823 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
824 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
825 if (ia && jailed && !jailed_ip(cred->cr_prison,
826 sintosa(&ia->ia_addr)))
829 if (!jailed && ia == NULL)
833 * If the destination address is multicast and an outgoing
834 * interface has been set as a multicast option, use the
835 * address of that interface as our source address.
837 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
838 inp->inp_moptions != NULL) {
839 struct ip_moptions *imo;
842 imo = inp->inp_moptions;
843 if (imo->imo_multicast_ifp != NULL) {
844 struct in_ifaddr_container *iac;
846 ifp = imo->imo_multicast_ifp;
849 &in_ifaddrheads[mycpuid], ia_link) {
850 if (iac->ia->ia_ifp == ifp) {
860 * Don't do pcblookup call here; return interface in plocal_sin
861 * and exit to caller, that will do the lookup.
863 if (ia == NULL && jailed) {
864 if ((jsin = prison_get_nonlocal(cred->cr_prison, AF_INET, NULL)) != NULL ||
865 (jsin = prison_get_local(cred->cr_prison, AF_INET, NULL)) != NULL) {
866 *plocal_sin = satosin(jsin);
872 *plocal_sin = &ia->ia_addr;
878 struct route *ro = &inp->inp_route;
880 if (ro->ro_rt != NULL)
882 bzero(ro, sizeof(*ro));
884 return (EADDRNOTAVAIL);
889 * Connect from a socket to a specified address.
890 * Both address and port must be specified in argument sin.
891 * If don't have a local address for this socket yet,
895 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
897 struct sockaddr_in *if_sin;
898 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
901 /* Call inner routine to assign local interface address. */
902 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
905 if (in_pcblookup_hash(inp->inp_cpcbinfo, sin->sin_addr, sin->sin_port,
906 inp->inp_laddr.s_addr ?
907 inp->inp_laddr : if_sin->sin_addr,
908 inp->inp_lport, FALSE, NULL) != NULL) {
911 if (inp->inp_laddr.s_addr == INADDR_ANY) {
912 if (inp->inp_lport == 0) {
913 error = in_pcbbind(inp, NULL, td);
917 inp->inp_laddr = if_sin->sin_addr;
919 inp->inp_faddr = sin->sin_addr;
920 inp->inp_fport = sin->sin_port;
921 in_pcbinsconnhash(inp);
926 in_pcbdisconnect(struct inpcb *inp)
929 inp->inp_faddr.s_addr = INADDR_ANY;
931 in_pcbremconnhash(inp);
932 if (inp->inp_socket->so_state & SS_NOFDREF)
937 in_pcbdetach(struct inpcb *inp)
939 struct socket *so = inp->inp_socket;
940 struct inpcbinfo *ipi = inp->inp_pcbinfo;
943 ipsec4_delete_pcbpolicy(inp);
945 inp->inp_gencnt = ++ipi->ipi_gencnt;
946 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
949 sofree(so); /* remove pcb ref */
950 if (inp->inp_options)
951 m_free(inp->inp_options);
952 if (inp->inp_route.ro_rt)
953 rtfree(inp->inp_route.ro_rt);
954 ip_freemoptions(inp->inp_moptions);
960 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
961 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
962 * in struct pr_usrreqs, so that protocols can just reference then directly
963 * without the need for a wrapper function. The socket must have a valid
964 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
965 * except through a kernel programming error, so it is acceptable to panic
966 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
967 * because there actually /is/ a programming error somewhere... XXX)
970 in_setsockaddr(struct socket *so, struct sockaddr **nam)
973 struct sockaddr_in *sin;
976 * Do the malloc first in case it blocks.
978 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
979 sin->sin_family = AF_INET;
980 sin->sin_len = sizeof *sin;
986 kfree(sin, M_SONAME);
989 sin->sin_port = inp->inp_lport;
990 sin->sin_addr = inp->inp_laddr;
993 *nam = (struct sockaddr *)sin;
998 in_setsockaddr_dispatch(netmsg_t msg)
1002 error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1003 lwkt_replymsg(&msg->lmsg, error);
1007 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
1010 struct sockaddr_in *sin;
1013 * Do the malloc first in case it blocks.
1015 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1016 sin->sin_family = AF_INET;
1017 sin->sin_len = sizeof *sin;
1023 kfree(sin, M_SONAME);
1024 return (ECONNRESET);
1026 sin->sin_port = inp->inp_fport;
1027 sin->sin_addr = inp->inp_faddr;
1030 *nam = (struct sockaddr *)sin;
1035 in_setpeeraddr_dispatch(netmsg_t msg)
1039 error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1040 lwkt_replymsg(&msg->lmsg, error);
1044 in_pcbnotifyall(struct inpcbhead *head, struct in_addr faddr, int err,
1045 void (*notify)(struct inpcb *, int))
1047 struct inpcb *inp, *ninp;
1050 * note: if INP_PLACEMARKER is set we must ignore the rest of
1051 * the structure and skip it.
1054 LIST_FOREACH_MUTABLE(inp, head, inp_list, ninp) {
1055 if (inp->inp_flags & INP_PLACEMARKER)
1058 if (!(inp->inp_vflag & INP_IPV4))
1061 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1062 inp->inp_socket == NULL)
1064 (*notify)(inp, err); /* can remove inp from list! */
1070 in_pcbpurgeif0(struct inpcb *head, struct ifnet *ifp)
1073 struct ip_moptions *imo;
1076 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
1077 if (inp->inp_flags & INP_PLACEMARKER)
1079 imo = inp->inp_moptions;
1080 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
1082 * Unselect the outgoing interface if it is being
1085 if (imo->imo_multicast_ifp == ifp)
1086 imo->imo_multicast_ifp = NULL;
1089 * Drop multicast group membership if we joined
1090 * through the interface being detached.
1092 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1094 if (imo->imo_membership[i]->inm_ifp == ifp) {
1095 in_delmulti(imo->imo_membership[i]);
1097 } else if (gap != 0)
1098 imo->imo_membership[i - gap] =
1099 imo->imo_membership[i];
1101 imo->imo_num_memberships -= gap;
1107 * Check for alternatives when higher level complains
1108 * about service problems. For now, invalidate cached
1109 * routing information. If the route was created dynamically
1110 * (by a redirect), time to try a default gateway again.
1113 in_losing(struct inpcb *inp)
1116 struct rt_addrinfo rtinfo;
1118 if ((rt = inp->inp_route.ro_rt)) {
1119 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1120 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1121 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1122 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1123 rtinfo.rti_flags = rt->rt_flags;
1124 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1125 if (rt->rt_flags & RTF_DYNAMIC)
1126 rtrequest1_global(RTM_DELETE, &rtinfo, NULL, NULL);
1127 inp->inp_route.ro_rt = NULL;
1130 * A new route can be allocated
1131 * the next time output is attempted.
1137 * After a routing change, flush old routing
1138 * and allocate a (hopefully) better one.
1141 in_rtchange(struct inpcb *inp, int err)
1143 if (inp->inp_route.ro_rt) {
1144 rtfree(inp->inp_route.ro_rt);
1145 inp->inp_route.ro_rt = NULL;
1147 * A new route can be allocated the next time
1148 * output is attempted.
1154 * Lookup a PCB based on the local address and port.
1157 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1158 u_int lport_arg, int wild_okay, struct ucred *cred)
1161 int matchwild = 3, wildcard;
1162 u_short lport = lport_arg;
1163 struct inpcbporthead *porthash;
1164 struct inpcbport *phd;
1165 struct inpcb *match = NULL;
1168 * If the porthashbase is shared across several cpus we need
1171 if (pcbinfo->porttoken)
1172 lwkt_gettoken(pcbinfo->porttoken);
1175 * Best fit PCB lookup.
1177 * First see if this local port is in use by looking on the
1180 porthash = &pcbinfo->porthashbase[
1181 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
1182 LIST_FOREACH(phd, porthash, phd_hash) {
1183 if (phd->phd_port == lport)
1188 * Port is in use by one or more PCBs. Look for best
1191 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1194 if ((inp->inp_vflag & INP_IPV4) == 0)
1197 if (inp->inp_faddr.s_addr != INADDR_ANY)
1199 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1200 if (laddr.s_addr == INADDR_ANY)
1202 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1205 if (laddr.s_addr != INADDR_ANY)
1208 if (wildcard && !wild_okay)
1210 if (wildcard < matchwild &&
1213 inp->inp_socket->so_cred->cr_prison)) {
1215 matchwild = wildcard;
1216 if (matchwild == 0) {
1222 if (pcbinfo->porttoken)
1223 lwkt_reltoken(pcbinfo->porttoken);
1228 * Lookup PCB in hash list.
1231 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1232 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1233 boolean_t wildcard, struct ifnet *ifp)
1235 struct inpcbhead *head;
1236 struct inpcb *inp, *jinp=NULL;
1237 u_short fport = fport_arg, lport = lport_arg;
1240 * First look for an exact match.
1242 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1243 laddr.s_addr, lport, pcbinfo->hashmask)];
1244 LIST_FOREACH(inp, head, inp_hash) {
1246 if (!(inp->inp_vflag & INP_IPV4))
1249 if (in_hosteq(inp->inp_faddr, faddr) &&
1250 in_hosteq(inp->inp_laddr, laddr) &&
1251 inp->inp_fport == fport && inp->inp_lport == lport) {
1253 if (inp->inp_socket == NULL ||
1254 inp->inp_socket->so_cred->cr_prison == NULL) {
1265 struct inpcb *local_wild = NULL;
1266 struct inpcb *jinp_wild = NULL;
1268 struct inpcb *local_wild_mapped = NULL;
1270 struct inpcontainer *ic;
1271 struct inpcontainerhead *chead;
1272 struct sockaddr_in jsin;
1276 * Order of socket selection:
1277 * 1. non-jailed, non-wild.
1278 * 2. non-jailed, wild.
1279 * 3. jailed, non-wild.
1282 jsin.sin_family = AF_INET;
1283 chead = &pcbinfo->wildcardhashbase[
1284 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1285 LIST_FOREACH(ic, chead, ic_list) {
1287 jsin.sin_addr.s_addr = laddr.s_addr;
1289 if (!(inp->inp_vflag & INP_IPV4))
1292 if (inp->inp_socket != NULL)
1293 cred = inp->inp_socket->so_cred;
1296 if (cred != NULL && jailed(cred)) {
1300 if (!jailed_ip(cred->cr_prison,
1301 (struct sockaddr *)&jsin))
1304 if (inp->inp_lport == lport) {
1305 if (ifp && ifp->if_type == IFT_FAITH &&
1306 !(inp->inp_flags & INP_FAITH))
1308 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1309 if (cred != NULL && jailed(cred))
1314 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1316 if (INP_CHECK_SOCKAF(inp->inp_socket,
1318 local_wild_mapped = inp;
1329 if (local_wild != NULL)
1330 return (local_wild);
1332 if (local_wild_mapped != NULL)
1333 return (local_wild_mapped);
1347 * Insert PCB into connection hash table.
1350 in_pcbinsconnhash(struct inpcb *inp)
1352 struct inpcbinfo *pcbinfo = inp->inp_cpcbinfo;
1353 struct inpcbhead *bucket;
1354 u_int32_t hashkey_faddr, hashkey_laddr;
1357 if (inp->inp_vflag & INP_IPV6) {
1358 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1359 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1362 hashkey_faddr = inp->inp_faddr.s_addr;
1363 hashkey_laddr = inp->inp_laddr.s_addr;
1368 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1369 ("already on wildcardhash"));
1370 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1371 ("already on connhash"));
1372 inp->inp_flags |= INP_CONNECTED;
1375 * Insert into the connection hash table.
1377 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1378 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1379 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1383 * Remove PCB from connection hash table.
1386 in_pcbremconnhash(struct inpcb *inp)
1388 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1389 LIST_REMOVE(inp, inp_hash);
1390 inp->inp_flags &= ~INP_CONNECTED;
1394 * Insert PCB into port hash table.
1397 in_pcbinsporthash(struct inpcb *inp)
1399 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1400 struct inpcbporthead *pcbporthash;
1401 struct inpcbport *phd;
1404 * If the porthashbase is shared across several cpus we need
1407 if (pcbinfo->porttoken)
1408 lwkt_gettoken(pcbinfo->porttoken);
1411 * Insert into the port hash table.
1413 pcbporthash = &pcbinfo->porthashbase[
1414 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->porthashmask)];
1416 /* Go through port list and look for a head for this lport. */
1417 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1418 if (phd->phd_port == inp->inp_lport)
1422 /* If none exists, malloc one and tack it on. */
1424 KKASSERT(pcbinfo->portsave != NULL);
1425 phd = pcbinfo->portsave;
1426 pcbinfo->portsave = NULL;
1427 phd->phd_port = inp->inp_lport;
1428 LIST_INIT(&phd->phd_pcblist);
1429 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1433 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1435 if (pcbinfo->porttoken)
1436 lwkt_reltoken(pcbinfo->porttoken);
1437 if (pcbinfo->portsave == NULL) {
1438 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1439 M_PCB, M_INTWAIT | M_ZERO);
1445 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1447 struct inpcontainer *ic;
1448 struct inpcontainerhead *bucket;
1450 bucket = &pcbinfo->wildcardhashbase[
1451 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1453 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1455 LIST_INSERT_HEAD(bucket, ic, ic_list);
1459 * Insert PCB into wildcard hash table.
1462 in_pcbinswildcardhash(struct inpcb *inp)
1464 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1466 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1467 ("already on connhash"));
1468 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1469 ("already on wildcardhash"));
1470 inp->inp_flags |= INP_WILDCARD;
1472 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1476 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1478 struct inpcontainer *ic;
1479 struct inpcontainerhead *head;
1482 head = &pcbinfo->wildcardhashbase[
1483 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1485 LIST_FOREACH(ic, head, ic_list) {
1486 if (ic->ic_inp == inp)
1489 return; /* not found! */
1492 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
1493 kfree(ic, M_TEMP); /* deallocate container */
1497 * Remove PCB from wildcard hash table.
1500 in_pcbremwildcardhash(struct inpcb *inp)
1502 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1504 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1505 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
1506 inp->inp_flags &= ~INP_WILDCARD;
1510 * Remove PCB from various lists.
1513 in_pcbremlists(struct inpcb *inp)
1515 struct inpcbinfo *pcbinfo;
1517 if (inp->inp_lport) {
1518 struct inpcbport *phd;
1520 pcbinfo = inp->inp_pcbinfo;
1521 if (pcbinfo->porttoken)
1522 lwkt_gettoken(pcbinfo->porttoken);
1525 LIST_REMOVE(inp, inp_portlist);
1526 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1527 LIST_REMOVE(phd, phd_hash);
1530 if (pcbinfo->porttoken)
1531 lwkt_reltoken(pcbinfo->porttoken);
1533 if (inp->inp_flags & INP_WILDCARD) {
1534 in_pcbremwildcardhash(inp);
1535 } else if (inp->inp_flags & INP_CONNECTED) {
1536 in_pcbremconnhash(inp);
1538 LIST_REMOVE(inp, inp_list);
1539 inp->inp_pcbinfo->ipi_count--;
1543 prison_xinpcb(struct thread *td, struct inpcb *inp)
1547 if (td->td_proc == NULL)
1549 cr = td->td_proc->p_ucred;
1550 if (cr->cr_prison == NULL)
1552 if (inp->inp_socket && inp->inp_socket->so_cred &&
1553 inp->inp_socket->so_cred->cr_prison &&
1554 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
1560 in_pcblist_global(SYSCTL_HANDLER_ARGS)
1562 struct inpcbinfo *pcbinfo = arg1;
1563 struct inpcb *inp, *marker;
1568 * The process of preparing the TCB list is too time-consuming and
1569 * resource-intensive to repeat twice on every request.
1571 if (req->oldptr == NULL) {
1572 n = pcbinfo->ipi_count;
1573 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1577 if (req->newptr != NULL)
1581 * OK, now we're committed to doing something. Re-fetch ipi_count
1582 * after obtaining the generation count.
1584 n = pcbinfo->ipi_count;
1586 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
1587 marker->inp_flags |= INP_PLACEMARKER;
1588 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1593 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
1594 LIST_REMOVE(marker, inp_list);
1595 LIST_INSERT_AFTER(inp, marker, inp_list);
1597 if (inp->inp_flags & INP_PLACEMARKER)
1599 if (prison_xinpcb(req->td, inp))
1601 bzero(&xi, sizeof xi);
1602 xi.xi_len = sizeof xi;
1603 bcopy(inp, &xi.xi_inp, sizeof *inp);
1604 if (inp->inp_socket)
1605 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1606 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
1610 LIST_REMOVE(marker, inp_list);
1611 if (error == 0 && i < n) {
1612 bzero(&xi, sizeof xi);
1613 xi.xi_len = sizeof xi;
1615 error = SYSCTL_OUT(req, &xi, sizeof xi);
1619 kfree(marker, M_TEMP);
1624 in_pcblist_global_nomarker(SYSCTL_HANDLER_ARGS, struct xinpcb **xi0, int *nxi0)
1626 struct inpcbinfo *pcbinfo = arg1;
1635 * The process of preparing the PCB list is too time-consuming and
1636 * resource-intensive to repeat twice on every request.
1638 if (req->oldptr == NULL) {
1639 int n = pcbinfo->ipi_count;
1641 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1645 if (req->newptr != NULL)
1648 if (pcbinfo->ipi_count == 0)
1652 xi = kmalloc(pcbinfo->ipi_count * sizeof(*xi), M_TEMP,
1653 M_WAITOK | M_ZERO | M_NULLOK);
1657 LIST_FOREACH(inp, &pcbinfo->pcblisthead, inp_list) {
1658 struct xinpcb *xi_ptr = &xi[nxi];
1660 if (prison_xinpcb(req->td, inp))
1663 xi_ptr->xi_len = sizeof(*xi_ptr);
1664 bcopy(inp, &xi_ptr->xi_inp, sizeof(*inp));
1665 if (inp->inp_socket)
1666 sotoxsocket(inp->inp_socket, &xi_ptr->xi_socket);
1682 in_savefaddr(struct socket *so, const struct sockaddr *faddr)
1684 struct sockaddr_in *sin;
1686 KASSERT(faddr->sa_family == AF_INET,
1687 ("not AF_INET faddr %d", faddr->sa_family));
1689 sin = kmalloc(sizeof(*sin), M_SONAME, M_WAITOK | M_ZERO);
1690 sin->sin_family = AF_INET;
1691 sin->sin_len = sizeof(*sin);
1692 sin->sin_port = ((const struct sockaddr_in *)faddr)->sin_port;
1693 sin->sin_addr = ((const struct sockaddr_in *)faddr)->sin_addr;
1695 so->so_faddr = (struct sockaddr *)sin;