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