xref: /freebsd/sys/net/if_stf.c (revision a220d00e74dd245b4fca59c5eca0c53963686325)
1 /*	$FreeBSD$	*/
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 "opt_inet.h"
78 #include "opt_inet6.h"
79 
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/socket.h>
83 #include <sys/sockio.h>
84 #include <sys/mbuf.h>
85 #include <sys/errno.h>
86 #include <sys/kernel.h>
87 #include <sys/protosw.h>
88 #include <sys/queue.h>
89 #include <machine/bus.h>	/* XXX: Shouldn't really be required! */
90 #include <sys/rman.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 #define STF_MAXUNIT	0	/* only one is currently allowed */
122 
123 #define IN6_IS_ADDR_6TO4(x)	(ntohs((x)->s6_addr16[0]) == 0x2002)
124 #define GET_V4(x)	((struct in_addr *)(&(x)->s6_addr16[1]))
125 
126 struct stf_softc {
127 	struct ifnet	sc_if;	   /* common area */
128 	union {
129 		struct route  __sc_ro4;
130 		struct route_in6 __sc_ro6; /* just for safety */
131 	} __sc_ro46;
132 #define sc_ro	__sc_ro46.__sc_ro4
133 	const struct encaptab *encap_cookie;
134 	struct resource *r_unit;	/* resource allocated for this unit */
135 	LIST_ENTRY(stf_softc) sc_list;	/* all stf's are linked */
136 };
137 
138 LIST_HEAD(, stf_softc) stf_softc_list;
139 
140 static MALLOC_DEFINE(M_STF, STFNAME, "6to4 Tunnel Interface");
141 static struct rman stfunits[1];
142 static int ip_stf_ttl = 40;
143 
144 extern  struct domain inetdomain;
145 struct protosw in_stf_protosw =
146 { SOCK_RAW,	&inetdomain,	IPPROTO_IPV6,	PR_ATOMIC|PR_ADDR,
147   in_stf_input, rip_output,	0,		rip_ctloutput,
148   0,
149   0,            0,              0,              0,
150   &rip_usrreqs
151 };
152 
153 static int stfmodevent __P((module_t, int, void *));
154 static int stf_encapcheck __P((const struct mbuf *, int, int, void *));
155 static struct in6_ifaddr *stf_getsrcifa6 __P((struct ifnet *));
156 static int stf_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
157 	struct rtentry *));
158 static int stf_checkaddr4 __P((struct stf_softc *, struct in_addr *,
159 	struct ifnet *));
160 static int stf_checkaddr6 __P((struct stf_softc *, struct in6_addr *,
161 	struct ifnet *));
162 static void stf_rtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
163 static int stf_ioctl __P((struct ifnet *, u_long, caddr_t));
164 
165 int	stf_clone_create __P((struct if_clone *, int *));
166 void	stf_clone_destroy __P((struct ifnet *));
167 
168 struct if_clone stf_cloner =
169     IF_CLONE_INITIALIZER(STFNAME, stf_clone_create, stf_clone_destroy);
170 
171 int
172 stf_clone_create(ifc, unit)
173 	struct if_clone *ifc;
174 	int *unit;
175 {
176 	struct resource *r;
177 	struct stf_softc *sc;
178 
179 	if (*unit > STF_MAXUNIT)
180 		return (ENXIO);
181 
182 	if (*unit < 0) {
183 		 r = rman_reserve_resource(stfunits, 0, STF_MAXUNIT, 1,
184 		     RF_ALLOCATED | RF_ACTIVE, NULL);
185 		 if (r == NULL)
186 			return (ENOSPC);
187 		 *unit = rman_get_start(r);
188 	} else {
189 		r = rman_reserve_resource(stfunits, *unit, *unit, 1,
190 		    RF_ALLOCATED | RF_ACTIVE, NULL);
191 		if (r == NULL)
192 			 return (EEXIST);
193 	}
194 
195 	sc = malloc(sizeof(struct stf_softc), M_STF, M_WAIT);
196 	bzero(sc, sizeof(struct stf_softc));
197 
198 	sc->sc_if.if_name = STFNAME;
199 	sc->sc_if.if_unit = *unit;
200 	sc->r_unit = r;
201 
202 	sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
203 	    stf_encapcheck, &in_stf_protosw, sc);
204 	if (sc->encap_cookie == NULL) {
205 		printf("%s: attach failed\n", if_name(&sc->sc_if));
206 		free(sc, M_STF);
207 		return (ENOMEM);
208 	}
209 
210 	sc->sc_if.if_mtu    = IPV6_MMTU;
211 	sc->sc_if.if_flags  = 0;
212 	sc->sc_if.if_ioctl  = stf_ioctl;
213 	sc->sc_if.if_output = stf_output;
214 	sc->sc_if.if_type   = IFT_STF;
215 	sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
216 	if_attach(&sc->sc_if);
217 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
218 	LIST_INSERT_HEAD(&stf_softc_list, sc, sc_list);
219 	return (0);
220 }
221 
222 void
223 stf_clone_destroy(ifp)
224 	struct ifnet *ifp;
225 {
226 	int err;
227 	struct stf_softc *sc = (void *) ifp;
228 
229 	LIST_REMOVE(sc, sc_list);
230 	err = encap_detach(sc->encap_cookie);
231 	KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
232 	bpfdetach(ifp);
233 	if_detach(ifp);
234 
235 	err = rman_release_resource(sc->r_unit);
236 	KASSERT(err == 0, ("Unexpected error freeing resource"));
237 
238 	free(sc, M_STF);
239 }
240 
241 static int
242 stfmodevent(mod, type, data)
243 	module_t mod;
244 	int type;
245 	void *data;
246 {
247 	int err;
248 
249 	switch (type) {
250 	case MOD_LOAD:
251 		stfunits->rm_type = RMAN_ARRAY;
252 		stfunits->rm_descr = "configurable if_stf units";
253 		err = rman_init(stfunits);
254 		if (err != 0)
255 			return (err);
256 		err = rman_manage_region(stfunits, 0, STF_MAXUNIT);
257 		if (err != 0) {
258 			printf("%s: stfunits: rman_manage_region: Failed %d\n",
259 			    STFNAME, err);
260 			rman_fini(stfunits);
261 			return (err);
262 		}
263 		LIST_INIT(&stf_softc_list);
264 		if_clone_attach(&stf_cloner);
265 
266 		break;
267 	case MOD_UNLOAD:
268 		if_clone_detach(&stf_cloner);
269 
270 		while (!LIST_EMPTY(&stf_softc_list))
271 			stf_clone_destroy(&LIST_FIRST(&stf_softc_list)->sc_if);
272 
273 		err = rman_fini(stfunits);
274 		KASSERT(err == 0, ("Unexpected error freeing resource"));
275 
276 		break;
277 	}
278 
279 	return (0);
280 }
281 
282 static moduledata_t stf_mod = {
283 	"if_stf",
284 	stfmodevent,
285 	0
286 };
287 
288 DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
289 
290 static int
291 stf_encapcheck(m, off, proto, arg)
292 	const struct mbuf *m;
293 	int off;
294 	int proto;
295 	void *arg;
296 {
297 	struct ip ip;
298 	struct in6_ifaddr *ia6;
299 	struct stf_softc *sc;
300 	struct in_addr a, b;
301 
302 	sc = (struct stf_softc *)arg;
303 	if (sc == NULL)
304 		return 0;
305 
306 	if ((sc->sc_if.if_flags & IFF_UP) == 0)
307 		return 0;
308 
309 	/* IFF_LINK0 means "no decapsulation" */
310 	if ((sc->sc_if.if_flags & IFF_LINK0) != 0)
311 		return 0;
312 
313 	if (proto != IPPROTO_IPV6)
314 		return 0;
315 
316 	/* LINTED const cast */
317 	m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
318 
319 	if (ip.ip_v != 4)
320 		return 0;
321 
322 	ia6 = stf_getsrcifa6(&sc->sc_if);
323 	if (ia6 == NULL)
324 		return 0;
325 
326 	/*
327 	 * check if IPv4 dst matches the IPv4 address derived from the
328 	 * local 6to4 address.
329 	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
330 	 */
331 	if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
332 	    sizeof(ip.ip_dst)) != 0)
333 		return 0;
334 
335 	/*
336 	 * check if IPv4 src matches the IPv4 address derived from the
337 	 * local 6to4 address masked by prefixmask.
338 	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
339 	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
340 	 */
341 	bzero(&a, sizeof(a));
342 	a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr;
343 	a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
344 	b = ip.ip_src;
345 	b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
346 	if (a.s_addr != b.s_addr)
347 		return 0;
348 
349 	/* stf interface makes single side match only */
350 	return 32;
351 }
352 
353 static struct in6_ifaddr *
354 stf_getsrcifa6(ifp)
355 	struct ifnet *ifp;
356 {
357 	struct ifaddr *ia;
358 	struct in_ifaddr *ia4;
359 	struct sockaddr_in6 *sin6;
360 	struct in_addr in;
361 
362 	for (ia = TAILQ_FIRST(&ifp->if_addrlist);
363 	     ia;
364 	     ia = TAILQ_NEXT(ia, ifa_list))
365 	{
366 		if (ia->ifa_addr == NULL)
367 			continue;
368 		if (ia->ifa_addr->sa_family != AF_INET6)
369 			continue;
370 		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
371 		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
372 			continue;
373 
374 		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
375 		LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
376 			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
377 				break;
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 	bzero(ip, 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_stf_ttl;
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 	return ip_output(m, NULL, &sc->sc_ro, 0, NULL);
510 }
511 
512 static int
513 stf_checkaddr4(sc, in, inifp)
514 	struct stf_softc *sc;
515 	struct in_addr *in;
516 	struct ifnet *inifp;	/* incoming interface */
517 {
518 	struct in_ifaddr *ia4;
519 
520 	/*
521 	 * reject packets with the following address:
522 	 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
523 	 */
524 	if (IN_MULTICAST(ntohl(in->s_addr)))
525 		return -1;
526 	switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
527 	case 0: case 127: case 255:
528 		return -1;
529 	}
530 
531 	/*
532 	 * reject packets with broadcast
533 	 */
534 	for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
535 	     ia4;
536 	     ia4 = TAILQ_NEXT(ia4, ia_link))
537 	{
538 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
539 			continue;
540 		if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
541 			return -1;
542 	}
543 
544 	/*
545 	 * perform ingress filter
546 	 */
547 	if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) {
548 		struct sockaddr_in sin;
549 		struct rtentry *rt;
550 
551 		bzero(&sin, sizeof(sin));
552 		sin.sin_family = AF_INET;
553 		sin.sin_len = sizeof(struct sockaddr_in);
554 		sin.sin_addr = *in;
555 		rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
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 in_stf_input(m, off)
598 	struct mbuf *m;
599 	int off;
600 {
601 	int proto;
602 	struct stf_softc *sc;
603 	struct ip *ip;
604 	struct ip6_hdr *ip6;
605 	u_int8_t otos, itos;
606 	int len, isr;
607 	struct ifqueue *ifq = NULL;
608 	struct ifnet *ifp;
609 
610 	proto = mtod(m, struct ip *)->ip_p;
611 
612 	if (proto != IPPROTO_IPV6) {
613 		m_freem(m);
614 		return;
615 	}
616 
617 	ip = mtod(m, struct ip *);
618 
619 	sc = (struct stf_softc *)encap_getarg(m);
620 
621 	if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
622 		m_freem(m);
623 		return;
624 	}
625 
626 	ifp = &sc->sc_if;
627 
628 	/*
629 	 * perform sanity check against outer src/dst.
630 	 * for source, perform ingress filter as well.
631 	 */
632 	if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
633 	    stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
634 		m_freem(m);
635 		return;
636 	}
637 
638 	otos = ip->ip_tos;
639 	m_adj(m, off);
640 
641 	if (m->m_len < sizeof(*ip6)) {
642 		m = m_pullup(m, sizeof(*ip6));
643 		if (!m)
644 			return;
645 	}
646 	ip6 = mtod(m, struct ip6_hdr *);
647 
648 	/*
649 	 * perform sanity check against inner src/dst.
650 	 * for source, perform ingress filter as well.
651 	 */
652 	if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
653 	    stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
654 		m_freem(m);
655 		return;
656 	}
657 
658 	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
659 	if ((ifp->if_flags & IFF_LINK1) != 0)
660 		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
661 	else
662 		ip_ecn_egress(ECN_NOCARE, &otos, &itos);
663 	ip6->ip6_flow &= ~htonl(0xff << 20);
664 	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
665 
666 	m->m_pkthdr.rcvif = ifp;
667 
668 	if (ifp->if_bpf) {
669 		/*
670 		 * We need to prepend the address family as
671 		 * a four byte field.  Cons up a dummy header
672 		 * to pacify bpf.  This is safe because bpf
673 		 * will only read from the mbuf (i.e., it won't
674 		 * try to free it or keep a pointer a to it).
675 		 */
676 		struct mbuf m0;
677 		u_int32_t af = AF_INET6;
678 
679 		m0.m_next = m;
680 		m0.m_len = 4;
681 		m0.m_data = (char *)&af;
682 
683 #ifdef HAVE_OLD_BPF
684 		bpf_mtap(ifp, &m0);
685 #else
686 		bpf_mtap(ifp->if_bpf, &m0);
687 #endif
688 	}
689 
690 	/*
691 	 * Put the packet to the network layer input queue according to the
692 	 * specified address family.
693 	 * See net/if_gif.c for possible issues with packet processing
694 	 * reorder due to extra queueing.
695 	 */
696 	ifq = &ip6intrq;
697 	isr = NETISR_IPV6;
698 
699 	len = m->m_pkthdr.len;
700 	if (! IF_HANDOFF(ifq, m, NULL))
701 		return;
702 	schednetisr(isr);
703 	ifp->if_ipackets++;
704 	ifp->if_ibytes += len;
705 }
706 
707 /* ARGSUSED */
708 static void
709 stf_rtrequest(cmd, rt, info)
710 	int cmd;
711 	struct rtentry *rt;
712 	struct rt_addrinfo *info;
713 {
714 
715 	if (rt)
716 		rt->rt_rmx.rmx_mtu = IPV6_MMTU;
717 }
718 
719 static int
720 stf_ioctl(ifp, cmd, data)
721 	struct ifnet *ifp;
722 	u_long cmd;
723 	caddr_t data;
724 {
725 	struct ifaddr *ifa;
726 	struct ifreq *ifr;
727 	struct sockaddr_in6 *sin6;
728 	int error;
729 
730 	error = 0;
731 	switch (cmd) {
732 	case SIOCSIFADDR:
733 		ifa = (struct ifaddr *)data;
734 		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
735 			error = EAFNOSUPPORT;
736 			break;
737 		}
738 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
739 		if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
740 			ifa->ifa_rtrequest = stf_rtrequest;
741 			ifp->if_flags |= IFF_UP;
742 		} else
743 			error = EINVAL;
744 		break;
745 
746 	case SIOCADDMULTI:
747 	case SIOCDELMULTI:
748 		ifr = (struct ifreq *)data;
749 		if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
750 			;
751 		else
752 			error = EAFNOSUPPORT;
753 		break;
754 
755 	default:
756 		error = EINVAL;
757 		break;
758 	}
759 
760 	return error;
761 }
762