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
66 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
73 #include "opt_inet6.h"
74 #include "opt_ipsec.h"
75 #include "opt_pcbgroup.h"
76 #include "opt_route.h"
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/malloc.h>
83 #include <sys/domain.h>
84 #include <sys/protosw.h>
85 #include <sys/socket.h>
86 #include <sys/socketvar.h>
87 #include <sys/sockio.h>
88 #include <sys/errno.h>
97 #include <net/if_var.h>
98 #include <net/if_llatbl.h>
99 #include <net/if_types.h>
100 #include <net/route.h>
101 #include <net/route/nhop.h>
103 #include <netinet/in.h>
104 #include <netinet/in_var.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/tcp_var.h>
107 #include <netinet/ip6.h>
108 #include <netinet/ip_var.h>
110 #include <netinet6/ip6_var.h>
111 #include <netinet6/nd6.h>
112 #include <netinet/in_pcb.h>
113 #include <netinet6/in6_pcb.h>
114 #include <netinet6/in6_fib.h>
115 #include <netinet6/scope6_var.h>
118 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam,
121 struct socket *so = inp->inp_socket;
122 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
123 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
125 int error, lookupflags = 0;
126 int reuseport = (so->so_options & SO_REUSEPORT);
129 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here
130 * so that we don't have to add to the (already messy) code below.
132 int reuseport_lb = (so->so_options & SO_REUSEPORT_LB);
134 INP_WLOCK_ASSERT(inp);
135 INP_HASH_WLOCK_ASSERT(pcbinfo);
137 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
139 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
140 lookupflags = INPLOOKUP_WILDCARD;
142 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
143 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
146 sin6 = (struct sockaddr_in6 *)nam;
147 KASSERT(sin6->sin6_family == AF_INET6,
148 ("%s: invalid address family for %p", __func__, sin6));
149 KASSERT(sin6->sin6_len == sizeof(*sin6),
150 ("%s: invalid address length for %p", __func__, sin6));
152 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
155 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
156 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
159 lport = sin6->sin6_port;
160 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
162 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
163 * allow compepte duplication of binding if
164 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
165 * and a multicast address is bound on both
166 * new and duplicated sockets.
168 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
169 reuseport = SO_REUSEADDR|SO_REUSEPORT;
171 * XXX: How to deal with SO_REUSEPORT_LB here?
172 * Treat same as SO_REUSEPORT for now.
174 if ((so->so_options &
175 (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0)
176 reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB;
177 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
178 struct epoch_tracker et;
181 sin6->sin6_port = 0; /* yech... */
183 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
185 (inp->inp_flags & INP_BINDANY) == 0) {
187 return (EADDRNOTAVAIL);
191 * XXX: bind to an anycast address might accidentally
192 * cause sending a packet with anycast source address.
193 * We should allow to bind to a deprecated address, since
194 * the application dares to use it.
197 ((struct in6_ifaddr *)ifa)->ia6_flags &
198 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
200 return (EADDRNOTAVAIL);
209 if (ntohs(lport) <= V_ipport_reservedhigh &&
210 ntohs(lport) >= V_ipport_reservedlow &&
211 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
213 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
214 priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) {
215 t = in6_pcblookup_local(pcbinfo,
216 &sin6->sin6_addr, lport,
217 INPLOOKUP_WILDCARD, cred);
219 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
220 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
221 (so->so_type != SOCK_STREAM ||
222 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
223 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
224 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
225 (t->inp_flags2 & INP_REUSEPORT) ||
226 (t->inp_flags2 & INP_REUSEPORT_LB) == 0) &&
227 (inp->inp_cred->cr_uid !=
228 t->inp_cred->cr_uid))
232 * If the socket is a BINDMULTI socket, then
233 * the credentials need to match and the
234 * original socket also has to have been bound
237 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
241 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
242 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
243 struct sockaddr_in sin;
245 in6_sin6_2_sin(&sin, sin6);
246 t = in_pcblookup_local(pcbinfo,
248 INPLOOKUP_WILDCARD, cred);
250 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
252 INP_TIMEWAIT) == 0) &&
253 (so->so_type != SOCK_STREAM ||
254 ntohl(t->inp_faddr.s_addr) ==
256 (inp->inp_cred->cr_uid !=
257 t->inp_cred->cr_uid))
260 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
265 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
266 lport, lookupflags, cred);
267 if (t && (t->inp_flags & INP_TIMEWAIT)) {
269 * XXXRW: If an incpb has had its timewait
270 * state recycled, we treat the address as
271 * being in use (for now). This is better
272 * than a panic, but not desirable.
276 ((reuseport & tw->tw_so_options) == 0 &&
277 (reuseport_lb & tw->tw_so_options) == 0))
279 } else if (t && (reuseport & inp_so_options(t)) == 0 &&
280 (reuseport_lb & inp_so_options(t)) == 0) {
284 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
285 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
286 struct sockaddr_in sin;
288 in6_sin6_2_sin(&sin, sin6);
289 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
290 lport, lookupflags, cred);
291 if (t && t->inp_flags & INP_TIMEWAIT) {
295 if ((reuseport & tw->tw_so_options) == 0
296 && (reuseport_lb & tw->tw_so_options) == 0
297 && (ntohl(t->inp_laddr.s_addr) !=
298 INADDR_ANY || ((inp->inp_vflag &
300 (t->inp_vflag & INP_IPV6PROTO))))
303 (reuseport & inp_so_options(t)) == 0 &&
304 (reuseport_lb & inp_so_options(t)) == 0 &&
305 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
306 (t->inp_vflag & INP_IPV6PROTO) != 0)) {
312 inp->in6p_laddr = sin6->sin6_addr;
315 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
316 /* Undo an address bind that may have occurred. */
317 inp->in6p_laddr = in6addr_any;
321 inp->inp_lport = lport;
322 if (in_pcbinshash(inp) != 0) {
323 inp->in6p_laddr = in6addr_any;
332 * Transform old in6_pcbconnect() into an inner subroutine for new
333 * in6_pcbconnect(): Do some validity-checking on the remote
334 * address (in mbuf 'nam') and then determine local host address
335 * (i.e., which interface) to use to access that remote host.
337 * This preserves definition of in6_pcbconnect(), while supporting a
338 * slightly different version for T/TCP. (This is more than
339 * a bit of a kludge, but cleaning up the internal interfaces would
340 * have forced minor changes in every protocol).
343 in6_pcbladdr(struct inpcb *inp, struct sockaddr_in6 *sin6,
344 struct in6_addr *plocal_addr6)
347 int scope_ambiguous = 0;
348 struct in6_addr in6a;
349 struct epoch_tracker et;
351 INP_WLOCK_ASSERT(inp);
352 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
354 if (sin6->sin6_port == 0)
355 return (EADDRNOTAVAIL);
357 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
359 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
362 if (!CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) {
364 * If the destination address is UNSPECIFIED addr,
365 * use the loopback addr, e.g ::1.
367 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
368 sin6->sin6_addr = in6addr_loopback;
370 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
374 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
375 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
381 * Do not update this earlier, in case we return with an error.
383 * XXX: this in6_selectsrc_socket result might replace the bound local
384 * address with the address specified by setsockopt(IPV6_PKTINFO).
385 * Is it the intended behavior?
387 *plocal_addr6 = in6a;
390 * Don't do pcblookup call here; return interface in
392 * and exit to caller, that will do the lookup.
400 * Connect from a socket to a specified address.
401 * Both address and port must be specified in argument sin.
402 * If don't have a local address for this socket yet,
406 in6_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
407 struct ucred *cred, struct mbuf *m, bool rehash)
409 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
410 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
411 struct sockaddr_in6 laddr6;
414 KASSERT(sin6->sin6_family == AF_INET6,
415 ("%s: invalid address family for %p", __func__, sin6));
416 KASSERT(sin6->sin6_len == sizeof(*sin6),
417 ("%s: invalid address length for %p", __func__, sin6));
419 bzero(&laddr6, sizeof(laddr6));
420 laddr6.sin6_family = AF_INET6;
422 INP_WLOCK_ASSERT(inp);
423 INP_HASH_WLOCK_ASSERT(pcbinfo);
426 if (CALC_FLOWID_OUTBOUND) {
427 uint32_t hash_type, hash_val;
429 hash_val = fib6_calc_software_hash(&inp->in6p_laddr,
430 &sin6->sin6_addr, 0, sin6->sin6_port,
431 inp->inp_socket->so_proto->pr_protocol, &hash_type);
432 inp->inp_flowid = hash_val;
433 inp->inp_flowtype = hash_type;
437 * Call inner routine, to assign local interface address.
438 * in6_pcbladdr() may automatically fill in sin6_scope_id.
440 if ((error = in6_pcbladdr(inp, sin6, &laddr6.sin6_addr)) != 0)
443 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
445 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
446 ? &laddr6.sin6_addr : &inp->in6p_laddr,
447 inp->inp_lport, 0, NULL, M_NODOM) != NULL) {
450 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
451 if (inp->inp_lport == 0) {
453 * rehash was required to be true in the past for
454 * this case; retain that convention. However,
455 * we now call in_pcb_lport_dest rather than
456 * in6_pcbbind; the former does not insert into
457 * the hash table, the latter does. Change rehash
458 * to false to do the in_pcbinshash below.
460 KASSERT(rehash == true,
461 ("Rehashing required for unbound inps"));
463 error = in_pcb_lport_dest(inp,
464 (struct sockaddr *) &laddr6, &inp->inp_lport,
465 (struct sockaddr *) sin6, sin6->sin6_port, cred,
470 inp->in6p_laddr = laddr6.sin6_addr;
472 inp->in6p_faddr = sin6->sin6_addr;
473 inp->inp_fport = sin6->sin6_port;
474 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
475 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
476 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
478 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
481 in_pcbrehash_mbuf(inp, m);
483 in_pcbinshash_mbuf(inp, m);
490 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
493 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL, true));
497 in6_pcbdisconnect(struct inpcb *inp)
500 INP_WLOCK_ASSERT(inp);
501 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
503 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
505 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
506 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
511 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
513 struct sockaddr_in6 *sin6;
515 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
516 bzero(sin6, sizeof *sin6);
517 sin6->sin6_family = AF_INET6;
518 sin6->sin6_len = sizeof(*sin6);
519 sin6->sin6_port = port;
520 sin6->sin6_addr = *addr_p;
521 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
523 return (struct sockaddr *)sin6;
527 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
529 struct sockaddr_in sin;
530 struct sockaddr_in6 *sin6_p;
532 bzero(&sin, sizeof sin);
533 sin.sin_family = AF_INET;
534 sin.sin_len = sizeof(sin);
536 sin.sin_addr = *addr_p;
538 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
540 in6_sin_2_v4mapsin6(&sin, sin6_p);
542 return (struct sockaddr *)sin6_p;
546 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
549 struct in6_addr addr;
553 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
556 port = inp->inp_lport;
557 addr = inp->in6p_laddr;
560 *nam = in6_sockaddr(port, &addr);
565 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
568 struct in6_addr addr;
572 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
575 port = inp->inp_fport;
576 addr = inp->in6p_faddr;
579 *nam = in6_sockaddr(port, &addr);
584 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
590 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
593 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
594 error = in_getsockaddr(so, nam);
596 in6_sin_2_v4mapsin6_in_sock(nam);
600 /* scope issues will be handled in in6_getsockaddr(). */
601 error = in6_getsockaddr(so, nam);
608 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
614 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
617 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
618 error = in_getpeeraddr(so, nam);
620 in6_sin_2_v4mapsin6_in_sock(nam);
623 /* scope issues will be handled in in6_getpeeraddr(). */
624 error = in6_getpeeraddr(so, nam);
630 * Pass some notification to all connections of a protocol
631 * associated with address dst. The local address and/or port numbers
632 * may be specified to limit the search. The "usual action" will be
633 * taken, depending on the ctlinput cmd. The caller must filter any
634 * cmds that are uninteresting (e.g., no error in the map).
635 * Call the protocol specific routine (if any) to report
636 * any errors for each matching socket.
639 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
640 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
641 int cmd, void *cmdarg,
642 struct inpcb *(*notify)(struct inpcb *, int))
644 struct inpcb *inp, *inp_temp;
645 struct sockaddr_in6 sa6_src, *sa6_dst;
646 u_short fport = fport_arg, lport = lport_arg;
650 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
653 sa6_dst = (struct sockaddr_in6 *)dst;
654 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
658 * note that src can be NULL when we get notify by local fragmentation.
660 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
661 flowinfo = sa6_src.sin6_flowinfo;
664 * Redirects go to all references to the destination,
665 * and use in6_rtchange to invalidate the route cache.
666 * Dead host indications: also use in6_rtchange to invalidate
667 * the cache, and deliver the error to all the sockets.
668 * Otherwise, if we have knowledge of the local port and address,
669 * deliver only to that socket.
671 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
674 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
676 if (cmd != PRC_HOSTDEAD)
677 notify = in6_rtchange;
679 errno = inet6ctlerrmap[cmd];
680 INP_INFO_WLOCK(pcbinfo);
681 CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
683 if ((inp->inp_vflag & INP_IPV6) == 0) {
689 * If the error designates a new path MTU for a destination
690 * and the application (associated with this socket) wanted to
691 * know the value, notify.
692 * XXX: should we avoid to notify the value to TCP sockets?
694 if (cmd == PRC_MSGSIZE && cmdarg != NULL)
695 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
696 *(u_int32_t *)cmdarg);
699 * Detect if we should notify the error. If no source and
700 * destination ports are specifed, but non-zero flowinfo and
701 * local address match, notify the error. This is the case
702 * when the error is delivered with an encrypted buffer
703 * by ESP. Otherwise, just compare addresses and ports
706 if (lport == 0 && fport == 0 && flowinfo &&
707 inp->inp_socket != NULL &&
708 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
709 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
711 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
712 &sa6_dst->sin6_addr) ||
713 inp->inp_socket == 0 ||
714 (lport && inp->inp_lport != lport) ||
715 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
716 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
717 &sa6_src.sin6_addr)) ||
718 (fport && inp->inp_fport != fport)) {
725 if ((*notify)(inp, errno))
730 INP_INFO_WUNLOCK(pcbinfo);
734 * Lookup a PCB based on the local address and port. Caller must hold the
735 * hash lock. No inpcb locks or references are acquired.
738 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
739 u_short lport, int lookupflags, struct ucred *cred)
742 int matchwild = 3, wildcard;
744 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
745 ("%s: invalid lookup flags %d", __func__, lookupflags));
747 INP_HASH_LOCK_ASSERT(pcbinfo);
749 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
750 struct inpcbhead *head;
752 * Look for an unconnected (wildcard foreign addr) PCB that
753 * matches the local address and port we're looking for.
755 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
756 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
757 pcbinfo->ipi_hashmask)];
758 CK_LIST_FOREACH(inp, head, inp_hash) {
759 /* XXX inp locking */
760 if ((inp->inp_vflag & INP_IPV6) == 0)
762 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
763 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
764 inp->inp_lport == lport) {
767 prison_equal_ip6(cred->cr_prison,
768 inp->inp_cred->cr_prison))
777 struct inpcbporthead *porthash;
778 struct inpcbport *phd;
779 struct inpcb *match = NULL;
781 * Best fit PCB lookup.
783 * First see if this local port is in use by looking on the
786 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
787 pcbinfo->ipi_porthashmask)];
788 CK_LIST_FOREACH(phd, porthash, phd_hash) {
789 if (phd->phd_port == lport)
794 * Port is in use by one or more PCBs. Look for best
797 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
800 !prison_equal_ip6(cred->cr_prison,
801 inp->inp_cred->cr_prison))
803 /* XXX inp locking */
804 if ((inp->inp_vflag & INP_IPV6) == 0)
806 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
808 if (!IN6_IS_ADDR_UNSPECIFIED(
810 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
812 else if (!IN6_ARE_ADDR_EQUAL(
813 &inp->in6p_laddr, laddr))
816 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
819 if (wildcard < matchwild) {
821 matchwild = wildcard;
832 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
835 struct in6_multi *inm;
836 struct in6_mfilter *imf;
837 struct ip6_moptions *im6o;
839 INP_INFO_WLOCK(pcbinfo);
840 CK_LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
842 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
846 im6o = inp->in6p_moptions;
847 if ((inp->inp_vflag & INP_IPV6) && im6o != NULL) {
849 * Unselect the outgoing ifp for multicast if it
852 if (im6o->im6o_multicast_ifp == ifp)
853 im6o->im6o_multicast_ifp = NULL;
855 * Drop multicast group membership if we joined
856 * through the interface being detached.
859 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) {
860 if ((inm = imf->im6f_in6m) == NULL)
862 if (inm->in6m_ifp != ifp)
864 ip6_mfilter_remove(&im6o->im6o_head, imf);
865 IN6_MULTI_LOCK_ASSERT();
866 in6_leavegroup_locked(inm, NULL);
867 ip6_mfilter_free(imf);
873 INP_INFO_WUNLOCK(pcbinfo);
877 * Check for alternatives when higher level complains
878 * about service problems. For now, invalidate cached
879 * routing information. If the route was created dynamically
880 * (by a redirect), time to try a default gateway again.
883 in6_losing(struct inpcb *inp)
886 RO_INVALIDATE_CACHE(&inp->inp_route6);
890 * After a routing change, flush old routing
891 * and allocate a (hopefully) better one.
894 in6_rtchange(struct inpcb *inp, int errno __unused)
897 RO_INVALIDATE_CACHE(&inp->inp_route6);
901 static struct inpcb *
902 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
903 const struct in6_addr *laddr, uint16_t lport, const struct in6_addr *faddr,
904 uint16_t fport, int lookupflags, uint8_t numa_domain)
906 struct inpcb *local_wild, *numa_wild;
907 const struct inpcblbgrouphead *hdr;
908 struct inpcblbgroup *grp;
911 INP_HASH_LOCK_ASSERT(pcbinfo);
913 hdr = &pcbinfo->ipi_lbgrouphashbase[
914 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
917 * Order of socket selection:
919 * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD).
922 * - Load balanced group does not contain jailed sockets.
923 * - Load balanced does not contain IPv4 mapped INET6 wild sockets.
927 CK_LIST_FOREACH(grp, hdr, il_list) {
929 if (!(grp->il_vflag & INP_IPV6))
932 if (grp->il_lport != lport)
935 idx = INP_PCBLBGROUP_PKTHASH(INP6_PCBHASHKEY(faddr), lport,
936 fport) % grp->il_inpcnt;
937 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr)) {
938 if (numa_domain == M_NODOM ||
939 grp->il_numa_domain == numa_domain) {
940 return (grp->il_inp[idx]);
943 numa_wild = grp->il_inp[idx];
945 if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr) &&
946 (lookupflags & INPLOOKUP_WILDCARD) != 0 &&
947 (local_wild == NULL || numa_domain == M_NODOM ||
948 grp->il_numa_domain == numa_domain)) {
949 local_wild = grp->il_inp[idx];
952 if (numa_wild != NULL)
959 * Lookup PCB in hash list, using pcbgroup tables.
961 static struct inpcb *
962 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
963 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
964 u_int lport_arg, int lookupflags, struct ifnet *ifp)
966 struct inpcbhead *head;
967 struct inpcb *inp, *tmpinp;
968 u_short fport = fport_arg, lport = lport_arg;
972 * First look for an exact match.
975 INP_GROUP_LOCK(pcbgroup);
976 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(
977 INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)];
978 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
979 /* XXX inp locking */
980 if ((inp->inp_vflag & INP_IPV6) == 0)
982 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
983 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
984 inp->inp_fport == fport &&
985 inp->inp_lport == lport) {
987 * XXX We should be able to directly return
988 * the inp here, without any checks.
989 * Well unless both bound with SO_REUSEPORT?
991 if (prison_flag(inp->inp_cred, PR_IP6))
997 if (tmpinp != NULL) {
1003 * Then look for a wildcard match in the pcbgroup.
1005 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1006 struct inpcb *local_wild = NULL, *local_exact = NULL;
1007 struct inpcb *jail_wild = NULL;
1011 * Order of socket selection - we always prefer jails.
1012 * 1. jailed, non-wild.
1014 * 3. non-jailed, non-wild.
1015 * 4. non-jailed, wild.
1017 head = &pcbgroup->ipg_hashbase[
1018 INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)];
1019 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1020 /* XXX inp locking */
1021 if ((inp->inp_vflag & INP_IPV6) == 0)
1024 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1025 inp->inp_lport != lport) {
1029 injail = prison_flag(inp->inp_cred, PR_IP6);
1031 if (prison_check_ip6(inp->inp_cred,
1035 if (local_exact != NULL)
1039 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1044 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1050 } /* LIST_FOREACH */
1064 * Then look for a wildcard match, if requested.
1066 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1067 struct inpcb *local_wild = NULL, *local_exact = NULL;
1068 struct inpcb *jail_wild = NULL;
1072 * Order of socket selection - we always prefer jails.
1073 * 1. jailed, non-wild.
1075 * 3. non-jailed, non-wild.
1076 * 4. non-jailed, wild.
1078 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(
1079 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1080 pcbinfo->ipi_wildmask)];
1081 CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1082 /* XXX inp locking */
1083 if ((inp->inp_vflag & INP_IPV6) == 0)
1086 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1087 inp->inp_lport != lport) {
1091 injail = prison_flag(inp->inp_cred, PR_IP6);
1093 if (prison_check_ip6(inp->inp_cred,
1097 if (local_exact != NULL)
1101 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1106 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1112 } /* LIST_FOREACH */
1123 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1124 INP_GROUP_UNLOCK(pcbgroup);
1128 if (lookupflags & INPLOOKUP_WLOCKPCB)
1129 locked = INP_TRY_WLOCK(inp);
1130 else if (lookupflags & INPLOOKUP_RLOCKPCB)
1131 locked = INP_TRY_RLOCK(inp);
1133 panic("%s: locking buf", __func__);
1136 INP_GROUP_UNLOCK(pcbgroup);
1138 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1140 if (in_pcbrele_wlocked(inp))
1144 if (in_pcbrele_rlocked(inp))
1149 if (lookupflags & INPLOOKUP_WLOCKPCB)
1150 INP_WLOCK_ASSERT(inp);
1152 INP_RLOCK_ASSERT(inp);
1156 #endif /* PCBGROUP */
1159 * Lookup PCB in hash list. Used in in_pcb.c as well as here.
1162 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1163 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1164 int lookupflags, struct ifnet *ifp, uint8_t numa_domain)
1166 struct inpcbhead *head;
1167 struct inpcb *inp, *tmpinp;
1168 u_short fport = fport_arg, lport = lport_arg;
1170 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1171 ("%s: invalid lookup flags %d", __func__, lookupflags));
1173 INP_HASH_LOCK_ASSERT(pcbinfo);
1176 * First look for an exact match.
1179 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1180 INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)];
1181 CK_LIST_FOREACH(inp, head, inp_hash) {
1182 /* XXX inp locking */
1183 if ((inp->inp_vflag & INP_IPV6) == 0)
1185 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1186 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1187 inp->inp_fport == fport &&
1188 inp->inp_lport == lport) {
1190 * XXX We should be able to directly return
1191 * the inp here, without any checks.
1192 * Well unless both bound with SO_REUSEPORT?
1194 if (prison_flag(inp->inp_cred, PR_IP6))
1204 * Then look in lb group (for wildcard match).
1206 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1207 inp = in6_pcblookup_lbgroup(pcbinfo, laddr, lport, faddr,
1208 fport, lookupflags, numa_domain);
1214 * Then look for a wildcard match, if requested.
1216 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1217 struct inpcb *local_wild = NULL, *local_exact = NULL;
1218 struct inpcb *jail_wild = NULL;
1222 * Order of socket selection - we always prefer jails.
1223 * 1. jailed, non-wild.
1225 * 3. non-jailed, non-wild.
1226 * 4. non-jailed, wild.
1228 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1229 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1230 pcbinfo->ipi_hashmask)];
1231 CK_LIST_FOREACH(inp, head, inp_hash) {
1232 /* XXX inp locking */
1233 if ((inp->inp_vflag & INP_IPV6) == 0)
1236 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1237 inp->inp_lport != lport) {
1241 injail = prison_flag(inp->inp_cred, PR_IP6);
1243 if (prison_check_ip6(inp->inp_cred,
1247 if (local_exact != NULL)
1251 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1256 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1262 } /* LIST_FOREACH */
1264 if (jail_wild != NULL)
1266 if (local_exact != NULL)
1267 return (local_exact);
1268 if (local_wild != NULL)
1269 return (local_wild);
1270 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1279 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1280 * hash list lock, and will return the inpcb locked (i.e., requires
1281 * INPLOOKUP_LOCKPCB).
1283 static struct inpcb *
1284 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1285 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1286 struct ifnet *ifp, uint8_t numa_domain)
1290 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1291 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp,
1294 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1296 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1300 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1302 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1307 panic("%s: locking bug", __func__);
1310 if (lookupflags & INPLOOKUP_WLOCKPCB)
1311 INP_WLOCK_ASSERT(inp);
1313 INP_RLOCK_ASSERT(inp);
1321 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1322 * from which a pre-calculated hash value may be extracted.
1324 * Possibly more of this logic should be in in6_pcbgroup.c.
1327 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1328 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1330 #if defined(PCBGROUP) && !defined(RSS)
1331 struct inpcbgroup *pcbgroup;
1334 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1335 ("%s: invalid lookup flags %d", __func__, lookupflags));
1336 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1337 ("%s: LOCKPCB not set", __func__));
1340 * When not using RSS, use connection groups in preference to the
1341 * reservation table when looking up 4-tuples. When using RSS, just
1342 * use the reservation table, due to the cost of the Toeplitz hash
1345 * XXXRW: This policy belongs in the pcbgroup code, as in principle
1346 * we could be doing RSS with a non-Toeplitz hash that is affordable
1349 #if defined(PCBGROUP) && !defined(RSS)
1350 if (in_pcbgroup_enabled(pcbinfo)) {
1351 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1353 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1354 laddr, lport, lookupflags, ifp));
1357 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1358 lookupflags, ifp, M_NODOM));
1362 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1363 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1364 struct ifnet *ifp, struct mbuf *m)
1367 struct inpcbgroup *pcbgroup;
1370 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1371 ("%s: invalid lookup flags %d", __func__, lookupflags));
1372 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1373 ("%s: LOCKPCB not set", __func__));
1377 * If we can use a hardware-generated hash to look up the connection
1378 * group, use that connection group to find the inpcb. Otherwise
1379 * fall back on a software hash -- or the reservation table if we're
1382 * XXXRW: As above, that policy belongs in the pcbgroup code.
1384 if (in_pcbgroup_enabled(pcbinfo) &&
1385 M_HASHTYPE_TEST(m, M_HASHTYPE_NONE) == 0) {
1386 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1387 m->m_pkthdr.flowid);
1388 if (pcbgroup != NULL)
1389 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1390 fport, laddr, lport, lookupflags, ifp));
1392 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1394 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1395 laddr, lport, lookupflags, ifp));
1399 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1400 lookupflags, ifp, m->m_pkthdr.numa_domain));
1404 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1408 ip = mtod(m, struct ip6_hdr *);
1409 bzero(sin6, sizeof(*sin6));
1410 sin6->sin6_len = sizeof(*sin6);
1411 sin6->sin6_family = AF_INET6;
1412 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1414 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */