1 /* $FreeBSD: src/sys/netinet6/in6_pcb.c,v 1.10.2.9 2003/01/24 05:11:35 sam Exp $ */
2 /* $DragonFly: src/sys/netinet6/in6_pcb.c,v 1.35 2008/09/04 09:08:22 hasso Exp $ */
3 /* $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
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
36 * Copyright (c) 1982, 1986, 1991, 1993
37 * The Regents of the University of California. All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/malloc.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/sockio.h>
83 #include <sys/errno.h>
89 #include <sys/thread2.h>
90 #include <sys/msgport2.h>
92 #include <vm/vm_zone.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/ip6.h>
102 #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>
109 #include <netinet6/ipsec.h>
111 #include <netinet6/ipsec6.h>
113 #include <netinet6/ah.h>
115 #include <netinet6/ah6.h>
117 #include <netproto/key/key.h>
121 #include <netproto/ipsec/ipsec.h>
122 #include <netproto/ipsec/ipsec6.h>
123 #include <netproto/ipsec/key.h>
125 #endif /* FAST_IPSEC */
127 struct in6_addr zeroin6_addr;
130 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
132 struct socket *so = inp->inp_socket;
133 struct sockaddr_in6 *sin6 = NULL;
134 struct sockaddr_in6 jsin6;
135 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
136 struct proc *p = td->td_proc;
137 struct ucred *cred = NULL;
139 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
141 if (!in6_ifaddr) /* XXX broken! */
142 return (EADDRNOTAVAIL);
143 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
145 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
150 sin6 = (struct sockaddr_in6 *)nam;
151 if (nam->sa_len != sizeof(*sin6))
156 if (nam->sa_family != AF_INET6)
157 return (EAFNOSUPPORT);
159 if (!prison_replace_wildcards(td, nam))
162 /* KAME hack: embed scopeid */
163 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
165 /* this must be cleared for ifa_ifwithaddr() */
166 sin6->sin6_scope_id = 0;
168 lport = sin6->sin6_port;
169 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
171 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
172 * allow compepte duplication of binding if
173 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
174 * and a multicast address is bound on both
175 * new and duplicated sockets.
177 if (so->so_options & SO_REUSEADDR)
178 reuseport = SO_REUSEADDR|SO_REUSEPORT;
179 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
180 struct ifaddr *ia = NULL;
182 sin6->sin6_port = 0; /* yech... */
183 if (!prison_replace_wildcards(td, (struct sockaddr *)sin6)) {
184 sin6->sin6_addr = kin6addr_any;
187 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
188 return (EADDRNOTAVAIL);
191 * XXX: bind to an anycast address might accidentally
192 * cause sending a packet with anycast source address.
193 * We should allow to bind to a deprecated address, since
194 * the application dare to use it.
197 ((struct in6_ifaddr *)ia)->ia6_flags &
198 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
199 return (EADDRNOTAVAIL);
206 if (ntohs(lport) < IPV6PORT_RESERVED && cred &&
207 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0))
209 if (so->so_cred->cr_uid != 0 &&
210 !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
211 t = in6_pcblookup_local(pcbinfo,
212 &sin6->sin6_addr, lport,
213 INPLOOKUP_WILDCARD, cred);
215 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
216 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
217 (t->inp_socket->so_options &
218 SO_REUSEPORT) == 0) &&
219 (so->so_cred->cr_uid !=
220 t->inp_socket->so_cred->cr_uid))
222 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
223 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
224 struct sockaddr_in sin;
226 in6_sin6_2_sin(&sin, sin6);
227 t = in_pcblookup_local(pcbinfo,
229 INPLOOKUP_WILDCARD, cred);
231 (so->so_cred->cr_uid !=
232 t->inp_socket->so_cred->cr_uid) &&
233 (ntohl(t->inp_laddr.s_addr) !=
236 INP_SOCKAF(t->inp_socket)))
240 if (cred && cred->cr_prison &&
241 !prison_replace_wildcards(td, nam))
242 return(EADDRNOTAVAIL);
243 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
245 if (t && (reuseport & t->inp_socket->so_options) == 0)
247 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
248 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
249 struct sockaddr_in sin;
251 in6_sin6_2_sin(&sin, sin6);
252 t = in_pcblookup_local(pcbinfo,
256 (reuseport & t->inp_socket->so_options)
258 (ntohl(t->inp_laddr.s_addr)
261 INP_SOCKAF(t->inp_socket)))
265 inp->in6p_laddr = sin6->sin6_addr;
270 jsin6.sin6_addr = inp->in6p_laddr;
271 jsin6.sin6_family = AF_INET6;
272 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin6)) {
273 inp->in6p_laddr = kin6addr_any;
278 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, td)) != 0)
282 inp->inp_lport = lport;
283 if (in_pcbinsporthash(inp) != 0) {
284 inp->in6p_laddr = kin6addr_any;
293 * Transform old in6_pcbconnect() into an inner subroutine for new
294 * in6_pcbconnect(): Do some validity-checking on the remote
295 * address (in mbuf 'nam') and then determine local host address
296 * (i.e., which interface) to use to access that remote host.
298 * This preserves definition of in6_pcbconnect(), while supporting a
299 * slightly different version for T/TCP. (This is more than
300 * a bit of a kludge, but cleaning up the internal interfaces would
301 * have forced minor changes in every protocol).
305 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
306 struct in6_addr **plocal_addr6, struct thread *td)
308 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
309 struct ifnet *ifp = NULL;
312 if (nam->sa_len != sizeof (*sin6))
314 if (sin6->sin6_family != AF_INET6)
315 return (EAFNOSUPPORT);
316 if (sin6->sin6_port == 0)
317 return (EADDRNOTAVAIL);
319 /* KAME hack: embed scopeid */
320 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
325 * If the destination address is UNSPECIFIED addr,
326 * use the loopback addr, e.g ::1.
328 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
329 sin6->sin6_addr = kin6addr_loopback;
333 * XXX: in6_selectsrc might replace the bound local address
334 * with the address specified by setsockopt(IPV6_PKTINFO).
335 * Is it the intended behavior?
337 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
340 &inp->in6p_laddr, &error, td);
341 if (*plocal_addr6 == 0) {
343 error = EADDRNOTAVAIL;
347 * Don't do pcblookup call here; return interface in
349 * and exit to caller, that will do the lookup.
353 if (inp->in6p_route.ro_rt)
354 ifp = inp->in6p_route.ro_rt->rt_ifp;
361 * Connect from a socket to a specified address.
362 * Both address and port must be specified in argument sin.
363 * If don't have a local address for this socket yet,
367 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
369 struct in6_addr *addr6;
370 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
374 * Call inner routine, to assign local interface address.
375 * in6_pcbladdr() may automatically fill in sin6_scope_id.
377 if ((error = in6_pcbladdr(inp, nam, &addr6, td)) != 0)
380 if (in6_pcblookup_hash(inp->inp_cpcbinfo, &sin6->sin6_addr,
382 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
383 ? addr6 : &inp->in6p_laddr,
384 inp->inp_lport, 0, NULL) != NULL) {
387 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
388 if (inp->inp_lport == 0) {
389 error = in6_pcbbind(inp, NULL, td);
393 inp->in6p_laddr = *addr6;
395 inp->in6p_faddr = sin6->sin6_addr;
396 inp->inp_fport = sin6->sin6_port;
397 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
398 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
399 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
400 inp->in6p_flowinfo |=
401 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
403 in_pcbinsconnhash(inp);
409 * Return an IPv6 address, which is the most appropriate for given
410 * destination and user specified options.
411 * If necessary, this function lookups the routing table and return
412 * an entry to the caller for later use.
415 in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
416 struct ip6_moptions *mopts, struct route_in6 *ro,
417 struct in6_addr *laddr, int *errorp, struct thread *td)
419 struct sockaddr_in6 jsin6;
420 struct ucred *cred = NULL;
421 struct in6_addr *dst;
422 struct in6_ifaddr *ia6 = 0;
423 struct in6_pktinfo *pi = NULL;
426 if (td && td->td_proc && td->td_proc->p_ucred)
427 cred = td->td_proc->p_ucred;
428 if (cred && cred->cr_prison)
430 jsin6.sin6_family = AF_INET6;
431 dst = &dstsock->sin6_addr;
435 * If the source address is explicitly specified by the caller,
438 if (opts && (pi = opts->ip6po_pktinfo) &&
439 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
440 jsin6.sin6_addr = pi->ipi6_addr;
441 if (jailed && !jailed_ip(cred->cr_prison,
442 (struct sockaddr *)&jsin6)) {
445 return (&pi->ipi6_addr);
450 * If the source address is not specified but the socket(if any)
451 * is already bound, use the bound address.
453 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
454 jsin6.sin6_addr = *laddr;
455 if (jailed && !jailed_ip(cred->cr_prison,
456 (struct sockaddr *)&jsin6)) {
464 * If the caller doesn't specify the source address but
465 * the outgoing interface, use an address associated with
468 if (pi && pi->ipi6_ifindex) {
469 /* XXX boundary check is assumed to be already done. */
470 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
474 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
475 if (!jailed_ip(cred->cr_prison,
476 (struct sockaddr *)&jsin6))
481 *errorp = EADDRNOTAVAIL;
484 return (&satosin6(&ia6->ia_addr)->sin6_addr);
488 * If the destination address is a link-local unicast address or
489 * a multicast address, and if the outgoing interface is specified
490 * by the sin6_scope_id filed, use an address associated with the
492 * XXX: We're now trying to define more specific semantics of
493 * sin6_scope_id field, so this part will be rewritten in
496 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
497 dstsock->sin6_scope_id) {
499 * I'm not sure if boundary check for scope_id is done
502 if (dstsock->sin6_scope_id < 0 ||
503 if_index < dstsock->sin6_scope_id) {
504 *errorp = ENXIO; /* XXX: better error? */
507 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
511 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
512 if (!jailed_ip(cred->cr_prison,
513 (struct sockaddr *)&jsin6))
518 *errorp = EADDRNOTAVAIL;
521 return (&satosin6(&ia6->ia_addr)->sin6_addr);
525 * If the destination address is a multicast address and
526 * the outgoing interface for the address is specified
527 * by the caller, use an address associated with the interface.
528 * There is a sanity check here; if the destination has node-local
529 * scope, the outgoing interfacde should be a loopback address.
530 * Even if the outgoing interface is not specified, we also
531 * choose a loopback interface as the outgoing interface.
533 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
534 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
536 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
541 ia6 = in6_ifawithscope(ifp, dst);
543 *errorp = EADDRNOTAVAIL;
546 return (&ia6->ia_addr.sin6_addr);
551 * If the next hop address for the packet is specified
552 * by caller, use an address associated with the route
556 struct sockaddr_in6 *sin6_next;
559 if (opts && opts->ip6po_nexthop) {
560 sin6_next = satosin6(opts->ip6po_nexthop);
561 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
563 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
565 ia6 = ifatoia6(rt->rt_ifa);
568 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
569 if (!jailed_ip(cred->cr_prison,
570 (struct sockaddr *)&jsin6))
575 *errorp = EADDRNOTAVAIL;
578 return (&satosin6(&ia6->ia_addr)->sin6_addr);
583 * If route is known or can be allocated now,
584 * our src addr is taken from the i/f, else punt.
588 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
592 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
593 struct sockaddr_in6 *dst6;
595 /* No route yet, so try to acquire one */
596 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
598 dst6->sin6_family = AF_INET6;
599 dst6->sin6_len = sizeof(struct sockaddr_in6);
600 dst6->sin6_addr = *dst;
601 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
603 rtpurelookup((struct sockaddr *)&ro->ro_dst);
605 rtalloc((struct route *)ro);
610 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
611 * the address. But we don't know why it does so.
612 * It is necessary to ensure the scope even for lo0
613 * so doesn't check out IFF_LOOPBACK.
617 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
619 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
620 if (!jailed_ip(cred->cr_prison,
621 (struct sockaddr *)&jsin6))
625 if (ia6 == 0) /* xxx scope error ?*/
626 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
629 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
630 if (!jailed_ip(cred->cr_prison,
631 (struct sockaddr *)&jsin6))
636 *errorp = EHOSTUNREACH; /* no route */
639 return (&satosin6(&ia6->ia_addr)->sin6_addr);
642 *errorp = EADDRNOTAVAIL;
647 * Default hop limit selection. The precedence is as follows:
648 * 1. Hoplimit valued specified via ioctl.
649 * 2. (If the outgoing interface is detected) the current
650 * hop limit of the interface specified by router advertisement.
651 * 3. The system default hoplimit.
654 in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp)
656 if (in6p && in6p->in6p_hops >= 0)
657 return (in6p->in6p_hops);
659 return (ND_IFINFO(ifp)->chlim);
661 return (ip6_defhlim);
666 in6_pcbdisconnect(struct inpcb *inp)
668 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
670 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
671 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
672 in_pcbremconnhash(inp);
673 if (inp->inp_socket->so_state & SS_NOFDREF)
678 in6_pcbdetach(struct inpcb *inp)
680 struct socket *so = inp->inp_socket;
681 struct inpcbinfo *ipi = inp->inp_pcbinfo;
684 if (inp->in6p_sp != NULL)
685 ipsec6_delete_pcbpolicy(inp);
687 inp->inp_gencnt = ++ipi->ipi_gencnt;
690 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
691 sofree(so); /* remove pcb ref */
693 if (inp->in6p_options)
694 m_freem(inp->in6p_options);
695 ip6_freepcbopts(inp->in6p_outputopts);
696 ip6_freemoptions(inp->in6p_moptions);
697 if (inp->in6p_route.ro_rt)
698 rtfree(inp->in6p_route.ro_rt);
699 /* Check and free IPv4 related resources in case of mapped addr */
700 if (inp->inp_options)
701 m_free(inp->inp_options);
702 ip_freemoptions(inp->inp_moptions);
709 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
710 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
711 * in struct pr_usrreqs, so that protocols can just reference then directly
712 * without the need for a wrapper function. The socket must have a valid
713 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
714 * except through a kernel programming error, so it is acceptable to panic
715 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
716 * because there actually /is/ a programming error somewhere... XXX)
719 in6_setsockaddr_dispatch(netmsg_t msg)
723 error = in6_setsockaddr(msg->sockaddr.base.nm_so, msg->sockaddr.nm_nam);
724 lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
728 in6_setsockaddr(struct socket *so, struct sockaddr **nam)
731 struct sockaddr_in6 *sin6;
734 * Do the malloc first in case it blocks.
736 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME,
738 sin6->sin6_family = AF_INET6;
739 sin6->sin6_len = sizeof(*sin6);
745 kfree(sin6, M_SONAME);
748 sin6->sin6_port = inp->inp_lport;
749 sin6->sin6_addr = inp->in6p_laddr;
751 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
752 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
754 sin6->sin6_scope_id = 0; /*XXX*/
755 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
756 sin6->sin6_addr.s6_addr16[1] = 0;
758 *nam = (struct sockaddr *)sin6;
763 in6_setpeeraddr_dispatch(netmsg_t msg)
767 error = in6_setpeeraddr(msg->peeraddr.base.nm_so, msg->peeraddr.nm_nam);
768 lwkt_replymsg(&msg->peeraddr.base.lmsg, error);
772 in6_setpeeraddr(struct socket *so, struct sockaddr **nam)
775 struct sockaddr_in6 *sin6;
778 * Do the malloc first in case it blocks.
780 MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME,
782 sin6->sin6_family = AF_INET6;
783 sin6->sin6_len = sizeof(struct sockaddr_in6);
789 kfree(sin6, M_SONAME);
792 sin6->sin6_port = inp->inp_fport;
793 sin6->sin6_addr = inp->in6p_faddr;
795 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
796 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
798 sin6->sin6_scope_id = 0; /*XXX*/
799 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
800 sin6->sin6_addr.s6_addr16[1] = 0;
802 *nam = (struct sockaddr *)sin6;
807 in6_mapped_sockaddr_dispatch(netmsg_t msg)
811 error = in6_mapped_sockaddr(msg->sockaddr.base.nm_so,
812 msg->sockaddr.nm_nam);
813 lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
817 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
819 struct inpcb *inp = so->so_pcb;
824 if (inp->inp_vflag & INP_IPV4) {
825 error = in_setsockaddr(so, nam);
827 in6_sin_2_v4mapsin6_in_sock(nam);
829 /* scope issues will be handled in in6_setsockaddr(). */
830 error = in6_setsockaddr(so, nam);
836 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
838 struct inpcb *inp = so->so_pcb;
843 if (inp->inp_vflag & INP_IPV4) {
844 error = in_setpeeraddr(so, nam);
846 in6_sin_2_v4mapsin6_in_sock(nam);
848 /* scope issues will be handled in in6_setpeeraddr(). */
849 error = in6_setpeeraddr(so, nam);
855 in6_mapped_peeraddr_dispatch(netmsg_t msg)
859 error = in6_mapped_peeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
860 lwkt_replymsg(&msg->base.lmsg, error);
864 * Pass some notification to all connections of a protocol
865 * associated with address dst. The local address and/or port numbers
866 * may be specified to limit the search. The "usual action" will be
867 * taken, depending on the ctlinput cmd. The caller must filter any
868 * cmds that are uninteresting (e.g., no error in the map).
869 * Call the protocol specific routine (if any) to report
870 * any errors for each matching socket.
872 * Must be called under crit_enter().
875 in6_pcbnotify(struct inpcbhead *head, struct sockaddr *dst, in_port_t fport,
876 const struct sockaddr *src, in_port_t lport, int cmd, int arg,
877 void (*notify) (struct inpcb *, int))
879 struct inpcb *inp, *ninp;
880 struct sockaddr_in6 sa6_src, *sa6_dst;
883 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
886 sa6_dst = (struct sockaddr_in6 *)dst;
887 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
891 * note that src can be NULL when we get notify by local fragmentation.
893 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
894 flowinfo = sa6_src.sin6_flowinfo;
897 * Redirects go to all references to the destination,
898 * and use in6_rtchange to invalidate the route cache.
899 * Dead host indications: also use in6_rtchange to invalidate
900 * the cache, and deliver the error to all the sockets.
901 * Otherwise, if we have knowledge of the local port and address,
902 * deliver only to that socket.
904 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
907 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
909 if (cmd != PRC_HOSTDEAD)
910 notify = in6_rtchange;
912 if (cmd != PRC_MSGSIZE)
913 arg = inet6ctlerrmap[cmd];
915 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
916 ninp = LIST_NEXT(inp, inp_list);
918 if (inp->inp_flags & INP_PLACEMARKER)
921 if ((inp->inp_vflag & INP_IPV6) == 0)
924 * If the error designates a new path MTU for a destination
925 * and the application (associated with this socket) wanted to
926 * know the value, notify. Note that we notify for all
927 * disconnected sockets if the corresponding application
928 * wanted. This is because some UDP applications keep sending
929 * sockets disconnected.
930 * XXX: should we avoid to notify the value to TCP sockets?
932 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
933 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
934 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
935 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, &arg);
939 * Detect if we should notify the error. If no source and
940 * destination ports are specifed, but non-zero flowinfo and
941 * local address match, notify the error. This is the case
942 * when the error is delivered with an encrypted buffer
943 * by ESP. Otherwise, just compare addresses and ports
946 if (lport == 0 && fport == 0 && flowinfo &&
947 inp->inp_socket != NULL &&
948 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
949 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
951 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
952 &sa6_dst->sin6_addr) ||
953 inp->inp_socket == 0 ||
954 (lport && inp->inp_lport != lport) ||
955 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
956 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
957 &sa6_src.sin6_addr)) ||
958 (fport && inp->inp_fport != fport))
969 * Lookup a PCB based on the local address and port.
972 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
973 u_int lport_arg, int wild_okay, struct ucred *cred)
976 int matchwild = 3, wildcard;
977 u_short lport = lport_arg;
978 struct inpcbporthead *porthash;
979 struct inpcbport *phd;
980 struct inpcb *match = NULL;
983 * If the porthashbase is shared across several cpus we need
986 if (pcbinfo->porttoken)
987 lwkt_gettoken(pcbinfo->porttoken);
990 * Best fit PCB lookup.
992 * First see if this local port is in use by looking on the
995 porthash = &pcbinfo->porthashbase[
996 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
997 LIST_FOREACH(phd, porthash, phd_hash) {
998 if (phd->phd_port == lport)
1004 * Port is in use by one or more PCBs. Look for best
1007 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1009 if ((inp->inp_vflag & INP_IPV6) == 0)
1011 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
1013 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1014 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
1016 else if (!IN6_ARE_ADDR_EQUAL(
1017 &inp->in6p_laddr, laddr))
1020 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
1023 if (wildcard && !wild_okay)
1025 if (wildcard < matchwild &&
1028 inp->inp_socket->so_cred->cr_prison)) {
1030 matchwild = wildcard;
1034 matchwild = wildcard;
1038 if (pcbinfo->porttoken)
1039 lwkt_reltoken(pcbinfo->porttoken);
1045 in6_pcbpurgeif0(struct in6pcb *head, struct ifnet *ifp)
1047 struct in6pcb *in6p;
1048 struct ip6_moptions *im6o;
1049 struct in6_multi_mship *imm, *nimm;
1051 for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
1052 if (in6p->in6p_flags & INP_PLACEMARKER)
1054 im6o = in6p->in6p_moptions;
1055 if ((in6p->inp_vflag & INP_IPV6) &&
1058 * Unselect the outgoing interface if it is being
1061 if (im6o->im6o_multicast_ifp == ifp)
1062 im6o->im6o_multicast_ifp = NULL;
1065 * Drop multicast group membership if we joined
1066 * through the interface being detached.
1067 * XXX controversial - is it really legal for kernel
1070 for (imm = im6o->im6o_memberships.lh_first;
1071 imm != NULL; imm = nimm) {
1072 nimm = imm->i6mm_chain.le_next;
1073 if (imm->i6mm_maddr->in6m_ifp == ifp) {
1074 LIST_REMOVE(imm, i6mm_chain);
1075 in6_delmulti(imm->i6mm_maddr);
1076 kfree(imm, M_IPMADDR);
1084 * Check for alternatives when higher level complains
1085 * about service problems. For now, invalidate cached
1086 * routing information. If the route was created dynamically
1087 * (by a redirect), time to try a default gateway again.
1090 in6_losing(struct inpcb *in6p)
1093 struct rt_addrinfo info;
1095 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
1096 bzero((caddr_t)&info, sizeof(info));
1097 info.rti_flags = rt->rt_flags;
1098 info.rti_info[RTAX_DST] = rt_key(rt);
1099 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1100 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1101 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
1102 if (rt->rt_flags & RTF_DYNAMIC)
1103 rtrequest1_global(RTM_DELETE, &info, NULL, NULL);
1104 in6p->in6p_route.ro_rt = NULL;
1107 * A new route can be allocated
1108 * the next time output is attempted.
1114 * After a routing change, flush old routing
1115 * and allocate a (hopefully) better one.
1118 in6_rtchange(struct inpcb *inp, int error)
1120 if (inp->in6p_route.ro_rt) {
1121 rtfree(inp->in6p_route.ro_rt);
1122 inp->in6p_route.ro_rt = 0;
1124 * A new route can be allocated the next time
1125 * output is attempted.
1131 * Lookup PCB in hash list.
1134 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1135 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1136 int wildcard, struct ifnet *ifp)
1138 struct inpcbhead *head;
1140 struct inpcb *jinp = NULL;
1141 u_short fport = fport_arg, lport = lport_arg;
1144 if (faithprefix_p != NULL)
1145 faith = (*faithprefix_p)(laddr);
1150 * First look for an exact match.
1152 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr->s6_addr32[3] /* XXX */,
1154 laddr->s6_addr32[3], /* XXX JH */
1156 pcbinfo->hashmask)];
1157 LIST_FOREACH(inp, head, inp_hash) {
1158 if ((inp->inp_vflag & INP_IPV6) == 0)
1160 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1161 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1162 inp->inp_fport == fport &&
1163 inp->inp_lport == lport) {
1167 if (inp->inp_socket == NULL ||
1168 inp->inp_socket->so_cred->cr_prison == NULL) {
1179 struct inpcontainerhead *chead;
1180 struct inpcontainer *ic;
1181 struct inpcb *local_wild = NULL;
1182 struct inpcb *jinp_wild = NULL;
1183 struct sockaddr_in6 jsin6;
1187 * Order of socket selection:
1188 * 1. non-jailed, non-wild.
1189 * 2. non-jailed, wild.
1190 * 3. jailed, non-wild.
1193 jsin6.sin6_family = AF_INET6;
1194 chead = &pcbinfo->wildcardhashbase[INP_PCBWILDCARDHASH(lport,
1195 pcbinfo->wildcardhashmask)];
1196 LIST_FOREACH(ic, chead, ic_list) {
1199 if (!(inp->inp_vflag & INP_IPV6))
1201 if (inp->inp_socket != NULL)
1202 cred = inp->inp_socket->so_cred;
1206 if (cred != NULL && jailed(cred)) {
1210 jsin6.sin6_addr = *laddr;
1211 if (!jailed_ip(cred->cr_prison,
1212 (struct sockaddr *)&jsin6))
1216 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1217 inp->inp_lport == lport) {
1218 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1220 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
1222 if (cred != NULL && jailed(cred))
1226 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1227 if (cred != NULL && jailed(cred))
1234 if (local_wild != NULL)
1235 return (local_wild);
1248 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1252 ip = mtod(m, struct ip6_hdr *);
1253 bzero(sin6, sizeof(*sin6));
1254 sin6->sin6_len = sizeof(*sin6);
1255 sin6->sin6_family = AF_INET6;
1256 sin6->sin6_addr = ip->ip6_src;
1257 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1258 sin6->sin6_addr.s6_addr16[1] = 0;
1259 sin6->sin6_scope_id =
1260 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1261 ? m->m_pkthdr.rcvif->if_index : 0;
1267 in6_savefaddr(struct socket *so, const struct sockaddr *faddr)
1269 struct sockaddr_in6 *sin6;
1271 KASSERT(faddr->sa_family == AF_INET6,
1272 ("not AF_INET6 faddr %d\n", faddr->sa_family));
1274 sin6 = kmalloc(sizeof(*sin6), M_SONAME, M_WAITOK | M_ZERO);
1275 sin6->sin6_family = AF_INET6;
1276 sin6->sin6_len = sizeof(*sin6);
1278 sin6->sin6_port = ((const struct sockaddr_in6 *)faddr)->sin6_port;
1279 sin6->sin6_addr = ((const struct sockaddr_in6 *)faddr)->sin6_addr;
1281 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1282 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
1284 sin6->sin6_scope_id = 0; /*XXX*/
1285 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1286 sin6->sin6_addr.s6_addr16[1] = 0;
1288 so->so_faddr = (struct sockaddr *)sin6;
1292 in6_mapped_savefaddr(struct socket *so, const struct sockaddr *faddr)
1294 if (faddr->sa_family == AF_INET) {
1295 in_savefaddr(so, faddr);
1296 in6_sin_2_v4mapsin6_in_sock(&so->so_faddr);
1298 in6_savefaddr(so, faddr);