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