1 /* $FreeBSD: src/sys/net/if_stf.c,v 1.1.2.6 2001/07/24 19:10:18 brooks Exp $ */
2 /* $KAME: if_stf.c,v 1.62 2001/06/07 22:32:16 itojun 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.
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/socket.h>
80 #include <sys/sockio.h>
82 #include <sys/errno.h>
83 #include <sys/protosw.h>
84 #include <sys/kernel.h>
85 #include <sys/syslog.h>
87 #include <sys/malloc.h>
90 #include <net/route.h>
91 #include <net/if_types.h>
93 #include <netinet/in.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/ip.h>
96 #include <netinet/ip_var.h>
97 #include <netinet/in_var.h>
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet6/in6_var.h>
102 #include <netinet/ip_ecn.h>
104 #include <netinet/ip_encap.h>
105 #include <net/dlil.h>
108 #include <net/net_osdep.h>
112 #define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002)
113 #define GET_V4(x) ((struct in_addr *)(&(x)->s6_addr16[1]))
116 struct ifnet sc_if
; /* common area */
118 u_long sc_protocol_family
; /* dlil protocol attached */
121 struct route __sc_ro4
;
122 struct route_in6 __sc_ro6
; /* just for safety */
124 #define sc_ro __sc_ro46.__sc_ro4
125 const struct encaptab
*encap_cookie
;
128 static struct stf_softc
*stf
;
131 void stfattach (void);
135 static MALLOC_DEFINE(M_STF
, "stf", "6to4 Tunnel Interface");
137 static int ip_stf_ttl
= 40;
139 static void in_stf_input(struct mbuf
*, int);
140 extern struct domain inetdomain
;
141 struct protosw in_stf_protosw
=
142 { SOCK_RAW
, &inetdomain
, IPPROTO_IPV6
, PR_ATOMIC
|PR_ADDR
,
143 in_stf_input
, 0, 0, rip_ctloutput
,
150 static int stf_encapcheck(const struct mbuf
*, int, int, void *);
151 static struct in6_ifaddr
*stf_getsrcifa6(struct ifnet
*);
152 int stf_pre_output(struct ifnet
*, u_long
, register struct mbuf
**,
153 const struct sockaddr
*, caddr_t
, char *, char *);
154 static int stf_checkaddr4(struct stf_softc
*, struct in_addr
*,
156 static int stf_checkaddr6(struct stf_softc
*, struct in6_addr
*,
158 static void stf_rtrequest(int, struct rtentry
*, struct sockaddr
*);
159 int stf_ioctl(struct ifnet
*, u_long
, void *);
164 u_long protocol_family
,
165 struct ddesc_head_str
*desc_head
)
167 /* Only one protocol may be attached at a time */
168 struct stf_softc
* stf
= (struct stf_softc
*)ifp
;
169 if (stf
->sc_protocol_family
== 0)
170 stf
->sc_protocol_family
= protocol_family
;
172 printf("stf_add_proto: stf already has a proto\n");
182 u_long protocol_family
)
184 if (((struct stf_softc
*)ifp
)->sc_protocol_family
== protocol_family
)
185 ((struct stf_softc
*)ifp
)->sc_protocol_family
= 0;
193 stf_attach_inet6(struct ifnet
*ifp
, u_long protocol_family
)
195 struct dlil_proto_reg_str reg
;
198 bzero(®
, sizeof(reg
));
199 TAILQ_INIT(®
.demux_desc_head
);
200 reg
.interface_family
= ifp
->if_family
;
201 reg
.unit_number
= ifp
->if_unit
;
202 reg
.pre_output
= stf_pre_output
;
203 reg
.protocol_family
= PF_INET6
;
205 stat
= dlil_attach_protocol(®
);
215 u_long
*protocol_family
)
217 *protocol_family
= PF_INET6
;
221 void stf_reg_if_mods()
225 /* Register protocol registration functions */
226 if ( error
= dlil_reg_proto_module(AF_INET6
, APPLE_IF_FAM_STF
, stf_attach_inet6
, NULL
) != 0)
227 kprintf("dlil_reg_proto_module failed for AF_INET6 error=%d\n", error
);
234 struct stf_softc
*sc
;
237 const struct encaptab
*p
;
239 stf_reg_if_mods(); /* DLIL modules */
241 sc
= _MALLOC(sizeof(struct stf_softc
), M_DEVBUF
, M_WAITOK
);
243 printf("stf softc attach failed\n" );
247 bzero(sc
, sizeof(*sc
));
248 sc
->sc_if
.if_name
= "stf";
249 sc
->sc_if
.if_unit
= 0;
251 p
= encap_attach_func(AF_INET
, IPPROTO_IPV6
, stf_encapcheck
,
252 &in_stf_protosw
, sc
);
254 printf("%s: attach failed\n", if_name(&sc
->sc_if
));
258 sc
->encap_cookie
= p
;
259 sc
->sc_if
.if_mtu
= IPV6_MMTU
;
260 sc
->sc_if
.if_flags
= 0;
261 sc
->sc_if
.if_ioctl
= stf_ioctl
;
262 sc
->sc_if
.if_output
= NULL
; /* processing done in pre_output */
263 sc
->sc_if
.if_type
= IFT_STF
;
264 sc
->sc_if
.if_family
= APPLE_IF_FAM_STF
;
265 sc
->sc_if
.if_add_proto
= stf_add_proto
;
266 sc
->sc_if
.if_del_proto
= stf_del_proto
;
267 sc
->sc_if
.if_demux
= stf_demux
;
269 /* turn off ingress filter */
270 sc
->sc_if
.if_flags
|= IFF_LINK2
;
272 sc
->sc_if
.if_snd
.ifq_maxlen
= IFQ_MAXLEN
;
274 if (error
= dlil_if_attach(&sc
->sc_if
))
275 printf("stfattach: can't dlil_if_attach error=%d\n");
277 bpfattach(&sc
->sc_if
, DLT_NULL
, sizeof(u_int
));
283 stf_encapcheck(m
, off
, proto
, arg
)
284 const struct mbuf
*m
;
290 struct in6_ifaddr
*ia6
;
291 struct stf_softc
*sc
;
294 sc
= (struct stf_softc
*)arg
;
298 if ((sc
->sc_if
.if_flags
& IFF_UP
) == 0)
301 /* IFF_LINK0 means "no decapsulation" */
302 if ((sc
->sc_if
.if_flags
& IFF_LINK0
) != 0)
305 if (proto
!= IPPROTO_IPV6
)
308 /* LINTED const cast */
309 m_copydata((struct mbuf
*)m
, 0, sizeof(ip
), (caddr_t
)&ip
);
314 ia6
= stf_getsrcifa6(&sc
->sc_if
);
319 * check if IPv4 dst matches the IPv4 address derived from the
320 * local 6to4 address.
321 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
323 if (bcmp(GET_V4(&ia6
->ia_addr
.sin6_addr
), &ip
.ip_dst
,
324 sizeof(ip
.ip_dst
)) != 0)
328 * check if IPv4 src matches the IPv4 address derived from the
329 * local 6to4 address masked by prefixmask.
330 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
331 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
333 bzero(&a
, sizeof(a
));
334 a
.s_addr
= GET_V4(&ia6
->ia_addr
.sin6_addr
)->s_addr
;
335 a
.s_addr
&= GET_V4(&ia6
->ia_prefixmask
.sin6_addr
)->s_addr
;
337 b
.s_addr
&= GET_V4(&ia6
->ia_prefixmask
.sin6_addr
)->s_addr
;
338 if (a
.s_addr
!= b
.s_addr
)
341 /* stf interface makes single side match only */
345 static struct in6_ifaddr
*
350 struct in_ifaddr
*ia4
;
351 struct sockaddr_in6
*sin6
;
354 ifnet_lock_shared(ifp
);
355 for (ia
= ifp
->if_addrlist
.tqh_first
;
357 ia
= ia
->ifa_list
.tqe_next
)
359 if (ia
->ifa_addr
== NULL
)
361 if (ia
->ifa_addr
->sa_family
!= AF_INET6
)
363 sin6
= (struct sockaddr_in6
*)ia
->ifa_addr
;
364 if (!IN6_IS_ADDR_6TO4(&sin6
->sin6_addr
))
367 bcopy(GET_V4(&sin6
->sin6_addr
), &in
, sizeof(in
));
368 lck_mtx_lock(rt_mtx
);
369 for (ia4
= TAILQ_FIRST(&in_ifaddrhead
);
371 ia4
= TAILQ_NEXT(ia4
, ia_link
))
373 if (ia4
->ia_addr
.sin_addr
.s_addr
== in
.s_addr
)
376 lck_mtx_unlock(rt_mtx
);
380 ifnet_lock_done(ifp
);
381 return (struct in6_ifaddr
*)ia
;
383 ifnet_lock_done(ifp
);
391 u_long protocol_family
,
392 register struct mbuf
**m0
,
393 const struct sockaddr
*dst
,
398 register struct mbuf
*m
= *m0
;
399 struct stf_softc
*sc
;
400 struct sockaddr_in6
*dst6
;
402 struct sockaddr_in
*dst4
;
406 struct in6_ifaddr
*ia6
;
409 sc
= (struct stf_softc
*)ifp
;
410 dst6
= (struct sockaddr_in6
*)dst
;
413 if ((ifp
->if_flags
& IFF_UP
) == 0) {
414 printf("stf: IFF_DOWN\n");
419 * If we don't have an ip4 address that match my inner ip6 address,
420 * we shouldn't generate output. Without this check, we'll end up
421 * using wrong IPv4 source.
423 ia6
= stf_getsrcifa6(ifp
);
428 if (m
->m_len
< sizeof(*ip6
)) {
429 m
= m_pullup(m
, sizeof(*ip6
));
431 *m0
= NULL
; /* makes sure this won't be double freed */
435 ip6
= mtod(m
, struct ip6_hdr
*);
436 tos
= (ntohl(ip6
->ip6_flow
) >> 20) & 0xff;
439 * Pickup the right outer dst addr from the list of candidates.
440 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
442 if (IN6_IS_ADDR_6TO4(&ip6
->ip6_dst
))
443 in4
= GET_V4(&ip6
->ip6_dst
);
444 else if (IN6_IS_ADDR_6TO4(&dst6
->sin6_addr
))
445 in4
= GET_V4(&dst6
->sin6_addr
);
452 * We need to prepend the address family as
453 * a four byte field. Cons up a dummy header
454 * to pacify bpf. This is safe because bpf
455 * will only read from the mbuf (i.e., it won't
456 * try to free it or keep a pointer a to it).
459 u_int32_t af
= AF_INET6
;
463 m1
.m_data
= (char *)&af
;
468 M_PREPEND(m
, sizeof(struct ip
), M_DONTWAIT
);
469 if (m
&& m
->m_len
< sizeof(struct ip
))
470 m
= m_pullup(m
, sizeof(struct ip
));
475 ip
= mtod(m
, struct ip
*);
477 bzero(ip
, sizeof(*ip
));
479 bcopy(GET_V4(&((struct sockaddr_in6
*)&ia6
->ia_addr
)->sin6_addr
),
480 &ip
->ip_src
, sizeof(ip
->ip_src
));
481 bcopy(in4
, &ip
->ip_dst
, sizeof(ip
->ip_dst
));
482 ip
->ip_p
= IPPROTO_IPV6
;
483 ip
->ip_ttl
= ip_stf_ttl
;
484 ip
->ip_len
= m
->m_pkthdr
.len
; /*host order*/
485 if (ifp
->if_flags
& IFF_LINK1
)
486 ip_ecn_ingress(ECN_ALLOWED
, &ip
->ip_tos
, &tos
);
488 ip_ecn_ingress(ECN_NOCARE
, &ip
->ip_tos
, &tos
);
490 dst4
= (struct sockaddr_in
*)&sc
->sc_ro
.ro_dst
;
491 if (dst4
->sin_family
!= AF_INET
||
492 bcmp(&dst4
->sin_addr
, &ip
->ip_dst
, sizeof(ip
->ip_dst
)) != 0) {
493 /* cache route doesn't match */
494 dst4
->sin_family
= AF_INET
;
495 dst4
->sin_len
= sizeof(struct sockaddr_in
);
496 bcopy(&ip
->ip_dst
, &dst4
->sin_addr
, sizeof(dst4
->sin_addr
));
497 if (sc
->sc_ro
.ro_rt
) {
498 rtfree(sc
->sc_ro
.ro_rt
);
499 sc
->sc_ro
.ro_rt
= NULL
;
503 if (sc
->sc_ro
.ro_rt
== NULL
) {
505 if (sc
->sc_ro
.ro_rt
== NULL
) {
510 error
= ip_output(m
, NULL
, &sc
->sc_ro
, 0, NULL
);
519 stf_checkaddr4(sc
, in
, inifp
)
520 struct stf_softc
*sc
;
522 struct ifnet
*inifp
; /* incoming interface */
524 struct in_ifaddr
*ia4
;
527 * reject packets with the following address:
528 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
530 if (IN_MULTICAST(ntohl(in
->s_addr
)))
532 switch ((ntohl(in
->s_addr
) & 0xff000000) >> 24) {
533 case 0: case 127: case 255:
538 * reject packets with broadcast
540 lck_mtx_lock(rt_mtx
);
541 for (ia4
= TAILQ_FIRST(&in_ifaddrhead
);
543 ia4
= TAILQ_NEXT(ia4
, ia_link
))
545 if ((ia4
->ia_ifa
.ifa_ifp
->if_flags
& IFF_BROADCAST
) == 0)
547 if (in
->s_addr
== ia4
->ia_broadaddr
.sin_addr
.s_addr
) {
548 lck_mtx_unlock(rt_mtx
);
552 lck_mtx_unlock(rt_mtx
);
555 * perform ingress filter
557 if (sc
&& (sc
->sc_if
.if_flags
& IFF_LINK2
) == 0 && inifp
) {
558 struct sockaddr_in sin
;
561 bzero(&sin
, sizeof(sin
));
562 sin
.sin_family
= AF_INET
;
563 sin
.sin_len
= sizeof(struct sockaddr_in
);
565 rt
= rtalloc1((struct sockaddr
*)&sin
, 0, 0UL);
566 if (!rt
|| rt
->rt_ifp
!= inifp
) {
568 log(LOG_WARNING
, "%s: packet from 0x%x dropped "
569 "due to ingress filter\n", if_name(&sc
->sc_if
),
570 (u_int32_t
)ntohl(sin
.sin_addr
.s_addr
));
583 stf_checkaddr6(sc
, in6
, inifp
)
584 struct stf_softc
*sc
;
585 struct in6_addr
*in6
;
586 struct ifnet
*inifp
; /* incoming interface */
589 * check 6to4 addresses
591 if (IN6_IS_ADDR_6TO4(in6
))
592 return stf_checkaddr4(sc
, GET_V4(in6
), inifp
);
595 * reject anything that look suspicious. the test is implemented
596 * in ip6_input too, but we check here as well to
597 * (1) reject bad packets earlier, and
598 * (2) to be safe against future ip6_input change.
600 if (IN6_IS_ADDR_V4COMPAT(in6
) || IN6_IS_ADDR_V4MAPPED(in6
))
611 struct stf_softc
*sc
;
618 ip
= mtod(m
, struct ip
*);
622 if (proto
!= IPPROTO_IPV6
) {
627 ip
= mtod(m
, struct ip
*);
629 sc
= (struct stf_softc
*)encap_getarg(m
);
631 if (sc
== NULL
|| (sc
->sc_if
.if_flags
& IFF_UP
) == 0) {
639 * perform sanity check against outer src/dst.
640 * for source, perform ingress filter as well.
642 if (stf_checkaddr4(sc
, &ip
->ip_dst
, NULL
) < 0 ||
643 stf_checkaddr4(sc
, &ip
->ip_src
, m
->m_pkthdr
.rcvif
) < 0) {
651 if (m
->m_len
< sizeof(*ip6
)) {
652 m
= m_pullup(m
, sizeof(*ip6
));
656 ip6
= mtod(m
, struct ip6_hdr
*);
659 * perform sanity check against inner src/dst.
660 * for source, perform ingress filter as well.
662 if (stf_checkaddr6(sc
, &ip6
->ip6_dst
, NULL
) < 0 ||
663 stf_checkaddr6(sc
, &ip6
->ip6_src
, m
->m_pkthdr
.rcvif
) < 0) {
668 itos
= (ntohl(ip6
->ip6_flow
) >> 20) & 0xff;
669 if ((ifp
->if_flags
& IFF_LINK1
) != 0)
670 ip_ecn_egress(ECN_ALLOWED
, &otos
, &itos
);
672 ip_ecn_egress(ECN_NOCARE
, &otos
, &itos
);
673 ip6
->ip6_flow
&= ~htonl(0xff << 20);
674 ip6
->ip6_flow
|= htonl((u_int32_t
)itos
<< 20);
676 m
->m_pkthdr
.rcvif
= ifp
;
680 * We need to prepend the address family as
681 * a four byte field. Cons up a dummy header
682 * to pacify bpf. This is safe because bpf
683 * will only read from the mbuf (i.e., it won't
684 * try to free it or keep a pointer a to it).
687 u_int32_t af
= AF_INET6
;
691 m0
.m_data
= (char *)&af
;
696 bpf_mtap(ifp
->if_bpf
, &m0
);
701 * Put the packet to the network layer input queue according to the
702 * specified address family.
703 * See net/if_gif.c for possible issues with packet processing
704 * reorder due to extra queueing.
706 proto_input(PF_INET6
, m
);
708 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
715 stf_rtrequest(cmd
, rt
, sa
)
722 rt
->rt_rmx
.rmx_mtu
= IPV6_MMTU
;
726 stf_ioctl(ifp
, cmd
, data
)
733 struct sockaddr_in6
*sin6
;
739 ifa
= (struct ifaddr
*)data
;
740 if (ifa
== NULL
|| ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
741 error
= EAFNOSUPPORT
;
744 sin6
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
745 if (IN6_IS_ADDR_6TO4(&sin6
->sin6_addr
)) {
746 if ( !(ifnet_flags( ifp
) & IFF_UP
) ) {
747 /* do this only if the interface is not already up */
748 ifa
->ifa_rtrequest
= stf_rtrequest
;
749 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
757 ifr
= (struct ifreq
*)data
;
758 if (ifr
&& ifr
->ifr_addr
.sa_family
== AF_INET6
)
761 error
= EAFNOSUPPORT
;