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 $
70 #include "opt_ipsec.h"
71 #include "opt_inet6.h"
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
77 #include <sys/domain.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
84 #include <sys/kernel.h>
85 #include <sys/sysctl.h>
87 #include <sys/thread2.h>
88 #include <sys/socketvar2.h>
89 #include <sys/msgport2.h>
91 #include <machine/limits.h>
94 #include <net/if_types.h>
95 #include <net/route.h>
97 #include <netinet/in.h>
98 #include <netinet/in_pcb.h>
99 #include <netinet/in_var.h>
100 #include <netinet/ip_var.h>
102 #include <netinet/ip6.h>
103 #include <netinet6/ip6_var.h>
107 #include <netinet6/ipsec.h>
108 #include <netproto/key/key.h>
109 #include <netproto/ipsec/esp_var.h>
113 #if defined(IPSEC) || defined(IPSEC_ESP)
114 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
117 #include <netproto/ipsec/ipsec.h>
118 #include <netproto/ipsec/key.h>
120 #endif /* FAST_IPSEC */
122 struct in_addr zeroin_addr;
125 * These configure the range of local port addresses assigned to
126 * "unspecified" outgoing connections/packets/whatever.
128 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
129 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
131 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
132 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
134 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
135 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
137 #define RANGECHK(var, min, max) \
138 if ((var) < (min)) { (var) = (min); } \
139 else if ((var) > (max)) { (var) = (max); }
141 int udpencap_enable = 1; /* enabled by default */
142 int udpencap_port = 4500; /* triggers decapsulation */
145 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
149 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
151 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
152 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
154 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
155 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
157 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
158 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
165 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
167 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
168 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
169 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
170 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
171 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
172 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
173 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
174 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
175 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
176 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
177 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
178 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
181 * in_pcb.c: manage the Protocol Control Blocks.
183 * NOTE: It is assumed that most of these functions will be called from
184 * a critical section. XXX - There are, unfortunately, a few exceptions
185 * to this rule that should be fixed.
187 * NOTE: The caller should initialize the cpu field to the cpu running the
188 * protocol stack associated with this inpcbinfo.
192 in_pcbinfo_init(struct inpcbinfo *pcbinfo)
194 LIST_INIT(&pcbinfo->pcblisthead);
196 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave), M_PCB,
200 struct baddynamicports baddynamicports;
203 * Check if the specified port is invalid for dynamic allocation.
206 in_baddynamic(u_int16_t port, u_int16_t proto)
210 return (DP_ISSET(baddynamicports.tcp, port));
213 /* Cannot preset this as it is a sysctl */
214 if (port == udpencap_port)
217 return (DP_ISSET(baddynamicports.udp, port));
225 * Allocate a PCB and associate it with the socket.
228 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
235 inp = kmalloc(pcbinfo->ipi_size, M_PCB, M_WAITOK|M_ZERO);
236 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
237 inp->inp_pcbinfo = inp->inp_cpcbinfo = pcbinfo;
238 inp->inp_socket = so;
240 error = ipsec_init_policy(so, &inp->inp_sp);
247 if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
248 inp->inp_flags |= IN6P_IPV6_V6ONLY;
249 if (ip6_auto_flowlabel)
250 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
254 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
255 pcbinfo->ipi_count++;
260 * Unlink a pcb with the intention of moving it to another cpu with a
261 * different pcbinfo. While unlinked nothing should attempt to dereference
262 * inp_pcbinfo, NULL it out so we assert if it does.
265 in_pcbunlink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
267 KKASSERT(inp->inp_pcbinfo == pcbinfo);
269 LIST_REMOVE(inp, inp_list);
270 pcbinfo->ipi_count--;
271 inp->inp_pcbinfo = NULL;
275 * Relink a pcb into a new pcbinfo.
278 in_pcblink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
280 KKASSERT(inp->inp_pcbinfo == NULL);
281 inp->inp_pcbinfo = pcbinfo;
282 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
283 pcbinfo->ipi_count++;
287 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
289 struct socket *so = inp->inp_socket;
290 unsigned short *lastport;
291 struct sockaddr_in *sin;
292 struct sockaddr_in jsin;
293 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
294 struct ucred *cred = NULL;
296 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
299 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
300 return (EADDRNOTAVAIL);
301 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
302 return (EINVAL); /* already bound */
304 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
305 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
307 cred = td->td_proc->p_ucred;
310 * This has to be atomic. If the porthash is shared across multiple
311 * protocol threads (aka tcp) then the token will be non-NULL.
313 if (pcbinfo->porttoken)
314 lwkt_gettoken(pcbinfo->porttoken);
317 sin = (struct sockaddr_in *)nam;
318 if (nam->sa_len != sizeof *sin) {
324 * We should check the family, but old programs
325 * incorrectly fail to initialize it.
327 if (sin->sin_family != AF_INET) {
328 error = EAFNOSUPPORT;
332 if (!prison_replace_wildcards(td, nam)) {
336 lport = sin->sin_port;
337 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
339 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
340 * allow complete duplication of binding if
341 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
342 * and a multicast address is bound on both
343 * new and duplicated sockets.
345 if (so->so_options & SO_REUSEADDR)
346 reuseport = SO_REUSEADDR | SO_REUSEPORT;
347 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
348 sin->sin_port = 0; /* yech... */
349 bzero(&sin->sin_zero, sizeof sin->sin_zero);
350 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL) {
351 error = EADDRNOTAVAIL;
359 if (ntohs(lport) < IPPORT_RESERVED &&
361 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0)) {
365 if (so->so_cred->cr_uid != 0 &&
366 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
367 t = in_pcblookup_local(pcbinfo,
373 (!in_nullhost(sin->sin_addr) ||
374 !in_nullhost(t->inp_laddr) ||
375 (t->inp_socket->so_options &
376 SO_REUSEPORT) == 0) &&
377 (so->so_cred->cr_uid !=
378 t->inp_socket->so_cred->cr_uid)) {
380 if (!in_nullhost(sin->sin_addr) ||
381 !in_nullhost(t->inp_laddr) ||
383 INP_SOCKAF(t->inp_socket))
391 if (cred && !prison_replace_wildcards(td, nam)) {
392 error = EADDRNOTAVAIL;
395 t = in_pcblookup_local(pcbinfo, sin->sin_addr, lport,
397 if (t && !(reuseport & t->inp_socket->so_options)) {
399 if (!in_nullhost(sin->sin_addr) ||
400 !in_nullhost(t->inp_laddr) ||
401 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
409 inp->inp_laddr = sin->sin_addr;
415 jsin.sin_family = AF_INET;
416 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
417 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
418 inp->inp_laddr.s_addr = INADDR_ANY;
422 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
424 inp->inp_flags |= INP_ANONPORT;
426 if (inp->inp_flags & INP_HIGHPORT) {
427 first = ipport_hifirstauto; /* sysctl */
428 last = ipport_hilastauto;
429 lastport = &pcbinfo->lasthi;
430 } else if (inp->inp_flags & INP_LOWPORT) {
432 (error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
433 inp->inp_laddr.s_addr = INADDR_ANY;
436 first = ipport_lowfirstauto; /* 1023 */
437 last = ipport_lowlastauto; /* 600 */
438 lastport = &pcbinfo->lastlow;
440 first = ipport_firstauto; /* sysctl */
441 last = ipport_lastauto;
442 lastport = &pcbinfo->lastport;
445 * Simple check to ensure all ports are not used up causing
448 * We split the two cases (up and down) so that the direction
449 * is not being tested on each round of the loop.
455 count = first - last;
458 if (count-- < 0) { /* completely used? */
459 inp->inp_laddr.s_addr = INADDR_ANY;
460 error = EADDRNOTAVAIL;
464 if (*lastport > first || *lastport < last)
466 lport = htons(*lastport);
467 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
473 count = last - first;
476 if (count-- < 0) { /* completely used? */
477 inp->inp_laddr.s_addr = INADDR_ANY;
478 error = EADDRNOTAVAIL;
482 if (*lastport < first || *lastport > last)
484 lport = htons(*lastport);
485 } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
489 inp->inp_lport = lport;
491 jsin.sin_family = AF_INET;
492 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
493 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin)) {
494 inp->inp_laddr.s_addr = INADDR_ANY;
499 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
501 if (in_pcbinsporthash(inp) != 0) {
502 inp->inp_laddr.s_addr = INADDR_ANY;
509 if (pcbinfo->porttoken)
510 lwkt_reltoken(pcbinfo->porttoken);
514 static struct inpcb *
515 in_pcblookup_addrport(struct inpcbinfo *pcbinfo, struct in_addr laddr,
516 u_short lport, struct in_addr faddr, u_short fport, struct ucred *cred)
519 struct inpcbporthead *porthash;
520 struct inpcbport *phd;
521 struct inpcb *match = NULL;
524 * If the porthashbase is shared across several cpus we need
527 if (pcbinfo->porttoken)
528 lwkt_gettoken(pcbinfo->porttoken);
531 * Best fit PCB lookup.
533 * First see if this local port is in use by looking on the
536 porthash = &pcbinfo->porthashbase[
537 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
538 LIST_FOREACH(phd, porthash, phd_hash) {
539 if (phd->phd_port == lport)
543 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
545 if ((inp->inp_vflag & INP_IPV4) == 0)
548 if (inp->inp_laddr.s_addr != INADDR_ANY &&
549 inp->inp_laddr.s_addr != laddr.s_addr)
552 if (inp->inp_faddr.s_addr != INADDR_ANY &&
553 inp->inp_faddr.s_addr != faddr.s_addr)
556 if (inp->inp_fport != 0 && inp->inp_fport != fport)
561 inp->inp_socket->so_cred->cr_prison) {
567 if (pcbinfo->porttoken)
568 lwkt_reltoken(pcbinfo->porttoken);
573 in_pcbconn_bind(struct inpcb *inp, const struct sockaddr *nam,
576 struct proc *p = td->td_proc;
577 unsigned short *lastport;
578 const struct sockaddr_in *sin = (const struct sockaddr_in *)nam;
579 struct sockaddr_in jsin;
580 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
581 struct ucred *cred = NULL;
584 int count, error, dup = 0;
586 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
587 return (EADDRNOTAVAIL);
589 KKASSERT(inp->inp_laddr.s_addr != INADDR_ANY);
590 if (inp->inp_lport != 0)
591 return (EINVAL); /* already bound */
597 * This has to be atomic. If the porthash is shared across multiple
598 * protocol threads (aka tcp) then the token will be non-NULL.
600 if (pcbinfo->porttoken)
601 lwkt_gettoken(pcbinfo->porttoken);
603 jsin.sin_family = AF_INET;
604 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
605 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
606 inp->inp_laddr.s_addr = INADDR_ANY;
610 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
612 inp->inp_flags |= INP_ANONPORT;
614 if (inp->inp_flags & INP_HIGHPORT) {
615 first = ipport_hifirstauto; /* sysctl */
616 last = ipport_hilastauto;
617 lastport = &pcbinfo->lasthi;
618 } else if (inp->inp_flags & INP_LOWPORT) {
620 (error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
621 inp->inp_laddr.s_addr = INADDR_ANY;
624 first = ipport_lowfirstauto; /* 1023 */
625 last = ipport_lowlastauto; /* 600 */
626 lastport = &pcbinfo->lastlow;
628 first = ipport_firstauto; /* sysctl */
629 last = ipport_lastauto;
630 lastport = &pcbinfo->lastport;
635 * Simple check to ensure all ports are not used up causing
638 * We split the two cases (up and down) so that the direction
639 * is not being tested on each round of the loop.
645 count = first - last;
648 if (count-- < 0) { /* completely used? */
649 inp->inp_laddr.s_addr = INADDR_ANY;
650 error = EADDRNOTAVAIL;
654 if (*lastport > first || *lastport < last)
656 lport = htons(*lastport);
657 } while (in_pcblookup_addrport(pcbinfo, inp->inp_laddr, lport,
658 sin->sin_addr, sin->sin_port, cred));
663 count = last - first;
666 if (count-- < 0) { /* completely used? */
667 inp->inp_laddr.s_addr = INADDR_ANY;
668 error = EADDRNOTAVAIL;
672 if (*lastport < first || *lastport > last)
674 lport = htons(*lastport);
675 } while (in_pcblookup_addrport(pcbinfo, inp->inp_laddr, lport,
676 sin->sin_addr, sin->sin_port, cred));
679 /* This could happen on loopback interface */
680 if (sin->sin_port == lport &&
681 sin->sin_addr.s_addr == inp->inp_laddr.s_addr) {
684 * Duplicate again; give up
686 inp->inp_laddr.s_addr = INADDR_ANY;
687 error = EADDRNOTAVAIL;
693 inp->inp_lport = lport;
695 jsin.sin_family = AF_INET;
696 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
697 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin)) {
698 inp->inp_laddr.s_addr = INADDR_ANY;
703 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
705 if (in_pcbinsporthash(inp) != 0) {
706 inp->inp_laddr.s_addr = INADDR_ANY;
713 if (pcbinfo->porttoken)
714 lwkt_reltoken(pcbinfo->porttoken);
719 * Transform old in_pcbconnect() into an inner subroutine for new
720 * in_pcbconnect(): Do some validity-checking on the remote
721 * address (in mbuf 'nam') and then determine local host address
722 * (i.e., which interface) to use to access that remote host.
724 * This preserves definition of in_pcbconnect(), while supporting a
725 * slightly different version for T/TCP. (This is more than
726 * a bit of a kludge, but cleaning up the internal interfaces would
727 * have forced minor changes in every protocol).
730 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
731 struct sockaddr_in **plocal_sin, struct thread *td)
733 struct in_ifaddr *ia;
734 struct ucred *cred = NULL;
735 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
736 struct sockaddr *jsin;
737 int jailed = 0, alloc_route = 0;
739 if (nam->sa_len != sizeof *sin)
741 if (sin->sin_family != AF_INET)
742 return (EAFNOSUPPORT);
743 if (sin->sin_port == 0)
744 return (EADDRNOTAVAIL);
745 if (td && td->td_proc && td->td_proc->p_ucred)
746 cred = td->td_proc->p_ucred;
747 if (cred && cred->cr_prison)
749 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
750 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
752 * If the destination address is INADDR_ANY,
753 * use the primary local address.
754 * If the supplied address is INADDR_BROADCAST,
755 * and the primary interface supports broadcast,
756 * choose the broadcast address for that interface.
758 if (sin->sin_addr.s_addr == INADDR_ANY)
759 sin->sin_addr = IA_SIN(ia)->sin_addr;
760 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
761 (ia->ia_ifp->if_flags & IFF_BROADCAST))
762 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
764 if (inp->inp_laddr.s_addr == INADDR_ANY) {
769 * If route is known or can be allocated now,
770 * our src addr is taken from the i/f, else punt.
771 * Note that we should check the address family of the cached
772 * destination, in case of sharing the cache with IPv6.
774 ro = &inp->inp_route;
776 (!(ro->ro_rt->rt_flags & RTF_UP) ||
777 ro->ro_dst.sa_family != AF_INET ||
778 satosin(&ro->ro_dst)->sin_addr.s_addr !=
779 sin->sin_addr.s_addr ||
780 inp->inp_socket->so_options & SO_DONTROUTE)) {
784 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
785 (ro->ro_rt == NULL ||
786 ro->ro_rt->rt_ifp == NULL)) {
787 /* No route yet, so try to acquire one */
788 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
789 ro->ro_dst.sa_family = AF_INET;
790 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
791 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
797 * If we found a route, use the address
798 * corresponding to the outgoing interface
799 * unless it is the loopback (in case a route
800 * to our address on another net goes to loopback).
802 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
804 if (jailed_ip(cred->cr_prison,
805 ro->ro_rt->rt_ifa->ifa_addr)) {
806 ia = ifatoia(ro->ro_rt->rt_ifa);
809 ia = ifatoia(ro->ro_rt->rt_ifa);
813 u_short fport = sin->sin_port;
816 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
817 if (ia && jailed && !jailed_ip(cred->cr_prison,
818 sintosa(&ia->ia_addr)))
821 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
822 if (ia && jailed && !jailed_ip(cred->cr_prison,
823 sintosa(&ia->ia_addr)))
825 sin->sin_port = fport;
827 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
828 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
829 if (ia && jailed && !jailed_ip(cred->cr_prison,
830 sintosa(&ia->ia_addr)))
833 if (!jailed && ia == NULL)
837 * If the destination address is multicast and an outgoing
838 * interface has been set as a multicast option, use the
839 * address of that interface as our source address.
841 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
842 inp->inp_moptions != NULL) {
843 struct ip_moptions *imo;
846 imo = inp->inp_moptions;
847 if (imo->imo_multicast_ifp != NULL) {
848 struct in_ifaddr_container *iac;
850 ifp = imo->imo_multicast_ifp;
853 &in_ifaddrheads[mycpuid], ia_link) {
854 if (iac->ia->ia_ifp == ifp) {
864 * Don't do pcblookup call here; return interface in plocal_sin
865 * and exit to caller, that will do the lookup.
867 if (ia == NULL && jailed) {
868 if ((jsin = prison_get_nonlocal(cred->cr_prison, AF_INET, NULL)) != NULL ||
869 (jsin = prison_get_local(cred->cr_prison, AF_INET, NULL)) != NULL) {
870 *plocal_sin = satosin(jsin);
876 *plocal_sin = &ia->ia_addr;
882 struct route *ro = &inp->inp_route;
884 if (ro->ro_rt != NULL)
886 bzero(ro, sizeof(*ro));
888 return (EADDRNOTAVAIL);
893 * Connect from a socket to a specified address.
894 * Both address and port must be specified in argument sin.
895 * If don't have a local address for this socket yet,
899 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
901 struct sockaddr_in *if_sin;
902 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
905 /* Call inner routine to assign local interface address. */
906 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
909 if (in_pcblookup_hash(inp->inp_cpcbinfo, sin->sin_addr, sin->sin_port,
910 inp->inp_laddr.s_addr ?
911 inp->inp_laddr : if_sin->sin_addr,
912 inp->inp_lport, FALSE, NULL) != NULL) {
915 if (inp->inp_laddr.s_addr == INADDR_ANY) {
916 if (inp->inp_lport == 0) {
917 error = in_pcbbind(inp, NULL, td);
921 inp->inp_laddr = if_sin->sin_addr;
923 inp->inp_faddr = sin->sin_addr;
924 inp->inp_fport = sin->sin_port;
925 in_pcbinsconnhash(inp);
930 in_pcbdisconnect(struct inpcb *inp)
933 inp->inp_faddr.s_addr = INADDR_ANY;
935 in_pcbremconnhash(inp);
936 if (inp->inp_socket->so_state & SS_NOFDREF)
941 in_pcbdetach(struct inpcb *inp)
943 struct socket *so = inp->inp_socket;
944 struct inpcbinfo *ipi = inp->inp_pcbinfo;
947 ipsec4_delete_pcbpolicy(inp);
949 inp->inp_gencnt = ++ipi->ipi_gencnt;
950 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
953 sofree(so); /* remove pcb ref */
954 if (inp->inp_options)
955 m_free(inp->inp_options);
956 if (inp->inp_route.ro_rt)
957 rtfree(inp->inp_route.ro_rt);
958 ip_freemoptions(inp->inp_moptions);
964 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
965 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
966 * in struct pr_usrreqs, so that protocols can just reference then directly
967 * without the need for a wrapper function. The socket must have a valid
968 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
969 * except through a kernel programming error, so it is acceptable to panic
970 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
971 * because there actually /is/ a programming error somewhere... XXX)
974 in_setsockaddr(struct socket *so, struct sockaddr **nam)
977 struct sockaddr_in *sin;
980 * Do the malloc first in case it blocks.
982 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
983 sin->sin_family = AF_INET;
984 sin->sin_len = sizeof *sin;
990 kfree(sin, M_SONAME);
993 sin->sin_port = inp->inp_lport;
994 sin->sin_addr = inp->inp_laddr;
997 *nam = (struct sockaddr *)sin;
1002 in_setsockaddr_dispatch(netmsg_t msg)
1006 error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1007 lwkt_replymsg(&msg->lmsg, error);
1011 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
1014 struct sockaddr_in *sin;
1017 * Do the malloc first in case it blocks.
1019 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1020 sin->sin_family = AF_INET;
1021 sin->sin_len = sizeof *sin;
1027 kfree(sin, M_SONAME);
1028 return (ECONNRESET);
1030 sin->sin_port = inp->inp_fport;
1031 sin->sin_addr = inp->inp_faddr;
1034 *nam = (struct sockaddr *)sin;
1039 in_setpeeraddr_dispatch(netmsg_t msg)
1043 error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1044 lwkt_replymsg(&msg->lmsg, error);
1048 in_pcbnotifyall(struct inpcbhead *head, struct in_addr faddr, int err,
1049 void (*notify)(struct inpcb *, int))
1051 struct inpcb *inp, *ninp;
1054 * note: if INP_PLACEMARKER is set we must ignore the rest of
1055 * the structure and skip it.
1058 LIST_FOREACH_MUTABLE(inp, head, inp_list, ninp) {
1059 if (inp->inp_flags & INP_PLACEMARKER)
1062 if (!(inp->inp_vflag & INP_IPV4))
1065 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1066 inp->inp_socket == NULL)
1068 (*notify)(inp, err); /* can remove inp from list! */
1074 in_pcbpurgeif0(struct inpcb *head, struct ifnet *ifp)
1077 struct ip_moptions *imo;
1080 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
1081 if (inp->inp_flags & INP_PLACEMARKER)
1083 imo = inp->inp_moptions;
1084 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
1086 * Unselect the outgoing interface if it is being
1089 if (imo->imo_multicast_ifp == ifp)
1090 imo->imo_multicast_ifp = NULL;
1093 * Drop multicast group membership if we joined
1094 * through the interface being detached.
1096 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1098 if (imo->imo_membership[i]->inm_ifp == ifp) {
1099 in_delmulti(imo->imo_membership[i]);
1101 } else if (gap != 0)
1102 imo->imo_membership[i - gap] =
1103 imo->imo_membership[i];
1105 imo->imo_num_memberships -= gap;
1111 * Check for alternatives when higher level complains
1112 * about service problems. For now, invalidate cached
1113 * routing information. If the route was created dynamically
1114 * (by a redirect), time to try a default gateway again.
1117 in_losing(struct inpcb *inp)
1120 struct rt_addrinfo rtinfo;
1122 if ((rt = inp->inp_route.ro_rt)) {
1123 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1124 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1125 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1126 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1127 rtinfo.rti_flags = rt->rt_flags;
1128 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1129 if (rt->rt_flags & RTF_DYNAMIC)
1130 rtrequest1_global(RTM_DELETE, &rtinfo, NULL, NULL);
1131 inp->inp_route.ro_rt = NULL;
1134 * A new route can be allocated
1135 * the next time output is attempted.
1141 * After a routing change, flush old routing
1142 * and allocate a (hopefully) better one.
1145 in_rtchange(struct inpcb *inp, int err)
1147 if (inp->inp_route.ro_rt) {
1148 rtfree(inp->inp_route.ro_rt);
1149 inp->inp_route.ro_rt = NULL;
1151 * A new route can be allocated the next time
1152 * output is attempted.
1158 * Lookup a PCB based on the local address and port.
1161 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
1162 u_int lport_arg, int wild_okay, struct ucred *cred)
1165 int matchwild = 3, wildcard;
1166 u_short lport = lport_arg;
1167 struct inpcbporthead *porthash;
1168 struct inpcbport *phd;
1169 struct inpcb *match = NULL;
1172 * If the porthashbase is shared across several cpus we need
1175 if (pcbinfo->porttoken)
1176 lwkt_gettoken(pcbinfo->porttoken);
1179 * Best fit PCB lookup.
1181 * First see if this local port is in use by looking on the
1184 porthash = &pcbinfo->porthashbase[
1185 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
1186 LIST_FOREACH(phd, porthash, phd_hash) {
1187 if (phd->phd_port == lport)
1192 * Port is in use by one or more PCBs. Look for best
1195 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1198 if ((inp->inp_vflag & INP_IPV4) == 0)
1201 if (inp->inp_faddr.s_addr != INADDR_ANY)
1203 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1204 if (laddr.s_addr == INADDR_ANY)
1206 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1209 if (laddr.s_addr != INADDR_ANY)
1212 if (wildcard && !wild_okay)
1214 if (wildcard < matchwild &&
1217 inp->inp_socket->so_cred->cr_prison)) {
1219 matchwild = wildcard;
1220 if (matchwild == 0) {
1226 if (pcbinfo->porttoken)
1227 lwkt_reltoken(pcbinfo->porttoken);
1232 * Lookup PCB in hash list.
1235 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1236 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1237 boolean_t wildcard, struct ifnet *ifp)
1239 struct inpcbhead *head;
1240 struct inpcb *inp, *jinp=NULL;
1241 u_short fport = fport_arg, lport = lport_arg;
1244 * First look for an exact match.
1246 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1247 laddr.s_addr, lport, pcbinfo->hashmask)];
1248 LIST_FOREACH(inp, head, inp_hash) {
1250 if (!(inp->inp_vflag & INP_IPV4))
1253 if (in_hosteq(inp->inp_faddr, faddr) &&
1254 in_hosteq(inp->inp_laddr, laddr) &&
1255 inp->inp_fport == fport && inp->inp_lport == lport) {
1257 if (inp->inp_socket == NULL ||
1258 inp->inp_socket->so_cred->cr_prison == NULL) {
1269 struct inpcb *local_wild = NULL;
1270 struct inpcb *jinp_wild = NULL;
1272 struct inpcb *local_wild_mapped = NULL;
1274 struct inpcontainer *ic;
1275 struct inpcontainerhead *chead;
1276 struct sockaddr_in jsin;
1280 * Order of socket selection:
1281 * 1. non-jailed, non-wild.
1282 * 2. non-jailed, wild.
1283 * 3. jailed, non-wild.
1286 jsin.sin_family = AF_INET;
1287 chead = &pcbinfo->wildcardhashbase[
1288 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1289 LIST_FOREACH(ic, chead, ic_list) {
1291 jsin.sin_addr.s_addr = laddr.s_addr;
1293 if (!(inp->inp_vflag & INP_IPV4))
1296 if (inp->inp_socket != NULL)
1297 cred = inp->inp_socket->so_cred;
1300 if (cred != NULL && jailed(cred)) {
1304 if (!jailed_ip(cred->cr_prison,
1305 (struct sockaddr *)&jsin))
1308 if (inp->inp_lport == lport) {
1309 if (ifp && ifp->if_type == IFT_FAITH &&
1310 !(inp->inp_flags & INP_FAITH))
1312 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1313 if (cred != NULL && jailed(cred))
1318 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1320 if (INP_CHECK_SOCKAF(inp->inp_socket,
1322 local_wild_mapped = inp;
1333 if (local_wild != NULL)
1334 return (local_wild);
1336 if (local_wild_mapped != NULL)
1337 return (local_wild_mapped);
1351 * Insert PCB into connection hash table.
1354 in_pcbinsconnhash(struct inpcb *inp)
1356 struct inpcbinfo *pcbinfo = inp->inp_cpcbinfo;
1357 struct inpcbhead *bucket;
1358 u_int32_t hashkey_faddr, hashkey_laddr;
1361 if (inp->inp_vflag & INP_IPV6) {
1362 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1363 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1366 hashkey_faddr = inp->inp_faddr.s_addr;
1367 hashkey_laddr = inp->inp_laddr.s_addr;
1372 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1373 ("already on wildcardhash\n"));
1374 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1375 ("already on connhash\n"));
1376 inp->inp_flags |= INP_CONNECTED;
1379 * Insert into the connection hash table.
1381 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1382 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1383 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1387 * Remove PCB from connection hash table.
1390 in_pcbremconnhash(struct inpcb *inp)
1392 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1393 LIST_REMOVE(inp, inp_hash);
1394 inp->inp_flags &= ~INP_CONNECTED;
1398 * Insert PCB into port hash table.
1401 in_pcbinsporthash(struct inpcb *inp)
1403 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1404 struct inpcbporthead *pcbporthash;
1405 struct inpcbport *phd;
1408 * If the porthashbase is shared across several cpus we need
1411 if (pcbinfo->porttoken)
1412 lwkt_gettoken(pcbinfo->porttoken);
1415 * Insert into the port hash table.
1417 pcbporthash = &pcbinfo->porthashbase[
1418 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->porthashmask)];
1420 /* Go through port list and look for a head for this lport. */
1421 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1422 if (phd->phd_port == inp->inp_lport)
1426 /* If none exists, malloc one and tack it on. */
1428 KKASSERT(pcbinfo->portsave != NULL);
1429 phd = pcbinfo->portsave;
1430 pcbinfo->portsave = NULL;
1431 phd->phd_port = inp->inp_lport;
1432 LIST_INIT(&phd->phd_pcblist);
1433 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1437 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1439 if (pcbinfo->porttoken)
1440 lwkt_reltoken(pcbinfo->porttoken);
1441 if (pcbinfo->portsave == NULL) {
1442 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1443 M_PCB, M_INTWAIT | M_ZERO);
1449 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1451 struct inpcontainer *ic;
1452 struct inpcontainerhead *bucket;
1454 bucket = &pcbinfo->wildcardhashbase[
1455 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1457 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1459 LIST_INSERT_HEAD(bucket, ic, ic_list);
1463 * Insert PCB into wildcard hash table.
1466 in_pcbinswildcardhash(struct inpcb *inp)
1468 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1470 KASSERT(!(inp->inp_flags & INP_CONNECTED),
1471 ("already on connhash\n"));
1472 KASSERT(!(inp->inp_flags & INP_WILDCARD),
1473 ("already on wildcardhash\n"));
1474 inp->inp_flags |= INP_WILDCARD;
1476 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1480 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1482 struct inpcontainer *ic;
1483 struct inpcontainerhead *head;
1486 head = &pcbinfo->wildcardhashbase[
1487 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1489 LIST_FOREACH(ic, head, ic_list) {
1490 if (ic->ic_inp == inp)
1493 return; /* not found! */
1496 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
1497 kfree(ic, M_TEMP); /* deallocate container */
1501 * Remove PCB from wildcard hash table.
1504 in_pcbremwildcardhash(struct inpcb *inp)
1506 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1508 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1509 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
1510 inp->inp_flags &= ~INP_WILDCARD;
1514 * Remove PCB from various lists.
1517 in_pcbremlists(struct inpcb *inp)
1519 struct inpcbinfo *pcbinfo;
1521 if (inp->inp_lport) {
1522 struct inpcbport *phd;
1524 pcbinfo = inp->inp_pcbinfo;
1525 if (pcbinfo->porttoken)
1526 lwkt_gettoken(pcbinfo->porttoken);
1529 LIST_REMOVE(inp, inp_portlist);
1530 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1531 LIST_REMOVE(phd, phd_hash);
1534 if (pcbinfo->porttoken)
1535 lwkt_reltoken(pcbinfo->porttoken);
1537 if (inp->inp_flags & INP_WILDCARD) {
1538 in_pcbremwildcardhash(inp);
1539 } else if (inp->inp_flags & INP_CONNECTED) {
1540 in_pcbremconnhash(inp);
1542 LIST_REMOVE(inp, inp_list);
1543 inp->inp_pcbinfo->ipi_count--;
1547 prison_xinpcb(struct thread *td, struct inpcb *inp)
1551 if (td->td_proc == NULL)
1553 cr = td->td_proc->p_ucred;
1554 if (cr->cr_prison == NULL)
1556 if (inp->inp_socket && inp->inp_socket->so_cred &&
1557 inp->inp_socket->so_cred->cr_prison &&
1558 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
1564 in_pcblist_global(SYSCTL_HANDLER_ARGS)
1566 struct inpcbinfo *pcbinfo = arg1;
1567 struct inpcb *inp, *marker;
1572 * The process of preparing the TCB list is too time-consuming and
1573 * resource-intensive to repeat twice on every request.
1575 if (req->oldptr == NULL) {
1576 n = pcbinfo->ipi_count;
1577 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1581 if (req->newptr != NULL)
1585 * OK, now we're committed to doing something. Re-fetch ipi_count
1586 * after obtaining the generation count.
1588 n = pcbinfo->ipi_count;
1590 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
1591 marker->inp_flags |= INP_PLACEMARKER;
1592 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1597 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
1598 LIST_REMOVE(marker, inp_list);
1599 LIST_INSERT_AFTER(inp, marker, inp_list);
1601 if (inp->inp_flags & INP_PLACEMARKER)
1603 if (prison_xinpcb(req->td, inp))
1605 bzero(&xi, sizeof xi);
1606 xi.xi_len = sizeof xi;
1607 bcopy(inp, &xi.xi_inp, sizeof *inp);
1608 if (inp->inp_socket)
1609 sotoxsocket(inp->inp_socket, &xi.xi_socket);
1610 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
1614 LIST_REMOVE(marker, inp_list);
1615 if (error == 0 && i < n) {
1616 bzero(&xi, sizeof xi);
1617 xi.xi_len = sizeof xi;
1619 error = SYSCTL_OUT(req, &xi, sizeof xi);
1623 kfree(marker, M_TEMP);
1628 in_pcblist_global_nomarker(SYSCTL_HANDLER_ARGS, struct xinpcb **xi0, int *nxi0)
1630 struct inpcbinfo *pcbinfo = arg1;
1639 * The process of preparing the PCB list is too time-consuming and
1640 * resource-intensive to repeat twice on every request.
1642 if (req->oldptr == NULL) {
1643 int n = pcbinfo->ipi_count;
1645 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1649 if (req->newptr != NULL)
1652 if (pcbinfo->ipi_count == 0)
1656 xi = kmalloc(pcbinfo->ipi_count * sizeof(*xi), M_TEMP,
1657 M_WAITOK | M_ZERO | M_NULLOK);
1661 LIST_FOREACH(inp, &pcbinfo->pcblisthead, inp_list) {
1662 struct xinpcb *xi_ptr = &xi[nxi];
1664 if (prison_xinpcb(req->td, inp))
1667 xi_ptr->xi_len = sizeof(*xi_ptr);
1668 bcopy(inp, &xi_ptr->xi_inp, sizeof(*inp));
1669 if (inp->inp_socket)
1670 sotoxsocket(inp->inp_socket, &xi_ptr->xi_socket);
1686 in_savefaddr(struct socket *so, const struct sockaddr *faddr)
1688 struct sockaddr_in *sin;
1690 KASSERT(faddr->sa_family == AF_INET,
1691 ("not AF_INET faddr %d\n", faddr->sa_family));
1693 sin = kmalloc(sizeof(*sin), M_SONAME, M_WAITOK | M_ZERO);
1694 sin->sin_family = AF_INET;
1695 sin->sin_len = sizeof(*sin);
1696 sin->sin_port = ((const struct sockaddr_in *)faddr)->sin_port;
1697 sin->sin_addr = ((const struct sockaddr_in *)faddr)->sin_addr;
1699 so->so_faddr = (struct sockaddr *)sin;