1 /* $NetBSD: ipsec.c,v 1.169 2019/07/09 16:56:24 maxv Exp $ */ 2 /* $FreeBSD: ipsec.c,v 1.2.2.2 2003/07/01 01:38:13 sam Exp $ */ 3 /* $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */ 4 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 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: ipsec.c,v 1.169 2019/07/09 16:56:24 maxv Exp $"); 36 37 /* 38 * IPsec controller part. 39 */ 40 41 #if defined(_KERNEL_OPT) 42 #include "opt_inet.h" 43 #include "opt_ipsec.h" 44 #endif 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/mbuf.h> 49 #include <sys/domain.h> 50 #include <sys/protosw.h> 51 #include <sys/socket.h> 52 #include <sys/socketvar.h> 53 #include <sys/errno.h> 54 #include <sys/time.h> 55 #include <sys/kernel.h> 56 #include <sys/syslog.h> 57 #include <sys/sysctl.h> 58 #include <sys/proc.h> 59 #include <sys/kauth.h> 60 #include <sys/cpu.h> 61 #include <sys/kmem.h> 62 #include <sys/pserialize.h> 63 64 #include <net/if.h> 65 #include <net/route.h> 66 67 #include <netinet/in.h> 68 #include <netinet/in_systm.h> 69 #include <netinet/ip.h> 70 #include <netinet/ip_var.h> 71 #include <netinet/in_var.h> 72 #include <netinet/udp.h> 73 #include <netinet/udp_var.h> 74 #include <netinet/tcp.h> 75 #include <netinet/udp.h> 76 #include <netinet/ip_icmp.h> 77 #include <netinet/ip_private.h> 78 79 #include <netinet/ip6.h> 80 #ifdef INET6 81 #include <netinet6/ip6_var.h> 82 #endif 83 #include <netinet/in_pcb.h> 84 #include <netinet/in_offload.h> 85 #ifdef INET6 86 #include <netinet6/in6_pcb.h> 87 #include <netinet/icmp6.h> 88 #endif 89 90 #include <netipsec/ipsec.h> 91 #include <netipsec/ipsec_var.h> 92 #include <netipsec/ipsec_private.h> 93 #ifdef INET6 94 #include <netipsec/ipsec6.h> 95 #endif 96 #include <netipsec/ah_var.h> 97 #include <netipsec/esp_var.h> 98 #include <netipsec/ipcomp.h> /*XXX*/ 99 #include <netipsec/ipcomp_var.h> 100 101 #include <netipsec/key.h> 102 #include <netipsec/keydb.h> 103 #include <netipsec/key_debug.h> 104 105 #include <netipsec/xform.h> 106 107 int ipsec_used = 0; 108 int ipsec_enabled = 1; 109 110 #ifdef IPSEC_DEBUG 111 int ipsec_debug = 1; 112 113 /* 114 * When set to 1, IPsec will send packets with the same sequence number. 115 * This allows to verify if the other side has proper replay attacks detection. 116 */ 117 int ipsec_replay = 0; 118 119 /* 120 * When set 1, IPsec will send packets with corrupted HMAC. 121 * This allows to verify if the other side properly detects modified packets. 122 */ 123 int ipsec_integrity = 0; 124 #else 125 int ipsec_debug = 0; 126 #endif 127 128 percpu_t *ipsecstat_percpu; 129 130 int ip4_ah_offsetmask = 0; /* maybe IP_DF? */ 131 int ip4_ipsec_dfbit = 2; /* DF bit on encap. 0: clear 1: set 2: copy */ 132 int ip4_esp_trans_deflev = IPSEC_LEVEL_USE; 133 int ip4_esp_net_deflev = IPSEC_LEVEL_USE; 134 int ip4_ah_trans_deflev = IPSEC_LEVEL_USE; 135 int ip4_ah_net_deflev = IPSEC_LEVEL_USE; 136 struct secpolicy ip4_def_policy; 137 int ip4_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */ 138 139 u_int ipsec_spdgen = 1; /* SPD generation # */ 140 141 static struct secpolicy ipsec_dummy_sp __read_mostly = { 142 .state = IPSEC_SPSTATE_ALIVE, 143 /* If ENTRUST, the dummy SP never be used. See ipsec_getpolicybysock. */ 144 .policy = IPSEC_POLICY_ENTRUST, 145 }; 146 147 static struct secpolicy *ipsec_checkpcbcache(struct mbuf *, 148 struct inpcbpolicy *, int); 149 static int ipsec_fillpcbcache(struct inpcbpolicy *, struct mbuf *, 150 struct secpolicy *, int); 151 static int ipsec_invalpcbcache(struct inpcbpolicy *, int); 152 153 /* 154 * Crypto support requirements: 155 * 156 * 1 require hardware support 157 * -1 require software support 158 * 0 take anything 159 */ 160 int crypto_support = 0; 161 162 static struct secpolicy *ipsec_getpolicybysock(struct mbuf *, u_int, 163 struct inpcb_hdr *, int *); 164 165 #ifdef INET6 166 int ip6_esp_trans_deflev = IPSEC_LEVEL_USE; 167 int ip6_esp_net_deflev = IPSEC_LEVEL_USE; 168 int ip6_ah_trans_deflev = IPSEC_LEVEL_USE; 169 int ip6_ah_net_deflev = IPSEC_LEVEL_USE; 170 struct secpolicy ip6_def_policy; 171 int ip6_ipsec_ecn = 0; /* ECN ignore(-1)/forbidden(0)/allowed(1) */ 172 #endif 173 174 static int ipsec_setspidx_inpcb(struct mbuf *, void *); 175 static int ipsec_setspidx(struct mbuf *, struct secpolicyindex *, int, int); 176 static void ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *, int); 177 static int ipsec4_setspidx_ipaddr(struct mbuf *, struct secpolicyindex *); 178 #ifdef INET6 179 static void ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *, int); 180 static int ipsec6_setspidx_ipaddr(struct mbuf *, struct secpolicyindex *); 181 #endif 182 static void ipsec_delpcbpolicy(struct inpcbpolicy *); 183 static void ipsec_destroy_policy(struct secpolicy *); 184 static int ipsec_sp_reject(const struct secpolicy *, const struct mbuf *); 185 static void vshiftl(unsigned char *, int, int); 186 static size_t ipsec_sp_hdrsiz(const struct secpolicy *, const struct mbuf *); 187 188 /* 189 * Try to validate and use cached policy on a PCB. 190 */ 191 static struct secpolicy * 192 ipsec_checkpcbcache(struct mbuf *m, struct inpcbpolicy *pcbsp, int dir) 193 { 194 struct secpolicyindex spidx; 195 struct secpolicy *sp = NULL; 196 int s; 197 198 KASSERT(IPSEC_DIR_IS_VALID(dir)); 199 KASSERT(pcbsp != NULL); 200 KASSERT(dir < __arraycount(pcbsp->sp_cache)); 201 KASSERT(inph_locked(pcbsp->sp_inph)); 202 203 /* 204 * Checking the generation and sp->state and taking a reference to an SP 205 * must be in a critical section of pserialize. See key_unlink_sp. 206 */ 207 s = pserialize_read_enter(); 208 /* SPD table change invalidate all the caches. */ 209 if (ipsec_spdgen != pcbsp->sp_cache[dir].cachegen) { 210 ipsec_invalpcbcache(pcbsp, dir); 211 goto out; 212 } 213 sp = pcbsp->sp_cache[dir].cachesp; 214 if (sp == NULL) 215 goto out; 216 if (sp->state != IPSEC_SPSTATE_ALIVE) { 217 sp = NULL; 218 ipsec_invalpcbcache(pcbsp, dir); 219 goto out; 220 } 221 if ((pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) == 0) { 222 /* NB: assume ipsec_setspidx never sleep */ 223 if (ipsec_setspidx(m, &spidx, dir, 1) != 0) { 224 sp = NULL; 225 goto out; 226 } 227 228 /* 229 * We have to make an exact match here since the cached rule 230 * might have lower priority than a rule that would otherwise 231 * have matched the packet. 232 */ 233 if (memcmp(&pcbsp->sp_cache[dir].cacheidx, &spidx, 234 sizeof(spidx))) { 235 sp = NULL; 236 goto out; 237 } 238 } else { 239 /* 240 * The pcb is connected, and the L4 code is sure that: 241 * - outgoing side uses inp_[lf]addr 242 * - incoming side looks up policy after inpcb lookup 243 * and address pair is know to be stable. We do not need 244 * to generate spidx again, nor check the address match again. 245 * 246 * For IPv4/v6 SOCK_STREAM sockets, this assumptions holds 247 * and there are calls to ipsec_pcbconn() from in_pcbconnect(). 248 */ 249 } 250 251 sp->lastused = time_second; 252 KEY_SP_REF(sp); 253 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 254 "DP cause refcnt++:%d SP:%p\n", 255 key_sp_refcnt(sp), pcbsp->sp_cache[dir].cachesp); 256 out: 257 pserialize_read_exit(s); 258 return sp; 259 } 260 261 static int 262 ipsec_fillpcbcache(struct inpcbpolicy *pcbsp, struct mbuf *m, 263 struct secpolicy *sp, int dir) 264 { 265 266 KASSERT(IPSEC_DIR_IS_INOROUT(dir)); 267 KASSERT(dir < __arraycount(pcbsp->sp_cache)); 268 KASSERT(inph_locked(pcbsp->sp_inph)); 269 270 pcbsp->sp_cache[dir].cachesp = NULL; 271 pcbsp->sp_cache[dir].cachehint = IPSEC_PCBHINT_UNKNOWN; 272 if (ipsec_setspidx(m, &pcbsp->sp_cache[dir].cacheidx, dir, 1) != 0) { 273 return EINVAL; 274 } 275 pcbsp->sp_cache[dir].cachesp = sp; 276 if (pcbsp->sp_cache[dir].cachesp) { 277 /* 278 * If the PCB is connected, we can remember a hint to 279 * possibly short-circuit IPsec processing in other places. 280 */ 281 if (pcbsp->sp_cacheflags & IPSEC_PCBSP_CONNECTED) { 282 switch (pcbsp->sp_cache[dir].cachesp->policy) { 283 case IPSEC_POLICY_NONE: 284 case IPSEC_POLICY_BYPASS: 285 pcbsp->sp_cache[dir].cachehint = 286 IPSEC_PCBHINT_NO; 287 break; 288 default: 289 pcbsp->sp_cache[dir].cachehint = 290 IPSEC_PCBHINT_YES; 291 } 292 } 293 } 294 pcbsp->sp_cache[dir].cachegen = ipsec_spdgen; 295 296 return 0; 297 } 298 299 static int 300 ipsec_invalpcbcache(struct inpcbpolicy *pcbsp, int dir) 301 { 302 int i; 303 304 KASSERT(inph_locked(pcbsp->sp_inph)); 305 306 for (i = IPSEC_DIR_INBOUND; i <= IPSEC_DIR_OUTBOUND; i++) { 307 if (dir != IPSEC_DIR_ANY && i != dir) 308 continue; 309 pcbsp->sp_cache[i].cachesp = NULL; 310 pcbsp->sp_cache[i].cachehint = IPSEC_PCBHINT_UNKNOWN; 311 pcbsp->sp_cache[i].cachegen = 0; 312 memset(&pcbsp->sp_cache[i].cacheidx, 0, 313 sizeof(pcbsp->sp_cache[i].cacheidx)); 314 } 315 return 0; 316 } 317 318 void 319 ipsec_pcbconn(struct inpcbpolicy *pcbsp) 320 { 321 322 KASSERT(inph_locked(pcbsp->sp_inph)); 323 324 pcbsp->sp_cacheflags |= IPSEC_PCBSP_CONNECTED; 325 ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY); 326 } 327 328 void 329 ipsec_pcbdisconn(struct inpcbpolicy *pcbsp) 330 { 331 332 KASSERT(inph_locked(pcbsp->sp_inph)); 333 334 pcbsp->sp_cacheflags &= ~IPSEC_PCBSP_CONNECTED; 335 ipsec_invalpcbcache(pcbsp, IPSEC_DIR_ANY); 336 } 337 338 void 339 ipsec_invalpcbcacheall(void) 340 { 341 342 if (ipsec_spdgen == UINT_MAX) 343 ipsec_spdgen = 1; 344 else 345 ipsec_spdgen++; 346 } 347 348 /* 349 * Return a held reference to the default SP. 350 */ 351 static struct secpolicy * 352 key_get_default_sp(int af, const char *where, int tag) 353 { 354 struct secpolicy *sp; 355 356 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP from %s:%u\n", where, tag); 357 358 switch(af) { 359 case AF_INET: 360 sp = &ip4_def_policy; 361 break; 362 #ifdef INET6 363 case AF_INET6: 364 sp = &ip6_def_policy; 365 break; 366 #endif 367 default: 368 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 369 "unexpected protocol family %u\n", af); 370 return NULL; 371 } 372 373 if (sp->policy != IPSEC_POLICY_DISCARD && 374 sp->policy != IPSEC_POLICY_NONE) { 375 IPSECLOG(LOG_INFO, "fixed system default policy: %d->%d\n", 376 sp->policy, IPSEC_POLICY_NONE); 377 sp->policy = IPSEC_POLICY_NONE; 378 } 379 KEY_SP_REF(sp); 380 381 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, "DP returns SP:%p (%u)\n", 382 sp, key_sp_refcnt(sp)); 383 return sp; 384 } 385 386 #define KEY_GET_DEFAULT_SP(af) \ 387 key_get_default_sp((af), __func__, __LINE__) 388 389 /* 390 * For OUTBOUND packet having a socket. Searching SPD for packet, 391 * and return a pointer to SP. 392 * OUT: NULL: no appropriate SP found, the following value is set to error. 393 * 0 : bypass 394 * EACCES : discard packet. 395 * ENOENT : ipsec_acquire() in progress, maybe. 396 * others : error occurred. 397 * others: a pointer to SP 398 * 399 * NOTE: IPv6 mapped address concern is implemented here. 400 */ 401 static struct secpolicy * 402 ipsec_getpolicybysock(struct mbuf *m, u_int dir, struct inpcb_hdr *inph, 403 int *error) 404 { 405 struct inpcbpolicy *pcbsp = NULL; 406 struct secpolicy *currsp = NULL; /* policy on socket */ 407 struct secpolicy *sp; 408 int af; 409 410 KASSERT(m != NULL); 411 KASSERT(inph != NULL); 412 KASSERT(error != NULL); 413 KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir); 414 415 KASSERT(inph->inph_socket != NULL); 416 KASSERT(inph_locked(inph)); 417 418 /* XXX FIXME inpcb/in6pcb vs socket*/ 419 af = inph->inph_af; 420 KASSERTMSG(af == AF_INET || af == AF_INET6, 421 "unexpected protocol family %u", af); 422 423 KASSERT(inph->inph_sp != NULL); 424 /* If we have a cached entry, and if it is still valid, use it. */ 425 IPSEC_STATINC(IPSEC_STAT_SPDCACHELOOKUP); 426 currsp = ipsec_checkpcbcache(m, inph->inph_sp, dir); 427 if (currsp) { 428 *error = 0; 429 return currsp; 430 } 431 IPSEC_STATINC(IPSEC_STAT_SPDCACHEMISS); 432 433 switch (af) { 434 case AF_INET: 435 #if defined(INET6) 436 case AF_INET6: 437 #endif 438 *error = ipsec_setspidx_inpcb(m, inph); 439 pcbsp = inph->inph_sp; 440 break; 441 default: 442 *error = EPFNOSUPPORT; 443 break; 444 } 445 if (*error) 446 return NULL; 447 448 KASSERT(pcbsp != NULL); 449 switch (dir) { 450 case IPSEC_DIR_INBOUND: 451 currsp = pcbsp->sp_in; 452 break; 453 case IPSEC_DIR_OUTBOUND: 454 currsp = pcbsp->sp_out; 455 break; 456 } 457 KASSERT(currsp != NULL); 458 459 if (pcbsp->priv) { /* when privileged socket */ 460 switch (currsp->policy) { 461 case IPSEC_POLICY_BYPASS: 462 case IPSEC_POLICY_IPSEC: 463 KEY_SP_REF(currsp); 464 sp = currsp; 465 break; 466 467 case IPSEC_POLICY_ENTRUST: 468 /* look for a policy in SPD */ 469 sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir); 470 if (sp == NULL) /* no SP found */ 471 sp = KEY_GET_DEFAULT_SP(af); 472 break; 473 474 default: 475 IPSECLOG(LOG_ERR, "Invalid policy for PCB %d\n", 476 currsp->policy); 477 *error = EINVAL; 478 return NULL; 479 } 480 } else { /* unpriv, SPD has policy */ 481 sp = KEY_LOOKUP_SP_BYSPIDX(&currsp->spidx, dir); 482 if (sp == NULL) { /* no SP found */ 483 switch (currsp->policy) { 484 case IPSEC_POLICY_BYPASS: 485 IPSECLOG(LOG_ERR, "Illegal policy for " 486 "non-priviliged defined %d\n", 487 currsp->policy); 488 *error = EINVAL; 489 return NULL; 490 491 case IPSEC_POLICY_ENTRUST: 492 sp = KEY_GET_DEFAULT_SP(af); 493 break; 494 495 case IPSEC_POLICY_IPSEC: 496 KEY_SP_REF(currsp); 497 sp = currsp; 498 break; 499 500 default: 501 IPSECLOG(LOG_ERR, "Invalid policy for " 502 "PCB %d\n", currsp->policy); 503 *error = EINVAL; 504 return NULL; 505 } 506 } 507 } 508 KASSERTMSG(sp != NULL, "null SP (priv %u policy %u", pcbsp->priv, 509 currsp->policy); 510 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_STAMP, 511 "DP (priv %u policy %u) allocates SP:%p (refcnt %u)\n", 512 pcbsp->priv, currsp->policy, sp, key_sp_refcnt(sp)); 513 ipsec_fillpcbcache(pcbsp, m, sp, dir); 514 return sp; 515 } 516 517 /* 518 * For FORWARDING packet or OUTBOUND without a socket. Searching SPD for packet, 519 * and return a pointer to SP. 520 * OUT: positive: a pointer to the entry for security policy leaf matched. 521 * NULL: no appropriate SP found, the following value is set to error. 522 * 0 : bypass 523 * EACCES : discard packet. 524 * ENOENT : ipsec_acquire() in progress, maybe. 525 * others : error occurred. 526 */ 527 static struct secpolicy * 528 ipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error) 529 { 530 struct secpolicyindex spidx; 531 struct secpolicy *sp; 532 533 KASSERT(m != NULL); 534 KASSERT(error != NULL); 535 KASSERTMSG(IPSEC_DIR_IS_INOROUT(dir), "invalid direction %u", dir); 536 537 sp = NULL; 538 539 /* Make an index to look for a policy. */ 540 *error = ipsec_setspidx(m, &spidx, dir, (flag & IP_FORWARDING) ? 0 : 1); 541 if (*error != 0) { 542 IPSECLOG(LOG_DEBUG, "setpidx failed, dir %u flag %u\n", dir, flag); 543 memset(&spidx, 0, sizeof(spidx)); 544 return NULL; 545 } 546 547 spidx.dir = dir; 548 549 if (key_havesp(dir)) { 550 sp = KEY_LOOKUP_SP_BYSPIDX(&spidx, dir); 551 } 552 if (sp == NULL) { 553 /* no SP found, use system default */ 554 sp = KEY_GET_DEFAULT_SP(spidx.dst.sa.sa_family); 555 } 556 557 KASSERT(sp != NULL); 558 return sp; 559 } 560 561 static struct secpolicy * 562 ipsec_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error, 563 void *inp) 564 { 565 struct secpolicy *sp; 566 567 *error = 0; 568 569 if (inp == NULL) { 570 sp = ipsec_getpolicybyaddr(m, dir, flag, error); 571 } else { 572 struct inpcb_hdr *inph = (struct inpcb_hdr *)inp; 573 KASSERT(inph->inph_socket != NULL); 574 sp = ipsec_getpolicybysock(m, dir, inph, error); 575 } 576 if (sp == NULL) { 577 KASSERTMSG(*error != 0, "getpolicy failed w/o error"); 578 IPSEC_STATINC(IPSEC_STAT_OUT_INVAL); 579 return NULL; 580 } 581 KASSERTMSG(*error == 0, "sp w/ error set to %u", *error); 582 583 switch (sp->policy) { 584 case IPSEC_POLICY_ENTRUST: 585 default: 586 printf("%s: invalid policy %u\n", __func__, sp->policy); 587 /* fall thru... */ 588 case IPSEC_POLICY_DISCARD: 589 IPSEC_STATINC(IPSEC_STAT_OUT_POLVIO); 590 *error = -EINVAL; /* packet is discarded by caller */ 591 break; 592 case IPSEC_POLICY_BYPASS: 593 case IPSEC_POLICY_NONE: 594 KEY_SP_UNREF(&sp); 595 sp = NULL; /* NB: force NULL result */ 596 break; 597 case IPSEC_POLICY_IPSEC: 598 KASSERT(sp->req != NULL); 599 break; 600 } 601 602 if (*error != 0) { 603 KEY_SP_UNREF(&sp); 604 sp = NULL; 605 IPSECLOG(LOG_DEBUG, "done, error %d\n", *error); 606 } 607 608 return sp; 609 } 610 611 int 612 ipsec4_output(struct mbuf *m, struct inpcb *inp, int flags, 613 u_long *mtu, bool *natt_frag, bool *done) 614 { 615 struct secpolicy *sp = NULL; 616 u_long _mtu = 0; 617 int error, s; 618 619 /* 620 * Check the security policy (SP) for the packet and, if required, 621 * do IPsec-related processing. There are two cases here; the first 622 * time a packet is sent through it will be untagged and handled by 623 * ipsec_checkpolicy(). If the packet is resubmitted to ip_output 624 * (e.g. after AH, ESP, etc. processing), there will be a tag to 625 * bypass the lookup and related policy checking. 626 */ 627 if (ipsec_outdone(m)) { 628 return 0; 629 } 630 s = splsoftnet(); 631 if (inp && ipsec_pcb_skip_ipsec(inp->inp_sp, IPSEC_DIR_OUTBOUND)) { 632 splx(s); 633 return 0; 634 } 635 sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, inp); 636 637 /* 638 * There are four return cases: 639 * sp != NULL apply IPsec policy 640 * sp == NULL, error == 0 no IPsec handling needed 641 * sp == NULL, error == -EINVAL discard packet w/o error 642 * sp == NULL, error != 0 discard packet, report error 643 */ 644 if (sp == NULL) { 645 splx(s); 646 if (error) { 647 /* 648 * Hack: -EINVAL is used to signal that a packet 649 * should be silently discarded. This is typically 650 * because we asked key management for an SA and 651 * it was delayed (e.g. kicked up to IKE). 652 */ 653 if (error == -EINVAL) 654 error = 0; 655 m_freem(m); 656 *done = true; 657 return error; 658 } 659 /* No IPsec processing for this packet. */ 660 return 0; 661 } 662 663 /* 664 * Do delayed checksums now because we send before 665 * this is done in the normal processing path. 666 */ 667 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 668 in_undefer_cksum_tcpudp(m); 669 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 670 } 671 672 error = ipsec4_process_packet(m, sp->req, &_mtu); 673 if (error == 0 && _mtu != 0) { 674 /* 675 * NAT-T ESP fragmentation: do not do IPSec processing 676 * now, we will do it on each fragmented packet. 677 */ 678 *mtu = _mtu; 679 *natt_frag = true; 680 KEY_SP_UNREF(&sp); 681 splx(s); 682 return 0; 683 } 684 685 /* 686 * Preserve KAME behaviour: ENOENT can be returned 687 * when an SA acquire is in progress. Don't propagate 688 * this to user-level; it confuses applications. 689 * 690 * XXX this will go away when the SADB is redone. 691 */ 692 if (error == ENOENT) 693 error = 0; 694 KEY_SP_UNREF(&sp); 695 splx(s); 696 *done = true; 697 return error; 698 } 699 700 int 701 ipsec_ip_input(struct mbuf *m, bool forward) 702 { 703 struct secpolicy *sp; 704 int error, s; 705 706 s = splsoftnet(); 707 error = ipsec_in_reject(m, NULL); 708 splx(s); 709 if (error) { 710 return EINVAL; 711 } 712 713 if (!forward || !(m->m_flags & M_CANFASTFWD)) { 714 return 0; 715 } 716 717 /* 718 * Peek at the outbound SP for this packet to determine if 719 * it is a Fast Forward candidate. 720 */ 721 s = splsoftnet(); 722 sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING, 723 &error, NULL); 724 if (sp != NULL) { 725 m->m_flags &= ~M_CANFASTFWD; 726 KEY_SP_UNREF(&sp); 727 } 728 splx(s); 729 730 return 0; 731 } 732 733 /* 734 * If the packet is routed over IPsec tunnel, tell the originator the 735 * tunnel MTU. 736 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 737 * 738 * XXX: Quick hack!!! 739 * 740 * XXX: And what if the MTU goes negative? 741 */ 742 void 743 ipsec_mtu(struct mbuf *m, int *destmtu) 744 { 745 struct secpolicy *sp; 746 size_t ipsechdr; 747 int error; 748 749 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, IP_FORWARDING, 750 &error); 751 if (sp == NULL) { 752 return; 753 } 754 755 /* Count IPsec header size. */ 756 ipsechdr = ipsec_sp_hdrsiz(sp, m); 757 758 /* 759 * Find the correct route for outer IP header, compute tunnel MTU. 760 */ 761 if (sp->req) { 762 struct secasvar *sav; 763 764 sav = ipsec_lookup_sa(sp->req, m); 765 if (sav != NULL) { 766 struct route *ro; 767 struct rtentry *rt; 768 769 ro = &sav->sah->sa_route; 770 rt = rtcache_validate(ro); 771 if (rt && rt->rt_ifp) { 772 *destmtu = rt->rt_rmx.rmx_mtu ? 773 rt->rt_rmx.rmx_mtu : rt->rt_ifp->if_mtu; 774 *destmtu -= ipsechdr; 775 } 776 rtcache_unref(rt, ro); 777 KEY_SA_UNREF(&sav); 778 } 779 } 780 KEY_SP_UNREF(&sp); 781 } 782 783 static int 784 ipsec_setspidx_inpcb(struct mbuf *m, void *pcb) 785 { 786 struct inpcb_hdr *inph = (struct inpcb_hdr *)pcb; 787 int error; 788 789 KASSERT(inph != NULL); 790 KASSERT(inph->inph_sp != NULL); 791 KASSERT(inph->inph_sp->sp_out != NULL); 792 KASSERT(inph->inph_sp->sp_in != NULL); 793 794 error = ipsec_setspidx(m, &inph->inph_sp->sp_in->spidx, 795 IPSEC_DIR_INBOUND, 1); 796 if (error == 0) { 797 inph->inph_sp->sp_out->spidx = inph->inph_sp->sp_in->spidx; 798 inph->inph_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND; 799 } else { 800 memset(&inph->inph_sp->sp_in->spidx, 0, 801 sizeof(inph->inph_sp->sp_in->spidx)); 802 memset(&inph->inph_sp->sp_out->spidx, 0, 803 sizeof(inph->inph_sp->sp_out->spidx)); 804 } 805 return error; 806 } 807 808 /* 809 * configure security policy index (src/dst/proto/sport/dport) 810 * by looking at the content of mbuf. 811 * the caller is responsible for error recovery (like clearing up spidx). 812 */ 813 static int 814 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int dir, 815 int needport) 816 { 817 struct ip *ip = NULL; 818 struct ip ipbuf; 819 u_int v; 820 int error; 821 822 KASSERT(m != NULL); 823 M_VERIFY_PACKET(m); 824 825 if (m->m_pkthdr.len < sizeof(struct ip)) { 826 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP, 827 "pkthdr.len(%d) < sizeof(struct ip), ignored.\n", 828 m->m_pkthdr.len); 829 return EINVAL; 830 } 831 832 memset(spidx, 0, sizeof(*spidx)); 833 spidx->dir = dir; 834 835 if (m->m_len >= sizeof(*ip)) { 836 ip = mtod(m, struct ip *); 837 } else { 838 m_copydata(m, 0, sizeof(ipbuf), &ipbuf); 839 ip = &ipbuf; 840 } 841 v = ip->ip_v; 842 switch (v) { 843 case 4: 844 error = ipsec4_setspidx_ipaddr(m, spidx); 845 if (error) 846 return error; 847 ipsec4_get_ulp(m, spidx, needport); 848 return 0; 849 #ifdef INET6 850 case 6: 851 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) { 852 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP, 853 "pkthdr.len(%d) < sizeof(struct ip6_hdr), " 854 "ignored.\n", m->m_pkthdr.len); 855 return EINVAL; 856 } 857 error = ipsec6_setspidx_ipaddr(m, spidx); 858 if (error) 859 return error; 860 ipsec6_get_ulp(m, spidx, needport); 861 return 0; 862 #endif 863 default: 864 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP, 865 "unknown IP version %u, ignored.\n", v); 866 return EINVAL; 867 } 868 } 869 870 static void 871 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport) 872 { 873 u_int8_t nxt; 874 int off; 875 876 KASSERT(m != NULL); 877 KASSERTMSG(m->m_pkthdr.len >= sizeof(struct ip), "packet too short"); 878 879 /* NB: ip_input() flips it into host endian XXX need more checking */ 880 if (m->m_len >= sizeof(struct ip)) { 881 struct ip *ip = mtod(m, struct ip *); 882 if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) 883 goto done; 884 off = ip->ip_hl << 2; 885 nxt = ip->ip_p; 886 } else { 887 struct ip ih; 888 889 m_copydata(m, 0, sizeof(struct ip), &ih); 890 if (ih.ip_off & htons(IP_MF | IP_OFFMASK)) 891 goto done; 892 off = ih.ip_hl << 2; 893 nxt = ih.ip_p; 894 } 895 896 while (off < m->m_pkthdr.len) { 897 struct ip6_ext ip6e; 898 struct tcphdr th; 899 struct udphdr uh; 900 struct icmp icmph; 901 902 switch (nxt) { 903 case IPPROTO_TCP: 904 spidx->ul_proto = nxt; 905 if (!needport) 906 goto done_proto; 907 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) 908 goto done; 909 m_copydata(m, off, sizeof(th), &th); 910 spidx->src.sin.sin_port = th.th_sport; 911 spidx->dst.sin.sin_port = th.th_dport; 912 return; 913 case IPPROTO_UDP: 914 spidx->ul_proto = nxt; 915 if (!needport) 916 goto done_proto; 917 if (off + sizeof(struct udphdr) > m->m_pkthdr.len) 918 goto done; 919 m_copydata(m, off, sizeof(uh), &uh); 920 spidx->src.sin.sin_port = uh.uh_sport; 921 spidx->dst.sin.sin_port = uh.uh_dport; 922 return; 923 case IPPROTO_AH: 924 if (off + sizeof(ip6e) > m->m_pkthdr.len) 925 goto done; 926 /* XXX sigh, this works but is totally bogus */ 927 m_copydata(m, off, sizeof(ip6e), &ip6e); 928 off += (ip6e.ip6e_len + 2) << 2; 929 nxt = ip6e.ip6e_nxt; 930 break; 931 case IPPROTO_ICMP: 932 spidx->ul_proto = nxt; 933 if (off + sizeof(struct icmp) > m->m_pkthdr.len) 934 goto done; 935 m_copydata(m, off, sizeof(icmph), &icmph); 936 ((struct sockaddr_in *)&spidx->src)->sin_port = 937 htons((uint16_t)icmph.icmp_type); 938 ((struct sockaddr_in *)&spidx->dst)->sin_port = 939 htons((uint16_t)icmph.icmp_code); 940 return; 941 default: 942 /* XXX intermediate headers??? */ 943 spidx->ul_proto = nxt; 944 goto done_proto; 945 } 946 } 947 done: 948 spidx->ul_proto = IPSEC_ULPROTO_ANY; 949 done_proto: 950 spidx->src.sin.sin_port = IPSEC_PORT_ANY; 951 spidx->dst.sin.sin_port = IPSEC_PORT_ANY; 952 } 953 954 static int 955 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx) 956 { 957 static const struct sockaddr_in template = { 958 sizeof(struct sockaddr_in), 959 AF_INET, 960 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 } 961 }; 962 963 spidx->src.sin = template; 964 spidx->dst.sin = template; 965 966 if (m->m_len < sizeof(struct ip)) { 967 m_copydata(m, offsetof(struct ip, ip_src), 968 sizeof(struct in_addr), &spidx->src.sin.sin_addr); 969 m_copydata(m, offsetof(struct ip, ip_dst), 970 sizeof(struct in_addr), &spidx->dst.sin.sin_addr); 971 } else { 972 struct ip *ip = mtod(m, struct ip *); 973 spidx->src.sin.sin_addr = ip->ip_src; 974 spidx->dst.sin.sin_addr = ip->ip_dst; 975 } 976 977 spidx->prefs = sizeof(struct in_addr) << 3; 978 spidx->prefd = sizeof(struct in_addr) << 3; 979 980 return 0; 981 } 982 983 #ifdef INET6 984 static void 985 ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport) 986 { 987 int off, nxt; 988 struct tcphdr th; 989 struct udphdr uh; 990 struct icmp6_hdr icmph; 991 992 KASSERT(m != NULL); 993 994 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) { 995 kdebug_mbuf(__func__, m); 996 } 997 998 /* set default */ 999 spidx->ul_proto = IPSEC_ULPROTO_ANY; 1000 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY; 1001 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY; 1002 1003 nxt = -1; 1004 off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); 1005 if (off < 0 || m->m_pkthdr.len < off) 1006 return; 1007 1008 switch (nxt) { 1009 case IPPROTO_TCP: 1010 spidx->ul_proto = nxt; 1011 if (!needport) 1012 break; 1013 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len) 1014 break; 1015 m_copydata(m, off, sizeof(th), &th); 1016 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport; 1017 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport; 1018 break; 1019 case IPPROTO_UDP: 1020 spidx->ul_proto = nxt; 1021 if (!needport) 1022 break; 1023 if (off + sizeof(struct udphdr) > m->m_pkthdr.len) 1024 break; 1025 m_copydata(m, off, sizeof(uh), &uh); 1026 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport; 1027 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport; 1028 break; 1029 case IPPROTO_ICMPV6: 1030 spidx->ul_proto = nxt; 1031 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len) 1032 break; 1033 m_copydata(m, off, sizeof(icmph), &icmph); 1034 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = 1035 htons((uint16_t)icmph.icmp6_type); 1036 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = 1037 htons((uint16_t)icmph.icmp6_code); 1038 break; 1039 default: 1040 /* XXX intermediate headers??? */ 1041 spidx->ul_proto = nxt; 1042 break; 1043 } 1044 } 1045 1046 static int 1047 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx) 1048 { 1049 struct ip6_hdr *ip6 = NULL; 1050 struct ip6_hdr ip6buf; 1051 struct sockaddr_in6 *sin6; 1052 1053 if (m->m_len >= sizeof(*ip6)) { 1054 ip6 = mtod(m, struct ip6_hdr *); 1055 } else { 1056 m_copydata(m, 0, sizeof(ip6buf), &ip6buf); 1057 ip6 = &ip6buf; 1058 } 1059 1060 sin6 = (struct sockaddr_in6 *)&spidx->src; 1061 memset(sin6, 0, sizeof(*sin6)); 1062 sin6->sin6_family = AF_INET6; 1063 sin6->sin6_len = sizeof(struct sockaddr_in6); 1064 memcpy(&sin6->sin6_addr, &ip6->ip6_src, sizeof(ip6->ip6_src)); 1065 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) { 1066 sin6->sin6_addr.s6_addr16[1] = 0; 1067 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]); 1068 } 1069 spidx->prefs = sizeof(struct in6_addr) << 3; 1070 1071 sin6 = (struct sockaddr_in6 *)&spidx->dst; 1072 memset(sin6, 0, sizeof(*sin6)); 1073 sin6->sin6_family = AF_INET6; 1074 sin6->sin6_len = sizeof(struct sockaddr_in6); 1075 memcpy(&sin6->sin6_addr, &ip6->ip6_dst, sizeof(ip6->ip6_dst)); 1076 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) { 1077 sin6->sin6_addr.s6_addr16[1] = 0; 1078 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]); 1079 } 1080 spidx->prefd = sizeof(struct in6_addr) << 3; 1081 1082 return 0; 1083 } 1084 #endif 1085 1086 static void 1087 ipsec_delpcbpolicy(struct inpcbpolicy *p) 1088 { 1089 1090 kmem_intr_free(p, sizeof(*p)); 1091 } 1092 1093 int 1094 ipsec_init_pcbpolicy(struct socket *so, struct inpcbpolicy **policy) 1095 { 1096 struct inpcbpolicy *new; 1097 1098 KASSERT(so != NULL); 1099 KASSERT(policy != NULL); 1100 1101 new = kmem_intr_zalloc(sizeof(*new), KM_NOSLEEP); 1102 if (new == NULL) { 1103 IPSECLOG(LOG_DEBUG, "No more memory.\n"); 1104 return ENOBUFS; 1105 } 1106 1107 if (IPSEC_PRIVILEGED_SO(so)) 1108 new->priv = 1; 1109 else 1110 new->priv = 0; 1111 1112 /* 1113 * Set dummy SPs. Actual SPs will be allocated later if needed. 1114 */ 1115 new->sp_in = &ipsec_dummy_sp; 1116 new->sp_out = &ipsec_dummy_sp; 1117 1118 *policy = new; 1119 1120 return 0; 1121 } 1122 1123 static void 1124 ipsec_destroy_policy(struct secpolicy *sp) 1125 { 1126 1127 if (sp == &ipsec_dummy_sp) { 1128 ; /* It's dummy. No need to free it. */ 1129 } else { 1130 /* 1131 * We cannot destroy here because it can be called in 1132 * softint. So mark the SP as DEAD and let the timer 1133 * destroy it. See key_timehandler_spd. 1134 */ 1135 sp->state = IPSEC_SPSTATE_DEAD; 1136 } 1137 } 1138 1139 int 1140 ipsec_set_policy(void *inp, const void *request, size_t len, 1141 kauth_cred_t cred) 1142 { 1143 struct inpcb_hdr *inph = (struct inpcb_hdr *)inp; 1144 const struct sadb_x_policy *xpl; 1145 struct secpolicy *newsp, *oldsp; 1146 struct secpolicy **policy; 1147 int error; 1148 1149 KASSERT(!cpu_softintr_p()); 1150 KASSERT(inph != NULL); 1151 KASSERT(inph_locked(inph)); 1152 KASSERT(request != NULL); 1153 1154 if (len < sizeof(*xpl)) 1155 return EINVAL; 1156 xpl = (const struct sadb_x_policy *)request; 1157 1158 KASSERT(inph->inph_sp != NULL); 1159 1160 /* select direction */ 1161 switch (xpl->sadb_x_policy_dir) { 1162 case IPSEC_DIR_INBOUND: 1163 policy = &inph->inph_sp->sp_in; 1164 break; 1165 case IPSEC_DIR_OUTBOUND: 1166 policy = &inph->inph_sp->sp_out; 1167 break; 1168 default: 1169 IPSECLOG(LOG_ERR, "invalid direction=%u\n", 1170 xpl->sadb_x_policy_dir); 1171 return EINVAL; 1172 } 1173 1174 /* sanity check. */ 1175 if (policy == NULL || *policy == NULL) 1176 return EINVAL; 1177 1178 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) { 1179 kdebug_sadb_xpolicy("set passed policy", request); 1180 } 1181 1182 /* check policy type */ 1183 /* ipsec_set_policy() accepts IPSEC, ENTRUST and BYPASS. */ 1184 if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD || 1185 xpl->sadb_x_policy_type == IPSEC_POLICY_NONE) 1186 return EINVAL; 1187 1188 /* check privileged socket */ 1189 if (xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) { 1190 error = kauth_authorize_network(cred, KAUTH_NETWORK_IPSEC, 1191 KAUTH_REQ_NETWORK_IPSEC_BYPASS, NULL, NULL, NULL); 1192 if (error) 1193 return error; 1194 } 1195 1196 /* allocation new SP entry */ 1197 if ((newsp = key_msg2sp(xpl, len, &error)) == NULL) 1198 return error; 1199 1200 key_init_sp(newsp); 1201 newsp->created = time_uptime; 1202 /* Insert the global list for SPs for sockets */ 1203 key_socksplist_add(newsp); 1204 1205 /* clear old SP and set new SP */ 1206 oldsp = *policy; 1207 *policy = newsp; 1208 ipsec_destroy_policy(oldsp); 1209 1210 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) { 1211 printf("%s: new policy\n", __func__); 1212 kdebug_secpolicy(newsp); 1213 } 1214 1215 return 0; 1216 } 1217 1218 int 1219 ipsec_get_policy(void *inp, const void *request, size_t len, 1220 struct mbuf **mp) 1221 { 1222 struct inpcb_hdr *inph = (struct inpcb_hdr *)inp; 1223 const struct sadb_x_policy *xpl; 1224 struct secpolicy *policy; 1225 1226 /* sanity check. */ 1227 if (inph == NULL || request == NULL || mp == NULL) 1228 return EINVAL; 1229 KASSERT(inph->inph_sp != NULL); 1230 if (len < sizeof(*xpl)) 1231 return EINVAL; 1232 xpl = (const struct sadb_x_policy *)request; 1233 1234 /* select direction */ 1235 switch (xpl->sadb_x_policy_dir) { 1236 case IPSEC_DIR_INBOUND: 1237 policy = inph->inph_sp->sp_in; 1238 break; 1239 case IPSEC_DIR_OUTBOUND: 1240 policy = inph->inph_sp->sp_out; 1241 break; 1242 default: 1243 IPSECLOG(LOG_ERR, "invalid direction=%u\n", 1244 xpl->sadb_x_policy_dir); 1245 return EINVAL; 1246 } 1247 1248 if (policy == NULL) 1249 return EINVAL; 1250 1251 *mp = key_sp2msg(policy, M_NOWAIT); 1252 if (!*mp) { 1253 IPSECLOG(LOG_DEBUG, "No more memory.\n"); 1254 return ENOBUFS; 1255 } 1256 1257 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DUMP)) { 1258 kdebug_mbuf(__func__, *mp); 1259 } 1260 1261 return 0; 1262 } 1263 1264 int 1265 ipsec_delete_pcbpolicy(void *inp) 1266 { 1267 struct inpcb_hdr *inph = (struct inpcb_hdr *)inp; 1268 1269 KASSERT(inph != NULL); 1270 1271 if (inph->inph_sp == NULL) 1272 return 0; 1273 1274 if (inph->inph_sp->sp_in != NULL) 1275 ipsec_destroy_policy(inph->inph_sp->sp_in); 1276 1277 if (inph->inph_sp->sp_out != NULL) 1278 ipsec_destroy_policy(inph->inph_sp->sp_out); 1279 1280 ipsec_invalpcbcache(inph->inph_sp, IPSEC_DIR_ANY); 1281 1282 ipsec_delpcbpolicy(inph->inph_sp); 1283 inph->inph_sp = NULL; 1284 1285 return 0; 1286 } 1287 1288 /* 1289 * Return the current level (either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE). 1290 */ 1291 u_int 1292 ipsec_get_reqlevel(const struct ipsecrequest *isr) 1293 { 1294 u_int level = 0; 1295 u_int esp_trans_deflev, esp_net_deflev; 1296 u_int ah_trans_deflev, ah_net_deflev; 1297 1298 KASSERT(isr != NULL); 1299 KASSERT(isr->sp != NULL); 1300 KASSERTMSG( 1301 isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family, 1302 "af family mismatch, src %u, dst %u", 1303 isr->sp->spidx.src.sa.sa_family, isr->sp->spidx.dst.sa.sa_family); 1304 1305 /* XXX note that we have ipseclog() expanded here - code sync issue */ 1306 #define IPSEC_CHECK_DEFAULT(lev) \ 1307 (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE \ 1308 && (lev) != IPSEC_LEVEL_UNIQUE) ? \ 1309 (ipsec_debug ? log(LOG_INFO, "fixed system default level " #lev \ 1310 ":%d->%d\n", (lev), IPSEC_LEVEL_REQUIRE) : (void)0), \ 1311 (lev) = IPSEC_LEVEL_REQUIRE, (lev) \ 1312 : (lev)) 1313 1314 /* set default level */ 1315 switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) { 1316 #ifdef INET 1317 case AF_INET: 1318 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_trans_deflev); 1319 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip4_esp_net_deflev); 1320 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_trans_deflev); 1321 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip4_ah_net_deflev); 1322 break; 1323 #endif 1324 #ifdef INET6 1325 case AF_INET6: 1326 esp_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_trans_deflev); 1327 esp_net_deflev = IPSEC_CHECK_DEFAULT(ip6_esp_net_deflev); 1328 ah_trans_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_trans_deflev); 1329 ah_net_deflev = IPSEC_CHECK_DEFAULT(ip6_ah_net_deflev); 1330 break; 1331 #endif 1332 default: 1333 panic("%s: unknown af %u", __func__, 1334 isr->sp->spidx.src.sa.sa_family); 1335 } 1336 1337 #undef IPSEC_CHECK_DEFAULT 1338 1339 /* set level */ 1340 switch (isr->level) { 1341 case IPSEC_LEVEL_DEFAULT: 1342 switch (isr->saidx.proto) { 1343 case IPPROTO_ESP: 1344 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 1345 level = esp_net_deflev; 1346 else 1347 level = esp_trans_deflev; 1348 break; 1349 case IPPROTO_AH: 1350 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) 1351 level = ah_net_deflev; 1352 else 1353 level = ah_trans_deflev; 1354 break; 1355 case IPPROTO_IPCOMP: 1356 /* 1357 * we don't really care, as IPcomp document says that 1358 * we shouldn't compress small packets 1359 */ 1360 level = IPSEC_LEVEL_USE; 1361 break; 1362 default: 1363 panic("%s: Illegal protocol defined %u", __func__, 1364 isr->saidx.proto); 1365 } 1366 break; 1367 1368 case IPSEC_LEVEL_USE: 1369 case IPSEC_LEVEL_REQUIRE: 1370 level = isr->level; 1371 break; 1372 case IPSEC_LEVEL_UNIQUE: 1373 level = IPSEC_LEVEL_REQUIRE; 1374 break; 1375 1376 default: 1377 panic("%s: Illegal IPsec level %u", __func__, isr->level); 1378 } 1379 1380 return level; 1381 } 1382 1383 /* 1384 * Check security policy requirements against the actual packet contents. 1385 * 1386 * If the SP requires an IPsec packet, and the packet was neither AH nor ESP, 1387 * then kick it. 1388 */ 1389 static int 1390 ipsec_sp_reject(const struct secpolicy *sp, const struct mbuf *m) 1391 { 1392 struct ipsecrequest *isr; 1393 1394 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) { 1395 printf("%s: using SP\n", __func__); 1396 kdebug_secpolicy(sp); 1397 } 1398 1399 /* check policy */ 1400 switch (sp->policy) { 1401 case IPSEC_POLICY_DISCARD: 1402 return 1; 1403 case IPSEC_POLICY_BYPASS: 1404 case IPSEC_POLICY_NONE: 1405 return 0; 1406 } 1407 1408 KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC, 1409 "invalid policy %u", sp->policy); 1410 1411 /* XXX should compare policy against ipsec header history */ 1412 1413 for (isr = sp->req; isr != NULL; isr = isr->next) { 1414 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE) 1415 continue; 1416 switch (isr->saidx.proto) { 1417 case IPPROTO_ESP: 1418 if ((m->m_flags & M_DECRYPTED) == 0) { 1419 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP, 1420 "ESP m_flags:%x\n", m->m_flags); 1421 return 1; 1422 } 1423 break; 1424 case IPPROTO_AH: 1425 if ((m->m_flags & M_AUTHIPHDR) == 0) { 1426 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DUMP, 1427 "AH m_flags:%x\n", m->m_flags); 1428 return 1; 1429 } 1430 break; 1431 case IPPROTO_IPCOMP: 1432 /* 1433 * We don't really care, as IPcomp document 1434 * says that we shouldn't compress small 1435 * packets, IPComp policy should always be 1436 * treated as being in "use" level. 1437 */ 1438 break; 1439 } 1440 } 1441 1442 return 0; 1443 } 1444 1445 /* 1446 * Check security policy requirements. 1447 */ 1448 int 1449 ipsec_in_reject(struct mbuf *m, void *inp) 1450 { 1451 struct inpcb_hdr *inph = (struct inpcb_hdr *)inp; 1452 struct secpolicy *sp; 1453 int error; 1454 int result; 1455 1456 KASSERT(m != NULL); 1457 1458 if (inph == NULL) 1459 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 1460 IP_FORWARDING, &error); 1461 else 1462 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, 1463 inph, &error); 1464 1465 if (sp != NULL) { 1466 result = ipsec_sp_reject(sp, m); 1467 if (result) 1468 IPSEC_STATINC(IPSEC_STAT_IN_POLVIO); 1469 KEY_SP_UNREF(&sp); 1470 } else { 1471 result = 0; 1472 } 1473 return result; 1474 } 1475 1476 /* 1477 * Compute the byte size to be occupied by the IPsec header. If it is 1478 * tunneled, it includes the size of outer IP header. 1479 */ 1480 static size_t 1481 ipsec_sp_hdrsiz(const struct secpolicy *sp, const struct mbuf *m) 1482 { 1483 struct ipsecrequest *isr; 1484 size_t siz; 1485 1486 if (KEYDEBUG_ON(KEYDEBUG_IPSEC_DATA)) { 1487 printf("%s: using SP\n", __func__); 1488 kdebug_secpolicy(sp); 1489 } 1490 1491 switch (sp->policy) { 1492 case IPSEC_POLICY_DISCARD: 1493 case IPSEC_POLICY_BYPASS: 1494 case IPSEC_POLICY_NONE: 1495 return 0; 1496 } 1497 1498 KASSERTMSG(sp->policy == IPSEC_POLICY_IPSEC, 1499 "invalid policy %u", sp->policy); 1500 1501 siz = 0; 1502 for (isr = sp->req; isr != NULL; isr = isr->next) { 1503 size_t clen = 0; 1504 struct secasvar *sav; 1505 1506 switch (isr->saidx.proto) { 1507 case IPPROTO_ESP: 1508 sav = ipsec_lookup_sa(isr, m); 1509 if (sav != NULL) { 1510 clen = esp_hdrsiz(sav); 1511 KEY_SA_UNREF(&sav); 1512 } else 1513 clen = esp_hdrsiz(NULL); 1514 break; 1515 case IPPROTO_AH: 1516 sav = ipsec_lookup_sa(isr, m); 1517 if (sav != NULL) { 1518 clen = ah_hdrsiz(sav); 1519 KEY_SA_UNREF(&sav); 1520 } else 1521 clen = ah_hdrsiz(NULL); 1522 break; 1523 case IPPROTO_IPCOMP: 1524 clen = sizeof(struct ipcomp); 1525 break; 1526 } 1527 1528 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) { 1529 switch (isr->saidx.dst.sa.sa_family) { 1530 case AF_INET: 1531 clen += sizeof(struct ip); 1532 break; 1533 #ifdef INET6 1534 case AF_INET6: 1535 clen += sizeof(struct ip6_hdr); 1536 break; 1537 #endif 1538 default: 1539 IPSECLOG(LOG_ERR, "unknown AF %d in " 1540 "IPsec tunnel SA\n", 1541 ((const struct sockaddr *)&isr->saidx.dst) 1542 ->sa_family); 1543 break; 1544 } 1545 } 1546 siz += clen; 1547 } 1548 1549 return siz; 1550 } 1551 1552 size_t 1553 ipsec_hdrsiz(struct mbuf *m, u_int dir, void *inp) 1554 { 1555 struct inpcb_hdr *inph = (struct inpcb_hdr *)inp; 1556 struct secpolicy *sp; 1557 int error; 1558 size_t size; 1559 1560 KASSERT(m != NULL); 1561 KASSERTMSG(inph == NULL || inph->inph_socket != NULL, 1562 "socket w/o inpcb"); 1563 1564 if (inph == NULL) 1565 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error); 1566 else 1567 sp = ipsec_getpolicybysock(m, dir, inph, &error); 1568 1569 if (sp != NULL) { 1570 size = ipsec_sp_hdrsiz(sp, m); 1571 KEYDEBUG_PRINTF(KEYDEBUG_IPSEC_DATA, "size:%zu.\n", size); 1572 KEY_SP_UNREF(&sp); 1573 } else { 1574 size = 0; 1575 } 1576 1577 return size; 1578 } 1579 1580 /* 1581 * Check the variable replay window. 1582 * ipsec_chkreplay() performs replay check before ICV verification. 1583 * ipsec_updatereplay() updates replay bitmap. This must be called after 1584 * ICV verification (it also performs replay check, which is usually done 1585 * beforehand). 1586 * 0 (zero) is returned if packet disallowed, 1 if packet permitted. 1587 * 1588 * based on RFC 2401. 1589 */ 1590 int 1591 ipsec_chkreplay(u_int32_t seq, const struct secasvar *sav) 1592 { 1593 const struct secreplay *replay; 1594 u_int32_t diff; 1595 int fr; 1596 u_int32_t wsizeb; /* constant: bits of window size */ 1597 int frlast; /* constant: last frame */ 1598 1599 KASSERT(sav != NULL); 1600 KASSERT(sav->replay != NULL); 1601 1602 replay = sav->replay; 1603 1604 if (replay->wsize == 0) 1605 return 1; /* no need to check replay. */ 1606 1607 /* constant */ 1608 frlast = replay->wsize - 1; 1609 wsizeb = replay->wsize << 3; 1610 1611 /* sequence number of 0 is invalid */ 1612 if (seq == 0) 1613 return 0; 1614 1615 /* first time is always okay */ 1616 if (replay->count == 0) 1617 return 1; 1618 1619 if (seq > replay->lastseq) { 1620 /* larger sequences are okay */ 1621 return 1; 1622 } else { 1623 /* seq is equal or less than lastseq. */ 1624 diff = replay->lastseq - seq; 1625 1626 /* over range to check, i.e. too old or wrapped */ 1627 if (diff >= wsizeb) 1628 return 0; 1629 1630 fr = frlast - diff / 8; 1631 1632 /* this packet already seen ? */ 1633 if ((replay->bitmap)[fr] & (1 << (diff % 8))) 1634 return 0; 1635 1636 /* out of order but good */ 1637 return 1; 1638 } 1639 } 1640 1641 /* 1642 * check replay counter whether to update or not. 1643 * OUT: 0: OK 1644 * 1: NG 1645 */ 1646 int 1647 ipsec_updatereplay(u_int32_t seq, const struct secasvar *sav) 1648 { 1649 struct secreplay *replay; 1650 u_int32_t diff; 1651 int fr; 1652 u_int32_t wsizeb; /* constant: bits of window size */ 1653 int frlast; /* constant: last frame */ 1654 1655 KASSERT(sav != NULL); 1656 KASSERT(sav->replay != NULL); 1657 1658 replay = sav->replay; 1659 1660 if (replay->wsize == 0) 1661 goto ok; /* no need to check replay. */ 1662 1663 /* constant */ 1664 frlast = replay->wsize - 1; 1665 wsizeb = replay->wsize << 3; 1666 1667 /* sequence number of 0 is invalid */ 1668 if (seq == 0) 1669 return 1; 1670 1671 /* first time */ 1672 if (replay->count == 0) { 1673 replay->lastseq = seq; 1674 memset(replay->bitmap, 0, replay->wsize); 1675 (replay->bitmap)[frlast] = 1; 1676 goto ok; 1677 } 1678 1679 if (seq > replay->lastseq) { 1680 /* seq is larger than lastseq. */ 1681 diff = seq - replay->lastseq; 1682 1683 /* new larger sequence number */ 1684 if (diff < wsizeb) { 1685 /* In window */ 1686 /* set bit for this packet */ 1687 vshiftl(replay->bitmap, diff, replay->wsize); 1688 (replay->bitmap)[frlast] |= 1; 1689 } else { 1690 /* this packet has a "way larger" */ 1691 memset(replay->bitmap, 0, replay->wsize); 1692 (replay->bitmap)[frlast] = 1; 1693 } 1694 replay->lastseq = seq; 1695 1696 /* larger is good */ 1697 } else { 1698 /* seq is equal or less than lastseq. */ 1699 diff = replay->lastseq - seq; 1700 1701 /* over range to check, i.e. too old or wrapped */ 1702 if (diff >= wsizeb) 1703 return 1; 1704 1705 fr = frlast - diff / 8; 1706 1707 /* this packet already seen ? */ 1708 if ((replay->bitmap)[fr] & (1 << (diff % 8))) 1709 return 1; 1710 1711 /* mark as seen */ 1712 (replay->bitmap)[fr] |= (1 << (diff % 8)); 1713 1714 /* out of order but good */ 1715 } 1716 1717 ok: 1718 if (replay->count == ~0) { 1719 char buf[IPSEC_LOGSASTRLEN]; 1720 1721 /* set overflow flag */ 1722 replay->overflow++; 1723 1724 /* don't increment, no more packets accepted */ 1725 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0) 1726 return 1; 1727 1728 IPSECLOG(LOG_WARNING, "replay counter made %d cycle. %s\n", 1729 replay->overflow, ipsec_logsastr(sav, buf, sizeof(buf))); 1730 } 1731 1732 replay->count++; 1733 1734 return 0; 1735 } 1736 1737 /* 1738 * shift variable length buffer to left. 1739 * IN: bitmap: pointer to the buffer 1740 * nbit: the number of to shift. 1741 * wsize: buffer size (bytes). 1742 */ 1743 static void 1744 vshiftl(unsigned char *bitmap, int nbit, int wsize) 1745 { 1746 int s, j, i; 1747 unsigned char over; 1748 1749 for (j = 0; j < nbit; j += 8) { 1750 s = (nbit - j < 8) ? (nbit - j): 8; 1751 bitmap[0] <<= s; 1752 for (i = 1; i < wsize; i++) { 1753 over = (bitmap[i] >> (8 - s)); 1754 bitmap[i] <<= s; 1755 bitmap[i-1] |= over; 1756 } 1757 } 1758 1759 return; 1760 } 1761 1762 /* Return a printable string for the address. */ 1763 const char * 1764 ipsec_address(const union sockaddr_union *sa, char *buf, size_t size) 1765 { 1766 switch (sa->sa.sa_family) { 1767 case AF_INET: 1768 in_print(buf, size, &sa->sin.sin_addr); 1769 return buf; 1770 #if INET6 1771 case AF_INET6: 1772 in6_print(buf, size, &sa->sin6.sin6_addr); 1773 return buf; 1774 #endif 1775 default: 1776 return "(unknown address family)"; 1777 } 1778 } 1779 1780 const char * 1781 ipsec_logsastr(const struct secasvar *sav, char *buf, size_t size) 1782 { 1783 const struct secasindex *saidx = &sav->sah->saidx; 1784 char sbuf[IPSEC_ADDRSTRLEN], dbuf[IPSEC_ADDRSTRLEN]; 1785 1786 KASSERTMSG(saidx->src.sa.sa_family == saidx->dst.sa.sa_family, 1787 "af family mismatch, src %u, dst %u", 1788 saidx->src.sa.sa_family, saidx->dst.sa.sa_family); 1789 1790 snprintf(buf, size, "SA(SPI=%u src=%s dst=%s)", 1791 (u_int32_t)ntohl(sav->spi), 1792 ipsec_address(&saidx->src, sbuf, sizeof(sbuf)), 1793 ipsec_address(&saidx->dst, dbuf, sizeof(dbuf))); 1794 1795 return buf; 1796 } 1797 1798 #ifdef INET6 1799 struct secpolicy * 1800 ipsec6_check_policy(struct mbuf *m, struct in6pcb *in6p, int flags, 1801 int *needipsecp, int *errorp) 1802 { 1803 struct secpolicy *sp = NULL; 1804 int s; 1805 int error = 0; 1806 int needipsec = 0; 1807 1808 if (ipsec_outdone(m)) { 1809 goto skippolicycheck; 1810 } 1811 s = splsoftnet(); 1812 if (in6p && ipsec_pcb_skip_ipsec(in6p->in6p_sp, IPSEC_DIR_OUTBOUND)) { 1813 splx(s); 1814 goto skippolicycheck; 1815 } 1816 sp = ipsec_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, &error, in6p); 1817 splx(s); 1818 1819 /* 1820 * There are four return cases: 1821 * sp != NULL apply IPsec policy 1822 * sp == NULL, error == 0 no IPsec handling needed 1823 * sp == NULL, error == -EINVAL discard packet w/o error 1824 * sp == NULL, error != 0 discard packet, report error 1825 */ 1826 if (sp == NULL) { 1827 needipsec = 0; 1828 } else { 1829 needipsec = 1; 1830 } 1831 1832 skippolicycheck: 1833 *errorp = error; 1834 *needipsecp = needipsec; 1835 return sp; 1836 } 1837 1838 /* 1839 * calculate UDP checksum for UDP encapsulated ESP for IPv6. 1840 * 1841 * RFC2460(Internet Protocol, Version 6 Specification) says: 1842 * 1843 * IPv6 receivers MUST discard UDP packets with a zero checksum. 1844 * 1845 * There is more relaxed speficication RFC6935(IPv6 and UDP Checksums for 1846 * Tunneled Packets). The document allows zero checksum. It's too 1847 * late to publish, there are a lot of interoperability problems... 1848 */ 1849 void 1850 ipsec6_udp_cksum(struct mbuf *m) 1851 { 1852 struct ip6_hdr *ip6; 1853 uint16_t plen, uh_sum; 1854 int off; 1855 1856 /* must called after m_pullup() */ 1857 KASSERT(m->m_len >= sizeof(struct ip6_hdr)); 1858 1859 ip6 = mtod(m, struct ip6_hdr *); 1860 KASSERT(ip6->ip6_nxt == IPPROTO_UDP); 1861 1862 /* ip6->ip6_plen can not be updated before ip6_output() */ 1863 plen = m->m_pkthdr.len - sizeof(*ip6); 1864 KASSERT(plen >= sizeof(struct udphdr)); 1865 1866 uh_sum = in6_cksum(m, IPPROTO_UDP, sizeof(*ip6), plen); 1867 if (uh_sum == 0) 1868 uh_sum = 0xffff; 1869 1870 off = sizeof(*ip6) + offsetof(struct udphdr, uh_sum); 1871 m_copyback(m, off, sizeof(uh_sum), (void *)&uh_sum); 1872 } 1873 #endif /* INET6 */ 1874 1875 /* 1876 * ----------------------------------------------------------------------------- 1877 */ 1878 1879 /* XXX this stuff doesn't belong here... */ 1880 1881 static struct xformsw *xforms = NULL; 1882 1883 /* 1884 * Register a transform; typically at system startup. 1885 */ 1886 void 1887 xform_register(struct xformsw *xsp) 1888 { 1889 xsp->xf_next = xforms; 1890 xforms = xsp; 1891 } 1892 1893 /* 1894 * Initialize transform support in an sav. 1895 */ 1896 int 1897 xform_init(struct secasvar *sav, int xftype) 1898 { 1899 struct xformsw *xsp; 1900 1901 if (sav->tdb_xform != NULL) /* previously initialized */ 1902 return 0; 1903 for (xsp = xforms; xsp; xsp = xsp->xf_next) 1904 if (xsp->xf_type == xftype) 1905 return (*xsp->xf_init)(sav, xsp); 1906 1907 IPSECLOG(LOG_DEBUG, "no match for xform type %d\n", xftype); 1908 return EINVAL; 1909 } 1910 1911 /* 1912 * XXXJRT This should be done as a protosw init call. 1913 */ 1914 void 1915 ipsec_attach(void) 1916 { 1917 1918 ipsec_output_init(); 1919 1920 ipsecstat_percpu = percpu_alloc(sizeof(uint64_t) * IPSEC_NSTATS); 1921 1922 sysctl_net_inet_ipsec_setup(NULL); 1923 #ifdef INET6 1924 sysctl_net_inet6_ipsec6_setup(NULL); 1925 #endif 1926 1927 ah_attach(); 1928 esp_attach(); 1929 ipcomp_attach(); 1930 ipe4_attach(); 1931 #ifdef TCP_SIGNATURE 1932 tcpsignature_attach(); 1933 #endif 1934 } 1935