2 /* $KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi Exp $ */
5 * Copyright (C) 2000 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * 6to4 interface, based on RFC3056.
36 * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
37 * There is no address mapping defined from IPv6 multicast address to IPv4
38 * address. Therefore, we do not have IFF_MULTICAST on the interface.
40 * Due to the lack of address mapping for link-local addresses, we cannot
41 * throw packets toward link-local addresses (fe80::x). Also, we cannot throw
42 * packets to link-local multicast addresses (ff02::x).
44 * Here are interesting symptoms due to the lack of link-local address:
46 * Unicast routing exchange:
47 * - RIPng: Impossible. Uses link-local multicast packet toward ff02::9,
48 * and link-local addresses as nexthop.
49 * - OSPFv6: Impossible. OSPFv6 assumes that there's link-local address
50 * assigned to the link, and makes use of them. Also, HELLO packets use
51 * link-local multicast addresses (ff02::5 and ff02::6).
52 * - BGP4+: Maybe. You can only use global address as nexthop, and global
53 * address as TCP endpoint address.
55 * Multicast routing protocols:
56 * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
57 * Adjacent PIM routers must be configured manually (is it really spec-wise
58 * correct thing to do?).
61 * - Redirects cannot be used due to the lack of link-local address.
63 * stf interface does not have, and will not need, a link-local address.
64 * It seems to have no real benefit and does not help the above symptoms much.
65 * Even if we assign link-locals to interface, we cannot really
66 * use link-local unicast/multicast on top of 6to4 cloud (since there's no
67 * encapsulation defined for link-local address), and the above analysis does
68 * not change. RFC3056 does not mandate the assignment of link-local address
71 * 6to4 interface has security issues. Refer to
72 * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
73 * for details. The code tries to filter out some of malicious packets.
74 * Note that there is no way to be 100% secure.
78 #include "opt_inet6.h"
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/socket.h>
84 #include <sys/sockio.h>
87 #include <sys/errno.h>
88 #include <sys/kernel.h>
89 #include <sys/module.h>
90 #include <sys/protosw.h>
91 #include <sys/queue.h>
92 #include <machine/cpu.h>
94 #include <sys/malloc.h>
97 #include <net/route.h>
98 #include <net/netisr.h>
99 #include <net/if_types.h>
100 #include <net/if_stf.h>
102 #include <netinet/in.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/ip.h>
105 #include <netinet/ip_var.h>
106 #include <netinet/in_var.h>
108 #include <netinet/ip6.h>
109 #include <netinet6/ip6_var.h>
110 #include <netinet6/in6_var.h>
111 #include <netinet/ip_ecn.h>
113 #include <netinet/ip_encap.h>
115 #include <machine/stdarg.h>
117 #include <net/net_osdep.h>
121 #define STFNAME "stf"
123 #define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002)
126 * XXX: Return a pointer with 16-bit aligned. Don't cast it to
127 * struct in_addr *; use bcopy() instead.
129 #define GET_V4(x) ((caddr_t)(&(x)->s6_addr16[1]))
132 struct ifnet sc_if; /* common area */
134 struct route __sc_ro4;
135 struct route_in6 __sc_ro6; /* just for safety */
137 #define sc_ro __sc_ro46.__sc_ro4
138 const struct encaptab *encap_cookie;
139 LIST_ENTRY(stf_softc) sc_list; /* all stf's are linked */
143 * All mutable global variables in if_stf.c are protected by stf_mtx.
144 * XXXRW: Note that mutable fields in the softc are not currently locked:
145 * in particular, sc_ro needs to be protected from concurrent entrance
148 static struct mtx stf_mtx;
149 static LIST_HEAD(, stf_softc) stf_softc_list;
151 static MALLOC_DEFINE(M_STF, STFNAME, "6to4 Tunnel Interface");
152 static const int ip_stf_ttl = 40;
154 extern struct domain inetdomain;
155 struct protosw in_stf_protosw =
156 { SOCK_RAW, &inetdomain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR,
157 in_stf_input, (pr_output_t*)rip_output, 0, rip_ctloutput,
163 static int stfmodevent(module_t, int, void *);
164 static int stf_encapcheck(const struct mbuf *, int, int, void *);
165 static struct in6_ifaddr *stf_getsrcifa6(struct ifnet *);
166 static int stf_output(struct ifnet *, struct mbuf *, struct sockaddr *,
168 static int isrfc1918addr(struct in_addr *);
169 static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
171 static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
173 static void stf_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
174 static int stf_ioctl(struct ifnet *, u_long, caddr_t);
176 static int stf_clone_create(struct if_clone *, int);
177 static void stf_clone_destroy(struct ifnet *);
179 /* only one clone is currently allowed */
180 struct if_clone stf_cloner =
181 IF_CLONE_INITIALIZER(STFNAME, stf_clone_create, stf_clone_destroy, 0, 0);
184 stf_clone_create(ifc, unit)
185 struct if_clone *ifc;
188 struct stf_softc *sc;
191 sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
193 if_initname(ifp, ifc->ifc_name, unit);
195 sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
196 stf_encapcheck, &in_stf_protosw, sc);
197 if (sc->encap_cookie == NULL) {
198 if_printf(ifp, "attach failed\n");
203 ifp->if_mtu = IPV6_MMTU;
204 ifp->if_ioctl = stf_ioctl;
205 ifp->if_output = stf_output;
206 ifp->if_type = IFT_STF;
207 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
209 bpfattach(ifp, DLT_NULL, sizeof(u_int));
211 LIST_INSERT_HEAD(&stf_softc_list, sc, sc_list);
212 mtx_unlock(&stf_mtx);
217 stf_destroy(struct stf_softc *sc)
221 err = encap_detach(sc->encap_cookie);
222 KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
223 bpfdetach(&sc->sc_if);
224 if_detach(&sc->sc_if);
230 stf_clone_destroy(ifp)
233 struct stf_softc *sc = (void *) ifp;
236 LIST_REMOVE(sc, sc_list);
237 mtx_unlock(&stf_mtx);
243 stfmodevent(mod, type, data)
248 struct stf_softc *sc;
252 mtx_init(&stf_mtx, "stf_mtx", NULL, MTX_DEF);
253 LIST_INIT(&stf_softc_list);
254 if_clone_attach(&stf_cloner);
258 if_clone_detach(&stf_cloner);
261 while ((sc = LIST_FIRST(&stf_softc_list)) != NULL) {
262 LIST_REMOVE(sc, sc_list);
263 mtx_unlock(&stf_mtx);
267 mtx_unlock(&stf_mtx);
268 mtx_destroy(&stf_mtx);
275 static moduledata_t stf_mod = {
281 DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
284 stf_encapcheck(m, off, proto, arg)
285 const struct mbuf *m;
291 struct in6_ifaddr *ia6;
292 struct stf_softc *sc;
293 struct in_addr a, b, mask;
295 sc = (struct stf_softc *)arg;
299 if ((sc->sc_if.if_flags & IFF_UP) == 0)
302 /* IFF_LINK0 means "no decapsulation" */
303 if ((sc->sc_if.if_flags & IFF_LINK0) != 0)
306 if (proto != IPPROTO_IPV6)
309 /* LINTED const cast */
310 m_copydata((struct mbuf *)(uintptr_t)m, 0, sizeof(ip), (caddr_t)&ip);
315 ia6 = stf_getsrcifa6(&sc->sc_if);
320 * check if IPv4 dst matches the IPv4 address derived from the
321 * local 6to4 address.
322 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
324 if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
325 sizeof(ip.ip_dst)) != 0)
329 * check if IPv4 src matches the IPv4 address derived from the
330 * local 6to4 address masked by prefixmask.
331 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
332 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
334 bzero(&a, sizeof(a));
335 bcopy(GET_V4(&ia6->ia_addr.sin6_addr), &a, sizeof(a));
336 bcopy(GET_V4(&ia6->ia_prefixmask.sin6_addr), &mask, sizeof(mask));
337 a.s_addr &= mask.s_addr;
339 b.s_addr &= mask.s_addr;
340 if (a.s_addr != b.s_addr)
343 /* stf interface makes single side match only */
347 static struct in6_ifaddr *
352 struct in_ifaddr *ia4;
353 struct sockaddr_in6 *sin6;
356 for (ia = TAILQ_FIRST(&ifp->if_addrlist);
358 ia = TAILQ_NEXT(ia, ifa_list))
360 if (ia->ifa_addr == NULL)
362 if (ia->ifa_addr->sa_family != AF_INET6)
364 sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
365 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
368 bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
369 LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
370 if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
375 return (struct in6_ifaddr *)ia;
382 stf_output(ifp, m, dst, rt)
385 struct sockaddr *dst;
388 struct stf_softc *sc;
389 struct sockaddr_in6 *dst6;
392 struct sockaddr_in *dst4;
396 struct in6_ifaddr *ia6;
400 error = mac_check_ifnet_transmit(ifp, m);
407 sc = (struct stf_softc*)ifp;
408 dst6 = (struct sockaddr_in6 *)dst;
411 if ((ifp->if_flags & IFF_UP) == 0) {
418 * If we don't have an ip4 address that match my inner ip6 address,
419 * we shouldn't generate output. Without this check, we'll end up
420 * using wrong IPv4 source.
422 ia6 = stf_getsrcifa6(ifp);
429 if (m->m_len < sizeof(*ip6)) {
430 m = m_pullup(m, sizeof(*ip6));
436 ip6 = mtod(m, struct ip6_hdr *);
437 tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
440 * Pickup the right outer dst addr from the list of candidates.
441 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
444 if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
445 ptr = GET_V4(&ip6->ip6_dst);
446 else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
447 ptr = GET_V4(&dst6->sin6_addr);
453 bcopy(ptr, &in4, sizeof(in4));
458 * We need to prepend the address family as
459 * a four byte field. Cons up a dummy header
460 * to pacify bpf. This is safe because bpf
461 * will only read from the mbuf (i.e., it won't
462 * try to free it or keep a pointer a to it).
464 u_int32_t af = AF_INET6;
470 m0.m_data = (char *)⁡
474 bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
477 #endif /*NBPFILTER > 0*/
479 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
480 if (m && m->m_len < sizeof(struct ip))
481 m = m_pullup(m, sizeof(struct ip));
486 ip = mtod(m, struct ip *);
488 bzero(ip, sizeof(*ip));
490 bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
491 &ip->ip_src, sizeof(ip->ip_src));
492 bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst));
493 ip->ip_p = IPPROTO_IPV6;
494 ip->ip_ttl = ip_stf_ttl;
495 ip->ip_len = m->m_pkthdr.len; /*host order*/
496 if (ifp->if_flags & IFF_LINK1)
497 ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
499 ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
502 * XXXRW: Locking of sc_ro required.
504 dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
505 if (dst4->sin_family != AF_INET ||
506 bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
507 /* cache route doesn't match */
508 dst4->sin_family = AF_INET;
509 dst4->sin_len = sizeof(struct sockaddr_in);
510 bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
511 if (sc->sc_ro.ro_rt) {
512 RTFREE(sc->sc_ro.ro_rt);
513 sc->sc_ro.ro_rt = NULL;
517 if (sc->sc_ro.ro_rt == NULL) {
519 if (sc->sc_ro.ro_rt == NULL) {
527 return ip_output(m, NULL, &sc->sc_ro, 0, NULL, NULL);
535 * returns 1 if private address range:
536 * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
538 if ((ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
539 (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
540 (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168)
547 stf_checkaddr4(sc, in, inifp)
548 struct stf_softc *sc;
550 struct ifnet *inifp; /* incoming interface */
552 struct in_ifaddr *ia4;
555 * reject packets with the following address:
556 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
558 if (IN_MULTICAST(ntohl(in->s_addr)))
560 switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
561 case 0: case 127: case 255:
566 * reject packets with private address range.
567 * (requirement from RFC3056 section 2 1st paragraph)
569 if (isrfc1918addr(in))
573 * reject packets with broadcast
575 for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
577 ia4 = TAILQ_NEXT(ia4, ia_link))
579 if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
581 if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
586 * perform ingress filter
588 if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) {
589 struct sockaddr_in sin;
592 bzero(&sin, sizeof(sin));
593 sin.sin_family = AF_INET;
594 sin.sin_len = sizeof(struct sockaddr_in);
596 rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
597 if (!rt || rt->rt_ifp != inifp) {
599 log(LOG_WARNING, "%s: packet from 0x%x dropped "
600 "due to ingress filter\n", if_name(&sc->sc_if),
601 (u_int32_t)ntohl(sin.sin_addr.s_addr));
614 stf_checkaddr6(sc, in6, inifp)
615 struct stf_softc *sc;
616 struct in6_addr *in6;
617 struct ifnet *inifp; /* incoming interface */
620 * check 6to4 addresses
622 if (IN6_IS_ADDR_6TO4(in6)) {
624 bcopy(GET_V4(in6), &in4, sizeof(in4));
625 return stf_checkaddr4(sc, &in4, inifp);
629 * reject anything that look suspicious. the test is implemented
630 * in ip6_input too, but we check here as well to
631 * (1) reject bad packets earlier, and
632 * (2) to be safe against future ip6_input change.
634 if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
646 struct stf_softc *sc;
652 proto = mtod(m, struct ip *)->ip_p;
654 if (proto != IPPROTO_IPV6) {
659 ip = mtod(m, struct ip *);
661 sc = (struct stf_softc *)encap_getarg(m);
663 if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
671 mac_create_mbuf_from_ifnet(ifp, m);
675 * perform sanity check against outer src/dst.
676 * for source, perform ingress filter as well.
678 if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
679 stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
687 if (m->m_len < sizeof(*ip6)) {
688 m = m_pullup(m, sizeof(*ip6));
692 ip6 = mtod(m, struct ip6_hdr *);
695 * perform sanity check against inner src/dst.
696 * for source, perform ingress filter as well.
698 if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
699 stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
704 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
705 if ((ifp->if_flags & IFF_LINK1) != 0)
706 ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
708 ip_ecn_egress(ECN_NOCARE, &otos, &itos);
709 ip6->ip6_flow &= ~htonl(0xff << 20);
710 ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
712 m->m_pkthdr.rcvif = ifp;
716 * We need to prepend the address family as
717 * a four byte field. Cons up a dummy header
718 * to pacify bpf. This is safe because bpf
719 * will only read from the mbuf (i.e., it won't
720 * try to free it or keep a pointer a to it).
722 u_int32_t af = AF_INET6;
728 m0.m_data = (char *)⁡
732 bpf_mtap2(ifp->if_bpf, &af, sizeof(ah), m);
737 * Put the packet to the network layer input queue according to the
738 * specified address family.
739 * See net/if_gif.c for possible issues with packet processing
740 * reorder due to extra queueing.
743 ifp->if_ibytes += m->m_pkthdr.len;
744 netisr_dispatch(NETISR_IPV6, m);
749 stf_rtrequest(cmd, rt, info)
752 struct rt_addrinfo *info;
757 rt->rt_rmx.rmx_mtu = IPV6_MMTU;
761 stf_ioctl(ifp, cmd, data)
768 struct sockaddr_in6 *sin6;
775 ifa = (struct ifaddr *)data;
776 if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
777 error = EAFNOSUPPORT;
780 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
781 if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
785 bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr));
786 if (isrfc1918addr(&addr)) {
791 ifa->ifa_rtrequest = stf_rtrequest;
792 ifp->if_flags |= IFF_UP;
797 ifr = (struct ifreq *)data;
798 if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
801 error = EAFNOSUPPORT;