xref: /freebsd/sys/net/if_stf.c (revision 437ffe182391fbcd9826ce327abbd175515b687d)
1686cdd19SJun-ichiro itojun Hagino /*	$FreeBSD$	*/
288ff5695SSUZUKI Shinsuke /*	$KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi 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"
796b459e49SRobert Watson #include "opt_mac.h"
80686cdd19SJun-ichiro itojun Hagino 
81686cdd19SJun-ichiro itojun Hagino #include <sys/param.h>
82686cdd19SJun-ichiro itojun Hagino #include <sys/systm.h>
83686cdd19SJun-ichiro itojun Hagino #include <sys/socket.h>
84686cdd19SJun-ichiro itojun Hagino #include <sys/sockio.h>
856b459e49SRobert Watson #include <sys/mac.h>
86686cdd19SJun-ichiro itojun Hagino #include <sys/mbuf.h>
87686cdd19SJun-ichiro itojun Hagino #include <sys/errno.h>
88686cdd19SJun-ichiro itojun Hagino #include <sys/kernel.h>
89abb64706SBrooks Davis #include <sys/protosw.h>
90abb64706SBrooks Davis #include <sys/queue.h>
91686cdd19SJun-ichiro itojun Hagino #include <machine/cpu.h>
92686cdd19SJun-ichiro itojun Hagino 
93686cdd19SJun-ichiro itojun Hagino #include <sys/malloc.h>
94686cdd19SJun-ichiro itojun Hagino 
95686cdd19SJun-ichiro itojun Hagino #include <net/if.h>
96686cdd19SJun-ichiro itojun Hagino #include <net/route.h>
97686cdd19SJun-ichiro itojun Hagino #include <net/netisr.h>
98686cdd19SJun-ichiro itojun Hagino #include <net/if_types.h>
99686cdd19SJun-ichiro itojun Hagino #include <net/if_stf.h>
100686cdd19SJun-ichiro itojun Hagino 
101686cdd19SJun-ichiro itojun Hagino #include <netinet/in.h>
102686cdd19SJun-ichiro itojun Hagino #include <netinet/in_systm.h>
103686cdd19SJun-ichiro itojun Hagino #include <netinet/ip.h>
104686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_var.h>
105686cdd19SJun-ichiro itojun Hagino #include <netinet/in_var.h>
106686cdd19SJun-ichiro itojun Hagino 
107686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
108686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6_var.h>
109686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_var.h>
110686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_ecn.h>
111686cdd19SJun-ichiro itojun Hagino 
112686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
113686cdd19SJun-ichiro itojun Hagino 
114686cdd19SJun-ichiro itojun Hagino #include <machine/stdarg.h>
115686cdd19SJun-ichiro itojun Hagino 
116686cdd19SJun-ichiro itojun Hagino #include <net/net_osdep.h>
117686cdd19SJun-ichiro itojun Hagino 
118686cdd19SJun-ichiro itojun Hagino #include <net/bpf.h>
119686cdd19SJun-ichiro itojun Hagino 
120abb64706SBrooks Davis #define STFNAME		"stf"
121abb64706SBrooks Davis 
122686cdd19SJun-ichiro itojun Hagino #define IN6_IS_ADDR_6TO4(x)	(ntohs((x)->s6_addr16[0]) == 0x2002)
1238d95b0ceSSUZUKI Shinsuke 
1248d95b0ceSSUZUKI Shinsuke /*
1258d95b0ceSSUZUKI Shinsuke  * XXX: Return a pointer with 16-bit aligned.  Don't cast it to
1268d95b0ceSSUZUKI Shinsuke  * struct in_addr *; use bcopy() instead.
1278d95b0ceSSUZUKI Shinsuke  */
1288d95b0ceSSUZUKI Shinsuke #define GET_V4(x)	((caddr_t)(&(x)->s6_addr16[1]))
129686cdd19SJun-ichiro itojun Hagino 
130686cdd19SJun-ichiro itojun Hagino struct stf_softc {
131686cdd19SJun-ichiro itojun Hagino 	struct ifnet	sc_if;	   /* common area */
132686cdd19SJun-ichiro itojun Hagino 	union {
133686cdd19SJun-ichiro itojun Hagino 		struct route  __sc_ro4;
134686cdd19SJun-ichiro itojun Hagino 		struct route_in6 __sc_ro6; /* just for safety */
135686cdd19SJun-ichiro itojun Hagino 	} __sc_ro46;
136686cdd19SJun-ichiro itojun Hagino #define sc_ro	__sc_ro46.__sc_ro4
137686cdd19SJun-ichiro itojun Hagino 	const struct encaptab *encap_cookie;
138abb64706SBrooks Davis 	LIST_ENTRY(stf_softc) sc_list;	/* all stf's are linked */
139686cdd19SJun-ichiro itojun Hagino };
140686cdd19SJun-ichiro itojun Hagino 
141e8783c4dSMike Smith static LIST_HEAD(, stf_softc) stf_softc_list;
142686cdd19SJun-ichiro itojun Hagino 
143abb64706SBrooks Davis static MALLOC_DEFINE(M_STF, STFNAME, "6to4 Tunnel Interface");
14453dab5feSBrooks Davis static int ip_stf_ttl = 40;
145686cdd19SJun-ichiro itojun Hagino 
14653dab5feSBrooks Davis extern  struct domain inetdomain;
147f0ffb944SJulian Elischer struct protosw in_stf_protosw =
14853dab5feSBrooks Davis { SOCK_RAW,	&inetdomain,	IPPROTO_IPV6,	PR_ATOMIC|PR_ADDR,
149777b9faaSPeter Wemm   in_stf_input,	(pr_output_t*)rip_output, 0,	rip_ctloutput,
15053dab5feSBrooks Davis   0,
15153dab5feSBrooks Davis   0,            0,              0,              0,
15253dab5feSBrooks Davis   &rip_usrreqs
15353dab5feSBrooks Davis };
154686cdd19SJun-ichiro itojun Hagino 
155929ddbbbSAlfred Perlstein static int stfmodevent(module_t, int, void *);
156929ddbbbSAlfred Perlstein static int stf_encapcheck(const struct mbuf *, int, int, void *);
157929ddbbbSAlfred Perlstein static struct in6_ifaddr *stf_getsrcifa6(struct ifnet *);
158929ddbbbSAlfred Perlstein static int stf_output(struct ifnet *, struct mbuf *, struct sockaddr *,
159929ddbbbSAlfred Perlstein 	struct rtentry *);
160ce9d7b2fSHajimu UMEMOTO static int isrfc1918addr(struct in_addr *);
161929ddbbbSAlfred Perlstein static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
162929ddbbbSAlfred Perlstein 	struct ifnet *);
163929ddbbbSAlfred Perlstein static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
164929ddbbbSAlfred Perlstein 	struct ifnet *);
165929ddbbbSAlfred Perlstein static void stf_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
166929ddbbbSAlfred Perlstein static int stf_ioctl(struct ifnet *, u_long, caddr_t);
167686cdd19SJun-ichiro itojun Hagino 
168929ddbbbSAlfred Perlstein int	stf_clone_create(struct if_clone *, int);
169ae5a19beSBrooks Davis void	stf_clone_destroy(struct ifnet *);
170abb64706SBrooks Davis 
1713b16e7b2SMaxime Henrion /* only one clone is currently allowed */
172abb64706SBrooks Davis struct if_clone stf_cloner =
173ae5a19beSBrooks Davis     IF_CLONE_INITIALIZER(STFNAME, stf_clone_create, stf_clone_destroy, 0, 0);
174abb64706SBrooks Davis 
175abb64706SBrooks Davis int
176abb64706SBrooks Davis stf_clone_create(ifc, unit)
177abb64706SBrooks Davis 	struct if_clone *ifc;
1783b16e7b2SMaxime Henrion 	int unit;
179686cdd19SJun-ichiro itojun Hagino {
180686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
181686cdd19SJun-ichiro itojun Hagino 
182a163d034SWarner Losh 	sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
1839bf40edeSBrooks Davis 	if_initname(&sc->sc_if, ifc->ifc_name, unit);
184abb64706SBrooks Davis 
185abb64706SBrooks Davis 	sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
186abb64706SBrooks Davis 	    stf_encapcheck, &in_stf_protosw, sc);
187abb64706SBrooks Davis 	if (sc->encap_cookie == NULL) {
188686cdd19SJun-ichiro itojun Hagino 		printf("%s: attach failed\n", if_name(&sc->sc_if));
189abb64706SBrooks Davis 		free(sc, M_STF);
19053dab5feSBrooks Davis 		return (ENOMEM);
191686cdd19SJun-ichiro itojun Hagino 	}
192686cdd19SJun-ichiro itojun Hagino 
193686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_mtu    = IPV6_MMTU;
194686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_ioctl  = stf_ioctl;
195686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_output = stf_output;
196686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_type   = IFT_STF;
197686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
198686cdd19SJun-ichiro itojun Hagino 	if_attach(&sc->sc_if);
199686cdd19SJun-ichiro itojun Hagino 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
200abb64706SBrooks Davis 	LIST_INSERT_HEAD(&stf_softc_list, sc, sc_list);
201abb64706SBrooks Davis 	return (0);
202abb64706SBrooks Davis }
203abb64706SBrooks Davis 
204ae5a19beSBrooks Davis void
205abb64706SBrooks Davis stf_clone_destroy(ifp)
206abb64706SBrooks Davis 	struct ifnet *ifp;
207abb64706SBrooks Davis {
208abb64706SBrooks Davis 	int err;
209abb64706SBrooks Davis 	struct stf_softc *sc = (void *) ifp;
210abb64706SBrooks Davis 
211abb64706SBrooks Davis 	LIST_REMOVE(sc, sc_list);
21253dab5feSBrooks Davis 	err = encap_detach(sc->encap_cookie);
21353dab5feSBrooks Davis 	KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
214abb64706SBrooks Davis 	bpfdetach(ifp);
215abb64706SBrooks Davis 	if_detach(ifp);
216abb64706SBrooks Davis 
21753dab5feSBrooks Davis 	free(sc, M_STF);
218abb64706SBrooks Davis }
219abb64706SBrooks Davis 
220abb64706SBrooks Davis static int
221abb64706SBrooks Davis stfmodevent(mod, type, data)
222abb64706SBrooks Davis 	module_t mod;
223abb64706SBrooks Davis 	int type;
224abb64706SBrooks Davis 	void *data;
225abb64706SBrooks Davis {
226abb64706SBrooks Davis 
227abb64706SBrooks Davis 	switch (type) {
228abb64706SBrooks Davis 	case MOD_LOAD:
229abb64706SBrooks Davis 		LIST_INIT(&stf_softc_list);
230abb64706SBrooks Davis 		if_clone_attach(&stf_cloner);
231abb64706SBrooks Davis 
232abb64706SBrooks Davis 		break;
233abb64706SBrooks Davis 	case MOD_UNLOAD:
234abb64706SBrooks Davis 		if_clone_detach(&stf_cloner);
235abb64706SBrooks Davis 
236abb64706SBrooks Davis 		while (!LIST_EMPTY(&stf_softc_list))
237abb64706SBrooks Davis 			stf_clone_destroy(&LIST_FIRST(&stf_softc_list)->sc_if);
23853dab5feSBrooks Davis 		break;
239686cdd19SJun-ichiro itojun Hagino 	}
240686cdd19SJun-ichiro itojun Hagino 
24153dab5feSBrooks Davis 	return (0);
24253dab5feSBrooks Davis }
24353dab5feSBrooks Davis 
24453dab5feSBrooks Davis static moduledata_t stf_mod = {
24553dab5feSBrooks Davis 	"if_stf",
24653dab5feSBrooks Davis 	stfmodevent,
24753dab5feSBrooks Davis 	0
24853dab5feSBrooks Davis };
24953dab5feSBrooks Davis 
25053dab5feSBrooks Davis DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
251686cdd19SJun-ichiro itojun Hagino 
252686cdd19SJun-ichiro itojun Hagino static int
253686cdd19SJun-ichiro itojun Hagino stf_encapcheck(m, off, proto, arg)
254686cdd19SJun-ichiro itojun Hagino 	const struct mbuf *m;
255686cdd19SJun-ichiro itojun Hagino 	int off;
256686cdd19SJun-ichiro itojun Hagino 	int proto;
257686cdd19SJun-ichiro itojun Hagino 	void *arg;
258686cdd19SJun-ichiro itojun Hagino {
259686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
260686cdd19SJun-ichiro itojun Hagino 	struct in6_ifaddr *ia6;
261686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
2622b0e5976SBill Fenner 	struct in_addr a, b, mask;
263686cdd19SJun-ichiro itojun Hagino 
264686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc *)arg;
265686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
266686cdd19SJun-ichiro itojun Hagino 		return 0;
267686cdd19SJun-ichiro itojun Hagino 
268686cdd19SJun-ichiro itojun Hagino 	if ((sc->sc_if.if_flags & IFF_UP) == 0)
269686cdd19SJun-ichiro itojun Hagino 		return 0;
270686cdd19SJun-ichiro itojun Hagino 
27133841545SHajimu UMEMOTO 	/* IFF_LINK0 means "no decapsulation" */
27233841545SHajimu UMEMOTO 	if ((sc->sc_if.if_flags & IFF_LINK0) != 0)
27333841545SHajimu UMEMOTO 		return 0;
27433841545SHajimu UMEMOTO 
275686cdd19SJun-ichiro itojun Hagino 	if (proto != IPPROTO_IPV6)
276686cdd19SJun-ichiro itojun Hagino 		return 0;
277686cdd19SJun-ichiro itojun Hagino 
278686cdd19SJun-ichiro itojun Hagino 	/* LINTED const cast */
279777b9faaSPeter Wemm 	m_copydata((struct mbuf *)(uintptr_t)m, 0, sizeof(ip), (caddr_t)&ip);
280686cdd19SJun-ichiro itojun Hagino 
281686cdd19SJun-ichiro itojun Hagino 	if (ip.ip_v != 4)
282686cdd19SJun-ichiro itojun Hagino 		return 0;
283686cdd19SJun-ichiro itojun Hagino 
284686cdd19SJun-ichiro itojun Hagino 	ia6 = stf_getsrcifa6(&sc->sc_if);
285686cdd19SJun-ichiro itojun Hagino 	if (ia6 == NULL)
286686cdd19SJun-ichiro itojun Hagino 		return 0;
287686cdd19SJun-ichiro itojun Hagino 
288686cdd19SJun-ichiro itojun Hagino 	/*
289686cdd19SJun-ichiro itojun Hagino 	 * check if IPv4 dst matches the IPv4 address derived from the
290686cdd19SJun-ichiro itojun Hagino 	 * local 6to4 address.
291686cdd19SJun-ichiro itojun Hagino 	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
292686cdd19SJun-ichiro itojun Hagino 	 */
293686cdd19SJun-ichiro itojun Hagino 	if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
294686cdd19SJun-ichiro itojun Hagino 	    sizeof(ip.ip_dst)) != 0)
295686cdd19SJun-ichiro itojun Hagino 		return 0;
296686cdd19SJun-ichiro itojun Hagino 
297686cdd19SJun-ichiro itojun Hagino 	/*
298686cdd19SJun-ichiro itojun Hagino 	 * check if IPv4 src matches the IPv4 address derived from the
299686cdd19SJun-ichiro itojun Hagino 	 * local 6to4 address masked by prefixmask.
300686cdd19SJun-ichiro itojun Hagino 	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
301686cdd19SJun-ichiro itojun Hagino 	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
302686cdd19SJun-ichiro itojun Hagino 	 */
303686cdd19SJun-ichiro itojun Hagino 	bzero(&a, sizeof(a));
3042b0e5976SBill Fenner 	bcopy(GET_V4(&ia6->ia_addr.sin6_addr), &a, sizeof(a));
3052b0e5976SBill Fenner 	bcopy(GET_V4(&ia6->ia_prefixmask.sin6_addr), &mask, sizeof(mask));
3062b0e5976SBill Fenner 	a.s_addr &= mask.s_addr;
307686cdd19SJun-ichiro itojun Hagino 	b = ip.ip_src;
3082b0e5976SBill Fenner 	b.s_addr &= mask.s_addr;
309686cdd19SJun-ichiro itojun Hagino 	if (a.s_addr != b.s_addr)
310686cdd19SJun-ichiro itojun Hagino 		return 0;
311686cdd19SJun-ichiro itojun Hagino 
312686cdd19SJun-ichiro itojun Hagino 	/* stf interface makes single side match only */
313686cdd19SJun-ichiro itojun Hagino 	return 32;
314686cdd19SJun-ichiro itojun Hagino }
315686cdd19SJun-ichiro itojun Hagino 
316686cdd19SJun-ichiro itojun Hagino static struct in6_ifaddr *
317686cdd19SJun-ichiro itojun Hagino stf_getsrcifa6(ifp)
318686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
319686cdd19SJun-ichiro itojun Hagino {
320686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ia;
321686cdd19SJun-ichiro itojun Hagino 	struct in_ifaddr *ia4;
322686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *sin6;
323686cdd19SJun-ichiro itojun Hagino 	struct in_addr in;
324686cdd19SJun-ichiro itojun Hagino 
32522f29826SPoul-Henning Kamp 	for (ia = TAILQ_FIRST(&ifp->if_addrlist);
326686cdd19SJun-ichiro itojun Hagino 	     ia;
32722f29826SPoul-Henning Kamp 	     ia = TAILQ_NEXT(ia, ifa_list))
328686cdd19SJun-ichiro itojun Hagino 	{
329686cdd19SJun-ichiro itojun Hagino 		if (ia->ifa_addr == NULL)
330686cdd19SJun-ichiro itojun Hagino 			continue;
331686cdd19SJun-ichiro itojun Hagino 		if (ia->ifa_addr->sa_family != AF_INET6)
332686cdd19SJun-ichiro itojun Hagino 			continue;
333686cdd19SJun-ichiro itojun Hagino 		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
334686cdd19SJun-ichiro itojun Hagino 		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
335686cdd19SJun-ichiro itojun Hagino 			continue;
336686cdd19SJun-ichiro itojun Hagino 
337686cdd19SJun-ichiro itojun Hagino 		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
338cf912c89SJonathan Lemon 		LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
339686cdd19SJun-ichiro itojun Hagino 			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
340686cdd19SJun-ichiro itojun Hagino 				break;
341686cdd19SJun-ichiro itojun Hagino 		if (ia4 == NULL)
342686cdd19SJun-ichiro itojun Hagino 			continue;
343686cdd19SJun-ichiro itojun Hagino 
344686cdd19SJun-ichiro itojun Hagino 		return (struct in6_ifaddr *)ia;
345686cdd19SJun-ichiro itojun Hagino 	}
346686cdd19SJun-ichiro itojun Hagino 
347686cdd19SJun-ichiro itojun Hagino 	return NULL;
348686cdd19SJun-ichiro itojun Hagino }
349686cdd19SJun-ichiro itojun Hagino 
350686cdd19SJun-ichiro itojun Hagino static int
351686cdd19SJun-ichiro itojun Hagino stf_output(ifp, m, dst, rt)
352686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
353686cdd19SJun-ichiro itojun Hagino 	struct mbuf *m;
354686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst;
355686cdd19SJun-ichiro itojun Hagino 	struct rtentry *rt;
356686cdd19SJun-ichiro itojun Hagino {
357686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
358686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *dst6;
3598d95b0ceSSUZUKI Shinsuke 	struct in_addr in4;
3608d95b0ceSSUZUKI Shinsuke 	caddr_t ptr;
361686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in *dst4;
362686cdd19SJun-ichiro itojun Hagino 	u_int8_t tos;
363686cdd19SJun-ichiro itojun Hagino 	struct ip *ip;
364686cdd19SJun-ichiro itojun Hagino 	struct ip6_hdr *ip6;
365686cdd19SJun-ichiro itojun Hagino 	struct in6_ifaddr *ia6;
3666b459e49SRobert Watson #ifdef MAC
3676b459e49SRobert Watson 	int error;
3686b459e49SRobert Watson 
3696b459e49SRobert Watson 	error = mac_check_ifnet_transmit(ifp, m);
3706b459e49SRobert Watson 	if (error) {
3716b459e49SRobert Watson 		m_freem(m);
3726b459e49SRobert Watson 		return (error);
3736b459e49SRobert Watson 	}
3746b459e49SRobert Watson #endif
375686cdd19SJun-ichiro itojun Hagino 
376686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc*)ifp;
377686cdd19SJun-ichiro itojun Hagino 	dst6 = (struct sockaddr_in6 *)dst;
378686cdd19SJun-ichiro itojun Hagino 
379686cdd19SJun-ichiro itojun Hagino 	/* just in case */
380686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_UP) == 0) {
381686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
382f26b2d5bSHajimu UMEMOTO 		ifp->if_oerrors++;
383686cdd19SJun-ichiro itojun Hagino 		return ENETDOWN;
384686cdd19SJun-ichiro itojun Hagino 	}
385686cdd19SJun-ichiro itojun Hagino 
386686cdd19SJun-ichiro itojun Hagino 	/*
387686cdd19SJun-ichiro itojun Hagino 	 * If we don't have an ip4 address that match my inner ip6 address,
388686cdd19SJun-ichiro itojun Hagino 	 * we shouldn't generate output.  Without this check, we'll end up
389686cdd19SJun-ichiro itojun Hagino 	 * using wrong IPv4 source.
390686cdd19SJun-ichiro itojun Hagino 	 */
391686cdd19SJun-ichiro itojun Hagino 	ia6 = stf_getsrcifa6(ifp);
392686cdd19SJun-ichiro itojun Hagino 	if (ia6 == NULL) {
393686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
394f26b2d5bSHajimu UMEMOTO 		ifp->if_oerrors++;
395686cdd19SJun-ichiro itojun Hagino 		return ENETDOWN;
396686cdd19SJun-ichiro itojun Hagino 	}
397686cdd19SJun-ichiro itojun Hagino 
398686cdd19SJun-ichiro itojun Hagino 	if (m->m_len < sizeof(*ip6)) {
399686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(*ip6));
400f26b2d5bSHajimu UMEMOTO 		if (!m) {
401f26b2d5bSHajimu UMEMOTO 			ifp->if_oerrors++;
402686cdd19SJun-ichiro itojun Hagino 			return ENOBUFS;
403686cdd19SJun-ichiro itojun Hagino 		}
404f26b2d5bSHajimu UMEMOTO 	}
405686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
406686cdd19SJun-ichiro itojun Hagino 	tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
407686cdd19SJun-ichiro itojun Hagino 
40833841545SHajimu UMEMOTO 	/*
40933841545SHajimu UMEMOTO 	 * Pickup the right outer dst addr from the list of candidates.
41033841545SHajimu UMEMOTO 	 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
41133841545SHajimu UMEMOTO 	 */
4128d95b0ceSSUZUKI Shinsuke 	ptr = NULL;
41333841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
4148d95b0ceSSUZUKI Shinsuke 		ptr = GET_V4(&ip6->ip6_dst);
41533841545SHajimu UMEMOTO 	else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
4168d95b0ceSSUZUKI Shinsuke 		ptr = GET_V4(&dst6->sin6_addr);
41733841545SHajimu UMEMOTO 	else {
41833841545SHajimu UMEMOTO 		m_freem(m);
419f26b2d5bSHajimu UMEMOTO 		ifp->if_oerrors++;
42033841545SHajimu UMEMOTO 		return ENETUNREACH;
42133841545SHajimu UMEMOTO 	}
4228d95b0ceSSUZUKI Shinsuke 	bcopy(ptr, &in4, sizeof(in4));
42333841545SHajimu UMEMOTO 
4248acb2290SHajimu UMEMOTO #if NBPFILTER > 0
4258acb2290SHajimu UMEMOTO 	if (ifp->if_bpf) {
4268acb2290SHajimu UMEMOTO 		/*
4278acb2290SHajimu UMEMOTO 		 * We need to prepend the address family as
4288acb2290SHajimu UMEMOTO 		 * a four byte field.  Cons up a dummy header
4298acb2290SHajimu UMEMOTO 		 * to pacify bpf.  This is safe because bpf
4308acb2290SHajimu UMEMOTO 		 * will only read from the mbuf (i.e., it won't
4318acb2290SHajimu UMEMOTO 		 * try to free it or keep a pointer a to it).
4328acb2290SHajimu UMEMOTO 		 */
4338acb2290SHajimu UMEMOTO 		u_int32_t af = AF_INET6;
434437ffe18SSam Leffler #ifdef HAVE_OLD_BPF
435437ffe18SSam Leffler 		struct mbuf m0;
4368acb2290SHajimu UMEMOTO 
4378acb2290SHajimu UMEMOTO 		m0.m_next = m;
4388acb2290SHajimu UMEMOTO 		m0.m_len = 4;
4398acb2290SHajimu UMEMOTO 		m0.m_data = (char *)&af;
4408acb2290SHajimu UMEMOTO 
4416fc32a24SSam Leffler 		BPF_MTAP(ifp, &m0);
4428acb2290SHajimu UMEMOTO #else
443437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
4448acb2290SHajimu UMEMOTO #endif
4458acb2290SHajimu UMEMOTO 	}
4468acb2290SHajimu UMEMOTO #endif /*NBPFILTER > 0*/
4478acb2290SHajimu UMEMOTO 
448a163d034SWarner Losh 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
449686cdd19SJun-ichiro itojun Hagino 	if (m && m->m_len < sizeof(struct ip))
450686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(struct ip));
451f26b2d5bSHajimu UMEMOTO 	if (m == NULL) {
452f26b2d5bSHajimu UMEMOTO 		ifp->if_oerrors++;
453686cdd19SJun-ichiro itojun Hagino 		return ENOBUFS;
454f26b2d5bSHajimu UMEMOTO 	}
455686cdd19SJun-ichiro itojun Hagino 	ip = mtod(m, struct ip *);
456686cdd19SJun-ichiro itojun Hagino 
457686cdd19SJun-ichiro itojun Hagino 	bzero(ip, sizeof(*ip));
458686cdd19SJun-ichiro itojun Hagino 
459686cdd19SJun-ichiro itojun Hagino 	bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
460686cdd19SJun-ichiro itojun Hagino 	    &ip->ip_src, sizeof(ip->ip_src));
4618d95b0ceSSUZUKI Shinsuke 	bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst));
462686cdd19SJun-ichiro itojun Hagino 	ip->ip_p = IPPROTO_IPV6;
46353dab5feSBrooks Davis 	ip->ip_ttl = ip_stf_ttl;
464686cdd19SJun-ichiro itojun Hagino 	ip->ip_len = m->m_pkthdr.len;	/*host order*/
465686cdd19SJun-ichiro itojun Hagino 	if (ifp->if_flags & IFF_LINK1)
466686cdd19SJun-ichiro itojun Hagino 		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
46733841545SHajimu UMEMOTO 	else
46833841545SHajimu UMEMOTO 		ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
469686cdd19SJun-ichiro itojun Hagino 
470686cdd19SJun-ichiro itojun Hagino 	dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
471686cdd19SJun-ichiro itojun Hagino 	if (dst4->sin_family != AF_INET ||
472686cdd19SJun-ichiro itojun Hagino 	    bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
473686cdd19SJun-ichiro itojun Hagino 		/* cache route doesn't match */
474686cdd19SJun-ichiro itojun Hagino 		dst4->sin_family = AF_INET;
475686cdd19SJun-ichiro itojun Hagino 		dst4->sin_len = sizeof(struct sockaddr_in);
476686cdd19SJun-ichiro itojun Hagino 		bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
477686cdd19SJun-ichiro itojun Hagino 		if (sc->sc_ro.ro_rt) {
478686cdd19SJun-ichiro itojun Hagino 			RTFREE(sc->sc_ro.ro_rt);
479686cdd19SJun-ichiro itojun Hagino 			sc->sc_ro.ro_rt = NULL;
480686cdd19SJun-ichiro itojun Hagino 		}
481686cdd19SJun-ichiro itojun Hagino 	}
482686cdd19SJun-ichiro itojun Hagino 
483686cdd19SJun-ichiro itojun Hagino 	if (sc->sc_ro.ro_rt == NULL) {
484686cdd19SJun-ichiro itojun Hagino 		rtalloc(&sc->sc_ro);
485686cdd19SJun-ichiro itojun Hagino 		if (sc->sc_ro.ro_rt == NULL) {
486686cdd19SJun-ichiro itojun Hagino 			m_freem(m);
487f26b2d5bSHajimu UMEMOTO 			ifp->if_oerrors++;
488686cdd19SJun-ichiro itojun Hagino 			return ENETUNREACH;
489686cdd19SJun-ichiro itojun Hagino 		}
490686cdd19SJun-ichiro itojun Hagino 	}
491686cdd19SJun-ichiro itojun Hagino 
492f26b2d5bSHajimu UMEMOTO 	ifp->if_opackets++;
4935d846453SSam Leffler 	return ip_output(m, NULL, &sc->sc_ro, 0, NULL, NULL);
494686cdd19SJun-ichiro itojun Hagino }
495686cdd19SJun-ichiro itojun Hagino 
496686cdd19SJun-ichiro itojun Hagino static int
497ce9d7b2fSHajimu UMEMOTO isrfc1918addr(in)
4988d95b0ceSSUZUKI Shinsuke 	struct in_addr *in;
499ce9d7b2fSHajimu UMEMOTO {
500ce9d7b2fSHajimu UMEMOTO 	/*
501ce9d7b2fSHajimu UMEMOTO 	 * returns 1 if private address range:
502ce9d7b2fSHajimu UMEMOTO 	 * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
503ce9d7b2fSHajimu UMEMOTO 	 */
5048d95b0ceSSUZUKI Shinsuke 	if ((ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
5058d95b0ceSSUZUKI Shinsuke 	    (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
5068d95b0ceSSUZUKI Shinsuke 	    (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168)
507ce9d7b2fSHajimu UMEMOTO 		return 1;
508ce9d7b2fSHajimu UMEMOTO 
509ce9d7b2fSHajimu UMEMOTO 	return 0;
510ce9d7b2fSHajimu UMEMOTO }
511ce9d7b2fSHajimu UMEMOTO 
512ce9d7b2fSHajimu UMEMOTO static int
51333841545SHajimu UMEMOTO stf_checkaddr4(sc, in, inifp)
51433841545SHajimu UMEMOTO 	struct stf_softc *sc;
5158d95b0ceSSUZUKI Shinsuke 	struct in_addr *in;
51633841545SHajimu UMEMOTO 	struct ifnet *inifp;	/* incoming interface */
517686cdd19SJun-ichiro itojun Hagino {
518686cdd19SJun-ichiro itojun Hagino 	struct in_ifaddr *ia4;
519686cdd19SJun-ichiro itojun Hagino 
520686cdd19SJun-ichiro itojun Hagino 	/*
521686cdd19SJun-ichiro itojun Hagino 	 * reject packets with the following address:
522686cdd19SJun-ichiro itojun Hagino 	 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
523686cdd19SJun-ichiro itojun Hagino 	 */
5248d95b0ceSSUZUKI Shinsuke 	if (IN_MULTICAST(ntohl(in->s_addr)))
525686cdd19SJun-ichiro itojun Hagino 		return -1;
5268d95b0ceSSUZUKI Shinsuke 	switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
5278d95b0ceSSUZUKI Shinsuke 	case 0: case 127: case 255:
528686cdd19SJun-ichiro itojun Hagino 		return -1;
5298d95b0ceSSUZUKI Shinsuke 	}
530686cdd19SJun-ichiro itojun Hagino 
531686cdd19SJun-ichiro itojun Hagino 	/*
532ce9d7b2fSHajimu UMEMOTO 	 * reject packets with private address range.
533ce9d7b2fSHajimu UMEMOTO 	 * (requirement from RFC3056 section 2 1st paragraph)
534ce9d7b2fSHajimu UMEMOTO 	 */
535ce9d7b2fSHajimu UMEMOTO 	if (isrfc1918addr(in))
536ce9d7b2fSHajimu UMEMOTO 		return -1;
537ce9d7b2fSHajimu UMEMOTO 
538ce9d7b2fSHajimu UMEMOTO 	/*
539686cdd19SJun-ichiro itojun Hagino 	 * reject packets with broadcast
540686cdd19SJun-ichiro itojun Hagino 	 */
541686cdd19SJun-ichiro itojun Hagino 	for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
542686cdd19SJun-ichiro itojun Hagino 	     ia4;
543686cdd19SJun-ichiro itojun Hagino 	     ia4 = TAILQ_NEXT(ia4, ia_link))
544686cdd19SJun-ichiro itojun Hagino 	{
545686cdd19SJun-ichiro itojun Hagino 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
546686cdd19SJun-ichiro itojun Hagino 			continue;
5478d95b0ceSSUZUKI Shinsuke 		if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
548686cdd19SJun-ichiro itojun Hagino 			return -1;
549686cdd19SJun-ichiro itojun Hagino 	}
550686cdd19SJun-ichiro itojun Hagino 
551686cdd19SJun-ichiro itojun Hagino 	/*
552686cdd19SJun-ichiro itojun Hagino 	 * perform ingress filter
553686cdd19SJun-ichiro itojun Hagino 	 */
55433841545SHajimu UMEMOTO 	if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) {
555686cdd19SJun-ichiro itojun Hagino 		struct sockaddr_in sin;
556686cdd19SJun-ichiro itojun Hagino 		struct rtentry *rt;
557686cdd19SJun-ichiro itojun Hagino 
558686cdd19SJun-ichiro itojun Hagino 		bzero(&sin, sizeof(sin));
559686cdd19SJun-ichiro itojun Hagino 		sin.sin_family = AF_INET;
560686cdd19SJun-ichiro itojun Hagino 		sin.sin_len = sizeof(struct sockaddr_in);
5618d95b0ceSSUZUKI Shinsuke 		sin.sin_addr = *in;
562686cdd19SJun-ichiro itojun Hagino 		rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
56333841545SHajimu UMEMOTO 		if (!rt || rt->rt_ifp != inifp) {
56433841545SHajimu UMEMOTO #if 0
56533841545SHajimu UMEMOTO 			log(LOG_WARNING, "%s: packet from 0x%x dropped "
56633841545SHajimu UMEMOTO 			    "due to ingress filter\n", if_name(&sc->sc_if),
56733841545SHajimu UMEMOTO 			    (u_int32_t)ntohl(sin.sin_addr.s_addr));
56833841545SHajimu UMEMOTO #endif
56933841545SHajimu UMEMOTO 			if (rt)
570686cdd19SJun-ichiro itojun Hagino 				rtfree(rt);
571686cdd19SJun-ichiro itojun Hagino 			return -1;
572686cdd19SJun-ichiro itojun Hagino 		}
573686cdd19SJun-ichiro itojun Hagino 		rtfree(rt);
574686cdd19SJun-ichiro itojun Hagino 	}
575686cdd19SJun-ichiro itojun Hagino 
576686cdd19SJun-ichiro itojun Hagino 	return 0;
577686cdd19SJun-ichiro itojun Hagino }
578686cdd19SJun-ichiro itojun Hagino 
579686cdd19SJun-ichiro itojun Hagino static int
58033841545SHajimu UMEMOTO stf_checkaddr6(sc, in6, inifp)
58133841545SHajimu UMEMOTO 	struct stf_softc *sc;
582686cdd19SJun-ichiro itojun Hagino 	struct in6_addr *in6;
58333841545SHajimu UMEMOTO 	struct ifnet *inifp;	/* incoming interface */
584686cdd19SJun-ichiro itojun Hagino {
585686cdd19SJun-ichiro itojun Hagino 	/*
586686cdd19SJun-ichiro itojun Hagino 	 * check 6to4 addresses
587686cdd19SJun-ichiro itojun Hagino 	 */
5888d95b0ceSSUZUKI Shinsuke 	if (IN6_IS_ADDR_6TO4(in6)) {
5898d95b0ceSSUZUKI Shinsuke 		struct in_addr in4;
5908d95b0ceSSUZUKI Shinsuke 		bcopy(GET_V4(in6), &in4, sizeof(in4));
5918d95b0ceSSUZUKI Shinsuke 		return stf_checkaddr4(sc, &in4, inifp);
5928d95b0ceSSUZUKI Shinsuke 	}
593686cdd19SJun-ichiro itojun Hagino 
594686cdd19SJun-ichiro itojun Hagino 	/*
595686cdd19SJun-ichiro itojun Hagino 	 * reject anything that look suspicious.  the test is implemented
596686cdd19SJun-ichiro itojun Hagino 	 * in ip6_input too, but we check here as well to
597686cdd19SJun-ichiro itojun Hagino 	 * (1) reject bad packets earlier, and
598686cdd19SJun-ichiro itojun Hagino 	 * (2) to be safe against future ip6_input change.
599686cdd19SJun-ichiro itojun Hagino 	 */
600686cdd19SJun-ichiro itojun Hagino 	if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
601686cdd19SJun-ichiro itojun Hagino 		return -1;
602686cdd19SJun-ichiro itojun Hagino 
603686cdd19SJun-ichiro itojun Hagino 	return 0;
604686cdd19SJun-ichiro itojun Hagino }
605686cdd19SJun-ichiro itojun Hagino 
606686cdd19SJun-ichiro itojun Hagino void
607ff265614SJulian Elischer in_stf_input(m, off)
60833841545SHajimu UMEMOTO 	struct mbuf *m;
609ff265614SJulian Elischer 	int off;
610686cdd19SJun-ichiro itojun Hagino {
611ff265614SJulian Elischer 	int proto;
612686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
613686cdd19SJun-ichiro itojun Hagino 	struct ip *ip;
614686cdd19SJun-ichiro itojun Hagino 	struct ip6_hdr *ip6;
615686cdd19SJun-ichiro itojun Hagino 	u_int8_t otos, itos;
616686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
617686cdd19SJun-ichiro itojun Hagino 
618f0ffb944SJulian Elischer 	proto = mtod(m, struct ip *)->ip_p;
619686cdd19SJun-ichiro itojun Hagino 
620686cdd19SJun-ichiro itojun Hagino 	if (proto != IPPROTO_IPV6) {
621686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
622686cdd19SJun-ichiro itojun Hagino 		return;
623686cdd19SJun-ichiro itojun Hagino 	}
624686cdd19SJun-ichiro itojun Hagino 
625686cdd19SJun-ichiro itojun Hagino 	ip = mtod(m, struct ip *);
626686cdd19SJun-ichiro itojun Hagino 
627686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc *)encap_getarg(m);
628686cdd19SJun-ichiro itojun Hagino 
629686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
630686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
631686cdd19SJun-ichiro itojun Hagino 		return;
632686cdd19SJun-ichiro itojun Hagino 	}
633686cdd19SJun-ichiro itojun Hagino 
634686cdd19SJun-ichiro itojun Hagino 	ifp = &sc->sc_if;
635686cdd19SJun-ichiro itojun Hagino 
6366b459e49SRobert Watson #ifdef MAC
6376b459e49SRobert Watson 	mac_create_mbuf_from_ifnet(ifp, m);
6386b459e49SRobert Watson #endif
6396b459e49SRobert Watson 
640686cdd19SJun-ichiro itojun Hagino 	/*
641686cdd19SJun-ichiro itojun Hagino 	 * perform sanity check against outer src/dst.
642686cdd19SJun-ichiro itojun Hagino 	 * for source, perform ingress filter as well.
643686cdd19SJun-ichiro itojun Hagino 	 */
64433841545SHajimu UMEMOTO 	if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
64533841545SHajimu UMEMOTO 	    stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
646686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
647686cdd19SJun-ichiro itojun Hagino 		return;
648686cdd19SJun-ichiro itojun Hagino 	}
649686cdd19SJun-ichiro itojun Hagino 
650686cdd19SJun-ichiro itojun Hagino 	otos = ip->ip_tos;
651686cdd19SJun-ichiro itojun Hagino 	m_adj(m, off);
652686cdd19SJun-ichiro itojun Hagino 
653686cdd19SJun-ichiro itojun Hagino 	if (m->m_len < sizeof(*ip6)) {
654686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(*ip6));
655686cdd19SJun-ichiro itojun Hagino 		if (!m)
656686cdd19SJun-ichiro itojun Hagino 			return;
657686cdd19SJun-ichiro itojun Hagino 	}
658686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
659686cdd19SJun-ichiro itojun Hagino 
660686cdd19SJun-ichiro itojun Hagino 	/*
661686cdd19SJun-ichiro itojun Hagino 	 * perform sanity check against inner src/dst.
662686cdd19SJun-ichiro itojun Hagino 	 * for source, perform ingress filter as well.
663686cdd19SJun-ichiro itojun Hagino 	 */
66433841545SHajimu UMEMOTO 	if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
66533841545SHajimu UMEMOTO 	    stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
666686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
667686cdd19SJun-ichiro itojun Hagino 		return;
668686cdd19SJun-ichiro itojun Hagino 	}
669686cdd19SJun-ichiro itojun Hagino 
670686cdd19SJun-ichiro itojun Hagino 	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
671686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_LINK1) != 0)
672686cdd19SJun-ichiro itojun Hagino 		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
67333841545SHajimu UMEMOTO 	else
67433841545SHajimu UMEMOTO 		ip_ecn_egress(ECN_NOCARE, &otos, &itos);
675686cdd19SJun-ichiro itojun Hagino 	ip6->ip6_flow &= ~htonl(0xff << 20);
676686cdd19SJun-ichiro itojun Hagino 	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
677686cdd19SJun-ichiro itojun Hagino 
678686cdd19SJun-ichiro itojun Hagino 	m->m_pkthdr.rcvif = ifp;
679686cdd19SJun-ichiro itojun Hagino 
680686cdd19SJun-ichiro itojun Hagino 	if (ifp->if_bpf) {
681686cdd19SJun-ichiro itojun Hagino 		/*
682686cdd19SJun-ichiro itojun Hagino 		 * We need to prepend the address family as
683686cdd19SJun-ichiro itojun Hagino 		 * a four byte field.  Cons up a dummy header
684686cdd19SJun-ichiro itojun Hagino 		 * to pacify bpf.  This is safe because bpf
685686cdd19SJun-ichiro itojun Hagino 		 * will only read from the mbuf (i.e., it won't
686686cdd19SJun-ichiro itojun Hagino 		 * try to free it or keep a pointer a to it).
687686cdd19SJun-ichiro itojun Hagino 		 */
68833841545SHajimu UMEMOTO 		u_int32_t af = AF_INET6;
689437ffe18SSam Leffler #ifdef HAVE_OLD_BPF
690437ffe18SSam Leffler 		struct mbuf m0;
691686cdd19SJun-ichiro itojun Hagino 
692686cdd19SJun-ichiro itojun Hagino 		m0.m_next = m;
693686cdd19SJun-ichiro itojun Hagino 		m0.m_len = 4;
694686cdd19SJun-ichiro itojun Hagino 		m0.m_data = (char *)&af;
695686cdd19SJun-ichiro itojun Hagino 
6966fc32a24SSam Leffler 		BPF_MTAP(ifp, &m0);
697686cdd19SJun-ichiro itojun Hagino #else
698437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af, sizeof(ah), m);
699686cdd19SJun-ichiro itojun Hagino #endif
700686cdd19SJun-ichiro itojun Hagino 	}
701686cdd19SJun-ichiro itojun Hagino 
702686cdd19SJun-ichiro itojun Hagino 	/*
703686cdd19SJun-ichiro itojun Hagino 	 * Put the packet to the network layer input queue according to the
704686cdd19SJun-ichiro itojun Hagino 	 * specified address family.
705686cdd19SJun-ichiro itojun Hagino 	 * See net/if_gif.c for possible issues with packet processing
706686cdd19SJun-ichiro itojun Hagino 	 * reorder due to extra queueing.
707686cdd19SJun-ichiro itojun Hagino 	 */
708686cdd19SJun-ichiro itojun Hagino 	ifp->if_ipackets++;
7091cafed39SJonathan Lemon 	ifp->if_ibytes += m->m_pkthdr.len;
7101cafed39SJonathan Lemon 	netisr_dispatch(NETISR_IPV6, m);
711686cdd19SJun-ichiro itojun Hagino }
712686cdd19SJun-ichiro itojun Hagino 
713686cdd19SJun-ichiro itojun Hagino /* ARGSUSED */
714686cdd19SJun-ichiro itojun Hagino static void
7158071913dSRuslan Ermilov stf_rtrequest(cmd, rt, info)
716686cdd19SJun-ichiro itojun Hagino 	int cmd;
717686cdd19SJun-ichiro itojun Hagino 	struct rtentry *rt;
7188071913dSRuslan Ermilov 	struct rt_addrinfo *info;
719686cdd19SJun-ichiro itojun Hagino {
720d1dd20beSSam Leffler 	RT_LOCK_ASSERT(rt);
721686cdd19SJun-ichiro itojun Hagino 
722686cdd19SJun-ichiro itojun Hagino 	if (rt)
723686cdd19SJun-ichiro itojun Hagino 		rt->rt_rmx.rmx_mtu = IPV6_MMTU;
724686cdd19SJun-ichiro itojun Hagino }
725686cdd19SJun-ichiro itojun Hagino 
726686cdd19SJun-ichiro itojun Hagino static int
727686cdd19SJun-ichiro itojun Hagino stf_ioctl(ifp, cmd, data)
728686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
729686cdd19SJun-ichiro itojun Hagino 	u_long cmd;
730686cdd19SJun-ichiro itojun Hagino 	caddr_t data;
731686cdd19SJun-ichiro itojun Hagino {
732686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ifa;
733686cdd19SJun-ichiro itojun Hagino 	struct ifreq *ifr;
734686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *sin6;
7358d95b0ceSSUZUKI Shinsuke 	struct in_addr addr;
736686cdd19SJun-ichiro itojun Hagino 	int error;
737686cdd19SJun-ichiro itojun Hagino 
738686cdd19SJun-ichiro itojun Hagino 	error = 0;
739686cdd19SJun-ichiro itojun Hagino 	switch (cmd) {
740686cdd19SJun-ichiro itojun Hagino 	case SIOCSIFADDR:
741686cdd19SJun-ichiro itojun Hagino 		ifa = (struct ifaddr *)data;
742686cdd19SJun-ichiro itojun Hagino 		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
743686cdd19SJun-ichiro itojun Hagino 			error = EAFNOSUPPORT;
744686cdd19SJun-ichiro itojun Hagino 			break;
745686cdd19SJun-ichiro itojun Hagino 		}
746686cdd19SJun-ichiro itojun Hagino 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
7478d95b0ceSSUZUKI Shinsuke 		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
7488d95b0ceSSUZUKI Shinsuke 			error = EINVAL;
7498d95b0ceSSUZUKI Shinsuke 			break;
7508d95b0ceSSUZUKI Shinsuke 		}
7518d95b0ceSSUZUKI Shinsuke 		bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr));
7528d95b0ceSSUZUKI Shinsuke 		if (isrfc1918addr(&addr)) {
7538d95b0ceSSUZUKI Shinsuke 			error = EINVAL;
7548d95b0ceSSUZUKI Shinsuke 			break;
7558d95b0ceSSUZUKI Shinsuke 		}
7568d95b0ceSSUZUKI Shinsuke 
757686cdd19SJun-ichiro itojun Hagino 		ifa->ifa_rtrequest = stf_rtrequest;
758686cdd19SJun-ichiro itojun Hagino 		ifp->if_flags |= IFF_UP;
759686cdd19SJun-ichiro itojun Hagino 		break;
760686cdd19SJun-ichiro itojun Hagino 
761686cdd19SJun-ichiro itojun Hagino 	case SIOCADDMULTI:
762686cdd19SJun-ichiro itojun Hagino 	case SIOCDELMULTI:
763686cdd19SJun-ichiro itojun Hagino 		ifr = (struct ifreq *)data;
764686cdd19SJun-ichiro itojun Hagino 		if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
765686cdd19SJun-ichiro itojun Hagino 			;
766686cdd19SJun-ichiro itojun Hagino 		else
767686cdd19SJun-ichiro itojun Hagino 			error = EAFNOSUPPORT;
768686cdd19SJun-ichiro itojun Hagino 		break;
769686cdd19SJun-ichiro itojun Hagino 
770686cdd19SJun-ichiro itojun Hagino 	default:
771686cdd19SJun-ichiro itojun Hagino 		error = EINVAL;
772686cdd19SJun-ichiro itojun Hagino 		break;
773686cdd19SJun-ichiro itojun Hagino 	}
774686cdd19SJun-ichiro itojun Hagino 
775686cdd19SJun-ichiro itojun Hagino 	return error;
776686cdd19SJun-ichiro itojun Hagino }
777