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"
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/malloc.h>
82 #include <sys/domain.h>
83 #include <sys/protosw.h>
84 #include <sys/socket.h>
85 #include <sys/socketvar.h>
86 #include <sys/sockio.h>
87 #include <sys/errno.h>
96 #include <net/if_var.h>
97 #include <net/if_llatbl.h>
98 #include <net/if_types.h>
99 #include <net/route.h>
100 #include <net/route/nhop.h>
102 #include <netinet/in.h>
103 #include <netinet/in_var.h>
104 #include <netinet/in_systm.h>
105 #include <netinet/tcp_var.h>
106 #include <netinet/ip6.h>
107 #include <netinet/ip_var.h>
109 #include <netinet6/ip6_var.h>
110 #include <netinet6/nd6.h>
111 #include <netinet/in_pcb.h>
112 #include <netinet6/in6_pcb.h>
113 #include <netinet6/in6_fib.h>
114 #include <netinet6/scope6_var.h>
117 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam,
120 struct socket *so = inp->inp_socket;
121 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
122 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
124 int error, lookupflags = 0;
125 int reuseport = (so->so_options & SO_REUSEPORT);
128 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here
129 * so that we don't have to add to the (already messy) code below.
131 int reuseport_lb = (so->so_options & SO_REUSEPORT_LB);
133 INP_WLOCK_ASSERT(inp);
134 INP_HASH_WLOCK_ASSERT(pcbinfo);
136 if (CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */
137 return (EADDRNOTAVAIL);
138 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
140 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
141 lookupflags = INPLOOKUP_WILDCARD;
143 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
144 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
147 sin6 = (struct sockaddr_in6 *)nam;
148 if (nam->sa_len != sizeof(*sin6))
153 if (nam->sa_family != AF_INET6)
154 return (EAFNOSUPPORT);
156 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
159 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
160 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
163 lport = sin6->sin6_port;
164 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
166 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
167 * allow compepte duplication of binding if
168 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
169 * and a multicast address is bound on both
170 * new and duplicated sockets.
172 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
173 reuseport = SO_REUSEADDR|SO_REUSEPORT;
175 * XXX: How to deal with SO_REUSEPORT_LB here?
176 * Treat same as SO_REUSEPORT for now.
178 if ((so->so_options &
179 (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0)
180 reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB;
181 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
182 struct epoch_tracker et;
185 sin6->sin6_port = 0; /* yech... */
187 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
189 (inp->inp_flags & INP_BINDANY) == 0) {
191 return (EADDRNOTAVAIL);
195 * XXX: bind to an anycast address might accidentally
196 * cause sending a packet with anycast source address.
197 * We should allow to bind to a deprecated address, since
198 * the application dares to use it.
201 ((struct in6_ifaddr *)ifa)->ia6_flags &
202 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
204 return (EADDRNOTAVAIL);
213 if (ntohs(lport) <= V_ipport_reservedhigh &&
214 ntohs(lport) >= V_ipport_reservedlow &&
215 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
217 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
218 priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) {
219 t = in6_pcblookup_local(pcbinfo,
220 &sin6->sin6_addr, lport,
221 INPLOOKUP_WILDCARD, cred);
223 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
224 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
225 (so->so_type != SOCK_STREAM ||
226 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
227 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
228 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
229 (t->inp_flags2 & INP_REUSEPORT) ||
230 (t->inp_flags2 & INP_REUSEPORT_LB) == 0) &&
231 (inp->inp_cred->cr_uid !=
232 t->inp_cred->cr_uid))
236 * If the socket is a BINDMULTI socket, then
237 * the credentials need to match and the
238 * original socket also has to have been bound
241 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
245 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
246 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
247 struct sockaddr_in sin;
249 in6_sin6_2_sin(&sin, sin6);
250 t = in_pcblookup_local(pcbinfo,
252 INPLOOKUP_WILDCARD, cred);
254 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
256 INP_TIMEWAIT) == 0) &&
257 (so->so_type != SOCK_STREAM ||
258 ntohl(t->inp_faddr.s_addr) ==
260 (inp->inp_cred->cr_uid !=
261 t->inp_cred->cr_uid))
264 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
269 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
270 lport, lookupflags, cred);
271 if (t && (t->inp_flags & INP_TIMEWAIT)) {
273 * XXXRW: If an incpb has had its timewait
274 * state recycled, we treat the address as
275 * being in use (for now). This is better
276 * than a panic, but not desirable.
280 ((reuseport & tw->tw_so_options) == 0 &&
281 (reuseport_lb & tw->tw_so_options) == 0))
283 } else if (t && (reuseport & inp_so_options(t)) == 0 &&
284 (reuseport_lb & inp_so_options(t)) == 0) {
288 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
289 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
290 struct sockaddr_in sin;
292 in6_sin6_2_sin(&sin, sin6);
293 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
294 lport, lookupflags, cred);
295 if (t && t->inp_flags & INP_TIMEWAIT) {
299 if ((reuseport & tw->tw_so_options) == 0
300 && (reuseport_lb & tw->tw_so_options) == 0
301 && (ntohl(t->inp_laddr.s_addr) !=
302 INADDR_ANY || ((inp->inp_vflag &
304 (t->inp_vflag & INP_IPV6PROTO))))
307 (reuseport & inp_so_options(t)) == 0 &&
308 (reuseport_lb & inp_so_options(t)) == 0 &&
309 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
310 (t->inp_vflag & INP_IPV6PROTO) != 0)) {
316 inp->in6p_laddr = sin6->sin6_addr;
319 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
320 /* Undo an address bind that may have occurred. */
321 inp->in6p_laddr = in6addr_any;
325 inp->inp_lport = lport;
326 if (in_pcbinshash(inp) != 0) {
327 inp->in6p_laddr = in6addr_any;
336 * Transform old in6_pcbconnect() into an inner subroutine for new
337 * in6_pcbconnect(): Do some validity-checking on the remote
338 * address (in mbuf 'nam') and then determine local host address
339 * (i.e., which interface) to use to access that remote host.
341 * This preserves definition of in6_pcbconnect(), while supporting a
342 * slightly different version for T/TCP. (This is more than
343 * a bit of a kludge, but cleaning up the internal interfaces would
344 * have forced minor changes in every protocol).
347 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
348 struct in6_addr *plocal_addr6)
350 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
352 int scope_ambiguous = 0;
353 struct in6_addr in6a;
355 INP_WLOCK_ASSERT(inp);
356 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
358 if (nam->sa_len != sizeof (*sin6))
360 if (sin6->sin6_family != AF_INET6)
361 return (EAFNOSUPPORT);
362 if (sin6->sin6_port == 0)
363 return (EADDRNOTAVAIL);
365 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
367 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
370 if (!CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) {
372 * If the destination address is UNSPECIFIED addr,
373 * use the loopback addr, e.g ::1.
375 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
376 sin6->sin6_addr = in6addr_loopback;
378 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
381 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
382 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
387 * Do not update this earlier, in case we return with an error.
389 * XXX: this in6_selectsrc_socket result might replace the bound local
390 * address with the address specified by setsockopt(IPV6_PKTINFO).
391 * Is it the intended behavior?
393 *plocal_addr6 = in6a;
396 * Don't do pcblookup call here; return interface in
398 * and exit to caller, that will do the lookup.
406 * Connect from a socket to a specified address.
407 * Both address and port must be specified in argument sin.
408 * If don't have a local address for this socket yet,
412 in6_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
413 struct ucred *cred, struct mbuf *m, bool rehash)
415 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
416 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
417 struct sockaddr_in6 laddr6;
420 bzero(&laddr6, sizeof(laddr6));
421 laddr6.sin6_family = AF_INET6;
423 INP_WLOCK_ASSERT(inp);
424 INP_HASH_WLOCK_ASSERT(pcbinfo);
427 * Call inner routine, to assign local interface address.
428 * in6_pcbladdr() may automatically fill in sin6_scope_id.
430 if ((error = in6_pcbladdr(inp, nam, &laddr6.sin6_addr)) != 0)
433 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
435 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
436 ? &laddr6.sin6_addr : &inp->in6p_laddr,
437 inp->inp_lport, 0, NULL) != NULL) {
440 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
441 if (inp->inp_lport == 0) {
443 * rehash was required to be true in the past for
444 * this case; retain that convention. However,
445 * we now call in_pcb_lport_dest rather than
446 * in6_pcbbind; the former does not insert into
447 * the hash table, the latter does. Change rehash
448 * to false to do the in_pcbinshash below.
450 KASSERT(rehash == true,
451 ("Rehashing required for unbound inps"));
453 error = in_pcb_lport_dest(inp,
454 (struct sockaddr *) &laddr6, &inp->inp_lport,
455 (struct sockaddr *) sin6, sin6->sin6_port, cred, 0);
459 inp->in6p_laddr = laddr6.sin6_addr;
461 inp->in6p_faddr = sin6->sin6_addr;
462 inp->inp_fport = sin6->sin6_port;
463 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
464 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
465 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
467 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
470 in_pcbrehash_mbuf(inp, m);
472 in_pcbinshash_mbuf(inp, m);
479 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
482 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL, true));
486 in6_pcbdisconnect(struct inpcb *inp)
489 INP_WLOCK_ASSERT(inp);
490 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
492 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
494 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
495 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
500 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
502 struct sockaddr_in6 *sin6;
504 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
505 bzero(sin6, sizeof *sin6);
506 sin6->sin6_family = AF_INET6;
507 sin6->sin6_len = sizeof(*sin6);
508 sin6->sin6_port = port;
509 sin6->sin6_addr = *addr_p;
510 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
512 return (struct sockaddr *)sin6;
516 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
518 struct sockaddr_in sin;
519 struct sockaddr_in6 *sin6_p;
521 bzero(&sin, sizeof sin);
522 sin.sin_family = AF_INET;
523 sin.sin_len = sizeof(sin);
525 sin.sin_addr = *addr_p;
527 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
529 in6_sin_2_v4mapsin6(&sin, sin6_p);
531 return (struct sockaddr *)sin6_p;
535 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
538 struct in6_addr addr;
542 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
545 port = inp->inp_lport;
546 addr = inp->in6p_laddr;
549 *nam = in6_sockaddr(port, &addr);
554 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
557 struct in6_addr addr;
561 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
564 port = inp->inp_fport;
565 addr = inp->in6p_faddr;
568 *nam = in6_sockaddr(port, &addr);
573 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
579 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
582 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
583 error = in_getsockaddr(so, nam);
585 in6_sin_2_v4mapsin6_in_sock(nam);
589 /* scope issues will be handled in in6_getsockaddr(). */
590 error = in6_getsockaddr(so, nam);
597 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
603 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
606 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
607 error = in_getpeeraddr(so, nam);
609 in6_sin_2_v4mapsin6_in_sock(nam);
612 /* scope issues will be handled in in6_getpeeraddr(). */
613 error = in6_getpeeraddr(so, nam);
619 * Pass some notification to all connections of a protocol
620 * associated with address dst. The local address and/or port numbers
621 * may be specified to limit the search. The "usual action" will be
622 * taken, depending on the ctlinput cmd. The caller must filter any
623 * cmds that are uninteresting (e.g., no error in the map).
624 * Call the protocol specific routine (if any) to report
625 * any errors for each matching socket.
628 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
629 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
630 int cmd, void *cmdarg,
631 struct inpcb *(*notify)(struct inpcb *, int))
633 struct inpcb *inp, *inp_temp;
634 struct sockaddr_in6 sa6_src, *sa6_dst;
635 u_short fport = fport_arg, lport = lport_arg;
639 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
642 sa6_dst = (struct sockaddr_in6 *)dst;
643 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
647 * note that src can be NULL when we get notify by local fragmentation.
649 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
650 flowinfo = sa6_src.sin6_flowinfo;
653 * Redirects go to all references to the destination,
654 * and use in6_rtchange to invalidate the route cache.
655 * Dead host indications: also use in6_rtchange to invalidate
656 * the cache, and deliver the error to all the sockets.
657 * Otherwise, if we have knowledge of the local port and address,
658 * deliver only to that socket.
660 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
663 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
665 if (cmd != PRC_HOSTDEAD)
666 notify = in6_rtchange;
668 errno = inet6ctlerrmap[cmd];
669 INP_INFO_WLOCK(pcbinfo);
670 CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
672 if ((inp->inp_vflag & INP_IPV6) == 0) {
678 * If the error designates a new path MTU for a destination
679 * and the application (associated with this socket) wanted to
680 * know the value, notify.
681 * XXX: should we avoid to notify the value to TCP sockets?
683 if (cmd == PRC_MSGSIZE && cmdarg != NULL)
684 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
685 *(u_int32_t *)cmdarg);
688 * Detect if we should notify the error. If no source and
689 * destination ports are specifed, but non-zero flowinfo and
690 * local address match, notify the error. This is the case
691 * when the error is delivered with an encrypted buffer
692 * by ESP. Otherwise, just compare addresses and ports
695 if (lport == 0 && fport == 0 && flowinfo &&
696 inp->inp_socket != NULL &&
697 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
698 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
700 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
701 &sa6_dst->sin6_addr) ||
702 inp->inp_socket == 0 ||
703 (lport && inp->inp_lport != lport) ||
704 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
705 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
706 &sa6_src.sin6_addr)) ||
707 (fport && inp->inp_fport != fport)) {
714 if ((*notify)(inp, errno))
719 INP_INFO_WUNLOCK(pcbinfo);
723 * Lookup a PCB based on the local address and port. Caller must hold the
724 * hash lock. No inpcb locks or references are acquired.
727 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
728 u_short lport, int lookupflags, struct ucred *cred)
731 int matchwild = 3, wildcard;
733 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
734 ("%s: invalid lookup flags %d", __func__, lookupflags));
736 INP_HASH_LOCK_ASSERT(pcbinfo);
738 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
739 struct inpcbhead *head;
741 * Look for an unconnected (wildcard foreign addr) PCB that
742 * matches the local address and port we're looking for.
744 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
745 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
746 pcbinfo->ipi_hashmask)];
747 CK_LIST_FOREACH(inp, head, inp_hash) {
748 /* XXX inp locking */
749 if ((inp->inp_vflag & INP_IPV6) == 0)
751 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
752 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
753 inp->inp_lport == lport) {
756 prison_equal_ip6(cred->cr_prison,
757 inp->inp_cred->cr_prison))
766 struct inpcbporthead *porthash;
767 struct inpcbport *phd;
768 struct inpcb *match = NULL;
770 * Best fit PCB lookup.
772 * First see if this local port is in use by looking on the
775 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
776 pcbinfo->ipi_porthashmask)];
777 CK_LIST_FOREACH(phd, porthash, phd_hash) {
778 if (phd->phd_port == lport)
783 * Port is in use by one or more PCBs. Look for best
786 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
789 !prison_equal_ip6(cred->cr_prison,
790 inp->inp_cred->cr_prison))
792 /* XXX inp locking */
793 if ((inp->inp_vflag & INP_IPV6) == 0)
795 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
797 if (!IN6_IS_ADDR_UNSPECIFIED(
799 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
801 else if (!IN6_ARE_ADDR_EQUAL(
802 &inp->in6p_laddr, laddr))
805 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
808 if (wildcard < matchwild) {
810 matchwild = wildcard;
821 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
824 struct in6_multi *inm;
825 struct in6_mfilter *imf;
826 struct ip6_moptions *im6o;
828 INP_INFO_WLOCK(pcbinfo);
829 CK_LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
831 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
835 im6o = inp->in6p_moptions;
836 if ((inp->inp_vflag & INP_IPV6) && im6o != NULL) {
838 * Unselect the outgoing ifp for multicast if it
841 if (im6o->im6o_multicast_ifp == ifp)
842 im6o->im6o_multicast_ifp = NULL;
844 * Drop multicast group membership if we joined
845 * through the interface being detached.
848 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) {
849 if ((inm = imf->im6f_in6m) == NULL)
851 if (inm->in6m_ifp != ifp)
853 ip6_mfilter_remove(&im6o->im6o_head, imf);
854 IN6_MULTI_LOCK_ASSERT();
855 in6_leavegroup_locked(inm, NULL);
856 ip6_mfilter_free(imf);
862 INP_INFO_WUNLOCK(pcbinfo);
866 * Check for alternatives when higher level complains
867 * about service problems. For now, invalidate cached
868 * routing information. If the route was created dynamically
869 * (by a redirect), time to try a default gateway again.
872 in6_losing(struct inpcb *inp)
875 RO_INVALIDATE_CACHE(&inp->inp_route6);
879 * After a routing change, flush old routing
880 * and allocate a (hopefully) better one.
883 in6_rtchange(struct inpcb *inp, int errno __unused)
886 RO_INVALIDATE_CACHE(&inp->inp_route6);
890 static struct inpcb *
891 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
892 const struct in6_addr *laddr, uint16_t lport, const struct in6_addr *faddr,
893 uint16_t fport, int lookupflags)
895 struct inpcb *local_wild;
896 const struct inpcblbgrouphead *hdr;
897 struct inpcblbgroup *grp;
900 INP_HASH_LOCK_ASSERT(pcbinfo);
902 hdr = &pcbinfo->ipi_lbgrouphashbase[
903 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
906 * Order of socket selection:
908 * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD).
911 * - Load balanced group does not contain jailed sockets.
912 * - Load balanced does not contain IPv4 mapped INET6 wild sockets.
915 CK_LIST_FOREACH(grp, hdr, il_list) {
917 if (!(grp->il_vflag & INP_IPV6))
920 if (grp->il_lport != lport)
923 idx = INP_PCBLBGROUP_PKTHASH(INP6_PCBHASHKEY(faddr), lport,
924 fport) % grp->il_inpcnt;
925 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr))
926 return (grp->il_inp[idx]);
927 if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr) &&
928 (lookupflags & INPLOOKUP_WILDCARD) != 0)
929 local_wild = grp->il_inp[idx];
936 * Lookup PCB in hash list, using pcbgroup tables.
938 static struct inpcb *
939 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
940 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
941 u_int lport_arg, int lookupflags, struct ifnet *ifp)
943 struct inpcbhead *head;
944 struct inpcb *inp, *tmpinp;
945 u_short fport = fport_arg, lport = lport_arg;
949 * First look for an exact match.
952 INP_GROUP_LOCK(pcbgroup);
953 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(
954 INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)];
955 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
956 /* XXX inp locking */
957 if ((inp->inp_vflag & INP_IPV6) == 0)
959 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
960 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
961 inp->inp_fport == fport &&
962 inp->inp_lport == lport) {
964 * XXX We should be able to directly return
965 * the inp here, without any checks.
966 * Well unless both bound with SO_REUSEPORT?
968 if (prison_flag(inp->inp_cred, PR_IP6))
974 if (tmpinp != NULL) {
980 * Then look for a wildcard match in the pcbgroup.
982 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
983 struct inpcb *local_wild = NULL, *local_exact = NULL;
984 struct inpcb *jail_wild = NULL;
988 * Order of socket selection - we always prefer jails.
989 * 1. jailed, non-wild.
991 * 3. non-jailed, non-wild.
992 * 4. non-jailed, wild.
994 head = &pcbgroup->ipg_hashbase[
995 INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)];
996 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
997 /* XXX inp locking */
998 if ((inp->inp_vflag & INP_IPV6) == 0)
1001 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1002 inp->inp_lport != lport) {
1006 injail = prison_flag(inp->inp_cred, PR_IP6);
1008 if (prison_check_ip6(inp->inp_cred,
1012 if (local_exact != NULL)
1016 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1021 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1027 } /* LIST_FOREACH */
1041 * Then look for a wildcard match, if requested.
1043 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1044 struct inpcb *local_wild = NULL, *local_exact = NULL;
1045 struct inpcb *jail_wild = NULL;
1049 * Order of socket selection - we always prefer jails.
1050 * 1. jailed, non-wild.
1052 * 3. non-jailed, non-wild.
1053 * 4. non-jailed, wild.
1055 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(
1056 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1057 pcbinfo->ipi_wildmask)];
1058 CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1059 /* XXX inp locking */
1060 if ((inp->inp_vflag & INP_IPV6) == 0)
1063 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1064 inp->inp_lport != lport) {
1068 injail = prison_flag(inp->inp_cred, PR_IP6);
1070 if (prison_check_ip6(inp->inp_cred,
1074 if (local_exact != NULL)
1078 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1083 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1089 } /* LIST_FOREACH */
1100 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1101 INP_GROUP_UNLOCK(pcbgroup);
1105 if (lookupflags & INPLOOKUP_WLOCKPCB)
1106 locked = INP_TRY_WLOCK(inp);
1107 else if (lookupflags & INPLOOKUP_RLOCKPCB)
1108 locked = INP_TRY_RLOCK(inp);
1110 panic("%s: locking buf", __func__);
1113 INP_GROUP_UNLOCK(pcbgroup);
1115 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1117 if (in_pcbrele_wlocked(inp))
1121 if (in_pcbrele_rlocked(inp))
1126 if (lookupflags & INPLOOKUP_WLOCKPCB)
1127 INP_WLOCK_ASSERT(inp);
1129 INP_RLOCK_ASSERT(inp);
1133 #endif /* PCBGROUP */
1136 * Lookup PCB in hash list. Used in in_pcb.c as well as here.
1139 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1140 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1141 int lookupflags, struct ifnet *ifp)
1143 struct inpcbhead *head;
1144 struct inpcb *inp, *tmpinp;
1145 u_short fport = fport_arg, lport = lport_arg;
1147 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1148 ("%s: invalid lookup flags %d", __func__, lookupflags));
1150 INP_HASH_LOCK_ASSERT(pcbinfo);
1153 * First look for an exact match.
1156 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1157 INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)];
1158 CK_LIST_FOREACH(inp, head, inp_hash) {
1159 /* XXX inp locking */
1160 if ((inp->inp_vflag & INP_IPV6) == 0)
1162 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1163 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1164 inp->inp_fport == fport &&
1165 inp->inp_lport == lport) {
1167 * XXX We should be able to directly return
1168 * the inp here, without any checks.
1169 * Well unless both bound with SO_REUSEPORT?
1171 if (prison_flag(inp->inp_cred, PR_IP6))
1181 * Then look in lb group (for wildcard match).
1183 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1184 inp = in6_pcblookup_lbgroup(pcbinfo, laddr, lport, faddr,
1185 fport, lookupflags);
1191 * Then look for a wildcard match, if requested.
1193 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1194 struct inpcb *local_wild = NULL, *local_exact = NULL;
1195 struct inpcb *jail_wild = NULL;
1199 * Order of socket selection - we always prefer jails.
1200 * 1. jailed, non-wild.
1202 * 3. non-jailed, non-wild.
1203 * 4. non-jailed, wild.
1205 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1206 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1207 pcbinfo->ipi_hashmask)];
1208 CK_LIST_FOREACH(inp, head, inp_hash) {
1209 /* XXX inp locking */
1210 if ((inp->inp_vflag & INP_IPV6) == 0)
1213 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1214 inp->inp_lport != lport) {
1218 injail = prison_flag(inp->inp_cred, PR_IP6);
1220 if (prison_check_ip6(inp->inp_cred,
1224 if (local_exact != NULL)
1228 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1233 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1239 } /* LIST_FOREACH */
1241 if (jail_wild != NULL)
1243 if (local_exact != NULL)
1244 return (local_exact);
1245 if (local_wild != NULL)
1246 return (local_wild);
1247 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1256 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1257 * hash list lock, and will return the inpcb locked (i.e., requires
1258 * INPLOOKUP_LOCKPCB).
1260 static struct inpcb *
1261 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1262 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1267 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1268 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1270 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1272 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1276 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1278 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1283 panic("%s: locking bug", __func__);
1286 if (lookupflags & INPLOOKUP_WLOCKPCB)
1287 INP_WLOCK_ASSERT(inp);
1289 INP_RLOCK_ASSERT(inp);
1297 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1298 * from which a pre-calculated hash value may be extracted.
1300 * Possibly more of this logic should be in in6_pcbgroup.c.
1303 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1304 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1306 #if defined(PCBGROUP) && !defined(RSS)
1307 struct inpcbgroup *pcbgroup;
1310 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1311 ("%s: invalid lookup flags %d", __func__, lookupflags));
1312 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1313 ("%s: LOCKPCB not set", __func__));
1316 * When not using RSS, use connection groups in preference to the
1317 * reservation table when looking up 4-tuples. When using RSS, just
1318 * use the reservation table, due to the cost of the Toeplitz hash
1321 * XXXRW: This policy belongs in the pcbgroup code, as in principle
1322 * we could be doing RSS with a non-Toeplitz hash that is affordable
1325 #if defined(PCBGROUP) && !defined(RSS)
1326 if (in_pcbgroup_enabled(pcbinfo)) {
1327 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1329 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1330 laddr, lport, lookupflags, ifp));
1333 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1338 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1339 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1340 struct ifnet *ifp, struct mbuf *m)
1343 struct inpcbgroup *pcbgroup;
1346 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1347 ("%s: invalid lookup flags %d", __func__, lookupflags));
1348 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1349 ("%s: LOCKPCB not set", __func__));
1353 * If we can use a hardware-generated hash to look up the connection
1354 * group, use that connection group to find the inpcb. Otherwise
1355 * fall back on a software hash -- or the reservation table if we're
1358 * XXXRW: As above, that policy belongs in the pcbgroup code.
1360 if (in_pcbgroup_enabled(pcbinfo) &&
1361 M_HASHTYPE_TEST(m, M_HASHTYPE_NONE) == 0) {
1362 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1363 m->m_pkthdr.flowid);
1364 if (pcbgroup != NULL)
1365 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1366 fport, laddr, lport, lookupflags, ifp));
1368 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1370 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1371 laddr, lport, lookupflags, ifp));
1375 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1380 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1384 ip = mtod(m, struct ip6_hdr *);
1385 bzero(sin6, sizeof(*sin6));
1386 sin6->sin6_len = sizeof(*sin6);
1387 sin6->sin6_family = AF_INET6;
1388 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1390 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */