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