xform_ipip.c revision 111119
1/* $FreeBSD: head/sys/netipsec/xform_ipip.c 111119 2003-02-19 05:47:46Z imp $ */ 2/* $OpenBSD: ip_ipip.c,v 1.25 2002/06/10 18:04:55 itojun Exp $ */ 3/* 4 * The authors of this code are John Ioannidis (ji@tla.org), 5 * Angelos D. Keromytis (kermit@csd.uch.gr) and 6 * Niels Provos (provos@physnet.uni-hamburg.de). 7 * 8 * The original version of this code was written by John Ioannidis 9 * for BSD/OS in Athens, Greece, in November 1995. 10 * 11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996, 12 * by Angelos D. Keromytis. 13 * 14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis 15 * and Niels Provos. 16 * 17 * Additional features in 1999 by Angelos D. Keromytis. 18 * 19 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis, 20 * Angelos D. Keromytis and Niels Provos. 21 * Copyright (c) 2001, Angelos D. Keromytis. 22 * 23 * Permission to use, copy, and modify this software with or without fee 24 * is hereby granted, provided that this entire notice is included in 25 * all copies of any software which is or includes a copy or 26 * modification of this software. 27 * You may use this code under the GNU public license if you so wish. Please 28 * contribute changes back to the authors under this freer than GPL license 29 * so that we may further the use of strong encryption without limitations to 30 * all. 31 * 32 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR 33 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY 34 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE 35 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR 36 * PURPOSE. 37 */ 38 39/* 40 * IP-inside-IP processing 41 */ 42#include "opt_inet.h" 43#include "opt_inet6.h" 44#include "opt_random_ip_id.h" 45 46#include <sys/param.h> 47#include <sys/systm.h> 48#include <sys/mbuf.h> 49#include <sys/socket.h> 50#include <sys/kernel.h> 51#include <sys/protosw.h> 52#include <sys/sysctl.h> 53 54#include <net/if.h> 55#include <net/route.h> 56#include <net/netisr.h> 57 58#include <netinet/in.h> 59#include <netinet/in_systm.h> 60#include <netinet/in_var.h> 61#include <netinet/ip.h> 62#include <netinet/ip_ecn.h> 63#include <netinet/ip_var.h> 64#include <netinet/ip_encap.h> 65#include <netinet/ipprotosw.h> 66 67#include <netipsec/ipsec.h> 68#include <netipsec/xform.h> 69 70#include <netipsec/ipip_var.h> 71 72#ifdef MROUTING 73#include <netinet/ip_mroute.h> 74#endif 75 76#ifdef INET6 77#include <netinet/ip6.h> 78#include <netipsec/ipsec6.h> 79#include <netinet6/ip6_ecn.h> 80#include <netinet6/in6_var.h> 81#include <netinet6/ip6protosw.h> 82#endif 83 84#include <netipsec/key.h> 85#include <netipsec/key_debug.h> 86 87#include <machine/stdarg.h> 88 89/* 90 * We can control the acceptance of IP4 packets by altering the sysctl 91 * net.inet.ipip.allow value. Zero means drop them, all else is acceptance. 92 */ 93int ipip_allow = 0; 94struct ipipstat ipipstat; 95 96SYSCTL_DECL(_net_inet_ipip); 97SYSCTL_INT(_net_inet_ipip, OID_AUTO, 98 ipip_allow, CTLFLAG_RW, &ipip_allow, 0, ""); 99SYSCTL_STRUCT(_net_inet_ipip, IPSECCTL_STATS, 100 stats, CTLFLAG_RD, &ipipstat, ipipstat, ""); 101 102/* XXX IPCOMP */ 103#define M_IPSEC (M_AUTHIPHDR|M_AUTHIPDGM|M_DECRYPTED) 104 105static void _ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp); 106 107#ifdef INET6 108/* 109 * Really only a wrapper for ipip_input(), for use with IPv6. 110 */ 111int 112ip4_input6(struct mbuf **m, int *offp, int proto) 113{ 114#if 0 115 /* If we do not accept IP-in-IP explicitly, drop. */ 116 if (!ipip_allow && ((*m)->m_flags & M_IPSEC) == 0) { 117 DPRINTF(("ip4_input6: dropped due to policy\n")); 118 ipipstat.ipips_pdrops++; 119 m_freem(*m); 120 return IPPROTO_DONE; 121 } 122#endif 123 _ipip_input(*m, *offp, NULL); 124 return IPPROTO_DONE; 125} 126#endif /* INET6 */ 127 128#ifdef INET 129/* 130 * Really only a wrapper for ipip_input(), for use with IPv4. 131 */ 132void 133ip4_input(struct mbuf *m, ...) 134{ 135 va_list ap; 136 int iphlen; 137 138#if 0 139 /* If we do not accept IP-in-IP explicitly, drop. */ 140 if (!ipip_allow && (m->m_flags & M_IPSEC) == 0) { 141 DPRINTF(("ip4_input: dropped due to policy\n")); 142 ipipstat.ipips_pdrops++; 143 m_freem(m); 144 return; 145 } 146#endif 147 va_start(ap, m); 148 iphlen = va_arg(ap, int); 149 va_end(ap); 150 151 _ipip_input(m, iphlen, NULL); 152} 153#endif /* INET */ 154 155/* 156 * ipip_input gets called when we receive an IP{46} encapsulated packet, 157 * either because we got it at a real interface, or because AH or ESP 158 * were being used in tunnel mode (in which case the rcvif element will 159 * contain the address of the encX interface associated with the tunnel. 160 */ 161 162static void 163_ipip_input(struct mbuf *m, int iphlen, struct ifnet *gifp) 164{ 165 register struct sockaddr_in *sin; 166 register struct ifnet *ifp; 167 register struct ifaddr *ifa; 168 struct ifqueue *ifq = NULL; 169 struct ip *ipo; 170#ifdef INET6 171 register struct sockaddr_in6 *sin6; 172 struct ip6_hdr *ip6 = NULL; 173 u_int8_t itos; 174#endif 175 u_int8_t nxt; 176 int isr; 177 u_int8_t otos; 178 u_int8_t v; 179 int hlen; 180 181 ipipstat.ipips_ipackets++; 182 183 m_copydata(m, 0, 1, &v); 184 185 switch (v >> 4) { 186#ifdef INET 187 case 4: 188 hlen = sizeof(struct ip); 189 break; 190#endif /* INET */ 191#ifdef INET6 192 case 6: 193 hlen = sizeof(struct ip6_hdr); 194 break; 195#endif 196 default: 197 ipipstat.ipips_family++; 198 m_freem(m); 199 return /* EAFNOSUPPORT */; 200 } 201 202 /* Bring the IP header in the first mbuf, if not there already */ 203 if (m->m_len < hlen) { 204 if ((m = m_pullup(m, hlen)) == NULL) { 205 DPRINTF(("ipip_input: m_pullup (1) failed\n")); 206 ipipstat.ipips_hdrops++; 207 return; 208 } 209 } 210 211 ipo = mtod(m, struct ip *); 212 213#ifdef MROUTING 214 if (ipo->ip_v == IPVERSION && ipo->ip_p == IPPROTO_IPV4) { 215 if (IN_MULTICAST(((struct ip *)((char *) ipo + iphlen))->ip_dst.s_addr)) { 216 ipip_mroute_input (m, iphlen); 217 return; 218 } 219 } 220#endif /* MROUTING */ 221 222 /* Keep outer ecn field. */ 223 switch (v >> 4) { 224#ifdef INET 225 case 4: 226 otos = ipo->ip_tos; 227 break; 228#endif /* INET */ 229#ifdef INET6 230 case 6: 231 otos = (ntohl(mtod(m, struct ip6_hdr *)->ip6_flow) >> 20) & 0xff; 232 break; 233#endif 234 default: 235 panic("ipip_input: unknown ip version %u (outer)", v>>4); 236 } 237 238 /* Remove outer IP header */ 239 m_adj(m, iphlen); 240 241 /* Sanity check */ 242 if (m->m_pkthdr.len < sizeof(struct ip)) { 243 ipipstat.ipips_hdrops++; 244 m_freem(m); 245 return; 246 } 247 248 m_copydata(m, 0, 1, &v); 249 250 switch (v >> 4) { 251#ifdef INET 252 case 4: 253 hlen = sizeof(struct ip); 254 break; 255#endif /* INET */ 256 257#ifdef INET6 258 case 6: 259 hlen = sizeof(struct ip6_hdr); 260 break; 261#endif 262 default: 263 ipipstat.ipips_family++; 264 m_freem(m); 265 return; /* EAFNOSUPPORT */ 266 } 267 268 /* 269 * Bring the inner IP header in the first mbuf, if not there already. 270 */ 271 if (m->m_len < hlen) { 272 if ((m = m_pullup(m, hlen)) == NULL) { 273 DPRINTF(("ipip_input: m_pullup (2) failed\n")); 274 ipipstat.ipips_hdrops++; 275 return; 276 } 277 } 278 279 /* 280 * RFC 1853 specifies that the inner TTL should not be touched on 281 * decapsulation. There's no reason this comment should be here, but 282 * this is as good as any a position. 283 */ 284 285 /* Some sanity checks in the inner IP header */ 286 switch (v >> 4) { 287#ifdef INET 288 case 4: 289 ipo = mtod(m, struct ip *); 290 nxt = ipo->ip_p; 291 ip_ecn_egress(ip4_ipsec_ecn, &otos, &ipo->ip_tos); 292 break; 293#endif /* INET */ 294#ifdef INET6 295 case 6: 296 ip6 = (struct ip6_hdr *) ipo; 297 nxt = ip6->ip6_nxt; 298 itos = (ntohl(ip6->ip6_flow) >> 20) & 0xff; 299 ip_ecn_egress(ip6_ipsec_ecn, &otos, &itos); 300 ip6->ip6_flow &= ~htonl(0xff << 20); 301 ip6->ip6_flow |= htonl((u_int32_t) itos << 20); 302 break; 303#endif 304 default: 305 panic("ipip_input: unknown ip version %u (inner)", v>>4); 306 } 307 308 /* Check for local address spoofing. */ 309 if ((m->m_pkthdr.rcvif == NULL || 310 !(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK)) && 311 ipip_allow != 2) { 312 IFNET_RLOCK(); 313 for (ifp = ifnet.tqh_first; ifp != 0; 314 ifp = ifp->if_list.tqe_next) { 315 for (ifa = ifp->if_addrlist.tqh_first; ifa != 0; 316 ifa = ifa->ifa_list.tqe_next) { 317#ifdef INET 318 if (ipo) { 319 if (ifa->ifa_addr->sa_family != 320 AF_INET) 321 continue; 322 323 sin = (struct sockaddr_in *) ifa->ifa_addr; 324 325 if (sin->sin_addr.s_addr == 326 ipo->ip_src.s_addr) { 327 ipipstat.ipips_spoof++; 328 m_freem(m); 329 IFNET_RUNLOCK(); 330 return; 331 } 332 } 333#endif /* INET */ 334 335#ifdef INET6 336 if (ip6) { 337 if (ifa->ifa_addr->sa_family != 338 AF_INET6) 339 continue; 340 341 sin6 = (struct sockaddr_in6 *) ifa->ifa_addr; 342 343 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_src)) { 344 ipipstat.ipips_spoof++; 345 m_freem(m); 346 IFNET_RUNLOCK(); 347 return; 348 } 349 350 } 351#endif /* INET6 */ 352 } 353 } 354 IFNET_RUNLOCK(); 355 } 356 357 /* Statistics */ 358 ipipstat.ipips_ibytes += m->m_pkthdr.len - iphlen; 359 360 /* 361 * Interface pointer stays the same; if no IPsec processing has 362 * been done (or will be done), this will point to a normal 363 * interface. Otherwise, it'll point to an enc interface, which 364 * will allow a packet filter to distinguish between secure and 365 * untrusted packets. 366 */ 367 368 switch (v >> 4) { 369#ifdef INET 370 case 4: 371 ifq = &ipintrq; 372 isr = NETISR_IP; 373 break; 374#endif 375#ifdef INET6 376 case 6: 377 ifq = &ip6intrq; 378 isr = NETISR_IPV6; 379 break; 380#endif 381 default: 382 panic("ipip_input: should never reach here"); 383 } 384 385 if (!IF_HANDOFF(ifq, m, NULL)) { 386 ipipstat.ipips_qfull++; 387 388 DPRINTF(("ipip_input: packet dropped because of full queue\n")); 389 } else { 390 schednetisr(isr); 391 } 392} 393 394int 395ipip_output( 396 struct mbuf *m, 397 struct ipsecrequest *isr, 398 struct mbuf **mp, 399 int skip, 400 int protoff 401) 402{ 403 struct secasvar *sav; 404 u_int8_t tp, otos; 405 struct secasindex *saidx; 406 int error; 407#ifdef INET 408 u_int8_t itos; 409 struct ip *ipo; 410#endif /* INET */ 411#ifdef INET6 412 struct ip6_hdr *ip6, *ip6o; 413#endif /* INET6 */ 414 415#if 0 416 SPLASSERT(net, "ipip_output"); 417#endif 418 419 sav = isr->sav; 420 KASSERT(sav != NULL, ("ipip_output: null SA")); 421 KASSERT(sav->sah != NULL, ("ipip_output: null SAH")); 422 423 /* XXX Deal with empty TDB source/destination addresses. */ 424 425 m_copydata(m, 0, 1, &tp); 426 tp = (tp >> 4) & 0xff; /* Get the IP version number. */ 427 428 saidx = &sav->sah->saidx; 429 switch (saidx->dst.sa.sa_family) { 430#ifdef INET 431 case AF_INET: 432 if (saidx->src.sa.sa_family != AF_INET || 433 saidx->src.sin.sin_addr.s_addr == INADDR_ANY || 434 saidx->dst.sin.sin_addr.s_addr == INADDR_ANY) { 435 DPRINTF(("ipip_output: unspecified tunnel endpoint " 436 "address in SA %s/%08lx\n", 437 ipsec_address(&saidx->dst), 438 (u_long) ntohl(sav->spi))); 439 ipipstat.ipips_unspec++; 440 error = EINVAL; 441 goto bad; 442 } 443 444 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 445 if (m == 0) { 446 DPRINTF(("ipip_output: M_PREPEND failed\n")); 447 ipipstat.ipips_hdrops++; 448 error = ENOBUFS; 449 goto bad; 450 } 451 452 ipo = mtod(m, struct ip *); 453 454 ipo->ip_v = IPVERSION; 455 ipo->ip_hl = 5; 456 ipo->ip_len = htons(m->m_pkthdr.len); 457 ipo->ip_ttl = ip_defttl; 458 ipo->ip_sum = 0; 459 ipo->ip_src = saidx->src.sin.sin_addr; 460 ipo->ip_dst = saidx->dst.sin.sin_addr; 461 462#ifdef RANDOM_IP_ID 463 ipo->ip_id = ip_randomid(); 464#else 465 ipo->ip_id = htons(ip_id++); 466#endif 467 468 /* If the inner protocol is IP... */ 469 if (tp == IPVERSION) { 470 /* Save ECN notification */ 471 m_copydata(m, sizeof(struct ip) + 472 offsetof(struct ip, ip_tos), 473 sizeof(u_int8_t), (caddr_t) &itos); 474 475 ipo->ip_p = IPPROTO_IPIP; 476 477 /* 478 * We should be keeping tunnel soft-state and 479 * send back ICMPs if needed. 480 */ 481 m_copydata(m, sizeof(struct ip) + 482 offsetof(struct ip, ip_off), 483 sizeof(u_int16_t), (caddr_t) &ipo->ip_off); 484 ipo->ip_off = ntohs(ipo->ip_off); 485 ipo->ip_off &= ~(IP_DF | IP_MF | IP_OFFMASK); 486 ipo->ip_off = htons(ipo->ip_off); 487 } 488#ifdef INET6 489 else if (tp == (IPV6_VERSION >> 4)) { 490 u_int32_t itos32; 491 492 /* Save ECN notification. */ 493 m_copydata(m, sizeof(struct ip) + 494 offsetof(struct ip6_hdr, ip6_flow), 495 sizeof(u_int32_t), (caddr_t) &itos32); 496 itos = ntohl(itos32) >> 20; 497 ipo->ip_p = IPPROTO_IPV6; 498 ipo->ip_off = 0; 499 } 500#endif /* INET6 */ 501 else { 502 goto nofamily; 503 } 504 505 otos = 0; 506 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos); 507 ipo->ip_tos = otos; 508 break; 509#endif /* INET */ 510 511#ifdef INET6 512 case AF_INET6: 513 if (IN6_IS_ADDR_UNSPECIFIED(&saidx->dst.sin6.sin6_addr) || 514 saidx->src.sa.sa_family != AF_INET6 || 515 IN6_IS_ADDR_UNSPECIFIED(&saidx->src.sin6.sin6_addr)) { 516 DPRINTF(("ipip_output: unspecified tunnel endpoint " 517 "address in SA %s/%08lx\n", 518 ipsec_address(&saidx->dst), 519 (u_long) ntohl(sav->spi))); 520 ipipstat.ipips_unspec++; 521 error = ENOBUFS; 522 goto bad; 523 } 524 525 /* scoped address handling */ 526 ip6 = mtod(m, struct ip6_hdr *); 527 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) 528 ip6->ip6_src.s6_addr16[1] = 0; 529 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) 530 ip6->ip6_dst.s6_addr16[1] = 0; 531 532 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT); 533 if (m == 0) { 534 DPRINTF(("ipip_output: M_PREPEND failed\n")); 535 ipipstat.ipips_hdrops++; 536 *mp = NULL; 537 error = ENOBUFS; 538 goto bad; 539 } 540 541 /* Initialize IPv6 header */ 542 ip6o = mtod(m, struct ip6_hdr *); 543 ip6o->ip6_flow = 0; 544 ip6o->ip6_vfc &= ~IPV6_VERSION_MASK; 545 ip6o->ip6_vfc |= IPV6_VERSION; 546 ip6o->ip6_plen = htons(m->m_pkthdr.len); 547 ip6o->ip6_hlim = ip_defttl; 548 ip6o->ip6_dst = saidx->dst.sin6.sin6_addr; 549 ip6o->ip6_src = saidx->src.sin6.sin6_addr; 550 551#ifdef INET 552 if (tp == IPVERSION) { 553 /* Save ECN notification */ 554 m_copydata(m, sizeof(struct ip6_hdr) + 555 offsetof(struct ip, ip_tos), sizeof(u_int8_t), 556 (caddr_t) &itos); 557 558 /* This is really IPVERSION. */ 559 ip6o->ip6_nxt = IPPROTO_IPIP; 560 } else 561#endif /* INET */ 562 if (tp == (IPV6_VERSION >> 4)) { 563 u_int32_t itos32; 564 565 /* Save ECN notification. */ 566 m_copydata(m, sizeof(struct ip6_hdr) + 567 offsetof(struct ip6_hdr, ip6_flow), 568 sizeof(u_int32_t), (caddr_t) &itos32); 569 itos = ntohl(itos32) >> 20; 570 571 ip6o->ip6_nxt = IPPROTO_IPV6; 572 } else { 573 goto nofamily; 574 } 575 576 otos = 0; 577 ip_ecn_ingress(ECN_ALLOWED, &otos, &itos); 578 ip6o->ip6_flow |= htonl((u_int32_t) otos << 20); 579 break; 580#endif /* INET6 */ 581 582 default: 583nofamily: 584 DPRINTF(("ipip_output: unsupported protocol family %u\n", 585 saidx->dst.sa.sa_family)); 586 ipipstat.ipips_family++; 587 error = EAFNOSUPPORT; /* XXX diffs from openbsd */ 588 goto bad; 589 } 590 591 ipipstat.ipips_opackets++; 592 *mp = m; 593 594#ifdef INET 595 if (saidx->dst.sa.sa_family == AF_INET) { 596#if 0 597 if (sav->tdb_xform->xf_type == XF_IP4) 598 tdb->tdb_cur_bytes += 599 m->m_pkthdr.len - sizeof(struct ip); 600#endif 601 ipipstat.ipips_obytes += m->m_pkthdr.len - sizeof(struct ip); 602 } 603#endif /* INET */ 604 605#ifdef INET6 606 if (saidx->dst.sa.sa_family == AF_INET6) { 607#if 0 608 if (sav->tdb_xform->xf_type == XF_IP4) 609 tdb->tdb_cur_bytes += 610 m->m_pkthdr.len - sizeof(struct ip6_hdr); 611#endif 612 ipipstat.ipips_obytes += 613 m->m_pkthdr.len - sizeof(struct ip6_hdr); 614 } 615#endif /* INET6 */ 616 617 return 0; 618bad: 619 if (m) 620 m_freem(m), *mp = NULL; 621 return (error); 622} 623 624#ifdef FAST_IPSEC 625static int 626ipe4_init(struct secasvar *sav, struct xformsw *xsp) 627{ 628 sav->tdb_xform = xsp; 629 return 0; 630} 631 632static int 633ipe4_zeroize(struct secasvar *sav) 634{ 635 sav->tdb_xform = NULL; 636 return 0; 637} 638 639static int 640ipe4_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff) 641{ 642 /* This is a rather serious mistake, so no conditional printing. */ 643 printf("ipe4_input: should never be called\n"); 644 if (m) 645 m_freem(m); 646 return EOPNOTSUPP; 647} 648 649static struct xformsw ipe4_xformsw = { 650 XF_IP4, 0, "IPv4 Simple Encapsulation", 651 ipe4_init, ipe4_zeroize, ipe4_input, ipip_output, 652}; 653 654extern struct domain inetdomain; 655static struct ipprotosw ipe4_protosw[] = { 656{ SOCK_RAW, &inetdomain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR|PR_LASTHDR, 657 (pr_in_input_t*) ip4_input, 658 0, 0, rip_ctloutput, 659 0, 660 0, 0, 0, 0, 661 &rip_usrreqs 662}, 663#ifdef INET6 664{ SOCK_RAW, &inetdomain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR|PR_LASTHDR, 665 (pr_in_input_t*) ip4_input, 666 0, 0, rip_ctloutput, 667 0, 668 0, 0, 0, 0, 669 &rip_usrreqs 670} 671#endif 672}; 673 674/* 675 * Check the encapsulated packet to see if we want it 676 */ 677static int 678ipe4_encapcheck(const struct mbuf *m, int off, int proto, void *arg) 679{ 680 /* 681 * Only take packets coming from IPSEC tunnels; the rest 682 * must be handled by the gif tunnel code. Note that we 683 * also return a minimum priority when we want the packet 684 * so any explicit gif tunnels take precedence. 685 */ 686 return ((m->m_flags & M_IPSEC) != 0 ? 1 : 0); 687} 688 689static void 690ipe4_attach(void) 691{ 692 xform_register(&ipe4_xformsw); 693 /* attach to encapsulation framework */ 694 /* XXX save return cookie for detach on module remove */ 695 (void) encap_attach_func(AF_INET, -1, 696 ipe4_encapcheck, (struct protosw*) &ipe4_protosw[0], NULL); 697#ifdef INET6 698 (void) encap_attach_func(AF_INET6, -1, 699 ipe4_encapcheck, (struct protosw*) &ipe4_protosw[1], NULL); 700#endif 701} 702SYSINIT(ipe4_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipe4_attach, NULL); 703#endif /* FAST_IPSEC */ 704