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;
588 int count, error, dup = 0;
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;
639 * Simple check to ensure all ports are not used up causing
642 * We split the two cases (up and down) so that the direction
643 * is not being tested on each round of the loop.
649 count = first - last;
652 if (count-- < 0) { /* completely used? */
653 inp->inp_laddr.s_addr = INADDR_ANY;
654 error = EADDRNOTAVAIL;
658 if (*lastport > first || *lastport < last)
660 lport = htons(*lastport);
661 } while (in_pcblookup_addrport(pcbinfo, inp->inp_laddr, lport,
662 sin->sin_addr, sin->sin_port, cred));
667 count = last - first;
670 if (count-- < 0) { /* completely used? */
671 inp->inp_laddr.s_addr = INADDR_ANY;
672 error = EADDRNOTAVAIL;
676 if (*lastport < first || *lastport > last)
678 lport = htons(*lastport);
679 } while (in_pcblookup_addrport(pcbinfo, inp->inp_laddr, lport,
680 sin->sin_addr, sin->sin_port, cred));
683 /* This could happen on loopback interface */
684 if (sin->sin_port == lport &&
685 sin->sin_addr.s_addr == inp->inp_laddr.s_addr) {
688 * Duplicate again; give up
690 inp->inp_laddr.s_addr = INADDR_ANY;
691 error = EADDRNOTAVAIL;
697 inp->inp_lport = lport;
699 jsin.sin_family = AF_INET;
700 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
701 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin)) {
702 inp->inp_laddr.s_addr = INADDR_ANY;
707 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
709 if (in_pcbinsporthash(inp) != 0) {
710 inp->inp_laddr.s_addr = INADDR_ANY;
717 if (pcbinfo->porttoken)
718 lwkt_reltoken(pcbinfo->porttoken);
723 * Transform old in_pcbconnect() into an inner subroutine for new
724 * in_pcbconnect(): Do some validity-checking on the remote
725 * address (in mbuf 'nam') and then determine local host address
726 * (i.e., which interface) to use to access that remote host.
728 * This preserves definition of in_pcbconnect(), while supporting a
729 * slightly different version for T/TCP. (This is more than
730 * a bit of a kludge, but cleaning up the internal interfaces would
731 * have forced minor changes in every protocol).
734 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
735 struct sockaddr_in **plocal_sin, struct thread *td)
737 struct in_ifaddr *ia;
738 struct ucred *cred = NULL;
739 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
740 struct sockaddr *jsin;
741 int jailed = 0, alloc_route = 0;
743 if (nam->sa_len != sizeof *sin)
745 if (sin->sin_family != AF_INET)
746 return (EAFNOSUPPORT);
747 if (sin->sin_port == 0)
748 return (EADDRNOTAVAIL);
749 if (td && td->td_proc && td->td_proc->p_ucred)
750 cred = td->td_proc->p_ucred;
751 if (cred && cred->cr_prison)
753 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
754 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
756 * If the destination address is INADDR_ANY,
757 * use the primary local address.
758 * If the supplied address is INADDR_BROADCAST,
759 * and the primary interface supports broadcast,
760 * choose the broadcast address for that interface.
762 if (sin->sin_addr.s_addr == INADDR_ANY)
763 sin->sin_addr = IA_SIN(ia)->sin_addr;
764 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
765 (ia->ia_ifp->if_flags & IFF_BROADCAST))
766 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
768 if (inp->inp_laddr.s_addr == INADDR_ANY) {
773 * If route is known or can be allocated now,
774 * our src addr is taken from the i/f, else punt.
775 * Note that we should check the address family of the cached
776 * destination, in case of sharing the cache with IPv6.
778 ro = &inp->inp_route;
780 (!(ro->ro_rt->rt_flags & RTF_UP) ||
781 ro->ro_dst.sa_family != AF_INET ||
782 satosin(&ro->ro_dst)->sin_addr.s_addr !=
783 sin->sin_addr.s_addr ||
784 inp->inp_socket->so_options & SO_DONTROUTE)) {
788 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
789 (ro->ro_rt == NULL ||
790 ro->ro_rt->rt_ifp == NULL)) {
791 /* No route yet, so try to acquire one */
792 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
793 ro->ro_dst.sa_family = AF_INET;
794 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
795 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
801 * If we found a route, use the address
802 * corresponding to the outgoing interface
803 * unless it is the loopback (in case a route
804 * to our address on another net goes to loopback).
806 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
808 if (jailed_ip(cred->cr_prison,
809 ro->ro_rt->rt_ifa->ifa_addr)) {
810 ia = ifatoia(ro->ro_rt->rt_ifa);
813 ia = ifatoia(ro->ro_rt->rt_ifa);
817 u_short fport = sin->sin_port;
820 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
821 if (ia && jailed && !jailed_ip(cred->cr_prison,
822 sintosa(&ia->ia_addr)))
825 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
826 if (ia && jailed && !jailed_ip(cred->cr_prison,
827 sintosa(&ia->ia_addr)))
829 sin->sin_port = fport;
831 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
832 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
833 if (ia && jailed && !jailed_ip(cred->cr_prison,
834 sintosa(&ia->ia_addr)))
837 if (!jailed && ia == NULL)
841 * If the destination address is multicast and an outgoing
842 * interface has been set as a multicast option, use the
843 * address of that interface as our source address.
845 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
846 inp->inp_moptions != NULL) {
847 struct ip_moptions *imo;
850 imo = inp->inp_moptions;
851 if (imo->imo_multicast_ifp != NULL) {
852 struct in_ifaddr_container *iac;
854 ifp = imo->imo_multicast_ifp;
857 &in_ifaddrheads[mycpuid], ia_link) {
858 if (iac->ia->ia_ifp == ifp) {
868 * Don't do pcblookup call here; return interface in plocal_sin
869 * and exit to caller, that will do the lookup.
871 if (ia == NULL && jailed) {
872 if ((jsin = prison_get_nonlocal(cred->cr_prison, AF_INET, NULL)) != NULL ||
873 (jsin = prison_get_local(cred->cr_prison, AF_INET, NULL)) != NULL) {
874 *plocal_sin = satosin(jsin);
880 *plocal_sin = &ia->ia_addr;
886 struct route *ro = &inp->inp_route;
888 if (ro->ro_rt != NULL)
890 bzero(ro, sizeof(*ro));
892 return (EADDRNOTAVAIL);
897 * Connect from a socket to a specified address.
898 * Both address and port must be specified in argument sin.
899 * If don't have a local address for this socket yet,
903 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
905 struct sockaddr_in *if_sin;
906 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
909 /* Call inner routine to assign local interface address. */
910 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
913 if (in_pcblookup_hash(inp->inp_cpcbinfo, sin->sin_addr, sin->sin_port,
914 inp->inp_laddr.s_addr ?
915 inp->inp_laddr : if_sin->sin_addr,
916 inp->inp_lport, FALSE, NULL) != NULL) {
919 if (inp->inp_laddr.s_addr == INADDR_ANY) {
920 if (inp->inp_lport == 0) {
921 error = in_pcbbind(inp, NULL, td);
925 inp->inp_laddr = if_sin->sin_addr;
927 inp->inp_faddr = sin->sin_addr;
928 inp->inp_fport = sin->sin_port;
929 in_pcbinsconnhash(inp);
934 in_pcbdisconnect(struct inpcb *inp)
937 inp->inp_faddr.s_addr = INADDR_ANY;
939 in_pcbremconnhash(inp);
940 if (inp->inp_socket->so_state & SS_NOFDREF)
945 in_pcbdetach(struct inpcb *inp)
947 struct socket *so = inp->inp_socket;
948 struct inpcbinfo *ipi = inp->inp_pcbinfo;
951 ipsec4_delete_pcbpolicy(inp);
953 inp->inp_gencnt = ++ipi->ipi_gencnt;
954 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
957 sofree(so); /* remove pcb ref */
958 if (inp->inp_options)
959 m_free(inp->inp_options);
960 if (inp->inp_route.ro_rt)
961 rtfree(inp->inp_route.ro_rt);
962 ip_freemoptions(inp->inp_moptions);
968 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
969 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
970 * in struct pr_usrreqs, so that protocols can just reference then directly
971 * without the need for a wrapper function. The socket must have a valid
972 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
973 * except through a kernel programming error, so it is acceptable to panic
974 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
975 * because there actually /is/ a programming error somewhere... XXX)
978 in_setsockaddr(struct socket *so, struct sockaddr **nam)
981 struct sockaddr_in *sin;
984 * Do the malloc first in case it blocks.
986 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
988 sin->sin_family = AF_INET;
989 sin->sin_len = sizeof *sin;
995 kfree(sin, M_SONAME);
998 sin->sin_port = inp->inp_lport;
999 sin->sin_addr = inp->inp_laddr;
1002 *nam = (struct sockaddr *)sin;
1007 in_setsockaddr_dispatch(netmsg_t msg)
1011 error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1012 lwkt_replymsg(&msg->lmsg, error);
1016 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
1019 struct sockaddr_in *sin;
1022 * Do the malloc first in case it blocks.
1024 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
1026 sin->sin_family = AF_INET;
1027 sin->sin_len = sizeof *sin;
1033 kfree(sin, M_SONAME);
1034 return (ECONNRESET);
1036 sin->sin_port = inp->inp_fport;
1037 sin->sin_addr = inp->inp_faddr;
1040 *nam = (struct sockaddr *)sin;
1045 in_setpeeraddr_dispatch(netmsg_t msg)
1049 error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1050 lwkt_replymsg(&msg->lmsg, error);
1054 in_pcbnotifyall(struct inpcbhead *head, struct in_addr faddr, int err,
1055 void (*notify)(struct inpcb *, int))
1057 struct inpcb *inp, *ninp;
1060 * note: if INP_PLACEMARKER is set we must ignore the rest of
1061 * the structure and skip it.
1064 LIST_FOREACH_MUTABLE(inp, head, inp_list, ninp) {
1065 if (inp->inp_flags & INP_PLACEMARKER)
1068 if (!(inp->inp_vflag & INP_IPV4))
1071 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1072 inp->inp_socket == NULL)
1074 (*notify)(inp, err); /* can remove inp from list! */
1080 in_pcbpurgeif0(struct inpcb *head, struct ifnet *ifp)
1083 struct ip_moptions *imo;
1086 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
1087 if (inp->inp_flags & INP_PLACEMARKER)
1089 imo = inp->inp_moptions;
1090 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
1092 * Unselect the outgoing interface if it is being
1095 if (imo->imo_multicast_ifp == ifp)
1096 imo->imo_multicast_ifp = NULL;
1099 * Drop multicast group membership if we joined
1100 * through the interface being detached.
1102 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1104 if (imo->imo_membership[i]->inm_ifp == ifp) {
1105 in_delmulti(imo->imo_membership[i]);
1107 } else if (gap != 0)
1108 imo->imo_membership[i - gap] =
1109 imo->imo_membership[i];
1111 imo->imo_num_memberships -= gap;
1117 * Check for alternatives when higher level complains
1118 * about service problems. For now, invalidate cached
1119 * routing information. If the route was created dynamically
1120 * (by a redirect), time to try a default gateway again.
1123 in_losing(struct inpcb *inp)
1126 struct rt_addrinfo rtinfo;
1128 if ((rt = inp->inp_route.ro_rt)) {
1129 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1130 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1131 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1132 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1133 rtinfo.rti_flags = rt->rt_flags;
1134 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1135 if (rt->rt_flags & RTF_DYNAMIC)
1136 rtrequest1_global(RTM_DELETE, &rtinfo, NULL, NULL);
1137 inp->inp_route.ro_rt = NULL;
1140 * A new route can be allocated
1141 * the next time output is attempted.
1147 * After a routing change, flush old routing
1148 * and allocate a (hopefully) better one.
1151 in_rtchange(struct inpcb *inp, int err)
1153 if (inp->inp_route.ro_rt) {
1154 rtfree(inp->inp_route.ro_rt);
1155 inp->inp_route.ro_rt = NULL;
1157 * A new route can be allocated the next time
1158 * output is attempted.
1164 * Lookup a PCB based on the local address and port.
1167 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1168 u_int lport_arg, int wild_okay, struct ucred *cred)
1171 int matchwild = 3, wildcard;
1172 u_short lport = lport_arg;
1173 struct inpcbporthead *porthash;
1174 struct inpcbport *phd;
1175 struct inpcb *match = NULL;
1178 * If the porthashbase is shared across several cpus we need
1181 if (pcbinfo->porttoken)
1182 lwkt_gettoken(pcbinfo->porttoken);
1185 * Best fit PCB lookup.
1187 * First see if this local port is in use by looking on the
1190 porthash = &pcbinfo->porthashbase[
1191 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
1192 LIST_FOREACH(phd, porthash, phd_hash) {
1193 if (phd->phd_port == lport)
1198 * Port is in use by one or more PCBs. Look for best
1201 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1204 if ((inp->inp_vflag & INP_IPV4) == 0)
1207 if (inp->inp_faddr.s_addr != INADDR_ANY)
1209 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1210 if (laddr.s_addr == INADDR_ANY)
1212 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1215 if (laddr.s_addr != INADDR_ANY)
1218 if (wildcard && !wild_okay)
1220 if (wildcard < matchwild &&
1223 inp->inp_socket->so_cred->cr_prison)) {
1225 matchwild = wildcard;
1226 if (matchwild == 0) {
1232 if (pcbinfo->porttoken)
1233 lwkt_reltoken(pcbinfo->porttoken);
1238 * Lookup PCB in hash list.
1241 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1242 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1243 boolean_t wildcard, struct ifnet *ifp)
1245 struct inpcbhead *head;
1246 struct inpcb *inp, *jinp=NULL;
1247 u_short fport = fport_arg, lport = lport_arg;
1250 * First look for an exact match.
1252 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1253 laddr.s_addr, lport, pcbinfo->hashmask)];
1254 LIST_FOREACH(inp, head, inp_hash) {
1256 if (!(inp->inp_vflag & INP_IPV4))
1259 if (in_hosteq(inp->inp_faddr, faddr) &&
1260 in_hosteq(inp->inp_laddr, laddr) &&
1261 inp->inp_fport == fport && inp->inp_lport == lport) {
1263 if (inp->inp_socket == NULL ||
1264 inp->inp_socket->so_cred->cr_prison == NULL) {
1275 struct inpcb *local_wild = NULL;
1276 struct inpcb *jinp_wild = NULL;
1278 struct inpcb *local_wild_mapped = NULL;
1280 struct inpcontainer *ic;
1281 struct inpcontainerhead *chead;
1282 struct sockaddr_in jsin;
1286 * Order of socket selection:
1287 * 1. non-jailed, non-wild.
1288 * 2. non-jailed, wild.
1289 * 3. jailed, non-wild.
1292 jsin.sin_family = AF_INET;
1293 chead = &pcbinfo->wildcardhashbase[
1294 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1295 LIST_FOREACH(ic, chead, ic_list) {
1297 jsin.sin_addr.s_addr = laddr.s_addr;
1299 if (!(inp->inp_vflag & INP_IPV4))
1302 if (inp->inp_socket != NULL)
1303 cred = inp->inp_socket->so_cred;
1306 if (cred != NULL && jailed(cred)) {
1310 if (!jailed_ip(cred->cr_prison,
1311 (struct sockaddr *)&jsin))
1314 if (inp->inp_lport == lport) {
1315 if (ifp && ifp->if_type == IFT_FAITH &&
1316 !(inp->inp_flags & INP_FAITH))
1318 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1319 if (cred != NULL && jailed(cred))
1324 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1326 if (INP_CHECK_SOCKAF(inp->inp_socket,
1328 local_wild_mapped = inp;
1339 if (local_wild != NULL)
1340 return (local_wild);
1342 if (local_wild_mapped != NULL)
1343 return (local_wild_mapped);
1357 * Insert PCB into connection hash table.
1360 in_pcbinsconnhash(struct inpcb *inp)
1362 struct inpcbinfo *pcbinfo = inp->inp_cpcbinfo;
1363 struct inpcbhead *bucket;
1364 u_int32_t hashkey_faddr, hashkey_laddr;
1367 if (inp->inp_vflag & INP_IPV6) {
1368 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1369 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1372 hashkey_faddr = inp->inp_faddr.s_addr;
1373 hashkey_laddr = inp->inp_laddr.s_addr;
1378 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1379 ("already on wildcardhash\n"));
1380 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1381 ("already on connhash\n"));
1382 inp->inp_flags |= INP_CONNECTED;
1385 * Insert into the connection hash table.
1387 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1388 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1389 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1393 * Remove PCB from connection hash table.
1396 in_pcbremconnhash(struct inpcb *inp)
1398 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1399 LIST_REMOVE(inp, inp_hash);
1400 inp->inp_flags &= ~INP_CONNECTED;
1404 * Insert PCB into port hash table.
1407 in_pcbinsporthash(struct inpcb *inp)
1409 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1410 struct inpcbporthead *pcbporthash;
1411 struct inpcbport *phd;
1414 * If the porthashbase is shared across several cpus we need
1417 if (pcbinfo->porttoken)
1418 lwkt_gettoken(pcbinfo->porttoken);
1421 * Insert into the port hash table.
1423 pcbporthash = &pcbinfo->porthashbase[
1424 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->porthashmask)];
1426 /* Go through port list and look for a head for this lport. */
1427 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1428 if (phd->phd_port == inp->inp_lport)
1432 /* If none exists, malloc one and tack it on. */
1434 KKASSERT(pcbinfo->portsave != NULL);
1435 phd = pcbinfo->portsave;
1436 pcbinfo->portsave = NULL;
1437 phd->phd_port = inp->inp_lport;
1438 LIST_INIT(&phd->phd_pcblist);
1439 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1443 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1445 if (pcbinfo->porttoken)
1446 lwkt_reltoken(pcbinfo->porttoken);
1447 if (pcbinfo->portsave == NULL) {
1448 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1449 M_PCB, M_INTWAIT | M_ZERO);
1455 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1457 struct inpcontainer *ic;
1458 struct inpcontainerhead *bucket;
1460 bucket = &pcbinfo->wildcardhashbase[
1461 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1463 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1465 LIST_INSERT_HEAD(bucket, ic, ic_list);
1469 * Insert PCB into wildcard hash table.
1472 in_pcbinswildcardhash(struct inpcb *inp)
1474 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1476 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1477 ("already on connhash\n"));
1478 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1479 ("already on wildcardhash\n"));
1480 inp->inp_flags |= INP_WILDCARD;
1482 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1486 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1488 struct inpcontainer *ic;
1489 struct inpcontainerhead *head;
1492 head = &pcbinfo->wildcardhashbase[
1493 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1495 LIST_FOREACH(ic, head, ic_list) {
1496 if (ic->ic_inp == inp)
1499 return; /* not found! */
1502 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
1503 kfree(ic, M_TEMP); /* deallocate container */
1507 * Remove PCB from wildcard hash table.
1510 in_pcbremwildcardhash(struct inpcb *inp)
1512 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1514 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1515 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
1516 inp->inp_flags &= ~INP_WILDCARD;
1520 * Remove PCB from various lists.
1523 in_pcbremlists(struct inpcb *inp)
1525 struct inpcbinfo *pcbinfo;
1527 if (inp->inp_lport) {
1528 struct inpcbport *phd;
1530 pcbinfo = inp->inp_pcbinfo;
1531 if (pcbinfo->porttoken)
1532 lwkt_gettoken(pcbinfo->porttoken);
1535 LIST_REMOVE(inp, inp_portlist);
1536 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1537 LIST_REMOVE(phd, phd_hash);
1540 if (pcbinfo->porttoken)
1541 lwkt_reltoken(pcbinfo->porttoken);
1543 if (inp->inp_flags & INP_WILDCARD) {
1544 in_pcbremwildcardhash(inp);
1545 } else if (inp->inp_flags & INP_CONNECTED) {
1546 in_pcbremconnhash(inp);
1548 LIST_REMOVE(inp, inp_list);
1549 inp->inp_pcbinfo->ipi_count--;
1553 prison_xinpcb(struct thread *td, struct inpcb *inp)
1557 if (td->td_proc == NULL)
1559 cr = td->td_proc->p_ucred;
1560 if (cr->cr_prison == NULL)
1562 if (inp->inp_socket && inp->inp_socket->so_cred &&
1563 inp->inp_socket->so_cred->cr_prison &&
1564 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
1570 in_pcblist_global(SYSCTL_HANDLER_ARGS)
1572 struct inpcbinfo *pcbinfo = arg1;
1573 struct inpcb *inp, *marker;
1578 * The process of preparing the TCB list is too time-consuming and
1579 * resource-intensive to repeat twice on every request.
1581 if (req->oldptr == NULL) {
1582 n = pcbinfo->ipi_count;
1583 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1587 if (req->newptr != NULL)
1591 * OK, now we're committed to doing something. Re-fetch ipi_count
1592 * after obtaining the generation count.
1594 n = pcbinfo->ipi_count;
1596 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
1597 marker->inp_flags |= INP_PLACEMARKER;
1598 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1603 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
1604 LIST_REMOVE(marker, inp_list);
1605 LIST_INSERT_AFTER(inp, marker, inp_list);
1607 if (inp->inp_flags & INP_PLACEMARKER)
1609 if (prison_xinpcb(req->td, inp))
1611 bzero(&xi, sizeof xi);
1612 xi.xi_len = sizeof xi;
1613 bcopy(inp, &xi.xi_inp, sizeof *inp);
1614 if (inp->inp_socket)
1615 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1616 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
1620 LIST_REMOVE(marker, inp_list);
1621 if (error == 0 && i < n) {
1622 bzero(&xi, sizeof xi);
1623 xi.xi_len = sizeof xi;
1625 error = SYSCTL_OUT(req, &xi, sizeof xi);
1629 kfree(marker, M_TEMP);
1634 in_pcblist_global_nomarker(SYSCTL_HANDLER_ARGS, struct xinpcb **xi0, int *nxi0)
1636 struct inpcbinfo *pcbinfo = arg1;
1645 * The process of preparing the PCB list is too time-consuming and
1646 * resource-intensive to repeat twice on every request.
1648 if (req->oldptr == NULL) {
1649 int n = pcbinfo->ipi_count;
1651 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1655 if (req->newptr != NULL)
1658 if (pcbinfo->ipi_count == 0)
1662 xi = kmalloc(pcbinfo->ipi_count * sizeof(*xi), M_TEMP,
1663 M_WAITOK | M_ZERO | M_NULLOK);
1667 LIST_FOREACH(inp, &pcbinfo->pcblisthead, inp_list) {
1668 struct xinpcb *xi_ptr = &xi[nxi];
1670 if (prison_xinpcb(req->td, inp))
1673 xi_ptr->xi_len = sizeof(*xi_ptr);
1674 bcopy(inp, &xi_ptr->xi_inp, sizeof(*inp));
1675 if (inp->inp_socket)
1676 sotoxsocket(inp->inp_socket, &xi_ptr->xi_socket);
1692 in_savefaddr(struct socket *so, const struct sockaddr *faddr)
1694 struct sockaddr_in *sin;
1696 KASSERT(faddr->sa_family == AF_INET,
1697 ("not AF_INET faddr %d\n", faddr->sa_family));
1699 sin = kmalloc(sizeof(*sin), M_SONAME, M_WAITOK | M_ZERO);
1700 sin->sin_family = AF_INET;
1701 sin->sin_len = sizeof(*sin);
1702 sin->sin_port = ((const struct sockaddr_in *)faddr)->sin_port;
1703 sin->sin_addr = ((const struct sockaddr_in *)faddr)->sin_addr;
1705 so->so_faddr = (struct sockaddr *)sin;