1c398230bSWarner 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.
310b9f5f8aSAndrey V. Elsukov *
320b9f5f8aSAndrey V. Elsukov * $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $
33cfa1ca9dSYoshinobu Inoue */
34cfa1ca9dSYoshinobu Inoue
354b421e2dSMike Silbersack #include <sys/cdefs.h>
36686cdd19SJun-ichiro itojun Hagino #include "opt_inet.h"
37cfa1ca9dSYoshinobu Inoue #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>
46cfa1ca9dSYoshinobu Inoue #include <sys/kernel.h>
47cfa1ca9dSYoshinobu Inoue #include <sys/sysctl.h>
48686cdd19SJun-ichiro itojun Hagino #include <sys/malloc.h>
496573d758SMatt Macy #include <sys/proc.h>
50cfa1ca9dSYoshinobu Inoue
51b941bc1dSAndrey V. Elsukov #include <net/ethernet.h>
52cfa1ca9dSYoshinobu Inoue #include <net/if.h>
5376039bc8SGleb Smirnoff #include <net/if_var.h>
543d0d5b21SJustin Hibbits #include <net/if_private.h>
55cfa1ca9dSYoshinobu Inoue #include <net/route.h>
56530c0060SRobert Watson #include <net/vnet.h>
57cfa1ca9dSYoshinobu Inoue
58cfa1ca9dSYoshinobu Inoue #include <netinet/in.h>
59cfa1ca9dSYoshinobu Inoue #include <netinet/in_systm.h>
60cfa1ca9dSYoshinobu Inoue #include <netinet/ip.h>
616a800098SYoshinobu Inoue #include <netinet/ip_var.h>
62686cdd19SJun-ichiro itojun Hagino #include <netinet/in_var.h>
63686cdd19SJun-ichiro itojun Hagino #include <netinet/ip_encap.h>
646a800098SYoshinobu Inoue #include <netinet/ip_ecn.h>
6565ff3638SAlexander V. Chernikov #include <netinet/in_fib.h>
66686cdd19SJun-ichiro itojun Hagino
676a800098SYoshinobu Inoue #ifdef INET6
68686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h>
696a800098SYoshinobu Inoue #endif
70cfa1ca9dSYoshinobu Inoue
71cfa1ca9dSYoshinobu Inoue #include <net/if_gif.h>
72cfa1ca9dSYoshinobu Inoue
73132c4490SAndrey V. Elsukov #define GIF_TTL 30
745f901c92SAndrew Turner VNET_DEFINE_STATIC(int, ip_gif_ttl) = GIF_TTL;
7582cea7e6SBjoern A. Zeeb #define V_ip_gif_ttl VNET(ip_gif_ttl)
766df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_VNET | CTLFLAG_RW,
776d8fdfa9SAndrey V. Elsukov &VNET_NAME(ip_gif_ttl), 0, "Default TTL value for encapsulated packets");
78cfa1ca9dSYoshinobu Inoue
79b941bc1dSAndrey V. Elsukov /*
80b941bc1dSAndrey V. Elsukov * We keep interfaces in a hash table using src+dst as key.
81b941bc1dSAndrey V. Elsukov * Interfaces with GIF_IGNORE_SOURCE flag are linked into plain list.
82b941bc1dSAndrey V. Elsukov */
835f901c92SAndrew Turner VNET_DEFINE_STATIC(struct gif_list *, ipv4_hashtbl) = NULL;
84009d82eeSAndrey V. Elsukov VNET_DEFINE_STATIC(struct gif_list *, ipv4_srchashtbl) = NULL;
855f901c92SAndrew Turner VNET_DEFINE_STATIC(struct gif_list, ipv4_list) = CK_LIST_HEAD_INITIALIZER();
86b941bc1dSAndrey V. Elsukov #define V_ipv4_hashtbl VNET(ipv4_hashtbl)
87009d82eeSAndrey V. Elsukov #define V_ipv4_srchashtbl VNET(ipv4_srchashtbl)
88b941bc1dSAndrey V. Elsukov #define V_ipv4_list VNET(ipv4_list)
89b941bc1dSAndrey V. Elsukov
90b941bc1dSAndrey V. Elsukov #define GIF_HASH(src, dst) (V_ipv4_hashtbl[\
91b941bc1dSAndrey V. Elsukov in_gif_hashval((src), (dst)) & (GIF_HASH_SIZE - 1)])
92009d82eeSAndrey V. Elsukov #define GIF_SRCHASH(src) (V_ipv4_srchashtbl[\
93009d82eeSAndrey V. Elsukov fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (GIF_HASH_SIZE - 1)])
94b941bc1dSAndrey V. Elsukov #define GIF_HASH_SC(sc) GIF_HASH((sc)->gif_iphdr->ip_src.s_addr,\
95b941bc1dSAndrey V. Elsukov (sc)->gif_iphdr->ip_dst.s_addr)
96b941bc1dSAndrey V. Elsukov static uint32_t
in_gif_hashval(in_addr_t src,in_addr_t dst)97b941bc1dSAndrey V. Elsukov in_gif_hashval(in_addr_t src, in_addr_t dst)
98b941bc1dSAndrey V. Elsukov {
99b941bc1dSAndrey V. Elsukov uint32_t ret;
100b941bc1dSAndrey V. Elsukov
101b941bc1dSAndrey V. Elsukov ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
102b941bc1dSAndrey V. Elsukov return (fnv_32_buf(&dst, sizeof(dst), ret));
103b941bc1dSAndrey V. Elsukov }
104b941bc1dSAndrey V. Elsukov
105b941bc1dSAndrey V. Elsukov static int
in_gif_checkdup(const struct gif_softc * sc,in_addr_t src,in_addr_t dst)106b941bc1dSAndrey V. Elsukov in_gif_checkdup(const struct gif_softc *sc, in_addr_t src, in_addr_t dst)
107b941bc1dSAndrey V. Elsukov {
108b941bc1dSAndrey V. Elsukov struct gif_softc *tmp;
109b941bc1dSAndrey V. Elsukov
110b941bc1dSAndrey V. Elsukov if (sc->gif_family == AF_INET &&
111b941bc1dSAndrey V. Elsukov sc->gif_iphdr->ip_src.s_addr == src &&
112b941bc1dSAndrey V. Elsukov sc->gif_iphdr->ip_dst.s_addr == dst)
113b941bc1dSAndrey V. Elsukov return (EEXIST);
114b941bc1dSAndrey V. Elsukov
115b941bc1dSAndrey V. Elsukov CK_LIST_FOREACH(tmp, &GIF_HASH(src, dst), chain) {
116b941bc1dSAndrey V. Elsukov if (tmp == sc)
117b941bc1dSAndrey V. Elsukov continue;
118b941bc1dSAndrey V. Elsukov if (tmp->gif_iphdr->ip_src.s_addr == src &&
119b941bc1dSAndrey V. Elsukov tmp->gif_iphdr->ip_dst.s_addr == dst)
120b941bc1dSAndrey V. Elsukov return (EADDRNOTAVAIL);
121b941bc1dSAndrey V. Elsukov }
122b941bc1dSAndrey V. Elsukov return (0);
123b941bc1dSAndrey V. Elsukov }
124b941bc1dSAndrey V. Elsukov
125009d82eeSAndrey V. Elsukov /*
126009d82eeSAndrey V. Elsukov * Check that ingress address belongs to local host.
127009d82eeSAndrey V. Elsukov */
128009d82eeSAndrey V. Elsukov static void
in_gif_set_running(struct gif_softc * sc)129009d82eeSAndrey V. Elsukov in_gif_set_running(struct gif_softc *sc)
130009d82eeSAndrey V. Elsukov {
131009d82eeSAndrey V. Elsukov
132009d82eeSAndrey V. Elsukov if (in_localip(sc->gif_iphdr->ip_src))
133009d82eeSAndrey V. Elsukov GIF2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
134009d82eeSAndrey V. Elsukov else
135009d82eeSAndrey V. Elsukov GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
136009d82eeSAndrey V. Elsukov }
137009d82eeSAndrey V. Elsukov
138009d82eeSAndrey V. Elsukov /*
139009d82eeSAndrey V. Elsukov * ifaddr_event handler.
140009d82eeSAndrey V. Elsukov * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
141009d82eeSAndrey V. Elsukov * source address spoofing.
142009d82eeSAndrey V. Elsukov */
143009d82eeSAndrey V. Elsukov static void
in_gif_srcaddr(void * arg __unused,const struct sockaddr * sa,int event __unused)144009d82eeSAndrey V. Elsukov in_gif_srcaddr(void *arg __unused, const struct sockaddr *sa,
145009d82eeSAndrey V. Elsukov int event __unused)
146009d82eeSAndrey V. Elsukov {
147009d82eeSAndrey V. Elsukov const struct sockaddr_in *sin;
148009d82eeSAndrey V. Elsukov struct gif_softc *sc;
149009d82eeSAndrey V. Elsukov
1508796e291SAndrey V. Elsukov /* Check that VNET is ready */
1518796e291SAndrey V. Elsukov if (V_ipv4_hashtbl == NULL)
152009d82eeSAndrey V. Elsukov return;
153009d82eeSAndrey V. Elsukov
15497168be8SGleb Smirnoff NET_EPOCH_ASSERT();
155009d82eeSAndrey V. Elsukov sin = (const struct sockaddr_in *)sa;
156009d82eeSAndrey V. Elsukov CK_LIST_FOREACH(sc, &GIF_SRCHASH(sin->sin_addr.s_addr), srchash) {
157009d82eeSAndrey V. Elsukov if (sc->gif_iphdr->ip_src.s_addr != sin->sin_addr.s_addr)
158009d82eeSAndrey V. Elsukov continue;
159009d82eeSAndrey V. Elsukov in_gif_set_running(sc);
160009d82eeSAndrey V. Elsukov }
161009d82eeSAndrey V. Elsukov }
162009d82eeSAndrey V. Elsukov
163b941bc1dSAndrey V. Elsukov static void
in_gif_attach(struct gif_softc * sc)164b941bc1dSAndrey V. Elsukov in_gif_attach(struct gif_softc *sc)
165b941bc1dSAndrey V. Elsukov {
166b941bc1dSAndrey V. Elsukov
167b941bc1dSAndrey V. Elsukov if (sc->gif_options & GIF_IGNORE_SOURCE)
168b941bc1dSAndrey V. Elsukov CK_LIST_INSERT_HEAD(&V_ipv4_list, sc, chain);
169b941bc1dSAndrey V. Elsukov else
170b941bc1dSAndrey V. Elsukov CK_LIST_INSERT_HEAD(&GIF_HASH_SC(sc), sc, chain);
171009d82eeSAndrey V. Elsukov
172009d82eeSAndrey V. Elsukov CK_LIST_INSERT_HEAD(&GIF_SRCHASH(sc->gif_iphdr->ip_src.s_addr),
173009d82eeSAndrey V. Elsukov sc, srchash);
174b941bc1dSAndrey V. Elsukov }
175b941bc1dSAndrey V. Elsukov
176b941bc1dSAndrey V. Elsukov int
in_gif_setopts(struct gif_softc * sc,u_int options)177b941bc1dSAndrey V. Elsukov in_gif_setopts(struct gif_softc *sc, u_int options)
178b941bc1dSAndrey V. Elsukov {
179b941bc1dSAndrey V. Elsukov
180b941bc1dSAndrey V. Elsukov /* NOTE: we are protected with gif_ioctl_sx lock */
181b941bc1dSAndrey V. Elsukov MPASS(sc->gif_family == AF_INET);
182b941bc1dSAndrey V. Elsukov MPASS(sc->gif_options != options);
183b941bc1dSAndrey V. Elsukov
184b941bc1dSAndrey V. Elsukov if ((options & GIF_IGNORE_SOURCE) !=
185b941bc1dSAndrey V. Elsukov (sc->gif_options & GIF_IGNORE_SOURCE)) {
186009d82eeSAndrey V. Elsukov CK_LIST_REMOVE(sc, srchash);
187b941bc1dSAndrey V. Elsukov CK_LIST_REMOVE(sc, chain);
188b941bc1dSAndrey V. Elsukov sc->gif_options = options;
189b941bc1dSAndrey V. Elsukov in_gif_attach(sc);
190b941bc1dSAndrey V. Elsukov }
191b941bc1dSAndrey V. Elsukov return (0);
192b941bc1dSAndrey V. Elsukov }
193b941bc1dSAndrey V. Elsukov
194b941bc1dSAndrey V. Elsukov int
in_gif_ioctl(struct gif_softc * sc,u_long cmd,caddr_t data)195b941bc1dSAndrey V. Elsukov in_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data)
196b941bc1dSAndrey V. Elsukov {
197b941bc1dSAndrey V. Elsukov struct ifreq *ifr = (struct ifreq *)data;
198c8ee75f2SGleb Smirnoff struct epoch_tracker et;
199b941bc1dSAndrey V. Elsukov struct sockaddr_in *dst, *src;
200b941bc1dSAndrey V. Elsukov struct ip *ip;
201b941bc1dSAndrey V. Elsukov int error;
202b941bc1dSAndrey V. Elsukov
203b941bc1dSAndrey V. Elsukov /* NOTE: we are protected with gif_ioctl_sx lock */
204b941bc1dSAndrey V. Elsukov error = EINVAL;
205b941bc1dSAndrey V. Elsukov switch (cmd) {
206b941bc1dSAndrey V. Elsukov case SIOCSIFPHYADDR:
207b941bc1dSAndrey V. Elsukov src = &((struct in_aliasreq *)data)->ifra_addr;
208b941bc1dSAndrey V. Elsukov dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
209b941bc1dSAndrey V. Elsukov
210b941bc1dSAndrey V. Elsukov /* sanity checks */
211b941bc1dSAndrey V. Elsukov if (src->sin_family != dst->sin_family ||
212b941bc1dSAndrey V. Elsukov src->sin_family != AF_INET ||
213b941bc1dSAndrey V. Elsukov src->sin_len != dst->sin_len ||
214b941bc1dSAndrey V. Elsukov src->sin_len != sizeof(*src))
215b941bc1dSAndrey V. Elsukov break;
216b941bc1dSAndrey V. Elsukov if (src->sin_addr.s_addr == INADDR_ANY ||
217b941bc1dSAndrey V. Elsukov dst->sin_addr.s_addr == INADDR_ANY) {
218b941bc1dSAndrey V. Elsukov error = EADDRNOTAVAIL;
219b941bc1dSAndrey V. Elsukov break;
220b941bc1dSAndrey V. Elsukov }
221009d82eeSAndrey V. Elsukov if (V_ipv4_hashtbl == NULL) {
222b941bc1dSAndrey V. Elsukov V_ipv4_hashtbl = gif_hashinit();
223009d82eeSAndrey V. Elsukov V_ipv4_srchashtbl = gif_hashinit();
224009d82eeSAndrey V. Elsukov }
225b941bc1dSAndrey V. Elsukov error = in_gif_checkdup(sc, src->sin_addr.s_addr,
226b941bc1dSAndrey V. Elsukov dst->sin_addr.s_addr);
227b941bc1dSAndrey V. Elsukov if (error == EADDRNOTAVAIL)
228b941bc1dSAndrey V. Elsukov break;
229b941bc1dSAndrey V. Elsukov if (error == EEXIST) {
230b941bc1dSAndrey V. Elsukov /* Addresses are the same. Just return. */
231b941bc1dSAndrey V. Elsukov error = 0;
232b941bc1dSAndrey V. Elsukov break;
233b941bc1dSAndrey V. Elsukov }
234b941bc1dSAndrey V. Elsukov ip = malloc(sizeof(*ip), M_GIF, M_WAITOK | M_ZERO);
235b941bc1dSAndrey V. Elsukov ip->ip_src.s_addr = src->sin_addr.s_addr;
236b941bc1dSAndrey V. Elsukov ip->ip_dst.s_addr = dst->sin_addr.s_addr;
237b941bc1dSAndrey V. Elsukov if (sc->gif_family != 0) {
238b941bc1dSAndrey V. Elsukov /* Detach existing tunnel first */
239009d82eeSAndrey V. Elsukov CK_LIST_REMOVE(sc, srchash);
240b941bc1dSAndrey V. Elsukov CK_LIST_REMOVE(sc, chain);
241b941bc1dSAndrey V. Elsukov GIF_WAIT();
242b941bc1dSAndrey V. Elsukov free(sc->gif_hdr, M_GIF);
243b941bc1dSAndrey V. Elsukov /* XXX: should we notify about link state change? */
244b941bc1dSAndrey V. Elsukov }
245b941bc1dSAndrey V. Elsukov sc->gif_family = AF_INET;
246b941bc1dSAndrey V. Elsukov sc->gif_iphdr = ip;
247b941bc1dSAndrey V. Elsukov in_gif_attach(sc);
248c8ee75f2SGleb Smirnoff NET_EPOCH_ENTER(et);
249009d82eeSAndrey V. Elsukov in_gif_set_running(sc);
250c8ee75f2SGleb Smirnoff NET_EPOCH_EXIT(et);
251b941bc1dSAndrey V. Elsukov break;
252b941bc1dSAndrey V. Elsukov case SIOCGIFPSRCADDR:
253b941bc1dSAndrey V. Elsukov case SIOCGIFPDSTADDR:
254b941bc1dSAndrey V. Elsukov if (sc->gif_family != AF_INET) {
255b941bc1dSAndrey V. Elsukov error = EADDRNOTAVAIL;
256b941bc1dSAndrey V. Elsukov break;
257b941bc1dSAndrey V. Elsukov }
258b941bc1dSAndrey V. Elsukov src = (struct sockaddr_in *)&ifr->ifr_addr;
259b941bc1dSAndrey V. Elsukov memset(src, 0, sizeof(*src));
260b941bc1dSAndrey V. Elsukov src->sin_family = AF_INET;
261b941bc1dSAndrey V. Elsukov src->sin_len = sizeof(*src);
262b941bc1dSAndrey V. Elsukov src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
263b941bc1dSAndrey V. Elsukov sc->gif_iphdr->ip_src: sc->gif_iphdr->ip_dst;
264b941bc1dSAndrey V. Elsukov error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
265b941bc1dSAndrey V. Elsukov if (error != 0)
266b941bc1dSAndrey V. Elsukov memset(src, 0, sizeof(*src));
267b941bc1dSAndrey V. Elsukov break;
268b941bc1dSAndrey V. Elsukov }
269b941bc1dSAndrey V. Elsukov return (error);
270b941bc1dSAndrey V. Elsukov }
271b941bc1dSAndrey V. Elsukov
272cfa1ca9dSYoshinobu Inoue int
in_gif_output(struct ifnet * ifp,struct mbuf * m,int proto,uint8_t ecn)2730b9f5f8aSAndrey V. Elsukov in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
274cfa1ca9dSYoshinobu Inoue {
275fc74a9f9SBrooks Davis struct gif_softc *sc = ifp->if_softc;
276cfa1ca9dSYoshinobu Inoue struct ip *ip;
277cfa1ca9dSYoshinobu Inoue
278cfa1ca9dSYoshinobu Inoue /* prepend new IP header */
27997168be8SGleb Smirnoff NET_EPOCH_ASSERT();
280*e82d7b29SMarius Strobl M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
2810b9f5f8aSAndrey V. Elsukov if (m == NULL)
2820b9f5f8aSAndrey V. Elsukov return (ENOBUFS);
2830b9f5f8aSAndrey V. Elsukov ip = mtod(m, struct ip *);
284686cdd19SJun-ichiro itojun Hagino
285b941bc1dSAndrey V. Elsukov MPASS(sc->gif_family == AF_INET);
286*e82d7b29SMarius Strobl memcpy(ip, sc->gif_iphdr, sizeof(struct ip));
2870b9f5f8aSAndrey V. Elsukov ip->ip_p = proto;
2880b9f5f8aSAndrey V. Elsukov /* version will be set in ip_output() */
2890b9f5f8aSAndrey V. Elsukov ip->ip_ttl = V_ip_gif_ttl;
2900b9f5f8aSAndrey V. Elsukov ip->ip_len = htons(m->m_pkthdr.len);
2910b9f5f8aSAndrey V. Elsukov ip->ip_tos = ecn;
292cfa1ca9dSYoshinobu Inoue
2930b9f5f8aSAndrey V. Elsukov return (ip_output(m, NULL, NULL, 0, NULL, NULL));
294cfa1ca9dSYoshinobu Inoue }
295cfa1ca9dSYoshinobu Inoue
296132c4490SAndrey V. Elsukov static int
in_gif_input(struct mbuf * m,int off,int proto,void * arg)2976d8fdfa9SAndrey V. Elsukov in_gif_input(struct mbuf *m, int off, int proto, void *arg)
298cfa1ca9dSYoshinobu Inoue {
2996d8fdfa9SAndrey V. Elsukov struct gif_softc *sc = arg;
3000b9f5f8aSAndrey V. Elsukov struct ifnet *gifp;
301cfa1ca9dSYoshinobu Inoue struct ip *ip;
3020b9f5f8aSAndrey V. Elsukov uint8_t ecn;
303cfa1ca9dSYoshinobu Inoue
30497168be8SGleb Smirnoff NET_EPOCH_ASSERT();
30567df9f38SBjoern A. Zeeb if (sc == NULL) {
30667df9f38SBjoern A. Zeeb m_freem(m);
307315e3e38SRobert Watson KMOD_IPSTAT_INC(ips_nogif);
3088f5a8818SKevin Lo return (IPPROTO_DONE);
30967df9f38SBjoern A. Zeeb }
31067df9f38SBjoern A. Zeeb gifp = GIF2IFP(sc);
3110b9f5f8aSAndrey V. Elsukov if ((gifp->if_flags & IFF_UP) != 0) {
312cfa1ca9dSYoshinobu Inoue ip = mtod(m, struct ip *);
3130b9f5f8aSAndrey V. Elsukov ecn = ip->ip_tos;
3146d8fdfa9SAndrey V. Elsukov m_adj(m, off);
3150b9f5f8aSAndrey V. Elsukov gif_input(m, gifp, proto, ecn);
3160b9f5f8aSAndrey V. Elsukov } else {
31759dfcba4SHajimu UMEMOTO m_freem(m);
318315e3e38SRobert Watson KMOD_IPSTAT_INC(ips_nogif);
319cfa1ca9dSYoshinobu Inoue }
3208f5a8818SKevin Lo return (IPPROTO_DONE);
321cfa1ca9dSYoshinobu Inoue }
322686cdd19SJun-ichiro itojun Hagino
323b941bc1dSAndrey V. Elsukov static int
in_gif_lookup(const struct mbuf * m,int off,int proto,void ** arg)324b941bc1dSAndrey V. Elsukov in_gif_lookup(const struct mbuf *m, int off, int proto, void **arg)
3259426aedfSHajimu UMEMOTO {
32610a0e0bfSAndrey V. Elsukov const struct ip *ip;
32710a0e0bfSAndrey V. Elsukov struct gif_softc *sc;
328c1b4f79dSAndrey V. Elsukov int ret;
329686cdd19SJun-ichiro itojun Hagino
3306e081509SAndrey V. Elsukov if (V_ipv4_hashtbl == NULL)
3316e081509SAndrey V. Elsukov return (0);
3326e081509SAndrey V. Elsukov
33397168be8SGleb Smirnoff NET_EPOCH_ASSERT();
33410a0e0bfSAndrey V. Elsukov ip = mtod(m, const struct ip *);
335b941bc1dSAndrey V. Elsukov /*
336b941bc1dSAndrey V. Elsukov * NOTE: it is safe to iterate without any locking here, because softc
337b941bc1dSAndrey V. Elsukov * can be reclaimed only when we are not within net_epoch_preempt
338b941bc1dSAndrey V. Elsukov * section, but ip_encap lookup+input are executed in epoch section.
339b941bc1dSAndrey V. Elsukov */
340b941bc1dSAndrey V. Elsukov ret = 0;
341b941bc1dSAndrey V. Elsukov CK_LIST_FOREACH(sc, &GIF_HASH(ip->ip_dst.s_addr,
342b941bc1dSAndrey V. Elsukov ip->ip_src.s_addr), chain) {
343b941bc1dSAndrey V. Elsukov /*
344b941bc1dSAndrey V. Elsukov * This is an inbound packet, its ip_dst is source address
345b941bc1dSAndrey V. Elsukov * in softc.
346b941bc1dSAndrey V. Elsukov */
347b941bc1dSAndrey V. Elsukov if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr &&
348b941bc1dSAndrey V. Elsukov sc->gif_iphdr->ip_dst.s_addr == ip->ip_src.s_addr) {
349b941bc1dSAndrey V. Elsukov ret = ENCAP_DRV_LOOKUP;
350b941bc1dSAndrey V. Elsukov goto done;
351b941bc1dSAndrey V. Elsukov }
352b941bc1dSAndrey V. Elsukov }
353b941bc1dSAndrey V. Elsukov /*
354b941bc1dSAndrey V. Elsukov * No exact match.
355b941bc1dSAndrey V. Elsukov * Check the list of interfaces with GIF_IGNORE_SOURCE flag.
356b941bc1dSAndrey V. Elsukov */
357b941bc1dSAndrey V. Elsukov CK_LIST_FOREACH(sc, &V_ipv4_list, chain) {
358b941bc1dSAndrey V. Elsukov if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr) {
3596d8fdfa9SAndrey V. Elsukov ret = 32 + 8; /* src + proto */
360b941bc1dSAndrey V. Elsukov goto done;
361b941bc1dSAndrey V. Elsukov }
362b941bc1dSAndrey V. Elsukov }
363c1b4f79dSAndrey V. Elsukov return (0);
364b941bc1dSAndrey V. Elsukov done:
365b941bc1dSAndrey V. Elsukov if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
366b941bc1dSAndrey V. Elsukov return (0);
367686cdd19SJun-ichiro itojun Hagino /* ingress filters on outer source */
36810a0e0bfSAndrey V. Elsukov if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
3697bfc98afSAlexander V. Chernikov if (fib4_check_urpf(sc->gif_fibnum, ip->ip_src, 0, NHR_NONE,
3707bfc98afSAlexander V. Chernikov m->m_pkthdr.rcvif) == 0)
37165ff3638SAlexander V. Chernikov return (0);
372686cdd19SJun-ichiro itojun Hagino }
373b941bc1dSAndrey V. Elsukov *arg = sc;
374c1b4f79dSAndrey V. Elsukov return (ret);
375686cdd19SJun-ichiro itojun Hagino }
3769426aedfSHajimu UMEMOTO
377009d82eeSAndrey V. Elsukov static const struct srcaddrtab *ipv4_srcaddrtab;
378b941bc1dSAndrey V. Elsukov static struct {
379b941bc1dSAndrey V. Elsukov const struct encap_config encap;
380b941bc1dSAndrey V. Elsukov const struct encaptab *cookie;
381b941bc1dSAndrey V. Elsukov } ipv4_encap_cfg[] = {
382b941bc1dSAndrey V. Elsukov {
383b941bc1dSAndrey V. Elsukov .encap = {
384b941bc1dSAndrey V. Elsukov .proto = IPPROTO_IPV4,
385b941bc1dSAndrey V. Elsukov .min_length = 2 * sizeof(struct ip),
386b941bc1dSAndrey V. Elsukov .exact_match = ENCAP_DRV_LOOKUP,
387b941bc1dSAndrey V. Elsukov .lookup = in_gif_lookup,
3886d8fdfa9SAndrey V. Elsukov .input = in_gif_input
389b941bc1dSAndrey V. Elsukov },
390b941bc1dSAndrey V. Elsukov },
391b941bc1dSAndrey V. Elsukov #ifdef INET6
392b941bc1dSAndrey V. Elsukov {
393b941bc1dSAndrey V. Elsukov .encap = {
394b941bc1dSAndrey V. Elsukov .proto = IPPROTO_IPV6,
395b941bc1dSAndrey V. Elsukov .min_length = sizeof(struct ip) +
396b941bc1dSAndrey V. Elsukov sizeof(struct ip6_hdr),
397b941bc1dSAndrey V. Elsukov .exact_match = ENCAP_DRV_LOOKUP,
398b941bc1dSAndrey V. Elsukov .lookup = in_gif_lookup,
399b941bc1dSAndrey V. Elsukov .input = in_gif_input
400b941bc1dSAndrey V. Elsukov },
401b941bc1dSAndrey V. Elsukov },
402b941bc1dSAndrey V. Elsukov #endif
403b941bc1dSAndrey V. Elsukov {
404b941bc1dSAndrey V. Elsukov .encap = {
405b941bc1dSAndrey V. Elsukov .proto = IPPROTO_ETHERIP,
406b941bc1dSAndrey V. Elsukov .min_length = sizeof(struct ip) +
407b941bc1dSAndrey V. Elsukov sizeof(struct etherip_header) +
408b941bc1dSAndrey V. Elsukov sizeof(struct ether_header),
409b941bc1dSAndrey V. Elsukov .exact_match = ENCAP_DRV_LOOKUP,
410b941bc1dSAndrey V. Elsukov .lookup = in_gif_lookup,
411b941bc1dSAndrey V. Elsukov .input = in_gif_input
412b941bc1dSAndrey V. Elsukov },
413b941bc1dSAndrey V. Elsukov }
4146d8fdfa9SAndrey V. Elsukov };
4156d8fdfa9SAndrey V. Elsukov
416b941bc1dSAndrey V. Elsukov void
in_gif_init(void)417b941bc1dSAndrey V. Elsukov in_gif_init(void)
4189426aedfSHajimu UMEMOTO {
419b941bc1dSAndrey V. Elsukov int i;
4200b9f5f8aSAndrey V. Elsukov
421b941bc1dSAndrey V. Elsukov if (!IS_DEFAULT_VNET(curvnet))
422b941bc1dSAndrey V. Elsukov return;
423009d82eeSAndrey V. Elsukov
424009d82eeSAndrey V. Elsukov ipv4_srcaddrtab = ip_encap_register_srcaddr(in_gif_srcaddr,
425009d82eeSAndrey V. Elsukov NULL, M_WAITOK);
426b941bc1dSAndrey V. Elsukov for (i = 0; i < nitems(ipv4_encap_cfg); i++)
427b941bc1dSAndrey V. Elsukov ipv4_encap_cfg[i].cookie = ip_encap_attach(
428b941bc1dSAndrey V. Elsukov &ipv4_encap_cfg[i].encap, NULL, M_WAITOK);
4299426aedfSHajimu UMEMOTO }
430b941bc1dSAndrey V. Elsukov
431b941bc1dSAndrey V. Elsukov void
in_gif_uninit(void)432b941bc1dSAndrey V. Elsukov in_gif_uninit(void)
433b941bc1dSAndrey V. Elsukov {
434b941bc1dSAndrey V. Elsukov int i;
435b941bc1dSAndrey V. Elsukov
436b941bc1dSAndrey V. Elsukov if (IS_DEFAULT_VNET(curvnet)) {
437b941bc1dSAndrey V. Elsukov for (i = 0; i < nitems(ipv4_encap_cfg); i++)
438b941bc1dSAndrey V. Elsukov ip_encap_detach(ipv4_encap_cfg[i].cookie);
439009d82eeSAndrey V. Elsukov ip_encap_unregister_srcaddr(ipv4_srcaddrtab);
440b941bc1dSAndrey V. Elsukov }
441009d82eeSAndrey V. Elsukov if (V_ipv4_hashtbl != NULL) {
442b941bc1dSAndrey V. Elsukov gif_hashdestroy(V_ipv4_hashtbl);
4438796e291SAndrey V. Elsukov V_ipv4_hashtbl = NULL;
4448796e291SAndrey V. Elsukov GIF_WAIT();
445009d82eeSAndrey V. Elsukov gif_hashdestroy(V_ipv4_srchashtbl);
446009d82eeSAndrey V. Elsukov }
447b941bc1dSAndrey V. Elsukov }
448