inpcb/localgroup: Use LSB of the hash to find inpcb.
[dragonfly.git] / sys / netinet / in_pcb.c
blob9c42d3f41b5ea34e8f2ccd00d10dd3d20d0874a2
1 /*
2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Jeffrey M. Hsu.
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.
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.
35 * Copyright (c) 1982, 1986, 1991, 1993, 1995
36 * The Regents of the University of California. All rights reserved.
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.
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.
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 $
66 #include "opt_ipsec.h"
67 #include "opt_inet6.h"
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>
83 #include <sys/thread2.h>
84 #include <sys/socketvar2.h>
85 #include <sys/msgport2.h>
87 #include <machine/limits.h>
89 #include <net/if.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
92 #include <net/netisr2.h>
93 #include <net/toeplitz2.h>
95 #include <netinet/in.h>
96 #include <netinet/in_pcb.h>
97 #include <netinet/in_var.h>
98 #include <netinet/ip_var.h>
99 #ifdef INET6
100 #include <netinet/ip6.h>
101 #include <netinet6/ip6_var.h>
102 #endif /* INET6 */
104 #ifdef IPSEC
105 #include <netinet6/ipsec.h>
106 #include <netproto/key/key.h>
107 #include <netproto/ipsec/esp_var.h>
108 #endif
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
115 #include <netproto/ipsec/ipsec.h>
116 #include <netproto/ipsec/key.h>
117 #define IPSEC
118 #endif /* FAST_IPSEC */
120 #define INP_LOCALGROUP_SIZMIN 8
121 #define INP_LOCALGROUP_SIZMAX 256
123 static struct inpcb *in_pcblookup_local(struct inpcbporthead *porthash,
124 struct in_addr laddr, u_int lport_arg, int wild_okay,
125 struct ucred *cred);
127 struct in_addr zeroin_addr;
130 * These configure the range of local port addresses assigned to
131 * "unspecified" outgoing connections/packets/whatever.
133 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
134 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
136 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
137 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
139 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
140 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
142 #define RANGECHK(var, min, max) \
143 if ((var) < (min)) { (var) = (min); } \
144 else if ((var) > (max)) { (var) = (max); }
146 int udpencap_enable = 1; /* enabled by default */
147 int udpencap_port = 4500; /* triggers decapsulation */
150 * Per-netisr inpcb markers.
151 * NOTE: they should only be used in netisrs.
153 static struct inpcb *in_pcbmarkers;
154 static struct inpcontainer *in_pcbcontainer_markers;
156 static int
157 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
159 int error;
161 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
162 if (!error) {
163 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
164 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
166 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
167 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
169 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
170 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
172 return (error);
175 #undef RANGECHK
177 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
179 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
180 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
181 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
182 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
183 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
184 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
185 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
186 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
187 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
188 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
189 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
190 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
192 static int ip_porthash_trycount = 15;
193 SYSCTL_INT(_net_inet_ip, OID_AUTO, porthash_trycount, CTLFLAG_RW,
194 &ip_porthash_trycount, 0,
195 "Number of tries to find local port matching hash of 4-tuple");
198 * in_pcb.c: manage the Protocol Control Blocks.
200 * NOTE: It is assumed that most of these functions will be called from
201 * a critical section. XXX - There are, unfortunately, a few exceptions
202 * to this rule that should be fixed.
204 * NOTE: The caller should initialize the cpu field to the cpu running the
205 * protocol stack associated with this inpcbinfo.
208 void
209 in_pcbinfo_init(struct inpcbinfo *pcbinfo, int cpu, boolean_t shared)
211 KASSERT(cpu >= 0 && cpu < ncpus, ("invalid cpu%d", cpu));
212 pcbinfo->cpu = cpu;
214 LIST_INIT(&pcbinfo->pcblisthead);
215 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave), M_PCB,
216 M_WAITOK | M_ZERO);
218 if (shared) {
219 pcbinfo->infotoken = kmalloc(sizeof(struct lwkt_token),
220 M_PCB, M_WAITOK);
221 lwkt_token_init(pcbinfo->infotoken, "infotoken");
222 } else {
223 pcbinfo->infotoken = NULL;
227 struct baddynamicports baddynamicports;
230 * Check if the specified port is invalid for dynamic allocation.
233 in_baddynamic(u_int16_t port, u_int16_t proto)
235 switch (proto) {
236 case IPPROTO_TCP:
237 return (DP_ISSET(baddynamicports.tcp, port));
238 case IPPROTO_UDP:
239 #ifdef IPSEC
240 /* Cannot preset this as it is a sysctl */
241 if (port == udpencap_port)
242 return (1);
243 #endif
244 return (DP_ISSET(baddynamicports.udp, port));
245 default:
246 return (0);
250 void
251 in_pcbonlist(struct inpcb *inp)
253 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
255 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
256 ("not in the correct netisr"));
257 KASSERT((inp->inp_flags & INP_ONLIST) == 0, ("already on pcblist"));
258 inp->inp_flags |= INP_ONLIST;
260 GET_PCBINFO_TOKEN(pcbinfo);
261 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
262 pcbinfo->ipi_count++;
263 REL_PCBINFO_TOKEN(pcbinfo);
266 void
267 in_pcbofflist(struct inpcb *inp)
269 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
271 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
272 ("not in the correct netisr"));
273 KASSERT(inp->inp_flags & INP_ONLIST, ("not on pcblist"));
274 inp->inp_flags &= ~INP_ONLIST;
276 GET_PCBINFO_TOKEN(pcbinfo);
277 LIST_REMOVE(inp, inp_list);
278 KASSERT(pcbinfo->ipi_count > 0,
279 ("invalid inpcb count %d", pcbinfo->ipi_count));
280 pcbinfo->ipi_count--;
281 REL_PCBINFO_TOKEN(pcbinfo);
285 * Allocate a PCB and associate it with the socket.
288 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
290 struct inpcb *inp;
291 #ifdef IPSEC
292 int error;
293 #endif
295 inp = kmalloc(pcbinfo->ipi_size, M_PCB, M_WAITOK|M_ZERO|M_NULLOK);
296 if (inp == NULL)
297 return (ENOMEM);
298 inp->inp_lgrpindex = -1;
299 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
300 inp->inp_pcbinfo = pcbinfo;
301 inp->inp_socket = so;
302 #ifdef IPSEC
303 error = ipsec_init_policy(so, &inp->inp_sp);
304 if (error != 0) {
305 kfree(inp, M_PCB);
306 return (error);
308 #endif
309 #ifdef INET6
310 if (INP_CHECK_SOCKAF(so, AF_INET6)) {
311 if (ip6_auto_flowlabel)
312 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
313 inp->inp_af = AF_INET6;
314 } else
315 #endif
316 inp->inp_af = AF_INET;
317 soreference(so);
318 so->so_pcb = inp;
320 in_pcbonlist(inp);
321 return (0);
325 * Unlink a pcb with the intention of moving it to another cpu with a
326 * different pcbinfo. While unlinked nothing should attempt to dereference
327 * inp_pcbinfo, NULL it out so we assert if it does.
329 void
330 in_pcbunlink_flags(struct inpcb *inp, struct inpcbinfo *pcbinfo, int flags)
332 KASSERT(inp->inp_pcbinfo == pcbinfo, ("pcbinfo mismatch"));
333 KASSERT((inp->inp_flags & (flags | INP_CONNECTED)) == 0,
334 ("already linked"));
336 in_pcbofflist(inp);
337 inp->inp_pcbinfo = NULL;
340 void
341 in_pcbunlink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
343 in_pcbunlink_flags(inp, pcbinfo, INP_WILDCARD);
347 * Relink a pcb into a new pcbinfo.
349 void
350 in_pcblink_flags(struct inpcb *inp, struct inpcbinfo *pcbinfo, int flags)
352 KASSERT(inp->inp_pcbinfo == NULL, ("has pcbinfo"));
353 KASSERT((inp->inp_flags & (flags | INP_CONNECTED)) == 0,
354 ("already linked"));
356 inp->inp_pcbinfo = pcbinfo;
357 in_pcbonlist(inp);
360 void
361 in_pcblink(struct inpcb *inp, struct inpcbinfo *pcbinfo)
363 return in_pcblink_flags(inp, pcbinfo, INP_WILDCARD);
366 static boolean_t
367 in_pcbporthash_update(struct inpcbportinfo *portinfo,
368 struct inpcb *inp, u_short lport, struct ucred *cred, int wild)
370 struct inpcbporthead *porthash;
373 * This has to be atomic. If the porthash is shared across multiple
374 * protocol threads, e.g. tcp and udp, then the token must be held.
376 porthash = in_pcbporthash_head(portinfo, lport);
377 GET_PORTHASH_TOKEN(porthash);
379 if (in_pcblookup_local(porthash, inp->inp_laddr, lport,
380 wild, cred) != NULL) {
381 REL_PORTHASH_TOKEN(porthash);
382 return FALSE;
384 inp->inp_lport = lport;
385 in_pcbinsporthash(porthash, inp);
387 REL_PORTHASH_TOKEN(porthash);
388 return TRUE;
391 static int
392 in_pcbsetlport(struct inpcb *inp, int wild, struct ucred *cred)
394 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
395 struct inpcbportinfo *portinfo;
396 u_short first, last, lport, step;
397 u_short *lastport;
398 int count, error;
399 int portinfo_first, portinfo_idx;
401 inp->inp_flags |= INP_ANONPORT;
403 step = pcbinfo->portinfo_cnt;
404 portinfo_first = mycpuid % pcbinfo->portinfo_cnt;
405 portinfo_idx = portinfo_first;
406 loop:
407 portinfo = &pcbinfo->portinfo[portinfo_idx];
409 if (inp->inp_flags & INP_HIGHPORT) {
410 first = ipport_hifirstauto; /* sysctl */
411 last = ipport_hilastauto;
412 lastport = &portinfo->lasthi;
413 } else if (inp->inp_flags & INP_LOWPORT) {
414 if (cred &&
415 (error =
416 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
417 inp->inp_laddr.s_addr = INADDR_ANY;
418 return error;
420 first = ipport_lowfirstauto; /* 1023 */
421 last = ipport_lowlastauto; /* 600 */
422 lastport = &portinfo->lastlow;
423 } else {
424 first = ipport_firstauto; /* sysctl */
425 last = ipport_lastauto;
426 lastport = &portinfo->lastport;
430 * Simple check to ensure all ports are not used up causing
431 * a deadlock here.
433 * We split the two cases (up and down) so that the direction
434 * is not being tested on each round of the loop.
436 if (first > last) {
438 * counting down
440 in_pcbportrange(&first, &last, portinfo->offset, step);
441 count = (first - last) / step;
443 for (;;) {
444 if (count-- < 0) { /* completely used? */
445 error = EADDRNOTAVAIL;
446 break;
448 lport = in_pcblastport_down(lastport,
449 first, last, step);
450 KKASSERT((lport % pcbinfo->portinfo_cnt) ==
451 portinfo->offset);
452 lport = htons(lport);
454 if (in_pcbporthash_update(portinfo, inp, lport,
455 cred, wild)) {
456 error = 0;
457 break;
460 } else {
462 * counting up
464 in_pcbportrange(&last, &first, portinfo->offset, step);
465 count = (last - first) / step;
467 for (;;) {
468 if (count-- < 0) { /* completely used? */
469 error = EADDRNOTAVAIL;
470 break;
472 lport = in_pcblastport_up(lastport, first, last, step);
473 KKASSERT((lport % pcbinfo->portinfo_cnt) ==
474 portinfo->offset);
475 lport = htons(lport);
477 if (in_pcbporthash_update(portinfo, inp, lport,
478 cred, wild)) {
479 error = 0;
480 break;
485 if (error) {
486 /* Try next portinfo */
487 portinfo_idx++;
488 portinfo_idx %= pcbinfo->portinfo_cnt;
489 if (portinfo_idx != portinfo_first)
490 goto loop;
491 inp->inp_laddr.s_addr = INADDR_ANY;
493 return error;
497 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
499 struct socket *so = inp->inp_socket;
500 struct sockaddr_in jsin;
501 struct ucred *cred = NULL;
502 int wild = 0;
504 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
505 return (EADDRNOTAVAIL);
506 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
507 return (EINVAL); /* already bound */
509 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
510 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
511 if (td->td_proc)
512 cred = td->td_proc->p_ucred;
514 if (nam != NULL) {
515 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
516 struct inpcbinfo *pcbinfo;
517 struct inpcbportinfo *portinfo;
518 struct inpcbporthead *porthash;
519 struct inpcb *t;
520 u_short lport, lport_ho;
521 int reuseport = (so->so_options & SO_REUSEPORT);
522 int error;
524 if (nam->sa_len != sizeof *sin)
525 return (EINVAL);
526 #ifdef notdef
528 * We should check the family, but old programs
529 * incorrectly fail to initialize it.
531 if (sin->sin_family != AF_INET)
532 return (EAFNOSUPPORT);
533 #endif
534 if (!prison_replace_wildcards(td, nam))
535 return (EINVAL);
537 lport = sin->sin_port;
538 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
540 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
541 * allow complete duplication of binding if
542 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
543 * and a multicast address is bound on both
544 * new and duplicated sockets.
546 if (so->so_options & SO_REUSEADDR)
547 reuseport = SO_REUSEADDR | SO_REUSEPORT;
548 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
549 sin->sin_port = 0; /* yech... */
550 bzero(&sin->sin_zero, sizeof sin->sin_zero);
551 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
552 return (EADDRNOTAVAIL);
555 inp->inp_laddr = sin->sin_addr;
557 jsin.sin_family = AF_INET;
558 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
559 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
560 inp->inp_laddr.s_addr = INADDR_ANY;
561 return (EINVAL);
563 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
565 if (lport == 0) {
566 /* Auto-select local port */
567 return in_pcbsetlport(inp, wild, cred);
569 lport_ho = ntohs(lport);
571 /* GROSS */
572 if (lport_ho < IPPORT_RESERVED && cred &&
573 (error =
574 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
575 inp->inp_laddr.s_addr = INADDR_ANY;
576 return (error);
580 * Locate the proper portinfo based on lport
582 pcbinfo = inp->inp_pcbinfo;
583 portinfo =
584 &pcbinfo->portinfo[lport_ho % pcbinfo->portinfo_cnt];
585 KKASSERT((lport_ho % pcbinfo->portinfo_cnt) ==
586 portinfo->offset);
589 * This has to be atomic. If the porthash is shared across
590 * multiple protocol threads, e.g. tcp and udp then the token
591 * must be held.
593 porthash = in_pcbporthash_head(portinfo, lport);
594 GET_PORTHASH_TOKEN(porthash);
596 if (so->so_cred->cr_uid != 0 &&
597 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
598 t = in_pcblookup_local(porthash, sin->sin_addr, lport,
599 INPLOOKUP_WILDCARD, cred);
600 if (t &&
601 (so->so_cred->cr_uid !=
602 t->inp_socket->so_cred->cr_uid)) {
603 inp->inp_laddr.s_addr = INADDR_ANY;
604 error = EADDRINUSE;
605 goto done;
608 if (cred && !prison_replace_wildcards(td, nam)) {
609 inp->inp_laddr.s_addr = INADDR_ANY;
610 error = EADDRNOTAVAIL;
611 goto done;
613 t = in_pcblookup_local(porthash, sin->sin_addr, lport,
614 wild, cred);
615 if (t && !(reuseport & t->inp_socket->so_options)) {
616 inp->inp_laddr.s_addr = INADDR_ANY;
617 error = EADDRINUSE;
618 goto done;
620 inp->inp_lport = lport;
621 in_pcbinsporthash(porthash, inp);
622 error = 0;
623 done:
624 REL_PORTHASH_TOKEN(porthash);
625 return (error);
626 } else {
627 jsin.sin_family = AF_INET;
628 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
629 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
630 inp->inp_laddr.s_addr = INADDR_ANY;
631 return (EINVAL);
633 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
635 return in_pcbsetlport(inp, wild, cred);
639 static struct inpcb *
640 in_pcblookup_localremote(struct inpcbporthead *porthash, struct in_addr laddr,
641 u_short lport, struct in_addr faddr, u_short fport, struct ucred *cred)
643 struct inpcb *inp;
644 struct inpcbport *phd;
645 struct inpcb *match = NULL;
648 * If the porthashbase is shared across several cpus, it must
649 * have been locked.
651 ASSERT_PORTHASH_TOKEN_HELD(porthash);
654 * Best fit PCB lookup.
656 * First see if this local port is in use by looking on the
657 * port hash list.
659 LIST_FOREACH(phd, porthash, phd_hash) {
660 if (phd->phd_port == lport)
661 break;
663 if (phd != NULL) {
664 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
665 #ifdef INET6
666 if (!INP_ISIPV4(inp))
667 continue;
668 #endif
669 if (inp->inp_laddr.s_addr != INADDR_ANY &&
670 inp->inp_laddr.s_addr != laddr.s_addr)
671 continue;
673 if (inp->inp_faddr.s_addr != INADDR_ANY &&
674 inp->inp_faddr.s_addr != faddr.s_addr)
675 continue;
677 if (inp->inp_fport != 0 && inp->inp_fport != fport)
678 continue;
680 if (cred == NULL ||
681 cred->cr_prison ==
682 inp->inp_socket->so_cred->cr_prison) {
683 match = inp;
684 break;
688 return (match);
691 static boolean_t
692 in_pcbporthash_update4(struct inpcbportinfo *portinfo,
693 struct inpcb *inp, u_short lport, const struct sockaddr_in *sin,
694 struct ucred *cred)
696 struct inpcbporthead *porthash;
699 * This has to be atomic. If the porthash is shared across multiple
700 * protocol threads, e.g. tcp and udp, then the token must be held.
702 porthash = in_pcbporthash_head(portinfo, lport);
703 GET_PORTHASH_TOKEN(porthash);
705 if (in_pcblookup_localremote(porthash, inp->inp_laddr,
706 lport, sin->sin_addr, sin->sin_port, cred) != NULL) {
707 REL_PORTHASH_TOKEN(porthash);
708 return FALSE;
710 inp->inp_lport = lport;
711 in_pcbinsporthash(porthash, inp);
713 REL_PORTHASH_TOKEN(porthash);
714 return TRUE;
718 in_pcbbind_remote(struct inpcb *inp, const struct sockaddr *remote,
719 struct thread *td)
721 struct proc *p = td->td_proc;
722 u_short *lastport;
723 const struct sockaddr_in *sin = (const struct sockaddr_in *)remote;
724 struct sockaddr_in jsin;
725 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
726 struct inpcbportinfo *portinfo;
727 struct ucred *cred = NULL;
728 u_short first, last, lport, step;
729 int count, error, selfconn;
730 int portinfo_first, portinfo_idx;
731 int hash_cpu, hash_count, hash_count0;
732 uint32_t hash_base = 0, hash;
734 if (TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) /* XXX broken! */
735 return (EADDRNOTAVAIL);
737 KKASSERT(inp->inp_laddr.s_addr != INADDR_ANY);
738 if (inp->inp_lport != 0)
739 return (EINVAL); /* already bound */
741 KKASSERT(p);
742 cred = p->p_ucred;
744 jsin.sin_family = AF_INET;
745 jsin.sin_addr.s_addr = inp->inp_laddr.s_addr;
746 if (!prison_replace_wildcards(td, (struct sockaddr *)&jsin)) {
747 inp->inp_laddr.s_addr = INADDR_ANY;
748 return (EINVAL);
750 inp->inp_laddr.s_addr = jsin.sin_addr.s_addr;
752 hash_count0 = ip_porthash_trycount;
753 if (hash_count0 > 0) {
754 hash_base = toeplitz_piecemeal_addr(sin->sin_addr.s_addr) ^
755 toeplitz_piecemeal_addr(inp->inp_laddr.s_addr) ^
756 toeplitz_piecemeal_port(sin->sin_port);
757 } else {
758 hash_count0 = 0;
761 inp->inp_flags |= INP_ANONPORT;
763 step = pcbinfo->portinfo_cnt;
764 portinfo_first = mycpuid % pcbinfo->portinfo_cnt;
765 portinfo_idx = portinfo_first;
766 loop:
767 hash_cpu = portinfo_idx % netisr_ncpus;
768 portinfo = &pcbinfo->portinfo[portinfo_idx];
769 selfconn = 0;
771 if (inp->inp_flags & INP_HIGHPORT) {
772 first = ipport_hifirstauto; /* sysctl */
773 last = ipport_hilastauto;
774 lastport = &portinfo->lasthi;
775 } else if (inp->inp_flags & INP_LOWPORT) {
776 if (cred &&
777 (error =
778 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))) {
779 inp->inp_laddr.s_addr = INADDR_ANY;
780 return (error);
782 first = ipport_lowfirstauto; /* 1023 */
783 last = ipport_lowlastauto; /* 600 */
784 lastport = &portinfo->lastlow;
785 } else {
786 first = ipport_firstauto; /* sysctl */
787 last = ipport_lastauto;
788 lastport = &portinfo->lastport;
791 /* This could happen on loopback interface */
792 #define IS_SELFCONNECT(inp, lport, sin) \
793 (__predict_false((sin)->sin_port == (lport) && \
794 (sin)->sin_addr.s_addr == (inp)->inp_laddr.s_addr))
797 * Simple check to ensure all ports are not used up causing
798 * a deadlock here.
800 * We split the two cases (up and down) so that the direction
801 * is not being tested on each round of the loop.
803 hash_count = hash_count0;
804 if (first > last) {
806 * counting down
808 in_pcbportrange(&first, &last, portinfo->offset, step);
809 count = ((first - last) / step) + hash_count;
811 for (;;) {
812 if (count-- < 0) { /* completely used? */
813 error = EADDRNOTAVAIL;
814 break;
817 lport = in_pcblastport_down(lastport, first, last,
818 step);
819 KKASSERT((lport % pcbinfo->portinfo_cnt) ==
820 portinfo->offset);
821 lport = htons(lport);
822 if (IS_SELFCONNECT(inp, lport, sin)) {
823 if (!selfconn) {
824 ++count; /* don't count this try */
825 selfconn = 1;
827 continue;
830 if (hash_count) {
831 --hash_count;
832 hash = hash_base ^
833 toeplitz_piecemeal_port(lport);
834 if (netisr_hashcpu(hash) != hash_cpu &&
835 hash_count)
836 continue;
839 if (in_pcbporthash_update4(portinfo,
840 inp, lport, sin, cred)) {
841 error = 0;
842 break;
845 } else {
847 * counting up
849 in_pcbportrange(&last, &first, portinfo->offset, step);
850 count = ((last - first) / step) + hash_count;
852 for (;;) {
853 if (count-- < 0) { /* completely used? */
854 error = EADDRNOTAVAIL;
855 break;
858 lport = in_pcblastport_up(lastport, first, last, step);
859 KKASSERT((lport % pcbinfo->portinfo_cnt) ==
860 portinfo->offset);
861 lport = htons(lport);
862 if (IS_SELFCONNECT(inp, lport, sin)) {
863 if (!selfconn) {
864 ++count; /* don't count this try */
865 selfconn = 1;
867 continue;
870 if (hash_count) {
871 --hash_count;
872 hash = hash_base ^
873 toeplitz_piecemeal_port(lport);
874 if (netisr_hashcpu(hash) != hash_cpu &&
875 hash_count)
876 continue;
879 if (in_pcbporthash_update4(portinfo,
880 inp, lport, sin, cred)) {
881 error = 0;
882 break;
887 #undef IS_SELFCONNECT
889 if (error) {
890 /* Try next portinfo */
891 portinfo_idx++;
892 portinfo_idx %= pcbinfo->portinfo_cnt;
893 if (portinfo_idx != portinfo_first)
894 goto loop;
895 inp->inp_laddr.s_addr = INADDR_ANY;
897 return error;
901 * Transform old in_pcbconnect() into an inner subroutine for new
902 * in_pcbconnect(): Do some validity-checking on the remote
903 * address (in mbuf 'nam') and then determine local host address
904 * (i.e., which interface) to use to access that remote host.
906 * This preserves definition of in_pcbconnect(), while supporting a
907 * slightly different version for T/TCP. (This is more than
908 * a bit of a kludge, but cleaning up the internal interfaces would
909 * have forced minor changes in every protocol).
912 in_pcbladdr_find(struct inpcb *inp, struct sockaddr *nam,
913 struct sockaddr_in **plocal_sin, struct thread *td, int find)
915 struct in_ifaddr *ia;
916 struct ucred *cred = NULL;
917 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
918 struct sockaddr *jsin;
919 int jailed = 0, alloc_route = 0;
921 if (nam->sa_len != sizeof *sin)
922 return (EINVAL);
923 if (sin->sin_family != AF_INET)
924 return (EAFNOSUPPORT);
925 if (sin->sin_port == 0)
926 return (EADDRNOTAVAIL);
927 if (td && td->td_proc && td->td_proc->p_ucred)
928 cred = td->td_proc->p_ucred;
929 if (cred && cred->cr_prison)
930 jailed = 1;
931 if (!TAILQ_EMPTY(&in_ifaddrheads[mycpuid])) {
932 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
934 * If the destination address is INADDR_ANY,
935 * use the primary local address.
936 * If the supplied address is INADDR_BROADCAST,
937 * and the primary interface supports broadcast,
938 * choose the broadcast address for that interface.
940 if (sin->sin_addr.s_addr == INADDR_ANY)
941 sin->sin_addr = IA_SIN(ia)->sin_addr;
942 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
943 (ia->ia_ifp->if_flags & IFF_BROADCAST))
944 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
946 if (find) {
947 struct route *ro;
949 ia = NULL;
951 * If route is known or can be allocated now,
952 * our src addr is taken from the i/f, else punt.
953 * Note that we should check the address family of the cached
954 * destination, in case of sharing the cache with IPv6.
956 ro = &inp->inp_route;
957 if (ro->ro_rt &&
958 (!(ro->ro_rt->rt_flags & RTF_UP) ||
959 ro->ro_dst.sa_family != AF_INET ||
960 satosin(&ro->ro_dst)->sin_addr.s_addr !=
961 sin->sin_addr.s_addr ||
962 inp->inp_socket->so_options & SO_DONTROUTE)) {
963 RTFREE(ro->ro_rt);
964 ro->ro_rt = NULL;
966 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
967 (ro->ro_rt == NULL ||
968 ro->ro_rt->rt_ifp == NULL)) {
969 /* No route yet, so try to acquire one */
970 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
971 ro->ro_dst.sa_family = AF_INET;
972 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
973 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
974 sin->sin_addr;
975 rtalloc(ro);
976 alloc_route = 1;
979 * If we found a route, use the address
980 * corresponding to the outgoing interface
981 * unless it is the loopback (in case a route
982 * to our address on another net goes to loopback).
984 if (ro->ro_rt &&
985 !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
986 if (jailed) {
987 if (jailed_ip(cred->cr_prison,
988 ro->ro_rt->rt_ifa->ifa_addr)) {
989 ia = ifatoia(ro->ro_rt->rt_ifa);
991 } else {
992 ia = ifatoia(ro->ro_rt->rt_ifa);
995 if (ia == NULL) {
996 u_short fport = sin->sin_port;
998 sin->sin_port = 0;
999 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
1000 if (ia && jailed && !jailed_ip(cred->cr_prison,
1001 sintosa(&ia->ia_addr)))
1002 ia = NULL;
1003 if (ia == NULL)
1004 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
1005 if (ia && jailed && !jailed_ip(cred->cr_prison,
1006 sintosa(&ia->ia_addr)))
1007 ia = NULL;
1008 sin->sin_port = fport;
1009 if (ia == NULL &&
1010 !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
1011 ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
1012 if (ia && jailed && !jailed_ip(cred->cr_prison,
1013 sintosa(&ia->ia_addr)))
1014 ia = NULL;
1016 if (!jailed && ia == NULL)
1017 goto fail;
1020 * If the destination address is multicast and an outgoing
1021 * interface has been set as a multicast option, use the
1022 * address of that interface as our source address.
1024 if (!jailed && IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
1025 inp->inp_moptions != NULL) {
1026 struct ip_moptions *imo;
1027 struct ifnet *ifp;
1029 imo = inp->inp_moptions;
1030 if ((ifp = imo->imo_multicast_ifp) != NULL) {
1031 struct in_ifaddr_container *iac;
1033 ia = NULL;
1034 TAILQ_FOREACH(iac,
1035 &in_ifaddrheads[mycpuid], ia_link) {
1036 if (iac->ia->ia_ifp == ifp) {
1037 ia = iac->ia;
1038 break;
1041 if (ia == NULL)
1042 goto fail;
1046 * Don't do pcblookup call here; return interface in plocal_sin
1047 * and exit to caller, that will do the lookup.
1049 if (ia == NULL && jailed) {
1050 if ((jsin = prison_get_nonlocal(
1051 cred->cr_prison, AF_INET, NULL)) != NULL ||
1052 (jsin = prison_get_local(
1053 cred->cr_prison, AF_INET, NULL)) != NULL) {
1054 *plocal_sin = satosin(jsin);
1055 } else {
1056 /* IPv6 only Jail */
1057 goto fail;
1059 } else {
1060 *plocal_sin = &ia->ia_addr;
1063 return (0);
1064 fail:
1065 if (alloc_route)
1066 in_pcbresetroute(inp);
1067 return (EADDRNOTAVAIL);
1071 in_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
1072 struct sockaddr_in **plocal_sin, struct thread *td)
1074 return in_pcbladdr_find(inp, nam, plocal_sin, td,
1075 (inp->inp_laddr.s_addr == INADDR_ANY));
1079 * Outer subroutine:
1080 * Connect from a socket to a specified address.
1081 * Both address and port must be specified in argument sin.
1082 * If don't have a local address for this socket yet,
1083 * then pick one.
1086 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
1088 struct sockaddr_in *if_sin;
1089 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1090 int error;
1092 if_sin = NULL; /* avoid gcc warnings */
1094 /* Call inner routine to assign local interface address. */
1095 if ((error = in_pcbladdr(inp, nam, &if_sin, td)) != 0)
1096 return (error);
1098 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
1099 inp->inp_laddr.s_addr ?
1100 inp->inp_laddr : if_sin->sin_addr,
1101 inp->inp_lport, FALSE, NULL) != NULL) {
1102 return (EADDRINUSE);
1104 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1105 if (inp->inp_lport == 0) {
1106 error = in_pcbbind(inp, NULL, td);
1107 if (error)
1108 return (error);
1110 inp->inp_laddr = if_sin->sin_addr;
1112 inp->inp_faddr = sin->sin_addr;
1113 inp->inp_fport = sin->sin_port;
1114 in_pcbinsconnhash(inp);
1115 return (0);
1118 void
1119 in_pcbdisconnect(struct inpcb *inp)
1122 in_pcbremconnhash(inp);
1123 inp->inp_faddr.s_addr = INADDR_ANY;
1124 inp->inp_fport = 0;
1127 void
1128 in_pcbdetach(struct inpcb *inp)
1130 struct socket *so = inp->inp_socket;
1131 struct inpcbinfo *ipi = inp->inp_pcbinfo;
1133 #ifdef IPSEC
1134 ipsec4_delete_pcbpolicy(inp);
1135 #endif /*IPSEC*/
1136 inp->inp_gencnt = ++ipi->ipi_gencnt;
1137 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
1138 in_pcbremlists(inp);
1139 so->so_pcb = NULL;
1140 sofree(so); /* remove pcb ref */
1141 if (inp->inp_options)
1142 m_free(inp->inp_options);
1143 if (inp->inp_route.ro_rt)
1144 rtfree(inp->inp_route.ro_rt);
1145 ip_freemoptions(inp->inp_moptions);
1146 kfree(inp, M_PCB);
1150 * The socket may have an invalid PCB, i.e. NULL. For example, a TCP
1151 * socket received RST.
1153 static int
1154 in_setsockaddr(struct socket *so, struct sockaddr **nam)
1156 struct inpcb *inp;
1157 struct sockaddr_in *sin;
1159 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
1160 inp = so->so_pcb;
1161 if (!inp)
1162 return (ECONNRESET);
1164 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1165 sin->sin_family = AF_INET;
1166 sin->sin_len = sizeof *sin;
1167 sin->sin_port = inp->inp_lport;
1168 sin->sin_addr = inp->inp_laddr;
1170 *nam = (struct sockaddr *)sin;
1171 return (0);
1174 void
1175 in_setsockaddr_dispatch(netmsg_t msg)
1177 int error;
1179 error = in_setsockaddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1180 lwkt_replymsg(&msg->lmsg, error);
1184 * The socket may have an invalid PCB, i.e. NULL. For example, a TCP
1185 * socket received RST.
1188 in_setpeeraddr(struct socket *so, struct sockaddr **nam)
1190 struct inpcb *inp;
1191 struct sockaddr_in *sin;
1193 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
1194 inp = so->so_pcb;
1195 if (!inp)
1196 return (ECONNRESET);
1198 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
1199 sin->sin_family = AF_INET;
1200 sin->sin_len = sizeof *sin;
1201 sin->sin_port = inp->inp_fport;
1202 sin->sin_addr = inp->inp_faddr;
1204 *nam = (struct sockaddr *)sin;
1205 return (0);
1208 void
1209 in_setpeeraddr_dispatch(netmsg_t msg)
1211 int error;
1213 error = in_setpeeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
1214 lwkt_replymsg(&msg->lmsg, error);
1217 void
1218 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int err,
1219 inp_notify_t notify)
1221 struct inpcb *inp, *marker;
1223 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1224 ("not in the correct netisr"));
1225 marker = &in_pcbmarkers[mycpuid];
1228 * NOTE:
1229 * - If INP_PLACEMARKER is set we must ignore the rest of the
1230 * structure and skip it.
1231 * - It is safe to nuke inpcbs here, since we are in their own
1232 * netisr.
1234 GET_PCBINFO_TOKEN(pcbinfo);
1236 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1237 while ((inp = LIST_NEXT(marker, inp_list)) != NULL) {
1238 LIST_REMOVE(marker, inp_list);
1239 LIST_INSERT_AFTER(inp, marker, inp_list);
1241 if (inp->inp_flags & INP_PLACEMARKER)
1242 continue;
1243 #ifdef INET6
1244 if (!INP_ISIPV4(inp))
1245 continue;
1246 #endif
1247 if (inp->inp_faddr.s_addr != faddr.s_addr ||
1248 inp->inp_socket == NULL)
1249 continue;
1250 (*notify)(inp, err); /* can remove inp from list! */
1252 LIST_REMOVE(marker, inp_list);
1254 REL_PCBINFO_TOKEN(pcbinfo);
1257 void
1258 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1260 struct inpcb *inp, *marker;
1263 * We only need to make sure that we are in netisr0, where all
1264 * multicast operation happen. We could check inpcbinfo which
1265 * does not belong to netisr0 by holding the inpcbinfo's token.
1266 * In this case, the pcbinfo must be able to be shared, i.e.
1267 * pcbinfo->infotoken is not NULL.
1269 ASSERT_IN_NETISR(0);
1270 KASSERT(pcbinfo->cpu == 0 || pcbinfo->infotoken != NULL,
1271 ("pcbinfo could not be shared"));
1274 * Get a marker for the current netisr (netisr0).
1276 * It is possible that the multicast address deletion blocks,
1277 * which could cause temporary token releasing. So we use
1278 * inpcb marker here to get a coherent view of the inpcb list.
1280 * While, on the other hand, moptions are only added and deleted
1281 * in netisr0, so we would not see staled moption or miss moption
1282 * even if the token was released due to the blocking multicast
1283 * address deletion.
1285 marker = &in_pcbmarkers[mycpuid];
1287 GET_PCBINFO_TOKEN(pcbinfo);
1289 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1290 while ((inp = LIST_NEXT(marker, inp_list)) != NULL) {
1291 struct ip_moptions *imo;
1293 LIST_REMOVE(marker, inp_list);
1294 LIST_INSERT_AFTER(inp, marker, inp_list);
1296 if (inp->inp_flags & INP_PLACEMARKER)
1297 continue;
1298 imo = inp->inp_moptions;
1299 if (INP_ISIPV4(inp) && imo != NULL) {
1300 int i, gap;
1303 * Unselect the outgoing interface if it is being
1304 * detached.
1306 if (imo->imo_multicast_ifp == ifp)
1307 imo->imo_multicast_ifp = NULL;
1310 * Drop multicast group membership if we joined
1311 * through the interface being detached.
1313 for (i = 0, gap = 0; i < imo->imo_num_memberships;
1314 i++) {
1315 if (imo->imo_membership[i]->inm_ifp == ifp) {
1317 * NOTE:
1318 * This could block and the pcbinfo
1319 * token could be passively released.
1321 in_delmulti(imo->imo_membership[i]);
1322 gap++;
1323 } else if (gap != 0)
1324 imo->imo_membership[i - gap] =
1325 imo->imo_membership[i];
1327 imo->imo_num_memberships -= gap;
1330 LIST_REMOVE(marker, inp_list);
1332 REL_PCBINFO_TOKEN(pcbinfo);
1336 * Check for alternatives when higher level complains
1337 * about service problems. For now, invalidate cached
1338 * routing information. If the route was created dynamically
1339 * (by a redirect), time to try a default gateway again.
1341 void
1342 in_losing(struct inpcb *inp)
1344 struct rtentry *rt;
1345 struct rt_addrinfo rtinfo;
1347 if ((rt = inp->inp_route.ro_rt)) {
1348 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1349 rtinfo.rti_info[RTAX_DST] = rt_key(rt);
1350 rtinfo.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1351 rtinfo.rti_info[RTAX_NETMASK] = rt_mask(rt);
1352 rtinfo.rti_flags = rt->rt_flags;
1353 rt_missmsg(RTM_LOSING, &rtinfo, rt->rt_flags, 0);
1354 if (rt->rt_flags & RTF_DYNAMIC) {
1355 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1356 rt_mask(rt), rt->rt_flags, NULL);
1358 inp->inp_route.ro_rt = NULL;
1359 rtfree(rt);
1361 * A new route can be allocated
1362 * the next time output is attempted.
1368 * After a routing change, flush old routing
1369 * and allocate a (hopefully) better one.
1371 void
1372 in_rtchange(struct inpcb *inp, int err)
1374 if (inp->inp_route.ro_rt) {
1375 rtfree(inp->inp_route.ro_rt);
1376 inp->inp_route.ro_rt = NULL;
1378 * A new route can be allocated the next time
1379 * output is attempted.
1385 * Lookup a PCB based on the local address and port.
1387 static struct inpcb *
1388 in_pcblookup_local(struct inpcbporthead *porthash, struct in_addr laddr,
1389 u_int lport_arg, int wild_okay, struct ucred *cred)
1391 struct inpcb *inp;
1392 int matchwild = 3, wildcard;
1393 u_short lport = lport_arg;
1394 struct inpcbport *phd;
1395 struct inpcb *match = NULL;
1398 * If the porthashbase is shared across several cpus, it must
1399 * have been locked.
1401 ASSERT_PORTHASH_TOKEN_HELD(porthash);
1404 * Best fit PCB lookup.
1406 * First see if this local port is in use by looking on the
1407 * port hash list.
1409 LIST_FOREACH(phd, porthash, phd_hash) {
1410 if (phd->phd_port == lport)
1411 break;
1413 if (phd != NULL) {
1415 * Port is in use by one or more PCBs. Look for best
1416 * fit.
1418 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1419 wildcard = 0;
1420 #ifdef INET6
1421 if (!INP_ISIPV4(inp))
1422 continue;
1423 #endif
1424 if (inp->inp_faddr.s_addr != INADDR_ANY)
1425 wildcard++;
1426 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1427 if (laddr.s_addr == INADDR_ANY)
1428 wildcard++;
1429 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1430 continue;
1431 } else {
1432 if (laddr.s_addr != INADDR_ANY)
1433 wildcard++;
1435 if (wildcard && !wild_okay)
1436 continue;
1437 if (wildcard < matchwild &&
1438 (cred == NULL ||
1439 cred->cr_prison ==
1440 inp->inp_socket->so_cred->cr_prison)) {
1441 match = inp;
1442 matchwild = wildcard;
1443 if (matchwild == 0) {
1444 break;
1449 return (match);
1452 struct inpcb *
1453 in_pcblocalgroup_last(const struct inpcbinfo *pcbinfo,
1454 const struct inpcb *inp)
1456 const struct inp_localgrphead *hdr;
1457 const struct inp_localgroup *grp;
1458 int i;
1460 if (pcbinfo->localgrphashbase == NULL)
1461 return NULL;
1463 GET_PCBINFO_TOKEN(pcbinfo);
1465 hdr = &pcbinfo->localgrphashbase[
1466 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1468 LIST_FOREACH(grp, hdr, il_list) {
1469 if (grp->il_af == inp->inp_af &&
1470 grp->il_lport == inp->inp_lport &&
1471 memcmp(&grp->il_dependladdr,
1472 &inp->inp_inc.inc_ie.ie_dependladdr,
1473 sizeof(grp->il_dependladdr)) == 0) {
1474 break;
1477 if (grp == NULL || grp->il_inpcnt == 1) {
1478 REL_PCBINFO_TOKEN(pcbinfo);
1479 return NULL;
1482 KASSERT(grp->il_inpcnt >= 2,
1483 ("invalid localgroup inp count %d", grp->il_inpcnt));
1484 for (i = 0; i < grp->il_inpcnt; ++i) {
1485 if (grp->il_inp[i] == inp) {
1486 int last = grp->il_inpcnt - 1;
1488 if (i == last)
1489 last = grp->il_inpcnt - 2;
1490 REL_PCBINFO_TOKEN(pcbinfo);
1491 return grp->il_inp[last];
1494 REL_PCBINFO_TOKEN(pcbinfo);
1495 return NULL;
1498 static struct inpcb *
1499 inp_localgroup_lookup(const struct inpcbinfo *pcbinfo,
1500 struct in_addr laddr, uint16_t lport, uint32_t pkt_hash)
1502 struct inpcb *local_wild = NULL;
1503 const struct inp_localgrphead *hdr;
1504 const struct inp_localgroup *grp;
1506 ASSERT_PCBINFO_TOKEN_HELD(pcbinfo);
1508 hdr = &pcbinfo->localgrphashbase[
1509 INP_PCBLOCALGRPHASH(lport, pcbinfo->localgrphashmask)];
1512 * Order of socket selection:
1513 * 1. non-wild.
1514 * 2. wild.
1516 * NOTE: Local group does not contain jailed sockets
1518 LIST_FOREACH(grp, hdr, il_list) {
1519 #ifdef INET6
1520 if (grp->il_af != AF_INET)
1521 continue;
1522 #endif
1523 if (grp->il_lport == lport) {
1524 int idx;
1527 * Modulo-N is used here, which greatly reduces
1528 * completion queue token contention, thus more
1529 * cpu time is saved.
1531 idx = netisr_hashlsb(pkt_hash) % grp->il_inpcnt;
1532 if (grp->il_laddr.s_addr == laddr.s_addr)
1533 return grp->il_inp[idx];
1534 else if (grp->il_laddr.s_addr == INADDR_ANY)
1535 local_wild = grp->il_inp[idx];
1538 if (local_wild != NULL)
1539 return local_wild;
1540 return NULL;
1544 * Lookup PCB in hash list.
1546 struct inpcb *
1547 in_pcblookup_pkthash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1548 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1549 boolean_t wildcard, struct ifnet *ifp, const struct mbuf *m)
1551 struct inpcbhead *head;
1552 struct inpcb *inp, *jinp=NULL;
1553 u_short fport = fport_arg, lport = lport_arg;
1556 * First look for an exact match.
1558 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
1559 laddr.s_addr, lport, pcbinfo->hashmask)];
1560 LIST_FOREACH(inp, head, inp_hash) {
1561 #ifdef INET6
1562 if (!INP_ISIPV4(inp))
1563 continue;
1564 #endif
1565 if (in_hosteq(inp->inp_faddr, faddr) &&
1566 in_hosteq(inp->inp_laddr, laddr) &&
1567 inp->inp_fport == fport && inp->inp_lport == lport) {
1568 /* found */
1569 if (inp->inp_socket == NULL ||
1570 inp->inp_socket->so_cred->cr_prison == NULL) {
1571 return (inp);
1572 } else {
1573 if (jinp == NULL)
1574 jinp = inp;
1578 if (jinp != NULL)
1579 return (jinp);
1581 if (wildcard) {
1582 struct inpcb *local_wild = NULL;
1583 struct inpcb *jinp_wild = NULL;
1584 struct inpcontainer *ic;
1585 struct inpcontainerhead *chead;
1586 struct sockaddr_in jsin;
1587 struct ucred *cred;
1589 GET_PCBINFO_TOKEN(pcbinfo);
1592 * Check local group first
1594 if (pcbinfo->localgrphashbase != NULL &&
1595 m != NULL && (m->m_flags & M_HASH) &&
1596 !(ifp && ifp->if_type == IFT_FAITH)) {
1597 inp = inp_localgroup_lookup(pcbinfo,
1598 laddr, lport, m->m_pkthdr.hash);
1599 if (inp != NULL) {
1600 REL_PCBINFO_TOKEN(pcbinfo);
1601 return inp;
1606 * Order of socket selection:
1607 * 1. non-jailed, non-wild.
1608 * 2. non-jailed, wild.
1609 * 3. jailed, non-wild.
1610 * 4. jailed, wild.
1612 jsin.sin_family = AF_INET;
1613 chead = &pcbinfo->wildcardhashbase[
1614 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
1615 LIST_FOREACH(ic, chead, ic_list) {
1616 inp = ic->ic_inp;
1617 if (inp->inp_flags & INP_PLACEMARKER)
1618 continue;
1620 jsin.sin_addr.s_addr = laddr.s_addr;
1621 #ifdef INET6
1622 if (!INP_ISIPV4(inp))
1623 continue;
1624 #endif
1625 if (inp->inp_socket != NULL)
1626 cred = inp->inp_socket->so_cred;
1627 else
1628 cred = NULL;
1629 if (cred != NULL && jailed(cred)) {
1630 if (jinp != NULL)
1631 continue;
1632 else
1633 if (!jailed_ip(cred->cr_prison,
1634 (struct sockaddr *)&jsin))
1635 continue;
1637 if (inp->inp_lport == lport) {
1638 if (ifp && ifp->if_type == IFT_FAITH &&
1639 !(inp->inp_flags & INP_FAITH))
1640 continue;
1641 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1642 if (cred != NULL && jailed(cred)) {
1643 jinp = inp;
1644 } else {
1645 REL_PCBINFO_TOKEN(pcbinfo);
1646 return (inp);
1649 if (inp->inp_laddr.s_addr == INADDR_ANY) {
1650 if (cred != NULL && jailed(cred))
1651 jinp_wild = inp;
1652 else
1653 local_wild = inp;
1658 REL_PCBINFO_TOKEN(pcbinfo);
1660 if (local_wild != NULL)
1661 return (local_wild);
1662 if (jinp != NULL)
1663 return (jinp);
1664 return (jinp_wild);
1668 * Not found.
1670 return (NULL);
1673 struct inpcb *
1674 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1675 u_int fport_arg, struct in_addr laddr, u_int lport_arg,
1676 boolean_t wildcard, struct ifnet *ifp)
1678 return in_pcblookup_pkthash(pcbinfo, faddr, fport_arg,
1679 laddr, lport_arg, wildcard, ifp, NULL);
1683 * Insert PCB into connection hash table.
1685 void
1686 in_pcbinsconnhash(struct inpcb *inp)
1688 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1689 struct inpcbhead *bucket;
1690 u_int32_t hashkey_faddr, hashkey_laddr;
1692 #ifdef INET6
1693 if (INP_ISIPV6(inp)) {
1694 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1695 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1696 } else {
1697 #endif
1698 hashkey_faddr = inp->inp_faddr.s_addr;
1699 hashkey_laddr = inp->inp_laddr.s_addr;
1700 #ifdef INET6
1702 #endif
1704 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1705 ("not in the correct netisr"));
1706 ASSERT_INP_NOTINHASH(inp);
1707 inp->inp_flags |= INP_CONNECTED;
1710 * Insert into the connection hash table.
1712 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1713 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1714 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1718 * Remove PCB from connection hash table.
1720 void
1721 in_pcbremconnhash(struct inpcb *inp)
1723 struct inpcbinfo *pcbinfo __debugvar = inp->inp_pcbinfo;
1725 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
1726 ("not in the correct netisr"));
1727 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1729 LIST_REMOVE(inp, inp_hash);
1730 inp->inp_flags &= ~INP_CONNECTED;
1734 * Insert PCB into port hash table.
1736 void
1737 in_pcbinsporthash(struct inpcbporthead *pcbporthash, struct inpcb *inp)
1739 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1740 struct inpcbport *phd;
1743 * If the porthashbase is shared across several cpus, it must
1744 * have been locked.
1746 ASSERT_PORTHASH_TOKEN_HELD(pcbporthash);
1749 * Insert into the port hash table.
1752 /* Go through port list and look for a head for this lport. */
1753 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1754 if (phd->phd_port == inp->inp_lport)
1755 break;
1758 /* If none exists, use saved one and tack it on. */
1759 if (phd == NULL) {
1760 KKASSERT(pcbinfo->portsave != NULL);
1761 phd = pcbinfo->portsave;
1762 pcbinfo->portsave = NULL;
1763 phd->phd_port = inp->inp_lport;
1764 LIST_INIT(&phd->phd_pcblist);
1765 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1768 inp->inp_porthash = pcbporthash;
1769 inp->inp_phd = phd;
1770 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1773 * Malloc one inpcbport for later use. It is safe to use
1774 * "wait" malloc here (port token would be released, if
1775 * malloc ever blocked), since all changes to the porthash
1776 * are done.
1778 if (pcbinfo->portsave == NULL) {
1779 pcbinfo->portsave = kmalloc(sizeof(*pcbinfo->portsave),
1780 M_PCB, M_INTWAIT | M_ZERO);
1784 void
1785 in_pcbinsporthash_lport(struct inpcb *inp)
1787 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1788 struct inpcbportinfo *portinfo;
1789 struct inpcbporthead *porthash;
1790 u_short lport_ho;
1792 /* Locate the proper portinfo based on lport */
1793 lport_ho = ntohs(inp->inp_lport);
1794 portinfo = &pcbinfo->portinfo[lport_ho % pcbinfo->portinfo_cnt];
1795 KKASSERT((lport_ho % pcbinfo->portinfo_cnt) == portinfo->offset);
1797 porthash = in_pcbporthash_head(portinfo, inp->inp_lport);
1798 GET_PORTHASH_TOKEN(porthash);
1799 in_pcbinsporthash(porthash, inp);
1800 REL_PORTHASH_TOKEN(porthash);
1803 void
1804 in_pcbremporthash(struct inpcb *inp)
1806 struct inpcbporthead *porthash;
1807 struct inpcbport *phd;
1809 if (inp->inp_phd == NULL)
1810 return;
1811 KASSERT(inp->inp_lport != 0, ("inpcb has no lport"));
1813 porthash = inp->inp_porthash;
1814 KASSERT(porthash != NULL, ("no porthash"));
1816 GET_PORTHASH_TOKEN(porthash);
1818 phd = inp->inp_phd;
1819 LIST_REMOVE(inp, inp_portlist);
1820 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1821 LIST_REMOVE(phd, phd_hash);
1822 kfree(phd, M_PCB);
1825 REL_PORTHASH_TOKEN(porthash);
1827 inp->inp_phd = NULL;
1828 /* NOTE: Don't whack inp_lport, which may be used later */
1831 static struct inp_localgroup *
1832 inp_localgroup_alloc(u_char af, uint16_t port,
1833 const union in_dependaddr *addr, int size)
1835 struct inp_localgroup *grp;
1837 grp = kmalloc(__offsetof(struct inp_localgroup, il_inp[size]),
1838 M_TEMP, M_INTWAIT | M_ZERO);
1839 grp->il_af = af;
1840 grp->il_lport = port;
1841 grp->il_dependladdr = *addr;
1842 grp->il_inpsiz = size;
1844 return grp;
1847 static void
1848 inp_localgroup_free(struct inp_localgroup *grp)
1850 kfree(grp, M_TEMP);
1853 static void
1854 inp_localgroup_destroy(struct inp_localgroup *grp)
1856 LIST_REMOVE(grp, il_list);
1857 inp_localgroup_free(grp);
1860 static void
1861 inp_localgroup_copy(struct inp_localgroup *grp,
1862 const struct inp_localgroup *old_grp)
1864 int i;
1866 KASSERT(old_grp->il_inpcnt < grp->il_inpsiz,
1867 ("invalid new local group size %d and old local group count %d",
1868 grp->il_inpsiz, old_grp->il_inpcnt));
1869 for (i = 0; i < old_grp->il_inpcnt; ++i)
1870 grp->il_inp[i] = old_grp->il_inp[i];
1871 grp->il_inpcnt = old_grp->il_inpcnt;
1874 static void
1875 in_pcbinslocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1877 struct inp_localgrphead *hdr;
1878 struct inp_localgroup *grp, *grp_alloc = NULL;
1879 struct ucred *cred;
1880 int i, idx;
1882 ASSERT_PCBINFO_TOKEN_HELD(pcbinfo);
1884 if (pcbinfo->localgrphashbase == NULL)
1885 return;
1888 * XXX don't allow jailed socket to join local group
1890 if (inp->inp_socket != NULL)
1891 cred = inp->inp_socket->so_cred;
1892 else
1893 cred = NULL;
1894 if (cred != NULL && jailed(cred))
1895 return;
1897 hdr = &pcbinfo->localgrphashbase[
1898 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
1900 again:
1901 LIST_FOREACH(grp, hdr, il_list) {
1902 if (grp->il_af == inp->inp_af &&
1903 grp->il_lport == inp->inp_lport &&
1904 memcmp(&grp->il_dependladdr,
1905 &inp->inp_inc.inc_ie.ie_dependladdr,
1906 sizeof(grp->il_dependladdr)) == 0) {
1907 break;
1910 if (grp == NULL) {
1912 * Create a new local group
1914 if (grp_alloc == NULL) {
1915 grp_alloc = inp_localgroup_alloc(inp->inp_af,
1916 inp->inp_lport, &inp->inp_inc.inc_ie.ie_dependladdr,
1917 INP_LOCALGROUP_SIZMIN);
1919 * Local group allocation could block and the
1920 * local group w/ the same property might have
1921 * been added by others when we were blocked;
1922 * check again.
1924 goto again;
1925 } else {
1926 /* Local group has been allocated; link it */
1927 grp = grp_alloc;
1928 grp_alloc = NULL;
1929 LIST_INSERT_HEAD(hdr, grp, il_list);
1931 } else if (grp->il_inpcnt == grp->il_inpsiz) {
1932 if (grp->il_inpsiz >= INP_LOCALGROUP_SIZMAX) {
1933 static int limit_logged = 0;
1935 if (!limit_logged) {
1936 limit_logged = 1;
1937 kprintf("local group port %d, "
1938 "limit reached\n", ntohs(grp->il_lport));
1940 if (grp_alloc != NULL) {
1942 * This would happen if the local group
1943 * w/ the same property was expanded when
1944 * our local group allocation blocked.
1946 inp_localgroup_free(grp_alloc);
1948 return;
1952 * Expand this local group
1954 if (grp_alloc == NULL ||
1955 grp->il_inpcnt >= grp_alloc->il_inpsiz) {
1956 if (grp_alloc != NULL)
1957 inp_localgroup_free(grp_alloc);
1958 grp_alloc = inp_localgroup_alloc(grp->il_af,
1959 grp->il_lport, &grp->il_dependladdr,
1960 grp->il_inpsiz * 2);
1962 * Local group allocation could block and the
1963 * local group w/ the same property might have
1964 * been expanded by others when we were blocked;
1965 * check again.
1967 goto again;
1971 * Save the old local group, link the new one, and then
1972 * destroy the old local group
1974 inp_localgroup_copy(grp_alloc, grp);
1975 LIST_INSERT_HEAD(hdr, grp_alloc, il_list);
1976 inp_localgroup_destroy(grp);
1978 grp = grp_alloc;
1979 grp_alloc = NULL;
1980 } else {
1982 * Found the local group
1984 if (grp_alloc != NULL) {
1986 * This would happen if the local group w/ the
1987 * same property was added or expanded when our
1988 * local group allocation blocked.
1990 inp_localgroup_free(grp_alloc);
1991 grp_alloc = NULL;
1995 KASSERT(grp->il_inpcnt < grp->il_inpsiz,
1996 ("invalid local group size %d and count %d",
1997 grp->il_inpsiz, grp->il_inpcnt));
2000 * Keep the local group sorted by the inpcb local group index
2001 * in ascending order.
2003 * This eases the multi-process userland application which uses
2004 * SO_REUSEPORT sockets and binds process to the owner cpu of
2005 * the SO_REUSEPORT socket:
2006 * If we didn't sort the local group by the inpcb local group
2007 * index and one of the process owning an inpcb in this local
2008 * group restarted, e.g. crashed and restarted by watchdog,
2009 * other processes owning a inpcb in this local group would have
2010 * to detect that event, refetch its socket's owner cpu, and
2011 * re-bind.
2013 idx = grp->il_inpcnt;
2014 for (i = 0; i < idx; ++i) {
2015 struct inpcb *oinp = grp->il_inp[i];
2017 if (oinp->inp_lgrpindex > i) {
2018 if (inp->inp_lgrpindex < 0) {
2019 inp->inp_lgrpindex = i;
2020 } else if (inp->inp_lgrpindex != i) {
2021 if (bootverbose) {
2022 kprintf("inp %p: grpidx %d, "
2023 "assigned to %d, cpu%d\n",
2024 inp, inp->inp_lgrpindex, i,
2025 mycpuid);
2028 grp->il_inp[i] = inp;
2030 /* Pull down inpcbs */
2031 for (; i < grp->il_inpcnt; ++i) {
2032 struct inpcb *oinp1 = grp->il_inp[i + 1];
2034 grp->il_inp[i + 1] = oinp;
2035 oinp = oinp1;
2037 grp->il_inpcnt++;
2038 return;
2042 if (inp->inp_lgrpindex < 0) {
2043 inp->inp_lgrpindex = idx;
2044 } else if (inp->inp_lgrpindex != idx) {
2045 if (bootverbose) {
2046 kprintf("inp %p: grpidx %d, assigned to %d, cpu%d\n",
2047 inp, inp->inp_lgrpindex, idx, mycpuid);
2050 grp->il_inp[idx] = inp;
2051 grp->il_inpcnt++;
2054 void
2055 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
2057 struct inpcontainer *ic;
2058 struct inpcontainerhead *bucket;
2060 GET_PCBINFO_TOKEN(pcbinfo);
2062 in_pcbinslocalgrphash_oncpu(inp, pcbinfo);
2064 bucket = &pcbinfo->wildcardhashbase[
2065 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
2067 ic = kmalloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
2068 ic->ic_inp = inp;
2069 LIST_INSERT_HEAD(bucket, ic, ic_list);
2071 REL_PCBINFO_TOKEN(pcbinfo);
2075 * Insert PCB into wildcard hash table.
2077 void
2078 in_pcbinswildcardhash(struct inpcb *inp)
2080 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2082 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
2083 ("not in correct netisr"));
2084 ASSERT_INP_NOTINHASH(inp);
2085 inp->inp_flags |= INP_WILDCARD;
2087 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
2090 static void
2091 in_pcbremlocalgrphash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
2093 struct inp_localgrphead *hdr;
2094 struct inp_localgroup *grp;
2096 ASSERT_PCBINFO_TOKEN_HELD(pcbinfo);
2098 if (pcbinfo->localgrphashbase == NULL)
2099 return;
2101 hdr = &pcbinfo->localgrphashbase[
2102 INP_PCBLOCALGRPHASH(inp->inp_lport, pcbinfo->localgrphashmask)];
2104 LIST_FOREACH(grp, hdr, il_list) {
2105 int i;
2107 for (i = 0; i < grp->il_inpcnt; ++i) {
2108 if (grp->il_inp[i] != inp)
2109 continue;
2111 if (grp->il_inpcnt == 1) {
2112 /* Destroy this local group */
2113 inp_localgroup_destroy(grp);
2114 } else {
2115 /* Pull up inpcbs */
2116 for (; i + 1 < grp->il_inpcnt; ++i)
2117 grp->il_inp[i] = grp->il_inp[i + 1];
2118 grp->il_inpcnt--;
2120 return;
2125 void
2126 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
2128 struct inpcontainer *ic;
2129 struct inpcontainerhead *head;
2131 GET_PCBINFO_TOKEN(pcbinfo);
2133 in_pcbremlocalgrphash_oncpu(inp, pcbinfo);
2135 /* find bucket */
2136 head = &pcbinfo->wildcardhashbase[
2137 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
2139 LIST_FOREACH(ic, head, ic_list) {
2140 if (ic->ic_inp == inp)
2141 goto found;
2143 REL_PCBINFO_TOKEN(pcbinfo);
2144 return; /* not found! */
2146 found:
2147 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
2148 REL_PCBINFO_TOKEN(pcbinfo);
2149 kfree(ic, M_TEMP); /* deallocate container */
2153 * Remove PCB from wildcard hash table.
2155 void
2156 in_pcbremwildcardhash(struct inpcb *inp)
2158 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
2160 KASSERT(&curthread->td_msgport == netisr_cpuport(pcbinfo->cpu),
2161 ("not in correct netisr"));
2162 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
2164 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
2165 inp->inp_lgrpindex = -1;
2166 inp->inp_flags &= ~INP_WILDCARD;
2170 * Remove PCB from various lists.
2172 void
2173 in_pcbremlists(struct inpcb *inp)
2175 in_pcbremporthash(inp);
2176 if (inp->inp_flags & INP_WILDCARD) {
2177 in_pcbremwildcardhash(inp);
2178 } else if (inp->inp_flags & INP_CONNECTED) {
2179 in_pcbremconnhash(inp);
2182 if (inp->inp_flags & INP_ONLIST)
2183 in_pcbofflist(inp);
2187 prison_xinpcb(struct thread *td, struct inpcb *inp)
2189 struct ucred *cr;
2191 if (td->td_proc == NULL)
2192 return (0);
2193 cr = td->td_proc->p_ucred;
2194 if (cr->cr_prison == NULL)
2195 return (0);
2196 if (inp->inp_socket && inp->inp_socket->so_cred &&
2197 inp->inp_socket->so_cred->cr_prison &&
2198 cr->cr_prison == inp->inp_socket->so_cred->cr_prison)
2199 return (0);
2200 return (1);
2204 in_pcblist_range(SYSCTL_HANDLER_ARGS)
2206 struct inpcbinfo *pcbinfo_arr = arg1;
2207 int pcbinfo_arrlen = arg2;
2208 struct inpcb *marker;
2209 int cpu, origcpu;
2210 int error, n;
2212 KASSERT(pcbinfo_arrlen <= ncpus && pcbinfo_arrlen >= 1,
2213 ("invalid pcbinfo count %d", pcbinfo_arrlen));
2216 * The process of preparing the TCB list is too time-consuming and
2217 * resource-intensive to repeat twice on every request.
2219 n = 0;
2220 if (req->oldptr == NULL) {
2221 for (cpu = 0; cpu < pcbinfo_arrlen; ++cpu)
2222 n += pcbinfo_arr[cpu].ipi_count;
2223 req->oldidx = (n + n/8 + 10) * sizeof(struct xinpcb);
2224 return 0;
2227 if (req->newptr != NULL)
2228 return EPERM;
2230 marker = kmalloc(sizeof(struct inpcb), M_TEMP, M_WAITOK|M_ZERO);
2231 marker->inp_flags |= INP_PLACEMARKER;
2234 * OK, now we're committed to doing something. Re-fetch ipi_count
2235 * after obtaining the generation count.
2237 error = 0;
2238 origcpu = mycpuid;
2239 for (cpu = 0; cpu < pcbinfo_arrlen && error == 0; ++cpu) {
2240 struct inpcbinfo *pcbinfo = &pcbinfo_arr[cpu];
2241 struct inpcb *inp;
2242 struct xinpcb xi;
2243 int i;
2245 lwkt_migratecpu(cpu);
2247 GET_PCBINFO_TOKEN(pcbinfo);
2249 n = pcbinfo->ipi_count;
2251 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
2252 i = 0;
2253 while ((inp = LIST_NEXT(marker, inp_list)) != NULL && i < n) {
2254 LIST_REMOVE(marker, inp_list);
2255 LIST_INSERT_AFTER(inp, marker, inp_list);
2257 if (inp->inp_flags & INP_PLACEMARKER)
2258 continue;
2259 if (prison_xinpcb(req->td, inp))
2260 continue;
2262 bzero(&xi, sizeof xi);
2263 xi.xi_len = sizeof xi;
2264 bcopy(inp, &xi.xi_inp, sizeof *inp);
2265 if (inp->inp_socket)
2266 sotoxsocket(inp->inp_socket, &xi.xi_socket);
2267 if ((error = SYSCTL_OUT(req, &xi, sizeof xi)) != 0)
2268 break;
2269 ++i;
2271 LIST_REMOVE(marker, inp_list);
2273 REL_PCBINFO_TOKEN(pcbinfo);
2275 if (error == 0 && i < n) {
2276 bzero(&xi, sizeof xi);
2277 xi.xi_len = sizeof xi;
2278 while (i < n) {
2279 error = SYSCTL_OUT(req, &xi, sizeof xi);
2280 if (error)
2281 break;
2282 ++i;
2287 lwkt_migratecpu(origcpu);
2288 kfree(marker, M_TEMP);
2289 return error;
2293 in_pcblist_ncpus(SYSCTL_HANDLER_ARGS)
2296 return (in_pcblist_range(oidp, arg1, netisr_ncpus, req));
2299 void
2300 in_savefaddr(struct socket *so, const struct sockaddr *faddr)
2302 struct sockaddr_in *sin;
2304 KASSERT(faddr->sa_family == AF_INET,
2305 ("not AF_INET faddr %d", faddr->sa_family));
2307 sin = kmalloc(sizeof(*sin), M_SONAME, M_WAITOK | M_ZERO);
2308 sin->sin_family = AF_INET;
2309 sin->sin_len = sizeof(*sin);
2310 sin->sin_port = ((const struct sockaddr_in *)faddr)->sin_port;
2311 sin->sin_addr = ((const struct sockaddr_in *)faddr)->sin_addr;
2313 so->so_faddr = (struct sockaddr *)sin;
2316 void
2317 in_pcbportinfo_init(struct inpcbportinfo *portinfo, int hashsize,
2318 u_short offset)
2320 memset(portinfo, 0, sizeof(*portinfo));
2322 portinfo->offset = offset;
2323 portinfo->lastport = offset;
2324 portinfo->lastlow = offset;
2325 portinfo->lasthi = offset;
2327 portinfo->porthashbase = hashinit(hashsize, M_PCB,
2328 &portinfo->porthashmask);
2331 void
2332 in_pcbportrange(u_short *hi0, u_short *lo0, u_short ofs, u_short step)
2334 int hi, lo;
2336 if (step == 1)
2337 return;
2339 hi = *hi0;
2340 lo = *lo0;
2342 hi = rounddown(hi, step);
2343 hi += ofs;
2344 if (hi > (int)*hi0)
2345 hi -= step;
2347 lo = roundup(lo, step);
2348 lo -= (step - ofs);
2349 if (lo < (int)*lo0)
2350 lo += step;
2352 *hi0 = hi;
2353 *lo0 = lo;
2356 void
2357 in_pcbglobalinit(void)
2359 int cpu;
2361 in_pcbmarkers = kmalloc(ncpus * sizeof(struct inpcb), M_PCB,
2362 M_WAITOK | M_ZERO);
2363 in_pcbcontainer_markers = kmalloc(ncpus * sizeof(struct inpcontainer),
2364 M_PCB, M_WAITOK | M_ZERO);
2366 for (cpu = 0; cpu < ncpus; ++cpu) {
2367 struct inpcontainer *ic = &in_pcbcontainer_markers[cpu];
2368 struct inpcb *marker = &in_pcbmarkers[cpu];
2370 marker->inp_flags |= INP_PLACEMARKER;
2371 ic->ic_inp = marker;
2375 struct inpcb *
2376 in_pcbmarker(int cpuid)
2378 KASSERT(cpuid >= 0 && cpuid < ncpus, ("invalid cpuid %d", cpuid));
2379 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
2381 return &in_pcbmarkers[cpuid];
2384 struct inpcontainer *
2385 in_pcbcontainer_marker(int cpuid)
2387 KASSERT(cpuid >= 0 && cpuid < ncpus, ("invalid cpuid %d", cpuid));
2388 KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
2390 return &in_pcbcontainer_markers[cpuid];
2393 void
2394 in_pcbresetroute(struct inpcb *inp)
2396 struct route *ro = &inp->inp_route;
2398 if (ro->ro_rt != NULL)
2399 RTFREE(ro->ro_rt);
2400 bzero(ro, sizeof(*ro));
2403 u_short
2404 in_pcblastport_down(volatile u_short *lastport, u_short first, u_short last,
2405 u_short step)
2407 u_short lport;
2409 for (;;) {
2410 u_short olport;
2412 olport = *lastport;
2413 lport = olport - step;
2414 if (__predict_false(lport > first || lport < last))
2415 lport = first;
2416 if (atomic_cmpset_short(lastport, olport, lport))
2417 break;
2419 return lport;
2422 u_short
2423 in_pcblastport_up(volatile u_short *lastport, u_short first, u_short last,
2424 u_short step)
2426 u_short lport;
2428 for (;;) {
2429 u_short olport;
2431 olport = *lastport;
2432 lport = olport + step;
2433 if (__predict_false(lport < first || lport > last))
2434 lport = first;
2435 if (atomic_cmpset_short(lastport, olport, lport))
2436 break;
2438 return lport;