2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD$");
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/malloc.h>
74 #include <sys/domain.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/sockio.h>
79 #include <sys/errno.h>
88 #include <net/if_types.h>
89 #include <net/route.h>
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/tcp_var.h>
95 #include <netinet/ip6.h>
96 #include <netinet/ip_var.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet6/nd6.h>
100 #include <netinet/in_pcb.h>
101 #include <netinet6/in6_pcb.h>
102 #include <netinet6/scope6_var.h>
104 #include <security/mac/mac_framework.h>
106 struct in6_addr zeroin6_addr;
109 in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
112 struct socket *so = inp->inp_socket;
113 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
114 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
116 int error, wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
118 INP_INFO_WLOCK_ASSERT(pcbinfo);
119 INP_WLOCK_ASSERT(inp);
121 if (TAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */
122 return (EADDRNOTAVAIL);
123 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
125 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
126 wild = INPLOOKUP_WILDCARD;
128 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
129 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
132 sin6 = (struct sockaddr_in6 *)nam;
133 if (nam->sa_len != sizeof(*sin6))
138 if (nam->sa_family != AF_INET6)
139 return (EAFNOSUPPORT);
141 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
144 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
145 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
148 lport = sin6->sin6_port;
149 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
151 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
152 * allow compepte duplication of binding if
153 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
154 * and a multicast address is bound on both
155 * new and duplicated sockets.
157 if (so->so_options & SO_REUSEADDR)
158 reuseport = SO_REUSEADDR|SO_REUSEPORT;
159 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
162 sin6->sin6_port = 0; /* yech... */
163 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
165 (inp->inp_flags & INP_BINDANY) == 0) {
166 return (EADDRNOTAVAIL);
170 * XXX: bind to an anycast address might accidentally
171 * cause sending a packet with anycast source address.
172 * We should allow to bind to a deprecated address, since
173 * the application dares to use it.
176 ((struct in6_ifaddr *)ifa)->ia6_flags &
177 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
179 return (EADDRNOTAVAIL);
188 if (ntohs(lport) <= V_ipport_reservedhigh &&
189 ntohs(lport) >= V_ipport_reservedlow &&
190 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
193 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
194 priv_check_cred(inp->inp_cred,
195 PRIV_NETINET_REUSEPORT, 0) != 0) {
196 t = in6_pcblookup_local(pcbinfo,
197 &sin6->sin6_addr, lport,
198 INPLOOKUP_WILDCARD, cred);
200 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
201 (so->so_type != SOCK_STREAM ||
202 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
203 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
204 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
205 (t->inp_socket->so_options & SO_REUSEPORT)
206 == 0) && (inp->inp_cred->cr_uid !=
207 t->inp_cred->cr_uid))
209 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
210 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
211 struct sockaddr_in sin;
213 in6_sin6_2_sin(&sin, sin6);
214 t = in_pcblookup_local(pcbinfo,
216 INPLOOKUP_WILDCARD, cred);
219 INP_TIMEWAIT) == 0) &&
220 (so->so_type != SOCK_STREAM ||
221 ntohl(t->inp_faddr.s_addr) ==
223 (inp->inp_cred->cr_uid !=
224 t->inp_cred->cr_uid))
228 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
230 if (t && (reuseport & ((t->inp_flags & INP_TIMEWAIT) ?
231 intotw(t)->tw_so_options :
232 t->inp_socket->so_options)) == 0)
234 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
235 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
236 struct sockaddr_in sin;
238 in6_sin6_2_sin(&sin, sin6);
239 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
241 if (t && t->inp_flags & INP_TIMEWAIT) {
243 intotw(t)->tw_so_options) == 0 &&
244 (ntohl(t->inp_laddr.s_addr) !=
245 INADDR_ANY || ((inp->inp_vflag &
247 (t->inp_vflag & INP_IPV6PROTO))))
251 (reuseport & t->inp_socket->so_options)
252 == 0 && (ntohl(t->inp_laddr.s_addr) !=
253 INADDR_ANY || INP_SOCKAF(so) ==
254 INP_SOCKAF(t->inp_socket)))
258 inp->in6p_laddr = sin6->sin6_addr;
261 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
264 inp->inp_lport = lport;
265 if (in_pcbinshash(inp) != 0) {
266 inp->in6p_laddr = in6addr_any;
275 * Transform old in6_pcbconnect() into an inner subroutine for new
276 * in6_pcbconnect(): Do some validity-checking on the remote
277 * address (in mbuf 'nam') and then determine local host address
278 * (i.e., which interface) to use to access that remote host.
280 * This preserves definition of in6_pcbconnect(), while supporting a
281 * slightly different version for T/TCP. (This is more than
282 * a bit of a kludge, but cleaning up the internal interfaces would
283 * have forced minor changes in every protocol).
286 in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
287 struct in6_addr *plocal_addr6)
289 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
291 struct ifnet *ifp = NULL;
292 int scope_ambiguous = 0;
293 struct in6_addr in6a;
295 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
296 INP_WLOCK_ASSERT(inp);
298 if (nam->sa_len != sizeof (*sin6))
300 if (sin6->sin6_family != AF_INET6)
301 return (EAFNOSUPPORT);
302 if (sin6->sin6_port == 0)
303 return (EADDRNOTAVAIL);
305 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
307 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
310 if (!TAILQ_EMPTY(&V_in6_ifaddrhead)) {
312 * If the destination address is UNSPECIFIED addr,
313 * use the loopback addr, e.g ::1.
315 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
316 sin6->sin6_addr = in6addr_loopback;
318 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
321 error = in6_selectsrc(sin6, inp->in6p_outputopts,
322 inp, NULL, inp->inp_cred, &ifp, &in6a);
326 if (ifp && scope_ambiguous &&
327 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
332 * Do not update this earlier, in case we return with an error.
334 * XXX: this in6_selectsrc result might replace the bound local
335 * aaddress with the address specified by setsockopt(IPV6_PKTINFO).
336 * Is it the intended behavior?
338 *plocal_addr6 = in6a;
341 * Don't do pcblookup call here; return interface in
343 * and exit to caller, that will do the lookup.
351 * Connect from a socket to a specified address.
352 * Both address and port must be specified in argument sin.
353 * If don't have a local address for this socket yet,
357 in6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
360 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
361 struct in6_addr addr6;
364 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
365 INP_WLOCK_ASSERT(inp);
368 * Call inner routine, to assign local interface address.
369 * in6_pcbladdr() may automatically fill in sin6_scope_id.
371 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
374 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
376 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
377 ? &addr6 : &inp->in6p_laddr,
378 inp->inp_lport, 0, NULL) != NULL) {
381 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
382 if (inp->inp_lport == 0) {
383 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
387 inp->in6p_laddr = addr6;
389 inp->in6p_faddr = sin6->sin6_addr;
390 inp->inp_fport = sin6->sin6_port;
391 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
392 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
393 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
395 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
403 in6_pcbdisconnect(struct inpcb *inp)
406 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
407 INP_WLOCK_ASSERT(inp);
409 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
411 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
412 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
417 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
419 struct sockaddr_in6 *sin6;
421 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
422 bzero(sin6, sizeof *sin6);
423 sin6->sin6_family = AF_INET6;
424 sin6->sin6_len = sizeof(*sin6);
425 sin6->sin6_port = port;
426 sin6->sin6_addr = *addr_p;
427 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
429 return (struct sockaddr *)sin6;
433 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
435 struct sockaddr_in sin;
436 struct sockaddr_in6 *sin6_p;
438 bzero(&sin, sizeof sin);
439 sin.sin_family = AF_INET;
440 sin.sin_len = sizeof(sin);
442 sin.sin_addr = *addr_p;
444 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
446 in6_sin_2_v4mapsin6(&sin, sin6_p);
448 return (struct sockaddr *)sin6_p;
452 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
454 register struct inpcb *inp;
455 struct in6_addr addr;
459 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
462 port = inp->inp_lport;
463 addr = inp->in6p_laddr;
466 *nam = in6_sockaddr(port, &addr);
471 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
474 struct in6_addr addr;
478 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
481 port = inp->inp_fport;
482 addr = inp->in6p_faddr;
485 *nam = in6_sockaddr(port, &addr);
490 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
496 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
498 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
499 error = in_getsockaddr(so, nam);
501 in6_sin_2_v4mapsin6_in_sock(nam);
503 /* scope issues will be handled in in6_getsockaddr(). */
504 error = in6_getsockaddr(so, nam);
511 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
517 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
519 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
520 error = in_getpeeraddr(so, nam);
522 in6_sin_2_v4mapsin6_in_sock(nam);
524 /* scope issues will be handled in in6_getpeeraddr(). */
525 error = in6_getpeeraddr(so, nam);
531 * Pass some notification to all connections of a protocol
532 * associated with address dst. The local address and/or port numbers
533 * may be specified to limit the search. The "usual action" will be
534 * taken, depending on the ctlinput cmd. The caller must filter any
535 * cmds that are uninteresting (e.g., no error in the map).
536 * Call the protocol specific routine (if any) to report
537 * any errors for each matching socket.
540 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
541 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
542 int cmd, void *cmdarg,
543 struct inpcb *(*notify)(struct inpcb *, int))
545 struct inpcb *inp, *inp_temp;
546 struct sockaddr_in6 sa6_src, *sa6_dst;
547 u_short fport = fport_arg, lport = lport_arg;
551 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
554 sa6_dst = (struct sockaddr_in6 *)dst;
555 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
559 * note that src can be NULL when we get notify by local fragmentation.
561 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
562 flowinfo = sa6_src.sin6_flowinfo;
565 * Redirects go to all references to the destination,
566 * and use in6_rtchange to invalidate the route cache.
567 * Dead host indications: also use in6_rtchange to invalidate
568 * the cache, and deliver the error to all the sockets.
569 * Otherwise, if we have knowledge of the local port and address,
570 * deliver only to that socket.
572 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
575 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
577 if (cmd != PRC_HOSTDEAD)
578 notify = in6_rtchange;
580 errno = inet6ctlerrmap[cmd];
581 INP_INFO_WLOCK(pcbinfo);
582 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
584 if ((inp->inp_vflag & INP_IPV6) == 0) {
590 * If the error designates a new path MTU for a destination
591 * and the application (associated with this socket) wanted to
592 * know the value, notify. Note that we notify for all
593 * disconnected sockets if the corresponding application
594 * wanted. This is because some UDP applications keep sending
595 * sockets disconnected.
596 * XXX: should we avoid to notify the value to TCP sockets?
598 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
599 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
600 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
601 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
602 (u_int32_t *)cmdarg);
606 * Detect if we should notify the error. If no source and
607 * destination ports are specifed, but non-zero flowinfo and
608 * local address match, notify the error. This is the case
609 * when the error is delivered with an encrypted buffer
610 * by ESP. Otherwise, just compare addresses and ports
613 if (lport == 0 && fport == 0 && flowinfo &&
614 inp->inp_socket != NULL &&
615 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
616 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
618 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
619 &sa6_dst->sin6_addr) ||
620 inp->inp_socket == 0 ||
621 (lport && inp->inp_lport != lport) ||
622 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
623 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
624 &sa6_src.sin6_addr)) ||
625 (fport && inp->inp_fport != fport)) {
632 if ((*notify)(inp, errno))
637 INP_INFO_WUNLOCK(pcbinfo);
641 * Lookup a PCB based on the local address and port.
644 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
645 u_short lport, int wild_okay, struct ucred *cred)
647 register struct inpcb *inp;
648 int matchwild = 3, wildcard;
650 INP_INFO_WLOCK_ASSERT(pcbinfo);
653 struct inpcbhead *head;
655 * Look for an unconnected (wildcard foreign addr) PCB that
656 * matches the local address and port we're looking for.
658 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
659 0, pcbinfo->ipi_hashmask)];
660 LIST_FOREACH(inp, head, inp_hash) {
661 /* XXX inp locking */
662 if ((inp->inp_vflag & INP_IPV6) == 0)
664 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
665 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
666 inp->inp_lport == lport) {
669 prison_equal_ip6(cred->cr_prison,
670 inp->inp_cred->cr_prison))
679 struct inpcbporthead *porthash;
680 struct inpcbport *phd;
681 struct inpcb *match = NULL;
683 * Best fit PCB lookup.
685 * First see if this local port is in use by looking on the
688 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
689 pcbinfo->ipi_porthashmask)];
690 LIST_FOREACH(phd, porthash, phd_hash) {
691 if (phd->phd_port == lport)
696 * Port is in use by one or more PCBs. Look for best
699 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
702 !prison_equal_ip6(cred->cr_prison,
703 inp->inp_cred->cr_prison))
705 /* XXX inp locking */
706 if ((inp->inp_vflag & INP_IPV6) == 0)
708 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
710 if (!IN6_IS_ADDR_UNSPECIFIED(
712 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
714 else if (!IN6_ARE_ADDR_EQUAL(
715 &inp->in6p_laddr, laddr))
718 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
721 if (wildcard < matchwild) {
723 matchwild = wildcard;
734 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
737 struct ip6_moptions *im6o;
740 INP_INFO_RLOCK(pcbinfo);
741 LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
743 im6o = in6p->in6p_moptions;
744 if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
746 * Unselect the outgoing ifp for multicast if it
749 if (im6o->im6o_multicast_ifp == ifp)
750 im6o->im6o_multicast_ifp = NULL;
752 * Drop multicast group membership if we joined
753 * through the interface being detached.
756 for (i = 0; i < im6o->im6o_num_memberships; i++) {
757 if (im6o->im6o_membership[i]->in6m_ifp ==
759 in6_mc_leave(im6o->im6o_membership[i],
762 } else if (gap != 0) {
763 im6o->im6o_membership[i - gap] =
764 im6o->im6o_membership[i];
767 im6o->im6o_num_memberships -= gap;
771 INP_INFO_RUNLOCK(pcbinfo);
775 * Check for alternatives when higher level complains
776 * about service problems. For now, invalidate cached
777 * routing information. If the route was created dynamically
778 * (by a redirect), time to try a default gateway again.
781 in6_losing(struct inpcb *in6p)
785 * We don't store route pointers in the routing table anymore
791 * After a routing change, flush old routing
792 * and allocate a (hopefully) better one.
795 in6_rtchange(struct inpcb *inp, int errno)
798 * We don't store route pointers in the routing table anymore
804 * Lookup PCB in hash list.
807 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
808 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
811 struct inpcbhead *head;
812 struct inpcb *inp, *tmpinp;
813 u_short fport = fport_arg, lport = lport_arg;
816 INP_INFO_LOCK_ASSERT(pcbinfo);
818 if (faithprefix_p != NULL)
819 faith = (*faithprefix_p)(laddr);
824 * First look for an exact match.
827 head = &pcbinfo->ipi_hashbase[
828 INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
829 pcbinfo->ipi_hashmask)];
830 LIST_FOREACH(inp, head, inp_hash) {
831 /* XXX inp locking */
832 if ((inp->inp_vflag & INP_IPV6) == 0)
834 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
835 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
836 inp->inp_fport == fport &&
837 inp->inp_lport == lport) {
839 * XXX We should be able to directly return
840 * the inp here, without any checks.
841 * Well unless both bound with SO_REUSEPORT?
843 if (prison_flag(inp->inp_cred, PR_IP6))
853 * Then look for a wildcard match, if requested.
855 if (wildcard == INPLOOKUP_WILDCARD) {
856 struct inpcb *local_wild = NULL, *local_exact = NULL;
857 struct inpcb *jail_wild = NULL;
861 * Order of socket selection - we always prefer jails.
862 * 1. jailed, non-wild.
864 * 3. non-jailed, non-wild.
865 * 4. non-jailed, wild.
867 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
868 0, pcbinfo->ipi_hashmask)];
869 LIST_FOREACH(inp, head, inp_hash) {
870 /* XXX inp locking */
871 if ((inp->inp_vflag & INP_IPV6) == 0)
874 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
875 inp->inp_lport != lport) {
879 /* XXX inp locking */
880 if (faith && (inp->inp_flags & INP_FAITH) == 0)
883 injail = prison_flag(inp->inp_cred, PR_IP6);
885 if (prison_check_ip6(inp->inp_cred,
889 if (local_exact != NULL)
893 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
898 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
906 if (jail_wild != NULL)
908 if (local_exact != NULL)
909 return (local_exact);
910 if (local_wild != NULL)
912 } /* if (wildcard == INPLOOKUP_WILDCARD) */
921 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
925 ip = mtod(m, struct ip6_hdr *);
926 bzero(sin6, sizeof(*sin6));
927 sin6->sin6_len = sizeof(*sin6);
928 sin6->sin6_family = AF_INET6;
929 sin6->sin6_addr = ip->ip6_src;
931 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */