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