2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Copyright (c) 2010-2011 Juniper Networks, Inc.
8 * Portions of this software were developed by Robert N. M. Watson under
9 * contract to Juniper Networks, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the project nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
39 * Copyright (c) 1982, 1986, 1991, 1993
40 * The Regents of the University of California. All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 #include <sys/cdefs.h>
69 #include "opt_inet6.h"
70 #include "opt_ipsec.h"
71 #include "opt_route.h"
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
79 #include <sys/domain.h>
81 #include <sys/protosw.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/sockio.h>
86 #include <sys/errno.h>
95 #include <net/if_var.h>
96 #include <net/if_llatbl.h>
97 #include <net/if_types.h>
98 #include <net/route.h>
99 #include <net/route/nhop.h>
101 #include <netinet/in.h>
102 #include <netinet/in_var.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/ip6.h>
105 #include <netinet/ip_var.h>
107 #include <netinet6/ip6_var.h>
108 #include <netinet6/nd6.h>
109 #include <netinet/in_pcb.h>
110 #include <netinet/in_pcb_var.h>
111 #include <netinet6/in6_pcb.h>
112 #include <netinet6/in6_fib.h>
113 #include <netinet6/scope6_var.h>
116 in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct ucred *cred)
118 struct socket *so = inp->inp_socket;
120 int error, lookupflags = 0;
122 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
125 INP_WLOCK_ASSERT(inp);
126 INP_HASH_WLOCK_ASSERT(pcbinfo);
128 error = prison_local_ip6(cred, laddr,
129 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0));
133 /* XXX: this is redundant when called from in6_pcbbind */
134 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
135 lookupflags = INPLOOKUP_WILDCARD;
137 inp->inp_flags |= INP_ANONPORT;
139 error = in_pcb_lport(inp, NULL, &lport, cred, lookupflags);
143 inp->inp_lport = lport;
144 if (in_pcbinshash(inp) != 0) {
145 inp->in6p_laddr = in6addr_any;
154 in6_pcbbind(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred)
156 struct socket *so = inp->inp_socket;
157 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
159 int error, lookupflags = 0;
160 int reuseport = (so->so_options & SO_REUSEPORT);
163 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here
164 * so that we don't have to add to the (already messy) code below.
166 int reuseport_lb = (so->so_options & SO_REUSEPORT_LB);
168 INP_WLOCK_ASSERT(inp);
169 INP_HASH_WLOCK_ASSERT(pcbinfo);
171 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
173 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
174 lookupflags = INPLOOKUP_WILDCARD;
176 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
177 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
180 KASSERT(sin6->sin6_family == AF_INET6,
181 ("%s: invalid address family for %p", __func__, sin6));
182 KASSERT(sin6->sin6_len == sizeof(*sin6),
183 ("%s: invalid address length for %p", __func__, sin6));
185 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
188 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
189 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
192 lport = sin6->sin6_port;
193 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
195 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
196 * allow compepte duplication of binding if
197 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
198 * and a multicast address is bound on both
199 * new and duplicated sockets.
201 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
202 reuseport = SO_REUSEADDR|SO_REUSEPORT;
204 * XXX: How to deal with SO_REUSEPORT_LB here?
205 * Treat same as SO_REUSEPORT for now.
207 if ((so->so_options &
208 (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0)
209 reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB;
210 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
211 struct epoch_tracker et;
214 sin6->sin6_port = 0; /* yech... */
216 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
218 (inp->inp_flags & INP_BINDANY) == 0) {
220 return (EADDRNOTAVAIL);
224 * XXX: bind to an anycast address might accidentally
225 * cause sending a packet with anycast source address.
226 * We should allow to bind to a deprecated address, since
227 * the application dares to use it.
230 ((struct in6_ifaddr *)ifa)->ia6_flags &
231 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
233 return (EADDRNOTAVAIL);
241 if (ntohs(lport) <= V_ipport_reservedhigh &&
242 ntohs(lport) >= V_ipport_reservedlow &&
243 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
245 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
246 priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) {
247 t = in6_pcblookup_local(pcbinfo,
248 &sin6->sin6_addr, lport,
249 INPLOOKUP_WILDCARD, cred);
251 (so->so_type != SOCK_STREAM ||
252 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
253 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
254 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
255 (t->inp_socket->so_options & SO_REUSEPORT) ||
256 (t->inp_socket->so_options & SO_REUSEPORT_LB) == 0) &&
257 (inp->inp_cred->cr_uid !=
258 t->inp_cred->cr_uid))
262 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
263 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
264 struct sockaddr_in sin;
266 in6_sin6_2_sin(&sin, sin6);
267 t = in_pcblookup_local(pcbinfo,
269 INPLOOKUP_WILDCARD, cred);
271 (so->so_type != SOCK_STREAM ||
272 ntohl(t->inp_faddr.s_addr) ==
274 (inp->inp_cred->cr_uid !=
275 t->inp_cred->cr_uid))
280 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
281 lport, lookupflags, cred);
282 if (t && (reuseport & t->inp_socket->so_options) == 0 &&
283 (reuseport_lb & t->inp_socket->so_options) == 0) {
287 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
288 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
289 struct sockaddr_in sin;
291 in6_sin6_2_sin(&sin, sin6);
292 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
293 lport, lookupflags, cred);
295 (reuseport & t->inp_socket->so_options) == 0 &&
296 (reuseport_lb & t->inp_socket->so_options) == 0 &&
297 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
298 (t->inp_vflag & INP_IPV6PROTO) != 0)) {
304 inp->in6p_laddr = sin6->sin6_addr;
307 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
308 /* Undo an address bind that may have occurred. */
309 inp->in6p_laddr = in6addr_any;
313 inp->inp_lport = lport;
314 if (in_pcbinshash(inp) != 0) {
315 inp->in6p_laddr = in6addr_any;
324 * Transform old in6_pcbconnect() into an inner subroutine for new
325 * in6_pcbconnect(): Do some validity-checking on the remote
326 * address (in mbuf 'nam') and then determine local host address
327 * (i.e., which interface) to use to access that remote host.
329 * This preserves definition of in6_pcbconnect(), while supporting a
330 * slightly different version for T/TCP. (This is more than
331 * a bit of a kludge, but cleaning up the internal interfaces would
332 * have forced minor changes in every protocol).
335 in6_pcbladdr(struct inpcb *inp, struct sockaddr_in6 *sin6,
336 struct in6_addr *plocal_addr6, bool sas_required)
339 int scope_ambiguous = 0;
340 struct in6_addr in6a;
343 INP_WLOCK_ASSERT(inp);
344 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
346 if (sin6->sin6_port == 0)
347 return (EADDRNOTAVAIL);
349 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
351 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
354 if (!CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) {
356 * If the destination address is UNSPECIFIED addr,
357 * use the loopback addr, e.g ::1.
359 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
360 sin6->sin6_addr = in6addr_loopback;
362 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
366 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
367 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
372 * Source address selection isn't required when syncache
373 * has already established connection and both source and
374 * destination addresses was chosen.
376 * This also includes the case when fwd_tag was used to
377 * select source address in tcp_input().
379 in6a = inp->in6p_laddr;
382 if (IN6_IS_ADDR_UNSPECIFIED(&in6a))
383 return (EHOSTUNREACH);
385 * Do not update this earlier, in case we return with an error.
387 * XXX: this in6_selectsrc_socket result might replace the bound local
388 * address with the address specified by setsockopt(IPV6_PKTINFO).
389 * Is it the intended behavior?
391 *plocal_addr6 = in6a;
394 * Don't do pcblookup call here; return interface in
396 * and exit to caller, that will do the lookup.
404 * Connect from a socket to a specified address.
405 * Both address and port must be specified in argument sin.
406 * If don't have a local address for this socket yet,
410 in6_pcbconnect(struct inpcb *inp, struct sockaddr_in6 *sin6, struct ucred *cred,
413 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
414 struct sockaddr_in6 laddr6;
418 INP_WLOCK_ASSERT(inp);
419 INP_HASH_WLOCK_ASSERT(pcbinfo);
420 KASSERT(sin6->sin6_family == AF_INET6,
421 ("%s: invalid address family for %p", __func__, sin6));
422 KASSERT(sin6->sin6_len == sizeof(*sin6),
423 ("%s: invalid address length for %p", __func__, sin6));
424 KASSERT(IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr),
425 ("%s: inp is already connected", __func__));
427 bzero(&laddr6, sizeof(laddr6));
428 laddr6.sin6_family = AF_INET6;
431 if (CALC_FLOWID_OUTBOUND) {
432 uint32_t hash_type, hash_val;
434 hash_val = fib6_calc_software_hash(&inp->in6p_laddr,
435 &sin6->sin6_addr, 0, sin6->sin6_port,
436 inp->inp_socket->so_proto->pr_protocol, &hash_type);
437 inp->inp_flowid = hash_val;
438 inp->inp_flowtype = hash_type;
442 * Call inner routine, to assign local interface address.
443 * in6_pcbladdr() may automatically fill in sin6_scope_id.
445 if ((error = in6_pcbladdr(inp, sin6, &laddr6.sin6_addr,
449 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
450 sin6->sin6_port, IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) ?
451 &laddr6.sin6_addr : &inp->in6p_laddr, inp->inp_lport, 0,
454 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
455 if (inp->inp_lport == 0) {
456 error = in_pcb_lport_dest(inp,
457 (struct sockaddr *) &laddr6, &inp->inp_lport,
458 (struct sockaddr *) sin6, sin6->sin6_port, cred,
463 inp->in6p_laddr = laddr6.sin6_addr;
465 inp->in6p_faddr = sin6->sin6_addr;
466 inp->inp_fport = sin6->sin6_port;
467 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
468 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
469 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
471 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
473 if ((inp->inp_flags & INP_INHASHLIST) != 0) {
483 in6_pcbdisconnect(struct inpcb *inp)
486 INP_WLOCK_ASSERT(inp);
487 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
488 KASSERT(inp->inp_smr == SMR_SEQ_INVALID,
489 ("%s: inp %p was already disconnected", __func__, inp));
491 in_pcbremhash_locked(inp);
493 /* See the comment in in_pcbinshash(). */
494 inp->inp_smr = smr_advance(inp->inp_pcbinfo->ipi_smr);
496 /* XXX-MJ torn writes are visible to SMR lookup */
497 memset(&inp->in6p_laddr, 0, sizeof(inp->in6p_laddr));
498 memset(&inp->in6p_faddr, 0, sizeof(inp->in6p_faddr));
500 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
501 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
505 in6_getsockaddr(struct socket *so, struct sockaddr *sa)
510 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
512 *(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
513 .sin6_len = sizeof(struct sockaddr_in6),
514 .sin6_family = AF_INET6,
515 .sin6_port = inp->inp_lport,
516 .sin6_addr = inp->in6p_laddr,
518 /* XXX: should catch errors */
519 (void)sa6_recoverscope((struct sockaddr_in6 *)sa);
525 in6_getpeeraddr(struct socket *so, struct sockaddr *sa)
530 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
532 *(struct sockaddr_in6 *)sa = (struct sockaddr_in6 ){
533 .sin6_len = sizeof(struct sockaddr_in6),
534 .sin6_family = AF_INET6,
535 .sin6_port = inp->inp_fport,
536 .sin6_addr = inp->in6p_faddr,
538 /* XXX: should catch errors */
539 (void)sa6_recoverscope((struct sockaddr_in6 *)sa);
545 in6_mapped_sockaddr(struct socket *so, struct sockaddr *sa)
552 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
554 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
555 struct sockaddr_in sin;
557 error = in_getsockaddr(so, (struct sockaddr *)&sin);
559 in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
563 /* scope issues will be handled in in6_getsockaddr(). */
564 error = in6_getsockaddr(so, sa);
571 in6_mapped_peeraddr(struct socket *so, struct sockaddr *sa)
578 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
580 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
581 struct sockaddr_in sin;
583 error = in_getpeeraddr(so, (struct sockaddr *)&sin);
585 in6_sin_2_v4mapsin6(&sin, (struct sockaddr_in6 *)sa);
589 /* scope issues will be handled in in6_getpeeraddr(). */
590 error = in6_getpeeraddr(so, sa);
597 * Pass some notification to all connections of a protocol
598 * associated with address dst. The local address and/or port numbers
599 * may be specified to limit the search. The "usual action" will be
600 * taken, depending on the ctlinput cmd. The caller must filter any
601 * cmds that are uninteresting (e.g., no error in the map).
602 * Call the protocol specific routine (if any) to report
603 * any errors for each matching socket.
606 inp_match6(const struct inpcb *inp, void *v __unused)
609 return ((inp->inp_vflag & INP_IPV6) != 0);
613 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr_in6 *sa6_dst,
614 u_int fport_arg, const struct sockaddr_in6 *src, u_int lport_arg,
615 int errno, void *cmdarg,
616 struct inpcb *(*notify)(struct inpcb *, int))
618 struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB,
621 struct sockaddr_in6 sa6_src;
622 u_short fport = fport_arg, lport = lport_arg;
625 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
629 * note that src can be NULL when we get notify by local fragmentation.
631 sa6_src = (src == NULL) ? sa6_any : *src;
632 flowinfo = sa6_src.sin6_flowinfo;
634 while ((inp = inp_next(&inpi)) != NULL) {
635 INP_WLOCK_ASSERT(inp);
637 * If the error designates a new path MTU for a destination
638 * and the application (associated with this socket) wanted to
639 * know the value, notify.
640 * XXX: should we avoid to notify the value to TCP sockets?
642 if (errno == EMSGSIZE && cmdarg != NULL)
643 ip6_notify_pmtu(inp, sa6_dst, *(uint32_t *)cmdarg);
646 * Detect if we should notify the error. If no source and
647 * destination ports are specified, but non-zero flowinfo and
648 * local address match, notify the error. This is the case
649 * when the error is delivered with an encrypted buffer
650 * by ESP. Otherwise, just compare addresses and ports
653 if (lport == 0 && fport == 0 && flowinfo &&
654 inp->inp_socket != NULL &&
655 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
656 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
658 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
659 &sa6_dst->sin6_addr) ||
660 inp->inp_socket == 0 ||
661 (lport && inp->inp_lport != lport) ||
662 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
663 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
664 &sa6_src.sin6_addr)) ||
665 (fport && inp->inp_fport != fport)) {
671 (*notify)(inp, errno);
676 * Lookup a PCB based on the local address and port. Caller must hold the
677 * hash lock. No inpcb locks or references are acquired.
680 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
681 u_short lport, int lookupflags, struct ucred *cred)
684 int matchwild = 3, wildcard;
686 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
687 ("%s: invalid lookup flags %d", __func__, lookupflags));
689 INP_HASH_LOCK_ASSERT(pcbinfo);
691 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
692 struct inpcbhead *head;
694 * Look for an unconnected (wildcard foreign addr) PCB that
695 * matches the local address and port we're looking for.
697 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
698 pcbinfo->ipi_hashmask)];
699 CK_LIST_FOREACH(inp, head, inp_hash_wild) {
700 /* XXX inp locking */
701 if ((inp->inp_vflag & INP_IPV6) == 0)
703 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
704 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
705 inp->inp_lport == lport) {
707 if (prison_equal_ip6(cred->cr_prison,
708 inp->inp_cred->cr_prison))
717 struct inpcbporthead *porthash;
718 struct inpcbport *phd;
719 struct inpcb *match = NULL;
721 * Best fit PCB lookup.
723 * First see if this local port is in use by looking on the
726 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
727 pcbinfo->ipi_porthashmask)];
728 CK_LIST_FOREACH(phd, porthash, phd_hash) {
729 if (phd->phd_port == lport)
734 * Port is in use by one or more PCBs. Look for best
737 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
739 if (!prison_equal_ip6(cred->cr_prison,
740 inp->inp_cred->cr_prison))
742 /* XXX inp locking */
743 if ((inp->inp_vflag & INP_IPV6) == 0)
745 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
747 if (!IN6_IS_ADDR_UNSPECIFIED(
749 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
751 else if (!IN6_ARE_ADDR_EQUAL(
752 &inp->in6p_laddr, laddr))
755 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
758 if (wildcard < matchwild) {
760 matchwild = wildcard;
771 in6_multi_match(const struct inpcb *inp, void *v __unused)
774 if ((inp->inp_vflag & INP_IPV6) && inp->in6p_moptions != NULL)
781 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
783 struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_RLOCKPCB,
784 in6_multi_match, NULL);
786 struct in6_multi *inm;
787 struct in6_mfilter *imf;
788 struct ip6_moptions *im6o;
790 IN6_MULTI_LOCK_ASSERT();
792 while ((inp = inp_next(&inpi)) != NULL) {
793 INP_RLOCK_ASSERT(inp);
795 im6o = inp->in6p_moptions;
797 * Unselect the outgoing ifp for multicast if it
800 if (im6o->im6o_multicast_ifp == ifp)
801 im6o->im6o_multicast_ifp = NULL;
803 * Drop multicast group membership if we joined
804 * through the interface being detached.
807 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) {
808 if ((inm = imf->im6f_in6m) == NULL)
810 if (inm->in6m_ifp != ifp)
812 ip6_mfilter_remove(&im6o->im6o_head, imf);
813 in6_leavegroup_locked(inm, NULL);
814 ip6_mfilter_free(imf);
821 * Check for alternatives when higher level complains
822 * about service problems. For now, invalidate cached
823 * routing information. If the route was created dynamically
824 * (by a redirect), time to try a default gateway again.
827 in6_losing(struct inpcb *inp)
830 RO_INVALIDATE_CACHE(&inp->inp_route6);
834 * After a routing change, flush old routing
835 * and allocate a (hopefully) better one.
838 in6_rtchange(struct inpcb *inp, int errno __unused)
841 RO_INVALIDATE_CACHE(&inp->inp_route6);
846 in6_pcblookup_lb_numa_match(const struct inpcblbgroup *grp, int domain)
848 return (domain == M_NODOM || domain == grp->il_numa_domain);
851 static struct inpcb *
852 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
853 const struct in6_addr *faddr, uint16_t fport, const struct in6_addr *laddr,
854 uint16_t lport, uint8_t domain)
856 const struct inpcblbgrouphead *hdr;
857 struct inpcblbgroup *grp;
858 struct inpcblbgroup *jail_exact, *jail_wild, *local_exact, *local_wild;
860 INP_HASH_LOCK_ASSERT(pcbinfo);
862 hdr = &pcbinfo->ipi_lbgrouphashbase[
863 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
866 * Search for an LB group match based on the following criteria:
867 * - prefer jailed groups to non-jailed groups
868 * - prefer exact source address matches to wildcard matches
869 * - prefer groups bound to the specified NUMA domain
871 jail_exact = jail_wild = local_exact = local_wild = NULL;
872 CK_LIST_FOREACH(grp, hdr, il_list) {
876 if (!(grp->il_vflag & INP_IPV6))
879 if (grp->il_lport != lport)
882 injail = prison_flag(grp->il_cred, PR_IP6) != 0;
883 if (injail && prison_check_ip6_locked(grp->il_cred->cr_prison,
887 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr)) {
890 if (in6_pcblookup_lb_numa_match(grp, domain))
891 /* This is a perfect match. */
893 } else if (local_exact == NULL ||
894 in6_pcblookup_lb_numa_match(grp, domain)) {
897 } else if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr)) {
899 if (jail_wild == NULL ||
900 in6_pcblookup_lb_numa_match(grp, domain))
902 } else if (local_wild == NULL ||
903 in6_pcblookup_lb_numa_match(grp, domain)) {
909 if (jail_exact != NULL)
911 else if (jail_wild != NULL)
913 else if (local_exact != NULL)
920 return (grp->il_inp[INP6_PCBLBGROUP_PKTHASH(faddr, lport, fport) %
925 in6_pcblookup_exact_match(const struct inpcb *inp, const struct in6_addr *faddr,
926 u_short fport, const struct in6_addr *laddr, u_short lport)
928 /* XXX inp locking */
929 if ((inp->inp_vflag & INP_IPV6) == 0)
931 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
932 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
933 inp->inp_fport == fport && inp->inp_lport == lport)
938 static struct inpcb *
939 in6_pcblookup_hash_exact(struct inpcbinfo *pcbinfo,
940 const struct in6_addr *faddr, u_short fport,
941 const struct in6_addr *laddr, u_short lport)
943 struct inpcbhead *head;
946 INP_HASH_LOCK_ASSERT(pcbinfo);
949 * First look for an exact match.
951 head = &pcbinfo->ipi_hash_exact[INP6_PCBHASH(faddr, lport, fport,
952 pcbinfo->ipi_hashmask)];
953 CK_LIST_FOREACH(inp, head, inp_hash_exact) {
954 if (in6_pcblookup_exact_match(inp, faddr, fport, laddr, lport))
961 INPLOOKUP_MATCH_NONE = 0,
962 INPLOOKUP_MATCH_WILD = 1,
963 INPLOOKUP_MATCH_LADDR = 2,
964 } inp_lookup_match_t;
966 static inp_lookup_match_t
967 in6_pcblookup_wild_match(const struct inpcb *inp, const struct in6_addr *laddr,
970 /* XXX inp locking */
971 if ((inp->inp_vflag & INP_IPV6) == 0)
972 return (INPLOOKUP_MATCH_NONE);
973 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
974 inp->inp_lport != lport)
975 return (INPLOOKUP_MATCH_NONE);
976 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
977 return (INPLOOKUP_MATCH_WILD);
978 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr))
979 return (INPLOOKUP_MATCH_LADDR);
980 return (INPLOOKUP_MATCH_NONE);
983 #define INP_LOOKUP_AGAIN ((struct inpcb *)(uintptr_t)-1)
985 static struct inpcb *
986 in6_pcblookup_hash_wild_smr(struct inpcbinfo *pcbinfo,
987 const struct in6_addr *faddr, u_short fport, const struct in6_addr *laddr,
988 u_short lport, const inp_lookup_t lockflags)
990 struct inpcbhead *head;
993 KASSERT(SMR_ENTERED(pcbinfo->ipi_smr),
994 ("%s: not in SMR read section", __func__));
996 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
997 pcbinfo->ipi_hashmask)];
998 CK_LIST_FOREACH(inp, head, inp_hash_wild) {
999 inp_lookup_match_t match;
1001 match = in6_pcblookup_wild_match(inp, laddr, lport);
1002 if (match == INPLOOKUP_MATCH_NONE)
1005 if (__predict_true(inp_smr_lock(inp, lockflags))) {
1006 match = in6_pcblookup_wild_match(inp, laddr, lport);
1007 if (match != INPLOOKUP_MATCH_NONE &&
1008 prison_check_ip6_locked(inp->inp_cred->cr_prison,
1011 inp_unlock(inp, lockflags);
1015 * The matching socket disappeared out from under us. Fall back
1016 * to a serialized lookup.
1018 return (INP_LOOKUP_AGAIN);
1023 static struct inpcb *
1024 in6_pcblookup_hash_wild_locked(struct inpcbinfo *pcbinfo,
1025 const struct in6_addr *faddr, u_short fport, const struct in6_addr *laddr,
1028 struct inpcbhead *head;
1029 struct inpcb *inp, *jail_wild, *local_exact, *local_wild;
1031 INP_HASH_LOCK_ASSERT(pcbinfo);
1034 * Order of socket selection - we always prefer jails.
1035 * 1. jailed, non-wild.
1037 * 3. non-jailed, non-wild.
1038 * 4. non-jailed, wild.
1040 head = &pcbinfo->ipi_hash_wild[INP_PCBHASH_WILD(lport,
1041 pcbinfo->ipi_hashmask)];
1042 local_wild = local_exact = jail_wild = NULL;
1043 CK_LIST_FOREACH(inp, head, inp_hash_wild) {
1044 inp_lookup_match_t match;
1047 match = in6_pcblookup_wild_match(inp, laddr, lport);
1048 if (match == INPLOOKUP_MATCH_NONE)
1051 injail = prison_flag(inp->inp_cred, PR_IP6) != 0;
1053 if (prison_check_ip6_locked(
1054 inp->inp_cred->cr_prison, laddr) != 0)
1057 if (local_exact != NULL)
1061 if (match == INPLOOKUP_MATCH_LADDR) {
1074 if (jail_wild != NULL)
1076 if (local_exact != NULL)
1077 return (local_exact);
1078 if (local_wild != NULL)
1079 return (local_wild);
1084 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo,
1085 const struct in6_addr *faddr, u_int fport_arg,
1086 const struct in6_addr *laddr, u_int lport_arg,
1087 int lookupflags, uint8_t numa_domain)
1090 u_short fport = fport_arg, lport = lport_arg;
1092 KASSERT((lookupflags & ~INPLOOKUP_WILDCARD) == 0,
1093 ("%s: invalid lookup flags %d", __func__, lookupflags));
1094 KASSERT(!IN6_IS_ADDR_UNSPECIFIED(faddr),
1095 ("%s: invalid foreign address", __func__));
1096 KASSERT(!IN6_IS_ADDR_UNSPECIFIED(laddr),
1097 ("%s: invalid local address", __func__));
1098 INP_HASH_LOCK_ASSERT(pcbinfo);
1100 inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport);
1104 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1105 inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport, laddr,
1106 lport, numa_domain);
1108 inp = in6_pcblookup_hash_wild_locked(pcbinfo, faddr,
1109 fport, laddr, lport);
1115 static struct inpcb *
1116 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, const struct in6_addr *faddr,
1117 u_int fport, const struct in6_addr *laddr, u_int lport, int lookupflags,
1118 uint8_t numa_domain)
1121 const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK;
1123 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1124 ("%s: LOCKPCB not set", __func__));
1126 INP_HASH_WLOCK(pcbinfo);
1127 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1128 lookupflags & ~INPLOOKUP_LOCKMASK, numa_domain);
1129 if (inp != NULL && !inp_trylock(inp, lockflags)) {
1131 INP_HASH_WUNLOCK(pcbinfo);
1132 inp_lock(inp, lockflags);
1133 if (in_pcbrele(inp, lockflags))
1134 /* XXX-MJ or retry until we get a negative match? */
1137 INP_HASH_WUNLOCK(pcbinfo);
1142 static struct inpcb *
1143 in6_pcblookup_hash_smr(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1144 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int lookupflags,
1145 uint8_t numa_domain)
1148 const inp_lookup_t lockflags = lookupflags & INPLOOKUP_LOCKMASK;
1149 const u_short fport = fport_arg, lport = lport_arg;
1151 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1152 ("%s: invalid lookup flags %d", __func__, lookupflags));
1153 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1154 ("%s: LOCKPCB not set", __func__));
1156 smr_enter(pcbinfo->ipi_smr);
1157 inp = in6_pcblookup_hash_exact(pcbinfo, faddr, fport, laddr, lport);
1159 if (__predict_true(inp_smr_lock(inp, lockflags))) {
1160 if (__predict_true(in6_pcblookup_exact_match(inp,
1161 faddr, fport, laddr, lport)))
1163 inp_unlock(inp, lockflags);
1166 * We failed to lock the inpcb, or its connection state changed
1167 * out from under us. Fall back to a precise search.
1169 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1170 lookupflags, numa_domain));
1173 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1174 inp = in6_pcblookup_lbgroup(pcbinfo, faddr, fport,
1175 laddr, lport, numa_domain);
1177 if (__predict_true(inp_smr_lock(inp, lockflags))) {
1178 if (__predict_true(in6_pcblookup_wild_match(inp,
1179 laddr, lport) != INPLOOKUP_MATCH_NONE))
1181 inp_unlock(inp, lockflags);
1183 inp = INP_LOOKUP_AGAIN;
1185 inp = in6_pcblookup_hash_wild_smr(pcbinfo, faddr, fport,
1186 laddr, lport, lockflags);
1188 if (inp == INP_LOOKUP_AGAIN) {
1189 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr,
1190 lport, lookupflags, numa_domain));
1195 smr_exit(pcbinfo->ipi_smr);
1201 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1202 * from which a pre-calculated hash value may be extracted.
1205 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1206 struct in6_addr *laddr, u_int lport, int lookupflags,
1207 struct ifnet *ifp __unused)
1209 return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport,
1210 lookupflags, M_NODOM));
1214 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1215 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1216 struct ifnet *ifp __unused, struct mbuf *m)
1218 return (in6_pcblookup_hash_smr(pcbinfo, faddr, fport, laddr, lport,
1219 lookupflags, m->m_pkthdr.numa_domain));
1223 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1227 ip = mtod(m, struct ip6_hdr *);
1228 bzero(sin6, sizeof(*sin6));
1229 sin6->sin6_len = sizeof(*sin6);
1230 sin6->sin6_family = AF_INET6;
1231 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1233 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */