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>
114 static struct inpcb *in6_pcblookup_hash_locked(struct inpcbinfo *,
115 struct in6_addr *, u_int, struct in6_addr *, u_int, int, struct ifnet *);
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);
128 INP_WLOCK_ASSERT(inp);
129 INP_HASH_WLOCK_ASSERT(pcbinfo);
131 if (CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */
132 return (EADDRNOTAVAIL);
133 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
135 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
136 lookupflags = INPLOOKUP_WILDCARD;
138 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
139 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
142 sin6 = (struct sockaddr_in6 *)nam;
143 if (nam->sa_len != sizeof(*sin6))
148 if (nam->sa_family != AF_INET6)
149 return (EAFNOSUPPORT);
151 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
154 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
155 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
158 lport = sin6->sin6_port;
159 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
161 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
162 * allow compepte duplication of binding if
163 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
164 * and a multicast address is bound on both
165 * new and duplicated sockets.
167 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
168 reuseport = SO_REUSEADDR|SO_REUSEPORT;
169 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
172 sin6->sin6_port = 0; /* yech... */
174 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
176 (inp->inp_flags & INP_BINDANY) == 0) {
178 return (EADDRNOTAVAIL);
182 * XXX: bind to an anycast address might accidentally
183 * cause sending a packet with anycast source address.
184 * We should allow to bind to a deprecated address, since
185 * the application dares to use it.
188 ((struct in6_ifaddr *)ifa)->ia6_flags &
189 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
191 return (EADDRNOTAVAIL);
200 if (ntohs(lport) <= V_ipport_reservedhigh &&
201 ntohs(lport) >= V_ipport_reservedlow &&
202 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
205 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
206 priv_check_cred(inp->inp_cred,
207 PRIV_NETINET_REUSEPORT, 0) != 0) {
208 t = in6_pcblookup_local(pcbinfo,
209 &sin6->sin6_addr, lport,
210 INPLOOKUP_WILDCARD, cred);
212 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
213 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
214 (so->so_type != SOCK_STREAM ||
215 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
216 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
217 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
218 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
219 (inp->inp_cred->cr_uid !=
220 t->inp_cred->cr_uid))
224 * If the socket is a BINDMULTI socket, then
225 * the credentials need to match and the
226 * original socket also has to have been bound
229 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
233 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
234 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
235 struct sockaddr_in sin;
237 in6_sin6_2_sin(&sin, sin6);
238 t = in_pcblookup_local(pcbinfo,
240 INPLOOKUP_WILDCARD, cred);
242 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
244 INP_TIMEWAIT) == 0) &&
245 (so->so_type != SOCK_STREAM ||
246 ntohl(t->inp_faddr.s_addr) ==
248 (inp->inp_cred->cr_uid !=
249 t->inp_cred->cr_uid))
252 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
257 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
258 lport, lookupflags, cred);
259 if (t && (t->inp_flags & INP_TIMEWAIT)) {
261 * XXXRW: If an incpb has had its timewait
262 * state recycled, we treat the address as
263 * being in use (for now). This is better
264 * than a panic, but not desirable.
268 (reuseport & tw->tw_so_options) == 0)
270 } else if (t && (reuseport & inp_so_options(t)) == 0) {
274 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
275 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
276 struct sockaddr_in sin;
278 in6_sin6_2_sin(&sin, sin6);
279 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
280 lport, lookupflags, cred);
281 if (t && t->inp_flags & INP_TIMEWAIT) {
285 if ((reuseport & tw->tw_so_options) == 0
286 && (ntohl(t->inp_laddr.s_addr) !=
287 INADDR_ANY || ((inp->inp_vflag &
289 (t->inp_vflag & INP_IPV6PROTO))))
292 (reuseport & inp_so_options(t)) == 0 &&
293 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
294 (t->inp_vflag & INP_IPV6PROTO) != 0))
299 inp->in6p_laddr = sin6->sin6_addr;
302 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
303 /* Undo an address bind that may have occurred. */
304 inp->in6p_laddr = in6addr_any;
308 inp->inp_lport = lport;
309 if (in_pcbinshash(inp) != 0) {
310 inp->in6p_laddr = in6addr_any;
319 * Transform old in6_pcbconnect() into an inner subroutine for new
320 * in6_pcbconnect(): Do some validity-checking on the remote
321 * address (in mbuf 'nam') and then determine local host address
322 * (i.e., which interface) to use to access that remote host.
324 * This preserves definition of in6_pcbconnect(), while supporting a
325 * slightly different version for T/TCP. (This is more than
326 * a bit of a kludge, but cleaning up the internal interfaces would
327 * have forced minor changes in every protocol).
330 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
331 struct in6_addr *plocal_addr6)
333 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
335 int scope_ambiguous = 0;
336 struct in6_addr in6a;
338 INP_WLOCK_ASSERT(inp);
339 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
341 if (nam->sa_len != sizeof (*sin6))
343 if (sin6->sin6_family != AF_INET6)
344 return (EAFNOSUPPORT);
345 if (sin6->sin6_port == 0)
346 return (EADDRNOTAVAIL);
348 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
350 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
353 if (!CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) {
355 * If the destination address is UNSPECIFIED addr,
356 * use the loopback addr, e.g ::1.
358 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
359 sin6->sin6_addr = in6addr_loopback;
361 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
364 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
365 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
370 * Do not update this earlier, in case we return with an error.
372 * XXX: this in6_selectsrc_socket result might replace the bound local
373 * address with the address specified by setsockopt(IPV6_PKTINFO).
374 * Is it the intended behavior?
376 *plocal_addr6 = in6a;
379 * Don't do pcblookup call here; return interface in
381 * and exit to caller, that will do the lookup.
389 * Connect from a socket to a specified address.
390 * Both address and port must be specified in argument sin.
391 * If don't have a local address for this socket yet,
395 in6_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
396 struct ucred *cred, struct mbuf *m)
398 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
399 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
400 struct in6_addr addr6;
403 INP_WLOCK_ASSERT(inp);
404 INP_HASH_WLOCK_ASSERT(pcbinfo);
407 * Call inner routine, to assign local interface address.
408 * in6_pcbladdr() may automatically fill in sin6_scope_id.
410 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
413 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
415 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
416 ? &addr6 : &inp->in6p_laddr,
417 inp->inp_lport, 0, NULL) != NULL) {
420 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
421 if (inp->inp_lport == 0) {
422 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
426 inp->in6p_laddr = addr6;
428 inp->in6p_faddr = sin6->sin6_addr;
429 inp->inp_fport = sin6->sin6_port;
430 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
431 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
432 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
434 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
436 in_pcbrehash_mbuf(inp, m);
442 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
445 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL));
449 in6_pcbdisconnect(struct inpcb *inp)
452 INP_WLOCK_ASSERT(inp);
453 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
455 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
457 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
458 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
463 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
465 struct sockaddr_in6 *sin6;
467 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
468 bzero(sin6, sizeof *sin6);
469 sin6->sin6_family = AF_INET6;
470 sin6->sin6_len = sizeof(*sin6);
471 sin6->sin6_port = port;
472 sin6->sin6_addr = *addr_p;
473 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
475 return (struct sockaddr *)sin6;
479 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
481 struct sockaddr_in sin;
482 struct sockaddr_in6 *sin6_p;
484 bzero(&sin, sizeof sin);
485 sin.sin_family = AF_INET;
486 sin.sin_len = sizeof(sin);
488 sin.sin_addr = *addr_p;
490 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
492 in6_sin_2_v4mapsin6(&sin, sin6_p);
494 return (struct sockaddr *)sin6_p;
498 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
501 struct in6_addr addr;
505 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
508 port = inp->inp_lport;
509 addr = inp->in6p_laddr;
512 *nam = in6_sockaddr(port, &addr);
517 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
520 struct in6_addr addr;
524 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
527 port = inp->inp_fport;
528 addr = inp->in6p_faddr;
531 *nam = in6_sockaddr(port, &addr);
536 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
542 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
545 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
546 error = in_getsockaddr(so, nam);
548 in6_sin_2_v4mapsin6_in_sock(nam);
552 /* scope issues will be handled in in6_getsockaddr(). */
553 error = in6_getsockaddr(so, nam);
560 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
566 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
569 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
570 error = in_getpeeraddr(so, nam);
572 in6_sin_2_v4mapsin6_in_sock(nam);
575 /* scope issues will be handled in in6_getpeeraddr(). */
576 error = in6_getpeeraddr(so, nam);
582 * Pass some notification to all connections of a protocol
583 * associated with address dst. The local address and/or port numbers
584 * may be specified to limit the search. The "usual action" will be
585 * taken, depending on the ctlinput cmd. The caller must filter any
586 * cmds that are uninteresting (e.g., no error in the map).
587 * Call the protocol specific routine (if any) to report
588 * any errors for each matching socket.
591 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
592 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
593 int cmd, void *cmdarg,
594 struct inpcb *(*notify)(struct inpcb *, int))
596 struct inpcb *inp, *inp_temp;
597 struct sockaddr_in6 sa6_src, *sa6_dst;
598 u_short fport = fport_arg, lport = lport_arg;
602 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
605 sa6_dst = (struct sockaddr_in6 *)dst;
606 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
610 * note that src can be NULL when we get notify by local fragmentation.
612 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
613 flowinfo = sa6_src.sin6_flowinfo;
616 * Redirects go to all references to the destination,
617 * and use in6_rtchange to invalidate the route cache.
618 * Dead host indications: also use in6_rtchange to invalidate
619 * the cache, and deliver the error to all the sockets.
620 * Otherwise, if we have knowledge of the local port and address,
621 * deliver only to that socket.
623 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
626 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
628 if (cmd != PRC_HOSTDEAD)
629 notify = in6_rtchange;
631 errno = inet6ctlerrmap[cmd];
632 INP_INFO_WLOCK(pcbinfo);
633 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
635 if ((inp->inp_vflag & INP_IPV6) == 0) {
641 * If the error designates a new path MTU for a destination
642 * and the application (associated with this socket) wanted to
643 * know the value, notify.
644 * XXX: should we avoid to notify the value to TCP sockets?
646 if (cmd == PRC_MSGSIZE && cmdarg != NULL)
647 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
648 *(u_int32_t *)cmdarg);
651 * Detect if we should notify the error. If no source and
652 * destination ports are specifed, but non-zero flowinfo and
653 * local address match, notify the error. This is the case
654 * when the error is delivered with an encrypted buffer
655 * by ESP. Otherwise, just compare addresses and ports
658 if (lport == 0 && fport == 0 && flowinfo &&
659 inp->inp_socket != NULL &&
660 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
661 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
663 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
664 &sa6_dst->sin6_addr) ||
665 inp->inp_socket == 0 ||
666 (lport && inp->inp_lport != lport) ||
667 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
668 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
669 &sa6_src.sin6_addr)) ||
670 (fport && inp->inp_fport != fport)) {
677 if ((*notify)(inp, errno))
682 INP_INFO_WUNLOCK(pcbinfo);
686 * Lookup a PCB based on the local address and port. Caller must hold the
687 * hash lock. No inpcb locks or references are acquired.
690 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
691 u_short lport, int lookupflags, struct ucred *cred)
694 int matchwild = 3, wildcard;
696 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
697 ("%s: invalid lookup flags %d", __func__, lookupflags));
699 INP_HASH_WLOCK_ASSERT(pcbinfo);
701 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
702 struct inpcbhead *head;
704 * Look for an unconnected (wildcard foreign addr) PCB that
705 * matches the local address and port we're looking for.
707 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
708 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
709 pcbinfo->ipi_hashmask)];
710 LIST_FOREACH(inp, head, inp_hash) {
711 /* XXX inp locking */
712 if ((inp->inp_vflag & INP_IPV6) == 0)
714 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
715 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
716 inp->inp_lport == lport) {
719 prison_equal_ip6(cred->cr_prison,
720 inp->inp_cred->cr_prison))
729 struct inpcbporthead *porthash;
730 struct inpcbport *phd;
731 struct inpcb *match = NULL;
733 * Best fit PCB lookup.
735 * First see if this local port is in use by looking on the
738 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
739 pcbinfo->ipi_porthashmask)];
740 LIST_FOREACH(phd, porthash, phd_hash) {
741 if (phd->phd_port == lport)
746 * Port is in use by one or more PCBs. Look for best
749 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
752 !prison_equal_ip6(cred->cr_prison,
753 inp->inp_cred->cr_prison))
755 /* XXX inp locking */
756 if ((inp->inp_vflag & INP_IPV6) == 0)
758 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
760 if (!IN6_IS_ADDR_UNSPECIFIED(
762 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
764 else if (!IN6_ARE_ADDR_EQUAL(
765 &inp->in6p_laddr, laddr))
768 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
771 if (wildcard < matchwild) {
773 matchwild = wildcard;
784 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
787 struct ip6_moptions *im6o;
790 INP_INFO_WLOCK(pcbinfo);
791 LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
793 im6o = in6p->in6p_moptions;
794 if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
796 * Unselect the outgoing ifp for multicast if it
799 if (im6o->im6o_multicast_ifp == ifp)
800 im6o->im6o_multicast_ifp = NULL;
802 * Drop multicast group membership if we joined
803 * through the interface being detached.
806 for (i = 0; i < im6o->im6o_num_memberships; i++) {
807 if (im6o->im6o_membership[i]->in6m_ifp ==
809 in6_leavegroup(im6o->im6o_membership[i], NULL);
811 } else if (gap != 0) {
812 im6o->im6o_membership[i - gap] =
813 im6o->im6o_membership[i];
816 im6o->im6o_num_memberships -= gap;
820 INP_INFO_WUNLOCK(pcbinfo);
824 * Check for alternatives when higher level complains
825 * about service problems. For now, invalidate cached
826 * routing information. If the route was created dynamically
827 * (by a redirect), time to try a default gateway again.
830 in6_losing(struct inpcb *in6p)
833 if (in6p->inp_route6.ro_rt) {
834 RTFREE(in6p->inp_route6.ro_rt);
835 in6p->inp_route6.ro_rt = (struct rtentry *)NULL;
837 if (in6p->inp_route.ro_lle)
838 LLE_FREE(in6p->inp_route.ro_lle); /* zeros ro_lle */
843 * After a routing change, flush old routing
844 * and allocate a (hopefully) better one.
847 in6_rtchange(struct inpcb *inp, int errno)
850 if (inp->inp_route6.ro_rt) {
851 RTFREE(inp->inp_route6.ro_rt);
852 inp->inp_route6.ro_rt = (struct rtentry *)NULL;
854 if (inp->inp_route.ro_lle)
855 LLE_FREE(inp->inp_route.ro_lle); /* zeros ro_lle */
861 * Lookup PCB in hash list, using pcbgroup tables.
863 static struct inpcb *
864 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
865 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
866 u_int lport_arg, int lookupflags, struct ifnet *ifp)
868 struct inpcbhead *head;
869 struct inpcb *inp, *tmpinp;
870 u_short fport = fport_arg, lport = lport_arg;
874 * First look for an exact match.
877 INP_GROUP_LOCK(pcbgroup);
878 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(
879 INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)];
880 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
881 /* XXX inp locking */
882 if ((inp->inp_vflag & INP_IPV6) == 0)
884 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
885 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
886 inp->inp_fport == fport &&
887 inp->inp_lport == lport) {
889 * XXX We should be able to directly return
890 * the inp here, without any checks.
891 * Well unless both bound with SO_REUSEPORT?
893 if (prison_flag(inp->inp_cred, PR_IP6))
899 if (tmpinp != NULL) {
905 * Then look for a wildcard match in the pcbgroup.
907 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
908 struct inpcb *local_wild = NULL, *local_exact = NULL;
909 struct inpcb *jail_wild = NULL;
913 * Order of socket selection - we always prefer jails.
914 * 1. jailed, non-wild.
916 * 3. non-jailed, non-wild.
917 * 4. non-jailed, wild.
919 head = &pcbgroup->ipg_hashbase[
920 INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)];
921 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
922 /* XXX inp locking */
923 if ((inp->inp_vflag & INP_IPV6) == 0)
926 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
927 inp->inp_lport != lport) {
931 injail = prison_flag(inp->inp_cred, PR_IP6);
933 if (prison_check_ip6(inp->inp_cred,
937 if (local_exact != NULL)
941 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
946 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
966 * Then look for a wildcard match, if requested.
968 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
969 struct inpcb *local_wild = NULL, *local_exact = NULL;
970 struct inpcb *jail_wild = NULL;
974 * Order of socket selection - we always prefer jails.
975 * 1. jailed, non-wild.
977 * 3. non-jailed, non-wild.
978 * 4. non-jailed, wild.
980 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(
981 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
982 pcbinfo->ipi_wildmask)];
983 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
984 /* XXX inp locking */
985 if ((inp->inp_vflag & INP_IPV6) == 0)
988 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
989 inp->inp_lport != lport) {
993 injail = prison_flag(inp->inp_cred, PR_IP6);
995 if (prison_check_ip6(inp->inp_cred,
999 if (local_exact != NULL)
1003 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1008 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1014 } /* LIST_FOREACH */
1025 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1026 INP_GROUP_UNLOCK(pcbgroup);
1030 if (lookupflags & INPLOOKUP_WLOCKPCB)
1031 locked = INP_TRY_WLOCK(inp);
1032 else if (lookupflags & INPLOOKUP_RLOCKPCB)
1033 locked = INP_TRY_RLOCK(inp);
1035 panic("%s: locking buf", __func__);
1038 INP_GROUP_UNLOCK(pcbgroup);
1040 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1042 if (in_pcbrele_wlocked(inp))
1046 if (in_pcbrele_rlocked(inp))
1051 if (lookupflags & INPLOOKUP_WLOCKPCB)
1052 INP_WLOCK_ASSERT(inp);
1054 INP_RLOCK_ASSERT(inp);
1058 #endif /* PCBGROUP */
1061 * Lookup PCB in hash list.
1063 static struct inpcb *
1064 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1065 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1066 int lookupflags, struct ifnet *ifp)
1068 struct inpcbhead *head;
1069 struct inpcb *inp, *tmpinp;
1070 u_short fport = fport_arg, lport = lport_arg;
1072 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1073 ("%s: invalid lookup flags %d", __func__, lookupflags));
1075 INP_HASH_LOCK_ASSERT(pcbinfo);
1078 * First look for an exact match.
1081 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1082 INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)];
1083 LIST_FOREACH(inp, head, inp_hash) {
1084 /* XXX inp locking */
1085 if ((inp->inp_vflag & INP_IPV6) == 0)
1087 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1088 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1089 inp->inp_fport == fport &&
1090 inp->inp_lport == lport) {
1092 * XXX We should be able to directly return
1093 * the inp here, without any checks.
1094 * Well unless both bound with SO_REUSEPORT?
1096 if (prison_flag(inp->inp_cred, PR_IP6))
1106 * Then look for a wildcard match, if requested.
1108 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1109 struct inpcb *local_wild = NULL, *local_exact = NULL;
1110 struct inpcb *jail_wild = NULL;
1114 * Order of socket selection - we always prefer jails.
1115 * 1. jailed, non-wild.
1117 * 3. non-jailed, non-wild.
1118 * 4. non-jailed, wild.
1120 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1121 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1122 pcbinfo->ipi_hashmask)];
1123 LIST_FOREACH(inp, head, inp_hash) {
1124 /* XXX inp locking */
1125 if ((inp->inp_vflag & INP_IPV6) == 0)
1128 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1129 inp->inp_lport != lport) {
1133 injail = prison_flag(inp->inp_cred, PR_IP6);
1135 if (prison_check_ip6(inp->inp_cred,
1139 if (local_exact != NULL)
1143 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1148 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1154 } /* LIST_FOREACH */
1156 if (jail_wild != NULL)
1158 if (local_exact != NULL)
1159 return (local_exact);
1160 if (local_wild != NULL)
1161 return (local_wild);
1162 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1171 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1172 * hash list lock, and will return the inpcb locked (i.e., requires
1173 * INPLOOKUP_LOCKPCB).
1175 static struct inpcb *
1176 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1177 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1183 INP_HASH_RLOCK(pcbinfo);
1184 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1185 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1187 if (lookupflags & INPLOOKUP_WLOCKPCB)
1188 locked = INP_TRY_WLOCK(inp);
1189 else if (lookupflags & INPLOOKUP_RLOCKPCB)
1190 locked = INP_TRY_RLOCK(inp);
1192 panic("%s: locking bug", __func__);
1195 INP_HASH_RUNLOCK(pcbinfo);
1197 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1199 if (in_pcbrele_wlocked(inp))
1203 if (in_pcbrele_rlocked(inp))
1208 if (lookupflags & INPLOOKUP_WLOCKPCB)
1209 INP_WLOCK_ASSERT(inp);
1211 INP_RLOCK_ASSERT(inp);
1214 INP_HASH_RUNLOCK(pcbinfo);
1219 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1220 * from which a pre-calculated hash value may be extracted.
1222 * Possibly more of this logic should be in in6_pcbgroup.c.
1225 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1226 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1228 #if defined(PCBGROUP) && !defined(RSS)
1229 struct inpcbgroup *pcbgroup;
1232 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1233 ("%s: invalid lookup flags %d", __func__, lookupflags));
1234 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1235 ("%s: LOCKPCB not set", __func__));
1238 * When not using RSS, use connection groups in preference to the
1239 * reservation table when looking up 4-tuples. When using RSS, just
1240 * use the reservation table, due to the cost of the Toeplitz hash
1243 * XXXRW: This policy belongs in the pcbgroup code, as in principle
1244 * we could be doing RSS with a non-Toeplitz hash that is affordable
1247 #if defined(PCBGROUP) && !defined(RSS)
1248 if (in_pcbgroup_enabled(pcbinfo)) {
1249 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1251 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1252 laddr, lport, lookupflags, ifp));
1255 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1260 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1261 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1262 struct ifnet *ifp, struct mbuf *m)
1265 struct inpcbgroup *pcbgroup;
1268 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1269 ("%s: invalid lookup flags %d", __func__, lookupflags));
1270 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1271 ("%s: LOCKPCB not set", __func__));
1275 * If we can use a hardware-generated hash to look up the connection
1276 * group, use that connection group to find the inpcb. Otherwise
1277 * fall back on a software hash -- or the reservation table if we're
1280 * XXXRW: As above, that policy belongs in the pcbgroup code.
1282 if (in_pcbgroup_enabled(pcbinfo) &&
1283 M_HASHTYPE_TEST(m, M_HASHTYPE_NONE) == 0) {
1284 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1285 m->m_pkthdr.flowid);
1286 if (pcbgroup != NULL)
1287 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1288 fport, laddr, lport, lookupflags, ifp));
1290 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1292 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1293 laddr, lport, lookupflags, ifp));
1297 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1302 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1306 ip = mtod(m, struct ip6_hdr *);
1307 bzero(sin6, sizeof(*sin6));
1308 sin6->sin6_len = sizeof(*sin6);
1309 sin6->sin6_family = AF_INET6;
1310 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1312 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */