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. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
67 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.27 2004/01/02 04:06:42 ambrisko Exp $
68 * $DragonFly: src/sys/netinet/in_pcb.c,v 1.48 2008/11/08 03:38:23 sephe Exp $
71 #include "opt_ipsec.h"
72 #include "opt_inet6.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/malloc.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
85 #include <sys/kernel.h>
86 #include <sys/sysctl.h>
88 #include <sys/thread2.h>
89 #include <sys/socketvar2.h>
90 #include <sys/msgport2.h>
92 #include <machine/limits.h>
95 #include <net/if_types.h>
96 #include <net/route.h>
98 #include <netinet/in.h>
99 #include <netinet/in_pcb.h>
100 #include <netinet/in_var.h>
101 #include <netinet/ip_var.h>
103 #include <netinet/ip6.h>
104 #include <netinet6/ip6_var.h>
108 #include <netinet6/ipsec.h>
109 #include <netproto/key/key.h>
110 #include <netproto/ipsec/esp_var.h>
114 #if defined(IPSEC) || defined(IPSEC_ESP)
115 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
118 #include <netproto/ipsec/ipsec.h>
119 #include <netproto/ipsec/key.h>
121 #endif /* FAST_IPSEC */
123 struct in_addr zeroin_addr;
126 * These configure the range of local port addresses assigned to
127 * "unspecified" outgoing connections/packets/whatever.
129 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
130 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
132 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
133 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
135 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
136 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
138 #define RANGECHK(var, min, max) \
139 if ((var) < (min)) { (var) = (min); } \
140 else if ((var) > (max)) { (var) = (max); }
142 int udpencap_enable = 1; /* enabled by default */
143 int udpencap_port = 4500; /* triggers decapsulation */
146 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
150 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
152 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
153 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
155 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
156 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
158 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
159 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
166 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
168 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
169 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
170 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
171 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
172 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
173 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
174 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
175 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
176 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
177 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
178 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
179 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
182 * in_pcb.c: manage the Protocol Control Blocks.
184 * NOTE: It is assumed that most of these functions will be called from
185 * a critical section. XXX - There are, unfortunately, a few exceptions
186 * to this rule that should be fixed.
188 * NOTE: The caller should initialize the cpu field to the cpu running the
189 * protocol stack associated with this inpcbinfo.
193 in_pcbinfo_init(struct inpcbinfo *pcbinfo)
195 LIST_INIT(&pcbinfo->pcblisthead);
197 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave), M_PCB,
201 struct baddynamicports baddynamicports;
204 * Check if the specified port is invalid for dynamic allocation.
207 in_baddynamic(u_int16_t port, u_int16_t proto)
211 return (DP_ISSET(baddynamicports.tcp, port));
214 /* Cannot preset this as it is a sysctl */
215 if (port == udpencap_port)
218 return (DP_ISSET(baddynamicports.udp, port));
226 * Allocate a PCB and associate it with the socket.
229 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
236 inp = kmalloc(pcbinfo->ipi_size, M_PCB, M_WAITOK|M_ZERO);
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 KKASSERT(inp->inp_pcbinfo == pcbinfo);
270 LIST_REMOVE(inp, inp_list);
271 pcbinfo->ipi_count--;
272 inp->inp_pcbinfo = NULL;
276 * Relink a pcb into a new pcbinfo.
279 in_pcblink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
281 KKASSERT(inp->inp_pcbinfo == NULL);
282 inp->inp_pcbinfo = pcbinfo;
283 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
284 pcbinfo->ipi_count++;
288 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
290 struct socket *so = inp->inp_socket;
291 struct proc *p = td->td_proc;
292 unsigned short *lastport;
293 struct sockaddr_in *sin;
294 struct sockaddr_in jsin;
295 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
296 struct ucred *cred = NULL;
298 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
303 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
304 return (EADDRNOTAVAIL);
305 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
306 return (EINVAL); /* already bound */
308 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
309 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
314 * This has to be atomic. If the porthash is shared across multiple
315 * protocol threads (aka tcp) then the token will be non-NULL.
317 if (pcbinfo->porttoken)
318 lwkt_gettoken(pcbinfo->porttoken);
321 sin = (struct sockaddr_in *)nam;
322 if (nam->sa_len != sizeof *sin) {
328 * We should check the family, but old programs
329 * incorrectly fail to initialize it.
331 if (sin->sin_family != AF_INET) {
332 error = EAFNOSUPPORT;
336 if (!prison_replace_wildcards(td, nam)) {
340 lport = sin->sin_port;
341 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
343 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
344 * allow complete duplication of binding if
345 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
346 * and a multicast address is bound on both
347 * new and duplicated sockets.
349 if (so->so_options & SO_REUSEADDR)
350 reuseport = SO_REUSEADDR | SO_REUSEPORT;
351 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
352 sin->sin_port = 0; /* yech... */
353 bzero(&sin->sin_zero, sizeof sin->sin_zero);
354 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL) {
355 error = EADDRNOTAVAIL;
363 if (ntohs(lport) < IPPORT_RESERVED &&
365 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0)) {
369 if (so->so_cred->cr_uid != 0 &&
370 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
371 t = in_pcblookup_local(pcbinfo,
377 (!in_nullhost(sin->sin_addr) ||
378 !in_nullhost(t->inp_laddr) ||
379 (t->inp_socket->so_options &
380 SO_REUSEPORT) == 0) &&
381 (so->so_cred->cr_uid !=
382 t->inp_socket->so_cred->cr_uid)) {
384 if (!in_nullhost(sin->sin_addr) ||
385 !in_nullhost(t->inp_laddr) ||
387 INP_SOCKAF(t->inp_socket))
395 if (cred && !prison_replace_wildcards(td, nam)) {
396 error = EADDRNOTAVAIL;
399 t = in_pcblookup_local(pcbinfo, sin->sin_addr, lport,
401 if (t && !(reuseport & t->inp_socket->so_options)) {
403 if (!in_nullhost(sin->sin_addr) ||
404 !in_nullhost(t->inp_laddr) ||
405 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
413 inp->inp_laddr = sin->sin_addr;
419 jsin.sin_family = AF_INET;
420 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
421 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
422 inp->inp_laddr.s_addr = INADDR_ANY;
426 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
428 inp->inp_flags |= INP_ANONPORT;
430 if (inp->inp_flags & INP_HIGHPORT) {
431 first = ipport_hifirstauto; /* sysctl */
432 last = ipport_hilastauto;
433 lastport = &pcbinfo->lasthi;
434 } else if (inp->inp_flags & INP_LOWPORT) {
436 (error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
437 inp->inp_laddr.s_addr = INADDR_ANY;
440 first = ipport_lowfirstauto; /* 1023 */
441 last = ipport_lowlastauto; /* 600 */
442 lastport = &pcbinfo->lastlow;
444 first = ipport_firstauto; /* sysctl */
445 last = ipport_lastauto;
446 lastport = &pcbinfo->lastport;
449 * Simple check to ensure all ports are not used up causing
452 * We split the two cases (up and down) so that the direction
453 * is not being tested on each round of the loop.
459 count = first - last;
462 if (count-- < 0) { /* completely used? */
463 inp->inp_laddr.s_addr = INADDR_ANY;
464 error = EADDRNOTAVAIL;
468 if (*lastport > first || *lastport < last)
470 lport = htons(*lastport);
471 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
477 count = last - first;
480 if (count-- < 0) { /* completely used? */
481 inp->inp_laddr.s_addr = INADDR_ANY;
482 error = EADDRNOTAVAIL;
486 if (*lastport < first || *lastport > last)
488 lport = htons(*lastport);
489 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
493 inp->inp_lport = lport;
495 jsin.sin_family = AF_INET;
496 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
497 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin)) {
498 inp->inp_laddr.s_addr = INADDR_ANY;
503 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
505 if (in_pcbinsporthash(inp) != 0) {
506 inp->inp_laddr.s_addr = INADDR_ANY;
513 if (pcbinfo->porttoken)
514 lwkt_reltoken(pcbinfo->porttoken);
518 static struct inpcb *
519 in_pcblookup_addrport(struct inpcbinfo *pcbinfo, struct in_addr laddr,
520 u_short lport, struct in_addr faddr, u_short fport, struct ucred *cred)
523 struct inpcbporthead *porthash;
524 struct inpcbport *phd;
525 struct inpcb *match = NULL;
528 * If the porthashbase is shared across several cpus we need
531 if (pcbinfo->porttoken)
532 lwkt_gettoken(pcbinfo->porttoken);
535 * Best fit PCB lookup.
537 * First see if this local port is in use by looking on the
540 porthash = &pcbinfo->porthashbase[
541 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
542 LIST_FOREACH(phd, porthash, phd_hash) {
543 if (phd->phd_port == lport)
547 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
549 if ((inp->inp_vflag & INP_IPV4) == 0)
552 if (inp->inp_laddr.s_addr != INADDR_ANY &&
553 inp->inp_laddr.s_addr != laddr.s_addr)
556 if (inp->inp_faddr.s_addr != INADDR_ANY &&
557 inp->inp_faddr.s_addr != faddr.s_addr)
560 if (inp->inp_fport != 0 && inp->inp_fport != fport)
565 inp->inp_socket->so_cred->cr_prison) {
571 if (pcbinfo->porttoken)
572 lwkt_reltoken(pcbinfo->porttoken);
577 in_pcbconn_bind(struct inpcb *inp, const struct sockaddr *nam,
580 struct proc *p = td->td_proc;
581 unsigned short *lastport;
582 const struct sockaddr_in *sin = (const struct sockaddr_in *)nam;
583 struct sockaddr_in jsin;
584 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
585 struct ucred *cred = NULL;
590 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
591 return (EADDRNOTAVAIL);
593 KKASSERT(inp->inp_laddr.s_addr != INADDR_ANY);
594 if (inp->inp_lport != 0)
595 return (EINVAL); /* already bound */
601 * This has to be atomic. If the porthash is shared across multiple
602 * protocol threads (aka tcp) then the token will be non-NULL.
604 if (pcbinfo->porttoken)
605 lwkt_gettoken(pcbinfo->porttoken);
607 jsin.sin_family = AF_INET;
608 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
609 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
610 inp->inp_laddr.s_addr = INADDR_ANY;
614 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
616 inp->inp_flags |= INP_ANONPORT;
618 if (inp->inp_flags & INP_HIGHPORT) {
619 first = ipport_hifirstauto; /* sysctl */
620 last = ipport_hilastauto;
621 lastport = &pcbinfo->lasthi;
622 } else if (inp->inp_flags & INP_LOWPORT) {
624 (error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
625 inp->inp_laddr.s_addr = INADDR_ANY;
628 first = ipport_lowfirstauto; /* 1023 */
629 last = ipport_lowlastauto; /* 600 */
630 lastport = &pcbinfo->lastlow;
632 first = ipport_firstauto; /* sysctl */
633 last = ipport_lastauto;
634 lastport = &pcbinfo->lastport;
637 * Simple check to ensure all ports are not used up causing
640 * We split the two cases (up and down) so that the direction
641 * is not being tested on each round of the loop.
647 count = first - last;
650 if (count-- < 0) { /* completely used? */
651 inp->inp_laddr.s_addr = INADDR_ANY;
652 error = EADDRNOTAVAIL;
656 if (*lastport > first || *lastport < last)
658 lport = htons(*lastport);
659 } while (in_pcblookup_addrport(pcbinfo, inp->inp_laddr, lport,
660 sin->sin_addr, sin->sin_port, cred));
665 count = last - first;
668 if (count-- < 0) { /* completely used? */
669 inp->inp_laddr.s_addr = INADDR_ANY;
670 error = EADDRNOTAVAIL;
674 if (*lastport < first || *lastport > last)
676 lport = htons(*lastport);
677 } while (in_pcblookup_addrport(pcbinfo, inp->inp_laddr, lport,
678 sin->sin_addr, sin->sin_port, cred));
680 inp->inp_lport = lport;
682 jsin.sin_family = AF_INET;
683 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
684 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin)) {
685 inp->inp_laddr.s_addr = INADDR_ANY;
690 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
692 if (in_pcbinsporthash(inp) != 0) {
693 inp->inp_laddr.s_addr = INADDR_ANY;
700 if (pcbinfo->porttoken)
701 lwkt_reltoken(pcbinfo->porttoken);
706 * Transform old in_pcbconnect() into an inner subroutine for new
707 * in_pcbconnect(): Do some validity-checking on the remote
708 * address (in mbuf 'nam') and then determine local host address
709 * (i.e., which interface) to use to access that remote host.
711 * This preserves definition of in_pcbconnect(), while supporting a
712 * slightly different version for T/TCP. (This is more than
713 * a bit of a kludge, but cleaning up the internal interfaces would
714 * have forced minor changes in every protocol).
717 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
718 struct sockaddr_in **plocal_sin, struct thread *td)
720 struct in_ifaddr *ia;
721 struct ucred *cred = NULL;
722 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
723 struct sockaddr *jsin;
724 int jailed = 0, alloc_route = 0;
726 if (nam->sa_len != sizeof *sin)
728 if (sin->sin_family != AF_INET)
729 return (EAFNOSUPPORT);
730 if (sin->sin_port == 0)
731 return (EADDRNOTAVAIL);
732 if (td && td->td_proc && td->td_proc->p_ucred)
733 cred = td->td_proc->p_ucred;
734 if (cred && cred->cr_prison)
736 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
737 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
739 * If the destination address is INADDR_ANY,
740 * use the primary local address.
741 * If the supplied address is INADDR_BROADCAST,
742 * and the primary interface supports broadcast,
743 * choose the broadcast address for that interface.
745 if (sin->sin_addr.s_addr == INADDR_ANY)
746 sin->sin_addr = IA_SIN(ia)->sin_addr;
747 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
748 (ia->ia_ifp->if_flags & IFF_BROADCAST))
749 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
751 if (inp->inp_laddr.s_addr == INADDR_ANY) {
756 * If route is known or can be allocated now,
757 * our src addr is taken from the i/f, else punt.
758 * Note that we should check the address family of the cached
759 * destination, in case of sharing the cache with IPv6.
761 ro = &inp->inp_route;
763 (!(ro->ro_rt->rt_flags & RTF_UP) ||
764 ro->ro_dst.sa_family != AF_INET ||
765 satosin(&ro->ro_dst)->sin_addr.s_addr !=
766 sin->sin_addr.s_addr ||
767 inp->inp_socket->so_options & SO_DONTROUTE)) {
771 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
772 (ro->ro_rt == NULL ||
773 ro->ro_rt->rt_ifp == NULL)) {
774 /* No route yet, so try to acquire one */
775 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
776 ro->ro_dst.sa_family = AF_INET;
777 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
778 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
784 * If we found a route, use the address
785 * corresponding to the outgoing interface
786 * unless it is the loopback (in case a route
787 * to our address on another net goes to loopback).
789 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
791 if (jailed_ip(cred->cr_prison,
792 ro->ro_rt->rt_ifa->ifa_addr)) {
793 ia = ifatoia(ro->ro_rt->rt_ifa);
796 ia = ifatoia(ro->ro_rt->rt_ifa);
800 u_short fport = sin->sin_port;
803 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
804 if (ia && jailed && !jailed_ip(cred->cr_prison,
805 sintosa(&ia->ia_addr)))
808 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
809 if (ia && jailed && !jailed_ip(cred->cr_prison,
810 sintosa(&ia->ia_addr)))
812 sin->sin_port = fport;
814 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
815 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
816 if (ia && jailed && !jailed_ip(cred->cr_prison,
817 sintosa(&ia->ia_addr)))
820 if (!jailed && ia == NULL)
824 * If the destination address is multicast and an outgoing
825 * interface has been set as a multicast option, use the
826 * address of that interface as our source address.
828 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
829 inp->inp_moptions != NULL) {
830 struct ip_moptions *imo;
833 imo = inp->inp_moptions;
834 if (imo->imo_multicast_ifp != NULL) {
835 struct in_ifaddr_container *iac;
837 ifp = imo->imo_multicast_ifp;
840 &in_ifaddrheads[mycpuid], ia_link) {
841 if (iac->ia->ia_ifp == ifp) {
851 * Don't do pcblookup call here; return interface in plocal_sin
852 * and exit to caller, that will do the lookup.
854 if (ia == NULL && jailed) {
855 if ((jsin = prison_get_nonlocal(cred->cr_prison, AF_INET, NULL)) != NULL ||
856 (jsin = prison_get_local(cred->cr_prison, AF_INET, NULL)) != NULL) {
857 *plocal_sin = satosin(jsin);
863 *plocal_sin = &ia->ia_addr;
869 struct route *ro = &inp->inp_route;
871 if (ro->ro_rt != NULL)
873 bzero(ro, sizeof(*ro));
875 return (EADDRNOTAVAIL);
880 * Connect from a socket to a specified address.
881 * Both address and port must be specified in argument sin.
882 * If don't have a local address for this socket yet,
886 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
888 struct sockaddr_in *if_sin;
889 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
892 /* Call inner routine to assign local interface address. */
893 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
896 if (in_pcblookup_hash(inp->inp_cpcbinfo, sin->sin_addr, sin->sin_port,
897 inp->inp_laddr.s_addr ?
898 inp->inp_laddr : if_sin->sin_addr,
899 inp->inp_lport, FALSE, NULL) != NULL) {
902 if (inp->inp_laddr.s_addr == INADDR_ANY) {
903 if (inp->inp_lport == 0) {
904 error = in_pcbbind(inp, NULL, td);
908 inp->inp_laddr = if_sin->sin_addr;
910 inp->inp_faddr = sin->sin_addr;
911 inp->inp_fport = sin->sin_port;
912 in_pcbinsconnhash(inp);
917 in_pcbdisconnect(struct inpcb *inp)
920 inp->inp_faddr.s_addr = INADDR_ANY;
922 in_pcbremconnhash(inp);
923 if (inp->inp_socket->so_state & SS_NOFDREF)
928 in_pcbdetach(struct inpcb *inp)
930 struct socket *so = inp->inp_socket;
931 struct inpcbinfo *ipi = inp->inp_pcbinfo;
934 ipsec4_delete_pcbpolicy(inp);
936 inp->inp_gencnt = ++ipi->ipi_gencnt;
937 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
940 sofree(so); /* remove pcb ref */
941 if (inp->inp_options)
942 m_free(inp->inp_options);
943 if (inp->inp_route.ro_rt)
944 rtfree(inp->inp_route.ro_rt);
945 ip_freemoptions(inp->inp_moptions);
951 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
952 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
953 * in struct pr_usrreqs, so that protocols can just reference then directly
954 * without the need for a wrapper function. The socket must have a valid
955 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
956 * except through a kernel programming error, so it is acceptable to panic
957 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
958 * because there actually /is/ a programming error somewhere... XXX)
961 in_setsockaddr(struct socket *so, struct sockaddr **nam)
964 struct sockaddr_in *sin;
967 * Do the malloc first in case it blocks.
969 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
971 sin->sin_family = AF_INET;
972 sin->sin_len = sizeof *sin;
978 kfree(sin, M_SONAME);
981 sin->sin_port = inp->inp_lport;
982 sin->sin_addr = inp->inp_laddr;
985 *nam = (struct sockaddr *)sin;
990 in_setsockaddr_dispatch(netmsg_t msg)
994 error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
995 lwkt_replymsg(&msg->lmsg, error);
999 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
1002 struct sockaddr_in *sin;
1005 * Do the malloc first in case it blocks.
1007 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
1009 sin->sin_family = AF_INET;
1010 sin->sin_len = sizeof *sin;
1016 kfree(sin, M_SONAME);
1017 return (ECONNRESET);
1019 sin->sin_port = inp->inp_fport;
1020 sin->sin_addr = inp->inp_faddr;
1023 *nam = (struct sockaddr *)sin;
1028 in_setpeeraddr_dispatch(netmsg_t msg)
1032 error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1033 lwkt_replymsg(&msg->lmsg, error);
1037 in_pcbnotifyall(struct inpcbhead *head, struct in_addr faddr, int err,
1038 void (*notify)(struct inpcb *, int))
1040 struct inpcb *inp, *ninp;
1043 * note: if INP_PLACEMARKER is set we must ignore the rest of
1044 * the structure and skip it.
1047 LIST_FOREACH_MUTABLE(inp, head, inp_list, ninp) {
1048 if (inp->inp_flags & INP_PLACEMARKER)
1051 if (!(inp->inp_vflag & INP_IPV4))
1054 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1055 inp->inp_socket == NULL)
1057 (*notify)(inp, err); /* can remove inp from list! */
1063 in_pcbpurgeif0(struct inpcb *head, struct ifnet *ifp)
1066 struct ip_moptions *imo;
1069 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
1070 if (inp->inp_flags & INP_PLACEMARKER)
1072 imo = inp->inp_moptions;
1073 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
1075 * Unselect the outgoing interface if it is being
1078 if (imo->imo_multicast_ifp == ifp)
1079 imo->imo_multicast_ifp = NULL;
1082 * Drop multicast group membership if we joined
1083 * through the interface being detached.
1085 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1087 if (imo->imo_membership[i]->inm_ifp == ifp) {
1088 in_delmulti(imo->imo_membership[i]);
1090 } else if (gap != 0)
1091 imo->imo_membership[i - gap] =
1092 imo->imo_membership[i];
1094 imo->imo_num_memberships -= gap;
1100 * Check for alternatives when higher level complains
1101 * about service problems. For now, invalidate cached
1102 * routing information. If the route was created dynamically
1103 * (by a redirect), time to try a default gateway again.
1106 in_losing(struct inpcb *inp)
1109 struct rt_addrinfo rtinfo;
1111 if ((rt = inp->inp_route.ro_rt)) {
1112 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1113 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1114 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1115 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1116 rtinfo.rti_flags = rt->rt_flags;
1117 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1118 if (rt->rt_flags & RTF_DYNAMIC)
1119 rtrequest1_global(RTM_DELETE, &rtinfo, NULL, NULL);
1120 inp->inp_route.ro_rt = NULL;
1123 * A new route can be allocated
1124 * the next time output is attempted.
1130 * After a routing change, flush old routing
1131 * and allocate a (hopefully) better one.
1134 in_rtchange(struct inpcb *inp, int err)
1136 if (inp->inp_route.ro_rt) {
1137 rtfree(inp->inp_route.ro_rt);
1138 inp->inp_route.ro_rt = NULL;
1140 * A new route can be allocated the next time
1141 * output is attempted.
1147 * Lookup a PCB based on the local address and port.
1150 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1151 u_int lport_arg, int wild_okay, struct ucred *cred)
1154 int matchwild = 3, wildcard;
1155 u_short lport = lport_arg;
1156 struct inpcbporthead *porthash;
1157 struct inpcbport *phd;
1158 struct inpcb *match = NULL;
1161 * If the porthashbase is shared across several cpus we need
1164 if (pcbinfo->porttoken)
1165 lwkt_gettoken(pcbinfo->porttoken);
1168 * Best fit PCB lookup.
1170 * First see if this local port is in use by looking on the
1173 porthash = &pcbinfo->porthashbase[
1174 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
1175 LIST_FOREACH(phd, porthash, phd_hash) {
1176 if (phd->phd_port == lport)
1181 * Port is in use by one or more PCBs. Look for best
1184 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1187 if ((inp->inp_vflag & INP_IPV4) == 0)
1190 if (inp->inp_faddr.s_addr != INADDR_ANY)
1192 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1193 if (laddr.s_addr == INADDR_ANY)
1195 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1198 if (laddr.s_addr != INADDR_ANY)
1201 if (wildcard && !wild_okay)
1203 if (wildcard < matchwild &&
1206 inp->inp_socket->so_cred->cr_prison)) {
1208 matchwild = wildcard;
1209 if (matchwild == 0) {
1215 if (pcbinfo->porttoken)
1216 lwkt_reltoken(pcbinfo->porttoken);
1221 * Lookup PCB in hash list.
1224 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1225 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1226 boolean_t wildcard, struct ifnet *ifp)
1228 struct inpcbhead *head;
1229 struct inpcb *inp, *jinp=NULL;
1230 u_short fport = fport_arg, lport = lport_arg;
1233 * First look for an exact match.
1235 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1236 laddr.s_addr, lport, pcbinfo->hashmask)];
1237 LIST_FOREACH(inp, head, inp_hash) {
1239 if (!(inp->inp_vflag & INP_IPV4))
1242 if (in_hosteq(inp->inp_faddr, faddr) &&
1243 in_hosteq(inp->inp_laddr, laddr) &&
1244 inp->inp_fport == fport && inp->inp_lport == lport) {
1246 if (inp->inp_socket == NULL ||
1247 inp->inp_socket->so_cred->cr_prison == NULL) {
1258 struct inpcb *local_wild = NULL;
1259 struct inpcb *jinp_wild = NULL;
1261 struct inpcb *local_wild_mapped = NULL;
1263 struct inpcontainer *ic;
1264 struct inpcontainerhead *chead;
1265 struct sockaddr_in jsin;
1269 * Order of socket selection:
1270 * 1. non-jailed, non-wild.
1271 * 2. non-jailed, wild.
1272 * 3. jailed, non-wild.
1275 jsin.sin_family = AF_INET;
1276 chead = &pcbinfo->wildcardhashbase[
1277 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1278 LIST_FOREACH(ic, chead, ic_list) {
1280 jsin.sin_addr.s_addr = laddr.s_addr;
1282 if (!(inp->inp_vflag & INP_IPV4))
1285 if (inp->inp_socket != NULL)
1286 cred = inp->inp_socket->so_cred;
1289 if (cred != NULL && jailed(cred)) {
1293 if (!jailed_ip(cred->cr_prison,
1294 (struct sockaddr *)&jsin))
1297 if (inp->inp_lport == lport) {
1298 if (ifp && ifp->if_type == IFT_FAITH &&
1299 !(inp->inp_flags & INP_FAITH))
1301 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1302 if (cred != NULL && jailed(cred))
1307 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1309 if (INP_CHECK_SOCKAF(inp->inp_socket,
1311 local_wild_mapped = inp;
1322 if (local_wild != NULL)
1323 return (local_wild);
1325 if (local_wild_mapped != NULL)
1326 return (local_wild_mapped);
1340 * Insert PCB into connection hash table.
1343 in_pcbinsconnhash(struct inpcb *inp)
1345 struct inpcbinfo *pcbinfo = inp->inp_cpcbinfo;
1346 struct inpcbhead *bucket;
1347 u_int32_t hashkey_faddr, hashkey_laddr;
1350 if (inp->inp_vflag & INP_IPV6) {
1351 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1352 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1355 hashkey_faddr = inp->inp_faddr.s_addr;
1356 hashkey_laddr = inp->inp_laddr.s_addr;
1361 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1362 ("already on wildcardhash\n"));
1363 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1364 ("already on connhash\n"));
1365 inp->inp_flags |= INP_CONNECTED;
1368 * Insert into the connection hash table.
1370 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1371 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1372 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1376 * Remove PCB from connection hash table.
1379 in_pcbremconnhash(struct inpcb *inp)
1381 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1382 LIST_REMOVE(inp, inp_hash);
1383 inp->inp_flags &= ~INP_CONNECTED;
1387 * Insert PCB into port hash table.
1390 in_pcbinsporthash(struct inpcb *inp)
1392 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1393 struct inpcbporthead *pcbporthash;
1394 struct inpcbport *phd;
1397 * If the porthashbase is shared across several cpus we need
1400 if (pcbinfo->porttoken)
1401 lwkt_gettoken(pcbinfo->porttoken);
1404 * Insert into the port hash table.
1406 pcbporthash = &pcbinfo->porthashbase[
1407 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->porthashmask)];
1409 /* Go through port list and look for a head for this lport. */
1410 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1411 if (phd->phd_port == inp->inp_lport)
1415 /* If none exists, malloc one and tack it on. */
1417 KKASSERT(pcbinfo->portsave != NULL);
1418 phd = pcbinfo->portsave;
1419 pcbinfo->portsave = NULL;
1420 phd->phd_port = inp->inp_lport;
1421 LIST_INIT(&phd->phd_pcblist);
1422 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1426 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1428 if (pcbinfo->porttoken)
1429 lwkt_reltoken(pcbinfo->porttoken);
1430 if (pcbinfo->portsave == NULL) {
1431 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1432 M_PCB, M_INTWAIT | M_ZERO);
1438 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1440 struct inpcontainer *ic;
1441 struct inpcontainerhead *bucket;
1443 bucket = &pcbinfo->wildcardhashbase[
1444 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1446 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1448 LIST_INSERT_HEAD(bucket, ic, ic_list);
1452 * Insert PCB into wildcard hash table.
1455 in_pcbinswildcardhash(struct inpcb *inp)
1457 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1459 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1460 ("already on connhash\n"));
1461 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1462 ("already on wildcardhash\n"));
1463 inp->inp_flags |= INP_WILDCARD;
1465 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1469 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1471 struct inpcontainer *ic;
1472 struct inpcontainerhead *head;
1475 head = &pcbinfo->wildcardhashbase[
1476 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1478 LIST_FOREACH(ic, head, ic_list) {
1479 if (ic->ic_inp == inp)
1482 return; /* not found! */
1485 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
1486 kfree(ic, M_TEMP); /* deallocate container */
1490 * Remove PCB from wildcard hash table.
1493 in_pcbremwildcardhash(struct inpcb *inp)
1495 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1497 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1498 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
1499 inp->inp_flags &= ~INP_WILDCARD;
1503 * Remove PCB from various lists.
1506 in_pcbremlists(struct inpcb *inp)
1508 struct inpcbinfo *pcbinfo;
1510 if (inp->inp_lport) {
1511 struct inpcbport *phd;
1513 pcbinfo = inp->inp_pcbinfo;
1514 if (pcbinfo->porttoken)
1515 lwkt_gettoken(pcbinfo->porttoken);
1518 LIST_REMOVE(inp, inp_portlist);
1519 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1520 LIST_REMOVE(phd, phd_hash);
1523 if (pcbinfo->porttoken)
1524 lwkt_reltoken(pcbinfo->porttoken);
1526 if (inp->inp_flags & INP_WILDCARD) {
1527 in_pcbremwildcardhash(inp);
1528 } else if (inp->inp_flags & INP_CONNECTED) {
1529 in_pcbremconnhash(inp);
1531 LIST_REMOVE(inp, inp_list);
1532 inp->inp_pcbinfo->ipi_count--;
1536 prison_xinpcb(struct thread *td, struct inpcb *inp)
1540 if (td->td_proc == NULL)
1542 cr = td->td_proc->p_ucred;
1543 if (cr->cr_prison == NULL)
1545 if (inp->inp_socket && inp->inp_socket->so_cred &&
1546 inp->inp_socket->so_cred->cr_prison &&
1547 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
1553 in_pcblist_global(SYSCTL_HANDLER_ARGS)
1555 struct inpcbinfo *pcbinfo = arg1;
1556 struct inpcb *inp, *marker;
1561 * The process of preparing the TCB list is too time-consuming and
1562 * resource-intensive to repeat twice on every request.
1564 if (req->oldptr == NULL) {
1565 n = pcbinfo->ipi_count;
1566 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1570 if (req->newptr != NULL)
1574 * OK, now we're committed to doing something. Re-fetch ipi_count
1575 * after obtaining the generation count.
1577 n = pcbinfo->ipi_count;
1579 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
1580 marker->inp_flags |= INP_PLACEMARKER;
1581 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1586 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
1587 LIST_REMOVE(marker, inp_list);
1588 LIST_INSERT_AFTER(inp, marker, inp_list);
1590 if (inp->inp_flags & INP_PLACEMARKER)
1592 if (prison_xinpcb(req->td, inp))
1594 bzero(&xi, sizeof xi);
1595 xi.xi_len = sizeof xi;
1596 bcopy(inp, &xi.xi_inp, sizeof *inp);
1597 if (inp->inp_socket)
1598 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1599 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
1603 LIST_REMOVE(marker, inp_list);
1604 if (error == 0 && i < n) {
1605 bzero(&xi, sizeof xi);
1606 xi.xi_len = sizeof xi;
1608 error = SYSCTL_OUT(req, &xi, sizeof xi);
1612 kfree(marker, M_TEMP);
1617 in_pcblist_global_nomarker(SYSCTL_HANDLER_ARGS, struct xinpcb **xi0, int *nxi0)
1619 struct inpcbinfo *pcbinfo = arg1;
1628 * The process of preparing the PCB list is too time-consuming and
1629 * resource-intensive to repeat twice on every request.
1631 if (req->oldptr == NULL) {
1632 int n = pcbinfo->ipi_count;
1634 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1638 if (req->newptr != NULL)
1641 if (pcbinfo->ipi_count == 0)
1645 xi = kmalloc(pcbinfo->ipi_count * sizeof(*xi), M_TEMP,
1646 M_WAITOK | M_ZERO | M_NULLOK);
1650 LIST_FOREACH(inp, &pcbinfo->pcblisthead, inp_list) {
1651 struct xinpcb *xi_ptr = &xi[nxi];
1653 if (prison_xinpcb(req->td, inp))
1656 xi_ptr->xi_len = sizeof(*xi_ptr);
1657 bcopy(inp, &xi_ptr->xi_inp, sizeof(*inp));
1658 if (inp->inp_socket)
1659 sotoxsocket(inp->inp_socket, &xi_ptr->xi_socket);