if_stf.c revision 108710
1/*	$FreeBSD: head/sys/net/if_stf.c 108710 2003-01-05 14:03:26Z fenner $	*/
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
125struct 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
136static LIST_HEAD(, stf_softc) stf_softc_list;
137
138static MALLOC_DEFINE(M_STF, STFNAME, "6to4 Tunnel Interface");
139static int ip_stf_ttl = 40;
140
141extern  struct domain inetdomain;
142struct 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
150static int stfmodevent(module_t, int, void *);
151static int stf_encapcheck(const struct mbuf *, int, int, void *);
152static struct in6_ifaddr *stf_getsrcifa6(struct ifnet *);
153static int stf_output(struct ifnet *, struct mbuf *, struct sockaddr *,
154	struct rtentry *);
155static int isrfc1918addr(struct in_addr *);
156static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
157	struct ifnet *);
158static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
159	struct ifnet *);
160static void stf_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
161static int stf_ioctl(struct ifnet *, u_long, caddr_t);
162
163int	stf_clone_create(struct if_clone *, int);
164void	stf_clone_destroy(struct ifnet *);
165
166/* only one clone is currently allowed */
167struct if_clone stf_cloner =
168    IF_CLONE_INITIALIZER(STFNAME, stf_clone_create, stf_clone_destroy, 0, 0);
169
170int
171stf_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
200void
201stf_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
216static int
217stfmodevent(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
240static moduledata_t stf_mod = {
241	"if_stf",
242	stfmodevent,
243	0
244};
245
246DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
247
248static int
249stf_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, mask;
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	bcopy(GET_V4(&ia6->ia_addr.sin6_addr), &a, sizeof(a));
301	bcopy(GET_V4(&ia6->ia_prefixmask.sin6_addr), &mask, sizeof(mask));
302	a.s_addr &= mask.s_addr;
303	b = ip.ip_src;
304	b.s_addr &= mask.s_addr;
305	if (a.s_addr != b.s_addr)
306		return 0;
307
308	/* stf interface makes single side match only */
309	return 32;
310}
311
312static struct in6_ifaddr *
313stf_getsrcifa6(ifp)
314	struct ifnet *ifp;
315{
316	struct ifaddr *ia;
317	struct in_ifaddr *ia4;
318	struct sockaddr_in6 *sin6;
319	struct in_addr in;
320
321	for (ia = TAILQ_FIRST(&ifp->if_addrlist);
322	     ia;
323	     ia = TAILQ_NEXT(ia, ifa_list))
324	{
325		if (ia->ifa_addr == NULL)
326			continue;
327		if (ia->ifa_addr->sa_family != AF_INET6)
328			continue;
329		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
330		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
331			continue;
332
333		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
334		LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
335			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
336				break;
337		if (ia4 == NULL)
338			continue;
339
340		return (struct in6_ifaddr *)ia;
341	}
342
343	return NULL;
344}
345
346static int
347stf_output(ifp, m, dst, rt)
348	struct ifnet *ifp;
349	struct mbuf *m;
350	struct sockaddr *dst;
351	struct rtentry *rt;
352{
353	struct stf_softc *sc;
354	struct sockaddr_in6 *dst6;
355	struct in_addr *in4;
356	struct sockaddr_in *dst4;
357	u_int8_t tos;
358	struct ip *ip;
359	struct ip6_hdr *ip6;
360	struct in6_ifaddr *ia6;
361#ifdef MAC
362	int error;
363
364	error = mac_check_ifnet_transmit(ifp, m);
365	if (error) {
366		m_freem(m);
367		return (error);
368	}
369#endif
370
371	sc = (struct stf_softc*)ifp;
372	dst6 = (struct sockaddr_in6 *)dst;
373
374	/* just in case */
375	if ((ifp->if_flags & IFF_UP) == 0) {
376		m_freem(m);
377		ifp->if_oerrors++;
378		return ENETDOWN;
379	}
380
381	/*
382	 * If we don't have an ip4 address that match my inner ip6 address,
383	 * we shouldn't generate output.  Without this check, we'll end up
384	 * using wrong IPv4 source.
385	 */
386	ia6 = stf_getsrcifa6(ifp);
387	if (ia6 == NULL) {
388		m_freem(m);
389		ifp->if_oerrors++;
390		return ENETDOWN;
391	}
392
393	if (m->m_len < sizeof(*ip6)) {
394		m = m_pullup(m, sizeof(*ip6));
395		if (!m) {
396			ifp->if_oerrors++;
397			return ENOBUFS;
398		}
399	}
400	ip6 = mtod(m, struct ip6_hdr *);
401	tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
402
403	/*
404	 * Pickup the right outer dst addr from the list of candidates.
405	 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
406	 */
407	if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
408		in4 = GET_V4(&ip6->ip6_dst);
409	else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
410		in4 = GET_V4(&dst6->sin6_addr);
411	else {
412		m_freem(m);
413		ifp->if_oerrors++;
414		return ENETUNREACH;
415	}
416
417#if NBPFILTER > 0
418	if (ifp->if_bpf) {
419		/*
420		 * We need to prepend the address family as
421		 * a four byte field.  Cons up a dummy header
422		 * to pacify bpf.  This is safe because bpf
423		 * will only read from the mbuf (i.e., it won't
424		 * try to free it or keep a pointer a to it).
425		 */
426		struct mbuf m0;
427		u_int32_t af = AF_INET6;
428
429		m0.m_next = m;
430		m0.m_len = 4;
431		m0.m_data = (char *)&af;
432
433#ifdef HAVE_OLD_BPF
434		BPF_MTAP(ifp, &m0);
435#else
436		bpf_mtap(ifp->if_bpf, &m0);
437#endif
438	}
439#endif /*NBPFILTER > 0*/
440
441	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
442	if (m && m->m_len < sizeof(struct ip))
443		m = m_pullup(m, sizeof(struct ip));
444	if (m == NULL) {
445		ifp->if_oerrors++;
446		return ENOBUFS;
447	}
448	ip = mtod(m, struct ip *);
449
450	bzero(ip, sizeof(*ip));
451
452	bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
453	    &ip->ip_src, sizeof(ip->ip_src));
454	bcopy(in4, &ip->ip_dst, sizeof(ip->ip_dst));
455	ip->ip_p = IPPROTO_IPV6;
456	ip->ip_ttl = ip_stf_ttl;
457	ip->ip_len = m->m_pkthdr.len;	/*host order*/
458	if (ifp->if_flags & IFF_LINK1)
459		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
460	else
461		ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
462
463	dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
464	if (dst4->sin_family != AF_INET ||
465	    bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
466		/* cache route doesn't match */
467		dst4->sin_family = AF_INET;
468		dst4->sin_len = sizeof(struct sockaddr_in);
469		bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
470		if (sc->sc_ro.ro_rt) {
471			RTFREE(sc->sc_ro.ro_rt);
472			sc->sc_ro.ro_rt = NULL;
473		}
474	}
475
476	if (sc->sc_ro.ro_rt == NULL) {
477		rtalloc(&sc->sc_ro);
478		if (sc->sc_ro.ro_rt == NULL) {
479			m_freem(m);
480			ifp->if_oerrors++;
481			return ENETUNREACH;
482		}
483	}
484
485	ifp->if_opackets++;
486	return ip_output(m, NULL, &sc->sc_ro, 0, NULL, NULL);
487}
488
489static int
490isrfc1918addr(in)
491	struct in_addr *in;	/* 16-bit aligned */
492{
493	u_char *a = (u_char *)in;
494
495	/*
496	 * returns 1 if private address range:
497	 * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
498	 */
499	if (a[0] == 10 ||
500	    (a[0] == 172 && (a[1] & 0xf0) == 16) ||
501	    (a[1] == 192 && a[1] == 168))
502		return 1;
503
504	return 0;
505}
506
507static int
508stf_checkaddr4(sc, in, inifp)
509	struct stf_softc *sc;
510	struct in_addr *in;	/* 16-bit aligned */
511	struct ifnet *inifp;	/* incoming interface */
512{
513	struct in_ifaddr *ia4;
514	u_char *a = (u_char *)in;
515
516	/*
517	 * reject packets with the following address:
518	 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
519	 */
520	if ((a[0] & 0xf0) == 0xe0)
521		return -1;
522	if (a[0] == 0 || a[0] == 127 || a[0] == 255)
523		return -1;
524
525	/*
526	 * reject packets with private address range.
527	 * (requirement from RFC3056 section 2 1st paragraph)
528	 */
529	if (isrfc1918addr(in))
530		return -1;
531
532	/*
533	 * reject packets with broadcast
534	 */
535	for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
536	     ia4;
537	     ia4 = TAILQ_NEXT(ia4, ia_link))
538	{
539		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
540			continue;
541		if (bcmp(in, &ia4->ia_broadaddr.sin_addr, sizeof(*in)) == 0)
542			return -1;
543	}
544
545	/*
546	 * perform ingress filter
547	 */
548	if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) {
549		struct sockaddr_in sin;
550		struct rtentry *rt;
551
552		bzero(&sin, sizeof(sin));
553		sin.sin_family = AF_INET;
554		sin.sin_len = sizeof(struct sockaddr_in);
555		bcopy(in, &sin.sin_addr, sizeof(*in));
556		rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
557		if (!rt || rt->rt_ifp != inifp) {
558#if 0
559			log(LOG_WARNING, "%s: packet from 0x%x dropped "
560			    "due to ingress filter\n", if_name(&sc->sc_if),
561			    (u_int32_t)ntohl(sin.sin_addr.s_addr));
562#endif
563			if (rt)
564				rtfree(rt);
565			return -1;
566		}
567		rtfree(rt);
568	}
569
570	return 0;
571}
572
573static int
574stf_checkaddr6(sc, in6, inifp)
575	struct stf_softc *sc;
576	struct in6_addr *in6;
577	struct ifnet *inifp;	/* incoming interface */
578{
579	/*
580	 * check 6to4 addresses
581	 */
582	if (IN6_IS_ADDR_6TO4(in6))
583		return stf_checkaddr4(sc, GET_V4(in6), inifp);
584
585	/*
586	 * reject anything that look suspicious.  the test is implemented
587	 * in ip6_input too, but we check here as well to
588	 * (1) reject bad packets earlier, and
589	 * (2) to be safe against future ip6_input change.
590	 */
591	if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
592		return -1;
593
594	return 0;
595}
596
597void
598in_stf_input(m, off)
599	struct mbuf *m;
600	int off;
601{
602	int proto;
603	struct stf_softc *sc;
604	struct ip *ip;
605	struct ip6_hdr *ip6;
606	u_int8_t otos, itos;
607	int len, isr;
608	struct ifqueue *ifq = NULL;
609	struct ifnet *ifp;
610
611	proto = mtod(m, struct ip *)->ip_p;
612
613	if (proto != IPPROTO_IPV6) {
614		m_freem(m);
615		return;
616	}
617
618	ip = mtod(m, struct ip *);
619
620	sc = (struct stf_softc *)encap_getarg(m);
621
622	if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
623		m_freem(m);
624		return;
625	}
626
627	ifp = &sc->sc_if;
628
629#ifdef MAC
630	mac_create_mbuf_from_ifnet(ifp, m);
631#endif
632
633	/*
634	 * perform sanity check against outer src/dst.
635	 * for source, perform ingress filter as well.
636	 */
637	if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
638	    stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
639		m_freem(m);
640		return;
641	}
642
643	otos = ip->ip_tos;
644	m_adj(m, off);
645
646	if (m->m_len < sizeof(*ip6)) {
647		m = m_pullup(m, sizeof(*ip6));
648		if (!m)
649			return;
650	}
651	ip6 = mtod(m, struct ip6_hdr *);
652
653	/*
654	 * perform sanity check against inner src/dst.
655	 * for source, perform ingress filter as well.
656	 */
657	if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
658	    stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
659		m_freem(m);
660		return;
661	}
662
663	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
664	if ((ifp->if_flags & IFF_LINK1) != 0)
665		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
666	else
667		ip_ecn_egress(ECN_NOCARE, &otos, &itos);
668	ip6->ip6_flow &= ~htonl(0xff << 20);
669	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
670
671	m->m_pkthdr.rcvif = ifp;
672
673	if (ifp->if_bpf) {
674		/*
675		 * We need to prepend the address family as
676		 * a four byte field.  Cons up a dummy header
677		 * to pacify bpf.  This is safe because bpf
678		 * will only read from the mbuf (i.e., it won't
679		 * try to free it or keep a pointer a to it).
680		 */
681		struct mbuf m0;
682		u_int32_t af = AF_INET6;
683
684		m0.m_next = m;
685		m0.m_len = 4;
686		m0.m_data = (char *)&af;
687
688#ifdef HAVE_OLD_BPF
689		BPF_MTAP(ifp, &m0);
690#else
691		bpf_mtap(ifp->if_bpf, &m0);
692#endif
693	}
694
695	/*
696	 * Put the packet to the network layer input queue according to the
697	 * specified address family.
698	 * See net/if_gif.c for possible issues with packet processing
699	 * reorder due to extra queueing.
700	 */
701	ifq = &ip6intrq;
702	isr = NETISR_IPV6;
703
704	len = m->m_pkthdr.len;
705	if (! IF_HANDOFF(ifq, m, NULL))
706		return;
707	schednetisr(isr);
708	ifp->if_ipackets++;
709	ifp->if_ibytes += len;
710}
711
712/* ARGSUSED */
713static void
714stf_rtrequest(cmd, rt, info)
715	int cmd;
716	struct rtentry *rt;
717	struct rt_addrinfo *info;
718{
719
720	if (rt)
721		rt->rt_rmx.rmx_mtu = IPV6_MMTU;
722}
723
724static int
725stf_ioctl(ifp, cmd, data)
726	struct ifnet *ifp;
727	u_long cmd;
728	caddr_t data;
729{
730	struct ifaddr *ifa;
731	struct ifreq *ifr;
732	struct sockaddr_in6 *sin6;
733	int error;
734
735	error = 0;
736	switch (cmd) {
737	case SIOCSIFADDR:
738		ifa = (struct ifaddr *)data;
739		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
740			error = EAFNOSUPPORT;
741			break;
742		}
743		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
744		if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr) &&
745		    !isrfc1918addr(GET_V4(&sin6->sin6_addr))) {
746			ifa->ifa_rtrequest = stf_rtrequest;
747			ifp->if_flags |= IFF_UP;
748		} else
749			error = EINVAL;
750		break;
751
752	case SIOCADDMULTI:
753	case SIOCDELMULTI:
754		ifr = (struct ifreq *)data;
755		if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
756			;
757		else
758			error = EAFNOSUPPORT;
759		break;
760
761	default:
762		error = EINVAL;
763		break;
764	}
765
766	return error;
767}
768