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>
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
73 #include "opt_pcbgroup.h"
74 #include "opt_route.h"
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/malloc.h>
81 #include <sys/domain.h>
82 #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/tcp_var.h>
105 #include <netinet/ip6.h>
106 #include <netinet/ip_var.h>
108 #include <netinet6/ip6_var.h>
109 #include <netinet6/nd6.h>
110 #include <netinet/in_pcb.h>
111 #include <netinet6/in6_pcb.h>
112 #include <netinet6/in6_fib.h>
113 #include <netinet6/scope6_var.h>
116 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam,
119 struct socket *so = inp->inp_socket;
120 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
121 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
123 int error, lookupflags = 0;
124 int reuseport = (so->so_options & SO_REUSEPORT);
127 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here
128 * so that we don't have to add to the (already messy) code below.
130 int reuseport_lb = (so->so_options & SO_REUSEPORT_LB);
132 INP_WLOCK_ASSERT(inp);
133 INP_HASH_WLOCK_ASSERT(pcbinfo);
135 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
137 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
138 lookupflags = INPLOOKUP_WILDCARD;
140 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
141 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
144 sin6 = (struct sockaddr_in6 *)nam;
145 KASSERT(sin6->sin6_family == AF_INET6,
146 ("%s: invalid address family for %p", __func__, sin6));
147 KASSERT(sin6->sin6_len == sizeof(*sin6),
148 ("%s: invalid address length for %p", __func__, sin6));
150 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
153 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
154 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
157 lport = sin6->sin6_port;
158 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
160 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
161 * allow compepte duplication of binding if
162 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
163 * and a multicast address is bound on both
164 * new and duplicated sockets.
166 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
167 reuseport = SO_REUSEADDR|SO_REUSEPORT;
169 * XXX: How to deal with SO_REUSEPORT_LB here?
170 * Treat same as SO_REUSEPORT for now.
172 if ((so->so_options &
173 (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0)
174 reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB;
175 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
176 struct epoch_tracker et;
179 sin6->sin6_port = 0; /* yech... */
181 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
183 (inp->inp_flags & INP_BINDANY) == 0) {
185 return (EADDRNOTAVAIL);
189 * XXX: bind to an anycast address might accidentally
190 * cause sending a packet with anycast source address.
191 * We should allow to bind to a deprecated address, since
192 * the application dares to use it.
195 ((struct in6_ifaddr *)ifa)->ia6_flags &
196 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
198 return (EADDRNOTAVAIL);
207 if (ntohs(lport) <= V_ipport_reservedhigh &&
208 ntohs(lport) >= V_ipport_reservedlow &&
209 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
211 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
212 priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) {
213 t = in6_pcblookup_local(pcbinfo,
214 &sin6->sin6_addr, lport,
215 INPLOOKUP_WILDCARD, cred);
217 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
218 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
219 (so->so_type != SOCK_STREAM ||
220 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
221 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
222 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
223 (t->inp_flags2 & INP_REUSEPORT) ||
224 (t->inp_flags2 & INP_REUSEPORT_LB) == 0) &&
225 (inp->inp_cred->cr_uid !=
226 t->inp_cred->cr_uid))
230 * If the socket is a BINDMULTI socket, then
231 * the credentials need to match and the
232 * original socket also has to have been bound
235 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
239 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
240 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
241 struct sockaddr_in sin;
243 in6_sin6_2_sin(&sin, sin6);
244 t = in_pcblookup_local(pcbinfo,
246 INPLOOKUP_WILDCARD, cred);
248 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
250 INP_TIMEWAIT) == 0) &&
251 (so->so_type != SOCK_STREAM ||
252 ntohl(t->inp_faddr.s_addr) ==
254 (inp->inp_cred->cr_uid !=
255 t->inp_cred->cr_uid))
258 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
263 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
264 lport, lookupflags, cred);
265 if (t && (t->inp_flags & INP_TIMEWAIT)) {
267 * XXXRW: If an incpb has had its timewait
268 * state recycled, we treat the address as
269 * being in use (for now). This is better
270 * than a panic, but not desirable.
274 ((reuseport & tw->tw_so_options) == 0 &&
275 (reuseport_lb & tw->tw_so_options) == 0))
277 } else if (t && (reuseport & inp_so_options(t)) == 0 &&
278 (reuseport_lb & inp_so_options(t)) == 0) {
282 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
283 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
284 struct sockaddr_in sin;
286 in6_sin6_2_sin(&sin, sin6);
287 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
288 lport, lookupflags, cred);
289 if (t && t->inp_flags & INP_TIMEWAIT) {
293 if ((reuseport & tw->tw_so_options) == 0
294 && (reuseport_lb & tw->tw_so_options) == 0
295 && (ntohl(t->inp_laddr.s_addr) !=
296 INADDR_ANY || ((inp->inp_vflag &
298 (t->inp_vflag & INP_IPV6PROTO))))
301 (reuseport & inp_so_options(t)) == 0 &&
302 (reuseport_lb & inp_so_options(t)) == 0 &&
303 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
304 (t->inp_vflag & INP_IPV6PROTO) != 0)) {
310 inp->in6p_laddr = sin6->sin6_addr;
313 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
314 /* Undo an address bind that may have occurred. */
315 inp->in6p_laddr = in6addr_any;
319 inp->inp_lport = lport;
320 if (in_pcbinshash(inp) != 0) {
321 inp->in6p_laddr = in6addr_any;
330 * Transform old in6_pcbconnect() into an inner subroutine for new
331 * in6_pcbconnect(): Do some validity-checking on the remote
332 * address (in mbuf 'nam') and then determine local host address
333 * (i.e., which interface) to use to access that remote host.
335 * This preserves definition of in6_pcbconnect(), while supporting a
336 * slightly different version for T/TCP. (This is more than
337 * a bit of a kludge, but cleaning up the internal interfaces would
338 * have forced minor changes in every protocol).
341 in6_pcbladdr(struct inpcb *inp, struct sockaddr_in6 *sin6,
342 struct in6_addr *plocal_addr6)
345 int scope_ambiguous = 0;
346 struct in6_addr in6a;
347 struct epoch_tracker et;
349 INP_WLOCK_ASSERT(inp);
350 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
352 if (sin6->sin6_port == 0)
353 return (EADDRNOTAVAIL);
355 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
357 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
360 if (!CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) {
362 * If the destination address is UNSPECIFIED addr,
363 * use the loopback addr, e.g ::1.
365 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
366 sin6->sin6_addr = in6addr_loopback;
368 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
372 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
373 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
377 if (IN6_IS_ADDR_UNSPECIFIED(&in6a))
378 return (EHOSTUNREACH);
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 memset(&inp->in6p_laddr, 0, sizeof(inp->in6p_laddr));
504 memset(&inp->in6p_faddr, 0, sizeof(inp->in6p_faddr));
506 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
507 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
512 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
514 struct sockaddr_in6 *sin6;
516 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
517 bzero(sin6, sizeof *sin6);
518 sin6->sin6_family = AF_INET6;
519 sin6->sin6_len = sizeof(*sin6);
520 sin6->sin6_port = port;
521 sin6->sin6_addr = *addr_p;
522 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
524 return (struct sockaddr *)sin6;
528 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
530 struct sockaddr_in sin;
531 struct sockaddr_in6 *sin6_p;
533 bzero(&sin, sizeof sin);
534 sin.sin_family = AF_INET;
535 sin.sin_len = sizeof(sin);
537 sin.sin_addr = *addr_p;
539 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
541 in6_sin_2_v4mapsin6(&sin, sin6_p);
543 return (struct sockaddr *)sin6_p;
547 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
550 struct in6_addr addr;
554 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
557 port = inp->inp_lport;
558 addr = inp->in6p_laddr;
561 *nam = in6_sockaddr(port, &addr);
566 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
569 struct in6_addr addr;
573 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
576 port = inp->inp_fport;
577 addr = inp->in6p_faddr;
580 *nam = in6_sockaddr(port, &addr);
585 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
591 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
594 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
595 error = in_getsockaddr(so, nam);
597 in6_sin_2_v4mapsin6_in_sock(nam);
601 /* scope issues will be handled in in6_getsockaddr(). */
602 error = in6_getsockaddr(so, nam);
609 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
615 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
618 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
619 error = in_getpeeraddr(so, nam);
621 in6_sin_2_v4mapsin6_in_sock(nam);
624 /* scope issues will be handled in in6_getpeeraddr(). */
625 error = in6_getpeeraddr(so, nam);
631 * Pass some notification to all connections of a protocol
632 * associated with address dst. The local address and/or port numbers
633 * may be specified to limit the search. The "usual action" will be
634 * taken, depending on the ctlinput cmd. The caller must filter any
635 * cmds that are uninteresting (e.g., no error in the map).
636 * Call the protocol specific routine (if any) to report
637 * any errors for each matching socket.
640 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
641 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
642 int cmd, void *cmdarg,
643 struct inpcb *(*notify)(struct inpcb *, int))
645 struct inpcb *inp, *inp_temp;
646 struct sockaddr_in6 sa6_src, *sa6_dst;
647 u_short fport = fport_arg, lport = lport_arg;
651 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
654 sa6_dst = (struct sockaddr_in6 *)dst;
655 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
659 * note that src can be NULL when we get notify by local fragmentation.
661 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
662 flowinfo = sa6_src.sin6_flowinfo;
665 * Redirects go to all references to the destination,
666 * and use in6_rtchange to invalidate the route cache.
667 * Dead host indications: also use in6_rtchange to invalidate
668 * the cache, and deliver the error to all the sockets.
669 * Otherwise, if we have knowledge of the local port and address,
670 * deliver only to that socket.
672 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
675 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
677 if (cmd != PRC_HOSTDEAD)
678 notify = in6_rtchange;
680 errno = inet6ctlerrmap[cmd];
681 INP_INFO_WLOCK(pcbinfo);
682 CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
684 if ((inp->inp_vflag & INP_IPV6) == 0) {
690 * If the error designates a new path MTU for a destination
691 * and the application (associated with this socket) wanted to
692 * know the value, notify.
693 * XXX: should we avoid to notify the value to TCP sockets?
695 if (cmd == PRC_MSGSIZE && cmdarg != NULL)
696 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
697 *(u_int32_t *)cmdarg);
700 * Detect if we should notify the error. If no source and
701 * destination ports are specifed, but non-zero flowinfo and
702 * local address match, notify the error. This is the case
703 * when the error is delivered with an encrypted buffer
704 * by ESP. Otherwise, just compare addresses and ports
707 if (lport == 0 && fport == 0 && flowinfo &&
708 inp->inp_socket != NULL &&
709 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
710 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
712 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
713 &sa6_dst->sin6_addr) ||
714 inp->inp_socket == 0 ||
715 (lport && inp->inp_lport != lport) ||
716 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
717 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
718 &sa6_src.sin6_addr)) ||
719 (fport && inp->inp_fport != fport)) {
726 if ((*notify)(inp, errno))
731 INP_INFO_WUNLOCK(pcbinfo);
735 * Lookup a PCB based on the local address and port. Caller must hold the
736 * hash lock. No inpcb locks or references are acquired.
739 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
740 u_short lport, int lookupflags, struct ucred *cred)
743 int matchwild = 3, wildcard;
745 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
746 ("%s: invalid lookup flags %d", __func__, lookupflags));
748 INP_HASH_LOCK_ASSERT(pcbinfo);
750 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
751 struct inpcbhead *head;
753 * Look for an unconnected (wildcard foreign addr) PCB that
754 * matches the local address and port we're looking for.
756 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
757 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
758 pcbinfo->ipi_hashmask)];
759 CK_LIST_FOREACH(inp, head, inp_hash) {
760 /* XXX inp locking */
761 if ((inp->inp_vflag & INP_IPV6) == 0)
763 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
764 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
765 inp->inp_lport == lport) {
768 prison_equal_ip6(cred->cr_prison,
769 inp->inp_cred->cr_prison))
778 struct inpcbporthead *porthash;
779 struct inpcbport *phd;
780 struct inpcb *match = NULL;
782 * Best fit PCB lookup.
784 * First see if this local port is in use by looking on the
787 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
788 pcbinfo->ipi_porthashmask)];
789 CK_LIST_FOREACH(phd, porthash, phd_hash) {
790 if (phd->phd_port == lport)
795 * Port is in use by one or more PCBs. Look for best
798 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
801 !prison_equal_ip6(cred->cr_prison,
802 inp->inp_cred->cr_prison))
804 /* XXX inp locking */
805 if ((inp->inp_vflag & INP_IPV6) == 0)
807 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
809 if (!IN6_IS_ADDR_UNSPECIFIED(
811 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
813 else if (!IN6_ARE_ADDR_EQUAL(
814 &inp->in6p_laddr, laddr))
817 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
820 if (wildcard < matchwild) {
822 matchwild = wildcard;
833 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
836 struct in6_multi *inm;
837 struct in6_mfilter *imf;
838 struct ip6_moptions *im6o;
840 INP_INFO_WLOCK(pcbinfo);
841 CK_LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
843 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
847 im6o = inp->in6p_moptions;
848 if ((inp->inp_vflag & INP_IPV6) && im6o != NULL) {
850 * Unselect the outgoing ifp for multicast if it
853 if (im6o->im6o_multicast_ifp == ifp)
854 im6o->im6o_multicast_ifp = NULL;
856 * Drop multicast group membership if we joined
857 * through the interface being detached.
860 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) {
861 if ((inm = imf->im6f_in6m) == NULL)
863 if (inm->in6m_ifp != ifp)
865 ip6_mfilter_remove(&im6o->im6o_head, imf);
866 IN6_MULTI_LOCK_ASSERT();
867 in6_leavegroup_locked(inm, NULL);
868 ip6_mfilter_free(imf);
874 INP_INFO_WUNLOCK(pcbinfo);
878 * Check for alternatives when higher level complains
879 * about service problems. For now, invalidate cached
880 * routing information. If the route was created dynamically
881 * (by a redirect), time to try a default gateway again.
884 in6_losing(struct inpcb *inp)
887 RO_INVALIDATE_CACHE(&inp->inp_route6);
891 * After a routing change, flush old routing
892 * and allocate a (hopefully) better one.
895 in6_rtchange(struct inpcb *inp, int errno __unused)
898 RO_INVALIDATE_CACHE(&inp->inp_route6);
902 static struct inpcb *
903 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
904 const struct in6_addr *laddr, uint16_t lport, const struct in6_addr *faddr,
905 uint16_t fport, int lookupflags, uint8_t numa_domain)
907 struct inpcb *local_wild, *numa_wild;
908 const struct inpcblbgrouphead *hdr;
909 struct inpcblbgroup *grp;
912 INP_HASH_LOCK_ASSERT(pcbinfo);
914 hdr = &pcbinfo->ipi_lbgrouphashbase[
915 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
918 * Order of socket selection:
920 * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD).
923 * - Load balanced group does not contain jailed sockets.
924 * - Load balanced does not contain IPv4 mapped INET6 wild sockets.
928 CK_LIST_FOREACH(grp, hdr, il_list) {
930 if (!(grp->il_vflag & INP_IPV6))
933 if (grp->il_lport != lport)
936 idx = INP_PCBLBGROUP_PKTHASH(INP6_PCBHASHKEY(faddr), lport,
937 fport) % grp->il_inpcnt;
938 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr)) {
939 if (numa_domain == M_NODOM ||
940 grp->il_numa_domain == numa_domain) {
941 return (grp->il_inp[idx]);
944 numa_wild = grp->il_inp[idx];
946 if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr) &&
947 (lookupflags & INPLOOKUP_WILDCARD) != 0 &&
948 (local_wild == NULL || numa_domain == M_NODOM ||
949 grp->il_numa_domain == numa_domain)) {
950 local_wild = grp->il_inp[idx];
953 if (numa_wild != NULL)
960 * Lookup PCB in hash list, using pcbgroup tables.
962 static struct inpcb *
963 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
964 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
965 u_int lport_arg, int lookupflags, struct ifnet *ifp)
967 struct inpcbhead *head;
968 struct inpcb *inp, *tmpinp;
969 u_short fport = fport_arg, lport = lport_arg;
973 * First look for an exact match.
976 INP_GROUP_LOCK(pcbgroup);
977 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(
978 INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)];
979 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
980 /* XXX inp locking */
981 if ((inp->inp_vflag & INP_IPV6) == 0)
983 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
984 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
985 inp->inp_fport == fport &&
986 inp->inp_lport == lport) {
988 * XXX We should be able to directly return
989 * the inp here, without any checks.
990 * Well unless both bound with SO_REUSEPORT?
992 if (prison_flag(inp->inp_cred, PR_IP6))
998 if (tmpinp != NULL) {
1004 * Then look for a wildcard match in the pcbgroup.
1006 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1007 struct inpcb *local_wild = NULL, *local_exact = NULL;
1008 struct inpcb *jail_wild = NULL;
1012 * Order of socket selection - we always prefer jails.
1013 * 1. jailed, non-wild.
1015 * 3. non-jailed, non-wild.
1016 * 4. non-jailed, wild.
1018 head = &pcbgroup->ipg_hashbase[
1019 INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)];
1020 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1021 /* XXX inp locking */
1022 if ((inp->inp_vflag & INP_IPV6) == 0)
1025 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1026 inp->inp_lport != lport) {
1030 injail = prison_flag(inp->inp_cred, PR_IP6);
1032 if (prison_check_ip6(inp->inp_cred,
1036 if (local_exact != NULL)
1040 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1045 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1051 } /* LIST_FOREACH */
1065 * Then look for a wildcard match, if requested.
1067 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1068 struct inpcb *local_wild = NULL, *local_exact = NULL;
1069 struct inpcb *jail_wild = NULL;
1073 * Order of socket selection - we always prefer jails.
1074 * 1. jailed, non-wild.
1076 * 3. non-jailed, non-wild.
1077 * 4. non-jailed, wild.
1079 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(
1080 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1081 pcbinfo->ipi_wildmask)];
1082 CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1083 /* XXX inp locking */
1084 if ((inp->inp_vflag & INP_IPV6) == 0)
1087 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1088 inp->inp_lport != lport) {
1092 injail = prison_flag(inp->inp_cred, PR_IP6);
1094 if (prison_check_ip6(inp->inp_cred,
1098 if (local_exact != NULL)
1102 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1107 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1113 } /* LIST_FOREACH */
1124 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1125 INP_GROUP_UNLOCK(pcbgroup);
1129 if (lookupflags & INPLOOKUP_WLOCKPCB)
1130 locked = INP_TRY_WLOCK(inp);
1131 else if (lookupflags & INPLOOKUP_RLOCKPCB)
1132 locked = INP_TRY_RLOCK(inp);
1134 panic("%s: locking buf", __func__);
1137 INP_GROUP_UNLOCK(pcbgroup);
1139 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1141 if (in_pcbrele_wlocked(inp))
1145 if (in_pcbrele_rlocked(inp))
1150 if (lookupflags & INPLOOKUP_WLOCKPCB)
1151 INP_WLOCK_ASSERT(inp);
1153 INP_RLOCK_ASSERT(inp);
1157 #endif /* PCBGROUP */
1160 * Lookup PCB in hash list. Used in in_pcb.c as well as here.
1163 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1164 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1165 int lookupflags, struct ifnet *ifp, uint8_t numa_domain)
1167 struct inpcbhead *head;
1168 struct inpcb *inp, *tmpinp;
1169 u_short fport = fport_arg, lport = lport_arg;
1171 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1172 ("%s: invalid lookup flags %d", __func__, lookupflags));
1174 INP_HASH_LOCK_ASSERT(pcbinfo);
1177 * First look for an exact match.
1180 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1181 INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)];
1182 CK_LIST_FOREACH(inp, head, inp_hash) {
1183 /* XXX inp locking */
1184 if ((inp->inp_vflag & INP_IPV6) == 0)
1186 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1187 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1188 inp->inp_fport == fport &&
1189 inp->inp_lport == lport) {
1191 * XXX We should be able to directly return
1192 * the inp here, without any checks.
1193 * Well unless both bound with SO_REUSEPORT?
1195 if (prison_flag(inp->inp_cred, PR_IP6))
1205 * Then look in lb group (for wildcard match).
1207 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1208 inp = in6_pcblookup_lbgroup(pcbinfo, laddr, lport, faddr,
1209 fport, lookupflags, numa_domain);
1215 * Then look for a wildcard match, if requested.
1217 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1218 struct inpcb *local_wild = NULL, *local_exact = NULL;
1219 struct inpcb *jail_wild = NULL;
1223 * Order of socket selection - we always prefer jails.
1224 * 1. jailed, non-wild.
1226 * 3. non-jailed, non-wild.
1227 * 4. non-jailed, wild.
1229 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1230 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1231 pcbinfo->ipi_hashmask)];
1232 CK_LIST_FOREACH(inp, head, inp_hash) {
1233 /* XXX inp locking */
1234 if ((inp->inp_vflag & INP_IPV6) == 0)
1237 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1238 inp->inp_lport != lport) {
1242 injail = prison_flag(inp->inp_cred, PR_IP6);
1244 if (prison_check_ip6(inp->inp_cred,
1248 if (local_exact != NULL)
1252 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1257 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1263 } /* LIST_FOREACH */
1265 if (jail_wild != NULL)
1267 if (local_exact != NULL)
1268 return (local_exact);
1269 if (local_wild != NULL)
1270 return (local_wild);
1271 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1280 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1281 * hash list lock, and will return the inpcb locked (i.e., requires
1282 * INPLOOKUP_LOCKPCB).
1284 static struct inpcb *
1285 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1286 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1287 struct ifnet *ifp, uint8_t numa_domain)
1291 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1292 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp,
1295 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1297 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1301 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1303 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1308 panic("%s: locking bug", __func__);
1311 if (lookupflags & INPLOOKUP_WLOCKPCB)
1312 INP_WLOCK_ASSERT(inp);
1314 INP_RLOCK_ASSERT(inp);
1322 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1323 * from which a pre-calculated hash value may be extracted.
1325 * Possibly more of this logic should be in in6_pcbgroup.c.
1328 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1329 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1331 #if defined(PCBGROUP) && !defined(RSS)
1332 struct inpcbgroup *pcbgroup;
1335 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1336 ("%s: invalid lookup flags %d", __func__, lookupflags));
1337 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1338 ("%s: LOCKPCB not set", __func__));
1341 * When not using RSS, use connection groups in preference to the
1342 * reservation table when looking up 4-tuples. When using RSS, just
1343 * use the reservation table, due to the cost of the Toeplitz hash
1346 * XXXRW: This policy belongs in the pcbgroup code, as in principle
1347 * we could be doing RSS with a non-Toeplitz hash that is affordable
1350 #if defined(PCBGROUP) && !defined(RSS)
1351 if (in_pcbgroup_enabled(pcbinfo)) {
1352 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1354 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1355 laddr, lport, lookupflags, ifp));
1358 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1359 lookupflags, ifp, M_NODOM));
1363 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1364 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1365 struct ifnet *ifp, struct mbuf *m)
1368 struct inpcbgroup *pcbgroup;
1371 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1372 ("%s: invalid lookup flags %d", __func__, lookupflags));
1373 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1374 ("%s: LOCKPCB not set", __func__));
1378 * If we can use a hardware-generated hash to look up the connection
1379 * group, use that connection group to find the inpcb. Otherwise
1380 * fall back on a software hash -- or the reservation table if we're
1383 * XXXRW: As above, that policy belongs in the pcbgroup code.
1385 if (in_pcbgroup_enabled(pcbinfo) &&
1386 M_HASHTYPE_TEST(m, M_HASHTYPE_NONE) == 0) {
1387 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1388 m->m_pkthdr.flowid);
1389 if (pcbgroup != NULL)
1390 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1391 fport, laddr, lport, lookupflags, ifp));
1393 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1395 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1396 laddr, lport, lookupflags, ifp));
1400 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1401 lookupflags, ifp, m->m_pkthdr.numa_domain));
1405 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1409 ip = mtod(m, struct ip6_hdr *);
1410 bzero(sin6, sizeof(*sin6));
1411 sin6->sin6_len = sizeof(*sin6);
1412 sin6->sin6_family = AF_INET6;
1413 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1415 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */