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 #ifdef INET 112 int 113 encap4_input(struct mbuf **mp, int *offp, int proto) 114 { 115 struct ip *ip; 116 struct mbuf *m; 117 struct sockaddr_in s, d; 118 const struct protosw *psw; 119 struct encaptab *ep, *match; 120 void *arg; 121 int matchprio, off, prio; 122 123 m = *mp; 124 off = *offp; 125 ip = mtod(m, struct ip *); 126 127 bzero(&s, sizeof(s)); 128 s.sin_family = AF_INET; 129 s.sin_len = sizeof(struct sockaddr_in); 130 s.sin_addr = ip->ip_src; 131 bzero(&d, sizeof(d)); 132 d.sin_family = AF_INET; 133 d.sin_len = sizeof(struct sockaddr_in); 134 d.sin_addr = ip->ip_dst; 135 136 arg = NULL; 137 psw = NULL; 138 match = NULL; 139 matchprio = 0; 140 mtx_lock(&encapmtx); 141 LIST_FOREACH(ep, &encaptab, chain) { 142 if (ep->af != AF_INET) 143 continue; 144 if (ep->proto >= 0 && ep->proto != proto) 145 continue; 146 if (ep->func) 147 prio = (*ep->func)(m, off, proto, ep->arg); 148 else { 149 /* 150 * it's inbound traffic, we need to match in reverse 151 * order 152 */ 153 prio = mask_match(ep, (struct sockaddr *)&d, 154 (struct sockaddr *)&s); 155 } 156 157 /* 158 * We prioritize the matches by using bit length of the 159 * matches. mask_match() and user-supplied matching function 160 * should return the bit length of the matches (for example, 161 * if both src/dst are matched for IPv4, 64 should be returned). 162 * 0 or negative return value means "it did not match". 163 * 164 * The question is, since we have two "mask" portion, we 165 * cannot really define total order between entries. 166 * For example, which of these should be preferred? 167 * mask_match() returns 48 (32 + 16) for both of them. 168 * src=3ffe::/16, dst=3ffe:501::/32 169 * src=3ffe:501::/32, dst=3ffe::/16 170 * 171 * We need to loop through all the possible candidates 172 * to get the best match - the search takes O(n) for 173 * n attachments (i.e. interfaces). 174 */ 175 if (prio <= 0) 176 continue; 177 if (prio > matchprio) { 178 matchprio = prio; 179 match = ep; 180 } 181 } 182 if (match != NULL) { 183 psw = match->psw; 184 arg = match->arg; 185 } 186 mtx_unlock(&encapmtx); 187 188 if (match != NULL) { 189 /* found a match, "match" has the best one */ 190 if (psw != NULL && psw->pr_input != NULL) { 191 encap_fillarg(m, arg); 192 (*psw->pr_input)(mp, offp, proto); 193 } else 194 m_freem(m); 195 return (IPPROTO_DONE); 196 } 197 198 /* last resort: inject to raw socket */ 199 return (rip_input(mp, offp, proto)); 200 } 201 #endif 202 203 #ifdef INET6 204 int 205 encap6_input(struct mbuf **mp, int *offp, int proto) 206 { 207 struct mbuf *m = *mp; 208 struct ip6_hdr *ip6; 209 struct sockaddr_in6 s, d; 210 const struct protosw *psw; 211 struct encaptab *ep, *match; 212 void *arg; 213 int prio, matchprio; 214 215 ip6 = mtod(m, struct ip6_hdr *); 216 217 bzero(&s, sizeof(s)); 218 s.sin6_family = AF_INET6; 219 s.sin6_len = sizeof(struct sockaddr_in6); 220 s.sin6_addr = ip6->ip6_src; 221 bzero(&d, sizeof(d)); 222 d.sin6_family = AF_INET6; 223 d.sin6_len = sizeof(struct sockaddr_in6); 224 d.sin6_addr = ip6->ip6_dst; 225 226 arg = NULL; 227 psw = NULL; 228 match = NULL; 229 matchprio = 0; 230 mtx_lock(&encapmtx); 231 LIST_FOREACH(ep, &encaptab, chain) { 232 if (ep->af != AF_INET6) 233 continue; 234 if (ep->proto >= 0 && ep->proto != proto) 235 continue; 236 if (ep->func) 237 prio = (*ep->func)(m, *offp, proto, ep->arg); 238 else { 239 /* 240 * it's inbound traffic, we need to match in reverse 241 * order 242 */ 243 prio = mask_match(ep, (struct sockaddr *)&d, 244 (struct sockaddr *)&s); 245 } 246 247 /* see encap4_input() for issues here */ 248 if (prio <= 0) 249 continue; 250 if (prio > matchprio) { 251 matchprio = prio; 252 match = ep; 253 } 254 } 255 if (match != NULL) { 256 psw = match->psw; 257 arg = match->arg; 258 } 259 mtx_unlock(&encapmtx); 260 261 if (match != NULL) { 262 /* found a match */ 263 if (psw != NULL && psw->pr_input != NULL) { 264 encap_fillarg(m, arg); 265 return (*psw->pr_input)(mp, offp, proto); 266 } else { 267 m_freem(m); 268 return (IPPROTO_DONE); 269 } 270 } 271 272 /* last resort: inject to raw socket */ 273 return rip6_input(mp, offp, proto); 274 } 275 #endif 276 277 /*lint -sem(encap_add, custodial(1)) */ 278 static void 279 encap_add(struct encaptab *ep) 280 { 281 282 mtx_assert(&encapmtx, MA_OWNED); 283 LIST_INSERT_HEAD(&encaptab, ep, chain); 284 } 285 286 /* 287 * sp (src ptr) is always my side, and dp (dst ptr) is always remote side. 288 * length of mask (sm and dm) is assumed to be same as sp/dp. 289 * Return value will be necessary as input (cookie) for encap_detach(). 290 */ 291 const struct encaptab * 292 encap_attach(int af, int proto, const struct sockaddr *sp, 293 const struct sockaddr *sm, const struct sockaddr *dp, 294 const struct sockaddr *dm, const struct protosw *psw, void *arg) 295 { 296 struct encaptab *ep; 297 298 /* sanity check on args */ 299 if (sp->sa_len > sizeof(ep->src) || dp->sa_len > sizeof(ep->dst)) 300 return (NULL); 301 if (sp->sa_len != dp->sa_len) 302 return (NULL); 303 if (af != sp->sa_family || af != dp->sa_family) 304 return (NULL); 305 306 /* check if anyone have already attached with exactly same config */ 307 mtx_lock(&encapmtx); 308 LIST_FOREACH(ep, &encaptab, chain) { 309 if (ep->af != af) 310 continue; 311 if (ep->proto != proto) 312 continue; 313 if (ep->src.ss_len != sp->sa_len || 314 bcmp(&ep->src, sp, sp->sa_len) != 0 || 315 bcmp(&ep->srcmask, sm, sp->sa_len) != 0) 316 continue; 317 if (ep->dst.ss_len != dp->sa_len || 318 bcmp(&ep->dst, dp, dp->sa_len) != 0 || 319 bcmp(&ep->dstmask, dm, dp->sa_len) != 0) 320 continue; 321 322 mtx_unlock(&encapmtx); 323 return (NULL); 324 } 325 326 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT); /*XXX*/ 327 if (ep == NULL) { 328 mtx_unlock(&encapmtx); 329 return (NULL); 330 } 331 bzero(ep, sizeof(*ep)); 332 333 ep->af = af; 334 ep->proto = proto; 335 bcopy(sp, &ep->src, sp->sa_len); 336 bcopy(sm, &ep->srcmask, sp->sa_len); 337 bcopy(dp, &ep->dst, dp->sa_len); 338 bcopy(dm, &ep->dstmask, dp->sa_len); 339 ep->psw = psw; 340 ep->arg = arg; 341 342 encap_add(ep); 343 mtx_unlock(&encapmtx); 344 return (ep); 345 } 346 347 const struct encaptab * 348 encap_attach_func(int af, int proto, 349 int (*func)(const struct mbuf *, int, int, void *), 350 const struct protosw *psw, void *arg) 351 { 352 struct encaptab *ep; 353 354 /* sanity check on args */ 355 if (!func) 356 return (NULL); 357 358 ep = malloc(sizeof(*ep), M_NETADDR, M_NOWAIT); /*XXX*/ 359 if (ep == NULL) 360 return (NULL); 361 bzero(ep, sizeof(*ep)); 362 363 ep->af = af; 364 ep->proto = proto; 365 ep->func = func; 366 ep->psw = psw; 367 ep->arg = arg; 368 369 mtx_lock(&encapmtx); 370 encap_add(ep); 371 mtx_unlock(&encapmtx); 372 return (ep); 373 } 374 375 int 376 encap_detach(const struct encaptab *cookie) 377 { 378 const struct encaptab *ep = cookie; 379 struct encaptab *p; 380 381 mtx_lock(&encapmtx); 382 LIST_FOREACH(p, &encaptab, chain) { 383 if (p == ep) { 384 LIST_REMOVE(p, chain); 385 mtx_unlock(&encapmtx); 386 free(p, M_NETADDR); /*XXX*/ 387 return 0; 388 } 389 } 390 mtx_unlock(&encapmtx); 391 392 return EINVAL; 393 } 394 395 static int 396 mask_match(const struct encaptab *ep, const struct sockaddr *sp, 397 const struct sockaddr *dp) 398 { 399 struct sockaddr_storage s; 400 struct sockaddr_storage d; 401 int i; 402 const u_int8_t *p, *q; 403 u_int8_t *r; 404 int matchlen; 405 406 if (sp->sa_len > sizeof(s) || dp->sa_len > sizeof(d)) 407 return 0; 408 if (sp->sa_family != ep->af || dp->sa_family != ep->af) 409 return 0; 410 if (sp->sa_len != ep->src.ss_len || dp->sa_len != ep->dst.ss_len) 411 return 0; 412 413 matchlen = 0; 414 415 p = (const u_int8_t *)sp; 416 q = (const u_int8_t *)&ep->srcmask; 417 r = (u_int8_t *)&s; 418 for (i = 0 ; i < sp->sa_len; i++) { 419 r[i] = p[i] & q[i]; 420 /* XXX estimate */ 421 matchlen += (q[i] ? 8 : 0); 422 } 423 424 p = (const u_int8_t *)dp; 425 q = (const u_int8_t *)&ep->dstmask; 426 r = (u_int8_t *)&d; 427 for (i = 0 ; i < dp->sa_len; i++) { 428 r[i] = p[i] & q[i]; 429 /* XXX rough estimate */ 430 matchlen += (q[i] ? 8 : 0); 431 } 432 433 /* need to overwrite len/family portion as we don't compare them */ 434 s.ss_len = sp->sa_len; 435 s.ss_family = sp->sa_family; 436 d.ss_len = dp->sa_len; 437 d.ss_family = dp->sa_family; 438 439 if (bcmp(&s, &ep->src, ep->src.ss_len) == 0 && 440 bcmp(&d, &ep->dst, ep->dst.ss_len) == 0) { 441 return matchlen; 442 } else 443 return 0; 444 } 445 446 static void 447 encap_fillarg(struct mbuf *m, void *arg) 448 { 449 struct m_tag *tag; 450 451 if (arg != NULL) { 452 tag = m_tag_get(PACKET_TAG_ENCAP, sizeof(void *), M_NOWAIT); 453 if (tag != NULL) { 454 *(void**)(tag+1) = arg; 455 m_tag_prepend(m, tag); 456 } 457 } 458 } 459 460 void * 461 encap_getarg(struct mbuf *m) 462 { 463 void *p = NULL; 464 struct m_tag *tag; 465 466 tag = m_tag_find(m, PACKET_TAG_ENCAP, NULL); 467 if (tag) { 468 p = *(void**)(tag+1); 469 m_tag_delete(m, tag); 470 } 471 return p; 472 } 473