1caf43b02SWarner Losh /*-
251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni *
4cfa1ca9dSYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5b941bc1dSAndrey V. Elsukov * Copyright (c) 2018 Andrey V. Elsukov <ae@FreeBSD.org>
6cfa1ca9dSYoshinobu Inoue * All rights reserved.
7cfa1ca9dSYoshinobu Inoue *
8cfa1ca9dSYoshinobu Inoue * Redistribution and use in source and binary forms, with or without
9cfa1ca9dSYoshinobu Inoue * modification, are permitted provided that the following conditions
10cfa1ca9dSYoshinobu Inoue * are met:
11cfa1ca9dSYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright
12cfa1ca9dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer.
13cfa1ca9dSYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright
14cfa1ca9dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the
15cfa1ca9dSYoshinobu Inoue * documentation and/or other materials provided with the distribution.
16cfa1ca9dSYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors
17cfa1ca9dSYoshinobu Inoue * may be used to endorse or promote products derived from this software
18cfa1ca9dSYoshinobu Inoue * without specific prior written permission.
19cfa1ca9dSYoshinobu Inoue *
20cfa1ca9dSYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21cfa1ca9dSYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22cfa1ca9dSYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23cfa1ca9dSYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24cfa1ca9dSYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25cfa1ca9dSYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26cfa1ca9dSYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27cfa1ca9dSYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28cfa1ca9dSYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29cfa1ca9dSYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30cfa1ca9dSYoshinobu Inoue * SUCH DAMAGE.
31b48287a3SDavid E. O'Brien *
32b48287a3SDavid E. O'Brien * $KAME: in6_gif.c,v 1.49 2001/05/14 14:02:17 itojun Exp $
33cfa1ca9dSYoshinobu Inoue */
34cfa1ca9dSYoshinobu Inoue
35b48287a3SDavid E. O'Brien #include <sys/cdefs.h>
36cfa1ca9dSYoshinobu Inoue #include "opt_inet.h"
37686cdd19SJun-ichiro itojun Hagino #include "opt_inet6.h"
38cfa1ca9dSYoshinobu Inoue
39cfa1ca9dSYoshinobu Inoue #include <sys/param.h>
40cfa1ca9dSYoshinobu Inoue #include <sys/systm.h>
41b941bc1dSAndrey V. Elsukov #include <sys/jail.h>
42cfa1ca9dSYoshinobu Inoue #include <sys/socket.h>
43cfa1ca9dSYoshinobu Inoue #include <sys/sockio.h>
44cfa1ca9dSYoshinobu Inoue #include <sys/mbuf.h>
45cfa1ca9dSYoshinobu Inoue #include <sys/errno.h>
4682cea7e6SBjoern A. Zeeb #include <sys/kernel.h>
4733841545SHajimu UMEMOTO #include <sys/syslog.h>
4882cea7e6SBjoern A. Zeeb #include <sys/sysctl.h>
4933841545SHajimu UMEMOTO #include <sys/malloc.h>
506573d758SMatt Macy #include <sys/proc.h>
51cfa1ca9dSYoshinobu Inoue
52b941bc1dSAndrey V. Elsukov #include <net/ethernet.h>
53cfa1ca9dSYoshinobu Inoue #include <net/if.h>
5476039bc8SGleb Smirnoff #include <net/if_var.h>
553d0d5b21SJustin Hibbits #include <net/if_private.h>
56cfa1ca9dSYoshinobu Inoue #include <net/route.h>
5776039bc8SGleb Smirnoff #include <net/vnet.h>
58cfa1ca9dSYoshinobu Inoue
59cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
60cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h>
61cfa1ca9dSYoshinobu Inoue #ifdef INET
62cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h>
63b941bc1dSAndrey V. Elsukov #include <netinet/ip_ecn.h>
64cfa1ca9dSYoshinobu Inoue #endif
65686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
66686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
67cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
68686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_var.h>
69b941bc1dSAndrey V. Elsukov #include <netinet6/scope6_var.h>
706a800098SYoshinobu Inoue #include <netinet6/ip6_ecn.h>
7165ff3638SAlexander V. Chernikov #include <netinet6/in6_fib.h>
72cfa1ca9dSYoshinobu Inoue
73cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h>
74cfa1ca9dSYoshinobu Inoue
75132c4490SAndrey V. Elsukov #define GIF_HLIM 30
765f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ip6_gif_hlim) = GIF_HLIM;
7782cea7e6SBjoern A. Zeeb #define V_ip6_gif_hlim VNET(ip6_gif_hlim)
7882cea7e6SBjoern A. Zeeb
7982cea7e6SBjoern A. Zeeb SYSCTL_DECL(_net_inet6_ip6);
806d8fdfa9SAndrey V. Elsukov SYSCTL_INT(_net_inet6_ip6, IPV6CTL_GIF_HLIM, gifhlim,
816d8fdfa9SAndrey V. Elsukov CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_gif_hlim), 0,
826d8fdfa9SAndrey V. Elsukov "Default hop limit for encapsulated packets");
839426aedfSHajimu UMEMOTO
84b941bc1dSAndrey V. Elsukov /*
85b941bc1dSAndrey V. Elsukov * We keep interfaces in a hash table using src+dst as key.
86b941bc1dSAndrey V. Elsukov * Interfaces with GIF_IGNORE_SOURCE flag are linked into plain list.
87b941bc1dSAndrey V. Elsukov */
885f901c92SAndrew Turner VNET_DEFINE_STATIC(struct gif_list *, ipv6_hashtbl) = NULL;
89009d82eeSAndrey V. Elsukov VNET_DEFINE_STATIC(struct gif_list *, ipv6_srchashtbl) = NULL;
905f901c92SAndrew Turner VNET_DEFINE_STATIC(struct gif_list, ipv6_list) = CK_LIST_HEAD_INITIALIZER();
91b941bc1dSAndrey V. Elsukov #define V_ipv6_hashtbl VNET(ipv6_hashtbl)
92009d82eeSAndrey V. Elsukov #define V_ipv6_srchashtbl VNET(ipv6_srchashtbl)
93b941bc1dSAndrey V. Elsukov #define V_ipv6_list VNET(ipv6_list)
94b941bc1dSAndrey V. Elsukov
95b941bc1dSAndrey V. Elsukov #define GIF_HASH(src, dst) (V_ipv6_hashtbl[\
96b941bc1dSAndrey V. Elsukov in6_gif_hashval((src), (dst)) & (GIF_HASH_SIZE - 1)])
97009d82eeSAndrey V. Elsukov #define GIF_SRCHASH(src) (V_ipv6_srchashtbl[\
98009d82eeSAndrey V. Elsukov fnv_32_buf((src), sizeof(*src), FNV1_32_INIT) & (GIF_HASH_SIZE - 1)])
99b941bc1dSAndrey V. Elsukov #define GIF_HASH_SC(sc) GIF_HASH(&(sc)->gif_ip6hdr->ip6_src,\
100b941bc1dSAndrey V. Elsukov &(sc)->gif_ip6hdr->ip6_dst)
101b941bc1dSAndrey V. Elsukov static uint32_t
in6_gif_hashval(const struct in6_addr * src,const struct in6_addr * dst)102b941bc1dSAndrey V. Elsukov in6_gif_hashval(const struct in6_addr *src, const struct in6_addr *dst)
103b941bc1dSAndrey V. Elsukov {
104b941bc1dSAndrey V. Elsukov uint32_t ret;
105b941bc1dSAndrey V. Elsukov
106b941bc1dSAndrey V. Elsukov ret = fnv_32_buf(src, sizeof(*src), FNV1_32_INIT);
107b941bc1dSAndrey V. Elsukov return (fnv_32_buf(dst, sizeof(*dst), ret));
108b941bc1dSAndrey V. Elsukov }
109b941bc1dSAndrey V. Elsukov
110b941bc1dSAndrey V. Elsukov static int
in6_gif_checkdup(const struct gif_softc * sc,const struct in6_addr * src,const struct in6_addr * dst)111b941bc1dSAndrey V. Elsukov in6_gif_checkdup(const struct gif_softc *sc, const struct in6_addr *src,
112b941bc1dSAndrey V. Elsukov const struct in6_addr *dst)
113b941bc1dSAndrey V. Elsukov {
114b941bc1dSAndrey V. Elsukov struct gif_softc *tmp;
115b941bc1dSAndrey V. Elsukov
116b941bc1dSAndrey V. Elsukov if (sc->gif_family == AF_INET6 &&
117b941bc1dSAndrey V. Elsukov IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src, src) &&
118b941bc1dSAndrey V. Elsukov IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst, dst))
119b941bc1dSAndrey V. Elsukov return (EEXIST);
120b941bc1dSAndrey V. Elsukov
121b941bc1dSAndrey V. Elsukov CK_LIST_FOREACH(tmp, &GIF_HASH(src, dst), chain) {
122b941bc1dSAndrey V. Elsukov if (tmp == sc)
123b941bc1dSAndrey V. Elsukov continue;
124b941bc1dSAndrey V. Elsukov if (IN6_ARE_ADDR_EQUAL(&tmp->gif_ip6hdr->ip6_src, src) &&
125b941bc1dSAndrey V. Elsukov IN6_ARE_ADDR_EQUAL(&tmp->gif_ip6hdr->ip6_dst, dst))
126b941bc1dSAndrey V. Elsukov return (EADDRNOTAVAIL);
127b941bc1dSAndrey V. Elsukov }
128b941bc1dSAndrey V. Elsukov return (0);
129b941bc1dSAndrey V. Elsukov }
130b941bc1dSAndrey V. Elsukov
131009d82eeSAndrey V. Elsukov /*
132009d82eeSAndrey V. Elsukov * Check that ingress address belongs to local host.
133009d82eeSAndrey V. Elsukov */
134009d82eeSAndrey V. Elsukov static void
in6_gif_set_running(struct gif_softc * sc)135009d82eeSAndrey V. Elsukov in6_gif_set_running(struct gif_softc *sc)
136009d82eeSAndrey V. Elsukov {
137009d82eeSAndrey V. Elsukov
138009d82eeSAndrey V. Elsukov if (in6_localip(&sc->gif_ip6hdr->ip6_src))
139009d82eeSAndrey V. Elsukov GIF2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
140009d82eeSAndrey V. Elsukov else
141009d82eeSAndrey V. Elsukov GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
142009d82eeSAndrey V. Elsukov }
143009d82eeSAndrey V. Elsukov
144009d82eeSAndrey V. Elsukov /*
145009d82eeSAndrey V. Elsukov * ifaddr_event handler.
146009d82eeSAndrey V. Elsukov * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
147009d82eeSAndrey V. Elsukov * source address spoofing.
148009d82eeSAndrey V. Elsukov */
149009d82eeSAndrey V. Elsukov static void
in6_gif_srcaddr(void * arg __unused,const struct sockaddr * sa,int event)150009d82eeSAndrey V. Elsukov in6_gif_srcaddr(void *arg __unused, const struct sockaddr *sa, int event)
151009d82eeSAndrey V. Elsukov {
152009d82eeSAndrey V. Elsukov const struct sockaddr_in6 *sin;
153009d82eeSAndrey V. Elsukov struct gif_softc *sc;
154009d82eeSAndrey V. Elsukov
1558796e291SAndrey V. Elsukov /* Check that VNET is ready */
1568796e291SAndrey V. Elsukov if (V_ipv6_hashtbl == NULL)
157009d82eeSAndrey V. Elsukov return;
158009d82eeSAndrey V. Elsukov
15997168be8SGleb Smirnoff NET_EPOCH_ASSERT();
160009d82eeSAndrey V. Elsukov sin = (const struct sockaddr_in6 *)sa;
161009d82eeSAndrey V. Elsukov CK_LIST_FOREACH(sc, &GIF_SRCHASH(&sin->sin6_addr), srchash) {
162009d82eeSAndrey V. Elsukov if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src,
163009d82eeSAndrey V. Elsukov &sin->sin6_addr) == 0)
164009d82eeSAndrey V. Elsukov continue;
165009d82eeSAndrey V. Elsukov in6_gif_set_running(sc);
166009d82eeSAndrey V. Elsukov }
167009d82eeSAndrey V. Elsukov }
168009d82eeSAndrey V. Elsukov
169b941bc1dSAndrey V. Elsukov static void
in6_gif_attach(struct gif_softc * sc)170b941bc1dSAndrey V. Elsukov in6_gif_attach(struct gif_softc *sc)
171b941bc1dSAndrey V. Elsukov {
172b941bc1dSAndrey V. Elsukov
173b941bc1dSAndrey V. Elsukov if (sc->gif_options & GIF_IGNORE_SOURCE)
174b941bc1dSAndrey V. Elsukov CK_LIST_INSERT_HEAD(&V_ipv6_list, sc, chain);
175b941bc1dSAndrey V. Elsukov else
176b941bc1dSAndrey V. Elsukov CK_LIST_INSERT_HEAD(&GIF_HASH_SC(sc), sc, chain);
177009d82eeSAndrey V. Elsukov
178009d82eeSAndrey V. Elsukov CK_LIST_INSERT_HEAD(&GIF_SRCHASH(&sc->gif_ip6hdr->ip6_src),
179009d82eeSAndrey V. Elsukov sc, srchash);
180b941bc1dSAndrey V. Elsukov }
181b941bc1dSAndrey V. Elsukov
182b941bc1dSAndrey V. Elsukov int
in6_gif_setopts(struct gif_softc * sc,u_int options)183b941bc1dSAndrey V. Elsukov in6_gif_setopts(struct gif_softc *sc, u_int options)
184b941bc1dSAndrey V. Elsukov {
185b941bc1dSAndrey V. Elsukov
186b941bc1dSAndrey V. Elsukov /* NOTE: we are protected with gif_ioctl_sx lock */
187b941bc1dSAndrey V. Elsukov MPASS(sc->gif_family == AF_INET6);
188b941bc1dSAndrey V. Elsukov MPASS(sc->gif_options != options);
189b941bc1dSAndrey V. Elsukov
190b941bc1dSAndrey V. Elsukov if ((options & GIF_IGNORE_SOURCE) !=
191b941bc1dSAndrey V. Elsukov (sc->gif_options & GIF_IGNORE_SOURCE)) {
192009d82eeSAndrey V. Elsukov CK_LIST_REMOVE(sc, srchash);
193b941bc1dSAndrey V. Elsukov CK_LIST_REMOVE(sc, chain);
194b941bc1dSAndrey V. Elsukov sc->gif_options = options;
195b941bc1dSAndrey V. Elsukov in6_gif_attach(sc);
196b941bc1dSAndrey V. Elsukov }
197b941bc1dSAndrey V. Elsukov return (0);
198b941bc1dSAndrey V. Elsukov }
199b941bc1dSAndrey V. Elsukov
200b941bc1dSAndrey V. Elsukov int
in6_gif_ioctl(struct gif_softc * sc,u_long cmd,caddr_t data)201b941bc1dSAndrey V. Elsukov in6_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data)
202b941bc1dSAndrey V. Elsukov {
203b941bc1dSAndrey V. Elsukov struct in6_ifreq *ifr = (struct in6_ifreq *)data;
204b941bc1dSAndrey V. Elsukov struct sockaddr_in6 *dst, *src;
205b941bc1dSAndrey V. Elsukov struct ip6_hdr *ip6;
206b941bc1dSAndrey V. Elsukov int error;
207b941bc1dSAndrey V. Elsukov
208b941bc1dSAndrey V. Elsukov /* NOTE: we are protected with gif_ioctl_sx lock */
209b941bc1dSAndrey V. Elsukov error = EINVAL;
210b941bc1dSAndrey V. Elsukov switch (cmd) {
211b941bc1dSAndrey V. Elsukov case SIOCSIFPHYADDR_IN6:
212b941bc1dSAndrey V. Elsukov src = &((struct in6_aliasreq *)data)->ifra_addr;
213b941bc1dSAndrey V. Elsukov dst = &((struct in6_aliasreq *)data)->ifra_dstaddr;
214b941bc1dSAndrey V. Elsukov
215b941bc1dSAndrey V. Elsukov /* sanity checks */
216b941bc1dSAndrey V. Elsukov if (src->sin6_family != dst->sin6_family ||
217b941bc1dSAndrey V. Elsukov src->sin6_family != AF_INET6 ||
218b941bc1dSAndrey V. Elsukov src->sin6_len != dst->sin6_len ||
219b941bc1dSAndrey V. Elsukov src->sin6_len != sizeof(*src))
220b941bc1dSAndrey V. Elsukov break;
221b941bc1dSAndrey V. Elsukov if (IN6_IS_ADDR_UNSPECIFIED(&src->sin6_addr) ||
222b941bc1dSAndrey V. Elsukov IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) {
223b941bc1dSAndrey V. Elsukov error = EADDRNOTAVAIL;
224b941bc1dSAndrey V. Elsukov break;
225b941bc1dSAndrey V. Elsukov }
226b941bc1dSAndrey V. Elsukov /*
227b941bc1dSAndrey V. Elsukov * Check validity of the scope zone ID of the
228b941bc1dSAndrey V. Elsukov * addresses, and convert it into the kernel
229b941bc1dSAndrey V. Elsukov * internal form if necessary.
230b941bc1dSAndrey V. Elsukov */
231b941bc1dSAndrey V. Elsukov if ((error = sa6_embedscope(src, 0)) != 0 ||
232b941bc1dSAndrey V. Elsukov (error = sa6_embedscope(dst, 0)) != 0)
233b941bc1dSAndrey V. Elsukov break;
234b941bc1dSAndrey V. Elsukov
235009d82eeSAndrey V. Elsukov if (V_ipv6_hashtbl == NULL) {
236b941bc1dSAndrey V. Elsukov V_ipv6_hashtbl = gif_hashinit();
237009d82eeSAndrey V. Elsukov V_ipv6_srchashtbl = gif_hashinit();
238009d82eeSAndrey V. Elsukov }
239b941bc1dSAndrey V. Elsukov error = in6_gif_checkdup(sc, &src->sin6_addr,
240b941bc1dSAndrey V. Elsukov &dst->sin6_addr);
241b941bc1dSAndrey V. Elsukov if (error == EADDRNOTAVAIL)
242b941bc1dSAndrey V. Elsukov break;
243b941bc1dSAndrey V. Elsukov if (error == EEXIST) {
244b941bc1dSAndrey V. Elsukov /* Addresses are the same. Just return. */
245b941bc1dSAndrey V. Elsukov error = 0;
246b941bc1dSAndrey V. Elsukov break;
247b941bc1dSAndrey V. Elsukov }
248b941bc1dSAndrey V. Elsukov ip6 = malloc(sizeof(*ip6), M_GIF, M_WAITOK | M_ZERO);
249b941bc1dSAndrey V. Elsukov ip6->ip6_src = src->sin6_addr;
250b941bc1dSAndrey V. Elsukov ip6->ip6_dst = dst->sin6_addr;
2518065bd0bSAndrey V. Elsukov ip6->ip6_vfc = IPV6_VERSION;
252b941bc1dSAndrey V. Elsukov if (sc->gif_family != 0) {
253b941bc1dSAndrey V. Elsukov /* Detach existing tunnel first */
254009d82eeSAndrey V. Elsukov CK_LIST_REMOVE(sc, srchash);
255b941bc1dSAndrey V. Elsukov CK_LIST_REMOVE(sc, chain);
256b941bc1dSAndrey V. Elsukov GIF_WAIT();
257b941bc1dSAndrey V. Elsukov free(sc->gif_hdr, M_GIF);
258b941bc1dSAndrey V. Elsukov /* XXX: should we notify about link state change? */
259b941bc1dSAndrey V. Elsukov }
260b941bc1dSAndrey V. Elsukov sc->gif_family = AF_INET6;
261b941bc1dSAndrey V. Elsukov sc->gif_ip6hdr = ip6;
262b941bc1dSAndrey V. Elsukov in6_gif_attach(sc);
263009d82eeSAndrey V. Elsukov in6_gif_set_running(sc);
264b941bc1dSAndrey V. Elsukov break;
265b941bc1dSAndrey V. Elsukov case SIOCGIFPSRCADDR_IN6:
266b941bc1dSAndrey V. Elsukov case SIOCGIFPDSTADDR_IN6:
267b941bc1dSAndrey V. Elsukov if (sc->gif_family != AF_INET6) {
268b941bc1dSAndrey V. Elsukov error = EADDRNOTAVAIL;
269b941bc1dSAndrey V. Elsukov break;
270b941bc1dSAndrey V. Elsukov }
271b941bc1dSAndrey V. Elsukov src = (struct sockaddr_in6 *)&ifr->ifr_addr;
272b941bc1dSAndrey V. Elsukov memset(src, 0, sizeof(*src));
273b941bc1dSAndrey V. Elsukov src->sin6_family = AF_INET6;
274b941bc1dSAndrey V. Elsukov src->sin6_len = sizeof(*src);
275b941bc1dSAndrey V. Elsukov src->sin6_addr = (cmd == SIOCGIFPSRCADDR_IN6) ?
276b941bc1dSAndrey V. Elsukov sc->gif_ip6hdr->ip6_src: sc->gif_ip6hdr->ip6_dst;
277b941bc1dSAndrey V. Elsukov error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
278b941bc1dSAndrey V. Elsukov if (error == 0)
279b941bc1dSAndrey V. Elsukov error = sa6_recoverscope(src);
280b941bc1dSAndrey V. Elsukov if (error != 0)
281b941bc1dSAndrey V. Elsukov memset(src, 0, sizeof(*src));
282b941bc1dSAndrey V. Elsukov break;
283b941bc1dSAndrey V. Elsukov }
284b941bc1dSAndrey V. Elsukov return (error);
285b941bc1dSAndrey V. Elsukov }
286b941bc1dSAndrey V. Elsukov
287cfa1ca9dSYoshinobu Inoue int
in6_gif_output(struct ifnet * ifp,struct mbuf * m,int proto,uint8_t ecn)2880b9f5f8aSAndrey V. Elsukov in6_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
289cfa1ca9dSYoshinobu Inoue {
290fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc;
291cfa1ca9dSYoshinobu Inoue struct ip6_hdr *ip6;
292cfa1ca9dSYoshinobu Inoue
293cfa1ca9dSYoshinobu Inoue /* prepend new IP header */
29497168be8SGleb Smirnoff NET_EPOCH_ASSERT();
295*e82d7b29SMarius Strobl M_PREPEND(m, sizeof(struct ip6_hdr), M_NOWAIT);
2960b9f5f8aSAndrey V. Elsukov if (m == NULL)
2970b9f5f8aSAndrey V. Elsukov return (ENOBUFS);
298cfa1ca9dSYoshinobu Inoue
299cfa1ca9dSYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *);
300b941bc1dSAndrey V. Elsukov MPASS(sc->gif_family == AF_INET6);
301*e82d7b29SMarius Strobl memcpy(ip6, sc->gif_ip6hdr, sizeof(struct ip6_hdr));
3020b9f5f8aSAndrey V. Elsukov
3030b9f5f8aSAndrey V. Elsukov ip6->ip6_flow |= htonl((uint32_t)ecn << 20);
304cfa1ca9dSYoshinobu Inoue ip6->ip6_nxt = proto;
305603724d3SBjoern A. Zeeb ip6->ip6_hlim = V_ip6_gif_hlim;
306686cdd19SJun-ichiro itojun Hagino /*
307686cdd19SJun-ichiro itojun Hagino * force fragmentation to minimum MTU, to avoid path MTU discovery.
308686cdd19SJun-ichiro itojun Hagino * it is too painful to ask for resend of inner packet, to achieve
309686cdd19SJun-ichiro itojun Hagino * path MTU discovery for encapsulated packets.
310686cdd19SJun-ichiro itojun Hagino */
3110b9f5f8aSAndrey V. Elsukov return (ip6_output(m, 0, NULL, IPV6_MINMTU, 0, NULL, NULL));
312cfa1ca9dSYoshinobu Inoue }
313cfa1ca9dSYoshinobu Inoue
314132c4490SAndrey V. Elsukov static int
in6_gif_input(struct mbuf * m,int off,int proto,void * arg)3156d8fdfa9SAndrey V. Elsukov in6_gif_input(struct mbuf *m, int off, int proto, void *arg)
316cfa1ca9dSYoshinobu Inoue {
3176d8fdfa9SAndrey V. Elsukov struct gif_softc *sc = arg;
3180b9f5f8aSAndrey V. Elsukov struct ifnet *gifp;
319cfa1ca9dSYoshinobu Inoue struct ip6_hdr *ip6;
3200b9f5f8aSAndrey V. Elsukov uint8_t ecn;
321cfa1ca9dSYoshinobu Inoue
32297168be8SGleb Smirnoff NET_EPOCH_ASSERT();
323d098c2c1SHajimu UMEMOTO if (sc == NULL) {
324d098c2c1SHajimu UMEMOTO m_freem(m);
3259cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_nogif);
3260b9f5f8aSAndrey V. Elsukov return (IPPROTO_DONE);
327d098c2c1SHajimu UMEMOTO }
328d098c2c1SHajimu UMEMOTO gifp = GIF2IFP(sc);
3290b9f5f8aSAndrey V. Elsukov if ((gifp->if_flags & IFF_UP) != 0) {
330cfa1ca9dSYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *);
331bb4a7d94SKristof Provost ecn = IPV6_TRAFFIC_CLASS(ip6);
3326d8fdfa9SAndrey V. Elsukov m_adj(m, off);
3330b9f5f8aSAndrey V. Elsukov gif_input(m, gifp, proto, ecn);
3340b9f5f8aSAndrey V. Elsukov } else {
33559dfcba4SHajimu UMEMOTO m_freem(m);
3369cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_nogif);
337cfa1ca9dSYoshinobu Inoue }
3380b9f5f8aSAndrey V. Elsukov return (IPPROTO_DONE);
339cfa1ca9dSYoshinobu Inoue }
340686cdd19SJun-ichiro itojun Hagino
341b941bc1dSAndrey V. Elsukov static int
in6_gif_lookup(const struct mbuf * m,int off,int proto,void ** arg)342b941bc1dSAndrey V. Elsukov in6_gif_lookup(const struct mbuf *m, int off, int proto, void **arg)
3439426aedfSHajimu UMEMOTO {
34410a0e0bfSAndrey V. Elsukov const struct ip6_hdr *ip6;
34510a0e0bfSAndrey V. Elsukov struct gif_softc *sc;
346c1b4f79dSAndrey V. Elsukov int ret;
347686cdd19SJun-ichiro itojun Hagino
3486e081509SAndrey V. Elsukov if (V_ipv6_hashtbl == NULL)
3496e081509SAndrey V. Elsukov return (0);
3506e081509SAndrey V. Elsukov
35197168be8SGleb Smirnoff NET_EPOCH_ASSERT();
3529426aedfSHajimu UMEMOTO /*
353b941bc1dSAndrey V. Elsukov * NOTE: it is safe to iterate without any locking here, because softc
354b941bc1dSAndrey V. Elsukov * can be reclaimed only when we are not within net_epoch_preempt
355b941bc1dSAndrey V. Elsukov * section, but ip_encap lookup+input are executed in epoch section.
3569426aedfSHajimu UMEMOTO */
35710a0e0bfSAndrey V. Elsukov ip6 = mtod(m, const struct ip6_hdr *);
358b941bc1dSAndrey V. Elsukov ret = 0;
359b941bc1dSAndrey V. Elsukov CK_LIST_FOREACH(sc, &GIF_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) {
360b941bc1dSAndrey V. Elsukov /*
361b941bc1dSAndrey V. Elsukov * This is an inbound packet, its ip6_dst is source address
362b941bc1dSAndrey V. Elsukov * in softc.
363b941bc1dSAndrey V. Elsukov */
364b941bc1dSAndrey V. Elsukov if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src,
365b941bc1dSAndrey V. Elsukov &ip6->ip6_dst) &&
366b941bc1dSAndrey V. Elsukov IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_dst,
367b941bc1dSAndrey V. Elsukov &ip6->ip6_src)) {
368b941bc1dSAndrey V. Elsukov ret = ENCAP_DRV_LOOKUP;
369b941bc1dSAndrey V. Elsukov goto done;
370b941bc1dSAndrey V. Elsukov }
371b941bc1dSAndrey V. Elsukov }
372b941bc1dSAndrey V. Elsukov /*
373b941bc1dSAndrey V. Elsukov * No exact match.
374b941bc1dSAndrey V. Elsukov * Check the list of interfaces with GIF_IGNORE_SOURCE flag.
375b941bc1dSAndrey V. Elsukov */
376b941bc1dSAndrey V. Elsukov CK_LIST_FOREACH(sc, &V_ipv6_list, chain) {
377b941bc1dSAndrey V. Elsukov if (IN6_ARE_ADDR_EQUAL(&sc->gif_ip6hdr->ip6_src,
378b941bc1dSAndrey V. Elsukov &ip6->ip6_dst)) {
379b941bc1dSAndrey V. Elsukov ret = 128 + 8; /* src + proto */
380b941bc1dSAndrey V. Elsukov goto done;
381b941bc1dSAndrey V. Elsukov }
382b941bc1dSAndrey V. Elsukov }
3830b9f5f8aSAndrey V. Elsukov return (0);
384b941bc1dSAndrey V. Elsukov done:
385b941bc1dSAndrey V. Elsukov if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
386c1b4f79dSAndrey V. Elsukov return (0);
387686cdd19SJun-ichiro itojun Hagino /* ingress filters on outer source */
38810a0e0bfSAndrey V. Elsukov if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
3897bfc98afSAlexander V. Chernikov if (fib6_check_urpf(sc->gif_fibnum, &ip6->ip6_src,
3907bfc98afSAlexander V. Chernikov ntohs(in6_getscope(&ip6->ip6_src)), NHR_NONE,
3917bfc98afSAlexander V. Chernikov m->m_pkthdr.rcvif) == 0)
39265ff3638SAlexander V. Chernikov return (0);
393686cdd19SJun-ichiro itojun Hagino }
394b941bc1dSAndrey V. Elsukov *arg = sc;
395c1b4f79dSAndrey V. Elsukov return (ret);
396686cdd19SJun-ichiro itojun Hagino }
3979426aedfSHajimu UMEMOTO
398009d82eeSAndrey V. Elsukov static const struct srcaddrtab *ipv6_srcaddrtab;
399b941bc1dSAndrey V. Elsukov static struct {
400b941bc1dSAndrey V. Elsukov const struct encap_config encap;
401b941bc1dSAndrey V. Elsukov const struct encaptab *cookie;
402b941bc1dSAndrey V. Elsukov } ipv6_encap_cfg[] = {
403b941bc1dSAndrey V. Elsukov #ifdef INET
404b941bc1dSAndrey V. Elsukov {
405b941bc1dSAndrey V. Elsukov .encap = {
406b941bc1dSAndrey V. Elsukov .proto = IPPROTO_IPV4,
407b941bc1dSAndrey V. Elsukov .min_length = sizeof(struct ip6_hdr) +
408b941bc1dSAndrey V. Elsukov sizeof(struct ip),
409b941bc1dSAndrey V. Elsukov .exact_match = ENCAP_DRV_LOOKUP,
410b941bc1dSAndrey V. Elsukov .lookup = in6_gif_lookup,
4116d8fdfa9SAndrey V. Elsukov .input = in6_gif_input
412b941bc1dSAndrey V. Elsukov },
413b941bc1dSAndrey V. Elsukov },
414b941bc1dSAndrey V. Elsukov #endif
415b941bc1dSAndrey V. Elsukov {
416b941bc1dSAndrey V. Elsukov .encap = {
417b941bc1dSAndrey V. Elsukov .proto = IPPROTO_IPV6,
418b941bc1dSAndrey V. Elsukov .min_length = 2 * sizeof(struct ip6_hdr),
419b941bc1dSAndrey V. Elsukov .exact_match = ENCAP_DRV_LOOKUP,
420b941bc1dSAndrey V. Elsukov .lookup = in6_gif_lookup,
421b941bc1dSAndrey V. Elsukov .input = in6_gif_input
422b941bc1dSAndrey V. Elsukov },
423b941bc1dSAndrey V. Elsukov },
424b941bc1dSAndrey V. Elsukov {
425b941bc1dSAndrey V. Elsukov .encap = {
426b941bc1dSAndrey V. Elsukov .proto = IPPROTO_ETHERIP,
427b941bc1dSAndrey V. Elsukov .min_length = sizeof(struct ip6_hdr) +
428b941bc1dSAndrey V. Elsukov sizeof(struct etherip_header) +
429b941bc1dSAndrey V. Elsukov sizeof(struct ether_header),
430b941bc1dSAndrey V. Elsukov .exact_match = ENCAP_DRV_LOOKUP,
431b941bc1dSAndrey V. Elsukov .lookup = in6_gif_lookup,
432b941bc1dSAndrey V. Elsukov .input = in6_gif_input
433b941bc1dSAndrey V. Elsukov },
434b941bc1dSAndrey V. Elsukov }
4356d8fdfa9SAndrey V. Elsukov };
4366d8fdfa9SAndrey V. Elsukov
437b941bc1dSAndrey V. Elsukov void
in6_gif_init(void)438b941bc1dSAndrey V. Elsukov in6_gif_init(void)
4399426aedfSHajimu UMEMOTO {
440b941bc1dSAndrey V. Elsukov int i;
4410b9f5f8aSAndrey V. Elsukov
442b941bc1dSAndrey V. Elsukov if (!IS_DEFAULT_VNET(curvnet))
443b941bc1dSAndrey V. Elsukov return;
444009d82eeSAndrey V. Elsukov
445009d82eeSAndrey V. Elsukov ipv6_srcaddrtab = ip6_encap_register_srcaddr(in6_gif_srcaddr,
446009d82eeSAndrey V. Elsukov NULL, M_WAITOK);
447b941bc1dSAndrey V. Elsukov for (i = 0; i < nitems(ipv6_encap_cfg); i++)
448b941bc1dSAndrey V. Elsukov ipv6_encap_cfg[i].cookie = ip6_encap_attach(
449b941bc1dSAndrey V. Elsukov &ipv6_encap_cfg[i].encap, NULL, M_WAITOK);
450b941bc1dSAndrey V. Elsukov }
451b941bc1dSAndrey V. Elsukov
452b941bc1dSAndrey V. Elsukov void
in6_gif_uninit(void)453b941bc1dSAndrey V. Elsukov in6_gif_uninit(void)
454b941bc1dSAndrey V. Elsukov {
455b941bc1dSAndrey V. Elsukov int i;
456b941bc1dSAndrey V. Elsukov
457b941bc1dSAndrey V. Elsukov if (IS_DEFAULT_VNET(curvnet)) {
458b941bc1dSAndrey V. Elsukov for (i = 0; i < nitems(ipv6_encap_cfg); i++)
459b941bc1dSAndrey V. Elsukov ip6_encap_detach(ipv6_encap_cfg[i].cookie);
460009d82eeSAndrey V. Elsukov ip6_encap_unregister_srcaddr(ipv6_srcaddrtab);
461b941bc1dSAndrey V. Elsukov }
462009d82eeSAndrey V. Elsukov if (V_ipv6_hashtbl != NULL) {
463b941bc1dSAndrey V. Elsukov gif_hashdestroy(V_ipv6_hashtbl);
4648796e291SAndrey V. Elsukov V_ipv6_hashtbl = NULL;
4658796e291SAndrey V. Elsukov GIF_WAIT();
466009d82eeSAndrey V. Elsukov gif_hashdestroy(V_ipv6_srchashtbl);
467009d82eeSAndrey V. Elsukov }
4689426aedfSHajimu UMEMOTO }
469