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