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>
86 #include <machine/cpu.h>
88 #include <sys/malloc.h>
91 #include <net/route.h>
92 #include <net/netisr.h>
93 #include <net/if_types.h>
94 #include <net/if_stf.h>
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/ip.h>
99 #include <netinet/ip_var.h>
100 #include <netinet/in_var.h>
102 #include <netinet/ip6.h>
103 #include <netinet6/ip6_var.h>
104 #include <netinet6/in6_var.h>
105 #include <netinet/ip_ecn.h>
107 #include <netinet/ip_encap.h>
108 #include <net/dlil.h>
111 #include <net/net_osdep.h>
115 #define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002)
116 #define GET_V4(x) ((struct in_addr *)(&(x)->s6_addr16[1]))
119 struct ifnet sc_if
; /* common area */
121 struct if_proto
*stf_proto
; /* dlil protocol attached */
124 struct route __sc_ro4
;
125 struct route_in6 __sc_ro6
; /* just for safety */
127 #define sc_ro __sc_ro46.__sc_ro4
128 const struct encaptab
*encap_cookie
;
131 static struct stf_softc
*stf
;
134 void stfattach
__P((void));
135 int stf_pre_output
__P((struct ifnet
*, register struct mbuf
**, struct sockaddr
*,
136 caddr_t
, char *, char *, u_long
));
137 static u_long stf_dl_tag
=0;
141 static MALLOC_DEFINE(M_STF
, "stf", "6to4 Tunnel Interface");
143 static int ip_stf_ttl
= 40;
145 extern struct domain inetdomain
;
146 struct protosw in_stf_protosw
=
147 { SOCK_RAW
, &inetdomain
, IPPROTO_IPV6
, PR_ATOMIC
|PR_ADDR
,
148 in_stf_input
, rip_output
, 0, rip_ctloutput
,
155 static int stf_encapcheck
__P((const struct mbuf
*, int, int, void *));
156 static struct in6_ifaddr
*stf_getsrcifa6
__P((struct ifnet
*));
157 int stf_pre_output
__P((struct ifnet
*, register struct mbuf
**, struct sockaddr
*,
158 caddr_t
, char *, char *, u_long
));
159 static int stf_checkaddr4
__P((struct stf_softc
*, struct in_addr
*,
161 static int stf_checkaddr6
__P((struct stf_softc
*, struct in6_addr
*,
163 static void stf_rtrequest
__P((int, struct rtentry
*, struct sockaddr
*));
164 int stf_ioctl
__P((struct ifnet
*, u_long
, void *));
168 int stf_add_if(struct ifnet
*ifp
)
176 int stf_del_if(struct ifnet
*ifp
)
182 int stf_add_proto(struct ddesc_head_str
*desc_head
, struct if_proto
*proto
, u_long dl_tag
)
184 /* Only one protocol may be attached at a time */
185 struct stf_softc
* stf
= (struct stf_softc
*)proto
->ifp
;
186 if (stf
->stf_proto
== NULL
)
187 stf
->stf_proto
= proto
;
189 printf("stf_add_proto: stf already has a proto\n");
197 int stf_del_proto(struct if_proto
*proto
, u_long dl_tag
)
199 if (((struct stf_softc
*)proto
->ifp
)->stf_proto
== proto
)
200 ((struct stf_softc
*)proto
->ifp
)->stf_proto
= NULL
;
212 void stf_reg_if_mods()
214 struct dlil_ifmod_reg_str stf_ifmod
;
216 bzero(&stf_ifmod
, sizeof(stf_ifmod
));
217 stf_ifmod
.add_if
= stf_add_if
;
218 stf_ifmod
.del_if
= stf_del_if
;
219 stf_ifmod
.add_proto
= stf_add_proto
;
220 stf_ifmod
.del_proto
= stf_del_proto
;
221 stf_ifmod
.ifmod_ioctl
= 0;
222 stf_ifmod
.shutdown
= stf_shutdown
;
225 if (dlil_reg_if_modules(APPLE_IF_FAM_STF
, &stf_ifmod
))
226 panic("Couldn't register stf modules\n");
230 u_long
stf_attach_inet6(struct ifnet
*ifp
)
232 struct dlil_proto_reg_str reg
;
233 struct dlil_demux_desc desc
;
240 TAILQ_INIT(®
.demux_desc_head
);
241 desc
.type
= DLIL_DESC_RAW
;
242 desc
.variants
.bitmask
.proto_id_length
= 0;
243 desc
.variants
.bitmask
.proto_id
= 0;
244 desc
.variants
.bitmask
.proto_id_mask
= 0;
245 desc
.native_type
= (char *) &native
;
246 TAILQ_INSERT_TAIL(®
.demux_desc_head
, &desc
, next
);
247 reg
.interface_family
= ifp
->if_family
;
248 reg
.unit_number
= ifp
->if_unit
;
250 reg
.pre_output
= stf_pre_output
;
254 reg
.default_proto
= 0;
255 reg
.protocol_family
= PF_INET6
;
257 stat
= dlil_attach_protocol(®
, &stf_dl_tag
);
259 panic("stf_attach_inet6 can't attach interface\n");
265 u_long
stf_detach_inet6(struct ifnet
*ifp
)
267 u_long ip_dl_tag
= 0;
270 stat
= dlil_find_dltag(ifp
->if_family
, ifp
->if_unit
, AF_INET6
, &ip_dl_tag
);
272 stat
= dlil_detach_protocol(ip_dl_tag
);
274 printf("WARNING: stf_detach can't detach IP AF_INET6 from interface\n");
285 struct stf_softc
*sc
;
290 const struct encaptab
*p
;
292 stf_reg_if_mods(); /* DLIL modules */
294 sc
= _MALLOC(sizeof(struct stf_softc
), M_DEVBUF
, M_WAITOK
);
296 printf("stf softc attach failed\n" );
300 bzero(sc
, sizeof(*sc
));
301 sc
->sc_if
.if_name
= "stf";
302 sc
->sc_if
.if_unit
= 0;
304 p
= encap_attach_func(AF_INET
, IPPROTO_IPV6
, stf_encapcheck
,
305 &in_stf_protosw
, sc
);
307 printf("%s: attach failed\n", if_name(&sc
->sc_if
));
311 sc
->encap_cookie
= p
;
312 sc
->sc_if
.if_mtu
= IPV6_MMTU
;
313 sc
->sc_if
.if_flags
= 0;
314 sc
->sc_if
.if_ioctl
= stf_ioctl
;
315 sc
->sc_if
.if_output
= NULL
; /* processing done in pre_output */
316 sc
->sc_if
.if_type
= IFT_STF
;
317 sc
->sc_if
.if_family
= APPLE_IF_FAM_STF
;
319 /* turn off ingress filter */
320 sc
->sc_if
.if_flags
|= IFF_LINK2
;
322 sc
->sc_if
.if_snd
.ifq_maxlen
= IFQ_MAXLEN
;
324 if (error
= dlil_if_attach(&sc
->sc_if
))
325 printf("stfattach: can't dlil_if_attach error=%d\n");
327 bpfattach(&sc
->sc_if
, DLT_NULL
, sizeof(u_int
));
333 stf_encapcheck(m
, off
, proto
, arg
)
334 const struct mbuf
*m
;
340 struct in6_ifaddr
*ia6
;
341 struct stf_softc
*sc
;
344 sc
= (struct stf_softc
*)arg
;
348 if ((sc
->sc_if
.if_flags
& IFF_UP
) == 0)
351 /* IFF_LINK0 means "no decapsulation" */
352 if ((sc
->sc_if
.if_flags
& IFF_LINK0
) != 0)
355 if (proto
!= IPPROTO_IPV6
)
358 /* LINTED const cast */
359 m_copydata((struct mbuf
*)m
, 0, sizeof(ip
), (caddr_t
)&ip
);
364 ia6
= stf_getsrcifa6(&sc
->sc_if
);
369 * check if IPv4 dst matches the IPv4 address derived from the
370 * local 6to4 address.
371 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
373 if (bcmp(GET_V4(&ia6
->ia_addr
.sin6_addr
), &ip
.ip_dst
,
374 sizeof(ip
.ip_dst
)) != 0)
378 * check if IPv4 src matches the IPv4 address derived from the
379 * local 6to4 address masked by prefixmask.
380 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
381 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
383 bzero(&a
, sizeof(a
));
384 a
.s_addr
= GET_V4(&ia6
->ia_addr
.sin6_addr
)->s_addr
;
385 a
.s_addr
&= GET_V4(&ia6
->ia_prefixmask
.sin6_addr
)->s_addr
;
387 b
.s_addr
&= GET_V4(&ia6
->ia_prefixmask
.sin6_addr
)->s_addr
;
388 if (a
.s_addr
!= b
.s_addr
)
391 /* stf interface makes single side match only */
395 static struct in6_ifaddr
*
400 struct in_ifaddr
*ia4
;
401 struct sockaddr_in6
*sin6
;
404 for (ia
= ifp
->if_addrlist
.tqh_first
;
406 ia
= ia
->ifa_list
.tqe_next
)
408 if (ia
->ifa_addr
== NULL
)
410 if (ia
->ifa_addr
->sa_family
!= AF_INET6
)
412 sin6
= (struct sockaddr_in6
*)ia
->ifa_addr
;
413 if (!IN6_IS_ADDR_6TO4(&sin6
->sin6_addr
))
416 bcopy(GET_V4(&sin6
->sin6_addr
), &in
, sizeof(in
));
417 for (ia4
= TAILQ_FIRST(&in_ifaddrhead
);
419 ia4
= TAILQ_NEXT(ia4
, ia_link
))
421 if (ia4
->ia_addr
.sin_addr
.s_addr
== in
.s_addr
)
427 return (struct in6_ifaddr
*)ia
;
434 stf_pre_output(ifp
, m0
, dst
, rt
, frame_type
, address
, dl_tag
)
436 register struct mbuf
**m0
;
437 struct sockaddr
*dst
;
443 register struct mbuf
*m
= *m0
;
444 struct stf_softc
*sc
;
445 struct sockaddr_in6
*dst6
;
447 struct sockaddr_in
*dst4
;
451 struct in6_ifaddr
*ia6
;
454 sc
= (struct stf_softc
*)ifp
;
455 dst6
= (struct sockaddr_in6
*)dst
;
458 if ((ifp
->if_flags
& IFF_UP
) == 0) {
459 printf("stf: IFF_DOWN\n");
464 * If we don't have an ip4 address that match my inner ip6 address,
465 * we shouldn't generate output. Without this check, we'll end up
466 * using wrong IPv4 source.
468 ia6
= stf_getsrcifa6(ifp
);
473 if (m
->m_len
< sizeof(*ip6
)) {
474 m
= m_pullup(m
, sizeof(*ip6
));
478 ip6
= mtod(m
, struct ip6_hdr
*);
479 tos
= (ntohl(ip6
->ip6_flow
) >> 20) & 0xff;
482 * Pickup the right outer dst addr from the list of candidates.
483 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
485 if (IN6_IS_ADDR_6TO4(&ip6
->ip6_dst
))
486 in4
= GET_V4(&ip6
->ip6_dst
);
487 else if (IN6_IS_ADDR_6TO4(&dst6
->sin6_addr
))
488 in4
= GET_V4(&dst6
->sin6_addr
);
495 * We need to prepend the address family as
496 * a four byte field. Cons up a dummy header
497 * to pacify bpf. This is safe because bpf
498 * will only read from the mbuf (i.e., it won't
499 * try to free it or keep a pointer a to it).
502 u_int32_t af
= AF_INET6
;
506 m0
.m_data
= (char *)&af
;
511 M_PREPEND(m
, sizeof(struct ip
), M_DONTWAIT
);
512 if (m
&& m
->m_len
< sizeof(struct ip
))
513 m
= m_pullup(m
, sizeof(struct ip
));
516 ip
= mtod(m
, struct ip
*);
518 bzero(ip
, sizeof(*ip
));
520 bcopy(GET_V4(&((struct sockaddr_in6
*)&ia6
->ia_addr
)->sin6_addr
),
521 &ip
->ip_src
, sizeof(ip
->ip_src
));
522 bcopy(in4
, &ip
->ip_dst
, sizeof(ip
->ip_dst
));
523 ip
->ip_p
= IPPROTO_IPV6
;
524 ip
->ip_ttl
= ip_stf_ttl
;
525 ip
->ip_len
= m
->m_pkthdr
.len
; /*host order*/
526 if (ifp
->if_flags
& IFF_LINK1
)
527 ip_ecn_ingress(ECN_ALLOWED
, &ip
->ip_tos
, &tos
);
529 ip_ecn_ingress(ECN_NOCARE
, &ip
->ip_tos
, &tos
);
531 dst4
= (struct sockaddr_in
*)&sc
->sc_ro
.ro_dst
;
532 if (dst4
->sin_family
!= AF_INET
||
533 bcmp(&dst4
->sin_addr
, &ip
->ip_dst
, sizeof(ip
->ip_dst
)) != 0) {
534 /* cache route doesn't match */
535 dst4
->sin_family
= AF_INET
;
536 dst4
->sin_len
= sizeof(struct sockaddr_in
);
537 bcopy(&ip
->ip_dst
, &dst4
->sin_addr
, sizeof(dst4
->sin_addr
));
538 if (sc
->sc_ro
.ro_rt
) {
539 RTFREE(sc
->sc_ro
.ro_rt
);
540 sc
->sc_ro
.ro_rt
= NULL
;
544 if (sc
->sc_ro
.ro_rt
== NULL
) {
546 if (sc
->sc_ro
.ro_rt
== NULL
) {
551 error
= ip_output(m
, NULL
, &sc
->sc_ro
, 0, NULL
);
557 stf_checkaddr4(sc
, in
, inifp
)
558 struct stf_softc
*sc
;
560 struct ifnet
*inifp
; /* incoming interface */
562 struct in_ifaddr
*ia4
;
565 * reject packets with the following address:
566 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
568 if (IN_MULTICAST(ntohl(in
->s_addr
)))
570 switch ((ntohl(in
->s_addr
) & 0xff000000) >> 24) {
571 case 0: case 127: case 255:
576 * reject packets with broadcast
578 for (ia4
= TAILQ_FIRST(&in_ifaddrhead
);
580 ia4
= TAILQ_NEXT(ia4
, ia_link
))
582 if ((ia4
->ia_ifa
.ifa_ifp
->if_flags
& IFF_BROADCAST
) == 0)
584 if (in
->s_addr
== ia4
->ia_broadaddr
.sin_addr
.s_addr
)
589 * perform ingress filter
591 if (sc
&& (sc
->sc_if
.if_flags
& IFF_LINK2
) == 0 && inifp
) {
592 struct sockaddr_in sin
;
595 bzero(&sin
, sizeof(sin
));
596 sin
.sin_family
= AF_INET
;
597 sin
.sin_len
= sizeof(struct sockaddr_in
);
599 rt
= rtalloc1((struct sockaddr
*)&sin
, 0, 0UL);
600 if (!rt
|| rt
->rt_ifp
!= inifp
) {
602 log(LOG_WARNING
, "%s: packet from 0x%x dropped "
603 "due to ingress filter\n", if_name(&sc
->sc_if
),
604 (u_int32_t
)ntohl(sin
.sin_addr
.s_addr
));
617 stf_checkaddr6(sc
, in6
, inifp
)
618 struct stf_softc
*sc
;
619 struct in6_addr
*in6
;
620 struct ifnet
*inifp
; /* incoming interface */
623 * check 6to4 addresses
625 if (IN6_IS_ADDR_6TO4(in6
))
626 return stf_checkaddr4(sc
, GET_V4(in6
), 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
))
645 struct stf_softc
*sc
;
650 struct ifqueue
*ifq
= NULL
;
653 ip
= mtod(m
, struct ip
*);
657 if (proto
!= IPPROTO_IPV6
) {
662 ip
= mtod(m
, struct ip
*);
664 sc
= (struct stf_softc
*)encap_getarg(m
);
666 if (sc
== NULL
|| (sc
->sc_if
.if_flags
& IFF_UP
) == 0) {
674 * perform sanity check against outer src/dst.
675 * for source, perform ingress filter as well.
677 if (stf_checkaddr4(sc
, &ip
->ip_dst
, NULL
) < 0 ||
678 stf_checkaddr4(sc
, &ip
->ip_src
, m
->m_pkthdr
.rcvif
) < 0) {
686 if (m
->m_len
< sizeof(*ip6
)) {
687 m
= m_pullup(m
, sizeof(*ip6
));
691 ip6
= mtod(m
, struct ip6_hdr
*);
694 * perform sanity check against inner src/dst.
695 * for source, perform ingress filter as well.
697 if (stf_checkaddr6(sc
, &ip6
->ip6_dst
, NULL
) < 0 ||
698 stf_checkaddr6(sc
, &ip6
->ip6_src
, m
->m_pkthdr
.rcvif
) < 0) {
703 itos
= (ntohl(ip6
->ip6_flow
) >> 20) & 0xff;
704 if ((ifp
->if_flags
& IFF_LINK1
) != 0)
705 ip_ecn_egress(ECN_ALLOWED
, &otos
, &itos
);
707 ip_ecn_egress(ECN_NOCARE
, &otos
, &itos
);
708 ip6
->ip6_flow
&= ~htonl(0xff << 20);
709 ip6
->ip6_flow
|= htonl((u_int32_t
)itos
<< 20);
711 m
->m_pkthdr
.rcvif
= ifp
;
715 * We need to prepend the address family as
716 * a four byte field. Cons up a dummy header
717 * to pacify bpf. This is safe because bpf
718 * will only read from the mbuf (i.e., it won't
719 * try to free it or keep a pointer a to it).
722 u_int32_t af
= AF_INET6
;
726 m0
.m_data
= (char *)&af
;
731 bpf_mtap(ifp
->if_bpf
, &m0
);
736 * Put the packet to the network layer input queue according to the
737 * specified address family.
738 * See net/if_gif.c for possible issues with packet processing
739 * reorder due to extra queueing.
746 IF_DROP(ifq
); /* update statistics */
754 ifp
->if_ibytes
+= m
->m_pkthdr
.len
;
760 stf_rtrequest(cmd
, rt
, sa
)
767 rt
->rt_rmx
.rmx_mtu
= IPV6_MMTU
;
771 stf_ioctl(ifp
, cmd
, data
)
778 struct sockaddr_in6
*sin6
;
784 ifa
= (struct ifaddr
*)data
;
785 if (ifa
== NULL
|| ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
786 error
= EAFNOSUPPORT
;
789 sin6
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
790 if (IN6_IS_ADDR_6TO4(&sin6
->sin6_addr
)) {
791 ifa
->ifa_rtrequest
= stf_rtrequest
;
792 ifp
->if_flags
|= IFF_UP
;
799 ifr
= (struct ifreq
*)data
;
800 if (ifr
&& ifr
->ifr_addr
.sa_family
== AF_INET6
)
803 error
= EAFNOSUPPORT
;