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>
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/scope6_var.h>
115 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam,
118 struct socket *so = inp->inp_socket;
119 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
120 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
122 int error, lookupflags = 0;
123 int reuseport = (so->so_options & SO_REUSEPORT);
126 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here
127 * so that we don't have to add to the (already messy) code below.
129 int reuseport_lb = (so->so_options & SO_REUSEPORT_LB);
131 INP_WLOCK_ASSERT(inp);
132 INP_HASH_WLOCK_ASSERT(pcbinfo);
134 if (CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */
135 return (EADDRNOTAVAIL);
136 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
138 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
139 lookupflags = INPLOOKUP_WILDCARD;
141 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
142 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
145 sin6 = (struct sockaddr_in6 *)nam;
146 if (nam->sa_len != sizeof(*sin6))
151 if (nam->sa_family != AF_INET6)
152 return (EAFNOSUPPORT);
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)) {
182 sin6->sin6_port = 0; /* yech... */
184 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
186 (inp->inp_flags & INP_BINDANY) == 0) {
188 return (EADDRNOTAVAIL);
192 * XXX: bind to an anycast address might accidentally
193 * cause sending a packet with anycast source address.
194 * We should allow to bind to a deprecated address, since
195 * the application dares to use it.
198 ((struct in6_ifaddr *)ifa)->ia6_flags &
199 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
201 return (EADDRNOTAVAIL);
210 if (ntohs(lport) <= V_ipport_reservedhigh &&
211 ntohs(lport) >= V_ipport_reservedlow &&
212 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
215 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
216 priv_check_cred(inp->inp_cred,
217 PRIV_NETINET_REUSEPORT, 0) != 0) {
218 t = in6_pcblookup_local(pcbinfo,
219 &sin6->sin6_addr, lport,
220 INPLOOKUP_WILDCARD, cred);
222 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
223 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
224 (so->so_type != SOCK_STREAM ||
225 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
226 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
227 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
228 (t->inp_flags2 & INP_REUSEPORT) ||
229 (t->inp_flags2 & INP_REUSEPORT_LB) == 0) &&
230 (inp->inp_cred->cr_uid !=
231 t->inp_cred->cr_uid))
235 * If the socket is a BINDMULTI socket, then
236 * the credentials need to match and the
237 * original socket also has to have been bound
240 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
244 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
245 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
246 struct sockaddr_in sin;
248 in6_sin6_2_sin(&sin, sin6);
249 t = in_pcblookup_local(pcbinfo,
251 INPLOOKUP_WILDCARD, cred);
253 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
255 INP_TIMEWAIT) == 0) &&
256 (so->so_type != SOCK_STREAM ||
257 ntohl(t->inp_faddr.s_addr) ==
259 (inp->inp_cred->cr_uid !=
260 t->inp_cred->cr_uid))
263 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
268 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
269 lport, lookupflags, cred);
270 if (t && (t->inp_flags & INP_TIMEWAIT)) {
272 * XXXRW: If an incpb has had its timewait
273 * state recycled, we treat the address as
274 * being in use (for now). This is better
275 * than a panic, but not desirable.
279 ((reuseport & tw->tw_so_options) == 0 &&
280 (reuseport_lb & tw->tw_so_options) == 0))
282 } else if (t && (reuseport & inp_so_options(t)) == 0 &&
283 (reuseport_lb & inp_so_options(t)) == 0) {
287 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
288 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
289 struct sockaddr_in sin;
291 in6_sin6_2_sin(&sin, sin6);
292 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
293 lport, lookupflags, cred);
294 if (t && t->inp_flags & INP_TIMEWAIT) {
298 if ((reuseport & tw->tw_so_options) == 0
299 && (reuseport_lb & tw->tw_so_options) == 0
300 && (ntohl(t->inp_laddr.s_addr) !=
301 INADDR_ANY || ((inp->inp_vflag &
303 (t->inp_vflag & INP_IPV6PROTO))))
306 (reuseport & inp_so_options(t)) == 0 &&
307 (reuseport_lb & inp_so_options(t)) == 0 &&
308 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
309 (t->inp_vflag & INP_IPV6PROTO) != 0)) {
315 inp->in6p_laddr = sin6->sin6_addr;
318 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
319 /* Undo an address bind that may have occurred. */
320 inp->in6p_laddr = in6addr_any;
324 inp->inp_lport = lport;
325 if (in_pcbinshash(inp) != 0) {
326 inp->in6p_laddr = in6addr_any;
335 * Transform old in6_pcbconnect() into an inner subroutine for new
336 * in6_pcbconnect(): Do some validity-checking on the remote
337 * address (in mbuf 'nam') and then determine local host address
338 * (i.e., which interface) to use to access that remote host.
340 * This preserves definition of in6_pcbconnect(), while supporting a
341 * slightly different version for T/TCP. (This is more than
342 * a bit of a kludge, but cleaning up the internal interfaces would
343 * have forced minor changes in every protocol).
346 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
347 struct in6_addr *plocal_addr6)
349 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
351 int scope_ambiguous = 0;
352 struct in6_addr in6a;
354 INP_WLOCK_ASSERT(inp);
355 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
357 if (nam->sa_len != sizeof (*sin6))
359 if (sin6->sin6_family != AF_INET6)
360 return (EAFNOSUPPORT);
361 if (sin6->sin6_port == 0)
362 return (EADDRNOTAVAIL);
364 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
366 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
369 if (!CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) {
371 * If the destination address is UNSPECIFIED addr,
372 * use the loopback addr, e.g ::1.
374 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
375 sin6->sin6_addr = in6addr_loopback;
377 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
380 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
381 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
386 * Do not update this earlier, in case we return with an error.
388 * XXX: this in6_selectsrc_socket result might replace the bound local
389 * address with the address specified by setsockopt(IPV6_PKTINFO).
390 * Is it the intended behavior?
392 *plocal_addr6 = in6a;
395 * Don't do pcblookup call here; return interface in
397 * and exit to caller, that will do the lookup.
405 * Connect from a socket to a specified address.
406 * Both address and port must be specified in argument sin.
407 * If don't have a local address for this socket yet,
411 in6_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
412 struct ucred *cred, struct mbuf *m, bool rehash)
414 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
415 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
416 struct sockaddr_in6 laddr6;
419 bzero(&laddr6, sizeof(laddr6));
420 laddr6.sin6_family = AF_INET6;
422 INP_WLOCK_ASSERT(inp);
423 INP_HASH_WLOCK_ASSERT(pcbinfo);
426 * Call inner routine, to assign local interface address.
427 * in6_pcbladdr() may automatically fill in sin6_scope_id.
429 if ((error = in6_pcbladdr(inp, nam, &laddr6.sin6_addr)) != 0)
432 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
434 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
435 ? &laddr6.sin6_addr : &inp->in6p_laddr,
436 inp->inp_lport, 0, NULL) != NULL) {
439 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
440 if (inp->inp_lport == 0) {
441 KASSERT(rehash == true,
442 ("Rehashing required for unbound inps"));
444 error = in_pcb_lport_dest(inp,
445 (struct sockaddr *) &laddr6, &inp->inp_lport,
446 (struct sockaddr *) sin6, sin6->sin6_port, cred, 0);
450 inp->in6p_laddr = laddr6.sin6_addr;
452 inp->in6p_faddr = sin6->sin6_addr;
453 inp->inp_fport = sin6->sin6_port;
454 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
455 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
456 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
458 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
461 in_pcbrehash_mbuf(inp, m);
463 in_pcbinshash_mbuf(inp, m);
470 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
473 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL, true));
477 in6_pcbdisconnect(struct inpcb *inp)
480 INP_WLOCK_ASSERT(inp);
481 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
483 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
485 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
486 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
491 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
493 struct sockaddr_in6 *sin6;
495 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
496 bzero(sin6, sizeof *sin6);
497 sin6->sin6_family = AF_INET6;
498 sin6->sin6_len = sizeof(*sin6);
499 sin6->sin6_port = port;
500 sin6->sin6_addr = *addr_p;
501 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
503 return (struct sockaddr *)sin6;
507 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
509 struct sockaddr_in sin;
510 struct sockaddr_in6 *sin6_p;
512 bzero(&sin, sizeof sin);
513 sin.sin_family = AF_INET;
514 sin.sin_len = sizeof(sin);
516 sin.sin_addr = *addr_p;
518 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
520 in6_sin_2_v4mapsin6(&sin, sin6_p);
522 return (struct sockaddr *)sin6_p;
526 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
529 struct in6_addr addr;
533 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
536 port = inp->inp_lport;
537 addr = inp->in6p_laddr;
540 *nam = in6_sockaddr(port, &addr);
545 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
548 struct in6_addr addr;
552 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
555 port = inp->inp_fport;
556 addr = inp->in6p_faddr;
559 *nam = in6_sockaddr(port, &addr);
564 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
570 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
573 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
574 error = in_getsockaddr(so, nam);
576 in6_sin_2_v4mapsin6_in_sock(nam);
580 /* scope issues will be handled in in6_getsockaddr(). */
581 error = in6_getsockaddr(so, nam);
588 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
594 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
597 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
598 error = in_getpeeraddr(so, nam);
600 in6_sin_2_v4mapsin6_in_sock(nam);
603 /* scope issues will be handled in in6_getpeeraddr(). */
604 error = in6_getpeeraddr(so, nam);
610 * Pass some notification to all connections of a protocol
611 * associated with address dst. The local address and/or port numbers
612 * may be specified to limit the search. The "usual action" will be
613 * taken, depending on the ctlinput cmd. The caller must filter any
614 * cmds that are uninteresting (e.g., no error in the map).
615 * Call the protocol specific routine (if any) to report
616 * any errors for each matching socket.
619 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
620 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
621 int cmd, void *cmdarg,
622 struct inpcb *(*notify)(struct inpcb *, int))
624 struct inpcb *inp, *inp_temp;
625 struct sockaddr_in6 sa6_src, *sa6_dst;
626 u_short fport = fport_arg, lport = lport_arg;
630 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
633 sa6_dst = (struct sockaddr_in6 *)dst;
634 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
638 * note that src can be NULL when we get notify by local fragmentation.
640 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
641 flowinfo = sa6_src.sin6_flowinfo;
644 * Redirects go to all references to the destination,
645 * and use in6_rtchange to invalidate the route cache.
646 * Dead host indications: also use in6_rtchange to invalidate
647 * the cache, and deliver the error to all the sockets.
648 * Otherwise, if we have knowledge of the local port and address,
649 * deliver only to that socket.
651 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
654 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
656 if (cmd != PRC_HOSTDEAD)
657 notify = in6_rtchange;
659 errno = inet6ctlerrmap[cmd];
660 INP_INFO_WLOCK(pcbinfo);
661 CK_LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
663 if ((inp->inp_vflag & INP_IPV6) == 0) {
669 * If the error designates a new path MTU for a destination
670 * and the application (associated with this socket) wanted to
671 * know the value, notify.
672 * XXX: should we avoid to notify the value to TCP sockets?
674 if (cmd == PRC_MSGSIZE && cmdarg != NULL)
675 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
676 *(u_int32_t *)cmdarg);
679 * Detect if we should notify the error. If no source and
680 * destination ports are specifed, but non-zero flowinfo and
681 * local address match, notify the error. This is the case
682 * when the error is delivered with an encrypted buffer
683 * by ESP. Otherwise, just compare addresses and ports
686 if (lport == 0 && fport == 0 && flowinfo &&
687 inp->inp_socket != NULL &&
688 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
689 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
691 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
692 &sa6_dst->sin6_addr) ||
693 inp->inp_socket == 0 ||
694 (lport && inp->inp_lport != lport) ||
695 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
696 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
697 &sa6_src.sin6_addr)) ||
698 (fport && inp->inp_fport != fport)) {
705 if ((*notify)(inp, errno))
710 INP_INFO_WUNLOCK(pcbinfo);
714 * Lookup a PCB based on the local address and port. Caller must hold the
715 * hash lock. No inpcb locks or references are acquired.
718 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
719 u_short lport, int lookupflags, struct ucred *cred)
722 int matchwild = 3, wildcard;
724 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
725 ("%s: invalid lookup flags %d", __func__, lookupflags));
727 INP_HASH_WLOCK_ASSERT(pcbinfo);
729 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
730 struct inpcbhead *head;
732 * Look for an unconnected (wildcard foreign addr) PCB that
733 * matches the local address and port we're looking for.
735 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
736 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
737 pcbinfo->ipi_hashmask)];
738 CK_LIST_FOREACH(inp, head, inp_hash) {
739 /* XXX inp locking */
740 if ((inp->inp_vflag & INP_IPV6) == 0)
742 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
743 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
744 inp->inp_lport == lport) {
747 prison_equal_ip6(cred->cr_prison,
748 inp->inp_cred->cr_prison))
757 struct inpcbporthead *porthash;
758 struct inpcbport *phd;
759 struct inpcb *match = NULL;
761 * Best fit PCB lookup.
763 * First see if this local port is in use by looking on the
766 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
767 pcbinfo->ipi_porthashmask)];
768 CK_LIST_FOREACH(phd, porthash, phd_hash) {
769 if (phd->phd_port == lport)
774 * Port is in use by one or more PCBs. Look for best
777 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
780 !prison_equal_ip6(cred->cr_prison,
781 inp->inp_cred->cr_prison))
783 /* XXX inp locking */
784 if ((inp->inp_vflag & INP_IPV6) == 0)
786 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
788 if (!IN6_IS_ADDR_UNSPECIFIED(
790 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
792 else if (!IN6_ARE_ADDR_EQUAL(
793 &inp->in6p_laddr, laddr))
796 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
799 if (wildcard < matchwild) {
801 matchwild = wildcard;
812 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
815 struct in6_multi *inm;
816 struct in6_mfilter *imf;
817 struct ip6_moptions *im6o;
819 INP_INFO_WLOCK(pcbinfo);
820 CK_LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
822 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
826 im6o = inp->in6p_moptions;
827 if ((inp->inp_vflag & INP_IPV6) && im6o != NULL) {
829 * Unselect the outgoing ifp for multicast if it
832 if (im6o->im6o_multicast_ifp == ifp)
833 im6o->im6o_multicast_ifp = NULL;
835 * Drop multicast group membership if we joined
836 * through the interface being detached.
839 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) {
840 if ((inm = imf->im6f_in6m) == NULL)
842 if (inm->in6m_ifp != ifp)
844 ip6_mfilter_remove(&im6o->im6o_head, imf);
845 IN6_MULTI_LOCK_ASSERT();
846 in6_leavegroup_locked(inm, NULL);
847 ip6_mfilter_free(imf);
853 INP_INFO_WUNLOCK(pcbinfo);
857 * Check for alternatives when higher level complains
858 * about service problems. For now, invalidate cached
859 * routing information. If the route was created dynamically
860 * (by a redirect), time to try a default gateway again.
863 in6_losing(struct inpcb *inp)
866 RO_INVALIDATE_CACHE(&inp->inp_route6);
870 * After a routing change, flush old routing
871 * and allocate a (hopefully) better one.
874 in6_rtchange(struct inpcb *inp, int errno __unused)
877 RO_INVALIDATE_CACHE(&inp->inp_route6);
881 static struct inpcb *
882 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
883 const struct in6_addr *laddr, uint16_t lport, const struct in6_addr *faddr,
884 uint16_t fport, int lookupflags)
886 struct inpcb *local_wild;
887 const struct inpcblbgrouphead *hdr;
888 struct inpcblbgroup *grp;
891 INP_HASH_LOCK_ASSERT(pcbinfo);
893 hdr = &pcbinfo->ipi_lbgrouphashbase[
894 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
897 * Order of socket selection:
899 * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD).
902 * - Load balanced group does not contain jailed sockets.
903 * - Load balanced does not contain IPv4 mapped INET6 wild sockets.
906 CK_LIST_FOREACH(grp, hdr, il_list) {
908 if (!(grp->il_vflag & INP_IPV6))
911 if (grp->il_lport != lport)
914 idx = INP_PCBLBGROUP_PKTHASH(INP6_PCBHASHKEY(faddr), lport,
915 fport) % grp->il_inpcnt;
916 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr))
917 return (grp->il_inp[idx]);
918 if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr) &&
919 (lookupflags & INPLOOKUP_WILDCARD) != 0)
920 local_wild = grp->il_inp[idx];
927 * Lookup PCB in hash list, using pcbgroup tables.
929 static struct inpcb *
930 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
931 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
932 u_int lport_arg, int lookupflags, struct ifnet *ifp)
934 struct inpcbhead *head;
935 struct inpcb *inp, *tmpinp;
936 u_short fport = fport_arg, lport = lport_arg;
940 * First look for an exact match.
943 INP_GROUP_LOCK(pcbgroup);
944 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(
945 INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)];
946 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
947 /* XXX inp locking */
948 if ((inp->inp_vflag & INP_IPV6) == 0)
950 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
951 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
952 inp->inp_fport == fport &&
953 inp->inp_lport == lport) {
955 * XXX We should be able to directly return
956 * the inp here, without any checks.
957 * Well unless both bound with SO_REUSEPORT?
959 if (prison_flag(inp->inp_cred, PR_IP6))
965 if (tmpinp != NULL) {
971 * Then look for a wildcard match in the pcbgroup.
973 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
974 struct inpcb *local_wild = NULL, *local_exact = NULL;
975 struct inpcb *jail_wild = NULL;
979 * Order of socket selection - we always prefer jails.
980 * 1. jailed, non-wild.
982 * 3. non-jailed, non-wild.
983 * 4. non-jailed, wild.
985 head = &pcbgroup->ipg_hashbase[
986 INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)];
987 CK_LIST_FOREACH(inp, head, inp_pcbgrouphash) {
988 /* XXX inp locking */
989 if ((inp->inp_vflag & INP_IPV6) == 0)
992 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
993 inp->inp_lport != lport) {
997 injail = prison_flag(inp->inp_cred, PR_IP6);
999 if (prison_check_ip6(inp->inp_cred,
1003 if (local_exact != NULL)
1007 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1012 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1018 } /* LIST_FOREACH */
1032 * Then look for a wildcard match, if requested.
1034 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1035 struct inpcb *local_wild = NULL, *local_exact = NULL;
1036 struct inpcb *jail_wild = NULL;
1040 * Order of socket selection - we always prefer jails.
1041 * 1. jailed, non-wild.
1043 * 3. non-jailed, non-wild.
1044 * 4. non-jailed, wild.
1046 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(
1047 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1048 pcbinfo->ipi_wildmask)];
1049 CK_LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
1050 /* XXX inp locking */
1051 if ((inp->inp_vflag & INP_IPV6) == 0)
1054 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1055 inp->inp_lport != lport) {
1059 injail = prison_flag(inp->inp_cred, PR_IP6);
1061 if (prison_check_ip6(inp->inp_cred,
1065 if (local_exact != NULL)
1069 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1074 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1080 } /* LIST_FOREACH */
1091 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1092 INP_GROUP_UNLOCK(pcbgroup);
1096 if (lookupflags & INPLOOKUP_WLOCKPCB)
1097 locked = INP_TRY_WLOCK(inp);
1098 else if (lookupflags & INPLOOKUP_RLOCKPCB)
1099 locked = INP_TRY_RLOCK(inp);
1101 panic("%s: locking buf", __func__);
1104 INP_GROUP_UNLOCK(pcbgroup);
1106 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1108 if (in_pcbrele_wlocked(inp))
1112 if (in_pcbrele_rlocked(inp))
1117 if (lookupflags & INPLOOKUP_WLOCKPCB)
1118 INP_WLOCK_ASSERT(inp);
1120 INP_RLOCK_ASSERT(inp);
1124 #endif /* PCBGROUP */
1127 * Lookup PCB in hash list. Used in in_pcb.c as well as here.
1130 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1131 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1132 int lookupflags, struct ifnet *ifp)
1134 struct inpcbhead *head;
1135 struct inpcb *inp, *tmpinp;
1136 u_short fport = fport_arg, lport = lport_arg;
1138 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1139 ("%s: invalid lookup flags %d", __func__, lookupflags));
1141 INP_HASH_LOCK_ASSERT(pcbinfo);
1144 * First look for an exact match.
1147 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1148 INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)];
1149 CK_LIST_FOREACH(inp, head, inp_hash) {
1150 /* XXX inp locking */
1151 if ((inp->inp_vflag & INP_IPV6) == 0)
1153 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1154 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1155 inp->inp_fport == fport &&
1156 inp->inp_lport == lport) {
1158 * XXX We should be able to directly return
1159 * the inp here, without any checks.
1160 * Well unless both bound with SO_REUSEPORT?
1162 if (prison_flag(inp->inp_cred, PR_IP6))
1172 * Then look in lb group (for wildcard match).
1174 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1175 inp = in6_pcblookup_lbgroup(pcbinfo, laddr, lport, faddr,
1176 fport, lookupflags);
1182 * Then look for a wildcard match, if requested.
1184 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1185 struct inpcb *local_wild = NULL, *local_exact = NULL;
1186 struct inpcb *jail_wild = NULL;
1190 * Order of socket selection - we always prefer jails.
1191 * 1. jailed, non-wild.
1193 * 3. non-jailed, non-wild.
1194 * 4. non-jailed, wild.
1196 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1197 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1198 pcbinfo->ipi_hashmask)];
1199 CK_LIST_FOREACH(inp, head, inp_hash) {
1200 /* XXX inp locking */
1201 if ((inp->inp_vflag & INP_IPV6) == 0)
1204 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1205 inp->inp_lport != lport) {
1209 injail = prison_flag(inp->inp_cred, PR_IP6);
1211 if (prison_check_ip6(inp->inp_cred,
1215 if (local_exact != NULL)
1219 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1224 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1230 } /* LIST_FOREACH */
1232 if (jail_wild != NULL)
1234 if (local_exact != NULL)
1235 return (local_exact);
1236 if (local_wild != NULL)
1237 return (local_wild);
1238 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1247 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1248 * hash list lock, and will return the inpcb locked (i.e., requires
1249 * INPLOOKUP_LOCKPCB).
1251 static struct inpcb *
1252 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1253 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1258 INP_HASH_RLOCK(pcbinfo);
1259 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1260 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1262 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1264 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1268 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1270 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
1275 panic("%s: locking bug", __func__);
1278 if (lookupflags & INPLOOKUP_WLOCKPCB)
1279 INP_WLOCK_ASSERT(inp);
1281 INP_RLOCK_ASSERT(inp);
1285 INP_HASH_RUNLOCK(pcbinfo);
1290 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1291 * from which a pre-calculated hash value may be extracted.
1293 * Possibly more of this logic should be in in6_pcbgroup.c.
1296 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1297 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1299 #if defined(PCBGROUP) && !defined(RSS)
1300 struct inpcbgroup *pcbgroup;
1303 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1304 ("%s: invalid lookup flags %d", __func__, lookupflags));
1305 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1306 ("%s: LOCKPCB not set", __func__));
1309 * When not using RSS, use connection groups in preference to the
1310 * reservation table when looking up 4-tuples. When using RSS, just
1311 * use the reservation table, due to the cost of the Toeplitz hash
1314 * XXXRW: This policy belongs in the pcbgroup code, as in principle
1315 * we could be doing RSS with a non-Toeplitz hash that is affordable
1318 #if defined(PCBGROUP) && !defined(RSS)
1319 if (in_pcbgroup_enabled(pcbinfo)) {
1320 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1322 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1323 laddr, lport, lookupflags, ifp));
1326 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1331 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1332 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1333 struct ifnet *ifp, struct mbuf *m)
1336 struct inpcbgroup *pcbgroup;
1339 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1340 ("%s: invalid lookup flags %d", __func__, lookupflags));
1341 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1342 ("%s: LOCKPCB not set", __func__));
1346 * If we can use a hardware-generated hash to look up the connection
1347 * group, use that connection group to find the inpcb. Otherwise
1348 * fall back on a software hash -- or the reservation table if we're
1351 * XXXRW: As above, that policy belongs in the pcbgroup code.
1353 if (in_pcbgroup_enabled(pcbinfo) &&
1354 M_HASHTYPE_TEST(m, M_HASHTYPE_NONE) == 0) {
1355 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1356 m->m_pkthdr.flowid);
1357 if (pcbgroup != NULL)
1358 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1359 fport, laddr, lport, lookupflags, ifp));
1361 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1363 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1364 laddr, lport, lookupflags, ifp));
1368 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1373 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1377 ip = mtod(m, struct ip6_hdr *);
1378 bzero(sin6, sizeof(*sin6));
1379 sin6->sin6_len = sizeof(*sin6);
1380 sin6->sin6_family = AF_INET6;
1381 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1383 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */