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