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
33 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
34 * support for mandatory and extensible security protections. This notice
35 * is included in support of clause 2.2 (b) of the Apple Public License,
40 * 6to4 interface, based on RFC3056.
42 * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
43 * There is no address mapping defined from IPv6 multicast address to IPv4
44 * address. Therefore, we do not have IFF_MULTICAST on the interface.
46 * Due to the lack of address mapping for link-local addresses, we cannot
47 * throw packets toward link-local addresses (fe80::x). Also, we cannot throw
48 * packets to link-local multicast addresses (ff02::x).
50 * Here are interesting symptoms due to the lack of link-local address:
52 * Unicast routing exchange:
53 * - RIPng: Impossible. Uses link-local multicast packet toward ff02::9,
54 * and link-local addresses as nexthop.
55 * - OSPFv6: Impossible. OSPFv6 assumes that there's link-local address
56 * assigned to the link, and makes use of them. Also, HELLO packets use
57 * link-local multicast addresses (ff02::5 and ff02::6).
58 * - BGP4+: Maybe. You can only use global address as nexthop, and global
59 * address as TCP endpoint address.
61 * Multicast routing protocols:
62 * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
63 * Adjacent PIM routers must be configured manually (is it really spec-wise
64 * correct thing to do?).
67 * - Redirects cannot be used due to the lack of link-local address.
69 * stf interface does not have, and will not need, a link-local address.
70 * It seems to have no real benefit and does not help the above symptoms much.
71 * Even if we assign link-locals to interface, we cannot really
72 * use link-local unicast/multicast on top of 6to4 cloud (since there's no
73 * encapsulation defined for link-local address), and the above analysis does
74 * not change. RFC3056 does not mandate the assignment of link-local address
77 * 6to4 interface has security issues. Refer to
78 * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
79 * for details. The code tries to filter out some of malicious packets.
80 * Note that there is no way to be 100% secure.
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/socket.h>
86 #include <sys/sockio.h>
88 #include <sys/errno.h>
89 #include <sys/protosw.h>
90 #include <sys/kernel.h>
91 #include <sys/syslog.h>
93 #include <sys/malloc.h>
96 #include <net/route.h>
97 #include <net/if_types.h>
99 #include <netinet/in.h>
100 #include <netinet/in_systm.h>
101 #include <netinet/ip.h>
102 #include <netinet/ip_var.h>
103 #include <netinet/in_var.h>
105 #include <netinet/ip6.h>
106 #include <netinet6/ip6_var.h>
107 #include <netinet6/in6_var.h>
108 #include <netinet/ip_ecn.h>
110 #include <netinet/ip_encap.h>
111 #include <net/kpi_interface.h>
112 #include <net/kpi_protocol.h>
115 #include <net/net_osdep.h>
120 #include <security/mac_framework.h>
123 #define IN6_IS_ADDR_6TO4(x) (ntohs((x)->s6_addr16[0]) == 0x2002)
124 #define GET_V4(x) ((const struct in_addr *)(&(x)->s6_addr16[1]))
127 ifnet_t sc_if
; /* common area */
128 u_long sc_protocol_family
; /* dlil protocol attached */
130 struct route __sc_ro4
;
131 struct route_in6 __sc_ro6
; /* just for safety */
133 #define sc_ro __sc_ro46.__sc_ro4
134 const struct encaptab
*encap_cookie
;
135 bpf_tap_mode tap_mode
;
136 bpf_packet_func tap_callback
;
139 void stfattach (void);
141 static int ip_stf_ttl
= 40;
143 static void in_stf_input(struct mbuf
*, int);
144 extern struct domain inetdomain
;
145 struct protosw in_stf_protosw
=
146 { SOCK_RAW
, &inetdomain
, IPPROTO_IPV6
, PR_ATOMIC
|PR_ADDR
,
147 in_stf_input
, NULL
, NULL
, rip_ctloutput
,
149 NULL
, NULL
, NULL
, NULL
,
152 NULL
, rip_unlock
, NULL
, {NULL
, NULL
}, NULL
, {0}
155 static int stf_encapcheck(const struct mbuf
*, int, int, void *);
156 static struct in6_ifaddr
*stf_getsrcifa6(struct ifnet
*);
157 int stf_pre_output(struct ifnet
*, protocol_family_t
, struct mbuf
**,
158 const struct sockaddr
*, void *, char *, char *);
159 static int stf_checkaddr4(struct stf_softc
*, const struct in_addr
*,
161 static int stf_checkaddr6(struct stf_softc
*, struct in6_addr
*,
163 static void stf_rtrequest(int, struct rtentry
*, struct sockaddr
*);
164 static errno_t
stf_ioctl(ifnet_t ifp
, u_int32_t cmd
, void *data
);
165 static errno_t
stf_output(ifnet_t ifp
, mbuf_t m
);
168 * gif_input is the input handler for IP and IPv6 attached to gif
172 __unused ifnet_t ifp
,
173 protocol_family_t protocol_family
,
175 __unused
char *frame_header
)
177 proto_input(protocol_family
, m
);
187 protocol_family_t protocol_family
,
188 __unused
const struct ifnet_demux_desc
*demux_array
,
189 __unused u_int32_t demux_count
)
191 /* Only one protocol may be attached at a time */
192 struct stf_softc
* stf
= ifnet_softc(ifp
);
193 if (stf
->sc_protocol_family
== 0)
194 stf
->sc_protocol_family
= protocol_family
;
196 printf("stf_add_proto: stf already has a proto\n");
206 protocol_family_t protocol_family
)
208 if (((struct stf_softc
*)ifnet_softc(ifp
))->sc_protocol_family
== protocol_family
)
209 ((struct stf_softc
*)ifnet_softc(ifp
))->sc_protocol_family
= 0;
217 protocol_family_t protocol_family
)
219 struct ifnet_attach_proto_param reg
;
222 if (protocol_family
!= PF_INET6
)
223 return EPROTONOSUPPORT
;
225 bzero(®
, sizeof(reg
));
226 reg
.input
= stf_media_input
;
227 reg
.pre_output
= stf_pre_output
;
229 stat
= ifnet_attach_protocol(ifp
, protocol_family
, ®
);
230 if (stat
&& stat
!= EEXIST
) {
231 printf("stf_attach_proto_family can't attach interface fam=%d\n",
242 __unused
char *frame_ptr
,
243 protocol_family_t
*protocol_family
)
245 struct stf_softc
* stf
= ifnet_softc(ifp
);
246 *protocol_family
= stf
->sc_protocol_family
;
254 bpf_packet_func callback
)
256 struct stf_softc
*sc
= ifnet_softc(ifp
);
259 sc
->tap_callback
= callback
;
267 struct stf_softc
*sc
;
269 const struct encaptab
*p
;
270 struct ifnet_init_params stf_init
;
272 error
= proto_register_plumber(PF_INET6
, APPLE_IF_FAM_STF
,
273 stf_attach_inet6
, NULL
);
275 printf("proto_register_plumber failed for AF_INET6 error=%d\n", error
);
277 sc
= _MALLOC(sizeof(struct stf_softc
), M_DEVBUF
, M_WAITOK
);
279 printf("stf softc attach failed\n" );
283 bzero(sc
, sizeof(*sc
));
285 p
= encap_attach_func(AF_INET
, IPPROTO_IPV6
, stf_encapcheck
,
286 &in_stf_protosw
, sc
);
288 printf("sftattach encap_attach_func failed\n");
292 sc
->encap_cookie
= p
;
294 bzero(&stf_init
, sizeof(stf_init
));
295 stf_init
.name
= "stf";
297 stf_init
.type
= IFT_STF
;
298 stf_init
.family
= IFNET_FAMILY_STF
;
299 stf_init
.output
= stf_output
;
300 stf_init
.demux
= stf_demux
;
301 stf_init
.add_proto
= stf_add_proto
;
302 stf_init
.del_proto
= stf_del_proto
;
304 stf_init
.ioctl
= stf_ioctl
;
305 stf_init
.set_bpf_tap
= stf_set_bpf_tap
;
307 error
= ifnet_allocate(&stf_init
, &sc
->sc_if
);
309 printf("stfattach, ifnet_allocate failed - %d\n", error
);
310 encap_detach(sc
->encap_cookie
);
314 ifnet_set_mtu(sc
->sc_if
, IPV6_MMTU
);
315 ifnet_set_flags(sc
->sc_if
, 0, 0xffff); /* clear all flags */
317 /* turn off ingress filter */
318 ifnet_set_flags(sc
->sc_if
, IFF_LINK2
, IFF_LINK2
);
322 mac_ifnet_label_init(&sc
->sc_if
);
325 error
= ifnet_attach(sc
->sc_if
, NULL
);
327 printf("stfattach: ifnet_attach returned error=%d\n", error
);
328 encap_detach(sc
->encap_cookie
);
329 ifnet_release(sc
->sc_if
);
334 bpfattach(sc
->sc_if
, DLT_NULL
, sizeof(u_int
));
341 const struct mbuf
*m
,
347 struct in6_ifaddr
*ia6
;
348 struct stf_softc
*sc
;
351 sc
= (struct stf_softc
*)arg
;
355 if ((ifnet_flags(sc
->sc_if
) & IFF_UP
) == 0)
358 /* IFF_LINK0 means "no decapsulation" */
359 if ((ifnet_flags(sc
->sc_if
) & IFF_LINK0
) != 0)
362 if (proto
!= IPPROTO_IPV6
)
365 /* LINTED const cast */
366 mbuf_copydata(m
, 0, sizeof(ip
), &ip
);
371 ia6
= stf_getsrcifa6(sc
->sc_if
);
376 * check if IPv4 dst matches the IPv4 address derived from the
377 * local 6to4 address.
378 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
380 if (bcmp(GET_V4(&ia6
->ia_addr
.sin6_addr
), &ip
.ip_dst
,
381 sizeof(ip
.ip_dst
)) != 0)
385 * check if IPv4 src matches the IPv4 address derived from the
386 * local 6to4 address masked by prefixmask.
387 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
388 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
390 bzero(&a
, sizeof(a
));
391 a
.s_addr
= GET_V4(&ia6
->ia_addr
.sin6_addr
)->s_addr
;
392 a
.s_addr
&= GET_V4(&ia6
->ia_prefixmask
.sin6_addr
)->s_addr
;
394 b
.s_addr
&= GET_V4(&ia6
->ia_prefixmask
.sin6_addr
)->s_addr
;
395 if (a
.s_addr
!= b
.s_addr
)
398 /* stf interface makes single side match only */
402 static struct in6_ifaddr
*
403 stf_getsrcifa6(struct ifnet
*ifp
)
406 struct in_ifaddr
*ia4
;
407 struct sockaddr_in6
*sin6
;
410 ifnet_lock_shared(ifp
);
411 for (ia
= ifp
->if_addrlist
.tqh_first
;
413 ia
= ia
->ifa_list
.tqe_next
)
415 if (ia
->ifa_addr
== NULL
)
417 if (ia
->ifa_addr
->sa_family
!= AF_INET6
)
419 sin6
= (struct sockaddr_in6
*)ia
->ifa_addr
;
420 if (!IN6_IS_ADDR_6TO4(&sin6
->sin6_addr
))
423 bcopy(GET_V4(&sin6
->sin6_addr
), &in
, sizeof(in
));
424 lck_mtx_lock(rt_mtx
);
425 for (ia4
= TAILQ_FIRST(&in_ifaddrhead
);
427 ia4
= TAILQ_NEXT(ia4
, ia_link
))
429 if (ia4
->ia_addr
.sin_addr
.s_addr
== in
.s_addr
)
432 lck_mtx_unlock(rt_mtx
);
436 ifnet_lock_done(ifp
);
437 return (struct in6_ifaddr
*)ia
;
439 ifnet_lock_done(ifp
);
447 __unused protocol_family_t protocol_family
,
449 const struct sockaddr
*dst
,
450 __unused
void *route
,
451 __unused
char *desk_linkaddr
,
452 __unused
char *frame_type
)
454 struct mbuf
*m
= *m0
;
455 struct stf_softc
*sc
;
456 const struct sockaddr_in6
*dst6
;
457 const struct in_addr
*in4
;
461 struct in6_ifaddr
*ia6
;
462 struct sockaddr_in
*dst4
;
465 sc
= ifnet_softc(ifp
);
466 dst6
= (const struct sockaddr_in6
*)dst
;
469 if ((ifnet_flags(ifp
) & IFF_UP
) == 0) {
470 printf("stf: IFF_DOWN\n");
475 * If we don't have an ip4 address that match my inner ip6 address,
476 * we shouldn't generate output. Without this check, we'll end up
477 * using wrong IPv4 source.
479 ia6
= stf_getsrcifa6(ifp
);
484 if (mbuf_len(m
) < sizeof(*ip6
)) {
485 m
= m_pullup(m
, sizeof(*ip6
));
487 *m0
= NULL
; /* makes sure this won't be double freed */
491 ip6
= mtod(m
, struct ip6_hdr
*);
492 tos
= (ntohl(ip6
->ip6_flow
) >> 20) & 0xff;
495 * Pickup the right outer dst addr from the list of candidates.
496 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
498 if (IN6_IS_ADDR_6TO4(&ip6
->ip6_dst
))
499 in4
= GET_V4(&ip6
->ip6_dst
);
500 else if (IN6_IS_ADDR_6TO4(&dst6
->sin6_addr
))
501 in4
= GET_V4(&dst6
->sin6_addr
);
507 /* We need to prepend the address family as a four byte field. */
508 u_int32_t af
= AF_INET6
;
510 bpf_tap_out(ifp
, 0, m
, &af
, sizeof(af
));
513 M_PREPEND(m
, sizeof(struct ip
), M_DONTWAIT
);
514 if (m
&& mbuf_len(m
) < sizeof(struct ip
))
515 m
= m_pullup(m
, sizeof(struct ip
));
520 ip
= mtod(m
, struct ip
*);
522 bzero(ip
, sizeof(*ip
));
524 bcopy(GET_V4(&((struct sockaddr_in6
*)&ia6
->ia_addr
)->sin6_addr
),
525 &ip
->ip_src
, sizeof(ip
->ip_src
));
526 bcopy(in4
, &ip
->ip_dst
, sizeof(ip
->ip_dst
));
527 ip
->ip_p
= IPPROTO_IPV6
;
528 ip
->ip_ttl
= ip_stf_ttl
;
529 ip
->ip_len
= m
->m_pkthdr
.len
; /*host order*/
530 if (ifp
->if_flags
& IFF_LINK1
)
531 ip_ecn_ingress(ECN_ALLOWED
, &ip
->ip_tos
, &tos
);
533 ip_ecn_ingress(ECN_NOCARE
, &ip
->ip_tos
, &tos
);
535 dst4
= (struct sockaddr_in
*)&sc
->sc_ro
.ro_dst
;
536 if (dst4
->sin_family
!= AF_INET
||
537 bcmp(&dst4
->sin_addr
, &ip
->ip_dst
, sizeof(ip
->ip_dst
)) != 0) {
538 /* cache route doesn't match */
539 printf("stf_output: cached route doesn't match \n");
540 dst4
->sin_family
= AF_INET
;
541 dst4
->sin_len
= sizeof(struct sockaddr_in
);
542 bcopy(&ip
->ip_dst
, &dst4
->sin_addr
, sizeof(dst4
->sin_addr
));
543 if (sc
->sc_ro
.ro_rt
) {
544 rtfree(sc
->sc_ro
.ro_rt
);
545 sc
->sc_ro
.ro_rt
= NULL
;
549 if (sc
->sc_ro
.ro_rt
== NULL
) {
551 if (sc
->sc_ro
.ro_rt
== NULL
) {
556 result
= ip_output_list(m
, 0, NULL
, &sc
->sc_ro
, 0, NULL
, NULL
);
557 /* Assumption: ip_output will free mbuf on errors */
558 /* All the output processing is done here, don't let stf_output be called */
560 result
= EJUSTRETURN
;
566 __unused ifnet_t ifp
,
569 /* All processing is done in stf_pre_output
570 * this shouldn't be called as the pre_output returns "EJUSTRETURN"
577 struct stf_softc
*sc
,
578 const struct in_addr
*in
,
579 struct ifnet
*inifp
) /* incoming interface */
581 struct in_ifaddr
*ia4
;
584 * reject packets with the following address:
585 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
587 if (IN_MULTICAST(ntohl(in
->s_addr
)))
589 switch ((ntohl(in
->s_addr
) & 0xff000000) >> 24) {
590 case 0: case 127: case 255:
595 * reject packets with broadcast
597 lck_mtx_lock(rt_mtx
);
598 for (ia4
= TAILQ_FIRST(&in_ifaddrhead
);
600 ia4
= TAILQ_NEXT(ia4
, ia_link
))
602 if ((ia4
->ia_ifa
.ifa_ifp
->if_flags
& IFF_BROADCAST
) == 0)
604 if (in
->s_addr
== ia4
->ia_broadaddr
.sin_addr
.s_addr
) {
605 lck_mtx_unlock(rt_mtx
);
609 lck_mtx_unlock(rt_mtx
);
612 * perform ingress filter
614 if (sc
&& (ifnet_flags(sc
->sc_if
) & IFF_LINK2
) == 0 && inifp
) {
615 struct sockaddr_in sin
;
618 bzero(&sin
, sizeof(sin
));
619 sin
.sin_family
= AF_INET
;
620 sin
.sin_len
= sizeof(struct sockaddr_in
);
622 rt
= rtalloc1((struct sockaddr
*)&sin
, 0, 0UL);
623 if (!rt
|| rt
->rt_ifp
!= inifp
) {
625 log(LOG_WARNING
, "%s: packet from 0x%x dropped "
626 "due to ingress filter\n", if_name(sc
->sc_if
),
627 (u_int32_t
)ntohl(sin
.sin_addr
.s_addr
));
641 struct stf_softc
*sc
,
642 struct in6_addr
*in6
,
643 struct ifnet
*inifp
) /* incoming interface */
646 * check 6to4 addresses
648 if (IN6_IS_ADDR_6TO4(in6
))
649 return stf_checkaddr4(sc
, GET_V4(in6
), inifp
);
652 * reject anything that look suspicious. the test is implemented
653 * in ip6_input too, but we check here as well to
654 * (1) reject bad packets earlier, and
655 * (2) to be safe against future ip6_input change.
657 if (IN6_IS_ADDR_V4COMPAT(in6
) || IN6_IS_ADDR_V4MAPPED(in6
))
668 struct stf_softc
*sc
;
674 struct ifnet_stat_increment_param stats
;
676 ip
= mtod(m
, struct ip
*);
679 if (proto
!= IPPROTO_IPV6
) {
684 ip
= mtod(m
, struct ip
*);
686 sc
= (struct stf_softc
*)encap_getarg(m
);
688 if (sc
== NULL
|| (ifnet_flags(sc
->sc_if
) & IFF_UP
) == 0) {
696 mac_mbuf_label_associate_ifnet(ifp
, m
);
700 * perform sanity check against outer src/dst.
701 * for source, perform ingress filter as well.
703 if (stf_checkaddr4(sc
, &ip
->ip_dst
, NULL
) < 0 ||
704 stf_checkaddr4(sc
, &ip
->ip_src
, m
->m_pkthdr
.rcvif
) < 0) {
710 mbuf_copydata(m
, off
, sizeof(ip6
), &ip6
);
713 * perform sanity check against inner src/dst.
714 * for source, perform ingress filter as well.
716 if (stf_checkaddr6(sc
, &ip6
.ip6_dst
, NULL
) < 0 ||
717 stf_checkaddr6(sc
, &ip6
.ip6_src
, m
->m_pkthdr
.rcvif
) < 0) {
722 itos
= (ntohl(ip6
.ip6_flow
) >> 20) & 0xff;
723 if ((ifnet_flags(ifp
) & IFF_LINK1
) != 0)
724 ip_ecn_egress(ECN_ALLOWED
, &otos
, &itos
);
726 ip_ecn_egress(ECN_NOCARE
, &otos
, &itos
);
727 ip6
.ip6_flow
&= ~htonl(0xff << 20);
728 ip6
.ip6_flow
|= htonl((u_int32_t
)itos
<< 20);
730 m
->m_pkthdr
.rcvif
= ifp
;
731 mbuf_pkthdr_setheader(m
, mbuf_data(m
));
735 /* We need to prepend the address family as a four byte field. */
736 u_int32_t af
= AF_INET6
;
737 bpf_tap_in(ifp
, 0, m
, &af
, sizeof(af
));
741 * Put the packet to the network layer input queue according to the
742 * specified address family.
743 * See net/if_gif.c for possible issues with packet processing
744 * reorder due to extra queueing.
746 bzero(&stats
, sizeof(stats
));
747 stats
.packets_in
= 1;
748 stats
.bytes_in
= mbuf_pkthdr_len(m
);
749 mbuf_pkthdr_setrcvif(m
, ifp
);
750 ifnet_input(ifp
, m
, &stats
);
759 __unused
struct sockaddr
*sa
)
763 rt
->rt_rmx
.rmx_mtu
= IPV6_MMTU
;
774 struct sockaddr_in6
*sin6
;
780 ifa
= (struct ifaddr
*)data
;
781 if (ifa
== NULL
|| ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
782 error
= EAFNOSUPPORT
;
785 sin6
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
786 if (IN6_IS_ADDR_6TO4(&sin6
->sin6_addr
)) {
787 if ( !(ifnet_flags( ifp
) & IFF_UP
) ) {
788 /* do this only if the interface is not already up */
789 ifa
->ifa_rtrequest
= stf_rtrequest
;
790 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
798 ifr
= (struct ifreq
*)data
;
799 if (ifr
&& ifr
->ifr_addr
.sa_family
== AF_INET6
)
802 error
= EAFNOSUPPORT
;