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