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