1 /* $KAME: ip_encap.c,v 1.41 2001/03/15 08:35:08 itojun Exp $ */ 2 3 /*- 4 * SPDX-License-Identifier: BSD-3-Clause 5 * 6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 7 * Copyright (c) 2018 Andrey V. Elsukov <ae@FreeBSD.org> 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the project nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 /* 35 * My grandfather said that there's a devil inside tunnelling technology... 36 * 37 * We have surprisingly many protocols that want packets with IP protocol 38 * #4 or #41. Here's a list of protocols that want protocol #41: 39 * RFC1933 configured tunnel 40 * RFC1933 automatic tunnel 41 * RFC2401 IPsec tunnel 42 * RFC2473 IPv6 generic packet tunnelling 43 * RFC2529 6over4 tunnel 44 * mobile-ip6 (uses RFC2473) 45 * RFC3056 6to4 tunnel 46 * isatap tunnel 47 * Here's a list of protocol that want protocol #4: 48 * RFC1853 IPv4-in-IPv4 tunnelling 49 * RFC2003 IPv4 encapsulation within IPv4 50 * RFC2344 reverse tunnelling for mobile-ip4 51 * RFC2401 IPsec tunnel 52 * Well, what can I say. They impose different en/decapsulation mechanism 53 * from each other, so they need separate protocol handler. The only one 54 * we can easily determine by protocol # is IPsec, which always has 55 * AH/ESP/IPComp header right after outer IP header. 56 * 57 * So, clearly good old protosw does not work for protocol #4 and #41. 58 * The code will let you match protocol via src/dst address pair. 59 */ 60 61 #include "opt_inet.h" 62 #include "opt_inet6.h" 63 64 #include <sys/param.h> 65 #include <sys/systm.h> 66 #include <sys/eventhandler.h> 67 #include <sys/kernel.h> 68 #include <sys/lock.h> 69 #include <sys/malloc.h> 70 #include <sys/mutex.h> 71 #include <sys/mbuf.h> 72 #include <sys/errno.h> 73 #include <sys/socket.h> 74 75 #include <net/if.h> 76 #include <net/if_var.h> 77 78 #include <netinet/in.h> 79 #include <netinet/ip_var.h> 80 #include <netinet/ip_encap.h> 81 82 #ifdef INET6 83 #include <netinet6/ip6_var.h> 84 #endif 85 86 static MALLOC_DEFINE(M_NETADDR, "encap_export_host", 87 "Export host address structure"); 88 89 struct encaptab { 90 CK_LIST_ENTRY(encaptab) chain; 91 int proto; 92 int min_length; 93 int exact_match; 94 void *arg; 95 96 encap_lookup_t lookup; 97 encap_check_t check; 98 encap_input_t input; 99 }; 100 101 struct srcaddrtab { 102 CK_LIST_ENTRY(srcaddrtab) chain; 103 104 encap_srcaddr_t srcaddr; 105 void *arg; 106 }; 107 108 CK_LIST_HEAD(encaptab_head, encaptab); 109 CK_LIST_HEAD(srcaddrtab_head, srcaddrtab); 110 #ifdef INET 111 static struct encaptab_head ipv4_encaptab = CK_LIST_HEAD_INITIALIZER(); 112 static struct srcaddrtab_head ipv4_srcaddrtab = CK_LIST_HEAD_INITIALIZER(); 113 #endif 114 #ifdef INET6 115 static struct encaptab_head ipv6_encaptab = CK_LIST_HEAD_INITIALIZER(); 116 static struct srcaddrtab_head ipv6_srcaddrtab = CK_LIST_HEAD_INITIALIZER(); 117 #endif 118 119 static struct mtx encapmtx, srcaddrmtx; 120 MTX_SYSINIT(encapmtx, &encapmtx, "encapmtx", MTX_DEF); 121 MTX_SYSINIT(srcaddrmtx, &srcaddrmtx, "srcaddrmtx", MTX_DEF); 122 #define ENCAP_WLOCK() mtx_lock(&encapmtx) 123 #define ENCAP_WUNLOCK() mtx_unlock(&encapmtx) 124 #define ENCAP_RLOCK_TRACKER struct epoch_tracker encap_et 125 #define ENCAP_RLOCK() NET_EPOCH_ENTER(encap_et) 126 #define ENCAP_RUNLOCK() NET_EPOCH_EXIT(encap_et) 127 #define ENCAP_WAIT() NET_EPOCH_WAIT() 128 129 #define SRCADDR_WLOCK() mtx_lock(&srcaddrmtx) 130 #define SRCADDR_WUNLOCK() mtx_unlock(&srcaddrmtx) 131 #define SRCADDR_RLOCK_TRACKER struct epoch_tracker srcaddr_et 132 #define SRCADDR_RLOCK() \ 133 epoch_enter_preempt(net_epoch_preempt, &srcaddr_et) 134 #define SRCADDR_RUNLOCK() \ 135 epoch_exit_preempt(net_epoch_preempt, &srcaddr_et) 136 #define SRCADDR_WAIT() epoch_wait_preempt(net_epoch_preempt) 137 138 /* 139 * ifaddr_event_ext handler. 140 * 141 * Tunnelling interfaces may request the kernel to notify when 142 * some interface addresses appears or disappears. Usually tunnelling 143 * interface must use an address configured on the local machine as 144 * ingress address to be able receive datagramms and do not send 145 * spoofed packets. 146 */ 147 static void 148 srcaddr_change_event(void *arg __unused, struct ifnet *ifp, 149 struct ifaddr *ifa, int event) 150 { 151 SRCADDR_RLOCK_TRACKER; 152 struct srcaddrtab_head *head; 153 struct srcaddrtab *p; 154 155 /* Support for old ifaddr_event. */ 156 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 157 158 switch (ifa->ifa_addr->sa_family) { 159 #ifdef INET 160 case AF_INET: 161 head = &ipv4_srcaddrtab; 162 break; 163 #endif 164 #ifdef INET6 165 case AF_INET6: 166 head = &ipv6_srcaddrtab; 167 break; 168 #endif 169 default: 170 /* ignore event */ 171 return; 172 } 173 174 SRCADDR_RLOCK(); 175 CK_LIST_FOREACH(p, head, chain) { 176 (*p->srcaddr)(p->arg, ifa->ifa_addr, event); 177 } 178 SRCADDR_RUNLOCK(); 179 } 180 EVENTHANDLER_DEFINE(ifaddr_event_ext, srcaddr_change_event, NULL, 0); 181 182 static struct srcaddrtab * 183 encap_register_srcaddr(struct srcaddrtab_head *head, encap_srcaddr_t func, 184 void *arg, int mflags) 185 { 186 struct srcaddrtab *p, *tmp; 187 188 if (func == NULL) 189 return (NULL); 190 p = malloc(sizeof(*p), M_NETADDR, mflags); 191 if (p == NULL) 192 return (NULL); 193 p->srcaddr = func; 194 p->arg = arg; 195 196 SRCADDR_WLOCK(); 197 CK_LIST_FOREACH(tmp, head, chain) { 198 if (func == tmp->srcaddr && arg == tmp->arg) 199 break; 200 } 201 if (tmp == NULL) 202 CK_LIST_INSERT_HEAD(head, p, chain); 203 SRCADDR_WUNLOCK(); 204 205 if (tmp != NULL) { 206 free(p, M_NETADDR); 207 p = tmp; 208 } 209 return (p); 210 } 211 212 static int 213 encap_unregister_srcaddr(struct srcaddrtab_head *head, 214 const struct srcaddrtab *cookie) 215 { 216 struct srcaddrtab *p; 217 218 SRCADDR_WLOCK(); 219 CK_LIST_FOREACH(p, head, chain) { 220 if (p == cookie) { 221 CK_LIST_REMOVE(p, chain); 222 SRCADDR_WUNLOCK(); 223 SRCADDR_WAIT(); 224 free(p, M_NETADDR); 225 return (0); 226 } 227 } 228 SRCADDR_WUNLOCK(); 229 return (EINVAL); 230 } 231 232 static struct encaptab * 233 encap_attach(struct encaptab_head *head, const struct encap_config *cfg, 234 void *arg, int mflags) 235 { 236 struct encaptab *ep, *tmp; 237 238 if (cfg == NULL || cfg->input == NULL || 239 (cfg->check == NULL && cfg->lookup == NULL) || 240 (cfg->lookup != NULL && cfg->exact_match != ENCAP_DRV_LOOKUP) || 241 (cfg->exact_match == ENCAP_DRV_LOOKUP && cfg->lookup == NULL)) 242 return (NULL); 243 244 ep = malloc(sizeof(*ep), M_NETADDR, mflags); 245 if (ep == NULL) 246 return (NULL); 247 248 ep->proto = cfg->proto; 249 ep->min_length = cfg->min_length; 250 ep->exact_match = cfg->exact_match; 251 ep->arg = arg; 252 ep->lookup = cfg->exact_match == ENCAP_DRV_LOOKUP ? cfg->lookup: NULL; 253 ep->check = cfg->exact_match != ENCAP_DRV_LOOKUP ? cfg->check: NULL; 254 ep->input = cfg->input; 255 256 ENCAP_WLOCK(); 257 CK_LIST_FOREACH(tmp, head, chain) { 258 if (tmp->exact_match <= ep->exact_match) 259 break; 260 } 261 if (tmp == NULL) 262 CK_LIST_INSERT_HEAD(head, ep, chain); 263 else 264 CK_LIST_INSERT_BEFORE(tmp, ep, chain); 265 ENCAP_WUNLOCK(); 266 return (ep); 267 } 268 269 static int 270 encap_detach(struct encaptab_head *head, const struct encaptab *cookie) 271 { 272 struct encaptab *ep; 273 274 ENCAP_WLOCK(); 275 CK_LIST_FOREACH(ep, head, chain) { 276 if (ep == cookie) { 277 CK_LIST_REMOVE(ep, chain); 278 ENCAP_WUNLOCK(); 279 ENCAP_WAIT(); 280 free(ep, M_NETADDR); 281 return (0); 282 } 283 } 284 ENCAP_WUNLOCK(); 285 return (EINVAL); 286 } 287 288 static int 289 encap_input(struct encaptab_head *head, struct mbuf *m, int off, int proto) 290 { 291 ENCAP_RLOCK_TRACKER; 292 struct encaptab *ep, *match; 293 void *arg; 294 int matchprio, ret; 295 296 match = NULL; 297 matchprio = 0; 298 299 ENCAP_RLOCK(); 300 CK_LIST_FOREACH(ep, head, chain) { 301 if (ep->proto >= 0 && ep->proto != proto) 302 continue; 303 if (ep->min_length > m->m_pkthdr.len) 304 continue; 305 if (ep->exact_match == ENCAP_DRV_LOOKUP) 306 ret = (*ep->lookup)(m, off, proto, &arg); 307 else 308 ret = (*ep->check)(m, off, proto, ep->arg); 309 if (ret <= 0) 310 continue; 311 if (ret > matchprio) { 312 match = ep; 313 if (ep->exact_match != ENCAP_DRV_LOOKUP) 314 arg = ep->arg; 315 /* 316 * No need to continue the search, we got the 317 * exact match. 318 */ 319 if (ret >= ep->exact_match) 320 break; 321 matchprio = ret; 322 } 323 } 324 325 if (match != NULL) { 326 /* found a match, "match" has the best one */ 327 ret = (*match->input)(m, off, proto, arg); 328 ENCAP_RUNLOCK(); 329 MPASS(ret == IPPROTO_DONE); 330 return (IPPROTO_DONE); 331 } 332 ENCAP_RUNLOCK(); 333 return (0); 334 } 335 336 #ifdef INET 337 const struct srcaddrtab * 338 ip_encap_register_srcaddr(encap_srcaddr_t func, void *arg, int mflags) 339 { 340 341 return (encap_register_srcaddr(&ipv4_srcaddrtab, func, arg, mflags)); 342 } 343 344 int 345 ip_encap_unregister_srcaddr(const struct srcaddrtab *cookie) 346 { 347 348 return (encap_unregister_srcaddr(&ipv4_srcaddrtab, cookie)); 349 } 350 351 const struct encaptab * 352 ip_encap_attach(const struct encap_config *cfg, void *arg, int mflags) 353 { 354 355 return (encap_attach(&ipv4_encaptab, cfg, arg, mflags)); 356 } 357 358 int 359 ip_encap_detach(const struct encaptab *cookie) 360 { 361 362 return (encap_detach(&ipv4_encaptab, cookie)); 363 } 364 365 int 366 encap4_input(struct mbuf **mp, int *offp, int proto) 367 { 368 369 if (encap_input(&ipv4_encaptab, *mp, *offp, proto) != IPPROTO_DONE) 370 return (rip_input(mp, offp, proto)); 371 return (IPPROTO_DONE); 372 } 373 #endif /* INET */ 374 375 #ifdef INET6 376 const struct srcaddrtab * 377 ip6_encap_register_srcaddr(encap_srcaddr_t func, void *arg, int mflags) 378 { 379 380 return (encap_register_srcaddr(&ipv6_srcaddrtab, func, arg, mflags)); 381 } 382 383 int 384 ip6_encap_unregister_srcaddr(const struct srcaddrtab *cookie) 385 { 386 387 return (encap_unregister_srcaddr(&ipv6_srcaddrtab, cookie)); 388 } 389 390 const struct encaptab * 391 ip6_encap_attach(const struct encap_config *cfg, void *arg, int mflags) 392 { 393 394 return (encap_attach(&ipv6_encaptab, cfg, arg, mflags)); 395 } 396 397 int 398 ip6_encap_detach(const struct encaptab *cookie) 399 { 400 401 return (encap_detach(&ipv6_encaptab, cookie)); 402 } 403 404 int 405 encap6_input(struct mbuf **mp, int *offp, int proto) 406 { 407 408 if (encap_input(&ipv6_encaptab, *mp, *offp, proto) != IPPROTO_DONE) 409 return (rip6_input(mp, offp, proto)); 410 return (IPPROTO_DONE); 411 } 412 #endif /* INET6 */ 413