xref: /freebsd/sys/net/if_stf.c (revision 8071913df2dce4a8c721eeecb338c9fb7d0f8485)
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>
89abb64706SBrooks Davis #include <machine/bus.h>	/* XXX: Shouldn't really be required! */
90abb64706SBrooks Davis #include <sys/rman.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 #define STF_MAXUNIT	0	/* only one is currently allowed */
122abb64706SBrooks Davis 
123686cdd19SJun-ichiro itojun Hagino #define IN6_IS_ADDR_6TO4(x)	(ntohs((x)->s6_addr16[0]) == 0x2002)
124686cdd19SJun-ichiro itojun Hagino #define GET_V4(x)	((struct in_addr *)(&(x)->s6_addr16[1]))
125686cdd19SJun-ichiro itojun Hagino 
126686cdd19SJun-ichiro itojun Hagino struct stf_softc {
127686cdd19SJun-ichiro itojun Hagino 	struct ifnet	sc_if;	   /* common area */
128686cdd19SJun-ichiro itojun Hagino 	union {
129686cdd19SJun-ichiro itojun Hagino 		struct route  __sc_ro4;
130686cdd19SJun-ichiro itojun Hagino 		struct route_in6 __sc_ro6; /* just for safety */
131686cdd19SJun-ichiro itojun Hagino 	} __sc_ro46;
132686cdd19SJun-ichiro itojun Hagino #define sc_ro	__sc_ro46.__sc_ro4
133686cdd19SJun-ichiro itojun Hagino 	const struct encaptab *encap_cookie;
134abb64706SBrooks Davis 	struct resource *r_unit;	/* resource allocated for this unit */
135abb64706SBrooks Davis 	LIST_ENTRY(stf_softc) sc_list;	/* all stf's are linked */
136686cdd19SJun-ichiro itojun Hagino };
137686cdd19SJun-ichiro itojun Hagino 
138abb64706SBrooks Davis LIST_HEAD(, stf_softc) stf_softc_list;
139686cdd19SJun-ichiro itojun Hagino 
140abb64706SBrooks Davis static MALLOC_DEFINE(M_STF, STFNAME, "6to4 Tunnel Interface");
141abb64706SBrooks Davis static struct rman stfunits[1];
14253dab5feSBrooks Davis static int ip_stf_ttl = 40;
143686cdd19SJun-ichiro itojun Hagino 
14453dab5feSBrooks Davis extern  struct domain inetdomain;
145f0ffb944SJulian Elischer struct protosw in_stf_protosw =
14653dab5feSBrooks Davis { SOCK_RAW,	&inetdomain,	IPPROTO_IPV6,	PR_ATOMIC|PR_ADDR,
14753dab5feSBrooks Davis   in_stf_input, rip_output,	0,		rip_ctloutput,
14853dab5feSBrooks Davis   0,
14953dab5feSBrooks Davis   0,            0,              0,              0,
15053dab5feSBrooks Davis   &rip_usrreqs
15153dab5feSBrooks Davis };
152686cdd19SJun-ichiro itojun Hagino 
15353dab5feSBrooks Davis static int stfmodevent __P((module_t, int, void *));
154686cdd19SJun-ichiro itojun Hagino static int stf_encapcheck __P((const struct mbuf *, int, int, void *));
155686cdd19SJun-ichiro itojun Hagino static struct in6_ifaddr *stf_getsrcifa6 __P((struct ifnet *));
156686cdd19SJun-ichiro itojun Hagino static int stf_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
157686cdd19SJun-ichiro itojun Hagino 	struct rtentry *));
15833841545SHajimu UMEMOTO static int stf_checkaddr4 __P((struct stf_softc *, struct in_addr *,
15933841545SHajimu UMEMOTO 	struct ifnet *));
16033841545SHajimu UMEMOTO static int stf_checkaddr6 __P((struct stf_softc *, struct in6_addr *,
16133841545SHajimu UMEMOTO 	struct ifnet *));
1628071913dSRuslan Ermilov static void stf_rtrequest __P((int, struct rtentry *, struct rt_addrinfo *));
163686cdd19SJun-ichiro itojun Hagino static int stf_ioctl __P((struct ifnet *, u_long, caddr_t));
164686cdd19SJun-ichiro itojun Hagino 
165abb64706SBrooks Davis int	stf_clone_create __P((struct if_clone *, int *));
166abb64706SBrooks Davis void	stf_clone_destroy __P((struct ifnet *));
167abb64706SBrooks Davis 
168abb64706SBrooks Davis struct if_clone stf_cloner =
169abb64706SBrooks Davis     IF_CLONE_INITIALIZER(STFNAME, stf_clone_create, stf_clone_destroy);
170abb64706SBrooks Davis 
171abb64706SBrooks Davis int
172abb64706SBrooks Davis stf_clone_create(ifc, unit)
173abb64706SBrooks Davis 	struct if_clone *ifc;
174abb64706SBrooks Davis 	int *unit;
175686cdd19SJun-ichiro itojun Hagino {
176abb64706SBrooks Davis 	struct resource *r;
177686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
178686cdd19SJun-ichiro itojun Hagino 
179abb64706SBrooks Davis 	if (*unit > STF_MAXUNIT)
180abb64706SBrooks Davis 		return (ENXIO);
181686cdd19SJun-ichiro itojun Hagino 
182abb64706SBrooks Davis 	if (*unit < 0) {
183abb64706SBrooks Davis 		 r = rman_reserve_resource(stfunits, 0, STF_MAXUNIT, 1,
184abb64706SBrooks Davis 		     RF_ALLOCATED | RF_ACTIVE, NULL);
185abb64706SBrooks Davis 		 if (r == NULL)
186abb64706SBrooks Davis 			return (ENOSPC);
187abb64706SBrooks Davis 		 *unit = rman_get_start(r);
188abb64706SBrooks Davis 	} else {
189abb64706SBrooks Davis 		r = rman_reserve_resource(stfunits, *unit, *unit, 1,
190abb64706SBrooks Davis 		    RF_ALLOCATED | RF_ACTIVE, NULL);
191abb64706SBrooks Davis 		if (r == NULL)
192abb64706SBrooks Davis 			 return (EEXIST);
193abb64706SBrooks Davis 	}
194686cdd19SJun-ichiro itojun Hagino 
195abb64706SBrooks Davis 	sc = malloc(sizeof(struct stf_softc), M_STF, M_WAIT);
196abb64706SBrooks Davis 	bzero(sc, sizeof(struct stf_softc));
197abb64706SBrooks Davis 
198abb64706SBrooks Davis 	sc->sc_if.if_name = STFNAME;
199abb64706SBrooks Davis 	sc->sc_if.if_unit = *unit;
200abb64706SBrooks Davis 	sc->r_unit = r;
201abb64706SBrooks Davis 
202abb64706SBrooks Davis 	sc->encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV6,
203abb64706SBrooks Davis 	    stf_encapcheck, &in_stf_protosw, sc);
204abb64706SBrooks Davis 	if (sc->encap_cookie == NULL) {
205686cdd19SJun-ichiro itojun Hagino 		printf("%s: attach failed\n", if_name(&sc->sc_if));
206abb64706SBrooks Davis 		free(sc, M_STF);
20753dab5feSBrooks Davis 		return (ENOMEM);
208686cdd19SJun-ichiro itojun Hagino 	}
209686cdd19SJun-ichiro itojun Hagino 
210686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_mtu    = IPV6_MMTU;
211686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_flags  = 0;
212686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_ioctl  = stf_ioctl;
213686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_output = stf_output;
214686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_type   = IFT_STF;
215686cdd19SJun-ichiro itojun Hagino 	sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
216686cdd19SJun-ichiro itojun Hagino 	if_attach(&sc->sc_if);
217686cdd19SJun-ichiro itojun Hagino 	bpfattach(&sc->sc_if, DLT_NULL, sizeof(u_int));
218abb64706SBrooks Davis 	LIST_INSERT_HEAD(&stf_softc_list, sc, sc_list);
219abb64706SBrooks Davis 	return (0);
220abb64706SBrooks Davis }
221abb64706SBrooks Davis 
222abb64706SBrooks Davis void
223abb64706SBrooks Davis stf_clone_destroy(ifp)
224abb64706SBrooks Davis 	struct ifnet *ifp;
225abb64706SBrooks Davis {
226abb64706SBrooks Davis 	int err;
227abb64706SBrooks Davis 	struct stf_softc *sc = (void *) ifp;
228abb64706SBrooks Davis 
229abb64706SBrooks Davis 	LIST_REMOVE(sc, sc_list);
23053dab5feSBrooks Davis 	err = encap_detach(sc->encap_cookie);
23153dab5feSBrooks Davis 	KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
232abb64706SBrooks Davis 	bpfdetach(ifp);
233abb64706SBrooks Davis 	if_detach(ifp);
234abb64706SBrooks Davis 
235abb64706SBrooks Davis 	err = rman_release_resource(sc->r_unit);
236abb64706SBrooks Davis 	KASSERT(err == 0, ("Unexpected error freeing resource"));
237abb64706SBrooks Davis 
23853dab5feSBrooks Davis 	free(sc, M_STF);
239abb64706SBrooks Davis }
240abb64706SBrooks Davis 
241abb64706SBrooks Davis static int
242abb64706SBrooks Davis stfmodevent(mod, type, data)
243abb64706SBrooks Davis 	module_t mod;
244abb64706SBrooks Davis 	int type;
245abb64706SBrooks Davis 	void *data;
246abb64706SBrooks Davis {
247abb64706SBrooks Davis 	int err;
248abb64706SBrooks Davis 
249abb64706SBrooks Davis 	switch (type) {
250abb64706SBrooks Davis 	case MOD_LOAD:
251abb64706SBrooks Davis 		stfunits->rm_type = RMAN_ARRAY;
252abb64706SBrooks Davis 		stfunits->rm_descr = "configurable if_stf units";
253abb64706SBrooks Davis 		err = rman_init(stfunits);
254abb64706SBrooks Davis 		if (err != 0)
255abb64706SBrooks Davis 			return (err);
256abb64706SBrooks Davis 		err = rman_manage_region(stfunits, 0, STF_MAXUNIT);
257abb64706SBrooks Davis 		if (err != 0) {
258abb64706SBrooks Davis 			printf("%s: stfunits: rman_manage_region: Failed %d\n",
259abb64706SBrooks Davis 			    STFNAME, err);
260abb64706SBrooks Davis 			rman_fini(stfunits);
261abb64706SBrooks Davis 			return (err);
262abb64706SBrooks Davis 		}
263abb64706SBrooks Davis 		LIST_INIT(&stf_softc_list);
264abb64706SBrooks Davis 		if_clone_attach(&stf_cloner);
265abb64706SBrooks Davis 
266abb64706SBrooks Davis 		break;
267abb64706SBrooks Davis 	case MOD_UNLOAD:
268abb64706SBrooks Davis 		if_clone_detach(&stf_cloner);
269abb64706SBrooks Davis 
270abb64706SBrooks Davis 		while (!LIST_EMPTY(&stf_softc_list))
271abb64706SBrooks Davis 			stf_clone_destroy(&LIST_FIRST(&stf_softc_list)->sc_if);
272abb64706SBrooks Davis 
273abb64706SBrooks Davis 		err = rman_fini(stfunits);
274abb64706SBrooks Davis 		KASSERT(err == 0, ("Unexpected error freeing resource"));
275abb64706SBrooks Davis 
27653dab5feSBrooks Davis 		break;
277686cdd19SJun-ichiro itojun Hagino 	}
278686cdd19SJun-ichiro itojun Hagino 
27953dab5feSBrooks Davis 	return (0);
28053dab5feSBrooks Davis }
28153dab5feSBrooks Davis 
28253dab5feSBrooks Davis static moduledata_t stf_mod = {
28353dab5feSBrooks Davis 	"if_stf",
28453dab5feSBrooks Davis 	stfmodevent,
28553dab5feSBrooks Davis 	0
28653dab5feSBrooks Davis };
28753dab5feSBrooks Davis 
28853dab5feSBrooks Davis DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
289686cdd19SJun-ichiro itojun Hagino 
290686cdd19SJun-ichiro itojun Hagino static int
291686cdd19SJun-ichiro itojun Hagino stf_encapcheck(m, off, proto, arg)
292686cdd19SJun-ichiro itojun Hagino 	const struct mbuf *m;
293686cdd19SJun-ichiro itojun Hagino 	int off;
294686cdd19SJun-ichiro itojun Hagino 	int proto;
295686cdd19SJun-ichiro itojun Hagino 	void *arg;
296686cdd19SJun-ichiro itojun Hagino {
297686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
298686cdd19SJun-ichiro itojun Hagino 	struct in6_ifaddr *ia6;
299686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
300686cdd19SJun-ichiro itojun Hagino 	struct in_addr a, b;
301686cdd19SJun-ichiro itojun Hagino 
302686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc *)arg;
303686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
304686cdd19SJun-ichiro itojun Hagino 		return 0;
305686cdd19SJun-ichiro itojun Hagino 
306686cdd19SJun-ichiro itojun Hagino 	if ((sc->sc_if.if_flags & IFF_UP) == 0)
307686cdd19SJun-ichiro itojun Hagino 		return 0;
308686cdd19SJun-ichiro itojun Hagino 
30933841545SHajimu UMEMOTO 	/* IFF_LINK0 means "no decapsulation" */
31033841545SHajimu UMEMOTO 	if ((sc->sc_if.if_flags & IFF_LINK0) != 0)
31133841545SHajimu UMEMOTO 		return 0;
31233841545SHajimu UMEMOTO 
313686cdd19SJun-ichiro itojun Hagino 	if (proto != IPPROTO_IPV6)
314686cdd19SJun-ichiro itojun Hagino 		return 0;
315686cdd19SJun-ichiro itojun Hagino 
316686cdd19SJun-ichiro itojun Hagino 	/* LINTED const cast */
317686cdd19SJun-ichiro itojun Hagino 	m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
318686cdd19SJun-ichiro itojun Hagino 
319686cdd19SJun-ichiro itojun Hagino 	if (ip.ip_v != 4)
320686cdd19SJun-ichiro itojun Hagino 		return 0;
321686cdd19SJun-ichiro itojun Hagino 
322686cdd19SJun-ichiro itojun Hagino 	ia6 = stf_getsrcifa6(&sc->sc_if);
323686cdd19SJun-ichiro itojun Hagino 	if (ia6 == NULL)
324686cdd19SJun-ichiro itojun Hagino 		return 0;
325686cdd19SJun-ichiro itojun Hagino 
326686cdd19SJun-ichiro itojun Hagino 	/*
327686cdd19SJun-ichiro itojun Hagino 	 * check if IPv4 dst matches the IPv4 address derived from the
328686cdd19SJun-ichiro itojun Hagino 	 * local 6to4 address.
329686cdd19SJun-ichiro itojun Hagino 	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
330686cdd19SJun-ichiro itojun Hagino 	 */
331686cdd19SJun-ichiro itojun Hagino 	if (bcmp(GET_V4(&ia6->ia_addr.sin6_addr), &ip.ip_dst,
332686cdd19SJun-ichiro itojun Hagino 	    sizeof(ip.ip_dst)) != 0)
333686cdd19SJun-ichiro itojun Hagino 		return 0;
334686cdd19SJun-ichiro itojun Hagino 
335686cdd19SJun-ichiro itojun Hagino 	/*
336686cdd19SJun-ichiro itojun Hagino 	 * check if IPv4 src matches the IPv4 address derived from the
337686cdd19SJun-ichiro itojun Hagino 	 * local 6to4 address masked by prefixmask.
338686cdd19SJun-ichiro itojun Hagino 	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
339686cdd19SJun-ichiro itojun Hagino 	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
340686cdd19SJun-ichiro itojun Hagino 	 */
341686cdd19SJun-ichiro itojun Hagino 	bzero(&a, sizeof(a));
342686cdd19SJun-ichiro itojun Hagino 	a.s_addr = GET_V4(&ia6->ia_addr.sin6_addr)->s_addr;
343686cdd19SJun-ichiro itojun Hagino 	a.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
344686cdd19SJun-ichiro itojun Hagino 	b = ip.ip_src;
345686cdd19SJun-ichiro itojun Hagino 	b.s_addr &= GET_V4(&ia6->ia_prefixmask.sin6_addr)->s_addr;
346686cdd19SJun-ichiro itojun Hagino 	if (a.s_addr != b.s_addr)
347686cdd19SJun-ichiro itojun Hagino 		return 0;
348686cdd19SJun-ichiro itojun Hagino 
349686cdd19SJun-ichiro itojun Hagino 	/* stf interface makes single side match only */
350686cdd19SJun-ichiro itojun Hagino 	return 32;
351686cdd19SJun-ichiro itojun Hagino }
352686cdd19SJun-ichiro itojun Hagino 
353686cdd19SJun-ichiro itojun Hagino static struct in6_ifaddr *
354686cdd19SJun-ichiro itojun Hagino stf_getsrcifa6(ifp)
355686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
356686cdd19SJun-ichiro itojun Hagino {
357686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ia;
358686cdd19SJun-ichiro itojun Hagino 	struct in_ifaddr *ia4;
359686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *sin6;
360686cdd19SJun-ichiro itojun Hagino 	struct in_addr in;
361686cdd19SJun-ichiro itojun Hagino 
36222f29826SPoul-Henning Kamp 	for (ia = TAILQ_FIRST(&ifp->if_addrlist);
363686cdd19SJun-ichiro itojun Hagino 	     ia;
36422f29826SPoul-Henning Kamp 	     ia = TAILQ_NEXT(ia, ifa_list))
365686cdd19SJun-ichiro itojun Hagino 	{
366686cdd19SJun-ichiro itojun Hagino 		if (ia->ifa_addr == NULL)
367686cdd19SJun-ichiro itojun Hagino 			continue;
368686cdd19SJun-ichiro itojun Hagino 		if (ia->ifa_addr->sa_family != AF_INET6)
369686cdd19SJun-ichiro itojun Hagino 			continue;
370686cdd19SJun-ichiro itojun Hagino 		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
371686cdd19SJun-ichiro itojun Hagino 		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
372686cdd19SJun-ichiro itojun Hagino 			continue;
373686cdd19SJun-ichiro itojun Hagino 
374686cdd19SJun-ichiro itojun Hagino 		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
375cf912c89SJonathan Lemon 		LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
376686cdd19SJun-ichiro itojun Hagino 			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
377686cdd19SJun-ichiro itojun Hagino 				break;
378686cdd19SJun-ichiro itojun Hagino 		if (ia4 == NULL)
379686cdd19SJun-ichiro itojun Hagino 			continue;
380686cdd19SJun-ichiro itojun Hagino 
381686cdd19SJun-ichiro itojun Hagino 		return (struct in6_ifaddr *)ia;
382686cdd19SJun-ichiro itojun Hagino 	}
383686cdd19SJun-ichiro itojun Hagino 
384686cdd19SJun-ichiro itojun Hagino 	return NULL;
385686cdd19SJun-ichiro itojun Hagino }
386686cdd19SJun-ichiro itojun Hagino 
387686cdd19SJun-ichiro itojun Hagino static int
388686cdd19SJun-ichiro itojun Hagino stf_output(ifp, m, dst, rt)
389686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
390686cdd19SJun-ichiro itojun Hagino 	struct mbuf *m;
391686cdd19SJun-ichiro itojun Hagino 	struct sockaddr *dst;
392686cdd19SJun-ichiro itojun Hagino 	struct rtentry *rt;
393686cdd19SJun-ichiro itojun Hagino {
394686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
395686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *dst6;
39633841545SHajimu UMEMOTO 	struct in_addr *in4;
397686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in *dst4;
398686cdd19SJun-ichiro itojun Hagino 	u_int8_t tos;
399686cdd19SJun-ichiro itojun Hagino 	struct ip *ip;
400686cdd19SJun-ichiro itojun Hagino 	struct ip6_hdr *ip6;
401686cdd19SJun-ichiro itojun Hagino 	struct in6_ifaddr *ia6;
402686cdd19SJun-ichiro itojun Hagino 
403686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc*)ifp;
404686cdd19SJun-ichiro itojun Hagino 	dst6 = (struct sockaddr_in6 *)dst;
405686cdd19SJun-ichiro itojun Hagino 
406686cdd19SJun-ichiro itojun Hagino 	/* just in case */
407686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_UP) == 0) {
408686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
409686cdd19SJun-ichiro itojun Hagino 		return ENETDOWN;
410686cdd19SJun-ichiro itojun Hagino 	}
411686cdd19SJun-ichiro itojun Hagino 
412686cdd19SJun-ichiro itojun Hagino 	/*
413686cdd19SJun-ichiro itojun Hagino 	 * If we don't have an ip4 address that match my inner ip6 address,
414686cdd19SJun-ichiro itojun Hagino 	 * we shouldn't generate output.  Without this check, we'll end up
415686cdd19SJun-ichiro itojun Hagino 	 * using wrong IPv4 source.
416686cdd19SJun-ichiro itojun Hagino 	 */
417686cdd19SJun-ichiro itojun Hagino 	ia6 = stf_getsrcifa6(ifp);
418686cdd19SJun-ichiro itojun Hagino 	if (ia6 == NULL) {
419686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
420686cdd19SJun-ichiro itojun Hagino 		return ENETDOWN;
421686cdd19SJun-ichiro itojun Hagino 	}
422686cdd19SJun-ichiro itojun Hagino 
423686cdd19SJun-ichiro itojun Hagino 	if (m->m_len < sizeof(*ip6)) {
424686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(*ip6));
425686cdd19SJun-ichiro itojun Hagino 		if (!m)
426686cdd19SJun-ichiro itojun Hagino 			return ENOBUFS;
427686cdd19SJun-ichiro itojun Hagino 	}
428686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
429686cdd19SJun-ichiro itojun Hagino 	tos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
430686cdd19SJun-ichiro itojun Hagino 
43133841545SHajimu UMEMOTO 	/*
43233841545SHajimu UMEMOTO 	 * Pickup the right outer dst addr from the list of candidates.
43333841545SHajimu UMEMOTO 	 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
43433841545SHajimu UMEMOTO 	 */
43533841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
43633841545SHajimu UMEMOTO 		in4 = GET_V4(&ip6->ip6_dst);
43733841545SHajimu UMEMOTO 	else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
43833841545SHajimu UMEMOTO 		in4 = GET_V4(&dst6->sin6_addr);
43933841545SHajimu UMEMOTO 	else {
44033841545SHajimu UMEMOTO 		m_freem(m);
44133841545SHajimu UMEMOTO 		return ENETUNREACH;
44233841545SHajimu UMEMOTO 	}
44333841545SHajimu UMEMOTO 
4448acb2290SHajimu UMEMOTO #if NBPFILTER > 0
4458acb2290SHajimu UMEMOTO 	if (ifp->if_bpf) {
4468acb2290SHajimu UMEMOTO 		/*
4478acb2290SHajimu UMEMOTO 		 * We need to prepend the address family as
4488acb2290SHajimu UMEMOTO 		 * a four byte field.  Cons up a dummy header
4498acb2290SHajimu UMEMOTO 		 * to pacify bpf.  This is safe because bpf
4508acb2290SHajimu UMEMOTO 		 * will only read from the mbuf (i.e., it won't
4518acb2290SHajimu UMEMOTO 		 * try to free it or keep a pointer a to it).
4528acb2290SHajimu UMEMOTO 		 */
4538acb2290SHajimu UMEMOTO 		struct mbuf m0;
4548acb2290SHajimu UMEMOTO 		u_int32_t af = AF_INET6;
4558acb2290SHajimu UMEMOTO 
4568acb2290SHajimu UMEMOTO 		m0.m_next = m;
4578acb2290SHajimu UMEMOTO 		m0.m_len = 4;
4588acb2290SHajimu UMEMOTO 		m0.m_data = (char *)&af;
4598acb2290SHajimu UMEMOTO 
4608acb2290SHajimu UMEMOTO #ifdef HAVE_OLD_BPF
4618acb2290SHajimu UMEMOTO 		bpf_mtap(ifp, &m0);
4628acb2290SHajimu UMEMOTO #else
4638acb2290SHajimu UMEMOTO 		bpf_mtap(ifp->if_bpf, &m0);
4648acb2290SHajimu UMEMOTO #endif
4658acb2290SHajimu UMEMOTO 	}
4668acb2290SHajimu UMEMOTO #endif /*NBPFILTER > 0*/
4678acb2290SHajimu UMEMOTO 
468686cdd19SJun-ichiro itojun Hagino 	M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
469686cdd19SJun-ichiro itojun Hagino 	if (m && m->m_len < sizeof(struct ip))
470686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(struct ip));
471686cdd19SJun-ichiro itojun Hagino 	if (m == NULL)
472686cdd19SJun-ichiro itojun Hagino 		return ENOBUFS;
473686cdd19SJun-ichiro itojun Hagino 	ip = mtod(m, struct ip *);
474686cdd19SJun-ichiro itojun Hagino 
475686cdd19SJun-ichiro itojun Hagino 	bzero(ip, sizeof(*ip));
476686cdd19SJun-ichiro itojun Hagino 
477686cdd19SJun-ichiro itojun Hagino 	bcopy(GET_V4(&((struct sockaddr_in6 *)&ia6->ia_addr)->sin6_addr),
478686cdd19SJun-ichiro itojun Hagino 	    &ip->ip_src, sizeof(ip->ip_src));
47933841545SHajimu UMEMOTO 	bcopy(in4, &ip->ip_dst, sizeof(ip->ip_dst));
480686cdd19SJun-ichiro itojun Hagino 	ip->ip_p = IPPROTO_IPV6;
48153dab5feSBrooks Davis 	ip->ip_ttl = ip_stf_ttl;
482686cdd19SJun-ichiro itojun Hagino 	ip->ip_len = m->m_pkthdr.len;	/*host order*/
483686cdd19SJun-ichiro itojun Hagino 	if (ifp->if_flags & IFF_LINK1)
484686cdd19SJun-ichiro itojun Hagino 		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
48533841545SHajimu UMEMOTO 	else
48633841545SHajimu UMEMOTO 		ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
487686cdd19SJun-ichiro itojun Hagino 
488686cdd19SJun-ichiro itojun Hagino 	dst4 = (struct sockaddr_in *)&sc->sc_ro.ro_dst;
489686cdd19SJun-ichiro itojun Hagino 	if (dst4->sin_family != AF_INET ||
490686cdd19SJun-ichiro itojun Hagino 	    bcmp(&dst4->sin_addr, &ip->ip_dst, sizeof(ip->ip_dst)) != 0) {
491686cdd19SJun-ichiro itojun Hagino 		/* cache route doesn't match */
492686cdd19SJun-ichiro itojun Hagino 		dst4->sin_family = AF_INET;
493686cdd19SJun-ichiro itojun Hagino 		dst4->sin_len = sizeof(struct sockaddr_in);
494686cdd19SJun-ichiro itojun Hagino 		bcopy(&ip->ip_dst, &dst4->sin_addr, sizeof(dst4->sin_addr));
495686cdd19SJun-ichiro itojun Hagino 		if (sc->sc_ro.ro_rt) {
496686cdd19SJun-ichiro itojun Hagino 			RTFREE(sc->sc_ro.ro_rt);
497686cdd19SJun-ichiro itojun Hagino 			sc->sc_ro.ro_rt = NULL;
498686cdd19SJun-ichiro itojun Hagino 		}
499686cdd19SJun-ichiro itojun Hagino 	}
500686cdd19SJun-ichiro itojun Hagino 
501686cdd19SJun-ichiro itojun Hagino 	if (sc->sc_ro.ro_rt == NULL) {
502686cdd19SJun-ichiro itojun Hagino 		rtalloc(&sc->sc_ro);
503686cdd19SJun-ichiro itojun Hagino 		if (sc->sc_ro.ro_rt == NULL) {
504686cdd19SJun-ichiro itojun Hagino 			m_freem(m);
505686cdd19SJun-ichiro itojun Hagino 			return ENETUNREACH;
506686cdd19SJun-ichiro itojun Hagino 		}
507686cdd19SJun-ichiro itojun Hagino 	}
508686cdd19SJun-ichiro itojun Hagino 
509686cdd19SJun-ichiro itojun Hagino 	return ip_output(m, NULL, &sc->sc_ro, 0, NULL);
510686cdd19SJun-ichiro itojun Hagino }
511686cdd19SJun-ichiro itojun Hagino 
512686cdd19SJun-ichiro itojun Hagino static int
51333841545SHajimu UMEMOTO stf_checkaddr4(sc, in, inifp)
51433841545SHajimu UMEMOTO 	struct stf_softc *sc;
515686cdd19SJun-ichiro itojun Hagino 	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 	 */
524d1d1144bSJun-ichiro itojun Hagino 	if (IN_MULTICAST(ntohl(in->s_addr)))
525686cdd19SJun-ichiro itojun Hagino 		return -1;
526686cdd19SJun-ichiro itojun Hagino 	switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
527686cdd19SJun-ichiro itojun Hagino 	case 0: case 127: case 255:
528686cdd19SJun-ichiro itojun Hagino 		return -1;
529686cdd19SJun-ichiro itojun Hagino 	}
530686cdd19SJun-ichiro itojun Hagino 
531686cdd19SJun-ichiro itojun Hagino 	/*
532686cdd19SJun-ichiro itojun Hagino 	 * reject packets with broadcast
533686cdd19SJun-ichiro itojun Hagino 	 */
534686cdd19SJun-ichiro itojun Hagino 	for (ia4 = TAILQ_FIRST(&in_ifaddrhead);
535686cdd19SJun-ichiro itojun Hagino 	     ia4;
536686cdd19SJun-ichiro itojun Hagino 	     ia4 = TAILQ_NEXT(ia4, ia_link))
537686cdd19SJun-ichiro itojun Hagino 	{
538686cdd19SJun-ichiro itojun Hagino 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
539686cdd19SJun-ichiro itojun Hagino 			continue;
540686cdd19SJun-ichiro itojun Hagino 		if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr)
541686cdd19SJun-ichiro itojun Hagino 			return -1;
542686cdd19SJun-ichiro itojun Hagino 	}
543686cdd19SJun-ichiro itojun Hagino 
544686cdd19SJun-ichiro itojun Hagino 	/*
545686cdd19SJun-ichiro itojun Hagino 	 * perform ingress filter
546686cdd19SJun-ichiro itojun Hagino 	 */
54733841545SHajimu UMEMOTO 	if (sc && (sc->sc_if.if_flags & IFF_LINK2) == 0 && inifp) {
548686cdd19SJun-ichiro itojun Hagino 		struct sockaddr_in sin;
549686cdd19SJun-ichiro itojun Hagino 		struct rtentry *rt;
550686cdd19SJun-ichiro itojun Hagino 
551686cdd19SJun-ichiro itojun Hagino 		bzero(&sin, sizeof(sin));
552686cdd19SJun-ichiro itojun Hagino 		sin.sin_family = AF_INET;
553686cdd19SJun-ichiro itojun Hagino 		sin.sin_len = sizeof(struct sockaddr_in);
554686cdd19SJun-ichiro itojun Hagino 		sin.sin_addr = *in;
555686cdd19SJun-ichiro itojun Hagino 		rt = rtalloc1((struct sockaddr *)&sin, 0, 0UL);
55633841545SHajimu UMEMOTO 		if (!rt || rt->rt_ifp != inifp) {
55733841545SHajimu UMEMOTO #if 0
55833841545SHajimu UMEMOTO 			log(LOG_WARNING, "%s: packet from 0x%x dropped "
55933841545SHajimu UMEMOTO 			    "due to ingress filter\n", if_name(&sc->sc_if),
56033841545SHajimu UMEMOTO 			    (u_int32_t)ntohl(sin.sin_addr.s_addr));
56133841545SHajimu UMEMOTO #endif
56233841545SHajimu UMEMOTO 			if (rt)
563686cdd19SJun-ichiro itojun Hagino 				rtfree(rt);
564686cdd19SJun-ichiro itojun Hagino 			return -1;
565686cdd19SJun-ichiro itojun Hagino 		}
566686cdd19SJun-ichiro itojun Hagino 		rtfree(rt);
567686cdd19SJun-ichiro itojun Hagino 	}
568686cdd19SJun-ichiro itojun Hagino 
569686cdd19SJun-ichiro itojun Hagino 	return 0;
570686cdd19SJun-ichiro itojun Hagino }
571686cdd19SJun-ichiro itojun Hagino 
572686cdd19SJun-ichiro itojun Hagino static int
57333841545SHajimu UMEMOTO stf_checkaddr6(sc, in6, inifp)
57433841545SHajimu UMEMOTO 	struct stf_softc *sc;
575686cdd19SJun-ichiro itojun Hagino 	struct in6_addr *in6;
57633841545SHajimu UMEMOTO 	struct ifnet *inifp;	/* incoming interface */
577686cdd19SJun-ichiro itojun Hagino {
578686cdd19SJun-ichiro itojun Hagino 	/*
579686cdd19SJun-ichiro itojun Hagino 	 * check 6to4 addresses
580686cdd19SJun-ichiro itojun Hagino 	 */
581686cdd19SJun-ichiro itojun Hagino 	if (IN6_IS_ADDR_6TO4(in6))
58233841545SHajimu UMEMOTO 		return stf_checkaddr4(sc, GET_V4(in6), inifp);
583686cdd19SJun-ichiro itojun Hagino 
584686cdd19SJun-ichiro itojun Hagino 	/*
585686cdd19SJun-ichiro itojun Hagino 	 * reject anything that look suspicious.  the test is implemented
586686cdd19SJun-ichiro itojun Hagino 	 * in ip6_input too, but we check here as well to
587686cdd19SJun-ichiro itojun Hagino 	 * (1) reject bad packets earlier, and
588686cdd19SJun-ichiro itojun Hagino 	 * (2) to be safe against future ip6_input change.
589686cdd19SJun-ichiro itojun Hagino 	 */
590686cdd19SJun-ichiro itojun Hagino 	if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
591686cdd19SJun-ichiro itojun Hagino 		return -1;
592686cdd19SJun-ichiro itojun Hagino 
593686cdd19SJun-ichiro itojun Hagino 	return 0;
594686cdd19SJun-ichiro itojun Hagino }
595686cdd19SJun-ichiro itojun Hagino 
596686cdd19SJun-ichiro itojun Hagino void
597ff265614SJulian Elischer in_stf_input(m, off)
59833841545SHajimu UMEMOTO 	struct mbuf *m;
599ff265614SJulian Elischer 	int off;
600686cdd19SJun-ichiro itojun Hagino {
601ff265614SJulian Elischer 	int proto;
602686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
603686cdd19SJun-ichiro itojun Hagino 	struct ip *ip;
604686cdd19SJun-ichiro itojun Hagino 	struct ip6_hdr *ip6;
605686cdd19SJun-ichiro itojun Hagino 	u_int8_t otos, itos;
606df5e1987SJonathan Lemon 	int len, isr;
607686cdd19SJun-ichiro itojun Hagino 	struct ifqueue *ifq = NULL;
608686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
609686cdd19SJun-ichiro itojun Hagino 
610f0ffb944SJulian Elischer 	proto = mtod(m, struct ip *)->ip_p;
611686cdd19SJun-ichiro itojun Hagino 
612686cdd19SJun-ichiro itojun Hagino 	if (proto != IPPROTO_IPV6) {
613686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
614686cdd19SJun-ichiro itojun Hagino 		return;
615686cdd19SJun-ichiro itojun Hagino 	}
616686cdd19SJun-ichiro itojun Hagino 
617686cdd19SJun-ichiro itojun Hagino 	ip = mtod(m, struct ip *);
618686cdd19SJun-ichiro itojun Hagino 
619686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc *)encap_getarg(m);
620686cdd19SJun-ichiro itojun Hagino 
621686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL || (sc->sc_if.if_flags & IFF_UP) == 0) {
622686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
623686cdd19SJun-ichiro itojun Hagino 		return;
624686cdd19SJun-ichiro itojun Hagino 	}
625686cdd19SJun-ichiro itojun Hagino 
626686cdd19SJun-ichiro itojun Hagino 	ifp = &sc->sc_if;
627686cdd19SJun-ichiro itojun Hagino 
628686cdd19SJun-ichiro itojun Hagino 	/*
629686cdd19SJun-ichiro itojun Hagino 	 * perform sanity check against outer src/dst.
630686cdd19SJun-ichiro itojun Hagino 	 * for source, perform ingress filter as well.
631686cdd19SJun-ichiro itojun Hagino 	 */
63233841545SHajimu UMEMOTO 	if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
63333841545SHajimu UMEMOTO 	    stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
634686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
635686cdd19SJun-ichiro itojun Hagino 		return;
636686cdd19SJun-ichiro itojun Hagino 	}
637686cdd19SJun-ichiro itojun Hagino 
638686cdd19SJun-ichiro itojun Hagino 	otos = ip->ip_tos;
639686cdd19SJun-ichiro itojun Hagino 	m_adj(m, off);
640686cdd19SJun-ichiro itojun Hagino 
641686cdd19SJun-ichiro itojun Hagino 	if (m->m_len < sizeof(*ip6)) {
642686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(*ip6));
643686cdd19SJun-ichiro itojun Hagino 		if (!m)
644686cdd19SJun-ichiro itojun Hagino 			return;
645686cdd19SJun-ichiro itojun Hagino 	}
646686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
647686cdd19SJun-ichiro itojun Hagino 
648686cdd19SJun-ichiro itojun Hagino 	/*
649686cdd19SJun-ichiro itojun Hagino 	 * perform sanity check against inner src/dst.
650686cdd19SJun-ichiro itojun Hagino 	 * for source, perform ingress filter as well.
651686cdd19SJun-ichiro itojun Hagino 	 */
65233841545SHajimu UMEMOTO 	if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
65333841545SHajimu UMEMOTO 	    stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
654686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
655686cdd19SJun-ichiro itojun Hagino 		return;
656686cdd19SJun-ichiro itojun Hagino 	}
657686cdd19SJun-ichiro itojun Hagino 
658686cdd19SJun-ichiro itojun Hagino 	itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
659686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_LINK1) != 0)
660686cdd19SJun-ichiro itojun Hagino 		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
66133841545SHajimu UMEMOTO 	else
66233841545SHajimu UMEMOTO 		ip_ecn_egress(ECN_NOCARE, &otos, &itos);
663686cdd19SJun-ichiro itojun Hagino 	ip6->ip6_flow &= ~htonl(0xff << 20);
664686cdd19SJun-ichiro itojun Hagino 	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
665686cdd19SJun-ichiro itojun Hagino 
666686cdd19SJun-ichiro itojun Hagino 	m->m_pkthdr.rcvif = ifp;
667686cdd19SJun-ichiro itojun Hagino 
668686cdd19SJun-ichiro itojun Hagino 	if (ifp->if_bpf) {
669686cdd19SJun-ichiro itojun Hagino 		/*
670686cdd19SJun-ichiro itojun Hagino 		 * We need to prepend the address family as
671686cdd19SJun-ichiro itojun Hagino 		 * a four byte field.  Cons up a dummy header
672686cdd19SJun-ichiro itojun Hagino 		 * to pacify bpf.  This is safe because bpf
673686cdd19SJun-ichiro itojun Hagino 		 * will only read from the mbuf (i.e., it won't
674686cdd19SJun-ichiro itojun Hagino 		 * try to free it or keep a pointer a to it).
675686cdd19SJun-ichiro itojun Hagino 		 */
676686cdd19SJun-ichiro itojun Hagino 		struct mbuf m0;
67733841545SHajimu UMEMOTO 		u_int32_t af = AF_INET6;
678686cdd19SJun-ichiro itojun Hagino 
679686cdd19SJun-ichiro itojun Hagino 		m0.m_next = m;
680686cdd19SJun-ichiro itojun Hagino 		m0.m_len = 4;
681686cdd19SJun-ichiro itojun Hagino 		m0.m_data = (char *)&af;
682686cdd19SJun-ichiro itojun Hagino 
683686cdd19SJun-ichiro itojun Hagino #ifdef HAVE_OLD_BPF
684686cdd19SJun-ichiro itojun Hagino 		bpf_mtap(ifp, &m0);
685686cdd19SJun-ichiro itojun Hagino #else
686686cdd19SJun-ichiro itojun Hagino 		bpf_mtap(ifp->if_bpf, &m0);
687686cdd19SJun-ichiro itojun Hagino #endif
688686cdd19SJun-ichiro itojun Hagino 	}
689686cdd19SJun-ichiro itojun Hagino 
690686cdd19SJun-ichiro itojun Hagino 	/*
691686cdd19SJun-ichiro itojun Hagino 	 * Put the packet to the network layer input queue according to the
692686cdd19SJun-ichiro itojun Hagino 	 * specified address family.
693686cdd19SJun-ichiro itojun Hagino 	 * See net/if_gif.c for possible issues with packet processing
694686cdd19SJun-ichiro itojun Hagino 	 * reorder due to extra queueing.
695686cdd19SJun-ichiro itojun Hagino 	 */
696686cdd19SJun-ichiro itojun Hagino 	ifq = &ip6intrq;
697686cdd19SJun-ichiro itojun Hagino 	isr = NETISR_IPV6;
698686cdd19SJun-ichiro itojun Hagino 
699df5e1987SJonathan Lemon 	len = m->m_pkthdr.len;
700df5e1987SJonathan Lemon 	if (! IF_HANDOFF(ifq, m, NULL))
701686cdd19SJun-ichiro itojun Hagino 		return;
702686cdd19SJun-ichiro itojun Hagino 	schednetisr(isr);
703686cdd19SJun-ichiro itojun Hagino 	ifp->if_ipackets++;
704df5e1987SJonathan Lemon 	ifp->if_ibytes += len;
705686cdd19SJun-ichiro itojun Hagino }
706686cdd19SJun-ichiro itojun Hagino 
707686cdd19SJun-ichiro itojun Hagino /* ARGSUSED */
708686cdd19SJun-ichiro itojun Hagino static void
7098071913dSRuslan Ermilov stf_rtrequest(cmd, rt, info)
710686cdd19SJun-ichiro itojun Hagino 	int cmd;
711686cdd19SJun-ichiro itojun Hagino 	struct rtentry *rt;
7128071913dSRuslan Ermilov 	struct rt_addrinfo *info;
713686cdd19SJun-ichiro itojun Hagino {
714686cdd19SJun-ichiro itojun Hagino 
715686cdd19SJun-ichiro itojun Hagino 	if (rt)
716686cdd19SJun-ichiro itojun Hagino 		rt->rt_rmx.rmx_mtu = IPV6_MMTU;
717686cdd19SJun-ichiro itojun Hagino }
718686cdd19SJun-ichiro itojun Hagino 
719686cdd19SJun-ichiro itojun Hagino static int
720686cdd19SJun-ichiro itojun Hagino stf_ioctl(ifp, cmd, data)
721686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
722686cdd19SJun-ichiro itojun Hagino 	u_long cmd;
723686cdd19SJun-ichiro itojun Hagino 	caddr_t data;
724686cdd19SJun-ichiro itojun Hagino {
725686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ifa;
726686cdd19SJun-ichiro itojun Hagino 	struct ifreq *ifr;
727686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *sin6;
728686cdd19SJun-ichiro itojun Hagino 	int error;
729686cdd19SJun-ichiro itojun Hagino 
730686cdd19SJun-ichiro itojun Hagino 	error = 0;
731686cdd19SJun-ichiro itojun Hagino 	switch (cmd) {
732686cdd19SJun-ichiro itojun Hagino 	case SIOCSIFADDR:
733686cdd19SJun-ichiro itojun Hagino 		ifa = (struct ifaddr *)data;
734686cdd19SJun-ichiro itojun Hagino 		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
735686cdd19SJun-ichiro itojun Hagino 			error = EAFNOSUPPORT;
736686cdd19SJun-ichiro itojun Hagino 			break;
737686cdd19SJun-ichiro itojun Hagino 		}
738686cdd19SJun-ichiro itojun Hagino 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
739686cdd19SJun-ichiro itojun Hagino 		if (IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
740686cdd19SJun-ichiro itojun Hagino 			ifa->ifa_rtrequest = stf_rtrequest;
741686cdd19SJun-ichiro itojun Hagino 			ifp->if_flags |= IFF_UP;
742686cdd19SJun-ichiro itojun Hagino 		} else
743686cdd19SJun-ichiro itojun Hagino 			error = EINVAL;
744686cdd19SJun-ichiro itojun Hagino 		break;
745686cdd19SJun-ichiro itojun Hagino 
746686cdd19SJun-ichiro itojun Hagino 	case SIOCADDMULTI:
747686cdd19SJun-ichiro itojun Hagino 	case SIOCDELMULTI:
748686cdd19SJun-ichiro itojun Hagino 		ifr = (struct ifreq *)data;
749686cdd19SJun-ichiro itojun Hagino 		if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
750686cdd19SJun-ichiro itojun Hagino 			;
751686cdd19SJun-ichiro itojun Hagino 		else
752686cdd19SJun-ichiro itojun Hagino 			error = EAFNOSUPPORT;
753686cdd19SJun-ichiro itojun Hagino 		break;
754686cdd19SJun-ichiro itojun Hagino 
755686cdd19SJun-ichiro itojun Hagino 	default:
756686cdd19SJun-ichiro itojun Hagino 		error = EINVAL;
757686cdd19SJun-ichiro itojun Hagino 		break;
758686cdd19SJun-ichiro itojun Hagino 	}
759686cdd19SJun-ichiro itojun Hagino 
760686cdd19SJun-ichiro itojun Hagino 	return error;
761686cdd19SJun-ichiro itojun Hagino }
762