2 * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include "opt_inet6.h"
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/socket.h>
37 #include <sys/sockio.h>
39 #include <sys/errno.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
46 #include <net/if_var.h>
49 #include <netinet/in.h>
51 #include <net/ethernet.h>
52 #include <netinet/ip.h>
54 #include <netinet/ip_encap.h>
55 #include <netinet/ip6.h>
56 #include <netinet6/ip6_var.h>
57 #include <netinet6/in6_var.h>
58 #include <netinet6/scope6_var.h>
59 #include <net/if_gre.h>
61 VNET_DEFINE(int, ip6_gre_hlim) = IPV6_DEFHLIM;
62 #define V_ip6_gre_hlim VNET(ip6_gre_hlim)
64 SYSCTL_DECL(_net_inet6_ip6);
65 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, grehlim, CTLFLAG_VNET | CTLFLAG_RW,
66 &VNET_NAME(ip6_gre_hlim), 0, "Default hop limit for encapsulated packets");
68 VNET_DEFINE_STATIC(struct gre_list *, ipv6_hashtbl) = NULL;
69 #define V_ipv6_hashtbl VNET(ipv6_hashtbl)
70 #define GRE_HASH(src, dst) (V_ipv6_hashtbl[\
71 in6_gre_hashval((src), (dst)) & (GRE_HASH_SIZE - 1)])
72 #define GRE_HASH_SC(sc) GRE_HASH(&(sc)->gre_oip6.ip6_src,\
73 &(sc)->gre_oip6.ip6_dst)
76 in6_gre_hashval(const struct in6_addr *src, const struct in6_addr *dst)
80 ret = fnv_32_buf(src, sizeof(*src), FNV1_32_INIT);
81 return (fnv_32_buf(dst, sizeof(*dst), ret));
85 in6_gre_checkdup(const struct gre_softc *sc, const struct in6_addr *src,
86 const struct in6_addr *dst)
88 struct gre_softc *tmp;
90 if (sc->gre_family == AF_INET6 &&
91 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, src) &&
92 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, dst))
95 CK_LIST_FOREACH(tmp, &GRE_HASH(src, dst), chain) {
98 if (IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_src, src) &&
99 IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_dst, dst))
100 return (EADDRNOTAVAIL);
106 in6_gre_lookup(const struct mbuf *m, int off, int proto, void **arg)
108 const struct ip6_hdr *ip6;
109 struct gre_softc *sc;
111 if (V_ipv6_hashtbl == NULL)
114 MPASS(in_epoch(net_epoch_preempt));
115 ip6 = mtod(m, const struct ip6_hdr *);
116 CK_LIST_FOREACH(sc, &GRE_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) {
118 * This is an inbound packet, its ip6_dst is source address
121 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src,
123 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst,
125 if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0)
128 return (ENCAP_DRV_LOOKUP);
135 in6_gre_attach(struct gre_softc *sc)
138 sc->gre_hlen = sizeof(struct greip6);
139 sc->gre_oip6.ip6_vfc = IPV6_VERSION;
140 sc->gre_oip6.ip6_nxt = IPPROTO_GRE;
141 gre_updatehdr(sc, &sc->gre_gi6hdr->gi6_gre);
142 CK_LIST_INSERT_HEAD(&GRE_HASH_SC(sc), sc, chain);
146 in6_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value)
149 MPASS(cmd == GRESKEY || cmd == GRESOPTS);
151 /* NOTE: we are protected with gre_ioctl_sx lock */
152 MPASS(sc->gre_family == AF_INET6);
153 CK_LIST_REMOVE(sc, chain);
158 sc->gre_options = value;
163 in6_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data)
165 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
166 struct sockaddr_in6 *dst, *src;
170 /* NOTE: we are protected with gre_ioctl_sx lock */
173 case SIOCSIFPHYADDR_IN6:
174 src = &((struct in6_aliasreq *)data)->ifra_addr;
175 dst = &((struct in6_aliasreq *)data)->ifra_dstaddr;
178 if (src->sin6_family != dst->sin6_family ||
179 src->sin6_family != AF_INET6 ||
180 src->sin6_len != dst->sin6_len ||
181 src->sin6_len != sizeof(*src))
183 if (IN6_IS_ADDR_UNSPECIFIED(&src->sin6_addr) ||
184 IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) {
185 error = EADDRNOTAVAIL;
189 * Check validity of the scope zone ID of the
190 * addresses, and convert it into the kernel
191 * internal form if necessary.
193 if ((error = sa6_embedscope(src, 0)) != 0 ||
194 (error = sa6_embedscope(dst, 0)) != 0)
197 if (V_ipv6_hashtbl == NULL)
198 V_ipv6_hashtbl = gre_hashinit();
199 error = in6_gre_checkdup(sc, &src->sin6_addr,
201 if (error == EADDRNOTAVAIL)
203 if (error == EEXIST) {
204 /* Addresses are the same. Just return. */
208 ip6 = malloc(sizeof(struct greip6) + 3 * sizeof(uint32_t),
209 M_GRE, M_WAITOK | M_ZERO);
210 ip6->ip6_src = src->sin6_addr;
211 ip6->ip6_dst = dst->sin6_addr;
212 if (sc->gre_family != 0) {
213 /* Detach existing tunnel first */
214 CK_LIST_REMOVE(sc, chain);
216 free(sc->gre_hdr, M_GRE);
217 /* XXX: should we notify about link state change? */
219 sc->gre_family = AF_INET6;
222 sc->gre_iseq = UINT32_MAX;
225 case SIOCGIFPSRCADDR_IN6:
226 case SIOCGIFPDSTADDR_IN6:
227 if (sc->gre_family != AF_INET6) {
228 error = EADDRNOTAVAIL;
231 src = (struct sockaddr_in6 *)&ifr->ifr_addr;
232 memset(src, 0, sizeof(*src));
233 src->sin6_family = AF_INET6;
234 src->sin6_len = sizeof(*src);
235 src->sin6_addr = (cmd == SIOCGIFPSRCADDR_IN6) ?
236 sc->gre_oip6.ip6_src: sc->gre_oip6.ip6_dst;
237 error = prison_if(curthread->td_ucred, (struct sockaddr *)src);
239 error = sa6_recoverscope(src);
241 memset(src, 0, sizeof(*src));
248 in6_gre_output(struct mbuf *m, int af __unused, int hlen __unused)
252 gi6 = mtod(m, struct greip6 *);
253 gi6->gi6_ip6.ip6_hlim = V_ip6_gre_hlim;
254 return (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, NULL));
257 static const struct encaptab *ecookie = NULL;
258 static const struct encap_config ipv6_encap_cfg = {
259 .proto = IPPROTO_GRE,
260 .min_length = sizeof(struct greip6) +
264 sizeof(struct ip6_hdr),
266 .exact_match = ENCAP_DRV_LOOKUP,
267 .lookup = in6_gre_lookup,
275 if (!IS_DEFAULT_VNET(curvnet))
277 ecookie = ip6_encap_attach(&ipv6_encap_cfg, NULL, M_WAITOK);
284 if (IS_DEFAULT_VNET(curvnet))
285 ip6_encap_detach(ecookie);
286 if (V_ipv6_hashtbl != NULL)
287 gre_hashdestroy(V_ipv6_hashtbl);