1ae4c092cd411e7bd20500dcc084fcfaf9cebc79
[dragonfly.git] / sys / netinet / in_pcb.c
1 /*
2  * Copyright (c) 2004 Jeffrey M. Hsu.  All rights reserved.
3  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Jeffrey M. Hsu.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
31  * SUCH DAMAGE.
32  */
33
34 /*
35  * Copyright (c) 1982, 1986, 1991, 1993, 1995
36  *      The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
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.
53  *
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
64  * SUCH DAMAGE.
65  *
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 $
69  */
70
71 #include "opt_ipsec.h"
72 #include "opt_inet6.h"
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/proc.h>
83 #include <sys/priv.h>
84 #include <sys/jail.h>
85 #include <sys/kernel.h>
86 #include <sys/sysctl.h>
87 #include <sys/thread2.h>
88
89 #include <machine/limits.h>
90
91 #include <net/if.h>
92 #include <net/if_types.h>
93 #include <net/route.h>
94
95 #include <netinet/in.h>
96 #include <netinet/in_pcb.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip_var.h>
99 #ifdef INET6
100 #include <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
102 #endif /* INET6 */
103
104 #ifdef IPSEC
105 #include <netinet6/ipsec.h>
106 #include <netproto/key/key.h>
107 #endif
108
109 #ifdef FAST_IPSEC
110 #if defined(IPSEC) || defined(IPSEC_ESP)
111 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
112 #endif
113
114 #include <netproto/ipsec/ipsec.h>
115 #include <netproto/ipsec/key.h>
116 #define IPSEC
117 #endif /* FAST_IPSEC */
118
119 struct in_addr zeroin_addr;
120
121 /*
122  * These configure the range of local port addresses assigned to
123  * "unspecified" outgoing connections/packets/whatever.
124  */
125 int ipport_lowfirstauto = IPPORT_RESERVED - 1;  /* 1023 */
126 int ipport_lowlastauto = IPPORT_RESERVEDSTART;  /* 600 */
127
128 int ipport_firstauto = IPPORT_RESERVED;         /* 1024 */
129 int ipport_lastauto = IPPORT_USERRESERVED;      /* 5000 */
130
131 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO;    /* 49152 */
132 int ipport_hilastauto = IPPORT_HILASTAUTO;      /* 65535 */
133
134 #define RANGECHK(var, min, max) \
135         if ((var) < (min)) { (var) = (min); } \
136         else if ((var) > (max)) { (var) = (max); }
137
138 static int
139 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
140 {
141         int error;
142
143         error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
144         if (!error) {
145                 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
146                 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
147
148                 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
149                 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
150
151                 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
152                 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
153         }
154         return (error);
155 }
156
157 #undef RANGECHK
158
159 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
160
161 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
162            &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
163 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
164            &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
165 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
166            &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
167 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
168            &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
169 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
170            &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
171 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
172            &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
173
174 /*
175  * in_pcb.c: manage the Protocol Control Blocks.
176  *
177  * NOTE: It is assumed that most of these functions will be called from
178  * a critical section.  XXX - There are, unfortunately, a few exceptions
179  * to this rule that should be fixed.
180  *
181  * NOTE: The caller should initialize the cpu field to the cpu running the
182  * protocol stack associated with this inpcbinfo.
183  */
184
185 void
186 in_pcbinfo_init(struct inpcbinfo *pcbinfo)
187 {
188         LIST_INIT(&pcbinfo->pcblisthead);
189         pcbinfo->cpu = -1;
190 }
191
192 /*
193  * Allocate a PCB and associate it with the socket.
194  */
195 int
196 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
197 {
198         struct inpcb *inp;
199 #ifdef IPSEC
200         int error;
201 #endif
202
203         inp = kmalloc(pcbinfo->ipi_size, M_PCB, M_WAITOK|M_ZERO);
204         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
205         inp->inp_pcbinfo = inp->inp_cpcbinfo = pcbinfo;
206         inp->inp_socket = so;
207 #ifdef IPSEC
208         error = ipsec_init_policy(so, &inp->inp_sp);
209         if (error != 0) {
210                 kfree(inp, M_PCB);
211                 return (error);
212         }
213 #endif
214 #ifdef INET6
215         if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
216                 inp->inp_flags |= IN6P_IPV6_V6ONLY;
217         if (ip6_auto_flowlabel)
218                 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
219 #endif
220         so->so_pcb = inp;
221         LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
222         pcbinfo->ipi_count++;
223         return (0);
224 }
225
226 int
227 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
228 {
229         struct socket *so = inp->inp_socket;
230         struct proc *p = td->td_proc;
231         unsigned short *lastport;
232         struct sockaddr_in *sin;
233         struct sockaddr_in jsin;
234         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
235         struct ucred *cred = NULL;
236         u_short lport = 0;
237         int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
238         int error;
239
240         KKASSERT(p);
241
242         if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
243                 return (EADDRNOTAVAIL);
244         if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
245                 return (EINVAL);        /* already bound */
246         if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
247                 wild = 1;    /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
248         if (p)
249                 cred = p->p_ucred;
250         if (nam != NULL) {
251                 sin = (struct sockaddr_in *)nam;
252                 if (nam->sa_len != sizeof *sin)
253                         return (EINVAL);
254 #ifdef notdef
255                 /*
256                  * We should check the family, but old programs
257                  * incorrectly fail to initialize it.
258                  */
259                 if (sin->sin_family != AF_INET)
260                         return (EAFNOSUPPORT);
261 #endif
262                 if (!prison_replace_wildcards(td, nam))
263                                 return (EINVAL);
264                 lport = sin->sin_port;
265                 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
266                         /*
267                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
268                          * allow complete duplication of binding if
269                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
270                          * and a multicast address is bound on both
271                          * new and duplicated sockets.
272                          */
273                         if (so->so_options & SO_REUSEADDR)
274                                 reuseport = SO_REUSEADDR | SO_REUSEPORT;
275                 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
276                         sin->sin_port = 0;              /* yech... */
277                         bzero(&sin->sin_zero, sizeof sin->sin_zero);
278                         if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
279                                 return (EADDRNOTAVAIL);
280                 }
281                 if (lport != 0) {
282                         struct inpcb *t;
283
284                         /* GROSS */
285                         if (ntohs(lport) < IPPORT_RESERVED &&
286                             cred && priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))
287                                 return (EACCES);
288                         if (so->so_cred->cr_uid != 0 &&
289                             !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
290                                 t = in_pcblookup_local(inp->inp_pcbinfo,
291                                     sin->sin_addr, lport,
292                                     INPLOOKUP_WILDCARD, cred);
293                                 if (t &&
294                                     (!in_nullhost(sin->sin_addr) ||
295                                      !in_nullhost(t->inp_laddr) ||
296                                      (t->inp_socket->so_options &
297                                          SO_REUSEPORT) == 0) &&
298                                     (so->so_cred->cr_uid !=
299                                      t->inp_socket->so_cred->cr_uid)) {
300 #ifdef INET6
301                                         if (!in_nullhost(sin->sin_addr) ||
302                                             !in_nullhost(t->inp_laddr) ||
303                                             INP_SOCKAF(so) ==
304                                             INP_SOCKAF(t->inp_socket))
305 #endif
306                                         return (EADDRINUSE);
307                                 }
308                         }
309                         if (cred && !prison_replace_wildcards(td, nam))
310                                 return (EADDRNOTAVAIL);
311                         t = in_pcblookup_local(pcbinfo, sin->sin_addr, lport,
312                                                wild, cred);
313                         if (t && !(reuseport & t->inp_socket->so_options)) {
314 #ifdef INET6
315                                 if (!in_nullhost(sin->sin_addr) ||
316                                     !in_nullhost(t->inp_laddr) ||
317                                     INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
318 #endif
319                                 return (EADDRINUSE);
320                         }
321                 }
322                 inp->inp_laddr = sin->sin_addr;
323         }
324         if (lport == 0) {
325                 ushort first, last;
326                 int count;
327
328                 jsin.sin_family = AF_INET;
329                 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
330                 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
331                         inp->inp_laddr.s_addr = INADDR_ANY;
332                         return (EINVAL);
333                 }
334                 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
335
336                 inp->inp_flags |= INP_ANONPORT;
337
338                 if (inp->inp_flags & INP_HIGHPORT) {
339                         first = ipport_hifirstauto;     /* sysctl */
340                         last  = ipport_hilastauto;
341                         lastport = &pcbinfo->lasthi;
342                 } else if (inp->inp_flags & INP_LOWPORT) {
343                         if (cred &&
344                             (error = priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
345                                 inp->inp_laddr.s_addr = INADDR_ANY;
346                                 return (error);
347                         }
348                         first = ipport_lowfirstauto;    /* 1023 */
349                         last  = ipport_lowlastauto;     /* 600 */
350                         lastport = &pcbinfo->lastlow;
351                 } else {
352                         first = ipport_firstauto;       /* sysctl */
353                         last  = ipport_lastauto;
354                         lastport = &pcbinfo->lastport;
355                 }
356                 /*
357                  * Simple check to ensure all ports are not used up causing
358                  * a deadlock here.
359                  *
360                  * We split the two cases (up and down) so that the direction
361                  * is not being tested on each round of the loop.
362                  */
363                 if (first > last) {
364                         /*
365                          * counting down
366                          */
367                         count = first - last;
368
369                         do {
370                                 if (count-- < 0) {      /* completely used? */
371                                         inp->inp_laddr.s_addr = INADDR_ANY;
372                                         return (EADDRNOTAVAIL);
373                                 }
374                                 --*lastport;
375                                 if (*lastport > first || *lastport < last)
376                                         *lastport = first;
377                                 lport = htons(*lastport);
378                         } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
379                                  lport, wild, cred));
380                 } else {
381                         /*
382                          * counting up
383                          */
384                         count = last - first;
385
386                         do {
387                                 if (count-- < 0) {      /* completely used? */
388                                         inp->inp_laddr.s_addr = INADDR_ANY;
389                                         return (EADDRNOTAVAIL);
390                                 }
391                                 ++*lastport;
392                                 if (*lastport < first || *lastport > last)
393                                         *lastport = first;
394                                 lport = htons(*lastport);
395                         } while (in_pcblookup_local(pcbinfo, inp->inp_laddr,
396                                  lport, wild, cred));
397                 }
398         }
399         inp->inp_lport = lport;
400
401         jsin.sin_family = AF_INET;
402         jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
403         if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin)) {
404                 inp->inp_laddr.s_addr = INADDR_ANY;
405                 inp->inp_lport = 0;
406                 return (EINVAL);
407         }
408         inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
409
410         if (in_pcbinsporthash(inp) != 0) {
411                 inp->inp_laddr.s_addr = INADDR_ANY;
412                 inp->inp_lport = 0;
413                 return (EAGAIN);
414         }
415         return (0);
416 }
417
418 /*
419  *   Transform old in_pcbconnect() into an inner subroutine for new
420  *   in_pcbconnect(): Do some validity-checking on the remote
421  *   address (in mbuf 'nam') and then determine local host address
422  *   (i.e., which interface) to use to access that remote host.
423  *
424  *   This preserves definition of in_pcbconnect(), while supporting a
425  *   slightly different version for T/TCP.  (This is more than
426  *   a bit of a kludge, but cleaning up the internal interfaces would
427  *   have forced minor changes in every protocol).
428  */
429 int
430 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
431         struct sockaddr_in **plocal_sin, struct thread *td)
432 {
433         struct in_ifaddr *ia;
434         struct ucred *cred = NULL;
435         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
436         struct sockaddr *jsin;
437         int jailed = 0, alloc_route = 0;
438
439         if (nam->sa_len != sizeof *sin)
440                 return (EINVAL);
441         if (sin->sin_family != AF_INET)
442                 return (EAFNOSUPPORT);
443         if (sin->sin_port == 0)
444                 return (EADDRNOTAVAIL);
445         if (td && td->td_proc && td->td_proc->p_ucred)
446                 cred = td->td_proc->p_ucred;
447         if (cred && cred->cr_prison)
448                 jailed = 1;
449         if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
450                 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
451                 /*
452                  * If the destination address is INADDR_ANY,
453                  * use the primary local address.
454                  * If the supplied address is INADDR_BROADCAST,
455                  * and the primary interface supports broadcast,
456                  * choose the broadcast address for that interface.
457                  */
458                 if (sin->sin_addr.s_addr == INADDR_ANY)
459                         sin->sin_addr = IA_SIN(ia)->sin_addr;
460                 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
461                     (ia->ia_ifp->if_flags & IFF_BROADCAST))
462                         sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
463         }
464         if (inp->inp_laddr.s_addr == INADDR_ANY) {
465                 struct route *ro;
466
467                 ia = NULL;
468                 /*
469                  * If route is known or can be allocated now,
470                  * our src addr is taken from the i/f, else punt.
471                  * Note that we should check the address family of the cached
472                  * destination, in case of sharing the cache with IPv6.
473                  */
474                 ro = &inp->inp_route;
475                 if (ro->ro_rt &&
476                     (!(ro->ro_rt->rt_flags & RTF_UP) ||
477                      ro->ro_dst.sa_family != AF_INET ||
478                      satosin(&ro->ro_dst)->sin_addr.s_addr !=
479                                       sin->sin_addr.s_addr ||
480                      inp->inp_socket->so_options & SO_DONTROUTE)) {
481                         RTFREE(ro->ro_rt);
482                         ro->ro_rt = NULL;
483                 }
484                 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
485                     (ro->ro_rt == NULL ||
486                     ro->ro_rt->rt_ifp == NULL)) {
487                         /* No route yet, so try to acquire one */
488                         bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
489                         ro->ro_dst.sa_family = AF_INET;
490                         ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
491                         ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
492                                 sin->sin_addr;
493                         rtalloc(ro);
494                         alloc_route = 1;
495                 }
496                 /*
497                  * If we found a route, use the address
498                  * corresponding to the outgoing interface
499                  * unless it is the loopback (in case a route
500                  * to our address on another net goes to loopback).
501                  */
502                 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
503                         if (jailed) {
504                                 if (jailed_ip(cred->cr_prison, 
505                                     ro->ro_rt->rt_ifa->ifa_addr)) {
506                                         ia = ifatoia(ro->ro_rt->rt_ifa);
507                                 }
508                         } else {
509                                 ia = ifatoia(ro->ro_rt->rt_ifa);
510                         }
511                 }
512                 if (ia == NULL) {
513                         u_short fport = sin->sin_port;
514
515                         sin->sin_port = 0;
516                         ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
517                         if (ia && jailed && !jailed_ip(cred->cr_prison,
518                             sintosa(&ia->ia_addr)))
519                                 ia = NULL;
520                         if (ia == NULL)
521                                 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
522                         if (ia && jailed && !jailed_ip(cred->cr_prison,
523                             sintosa(&ia->ia_addr)))
524                                 ia = NULL;
525                         sin->sin_port = fport;
526                         if (ia == NULL &&
527                             !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
528                                 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
529                         if (ia && jailed && !jailed_ip(cred->cr_prison,
530                             sintosa(&ia->ia_addr)))
531                                 ia = NULL;
532
533                         if (!jailed && ia == NULL)
534                                 goto fail;
535                 }
536                 /*
537                  * If the destination address is multicast and an outgoing
538                  * interface has been set as a multicast option, use the
539                  * address of that interface as our source address.
540                  */
541                 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
542                     inp->inp_moptions != NULL) {
543                         struct ip_moptions *imo;
544                         struct ifnet *ifp;
545
546                         imo = inp->inp_moptions;
547                         if (imo->imo_multicast_ifp != NULL) {
548                                 struct in_ifaddr_container *iac;
549
550                                 ifp = imo->imo_multicast_ifp;
551                                 ia = NULL;
552                                 TAILQ_FOREACH(iac,
553                                 &in_ifaddrheads[mycpuid], ia_link) {
554                                         if (iac->ia->ia_ifp == ifp) {
555                                                 ia = iac->ia;
556                                                 break;
557                                         }
558                                 }
559                                 if (ia == NULL)
560                                         goto fail;
561                         }
562                 }
563                 /*
564                  * Don't do pcblookup call here; return interface in plocal_sin
565                  * and exit to caller, that will do the lookup.
566                  */
567                 if (ia == NULL && jailed) {
568                         if ((jsin = prison_get_nonlocal(cred->cr_prison, AF_INET, NULL)) != NULL ||
569                             (jsin = prison_get_local(cred->cr_prison, AF_INET, NULL)) != NULL) {
570                                 *plocal_sin = satosin(jsin);
571                         } else {
572                                 /* IPv6 only Jail */
573                                 goto fail;
574                         }
575                 } else {
576                         *plocal_sin = &ia->ia_addr;
577                 }
578         }
579         return (0);
580 fail:
581         if (alloc_route) {
582                 struct route *ro = &inp->inp_route;
583
584                 if (ro->ro_rt != NULL)
585                         RTFREE(ro->ro_rt);
586                 bzero(ro, sizeof(*ro));
587         }
588         return (EADDRNOTAVAIL);
589 }
590
591 /*
592  * Outer subroutine:
593  * Connect from a socket to a specified address.
594  * Both address and port must be specified in argument sin.
595  * If don't have a local address for this socket yet,
596  * then pick one.
597  */
598 int
599 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
600 {
601         struct sockaddr_in *if_sin;
602         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
603         int error;
604
605         /* Call inner routine to assign local interface address. */
606         if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
607                 return (error);
608
609         if (in_pcblookup_hash(inp->inp_cpcbinfo, sin->sin_addr, sin->sin_port,
610             inp->inp_laddr.s_addr ? inp->inp_laddr : if_sin->sin_addr,
611             inp->inp_lport, FALSE, NULL) != NULL) {
612                 return (EADDRINUSE);
613         }
614         if (inp->inp_laddr.s_addr == INADDR_ANY) {
615                 if (inp->inp_lport == 0) {
616                         error = in_pcbbind(inp, NULL, td);
617                         if (error)
618                                 return (error);
619                 }
620                 inp->inp_laddr = if_sin->sin_addr;
621         }
622         inp->inp_faddr = sin->sin_addr;
623         inp->inp_fport = sin->sin_port;
624         in_pcbinsconnhash(inp);
625         return (0);
626 }
627
628 void
629 in_pcbdisconnect(struct inpcb *inp)
630 {
631
632         inp->inp_faddr.s_addr = INADDR_ANY;
633         inp->inp_fport = 0;
634         in_pcbremconnhash(inp);
635         if (inp->inp_socket->so_state & SS_NOFDREF)
636                 in_pcbdetach(inp);
637 }
638
639 void
640 in_pcbdetach(struct inpcb *inp)
641 {
642         struct socket *so = inp->inp_socket;
643         struct inpcbinfo *ipi = inp->inp_pcbinfo;
644
645 #ifdef IPSEC
646         ipsec4_delete_pcbpolicy(inp);
647 #endif /*IPSEC*/
648         inp->inp_gencnt = ++ipi->ipi_gencnt;
649         in_pcbremlists(inp);
650         so->so_pcb = NULL;
651         sofree(so);
652         if (inp->inp_options)
653                 m_free(inp->inp_options);
654         if (inp->inp_route.ro_rt)
655                 rtfree(inp->inp_route.ro_rt);
656         ip_freemoptions(inp->inp_moptions);
657         inp->inp_vflag = 0;
658         kfree(inp, M_PCB);
659 }
660
661 /*
662  * The calling convention of in_setsockaddr() and in_setpeeraddr() was
663  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
664  * in struct pr_usrreqs, so that protocols can just reference then directly
665  * without the need for a wrapper function.  The socket must have a valid
666  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
667  * except through a kernel programming error, so it is acceptable to panic
668  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
669  * because there actually /is/ a programming error somewhere... XXX)
670  */
671 int
672 in_setsockaddr(struct socket *so, struct sockaddr **nam)
673 {
674         struct inpcb *inp;
675         struct sockaddr_in *sin;
676
677         /*
678          * Do the malloc first in case it blocks.
679          */
680         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
681                 M_WAITOK | M_ZERO);
682         sin->sin_family = AF_INET;
683         sin->sin_len = sizeof *sin;
684
685         crit_enter();
686         inp = so->so_pcb;
687         if (!inp) {
688                 crit_exit();
689                 kfree(sin, M_SONAME);
690                 return (ECONNRESET);
691         }
692         sin->sin_port = inp->inp_lport;
693         sin->sin_addr = inp->inp_laddr;
694         crit_exit();
695
696         *nam = (struct sockaddr *)sin;
697         return (0);
698 }
699
700 int
701 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
702 {
703         struct inpcb *inp;
704         struct sockaddr_in *sin;
705
706         /*
707          * Do the malloc first in case it blocks.
708          */
709         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
710                 M_WAITOK | M_ZERO);
711         sin->sin_family = AF_INET;
712         sin->sin_len = sizeof *sin;
713
714         crit_enter();
715         inp = so->so_pcb;
716         if (!inp) {
717                 crit_exit();
718                 kfree(sin, M_SONAME);
719                 return (ECONNRESET);
720         }
721         sin->sin_port = inp->inp_fport;
722         sin->sin_addr = inp->inp_faddr;
723         crit_exit();
724
725         *nam = (struct sockaddr *)sin;
726         return (0);
727 }
728
729 void
730 in_pcbnotifyall(struct inpcbhead *head, struct in_addr faddr, int err,
731                 void (*notify)(struct inpcb *, int))
732 {
733         struct inpcb *inp, *ninp;
734
735         /*
736          * note: if INP_PLACEMARKER is set we must ignore the rest of
737          * the structure and skip it.
738          */
739         crit_enter();
740         LIST_FOREACH_MUTABLE(inp, head, inp_list, ninp) {
741                 if (inp->inp_flags & INP_PLACEMARKER)
742                         continue;
743 #ifdef INET6
744                 if (!(inp->inp_vflag & INP_IPV4))
745                         continue;
746 #endif
747                 if (inp->inp_faddr.s_addr != faddr.s_addr ||
748                     inp->inp_socket == NULL)
749                         continue;
750                 (*notify)(inp, err);            /* can remove inp from list! */
751         }
752         crit_exit();
753 }
754
755 void
756 in_pcbpurgeif0(struct inpcb *head, struct ifnet *ifp)
757 {
758         struct inpcb *inp;
759         struct ip_moptions *imo;
760         int i, gap;
761
762         for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
763                 if (inp->inp_flags & INP_PLACEMARKER)
764                         continue;
765                 imo = inp->inp_moptions;
766                 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
767                         /*
768                          * Unselect the outgoing interface if it is being
769                          * detached.
770                          */
771                         if (imo->imo_multicast_ifp == ifp)
772                                 imo->imo_multicast_ifp = NULL;
773
774                         /*
775                          * Drop multicast group membership if we joined
776                          * through the interface being detached.
777                          */
778                         for (i = 0, gap = 0; i < imo->imo_num_memberships;
779                             i++) {
780                                 if (imo->imo_membership[i]->inm_ifp == ifp) {
781                                         in_delmulti(imo->imo_membership[i]);
782                                         gap++;
783                                 } else if (gap != 0)
784                                         imo->imo_membership[i - gap] =
785                                             imo->imo_membership[i];
786                         }
787                         imo->imo_num_memberships -= gap;
788                 }
789         }
790 }
791
792 /*
793  * Check for alternatives when higher level complains
794  * about service problems.  For now, invalidate cached
795  * routing information.  If the route was created dynamically
796  * (by a redirect), time to try a default gateway again.
797  */
798 void
799 in_losing(struct inpcb *inp)
800 {
801         struct rtentry *rt;
802         struct rt_addrinfo rtinfo;
803
804         if ((rt = inp->inp_route.ro_rt)) {
805                 bzero(&rtinfo, sizeof(struct rt_addrinfo));
806                 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
807                 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
808                 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
809                 rtinfo.rti_flags = rt->rt_flags;
810                 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
811                 if (rt->rt_flags & RTF_DYNAMIC)
812                         rtrequest1_global(RTM_DELETE, &rtinfo, NULL, NULL);
813                 inp->inp_route.ro_rt = NULL;
814                 rtfree(rt);
815                 /*
816                  * A new route can be allocated
817                  * the next time output is attempted.
818                  */
819         }
820 }
821
822 /*
823  * After a routing change, flush old routing
824  * and allocate a (hopefully) better one.
825  */
826 void
827 in_rtchange(struct inpcb *inp, int err)
828 {
829         if (inp->inp_route.ro_rt) {
830                 rtfree(inp->inp_route.ro_rt);
831                 inp->inp_route.ro_rt = NULL;
832                 /*
833                  * A new route can be allocated the next time
834                  * output is attempted.
835                  */
836         }
837 }
838
839 /*
840  * Lookup a PCB based on the local address and port.
841  */
842 struct inpcb *
843 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
844                    u_int lport_arg, int wild_okay, struct ucred *cred)
845 {
846         struct inpcb *inp;
847         int matchwild = 3, wildcard;
848         u_short lport = lport_arg;
849
850         struct inpcbporthead *porthash;
851         struct inpcbport *phd;
852         struct inpcb *match = NULL;
853
854         /*
855          * Best fit PCB lookup.
856          *
857          * First see if this local port is in use by looking on the
858          * port hash list.
859          */
860         porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
861             pcbinfo->porthashmask)];
862         LIST_FOREACH(phd, porthash, phd_hash) {
863                 if (phd->phd_port == lport)
864                         break;
865         }
866         if (phd != NULL) {
867                 /*
868                  * Port is in use by one or more PCBs. Look for best
869                  * fit.
870                  */
871                 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
872                         wildcard = 0;
873 #ifdef INET6
874                         if ((inp->inp_vflag & INP_IPV4) == 0)
875                                 continue;
876 #endif
877                         if (inp->inp_faddr.s_addr != INADDR_ANY)
878                                 wildcard++;
879                         if (inp->inp_laddr.s_addr != INADDR_ANY) {
880                                 if (laddr.s_addr == INADDR_ANY)
881                                         wildcard++;
882                                 else if (inp->inp_laddr.s_addr != laddr.s_addr)
883                                         continue;
884                         } else {
885                                 if (laddr.s_addr != INADDR_ANY)
886                                         wildcard++;
887                         }
888                         if (wildcard && !wild_okay)
889                                 continue;
890                         if (wildcard < matchwild &&
891                             (cred == NULL ||
892                              cred->cr_prison == 
893                                         inp->inp_socket->so_cred->cr_prison)) {
894                                 match = inp;
895                                 matchwild = wildcard;
896                                 if (matchwild == 0) {
897                                         break;
898                                 }
899                         }
900                 }
901         }
902         return (match);
903 }
904
905 /*
906  * Lookup PCB in hash list.
907  */
908 struct inpcb *
909 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr, u_int fport_arg,
910                   struct in_addr laddr, u_int lport_arg, boolean_t wildcard,
911                   struct ifnet *ifp)
912 {
913         struct inpcbhead *head;
914         struct inpcb *inp, *jinp=NULL;
915         u_short fport = fport_arg, lport = lport_arg;
916
917         /*
918          * First look for an exact match.
919          */
920         head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
921             laddr.s_addr, lport, pcbinfo->hashmask)];
922         LIST_FOREACH(inp, head, inp_hash) {
923 #ifdef INET6
924                 if (!(inp->inp_vflag & INP_IPV4))
925                         continue;
926 #endif
927                 if (in_hosteq(inp->inp_faddr, faddr) &&
928                     in_hosteq(inp->inp_laddr, laddr) &&
929                     inp->inp_fport == fport && inp->inp_lport == lport) {
930                         /* found */
931                         if (inp->inp_socket == NULL ||
932                             inp->inp_socket->so_cred->cr_prison == NULL) {
933                                 return (inp);
934                         } else {
935                                 if  (jinp == NULL)
936                                         jinp = inp;
937                         }
938                 }
939         }
940         if (jinp != NULL)
941                 return (jinp);
942         if (wildcard) {
943                 struct inpcb *local_wild = NULL;
944                 struct inpcb *jinp_wild = NULL;
945 #ifdef INET6
946                 struct inpcb *local_wild_mapped = NULL;
947 #endif
948                 struct inpcontainer *ic;
949                 struct inpcontainerhead *chead;
950                 struct sockaddr_in jsin;
951                 struct ucred *cred;
952
953                 /*
954                  * Order of socket selection:
955                  * 1. non-jailed, non-wild.
956                  * 2. non-jailed, wild.
957                  * 3. jailed, non-wild.
958                  * 4. jailed, wild.
959                  */
960                 jsin.sin_family = AF_INET;
961                 chead = &pcbinfo->wildcardhashbase[
962                     INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
963                 LIST_FOREACH(ic, chead, ic_list) {
964                         inp = ic->ic_inp;
965                         jsin.sin_addr.s_addr = laddr.s_addr;
966 #ifdef INET6
967                         if (!(inp->inp_vflag & INP_IPV4))
968                                 continue;
969 #endif
970                         if (inp->inp_socket != NULL)
971                                 cred = inp->inp_socket->so_cred;
972                         else
973                                 cred = NULL;
974                         if (cred != NULL && jailed(cred)) {
975                                 if (jinp != NULL)
976                                         continue;
977                                 else
978                                         if (!jailed_ip(cred->cr_prison,
979                                             (struct sockaddr *)&jsin))
980                                                 continue;
981                         }
982                         if (inp->inp_lport == lport) {
983                                 if (ifp && ifp->if_type == IFT_FAITH &&
984                                     !(inp->inp_flags & INP_FAITH))
985                                         continue;
986                                 if (inp->inp_laddr.s_addr == laddr.s_addr) {
987                                         if (cred != NULL && jailed(cred))
988                                                 jinp = inp;
989                                         else
990                                                 return (inp);
991                                 }
992                                 if (inp->inp_laddr.s_addr == INADDR_ANY) {
993 #ifdef INET6
994                                         if (INP_CHECK_SOCKAF(inp->inp_socket,
995                                                              AF_INET6))
996                                                 local_wild_mapped = inp;
997                                         else
998 #endif
999                                                 if (cred != NULL &&
1000                                                     jailed(cred))
1001                                                         jinp_wild = inp;
1002                                                 else
1003                                                         local_wild = inp;
1004                                 }
1005                         }
1006                 }
1007                 if (local_wild != NULL)
1008                         return (local_wild);
1009 #ifdef INET6
1010                 if (local_wild_mapped != NULL)
1011                         return (local_wild_mapped);
1012 #endif
1013                 if (jinp != NULL)
1014                         return (jinp);
1015                 return (jinp_wild);
1016         }
1017
1018         /*
1019          * Not found.
1020          */
1021         return (NULL);
1022 }
1023
1024 /*
1025  * Insert PCB into connection hash table.
1026  */
1027 void
1028 in_pcbinsconnhash(struct inpcb *inp)
1029 {
1030         struct inpcbinfo *pcbinfo = inp->inp_cpcbinfo;
1031         struct inpcbhead *bucket;
1032         u_int32_t hashkey_faddr, hashkey_laddr;
1033
1034 #ifdef INET6
1035         if (inp->inp_vflag & INP_IPV6) {
1036                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1037                 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1038         } else {
1039 #endif
1040                 hashkey_faddr = inp->inp_faddr.s_addr;
1041                 hashkey_laddr = inp->inp_laddr.s_addr;
1042 #ifdef INET6
1043         }
1044 #endif
1045
1046         KASSERT(!(inp->inp_flags & INP_CONNECTED), ("already on hash list"));
1047         inp->inp_flags |= INP_CONNECTED;
1048
1049         /*
1050          * Insert into the connection hash table.
1051          */
1052         bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1053             inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1054         LIST_INSERT_HEAD(bucket, inp, inp_hash);
1055 }
1056
1057 /*
1058  * Remove PCB from connection hash table.
1059  */
1060 void
1061 in_pcbremconnhash(struct inpcb *inp)
1062 {
1063         KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1064         LIST_REMOVE(inp, inp_hash);
1065         inp->inp_flags &= ~INP_CONNECTED;
1066 }
1067
1068 /*
1069  * Insert PCB into port hash table.
1070  */
1071 int
1072 in_pcbinsporthash(struct inpcb *inp)
1073 {
1074         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1075         struct inpcbporthead *pcbporthash;
1076         struct inpcbport *phd;
1077
1078         /*
1079          * Insert into the port hash table.
1080          */
1081         pcbporthash = &pcbinfo->porthashbase[
1082             INP_PCBPORTHASH(inp->inp_lport, pcbinfo->porthashmask)];
1083
1084         /* Go through port list and look for a head for this lport. */
1085         LIST_FOREACH(phd, pcbporthash, phd_hash)
1086                 if (phd->phd_port == inp->inp_lport)
1087                         break;
1088
1089         /* If none exists, malloc one and tack it on. */
1090         if (phd == NULL) {
1091                 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport),
1092                     M_PCB, M_INTWAIT | M_NULLOK);
1093                 if (phd == NULL)
1094                         return (ENOBUFS); /* XXX */
1095                 phd->phd_port = inp->inp_lport;
1096                 LIST_INIT(&phd->phd_pcblist);
1097                 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1098         }
1099
1100         inp->inp_phd = phd;
1101         LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1102
1103         return (0);
1104 }
1105
1106 void
1107 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1108 {
1109         struct inpcontainer *ic;
1110         struct inpcontainerhead *bucket;
1111
1112         bucket = &pcbinfo->wildcardhashbase[
1113             INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1114
1115         ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1116         ic->ic_inp = inp;
1117         LIST_INSERT_HEAD(bucket, ic, ic_list);
1118 }
1119
1120 /*
1121  * Insert PCB into wildcard hash table.
1122  */
1123 void
1124 in_pcbinswildcardhash(struct inpcb *inp)
1125 {
1126         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1127         
1128         KKASSERT(pcbinfo != NULL);
1129
1130         in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1131         inp->inp_flags |= INP_WILDCARD;
1132 }
1133
1134 void
1135 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1136 {
1137         struct inpcontainer *ic;
1138         struct inpcontainerhead *head;
1139
1140         /* find bucket */
1141         head = &pcbinfo->wildcardhashbase[
1142             INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1143
1144         LIST_FOREACH(ic, head, ic_list) {
1145                 if (ic->ic_inp == inp)
1146                         goto found;
1147         }
1148         return;                 /* not found! */
1149
1150 found:
1151         LIST_REMOVE(ic, ic_list);       /* remove container from bucket chain */
1152         kfree(ic, M_TEMP);              /* deallocate container */
1153 }
1154
1155 /*
1156  * Remove PCB from wildcard hash table.
1157  */
1158 void
1159 in_pcbremwildcardhash(struct inpcb *inp)
1160 {
1161         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1162
1163         KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1164         in_pcbremwildcardhash_oncpu(inp, pcbinfo);
1165         inp->inp_flags &= ~INP_WILDCARD;
1166 }
1167
1168 /*
1169  * Remove PCB from various lists.
1170  */
1171 void
1172 in_pcbremlists(struct inpcb *inp)
1173 {
1174         if (inp->inp_lport) {
1175                 struct inpcbport *phd = inp->inp_phd;
1176
1177                 LIST_REMOVE(inp, inp_portlist);
1178                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1179                         LIST_REMOVE(phd, phd_hash);
1180                         kfree(phd, M_PCB);
1181                 }
1182         }
1183         if (inp->inp_flags & INP_WILDCARD) {
1184                 in_pcbremwildcardhash(inp);
1185         } else if (inp->inp_flags & INP_CONNECTED) {
1186                 in_pcbremconnhash(inp);
1187         }
1188         LIST_REMOVE(inp, inp_list);
1189         inp->inp_pcbinfo->ipi_count--;
1190 }
1191
1192 int
1193 prison_xinpcb(struct thread *td, struct inpcb *inp)
1194 {
1195         struct ucred *cr;
1196
1197         if (td->td_proc == NULL)
1198                 return (0);
1199         cr = td->td_proc->p_ucred;
1200         if (cr->cr_prison == NULL)
1201                 return (0);
1202         if (inp->inp_socket && inp->inp_socket->so_cred &&
1203             inp->inp_socket->so_cred->cr_prison &&
1204             cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
1205                 return (0);
1206         return (1);
1207 }
1208
1209 int
1210 in_pcblist_global(SYSCTL_HANDLER_ARGS)
1211 {
1212         struct inpcbinfo *pcbinfo = arg1;
1213         struct inpcb *inp, *marker;
1214         struct xinpcb xi;
1215         int error, i, n;
1216         inp_gen_t gencnt;
1217
1218         /*
1219          * The process of preparing the TCB list is too time-consuming and
1220          * resource-intensive to repeat twice on every request.
1221          */
1222         if (req->oldptr == NULL) {
1223                 n = pcbinfo->ipi_count;
1224                 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
1225                 return 0;
1226         }
1227
1228         if (req->newptr != NULL)
1229                 return EPERM;
1230
1231         /*
1232          * OK, now we're committed to doing something.  Re-fetch ipi_count
1233          * after obtaining the generation count.
1234          */
1235         gencnt = pcbinfo->ipi_gencnt;
1236         n = pcbinfo->ipi_count;
1237
1238         marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
1239         marker->inp_flags |= INP_PLACEMARKER;
1240         LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1241
1242         i = 0;
1243         error = 0;
1244
1245         while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
1246                 LIST_REMOVE(marker, inp_list);
1247                 LIST_INSERT_AFTER(inp, marker, inp_list);
1248
1249                 if (inp->inp_flags & INP_PLACEMARKER)
1250                         continue;
1251                 if (inp->inp_gencnt > gencnt)
1252                         continue;
1253                 if (prison_xinpcb(req->td, inp))
1254                         continue;
1255                 bzero(&xi, sizeof xi);
1256                 xi.xi_len = sizeof xi;
1257                 bcopy(inp, &xi.xi_inp, sizeof *inp);
1258                 if (inp->inp_socket)
1259                         sotoxsocket(inp->inp_socket, &xi.xi_socket);
1260                 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
1261                         break;
1262                 ++i;
1263         }
1264         LIST_REMOVE(marker, inp_list);
1265         if (error == 0 && i < n) {
1266                 bzero(&xi, sizeof xi);
1267                 xi.xi_len = sizeof xi;
1268                 while (i < n) {
1269                         error = SYSCTL_OUT(req, &xi, sizeof xi);
1270                         ++i;
1271                 }
1272         }
1273         kfree(marker, M_TEMP);
1274         return(error);
1275 }