xref: /freebsd/sys/net/if_stf.c (revision bb4a7d94b99fbf7f59c876ffff8ded5f6a5b5c3e)
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 
4c398230bSWarner Losh /*-
551369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
651369649SPedro F. Giffuni  *
7686cdd19SJun-ichiro itojun Hagino  * Copyright (C) 2000 WIDE Project.
8686cdd19SJun-ichiro itojun Hagino  * All rights reserved.
9686cdd19SJun-ichiro itojun Hagino  *
10686cdd19SJun-ichiro itojun Hagino  * Redistribution and use in source and binary forms, with or without
11686cdd19SJun-ichiro itojun Hagino  * modification, are permitted provided that the following conditions
12686cdd19SJun-ichiro itojun Hagino  * are met:
13686cdd19SJun-ichiro itojun Hagino  * 1. Redistributions of source code must retain the above copyright
14686cdd19SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer.
15686cdd19SJun-ichiro itojun Hagino  * 2. Redistributions in binary form must reproduce the above copyright
16686cdd19SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer in the
17686cdd19SJun-ichiro itojun Hagino  *    documentation and/or other materials provided with the distribution.
18686cdd19SJun-ichiro itojun Hagino  * 3. Neither the name of the project nor the names of its contributors
19686cdd19SJun-ichiro itojun Hagino  *    may be used to endorse or promote products derived from this software
20686cdd19SJun-ichiro itojun Hagino  *    without specific prior written permission.
21686cdd19SJun-ichiro itojun Hagino  *
22686cdd19SJun-ichiro itojun Hagino  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23686cdd19SJun-ichiro itojun Hagino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24686cdd19SJun-ichiro itojun Hagino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25686cdd19SJun-ichiro itojun Hagino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26686cdd19SJun-ichiro itojun Hagino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27686cdd19SJun-ichiro itojun Hagino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28686cdd19SJun-ichiro itojun Hagino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29686cdd19SJun-ichiro itojun Hagino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30686cdd19SJun-ichiro itojun Hagino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31686cdd19SJun-ichiro itojun Hagino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32686cdd19SJun-ichiro itojun Hagino  * SUCH DAMAGE.
33686cdd19SJun-ichiro itojun Hagino  */
34686cdd19SJun-ichiro itojun Hagino 
35686cdd19SJun-ichiro itojun Hagino /*
3633841545SHajimu UMEMOTO  * 6to4 interface, based on RFC3056.
37686cdd19SJun-ichiro itojun Hagino  *
38686cdd19SJun-ichiro itojun Hagino  * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
39686cdd19SJun-ichiro itojun Hagino  * There is no address mapping defined from IPv6 multicast address to IPv4
40686cdd19SJun-ichiro itojun Hagino  * address.  Therefore, we do not have IFF_MULTICAST on the interface.
41686cdd19SJun-ichiro itojun Hagino  *
42686cdd19SJun-ichiro itojun Hagino  * Due to the lack of address mapping for link-local addresses, we cannot
43686cdd19SJun-ichiro itojun Hagino  * throw packets toward link-local addresses (fe80::x).  Also, we cannot throw
44686cdd19SJun-ichiro itojun Hagino  * packets to link-local multicast addresses (ff02::x).
45686cdd19SJun-ichiro itojun Hagino  *
46686cdd19SJun-ichiro itojun Hagino  * Here are interesting symptoms due to the lack of link-local address:
47686cdd19SJun-ichiro itojun Hagino  *
48686cdd19SJun-ichiro itojun Hagino  * Unicast routing exchange:
49686cdd19SJun-ichiro itojun Hagino  * - RIPng: Impossible.  Uses link-local multicast packet toward ff02::9,
50686cdd19SJun-ichiro itojun Hagino  *   and link-local addresses as nexthop.
51686cdd19SJun-ichiro itojun Hagino  * - OSPFv6: Impossible.  OSPFv6 assumes that there's link-local address
52686cdd19SJun-ichiro itojun Hagino  *   assigned to the link, and makes use of them.  Also, HELLO packets use
53686cdd19SJun-ichiro itojun Hagino  *   link-local multicast addresses (ff02::5 and ff02::6).
54686cdd19SJun-ichiro itojun Hagino  * - BGP4+: Maybe.  You can only use global address as nexthop, and global
55686cdd19SJun-ichiro itojun Hagino  *   address as TCP endpoint address.
56686cdd19SJun-ichiro itojun Hagino  *
57686cdd19SJun-ichiro itojun Hagino  * Multicast routing protocols:
58686cdd19SJun-ichiro itojun Hagino  * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
59686cdd19SJun-ichiro itojun Hagino  *   Adjacent PIM routers must be configured manually (is it really spec-wise
60686cdd19SJun-ichiro itojun Hagino  *   correct thing to do?).
61686cdd19SJun-ichiro itojun Hagino  *
62686cdd19SJun-ichiro itojun Hagino  * ICMPv6:
63686cdd19SJun-ichiro itojun Hagino  * - Redirects cannot be used due to the lack of link-local address.
64686cdd19SJun-ichiro itojun Hagino  *
6533841545SHajimu UMEMOTO  * stf interface does not have, and will not need, a link-local address.
6633841545SHajimu UMEMOTO  * It seems to have no real benefit and does not help the above symptoms much.
6733841545SHajimu UMEMOTO  * Even if we assign link-locals to interface, we cannot really
6833841545SHajimu UMEMOTO  * use link-local unicast/multicast on top of 6to4 cloud (since there's no
6933841545SHajimu UMEMOTO  * encapsulation defined for link-local address), and the above analysis does
7033841545SHajimu UMEMOTO  * not change.  RFC3056 does not mandate the assignment of link-local address
7133841545SHajimu UMEMOTO  * either.
72686cdd19SJun-ichiro itojun Hagino  *
73686cdd19SJun-ichiro itojun Hagino  * 6to4 interface has security issues.  Refer to
74686cdd19SJun-ichiro itojun Hagino  * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
75686cdd19SJun-ichiro itojun Hagino  * for details.  The code tries to filter out some of malicious packets.
76686cdd19SJun-ichiro itojun Hagino  * Note that there is no way to be 100% secure.
77686cdd19SJun-ichiro itojun Hagino  */
78686cdd19SJun-ichiro itojun Hagino 
79686cdd19SJun-ichiro itojun Hagino #include <sys/param.h>
80686cdd19SJun-ichiro itojun Hagino #include <sys/systm.h>
81686cdd19SJun-ichiro itojun Hagino #include <sys/socket.h>
82686cdd19SJun-ichiro itojun Hagino #include <sys/sockio.h>
83686cdd19SJun-ichiro itojun Hagino #include <sys/mbuf.h>
84686cdd19SJun-ichiro itojun Hagino #include <sys/errno.h>
85686cdd19SJun-ichiro itojun Hagino #include <sys/kernel.h>
86cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
875dba30f1SPoul-Henning Kamp #include <sys/module.h>
888b07e49aSJulian Elischer #include <sys/proc.h>
89abb64706SBrooks Davis #include <sys/queue.h>
90cc0a3c8cSAndrey V. Elsukov #include <sys/rmlock.h>
910dae32f2SDavid Malone #include <sys/sysctl.h>
92686cdd19SJun-ichiro itojun Hagino #include <machine/cpu.h>
93686cdd19SJun-ichiro itojun Hagino 
94686cdd19SJun-ichiro itojun Hagino #include <sys/malloc.h>
95686cdd19SJun-ichiro itojun Hagino 
96686cdd19SJun-ichiro itojun Hagino #include <net/if.h>
9776039bc8SGleb Smirnoff #include <net/if_var.h>
98f889d2efSBrooks Davis #include <net/if_clone.h>
99686cdd19SJun-ichiro itojun Hagino #include <net/route.h>
1006ad7446cSAlexander V. Chernikov #include <net/route/nhop.h>
101686cdd19SJun-ichiro itojun Hagino #include <net/netisr.h>
102686cdd19SJun-ichiro itojun Hagino #include <net/if_types.h>
103530c0060SRobert Watson #include <net/vnet.h>
104686cdd19SJun-ichiro itojun Hagino 
105686cdd19SJun-ichiro itojun Hagino #include <netinet/in.h>
1060792bcbbSAlexander V. Chernikov #include <netinet/in_fib.h>
107686cdd19SJun-ichiro itojun Hagino #include <netinet/in_systm.h>
108686cdd19SJun-ichiro itojun Hagino #include <netinet/ip.h>
109686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_var.h>
110686cdd19SJun-ichiro itojun Hagino #include <netinet/in_var.h>
111686cdd19SJun-ichiro itojun Hagino 
112686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
113686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6_var.h>
114686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_var.h>
115686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_ecn.h>
116686cdd19SJun-ichiro itojun Hagino 
117686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
118686cdd19SJun-ichiro itojun Hagino 
119686cdd19SJun-ichiro itojun Hagino #include <machine/stdarg.h>
120686cdd19SJun-ichiro itojun Hagino 
121686cdd19SJun-ichiro itojun Hagino #include <net/bpf.h>
122686cdd19SJun-ichiro itojun Hagino 
123aed55708SRobert Watson #include <security/mac/mac_framework.h>
124aed55708SRobert Watson 
1250dae32f2SDavid Malone SYSCTL_DECL(_net_link);
1267029da5cSPawel Biernacki static SYSCTL_NODE(_net_link, IFT_STF, stf, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1277029da5cSPawel Biernacki     "6to4 Interface");
1280dae32f2SDavid Malone 
12951743c5fSAndrey V. Elsukov static int stf_permit_rfc1918 = 0;
130af3b2549SHans Petter Selasky SYSCTL_INT(_net_link_stf, OID_AUTO, permit_rfc1918, CTLFLAG_RWTUN,
13151743c5fSAndrey V. Elsukov     &stf_permit_rfc1918, 0, "Permit the use of private IPv4 addresses");
13251743c5fSAndrey V. Elsukov 
133f889d2efSBrooks Davis #define STFUNIT		0
134abb64706SBrooks Davis 
135686cdd19SJun-ichiro itojun Hagino #define IN6_IS_ADDR_6TO4(x)	(ntohs((x)->s6_addr16[0]) == 0x2002)
1368d95b0ceSSUZUKI Shinsuke 
1378d95b0ceSSUZUKI Shinsuke /*
1388d95b0ceSSUZUKI Shinsuke  * XXX: Return a pointer with 16-bit aligned.  Don't cast it to
1398d95b0ceSSUZUKI Shinsuke  * struct in_addr *; use bcopy() instead.
1408d95b0ceSSUZUKI Shinsuke  */
14147e8d432SGleb Smirnoff #define GET_V4(x)	(&(x)->s6_addr16[1])
142686cdd19SJun-ichiro itojun Hagino 
143686cdd19SJun-ichiro itojun Hagino struct stf_softc {
144fc74a9f9SBrooks Davis 	struct ifnet	*sc_ifp;
1458b07e49aSJulian Elischer 	u_int	sc_fibnum;
146686cdd19SJun-ichiro itojun Hagino 	const struct encaptab *encap_cookie;
147686cdd19SJun-ichiro itojun Hagino };
148fc74a9f9SBrooks Davis #define STF2IFP(sc)	((sc)->sc_ifp)
149686cdd19SJun-ichiro itojun Hagino 
15042a58907SGleb Smirnoff static const char stfname[] = "stf";
15142a58907SGleb Smirnoff 
15242a58907SGleb Smirnoff static MALLOC_DEFINE(M_STF, stfname, "6to4 Tunnel Interface");
153591cf7ceSRobert Watson static const int ip_stf_ttl = 40;
154686cdd19SJun-ichiro itojun Hagino 
1556d8fdfa9SAndrey V. Elsukov static int in_stf_input(struct mbuf *, int, int, void *);
156f889d2efSBrooks Davis static char *stfnames[] = {"stf0", "stf", "6to4", NULL};
157f889d2efSBrooks Davis 
158929ddbbbSAlfred Perlstein static int stfmodevent(module_t, int, void *);
159929ddbbbSAlfred Perlstein static int stf_encapcheck(const struct mbuf *, int, int, void *);
160d74b9a2cSAlexander V. Chernikov static int stf_getsrcifa6(struct ifnet *, struct in6_addr *, struct in6_addr *);
16147e8d432SGleb Smirnoff static int stf_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
162279aa3d4SKip Macy 	struct route *);
163ce9d7b2fSHajimu UMEMOTO static int isrfc1918addr(struct in_addr *);
164929ddbbbSAlfred Perlstein static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
165929ddbbbSAlfred Perlstein 	struct ifnet *);
166929ddbbbSAlfred Perlstein static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
167929ddbbbSAlfred Perlstein 	struct ifnet *);
168929ddbbbSAlfred Perlstein static int stf_ioctl(struct ifnet *, u_long, caddr_t);
169686cdd19SJun-ichiro itojun Hagino 
170f889d2efSBrooks Davis static int stf_clone_match(struct if_clone *, const char *);
1716b7330e2SSam Leffler static int stf_clone_create(struct if_clone *, char *, size_t, caddr_t);
172f889d2efSBrooks Davis static int stf_clone_destroy(struct if_clone *, struct ifnet *);
17342a58907SGleb Smirnoff static struct if_clone *stf_cloner;
174abb64706SBrooks Davis 
1756d8fdfa9SAndrey V. Elsukov static const struct encap_config ipv4_encap_cfg = {
1766d8fdfa9SAndrey V. Elsukov 	.proto = IPPROTO_IPV6,
1776d8fdfa9SAndrey V. Elsukov 	.min_length = sizeof(struct ip),
1786d8fdfa9SAndrey V. Elsukov 	.exact_match = (sizeof(in_addr_t) << 3) + 8,
1796d8fdfa9SAndrey V. Elsukov 	.check = stf_encapcheck,
1806d8fdfa9SAndrey V. Elsukov 	.input = in_stf_input
1816d8fdfa9SAndrey V. Elsukov };
1826d8fdfa9SAndrey V. Elsukov 
183bb2bfb4fSBrooks Davis static int
184f889d2efSBrooks Davis stf_clone_match(struct if_clone *ifc, const char *name)
185686cdd19SJun-ichiro itojun Hagino {
186f889d2efSBrooks Davis 	int i;
187f889d2efSBrooks Davis 
188f889d2efSBrooks Davis 	for(i = 0; stfnames[i] != NULL; i++) {
189f889d2efSBrooks Davis 		if (strcmp(stfnames[i], name) == 0)
190f889d2efSBrooks Davis 			return (1);
191f889d2efSBrooks Davis 	}
192f889d2efSBrooks Davis 
193f889d2efSBrooks Davis 	return (0);
194f889d2efSBrooks Davis }
195f889d2efSBrooks Davis 
196f889d2efSBrooks Davis static int
1976b7330e2SSam Leffler stf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
198f889d2efSBrooks Davis {
199d177868cSLuiz Otavio O Souza 	char *dp;
200d177868cSLuiz Otavio O Souza 	int err, unit, wildcard;
201686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
2021861b710SBrooks Davis 	struct ifnet *ifp;
203686cdd19SJun-ichiro itojun Hagino 
204d177868cSLuiz Otavio O Souza 	err = ifc_name2unit(name, &unit);
205d177868cSLuiz Otavio O Souza 	if (err != 0)
206d177868cSLuiz Otavio O Souza 		return (err);
207d177868cSLuiz Otavio O Souza 	wildcard = (unit < 0);
208d177868cSLuiz Otavio O Souza 
209f889d2efSBrooks Davis 	/*
210f889d2efSBrooks Davis 	 * We can only have one unit, but since unit allocation is
211f889d2efSBrooks Davis 	 * already locked, we use it to keep from allocating extra
212f889d2efSBrooks Davis 	 * interfaces.
213f889d2efSBrooks Davis 	 */
214f889d2efSBrooks Davis 	unit = STFUNIT;
215f889d2efSBrooks Davis 	err = ifc_alloc_unit(ifc, &unit);
216f889d2efSBrooks Davis 	if (err != 0)
217f889d2efSBrooks Davis 		return (err);
218f889d2efSBrooks Davis 
219a163d034SWarner Losh 	sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
220b03965ddSBrooks Davis 	ifp = STF2IFP(sc) = if_alloc(IFT_STF);
221fc74a9f9SBrooks Davis 	if (ifp == NULL) {
222fc74a9f9SBrooks Davis 		free(sc, M_STF);
223fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
224fc74a9f9SBrooks Davis 		return (ENOSPC);
225fc74a9f9SBrooks Davis 	}
226b03965ddSBrooks Davis 	ifp->if_softc = sc;
2278b07e49aSJulian Elischer 	sc->sc_fibnum = curthread->td_proc->p_fibnum;
228b03965ddSBrooks Davis 
229f889d2efSBrooks Davis 	/*
230f889d2efSBrooks Davis 	 * Set the name manually rather then using if_initname because
231f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
232d177868cSLuiz Otavio O Souza 	 * In the wildcard case, we need to update the name.
233f889d2efSBrooks Davis 	 */
234d177868cSLuiz Otavio O Souza 	if (wildcard) {
235d177868cSLuiz Otavio O Souza 		for (dp = name; *dp != '\0'; dp++);
236d177868cSLuiz Otavio O Souza 		if (snprintf(dp, len - (dp-name), "%d", unit) >
237d177868cSLuiz Otavio O Souza 		    len - (dp-name) - 1) {
238d177868cSLuiz Otavio O Souza 			/*
239d177868cSLuiz Otavio O Souza 			 * This can only be a programmer error and
240d177868cSLuiz Otavio O Souza 			 * there's no straightforward way to recover if
241d177868cSLuiz Otavio O Souza 			 * it happens.
242d177868cSLuiz Otavio O Souza 			 */
243d177868cSLuiz Otavio O Souza 			panic("if_clone_create(): interface name too long");
244d177868cSLuiz Otavio O Souza 		}
245d177868cSLuiz Otavio O Souza 	}
246f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
24742a58907SGleb Smirnoff 	ifp->if_dname = stfname;
248f889d2efSBrooks Davis 	ifp->if_dunit = IF_DUNIT_NONE;
249abb64706SBrooks Davis 
2506d8fdfa9SAndrey V. Elsukov 	sc->encap_cookie = ip_encap_attach(&ipv4_encap_cfg, sc, M_WAITOK);
251abb64706SBrooks Davis 	if (sc->encap_cookie == NULL) {
2521861b710SBrooks Davis 		if_printf(ifp, "attach failed\n");
253abb64706SBrooks Davis 		free(sc, M_STF);
254f889d2efSBrooks Davis 		ifc_free_unit(ifc, unit);
25553dab5feSBrooks Davis 		return (ENOMEM);
256686cdd19SJun-ichiro itojun Hagino 	}
257686cdd19SJun-ichiro itojun Hagino 
2581861b710SBrooks Davis 	ifp->if_mtu    = IPV6_MMTU;
2591861b710SBrooks Davis 	ifp->if_ioctl  = stf_ioctl;
2601861b710SBrooks Davis 	ifp->if_output = stf_output;
261e50d35e6SMaxim Sobolev 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
2621861b710SBrooks Davis 	if_attach(ifp);
26301399f34SDavid Malone 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
264abb64706SBrooks Davis 	return (0);
265abb64706SBrooks Davis }
266abb64706SBrooks Davis 
267f889d2efSBrooks Davis static int
268f889d2efSBrooks Davis stf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
269abb64706SBrooks Davis {
270fc74a9f9SBrooks Davis 	struct stf_softc *sc = ifp->if_softc;
27146d0f824SMatt Macy 	int err __unused;
272abb64706SBrooks Davis 
2736d8fdfa9SAndrey V. Elsukov 	err = ip_encap_detach(sc->encap_cookie);
274febd0759SAndrew Thompson 	KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
275febd0759SAndrew Thompson 	bpfdetach(ifp);
276febd0759SAndrew Thompson 	if_detach(ifp);
277febd0759SAndrew Thompson 	if_free(ifp);
278febd0759SAndrew Thompson 
279febd0759SAndrew Thompson 	free(sc, M_STF);
280f889d2efSBrooks Davis 	ifc_free_unit(ifc, STFUNIT);
281f889d2efSBrooks Davis 
282f889d2efSBrooks Davis 	return (0);
283abb64706SBrooks Davis }
284abb64706SBrooks Davis 
285abb64706SBrooks Davis static int
286926381e1SAndrey V. Elsukov stfmodevent(module_t mod, int type, void *data)
287abb64706SBrooks Davis {
288abb64706SBrooks Davis 
289abb64706SBrooks Davis 	switch (type) {
290abb64706SBrooks Davis 	case MOD_LOAD:
29142a58907SGleb Smirnoff 		stf_cloner = if_clone_advanced(stfname, 0, stf_clone_match,
29242a58907SGleb Smirnoff 		    stf_clone_create, stf_clone_destroy);
293abb64706SBrooks Davis 		break;
294abb64706SBrooks Davis 	case MOD_UNLOAD:
29542a58907SGleb Smirnoff 		if_clone_detach(stf_cloner);
29653dab5feSBrooks Davis 		break;
2973e019deaSPoul-Henning Kamp 	default:
2983e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
299686cdd19SJun-ichiro itojun Hagino 	}
300686cdd19SJun-ichiro itojun Hagino 
30153dab5feSBrooks Davis 	return (0);
30253dab5feSBrooks Davis }
30353dab5feSBrooks Davis 
30453dab5feSBrooks Davis static moduledata_t stf_mod = {
30553dab5feSBrooks Davis 	"if_stf",
30653dab5feSBrooks Davis 	stfmodevent,
3079823d527SKevin Lo 	0
30853dab5feSBrooks Davis };
30953dab5feSBrooks Davis 
31053dab5feSBrooks Davis DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
311686cdd19SJun-ichiro itojun Hagino 
312686cdd19SJun-ichiro itojun Hagino static int
313926381e1SAndrey V. Elsukov stf_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
314686cdd19SJun-ichiro itojun Hagino {
315686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
316686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
3172b0e5976SBill Fenner 	struct in_addr a, b, mask;
318d74b9a2cSAlexander V. Chernikov 	struct in6_addr addr6, mask6;
319686cdd19SJun-ichiro itojun Hagino 
320686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc *)arg;
321686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
322686cdd19SJun-ichiro itojun Hagino 		return 0;
323686cdd19SJun-ichiro itojun Hagino 
324fc74a9f9SBrooks Davis 	if ((STF2IFP(sc)->if_flags & IFF_UP) == 0)
325686cdd19SJun-ichiro itojun Hagino 		return 0;
326686cdd19SJun-ichiro itojun Hagino 
32733841545SHajimu UMEMOTO 	/* IFF_LINK0 means "no decapsulation" */
328fc74a9f9SBrooks Davis 	if ((STF2IFP(sc)->if_flags & IFF_LINK0) != 0)
32933841545SHajimu UMEMOTO 		return 0;
33033841545SHajimu UMEMOTO 
331686cdd19SJun-ichiro itojun Hagino 	if (proto != IPPROTO_IPV6)
332686cdd19SJun-ichiro itojun Hagino 		return 0;
333686cdd19SJun-ichiro itojun Hagino 
334350e6227SAndrey V. Elsukov 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
335686cdd19SJun-ichiro itojun Hagino 
336686cdd19SJun-ichiro itojun Hagino 	if (ip.ip_v != 4)
337686cdd19SJun-ichiro itojun Hagino 		return 0;
338686cdd19SJun-ichiro itojun Hagino 
339d74b9a2cSAlexander V. Chernikov 	if (stf_getsrcifa6(STF2IFP(sc), &addr6, &mask6) != 0)
340d74b9a2cSAlexander V. Chernikov 		return (0);
341686cdd19SJun-ichiro itojun Hagino 
342686cdd19SJun-ichiro itojun Hagino 	/*
343686cdd19SJun-ichiro itojun Hagino 	 * check if IPv4 dst matches the IPv4 address derived from the
344686cdd19SJun-ichiro itojun Hagino 	 * local 6to4 address.
345686cdd19SJun-ichiro itojun Hagino 	 * success on: dst = 10.1.1.1, ia6->ia_addr = 2002:0a01:0101:...
346686cdd19SJun-ichiro itojun Hagino 	 */
347d74b9a2cSAlexander V. Chernikov 	if (bcmp(GET_V4(&addr6), &ip.ip_dst, sizeof(ip.ip_dst)) != 0)
348686cdd19SJun-ichiro itojun Hagino 		return 0;
349686cdd19SJun-ichiro itojun Hagino 
350686cdd19SJun-ichiro itojun Hagino 	/*
351686cdd19SJun-ichiro itojun Hagino 	 * check if IPv4 src matches the IPv4 address derived from the
352686cdd19SJun-ichiro itojun Hagino 	 * local 6to4 address masked by prefixmask.
353686cdd19SJun-ichiro itojun Hagino 	 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
354686cdd19SJun-ichiro itojun Hagino 	 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
355686cdd19SJun-ichiro itojun Hagino 	 */
356686cdd19SJun-ichiro itojun Hagino 	bzero(&a, sizeof(a));
357d74b9a2cSAlexander V. Chernikov 	bcopy(GET_V4(&addr6), &a, sizeof(a));
358d74b9a2cSAlexander V. Chernikov 	bcopy(GET_V4(&mask6), &mask, sizeof(mask));
3592b0e5976SBill Fenner 	a.s_addr &= mask.s_addr;
360686cdd19SJun-ichiro itojun Hagino 	b = ip.ip_src;
3612b0e5976SBill Fenner 	b.s_addr &= mask.s_addr;
362686cdd19SJun-ichiro itojun Hagino 	if (a.s_addr != b.s_addr)
363686cdd19SJun-ichiro itojun Hagino 		return 0;
364686cdd19SJun-ichiro itojun Hagino 
365686cdd19SJun-ichiro itojun Hagino 	/* stf interface makes single side match only */
366686cdd19SJun-ichiro itojun Hagino 	return 32;
367686cdd19SJun-ichiro itojun Hagino }
368686cdd19SJun-ichiro itojun Hagino 
369d74b9a2cSAlexander V. Chernikov static int
370d74b9a2cSAlexander V. Chernikov stf_getsrcifa6(struct ifnet *ifp, struct in6_addr *addr, struct in6_addr *mask)
371686cdd19SJun-ichiro itojun Hagino {
372232485a1SEugene Grosbein 	struct rm_priotracker in_ifa_tracker;
373686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ia;
374686cdd19SJun-ichiro itojun Hagino 	struct in_ifaddr *ia4;
375d74b9a2cSAlexander V. Chernikov 	struct in6_ifaddr *ia6;
376686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *sin6;
377686cdd19SJun-ichiro itojun Hagino 	struct in_addr in;
378686cdd19SJun-ichiro itojun Hagino 
3794b24e5b1SGleb Smirnoff 	NET_EPOCH_ASSERT();
3804b24e5b1SGleb Smirnoff 
381d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
382686cdd19SJun-ichiro itojun Hagino 		if (ia->ifa_addr->sa_family != AF_INET6)
383686cdd19SJun-ichiro itojun Hagino 			continue;
384686cdd19SJun-ichiro itojun Hagino 		sin6 = (struct sockaddr_in6 *)ia->ifa_addr;
385686cdd19SJun-ichiro itojun Hagino 		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr))
386686cdd19SJun-ichiro itojun Hagino 			continue;
387686cdd19SJun-ichiro itojun Hagino 
388686cdd19SJun-ichiro itojun Hagino 		bcopy(GET_V4(&sin6->sin6_addr), &in, sizeof(in));
389232485a1SEugene Grosbein 		IN_IFADDR_RLOCK(&in_ifa_tracker);
390cf912c89SJonathan Lemon 		LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
391686cdd19SJun-ichiro itojun Hagino 			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
392686cdd19SJun-ichiro itojun Hagino 				break;
393232485a1SEugene Grosbein 		IN_IFADDR_RUNLOCK(&in_ifa_tracker);
394686cdd19SJun-ichiro itojun Hagino 		if (ia4 == NULL)
395686cdd19SJun-ichiro itojun Hagino 			continue;
396686cdd19SJun-ichiro itojun Hagino 
397d74b9a2cSAlexander V. Chernikov 		ia6 = (struct in6_ifaddr *)ia;
398d74b9a2cSAlexander V. Chernikov 
399d74b9a2cSAlexander V. Chernikov 		*addr = sin6->sin6_addr;
400d74b9a2cSAlexander V. Chernikov 		*mask = ia6->ia_prefixmask.sin6_addr;
401d74b9a2cSAlexander V. Chernikov 		return (0);
402686cdd19SJun-ichiro itojun Hagino 	}
403686cdd19SJun-ichiro itojun Hagino 
404d74b9a2cSAlexander V. Chernikov 	return (ENOENT);
405686cdd19SJun-ichiro itojun Hagino }
406686cdd19SJun-ichiro itojun Hagino 
407686cdd19SJun-ichiro itojun Hagino static int
40847e8d432SGleb Smirnoff stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
40947e8d432SGleb Smirnoff     struct route *ro)
410686cdd19SJun-ichiro itojun Hagino {
411686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
41247e8d432SGleb Smirnoff 	const struct sockaddr_in6 *dst6;
4138d95b0ceSSUZUKI Shinsuke 	struct in_addr in4;
41447e8d432SGleb Smirnoff 	const void *ptr;
415686cdd19SJun-ichiro itojun Hagino 	u_int8_t tos;
416686cdd19SJun-ichiro itojun Hagino 	struct ip *ip;
417686cdd19SJun-ichiro itojun Hagino 	struct ip6_hdr *ip6;
418d74b9a2cSAlexander V. Chernikov 	struct in6_addr addr6, mask6;
4196b459e49SRobert Watson 	int error;
4206b459e49SRobert Watson 
4210dae32f2SDavid Malone #ifdef MAC
42230d239bcSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
4236b459e49SRobert Watson 	if (error) {
4246b459e49SRobert Watson 		m_freem(m);
4256b459e49SRobert Watson 		return (error);
4266b459e49SRobert Watson 	}
4276b459e49SRobert Watson #endif
428686cdd19SJun-ichiro itojun Hagino 
429fc74a9f9SBrooks Davis 	sc = ifp->if_softc;
43047e8d432SGleb Smirnoff 	dst6 = (const struct sockaddr_in6 *)dst;
431686cdd19SJun-ichiro itojun Hagino 
432686cdd19SJun-ichiro itojun Hagino 	/* just in case */
433686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_UP) == 0) {
434686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
4353751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
436686cdd19SJun-ichiro itojun Hagino 		return ENETDOWN;
437686cdd19SJun-ichiro itojun Hagino 	}
438686cdd19SJun-ichiro itojun Hagino 
439686cdd19SJun-ichiro itojun Hagino 	/*
440686cdd19SJun-ichiro itojun Hagino 	 * If we don't have an ip4 address that match my inner ip6 address,
441686cdd19SJun-ichiro itojun Hagino 	 * we shouldn't generate output.  Without this check, we'll end up
442686cdd19SJun-ichiro itojun Hagino 	 * using wrong IPv4 source.
443686cdd19SJun-ichiro itojun Hagino 	 */
444d74b9a2cSAlexander V. Chernikov 	if (stf_getsrcifa6(ifp, &addr6, &mask6) != 0) {
445686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
4463751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
447686cdd19SJun-ichiro itojun Hagino 		return ENETDOWN;
448686cdd19SJun-ichiro itojun Hagino 	}
449686cdd19SJun-ichiro itojun Hagino 
450686cdd19SJun-ichiro itojun Hagino 	if (m->m_len < sizeof(*ip6)) {
451686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(*ip6));
452f26b2d5bSHajimu UMEMOTO 		if (!m) {
4533751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
454686cdd19SJun-ichiro itojun Hagino 			return ENOBUFS;
455686cdd19SJun-ichiro itojun Hagino 		}
456f26b2d5bSHajimu UMEMOTO 	}
457686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
458*bb4a7d94SKristof Provost 	tos = IPV6_TRAFFIC_CLASS(ip6);
459686cdd19SJun-ichiro itojun Hagino 
46033841545SHajimu UMEMOTO 	/*
46133841545SHajimu UMEMOTO 	 * Pickup the right outer dst addr from the list of candidates.
46233841545SHajimu UMEMOTO 	 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
46333841545SHajimu UMEMOTO 	 */
4648d95b0ceSSUZUKI Shinsuke 	ptr = NULL;
46533841545SHajimu UMEMOTO 	if (IN6_IS_ADDR_6TO4(&ip6->ip6_dst))
4668d95b0ceSSUZUKI Shinsuke 		ptr = GET_V4(&ip6->ip6_dst);
46733841545SHajimu UMEMOTO 	else if (IN6_IS_ADDR_6TO4(&dst6->sin6_addr))
4688d95b0ceSSUZUKI Shinsuke 		ptr = GET_V4(&dst6->sin6_addr);
46933841545SHajimu UMEMOTO 	else {
47033841545SHajimu UMEMOTO 		m_freem(m);
4713751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
47233841545SHajimu UMEMOTO 		return ENETUNREACH;
47333841545SHajimu UMEMOTO 	}
4748d95b0ceSSUZUKI Shinsuke 	bcopy(ptr, &in4, sizeof(in4));
47533841545SHajimu UMEMOTO 
47616d878ccSChristian S.J. Peron 	if (bpf_peers_present(ifp->if_bpf)) {
4778acb2290SHajimu UMEMOTO 		/*
4788acb2290SHajimu UMEMOTO 		 * We need to prepend the address family as
4798acb2290SHajimu UMEMOTO 		 * a four byte field.  Cons up a dummy header
4808acb2290SHajimu UMEMOTO 		 * to pacify bpf.  This is safe because bpf
4818acb2290SHajimu UMEMOTO 		 * will only read from the mbuf (i.e., it won't
4828acb2290SHajimu UMEMOTO 		 * try to free it or keep a pointer a to it).
4838acb2290SHajimu UMEMOTO 		 */
48447e8d432SGleb Smirnoff 		u_int af = AF_INET6;
485437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
4868acb2290SHajimu UMEMOTO 	}
4878acb2290SHajimu UMEMOTO 
488eb1b1807SGleb Smirnoff 	M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
489f26b2d5bSHajimu UMEMOTO 	if (m == NULL) {
4903751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
491686cdd19SJun-ichiro itojun Hagino 		return ENOBUFS;
492f26b2d5bSHajimu UMEMOTO 	}
493686cdd19SJun-ichiro itojun Hagino 	ip = mtod(m, struct ip *);
494686cdd19SJun-ichiro itojun Hagino 
495686cdd19SJun-ichiro itojun Hagino 	bzero(ip, sizeof(*ip));
496686cdd19SJun-ichiro itojun Hagino 
497d74b9a2cSAlexander V. Chernikov 	bcopy(GET_V4(&addr6), &ip->ip_src, sizeof(ip->ip_src));
4988d95b0ceSSUZUKI Shinsuke 	bcopy(&in4, &ip->ip_dst, sizeof(ip->ip_dst));
499686cdd19SJun-ichiro itojun Hagino 	ip->ip_p = IPPROTO_IPV6;
50053dab5feSBrooks Davis 	ip->ip_ttl = ip_stf_ttl;
5018f134647SGleb Smirnoff 	ip->ip_len = htons(m->m_pkthdr.len);
502686cdd19SJun-ichiro itojun Hagino 	if (ifp->if_flags & IFF_LINK1)
503686cdd19SJun-ichiro itojun Hagino 		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
50433841545SHajimu UMEMOTO 	else
50533841545SHajimu UMEMOTO 		ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
506686cdd19SJun-ichiro itojun Hagino 
5078b07e49aSJulian Elischer 	M_SETFIB(m, sc->sc_fibnum);
5083751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
509d74b9a2cSAlexander V. Chernikov 	error = ip_output(m, NULL, NULL, 0, NULL, NULL);
5100dae32f2SDavid Malone 
5110dae32f2SDavid Malone 	return error;
512686cdd19SJun-ichiro itojun Hagino }
513686cdd19SJun-ichiro itojun Hagino 
514686cdd19SJun-ichiro itojun Hagino static int
515926381e1SAndrey V. Elsukov isrfc1918addr(struct in_addr *in)
516ce9d7b2fSHajimu UMEMOTO {
517ce9d7b2fSHajimu UMEMOTO 	/*
518ce9d7b2fSHajimu UMEMOTO 	 * returns 1 if private address range:
519ce9d7b2fSHajimu UMEMOTO 	 * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
520ce9d7b2fSHajimu UMEMOTO 	 */
52151743c5fSAndrey V. Elsukov 	if (stf_permit_rfc1918 == 0 && (
52251743c5fSAndrey V. Elsukov 	    (ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
5238d95b0ceSSUZUKI Shinsuke 	    (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
52451743c5fSAndrey V. Elsukov 	    (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168))
525ce9d7b2fSHajimu UMEMOTO 		return 1;
526ce9d7b2fSHajimu UMEMOTO 
527ce9d7b2fSHajimu UMEMOTO 	return 0;
528ce9d7b2fSHajimu UMEMOTO }
529ce9d7b2fSHajimu UMEMOTO 
530ce9d7b2fSHajimu UMEMOTO static int
531926381e1SAndrey V. Elsukov stf_checkaddr4(struct stf_softc *sc, struct in_addr *in, struct ifnet *inifp)
532686cdd19SJun-ichiro itojun Hagino {
533cc0a3c8cSAndrey V. Elsukov 	struct rm_priotracker in_ifa_tracker;
534686cdd19SJun-ichiro itojun Hagino 	struct in_ifaddr *ia4;
535686cdd19SJun-ichiro itojun Hagino 
536686cdd19SJun-ichiro itojun Hagino 	/*
537686cdd19SJun-ichiro itojun Hagino 	 * reject packets with the following address:
538686cdd19SJun-ichiro itojun Hagino 	 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
539686cdd19SJun-ichiro itojun Hagino 	 */
5408d95b0ceSSUZUKI Shinsuke 	if (IN_MULTICAST(ntohl(in->s_addr)))
541686cdd19SJun-ichiro itojun Hagino 		return -1;
5428d95b0ceSSUZUKI Shinsuke 	switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
5438d95b0ceSSUZUKI Shinsuke 	case 0: case 127: case 255:
544686cdd19SJun-ichiro itojun Hagino 		return -1;
5458d95b0ceSSUZUKI Shinsuke 	}
546686cdd19SJun-ichiro itojun Hagino 
547686cdd19SJun-ichiro itojun Hagino 	/*
548ce9d7b2fSHajimu UMEMOTO 	 * reject packets with private address range.
549ce9d7b2fSHajimu UMEMOTO 	 * (requirement from RFC3056 section 2 1st paragraph)
550ce9d7b2fSHajimu UMEMOTO 	 */
551ce9d7b2fSHajimu UMEMOTO 	if (isrfc1918addr(in))
552ce9d7b2fSHajimu UMEMOTO 		return -1;
553ce9d7b2fSHajimu UMEMOTO 
554ce9d7b2fSHajimu UMEMOTO 	/*
555686cdd19SJun-ichiro itojun Hagino 	 * reject packets with broadcast
556686cdd19SJun-ichiro itojun Hagino 	 */
557cc0a3c8cSAndrey V. Elsukov 	IN_IFADDR_RLOCK(&in_ifa_tracker);
558d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
559686cdd19SJun-ichiro itojun Hagino 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
560686cdd19SJun-ichiro itojun Hagino 			continue;
5612d9cfabaSRobert Watson 		if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
562cc0a3c8cSAndrey V. Elsukov 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
563686cdd19SJun-ichiro itojun Hagino 			return -1;
564686cdd19SJun-ichiro itojun Hagino 		}
5652d9cfabaSRobert Watson 	}
566cc0a3c8cSAndrey V. Elsukov 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
567686cdd19SJun-ichiro itojun Hagino 
568686cdd19SJun-ichiro itojun Hagino 	/*
569686cdd19SJun-ichiro itojun Hagino 	 * perform ingress filter
570686cdd19SJun-ichiro itojun Hagino 	 */
571fc74a9f9SBrooks Davis 	if (sc && (STF2IFP(sc)->if_flags & IFF_LINK2) == 0 && inifp) {
5726ad7446cSAlexander V. Chernikov 		struct nhop_object *nh;
573686cdd19SJun-ichiro itojun Hagino 
5746ad7446cSAlexander V. Chernikov 		NET_EPOCH_ASSERT();
5756ad7446cSAlexander V. Chernikov 		nh = fib4_lookup(sc->sc_fibnum, *in, 0, 0, 0);
5766ad7446cSAlexander V. Chernikov 		if (nh == NULL)
5770792bcbbSAlexander V. Chernikov 			return (-1);
5780792bcbbSAlexander V. Chernikov 
5796ad7446cSAlexander V. Chernikov 		if (nh->nh_ifp != inifp)
5800792bcbbSAlexander V. Chernikov 			return (-1);
581686cdd19SJun-ichiro itojun Hagino 	}
582686cdd19SJun-ichiro itojun Hagino 
583686cdd19SJun-ichiro itojun Hagino 	return 0;
584686cdd19SJun-ichiro itojun Hagino }
585686cdd19SJun-ichiro itojun Hagino 
586686cdd19SJun-ichiro itojun Hagino static int
587926381e1SAndrey V. Elsukov stf_checkaddr6(struct stf_softc *sc, struct in6_addr *in6, struct ifnet *inifp)
588686cdd19SJun-ichiro itojun Hagino {
589686cdd19SJun-ichiro itojun Hagino 	/*
590686cdd19SJun-ichiro itojun Hagino 	 * check 6to4 addresses
591686cdd19SJun-ichiro itojun Hagino 	 */
5928d95b0ceSSUZUKI Shinsuke 	if (IN6_IS_ADDR_6TO4(in6)) {
5938d95b0ceSSUZUKI Shinsuke 		struct in_addr in4;
5948d95b0ceSSUZUKI Shinsuke 		bcopy(GET_V4(in6), &in4, sizeof(in4));
5958d95b0ceSSUZUKI Shinsuke 		return stf_checkaddr4(sc, &in4, inifp);
5968d95b0ceSSUZUKI Shinsuke 	}
597686cdd19SJun-ichiro itojun Hagino 
598686cdd19SJun-ichiro itojun Hagino 	/*
599686cdd19SJun-ichiro itojun Hagino 	 * reject anything that look suspicious.  the test is implemented
600686cdd19SJun-ichiro itojun Hagino 	 * in ip6_input too, but we check here as well to
601686cdd19SJun-ichiro itojun Hagino 	 * (1) reject bad packets earlier, and
602686cdd19SJun-ichiro itojun Hagino 	 * (2) to be safe against future ip6_input change.
603686cdd19SJun-ichiro itojun Hagino 	 */
604686cdd19SJun-ichiro itojun Hagino 	if (IN6_IS_ADDR_V4COMPAT(in6) || IN6_IS_ADDR_V4MAPPED(in6))
605686cdd19SJun-ichiro itojun Hagino 		return -1;
606686cdd19SJun-ichiro itojun Hagino 
607686cdd19SJun-ichiro itojun Hagino 	return 0;
608686cdd19SJun-ichiro itojun Hagino }
609686cdd19SJun-ichiro itojun Hagino 
61006cd035aSAndrey V. Elsukov static int
6116d8fdfa9SAndrey V. Elsukov in_stf_input(struct mbuf *m, int off, int proto, void *arg)
612686cdd19SJun-ichiro itojun Hagino {
6136d8fdfa9SAndrey V. Elsukov 	struct stf_softc *sc = arg;
614686cdd19SJun-ichiro itojun Hagino 	struct ip *ip;
615686cdd19SJun-ichiro itojun Hagino 	struct ip6_hdr *ip6;
616686cdd19SJun-ichiro itojun Hagino 	u_int8_t otos, itos;
617686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
618686cdd19SJun-ichiro itojun Hagino 
619b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
620b8a6e03fSGleb Smirnoff 
621686cdd19SJun-ichiro itojun Hagino 	if (proto != IPPROTO_IPV6) {
622686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
6238f5a8818SKevin Lo 		return (IPPROTO_DONE);
624686cdd19SJun-ichiro itojun Hagino 	}
625686cdd19SJun-ichiro itojun Hagino 
626686cdd19SJun-ichiro itojun Hagino 	ip = mtod(m, struct ip *);
627fc74a9f9SBrooks Davis 	if (sc == NULL || (STF2IFP(sc)->if_flags & IFF_UP) == 0) {
628686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
6298f5a8818SKevin Lo 		return (IPPROTO_DONE);
630686cdd19SJun-ichiro itojun Hagino 	}
631686cdd19SJun-ichiro itojun Hagino 
632fc74a9f9SBrooks Davis 	ifp = STF2IFP(sc);
633686cdd19SJun-ichiro itojun Hagino 
6346b459e49SRobert Watson #ifdef MAC
63530d239bcSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
6366b459e49SRobert Watson #endif
6376b459e49SRobert Watson 
638686cdd19SJun-ichiro itojun Hagino 	/*
639686cdd19SJun-ichiro itojun Hagino 	 * perform sanity check against outer src/dst.
640686cdd19SJun-ichiro itojun Hagino 	 * for source, perform ingress filter as well.
641686cdd19SJun-ichiro itojun Hagino 	 */
64233841545SHajimu UMEMOTO 	if (stf_checkaddr4(sc, &ip->ip_dst, NULL) < 0 ||
64333841545SHajimu UMEMOTO 	    stf_checkaddr4(sc, &ip->ip_src, m->m_pkthdr.rcvif) < 0) {
644686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
6458f5a8818SKevin Lo 		return (IPPROTO_DONE);
646686cdd19SJun-ichiro itojun Hagino 	}
647686cdd19SJun-ichiro itojun Hagino 
648686cdd19SJun-ichiro itojun Hagino 	otos = ip->ip_tos;
649686cdd19SJun-ichiro itojun Hagino 	m_adj(m, off);
650686cdd19SJun-ichiro itojun Hagino 
651686cdd19SJun-ichiro itojun Hagino 	if (m->m_len < sizeof(*ip6)) {
652686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(*ip6));
653686cdd19SJun-ichiro itojun Hagino 		if (!m)
6548f5a8818SKevin Lo 			return (IPPROTO_DONE);
655686cdd19SJun-ichiro itojun Hagino 	}
656686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
657686cdd19SJun-ichiro itojun Hagino 
658686cdd19SJun-ichiro itojun Hagino 	/*
659686cdd19SJun-ichiro itojun Hagino 	 * perform sanity check against inner src/dst.
660686cdd19SJun-ichiro itojun Hagino 	 * for source, perform ingress filter as well.
661686cdd19SJun-ichiro itojun Hagino 	 */
66233841545SHajimu UMEMOTO 	if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
66333841545SHajimu UMEMOTO 	    stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
664686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
6658f5a8818SKevin Lo 		return (IPPROTO_DONE);
666686cdd19SJun-ichiro itojun Hagino 	}
667686cdd19SJun-ichiro itojun Hagino 
668*bb4a7d94SKristof Provost 	itos = IPV6_TRAFFIC_CLASS(ip6);
669686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_LINK1) != 0)
670686cdd19SJun-ichiro itojun Hagino 		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
67133841545SHajimu UMEMOTO 	else
67233841545SHajimu UMEMOTO 		ip_ecn_egress(ECN_NOCARE, &otos, &itos);
673686cdd19SJun-ichiro itojun Hagino 	ip6->ip6_flow &= ~htonl(0xff << 20);
674686cdd19SJun-ichiro itojun Hagino 	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
675686cdd19SJun-ichiro itojun Hagino 
676686cdd19SJun-ichiro itojun Hagino 	m->m_pkthdr.rcvif = ifp;
677686cdd19SJun-ichiro itojun Hagino 
67816d878ccSChristian S.J. Peron 	if (bpf_peers_present(ifp->if_bpf)) {
679686cdd19SJun-ichiro itojun Hagino 		/*
680686cdd19SJun-ichiro itojun Hagino 		 * We need to prepend the address family as
681686cdd19SJun-ichiro itojun Hagino 		 * a four byte field.  Cons up a dummy header
682686cdd19SJun-ichiro itojun Hagino 		 * to pacify bpf.  This is safe because bpf
683686cdd19SJun-ichiro itojun Hagino 		 * will only read from the mbuf (i.e., it won't
684686cdd19SJun-ichiro itojun Hagino 		 * try to free it or keep a pointer a to it).
685686cdd19SJun-ichiro itojun Hagino 		 */
68633841545SHajimu UMEMOTO 		u_int32_t af = AF_INET6;
6879b1a7076SHajimu UMEMOTO 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
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 	 */
6963751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
6973751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
698a34c6aebSBjoern A. Zeeb 	M_SETFIB(m, ifp->if_fib);
6991cafed39SJonathan Lemon 	netisr_dispatch(NETISR_IPV6, m);
7008f5a8818SKevin Lo 	return (IPPROTO_DONE);
701686cdd19SJun-ichiro itojun Hagino }
702686cdd19SJun-ichiro itojun Hagino 
703686cdd19SJun-ichiro itojun Hagino static int
704926381e1SAndrey V. Elsukov stf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
705686cdd19SJun-ichiro itojun Hagino {
706686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ifa;
707686cdd19SJun-ichiro itojun Hagino 	struct ifreq *ifr;
708686cdd19SJun-ichiro itojun Hagino 	struct sockaddr_in6 *sin6;
7098d95b0ceSSUZUKI Shinsuke 	struct in_addr addr;
71099ab4b12SAlexander V. Chernikov 	int error, mtu;
711686cdd19SJun-ichiro itojun Hagino 
712686cdd19SJun-ichiro itojun Hagino 	error = 0;
713686cdd19SJun-ichiro itojun Hagino 	switch (cmd) {
714686cdd19SJun-ichiro itojun Hagino 	case SIOCSIFADDR:
715686cdd19SJun-ichiro itojun Hagino 		ifa = (struct ifaddr *)data;
716686cdd19SJun-ichiro itojun Hagino 		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
717686cdd19SJun-ichiro itojun Hagino 			error = EAFNOSUPPORT;
718686cdd19SJun-ichiro itojun Hagino 			break;
719686cdd19SJun-ichiro itojun Hagino 		}
720686cdd19SJun-ichiro itojun Hagino 		sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
7218d95b0ceSSUZUKI Shinsuke 		if (!IN6_IS_ADDR_6TO4(&sin6->sin6_addr)) {
7228d95b0ceSSUZUKI Shinsuke 			error = EINVAL;
7238d95b0ceSSUZUKI Shinsuke 			break;
7248d95b0ceSSUZUKI Shinsuke 		}
7258d95b0ceSSUZUKI Shinsuke 		bcopy(GET_V4(&sin6->sin6_addr), &addr, sizeof(addr));
7268d95b0ceSSUZUKI Shinsuke 		if (isrfc1918addr(&addr)) {
7278d95b0ceSSUZUKI Shinsuke 			error = EINVAL;
7288d95b0ceSSUZUKI Shinsuke 			break;
7298d95b0ceSSUZUKI Shinsuke 		}
7308d95b0ceSSUZUKI Shinsuke 
731686cdd19SJun-ichiro itojun Hagino 		ifp->if_flags |= IFF_UP;
732ca1163bdSMark Johnston 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
733686cdd19SJun-ichiro itojun Hagino 		break;
734686cdd19SJun-ichiro itojun Hagino 
735686cdd19SJun-ichiro itojun Hagino 	case SIOCADDMULTI:
736686cdd19SJun-ichiro itojun Hagino 	case SIOCDELMULTI:
737686cdd19SJun-ichiro itojun Hagino 		ifr = (struct ifreq *)data;
738686cdd19SJun-ichiro itojun Hagino 		if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
739686cdd19SJun-ichiro itojun Hagino 			;
740686cdd19SJun-ichiro itojun Hagino 		else
741686cdd19SJun-ichiro itojun Hagino 			error = EAFNOSUPPORT;
742686cdd19SJun-ichiro itojun Hagino 		break;
743686cdd19SJun-ichiro itojun Hagino 
74499ab4b12SAlexander V. Chernikov 	case SIOCGIFMTU:
74599ab4b12SAlexander V. Chernikov 		break;
74699ab4b12SAlexander V. Chernikov 
74799ab4b12SAlexander V. Chernikov 	case SIOCSIFMTU:
74899ab4b12SAlexander V. Chernikov 		ifr = (struct ifreq *)data;
74999ab4b12SAlexander V. Chernikov 		mtu = ifr->ifr_mtu;
75099ab4b12SAlexander V. Chernikov 		/* RFC 4213 3.2 ideal world MTU */
75199ab4b12SAlexander V. Chernikov 		if (mtu < IPV6_MINMTU || mtu > IF_MAXMTU - 20)
75299ab4b12SAlexander V. Chernikov 			return (EINVAL);
75399ab4b12SAlexander V. Chernikov 		ifp->if_mtu = mtu;
75499ab4b12SAlexander V. Chernikov 		break;
75599ab4b12SAlexander V. Chernikov 
756686cdd19SJun-ichiro itojun Hagino 	default:
757686cdd19SJun-ichiro itojun Hagino 		error = EINVAL;
758686cdd19SJun-ichiro itojun Hagino 		break;
759686cdd19SJun-ichiro itojun Hagino 	}
760686cdd19SJun-ichiro itojun Hagino 
761686cdd19SJun-ichiro itojun Hagino 	return error;
762686cdd19SJun-ichiro itojun Hagino }
763