2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * Copyright (c) 2010-2011 Juniper Networks, Inc.
6 * Portions of this software were developed by Robert N. M. Watson under
7 * contract to Juniper Networks, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
37 * Copyright (c) 1982, 1986, 1991, 1993
38 * The Regents of the University of California. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
73 #include "opt_pcbgroup.h"
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/malloc.h>
80 #include <sys/domain.h>
81 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/sockio.h>
85 #include <sys/errno.h>
94 #include <net/if_var.h>
95 #include <net/if_types.h>
96 #include <net/route.h>
98 #include <netinet/in.h>
99 #include <netinet/in_var.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/tcp_var.h>
102 #include <netinet/ip6.h>
103 #include <netinet/ip_var.h>
105 #include <netinet6/ip6_var.h>
106 #include <netinet6/nd6.h>
107 #include <netinet/in_pcb.h>
108 #include <netinet6/in6_pcb.h>
109 #include <netinet6/scope6_var.h>
111 static struct inpcb *in6_pcblookup_hash_locked(struct inpcbinfo *,
112 struct in6_addr *, u_int, struct in6_addr *, u_int, int, struct ifnet *);
115 in6_pcbbind(register 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);
125 INP_WLOCK_ASSERT(inp);
126 INP_HASH_WLOCK_ASSERT(pcbinfo);
128 if (TAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */
129 return (EADDRNOTAVAIL);
130 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
132 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
133 lookupflags = INPLOOKUP_WILDCARD;
135 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
136 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
139 sin6 = (struct sockaddr_in6 *)nam;
140 if (nam->sa_len != sizeof(*sin6))
145 if (nam->sa_family != AF_INET6)
146 return (EAFNOSUPPORT);
148 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
151 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
152 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
155 lport = sin6->sin6_port;
156 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
158 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
159 * allow compepte duplication of binding if
160 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
161 * and a multicast address is bound on both
162 * new and duplicated sockets.
164 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
165 reuseport = SO_REUSEADDR|SO_REUSEPORT;
166 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
169 sin6->sin6_port = 0; /* yech... */
170 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
172 (inp->inp_flags & INP_BINDANY) == 0) {
173 return (EADDRNOTAVAIL);
177 * XXX: bind to an anycast address might accidentally
178 * cause sending a packet with anycast source address.
179 * We should allow to bind to a deprecated address, since
180 * the application dares to use it.
183 ((struct in6_ifaddr *)ifa)->ia6_flags &
184 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
186 return (EADDRNOTAVAIL);
196 if (ntohs(lport) <= V_ipport_reservedhigh &&
197 ntohs(lport) >= V_ipport_reservedlow &&
198 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
201 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
202 priv_check_cred(inp->inp_cred,
203 PRIV_NETINET_REUSEPORT, 0) != 0) {
204 t = in6_pcblookup_local(pcbinfo,
205 &sin6->sin6_addr, lport,
206 INPLOOKUP_WILDCARD, cred);
208 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
209 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
210 (so->so_type != SOCK_STREAM ||
211 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
212 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
213 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
214 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
215 (inp->inp_cred->cr_uid !=
216 t->inp_cred->cr_uid))
220 * If the socket is a BINDMULTI socket, then
221 * the credentials need to match and the
222 * original socket also has to have been bound
225 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
229 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
230 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
231 struct sockaddr_in sin;
233 in6_sin6_2_sin(&sin, sin6);
234 t = in_pcblookup_local(pcbinfo,
236 INPLOOKUP_WILDCARD, cred);
238 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
240 INP_TIMEWAIT) == 0) &&
241 (so->so_type != SOCK_STREAM ||
242 ntohl(t->inp_faddr.s_addr) ==
244 (inp->inp_cred->cr_uid !=
245 t->inp_cred->cr_uid))
248 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
253 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
254 lport, lookupflags, cred);
255 if (t && (t->inp_flags & INP_TIMEWAIT)) {
257 * XXXRW: If an incpb has had its timewait
258 * state recycled, we treat the address as
259 * being in use (for now). This is better
260 * than a panic, but not desirable.
264 (reuseport & tw->tw_so_options) == 0)
266 } else if (t && (reuseport & inp_so_options(t)) == 0) {
270 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
271 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
272 struct sockaddr_in sin;
274 in6_sin6_2_sin(&sin, sin6);
275 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
276 lport, lookupflags, cred);
277 if (t && t->inp_flags & INP_TIMEWAIT) {
281 if ((reuseport & tw->tw_so_options) == 0
282 && (ntohl(t->inp_laddr.s_addr) !=
283 INADDR_ANY || ((inp->inp_vflag &
285 (t->inp_vflag & INP_IPV6PROTO))))
288 (reuseport & inp_so_options(t)) == 0 &&
289 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
290 (t->inp_vflag & INP_IPV6PROTO) != 0))
295 inp->in6p_laddr = sin6->sin6_addr;
298 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
299 /* Undo an address bind that may have occurred. */
300 inp->in6p_laddr = in6addr_any;
304 inp->inp_lport = lport;
305 if (in_pcbinshash(inp) != 0) {
306 inp->in6p_laddr = in6addr_any;
315 * Transform old in6_pcbconnect() into an inner subroutine for new
316 * in6_pcbconnect(): Do some validity-checking on the remote
317 * address (in mbuf 'nam') and then determine local host address
318 * (i.e., which interface) to use to access that remote host.
320 * This preserves definition of in6_pcbconnect(), while supporting a
321 * slightly different version for T/TCP. (This is more than
322 * a bit of a kludge, but cleaning up the internal interfaces would
323 * have forced minor changes in every protocol).
326 in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
327 struct in6_addr *plocal_addr6)
329 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
331 struct ifnet *ifp = NULL;
332 int scope_ambiguous = 0;
333 struct in6_addr in6a;
335 INP_WLOCK_ASSERT(inp);
336 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
338 if (nam->sa_len != sizeof (*sin6))
340 if (sin6->sin6_family != AF_INET6)
341 return (EAFNOSUPPORT);
342 if (sin6->sin6_port == 0)
343 return (EADDRNOTAVAIL);
345 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
347 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
350 if (!TAILQ_EMPTY(&V_in6_ifaddrhead)) {
352 * If the destination address is UNSPECIFIED addr,
353 * use the loopback addr, e.g ::1.
355 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
356 sin6->sin6_addr = in6addr_loopback;
358 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
361 error = in6_selectsrc(sin6, inp->in6p_outputopts,
362 inp, NULL, inp->inp_cred, &ifp, &in6a);
366 if (ifp && scope_ambiguous &&
367 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
372 * Do not update this earlier, in case we return with an error.
374 * XXX: this in6_selectsrc result might replace the bound local
375 * address with the address specified by setsockopt(IPV6_PKTINFO).
376 * Is it the intended behavior?
378 *plocal_addr6 = in6a;
381 * Don't do pcblookup call here; return interface in
383 * and exit to caller, that will do the lookup.
391 * Connect from a socket to a specified address.
392 * Both address and port must be specified in argument sin.
393 * If don't have a local address for this socket yet,
397 in6_pcbconnect_mbuf(register struct inpcb *inp, struct sockaddr *nam,
398 struct ucred *cred, struct mbuf *m)
400 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
401 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
402 struct in6_addr addr6;
405 INP_WLOCK_ASSERT(inp);
406 INP_HASH_WLOCK_ASSERT(pcbinfo);
409 * Call inner routine, to assign local interface address.
410 * in6_pcbladdr() may automatically fill in sin6_scope_id.
412 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
415 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
417 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
418 ? &addr6 : &inp->in6p_laddr,
419 inp->inp_lport, 0, NULL) != NULL) {
422 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
423 if (inp->inp_lport == 0) {
424 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
428 inp->in6p_laddr = addr6;
430 inp->in6p_faddr = sin6->sin6_addr;
431 inp->inp_fport = sin6->sin6_port;
432 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
433 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
434 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
436 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
438 in_pcbrehash_mbuf(inp, m);
444 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
447 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL));
451 in6_pcbdisconnect(struct inpcb *inp)
454 INP_WLOCK_ASSERT(inp);
455 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
457 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
459 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
460 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
465 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
467 struct sockaddr_in6 *sin6;
469 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
470 bzero(sin6, sizeof *sin6);
471 sin6->sin6_family = AF_INET6;
472 sin6->sin6_len = sizeof(*sin6);
473 sin6->sin6_port = port;
474 sin6->sin6_addr = *addr_p;
475 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
477 return (struct sockaddr *)sin6;
481 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
483 struct sockaddr_in sin;
484 struct sockaddr_in6 *sin6_p;
486 bzero(&sin, sizeof sin);
487 sin.sin_family = AF_INET;
488 sin.sin_len = sizeof(sin);
490 sin.sin_addr = *addr_p;
492 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
494 in6_sin_2_v4mapsin6(&sin, sin6_p);
496 return (struct sockaddr *)sin6_p;
500 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
502 register struct inpcb *inp;
503 struct in6_addr addr;
507 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
510 port = inp->inp_lport;
511 addr = inp->in6p_laddr;
514 *nam = in6_sockaddr(port, &addr);
519 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
522 struct in6_addr addr;
526 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
529 port = inp->inp_fport;
530 addr = inp->in6p_faddr;
533 *nam = in6_sockaddr(port, &addr);
538 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
544 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
547 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
548 error = in_getsockaddr(so, nam);
550 in6_sin_2_v4mapsin6_in_sock(nam);
554 /* scope issues will be handled in in6_getsockaddr(). */
555 error = in6_getsockaddr(so, nam);
562 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
568 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
571 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
572 error = in_getpeeraddr(so, nam);
574 in6_sin_2_v4mapsin6_in_sock(nam);
577 /* scope issues will be handled in in6_getpeeraddr(). */
578 error = in6_getpeeraddr(so, nam);
584 * Pass some notification to all connections of a protocol
585 * associated with address dst. The local address and/or port numbers
586 * may be specified to limit the search. The "usual action" will be
587 * taken, depending on the ctlinput cmd. The caller must filter any
588 * cmds that are uninteresting (e.g., no error in the map).
589 * Call the protocol specific routine (if any) to report
590 * any errors for each matching socket.
593 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
594 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
595 int cmd, void *cmdarg,
596 struct inpcb *(*notify)(struct inpcb *, int))
598 struct inpcb *inp, *inp_temp;
599 struct sockaddr_in6 sa6_src, *sa6_dst;
600 u_short fport = fport_arg, lport = lport_arg;
604 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
607 sa6_dst = (struct sockaddr_in6 *)dst;
608 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
612 * note that src can be NULL when we get notify by local fragmentation.
614 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
615 flowinfo = sa6_src.sin6_flowinfo;
618 * Redirects go to all references to the destination,
619 * and use in6_rtchange to invalidate the route cache.
620 * Dead host indications: also use in6_rtchange to invalidate
621 * the cache, and deliver the error to all the sockets.
622 * Otherwise, if we have knowledge of the local port and address,
623 * deliver only to that socket.
625 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
628 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
630 if (cmd != PRC_HOSTDEAD)
631 notify = in6_rtchange;
633 errno = inet6ctlerrmap[cmd];
634 INP_INFO_WLOCK(pcbinfo);
635 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
637 if ((inp->inp_vflag & INP_IPV6) == 0) {
643 * If the error designates a new path MTU for a destination
644 * and the application (associated with this socket) wanted to
645 * know the value, notify. Note that we notify for all
646 * disconnected sockets if the corresponding application
647 * wanted. This is because some UDP applications keep sending
648 * sockets disconnected.
649 * XXX: should we avoid to notify the value to TCP sockets?
651 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
652 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
653 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
654 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
655 (u_int32_t *)cmdarg);
659 * Detect if we should notify the error. If no source and
660 * destination ports are specifed, but non-zero flowinfo and
661 * local address match, notify the error. This is the case
662 * when the error is delivered with an encrypted buffer
663 * by ESP. Otherwise, just compare addresses and ports
666 if (lport == 0 && fport == 0 && flowinfo &&
667 inp->inp_socket != NULL &&
668 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
669 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
671 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
672 &sa6_dst->sin6_addr) ||
673 inp->inp_socket == 0 ||
674 (lport && inp->inp_lport != lport) ||
675 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
676 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
677 &sa6_src.sin6_addr)) ||
678 (fport && inp->inp_fport != fport)) {
685 if ((*notify)(inp, errno))
690 INP_INFO_WUNLOCK(pcbinfo);
694 * Lookup a PCB based on the local address and port. Caller must hold the
695 * hash lock. No inpcb locks or references are acquired.
698 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
699 u_short lport, int lookupflags, struct ucred *cred)
701 register struct inpcb *inp;
702 int matchwild = 3, wildcard;
704 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
705 ("%s: invalid lookup flags %d", __func__, lookupflags));
707 INP_HASH_WLOCK_ASSERT(pcbinfo);
709 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
710 struct inpcbhead *head;
712 * Look for an unconnected (wildcard foreign addr) PCB that
713 * matches the local address and port we're looking for.
715 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
716 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
717 pcbinfo->ipi_hashmask)];
718 LIST_FOREACH(inp, head, inp_hash) {
719 /* XXX inp locking */
720 if ((inp->inp_vflag & INP_IPV6) == 0)
722 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
723 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
724 inp->inp_lport == lport) {
727 prison_equal_ip6(cred->cr_prison,
728 inp->inp_cred->cr_prison))
737 struct inpcbporthead *porthash;
738 struct inpcbport *phd;
739 struct inpcb *match = NULL;
741 * Best fit PCB lookup.
743 * First see if this local port is in use by looking on the
746 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
747 pcbinfo->ipi_porthashmask)];
748 LIST_FOREACH(phd, porthash, phd_hash) {
749 if (phd->phd_port == lport)
754 * Port is in use by one or more PCBs. Look for best
757 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
760 !prison_equal_ip6(cred->cr_prison,
761 inp->inp_cred->cr_prison))
763 /* XXX inp locking */
764 if ((inp->inp_vflag & INP_IPV6) == 0)
766 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
768 if (!IN6_IS_ADDR_UNSPECIFIED(
770 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
772 else if (!IN6_ARE_ADDR_EQUAL(
773 &inp->in6p_laddr, laddr))
776 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
779 if (wildcard < matchwild) {
781 matchwild = wildcard;
792 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
795 struct ip6_moptions *im6o;
798 INP_INFO_RLOCK(pcbinfo);
799 LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
801 im6o = in6p->in6p_moptions;
802 if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
804 * Unselect the outgoing ifp for multicast if it
807 if (im6o->im6o_multicast_ifp == ifp)
808 im6o->im6o_multicast_ifp = NULL;
810 * Drop multicast group membership if we joined
811 * through the interface being detached.
814 for (i = 0; i < im6o->im6o_num_memberships; i++) {
815 if (im6o->im6o_membership[i]->in6m_ifp ==
817 in6_mc_leave(im6o->im6o_membership[i],
820 } else if (gap != 0) {
821 im6o->im6o_membership[i - gap] =
822 im6o->im6o_membership[i];
825 im6o->im6o_num_memberships -= gap;
829 INP_INFO_RUNLOCK(pcbinfo);
833 * Check for alternatives when higher level complains
834 * about service problems. For now, invalidate cached
835 * routing information. If the route was created dynamically
836 * (by a redirect), time to try a default gateway again.
839 in6_losing(struct inpcb *in6p)
843 * We don't store route pointers in the routing table anymore
849 * After a routing change, flush old routing
850 * and allocate a (hopefully) better one.
853 in6_rtchange(struct inpcb *inp, int errno)
856 * We don't store route pointers in the routing table anymore
863 * Lookup PCB in hash list, using pcbgroup tables.
865 static struct inpcb *
866 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
867 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
868 u_int lport_arg, int lookupflags, struct ifnet *ifp)
870 struct inpcbhead *head;
871 struct inpcb *inp, *tmpinp;
872 u_short fport = fport_arg, lport = lport_arg;
875 if (faithprefix_p != NULL)
876 faith = (*faithprefix_p)(laddr);
881 * First look for an exact match.
884 INP_GROUP_LOCK(pcbgroup);
885 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(
886 INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)];
887 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
888 /* XXX inp locking */
889 if ((inp->inp_vflag & INP_IPV6) == 0)
891 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
892 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
893 inp->inp_fport == fport &&
894 inp->inp_lport == lport) {
896 * XXX We should be able to directly return
897 * the inp here, without any checks.
898 * Well unless both bound with SO_REUSEPORT?
900 if (prison_flag(inp->inp_cred, PR_IP6))
906 if (tmpinp != NULL) {
912 * Then look for a wildcard match in the pcbgroup.
914 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
915 struct inpcb *local_wild = NULL, *local_exact = NULL;
916 struct inpcb *jail_wild = NULL;
920 * Order of socket selection - we always prefer jails.
921 * 1. jailed, non-wild.
923 * 3. non-jailed, non-wild.
924 * 4. non-jailed, wild.
926 head = &pcbgroup->ipg_hashbase[
927 INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)];
928 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
929 /* XXX inp locking */
930 if ((inp->inp_vflag & INP_IPV6) == 0)
933 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
934 inp->inp_lport != lport) {
938 /* XXX inp locking */
939 if (faith && (inp->inp_flags & INP_FAITH) == 0)
942 injail = prison_flag(inp->inp_cred, PR_IP6);
944 if (prison_check_ip6(inp->inp_cred,
948 if (local_exact != NULL)
952 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
957 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
977 * Then look for a wildcard match, if requested.
979 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
980 struct inpcb *local_wild = NULL, *local_exact = NULL;
981 struct inpcb *jail_wild = NULL;
985 * Order of socket selection - we always prefer jails.
986 * 1. jailed, non-wild.
988 * 3. non-jailed, non-wild.
989 * 4. non-jailed, wild.
991 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(
992 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
993 pcbinfo->ipi_wildmask)];
994 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
995 /* XXX inp locking */
996 if ((inp->inp_vflag & INP_IPV6) == 0)
999 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1000 inp->inp_lport != lport) {
1004 /* XXX inp locking */
1005 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1008 injail = prison_flag(inp->inp_cred, PR_IP6);
1010 if (prison_check_ip6(inp->inp_cred,
1014 if (local_exact != NULL)
1018 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1023 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1029 } /* LIST_FOREACH */
1040 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1041 INP_GROUP_UNLOCK(pcbgroup);
1046 INP_GROUP_UNLOCK(pcbgroup);
1047 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1049 if (in_pcbrele_wlocked(inp))
1051 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1053 if (in_pcbrele_rlocked(inp))
1056 panic("%s: locking buf", __func__);
1059 #endif /* PCBGROUP */
1062 * Lookup PCB in hash list.
1064 static struct inpcb *
1065 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1066 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1067 int lookupflags, struct ifnet *ifp)
1069 struct inpcbhead *head;
1070 struct inpcb *inp, *tmpinp;
1071 u_short fport = fport_arg, lport = lport_arg;
1074 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1075 ("%s: invalid lookup flags %d", __func__, lookupflags));
1077 INP_HASH_LOCK_ASSERT(pcbinfo);
1079 if (faithprefix_p != NULL)
1080 faith = (*faithprefix_p)(laddr);
1085 * First look for an exact match.
1088 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1089 INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)];
1090 LIST_FOREACH(inp, head, inp_hash) {
1091 /* XXX inp locking */
1092 if ((inp->inp_vflag & INP_IPV6) == 0)
1094 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1095 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1096 inp->inp_fport == fport &&
1097 inp->inp_lport == lport) {
1099 * XXX We should be able to directly return
1100 * the inp here, without any checks.
1101 * Well unless both bound with SO_REUSEPORT?
1103 if (prison_flag(inp->inp_cred, PR_IP6))
1113 * Then look for a wildcard match, if requested.
1115 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1116 struct inpcb *local_wild = NULL, *local_exact = NULL;
1117 struct inpcb *jail_wild = NULL;
1121 * Order of socket selection - we always prefer jails.
1122 * 1. jailed, non-wild.
1124 * 3. non-jailed, non-wild.
1125 * 4. non-jailed, wild.
1127 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1128 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1129 pcbinfo->ipi_hashmask)];
1130 LIST_FOREACH(inp, head, inp_hash) {
1131 /* XXX inp locking */
1132 if ((inp->inp_vflag & INP_IPV6) == 0)
1135 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1136 inp->inp_lport != lport) {
1140 /* XXX inp locking */
1141 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1144 injail = prison_flag(inp->inp_cred, PR_IP6);
1146 if (prison_check_ip6(inp->inp_cred,
1150 if (local_exact != NULL)
1154 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1159 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1165 } /* LIST_FOREACH */
1167 if (jail_wild != NULL)
1169 if (local_exact != NULL)
1170 return (local_exact);
1171 if (local_wild != NULL)
1172 return (local_wild);
1173 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1182 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1183 * hash list lock, and will return the inpcb locked (i.e., requires
1184 * INPLOOKUP_LOCKPCB).
1186 static struct inpcb *
1187 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1188 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1193 INP_HASH_RLOCK(pcbinfo);
1194 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1195 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1198 INP_HASH_RUNLOCK(pcbinfo);
1199 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1201 if (in_pcbrele_wlocked(inp))
1203 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1205 if (in_pcbrele_rlocked(inp))
1208 panic("%s: locking bug", __func__);
1210 INP_HASH_RUNLOCK(pcbinfo);
1215 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1216 * from which a pre-calculated hash value may be extracted.
1218 * Possibly more of this logic should be in in6_pcbgroup.c.
1221 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1222 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1224 #if defined(PCBGROUP) && !defined(RSS)
1225 struct inpcbgroup *pcbgroup;
1228 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1229 ("%s: invalid lookup flags %d", __func__, lookupflags));
1230 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1231 ("%s: LOCKPCB not set", __func__));
1234 * When not using RSS, use connection groups in preference to the
1235 * reservation table when looking up 4-tuples. When using RSS, just
1236 * use the reservation table, due to the cost of the Toeplitz hash
1239 * XXXRW: This policy belongs in the pcbgroup code, as in principle
1240 * we could be doing RSS with a non-Toeplitz hash that is affordable
1243 #if defined(PCBGROUP) && !defined(RSS)
1244 if (in_pcbgroup_enabled(pcbinfo)) {
1245 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1247 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1248 laddr, lport, lookupflags, ifp));
1251 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1256 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1257 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1258 struct ifnet *ifp, struct mbuf *m)
1261 struct inpcbgroup *pcbgroup;
1264 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1265 ("%s: invalid lookup flags %d", __func__, lookupflags));
1266 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1267 ("%s: LOCKPCB not set", __func__));
1271 * If we can use a hardware-generated hash to look up the connection
1272 * group, use that connection group to find the inpcb. Otherwise
1273 * fall back on a software hash -- or the reservation table if we're
1276 * XXXRW: As above, that policy belongs in the pcbgroup code.
1278 if (in_pcbgroup_enabled(pcbinfo) &&
1279 !(M_HASHTYPE_TEST(m, M_HASHTYPE_NONE))) {
1280 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1281 m->m_pkthdr.flowid);
1282 if (pcbgroup != NULL)
1283 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1284 fport, laddr, lport, lookupflags, ifp));
1286 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1288 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1289 laddr, lport, lookupflags, ifp));
1293 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1298 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1302 ip = mtod(m, struct ip6_hdr *);
1303 bzero(sin6, sizeof(*sin6));
1304 sin6->sin6_len = sizeof(*sin6);
1305 sin6->sin6_family = AF_INET6;
1306 sin6->sin6_addr = ip->ip6_src;
1308 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */