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"
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
79 #include <sys/domain.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/sockio.h>
84 #include <sys/errno.h>
93 #include <net/if_types.h>
94 #include <net/route.h>
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/tcp_var.h>
100 #include <netinet/ip6.h>
101 #include <netinet/ip_var.h>
103 #include <netinet6/ip6_var.h>
104 #include <netinet6/nd6.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet6/in6_pcb.h>
107 #include <netinet6/scope6_var.h>
109 struct in6_addr zeroin6_addr;
112 in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
115 struct socket *so = inp->inp_socket;
116 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
117 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
119 int error, lookupflags = 0;
120 int reuseport = (so->so_options & SO_REUSEPORT);
122 INP_WLOCK_ASSERT(inp);
123 INP_HASH_WLOCK_ASSERT(pcbinfo);
125 if (TAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */
126 return (EADDRNOTAVAIL);
127 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
129 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
130 lookupflags = INPLOOKUP_WILDCARD;
132 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
133 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
136 sin6 = (struct sockaddr_in6 *)nam;
137 if (nam->sa_len != sizeof(*sin6))
142 if (nam->sa_family != AF_INET6)
143 return (EAFNOSUPPORT);
145 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
148 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
149 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
152 lport = sin6->sin6_port;
153 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
155 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
156 * allow compepte duplication of binding if
157 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
158 * and a multicast address is bound on both
159 * new and duplicated sockets.
161 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
162 reuseport = SO_REUSEADDR|SO_REUSEPORT;
163 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
166 sin6->sin6_port = 0; /* yech... */
167 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
169 (inp->inp_flags & INP_BINDANY) == 0) {
170 return (EADDRNOTAVAIL);
174 * XXX: bind to an anycast address might accidentally
175 * cause sending a packet with anycast source address.
176 * We should allow to bind to a deprecated address, since
177 * the application dares to use it.
180 ((struct in6_ifaddr *)ifa)->ia6_flags &
181 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
183 return (EADDRNOTAVAIL);
193 if (ntohs(lport) <= V_ipport_reservedhigh &&
194 ntohs(lport) >= V_ipport_reservedlow &&
195 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
198 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
199 priv_check_cred(inp->inp_cred,
200 PRIV_NETINET_REUSEPORT, 0) != 0) {
201 t = in6_pcblookup_local(pcbinfo,
202 &sin6->sin6_addr, lport,
203 INPLOOKUP_WILDCARD, cred);
205 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
206 (so->so_type != SOCK_STREAM ||
207 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
208 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
209 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
210 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
211 (inp->inp_cred->cr_uid !=
212 t->inp_cred->cr_uid))
215 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
216 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
217 struct sockaddr_in sin;
219 in6_sin6_2_sin(&sin, sin6);
220 t = in_pcblookup_local(pcbinfo,
222 INPLOOKUP_WILDCARD, cred);
225 INP_TIMEWAIT) == 0) &&
226 (so->so_type != SOCK_STREAM ||
227 ntohl(t->inp_faddr.s_addr) ==
229 (inp->inp_cred->cr_uid !=
230 t->inp_cred->cr_uid))
235 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
236 lport, lookupflags, cred);
237 if (t && (t->inp_flags & INP_TIMEWAIT)) {
239 * XXXRW: If an incpb has had its timewait
240 * state recycled, we treat the address as
241 * being in use (for now). This is better
242 * than a panic, but not desirable.
246 (reuseport & tw->tw_so_options) == 0)
248 } else if (t && (reuseport & inp_so_options(t)) == 0) {
252 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
253 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
254 struct sockaddr_in sin;
256 in6_sin6_2_sin(&sin, sin6);
257 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
258 lport, lookupflags, cred);
259 if (t && t->inp_flags & INP_TIMEWAIT) {
263 if ((reuseport & tw->tw_so_options) == 0
264 && (ntohl(t->inp_laddr.s_addr) !=
265 INADDR_ANY || ((inp->inp_vflag &
267 (t->inp_vflag & INP_IPV6PROTO))))
270 (reuseport & inp_so_options(t)) == 0 &&
271 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
272 (t->inp_vflag & INP_IPV6PROTO) != 0))
277 inp->in6p_laddr = sin6->sin6_addr;
280 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
281 /* Undo an address bind that may have occurred. */
282 inp->in6p_laddr = in6addr_any;
286 inp->inp_lport = lport;
287 if (in_pcbinshash(inp) != 0) {
288 inp->in6p_laddr = in6addr_any;
297 * Transform old in6_pcbconnect() into an inner subroutine for new
298 * in6_pcbconnect(): Do some validity-checking on the remote
299 * address (in mbuf 'nam') and then determine local host address
300 * (i.e., which interface) to use to access that remote host.
302 * This preserves definition of in6_pcbconnect(), while supporting a
303 * slightly different version for T/TCP. (This is more than
304 * a bit of a kludge, but cleaning up the internal interfaces would
305 * have forced minor changes in every protocol).
308 in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
309 struct in6_addr *plocal_addr6)
311 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
313 struct ifnet *ifp = NULL;
314 int scope_ambiguous = 0;
315 struct in6_addr in6a;
317 INP_WLOCK_ASSERT(inp);
318 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
320 if (nam->sa_len != sizeof (*sin6))
322 if (sin6->sin6_family != AF_INET6)
323 return (EAFNOSUPPORT);
324 if (sin6->sin6_port == 0)
325 return (EADDRNOTAVAIL);
327 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
329 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
332 if (!TAILQ_EMPTY(&V_in6_ifaddrhead)) {
334 * If the destination address is UNSPECIFIED addr,
335 * use the loopback addr, e.g ::1.
337 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
338 sin6->sin6_addr = in6addr_loopback;
340 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
343 error = in6_selectsrc(sin6, inp->in6p_outputopts,
344 inp, NULL, inp->inp_cred, &ifp, &in6a);
348 if (ifp && scope_ambiguous &&
349 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
354 * Do not update this earlier, in case we return with an error.
356 * XXX: this in6_selectsrc result might replace the bound local
357 * address with the address specified by setsockopt(IPV6_PKTINFO).
358 * Is it the intended behavior?
360 *plocal_addr6 = in6a;
363 * Don't do pcblookup call here; return interface in
365 * and exit to caller, that will do the lookup.
373 * Connect from a socket to a specified address.
374 * Both address and port must be specified in argument sin.
375 * If don't have a local address for this socket yet,
379 in6_pcbconnect_mbuf(register struct inpcb *inp, struct sockaddr *nam,
380 struct ucred *cred, struct mbuf *m)
382 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
383 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
384 struct in6_addr addr6;
387 INP_WLOCK_ASSERT(inp);
388 INP_HASH_WLOCK_ASSERT(pcbinfo);
391 * Call inner routine, to assign local interface address.
392 * in6_pcbladdr() may automatically fill in sin6_scope_id.
394 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
397 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
399 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
400 ? &addr6 : &inp->in6p_laddr,
401 inp->inp_lport, 0, NULL) != NULL) {
404 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
405 if (inp->inp_lport == 0) {
406 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
410 inp->in6p_laddr = addr6;
412 inp->in6p_faddr = sin6->sin6_addr;
413 inp->inp_fport = sin6->sin6_port;
414 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
415 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
416 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
418 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
420 in_pcbrehash_mbuf(inp, m);
426 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
429 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL));
433 in6_pcbdisconnect(struct inpcb *inp)
436 INP_WLOCK_ASSERT(inp);
437 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
439 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
441 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
442 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
447 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
449 struct sockaddr_in6 *sin6;
451 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
452 bzero(sin6, sizeof *sin6);
453 sin6->sin6_family = AF_INET6;
454 sin6->sin6_len = sizeof(*sin6);
455 sin6->sin6_port = port;
456 sin6->sin6_addr = *addr_p;
457 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
459 return (struct sockaddr *)sin6;
463 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
465 struct sockaddr_in sin;
466 struct sockaddr_in6 *sin6_p;
468 bzero(&sin, sizeof sin);
469 sin.sin_family = AF_INET;
470 sin.sin_len = sizeof(sin);
472 sin.sin_addr = *addr_p;
474 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
476 in6_sin_2_v4mapsin6(&sin, sin6_p);
478 return (struct sockaddr *)sin6_p;
482 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
484 register struct inpcb *inp;
485 struct in6_addr addr;
489 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
492 port = inp->inp_lport;
493 addr = inp->in6p_laddr;
496 *nam = in6_sockaddr(port, &addr);
501 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
504 struct in6_addr addr;
508 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
511 port = inp->inp_fport;
512 addr = inp->in6p_faddr;
515 *nam = in6_sockaddr(port, &addr);
520 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
526 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
529 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
530 error = in_getsockaddr(so, nam);
532 in6_sin_2_v4mapsin6_in_sock(nam);
536 /* scope issues will be handled in in6_getsockaddr(). */
537 error = in6_getsockaddr(so, nam);
544 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
550 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
553 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
554 error = in_getpeeraddr(so, nam);
556 in6_sin_2_v4mapsin6_in_sock(nam);
559 /* scope issues will be handled in in6_getpeeraddr(). */
560 error = in6_getpeeraddr(so, nam);
566 * Pass some notification to all connections of a protocol
567 * associated with address dst. The local address and/or port numbers
568 * may be specified to limit the search. The "usual action" will be
569 * taken, depending on the ctlinput cmd. The caller must filter any
570 * cmds that are uninteresting (e.g., no error in the map).
571 * Call the protocol specific routine (if any) to report
572 * any errors for each matching socket.
575 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
576 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
577 int cmd, void *cmdarg,
578 struct inpcb *(*notify)(struct inpcb *, int))
580 struct inpcb *inp, *inp_temp;
581 struct sockaddr_in6 sa6_src, *sa6_dst;
582 u_short fport = fport_arg, lport = lport_arg;
586 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
589 sa6_dst = (struct sockaddr_in6 *)dst;
590 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
594 * note that src can be NULL when we get notify by local fragmentation.
596 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
597 flowinfo = sa6_src.sin6_flowinfo;
600 * Redirects go to all references to the destination,
601 * and use in6_rtchange to invalidate the route cache.
602 * Dead host indications: also use in6_rtchange to invalidate
603 * the cache, and deliver the error to all the sockets.
604 * Otherwise, if we have knowledge of the local port and address,
605 * deliver only to that socket.
607 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
610 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
612 if (cmd != PRC_HOSTDEAD)
613 notify = in6_rtchange;
615 errno = inet6ctlerrmap[cmd];
616 INP_INFO_WLOCK(pcbinfo);
617 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
619 if ((inp->inp_vflag & INP_IPV6) == 0) {
625 * If the error designates a new path MTU for a destination
626 * and the application (associated with this socket) wanted to
627 * know the value, notify. Note that we notify for all
628 * disconnected sockets if the corresponding application
629 * wanted. This is because some UDP applications keep sending
630 * sockets disconnected.
631 * XXX: should we avoid to notify the value to TCP sockets?
633 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
634 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
635 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
636 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
637 (u_int32_t *)cmdarg);
641 * Detect if we should notify the error. If no source and
642 * destination ports are specifed, but non-zero flowinfo and
643 * local address match, notify the error. This is the case
644 * when the error is delivered with an encrypted buffer
645 * by ESP. Otherwise, just compare addresses and ports
648 if (lport == 0 && fport == 0 && flowinfo &&
649 inp->inp_socket != NULL &&
650 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
651 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
653 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
654 &sa6_dst->sin6_addr) ||
655 inp->inp_socket == 0 ||
656 (lport && inp->inp_lport != lport) ||
657 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
658 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
659 &sa6_src.sin6_addr)) ||
660 (fport && inp->inp_fport != fport)) {
667 if ((*notify)(inp, errno))
672 INP_INFO_WUNLOCK(pcbinfo);
676 * Lookup a PCB based on the local address and port. Caller must hold the
677 * hash lock. No inpcb locks or references are acquired.
680 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
681 u_short lport, int lookupflags, struct ucred *cred)
683 register struct inpcb *inp;
684 int matchwild = 3, wildcard;
686 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
687 ("%s: invalid lookup flags %d", __func__, lookupflags));
689 INP_HASH_WLOCK_ASSERT(pcbinfo);
691 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
692 struct inpcbhead *head;
694 * Look for an unconnected (wildcard foreign addr) PCB that
695 * matches the local address and port we're looking for.
697 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
698 0, pcbinfo->ipi_hashmask)];
699 LIST_FOREACH(inp, head, inp_hash) {
700 /* XXX inp locking */
701 if ((inp->inp_vflag & INP_IPV6) == 0)
703 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
704 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
705 inp->inp_lport == lport) {
708 prison_equal_ip6(cred->cr_prison,
709 inp->inp_cred->cr_prison))
718 struct inpcbporthead *porthash;
719 struct inpcbport *phd;
720 struct inpcb *match = NULL;
722 * Best fit PCB lookup.
724 * First see if this local port is in use by looking on the
727 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
728 pcbinfo->ipi_porthashmask)];
729 LIST_FOREACH(phd, porthash, phd_hash) {
730 if (phd->phd_port == lport)
735 * Port is in use by one or more PCBs. Look for best
738 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
741 !prison_equal_ip6(cred->cr_prison,
742 inp->inp_cred->cr_prison))
744 /* XXX inp locking */
745 if ((inp->inp_vflag & INP_IPV6) == 0)
747 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
749 if (!IN6_IS_ADDR_UNSPECIFIED(
751 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
753 else if (!IN6_ARE_ADDR_EQUAL(
754 &inp->in6p_laddr, laddr))
757 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
760 if (wildcard < matchwild) {
762 matchwild = wildcard;
773 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
776 struct ip6_moptions *im6o;
779 INP_INFO_RLOCK(pcbinfo);
780 LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
782 im6o = in6p->in6p_moptions;
783 if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
785 * Unselect the outgoing ifp for multicast if it
788 if (im6o->im6o_multicast_ifp == ifp)
789 im6o->im6o_multicast_ifp = NULL;
791 * Drop multicast group membership if we joined
792 * through the interface being detached.
795 for (i = 0; i < im6o->im6o_num_memberships; i++) {
796 if (im6o->im6o_membership[i]->in6m_ifp ==
798 in6_mc_leave(im6o->im6o_membership[i],
801 } else if (gap != 0) {
802 im6o->im6o_membership[i - gap] =
803 im6o->im6o_membership[i];
806 im6o->im6o_num_memberships -= gap;
810 INP_INFO_RUNLOCK(pcbinfo);
814 * Check for alternatives when higher level complains
815 * about service problems. For now, invalidate cached
816 * routing information. If the route was created dynamically
817 * (by a redirect), time to try a default gateway again.
820 in6_losing(struct inpcb *in6p)
824 * We don't store route pointers in the routing table anymore
830 * After a routing change, flush old routing
831 * and allocate a (hopefully) better one.
834 in6_rtchange(struct inpcb *inp, int errno)
837 * We don't store route pointers in the routing table anymore
844 * Lookup PCB in hash list, using pcbgroup tables.
846 static struct inpcb *
847 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
848 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
849 u_int lport_arg, int lookupflags, struct ifnet *ifp)
851 struct inpcbhead *head;
852 struct inpcb *inp, *tmpinp;
853 u_short fport = fport_arg, lport = lport_arg;
856 if (faithprefix_p != NULL)
857 faith = (*faithprefix_p)(laddr);
862 * First look for an exact match.
865 INP_GROUP_LOCK(pcbgroup);
866 head = &pcbgroup->ipg_hashbase[
867 INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
868 pcbgroup->ipg_hashmask)];
869 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
870 /* XXX inp locking */
871 if ((inp->inp_vflag & INP_IPV6) == 0)
873 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
874 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
875 inp->inp_fport == fport &&
876 inp->inp_lport == lport) {
878 * XXX We should be able to directly return
879 * the inp here, without any checks.
880 * Well unless both bound with SO_REUSEPORT?
882 if (prison_flag(inp->inp_cred, PR_IP6))
888 if (tmpinp != NULL) {
894 * Then look for a wildcard match, if requested.
896 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
897 struct inpcb *local_wild = NULL, *local_exact = NULL;
898 struct inpcb *jail_wild = NULL;
902 * Order of socket selection - we always prefer jails.
903 * 1. jailed, non-wild.
905 * 3. non-jailed, non-wild.
906 * 4. non-jailed, wild.
908 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
909 0, pcbinfo->ipi_wildmask)];
910 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
911 /* XXX inp locking */
912 if ((inp->inp_vflag & INP_IPV6) == 0)
915 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
916 inp->inp_lport != lport) {
920 /* XXX inp locking */
921 if (faith && (inp->inp_flags & INP_FAITH) == 0)
924 injail = prison_flag(inp->inp_cred, PR_IP6);
926 if (prison_check_ip6(inp->inp_cred,
930 if (local_exact != NULL)
934 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
939 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
956 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
957 INP_GROUP_UNLOCK(pcbgroup);
962 INP_GROUP_UNLOCK(pcbgroup);
963 if (lookupflags & INPLOOKUP_WLOCKPCB) {
965 if (in_pcbrele_wlocked(inp))
967 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
969 if (in_pcbrele_rlocked(inp))
972 panic("%s: locking buf", __func__);
975 #endif /* PCBGROUP */
978 * Lookup PCB in hash list.
981 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
982 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
983 int lookupflags, struct ifnet *ifp)
985 struct inpcbhead *head;
986 struct inpcb *inp, *tmpinp;
987 u_short fport = fport_arg, lport = lport_arg;
990 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
991 ("%s: invalid lookup flags %d", __func__, lookupflags));
993 INP_HASH_LOCK_ASSERT(pcbinfo);
995 if (faithprefix_p != NULL)
996 faith = (*faithprefix_p)(laddr);
1001 * First look for an exact match.
1004 head = &pcbinfo->ipi_hashbase[
1005 INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
1006 pcbinfo->ipi_hashmask)];
1007 LIST_FOREACH(inp, head, inp_hash) {
1008 /* XXX inp locking */
1009 if ((inp->inp_vflag & INP_IPV6) == 0)
1011 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1012 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1013 inp->inp_fport == fport &&
1014 inp->inp_lport == lport) {
1016 * XXX We should be able to directly return
1017 * the inp here, without any checks.
1018 * Well unless both bound with SO_REUSEPORT?
1020 if (prison_flag(inp->inp_cred, PR_IP6))
1030 * Then look for a wildcard match, if requested.
1032 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1033 struct inpcb *local_wild = NULL, *local_exact = NULL;
1034 struct inpcb *jail_wild = NULL;
1038 * Order of socket selection - we always prefer jails.
1039 * 1. jailed, non-wild.
1041 * 3. non-jailed, non-wild.
1042 * 4. non-jailed, wild.
1044 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1045 0, pcbinfo->ipi_hashmask)];
1046 LIST_FOREACH(inp, head, inp_hash) {
1047 /* XXX inp locking */
1048 if ((inp->inp_vflag & INP_IPV6) == 0)
1051 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1052 inp->inp_lport != lport) {
1056 /* XXX inp locking */
1057 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1060 injail = prison_flag(inp->inp_cred, PR_IP6);
1062 if (prison_check_ip6(inp->inp_cred,
1066 if (local_exact != NULL)
1070 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1075 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1081 } /* LIST_FOREACH */
1083 if (jail_wild != NULL)
1085 if (local_exact != NULL)
1086 return (local_exact);
1087 if (local_wild != NULL)
1088 return (local_wild);
1089 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1098 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1099 * hash list lock, and will return the inpcb locked (i.e., requires
1100 * INPLOOKUP_LOCKPCB).
1102 static struct inpcb *
1103 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1104 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1109 INP_HASH_RLOCK(pcbinfo);
1110 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1111 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1114 INP_HASH_RUNLOCK(pcbinfo);
1115 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1117 if (in_pcbrele_wlocked(inp))
1119 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1121 if (in_pcbrele_rlocked(inp))
1124 panic("%s: locking bug", __func__);
1126 INP_HASH_RUNLOCK(pcbinfo);
1131 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1132 * from which a pre-calculated hash value may be extracted.
1134 * Possibly more of this logic should be in in6_pcbgroup.c.
1137 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1138 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1140 #if defined(PCBGROUP)
1141 struct inpcbgroup *pcbgroup;
1144 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1145 ("%s: invalid lookup flags %d", __func__, lookupflags));
1146 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1147 ("%s: LOCKPCB not set", __func__));
1149 #if defined(PCBGROUP)
1150 if (in_pcbgroup_enabled(pcbinfo)) {
1151 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1153 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1154 laddr, lport, lookupflags, ifp));
1157 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1162 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1163 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1164 struct ifnet *ifp, struct mbuf *m)
1167 struct inpcbgroup *pcbgroup;
1170 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1171 ("%s: invalid lookup flags %d", __func__, lookupflags));
1172 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1173 ("%s: LOCKPCB not set", __func__));
1176 if (in_pcbgroup_enabled(pcbinfo)) {
1177 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1178 m->m_pkthdr.flowid);
1179 if (pcbgroup != NULL)
1180 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1181 fport, laddr, lport, lookupflags, ifp));
1182 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1184 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1185 laddr, lport, lookupflags, ifp));
1188 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1193 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1197 ip = mtod(m, struct ip6_hdr *);
1198 bzero(sin6, sizeof(*sin6));
1199 sin6->sin6_len = sizeof(*sin6);
1200 sin6->sin6_family = AF_INET6;
1201 sin6->sin6_addr = ip->ip6_src;
1203 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */