1 /*- 2 * Copyright (c) 2014, 2018 Andrey V. Elsukov <ae@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 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. 14 * 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. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include "opt_inet.h" 31 #include "opt_inet6.h" 32 33 #include <sys/param.h> 34 #include <sys/jail.h> 35 #include <sys/systm.h> 36 #include <sys/socket.h> 37 #include <sys/sockio.h> 38 #include <sys/mbuf.h> 39 #include <sys/errno.h> 40 #include <sys/kernel.h> 41 #include <sys/sysctl.h> 42 #include <sys/malloc.h> 43 #include <sys/proc.h> 44 45 #include <net/if.h> 46 #include <net/if_var.h> 47 #include <net/vnet.h> 48 49 #include <netinet/in.h> 50 #ifdef INET 51 #include <net/ethernet.h> 52 #include <netinet/ip.h> 53 #endif 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> 60 61 VNET_DEFINE(int, ip6_gre_hlim) = IPV6_DEFHLIM; 62 #define V_ip6_gre_hlim VNET(ip6_gre_hlim) 63 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"); 67 68 VNET_DEFINE_STATIC(struct gre_list *, ipv6_hashtbl) = NULL; 69 VNET_DEFINE_STATIC(struct gre_list *, ipv6_srchashtbl) = NULL; 70 #define V_ipv6_hashtbl VNET(ipv6_hashtbl) 71 #define V_ipv6_srchashtbl VNET(ipv6_srchashtbl) 72 #define GRE_HASH(src, dst) (V_ipv6_hashtbl[\ 73 in6_gre_hashval((src), (dst)) & (GRE_HASH_SIZE - 1)]) 74 #define GRE_SRCHASH(src) (V_ipv6_srchashtbl[\ 75 fnv_32_buf((src), sizeof(*src), FNV1_32_INIT) & (GRE_HASH_SIZE - 1)]) 76 #define GRE_HASH_SC(sc) GRE_HASH(&(sc)->gre_oip6.ip6_src,\ 77 &(sc)->gre_oip6.ip6_dst) 78 79 static uint32_t 80 in6_gre_hashval(const struct in6_addr *src, const struct in6_addr *dst) 81 { 82 uint32_t ret; 83 84 ret = fnv_32_buf(src, sizeof(*src), FNV1_32_INIT); 85 return (fnv_32_buf(dst, sizeof(*dst), ret)); 86 } 87 88 static int 89 in6_gre_checkdup(const struct gre_softc *sc, const struct in6_addr *src, 90 const struct in6_addr *dst) 91 { 92 struct gre_softc *tmp; 93 94 if (sc->gre_family == AF_INET6 && 95 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, src) && 96 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, dst)) 97 return (EEXIST); 98 99 CK_LIST_FOREACH(tmp, &GRE_HASH(src, dst), chain) { 100 if (tmp == sc) 101 continue; 102 if (IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_src, src) && 103 IN6_ARE_ADDR_EQUAL(&tmp->gre_oip6.ip6_dst, dst)) 104 return (EADDRNOTAVAIL); 105 } 106 return (0); 107 } 108 109 static int 110 in6_gre_lookup(const struct mbuf *m, int off, int proto, void **arg) 111 { 112 const struct ip6_hdr *ip6; 113 struct gre_softc *sc; 114 115 if (V_ipv6_hashtbl == NULL) 116 return (0); 117 118 MPASS(in_epoch(net_epoch_preempt)); 119 ip6 = mtod(m, const struct ip6_hdr *); 120 CK_LIST_FOREACH(sc, &GRE_HASH(&ip6->ip6_dst, &ip6->ip6_src), chain) { 121 /* 122 * This is an inbound packet, its ip6_dst is source address 123 * in softc. 124 */ 125 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, 126 &ip6->ip6_dst) && 127 IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_dst, 128 &ip6->ip6_src)) { 129 if ((GRE2IFP(sc)->if_flags & IFF_UP) == 0) 130 return (0); 131 *arg = sc; 132 return (ENCAP_DRV_LOOKUP); 133 } 134 } 135 return (0); 136 } 137 138 /* 139 * Check that ingress address belongs to local host. 140 */ 141 static void 142 in6_gre_set_running(struct gre_softc *sc) 143 { 144 145 if (in6_localip(&sc->gre_oip6.ip6_src)) 146 GRE2IFP(sc)->if_drv_flags |= IFF_DRV_RUNNING; 147 else 148 GRE2IFP(sc)->if_drv_flags &= ~IFF_DRV_RUNNING; 149 } 150 151 /* 152 * ifaddr_event handler. 153 * Clear IFF_DRV_RUNNING flag when ingress address disappears to prevent 154 * source address spoofing. 155 */ 156 static void 157 in6_gre_srcaddr(void *arg __unused, const struct sockaddr *sa, 158 int event __unused) 159 { 160 const struct sockaddr_in6 *sin; 161 struct gre_softc *sc; 162 163 if (V_ipv6_srchashtbl == NULL) 164 return; 165 166 MPASS(in_epoch(net_epoch_preempt)); 167 sin = (const struct sockaddr_in6 *)sa; 168 CK_LIST_FOREACH(sc, &GRE_SRCHASH(&sin->sin6_addr), srchash) { 169 if (IN6_ARE_ADDR_EQUAL(&sc->gre_oip6.ip6_src, 170 &sin->sin6_addr) == 0) 171 continue; 172 in6_gre_set_running(sc); 173 } 174 } 175 176 static void 177 in6_gre_attach(struct gre_softc *sc) 178 { 179 180 sc->gre_hlen = sizeof(struct greip6); 181 sc->gre_oip6.ip6_vfc = IPV6_VERSION; 182 sc->gre_oip6.ip6_nxt = IPPROTO_GRE; 183 gre_updatehdr(sc, &sc->gre_gi6hdr->gi6_gre); 184 CK_LIST_INSERT_HEAD(&GRE_HASH_SC(sc), sc, chain); 185 CK_LIST_INSERT_HEAD(&GRE_SRCHASH(&sc->gre_oip6.ip6_src), sc, srchash); 186 } 187 188 void 189 in6_gre_setopts(struct gre_softc *sc, u_long cmd, uint32_t value) 190 { 191 192 MPASS(cmd == GRESKEY || cmd == GRESOPTS); 193 194 /* NOTE: we are protected with gre_ioctl_sx lock */ 195 MPASS(sc->gre_family == AF_INET6); 196 CK_LIST_REMOVE(sc, chain); 197 CK_LIST_REMOVE(sc, srchash); 198 GRE_WAIT(); 199 if (cmd == GRESKEY) 200 sc->gre_key = value; 201 else 202 sc->gre_options = value; 203 in6_gre_attach(sc); 204 } 205 206 int 207 in6_gre_ioctl(struct gre_softc *sc, u_long cmd, caddr_t data) 208 { 209 struct in6_ifreq *ifr = (struct in6_ifreq *)data; 210 struct sockaddr_in6 *dst, *src; 211 struct ip6_hdr *ip6; 212 int error; 213 214 /* NOTE: we are protected with gre_ioctl_sx lock */ 215 error = EINVAL; 216 switch (cmd) { 217 case SIOCSIFPHYADDR_IN6: 218 src = &((struct in6_aliasreq *)data)->ifra_addr; 219 dst = &((struct in6_aliasreq *)data)->ifra_dstaddr; 220 221 /* sanity checks */ 222 if (src->sin6_family != dst->sin6_family || 223 src->sin6_family != AF_INET6 || 224 src->sin6_len != dst->sin6_len || 225 src->sin6_len != sizeof(*src)) 226 break; 227 if (IN6_IS_ADDR_UNSPECIFIED(&src->sin6_addr) || 228 IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr)) { 229 error = EADDRNOTAVAIL; 230 break; 231 } 232 /* 233 * Check validity of the scope zone ID of the 234 * addresses, and convert it into the kernel 235 * internal form if necessary. 236 */ 237 if ((error = sa6_embedscope(src, 0)) != 0 || 238 (error = sa6_embedscope(dst, 0)) != 0) 239 break; 240 241 if (V_ipv6_hashtbl == NULL) { 242 V_ipv6_hashtbl = gre_hashinit(); 243 V_ipv6_srchashtbl = gre_hashinit(); 244 } 245 error = in6_gre_checkdup(sc, &src->sin6_addr, 246 &dst->sin6_addr); 247 if (error == EADDRNOTAVAIL) 248 break; 249 if (error == EEXIST) { 250 /* Addresses are the same. Just return. */ 251 error = 0; 252 break; 253 } 254 ip6 = malloc(sizeof(struct greip6) + 3 * sizeof(uint32_t), 255 M_GRE, M_WAITOK | M_ZERO); 256 ip6->ip6_src = src->sin6_addr; 257 ip6->ip6_dst = dst->sin6_addr; 258 if (sc->gre_family != 0) { 259 /* Detach existing tunnel first */ 260 CK_LIST_REMOVE(sc, chain); 261 CK_LIST_REMOVE(sc, srchash); 262 GRE_WAIT(); 263 free(sc->gre_hdr, M_GRE); 264 /* XXX: should we notify about link state change? */ 265 } 266 sc->gre_family = AF_INET6; 267 sc->gre_hdr = ip6; 268 sc->gre_oseq = 0; 269 sc->gre_iseq = UINT32_MAX; 270 in6_gre_attach(sc); 271 in6_gre_set_running(sc); 272 break; 273 case SIOCGIFPSRCADDR_IN6: 274 case SIOCGIFPDSTADDR_IN6: 275 if (sc->gre_family != AF_INET6) { 276 error = EADDRNOTAVAIL; 277 break; 278 } 279 src = (struct sockaddr_in6 *)&ifr->ifr_addr; 280 memset(src, 0, sizeof(*src)); 281 src->sin6_family = AF_INET6; 282 src->sin6_len = sizeof(*src); 283 src->sin6_addr = (cmd == SIOCGIFPSRCADDR_IN6) ? 284 sc->gre_oip6.ip6_src: sc->gre_oip6.ip6_dst; 285 error = prison_if(curthread->td_ucred, (struct sockaddr *)src); 286 if (error == 0) 287 error = sa6_recoverscope(src); 288 if (error != 0) 289 memset(src, 0, sizeof(*src)); 290 break; 291 } 292 return (error); 293 } 294 295 int 296 in6_gre_output(struct mbuf *m, int af __unused, int hlen __unused) 297 { 298 struct greip6 *gi6; 299 300 gi6 = mtod(m, struct greip6 *); 301 gi6->gi6_ip6.ip6_hlim = V_ip6_gre_hlim; 302 return (ip6_output(m, NULL, NULL, IPV6_MINMTU, NULL, NULL, NULL)); 303 } 304 305 static const struct srcaddrtab *ipv6_srcaddrtab = NULL; 306 static const struct encaptab *ecookie = NULL; 307 static const struct encap_config ipv6_encap_cfg = { 308 .proto = IPPROTO_GRE, 309 .min_length = sizeof(struct greip6) + 310 #ifdef INET 311 sizeof(struct ip), 312 #else 313 sizeof(struct ip6_hdr), 314 #endif 315 .exact_match = ENCAP_DRV_LOOKUP, 316 .lookup = in6_gre_lookup, 317 .input = gre_input 318 }; 319 320 void 321 in6_gre_init(void) 322 { 323 324 if (!IS_DEFAULT_VNET(curvnet)) 325 return; 326 ipv6_srcaddrtab = ip6_encap_register_srcaddr(in6_gre_srcaddr, 327 NULL, M_WAITOK); 328 ecookie = ip6_encap_attach(&ipv6_encap_cfg, NULL, M_WAITOK); 329 } 330 331 void 332 in6_gre_uninit(void) 333 { 334 335 if (IS_DEFAULT_VNET(curvnet)) { 336 ip6_encap_detach(ecookie); 337 ip6_encap_unregister_srcaddr(ipv6_srcaddrtab); 338 } 339 if (V_ipv6_hashtbl != NULL) { 340 gre_hashdestroy(V_ipv6_hashtbl); 341 gre_hashdestroy(V_ipv6_srchashtbl); 342 } 343 } 344