inpcb: Remove the code that does hash-threshold wildcard sockets selection
[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. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  *      @(#)in_pcb.c    8.4 (Berkeley) 5/24/95
63  * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.27 2004/01/02 04:06:42 ambrisko Exp $
64  */
65
66 #include "opt_ipsec.h"
67 #include "opt_inet6.h"
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/domain.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/proc.h>
78 #include <sys/priv.h>
79 #include <sys/jail.h>
80 #include <sys/kernel.h>
81 #include <sys/sysctl.h>
82
83 #include <sys/thread2.h>
84 #include <sys/socketvar2.h>
85 #include <sys/msgport2.h>
86
87 #include <machine/limits.h>
88
89 #include <net/if.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
92 #include <net/netisr2.h>
93
94 #include <netinet/in.h>
95 #include <netinet/in_pcb.h>
96 #include <netinet/in_var.h>
97 #include <netinet/ip_var.h>
98 #ifdef INET6
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
101 #endif /* INET6 */
102
103 #ifdef IPSEC
104 #include <netinet6/ipsec.h>
105 #include <netproto/key/key.h>
106 #include <netproto/ipsec/esp_var.h>
107 #endif
108
109 #ifdef FAST_IPSEC
110 #if defined(IPSEC) || defined(IPSEC_ESP)
111 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
112 #endif
113
114 #include <netproto/ipsec/ipsec.h>
115 #include <netproto/ipsec/key.h>
116 #define IPSEC
117 #endif /* FAST_IPSEC */
118
119 #define INP_LOCALGROUP_SIZMIN   8
120 #define INP_LOCALGROUP_SIZMAX   256
121
122 struct in_addr zeroin_addr;
123
124 /*
125  * These configure the range of local port addresses assigned to
126  * "unspecified" outgoing connections/packets/whatever.
127  */
128 int ipport_lowfirstauto = IPPORT_RESERVED - 1;  /* 1023 */
129 int ipport_lowlastauto = IPPORT_RESERVEDSTART;  /* 600 */
130
131 int ipport_firstauto = IPPORT_RESERVED;         /* 1024 */
132 int ipport_lastauto = IPPORT_USERRESERVED;      /* 5000 */
133
134 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO;    /* 49152 */
135 int ipport_hilastauto = IPPORT_HILASTAUTO;      /* 65535 */
136
137 #define RANGECHK(var, min, max) \
138         if ((var) < (min)) { (var) = (min); } \
139         else if ((var) > (max)) { (var) = (max); }
140
141 int udpencap_enable = 1;        /* enabled by default */
142 int udpencap_port = 4500;       /* triggers decapsulation */
143
144 /*
145  * Per-netisr inpcb markers.
146  * NOTE: they should only be used in netisrs.
147  */
148 static struct inpcb             *in_pcbmarkers;
149 static struct inpcontainer      *in_pcbcontainer_markers;
150
151 static int
152 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
153 {
154         int error;
155
156         error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
157         if (!error) {
158                 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
159                 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
160
161                 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
162                 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
163
164                 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
165                 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
166         }
167         return (error);
168 }
169
170 #undef RANGECHK
171
172 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
173
174 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
175            &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
176 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
177            &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
178 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
179            &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
180 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
181            &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
182 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
183            &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
184 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
185            &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
186
187 /*
188  * in_pcb.c: manage the Protocol Control Blocks.
189  *
190  * NOTE: It is assumed that most of these functions will be called from
191  * a critical section.  XXX - There are, unfortunately, a few exceptions
192  * to this rule that should be fixed.
193  *
194  * NOTE: The caller should initialize the cpu field to the cpu running the
195  * protocol stack associated with this inpcbinfo.
196  */
197
198 void
199 in_pcbinfo_init(struct inpcbinfo *pcbinfo, int cpu, boolean_t shared)
200 {
201         KASSERT(cpu >= 0 && cpu < ncpus, ("invalid cpu%d", cpu));
202         pcbinfo->cpu = cpu;
203
204         LIST_INIT(&pcbinfo->pcblisthead);
205         pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave), M_PCB,
206                                     M_WAITOK | M_ZERO);
207
208         if (shared) {
209                 pcbinfo->infotoken = kmalloc(sizeof(struct lwkt_token),
210                     M_PCB, M_WAITOK);
211                 lwkt_token_init(pcbinfo->infotoken, "infotoken");
212         } else {
213                 pcbinfo->infotoken = NULL;
214         }
215 }
216
217 struct baddynamicports baddynamicports;
218
219 /*
220  * Check if the specified port is invalid for dynamic allocation.
221  */
222 int
223 in_baddynamic(u_int16_t port, u_int16_t proto)
224 {
225         switch (proto) {
226         case IPPROTO_TCP:
227                 return (DP_ISSET(baddynamicports.tcp, port));
228         case IPPROTO_UDP:
229 #ifdef IPSEC
230                 /* Cannot preset this as it is a sysctl */
231                 if (port == udpencap_port)
232                         return (1);
233 #endif
234                 return (DP_ISSET(baddynamicports.udp, port));
235         default:
236                 return (0);
237         }
238 }
239
240 void
241 in_pcbonlist(struct inpcb *inp)
242 {
243         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
244
245         KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
246             ("not in the correct netisr"));
247         KASSERT((inp->inp_flags & INP_ONLIST) == 0, ("already on pcblist"));
248         inp->inp_flags |= INP_ONLIST;
249
250         GET_PCBINFO_TOKEN(pcbinfo);
251         LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
252         pcbinfo->ipi_count++;
253         REL_PCBINFO_TOKEN(pcbinfo);
254 }
255
256 void
257 in_pcbofflist(struct inpcb *inp)
258 {
259         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
260
261         KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
262             ("not in the correct netisr"));
263         KASSERT(inp->inp_flags & INP_ONLIST, ("not on pcblist"));
264         inp->inp_flags &= ~INP_ONLIST;
265
266         GET_PCBINFO_TOKEN(pcbinfo);
267         LIST_REMOVE(inp, inp_list);
268         KASSERT(pcbinfo->ipi_count > 0,
269             ("invalid inpcb count %d", pcbinfo->ipi_count));
270         pcbinfo->ipi_count--;
271         REL_PCBINFO_TOKEN(pcbinfo);
272 }
273
274 /*
275  * Allocate a PCB and associate it with the socket.
276  */
277 int
278 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
279 {
280         struct inpcb *inp;
281 #ifdef IPSEC
282         int error;
283 #endif
284
285         inp = kmalloc(pcbinfo->ipi_size, M_PCB, M_WAITOK|M_ZERO|M_NULLOK);
286         if (inp == NULL)
287                 return (ENOMEM);
288         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
289         inp->inp_pcbinfo = pcbinfo;
290         inp->inp_socket = so;
291 #ifdef IPSEC
292         error = ipsec_init_policy(so, &inp->inp_sp);
293         if (error != 0) {
294                 kfree(inp, M_PCB);
295                 return (error);
296         }
297 #endif
298 #ifdef INET6
299         if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
300                 inp->inp_flags |= IN6P_IPV6_V6ONLY;
301         if (ip6_auto_flowlabel)
302                 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
303 #endif
304         soreference(so);
305         so->so_pcb = inp;
306
307         in_pcbonlist(inp);
308         return (0);
309 }
310
311 /*
312  * Unlink a pcb with the intention of moving it to another cpu with a
313  * different pcbinfo.  While unlinked nothing should attempt to dereference
314  * inp_pcbinfo, NULL it out so we assert if it does.
315  */
316 void
317 in_pcbunlink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
318 {
319         KASSERT(inp->inp_pcbinfo == pcbinfo, ("pcbinfo mismatch"));
320         KASSERT((inp->inp_flags & (INP_WILDCARD | INP_CONNECTED)) == 0,
321             ("already linked"));
322
323         in_pcbofflist(inp);
324         inp->inp_pcbinfo = NULL;
325 }
326
327 /*
328  * Relink a pcb into a new pcbinfo.
329  */
330 void
331 in_pcblink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
332 {
333         KASSERT(inp->inp_pcbinfo == NULL, ("has pcbinfo"));
334         KASSERT((inp->inp_flags & (INP_WILDCARD | INP_CONNECTED)) == 0,
335             ("already linked"));
336
337         inp->inp_pcbinfo = pcbinfo;
338         in_pcbonlist(inp);
339 }
340
341 static int
342 in_pcbsetlport(struct inpcb *inp, int wild, struct ucred *cred)
343 {
344         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
345         struct inpcbportinfo *portinfo;
346         u_short first, last, lport, step;
347         u_short *lastport;
348         int count, error;
349         int portinfo_first, portinfo_idx;
350
351         inp->inp_flags |= INP_ANONPORT;
352
353         step = pcbinfo->portinfo_mask + 1;
354         portinfo_first = mycpuid & pcbinfo->portinfo_mask;
355         portinfo_idx = portinfo_first;
356 loop:
357         portinfo = &pcbinfo->portinfo[portinfo_idx];
358
359         if (inp->inp_flags & INP_HIGHPORT) {
360                 first = ipport_hifirstauto;     /* sysctl */
361                 last  = ipport_hilastauto;
362                 lastport = &portinfo->lasthi;
363         } else if (inp->inp_flags & INP_LOWPORT) {
364                 if (cred &&
365                     (error =
366                      priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
367                         inp->inp_laddr.s_addr = INADDR_ANY;
368                         return error;
369                 }
370                 first = ipport_lowfirstauto;    /* 1023 */
371                 last  = ipport_lowlastauto;     /* 600 */
372                 lastport = &portinfo->lastlow;
373         } else {
374                 first = ipport_firstauto;       /* sysctl */
375                 last  = ipport_lastauto;
376                 lastport = &portinfo->lastport;
377         }
378
379         /*
380          * This has to be atomic.  If the porthash is shared across multiple
381          * protocol threads (aka tcp) then the token must be held.
382          */
383         GET_PORT_TOKEN(portinfo);
384
385         /*
386          * Simple check to ensure all ports are not used up causing
387          * a deadlock here.
388          *
389          * We split the two cases (up and down) so that the direction
390          * is not being tested on each round of the loop.
391          */
392         if (first > last) {
393                 /*
394                  * counting down
395                  */
396                 in_pcbportrange(&first, &last, portinfo->offset, step);
397                 count = (first - last) / step;
398
399                 do {
400                         if (count-- < 0) {      /* completely used? */
401                                 error = EADDRNOTAVAIL;
402                                 goto done;
403                         }
404                         *lastport -= step;
405                         if (*lastport > first || *lastport < last)
406                                 *lastport = first;
407                         KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
408                             portinfo->offset);
409                         lport = htons(*lastport);
410                 } while (in_pcblookup_local(portinfo, inp->inp_laddr, lport,
411                     wild, cred));
412         } else {
413                 /*
414                  * counting up
415                  */
416                 in_pcbportrange(&last, &first, portinfo->offset, step);
417                 count = (last - first) / step;
418
419                 do {
420                         if (count-- < 0) {      /* completely used? */
421                                 error = EADDRNOTAVAIL;
422                                 goto done;
423                         }
424                         *lastport += step;
425                         if (*lastport < first || *lastport > last)
426                                 *lastport = first;
427                         KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
428                             portinfo->offset);
429                         lport = htons(*lastport);
430                 } while (in_pcblookup_local(portinfo, inp->inp_laddr, lport,
431                     wild, cred));
432         }
433         inp->inp_lport = lport;
434         in_pcbinsporthash(portinfo, inp);
435         error = 0;
436 done:
437         REL_PORT_TOKEN(portinfo);
438
439         if (error) {
440                 /* Try next portinfo */
441                 portinfo_idx++;
442                 portinfo_idx &= pcbinfo->portinfo_mask;
443                 if (portinfo_idx != portinfo_first)
444                         goto loop;
445                 inp->inp_laddr.s_addr = INADDR_ANY;
446         }
447         return error;
448 }
449
450 int
451 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
452 {
453         struct socket *so = inp->inp_socket;
454         struct sockaddr_in jsin;
455         struct ucred *cred = NULL;
456         int wild = 0;
457
458         if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
459                 return (EADDRNOTAVAIL);
460         if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
461                 return (EINVAL);        /* already bound */
462
463         if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
464                 wild = 1;    /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
465         if (td->td_proc)
466                 cred = td->td_proc->p_ucred;
467
468         if (nam != NULL) {
469                 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
470                 struct inpcbinfo *pcbinfo;
471                 struct inpcbportinfo *portinfo;
472                 struct inpcb *t;
473                 u_short lport, lport_ho;
474                 int reuseport = (so->so_options & SO_REUSEPORT);
475                 int error;
476
477                 if (nam->sa_len != sizeof *sin)
478                         return (EINVAL);
479 #ifdef notdef
480                 /*
481                  * We should check the family, but old programs
482                  * incorrectly fail to initialize it.
483                  */
484                 if (sin->sin_family != AF_INET)
485                         return (EAFNOSUPPORT);
486 #endif
487                 if (!prison_replace_wildcards(td, nam))
488                         return (EINVAL);
489
490                 lport = sin->sin_port;
491                 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
492                         /*
493                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
494                          * allow complete duplication of binding if
495                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
496                          * and a multicast address is bound on both
497                          * new and duplicated sockets.
498                          */
499                         if (so->so_options & SO_REUSEADDR)
500                                 reuseport = SO_REUSEADDR | SO_REUSEPORT;
501                 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
502                         sin->sin_port = 0;              /* yech... */
503                         bzero(&sin->sin_zero, sizeof sin->sin_zero);
504                         if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
505                                 return (EADDRNOTAVAIL);
506                 }
507
508                 inp->inp_laddr = sin->sin_addr;
509
510                 jsin.sin_family = AF_INET;
511                 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
512                 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
513                         inp->inp_laddr.s_addr = INADDR_ANY;
514                         return (EINVAL);
515                 }
516                 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
517
518                 if (lport == 0) {
519                         /* Auto-select local port */
520                         return in_pcbsetlport(inp, wild, cred);
521                 }
522                 lport_ho = ntohs(lport);
523
524                 /* GROSS */
525                 if (lport_ho < IPPORT_RESERVED && cred &&
526                     (error =
527                      priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
528                         inp->inp_laddr.s_addr = INADDR_ANY;
529                         return (error);
530                 }
531
532                 /*
533                  * Locate the proper portinfo based on lport
534                  */
535                 pcbinfo = inp->inp_pcbinfo;
536                 portinfo =
537                     &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
538                 KKASSERT((lport_ho & pcbinfo->portinfo_mask) ==
539                     portinfo->offset);
540
541                 /*
542                  * This has to be atomic.  If the porthash is shared across
543                  * multiple protocol threads (aka tcp) then the token must
544                  * be held.
545                  */
546                 GET_PORT_TOKEN(portinfo);
547
548                 if (so->so_cred->cr_uid != 0 &&
549                     !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
550                         t = in_pcblookup_local(portinfo, sin->sin_addr, lport,
551                             INPLOOKUP_WILDCARD, cred);
552                         if (t &&
553                             (!in_nullhost(sin->sin_addr) ||
554                              !in_nullhost(t->inp_laddr) ||
555                              (t->inp_socket->so_options & SO_REUSEPORT) == 0) &&
556                             (so->so_cred->cr_uid !=
557                              t->inp_socket->so_cred->cr_uid)) {
558 #ifdef INET6
559                                 if (!in_nullhost(sin->sin_addr) ||
560                                     !in_nullhost(t->inp_laddr) ||
561                                     INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
562 #endif
563                                 {
564                                         inp->inp_laddr.s_addr = INADDR_ANY;
565                                         error = EADDRINUSE;
566                                         goto done;
567                                 }
568                         }
569                 }
570                 if (cred && !prison_replace_wildcards(td, nam)) {
571                         inp->inp_laddr.s_addr = INADDR_ANY;
572                         error = EADDRNOTAVAIL;
573                         goto done;
574                 }
575                 t = in_pcblookup_local(portinfo, sin->sin_addr, lport,
576                     wild, cred);
577                 if (t && !(reuseport & t->inp_socket->so_options)) {
578 #ifdef INET6
579                         if (!in_nullhost(sin->sin_addr) ||
580                             !in_nullhost(t->inp_laddr) ||
581                             INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
582 #endif
583                         {
584                                 inp->inp_laddr.s_addr = INADDR_ANY;
585                                 error = EADDRINUSE;
586                                 goto done;
587                         }
588                 }
589                 inp->inp_lport = lport;
590                 in_pcbinsporthash(portinfo, inp);
591                 error = 0;
592 done:
593                 REL_PORT_TOKEN(portinfo);
594                 return (error);
595         } else {
596                 jsin.sin_family = AF_INET;
597                 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
598                 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
599                         inp->inp_laddr.s_addr = INADDR_ANY;
600                         return (EINVAL);
601                 }
602                 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
603
604                 return in_pcbsetlport(inp, wild, cred);
605         }
606 }
607
608 static struct inpcb *
609 in_pcblookup_localremote(struct inpcbportinfo *portinfo, struct in_addr laddr,
610     u_short lport, struct in_addr faddr, u_short fport, struct ucred *cred)
611 {
612         struct inpcb *inp;
613         struct inpcbporthead *porthash;
614         struct inpcbport *phd;
615         struct inpcb *match = NULL;
616
617         /*
618          * If the porthashbase is shared across several cpus, it must
619          * have been locked.
620          */
621         ASSERT_PORT_TOKEN_HELD(portinfo);
622
623         /*
624          * Best fit PCB lookup.
625          *
626          * First see if this local port is in use by looking on the
627          * port hash list.
628          */
629         porthash = &portinfo->porthashbase[
630                         INP_PCBPORTHASH(lport, portinfo->porthashmask)];
631         LIST_FOREACH(phd, porthash, phd_hash) {
632                 if (phd->phd_port == lport)
633                         break;
634         }
635         if (phd != NULL) {
636                 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
637 #ifdef INET6
638                         if ((inp->inp_vflag & INP_IPV4) == 0)
639                                 continue;
640 #endif
641                         if (inp->inp_laddr.s_addr != INADDR_ANY &&
642                             inp->inp_laddr.s_addr != laddr.s_addr)
643                                 continue;
644
645                         if (inp->inp_faddr.s_addr != INADDR_ANY &&
646                             inp->inp_faddr.s_addr != faddr.s_addr)
647                                 continue;
648
649                         if (inp->inp_fport != 0 && inp->inp_fport != fport)
650                                 continue;
651
652                         if (cred == NULL ||
653                             cred->cr_prison ==
654                             inp->inp_socket->so_cred->cr_prison) {
655                                 match = inp;
656                                 break;
657                         }
658                 }
659         }
660         return (match);
661 }
662
663 int
664 in_pcbbind_remote(struct inpcb *inp, const struct sockaddr *remote,
665     struct thread *td)
666 {
667         struct proc *p = td->td_proc;
668         unsigned short *lastport;
669         const struct sockaddr_in *sin = (const struct sockaddr_in *)remote;
670         struct sockaddr_in jsin;
671         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
672         struct inpcbportinfo *portinfo;
673         struct ucred *cred = NULL;
674         u_short first, last, lport, step;
675         int count, error, dup;
676         int portinfo_first, portinfo_idx;
677
678         if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
679                 return (EADDRNOTAVAIL);
680
681         KKASSERT(inp->inp_laddr.s_addr != INADDR_ANY);
682         if (inp->inp_lport != 0)
683                 return (EINVAL);        /* already bound */
684
685         KKASSERT(p);
686         cred = p->p_ucred;
687
688         jsin.sin_family = AF_INET;
689         jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
690         if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
691                 inp->inp_laddr.s_addr = INADDR_ANY;
692                 return (EINVAL);
693         }
694         inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
695
696         inp->inp_flags |= INP_ANONPORT;
697
698         step = pcbinfo->portinfo_mask + 1;
699         portinfo_first = mycpuid & pcbinfo->portinfo_mask;
700         portinfo_idx = portinfo_first;
701 loop:
702         portinfo = &pcbinfo->portinfo[portinfo_idx];
703         dup = 0;
704
705         if (inp->inp_flags & INP_HIGHPORT) {
706                 first = ipport_hifirstauto;     /* sysctl */
707                 last  = ipport_hilastauto;
708                 lastport = &portinfo->lasthi;
709         } else if (inp->inp_flags & INP_LOWPORT) {
710                 if (cred &&
711                     (error =
712                      priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
713                         inp->inp_laddr.s_addr = INADDR_ANY;
714                         return (error);
715                 }
716                 first = ipport_lowfirstauto;    /* 1023 */
717                 last  = ipport_lowlastauto;     /* 600 */
718                 lastport = &portinfo->lastlow;
719         } else {
720                 first = ipport_firstauto;       /* sysctl */
721                 last  = ipport_lastauto;
722                 lastport = &portinfo->lastport;
723         }
724
725         /*
726          * This has to be atomic.  If the porthash is shared across multiple
727          * protocol threads (aka tcp) then the token must be held.
728          */
729         GET_PORT_TOKEN(portinfo);
730
731 again:
732         /*
733          * Simple check to ensure all ports are not used up causing
734          * a deadlock here.
735          *
736          * We split the two cases (up and down) so that the direction
737          * is not being tested on each round of the loop.
738          */
739         if (first > last) {
740                 /*
741                  * counting down
742                  */
743                 in_pcbportrange(&first, &last, portinfo->offset, step);
744                 count = (first - last) / step;
745
746                 do {
747                         if (count-- < 0) {      /* completely used? */
748                                 error = EADDRNOTAVAIL;
749                                 goto done;
750                         }
751                         *lastport -= step;
752                         if (*lastport > first || *lastport < last)
753                                 *lastport = first;
754                         KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
755                             portinfo->offset);
756                         lport = htons(*lastport);
757                 } while (in_pcblookup_localremote(portinfo, inp->inp_laddr,
758                     lport, sin->sin_addr, sin->sin_port, cred));
759         } else {
760                 /*
761                  * counting up
762                  */
763                 in_pcbportrange(&last, &first, portinfo->offset, step);
764                 count = (last - first) / step;
765
766                 do {
767                         if (count-- < 0) {      /* completely used? */
768                                 error = EADDRNOTAVAIL;
769                                 goto done;
770                         }
771                         *lastport += step;
772                         if (*lastport < first || *lastport > last)
773                                 *lastport = first;
774                         KKASSERT((*lastport & pcbinfo->portinfo_mask) ==
775                             portinfo->offset);
776                         lport = htons(*lastport);
777                 } while (in_pcblookup_localremote(portinfo, inp->inp_laddr,
778                     lport, sin->sin_addr, sin->sin_port, cred));
779         }
780
781         /* This could happen on loopback interface */
782         if (sin->sin_port == lport &&
783             sin->sin_addr.s_addr == inp->inp_laddr.s_addr) {
784                 if (dup) {
785                         /*
786                          * Duplicate again; give up
787                          */
788                         error = EADDRNOTAVAIL;
789                         goto done;
790                 }
791                 dup = 1;
792                 goto again;
793         }
794         inp->inp_lport = lport;
795         in_pcbinsporthash(portinfo, inp);
796         error = 0;
797 done:
798         REL_PORT_TOKEN(portinfo);
799
800         if (error) {
801                 /* Try next portinfo */
802                 portinfo_idx++;
803                 portinfo_idx &= pcbinfo->portinfo_mask;
804                 if (portinfo_idx != portinfo_first)
805                         goto loop;
806                 inp->inp_laddr.s_addr = INADDR_ANY;
807         }
808         return error;
809 }
810
811 /*
812  *   Transform old in_pcbconnect() into an inner subroutine for new
813  *   in_pcbconnect(): Do some validity-checking on the remote
814  *   address (in mbuf 'nam') and then determine local host address
815  *   (i.e., which interface) to use to access that remote host.
816  *
817  *   This preserves definition of in_pcbconnect(), while supporting a
818  *   slightly different version for T/TCP.  (This is more than
819  *   a bit of a kludge, but cleaning up the internal interfaces would
820  *   have forced minor changes in every protocol).
821  */
822 int
823 in_pcbladdr_find(struct inpcb *inp, struct sockaddr *nam,
824     struct sockaddr_in **plocal_sin, struct thread *td, int find)
825 {
826         struct in_ifaddr *ia;
827         struct ucred *cred = NULL;
828         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
829         struct sockaddr *jsin;
830         int jailed = 0, alloc_route = 0;
831
832         if (nam->sa_len != sizeof *sin)
833                 return (EINVAL);
834         if (sin->sin_family != AF_INET)
835                 return (EAFNOSUPPORT);
836         if (sin->sin_port == 0)
837                 return (EADDRNOTAVAIL);
838         if (td && td->td_proc && td->td_proc->p_ucred)
839                 cred = td->td_proc->p_ucred;
840         if (cred && cred->cr_prison)
841                 jailed = 1;
842         if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
843                 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
844                 /*
845                  * If the destination address is INADDR_ANY,
846                  * use the primary local address.
847                  * If the supplied address is INADDR_BROADCAST,
848                  * and the primary interface supports broadcast,
849                  * choose the broadcast address for that interface.
850                  */
851                 if (sin->sin_addr.s_addr == INADDR_ANY)
852                         sin->sin_addr = IA_SIN(ia)->sin_addr;
853                 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
854                     (ia->ia_ifp->if_flags & IFF_BROADCAST))
855                         sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
856         }
857         if (find) {
858                 struct route *ro;
859
860                 ia = NULL;
861                 /*
862                  * If route is known or can be allocated now,
863                  * our src addr is taken from the i/f, else punt.
864                  * Note that we should check the address family of the cached
865                  * destination, in case of sharing the cache with IPv6.
866                  */
867                 ro = &inp->inp_route;
868                 if (ro->ro_rt &&
869                     (!(ro->ro_rt->rt_flags & RTF_UP) ||
870                      ro->ro_dst.sa_family != AF_INET ||
871                      satosin(&ro->ro_dst)->sin_addr.s_addr !=
872                                       sin->sin_addr.s_addr ||
873                      inp->inp_socket->so_options & SO_DONTROUTE)) {
874                         RTFREE(ro->ro_rt);
875                         ro->ro_rt = NULL;
876                 }
877                 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
878                     (ro->ro_rt == NULL ||
879                     ro->ro_rt->rt_ifp == NULL)) {
880                         /* No route yet, so try to acquire one */
881                         bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
882                         ro->ro_dst.sa_family = AF_INET;
883                         ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
884                         ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
885                                 sin->sin_addr;
886                         rtalloc(ro);
887                         alloc_route = 1;
888                 }
889                 /*
890                  * If we found a route, use the address
891                  * corresponding to the outgoing interface
892                  * unless it is the loopback (in case a route
893                  * to our address on another net goes to loopback).
894                  */
895                 if (ro->ro_rt &&
896                     !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
897                         if (jailed) {
898                                 if (jailed_ip(cred->cr_prison, 
899                                     ro->ro_rt->rt_ifa->ifa_addr)) {
900                                         ia = ifatoia(ro->ro_rt->rt_ifa);
901                                 }
902                         } else {
903                                 ia = ifatoia(ro->ro_rt->rt_ifa);
904                         }
905                 }
906                 if (ia == NULL) {
907                         u_short fport = sin->sin_port;
908
909                         sin->sin_port = 0;
910                         ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
911                         if (ia && jailed && !jailed_ip(cred->cr_prison,
912                             sintosa(&ia->ia_addr)))
913                                 ia = NULL;
914                         if (ia == NULL)
915                                 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
916                         if (ia && jailed && !jailed_ip(cred->cr_prison,
917                             sintosa(&ia->ia_addr)))
918                                 ia = NULL;
919                         sin->sin_port = fport;
920                         if (ia == NULL &&
921                             !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
922                                 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
923                         if (ia && jailed && !jailed_ip(cred->cr_prison,
924                             sintosa(&ia->ia_addr)))
925                                 ia = NULL;
926
927                         if (!jailed && ia == NULL)
928                                 goto fail;
929                 }
930                 /*
931                  * If the destination address is multicast and an outgoing
932                  * interface has been set as a multicast option, use the
933                  * address of that interface as our source address.
934                  */
935                 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
936                     inp->inp_moptions != NULL) {
937                         struct ip_moptions *imo;
938                         struct ifnet *ifp;
939
940                         imo = inp->inp_moptions;
941                         if (imo->imo_multicast_ifp != NULL) {
942                                 struct in_ifaddr_container *iac;
943
944                                 ifp = imo->imo_multicast_ifp;
945                                 ia = NULL;
946                                 TAILQ_FOREACH(iac,
947                                 &in_ifaddrheads[mycpuid], ia_link) {
948                                         if (iac->ia->ia_ifp == ifp) {
949                                                 ia = iac->ia;
950                                                 break;
951                                         }
952                                 }
953                                 if (ia == NULL)
954                                         goto fail;
955                         }
956                 }
957                 /*
958                  * Don't do pcblookup call here; return interface in plocal_sin
959                  * and exit to caller, that will do the lookup.
960                  */
961                 if (ia == NULL && jailed) {
962                         if ((jsin = prison_get_nonlocal(
963                                 cred->cr_prison, AF_INET, NULL)) != NULL ||
964                             (jsin = prison_get_local(
965                                 cred->cr_prison, AF_INET, NULL)) != NULL) {
966                                 *plocal_sin = satosin(jsin);
967                         } else {
968                                 /* IPv6 only Jail */
969                                 goto fail;
970                         }
971                 } else {
972                         *plocal_sin = &ia->ia_addr;
973                 }
974         }
975         return (0);
976 fail:
977         if (alloc_route) {
978                 struct route *ro = &inp->inp_route;
979
980                 if (ro->ro_rt != NULL)
981                         RTFREE(ro->ro_rt);
982                 bzero(ro, sizeof(*ro));
983         }
984         return (EADDRNOTAVAIL);
985 }
986
987 int
988 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
989     struct sockaddr_in **plocal_sin, struct thread *td)
990 {
991         return in_pcbladdr_find(inp, nam, plocal_sin, td,
992             (inp->inp_laddr.s_addr == INADDR_ANY));
993 }
994
995 /*
996  * Outer subroutine:
997  * Connect from a socket to a specified address.
998  * Both address and port must be specified in argument sin.
999  * If don't have a local address for this socket yet,
1000  * then pick one.
1001  */
1002 int
1003 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
1004 {
1005         struct sockaddr_in *if_sin;
1006         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1007         int error;
1008
1009         /* Call inner routine to assign local interface address. */
1010         if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
1011                 return (error);
1012
1013         if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
1014                               inp->inp_laddr.s_addr ?
1015                                 inp->inp_laddr : if_sin->sin_addr,
1016                               inp->inp_lport, FALSE, NULL) != NULL) {
1017                 return (EADDRINUSE);
1018         }
1019         if (inp->inp_laddr.s_addr == INADDR_ANY) {
1020                 if (inp->inp_lport == 0) {
1021                         error = in_pcbbind(inp, NULL, td);
1022                         if (error)
1023                                 return (error);
1024                 }
1025                 inp->inp_laddr = if_sin->sin_addr;
1026         }
1027         inp->inp_faddr = sin->sin_addr;
1028         inp->inp_fport = sin->sin_port;
1029         in_pcbinsconnhash(inp);
1030         return (0);
1031 }
1032
1033 void
1034 in_pcbdisconnect(struct inpcb *inp)
1035 {
1036
1037         in_pcbremconnhash(inp);
1038         inp->inp_faddr.s_addr = INADDR_ANY;
1039         inp->inp_fport = 0;
1040 }
1041
1042 void
1043 in_pcbdetach(struct inpcb *inp)
1044 {
1045         struct socket *so = inp->inp_socket;
1046         struct inpcbinfo *ipi = inp->inp_pcbinfo;
1047
1048 #ifdef IPSEC
1049         ipsec4_delete_pcbpolicy(inp);
1050 #endif /*IPSEC*/
1051         inp->inp_gencnt = ++ipi->ipi_gencnt;
1052         KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
1053         in_pcbremlists(inp);
1054         so->so_pcb = NULL;
1055         sofree(so);                     /* remove pcb ref */
1056         if (inp->inp_options)
1057                 m_free(inp->inp_options);
1058         if (inp->inp_route.ro_rt)
1059                 rtfree(inp->inp_route.ro_rt);
1060         ip_freemoptions(inp->inp_moptions);
1061         inp->inp_vflag = 0;
1062         kfree(inp, M_PCB);
1063 }
1064
1065 /*
1066  * The calling convention of in_setsockaddr() and in_setpeeraddr() was
1067  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
1068  * in struct pr_usrreqs, so that protocols can just reference then directly
1069  * without the need for a wrapper function.  The socket must have a valid
1070  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
1071  * except through a kernel programming error, so it is acceptable to panic
1072  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
1073  * because there actually /is/ a programming error somewhere... XXX)
1074  */
1075 int
1076 in_setsockaddr(struct socket *so, struct sockaddr **nam)
1077 {
1078         struct inpcb *inp;
1079         struct sockaddr_in *sin;
1080
1081         /*
1082          * Do the malloc first in case it blocks.
1083          */
1084         sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1085         sin->sin_family = AF_INET;
1086         sin->sin_len = sizeof *sin;
1087
1088         crit_enter();
1089         inp = so->so_pcb;
1090         if (!inp) {
1091                 crit_exit();
1092                 kfree(sin, M_SONAME);
1093                 return (ECONNRESET);
1094         }
1095         sin->sin_port = inp->inp_lport;
1096         sin->sin_addr = inp->inp_laddr;
1097         crit_exit();
1098
1099         *nam = (struct sockaddr *)sin;
1100         return (0);
1101 }
1102
1103 void
1104 in_setsockaddr_dispatch(netmsg_t msg)
1105 {
1106         int error;
1107
1108         error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1109         lwkt_replymsg(&msg->lmsg, error);
1110 }
1111
1112 int
1113 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
1114 {
1115         struct inpcb *inp;
1116         struct sockaddr_in *sin;
1117
1118         /*
1119          * Do the malloc first in case it blocks.
1120          */
1121         sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1122         sin->sin_family = AF_INET;
1123         sin->sin_len = sizeof *sin;
1124
1125         crit_enter();
1126         inp = so->so_pcb;
1127         if (!inp) {
1128                 crit_exit();
1129                 kfree(sin, M_SONAME);
1130                 return (ECONNRESET);
1131         }
1132         sin->sin_port = inp->inp_fport;
1133         sin->sin_addr = inp->inp_faddr;
1134         crit_exit();
1135
1136         *nam = (struct sockaddr *)sin;
1137         return (0);
1138 }
1139
1140 void
1141 in_setpeeraddr_dispatch(netmsg_t msg)
1142 {
1143         int error;
1144
1145         error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1146         lwkt_replymsg(&msg->lmsg, error);
1147 }
1148
1149 void
1150 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int err,
1151                 void (*notify)(struct inpcb *, int))
1152 {
1153         struct inpcb *inp, *marker;
1154
1155         KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1156             ("not in the correct netisr"));
1157         marker = &in_pcbmarkers[mycpuid];
1158
1159         /*
1160          * NOTE:
1161          * - If INP_PLACEMARKER is set we must ignore the rest of the
1162          *   structure and skip it.
1163          * - It is safe to nuke inpcbs here, since we are in their own
1164          *   netisr.
1165          */
1166         GET_PCBINFO_TOKEN(pcbinfo);
1167
1168         LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1169         while ((inp = LIST_NEXT(marker, inp_list)) != NULL) {
1170                 LIST_REMOVE(marker, inp_list);
1171                 LIST_INSERT_AFTER(inp, marker, inp_list);
1172
1173                 if (inp->inp_flags & INP_PLACEMARKER)
1174                         continue;
1175 #ifdef INET6
1176                 if (!(inp->inp_vflag & INP_IPV4))
1177                         continue;
1178 #endif
1179                 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1180                     inp->inp_socket == NULL)
1181                         continue;
1182                 (*notify)(inp, err);            /* can remove inp from list! */
1183         }
1184         LIST_REMOVE(marker, inp_list);
1185
1186         REL_PCBINFO_TOKEN(pcbinfo);
1187 }
1188
1189 void
1190 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1191 {
1192         struct inpcb *inp, *marker;
1193
1194         /*
1195          * We only need to make sure that we are in netisr0, where all
1196          * multicast operation happen.  We could check inpcbinfo which
1197          * does not belong to netisr0 by holding the inpcbinfo's token.
1198          * In this case, the pcbinfo must be able to be shared, i.e.
1199          * pcbinfo->infotoken is not NULL.
1200          */
1201         KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1202             ("not in netisr0"));
1203         KASSERT(pcbinfo->cpu == 0 || pcbinfo->infotoken != NULL,
1204             ("pcbinfo could not be shared"));
1205
1206         /*
1207          * Get a marker for the current netisr (netisr0).
1208          *
1209          * It is possible that the multicast address deletion blocks,
1210          * which could cause temporary token releasing.  So we use
1211          * inpcb marker here to get a coherent view of the inpcb list.
1212          *
1213          * While, on the other hand, moptions are only added and deleted
1214          * in netisr0, so we would not see staled moption or miss moption
1215          * even if the token was released due to the blocking multicast
1216          * address deletion.
1217          */
1218         marker = &in_pcbmarkers[mycpuid];
1219
1220         GET_PCBINFO_TOKEN(pcbinfo);
1221
1222         LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1223         while ((inp = LIST_NEXT(marker, inp_list)) != NULL) {
1224                 struct ip_moptions *imo;
1225
1226                 LIST_REMOVE(marker, inp_list);
1227                 LIST_INSERT_AFTER(inp, marker, inp_list);
1228
1229                 if (inp->inp_flags & INP_PLACEMARKER)
1230                         continue;
1231                 imo = inp->inp_moptions;
1232                 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
1233                         int i, gap;
1234
1235                         /*
1236                          * Unselect the outgoing interface if it is being
1237                          * detached.
1238                          */
1239                         if (imo->imo_multicast_ifp == ifp)
1240                                 imo->imo_multicast_ifp = NULL;
1241
1242                         /*
1243                          * Drop multicast group membership if we joined
1244                          * through the interface being detached.
1245                          */
1246                         for (i = 0, gap = 0; i < imo->imo_num_memberships;
1247                             i++) {
1248                                 if (imo->imo_membership[i]->inm_ifp == ifp) {
1249                                         /*
1250                                          * NOTE:
1251                                          * This could block and the pcbinfo
1252                                          * token could be passively released.
1253                                          */
1254                                         in_delmulti(imo->imo_membership[i]);
1255                                         gap++;
1256                                 } else if (gap != 0)
1257                                         imo->imo_membership[i - gap] =
1258                                             imo->imo_membership[i];
1259                         }
1260                         imo->imo_num_memberships -= gap;
1261                 }
1262         }
1263         LIST_REMOVE(marker, inp_list);
1264
1265         REL_PCBINFO_TOKEN(pcbinfo);
1266 }
1267
1268 /*
1269  * Check for alternatives when higher level complains
1270  * about service problems.  For now, invalidate cached
1271  * routing information.  If the route was created dynamically
1272  * (by a redirect), time to try a default gateway again.
1273  */
1274 void
1275 in_losing(struct inpcb *inp)
1276 {
1277         struct rtentry *rt;
1278         struct rt_addrinfo rtinfo;
1279
1280         if ((rt = inp->inp_route.ro_rt)) {
1281                 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1282                 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1283                 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1284                 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1285                 rtinfo.rti_flags = rt->rt_flags;
1286                 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1287                 if (rt->rt_flags & RTF_DYNAMIC) {
1288                         rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1289                             rt_mask(rt), rt->rt_flags, NULL);
1290                 }
1291                 inp->inp_route.ro_rt = NULL;
1292                 rtfree(rt);
1293                 /*
1294                  * A new route can be allocated
1295                  * the next time output is attempted.
1296                  */
1297         }
1298 }
1299
1300 /*
1301  * After a routing change, flush old routing
1302  * and allocate a (hopefully) better one.
1303  */
1304 void
1305 in_rtchange(struct inpcb *inp, int err)
1306 {
1307         if (inp->inp_route.ro_rt) {
1308                 rtfree(inp->inp_route.ro_rt);
1309                 inp->inp_route.ro_rt = NULL;
1310                 /*
1311                  * A new route can be allocated the next time
1312                  * output is attempted.
1313                  */
1314         }
1315 }
1316
1317 /*
1318  * Lookup a PCB based on the local address and port.
1319  */
1320 struct inpcb *
1321 in_pcblookup_local(struct inpcbportinfo *portinfo, struct in_addr laddr,
1322                    u_int lport_arg, int wild_okay, struct ucred *cred)
1323 {
1324         struct inpcb *inp;
1325         int matchwild = 3, wildcard;
1326         u_short lport = lport_arg;
1327         struct inpcbporthead *porthash;
1328         struct inpcbport *phd;
1329         struct inpcb *match = NULL;
1330
1331         /*
1332          * If the porthashbase is shared across several cpus, it must
1333          * have been locked.
1334          */
1335         ASSERT_PORT_TOKEN_HELD(portinfo);
1336
1337         /*
1338          * Best fit PCB lookup.
1339          *
1340          * First see if this local port is in use by looking on the
1341          * port hash list.
1342          */
1343         porthash = &portinfo->porthashbase[
1344                         INP_PCBPORTHASH(lport, portinfo->porthashmask)];
1345         LIST_FOREACH(phd, porthash, phd_hash) {
1346                 if (phd->phd_port == lport)
1347                         break;
1348         }
1349         if (phd != NULL) {
1350                 /*
1351                  * Port is in use by one or more PCBs. Look for best
1352                  * fit.
1353                  */
1354                 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1355                         wildcard = 0;
1356 #ifdef INET6
1357                         if ((inp->inp_vflag & INP_IPV4) == 0)
1358                                 continue;
1359 #endif
1360                         if (inp->inp_faddr.s_addr != INADDR_ANY)
1361                                 wildcard++;
1362                         if (inp->inp_laddr.s_addr != INADDR_ANY) {
1363                                 if (laddr.s_addr == INADDR_ANY)
1364                                         wildcard++;
1365                                 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1366                                         continue;
1367                         } else {
1368                                 if (laddr.s_addr != INADDR_ANY)
1369                                         wildcard++;
1370                         }
1371                         if (wildcard && !wild_okay)
1372                                 continue;
1373                         if (wildcard < matchwild &&
1374                             (cred == NULL ||
1375                              cred->cr_prison == 
1376                                         inp->inp_socket->so_cred->cr_prison)) {
1377                                 match = inp;
1378                                 matchwild = wildcard;
1379                                 if (matchwild == 0) {
1380                                         break;
1381                                 }
1382                         }
1383                 }
1384         }
1385         return (match);
1386 }
1387
1388 struct inpcb *
1389 in_pcblocalgroup_last(const struct inpcbinfo *pcbinfo,
1390     const struct inpcb *inp)
1391 {
1392         const struct inp_localgrphead *hdr;
1393         const struct inp_localgroup *grp;
1394         int i;
1395
1396         if (pcbinfo->localgrphashbase == NULL)
1397                 return NULL;
1398
1399         GET_PCBINFO_TOKEN(pcbinfo);
1400
1401         hdr = &pcbinfo->localgrphashbase[
1402             INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1403
1404         LIST_FOREACH(grp, hdr, il_list) {
1405                 if (grp->il_vflag == inp->inp_vflag &&
1406                     grp->il_lport == inp->inp_lport &&
1407                     memcmp(&grp->il_dependladdr,
1408                         &inp->inp_inc.inc_ie.ie_dependladdr,
1409                         sizeof(grp->il_dependladdr)) == 0) {
1410                         break;
1411                 }
1412         }
1413         if (grp == NULL || grp->il_inpcnt == 1) {
1414                 REL_PCBINFO_TOKEN(pcbinfo);
1415                 return NULL;
1416         }
1417
1418         KASSERT(grp->il_inpcnt >= 2,
1419             ("invalid localgroup inp count %d", grp->il_inpcnt));
1420         for (i = 0; i < grp->il_inpcnt; ++i) {
1421                 if (grp->il_inp[i] == inp) {
1422                         int last = grp->il_inpcnt - 1;
1423
1424                         if (i == last)
1425                                 last = grp->il_inpcnt - 2;
1426                         REL_PCBINFO_TOKEN(pcbinfo);
1427                         return grp->il_inp[last];
1428                 }
1429         }
1430         REL_PCBINFO_TOKEN(pcbinfo);
1431         return NULL;
1432 }
1433
1434 static struct inpcb *
1435 inp_localgroup_lookup(const struct inpcbinfo *pcbinfo,
1436     struct in_addr laddr, uint16_t lport, uint32_t pkt_hash)
1437 {
1438         struct inpcb *local_wild = NULL;
1439         const struct inp_localgrphead *hdr;
1440         const struct inp_localgroup *grp;
1441
1442         ASSERT_PCBINFO_TOKEN_HELD(pcbinfo);
1443
1444         hdr = &pcbinfo->localgrphashbase[
1445             INP_PCBLOCALGRPHASH(lport, pcbinfo->localgrphashmask)];
1446
1447         /*
1448          * Order of socket selection:
1449          * 1. non-wild.
1450          * 2. wild.
1451          *
1452          * NOTE:
1453          * - Local group does not contain jailed sockets
1454          * - Local group does not contain IPv4 mapped INET6 wild sockets
1455          */
1456         LIST_FOREACH(grp, hdr, il_list) {
1457 #ifdef INET6
1458                 if (!(grp->il_vflag & INP_IPV4))
1459                         continue;
1460 #endif
1461                 if (grp->il_lport == lport) {
1462                         int idx;
1463
1464                         /*
1465                          * Modulo-N is used here, which greatly reduces
1466                          * completion queue token contention, thus more
1467                          * cpu time is saved.
1468                          */
1469                         idx = pkt_hash % grp->il_inpcnt;
1470                         if (grp->il_laddr.s_addr == laddr.s_addr)
1471                                 return grp->il_inp[idx];
1472                         else if (grp->il_laddr.s_addr == INADDR_ANY)
1473                                 local_wild = grp->il_inp[idx];
1474                 }
1475         }
1476         if (local_wild != NULL)
1477                 return local_wild;
1478         return NULL;
1479 }
1480
1481 /*
1482  * Lookup PCB in hash list.
1483  */
1484 struct inpcb *
1485 in_pcblookup_pkthash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1486     u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1487     boolean_t wildcard, struct ifnet *ifp, const struct mbuf *m)
1488 {
1489         struct inpcbhead *head;
1490         struct inpcb *inp, *jinp=NULL;
1491         u_short fport = fport_arg, lport = lport_arg;
1492
1493         /*
1494          * First look for an exact match.
1495          */
1496         head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1497             laddr.s_addr, lport, pcbinfo->hashmask)];
1498         LIST_FOREACH(inp, head, inp_hash) {
1499 #ifdef INET6
1500                 if (!(inp->inp_vflag & INP_IPV4))
1501                         continue;
1502 #endif
1503                 if (in_hosteq(inp->inp_faddr, faddr) &&
1504                     in_hosteq(inp->inp_laddr, laddr) &&
1505                     inp->inp_fport == fport && inp->inp_lport == lport) {
1506                         /* found */
1507                         if (inp->inp_socket == NULL ||
1508                             inp->inp_socket->so_cred->cr_prison == NULL) {
1509                                 return (inp);
1510                         } else {
1511                                 if  (jinp == NULL)
1512                                         jinp = inp;
1513                         }
1514                 }
1515         }
1516         if (jinp != NULL)
1517                 return (jinp);
1518
1519         if (wildcard) {
1520                 struct inpcb *local_wild = NULL;
1521                 struct inpcb *jinp_wild = NULL;
1522 #ifdef INET6
1523                 struct inpcb *local_wild_mapped = NULL;
1524 #endif
1525                 struct inpcontainer *ic;
1526                 struct inpcontainerhead *chead;
1527                 struct sockaddr_in jsin;
1528                 struct ucred *cred;
1529
1530                 GET_PCBINFO_TOKEN(pcbinfo);
1531
1532                 /*
1533                  * Check local group first
1534                  */
1535                 if (pcbinfo->localgrphashbase != NULL &&
1536                     m != NULL && (m->m_flags & M_HASH) &&
1537                     !(ifp && ifp->if_type == IFT_FAITH)) {
1538                         inp = inp_localgroup_lookup(pcbinfo,
1539                             laddr, lport, m->m_pkthdr.hash);
1540                         if (inp != NULL) {
1541                                 REL_PCBINFO_TOKEN(pcbinfo);
1542                                 return inp;
1543                         }
1544                 }
1545
1546                 /*
1547                  * Order of socket selection:
1548                  * 1. non-jailed, non-wild.
1549                  * 2. non-jailed, wild.
1550                  * 3. jailed, non-wild.
1551                  * 4. jailed, wild.
1552                  */
1553                 jsin.sin_family = AF_INET;
1554                 chead = &pcbinfo->wildcardhashbase[
1555                     INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1556                 LIST_FOREACH(ic, chead, ic_list) {
1557                         inp = ic->ic_inp;
1558                         if (inp->inp_flags & INP_PLACEMARKER)
1559                                 continue;
1560
1561                         jsin.sin_addr.s_addr = laddr.s_addr;
1562 #ifdef INET6
1563                         if (!(inp->inp_vflag & INP_IPV4))
1564                                 continue;
1565 #endif
1566                         if (inp->inp_socket != NULL)
1567                                 cred = inp->inp_socket->so_cred;
1568                         else
1569                                 cred = NULL;
1570                         if (cred != NULL && jailed(cred)) {
1571                                 if (jinp != NULL)
1572                                         continue;
1573                                 else
1574                                         if (!jailed_ip(cred->cr_prison,
1575                                             (struct sockaddr *)&jsin))
1576                                                 continue;
1577                         }
1578                         if (inp->inp_lport == lport) {
1579                                 if (ifp && ifp->if_type == IFT_FAITH &&
1580                                     !(inp->inp_flags & INP_FAITH))
1581                                         continue;
1582                                 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1583                                         if (cred != NULL && jailed(cred)) {
1584                                                 jinp = inp;
1585                                         } else {
1586                                                 REL_PCBINFO_TOKEN(pcbinfo);
1587                                                 return (inp);
1588                                         }
1589                                 }
1590                                 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1591 #ifdef INET6
1592                                         if (INP_CHECK_SOCKAF(inp->inp_socket,
1593                                                              AF_INET6))
1594                                                 local_wild_mapped = inp;
1595                                         else
1596 #endif
1597                                                 if (cred != NULL &&
1598                                                     jailed(cred))
1599                                                         jinp_wild = inp;
1600                                                 else
1601                                                         local_wild = inp;
1602                                 }
1603                         }
1604                 }
1605
1606                 REL_PCBINFO_TOKEN(pcbinfo);
1607
1608                 if (local_wild != NULL)
1609                         return (local_wild);
1610 #ifdef INET6
1611                 if (local_wild_mapped != NULL)
1612                         return (local_wild_mapped);
1613 #endif
1614                 if (jinp != NULL)
1615                         return (jinp);
1616                 return (jinp_wild);
1617         }
1618
1619         /*
1620          * Not found.
1621          */
1622         return (NULL);
1623 }
1624
1625 struct inpcb *
1626 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1627     u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1628     boolean_t wildcard, struct ifnet *ifp)
1629 {
1630         return in_pcblookup_pkthash(pcbinfo, faddr, fport_arg,
1631             laddr, lport_arg, wildcard, ifp, NULL);
1632 }
1633
1634 /*
1635  * Insert PCB into connection hash table.
1636  */
1637 void
1638 in_pcbinsconnhash(struct inpcb *inp)
1639 {
1640         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1641         struct inpcbhead *bucket;
1642         u_int32_t hashkey_faddr, hashkey_laddr;
1643
1644 #ifdef INET6
1645         if (inp->inp_vflag & INP_IPV6) {
1646                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1647                 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1648         } else {
1649 #endif
1650                 hashkey_faddr = inp->inp_faddr.s_addr;
1651                 hashkey_laddr = inp->inp_laddr.s_addr;
1652 #ifdef INET6
1653         }
1654 #endif
1655
1656         KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1657             ("not in the correct netisr"));
1658         KASSERT(!(inp->inp_flags & INP_WILDCARD), ("already on wildcardhash"));
1659         KASSERT(!(inp->inp_flags & INP_CONNECTED), ("already on connhash"));
1660         inp->inp_flags |= INP_CONNECTED;
1661
1662         /*
1663          * Insert into the connection hash table.
1664          */
1665         bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1666             inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1667         LIST_INSERT_HEAD(bucket, inp, inp_hash);
1668 }
1669
1670 /*
1671  * Remove PCB from connection hash table.
1672  */
1673 void
1674 in_pcbremconnhash(struct inpcb *inp)
1675 {
1676         struct inpcbinfo *pcbinfo __debugvar = inp->inp_pcbinfo;
1677
1678         KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1679             ("not in the correct netisr"));
1680         KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1681
1682         LIST_REMOVE(inp, inp_hash);
1683         inp->inp_flags &= ~INP_CONNECTED;
1684 }
1685
1686 /*
1687  * Insert PCB into port hash table.
1688  */
1689 void
1690 in_pcbinsporthash(struct inpcbportinfo *portinfo, struct inpcb *inp)
1691 {
1692         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1693         struct inpcbporthead *pcbporthash;
1694         struct inpcbport *phd;
1695
1696         /*
1697          * If the porthashbase is shared across several cpus, it must
1698          * have been locked.
1699          */
1700         ASSERT_PORT_TOKEN_HELD(portinfo);
1701
1702         /*
1703          * Insert into the port hash table.
1704          */
1705         pcbporthash = &portinfo->porthashbase[
1706             INP_PCBPORTHASH(inp->inp_lport, portinfo->porthashmask)];
1707
1708         /* Go through port list and look for a head for this lport. */
1709         LIST_FOREACH(phd, pcbporthash, phd_hash) {
1710                 if (phd->phd_port == inp->inp_lport)
1711                         break;
1712         }
1713
1714         /* If none exists, use saved one and tack it on. */
1715         if (phd == NULL) {
1716                 KKASSERT(pcbinfo->portsave != NULL);
1717                 phd = pcbinfo->portsave;
1718                 pcbinfo->portsave = NULL;
1719                 phd->phd_port = inp->inp_lport;
1720                 LIST_INIT(&phd->phd_pcblist);
1721                 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1722         }
1723
1724         inp->inp_portinfo = portinfo;
1725         inp->inp_phd = phd;
1726         LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1727
1728         /*
1729          * Malloc one inpcbport for later use.  It is safe to use
1730          * "wait" malloc here (port token would be released, if
1731          * malloc ever blocked), since all changes to the porthash
1732          * are done.
1733          */
1734         if (pcbinfo->portsave == NULL) {
1735                 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1736                                             M_PCB, M_INTWAIT | M_ZERO);
1737         }
1738 }
1739
1740 void
1741 in_pcbinsporthash_lport(struct inpcb *inp)
1742 {
1743         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1744         struct inpcbportinfo *portinfo;
1745         u_short lport_ho;
1746
1747         /* Locate the proper portinfo based on lport */
1748         lport_ho = ntohs(inp->inp_lport);
1749         portinfo = &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
1750         KKASSERT((lport_ho & pcbinfo->portinfo_mask) == portinfo->offset);
1751
1752         GET_PORT_TOKEN(portinfo);
1753         in_pcbinsporthash(portinfo, inp);
1754         REL_PORT_TOKEN(portinfo);
1755 }
1756
1757 static struct inp_localgroup *
1758 inp_localgroup_alloc(u_char vflag,
1759     uint16_t port, const union in_dependaddr *addr, int size)
1760 {
1761         struct inp_localgroup *grp;
1762
1763         grp = kmalloc(__offsetof(struct inp_localgroup, il_inp[size]),
1764             M_TEMP, M_INTWAIT | M_ZERO);
1765         grp->il_vflag = vflag;
1766         grp->il_lport = port;
1767         grp->il_dependladdr = *addr;
1768         grp->il_inpsiz = size;
1769
1770         return grp;
1771 }
1772
1773 static void
1774 inp_localgroup_free(struct inp_localgroup *grp)
1775 {
1776         kfree(grp, M_TEMP);
1777 }
1778
1779 static void
1780 inp_localgroup_destroy(struct inp_localgroup *grp)
1781 {
1782         LIST_REMOVE(grp, il_list);
1783         inp_localgroup_free(grp);
1784 }
1785
1786 static void
1787 inp_localgroup_copy(struct inp_localgroup *grp,
1788     const struct inp_localgroup *old_grp)
1789 {
1790         int i;
1791
1792         KASSERT(old_grp->il_inpcnt < grp->il_inpsiz,
1793             ("invalid new local group size %d and old local group count %d",
1794              grp->il_inpsiz, old_grp->il_inpcnt));
1795         for (i = 0; i < old_grp->il_inpcnt; ++i)
1796                 grp->il_inp[i] = old_grp->il_inp[i];
1797         grp->il_inpcnt = old_grp->il_inpcnt;
1798 }
1799
1800 static void
1801 in_pcbinslocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1802 {
1803         struct inp_localgrphead *hdr;
1804         struct inp_localgroup *grp, *grp_alloc = NULL;
1805         struct ucred *cred;
1806
1807         ASSERT_PCBINFO_TOKEN_HELD(pcbinfo);
1808
1809         if (pcbinfo->localgrphashbase == NULL)
1810                 return;
1811
1812         /*
1813          * XXX don't allow jailed socket to join local group
1814          */
1815         if (inp->inp_socket != NULL)
1816                 cred = inp->inp_socket->so_cred;
1817         else
1818                 cred = NULL;
1819         if (cred != NULL && jailed(cred))
1820                 return;
1821
1822 #ifdef INET6
1823         /*
1824          * XXX don't allow IPv4 mapped INET6 wild socket
1825          */
1826         if ((inp->inp_vflag & INP_IPV4) &&
1827             inp->inp_laddr.s_addr == INADDR_ANY &&
1828             INP_CHECK_SOCKAF(inp->inp_socket, AF_INET6))
1829                 return;
1830 #endif
1831
1832         hdr = &pcbinfo->localgrphashbase[
1833             INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1834
1835 again:
1836         LIST_FOREACH(grp, hdr, il_list) {
1837                 if (grp->il_vflag == inp->inp_vflag &&
1838                     grp->il_lport == inp->inp_lport &&
1839                     memcmp(&grp->il_dependladdr,
1840                         &inp->inp_inc.inc_ie.ie_dependladdr,
1841                         sizeof(grp->il_dependladdr)) == 0) {
1842                         break;
1843                 }
1844         }
1845         if (grp == NULL) {
1846                 /*
1847                  * Create a new local group
1848                  */
1849                 if (grp_alloc == NULL) {
1850                         grp_alloc = inp_localgroup_alloc(inp->inp_vflag,
1851                             inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr,
1852                             INP_LOCALGROUP_SIZMIN);
1853                         /*
1854                          * Local group allocation could block and the
1855                          * local group w/ the same property might have
1856                          * been added by others when we were blocked;
1857                          * check again.
1858                          */
1859                         goto again;
1860                 } else {
1861                         /* Local group has been allocated; link it */
1862                         grp = grp_alloc;
1863                         grp_alloc = NULL;
1864                         LIST_INSERT_HEAD(hdr, grp, il_list);
1865                 }
1866         } else if (grp->il_inpcnt == grp->il_inpsiz) {
1867                 if (grp->il_inpsiz >= INP_LOCALGROUP_SIZMAX) {
1868                         static int limit_logged = 0;
1869
1870                         if (!limit_logged) {
1871                                 limit_logged = 1;
1872                                 kprintf("local group port %d, "
1873                                     "limit reached\n", ntohs(grp->il_lport));
1874                         }
1875                         if (grp_alloc != NULL) {
1876                                 /*
1877                                  * This would happen if the local group
1878                                  * w/ the same property was expanded when
1879                                  * our local group allocation blocked.
1880                                  */
1881                                 inp_localgroup_free(grp_alloc);
1882                         }
1883                         return;
1884                 }
1885
1886                 /*
1887                  * Expand this local group
1888                  */
1889                 if (grp_alloc == NULL ||
1890                     grp->il_inpcnt >= grp_alloc->il_inpsiz) {
1891                         if (grp_alloc != NULL)
1892                                 inp_localgroup_free(grp_alloc);
1893                         grp_alloc = inp_localgroup_alloc(grp->il_vflag,
1894                             grp->il_lport, &grp->il_dependladdr,
1895                             grp->il_inpsiz * 2);
1896                         /*
1897                          * Local group allocation could block and the
1898                          * local group w/ the same property might have
1899                          * been expanded by others when we were blocked;
1900                          * check again.
1901                          */
1902                         goto again;
1903                 }
1904
1905                 /*
1906                  * Save the old local group, link the new one, and then
1907                  * destroy the old local group
1908                  */
1909                 inp_localgroup_copy(grp_alloc, grp);
1910                 LIST_INSERT_HEAD(hdr, grp_alloc, il_list);
1911                 inp_localgroup_destroy(grp);
1912
1913                 grp = grp_alloc;
1914                 grp_alloc = NULL;
1915         } else {
1916                 /*
1917                  * Found the local group
1918                  */
1919                 if (grp_alloc != NULL) {
1920                         /*
1921                          * This would happen if the local group w/ the
1922                          * same property was added or expanded when our
1923                          * local group allocation blocked.
1924                          */
1925                         inp_localgroup_free(grp_alloc);
1926                         grp_alloc = NULL;
1927                 }
1928         }
1929
1930         KASSERT(grp->il_inpcnt < grp->il_inpsiz,
1931             ("invalid local group size %d and count %d",
1932              grp->il_inpsiz, grp->il_inpcnt));
1933         grp->il_inp[grp->il_inpcnt] = inp;
1934         grp->il_inpcnt++;
1935 }
1936
1937 void
1938 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1939 {
1940         struct inpcontainer *ic;
1941         struct inpcontainerhead *bucket;
1942
1943         GET_PCBINFO_TOKEN(pcbinfo);
1944
1945         in_pcbinslocalgrphash_oncpu(inp, pcbinfo);
1946
1947         bucket = &pcbinfo->wildcardhashbase[
1948             INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1949
1950         ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1951         ic->ic_inp = inp;
1952         LIST_INSERT_HEAD(bucket, ic, ic_list);
1953
1954         REL_PCBINFO_TOKEN(pcbinfo);
1955 }
1956
1957 /*
1958  * Insert PCB into wildcard hash table.
1959  */
1960 void
1961 in_pcbinswildcardhash(struct inpcb *inp)
1962 {
1963         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1964
1965         KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1966             ("not in correct netisr"));
1967         KASSERT(!(inp->inp_flags & INP_CONNECTED),
1968                 ("already on connhash"));
1969         KASSERT(!(inp->inp_flags & INP_WILDCARD),
1970                 ("already on wildcardhash"));
1971         inp->inp_flags |= INP_WILDCARD;
1972
1973         in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1974 }
1975
1976 static void
1977 in_pcbremlocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1978 {
1979         struct inp_localgrphead *hdr;
1980         struct inp_localgroup *grp;
1981
1982         ASSERT_PCBINFO_TOKEN_HELD(pcbinfo);
1983
1984         if (pcbinfo->localgrphashbase == NULL)
1985                 return;
1986
1987         hdr = &pcbinfo->localgrphashbase[
1988             INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1989
1990         LIST_FOREACH(grp, hdr, il_list) {
1991                 int i;
1992
1993                 for (i = 0; i < grp->il_inpcnt; ++i) {
1994                         if (grp->il_inp[i] != inp)
1995                                 continue;
1996
1997                         if (grp->il_inpcnt == 1) {
1998                                 /* Destroy this local group */
1999                                 inp_localgroup_destroy(grp);
2000                         } else {
2001                                 /* Pull up inpcbs */
2002                                 for (; i + 1 < grp->il_inpcnt; ++i)
2003                                         grp->il_inp[i] = grp->il_inp[i + 1];
2004                                 grp->il_inpcnt--;
2005                         }
2006                         return;
2007                 }
2008         }
2009 }
2010
2011 void
2012 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
2013 {
2014         struct inpcontainer *ic;
2015         struct inpcontainerhead *head;
2016
2017         GET_PCBINFO_TOKEN(pcbinfo);
2018
2019         in_pcbremlocalgrphash_oncpu(inp, pcbinfo);
2020
2021         /* find bucket */
2022         head = &pcbinfo->wildcardhashbase[
2023             INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
2024
2025         LIST_FOREACH(ic, head, ic_list) {
2026                 if (ic->ic_inp == inp)
2027                         goto found;
2028         }
2029         REL_PCBINFO_TOKEN(pcbinfo);
2030         return;                 /* not found! */
2031
2032 found:
2033         LIST_REMOVE(ic, ic_list);       /* remove container from bucket chain */
2034         REL_PCBINFO_TOKEN(pcbinfo);
2035         kfree(ic, M_TEMP);              /* deallocate container */
2036 }
2037
2038 /*
2039  * Remove PCB from wildcard hash table.
2040  */
2041 void
2042 in_pcbremwildcardhash(struct inpcb *inp)
2043 {
2044         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2045
2046         KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
2047             ("not in correct netisr"));
2048         KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
2049
2050         in_pcbremwildcardhash_oncpu(inp, pcbinfo);
2051         inp->inp_flags &= ~INP_WILDCARD;
2052 }
2053
2054 /*
2055  * Remove PCB from various lists.
2056  */
2057 void
2058 in_pcbremlists(struct inpcb *inp)
2059 {
2060         if (inp->inp_lport) {
2061                 struct inpcbportinfo *portinfo;
2062                 struct inpcbport *phd;
2063
2064                 /*
2065                  * NOTE:
2066                  * inp->inp_portinfo is _not_ necessary same as
2067                  * inp->inp_pcbinfo->portinfo.
2068                  */
2069                 portinfo = inp->inp_portinfo;
2070                 GET_PORT_TOKEN(portinfo);
2071
2072                 phd = inp->inp_phd;
2073                 LIST_REMOVE(inp, inp_portlist);
2074                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
2075                         LIST_REMOVE(phd, phd_hash);
2076                         kfree(phd, M_PCB);
2077                 }
2078
2079                 REL_PORT_TOKEN(portinfo);
2080         }
2081         if (inp->inp_flags & INP_WILDCARD) {
2082                 in_pcbremwildcardhash(inp);
2083         } else if (inp->inp_flags & INP_CONNECTED) {
2084                 in_pcbremconnhash(inp);
2085         }
2086
2087         if (inp->inp_flags & INP_ONLIST)
2088                 in_pcbofflist(inp);
2089 }
2090
2091 int
2092 prison_xinpcb(struct thread *td, struct inpcb *inp)
2093 {
2094         struct ucred *cr;
2095
2096         if (td->td_proc == NULL)
2097                 return (0);
2098         cr = td->td_proc->p_ucred;
2099         if (cr->cr_prison == NULL)
2100                 return (0);
2101         if (inp->inp_socket && inp->inp_socket->so_cred &&
2102             inp->inp_socket->so_cred->cr_prison &&
2103             cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
2104                 return (0);
2105         return (1);
2106 }
2107
2108 int
2109 in_pcblist_global(SYSCTL_HANDLER_ARGS)
2110 {
2111         struct inpcbinfo *pcbinfo_arr = arg1;
2112         int pcbinfo_arrlen = arg2;
2113         struct inpcb *marker;
2114         int cpu, origcpu;
2115         int error, n;
2116
2117         KASSERT(pcbinfo_arrlen <= ncpus && pcbinfo_arrlen >= 1,
2118             ("invalid pcbinfo count %d", pcbinfo_arrlen));
2119
2120         /*
2121          * The process of preparing the TCB list is too time-consuming and
2122          * resource-intensive to repeat twice on every request.
2123          */
2124         n = 0;
2125         if (req->oldptr == NULL) {
2126                 for (cpu = 0; cpu < pcbinfo_arrlen; ++cpu)
2127                         n += pcbinfo_arr[cpu].ipi_count;
2128                 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
2129                 return 0;
2130         }
2131
2132         if (req->newptr != NULL)
2133                 return EPERM;
2134
2135         marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
2136         marker->inp_flags |= INP_PLACEMARKER;
2137
2138         /*
2139          * OK, now we're committed to doing something.  Re-fetch ipi_count
2140          * after obtaining the generation count.
2141          */
2142         error = 0;
2143         origcpu = mycpuid;
2144         for (cpu = 0; cpu < pcbinfo_arrlen && error == 0; ++cpu) {
2145                 struct inpcbinfo *pcbinfo = &pcbinfo_arr[cpu];
2146                 struct inpcb *inp;
2147                 struct xinpcb xi;
2148                 int i;
2149
2150                 lwkt_migratecpu(cpu);
2151
2152                 GET_PCBINFO_TOKEN(pcbinfo);
2153
2154                 n = pcbinfo->ipi_count;
2155
2156                 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
2157                 i = 0;
2158                 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
2159                         LIST_REMOVE(marker, inp_list);
2160                         LIST_INSERT_AFTER(inp, marker, inp_list);
2161
2162                         if (inp->inp_flags & INP_PLACEMARKER)
2163                                 continue;
2164                         if (prison_xinpcb(req->td, inp))
2165                                 continue;
2166
2167                         bzero(&xi, sizeof xi);
2168                         xi.xi_len = sizeof xi;
2169                         bcopy(inp, &xi.xi_inp, sizeof *inp);
2170                         if (inp->inp_socket)
2171                                 sotoxsocket(inp->inp_socket, &xi.xi_socket);
2172                         if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
2173                                 break;
2174                         ++i;
2175                 }
2176                 LIST_REMOVE(marker, inp_list);
2177
2178                 REL_PCBINFO_TOKEN(pcbinfo);
2179
2180                 if (error == 0 && i < n) {
2181                         bzero(&xi, sizeof xi);
2182                         xi.xi_len = sizeof xi;
2183                         while (i < n) {
2184                                 error = SYSCTL_OUT(req, &xi, sizeof xi);
2185                                 if (error)
2186                                         break;
2187                                 ++i;
2188                         }
2189                 }
2190         }
2191
2192         lwkt_migratecpu(origcpu);
2193         kfree(marker, M_TEMP);
2194         return error;
2195 }
2196
2197 int
2198 in_pcblist_global_ncpus2(SYSCTL_HANDLER_ARGS)
2199 {
2200         return in_pcblist_global(oidp, arg1, ncpus2, req);
2201 }
2202
2203 void
2204 in_savefaddr(struct socket *so, const struct sockaddr *faddr)
2205 {
2206         struct sockaddr_in *sin;
2207
2208         KASSERT(faddr->sa_family == AF_INET,
2209             ("not AF_INET faddr %d", faddr->sa_family));
2210
2211         sin = kmalloc(sizeof(*sin), M_SONAME, M_WAITOK | M_ZERO);
2212         sin->sin_family = AF_INET;
2213         sin->sin_len = sizeof(*sin);
2214         sin->sin_port = ((const struct sockaddr_in *)faddr)->sin_port;
2215         sin->sin_addr = ((const struct sockaddr_in *)faddr)->sin_addr;
2216
2217         so->so_faddr = (struct sockaddr *)sin;
2218 }
2219
2220 void
2221 in_pcbportinfo_init(struct inpcbportinfo *portinfo, int hashsize,
2222     boolean_t shared, u_short offset)
2223 {
2224         memset(portinfo, 0, sizeof(*portinfo));
2225
2226         portinfo->offset = offset;
2227         portinfo->lastport = offset;
2228         portinfo->lastlow = offset;
2229         portinfo->lasthi = offset;
2230
2231         portinfo->porthashbase = hashinit(hashsize, M_PCB,
2232             &portinfo->porthashmask);
2233
2234         if (shared) {
2235                 portinfo->porttoken = kmalloc(sizeof(struct lwkt_token),
2236                     M_PCB, M_WAITOK);
2237                 lwkt_token_init(portinfo->porttoken, "porttoken");
2238         }
2239 }
2240
2241 void
2242 in_pcbportrange(u_short *hi0, u_short *lo0, u_short ofs, u_short step)
2243 {
2244         int hi, lo;
2245
2246         if (step == 1)
2247                 return;
2248
2249         hi = *hi0;
2250         lo = *lo0;
2251
2252         hi = rounddown2(hi, step);
2253         hi += ofs;
2254         if (hi > (int)*hi0)
2255                 hi -= step;
2256
2257         lo = roundup2(lo, step);
2258         lo -= (step - ofs);
2259         if (lo < (int)*lo0)
2260                 lo += step;
2261
2262         *hi0 = hi;
2263         *lo0 = lo;
2264 }
2265
2266 void
2267 in_pcbglobalinit(void)
2268 {
2269         int cpu;
2270
2271         in_pcbmarkers = kmalloc(ncpus * sizeof(struct inpcb), M_PCB,
2272             M_WAITOK | M_ZERO);
2273         in_pcbcontainer_markers = kmalloc(ncpus * sizeof(struct inpcontainer),
2274             M_PCB, M_WAITOK | M_ZERO);
2275
2276         for (cpu = 0; cpu < ncpus; ++cpu) {
2277                 struct inpcontainer *ic = &in_pcbcontainer_markers[cpu];
2278                 struct inpcb *marker = &in_pcbmarkers[cpu];
2279
2280                 marker->inp_flags |= INP_PLACEMARKER;
2281                 ic->ic_inp = marker;
2282         }
2283 }
2284
2285 struct inpcb *
2286 in_pcbmarker(int cpuid)
2287 {
2288         KASSERT(cpuid >= 0 && cpuid < ncpus, ("invalid cpuid %d", cpuid));
2289         KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
2290
2291         return &in_pcbmarkers[cpuid];
2292 }
2293
2294 struct inpcontainer *
2295 in_pcbcontainer_marker(int cpuid)
2296 {
2297         KASSERT(cpuid >= 0 && cpuid < ncpus, ("invalid cpuid %d", cpuid));
2298         KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
2299
2300         return &in_pcbcontainer_markers[cpuid];
2301 }