xref: /netbsd-src/sys/net/if_stf.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: if_stf.c,v 1.5 2000/07/05 17:08:18 thorpej Exp $	*/
2 /*	$KAME: if_stf.c,v 1.39 2000/06/07 23:35:18 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 2000 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * 6to4 interface, based on draft-ietf-ngtrans-6to4-06.txt.
35  *
36  * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
37  * There is no address mapping defined from IPv6 multicast address to IPv4
38  * address.  Therefore, we do not have IFF_MULTICAST on the interface.
39  *
40  * Due to the lack of address mapping for link-local addresses, we cannot
41  * throw packets toward link-local addresses (fe80::x).  Also, we cannot throw
42  * packets to link-local multicast addresses (ff02::x).
43  *
44  * Here are interesting symptoms due to the lack of link-local address:
45  *
46  * Unicast routing exchange:
47  * - RIPng: Impossible.  Uses link-local multicast packet toward ff02::9,
48  *   and link-local addresses as nexthop.
49  * - OSPFv6: Impossible.  OSPFv6 assumes that there's link-local address
50  *   assigned to the link, and makes use of them.  Also, HELLO packets use
51  *   link-local multicast addresses (ff02::5 and ff02::6).
52  * - BGP4+: Maybe.  You can only use global address as nexthop, and global
53  *   address as TCP endpoint address.
54  *
55  * Multicast routing protocols:
56  * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
57  *   Adjacent PIM routers must be configured manually (is it really spec-wise
58  *   correct thing to do?).
59  *
60  * ICMPv6:
61  * - Redirects cannot be used due to the lack of link-local address.
62  *
63  * Starting from 04 draft, the specification suggests how to construct
64  * link-local address for 6to4 interface.
65  * However, it seems to have no real use and does not help the above symptom
66  * much.  Even if we assign link-locals to interface, we cannot really
67  * use link-local unicast/multicast on top of 6to4 cloud, and the above
68  * analysis does not change.
69  *
70  * 6to4 interface has security issues.  Refer to
71  * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
72  * for details.  The code tries to filter out some of malicious packets.
73  * Note that there is no way to be 100% secure.
74  */
75 
76 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
77 #include "opt_inet.h"
78 #include "opt_inet6.h"
79 #endif
80 #ifdef __NetBSD__
81 #include "opt_inet.h"
82 #endif
83 
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/socket.h>
87 #include <sys/sockio.h>
88 #include <sys/mbuf.h>
89 #include <sys/errno.h>
90 #if !(defined(__FreeBSD__) && __FreeBSD__ >= 3)
91 #include <sys/ioctl.h>
92 #endif
93 #include <sys/protosw.h>
94 #ifdef __FreeBSD__
95 #include <sys/kernel.h>
96 #endif
97 #include <sys/queue.h>
98 
99 #include <machine/cpu.h>
100 
101 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
102 #include <sys/malloc.h>
103 #endif
104 
105 #include <net/if.h>
106 #include <net/route.h>
107 #include <net/netisr.h>
108 #include <net/if_types.h>
109 #include <net/if_stf.h>
110 
111 #include <netinet/in.h>
112 #include <netinet/in_systm.h>
113 #include <netinet/ip.h>
114 #include <netinet/ip_var.h>
115 #include <netinet/in_var.h>
116 
117 #include <netinet/ip6.h>
118 #include <netinet6/ip6_var.h>
119 #include <netinet6/in6_gif.h>
120 #include <netinet6/in6_var.h>
121 #include <netinet/ip_ecn.h>
122 
123 #include <netinet/ip_encap.h>
124 
125 #include <machine/stdarg.h>
126 
127 #include <net/net_osdep.h>
128 
129 #if defined(__FreeBSD__) && __FreeBSD__ >= 4
130 #include "bpf.h"
131 #define NBPFILTER	NBPF
132 #else
133 #include "bpfilter.h"
134 #endif
135 #include "stf.h"
136 #include "gif.h"	/*XXX*/
137 
138 #if NBPFILTER > 0
139 #include <net/bpf.h>
140 #endif
141 
142 #if NGIF > 0
143 #include <net/if_gif.h>
144 #endif
145 
146 #if NSTF > 0
147 
148 #define IN6_IS_ADDR_6TO4(x)	(ntohs((x)->s6_addr16[0]) == 0x2002)
149 #define GET_V4(x)	((struct in_addr *)(&(x)->s6_addr16[1]))
150 
151 struct stf_softc {
152 	struct ifnet	sc_if;	   /* common area */
153 	union {
154 		struct route  __sc_ro4;
155 		struct route_in6 __sc_ro6; /* just for safety */
156 	} __sc_ro46;
157 #define sc_ro	__sc_ro46.__sc_ro4
158 	const struct encaptab *encap_cookie;
159 	LIST_ENTRY(stf_softc) sc_list;
160 };
161 
162 LIST_HEAD(, stf_softc) stf_softc_list;
163 
164 int	stf_clone_create __P((struct if_clone *, int));
165 void	stf_clone_destroy __P((struct ifnet *));
166 
167 struct if_clone stf_cloner =
168     IF_CLONE_INITIALIZER("stf", stf_clone_create, stf_clone_destroy);
169 
170 #if NGIF > 0
171 extern int ip_gif_ttl;	/*XXX*/
172 #else
173 static int ip_gif_ttl = 40;	/*XXX*/
174 #endif
175 
176 extern struct protosw in_stf_protosw;
177 
178 #ifdef __FreeBSD__
179 void stfattach __P((void *));
180 #else
181 void stfattach __P((int));
182 #endif
183 static int stf_encapcheck __P((const struct mbuf *, int, int, void *));
184 static struct in6_ifaddr *stf_getsrcifa6 __P((struct ifnet *));
185 static int stf_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
186 	struct rtentry *));
187 static int stf_checkaddr4 __P((struct in_addr *, struct ifnet *));
188 static int stf_checkaddr6 __P((struct in6_addr *, struct ifnet *));
189 #if defined(__bsdi__) && _BSDI_VERSION >= 199802
190 static void stf_rtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
191 #else
192 static void stf_rtrequest __P((int, struct rtentry *, struct sockaddr *));
193 #endif
194 #if defined(__FreeBSD__) && __FreeBSD__ < 3
195 static int stf_ioctl __P((struct ifnet *, int, caddr_t));
196 #else
197 static int stf_ioctl __P((struct ifnet *, u_long, caddr_t));
198 #endif
199 
200 /* ARGSUSED */
201 void
202 stfattach(count)
203 	int count;
204 {
205 
206 	LIST_INIT(&stf_softc_list);
207 	if_clone_attach(&stf_cloner);
208 }
209 
210 int
211 stf_clone_create(ifc, unit)
212 	struct if_clone *ifc;
213 	int unit;
214 {
215 	struct stf_softc *sc;
216 
217 	if (LIST_FIRST(&stf_softc_list) != NULL) {
218 		/* Only one stf interface is allowed. */
219 		return (EEXIST);
220 	}
221 
222 	sc = malloc(sizeof(struct stf_softc), M_DEVBUF, M_WAIT);
223 	bzero(sc, sizeof(struct stf_softc));
224 
225 	sprintf(sc->sc_if.if_xname, "%s%d", ifc->ifc_name, unit);
226 
227 	sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
228 	    stf_encapcheck, &in_stf_protosw, sc);
229 	if (sc->encap_cookie == NULL) {
230 		printf("%s: unable to attach encap\n", if_name(&sc->sc_if));
231 		free(sc, M_DEVBUF);
232 		return (EIO);	/* XXX */
233 	}
234 
235 	sc->sc_if.if_mtu    = IPV6_MMTU;
236 	sc->sc_if.if_flags  = 0;
237 	sc->sc_if.if_ioctl  = stf_ioctl;
238 	sc->sc_if.if_output = stf_output;
239 	sc->sc_if.if_type   = IFT_STF;
240 #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 NBPFILTER > 0
245 #ifdef HAVE_OLD_BPF
246 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
247 #else
248 	bpfattach(&sc->sc_if.if_bpf, &sc->sc_if, DLT_NULL, sizeof(u_int));
249 #endif
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 	if (proto != IPPROTO_IPV6)
294 		return 0;
295 
296 	/* LINTED const cast */
297 	m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
298 
299 	if (ip.ip_v != 4)
300 		return 0;
301 
302 	ia6 = stf_getsrcifa6(&sc->sc_if);
303 	if (ia6 == NULL)
304 		return 0;
305 
306 	/*
307 	 * check if IPv4 dst matches the IPv4 address derived from the
308 	 * local 6to4 address.
309 	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
310 	 */
311 	if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
312 	    sizeof(ip.ip_dst)) != 0)
313 		return 0;
314 
315 	/*
316 	 * check if IPv4 src matches the IPv4 address derived from the
317 	 * local 6to4 address masked by prefixmask.
318 	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
319 	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
320 	 */
321 	bzero(&a, sizeof(a));
322 	a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr;
323 	a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
324 	b = ip.ip_src;
325 	b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
326 	if (a.s_addr != b.s_addr)
327 		return 0;
328 
329 	/* stf interface makes single side match only */
330 	return 32;
331 }
332 
333 static struct in6_ifaddr *
334 stf_getsrcifa6(ifp)
335 	struct ifnet *ifp;
336 {
337 	struct ifaddr *ia;
338 	struct in_ifaddr *ia4;
339 	struct sockaddr_in6 *sin6;
340 	struct in_addr in;
341 
342 #if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3)
343 	for (ia = ifp->if_addrlist; ia; ia = ia->ifa_next)
344 #else
345 	for (ia = ifp->if_addrlist.tqh_first;
346 	     ia;
347 	     ia = ia->ifa_list.tqe_next)
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 = in_ifaddr.tqh_first;
364 		     ia4;
365 		     ia4 = ia4->ia_list.tqe_next)
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 sockaddr_in *dst4;
397 	u_int8_t tos;
398 	struct ip *ip;
399 	struct ip6_hdr *ip6;
400 	struct in6_ifaddr *ia6;
401 
402 	sc = (struct stf_softc*)ifp;
403 	dst6 = (struct sockaddr_in6 *)dst;
404 
405 	/* just in case */
406 	if ((ifp->if_flags & IFF_UP) == 0) {
407 		m_freem(m);
408 		return ENETDOWN;
409 	}
410 
411 	/*
412 	 * If we don't have an ip4 address that match my inner ip6 address,
413 	 * we shouldn't generate output.  Without this check, we'll end up
414 	 * using wrong IPv4 source.
415 	 */
416 	ia6 = stf_getsrcifa6(ifp);
417 	if (ia6 == NULL) {
418 		m_freem(m);
419 		return ENETDOWN;
420 	}
421 
422 	if (m->m_len < sizeof(*ip6)) {
423 		m = m_pullup(m, sizeof(*ip6));
424 		if (!m)
425 			return ENOBUFS;
426 	}
427 	ip6 = mtod(m, struct ip6_hdr *);
428 	tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
429 
430 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
431 	if (m && m->m_len < sizeof(struct ip))
432 		m = m_pullup(m, sizeof(struct ip));
433 	if (m == NULL)
434 		return ENOBUFS;
435 	ip = mtod(m, struct ip *);
436 
437 	bzero(ip, sizeof(*ip));
438 
439 	bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
440 	    &ip->ip_src, sizeof(ip->ip_src));
441 	bcopy(GET_V4(&dst6->sin6_addr), &ip->ip_dst, sizeof(ip->ip_dst));
442 	ip->ip_p = IPPROTO_IPV6;
443 	ip->ip_ttl = ip_gif_ttl;	/*XXX*/
444 	ip->ip_len = m->m_pkthdr.len;	/*host order*/
445 	if (ifp->if_flags & IFF_LINK1)
446 		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
447 
448 	dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
449 	if (dst4->sin_family != AF_INET ||
450 	    bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
451 		/* cache route doesn't match */
452 		dst4->sin_family = AF_INET;
453 		dst4->sin_len = sizeof(struct sockaddr_in);
454 		bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
455 		if (sc->sc_ro.ro_rt) {
456 			RTFREE(sc->sc_ro.ro_rt);
457 			sc->sc_ro.ro_rt = NULL;
458 		}
459 	}
460 
461 	if (sc->sc_ro.ro_rt == NULL) {
462 		rtalloc(&sc->sc_ro);
463 		if (sc->sc_ro.ro_rt == NULL) {
464 			m_freem(m);
465 			return ENETUNREACH;
466 		}
467 	}
468 
469 #ifndef __OpenBSD__
470 	return ip_output(m, NULL, &sc->sc_ro, 0, NULL);
471 #else
472 	return ip_output(m, NULL, &sc->sc_ro, 0, NULL, NULL);
473 #endif
474 }
475 
476 static int
477 stf_checkaddr4(in, ifp)
478 	struct in_addr *in;
479 	struct ifnet *ifp;	/* incoming interface */
480 {
481 	struct in_ifaddr *ia4;
482 
483 	/*
484 	 * reject packets with the following address:
485 	 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
486 	 */
487 	if (IN_MULTICAST(in->s_addr))
488 		return -1;
489 	switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
490 	case 0: case 127: case 255:
491 		return -1;
492 	}
493 
494 	/*
495 	 * reject packets with broadcast
496 	 */
497 #if defined(__OpenBSD__) || defined(__NetBSD__)
498 	for (ia4 = in_ifaddr.tqh_first; ia4; ia4 = ia4->ia_list.tqe_next)
499 #elif defined(__FreeBSD__) && __FreeBSD__ >= 3
500 	for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
501 	     ia4;
502 	     ia4 = TAILQ_NEXT(ia4, ia_link))
503 #else
504 	for (ia4 = in_ifaddr; ia4 != NULL; ia4 = ia4->ia_next)
505 #endif
506 	{
507 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
508 			continue;
509 		if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
510 			return -1;
511 	}
512 
513 	/*
514 	 * perform ingress filter
515 	 */
516 	if (ifp) {
517 		struct sockaddr_in sin;
518 		struct rtentry *rt;
519 
520 		bzero(&sin, sizeof(sin));
521 		sin.sin_family = AF_INET;
522 		sin.sin_len = sizeof(struct sockaddr_in);
523 		sin.sin_addr = *in;
524 #ifdef __FreeBSD__
525 		rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
526 #else
527 		rt = rtalloc1((struct sockaddr *)&sin, 0);
528 #endif
529 		if (!rt)
530 			return -1;
531 		if (rt->rt_ifp != ifp) {
532 			rtfree(rt);
533 			return -1;
534 		}
535 		rtfree(rt);
536 	}
537 
538 	return 0;
539 }
540 
541 static int
542 stf_checkaddr6(in6, ifp)
543 	struct in6_addr *in6;
544 	struct ifnet *ifp;	/* incoming interface */
545 {
546 	/*
547 	 * check 6to4 addresses
548 	 */
549 	if (IN6_IS_ADDR_6TO4(in6))
550 		return stf_checkaddr4(GET_V4(in6), ifp);
551 
552 	/*
553 	 * reject anything that look suspicious.  the test is implemented
554 	 * in ip6_input too, but we check here as well to
555 	 * (1) reject bad packets earlier, and
556 	 * (2) to be safe against future ip6_input change.
557 	 */
558 	if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
559 		return -1;
560 
561 	return 0;
562 }
563 
564 void
565 #if __STDC__
566 in_stf_input(struct mbuf *m, ...)
567 #else
568 in_stf_input(m, va_alist)
569 	register struct mbuf *m;
570 #endif
571 {
572 	int off, proto;
573 	struct stf_softc *sc;
574 	struct ip *ip;
575 	struct ip6_hdr *ip6;
576 	u_int8_t otos, itos;
577 	int s, isr;
578 	struct ifqueue *ifq = NULL;
579 	struct ifnet *ifp;
580 	va_list ap;
581 
582 	va_start(ap, m);
583 	off = va_arg(ap, int);
584 	proto = va_arg(ap, int);
585 	va_end(ap);
586 
587 	if (proto != IPPROTO_IPV6) {
588 		m_freem(m);
589 		return;
590 	}
591 
592 	ip = mtod(m, struct ip *);
593 
594 	sc = (struct stf_softc *)encap_getarg(m);
595 
596 	if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
597 		m_freem(m);
598 		return;
599 	}
600 
601 	ifp = &sc->sc_if;
602 
603 	/*
604 	 * perform sanity check against outer src/dst.
605 	 * for source, perform ingress filter as well.
606 	 */
607 	if (stf_checkaddr4(&ip->ip_dst, NULL) < 0 ||
608 	    stf_checkaddr4(&ip->ip_src, m->m_pkthdr.rcvif) < 0) {
609 		m_freem(m);
610 		return;
611 	}
612 
613 	otos = ip->ip_tos;
614 	m_adj(m, off);
615 
616 	if (m->m_len < sizeof(*ip6)) {
617 		m = m_pullup(m, sizeof(*ip6));
618 		if (!m)
619 			return;
620 	}
621 	ip6 = mtod(m, struct ip6_hdr *);
622 
623 	/*
624 	 * perform sanity check against inner src/dst.
625 	 * for source, perform ingress filter as well.
626 	 */
627 	if (stf_checkaddr6(&ip6->ip6_dst, NULL) < 0 ||
628 	    stf_checkaddr6(&ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
629 		m_freem(m);
630 		return;
631 	}
632 
633 	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
634 	if ((ifp->if_flags & IFF_LINK1) != 0)
635 		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
636 	ip6->ip6_flow &= ~htonl(0xff << 20);
637 	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
638 
639 	m->m_pkthdr.rcvif = ifp;
640 
641 #if NBPFILTER > 0
642 	if (ifp->if_bpf) {
643 		/*
644 		 * We need to prepend the address family as
645 		 * a four byte field.  Cons up a dummy header
646 		 * to pacify bpf.  This is safe because bpf
647 		 * will only read from the mbuf (i.e., it won't
648 		 * try to free it or keep a pointer a to it).
649 		 */
650 		struct mbuf m0;
651 		u_int af = AF_INET6;
652 
653 		m0.m_next = m;
654 		m0.m_len = 4;
655 		m0.m_data = (char *)&af;
656 
657 #ifdef HAVE_OLD_BPF
658 		bpf_mtap(ifp, &m0);
659 #else
660 		bpf_mtap(ifp->if_bpf, &m0);
661 #endif
662 	}
663 #endif /*NBPFILTER > 0*/
664 
665 	/*
666 	 * Put the packet to the network layer input queue according to the
667 	 * specified address family.
668 	 * See net/if_gif.c for possible issues with packet processing
669 	 * reorder due to extra queueing.
670 	 */
671 	ifq = &ip6intrq;
672 	isr = NETISR_IPV6;
673 
674 	s = splimp();
675 	if (IF_QFULL(ifq)) {
676 		IF_DROP(ifq);	/* update statistics */
677 		m_freem(m);
678 		splx(s);
679 		return;
680 	}
681 	IF_ENQUEUE(ifq, m);
682 	schednetisr(isr);
683 	ifp->if_ipackets++;
684 	ifp->if_ibytes += m->m_pkthdr.len;
685 	splx(s);
686 }
687 
688 /* ARGSUSED */
689 static void
690 stf_rtrequest(cmd, rt, sa)
691 	int cmd;
692 	struct rtentry *rt;
693 #if defined(__bsdi__) && _BSDI_VERSION >= 199802
694 	struct rt_addrinfo *sa;
695 #else
696 	struct sockaddr *sa;
697 #endif
698 {
699 
700 	if (rt)
701 		rt->rt_rmx.rmx_mtu = IPV6_MMTU;
702 }
703 
704 static int
705 stf_ioctl(ifp, cmd, data)
706 	struct ifnet *ifp;
707 #if defined(__FreeBSD__) && __FreeBSD__ < 3
708 	int cmd;
709 #else
710 	u_long cmd;
711 #endif
712 	caddr_t data;
713 {
714 	struct ifaddr *ifa;
715 	struct ifreq *ifr;
716 	struct sockaddr_in6 *sin6;
717 	int error;
718 
719 	error = 0;
720 	switch (cmd) {
721 	case SIOCSIFADDR:
722 		ifa = (struct ifaddr *)data;
723 		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
724 			error = EAFNOSUPPORT;
725 			break;
726 		}
727 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
728 		if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
729 			ifa->ifa_rtrequest = stf_rtrequest;
730 			ifp->if_flags |= IFF_UP;
731 		} else
732 			error = EINVAL;
733 		break;
734 
735 	case SIOCADDMULTI:
736 	case SIOCDELMULTI:
737 		ifr = (struct ifreq *)data;
738 		if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
739 			;
740 		else
741 			error = EAFNOSUPPORT;
742 		break;
743 
744 	default:
745 		error = EINVAL;
746 		break;
747 	}
748 
749 	return error;
750 }
751 
752 #endif /* NSTF > 0 */
753