xref: /freebsd/sys/net/if_stf.c (revision 3b16e7b25213e43f7c08f9d5275a9d33d6c22472)
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/kernel.h>
87abb64706SBrooks Davis #include <sys/protosw.h>
88abb64706SBrooks Davis #include <sys/queue.h>
89686cdd19SJun-ichiro itojun Hagino #include <machine/cpu.h>
90686cdd19SJun-ichiro itojun Hagino 
91686cdd19SJun-ichiro itojun Hagino #include <sys/malloc.h>
92686cdd19SJun-ichiro itojun Hagino 
93686cdd19SJun-ichiro itojun Hagino #include <net/if.h>
94686cdd19SJun-ichiro itojun Hagino #include <net/route.h>
95686cdd19SJun-ichiro itojun Hagino #include <net/netisr.h>
96686cdd19SJun-ichiro itojun Hagino #include <net/if_types.h>
97686cdd19SJun-ichiro itojun Hagino #include <net/if_stf.h>
98686cdd19SJun-ichiro itojun Hagino 
99686cdd19SJun-ichiro itojun Hagino #include <netinet/in.h>
100686cdd19SJun-ichiro itojun Hagino #include <netinet/in_systm.h>
101686cdd19SJun-ichiro itojun Hagino #include <netinet/ip.h>
102686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_var.h>
103686cdd19SJun-ichiro itojun Hagino #include <netinet/in_var.h>
104686cdd19SJun-ichiro itojun Hagino 
105686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
106686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6_var.h>
107686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_var.h>
108686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_ecn.h>
109686cdd19SJun-ichiro itojun Hagino 
110686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
111686cdd19SJun-ichiro itojun Hagino 
112686cdd19SJun-ichiro itojun Hagino #include <machine/stdarg.h>
113686cdd19SJun-ichiro itojun Hagino 
114686cdd19SJun-ichiro itojun Hagino #include <net/net_osdep.h>
115686cdd19SJun-ichiro itojun Hagino 
116686cdd19SJun-ichiro itojun Hagino #include <net/bpf.h>
117686cdd19SJun-ichiro itojun Hagino 
118abb64706SBrooks Davis #define STFNAME		"stf"
119abb64706SBrooks Davis 
120686cdd19SJun-ichiro itojun Hagino #define IN6_IS_ADDR_6TO4(x)	(ntohs((x)->s6_addr16[0]) == 0x2002)
121686cdd19SJun-ichiro itojun Hagino #define GET_V4(x)	((struct in_addr *)(&(x)->s6_addr16[1]))
122686cdd19SJun-ichiro itojun Hagino 
123686cdd19SJun-ichiro itojun Hagino struct stf_softc {
124686cdd19SJun-ichiro itojun Hagino 	struct ifnet	sc_if;	   /* common area */
125686cdd19SJun-ichiro itojun Hagino 	union {
126686cdd19SJun-ichiro itojun Hagino 		struct route  __sc_ro4;
127686cdd19SJun-ichiro itojun Hagino 		struct route_in6 __sc_ro6; /* just for safety */
128686cdd19SJun-ichiro itojun Hagino 	} __sc_ro46;
129686cdd19SJun-ichiro itojun Hagino #define sc_ro	__sc_ro46.__sc_ro4
130686cdd19SJun-ichiro itojun Hagino 	const struct encaptab *encap_cookie;
131abb64706SBrooks Davis 	LIST_ENTRY(stf_softc) sc_list;	/* all stf's are linked */
132686cdd19SJun-ichiro itojun Hagino };
133686cdd19SJun-ichiro itojun Hagino 
134e8783c4dSMike Smith static LIST_HEAD(, stf_softc) stf_softc_list;
135686cdd19SJun-ichiro itojun Hagino 
136abb64706SBrooks Davis static MALLOC_DEFINE(M_STF, STFNAME, "6to4 Tunnel Interface");
13753dab5feSBrooks Davis static int ip_stf_ttl = 40;
138686cdd19SJun-ichiro itojun Hagino 
13953dab5feSBrooks Davis extern  struct domain inetdomain;
140f0ffb944SJulian Elischer struct protosw in_stf_protosw =
14153dab5feSBrooks Davis { SOCK_RAW,	&inetdomain,	IPPROTO_IPV6,	PR_ATOMIC|PR_ADDR,
142777b9faaSPeter Wemm   in_stf_input,	(pr_output_t*)rip_output, 0,	rip_ctloutput,
14353dab5feSBrooks Davis   0,
14453dab5feSBrooks Davis   0,            0,              0,              0,
14553dab5feSBrooks Davis   &rip_usrreqs
14653dab5feSBrooks Davis };
147686cdd19SJun-ichiro itojun Hagino 
14853dab5feSBrooks Davis static int stfmodevent __P((module_t, int, void *));
149686cdd19SJun-ichiro itojun Hagino static int stf_encapcheck __P((const struct mbuf *, int, int, void *));
150686cdd19SJun-ichiro itojun Hagino static struct in6_ifaddr *stf_getsrcifa6 __P((struct ifnet *));
151686cdd19SJun-ichiro itojun Hagino static int stf_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
152686cdd19SJun-ichiro itojun Hagino 	struct rtentry *));
15333841545SHajimu UMEMOTO static int stf_checkaddr4 __P((struct stf_softc *, struct in_addr *,
15433841545SHajimu UMEMOTO 	struct ifnet *));
15533841545SHajimu UMEMOTO static int stf_checkaddr6 __P((struct stf_softc *, struct in6_addr *,
15633841545SHajimu UMEMOTO 	struct ifnet *));
1578071913dSRuslan Ermilov static void stf_rtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
158686cdd19SJun-ichiro itojun Hagino static int stf_ioctl __P((struct ifnet *, u_long, caddr_t));
159686cdd19SJun-ichiro itojun Hagino 
1603b16e7b2SMaxime Henrion int	stf_clone_create __P((struct if_clone *, int));
161b75496feSBrooks Davis int	stf_clone_destroy __P((struct ifnet *));
162abb64706SBrooks Davis 
1633b16e7b2SMaxime Henrion /* only one clone is currently allowed */
164abb64706SBrooks Davis struct if_clone stf_cloner =
1653b16e7b2SMaxime Henrion     IF_CLONE_INITIALIZER(STFNAME, stf_clone_create, stf_clone_destroy, 0);
166abb64706SBrooks Davis 
167abb64706SBrooks Davis int
168abb64706SBrooks Davis stf_clone_create(ifc, unit)
169abb64706SBrooks Davis 	struct if_clone *ifc;
1703b16e7b2SMaxime Henrion 	int unit;
171686cdd19SJun-ichiro itojun Hagino {
172686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
173686cdd19SJun-ichiro itojun Hagino 
1741ed4b9feSAndrew R. Reiter 	sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
175abb64706SBrooks Davis 	sc->sc_if.if_name = STFNAME;
1763b16e7b2SMaxime Henrion 	sc->sc_if.if_unit = unit;
177abb64706SBrooks Davis 
178abb64706SBrooks Davis 	sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
179abb64706SBrooks Davis 	    stf_encapcheck, &in_stf_protosw, sc);
180abb64706SBrooks Davis 	if (sc->encap_cookie == NULL) {
181686cdd19SJun-ichiro itojun Hagino 		printf("%s: attach failed\n", if_name(&sc->sc_if));
182abb64706SBrooks Davis 		free(sc, M_STF);
18353dab5feSBrooks Davis 		return (ENOMEM);
184686cdd19SJun-ichiro itojun Hagino 	}
185686cdd19SJun-ichiro itojun Hagino 
186686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_mtu    = IPV6_MMTU;
187686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_ioctl  = stf_ioctl;
188686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_output = stf_output;
189686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_type   = IFT_STF;
190686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
191686cdd19SJun-ichiro itojun Hagino 	if_attach(&sc->sc_if);
192686cdd19SJun-ichiro itojun Hagino 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
193abb64706SBrooks Davis 	LIST_INSERT_HEAD(&stf_softc_list, sc, sc_list);
194abb64706SBrooks Davis 	return (0);
195abb64706SBrooks Davis }
196abb64706SBrooks Davis 
197b75496feSBrooks Davis int
198abb64706SBrooks Davis stf_clone_destroy(ifp)
199abb64706SBrooks Davis 	struct ifnet *ifp;
200abb64706SBrooks Davis {
201abb64706SBrooks Davis 	int err;
202abb64706SBrooks Davis 	struct stf_softc *sc = (void *) ifp;
203abb64706SBrooks Davis 
204abb64706SBrooks Davis 	LIST_REMOVE(sc, sc_list);
20553dab5feSBrooks Davis 	err = encap_detach(sc->encap_cookie);
20653dab5feSBrooks Davis 	KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
207abb64706SBrooks Davis 	bpfdetach(ifp);
208abb64706SBrooks Davis 	if_detach(ifp);
209abb64706SBrooks Davis 
21053dab5feSBrooks Davis 	free(sc, M_STF);
211b75496feSBrooks Davis 	return (0);
212abb64706SBrooks Davis }
213abb64706SBrooks Davis 
214abb64706SBrooks Davis static int
215abb64706SBrooks Davis stfmodevent(mod, type, data)
216abb64706SBrooks Davis 	module_t mod;
217abb64706SBrooks Davis 	int type;
218abb64706SBrooks Davis 	void *data;
219abb64706SBrooks Davis {
220abb64706SBrooks Davis 
221abb64706SBrooks Davis 	switch (type) {
222abb64706SBrooks Davis 	case MOD_LOAD:
223abb64706SBrooks Davis 		LIST_INIT(&stf_softc_list);
224abb64706SBrooks Davis 		if_clone_attach(&stf_cloner);
225abb64706SBrooks Davis 
226abb64706SBrooks Davis 		break;
227abb64706SBrooks Davis 	case MOD_UNLOAD:
228abb64706SBrooks Davis 		if_clone_detach(&stf_cloner);
229abb64706SBrooks Davis 
230abb64706SBrooks Davis 		while (!LIST_EMPTY(&stf_softc_list))
231abb64706SBrooks Davis 			stf_clone_destroy(&LIST_FIRST(&stf_softc_list)->sc_if);
23253dab5feSBrooks Davis 		break;
233686cdd19SJun-ichiro itojun Hagino 	}
234686cdd19SJun-ichiro itojun Hagino 
23553dab5feSBrooks Davis 	return (0);
23653dab5feSBrooks Davis }
23753dab5feSBrooks Davis 
23853dab5feSBrooks Davis static moduledata_t stf_mod = {
23953dab5feSBrooks Davis 	"if_stf",
24053dab5feSBrooks Davis 	stfmodevent,
24153dab5feSBrooks Davis 	0
24253dab5feSBrooks Davis };
24353dab5feSBrooks Davis 
24453dab5feSBrooks Davis DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
245686cdd19SJun-ichiro itojun Hagino 
246686cdd19SJun-ichiro itojun Hagino static int
247686cdd19SJun-ichiro itojun Hagino stf_encapcheck(m, off, proto, arg)
248686cdd19SJun-ichiro itojun Hagino 	const struct mbuf *m;
249686cdd19SJun-ichiro itojun Hagino 	int off;
250686cdd19SJun-ichiro itojun Hagino 	int proto;
251686cdd19SJun-ichiro itojun Hagino 	void *arg;
252686cdd19SJun-ichiro itojun Hagino {
253686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
254686cdd19SJun-ichiro itojun Hagino 	struct in6_ifaddr *ia6;
255686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
256686cdd19SJun-ichiro itojun Hagino 	struct in_addr a, b;
257686cdd19SJun-ichiro itojun Hagino 
258686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc *)arg;
259686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
260686cdd19SJun-ichiro itojun Hagino 		return 0;
261686cdd19SJun-ichiro itojun Hagino 
262686cdd19SJun-ichiro itojun Hagino 	if ((sc->sc_if.if_flags & IFF_UP) == 0)
263686cdd19SJun-ichiro itojun Hagino 		return 0;
264686cdd19SJun-ichiro itojun Hagino 
26533841545SHajimu UMEMOTO 	/* IFF_LINK0 means "no decapsulation" */
26633841545SHajimu UMEMOTO 	if ((sc->sc_if.if_flags & IFF_LINK0) != 0)
26733841545SHajimu UMEMOTO 		return 0;
26833841545SHajimu UMEMOTO 
269686cdd19SJun-ichiro itojun Hagino 	if (proto != IPPROTO_IPV6)
270686cdd19SJun-ichiro itojun Hagino 		return 0;
271686cdd19SJun-ichiro itojun Hagino 
272686cdd19SJun-ichiro itojun Hagino 	/* LINTED const cast */
273777b9faaSPeter Wemm 	m_copydata((struct mbuf *)(uintptr_t)m, 0, sizeof(ip), (caddr_t)&ip);
274686cdd19SJun-ichiro itojun Hagino 
275686cdd19SJun-ichiro itojun Hagino 	if (ip.ip_v != 4)
276686cdd19SJun-ichiro itojun Hagino 		return 0;
277686cdd19SJun-ichiro itojun Hagino 
278686cdd19SJun-ichiro itojun Hagino 	ia6 = stf_getsrcifa6(&sc->sc_if);
279686cdd19SJun-ichiro itojun Hagino 	if (ia6 == NULL)
280686cdd19SJun-ichiro itojun Hagino 		return 0;
281686cdd19SJun-ichiro itojun Hagino 
282686cdd19SJun-ichiro itojun Hagino 	/*
283686cdd19SJun-ichiro itojun Hagino 	 * check if IPv4 dst matches the IPv4 address derived from the
284686cdd19SJun-ichiro itojun Hagino 	 * local 6to4 address.
285686cdd19SJun-ichiro itojun Hagino 	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
286686cdd19SJun-ichiro itojun Hagino 	 */
287686cdd19SJun-ichiro itojun Hagino 	if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
288686cdd19SJun-ichiro itojun Hagino 	    sizeof(ip.ip_dst)) != 0)
289686cdd19SJun-ichiro itojun Hagino 		return 0;
290686cdd19SJun-ichiro itojun Hagino 
291686cdd19SJun-ichiro itojun Hagino 	/*
292686cdd19SJun-ichiro itojun Hagino 	 * check if IPv4 src matches the IPv4 address derived from the
293686cdd19SJun-ichiro itojun Hagino 	 * local 6to4 address masked by prefixmask.
294686cdd19SJun-ichiro itojun Hagino 	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
295686cdd19SJun-ichiro itojun Hagino 	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
296686cdd19SJun-ichiro itojun Hagino 	 */
297686cdd19SJun-ichiro itojun Hagino 	bzero(&a, sizeof(a));
298686cdd19SJun-ichiro itojun Hagino 	a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr;
299686cdd19SJun-ichiro itojun Hagino 	a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
300686cdd19SJun-ichiro itojun Hagino 	b = ip.ip_src;
301686cdd19SJun-ichiro itojun Hagino 	b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
302686cdd19SJun-ichiro itojun Hagino 	if (a.s_addr != b.s_addr)
303686cdd19SJun-ichiro itojun Hagino 		return 0;
304686cdd19SJun-ichiro itojun Hagino 
305686cdd19SJun-ichiro itojun Hagino 	/* stf interface makes single side match only */
306686cdd19SJun-ichiro itojun Hagino 	return 32;
307686cdd19SJun-ichiro itojun Hagino }
308686cdd19SJun-ichiro itojun Hagino 
309686cdd19SJun-ichiro itojun Hagino static struct in6_ifaddr *
310686cdd19SJun-ichiro itojun Hagino stf_getsrcifa6(ifp)
311686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
312686cdd19SJun-ichiro itojun Hagino {
313686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ia;
314686cdd19SJun-ichiro itojun Hagino 	struct in_ifaddr *ia4;
315686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *sin6;
316686cdd19SJun-ichiro itojun Hagino 	struct in_addr in;
317686cdd19SJun-ichiro itojun Hagino 
31822f29826SPoul-Henning Kamp 	for (ia = TAILQ_FIRST(&ifp->if_addrlist);
319686cdd19SJun-ichiro itojun Hagino 	     ia;
32022f29826SPoul-Henning Kamp 	     ia = TAILQ_NEXT(ia, ifa_list))
321686cdd19SJun-ichiro itojun Hagino 	{
322686cdd19SJun-ichiro itojun Hagino 		if (ia->ifa_addr == NULL)
323686cdd19SJun-ichiro itojun Hagino 			continue;
324686cdd19SJun-ichiro itojun Hagino 		if (ia->ifa_addr->sa_family != AF_INET6)
325686cdd19SJun-ichiro itojun Hagino 			continue;
326686cdd19SJun-ichiro itojun Hagino 		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
327686cdd19SJun-ichiro itojun Hagino 		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
328686cdd19SJun-ichiro itojun Hagino 			continue;
329686cdd19SJun-ichiro itojun Hagino 
330686cdd19SJun-ichiro itojun Hagino 		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
331cf912c89SJonathan Lemon 		LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
332686cdd19SJun-ichiro itojun Hagino 			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
333686cdd19SJun-ichiro itojun Hagino 				break;
334686cdd19SJun-ichiro itojun Hagino 		if (ia4 == NULL)
335686cdd19SJun-ichiro itojun Hagino 			continue;
336686cdd19SJun-ichiro itojun Hagino 
337686cdd19SJun-ichiro itojun Hagino 		return (struct in6_ifaddr *)ia;
338686cdd19SJun-ichiro itojun Hagino 	}
339686cdd19SJun-ichiro itojun Hagino 
340686cdd19SJun-ichiro itojun Hagino 	return NULL;
341686cdd19SJun-ichiro itojun Hagino }
342686cdd19SJun-ichiro itojun Hagino 
343686cdd19SJun-ichiro itojun Hagino static int
344686cdd19SJun-ichiro itojun Hagino stf_output(ifp, m, dst, rt)
345686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
346686cdd19SJun-ichiro itojun Hagino 	struct mbuf *m;
347686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst;
348686cdd19SJun-ichiro itojun Hagino 	struct rtentry *rt;
349686cdd19SJun-ichiro itojun Hagino {
350686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
351686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *dst6;
35233841545SHajimu UMEMOTO 	struct in_addr *in4;
353686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in *dst4;
354686cdd19SJun-ichiro itojun Hagino 	u_int8_t tos;
355686cdd19SJun-ichiro itojun Hagino 	struct ip *ip;
356686cdd19SJun-ichiro itojun Hagino 	struct ip6_hdr *ip6;
357686cdd19SJun-ichiro itojun Hagino 	struct in6_ifaddr *ia6;
358686cdd19SJun-ichiro itojun Hagino 
359686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc*)ifp;
360686cdd19SJun-ichiro itojun Hagino 	dst6 = (struct sockaddr_in6 *)dst;
361686cdd19SJun-ichiro itojun Hagino 
362686cdd19SJun-ichiro itojun Hagino 	/* just in case */
363686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_UP) == 0) {
364686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
365686cdd19SJun-ichiro itojun Hagino 		return ENETDOWN;
366686cdd19SJun-ichiro itojun Hagino 	}
367686cdd19SJun-ichiro itojun Hagino 
368686cdd19SJun-ichiro itojun Hagino 	/*
369686cdd19SJun-ichiro itojun Hagino 	 * If we don't have an ip4 address that match my inner ip6 address,
370686cdd19SJun-ichiro itojun Hagino 	 * we shouldn't generate output.  Without this check, we'll end up
371686cdd19SJun-ichiro itojun Hagino 	 * using wrong IPv4 source.
372686cdd19SJun-ichiro itojun Hagino 	 */
373686cdd19SJun-ichiro itojun Hagino 	ia6 = stf_getsrcifa6(ifp);
374686cdd19SJun-ichiro itojun Hagino 	if (ia6 == NULL) {
375686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
376686cdd19SJun-ichiro itojun Hagino 		return ENETDOWN;
377686cdd19SJun-ichiro itojun Hagino 	}
378686cdd19SJun-ichiro itojun Hagino 
379686cdd19SJun-ichiro itojun Hagino 	if (m->m_len < sizeof(*ip6)) {
380686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(*ip6));
381686cdd19SJun-ichiro itojun Hagino 		if (!m)
382686cdd19SJun-ichiro itojun Hagino 			return ENOBUFS;
383686cdd19SJun-ichiro itojun Hagino 	}
384686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
385686cdd19SJun-ichiro itojun Hagino 	tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
386686cdd19SJun-ichiro itojun Hagino 
38733841545SHajimu UMEMOTO 	/*
38833841545SHajimu UMEMOTO 	 * Pickup the right outer dst addr from the list of candidates.
38933841545SHajimu UMEMOTO 	 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
39033841545SHajimu UMEMOTO 	 */
39133841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
39233841545SHajimu UMEMOTO 		in4 = GET_V4(&ip6->ip6_dst);
39333841545SHajimu UMEMOTO 	else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
39433841545SHajimu UMEMOTO 		in4 = GET_V4(&dst6->sin6_addr);
39533841545SHajimu UMEMOTO 	else {
39633841545SHajimu UMEMOTO 		m_freem(m);
39733841545SHajimu UMEMOTO 		return ENETUNREACH;
39833841545SHajimu UMEMOTO 	}
39933841545SHajimu UMEMOTO 
4008acb2290SHajimu UMEMOTO #if NBPFILTER > 0
4018acb2290SHajimu UMEMOTO 	if (ifp->if_bpf) {
4028acb2290SHajimu UMEMOTO 		/*
4038acb2290SHajimu UMEMOTO 		 * We need to prepend the address family as
4048acb2290SHajimu UMEMOTO 		 * a four byte field.  Cons up a dummy header
4058acb2290SHajimu UMEMOTO 		 * to pacify bpf.  This is safe because bpf
4068acb2290SHajimu UMEMOTO 		 * will only read from the mbuf (i.e., it won't
4078acb2290SHajimu UMEMOTO 		 * try to free it or keep a pointer a to it).
4088acb2290SHajimu UMEMOTO 		 */
4098acb2290SHajimu UMEMOTO 		struct mbuf m0;
4108acb2290SHajimu UMEMOTO 		u_int32_t af = AF_INET6;
4118acb2290SHajimu UMEMOTO 
4128acb2290SHajimu UMEMOTO 		m0.m_next = m;
4138acb2290SHajimu UMEMOTO 		m0.m_len = 4;
4148acb2290SHajimu UMEMOTO 		m0.m_data = (char *)&af;
4158acb2290SHajimu UMEMOTO 
4168acb2290SHajimu UMEMOTO #ifdef HAVE_OLD_BPF
4178acb2290SHajimu UMEMOTO 		bpf_mtap(ifp, &m0);
4188acb2290SHajimu UMEMOTO #else
4198acb2290SHajimu UMEMOTO 		bpf_mtap(ifp->if_bpf, &m0);
4208acb2290SHajimu UMEMOTO #endif
4218acb2290SHajimu UMEMOTO 	}
4228acb2290SHajimu UMEMOTO #endif /*NBPFILTER > 0*/
4238acb2290SHajimu UMEMOTO 
424686cdd19SJun-ichiro itojun Hagino 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
425686cdd19SJun-ichiro itojun Hagino 	if (m && m->m_len < sizeof(struct ip))
426686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(struct ip));
427686cdd19SJun-ichiro itojun Hagino 	if (m == NULL)
428686cdd19SJun-ichiro itojun Hagino 		return ENOBUFS;
429686cdd19SJun-ichiro itojun Hagino 	ip = mtod(m, struct ip *);
430686cdd19SJun-ichiro itojun Hagino 
431686cdd19SJun-ichiro itojun Hagino 	bzero(ip, sizeof(*ip));
432686cdd19SJun-ichiro itojun Hagino 
433686cdd19SJun-ichiro itojun Hagino 	bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
434686cdd19SJun-ichiro itojun Hagino 	    &ip->ip_src, sizeof(ip->ip_src));
43533841545SHajimu UMEMOTO 	bcopy(in4, &ip->ip_dst, sizeof(ip->ip_dst));
436686cdd19SJun-ichiro itojun Hagino 	ip->ip_p = IPPROTO_IPV6;
43753dab5feSBrooks Davis 	ip->ip_ttl = ip_stf_ttl;
438686cdd19SJun-ichiro itojun Hagino 	ip->ip_len = m->m_pkthdr.len;	/*host order*/
439686cdd19SJun-ichiro itojun Hagino 	if (ifp->if_flags & IFF_LINK1)
440686cdd19SJun-ichiro itojun Hagino 		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
44133841545SHajimu UMEMOTO 	else
44233841545SHajimu UMEMOTO 		ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
443686cdd19SJun-ichiro itojun Hagino 
444686cdd19SJun-ichiro itojun Hagino 	dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
445686cdd19SJun-ichiro itojun Hagino 	if (dst4->sin_family != AF_INET ||
446686cdd19SJun-ichiro itojun Hagino 	    bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
447686cdd19SJun-ichiro itojun Hagino 		/* cache route doesn't match */
448686cdd19SJun-ichiro itojun Hagino 		dst4->sin_family = AF_INET;
449686cdd19SJun-ichiro itojun Hagino 		dst4->sin_len = sizeof(struct sockaddr_in);
450686cdd19SJun-ichiro itojun Hagino 		bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
451686cdd19SJun-ichiro itojun Hagino 		if (sc->sc_ro.ro_rt) {
452686cdd19SJun-ichiro itojun Hagino 			RTFREE(sc->sc_ro.ro_rt);
453686cdd19SJun-ichiro itojun Hagino 			sc->sc_ro.ro_rt = NULL;
454686cdd19SJun-ichiro itojun Hagino 		}
455686cdd19SJun-ichiro itojun Hagino 	}
456686cdd19SJun-ichiro itojun Hagino 
457686cdd19SJun-ichiro itojun Hagino 	if (sc->sc_ro.ro_rt == NULL) {
458686cdd19SJun-ichiro itojun Hagino 		rtalloc(&sc->sc_ro);
459686cdd19SJun-ichiro itojun Hagino 		if (sc->sc_ro.ro_rt == NULL) {
460686cdd19SJun-ichiro itojun Hagino 			m_freem(m);
461686cdd19SJun-ichiro itojun Hagino 			return ENETUNREACH;
462686cdd19SJun-ichiro itojun Hagino 		}
463686cdd19SJun-ichiro itojun Hagino 	}
464686cdd19SJun-ichiro itojun Hagino 
465686cdd19SJun-ichiro itojun Hagino 	return ip_output(m, NULL, &sc->sc_ro, 0, NULL);
466686cdd19SJun-ichiro itojun Hagino }
467686cdd19SJun-ichiro itojun Hagino 
468686cdd19SJun-ichiro itojun Hagino static int
46933841545SHajimu UMEMOTO stf_checkaddr4(sc, in, inifp)
47033841545SHajimu UMEMOTO 	struct stf_softc *sc;
471686cdd19SJun-ichiro itojun Hagino 	struct in_addr *in;
47233841545SHajimu UMEMOTO 	struct ifnet *inifp;	/* incoming interface */
473686cdd19SJun-ichiro itojun Hagino {
474686cdd19SJun-ichiro itojun Hagino 	struct in_ifaddr *ia4;
475686cdd19SJun-ichiro itojun Hagino 
476686cdd19SJun-ichiro itojun Hagino 	/*
477686cdd19SJun-ichiro itojun Hagino 	 * reject packets with the following address:
478686cdd19SJun-ichiro itojun Hagino 	 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
479686cdd19SJun-ichiro itojun Hagino 	 */
480d1d1144bSJun-ichiro itojun Hagino 	if (IN_MULTICAST(ntohl(in->s_addr)))
481686cdd19SJun-ichiro itojun Hagino 		return -1;
482686cdd19SJun-ichiro itojun Hagino 	switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
483686cdd19SJun-ichiro itojun Hagino 	case 0: case 127: case 255:
484686cdd19SJun-ichiro itojun Hagino 		return -1;
485686cdd19SJun-ichiro itojun Hagino 	}
486686cdd19SJun-ichiro itojun Hagino 
487686cdd19SJun-ichiro itojun Hagino 	/*
488686cdd19SJun-ichiro itojun Hagino 	 * reject packets with broadcast
489686cdd19SJun-ichiro itojun Hagino 	 */
490686cdd19SJun-ichiro itojun Hagino 	for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
491686cdd19SJun-ichiro itojun Hagino 	     ia4;
492686cdd19SJun-ichiro itojun Hagino 	     ia4 = TAILQ_NEXT(ia4, ia_link))
493686cdd19SJun-ichiro itojun Hagino 	{
494686cdd19SJun-ichiro itojun Hagino 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
495686cdd19SJun-ichiro itojun Hagino 			continue;
496686cdd19SJun-ichiro itojun Hagino 		if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
497686cdd19SJun-ichiro itojun Hagino 			return -1;
498686cdd19SJun-ichiro itojun Hagino 	}
499686cdd19SJun-ichiro itojun Hagino 
500686cdd19SJun-ichiro itojun Hagino 	/*
501686cdd19SJun-ichiro itojun Hagino 	 * perform ingress filter
502686cdd19SJun-ichiro itojun Hagino 	 */
50333841545SHajimu UMEMOTO 	if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) {
504686cdd19SJun-ichiro itojun Hagino 		struct sockaddr_in sin;
505686cdd19SJun-ichiro itojun Hagino 		struct rtentry *rt;
506686cdd19SJun-ichiro itojun Hagino 
507686cdd19SJun-ichiro itojun Hagino 		bzero(&sin, sizeof(sin));
508686cdd19SJun-ichiro itojun Hagino 		sin.sin_family = AF_INET;
509686cdd19SJun-ichiro itojun Hagino 		sin.sin_len = sizeof(struct sockaddr_in);
510686cdd19SJun-ichiro itojun Hagino 		sin.sin_addr = *in;
511686cdd19SJun-ichiro itojun Hagino 		rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
51233841545SHajimu UMEMOTO 		if (!rt || rt->rt_ifp != inifp) {
51333841545SHajimu UMEMOTO #if 0
51433841545SHajimu UMEMOTO 			log(LOG_WARNING, "%s: packet from 0x%x dropped "
51533841545SHajimu UMEMOTO 			    "due to ingress filter\n", if_name(&sc->sc_if),
51633841545SHajimu UMEMOTO 			    (u_int32_t)ntohl(sin.sin_addr.s_addr));
51733841545SHajimu UMEMOTO #endif
51833841545SHajimu UMEMOTO 			if (rt)
519686cdd19SJun-ichiro itojun Hagino 				rtfree(rt);
520686cdd19SJun-ichiro itojun Hagino 			return -1;
521686cdd19SJun-ichiro itojun Hagino 		}
522686cdd19SJun-ichiro itojun Hagino 		rtfree(rt);
523686cdd19SJun-ichiro itojun Hagino 	}
524686cdd19SJun-ichiro itojun Hagino 
525686cdd19SJun-ichiro itojun Hagino 	return 0;
526686cdd19SJun-ichiro itojun Hagino }
527686cdd19SJun-ichiro itojun Hagino 
528686cdd19SJun-ichiro itojun Hagino static int
52933841545SHajimu UMEMOTO stf_checkaddr6(sc, in6, inifp)
53033841545SHajimu UMEMOTO 	struct stf_softc *sc;
531686cdd19SJun-ichiro itojun Hagino 	struct in6_addr *in6;
53233841545SHajimu UMEMOTO 	struct ifnet *inifp;	/* incoming interface */
533686cdd19SJun-ichiro itojun Hagino {
534686cdd19SJun-ichiro itojun Hagino 	/*
535686cdd19SJun-ichiro itojun Hagino 	 * check 6to4 addresses
536686cdd19SJun-ichiro itojun Hagino 	 */
537686cdd19SJun-ichiro itojun Hagino 	if (IN6_IS_ADDR_6TO4(in6))
53833841545SHajimu UMEMOTO 		return stf_checkaddr4(sc, GET_V4(in6), inifp);
539686cdd19SJun-ichiro itojun Hagino 
540686cdd19SJun-ichiro itojun Hagino 	/*
541686cdd19SJun-ichiro itojun Hagino 	 * reject anything that look suspicious.  the test is implemented
542686cdd19SJun-ichiro itojun Hagino 	 * in ip6_input too, but we check here as well to
543686cdd19SJun-ichiro itojun Hagino 	 * (1) reject bad packets earlier, and
544686cdd19SJun-ichiro itojun Hagino 	 * (2) to be safe against future ip6_input change.
545686cdd19SJun-ichiro itojun Hagino 	 */
546686cdd19SJun-ichiro itojun Hagino 	if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
547686cdd19SJun-ichiro itojun Hagino 		return -1;
548686cdd19SJun-ichiro itojun Hagino 
549686cdd19SJun-ichiro itojun Hagino 	return 0;
550686cdd19SJun-ichiro itojun Hagino }
551686cdd19SJun-ichiro itojun Hagino 
552686cdd19SJun-ichiro itojun Hagino void
553ff265614SJulian Elischer in_stf_input(m, off)
55433841545SHajimu UMEMOTO 	struct mbuf *m;
555ff265614SJulian Elischer 	int off;
556686cdd19SJun-ichiro itojun Hagino {
557ff265614SJulian Elischer 	int proto;
558686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
559686cdd19SJun-ichiro itojun Hagino 	struct ip *ip;
560686cdd19SJun-ichiro itojun Hagino 	struct ip6_hdr *ip6;
561686cdd19SJun-ichiro itojun Hagino 	u_int8_t otos, itos;
562df5e1987SJonathan Lemon 	int len, isr;
563686cdd19SJun-ichiro itojun Hagino 	struct ifqueue *ifq = NULL;
564686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
565686cdd19SJun-ichiro itojun Hagino 
566f0ffb944SJulian Elischer 	proto = mtod(m, struct ip *)->ip_p;
567686cdd19SJun-ichiro itojun Hagino 
568686cdd19SJun-ichiro itojun Hagino 	if (proto != IPPROTO_IPV6) {
569686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
570686cdd19SJun-ichiro itojun Hagino 		return;
571686cdd19SJun-ichiro itojun Hagino 	}
572686cdd19SJun-ichiro itojun Hagino 
573686cdd19SJun-ichiro itojun Hagino 	ip = mtod(m, struct ip *);
574686cdd19SJun-ichiro itojun Hagino 
575686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc *)encap_getarg(m);
576686cdd19SJun-ichiro itojun Hagino 
577686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
578686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
579686cdd19SJun-ichiro itojun Hagino 		return;
580686cdd19SJun-ichiro itojun Hagino 	}
581686cdd19SJun-ichiro itojun Hagino 
582686cdd19SJun-ichiro itojun Hagino 	ifp = &sc->sc_if;
583686cdd19SJun-ichiro itojun Hagino 
584686cdd19SJun-ichiro itojun Hagino 	/*
585686cdd19SJun-ichiro itojun Hagino 	 * perform sanity check against outer src/dst.
586686cdd19SJun-ichiro itojun Hagino 	 * for source, perform ingress filter as well.
587686cdd19SJun-ichiro itojun Hagino 	 */
58833841545SHajimu UMEMOTO 	if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
58933841545SHajimu UMEMOTO 	    stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
590686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
591686cdd19SJun-ichiro itojun Hagino 		return;
592686cdd19SJun-ichiro itojun Hagino 	}
593686cdd19SJun-ichiro itojun Hagino 
594686cdd19SJun-ichiro itojun Hagino 	otos = ip->ip_tos;
595686cdd19SJun-ichiro itojun Hagino 	m_adj(m, off);
596686cdd19SJun-ichiro itojun Hagino 
597686cdd19SJun-ichiro itojun Hagino 	if (m->m_len < sizeof(*ip6)) {
598686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(*ip6));
599686cdd19SJun-ichiro itojun Hagino 		if (!m)
600686cdd19SJun-ichiro itojun Hagino 			return;
601686cdd19SJun-ichiro itojun Hagino 	}
602686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
603686cdd19SJun-ichiro itojun Hagino 
604686cdd19SJun-ichiro itojun Hagino 	/*
605686cdd19SJun-ichiro itojun Hagino 	 * perform sanity check against inner src/dst.
606686cdd19SJun-ichiro itojun Hagino 	 * for source, perform ingress filter as well.
607686cdd19SJun-ichiro itojun Hagino 	 */
60833841545SHajimu UMEMOTO 	if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
60933841545SHajimu UMEMOTO 	    stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
610686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
611686cdd19SJun-ichiro itojun Hagino 		return;
612686cdd19SJun-ichiro itojun Hagino 	}
613686cdd19SJun-ichiro itojun Hagino 
614686cdd19SJun-ichiro itojun Hagino 	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
615686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_LINK1) != 0)
616686cdd19SJun-ichiro itojun Hagino 		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
61733841545SHajimu UMEMOTO 	else
61833841545SHajimu UMEMOTO 		ip_ecn_egress(ECN_NOCARE, &otos, &itos);
619686cdd19SJun-ichiro itojun Hagino 	ip6->ip6_flow &= ~htonl(0xff << 20);
620686cdd19SJun-ichiro itojun Hagino 	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
621686cdd19SJun-ichiro itojun Hagino 
622686cdd19SJun-ichiro itojun Hagino 	m->m_pkthdr.rcvif = ifp;
623686cdd19SJun-ichiro itojun Hagino 
624686cdd19SJun-ichiro itojun Hagino 	if (ifp->if_bpf) {
625686cdd19SJun-ichiro itojun Hagino 		/*
626686cdd19SJun-ichiro itojun Hagino 		 * We need to prepend the address family as
627686cdd19SJun-ichiro itojun Hagino 		 * a four byte field.  Cons up a dummy header
628686cdd19SJun-ichiro itojun Hagino 		 * to pacify bpf.  This is safe because bpf
629686cdd19SJun-ichiro itojun Hagino 		 * will only read from the mbuf (i.e., it won't
630686cdd19SJun-ichiro itojun Hagino 		 * try to free it or keep a pointer a to it).
631686cdd19SJun-ichiro itojun Hagino 		 */
632686cdd19SJun-ichiro itojun Hagino 		struct mbuf m0;
63333841545SHajimu UMEMOTO 		u_int32_t af = AF_INET6;
634686cdd19SJun-ichiro itojun Hagino 
635686cdd19SJun-ichiro itojun Hagino 		m0.m_next = m;
636686cdd19SJun-ichiro itojun Hagino 		m0.m_len = 4;
637686cdd19SJun-ichiro itojun Hagino 		m0.m_data = (char *)&af;
638686cdd19SJun-ichiro itojun Hagino 
639686cdd19SJun-ichiro itojun Hagino #ifdef HAVE_OLD_BPF
640686cdd19SJun-ichiro itojun Hagino 		bpf_mtap(ifp, &m0);
641686cdd19SJun-ichiro itojun Hagino #else
642686cdd19SJun-ichiro itojun Hagino 		bpf_mtap(ifp->if_bpf, &m0);
643686cdd19SJun-ichiro itojun Hagino #endif
644686cdd19SJun-ichiro itojun Hagino 	}
645686cdd19SJun-ichiro itojun Hagino 
646686cdd19SJun-ichiro itojun Hagino 	/*
647686cdd19SJun-ichiro itojun Hagino 	 * Put the packet to the network layer input queue according to the
648686cdd19SJun-ichiro itojun Hagino 	 * specified address family.
649686cdd19SJun-ichiro itojun Hagino 	 * See net/if_gif.c for possible issues with packet processing
650686cdd19SJun-ichiro itojun Hagino 	 * reorder due to extra queueing.
651686cdd19SJun-ichiro itojun Hagino 	 */
652686cdd19SJun-ichiro itojun Hagino 	ifq = &ip6intrq;
653686cdd19SJun-ichiro itojun Hagino 	isr = NETISR_IPV6;
654686cdd19SJun-ichiro itojun Hagino 
655df5e1987SJonathan Lemon 	len = m->m_pkthdr.len;
656df5e1987SJonathan Lemon 	if (! IF_HANDOFF(ifq, m, NULL))
657686cdd19SJun-ichiro itojun Hagino 		return;
658686cdd19SJun-ichiro itojun Hagino 	schednetisr(isr);
659686cdd19SJun-ichiro itojun Hagino 	ifp->if_ipackets++;
660df5e1987SJonathan Lemon 	ifp->if_ibytes += len;
661686cdd19SJun-ichiro itojun Hagino }
662686cdd19SJun-ichiro itojun Hagino 
663686cdd19SJun-ichiro itojun Hagino /* ARGSUSED */
664686cdd19SJun-ichiro itojun Hagino static void
6658071913dSRuslan Ermilov stf_rtrequest(cmd, rt, info)
666686cdd19SJun-ichiro itojun Hagino 	int cmd;
667686cdd19SJun-ichiro itojun Hagino 	struct rtentry *rt;
6688071913dSRuslan Ermilov 	struct rt_addrinfo *info;
669686cdd19SJun-ichiro itojun Hagino {
670686cdd19SJun-ichiro itojun Hagino 
671686cdd19SJun-ichiro itojun Hagino 	if (rt)
672686cdd19SJun-ichiro itojun Hagino 		rt->rt_rmx.rmx_mtu = IPV6_MMTU;
673686cdd19SJun-ichiro itojun Hagino }
674686cdd19SJun-ichiro itojun Hagino 
675686cdd19SJun-ichiro itojun Hagino static int
676686cdd19SJun-ichiro itojun Hagino stf_ioctl(ifp, cmd, data)
677686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
678686cdd19SJun-ichiro itojun Hagino 	u_long cmd;
679686cdd19SJun-ichiro itojun Hagino 	caddr_t data;
680686cdd19SJun-ichiro itojun Hagino {
681686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ifa;
682686cdd19SJun-ichiro itojun Hagino 	struct ifreq *ifr;
683686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *sin6;
684686cdd19SJun-ichiro itojun Hagino 	int error;
685686cdd19SJun-ichiro itojun Hagino 
686686cdd19SJun-ichiro itojun Hagino 	error = 0;
687686cdd19SJun-ichiro itojun Hagino 	switch (cmd) {
688686cdd19SJun-ichiro itojun Hagino 	case SIOCSIFADDR:
689686cdd19SJun-ichiro itojun Hagino 		ifa = (struct ifaddr *)data;
690686cdd19SJun-ichiro itojun Hagino 		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
691686cdd19SJun-ichiro itojun Hagino 			error = EAFNOSUPPORT;
692686cdd19SJun-ichiro itojun Hagino 			break;
693686cdd19SJun-ichiro itojun Hagino 		}
694686cdd19SJun-ichiro itojun Hagino 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
695686cdd19SJun-ichiro itojun Hagino 		if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
696686cdd19SJun-ichiro itojun Hagino 			ifa->ifa_rtrequest = stf_rtrequest;
697686cdd19SJun-ichiro itojun Hagino 			ifp->if_flags |= IFF_UP;
698686cdd19SJun-ichiro itojun Hagino 		} else
699686cdd19SJun-ichiro itojun Hagino 			error = EINVAL;
700686cdd19SJun-ichiro itojun Hagino 		break;
701686cdd19SJun-ichiro itojun Hagino 
702686cdd19SJun-ichiro itojun Hagino 	case SIOCADDMULTI:
703686cdd19SJun-ichiro itojun Hagino 	case SIOCDELMULTI:
704686cdd19SJun-ichiro itojun Hagino 		ifr = (struct ifreq *)data;
705686cdd19SJun-ichiro itojun Hagino 		if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
706686cdd19SJun-ichiro itojun Hagino 			;
707686cdd19SJun-ichiro itojun Hagino 		else
708686cdd19SJun-ichiro itojun Hagino 			error = EAFNOSUPPORT;
709686cdd19SJun-ichiro itojun Hagino 		break;
710686cdd19SJun-ichiro itojun Hagino 
711686cdd19SJun-ichiro itojun Hagino 	default:
712686cdd19SJun-ichiro itojun Hagino 		error = EINVAL;
713686cdd19SJun-ichiro itojun Hagino 		break;
714686cdd19SJun-ichiro itojun Hagino 	}
715686cdd19SJun-ichiro itojun Hagino 
716686cdd19SJun-ichiro itojun Hagino 	return error;
717686cdd19SJun-ichiro itojun Hagino }
718