xref: /freebsd/sys/net/if_stf.c (revision f0ffb944d259efbe7c4e223b4068be440c1a51cf)
1686cdd19SJun-ichiro itojun Hagino /*	$FreeBSD$	*/
28acb2290SHajimu UMEMOTO /*	$KAME: if_stf.c,v 1.62 2001/06/07 22:32:16 itojun Exp $	*/
3686cdd19SJun-ichiro itojun Hagino 
4686cdd19SJun-ichiro itojun Hagino /*
5686cdd19SJun-ichiro itojun Hagino  * Copyright (C) 2000 WIDE Project.
6686cdd19SJun-ichiro itojun Hagino  * All rights reserved.
7686cdd19SJun-ichiro itojun Hagino  *
8686cdd19SJun-ichiro itojun Hagino  * Redistribution and use in source and binary forms, with or without
9686cdd19SJun-ichiro itojun Hagino  * modification, are permitted provided that the following conditions
10686cdd19SJun-ichiro itojun Hagino  * are met:
11686cdd19SJun-ichiro itojun Hagino  * 1. Redistributions of source code must retain the above copyright
12686cdd19SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer.
13686cdd19SJun-ichiro itojun Hagino  * 2. Redistributions in binary form must reproduce the above copyright
14686cdd19SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer in the
15686cdd19SJun-ichiro itojun Hagino  *    documentation and/or other materials provided with the distribution.
16686cdd19SJun-ichiro itojun Hagino  * 3. Neither the name of the project nor the names of its contributors
17686cdd19SJun-ichiro itojun Hagino  *    may be used to endorse or promote products derived from this software
18686cdd19SJun-ichiro itojun Hagino  *    without specific prior written permission.
19686cdd19SJun-ichiro itojun Hagino  *
20686cdd19SJun-ichiro itojun Hagino  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21686cdd19SJun-ichiro itojun Hagino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22686cdd19SJun-ichiro itojun Hagino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23686cdd19SJun-ichiro itojun Hagino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24686cdd19SJun-ichiro itojun Hagino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25686cdd19SJun-ichiro itojun Hagino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26686cdd19SJun-ichiro itojun Hagino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27686cdd19SJun-ichiro itojun Hagino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28686cdd19SJun-ichiro itojun Hagino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29686cdd19SJun-ichiro itojun Hagino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30686cdd19SJun-ichiro itojun Hagino  * SUCH DAMAGE.
31686cdd19SJun-ichiro itojun Hagino  */
32686cdd19SJun-ichiro itojun Hagino 
33686cdd19SJun-ichiro itojun Hagino /*
3433841545SHajimu UMEMOTO  * 6to4 interface, based on RFC3056.
35686cdd19SJun-ichiro itojun Hagino  *
36686cdd19SJun-ichiro itojun Hagino  * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
37686cdd19SJun-ichiro itojun Hagino  * There is no address mapping defined from IPv6 multicast address to IPv4
38686cdd19SJun-ichiro itojun Hagino  * address.  Therefore, we do not have IFF_MULTICAST on the interface.
39686cdd19SJun-ichiro itojun Hagino  *
40686cdd19SJun-ichiro itojun Hagino  * Due to the lack of address mapping for link-local addresses, we cannot
41686cdd19SJun-ichiro itojun Hagino  * throw packets toward link-local addresses (fe80::x).  Also, we cannot throw
42686cdd19SJun-ichiro itojun Hagino  * packets to link-local multicast addresses (ff02::x).
43686cdd19SJun-ichiro itojun Hagino  *
44686cdd19SJun-ichiro itojun Hagino  * Here are interesting symptoms due to the lack of link-local address:
45686cdd19SJun-ichiro itojun Hagino  *
46686cdd19SJun-ichiro itojun Hagino  * Unicast routing exchange:
47686cdd19SJun-ichiro itojun Hagino  * - RIPng: Impossible.  Uses link-local multicast packet toward ff02::9,
48686cdd19SJun-ichiro itojun Hagino  *   and link-local addresses as nexthop.
49686cdd19SJun-ichiro itojun Hagino  * - OSPFv6: Impossible.  OSPFv6 assumes that there's link-local address
50686cdd19SJun-ichiro itojun Hagino  *   assigned to the link, and makes use of them.  Also, HELLO packets use
51686cdd19SJun-ichiro itojun Hagino  *   link-local multicast addresses (ff02::5 and ff02::6).
52686cdd19SJun-ichiro itojun Hagino  * - BGP4+: Maybe.  You can only use global address as nexthop, and global
53686cdd19SJun-ichiro itojun Hagino  *   address as TCP endpoint address.
54686cdd19SJun-ichiro itojun Hagino  *
55686cdd19SJun-ichiro itojun Hagino  * Multicast routing protocols:
56686cdd19SJun-ichiro itojun Hagino  * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
57686cdd19SJun-ichiro itojun Hagino  *   Adjacent PIM routers must be configured manually (is it really spec-wise
58686cdd19SJun-ichiro itojun Hagino  *   correct thing to do?).
59686cdd19SJun-ichiro itojun Hagino  *
60686cdd19SJun-ichiro itojun Hagino  * ICMPv6:
61686cdd19SJun-ichiro itojun Hagino  * - Redirects cannot be used due to the lack of link-local address.
62686cdd19SJun-ichiro itojun Hagino  *
6333841545SHajimu UMEMOTO  * stf interface does not have, and will not need, a link-local address.
6433841545SHajimu UMEMOTO  * It seems to have no real benefit and does not help the above symptoms much.
6533841545SHajimu UMEMOTO  * Even if we assign link-locals to interface, we cannot really
6633841545SHajimu UMEMOTO  * use link-local unicast/multicast on top of 6to4 cloud (since there's no
6733841545SHajimu UMEMOTO  * encapsulation defined for link-local address), and the above analysis does
6833841545SHajimu UMEMOTO  * not change.  RFC3056 does not mandate the assignment of link-local address
6933841545SHajimu UMEMOTO  * either.
70686cdd19SJun-ichiro itojun Hagino  *
71686cdd19SJun-ichiro itojun Hagino  * 6to4 interface has security issues.  Refer to
72686cdd19SJun-ichiro itojun Hagino  * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
73686cdd19SJun-ichiro itojun Hagino  * for details.  The code tries to filter out some of malicious packets.
74686cdd19SJun-ichiro itojun Hagino  * Note that there is no way to be 100% secure.
75686cdd19SJun-ichiro itojun Hagino  */
76686cdd19SJun-ichiro itojun Hagino 
77686cdd19SJun-ichiro itojun Hagino #include "opt_inet.h"
78686cdd19SJun-ichiro itojun Hagino #include "opt_inet6.h"
79686cdd19SJun-ichiro itojun Hagino 
80686cdd19SJun-ichiro itojun Hagino #include <sys/param.h>
81686cdd19SJun-ichiro itojun Hagino #include <sys/systm.h>
82686cdd19SJun-ichiro itojun Hagino #include <sys/socket.h>
83686cdd19SJun-ichiro itojun Hagino #include <sys/sockio.h>
84686cdd19SJun-ichiro itojun Hagino #include <sys/mbuf.h>
85686cdd19SJun-ichiro itojun Hagino #include <sys/errno.h>
86686cdd19SJun-ichiro itojun Hagino #include <sys/protosw.h>
87686cdd19SJun-ichiro itojun Hagino #include <sys/kernel.h>
88686cdd19SJun-ichiro itojun Hagino #include <machine/cpu.h>
89686cdd19SJun-ichiro itojun Hagino 
90686cdd19SJun-ichiro itojun Hagino #include <sys/malloc.h>
91686cdd19SJun-ichiro itojun Hagino 
92686cdd19SJun-ichiro itojun Hagino #include <net/if.h>
93686cdd19SJun-ichiro itojun Hagino #include <net/route.h>
94686cdd19SJun-ichiro itojun Hagino #include <net/netisr.h>
95686cdd19SJun-ichiro itojun Hagino #include <net/if_types.h>
96686cdd19SJun-ichiro itojun Hagino #include <net/if_stf.h>
97686cdd19SJun-ichiro itojun Hagino 
98686cdd19SJun-ichiro itojun Hagino #include <netinet/in.h>
99686cdd19SJun-ichiro itojun Hagino #include <netinet/in_systm.h>
100686cdd19SJun-ichiro itojun Hagino #include <netinet/ip.h>
101686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_var.h>
102686cdd19SJun-ichiro itojun Hagino #include <netinet/in_var.h>
103686cdd19SJun-ichiro itojun Hagino 
104686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
105686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6_var.h>
106686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_var.h>
107686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_ecn.h>
108686cdd19SJun-ichiro itojun Hagino 
109686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
110686cdd19SJun-ichiro itojun Hagino 
111686cdd19SJun-ichiro itojun Hagino #include <machine/stdarg.h>
112686cdd19SJun-ichiro itojun Hagino 
113686cdd19SJun-ichiro itojun Hagino #include <net/net_osdep.h>
114686cdd19SJun-ichiro itojun Hagino 
115686cdd19SJun-ichiro itojun Hagino #include <net/bpf.h>
116686cdd19SJun-ichiro itojun Hagino 
117686cdd19SJun-ichiro itojun Hagino #define IN6_IS_ADDR_6TO4(x)	(ntohs((x)->s6_addr16[0]) == 0x2002)
118686cdd19SJun-ichiro itojun Hagino #define GET_V4(x)	((struct in_addr *)(&(x)->s6_addr16[1]))
119686cdd19SJun-ichiro itojun Hagino 
120686cdd19SJun-ichiro itojun Hagino struct stf_softc {
121686cdd19SJun-ichiro itojun Hagino 	struct ifnet	sc_if;	   /* common area */
122686cdd19SJun-ichiro itojun Hagino 	union {
123686cdd19SJun-ichiro itojun Hagino 		struct route  __sc_ro4;
124686cdd19SJun-ichiro itojun Hagino 		struct route_in6 __sc_ro6; /* just for safety */
125686cdd19SJun-ichiro itojun Hagino 	} __sc_ro46;
126686cdd19SJun-ichiro itojun Hagino #define sc_ro	__sc_ro46.__sc_ro4
127686cdd19SJun-ichiro itojun Hagino 	const struct encaptab *encap_cookie;
128686cdd19SJun-ichiro itojun Hagino };
129686cdd19SJun-ichiro itojun Hagino 
130686cdd19SJun-ichiro itojun Hagino static struct stf_softc *stf;
131686cdd19SJun-ichiro itojun Hagino 
13253dab5feSBrooks Davis static MALLOC_DEFINE(M_STF, "stf", "6to4 Tunnel Interface");
13353dab5feSBrooks Davis static int ip_stf_ttl = 40;
134686cdd19SJun-ichiro itojun Hagino 
13553dab5feSBrooks Davis extern  struct domain inetdomain;
136f0ffb944SJulian Elischer struct protosw in_stf_protosw =
13753dab5feSBrooks Davis { SOCK_RAW,	&inetdomain,	IPPROTO_IPV6,	PR_ATOMIC|PR_ADDR,
13853dab5feSBrooks Davis   in_stf_input, rip_output,	0,		rip_ctloutput,
13953dab5feSBrooks Davis   0,
14053dab5feSBrooks Davis   0,            0,              0,              0,
14153dab5feSBrooks Davis   &rip_usrreqs
14253dab5feSBrooks Davis };
143686cdd19SJun-ichiro itojun Hagino 
14453dab5feSBrooks Davis static int stfmodevent __P((module_t, int, void *));
145686cdd19SJun-ichiro itojun Hagino static int stf_encapcheck __P((const struct mbuf *, int, int, void *));
146686cdd19SJun-ichiro itojun Hagino static struct in6_ifaddr *stf_getsrcifa6 __P((struct ifnet *));
147686cdd19SJun-ichiro itojun Hagino static int stf_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
148686cdd19SJun-ichiro itojun Hagino 	struct rtentry *));
14933841545SHajimu UMEMOTO static int stf_checkaddr4 __P((struct stf_softc *, struct in_addr *,
15033841545SHajimu UMEMOTO 	struct ifnet *));
15133841545SHajimu UMEMOTO static int stf_checkaddr6 __P((struct stf_softc *, struct in6_addr *,
15233841545SHajimu UMEMOTO 	struct ifnet *));
153686cdd19SJun-ichiro itojun Hagino static void stf_rtrequest __P((int, struct rtentry *, struct sockaddr *));
154686cdd19SJun-ichiro itojun Hagino static int stf_ioctl __P((struct ifnet *, u_long, caddr_t));
155686cdd19SJun-ichiro itojun Hagino 
15653dab5feSBrooks Davis static int
15753dab5feSBrooks Davis stfmodevent(mod, type, data)
15853dab5feSBrooks Davis 	module_t mod;
15953dab5feSBrooks Davis 	int type;
16053dab5feSBrooks Davis 	void *data;
161686cdd19SJun-ichiro itojun Hagino {
162686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
16353dab5feSBrooks Davis 	int err;
164686cdd19SJun-ichiro itojun Hagino 	const struct encaptab *p;
165686cdd19SJun-ichiro itojun Hagino 
16653dab5feSBrooks Davis 	switch (type) {
16753dab5feSBrooks Davis 	case MOD_LOAD:
16853dab5feSBrooks Davis 		stf = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK);
16953dab5feSBrooks Davis 		bzero(stf, sizeof(struct stf_softc));
170686cdd19SJun-ichiro itojun Hagino 		sc = stf;
171686cdd19SJun-ichiro itojun Hagino 
172686cdd19SJun-ichiro itojun Hagino 		bzero(sc, sizeof(*sc));
173686cdd19SJun-ichiro itojun Hagino 		sc->sc_if.if_name = "stf";
17453dab5feSBrooks Davis 		sc->sc_if.if_unit = 0;
175686cdd19SJun-ichiro itojun Hagino 
176686cdd19SJun-ichiro itojun Hagino 		p = encap_attach_func(AF_INET, IPPROTO_IPV6, stf_encapcheck,
177686cdd19SJun-ichiro itojun Hagino 		    &in_stf_protosw, sc);
178686cdd19SJun-ichiro itojun Hagino 		if (p == NULL) {
179686cdd19SJun-ichiro itojun Hagino 			printf("%s: attach failed\n", if_name(&sc->sc_if));
18053dab5feSBrooks Davis 			return (ENOMEM);
181686cdd19SJun-ichiro itojun Hagino 		}
182686cdd19SJun-ichiro itojun Hagino 		sc->encap_cookie = p;
183686cdd19SJun-ichiro itojun Hagino 
184686cdd19SJun-ichiro itojun Hagino 		sc->sc_if.if_mtu    = IPV6_MMTU;
185686cdd19SJun-ichiro itojun Hagino 		sc->sc_if.if_flags  = 0;
186686cdd19SJun-ichiro itojun Hagino 		sc->sc_if.if_ioctl  = stf_ioctl;
187686cdd19SJun-ichiro itojun Hagino 		sc->sc_if.if_output = stf_output;
188686cdd19SJun-ichiro itojun Hagino 		sc->sc_if.if_type   = IFT_STF;
18933841545SHajimu UMEMOTO #if 0
19033841545SHajimu UMEMOTO 		/* turn off ingress filter */
19133841545SHajimu UMEMOTO 		sc->sc_if.if_flags  |= IFF_LINK2;
19233841545SHajimu UMEMOTO #endif
193686cdd19SJun-ichiro itojun Hagino 		sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
194686cdd19SJun-ichiro itojun Hagino 		if_attach(&sc->sc_if);
195686cdd19SJun-ichiro itojun Hagino #ifdef HAVE_OLD_BPF
196686cdd19SJun-ichiro itojun Hagino 		bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
197686cdd19SJun-ichiro itojun Hagino #else
198686cdd19SJun-ichiro itojun Hagino 		bpfattach(&sc->sc_if.if_bpf, &sc->sc_if, DLT_NULL, sizeof(u_int));
199686cdd19SJun-ichiro itojun Hagino #endif
20053dab5feSBrooks Davis 		break;
20153dab5feSBrooks Davis 	case MOD_UNLOAD:
20253dab5feSBrooks Davis 		sc = stf;
20353dab5feSBrooks Davis 		bpfdetach(&sc->sc_if);
20453dab5feSBrooks Davis 		if_detach(&sc->sc_if);
20553dab5feSBrooks Davis 		err = encap_detach(sc->encap_cookie);
20653dab5feSBrooks Davis 		KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
20753dab5feSBrooks Davis 		free(sc, M_STF);
20853dab5feSBrooks Davis 		break;
209686cdd19SJun-ichiro itojun Hagino 	}
210686cdd19SJun-ichiro itojun Hagino 
21153dab5feSBrooks Davis 	return (0);
21253dab5feSBrooks Davis }
21353dab5feSBrooks Davis 
21453dab5feSBrooks Davis static moduledata_t stf_mod = {
21553dab5feSBrooks Davis 	"if_stf",
21653dab5feSBrooks Davis 	stfmodevent,
21753dab5feSBrooks Davis 	0
21853dab5feSBrooks Davis };
21953dab5feSBrooks Davis 
22053dab5feSBrooks Davis DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
221686cdd19SJun-ichiro itojun Hagino 
222686cdd19SJun-ichiro itojun Hagino static int
223686cdd19SJun-ichiro itojun Hagino stf_encapcheck(m, off, proto, arg)
224686cdd19SJun-ichiro itojun Hagino 	const struct mbuf *m;
225686cdd19SJun-ichiro itojun Hagino 	int off;
226686cdd19SJun-ichiro itojun Hagino 	int proto;
227686cdd19SJun-ichiro itojun Hagino 	void *arg;
228686cdd19SJun-ichiro itojun Hagino {
229686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
230686cdd19SJun-ichiro itojun Hagino 	struct in6_ifaddr *ia6;
231686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
232686cdd19SJun-ichiro itojun Hagino 	struct in_addr a, b;
233686cdd19SJun-ichiro itojun Hagino 
234686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc *)arg;
235686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
236686cdd19SJun-ichiro itojun Hagino 		return 0;
237686cdd19SJun-ichiro itojun Hagino 
238686cdd19SJun-ichiro itojun Hagino 	if ((sc->sc_if.if_flags & IFF_UP) == 0)
239686cdd19SJun-ichiro itojun Hagino 		return 0;
240686cdd19SJun-ichiro itojun Hagino 
24133841545SHajimu UMEMOTO 	/* IFF_LINK0 means "no decapsulation" */
24233841545SHajimu UMEMOTO 	if ((sc->sc_if.if_flags & IFF_LINK0) != 0)
24333841545SHajimu UMEMOTO 		return 0;
24433841545SHajimu UMEMOTO 
245686cdd19SJun-ichiro itojun Hagino 	if (proto != IPPROTO_IPV6)
246686cdd19SJun-ichiro itojun Hagino 		return 0;
247686cdd19SJun-ichiro itojun Hagino 
248686cdd19SJun-ichiro itojun Hagino 	/* LINTED const cast */
249686cdd19SJun-ichiro itojun Hagino 	m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
250686cdd19SJun-ichiro itojun Hagino 
251686cdd19SJun-ichiro itojun Hagino 	if (ip.ip_v != 4)
252686cdd19SJun-ichiro itojun Hagino 		return 0;
253686cdd19SJun-ichiro itojun Hagino 
254686cdd19SJun-ichiro itojun Hagino 	ia6 = stf_getsrcifa6(&sc->sc_if);
255686cdd19SJun-ichiro itojun Hagino 	if (ia6 == NULL)
256686cdd19SJun-ichiro itojun Hagino 		return 0;
257686cdd19SJun-ichiro itojun Hagino 
258686cdd19SJun-ichiro itojun Hagino 	/*
259686cdd19SJun-ichiro itojun Hagino 	 * check if IPv4 dst matches the IPv4 address derived from the
260686cdd19SJun-ichiro itojun Hagino 	 * local 6to4 address.
261686cdd19SJun-ichiro itojun Hagino 	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
262686cdd19SJun-ichiro itojun Hagino 	 */
263686cdd19SJun-ichiro itojun Hagino 	if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
264686cdd19SJun-ichiro itojun Hagino 	    sizeof(ip.ip_dst)) != 0)
265686cdd19SJun-ichiro itojun Hagino 		return 0;
266686cdd19SJun-ichiro itojun Hagino 
267686cdd19SJun-ichiro itojun Hagino 	/*
268686cdd19SJun-ichiro itojun Hagino 	 * check if IPv4 src matches the IPv4 address derived from the
269686cdd19SJun-ichiro itojun Hagino 	 * local 6to4 address masked by prefixmask.
270686cdd19SJun-ichiro itojun Hagino 	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
271686cdd19SJun-ichiro itojun Hagino 	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
272686cdd19SJun-ichiro itojun Hagino 	 */
273686cdd19SJun-ichiro itojun Hagino 	bzero(&a, sizeof(a));
274686cdd19SJun-ichiro itojun Hagino 	a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr;
275686cdd19SJun-ichiro itojun Hagino 	a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
276686cdd19SJun-ichiro itojun Hagino 	b = ip.ip_src;
277686cdd19SJun-ichiro itojun Hagino 	b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
278686cdd19SJun-ichiro itojun Hagino 	if (a.s_addr != b.s_addr)
279686cdd19SJun-ichiro itojun Hagino 		return 0;
280686cdd19SJun-ichiro itojun Hagino 
281686cdd19SJun-ichiro itojun Hagino 	/* stf interface makes single side match only */
282686cdd19SJun-ichiro itojun Hagino 	return 32;
283686cdd19SJun-ichiro itojun Hagino }
284686cdd19SJun-ichiro itojun Hagino 
285686cdd19SJun-ichiro itojun Hagino static struct in6_ifaddr *
286686cdd19SJun-ichiro itojun Hagino stf_getsrcifa6(ifp)
287686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
288686cdd19SJun-ichiro itojun Hagino {
289686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ia;
290686cdd19SJun-ichiro itojun Hagino 	struct in_ifaddr *ia4;
291686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *sin6;
292686cdd19SJun-ichiro itojun Hagino 	struct in_addr in;
293686cdd19SJun-ichiro itojun Hagino 
29422f29826SPoul-Henning Kamp 	for (ia = TAILQ_FIRST(&ifp->if_addrlist);
295686cdd19SJun-ichiro itojun Hagino 	     ia;
29622f29826SPoul-Henning Kamp 	     ia = TAILQ_NEXT(ia, ifa_list))
297686cdd19SJun-ichiro itojun Hagino 	{
298686cdd19SJun-ichiro itojun Hagino 		if (ia->ifa_addr == NULL)
299686cdd19SJun-ichiro itojun Hagino 			continue;
300686cdd19SJun-ichiro itojun Hagino 		if (ia->ifa_addr->sa_family != AF_INET6)
301686cdd19SJun-ichiro itojun Hagino 			continue;
302686cdd19SJun-ichiro itojun Hagino 		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
303686cdd19SJun-ichiro itojun Hagino 		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
304686cdd19SJun-ichiro itojun Hagino 			continue;
305686cdd19SJun-ichiro itojun Hagino 
306686cdd19SJun-ichiro itojun Hagino 		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
307686cdd19SJun-ichiro itojun Hagino 		for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
308686cdd19SJun-ichiro itojun Hagino 		     ia4;
309686cdd19SJun-ichiro itojun Hagino 		     ia4 = TAILQ_NEXT(ia4, ia_link))
310686cdd19SJun-ichiro itojun Hagino 		{
311686cdd19SJun-ichiro itojun Hagino 			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
312686cdd19SJun-ichiro itojun Hagino 				break;
313686cdd19SJun-ichiro itojun Hagino 		}
314686cdd19SJun-ichiro itojun Hagino 		if (ia4 == NULL)
315686cdd19SJun-ichiro itojun Hagino 			continue;
316686cdd19SJun-ichiro itojun Hagino 
317686cdd19SJun-ichiro itojun Hagino 		return (struct in6_ifaddr *)ia;
318686cdd19SJun-ichiro itojun Hagino 	}
319686cdd19SJun-ichiro itojun Hagino 
320686cdd19SJun-ichiro itojun Hagino 	return NULL;
321686cdd19SJun-ichiro itojun Hagino }
322686cdd19SJun-ichiro itojun Hagino 
323686cdd19SJun-ichiro itojun Hagino static int
324686cdd19SJun-ichiro itojun Hagino stf_output(ifp, m, dst, rt)
325686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
326686cdd19SJun-ichiro itojun Hagino 	struct mbuf *m;
327686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst;
328686cdd19SJun-ichiro itojun Hagino 	struct rtentry *rt;
329686cdd19SJun-ichiro itojun Hagino {
330686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
331686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *dst6;
33233841545SHajimu UMEMOTO 	struct in_addr *in4;
333686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in *dst4;
334686cdd19SJun-ichiro itojun Hagino 	u_int8_t tos;
335686cdd19SJun-ichiro itojun Hagino 	struct ip *ip;
336686cdd19SJun-ichiro itojun Hagino 	struct ip6_hdr *ip6;
337686cdd19SJun-ichiro itojun Hagino 	struct in6_ifaddr *ia6;
338686cdd19SJun-ichiro itojun Hagino 
339686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc*)ifp;
340686cdd19SJun-ichiro itojun Hagino 	dst6 = (struct sockaddr_in6 *)dst;
341686cdd19SJun-ichiro itojun Hagino 
342686cdd19SJun-ichiro itojun Hagino 	/* just in case */
343686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_UP) == 0) {
344686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
345686cdd19SJun-ichiro itojun Hagino 		return ENETDOWN;
346686cdd19SJun-ichiro itojun Hagino 	}
347686cdd19SJun-ichiro itojun Hagino 
348686cdd19SJun-ichiro itojun Hagino 	/*
349686cdd19SJun-ichiro itojun Hagino 	 * If we don't have an ip4 address that match my inner ip6 address,
350686cdd19SJun-ichiro itojun Hagino 	 * we shouldn't generate output.  Without this check, we'll end up
351686cdd19SJun-ichiro itojun Hagino 	 * using wrong IPv4 source.
352686cdd19SJun-ichiro itojun Hagino 	 */
353686cdd19SJun-ichiro itojun Hagino 	ia6 = stf_getsrcifa6(ifp);
354686cdd19SJun-ichiro itojun Hagino 	if (ia6 == NULL) {
355686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
356686cdd19SJun-ichiro itojun Hagino 		return ENETDOWN;
357686cdd19SJun-ichiro itojun Hagino 	}
358686cdd19SJun-ichiro itojun Hagino 
359686cdd19SJun-ichiro itojun Hagino 	if (m->m_len < sizeof(*ip6)) {
360686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(*ip6));
361686cdd19SJun-ichiro itojun Hagino 		if (!m)
362686cdd19SJun-ichiro itojun Hagino 			return ENOBUFS;
363686cdd19SJun-ichiro itojun Hagino 	}
364686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
365686cdd19SJun-ichiro itojun Hagino 	tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
366686cdd19SJun-ichiro itojun Hagino 
36733841545SHajimu UMEMOTO 	/*
36833841545SHajimu UMEMOTO 	 * Pickup the right outer dst addr from the list of candidates.
36933841545SHajimu UMEMOTO 	 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
37033841545SHajimu UMEMOTO 	 */
37133841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
37233841545SHajimu UMEMOTO 		in4 = GET_V4(&ip6->ip6_dst);
37333841545SHajimu UMEMOTO 	else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
37433841545SHajimu UMEMOTO 		in4 = GET_V4(&dst6->sin6_addr);
37533841545SHajimu UMEMOTO 	else {
37633841545SHajimu UMEMOTO 		m_freem(m);
37733841545SHajimu UMEMOTO 		return ENETUNREACH;
37833841545SHajimu UMEMOTO 	}
37933841545SHajimu UMEMOTO 
3808acb2290SHajimu UMEMOTO #if NBPFILTER > 0
3818acb2290SHajimu UMEMOTO 	if (ifp->if_bpf) {
3828acb2290SHajimu UMEMOTO 		/*
3838acb2290SHajimu UMEMOTO 		 * We need to prepend the address family as
3848acb2290SHajimu UMEMOTO 		 * a four byte field.  Cons up a dummy header
3858acb2290SHajimu UMEMOTO 		 * to pacify bpf.  This is safe because bpf
3868acb2290SHajimu UMEMOTO 		 * will only read from the mbuf (i.e., it won't
3878acb2290SHajimu UMEMOTO 		 * try to free it or keep a pointer a to it).
3888acb2290SHajimu UMEMOTO 		 */
3898acb2290SHajimu UMEMOTO 		struct mbuf m0;
3908acb2290SHajimu UMEMOTO 		u_int32_t af = AF_INET6;
3918acb2290SHajimu UMEMOTO 
3928acb2290SHajimu UMEMOTO 		m0.m_next = m;
3938acb2290SHajimu UMEMOTO 		m0.m_len = 4;
3948acb2290SHajimu UMEMOTO 		m0.m_data = (char *)&af;
3958acb2290SHajimu UMEMOTO 
3968acb2290SHajimu UMEMOTO #ifdef HAVE_OLD_BPF
3978acb2290SHajimu UMEMOTO 		bpf_mtap(ifp, &m0);
3988acb2290SHajimu UMEMOTO #else
3998acb2290SHajimu UMEMOTO 		bpf_mtap(ifp->if_bpf, &m0);
4008acb2290SHajimu UMEMOTO #endif
4018acb2290SHajimu UMEMOTO 	}
4028acb2290SHajimu UMEMOTO #endif /*NBPFILTER > 0*/
4038acb2290SHajimu UMEMOTO 
404686cdd19SJun-ichiro itojun Hagino 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
405686cdd19SJun-ichiro itojun Hagino 	if (m && m->m_len < sizeof(struct ip))
406686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(struct ip));
407686cdd19SJun-ichiro itojun Hagino 	if (m == NULL)
408686cdd19SJun-ichiro itojun Hagino 		return ENOBUFS;
409686cdd19SJun-ichiro itojun Hagino 	ip = mtod(m, struct ip *);
410686cdd19SJun-ichiro itojun Hagino 
411686cdd19SJun-ichiro itojun Hagino 	bzero(ip, sizeof(*ip));
412686cdd19SJun-ichiro itojun Hagino 
413686cdd19SJun-ichiro itojun Hagino 	bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
414686cdd19SJun-ichiro itojun Hagino 	    &ip->ip_src, sizeof(ip->ip_src));
41533841545SHajimu UMEMOTO 	bcopy(in4, &ip->ip_dst, sizeof(ip->ip_dst));
416686cdd19SJun-ichiro itojun Hagino 	ip->ip_p = IPPROTO_IPV6;
41753dab5feSBrooks Davis 	ip->ip_ttl = ip_stf_ttl;
418686cdd19SJun-ichiro itojun Hagino 	ip->ip_len = m->m_pkthdr.len;	/*host order*/
419686cdd19SJun-ichiro itojun Hagino 	if (ifp->if_flags & IFF_LINK1)
420686cdd19SJun-ichiro itojun Hagino 		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
42133841545SHajimu UMEMOTO 	else
42233841545SHajimu UMEMOTO 		ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
423686cdd19SJun-ichiro itojun Hagino 
424686cdd19SJun-ichiro itojun Hagino 	dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
425686cdd19SJun-ichiro itojun Hagino 	if (dst4->sin_family != AF_INET ||
426686cdd19SJun-ichiro itojun Hagino 	    bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
427686cdd19SJun-ichiro itojun Hagino 		/* cache route doesn't match */
428686cdd19SJun-ichiro itojun Hagino 		dst4->sin_family = AF_INET;
429686cdd19SJun-ichiro itojun Hagino 		dst4->sin_len = sizeof(struct sockaddr_in);
430686cdd19SJun-ichiro itojun Hagino 		bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
431686cdd19SJun-ichiro itojun Hagino 		if (sc->sc_ro.ro_rt) {
432686cdd19SJun-ichiro itojun Hagino 			RTFREE(sc->sc_ro.ro_rt);
433686cdd19SJun-ichiro itojun Hagino 			sc->sc_ro.ro_rt = NULL;
434686cdd19SJun-ichiro itojun Hagino 		}
435686cdd19SJun-ichiro itojun Hagino 	}
436686cdd19SJun-ichiro itojun Hagino 
437686cdd19SJun-ichiro itojun Hagino 	if (sc->sc_ro.ro_rt == NULL) {
438686cdd19SJun-ichiro itojun Hagino 		rtalloc(&sc->sc_ro);
439686cdd19SJun-ichiro itojun Hagino 		if (sc->sc_ro.ro_rt == NULL) {
440686cdd19SJun-ichiro itojun Hagino 			m_freem(m);
441686cdd19SJun-ichiro itojun Hagino 			return ENETUNREACH;
442686cdd19SJun-ichiro itojun Hagino 		}
443686cdd19SJun-ichiro itojun Hagino 	}
444686cdd19SJun-ichiro itojun Hagino 
445686cdd19SJun-ichiro itojun Hagino 	return ip_output(m, NULL, &sc->sc_ro, 0, NULL);
446686cdd19SJun-ichiro itojun Hagino }
447686cdd19SJun-ichiro itojun Hagino 
448686cdd19SJun-ichiro itojun Hagino static int
44933841545SHajimu UMEMOTO stf_checkaddr4(sc, in, inifp)
45033841545SHajimu UMEMOTO 	struct stf_softc *sc;
451686cdd19SJun-ichiro itojun Hagino 	struct in_addr *in;
45233841545SHajimu UMEMOTO 	struct ifnet *inifp;	/* incoming interface */
453686cdd19SJun-ichiro itojun Hagino {
454686cdd19SJun-ichiro itojun Hagino 	struct in_ifaddr *ia4;
455686cdd19SJun-ichiro itojun Hagino 
456686cdd19SJun-ichiro itojun Hagino 	/*
457686cdd19SJun-ichiro itojun Hagino 	 * reject packets with the following address:
458686cdd19SJun-ichiro itojun Hagino 	 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
459686cdd19SJun-ichiro itojun Hagino 	 */
460d1d1144bSJun-ichiro itojun Hagino 	if (IN_MULTICAST(ntohl(in->s_addr)))
461686cdd19SJun-ichiro itojun Hagino 		return -1;
462686cdd19SJun-ichiro itojun Hagino 	switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
463686cdd19SJun-ichiro itojun Hagino 	case 0: case 127: case 255:
464686cdd19SJun-ichiro itojun Hagino 		return -1;
465686cdd19SJun-ichiro itojun Hagino 	}
466686cdd19SJun-ichiro itojun Hagino 
467686cdd19SJun-ichiro itojun Hagino 	/*
468686cdd19SJun-ichiro itojun Hagino 	 * reject packets with broadcast
469686cdd19SJun-ichiro itojun Hagino 	 */
470686cdd19SJun-ichiro itojun Hagino 	for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
471686cdd19SJun-ichiro itojun Hagino 	     ia4;
472686cdd19SJun-ichiro itojun Hagino 	     ia4 = TAILQ_NEXT(ia4, ia_link))
473686cdd19SJun-ichiro itojun Hagino 	{
474686cdd19SJun-ichiro itojun Hagino 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
475686cdd19SJun-ichiro itojun Hagino 			continue;
476686cdd19SJun-ichiro itojun Hagino 		if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
477686cdd19SJun-ichiro itojun Hagino 			return -1;
478686cdd19SJun-ichiro itojun Hagino 	}
479686cdd19SJun-ichiro itojun Hagino 
480686cdd19SJun-ichiro itojun Hagino 	/*
481686cdd19SJun-ichiro itojun Hagino 	 * perform ingress filter
482686cdd19SJun-ichiro itojun Hagino 	 */
48333841545SHajimu UMEMOTO 	if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) {
484686cdd19SJun-ichiro itojun Hagino 		struct sockaddr_in sin;
485686cdd19SJun-ichiro itojun Hagino 		struct rtentry *rt;
486686cdd19SJun-ichiro itojun Hagino 
487686cdd19SJun-ichiro itojun Hagino 		bzero(&sin, sizeof(sin));
488686cdd19SJun-ichiro itojun Hagino 		sin.sin_family = AF_INET;
489686cdd19SJun-ichiro itojun Hagino 		sin.sin_len = sizeof(struct sockaddr_in);
490686cdd19SJun-ichiro itojun Hagino 		sin.sin_addr = *in;
491686cdd19SJun-ichiro itojun Hagino 		rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
49233841545SHajimu UMEMOTO 		if (!rt || rt->rt_ifp != inifp) {
49333841545SHajimu UMEMOTO #if 0
49433841545SHajimu UMEMOTO 			log(LOG_WARNING, "%s: packet from 0x%x dropped "
49533841545SHajimu UMEMOTO 			    "due to ingress filter\n", if_name(&sc->sc_if),
49633841545SHajimu UMEMOTO 			    (u_int32_t)ntohl(sin.sin_addr.s_addr));
49733841545SHajimu UMEMOTO #endif
49833841545SHajimu UMEMOTO 			if (rt)
499686cdd19SJun-ichiro itojun Hagino 				rtfree(rt);
500686cdd19SJun-ichiro itojun Hagino 			return -1;
501686cdd19SJun-ichiro itojun Hagino 		}
502686cdd19SJun-ichiro itojun Hagino 		rtfree(rt);
503686cdd19SJun-ichiro itojun Hagino 	}
504686cdd19SJun-ichiro itojun Hagino 
505686cdd19SJun-ichiro itojun Hagino 	return 0;
506686cdd19SJun-ichiro itojun Hagino }
507686cdd19SJun-ichiro itojun Hagino 
508686cdd19SJun-ichiro itojun Hagino static int
50933841545SHajimu UMEMOTO stf_checkaddr6(sc, in6, inifp)
51033841545SHajimu UMEMOTO 	struct stf_softc *sc;
511686cdd19SJun-ichiro itojun Hagino 	struct in6_addr *in6;
51233841545SHajimu UMEMOTO 	struct ifnet *inifp;	/* incoming interface */
513686cdd19SJun-ichiro itojun Hagino {
514686cdd19SJun-ichiro itojun Hagino 	/*
515686cdd19SJun-ichiro itojun Hagino 	 * check 6to4 addresses
516686cdd19SJun-ichiro itojun Hagino 	 */
517686cdd19SJun-ichiro itojun Hagino 	if (IN6_IS_ADDR_6TO4(in6))
51833841545SHajimu UMEMOTO 		return stf_checkaddr4(sc, GET_V4(in6), inifp);
519686cdd19SJun-ichiro itojun Hagino 
520686cdd19SJun-ichiro itojun Hagino 	/*
521686cdd19SJun-ichiro itojun Hagino 	 * reject anything that look suspicious.  the test is implemented
522686cdd19SJun-ichiro itojun Hagino 	 * in ip6_input too, but we check here as well to
523686cdd19SJun-ichiro itojun Hagino 	 * (1) reject bad packets earlier, and
524686cdd19SJun-ichiro itojun Hagino 	 * (2) to be safe against future ip6_input change.
525686cdd19SJun-ichiro itojun Hagino 	 */
526686cdd19SJun-ichiro itojun Hagino 	if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
527686cdd19SJun-ichiro itojun Hagino 		return -1;
528686cdd19SJun-ichiro itojun Hagino 
529686cdd19SJun-ichiro itojun Hagino 	return 0;
530686cdd19SJun-ichiro itojun Hagino }
531686cdd19SJun-ichiro itojun Hagino 
532686cdd19SJun-ichiro itojun Hagino void
533686cdd19SJun-ichiro itojun Hagino #if __STDC__
534686cdd19SJun-ichiro itojun Hagino in_stf_input(struct mbuf *m, ...)
535686cdd19SJun-ichiro itojun Hagino #else
536686cdd19SJun-ichiro itojun Hagino in_stf_input(m, va_alist)
53733841545SHajimu UMEMOTO 	struct mbuf *m;
538686cdd19SJun-ichiro itojun Hagino #endif
539686cdd19SJun-ichiro itojun Hagino {
540686cdd19SJun-ichiro itojun Hagino 	int off, proto;
541686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
542686cdd19SJun-ichiro itojun Hagino 	struct ip *ip;
543686cdd19SJun-ichiro itojun Hagino 	struct ip6_hdr *ip6;
544686cdd19SJun-ichiro itojun Hagino 	u_int8_t otos, itos;
545df5e1987SJonathan Lemon 	int len, isr;
546686cdd19SJun-ichiro itojun Hagino 	struct ifqueue *ifq = NULL;
547686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
548686cdd19SJun-ichiro itojun Hagino 	va_list ap;
549686cdd19SJun-ichiro itojun Hagino 
550686cdd19SJun-ichiro itojun Hagino 	va_start(ap, m);
551686cdd19SJun-ichiro itojun Hagino 	off = va_arg(ap, int);
552f0ffb944SJulian Elischer 	proto = mtod(m, struct ip *)->ip_p;
553686cdd19SJun-ichiro itojun Hagino 	va_end(ap);
554686cdd19SJun-ichiro itojun Hagino 
555686cdd19SJun-ichiro itojun Hagino 	if (proto != IPPROTO_IPV6) {
556686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
557686cdd19SJun-ichiro itojun Hagino 		return;
558686cdd19SJun-ichiro itojun Hagino 	}
559686cdd19SJun-ichiro itojun Hagino 
560686cdd19SJun-ichiro itojun Hagino 	ip = mtod(m, struct ip *);
561686cdd19SJun-ichiro itojun Hagino 
562686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc *)encap_getarg(m);
563686cdd19SJun-ichiro itojun Hagino 
564686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
565686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
566686cdd19SJun-ichiro itojun Hagino 		return;
567686cdd19SJun-ichiro itojun Hagino 	}
568686cdd19SJun-ichiro itojun Hagino 
569686cdd19SJun-ichiro itojun Hagino 	ifp = &sc->sc_if;
570686cdd19SJun-ichiro itojun Hagino 
571686cdd19SJun-ichiro itojun Hagino 	/*
572686cdd19SJun-ichiro itojun Hagino 	 * perform sanity check against outer src/dst.
573686cdd19SJun-ichiro itojun Hagino 	 * for source, perform ingress filter as well.
574686cdd19SJun-ichiro itojun Hagino 	 */
57533841545SHajimu UMEMOTO 	if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
57633841545SHajimu UMEMOTO 	    stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
577686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
578686cdd19SJun-ichiro itojun Hagino 		return;
579686cdd19SJun-ichiro itojun Hagino 	}
580686cdd19SJun-ichiro itojun Hagino 
581686cdd19SJun-ichiro itojun Hagino 	otos = ip->ip_tos;
582686cdd19SJun-ichiro itojun Hagino 	m_adj(m, off);
583686cdd19SJun-ichiro itojun Hagino 
584686cdd19SJun-ichiro itojun Hagino 	if (m->m_len < sizeof(*ip6)) {
585686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(*ip6));
586686cdd19SJun-ichiro itojun Hagino 		if (!m)
587686cdd19SJun-ichiro itojun Hagino 			return;
588686cdd19SJun-ichiro itojun Hagino 	}
589686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
590686cdd19SJun-ichiro itojun Hagino 
591686cdd19SJun-ichiro itojun Hagino 	/*
592686cdd19SJun-ichiro itojun Hagino 	 * perform sanity check against inner src/dst.
593686cdd19SJun-ichiro itojun Hagino 	 * for source, perform ingress filter as well.
594686cdd19SJun-ichiro itojun Hagino 	 */
59533841545SHajimu UMEMOTO 	if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
59633841545SHajimu UMEMOTO 	    stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
597686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
598686cdd19SJun-ichiro itojun Hagino 		return;
599686cdd19SJun-ichiro itojun Hagino 	}
600686cdd19SJun-ichiro itojun Hagino 
601686cdd19SJun-ichiro itojun Hagino 	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
602686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_LINK1) != 0)
603686cdd19SJun-ichiro itojun Hagino 		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
60433841545SHajimu UMEMOTO 	else
60533841545SHajimu UMEMOTO 		ip_ecn_egress(ECN_NOCARE, &otos, &itos);
606686cdd19SJun-ichiro itojun Hagino 	ip6->ip6_flow &= ~htonl(0xff << 20);
607686cdd19SJun-ichiro itojun Hagino 	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
608686cdd19SJun-ichiro itojun Hagino 
609686cdd19SJun-ichiro itojun Hagino 	m->m_pkthdr.rcvif = ifp;
610686cdd19SJun-ichiro itojun Hagino 
611686cdd19SJun-ichiro itojun Hagino 	if (ifp->if_bpf) {
612686cdd19SJun-ichiro itojun Hagino 		/*
613686cdd19SJun-ichiro itojun Hagino 		 * We need to prepend the address family as
614686cdd19SJun-ichiro itojun Hagino 		 * a four byte field.  Cons up a dummy header
615686cdd19SJun-ichiro itojun Hagino 		 * to pacify bpf.  This is safe because bpf
616686cdd19SJun-ichiro itojun Hagino 		 * will only read from the mbuf (i.e., it won't
617686cdd19SJun-ichiro itojun Hagino 		 * try to free it or keep a pointer a to it).
618686cdd19SJun-ichiro itojun Hagino 		 */
619686cdd19SJun-ichiro itojun Hagino 		struct mbuf m0;
62033841545SHajimu UMEMOTO 		u_int32_t af = AF_INET6;
621686cdd19SJun-ichiro itojun Hagino 
622686cdd19SJun-ichiro itojun Hagino 		m0.m_next = m;
623686cdd19SJun-ichiro itojun Hagino 		m0.m_len = 4;
624686cdd19SJun-ichiro itojun Hagino 		m0.m_data = (char *)&af;
625686cdd19SJun-ichiro itojun Hagino 
626686cdd19SJun-ichiro itojun Hagino #ifdef HAVE_OLD_BPF
627686cdd19SJun-ichiro itojun Hagino 		bpf_mtap(ifp, &m0);
628686cdd19SJun-ichiro itojun Hagino #else
629686cdd19SJun-ichiro itojun Hagino 		bpf_mtap(ifp->if_bpf, &m0);
630686cdd19SJun-ichiro itojun Hagino #endif
631686cdd19SJun-ichiro itojun Hagino 	}
632686cdd19SJun-ichiro itojun Hagino 
633686cdd19SJun-ichiro itojun Hagino 	/*
634686cdd19SJun-ichiro itojun Hagino 	 * Put the packet to the network layer input queue according to the
635686cdd19SJun-ichiro itojun Hagino 	 * specified address family.
636686cdd19SJun-ichiro itojun Hagino 	 * See net/if_gif.c for possible issues with packet processing
637686cdd19SJun-ichiro itojun Hagino 	 * reorder due to extra queueing.
638686cdd19SJun-ichiro itojun Hagino 	 */
639686cdd19SJun-ichiro itojun Hagino 	ifq = &ip6intrq;
640686cdd19SJun-ichiro itojun Hagino 	isr = NETISR_IPV6;
641686cdd19SJun-ichiro itojun Hagino 
642df5e1987SJonathan Lemon 	len = m->m_pkthdr.len;
643df5e1987SJonathan Lemon 	if (! IF_HANDOFF(ifq, m, NULL))
644686cdd19SJun-ichiro itojun Hagino 		return;
645686cdd19SJun-ichiro itojun Hagino 	schednetisr(isr);
646686cdd19SJun-ichiro itojun Hagino 	ifp->if_ipackets++;
647df5e1987SJonathan Lemon 	ifp->if_ibytes += len;
648686cdd19SJun-ichiro itojun Hagino }
649686cdd19SJun-ichiro itojun Hagino 
650686cdd19SJun-ichiro itojun Hagino /* ARGSUSED */
651686cdd19SJun-ichiro itojun Hagino static void
652686cdd19SJun-ichiro itojun Hagino stf_rtrequest(cmd, rt, sa)
653686cdd19SJun-ichiro itojun Hagino 	int cmd;
654686cdd19SJun-ichiro itojun Hagino 	struct rtentry *rt;
655686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *sa;
656686cdd19SJun-ichiro itojun Hagino {
657686cdd19SJun-ichiro itojun Hagino 
658686cdd19SJun-ichiro itojun Hagino 	if (rt)
659686cdd19SJun-ichiro itojun Hagino 		rt->rt_rmx.rmx_mtu = IPV6_MMTU;
660686cdd19SJun-ichiro itojun Hagino }
661686cdd19SJun-ichiro itojun Hagino 
662686cdd19SJun-ichiro itojun Hagino static int
663686cdd19SJun-ichiro itojun Hagino stf_ioctl(ifp, cmd, data)
664686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
665686cdd19SJun-ichiro itojun Hagino 	u_long cmd;
666686cdd19SJun-ichiro itojun Hagino 	caddr_t data;
667686cdd19SJun-ichiro itojun Hagino {
668686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ifa;
669686cdd19SJun-ichiro itojun Hagino 	struct ifreq *ifr;
670686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *sin6;
671686cdd19SJun-ichiro itojun Hagino 	int error;
672686cdd19SJun-ichiro itojun Hagino 
673686cdd19SJun-ichiro itojun Hagino 	error = 0;
674686cdd19SJun-ichiro itojun Hagino 	switch (cmd) {
675686cdd19SJun-ichiro itojun Hagino 	case SIOCSIFADDR:
676686cdd19SJun-ichiro itojun Hagino 		ifa = (struct ifaddr *)data;
677686cdd19SJun-ichiro itojun Hagino 		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
678686cdd19SJun-ichiro itojun Hagino 			error = EAFNOSUPPORT;
679686cdd19SJun-ichiro itojun Hagino 			break;
680686cdd19SJun-ichiro itojun Hagino 		}
681686cdd19SJun-ichiro itojun Hagino 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
682686cdd19SJun-ichiro itojun Hagino 		if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
683686cdd19SJun-ichiro itojun Hagino 			ifa->ifa_rtrequest = stf_rtrequest;
684686cdd19SJun-ichiro itojun Hagino 			ifp->if_flags |= IFF_UP;
685686cdd19SJun-ichiro itojun Hagino 		} else
686686cdd19SJun-ichiro itojun Hagino 			error = EINVAL;
687686cdd19SJun-ichiro itojun Hagino 		break;
688686cdd19SJun-ichiro itojun Hagino 
689686cdd19SJun-ichiro itojun Hagino 	case SIOCADDMULTI:
690686cdd19SJun-ichiro itojun Hagino 	case SIOCDELMULTI:
691686cdd19SJun-ichiro itojun Hagino 		ifr = (struct ifreq *)data;
692686cdd19SJun-ichiro itojun Hagino 		if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
693686cdd19SJun-ichiro itojun Hagino 			;
694686cdd19SJun-ichiro itojun Hagino 		else
695686cdd19SJun-ichiro itojun Hagino 			error = EAFNOSUPPORT;
696686cdd19SJun-ichiro itojun Hagino 		break;
697686cdd19SJun-ichiro itojun Hagino 
698686cdd19SJun-ichiro itojun Hagino 	default:
699686cdd19SJun-ichiro itojun Hagino 		error = EINVAL;
700686cdd19SJun-ichiro itojun Hagino 		break;
701686cdd19SJun-ichiro itojun Hagino 	}
702686cdd19SJun-ichiro itojun Hagino 
703686cdd19SJun-ichiro itojun Hagino 	return error;
704686cdd19SJun-ichiro itojun Hagino }
705