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_llatbl.h>
96 #include <net/if_types.h>
97 #include <net/route.h>
99 #include <netinet/in.h>
100 #include <netinet/in_var.h>
101 #include <netinet/in_systm.h>
102 #include <netinet/tcp_var.h>
103 #include <netinet/ip6.h>
104 #include <netinet/ip_var.h>
106 #include <netinet6/ip6_var.h>
107 #include <netinet6/nd6.h>
108 #include <netinet/in_pcb.h>
109 #include <netinet6/in6_pcb.h>
110 #include <netinet6/scope6_var.h>
112 static struct inpcb *in6_pcblookup_hash_locked(struct inpcbinfo *,
113 struct in6_addr *, u_int, struct in6_addr *, u_int, int, struct ifnet *);
116 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam,
119 struct socket *so = inp->inp_socket;
120 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
121 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
123 int error, lookupflags = 0;
124 int reuseport = (so->so_options & SO_REUSEPORT);
126 INP_WLOCK_ASSERT(inp);
127 INP_HASH_WLOCK_ASSERT(pcbinfo);
129 if (TAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */
130 return (EADDRNOTAVAIL);
131 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
133 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
134 lookupflags = INPLOOKUP_WILDCARD;
136 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
137 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
140 sin6 = (struct sockaddr_in6 *)nam;
141 if (nam->sa_len != sizeof(*sin6))
146 if (nam->sa_family != AF_INET6)
147 return (EAFNOSUPPORT);
149 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
152 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
153 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
156 lport = sin6->sin6_port;
157 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
159 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
160 * allow compepte duplication of binding if
161 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
162 * and a multicast address is bound on both
163 * new and duplicated sockets.
165 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
166 reuseport = SO_REUSEADDR|SO_REUSEPORT;
167 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
170 sin6->sin6_port = 0; /* yech... */
171 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
173 (inp->inp_flags & INP_BINDANY) == 0) {
174 return (EADDRNOTAVAIL);
178 * XXX: bind to an anycast address might accidentally
179 * cause sending a packet with anycast source address.
180 * We should allow to bind to a deprecated address, since
181 * the application dares to use it.
184 ((struct in6_ifaddr *)ifa)->ia6_flags &
185 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
187 return (EADDRNOTAVAIL);
197 if (ntohs(lport) <= V_ipport_reservedhigh &&
198 ntohs(lport) >= V_ipport_reservedlow &&
199 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
202 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
203 priv_check_cred(inp->inp_cred,
204 PRIV_NETINET_REUSEPORT, 0) != 0) {
205 t = in6_pcblookup_local(pcbinfo,
206 &sin6->sin6_addr, lport,
207 INPLOOKUP_WILDCARD, cred);
209 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
210 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
211 (so->so_type != SOCK_STREAM ||
212 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
213 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
214 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
215 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
216 (inp->inp_cred->cr_uid !=
217 t->inp_cred->cr_uid))
221 * If the socket is a BINDMULTI socket, then
222 * the credentials need to match and the
223 * original socket also has to have been bound
226 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
230 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
231 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
232 struct sockaddr_in sin;
234 in6_sin6_2_sin(&sin, sin6);
235 t = in_pcblookup_local(pcbinfo,
237 INPLOOKUP_WILDCARD, cred);
239 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
241 INP_TIMEWAIT) == 0) &&
242 (so->so_type != SOCK_STREAM ||
243 ntohl(t->inp_faddr.s_addr) ==
245 (inp->inp_cred->cr_uid !=
246 t->inp_cred->cr_uid))
249 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
254 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
255 lport, lookupflags, cred);
256 if (t && (t->inp_flags & INP_TIMEWAIT)) {
258 * XXXRW: If an incpb has had its timewait
259 * state recycled, we treat the address as
260 * being in use (for now). This is better
261 * than a panic, but not desirable.
265 (reuseport & tw->tw_so_options) == 0)
267 } else if (t && (reuseport & inp_so_options(t)) == 0) {
271 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
272 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
273 struct sockaddr_in sin;
275 in6_sin6_2_sin(&sin, sin6);
276 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
277 lport, lookupflags, cred);
278 if (t && t->inp_flags & INP_TIMEWAIT) {
282 if ((reuseport & tw->tw_so_options) == 0
283 && (ntohl(t->inp_laddr.s_addr) !=
284 INADDR_ANY || ((inp->inp_vflag &
286 (t->inp_vflag & INP_IPV6PROTO))))
289 (reuseport & inp_so_options(t)) == 0 &&
290 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
291 (t->inp_vflag & INP_IPV6PROTO) != 0))
296 inp->in6p_laddr = sin6->sin6_addr;
299 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
300 /* Undo an address bind that may have occurred. */
301 inp->in6p_laddr = in6addr_any;
305 inp->inp_lport = lport;
306 if (in_pcbinshash(inp) != 0) {
307 inp->in6p_laddr = in6addr_any;
316 * Transform old in6_pcbconnect() into an inner subroutine for new
317 * in6_pcbconnect(): Do some validity-checking on the remote
318 * address (in mbuf 'nam') and then determine local host address
319 * (i.e., which interface) to use to access that remote host.
321 * This preserves definition of in6_pcbconnect(), while supporting a
322 * slightly different version for T/TCP. (This is more than
323 * a bit of a kludge, but cleaning up the internal interfaces would
324 * have forced minor changes in every protocol).
327 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
328 struct in6_addr *plocal_addr6)
330 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
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_socket(sin6, inp->in6p_outputopts,
362 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
367 * Do not update this earlier, in case we return with an error.
369 * XXX: this in6_selectsrc_socket result might replace the bound local
370 * address with the address specified by setsockopt(IPV6_PKTINFO).
371 * Is it the intended behavior?
373 *plocal_addr6 = in6a;
376 * Don't do pcblookup call here; return interface in
378 * and exit to caller, that will do the lookup.
386 * Connect from a socket to a specified address.
387 * Both address and port must be specified in argument sin.
388 * If don't have a local address for this socket yet,
392 in6_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
393 struct ucred *cred, struct mbuf *m)
395 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
396 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
397 struct in6_addr addr6;
400 INP_WLOCK_ASSERT(inp);
401 INP_HASH_WLOCK_ASSERT(pcbinfo);
404 * Call inner routine, to assign local interface address.
405 * in6_pcbladdr() may automatically fill in sin6_scope_id.
407 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
410 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
412 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
413 ? &addr6 : &inp->in6p_laddr,
414 inp->inp_lport, 0, NULL) != NULL) {
417 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
418 if (inp->inp_lport == 0) {
419 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
423 inp->in6p_laddr = addr6;
425 inp->in6p_faddr = sin6->sin6_addr;
426 inp->inp_fport = sin6->sin6_port;
427 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
428 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
429 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
431 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
433 in_pcbrehash_mbuf(inp, m);
439 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
442 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL));
446 in6_pcbdisconnect(struct inpcb *inp)
449 INP_WLOCK_ASSERT(inp);
450 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
452 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
454 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
455 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
460 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
462 struct sockaddr_in6 *sin6;
464 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
465 bzero(sin6, sizeof *sin6);
466 sin6->sin6_family = AF_INET6;
467 sin6->sin6_len = sizeof(*sin6);
468 sin6->sin6_port = port;
469 sin6->sin6_addr = *addr_p;
470 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
472 return (struct sockaddr *)sin6;
476 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
478 struct sockaddr_in sin;
479 struct sockaddr_in6 *sin6_p;
481 bzero(&sin, sizeof sin);
482 sin.sin_family = AF_INET;
483 sin.sin_len = sizeof(sin);
485 sin.sin_addr = *addr_p;
487 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
489 in6_sin_2_v4mapsin6(&sin, sin6_p);
491 return (struct sockaddr *)sin6_p;
495 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
498 struct in6_addr addr;
502 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
505 port = inp->inp_lport;
506 addr = inp->in6p_laddr;
509 *nam = in6_sockaddr(port, &addr);
514 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
517 struct in6_addr addr;
521 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
524 port = inp->inp_fport;
525 addr = inp->in6p_faddr;
528 *nam = in6_sockaddr(port, &addr);
533 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
539 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
542 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
543 error = in_getsockaddr(so, nam);
545 in6_sin_2_v4mapsin6_in_sock(nam);
549 /* scope issues will be handled in in6_getsockaddr(). */
550 error = in6_getsockaddr(so, nam);
557 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
563 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
566 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
567 error = in_getpeeraddr(so, nam);
569 in6_sin_2_v4mapsin6_in_sock(nam);
572 /* scope issues will be handled in in6_getpeeraddr(). */
573 error = in6_getpeeraddr(so, nam);
579 * Pass some notification to all connections of a protocol
580 * associated with address dst. The local address and/or port numbers
581 * may be specified to limit the search. The "usual action" will be
582 * taken, depending on the ctlinput cmd. The caller must filter any
583 * cmds that are uninteresting (e.g., no error in the map).
584 * Call the protocol specific routine (if any) to report
585 * any errors for each matching socket.
588 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
589 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
590 int cmd, void *cmdarg,
591 struct inpcb *(*notify)(struct inpcb *, int))
593 struct inpcb *inp, *inp_temp;
594 struct sockaddr_in6 sa6_src, *sa6_dst;
595 u_short fport = fport_arg, lport = lport_arg;
599 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
602 sa6_dst = (struct sockaddr_in6 *)dst;
603 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
607 * note that src can be NULL when we get notify by local fragmentation.
609 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
610 flowinfo = sa6_src.sin6_flowinfo;
613 * Redirects go to all references to the destination,
614 * and use in6_rtchange to invalidate the route cache.
615 * Dead host indications: also use in6_rtchange to invalidate
616 * the cache, and deliver the error to all the sockets.
617 * Otherwise, if we have knowledge of the local port and address,
618 * deliver only to that socket.
620 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
623 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
625 if (cmd != PRC_HOSTDEAD)
626 notify = in6_rtchange;
628 errno = inet6ctlerrmap[cmd];
629 INP_INFO_WLOCK(pcbinfo);
630 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
632 if ((inp->inp_vflag & INP_IPV6) == 0) {
638 * If the error designates a new path MTU for a destination
639 * and the application (associated with this socket) wanted to
640 * know the value, notify.
641 * XXX: should we avoid to notify the value to TCP sockets?
643 if (cmd == PRC_MSGSIZE && cmdarg != NULL)
644 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
645 *(u_int32_t *)cmdarg);
648 * Detect if we should notify the error. If no source and
649 * destination ports are specifed, but non-zero flowinfo and
650 * local address match, notify the error. This is the case
651 * when the error is delivered with an encrypted buffer
652 * by ESP. Otherwise, just compare addresses and ports
655 if (lport == 0 && fport == 0 && flowinfo &&
656 inp->inp_socket != NULL &&
657 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
658 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
660 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
661 &sa6_dst->sin6_addr) ||
662 inp->inp_socket == 0 ||
663 (lport && inp->inp_lport != lport) ||
664 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
665 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
666 &sa6_src.sin6_addr)) ||
667 (fport && inp->inp_fport != fport)) {
674 if ((*notify)(inp, errno))
679 INP_INFO_WUNLOCK(pcbinfo);
683 * Lookup a PCB based on the local address and port. Caller must hold the
684 * hash lock. No inpcb locks or references are acquired.
687 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
688 u_short lport, int lookupflags, struct ucred *cred)
691 int matchwild = 3, wildcard;
693 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
694 ("%s: invalid lookup flags %d", __func__, lookupflags));
696 INP_HASH_WLOCK_ASSERT(pcbinfo);
698 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
699 struct inpcbhead *head;
701 * Look for an unconnected (wildcard foreign addr) PCB that
702 * matches the local address and port we're looking for.
704 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
705 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
706 pcbinfo->ipi_hashmask)];
707 LIST_FOREACH(inp, head, inp_hash) {
708 /* XXX inp locking */
709 if ((inp->inp_vflag & INP_IPV6) == 0)
711 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
712 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
713 inp->inp_lport == lport) {
716 prison_equal_ip6(cred->cr_prison,
717 inp->inp_cred->cr_prison))
726 struct inpcbporthead *porthash;
727 struct inpcbport *phd;
728 struct inpcb *match = NULL;
730 * Best fit PCB lookup.
732 * First see if this local port is in use by looking on the
735 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
736 pcbinfo->ipi_porthashmask)];
737 LIST_FOREACH(phd, porthash, phd_hash) {
738 if (phd->phd_port == lport)
743 * Port is in use by one or more PCBs. Look for best
746 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
749 !prison_equal_ip6(cred->cr_prison,
750 inp->inp_cred->cr_prison))
752 /* XXX inp locking */
753 if ((inp->inp_vflag & INP_IPV6) == 0)
755 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
757 if (!IN6_IS_ADDR_UNSPECIFIED(
759 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
761 else if (!IN6_ARE_ADDR_EQUAL(
762 &inp->in6p_laddr, laddr))
765 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
768 if (wildcard < matchwild) {
770 matchwild = wildcard;
781 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
784 struct ip6_moptions *im6o;
787 INP_INFO_WLOCK(pcbinfo);
788 LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
790 im6o = in6p->in6p_moptions;
791 if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
793 * Unselect the outgoing ifp for multicast if it
796 if (im6o->im6o_multicast_ifp == ifp)
797 im6o->im6o_multicast_ifp = NULL;
799 * Drop multicast group membership if we joined
800 * through the interface being detached.
803 for (i = 0; i < im6o->im6o_num_memberships; i++) {
804 if (im6o->im6o_membership[i]->in6m_ifp ==
806 in6_mc_leave(im6o->im6o_membership[i],
809 } else if (gap != 0) {
810 im6o->im6o_membership[i - gap] =
811 im6o->im6o_membership[i];
814 im6o->im6o_num_memberships -= gap;
818 INP_INFO_WUNLOCK(pcbinfo);
822 * Check for alternatives when higher level complains
823 * about service problems. For now, invalidate cached
824 * routing information. If the route was created dynamically
825 * (by a redirect), time to try a default gateway again.
828 in6_losing(struct inpcb *in6p)
831 if (in6p->inp_route6.ro_rt) {
832 RTFREE(in6p->inp_route6.ro_rt);
833 in6p->inp_route6.ro_rt = (struct rtentry *)NULL;
835 if (in6p->inp_route.ro_lle)
836 LLE_FREE(in6p->inp_route.ro_lle); /* zeros ro_lle */
841 * After a routing change, flush old routing
842 * and allocate a (hopefully) better one.
845 in6_rtchange(struct inpcb *inp, int errno)
848 if (inp->inp_route6.ro_rt) {
849 RTFREE(inp->inp_route6.ro_rt);
850 inp->inp_route6.ro_rt = (struct rtentry *)NULL;
852 if (inp->inp_route.ro_lle)
853 LLE_FREE(inp->inp_route.ro_lle); /* zeros ro_lle */
859 * Lookup PCB in hash list, using pcbgroup tables.
861 static struct inpcb *
862 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
863 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
864 u_int lport_arg, int lookupflags, struct ifnet *ifp)
866 struct inpcbhead *head;
867 struct inpcb *inp, *tmpinp;
868 u_short fport = fport_arg, lport = lport_arg;
872 * First look for an exact match.
875 INP_GROUP_LOCK(pcbgroup);
876 head = &pcbgroup->ipg_hashbase[INP_PCBHASH(
877 INP6_PCBHASHKEY(faddr), lport, fport, pcbgroup->ipg_hashmask)];
878 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
879 /* XXX inp locking */
880 if ((inp->inp_vflag & INP_IPV6) == 0)
882 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
883 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
884 inp->inp_fport == fport &&
885 inp->inp_lport == lport) {
887 * XXX We should be able to directly return
888 * the inp here, without any checks.
889 * Well unless both bound with SO_REUSEPORT?
891 if (prison_flag(inp->inp_cred, PR_IP6))
897 if (tmpinp != NULL) {
903 * Then look for a wildcard match in the pcbgroup.
905 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
906 struct inpcb *local_wild = NULL, *local_exact = NULL;
907 struct inpcb *jail_wild = NULL;
911 * Order of socket selection - we always prefer jails.
912 * 1. jailed, non-wild.
914 * 3. non-jailed, non-wild.
915 * 4. non-jailed, wild.
917 head = &pcbgroup->ipg_hashbase[
918 INP_PCBHASH(INADDR_ANY, lport, 0, pcbgroup->ipg_hashmask)];
919 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
920 /* XXX inp locking */
921 if ((inp->inp_vflag & INP_IPV6) == 0)
924 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
925 inp->inp_lport != lport) {
929 injail = prison_flag(inp->inp_cred, PR_IP6);
931 if (prison_check_ip6(inp->inp_cred,
935 if (local_exact != NULL)
939 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
944 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
964 * Then look for a wildcard match, if requested.
966 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
967 struct inpcb *local_wild = NULL, *local_exact = NULL;
968 struct inpcb *jail_wild = NULL;
972 * Order of socket selection - we always prefer jails.
973 * 1. jailed, non-wild.
975 * 3. non-jailed, non-wild.
976 * 4. non-jailed, wild.
978 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(
979 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
980 pcbinfo->ipi_wildmask)];
981 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
982 /* XXX inp locking */
983 if ((inp->inp_vflag & INP_IPV6) == 0)
986 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
987 inp->inp_lport != lport) {
991 injail = prison_flag(inp->inp_cred, PR_IP6);
993 if (prison_check_ip6(inp->inp_cred,
997 if (local_exact != NULL)
1001 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1006 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1012 } /* LIST_FOREACH */
1023 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1024 INP_GROUP_UNLOCK(pcbgroup);
1028 if (lookupflags & INPLOOKUP_WLOCKPCB)
1029 locked = INP_TRY_WLOCK(inp);
1030 else if (lookupflags & INPLOOKUP_RLOCKPCB)
1031 locked = INP_TRY_RLOCK(inp);
1033 panic("%s: locking buf", __func__);
1036 INP_GROUP_UNLOCK(pcbgroup);
1038 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1040 if (in_pcbrele_wlocked(inp))
1044 if (in_pcbrele_rlocked(inp))
1049 if (lookupflags & INPLOOKUP_WLOCKPCB)
1050 INP_WLOCK_ASSERT(inp);
1052 INP_RLOCK_ASSERT(inp);
1056 #endif /* PCBGROUP */
1059 * Lookup PCB in hash list.
1061 static struct inpcb *
1062 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1063 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1064 int lookupflags, struct ifnet *ifp)
1066 struct inpcbhead *head;
1067 struct inpcb *inp, *tmpinp;
1068 u_short fport = fport_arg, lport = lport_arg;
1070 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1071 ("%s: invalid lookup flags %d", __func__, lookupflags));
1073 INP_HASH_LOCK_ASSERT(pcbinfo);
1076 * First look for an exact match.
1079 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1080 INP6_PCBHASHKEY(faddr), lport, fport, pcbinfo->ipi_hashmask)];
1081 LIST_FOREACH(inp, head, inp_hash) {
1082 /* XXX inp locking */
1083 if ((inp->inp_vflag & INP_IPV6) == 0)
1085 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1086 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1087 inp->inp_fport == fport &&
1088 inp->inp_lport == lport) {
1090 * XXX We should be able to directly return
1091 * the inp here, without any checks.
1092 * Well unless both bound with SO_REUSEPORT?
1094 if (prison_flag(inp->inp_cred, PR_IP6))
1104 * Then look for a wildcard match, if requested.
1106 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1107 struct inpcb *local_wild = NULL, *local_exact = NULL;
1108 struct inpcb *jail_wild = NULL;
1112 * Order of socket selection - we always prefer jails.
1113 * 1. jailed, non-wild.
1115 * 3. non-jailed, non-wild.
1116 * 4. non-jailed, wild.
1118 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(
1119 INP6_PCBHASHKEY(&in6addr_any), lport, 0,
1120 pcbinfo->ipi_hashmask)];
1121 LIST_FOREACH(inp, head, inp_hash) {
1122 /* XXX inp locking */
1123 if ((inp->inp_vflag & INP_IPV6) == 0)
1126 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1127 inp->inp_lport != lport) {
1131 injail = prison_flag(inp->inp_cred, PR_IP6);
1133 if (prison_check_ip6(inp->inp_cred,
1137 if (local_exact != NULL)
1141 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1146 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1152 } /* LIST_FOREACH */
1154 if (jail_wild != NULL)
1156 if (local_exact != NULL)
1157 return (local_exact);
1158 if (local_wild != NULL)
1159 return (local_wild);
1160 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1169 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1170 * hash list lock, and will return the inpcb locked (i.e., requires
1171 * INPLOOKUP_LOCKPCB).
1173 static struct inpcb *
1174 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1175 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1181 INP_HASH_RLOCK(pcbinfo);
1182 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1183 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1185 if (lookupflags & INPLOOKUP_WLOCKPCB)
1186 locked = INP_TRY_WLOCK(inp);
1187 else if (lookupflags & INPLOOKUP_RLOCKPCB)
1188 locked = INP_TRY_RLOCK(inp);
1190 panic("%s: locking bug", __func__);
1193 INP_HASH_RUNLOCK(pcbinfo);
1195 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1197 if (in_pcbrele_wlocked(inp))
1201 if (in_pcbrele_rlocked(inp))
1206 if (lookupflags & INPLOOKUP_WLOCKPCB)
1207 INP_WLOCK_ASSERT(inp);
1209 INP_RLOCK_ASSERT(inp);
1212 INP_HASH_RUNLOCK(pcbinfo);
1217 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1218 * from which a pre-calculated hash value may be extracted.
1220 * Possibly more of this logic should be in in6_pcbgroup.c.
1223 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1224 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1226 #if defined(PCBGROUP) && !defined(RSS)
1227 struct inpcbgroup *pcbgroup;
1230 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1231 ("%s: invalid lookup flags %d", __func__, lookupflags));
1232 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1233 ("%s: LOCKPCB not set", __func__));
1236 * When not using RSS, use connection groups in preference to the
1237 * reservation table when looking up 4-tuples. When using RSS, just
1238 * use the reservation table, due to the cost of the Toeplitz hash
1241 * XXXRW: This policy belongs in the pcbgroup code, as in principle
1242 * we could be doing RSS with a non-Toeplitz hash that is affordable
1245 #if defined(PCBGROUP) && !defined(RSS)
1246 if (in_pcbgroup_enabled(pcbinfo)) {
1247 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1249 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1250 laddr, lport, lookupflags, ifp));
1253 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1258 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1259 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1260 struct ifnet *ifp, struct mbuf *m)
1263 struct inpcbgroup *pcbgroup;
1266 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1267 ("%s: invalid lookup flags %d", __func__, lookupflags));
1268 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1269 ("%s: LOCKPCB not set", __func__));
1273 * If we can use a hardware-generated hash to look up the connection
1274 * group, use that connection group to find the inpcb. Otherwise
1275 * fall back on a software hash -- or the reservation table if we're
1278 * XXXRW: As above, that policy belongs in the pcbgroup code.
1280 if (in_pcbgroup_enabled(pcbinfo) &&
1281 M_HASHTYPE_TEST(m, M_HASHTYPE_NONE) == 0) {
1282 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1283 m->m_pkthdr.flowid);
1284 if (pcbgroup != NULL)
1285 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1286 fport, laddr, lport, lookupflags, ifp));
1288 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1290 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1291 laddr, lport, lookupflags, ifp));
1295 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1300 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1304 ip = mtod(m, struct ip6_hdr *);
1305 bzero(sin6, sizeof(*sin6));
1306 sin6->sin6_len = sizeof(*sin6);
1307 sin6->sin6_family = AF_INET6;
1308 sin6->sin6_addr = ip->ip6_src;
1310 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */