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