1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/stream.h> 28 #include <sys/stropts.h> 29 #include <sys/strsubr.h> 30 #include <sys/errno.h> 31 #include <sys/ddi.h> 32 #include <sys/debug.h> 33 #include <sys/cmn_err.h> 34 #include <sys/stream.h> 35 #include <sys/strlog.h> 36 #include <sys/kmem.h> 37 #include <sys/sunddi.h> 38 #include <sys/tihdr.h> 39 #include <sys/atomic.h> 40 #include <sys/socket.h> 41 #include <sys/sysmacros.h> 42 #include <sys/crypto/common.h> 43 #include <sys/crypto/api.h> 44 #include <sys/zone.h> 45 #include <netinet/in.h> 46 #include <net/if.h> 47 #include <net/pfkeyv2.h> 48 #include <inet/common.h> 49 #include <netinet/ip6.h> 50 #include <inet/ip.h> 51 #include <inet/ip_ire.h> 52 #include <inet/ip6.h> 53 #include <inet/ipsec_info.h> 54 #include <inet/tcp.h> 55 #include <inet/sadb.h> 56 #include <inet/ipsec_impl.h> 57 #include <inet/ipsecah.h> 58 #include <inet/ipsecesp.h> 59 #include <sys/random.h> 60 #include <sys/dlpi.h> 61 #include <sys/iphada.h> 62 #include <inet/ip_if.h> 63 #include <inet/ipdrop.h> 64 #include <inet/ipclassifier.h> 65 #include <inet/sctp_ip.h> 66 #include <inet/tun.h> 67 68 /* 69 * This source file contains Security Association Database (SADB) common 70 * routines. They are linked in with the AH module. Since AH has no chance 71 * of falling under export control, it was safe to link it in there. 72 */ 73 74 static mblk_t *sadb_extended_acquire(ipsec_selector_t *, ipsec_policy_t *, 75 ipsec_action_t *, boolean_t, uint32_t, uint32_t, netstack_t *); 76 static void sadb_ill_df(ill_t *, mblk_t *, isaf_t *, int, boolean_t); 77 static ipsa_t *sadb_torch_assoc(isaf_t *, ipsa_t *, boolean_t, mblk_t **); 78 static void sadb_drain_torchq(queue_t *, mblk_t *); 79 static void sadb_destroy_acqlist(iacqf_t **, uint_t, boolean_t, 80 netstack_t *); 81 static void sadb_destroy(sadb_t *, netstack_t *); 82 static mblk_t *sadb_sa2msg(ipsa_t *, sadb_msg_t *); 83 84 static time_t sadb_add_time(time_t, uint64_t); 85 static void lifetime_fuzz(ipsa_t *); 86 static void age_pair_peer_list(templist_t *, sadb_t *, boolean_t); 87 static void ipsa_set_replay(ipsa_t *ipsa, uint32_t offset); 88 89 extern void (*cl_inet_getspi)(netstackid_t stack_id, uint8_t protocol, 90 uint8_t *ptr, size_t len, void *args); 91 extern int (*cl_inet_checkspi)(netstackid_t stack_id, uint8_t protocol, 92 uint32_t spi, void *args); 93 extern void (*cl_inet_deletespi)(netstackid_t stack_id, uint8_t protocol, 94 uint32_t spi, void *args); 95 96 /* 97 * ipsacq_maxpackets is defined here to make it tunable 98 * from /etc/system. 99 */ 100 extern uint64_t ipsacq_maxpackets; 101 102 #define SET_EXPIRE(sa, delta, exp) { \ 103 if (((sa)->ipsa_ ## delta) != 0) { \ 104 (sa)->ipsa_ ## exp = sadb_add_time((sa)->ipsa_addtime, \ 105 (sa)->ipsa_ ## delta); \ 106 } \ 107 } 108 109 #define UPDATE_EXPIRE(sa, delta, exp) { \ 110 if (((sa)->ipsa_ ## delta) != 0) { \ 111 time_t tmp = sadb_add_time((sa)->ipsa_usetime, \ 112 (sa)->ipsa_ ## delta); \ 113 if (((sa)->ipsa_ ## exp) == 0) \ 114 (sa)->ipsa_ ## exp = tmp; \ 115 else \ 116 (sa)->ipsa_ ## exp = \ 117 MIN((sa)->ipsa_ ## exp, tmp); \ 118 } \ 119 } 120 121 122 /* wrap the macro so we can pass it as a function pointer */ 123 void 124 sadb_sa_refrele(void *target) 125 { 126 IPSA_REFRELE(((ipsa_t *)target)); 127 } 128 129 /* 130 * We presume that sizeof (long) == sizeof (time_t) and that time_t is 131 * a signed type. 132 */ 133 #define TIME_MAX LONG_MAX 134 135 /* 136 * PF_KEY gives us lifetimes in uint64_t seconds. We presume that 137 * time_t is defined to be a signed type with the same range as 138 * "long". On ILP32 systems, we thus run the risk of wrapping around 139 * at end of time, as well as "overwrapping" the clock back around 140 * into a seemingly valid but incorrect future date earlier than the 141 * desired expiration. 142 * 143 * In order to avoid odd behavior (either negative lifetimes or loss 144 * of high order bits) when someone asks for bizarrely long SA 145 * lifetimes, we do a saturating add for expire times. 146 * 147 * We presume that ILP32 systems will be past end of support life when 148 * the 32-bit time_t overflows (a dangerous assumption, mind you..). 149 * 150 * On LP64, 2^64 seconds are about 5.8e11 years, at which point we 151 * will hopefully have figured out clever ways to avoid the use of 152 * fixed-sized integers in computation. 153 */ 154 static time_t 155 sadb_add_time(time_t base, uint64_t delta) 156 { 157 time_t sum; 158 159 /* 160 * Clip delta to the maximum possible time_t value to 161 * prevent "overwrapping" back into a shorter-than-desired 162 * future time. 163 */ 164 if (delta > TIME_MAX) 165 delta = TIME_MAX; 166 /* 167 * This sum may still overflow. 168 */ 169 sum = base + delta; 170 171 /* 172 * .. so if the result is less than the base, we overflowed. 173 */ 174 if (sum < base) 175 sum = TIME_MAX; 176 177 return (sum); 178 } 179 180 /* 181 * Callers of this function have already created a working security 182 * association, and have found the appropriate table & hash chain. All this 183 * function does is check duplicates, and insert the SA. The caller needs to 184 * hold the hash bucket lock and increment the refcnt before insertion. 185 * 186 * Return 0 if success, EEXIST if collision. 187 */ 188 #define SA_UNIQUE_MATCH(sa1, sa2) \ 189 (((sa1)->ipsa_unique_id & (sa1)->ipsa_unique_mask) == \ 190 ((sa2)->ipsa_unique_id & (sa2)->ipsa_unique_mask)) 191 192 int 193 sadb_insertassoc(ipsa_t *ipsa, isaf_t *bucket) 194 { 195 ipsa_t **ptpn = NULL; 196 ipsa_t *walker; 197 boolean_t unspecsrc; 198 199 ASSERT(MUTEX_HELD(&bucket->isaf_lock)); 200 201 unspecsrc = IPSA_IS_ADDR_UNSPEC(ipsa->ipsa_srcaddr, ipsa->ipsa_addrfam); 202 203 walker = bucket->isaf_ipsa; 204 ASSERT(walker == NULL || ipsa->ipsa_addrfam == walker->ipsa_addrfam); 205 206 /* 207 * Find insertion point (pointed to with **ptpn). Insert at the head 208 * of the list unless there's an unspecified source address, then 209 * insert it after the last SA with a specified source address. 210 * 211 * BTW, you'll have to walk the whole chain, matching on {DST, SPI} 212 * checking for collisions. 213 */ 214 215 while (walker != NULL) { 216 if (IPSA_ARE_ADDR_EQUAL(walker->ipsa_dstaddr, 217 ipsa->ipsa_dstaddr, ipsa->ipsa_addrfam)) { 218 if (walker->ipsa_spi == ipsa->ipsa_spi) 219 return (EEXIST); 220 221 mutex_enter(&walker->ipsa_lock); 222 if (ipsa->ipsa_state == IPSA_STATE_MATURE && 223 (walker->ipsa_flags & IPSA_F_USED) && 224 SA_UNIQUE_MATCH(walker, ipsa)) { 225 walker->ipsa_flags |= IPSA_F_CINVALID; 226 } 227 mutex_exit(&walker->ipsa_lock); 228 } 229 230 if (ptpn == NULL && unspecsrc) { 231 if (IPSA_IS_ADDR_UNSPEC(walker->ipsa_srcaddr, 232 walker->ipsa_addrfam)) 233 ptpn = walker->ipsa_ptpn; 234 else if (walker->ipsa_next == NULL) 235 ptpn = &walker->ipsa_next; 236 } 237 238 walker = walker->ipsa_next; 239 } 240 241 if (ptpn == NULL) 242 ptpn = &bucket->isaf_ipsa; 243 ipsa->ipsa_next = *ptpn; 244 ipsa->ipsa_ptpn = ptpn; 245 if (ipsa->ipsa_next != NULL) 246 ipsa->ipsa_next->ipsa_ptpn = &ipsa->ipsa_next; 247 *ptpn = ipsa; 248 ipsa->ipsa_linklock = &bucket->isaf_lock; 249 250 return (0); 251 } 252 #undef SA_UNIQUE_MATCH 253 254 /* 255 * Free a security association. Its reference count is 0, which means 256 * I must free it. The SA must be unlocked and must not be linked into 257 * any fanout list. 258 */ 259 static void 260 sadb_freeassoc(ipsa_t *ipsa) 261 { 262 ipsec_stack_t *ipss = ipsa->ipsa_netstack->netstack_ipsec; 263 264 ASSERT(ipss != NULL); 265 ASSERT(MUTEX_NOT_HELD(&ipsa->ipsa_lock)); 266 ASSERT(ipsa->ipsa_refcnt == 0); 267 ASSERT(ipsa->ipsa_next == NULL); 268 ASSERT(ipsa->ipsa_ptpn == NULL); 269 270 mutex_enter(&ipsa->ipsa_lock); 271 /* Don't call sadb_clear_lpkt() since we hold the ipsa_lock anyway. */ 272 ip_drop_packet(ipsa->ipsa_lpkt, B_TRUE, NULL, NULL, 273 DROPPER(ipss, ipds_sadb_inlarval_timeout), 274 &ipss->ipsec_sadb_dropper); 275 ipsec_destroy_ctx_tmpl(ipsa, IPSEC_ALG_AUTH); 276 ipsec_destroy_ctx_tmpl(ipsa, IPSEC_ALG_ENCR); 277 mutex_exit(&ipsa->ipsa_lock); 278 279 /* bzero() these fields for paranoia's sake. */ 280 if (ipsa->ipsa_authkey != NULL) { 281 bzero(ipsa->ipsa_authkey, ipsa->ipsa_authkeylen); 282 kmem_free(ipsa->ipsa_authkey, ipsa->ipsa_authkeylen); 283 } 284 if (ipsa->ipsa_encrkey != NULL) { 285 bzero(ipsa->ipsa_encrkey, ipsa->ipsa_encrkeylen); 286 kmem_free(ipsa->ipsa_encrkey, ipsa->ipsa_encrkeylen); 287 } 288 if (ipsa->ipsa_src_cid != NULL) { 289 IPSID_REFRELE(ipsa->ipsa_src_cid); 290 } 291 if (ipsa->ipsa_dst_cid != NULL) { 292 IPSID_REFRELE(ipsa->ipsa_dst_cid); 293 } 294 if (ipsa->ipsa_integ != NULL) 295 kmem_free(ipsa->ipsa_integ, ipsa->ipsa_integlen); 296 if (ipsa->ipsa_sens != NULL) 297 kmem_free(ipsa->ipsa_sens, ipsa->ipsa_senslen); 298 299 mutex_destroy(&ipsa->ipsa_lock); 300 kmem_free(ipsa, sizeof (*ipsa)); 301 } 302 303 /* 304 * Unlink a security association from a hash bucket. Assume the hash bucket 305 * lock is held, but the association's lock is not. 306 * 307 * Note that we do not bump the bucket's generation number here because 308 * we might not be making a visible change to the set of visible SA's. 309 * All callers MUST bump the bucket's generation number before they unlock 310 * the bucket if they use sadb_unlinkassoc to permanetly remove an SA which 311 * was present in the bucket at the time it was locked. 312 */ 313 void 314 sadb_unlinkassoc(ipsa_t *ipsa) 315 { 316 ASSERT(ipsa->ipsa_linklock != NULL); 317 ASSERT(MUTEX_HELD(ipsa->ipsa_linklock)); 318 319 /* These fields are protected by the link lock. */ 320 *(ipsa->ipsa_ptpn) = ipsa->ipsa_next; 321 if (ipsa->ipsa_next != NULL) { 322 ipsa->ipsa_next->ipsa_ptpn = ipsa->ipsa_ptpn; 323 ipsa->ipsa_next = NULL; 324 } 325 326 ipsa->ipsa_ptpn = NULL; 327 328 /* This may destroy the SA. */ 329 IPSA_REFRELE(ipsa); 330 } 331 332 void 333 sadb_delete_cluster(ipsa_t *assoc) 334 { 335 uint8_t protocol; 336 337 if (cl_inet_deletespi && 338 ((assoc->ipsa_state == IPSA_STATE_LARVAL) || 339 (assoc->ipsa_state == IPSA_STATE_MATURE))) { 340 protocol = (assoc->ipsa_type == SADB_SATYPE_AH) ? 341 IPPROTO_AH : IPPROTO_ESP; 342 cl_inet_deletespi(assoc->ipsa_netstack->netstack_stackid, 343 protocol, assoc->ipsa_spi, NULL); 344 } 345 } 346 347 /* 348 * Create a larval security association with the specified SPI. All other 349 * fields are zeroed. 350 */ 351 static ipsa_t * 352 sadb_makelarvalassoc(uint32_t spi, uint32_t *src, uint32_t *dst, int addrfam, 353 netstack_t *ns) 354 { 355 ipsa_t *newbie; 356 357 /* 358 * Allocate... 359 */ 360 361 newbie = (ipsa_t *)kmem_zalloc(sizeof (ipsa_t), KM_NOSLEEP); 362 if (newbie == NULL) { 363 /* Can't make new larval SA. */ 364 return (NULL); 365 } 366 367 /* Assigned requested SPI, assume caller does SPI allocation magic. */ 368 newbie->ipsa_spi = spi; 369 newbie->ipsa_netstack = ns; /* No netstack_hold */ 370 371 /* 372 * Copy addresses... 373 */ 374 375 IPSA_COPY_ADDR(newbie->ipsa_srcaddr, src, addrfam); 376 IPSA_COPY_ADDR(newbie->ipsa_dstaddr, dst, addrfam); 377 378 newbie->ipsa_addrfam = addrfam; 379 380 /* 381 * Set common initialization values, including refcnt. 382 */ 383 mutex_init(&newbie->ipsa_lock, NULL, MUTEX_DEFAULT, NULL); 384 newbie->ipsa_state = IPSA_STATE_LARVAL; 385 newbie->ipsa_refcnt = 1; 386 newbie->ipsa_freefunc = sadb_freeassoc; 387 388 /* 389 * There aren't a lot of other common initialization values, as 390 * they are copied in from the PF_KEY message. 391 */ 392 393 return (newbie); 394 } 395 396 /* 397 * Call me to initialize a security association fanout. 398 */ 399 static int 400 sadb_init_fanout(isaf_t **tablep, uint_t size, int kmflag) 401 { 402 isaf_t *table; 403 int i; 404 405 table = (isaf_t *)kmem_alloc(size * sizeof (*table), kmflag); 406 *tablep = table; 407 408 if (table == NULL) 409 return (ENOMEM); 410 411 for (i = 0; i < size; i++) { 412 mutex_init(&(table[i].isaf_lock), NULL, MUTEX_DEFAULT, NULL); 413 table[i].isaf_ipsa = NULL; 414 table[i].isaf_gen = 0; 415 } 416 417 return (0); 418 } 419 420 /* 421 * Call me to initialize an acquire fanout 422 */ 423 static int 424 sadb_init_acfanout(iacqf_t **tablep, uint_t size, int kmflag) 425 { 426 iacqf_t *table; 427 int i; 428 429 table = (iacqf_t *)kmem_alloc(size * sizeof (*table), kmflag); 430 *tablep = table; 431 432 if (table == NULL) 433 return (ENOMEM); 434 435 for (i = 0; i < size; i++) { 436 mutex_init(&(table[i].iacqf_lock), NULL, MUTEX_DEFAULT, NULL); 437 table[i].iacqf_ipsacq = NULL; 438 } 439 440 return (0); 441 } 442 443 /* 444 * Attempt to initialize an SADB instance. On failure, return ENOMEM; 445 * caller must clean up partial allocations. 446 */ 447 static int 448 sadb_init_trial(sadb_t *sp, uint_t size, int kmflag) 449 { 450 ASSERT(sp->sdb_of == NULL); 451 ASSERT(sp->sdb_if == NULL); 452 ASSERT(sp->sdb_acq == NULL); 453 454 sp->sdb_hashsize = size; 455 if (sadb_init_fanout(&sp->sdb_of, size, kmflag) != 0) 456 return (ENOMEM); 457 if (sadb_init_fanout(&sp->sdb_if, size, kmflag) != 0) 458 return (ENOMEM); 459 if (sadb_init_acfanout(&sp->sdb_acq, size, kmflag) != 0) 460 return (ENOMEM); 461 462 return (0); 463 } 464 465 /* 466 * Call me to initialize an SADB instance; fall back to default size on failure. 467 */ 468 static void 469 sadb_init(const char *name, sadb_t *sp, uint_t size, uint_t ver, 470 netstack_t *ns) 471 { 472 ASSERT(sp->sdb_of == NULL); 473 ASSERT(sp->sdb_if == NULL); 474 ASSERT(sp->sdb_acq == NULL); 475 476 if (size < IPSEC_DEFAULT_HASH_SIZE) 477 size = IPSEC_DEFAULT_HASH_SIZE; 478 479 if (sadb_init_trial(sp, size, KM_NOSLEEP) != 0) { 480 481 cmn_err(CE_WARN, 482 "Unable to allocate %u entry IPv%u %s SADB hash table", 483 size, ver, name); 484 485 sadb_destroy(sp, ns); 486 size = IPSEC_DEFAULT_HASH_SIZE; 487 cmn_err(CE_WARN, "Falling back to %d entries", size); 488 (void) sadb_init_trial(sp, size, KM_SLEEP); 489 } 490 } 491 492 493 /* 494 * Initialize an SADB-pair. 495 */ 496 void 497 sadbp_init(const char *name, sadbp_t *sp, int type, int size, netstack_t *ns) 498 { 499 sadb_init(name, &sp->s_v4, size, 4, ns); 500 sadb_init(name, &sp->s_v6, size, 6, ns); 501 502 sp->s_satype = type; 503 504 ASSERT((type == SADB_SATYPE_AH) || (type == SADB_SATYPE_ESP)); 505 if (type == SADB_SATYPE_AH) { 506 ipsec_stack_t *ipss = ns->netstack_ipsec; 507 508 ip_drop_register(&ipss->ipsec_sadb_dropper, "IPsec SADB"); 509 sp->s_addflags = AH_ADD_SETTABLE_FLAGS; 510 sp->s_updateflags = AH_UPDATE_SETTABLE_FLAGS; 511 } else { 512 sp->s_addflags = ESP_ADD_SETTABLE_FLAGS; 513 sp->s_updateflags = ESP_UPDATE_SETTABLE_FLAGS; 514 } 515 } 516 517 /* 518 * Deliver a single SADB_DUMP message representing a single SA. This is 519 * called many times by sadb_dump(). 520 * 521 * If the return value of this is ENOBUFS (not the same as ENOMEM), then 522 * the caller should take that as a hint that dupb() on the "original answer" 523 * failed, and that perhaps the caller should try again with a copyb()ed 524 * "original answer". 525 */ 526 static int 527 sadb_dump_deliver(queue_t *pfkey_q, mblk_t *original_answer, ipsa_t *ipsa, 528 sadb_msg_t *samsg) 529 { 530 mblk_t *answer; 531 532 answer = dupb(original_answer); 533 if (answer == NULL) 534 return (ENOBUFS); 535 answer->b_cont = sadb_sa2msg(ipsa, samsg); 536 if (answer->b_cont == NULL) { 537 freeb(answer); 538 return (ENOMEM); 539 } 540 541 /* Just do a putnext, and let keysock deal with flow control. */ 542 putnext(pfkey_q, answer); 543 return (0); 544 } 545 546 /* 547 * Common function to allocate and prepare a keysock_out_t M_CTL message. 548 */ 549 mblk_t * 550 sadb_keysock_out(minor_t serial) 551 { 552 mblk_t *mp; 553 keysock_out_t *kso; 554 555 mp = allocb(sizeof (ipsec_info_t), BPRI_HI); 556 if (mp != NULL) { 557 mp->b_datap->db_type = M_CTL; 558 mp->b_wptr += sizeof (ipsec_info_t); 559 kso = (keysock_out_t *)mp->b_rptr; 560 kso->ks_out_type = KEYSOCK_OUT; 561 kso->ks_out_len = sizeof (*kso); 562 kso->ks_out_serial = serial; 563 } 564 565 return (mp); 566 } 567 568 /* 569 * Perform an SADB_DUMP, spewing out every SA in an array of SA fanouts 570 * to keysock. 571 */ 572 static int 573 sadb_dump_fanout(queue_t *pfkey_q, mblk_t *mp, minor_t serial, isaf_t *fanout, 574 int num_entries, boolean_t do_peers, time_t active_time) 575 { 576 int i, error = 0; 577 mblk_t *original_answer; 578 ipsa_t *walker; 579 sadb_msg_t *samsg; 580 time_t current; 581 582 /* 583 * For each IPSA hash bucket do: 584 * - Hold the mutex 585 * - Walk each entry, doing an sadb_dump_deliver() on it. 586 */ 587 ASSERT(mp->b_cont != NULL); 588 samsg = (sadb_msg_t *)mp->b_cont->b_rptr; 589 590 original_answer = sadb_keysock_out(serial); 591 if (original_answer == NULL) 592 return (ENOMEM); 593 594 current = gethrestime_sec(); 595 for (i = 0; i < num_entries; i++) { 596 mutex_enter(&fanout[i].isaf_lock); 597 for (walker = fanout[i].isaf_ipsa; walker != NULL; 598 walker = walker->ipsa_next) { 599 if (!do_peers && walker->ipsa_haspeer) 600 continue; 601 if ((active_time != 0) && 602 ((current - walker->ipsa_lastuse) > active_time)) 603 continue; 604 error = sadb_dump_deliver(pfkey_q, original_answer, 605 walker, samsg); 606 if (error == ENOBUFS) { 607 mblk_t *new_original_answer; 608 609 /* Ran out of dupb's. Try a copyb. */ 610 new_original_answer = copyb(original_answer); 611 if (new_original_answer == NULL) { 612 error = ENOMEM; 613 } else { 614 freeb(original_answer); 615 original_answer = new_original_answer; 616 error = sadb_dump_deliver(pfkey_q, 617 original_answer, walker, samsg); 618 } 619 } 620 if (error != 0) 621 break; /* out of for loop. */ 622 } 623 mutex_exit(&fanout[i].isaf_lock); 624 if (error != 0) 625 break; /* out of for loop. */ 626 } 627 628 freeb(original_answer); 629 return (error); 630 } 631 632 /* 633 * Dump an entire SADB; outbound first, then inbound. 634 */ 635 636 int 637 sadb_dump(queue_t *pfkey_q, mblk_t *mp, keysock_in_t *ksi, sadb_t *sp) 638 { 639 int error; 640 time_t active_time = 0; 641 sadb_x_edump_t *edump = 642 (sadb_x_edump_t *)ksi->ks_in_extv[SADB_X_EXT_EDUMP]; 643 644 if (edump != NULL) { 645 active_time = edump->sadb_x_edump_timeout; 646 } 647 648 /* Dump outbound */ 649 error = sadb_dump_fanout(pfkey_q, mp, ksi->ks_in_serial, sp->sdb_of, 650 sp->sdb_hashsize, B_TRUE, active_time); 651 if (error) 652 return (error); 653 654 /* Dump inbound */ 655 return sadb_dump_fanout(pfkey_q, mp, ksi->ks_in_serial, sp->sdb_if, 656 sp->sdb_hashsize, B_FALSE, active_time); 657 } 658 659 /* 660 * Generic sadb table walker. 661 * 662 * Call "walkfn" for each SA in each bucket in "table"; pass the 663 * bucket, the entry and "cookie" to the callback function. 664 * Take care to ensure that walkfn can delete the SA without screwing 665 * up our traverse. 666 * 667 * The bucket is locked for the duration of the callback, both so that the 668 * callback can just call sadb_unlinkassoc() when it wants to delete something, 669 * and so that no new entries are added while we're walking the list. 670 */ 671 static void 672 sadb_walker(isaf_t *table, uint_t numentries, 673 void (*walkfn)(isaf_t *head, ipsa_t *entry, void *cookie), 674 void *cookie) 675 { 676 int i; 677 for (i = 0; i < numentries; i++) { 678 ipsa_t *entry, *next; 679 680 mutex_enter(&table[i].isaf_lock); 681 682 for (entry = table[i].isaf_ipsa; entry != NULL; 683 entry = next) { 684 next = entry->ipsa_next; 685 (*walkfn)(&table[i], entry, cookie); 686 } 687 mutex_exit(&table[i].isaf_lock); 688 } 689 } 690 691 /* 692 * From the given SA, construct a dl_ct_ipsec_key and 693 * a dl_ct_ipsec structures to be sent to the adapter as part 694 * of a DL_CONTROL_REQ. 695 * 696 * ct_sa must point to the storage allocated for the key 697 * structure and must be followed by storage allocated 698 * for the SA information that must be sent to the driver 699 * as part of the DL_CONTROL_REQ request. 700 * 701 * The is_inbound boolean indicates whether the specified 702 * SA is part of an inbound SA table. 703 * 704 * Returns B_TRUE if the corresponding SA must be passed to 705 * a provider, B_FALSE otherwise; frees *mp if it returns B_FALSE. 706 */ 707 static boolean_t 708 sadb_req_from_sa(ipsa_t *sa, mblk_t *mp, boolean_t is_inbound) 709 { 710 dl_ct_ipsec_key_t *keyp; 711 dl_ct_ipsec_t *sap; 712 void *ct_sa = mp->b_wptr; 713 714 ASSERT(MUTEX_HELD(&sa->ipsa_lock)); 715 716 keyp = (dl_ct_ipsec_key_t *)(ct_sa); 717 sap = (dl_ct_ipsec_t *)(keyp + 1); 718 719 IPSECHW_DEBUG(IPSECHW_CAPAB, ("sadb_req_from_sa: " 720 "is_inbound = %d\n", is_inbound)); 721 722 /* initialize flag */ 723 sap->sadb_sa_flags = 0; 724 if (is_inbound) { 725 sap->sadb_sa_flags |= DL_CT_IPSEC_INBOUND; 726 /* 727 * If an inbound SA has a peer, then mark it has being 728 * an outbound SA as well. 729 */ 730 if (sa->ipsa_haspeer) 731 sap->sadb_sa_flags |= DL_CT_IPSEC_OUTBOUND; 732 } else { 733 /* 734 * If an outbound SA has a peer, then don't send it, 735 * since we will send the copy from the inbound table. 736 */ 737 if (sa->ipsa_haspeer) { 738 freemsg(mp); 739 return (B_FALSE); 740 } 741 sap->sadb_sa_flags |= DL_CT_IPSEC_OUTBOUND; 742 } 743 744 keyp->dl_key_spi = sa->ipsa_spi; 745 bcopy(sa->ipsa_dstaddr, keyp->dl_key_dest_addr, 746 DL_CTL_IPSEC_ADDR_LEN); 747 keyp->dl_key_addr_family = sa->ipsa_addrfam; 748 749 sap->sadb_sa_auth = sa->ipsa_auth_alg; 750 sap->sadb_sa_encrypt = sa->ipsa_encr_alg; 751 752 sap->sadb_key_len_a = sa->ipsa_authkeylen; 753 sap->sadb_key_bits_a = sa->ipsa_authkeybits; 754 bcopy(sa->ipsa_authkey, 755 sap->sadb_key_data_a, sap->sadb_key_len_a); 756 757 sap->sadb_key_len_e = sa->ipsa_encrkeylen; 758 sap->sadb_key_bits_e = sa->ipsa_encrkeybits; 759 bcopy(sa->ipsa_encrkey, 760 sap->sadb_key_data_e, sap->sadb_key_len_e); 761 762 mp->b_wptr += sizeof (dl_ct_ipsec_t) + sizeof (dl_ct_ipsec_key_t); 763 return (B_TRUE); 764 } 765 766 /* 767 * Called from AH or ESP to format a message which will be used to inform 768 * IPsec-acceleration-capable ills of a SADB change. 769 * (It is not possible to send the message to IP directly from this function 770 * since the SA, if any, is locked during the call). 771 * 772 * dl_operation: DL_CONTROL_REQ operation (add, delete, update, etc) 773 * sa_type: identifies whether the operation applies to AH or ESP 774 * (must be one of SADB_SATYPE_AH or SADB_SATYPE_ESP) 775 * sa: Pointer to an SA. Must be non-NULL and locked 776 * for ADD, DELETE, GET, and UPDATE operations. 777 * This function returns an mblk chain that must be passed to IP 778 * for forwarding to the IPsec capable providers. 779 */ 780 mblk_t * 781 sadb_fmt_sa_req(uint_t dl_operation, uint_t sa_type, ipsa_t *sa, 782 boolean_t is_inbound) 783 { 784 mblk_t *mp; 785 dl_control_req_t *ctrl; 786 boolean_t need_key = B_FALSE; 787 mblk_t *ctl_mp = NULL; 788 ipsec_ctl_t *ctl; 789 790 /* 791 * 1 allocate and initialize DL_CONTROL_REQ M_PROTO 792 * 2 if a key is needed for the operation 793 * 2.1 initialize key 794 * 2.2 if a full SA is needed for the operation 795 * 2.2.1 initialize full SA info 796 * 3 return message; caller will call ill_ipsec_capab_send_all() 797 * to send the resulting message to IPsec capable ills. 798 */ 799 800 ASSERT(sa_type == SADB_SATYPE_AH || sa_type == SADB_SATYPE_ESP); 801 802 /* 803 * Allocate DL_CONTROL_REQ M_PROTO 804 * We allocate room for the SA even if it's not needed 805 * by some of the operations (for example flush) 806 */ 807 mp = allocb(sizeof (dl_control_req_t) + 808 sizeof (dl_ct_ipsec_key_t) + sizeof (dl_ct_ipsec_t), BPRI_HI); 809 if (mp == NULL) 810 return (NULL); 811 mp->b_datap->db_type = M_PROTO; 812 813 /* initialize dl_control_req_t */ 814 ctrl = (dl_control_req_t *)mp->b_wptr; 815 ctrl->dl_primitive = DL_CONTROL_REQ; 816 ctrl->dl_operation = dl_operation; 817 ctrl->dl_type = sa_type == SADB_SATYPE_AH ? DL_CT_IPSEC_AH : 818 DL_CT_IPSEC_ESP; 819 ctrl->dl_key_offset = sizeof (dl_control_req_t); 820 ctrl->dl_key_length = sizeof (dl_ct_ipsec_key_t); 821 ctrl->dl_data_offset = sizeof (dl_control_req_t) + 822 sizeof (dl_ct_ipsec_key_t); 823 ctrl->dl_data_length = sizeof (dl_ct_ipsec_t); 824 mp->b_wptr += sizeof (dl_control_req_t); 825 826 if ((dl_operation == DL_CO_SET) || (dl_operation == DL_CO_DELETE)) { 827 ASSERT(sa != NULL); 828 ASSERT(MUTEX_HELD(&sa->ipsa_lock)); 829 830 need_key = B_TRUE; 831 832 /* 833 * Initialize key and SA data. Note that for some 834 * operations the SA data is ignored by the provider 835 * (delete, etc.) 836 */ 837 if (!sadb_req_from_sa(sa, mp, is_inbound)) 838 return (NULL); 839 } 840 841 /* construct control message */ 842 ctl_mp = allocb(sizeof (ipsec_ctl_t), BPRI_HI); 843 if (ctl_mp == NULL) { 844 cmn_err(CE_WARN, "sadb_fmt_sa_req: allocb failed\n"); 845 freemsg(mp); 846 return (NULL); 847 } 848 849 ctl_mp->b_datap->db_type = M_CTL; 850 ctl_mp->b_wptr += sizeof (ipsec_ctl_t); 851 ctl_mp->b_cont = mp; 852 853 ctl = (ipsec_ctl_t *)ctl_mp->b_rptr; 854 ctl->ipsec_ctl_type = IPSEC_CTL; 855 ctl->ipsec_ctl_len = sizeof (ipsec_ctl_t); 856 ctl->ipsec_ctl_sa_type = sa_type; 857 858 if (need_key) { 859 /* 860 * Keep an additional reference on SA, since it will be 861 * needed by IP to send control messages corresponding 862 * to that SA from its perimeter. IP will do a 863 * IPSA_REFRELE when done with the request. 864 */ 865 ASSERT(MUTEX_HELD(&sa->ipsa_lock)); 866 IPSA_REFHOLD(sa); 867 ctl->ipsec_ctl_sa = sa; 868 } else 869 ctl->ipsec_ctl_sa = NULL; 870 871 return (ctl_mp); 872 } 873 874 875 /* 876 * Called by sadb_ill_download() to dump the entries for a specific 877 * fanout table. For each SA entry in the table passed as argument, 878 * use mp as a template and constructs a full DL_CONTROL message, and 879 * call ill_dlpi_send(), provided by IP, to send the resulting 880 * messages to the ill. 881 */ 882 static void 883 sadb_ill_df(ill_t *ill, mblk_t *mp, isaf_t *fanout, int num_entries, 884 boolean_t is_inbound) 885 { 886 ipsa_t *walker; 887 mblk_t *nmp, *salist; 888 int i, error = 0; 889 ip_stack_t *ipst = ill->ill_ipst; 890 netstack_t *ns = ipst->ips_netstack; 891 892 IPSECHW_DEBUG(IPSECHW_SADB, ("sadb_ill_df: fanout at 0x%p ne=%d\n", 893 (void *)fanout, num_entries)); 894 /* 895 * For each IPSA hash bucket do: 896 * - Hold the mutex 897 * - Walk each entry, sending a corresponding request to IP 898 * for it. 899 */ 900 ASSERT(mp->b_datap->db_type == M_PROTO); 901 902 for (i = 0; i < num_entries; i++) { 903 mutex_enter(&fanout[i].isaf_lock); 904 salist = NULL; 905 906 for (walker = fanout[i].isaf_ipsa; walker != NULL; 907 walker = walker->ipsa_next) { 908 IPSECHW_DEBUG(IPSECHW_SADB, 909 ("sadb_ill_df: sending SA to ill via IP \n")); 910 /* 911 * Duplicate the template mp passed and 912 * complete DL_CONTROL_REQ data. 913 * To be more memory efficient, we could use 914 * dupb() for the M_CTL and copyb() for the M_PROTO 915 * as the M_CTL, since the M_CTL is the same for 916 * every SA entry passed down to IP for the same ill. 917 * 918 * Note that copymsg/copyb ensure that the new mblk 919 * is at least as large as the source mblk even if it's 920 * not using all its storage -- therefore, nmp 921 * has trailing space for sadb_req_from_sa to add 922 * the SA-specific bits. 923 */ 924 mutex_enter(&walker->ipsa_lock); 925 if (ipsec_capab_match(ill, 926 ill->ill_phyint->phyint_ifindex, ill->ill_isv6, 927 walker, ns)) { 928 nmp = copymsg(mp); 929 if (nmp == NULL) { 930 IPSECHW_DEBUG(IPSECHW_SADB, 931 ("sadb_ill_df: alloc error\n")); 932 error = ENOMEM; 933 mutex_exit(&walker->ipsa_lock); 934 break; 935 } 936 if (sadb_req_from_sa(walker, nmp, is_inbound)) { 937 nmp->b_next = salist; 938 salist = nmp; 939 } 940 } 941 mutex_exit(&walker->ipsa_lock); 942 } 943 mutex_exit(&fanout[i].isaf_lock); 944 while (salist != NULL) { 945 nmp = salist; 946 salist = nmp->b_next; 947 nmp->b_next = NULL; 948 ill_dlpi_send(ill, nmp); 949 } 950 if (error != 0) 951 break; /* out of for loop. */ 952 } 953 } 954 955 /* 956 * Called by ill_ipsec_capab_add(). Sends a copy of the SADB of 957 * the type specified by sa_type to the specified ill. 958 * 959 * We call for each fanout table defined by the SADB (one per 960 * protocol). sadb_ill_df() finally calls ill_dlpi_send() for 961 * each SADB entry in order to send a corresponding DL_CONTROL_REQ 962 * message to the ill. 963 */ 964 void 965 sadb_ill_download(ill_t *ill, uint_t sa_type) 966 { 967 mblk_t *protomp; /* prototype message */ 968 dl_control_req_t *ctrl; 969 sadbp_t *spp; 970 sadb_t *sp; 971 int dlt; 972 ip_stack_t *ipst = ill->ill_ipst; 973 netstack_t *ns = ipst->ips_netstack; 974 975 ASSERT(sa_type == SADB_SATYPE_AH || sa_type == SADB_SATYPE_ESP); 976 977 /* 978 * Allocate and initialize prototype answer. A duplicate for 979 * each SA is sent down to the interface. 980 */ 981 982 /* DL_CONTROL_REQ M_PROTO mblk_t */ 983 protomp = allocb(sizeof (dl_control_req_t) + 984 sizeof (dl_ct_ipsec_key_t) + sizeof (dl_ct_ipsec_t), BPRI_HI); 985 if (protomp == NULL) 986 return; 987 protomp->b_datap->db_type = M_PROTO; 988 989 dlt = (sa_type == SADB_SATYPE_AH) ? DL_CT_IPSEC_AH : DL_CT_IPSEC_ESP; 990 if (sa_type == SADB_SATYPE_ESP) { 991 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 992 993 spp = &espstack->esp_sadb; 994 } else { 995 ipsecah_stack_t *ahstack = ns->netstack_ipsecah; 996 997 spp = &ahstack->ah_sadb; 998 } 999 1000 ctrl = (dl_control_req_t *)protomp->b_wptr; 1001 ctrl->dl_primitive = DL_CONTROL_REQ; 1002 ctrl->dl_operation = DL_CO_SET; 1003 ctrl->dl_type = dlt; 1004 ctrl->dl_key_offset = sizeof (dl_control_req_t); 1005 ctrl->dl_key_length = sizeof (dl_ct_ipsec_key_t); 1006 ctrl->dl_data_offset = sizeof (dl_control_req_t) + 1007 sizeof (dl_ct_ipsec_key_t); 1008 ctrl->dl_data_length = sizeof (dl_ct_ipsec_t); 1009 protomp->b_wptr += sizeof (dl_control_req_t); 1010 1011 /* 1012 * then for each SADB entry, we fill out the dl_ct_ipsec_key_t 1013 * and dl_ct_ipsec_t 1014 */ 1015 sp = ill->ill_isv6 ? &(spp->s_v6) : &(spp->s_v4); 1016 sadb_ill_df(ill, protomp, sp->sdb_of, sp->sdb_hashsize, B_FALSE); 1017 sadb_ill_df(ill, protomp, sp->sdb_if, sp->sdb_hashsize, B_TRUE); 1018 freemsg(protomp); 1019 } 1020 1021 /* 1022 * Call me to free up a security association fanout. Use the forever 1023 * variable to indicate freeing up the SAs (forever == B_FALSE, e.g. 1024 * an SADB_FLUSH message), or destroying everything (forever == B_TRUE, 1025 * when a module is unloaded). 1026 */ 1027 static void 1028 sadb_destroyer(isaf_t **tablep, uint_t numentries, boolean_t forever, 1029 boolean_t inbound) 1030 { 1031 int i; 1032 isaf_t *table = *tablep; 1033 uint8_t protocol; 1034 ipsa_t *sa; 1035 netstackid_t sid; 1036 1037 if (table == NULL) 1038 return; 1039 1040 for (i = 0; i < numentries; i++) { 1041 mutex_enter(&table[i].isaf_lock); 1042 while ((sa = table[i].isaf_ipsa) != NULL) { 1043 if (inbound && cl_inet_deletespi && 1044 (sa->ipsa_state != IPSA_STATE_ACTIVE_ELSEWHERE) && 1045 (sa->ipsa_state != IPSA_STATE_IDLE)) { 1046 protocol = (sa->ipsa_type == SADB_SATYPE_AH) ? 1047 IPPROTO_AH : IPPROTO_ESP; 1048 sid = sa->ipsa_netstack->netstack_stackid; 1049 cl_inet_deletespi(sid, protocol, sa->ipsa_spi, 1050 NULL); 1051 } 1052 sadb_unlinkassoc(sa); 1053 } 1054 table[i].isaf_gen++; 1055 mutex_exit(&table[i].isaf_lock); 1056 if (forever) 1057 mutex_destroy(&(table[i].isaf_lock)); 1058 } 1059 1060 if (forever) { 1061 *tablep = NULL; 1062 kmem_free(table, numentries * sizeof (*table)); 1063 } 1064 } 1065 1066 /* 1067 * Entry points to sadb_destroyer(). 1068 */ 1069 static void 1070 sadb_flush(sadb_t *sp, netstack_t *ns) 1071 { 1072 /* 1073 * Flush out each bucket, one at a time. Were it not for keysock's 1074 * enforcement, there would be a subtlety where I could add on the 1075 * heels of a flush. With keysock's enforcement, however, this 1076 * makes ESP's job easy. 1077 */ 1078 sadb_destroyer(&sp->sdb_of, sp->sdb_hashsize, B_FALSE, B_FALSE); 1079 sadb_destroyer(&sp->sdb_if, sp->sdb_hashsize, B_FALSE, B_TRUE); 1080 1081 /* For each acquire, destroy it; leave the bucket mutex alone. */ 1082 sadb_destroy_acqlist(&sp->sdb_acq, sp->sdb_hashsize, B_FALSE, ns); 1083 } 1084 1085 static void 1086 sadb_destroy(sadb_t *sp, netstack_t *ns) 1087 { 1088 sadb_destroyer(&sp->sdb_of, sp->sdb_hashsize, B_TRUE, B_FALSE); 1089 sadb_destroyer(&sp->sdb_if, sp->sdb_hashsize, B_TRUE, B_TRUE); 1090 1091 /* For each acquire, destroy it, including the bucket mutex. */ 1092 sadb_destroy_acqlist(&sp->sdb_acq, sp->sdb_hashsize, B_TRUE, ns); 1093 1094 ASSERT(sp->sdb_of == NULL); 1095 ASSERT(sp->sdb_if == NULL); 1096 ASSERT(sp->sdb_acq == NULL); 1097 } 1098 1099 static void 1100 sadb_send_flush_req(sadbp_t *spp) 1101 { 1102 mblk_t *ctl_mp; 1103 1104 /* 1105 * we've been unplumbed, or never were plumbed; don't go there. 1106 */ 1107 if (spp->s_ip_q == NULL) 1108 return; 1109 1110 /* have IP send a flush msg to the IPsec accelerators */ 1111 ctl_mp = sadb_fmt_sa_req(DL_CO_FLUSH, spp->s_satype, NULL, B_TRUE); 1112 if (ctl_mp != NULL) 1113 putnext(spp->s_ip_q, ctl_mp); 1114 } 1115 1116 void 1117 sadbp_flush(sadbp_t *spp, netstack_t *ns) 1118 { 1119 sadb_flush(&spp->s_v4, ns); 1120 sadb_flush(&spp->s_v6, ns); 1121 1122 sadb_send_flush_req(spp); 1123 } 1124 1125 void 1126 sadbp_destroy(sadbp_t *spp, netstack_t *ns) 1127 { 1128 sadb_destroy(&spp->s_v4, ns); 1129 sadb_destroy(&spp->s_v6, ns); 1130 1131 sadb_send_flush_req(spp); 1132 if (spp->s_satype == SADB_SATYPE_AH) { 1133 ipsec_stack_t *ipss = ns->netstack_ipsec; 1134 1135 ip_drop_unregister(&ipss->ipsec_sadb_dropper); 1136 } 1137 } 1138 1139 1140 /* 1141 * Check hard vs. soft lifetimes. If there's a reality mismatch (e.g. 1142 * soft lifetimes > hard lifetimes) return an appropriate diagnostic for 1143 * EINVAL. 1144 */ 1145 int 1146 sadb_hardsoftchk(sadb_lifetime_t *hard, sadb_lifetime_t *soft, 1147 sadb_lifetime_t *idle) 1148 { 1149 if (hard == NULL || soft == NULL) 1150 return (0); 1151 1152 if (hard->sadb_lifetime_allocations != 0 && 1153 soft->sadb_lifetime_allocations != 0 && 1154 hard->sadb_lifetime_allocations < soft->sadb_lifetime_allocations) 1155 return (SADB_X_DIAGNOSTIC_ALLOC_HSERR); 1156 1157 if (hard->sadb_lifetime_bytes != 0 && 1158 soft->sadb_lifetime_bytes != 0 && 1159 hard->sadb_lifetime_bytes < soft->sadb_lifetime_bytes) 1160 return (SADB_X_DIAGNOSTIC_BYTES_HSERR); 1161 1162 if (hard->sadb_lifetime_addtime != 0 && 1163 soft->sadb_lifetime_addtime != 0 && 1164 hard->sadb_lifetime_addtime < soft->sadb_lifetime_addtime) 1165 return (SADB_X_DIAGNOSTIC_ADDTIME_HSERR); 1166 1167 if (hard->sadb_lifetime_usetime != 0 && 1168 soft->sadb_lifetime_usetime != 0 && 1169 hard->sadb_lifetime_usetime < soft->sadb_lifetime_usetime) 1170 return (SADB_X_DIAGNOSTIC_USETIME_HSERR); 1171 1172 if (idle != NULL) { 1173 if (hard->sadb_lifetime_addtime != 0 && 1174 idle->sadb_lifetime_addtime != 0 && 1175 hard->sadb_lifetime_addtime < idle->sadb_lifetime_addtime) 1176 return (SADB_X_DIAGNOSTIC_ADDTIME_HSERR); 1177 1178 if (soft->sadb_lifetime_addtime != 0 && 1179 idle->sadb_lifetime_addtime != 0 && 1180 soft->sadb_lifetime_addtime < idle->sadb_lifetime_addtime) 1181 return (SADB_X_DIAGNOSTIC_ADDTIME_HSERR); 1182 1183 if (hard->sadb_lifetime_usetime != 0 && 1184 idle->sadb_lifetime_usetime != 0 && 1185 hard->sadb_lifetime_usetime < idle->sadb_lifetime_usetime) 1186 return (SADB_X_DIAGNOSTIC_USETIME_HSERR); 1187 1188 if (soft->sadb_lifetime_usetime != 0 && 1189 idle->sadb_lifetime_usetime != 0 && 1190 soft->sadb_lifetime_usetime < idle->sadb_lifetime_usetime) 1191 return (SADB_X_DIAGNOSTIC_USETIME_HSERR); 1192 } 1193 1194 return (0); 1195 } 1196 1197 /* 1198 * Clone a security association for the purposes of inserting a single SA 1199 * into inbound and outbound tables respectively. This function should only 1200 * be called from sadb_common_add(). 1201 */ 1202 static ipsa_t * 1203 sadb_cloneassoc(ipsa_t *ipsa) 1204 { 1205 ipsa_t *newbie; 1206 boolean_t error = B_FALSE; 1207 1208 ASSERT(MUTEX_NOT_HELD(&(ipsa->ipsa_lock))); 1209 1210 newbie = kmem_alloc(sizeof (ipsa_t), KM_NOSLEEP); 1211 if (newbie == NULL) 1212 return (NULL); 1213 1214 /* Copy over what we can. */ 1215 *newbie = *ipsa; 1216 1217 /* bzero and initialize locks, in case *_init() allocates... */ 1218 mutex_init(&newbie->ipsa_lock, NULL, MUTEX_DEFAULT, NULL); 1219 1220 /* 1221 * While somewhat dain-bramaged, the most graceful way to 1222 * recover from errors is to keep plowing through the 1223 * allocations, and getting what I can. It's easier to call 1224 * sadb_freeassoc() on the stillborn clone when all the 1225 * pointers aren't pointing to the parent's data. 1226 */ 1227 1228 if (ipsa->ipsa_authkey != NULL) { 1229 newbie->ipsa_authkey = kmem_alloc(newbie->ipsa_authkeylen, 1230 KM_NOSLEEP); 1231 if (newbie->ipsa_authkey == NULL) { 1232 error = B_TRUE; 1233 } else { 1234 bcopy(ipsa->ipsa_authkey, newbie->ipsa_authkey, 1235 newbie->ipsa_authkeylen); 1236 1237 newbie->ipsa_kcfauthkey.ck_data = 1238 newbie->ipsa_authkey; 1239 } 1240 1241 if (newbie->ipsa_amech.cm_param != NULL) { 1242 newbie->ipsa_amech.cm_param = 1243 (char *)&newbie->ipsa_mac_len; 1244 } 1245 } 1246 1247 if (ipsa->ipsa_encrkey != NULL) { 1248 newbie->ipsa_encrkey = kmem_alloc(newbie->ipsa_encrkeylen, 1249 KM_NOSLEEP); 1250 if (newbie->ipsa_encrkey == NULL) { 1251 error = B_TRUE; 1252 } else { 1253 bcopy(ipsa->ipsa_encrkey, newbie->ipsa_encrkey, 1254 newbie->ipsa_encrkeylen); 1255 1256 newbie->ipsa_kcfencrkey.ck_data = 1257 newbie->ipsa_encrkey; 1258 } 1259 } 1260 1261 newbie->ipsa_authtmpl = NULL; 1262 newbie->ipsa_encrtmpl = NULL; 1263 newbie->ipsa_haspeer = B_TRUE; 1264 1265 if (ipsa->ipsa_integ != NULL) { 1266 newbie->ipsa_integ = kmem_alloc(newbie->ipsa_integlen, 1267 KM_NOSLEEP); 1268 if (newbie->ipsa_integ == NULL) { 1269 error = B_TRUE; 1270 } else { 1271 bcopy(ipsa->ipsa_integ, newbie->ipsa_integ, 1272 newbie->ipsa_integlen); 1273 } 1274 } 1275 1276 if (ipsa->ipsa_sens != NULL) { 1277 newbie->ipsa_sens = kmem_alloc(newbie->ipsa_senslen, 1278 KM_NOSLEEP); 1279 if (newbie->ipsa_sens == NULL) { 1280 error = B_TRUE; 1281 } else { 1282 bcopy(ipsa->ipsa_sens, newbie->ipsa_sens, 1283 newbie->ipsa_senslen); 1284 } 1285 } 1286 1287 if (ipsa->ipsa_src_cid != NULL) { 1288 newbie->ipsa_src_cid = ipsa->ipsa_src_cid; 1289 IPSID_REFHOLD(ipsa->ipsa_src_cid); 1290 } 1291 1292 if (ipsa->ipsa_dst_cid != NULL) { 1293 newbie->ipsa_dst_cid = ipsa->ipsa_dst_cid; 1294 IPSID_REFHOLD(ipsa->ipsa_dst_cid); 1295 } 1296 1297 if (error) { 1298 sadb_freeassoc(newbie); 1299 return (NULL); 1300 } 1301 1302 return (newbie); 1303 } 1304 1305 /* 1306 * Initialize a SADB address extension at the address specified by addrext. 1307 * Return a pointer to the end of the new address extension. 1308 */ 1309 static uint8_t * 1310 sadb_make_addr_ext(uint8_t *start, uint8_t *end, uint16_t exttype, 1311 sa_family_t af, uint32_t *addr, uint16_t port, uint8_t proto, int prefix) 1312 { 1313 struct sockaddr_in *sin; 1314 struct sockaddr_in6 *sin6; 1315 uint8_t *cur = start; 1316 int addrext_len; 1317 int sin_len; 1318 sadb_address_t *addrext = (sadb_address_t *)cur; 1319 1320 if (cur == NULL) 1321 return (NULL); 1322 1323 cur += sizeof (*addrext); 1324 if (cur > end) 1325 return (NULL); 1326 1327 addrext->sadb_address_proto = proto; 1328 addrext->sadb_address_prefixlen = prefix; 1329 addrext->sadb_address_reserved = 0; 1330 addrext->sadb_address_exttype = exttype; 1331 1332 switch (af) { 1333 case AF_INET: 1334 sin = (struct sockaddr_in *)cur; 1335 sin_len = sizeof (*sin); 1336 cur += sin_len; 1337 if (cur > end) 1338 return (NULL); 1339 1340 sin->sin_family = af; 1341 bzero(sin->sin_zero, sizeof (sin->sin_zero)); 1342 sin->sin_port = port; 1343 IPSA_COPY_ADDR(&sin->sin_addr, addr, af); 1344 break; 1345 case AF_INET6: 1346 sin6 = (struct sockaddr_in6 *)cur; 1347 sin_len = sizeof (*sin6); 1348 cur += sin_len; 1349 if (cur > end) 1350 return (NULL); 1351 1352 bzero(sin6, sizeof (*sin6)); 1353 sin6->sin6_family = af; 1354 sin6->sin6_port = port; 1355 IPSA_COPY_ADDR(&sin6->sin6_addr, addr, af); 1356 break; 1357 } 1358 1359 addrext_len = roundup(cur - start, sizeof (uint64_t)); 1360 addrext->sadb_address_len = SADB_8TO64(addrext_len); 1361 1362 cur = start + addrext_len; 1363 if (cur > end) 1364 cur = NULL; 1365 1366 return (cur); 1367 } 1368 1369 /* 1370 * Construct a key management cookie extension. 1371 */ 1372 1373 static uint8_t * 1374 sadb_make_kmc_ext(uint8_t *cur, uint8_t *end, uint32_t kmp, uint32_t kmc) 1375 { 1376 sadb_x_kmc_t *kmcext = (sadb_x_kmc_t *)cur; 1377 1378 if (cur == NULL) 1379 return (NULL); 1380 1381 cur += sizeof (*kmcext); 1382 1383 if (cur > end) 1384 return (NULL); 1385 1386 kmcext->sadb_x_kmc_len = SADB_8TO64(sizeof (*kmcext)); 1387 kmcext->sadb_x_kmc_exttype = SADB_X_EXT_KM_COOKIE; 1388 kmcext->sadb_x_kmc_proto = kmp; 1389 kmcext->sadb_x_kmc_cookie = kmc; 1390 kmcext->sadb_x_kmc_reserved = 0; 1391 1392 return (cur); 1393 } 1394 1395 /* 1396 * Given an original message header with sufficient space following it, and an 1397 * SA, construct a full PF_KEY message with all of the relevant extensions. 1398 * This is mostly used for SADB_GET, and SADB_DUMP. 1399 */ 1400 static mblk_t * 1401 sadb_sa2msg(ipsa_t *ipsa, sadb_msg_t *samsg) 1402 { 1403 int alloclen, addrsize, paddrsize, authsize, encrsize; 1404 int srcidsize, dstidsize; 1405 sa_family_t fam, pfam; /* Address family for SADB_EXT_ADDRESS */ 1406 /* src/dst and proxy sockaddrs. */ 1407 /* 1408 * The following are pointers into the PF_KEY message this PF_KEY 1409 * message creates. 1410 */ 1411 sadb_msg_t *newsamsg; 1412 sadb_sa_t *assoc; 1413 sadb_lifetime_t *lt; 1414 sadb_key_t *key; 1415 sadb_ident_t *ident; 1416 sadb_sens_t *sens; 1417 sadb_ext_t *walker; /* For when we need a generic ext. pointer. */ 1418 sadb_x_replay_ctr_t *repl_ctr; 1419 sadb_x_pair_t *pair_ext; 1420 1421 mblk_t *mp; 1422 uint64_t *bitmap; 1423 uint8_t *cur, *end; 1424 /* These indicate the presence of the above extension fields. */ 1425 boolean_t soft, hard, isrc, idst, auth, encr, sensinteg, srcid, dstid; 1426 boolean_t idle; 1427 boolean_t paired; 1428 uint32_t otherspi; 1429 1430 /* First off, figure out the allocation length for this message. */ 1431 1432 /* 1433 * Constant stuff. This includes base, SA, address (src, dst), 1434 * and lifetime (current). 1435 */ 1436 alloclen = sizeof (sadb_msg_t) + sizeof (sadb_sa_t) + 1437 sizeof (sadb_lifetime_t); 1438 1439 fam = ipsa->ipsa_addrfam; 1440 switch (fam) { 1441 case AF_INET: 1442 addrsize = roundup(sizeof (struct sockaddr_in) + 1443 sizeof (sadb_address_t), sizeof (uint64_t)); 1444 break; 1445 case AF_INET6: 1446 addrsize = roundup(sizeof (struct sockaddr_in6) + 1447 sizeof (sadb_address_t), sizeof (uint64_t)); 1448 break; 1449 default: 1450 return (NULL); 1451 } 1452 /* 1453 * Allocate TWO address extensions, for source and destination. 1454 * (Thus, the * 2.) 1455 */ 1456 alloclen += addrsize * 2; 1457 if (ipsa->ipsa_flags & IPSA_F_NATT_REM) 1458 alloclen += addrsize; 1459 if (ipsa->ipsa_flags & IPSA_F_NATT_LOC) 1460 alloclen += addrsize; 1461 1462 if (ipsa->ipsa_flags & IPSA_F_PAIRED) { 1463 paired = B_TRUE; 1464 alloclen += sizeof (sadb_x_pair_t); 1465 otherspi = ipsa->ipsa_otherspi; 1466 } else { 1467 paired = B_FALSE; 1468 } 1469 1470 /* How 'bout other lifetimes? */ 1471 if (ipsa->ipsa_softaddlt != 0 || ipsa->ipsa_softuselt != 0 || 1472 ipsa->ipsa_softbyteslt != 0 || ipsa->ipsa_softalloc != 0) { 1473 alloclen += sizeof (sadb_lifetime_t); 1474 soft = B_TRUE; 1475 } else { 1476 soft = B_FALSE; 1477 } 1478 1479 if (ipsa->ipsa_hardaddlt != 0 || ipsa->ipsa_harduselt != 0 || 1480 ipsa->ipsa_hardbyteslt != 0 || ipsa->ipsa_hardalloc != 0) { 1481 alloclen += sizeof (sadb_lifetime_t); 1482 hard = B_TRUE; 1483 } else { 1484 hard = B_FALSE; 1485 } 1486 1487 if (ipsa->ipsa_idleaddlt != 0 || ipsa->ipsa_idleuselt != 0) { 1488 alloclen += sizeof (sadb_lifetime_t); 1489 idle = B_TRUE; 1490 } else { 1491 idle = B_FALSE; 1492 } 1493 1494 /* Inner addresses. */ 1495 if (ipsa->ipsa_innerfam == 0) { 1496 isrc = B_FALSE; 1497 idst = B_FALSE; 1498 } else { 1499 pfam = ipsa->ipsa_innerfam; 1500 switch (pfam) { 1501 case AF_INET6: 1502 paddrsize = roundup(sizeof (struct sockaddr_in6) + 1503 sizeof (sadb_address_t), sizeof (uint64_t)); 1504 break; 1505 case AF_INET: 1506 paddrsize = roundup(sizeof (struct sockaddr_in) + 1507 sizeof (sadb_address_t), sizeof (uint64_t)); 1508 break; 1509 default: 1510 cmn_err(CE_PANIC, 1511 "IPsec SADB: Proxy length failure.\n"); 1512 break; 1513 } 1514 isrc = B_TRUE; 1515 idst = B_TRUE; 1516 alloclen += 2 * paddrsize; 1517 } 1518 1519 /* For the following fields, assume that length != 0 ==> stuff */ 1520 if (ipsa->ipsa_authkeylen != 0) { 1521 authsize = roundup(sizeof (sadb_key_t) + ipsa->ipsa_authkeylen, 1522 sizeof (uint64_t)); 1523 alloclen += authsize; 1524 auth = B_TRUE; 1525 } else { 1526 auth = B_FALSE; 1527 } 1528 1529 if (ipsa->ipsa_encrkeylen != 0) { 1530 encrsize = roundup(sizeof (sadb_key_t) + ipsa->ipsa_encrkeylen, 1531 sizeof (uint64_t)); 1532 alloclen += encrsize; 1533 encr = B_TRUE; 1534 } else { 1535 encr = B_FALSE; 1536 } 1537 1538 /* No need for roundup on sens and integ. */ 1539 if (ipsa->ipsa_integlen != 0 || ipsa->ipsa_senslen != 0) { 1540 alloclen += sizeof (sadb_key_t) + ipsa->ipsa_integlen + 1541 ipsa->ipsa_senslen; 1542 sensinteg = B_TRUE; 1543 } else { 1544 sensinteg = B_FALSE; 1545 } 1546 1547 /* 1548 * Must use strlen() here for lengths. Identities use NULL 1549 * pointers to indicate their nonexistence. 1550 */ 1551 if (ipsa->ipsa_src_cid != NULL) { 1552 srcidsize = roundup(sizeof (sadb_ident_t) + 1553 strlen(ipsa->ipsa_src_cid->ipsid_cid) + 1, 1554 sizeof (uint64_t)); 1555 alloclen += srcidsize; 1556 srcid = B_TRUE; 1557 } else { 1558 srcid = B_FALSE; 1559 } 1560 1561 if (ipsa->ipsa_dst_cid != NULL) { 1562 dstidsize = roundup(sizeof (sadb_ident_t) + 1563 strlen(ipsa->ipsa_dst_cid->ipsid_cid) + 1, 1564 sizeof (uint64_t)); 1565 alloclen += dstidsize; 1566 dstid = B_TRUE; 1567 } else { 1568 dstid = B_FALSE; 1569 } 1570 1571 if ((ipsa->ipsa_kmp != 0) || (ipsa->ipsa_kmc != 0)) 1572 alloclen += sizeof (sadb_x_kmc_t); 1573 1574 if (ipsa->ipsa_replay != 0) { 1575 alloclen += sizeof (sadb_x_replay_ctr_t); 1576 } 1577 1578 /* Make sure the allocation length is a multiple of 8 bytes. */ 1579 ASSERT((alloclen & 0x7) == 0); 1580 1581 /* XXX Possibly make it esballoc, with a bzero-ing free_ftn. */ 1582 mp = allocb(alloclen, BPRI_HI); 1583 if (mp == NULL) 1584 return (NULL); 1585 1586 mp->b_wptr += alloclen; 1587 end = mp->b_wptr; 1588 newsamsg = (sadb_msg_t *)mp->b_rptr; 1589 *newsamsg = *samsg; 1590 newsamsg->sadb_msg_len = (uint16_t)SADB_8TO64(alloclen); 1591 1592 mutex_enter(&ipsa->ipsa_lock); /* Since I'm grabbing SA fields... */ 1593 1594 newsamsg->sadb_msg_satype = ipsa->ipsa_type; 1595 1596 assoc = (sadb_sa_t *)(newsamsg + 1); 1597 assoc->sadb_sa_len = SADB_8TO64(sizeof (*assoc)); 1598 assoc->sadb_sa_exttype = SADB_EXT_SA; 1599 assoc->sadb_sa_spi = ipsa->ipsa_spi; 1600 assoc->sadb_sa_replay = ipsa->ipsa_replay_wsize; 1601 assoc->sadb_sa_state = ipsa->ipsa_state; 1602 assoc->sadb_sa_auth = ipsa->ipsa_auth_alg; 1603 assoc->sadb_sa_encrypt = ipsa->ipsa_encr_alg; 1604 assoc->sadb_sa_flags = ipsa->ipsa_flags; 1605 1606 lt = (sadb_lifetime_t *)(assoc + 1); 1607 lt->sadb_lifetime_len = SADB_8TO64(sizeof (*lt)); 1608 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; 1609 /* We do not support the concept. */ 1610 lt->sadb_lifetime_allocations = 0; 1611 lt->sadb_lifetime_bytes = ipsa->ipsa_bytes; 1612 lt->sadb_lifetime_addtime = ipsa->ipsa_addtime; 1613 lt->sadb_lifetime_usetime = ipsa->ipsa_usetime; 1614 1615 if (hard) { 1616 lt++; 1617 lt->sadb_lifetime_len = SADB_8TO64(sizeof (*lt)); 1618 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD; 1619 lt->sadb_lifetime_allocations = ipsa->ipsa_hardalloc; 1620 lt->sadb_lifetime_bytes = ipsa->ipsa_hardbyteslt; 1621 lt->sadb_lifetime_addtime = ipsa->ipsa_hardaddlt; 1622 lt->sadb_lifetime_usetime = ipsa->ipsa_harduselt; 1623 } 1624 1625 if (soft) { 1626 lt++; 1627 lt->sadb_lifetime_len = SADB_8TO64(sizeof (*lt)); 1628 lt->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT; 1629 lt->sadb_lifetime_allocations = ipsa->ipsa_softalloc; 1630 lt->sadb_lifetime_bytes = ipsa->ipsa_softbyteslt; 1631 lt->sadb_lifetime_addtime = ipsa->ipsa_softaddlt; 1632 lt->sadb_lifetime_usetime = ipsa->ipsa_softuselt; 1633 } 1634 1635 if (idle) { 1636 lt++; 1637 lt->sadb_lifetime_len = SADB_8TO64(sizeof (*lt)); 1638 lt->sadb_lifetime_exttype = SADB_X_EXT_LIFETIME_IDLE; 1639 lt->sadb_lifetime_addtime = ipsa->ipsa_idleaddlt; 1640 lt->sadb_lifetime_usetime = ipsa->ipsa_idleuselt; 1641 } 1642 1643 cur = (uint8_t *)(lt + 1); 1644 1645 /* NOTE: Don't fill in ports here if we are a tunnel-mode SA. */ 1646 cur = sadb_make_addr_ext(cur, end, SADB_EXT_ADDRESS_SRC, fam, 1647 ipsa->ipsa_srcaddr, (!isrc && !idst) ? SA_SRCPORT(ipsa) : 0, 1648 SA_PROTO(ipsa), 0); 1649 if (cur == NULL) { 1650 freemsg(mp); 1651 mp = NULL; 1652 goto bail; 1653 } 1654 1655 cur = sadb_make_addr_ext(cur, end, SADB_EXT_ADDRESS_DST, fam, 1656 ipsa->ipsa_dstaddr, (!isrc && !idst) ? SA_DSTPORT(ipsa) : 0, 1657 SA_PROTO(ipsa), 0); 1658 if (cur == NULL) { 1659 freemsg(mp); 1660 mp = NULL; 1661 goto bail; 1662 } 1663 1664 if (ipsa->ipsa_flags & IPSA_F_NATT_LOC) { 1665 cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_NATT_LOC, 1666 fam, &ipsa->ipsa_natt_addr_loc, ipsa->ipsa_local_nat_port, 1667 IPPROTO_UDP, 0); 1668 if (cur == NULL) { 1669 freemsg(mp); 1670 mp = NULL; 1671 goto bail; 1672 } 1673 } 1674 1675 if (ipsa->ipsa_flags & IPSA_F_NATT_REM) { 1676 cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_NATT_REM, 1677 fam, &ipsa->ipsa_natt_addr_rem, ipsa->ipsa_remote_nat_port, 1678 IPPROTO_UDP, 0); 1679 if (cur == NULL) { 1680 freemsg(mp); 1681 mp = NULL; 1682 goto bail; 1683 } 1684 } 1685 1686 /* If we are a tunnel-mode SA, fill in the inner-selectors. */ 1687 if (isrc) { 1688 cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_INNER_SRC, 1689 pfam, ipsa->ipsa_innersrc, SA_SRCPORT(ipsa), 1690 SA_IPROTO(ipsa), ipsa->ipsa_innersrcpfx); 1691 if (cur == NULL) { 1692 freemsg(mp); 1693 mp = NULL; 1694 goto bail; 1695 } 1696 } 1697 1698 if (idst) { 1699 cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_INNER_DST, 1700 pfam, ipsa->ipsa_innerdst, SA_DSTPORT(ipsa), 1701 SA_IPROTO(ipsa), ipsa->ipsa_innerdstpfx); 1702 if (cur == NULL) { 1703 freemsg(mp); 1704 mp = NULL; 1705 goto bail; 1706 } 1707 } 1708 1709 if ((ipsa->ipsa_kmp != 0) || (ipsa->ipsa_kmc != 0)) { 1710 cur = sadb_make_kmc_ext(cur, end, 1711 ipsa->ipsa_kmp, ipsa->ipsa_kmc); 1712 if (cur == NULL) { 1713 freemsg(mp); 1714 mp = NULL; 1715 goto bail; 1716 } 1717 } 1718 1719 walker = (sadb_ext_t *)cur; 1720 if (auth) { 1721 key = (sadb_key_t *)walker; 1722 key->sadb_key_len = SADB_8TO64(authsize); 1723 key->sadb_key_exttype = SADB_EXT_KEY_AUTH; 1724 key->sadb_key_bits = ipsa->ipsa_authkeybits; 1725 key->sadb_key_reserved = 0; 1726 bcopy(ipsa->ipsa_authkey, key + 1, ipsa->ipsa_authkeylen); 1727 walker = (sadb_ext_t *)((uint64_t *)walker + 1728 walker->sadb_ext_len); 1729 } 1730 1731 if (encr) { 1732 key = (sadb_key_t *)walker; 1733 key->sadb_key_len = SADB_8TO64(encrsize); 1734 key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT; 1735 key->sadb_key_bits = ipsa->ipsa_encrkeybits; 1736 key->sadb_key_reserved = 0; 1737 bcopy(ipsa->ipsa_encrkey, key + 1, ipsa->ipsa_encrkeylen); 1738 walker = (sadb_ext_t *)((uint64_t *)walker + 1739 walker->sadb_ext_len); 1740 } 1741 1742 if (srcid) { 1743 ident = (sadb_ident_t *)walker; 1744 ident->sadb_ident_len = SADB_8TO64(srcidsize); 1745 ident->sadb_ident_exttype = SADB_EXT_IDENTITY_SRC; 1746 ident->sadb_ident_type = ipsa->ipsa_src_cid->ipsid_type; 1747 ident->sadb_ident_id = 0; 1748 ident->sadb_ident_reserved = 0; 1749 (void) strcpy((char *)(ident + 1), 1750 ipsa->ipsa_src_cid->ipsid_cid); 1751 walker = (sadb_ext_t *)((uint64_t *)walker + 1752 walker->sadb_ext_len); 1753 } 1754 1755 if (dstid) { 1756 ident = (sadb_ident_t *)walker; 1757 ident->sadb_ident_len = SADB_8TO64(dstidsize); 1758 ident->sadb_ident_exttype = SADB_EXT_IDENTITY_DST; 1759 ident->sadb_ident_type = ipsa->ipsa_dst_cid->ipsid_type; 1760 ident->sadb_ident_id = 0; 1761 ident->sadb_ident_reserved = 0; 1762 (void) strcpy((char *)(ident + 1), 1763 ipsa->ipsa_dst_cid->ipsid_cid); 1764 walker = (sadb_ext_t *)((uint64_t *)walker + 1765 walker->sadb_ext_len); 1766 } 1767 1768 if (sensinteg) { 1769 sens = (sadb_sens_t *)walker; 1770 sens->sadb_sens_len = SADB_8TO64(sizeof (sadb_sens_t *) + 1771 ipsa->ipsa_senslen + ipsa->ipsa_integlen); 1772 sens->sadb_sens_dpd = ipsa->ipsa_dpd; 1773 sens->sadb_sens_sens_level = ipsa->ipsa_senslevel; 1774 sens->sadb_sens_integ_level = ipsa->ipsa_integlevel; 1775 sens->sadb_sens_sens_len = SADB_8TO64(ipsa->ipsa_senslen); 1776 sens->sadb_sens_integ_len = SADB_8TO64(ipsa->ipsa_integlen); 1777 sens->sadb_sens_reserved = 0; 1778 bitmap = (uint64_t *)(sens + 1); 1779 if (ipsa->ipsa_sens != NULL) { 1780 bcopy(ipsa->ipsa_sens, bitmap, ipsa->ipsa_senslen); 1781 bitmap += sens->sadb_sens_sens_len; 1782 } 1783 if (ipsa->ipsa_integ != NULL) 1784 bcopy(ipsa->ipsa_integ, bitmap, ipsa->ipsa_integlen); 1785 walker = (sadb_ext_t *)((uint64_t *)walker + 1786 walker->sadb_ext_len); 1787 } 1788 1789 if (paired) { 1790 pair_ext = (sadb_x_pair_t *)walker; 1791 1792 pair_ext->sadb_x_pair_len = SADB_8TO64(sizeof (sadb_x_pair_t)); 1793 pair_ext->sadb_x_pair_exttype = SADB_X_EXT_PAIR; 1794 pair_ext->sadb_x_pair_spi = otherspi; 1795 1796 walker = (sadb_ext_t *)((uint64_t *)walker + 1797 walker->sadb_ext_len); 1798 } 1799 1800 if (ipsa->ipsa_replay != 0) { 1801 repl_ctr = (sadb_x_replay_ctr_t *)walker; 1802 repl_ctr->sadb_x_rc_len = SADB_8TO64(sizeof (*repl_ctr)); 1803 repl_ctr->sadb_x_rc_exttype = SADB_X_EXT_REPLAY_VALUE; 1804 repl_ctr->sadb_x_rc_replay32 = ipsa->ipsa_replay; 1805 repl_ctr->sadb_x_rc_replay64 = 0; 1806 walker = (sadb_ext_t *)(repl_ctr + 1); 1807 } 1808 1809 bail: 1810 /* Pardon any delays... */ 1811 mutex_exit(&ipsa->ipsa_lock); 1812 1813 return (mp); 1814 } 1815 1816 /* 1817 * Strip out key headers or unmarked headers (SADB_EXT_KEY_*, SADB_EXT_UNKNOWN) 1818 * and adjust base message accordingly. 1819 * 1820 * Assume message is pulled up in one piece of contiguous memory. 1821 * 1822 * Say if we start off with: 1823 * 1824 * +------+----+-------------+-----------+---------------+---------------+ 1825 * | base | SA | source addr | dest addr | rsrvd. or key | soft lifetime | 1826 * +------+----+-------------+-----------+---------------+---------------+ 1827 * 1828 * we will end up with 1829 * 1830 * +------+----+-------------+-----------+---------------+ 1831 * | base | SA | source addr | dest addr | soft lifetime | 1832 * +------+----+-------------+-----------+---------------+ 1833 */ 1834 static void 1835 sadb_strip(sadb_msg_t *samsg) 1836 { 1837 sadb_ext_t *ext; 1838 uint8_t *target = NULL; 1839 uint8_t *msgend; 1840 int sofar = SADB_8TO64(sizeof (*samsg)); 1841 int copylen; 1842 1843 ext = (sadb_ext_t *)(samsg + 1); 1844 msgend = (uint8_t *)samsg; 1845 msgend += SADB_64TO8(samsg->sadb_msg_len); 1846 while ((uint8_t *)ext < msgend) { 1847 if (ext->sadb_ext_type == SADB_EXT_RESERVED || 1848 ext->sadb_ext_type == SADB_EXT_KEY_AUTH || 1849 ext->sadb_ext_type == SADB_X_EXT_EDUMP || 1850 ext->sadb_ext_type == SADB_EXT_KEY_ENCRYPT) { 1851 /* 1852 * Aha! I found a header to be erased. 1853 */ 1854 1855 if (target != NULL) { 1856 /* 1857 * If I had a previous header to be erased, 1858 * copy over it. I can get away with just 1859 * copying backwards because the target will 1860 * always be 8 bytes behind the source. 1861 */ 1862 copylen = ((uint8_t *)ext) - (target + 1863 SADB_64TO8( 1864 ((sadb_ext_t *)target)->sadb_ext_len)); 1865 ovbcopy(((uint8_t *)ext - copylen), target, 1866 copylen); 1867 target += copylen; 1868 ((sadb_ext_t *)target)->sadb_ext_len = 1869 SADB_8TO64(((uint8_t *)ext) - target + 1870 SADB_64TO8(ext->sadb_ext_len)); 1871 } else { 1872 target = (uint8_t *)ext; 1873 } 1874 } else { 1875 sofar += ext->sadb_ext_len; 1876 } 1877 1878 ext = (sadb_ext_t *)(((uint64_t *)ext) + ext->sadb_ext_len); 1879 } 1880 1881 ASSERT((uint8_t *)ext == msgend); 1882 1883 if (target != NULL) { 1884 copylen = ((uint8_t *)ext) - (target + 1885 SADB_64TO8(((sadb_ext_t *)target)->sadb_ext_len)); 1886 if (copylen != 0) 1887 ovbcopy(((uint8_t *)ext - copylen), target, copylen); 1888 } 1889 1890 /* Adjust samsg. */ 1891 samsg->sadb_msg_len = (uint16_t)sofar; 1892 1893 /* Assume all of the rest is cleared by caller in sadb_pfkey_echo(). */ 1894 } 1895 1896 /* 1897 * AH needs to send an error to PF_KEY. Assume mp points to an M_CTL 1898 * followed by an M_DATA with a PF_KEY message in it. The serial of 1899 * the sending keysock instance is included. 1900 */ 1901 void 1902 sadb_pfkey_error(queue_t *pfkey_q, mblk_t *mp, int error, int diagnostic, 1903 uint_t serial) 1904 { 1905 mblk_t *msg = mp->b_cont; 1906 sadb_msg_t *samsg; 1907 keysock_out_t *kso; 1908 1909 /* 1910 * Enough functions call this to merit a NULL queue check. 1911 */ 1912 if (pfkey_q == NULL) { 1913 freemsg(mp); 1914 return; 1915 } 1916 1917 ASSERT(msg != NULL); 1918 ASSERT((mp->b_wptr - mp->b_rptr) == sizeof (ipsec_info_t)); 1919 ASSERT((msg->b_wptr - msg->b_rptr) >= sizeof (sadb_msg_t)); 1920 samsg = (sadb_msg_t *)msg->b_rptr; 1921 kso = (keysock_out_t *)mp->b_rptr; 1922 1923 kso->ks_out_type = KEYSOCK_OUT; 1924 kso->ks_out_len = sizeof (*kso); 1925 kso->ks_out_serial = serial; 1926 1927 /* 1928 * Only send the base message up in the event of an error. 1929 * Don't worry about bzero()-ing, because it was probably bogus 1930 * anyway. 1931 */ 1932 msg->b_wptr = msg->b_rptr + sizeof (*samsg); 1933 samsg = (sadb_msg_t *)msg->b_rptr; 1934 samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg)); 1935 samsg->sadb_msg_errno = (uint8_t)error; 1936 if (diagnostic != SADB_X_DIAGNOSTIC_PRESET) 1937 samsg->sadb_x_msg_diagnostic = (uint16_t)diagnostic; 1938 1939 putnext(pfkey_q, mp); 1940 } 1941 1942 /* 1943 * Send a successful return packet back to keysock via the queue in pfkey_q. 1944 * 1945 * Often, an SA is associated with the reply message, it's passed in if needed, 1946 * and NULL if not. BTW, that ipsa will have its refcnt appropriately held, 1947 * and the caller will release said refcnt. 1948 */ 1949 void 1950 sadb_pfkey_echo(queue_t *pfkey_q, mblk_t *mp, sadb_msg_t *samsg, 1951 keysock_in_t *ksi, ipsa_t *ipsa) 1952 { 1953 keysock_out_t *kso; 1954 mblk_t *mp1; 1955 sadb_msg_t *newsamsg; 1956 uint8_t *oldend; 1957 1958 ASSERT((mp->b_cont != NULL) && 1959 ((void *)samsg == (void *)mp->b_cont->b_rptr) && 1960 ((void *)mp->b_rptr == (void *)ksi)); 1961 1962 switch (samsg->sadb_msg_type) { 1963 case SADB_ADD: 1964 case SADB_UPDATE: 1965 case SADB_X_UPDATEPAIR: 1966 case SADB_X_DELPAIR_STATE: 1967 case SADB_FLUSH: 1968 case SADB_DUMP: 1969 /* 1970 * I have all of the message already. I just need to strip 1971 * out the keying material and echo the message back. 1972 * 1973 * NOTE: for SADB_DUMP, the function sadb_dump() did the 1974 * work. When DUMP reaches here, it should only be a base 1975 * message. 1976 */ 1977 justecho: 1978 if (ksi->ks_in_extv[SADB_EXT_KEY_AUTH] != NULL || 1979 ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT] != NULL || 1980 ksi->ks_in_extv[SADB_X_EXT_EDUMP] != NULL) { 1981 sadb_strip(samsg); 1982 /* Assume PF_KEY message is contiguous. */ 1983 ASSERT(mp->b_cont->b_cont == NULL); 1984 oldend = mp->b_cont->b_wptr; 1985 mp->b_cont->b_wptr = mp->b_cont->b_rptr + 1986 SADB_64TO8(samsg->sadb_msg_len); 1987 bzero(mp->b_cont->b_wptr, oldend - mp->b_cont->b_wptr); 1988 } 1989 break; 1990 case SADB_GET: 1991 /* 1992 * Do a lot of work here, because of the ipsa I just found. 1993 * First construct the new PF_KEY message, then abandon 1994 * the old one. 1995 */ 1996 mp1 = sadb_sa2msg(ipsa, samsg); 1997 if (mp1 == NULL) { 1998 sadb_pfkey_error(pfkey_q, mp, ENOMEM, 1999 SADB_X_DIAGNOSTIC_NONE, ksi->ks_in_serial); 2000 return; 2001 } 2002 freemsg(mp->b_cont); 2003 mp->b_cont = mp1; 2004 break; 2005 case SADB_DELETE: 2006 case SADB_X_DELPAIR: 2007 if (ipsa == NULL) 2008 goto justecho; 2009 /* 2010 * Because listening KMds may require more info, treat 2011 * DELETE like a special case of GET. 2012 */ 2013 mp1 = sadb_sa2msg(ipsa, samsg); 2014 if (mp1 == NULL) { 2015 sadb_pfkey_error(pfkey_q, mp, ENOMEM, 2016 SADB_X_DIAGNOSTIC_NONE, ksi->ks_in_serial); 2017 return; 2018 } 2019 newsamsg = (sadb_msg_t *)mp1->b_rptr; 2020 sadb_strip(newsamsg); 2021 oldend = mp1->b_wptr; 2022 mp1->b_wptr = mp1->b_rptr + SADB_64TO8(newsamsg->sadb_msg_len); 2023 bzero(mp1->b_wptr, oldend - mp1->b_wptr); 2024 freemsg(mp->b_cont); 2025 mp->b_cont = mp1; 2026 break; 2027 default: 2028 if (mp != NULL) 2029 freemsg(mp); 2030 return; 2031 } 2032 2033 /* ksi is now null and void. */ 2034 kso = (keysock_out_t *)ksi; 2035 kso->ks_out_type = KEYSOCK_OUT; 2036 kso->ks_out_len = sizeof (*kso); 2037 kso->ks_out_serial = ksi->ks_in_serial; 2038 /* We're ready to send... */ 2039 putnext(pfkey_q, mp); 2040 } 2041 2042 /* 2043 * Set up a global pfkey_q instance for AH, ESP, or some other consumer. 2044 */ 2045 void 2046 sadb_keysock_hello(queue_t **pfkey_qp, queue_t *q, mblk_t *mp, 2047 void (*ager)(void *), void *agerarg, timeout_id_t *top, int satype) 2048 { 2049 keysock_hello_ack_t *kha; 2050 queue_t *oldq; 2051 2052 ASSERT(OTHERQ(q) != NULL); 2053 2054 /* 2055 * First, check atomically that I'm the first and only keysock 2056 * instance. 2057 * 2058 * Use OTHERQ(q), because qreply(q, mp) == putnext(OTHERQ(q), mp), 2059 * and I want this module to say putnext(*_pfkey_q, mp) for PF_KEY 2060 * messages. 2061 */ 2062 2063 oldq = casptr((void **)pfkey_qp, NULL, OTHERQ(q)); 2064 if (oldq != NULL) { 2065 ASSERT(oldq != q); 2066 cmn_err(CE_WARN, "Danger! Multiple keysocks on top of %s.\n", 2067 (satype == SADB_SATYPE_ESP)? "ESP" : "AH or other"); 2068 freemsg(mp); 2069 return; 2070 } 2071 2072 kha = (keysock_hello_ack_t *)mp->b_rptr; 2073 kha->ks_hello_len = sizeof (keysock_hello_ack_t); 2074 kha->ks_hello_type = KEYSOCK_HELLO_ACK; 2075 kha->ks_hello_satype = (uint8_t)satype; 2076 2077 /* 2078 * If we made it past the casptr, then we have "exclusive" access 2079 * to the timeout handle. Fire it off in 4 seconds, because it 2080 * just seems like a good interval. 2081 */ 2082 *top = qtimeout(*pfkey_qp, ager, agerarg, drv_usectohz(4000000)); 2083 2084 putnext(*pfkey_qp, mp); 2085 } 2086 2087 /* 2088 * Normalize IPv4-mapped IPv6 addresses (and prefixes) as appropriate. 2089 * 2090 * Check addresses themselves for wildcard or multicast. 2091 * Check ire table for local/non-local/broadcast. 2092 */ 2093 int 2094 sadb_addrcheck(queue_t *pfkey_q, mblk_t *mp, sadb_ext_t *ext, uint_t serial, 2095 netstack_t *ns) 2096 { 2097 sadb_address_t *addr = (sadb_address_t *)ext; 2098 struct sockaddr_in *sin; 2099 struct sockaddr_in6 *sin6; 2100 ire_t *ire; 2101 int diagnostic, type; 2102 boolean_t normalized = B_FALSE; 2103 2104 ASSERT(ext != NULL); 2105 ASSERT((ext->sadb_ext_type == SADB_EXT_ADDRESS_SRC) || 2106 (ext->sadb_ext_type == SADB_EXT_ADDRESS_DST) || 2107 (ext->sadb_ext_type == SADB_X_EXT_ADDRESS_INNER_SRC) || 2108 (ext->sadb_ext_type == SADB_X_EXT_ADDRESS_INNER_DST) || 2109 (ext->sadb_ext_type == SADB_X_EXT_ADDRESS_NATT_LOC) || 2110 (ext->sadb_ext_type == SADB_X_EXT_ADDRESS_NATT_REM)); 2111 2112 /* Assign both sockaddrs, the compiler will do the right thing. */ 2113 sin = (struct sockaddr_in *)(addr + 1); 2114 sin6 = (struct sockaddr_in6 *)(addr + 1); 2115 2116 if (sin6->sin6_family == AF_INET6) { 2117 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 2118 /* 2119 * Convert to an AF_INET sockaddr. This means the 2120 * return messages will have the extra space, but have 2121 * AF_INET sockaddrs instead of AF_INET6. 2122 * 2123 * Yes, RFC 2367 isn't clear on what to do here w.r.t. 2124 * mapped addresses, but since AF_INET6 ::ffff:<v4> is 2125 * equal to AF_INET <v4>, it shouldnt be a huge 2126 * problem. 2127 */ 2128 sin->sin_family = AF_INET; 2129 IN6_V4MAPPED_TO_INADDR(&sin6->sin6_addr, 2130 &sin->sin_addr); 2131 bzero(&sin->sin_zero, sizeof (sin->sin_zero)); 2132 normalized = B_TRUE; 2133 } 2134 } else if (sin->sin_family != AF_INET) { 2135 switch (ext->sadb_ext_type) { 2136 case SADB_EXT_ADDRESS_SRC: 2137 diagnostic = SADB_X_DIAGNOSTIC_BAD_SRC_AF; 2138 break; 2139 case SADB_EXT_ADDRESS_DST: 2140 diagnostic = SADB_X_DIAGNOSTIC_BAD_DST_AF; 2141 break; 2142 case SADB_X_EXT_ADDRESS_INNER_SRC: 2143 diagnostic = SADB_X_DIAGNOSTIC_BAD_PROXY_AF; 2144 break; 2145 case SADB_X_EXT_ADDRESS_INNER_DST: 2146 diagnostic = SADB_X_DIAGNOSTIC_BAD_INNER_DST_AF; 2147 break; 2148 case SADB_X_EXT_ADDRESS_NATT_LOC: 2149 diagnostic = SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF; 2150 break; 2151 case SADB_X_EXT_ADDRESS_NATT_REM: 2152 diagnostic = SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF; 2153 break; 2154 /* There is no default, see above ASSERT. */ 2155 } 2156 bail: 2157 if (pfkey_q != NULL) { 2158 sadb_pfkey_error(pfkey_q, mp, EINVAL, diagnostic, 2159 serial); 2160 } else { 2161 /* 2162 * Scribble in sadb_msg that we got passed in. 2163 * Overload "mp" to be an sadb_msg pointer. 2164 */ 2165 sadb_msg_t *samsg = (sadb_msg_t *)mp; 2166 2167 samsg->sadb_msg_errno = EINVAL; 2168 samsg->sadb_x_msg_diagnostic = diagnostic; 2169 } 2170 return (KS_IN_ADDR_UNKNOWN); 2171 } 2172 2173 if (ext->sadb_ext_type == SADB_X_EXT_ADDRESS_INNER_SRC || 2174 ext->sadb_ext_type == SADB_X_EXT_ADDRESS_INNER_DST) { 2175 /* 2176 * We need only check for prefix issues. 2177 */ 2178 2179 /* Set diagnostic now, in case we need it later. */ 2180 diagnostic = 2181 (ext->sadb_ext_type == SADB_X_EXT_ADDRESS_INNER_SRC) ? 2182 SADB_X_DIAGNOSTIC_PREFIX_INNER_SRC : 2183 SADB_X_DIAGNOSTIC_PREFIX_INNER_DST; 2184 2185 if (normalized) 2186 addr->sadb_address_prefixlen -= 96; 2187 2188 /* 2189 * Verify and mask out inner-addresses based on prefix length. 2190 */ 2191 if (sin->sin_family == AF_INET) { 2192 if (addr->sadb_address_prefixlen > 32) 2193 goto bail; 2194 sin->sin_addr.s_addr &= 2195 ip_plen_to_mask(addr->sadb_address_prefixlen); 2196 } else { 2197 in6_addr_t mask; 2198 2199 ASSERT(sin->sin_family == AF_INET6); 2200 /* 2201 * ip_plen_to_mask_v6() returns NULL if the value in 2202 * question is out of range. 2203 */ 2204 if (ip_plen_to_mask_v6(addr->sadb_address_prefixlen, 2205 &mask) == NULL) 2206 goto bail; 2207 sin6->sin6_addr.s6_addr32[0] &= mask.s6_addr32[0]; 2208 sin6->sin6_addr.s6_addr32[1] &= mask.s6_addr32[1]; 2209 sin6->sin6_addr.s6_addr32[2] &= mask.s6_addr32[2]; 2210 sin6->sin6_addr.s6_addr32[3] &= mask.s6_addr32[3]; 2211 } 2212 2213 /* We don't care in these cases. */ 2214 return (KS_IN_ADDR_DONTCARE); 2215 } 2216 2217 if (sin->sin_family == AF_INET6) { 2218 /* Check the easy ones now. */ 2219 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 2220 return (KS_IN_ADDR_MBCAST); 2221 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 2222 return (KS_IN_ADDR_UNSPEC); 2223 /* 2224 * At this point, we're a unicast IPv6 address. 2225 * 2226 * A ctable lookup for local is sufficient here. If we're 2227 * local, return KS_IN_ADDR_ME, otherwise KS_IN_ADDR_NOTME. 2228 * 2229 * XXX Zones alert -> me/notme decision needs to be tempered 2230 * by what zone we're in when we go to zone-aware IPsec. 2231 */ 2232 ire = ire_ctable_lookup_v6(&sin6->sin6_addr, NULL, 2233 IRE_LOCAL, NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, 2234 ns->netstack_ip); 2235 if (ire != NULL) { 2236 /* Hey hey, it's local. */ 2237 IRE_REFRELE(ire); 2238 return (KS_IN_ADDR_ME); 2239 } 2240 } else { 2241 ASSERT(sin->sin_family == AF_INET); 2242 if (sin->sin_addr.s_addr == INADDR_ANY) 2243 return (KS_IN_ADDR_UNSPEC); 2244 if (CLASSD(sin->sin_addr.s_addr)) 2245 return (KS_IN_ADDR_MBCAST); 2246 /* 2247 * At this point we're a unicast or broadcast IPv4 address. 2248 * 2249 * Lookup on the ctable for IRE_BROADCAST or IRE_LOCAL. 2250 * A NULL return value is NOTME, otherwise, look at the 2251 * returned ire for broadcast or not and return accordingly. 2252 * 2253 * XXX Zones alert -> me/notme decision needs to be tempered 2254 * by what zone we're in when we go to zone-aware IPsec. 2255 */ 2256 ire = ire_ctable_lookup(sin->sin_addr.s_addr, 0, 2257 IRE_LOCAL | IRE_BROADCAST, NULL, ALL_ZONES, NULL, 2258 MATCH_IRE_TYPE, ns->netstack_ip); 2259 if (ire != NULL) { 2260 /* Check for local or broadcast */ 2261 type = ire->ire_type; 2262 IRE_REFRELE(ire); 2263 ASSERT(type == IRE_LOCAL || type == IRE_BROADCAST); 2264 return ((type == IRE_LOCAL) ? KS_IN_ADDR_ME : 2265 KS_IN_ADDR_MBCAST); 2266 } 2267 } 2268 2269 return (KS_IN_ADDR_NOTME); 2270 } 2271 2272 /* 2273 * Address normalizations and reality checks for inbound PF_KEY messages. 2274 * 2275 * For the case of src == unspecified AF_INET6, and dst == AF_INET, convert 2276 * the source to AF_INET. Do the same for the inner sources. 2277 */ 2278 boolean_t 2279 sadb_addrfix(keysock_in_t *ksi, queue_t *pfkey_q, mblk_t *mp, netstack_t *ns) 2280 { 2281 struct sockaddr_in *src, *isrc; 2282 struct sockaddr_in6 *dst, *idst; 2283 sadb_address_t *srcext, *dstext; 2284 uint16_t sport; 2285 sadb_ext_t **extv = ksi->ks_in_extv; 2286 int rc; 2287 2288 if (extv[SADB_EXT_ADDRESS_SRC] != NULL) { 2289 rc = sadb_addrcheck(pfkey_q, mp, extv[SADB_EXT_ADDRESS_SRC], 2290 ksi->ks_in_serial, ns); 2291 if (rc == KS_IN_ADDR_UNKNOWN) 2292 return (B_FALSE); 2293 if (rc == KS_IN_ADDR_MBCAST) { 2294 sadb_pfkey_error(pfkey_q, mp, EINVAL, 2295 SADB_X_DIAGNOSTIC_BAD_SRC, ksi->ks_in_serial); 2296 return (B_FALSE); 2297 } 2298 ksi->ks_in_srctype = rc; 2299 } 2300 2301 if (extv[SADB_EXT_ADDRESS_DST] != NULL) { 2302 rc = sadb_addrcheck(pfkey_q, mp, extv[SADB_EXT_ADDRESS_DST], 2303 ksi->ks_in_serial, ns); 2304 if (rc == KS_IN_ADDR_UNKNOWN) 2305 return (B_FALSE); 2306 if (rc == KS_IN_ADDR_UNSPEC) { 2307 sadb_pfkey_error(pfkey_q, mp, EINVAL, 2308 SADB_X_DIAGNOSTIC_BAD_DST, ksi->ks_in_serial); 2309 return (B_FALSE); 2310 } 2311 ksi->ks_in_dsttype = rc; 2312 } 2313 2314 /* 2315 * NAT-Traversal addrs are simple enough to not require all of 2316 * the checks in sadb_addrcheck(). Just normalize or reject if not 2317 * AF_INET. 2318 */ 2319 if (extv[SADB_X_EXT_ADDRESS_NATT_LOC] != NULL) { 2320 rc = sadb_addrcheck(pfkey_q, mp, 2321 extv[SADB_X_EXT_ADDRESS_NATT_LOC], ksi->ks_in_serial, ns); 2322 2323 /* 2324 * Local NAT-T addresses never use an IRE_LOCAL, so it should 2325 * always be NOTME, or UNSPEC (to handle both tunnel mode 2326 * AND local-port flexibility). 2327 */ 2328 if (rc != KS_IN_ADDR_NOTME && rc != KS_IN_ADDR_UNSPEC) { 2329 sadb_pfkey_error(pfkey_q, mp, EINVAL, 2330 SADB_X_DIAGNOSTIC_MALFORMED_NATT_LOC, 2331 ksi->ks_in_serial); 2332 return (B_FALSE); 2333 } 2334 src = (struct sockaddr_in *) 2335 (((sadb_address_t *)extv[SADB_X_EXT_ADDRESS_NATT_LOC]) + 1); 2336 if (src->sin_family != AF_INET) { 2337 sadb_pfkey_error(pfkey_q, mp, EINVAL, 2338 SADB_X_DIAGNOSTIC_BAD_NATT_LOC_AF, 2339 ksi->ks_in_serial); 2340 return (B_FALSE); 2341 } 2342 } 2343 2344 if (extv[SADB_X_EXT_ADDRESS_NATT_REM] != NULL) { 2345 rc = sadb_addrcheck(pfkey_q, mp, 2346 extv[SADB_X_EXT_ADDRESS_NATT_REM], ksi->ks_in_serial, ns); 2347 2348 /* 2349 * Remote NAT-T addresses never use an IRE_LOCAL, so it should 2350 * always be NOTME, or UNSPEC if it's a tunnel-mode SA. 2351 */ 2352 if (rc != KS_IN_ADDR_NOTME && 2353 !(extv[SADB_X_EXT_ADDRESS_INNER_SRC] != NULL && 2354 rc == KS_IN_ADDR_UNSPEC)) { 2355 sadb_pfkey_error(pfkey_q, mp, EINVAL, 2356 SADB_X_DIAGNOSTIC_MALFORMED_NATT_REM, 2357 ksi->ks_in_serial); 2358 return (B_FALSE); 2359 } 2360 src = (struct sockaddr_in *) 2361 (((sadb_address_t *)extv[SADB_X_EXT_ADDRESS_NATT_REM]) + 1); 2362 if (src->sin_family != AF_INET) { 2363 sadb_pfkey_error(pfkey_q, mp, EINVAL, 2364 SADB_X_DIAGNOSTIC_BAD_NATT_REM_AF, 2365 ksi->ks_in_serial); 2366 return (B_FALSE); 2367 } 2368 } 2369 2370 if (extv[SADB_X_EXT_ADDRESS_INNER_SRC] != NULL) { 2371 if (extv[SADB_X_EXT_ADDRESS_INNER_DST] == NULL) { 2372 sadb_pfkey_error(pfkey_q, mp, EINVAL, 2373 SADB_X_DIAGNOSTIC_MISSING_INNER_DST, 2374 ksi->ks_in_serial); 2375 return (B_FALSE); 2376 } 2377 2378 if (sadb_addrcheck(pfkey_q, mp, 2379 extv[SADB_X_EXT_ADDRESS_INNER_DST], ksi->ks_in_serial, ns) 2380 == KS_IN_ADDR_UNKNOWN || 2381 sadb_addrcheck(pfkey_q, mp, 2382 extv[SADB_X_EXT_ADDRESS_INNER_SRC], ksi->ks_in_serial, ns) 2383 == KS_IN_ADDR_UNKNOWN) 2384 return (B_FALSE); 2385 2386 isrc = (struct sockaddr_in *) 2387 (((sadb_address_t *)extv[SADB_X_EXT_ADDRESS_INNER_SRC]) + 2388 1); 2389 idst = (struct sockaddr_in6 *) 2390 (((sadb_address_t *)extv[SADB_X_EXT_ADDRESS_INNER_DST]) + 2391 1); 2392 if (isrc->sin_family != idst->sin6_family) { 2393 sadb_pfkey_error(pfkey_q, mp, EINVAL, 2394 SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH, 2395 ksi->ks_in_serial); 2396 return (B_FALSE); 2397 } 2398 } else if (extv[SADB_X_EXT_ADDRESS_INNER_DST] != NULL) { 2399 sadb_pfkey_error(pfkey_q, mp, EINVAL, 2400 SADB_X_DIAGNOSTIC_MISSING_INNER_SRC, 2401 ksi->ks_in_serial); 2402 return (B_FALSE); 2403 } else { 2404 isrc = NULL; /* For inner/outer port check below. */ 2405 } 2406 2407 dstext = (sadb_address_t *)extv[SADB_EXT_ADDRESS_DST]; 2408 srcext = (sadb_address_t *)extv[SADB_EXT_ADDRESS_SRC]; 2409 2410 if (dstext == NULL || srcext == NULL) 2411 return (B_TRUE); 2412 2413 dst = (struct sockaddr_in6 *)(dstext + 1); 2414 src = (struct sockaddr_in *)(srcext + 1); 2415 2416 if (isrc != NULL && 2417 (isrc->sin_port != 0 || idst->sin6_port != 0) && 2418 (src->sin_port != 0 || dst->sin6_port != 0)) { 2419 /* Can't set inner and outer ports in one SA. */ 2420 sadb_pfkey_error(pfkey_q, mp, EINVAL, 2421 SADB_X_DIAGNOSTIC_DUAL_PORT_SETS, 2422 ksi->ks_in_serial); 2423 return (B_FALSE); 2424 } 2425 2426 if (dst->sin6_family == src->sin_family) 2427 return (B_TRUE); 2428 2429 if (srcext->sadb_address_proto != dstext->sadb_address_proto) { 2430 if (srcext->sadb_address_proto == 0) { 2431 srcext->sadb_address_proto = dstext->sadb_address_proto; 2432 } else if (dstext->sadb_address_proto == 0) { 2433 dstext->sadb_address_proto = srcext->sadb_address_proto; 2434 } else { 2435 /* Inequal protocols, neither were 0. Report error. */ 2436 sadb_pfkey_error(pfkey_q, mp, EINVAL, 2437 SADB_X_DIAGNOSTIC_PROTO_MISMATCH, 2438 ksi->ks_in_serial); 2439 return (B_FALSE); 2440 } 2441 } 2442 2443 /* 2444 * With the exception of an unspec IPv6 source and an IPv4 2445 * destination, address families MUST me matched. 2446 */ 2447 if (src->sin_family == AF_INET || 2448 ksi->ks_in_srctype != KS_IN_ADDR_UNSPEC) { 2449 sadb_pfkey_error(pfkey_q, mp, EINVAL, 2450 SADB_X_DIAGNOSTIC_AF_MISMATCH, ksi->ks_in_serial); 2451 return (B_FALSE); 2452 } 2453 2454 /* 2455 * Convert "src" to AF_INET INADDR_ANY. We rely on sin_port being 2456 * in the same place for sockaddr_in and sockaddr_in6. 2457 */ 2458 sport = src->sin_port; 2459 bzero(src, sizeof (*src)); 2460 src->sin_family = AF_INET; 2461 src->sin_port = sport; 2462 2463 return (B_TRUE); 2464 } 2465 2466 /* 2467 * Set the results in "addrtype", given an IRE as requested by 2468 * sadb_addrcheck(). 2469 */ 2470 int 2471 sadb_addrset(ire_t *ire) 2472 { 2473 if ((ire->ire_type & IRE_BROADCAST) || 2474 (ire->ire_ipversion == IPV4_VERSION && CLASSD(ire->ire_addr)) || 2475 (ire->ire_ipversion == IPV6_VERSION && 2476 IN6_IS_ADDR_MULTICAST(&(ire->ire_addr_v6)))) 2477 return (KS_IN_ADDR_MBCAST); 2478 if (ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) 2479 return (KS_IN_ADDR_ME); 2480 return (KS_IN_ADDR_NOTME); 2481 } 2482 2483 2484 /* 2485 * Walker callback function to delete sa's based on src/dst address. 2486 * Assumes that we're called with *head locked, no other locks held; 2487 * Conveniently, and not coincidentally, this is both what sadb_walker 2488 * gives us and also what sadb_unlinkassoc expects. 2489 */ 2490 2491 struct sadb_purge_state 2492 { 2493 uint32_t *src; 2494 uint32_t *dst; 2495 sa_family_t af; 2496 boolean_t inbnd; 2497 char *sidstr; 2498 char *didstr; 2499 uint16_t sidtype; 2500 uint16_t didtype; 2501 uint32_t kmproto; 2502 uint8_t sadb_sa_state; 2503 mblk_t *mq; 2504 sadb_t *sp; 2505 }; 2506 2507 static void 2508 sadb_purge_cb(isaf_t *head, ipsa_t *entry, void *cookie) 2509 { 2510 struct sadb_purge_state *ps = (struct sadb_purge_state *)cookie; 2511 2512 ASSERT(MUTEX_HELD(&head->isaf_lock)); 2513 2514 mutex_enter(&entry->ipsa_lock); 2515 2516 if ((entry->ipsa_state == IPSA_STATE_LARVAL) || 2517 (ps->src != NULL && 2518 !IPSA_ARE_ADDR_EQUAL(entry->ipsa_srcaddr, ps->src, ps->af)) || 2519 (ps->dst != NULL && 2520 !IPSA_ARE_ADDR_EQUAL(entry->ipsa_dstaddr, ps->dst, ps->af)) || 2521 (ps->didstr != NULL && (entry->ipsa_dst_cid != NULL) && 2522 !(ps->didtype == entry->ipsa_dst_cid->ipsid_type && 2523 strcmp(ps->didstr, entry->ipsa_dst_cid->ipsid_cid) == 0)) || 2524 (ps->sidstr != NULL && (entry->ipsa_src_cid != NULL) && 2525 !(ps->sidtype == entry->ipsa_src_cid->ipsid_type && 2526 strcmp(ps->sidstr, entry->ipsa_src_cid->ipsid_cid) == 0)) || 2527 (ps->kmproto <= SADB_X_KMP_MAX && ps->kmproto != entry->ipsa_kmp)) { 2528 mutex_exit(&entry->ipsa_lock); 2529 return; 2530 } 2531 2532 if (ps->inbnd) { 2533 sadb_delete_cluster(entry); 2534 } 2535 entry->ipsa_state = IPSA_STATE_DEAD; 2536 (void) sadb_torch_assoc(head, entry, ps->inbnd, &ps->mq); 2537 } 2538 2539 /* 2540 * Common code to purge an SA with a matching src or dst address. 2541 * Don't kill larval SA's in such a purge. 2542 */ 2543 int 2544 sadb_purge_sa(mblk_t *mp, keysock_in_t *ksi, sadb_t *sp, queue_t *pfkey_q, 2545 queue_t *ip_q) 2546 { 2547 sadb_address_t *dstext = 2548 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 2549 sadb_address_t *srcext = 2550 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC]; 2551 sadb_ident_t *dstid = 2552 (sadb_ident_t *)ksi->ks_in_extv[SADB_EXT_IDENTITY_DST]; 2553 sadb_ident_t *srcid = 2554 (sadb_ident_t *)ksi->ks_in_extv[SADB_EXT_IDENTITY_SRC]; 2555 sadb_x_kmc_t *kmc = 2556 (sadb_x_kmc_t *)ksi->ks_in_extv[SADB_X_EXT_KM_COOKIE]; 2557 struct sockaddr_in *src, *dst; 2558 struct sockaddr_in6 *src6, *dst6; 2559 struct sadb_purge_state ps; 2560 2561 /* 2562 * Don't worry about IPv6 v4-mapped addresses, sadb_addrcheck() 2563 * takes care of them. 2564 */ 2565 2566 /* enforced by caller */ 2567 ASSERT((dstext != NULL) || (srcext != NULL)); 2568 2569 ps.src = NULL; 2570 ps.dst = NULL; 2571 #ifdef DEBUG 2572 ps.af = (sa_family_t)-1; 2573 #endif 2574 ps.mq = NULL; 2575 ps.sidstr = NULL; 2576 ps.didstr = NULL; 2577 ps.kmproto = SADB_X_KMP_MAX + 1; 2578 2579 if (dstext != NULL) { 2580 dst = (struct sockaddr_in *)(dstext + 1); 2581 ps.af = dst->sin_family; 2582 if (dst->sin_family == AF_INET6) { 2583 dst6 = (struct sockaddr_in6 *)dst; 2584 ps.dst = (uint32_t *)&dst6->sin6_addr; 2585 } else { 2586 ps.dst = (uint32_t *)&dst->sin_addr; 2587 } 2588 } 2589 2590 if (srcext != NULL) { 2591 src = (struct sockaddr_in *)(srcext + 1); 2592 ps.af = src->sin_family; 2593 if (src->sin_family == AF_INET6) { 2594 src6 = (struct sockaddr_in6 *)(srcext + 1); 2595 ps.src = (uint32_t *)&src6->sin6_addr; 2596 } else { 2597 ps.src = (uint32_t *)&src->sin_addr; 2598 } 2599 ASSERT(dstext == NULL || src->sin_family == dst->sin_family); 2600 } 2601 2602 ASSERT(ps.af != (sa_family_t)-1); 2603 2604 if (dstid != NULL) { 2605 /* 2606 * NOTE: May need to copy string in the future 2607 * if the inbound keysock message disappears for some strange 2608 * reason. 2609 */ 2610 ps.didstr = (char *)(dstid + 1); 2611 ps.didtype = dstid->sadb_ident_type; 2612 } 2613 2614 if (srcid != NULL) { 2615 /* 2616 * NOTE: May need to copy string in the future 2617 * if the inbound keysock message disappears for some strange 2618 * reason. 2619 */ 2620 ps.sidstr = (char *)(srcid + 1); 2621 ps.sidtype = srcid->sadb_ident_type; 2622 } 2623 2624 if (kmc != NULL) 2625 ps.kmproto = kmc->sadb_x_kmc_proto; 2626 2627 /* 2628 * This is simple, crude, and effective. 2629 * Unimplemented optimizations (TBD): 2630 * - we can limit how many places we search based on where we 2631 * think the SA is filed. 2632 * - if we get a dst address, we can hash based on dst addr to find 2633 * the correct bucket in the outbound table. 2634 */ 2635 ps.inbnd = B_TRUE; 2636 sadb_walker(sp->sdb_if, sp->sdb_hashsize, sadb_purge_cb, &ps); 2637 ps.inbnd = B_FALSE; 2638 sadb_walker(sp->sdb_of, sp->sdb_hashsize, sadb_purge_cb, &ps); 2639 2640 if (ps.mq != NULL) 2641 sadb_drain_torchq(ip_q, ps.mq); 2642 2643 ASSERT(mp->b_cont != NULL); 2644 sadb_pfkey_echo(pfkey_q, mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi, 2645 NULL); 2646 return (0); 2647 } 2648 2649 static void 2650 sadb_delpair_state(isaf_t *head, ipsa_t *entry, void *cookie) 2651 { 2652 struct sadb_purge_state *ps = (struct sadb_purge_state *)cookie; 2653 isaf_t *inbound_bucket; 2654 ipsa_t *peer_assoc; 2655 2656 ASSERT(MUTEX_HELD(&head->isaf_lock)); 2657 2658 mutex_enter(&entry->ipsa_lock); 2659 2660 if ((entry->ipsa_state != ps->sadb_sa_state) || 2661 ((ps->src != NULL) && 2662 !IPSA_ARE_ADDR_EQUAL(entry->ipsa_srcaddr, ps->src, ps->af))) { 2663 mutex_exit(&entry->ipsa_lock); 2664 return; 2665 } 2666 2667 /* 2668 * The isaf_t *, which is passed in , is always an outbound bucket, 2669 * and we are preserving the outbound-then-inbound hash-bucket lock 2670 * ordering. The sadb_walker() which triggers this function is called 2671 * only on the outbound fanout, and the corresponding inbound bucket 2672 * lock is safe to acquire here. 2673 */ 2674 2675 if (entry->ipsa_haspeer) { 2676 inbound_bucket = INBOUND_BUCKET(ps->sp, entry->ipsa_spi); 2677 mutex_enter(&inbound_bucket->isaf_lock); 2678 peer_assoc = ipsec_getassocbyspi(inbound_bucket, 2679 entry->ipsa_spi, entry->ipsa_srcaddr, 2680 entry->ipsa_dstaddr, entry->ipsa_addrfam); 2681 } else { 2682 inbound_bucket = INBOUND_BUCKET(ps->sp, entry->ipsa_otherspi); 2683 mutex_enter(&inbound_bucket->isaf_lock); 2684 peer_assoc = ipsec_getassocbyspi(inbound_bucket, 2685 entry->ipsa_otherspi, entry->ipsa_dstaddr, 2686 entry->ipsa_srcaddr, entry->ipsa_addrfam); 2687 } 2688 2689 entry->ipsa_state = IPSA_STATE_DEAD; 2690 (void) sadb_torch_assoc(head, entry, B_FALSE, &ps->mq); 2691 if (peer_assoc != NULL) { 2692 mutex_enter(&peer_assoc->ipsa_lock); 2693 peer_assoc->ipsa_state = IPSA_STATE_DEAD; 2694 (void) sadb_torch_assoc(inbound_bucket, peer_assoc, 2695 B_FALSE, &ps->mq); 2696 } 2697 mutex_exit(&inbound_bucket->isaf_lock); 2698 } 2699 2700 /* 2701 * Common code to delete/get an SA. 2702 */ 2703 int 2704 sadb_delget_sa(mblk_t *mp, keysock_in_t *ksi, sadbp_t *spp, 2705 int *diagnostic, queue_t *pfkey_q, uint8_t sadb_msg_type) 2706 { 2707 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA]; 2708 sadb_address_t *srcext = 2709 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC]; 2710 sadb_address_t *dstext = 2711 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 2712 ipsa_t *echo_target = NULL; 2713 ipsap_t *ipsapp; 2714 mblk_t *torchq = NULL; 2715 uint_t error = 0; 2716 2717 if (assoc == NULL) { 2718 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA; 2719 return (EINVAL); 2720 } 2721 2722 if (sadb_msg_type == SADB_X_DELPAIR_STATE) { 2723 struct sockaddr_in *src; 2724 struct sockaddr_in6 *src6; 2725 struct sadb_purge_state ps; 2726 2727 if (srcext == NULL) { 2728 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC; 2729 return (EINVAL); 2730 } 2731 ps.src = NULL; 2732 ps.mq = NULL; 2733 src = (struct sockaddr_in *)(srcext + 1); 2734 ps.af = src->sin_family; 2735 if (src->sin_family == AF_INET6) { 2736 src6 = (struct sockaddr_in6 *)(srcext + 1); 2737 ps.src = (uint32_t *)&src6->sin6_addr; 2738 ps.sp = &spp->s_v6; 2739 } else { 2740 ps.src = (uint32_t *)&src->sin_addr; 2741 ps.sp = &spp->s_v4; 2742 } 2743 ps.inbnd = B_FALSE; 2744 ps.sadb_sa_state = assoc->sadb_sa_state; 2745 sadb_walker(ps.sp->sdb_of, ps.sp->sdb_hashsize, 2746 sadb_delpair_state, &ps); 2747 2748 if (ps.mq != NULL) 2749 sadb_drain_torchq(pfkey_q, ps.mq); 2750 2751 ASSERT(mp->b_cont != NULL); 2752 sadb_pfkey_echo(pfkey_q, mp, (sadb_msg_t *)mp->b_cont->b_rptr, 2753 ksi, NULL); 2754 return (0); 2755 } 2756 2757 if (dstext == NULL) { 2758 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST; 2759 return (EINVAL); 2760 } 2761 2762 ipsapp = get_ipsa_pair(assoc, srcext, dstext, spp); 2763 if (ipsapp == NULL) { 2764 *diagnostic = SADB_X_DIAGNOSTIC_SA_NOTFOUND; 2765 return (ESRCH); 2766 } 2767 2768 echo_target = ipsapp->ipsap_sa_ptr; 2769 if (echo_target == NULL) 2770 echo_target = ipsapp->ipsap_psa_ptr; 2771 2772 if (sadb_msg_type == SADB_DELETE || sadb_msg_type == SADB_X_DELPAIR) { 2773 /* 2774 * Bucket locks will be required if SA is actually unlinked. 2775 * get_ipsa_pair() returns valid hash bucket pointers even 2776 * if it can't find a pair SA pointer. 2777 */ 2778 mutex_enter(&ipsapp->ipsap_bucket->isaf_lock); 2779 mutex_enter(&ipsapp->ipsap_pbucket->isaf_lock); 2780 2781 if (ipsapp->ipsap_sa_ptr != NULL) { 2782 mutex_enter(&ipsapp->ipsap_sa_ptr->ipsa_lock); 2783 if (ipsapp->ipsap_sa_ptr->ipsa_flags & IPSA_F_INBOUND) { 2784 sadb_delete_cluster(ipsapp->ipsap_sa_ptr); 2785 } 2786 ipsapp->ipsap_sa_ptr->ipsa_state = IPSA_STATE_DEAD; 2787 (void) sadb_torch_assoc(ipsapp->ipsap_bucket, 2788 ipsapp->ipsap_sa_ptr, B_FALSE, &torchq); 2789 /* 2790 * sadb_torch_assoc() releases the ipsa_lock 2791 * and calls sadb_unlinkassoc() which does a 2792 * IPSA_REFRELE. 2793 */ 2794 } 2795 if (ipsapp->ipsap_psa_ptr != NULL) { 2796 mutex_enter(&ipsapp->ipsap_psa_ptr->ipsa_lock); 2797 if (sadb_msg_type == SADB_X_DELPAIR || 2798 ipsapp->ipsap_psa_ptr->ipsa_haspeer) { 2799 if (ipsapp->ipsap_psa_ptr->ipsa_flags & 2800 IPSA_F_INBOUND) { 2801 sadb_delete_cluster( 2802 ipsapp->ipsap_psa_ptr); 2803 } 2804 ipsapp->ipsap_psa_ptr->ipsa_state = 2805 IPSA_STATE_DEAD; 2806 (void) sadb_torch_assoc(ipsapp->ipsap_pbucket, 2807 ipsapp->ipsap_psa_ptr, B_FALSE, &torchq); 2808 } else { 2809 /* 2810 * Only half of the "pair" has been deleted. 2811 * Update the remaining SA and remove references 2812 * to its pair SA, which is now gone. 2813 */ 2814 ipsapp->ipsap_psa_ptr->ipsa_otherspi = 0; 2815 ipsapp->ipsap_psa_ptr->ipsa_flags &= 2816 ~IPSA_F_PAIRED; 2817 mutex_exit(&ipsapp->ipsap_psa_ptr->ipsa_lock); 2818 } 2819 } else if (sadb_msg_type == SADB_X_DELPAIR) { 2820 *diagnostic = SADB_X_DIAGNOSTIC_PAIR_SA_NOTFOUND; 2821 error = ESRCH; 2822 } 2823 mutex_exit(&ipsapp->ipsap_bucket->isaf_lock); 2824 mutex_exit(&ipsapp->ipsap_pbucket->isaf_lock); 2825 } 2826 2827 if (torchq != NULL) 2828 sadb_drain_torchq(spp->s_ip_q, torchq); 2829 2830 ASSERT(mp->b_cont != NULL); 2831 2832 if (error == 0) 2833 sadb_pfkey_echo(pfkey_q, mp, (sadb_msg_t *) 2834 mp->b_cont->b_rptr, ksi, echo_target); 2835 2836 destroy_ipsa_pair(ipsapp); 2837 2838 return (error); 2839 } 2840 2841 /* 2842 * This function takes a sadb_sa_t and finds the ipsa_t structure 2843 * and the isaf_t (hash bucket) that its stored under. If the security 2844 * association has a peer, the ipsa_t structure and bucket for that security 2845 * association are also searched for. The "pair" of ipsa_t's and isaf_t's 2846 * are returned as a ipsap_t. 2847 * 2848 * Note that a "pair" is defined as one (but not both) of the following: 2849 * 2850 * A security association which has a soft reference to another security 2851 * association via its SPI. 2852 * 2853 * A security association that is not obviously "inbound" or "outbound" so 2854 * it appears in both hash tables, the "peer" being the same security 2855 * association in the other hash table. 2856 * 2857 * This function will return NULL if the ipsa_t can't be found in the 2858 * inbound or outbound hash tables (not found). If only one ipsa_t is 2859 * found, the pair ipsa_t will be NULL. Both isaf_t values are valid 2860 * provided at least one ipsa_t is found. 2861 */ 2862 ipsap_t * 2863 get_ipsa_pair(sadb_sa_t *assoc, sadb_address_t *srcext, sadb_address_t *dstext, 2864 sadbp_t *spp) 2865 { 2866 struct sockaddr_in *src, *dst; 2867 struct sockaddr_in6 *src6, *dst6; 2868 sadb_t *sp; 2869 uint32_t *srcaddr, *dstaddr; 2870 isaf_t *outbound_bucket, *inbound_bucket; 2871 boolean_t in_inbound_table = B_FALSE; 2872 ipsap_t *ipsapp; 2873 sa_family_t af; 2874 2875 uint32_t pair_srcaddr[IPSA_MAX_ADDRLEN]; 2876 uint32_t pair_dstaddr[IPSA_MAX_ADDRLEN]; 2877 uint32_t pair_spi; 2878 2879 ipsapp = kmem_zalloc(sizeof (*ipsapp), KM_NOSLEEP); 2880 if (ipsapp == NULL) 2881 return (NULL); 2882 2883 /* 2884 * Don't worry about IPv6 v4-mapped addresses, sadb_addrcheck() 2885 * takes care of them. 2886 */ 2887 2888 dst = (struct sockaddr_in *)(dstext + 1); 2889 af = dst->sin_family; 2890 if (af == AF_INET6) { 2891 sp = &spp->s_v6; 2892 dst6 = (struct sockaddr_in6 *)dst; 2893 dstaddr = (uint32_t *)&dst6->sin6_addr; 2894 if (srcext != NULL) { 2895 src6 = (struct sockaddr_in6 *)(srcext + 1); 2896 srcaddr = (uint32_t *)&src6->sin6_addr; 2897 ASSERT(src6->sin6_family == af); 2898 ASSERT(src6->sin6_family == AF_INET6); 2899 } else { 2900 srcaddr = ALL_ZEROES_PTR; 2901 } 2902 outbound_bucket = OUTBOUND_BUCKET_V6(sp, 2903 *(uint32_t *)dstaddr); 2904 } else { 2905 sp = &spp->s_v4; 2906 dstaddr = (uint32_t *)&dst->sin_addr; 2907 if (srcext != NULL) { 2908 src = (struct sockaddr_in *)(srcext + 1); 2909 srcaddr = (uint32_t *)&src->sin_addr; 2910 ASSERT(src->sin_family == af); 2911 ASSERT(src->sin_family == AF_INET); 2912 } else { 2913 srcaddr = ALL_ZEROES_PTR; 2914 } 2915 outbound_bucket = OUTBOUND_BUCKET_V4(sp, 2916 *(uint32_t *)dstaddr); 2917 } 2918 2919 inbound_bucket = INBOUND_BUCKET(sp, assoc->sadb_sa_spi); 2920 2921 /* Lock down both buckets. */ 2922 mutex_enter(&outbound_bucket->isaf_lock); 2923 mutex_enter(&inbound_bucket->isaf_lock); 2924 2925 if (assoc->sadb_sa_flags & IPSA_F_INBOUND) { 2926 ipsapp->ipsap_sa_ptr = ipsec_getassocbyspi(inbound_bucket, 2927 assoc->sadb_sa_spi, srcaddr, dstaddr, af); 2928 if (ipsapp->ipsap_sa_ptr != NULL) { 2929 ipsapp->ipsap_bucket = inbound_bucket; 2930 ipsapp->ipsap_pbucket = outbound_bucket; 2931 in_inbound_table = B_TRUE; 2932 } else { 2933 ipsapp->ipsap_sa_ptr = 2934 ipsec_getassocbyspi(outbound_bucket, 2935 assoc->sadb_sa_spi, srcaddr, dstaddr, af); 2936 ipsapp->ipsap_bucket = outbound_bucket; 2937 ipsapp->ipsap_pbucket = inbound_bucket; 2938 } 2939 } else { 2940 /* IPSA_F_OUTBOUND is set *or* no directions flags set. */ 2941 ipsapp->ipsap_sa_ptr = 2942 ipsec_getassocbyspi(outbound_bucket, 2943 assoc->sadb_sa_spi, srcaddr, dstaddr, af); 2944 if (ipsapp->ipsap_sa_ptr != NULL) { 2945 ipsapp->ipsap_bucket = outbound_bucket; 2946 ipsapp->ipsap_pbucket = inbound_bucket; 2947 } else { 2948 ipsapp->ipsap_sa_ptr = 2949 ipsec_getassocbyspi(inbound_bucket, 2950 assoc->sadb_sa_spi, srcaddr, dstaddr, af); 2951 ipsapp->ipsap_bucket = inbound_bucket; 2952 ipsapp->ipsap_pbucket = outbound_bucket; 2953 if (ipsapp->ipsap_sa_ptr != NULL) 2954 in_inbound_table = B_TRUE; 2955 } 2956 } 2957 2958 if (ipsapp->ipsap_sa_ptr == NULL) { 2959 mutex_exit(&outbound_bucket->isaf_lock); 2960 mutex_exit(&inbound_bucket->isaf_lock); 2961 kmem_free(ipsapp, sizeof (*ipsapp)); 2962 return (NULL); 2963 } 2964 2965 if ((ipsapp->ipsap_sa_ptr->ipsa_state == IPSA_STATE_LARVAL) && 2966 in_inbound_table) { 2967 mutex_exit(&outbound_bucket->isaf_lock); 2968 mutex_exit(&inbound_bucket->isaf_lock); 2969 return (ipsapp); 2970 } 2971 2972 mutex_enter(&ipsapp->ipsap_sa_ptr->ipsa_lock); 2973 if (ipsapp->ipsap_sa_ptr->ipsa_haspeer) { 2974 /* 2975 * haspeer implies no sa_pairing, look for same spi 2976 * in other hashtable. 2977 */ 2978 ipsapp->ipsap_psa_ptr = 2979 ipsec_getassocbyspi(ipsapp->ipsap_pbucket, 2980 assoc->sadb_sa_spi, srcaddr, dstaddr, af); 2981 mutex_exit(&ipsapp->ipsap_sa_ptr->ipsa_lock); 2982 mutex_exit(&outbound_bucket->isaf_lock); 2983 mutex_exit(&inbound_bucket->isaf_lock); 2984 return (ipsapp); 2985 } 2986 pair_spi = ipsapp->ipsap_sa_ptr->ipsa_otherspi; 2987 IPSA_COPY_ADDR(&pair_srcaddr, 2988 ipsapp->ipsap_sa_ptr->ipsa_srcaddr, af); 2989 IPSA_COPY_ADDR(&pair_dstaddr, 2990 ipsapp->ipsap_sa_ptr->ipsa_dstaddr, af); 2991 mutex_exit(&ipsapp->ipsap_sa_ptr->ipsa_lock); 2992 mutex_exit(&outbound_bucket->isaf_lock); 2993 mutex_exit(&inbound_bucket->isaf_lock); 2994 2995 if (pair_spi == 0) { 2996 ASSERT(ipsapp->ipsap_bucket != NULL); 2997 ASSERT(ipsapp->ipsap_pbucket != NULL); 2998 return (ipsapp); 2999 } 3000 3001 /* found sa in outbound sadb, peer should be inbound */ 3002 3003 if (in_inbound_table) { 3004 /* Found SA in inbound table, pair will be in outbound. */ 3005 if (af == AF_INET6) { 3006 ipsapp->ipsap_pbucket = OUTBOUND_BUCKET_V6(sp, 3007 *(uint32_t *)pair_srcaddr); 3008 } else { 3009 ipsapp->ipsap_pbucket = OUTBOUND_BUCKET_V4(sp, 3010 *(uint32_t *)pair_srcaddr); 3011 } 3012 } else { 3013 ipsapp->ipsap_pbucket = INBOUND_BUCKET(sp, pair_spi); 3014 } 3015 mutex_enter(&ipsapp->ipsap_pbucket->isaf_lock); 3016 ipsapp->ipsap_psa_ptr = ipsec_getassocbyspi(ipsapp->ipsap_pbucket, 3017 pair_spi, pair_dstaddr, pair_srcaddr, af); 3018 mutex_exit(&ipsapp->ipsap_pbucket->isaf_lock); 3019 ASSERT(ipsapp->ipsap_bucket != NULL); 3020 ASSERT(ipsapp->ipsap_pbucket != NULL); 3021 return (ipsapp); 3022 } 3023 3024 /* 3025 * Initialize the mechanism parameters associated with an SA. 3026 * These parameters can be shared by multiple packets, which saves 3027 * us from the overhead of consulting the algorithm table for 3028 * each packet. 3029 */ 3030 static void 3031 sadb_init_alginfo(ipsa_t *sa) 3032 { 3033 ipsec_alginfo_t *alg; 3034 ipsec_stack_t *ipss = sa->ipsa_netstack->netstack_ipsec; 3035 3036 mutex_enter(&ipss->ipsec_alg_lock); 3037 3038 if (sa->ipsa_encrkey != NULL) { 3039 alg = ipss->ipsec_alglists[IPSEC_ALG_ENCR][sa->ipsa_encr_alg]; 3040 if (alg != NULL && ALG_VALID(alg)) { 3041 sa->ipsa_emech.cm_type = alg->alg_mech_type; 3042 sa->ipsa_emech.cm_param = NULL; 3043 sa->ipsa_emech.cm_param_len = 0; 3044 sa->ipsa_iv_len = alg->alg_datalen; 3045 } else 3046 sa->ipsa_emech.cm_type = CRYPTO_MECHANISM_INVALID; 3047 } 3048 3049 if (sa->ipsa_authkey != NULL) { 3050 alg = ipss->ipsec_alglists[IPSEC_ALG_AUTH][sa->ipsa_auth_alg]; 3051 if (alg != NULL && ALG_VALID(alg)) { 3052 sa->ipsa_amech.cm_type = alg->alg_mech_type; 3053 sa->ipsa_amech.cm_param = (char *)&sa->ipsa_mac_len; 3054 sa->ipsa_amech.cm_param_len = sizeof (size_t); 3055 sa->ipsa_mac_len = (size_t)alg->alg_datalen; 3056 } else 3057 sa->ipsa_amech.cm_type = CRYPTO_MECHANISM_INVALID; 3058 } 3059 3060 mutex_exit(&ipss->ipsec_alg_lock); 3061 } 3062 3063 /* 3064 * Perform NAT-traversal cached checksum offset calculations here. 3065 */ 3066 static void 3067 sadb_nat_calculations(ipsa_t *newbie, sadb_address_t *natt_loc_ext, 3068 sadb_address_t *natt_rem_ext, uint32_t *src_addr_ptr, 3069 uint32_t *dst_addr_ptr) 3070 { 3071 struct sockaddr_in *natt_loc, *natt_rem; 3072 uint32_t *natt_loc_ptr = NULL, *natt_rem_ptr = NULL; 3073 uint32_t running_sum = 0; 3074 3075 #define DOWN_SUM(x) (x) = ((x) & 0xFFFF) + ((x) >> 16) 3076 3077 if (natt_rem_ext != NULL) { 3078 uint32_t l_src; 3079 uint32_t l_rem; 3080 3081 natt_rem = (struct sockaddr_in *)(natt_rem_ext + 1); 3082 3083 /* Ensured by sadb_addrfix(). */ 3084 ASSERT(natt_rem->sin_family == AF_INET); 3085 3086 natt_rem_ptr = (uint32_t *)(&natt_rem->sin_addr); 3087 newbie->ipsa_remote_nat_port = natt_rem->sin_port; 3088 l_src = *src_addr_ptr; 3089 l_rem = *natt_rem_ptr; 3090 3091 /* Instead of IPSA_COPY_ADDR(), just copy first 32 bits. */ 3092 newbie->ipsa_natt_addr_rem = *natt_rem_ptr; 3093 3094 l_src = ntohl(l_src); 3095 DOWN_SUM(l_src); 3096 DOWN_SUM(l_src); 3097 l_rem = ntohl(l_rem); 3098 DOWN_SUM(l_rem); 3099 DOWN_SUM(l_rem); 3100 3101 /* 3102 * We're 1's complement for checksums, so check for wraparound 3103 * here. 3104 */ 3105 if (l_rem > l_src) 3106 l_src--; 3107 3108 running_sum += l_src - l_rem; 3109 3110 DOWN_SUM(running_sum); 3111 DOWN_SUM(running_sum); 3112 } 3113 3114 if (natt_loc_ext != NULL) { 3115 natt_loc = (struct sockaddr_in *)(natt_loc_ext + 1); 3116 3117 /* Ensured by sadb_addrfix(). */ 3118 ASSERT(natt_loc->sin_family == AF_INET); 3119 3120 natt_loc_ptr = (uint32_t *)(&natt_loc->sin_addr); 3121 newbie->ipsa_local_nat_port = natt_loc->sin_port; 3122 3123 /* Instead of IPSA_COPY_ADDR(), just copy first 32 bits. */ 3124 newbie->ipsa_natt_addr_loc = *natt_loc_ptr; 3125 3126 /* 3127 * NAT-T port agility means we may have natt_loc_ext, but 3128 * only for a local-port change. 3129 */ 3130 if (natt_loc->sin_addr.s_addr != INADDR_ANY) { 3131 uint32_t l_dst = ntohl(*dst_addr_ptr); 3132 uint32_t l_loc = ntohl(*natt_loc_ptr); 3133 3134 DOWN_SUM(l_loc); 3135 DOWN_SUM(l_loc); 3136 DOWN_SUM(l_dst); 3137 DOWN_SUM(l_dst); 3138 3139 /* 3140 * We're 1's complement for checksums, so check for 3141 * wraparound here. 3142 */ 3143 if (l_loc > l_dst) 3144 l_dst--; 3145 3146 running_sum += l_dst - l_loc; 3147 DOWN_SUM(running_sum); 3148 DOWN_SUM(running_sum); 3149 } 3150 } 3151 3152 newbie->ipsa_inbound_cksum = running_sum; 3153 #undef DOWN_SUM 3154 } 3155 3156 /* 3157 * This function is called from consumers that need to insert a fully-grown 3158 * security association into its tables. This function takes into account that 3159 * SAs can be "inbound", "outbound", or "both". The "primary" and "secondary" 3160 * hash bucket parameters are set in order of what the SA will be most of the 3161 * time. (For example, an SA with an unspecified source, and a multicast 3162 * destination will primarily be an outbound SA. OTOH, if that destination 3163 * is unicast for this node, then the SA will primarily be inbound.) 3164 * 3165 * It takes a lot of parameters because even if clone is B_FALSE, this needs 3166 * to check both buckets for purposes of collision. 3167 * 3168 * Return 0 upon success. Return various errnos (ENOMEM, EEXIST) for 3169 * various error conditions. We may need to set samsg->sadb_x_msg_diagnostic 3170 * with additional diagnostic information because there is at least one EINVAL 3171 * case here. 3172 */ 3173 int 3174 sadb_common_add(queue_t *ip_q, queue_t *pfkey_q, mblk_t *mp, sadb_msg_t *samsg, 3175 keysock_in_t *ksi, isaf_t *primary, isaf_t *secondary, 3176 ipsa_t *newbie, boolean_t clone, boolean_t is_inbound, int *diagnostic, 3177 netstack_t *ns, sadbp_t *spp) 3178 { 3179 ipsa_t *newbie_clone = NULL, *scratch; 3180 ipsap_t *ipsapp = NULL; 3181 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA]; 3182 sadb_address_t *srcext = 3183 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC]; 3184 sadb_address_t *dstext = 3185 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 3186 sadb_address_t *isrcext = 3187 (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_SRC]; 3188 sadb_address_t *idstext = 3189 (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_DST]; 3190 sadb_x_kmc_t *kmcext = 3191 (sadb_x_kmc_t *)ksi->ks_in_extv[SADB_X_EXT_KM_COOKIE]; 3192 sadb_key_t *akey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH]; 3193 sadb_key_t *ekey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT]; 3194 sadb_x_pair_t *pair_ext = 3195 (sadb_x_pair_t *)ksi->ks_in_extv[SADB_X_EXT_PAIR]; 3196 sadb_x_replay_ctr_t *replayext = 3197 (sadb_x_replay_ctr_t *)ksi->ks_in_extv[SADB_X_EXT_REPLAY_VALUE]; 3198 uint8_t protocol = 3199 (samsg->sadb_msg_satype == SADB_SATYPE_AH) ? IPPROTO_AH:IPPROTO_ESP; 3200 #if 0 3201 /* 3202 * XXXMLS - When Trusted Solaris or Multi-Level Secure functionality 3203 * comes to ON, examine these if 0'ed fragments. Look for XXXMLS. 3204 */ 3205 sadb_sens_t *sens = (sadb_sens_t *); 3206 #endif 3207 struct sockaddr_in *src, *dst, *isrc, *idst; 3208 struct sockaddr_in6 *src6, *dst6, *isrc6, *idst6; 3209 sadb_lifetime_t *soft = 3210 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT]; 3211 sadb_lifetime_t *hard = 3212 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD]; 3213 sadb_lifetime_t *idle = 3214 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_X_EXT_LIFETIME_IDLE]; 3215 sa_family_t af; 3216 int error = 0; 3217 boolean_t isupdate = (newbie != NULL); 3218 uint32_t *src_addr_ptr, *dst_addr_ptr, *isrc_addr_ptr, *idst_addr_ptr; 3219 mblk_t *ctl_mp = NULL; 3220 ipsec_stack_t *ipss = ns->netstack_ipsec; 3221 int rcode; 3222 3223 if (srcext == NULL) { 3224 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC; 3225 return (EINVAL); 3226 } 3227 if (dstext == NULL) { 3228 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST; 3229 return (EINVAL); 3230 } 3231 if (assoc == NULL) { 3232 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA; 3233 return (EINVAL); 3234 } 3235 3236 src = (struct sockaddr_in *)(srcext + 1); 3237 src6 = (struct sockaddr_in6 *)(srcext + 1); 3238 dst = (struct sockaddr_in *)(dstext + 1); 3239 dst6 = (struct sockaddr_in6 *)(dstext + 1); 3240 if (isrcext != NULL) { 3241 isrc = (struct sockaddr_in *)(isrcext + 1); 3242 isrc6 = (struct sockaddr_in6 *)(isrcext + 1); 3243 ASSERT(idstext != NULL); 3244 idst = (struct sockaddr_in *)(idstext + 1); 3245 idst6 = (struct sockaddr_in6 *)(idstext + 1); 3246 } else { 3247 isrc = NULL; 3248 isrc6 = NULL; 3249 } 3250 3251 af = src->sin_family; 3252 3253 if (af == AF_INET) { 3254 src_addr_ptr = (uint32_t *)&src->sin_addr; 3255 dst_addr_ptr = (uint32_t *)&dst->sin_addr; 3256 } else { 3257 ASSERT(af == AF_INET6); 3258 src_addr_ptr = (uint32_t *)&src6->sin6_addr; 3259 dst_addr_ptr = (uint32_t *)&dst6->sin6_addr; 3260 } 3261 3262 if (!isupdate && (clone == B_TRUE || is_inbound == B_TRUE) && 3263 cl_inet_checkspi && 3264 (assoc->sadb_sa_state != SADB_X_SASTATE_ACTIVE_ELSEWHERE)) { 3265 rcode = cl_inet_checkspi(ns->netstack_stackid, protocol, 3266 assoc->sadb_sa_spi, NULL); 3267 if (rcode == -1) { 3268 return (EEXIST); 3269 } 3270 } 3271 3272 /* 3273 * Check to see if the new SA will be cloned AND paired. The 3274 * reason a SA will be cloned is the source or destination addresses 3275 * are not specific enough to determine if the SA goes in the outbound 3276 * or the inbound hash table, so its cloned and put in both. If 3277 * the SA is paired, it's soft linked to another SA for the other 3278 * direction. Keeping track and looking up SA's that are direction 3279 * unspecific and linked is too hard. 3280 */ 3281 if (clone && (pair_ext != NULL)) { 3282 *diagnostic = SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE; 3283 return (EINVAL); 3284 } 3285 3286 if (!isupdate) { 3287 newbie = sadb_makelarvalassoc(assoc->sadb_sa_spi, 3288 src_addr_ptr, dst_addr_ptr, af, ns); 3289 if (newbie == NULL) 3290 return (ENOMEM); 3291 } 3292 3293 mutex_enter(&newbie->ipsa_lock); 3294 3295 if (isrc != NULL) { 3296 if (isrc->sin_family == AF_INET) { 3297 if (srcext->sadb_address_proto != IPPROTO_ENCAP) { 3298 if (srcext->sadb_address_proto != 0) { 3299 /* 3300 * Mismatched outer-packet protocol 3301 * and inner-packet address family. 3302 */ 3303 mutex_exit(&newbie->ipsa_lock); 3304 error = EPROTOTYPE; 3305 goto error; 3306 } else { 3307 /* Fill in with explicit protocol. */ 3308 srcext->sadb_address_proto = 3309 IPPROTO_ENCAP; 3310 dstext->sadb_address_proto = 3311 IPPROTO_ENCAP; 3312 } 3313 } 3314 isrc_addr_ptr = (uint32_t *)&isrc->sin_addr; 3315 idst_addr_ptr = (uint32_t *)&idst->sin_addr; 3316 } else { 3317 ASSERT(isrc->sin_family == AF_INET6); 3318 if (srcext->sadb_address_proto != IPPROTO_IPV6) { 3319 if (srcext->sadb_address_proto != 0) { 3320 /* 3321 * Mismatched outer-packet protocol 3322 * and inner-packet address family. 3323 */ 3324 mutex_exit(&newbie->ipsa_lock); 3325 error = EPROTOTYPE; 3326 goto error; 3327 } else { 3328 /* Fill in with explicit protocol. */ 3329 srcext->sadb_address_proto = 3330 IPPROTO_IPV6; 3331 dstext->sadb_address_proto = 3332 IPPROTO_IPV6; 3333 } 3334 } 3335 isrc_addr_ptr = (uint32_t *)&isrc6->sin6_addr; 3336 idst_addr_ptr = (uint32_t *)&idst6->sin6_addr; 3337 } 3338 newbie->ipsa_innerfam = isrc->sin_family; 3339 3340 IPSA_COPY_ADDR(newbie->ipsa_innersrc, isrc_addr_ptr, 3341 newbie->ipsa_innerfam); 3342 IPSA_COPY_ADDR(newbie->ipsa_innerdst, idst_addr_ptr, 3343 newbie->ipsa_innerfam); 3344 newbie->ipsa_innersrcpfx = isrcext->sadb_address_prefixlen; 3345 newbie->ipsa_innerdstpfx = idstext->sadb_address_prefixlen; 3346 3347 /* Unique value uses inner-ports for Tunnel Mode... */ 3348 newbie->ipsa_unique_id = SA_UNIQUE_ID(isrc->sin_port, 3349 idst->sin_port, dstext->sadb_address_proto, 3350 idstext->sadb_address_proto); 3351 newbie->ipsa_unique_mask = SA_UNIQUE_MASK(isrc->sin_port, 3352 idst->sin_port, dstext->sadb_address_proto, 3353 idstext->sadb_address_proto); 3354 } else { 3355 /* ... and outer-ports for Transport Mode. */ 3356 newbie->ipsa_unique_id = SA_UNIQUE_ID(src->sin_port, 3357 dst->sin_port, dstext->sadb_address_proto, 0); 3358 newbie->ipsa_unique_mask = SA_UNIQUE_MASK(src->sin_port, 3359 dst->sin_port, dstext->sadb_address_proto, 0); 3360 } 3361 if (newbie->ipsa_unique_mask != (uint64_t)0) 3362 newbie->ipsa_flags |= IPSA_F_UNIQUE; 3363 3364 sadb_nat_calculations(newbie, 3365 (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_LOC], 3366 (sadb_address_t *)ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_REM], 3367 src_addr_ptr, dst_addr_ptr); 3368 3369 newbie->ipsa_type = samsg->sadb_msg_satype; 3370 ASSERT((assoc->sadb_sa_state == SADB_SASTATE_MATURE) || 3371 (assoc->sadb_sa_state == SADB_X_SASTATE_ACTIVE_ELSEWHERE)); 3372 newbie->ipsa_auth_alg = assoc->sadb_sa_auth; 3373 newbie->ipsa_encr_alg = assoc->sadb_sa_encrypt; 3374 3375 newbie->ipsa_flags |= assoc->sadb_sa_flags; 3376 if ((newbie->ipsa_flags & SADB_X_SAFLAGS_NATT_LOC && 3377 ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_LOC] == NULL) || 3378 (newbie->ipsa_flags & SADB_X_SAFLAGS_NATT_REM && 3379 ksi->ks_in_extv[SADB_X_EXT_ADDRESS_NATT_REM] == NULL) || 3380 (newbie->ipsa_flags & SADB_X_SAFLAGS_TUNNEL && 3381 ksi->ks_in_extv[SADB_X_EXT_ADDRESS_INNER_SRC] == NULL)) { 3382 mutex_exit(&newbie->ipsa_lock); 3383 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS; 3384 error = EINVAL; 3385 goto error; 3386 } 3387 /* 3388 * If unspecified source address, force replay_wsize to 0. 3389 * This is because an SA that has multiple sources of secure 3390 * traffic cannot enforce a replay counter w/o synchronizing the 3391 * senders. 3392 */ 3393 if (ksi->ks_in_srctype != KS_IN_ADDR_UNSPEC) 3394 newbie->ipsa_replay_wsize = assoc->sadb_sa_replay; 3395 else 3396 newbie->ipsa_replay_wsize = 0; 3397 3398 newbie->ipsa_addtime = gethrestime_sec(); 3399 3400 if (kmcext != NULL) { 3401 newbie->ipsa_kmp = kmcext->sadb_x_kmc_proto; 3402 newbie->ipsa_kmc = kmcext->sadb_x_kmc_cookie; 3403 } 3404 3405 /* 3406 * XXX CURRENT lifetime checks MAY BE needed for an UPDATE. 3407 * The spec says that one can update current lifetimes, but 3408 * that seems impractical, especially in the larval-to-mature 3409 * update that this function performs. 3410 */ 3411 if (soft != NULL) { 3412 newbie->ipsa_softaddlt = soft->sadb_lifetime_addtime; 3413 newbie->ipsa_softuselt = soft->sadb_lifetime_usetime; 3414 newbie->ipsa_softbyteslt = soft->sadb_lifetime_bytes; 3415 newbie->ipsa_softalloc = soft->sadb_lifetime_allocations; 3416 SET_EXPIRE(newbie, softaddlt, softexpiretime); 3417 } 3418 if (hard != NULL) { 3419 newbie->ipsa_hardaddlt = hard->sadb_lifetime_addtime; 3420 newbie->ipsa_harduselt = hard->sadb_lifetime_usetime; 3421 newbie->ipsa_hardbyteslt = hard->sadb_lifetime_bytes; 3422 newbie->ipsa_hardalloc = hard->sadb_lifetime_allocations; 3423 SET_EXPIRE(newbie, hardaddlt, hardexpiretime); 3424 } 3425 if (idle != NULL) { 3426 newbie->ipsa_idleaddlt = idle->sadb_lifetime_addtime; 3427 newbie->ipsa_idleuselt = idle->sadb_lifetime_usetime; 3428 newbie->ipsa_idleexpiretime = newbie->ipsa_addtime + 3429 newbie->ipsa_idleaddlt; 3430 newbie->ipsa_idletime = newbie->ipsa_idleaddlt; 3431 } 3432 3433 newbie->ipsa_authtmpl = NULL; 3434 newbie->ipsa_encrtmpl = NULL; 3435 3436 if (akey != NULL) { 3437 newbie->ipsa_authkeybits = akey->sadb_key_bits; 3438 newbie->ipsa_authkeylen = SADB_1TO8(akey->sadb_key_bits); 3439 /* In case we have to round up to the next byte... */ 3440 if ((akey->sadb_key_bits & 0x7) != 0) 3441 newbie->ipsa_authkeylen++; 3442 newbie->ipsa_authkey = kmem_alloc(newbie->ipsa_authkeylen, 3443 KM_NOSLEEP); 3444 if (newbie->ipsa_authkey == NULL) { 3445 error = ENOMEM; 3446 mutex_exit(&newbie->ipsa_lock); 3447 goto error; 3448 } 3449 bcopy(akey + 1, newbie->ipsa_authkey, newbie->ipsa_authkeylen); 3450 bzero(akey + 1, newbie->ipsa_authkeylen); 3451 3452 /* 3453 * Pre-initialize the kernel crypto framework key 3454 * structure. 3455 */ 3456 newbie->ipsa_kcfauthkey.ck_format = CRYPTO_KEY_RAW; 3457 newbie->ipsa_kcfauthkey.ck_length = newbie->ipsa_authkeybits; 3458 newbie->ipsa_kcfauthkey.ck_data = newbie->ipsa_authkey; 3459 3460 mutex_enter(&ipss->ipsec_alg_lock); 3461 error = ipsec_create_ctx_tmpl(newbie, IPSEC_ALG_AUTH); 3462 mutex_exit(&ipss->ipsec_alg_lock); 3463 if (error != 0) { 3464 mutex_exit(&newbie->ipsa_lock); 3465 goto error; 3466 } 3467 } 3468 3469 if (ekey != NULL) { 3470 newbie->ipsa_encrkeybits = ekey->sadb_key_bits; 3471 newbie->ipsa_encrkeylen = SADB_1TO8(ekey->sadb_key_bits); 3472 /* In case we have to round up to the next byte... */ 3473 if ((ekey->sadb_key_bits & 0x7) != 0) 3474 newbie->ipsa_encrkeylen++; 3475 newbie->ipsa_encrkey = kmem_alloc(newbie->ipsa_encrkeylen, 3476 KM_NOSLEEP); 3477 if (newbie->ipsa_encrkey == NULL) { 3478 error = ENOMEM; 3479 mutex_exit(&newbie->ipsa_lock); 3480 goto error; 3481 } 3482 bcopy(ekey + 1, newbie->ipsa_encrkey, newbie->ipsa_encrkeylen); 3483 /* XXX is this safe w.r.t db_ref, etc? */ 3484 bzero(ekey + 1, newbie->ipsa_encrkeylen); 3485 3486 /* 3487 * Pre-initialize the kernel crypto framework key 3488 * structure. 3489 */ 3490 newbie->ipsa_kcfencrkey.ck_format = CRYPTO_KEY_RAW; 3491 newbie->ipsa_kcfencrkey.ck_length = newbie->ipsa_encrkeybits; 3492 newbie->ipsa_kcfencrkey.ck_data = newbie->ipsa_encrkey; 3493 3494 mutex_enter(&ipss->ipsec_alg_lock); 3495 error = ipsec_create_ctx_tmpl(newbie, IPSEC_ALG_ENCR); 3496 mutex_exit(&ipss->ipsec_alg_lock); 3497 if (error != 0) { 3498 mutex_exit(&newbie->ipsa_lock); 3499 goto error; 3500 } 3501 } 3502 3503 sadb_init_alginfo(newbie); 3504 3505 /* 3506 * Ptrs to processing functions. 3507 */ 3508 if (newbie->ipsa_type == SADB_SATYPE_ESP) 3509 ipsecesp_init_funcs(newbie); 3510 else 3511 ipsecah_init_funcs(newbie); 3512 ASSERT(newbie->ipsa_output_func != NULL && 3513 newbie->ipsa_input_func != NULL); 3514 3515 /* 3516 * Certificate ID stuff. 3517 */ 3518 if (ksi->ks_in_extv[SADB_EXT_IDENTITY_SRC] != NULL) { 3519 sadb_ident_t *id = 3520 (sadb_ident_t *)ksi->ks_in_extv[SADB_EXT_IDENTITY_SRC]; 3521 3522 /* 3523 * Can assume strlen() will return okay because ext_check() in 3524 * keysock.c prepares the string for us. 3525 */ 3526 newbie->ipsa_src_cid = ipsid_lookup(id->sadb_ident_type, 3527 (char *)(id+1), ns); 3528 if (newbie->ipsa_src_cid == NULL) { 3529 error = ENOMEM; 3530 mutex_exit(&newbie->ipsa_lock); 3531 goto error; 3532 } 3533 } 3534 3535 if (ksi->ks_in_extv[SADB_EXT_IDENTITY_DST] != NULL) { 3536 sadb_ident_t *id = 3537 (sadb_ident_t *)ksi->ks_in_extv[SADB_EXT_IDENTITY_DST]; 3538 3539 /* 3540 * Can assume strlen() will return okay because ext_check() in 3541 * keysock.c prepares the string for us. 3542 */ 3543 newbie->ipsa_dst_cid = ipsid_lookup(id->sadb_ident_type, 3544 (char *)(id+1), ns); 3545 if (newbie->ipsa_dst_cid == NULL) { 3546 error = ENOMEM; 3547 mutex_exit(&newbie->ipsa_lock); 3548 goto error; 3549 } 3550 } 3551 3552 #if 0 3553 /* XXXMLS SENSITIVITY handling code. */ 3554 if (sens != NULL) { 3555 int i; 3556 uint64_t *bitmap = (uint64_t *)(sens + 1); 3557 3558 newbie->ipsa_dpd = sens->sadb_sens_dpd; 3559 newbie->ipsa_senslevel = sens->sadb_sens_sens_level; 3560 newbie->ipsa_integlevel = sens->sadb_sens_integ_level; 3561 newbie->ipsa_senslen = SADB_64TO8(sens->sadb_sens_sens_len); 3562 newbie->ipsa_integlen = SADB_64TO8(sens->sadb_sens_integ_len); 3563 newbie->ipsa_integ = kmem_alloc(newbie->ipsa_integlen, 3564 KM_NOSLEEP); 3565 if (newbie->ipsa_integ == NULL) { 3566 error = ENOMEM; 3567 mutex_exit(&newbie->ipsa_lock); 3568 goto error; 3569 } 3570 newbie->ipsa_sens = kmem_alloc(newbie->ipsa_senslen, 3571 KM_NOSLEEP); 3572 if (newbie->ipsa_sens == NULL) { 3573 error = ENOMEM; 3574 mutex_exit(&newbie->ipsa_lock); 3575 goto error; 3576 } 3577 for (i = 0; i < sens->sadb_sens_sens_len; i++) { 3578 newbie->ipsa_sens[i] = *bitmap; 3579 bitmap++; 3580 } 3581 for (i = 0; i < sens->sadb_sens_integ_len; i++) { 3582 newbie->ipsa_integ[i] = *bitmap; 3583 bitmap++; 3584 } 3585 } 3586 3587 #endif 3588 3589 if (replayext != NULL) { 3590 if ((replayext->sadb_x_rc_replay32 == 0) && 3591 (replayext->sadb_x_rc_replay64 != 0)) { 3592 error = EOPNOTSUPP; 3593 mutex_exit(&newbie->ipsa_lock); 3594 goto error; 3595 } 3596 newbie->ipsa_replay = replayext->sadb_x_rc_replay32; 3597 } 3598 3599 /* now that the SA has been updated, set its new state */ 3600 newbie->ipsa_state = assoc->sadb_sa_state; 3601 3602 if (clone) { 3603 newbie->ipsa_haspeer = B_TRUE; 3604 } else { 3605 if (!is_inbound) { 3606 lifetime_fuzz(newbie); 3607 } 3608 } 3609 /* 3610 * The less locks I hold when doing an insertion and possible cloning, 3611 * the better! 3612 */ 3613 mutex_exit(&newbie->ipsa_lock); 3614 3615 if (clone) { 3616 newbie_clone = sadb_cloneassoc(newbie); 3617 3618 if (newbie_clone == NULL) { 3619 error = ENOMEM; 3620 goto error; 3621 } 3622 } 3623 3624 /* 3625 * Enter the bucket locks. The order of entry is outbound, 3626 * inbound. We map "primary" and "secondary" into outbound and inbound 3627 * based on the destination address type. If the destination address 3628 * type is for a node that isn't mine (or potentially mine), the 3629 * "primary" bucket is the outbound one. 3630 */ 3631 if (!is_inbound) { 3632 /* primary == outbound */ 3633 mutex_enter(&primary->isaf_lock); 3634 mutex_enter(&secondary->isaf_lock); 3635 } else { 3636 /* primary == inbound */ 3637 mutex_enter(&secondary->isaf_lock); 3638 mutex_enter(&primary->isaf_lock); 3639 } 3640 3641 IPSECHW_DEBUG(IPSECHW_SADB, ("sadb_common_add: spi = 0x%x\n", 3642 newbie->ipsa_spi)); 3643 3644 /* 3645 * sadb_insertassoc() doesn't increment the reference 3646 * count. We therefore have to increment the 3647 * reference count one more time to reflect the 3648 * pointers of the table that reference this SA. 3649 */ 3650 IPSA_REFHOLD(newbie); 3651 3652 if (isupdate) { 3653 /* 3654 * Unlink from larval holding cell in the "inbound" fanout. 3655 */ 3656 ASSERT(newbie->ipsa_linklock == &primary->isaf_lock || 3657 newbie->ipsa_linklock == &secondary->isaf_lock); 3658 sadb_unlinkassoc(newbie); 3659 } 3660 3661 mutex_enter(&newbie->ipsa_lock); 3662 error = sadb_insertassoc(newbie, primary); 3663 if (error == 0) { 3664 ctl_mp = sadb_fmt_sa_req(DL_CO_SET, newbie->ipsa_type, newbie, 3665 is_inbound); 3666 } 3667 mutex_exit(&newbie->ipsa_lock); 3668 3669 if (error != 0) { 3670 /* 3671 * Since sadb_insertassoc() failed, we must decrement the 3672 * refcount again so the cleanup code will actually free 3673 * the offending SA. 3674 */ 3675 IPSA_REFRELE(newbie); 3676 goto error_unlock; 3677 } 3678 3679 if (newbie_clone != NULL) { 3680 mutex_enter(&newbie_clone->ipsa_lock); 3681 error = sadb_insertassoc(newbie_clone, secondary); 3682 mutex_exit(&newbie_clone->ipsa_lock); 3683 if (error != 0) { 3684 /* Collision in secondary table. */ 3685 sadb_unlinkassoc(newbie); /* This does REFRELE. */ 3686 goto error_unlock; 3687 } 3688 IPSA_REFHOLD(newbie_clone); 3689 } else { 3690 ASSERT(primary != secondary); 3691 scratch = ipsec_getassocbyspi(secondary, newbie->ipsa_spi, 3692 ALL_ZEROES_PTR, newbie->ipsa_dstaddr, af); 3693 if (scratch != NULL) { 3694 /* Collision in secondary table. */ 3695 sadb_unlinkassoc(newbie); /* This does REFRELE. */ 3696 /* Set the error, since ipsec_getassocbyspi() can't. */ 3697 error = EEXIST; 3698 goto error_unlock; 3699 } 3700 } 3701 3702 /* OKAY! So let's do some reality check assertions. */ 3703 3704 ASSERT(MUTEX_NOT_HELD(&newbie->ipsa_lock)); 3705 ASSERT(newbie_clone == NULL || 3706 (MUTEX_NOT_HELD(&newbie_clone->ipsa_lock))); 3707 /* 3708 * If hardware acceleration could happen, send it. 3709 */ 3710 if (ctl_mp != NULL) { 3711 putnext(ip_q, ctl_mp); 3712 ctl_mp = NULL; 3713 } 3714 3715 error_unlock: 3716 3717 /* 3718 * We can exit the locks in any order. Only entrance needs to 3719 * follow any protocol. 3720 */ 3721 mutex_exit(&secondary->isaf_lock); 3722 mutex_exit(&primary->isaf_lock); 3723 3724 if (pair_ext != NULL && error == 0) { 3725 /* update pair_spi if it exists. */ 3726 ipsapp = get_ipsa_pair(assoc, srcext, dstext, spp); 3727 if (ipsapp == NULL) { 3728 error = ESRCH; 3729 *diagnostic = SADB_X_DIAGNOSTIC_PAIR_SA_NOTFOUND; 3730 } else if (ipsapp->ipsap_psa_ptr != NULL) { 3731 *diagnostic = SADB_X_DIAGNOSTIC_PAIR_ALREADY; 3732 error = EINVAL; 3733 } else { 3734 /* update_pairing() sets diagnostic */ 3735 error = update_pairing(ipsapp, ksi, diagnostic, spp); 3736 } 3737 } 3738 /* Common error point for this routine. */ 3739 error: 3740 if (newbie != NULL) { 3741 if (error != 0) { 3742 /* This SA is broken, let the reaper clean up. */ 3743 mutex_enter(&newbie->ipsa_lock); 3744 newbie->ipsa_state = IPSA_STATE_DEAD; 3745 newbie->ipsa_hardexpiretime = 1; 3746 mutex_exit(&newbie->ipsa_lock); 3747 } 3748 IPSA_REFRELE(newbie); 3749 } 3750 if (newbie_clone != NULL) { 3751 IPSA_REFRELE(newbie_clone); 3752 } 3753 if (ctl_mp != NULL) 3754 freemsg(ctl_mp); 3755 3756 if (error == 0) { 3757 /* 3758 * Construct favorable PF_KEY return message and send to 3759 * keysock. Update the flags in the original keysock message 3760 * to reflect the actual flags in the new SA. 3761 * (Q: Do I need to pass "newbie"? If I do, 3762 * make sure to REFHOLD, call, then REFRELE.) 3763 */ 3764 assoc->sadb_sa_flags = newbie->ipsa_flags; 3765 sadb_pfkey_echo(pfkey_q, mp, samsg, ksi, NULL); 3766 } 3767 3768 destroy_ipsa_pair(ipsapp); 3769 return (error); 3770 } 3771 3772 /* 3773 * Set the time of first use for a security association. Update any 3774 * expiration times as a result. 3775 */ 3776 void 3777 sadb_set_usetime(ipsa_t *assoc) 3778 { 3779 time_t snapshot = gethrestime_sec(); 3780 3781 mutex_enter(&assoc->ipsa_lock); 3782 assoc->ipsa_lastuse = snapshot; 3783 assoc->ipsa_idleexpiretime = snapshot + assoc->ipsa_idletime; 3784 3785 /* 3786 * Caller does check usetime before calling me usually, and 3787 * double-checking is better than a mutex_enter/exit hit. 3788 */ 3789 if (assoc->ipsa_usetime == 0) { 3790 /* 3791 * This is redundant for outbound SA's, as 3792 * ipsec_getassocbyconn() sets the IPSA_F_USED flag already. 3793 * Inbound SAs, however, have no such protection. 3794 */ 3795 assoc->ipsa_flags |= IPSA_F_USED; 3796 assoc->ipsa_usetime = snapshot; 3797 3798 /* 3799 * After setting the use time, see if we have a use lifetime 3800 * that would cause the actual SA expiration time to shorten. 3801 */ 3802 UPDATE_EXPIRE(assoc, softuselt, softexpiretime); 3803 UPDATE_EXPIRE(assoc, harduselt, hardexpiretime); 3804 } 3805 mutex_exit(&assoc->ipsa_lock); 3806 } 3807 3808 /* 3809 * Send up a PF_KEY expire message for this association. 3810 */ 3811 static void 3812 sadb_expire_assoc(queue_t *pfkey_q, ipsa_t *assoc) 3813 { 3814 mblk_t *mp, *mp1; 3815 int alloclen, af; 3816 sadb_msg_t *samsg; 3817 sadb_lifetime_t *current, *expire; 3818 sadb_sa_t *saext; 3819 uint8_t *end; 3820 boolean_t tunnel_mode; 3821 3822 ASSERT(MUTEX_HELD(&assoc->ipsa_lock)); 3823 3824 /* Don't bother sending if there's no queue. */ 3825 if (pfkey_q == NULL) 3826 return; 3827 3828 /* If the SA is one of a pair, only SOFT expire the OUTBOUND SA */ 3829 if (assoc->ipsa_state == IPSA_STATE_DYING && 3830 (assoc->ipsa_flags & IPSA_F_PAIRED) && 3831 !(assoc->ipsa_flags & IPSA_F_OUTBOUND)) { 3832 return; 3833 } 3834 3835 mp = sadb_keysock_out(0); 3836 if (mp == NULL) { 3837 /* cmn_err(CE_WARN, */ 3838 /* "sadb_expire_assoc: Can't allocate KEYSOCK_OUT.\n"); */ 3839 return; 3840 } 3841 3842 alloclen = sizeof (*samsg) + sizeof (*current) + sizeof (*expire) + 3843 2 * sizeof (sadb_address_t) + sizeof (*saext); 3844 3845 af = assoc->ipsa_addrfam; 3846 switch (af) { 3847 case AF_INET: 3848 alloclen += 2 * sizeof (struct sockaddr_in); 3849 break; 3850 case AF_INET6: 3851 alloclen += 2 * sizeof (struct sockaddr_in6); 3852 break; 3853 default: 3854 /* Won't happen unless there's a kernel bug. */ 3855 freeb(mp); 3856 cmn_err(CE_WARN, 3857 "sadb_expire_assoc: Unknown address length.\n"); 3858 return; 3859 } 3860 3861 tunnel_mode = (assoc->ipsa_flags & IPSA_F_TUNNEL); 3862 if (tunnel_mode) { 3863 alloclen += 2 * sizeof (sadb_address_t); 3864 switch (assoc->ipsa_innerfam) { 3865 case AF_INET: 3866 alloclen += 2 * sizeof (struct sockaddr_in); 3867 break; 3868 case AF_INET6: 3869 alloclen += 2 * sizeof (struct sockaddr_in6); 3870 break; 3871 default: 3872 /* Won't happen unless there's a kernel bug. */ 3873 freeb(mp); 3874 cmn_err(CE_WARN, "sadb_expire_assoc: " 3875 "Unknown inner address length.\n"); 3876 return; 3877 } 3878 } 3879 3880 mp->b_cont = allocb(alloclen, BPRI_HI); 3881 if (mp->b_cont == NULL) { 3882 freeb(mp); 3883 /* cmn_err(CE_WARN, */ 3884 /* "sadb_expire_assoc: Can't allocate message.\n"); */ 3885 return; 3886 } 3887 3888 mp1 = mp; 3889 mp = mp->b_cont; 3890 end = mp->b_wptr + alloclen; 3891 3892 samsg = (sadb_msg_t *)mp->b_wptr; 3893 mp->b_wptr += sizeof (*samsg); 3894 samsg->sadb_msg_version = PF_KEY_V2; 3895 samsg->sadb_msg_type = SADB_EXPIRE; 3896 samsg->sadb_msg_errno = 0; 3897 samsg->sadb_msg_satype = assoc->ipsa_type; 3898 samsg->sadb_msg_len = SADB_8TO64(alloclen); 3899 samsg->sadb_msg_reserved = 0; 3900 samsg->sadb_msg_seq = 0; 3901 samsg->sadb_msg_pid = 0; 3902 3903 saext = (sadb_sa_t *)mp->b_wptr; 3904 mp->b_wptr += sizeof (*saext); 3905 saext->sadb_sa_len = SADB_8TO64(sizeof (*saext)); 3906 saext->sadb_sa_exttype = SADB_EXT_SA; 3907 saext->sadb_sa_spi = assoc->ipsa_spi; 3908 saext->sadb_sa_replay = assoc->ipsa_replay_wsize; 3909 saext->sadb_sa_state = assoc->ipsa_state; 3910 saext->sadb_sa_auth = assoc->ipsa_auth_alg; 3911 saext->sadb_sa_encrypt = assoc->ipsa_encr_alg; 3912 saext->sadb_sa_flags = assoc->ipsa_flags; 3913 3914 current = (sadb_lifetime_t *)mp->b_wptr; 3915 mp->b_wptr += sizeof (sadb_lifetime_t); 3916 current->sadb_lifetime_len = SADB_8TO64(sizeof (*current)); 3917 current->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT; 3918 /* We do not support the concept. */ 3919 current->sadb_lifetime_allocations = 0; 3920 current->sadb_lifetime_bytes = assoc->ipsa_bytes; 3921 current->sadb_lifetime_addtime = assoc->ipsa_addtime; 3922 current->sadb_lifetime_usetime = assoc->ipsa_usetime; 3923 3924 expire = (sadb_lifetime_t *)mp->b_wptr; 3925 mp->b_wptr += sizeof (*expire); 3926 expire->sadb_lifetime_len = SADB_8TO64(sizeof (*expire)); 3927 3928 if (assoc->ipsa_state == IPSA_STATE_DEAD) { 3929 expire->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD; 3930 expire->sadb_lifetime_allocations = assoc->ipsa_hardalloc; 3931 expire->sadb_lifetime_bytes = assoc->ipsa_hardbyteslt; 3932 expire->sadb_lifetime_addtime = assoc->ipsa_hardaddlt; 3933 expire->sadb_lifetime_usetime = assoc->ipsa_harduselt; 3934 } else if (assoc->ipsa_state == IPSA_STATE_DYING) { 3935 expire->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT; 3936 expire->sadb_lifetime_allocations = assoc->ipsa_softalloc; 3937 expire->sadb_lifetime_bytes = assoc->ipsa_softbyteslt; 3938 expire->sadb_lifetime_addtime = assoc->ipsa_softaddlt; 3939 expire->sadb_lifetime_usetime = assoc->ipsa_softuselt; 3940 } else { 3941 ASSERT(assoc->ipsa_state == IPSA_STATE_MATURE); 3942 expire->sadb_lifetime_exttype = SADB_X_EXT_LIFETIME_IDLE; 3943 expire->sadb_lifetime_allocations = 0; 3944 expire->sadb_lifetime_bytes = 0; 3945 expire->sadb_lifetime_addtime = assoc->ipsa_idleaddlt; 3946 expire->sadb_lifetime_usetime = assoc->ipsa_idleuselt; 3947 } 3948 3949 mp->b_wptr = sadb_make_addr_ext(mp->b_wptr, end, SADB_EXT_ADDRESS_SRC, 3950 af, assoc->ipsa_srcaddr, tunnel_mode ? 0 : SA_SRCPORT(assoc), 3951 SA_PROTO(assoc), 0); 3952 ASSERT(mp->b_wptr != NULL); 3953 3954 mp->b_wptr = sadb_make_addr_ext(mp->b_wptr, end, SADB_EXT_ADDRESS_DST, 3955 af, assoc->ipsa_dstaddr, tunnel_mode ? 0 : SA_DSTPORT(assoc), 3956 SA_PROTO(assoc), 0); 3957 ASSERT(mp->b_wptr != NULL); 3958 3959 if (tunnel_mode) { 3960 mp->b_wptr = sadb_make_addr_ext(mp->b_wptr, end, 3961 SADB_X_EXT_ADDRESS_INNER_SRC, assoc->ipsa_innerfam, 3962 assoc->ipsa_innersrc, SA_SRCPORT(assoc), SA_IPROTO(assoc), 3963 assoc->ipsa_innersrcpfx); 3964 ASSERT(mp->b_wptr != NULL); 3965 mp->b_wptr = sadb_make_addr_ext(mp->b_wptr, end, 3966 SADB_X_EXT_ADDRESS_INNER_DST, assoc->ipsa_innerfam, 3967 assoc->ipsa_innerdst, SA_DSTPORT(assoc), SA_IPROTO(assoc), 3968 assoc->ipsa_innerdstpfx); 3969 ASSERT(mp->b_wptr != NULL); 3970 } 3971 3972 /* Can just putnext, we're ready to go! */ 3973 putnext(pfkey_q, mp1); 3974 } 3975 3976 /* 3977 * "Age" the SA with the number of bytes that was used to protect traffic. 3978 * Send an SADB_EXPIRE message if appropriate. Return B_TRUE if there was 3979 * enough "charge" left in the SA to protect the data. Return B_FALSE 3980 * otherwise. (If B_FALSE is returned, the association either was, or became 3981 * DEAD.) 3982 */ 3983 boolean_t 3984 sadb_age_bytes(queue_t *pfkey_q, ipsa_t *assoc, uint64_t bytes, 3985 boolean_t sendmsg) 3986 { 3987 boolean_t rc = B_TRUE; 3988 uint64_t newtotal; 3989 3990 mutex_enter(&assoc->ipsa_lock); 3991 newtotal = assoc->ipsa_bytes + bytes; 3992 if (assoc->ipsa_hardbyteslt != 0 && 3993 newtotal >= assoc->ipsa_hardbyteslt) { 3994 if (assoc->ipsa_state != IPSA_STATE_DEAD) { 3995 sadb_delete_cluster(assoc); 3996 /* 3997 * Send EXPIRE message to PF_KEY. May wish to pawn 3998 * this off on another non-interrupt thread. Also 3999 * unlink this SA immediately. 4000 */ 4001 assoc->ipsa_state = IPSA_STATE_DEAD; 4002 if (sendmsg) 4003 sadb_expire_assoc(pfkey_q, assoc); 4004 /* 4005 * Set non-zero expiration time so sadb_age_assoc() 4006 * will work when reaping. 4007 */ 4008 assoc->ipsa_hardexpiretime = (time_t)1; 4009 } /* Else someone beat me to it! */ 4010 rc = B_FALSE; 4011 } else if (assoc->ipsa_softbyteslt != 0 && 4012 (newtotal >= assoc->ipsa_softbyteslt)) { 4013 if (assoc->ipsa_state < IPSA_STATE_DYING) { 4014 /* 4015 * Send EXPIRE message to PF_KEY. May wish to pawn 4016 * this off on another non-interrupt thread. 4017 */ 4018 assoc->ipsa_state = IPSA_STATE_DYING; 4019 assoc->ipsa_bytes = newtotal; 4020 if (sendmsg) 4021 sadb_expire_assoc(pfkey_q, assoc); 4022 } /* Else someone beat me to it! */ 4023 } 4024 if (rc == B_TRUE) 4025 assoc->ipsa_bytes = newtotal; 4026 mutex_exit(&assoc->ipsa_lock); 4027 return (rc); 4028 } 4029 4030 /* 4031 * Push one or more DL_CO_DELETE messages queued up by 4032 * sadb_torch_assoc down to the underlying driver now that it's a 4033 * convenient time for it (i.e., ipsa bucket locks not held). 4034 */ 4035 static void 4036 sadb_drain_torchq(queue_t *q, mblk_t *mp) 4037 { 4038 while (mp != NULL) { 4039 mblk_t *next = mp->b_next; 4040 mp->b_next = NULL; 4041 if (q != NULL) 4042 putnext(q, mp); 4043 else 4044 freemsg(mp); 4045 mp = next; 4046 } 4047 } 4048 4049 /* 4050 * "Torch" an individual SA. Returns NULL, so it can be tail-called from 4051 * sadb_age_assoc(). 4052 * 4053 * If SA is hardware-accelerated, and we can't allocate the mblk 4054 * containing the DL_CO_DELETE, just return; it will remain in the 4055 * table and be swept up by sadb_ager() in a subsequent pass. 4056 */ 4057 static ipsa_t * 4058 sadb_torch_assoc(isaf_t *head, ipsa_t *sa, boolean_t inbnd, mblk_t **mq) 4059 { 4060 mblk_t *mp; 4061 4062 ASSERT(MUTEX_HELD(&head->isaf_lock)); 4063 ASSERT(MUTEX_HELD(&sa->ipsa_lock)); 4064 ASSERT(sa->ipsa_state == IPSA_STATE_DEAD); 4065 4066 /* 4067 * Force cached SAs to be revalidated.. 4068 */ 4069 head->isaf_gen++; 4070 4071 if (sa->ipsa_flags & IPSA_F_HW) { 4072 mp = sadb_fmt_sa_req(DL_CO_DELETE, sa->ipsa_type, sa, inbnd); 4073 if (mp == NULL) { 4074 mutex_exit(&sa->ipsa_lock); 4075 return (NULL); 4076 } 4077 mp->b_next = *mq; 4078 *mq = mp; 4079 } 4080 mutex_exit(&sa->ipsa_lock); 4081 sadb_unlinkassoc(sa); 4082 4083 return (NULL); 4084 } 4085 4086 /* 4087 * Do various SA-is-idle activities depending on delta (the number of idle 4088 * seconds on the SA) and/or other properties of the SA. 4089 * 4090 * Return B_TRUE if I've sent a packet, because I have to drop the 4091 * association's mutex before sending a packet out the wire. 4092 */ 4093 /* ARGSUSED */ 4094 static boolean_t 4095 sadb_idle_activities(ipsa_t *assoc, time_t delta, boolean_t inbound) 4096 { 4097 ipsecesp_stack_t *espstack = assoc->ipsa_netstack->netstack_ipsecesp; 4098 int nat_t_interval = espstack->ipsecesp_nat_keepalive_interval; 4099 4100 ASSERT(MUTEX_HELD(&assoc->ipsa_lock)); 4101 4102 if (!inbound && (assoc->ipsa_flags & IPSA_F_NATT_LOC) && 4103 delta >= nat_t_interval && 4104 gethrestime_sec() - assoc->ipsa_last_nat_t_ka >= nat_t_interval) { 4105 ASSERT(assoc->ipsa_type == SADB_SATYPE_ESP); 4106 assoc->ipsa_last_nat_t_ka = gethrestime_sec(); 4107 mutex_exit(&assoc->ipsa_lock); 4108 ipsecesp_send_keepalive(assoc); 4109 return (B_TRUE); 4110 } 4111 return (B_FALSE); 4112 } 4113 4114 /* 4115 * Return "assoc" if haspeer is true and I send an expire. This allows 4116 * the consumers' aging functions to tidy up an expired SA's peer. 4117 */ 4118 static ipsa_t * 4119 sadb_age_assoc(isaf_t *head, queue_t *pfkey_q, ipsa_t *assoc, 4120 time_t current, int reap_delay, boolean_t inbound, mblk_t **mq) 4121 { 4122 ipsa_t *retval = NULL; 4123 boolean_t dropped_mutex = B_FALSE; 4124 4125 ASSERT(MUTEX_HELD(&head->isaf_lock)); 4126 4127 mutex_enter(&assoc->ipsa_lock); 4128 4129 if (((assoc->ipsa_state == IPSA_STATE_LARVAL) || 4130 ((assoc->ipsa_state == IPSA_STATE_IDLE) || 4131 (assoc->ipsa_state == IPSA_STATE_ACTIVE_ELSEWHERE) && 4132 (assoc->ipsa_hardexpiretime != 0))) && 4133 (assoc->ipsa_hardexpiretime <= current)) { 4134 assoc->ipsa_state = IPSA_STATE_DEAD; 4135 return (sadb_torch_assoc(head, assoc, inbound, mq)); 4136 } 4137 4138 /* 4139 * Check lifetimes. Fortunately, SA setup is done 4140 * such that there are only two times to look at, 4141 * softexpiretime, and hardexpiretime. 4142 * 4143 * Check hard first. 4144 */ 4145 4146 if (assoc->ipsa_hardexpiretime != 0 && 4147 assoc->ipsa_hardexpiretime <= current) { 4148 if (assoc->ipsa_state == IPSA_STATE_DEAD) 4149 return (sadb_torch_assoc(head, assoc, inbound, mq)); 4150 4151 if (inbound) { 4152 sadb_delete_cluster(assoc); 4153 } 4154 4155 /* 4156 * Send SADB_EXPIRE with hard lifetime, delay for unlinking. 4157 */ 4158 assoc->ipsa_state = IPSA_STATE_DEAD; 4159 if (assoc->ipsa_haspeer || assoc->ipsa_otherspi != 0) { 4160 /* 4161 * If the SA is paired or peered with another, put 4162 * a copy on a list which can be processed later, the 4163 * pair/peer SA needs to be updated so the both die 4164 * at the same time. 4165 * 4166 * If I return assoc, I have to bump up its reference 4167 * count to keep with the ipsa_t reference count 4168 * semantics. 4169 */ 4170 IPSA_REFHOLD(assoc); 4171 retval = assoc; 4172 } 4173 sadb_expire_assoc(pfkey_q, assoc); 4174 assoc->ipsa_hardexpiretime = current + reap_delay; 4175 } else if (assoc->ipsa_softexpiretime != 0 && 4176 assoc->ipsa_softexpiretime <= current && 4177 assoc->ipsa_state < IPSA_STATE_DYING) { 4178 /* 4179 * Send EXPIRE message to PF_KEY. May wish to pawn 4180 * this off on another non-interrupt thread. 4181 */ 4182 assoc->ipsa_state = IPSA_STATE_DYING; 4183 if (assoc->ipsa_haspeer) { 4184 /* 4185 * If the SA has a peer, update the peer's state 4186 * on SOFT_EXPIRE, this is mostly to prevent two 4187 * expire messages from effectively the same SA. 4188 * 4189 * Don't care about paired SA's, then can (and should) 4190 * be able to soft expire at different times. 4191 * 4192 * If I return assoc, I have to bump up its 4193 * reference count to keep with the ipsa_t reference 4194 * count semantics. 4195 */ 4196 IPSA_REFHOLD(assoc); 4197 retval = assoc; 4198 } 4199 sadb_expire_assoc(pfkey_q, assoc); 4200 } else if (assoc->ipsa_idletime != 0 && 4201 assoc->ipsa_idleexpiretime <= current) { 4202 if (assoc->ipsa_state == IPSA_STATE_ACTIVE_ELSEWHERE) { 4203 assoc->ipsa_state = IPSA_STATE_IDLE; 4204 } 4205 4206 /* 4207 * Need to handle Mature case 4208 */ 4209 if (assoc->ipsa_state == IPSA_STATE_MATURE) { 4210 sadb_expire_assoc(pfkey_q, assoc); 4211 } 4212 } else { 4213 /* Check idle time activities. */ 4214 dropped_mutex = sadb_idle_activities(assoc, 4215 current - assoc->ipsa_lastuse, inbound); 4216 } 4217 4218 if (!dropped_mutex) 4219 mutex_exit(&assoc->ipsa_lock); 4220 return (retval); 4221 } 4222 4223 /* 4224 * Called by a consumer protocol to do ther dirty work of reaping dead 4225 * Security Associations. 4226 * 4227 * NOTE: sadb_age_assoc() marks expired SA's as DEAD but only removed 4228 * SA's that are already marked DEAD, so expired SA's are only reaped 4229 * the second time sadb_ager() runs. 4230 */ 4231 void 4232 sadb_ager(sadb_t *sp, queue_t *pfkey_q, queue_t *ip_q, int reap_delay, 4233 netstack_t *ns) 4234 { 4235 int i; 4236 isaf_t *bucket; 4237 ipsa_t *assoc, *spare; 4238 iacqf_t *acqlist; 4239 ipsacq_t *acqrec, *spareacq; 4240 templist_t *haspeerlist, *newbie; 4241 /* Snapshot current time now. */ 4242 time_t current = gethrestime_sec(); 4243 mblk_t *mq = NULL; 4244 haspeerlist = NULL; 4245 4246 /* 4247 * Do my dirty work. This includes aging real entries, aging 4248 * larvals, and aging outstanding ACQUIREs. 4249 * 4250 * I hope I don't tie up resources for too long. 4251 */ 4252 4253 /* Age acquires. */ 4254 4255 for (i = 0; i < sp->sdb_hashsize; i++) { 4256 acqlist = &sp->sdb_acq[i]; 4257 mutex_enter(&acqlist->iacqf_lock); 4258 for (acqrec = acqlist->iacqf_ipsacq; acqrec != NULL; 4259 acqrec = spareacq) { 4260 spareacq = acqrec->ipsacq_next; 4261 if (current > acqrec->ipsacq_expire) 4262 sadb_destroy_acquire(acqrec, ns); 4263 } 4264 mutex_exit(&acqlist->iacqf_lock); 4265 } 4266 4267 /* Age inbound associations. */ 4268 for (i = 0; i < sp->sdb_hashsize; i++) { 4269 bucket = &(sp->sdb_if[i]); 4270 mutex_enter(&bucket->isaf_lock); 4271 for (assoc = bucket->isaf_ipsa; assoc != NULL; 4272 assoc = spare) { 4273 spare = assoc->ipsa_next; 4274 if (sadb_age_assoc(bucket, pfkey_q, assoc, current, 4275 reap_delay, B_TRUE, &mq) != NULL) { 4276 /* 4277 * Put SA's which have a peer or SA's which 4278 * are paired on a list for processing after 4279 * all the hash tables have been walked. 4280 * 4281 * sadb_age_assoc() increments the refcnt, 4282 * effectively doing an IPSA_REFHOLD(). 4283 */ 4284 newbie = kmem_alloc(sizeof (*newbie), 4285 KM_NOSLEEP); 4286 if (newbie == NULL) { 4287 /* 4288 * Don't forget to REFRELE(). 4289 */ 4290 IPSA_REFRELE(assoc); 4291 continue; /* for loop... */ 4292 } 4293 newbie->next = haspeerlist; 4294 newbie->ipsa = assoc; 4295 haspeerlist = newbie; 4296 } 4297 } 4298 mutex_exit(&bucket->isaf_lock); 4299 } 4300 4301 if (mq != NULL) { 4302 sadb_drain_torchq(ip_q, mq); 4303 mq = NULL; 4304 } 4305 age_pair_peer_list(haspeerlist, sp, B_FALSE); 4306 haspeerlist = NULL; 4307 4308 /* Age outbound associations. */ 4309 for (i = 0; i < sp->sdb_hashsize; i++) { 4310 bucket = &(sp->sdb_of[i]); 4311 mutex_enter(&bucket->isaf_lock); 4312 for (assoc = bucket->isaf_ipsa; assoc != NULL; 4313 assoc = spare) { 4314 spare = assoc->ipsa_next; 4315 if (sadb_age_assoc(bucket, pfkey_q, assoc, current, 4316 reap_delay, B_FALSE, &mq) != NULL) { 4317 /* 4318 * sadb_age_assoc() increments the refcnt, 4319 * effectively doing an IPSA_REFHOLD(). 4320 */ 4321 newbie = kmem_alloc(sizeof (*newbie), 4322 KM_NOSLEEP); 4323 if (newbie == NULL) { 4324 /* 4325 * Don't forget to REFRELE(). 4326 */ 4327 IPSA_REFRELE(assoc); 4328 continue; /* for loop... */ 4329 } 4330 newbie->next = haspeerlist; 4331 newbie->ipsa = assoc; 4332 haspeerlist = newbie; 4333 } 4334 } 4335 mutex_exit(&bucket->isaf_lock); 4336 } 4337 if (mq != NULL) { 4338 sadb_drain_torchq(ip_q, mq); 4339 mq = NULL; 4340 } 4341 4342 age_pair_peer_list(haspeerlist, sp, B_TRUE); 4343 4344 /* 4345 * Run a GC pass to clean out dead identities. 4346 */ 4347 ipsid_gc(ns); 4348 } 4349 4350 /* 4351 * Figure out when to reschedule the ager. 4352 */ 4353 timeout_id_t 4354 sadb_retimeout(hrtime_t begin, queue_t *pfkey_q, void (*ager)(void *), 4355 void *agerarg, uint_t *intp, uint_t intmax, short mid) 4356 { 4357 hrtime_t end = gethrtime(); 4358 uint_t interval = *intp; 4359 4360 /* 4361 * See how long this took. If it took too long, increase the 4362 * aging interval. 4363 */ 4364 if ((end - begin) > interval * 1000000) { 4365 if (interval >= intmax) { 4366 /* XXX Rate limit this? Or recommend flush? */ 4367 (void) strlog(mid, 0, 0, SL_ERROR | SL_WARN, 4368 "Too many SA's to age out in %d msec.\n", 4369 intmax); 4370 } else { 4371 /* Double by shifting by one bit. */ 4372 interval <<= 1; 4373 interval = min(interval, intmax); 4374 } 4375 } else if ((end - begin) <= interval * 500000 && 4376 interval > SADB_AGE_INTERVAL_DEFAULT) { 4377 /* 4378 * If I took less than half of the interval, then I should 4379 * ratchet the interval back down. Never automatically 4380 * shift below the default aging interval. 4381 * 4382 * NOTE:This even overrides manual setting of the age 4383 * interval using NDD. 4384 */ 4385 /* Halve by shifting one bit. */ 4386 interval >>= 1; 4387 interval = max(interval, SADB_AGE_INTERVAL_DEFAULT); 4388 } 4389 *intp = interval; 4390 return (qtimeout(pfkey_q, ager, agerarg, 4391 interval * drv_usectohz(1000))); 4392 } 4393 4394 4395 /* 4396 * Update the lifetime values of an SA. This is the path an SADB_UPDATE 4397 * message takes when updating a MATURE or DYING SA. 4398 */ 4399 static void 4400 sadb_update_lifetimes(ipsa_t *assoc, sadb_lifetime_t *hard, 4401 sadb_lifetime_t *soft, sadb_lifetime_t *idle, boolean_t outbound) 4402 { 4403 mutex_enter(&assoc->ipsa_lock); 4404 4405 /* 4406 * XXX RFC 2367 mentions how an SADB_EXT_LIFETIME_CURRENT can be 4407 * passed in during an update message. We currently don't handle 4408 * these. 4409 */ 4410 4411 if (hard != NULL) { 4412 if (hard->sadb_lifetime_bytes != 0) 4413 assoc->ipsa_hardbyteslt = hard->sadb_lifetime_bytes; 4414 if (hard->sadb_lifetime_usetime != 0) 4415 assoc->ipsa_harduselt = hard->sadb_lifetime_usetime; 4416 if (hard->sadb_lifetime_addtime != 0) 4417 assoc->ipsa_hardaddlt = hard->sadb_lifetime_addtime; 4418 if (assoc->ipsa_hardaddlt != 0) { 4419 assoc->ipsa_hardexpiretime = 4420 assoc->ipsa_addtime + assoc->ipsa_hardaddlt; 4421 } 4422 if (assoc->ipsa_harduselt != 0 && 4423 assoc->ipsa_flags & IPSA_F_USED) { 4424 UPDATE_EXPIRE(assoc, harduselt, hardexpiretime); 4425 } 4426 if (hard->sadb_lifetime_allocations != 0) 4427 assoc->ipsa_hardalloc = hard->sadb_lifetime_allocations; 4428 } 4429 4430 if (soft != NULL) { 4431 if (soft->sadb_lifetime_bytes != 0) { 4432 if (soft->sadb_lifetime_bytes > 4433 assoc->ipsa_hardbyteslt) { 4434 assoc->ipsa_softbyteslt = 4435 assoc->ipsa_hardbyteslt; 4436 } else { 4437 assoc->ipsa_softbyteslt = 4438 soft->sadb_lifetime_bytes; 4439 } 4440 } 4441 if (soft->sadb_lifetime_usetime != 0) { 4442 if (soft->sadb_lifetime_usetime > 4443 assoc->ipsa_harduselt) { 4444 assoc->ipsa_softuselt = 4445 assoc->ipsa_harduselt; 4446 } else { 4447 assoc->ipsa_softuselt = 4448 soft->sadb_lifetime_usetime; 4449 } 4450 } 4451 if (soft->sadb_lifetime_addtime != 0) { 4452 if (soft->sadb_lifetime_addtime > 4453 assoc->ipsa_hardexpiretime) { 4454 assoc->ipsa_softexpiretime = 4455 assoc->ipsa_hardexpiretime; 4456 } else { 4457 assoc->ipsa_softaddlt = 4458 soft->sadb_lifetime_addtime; 4459 } 4460 } 4461 if (assoc->ipsa_softaddlt != 0) { 4462 assoc->ipsa_softexpiretime = 4463 assoc->ipsa_addtime + assoc->ipsa_softaddlt; 4464 } 4465 if (assoc->ipsa_softuselt != 0 && 4466 assoc->ipsa_flags & IPSA_F_USED) { 4467 UPDATE_EXPIRE(assoc, softuselt, softexpiretime); 4468 } 4469 if (outbound && assoc->ipsa_softexpiretime != 0) { 4470 if (assoc->ipsa_state == IPSA_STATE_MATURE) 4471 lifetime_fuzz(assoc); 4472 } 4473 4474 if (soft->sadb_lifetime_allocations != 0) 4475 assoc->ipsa_softalloc = soft->sadb_lifetime_allocations; 4476 } 4477 4478 if (idle != NULL) { 4479 time_t current = gethrestime_sec(); 4480 if ((assoc->ipsa_idleexpiretime <= current) && 4481 (assoc->ipsa_idleaddlt == idle->sadb_lifetime_addtime)) { 4482 assoc->ipsa_idleexpiretime = 4483 current + assoc->ipsa_idleaddlt; 4484 } 4485 if (idle->sadb_lifetime_addtime != 0) 4486 assoc->ipsa_idleaddlt = idle->sadb_lifetime_addtime; 4487 if (idle->sadb_lifetime_usetime != 0) 4488 assoc->ipsa_idleuselt = idle->sadb_lifetime_usetime; 4489 if (assoc->ipsa_idleaddlt != 0) { 4490 assoc->ipsa_idleexpiretime = 4491 current + idle->sadb_lifetime_addtime; 4492 assoc->ipsa_idletime = idle->sadb_lifetime_addtime; 4493 } 4494 if (assoc->ipsa_idleuselt != 0) { 4495 if (assoc->ipsa_idletime != 0) { 4496 assoc->ipsa_idletime = min(assoc->ipsa_idletime, 4497 assoc->ipsa_idleuselt); 4498 assoc->ipsa_idleexpiretime = 4499 current + assoc->ipsa_idletime; 4500 } else { 4501 assoc->ipsa_idleexpiretime = 4502 current + assoc->ipsa_idleuselt; 4503 assoc->ipsa_idletime = assoc->ipsa_idleuselt; 4504 } 4505 } 4506 } 4507 mutex_exit(&assoc->ipsa_lock); 4508 } 4509 4510 static int 4511 sadb_update_state(ipsa_t *assoc, uint_t new_state, mblk_t **ipkt_lst) 4512 { 4513 int rcode = 0; 4514 time_t current = gethrestime_sec(); 4515 4516 mutex_enter(&assoc->ipsa_lock); 4517 4518 switch (new_state) { 4519 case SADB_X_SASTATE_ACTIVE_ELSEWHERE: 4520 if (assoc->ipsa_state == SADB_X_SASTATE_IDLE) { 4521 assoc->ipsa_state = IPSA_STATE_ACTIVE_ELSEWHERE; 4522 assoc->ipsa_idleexpiretime = 4523 current + assoc->ipsa_idletime; 4524 } 4525 break; 4526 case SADB_X_SASTATE_IDLE: 4527 if (assoc->ipsa_state == SADB_X_SASTATE_ACTIVE_ELSEWHERE) { 4528 assoc->ipsa_state = IPSA_STATE_IDLE; 4529 assoc->ipsa_idleexpiretime = 4530 current + assoc->ipsa_idletime; 4531 } else { 4532 rcode = EINVAL; 4533 } 4534 break; 4535 4536 case SADB_X_SASTATE_ACTIVE: 4537 if (assoc->ipsa_state != SADB_X_SASTATE_IDLE) { 4538 rcode = EINVAL; 4539 break; 4540 } 4541 assoc->ipsa_state = IPSA_STATE_MATURE; 4542 assoc->ipsa_idleexpiretime = current + assoc->ipsa_idletime; 4543 4544 if (ipkt_lst == NULL) { 4545 break; 4546 } 4547 4548 if (assoc->ipsa_bpkt_head != NULL) { 4549 *ipkt_lst = assoc->ipsa_bpkt_head; 4550 assoc->ipsa_bpkt_head = assoc->ipsa_bpkt_tail = NULL; 4551 assoc->ipsa_mblkcnt = 0; 4552 } else { 4553 *ipkt_lst = NULL; 4554 } 4555 break; 4556 default: 4557 rcode = EINVAL; 4558 break; 4559 } 4560 4561 mutex_exit(&assoc->ipsa_lock); 4562 return (rcode); 4563 } 4564 4565 /* 4566 * Common code to update an SA. 4567 */ 4568 4569 int 4570 sadb_update_sa(mblk_t *mp, keysock_in_t *ksi, mblk_t **ipkt_lst, 4571 sadbp_t *spp, int *diagnostic, queue_t *pfkey_q, 4572 int (*add_sa_func)(mblk_t *, keysock_in_t *, int *, netstack_t *), 4573 netstack_t *ns, uint8_t sadb_msg_type) 4574 { 4575 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA]; 4576 sadb_address_t *srcext = 4577 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC]; 4578 sadb_address_t *dstext = 4579 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 4580 sadb_x_kmc_t *kmcext = 4581 (sadb_x_kmc_t *)ksi->ks_in_extv[SADB_X_EXT_KM_COOKIE]; 4582 sadb_key_t *akey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH]; 4583 sadb_key_t *ekey = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT]; 4584 sadb_x_replay_ctr_t *replext = 4585 (sadb_x_replay_ctr_t *)ksi->ks_in_extv[SADB_X_EXT_REPLAY_VALUE]; 4586 sadb_lifetime_t *soft = 4587 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT]; 4588 sadb_lifetime_t *hard = 4589 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD]; 4590 sadb_lifetime_t *idle = 4591 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_X_EXT_LIFETIME_IDLE]; 4592 sadb_x_pair_t *pair_ext = 4593 (sadb_x_pair_t *)ksi->ks_in_extv[SADB_X_EXT_PAIR]; 4594 ipsa_t *echo_target = NULL; 4595 int error = 0; 4596 ipsap_t *ipsapp = NULL; 4597 uint32_t kmp = 0, kmc = 0; 4598 time_t current = gethrestime_sec(); 4599 4600 4601 /* I need certain extensions present for either UPDATE message. */ 4602 if (srcext == NULL) { 4603 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC; 4604 return (EINVAL); 4605 } 4606 if (dstext == NULL) { 4607 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST; 4608 return (EINVAL); 4609 } 4610 if (assoc == NULL) { 4611 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA; 4612 return (EINVAL); 4613 } 4614 4615 if (kmcext != NULL) { 4616 kmp = kmcext->sadb_x_kmc_proto; 4617 kmc = kmcext->sadb_x_kmc_cookie; 4618 } 4619 4620 ipsapp = get_ipsa_pair(assoc, srcext, dstext, spp); 4621 if (ipsapp == NULL) { 4622 *diagnostic = SADB_X_DIAGNOSTIC_SA_NOTFOUND; 4623 return (ESRCH); 4624 } 4625 4626 if (ipsapp->ipsap_psa_ptr == NULL && ipsapp->ipsap_sa_ptr != NULL) { 4627 if (ipsapp->ipsap_sa_ptr->ipsa_state == IPSA_STATE_LARVAL) { 4628 /* 4629 * REFRELE the target and let the add_sa_func() 4630 * deal with updating a larval SA. 4631 */ 4632 destroy_ipsa_pair(ipsapp); 4633 return (add_sa_func(mp, ksi, diagnostic, ns)); 4634 } 4635 } 4636 4637 if (assoc->sadb_sa_state == SADB_X_SASTATE_ACTIVE_ELSEWHERE) { 4638 if (ipsapp->ipsap_sa_ptr != NULL && 4639 ipsapp->ipsap_sa_ptr->ipsa_state == IPSA_STATE_IDLE) { 4640 if ((error = sadb_update_state(ipsapp->ipsap_sa_ptr, 4641 assoc->sadb_sa_state, NULL)) != 0) { 4642 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE; 4643 goto bail; 4644 } 4645 } 4646 if (ipsapp->ipsap_psa_ptr != NULL && 4647 ipsapp->ipsap_psa_ptr->ipsa_state == IPSA_STATE_IDLE) { 4648 if ((error = sadb_update_state(ipsapp->ipsap_psa_ptr, 4649 assoc->sadb_sa_state, NULL)) != 0) { 4650 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE; 4651 goto bail; 4652 } 4653 } 4654 } 4655 if (assoc->sadb_sa_state == SADB_X_SASTATE_ACTIVE) { 4656 if (ipsapp->ipsap_sa_ptr != NULL) { 4657 error = sadb_update_state(ipsapp->ipsap_sa_ptr, 4658 assoc->sadb_sa_state, 4659 (ipsapp->ipsap_sa_ptr->ipsa_flags & 4660 IPSA_F_INBOUND) ? ipkt_lst : NULL); 4661 if (error) { 4662 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE; 4663 goto bail; 4664 } 4665 } 4666 if (ipsapp->ipsap_psa_ptr != NULL) { 4667 error = sadb_update_state(ipsapp->ipsap_psa_ptr, 4668 assoc->sadb_sa_state, 4669 (ipsapp->ipsap_psa_ptr->ipsa_flags & 4670 IPSA_F_INBOUND) ? ipkt_lst : NULL); 4671 if (error) { 4672 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE; 4673 goto bail; 4674 } 4675 } 4676 sadb_pfkey_echo(pfkey_q, mp, (sadb_msg_t *)mp->b_cont->b_rptr, 4677 ksi, echo_target); 4678 goto bail; 4679 } 4680 4681 /* 4682 * Reality checks for updates of active associations. 4683 * Sundry first-pass UPDATE-specific reality checks. 4684 * Have to do the checks here, because it's after the add_sa code. 4685 * XXX STATS : logging/stats here? 4686 */ 4687 4688 if (!((assoc->sadb_sa_state == SADB_SASTATE_MATURE) || 4689 (assoc->sadb_sa_state == SADB_X_SASTATE_ACTIVE_ELSEWHERE))) { 4690 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE; 4691 error = EINVAL; 4692 goto bail; 4693 } 4694 4695 if (assoc->sadb_sa_flags & ~spp->s_updateflags) { 4696 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS; 4697 error = EINVAL; 4698 goto bail; 4699 } 4700 4701 if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL) { 4702 error = EOPNOTSUPP; 4703 goto bail; 4704 } 4705 4706 if ((*diagnostic = sadb_hardsoftchk(hard, soft, idle)) != 0) { 4707 error = EINVAL; 4708 goto bail; 4709 } 4710 if (akey != NULL) { 4711 *diagnostic = SADB_X_DIAGNOSTIC_AKEY_PRESENT; 4712 error = EINVAL; 4713 goto bail; 4714 } 4715 if (ekey != NULL) { 4716 *diagnostic = SADB_X_DIAGNOSTIC_EKEY_PRESENT; 4717 error = EINVAL; 4718 goto bail; 4719 } 4720 4721 if (ipsapp->ipsap_sa_ptr != NULL) { 4722 if (ipsapp->ipsap_sa_ptr->ipsa_state == IPSA_STATE_DEAD) { 4723 error = ESRCH; /* DEAD == Not there, in this case. */ 4724 *diagnostic = SADB_X_DIAGNOSTIC_SA_EXPIRED; 4725 goto bail; 4726 } 4727 if ((kmp != 0) && 4728 ((ipsapp->ipsap_sa_ptr->ipsa_kmp != 0) || 4729 (ipsapp->ipsap_sa_ptr->ipsa_kmp != kmp))) { 4730 *diagnostic = SADB_X_DIAGNOSTIC_DUPLICATE_KMP; 4731 error = EINVAL; 4732 goto bail; 4733 } 4734 if ((kmc != 0) && 4735 ((ipsapp->ipsap_sa_ptr->ipsa_kmc != 0) || 4736 (ipsapp->ipsap_sa_ptr->ipsa_kmc != kmc))) { 4737 *diagnostic = SADB_X_DIAGNOSTIC_DUPLICATE_KMC; 4738 error = EINVAL; 4739 goto bail; 4740 } 4741 /* 4742 * Do not allow replay value change for MATURE or LARVAL SA. 4743 */ 4744 4745 if ((replext != NULL) && 4746 ((ipsapp->ipsap_sa_ptr->ipsa_state == IPSA_STATE_LARVAL) || 4747 (ipsapp->ipsap_sa_ptr->ipsa_state == IPSA_STATE_MATURE))) { 4748 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE; 4749 error = EINVAL; 4750 goto bail; 4751 } 4752 } 4753 4754 if (ipsapp->ipsap_psa_ptr != NULL) { 4755 if (ipsapp->ipsap_psa_ptr->ipsa_state == IPSA_STATE_DEAD) { 4756 *diagnostic = SADB_X_DIAGNOSTIC_SA_EXPIRED; 4757 error = ESRCH; /* DEAD == Not there, in this case. */ 4758 goto bail; 4759 } 4760 if ((kmp != 0) && 4761 ((ipsapp->ipsap_psa_ptr->ipsa_kmp != 0) || 4762 (ipsapp->ipsap_psa_ptr->ipsa_kmp != kmp))) { 4763 *diagnostic = SADB_X_DIAGNOSTIC_DUPLICATE_KMP; 4764 error = EINVAL; 4765 goto bail; 4766 } 4767 if ((kmc != 0) && 4768 ((ipsapp->ipsap_psa_ptr->ipsa_kmc != 0) || 4769 (ipsapp->ipsap_psa_ptr->ipsa_kmc != kmc))) { 4770 *diagnostic = SADB_X_DIAGNOSTIC_DUPLICATE_KMC; 4771 error = EINVAL; 4772 goto bail; 4773 } 4774 } 4775 4776 if (ipsapp->ipsap_sa_ptr != NULL) { 4777 sadb_update_lifetimes(ipsapp->ipsap_sa_ptr, hard, soft, 4778 idle, B_TRUE); 4779 if (kmp != 0) 4780 ipsapp->ipsap_sa_ptr->ipsa_kmp = kmp; 4781 if (kmc != 0) 4782 ipsapp->ipsap_sa_ptr->ipsa_kmc = kmc; 4783 if ((replext != NULL) && 4784 (ipsapp->ipsap_sa_ptr->ipsa_replay_wsize != 0)) { 4785 /* 4786 * If an inbound SA, update the replay counter 4787 * and check off all the other sequence number 4788 */ 4789 if (ksi->ks_in_dsttype == KS_IN_ADDR_ME) { 4790 if (!sadb_replay_check(ipsapp->ipsap_sa_ptr, 4791 replext->sadb_x_rc_replay32)) { 4792 error = EINVAL; 4793 goto bail; 4794 } 4795 mutex_enter(&ipsapp->ipsap_sa_ptr->ipsa_lock); 4796 ipsapp->ipsap_sa_ptr->ipsa_idleexpiretime = 4797 current + 4798 ipsapp->ipsap_sa_ptr->ipsa_idletime; 4799 mutex_exit(&ipsapp->ipsap_sa_ptr->ipsa_lock); 4800 } else { 4801 mutex_enter(&ipsapp->ipsap_sa_ptr->ipsa_lock); 4802 ipsapp->ipsap_sa_ptr->ipsa_replay = 4803 replext->sadb_x_rc_replay32; 4804 ipsapp->ipsap_sa_ptr->ipsa_idleexpiretime = 4805 current + 4806 ipsapp->ipsap_sa_ptr->ipsa_idletime; 4807 mutex_exit(&ipsapp->ipsap_sa_ptr->ipsa_lock); 4808 } 4809 } 4810 } 4811 4812 if (sadb_msg_type == SADB_X_UPDATEPAIR) { 4813 if (ipsapp->ipsap_psa_ptr != NULL) { 4814 sadb_update_lifetimes(ipsapp->ipsap_psa_ptr, hard, soft, 4815 idle, B_FALSE); 4816 if (kmp != 0) 4817 ipsapp->ipsap_psa_ptr->ipsa_kmp = kmp; 4818 if (kmc != 0) 4819 ipsapp->ipsap_psa_ptr->ipsa_kmc = kmc; 4820 } else { 4821 *diagnostic = SADB_X_DIAGNOSTIC_PAIR_SA_NOTFOUND; 4822 error = ESRCH; 4823 goto bail; 4824 } 4825 } 4826 4827 if (pair_ext != NULL) 4828 error = update_pairing(ipsapp, ksi, diagnostic, spp); 4829 4830 if (error == 0) 4831 sadb_pfkey_echo(pfkey_q, mp, (sadb_msg_t *)mp->b_cont->b_rptr, 4832 ksi, echo_target); 4833 bail: 4834 4835 destroy_ipsa_pair(ipsapp); 4836 4837 return (error); 4838 } 4839 4840 4841 int 4842 update_pairing(ipsap_t *ipsapp, keysock_in_t *ksi, int *diagnostic, 4843 sadbp_t *spp) 4844 { 4845 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA]; 4846 sadb_address_t *srcext = 4847 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC]; 4848 sadb_address_t *dstext = 4849 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 4850 sadb_x_pair_t *pair_ext = 4851 (sadb_x_pair_t *)ksi->ks_in_extv[SADB_X_EXT_PAIR]; 4852 int error = 0; 4853 ipsap_t *oipsapp = NULL; 4854 boolean_t undo_pair = B_FALSE; 4855 uint32_t ipsa_flags; 4856 4857 if (pair_ext->sadb_x_pair_spi == 0 || pair_ext->sadb_x_pair_spi == 4858 assoc->sadb_sa_spi) { 4859 *diagnostic = SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE; 4860 return (EINVAL); 4861 } 4862 4863 /* 4864 * Assume for now that the spi value provided in the SADB_UPDATE 4865 * message was valid, update the SA with its pair spi value. 4866 * If the spi turns out to be bogus or the SA no longer exists 4867 * then this will be detected when the reverse update is made 4868 * below. 4869 */ 4870 mutex_enter(&ipsapp->ipsap_sa_ptr->ipsa_lock); 4871 ipsapp->ipsap_sa_ptr->ipsa_flags |= IPSA_F_PAIRED; 4872 ipsapp->ipsap_sa_ptr->ipsa_otherspi = pair_ext->sadb_x_pair_spi; 4873 mutex_exit(&ipsapp->ipsap_sa_ptr->ipsa_lock); 4874 4875 /* 4876 * After updating the ipsa_otherspi element of the SA, get_ipsa_pair() 4877 * should now return pointers to the SA *AND* its pair, if this is not 4878 * the case, the "otherspi" either did not exist or was deleted. Also 4879 * check that "otherspi" is not already paired. If everything looks 4880 * good, complete the update. IPSA_REFRELE the first pair_pointer 4881 * after this update to ensure its not deleted until we are done. 4882 */ 4883 oipsapp = get_ipsa_pair(assoc, srcext, dstext, spp); 4884 if (oipsapp == NULL) { 4885 /* 4886 * This should never happen, calling function still has 4887 * IPSA_REFHELD on the SA we just updated. 4888 */ 4889 *diagnostic = SADB_X_DIAGNOSTIC_PAIR_SA_NOTFOUND; 4890 return (EINVAL); 4891 } 4892 4893 if (oipsapp->ipsap_psa_ptr == NULL) { 4894 *diagnostic = SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE; 4895 undo_pair = B_TRUE; 4896 } else { 4897 ipsa_flags = oipsapp->ipsap_psa_ptr->ipsa_flags; 4898 if ((oipsapp->ipsap_psa_ptr->ipsa_state == IPSA_STATE_DEAD) || 4899 (oipsapp->ipsap_psa_ptr->ipsa_state == IPSA_STATE_DYING)) { 4900 /* Its dead Jim! */ 4901 *diagnostic = SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE; 4902 undo_pair = B_TRUE; 4903 } else if ((ipsa_flags & (IPSA_F_OUTBOUND | IPSA_F_INBOUND)) == 4904 (IPSA_F_OUTBOUND | IPSA_F_INBOUND)) { 4905 /* This SA is in both hashtables. */ 4906 *diagnostic = SADB_X_DIAGNOSTIC_PAIR_INAPPROPRIATE; 4907 undo_pair = B_TRUE; 4908 } else if (ipsa_flags & IPSA_F_PAIRED) { 4909 /* This SA is already paired with another. */ 4910 *diagnostic = SADB_X_DIAGNOSTIC_PAIR_ALREADY; 4911 undo_pair = B_TRUE; 4912 } 4913 } 4914 4915 if (undo_pair) { 4916 /* The pair SA does not exist. */ 4917 mutex_enter(&ipsapp->ipsap_sa_ptr->ipsa_lock); 4918 ipsapp->ipsap_sa_ptr->ipsa_flags &= ~IPSA_F_PAIRED; 4919 ipsapp->ipsap_sa_ptr->ipsa_otherspi = 0; 4920 mutex_exit(&ipsapp->ipsap_sa_ptr->ipsa_lock); 4921 error = EINVAL; 4922 } else { 4923 mutex_enter(&oipsapp->ipsap_psa_ptr->ipsa_lock); 4924 oipsapp->ipsap_psa_ptr->ipsa_otherspi = assoc->sadb_sa_spi; 4925 oipsapp->ipsap_psa_ptr->ipsa_flags |= IPSA_F_PAIRED; 4926 mutex_exit(&oipsapp->ipsap_psa_ptr->ipsa_lock); 4927 } 4928 4929 destroy_ipsa_pair(oipsapp); 4930 return (error); 4931 } 4932 4933 /* 4934 * The following functions deal with ACQUIRE LISTS. An ACQUIRE list is 4935 * a list of outstanding SADB_ACQUIRE messages. If ipsec_getassocbyconn() fails 4936 * for an outbound datagram, that datagram is queued up on an ACQUIRE record, 4937 * and an SADB_ACQUIRE message is sent up. Presumably, a user-space key 4938 * management daemon will process the ACQUIRE, use a SADB_GETSPI to reserve 4939 * an SPI value and a larval SA, then SADB_UPDATE the larval SA, and ADD the 4940 * other direction's SA. 4941 */ 4942 4943 /* 4944 * Check the ACQUIRE lists. If there's an existing ACQUIRE record, 4945 * grab it, lock it, and return it. Otherwise return NULL. 4946 */ 4947 static ipsacq_t * 4948 sadb_checkacquire(iacqf_t *bucket, ipsec_action_t *ap, ipsec_policy_t *pp, 4949 uint32_t *src, uint32_t *dst, uint32_t *isrc, uint32_t *idst, 4950 uint64_t unique_id) 4951 { 4952 ipsacq_t *walker; 4953 sa_family_t fam; 4954 uint32_t blank_address[4] = {0, 0, 0, 0}; 4955 4956 if (isrc == NULL) { 4957 ASSERT(idst == NULL); 4958 isrc = idst = blank_address; 4959 } 4960 4961 /* 4962 * Scan list for duplicates. Check for UNIQUE, src/dest, policy. 4963 * 4964 * XXX May need search for duplicates based on other things too! 4965 */ 4966 for (walker = bucket->iacqf_ipsacq; walker != NULL; 4967 walker = walker->ipsacq_next) { 4968 mutex_enter(&walker->ipsacq_lock); 4969 fam = walker->ipsacq_addrfam; 4970 if (IPSA_ARE_ADDR_EQUAL(dst, walker->ipsacq_dstaddr, fam) && 4971 IPSA_ARE_ADDR_EQUAL(src, walker->ipsacq_srcaddr, fam) && 4972 ip_addr_match((uint8_t *)isrc, walker->ipsacq_innersrcpfx, 4973 (in6_addr_t *)walker->ipsacq_innersrc) && 4974 ip_addr_match((uint8_t *)idst, walker->ipsacq_innerdstpfx, 4975 (in6_addr_t *)walker->ipsacq_innerdst) && 4976 (ap == walker->ipsacq_act) && 4977 (pp == walker->ipsacq_policy) && 4978 /* XXX do deep compares of ap/pp? */ 4979 (unique_id == walker->ipsacq_unique_id)) 4980 break; /* everything matched */ 4981 mutex_exit(&walker->ipsacq_lock); 4982 } 4983 4984 return (walker); 4985 } 4986 4987 /* 4988 * For this mblk, insert a new acquire record. Assume bucket contains addrs 4989 * of all of the same length. Give up (and drop) if memory 4990 * cannot be allocated for a new one; otherwise, invoke callback to 4991 * send the acquire up.. 4992 * 4993 * In cases where we need both AH and ESP, add the SA to the ESP ACQUIRE 4994 * list. The ah_add_sa_finish() routines can look at the packet's ipsec_out_t 4995 * and handle this case specially. 4996 */ 4997 void 4998 sadb_acquire(mblk_t *mp, ipsec_out_t *io, boolean_t need_ah, boolean_t need_esp) 4999 { 5000 sadbp_t *spp; 5001 sadb_t *sp; 5002 ipsacq_t *newbie; 5003 iacqf_t *bucket; 5004 mblk_t *datamp = mp->b_cont; 5005 mblk_t *extended; 5006 ipha_t *ipha = (ipha_t *)datamp->b_rptr; 5007 ip6_t *ip6h = (ip6_t *)datamp->b_rptr; 5008 uint32_t *src, *dst, *isrc, *idst; 5009 ipsec_policy_t *pp = io->ipsec_out_policy; 5010 ipsec_action_t *ap = io->ipsec_out_act; 5011 sa_family_t af; 5012 int hashoffset; 5013 uint32_t seq; 5014 uint64_t unique_id = 0; 5015 ipsec_selector_t sel; 5016 boolean_t tunnel_mode = io->ipsec_out_tunnel; 5017 netstack_t *ns = io->ipsec_out_ns; 5018 ipsec_stack_t *ipss = ns->netstack_ipsec; 5019 5020 ASSERT((pp != NULL) || (ap != NULL)); 5021 5022 ASSERT(need_ah != NULL || need_esp != NULL); 5023 /* Assign sadb pointers */ 5024 if (need_esp) { /* ESP for AH+ESP */ 5025 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 5026 5027 spp = &espstack->esp_sadb; 5028 } else { 5029 ipsecah_stack_t *ahstack = ns->netstack_ipsecah; 5030 5031 spp = &ahstack->ah_sadb; 5032 } 5033 sp = io->ipsec_out_v4 ? &spp->s_v4 : &spp->s_v6; 5034 5035 if (ap == NULL) 5036 ap = pp->ipsp_act; 5037 5038 ASSERT(ap != NULL); 5039 5040 if (ap->ipa_act.ipa_apply.ipp_use_unique || tunnel_mode) 5041 unique_id = SA_FORM_UNIQUE_ID(io); 5042 5043 /* 5044 * Set up an ACQUIRE record. 5045 * 5046 * Immediately, make sure the ACQUIRE sequence number doesn't slip 5047 * below the lowest point allowed in the kernel. (In other words, 5048 * make sure the high bit on the sequence number is set.) 5049 */ 5050 5051 seq = keysock_next_seq(ns) | IACQF_LOWEST_SEQ; 5052 5053 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 5054 src = (uint32_t *)&ipha->ipha_src; 5055 dst = (uint32_t *)&ipha->ipha_dst; 5056 af = AF_INET; 5057 hashoffset = OUTBOUND_HASH_V4(sp, ipha->ipha_dst); 5058 ASSERT(io->ipsec_out_v4 == B_TRUE); 5059 } else { 5060 ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION); 5061 src = (uint32_t *)&ip6h->ip6_src; 5062 dst = (uint32_t *)&ip6h->ip6_dst; 5063 af = AF_INET6; 5064 hashoffset = OUTBOUND_HASH_V6(sp, ip6h->ip6_dst); 5065 ASSERT(io->ipsec_out_v4 == B_FALSE); 5066 } 5067 5068 if (tunnel_mode) { 5069 /* Snag inner addresses. */ 5070 isrc = io->ipsec_out_insrc; 5071 idst = io->ipsec_out_indst; 5072 } else { 5073 isrc = idst = NULL; 5074 } 5075 5076 /* 5077 * Check buckets to see if there is an existing entry. If so, 5078 * grab it. sadb_checkacquire locks newbie if found. 5079 */ 5080 bucket = &(sp->sdb_acq[hashoffset]); 5081 mutex_enter(&bucket->iacqf_lock); 5082 newbie = sadb_checkacquire(bucket, ap, pp, src, dst, isrc, idst, 5083 unique_id); 5084 5085 if (newbie == NULL) { 5086 /* 5087 * Otherwise, allocate a new one. 5088 */ 5089 newbie = kmem_zalloc(sizeof (*newbie), KM_NOSLEEP); 5090 if (newbie == NULL) { 5091 mutex_exit(&bucket->iacqf_lock); 5092 ip_drop_packet(mp, B_FALSE, NULL, NULL, 5093 DROPPER(ipss, ipds_sadb_acquire_nomem), 5094 &ipss->ipsec_sadb_dropper); 5095 return; 5096 } 5097 newbie->ipsacq_policy = pp; 5098 if (pp != NULL) { 5099 IPPOL_REFHOLD(pp); 5100 } 5101 IPACT_REFHOLD(ap); 5102 newbie->ipsacq_act = ap; 5103 newbie->ipsacq_linklock = &bucket->iacqf_lock; 5104 newbie->ipsacq_next = bucket->iacqf_ipsacq; 5105 newbie->ipsacq_ptpn = &bucket->iacqf_ipsacq; 5106 if (newbie->ipsacq_next != NULL) 5107 newbie->ipsacq_next->ipsacq_ptpn = &newbie->ipsacq_next; 5108 bucket->iacqf_ipsacq = newbie; 5109 mutex_init(&newbie->ipsacq_lock, NULL, MUTEX_DEFAULT, NULL); 5110 mutex_enter(&newbie->ipsacq_lock); 5111 } 5112 5113 mutex_exit(&bucket->iacqf_lock); 5114 5115 /* 5116 * This assert looks silly for now, but we may need to enter newbie's 5117 * mutex during a search. 5118 */ 5119 ASSERT(MUTEX_HELD(&newbie->ipsacq_lock)); 5120 5121 mp->b_next = NULL; 5122 /* Queue up packet. Use b_next. */ 5123 if (newbie->ipsacq_numpackets == 0) { 5124 /* First one. */ 5125 newbie->ipsacq_mp = mp; 5126 newbie->ipsacq_numpackets = 1; 5127 newbie->ipsacq_expire = gethrestime_sec(); 5128 /* 5129 * Extended ACQUIRE with both AH+ESP will use ESP's timeout 5130 * value. 5131 */ 5132 newbie->ipsacq_expire += *spp->s_acquire_timeout; 5133 newbie->ipsacq_seq = seq; 5134 newbie->ipsacq_addrfam = af; 5135 5136 newbie->ipsacq_srcport = io->ipsec_out_src_port; 5137 newbie->ipsacq_dstport = io->ipsec_out_dst_port; 5138 newbie->ipsacq_icmp_type = io->ipsec_out_icmp_type; 5139 newbie->ipsacq_icmp_code = io->ipsec_out_icmp_code; 5140 if (tunnel_mode) { 5141 newbie->ipsacq_inneraddrfam = io->ipsec_out_inaf; 5142 newbie->ipsacq_proto = io->ipsec_out_inaf == AF_INET6 ? 5143 IPPROTO_IPV6 : IPPROTO_ENCAP; 5144 newbie->ipsacq_innersrcpfx = io->ipsec_out_insrcpfx; 5145 newbie->ipsacq_innerdstpfx = io->ipsec_out_indstpfx; 5146 IPSA_COPY_ADDR(newbie->ipsacq_innersrc, 5147 io->ipsec_out_insrc, io->ipsec_out_inaf); 5148 IPSA_COPY_ADDR(newbie->ipsacq_innerdst, 5149 io->ipsec_out_indst, io->ipsec_out_inaf); 5150 } else { 5151 newbie->ipsacq_proto = io->ipsec_out_proto; 5152 } 5153 newbie->ipsacq_unique_id = unique_id; 5154 } else { 5155 /* Scan to the end of the list & insert. */ 5156 mblk_t *lastone = newbie->ipsacq_mp; 5157 5158 while (lastone->b_next != NULL) 5159 lastone = lastone->b_next; 5160 lastone->b_next = mp; 5161 if (newbie->ipsacq_numpackets++ == ipsacq_maxpackets) { 5162 newbie->ipsacq_numpackets = ipsacq_maxpackets; 5163 lastone = newbie->ipsacq_mp; 5164 newbie->ipsacq_mp = lastone->b_next; 5165 lastone->b_next = NULL; 5166 ip_drop_packet(lastone, B_FALSE, NULL, NULL, 5167 DROPPER(ipss, ipds_sadb_acquire_toofull), 5168 &ipss->ipsec_sadb_dropper); 5169 } else { 5170 IP_ACQUIRE_STAT(ipss, qhiwater, 5171 newbie->ipsacq_numpackets); 5172 } 5173 } 5174 5175 /* 5176 * Reset addresses. Set them to the most recently added mblk chain, 5177 * so that the address pointers in the acquire record will point 5178 * at an mblk still attached to the acquire list. 5179 */ 5180 5181 newbie->ipsacq_srcaddr = src; 5182 newbie->ipsacq_dstaddr = dst; 5183 5184 /* 5185 * If the acquire record has more than one queued packet, we've 5186 * already sent an ACQUIRE, and don't need to repeat ourself. 5187 */ 5188 if (newbie->ipsacq_seq != seq || newbie->ipsacq_numpackets > 1) { 5189 /* I have an acquire outstanding already! */ 5190 mutex_exit(&newbie->ipsacq_lock); 5191 return; 5192 } 5193 5194 if (keysock_extended_reg(ns)) { 5195 /* 5196 * Construct an extended ACQUIRE. There are logging 5197 * opportunities here in failure cases. 5198 */ 5199 5200 (void) memset(&sel, 0, sizeof (sel)); 5201 sel.ips_isv4 = io->ipsec_out_v4; 5202 if (tunnel_mode) { 5203 sel.ips_protocol = (io->ipsec_out_inaf == AF_INET) ? 5204 IPPROTO_ENCAP : IPPROTO_IPV6; 5205 } else { 5206 sel.ips_protocol = io->ipsec_out_proto; 5207 sel.ips_local_port = io->ipsec_out_src_port; 5208 sel.ips_remote_port = io->ipsec_out_dst_port; 5209 } 5210 sel.ips_icmp_type = io->ipsec_out_icmp_type; 5211 sel.ips_icmp_code = io->ipsec_out_icmp_code; 5212 sel.ips_is_icmp_inv_acq = 0; 5213 if (af == AF_INET) { 5214 sel.ips_local_addr_v4 = ipha->ipha_src; 5215 sel.ips_remote_addr_v4 = ipha->ipha_dst; 5216 } else { 5217 sel.ips_local_addr_v6 = ip6h->ip6_src; 5218 sel.ips_remote_addr_v6 = ip6h->ip6_dst; 5219 } 5220 5221 extended = sadb_keysock_out(0); 5222 if (extended != NULL) { 5223 extended->b_cont = sadb_extended_acquire(&sel, pp, ap, 5224 tunnel_mode, seq, 0, ns); 5225 if (extended->b_cont == NULL) { 5226 freeb(extended); 5227 extended = NULL; 5228 } 5229 } 5230 } else 5231 extended = NULL; 5232 5233 /* 5234 * Send an ACQUIRE message (and possible an extended ACQUIRE) based on 5235 * this new record. The send-acquire callback assumes that acqrec is 5236 * already locked. 5237 */ 5238 (*spp->s_acqfn)(newbie, extended, ns); 5239 } 5240 5241 /* 5242 * Unlink and free an acquire record. 5243 */ 5244 void 5245 sadb_destroy_acquire(ipsacq_t *acqrec, netstack_t *ns) 5246 { 5247 mblk_t *mp; 5248 ipsec_stack_t *ipss = ns->netstack_ipsec; 5249 5250 ASSERT(MUTEX_HELD(acqrec->ipsacq_linklock)); 5251 5252 if (acqrec->ipsacq_policy != NULL) { 5253 IPPOL_REFRELE(acqrec->ipsacq_policy, ns); 5254 } 5255 if (acqrec->ipsacq_act != NULL) { 5256 IPACT_REFRELE(acqrec->ipsacq_act); 5257 } 5258 5259 /* Unlink */ 5260 *(acqrec->ipsacq_ptpn) = acqrec->ipsacq_next; 5261 if (acqrec->ipsacq_next != NULL) 5262 acqrec->ipsacq_next->ipsacq_ptpn = acqrec->ipsacq_ptpn; 5263 5264 /* 5265 * Free hanging mp's. 5266 * 5267 * XXX Instead of freemsg(), perhaps use IPSEC_REQ_FAILED. 5268 */ 5269 5270 mutex_enter(&acqrec->ipsacq_lock); 5271 while (acqrec->ipsacq_mp != NULL) { 5272 mp = acqrec->ipsacq_mp; 5273 acqrec->ipsacq_mp = mp->b_next; 5274 mp->b_next = NULL; 5275 ip_drop_packet(mp, B_FALSE, NULL, NULL, 5276 DROPPER(ipss, ipds_sadb_acquire_timeout), 5277 &ipss->ipsec_sadb_dropper); 5278 } 5279 mutex_exit(&acqrec->ipsacq_lock); 5280 5281 /* Free */ 5282 mutex_destroy(&acqrec->ipsacq_lock); 5283 kmem_free(acqrec, sizeof (*acqrec)); 5284 } 5285 5286 /* 5287 * Destroy an acquire list fanout. 5288 */ 5289 static void 5290 sadb_destroy_acqlist(iacqf_t **listp, uint_t numentries, boolean_t forever, 5291 netstack_t *ns) 5292 { 5293 int i; 5294 iacqf_t *list = *listp; 5295 5296 if (list == NULL) 5297 return; 5298 5299 for (i = 0; i < numentries; i++) { 5300 mutex_enter(&(list[i].iacqf_lock)); 5301 while (list[i].iacqf_ipsacq != NULL) 5302 sadb_destroy_acquire(list[i].iacqf_ipsacq, ns); 5303 mutex_exit(&(list[i].iacqf_lock)); 5304 if (forever) 5305 mutex_destroy(&(list[i].iacqf_lock)); 5306 } 5307 5308 if (forever) { 5309 *listp = NULL; 5310 kmem_free(list, numentries * sizeof (*list)); 5311 } 5312 } 5313 5314 /* 5315 * Create an algorithm descriptor for an extended ACQUIRE. Filter crypto 5316 * framework's view of reality vs. IPsec's. EF's wins, BTW. 5317 */ 5318 static uint8_t * 5319 sadb_new_algdesc(uint8_t *start, uint8_t *limit, 5320 sadb_x_ecomb_t *ecomb, uint8_t satype, uint8_t algtype, 5321 uint8_t alg, uint16_t minbits, uint16_t maxbits, ipsec_stack_t *ipss) 5322 { 5323 uint8_t *cur = start; 5324 ipsec_alginfo_t *algp; 5325 sadb_x_algdesc_t *algdesc = (sadb_x_algdesc_t *)cur; 5326 5327 cur += sizeof (*algdesc); 5328 if (cur >= limit) 5329 return (NULL); 5330 5331 ecomb->sadb_x_ecomb_numalgs++; 5332 5333 /* 5334 * Normalize vs. crypto framework's limits. This way, you can specify 5335 * a stronger policy, and when the framework loads a stronger version, 5336 * you can just keep plowing w/o rewhacking your SPD. 5337 */ 5338 mutex_enter(&ipss->ipsec_alg_lock); 5339 algp = ipss->ipsec_alglists[(algtype == SADB_X_ALGTYPE_AUTH) ? 5340 IPSEC_ALG_AUTH : IPSEC_ALG_ENCR][alg]; 5341 if (algp == NULL) { 5342 mutex_exit(&ipss->ipsec_alg_lock); 5343 return (NULL); /* Algorithm doesn't exist. Fail gracefully. */ 5344 } 5345 if (minbits < algp->alg_ef_minbits) 5346 minbits = algp->alg_ef_minbits; 5347 if (maxbits > algp->alg_ef_maxbits) 5348 maxbits = algp->alg_ef_maxbits; 5349 mutex_exit(&ipss->ipsec_alg_lock); 5350 5351 algdesc->sadb_x_algdesc_satype = satype; 5352 algdesc->sadb_x_algdesc_algtype = algtype; 5353 algdesc->sadb_x_algdesc_alg = alg; 5354 algdesc->sadb_x_algdesc_minbits = minbits; 5355 algdesc->sadb_x_algdesc_maxbits = maxbits; 5356 algdesc->sadb_x_algdesc_reserved = 0; 5357 return (cur); 5358 } 5359 5360 /* 5361 * Convert the given ipsec_action_t into an ecomb starting at *ecomb 5362 * which must fit before *limit 5363 * 5364 * return NULL if we ran out of room or a pointer to the end of the ecomb. 5365 */ 5366 static uint8_t * 5367 sadb_action_to_ecomb(uint8_t *start, uint8_t *limit, ipsec_action_t *act, 5368 netstack_t *ns) 5369 { 5370 uint8_t *cur = start; 5371 sadb_x_ecomb_t *ecomb = (sadb_x_ecomb_t *)cur; 5372 ipsec_prot_t *ipp; 5373 ipsec_stack_t *ipss = ns->netstack_ipsec; 5374 5375 cur += sizeof (*ecomb); 5376 if (cur >= limit) 5377 return (NULL); 5378 5379 ASSERT(act->ipa_act.ipa_type == IPSEC_ACT_APPLY); 5380 5381 ipp = &act->ipa_act.ipa_apply; 5382 5383 ecomb->sadb_x_ecomb_numalgs = 0; 5384 ecomb->sadb_x_ecomb_reserved = 0; 5385 ecomb->sadb_x_ecomb_reserved2 = 0; 5386 /* 5387 * No limits on allocations, since we really don't support that 5388 * concept currently. 5389 */ 5390 ecomb->sadb_x_ecomb_soft_allocations = 0; 5391 ecomb->sadb_x_ecomb_hard_allocations = 0; 5392 5393 /* 5394 * XXX TBD: Policy or global parameters will eventually be 5395 * able to fill in some of these. 5396 */ 5397 ecomb->sadb_x_ecomb_flags = 0; 5398 ecomb->sadb_x_ecomb_soft_bytes = 0; 5399 ecomb->sadb_x_ecomb_hard_bytes = 0; 5400 ecomb->sadb_x_ecomb_soft_addtime = 0; 5401 ecomb->sadb_x_ecomb_hard_addtime = 0; 5402 ecomb->sadb_x_ecomb_soft_usetime = 0; 5403 ecomb->sadb_x_ecomb_hard_usetime = 0; 5404 5405 if (ipp->ipp_use_ah) { 5406 cur = sadb_new_algdesc(cur, limit, ecomb, 5407 SADB_SATYPE_AH, SADB_X_ALGTYPE_AUTH, ipp->ipp_auth_alg, 5408 ipp->ipp_ah_minbits, ipp->ipp_ah_maxbits, ipss); 5409 if (cur == NULL) 5410 return (NULL); 5411 ipsecah_fill_defs(ecomb, ns); 5412 } 5413 5414 if (ipp->ipp_use_esp) { 5415 if (ipp->ipp_use_espa) { 5416 cur = sadb_new_algdesc(cur, limit, ecomb, 5417 SADB_SATYPE_ESP, SADB_X_ALGTYPE_AUTH, 5418 ipp->ipp_esp_auth_alg, 5419 ipp->ipp_espa_minbits, 5420 ipp->ipp_espa_maxbits, ipss); 5421 if (cur == NULL) 5422 return (NULL); 5423 } 5424 5425 cur = sadb_new_algdesc(cur, limit, ecomb, 5426 SADB_SATYPE_ESP, SADB_X_ALGTYPE_CRYPT, 5427 ipp->ipp_encr_alg, 5428 ipp->ipp_espe_minbits, 5429 ipp->ipp_espe_maxbits, ipss); 5430 if (cur == NULL) 5431 return (NULL); 5432 /* Fill in lifetimes if and only if AH didn't already... */ 5433 if (!ipp->ipp_use_ah) 5434 ipsecesp_fill_defs(ecomb, ns); 5435 } 5436 5437 return (cur); 5438 } 5439 5440 /* 5441 * Construct an extended ACQUIRE message based on a selector and the resulting 5442 * IPsec action. 5443 * 5444 * NOTE: This is used by both inverse ACQUIRE and actual ACQUIRE 5445 * generation. As a consequence, expect this function to evolve 5446 * rapidly. 5447 */ 5448 static mblk_t * 5449 sadb_extended_acquire(ipsec_selector_t *sel, ipsec_policy_t *pol, 5450 ipsec_action_t *act, boolean_t tunnel_mode, uint32_t seq, uint32_t pid, 5451 netstack_t *ns) 5452 { 5453 mblk_t *mp; 5454 sadb_msg_t *samsg; 5455 uint8_t *start, *cur, *end; 5456 uint32_t *saddrptr, *daddrptr; 5457 sa_family_t af; 5458 sadb_prop_t *eprop; 5459 ipsec_action_t *ap, *an; 5460 ipsec_selkey_t *ipsl; 5461 uint8_t proto, pfxlen; 5462 uint16_t lport, rport; 5463 uint32_t kmp, kmc; 5464 5465 /* 5466 * Find the action we want sooner rather than later.. 5467 */ 5468 an = NULL; 5469 if (pol == NULL) { 5470 ap = act; 5471 } else { 5472 ap = pol->ipsp_act; 5473 5474 if (ap != NULL) 5475 an = ap->ipa_next; 5476 } 5477 5478 /* 5479 * Just take a swag for the allocation for now. We can always 5480 * alter it later. 5481 */ 5482 #define SADB_EXTENDED_ACQUIRE_SIZE 4096 5483 mp = allocb(SADB_EXTENDED_ACQUIRE_SIZE, BPRI_HI); 5484 if (mp == NULL) 5485 return (NULL); 5486 5487 start = mp->b_rptr; 5488 end = start + SADB_EXTENDED_ACQUIRE_SIZE; 5489 5490 cur = start; 5491 5492 samsg = (sadb_msg_t *)cur; 5493 cur += sizeof (*samsg); 5494 5495 samsg->sadb_msg_version = PF_KEY_V2; 5496 samsg->sadb_msg_type = SADB_ACQUIRE; 5497 samsg->sadb_msg_errno = 0; 5498 samsg->sadb_msg_reserved = 0; 5499 samsg->sadb_msg_satype = 0; 5500 samsg->sadb_msg_seq = seq; 5501 samsg->sadb_msg_pid = pid; 5502 5503 if (tunnel_mode) { 5504 /* 5505 * Form inner address extensions based NOT on the inner 5506 * selectors (i.e. the packet data), but on the policy's 5507 * selector key (i.e. the policy's selector information). 5508 * 5509 * NOTE: The position of IPv4 and IPv6 addresses is the 5510 * same in ipsec_selkey_t (unless the compiler does very 5511 * strange things with unions, consult your local C language 5512 * lawyer for details). 5513 */ 5514 ipsl = &(pol->ipsp_sel->ipsl_key); 5515 if (ipsl->ipsl_valid & IPSL_IPV4) { 5516 af = AF_INET; 5517 ASSERT(sel->ips_protocol == IPPROTO_ENCAP); 5518 ASSERT(!(ipsl->ipsl_valid & IPSL_IPV6)); 5519 } else { 5520 af = AF_INET6; 5521 ASSERT(sel->ips_protocol == IPPROTO_IPV6); 5522 ASSERT(ipsl->ipsl_valid & IPSL_IPV6); 5523 } 5524 5525 if (ipsl->ipsl_valid & IPSL_LOCAL_ADDR) { 5526 saddrptr = (uint32_t *)(&ipsl->ipsl_local); 5527 pfxlen = ipsl->ipsl_local_pfxlen; 5528 } else { 5529 saddrptr = (uint32_t *)(&ipv6_all_zeros); 5530 pfxlen = 0; 5531 } 5532 /* XXX What about ICMP type/code? */ 5533 lport = (ipsl->ipsl_valid & IPSL_LOCAL_PORT) ? 5534 ipsl->ipsl_lport : 0; 5535 proto = (ipsl->ipsl_valid & IPSL_PROTOCOL) ? 5536 ipsl->ipsl_proto : 0; 5537 5538 cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_INNER_SRC, 5539 af, saddrptr, lport, proto, pfxlen); 5540 if (cur == NULL) { 5541 freeb(mp); 5542 return (NULL); 5543 } 5544 5545 if (ipsl->ipsl_valid & IPSL_REMOTE_ADDR) { 5546 daddrptr = (uint32_t *)(&ipsl->ipsl_remote); 5547 pfxlen = ipsl->ipsl_remote_pfxlen; 5548 } else { 5549 daddrptr = (uint32_t *)(&ipv6_all_zeros); 5550 pfxlen = 0; 5551 } 5552 /* XXX What about ICMP type/code? */ 5553 rport = (ipsl->ipsl_valid & IPSL_REMOTE_PORT) ? 5554 ipsl->ipsl_rport : 0; 5555 5556 cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_INNER_DST, 5557 af, daddrptr, rport, proto, pfxlen); 5558 if (cur == NULL) { 5559 freeb(mp); 5560 return (NULL); 5561 } 5562 /* 5563 * TODO - if we go to 3408's dream of transport mode IP-in-IP 5564 * _with_ inner-packet address selectors, we'll need to further 5565 * distinguish tunnel mode here. For now, having inner 5566 * addresses and/or ports is sufficient. 5567 * 5568 * Meanwhile, whack proto/ports to reflect IP-in-IP for the 5569 * outer addresses. 5570 */ 5571 proto = sel->ips_protocol; /* Either _ENCAP or _IPV6 */ 5572 lport = rport = 0; 5573 } else if ((ap != NULL) && (!ap->ipa_want_unique)) { 5574 proto = 0; 5575 lport = 0; 5576 rport = 0; 5577 if (pol != NULL) { 5578 ipsl = &(pol->ipsp_sel->ipsl_key); 5579 if (ipsl->ipsl_valid & IPSL_PROTOCOL) 5580 proto = ipsl->ipsl_proto; 5581 if (ipsl->ipsl_valid & IPSL_REMOTE_PORT) 5582 rport = ipsl->ipsl_rport; 5583 if (ipsl->ipsl_valid & IPSL_LOCAL_PORT) 5584 lport = ipsl->ipsl_lport; 5585 } 5586 } else { 5587 proto = sel->ips_protocol; 5588 lport = sel->ips_local_port; 5589 rport = sel->ips_remote_port; 5590 } 5591 5592 af = sel->ips_isv4 ? AF_INET : AF_INET6; 5593 5594 /* 5595 * NOTE: The position of IPv4 and IPv6 addresses is the same in 5596 * ipsec_selector_t. 5597 */ 5598 cur = sadb_make_addr_ext(cur, end, SADB_EXT_ADDRESS_SRC, af, 5599 (uint32_t *)(&sel->ips_local_addr_v6), lport, proto, 0); 5600 5601 if (cur == NULL) { 5602 freeb(mp); 5603 return (NULL); 5604 } 5605 5606 cur = sadb_make_addr_ext(cur, end, SADB_EXT_ADDRESS_DST, af, 5607 (uint32_t *)(&sel->ips_remote_addr_v6), rport, proto, 0); 5608 5609 if (cur == NULL) { 5610 freeb(mp); 5611 return (NULL); 5612 } 5613 5614 /* 5615 * This section will change a lot as policy evolves. 5616 * For now, it'll be relatively simple. 5617 */ 5618 eprop = (sadb_prop_t *)cur; 5619 cur += sizeof (*eprop); 5620 if (cur > end) { 5621 /* no space left */ 5622 freeb(mp); 5623 return (NULL); 5624 } 5625 5626 eprop->sadb_prop_exttype = SADB_X_EXT_EPROP; 5627 eprop->sadb_x_prop_ereserved = 0; 5628 eprop->sadb_x_prop_numecombs = 0; 5629 eprop->sadb_prop_replay = 32; /* default */ 5630 5631 kmc = kmp = 0; 5632 5633 for (; ap != NULL; ap = an) { 5634 an = (pol != NULL) ? ap->ipa_next : NULL; 5635 5636 /* 5637 * Skip non-IPsec policies 5638 */ 5639 if (ap->ipa_act.ipa_type != IPSEC_ACT_APPLY) 5640 continue; 5641 5642 if (ap->ipa_act.ipa_apply.ipp_km_proto) 5643 kmp = ap->ipa_act.ipa_apply.ipp_km_proto; 5644 if (ap->ipa_act.ipa_apply.ipp_km_cookie) 5645 kmc = ap->ipa_act.ipa_apply.ipp_km_cookie; 5646 if (ap->ipa_act.ipa_apply.ipp_replay_depth) { 5647 eprop->sadb_prop_replay = 5648 ap->ipa_act.ipa_apply.ipp_replay_depth; 5649 } 5650 5651 cur = sadb_action_to_ecomb(cur, end, ap, ns); 5652 if (cur == NULL) { /* no space */ 5653 freeb(mp); 5654 return (NULL); 5655 } 5656 eprop->sadb_x_prop_numecombs++; 5657 } 5658 5659 if (eprop->sadb_x_prop_numecombs == 0) { 5660 /* 5661 * This will happen if we fail to find a policy 5662 * allowing for IPsec processing. 5663 * Construct an error message. 5664 */ 5665 samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg)); 5666 samsg->sadb_msg_errno = ENOENT; 5667 samsg->sadb_x_msg_diagnostic = 0; 5668 return (mp); 5669 } 5670 5671 if ((kmp != 0) || (kmc != 0)) { 5672 cur = sadb_make_kmc_ext(cur, end, kmp, kmc); 5673 if (cur == NULL) { 5674 freeb(mp); 5675 return (NULL); 5676 } 5677 } 5678 5679 eprop->sadb_prop_len = SADB_8TO64(cur - (uint8_t *)eprop); 5680 samsg->sadb_msg_len = SADB_8TO64(cur - start); 5681 mp->b_wptr = cur; 5682 5683 return (mp); 5684 } 5685 5686 /* 5687 * Generic setup of an RFC 2367 ACQUIRE message. Caller sets satype. 5688 * 5689 * NOTE: This function acquires alg_lock as a side-effect if-and-only-if we 5690 * succeed (i.e. return non-NULL). Caller MUST release it. This is to 5691 * maximize code consolidation while preventing algorithm changes from messing 5692 * with the callers finishing touches on the ACQUIRE itself. 5693 */ 5694 mblk_t * 5695 sadb_setup_acquire(ipsacq_t *acqrec, uint8_t satype, ipsec_stack_t *ipss) 5696 { 5697 uint_t allocsize; 5698 mblk_t *pfkeymp, *msgmp; 5699 sa_family_t af; 5700 uint8_t *cur, *end; 5701 sadb_msg_t *samsg; 5702 uint16_t sport_typecode; 5703 uint16_t dport_typecode; 5704 uint8_t check_proto; 5705 boolean_t tunnel_mode = (acqrec->ipsacq_inneraddrfam != 0); 5706 5707 ASSERT(MUTEX_HELD(&acqrec->ipsacq_lock)); 5708 5709 pfkeymp = sadb_keysock_out(0); 5710 if (pfkeymp == NULL) 5711 return (NULL); 5712 5713 /* 5714 * First, allocate a basic ACQUIRE message 5715 */ 5716 allocsize = sizeof (sadb_msg_t) + sizeof (sadb_address_t) + 5717 sizeof (sadb_address_t) + sizeof (sadb_prop_t); 5718 5719 /* Make sure there's enough to cover both AF_INET and AF_INET6. */ 5720 allocsize += 2 * sizeof (struct sockaddr_in6); 5721 5722 mutex_enter(&ipss->ipsec_alg_lock); 5723 /* NOTE: The lock is now held through to this function's return. */ 5724 allocsize += ipss->ipsec_nalgs[IPSEC_ALG_AUTH] * 5725 ipss->ipsec_nalgs[IPSEC_ALG_ENCR] * sizeof (sadb_comb_t); 5726 5727 if (tunnel_mode) { 5728 /* Tunnel mode! */ 5729 allocsize += 2 * sizeof (sadb_address_t); 5730 /* Enough to cover both AF_INET and AF_INET6. */ 5731 allocsize += 2 * sizeof (struct sockaddr_in6); 5732 } 5733 5734 msgmp = allocb(allocsize, BPRI_HI); 5735 if (msgmp == NULL) { 5736 freeb(pfkeymp); 5737 mutex_exit(&ipss->ipsec_alg_lock); 5738 return (NULL); 5739 } 5740 5741 pfkeymp->b_cont = msgmp; 5742 cur = msgmp->b_rptr; 5743 end = cur + allocsize; 5744 samsg = (sadb_msg_t *)cur; 5745 cur += sizeof (sadb_msg_t); 5746 5747 af = acqrec->ipsacq_addrfam; 5748 switch (af) { 5749 case AF_INET: 5750 check_proto = IPPROTO_ICMP; 5751 break; 5752 case AF_INET6: 5753 check_proto = IPPROTO_ICMPV6; 5754 break; 5755 default: 5756 /* This should never happen unless we have kernel bugs. */ 5757 cmn_err(CE_WARN, 5758 "sadb_setup_acquire: corrupt ACQUIRE record.\n"); 5759 ASSERT(0); 5760 mutex_exit(&ipss->ipsec_alg_lock); 5761 return (NULL); 5762 } 5763 5764 samsg->sadb_msg_version = PF_KEY_V2; 5765 samsg->sadb_msg_type = SADB_ACQUIRE; 5766 samsg->sadb_msg_satype = satype; 5767 samsg->sadb_msg_errno = 0; 5768 samsg->sadb_msg_pid = 0; 5769 samsg->sadb_msg_reserved = 0; 5770 samsg->sadb_msg_seq = acqrec->ipsacq_seq; 5771 5772 ASSERT(MUTEX_HELD(&acqrec->ipsacq_lock)); 5773 5774 if ((acqrec->ipsacq_proto == check_proto) || tunnel_mode) { 5775 sport_typecode = dport_typecode = 0; 5776 } else { 5777 sport_typecode = acqrec->ipsacq_srcport; 5778 dport_typecode = acqrec->ipsacq_dstport; 5779 } 5780 5781 cur = sadb_make_addr_ext(cur, end, SADB_EXT_ADDRESS_SRC, af, 5782 acqrec->ipsacq_srcaddr, sport_typecode, acqrec->ipsacq_proto, 0); 5783 5784 cur = sadb_make_addr_ext(cur, end, SADB_EXT_ADDRESS_DST, af, 5785 acqrec->ipsacq_dstaddr, dport_typecode, acqrec->ipsacq_proto, 0); 5786 5787 if (tunnel_mode) { 5788 sport_typecode = acqrec->ipsacq_srcport; 5789 dport_typecode = acqrec->ipsacq_dstport; 5790 cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_INNER_SRC, 5791 acqrec->ipsacq_inneraddrfam, acqrec->ipsacq_innersrc, 5792 sport_typecode, acqrec->ipsacq_inner_proto, 5793 acqrec->ipsacq_innersrcpfx); 5794 cur = sadb_make_addr_ext(cur, end, SADB_X_EXT_ADDRESS_INNER_DST, 5795 acqrec->ipsacq_inneraddrfam, acqrec->ipsacq_innerdst, 5796 dport_typecode, acqrec->ipsacq_inner_proto, 5797 acqrec->ipsacq_innerdstpfx); 5798 } 5799 5800 /* XXX Insert identity information here. */ 5801 5802 /* XXXMLS Insert sensitivity information here. */ 5803 5804 if (cur != NULL) 5805 samsg->sadb_msg_len = SADB_8TO64(cur - msgmp->b_rptr); 5806 else 5807 mutex_exit(&ipss->ipsec_alg_lock); 5808 5809 return (pfkeymp); 5810 } 5811 5812 /* 5813 * Given an SADB_GETSPI message, find an appropriately ranged SA and 5814 * allocate an SA. If there are message improprieties, return (ipsa_t *)-1. 5815 * If there was a memory allocation error, return NULL. (Assume NULL != 5816 * (ipsa_t *)-1). 5817 * 5818 * master_spi is passed in host order. 5819 */ 5820 ipsa_t * 5821 sadb_getspi(keysock_in_t *ksi, uint32_t master_spi, int *diagnostic, 5822 netstack_t *ns, uint_t sa_type) 5823 { 5824 sadb_address_t *src = 5825 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC], 5826 *dst = (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 5827 sadb_spirange_t *range = 5828 (sadb_spirange_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE]; 5829 struct sockaddr_in *ssa, *dsa; 5830 struct sockaddr_in6 *ssa6, *dsa6; 5831 uint32_t *srcaddr, *dstaddr; 5832 sa_family_t af; 5833 uint32_t add, min, max; 5834 uint8_t protocol = 5835 (sa_type == SADB_SATYPE_AH) ? IPPROTO_AH : IPPROTO_ESP; 5836 5837 if (src == NULL) { 5838 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC; 5839 return ((ipsa_t *)-1); 5840 } 5841 if (dst == NULL) { 5842 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST; 5843 return ((ipsa_t *)-1); 5844 } 5845 if (range == NULL) { 5846 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_RANGE; 5847 return ((ipsa_t *)-1); 5848 } 5849 5850 min = ntohl(range->sadb_spirange_min); 5851 max = ntohl(range->sadb_spirange_max); 5852 dsa = (struct sockaddr_in *)(dst + 1); 5853 dsa6 = (struct sockaddr_in6 *)dsa; 5854 5855 ssa = (struct sockaddr_in *)(src + 1); 5856 ssa6 = (struct sockaddr_in6 *)ssa; 5857 ASSERT(dsa->sin_family == ssa->sin_family); 5858 5859 srcaddr = ALL_ZEROES_PTR; 5860 af = dsa->sin_family; 5861 switch (af) { 5862 case AF_INET: 5863 if (src != NULL) 5864 srcaddr = (uint32_t *)(&ssa->sin_addr); 5865 dstaddr = (uint32_t *)(&dsa->sin_addr); 5866 break; 5867 case AF_INET6: 5868 if (src != NULL) 5869 srcaddr = (uint32_t *)(&ssa6->sin6_addr); 5870 dstaddr = (uint32_t *)(&dsa6->sin6_addr); 5871 break; 5872 default: 5873 *diagnostic = SADB_X_DIAGNOSTIC_BAD_DST_AF; 5874 return ((ipsa_t *)-1); 5875 } 5876 5877 if (master_spi < min || master_spi > max) { 5878 /* Return a random value in the range. */ 5879 if (cl_inet_getspi) { 5880 cl_inet_getspi(ns->netstack_stackid, protocol, 5881 (uint8_t *)&add, sizeof (add), NULL); 5882 } else { 5883 (void) random_get_pseudo_bytes((uint8_t *)&add, 5884 sizeof (add)); 5885 } 5886 master_spi = min + (add % (max - min + 1)); 5887 } 5888 5889 /* 5890 * Since master_spi is passed in host order, we need to htonl() it 5891 * for the purposes of creating a new SA. 5892 */ 5893 return (sadb_makelarvalassoc(htonl(master_spi), srcaddr, dstaddr, af, 5894 ns)); 5895 } 5896 5897 /* 5898 * 5899 * Locate an ACQUIRE and nuke it. If I have an samsg that's larger than the 5900 * base header, just ignore it. Otherwise, lock down the whole ACQUIRE list 5901 * and scan for the sequence number in question. I may wish to accept an 5902 * address pair with it, for easier searching. 5903 * 5904 * Caller frees the message, so we don't have to here. 5905 * 5906 * NOTE: The ip_q parameter may be used in the future for ACQUIRE 5907 * failures. 5908 */ 5909 /* ARGSUSED */ 5910 void 5911 sadb_in_acquire(sadb_msg_t *samsg, sadbp_t *sp, queue_t *ip_q, netstack_t *ns) 5912 { 5913 int i; 5914 ipsacq_t *acqrec; 5915 iacqf_t *bucket; 5916 5917 /* 5918 * I only accept the base header for this! 5919 * Though to be honest, requiring the dst address would help 5920 * immensely. 5921 * 5922 * XXX There are already cases where I can get the dst address. 5923 */ 5924 if (samsg->sadb_msg_len > SADB_8TO64(sizeof (*samsg))) 5925 return; 5926 5927 /* 5928 * Using the samsg->sadb_msg_seq, find the ACQUIRE record, delete it, 5929 * (and in the future send a message to IP with the appropriate error 5930 * number). 5931 * 5932 * Q: Do I want to reject if pid != 0? 5933 */ 5934 5935 for (i = 0; i < sp->s_v4.sdb_hashsize; i++) { 5936 bucket = &sp->s_v4.sdb_acq[i]; 5937 mutex_enter(&bucket->iacqf_lock); 5938 for (acqrec = bucket->iacqf_ipsacq; acqrec != NULL; 5939 acqrec = acqrec->ipsacq_next) { 5940 if (samsg->sadb_msg_seq == acqrec->ipsacq_seq) 5941 break; /* for acqrec... loop. */ 5942 } 5943 if (acqrec != NULL) 5944 break; /* for i = 0... loop. */ 5945 5946 mutex_exit(&bucket->iacqf_lock); 5947 } 5948 5949 if (acqrec == NULL) { 5950 for (i = 0; i < sp->s_v6.sdb_hashsize; i++) { 5951 bucket = &sp->s_v6.sdb_acq[i]; 5952 mutex_enter(&bucket->iacqf_lock); 5953 for (acqrec = bucket->iacqf_ipsacq; acqrec != NULL; 5954 acqrec = acqrec->ipsacq_next) { 5955 if (samsg->sadb_msg_seq == acqrec->ipsacq_seq) 5956 break; /* for acqrec... loop. */ 5957 } 5958 if (acqrec != NULL) 5959 break; /* for i = 0... loop. */ 5960 5961 mutex_exit(&bucket->iacqf_lock); 5962 } 5963 } 5964 5965 5966 if (acqrec == NULL) 5967 return; 5968 5969 /* 5970 * What do I do with the errno and IP? I may need mp's services a 5971 * little more. See sadb_destroy_acquire() for future directions 5972 * beyond free the mblk chain on the acquire record. 5973 */ 5974 5975 ASSERT(&bucket->iacqf_lock == acqrec->ipsacq_linklock); 5976 sadb_destroy_acquire(acqrec, ns); 5977 /* Have to exit mutex here, because of breaking out of for loop. */ 5978 mutex_exit(&bucket->iacqf_lock); 5979 } 5980 5981 /* 5982 * The following functions work with the replay windows of an SA. They assume 5983 * the ipsa->ipsa_replay_arr is an array of uint64_t, and that the bit vector 5984 * represents the highest sequence number packet received, and back 5985 * (ipsa->ipsa_replay_wsize) packets. 5986 */ 5987 5988 /* 5989 * Is the replay bit set? 5990 */ 5991 static boolean_t 5992 ipsa_is_replay_set(ipsa_t *ipsa, uint32_t offset) 5993 { 5994 uint64_t bit = (uint64_t)1 << (uint64_t)(offset & 63); 5995 5996 return ((bit & ipsa->ipsa_replay_arr[offset >> 6]) ? B_TRUE : B_FALSE); 5997 } 5998 5999 /* 6000 * Shift the bits of the replay window over. 6001 */ 6002 static void 6003 ipsa_shift_replay(ipsa_t *ipsa, uint32_t shift) 6004 { 6005 int i; 6006 int jump = ((shift - 1) >> 6) + 1; 6007 6008 if (shift == 0) 6009 return; 6010 6011 for (i = (ipsa->ipsa_replay_wsize - 1) >> 6; i >= 0; i--) { 6012 if (i + jump <= (ipsa->ipsa_replay_wsize - 1) >> 6) { 6013 ipsa->ipsa_replay_arr[i + jump] |= 6014 ipsa->ipsa_replay_arr[i] >> (64 - (shift & 63)); 6015 } 6016 ipsa->ipsa_replay_arr[i] <<= shift; 6017 } 6018 } 6019 6020 /* 6021 * Set a bit in the bit vector. 6022 */ 6023 static void 6024 ipsa_set_replay(ipsa_t *ipsa, uint32_t offset) 6025 { 6026 uint64_t bit = (uint64_t)1 << (uint64_t)(offset & 63); 6027 6028 ipsa->ipsa_replay_arr[offset >> 6] |= bit; 6029 } 6030 6031 #define SADB_MAX_REPLAY_VALUE 0xffffffff 6032 6033 /* 6034 * Assume caller has NOT done ntohl() already on seq. Check to see 6035 * if replay sequence number "seq" has been seen already. 6036 */ 6037 boolean_t 6038 sadb_replay_check(ipsa_t *ipsa, uint32_t seq) 6039 { 6040 boolean_t rc; 6041 uint32_t diff; 6042 6043 if (ipsa->ipsa_replay_wsize == 0) 6044 return (B_TRUE); 6045 6046 /* 6047 * NOTE: I've already checked for 0 on the wire in sadb_replay_peek(). 6048 */ 6049 6050 /* Convert sequence number into host order before holding the mutex. */ 6051 seq = ntohl(seq); 6052 6053 mutex_enter(&ipsa->ipsa_lock); 6054 6055 /* Initialize inbound SA's ipsa_replay field to last one received. */ 6056 if (ipsa->ipsa_replay == 0) 6057 ipsa->ipsa_replay = 1; 6058 6059 if (seq > ipsa->ipsa_replay) { 6060 /* 6061 * I have received a new "highest value received". Shift 6062 * the replay window over. 6063 */ 6064 diff = seq - ipsa->ipsa_replay; 6065 if (diff < ipsa->ipsa_replay_wsize) { 6066 /* In replay window, shift bits over. */ 6067 ipsa_shift_replay(ipsa, diff); 6068 } else { 6069 /* WAY FAR AHEAD, clear bits and start again. */ 6070 bzero(ipsa->ipsa_replay_arr, 6071 sizeof (ipsa->ipsa_replay_arr)); 6072 } 6073 ipsa_set_replay(ipsa, 0); 6074 ipsa->ipsa_replay = seq; 6075 rc = B_TRUE; 6076 goto done; 6077 } 6078 diff = ipsa->ipsa_replay - seq; 6079 if (diff >= ipsa->ipsa_replay_wsize || ipsa_is_replay_set(ipsa, diff)) { 6080 rc = B_FALSE; 6081 goto done; 6082 } 6083 /* Set this packet as seen. */ 6084 ipsa_set_replay(ipsa, diff); 6085 6086 rc = B_TRUE; 6087 done: 6088 mutex_exit(&ipsa->ipsa_lock); 6089 return (rc); 6090 } 6091 6092 /* 6093 * "Peek" and see if we should even bother going through the effort of 6094 * running an authentication check on the sequence number passed in. 6095 * this takes into account packets that are below the replay window, 6096 * and collisions with already replayed packets. Return B_TRUE if it 6097 * is okay to proceed, B_FALSE if this packet should be dropped immediately. 6098 * Assume same byte-ordering as sadb_replay_check. 6099 */ 6100 boolean_t 6101 sadb_replay_peek(ipsa_t *ipsa, uint32_t seq) 6102 { 6103 boolean_t rc = B_FALSE; 6104 uint32_t diff; 6105 6106 if (ipsa->ipsa_replay_wsize == 0) 6107 return (B_TRUE); 6108 6109 /* 6110 * 0 is 0, regardless of byte order... :) 6111 * 6112 * If I get 0 on the wire (and there is a replay window) then the 6113 * sender most likely wrapped. This ipsa may need to be marked or 6114 * something. 6115 */ 6116 if (seq == 0) 6117 return (B_FALSE); 6118 6119 seq = ntohl(seq); 6120 mutex_enter(&ipsa->ipsa_lock); 6121 if (seq < ipsa->ipsa_replay - ipsa->ipsa_replay_wsize && 6122 ipsa->ipsa_replay >= ipsa->ipsa_replay_wsize) 6123 goto done; 6124 6125 /* 6126 * If I've hit 0xffffffff, then quite honestly, I don't need to 6127 * bother with formalities. I'm not accepting any more packets 6128 * on this SA. 6129 */ 6130 if (ipsa->ipsa_replay == SADB_MAX_REPLAY_VALUE) { 6131 /* 6132 * Since we're already holding the lock, update the 6133 * expire time ala. sadb_replay_delete() and return. 6134 */ 6135 ipsa->ipsa_hardexpiretime = (time_t)1; 6136 goto done; 6137 } 6138 6139 if (seq <= ipsa->ipsa_replay) { 6140 /* 6141 * This seq is in the replay window. I'm not below it, 6142 * because I already checked for that above! 6143 */ 6144 diff = ipsa->ipsa_replay - seq; 6145 if (ipsa_is_replay_set(ipsa, diff)) 6146 goto done; 6147 } 6148 /* Else return B_TRUE, I'm going to advance the window. */ 6149 6150 rc = B_TRUE; 6151 done: 6152 mutex_exit(&ipsa->ipsa_lock); 6153 return (rc); 6154 } 6155 6156 /* 6157 * Delete a single SA. 6158 * 6159 * For now, use the quick-and-dirty trick of making the association's 6160 * hard-expire lifetime (time_t)1, ensuring deletion by the *_ager(). 6161 */ 6162 void 6163 sadb_replay_delete(ipsa_t *assoc) 6164 { 6165 mutex_enter(&assoc->ipsa_lock); 6166 assoc->ipsa_hardexpiretime = (time_t)1; 6167 mutex_exit(&assoc->ipsa_lock); 6168 } 6169 6170 /* 6171 * Given a queue that presumably points to IP, send a T_BIND_REQ for _proto_ 6172 * down. The caller will handle the T_BIND_ACK locally. 6173 */ 6174 boolean_t 6175 sadb_t_bind_req(queue_t *q, int proto) 6176 { 6177 struct T_bind_req *tbr; 6178 mblk_t *mp; 6179 6180 mp = allocb_cred(sizeof (struct T_bind_req) + 1, kcred, NOPID); 6181 if (mp == NULL) { 6182 /* cmn_err(CE_WARN, */ 6183 /* "sadb_t_bind_req(%d): couldn't allocate mblk\n", proto); */ 6184 return (B_FALSE); 6185 } 6186 mp->b_datap->db_type = M_PCPROTO; 6187 tbr = (struct T_bind_req *)mp->b_rptr; 6188 mp->b_wptr += sizeof (struct T_bind_req); 6189 tbr->PRIM_type = T_BIND_REQ; 6190 tbr->ADDR_length = 0; 6191 tbr->ADDR_offset = 0; 6192 tbr->CONIND_number = 0; 6193 *mp->b_wptr = (uint8_t)proto; 6194 mp->b_wptr++; 6195 6196 putnext(q, mp); 6197 return (B_TRUE); 6198 } 6199 6200 /* 6201 * Special front-end to ipsec_rl_strlog() dealing with SA failure. 6202 * this is designed to take only a format string with "* %x * %s *", so 6203 * that "spi" is printed first, then "addr" is converted using inet_pton(). 6204 * 6205 * This is abstracted out to save the stack space for only when inet_pton() 6206 * is called. Make sure "spi" is in network order; it usually is when this 6207 * would get called. 6208 */ 6209 void 6210 ipsec_assocfailure(short mid, short sid, char level, ushort_t sl, char *fmt, 6211 uint32_t spi, void *addr, int af, netstack_t *ns) 6212 { 6213 char buf[INET6_ADDRSTRLEN]; 6214 6215 ASSERT(af == AF_INET6 || af == AF_INET); 6216 6217 ipsec_rl_strlog(ns, mid, sid, level, sl, fmt, ntohl(spi), 6218 inet_ntop(af, addr, buf, sizeof (buf))); 6219 } 6220 6221 /* 6222 * Fills in a reference to the policy, if any, from the conn, in *ppp 6223 * Releases a reference to the passed conn_t. 6224 */ 6225 static void 6226 ipsec_conn_pol(ipsec_selector_t *sel, conn_t *connp, ipsec_policy_t **ppp) 6227 { 6228 ipsec_policy_t *pp; 6229 ipsec_latch_t *ipl = connp->conn_latch; 6230 6231 if ((ipl != NULL) && (ipl->ipl_out_policy != NULL)) { 6232 pp = ipl->ipl_out_policy; 6233 IPPOL_REFHOLD(pp); 6234 } else { 6235 pp = ipsec_find_policy(IPSEC_TYPE_OUTBOUND, connp, NULL, sel, 6236 connp->conn_netstack); 6237 } 6238 *ppp = pp; 6239 CONN_DEC_REF(connp); 6240 } 6241 6242 /* 6243 * The following functions scan through active conn_t structures 6244 * and return a reference to the best-matching policy it can find. 6245 * Caller must release the reference. 6246 */ 6247 static void 6248 ipsec_udp_pol(ipsec_selector_t *sel, ipsec_policy_t **ppp, ip_stack_t *ipst) 6249 { 6250 connf_t *connfp; 6251 conn_t *connp = NULL; 6252 ipsec_selector_t portonly; 6253 6254 bzero((void *)&portonly, sizeof (portonly)); 6255 6256 if (sel->ips_local_port == 0) 6257 return; 6258 6259 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(sel->ips_local_port, 6260 ipst)]; 6261 mutex_enter(&connfp->connf_lock); 6262 6263 if (sel->ips_isv4) { 6264 connp = connfp->connf_head; 6265 while (connp != NULL) { 6266 if (IPCL_UDP_MATCH(connp, sel->ips_local_port, 6267 sel->ips_local_addr_v4, sel->ips_remote_port, 6268 sel->ips_remote_addr_v4)) 6269 break; 6270 connp = connp->conn_next; 6271 } 6272 6273 if (connp == NULL) { 6274 /* Try port-only match in IPv6. */ 6275 portonly.ips_local_port = sel->ips_local_port; 6276 sel = &portonly; 6277 } 6278 } 6279 6280 if (connp == NULL) { 6281 connp = connfp->connf_head; 6282 while (connp != NULL) { 6283 if (IPCL_UDP_MATCH_V6(connp, sel->ips_local_port, 6284 sel->ips_local_addr_v6, sel->ips_remote_port, 6285 sel->ips_remote_addr_v6)) 6286 break; 6287 connp = connp->conn_next; 6288 } 6289 6290 if (connp == NULL) { 6291 mutex_exit(&connfp->connf_lock); 6292 return; 6293 } 6294 } 6295 6296 CONN_INC_REF(connp); 6297 mutex_exit(&connfp->connf_lock); 6298 6299 ipsec_conn_pol(sel, connp, ppp); 6300 } 6301 6302 static conn_t * 6303 ipsec_find_listen_conn(uint16_t *pptr, ipsec_selector_t *sel, ip_stack_t *ipst) 6304 { 6305 connf_t *connfp; 6306 conn_t *connp = NULL; 6307 const in6_addr_t *v6addrmatch = &sel->ips_local_addr_v6; 6308 6309 if (sel->ips_local_port == 0) 6310 return (NULL); 6311 6312 connfp = &ipst->ips_ipcl_bind_fanout[ 6313 IPCL_BIND_HASH(sel->ips_local_port, ipst)]; 6314 mutex_enter(&connfp->connf_lock); 6315 6316 if (sel->ips_isv4) { 6317 connp = connfp->connf_head; 6318 while (connp != NULL) { 6319 if (IPCL_BIND_MATCH(connp, IPPROTO_TCP, 6320 sel->ips_local_addr_v4, pptr[1])) 6321 break; 6322 connp = connp->conn_next; 6323 } 6324 6325 if (connp == NULL) { 6326 /* Match to all-zeroes. */ 6327 v6addrmatch = &ipv6_all_zeros; 6328 } 6329 } 6330 6331 if (connp == NULL) { 6332 connp = connfp->connf_head; 6333 while (connp != NULL) { 6334 if (IPCL_BIND_MATCH_V6(connp, IPPROTO_TCP, 6335 *v6addrmatch, pptr[1])) 6336 break; 6337 connp = connp->conn_next; 6338 } 6339 6340 if (connp == NULL) { 6341 mutex_exit(&connfp->connf_lock); 6342 return (NULL); 6343 } 6344 } 6345 6346 CONN_INC_REF(connp); 6347 mutex_exit(&connfp->connf_lock); 6348 return (connp); 6349 } 6350 6351 static void 6352 ipsec_tcp_pol(ipsec_selector_t *sel, ipsec_policy_t **ppp, ip_stack_t *ipst) 6353 { 6354 connf_t *connfp; 6355 conn_t *connp; 6356 uint32_t ports; 6357 uint16_t *pptr = (uint16_t *)&ports; 6358 6359 /* 6360 * Find TCP state in the following order: 6361 * 1.) Connected conns. 6362 * 2.) Listeners. 6363 * 6364 * Even though #2 will be the common case for inbound traffic, only 6365 * following this order insures correctness. 6366 */ 6367 6368 if (sel->ips_local_port == 0) 6369 return; 6370 6371 /* 6372 * 0 should be fport, 1 should be lport. SRC is the local one here. 6373 * See ipsec_construct_inverse_acquire() for details. 6374 */ 6375 pptr[0] = sel->ips_remote_port; 6376 pptr[1] = sel->ips_local_port; 6377 6378 connfp = &ipst->ips_ipcl_conn_fanout[ 6379 IPCL_CONN_HASH(sel->ips_remote_addr_v4, ports, ipst)]; 6380 mutex_enter(&connfp->connf_lock); 6381 connp = connfp->connf_head; 6382 6383 if (sel->ips_isv4) { 6384 while (connp != NULL) { 6385 if (IPCL_CONN_MATCH(connp, IPPROTO_TCP, 6386 sel->ips_remote_addr_v4, sel->ips_local_addr_v4, 6387 ports)) 6388 break; 6389 connp = connp->conn_next; 6390 } 6391 } else { 6392 while (connp != NULL) { 6393 if (IPCL_CONN_MATCH_V6(connp, IPPROTO_TCP, 6394 sel->ips_remote_addr_v6, sel->ips_local_addr_v6, 6395 ports)) 6396 break; 6397 connp = connp->conn_next; 6398 } 6399 } 6400 6401 if (connp != NULL) { 6402 CONN_INC_REF(connp); 6403 mutex_exit(&connfp->connf_lock); 6404 } else { 6405 mutex_exit(&connfp->connf_lock); 6406 6407 /* Try the listen hash. */ 6408 if ((connp = ipsec_find_listen_conn(pptr, sel, ipst)) == NULL) 6409 return; 6410 } 6411 6412 ipsec_conn_pol(sel, connp, ppp); 6413 } 6414 6415 static void 6416 ipsec_sctp_pol(ipsec_selector_t *sel, ipsec_policy_t **ppp, 6417 ip_stack_t *ipst) 6418 { 6419 conn_t *connp; 6420 uint32_t ports; 6421 uint16_t *pptr = (uint16_t *)&ports; 6422 6423 /* 6424 * Find SCP state in the following order: 6425 * 1.) Connected conns. 6426 * 2.) Listeners. 6427 * 6428 * Even though #2 will be the common case for inbound traffic, only 6429 * following this order insures correctness. 6430 */ 6431 6432 if (sel->ips_local_port == 0) 6433 return; 6434 6435 /* 6436 * 0 should be fport, 1 should be lport. SRC is the local one here. 6437 * See ipsec_construct_inverse_acquire() for details. 6438 */ 6439 pptr[0] = sel->ips_remote_port; 6440 pptr[1] = sel->ips_local_port; 6441 6442 if (sel->ips_isv4) { 6443 in6_addr_t src, dst; 6444 6445 IN6_IPADDR_TO_V4MAPPED(sel->ips_remote_addr_v4, &dst); 6446 IN6_IPADDR_TO_V4MAPPED(sel->ips_local_addr_v4, &src); 6447 connp = sctp_find_conn(&dst, &src, ports, ALL_ZONES, 6448 ipst->ips_netstack->netstack_sctp); 6449 } else { 6450 connp = sctp_find_conn(&sel->ips_remote_addr_v6, 6451 &sel->ips_local_addr_v6, ports, ALL_ZONES, 6452 ipst->ips_netstack->netstack_sctp); 6453 } 6454 if (connp == NULL) 6455 return; 6456 ipsec_conn_pol(sel, connp, ppp); 6457 } 6458 6459 /* 6460 * Fill in a query for the SPD (in "sel") using two PF_KEY address extensions. 6461 * Returns 0 or errno, and always sets *diagnostic to something appropriate 6462 * to PF_KEY. 6463 * 6464 * NOTE: For right now, this function (and ipsec_selector_t for that matter), 6465 * ignore prefix lengths in the address extension. Since we match on first- 6466 * entered policies, this shouldn't matter. Also, since we normalize prefix- 6467 * set addresses to mask out the lower bits, we should get a suitable search 6468 * key for the SPD anyway. This is the function to change if the assumption 6469 * about suitable search keys is wrong. 6470 */ 6471 static int 6472 ipsec_get_inverse_acquire_sel(ipsec_selector_t *sel, sadb_address_t *srcext, 6473 sadb_address_t *dstext, int *diagnostic) 6474 { 6475 struct sockaddr_in *src, *dst; 6476 struct sockaddr_in6 *src6, *dst6; 6477 6478 *diagnostic = 0; 6479 6480 bzero(sel, sizeof (*sel)); 6481 sel->ips_protocol = srcext->sadb_address_proto; 6482 dst = (struct sockaddr_in *)(dstext + 1); 6483 if (dst->sin_family == AF_INET6) { 6484 dst6 = (struct sockaddr_in6 *)dst; 6485 src6 = (struct sockaddr_in6 *)(srcext + 1); 6486 if (src6->sin6_family != AF_INET6) { 6487 *diagnostic = SADB_X_DIAGNOSTIC_AF_MISMATCH; 6488 return (EINVAL); 6489 } 6490 sel->ips_remote_addr_v6 = dst6->sin6_addr; 6491 sel->ips_local_addr_v6 = src6->sin6_addr; 6492 if (sel->ips_protocol == IPPROTO_ICMPV6) { 6493 sel->ips_is_icmp_inv_acq = 1; 6494 } else { 6495 sel->ips_remote_port = dst6->sin6_port; 6496 sel->ips_local_port = src6->sin6_port; 6497 } 6498 sel->ips_isv4 = B_FALSE; 6499 } else { 6500 src = (struct sockaddr_in *)(srcext + 1); 6501 if (src->sin_family != AF_INET) { 6502 *diagnostic = SADB_X_DIAGNOSTIC_AF_MISMATCH; 6503 return (EINVAL); 6504 } 6505 sel->ips_remote_addr_v4 = dst->sin_addr.s_addr; 6506 sel->ips_local_addr_v4 = src->sin_addr.s_addr; 6507 if (sel->ips_protocol == IPPROTO_ICMP) { 6508 sel->ips_is_icmp_inv_acq = 1; 6509 } else { 6510 sel->ips_remote_port = dst->sin_port; 6511 sel->ips_local_port = src->sin_port; 6512 } 6513 sel->ips_isv4 = B_TRUE; 6514 } 6515 return (0); 6516 } 6517 6518 /* 6519 * We have encapsulation. 6520 * - Lookup tun_t by address and look for an associated 6521 * tunnel policy 6522 * - If there are inner selectors 6523 * - check ITPF_P_TUNNEL and ITPF_P_ACTIVE 6524 * - Look up tunnel policy based on selectors 6525 * - Else 6526 * - Sanity check the negotation 6527 * - If appropriate, fall through to global policy 6528 */ 6529 static int 6530 ipsec_tun_pol(ipsec_selector_t *sel, ipsec_policy_t **ppp, 6531 sadb_address_t *innsrcext, sadb_address_t *inndstext, ipsec_tun_pol_t *itp, 6532 int *diagnostic, netstack_t *ns) 6533 { 6534 int err; 6535 ipsec_policy_head_t *polhead; 6536 6537 /* Check for inner selectors and act appropriately */ 6538 6539 if (innsrcext != NULL) { 6540 /* Inner selectors present */ 6541 ASSERT(inndstext != NULL); 6542 if ((itp == NULL) || 6543 (itp->itp_flags & (ITPF_P_ACTIVE | ITPF_P_TUNNEL)) != 6544 (ITPF_P_ACTIVE | ITPF_P_TUNNEL)) { 6545 /* 6546 * If inner packet selectors, we must have negotiate 6547 * tunnel and active policy. If the tunnel has 6548 * transport-mode policy set on it, or has no policy, 6549 * fail. 6550 */ 6551 return (ENOENT); 6552 } else { 6553 /* 6554 * Reset "sel" to indicate inner selectors. Pass 6555 * inner PF_KEY address extensions for this to happen. 6556 */ 6557 err = ipsec_get_inverse_acquire_sel(sel, 6558 innsrcext, inndstext, diagnostic); 6559 if (err != 0) { 6560 ITP_REFRELE(itp, ns); 6561 return (err); 6562 } 6563 /* 6564 * Now look for a tunnel policy based on those inner 6565 * selectors. (Common code is below.) 6566 */ 6567 } 6568 } else { 6569 /* No inner selectors present */ 6570 if ((itp == NULL) || !(itp->itp_flags & ITPF_P_ACTIVE)) { 6571 /* 6572 * Transport mode negotiation with no tunnel policy 6573 * configured - return to indicate a global policy 6574 * check is needed. 6575 */ 6576 if (itp != NULL) { 6577 ITP_REFRELE(itp, ns); 6578 } 6579 return (0); 6580 } else if (itp->itp_flags & ITPF_P_TUNNEL) { 6581 /* Tunnel mode set with no inner selectors. */ 6582 ITP_REFRELE(itp, ns); 6583 return (ENOENT); 6584 } 6585 /* 6586 * Else, this is a tunnel policy configured with ifconfig(1m) 6587 * or "negotiate transport" with ipsecconf(1m). We have an 6588 * itp with policy set based on any match, so don't bother 6589 * changing fields in "sel". 6590 */ 6591 } 6592 6593 ASSERT(itp != NULL); 6594 polhead = itp->itp_policy; 6595 ASSERT(polhead != NULL); 6596 rw_enter(&polhead->iph_lock, RW_READER); 6597 *ppp = ipsec_find_policy_head(NULL, polhead, 6598 IPSEC_TYPE_INBOUND, sel, ns); 6599 rw_exit(&polhead->iph_lock); 6600 ITP_REFRELE(itp, ns); 6601 6602 /* 6603 * Don't default to global if we didn't find a matching policy entry. 6604 * Instead, send ENOENT, just like if we hit a transport-mode tunnel. 6605 */ 6606 if (*ppp == NULL) 6607 return (ENOENT); 6608 6609 return (0); 6610 } 6611 6612 static void 6613 ipsec_oth_pol(ipsec_selector_t *sel, ipsec_policy_t **ppp, 6614 ip_stack_t *ipst) 6615 { 6616 boolean_t isv4 = sel->ips_isv4; 6617 connf_t *connfp; 6618 conn_t *connp; 6619 6620 if (isv4) { 6621 connfp = &ipst->ips_ipcl_proto_fanout[sel->ips_protocol]; 6622 } else { 6623 connfp = &ipst->ips_ipcl_proto_fanout_v6[sel->ips_protocol]; 6624 } 6625 6626 mutex_enter(&connfp->connf_lock); 6627 for (connp = connfp->connf_head; connp != NULL; 6628 connp = connp->conn_next) { 6629 if (!((isv4 && !((connp->conn_src == 0 || 6630 connp->conn_src == sel->ips_local_addr_v4) && 6631 (connp->conn_rem == 0 || 6632 connp->conn_rem == sel->ips_remote_addr_v4))) || 6633 (!isv4 && !((IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) || 6634 IN6_ARE_ADDR_EQUAL(&connp->conn_srcv6, 6635 &sel->ips_local_addr_v6)) && 6636 (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6) || 6637 IN6_ARE_ADDR_EQUAL(&connp->conn_remv6, 6638 &sel->ips_remote_addr_v6)))))) { 6639 break; 6640 } 6641 } 6642 if (connp == NULL) { 6643 mutex_exit(&connfp->connf_lock); 6644 return; 6645 } 6646 6647 CONN_INC_REF(connp); 6648 mutex_exit(&connfp->connf_lock); 6649 6650 ipsec_conn_pol(sel, connp, ppp); 6651 } 6652 6653 /* 6654 * Construct an inverse ACQUIRE reply based on: 6655 * 6656 * 1.) Current global policy. 6657 * 2.) An conn_t match depending on what all was passed in the extv[]. 6658 * 3.) A tunnel's policy head. 6659 * ... 6660 * N.) Other stuff TBD (e.g. identities) 6661 * 6662 * If there is an error, set sadb_msg_errno and sadb_x_msg_diagnostic 6663 * in this function so the caller can extract them where appropriately. 6664 * 6665 * The SRC address is the local one - just like an outbound ACQUIRE message. 6666 */ 6667 mblk_t * 6668 ipsec_construct_inverse_acquire(sadb_msg_t *samsg, sadb_ext_t *extv[], 6669 netstack_t *ns) 6670 { 6671 int err; 6672 int diagnostic; 6673 sadb_address_t *srcext = (sadb_address_t *)extv[SADB_EXT_ADDRESS_SRC], 6674 *dstext = (sadb_address_t *)extv[SADB_EXT_ADDRESS_DST], 6675 *innsrcext = (sadb_address_t *)extv[SADB_X_EXT_ADDRESS_INNER_SRC], 6676 *inndstext = (sadb_address_t *)extv[SADB_X_EXT_ADDRESS_INNER_DST]; 6677 struct sockaddr_in6 *src, *dst; 6678 struct sockaddr_in6 *isrc, *idst; 6679 ipsec_tun_pol_t *itp = NULL; 6680 ipsec_policy_t *pp = NULL; 6681 ipsec_selector_t sel, isel; 6682 mblk_t *retmp; 6683 ip_stack_t *ipst = ns->netstack_ip; 6684 ipsec_stack_t *ipss = ns->netstack_ipsec; 6685 6686 /* Normalize addresses */ 6687 if (sadb_addrcheck(NULL, (mblk_t *)samsg, (sadb_ext_t *)srcext, 0, ns) 6688 == KS_IN_ADDR_UNKNOWN) { 6689 err = EINVAL; 6690 diagnostic = SADB_X_DIAGNOSTIC_BAD_SRC; 6691 goto bail; 6692 } 6693 src = (struct sockaddr_in6 *)(srcext + 1); 6694 if (sadb_addrcheck(NULL, (mblk_t *)samsg, (sadb_ext_t *)dstext, 0, ns) 6695 == KS_IN_ADDR_UNKNOWN) { 6696 err = EINVAL; 6697 diagnostic = SADB_X_DIAGNOSTIC_BAD_DST; 6698 goto bail; 6699 } 6700 dst = (struct sockaddr_in6 *)(dstext + 1); 6701 if (src->sin6_family != dst->sin6_family) { 6702 err = EINVAL; 6703 diagnostic = SADB_X_DIAGNOSTIC_AF_MISMATCH; 6704 goto bail; 6705 } 6706 6707 /* Check for tunnel mode and act appropriately */ 6708 if (innsrcext != NULL) { 6709 if (inndstext == NULL) { 6710 err = EINVAL; 6711 diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_DST; 6712 goto bail; 6713 } 6714 if (sadb_addrcheck(NULL, (mblk_t *)samsg, 6715 (sadb_ext_t *)innsrcext, 0, ns) == KS_IN_ADDR_UNKNOWN) { 6716 err = EINVAL; 6717 diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_INNER_SRC; 6718 goto bail; 6719 } 6720 isrc = (struct sockaddr_in6 *)(innsrcext + 1); 6721 if (sadb_addrcheck(NULL, (mblk_t *)samsg, 6722 (sadb_ext_t *)inndstext, 0, ns) == KS_IN_ADDR_UNKNOWN) { 6723 err = EINVAL; 6724 diagnostic = SADB_X_DIAGNOSTIC_MALFORMED_INNER_DST; 6725 goto bail; 6726 } 6727 idst = (struct sockaddr_in6 *)(inndstext + 1); 6728 if (isrc->sin6_family != idst->sin6_family) { 6729 err = EINVAL; 6730 diagnostic = SADB_X_DIAGNOSTIC_INNER_AF_MISMATCH; 6731 goto bail; 6732 } 6733 if (isrc->sin6_family != AF_INET && 6734 isrc->sin6_family != AF_INET6) { 6735 err = EINVAL; 6736 diagnostic = SADB_X_DIAGNOSTIC_BAD_INNER_SRC_AF; 6737 goto bail; 6738 } 6739 } else if (inndstext != NULL) { 6740 err = EINVAL; 6741 diagnostic = SADB_X_DIAGNOSTIC_MISSING_INNER_SRC; 6742 goto bail; 6743 } 6744 6745 /* Get selectors first, based on outer addresses */ 6746 err = ipsec_get_inverse_acquire_sel(&sel, srcext, dstext, &diagnostic); 6747 if (err != 0) 6748 goto bail; 6749 6750 /* Check for tunnel mode mismatches. */ 6751 if (innsrcext != NULL && 6752 ((isrc->sin6_family == AF_INET && 6753 sel.ips_protocol != IPPROTO_ENCAP && sel.ips_protocol != 0) || 6754 (isrc->sin6_family == AF_INET6 && 6755 sel.ips_protocol != IPPROTO_IPV6 && sel.ips_protocol != 0))) { 6756 err = EPROTOTYPE; 6757 goto bail; 6758 } 6759 6760 /* 6761 * Okay, we have the addresses and other selector information. 6762 * Let's first find a conn... 6763 */ 6764 pp = NULL; 6765 switch (sel.ips_protocol) { 6766 case IPPROTO_TCP: 6767 ipsec_tcp_pol(&sel, &pp, ipst); 6768 break; 6769 case IPPROTO_UDP: 6770 ipsec_udp_pol(&sel, &pp, ipst); 6771 break; 6772 case IPPROTO_SCTP: 6773 ipsec_sctp_pol(&sel, &pp, ipst); 6774 break; 6775 case IPPROTO_ENCAP: 6776 case IPPROTO_IPV6: 6777 rw_enter(&ipss->ipsec_itp_get_byaddr_rw_lock, RW_READER); 6778 /* 6779 * Assume sel.ips_remote_addr_* has the right address at 6780 * that exact position. 6781 */ 6782 itp = ipss->ipsec_itp_get_byaddr( 6783 (uint32_t *)(&sel.ips_local_addr_v6), 6784 (uint32_t *)(&sel.ips_remote_addr_v6), 6785 src->sin6_family, ns); 6786 rw_exit(&ipss->ipsec_itp_get_byaddr_rw_lock); 6787 if (innsrcext == NULL) { 6788 /* 6789 * Transport-mode tunnel, make sure we fake out isel 6790 * to contain something based on the outer protocol. 6791 */ 6792 bzero(&isel, sizeof (isel)); 6793 isel.ips_isv4 = (sel.ips_protocol == IPPROTO_ENCAP); 6794 } /* Else isel is initialized by ipsec_tun_pol(). */ 6795 err = ipsec_tun_pol(&isel, &pp, innsrcext, inndstext, itp, 6796 &diagnostic, ns); 6797 /* 6798 * NOTE: isel isn't used for now, but in RFC 430x IPsec, it 6799 * may be. 6800 */ 6801 if (err != 0) 6802 goto bail; 6803 break; 6804 default: 6805 ipsec_oth_pol(&sel, &pp, ipst); 6806 break; 6807 } 6808 6809 /* 6810 * If we didn't find a matching conn_t or other policy head, take a 6811 * look in the global policy. 6812 */ 6813 if (pp == NULL) { 6814 pp = ipsec_find_policy(IPSEC_TYPE_OUTBOUND, NULL, NULL, &sel, 6815 ns); 6816 if (pp == NULL) { 6817 /* There's no global policy. */ 6818 err = ENOENT; 6819 diagnostic = 0; 6820 goto bail; 6821 } 6822 } 6823 6824 /* 6825 * Now that we have a policy entry/widget, construct an ACQUIRE 6826 * message based on that, fix fields where appropriate, 6827 * and return the message. 6828 */ 6829 retmp = sadb_extended_acquire(&sel, pp, NULL, 6830 (itp != NULL && (itp->itp_flags & ITPF_P_TUNNEL)), 6831 samsg->sadb_msg_seq, samsg->sadb_msg_pid, ns); 6832 if (pp != NULL) { 6833 IPPOL_REFRELE(pp, ns); 6834 } 6835 if (retmp != NULL) { 6836 return (retmp); 6837 } else { 6838 err = ENOMEM; 6839 diagnostic = 0; 6840 } 6841 bail: 6842 samsg->sadb_msg_errno = (uint8_t)err; 6843 samsg->sadb_x_msg_diagnostic = (uint16_t)diagnostic; 6844 return (NULL); 6845 } 6846 6847 /* 6848 * ipsa_lpkt is a one-element queue, only manipulated by the next two 6849 * functions. They have to hold the ipsa_lock because of potential races 6850 * between key management using SADB_UPDATE, and inbound packets that may 6851 * queue up on the larval SA (hence the 'l' in "lpkt"). 6852 */ 6853 6854 /* 6855 * sadb_set_lpkt: Return TRUE if we can swap in a value to ipsa->ipsa_lpkt and 6856 * freemsg the previous value. Return FALSE if we lost the race and the SA is 6857 * in a non-LARVAL state. free clue: ip_drop_packet(NULL) is safe. 6858 */ 6859 boolean_t 6860 sadb_set_lpkt(ipsa_t *ipsa, mblk_t *npkt, netstack_t *ns) 6861 { 6862 mblk_t *opkt; 6863 ipsec_stack_t *ipss = ns->netstack_ipsec; 6864 boolean_t is_larval; 6865 6866 /* 6867 * Check the packet's netstack id in case we go asynch with a 6868 * taskq_dispatch. 6869 */ 6870 ASSERT(((ipsec_in_t *)npkt->b_rptr)->ipsec_in_type == IPSEC_IN); 6871 ASSERT(((ipsec_in_t *)npkt->b_rptr)->ipsec_in_stackid == 6872 ns->netstack_stackid); 6873 6874 mutex_enter(&ipsa->ipsa_lock); 6875 is_larval = (ipsa->ipsa_state == IPSA_STATE_LARVAL); 6876 if (is_larval) { 6877 opkt = ipsa->ipsa_lpkt; 6878 ipsa->ipsa_lpkt = npkt; 6879 } else { 6880 /* We lost the race. */ 6881 opkt = NULL; 6882 ASSERT(ipsa->ipsa_lpkt == NULL); 6883 } 6884 mutex_exit(&ipsa->ipsa_lock); 6885 6886 ip_drop_packet(opkt, B_TRUE, NULL, NULL, 6887 DROPPER(ipss, ipds_sadb_inlarval_replace), 6888 &ipss->ipsec_sadb_dropper); 6889 return (is_larval); 6890 } 6891 6892 /* 6893 * sadb_clear_lpkt: Atomically clear ipsa->ipsa_lpkt and return the 6894 * previous value. 6895 */ 6896 mblk_t * 6897 sadb_clear_lpkt(ipsa_t *ipsa) 6898 { 6899 mblk_t *opkt; 6900 6901 mutex_enter(&ipsa->ipsa_lock); 6902 opkt = ipsa->ipsa_lpkt; 6903 ipsa->ipsa_lpkt = NULL; 6904 mutex_exit(&ipsa->ipsa_lock); 6905 6906 return (opkt); 6907 } 6908 6909 /* 6910 * Buffer a packet that's in IDLE state as set by Solaris Clustering. 6911 */ 6912 void 6913 sadb_buf_pkt(ipsa_t *ipsa, mblk_t *bpkt, netstack_t *ns) 6914 { 6915 ipsec_stack_t *ipss = ns->netstack_ipsec; 6916 extern void (*cl_inet_idlesa)(netstackid_t, uint8_t, uint32_t, 6917 sa_family_t, in6_addr_t, in6_addr_t, void *); 6918 in6_addr_t *srcaddr = (in6_addr_t *)(&ipsa->ipsa_srcaddr); 6919 in6_addr_t *dstaddr = (in6_addr_t *)(&ipsa->ipsa_dstaddr); 6920 6921 ASSERT(ipsa->ipsa_state == IPSA_STATE_IDLE); 6922 6923 if (cl_inet_idlesa == NULL) { 6924 ip_drop_packet(bpkt, B_TRUE, NULL, NULL, 6925 DROPPER(ipss, ipds_sadb_inidle_overflow), 6926 &ipss->ipsec_sadb_dropper); 6927 return; 6928 } 6929 6930 cl_inet_idlesa(ns->netstack_stackid, 6931 (ipsa->ipsa_type == SADB_SATYPE_AH) ? IPPROTO_AH : IPPROTO_ESP, 6932 ipsa->ipsa_spi, ipsa->ipsa_addrfam, *srcaddr, *dstaddr, NULL); 6933 6934 /* 6935 * Check the packet's netstack id in case we go asynch with a 6936 * taskq_dispatch. 6937 */ 6938 ASSERT(((ipsec_in_t *)bpkt->b_rptr)->ipsec_in_type == IPSEC_IN); 6939 ASSERT(((ipsec_in_t *)bpkt->b_rptr)->ipsec_in_stackid == 6940 ns->netstack_stackid); 6941 6942 mutex_enter(&ipsa->ipsa_lock); 6943 ipsa->ipsa_mblkcnt++; 6944 if (ipsa->ipsa_bpkt_head == NULL) { 6945 ipsa->ipsa_bpkt_head = ipsa->ipsa_bpkt_tail = bpkt; 6946 } else { 6947 ipsa->ipsa_bpkt_tail->b_next = bpkt; 6948 ipsa->ipsa_bpkt_tail = bpkt; 6949 if (ipsa->ipsa_mblkcnt > SADB_MAX_IDLEPKTS) { 6950 mblk_t *tmp; 6951 tmp = ipsa->ipsa_bpkt_head; 6952 ipsa->ipsa_bpkt_head = ipsa->ipsa_bpkt_head->b_next; 6953 ip_drop_packet(tmp, B_TRUE, NULL, NULL, 6954 DROPPER(ipss, ipds_sadb_inidle_overflow), 6955 &ipss->ipsec_sadb_dropper); 6956 ipsa->ipsa_mblkcnt --; 6957 } 6958 } 6959 mutex_exit(&ipsa->ipsa_lock); 6960 6961 } 6962 6963 /* 6964 * Stub function that taskq_dispatch() invokes to take the mblk (in arg) 6965 * and put into STREAMS again. 6966 */ 6967 void 6968 sadb_clear_buf_pkt(void *ipkt) 6969 { 6970 mblk_t *tmp, *buf_pkt; 6971 netstack_t *ns; 6972 ipsec_in_t *ii; 6973 6974 buf_pkt = (mblk_t *)ipkt; 6975 6976 ii = (ipsec_in_t *)buf_pkt->b_rptr; 6977 ASSERT(ii->ipsec_in_type == IPSEC_IN); 6978 ns = netstack_find_by_stackid(ii->ipsec_in_stackid); 6979 if (ns != NULL && ns != ii->ipsec_in_ns) { 6980 netstack_rele(ns); 6981 ns = NULL; /* For while-loop below. */ 6982 } 6983 6984 while (buf_pkt != NULL) { 6985 tmp = buf_pkt->b_next; 6986 buf_pkt->b_next = NULL; 6987 if (ns != NULL) 6988 ip_fanout_proto_again(buf_pkt, NULL, NULL, NULL); 6989 else 6990 freemsg(buf_pkt); 6991 buf_pkt = tmp; 6992 } 6993 if (ns != NULL) 6994 netstack_rele(ns); 6995 } 6996 /* 6997 * Walker callback used by sadb_alg_update() to free/create crypto 6998 * context template when a crypto software provider is removed or 6999 * added. 7000 */ 7001 7002 struct sadb_update_alg_state { 7003 ipsec_algtype_t alg_type; 7004 uint8_t alg_id; 7005 boolean_t is_added; 7006 }; 7007 7008 static void 7009 sadb_alg_update_cb(isaf_t *head, ipsa_t *entry, void *cookie) 7010 { 7011 struct sadb_update_alg_state *update_state = 7012 (struct sadb_update_alg_state *)cookie; 7013 crypto_ctx_template_t *ctx_tmpl = NULL; 7014 7015 ASSERT(MUTEX_HELD(&head->isaf_lock)); 7016 7017 if (entry->ipsa_state == IPSA_STATE_LARVAL) 7018 return; 7019 7020 mutex_enter(&entry->ipsa_lock); 7021 7022 switch (update_state->alg_type) { 7023 case IPSEC_ALG_AUTH: 7024 if (entry->ipsa_auth_alg == update_state->alg_id) 7025 ctx_tmpl = &entry->ipsa_authtmpl; 7026 break; 7027 case IPSEC_ALG_ENCR: 7028 if (entry->ipsa_encr_alg == update_state->alg_id) 7029 ctx_tmpl = &entry->ipsa_encrtmpl; 7030 break; 7031 default: 7032 ctx_tmpl = NULL; 7033 } 7034 7035 if (ctx_tmpl == NULL) { 7036 mutex_exit(&entry->ipsa_lock); 7037 return; 7038 } 7039 7040 /* 7041 * The context template of the SA may be affected by the change 7042 * of crypto provider. 7043 */ 7044 if (update_state->is_added) { 7045 /* create the context template if not already done */ 7046 if (*ctx_tmpl == NULL) { 7047 (void) ipsec_create_ctx_tmpl(entry, 7048 update_state->alg_type); 7049 } 7050 } else { 7051 /* 7052 * The crypto provider was removed. If the context template 7053 * exists but it is no longer valid, free it. 7054 */ 7055 if (*ctx_tmpl != NULL) 7056 ipsec_destroy_ctx_tmpl(entry, update_state->alg_type); 7057 } 7058 7059 mutex_exit(&entry->ipsa_lock); 7060 } 7061 7062 /* 7063 * Invoked by IP when an software crypto provider has been updated. 7064 * The type and id of the corresponding algorithm is passed as argument. 7065 * is_added is B_TRUE if the provider was added, B_FALSE if it was 7066 * removed. The function updates the SADB and free/creates the 7067 * context templates associated with SAs if needed. 7068 */ 7069 7070 #define SADB_ALG_UPDATE_WALK(sadb, table) \ 7071 sadb_walker((sadb).table, (sadb).sdb_hashsize, sadb_alg_update_cb, \ 7072 &update_state) 7073 7074 void 7075 sadb_alg_update(ipsec_algtype_t alg_type, uint8_t alg_id, boolean_t is_added, 7076 netstack_t *ns) 7077 { 7078 struct sadb_update_alg_state update_state; 7079 ipsecah_stack_t *ahstack = ns->netstack_ipsecah; 7080 ipsecesp_stack_t *espstack = ns->netstack_ipsecesp; 7081 7082 update_state.alg_type = alg_type; 7083 update_state.alg_id = alg_id; 7084 update_state.is_added = is_added; 7085 7086 if (alg_type == IPSEC_ALG_AUTH) { 7087 /* walk the AH tables only for auth. algorithm changes */ 7088 SADB_ALG_UPDATE_WALK(ahstack->ah_sadb.s_v4, sdb_of); 7089 SADB_ALG_UPDATE_WALK(ahstack->ah_sadb.s_v4, sdb_if); 7090 SADB_ALG_UPDATE_WALK(ahstack->ah_sadb.s_v6, sdb_of); 7091 SADB_ALG_UPDATE_WALK(ahstack->ah_sadb.s_v6, sdb_if); 7092 } 7093 7094 /* walk the ESP tables */ 7095 SADB_ALG_UPDATE_WALK(espstack->esp_sadb.s_v4, sdb_of); 7096 SADB_ALG_UPDATE_WALK(espstack->esp_sadb.s_v4, sdb_if); 7097 SADB_ALG_UPDATE_WALK(espstack->esp_sadb.s_v6, sdb_of); 7098 SADB_ALG_UPDATE_WALK(espstack->esp_sadb.s_v6, sdb_if); 7099 } 7100 7101 /* 7102 * Creates a context template for the specified SA. This function 7103 * is called when an SA is created and when a context template needs 7104 * to be created due to a change of software provider. 7105 */ 7106 int 7107 ipsec_create_ctx_tmpl(ipsa_t *sa, ipsec_algtype_t alg_type) 7108 { 7109 ipsec_alginfo_t *alg; 7110 crypto_mechanism_t mech; 7111 crypto_key_t *key; 7112 crypto_ctx_template_t *sa_tmpl; 7113 int rv; 7114 ipsec_stack_t *ipss = sa->ipsa_netstack->netstack_ipsec; 7115 7116 ASSERT(MUTEX_HELD(&ipss->ipsec_alg_lock)); 7117 ASSERT(MUTEX_HELD(&sa->ipsa_lock)); 7118 7119 /* get pointers to the algorithm info, context template, and key */ 7120 switch (alg_type) { 7121 case IPSEC_ALG_AUTH: 7122 key = &sa->ipsa_kcfauthkey; 7123 sa_tmpl = &sa->ipsa_authtmpl; 7124 alg = ipss->ipsec_alglists[alg_type][sa->ipsa_auth_alg]; 7125 break; 7126 case IPSEC_ALG_ENCR: 7127 key = &sa->ipsa_kcfencrkey; 7128 sa_tmpl = &sa->ipsa_encrtmpl; 7129 alg = ipss->ipsec_alglists[alg_type][sa->ipsa_encr_alg]; 7130 break; 7131 default: 7132 alg = NULL; 7133 } 7134 7135 if (alg == NULL || !ALG_VALID(alg)) 7136 return (EINVAL); 7137 7138 /* initialize the mech info structure for the framework */ 7139 ASSERT(alg->alg_mech_type != CRYPTO_MECHANISM_INVALID); 7140 mech.cm_type = alg->alg_mech_type; 7141 mech.cm_param = NULL; 7142 mech.cm_param_len = 0; 7143 7144 /* create a new context template */ 7145 rv = crypto_create_ctx_template(&mech, key, sa_tmpl, KM_NOSLEEP); 7146 7147 /* 7148 * CRYPTO_MECH_NOT_SUPPORTED can be returned if only hardware 7149 * providers are available for that mechanism. In that case 7150 * we don't fail, and will generate the context template from 7151 * the framework callback when a software provider for that 7152 * mechanism registers. 7153 * 7154 * The context template is assigned the special value 7155 * IPSEC_CTX_TMPL_ALLOC if the allocation failed due to a 7156 * lack of memory. No attempt will be made to use 7157 * the context template if it is set to this value. 7158 */ 7159 if (rv == CRYPTO_HOST_MEMORY) { 7160 *sa_tmpl = IPSEC_CTX_TMPL_ALLOC; 7161 } else if (rv != CRYPTO_SUCCESS) { 7162 *sa_tmpl = NULL; 7163 if (rv != CRYPTO_MECH_NOT_SUPPORTED) 7164 return (EINVAL); 7165 } 7166 7167 return (0); 7168 } 7169 7170 /* 7171 * Destroy the context template of the specified algorithm type 7172 * of the specified SA. Must be called while holding the SA lock. 7173 */ 7174 void 7175 ipsec_destroy_ctx_tmpl(ipsa_t *sa, ipsec_algtype_t alg_type) 7176 { 7177 ASSERT(MUTEX_HELD(&sa->ipsa_lock)); 7178 7179 if (alg_type == IPSEC_ALG_AUTH) { 7180 if (sa->ipsa_authtmpl == IPSEC_CTX_TMPL_ALLOC) 7181 sa->ipsa_authtmpl = NULL; 7182 else if (sa->ipsa_authtmpl != NULL) { 7183 crypto_destroy_ctx_template(sa->ipsa_authtmpl); 7184 sa->ipsa_authtmpl = NULL; 7185 } 7186 } else { 7187 ASSERT(alg_type == IPSEC_ALG_ENCR); 7188 if (sa->ipsa_encrtmpl == IPSEC_CTX_TMPL_ALLOC) 7189 sa->ipsa_encrtmpl = NULL; 7190 else if (sa->ipsa_encrtmpl != NULL) { 7191 crypto_destroy_ctx_template(sa->ipsa_encrtmpl); 7192 sa->ipsa_encrtmpl = NULL; 7193 } 7194 } 7195 } 7196 7197 /* 7198 * Use the kernel crypto framework to check the validity of a key received 7199 * via keysock. Returns 0 if the key is OK, -1 otherwise. 7200 */ 7201 int 7202 ipsec_check_key(crypto_mech_type_t mech_type, sadb_key_t *sadb_key, 7203 boolean_t is_auth, int *diag) 7204 { 7205 crypto_mechanism_t mech; 7206 crypto_key_t crypto_key; 7207 int crypto_rc; 7208 7209 mech.cm_type = mech_type; 7210 mech.cm_param = NULL; 7211 mech.cm_param_len = 0; 7212 7213 crypto_key.ck_format = CRYPTO_KEY_RAW; 7214 crypto_key.ck_data = sadb_key + 1; 7215 crypto_key.ck_length = sadb_key->sadb_key_bits; 7216 7217 crypto_rc = crypto_key_check(&mech, &crypto_key); 7218 7219 switch (crypto_rc) { 7220 case CRYPTO_SUCCESS: 7221 return (0); 7222 case CRYPTO_MECHANISM_INVALID: 7223 case CRYPTO_MECH_NOT_SUPPORTED: 7224 *diag = is_auth ? SADB_X_DIAGNOSTIC_BAD_AALG : 7225 SADB_X_DIAGNOSTIC_BAD_EALG; 7226 break; 7227 case CRYPTO_KEY_SIZE_RANGE: 7228 *diag = is_auth ? SADB_X_DIAGNOSTIC_BAD_AKEYBITS : 7229 SADB_X_DIAGNOSTIC_BAD_EKEYBITS; 7230 break; 7231 case CRYPTO_WEAK_KEY: 7232 *diag = is_auth ? SADB_X_DIAGNOSTIC_WEAK_AKEY : 7233 SADB_X_DIAGNOSTIC_WEAK_EKEY; 7234 break; 7235 } 7236 7237 return (-1); 7238 } 7239 /* 7240 * If this is an outgoing SA then add some fuzz to the 7241 * SOFT EXPIRE time. The reason for this is to stop 7242 * peers trying to renegotiate SOFT expiring SA's at 7243 * the same time. The amount of fuzz needs to be at 7244 * least 10 seconds which is the typical interval 7245 * sadb_ager(), although this is only a guide as it 7246 * selftunes. 7247 */ 7248 void 7249 lifetime_fuzz(ipsa_t *assoc) 7250 { 7251 uint8_t rnd; 7252 7253 if (assoc->ipsa_softaddlt == 0) 7254 return; 7255 7256 (void) random_get_pseudo_bytes(&rnd, sizeof (rnd)); 7257 rnd = (rnd & 0xF) + 10; 7258 assoc->ipsa_softexpiretime -= rnd; 7259 assoc->ipsa_softaddlt -= rnd; 7260 } 7261 void 7262 destroy_ipsa_pair(ipsap_t *ipsapp) 7263 { 7264 if (ipsapp == NULL) 7265 return; 7266 7267 /* 7268 * Because of the multi-line macro nature of IPSA_REFRELE, keep 7269 * them in { }. 7270 */ 7271 if (ipsapp->ipsap_sa_ptr != NULL) { 7272 IPSA_REFRELE(ipsapp->ipsap_sa_ptr); 7273 } 7274 if (ipsapp->ipsap_psa_ptr != NULL) { 7275 IPSA_REFRELE(ipsapp->ipsap_psa_ptr); 7276 } 7277 7278 kmem_free(ipsapp, sizeof (*ipsapp)); 7279 } 7280 7281 /* 7282 * The sadb_ager() function walks through the hash tables of SA's and ages 7283 * them, if the SA expires as a result, its marked as DEAD and will be reaped 7284 * the next time sadb_ager() runs. SA's which are paired or have a peer (same 7285 * SA appears in both the inbound and outbound tables because its not possible 7286 * to determine its direction) are placed on a list when they expire. This is 7287 * to ensure that pair/peer SA's are reaped at the same time, even if they 7288 * expire at different times. 7289 * 7290 * This function is called twice by sadb_ager(), one after processing the 7291 * inbound table, then again after processing the outbound table. 7292 */ 7293 void 7294 age_pair_peer_list(templist_t *haspeerlist, sadb_t *sp, boolean_t outbound) 7295 { 7296 templist_t *listptr; 7297 int outhash; 7298 isaf_t *bucket; 7299 boolean_t haspeer; 7300 ipsa_t *peer_assoc, *dying; 7301 /* 7302 * Haspeer cases will contain both IPv4 and IPv6. This code 7303 * is address independent. 7304 */ 7305 while (haspeerlist != NULL) { 7306 /* "dying" contains the SA that has a peer. */ 7307 dying = haspeerlist->ipsa; 7308 haspeer = (dying->ipsa_haspeer); 7309 listptr = haspeerlist; 7310 haspeerlist = listptr->next; 7311 kmem_free(listptr, sizeof (*listptr)); 7312 /* 7313 * Pick peer bucket based on addrfam. 7314 */ 7315 if (outbound) { 7316 if (haspeer) 7317 bucket = INBOUND_BUCKET(sp, dying->ipsa_spi); 7318 else 7319 bucket = INBOUND_BUCKET(sp, 7320 dying->ipsa_otherspi); 7321 } else { /* inbound */ 7322 if (haspeer) { 7323 if (dying->ipsa_addrfam == AF_INET6) { 7324 outhash = OUTBOUND_HASH_V6(sp, 7325 *((in6_addr_t *)&dying-> 7326 ipsa_dstaddr)); 7327 } else { 7328 outhash = OUTBOUND_HASH_V4(sp, 7329 *((ipaddr_t *)&dying-> 7330 ipsa_dstaddr)); 7331 } 7332 } else if (dying->ipsa_addrfam == AF_INET6) { 7333 outhash = OUTBOUND_HASH_V6(sp, 7334 *((in6_addr_t *)&dying-> 7335 ipsa_srcaddr)); 7336 } else { 7337 outhash = OUTBOUND_HASH_V4(sp, 7338 *((ipaddr_t *)&dying-> 7339 ipsa_srcaddr)); 7340 } 7341 bucket = &(sp->sdb_of[outhash]); 7342 } 7343 7344 mutex_enter(&bucket->isaf_lock); 7345 /* 7346 * "haspeer" SA's have the same src/dst address ordering, 7347 * "paired" SA's have the src/dst addresses reversed. 7348 */ 7349 if (haspeer) { 7350 peer_assoc = ipsec_getassocbyspi(bucket, 7351 dying->ipsa_spi, dying->ipsa_srcaddr, 7352 dying->ipsa_dstaddr, dying->ipsa_addrfam); 7353 } else { 7354 peer_assoc = ipsec_getassocbyspi(bucket, 7355 dying->ipsa_otherspi, dying->ipsa_dstaddr, 7356 dying->ipsa_srcaddr, dying->ipsa_addrfam); 7357 } 7358 7359 mutex_exit(&bucket->isaf_lock); 7360 if (peer_assoc != NULL) { 7361 mutex_enter(&peer_assoc->ipsa_lock); 7362 mutex_enter(&dying->ipsa_lock); 7363 if (!haspeer) { 7364 /* 7365 * Only SA's which have a "peer" or are 7366 * "paired" end up on this list, so this 7367 * must be a "paired" SA, update the flags 7368 * to break the pair. 7369 */ 7370 peer_assoc->ipsa_otherspi = 0; 7371 peer_assoc->ipsa_flags &= ~IPSA_F_PAIRED; 7372 dying->ipsa_otherspi = 0; 7373 dying->ipsa_flags &= ~IPSA_F_PAIRED; 7374 } 7375 if (haspeer || outbound) { 7376 /* 7377 * Update the state of the "inbound" SA when 7378 * the "outbound" SA has expired. Don't update 7379 * the "outbound" SA when the "inbound" SA 7380 * SA expires because setting the hard_addtime 7381 * below will cause this to happen. 7382 */ 7383 peer_assoc->ipsa_state = dying->ipsa_state; 7384 } 7385 if (dying->ipsa_state == IPSA_STATE_DEAD) 7386 peer_assoc->ipsa_hardexpiretime = 1; 7387 7388 mutex_exit(&dying->ipsa_lock); 7389 mutex_exit(&peer_assoc->ipsa_lock); 7390 IPSA_REFRELE(peer_assoc); 7391 } 7392 IPSA_REFRELE(dying); 7393 } 7394 } 7395