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