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