Home | History | Annotate | Line # | Download | only in net
if_stf.c revision 1.1
      1 /*	$NetBSD: if_stf.c,v 1.1 2000/04/19 06:30:53 itojun Exp $	*/
      2 /*	$KAME: if_stf.c,v 1.31 2000/04/19 05:51:20 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 2000 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     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.
     19  *
     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
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * 6to4 interface, based on draft-ietf-ngtrans-6to4-03.txt.
     35  *
     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.
     39  *
     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).
     43  *
     44  * Here are interesting symptoms due to the lack of link-local address:
     45  *
     46  * Unicast routing exchange:
     47  * - RIPng: Impossible.  Uses link-local multicast packet toward ff02::2,
     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.
     54  *
     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?).
     59  *
     60  * ICMPv6:
     61  * - Redirects cannot be used due to the lack of link-local address.
     62  *
     63  * 04 draft suggests to have link-local address onto 6to4 interface.
     64  * However, it seems to have no real use and does not help the above symptom
     65  * much.
     66  *
     67  * 6to4 interface has security issues.  Refer to
     68  * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
     69  * for details.  The code tries to filter out some of malicious packets.
     70  * Note that there is no way to be 100% secure.
     71  */
     72 
     73 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
     74 #include "opt_inet.h"
     75 #endif
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/socket.h>
     80 #include <sys/sockio.h>
     81 #include <sys/mbuf.h>
     82 #include <sys/errno.h>
     83 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
     84 #include <sys/ioctl.h>
     85 #endif
     86 #include <sys/protosw.h>
     87 #ifdef __FreeBSD__
     88 #include <sys/kernel.h>
     89 #endif
     90 #include <machine/cpu.h>
     91 
     92 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
     93 #include <sys/malloc.h>
     94 #endif
     95 
     96 #include <net/if.h>
     97 #include <net/route.h>
     98 #include <net/netisr.h>
     99 #include <net/if_types.h>
    100 #include <net/if_stf.h>
    101 
    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>
    107 
    108 #include <netinet/ip6.h>
    109 #include <netinet6/ip6_var.h>
    110 #include <netinet6/in6_gif.h>
    111 #include <netinet6/in6_var.h>
    112 #include <netinet/ip_ecn.h>
    113 
    114 #include <netinet/ip_encap.h>
    115 
    116 #include <machine/stdarg.h>
    117 
    118 #include <net/net_osdep.h>
    119 
    120 #include "bpfilter.h"
    121 #include "stf.h"
    122 #include "gif.h"	/*XXX*/
    123 
    124 #if NBPFILTER > 0
    125 #include <net/bpf.h>
    126 #endif
    127 
    128 #if NGIF > 0
    129 #include <net/if_gif.h>
    130 #endif
    131 
    132 #if NSTF > 0
    133 #if NSTF != 1
    134 # error only single stf interface allowed
    135 #endif
    136 
    137 #define IN6_IS_ADDR_6TO4(x)	(ntohs((x)->s6_addr16[0]) == 0x2002)
    138 #define GET_V4(x)	((struct in_addr *)(&(x)->s6_addr16[1]))
    139 
    140 struct stf_softc {
    141 	struct ifnet	sc_if;	   /* common area */
    142 	union {
    143 		struct route  __sc_ro4;
    144 		struct route_in6 __sc_ro6; /* just for safety */
    145 	} __sc_ro46;
    146 #define sc_ro	__sc_ro46.__sc_ro4
    147 	const struct encaptab *encap_cookie;
    148 };
    149 
    150 static struct stf_softc *stf;
    151 static int nstf;
    152 
    153 #if NGIF > 0
    154 extern int ip_gif_ttl;	/*XXX*/
    155 #else
    156 static int ip_gif_ttl = 40;	/*XXX*/
    157 #endif
    158 
    159 extern struct protosw in_stf_protosw;
    160 
    161 #ifdef __FreeBSD__
    162 void stfattach __P((void *));
    163 #else
    164 void stfattach __P((int));
    165 #endif
    166 static int stf_encapcheck __P((const struct mbuf *, int, int, void *));
    167 static struct in6_ifaddr *stf_getsrcifa6 __P((struct ifnet *));
    168 static int stf_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
    169 	struct rtentry *));
    170 static int stf_checkaddr4 __P((struct in_addr *, struct ifnet *));
    171 static int stf_checkaddr6 __P((struct in6_addr *, struct ifnet *));
    172 #if defined(__bsdi__) && _BSDI_VERSION >= 199802
    173 static void stf_rtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
    174 #else
    175 static void stf_rtrequest __P((int, struct rtentry *, struct sockaddr *));
    176 #endif
    177 #if defined(__FreeBSD__) && __FreeBSD__ < 3
    178 static int stf_ioctl __P((struct ifnet *, int, caddr_t));
    179 #else
    180 static int stf_ioctl __P((struct ifnet *, u_long, caddr_t));
    181 #endif
    182 
    183 void
    184 stfattach(dummy)
    185 #ifdef __FreeBSD__
    186 	void *dummy;
    187 #else
    188 	int dummy;
    189 #endif
    190 {
    191 	struct stf_softc *sc;
    192 	int i;
    193 	const struct encaptab *p;
    194 
    195 #ifdef __NetBSD__
    196 	nstf = dummy;
    197 #else
    198 	nstf = NSTF;
    199 #endif
    200 	stf = malloc(nstf * sizeof(struct stf_softc), M_DEVBUF, M_WAIT);
    201 	bzero(stf, nstf * sizeof(struct stf_softc));
    202 	sc = stf;
    203 
    204 	/* XXX just in case... */
    205 	for (i = 0; i < nstf; i++) {
    206 		sc = &stf[i];
    207 		bzero(sc, sizeof(*sc));
    208 #if defined(__NetBSD__) || defined(__OpenBSD__)
    209 		sprintf(sc->sc_if.if_xname, "stf%d", i);
    210 #else
    211 		sc->sc_if.if_name = "stf";
    212 		sc->sc_if.if_unit = i;
    213 #endif
    214 
    215 		p = encap_attach_func(AF_INET, IPPROTO_IPV6, stf_encapcheck,
    216 		    &in_stf_protosw, sc);
    217 		if (p == NULL) {
    218 			printf("%s: attach failed\n", if_name(&sc->sc_if));
    219 			continue;
    220 		}
    221 		sc->encap_cookie = p;
    222 
    223 		sc->sc_if.if_mtu    = IPV6_MMTU;
    224 		sc->sc_if.if_flags  = 0;
    225 		sc->sc_if.if_ioctl  = stf_ioctl;
    226 		sc->sc_if.if_output = stf_output;
    227 		sc->sc_if.if_type   = IFT_STF;
    228 		if_attach(&sc->sc_if);
    229 #if NBPFILTER > 0
    230 #ifdef HAVE_OLD_BPF
    231 		bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
    232 #else
    233 		bpfattach(&sc->sc_if.if_bpf, &sc->sc_if, DLT_NULL, sizeof(u_int));
    234 #endif
    235 #endif
    236 	}
    237 }
    238 
    239 #ifdef __FreeBSD__
    240 PSEUDO_SET(stfattach, if_stf);
    241 #endif
    242 
    243 static int
    244 stf_encapcheck(m, off, proto, arg)
    245 	const struct mbuf *m;
    246 	int off;
    247 	int proto;
    248 	void *arg;
    249 {
    250 	struct ip ip;
    251 	struct in6_ifaddr *ia6;
    252 	struct stf_softc *sc;
    253 	struct in_addr a, b;
    254 
    255 	sc = (struct stf_softc *)arg;
    256 	if (sc == NULL)
    257 		return 0;
    258 
    259 	if ((sc->sc_if.if_flags & IFF_UP) == 0)
    260 		return 0;
    261 
    262 	if (proto != IPPROTO_IPV6)
    263 		return 0;
    264 
    265 	/* LINTED const cast */
    266 	m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
    267 
    268 	if (ip.ip_v != 4)
    269 		return 0;
    270 
    271 	ia6 = stf_getsrcifa6(&sc->sc_if);
    272 	if (ia6 == NULL)
    273 		return 0;
    274 
    275 	/*
    276 	 * check if IPv4 dst matches the IPv4 address derived from the
    277 	 * local 6to4 address.
    278 	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
    279 	 */
    280 	if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
    281 	    sizeof(ip.ip_dst)) != 0)
    282 		return 0;
    283 
    284 	/*
    285 	 * check if IPv4 src matches the IPv4 address derived from the
    286 	 * local 6to4 address masked by prefixmask.
    287 	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
    288 	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
    289 	 */
    290 	bzero(&a, sizeof(a));
    291 	a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr;
    292 	a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
    293 	b = ip.ip_src;
    294 	b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
    295 	if (a.s_addr != b.s_addr)
    296 		return 0;
    297 
    298 	/* stf interface makes single side match only */
    299 	return 32;
    300 }
    301 
    302 static struct in6_ifaddr *
    303 stf_getsrcifa6(ifp)
    304 	struct ifnet *ifp;
    305 {
    306 	struct ifaddr *ia;
    307 	struct in_ifaddr *ia4;
    308 	struct sockaddr_in6 *sin6;
    309 	struct in_addr in;
    310 
    311 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
    312 	for (ia = ifp->if_addrlist; ia; ia = ia->ifa_next)
    313 #else
    314 	for (ia = ifp->if_addrlist.tqh_first;
    315 	     ia;
    316 	     ia = ia->ifa_list.tqe_next)
    317 #endif
    318 	{
    319 		if (ia->ifa_addr == NULL)
    320 			continue;
    321 		if (ia->ifa_addr->sa_family != AF_INET6)
    322 			continue;
    323 		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
    324 		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
    325 			continue;
    326 
    327 		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
    328 #ifdef __NetBSD__
    329 		INADDR_TO_IA(in, ia4);
    330 #else
    331 #ifdef __OpenBSD__
    332 		for (ia4 = in_ifaddr.tqh_first;
    333 		     ia4;
    334 		     ia4 = ia4->ia_list.tqe_next)
    335 #else
    336 		for (ia4 = in_ifaddr; ia4 != NULL; ia4 = ia4->ia_next)
    337 #endif
    338 		{
    339 			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
    340 				break;
    341 		}
    342 #endif
    343 		if (ia4 == NULL)
    344 			continue;
    345 
    346 		return (struct in6_ifaddr *)ia;
    347 	}
    348 
    349 	return NULL;
    350 }
    351 
    352 static int
    353 stf_output(ifp, m, dst, rt)
    354 	struct ifnet *ifp;
    355 	struct mbuf *m;
    356 	struct sockaddr *dst;
    357 	struct rtentry *rt;
    358 {
    359 	struct stf_softc *sc;
    360 	struct sockaddr_in6 *dst6;
    361 	struct sockaddr_in *dst4;
    362 	u_int8_t tos;
    363 	struct ip *ip;
    364 	struct ip6_hdr *ip6;
    365 	struct in6_ifaddr *ia6;
    366 
    367 	sc = (struct stf_softc*)ifp;
    368 	dst6 = (struct sockaddr_in6 *)dst;
    369 
    370 	/* just in case */
    371 	if ((ifp->if_flags & IFF_UP) == 0) {
    372 		m_freem(m);
    373 		return ENETDOWN;
    374 	}
    375 
    376 	/*
    377 	 * If we don't have an ip4 address that match my inner ip6 address,
    378 	 * we shouldn't generate output.  Without this check, we'll end up
    379 	 * using wrong IPv4 source.
    380 	 */
    381 	ia6 = stf_getsrcifa6(ifp);
    382 	if (ia6 == NULL) {
    383 		m_freem(m);
    384 		return ENETDOWN;
    385 	}
    386 
    387 	if (m->m_len < sizeof(*ip6)) {
    388 		m = m_pullup(m, sizeof(*ip6));
    389 		if (!m)
    390 			return ENOBUFS;
    391 	}
    392 	ip6 = mtod(m, struct ip6_hdr *);
    393 	tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    394 
    395 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    396 	if (m && m->m_len < sizeof(struct ip))
    397 		m = m_pullup(m, sizeof(struct ip));
    398 	if (m == NULL)
    399 		return ENOBUFS;
    400 	ip = mtod(m, struct ip *);
    401 
    402 	bzero(ip, sizeof(*ip));
    403 
    404 	bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
    405 	    &ip->ip_src, sizeof(ip->ip_src));
    406 	bcopy(GET_V4(&dst6->sin6_addr), &ip->ip_dst, sizeof(ip->ip_dst));
    407 	ip->ip_p = IPPROTO_IPV6;
    408 	ip->ip_ttl = ip_gif_ttl;	/*XXX*/
    409 	ip->ip_len = m->m_pkthdr.len;	/*host order*/
    410 	if (ifp->if_flags & IFF_LINK1)
    411 		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
    412 
    413 	dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
    414 	if (dst4->sin_family != AF_INET ||
    415 	    bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
    416 		/* cache route doesn't match */
    417 		dst4->sin_family = AF_INET;
    418 		dst4->sin_len = sizeof(struct sockaddr_in);
    419 		bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
    420 		if (sc->sc_ro.ro_rt) {
    421 			RTFREE(sc->sc_ro.ro_rt);
    422 			sc->sc_ro.ro_rt = NULL;
    423 		}
    424 	}
    425 
    426 	if (sc->sc_ro.ro_rt == NULL) {
    427 		rtalloc(&sc->sc_ro);
    428 		if (sc->sc_ro.ro_rt == NULL) {
    429 			m_freem(m);
    430 			return ENETUNREACH;
    431 		}
    432 	}
    433 
    434 #ifndef __OpenBSD__
    435 	return ip_output(m, NULL, &sc->sc_ro, 0, NULL);
    436 #else
    437 	return ip_output(m, NULL, &sc->sc_ro, 0, NULL, NULL);
    438 #endif
    439 }
    440 
    441 static int
    442 stf_checkaddr4(in, ifp)
    443 	struct in_addr *in;
    444 	struct ifnet *ifp;	/* incoming interface */
    445 {
    446 	struct in_ifaddr *ia4;
    447 
    448 	/*
    449 	 * reject packets with the following address:
    450 	 * 224.0.0.0/4 0.0.0.0/32 255.255.255.255/32
    451 	 */
    452 	if (IN_MULTICAST(in->s_addr) || in->s_addr == INADDR_ANY ||
    453 	    in->s_addr == INADDR_BROADCAST) {
    454 		return -1;
    455 	}
    456 
    457 	/*
    458 	 * reject packets with broadcast
    459 	 */
    460 #if defined(__OpenBSD__) || defined(__NetBSD__)
    461 	for (ia4 = in_ifaddr.tqh_first; ia4; ia4 = ia4->ia_list.tqe_next)
    462 #else
    463 	for (ia4 = in_ifaddr; ia4 != NULL; ia4 = ia4->ia_next)
    464 #endif
    465 	{
    466 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
    467 			continue;
    468 		if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
    469 			return -1;
    470 	}
    471 
    472 	/*
    473 	 * perform ingress filter
    474 	 */
    475 	if (ifp) {
    476 		struct sockaddr_in sin;
    477 		struct rtentry *rt;
    478 
    479 		bzero(&sin, sizeof(sin));
    480 		sin.sin_family = AF_INET;
    481 		sin.sin_len = sizeof(struct sockaddr_in);
    482 		sin.sin_addr = *in;
    483 #ifdef __FreeBSD__
    484 		rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
    485 #else
    486 		rt = rtalloc1((struct sockaddr *)&sin, 0);
    487 #endif
    488 		if (!rt)
    489 			return -1;
    490 		if (rt->rt_ifp != ifp) {
    491 			rtfree(rt);
    492 			return -1;
    493 		}
    494 		rtfree(rt);
    495 	}
    496 
    497 	return 0;
    498 }
    499 
    500 static int
    501 stf_checkaddr6(in6, ifp)
    502 	struct in6_addr *in6;
    503 	struct ifnet *ifp;	/* incoming interface */
    504 {
    505 	/*
    506 	 * check 6to4 addresses
    507 	 */
    508 	if (IN6_IS_ADDR_6TO4(in6))
    509 		return stf_checkaddr4(GET_V4(in6), ifp);
    510 
    511 	/*
    512 	 * reject anything that look suspicious.  the test is implemented
    513 	 * in ip6_input too, but we check here as well to
    514 	 * (1) reject bad packets earlier, and
    515 	 * (2) to be safe against future ip6_input change.
    516 	 */
    517 	if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
    518 		return -1;
    519 
    520 	return 0;
    521 }
    522 
    523 void
    524 #if __STDC__
    525 in_stf_input(struct mbuf *m, ...)
    526 #else
    527 in_stf_input(m, va_alist)
    528 	register struct mbuf *m;
    529 #endif
    530 {
    531 	int off, proto;
    532 	struct stf_softc *sc;
    533 	struct ip *ip;
    534 	struct ip6_hdr *ip6;
    535 	u_int8_t otos, itos;
    536 	int s, isr;
    537 	struct ifqueue *ifq = NULL;
    538 	struct ifnet *ifp;
    539 	va_list ap;
    540 
    541 	va_start(ap, m);
    542 	off = va_arg(ap, int);
    543 	proto = va_arg(ap, int);
    544 	va_end(ap);
    545 
    546 	if (proto != IPPROTO_IPV6) {
    547 		m_freem(m);
    548 		return;
    549 	}
    550 
    551 	ip = mtod(m, struct ip *);
    552 
    553 	sc = (struct stf_softc *)encap_getarg(m);
    554 
    555 	if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
    556 		m_freem(m);
    557 		return;
    558 	}
    559 
    560 	ifp = &sc->sc_if;
    561 
    562 	/*
    563 	 * perform sanity check against outer src/dst.
    564 	 * for source, perform ingress filter as well.
    565 	 */
    566 	if (stf_checkaddr4(&ip->ip_dst, NULL) < 0 ||
    567 	    stf_checkaddr4(&ip->ip_src, m->m_pkthdr.rcvif) < 0) {
    568 		m_freem(m);
    569 		return;
    570 	}
    571 
    572 	otos = ip->ip_tos;
    573 	m_adj(m, off);
    574 
    575 	if (m->m_len < sizeof(*ip6)) {
    576 		m = m_pullup(m, sizeof(*ip6));
    577 		if (!m)
    578 			return;
    579 	}
    580 	ip6 = mtod(m, struct ip6_hdr *);
    581 
    582 	/*
    583 	 * perform sanity check against inner src/dst.
    584 	 * for source, perform ingress filter as well.
    585 	 */
    586 	if (stf_checkaddr6(&ip6->ip6_dst, NULL) < 0 ||
    587 	    stf_checkaddr6(&ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
    588 		m_freem(m);
    589 		return;
    590 	}
    591 
    592 	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    593 	if ((ifp->if_flags & IFF_LINK1) != 0)
    594 		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
    595 	ip6->ip6_flow &= ~htonl(0xff << 20);
    596 	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
    597 
    598 	m->m_pkthdr.rcvif = ifp;
    599 
    600 #if NBPFILTER > 0
    601 	if (ifp->if_bpf) {
    602 		/*
    603 		 * We need to prepend the address family as
    604 		 * a four byte field.  Cons up a dummy header
    605 		 * to pacify bpf.  This is safe because bpf
    606 		 * will only read from the mbuf (i.e., it won't
    607 		 * try to free it or keep a pointer a to it).
    608 		 */
    609 		struct mbuf m0;
    610 		u_int af = AF_INET6;
    611 
    612 		m0.m_next = m;
    613 		m0.m_len = 4;
    614 		m0.m_data = (char *)&af;
    615 
    616 #ifdef HAVE_OLD_BPF
    617 		bpf_mtap(ifp, &m0);
    618 #else
    619 		bpf_mtap(ifp->if_bpf, &m0);
    620 #endif
    621 	}
    622 #endif /*NBPFILTER > 0*/
    623 
    624 	/*
    625 	 * Put the packet to the network layer input queue according to the
    626 	 * specified address family.
    627 	 * See net/if_gif.c for possible issues with packet processing
    628 	 * reorder due to extra queueing.
    629 	 */
    630 	ifq = &ip6intrq;
    631 	isr = NETISR_IPV6;
    632 
    633 	s = splimp();
    634 	if (IF_QFULL(ifq)) {
    635 		IF_DROP(ifq);	/* update statistics */
    636 		m_freem(m);
    637 		splx(s);
    638 		return;
    639 	}
    640 	IF_ENQUEUE(ifq, m);
    641 	schednetisr(isr);
    642 	ifp->if_ipackets++;
    643 	ifp->if_ibytes += m->m_pkthdr.len;
    644 	splx(s);
    645 }
    646 
    647 /* ARGSUSED */
    648 static void
    649 stf_rtrequest(cmd, rt, sa)
    650 	int cmd;
    651 	struct rtentry *rt;
    652 #if defined(__bsdi__) && _BSDI_VERSION >= 199802
    653 	struct rt_addrinfo *sa;
    654 #else
    655 	struct sockaddr *sa;
    656 #endif
    657 {
    658 
    659 	if (rt)
    660 		rt->rt_rmx.rmx_mtu = IPV6_MMTU;
    661 }
    662 
    663 static int
    664 stf_ioctl(ifp, cmd, data)
    665 	struct ifnet *ifp;
    666 #if defined(__FreeBSD__) && __FreeBSD__ < 3
    667 	int cmd;
    668 #else
    669 	u_long cmd;
    670 #endif
    671 	caddr_t data;
    672 {
    673 	struct ifaddr *ifa;
    674 	struct ifreq *ifr;
    675 	struct sockaddr_in6 *sin6;
    676 	int error;
    677 
    678 	error = 0;
    679 	switch (cmd) {
    680 	case SIOCSIFADDR:
    681 		ifa = (struct ifaddr *)data;
    682 		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
    683 			error = EAFNOSUPPORT;
    684 			break;
    685 		}
    686 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
    687 		if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
    688 			ifa->ifa_rtrequest = stf_rtrequest;
    689 			ifp->if_flags |= IFF_UP;
    690 		} else
    691 			error = EINVAL;
    692 		break;
    693 
    694 	case SIOCADDMULTI:
    695 	case SIOCDELMULTI:
    696 		ifr = (struct ifreq *)data;
    697 		if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
    698 			;
    699 		else
    700 			error = EAFNOSUPPORT;
    701 		break;
    702 
    703 	default:
    704 		error = EINVAL;
    705 		break;
    706 	}
    707 
    708 	return error;
    709 }
    710 
    711 #endif /* NSTF > 0 */
    712