]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in_gif.c
Import OpenCSD v.1.4.0.
[FreeBSD/FreeBSD.git] / sys / netinet / in_gif.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * Copyright (c) 2018 Andrey V. Elsukov <ae@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      $KAME: in_gif.c,v 1.54 2001/05/14 14:02:16 itojun Exp $
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/jail.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/mbuf.h>
47 #include <sys/errno.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
50 #include <sys/malloc.h>
51 #include <sys/proc.h>
52
53 #include <net/ethernet.h>
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/if_private.h>
57 #include <net/route.h>
58 #include <net/vnet.h>
59
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip_encap.h>
66 #include <netinet/ip_ecn.h>
67 #include <netinet/in_fib.h>
68
69 #ifdef INET6
70 #include <netinet/ip6.h>
71 #endif
72
73 #include <net/if_gif.h>
74
75 #define GIF_TTL         30
76 VNET_DEFINE_STATIC(int, ip_gif_ttl) = GIF_TTL;
77 #define V_ip_gif_ttl            VNET(ip_gif_ttl)
78 SYSCTL_INT(_net_inet_ip, IPCTL_GIF_TTL, gifttl, CTLFLAG_VNET | CTLFLAG_RW,
79     &VNET_NAME(ip_gif_ttl), 0, "Default TTL value for encapsulated packets");
80
81 /*
82  * We keep interfaces in a hash table using src+dst as key.
83  * Interfaces with GIF_IGNORE_SOURCE flag are linked into plain list.
84  */
85 VNET_DEFINE_STATIC(struct gif_list *, ipv4_hashtbl) = NULL;
86 VNET_DEFINE_STATIC(struct gif_list *, ipv4_srchashtbl) = NULL;
87 VNET_DEFINE_STATIC(struct gif_list, ipv4_list) = CK_LIST_HEAD_INITIALIZER();
88 #define V_ipv4_hashtbl          VNET(ipv4_hashtbl)
89 #define V_ipv4_srchashtbl       VNET(ipv4_srchashtbl)
90 #define V_ipv4_list             VNET(ipv4_list)
91
92 #define GIF_HASH(src, dst)      (V_ipv4_hashtbl[\
93     in_gif_hashval((src), (dst)) & (GIF_HASH_SIZE - 1)])
94 #define GIF_SRCHASH(src)        (V_ipv4_srchashtbl[\
95     fnv_32_buf(&(src), sizeof(src), FNV1_32_INIT) & (GIF_HASH_SIZE - 1)])
96 #define GIF_HASH_SC(sc)         GIF_HASH((sc)->gif_iphdr->ip_src.s_addr,\
97     (sc)->gif_iphdr->ip_dst.s_addr)
98 static uint32_t
99 in_gif_hashval(in_addr_t src, in_addr_t dst)
100 {
101         uint32_t ret;
102
103         ret = fnv_32_buf(&src, sizeof(src), FNV1_32_INIT);
104         return (fnv_32_buf(&dst, sizeof(dst), ret));
105 }
106
107 static int
108 in_gif_checkdup(const struct gif_softc *sc, in_addr_t src, in_addr_t dst)
109 {
110         struct gif_softc *tmp;
111
112         if (sc->gif_family == AF_INET &&
113             sc->gif_iphdr->ip_src.s_addr == src &&
114             sc->gif_iphdr->ip_dst.s_addr == dst)
115                 return (EEXIST);
116
117         CK_LIST_FOREACH(tmp, &GIF_HASH(src, dst), chain) {
118                 if (tmp == sc)
119                         continue;
120                 if (tmp->gif_iphdr->ip_src.s_addr == src &&
121                     tmp->gif_iphdr->ip_dst.s_addr == dst)
122                         return (EADDRNOTAVAIL);
123         }
124         return (0);
125 }
126
127 /*
128  * Check that ingress address belongs to local host.
129  */
130 static void
131 in_gif_set_running(struct gif_softc *sc)
132 {
133
134         if (in_localip(sc->gif_iphdr->ip_src))
135                 GIF2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING;
136         else
137                 GIF2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING;
138 }
139
140 /*
141  * ifaddr_event handler.
142  * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent
143  * source address spoofing.
144  */
145 static void
146 in_gif_srcaddr(void *arg __unused, const struct sockaddr *sa,
147     int event __unused)
148 {
149         const struct sockaddr_in *sin;
150         struct gif_softc *sc;
151
152         /* Check that VNET is ready */
153         if (V_ipv4_hashtbl == NULL)
154                 return;
155
156         NET_EPOCH_ASSERT();
157         sin = (const struct sockaddr_in *)sa;
158         CK_LIST_FOREACH(sc, &GIF_SRCHASH(sin->sin_addr.s_addr), srchash) {
159                 if (sc->gif_iphdr->ip_src.s_addr != sin->sin_addr.s_addr)
160                         continue;
161                 in_gif_set_running(sc);
162         }
163 }
164
165 static void
166 in_gif_attach(struct gif_softc *sc)
167 {
168
169         if (sc->gif_options & GIF_IGNORE_SOURCE)
170                 CK_LIST_INSERT_HEAD(&V_ipv4_list, sc, chain);
171         else
172                 CK_LIST_INSERT_HEAD(&GIF_HASH_SC(sc), sc, chain);
173
174         CK_LIST_INSERT_HEAD(&GIF_SRCHASH(sc->gif_iphdr->ip_src.s_addr),
175             sc, srchash);
176 }
177
178 int
179 in_gif_setopts(struct gif_softc *sc, u_int options)
180 {
181
182         /* NOTE: we are protected with gif_ioctl_sx lock */
183         MPASS(sc->gif_family == AF_INET);
184         MPASS(sc->gif_options != options);
185
186         if ((options & GIF_IGNORE_SOURCE) !=
187             (sc->gif_options & GIF_IGNORE_SOURCE)) {
188                 CK_LIST_REMOVE(sc, srchash);
189                 CK_LIST_REMOVE(sc, chain);
190                 sc->gif_options = options;
191                 in_gif_attach(sc);
192         }
193         return (0);
194 }
195
196 int
197 in_gif_ioctl(struct gif_softc *sc, u_long cmd, caddr_t data)
198 {
199         struct ifreq *ifr = (struct ifreq *)data;
200         struct epoch_tracker et;
201         struct sockaddr_in *dst, *src;
202         struct ip *ip;
203         int error;
204
205         /* NOTE: we are protected with gif_ioctl_sx lock */
206         error = EINVAL;
207         switch (cmd) {
208         case SIOCSIFPHYADDR:
209                 src = &((struct in_aliasreq *)data)->ifra_addr;
210                 dst = &((struct in_aliasreq *)data)->ifra_dstaddr;
211
212                 /* sanity checks */
213                 if (src->sin_family != dst->sin_family ||
214                     src->sin_family != AF_INET ||
215                     src->sin_len != dst->sin_len ||
216                     src->sin_len != sizeof(*src))
217                         break;
218                 if (src->sin_addr.s_addr == INADDR_ANY ||
219                     dst->sin_addr.s_addr == INADDR_ANY) {
220                         error = EADDRNOTAVAIL;
221                         break;
222                 }
223                 if (V_ipv4_hashtbl == NULL) {
224                         V_ipv4_hashtbl = gif_hashinit();
225                         V_ipv4_srchashtbl = gif_hashinit();
226                 }
227                 error = in_gif_checkdup(sc, src->sin_addr.s_addr,
228                     dst->sin_addr.s_addr);
229                 if (error == EADDRNOTAVAIL)
230                         break;
231                 if (error == EEXIST) {
232                         /* Addresses are the same. Just return. */
233                         error = 0;
234                         break;
235                 }
236                 ip = malloc(sizeof(*ip), M_GIF, M_WAITOK | M_ZERO);
237                 ip->ip_src.s_addr = src->sin_addr.s_addr;
238                 ip->ip_dst.s_addr = dst->sin_addr.s_addr;
239                 if (sc->gif_family != 0) {
240                         /* Detach existing tunnel first */
241                         CK_LIST_REMOVE(sc, srchash);
242                         CK_LIST_REMOVE(sc, chain);
243                         GIF_WAIT();
244                         free(sc->gif_hdr, M_GIF);
245                         /* XXX: should we notify about link state change? */
246                 }
247                 sc->gif_family = AF_INET;
248                 sc->gif_iphdr = ip;
249                 in_gif_attach(sc);
250                 NET_EPOCH_ENTER(et);
251                 in_gif_set_running(sc);
252                 NET_EPOCH_EXIT(et);
253                 break;
254         case SIOCGIFPSRCADDR:
255         case SIOCGIFPDSTADDR:
256                 if (sc->gif_family != AF_INET) {
257                         error = EADDRNOTAVAIL;
258                         break;
259                 }
260                 src = (struct sockaddr_in *)&ifr->ifr_addr;
261                 memset(src, 0, sizeof(*src));
262                 src->sin_family = AF_INET;
263                 src->sin_len = sizeof(*src);
264                 src->sin_addr = (cmd == SIOCGIFPSRCADDR) ?
265                     sc->gif_iphdr->ip_src: sc->gif_iphdr->ip_dst;
266                 error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
267                 if (error != 0)
268                         memset(src, 0, sizeof(*src));
269                 break;
270         }
271         return (error);
272 }
273
274 int
275 in_gif_output(struct ifnet *ifp, struct mbuf *m, int proto, uint8_t ecn)
276 {
277         struct gif_softc *sc = ifp->if_softc;
278         struct ip *ip;
279         int len;
280
281         /* prepend new IP header */
282         NET_EPOCH_ASSERT();
283         len = sizeof(struct ip);
284 #ifndef __NO_STRICT_ALIGNMENT
285         if (proto == IPPROTO_ETHERIP)
286                 len += ETHERIP_ALIGN;
287 #endif
288         M_PREPEND(m, len, M_NOWAIT);
289         if (m == NULL)
290                 return (ENOBUFS);
291 #ifndef __NO_STRICT_ALIGNMENT
292         if (proto == IPPROTO_ETHERIP) {
293                 len = mtod(m, vm_offset_t) & 3;
294                 KASSERT(len == 0 || len == ETHERIP_ALIGN,
295                     ("in_gif_output: unexpected misalignment"));
296                 m->m_data += len;
297                 m->m_len -= ETHERIP_ALIGN;
298         }
299 #endif
300         ip = mtod(m, struct ip *);
301
302         MPASS(sc->gif_family == AF_INET);
303         bcopy(sc->gif_iphdr, ip, sizeof(struct ip));
304         ip->ip_p = proto;
305         /* version will be set in ip_output() */
306         ip->ip_ttl = V_ip_gif_ttl;
307         ip->ip_len = htons(m->m_pkthdr.len);
308         ip->ip_tos = ecn;
309
310         return (ip_output(m, NULL, NULL, 0, NULL, NULL));
311 }
312
313 static int
314 in_gif_input(struct mbuf *m, int off, int proto, void *arg)
315 {
316         struct gif_softc *sc = arg;
317         struct ifnet *gifp;
318         struct ip *ip;
319         uint8_t ecn;
320
321         NET_EPOCH_ASSERT();
322         if (sc == NULL) {
323                 m_freem(m);
324                 KMOD_IPSTAT_INC(ips_nogif);
325                 return (IPPROTO_DONE);
326         }
327         gifp = GIF2IFP(sc);
328         if ((gifp->if_flags & IFF_UP) != 0) {
329                 ip = mtod(m, struct ip *);
330                 ecn = ip->ip_tos;
331                 m_adj(m, off);
332                 gif_input(m, gifp, proto, ecn);
333         } else {
334                 m_freem(m);
335                 KMOD_IPSTAT_INC(ips_nogif);
336         }
337         return (IPPROTO_DONE);
338 }
339
340 static int
341 in_gif_lookup(const struct mbuf *m, int off, int proto, void **arg)
342 {
343         const struct ip *ip;
344         struct gif_softc *sc;
345         int ret;
346
347         if (V_ipv4_hashtbl == NULL)
348                 return (0);
349
350         NET_EPOCH_ASSERT();
351         ip = mtod(m, const struct ip *);
352         /*
353          * NOTE: it is safe to iterate without any locking here, because softc
354          * can be reclaimed only when we are not within net_epoch_preempt
355          * section, but ip_encap lookup+input are executed in epoch section.
356          */
357         ret = 0;
358         CK_LIST_FOREACH(sc, &GIF_HASH(ip->ip_dst.s_addr,
359             ip->ip_src.s_addr), chain) {
360                 /*
361                  * This is an inbound packet, its ip_dst is source address
362                  * in softc.
363                  */
364                 if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr &&
365                     sc->gif_iphdr->ip_dst.s_addr == ip->ip_src.s_addr) {
366                         ret = ENCAP_DRV_LOOKUP;
367                         goto done;
368                 }
369         }
370         /*
371          * No exact match.
372          * Check the list of interfaces with GIF_IGNORE_SOURCE flag.
373          */
374         CK_LIST_FOREACH(sc, &V_ipv4_list, chain) {
375                 if (sc->gif_iphdr->ip_src.s_addr == ip->ip_dst.s_addr) {
376                         ret = 32 + 8; /* src + proto */
377                         goto done;
378                 }
379         }
380         return (0);
381 done:
382         if ((GIF2IFP(sc)->if_flags & IFF_UP) == 0)
383                 return (0);
384         /* ingress filters on outer source */
385         if ((GIF2IFP(sc)->if_flags & IFF_LINK2) == 0) {
386                 if (fib4_check_urpf(sc->gif_fibnum, ip->ip_src, 0, NHR_NONE,
387                                         m->m_pkthdr.rcvif) == 0)
388                         return (0);
389         }
390         *arg = sc;
391         return (ret);
392 }
393
394 static const struct srcaddrtab *ipv4_srcaddrtab;
395 static struct {
396         const struct encap_config encap;
397         const struct encaptab *cookie;
398 } ipv4_encap_cfg[] = {
399         {
400                 .encap = {
401                         .proto = IPPROTO_IPV4,
402                         .min_length = 2 * sizeof(struct ip),
403                         .exact_match = ENCAP_DRV_LOOKUP,
404                         .lookup = in_gif_lookup,
405                         .input = in_gif_input
406                 },
407         },
408 #ifdef INET6
409         {
410                 .encap = {
411                         .proto = IPPROTO_IPV6,
412                         .min_length = sizeof(struct ip) +
413                             sizeof(struct ip6_hdr),
414                         .exact_match = ENCAP_DRV_LOOKUP,
415                         .lookup = in_gif_lookup,
416                         .input = in_gif_input
417                 },
418         },
419 #endif
420         {
421                 .encap = {
422                         .proto = IPPROTO_ETHERIP,
423                         .min_length = sizeof(struct ip) +
424                             sizeof(struct etherip_header) +
425                             sizeof(struct ether_header),
426                         .exact_match = ENCAP_DRV_LOOKUP,
427                         .lookup = in_gif_lookup,
428                         .input = in_gif_input
429                 },
430         }
431 };
432
433 void
434 in_gif_init(void)
435 {
436         int i;
437
438         if (!IS_DEFAULT_VNET(curvnet))
439                 return;
440
441         ipv4_srcaddrtab = ip_encap_register_srcaddr(in_gif_srcaddr,
442             NULL, M_WAITOK);
443         for (i = 0; i < nitems(ipv4_encap_cfg); i++)
444                 ipv4_encap_cfg[i].cookie = ip_encap_attach(
445                     &ipv4_encap_cfg[i].encap, NULL, M_WAITOK);
446 }
447
448 void
449 in_gif_uninit(void)
450 {
451         int i;
452
453         if (IS_DEFAULT_VNET(curvnet)) {
454                 for (i = 0; i < nitems(ipv4_encap_cfg); i++)
455                         ip_encap_detach(ipv4_encap_cfg[i].cookie);
456                 ip_encap_unregister_srcaddr(ipv4_srcaddrtab);
457         }
458         if (V_ipv4_hashtbl != NULL) {
459                 gif_hashdestroy(V_ipv4_hashtbl);
460                 V_ipv4_hashtbl = NULL;
461                 GIF_WAIT();
462                 gif_hashdestroy(V_ipv4_srchashtbl);
463         }
464 }