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 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the project nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 /* 34 * My grandfather said that there's a devil inside tunnelling technology... 35 * 36 * We have surprisingly many protocols that want packets with IP protocol 37 * #4 or #41. Here's a list of protocols that want protocol #41: 38 * RFC1933 configured tunnel 39 * RFC1933 automatic tunnel 40 * RFC2401 IPsec tunnel 41 * RFC2473 IPv6 generic packet tunnelling 42 * RFC2529 6over4 tunnel 43 * mobile-ip6 (uses RFC2473) 44 * RFC3056 6to4 tunnel 45 * isatap tunnel 46 * Here's a list of protocol that want protocol #4: 47 * RFC1853 IPv4-in-IPv4 tunnelling 48 * RFC2003 IPv4 encapsulation within IPv4 49 * RFC2344 reverse tunnelling for mobile-ip4 50 * RFC2401 IPsec tunnel 51 * Well, what can I say. They impose different en/decapsulation mechanism 52 * from each other, so they need separate protocol handler. The only one 53 * we can easily determine by protocol # is IPsec, which always has 54 * AH/ESP/IPComp header right after outer IP header. 55 * 56 * So, clearly good old protosw does not work for protocol #4 and #41. 57 * The code will let you match protocol via src/dst address pair. 58 */ 59 /* XXX is M_NETADDR correct? */ 60 61 #include <sys/cdefs.h> 62 __FBSDID("$FreeBSD$"); 63 64 #include "opt_mrouting.h" 65 #include "opt_inet.h" 66 #include "opt_inet6.h" 67 68 #include <sys/param.h> 69 #include <sys/systm.h> 70 #include <sys/lock.h> 71 #include <sys/mutex.h> 72 #include <sys/socket.h> 73 #include <sys/sockio.h> 74 #include <sys/mbuf.h> 75 #include <sys/errno.h> 76 #include <sys/protosw.h> 77 #include <sys/queue.h> 78 79 #include <net/if.h> 80 #include <net/route.h> 81 82 #include <netinet/in.h> 83 #include <netinet/in_systm.h> 84 #include <netinet/ip.h> 85 #include <netinet/ip_var.h> 86 #include <netinet/ip_encap.h> 87 88 #ifdef INET6 89 #include <netinet/ip6.h> 90 #include <netinet6/ip6_var.h> 91 #endif 92 93 #include <machine/stdarg.h> 94 95 #include <sys/kernel.h> 96 #include <sys/malloc.h> 97 static MALLOC_DEFINE(M_NETADDR, "encap_export_host", "Export host address structure"); 98 99 static void encap_add(struct encaptab *); 100 static int mask_match(const struct encaptab *, const struct sockaddr *, 101 const struct sockaddr *); 102 static void encap_fillarg(struct mbuf *, void *); 103 104 /* 105 * All global variables in ip_encap.c are locked using encapmtx. 106 */ 107 static struct mtx encapmtx; 108 MTX_SYSINIT(encapmtx, &encapmtx, "encapmtx", MTX_DEF); 109 static LIST_HEAD(, encaptab) encaptab = LIST_HEAD_INITIALIZER(encaptab); 110 111 /* 112 * We currently keey encap_init() for source code compatibility reasons -- 113 * it's referenced by KAME pieces in netinet6. 114 */ 115 void 116 encap_init(void) 117 { 118 } 119 120 #ifdef INET 121 int 122 encap4_input(struct mbuf **mp, int *offp, int proto) 123 { 124 struct ip *ip; 125 struct mbuf *m; 126 struct sockaddr_in s, d; 127 const struct protosw *psw; 128 struct encaptab *ep, *match; 129 void *arg; 130 int matchprio, off, prio; 131 132 m = *mp; 133 off = *offp; 134 ip = mtod(m, struct ip *); 135 136 bzero(&s, sizeof(s)); 137 s.sin_family = AF_INET; 138 s.sin_len = sizeof(struct sockaddr_in); 139 s.sin_addr = ip->ip_src; 140 bzero(&d, sizeof(d)); 141 d.sin_family = AF_INET; 142 d.sin_len = sizeof(struct sockaddr_in); 143 d.sin_addr = ip->ip_dst; 144 145 arg = NULL; 146 psw = NULL; 147 match = NULL; 148 matchprio = 0; 149 mtx_lock(&encapmtx); 150 LIST_FOREACH(ep, &encaptab, chain) { 151 if (ep->af != AF_INET) 152 continue; 153 if (ep->proto >= 0 && ep->proto != proto) 154 continue; 155 if (ep->func) 156 prio = (*ep->func)(m, off, proto, ep->arg); 157 else { 158 /* 159 * it's inbound traffic, we need to match in reverse 160 * order 161 */ 162 prio = mask_match(ep, (struct sockaddr *)&d, 163 (struct sockaddr *)&s); 164 } 165 166 /* 167 * We prioritize the matches by using bit length of the 168 * matches. mask_match() and user-supplied matching function 169 * should return the bit length of the matches (for example, 170 * if both src/dst are matched for IPv4, 64 should be returned). 171 * 0 or negative return value means "it did not match". 172 * 173 * The question is, since we have two "mask" portion, we 174 * cannot really define total order between entries. 175 * For example, which of these should be preferred? 176 * mask_match() returns 48 (32 + 16) for both of them. 177 * src=3ffe::/16, dst=3ffe:501::/32 178 * src=3ffe:501::/32, dst=3ffe::/16 179 * 180 * We need to loop through all the possible candidates 181 * to get the best match - the search takes O(n) for 182 * n attachments (i.e. interfaces). 183 */ 184 if (prio <= 0) 185 continue; 186 if (prio > matchprio) { 187 matchprio = prio; 188 match = ep; 189 } 190 } 191 if (match != NULL) { 192 psw = match->psw; 193 arg = match->arg; 194 } 195 mtx_unlock(&encapmtx); 196 197 if (match != NULL) { 198 /* found a match, "match" has the best one */ 199 if (psw != NULL && psw->pr_input != NULL) { 200 encap_fillarg(m, arg); 201 (*psw->pr_input)(mp, offp, proto); 202 } else 203 m_freem(m); 204 return (IPPROTO_DONE); 205 } 206 207 /* last resort: inject to raw socket */ 208 return (rip_input(mp, offp, proto)); 209 } 210 #endif 211 212 #ifdef INET6 213 int 214 encap6_input(struct mbuf **mp, int *offp, int proto) 215 { 216 struct mbuf *m = *mp; 217 struct ip6_hdr *ip6; 218 struct sockaddr_in6 s, d; 219 const struct protosw *psw; 220 struct encaptab *ep, *match; 221 void *arg; 222 int prio, matchprio; 223 224 ip6 = mtod(m, struct ip6_hdr *); 225 226 bzero(&s, sizeof(s)); 227 s.sin6_family = AF_INET6; 228 s.sin6_len = sizeof(struct sockaddr_in6); 229 s.sin6_addr = ip6->ip6_src; 230 bzero(&d, sizeof(d)); 231 d.sin6_family = AF_INET6; 232 d.sin6_len = sizeof(struct sockaddr_in6); 233 d.sin6_addr = ip6->ip6_dst; 234 235 arg = NULL; 236 psw = NULL; 237 match = NULL; 238 matchprio = 0; 239 mtx_lock(&encapmtx); 240 LIST_FOREACH(ep, &encaptab, chain) { 241 if (ep->af != AF_INET6) 242 continue; 243 if (ep->proto >= 0 && ep->proto != proto) 244 continue; 245 if (ep->func) 246 prio = (*ep->func)(m, *offp, proto, ep->arg); 247 else { 248 /* 249 * it's inbound traffic, we need to match in reverse 250 * order 251 */ 252 prio = mask_match(ep, (struct sockaddr *)&d, 253 (struct sockaddr *)&s); 254 } 255 256 /* see encap4_input() for issues here */ 257 if (prio <= 0) 258 continue; 259 if (prio > matchprio) { 260 matchprio = prio; 261 match = ep; 262 } 263 } 264 if (match != NULL) { 265 psw = match->psw; 266 arg = match->arg; 267 } 268 mtx_unlock(&encapmtx); 269 270 if (match != NULL) { 271 /* found a match */ 272 if (psw != NULL && psw->pr_input != NULL) { 273 encap_fillarg(m, arg); 274 return (*psw->pr_input)(mp, offp, proto); 275 } else { 276 m_freem(m); 277 return (IPPROTO_DONE); 278 } 279 } 280 281 /* last resort: inject to raw socket */ 282 return rip6_input(mp, offp, proto); 283 } 284 #endif 285 286 /*lint -sem(encap_add, custodial(1)) */ 287 static void 288 encap_add(struct encaptab *ep) 289 { 290 291 mtx_assert(&encapmtx, MA_OWNED); 292 LIST_INSERT_HEAD(&encaptab, ep, chain); 293 } 294 295 /* 296 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side. 297 * length of mask (sm and dm) is assumed to be same as sp/dp. 298 * Return value will be necessary as input (cookie) for encap_detach(). 299 */ 300 const struct encaptab * 301 encap_attach(int af, int proto, const struct sockaddr *sp, 302 const struct sockaddr *sm, const struct sockaddr *dp, 303 const struct sockaddr *dm, const struct protosw *psw, void *arg) 304 { 305 struct encaptab *ep; 306 307 /* sanity check on args */ 308 if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst)) 309 return (NULL); 310 if (sp->sa_len != dp->sa_len) 311 return (NULL); 312 if (af != sp->sa_family || af != dp->sa_family) 313 return (NULL); 314 315 /* check if anyone have already attached with exactly same config */ 316 mtx_lock(&encapmtx); 317 LIST_FOREACH(ep, &encaptab, chain) { 318 if (ep->af != af) 319 continue; 320 if (ep->proto != proto) 321 continue; 322 if (ep->src.ss_len != sp->sa_len || 323 bcmp(&ep->src, sp, sp->sa_len) != 0 || 324 bcmp(&ep->srcmask, sm, sp->sa_len) != 0) 325 continue; 326 if (ep->dst.ss_len != dp->sa_len || 327 bcmp(&ep->dst, dp, dp->sa_len) != 0 || 328 bcmp(&ep->dstmask, dm, dp->sa_len) != 0) 329 continue; 330 331 mtx_unlock(&encapmtx); 332 return (NULL); 333 } 334 335 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT); /*XXX*/ 336 if (ep == NULL) { 337 mtx_unlock(&encapmtx); 338 return (NULL); 339 } 340 bzero(ep, sizeof(*ep)); 341 342 ep->af = af; 343 ep->proto = proto; 344 bcopy(sp, &ep->src, sp->sa_len); 345 bcopy(sm, &ep->srcmask, sp->sa_len); 346 bcopy(dp, &ep->dst, dp->sa_len); 347 bcopy(dm, &ep->dstmask, dp->sa_len); 348 ep->psw = psw; 349 ep->arg = arg; 350 351 encap_add(ep); 352 mtx_unlock(&encapmtx); 353 return (ep); 354 } 355 356 const struct encaptab * 357 encap_attach_func(int af, int proto, 358 int (*func)(const struct mbuf *, int, int, void *), 359 const struct protosw *psw, void *arg) 360 { 361 struct encaptab *ep; 362 363 /* sanity check on args */ 364 if (!func) 365 return (NULL); 366 367 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT); /*XXX*/ 368 if (ep == NULL) 369 return (NULL); 370 bzero(ep, sizeof(*ep)); 371 372 ep->af = af; 373 ep->proto = proto; 374 ep->func = func; 375 ep->psw = psw; 376 ep->arg = arg; 377 378 mtx_lock(&encapmtx); 379 encap_add(ep); 380 mtx_unlock(&encapmtx); 381 return (ep); 382 } 383 384 int 385 encap_detach(const struct encaptab *cookie) 386 { 387 const struct encaptab *ep = cookie; 388 struct encaptab *p; 389 390 mtx_lock(&encapmtx); 391 LIST_FOREACH(p, &encaptab, chain) { 392 if (p == ep) { 393 LIST_REMOVE(p, chain); 394 mtx_unlock(&encapmtx); 395 free(p, M_NETADDR); /*XXX*/ 396 return 0; 397 } 398 } 399 mtx_unlock(&encapmtx); 400 401 return EINVAL; 402 } 403 404 static int 405 mask_match(const struct encaptab *ep, const struct sockaddr *sp, 406 const struct sockaddr *dp) 407 { 408 struct sockaddr_storage s; 409 struct sockaddr_storage d; 410 int i; 411 const u_int8_t *p, *q; 412 u_int8_t *r; 413 int matchlen; 414 415 if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d)) 416 return 0; 417 if (sp->sa_family != ep->af || dp->sa_family != ep->af) 418 return 0; 419 if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len) 420 return 0; 421 422 matchlen = 0; 423 424 p = (const u_int8_t *)sp; 425 q = (const u_int8_t *)&ep->srcmask; 426 r = (u_int8_t *)&s; 427 for (i = 0 ; i < sp->sa_len; i++) { 428 r[i] = p[i] & q[i]; 429 /* XXX estimate */ 430 matchlen += (q[i] ? 8 : 0); 431 } 432 433 p = (const u_int8_t *)dp; 434 q = (const u_int8_t *)&ep->dstmask; 435 r = (u_int8_t *)&d; 436 for (i = 0 ; i < dp->sa_len; i++) { 437 r[i] = p[i] & q[i]; 438 /* XXX rough estimate */ 439 matchlen += (q[i] ? 8 : 0); 440 } 441 442 /* need to overwrite len/family portion as we don't compare them */ 443 s.ss_len = sp->sa_len; 444 s.ss_family = sp->sa_family; 445 d.ss_len = dp->sa_len; 446 d.ss_family = dp->sa_family; 447 448 if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 && 449 bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) { 450 return matchlen; 451 } else 452 return 0; 453 } 454 455 static void 456 encap_fillarg(struct mbuf *m, void *arg) 457 { 458 struct m_tag *tag; 459 460 if (arg != NULL) { 461 tag = m_tag_get(PACKET_TAG_ENCAP, sizeof(void *), M_NOWAIT); 462 if (tag != NULL) { 463 *(void**)(tag+1) = arg; 464 m_tag_prepend(m, tag); 465 } 466 } 467 } 468 469 void * 470 encap_getarg(struct mbuf *m) 471 { 472 void *p = NULL; 473 struct m_tag *tag; 474 475 tag = m_tag_find(m, PACKET_TAG_ENCAP, NULL); 476 if (tag) { 477 p = *(void**)(tag+1); 478 m_tag_delete(m, tag); 479 } 480 return p; 481 } 482