188ff5695SSUZUKI Shinsuke /* $KAME: if_stf.c,v 1.73 2001/12/03 11:08:30 keiichi Exp $ */
2686cdd19SJun-ichiro itojun Hagino
3c398230bSWarner Losh /*-
451369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
551369649SPedro F. Giffuni *
6686cdd19SJun-ichiro itojun Hagino * Copyright (C) 2000 WIDE Project.
719dc6445SKristof Provost * Copyright (c) 2010 Hiroki Sato <hrs@FreeBSD.org>
819dc6445SKristof Provost * Copyright (c) 2013 Ermal Luci <eri@FreeBSD.org>
919dc6445SKristof Provost * Copyright (c) 2017-2021 Rubicon Communications, LLC (Netgate)
10686cdd19SJun-ichiro itojun Hagino * All rights reserved.
11686cdd19SJun-ichiro itojun Hagino *
12686cdd19SJun-ichiro itojun Hagino * Redistribution and use in source and binary forms, with or without
13686cdd19SJun-ichiro itojun Hagino * modification, are permitted provided that the following conditions
14686cdd19SJun-ichiro itojun Hagino * are met:
15686cdd19SJun-ichiro itojun Hagino * 1. Redistributions of source code must retain the above copyright
16686cdd19SJun-ichiro itojun Hagino * notice, this list of conditions and the following disclaimer.
17686cdd19SJun-ichiro itojun Hagino * 2. Redistributions in binary form must reproduce the above copyright
18686cdd19SJun-ichiro itojun Hagino * notice, this list of conditions and the following disclaimer in the
19686cdd19SJun-ichiro itojun Hagino * documentation and/or other materials provided with the distribution.
20686cdd19SJun-ichiro itojun Hagino * 3. Neither the name of the project nor the names of its contributors
21686cdd19SJun-ichiro itojun Hagino * may be used to endorse or promote products derived from this software
22686cdd19SJun-ichiro itojun Hagino * without specific prior written permission.
23686cdd19SJun-ichiro itojun Hagino *
24686cdd19SJun-ichiro itojun Hagino * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
25686cdd19SJun-ichiro itojun Hagino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26686cdd19SJun-ichiro itojun Hagino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27686cdd19SJun-ichiro itojun Hagino * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
28686cdd19SJun-ichiro itojun Hagino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29686cdd19SJun-ichiro itojun Hagino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30686cdd19SJun-ichiro itojun Hagino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31686cdd19SJun-ichiro itojun Hagino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32686cdd19SJun-ichiro itojun Hagino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33686cdd19SJun-ichiro itojun Hagino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34686cdd19SJun-ichiro itojun Hagino * SUCH DAMAGE.
35686cdd19SJun-ichiro itojun Hagino */
36686cdd19SJun-ichiro itojun Hagino
37686cdd19SJun-ichiro itojun Hagino /*
3833841545SHajimu UMEMOTO * 6to4 interface, based on RFC3056.
39686cdd19SJun-ichiro itojun Hagino *
40686cdd19SJun-ichiro itojun Hagino * 6to4 interface is NOT capable of link-layer (I mean, IPv4) multicasting.
41686cdd19SJun-ichiro itojun Hagino * There is no address mapping defined from IPv6 multicast address to IPv4
42686cdd19SJun-ichiro itojun Hagino * address. Therefore, we do not have IFF_MULTICAST on the interface.
43686cdd19SJun-ichiro itojun Hagino *
44686cdd19SJun-ichiro itojun Hagino * Due to the lack of address mapping for link-local addresses, we cannot
45686cdd19SJun-ichiro itojun Hagino * throw packets toward link-local addresses (fe80::x). Also, we cannot throw
46686cdd19SJun-ichiro itojun Hagino * packets to link-local multicast addresses (ff02::x).
47686cdd19SJun-ichiro itojun Hagino *
48686cdd19SJun-ichiro itojun Hagino * Here are interesting symptoms due to the lack of link-local address:
49686cdd19SJun-ichiro itojun Hagino *
50686cdd19SJun-ichiro itojun Hagino * Unicast routing exchange:
51686cdd19SJun-ichiro itojun Hagino * - RIPng: Impossible. Uses link-local multicast packet toward ff02::9,
52686cdd19SJun-ichiro itojun Hagino * and link-local addresses as nexthop.
53686cdd19SJun-ichiro itojun Hagino * - OSPFv6: Impossible. OSPFv6 assumes that there's link-local address
54686cdd19SJun-ichiro itojun Hagino * assigned to the link, and makes use of them. Also, HELLO packets use
55686cdd19SJun-ichiro itojun Hagino * link-local multicast addresses (ff02::5 and ff02::6).
56686cdd19SJun-ichiro itojun Hagino * - BGP4+: Maybe. You can only use global address as nexthop, and global
57686cdd19SJun-ichiro itojun Hagino * address as TCP endpoint address.
58686cdd19SJun-ichiro itojun Hagino *
59686cdd19SJun-ichiro itojun Hagino * Multicast routing protocols:
60686cdd19SJun-ichiro itojun Hagino * - PIM: Hello packet cannot be used to discover adjacent PIM routers.
61686cdd19SJun-ichiro itojun Hagino * Adjacent PIM routers must be configured manually (is it really spec-wise
62686cdd19SJun-ichiro itojun Hagino * correct thing to do?).
63686cdd19SJun-ichiro itojun Hagino *
64686cdd19SJun-ichiro itojun Hagino * ICMPv6:
65686cdd19SJun-ichiro itojun Hagino * - Redirects cannot be used due to the lack of link-local address.
66686cdd19SJun-ichiro itojun Hagino *
6733841545SHajimu UMEMOTO * stf interface does not have, and will not need, a link-local address.
6833841545SHajimu UMEMOTO * It seems to have no real benefit and does not help the above symptoms much.
6933841545SHajimu UMEMOTO * Even if we assign link-locals to interface, we cannot really
7033841545SHajimu UMEMOTO * use link-local unicast/multicast on top of 6to4 cloud (since there's no
7133841545SHajimu UMEMOTO * encapsulation defined for link-local address), and the above analysis does
7233841545SHajimu UMEMOTO * not change. RFC3056 does not mandate the assignment of link-local address
7333841545SHajimu UMEMOTO * either.
74686cdd19SJun-ichiro itojun Hagino *
75686cdd19SJun-ichiro itojun Hagino * 6to4 interface has security issues. Refer to
76686cdd19SJun-ichiro itojun Hagino * http://playground.iijlab.net/i-d/draft-itojun-ipv6-transition-abuse-00.txt
77686cdd19SJun-ichiro itojun Hagino * for details. The code tries to filter out some of malicious packets.
78686cdd19SJun-ichiro itojun Hagino * Note that there is no way to be 100% secure.
79686cdd19SJun-ichiro itojun Hagino */
80686cdd19SJun-ichiro itojun Hagino
81686cdd19SJun-ichiro itojun Hagino #include <sys/param.h>
82686cdd19SJun-ichiro itojun Hagino #include <sys/systm.h>
83686cdd19SJun-ichiro itojun Hagino #include <sys/socket.h>
84686cdd19SJun-ichiro itojun Hagino #include <sys/sockio.h>
85686cdd19SJun-ichiro itojun Hagino #include <sys/mbuf.h>
8619dc6445SKristof Provost #include <sys/endian.h>
87686cdd19SJun-ichiro itojun Hagino #include <sys/errno.h>
88686cdd19SJun-ichiro itojun Hagino #include <sys/kernel.h>
89cc0a3c8cSAndrey V. Elsukov #include <sys/lock.h>
905dba30f1SPoul-Henning Kamp #include <sys/module.h>
9119dc6445SKristof Provost #include <sys/priv.h>
928b07e49aSJulian Elischer #include <sys/proc.h>
93abb64706SBrooks Davis #include <sys/queue.h>
94b46512f7SKristof Provost #include <sys/sdt.h>
950dae32f2SDavid Malone #include <sys/sysctl.h>
96686cdd19SJun-ichiro itojun Hagino #include <machine/cpu.h>
97686cdd19SJun-ichiro itojun Hagino
98686cdd19SJun-ichiro itojun Hagino #include <sys/malloc.h>
99686cdd19SJun-ichiro itojun Hagino
100686cdd19SJun-ichiro itojun Hagino #include <net/if.h>
10176039bc8SGleb Smirnoff #include <net/if_var.h>
102*2c2b37adSJustin Hibbits #include <net/if_private.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
2178e45fed3SKristof Provost VNET_DEFINE_STATIC(struct if_clone *, stf_cloner);
2188e45fed3SKristof Provost #define V_stf_cloner VNET(stf_cloner)
219abb64706SBrooks Davis
2206d8fdfa9SAndrey V. Elsukov static const struct encap_config ipv4_encap_cfg = {
2216d8fdfa9SAndrey V. Elsukov .proto = IPPROTO_IPV6,
2226d8fdfa9SAndrey V. Elsukov .min_length = sizeof(struct ip),
2236d8fdfa9SAndrey V. Elsukov .exact_match = (sizeof(in_addr_t) << 3) + 8,
2246d8fdfa9SAndrey V. Elsukov .check = stf_encapcheck,
2256d8fdfa9SAndrey V. Elsukov .input = in_stf_input
2266d8fdfa9SAndrey V. Elsukov };
2276d8fdfa9SAndrey V. Elsukov
228bb2bfb4fSBrooks Davis static int
stf_clone_match(struct if_clone * ifc,const char * name)229f889d2efSBrooks Davis stf_clone_match(struct if_clone *ifc, const char *name)
230686cdd19SJun-ichiro itojun Hagino {
231f889d2efSBrooks Davis int i;
232f889d2efSBrooks Davis
233f889d2efSBrooks Davis for(i = 0; stfnames[i] != NULL; i++) {
234f889d2efSBrooks Davis if (strcmp(stfnames[i], name) == 0)
235f889d2efSBrooks Davis return (1);
236f889d2efSBrooks Davis }
237f889d2efSBrooks Davis
238f889d2efSBrooks Davis return (0);
239f889d2efSBrooks Davis }
240f889d2efSBrooks Davis
241f889d2efSBrooks Davis static int
stf_clone_create(struct if_clone * ifc,char * name,size_t len,struct ifc_data * ifd,struct ifnet ** ifpp)24291ebcbe0SAlexander V. Chernikov stf_clone_create(struct if_clone *ifc, char *name, size_t len,
24391ebcbe0SAlexander V. Chernikov struct ifc_data *ifd, struct ifnet **ifpp)
244f889d2efSBrooks Davis {
245d177868cSLuiz Otavio O Souza char *dp;
246d177868cSLuiz Otavio O Souza int err, unit, wildcard;
247686cdd19SJun-ichiro itojun Hagino struct stf_softc *sc;
2481861b710SBrooks Davis struct ifnet *ifp;
249686cdd19SJun-ichiro itojun Hagino
250d177868cSLuiz Otavio O Souza err = ifc_name2unit(name, &unit);
251d177868cSLuiz Otavio O Souza if (err != 0)
252d177868cSLuiz Otavio O Souza return (err);
253d177868cSLuiz Otavio O Souza wildcard = (unit < 0);
254d177868cSLuiz Otavio O Souza
255f889d2efSBrooks Davis /*
256f889d2efSBrooks Davis * We can only have one unit, but since unit allocation is
257f889d2efSBrooks Davis * already locked, we use it to keep from allocating extra
258f889d2efSBrooks Davis * interfaces.
259f889d2efSBrooks Davis */
260f889d2efSBrooks Davis unit = STFUNIT;
261f889d2efSBrooks Davis err = ifc_alloc_unit(ifc, &unit);
262f889d2efSBrooks Davis if (err != 0)
263f889d2efSBrooks Davis return (err);
264f889d2efSBrooks Davis
265a163d034SWarner Losh sc = malloc(sizeof(struct stf_softc), M_STF, M_WAITOK | M_ZERO);
266b03965ddSBrooks Davis ifp = STF2IFP(sc) = if_alloc(IFT_STF);
267b03965ddSBrooks Davis ifp->if_softc = sc;
2688b07e49aSJulian Elischer sc->sc_fibnum = curthread->td_proc->p_fibnum;
269b03965ddSBrooks Davis
270f889d2efSBrooks Davis /*
271f889d2efSBrooks Davis * Set the name manually rather then using if_initname because
272f889d2efSBrooks Davis * we don't conform to the default naming convention for interfaces.
273d177868cSLuiz Otavio O Souza * In the wildcard case, we need to update the name.
274f889d2efSBrooks Davis */
275d177868cSLuiz Otavio O Souza if (wildcard) {
276d177868cSLuiz Otavio O Souza for (dp = name; *dp != '\0'; dp++);
277d177868cSLuiz Otavio O Souza if (snprintf(dp, len - (dp-name), "%d", unit) >
278d177868cSLuiz Otavio O Souza len - (dp-name) - 1) {
279d177868cSLuiz Otavio O Souza /*
280d177868cSLuiz Otavio O Souza * This can only be a programmer error and
281d177868cSLuiz Otavio O Souza * there's no straightforward way to recover if
282d177868cSLuiz Otavio O Souza * it happens.
283d177868cSLuiz Otavio O Souza */
284d177868cSLuiz Otavio O Souza panic("if_clone_create(): interface name too long");
285d177868cSLuiz Otavio O Souza }
286d177868cSLuiz Otavio O Souza }
287f889d2efSBrooks Davis strlcpy(ifp->if_xname, name, IFNAMSIZ);
28842a58907SGleb Smirnoff ifp->if_dname = stfname;
289f889d2efSBrooks Davis ifp->if_dunit = IF_DUNIT_NONE;
290abb64706SBrooks Davis
2916d8fdfa9SAndrey V. Elsukov sc->encap_cookie = ip_encap_attach(&ipv4_encap_cfg, sc, M_WAITOK);
292686cdd19SJun-ichiro itojun Hagino
2931861b710SBrooks Davis ifp->if_mtu = IPV6_MMTU;
2941861b710SBrooks Davis ifp->if_ioctl = stf_ioctl;
2951861b710SBrooks Davis ifp->if_output = stf_output;
296e50d35e6SMaxim Sobolev ifp->if_snd.ifq_maxlen = ifqmaxlen;
2971861b710SBrooks Davis if_attach(ifp);
29801399f34SDavid Malone bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
29991ebcbe0SAlexander V. Chernikov *ifpp = ifp;
30091ebcbe0SAlexander V. Chernikov
301abb64706SBrooks Davis return (0);
302abb64706SBrooks Davis }
303abb64706SBrooks Davis
304f889d2efSBrooks Davis static int
stf_clone_destroy(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)30591ebcbe0SAlexander V. Chernikov stf_clone_destroy(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
306abb64706SBrooks Davis {
307fc74a9f9SBrooks Davis struct stf_softc *sc = ifp->if_softc;
30846d0f824SMatt Macy int err __unused;
309abb64706SBrooks Davis
3106d8fdfa9SAndrey V. Elsukov err = ip_encap_detach(sc->encap_cookie);
311febd0759SAndrew Thompson KASSERT(err == 0, ("Unexpected error detaching encap_cookie"));
312febd0759SAndrew Thompson bpfdetach(ifp);
313febd0759SAndrew Thompson if_detach(ifp);
314febd0759SAndrew Thompson if_free(ifp);
315febd0759SAndrew Thompson
316febd0759SAndrew Thompson free(sc, M_STF);
317f889d2efSBrooks Davis ifc_free_unit(ifc, STFUNIT);
318f889d2efSBrooks Davis
319f889d2efSBrooks Davis return (0);
320abb64706SBrooks Davis }
321abb64706SBrooks Davis
3228e45fed3SKristof Provost static void
vnet_stf_init(const void * unused __unused)3238e45fed3SKristof Provost vnet_stf_init(const void *unused __unused)
3248e45fed3SKristof Provost {
32591ebcbe0SAlexander V. Chernikov struct if_clone_addreq req = {
32691ebcbe0SAlexander V. Chernikov .match_f = stf_clone_match,
32791ebcbe0SAlexander V. Chernikov .create_f = stf_clone_create,
32891ebcbe0SAlexander V. Chernikov .destroy_f = stf_clone_destroy,
32991ebcbe0SAlexander V. Chernikov };
33091ebcbe0SAlexander V. Chernikov V_stf_cloner = ifc_attach_cloner(stfname, &req);
3318e45fed3SKristof Provost }
3328e45fed3SKristof Provost VNET_SYSINIT(vnet_stf_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_stf_init, NULL);
3338e45fed3SKristof Provost
3348e45fed3SKristof Provost static void
vnet_stf_uninit(const void * unused __unused)3358e45fed3SKristof Provost vnet_stf_uninit(const void *unused __unused)
3368e45fed3SKristof Provost {
3378e45fed3SKristof Provost if_clone_detach(V_stf_cloner);
3388e45fed3SKristof Provost V_stf_cloner = NULL;
3398e45fed3SKristof Provost }
3408e45fed3SKristof Provost VNET_SYSUNINIT(vnet_stf_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_stf_uninit,
3418e45fed3SKristof Provost NULL);
3428e45fed3SKristof Provost
343abb64706SBrooks Davis static int
stfmodevent(module_t mod,int type,void * data)344926381e1SAndrey V. Elsukov stfmodevent(module_t mod, int type, void *data)
345abb64706SBrooks Davis {
346abb64706SBrooks Davis
347abb64706SBrooks Davis switch (type) {
348abb64706SBrooks Davis case MOD_LOAD:
3498e45fed3SKristof Provost /* Done in vnet_stf_init() */
350abb64706SBrooks Davis break;
351abb64706SBrooks Davis case MOD_UNLOAD:
3528e45fed3SKristof Provost /* Done in vnet_stf_uninit() */
35353dab5feSBrooks Davis break;
3543e019deaSPoul-Henning Kamp default:
3553e019deaSPoul-Henning Kamp return (EOPNOTSUPP);
356686cdd19SJun-ichiro itojun Hagino }
357686cdd19SJun-ichiro itojun Hagino
35853dab5feSBrooks Davis return (0);
35953dab5feSBrooks Davis }
36053dab5feSBrooks Davis
36153dab5feSBrooks Davis static moduledata_t stf_mod = {
36253dab5feSBrooks Davis "if_stf",
36353dab5feSBrooks Davis stfmodevent,
3649823d527SKevin Lo 0
36553dab5feSBrooks Davis };
36653dab5feSBrooks Davis
36753dab5feSBrooks Davis DECLARE_MODULE(if_stf, stf_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
36819dc6445SKristof Provost MODULE_VERSION(if_stf, 2);
369686cdd19SJun-ichiro itojun Hagino
370686cdd19SJun-ichiro itojun Hagino static int
stf_encapcheck(const struct mbuf * m,int off,int proto,void * arg)371926381e1SAndrey V. Elsukov stf_encapcheck(const struct mbuf *m, int off, int proto, void *arg)
372686cdd19SJun-ichiro itojun Hagino {
373686cdd19SJun-ichiro itojun Hagino struct ip ip;
374686cdd19SJun-ichiro itojun Hagino struct stf_softc *sc;
375d74b9a2cSAlexander V. Chernikov struct in6_addr addr6, mask6;
37619dc6445SKristof Provost struct sockaddr_in sin4addr, sin4mask;
377686cdd19SJun-ichiro itojun Hagino
378b46512f7SKristof Provost SDT_PROBE3(if_stf, , encapcheck, in, m, off, proto);
379b46512f7SKristof Provost
380686cdd19SJun-ichiro itojun Hagino sc = (struct stf_softc *)arg;
381686cdd19SJun-ichiro itojun Hagino if (sc == NULL)
3823576121cSKristof Provost return (0);
383686cdd19SJun-ichiro itojun Hagino
384fc74a9f9SBrooks Davis if ((STF2IFP(sc)->if_flags & IFF_UP) == 0)
3853576121cSKristof Provost return (0);
386686cdd19SJun-ichiro itojun Hagino
38733841545SHajimu UMEMOTO /* IFF_LINK0 means "no decapsulation" */
388fc74a9f9SBrooks Davis if ((STF2IFP(sc)->if_flags & IFF_LINK0) != 0)
3893576121cSKristof Provost return (0);
39033841545SHajimu UMEMOTO
391686cdd19SJun-ichiro itojun Hagino if (proto != IPPROTO_IPV6)
3923576121cSKristof Provost return (0);
393686cdd19SJun-ichiro itojun Hagino
394350e6227SAndrey V. Elsukov m_copydata(m, 0, sizeof(ip), (caddr_t)&ip);
395686cdd19SJun-ichiro itojun Hagino
396686cdd19SJun-ichiro itojun Hagino if (ip.ip_v != 4)
3973576121cSKristof Provost return (0);
398686cdd19SJun-ichiro itojun Hagino
399d74b9a2cSAlexander V. Chernikov if (stf_getsrcifa6(STF2IFP(sc), &addr6, &mask6) != 0)
400d74b9a2cSAlexander V. Chernikov return (0);
401686cdd19SJun-ichiro itojun Hagino
40219dc6445SKristof Provost if (sc->srcv4_addr != INADDR_ANY) {
40319dc6445SKristof Provost sin4addr.sin_addr.s_addr = sc->srcv4_addr;
40419dc6445SKristof Provost sin4addr.sin_family = AF_INET;
40519dc6445SKristof Provost } else
40619dc6445SKristof Provost if (stf_getin4addr(sc, &sin4addr, addr6, mask6) == NULL)
4073576121cSKristof Provost return (0);
408686cdd19SJun-ichiro itojun Hagino
40919dc6445SKristof Provost if (sin4addr.sin_addr.s_addr != ip.ip_dst.s_addr)
41019dc6445SKristof Provost return (0);
41119dc6445SKristof Provost
41219dc6445SKristof Provost if (IN6_IS_ADDR_6TO4(&addr6)) {
413686cdd19SJun-ichiro itojun Hagino /*
41419dc6445SKristof Provost * 6to4 (RFC 3056).
41519dc6445SKristof Provost * Check if IPv4 src matches the IPv4 address derived
41619dc6445SKristof Provost * from the local 6to4 address masked by prefixmask.
417686cdd19SJun-ichiro itojun Hagino * success on: src = 10.1.1.1, ia6->ia_addr = 2002:0a00:.../24
418686cdd19SJun-ichiro itojun Hagino * fail on: src = 10.1.1.1, ia6->ia_addr = 2002:0b00:.../24
419686cdd19SJun-ichiro itojun Hagino */
42019dc6445SKristof Provost memcpy(&sin4mask.sin_addr, GET_V4(&mask6),
42119dc6445SKristof Provost sizeof(sin4mask.sin_addr));
42219dc6445SKristof Provost if ((sin4addr.sin_addr.s_addr & sin4mask.sin_addr.s_addr) !=
42319dc6445SKristof Provost (ip.ip_src.s_addr & sin4mask.sin_addr.s_addr))
4243576121cSKristof Provost return (0);
42519dc6445SKristof Provost } else {
42619dc6445SKristof Provost /* 6rd (RFC 5569) */
42719dc6445SKristof Provost /*
42819dc6445SKristof Provost * No restriction on the src address in the case of
42919dc6445SKristof Provost * 6rd because the stf(4) interface always has a
43019dc6445SKristof Provost * prefix which covers whole of IPv4 src address
43119dc6445SKristof Provost * range. So, stf_output() will catch all of
43219dc6445SKristof Provost * 6rd-capsuled IPv4 traffic with suspicious inner dst
43319dc6445SKristof Provost * IPv4 address (i.e. the IPv6 destination address is
43419dc6445SKristof Provost * one the admin does not like to route to outside),
43519dc6445SKristof Provost * and then it discard them silently.
43619dc6445SKristof Provost */
43719dc6445SKristof Provost }
438686cdd19SJun-ichiro itojun Hagino
439b46512f7SKristof Provost SDT_PROBE0(if_stf, , encapcheck, accept);
440b46512f7SKristof Provost
441686cdd19SJun-ichiro itojun Hagino /* stf interface makes single side match only */
4423576121cSKristof Provost return (32);
443686cdd19SJun-ichiro itojun Hagino }
444686cdd19SJun-ichiro itojun Hagino
445d74b9a2cSAlexander V. Chernikov static int
stf_getsrcifa6(struct ifnet * ifp,struct in6_addr * addr,struct in6_addr * mask)446d74b9a2cSAlexander V. Chernikov stf_getsrcifa6(struct ifnet *ifp, struct in6_addr *addr, struct in6_addr *mask)
447686cdd19SJun-ichiro itojun Hagino {
448686cdd19SJun-ichiro itojun Hagino struct ifaddr *ia;
449686cdd19SJun-ichiro itojun Hagino struct in_ifaddr *ia4;
45019dc6445SKristof Provost struct in6_addr addr6, mask6;
45119dc6445SKristof Provost struct sockaddr_in sin4;
45219dc6445SKristof Provost struct stf_softc *sc;
453686cdd19SJun-ichiro itojun Hagino struct in_addr in;
454686cdd19SJun-ichiro itojun Hagino
4554b24e5b1SGleb Smirnoff NET_EPOCH_ASSERT();
4564b24e5b1SGleb Smirnoff
45719dc6445SKristof Provost sc = ifp->if_softc;
45819dc6445SKristof Provost
459b46512f7SKristof Provost SDT_PROBE3(if_stf, , getsrcifa6, in, ifp, addr, mask);
460b46512f7SKristof Provost
461d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia, &ifp->if_addrhead, ifa_link) {
462686cdd19SJun-ichiro itojun Hagino if (ia->ifa_addr->sa_family != AF_INET6)
463686cdd19SJun-ichiro itojun Hagino continue;
464686cdd19SJun-ichiro itojun Hagino
46519dc6445SKristof Provost addr6 = *IFA_IN6(ia);
46619dc6445SKristof Provost mask6 = *IFA_MASKIN6(ia);
46719dc6445SKristof Provost if (sc->srcv4_addr != INADDR_ANY)
46819dc6445SKristof Provost bcopy(&sc->srcv4_addr, &in, sizeof(in));
46919dc6445SKristof Provost else {
47019dc6445SKristof Provost if (stf_getin4addr(sc, &sin4, addr6, mask6) == NULL)
47119dc6445SKristof Provost continue;
47219dc6445SKristof Provost bcopy(&sin4.sin_addr, &in, sizeof(in));
47319dc6445SKristof Provost }
47419dc6445SKristof Provost
475c8ee75f2SGleb Smirnoff CK_LIST_FOREACH(ia4, INADDR_HASH(in.s_addr), ia_hash)
476686cdd19SJun-ichiro itojun Hagino if (ia4->ia_addr.sin_addr.s_addr == in.s_addr)
477686cdd19SJun-ichiro itojun Hagino break;
478686cdd19SJun-ichiro itojun Hagino if (ia4 == NULL)
479686cdd19SJun-ichiro itojun Hagino continue;
480686cdd19SJun-ichiro itojun Hagino
48119dc6445SKristof Provost *addr = addr6;
48219dc6445SKristof Provost *mask = mask6;
483d74b9a2cSAlexander V. Chernikov
484b46512f7SKristof Provost SDT_PROBE2(if_stf, , getsrcifa6, found, addr, mask);
485b46512f7SKristof Provost
486d74b9a2cSAlexander V. Chernikov return (0);
487686cdd19SJun-ichiro itojun Hagino }
488686cdd19SJun-ichiro itojun Hagino
489b46512f7SKristof Provost SDT_PROBE0(if_stf, , getsrcifa6, notfound);
490b46512f7SKristof Provost
491d74b9a2cSAlexander V. Chernikov return (ENOENT);
492686cdd19SJun-ichiro itojun Hagino }
493686cdd19SJun-ichiro itojun Hagino
494686cdd19SJun-ichiro itojun Hagino static int
stf_output(struct ifnet * ifp,struct mbuf * m,const struct sockaddr * dst,struct route * ro)49547e8d432SGleb Smirnoff stf_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
49647e8d432SGleb Smirnoff struct route *ro)
497686cdd19SJun-ichiro itojun Hagino {
498686cdd19SJun-ichiro itojun Hagino struct stf_softc *sc;
49947e8d432SGleb Smirnoff const struct sockaddr_in6 *dst6;
50019dc6445SKristof Provost struct sockaddr_in dst4, src4;
501686cdd19SJun-ichiro itojun Hagino u_int8_t tos;
502686cdd19SJun-ichiro itojun Hagino struct ip *ip;
503686cdd19SJun-ichiro itojun Hagino struct ip6_hdr *ip6;
504d74b9a2cSAlexander V. Chernikov struct in6_addr addr6, mask6;
5056b459e49SRobert Watson int error;
5066b459e49SRobert Watson
507b46512f7SKristof Provost SDT_PROBE4(if_stf, , stf_output, in, ifp, m, dst, ro);
508b46512f7SKristof Provost
5090dae32f2SDavid Malone #ifdef MAC
51030d239bcSRobert Watson error = mac_ifnet_check_transmit(ifp, m);
5116b459e49SRobert Watson if (error) {
5126b459e49SRobert Watson m_freem(m);
513b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, error, __LINE__);
5146b459e49SRobert Watson return (error);
5156b459e49SRobert Watson }
5166b459e49SRobert Watson #endif
517686cdd19SJun-ichiro itojun Hagino
518fc74a9f9SBrooks Davis sc = ifp->if_softc;
51947e8d432SGleb Smirnoff dst6 = (const struct sockaddr_in6 *)dst;
520686cdd19SJun-ichiro itojun Hagino
521686cdd19SJun-ichiro itojun Hagino /* just in case */
522686cdd19SJun-ichiro itojun Hagino if ((ifp->if_flags & IFF_UP) == 0) {
523686cdd19SJun-ichiro itojun Hagino m_freem(m);
5243751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
525b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENETDOWN, __LINE__);
5263576121cSKristof Provost return (ENETDOWN);
527686cdd19SJun-ichiro itojun Hagino }
528686cdd19SJun-ichiro itojun Hagino
529686cdd19SJun-ichiro itojun Hagino /*
530686cdd19SJun-ichiro itojun Hagino * If we don't have an ip4 address that match my inner ip6 address,
531686cdd19SJun-ichiro itojun Hagino * we shouldn't generate output. Without this check, we'll end up
532686cdd19SJun-ichiro itojun Hagino * using wrong IPv4 source.
533686cdd19SJun-ichiro itojun Hagino */
534d74b9a2cSAlexander V. Chernikov if (stf_getsrcifa6(ifp, &addr6, &mask6) != 0) {
535686cdd19SJun-ichiro itojun Hagino m_freem(m);
5363751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
537b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENETDOWN, __LINE__);
5383576121cSKristof Provost return (ENETDOWN);
539686cdd19SJun-ichiro itojun Hagino }
540686cdd19SJun-ichiro itojun Hagino
541686cdd19SJun-ichiro itojun Hagino if (m->m_len < sizeof(*ip6)) {
542686cdd19SJun-ichiro itojun Hagino m = m_pullup(m, sizeof(*ip6));
543f26b2d5bSHajimu UMEMOTO if (!m) {
5443751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
545b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENOBUFS,
546b46512f7SKristof Provost __LINE__);
5473576121cSKristof Provost return (ENOBUFS);
548686cdd19SJun-ichiro itojun Hagino }
549f26b2d5bSHajimu UMEMOTO }
550686cdd19SJun-ichiro itojun Hagino ip6 = mtod(m, struct ip6_hdr *);
551bb4a7d94SKristof Provost tos = IPV6_TRAFFIC_CLASS(ip6);
552686cdd19SJun-ichiro itojun Hagino
55333841545SHajimu UMEMOTO /*
55433841545SHajimu UMEMOTO * Pickup the right outer dst addr from the list of candidates.
55533841545SHajimu UMEMOTO * ip6_dst has priority as it may be able to give us shorter IPv4 hops.
55633841545SHajimu UMEMOTO */
55719dc6445SKristof Provost if (stf_getin4addr_in6(sc, &dst4, addr6, mask6,
55819dc6445SKristof Provost ip6->ip6_dst) == NULL) {
55919dc6445SKristof Provost if (sc->braddr != INADDR_ANY)
56019dc6445SKristof Provost dst4.sin_addr.s_addr = sc->braddr;
56119dc6445SKristof Provost else if (stf_getin4addr_in6(sc, &dst4, addr6, mask6,
56219dc6445SKristof Provost dst6->sin6_addr) == NULL) {
56333841545SHajimu UMEMOTO m_freem(m);
5643751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
565b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENETUNREACH,
566b46512f7SKristof Provost __LINE__);
5673576121cSKristof Provost return (ENETUNREACH);
56833841545SHajimu UMEMOTO }
56919dc6445SKristof Provost }
57033841545SHajimu UMEMOTO
57116d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) {
5728acb2290SHajimu UMEMOTO /*
5738acb2290SHajimu UMEMOTO * We need to prepend the address family as
5748acb2290SHajimu UMEMOTO * a four byte field. Cons up a dummy header
5758acb2290SHajimu UMEMOTO * to pacify bpf. This is safe because bpf
5768acb2290SHajimu UMEMOTO * will only read from the mbuf (i.e., it won't
5778acb2290SHajimu UMEMOTO * try to free it or keep a pointer a to it).
5788acb2290SHajimu UMEMOTO */
57947e8d432SGleb Smirnoff u_int af = AF_INET6;
580437ffe18SSam Leffler bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
5818acb2290SHajimu UMEMOTO }
5828acb2290SHajimu UMEMOTO
583eb1b1807SGleb Smirnoff M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
584f26b2d5bSHajimu UMEMOTO if (m == NULL) {
5853751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
586b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENOBUFS, __LINE__);
5873576121cSKristof Provost return (ENOBUFS);
588f26b2d5bSHajimu UMEMOTO }
589686cdd19SJun-ichiro itojun Hagino ip = mtod(m, struct ip *);
590686cdd19SJun-ichiro itojun Hagino
591686cdd19SJun-ichiro itojun Hagino bzero(ip, sizeof(*ip));
592686cdd19SJun-ichiro itojun Hagino
59319dc6445SKristof Provost if (sc->srcv4_addr != INADDR_ANY)
59419dc6445SKristof Provost src4.sin_addr.s_addr = sc->srcv4_addr;
59519dc6445SKristof Provost else if (stf_getin4addr(sc, &src4, addr6, mask6) == NULL) {
59619dc6445SKristof Provost m_freem(m);
59719dc6445SKristof Provost if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
598b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_output, error, ENETUNREACH, __LINE__);
59919dc6445SKristof Provost return (ENETUNREACH);
60019dc6445SKristof Provost }
60119dc6445SKristof Provost bcopy(&src4.sin_addr, &ip->ip_src, sizeof(ip->ip_src));
60219dc6445SKristof Provost bcopy(&dst4.sin_addr, &ip->ip_dst, sizeof(ip->ip_dst));
60319dc6445SKristof Provost
604686cdd19SJun-ichiro itojun Hagino ip->ip_p = IPPROTO_IPV6;
60553dab5feSBrooks Davis ip->ip_ttl = ip_stf_ttl;
6068f134647SGleb Smirnoff ip->ip_len = htons(m->m_pkthdr.len);
607686cdd19SJun-ichiro itojun Hagino if (ifp->if_flags & IFF_LINK1)
608686cdd19SJun-ichiro itojun Hagino ip_ecn_ingress(ECN_ALLOWED, &ip->ip_tos, &tos);
60933841545SHajimu UMEMOTO else
61033841545SHajimu UMEMOTO ip_ecn_ingress(ECN_NOCARE, &ip->ip_tos, &tos);
611686cdd19SJun-ichiro itojun Hagino
6128b07e49aSJulian Elischer M_SETFIB(m, sc->sc_fibnum);
6133751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
614d74b9a2cSAlexander V. Chernikov error = ip_output(m, NULL, NULL, 0, NULL, NULL);
6150dae32f2SDavid Malone
616b46512f7SKristof Provost SDT_PROBE1(if_stf, , stf_output, out, error);
6173576121cSKristof Provost return (error);
618686cdd19SJun-ichiro itojun Hagino }
619686cdd19SJun-ichiro itojun Hagino
620686cdd19SJun-ichiro itojun Hagino static int
isrfc1918addr(struct in_addr * in)621926381e1SAndrey V. Elsukov isrfc1918addr(struct in_addr *in)
622ce9d7b2fSHajimu UMEMOTO {
623ce9d7b2fSHajimu UMEMOTO /*
624ce9d7b2fSHajimu UMEMOTO * returns 1 if private address range:
625ce9d7b2fSHajimu UMEMOTO * 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
626ce9d7b2fSHajimu UMEMOTO */
62751743c5fSAndrey V. Elsukov if (stf_permit_rfc1918 == 0 && (
62851743c5fSAndrey V. Elsukov (ntohl(in->s_addr) & 0xff000000) >> 24 == 10 ||
6298d95b0ceSSUZUKI Shinsuke (ntohl(in->s_addr) & 0xfff00000) >> 16 == 172 * 256 + 16 ||
63051743c5fSAndrey V. Elsukov (ntohl(in->s_addr) & 0xffff0000) >> 16 == 192 * 256 + 168))
6313576121cSKristof Provost return (1);
632ce9d7b2fSHajimu UMEMOTO
6333576121cSKristof Provost return (0);
634ce9d7b2fSHajimu UMEMOTO }
635ce9d7b2fSHajimu UMEMOTO
636ce9d7b2fSHajimu UMEMOTO static int
stf_checkaddr4(struct stf_softc * sc,struct in_addr * in,struct ifnet * inifp)637926381e1SAndrey V. Elsukov stf_checkaddr4(struct stf_softc *sc, struct in_addr *in, struct ifnet *inifp)
638686cdd19SJun-ichiro itojun Hagino {
639686cdd19SJun-ichiro itojun Hagino struct in_ifaddr *ia4;
640686cdd19SJun-ichiro itojun Hagino
641686cdd19SJun-ichiro itojun Hagino /*
642686cdd19SJun-ichiro itojun Hagino * reject packets with the following address:
643686cdd19SJun-ichiro itojun Hagino * 224.0.0.0/4 0.0.0.0/8 127.0.0.0/8 255.0.0.0/8
644686cdd19SJun-ichiro itojun Hagino */
6458d95b0ceSSUZUKI Shinsuke if (IN_MULTICAST(ntohl(in->s_addr)))
6463576121cSKristof Provost return (-1);
6478d95b0ceSSUZUKI Shinsuke switch ((ntohl(in->s_addr) & 0xff000000) >> 24) {
6488d95b0ceSSUZUKI Shinsuke case 0: case 127: case 255:
6493576121cSKristof Provost return (-1);
6508d95b0ceSSUZUKI Shinsuke }
651686cdd19SJun-ichiro itojun Hagino
652686cdd19SJun-ichiro itojun Hagino /*
653686cdd19SJun-ichiro itojun Hagino * reject packets with broadcast
654686cdd19SJun-ichiro itojun Hagino */
655d7c5a620SMatt Macy CK_STAILQ_FOREACH(ia4, &V_in_ifaddrhead, ia_link) {
656686cdd19SJun-ichiro itojun Hagino if ((ia4->ia_ifa.ifa_ifp->if_flags & IFF_BROADCAST) == 0)
657686cdd19SJun-ichiro itojun Hagino continue;
6582d9cfabaSRobert Watson if (in->s_addr == ia4->ia_broadaddr.sin_addr.s_addr) {
6593576121cSKristof Provost return (-1);
660686cdd19SJun-ichiro itojun Hagino }
6612d9cfabaSRobert Watson }
662686cdd19SJun-ichiro itojun Hagino
663686cdd19SJun-ichiro itojun Hagino /*
664686cdd19SJun-ichiro itojun Hagino * perform ingress filter
665686cdd19SJun-ichiro itojun Hagino */
666fc74a9f9SBrooks Davis if (sc && (STF2IFP(sc)->if_flags & IFF_LINK2) == 0 && inifp) {
6676ad7446cSAlexander V. Chernikov struct nhop_object *nh;
668686cdd19SJun-ichiro itojun Hagino
6696ad7446cSAlexander V. Chernikov NET_EPOCH_ASSERT();
6706ad7446cSAlexander V. Chernikov nh = fib4_lookup(sc->sc_fibnum, *in, 0, 0, 0);
6716ad7446cSAlexander V. Chernikov if (nh == NULL)
6720792bcbbSAlexander V. Chernikov return (-1);
6730792bcbbSAlexander V. Chernikov
6746ad7446cSAlexander V. Chernikov if (nh->nh_ifp != inifp)
6750792bcbbSAlexander V. Chernikov return (-1);
676686cdd19SJun-ichiro itojun Hagino }
677686cdd19SJun-ichiro itojun Hagino
6783576121cSKristof Provost return (0);
679686cdd19SJun-ichiro itojun Hagino }
680686cdd19SJun-ichiro itojun Hagino
681686cdd19SJun-ichiro itojun Hagino static int
stf_checkaddr6(struct stf_softc * sc,struct in6_addr * in6,struct ifnet * inifp)682926381e1SAndrey V. Elsukov stf_checkaddr6(struct stf_softc *sc, struct in6_addr *in6, struct ifnet *inifp)
683686cdd19SJun-ichiro itojun Hagino {
684b46512f7SKristof Provost SDT_PROBE3(if_stf, , checkaddr6, in, sc, in6, inifp);
685b46512f7SKristof Provost
686686cdd19SJun-ichiro itojun Hagino /*
687686cdd19SJun-ichiro itojun Hagino * check 6to4 addresses
688686cdd19SJun-ichiro itojun Hagino */
6898d95b0ceSSUZUKI Shinsuke if (IN6_IS_ADDR_6TO4(in6)) {
6908d95b0ceSSUZUKI Shinsuke struct in_addr in4;
691b46512f7SKristof Provost int ret;
692b46512f7SKristof Provost
6938d95b0ceSSUZUKI Shinsuke bcopy(GET_V4(in6), &in4, sizeof(in4));
694b46512f7SKristof Provost ret = stf_checkaddr4(sc, &in4, inifp);
695b46512f7SKristof Provost SDT_PROBE2(if_stf, , checkaddr6, out, ret, __LINE__);
696b46512f7SKristof Provost return (ret);
6978d95b0ceSSUZUKI Shinsuke }
698686cdd19SJun-ichiro itojun Hagino
699686cdd19SJun-ichiro itojun Hagino /*
700686cdd19SJun-ichiro itojun Hagino * reject anything that look suspicious. the test is implemented
701686cdd19SJun-ichiro itojun Hagino * in ip6_input too, but we check here as well to
702686cdd19SJun-ichiro itojun Hagino * (1) reject bad packets earlier, and
703686cdd19SJun-ichiro itojun Hagino * (2) to be safe against future ip6_input change.
704686cdd19SJun-ichiro itojun Hagino */
705b46512f7SKristof Provost if (IN6_IS_ADDR_V4COMPAT(in6)) {
706b46512f7SKristof Provost SDT_PROBE2(if_stf, , checkaddr6, out, -1, __LINE__);
7073576121cSKristof Provost return (-1);
708b46512f7SKristof Provost }
709686cdd19SJun-ichiro itojun Hagino
710b46512f7SKristof Provost if (IN6_IS_ADDR_V4MAPPED(in6)) {
711b46512f7SKristof Provost SDT_PROBE2(if_stf, , checkaddr6, out, -1, __LINE__);
712b46512f7SKristof Provost return (-1);
713b46512f7SKristof Provost }
714b46512f7SKristof Provost
715b46512f7SKristof Provost SDT_PROBE2(if_stf, , checkaddr6, out, 0, __LINE__);
7163576121cSKristof Provost return (0);
717686cdd19SJun-ichiro itojun Hagino }
718686cdd19SJun-ichiro itojun Hagino
71906cd035aSAndrey V. Elsukov static int
in_stf_input(struct mbuf * m,int off,int proto,void * arg)7206d8fdfa9SAndrey V. Elsukov in_stf_input(struct mbuf *m, int off, int proto, void *arg)
721686cdd19SJun-ichiro itojun Hagino {
7226d8fdfa9SAndrey V. Elsukov struct stf_softc *sc = arg;
723439da7f0SKristof Provost struct ip ip;
724686cdd19SJun-ichiro itojun Hagino struct ip6_hdr *ip6;
725686cdd19SJun-ichiro itojun Hagino u_int8_t otos, itos;
726686cdd19SJun-ichiro itojun Hagino struct ifnet *ifp;
72719dc6445SKristof Provost struct nhop_object *nh;
728686cdd19SJun-ichiro itojun Hagino
729b8a6e03fSGleb Smirnoff NET_EPOCH_ASSERT();
730b8a6e03fSGleb Smirnoff
731b46512f7SKristof Provost SDT_PROBE3(if_stf, , stf_input, in, m, off, proto);
732b46512f7SKristof Provost
733686cdd19SJun-ichiro itojun Hagino if (proto != IPPROTO_IPV6) {
734686cdd19SJun-ichiro itojun Hagino m_freem(m);
735b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
7368f5a8818SKevin Lo return (IPPROTO_DONE);
737686cdd19SJun-ichiro itojun Hagino }
738686cdd19SJun-ichiro itojun Hagino
739439da7f0SKristof Provost m_copydata(m, 0, sizeof(struct ip), (caddr_t)&ip);
740fc74a9f9SBrooks Davis if (sc == NULL || (STF2IFP(sc)->if_flags & IFF_UP) == 0) {
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
746fc74a9f9SBrooks Davis ifp = STF2IFP(sc);
747686cdd19SJun-ichiro itojun Hagino
7486b459e49SRobert Watson #ifdef MAC
74930d239bcSRobert Watson mac_ifnet_create_mbuf(ifp, m);
7506b459e49SRobert Watson #endif
7516b459e49SRobert Watson
752686cdd19SJun-ichiro itojun Hagino /*
753686cdd19SJun-ichiro itojun Hagino * perform sanity check against outer src/dst.
754686cdd19SJun-ichiro itojun Hagino * for source, perform ingress filter as well.
755686cdd19SJun-ichiro itojun Hagino */
756439da7f0SKristof Provost if (stf_checkaddr4(sc, &ip.ip_dst, NULL) < 0 ||
757439da7f0SKristof Provost stf_checkaddr4(sc, &ip.ip_src, m->m_pkthdr.rcvif) < 0) {
758686cdd19SJun-ichiro itojun Hagino m_freem(m);
759b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
7608f5a8818SKevin Lo return (IPPROTO_DONE);
761686cdd19SJun-ichiro itojun Hagino }
762686cdd19SJun-ichiro itojun Hagino
763439da7f0SKristof Provost otos = ip.ip_tos;
764686cdd19SJun-ichiro itojun Hagino m_adj(m, off);
765686cdd19SJun-ichiro itojun Hagino
766686cdd19SJun-ichiro itojun Hagino if (m->m_len < sizeof(*ip6)) {
767686cdd19SJun-ichiro itojun Hagino m = m_pullup(m, sizeof(*ip6));
768b46512f7SKristof Provost if (!m) {
769b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE,
770b46512f7SKristof Provost __LINE__);
7718f5a8818SKevin Lo return (IPPROTO_DONE);
772686cdd19SJun-ichiro itojun Hagino }
773b46512f7SKristof Provost }
774686cdd19SJun-ichiro itojun Hagino ip6 = mtod(m, struct ip6_hdr *);
775686cdd19SJun-ichiro itojun Hagino
776686cdd19SJun-ichiro itojun Hagino /*
777686cdd19SJun-ichiro itojun Hagino * perform sanity check against inner src/dst.
778686cdd19SJun-ichiro itojun Hagino * for source, perform ingress filter as well.
779686cdd19SJun-ichiro itojun Hagino */
78033841545SHajimu UMEMOTO if (stf_checkaddr6(sc, &ip6->ip6_dst, NULL) < 0 ||
78133841545SHajimu UMEMOTO stf_checkaddr6(sc, &ip6->ip6_src, m->m_pkthdr.rcvif) < 0) {
782686cdd19SJun-ichiro itojun Hagino m_freem(m);
783b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
7848f5a8818SKevin Lo return (IPPROTO_DONE);
785686cdd19SJun-ichiro itojun Hagino }
786686cdd19SJun-ichiro itojun Hagino
78719dc6445SKristof Provost /*
78819dc6445SKristof Provost * reject packets with private address range.
78919dc6445SKristof Provost * (requirement from RFC3056 section 2 1st paragraph)
79019dc6445SKristof Provost */
791439da7f0SKristof Provost if ((IN6_IS_ADDR_6TO4(&ip6->ip6_src) && isrfc1918addr(&ip.ip_src)) ||
792439da7f0SKristof Provost (IN6_IS_ADDR_6TO4(&ip6->ip6_dst) && isrfc1918addr(&ip.ip_dst))) {
79319dc6445SKristof Provost m_freem(m);
794b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
79519dc6445SKristof Provost return (IPPROTO_DONE);
79619dc6445SKristof Provost }
79719dc6445SKristof Provost
79819dc6445SKristof Provost /*
79919dc6445SKristof Provost * Ignore if the destination is the same stf interface because
80019dc6445SKristof Provost * all of valid IPv6 outgoing traffic should go interfaces
80119dc6445SKristof Provost * except for it.
80219dc6445SKristof Provost */
80319dc6445SKristof Provost nh = fib6_lookup(sc->sc_fibnum, &ip6->ip6_dst, 0, 0, 0);
80419dc6445SKristof Provost if (nh == NULL) {
80519dc6445SKristof Provost m_free(m);
806b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
80719dc6445SKristof Provost return (IPPROTO_DONE);
80819dc6445SKristof Provost }
80919dc6445SKristof Provost if ((nh->nh_ifp == ifp) &&
81019dc6445SKristof Provost (!IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &nh->gw6_sa.sin6_addr))) {
81119dc6445SKristof Provost m_free(m);
812b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
81319dc6445SKristof Provost return (IPPROTO_DONE);
81419dc6445SKristof Provost }
81519dc6445SKristof Provost
816bb4a7d94SKristof Provost itos = IPV6_TRAFFIC_CLASS(ip6);
817686cdd19SJun-ichiro itojun Hagino if ((ifp->if_flags & IFF_LINK1) != 0)
818686cdd19SJun-ichiro itojun Hagino ip_ecn_egress(ECN_ALLOWED, &otos, &itos);
81933841545SHajimu UMEMOTO else
82033841545SHajimu UMEMOTO ip_ecn_egress(ECN_NOCARE, &otos, &itos);
821686cdd19SJun-ichiro itojun Hagino ip6->ip6_flow &= ~htonl(0xff << 20);
822686cdd19SJun-ichiro itojun Hagino ip6->ip6_flow |= htonl((u_int32_t)itos << 20);
823686cdd19SJun-ichiro itojun Hagino
824686cdd19SJun-ichiro itojun Hagino m->m_pkthdr.rcvif = ifp;
825686cdd19SJun-ichiro itojun Hagino
82616d878ccSChristian S.J. Peron if (bpf_peers_present(ifp->if_bpf)) {
827686cdd19SJun-ichiro itojun Hagino /*
828686cdd19SJun-ichiro itojun Hagino * We need to prepend the address family as
829686cdd19SJun-ichiro itojun Hagino * a four byte field. Cons up a dummy header
830686cdd19SJun-ichiro itojun Hagino * to pacify bpf. This is safe because bpf
831686cdd19SJun-ichiro itojun Hagino * will only read from the mbuf (i.e., it won't
832686cdd19SJun-ichiro itojun Hagino * try to free it or keep a pointer a to it).
833686cdd19SJun-ichiro itojun Hagino */
83433841545SHajimu UMEMOTO u_int32_t af = AF_INET6;
8359b1a7076SHajimu UMEMOTO bpf_mtap2(ifp->if_bpf, &af, sizeof(af), m);
836686cdd19SJun-ichiro itojun Hagino }
837686cdd19SJun-ichiro itojun Hagino
838686cdd19SJun-ichiro itojun Hagino /*
839686cdd19SJun-ichiro itojun Hagino * Put the packet to the network layer input queue according to the
840686cdd19SJun-ichiro itojun Hagino * specified address family.
841686cdd19SJun-ichiro itojun Hagino * See net/if_gif.c for possible issues with packet processing
842686cdd19SJun-ichiro itojun Hagino * reorder due to extra queueing.
843686cdd19SJun-ichiro itojun Hagino */
8443751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
8453751dddbSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
846a34c6aebSBjoern A. Zeeb M_SETFIB(m, ifp->if_fib);
8471cafed39SJonathan Lemon netisr_dispatch(NETISR_IPV6, m);
848b46512f7SKristof Provost SDT_PROBE2(if_stf, , stf_input, out, IPPROTO_DONE, __LINE__);
8498f5a8818SKevin Lo return (IPPROTO_DONE);
850686cdd19SJun-ichiro itojun Hagino }
851686cdd19SJun-ichiro itojun Hagino
85219dc6445SKristof Provost static struct sockaddr_in *
stf_getin4addr_in6(struct stf_softc * sc,struct sockaddr_in * sin,struct in6_addr addr6,struct in6_addr mask6,struct in6_addr in6)85319dc6445SKristof Provost stf_getin4addr_in6(struct stf_softc *sc, struct sockaddr_in *sin,
85419dc6445SKristof Provost struct in6_addr addr6, struct in6_addr mask6, struct in6_addr in6)
85519dc6445SKristof Provost {
85619dc6445SKristof Provost int i;
857b46512f7SKristof Provost struct sockaddr_in *out;
85819dc6445SKristof Provost
85919dc6445SKristof Provost /*
86019dc6445SKristof Provost * When (src addr & src mask) != (in6 & src mask),
86119dc6445SKristof Provost * the dst is not in the 6rd domain. The IPv4 address must
86219dc6445SKristof Provost * not be used.
86319dc6445SKristof Provost */
86419dc6445SKristof Provost for (i = 0; i < sizeof(addr6); i++) {
86519dc6445SKristof Provost if ((((u_char *)&addr6)[i] & ((u_char *)&mask6)[i]) !=
866b46512f7SKristof Provost (((u_char *)&in6)[i] & ((u_char *)&mask6)[i])) {
867b46512f7SKristof Provost SDT_PROBE4(if_stf, , getin4addr_in6, out, &addr6,
868b46512f7SKristof Provost &mask6, &in6, NULL);
86919dc6445SKristof Provost return (NULL);
87019dc6445SKristof Provost }
871b46512f7SKristof Provost }
87219dc6445SKristof Provost
87319dc6445SKristof Provost /* After the mask check, use in6 instead of addr6. */
874b46512f7SKristof Provost out = stf_getin4addr(sc, sin, in6, mask6);
875b46512f7SKristof Provost SDT_PROBE4(if_stf, , getin4addr_in6, out, &addr6, &mask6, &in6, out);
876b46512f7SKristof Provost return (out);
87719dc6445SKristof Provost }
87819dc6445SKristof Provost
87919dc6445SKristof Provost static struct sockaddr_in *
stf_getin4addr(struct stf_softc * sc,struct sockaddr_in * sin,struct in6_addr addr6,struct in6_addr mask6)88019dc6445SKristof Provost stf_getin4addr(struct stf_softc *sc, struct sockaddr_in *sin,
88119dc6445SKristof Provost struct in6_addr addr6, struct in6_addr mask6)
88219dc6445SKristof Provost {
88319dc6445SKristof Provost struct in_addr *in;
88419dc6445SKristof Provost
885b46512f7SKristof Provost SDT_PROBE2(if_stf, , getin4addr, in, &addr6, &mask6);
886b46512f7SKristof Provost
88719dc6445SKristof Provost memset(sin, 0, sizeof(*sin));
88819dc6445SKristof Provost in = &sin->sin_addr;
88919dc6445SKristof Provost if (IN6_IS_ADDR_6TO4(&addr6)) {
89019dc6445SKristof Provost /* 6to4 (RFC 3056) */
89119dc6445SKristof Provost bcopy(GET_V4(&addr6), in, sizeof(*in));
89219dc6445SKristof Provost if (isrfc1918addr(in))
89319dc6445SKristof Provost return (NULL);
89419dc6445SKristof Provost } else {
89519dc6445SKristof Provost /* 6rd (RFC 5569) */
89619dc6445SKristof Provost in_addr_t v4prefix;
89719dc6445SKristof Provost uint8_t *v6 = (uint8_t*)&addr6;
89819dc6445SKristof Provost uint64_t v6prefix;
89919dc6445SKristof Provost u_int plen;
90019dc6445SKristof Provost u_int v4suffixlen;
90119dc6445SKristof Provost
90219dc6445SKristof Provost v4prefix = 0;
90319dc6445SKristof Provost if (sc->v4prefixlen < 32) {
90419dc6445SKristof Provost v4suffixlen = 32 - sc->v4prefixlen;
90519dc6445SKristof Provost v4prefix = ntohl(sc->srcv4_addr) &
90619dc6445SKristof Provost (0xffffffffU << v4suffixlen);
90719dc6445SKristof Provost } else {
90819dc6445SKristof Provost MPASS(sc->v4prefixlen == 32);
90919dc6445SKristof Provost v4suffixlen = 32;
91019dc6445SKristof Provost }
91119dc6445SKristof Provost
91219dc6445SKristof Provost plen = in6_mask2len(&mask6, NULL);
91319dc6445SKristof Provost if (plen > 64)
91419dc6445SKristof Provost return (NULL);
91519dc6445SKristof Provost
91619dc6445SKristof Provost /* To make this simple we do not support prefixes longer than
91719dc6445SKristof Provost * 64 bits. RFC5969 says "a 6rd delegated prefix SHOULD be /64
91819dc6445SKristof Provost * or shorter." so this is a moderately safe assumption. */
91919dc6445SKristof Provost v6prefix = be64toh(*(uint64_t *)v6);
92019dc6445SKristof Provost
92119dc6445SKristof Provost /* Shift away the v6 prefix itself. */
92219dc6445SKristof Provost v6prefix <<= plen;
92319dc6445SKristof Provost v6prefix >>= plen;
92419dc6445SKristof Provost
92519dc6445SKristof Provost /* Now shift away everything after the v4 address. */
92619dc6445SKristof Provost v6prefix >>= 64 - plen - v4suffixlen;
92719dc6445SKristof Provost
92819dc6445SKristof Provost sin->sin_addr.s_addr = htonl(v4prefix | (uint32_t)v6prefix);
92919dc6445SKristof Provost }
93019dc6445SKristof Provost
931b46512f7SKristof Provost SDT_PROBE1(if_stf, , getin4addr, out, sin);
932b46512f7SKristof Provost
93319dc6445SKristof Provost return (sin);
93419dc6445SKristof Provost }
93519dc6445SKristof Provost
936686cdd19SJun-ichiro itojun Hagino static int
stf_ioctl(struct ifnet * ifp,u_long cmd,caddr_t data)937926381e1SAndrey V. Elsukov stf_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
938686cdd19SJun-ichiro itojun Hagino {
939686cdd19SJun-ichiro itojun Hagino struct ifaddr *ifa;
94019dc6445SKristof Provost struct ifdrv *ifd;
941686cdd19SJun-ichiro itojun Hagino struct ifreq *ifr;
94219dc6445SKristof Provost struct sockaddr_in sin4;
94319dc6445SKristof Provost struct stf_softc *sc_cur;
94419dc6445SKristof Provost struct stfv4args args;
94599ab4b12SAlexander V. Chernikov int error, mtu;
946686cdd19SJun-ichiro itojun Hagino
947686cdd19SJun-ichiro itojun Hagino error = 0;
94819dc6445SKristof Provost sc_cur = ifp->if_softc;
94919dc6445SKristof Provost
950686cdd19SJun-ichiro itojun Hagino switch (cmd) {
95119dc6445SKristof Provost case SIOCSDRVSPEC:
95219dc6445SKristof Provost ifd = (struct ifdrv *)data;
95319dc6445SKristof Provost error = priv_check(curthread, PRIV_NET_ADDIFADDR);
95419dc6445SKristof Provost if (error)
95519dc6445SKristof Provost break;
95619dc6445SKristof Provost if (ifd->ifd_cmd == STF6RD_SV4NET) {
95719dc6445SKristof Provost if (ifd->ifd_len != sizeof(args)) {
95819dc6445SKristof Provost error = EINVAL;
95919dc6445SKristof Provost break;
96019dc6445SKristof Provost }
96119dc6445SKristof Provost bzero(&args, sizeof(args));
96219dc6445SKristof Provost error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
96319dc6445SKristof Provost if (error)
96419dc6445SKristof Provost break;
96519dc6445SKristof Provost
96619dc6445SKristof Provost if (args.v4_prefixlen < 1 || args.v4_prefixlen > 32) {
96719dc6445SKristof Provost error = EINVAL;
96819dc6445SKristof Provost break;
96919dc6445SKristof Provost }
97019dc6445SKristof Provost
97119dc6445SKristof Provost bcopy(&args.srcv4_addr, &sc_cur->srcv4_addr,
97219dc6445SKristof Provost sizeof(sc_cur->srcv4_addr));
97319dc6445SKristof Provost sc_cur->v4prefixlen = args.v4_prefixlen;
974b46512f7SKristof Provost SDT_PROBE3(if_stf, , ioctl, sv4net, sc_cur->srcv4_addr,
975b46512f7SKristof Provost sc_cur->srcv4_addr, sc_cur->v4prefixlen);
97619dc6445SKristof Provost } else if (ifd->ifd_cmd == STF6RD_SBR) {
97719dc6445SKristof Provost if (ifd->ifd_len != sizeof(args)) {
97819dc6445SKristof Provost error = EINVAL;
97919dc6445SKristof Provost break;
98019dc6445SKristof Provost }
98119dc6445SKristof Provost bzero(&args, sizeof(args));
98219dc6445SKristof Provost error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
98319dc6445SKristof Provost if (error)
98419dc6445SKristof Provost break;
98519dc6445SKristof Provost sc_cur->braddr = args.braddr.s_addr;
986b46512f7SKristof Provost SDT_PROBE1(if_stf, , ioctl, sdstv4,
987b46512f7SKristof Provost sc_cur->braddr);
98819dc6445SKristof Provost } else
98919dc6445SKristof Provost error = EINVAL;
99019dc6445SKristof Provost break;
99119dc6445SKristof Provost case SIOCGDRVSPEC:
99219dc6445SKristof Provost ifd = (struct ifdrv *)data;
99319dc6445SKristof Provost if (ifd->ifd_cmd != STF6RD_GV4NET) {
99419dc6445SKristof Provost error = EINVAL;
99519dc6445SKristof Provost break;
99619dc6445SKristof Provost }
99719dc6445SKristof Provost if (ifd->ifd_len != sizeof(args)) {
99819dc6445SKristof Provost error = EINVAL;
99919dc6445SKristof Provost break;
100019dc6445SKristof Provost }
100119dc6445SKristof Provost bzero(&args, sizeof(args));
100219dc6445SKristof Provost args.srcv4_addr.s_addr = sc_cur->srcv4_addr;
100319dc6445SKristof Provost args.braddr.s_addr = sc_cur->braddr;
100419dc6445SKristof Provost args.v4_prefixlen = sc_cur->v4prefixlen;
100519dc6445SKristof Provost error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
100619dc6445SKristof Provost break;
1007686cdd19SJun-ichiro itojun Hagino case SIOCSIFADDR:
1008686cdd19SJun-ichiro itojun Hagino ifa = (struct ifaddr *)data;
1009b46512f7SKristof Provost SDT_PROBE1(if_stf, , ioctl, ifaddr, ifa);
1010686cdd19SJun-ichiro itojun Hagino if (ifa == NULL || ifa->ifa_addr->sa_family != AF_INET6) {
1011686cdd19SJun-ichiro itojun Hagino error = EAFNOSUPPORT;
1012686cdd19SJun-ichiro itojun Hagino break;
1013686cdd19SJun-ichiro itojun Hagino }
101419dc6445SKristof Provost if (stf_getin4addr(sc_cur, &sin4,
101519dc6445SKristof Provost satosin6(ifa->ifa_addr)->sin6_addr,
101619dc6445SKristof Provost satosin6(ifa->ifa_netmask)->sin6_addr) == NULL) {
10178d95b0ceSSUZUKI Shinsuke error = EINVAL;
10188d95b0ceSSUZUKI Shinsuke break;
10198d95b0ceSSUZUKI Shinsuke }
1020686cdd19SJun-ichiro itojun Hagino ifp->if_flags |= IFF_UP;
1021ca1163bdSMark Johnston ifp->if_drv_flags |= IFF_DRV_RUNNING;
1022686cdd19SJun-ichiro itojun Hagino break;
1023686cdd19SJun-ichiro itojun Hagino
1024686cdd19SJun-ichiro itojun Hagino case SIOCADDMULTI:
1025686cdd19SJun-ichiro itojun Hagino case SIOCDELMULTI:
1026686cdd19SJun-ichiro itojun Hagino ifr = (struct ifreq *)data;
1027686cdd19SJun-ichiro itojun Hagino if (ifr && ifr->ifr_addr.sa_family == AF_INET6)
1028686cdd19SJun-ichiro itojun Hagino ;
1029686cdd19SJun-ichiro itojun Hagino else
1030686cdd19SJun-ichiro itojun Hagino error = EAFNOSUPPORT;
1031686cdd19SJun-ichiro itojun Hagino break;
1032686cdd19SJun-ichiro itojun Hagino
103399ab4b12SAlexander V. Chernikov case SIOCGIFMTU:
103499ab4b12SAlexander V. Chernikov break;
103599ab4b12SAlexander V. Chernikov
103699ab4b12SAlexander V. Chernikov case SIOCSIFMTU:
103799ab4b12SAlexander V. Chernikov ifr = (struct ifreq *)data;
103899ab4b12SAlexander V. Chernikov mtu = ifr->ifr_mtu;
103999ab4b12SAlexander V. Chernikov /* RFC 4213 3.2 ideal world MTU */
104099ab4b12SAlexander V. Chernikov if (mtu < IPV6_MINMTU || mtu > IF_MAXMTU - 20)
104199ab4b12SAlexander V. Chernikov return (EINVAL);
104299ab4b12SAlexander V. Chernikov ifp->if_mtu = mtu;
104399ab4b12SAlexander V. Chernikov break;
104499ab4b12SAlexander V. Chernikov
1045686cdd19SJun-ichiro itojun Hagino default:
1046686cdd19SJun-ichiro itojun Hagino error = EINVAL;
1047686cdd19SJun-ichiro itojun Hagino break;
1048686cdd19SJun-ichiro itojun Hagino }
1049686cdd19SJun-ichiro itojun Hagino
10503576121cSKristof Provost return (error);
1051686cdd19SJun-ichiro itojun Hagino }
1052