xref: /freebsd/sys/net/if_stf.c (revision 439da7f06dcea5af17e7d5b3b4784e54207194ac)
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.
819dc6445SKristof Provost  * Copyright (c) 2010 Hiroki Sato <hrs@FreeBSD.org>
919dc6445SKristof Provost  * Copyright (c) 2013 Ermal Luci <eri@FreeBSD.org>
1019dc6445SKristof Provost  * Copyright (c) 2017-2021 Rubicon Communications, LLC (Netgate)
11686cdd19SJun-ichiro itojun Hagino  * All rights reserved.
12686cdd19SJun-ichiro itojun Hagino  *
13686cdd19SJun-ichiro itojun Hagino  * Redistribution and use in source and binary forms, with or without
14686cdd19SJun-ichiro itojun Hagino  * modification, are permitted provided that the following conditions
15686cdd19SJun-ichiro itojun Hagino  * are met:
16686cdd19SJun-ichiro itojun Hagino  * 1. Redistributions of source code must retain the above copyright
17686cdd19SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer.
18686cdd19SJun-ichiro itojun Hagino  * 2. Redistributions in binary form must reproduce the above copyright
19686cdd19SJun-ichiro itojun Hagino  *    notice, this list of conditions and the following disclaimer in the
20686cdd19SJun-ichiro itojun Hagino  *    documentation and/or other materials provided with the distribution.
21686cdd19SJun-ichiro itojun Hagino  * 3. Neither the name of the project nor the names of its contributors
22686cdd19SJun-ichiro itojun Hagino  *    may be used to endorse or promote products derived from this software
23686cdd19SJun-ichiro itojun Hagino  *    without specific prior written permission.
24686cdd19SJun-ichiro itojun Hagino  *
25686cdd19SJun-ichiro itojun Hagino  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
26686cdd19SJun-ichiro itojun Hagino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27686cdd19SJun-ichiro itojun Hagino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28686cdd19SJun-ichiro itojun Hagino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
29686cdd19SJun-ichiro itojun Hagino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30686cdd19SJun-ichiro itojun Hagino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31686cdd19SJun-ichiro itojun Hagino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32686cdd19SJun-ichiro itojun Hagino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33686cdd19SJun-ichiro itojun Hagino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34686cdd19SJun-ichiro itojun Hagino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35686cdd19SJun-ichiro itojun Hagino  * SUCH DAMAGE.
36686cdd19SJun-ichiro itojun Hagino  */
37686cdd19SJun-ichiro itojun Hagino 
38686cdd19SJun-ichiro itojun Hagino /*
3933841545SHajimu UMEMOTO  * 6to4 interface, based on RFC3056.
40686cdd19SJun-ichiro itojun Hagino  *
41686cdd19SJun-ichiro itojun Hagino  * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
42686cdd19SJun-ichiro itojun Hagino  * There is no address mapping defined from IPv6 multicast address to IPv4
43686cdd19SJun-ichiro itojun Hagino  * address.  Therefore, we do not have IFF_MULTICAST on the interface.
44686cdd19SJun-ichiro itojun Hagino  *
45686cdd19SJun-ichiro itojun Hagino  * Due to the lack of address mapping for link-local addresses, we cannot
46686cdd19SJun-ichiro itojun Hagino  * throw packets toward link-local addresses (fe80::x).  Also, we cannot throw
47686cdd19SJun-ichiro itojun Hagino  * packets to link-local multicast addresses (ff02::x).
48686cdd19SJun-ichiro itojun Hagino  *
49686cdd19SJun-ichiro itojun Hagino  * Here are interesting symptoms due to the lack of link-local address:
50686cdd19SJun-ichiro itojun Hagino  *
51686cdd19SJun-ichiro itojun Hagino  * Unicast routing exchange:
52686cdd19SJun-ichiro itojun Hagino  * - RIPng: Impossible.  Uses link-local multicast packet toward ff02::9,
53686cdd19SJun-ichiro itojun Hagino  *   and link-local addresses as nexthop.
54686cdd19SJun-ichiro itojun Hagino  * - OSPFv6: Impossible.  OSPFv6 assumes that there's link-local address
55686cdd19SJun-ichiro itojun Hagino  *   assigned to the link, and makes use of them.  Also, HELLO packets use
56686cdd19SJun-ichiro itojun Hagino  *   link-local multicast addresses (ff02::5 and ff02::6).
57686cdd19SJun-ichiro itojun Hagino  * - BGP4+: Maybe.  You can only use global address as nexthop, and global
58686cdd19SJun-ichiro itojun Hagino  *   address as TCP endpoint address.
59686cdd19SJun-ichiro itojun Hagino  *
60686cdd19SJun-ichiro itojun Hagino  * Multicast routing protocols:
61686cdd19SJun-ichiro itojun Hagino  * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
62686cdd19SJun-ichiro itojun Hagino  *   Adjacent PIM routers must be configured manually (is it really spec-wise
63686cdd19SJun-ichiro itojun Hagino  *   correct thing to do?).
64686cdd19SJun-ichiro itojun Hagino  *
65686cdd19SJun-ichiro itojun Hagino  * ICMPv6:
66686cdd19SJun-ichiro itojun Hagino  * - Redirects cannot be used due to the lack of link-local address.
67686cdd19SJun-ichiro itojun Hagino  *
6833841545SHajimu UMEMOTO  * stf interface does not have, and will not need, a link-local address.
6933841545SHajimu UMEMOTO  * It seems to have no real benefit and does not help the above symptoms much.
7033841545SHajimu UMEMOTO  * Even if we assign link-locals to interface, we cannot really
7133841545SHajimu UMEMOTO  * use link-local unicast/multicast on top of 6to4 cloud (since there's no
7233841545SHajimu UMEMOTO  * encapsulation defined for link-local address), and the above analysis does
7333841545SHajimu UMEMOTO  * not change.  RFC3056 does not mandate the assignment of link-local address
7433841545SHajimu UMEMOTO  * either.
75686cdd19SJun-ichiro itojun Hagino  *
76686cdd19SJun-ichiro itojun Hagino  * 6to4 interface has security issues.  Refer to
77686cdd19SJun-ichiro itojun Hagino  * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
78686cdd19SJun-ichiro itojun Hagino  * for details.  The code tries to filter out some of malicious packets.
79686cdd19SJun-ichiro itojun Hagino  * Note that there is no way to be 100% secure.
80686cdd19SJun-ichiro itojun Hagino  */
81686cdd19SJun-ichiro itojun Hagino 
82686cdd19SJun-ichiro itojun Hagino #include <sys/param.h>
83686cdd19SJun-ichiro itojun Hagino #include <sys/systm.h>
84686cdd19SJun-ichiro itojun Hagino #include <sys/socket.h>
85686cdd19SJun-ichiro itojun Hagino #include <sys/sockio.h>
86686cdd19SJun-ichiro itojun Hagino #include <sys/mbuf.h>
8719dc6445SKristof Provost #include <sys/endian.h>
88686cdd19SJun-ichiro itojun Hagino #include <sys/errno.h>
89686cdd19SJun-ichiro itojun Hagino #include <sys/kernel.h>
90cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
915dba30f1SPoul-Henning Kamp #include <sys/module.h>
9219dc6445SKristof Provost #include <sys/priv.h>
938b07e49aSJulian Elischer #include <sys/proc.h>
94abb64706SBrooks Davis #include <sys/queue.h>
95b46512f7SKristof Provost #include <sys/sdt.h>
960dae32f2SDavid Malone #include <sys/sysctl.h>
97686cdd19SJun-ichiro itojun Hagino #include <machine/cpu.h>
98686cdd19SJun-ichiro itojun Hagino 
99686cdd19SJun-ichiro itojun Hagino #include <sys/malloc.h>
100686cdd19SJun-ichiro itojun Hagino 
101686cdd19SJun-ichiro itojun Hagino #include <net/if.h>
10276039bc8SGleb Smirnoff #include <net/if_var.h>
103f889d2efSBrooks Davis #include <net/if_clone.h>
104686cdd19SJun-ichiro itojun Hagino #include <net/route.h>
1056ad7446cSAlexander V. Chernikov #include <net/route/nhop.h>
106686cdd19SJun-ichiro itojun Hagino #include <net/netisr.h>
10719dc6445SKristof Provost #include <net/if_stf.h>
108686cdd19SJun-ichiro itojun Hagino #include <net/if_types.h>
109530c0060SRobert Watson #include <net/vnet.h>
110686cdd19SJun-ichiro itojun Hagino 
111686cdd19SJun-ichiro itojun Hagino #include <netinet/in.h>
1120792bcbbSAlexander V. Chernikov #include <netinet/in_fib.h>
113686cdd19SJun-ichiro itojun Hagino #include <netinet/in_systm.h>
114686cdd19SJun-ichiro itojun Hagino #include <netinet/ip.h>
115686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_var.h>
116686cdd19SJun-ichiro itojun Hagino #include <netinet/in_var.h>
117686cdd19SJun-ichiro itojun Hagino 
118686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
11919dc6445SKristof Provost #include <netinet6/in6_fib.h>
120686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6_var.h>
121686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_var.h>
122686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_ecn.h>
123686cdd19SJun-ichiro itojun Hagino 
124686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
125686cdd19SJun-ichiro itojun Hagino 
126686cdd19SJun-ichiro itojun Hagino #include <machine/stdarg.h>
127686cdd19SJun-ichiro itojun Hagino 
128686cdd19SJun-ichiro itojun Hagino #include <net/bpf.h>
129686cdd19SJun-ichiro itojun Hagino 
130aed55708SRobert Watson #include <security/mac/mac_framework.h>
131aed55708SRobert Watson 
132b46512f7SKristof Provost SDT_PROVIDER_DEFINE(if_stf);
133b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , encapcheck, in, "struct mbuf *", "int", "int");
134b46512f7SKristof Provost SDT_PROBE_DEFINE0(if_stf, , encapcheck, accept);
135b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , getsrcifa6, in, "struct ifnet *",
136b46512f7SKristof Provost     "struct in6_addr *", "struct in6_addr *");
137b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , getsrcifa6, found, "struct in6_addr *",
138b46512f7SKristof Provost     "struct in6_addr *");
139b46512f7SKristof Provost SDT_PROBE_DEFINE0(if_stf, , getsrcifa6, notfound);
140b46512f7SKristof Provost 
141b46512f7SKristof Provost SDT_PROBE_DEFINE4(if_stf, , stf_output, in, "struct ifnet *", "struct mbuf *",
142b46512f7SKristof Provost     "struct sockaddr *", "struct route *");
143b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , stf_output, error, "int", "int");
144b46512f7SKristof Provost SDT_PROBE_DEFINE1(if_stf, , stf_output, out, "int");
145b46512f7SKristof Provost 
146b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , checkaddr6, in, "struct stf_softc *",
147b46512f7SKristof Provost     "struct in6_addr *", "struct ifnet *");
148b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , checkaddr6, out, "int", "int");
149b46512f7SKristof Provost 
150b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , stf_input, in, "struct mbuf *", "int", "int");
151b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , stf_input, out, "int", "int");
152b46512f7SKristof Provost 
153b46512f7SKristof Provost SDT_PROBE_DEFINE3(if_stf, , ioctl, sv4net, "struct in_addr *",
154b46512f7SKristof Provost     "struct in_addr *", "int");
155b46512f7SKristof Provost SDT_PROBE_DEFINE1(if_stf, , ioctl, sdstv4, "struct in_addr *");
156b46512f7SKristof Provost SDT_PROBE_DEFINE1(if_stf, , ioctl, ifaddr, "struct ifaddr *");
157b46512f7SKristof Provost 
158b46512f7SKristof Provost SDT_PROBE_DEFINE4(if_stf, , getin4addr_in6, out, "struct in6_addr *",
159b46512f7SKristof Provost     "struct in6_addr *", "struct in6_addr *", "struct sockaddr_in *");
160b46512f7SKristof Provost 
161b46512f7SKristof Provost SDT_PROBE_DEFINE2(if_stf, , getin4addr, in, "struct in6_addr *", "struct in6_addr *");
162b46512f7SKristof Provost SDT_PROBE_DEFINE1(if_stf, , getin4addr, out, "struct sockaddr_in *");
163b46512f7SKristof Provost 
1640dae32f2SDavid Malone SYSCTL_DECL(_net_link);
1657029da5cSPawel Biernacki static SYSCTL_NODE(_net_link, IFT_STF, stf, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1667029da5cSPawel Biernacki     "6to4 Interface");
1670dae32f2SDavid Malone 
16851743c5fSAndrey V. Elsukov static int stf_permit_rfc1918 = 0;
169af3b2549SHans Petter Selasky SYSCTL_INT(_net_link_stf, OID_AUTO, permit_rfc1918, CTLFLAG_RWTUN,
17051743c5fSAndrey V. Elsukov     &stf_permit_rfc1918, 0, "Permit the use of private IPv4 addresses");
17151743c5fSAndrey V. Elsukov 
172f889d2efSBrooks Davis #define STFUNIT		0
173abb64706SBrooks Davis 
174686cdd19SJun-ichiro itojun Hagino #define IN6_IS_ADDR_6TO4(x)	(ntohs((x)->s6_addr16[0]) == 0x2002)
1758d95b0ceSSUZUKI Shinsuke 
1768d95b0ceSSUZUKI Shinsuke /*
1778d95b0ceSSUZUKI Shinsuke  * XXX: Return a pointer with 16-bit aligned.  Don't cast it to
1788d95b0ceSSUZUKI Shinsuke  * struct in_addr *; use bcopy() instead.
1798d95b0ceSSUZUKI Shinsuke  */
18047e8d432SGleb Smirnoff #define GET_V4(x)	(&(x)->s6_addr16[1])
181686cdd19SJun-ichiro itojun Hagino 
182686cdd19SJun-ichiro itojun Hagino struct stf_softc {
183fc74a9f9SBrooks Davis 	struct ifnet	*sc_ifp;
18419dc6445SKristof Provost 	in_addr_t	braddr;		/* Border relay IPv4 address */
18519dc6445SKristof Provost 	in_addr_t	srcv4_addr;	/* Our IPv4 WAN address */
18619dc6445SKristof Provost 	u_int		v4prefixlen;	/* How much of the v4 address to include in our address. */
1878b07e49aSJulian Elischer 	u_int		sc_fibnum;
188686cdd19SJun-ichiro itojun Hagino 	const struct encaptab *encap_cookie;
189686cdd19SJun-ichiro itojun Hagino };
190fc74a9f9SBrooks Davis #define STF2IFP(sc)	((sc)->sc_ifp)
191686cdd19SJun-ichiro itojun Hagino 
19242a58907SGleb Smirnoff static const char stfname[] = "stf";
19342a58907SGleb Smirnoff 
19442a58907SGleb Smirnoff static MALLOC_DEFINE(M_STF, stfname, "6to4 Tunnel Interface");
195591cf7ceSRobert Watson static const int ip_stf_ttl = 40;
196686cdd19SJun-ichiro itojun Hagino 
1976d8fdfa9SAndrey V. Elsukov static int in_stf_input(struct mbuf *, int, int, void *);
198f889d2efSBrooks Davis static char *stfnames[] = {"stf0", "stf", "6to4", NULL};
199f889d2efSBrooks Davis 
200929ddbbbSAlfred Perlstein static int stfmodevent(module_t, int, void *);
201929ddbbbSAlfred Perlstein static int stf_encapcheck(const struct mbuf *, int, int, void *);
202d74b9a2cSAlexander V. Chernikov static int stf_getsrcifa6(struct ifnet *, struct in6_addr *, struct in6_addr *);
20347e8d432SGleb Smirnoff static int stf_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
204279aa3d4SKip Macy 	struct route *);
205ce9d7b2fSHajimu UMEMOTO static int isrfc1918addr(struct in_addr *);
206929ddbbbSAlfred Perlstein static int stf_checkaddr4(struct stf_softc *, struct in_addr *,
207929ddbbbSAlfred Perlstein 	struct ifnet *);
208929ddbbbSAlfred Perlstein static int stf_checkaddr6(struct stf_softc *, struct in6_addr *,
209929ddbbbSAlfred Perlstein 	struct ifnet *);
21019dc6445SKristof Provost static struct sockaddr_in *stf_getin4addr_in6(struct stf_softc *,
21119dc6445SKristof Provost 	struct sockaddr_in *, struct in6_addr, struct in6_addr,
21219dc6445SKristof Provost 	struct in6_addr);
21319dc6445SKristof Provost static struct sockaddr_in *stf_getin4addr(struct stf_softc *,
21419dc6445SKristof Provost 	struct sockaddr_in *, struct in6_addr, struct in6_addr);
215929ddbbbSAlfred Perlstein static int stf_ioctl(struct ifnet *, u_long, caddr_t);
216686cdd19SJun-ichiro itojun Hagino 
217f889d2efSBrooks Davis static int stf_clone_match(struct if_clone *, const char *);
2186b7330e2SSam Leffler static int stf_clone_create(struct if_clone *, char *, size_t, caddr_t);
219f889d2efSBrooks Davis static int stf_clone_destroy(struct if_clone *, struct ifnet *);
2208e45fed3SKristof Provost VNET_DEFINE_STATIC(struct if_clone *, stf_cloner);
2218e45fed3SKristof Provost #define V_stf_cloner	VNET(stf_cloner)
222abb64706SBrooks Davis 
2236d8fdfa9SAndrey V. Elsukov static const struct encap_config ipv4_encap_cfg = {
2246d8fdfa9SAndrey V. Elsukov 	.proto = IPPROTO_IPV6,
2256d8fdfa9SAndrey V. Elsukov 	.min_length = sizeof(struct ip),
2266d8fdfa9SAndrey V. Elsukov 	.exact_match = (sizeof(in_addr_t) << 3) + 8,
2276d8fdfa9SAndrey V. Elsukov 	.check = stf_encapcheck,
2286d8fdfa9SAndrey V. Elsukov 	.input = in_stf_input
2296d8fdfa9SAndrey V. Elsukov };
2306d8fdfa9SAndrey V. Elsukov 
231bb2bfb4fSBrooks Davis static int
232f889d2efSBrooks Davis stf_clone_match(struct if_clone *ifc, const char *name)
233686cdd19SJun-ichiro itojun Hagino {
234f889d2efSBrooks Davis 	int i;
235f889d2efSBrooks Davis 
236f889d2efSBrooks Davis 	for(i = 0; stfnames[i] != NULL; i++) {
237f889d2efSBrooks Davis 		if (strcmp(stfnames[i], name) == 0)
238f889d2efSBrooks Davis 			return (1);
239f889d2efSBrooks Davis 	}
240f889d2efSBrooks Davis 
241f889d2efSBrooks Davis 	return (0);
242f889d2efSBrooks Davis }
243f889d2efSBrooks Davis 
244f889d2efSBrooks Davis static int
2456b7330e2SSam Leffler stf_clone_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
246f889d2efSBrooks Davis {
247d177868cSLuiz Otavio O Souza 	char *dp;
248d177868cSLuiz Otavio O Souza 	int err, unit, wildcard;
249686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
2501861b710SBrooks Davis 	struct ifnet *ifp;
251686cdd19SJun-ichiro itojun Hagino 
252d177868cSLuiz Otavio O Souza 	err = ifc_name2unit(name, &unit);
253d177868cSLuiz Otavio O Souza 	if (err != 0)
254d177868cSLuiz Otavio O Souza 		return (err);
255d177868cSLuiz Otavio O Souza 	wildcard = (unit < 0);
256d177868cSLuiz Otavio O Souza 
257f889d2efSBrooks Davis 	/*
258f889d2efSBrooks Davis 	 * We can only have one unit, but since unit allocation is
259f889d2efSBrooks Davis 	 * already locked, we use it to keep from allocating extra
260f889d2efSBrooks Davis 	 * interfaces.
261f889d2efSBrooks Davis 	 */
262f889d2efSBrooks Davis 	unit = STFUNIT;
263f889d2efSBrooks Davis 	err = ifc_alloc_unit(ifc, &unit);
264f889d2efSBrooks Davis 	if (err != 0)
265f889d2efSBrooks Davis 		return (err);
266f889d2efSBrooks Davis 
267a163d034SWarner Losh 	sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
268b03965ddSBrooks Davis 	ifp = STF2IFP(sc) = if_alloc(IFT_STF);
269fc74a9f9SBrooks Davis 	if (ifp == NULL) {
270fc74a9f9SBrooks Davis 		free(sc, M_STF);
271fc74a9f9SBrooks Davis 		ifc_free_unit(ifc, unit);
272fc74a9f9SBrooks Davis 		return (ENOSPC);
273fc74a9f9SBrooks Davis 	}
274b03965ddSBrooks Davis 	ifp->if_softc = sc;
2758b07e49aSJulian Elischer 	sc->sc_fibnum = curthread->td_proc->p_fibnum;
276b03965ddSBrooks Davis 
277f889d2efSBrooks Davis 	/*
278f889d2efSBrooks Davis 	 * Set the name manually rather then using if_initname because
279f889d2efSBrooks Davis 	 * we don't conform to the default naming convention for interfaces.
280d177868cSLuiz Otavio O Souza 	 * In the wildcard case, we need to update the name.
281f889d2efSBrooks Davis 	 */
282d177868cSLuiz Otavio O Souza 	if (wildcard) {
283d177868cSLuiz Otavio O Souza 		for (dp = name; *dp != '\0'; dp++);
284d177868cSLuiz Otavio O Souza 		if (snprintf(dp, len - (dp-name), "%d", unit) >
285d177868cSLuiz Otavio O Souza 		    len - (dp-name) - 1) {
286d177868cSLuiz Otavio O Souza 			/*
287d177868cSLuiz Otavio O Souza 			 * This can only be a programmer error and
288d177868cSLuiz Otavio O Souza 			 * there's no straightforward way to recover if
289d177868cSLuiz Otavio O Souza 			 * it happens.
290d177868cSLuiz Otavio O Souza 			 */
291d177868cSLuiz Otavio O Souza 			panic("if_clone_create(): interface name too long");
292d177868cSLuiz Otavio O Souza 		}
293d177868cSLuiz Otavio O Souza 	}
294f889d2efSBrooks Davis 	strlcpy(ifp->if_xname, name, IFNAMSIZ);
29542a58907SGleb Smirnoff 	ifp->if_dname = stfname;
296f889d2efSBrooks Davis 	ifp->if_dunit = IF_DUNIT_NONE;
297abb64706SBrooks Davis 
2986d8fdfa9SAndrey V. Elsukov 	sc->encap_cookie = ip_encap_attach(&ipv4_encap_cfg, sc, M_WAITOK);
299abb64706SBrooks Davis 	if (sc->encap_cookie == NULL) {
3001861b710SBrooks Davis 		if_printf(ifp, "attach failed\n");
301abb64706SBrooks Davis 		free(sc, M_STF);
302f889d2efSBrooks Davis 		ifc_free_unit(ifc, unit);
30353dab5feSBrooks Davis 		return (ENOMEM);
304686cdd19SJun-ichiro itojun Hagino 	}
305686cdd19SJun-ichiro itojun Hagino 
3061861b710SBrooks Davis 	ifp->if_mtu    = IPV6_MMTU;
3071861b710SBrooks Davis 	ifp->if_ioctl  = stf_ioctl;
3081861b710SBrooks Davis 	ifp->if_output = stf_output;
309e50d35e6SMaxim Sobolev 	ifp->if_snd.ifq_maxlen = ifqmaxlen;
3101861b710SBrooks Davis 	if_attach(ifp);
31101399f34SDavid Malone 	bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
312abb64706SBrooks Davis 	return (0);
313abb64706SBrooks Davis }
314abb64706SBrooks Davis 
315f889d2efSBrooks Davis static int
316f889d2efSBrooks Davis stf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
317abb64706SBrooks Davis {
318fc74a9f9SBrooks Davis 	struct stf_softc *sc = ifp->if_softc;
31946d0f824SMatt Macy 	int err __unused;
320abb64706SBrooks Davis 
3216d8fdfa9SAndrey V. Elsukov 	err = ip_encap_detach(sc->encap_cookie);
322febd0759SAndrew Thompson 	KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
323febd0759SAndrew Thompson 	bpfdetach(ifp);
324febd0759SAndrew Thompson 	if_detach(ifp);
325febd0759SAndrew Thompson 	if_free(ifp);
326febd0759SAndrew Thompson 
327febd0759SAndrew Thompson 	free(sc, M_STF);
328f889d2efSBrooks Davis 	ifc_free_unit(ifc, STFUNIT);
329f889d2efSBrooks Davis 
330f889d2efSBrooks Davis 	return (0);
331abb64706SBrooks Davis }
332abb64706SBrooks Davis 
3338e45fed3SKristof Provost static void
3348e45fed3SKristof Provost vnet_stf_init(const void *unused __unused)
3358e45fed3SKristof Provost {
3368e45fed3SKristof Provost 	V_stf_cloner = if_clone_advanced(stfname, 0, stf_clone_match,
3378e45fed3SKristof Provost 	    stf_clone_create, stf_clone_destroy);
3388e45fed3SKristof Provost }
3398e45fed3SKristof Provost VNET_SYSINIT(vnet_stf_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_stf_init, NULL);
3408e45fed3SKristof Provost 
3418e45fed3SKristof Provost static void
3428e45fed3SKristof Provost vnet_stf_uninit(const void *unused __unused)
3438e45fed3SKristof Provost {
3448e45fed3SKristof Provost 	if_clone_detach(V_stf_cloner);
3458e45fed3SKristof Provost 	V_stf_cloner = NULL;
3468e45fed3SKristof Provost }
3478e45fed3SKristof Provost VNET_SYSUNINIT(vnet_stf_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_stf_uninit,
3488e45fed3SKristof Provost     NULL);
3498e45fed3SKristof Provost 
350abb64706SBrooks Davis static int
351926381e1SAndrey V. Elsukov stfmodevent(module_t mod, int type, void *data)
352abb64706SBrooks Davis {
353abb64706SBrooks Davis 
354abb64706SBrooks Davis 	switch (type) {
355abb64706SBrooks Davis 	case MOD_LOAD:
3568e45fed3SKristof Provost 		/* Done in vnet_stf_init() */
357abb64706SBrooks Davis 		break;
358abb64706SBrooks Davis 	case MOD_UNLOAD:
3598e45fed3SKristof Provost 		/* Done in vnet_stf_uninit() */
36053dab5feSBrooks Davis 		break;
3613e019deaSPoul-Henning Kamp 	default:
3623e019deaSPoul-Henning Kamp 		return (EOPNOTSUPP);
363686cdd19SJun-ichiro itojun Hagino 	}
364686cdd19SJun-ichiro itojun Hagino 
36553dab5feSBrooks Davis 	return (0);
36653dab5feSBrooks Davis }
36753dab5feSBrooks Davis 
36853dab5feSBrooks Davis static moduledata_t stf_mod = {
36953dab5feSBrooks Davis 	"if_stf",
37053dab5feSBrooks Davis 	stfmodevent,
3719823d527SKevin Lo 	0
37253dab5feSBrooks Davis };
37353dab5feSBrooks Davis 
37453dab5feSBrooks Davis DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
37519dc6445SKristof Provost MODULE_VERSION(if_stf, 2);
376686cdd19SJun-ichiro itojun Hagino 
377686cdd19SJun-ichiro itojun Hagino static int
378926381e1SAndrey V. Elsukov stf_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
379686cdd19SJun-ichiro itojun Hagino {
380686cdd19SJun-ichiro itojun Hagino 	struct ip ip;
381686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
382d74b9a2cSAlexander V. Chernikov 	struct in6_addr addr6, mask6;
38319dc6445SKristof Provost 	struct sockaddr_in sin4addr, sin4mask;
384686cdd19SJun-ichiro itojun Hagino 
385b46512f7SKristof Provost 	SDT_PROBE3(if_stf, , encapcheck, in, m, off, proto);
386b46512f7SKristof Provost 
387686cdd19SJun-ichiro itojun Hagino 	sc = (struct stf_softc *)arg;
388686cdd19SJun-ichiro itojun Hagino 	if (sc == NULL)
3893576121cSKristof Provost 		return (0);
390686cdd19SJun-ichiro itojun Hagino 
391fc74a9f9SBrooks Davis 	if ((STF2IFP(sc)->if_flags & IFF_UP) == 0)
3923576121cSKristof Provost 		return (0);
393686cdd19SJun-ichiro itojun Hagino 
39433841545SHajimu UMEMOTO 	/* IFF_LINK0 means "no decapsulation" */
395fc74a9f9SBrooks Davis 	if ((STF2IFP(sc)->if_flags & IFF_LINK0) != 0)
3963576121cSKristof Provost 		return (0);
39733841545SHajimu UMEMOTO 
398686cdd19SJun-ichiro itojun Hagino 	if (proto != IPPROTO_IPV6)
3993576121cSKristof Provost 		return (0);
400686cdd19SJun-ichiro itojun Hagino 
401350e6227SAndrey V. Elsukov 	m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
402686cdd19SJun-ichiro itojun Hagino 
403686cdd19SJun-ichiro itojun Hagino 	if (ip.ip_v != 4)
4043576121cSKristof Provost 		return (0);
405686cdd19SJun-ichiro itojun Hagino 
406d74b9a2cSAlexander V. Chernikov 	if (stf_getsrcifa6(STF2IFP(sc), &addr6, &mask6) != 0)
407d74b9a2cSAlexander V. Chernikov 		return (0);
408686cdd19SJun-ichiro itojun Hagino 
40919dc6445SKristof Provost 	if (sc->srcv4_addr != INADDR_ANY) {
41019dc6445SKristof Provost 		sin4addr.sin_addr.s_addr = sc->srcv4_addr;
41119dc6445SKristof Provost 		sin4addr.sin_family = AF_INET;
41219dc6445SKristof Provost 	} else
41319dc6445SKristof Provost 		if (stf_getin4addr(sc, &sin4addr, addr6, mask6) == NULL)
4143576121cSKristof Provost 			return (0);
415686cdd19SJun-ichiro itojun Hagino 
41619dc6445SKristof Provost 	if (sin4addr.sin_addr.s_addr != ip.ip_dst.s_addr)
41719dc6445SKristof Provost 		return (0);
41819dc6445SKristof Provost 
41919dc6445SKristof Provost 	if (IN6_IS_ADDR_6TO4(&addr6)) {
420686cdd19SJun-ichiro itojun Hagino 		/*
42119dc6445SKristof Provost 		 * 6to4 (RFC 3056).
42219dc6445SKristof Provost 		 * Check if IPv4 src matches the IPv4 address derived
42319dc6445SKristof Provost 		 * from the local 6to4 address masked by prefixmask.
424686cdd19SJun-ichiro itojun Hagino 		 * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
425686cdd19SJun-ichiro itojun Hagino 		 * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
426686cdd19SJun-ichiro itojun Hagino 		 */
42719dc6445SKristof Provost 		memcpy(&sin4mask.sin_addr, GET_V4(&mask6),
42819dc6445SKristof Provost 		    sizeof(sin4mask.sin_addr));
42919dc6445SKristof Provost 		if ((sin4addr.sin_addr.s_addr & sin4mask.sin_addr.s_addr) !=
43019dc6445SKristof Provost 		    (ip.ip_src.s_addr & sin4mask.sin_addr.s_addr))
4313576121cSKristof Provost 			return (0);
43219dc6445SKristof Provost 	} else {
43319dc6445SKristof Provost 		/* 6rd (RFC 5569) */
43419dc6445SKristof Provost 		/*
43519dc6445SKristof Provost 		 * No restriction on the src address in the case of
43619dc6445SKristof Provost 		 * 6rd because the stf(4) interface always has a
43719dc6445SKristof Provost 		 * prefix which covers whole of IPv4 src address
43819dc6445SKristof Provost 		 * range.  So, stf_output() will catch all of
43919dc6445SKristof Provost 		 * 6rd-capsuled IPv4 traffic with suspicious inner dst
44019dc6445SKristof Provost 		 * IPv4 address (i.e. the IPv6 destination address is
44119dc6445SKristof Provost 		 * one the admin does not like to route to outside),
44219dc6445SKristof Provost 		 * and then it discard them silently.
44319dc6445SKristof Provost 		 */
44419dc6445SKristof Provost 	}
445686cdd19SJun-ichiro itojun Hagino 
446b46512f7SKristof Provost 	SDT_PROBE0(if_stf, , encapcheck, accept);
447b46512f7SKristof Provost 
448686cdd19SJun-ichiro itojun Hagino 	/* stf interface makes single side match only */
4493576121cSKristof Provost 	return (32);
450686cdd19SJun-ichiro itojun Hagino }
451686cdd19SJun-ichiro itojun Hagino 
452d74b9a2cSAlexander V. Chernikov static int
453d74b9a2cSAlexander V. Chernikov stf_getsrcifa6(struct ifnet *ifp, struct in6_addr *addr, struct in6_addr *mask)
454686cdd19SJun-ichiro itojun Hagino {
455686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ia;
456686cdd19SJun-ichiro itojun Hagino 	struct in_ifaddr *ia4;
45719dc6445SKristof Provost 	struct in6_addr addr6, mask6;
45819dc6445SKristof Provost 	struct sockaddr_in sin4;
45919dc6445SKristof Provost 	struct stf_softc *sc;
460686cdd19SJun-ichiro itojun Hagino 	struct in_addr in;
461686cdd19SJun-ichiro itojun Hagino 
4624b24e5b1SGleb Smirnoff 	NET_EPOCH_ASSERT();
4634b24e5b1SGleb Smirnoff 
46419dc6445SKristof Provost 	sc = ifp->if_softc;
46519dc6445SKristof Provost 
466b46512f7SKristof Provost 	SDT_PROBE3(if_stf, , getsrcifa6, in, ifp, addr, mask);
467b46512f7SKristof Provost 
468d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
469686cdd19SJun-ichiro itojun Hagino 		if (ia->ifa_addr->sa_family != AF_INET6)
470686cdd19SJun-ichiro itojun Hagino 			continue;
471686cdd19SJun-ichiro itojun Hagino 
47219dc6445SKristof Provost 		addr6 = *IFA_IN6(ia);
47319dc6445SKristof Provost 		mask6 = *IFA_MASKIN6(ia);
47419dc6445SKristof Provost 		if (sc->srcv4_addr != INADDR_ANY)
47519dc6445SKristof Provost 			bcopy(&sc->srcv4_addr, &in, sizeof(in));
47619dc6445SKristof Provost 		else {
47719dc6445SKristof Provost 			if (stf_getin4addr(sc, &sin4, addr6, mask6) == NULL)
47819dc6445SKristof Provost 				continue;
47919dc6445SKristof Provost 			bcopy(&sin4.sin_addr, &in, sizeof(in));
48019dc6445SKristof Provost 		}
48119dc6445SKristof Provost 
482c8ee75f2SGleb Smirnoff 		CK_LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
483686cdd19SJun-ichiro itojun Hagino 			if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
484686cdd19SJun-ichiro itojun Hagino 				break;
485686cdd19SJun-ichiro itojun Hagino 		if (ia4 == NULL)
486686cdd19SJun-ichiro itojun Hagino 			continue;
487686cdd19SJun-ichiro itojun Hagino 
48819dc6445SKristof Provost 		*addr = addr6;
48919dc6445SKristof Provost 		*mask = mask6;
490d74b9a2cSAlexander V. Chernikov 
491b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , getsrcifa6, found, addr, mask);
492b46512f7SKristof Provost 
493d74b9a2cSAlexander V. Chernikov 		return (0);
494686cdd19SJun-ichiro itojun Hagino 	}
495686cdd19SJun-ichiro itojun Hagino 
496b46512f7SKristof Provost 	SDT_PROBE0(if_stf, , getsrcifa6, notfound);
497b46512f7SKristof Provost 
498d74b9a2cSAlexander V. Chernikov 	return (ENOENT);
499686cdd19SJun-ichiro itojun Hagino }
500686cdd19SJun-ichiro itojun Hagino 
501686cdd19SJun-ichiro itojun Hagino static int
50247e8d432SGleb Smirnoff stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
50347e8d432SGleb Smirnoff     struct route *ro)
504686cdd19SJun-ichiro itojun Hagino {
505686cdd19SJun-ichiro itojun Hagino 	struct stf_softc *sc;
50647e8d432SGleb Smirnoff 	const struct sockaddr_in6 *dst6;
50719dc6445SKristof Provost 	struct sockaddr_in dst4, src4;
508686cdd19SJun-ichiro itojun Hagino 	u_int8_t tos;
509686cdd19SJun-ichiro itojun Hagino 	struct ip *ip;
510686cdd19SJun-ichiro itojun Hagino 	struct ip6_hdr *ip6;
511d74b9a2cSAlexander V. Chernikov 	struct in6_addr addr6, mask6;
5126b459e49SRobert Watson 	int error;
5136b459e49SRobert Watson 
514b46512f7SKristof Provost 	SDT_PROBE4(if_stf, , stf_output, in, ifp, m, dst, ro);
515b46512f7SKristof Provost 
5160dae32f2SDavid Malone #ifdef MAC
51730d239bcSRobert Watson 	error = mac_ifnet_check_transmit(ifp, m);
5186b459e49SRobert Watson 	if (error) {
5196b459e49SRobert Watson 		m_freem(m);
520b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , stf_output, error, error, __LINE__);
5216b459e49SRobert Watson 		return (error);
5226b459e49SRobert Watson 	}
5236b459e49SRobert Watson #endif
524686cdd19SJun-ichiro itojun Hagino 
525fc74a9f9SBrooks Davis 	sc = ifp->if_softc;
52647e8d432SGleb Smirnoff 	dst6 = (const struct sockaddr_in6 *)dst;
527686cdd19SJun-ichiro itojun Hagino 
528686cdd19SJun-ichiro itojun Hagino 	/* just in case */
529686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_UP) == 0) {
530686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
5313751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
532b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , stf_output, error, ENETDOWN, __LINE__);
5333576121cSKristof Provost 		return (ENETDOWN);
534686cdd19SJun-ichiro itojun Hagino 	}
535686cdd19SJun-ichiro itojun Hagino 
536686cdd19SJun-ichiro itojun Hagino 	/*
537686cdd19SJun-ichiro itojun Hagino 	 * If we don't have an ip4 address that match my inner ip6 address,
538686cdd19SJun-ichiro itojun Hagino 	 * we shouldn't generate output.  Without this check, we'll end up
539686cdd19SJun-ichiro itojun Hagino 	 * using wrong IPv4 source.
540686cdd19SJun-ichiro itojun Hagino 	 */
541d74b9a2cSAlexander V. Chernikov 	if (stf_getsrcifa6(ifp, &addr6, &mask6) != 0) {
542686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
5433751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
544b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , stf_output, error, ENETDOWN, __LINE__);
5453576121cSKristof Provost 		return (ENETDOWN);
546686cdd19SJun-ichiro itojun Hagino 	}
547686cdd19SJun-ichiro itojun Hagino 
548686cdd19SJun-ichiro itojun Hagino 	if (m->m_len < sizeof(*ip6)) {
549686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(*ip6));
550f26b2d5bSHajimu UMEMOTO 		if (!m) {
5513751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
552b46512f7SKristof Provost 			SDT_PROBE2(if_stf, , stf_output, error, ENOBUFS,
553b46512f7SKristof Provost 			    __LINE__);
5543576121cSKristof Provost 			return (ENOBUFS);
555686cdd19SJun-ichiro itojun Hagino 		}
556f26b2d5bSHajimu UMEMOTO 	}
557686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
558bb4a7d94SKristof Provost 	tos = IPV6_TRAFFIC_CLASS(ip6);
559686cdd19SJun-ichiro itojun Hagino 
56033841545SHajimu UMEMOTO 	/*
56133841545SHajimu UMEMOTO 	 * Pickup the right outer dst addr from the list of candidates.
56233841545SHajimu UMEMOTO 	 * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
56333841545SHajimu UMEMOTO 	 */
56419dc6445SKristof Provost 	if (stf_getin4addr_in6(sc, &dst4, addr6, mask6,
56519dc6445SKristof Provost 	    ip6->ip6_dst) == NULL) {
56619dc6445SKristof Provost 		if (sc->braddr != INADDR_ANY)
56719dc6445SKristof Provost 			dst4.sin_addr.s_addr = sc->braddr;
56819dc6445SKristof Provost 		else if (stf_getin4addr_in6(sc, &dst4, addr6, mask6,
56919dc6445SKristof Provost 		    dst6->sin6_addr) == NULL) {
57033841545SHajimu UMEMOTO 			m_freem(m);
5713751dddbSGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
572b46512f7SKristof Provost 			SDT_PROBE2(if_stf, , stf_output, error, ENETUNREACH,
573b46512f7SKristof Provost 			    __LINE__);
5743576121cSKristof Provost 			return (ENETUNREACH);
57533841545SHajimu UMEMOTO 		}
57619dc6445SKristof Provost 	}
57733841545SHajimu UMEMOTO 
57816d878ccSChristian S.J. Peron 	if (bpf_peers_present(ifp->if_bpf)) {
5798acb2290SHajimu UMEMOTO 		/*
5808acb2290SHajimu UMEMOTO 		 * We need to prepend the address family as
5818acb2290SHajimu UMEMOTO 		 * a four byte field.  Cons up a dummy header
5828acb2290SHajimu UMEMOTO 		 * to pacify bpf.  This is safe because bpf
5838acb2290SHajimu UMEMOTO 		 * will only read from the mbuf (i.e., it won't
5848acb2290SHajimu UMEMOTO 		 * try to free it or keep a pointer a to it).
5858acb2290SHajimu UMEMOTO 		 */
58647e8d432SGleb Smirnoff 		u_int af = AF_INET6;
587437ffe18SSam Leffler 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
5888acb2290SHajimu UMEMOTO 	}
5898acb2290SHajimu UMEMOTO 
590eb1b1807SGleb Smirnoff 	M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
591f26b2d5bSHajimu UMEMOTO 	if (m == NULL) {
5923751dddbSGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
593b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , stf_output, error, ENOBUFS, __LINE__);
5943576121cSKristof Provost 		return (ENOBUFS);
595f26b2d5bSHajimu UMEMOTO 	}
596686cdd19SJun-ichiro itojun Hagino 	ip = mtod(m, struct ip *);
597686cdd19SJun-ichiro itojun Hagino 
598686cdd19SJun-ichiro itojun Hagino 	bzero(ip, sizeof(*ip));
599686cdd19SJun-ichiro itojun Hagino 
60019dc6445SKristof Provost 	if (sc->srcv4_addr != INADDR_ANY)
60119dc6445SKristof Provost 		src4.sin_addr.s_addr = sc->srcv4_addr;
60219dc6445SKristof Provost 	else if (stf_getin4addr(sc, &src4, addr6, mask6) == NULL) {
60319dc6445SKristof Provost 		m_freem(m);
60419dc6445SKristof Provost 		if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
605b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , stf_output, error, ENETUNREACH, __LINE__);
60619dc6445SKristof Provost 		return (ENETUNREACH);
60719dc6445SKristof Provost 	}
60819dc6445SKristof Provost 	bcopy(&src4.sin_addr, &ip->ip_src, sizeof(ip->ip_src));
60919dc6445SKristof Provost 	bcopy(&dst4.sin_addr, &ip->ip_dst, sizeof(ip->ip_dst));
61019dc6445SKristof Provost 
611686cdd19SJun-ichiro itojun Hagino 	ip->ip_p = IPPROTO_IPV6;
61253dab5feSBrooks Davis 	ip->ip_ttl = ip_stf_ttl;
6138f134647SGleb Smirnoff 	ip->ip_len = htons(m->m_pkthdr.len);
614686cdd19SJun-ichiro itojun Hagino 	if (ifp->if_flags & IFF_LINK1)
615686cdd19SJun-ichiro itojun Hagino 		ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
61633841545SHajimu UMEMOTO 	else
61733841545SHajimu UMEMOTO 		ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
618686cdd19SJun-ichiro itojun Hagino 
6198b07e49aSJulian Elischer 	M_SETFIB(m, sc->sc_fibnum);
6203751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
621d74b9a2cSAlexander V. Chernikov 	error = ip_output(m, NULL, NULL, 0, NULL, NULL);
6220dae32f2SDavid Malone 
623b46512f7SKristof Provost 	SDT_PROBE1(if_stf, , stf_output, out, error);
6243576121cSKristof Provost 	return (error);
625686cdd19SJun-ichiro itojun Hagino }
626686cdd19SJun-ichiro itojun Hagino 
627686cdd19SJun-ichiro itojun Hagino static int
628926381e1SAndrey V. Elsukov isrfc1918addr(struct in_addr *in)
629ce9d7b2fSHajimu UMEMOTO {
630ce9d7b2fSHajimu UMEMOTO 	/*
631ce9d7b2fSHajimu UMEMOTO 	 * returns 1 if private address range:
632ce9d7b2fSHajimu UMEMOTO 	 * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
633ce9d7b2fSHajimu UMEMOTO 	 */
63451743c5fSAndrey V. Elsukov 	if (stf_permit_rfc1918 == 0 && (
63551743c5fSAndrey V. Elsukov 	    (ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
6368d95b0ceSSUZUKI Shinsuke 	    (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
63751743c5fSAndrey V. Elsukov 	    (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168))
6383576121cSKristof Provost 		return (1);
639ce9d7b2fSHajimu UMEMOTO 
6403576121cSKristof Provost 	return (0);
641ce9d7b2fSHajimu UMEMOTO }
642ce9d7b2fSHajimu UMEMOTO 
643ce9d7b2fSHajimu UMEMOTO static int
644926381e1SAndrey V. Elsukov stf_checkaddr4(struct stf_softc *sc, struct in_addr *in, struct ifnet *inifp)
645686cdd19SJun-ichiro itojun Hagino {
646686cdd19SJun-ichiro itojun Hagino 	struct in_ifaddr *ia4;
647686cdd19SJun-ichiro itojun Hagino 
648686cdd19SJun-ichiro itojun Hagino 	/*
649686cdd19SJun-ichiro itojun Hagino 	 * reject packets with the following address:
650686cdd19SJun-ichiro itojun Hagino 	 * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
651686cdd19SJun-ichiro itojun Hagino 	 */
6528d95b0ceSSUZUKI Shinsuke 	if (IN_MULTICAST(ntohl(in->s_addr)))
6533576121cSKristof Provost 		return (-1);
6548d95b0ceSSUZUKI Shinsuke 	switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
6558d95b0ceSSUZUKI Shinsuke 	case 0: case 127: case 255:
6563576121cSKristof Provost 		return (-1);
6578d95b0ceSSUZUKI Shinsuke 	}
658686cdd19SJun-ichiro itojun Hagino 
659686cdd19SJun-ichiro itojun Hagino 	/*
660686cdd19SJun-ichiro itojun Hagino 	 * reject packets with broadcast
661686cdd19SJun-ichiro itojun Hagino 	 */
662d7c5a620SMatt Macy 	CK_STAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
663686cdd19SJun-ichiro itojun Hagino 		if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
664686cdd19SJun-ichiro itojun Hagino 			continue;
6652d9cfabaSRobert Watson 		if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
6663576121cSKristof Provost 			return (-1);
667686cdd19SJun-ichiro itojun Hagino 		}
6682d9cfabaSRobert Watson 	}
669686cdd19SJun-ichiro itojun Hagino 
670686cdd19SJun-ichiro itojun Hagino 	/*
671686cdd19SJun-ichiro itojun Hagino 	 * perform ingress filter
672686cdd19SJun-ichiro itojun Hagino 	 */
673fc74a9f9SBrooks Davis 	if (sc && (STF2IFP(sc)->if_flags & IFF_LINK2) == 0 && inifp) {
6746ad7446cSAlexander V. Chernikov 		struct nhop_object *nh;
675686cdd19SJun-ichiro itojun Hagino 
6766ad7446cSAlexander V. Chernikov 		NET_EPOCH_ASSERT();
6776ad7446cSAlexander V. Chernikov 		nh = fib4_lookup(sc->sc_fibnum, *in, 0, 0, 0);
6786ad7446cSAlexander V. Chernikov 		if (nh == NULL)
6790792bcbbSAlexander V. Chernikov 			return (-1);
6800792bcbbSAlexander V. Chernikov 
6816ad7446cSAlexander V. Chernikov 		if (nh->nh_ifp != inifp)
6820792bcbbSAlexander V. Chernikov 			return (-1);
683686cdd19SJun-ichiro itojun Hagino 	}
684686cdd19SJun-ichiro itojun Hagino 
6853576121cSKristof Provost 	return (0);
686686cdd19SJun-ichiro itojun Hagino }
687686cdd19SJun-ichiro itojun Hagino 
688686cdd19SJun-ichiro itojun Hagino static int
689926381e1SAndrey V. Elsukov stf_checkaddr6(struct stf_softc *sc, struct in6_addr *in6, struct ifnet *inifp)
690686cdd19SJun-ichiro itojun Hagino {
691b46512f7SKristof Provost 	SDT_PROBE3(if_stf, , checkaddr6, in, sc, in6, inifp);
692b46512f7SKristof Provost 
693686cdd19SJun-ichiro itojun Hagino 	/*
694686cdd19SJun-ichiro itojun Hagino 	 * check 6to4 addresses
695686cdd19SJun-ichiro itojun Hagino 	 */
6968d95b0ceSSUZUKI Shinsuke 	if (IN6_IS_ADDR_6TO4(in6)) {
6978d95b0ceSSUZUKI Shinsuke 		struct in_addr in4;
698b46512f7SKristof Provost 		int ret;
699b46512f7SKristof Provost 
7008d95b0ceSSUZUKI Shinsuke 		bcopy(GET_V4(in6), &in4, sizeof(in4));
701b46512f7SKristof Provost 		ret = stf_checkaddr4(sc, &in4, inifp);
702b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , checkaddr6, out, ret, __LINE__);
703b46512f7SKristof Provost 		return (ret);
7048d95b0ceSSUZUKI Shinsuke 	}
705686cdd19SJun-ichiro itojun Hagino 
706686cdd19SJun-ichiro itojun Hagino 	/*
707686cdd19SJun-ichiro itojun Hagino 	 * reject anything that look suspicious.  the test is implemented
708686cdd19SJun-ichiro itojun Hagino 	 * in ip6_input too, but we check here as well to
709686cdd19SJun-ichiro itojun Hagino 	 * (1) reject bad packets earlier, and
710686cdd19SJun-ichiro itojun Hagino 	 * (2) to be safe against future ip6_input change.
711686cdd19SJun-ichiro itojun Hagino 	 */
712b46512f7SKristof Provost 	if (IN6_IS_ADDR_V4COMPAT(in6)) {
713b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , checkaddr6, out, -1, __LINE__);
7143576121cSKristof Provost 		return (-1);
715b46512f7SKristof Provost 	}
716686cdd19SJun-ichiro itojun Hagino 
717b46512f7SKristof Provost 	if (IN6_IS_ADDR_V4MAPPED(in6)) {
718b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , checkaddr6, out, -1, __LINE__);
719b46512f7SKristof Provost 		return (-1);
720b46512f7SKristof Provost 	}
721b46512f7SKristof Provost 
722b46512f7SKristof Provost 	SDT_PROBE2(if_stf, , checkaddr6, out, 0, __LINE__);
7233576121cSKristof Provost 	return (0);
724686cdd19SJun-ichiro itojun Hagino }
725686cdd19SJun-ichiro itojun Hagino 
72606cd035aSAndrey V. Elsukov static int
7276d8fdfa9SAndrey V. Elsukov in_stf_input(struct mbuf *m, int off, int proto, void *arg)
728686cdd19SJun-ichiro itojun Hagino {
7296d8fdfa9SAndrey V. Elsukov 	struct stf_softc *sc = arg;
730*439da7f0SKristof Provost 	struct ip ip;
731686cdd19SJun-ichiro itojun Hagino 	struct ip6_hdr *ip6;
732686cdd19SJun-ichiro itojun Hagino 	u_int8_t otos, itos;
733686cdd19SJun-ichiro itojun Hagino 	struct ifnet *ifp;
73419dc6445SKristof Provost 	struct nhop_object *nh;
735686cdd19SJun-ichiro itojun Hagino 
736b8a6e03fSGleb Smirnoff 	NET_EPOCH_ASSERT();
737b8a6e03fSGleb Smirnoff 
738b46512f7SKristof Provost 	SDT_PROBE3(if_stf, , stf_input, in, m, off, proto);
739b46512f7SKristof Provost 
740686cdd19SJun-ichiro itojun Hagino 	if (proto != IPPROTO_IPV6) {
741686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
742b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
7438f5a8818SKevin Lo 		return (IPPROTO_DONE);
744686cdd19SJun-ichiro itojun Hagino 	}
745686cdd19SJun-ichiro itojun Hagino 
746*439da7f0SKristof Provost 	m_copydata(m, 0, sizeof(struct ip), (caddr_t)&ip);
747fc74a9f9SBrooks Davis 	if (sc == NULL || (STF2IFP(sc)->if_flags & IFF_UP) == 0) {
748686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
749b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
7508f5a8818SKevin Lo 		return (IPPROTO_DONE);
751686cdd19SJun-ichiro itojun Hagino 	}
752686cdd19SJun-ichiro itojun Hagino 
753fc74a9f9SBrooks Davis 	ifp = STF2IFP(sc);
754686cdd19SJun-ichiro itojun Hagino 
7556b459e49SRobert Watson #ifdef MAC
75630d239bcSRobert Watson 	mac_ifnet_create_mbuf(ifp, m);
7576b459e49SRobert Watson #endif
7586b459e49SRobert Watson 
759686cdd19SJun-ichiro itojun Hagino 	/*
760686cdd19SJun-ichiro itojun Hagino 	 * perform sanity check against outer src/dst.
761686cdd19SJun-ichiro itojun Hagino 	 * for source, perform ingress filter as well.
762686cdd19SJun-ichiro itojun Hagino 	 */
763*439da7f0SKristof Provost 	if (stf_checkaddr4(sc, &ip.ip_dst, NULL) < 0 ||
764*439da7f0SKristof Provost 	    stf_checkaddr4(sc, &ip.ip_src, m->m_pkthdr.rcvif) < 0) {
765686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
766b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
7678f5a8818SKevin Lo 		return (IPPROTO_DONE);
768686cdd19SJun-ichiro itojun Hagino 	}
769686cdd19SJun-ichiro itojun Hagino 
770*439da7f0SKristof Provost 	otos = ip.ip_tos;
771686cdd19SJun-ichiro itojun Hagino 	m_adj(m, off);
772686cdd19SJun-ichiro itojun Hagino 
773686cdd19SJun-ichiro itojun Hagino 	if (m->m_len < sizeof(*ip6)) {
774686cdd19SJun-ichiro itojun Hagino 		m = m_pullup(m, sizeof(*ip6));
775b46512f7SKristof Provost 		if (!m) {
776b46512f7SKristof Provost 			SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE,
777b46512f7SKristof Provost 			    __LINE__);
7788f5a8818SKevin Lo 			return (IPPROTO_DONE);
779686cdd19SJun-ichiro itojun Hagino 		}
780b46512f7SKristof Provost 	}
781686cdd19SJun-ichiro itojun Hagino 	ip6 = mtod(m, struct ip6_hdr *);
782686cdd19SJun-ichiro itojun Hagino 
783686cdd19SJun-ichiro itojun Hagino 	/*
784686cdd19SJun-ichiro itojun Hagino 	 * perform sanity check against inner src/dst.
785686cdd19SJun-ichiro itojun Hagino 	 * for source, perform ingress filter as well.
786686cdd19SJun-ichiro itojun Hagino 	 */
78733841545SHajimu UMEMOTO 	if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
78833841545SHajimu UMEMOTO 	    stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
789686cdd19SJun-ichiro itojun Hagino 		m_freem(m);
790b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
7918f5a8818SKevin Lo 		return (IPPROTO_DONE);
792686cdd19SJun-ichiro itojun Hagino 	}
793686cdd19SJun-ichiro itojun Hagino 
79419dc6445SKristof Provost 	/*
79519dc6445SKristof Provost 	 * reject packets with private address range.
79619dc6445SKristof Provost 	 * (requirement from RFC3056 section 2 1st paragraph)
79719dc6445SKristof Provost 	 */
798*439da7f0SKristof Provost 	if ((IN6_IS_ADDR_6TO4(&ip6->ip6_src) && isrfc1918addr(&ip.ip_src)) ||
799*439da7f0SKristof Provost 	    (IN6_IS_ADDR_6TO4(&ip6->ip6_dst) && isrfc1918addr(&ip.ip_dst))) {
80019dc6445SKristof Provost 		m_freem(m);
801b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
80219dc6445SKristof Provost 		return (IPPROTO_DONE);
80319dc6445SKristof Provost 	}
80419dc6445SKristof Provost 
80519dc6445SKristof Provost 	/*
80619dc6445SKristof Provost 	 * Ignore if the destination is the same stf interface because
80719dc6445SKristof Provost 	 * all of valid IPv6 outgoing traffic should go interfaces
80819dc6445SKristof Provost 	 * except for it.
80919dc6445SKristof Provost 	 */
81019dc6445SKristof Provost 	nh = fib6_lookup(sc->sc_fibnum, &ip6->ip6_dst, 0, 0, 0);
81119dc6445SKristof Provost 	if (nh == NULL) {
81219dc6445SKristof Provost 		m_free(m);
813b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
81419dc6445SKristof Provost 		return (IPPROTO_DONE);
81519dc6445SKristof Provost 	}
81619dc6445SKristof Provost 	if ((nh->nh_ifp == ifp) &&
81719dc6445SKristof Provost 	    (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &nh->gw6_sa.sin6_addr))) {
81819dc6445SKristof Provost 		m_free(m);
819b46512f7SKristof Provost 		SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
82019dc6445SKristof Provost 		return (IPPROTO_DONE);
82119dc6445SKristof Provost 	}
82219dc6445SKristof Provost 
823bb4a7d94SKristof Provost 	itos = IPV6_TRAFFIC_CLASS(ip6);
824686cdd19SJun-ichiro itojun Hagino 	if ((ifp->if_flags & IFF_LINK1) != 0)
825686cdd19SJun-ichiro itojun Hagino 		ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
82633841545SHajimu UMEMOTO 	else
82733841545SHajimu UMEMOTO 		ip_ecn_egress(ECN_NOCARE, &otos, &itos);
828686cdd19SJun-ichiro itojun Hagino 	ip6->ip6_flow &= ~htonl(0xff << 20);
829686cdd19SJun-ichiro itojun Hagino 	ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
830686cdd19SJun-ichiro itojun Hagino 
831686cdd19SJun-ichiro itojun Hagino 	m->m_pkthdr.rcvif = ifp;
832686cdd19SJun-ichiro itojun Hagino 
83316d878ccSChristian S.J. Peron 	if (bpf_peers_present(ifp->if_bpf)) {
834686cdd19SJun-ichiro itojun Hagino 		/*
835686cdd19SJun-ichiro itojun Hagino 		 * We need to prepend the address family as
836686cdd19SJun-ichiro itojun Hagino 		 * a four byte field.  Cons up a dummy header
837686cdd19SJun-ichiro itojun Hagino 		 * to pacify bpf.  This is safe because bpf
838686cdd19SJun-ichiro itojun Hagino 		 * will only read from the mbuf (i.e., it won't
839686cdd19SJun-ichiro itojun Hagino 		 * try to free it or keep a pointer a to it).
840686cdd19SJun-ichiro itojun Hagino 		 */
84133841545SHajimu UMEMOTO 		u_int32_t af = AF_INET6;
8429b1a7076SHajimu UMEMOTO 		bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
843686cdd19SJun-ichiro itojun Hagino 	}
844686cdd19SJun-ichiro itojun Hagino 
845686cdd19SJun-ichiro itojun Hagino 	/*
846686cdd19SJun-ichiro itojun Hagino 	 * Put the packet to the network layer input queue according to the
847686cdd19SJun-ichiro itojun Hagino 	 * specified address family.
848686cdd19SJun-ichiro itojun Hagino 	 * See net/if_gif.c for possible issues with packet processing
849686cdd19SJun-ichiro itojun Hagino 	 * reorder due to extra queueing.
850686cdd19SJun-ichiro itojun Hagino 	 */
8513751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
8523751dddbSGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
853a34c6aebSBjoern A. Zeeb 	M_SETFIB(m, ifp->if_fib);
8541cafed39SJonathan Lemon 	netisr_dispatch(NETISR_IPV6, m);
855b46512f7SKristof Provost 	SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
8568f5a8818SKevin Lo 	return (IPPROTO_DONE);
857686cdd19SJun-ichiro itojun Hagino }
858686cdd19SJun-ichiro itojun Hagino 
85919dc6445SKristof Provost static struct sockaddr_in *
86019dc6445SKristof Provost stf_getin4addr_in6(struct stf_softc *sc, struct sockaddr_in *sin,
86119dc6445SKristof Provost     struct in6_addr addr6, struct in6_addr mask6, struct in6_addr in6)
86219dc6445SKristof Provost {
86319dc6445SKristof Provost        int i;
864b46512f7SKristof Provost        struct sockaddr_in *out;
86519dc6445SKristof Provost 
86619dc6445SKristof Provost 	/*
86719dc6445SKristof Provost 	* When (src addr & src mask) != (in6 & src mask),
86819dc6445SKristof Provost 	* the dst is not in the 6rd domain.  The IPv4 address must
86919dc6445SKristof Provost 	* not be used.
87019dc6445SKristof Provost 	*/
87119dc6445SKristof Provost 	for (i = 0; i < sizeof(addr6); i++) {
87219dc6445SKristof Provost 		if ((((u_char *)&addr6)[i] & ((u_char *)&mask6)[i]) !=
873b46512f7SKristof Provost 		    (((u_char *)&in6)[i] & ((u_char *)&mask6)[i])) {
874b46512f7SKristof Provost 			SDT_PROBE4(if_stf, , getin4addr_in6, out, &addr6,
875b46512f7SKristof Provost 			    &mask6, &in6, NULL);
87619dc6445SKristof Provost 			return (NULL);
87719dc6445SKristof Provost 		}
878b46512f7SKristof Provost 	}
87919dc6445SKristof Provost 
88019dc6445SKristof Provost 	/* After the mask check, use in6 instead of addr6. */
881b46512f7SKristof Provost 	out = stf_getin4addr(sc, sin, in6, mask6);
882b46512f7SKristof Provost 	SDT_PROBE4(if_stf, , getin4addr_in6, out, &addr6, &mask6, &in6, out);
883b46512f7SKristof Provost 	return (out);
88419dc6445SKristof Provost }
88519dc6445SKristof Provost 
88619dc6445SKristof Provost static struct sockaddr_in *
88719dc6445SKristof Provost stf_getin4addr(struct stf_softc *sc, struct sockaddr_in *sin,
88819dc6445SKristof Provost     struct in6_addr addr6, struct in6_addr mask6)
88919dc6445SKristof Provost {
89019dc6445SKristof Provost 	struct in_addr *in;
89119dc6445SKristof Provost 
892b46512f7SKristof Provost 	SDT_PROBE2(if_stf, , getin4addr, in, &addr6, &mask6);
893b46512f7SKristof Provost 
89419dc6445SKristof Provost 	memset(sin, 0, sizeof(*sin));
89519dc6445SKristof Provost 	in = &sin->sin_addr;
89619dc6445SKristof Provost 	if (IN6_IS_ADDR_6TO4(&addr6)) {
89719dc6445SKristof Provost 		/* 6to4 (RFC 3056) */
89819dc6445SKristof Provost 		bcopy(GET_V4(&addr6), in, sizeof(*in));
89919dc6445SKristof Provost 		if (isrfc1918addr(in))
90019dc6445SKristof Provost 			return (NULL);
90119dc6445SKristof Provost 	} else {
90219dc6445SKristof Provost 		/* 6rd (RFC 5569) */
90319dc6445SKristof Provost 		in_addr_t v4prefix;
90419dc6445SKristof Provost 		uint8_t *v6 = (uint8_t*)&addr6;
90519dc6445SKristof Provost 		uint64_t v6prefix;
90619dc6445SKristof Provost 		u_int plen;
90719dc6445SKristof Provost 		u_int v4suffixlen;
90819dc6445SKristof Provost 
90919dc6445SKristof Provost 		v4prefix = 0;
91019dc6445SKristof Provost 		if (sc->v4prefixlen < 32) {
91119dc6445SKristof Provost 			v4suffixlen = 32 - sc->v4prefixlen;
91219dc6445SKristof Provost 			v4prefix = ntohl(sc->srcv4_addr) &
91319dc6445SKristof Provost 			    (0xffffffffU << v4suffixlen);
91419dc6445SKristof Provost 		} else {
91519dc6445SKristof Provost 			MPASS(sc->v4prefixlen == 32);
91619dc6445SKristof Provost 			v4suffixlen = 32;
91719dc6445SKristof Provost 		}
91819dc6445SKristof Provost 
91919dc6445SKristof Provost 		plen = in6_mask2len(&mask6, NULL);
92019dc6445SKristof Provost 		if (plen > 64)
92119dc6445SKristof Provost 			return (NULL);
92219dc6445SKristof Provost 
92319dc6445SKristof Provost 		/* To make this simple we do not support prefixes longer than
92419dc6445SKristof Provost 		 * 64 bits. RFC5969 says "a 6rd delegated prefix SHOULD be /64
92519dc6445SKristof Provost 		 * or shorter." so this is a moderately safe assumption. */
92619dc6445SKristof Provost 		v6prefix = be64toh(*(uint64_t *)v6);
92719dc6445SKristof Provost 
92819dc6445SKristof Provost 		/* Shift away the v6 prefix itself. */
92919dc6445SKristof Provost 		v6prefix <<= plen;
93019dc6445SKristof Provost 		v6prefix >>= plen;
93119dc6445SKristof Provost 
93219dc6445SKristof Provost 		/* Now shift away everything after the v4 address. */
93319dc6445SKristof Provost 		v6prefix >>= 64 - plen - v4suffixlen;
93419dc6445SKristof Provost 
93519dc6445SKristof Provost 		sin->sin_addr.s_addr = htonl(v4prefix | (uint32_t)v6prefix);
93619dc6445SKristof Provost 	}
93719dc6445SKristof Provost 
938b46512f7SKristof Provost 	SDT_PROBE1(if_stf, , getin4addr, out, sin);
939b46512f7SKristof Provost 
94019dc6445SKristof Provost 	return (sin);
94119dc6445SKristof Provost }
94219dc6445SKristof Provost 
943686cdd19SJun-ichiro itojun Hagino static int
944926381e1SAndrey V. Elsukov stf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
945686cdd19SJun-ichiro itojun Hagino {
946686cdd19SJun-ichiro itojun Hagino 	struct ifaddr *ifa;
94719dc6445SKristof Provost 	struct ifdrv *ifd;
948686cdd19SJun-ichiro itojun Hagino 	struct ifreq *ifr;
94919dc6445SKristof Provost 	struct sockaddr_in sin4;
95019dc6445SKristof Provost 	struct stf_softc *sc_cur;
95119dc6445SKristof Provost 	struct stfv4args args;
95299ab4b12SAlexander V. Chernikov 	int error, mtu;
953686cdd19SJun-ichiro itojun Hagino 
954686cdd19SJun-ichiro itojun Hagino 	error = 0;
95519dc6445SKristof Provost 	sc_cur = ifp->if_softc;
95619dc6445SKristof Provost 
957686cdd19SJun-ichiro itojun Hagino 	switch (cmd) {
95819dc6445SKristof Provost 	case SIOCSDRVSPEC:
95919dc6445SKristof Provost 		ifd = (struct ifdrv *)data;
96019dc6445SKristof Provost 		error = priv_check(curthread, PRIV_NET_ADDIFADDR);
96119dc6445SKristof Provost 		if (error)
96219dc6445SKristof Provost 			break;
96319dc6445SKristof Provost 		if (ifd->ifd_cmd == STF6RD_SV4NET) {
96419dc6445SKristof Provost 			if (ifd->ifd_len != sizeof(args)) {
96519dc6445SKristof Provost 				error = EINVAL;
96619dc6445SKristof Provost 				break;
96719dc6445SKristof Provost 			}
96819dc6445SKristof Provost 			bzero(&args, sizeof(args));
96919dc6445SKristof Provost 			error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
97019dc6445SKristof Provost 			if (error)
97119dc6445SKristof Provost 				break;
97219dc6445SKristof Provost 
97319dc6445SKristof Provost 			if (args.v4_prefixlen < 1 || args.v4_prefixlen > 32) {
97419dc6445SKristof Provost 				error = EINVAL;
97519dc6445SKristof Provost 				break;
97619dc6445SKristof Provost 			}
97719dc6445SKristof Provost 
97819dc6445SKristof Provost 			bcopy(&args.srcv4_addr, &sc_cur->srcv4_addr,
97919dc6445SKristof Provost 			    sizeof(sc_cur->srcv4_addr));
98019dc6445SKristof Provost 			sc_cur->v4prefixlen = args.v4_prefixlen;
981b46512f7SKristof Provost 			SDT_PROBE3(if_stf, , ioctl, sv4net, sc_cur->srcv4_addr,
982b46512f7SKristof Provost 			    sc_cur->srcv4_addr, sc_cur->v4prefixlen);
98319dc6445SKristof Provost 		} else if (ifd->ifd_cmd == STF6RD_SBR) {
98419dc6445SKristof Provost 			if (ifd->ifd_len != sizeof(args)) {
98519dc6445SKristof Provost 				error = EINVAL;
98619dc6445SKristof Provost 				break;
98719dc6445SKristof Provost 			}
98819dc6445SKristof Provost 			bzero(&args, sizeof(args));
98919dc6445SKristof Provost 			error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
99019dc6445SKristof Provost 			if (error)
99119dc6445SKristof Provost 				break;
99219dc6445SKristof Provost 			sc_cur->braddr = args.braddr.s_addr;
993b46512f7SKristof Provost 			SDT_PROBE1(if_stf, , ioctl, sdstv4,
994b46512f7SKristof Provost 			    sc_cur->braddr);
99519dc6445SKristof Provost 		} else
99619dc6445SKristof Provost 			error = EINVAL;
99719dc6445SKristof Provost 		break;
99819dc6445SKristof Provost 	case SIOCGDRVSPEC:
99919dc6445SKristof Provost 		ifd = (struct ifdrv *)data;
100019dc6445SKristof Provost 		if (ifd->ifd_cmd != STF6RD_GV4NET) {
100119dc6445SKristof Provost 			error = EINVAL;
100219dc6445SKristof Provost 			break;
100319dc6445SKristof Provost 		}
100419dc6445SKristof Provost 		if (ifd->ifd_len != sizeof(args)) {
100519dc6445SKristof Provost 			error = EINVAL;
100619dc6445SKristof Provost 			break;
100719dc6445SKristof Provost 		}
100819dc6445SKristof Provost 		bzero(&args, sizeof(args));
100919dc6445SKristof Provost 		args.srcv4_addr.s_addr = sc_cur->srcv4_addr;
101019dc6445SKristof Provost 		args.braddr.s_addr = sc_cur->braddr;
101119dc6445SKristof Provost 		args.v4_prefixlen = sc_cur->v4prefixlen;
101219dc6445SKristof Provost 		error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
101319dc6445SKristof Provost 		break;
1014686cdd19SJun-ichiro itojun Hagino 	case SIOCSIFADDR:
1015686cdd19SJun-ichiro itojun Hagino 		ifa = (struct ifaddr *)data;
1016b46512f7SKristof Provost 		SDT_PROBE1(if_stf, , ioctl, ifaddr, ifa);
1017686cdd19SJun-ichiro itojun Hagino 		if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
1018686cdd19SJun-ichiro itojun Hagino 			error = EAFNOSUPPORT;
1019686cdd19SJun-ichiro itojun Hagino 			break;
1020686cdd19SJun-ichiro itojun Hagino 		}
102119dc6445SKristof Provost 		if (stf_getin4addr(sc_cur, &sin4,
102219dc6445SKristof Provost 		    satosin6(ifa->ifa_addr)->sin6_addr,
102319dc6445SKristof Provost 		    satosin6(ifa->ifa_netmask)->sin6_addr) == NULL) {
10248d95b0ceSSUZUKI Shinsuke 			error = EINVAL;
10258d95b0ceSSUZUKI Shinsuke 			break;
10268d95b0ceSSUZUKI Shinsuke 		}
1027686cdd19SJun-ichiro itojun Hagino 		ifp->if_flags |= IFF_UP;
1028ca1163bdSMark Johnston 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
1029686cdd19SJun-ichiro itojun Hagino 		break;
1030686cdd19SJun-ichiro itojun Hagino 
1031686cdd19SJun-ichiro itojun Hagino 	case SIOCADDMULTI:
1032686cdd19SJun-ichiro itojun Hagino 	case SIOCDELMULTI:
1033686cdd19SJun-ichiro itojun Hagino 		ifr = (struct ifreq *)data;
1034686cdd19SJun-ichiro itojun Hagino 		if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
1035686cdd19SJun-ichiro itojun Hagino 			;
1036686cdd19SJun-ichiro itojun Hagino 		else
1037686cdd19SJun-ichiro itojun Hagino 			error = EAFNOSUPPORT;
1038686cdd19SJun-ichiro itojun Hagino 		break;
1039686cdd19SJun-ichiro itojun Hagino 
104099ab4b12SAlexander V. Chernikov 	case SIOCGIFMTU:
104199ab4b12SAlexander V. Chernikov 		break;
104299ab4b12SAlexander V. Chernikov 
104399ab4b12SAlexander V. Chernikov 	case SIOCSIFMTU:
104499ab4b12SAlexander V. Chernikov 		ifr = (struct ifreq *)data;
104599ab4b12SAlexander V. Chernikov 		mtu = ifr->ifr_mtu;
104699ab4b12SAlexander V. Chernikov 		/* RFC 4213 3.2 ideal world MTU */
104799ab4b12SAlexander V. Chernikov 		if (mtu < IPV6_MINMTU || mtu > IF_MAXMTU - 20)
104899ab4b12SAlexander V. Chernikov 			return (EINVAL);
104999ab4b12SAlexander V. Chernikov 		ifp->if_mtu = mtu;
105099ab4b12SAlexander V. Chernikov 		break;
105199ab4b12SAlexander V. Chernikov 
1052686cdd19SJun-ichiro itojun Hagino 	default:
1053686cdd19SJun-ichiro itojun Hagino 		error = EINVAL;
1054686cdd19SJun-ichiro itojun Hagino 		break;
1055686cdd19SJun-ichiro itojun Hagino 	}
1056686cdd19SJun-ichiro itojun Hagino 
10573576121cSKristof Provost 	return (error);
1058686cdd19SJun-ichiro itojun Hagino }
1059