2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $FreeBSD: src/sys/net/if_stf.c,v 1.1.2.6 2001/07/24 19:10:18 brooks Exp $ */
30 /* $KAME: if_stf.c,v 1.62 2001/06/07 22:32:16 itojun Exp $ */
33 * Copyright (C) 2000 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
62 * support for mandatory and extensible security protections. This notice
63 * is included in support of clause 2.2 (b) of the Apple Public License,
68 * 6to4 interface, based on RFC3056.
70 * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
71 * There is no address mapping defined from IPv6 multicast address to IPv4
72 * address. Therefore, we do not have IFF_MULTICAST on the interface.
74 * Due to the lack of address mapping for link-local addresses, we cannot
75 * throw packets toward link-local addresses (fe80::x). Also, we cannot throw
76 * packets to link-local multicast addresses (ff02::x).
78 * Here are interesting symptoms due to the lack of link-local address:
80 * Unicast routing exchange:
81 * - RIPng: Impossible. Uses link-local multicast packet toward ff02::9,
82 * and link-local addresses as nexthop.
83 * - OSPFv6: Impossible. OSPFv6 assumes that there's link-local address
84 * assigned to the link, and makes use of them. Also, HELLO packets use
85 * link-local multicast addresses (ff02::5 and ff02::6).
86 * - BGP4+: Maybe. You can only use global address as nexthop, and global
87 * address as TCP endpoint address.
89 * Multicast routing protocols:
90 * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
91 * Adjacent PIM routers must be configured manually (is it really spec-wise
92 * correct thing to do?).
95 * - Redirects cannot be used due to the lack of link-local address.
97 * stf interface does not have, and will not need, a link-local address.
98 * It seems to have no real benefit and does not help the above symptoms much.
99 * Even if we assign link-locals to interface, we cannot really
100 * use link-local unicast/multicast on top of 6to4 cloud (since there's no
101 * encapsulation defined for link-local address), and the above analysis does
102 * not change. RFC3056 does not mandate the assignment of link-local address
105 * 6to4 interface has security issues. Refer to
106 * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
107 * for details. The code tries to filter out some of malicious packets.
108 * Note that there is no way to be 100% secure.
111 #include <sys/param.h>
112 #include <sys/systm.h>
113 #include <sys/socket.h>
114 #include <sys/sockio.h>
115 #include <sys/mbuf.h>
116 #include <sys/errno.h>
117 #include <sys/protosw.h>
118 #include <sys/kernel.h>
119 #include <sys/syslog.h>
121 #include <sys/malloc.h>
123 #include <kern/locks.h>
126 #include <net/route.h>
127 #include <net/if_types.h>
129 #include <netinet/in.h>
130 #include <netinet/in_systm.h>
131 #include <netinet/ip.h>
132 #include <netinet/ip_var.h>
133 #include <netinet/in_var.h>
135 #include <netinet/ip6.h>
136 #include <netinet6/ip6_var.h>
137 #include <netinet6/in6_var.h>
138 #include <netinet/ip_ecn.h>
140 #include <netinet/ip_encap.h>
141 #include <net/kpi_interface.h>
142 #include <net/kpi_protocol.h>
145 #include <net/net_osdep.h>
150 #include <security/mac_framework.h>
153 #define GET_V4(x) ((const struct in_addr *)(const void *)(&(x)->s6_addr16[1]))
155 static lck_grp_t
*stf_mtx_grp
;
158 ifnet_t sc_if
; /* common area */
159 u_int32_t sc_protocol_family
; /* dlil protocol attached */
161 struct route __sc_ro4
;
162 struct route_in6 __sc_ro6
; /* just for safety */
164 #define sc_ro __sc_ro46.__sc_ro4
165 decl_lck_mtx_data(, sc_ro_mtx
);
166 const struct encaptab
*encap_cookie
;
167 bpf_tap_mode tap_mode
;
168 bpf_packet_func tap_callback
;
171 void stfattach (void);
173 static int ip_stf_ttl
= 40;
174 static int stf_init_done
;
176 static void in_stf_input(struct mbuf
*, int);
177 static void stfinit(void);
179 static struct protosw in_stf_protosw
=
182 .pr_protocol
= IPPROTO_IPV6
,
183 .pr_flags
= PR_ATOMIC
|PR_ADDR
,
184 .pr_input
= in_stf_input
,
185 .pr_ctloutput
= rip_ctloutput
,
186 .pr_usrreqs
= &rip_usrreqs
,
187 .pr_unlock
= rip_unlock
,
190 static int stf_encapcheck(const struct mbuf
*, int, int, void *);
191 static struct in6_ifaddr
*stf_getsrcifa6(struct ifnet
*);
192 int stf_pre_output(struct ifnet
*, protocol_family_t
, struct mbuf
**,
193 const struct sockaddr
*, void *, char *, char *);
194 static int stf_checkaddr4(struct stf_softc
*, const struct in_addr
*,
196 static int stf_checkaddr6(struct stf_softc
*, struct in6_addr
*,
198 static void stf_rtrequest(int, struct rtentry
*, struct sockaddr
*);
199 static errno_t
stf_ioctl(ifnet_t ifp
, u_long cmd
, void *data
);
200 static errno_t
stf_output(ifnet_t ifp
, mbuf_t m
);
205 if (!stf_init_done
) {
206 stf_mtx_grp
= lck_grp_alloc_init("stf", LCK_GRP_ATTR_NULL
);
212 * gif_input is the input handler for IP and IPv6 attached to gif
216 __unused ifnet_t ifp
,
217 protocol_family_t protocol_family
,
219 __unused
char *frame_header
)
221 if (proto_input(protocol_family
, m
) != 0)
232 protocol_family_t protocol_family
,
233 __unused
const struct ifnet_demux_desc
*demux_array
,
234 __unused u_int32_t demux_count
)
236 /* Only one protocol may be attached at a time */
237 struct stf_softc
* stf
= ifnet_softc(ifp
);
238 if (stf
->sc_protocol_family
== 0)
239 stf
->sc_protocol_family
= protocol_family
;
241 printf("stf_add_proto: stf already has a proto\n");
251 protocol_family_t protocol_family
)
253 if (((struct stf_softc
*)ifnet_softc(ifp
))->sc_protocol_family
== protocol_family
)
254 ((struct stf_softc
*)ifnet_softc(ifp
))->sc_protocol_family
= 0;
262 protocol_family_t protocol_family
)
264 struct ifnet_attach_proto_param reg
;
267 if (protocol_family
!= PF_INET6
)
268 return EPROTONOSUPPORT
;
270 bzero(®
, sizeof(reg
));
271 reg
.input
= stf_media_input
;
272 reg
.pre_output
= stf_pre_output
;
274 stat
= ifnet_attach_protocol(ifp
, protocol_family
, ®
);
275 if (stat
&& stat
!= EEXIST
) {
276 printf("stf_attach_proto_family can't attach interface fam=%d\n",
287 __unused
char *frame_ptr
,
288 protocol_family_t
*protocol_family
)
290 struct stf_softc
* stf
= ifnet_softc(ifp
);
291 *protocol_family
= stf
->sc_protocol_family
;
299 bpf_packet_func callback
)
301 struct stf_softc
*sc
= ifnet_softc(ifp
);
304 sc
->tap_callback
= callback
;
312 struct stf_softc
*sc
;
314 const struct encaptab
*p
;
315 struct ifnet_init_params stf_init
;
319 error
= proto_register_plumber(PF_INET6
, APPLE_IF_FAM_STF
,
320 stf_attach_inet6
, NULL
);
322 printf("proto_register_plumber failed for AF_INET6 error=%d\n", error
);
324 sc
= _MALLOC(sizeof(struct stf_softc
), M_DEVBUF
, M_WAITOK
);
326 printf("stf softc attach failed\n" );
330 bzero(sc
, sizeof(*sc
));
332 p
= encap_attach_func(AF_INET
, IPPROTO_IPV6
, stf_encapcheck
,
333 &in_stf_protosw
, sc
);
335 printf("sftattach encap_attach_func failed\n");
339 sc
->encap_cookie
= p
;
340 lck_mtx_init(&sc
->sc_ro_mtx
, stf_mtx_grp
, LCK_ATTR_NULL
);
342 bzero(&stf_init
, sizeof(stf_init
));
343 stf_init
.name
= "stf";
345 stf_init
.type
= IFT_STF
;
346 stf_init
.family
= IFNET_FAMILY_STF
;
347 stf_init
.output
= stf_output
;
348 stf_init
.demux
= stf_demux
;
349 stf_init
.add_proto
= stf_add_proto
;
350 stf_init
.del_proto
= stf_del_proto
;
352 stf_init
.ioctl
= stf_ioctl
;
353 stf_init
.set_bpf_tap
= stf_set_bpf_tap
;
355 error
= ifnet_allocate(&stf_init
, &sc
->sc_if
);
357 printf("stfattach, ifnet_allocate failed - %d\n", error
);
358 encap_detach(sc
->encap_cookie
);
359 lck_mtx_destroy(&sc
->sc_ro_mtx
, stf_mtx_grp
);
363 ifnet_set_mtu(sc
->sc_if
, IPV6_MMTU
);
364 ifnet_set_flags(sc
->sc_if
, 0, 0xffff); /* clear all flags */
366 /* turn off ingress filter */
367 ifnet_set_flags(sc
->sc_if
, IFF_LINK2
, IFF_LINK2
);
371 mac_ifnet_label_init(&sc
->sc_if
);
374 error
= ifnet_attach(sc
->sc_if
, NULL
);
376 printf("stfattach: ifnet_attach returned error=%d\n", error
);
377 encap_detach(sc
->encap_cookie
);
378 ifnet_release(sc
->sc_if
);
379 lck_mtx_destroy(&sc
->sc_ro_mtx
, stf_mtx_grp
);
384 bpfattach(sc
->sc_if
, DLT_NULL
, sizeof(u_int
));
391 const struct mbuf
*m
,
397 struct in6_ifaddr
*ia6
;
398 struct stf_softc
*sc
;
401 sc
= (struct stf_softc
*)arg
;
405 if ((ifnet_flags(sc
->sc_if
) & IFF_UP
) == 0)
408 /* IFF_LINK0 means "no decapsulation" */
409 if ((ifnet_flags(sc
->sc_if
) & IFF_LINK0
) != 0)
412 if (proto
!= IPPROTO_IPV6
)
415 mbuf_copydata((struct mbuf
*)(size_t)m
, 0, sizeof(ip
), &ip
);
420 ia6
= stf_getsrcifa6(sc
->sc_if
);
425 * check if IPv4 dst matches the IPv4 address derived from the
426 * local 6to4 address.
427 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
429 IFA_LOCK(&ia6
->ia_ifa
);
430 if (bcmp(GET_V4(&ia6
->ia_addr
.sin6_addr
), &ip
.ip_dst
,
431 sizeof(ip
.ip_dst
)) != 0) {
432 IFA_UNLOCK(&ia6
->ia_ifa
);
433 IFA_REMREF(&ia6
->ia_ifa
);
437 * check if IPv4 src matches the IPv4 address derived from the
438 * local 6to4 address masked by prefixmask.
439 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
440 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
442 bzero(&a
, sizeof(a
));
443 a
.s_addr
= GET_V4(&ia6
->ia_addr
.sin6_addr
)->s_addr
;
444 a
.s_addr
&= GET_V4(&ia6
->ia_prefixmask
.sin6_addr
)->s_addr
;
446 b
.s_addr
&= GET_V4(&ia6
->ia_prefixmask
.sin6_addr
)->s_addr
;
447 if (a
.s_addr
!= b
.s_addr
) {
448 IFA_UNLOCK(&ia6
->ia_ifa
);
449 IFA_REMREF(&ia6
->ia_ifa
);
452 /* stf interface makes single side match only */
453 IFA_UNLOCK(&ia6
->ia_ifa
);
454 IFA_REMREF(&ia6
->ia_ifa
);
458 static struct in6_ifaddr
*
459 stf_getsrcifa6(struct ifnet
*ifp
)
462 struct in_ifaddr
*ia4
;
463 struct sockaddr_in6
*sin6
;
466 ifnet_lock_shared(ifp
);
467 for (ia
= ifp
->if_addrlist
.tqh_first
; ia
; ia
= ia
->ifa_list
.tqe_next
) {
469 if (ia
->ifa_addr
== NULL
) {
473 if (ia
->ifa_addr
->sa_family
!= AF_INET6
) {
477 sin6
= (struct sockaddr_in6
*)(void *)ia
->ifa_addr
;
478 if (!IN6_IS_ADDR_6TO4(&sin6
->sin6_addr
)) {
482 bcopy(GET_V4(&sin6
->sin6_addr
), &in
, sizeof(in
));
484 lck_rw_lock_shared(in_ifaddr_rwlock
);
485 for (ia4
= TAILQ_FIRST(&in_ifaddrhead
);
487 ia4
= TAILQ_NEXT(ia4
, ia_link
))
489 IFA_LOCK(&ia4
->ia_ifa
);
490 if (ia4
->ia_addr
.sin_addr
.s_addr
== in
.s_addr
) {
491 IFA_UNLOCK(&ia4
->ia_ifa
);
494 IFA_UNLOCK(&ia4
->ia_ifa
);
496 lck_rw_done(in_ifaddr_rwlock
);
500 IFA_ADDREF(ia
); /* for caller */
501 ifnet_lock_done(ifp
);
502 return ((struct in6_ifaddr
*)ia
);
504 ifnet_lock_done(ifp
);
512 __unused protocol_family_t protocol_family
,
514 const struct sockaddr
*dst
,
515 __unused
void *route
,
516 __unused
char *desk_linkaddr
,
517 __unused
char *frame_type
)
519 struct mbuf
*m
= *m0
;
520 struct stf_softc
*sc
;
521 const struct sockaddr_in6
*dst6
;
522 const struct in_addr
*in4
;
526 struct in6_ifaddr
*ia6
;
527 struct sockaddr_in
*dst4
;
528 struct ip_out_args ipoa
=
529 { IFSCOPE_NONE
, { 0 }, IPOAF_SELECT_SRCIF
, 0 };
532 sc
= ifnet_softc(ifp
);
533 dst6
= (const struct sockaddr_in6
*)(const void *)dst
;
536 if ((ifnet_flags(ifp
) & IFF_UP
) == 0) {
537 printf("stf: IFF_DOWN\n");
542 * If we don't have an ip4 address that match my inner ip6 address,
543 * we shouldn't generate output. Without this check, we'll end up
544 * using wrong IPv4 source.
546 ia6
= stf_getsrcifa6(ifp
);
551 if (mbuf_len(m
) < sizeof(*ip6
)) {
552 m
= m_pullup(m
, sizeof(*ip6
));
554 *m0
= NULL
; /* makes sure this won't be double freed */
555 IFA_REMREF(&ia6
->ia_ifa
);
559 ip6
= mtod(m
, struct ip6_hdr
*);
560 tos
= (ntohl(ip6
->ip6_flow
) >> 20) & 0xff;
563 * Pickup the right outer dst addr from the list of candidates.
564 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
566 if (IN6_IS_ADDR_6TO4(&ip6
->ip6_dst
))
567 in4
= GET_V4(&ip6
->ip6_dst
);
568 else if (IN6_IS_ADDR_6TO4(&dst6
->sin6_addr
))
569 in4
= GET_V4(&dst6
->sin6_addr
);
571 IFA_REMREF(&ia6
->ia_ifa
);
576 /* We need to prepend the address family as a four byte field. */
577 u_int32_t af
= AF_INET6
;
579 bpf_tap_out(ifp
, 0, m
, &af
, sizeof(af
));
582 M_PREPEND(m
, sizeof(struct ip
), M_DONTWAIT
);
583 if (m
&& mbuf_len(m
) < sizeof(struct ip
))
584 m
= m_pullup(m
, sizeof(struct ip
));
587 IFA_REMREF(&ia6
->ia_ifa
);
590 ip
= mtod(m
, struct ip
*);
592 bzero(ip
, sizeof(*ip
));
594 IFA_LOCK_SPIN(&ia6
->ia_ifa
);
595 bcopy(GET_V4(&((struct sockaddr_in6
*)&ia6
->ia_addr
)->sin6_addr
),
596 &ip
->ip_src
, sizeof(ip
->ip_src
));
597 IFA_UNLOCK(&ia6
->ia_ifa
);
598 bcopy(in4
, &ip
->ip_dst
, sizeof(ip
->ip_dst
));
599 ip
->ip_p
= IPPROTO_IPV6
;
600 ip
->ip_ttl
= ip_stf_ttl
;
601 ip
->ip_len
= m
->m_pkthdr
.len
; /*host order*/
602 if (ifp
->if_flags
& IFF_LINK1
)
603 ip_ecn_ingress(ECN_ALLOWED
, &ip
->ip_tos
, &tos
);
605 ip_ecn_ingress(ECN_NOCARE
, &ip
->ip_tos
, &tos
);
607 lck_mtx_lock(&sc
->sc_ro_mtx
);
608 dst4
= (struct sockaddr_in
*)(void *)&sc
->sc_ro
.ro_dst
;
609 if (ROUTE_UNUSABLE(&sc
->sc_ro
) || dst4
->sin_family
!= AF_INET
||
610 bcmp(&dst4
->sin_addr
, &ip
->ip_dst
, sizeof(ip
->ip_dst
)) != 0) {
611 ROUTE_RELEASE(&sc
->sc_ro
);
612 /* cache route doesn't match: always the case during the first use */
613 dst4
->sin_family
= AF_INET
;
614 dst4
->sin_len
= sizeof(struct sockaddr_in
);
615 bcopy(&ip
->ip_dst
, &dst4
->sin_addr
, sizeof(dst4
->sin_addr
));
618 result
= ip_output(m
, NULL
, &sc
->sc_ro
, IP_OUTARGS
, NULL
, &ipoa
);
619 lck_mtx_unlock(&sc
->sc_ro_mtx
);
621 /* Assumption: ip_output will free mbuf on errors */
622 /* All the output processing is done here, don't let stf_output be called */
624 result
= EJUSTRETURN
;
626 IFA_REMREF(&ia6
->ia_ifa
);
631 __unused ifnet_t ifp
,
634 /* All processing is done in stf_pre_output
635 * this shouldn't be called as the pre_output returns "EJUSTRETURN"
642 struct stf_softc
*sc
,
643 const struct in_addr
*in
,
644 struct ifnet
*inifp
) /* incoming interface */
646 struct in_ifaddr
*ia4
;
649 * reject packets with the following address:
650 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
652 if (IN_MULTICAST(ntohl(in
->s_addr
)))
654 switch ((ntohl(in
->s_addr
) & 0xff000000) >> 24) {
655 case 0: case 127: case 255:
660 * reject packets with broadcast
662 lck_rw_lock_shared(in_ifaddr_rwlock
);
663 for (ia4
= TAILQ_FIRST(&in_ifaddrhead
);
665 ia4
= TAILQ_NEXT(ia4
, ia_link
))
667 IFA_LOCK(&ia4
->ia_ifa
);
668 if ((ia4
->ia_ifa
.ifa_ifp
->if_flags
& IFF_BROADCAST
) == 0) {
669 IFA_UNLOCK(&ia4
->ia_ifa
);
672 if (in
->s_addr
== ia4
->ia_broadaddr
.sin_addr
.s_addr
) {
673 IFA_UNLOCK(&ia4
->ia_ifa
);
674 lck_rw_done(in_ifaddr_rwlock
);
677 IFA_UNLOCK(&ia4
->ia_ifa
);
679 lck_rw_done(in_ifaddr_rwlock
);
682 * perform ingress filter
684 if (sc
&& (ifnet_flags(sc
->sc_if
) & IFF_LINK2
) == 0 && inifp
) {
685 struct sockaddr_in sin
;
688 bzero(&sin
, sizeof(sin
));
689 sin
.sin_family
= AF_INET
;
690 sin
.sin_len
= sizeof(struct sockaddr_in
);
692 rt
= rtalloc1((struct sockaddr
*)&sin
, 0, 0);
695 if (rt
== NULL
|| rt
->rt_ifp
!= inifp
) {
697 log(LOG_WARNING
, "%s: packet from 0x%x dropped "
698 "due to ingress filter\n", if_name(sc
->sc_if
),
699 (u_int32_t
)ntohl(sin
.sin_addr
.s_addr
));
716 struct stf_softc
*sc
,
717 struct in6_addr
*in6
,
718 struct ifnet
*inifp
) /* incoming interface */
721 * check 6to4 addresses
723 if (IN6_IS_ADDR_6TO4(in6
))
724 return stf_checkaddr4(sc
, GET_V4(in6
), inifp
);
727 * reject anything that look suspicious. the test is implemented
728 * in ip6_input too, but we check here as well to
729 * (1) reject bad packets earlier, and
730 * (2) to be safe against future ip6_input change.
732 if (IN6_IS_ADDR_V4COMPAT(in6
) || IN6_IS_ADDR_V4MAPPED(in6
))
743 struct stf_softc
*sc
;
749 struct ifnet_stat_increment_param stats
;
751 ip
= mtod(m
, struct ip
*);
754 if (proto
!= IPPROTO_IPV6
) {
759 ip
= mtod(m
, struct ip
*);
761 sc
= (struct stf_softc
*)encap_getarg(m
);
763 if (sc
== NULL
|| (ifnet_flags(sc
->sc_if
) & IFF_UP
) == 0) {
771 mac_mbuf_label_associate_ifnet(ifp
, m
);
775 * perform sanity check against outer src/dst.
776 * for source, perform ingress filter as well.
778 if (stf_checkaddr4(sc
, &ip
->ip_dst
, NULL
) < 0 ||
779 stf_checkaddr4(sc
, &ip
->ip_src
, m
->m_pkthdr
.rcvif
) < 0) {
785 mbuf_copydata(m
, off
, sizeof(ip6
), &ip6
);
788 * perform sanity check against inner src/dst.
789 * for source, perform ingress filter as well.
791 if (stf_checkaddr6(sc
, &ip6
.ip6_dst
, NULL
) < 0 ||
792 stf_checkaddr6(sc
, &ip6
.ip6_src
, m
->m_pkthdr
.rcvif
) < 0) {
797 itos
= (ntohl(ip6
.ip6_flow
) >> 20) & 0xff;
798 if ((ifnet_flags(ifp
) & IFF_LINK1
) != 0)
799 ip_ecn_egress(ECN_ALLOWED
, &otos
, &itos
);
801 ip_ecn_egress(ECN_NOCARE
, &otos
, &itos
);
802 ip6
.ip6_flow
&= ~htonl(0xff << 20);
803 ip6
.ip6_flow
|= htonl((u_int32_t
)itos
<< 20);
805 m
->m_pkthdr
.rcvif
= ifp
;
806 mbuf_pkthdr_setheader(m
, mbuf_data(m
));
810 /* We need to prepend the address family as a four byte field. */
811 u_int32_t af
= AF_INET6
;
812 bpf_tap_in(ifp
, 0, m
, &af
, sizeof(af
));
816 * Put the packet to the network layer input queue according to the
817 * specified address family.
818 * See net/if_gif.c for possible issues with packet processing
819 * reorder due to extra queueing.
821 bzero(&stats
, sizeof(stats
));
822 stats
.packets_in
= 1;
823 stats
.bytes_in
= mbuf_pkthdr_len(m
);
824 mbuf_pkthdr_setrcvif(m
, ifp
);
825 ifnet_input(ifp
, m
, &stats
);
834 __unused
struct sockaddr
*sa
)
837 RT_LOCK_ASSERT_HELD(rt
);
838 rt
->rt_rmx
.rmx_mtu
= IPV6_MMTU
;
850 struct sockaddr_in6
*sin6
;
856 ifa
= (struct ifaddr
*)data
;
858 error
= EAFNOSUPPORT
;
862 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
) {
864 error
= EAFNOSUPPORT
;
867 sin6
= (struct sockaddr_in6
*)(void *)ifa
->ifa_addr
;
868 if (IN6_IS_ADDR_6TO4(&sin6
->sin6_addr
)) {
869 if ( !(ifnet_flags( ifp
) & IFF_UP
) ) {
870 /* do this only if the interface is not already up */
871 ifa
->ifa_rtrequest
= stf_rtrequest
;
873 ifnet_set_flags(ifp
, IFF_UP
, IFF_UP
);
881 IFA_LOCK_ASSERT_NOTHELD(ifa
);
886 ifr
= (struct ifreq
*)data
;
887 if (ifr
&& ifr
->ifr_addr
.sa_family
== AF_INET6
)
890 error
= EAFNOSUPPORT
;