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 (CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */
138 return (EADDRNOTAVAIL);
139 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
141 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
142 lookupflags = INPLOOKUP_WILDCARD;
144 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
145 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
148 sin6 = (struct sockaddr_in6 *)nam;
149 KASSERT(sin6->sin6_family == AF_INET6,
150 ("%s: invalid address family for %p", __func__, sin6));
151 KASSERT(sin6->sin6_len == sizeof(*sin6),
152 ("%s: invalid address length for %p", __func__, sin6));
154 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
157 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
158 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
161 lport = sin6->sin6_port;
162 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
164 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
165 * allow compepte duplication of binding if
166 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
167 * and a multicast address is bound on both
168 * new and duplicated sockets.
170 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
171 reuseport = SO_REUSEADDR|SO_REUSEPORT;
173 * XXX: How to deal with SO_REUSEPORT_LB here?
174 * Treat same as SO_REUSEPORT for now.
176 if ((so->so_options &
177 (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0)
178 reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB;
179 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
180 struct epoch_tracker et;
183 sin6->sin6_port = 0; /* yech... */
185 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
187 (inp->inp_flags & INP_BINDANY) == 0) {
189 return (EADDRNOTAVAIL);
193 * XXX: bind to an anycast address might accidentally
194 * cause sending a packet with anycast source address.
195 * We should allow to bind to a deprecated address, since
196 * the application dares to use it.
199 ((struct in6_ifaddr *)ifa)->ia6_flags &
200 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
202 return (EADDRNOTAVAIL);
211 if (ntohs(lport) <= V_ipport_reservedhigh &&
212 ntohs(lport) >= V_ipport_reservedlow &&
213 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
215 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
216 priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) {
217 t = in6_pcblookup_local(pcbinfo,
218 &sin6->sin6_addr, lport,
219 INPLOOKUP_WILDCARD, cred);
221 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
222 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
223 (so->so_type != SOCK_STREAM ||
224 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
225 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
226 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
227 (t->inp_flags2 & INP_REUSEPORT) ||
228 (t->inp_flags2 & INP_REUSEPORT_LB) == 0) &&
229 (inp->inp_cred->cr_uid !=
230 t->inp_cred->cr_uid))
234 * If the socket is a BINDMULTI socket, then
235 * the credentials need to match and the
236 * original socket also has to have been bound
239 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
243 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
244 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
245 struct sockaddr_in sin;
247 in6_sin6_2_sin(&sin, sin6);
248 t = in_pcblookup_local(pcbinfo,
250 INPLOOKUP_WILDCARD, cred);
252 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
254 INP_TIMEWAIT) == 0) &&
255 (so->so_type != SOCK_STREAM ||
256 ntohl(t->inp_faddr.s_addr) ==
258 (inp->inp_cred->cr_uid !=
259 t->inp_cred->cr_uid))
262 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
267 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
268 lport, lookupflags, cred);
269 if (t && (t->inp_flags & INP_TIMEWAIT)) {
271 * XXXRW: If an incpb has had its timewait
272 * state recycled, we treat the address as
273 * being in use (for now). This is better
274 * than a panic, but not desirable.
278 ((reuseport & tw->tw_so_options) == 0 &&
279 (reuseport_lb & tw->tw_so_options) == 0))
281 } else if (t && (reuseport & inp_so_options(t)) == 0 &&
282 (reuseport_lb & inp_so_options(t)) == 0) {
286 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
287 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
288 struct sockaddr_in sin;
290 in6_sin6_2_sin(&sin, sin6);
291 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
292 lport, lookupflags, cred);
293 if (t && t->inp_flags & INP_TIMEWAIT) {
297 if ((reuseport & tw->tw_so_options) == 0
298 && (reuseport_lb & tw->tw_so_options) == 0
299 && (ntohl(t->inp_laddr.s_addr) !=
300 INADDR_ANY || ((inp->inp_vflag &
302 (t->inp_vflag & INP_IPV6PROTO))))
305 (reuseport & inp_so_options(t)) == 0 &&
306 (reuseport_lb & inp_so_options(t)) == 0 &&
307 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
308 (t->inp_vflag & INP_IPV6PROTO) != 0)) {
314 inp->in6p_laddr = sin6->sin6_addr;
317 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
318 /* Undo an address bind that may have occurred. */
319 inp->in6p_laddr = in6addr_any;
323 inp->inp_lport = lport;
324 if (in_pcbinshash(inp) != 0) {
325 inp->in6p_laddr = in6addr_any;
334 * Transform old in6_pcbconnect() into an inner subroutine for new
335 * in6_pcbconnect(): Do some validity-checking on the remote
336 * address (in mbuf 'nam') and then determine local host address
337 * (i.e., which interface) to use to access that remote host.
339 * This preserves definition of in6_pcbconnect(), while supporting a
340 * slightly different version for T/TCP. (This is more than
341 * a bit of a kludge, but cleaning up the internal interfaces would
342 * have forced minor changes in every protocol).
345 in6_pcbladdr(struct inpcb *inp, struct sockaddr_in6 *sin6,
346 struct in6_addr *plocal_addr6)
349 int scope_ambiguous = 0;
350 struct in6_addr in6a;
351 struct epoch_tracker et;
353 INP_WLOCK_ASSERT(inp);
354 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
356 if (sin6->sin6_port == 0)
357 return (EADDRNOTAVAIL);
359 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
361 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
364 if (!CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) {
366 * If the destination address is UNSPECIFIED addr,
367 * use the loopback addr, e.g ::1.
369 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
370 sin6->sin6_addr = in6addr_loopback;
372 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
376 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
377 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
383 * Do not update this earlier, in case we return with an error.
385 * XXX: this in6_selectsrc_socket result might replace the bound local
386 * address with the address specified by setsockopt(IPV6_PKTINFO).
387 * Is it the intended behavior?
389 *plocal_addr6 = in6a;
392 * Don't do pcblookup call here; return interface in
394 * and exit to caller, that will do the lookup.
402 * Connect from a socket to a specified address.
403 * Both address and port must be specified in argument sin.
404 * If don't have a local address for this socket yet,
408 in6_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
409 struct ucred *cred, struct mbuf *m, bool rehash)
411 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
412 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
413 struct sockaddr_in6 laddr6;
416 KASSERT(sin6->sin6_family == AF_INET6,
417 ("%s: invalid address family for %p", __func__, sin6));
418 KASSERT(sin6->sin6_len == sizeof(*sin6),
419 ("%s: invalid address length for %p", __func__, sin6));
421 bzero(&laddr6, sizeof(laddr6));
422 laddr6.sin6_family = AF_INET6;
424 INP_WLOCK_ASSERT(inp);
425 INP_HASH_WLOCK_ASSERT(pcbinfo);
428 if (CALC_FLOWID_OUTBOUND) {
429 uint32_t hash_type, hash_val;
431 hash_val = fib6_calc_software_hash(&inp->in6p_laddr,
432 &sin6->sin6_addr, 0, sin6->sin6_port,
433 inp->inp_socket->so_proto->pr_protocol, &hash_type);
434 inp->inp_flowid = hash_val;
435 inp->inp_flowtype = hash_type;
439 * Call inner routine, to assign local interface address.
440 * in6_pcbladdr() may automatically fill in sin6_scope_id.
442 if ((error = in6_pcbladdr(inp, sin6, &laddr6.sin6_addr)) != 0)
445 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
447 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
448 ? &laddr6.sin6_addr : &inp->in6p_laddr,
449 inp->inp_lport, 0, NULL, M_NODOM) != NULL) {
452 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
453 if (inp->inp_lport == 0) {
455 * rehash was required to be true in the past for
456 * this case; retain that convention. However,
457 * we now call in_pcb_lport_dest rather than
458 * in6_pcbbind; the former does not insert into
459 * the hash table, the latter does. Change rehash
460 * to false to do the in_pcbinshash below.
462 KASSERT(rehash == true,
463 ("Rehashing required for unbound inps"));
465 error = in_pcb_lport_dest(inp,
466 (struct sockaddr *) &laddr6, &inp->inp_lport,
467 (struct sockaddr *) sin6, sin6->sin6_port, cred,
472 inp->in6p_laddr = laddr6.sin6_addr;
474 inp->in6p_faddr = sin6->sin6_addr;
475 inp->inp_fport = sin6->sin6_port;
476 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
477 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
478 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
480 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
483 in_pcbrehash_mbuf(inp, m);
485 in_pcbinshash_mbuf(inp, m);
492 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
495 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL, true));
499 in6_pcbdisconnect(struct inpcb *inp)
502 INP_WLOCK_ASSERT(inp);
503 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
505 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
507 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
508 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
513 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
515 struct sockaddr_in6 *sin6;
517 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
518 bzero(sin6, sizeof *sin6);
519 sin6->sin6_family = AF_INET6;
520 sin6->sin6_len = sizeof(*sin6);
521 sin6->sin6_port = port;
522 sin6->sin6_addr = *addr_p;
523 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
525 return (struct sockaddr *)sin6;
529 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
531 struct sockaddr_in sin;
532 struct sockaddr_in6 *sin6_p;
534 bzero(&sin, sizeof sin);
535 sin.sin_family = AF_INET;
536 sin.sin_len = sizeof(sin);
538 sin.sin_addr = *addr_p;
540 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
542 in6_sin_2_v4mapsin6(&sin, sin6_p);
544 return (struct sockaddr *)sin6_p;
548 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
551 struct in6_addr addr;
555 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
558 port = inp->inp_lport;
559 addr = inp->in6p_laddr;
562 *nam = in6_sockaddr(port, &addr);
567 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
570 struct in6_addr addr;
574 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
577 port = inp->inp_fport;
578 addr = inp->in6p_faddr;
581 *nam = in6_sockaddr(port, &addr);
586 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
592 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
595 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
596 error = in_getsockaddr(so, nam);
598 in6_sin_2_v4mapsin6_in_sock(nam);
602 /* scope issues will be handled in in6_getsockaddr(). */
603 error = in6_getsockaddr(so, nam);
610 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
616 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
619 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
620 error = in_getpeeraddr(so, nam);
622 in6_sin_2_v4mapsin6_in_sock(nam);
625 /* scope issues will be handled in in6_getpeeraddr(). */
626 error = in6_getpeeraddr(so, nam);
632 * Pass some notification to all connections of a protocol
633 * associated with address dst. The local address and/or port numbers
634 * may be specified to limit the search. The "usual action" will be
635 * taken, depending on the ctlinput cmd. The caller must filter any
636 * cmds that are uninteresting (e.g., no error in the map).
637 * Call the protocol specific routine (if any) to report
638 * any errors for each matching socket.
641 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
642 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
643 int cmd, void *cmdarg,
644 struct inpcb *(*notify)(struct inpcb *, int))
646 struct inpcb *inp, *inp_temp;
647 struct sockaddr_in6 sa6_src, *sa6_dst;
648 u_short fport = fport_arg, lport = lport_arg;
652 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
655 sa6_dst = (struct sockaddr_in6 *)dst;
656 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
660 * note that src can be NULL when we get notify by local fragmentation.
662 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
663 flowinfo = sa6_src.sin6_flowinfo;
666 * Redirects go to all references to the destination,
667 * and use in6_rtchange to invalidate the route cache.
668 * Dead host indications: also use in6_rtchange to invalidate
669 * the cache, and deliver the error to all the sockets.
670 * Otherwise, if we have knowledge of the local port and address,
671 * deliver only to that socket.
673 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
676 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
678 if (cmd != PRC_HOSTDEAD)
679 notify = in6_rtchange;
681 errno = inet6ctlerrmap[cmd];
682 INP_INFO_WLOCK(pcbinfo);
683 CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
685 if ((inp->inp_vflag & INP_IPV6) == 0) {
691 * If the error designates a new path MTU for a destination
692 * and the application (associated with this socket) wanted to
693 * know the value, notify.
694 * XXX: should we avoid to notify the value to TCP sockets?
696 if (cmd == PRC_MSGSIZE && cmdarg != NULL)
697 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
698 *(u_int32_t *)cmdarg);
701 * Detect if we should notify the error. If no source and
702 * destination ports are specifed, but non-zero flowinfo and
703 * local address match, notify the error. This is the case
704 * when the error is delivered with an encrypted buffer
705 * by ESP. Otherwise, just compare addresses and ports
708 if (lport == 0 && fport == 0 && flowinfo &&
709 inp->inp_socket != NULL &&
710 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
711 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
713 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
714 &sa6_dst->sin6_addr) ||
715 inp->inp_socket == 0 ||
716 (lport && inp->inp_lport != lport) ||
717 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
718 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
719 &sa6_src.sin6_addr)) ||
720 (fport && inp->inp_fport != fport)) {
727 if ((*notify)(inp, errno))
732 INP_INFO_WUNLOCK(pcbinfo);
736 * Lookup a PCB based on the local address and port. Caller must hold the
737 * hash lock. No inpcb locks or references are acquired.
740 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
741 u_short lport, int lookupflags, struct ucred *cred)
744 int matchwild = 3, wildcard;
746 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
747 ("%s: invalid lookup flags %d", __func__, lookupflags));
749 INP_HASH_LOCK_ASSERT(pcbinfo);
751 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
752 struct inpcbhead *head;
754 * Look for an unconnected (wildcard foreign addr) PCB that
755 * matches the local address and port we're looking for.
757 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
758 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
759 pcbinfo->ipi_hashmask)];
760 CK_LIST_FOREACH(inp, head, inp_hash) {
761 /* XXX inp locking */
762 if ((inp->inp_vflag & INP_IPV6) == 0)
764 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
765 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
766 inp->inp_lport == lport) {
769 prison_equal_ip6(cred->cr_prison,
770 inp->inp_cred->cr_prison))
779 struct inpcbporthead *porthash;
780 struct inpcbport *phd;
781 struct inpcb *match = NULL;
783 * Best fit PCB lookup.
785 * First see if this local port is in use by looking on the
788 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
789 pcbinfo->ipi_porthashmask)];
790 CK_LIST_FOREACH(phd, porthash, phd_hash) {
791 if (phd->phd_port == lport)
796 * Port is in use by one or more PCBs. Look for best
799 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
802 !prison_equal_ip6(cred->cr_prison,
803 inp->inp_cred->cr_prison))
805 /* XXX inp locking */
806 if ((inp->inp_vflag & INP_IPV6) == 0)
808 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
810 if (!IN6_IS_ADDR_UNSPECIFIED(
812 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
814 else if (!IN6_ARE_ADDR_EQUAL(
815 &inp->in6p_laddr, laddr))
818 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
821 if (wildcard < matchwild) {
823 matchwild = wildcard;
834 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
837 struct in6_multi *inm;
838 struct in6_mfilter *imf;
839 struct ip6_moptions *im6o;
841 INP_INFO_WLOCK(pcbinfo);
842 CK_LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
844 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
848 im6o = inp->in6p_moptions;
849 if ((inp->inp_vflag & INP_IPV6) && im6o != NULL) {
851 * Unselect the outgoing ifp for multicast if it
854 if (im6o->im6o_multicast_ifp == ifp)
855 im6o->im6o_multicast_ifp = NULL;
857 * Drop multicast group membership if we joined
858 * through the interface being detached.
861 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) {
862 if ((inm = imf->im6f_in6m) == NULL)
864 if (inm->in6m_ifp != ifp)
866 ip6_mfilter_remove(&im6o->im6o_head, imf);
867 IN6_MULTI_LOCK_ASSERT();
868 in6_leavegroup_locked(inm, NULL);
869 ip6_mfilter_free(imf);
875 INP_INFO_WUNLOCK(pcbinfo);
879 * Check for alternatives when higher level complains
880 * about service problems. For now, invalidate cached
881 * routing information. If the route was created dynamically
882 * (by a redirect), time to try a default gateway again.
885 in6_losing(struct inpcb *inp)
888 RO_INVALIDATE_CACHE(&inp->inp_route6);
892 * After a routing change, flush old routing
893 * and allocate a (hopefully) better one.
896 in6_rtchange(struct inpcb *inp, int errno __unused)
899 RO_INVALIDATE_CACHE(&inp->inp_route6);
903 static struct inpcb *
904 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
905 const struct in6_addr *laddr, uint16_t lport, const struct in6_addr *faddr,
906 uint16_t fport, int lookupflags, uint8_t numa_domain)
908 struct inpcb *local_wild, *numa_wild;
909 const struct inpcblbgrouphead *hdr;
910 struct inpcblbgroup *grp;
913 INP_HASH_LOCK_ASSERT(pcbinfo);
915 hdr = &pcbinfo->ipi_lbgrouphashbase[
916 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
919 * Order of socket selection:
921 * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD).
924 * - Load balanced group does not contain jailed sockets.
925 * - Load balanced does not contain IPv4 mapped INET6 wild sockets.
929 CK_LIST_FOREACH(grp, hdr, il_list) {
931 if (!(grp->il_vflag & INP_IPV6))
934 if (grp->il_lport != lport)
937 idx = INP_PCBLBGROUP_PKTHASH(INP6_PCBHASHKEY(faddr), lport,
938 fport) % grp->il_inpcnt;
939 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr)) {
940 if (numa_domain == M_NODOM ||
941 grp->il_numa_domain == numa_domain) {
942 return (grp->il_inp[idx]);
945 numa_wild = grp->il_inp[idx];
947 if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr) &&
948 (lookupflags & INPLOOKUP_WILDCARD) != 0 &&
949 (local_wild == NULL || numa_domain == M_NODOM ||
950 grp->il_numa_domain == numa_domain)) {
951 local_wild = grp->il_inp[idx];
954 if (numa_wild != NULL)
961 * Lookup PCB in hash list, using pcbgroup tables.
963 static struct inpcb *
964 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
965 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
966 u_int lport_arg, int lookupflags, struct ifnet *ifp)
968 struct inpcbhead *head;
969 struct inpcb *inp, *tmpinp;
970 u_short fport = fport_arg, lport = lport_arg;
974 * First look for an exact match.
977 INP_GROUP_LOCK(pcbgroup);
978 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(
979 INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)];
980 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
981 /* XXX inp locking */
982 if ((inp->inp_vflag & INP_IPV6) == 0)
984 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
985 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
986 inp->inp_fport == fport &&
987 inp->inp_lport == lport) {
989 * XXX We should be able to directly return
990 * the inp here, without any checks.
991 * Well unless both bound with SO_REUSEPORT?
993 if (prison_flag(inp->inp_cred, PR_IP6))
999 if (tmpinp != NULL) {
1005 * Then look for a wildcard match in the pcbgroup.
1007 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1008 struct inpcb *local_wild = NULL, *local_exact = NULL;
1009 struct inpcb *jail_wild = NULL;
1013 * Order of socket selection - we always prefer jails.
1014 * 1. jailed, non-wild.
1016 * 3. non-jailed, non-wild.
1017 * 4. non-jailed, wild.
1019 head = &pcbgroup->ipg_hashbase[
1020 INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)];
1021 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
1022 /* XXX inp locking */
1023 if ((inp->inp_vflag & INP_IPV6) == 0)
1026 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1027 inp->inp_lport != lport) {
1031 injail = prison_flag(inp->inp_cred, PR_IP6);
1033 if (prison_check_ip6(inp->inp_cred,
1037 if (local_exact != NULL)
1041 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1046 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1052 } /* LIST_FOREACH */
1066 * Then look for a wildcard match, if requested.
1068 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1069 struct inpcb *local_wild = NULL, *local_exact = NULL;
1070 struct inpcb *jail_wild = NULL;
1074 * Order of socket selection - we always prefer jails.
1075 * 1. jailed, non-wild.
1077 * 3. non-jailed, non-wild.
1078 * 4. non-jailed, wild.
1080 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(
1081 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1082 pcbinfo->ipi_wildmask)];
1083 CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1084 /* XXX inp locking */
1085 if ((inp->inp_vflag & INP_IPV6) == 0)
1088 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1089 inp->inp_lport != lport) {
1093 injail = prison_flag(inp->inp_cred, PR_IP6);
1095 if (prison_check_ip6(inp->inp_cred,
1099 if (local_exact != NULL)
1103 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1108 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1114 } /* LIST_FOREACH */
1125 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1126 INP_GROUP_UNLOCK(pcbgroup);
1130 if (lookupflags & INPLOOKUP_WLOCKPCB)
1131 locked = INP_TRY_WLOCK(inp);
1132 else if (lookupflags & INPLOOKUP_RLOCKPCB)
1133 locked = INP_TRY_RLOCK(inp);
1135 panic("%s: locking buf", __func__);
1138 INP_GROUP_UNLOCK(pcbgroup);
1140 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1142 if (in_pcbrele_wlocked(inp))
1146 if (in_pcbrele_rlocked(inp))
1151 if (lookupflags & INPLOOKUP_WLOCKPCB)
1152 INP_WLOCK_ASSERT(inp);
1154 INP_RLOCK_ASSERT(inp);
1158 #endif /* PCBGROUP */
1161 * Lookup PCB in hash list. Used in in_pcb.c as well as here.
1164 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1165 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1166 int lookupflags, struct ifnet *ifp, uint8_t numa_domain)
1168 struct inpcbhead *head;
1169 struct inpcb *inp, *tmpinp;
1170 u_short fport = fport_arg, lport = lport_arg;
1172 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1173 ("%s: invalid lookup flags %d", __func__, lookupflags));
1175 INP_HASH_LOCK_ASSERT(pcbinfo);
1178 * First look for an exact match.
1181 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1182 INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)];
1183 CK_LIST_FOREACH(inp, head, inp_hash) {
1184 /* XXX inp locking */
1185 if ((inp->inp_vflag & INP_IPV6) == 0)
1187 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1188 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1189 inp->inp_fport == fport &&
1190 inp->inp_lport == lport) {
1192 * XXX We should be able to directly return
1193 * the inp here, without any checks.
1194 * Well unless both bound with SO_REUSEPORT?
1196 if (prison_flag(inp->inp_cred, PR_IP6))
1206 * Then look in lb group (for wildcard match).
1208 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1209 inp = in6_pcblookup_lbgroup(pcbinfo, laddr, lport, faddr,
1210 fport, lookupflags, numa_domain);
1216 * Then look for a wildcard match, if requested.
1218 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1219 struct inpcb *local_wild = NULL, *local_exact = NULL;
1220 struct inpcb *jail_wild = NULL;
1224 * Order of socket selection - we always prefer jails.
1225 * 1. jailed, non-wild.
1227 * 3. non-jailed, non-wild.
1228 * 4. non-jailed, wild.
1230 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1231 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1232 pcbinfo->ipi_hashmask)];
1233 CK_LIST_FOREACH(inp, head, inp_hash) {
1234 /* XXX inp locking */
1235 if ((inp->inp_vflag & INP_IPV6) == 0)
1238 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1239 inp->inp_lport != lport) {
1243 injail = prison_flag(inp->inp_cred, PR_IP6);
1245 if (prison_check_ip6(inp->inp_cred,
1249 if (local_exact != NULL)
1253 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1258 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1264 } /* LIST_FOREACH */
1266 if (jail_wild != NULL)
1268 if (local_exact != NULL)
1269 return (local_exact);
1270 if (local_wild != NULL)
1271 return (local_wild);
1272 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1281 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1282 * hash list lock, and will return the inpcb locked (i.e., requires
1283 * INPLOOKUP_LOCKPCB).
1285 static struct inpcb *
1286 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1287 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1288 struct ifnet *ifp, uint8_t numa_domain)
1292 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1293 lookupflags & INPLOOKUP_WILDCARD, ifp, numa_domain);
1295 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1297 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1300 panic("%s: locking bug", __func__);
1301 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1310 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1311 * from which a pre-calculated hash value may be extracted.
1313 * Possibly more of this logic should be in in6_pcbgroup.c.
1316 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1317 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1319 #if defined(PCBGROUP) && !defined(RSS)
1320 struct inpcbgroup *pcbgroup;
1323 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1324 ("%s: invalid lookup flags %d", __func__, lookupflags));
1325 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1326 ("%s: LOCKPCB not set", __func__));
1329 * When not using RSS, use connection groups in preference to the
1330 * reservation table when looking up 4-tuples. When using RSS, just
1331 * use the reservation table, due to the cost of the Toeplitz hash
1334 * XXXRW: This policy belongs in the pcbgroup code, as in principle
1335 * we could be doing RSS with a non-Toeplitz hash that is affordable
1338 #if defined(PCBGROUP) && !defined(RSS)
1339 if (in_pcbgroup_enabled(pcbinfo)) {
1340 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1342 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1343 laddr, lport, lookupflags, ifp));
1346 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1347 lookupflags, ifp, M_NODOM));
1351 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1352 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1353 struct ifnet *ifp, struct mbuf *m)
1356 struct inpcbgroup *pcbgroup;
1359 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1360 ("%s: invalid lookup flags %d", __func__, lookupflags));
1361 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1362 ("%s: LOCKPCB not set", __func__));
1366 * If we can use a hardware-generated hash to look up the connection
1367 * group, use that connection group to find the inpcb. Otherwise
1368 * fall back on a software hash -- or the reservation table if we're
1371 * XXXRW: As above, that policy belongs in the pcbgroup code.
1373 if (in_pcbgroup_enabled(pcbinfo) &&
1374 M_HASHTYPE_TEST(m, M_HASHTYPE_NONE) == 0) {
1375 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1376 m->m_pkthdr.flowid);
1377 if (pcbgroup != NULL)
1378 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1379 fport, laddr, lport, lookupflags, ifp));
1381 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1383 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1384 laddr, lport, lookupflags, ifp));
1388 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1389 lookupflags, ifp, m->m_pkthdr.numa_domain));
1393 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1397 ip = mtod(m, struct ip6_hdr *);
1398 bzero(sin6, sizeof(*sin6));
1399 sin6->sin6_len = sizeof(*sin6);
1400 sin6->sin6_family = AF_INET6;
1401 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1403 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */