Home | History | Annotate | Line # | Download | only in net
if_stf.c revision 1.8
      1 /*	$NetBSD: if_stf.c,v 1.8 2001/01/17 00:30:52 thorpej Exp $	*/
      2 /*	$KAME: if_stf.c,v 1.39 2000/06/07 23:35:18 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 
     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 in_addr *, struct ifnet *));
    188 static int stf_checkaddr6 __P((struct in6_addr *, struct ifnet *));
    189 #if defined(__bsdi__) && _BSDI_VERSION >= 199802
    190 static void stf_rtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
    191 #else
    192 static void stf_rtrequest __P((int, struct rtentry *, struct sockaddr *));
    193 #endif
    194 #if defined(__FreeBSD__) && __FreeBSD__ < 3
    195 static int stf_ioctl __P((struct ifnet *, int, caddr_t));
    196 #else
    197 static int stf_ioctl __P((struct ifnet *, u_long, caddr_t));
    198 #endif
    199 
    200 /* ARGSUSED */
    201 void
    202 stfattach(count)
    203 	int count;
    204 {
    205 
    206 	LIST_INIT(&stf_softc_list);
    207 	if_clone_attach(&stf_cloner);
    208 }
    209 
    210 int
    211 stf_clone_create(ifc, unit)
    212 	struct if_clone *ifc;
    213 	int unit;
    214 {
    215 	struct stf_softc *sc;
    216 
    217 	if (LIST_FIRST(&stf_softc_list) != NULL) {
    218 		/* Only one stf interface is allowed. */
    219 		return (EEXIST);
    220 	}
    221 
    222 	sc = malloc(sizeof(struct stf_softc), M_DEVBUF, M_WAIT);
    223 	bzero(sc, sizeof(struct stf_softc));
    224 
    225 	sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit);
    226 
    227 	sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
    228 	    stf_encapcheck, &in_stf_protosw, sc);
    229 	if (sc->encap_cookie == NULL) {
    230 		printf("%s: unable to attach encap\n", if_name(&sc->sc_if));
    231 		free(sc, M_DEVBUF);
    232 		return (EIO);	/* XXX */
    233 	}
    234 
    235 	sc->sc_if.if_mtu    = IPV6_MMTU;
    236 	sc->sc_if.if_flags  = 0;
    237 	sc->sc_if.if_ioctl  = stf_ioctl;
    238 	sc->sc_if.if_output = stf_output;
    239 	sc->sc_if.if_type   = IFT_STF;
    240 	sc->sc_if.if_dlt    = DLT_NULL;
    241 #if defined(__FreeBSD__) && __FreeBSD__ >= 4
    242 	sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
    243 #endif
    244 	if_attach(&sc->sc_if);
    245 	if_alloc_sadl(&sc->sc_if);
    246 #if NBPFILTER > 0
    247 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
    248 #endif
    249 	LIST_INSERT_HEAD(&stf_softc_list, sc, sc_list);
    250 	return (0);
    251 }
    252 
    253 void
    254 stf_clone_destroy(ifp)
    255 	struct ifnet *ifp;
    256 {
    257 	struct stf_softc *sc = (void *) ifp;
    258 
    259 	LIST_REMOVE(sc, sc_list);
    260 	encap_detach(sc->encap_cookie);
    261 #if NBPFILTER > 0
    262 	bpfdetach(ifp);
    263 #endif
    264 	if_detach(ifp);
    265 	free(sc, M_DEVBUF);
    266 }
    267 
    268 #ifdef __FreeBSD__
    269 PSEUDO_SET(stfattach, if_stf);
    270 #endif
    271 
    272 static int
    273 stf_encapcheck(m, off, proto, arg)
    274 	const struct mbuf *m;
    275 	int off;
    276 	int proto;
    277 	void *arg;
    278 {
    279 	struct ip ip;
    280 	struct in6_ifaddr *ia6;
    281 	struct stf_softc *sc;
    282 	struct in_addr a, b;
    283 
    284 	sc = (struct stf_softc *)arg;
    285 	if (sc == NULL)
    286 		return 0;
    287 
    288 	if ((sc->sc_if.if_flags & IFF_UP) == 0)
    289 		return 0;
    290 
    291 	if (proto != IPPROTO_IPV6)
    292 		return 0;
    293 
    294 	/* LINTED const cast */
    295 	m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
    296 
    297 	if (ip.ip_v != 4)
    298 		return 0;
    299 
    300 	ia6 = stf_getsrcifa6(&sc->sc_if);
    301 	if (ia6 == NULL)
    302 		return 0;
    303 
    304 	/*
    305 	 * check if IPv4 dst matches the IPv4 address derived from the
    306 	 * local 6to4 address.
    307 	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
    308 	 */
    309 	if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
    310 	    sizeof(ip.ip_dst)) != 0)
    311 		return 0;
    312 
    313 	/*
    314 	 * check if IPv4 src matches the IPv4 address derived from the
    315 	 * local 6to4 address masked by prefixmask.
    316 	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
    317 	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
    318 	 */
    319 	bzero(&a, sizeof(a));
    320 	a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr;
    321 	a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
    322 	b = ip.ip_src;
    323 	b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
    324 	if (a.s_addr != b.s_addr)
    325 		return 0;
    326 
    327 	/* stf interface makes single side match only */
    328 	return 32;
    329 }
    330 
    331 static struct in6_ifaddr *
    332 stf_getsrcifa6(ifp)
    333 	struct ifnet *ifp;
    334 {
    335 	struct ifaddr *ia;
    336 	struct in_ifaddr *ia4;
    337 	struct sockaddr_in6 *sin6;
    338 	struct in_addr in;
    339 
    340 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
    341 	for (ia = ifp->if_addrlist; ia; ia = ia->ifa_next)
    342 #else
    343 	for (ia = ifp->if_addrlist.tqh_first;
    344 	     ia;
    345 	     ia = ia->ifa_list.tqe_next)
    346 #endif
    347 	{
    348 		if (ia->ifa_addr == NULL)
    349 			continue;
    350 		if (ia->ifa_addr->sa_family != AF_INET6)
    351 			continue;
    352 		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
    353 		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
    354 			continue;
    355 
    356 		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
    357 #ifdef __NetBSD__
    358 		INADDR_TO_IA(in, ia4);
    359 #else
    360 #ifdef __OpenBSD__
    361 		for (ia4 = in_ifaddr.tqh_first;
    362 		     ia4;
    363 		     ia4 = ia4->ia_list.tqe_next)
    364 #elif defined(__FreeBSD__) && __FreeBSD__ >= 3
    365 		for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
    366 		     ia4;
    367 		     ia4 = TAILQ_NEXT(ia4, ia_link))
    368 #else
    369 		for (ia4 = in_ifaddr; ia4 != NULL; ia4 = ia4->ia_next)
    370 #endif
    371 		{
    372 			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
    373 				break;
    374 		}
    375 #endif
    376 		if (ia4 == NULL)
    377 			continue;
    378 
    379 		return (struct in6_ifaddr *)ia;
    380 	}
    381 
    382 	return NULL;
    383 }
    384 
    385 static int
    386 stf_output(ifp, m, dst, rt)
    387 	struct ifnet *ifp;
    388 	struct mbuf *m;
    389 	struct sockaddr *dst;
    390 	struct rtentry *rt;
    391 {
    392 	struct stf_softc *sc;
    393 	struct sockaddr_in6 *dst6;
    394 	struct sockaddr_in *dst4;
    395 	u_int8_t tos;
    396 	struct ip *ip;
    397 	struct ip6_hdr *ip6;
    398 	struct in6_ifaddr *ia6;
    399 
    400 	sc = (struct stf_softc*)ifp;
    401 	dst6 = (struct sockaddr_in6 *)dst;
    402 
    403 	/* just in case */
    404 	if ((ifp->if_flags & IFF_UP) == 0) {
    405 		m_freem(m);
    406 		return ENETDOWN;
    407 	}
    408 
    409 	/*
    410 	 * If we don't have an ip4 address that match my inner ip6 address,
    411 	 * we shouldn't generate output.  Without this check, we'll end up
    412 	 * using wrong IPv4 source.
    413 	 */
    414 	ia6 = stf_getsrcifa6(ifp);
    415 	if (ia6 == NULL) {
    416 		m_freem(m);
    417 		return ENETDOWN;
    418 	}
    419 
    420 	if (m->m_len < sizeof(*ip6)) {
    421 		m = m_pullup(m, sizeof(*ip6));
    422 		if (!m)
    423 			return ENOBUFS;
    424 	}
    425 	ip6 = mtod(m, struct ip6_hdr *);
    426 	tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    427 
    428 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
    429 	if (m && m->m_len < sizeof(struct ip))
    430 		m = m_pullup(m, sizeof(struct ip));
    431 	if (m == NULL)
    432 		return ENOBUFS;
    433 	ip = mtod(m, struct ip *);
    434 
    435 	bzero(ip, sizeof(*ip));
    436 
    437 	bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
    438 	    &ip->ip_src, sizeof(ip->ip_src));
    439 	bcopy(GET_V4(&dst6->sin6_addr), &ip->ip_dst, sizeof(ip->ip_dst));
    440 	ip->ip_p = IPPROTO_IPV6;
    441 	ip->ip_ttl = ip_gif_ttl;	/*XXX*/
    442 	ip->ip_len = m->m_pkthdr.len;	/*host order*/
    443 	if (ifp->if_flags & IFF_LINK1)
    444 		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
    445 
    446 	dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
    447 	if (dst4->sin_family != AF_INET ||
    448 	    bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
    449 		/* cache route doesn't match */
    450 		dst4->sin_family = AF_INET;
    451 		dst4->sin_len = sizeof(struct sockaddr_in);
    452 		bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
    453 		if (sc->sc_ro.ro_rt) {
    454 			RTFREE(sc->sc_ro.ro_rt);
    455 			sc->sc_ro.ro_rt = NULL;
    456 		}
    457 	}
    458 
    459 	if (sc->sc_ro.ro_rt == NULL) {
    460 		rtalloc(&sc->sc_ro);
    461 		if (sc->sc_ro.ro_rt == NULL) {
    462 			m_freem(m);
    463 			return ENETUNREACH;
    464 		}
    465 	}
    466 
    467 #ifndef __OpenBSD__
    468 	return ip_output(m, NULL, &sc->sc_ro, 0, NULL);
    469 #else
    470 	return ip_output(m, NULL, &sc->sc_ro, 0, NULL, NULL);
    471 #endif
    472 }
    473 
    474 static int
    475 stf_checkaddr4(in, ifp)
    476 	struct in_addr *in;
    477 	struct ifnet *ifp;	/* incoming interface */
    478 {
    479 	struct in_ifaddr *ia4;
    480 
    481 	/*
    482 	 * reject packets with the following address:
    483 	 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
    484 	 */
    485 	if (IN_MULTICAST(in->s_addr))
    486 		return -1;
    487 	switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
    488 	case 0: case 127: case 255:
    489 		return -1;
    490 	}
    491 
    492 	/*
    493 	 * reject packets with broadcast
    494 	 */
    495 #if defined(__OpenBSD__) || defined(__NetBSD__)
    496 	for (ia4 = in_ifaddr.tqh_first; ia4; ia4 = ia4->ia_list.tqe_next)
    497 #elif defined(__FreeBSD__) && __FreeBSD__ >= 3
    498 	for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
    499 	     ia4;
    500 	     ia4 = TAILQ_NEXT(ia4, ia_link))
    501 #else
    502 	for (ia4 = in_ifaddr; ia4 != NULL; ia4 = ia4->ia_next)
    503 #endif
    504 	{
    505 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
    506 			continue;
    507 		if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
    508 			return -1;
    509 	}
    510 
    511 	/*
    512 	 * perform ingress filter
    513 	 */
    514 	if (ifp) {
    515 		struct sockaddr_in sin;
    516 		struct rtentry *rt;
    517 
    518 		bzero(&sin, sizeof(sin));
    519 		sin.sin_family = AF_INET;
    520 		sin.sin_len = sizeof(struct sockaddr_in);
    521 		sin.sin_addr = *in;
    522 #ifdef __FreeBSD__
    523 		rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
    524 #else
    525 		rt = rtalloc1((struct sockaddr *)&sin, 0);
    526 #endif
    527 		if (!rt)
    528 			return -1;
    529 		if (rt->rt_ifp != ifp) {
    530 			rtfree(rt);
    531 			return -1;
    532 		}
    533 		rtfree(rt);
    534 	}
    535 
    536 	return 0;
    537 }
    538 
    539 static int
    540 stf_checkaddr6(in6, ifp)
    541 	struct in6_addr *in6;
    542 	struct ifnet *ifp;	/* incoming interface */
    543 {
    544 	/*
    545 	 * check 6to4 addresses
    546 	 */
    547 	if (IN6_IS_ADDR_6TO4(in6))
    548 		return stf_checkaddr4(GET_V4(in6), ifp);
    549 
    550 	/*
    551 	 * reject anything that look suspicious.  the test is implemented
    552 	 * in ip6_input too, but we check here as well to
    553 	 * (1) reject bad packets earlier, and
    554 	 * (2) to be safe against future ip6_input change.
    555 	 */
    556 	if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
    557 		return -1;
    558 
    559 	return 0;
    560 }
    561 
    562 void
    563 #if __STDC__
    564 in_stf_input(struct mbuf *m, ...)
    565 #else
    566 in_stf_input(m, va_alist)
    567 	register struct mbuf *m;
    568 #endif
    569 {
    570 	int off, proto;
    571 	struct stf_softc *sc;
    572 	struct ip *ip;
    573 	struct ip6_hdr *ip6;
    574 	u_int8_t otos, itos;
    575 	int s, isr;
    576 	struct ifqueue *ifq = NULL;
    577 	struct ifnet *ifp;
    578 	va_list ap;
    579 
    580 	va_start(ap, m);
    581 	off = va_arg(ap, int);
    582 	proto = va_arg(ap, int);
    583 	va_end(ap);
    584 
    585 	if (proto != IPPROTO_IPV6) {
    586 		m_freem(m);
    587 		return;
    588 	}
    589 
    590 	ip = mtod(m, struct ip *);
    591 
    592 	sc = (struct stf_softc *)encap_getarg(m);
    593 
    594 	if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
    595 		m_freem(m);
    596 		return;
    597 	}
    598 
    599 	ifp = &sc->sc_if;
    600 
    601 	/*
    602 	 * perform sanity check against outer src/dst.
    603 	 * for source, perform ingress filter as well.
    604 	 */
    605 	if (stf_checkaddr4(&ip->ip_dst, NULL) < 0 ||
    606 	    stf_checkaddr4(&ip->ip_src, m->m_pkthdr.rcvif) < 0) {
    607 		m_freem(m);
    608 		return;
    609 	}
    610 
    611 	otos = ip->ip_tos;
    612 	m_adj(m, off);
    613 
    614 	if (m->m_len < sizeof(*ip6)) {
    615 		m = m_pullup(m, sizeof(*ip6));
    616 		if (!m)
    617 			return;
    618 	}
    619 	ip6 = mtod(m, struct ip6_hdr *);
    620 
    621 	/*
    622 	 * perform sanity check against inner src/dst.
    623 	 * for source, perform ingress filter as well.
    624 	 */
    625 	if (stf_checkaddr6(&ip6->ip6_dst, NULL) < 0 ||
    626 	    stf_checkaddr6(&ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
    627 		m_freem(m);
    628 		return;
    629 	}
    630 
    631 	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
    632 	if ((ifp->if_flags & IFF_LINK1) != 0)
    633 		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
    634 	ip6->ip6_flow &= ~htonl(0xff << 20);
    635 	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
    636 
    637 	m->m_pkthdr.rcvif = ifp;
    638 
    639 #if NBPFILTER > 0
    640 	if (ifp->if_bpf) {
    641 		/*
    642 		 * We need to prepend the address family as
    643 		 * a four byte field.  Cons up a dummy header
    644 		 * to pacify bpf.  This is safe because bpf
    645 		 * will only read from the mbuf (i.e., it won't
    646 		 * try to free it or keep a pointer a to it).
    647 		 */
    648 		struct mbuf m0;
    649 		u_int af = AF_INET6;
    650 
    651 		m0.m_next = m;
    652 		m0.m_len = 4;
    653 		m0.m_data = (char *)&af;
    654 
    655 #ifdef HAVE_OLD_BPF
    656 		bpf_mtap(ifp, &m0);
    657 #else
    658 		bpf_mtap(ifp->if_bpf, &m0);
    659 #endif
    660 	}
    661 #endif /*NBPFILTER > 0*/
    662 
    663 	/*
    664 	 * Put the packet to the network layer input queue according to the
    665 	 * specified address family.
    666 	 * See net/if_gif.c for possible issues with packet processing
    667 	 * reorder due to extra queueing.
    668 	 */
    669 	ifq = &ip6intrq;
    670 	isr = NETISR_IPV6;
    671 
    672 	s = splimp();
    673 	if (IF_QFULL(ifq)) {
    674 		IF_DROP(ifq);	/* update statistics */
    675 		m_freem(m);
    676 		splx(s);
    677 		return;
    678 	}
    679 	IF_ENQUEUE(ifq, m);
    680 	schednetisr(isr);
    681 	ifp->if_ipackets++;
    682 	ifp->if_ibytes += m->m_pkthdr.len;
    683 	splx(s);
    684 }
    685 
    686 /* ARGSUSED */
    687 static void
    688 stf_rtrequest(cmd, rt, sa)
    689 	int cmd;
    690 	struct rtentry *rt;
    691 #if defined(__bsdi__) && _BSDI_VERSION >= 199802
    692 	struct rt_addrinfo *sa;
    693 #else
    694 	struct sockaddr *sa;
    695 #endif
    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