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