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, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/stream.h> 31 #include <sys/stropts.h> 32 #include <sys/errno.h> 33 #include <sys/strlog.h> 34 #include <sys/tihdr.h> 35 #include <sys/socket.h> 36 #include <sys/ddi.h> 37 #include <sys/sunddi.h> 38 #include <sys/kmem.h> 39 #include <sys/cmn_err.h> 40 #include <sys/vtrace.h> 41 #include <sys/debug.h> 42 #include <sys/atomic.h> 43 #include <sys/strsun.h> 44 #include <sys/random.h> 45 #include <netinet/in.h> 46 #include <net/if.h> 47 #include <netinet/ip6.h> 48 #include <netinet/icmp6.h> 49 #include <net/pfkeyv2.h> 50 51 #include <inet/common.h> 52 #include <inet/mi.h> 53 #include <inet/ip.h> 54 #include <inet/ip6.h> 55 #include <inet/nd.h> 56 #include <inet/ipsec_info.h> 57 #include <inet/ipsec_impl.h> 58 #include <inet/sadb.h> 59 #include <inet/ipsecah.h> 60 #include <inet/ipsec_impl.h> 61 #include <inet/ipdrop.h> 62 #include <sys/taskq.h> 63 #include <sys/policy.h> 64 #include <sys/iphada.h> 65 #include <sys/strsun.h> 66 67 #include <sys/crypto/common.h> 68 #include <sys/crypto/api.h> 69 #include <sys/kstat.h> 70 71 /* Packet dropper for AH drops. */ 72 static ipdropper_t ah_dropper; 73 74 static kmutex_t ipsecah_param_lock; /* Protect ipsecah_param_arr[] below. */ 75 /* 76 * Table of ND variables supported by ipsecah. These are loaded into 77 * ipsecah_g_nd in ipsecah_init_nd. 78 * All of these are alterable, within the min/max values given, at run time. 79 */ 80 static ipsecahparam_t ipsecah_param_arr[] = { 81 /* min max value name */ 82 { 0, 3, 0, "ipsecah_debug"}, 83 { 125, 32000, SADB_AGE_INTERVAL_DEFAULT, "ipsecah_age_interval"}, 84 { 1, 10, 1, "ipsecah_reap_delay"}, 85 { 1, SADB_MAX_REPLAY, 64, "ipsecah_replay_size"}, 86 { 1, 300, 15, "ipsecah_acquire_timeout"}, 87 { 1, 1800, 90, "ipsecah_larval_timeout"}, 88 /* Default lifetime values for ACQUIRE messages. */ 89 { 0, 0xffffffffU, 0, "ipsecah_default_soft_bytes"}, 90 { 0, 0xffffffffU, 0, "ipsecah_default_hard_bytes"}, 91 { 0, 0xffffffffU, 24000, "ipsecah_default_soft_addtime"}, 92 { 0, 0xffffffffU, 28800, "ipsecah_default_hard_addtime"}, 93 { 0, 0xffffffffU, 0, "ipsecah_default_soft_usetime"}, 94 { 0, 0xffffffffU, 0, "ipsecah_default_hard_usetime"}, 95 { 0, 1, 0, "ipsecah_log_unknown_spi"}, 96 }; 97 #define ipsecah_debug ipsecah_param_arr[0].ipsecah_param_value 98 #define ipsecah_age_interval ipsecah_param_arr[1].ipsecah_param_value 99 #define ipsecah_age_int_max ipsecah_param_arr[1].ipsecah_param_max 100 #define ipsecah_reap_delay ipsecah_param_arr[2].ipsecah_param_value 101 #define ipsecah_replay_size ipsecah_param_arr[3].ipsecah_param_value 102 #define ipsecah_acquire_timeout ipsecah_param_arr[4].ipsecah_param_value 103 #define ipsecah_larval_timeout ipsecah_param_arr[5].ipsecah_param_value 104 #define ipsecah_default_soft_bytes ipsecah_param_arr[6].ipsecah_param_value 105 #define ipsecah_default_hard_bytes ipsecah_param_arr[7].ipsecah_param_value 106 #define ipsecah_default_soft_addtime ipsecah_param_arr[8].ipsecah_param_value 107 #define ipsecah_default_hard_addtime ipsecah_param_arr[9].ipsecah_param_value 108 #define ipsecah_default_soft_usetime ipsecah_param_arr[10].ipsecah_param_value 109 #define ipsecah_default_hard_usetime ipsecah_param_arr[11].ipsecah_param_value 110 #define ipsecah_log_unknown_spi ipsecah_param_arr[12].ipsecah_param_value 111 112 #define ah0dbg(a) printf a 113 /* NOTE: != 0 instead of > 0 so lint doesn't complain. */ 114 #define ah1dbg(a) if (ipsecah_debug != 0) printf a 115 #define ah2dbg(a) if (ipsecah_debug > 1) printf a 116 #define ah3dbg(a) if (ipsecah_debug > 2) printf a 117 118 static IDP ipsecah_g_nd; 119 120 /* 121 * XXX This is broken. Padding should be determined dynamically 122 * depending on the ICV size and IP version number so that the 123 * total AH header size is a multiple of 32 bits or 64 bits 124 * for V4 and V6 respectively. For 96bit ICVs we have no problems. 125 * Anything different from that, we need to fix our code. 126 */ 127 #define IPV4_PADDING_ALIGN 0x04 /* Multiple of 32 bits */ 128 #define IPV6_PADDING_ALIGN 0x04 /* Multiple of 32 bits */ 129 130 /* 131 * Helper macro. Avoids a call to msgdsize if there is only one 132 * mblk in the chain. 133 */ 134 #define AH_MSGSIZE(mp) ((mp)->b_cont != NULL ? msgdsize(mp) : MBLKL(mp)) 135 136 static ipsec_status_t ah_auth_out_done(mblk_t *); 137 static ipsec_status_t ah_auth_in_done(mblk_t *); 138 static mblk_t *ah_process_ip_options_v4(mblk_t *, ipsa_t *, int *, uint_t, 139 boolean_t); 140 static mblk_t *ah_process_ip_options_v6(mblk_t *, ipsa_t *, int *, uint_t, 141 boolean_t); 142 static void ah_getspi(mblk_t *, keysock_in_t *); 143 static ipsec_status_t ah_inbound_accelerated(mblk_t *, boolean_t, ipsa_t *, 144 uint32_t); 145 static ipsec_status_t ah_outbound_accelerated_v4(mblk_t *, ipsa_t *); 146 static ipsec_status_t ah_outbound_accelerated_v6(mblk_t *, ipsa_t *); 147 static ipsec_status_t ah_outbound(mblk_t *); 148 149 static int ipsecah_open(queue_t *, dev_t *, int, int, cred_t *); 150 static int ipsecah_close(queue_t *); 151 static void ipsecah_rput(queue_t *, mblk_t *); 152 static void ipsecah_wput(queue_t *, mblk_t *); 153 static void ah_send_acquire(ipsacq_t *, mblk_t *); 154 static boolean_t ah_register_out(uint32_t, uint32_t, uint_t); 155 156 static struct module_info info = { 157 5136, "ipsecah", 0, INFPSZ, 65536, 1024 158 }; 159 160 static struct qinit rinit = { 161 (pfi_t)ipsecah_rput, NULL, ipsecah_open, ipsecah_close, NULL, &info, 162 NULL 163 }; 164 165 static struct qinit winit = { 166 (pfi_t)ipsecah_wput, NULL, ipsecah_open, ipsecah_close, NULL, &info, 167 NULL 168 }; 169 170 struct streamtab ipsecahinfo = { 171 &rinit, &winit, NULL, NULL 172 }; 173 174 /* 175 * Keysock instance of AH. "There can be only one." :) 176 * Use casptr() on this because I don't set it until KEYSOCK_HELLO comes down. 177 * Paired up with the ah_pfkey_q is the ah_event, which will age SAs. 178 */ 179 static queue_t *ah_pfkey_q; 180 static timeout_id_t ah_event; 181 static taskq_t *ah_taskq; 182 183 static mblk_t *ah_ip_unbind; 184 185 /* 186 * Stats. This may eventually become a full-blown SNMP MIB once that spec 187 * stabilizes. 188 */ 189 typedef struct 190 { 191 kstat_named_t ah_stat_num_aalgs; 192 kstat_named_t ah_stat_good_auth; 193 kstat_named_t ah_stat_bad_auth; 194 kstat_named_t ah_stat_replay_failures; 195 kstat_named_t ah_stat_replay_early_failures; 196 kstat_named_t ah_stat_keysock_in; 197 kstat_named_t ah_stat_out_requests; 198 kstat_named_t ah_stat_acquire_requests; 199 kstat_named_t ah_stat_bytes_expired; 200 kstat_named_t ah_stat_out_discards; 201 kstat_named_t ah_stat_in_accelerated; 202 kstat_named_t ah_stat_out_accelerated; 203 kstat_named_t ah_stat_noaccel; 204 kstat_named_t ah_stat_crypto_sync; 205 kstat_named_t ah_stat_crypto_async; 206 kstat_named_t ah_stat_crypto_failures; 207 } ah_kstats_t; 208 209 #define AH_BUMP_STAT(x) (ah_kstats->ah_stat_ ## x).value.ui64++ 210 #define AH_DEBUMP_STAT(x) (ah_kstats->ah_stat_ ## x).value.ui64-- 211 212 uint32_t ah_hash_size = IPSEC_DEFAULT_HASH_SIZE; 213 static kstat_t *ah_ksp; 214 static ah_kstats_t *ah_kstats; 215 216 static int ah_kstat_update(kstat_t *, int); 217 218 static boolean_t 219 ah_kstat_init(void) 220 { 221 222 ah_ksp = kstat_create("ipsecah", 0, "ah_stat", "net", 223 KSTAT_TYPE_NAMED, sizeof (*ah_kstats) / sizeof (kstat_named_t), 224 KSTAT_FLAG_PERSISTENT); 225 226 if (ah_ksp == NULL) 227 return (B_FALSE); 228 229 ah_kstats = ah_ksp->ks_data; 230 231 ah_ksp->ks_update = ah_kstat_update; 232 233 #define K64 KSTAT_DATA_UINT64 234 #define KI(x) kstat_named_init(&(ah_kstats->ah_stat_##x), #x, K64) 235 236 KI(num_aalgs); 237 KI(good_auth); 238 KI(bad_auth); 239 KI(replay_failures); 240 KI(replay_early_failures); 241 KI(keysock_in); 242 KI(out_requests); 243 KI(acquire_requests); 244 KI(bytes_expired); 245 KI(out_discards); 246 KI(in_accelerated); 247 KI(out_accelerated); 248 KI(noaccel); 249 KI(crypto_sync); 250 KI(crypto_async); 251 KI(crypto_failures); 252 253 #undef KI 254 #undef K64 255 256 kstat_install(ah_ksp); 257 258 return (B_TRUE); 259 } 260 261 static int 262 ah_kstat_update(kstat_t *kp, int rw) 263 { 264 ah_kstats_t *ekp; 265 266 if ((kp == NULL) || (kp->ks_data == NULL)) 267 return (EIO); 268 269 if (rw == KSTAT_WRITE) 270 return (EACCES); 271 272 ASSERT(kp == ah_ksp); 273 ekp = (ah_kstats_t *)kp->ks_data; 274 ASSERT(ekp == ah_kstats); 275 276 mutex_enter(&alg_lock); 277 ekp->ah_stat_num_aalgs.value.ui64 = ipsec_nalgs[IPSEC_ALG_AUTH]; 278 mutex_exit(&alg_lock); 279 280 return (0); 281 } 282 283 /* 284 * Don't have to lock ipsec_age_interval, as only one thread will access it at 285 * a time, because I control the one function that does a qtimeout() on 286 * ah_pfkey_q. 287 */ 288 /* ARGSUSED */ 289 static void 290 ah_ager(void *ignoreme) 291 { 292 hrtime_t begin = gethrtime(); 293 294 sadb_ager(&ah_sadb.s_v4, ah_pfkey_q, ah_sadb.s_ip_q, 295 ipsecah_reap_delay); 296 sadb_ager(&ah_sadb.s_v6, ah_pfkey_q, ah_sadb.s_ip_q, 297 ipsecah_reap_delay); 298 299 ah_event = sadb_retimeout(begin, ah_pfkey_q, ah_ager, 300 &ipsecah_age_interval, ipsecah_age_int_max, info.mi_idnum); 301 } 302 303 /* 304 * Get an AH NDD parameter. 305 */ 306 /* ARGSUSED */ 307 static int 308 ipsecah_param_get(q, mp, cp, cr) 309 queue_t *q; 310 mblk_t *mp; 311 caddr_t cp; 312 cred_t *cr; 313 { 314 ipsecahparam_t *ipsecahpa = (ipsecahparam_t *)cp; 315 uint_t value; 316 317 mutex_enter(&ipsecah_param_lock); 318 value = ipsecahpa->ipsecah_param_value; 319 mutex_exit(&ipsecah_param_lock); 320 321 (void) mi_mpprintf(mp, "%u", value); 322 return (0); 323 } 324 325 /* 326 * This routine sets an NDD variable in a ipsecahparam_t structure. 327 */ 328 /* ARGSUSED */ 329 static int 330 ipsecah_param_set(q, mp, value, cp, cr) 331 queue_t *q; 332 mblk_t *mp; 333 char *value; 334 caddr_t cp; 335 cred_t *cr; 336 { 337 ulong_t new_value; 338 ipsecahparam_t *ipsecahpa = (ipsecahparam_t *)cp; 339 340 /* 341 * Fail the request if the new value does not lie within the 342 * required bounds. 343 */ 344 if (ddi_strtoul(value, NULL, 10, &new_value) != 0 || 345 new_value < ipsecahpa->ipsecah_param_min || 346 new_value > ipsecahpa->ipsecah_param_max) { 347 return (EINVAL); 348 } 349 350 /* Set the new value */ 351 mutex_enter(&ipsecah_param_lock); 352 ipsecahpa->ipsecah_param_value = new_value; 353 mutex_exit(&ipsecah_param_lock); 354 return (0); 355 } 356 357 /* 358 * Using lifetime NDD variables, fill in an extended combination's 359 * lifetime information. 360 */ 361 void 362 ipsecah_fill_defs(sadb_x_ecomb_t *ecomb) 363 { 364 ecomb->sadb_x_ecomb_soft_bytes = ipsecah_default_soft_bytes; 365 ecomb->sadb_x_ecomb_hard_bytes = ipsecah_default_hard_bytes; 366 ecomb->sadb_x_ecomb_soft_addtime = ipsecah_default_soft_addtime; 367 ecomb->sadb_x_ecomb_hard_addtime = ipsecah_default_hard_addtime; 368 ecomb->sadb_x_ecomb_soft_usetime = ipsecah_default_soft_usetime; 369 ecomb->sadb_x_ecomb_hard_usetime = ipsecah_default_hard_usetime; 370 } 371 372 /* 373 * Initialize things for AH at module load time. 374 */ 375 boolean_t 376 ipsecah_ddi_init(void) 377 { 378 int count; 379 ipsecahparam_t *ahp = ipsecah_param_arr; 380 381 for (count = A_CNT(ipsecah_param_arr); count-- > 0; ahp++) { 382 if (ahp->ipsecah_param_name != NULL && 383 ahp->ipsecah_param_name[0]) { 384 if (!nd_load(&ipsecah_g_nd, ahp->ipsecah_param_name, 385 ipsecah_param_get, ipsecah_param_set, 386 (caddr_t)ahp)) { 387 nd_free(&ipsecah_g_nd); 388 return (B_FALSE); 389 } 390 } 391 } 392 393 if (!ah_kstat_init()) { 394 nd_free(&ipsecah_g_nd); 395 return (B_FALSE); 396 } 397 398 ah_taskq = taskq_create("ah_taskq", 1, minclsyspri, 399 IPSEC_TASKQ_MIN, IPSEC_TASKQ_MAX, 0); 400 401 ah_sadb.s_acquire_timeout = &ipsecah_acquire_timeout; 402 ah_sadb.s_acqfn = ah_send_acquire; 403 404 sadbp_init("AH", &ah_sadb, SADB_SATYPE_AH, ah_hash_size); 405 406 mutex_init(&ipsecah_param_lock, NULL, MUTEX_DEFAULT, 0); 407 408 ip_drop_register(&ah_dropper, "IPsec AH"); 409 410 return (B_TRUE); 411 } 412 413 /* 414 * Destroy things for AH at module unload time. 415 */ 416 void 417 ipsecah_ddi_destroy(void) 418 { 419 ah1dbg(("In ddi_destroy.\n")); 420 421 sadbp_destroy(&ah_sadb); 422 ip_drop_unregister(&ah_dropper); 423 taskq_destroy(ah_taskq); 424 mutex_destroy(&ipsecah_param_lock); 425 nd_free(&ipsecah_g_nd); 426 427 kstat_delete(ah_ksp); 428 } 429 430 /* 431 * AH module open routine. The module should be opened by keysock. 432 */ 433 /* ARGSUSED */ 434 static int 435 ipsecah_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 436 { 437 if (secpolicy_net_config(credp, B_FALSE) != 0) { 438 ah1dbg(("Non-privileged user trying to open ipsecah.\n")); 439 return (EPERM); 440 } 441 442 if (q->q_ptr != NULL) 443 return (0); /* Re-open of an already open instance. */ 444 445 if (sflag != MODOPEN) 446 return (EINVAL); 447 448 /* 449 * ASSUMPTIONS (because I'm MT_OCEXCL): 450 * 451 * * I'm being pushed on top of IP for all my opens (incl. #1). 452 * * Only ipsecah_open() can write into ah_sadb.s_ip_q. 453 * * Because of this, I can check lazily for ah_sadb.s_ip_q. 454 * 455 * If these assumptions are wrong, I'm in BIG trouble... 456 */ 457 458 q->q_ptr = q; /* just so I know I'm open */ 459 460 if (ah_sadb.s_ip_q == NULL) { 461 struct T_unbind_req *tur; 462 463 ah_sadb.s_ip_q = WR(q); 464 /* Allocate an unbind... */ 465 ah_ip_unbind = allocb(sizeof (struct T_unbind_req), BPRI_HI); 466 467 /* 468 * Send down T_BIND_REQ to bind IPPROTO_AH. 469 * Handle the ACK here in AH. 470 */ 471 qprocson(q); 472 if (ah_ip_unbind == NULL || 473 !sadb_t_bind_req(ah_sadb.s_ip_q, IPPROTO_AH)) { 474 if (ah_ip_unbind != NULL) { 475 freeb(ah_ip_unbind); 476 ah_ip_unbind = NULL; 477 } 478 q->q_ptr = NULL; 479 qprocsoff(q); 480 return (ENOMEM); 481 } 482 483 ah_ip_unbind->b_datap->db_type = M_PROTO; 484 tur = (struct T_unbind_req *)ah_ip_unbind->b_rptr; 485 tur->PRIM_type = T_UNBIND_REQ; 486 } else { 487 qprocson(q); 488 } 489 490 /* 491 * For now, there's not much I can do. I'll be getting a message 492 * passed down to me from keysock (in my wput), and a T_BIND_ACK 493 * up from IP (in my rput). 494 */ 495 496 return (0); 497 } 498 499 /* 500 * AH module close routine. 501 */ 502 static int 503 ipsecah_close(queue_t *q) 504 { 505 /* 506 * If ah_sadb.s_ip_q is attached to this instance, send a 507 * T_UNBIND_REQ to IP for the instance before doing 508 * a qprocsoff(). 509 */ 510 if (WR(q) == ah_sadb.s_ip_q && ah_ip_unbind != NULL) { 511 putnext(WR(q), ah_ip_unbind); 512 ah_ip_unbind = NULL; 513 } 514 515 /* 516 * Clean up q_ptr, if needed. 517 */ 518 qprocsoff(q); 519 520 /* Keysock queue check is safe, because of OCEXCL perimeter. */ 521 522 if (q == ah_pfkey_q) { 523 ah0dbg(("ipsecah_close: Ummm... keysock is closing AH.\n")); 524 ah_pfkey_q = NULL; 525 /* Detach qtimeouts. */ 526 (void) quntimeout(q, ah_event); 527 } 528 529 if (WR(q) == ah_sadb.s_ip_q) { 530 /* 531 * If the ah_sadb.s_ip_q is attached to this instance, find 532 * another. The OCEXCL outer perimeter helps us here. 533 */ 534 535 ah_sadb.s_ip_q = NULL; 536 537 /* 538 * Find a replacement queue for ah_sadb.s_ip_q. 539 */ 540 if (ah_pfkey_q != NULL && ah_pfkey_q != RD(q)) { 541 /* 542 * See if we can use the pfkey_q. 543 */ 544 ah_sadb.s_ip_q = WR(ah_pfkey_q); 545 } 546 547 if (ah_sadb.s_ip_q == NULL || 548 !sadb_t_bind_req(ah_sadb.s_ip_q, IPPROTO_AH)) { 549 ah1dbg(("ipsecah: Can't reassign ah_sadb.s_ip_q.\n")); 550 ah_sadb.s_ip_q = NULL; 551 } else { 552 ah_ip_unbind = allocb(sizeof (struct T_unbind_req), 553 BPRI_HI); 554 555 if (ah_ip_unbind != NULL) { 556 struct T_unbind_req *tur; 557 558 ah_ip_unbind->b_datap->db_type = M_PROTO; 559 tur = (struct T_unbind_req *) 560 ah_ip_unbind->b_rptr; 561 tur->PRIM_type = T_UNBIND_REQ; 562 } 563 /* If it's NULL, I can't do much here. */ 564 } 565 } 566 567 return (0); 568 } 569 570 /* 571 * AH module read put routine. 572 */ 573 /* ARGSUSED */ 574 static void 575 ipsecah_rput(queue_t *q, mblk_t *mp) 576 { 577 keysock_in_t *ksi; 578 int *addrtype; 579 ire_t *ire; 580 mblk_t *ire_mp, *last_mp; 581 582 switch (mp->b_datap->db_type) { 583 case M_CTL: 584 /* 585 * IPsec request of some variety from IP. IPSEC_{IN,OUT} 586 * are the common cases, but even ICMP error messages from IP 587 * may rise up here. 588 * 589 * Ummmm, actually, this can also be the reflected KEYSOCK_IN 590 * message, with an IRE_DB_TYPE hung off at the end. 591 */ 592 switch (((ipsec_info_t *)(mp->b_rptr))->ipsec_info_type) { 593 case KEYSOCK_IN: 594 last_mp = mp; 595 while (last_mp->b_cont != NULL && 596 last_mp->b_cont->b_datap->db_type != IRE_DB_TYPE) 597 last_mp = last_mp->b_cont; 598 599 if (last_mp->b_cont == NULL) { 600 freemsg(mp); 601 break; /* Out of switch. */ 602 } 603 604 ire_mp = last_mp->b_cont; 605 last_mp->b_cont = NULL; 606 607 ksi = (keysock_in_t *)mp->b_rptr; 608 609 if (ksi->ks_in_srctype == KS_IN_ADDR_UNKNOWN) 610 addrtype = &ksi->ks_in_srctype; 611 else if (ksi->ks_in_dsttype == KS_IN_ADDR_UNKNOWN) 612 addrtype = &ksi->ks_in_dsttype; 613 else if (ksi->ks_in_proxytype == KS_IN_ADDR_UNKNOWN) 614 addrtype = &ksi->ks_in_proxytype; 615 616 ire = (ire_t *)ire_mp->b_rptr; 617 618 *addrtype = sadb_addrset(ire); 619 620 freemsg(ire_mp); 621 if (ah_pfkey_q != NULL) { 622 /* 623 * Decrement counter to make up for 624 * auto-increment in ipsecah_wput(). 625 * I'm running all MT-hot through here, so 626 * don't worry about perimeters and lateral 627 * puts. 628 */ 629 AH_DEBUMP_STAT(keysock_in); 630 ipsecah_wput(WR(ah_pfkey_q), mp); 631 } else { 632 freemsg(mp); 633 } 634 break; 635 default: 636 freemsg(mp); 637 break; 638 } 639 break; 640 case M_PROTO: 641 case M_PCPROTO: 642 /* TPI message of some sort. */ 643 switch (*((t_scalar_t *)mp->b_rptr)) { 644 case T_BIND_ACK: 645 /* We expect this. */ 646 ah3dbg(("Thank you IP from AH for T_BIND_ACK\n")); 647 break; 648 case T_ERROR_ACK: 649 cmn_err(CE_WARN, 650 "ipsecah: AH received T_ERROR_ACK from IP."); 651 break; 652 case T_OK_ACK: 653 /* Probably from a (rarely sent) T_UNBIND_REQ. */ 654 break; 655 default: 656 ah1dbg(("Unknown M_{,PC}PROTO message.\n")); 657 } 658 freemsg(mp); 659 break; 660 default: 661 /* For now, passthru message. */ 662 ah2dbg(("AH got unknown mblk type %d.\n", 663 mp->b_datap->db_type)); 664 putnext(q, mp); 665 } 666 } 667 668 /* 669 * Construct an SADB_REGISTER message with the current algorithms. 670 */ 671 static boolean_t 672 ah_register_out(uint32_t sequence, uint32_t pid, uint_t serial) 673 { 674 mblk_t *mp; 675 boolean_t rc = B_TRUE; 676 sadb_msg_t *samsg; 677 sadb_supported_t *sasupp; 678 sadb_alg_t *saalg; 679 uint_t allocsize = sizeof (*samsg); 680 uint_t i, numalgs_snap; 681 ipsec_alginfo_t **authalgs; 682 uint_t num_aalgs; 683 684 /* Allocate the KEYSOCK_OUT. */ 685 mp = sadb_keysock_out(serial); 686 if (mp == NULL) { 687 ah0dbg(("ah_register_out: couldn't allocate mblk.\n")); 688 return (B_FALSE); 689 } 690 691 /* 692 * Allocate the PF_KEY message that follows KEYSOCK_OUT. 693 * The alg reader lock needs to be held while allocating 694 * the variable part (i.e. the algorithms) of the message. 695 */ 696 697 mutex_enter(&alg_lock); 698 699 /* 700 * Return only valid algorithms, so the number of algorithms 701 * to send up may be less than the number of algorithm entries 702 * in the table. 703 */ 704 authalgs = ipsec_alglists[IPSEC_ALG_AUTH]; 705 for (num_aalgs = 0, i = 0; i < IPSEC_MAX_ALGS; i++) 706 if (authalgs[i] != NULL && ALG_VALID(authalgs[i])) 707 num_aalgs++; 708 709 /* 710 * Fill SADB_REGISTER message's algorithm descriptors. Hold 711 * down the lock while filling it. 712 */ 713 if (num_aalgs != 0) { 714 allocsize += (num_aalgs * sizeof (*saalg)); 715 allocsize += sizeof (*sasupp); 716 } 717 mp->b_cont = allocb(allocsize, BPRI_HI); 718 if (mp->b_cont == NULL) { 719 mutex_exit(&alg_lock); 720 freemsg(mp); 721 return (B_FALSE); 722 } 723 724 mp->b_cont->b_wptr += allocsize; 725 if (num_aalgs != 0) { 726 727 saalg = (sadb_alg_t *)(mp->b_cont->b_rptr + sizeof (*samsg) + 728 sizeof (*sasupp)); 729 ASSERT(((ulong_t)saalg & 0x7) == 0); 730 731 numalgs_snap = 0; 732 for (i = 0; 733 ((i < IPSEC_MAX_ALGS) && (numalgs_snap < num_aalgs)); i++) { 734 if (authalgs[i] == NULL || !ALG_VALID(authalgs[i])) 735 continue; 736 737 saalg->sadb_alg_id = authalgs[i]->alg_id; 738 saalg->sadb_alg_ivlen = 0; 739 saalg->sadb_alg_minbits = authalgs[i]->alg_ef_minbits; 740 saalg->sadb_alg_maxbits = authalgs[i]->alg_ef_maxbits; 741 saalg->sadb_x_alg_increment = 742 authalgs[i]->alg_increment; 743 saalg->sadb_x_alg_defincr = authalgs[i]->alg_ef_default; 744 numalgs_snap++; 745 saalg++; 746 } 747 ASSERT(numalgs_snap == num_aalgs); 748 #ifdef DEBUG 749 /* 750 * Reality check to make sure I snagged all of the 751 * algorithms. 752 */ 753 for (; i < IPSEC_MAX_ALGS; i++) 754 if (authalgs[i] != NULL && ALG_VALID(authalgs[i])) 755 cmn_err(CE_PANIC, 756 "ah_register_out()! Missed #%d.\n", i); 757 #endif /* DEBUG */ 758 } 759 760 mutex_exit(&alg_lock); 761 762 /* Now fill the restof the SADB_REGISTER message. */ 763 764 samsg = (sadb_msg_t *)mp->b_cont->b_rptr; 765 samsg->sadb_msg_version = PF_KEY_V2; 766 samsg->sadb_msg_type = SADB_REGISTER; 767 samsg->sadb_msg_errno = 0; 768 samsg->sadb_msg_satype = SADB_SATYPE_AH; 769 samsg->sadb_msg_len = SADB_8TO64(allocsize); 770 samsg->sadb_msg_reserved = 0; 771 /* 772 * Assume caller has sufficient sequence/pid number info. If it's one 773 * from me over a new alg., I could give two hoots about sequence. 774 */ 775 samsg->sadb_msg_seq = sequence; 776 samsg->sadb_msg_pid = pid; 777 778 if (allocsize > sizeof (*samsg)) { 779 sasupp = (sadb_supported_t *)(samsg + 1); 780 sasupp->sadb_supported_len = 781 SADB_8TO64(allocsize - sizeof (sadb_msg_t)); 782 sasupp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH; 783 sasupp->sadb_supported_reserved = 0; 784 } 785 786 if (ah_pfkey_q != NULL) 787 putnext(ah_pfkey_q, mp); 788 else { 789 rc = B_FALSE; 790 freemsg(mp); 791 } 792 793 return (rc); 794 } 795 796 /* 797 * Invoked when the algorithm table changes. Causes SADB_REGISTER 798 * messages continaining the current list of algorithms to be 799 * sent up to the AH listeners. 800 */ 801 void 802 ipsecah_algs_changed(void) 803 { 804 /* 805 * Time to send a PF_KEY SADB_REGISTER message to AH listeners 806 * everywhere. (The function itself checks for NULL ah_pfkey_q.) 807 */ 808 (void) ah_register_out(0, 0, 0); 809 } 810 811 /* 812 * Stub function that taskq_dispatch() invokes to take the mblk (in arg) 813 * and put() it into AH and STREAMS again. 814 */ 815 static void 816 inbound_task(void *arg) 817 { 818 ah_t *ah; 819 mblk_t *mp = (mblk_t *)arg; 820 ipsec_in_t *ii = (ipsec_in_t *)mp->b_rptr; 821 int ipsec_rc; 822 823 ah2dbg(("in AH inbound_task")); 824 825 ah = ipsec_inbound_ah_sa(mp); 826 if (ah == NULL) 827 return; 828 ASSERT(ii->ipsec_in_ah_sa != NULL); 829 ipsec_rc = ii->ipsec_in_ah_sa->ipsa_input_func(mp, ah); 830 if (ipsec_rc != IPSEC_STATUS_SUCCESS) 831 return; 832 ip_fanout_proto_again(mp, NULL, NULL, NULL); 833 } 834 835 836 /* 837 * Now that weak-key passed, actually ADD the security association, and 838 * send back a reply ADD message. 839 */ 840 static int 841 ah_add_sa_finish(mblk_t *mp, sadb_msg_t *samsg, keysock_in_t *ksi) 842 { 843 isaf_t *primary, *secondary, *inbound, *outbound; 844 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA]; 845 sadb_address_t *dstext = 846 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 847 struct sockaddr_in *dst; 848 struct sockaddr_in6 *dst6; 849 boolean_t is_ipv4, clone = B_FALSE, is_inbound = B_FALSE; 850 uint32_t *dstaddr; 851 ipsa_t *larval; 852 ipsacq_t *acqrec; 853 iacqf_t *acq_bucket; 854 mblk_t *acq_msgs = NULL; 855 mblk_t *lpkt; 856 int rc; 857 sadb_t *sp; 858 int outhash; 859 860 /* 861 * Locate the appropriate table(s). 862 */ 863 864 dst = (struct sockaddr_in *)(dstext + 1); 865 dst6 = (struct sockaddr_in6 *)dst; 866 is_ipv4 = (dst->sin_family == AF_INET); 867 if (is_ipv4) { 868 sp = &ah_sadb.s_v4; 869 dstaddr = (uint32_t *)(&dst->sin_addr); 870 outhash = OUTBOUND_HASH_V4(sp, *(ipaddr_t *)dstaddr); 871 } else { 872 ASSERT(dst->sin_family == AF_INET6); 873 sp = &ah_sadb.s_v6; 874 dstaddr = (uint32_t *)(&dst6->sin6_addr); 875 outhash = OUTBOUND_HASH_V6(sp, *(in6_addr_t *)dstaddr); 876 } 877 878 inbound = INBOUND_BUCKET(sp, assoc->sadb_sa_spi); 879 outbound = &sp->sdb_of[outhash]; 880 881 switch (ksi->ks_in_dsttype) { 882 case KS_IN_ADDR_MBCAST: 883 clone = B_TRUE; /* All mcast SAs can be bidirectional */ 884 /* FALLTHRU */ 885 case KS_IN_ADDR_ME: 886 primary = inbound; 887 secondary = outbound; 888 /* 889 * If the source address is either one of mine, or unspecified 890 * (which is best summed up by saying "not 'not mine'"), 891 * then the association is potentially bi-directional, 892 * in that it can be used for inbound traffic and outbound 893 * traffic. The best example of such and SA is a multicast 894 * SA (which allows me to receive the outbound traffic). 895 */ 896 if (ksi->ks_in_srctype != KS_IN_ADDR_NOTME) 897 clone = B_TRUE; 898 is_inbound = B_TRUE; 899 break; 900 case KS_IN_ADDR_NOTME: 901 primary = outbound; 902 secondary = inbound; 903 /* 904 * If the source address literally not mine (either 905 * unspecified or not mine), then this SA may have an 906 * address that WILL be mine after some configuration. 907 * We pay the price for this by making it a bi-directional 908 * SA. 909 */ 910 if (ksi->ks_in_srctype != KS_IN_ADDR_ME) 911 clone = B_TRUE; 912 break; 913 default: 914 samsg->sadb_x_msg_diagnostic = SADB_X_DIAGNOSTIC_BAD_DST; 915 return (EINVAL); 916 } 917 918 /* 919 * Find a ACQUIRE list entry if possible. If we've added an SA that 920 * suits the needs of an ACQUIRE list entry, we can eliminate the 921 * ACQUIRE list entry and transmit the enqueued packets. Use the 922 * high-bit of the sequence number to queue it. Key off destination 923 * addr, and change acqrec's state. 924 */ 925 926 if (samsg->sadb_msg_seq & IACQF_LOWEST_SEQ) { 927 acq_bucket = &sp->sdb_acq[outhash]; 928 mutex_enter(&acq_bucket->iacqf_lock); 929 for (acqrec = acq_bucket->iacqf_ipsacq; acqrec != NULL; 930 acqrec = acqrec->ipsacq_next) { 931 mutex_enter(&acqrec->ipsacq_lock); 932 /* 933 * Q: I only check sequence. Should I check dst? 934 * A: Yes, check dest because those are the packets 935 * that are queued up. 936 */ 937 if (acqrec->ipsacq_seq == samsg->sadb_msg_seq && 938 IPSA_ARE_ADDR_EQUAL(dstaddr, 939 acqrec->ipsacq_dstaddr, acqrec->ipsacq_addrfam)) 940 break; 941 mutex_exit(&acqrec->ipsacq_lock); 942 } 943 if (acqrec != NULL) { 944 /* 945 * AHA! I found an ACQUIRE record for this SA. 946 * Grab the msg list, and free the acquire record. 947 * I already am holding the lock for this record, 948 * so all I have to do is free it. 949 */ 950 acq_msgs = acqrec->ipsacq_mp; 951 acqrec->ipsacq_mp = NULL; 952 mutex_exit(&acqrec->ipsacq_lock); 953 sadb_destroy_acquire(acqrec); 954 } 955 mutex_exit(&acq_bucket->iacqf_lock); 956 } 957 958 /* 959 * Find PF_KEY message, and see if I'm an update. If so, find entry 960 * in larval list (if there). 961 */ 962 963 larval = NULL; 964 965 if (samsg->sadb_msg_type == SADB_UPDATE) { 966 mutex_enter(&inbound->isaf_lock); 967 larval = ipsec_getassocbyspi(inbound, assoc->sadb_sa_spi, 968 ALL_ZEROES_PTR, dstaddr, dst->sin_family); 969 mutex_exit(&inbound->isaf_lock); 970 971 if ((larval == NULL) || 972 (larval->ipsa_state != IPSA_STATE_LARVAL)) { 973 ah0dbg(("Larval update, but larval disappeared.\n")); 974 return (ESRCH); 975 } /* Else sadb_common_add unlinks it for me! */ 976 } 977 978 lpkt = NULL; 979 if (larval != NULL) 980 lpkt = sadb_clear_lpkt(larval); 981 982 rc = sadb_common_add(ah_sadb.s_ip_q, ah_pfkey_q, mp, samsg, ksi, 983 primary, secondary, larval, clone, is_inbound); 984 985 /* 986 * How much more stack will I create with all of these 987 * ah_inbound_* and ah_outbound_*() calls? 988 */ 989 990 991 if (rc == 0 && lpkt != NULL) 992 rc = !taskq_dispatch(ah_taskq, inbound_task, 993 (void *) lpkt, TQ_NOSLEEP); 994 995 if (rc != 0) { 996 ip_drop_packet(lpkt, B_TRUE, NULL, NULL, 997 &ipdrops_sadb_inlarval_timeout, &ah_dropper); 998 } 999 1000 while (acq_msgs != NULL) { 1001 mblk_t *mp = acq_msgs; 1002 1003 acq_msgs = acq_msgs->b_next; 1004 mp->b_next = NULL; 1005 if (rc == 0) { 1006 ipsec_out_t *io = (ipsec_out_t *)mp->b_rptr; 1007 1008 ASSERT(ah_sadb.s_ip_q != NULL); 1009 if (ipsec_outbound_sa(mp, IPPROTO_AH)) { 1010 io->ipsec_out_ah_done = B_TRUE; 1011 if (ah_outbound(mp) == IPSEC_STATUS_SUCCESS) { 1012 ipha_t *ipha = (ipha_t *) 1013 mp->b_cont->b_rptr; 1014 if (is_ipv4) { 1015 ip_wput_ipsec_out(NULL, mp, 1016 ipha, NULL, NULL); 1017 } else { 1018 ip6_t *ip6h = (ip6_t *)ipha; 1019 ip_wput_ipsec_out_v6(NULL, 1020 mp, ip6h, NULL, NULL); 1021 } 1022 } 1023 continue; 1024 } 1025 } 1026 AH_BUMP_STAT(out_discards); 1027 ip_drop_packet(mp, B_FALSE, NULL, NULL, 1028 &ipdrops_sadb_acquire_timeout, &ah_dropper); 1029 } 1030 1031 return (rc); 1032 } 1033 1034 /* 1035 * Add new AH security association. This may become a generic AH/ESP 1036 * routine eventually. 1037 */ 1038 static int 1039 ah_add_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic) 1040 { 1041 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA]; 1042 sadb_address_t *srcext = 1043 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC]; 1044 sadb_address_t *dstext = 1045 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 1046 sadb_key_t *key = (sadb_key_t *)ksi->ks_in_extv[SADB_EXT_KEY_AUTH]; 1047 struct sockaddr_in *src, *dst; 1048 /* We don't need sockaddr_in6 for now. */ 1049 sadb_lifetime_t *soft = 1050 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_SOFT]; 1051 sadb_lifetime_t *hard = 1052 (sadb_lifetime_t *)ksi->ks_in_extv[SADB_EXT_LIFETIME_HARD]; 1053 ipsec_alginfo_t *aalg; 1054 1055 /* I need certain extensions present for an ADD message. */ 1056 if (srcext == NULL) { 1057 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SRC; 1058 return (EINVAL); 1059 } 1060 if (dstext == NULL) { 1061 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST; 1062 return (EINVAL); 1063 } 1064 if (assoc == NULL) { 1065 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA; 1066 return (EINVAL); 1067 } 1068 if (key == NULL) { 1069 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_AKEY; 1070 return (EINVAL); 1071 } 1072 1073 src = (struct sockaddr_in *)(srcext + 1); 1074 dst = (struct sockaddr_in *)(dstext + 1); 1075 1076 /* Sundry ADD-specific reality checks. */ 1077 /* XXX STATS : Logging/stats here? */ 1078 1079 if (assoc->sadb_sa_state != SADB_SASTATE_MATURE) { 1080 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SASTATE; 1081 return (EINVAL); 1082 } 1083 if (assoc->sadb_sa_encrypt != SADB_EALG_NONE) { 1084 *diagnostic = SADB_X_DIAGNOSTIC_ENCR_NOTSUPP; 1085 return (EINVAL); 1086 } 1087 if (assoc->sadb_sa_flags & ~(SADB_SAFLAGS_NOREPLAY)) { 1088 *diagnostic = SADB_X_DIAGNOSTIC_BAD_SAFLAGS; 1089 return (EINVAL); 1090 } 1091 1092 if ((*diagnostic = sadb_hardsoftchk(hard, soft)) != 0) 1093 return (EINVAL); 1094 1095 if (src->sin_family != dst->sin_family) { 1096 *diagnostic = SADB_X_DIAGNOSTIC_AF_MISMATCH; 1097 return (EINVAL); 1098 } 1099 1100 /* Stuff I don't support, for now. XXX Diagnostic? */ 1101 if (ksi->ks_in_extv[SADB_EXT_LIFETIME_CURRENT] != NULL || 1102 ksi->ks_in_extv[SADB_EXT_SENSITIVITY] != NULL) 1103 return (EOPNOTSUPP); 1104 1105 /* 1106 * XXX Policy : I'm not checking identities or sensitivity 1107 * labels at this time, but if I did, I'd do them here, before I sent 1108 * the weak key check up to the algorithm. 1109 */ 1110 1111 /* verify that there is a mapping for the specified algorithm */ 1112 mutex_enter(&alg_lock); 1113 aalg = ipsec_alglists[IPSEC_ALG_AUTH][assoc->sadb_sa_auth]; 1114 if (aalg == NULL || !ALG_VALID(aalg)) { 1115 mutex_exit(&alg_lock); 1116 ah1dbg(("Couldn't find auth alg #%d.\n", assoc->sadb_sa_auth)); 1117 *diagnostic = SADB_X_DIAGNOSTIC_BAD_AALG; 1118 return (EINVAL); 1119 } 1120 ASSERT(aalg->alg_mech_type != CRYPTO_MECHANISM_INVALID); 1121 1122 /* sanity check key sizes */ 1123 if (!ipsec_valid_key_size(key->sadb_key_bits, aalg)) { 1124 mutex_exit(&alg_lock); 1125 *diagnostic = SADB_X_DIAGNOSTIC_BAD_AKEYBITS; 1126 return (EINVAL); 1127 } 1128 1129 /* check key and fix parity if needed */ 1130 if (ipsec_check_key(aalg->alg_mech_type, key, B_TRUE, 1131 diagnostic) != 0) { 1132 mutex_exit(&alg_lock); 1133 return (EINVAL); 1134 } 1135 1136 mutex_exit(&alg_lock); 1137 1138 return (ah_add_sa_finish(mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi)); 1139 } 1140 1141 /* 1142 * Update a security association. Updates come in two varieties. The first 1143 * is an update of lifetimes on a non-larval SA. The second is an update of 1144 * a larval SA, which ends up looking a lot more like an add. 1145 */ 1146 static int 1147 ah_update_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic) 1148 { 1149 sadb_address_t *dstext = 1150 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 1151 struct sockaddr_in *sin; 1152 1153 if (dstext == NULL) { 1154 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_DST; 1155 return (EINVAL); 1156 } 1157 sin = (struct sockaddr_in *)(dstext + 1); 1158 return (sadb_update_sa(mp, ksi, 1159 (sin->sin_family == AF_INET6) ? &ah_sadb.s_v6 : &ah_sadb.s_v4, 1160 diagnostic, ah_pfkey_q, ah_add_sa)); 1161 } 1162 1163 /* 1164 * Delete a security association. This is REALLY likely to be code common to 1165 * both AH and ESP. Find the association, then unlink it. 1166 */ 1167 static int 1168 ah_del_sa(mblk_t *mp, keysock_in_t *ksi, int *diagnostic) 1169 { 1170 sadb_sa_t *assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SA]; 1171 sadb_address_t *dstext = 1172 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_DST]; 1173 sadb_address_t *srcext = 1174 (sadb_address_t *)ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC]; 1175 struct sockaddr_in *sin; 1176 1177 if (assoc == NULL) { 1178 if (dstext != NULL) 1179 sin = (struct sockaddr_in *)(dstext + 1); 1180 else if (srcext != NULL) 1181 sin = (struct sockaddr_in *)(srcext + 1); 1182 else { 1183 *diagnostic = SADB_X_DIAGNOSTIC_MISSING_SA; 1184 return (EINVAL); 1185 } 1186 return sadb_purge_sa(mp, ksi, 1187 (sin->sin_family == AF_INET6) ? &ah_sadb.s_v6 : 1188 &ah_sadb.s_v4, 1189 diagnostic, ah_pfkey_q, ah_sadb.s_ip_q); 1190 } 1191 1192 return (sadb_del_sa(mp, ksi, &ah_sadb, diagnostic, ah_pfkey_q)); 1193 } 1194 1195 /* 1196 * Convert the entire contents of all of AH's SA tables into PF_KEY SADB_DUMP 1197 * messages. 1198 */ 1199 static void 1200 ah_dump(mblk_t *mp, keysock_in_t *ksi) 1201 { 1202 int error; 1203 sadb_msg_t *samsg; 1204 1205 /* 1206 * Dump each fanout, bailing if error is non-zero. 1207 */ 1208 1209 error = sadb_dump(ah_pfkey_q, mp, ksi->ks_in_serial, &ah_sadb.s_v4); 1210 if (error != 0) 1211 goto bail; 1212 1213 error = sadb_dump(ah_pfkey_q, mp, ksi->ks_in_serial, &ah_sadb.s_v6); 1214 bail: 1215 ASSERT(mp->b_cont != NULL); 1216 samsg = (sadb_msg_t *)mp->b_cont->b_rptr; 1217 samsg->sadb_msg_errno = (uint8_t)error; 1218 sadb_pfkey_echo(ah_pfkey_q, mp, (sadb_msg_t *)mp->b_cont->b_rptr, ksi, 1219 NULL); 1220 } 1221 1222 /* 1223 * AH parsing of PF_KEY messages. Keysock did most of the really silly 1224 * error cases. What I receive is a fully-formed, syntactically legal 1225 * PF_KEY message. I then need to check semantics... 1226 * 1227 * This code may become common to AH and ESP. Stay tuned. 1228 * 1229 * I also make the assumption that db_ref's are cool. If this assumption 1230 * is wrong, this means that someone other than keysock or me has been 1231 * mucking with PF_KEY messages. 1232 */ 1233 static void 1234 ah_parse_pfkey(mblk_t *mp) 1235 { 1236 mblk_t *msg = mp->b_cont; 1237 sadb_msg_t *samsg; 1238 keysock_in_t *ksi; 1239 int error; 1240 int diagnostic = SADB_X_DIAGNOSTIC_NONE; 1241 1242 ASSERT(msg != NULL); 1243 samsg = (sadb_msg_t *)msg->b_rptr; 1244 ksi = (keysock_in_t *)mp->b_rptr; 1245 1246 /* 1247 * If applicable, convert unspecified AF_INET6 to unspecified 1248 * AF_INET. 1249 */ 1250 sadb_srcaddrfix(ksi); 1251 1252 switch (samsg->sadb_msg_type) { 1253 case SADB_ADD: 1254 error = ah_add_sa(mp, ksi, &diagnostic); 1255 if (error != 0) { 1256 sadb_pfkey_error(ah_pfkey_q, mp, error, diagnostic, 1257 ksi->ks_in_serial); 1258 } 1259 /* else ah_add_sa() took care of things. */ 1260 break; 1261 case SADB_DELETE: 1262 error = ah_del_sa(mp, ksi, &diagnostic); 1263 if (error != 0) { 1264 sadb_pfkey_error(ah_pfkey_q, mp, error, diagnostic, 1265 ksi->ks_in_serial); 1266 } 1267 /* Else ah_del_sa() took care of things. */ 1268 break; 1269 case SADB_GET: 1270 error = sadb_get_sa(mp, ksi, &ah_sadb, &diagnostic, ah_pfkey_q); 1271 if (error != 0) { 1272 sadb_pfkey_error(ah_pfkey_q, mp, error, diagnostic, 1273 ksi->ks_in_serial); 1274 } 1275 /* Else sadb_get_sa() took care of things. */ 1276 break; 1277 case SADB_FLUSH: 1278 sadbp_flush(&ah_sadb); 1279 sadb_pfkey_echo(ah_pfkey_q, mp, samsg, ksi, NULL); 1280 break; 1281 case SADB_REGISTER: 1282 /* 1283 * Hmmm, let's do it! Check for extensions (there should 1284 * be none), extract the fields, call ah_register_out(), 1285 * then either free or report an error. 1286 * 1287 * Keysock takes care of the PF_KEY bookkeeping for this. 1288 */ 1289 if (ah_register_out(samsg->sadb_msg_seq, samsg->sadb_msg_pid, 1290 ksi->ks_in_serial)) { 1291 freemsg(mp); 1292 } else { 1293 /* 1294 * Only way this path hits is if there is a memory 1295 * failure. It will not return B_FALSE because of 1296 * lack of ah_pfkey_q if I am in wput(). 1297 */ 1298 sadb_pfkey_error(ah_pfkey_q, mp, ENOMEM, diagnostic, 1299 ksi->ks_in_serial); 1300 } 1301 break; 1302 case SADB_UPDATE: 1303 /* 1304 * Find a larval, if not there, find a full one and get 1305 * strict. 1306 */ 1307 error = ah_update_sa(mp, ksi, &diagnostic); 1308 if (error != 0) { 1309 sadb_pfkey_error(ah_pfkey_q, mp, error, diagnostic, 1310 ksi->ks_in_serial); 1311 } 1312 /* else ah_update_sa() took care of things. */ 1313 break; 1314 case SADB_GETSPI: 1315 /* 1316 * Reserve a new larval entry. 1317 */ 1318 ah_getspi(mp, ksi); 1319 break; 1320 case SADB_ACQUIRE: 1321 /* 1322 * Find larval and/or ACQUIRE record and kill it (them), I'm 1323 * most likely an error. Inbound ACQUIRE messages should only 1324 * have the base header. 1325 */ 1326 sadb_in_acquire(samsg, &ah_sadb, ah_pfkey_q); 1327 freemsg(mp); 1328 break; 1329 case SADB_DUMP: 1330 /* 1331 * Dump all entries. 1332 */ 1333 ah_dump(mp, ksi); 1334 /* ah_dump will take care of the return message, etc. */ 1335 break; 1336 case SADB_EXPIRE: 1337 /* Should never reach me. */ 1338 sadb_pfkey_error(ah_pfkey_q, mp, EOPNOTSUPP, diagnostic, 1339 ksi->ks_in_serial); 1340 break; 1341 default: 1342 sadb_pfkey_error(ah_pfkey_q, mp, EINVAL, 1343 SADB_X_DIAGNOSTIC_UNKNOWN_MSG, ksi->ks_in_serial); 1344 break; 1345 } 1346 } 1347 1348 /* 1349 * Handle case where PF_KEY says it can't find a keysock for one of my 1350 * ACQUIRE messages. 1351 */ 1352 static void 1353 ah_keysock_no_socket(mblk_t *mp) 1354 { 1355 sadb_msg_t *samsg; 1356 keysock_out_err_t *kse = (keysock_out_err_t *)mp->b_rptr; 1357 1358 if (mp->b_cont == NULL) { 1359 freemsg(mp); 1360 return; 1361 } 1362 samsg = (sadb_msg_t *)mp->b_cont->b_rptr; 1363 1364 /* 1365 * If keysock can't find any registered, delete the acquire record 1366 * immediately, and handle errors. 1367 */ 1368 if (samsg->sadb_msg_type == SADB_ACQUIRE) { 1369 samsg->sadb_msg_errno = kse->ks_err_errno; 1370 samsg->sadb_msg_len = SADB_8TO64(sizeof (*samsg)); 1371 /* 1372 * Use the write-side of the ah_pfkey_q, in case there is 1373 * no ah_sadb.s_ip_q. 1374 */ 1375 sadb_in_acquire(samsg, &ah_sadb, WR(ah_pfkey_q)); 1376 } 1377 1378 freemsg(mp); 1379 } 1380 1381 /* 1382 * First-cut reality check for an inbound PF_KEY message. 1383 */ 1384 static boolean_t 1385 ah_pfkey_reality_failures(mblk_t *mp, keysock_in_t *ksi) 1386 { 1387 int diagnostic; 1388 1389 if (mp->b_cont == NULL) { 1390 freemsg(mp); 1391 return (B_TRUE); 1392 } 1393 1394 if (ksi->ks_in_extv[SADB_EXT_KEY_ENCRYPT] != NULL) { 1395 diagnostic = SADB_X_DIAGNOSTIC_EKEY_PRESENT; 1396 goto badmsg; 1397 } 1398 if (ksi->ks_in_extv[SADB_EXT_PROPOSAL] != NULL) { 1399 diagnostic = SADB_X_DIAGNOSTIC_PROP_PRESENT; 1400 goto badmsg; 1401 } 1402 if (ksi->ks_in_extv[SADB_EXT_SUPPORTED_AUTH] != NULL || 1403 ksi->ks_in_extv[SADB_EXT_SUPPORTED_ENCRYPT] != NULL) { 1404 diagnostic = SADB_X_DIAGNOSTIC_SUPP_PRESENT; 1405 goto badmsg; 1406 } 1407 if (ksi->ks_in_srctype == KS_IN_ADDR_MBCAST) { 1408 diagnostic = SADB_X_DIAGNOSTIC_BAD_SRC; 1409 goto badmsg; 1410 } 1411 if (ksi->ks_in_dsttype == KS_IN_ADDR_UNSPEC) { 1412 diagnostic = SADB_X_DIAGNOSTIC_BAD_DST; 1413 goto badmsg; 1414 } 1415 1416 return (B_FALSE); /* False ==> no failures */ 1417 1418 badmsg: 1419 sadb_pfkey_error(ah_pfkey_q, mp, EINVAL, diagnostic, ksi->ks_in_serial); 1420 return (B_TRUE); /* True ==> failures */ 1421 } 1422 1423 /* 1424 * AH module write put routine. 1425 */ 1426 static void 1427 ipsecah_wput(queue_t *q, mblk_t *mp) 1428 { 1429 ipsec_info_t *ii; 1430 keysock_in_t *ksi; 1431 int rc; 1432 struct iocblk *iocp; 1433 1434 ah3dbg(("In ah_wput().\n")); 1435 1436 /* NOTE: Each case must take care of freeing or passing mp. */ 1437 switch (mp->b_datap->db_type) { 1438 case M_CTL: 1439 if ((mp->b_wptr - mp->b_rptr) < sizeof (ipsec_info_t)) { 1440 /* Not big enough message. */ 1441 freemsg(mp); 1442 break; 1443 } 1444 ii = (ipsec_info_t *)mp->b_rptr; 1445 1446 switch (ii->ipsec_info_type) { 1447 case KEYSOCK_OUT_ERR: 1448 ah1dbg(("Got KEYSOCK_OUT_ERR message.\n")); 1449 ah_keysock_no_socket(mp); 1450 break; 1451 case KEYSOCK_IN: 1452 AH_BUMP_STAT(keysock_in); 1453 ah3dbg(("Got KEYSOCK_IN message.\n")); 1454 ksi = (keysock_in_t *)ii; 1455 /* 1456 * Some common reality checks. 1457 */ 1458 1459 if (ah_pfkey_reality_failures(mp, ksi)) 1460 return; 1461 1462 /* 1463 * Use 'q' instead of ah_sadb.s_ip_q, since 1464 * it's the write side already, and it'll go 1465 * down to IP. Use ah_pfkey_q because we 1466 * wouldn't get here if that weren't set, and 1467 * the RD(q) has been done already. 1468 */ 1469 if (ksi->ks_in_srctype == KS_IN_ADDR_UNKNOWN) { 1470 rc = sadb_addrcheck(q, ah_pfkey_q, mp, 1471 ksi->ks_in_extv[SADB_EXT_ADDRESS_SRC], 1472 ksi->ks_in_serial); 1473 if (rc == KS_IN_ADDR_UNKNOWN) 1474 return; 1475 else 1476 ksi->ks_in_srctype = rc; 1477 } 1478 if (ksi->ks_in_dsttype == KS_IN_ADDR_UNKNOWN) { 1479 rc = sadb_addrcheck(q, ah_pfkey_q, mp, 1480 ksi->ks_in_extv[SADB_EXT_ADDRESS_DST], 1481 ksi->ks_in_serial); 1482 if (rc == KS_IN_ADDR_UNKNOWN) 1483 return; 1484 else 1485 ksi->ks_in_dsttype = rc; 1486 } 1487 /* 1488 * XXX Proxy may be a different address family. 1489 */ 1490 if (ksi->ks_in_proxytype == KS_IN_ADDR_UNKNOWN) { 1491 rc = sadb_addrcheck(q, ah_pfkey_q, mp, 1492 ksi->ks_in_extv[SADB_EXT_ADDRESS_PROXY], 1493 ksi->ks_in_serial); 1494 if (rc == KS_IN_ADDR_UNKNOWN) 1495 return; 1496 else 1497 ksi->ks_in_proxytype = rc; 1498 } 1499 ah_parse_pfkey(mp); 1500 break; 1501 case KEYSOCK_HELLO: 1502 sadb_keysock_hello(&ah_pfkey_q, q, mp, 1503 ah_ager, &ah_event, SADB_SATYPE_AH); 1504 break; 1505 default: 1506 ah1dbg(("Got M_CTL from above of 0x%x.\n", 1507 ii->ipsec_info_type)); 1508 freemsg(mp); 1509 break; 1510 } 1511 break; 1512 case M_IOCTL: 1513 iocp = (struct iocblk *)mp->b_rptr; 1514 switch (iocp->ioc_cmd) { 1515 case ND_SET: 1516 case ND_GET: 1517 if (nd_getset(q, ipsecah_g_nd, mp)) { 1518 qreply(q, mp); 1519 return; 1520 } else { 1521 iocp->ioc_error = ENOENT; 1522 } 1523 /* FALLTHRU */ 1524 default: 1525 /* We really don't support any other ioctls, do we? */ 1526 1527 /* Return EINVAL */ 1528 if (iocp->ioc_error != ENOENT) 1529 iocp->ioc_error = EINVAL; 1530 iocp->ioc_count = 0; 1531 mp->b_datap->db_type = M_IOCACK; 1532 qreply(q, mp); 1533 return; 1534 } 1535 default: 1536 ah3dbg(("Got default message, type %d, passing to IP.\n", 1537 mp->b_datap->db_type)); 1538 putnext(q, mp); 1539 } 1540 } 1541 1542 /* 1543 * Updating use times can be tricky business if the ipsa_haspeer flag is 1544 * set. This function is called once in an SA's lifetime. 1545 * 1546 * Caller has to REFRELE "assoc" which is passed in. This function has 1547 * to REFRELE any peer SA that is obtained. 1548 */ 1549 static void 1550 ah_set_usetime(ipsa_t *assoc, boolean_t inbound) 1551 { 1552 ipsa_t *inassoc, *outassoc; 1553 isaf_t *bucket; 1554 sadb_t *sp; 1555 int outhash; 1556 boolean_t isv6; 1557 1558 /* No peer? No problem! */ 1559 if (!assoc->ipsa_haspeer) { 1560 sadb_set_usetime(assoc); 1561 return; 1562 } 1563 1564 /* 1565 * Otherwise, we want to grab both the original assoc and its peer. 1566 * There might be a race for this, but if it's a real race, the times 1567 * will be out-of-synch by at most a second, and since our time 1568 * granularity is a second, this won't be a problem. 1569 * 1570 * If we need tight synchronization on the peer SA, then we need to 1571 * reconsider. 1572 */ 1573 1574 /* Use address family to select IPv6/IPv4 */ 1575 isv6 = (assoc->ipsa_addrfam == AF_INET6); 1576 if (isv6) { 1577 sp = &ah_sadb.s_v6; 1578 } else { 1579 sp = &ah_sadb.s_v4; 1580 ASSERT(assoc->ipsa_addrfam == AF_INET); 1581 } 1582 if (inbound) { 1583 inassoc = assoc; 1584 if (isv6) 1585 outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *) 1586 &inassoc->ipsa_dstaddr)); 1587 else 1588 outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *) 1589 &inassoc->ipsa_dstaddr)); 1590 bucket = &sp->sdb_of[outhash]; 1591 1592 mutex_enter(&bucket->isaf_lock); 1593 outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi, 1594 inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr, 1595 inassoc->ipsa_addrfam); 1596 mutex_exit(&bucket->isaf_lock); 1597 if (outassoc == NULL) { 1598 /* Q: Do we wish to set haspeer == B_FALSE? */ 1599 ah0dbg(("ah_set_usetime: " 1600 "can't find peer for inbound.\n")); 1601 sadb_set_usetime(inassoc); 1602 return; 1603 } 1604 } else { 1605 outassoc = assoc; 1606 bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi); 1607 mutex_enter(&bucket->isaf_lock); 1608 inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi, 1609 outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr, 1610 outassoc->ipsa_addrfam); 1611 mutex_exit(&bucket->isaf_lock); 1612 if (inassoc == NULL) { 1613 /* Q: Do we wish to set haspeer == B_FALSE? */ 1614 ah0dbg(("ah_set_usetime: " 1615 "can't find peer for outbound.\n")); 1616 sadb_set_usetime(outassoc); 1617 return; 1618 } 1619 } 1620 1621 /* Update usetime on both. */ 1622 sadb_set_usetime(inassoc); 1623 sadb_set_usetime(outassoc); 1624 1625 /* 1626 * REFRELE any peer SA. 1627 * 1628 * Because of the multi-line macro nature of IPSA_REFRELE, keep 1629 * them in { }. 1630 */ 1631 if (inbound) { 1632 IPSA_REFRELE(outassoc); 1633 } else { 1634 IPSA_REFRELE(inassoc); 1635 } 1636 } 1637 1638 /* 1639 * Add a number of bytes to what the SA has protected so far. Return 1640 * B_TRUE if the SA can still protect that many bytes. 1641 * 1642 * Caller must REFRELE the passed-in assoc. This function must REFRELE 1643 * any obtained peer SA. 1644 */ 1645 static boolean_t 1646 ah_age_bytes(ipsa_t *assoc, uint64_t bytes, boolean_t inbound) 1647 { 1648 ipsa_t *inassoc, *outassoc; 1649 isaf_t *bucket; 1650 boolean_t inrc, outrc, isv6; 1651 sadb_t *sp; 1652 int outhash; 1653 1654 /* No peer? No problem! */ 1655 if (!assoc->ipsa_haspeer) { 1656 return (sadb_age_bytes(ah_pfkey_q, assoc, bytes, 1657 B_TRUE)); 1658 } 1659 1660 /* 1661 * Otherwise, we want to grab both the original assoc and its peer. 1662 * There might be a race for this, but if it's a real race, two 1663 * expire messages may occur. We limit this by only sending the 1664 * expire message on one of the peers, we'll pick the inbound 1665 * arbitrarily. 1666 * 1667 * If we need tight synchronization on the peer SA, then we need to 1668 * reconsider. 1669 */ 1670 1671 /* Pick v4/v6 bucket based on addrfam. */ 1672 isv6 = (assoc->ipsa_addrfam == AF_INET6); 1673 if (isv6) { 1674 sp = &ah_sadb.s_v6; 1675 } else { 1676 sp = &ah_sadb.s_v4; 1677 ASSERT(assoc->ipsa_addrfam == AF_INET); 1678 } 1679 if (inbound) { 1680 inassoc = assoc; 1681 if (isv6) 1682 outhash = OUTBOUND_HASH_V6(sp, *((in6_addr_t *) 1683 &inassoc->ipsa_dstaddr)); 1684 else 1685 outhash = OUTBOUND_HASH_V4(sp, *((ipaddr_t *) 1686 &inassoc->ipsa_dstaddr)); 1687 bucket = &sp->sdb_of[outhash]; 1688 mutex_enter(&bucket->isaf_lock); 1689 outassoc = ipsec_getassocbyspi(bucket, inassoc->ipsa_spi, 1690 inassoc->ipsa_srcaddr, inassoc->ipsa_dstaddr, 1691 inassoc->ipsa_addrfam); 1692 mutex_exit(&bucket->isaf_lock); 1693 if (outassoc == NULL) { 1694 /* Q: Do we wish to set haspeer == B_FALSE? */ 1695 ah0dbg(("ah_age_bytes: " 1696 "can't find peer for inbound.\n")); 1697 return (sadb_age_bytes(ah_pfkey_q, inassoc, 1698 bytes, B_TRUE)); 1699 } 1700 } else { 1701 outassoc = assoc; 1702 bucket = INBOUND_BUCKET(sp, outassoc->ipsa_spi); 1703 mutex_enter(&bucket->isaf_lock); 1704 inassoc = ipsec_getassocbyspi(bucket, outassoc->ipsa_spi, 1705 outassoc->ipsa_srcaddr, outassoc->ipsa_dstaddr, 1706 outassoc->ipsa_addrfam); 1707 mutex_exit(&bucket->isaf_lock); 1708 if (inassoc == NULL) { 1709 /* Q: Do we wish to set haspeer == B_FALSE? */ 1710 ah0dbg(("ah_age_bytes: " 1711 "can't find peer for outbound.\n")); 1712 return (sadb_age_bytes(ah_pfkey_q, outassoc, 1713 bytes, B_TRUE)); 1714 } 1715 } 1716 1717 inrc = sadb_age_bytes(ah_pfkey_q, inassoc, bytes, B_TRUE); 1718 outrc = sadb_age_bytes(ah_pfkey_q, outassoc, bytes, B_FALSE); 1719 1720 /* 1721 * REFRELE any peer SA. 1722 * 1723 * Because of the multi-line macro nature of IPSA_REFRELE, keep 1724 * them in { }. 1725 */ 1726 if (inbound) { 1727 IPSA_REFRELE(outassoc); 1728 } else { 1729 IPSA_REFRELE(inassoc); 1730 } 1731 1732 return (inrc && outrc); 1733 } 1734 1735 /* 1736 * Perform the really difficult work of inserting the proposed situation. 1737 * Called while holding the algorithm lock. 1738 */ 1739 static void 1740 ah_insert_prop(sadb_prop_t *prop, ipsacq_t *acqrec, uint_t combs) 1741 { 1742 sadb_comb_t *comb = (sadb_comb_t *)(prop + 1); 1743 ipsec_out_t *io; 1744 ipsec_action_t *ap; 1745 ipsec_prot_t *prot; 1746 io = (ipsec_out_t *)acqrec->ipsacq_mp->b_rptr; 1747 1748 ASSERT(MUTEX_HELD(&alg_lock)); 1749 ASSERT(io->ipsec_out_type == IPSEC_OUT); 1750 1751 prop->sadb_prop_exttype = SADB_EXT_PROPOSAL; 1752 prop->sadb_prop_len = SADB_8TO64(sizeof (sadb_prop_t)); 1753 *(uint32_t *)(&prop->sadb_prop_replay) = 0; /* Quick zero-out! */ 1754 1755 prop->sadb_prop_replay = ipsecah_replay_size; 1756 1757 /* 1758 * Based upon algorithm properties, and what-not, prioritize a 1759 * proposal, based on the ordering of the ah algorithms in the 1760 * alternatives presented in the policy rule passed down 1761 * through the ipsec_out_t and attached to the acquire record. 1762 */ 1763 1764 for (ap = acqrec->ipsacq_act; ap != NULL; 1765 ap = ap->ipa_next) { 1766 ipsec_alginfo_t *aalg; 1767 1768 if ((ap->ipa_act.ipa_type != IPSEC_POLICY_APPLY) || 1769 (!ap->ipa_act.ipa_apply.ipp_use_ah)) 1770 continue; 1771 1772 prot = &ap->ipa_act.ipa_apply; 1773 1774 ASSERT(prot->ipp_auth_alg > 0); 1775 1776 aalg = ipsec_alglists[IPSEC_ALG_AUTH][prot->ipp_auth_alg]; 1777 if (aalg == NULL || !ALG_VALID(aalg)) 1778 continue; 1779 1780 /* XXX check aalg for duplicates??.. */ 1781 1782 comb->sadb_comb_flags = 0; 1783 comb->sadb_comb_reserved = 0; 1784 comb->sadb_comb_encrypt = 0; 1785 comb->sadb_comb_encrypt_minbits = 0; 1786 comb->sadb_comb_encrypt_maxbits = 0; 1787 1788 comb->sadb_comb_auth = aalg->alg_id; 1789 comb->sadb_comb_auth_minbits = prot->ipp_ah_minbits; 1790 comb->sadb_comb_auth_maxbits = prot->ipp_ah_maxbits; 1791 1792 /* 1793 * The following may be based on algorithm 1794 * properties, but in the meantime, we just pick 1795 * some good, sensible numbers. Key mgmt. can 1796 * (and perhaps should) be the place to finalize 1797 * such decisions. 1798 */ 1799 1800 /* 1801 * No limits on allocations, since we really don't 1802 * support that concept currently. 1803 */ 1804 comb->sadb_comb_soft_allocations = 0; 1805 comb->sadb_comb_hard_allocations = 0; 1806 1807 /* 1808 * These may want to come from policy rule.. 1809 */ 1810 comb->sadb_comb_soft_bytes = ipsecah_default_soft_bytes; 1811 comb->sadb_comb_hard_bytes = ipsecah_default_hard_bytes; 1812 comb->sadb_comb_soft_addtime = ipsecah_default_soft_addtime; 1813 comb->sadb_comb_hard_addtime = ipsecah_default_hard_addtime; 1814 comb->sadb_comb_soft_usetime = ipsecah_default_soft_usetime; 1815 comb->sadb_comb_hard_usetime = ipsecah_default_hard_usetime; 1816 1817 prop->sadb_prop_len += SADB_8TO64(sizeof (*comb)); 1818 if (--combs == 0) 1819 return; /* out of space.. */ 1820 comb++; 1821 } 1822 } 1823 1824 /* 1825 * Prepare and actually send the SADB_ACQUIRE message to PF_KEY. 1826 */ 1827 static void 1828 ah_send_acquire(ipsacq_t *acqrec, mblk_t *extended) 1829 { 1830 mblk_t *pfkeymp, *msgmp; 1831 uint_t allocsize, combs; 1832 sadb_msg_t *samsg; 1833 sadb_prop_t *prop; 1834 uint8_t *cur, *end; 1835 1836 AH_BUMP_STAT(acquire_requests); 1837 1838 ASSERT(MUTEX_HELD(&acqrec->ipsacq_lock)); 1839 1840 pfkeymp = sadb_keysock_out(0); 1841 if (pfkeymp == NULL) { 1842 ah1dbg(("ah_send_acquire: 1st allocb() failed.\n")); 1843 /* Just bail. */ 1844 goto done; 1845 } 1846 1847 /* 1848 * First, allocate a basic ACQUIRE message. Beyond that, 1849 * you need to extract certificate info from 1850 */ 1851 allocsize = sizeof (sadb_msg_t) + sizeof (sadb_address_t) + 1852 sizeof (sadb_address_t) + sizeof (sadb_prop_t); 1853 1854 switch (acqrec->ipsacq_addrfam) { 1855 case AF_INET: 1856 allocsize += 2 * sizeof (struct sockaddr_in); 1857 break; 1858 case AF_INET6: 1859 allocsize += 2 * sizeof (struct sockaddr_in6); 1860 break; 1861 } 1862 1863 mutex_enter(&alg_lock); 1864 1865 combs = ipsec_nalgs[IPSEC_ALG_AUTH]; 1866 1867 allocsize += combs * sizeof (sadb_comb_t); 1868 1869 /* 1870 * XXX If there are: 1871 * certificate IDs 1872 * proxy address 1873 * <Others> 1874 * add additional allocation size. 1875 */ 1876 1877 msgmp = allocb(allocsize, BPRI_HI); 1878 if (msgmp == NULL) { 1879 ah0dbg(("ah_send_acquire: 2nd allocb() failed.\n")); 1880 /* Just bail. */ 1881 freemsg(pfkeymp); 1882 pfkeymp = NULL; 1883 goto done; 1884 } 1885 1886 cur = msgmp->b_rptr; 1887 end = cur + allocsize; 1888 samsg = (sadb_msg_t *)cur; 1889 pfkeymp->b_cont = msgmp; 1890 1891 /* Set up ACQUIRE. */ 1892 cur = sadb_setup_acquire(cur, end, acqrec); 1893 if (cur == NULL) { 1894 ah0dbg(("sadb_setup_acquire failed.\n")); 1895 /* Just bail. */ 1896 freemsg(pfkeymp); 1897 pfkeymp = NULL; 1898 goto done; 1899 } 1900 samsg->sadb_msg_satype = SADB_SATYPE_AH; 1901 1902 /* XXX Insert proxy address information here. */ 1903 1904 /* XXX Insert identity information here. */ 1905 1906 /* XXXMLS Insert sensitivity information here. */ 1907 1908 /* Insert proposal here. */ 1909 1910 prop = (sadb_prop_t *)(((uint64_t *)samsg) + samsg->sadb_msg_len); 1911 ah_insert_prop(prop, acqrec, combs); 1912 samsg->sadb_msg_len += prop->sadb_prop_len; 1913 msgmp->b_wptr += SADB_64TO8(samsg->sadb_msg_len); 1914 1915 done: 1916 mutex_exit(&alg_lock); 1917 1918 /* 1919 * Must mutex_exit() before sending PF_KEY message up, in 1920 * order to avoid recursive mutex_enter() if there are no registered 1921 * listeners. 1922 * 1923 * Once I've sent the message, I'm cool anyway. 1924 */ 1925 mutex_exit(&acqrec->ipsacq_lock); 1926 if (ah_pfkey_q != NULL && pfkeymp != NULL) { 1927 if (extended != NULL) { 1928 putnext(ah_pfkey_q, extended); 1929 } 1930 putnext(ah_pfkey_q, pfkeymp); 1931 return; 1932 } 1933 /* NOTE: freemsg() works for extended == NULL. */ 1934 freemsg(extended); 1935 freemsg(pfkeymp); 1936 } 1937 1938 /* 1939 * Handle the SADB_GETSPI message. Create a larval SA. 1940 */ 1941 static void 1942 ah_getspi(mblk_t *mp, keysock_in_t *ksi) 1943 { 1944 ipsa_t *newbie, *target; 1945 isaf_t *outbound, *inbound; 1946 int rc, diagnostic; 1947 sadb_sa_t *assoc; 1948 keysock_out_t *kso; 1949 uint32_t newspi; 1950 1951 /* 1952 * Randomly generate a proposed SPI value. 1953 */ 1954 (void) random_get_pseudo_bytes((uint8_t *)&newspi, sizeof (uint32_t)); 1955 newbie = sadb_getspi(ksi, newspi, &diagnostic); 1956 1957 if (newbie == NULL) { 1958 sadb_pfkey_error(ah_pfkey_q, mp, ENOMEM, diagnostic, 1959 ksi->ks_in_serial); 1960 return; 1961 } else if (newbie == (ipsa_t *)-1) { 1962 sadb_pfkey_error(ah_pfkey_q, mp, EINVAL, diagnostic, 1963 ksi->ks_in_serial); 1964 return; 1965 } 1966 1967 /* 1968 * XXX - We may randomly collide. We really should recover from this. 1969 * Unfortunately, that could require spending way-too-much-time 1970 * in here. For now, let the user retry. 1971 */ 1972 1973 if (newbie->ipsa_addrfam == AF_INET6) { 1974 outbound = OUTBOUND_BUCKET_V6(&ah_sadb.s_v6, 1975 *(uint32_t *)(newbie->ipsa_dstaddr)); 1976 inbound = INBOUND_BUCKET(&ah_sadb.s_v6, newbie->ipsa_spi); 1977 } else { 1978 outbound = OUTBOUND_BUCKET_V4(&ah_sadb.s_v4, 1979 *(uint32_t *)(newbie->ipsa_dstaddr)); 1980 inbound = INBOUND_BUCKET(&ah_sadb.s_v4, newbie->ipsa_spi); 1981 } 1982 1983 mutex_enter(&outbound->isaf_lock); 1984 mutex_enter(&inbound->isaf_lock); 1985 1986 /* 1987 * Check for collisions (i.e. did sadb_getspi() return with something 1988 * that already exists?). 1989 * 1990 * Try outbound first. Even though SADB_GETSPI is traditionally 1991 * for inbound SAs, you never know what a user might do. 1992 */ 1993 target = ipsec_getassocbyspi(outbound, newbie->ipsa_spi, 1994 newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, newbie->ipsa_addrfam); 1995 if (target == NULL) { 1996 target = ipsec_getassocbyspi(inbound, newbie->ipsa_spi, 1997 newbie->ipsa_srcaddr, newbie->ipsa_dstaddr, 1998 newbie->ipsa_addrfam); 1999 } 2000 2001 /* 2002 * I don't have collisions elsewhere! 2003 * (Nor will I because I'm still holding inbound/outbound locks.) 2004 */ 2005 2006 if (target != NULL) { 2007 rc = EEXIST; 2008 IPSA_REFRELE(target); 2009 } else { 2010 /* 2011 * sadb_insertassoc() also checks for collisions, so 2012 * if there's a colliding larval entry, rc will be set 2013 * to EEXIST. 2014 */ 2015 rc = sadb_insertassoc(newbie, inbound); 2016 (void) drv_getparm(TIME, &newbie->ipsa_hardexpiretime); 2017 newbie->ipsa_hardexpiretime += ipsecah_larval_timeout; 2018 } 2019 2020 /* 2021 * Can exit outbound mutex. Hold inbound until we're done with 2022 * newbie. 2023 */ 2024 mutex_exit(&outbound->isaf_lock); 2025 2026 if (rc != 0) { 2027 mutex_exit(&inbound->isaf_lock); 2028 IPSA_REFRELE(newbie); 2029 sadb_pfkey_error(ah_pfkey_q, mp, rc, SADB_X_DIAGNOSTIC_NONE, 2030 ksi->ks_in_serial); 2031 return; 2032 } 2033 2034 /* Can write here because I'm still holding the bucket lock. */ 2035 newbie->ipsa_type = SADB_SATYPE_AH; 2036 2037 /* 2038 * Construct successful return message. We have one thing going 2039 * for us in PF_KEY v2. That's the fact that 2040 * sizeof (sadb_spirange_t) == sizeof (sadb_sa_t) 2041 */ 2042 assoc = (sadb_sa_t *)ksi->ks_in_extv[SADB_EXT_SPIRANGE]; 2043 assoc->sadb_sa_exttype = SADB_EXT_SA; 2044 assoc->sadb_sa_spi = newbie->ipsa_spi; 2045 *((uint64_t *)(&assoc->sadb_sa_replay)) = 0; 2046 mutex_exit(&inbound->isaf_lock); 2047 2048 /* Convert KEYSOCK_IN to KEYSOCK_OUT. */ 2049 kso = (keysock_out_t *)ksi; 2050 kso->ks_out_len = sizeof (*kso); 2051 kso->ks_out_serial = ksi->ks_in_serial; 2052 kso->ks_out_type = KEYSOCK_OUT; 2053 2054 /* 2055 * Can safely putnext() to ah_pfkey_q, because this is a turnaround 2056 * from the ah_pfkey_q. 2057 */ 2058 putnext(ah_pfkey_q, mp); 2059 } 2060 2061 /* 2062 * IPv6 sends up the ICMP errors for validation and the removal of the AH 2063 * header. 2064 */ 2065 static ipsec_status_t 2066 ah_icmp_error_v6(mblk_t *ipsec_mp) 2067 { 2068 mblk_t *mp; 2069 ip6_t *ip6h, *oip6h; 2070 uint16_t hdr_length, ah_length; 2071 uint8_t *nexthdrp; 2072 ah_t *ah; 2073 icmp6_t *icmp6; 2074 isaf_t *isaf; 2075 ipsa_t *assoc; 2076 uint8_t *post_ah_ptr; 2077 2078 mp = ipsec_mp->b_cont; 2079 ASSERT(mp->b_datap->db_type == M_CTL); 2080 2081 /* 2082 * Change the type to M_DATA till we finish pullups. 2083 */ 2084 mp->b_datap->db_type = M_DATA; 2085 2086 /* 2087 * Eat the cost of a pullupmsg() for now. It makes the rest of this 2088 * code far less convoluted. 2089 */ 2090 if (!pullupmsg(mp, -1) || 2091 !ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, &hdr_length, 2092 &nexthdrp) || 2093 mp->b_rptr + hdr_length + sizeof (icmp6_t) + sizeof (ip6_t) + 2094 sizeof (ah_t) > mp->b_wptr) { 2095 IP_AH_BUMP_STAT(in_discards); 2096 ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, &ipdrops_ah_nomem, 2097 &ah_dropper); 2098 return (IPSEC_STATUS_FAILED); 2099 } 2100 2101 oip6h = (ip6_t *)mp->b_rptr; 2102 icmp6 = (icmp6_t *)((uint8_t *)oip6h + hdr_length); 2103 ip6h = (ip6_t *)(icmp6 + 1); 2104 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &hdr_length, &nexthdrp)) { 2105 IP_AH_BUMP_STAT(in_discards); 2106 ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, 2107 &ipdrops_ah_bad_v6_hdrs, &ah_dropper); 2108 return (IPSEC_STATUS_FAILED); 2109 } 2110 ah = (ah_t *)((uint8_t *)ip6h + hdr_length); 2111 2112 isaf = OUTBOUND_BUCKET_V6(&ah_sadb.s_v6, ip6h->ip6_dst); 2113 mutex_enter(&isaf->isaf_lock); 2114 assoc = ipsec_getassocbyspi(isaf, ah->ah_spi, 2115 (uint32_t *)&ip6h->ip6_src, (uint32_t *)&ip6h->ip6_dst, AF_INET6); 2116 mutex_exit(&isaf->isaf_lock); 2117 2118 if (assoc == NULL) { 2119 IP_AH_BUMP_STAT(lookup_failure); 2120 IP_AH_BUMP_STAT(in_discards); 2121 if (ipsecah_log_unknown_spi) { 2122 ipsec_assocfailure(info.mi_idnum, 0, 0, 2123 SL_CONSOLE | SL_WARN | SL_ERROR, 2124 "Bad ICMP message - No association for the " 2125 "attached AH header whose spi is 0x%x, " 2126 "sender is 0x%x\n", 2127 ah->ah_spi, &oip6h->ip6_src, AF_INET6); 2128 } 2129 ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, &ipdrops_ah_no_sa, 2130 &ah_dropper); 2131 return (IPSEC_STATUS_FAILED); 2132 } 2133 2134 IPSA_REFRELE(assoc); 2135 2136 /* 2137 * There seems to be a valid association. If there is enough of AH 2138 * header remove it, otherwise bail. One could check whether it has 2139 * complete AH header plus 8 bytes but it does not make sense if an 2140 * icmp error is returned for ICMP messages e.g ICMP time exceeded, 2141 * that are being sent up. Let the caller figure out. 2142 * 2143 * NOTE: ah_length is the number of 32 bit words minus 2. 2144 */ 2145 ah_length = (ah->ah_length << 2) + 8; 2146 post_ah_ptr = (uint8_t *)ah + ah_length; 2147 2148 if (post_ah_ptr > mp->b_wptr) { 2149 IP_AH_BUMP_STAT(in_discards); 2150 ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, 2151 &ipdrops_ah_bad_length, &ah_dropper); 2152 return (IPSEC_STATUS_FAILED); 2153 } 2154 2155 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - ah_length); 2156 *nexthdrp = ah->ah_nexthdr; 2157 ovbcopy(post_ah_ptr, ah, 2158 (size_t)((uintptr_t)mp->b_wptr - (uintptr_t)post_ah_ptr)); 2159 mp->b_wptr -= ah_length; 2160 /* Rewhack to be an ICMP error. */ 2161 mp->b_datap->db_type = M_CTL; 2162 2163 return (IPSEC_STATUS_SUCCESS); 2164 } 2165 2166 /* 2167 * IP sends up the ICMP errors for validation and the removal of 2168 * the AH header. 2169 */ 2170 static ipsec_status_t 2171 ah_icmp_error_v4(mblk_t *ipsec_mp) 2172 { 2173 mblk_t *mp; 2174 mblk_t *mp1; 2175 icmph_t *icmph; 2176 int iph_hdr_length; 2177 int hdr_length; 2178 isaf_t *hptr; 2179 ipsa_t *assoc; 2180 int ah_length; 2181 ipha_t *ipha; 2182 ipha_t *oipha; 2183 ah_t *ah; 2184 uint32_t length; 2185 int alloc_size; 2186 uint8_t nexthdr; 2187 2188 mp = ipsec_mp->b_cont; 2189 ASSERT(mp->b_datap->db_type == M_CTL); 2190 2191 /* 2192 * Change the type to M_DATA till we finish pullups. 2193 */ 2194 mp->b_datap->db_type = M_DATA; 2195 2196 oipha = ipha = (ipha_t *)mp->b_rptr; 2197 iph_hdr_length = IPH_HDR_LENGTH(ipha); 2198 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2199 2200 ipha = (ipha_t *)&icmph[1]; 2201 hdr_length = IPH_HDR_LENGTH(ipha); 2202 2203 /* 2204 * See if we have enough to locate the SPI 2205 */ 2206 if ((uchar_t *)ipha + hdr_length + 8 > mp->b_wptr) { 2207 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length + 8 - 2208 mp->b_rptr)) { 2209 ipsec_rl_strlog(info.mi_idnum, 0, 0, 2210 SL_WARN | SL_ERROR, 2211 "ICMP error: Small AH header\n"); 2212 IP_AH_BUMP_STAT(in_discards); 2213 ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, 2214 &ipdrops_ah_bad_length, &ah_dropper); 2215 return (IPSEC_STATUS_FAILED); 2216 } 2217 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2218 ipha = (ipha_t *)&icmph[1]; 2219 } 2220 2221 ah = (ah_t *)((uint8_t *)ipha + hdr_length); 2222 nexthdr = ah->ah_nexthdr; 2223 2224 hptr = OUTBOUND_BUCKET_V4(&ah_sadb.s_v4, ipha->ipha_dst); 2225 mutex_enter(&hptr->isaf_lock); 2226 assoc = ipsec_getassocbyspi(hptr, ah->ah_spi, 2227 (uint32_t *)&ipha->ipha_src, (uint32_t *)&ipha->ipha_dst, AF_INET); 2228 mutex_exit(&hptr->isaf_lock); 2229 2230 if (assoc == NULL) { 2231 IP_AH_BUMP_STAT(lookup_failure); 2232 IP_AH_BUMP_STAT(in_discards); 2233 if (ipsecah_log_unknown_spi) { 2234 ipsec_assocfailure(info.mi_idnum, 0, 0, 2235 SL_CONSOLE | SL_WARN | SL_ERROR, 2236 "Bad ICMP message - No association for the " 2237 "attached AH header whose spi is 0x%x, " 2238 "sender is 0x%x\n", 2239 ah->ah_spi, &oipha->ipha_src, AF_INET); 2240 } 2241 ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, &ipdrops_ah_no_sa, 2242 &ah_dropper); 2243 return (IPSEC_STATUS_FAILED); 2244 } 2245 2246 IPSA_REFRELE(assoc); 2247 /* 2248 * There seems to be a valid association. If there 2249 * is enough of AH header remove it, otherwise remove 2250 * as much as possible and send it back. One could check 2251 * whether it has complete AH header plus 8 bytes but it 2252 * does not make sense if an icmp error is returned for 2253 * ICMP messages e.g ICMP time exceeded, that are being 2254 * sent up. Let the caller figure out. 2255 * 2256 * NOTE: ah_length is the number of 32 bit words minus 2. 2257 */ 2258 ah_length = (ah->ah_length << 2) + 8; 2259 2260 if ((uchar_t *)ipha + hdr_length + ah_length > mp->b_wptr) { 2261 if (mp->b_cont == NULL) { 2262 /* 2263 * There is nothing to pullup. Just remove as 2264 * much as possible. This is a common case for 2265 * IPV4. 2266 */ 2267 ah_length = (mp->b_wptr - ((uchar_t *)ipha + 2268 hdr_length)); 2269 goto done; 2270 } 2271 /* Pullup the full ah header */ 2272 if (!pullupmsg(mp, (uchar_t *)ah + ah_length - mp->b_rptr)) { 2273 /* 2274 * pullupmsg could have failed if there was not 2275 * enough to pullup or memory allocation failed. 2276 * We tried hard, give up now. 2277 */ 2278 IP_AH_BUMP_STAT(in_discards); 2279 ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, 2280 &ipdrops_ah_nomem, &ah_dropper); 2281 return (IPSEC_STATUS_FAILED); 2282 } 2283 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 2284 ipha = (ipha_t *)&icmph[1]; 2285 } 2286 done: 2287 /* 2288 * Remove the AH header and change the protocol. 2289 * Don't update the spi fields in the ipsec_in 2290 * message as we are called just to validate the 2291 * message attached to the ICMP message. 2292 * 2293 * If we never pulled up since all of the message 2294 * is in one single mblk, we can't remove the AH header 2295 * by just setting the b_wptr to the beginning of the 2296 * AH header. We need to allocate a mblk that can hold 2297 * up until the inner IP header and copy them. 2298 */ 2299 alloc_size = iph_hdr_length + sizeof (icmph_t) + hdr_length; 2300 2301 if ((mp1 = allocb(alloc_size, BPRI_LO)) == NULL) { 2302 IP_AH_BUMP_STAT(in_discards); 2303 ip_drop_packet(ipsec_mp, B_TRUE, NULL, NULL, &ipdrops_ah_nomem, 2304 &ah_dropper); 2305 return (IPSEC_STATUS_FAILED); 2306 } 2307 /* ICMP errors are M_CTL messages */ 2308 mp1->b_datap->db_type = M_CTL; 2309 ipsec_mp->b_cont = mp1; 2310 bcopy(mp->b_rptr, mp1->b_rptr, alloc_size); 2311 mp1->b_wptr += alloc_size; 2312 2313 /* 2314 * Skip whatever we have copied and as much of AH header 2315 * possible. If we still have something left in the original 2316 * message, tag on. 2317 */ 2318 mp->b_rptr = (uchar_t *)ipha + hdr_length + ah_length; 2319 2320 if (mp->b_rptr != mp->b_wptr) { 2321 mp1->b_cont = mp; 2322 } else { 2323 if (mp->b_cont != NULL) 2324 mp1->b_cont = mp->b_cont; 2325 freeb(mp); 2326 } 2327 2328 ipha = (ipha_t *)(mp1->b_rptr + iph_hdr_length + sizeof (icmph_t)); 2329 ipha->ipha_protocol = nexthdr; 2330 length = ntohs(ipha->ipha_length); 2331 length -= ah_length; 2332 ipha->ipha_length = htons((uint16_t)length); 2333 ipha->ipha_hdr_checksum = 0; 2334 ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha); 2335 2336 return (IPSEC_STATUS_SUCCESS); 2337 } 2338 2339 /* 2340 * IP calls this to validate the ICMP errors that 2341 * we got from the network. 2342 */ 2343 ipsec_status_t 2344 ipsecah_icmp_error(mblk_t *mp) 2345 { 2346 ipsec_in_t *ii = (ipsec_in_t *)mp->b_rptr; 2347 2348 if (ii->ipsec_in_v4) 2349 return (ah_icmp_error_v4(mp)); 2350 else 2351 return (ah_icmp_error_v6(mp)); 2352 } 2353 2354 static int 2355 ah_fix_tlv_options_v6(uint8_t *oi_opt, uint8_t *pi_opt, uint_t ehdrlen, 2356 uint8_t hdr_type, boolean_t copy_always) 2357 { 2358 uint8_t opt_type; 2359 uint_t optlen; 2360 2361 ASSERT(hdr_type == IPPROTO_DSTOPTS || hdr_type == IPPROTO_HOPOPTS); 2362 2363 /* 2364 * Copy the next header and hdr ext. len of the HOP-by-HOP 2365 * and Destination option. 2366 */ 2367 *pi_opt++ = *oi_opt++; 2368 *pi_opt++ = *oi_opt++; 2369 ehdrlen -= 2; 2370 2371 /* 2372 * Now handle all the TLV encoded options. 2373 */ 2374 while (ehdrlen != 0) { 2375 opt_type = *oi_opt; 2376 2377 if (opt_type == IP6OPT_PAD1) { 2378 optlen = 1; 2379 } else { 2380 if (ehdrlen < 2) 2381 goto bad_opt; 2382 optlen = 2 + oi_opt[1]; 2383 if (optlen > ehdrlen) 2384 goto bad_opt; 2385 } 2386 if (copy_always || !(opt_type & IP6OPT_MUTABLE)) { 2387 bcopy(oi_opt, pi_opt, optlen); 2388 } else { 2389 if (optlen == 1) { 2390 *pi_opt = 0; 2391 } else { 2392 /* 2393 * Copy the type and data length fields. 2394 * Zero the option data by skipping 2395 * option type and option data len 2396 * fields. 2397 */ 2398 *pi_opt = *oi_opt; 2399 *(pi_opt + 1) = *(oi_opt + 1); 2400 bzero(pi_opt + 2, optlen - 2); 2401 } 2402 } 2403 ehdrlen -= optlen; 2404 oi_opt += optlen; 2405 pi_opt += optlen; 2406 } 2407 return (0); 2408 bad_opt: 2409 return (-1); 2410 } 2411 2412 /* 2413 * Construct a pseudo header for AH, processing all the options. 2414 * 2415 * oip6h is the IPv6 header of the incoming or outgoing packet. 2416 * ip6h is the pointer to the pseudo headers IPV6 header. All 2417 * the space needed for the options have been allocated including 2418 * the AH header. 2419 * 2420 * If copy_always is set, all the options that appear before AH are copied 2421 * blindly without checking for IP6OPT_MUTABLE. This is used by 2422 * ah_auth_out_done(). Please refer to that function for details. 2423 * 2424 * NOTE : 2425 * 2426 * * AH header is never copied in this function even if copy_always 2427 * is set. It just returns the ah_offset - offset of the AH header 2428 * and the caller needs to do the copying. This is done so that we 2429 * don't have pass extra arguments e.g. SA etc. and also, 2430 * it is not needed when ah_auth_out_done is calling this function. 2431 */ 2432 static uint_t 2433 ah_fix_phdr_v6(ip6_t *ip6h, ip6_t *oip6h, boolean_t outbound, 2434 boolean_t copy_always) 2435 { 2436 uint8_t *oi_opt; 2437 uint8_t *pi_opt; 2438 uint8_t nexthdr; 2439 uint8_t *prev_nexthdr; 2440 ip6_hbh_t *hbhhdr; 2441 ip6_dest_t *dsthdr = NULL; 2442 ip6_rthdr0_t *rthdr; 2443 int ehdrlen; 2444 ah_t *ah; 2445 int ret; 2446 2447 /* 2448 * In the outbound case for source route, ULP has already moved 2449 * the first hop, which is now in ip6_dst. We need to re-arrange 2450 * the header to make it look like how it would appear in the 2451 * receiver i.e 2452 * 2453 * Because of ip_massage_options_v6 the header looks like 2454 * this : 2455 * 2456 * ip6_src = S, ip6_dst = I1. followed by I2,I3,D. 2457 * 2458 * When it reaches the receiver, it would look like 2459 * 2460 * ip6_src = S, ip6_dst = D. followed by I1,I2,I3. 2461 * 2462 * NOTE : We assume that there are no problems with the options 2463 * as IP should have already checked this. 2464 */ 2465 2466 oi_opt = (uchar_t *)&oip6h[1]; 2467 pi_opt = (uchar_t *)&ip6h[1]; 2468 2469 /* 2470 * We set the prev_nexthdr properly in the pseudo header. 2471 * After we finish authentication and come back from the 2472 * algorithm module, pseudo header will become the real 2473 * IP header. 2474 */ 2475 prev_nexthdr = (uint8_t *)&ip6h->ip6_nxt; 2476 nexthdr = oip6h->ip6_nxt; 2477 /* Assume IP has already stripped it */ 2478 ASSERT(nexthdr != IPPROTO_FRAGMENT && nexthdr != IPPROTO_RAW); 2479 ah = NULL; 2480 dsthdr = NULL; 2481 for (;;) { 2482 switch (nexthdr) { 2483 case IPPROTO_HOPOPTS: 2484 hbhhdr = (ip6_hbh_t *)oi_opt; 2485 nexthdr = hbhhdr->ip6h_nxt; 2486 ehdrlen = 8 * (hbhhdr->ip6h_len + 1); 2487 ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen, 2488 IPPROTO_HOPOPTS, copy_always); 2489 /* 2490 * Return a zero offset indicating error if there 2491 * was error. 2492 */ 2493 if (ret == -1) 2494 return (0); 2495 hbhhdr = (ip6_hbh_t *)pi_opt; 2496 prev_nexthdr = (uint8_t *)&hbhhdr->ip6h_nxt; 2497 break; 2498 case IPPROTO_ROUTING: 2499 rthdr = (ip6_rthdr0_t *)oi_opt; 2500 nexthdr = rthdr->ip6r0_nxt; 2501 ehdrlen = 8 * (rthdr->ip6r0_len + 1); 2502 if (!copy_always && outbound) { 2503 int i, left; 2504 ip6_rthdr0_t *prthdr; 2505 in6_addr_t *ap, *pap; 2506 2507 left = rthdr->ip6r0_segleft; 2508 prthdr = (ip6_rthdr0_t *)pi_opt; 2509 pap = (in6_addr_t *)(prthdr + 1); 2510 ap = (in6_addr_t *)(rthdr + 1); 2511 /* 2512 * First eight bytes except seg_left 2513 * does not change en route. 2514 */ 2515 bcopy(oi_opt, pi_opt, 8); 2516 prthdr->ip6r0_segleft = 0; 2517 /* 2518 * First address has been moved to 2519 * the destination address of the 2520 * ip header by ip_massage_options_v6. 2521 * And the real destination address is 2522 * in the last address part of the 2523 * option. 2524 */ 2525 *pap = oip6h->ip6_dst; 2526 for (i = 1; i < left - 1; i++) 2527 pap[i] = ap[i - 1]; 2528 ip6h->ip6_dst = *(ap + left - 1); 2529 } else { 2530 bcopy(oi_opt, pi_opt, ehdrlen); 2531 } 2532 rthdr = (ip6_rthdr0_t *)pi_opt; 2533 prev_nexthdr = (uint8_t *)&rthdr->ip6r0_nxt; 2534 break; 2535 case IPPROTO_DSTOPTS: 2536 /* 2537 * Destination options are tricky. If there is 2538 * a terminal (e.g. non-IPv6-extension) header 2539 * following the destination options, don't 2540 * reset prev_nexthdr or advance the AH insertion 2541 * point and just treat this as a terminal header. 2542 * 2543 * If this is an inbound packet, just deal with 2544 * it as is. 2545 */ 2546 dsthdr = (ip6_dest_t *)oi_opt; 2547 /* 2548 * XXX I hope common-subexpression elimination 2549 * saves us the double-evaluate. 2550 */ 2551 if (outbound && dsthdr->ip6d_nxt != IPPROTO_ROUTING && 2552 dsthdr->ip6d_nxt != IPPROTO_HOPOPTS) 2553 goto terminal_hdr; 2554 nexthdr = dsthdr->ip6d_nxt; 2555 ehdrlen = 8 * (dsthdr->ip6d_len + 1); 2556 ret = ah_fix_tlv_options_v6(oi_opt, pi_opt, ehdrlen, 2557 IPPROTO_DSTOPTS, copy_always); 2558 /* 2559 * Return a zero offset indicating error if there 2560 * was error. 2561 */ 2562 if (ret == -1) 2563 return (0); 2564 break; 2565 case IPPROTO_AH: 2566 /* 2567 * Be conservative in what you send. We shouldn't 2568 * see two same-scoped AH's in one packet. 2569 * (Inner-IP-scoped AH will be hit by terminal 2570 * header of IP or IPv6.) 2571 */ 2572 ASSERT(!outbound); 2573 return ((uint_t)(pi_opt - (uint8_t *)ip6h)); 2574 default: 2575 ASSERT(outbound); 2576 terminal_hdr: 2577 *prev_nexthdr = IPPROTO_AH; 2578 ah = (ah_t *)pi_opt; 2579 ah->ah_nexthdr = nexthdr; 2580 return ((uint_t)(pi_opt - (uint8_t *)ip6h)); 2581 } 2582 pi_opt += ehdrlen; 2583 oi_opt += ehdrlen; 2584 } 2585 /* NOTREACHED */ 2586 } 2587 2588 static boolean_t 2589 ah_finish_up(ah_t *phdr_ah, ah_t *inbound_ah, ipsa_t *assoc, 2590 int ah_data_sz, int ah_align_sz) 2591 { 2592 int i; 2593 2594 /* 2595 * Padding : 2596 * 2597 * 1) Authentication data may have to be padded 2598 * before ICV calculation if ICV is not a multiple 2599 * of 64 bits. This padding is arbitrary and transmitted 2600 * with the packet at the end of the authentication data. 2601 * Payload length should include the padding bytes. 2602 * 2603 * 2) Explicit padding of the whole datagram may be 2604 * required by the algorithm which need not be 2605 * transmitted. It is assumed that this will be taken 2606 * care by the algorithm module. 2607 */ 2608 bzero(phdr_ah + 1, ah_data_sz); /* Zero out ICV for pseudo-hdr. */ 2609 2610 if (inbound_ah == NULL) { 2611 /* Outbound AH datagram. */ 2612 2613 phdr_ah->ah_length = (ah_align_sz >> 2) + 1; 2614 phdr_ah->ah_reserved = 0; 2615 phdr_ah->ah_spi = assoc->ipsa_spi; 2616 2617 phdr_ah->ah_replay = 2618 htonl(atomic_add_32_nv(&assoc->ipsa_replay, 1)); 2619 if (phdr_ah->ah_replay == 0 && assoc->ipsa_replay_wsize != 0) { 2620 /* 2621 * XXX We have replay counter wrapping. We probably 2622 * want to nuke this SA (and its peer). 2623 */ 2624 ipsec_assocfailure(info.mi_idnum, 0, 0, 2625 SL_ERROR | SL_CONSOLE | SL_WARN, 2626 "Outbound AH SA (0x%x), dst %s has wrapped " 2627 "sequence.\n", phdr_ah->ah_spi, 2628 assoc->ipsa_dstaddr, assoc->ipsa_addrfam); 2629 2630 sadb_replay_delete(assoc); 2631 /* Caller will free phdr_mp and return NULL. */ 2632 return (B_FALSE); 2633 } 2634 2635 if (ah_data_sz != ah_align_sz) { 2636 uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) + 2637 ah_data_sz); 2638 2639 for (i = 0; i < (ah_align_sz - ah_data_sz); i++) { 2640 pad[i] = (uchar_t)i; /* Fill the padding */ 2641 } 2642 } 2643 } else { 2644 /* Inbound AH datagram. */ 2645 phdr_ah->ah_nexthdr = inbound_ah->ah_nexthdr; 2646 phdr_ah->ah_length = inbound_ah->ah_length; 2647 phdr_ah->ah_reserved = 0; 2648 ASSERT(inbound_ah->ah_spi == assoc->ipsa_spi); 2649 phdr_ah->ah_spi = inbound_ah->ah_spi; 2650 phdr_ah->ah_replay = inbound_ah->ah_replay; 2651 2652 if (ah_data_sz != ah_align_sz) { 2653 uchar_t *opad = ((uchar_t *)inbound_ah + sizeof (ah_t) + 2654 ah_data_sz); 2655 uchar_t *pad = ((uchar_t *)phdr_ah + sizeof (ah_t) + 2656 ah_data_sz); 2657 2658 for (i = 0; i < (ah_align_sz - ah_data_sz); i++) { 2659 pad[i] = opad[i]; /* Copy the padding */ 2660 } 2661 } 2662 } 2663 2664 return (B_TRUE); 2665 } 2666 2667 /* 2668 * Called upon failing the inbound ICV check. The message passed as 2669 * argument is freed. 2670 */ 2671 static void 2672 ah_log_bad_auth(mblk_t *ipsec_in) 2673 { 2674 mblk_t *mp = ipsec_in->b_cont->b_cont; 2675 ipsec_in_t *ii = (ipsec_in_t *)ipsec_in->b_rptr; 2676 boolean_t isv4 = ii->ipsec_in_v4; 2677 ipsa_t *assoc = ii->ipsec_in_ah_sa; 2678 int af; 2679 void *addr; 2680 2681 mp->b_rptr -= ii->ipsec_in_skip_len; 2682 2683 if (isv4) { 2684 ipha_t *ipha = (ipha_t *)mp->b_rptr; 2685 addr = &ipha->ipha_dst; 2686 af = AF_INET; 2687 } else { 2688 ip6_t *ip6h = (ip6_t *)mp->b_rptr; 2689 addr = &ip6h->ip6_dst; 2690 af = AF_INET6; 2691 } 2692 2693 /* 2694 * Log the event. Don't print to the console, block 2695 * potential denial-of-service attack. 2696 */ 2697 AH_BUMP_STAT(bad_auth); 2698 2699 ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN, 2700 "AH Authentication failed spi %x, dst_addr %s", 2701 assoc->ipsa_spi, addr, af); 2702 2703 IP_AH_BUMP_STAT(in_discards); 2704 ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, &ipdrops_ah_bad_auth, 2705 &ah_dropper); 2706 } 2707 2708 /* 2709 * Kernel crypto framework callback invoked after completion of async 2710 * crypto requests. 2711 */ 2712 static void 2713 ah_kcf_callback(void *arg, int status) 2714 { 2715 mblk_t *ipsec_mp = (mblk_t *)arg; 2716 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 2717 boolean_t is_inbound = (ii->ipsec_in_type == IPSEC_IN); 2718 2719 ASSERT(ipsec_mp->b_cont != NULL); 2720 2721 if (status == CRYPTO_SUCCESS) { 2722 if (is_inbound) { 2723 if (ah_auth_in_done(ipsec_mp) != IPSEC_STATUS_SUCCESS) 2724 return; 2725 /* finish IPsec processing */ 2726 ip_fanout_proto_again(ipsec_mp, NULL, NULL, NULL); 2727 } else { 2728 ipha_t *ipha; 2729 2730 if (ah_auth_out_done(ipsec_mp) != IPSEC_STATUS_SUCCESS) 2731 return; 2732 2733 /* finish IPsec processing */ 2734 ipha = (ipha_t *)ipsec_mp->b_cont->b_rptr; 2735 if (IPH_HDR_VERSION(ipha) == IP_VERSION) { 2736 ip_wput_ipsec_out(NULL, ipsec_mp, ipha, NULL, 2737 NULL); 2738 } else { 2739 ip6_t *ip6h = (ip6_t *)ipha; 2740 ip_wput_ipsec_out_v6(NULL, ipsec_mp, ip6h, 2741 NULL, NULL); 2742 } 2743 } 2744 2745 } else if (status == CRYPTO_INVALID_MAC) { 2746 ah_log_bad_auth(ipsec_mp); 2747 2748 } else { 2749 ah1dbg(("ah_kcf_callback: crypto failed with 0x%x\n", status)); 2750 AH_BUMP_STAT(crypto_failures); 2751 if (is_inbound) 2752 IP_AH_BUMP_STAT(in_discards); 2753 else 2754 AH_BUMP_STAT(out_discards); 2755 ip_drop_packet(ipsec_mp, is_inbound, NULL, NULL, 2756 &ipdrops_ah_crypto_failed, &ah_dropper); 2757 } 2758 } 2759 2760 /* 2761 * Invoked on kernel crypto failure during inbound and outbound processing. 2762 */ 2763 static void 2764 ah_crypto_failed(mblk_t *mp, boolean_t is_inbound, int kef_rc) 2765 { 2766 ah1dbg(("crypto failed for %s AH with 0x%x\n", 2767 is_inbound ? "inbound" : "outbound", kef_rc)); 2768 ip_drop_packet(mp, is_inbound, NULL, NULL, &ipdrops_ah_crypto_failed, 2769 &ah_dropper); 2770 AH_BUMP_STAT(crypto_failures); 2771 if (is_inbound) 2772 IP_AH_BUMP_STAT(in_discards); 2773 else 2774 AH_BUMP_STAT(out_discards); 2775 } 2776 2777 /* 2778 * Helper macros for the ah_submit_req_{inbound,outbound}() functions. 2779 */ 2780 2781 #define AH_INIT_CALLREQ(_cr) { \ 2782 (_cr)->cr_flag = CRYPTO_SKIP_REQID|CRYPTO_RESTRICTED; \ 2783 if (ipsec_algs_exec_mode[IPSEC_ALG_AUTH] == IPSEC_ALGS_EXEC_ASYNC) \ 2784 (_cr)->cr_flag |= CRYPTO_ALWAYS_QUEUE; \ 2785 (_cr)->cr_callback_arg = ipsec_mp; \ 2786 (_cr)->cr_callback_func = ah_kcf_callback; \ 2787 } 2788 2789 #define AH_INIT_CRYPTO_DATA(data, msglen, mblk) { \ 2790 (data)->cd_format = CRYPTO_DATA_MBLK; \ 2791 (data)->cd_mp = mblk; \ 2792 (data)->cd_offset = 0; \ 2793 (data)->cd_length = msglen; \ 2794 } 2795 2796 #define AH_INIT_CRYPTO_MAC(mac, icvlen, icvbuf) { \ 2797 (mac)->cd_format = CRYPTO_DATA_RAW; \ 2798 (mac)->cd_offset = 0; \ 2799 (mac)->cd_length = icvlen; \ 2800 (mac)->cd_raw.iov_base = icvbuf; \ 2801 (mac)->cd_raw.iov_len = icvlen; \ 2802 } 2803 2804 /* 2805 * Submit an inbound packet for processing by the crypto framework. 2806 */ 2807 static ipsec_status_t 2808 ah_submit_req_inbound(mblk_t *ipsec_mp, size_t skip_len, uint32_t ah_offset, 2809 ipsa_t *assoc) 2810 { 2811 int kef_rc; 2812 mblk_t *phdr_mp; 2813 crypto_call_req_t call_req; 2814 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 2815 uint_t icv_len = assoc->ipsa_mac_len; 2816 crypto_ctx_template_t ctx_tmpl; 2817 2818 phdr_mp = ipsec_mp->b_cont; 2819 ASSERT(phdr_mp != NULL); 2820 ASSERT(ii->ipsec_in_type == IPSEC_IN); 2821 2822 /* init arguments for the crypto framework */ 2823 AH_INIT_CRYPTO_DATA(&ii->ipsec_in_crypto_data, AH_MSGSIZE(phdr_mp), 2824 phdr_mp); 2825 2826 AH_INIT_CRYPTO_MAC(&ii->ipsec_in_crypto_mac, icv_len, 2827 (char *)phdr_mp->b_cont->b_rptr - skip_len + ah_offset + 2828 sizeof (ah_t)); 2829 2830 AH_INIT_CALLREQ(&call_req); 2831 2832 ii->ipsec_in_skip_len = skip_len; 2833 2834 IPSEC_CTX_TMPL(assoc, ipsa_authtmpl, IPSEC_ALG_AUTH, ctx_tmpl); 2835 2836 /* call KEF to do the MAC operation */ 2837 kef_rc = crypto_mac_verify(&assoc->ipsa_amech, 2838 &ii->ipsec_in_crypto_data, &assoc->ipsa_kcfauthkey, ctx_tmpl, 2839 &ii->ipsec_in_crypto_mac, &call_req); 2840 2841 switch (kef_rc) { 2842 case CRYPTO_SUCCESS: 2843 AH_BUMP_STAT(crypto_sync); 2844 return (ah_auth_in_done(ipsec_mp)); 2845 case CRYPTO_QUEUED: 2846 /* ah_callback() will be invoked on completion */ 2847 AH_BUMP_STAT(crypto_async); 2848 return (IPSEC_STATUS_PENDING); 2849 case CRYPTO_INVALID_MAC: 2850 AH_BUMP_STAT(crypto_sync); 2851 ah_log_bad_auth(ipsec_mp); 2852 return (IPSEC_STATUS_FAILED); 2853 } 2854 2855 ah_crypto_failed(ipsec_mp, B_TRUE, kef_rc); 2856 return (IPSEC_STATUS_FAILED); 2857 } 2858 2859 /* 2860 * Submit an outbound packet for processing by the crypto framework. 2861 */ 2862 static ipsec_status_t 2863 ah_submit_req_outbound(mblk_t *ipsec_mp, size_t skip_len, ipsa_t *assoc) 2864 { 2865 int kef_rc; 2866 mblk_t *phdr_mp; 2867 crypto_call_req_t call_req; 2868 ipsec_out_t *io = (ipsec_out_t *)ipsec_mp->b_rptr; 2869 uint_t icv_len = assoc->ipsa_mac_len; 2870 2871 phdr_mp = ipsec_mp->b_cont; 2872 ASSERT(phdr_mp != NULL); 2873 ASSERT(io->ipsec_out_type == IPSEC_OUT); 2874 2875 /* init arguments for the crypto framework */ 2876 AH_INIT_CRYPTO_DATA(&io->ipsec_out_crypto_data, AH_MSGSIZE(phdr_mp), 2877 phdr_mp); 2878 2879 AH_INIT_CRYPTO_MAC(&io->ipsec_out_crypto_mac, icv_len, 2880 (char *)phdr_mp->b_wptr); 2881 2882 AH_INIT_CALLREQ(&call_req); 2883 2884 io->ipsec_out_skip_len = skip_len; 2885 2886 ASSERT(io->ipsec_out_ah_sa != NULL); 2887 2888 /* call KEF to do the MAC operation */ 2889 kef_rc = crypto_mac(&assoc->ipsa_amech, &io->ipsec_out_crypto_data, 2890 &assoc->ipsa_kcfauthkey, assoc->ipsa_authtmpl, 2891 &io->ipsec_out_crypto_mac, &call_req); 2892 2893 switch (kef_rc) { 2894 case CRYPTO_SUCCESS: 2895 AH_BUMP_STAT(crypto_sync); 2896 return (ah_auth_out_done(ipsec_mp)); 2897 case CRYPTO_QUEUED: 2898 /* ah_callback() will be invoked on completion */ 2899 AH_BUMP_STAT(crypto_async); 2900 return (IPSEC_STATUS_PENDING); 2901 } 2902 2903 ah_crypto_failed(ipsec_mp, B_FALSE, kef_rc); 2904 return (IPSEC_STATUS_FAILED); 2905 } 2906 2907 /* 2908 * This function constructs a pseudo header by looking at the IP header 2909 * and options if any. This is called for both outbound and inbound, 2910 * before computing the ICV. 2911 */ 2912 static mblk_t * 2913 ah_process_ip_options_v6(mblk_t *mp, ipsa_t *assoc, int *length_to_skip, 2914 uint_t ah_data_sz, boolean_t outbound) 2915 { 2916 ip6_t *ip6h; 2917 ip6_t *oip6h; 2918 mblk_t *phdr_mp; 2919 int option_length; 2920 uint_t ah_align_sz; 2921 uint_t ah_offset; 2922 int hdr_size; 2923 2924 /* 2925 * Allocate space for the authentication data also. It is 2926 * useful both during the ICV calculation where we need to 2927 * feed in zeroes and while sending the datagram back to IP 2928 * where we will be using the same space. 2929 * 2930 * We need to allocate space for padding bytes if it is not 2931 * a multiple of IPV6_PADDING_ALIGN. 2932 * 2933 * In addition, we allocate space for the ICV computed by 2934 * the kernel crypto framework, saving us a separate kmem 2935 * allocation down the road. 2936 */ 2937 2938 ah_align_sz = P2ALIGN(ah_data_sz + IPV6_PADDING_ALIGN - 1, 2939 IPV6_PADDING_ALIGN); 2940 2941 ASSERT(ah_align_sz >= ah_data_sz); 2942 2943 hdr_size = ipsec_ah_get_hdr_size_v6(mp, B_FALSE); 2944 option_length = hdr_size - IPV6_HDR_LEN; 2945 2946 /* This was not included in ipsec_ah_get_hdr_size_v6() */ 2947 hdr_size += (sizeof (ah_t) + ah_align_sz); 2948 2949 if (!outbound && (MBLKL(mp) < hdr_size)) { 2950 /* 2951 * We have post-AH header options in a separate mblk, 2952 * a pullup is required. 2953 */ 2954 if (!pullupmsg(mp, hdr_size)) 2955 return (NULL); 2956 } 2957 2958 if ((phdr_mp = allocb(hdr_size + ah_data_sz, BPRI_HI)) == NULL) { 2959 return (NULL); 2960 } 2961 2962 oip6h = (ip6_t *)mp->b_rptr; 2963 2964 /* 2965 * Form the basic IP header first. Zero out the header 2966 * so that the mutable fields are zeroed out. 2967 */ 2968 ip6h = (ip6_t *)phdr_mp->b_rptr; 2969 bzero(ip6h, sizeof (ip6_t)); 2970 ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 2971 2972 if (outbound) { 2973 /* 2974 * Include the size of AH and authentication data. 2975 * This is how our recipient would compute the 2976 * authentication data. Look at what we do in the 2977 * inbound case below. 2978 */ 2979 ip6h->ip6_plen = htons(ntohs(oip6h->ip6_plen) + 2980 sizeof (ah_t) + ah_align_sz); 2981 } else { 2982 ip6h->ip6_plen = oip6h->ip6_plen; 2983 } 2984 2985 ip6h->ip6_src = oip6h->ip6_src; 2986 ip6h->ip6_dst = oip6h->ip6_dst; 2987 2988 *length_to_skip = IPV6_HDR_LEN; 2989 if (option_length == 0) { 2990 /* Form the AH header */ 2991 ip6h->ip6_nxt = IPPROTO_AH; 2992 ((ah_t *)(ip6h + 1))->ah_nexthdr = oip6h->ip6_nxt; 2993 ah_offset = *length_to_skip; 2994 } else { 2995 ip6h->ip6_nxt = oip6h->ip6_nxt; 2996 /* option_length does not include the AH header's size */ 2997 *length_to_skip += option_length; 2998 2999 ah_offset = ah_fix_phdr_v6(ip6h, oip6h, outbound, B_FALSE); 3000 if (ah_offset == 0) { 3001 ip_drop_packet(phdr_mp, !outbound, NULL, NULL, 3002 &ipdrops_ah_bad_v6_hdrs, &ah_dropper); 3003 return (NULL); 3004 } 3005 } 3006 3007 if (!ah_finish_up(((ah_t *)((uint8_t *)ip6h + ah_offset)), 3008 (outbound ? NULL : ((ah_t *)((uint8_t *)oip6h + ah_offset))), 3009 assoc, ah_data_sz, ah_align_sz)) { 3010 freeb(phdr_mp); 3011 /* 3012 * Returning NULL will tell the caller to 3013 * IPSA_REFELE(), free the memory, etc. 3014 */ 3015 return (NULL); 3016 } 3017 3018 phdr_mp->b_wptr = ((uint8_t *)ip6h + ah_offset + sizeof (ah_t) + 3019 ah_align_sz); 3020 if (!outbound) 3021 *length_to_skip += sizeof (ah_t) + ah_align_sz; 3022 return (phdr_mp); 3023 } 3024 3025 /* 3026 * This function constructs a pseudo header by looking at the IP header 3027 * and options if any. This is called for both outbound and inbound, 3028 * before computing the ICV. 3029 */ 3030 static mblk_t * 3031 ah_process_ip_options_v4(mblk_t *mp, ipsa_t *assoc, int *length_to_skip, 3032 uint_t ah_data_sz, boolean_t outbound) 3033 { 3034 ipoptp_t opts; 3035 uint32_t option_length; 3036 ipha_t *ipha; 3037 ipha_t *oipha; 3038 mblk_t *phdr_mp; 3039 int size; 3040 uchar_t *optptr; 3041 uint8_t optval; 3042 uint8_t optlen; 3043 ipaddr_t dst; 3044 uint32_t v_hlen_tos_len; 3045 int ip_hdr_length; 3046 uint_t ah_align_sz; 3047 uint32_t off; 3048 3049 #ifdef _BIG_ENDIAN 3050 #define V_HLEN (v_hlen_tos_len >> 24) 3051 #else 3052 #define V_HLEN (v_hlen_tos_len & 0xFF) 3053 #endif 3054 3055 oipha = (ipha_t *)mp->b_rptr; 3056 v_hlen_tos_len = ((uint32_t *)oipha)[0]; 3057 3058 /* 3059 * Allocate space for the authentication data also. It is 3060 * useful both during the ICV calculation where we need to 3061 * feed in zeroes and while sending the datagram back to IP 3062 * where we will be using the same space. 3063 * 3064 * We need to allocate space for padding bytes if it is not 3065 * a multiple of IPV4_PADDING_ALIGN. 3066 * 3067 * In addition, we allocate space for the ICV computed by 3068 * the kernel crypto framework, saving us a separate kmem 3069 * allocation down the road. 3070 */ 3071 3072 ah_align_sz = P2ALIGN(ah_data_sz + IPV4_PADDING_ALIGN - 1, 3073 IPV4_PADDING_ALIGN); 3074 3075 ASSERT(ah_align_sz >= ah_data_sz); 3076 3077 size = IP_SIMPLE_HDR_LENGTH + sizeof (ah_t) + ah_align_sz + 3078 ah_data_sz; 3079 3080 if (V_HLEN != IP_SIMPLE_HDR_VERSION) { 3081 option_length = oipha->ipha_version_and_hdr_length - 3082 (uint8_t)((IP_VERSION << 4) + 3083 IP_SIMPLE_HDR_LENGTH_IN_WORDS); 3084 option_length <<= 2; 3085 size += option_length; 3086 } 3087 3088 if ((phdr_mp = allocb(size, BPRI_HI)) == NULL) { 3089 return (NULL); 3090 } 3091 3092 /* 3093 * Form the basic IP header first. 3094 */ 3095 ipha = (ipha_t *)phdr_mp->b_rptr; 3096 ipha->ipha_version_and_hdr_length = oipha->ipha_version_and_hdr_length; 3097 ipha->ipha_type_of_service = 0; 3098 3099 if (outbound) { 3100 /* 3101 * Include the size of AH and authentication data. 3102 * This is how our recipient would compute the 3103 * authentication data. Look at what we do in the 3104 * inbound case below. 3105 */ 3106 ipha->ipha_length = ntohs(htons(oipha->ipha_length) + 3107 sizeof (ah_t) + ah_align_sz); 3108 } else { 3109 ipha->ipha_length = oipha->ipha_length; 3110 } 3111 3112 ipha->ipha_ident = oipha->ipha_ident; 3113 ipha->ipha_fragment_offset_and_flags = 0; 3114 ipha->ipha_ttl = 0; 3115 ipha->ipha_protocol = IPPROTO_AH; 3116 ipha->ipha_hdr_checksum = 0; 3117 ipha->ipha_src = oipha->ipha_src; 3118 ipha->ipha_dst = dst = oipha->ipha_dst; 3119 3120 /* 3121 * If there is no option to process return now. 3122 */ 3123 ip_hdr_length = IP_SIMPLE_HDR_LENGTH; 3124 3125 if (V_HLEN == IP_SIMPLE_HDR_VERSION) { 3126 /* Form the AH header */ 3127 goto ah_hdr; 3128 } 3129 3130 ip_hdr_length += option_length; 3131 3132 /* 3133 * We have options. In the outbound case for source route, 3134 * ULP has already moved the first hop, which is now in 3135 * ipha_dst. We need the final destination for the calculation 3136 * of authentication data. And also make sure that mutable 3137 * and experimental fields are zeroed out in the IP options. 3138 */ 3139 3140 bcopy(&oipha[1], &ipha[1], option_length); 3141 3142 for (optval = ipoptp_first(&opts, ipha); 3143 optval != IPOPT_EOL; 3144 optval = ipoptp_next(&opts)) { 3145 optptr = opts.ipoptp_cur; 3146 optlen = opts.ipoptp_len; 3147 switch (optval) { 3148 case IPOPT_EXTSEC: 3149 case IPOPT_COMSEC: 3150 case IPOPT_RA: 3151 case IPOPT_SDMDD: 3152 case IPOPT_SECURITY: 3153 /* 3154 * These options are Immutable, leave them as-is. 3155 * Note that IPOPT_NOP is also Immutable, but it 3156 * was skipped by ipoptp_next() and thus remains 3157 * intact in the header. 3158 */ 3159 break; 3160 case IPOPT_SSRR: 3161 case IPOPT_LSRR: 3162 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) 3163 goto bad_ipv4opt; 3164 /* 3165 * These two are mutable and will be zeroed, but 3166 * first get the final destination. 3167 */ 3168 off = optptr[IPOPT_OFFSET]; 3169 /* 3170 * If one of the conditions is true, it means 3171 * end of options and dst already has the right 3172 * value. So, just fall through. 3173 */ 3174 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) { 3175 off = optlen - IP_ADDR_LEN; 3176 bcopy(&optptr[off], &dst, IP_ADDR_LEN); 3177 } 3178 /* FALLTHRU */ 3179 case IPOPT_RR: 3180 case IPOPT_TS: 3181 case IPOPT_SATID: 3182 default: 3183 /* 3184 * optlen should include from the beginning of an 3185 * option. 3186 * NOTE : Stream Identifier Option (SID): RFC 791 3187 * shows the bit pattern of optlen as 2 and documents 3188 * the length as 4. We assume it to be 2 here. 3189 */ 3190 bzero(optptr, optlen); 3191 break; 3192 } 3193 } 3194 3195 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) { 3196 bad_ipv4opt: 3197 ah1dbg(("AH : bad IPv4 option")); 3198 freeb(phdr_mp); 3199 return (NULL); 3200 } 3201 3202 /* 3203 * Don't change ipha_dst for an inbound datagram as it points 3204 * to the right value. Only for the outbound with LSRR/SSRR, 3205 * because of ip_massage_options called by the ULP, ipha_dst 3206 * points to the first hop and we need to use the final 3207 * destination for computing the ICV. 3208 */ 3209 3210 if (outbound) 3211 ipha->ipha_dst = dst; 3212 ah_hdr: 3213 ((ah_t *)((uint8_t *)ipha + ip_hdr_length))->ah_nexthdr = 3214 oipha->ipha_protocol; 3215 if (!ah_finish_up(((ah_t *)((uint8_t *)ipha + ip_hdr_length)), 3216 (outbound ? NULL : ((ah_t *)((uint8_t *)oipha + ip_hdr_length))), 3217 assoc, ah_data_sz, ah_align_sz)) { 3218 freeb(phdr_mp); 3219 /* 3220 * Returning NULL will tell the caller to IPSA_REFELE(), free 3221 * the memory, etc. 3222 */ 3223 return (NULL); 3224 } 3225 3226 phdr_mp->b_wptr = ((uchar_t *)ipha + ip_hdr_length + 3227 sizeof (ah_t) + ah_align_sz); 3228 3229 ASSERT(phdr_mp->b_wptr <= phdr_mp->b_datap->db_lim); 3230 if (outbound) 3231 *length_to_skip = ip_hdr_length; 3232 else 3233 *length_to_skip = ip_hdr_length + sizeof (ah_t) + ah_align_sz; 3234 return (phdr_mp); 3235 } 3236 3237 /* 3238 * Authenticate an outbound datagram. This function is called 3239 * whenever IP sends an outbound datagram that needs authentication. 3240 */ 3241 static ipsec_status_t 3242 ah_outbound(mblk_t *ipsec_out) 3243 { 3244 mblk_t *mp; 3245 mblk_t *phdr_mp; 3246 ipsec_out_t *oi; 3247 ipsa_t *assoc; 3248 int length_to_skip; 3249 uint_t ah_align_sz; 3250 uint_t age_bytes; 3251 3252 /* 3253 * Construct the chain of mblks 3254 * 3255 * IPSEC_OUT->PSEUDO_HDR->DATA 3256 * 3257 * one by one. 3258 */ 3259 3260 AH_BUMP_STAT(out_requests); 3261 3262 ASSERT(ipsec_out->b_datap->db_type == M_CTL); 3263 3264 ASSERT(MBLKL(ipsec_out) >= sizeof (ipsec_info_t)); 3265 3266 mp = ipsec_out->b_cont; 3267 oi = (ipsec_out_t *)ipsec_out->b_rptr; 3268 3269 ASSERT(mp->b_datap->db_type == M_DATA); 3270 3271 assoc = oi->ipsec_out_ah_sa; 3272 ASSERT(assoc != NULL); 3273 if (assoc->ipsa_usetime == 0) 3274 ah_set_usetime(assoc, B_FALSE); 3275 3276 /* 3277 * Age SA according to number of bytes that will be sent after 3278 * adding the AH header, ICV, and padding to the packet. 3279 */ 3280 3281 if (oi->ipsec_out_v4) { 3282 ipha_t *ipha = (ipha_t *)mp->b_rptr; 3283 ah_align_sz = P2ALIGN(assoc->ipsa_mac_len + 3284 IPV4_PADDING_ALIGN - 1, IPV4_PADDING_ALIGN); 3285 age_bytes = ntohs(ipha->ipha_length) + sizeof (ah_t) + 3286 ah_align_sz; 3287 } else { 3288 ip6_t *ip6h = (ip6_t *)mp->b_rptr; 3289 ah_align_sz = P2ALIGN(assoc->ipsa_mac_len + 3290 IPV6_PADDING_ALIGN - 1, IPV6_PADDING_ALIGN); 3291 age_bytes = sizeof (ip6_t) + ntohs(ip6h->ip6_plen) + 3292 sizeof (ah_t) + ah_align_sz; 3293 } 3294 3295 if (!ah_age_bytes(assoc, age_bytes, B_FALSE)) { 3296 /* rig things as if ipsec_getassocbyconn() failed */ 3297 ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN, 3298 "AH association 0x%x, dst %s had bytes expire.\n", 3299 ntohl(assoc->ipsa_spi), assoc->ipsa_dstaddr, AF_INET); 3300 freemsg(ipsec_out); 3301 return (IPSEC_STATUS_FAILED); 3302 } 3303 3304 if (oi->ipsec_out_is_capab_ill) { 3305 ah3dbg(("ah_outbound: pkt can be accelerated\n")); 3306 if (oi->ipsec_out_v4) 3307 return (ah_outbound_accelerated_v4(ipsec_out, assoc)); 3308 else 3309 return (ah_outbound_accelerated_v6(ipsec_out, assoc)); 3310 } 3311 AH_BUMP_STAT(noaccel); 3312 3313 /* 3314 * Insert pseudo header: 3315 * IPSEC_INFO -> [IP, ULP] => IPSEC_INFO -> [IP, AH, ICV] -> ULP 3316 */ 3317 3318 if (oi->ipsec_out_v4) { 3319 phdr_mp = ah_process_ip_options_v4(mp, assoc, &length_to_skip, 3320 assoc->ipsa_mac_len, B_TRUE); 3321 } else { 3322 phdr_mp = ah_process_ip_options_v6(mp, assoc, &length_to_skip, 3323 assoc->ipsa_mac_len, B_TRUE); 3324 } 3325 3326 if (phdr_mp == NULL) { 3327 AH_BUMP_STAT(out_discards); 3328 ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL, 3329 &ipdrops_ah_bad_v4_opts, &ah_dropper); 3330 return (IPSEC_STATUS_FAILED); 3331 } 3332 3333 ipsec_out->b_cont = phdr_mp; 3334 phdr_mp->b_cont = mp; 3335 mp->b_rptr += length_to_skip; 3336 3337 /* 3338 * At this point ipsec_out points to the IPSEC_OUT, new_mp 3339 * points to an mblk containing the pseudo header (IP header, 3340 * AH header, and ICV with mutable fields zero'ed out). 3341 * mp points to the mblk containing the ULP data. The original 3342 * IP header is kept before the ULP data in mp. 3343 */ 3344 3345 /* submit MAC request to KCF */ 3346 return (ah_submit_req_outbound(ipsec_out, length_to_skip, assoc)); 3347 } 3348 3349 static ipsec_status_t 3350 ah_inbound(mblk_t *ipsec_in_mp, void *arg) 3351 { 3352 mblk_t *data_mp = ipsec_in_mp->b_cont; 3353 ipsec_in_t *ii = (ipsec_in_t *)ipsec_in_mp->b_rptr; 3354 ah_t *ah = (ah_t *)arg; 3355 ipsa_t *assoc = ii->ipsec_in_ah_sa; 3356 int length_to_skip; 3357 int ah_length; 3358 mblk_t *phdr_mp; 3359 uint32_t ah_offset; 3360 3361 ASSERT(assoc != NULL); 3362 if (assoc->ipsa_usetime == 0) 3363 ah_set_usetime(assoc, B_TRUE); 3364 3365 /* 3366 * We may wish to check replay in-range-only here as an optimization. 3367 * Include the reality check of ipsa->ipsa_replay > 3368 * ipsa->ipsa_replay_wsize for times when it's the first N packets, 3369 * where N == ipsa->ipsa_replay_wsize. 3370 * 3371 * Another check that may come here later is the "collision" check. 3372 * If legitimate packets flow quickly enough, this won't be a problem, 3373 * but collisions may cause authentication algorithm crunching to 3374 * take place when it doesn't need to. 3375 */ 3376 if (!sadb_replay_peek(assoc, ah->ah_replay)) { 3377 AH_BUMP_STAT(replay_early_failures); 3378 IP_AH_BUMP_STAT(in_discards); 3379 ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL, 3380 &ipdrops_ah_early_replay, &ah_dropper); 3381 return (IPSEC_STATUS_FAILED); 3382 } 3383 3384 /* 3385 * The offset of the AH header can be computed from its pointer 3386 * within the data mblk, which was pulled up until the AH header 3387 * by ipsec_inbound_ah_sa() during SA selection. 3388 */ 3389 ah_offset = (uchar_t *)ah - data_mp->b_rptr; 3390 3391 /* 3392 * Has this packet already been processed by a hardware 3393 * IPsec accelerator? 3394 */ 3395 if (ii->ipsec_in_accelerated) { 3396 ah3dbg(("ah_inbound_v6: pkt processed by ill=%d isv6=%d\n", 3397 ii->ipsec_in_ill_index, !ii->ipsec_in_v4)); 3398 return (ah_inbound_accelerated(ipsec_in_mp, ii->ipsec_in_v4, 3399 assoc, ah_offset)); 3400 } 3401 AH_BUMP_STAT(noaccel); 3402 3403 /* 3404 * We need to pullup until the ICV before we call 3405 * ah_process_ip_options_v6. 3406 */ 3407 ah_length = (ah->ah_length << 2) + 8; 3408 3409 /* 3410 * NOTE : If we want to use any field of IP/AH header, you need 3411 * to re-assign following the pullup. 3412 */ 3413 if (((uchar_t *)ah + ah_length) > data_mp->b_wptr) { 3414 if (!pullupmsg(data_mp, (uchar_t *)ah + ah_length - 3415 data_mp->b_rptr)) { 3416 (void) ipsec_rl_strlog(info.mi_idnum, 0, 0, 3417 SL_WARN | SL_ERROR, 3418 "ah_inbound: Small AH header\n"); 3419 IP_AH_BUMP_STAT(in_discards); 3420 ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL, 3421 &ipdrops_ah_nomem, &ah_dropper); 3422 return (IPSEC_STATUS_FAILED); 3423 } 3424 } 3425 3426 /* 3427 * Insert pseudo header: 3428 * IPSEC_INFO -> [IP, ULP] => IPSEC_INFO -> [IP, AH, ICV] -> ULP 3429 */ 3430 if (ii->ipsec_in_v4) { 3431 phdr_mp = ah_process_ip_options_v4(data_mp, assoc, 3432 &length_to_skip, assoc->ipsa_mac_len, B_FALSE); 3433 } else { 3434 phdr_mp = ah_process_ip_options_v6(data_mp, assoc, 3435 &length_to_skip, assoc->ipsa_mac_len, B_FALSE); 3436 } 3437 3438 if (phdr_mp == NULL) { 3439 IP_AH_BUMP_STAT(in_discards); 3440 ip_drop_packet(ipsec_in_mp, B_TRUE, NULL, NULL, 3441 ii->ipsec_in_v4 ? &ipdrops_ah_bad_v4_opts : 3442 &ipdrops_ah_bad_v6_hdrs, &ah_dropper); 3443 return (IPSEC_STATUS_FAILED); 3444 } 3445 3446 ipsec_in_mp->b_cont = phdr_mp; 3447 phdr_mp->b_cont = data_mp; 3448 data_mp->b_rptr += length_to_skip; 3449 3450 /* submit request to KCF */ 3451 return (ah_submit_req_inbound(ipsec_in_mp, length_to_skip, ah_offset, 3452 assoc)); 3453 } 3454 3455 /* 3456 * ah_inbound_accelerated: 3457 * Called from ah_inbound() to process IPsec packets that have been 3458 * accelerated by hardware. 3459 * 3460 * Basically does what ah_auth_in_done() with some changes since 3461 * no pseudo-headers are involved, i.e. the passed message is a 3462 * IPSEC_INFO->DATA. 3463 * 3464 * It is assumed that only packets that have been successfully 3465 * processed by the adapter come here. 3466 * 3467 * 1. get algorithm structure corresponding to association 3468 * 2. calculate pointers to authentication header and ICV 3469 * 3. compare ICV in AH header with ICV in data attributes 3470 * 3.1 if different: 3471 * 3.1.1 generate error 3472 * 3.1.2 discard message 3473 * 3.2 if ICV matches: 3474 * 3.2.1 check replay 3475 * 3.2.2 remove AH header 3476 * 3.2.3 age SA byte 3477 * 3.2.4 send to IP 3478 */ 3479 ipsec_status_t 3480 ah_inbound_accelerated(mblk_t *ipsec_in, boolean_t isv4, ipsa_t *assoc, 3481 uint32_t ah_offset) 3482 { 3483 mblk_t *mp; 3484 ipha_t *ipha; 3485 ah_t *ah; 3486 ipsec_in_t *ii; 3487 uint32_t icv_len; 3488 uint32_t align_len; 3489 uint32_t age_bytes; 3490 ip6_t *ip6h; 3491 uint8_t *in_icv; 3492 mblk_t *hada_mp; 3493 uint32_t next_hdr; 3494 da_ipsec_t *hada; 3495 kstat_named_t *counter; 3496 3497 AH_BUMP_STAT(in_accelerated); 3498 3499 ii = (ipsec_in_t *)ipsec_in->b_rptr; 3500 mp = ipsec_in->b_cont; 3501 hada_mp = ii->ipsec_in_da; 3502 ASSERT(hada_mp != NULL); 3503 hada = (da_ipsec_t *)hada_mp->b_rptr; 3504 3505 /* 3506 * We only support one level of decapsulation in hardware, so 3507 * nuke the pointer. 3508 */ 3509 ii->ipsec_in_da = NULL; 3510 ii->ipsec_in_accelerated = B_FALSE; 3511 3512 /* 3513 * Extract ICV length from attributes M_CTL and sanity check 3514 * its value. We allow the mblk to be smaller than da_ipsec_t 3515 * for a small ICV, as long as the entire ICV fits within the mblk. 3516 * Also ensures that the ICV length computed by Provider 3517 * corresponds to the ICV length of the algorithm specified by the SA. 3518 */ 3519 icv_len = hada->da_icv_len; 3520 if ((icv_len != assoc->ipsa_mac_len) || 3521 (icv_len > DA_ICV_MAX_LEN) || (MBLKL(hada_mp) < 3522 (sizeof (da_ipsec_t) - DA_ICV_MAX_LEN + icv_len))) { 3523 ah0dbg(("ah_inbound_accelerated: " 3524 "ICV len (%u) incorrect or mblk too small (%u)\n", 3525 icv_len, (uint32_t)(MBLKL(hada_mp)))); 3526 counter = &ipdrops_ah_bad_length; 3527 goto ah_in_discard; 3528 } 3529 ASSERT(icv_len != 0); 3530 3531 /* compute the padded AH ICV len */ 3532 if (isv4) { 3533 ipha = (ipha_t *)mp->b_rptr; 3534 align_len = (icv_len + IPV4_PADDING_ALIGN - 1) & 3535 -IPV4_PADDING_ALIGN; 3536 } else { 3537 ip6h = (ip6_t *)mp->b_rptr; 3538 align_len = (icv_len + IPV6_PADDING_ALIGN - 1) & 3539 -IPV6_PADDING_ALIGN; 3540 } 3541 3542 ah = (ah_t *)(mp->b_rptr + ah_offset); 3543 in_icv = (uint8_t *)ah + sizeof (ah_t); 3544 3545 /* compare ICV in AH header vs ICV computed by adapter */ 3546 if (bcmp(hada->da_icv, in_icv, icv_len)) { 3547 int af; 3548 void *addr; 3549 3550 if (isv4) { 3551 addr = &ipha->ipha_dst; 3552 af = AF_INET; 3553 } else { 3554 addr = &ip6h->ip6_dst; 3555 af = AF_INET6; 3556 } 3557 3558 /* 3559 * Log the event. Don't print to the console, block 3560 * potential denial-of-service attack. 3561 */ 3562 AH_BUMP_STAT(bad_auth); 3563 ipsec_assocfailure(info.mi_idnum, 0, 0, SL_ERROR | SL_WARN, 3564 "AH Authentication failed spi %x, dst_addr %s", 3565 assoc->ipsa_spi, addr, af); 3566 counter = &ipdrops_ah_bad_auth; 3567 goto ah_in_discard; 3568 } 3569 3570 ah3dbg(("AH succeeded, checking replay\n")); 3571 AH_BUMP_STAT(good_auth); 3572 3573 if (!sadb_replay_check(assoc, ah->ah_replay)) { 3574 int af; 3575 void *addr; 3576 3577 if (isv4) { 3578 addr = &ipha->ipha_dst; 3579 af = AF_INET; 3580 } else { 3581 addr = &ip6h->ip6_dst; 3582 af = AF_INET6; 3583 } 3584 3585 /* 3586 * Log the event. As of now we print out an event. 3587 * Do not print the replay failure number, or else 3588 * syslog cannot collate the error messages. Printing 3589 * the replay number that failed (or printing to the 3590 * console) opens a denial-of-service attack. 3591 */ 3592 AH_BUMP_STAT(replay_failures); 3593 ipsec_assocfailure(info.mi_idnum, 0, 0, 3594 SL_ERROR | SL_WARN, 3595 "Replay failed for AH spi %x, dst_addr %s", 3596 assoc->ipsa_spi, addr, af); 3597 counter = &ipdrops_ah_replay; 3598 goto ah_in_discard; 3599 } 3600 3601 /* 3602 * Remove AH header. We do this by copying everything before 3603 * the AH header onto the AH header+ICV. 3604 */ 3605 /* overwrite AH with what was preceeding it (IP header) */ 3606 next_hdr = ah->ah_nexthdr; 3607 ovbcopy(mp->b_rptr, mp->b_rptr + sizeof (ah_t) + align_len, 3608 ah_offset); 3609 mp->b_rptr += sizeof (ah_t) + align_len; 3610 if (isv4) { 3611 /* adjust IP header next protocol */ 3612 ipha = (ipha_t *)mp->b_rptr; 3613 ipha->ipha_protocol = next_hdr; 3614 3615 age_bytes = ipha->ipha_length; 3616 3617 /* adjust length in IP header */ 3618 ipha->ipha_length -= (sizeof (ah_t) + align_len); 3619 3620 /* recalculate checksum */ 3621 ipha->ipha_hdr_checksum = 0; 3622 ipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(ipha); 3623 } else { 3624 /* adjust IP header next protocol */ 3625 ip6h = (ip6_t *)mp->b_rptr; 3626 ip6h->ip6_nxt = next_hdr; 3627 3628 age_bytes = sizeof (ip6_t) + ntohs(ip6h->ip6_plen) + 3629 sizeof (ah_t); 3630 3631 /* adjust length in IP header */ 3632 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 3633 (sizeof (ah_t) + align_len)); 3634 } 3635 3636 /* age SA */ 3637 if (!ah_age_bytes(assoc, age_bytes, B_TRUE)) { 3638 /* The ipsa has hit hard expiration, LOG and AUDIT. */ 3639 ipsec_assocfailure(info.mi_idnum, 0, 0, 3640 SL_ERROR | SL_WARN, 3641 "AH Association 0x%x, dst %s had bytes expire.\n", 3642 assoc->ipsa_spi, assoc->ipsa_dstaddr, 3643 AF_INET); 3644 AH_BUMP_STAT(bytes_expired); 3645 counter = &ipdrops_ah_bytes_expire; 3646 goto ah_in_discard; 3647 } 3648 3649 freeb(hada_mp); 3650 return (IPSEC_STATUS_SUCCESS); 3651 3652 ah_in_discard: 3653 IP_AH_BUMP_STAT(in_discards); 3654 freeb(hada_mp); 3655 ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter, &ah_dropper); 3656 return (IPSEC_STATUS_FAILED); 3657 } 3658 3659 /* 3660 * ah_outbound_accelerated_v4: 3661 * Called from ah_outbound_v4() and once it is determined that the 3662 * packet is elligible for hardware acceleration. 3663 * 3664 * We proceed as follows: 3665 * 1. allocate and initialize attributes mblk 3666 * 2. mark IPSEC_OUT to indicate that pkt is accelerated 3667 * 3. insert AH header 3668 */ 3669 static ipsec_status_t 3670 ah_outbound_accelerated_v4(mblk_t *ipsec_mp, ipsa_t *assoc) 3671 { 3672 mblk_t *mp, *new_mp; 3673 ipsec_out_t *oi; 3674 uint_t ah_data_sz; /* ICV length, algorithm dependent */ 3675 uint_t ah_align_sz; /* ICV length + padding */ 3676 uint32_t v_hlen_tos_len; /* from original IP header */ 3677 ipha_t *oipha; /* original IP header */ 3678 ipha_t *nipha; /* new IP header */ 3679 uint_t option_length = 0; 3680 uint_t new_hdr_len; /* new header length */ 3681 uint_t iphdr_length; 3682 ah_t *ah_hdr; /* ptr to AH header */ 3683 3684 AH_BUMP_STAT(out_accelerated); 3685 3686 oi = (ipsec_out_t *)ipsec_mp->b_rptr; 3687 mp = ipsec_mp->b_cont; 3688 3689 oipha = (ipha_t *)mp->b_rptr; 3690 v_hlen_tos_len = ((uint32_t *)oipha)[0]; 3691 3692 /* mark packet as being accelerated in IPSEC_OUT */ 3693 ASSERT(oi->ipsec_out_accelerated == B_FALSE); 3694 oi->ipsec_out_accelerated = B_TRUE; 3695 3696 /* calculate authentication data length, i.e. ICV + padding */ 3697 ah_data_sz = assoc->ipsa_mac_len; 3698 ah_align_sz = (ah_data_sz + IPV4_PADDING_ALIGN - 1) & 3699 -IPV4_PADDING_ALIGN; 3700 3701 /* 3702 * Insert pseudo header: 3703 * IPSEC_INFO -> [IP, ULP] => IPSEC_INFO -> [IP, AH, ICV] -> ULP 3704 */ 3705 3706 /* IP + AH + authentication + padding data length */ 3707 new_hdr_len = IP_SIMPLE_HDR_LENGTH + sizeof (ah_t) + ah_align_sz; 3708 if (V_HLEN != IP_SIMPLE_HDR_VERSION) { 3709 option_length = oipha->ipha_version_and_hdr_length - 3710 (uint8_t)((IP_VERSION << 4) + 3711 IP_SIMPLE_HDR_LENGTH_IN_WORDS); 3712 option_length <<= 2; 3713 new_hdr_len += option_length; 3714 } 3715 3716 /* allocate pseudo-header mblk */ 3717 if ((new_mp = allocb(new_hdr_len, BPRI_HI)) == NULL) { 3718 /* IPsec kstats: bump bean counter here */ 3719 ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL, 3720 &ipdrops_ah_nomem, &ah_dropper); 3721 return (IPSEC_STATUS_FAILED); 3722 } 3723 3724 new_mp->b_cont = mp; 3725 ipsec_mp->b_cont = new_mp; 3726 new_mp->b_wptr += new_hdr_len; 3727 3728 /* copy original IP header to new header */ 3729 bcopy(mp->b_rptr, new_mp->b_rptr, IP_SIMPLE_HDR_LENGTH + 3730 option_length); 3731 3732 /* update IP header */ 3733 nipha = (ipha_t *)new_mp->b_rptr; 3734 nipha->ipha_protocol = IPPROTO_AH; 3735 iphdr_length = ntohs(nipha->ipha_length); 3736 iphdr_length += sizeof (ah_t) + ah_align_sz; 3737 nipha->ipha_length = htons(iphdr_length); 3738 nipha->ipha_hdr_checksum = 0; 3739 nipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(nipha); 3740 3741 /* skip original IP header in mp */ 3742 mp->b_rptr += IP_SIMPLE_HDR_LENGTH + option_length; 3743 3744 /* initialize AH header */ 3745 ah_hdr = (ah_t *)(new_mp->b_rptr + IP_SIMPLE_HDR_LENGTH + 3746 option_length); 3747 ah_hdr->ah_nexthdr = oipha->ipha_protocol; 3748 if (!ah_finish_up(ah_hdr, NULL, assoc, ah_data_sz, ah_align_sz)) { 3749 /* Only way this fails is if outbound replay counter wraps. */ 3750 ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL, 3751 &ipdrops_ah_replay, &ah_dropper); 3752 return (IPSEC_STATUS_FAILED); 3753 } 3754 3755 return (IPSEC_STATUS_SUCCESS); 3756 } 3757 3758 /* 3759 * ah_outbound_accelerated_v6: 3760 * 3761 * Called from ah_outbound_v6() once it is determined that the packet 3762 * is eligible for hardware acceleration. 3763 * 3764 * We proceed as follows: 3765 * 1. allocate and initialize attributes mblk 3766 * 2. mark IPSEC_OUT to indicate that pkt is accelerated 3767 * 3. insert AH header 3768 */ 3769 static ipsec_status_t 3770 ah_outbound_accelerated_v6(mblk_t *ipsec_mp, ipsa_t *assoc) 3771 { 3772 mblk_t *mp, *phdr_mp; 3773 ipsec_out_t *oi; 3774 uint_t ah_data_sz; /* ICV length, algorithm dependent */ 3775 uint_t ah_align_sz; /* ICV length + padding */ 3776 ip6_t *oip6h; /* original IP header */ 3777 ip6_t *ip6h; /* new IP header */ 3778 uint_t option_length = 0; 3779 uint_t hdr_size; 3780 uint_t ah_offset; 3781 ah_t *ah_hdr; /* ptr to AH header */ 3782 3783 AH_BUMP_STAT(out_accelerated); 3784 3785 oi = (ipsec_out_t *)ipsec_mp->b_rptr; 3786 mp = ipsec_mp->b_cont; 3787 3788 oip6h = (ip6_t *)mp->b_rptr; 3789 3790 /* mark packet as being accelerated in IPSEC_OUT */ 3791 ASSERT(oi->ipsec_out_accelerated == B_FALSE); 3792 oi->ipsec_out_accelerated = B_TRUE; 3793 3794 /* calculate authentication data length, i.e. ICV + padding */ 3795 ah_data_sz = assoc->ipsa_mac_len; 3796 ah_align_sz = (ah_data_sz + IPV4_PADDING_ALIGN - 1) & 3797 -IPV4_PADDING_ALIGN; 3798 3799 ASSERT(ah_align_sz >= ah_data_sz); 3800 3801 hdr_size = ipsec_ah_get_hdr_size_v6(mp, B_FALSE); 3802 option_length = hdr_size - IPV6_HDR_LEN; 3803 3804 /* This was not included in ipsec_ah_get_hdr_size_v6() */ 3805 hdr_size += (sizeof (ah_t) + ah_align_sz); 3806 3807 if ((phdr_mp = allocb(hdr_size, BPRI_HI)) == NULL) { 3808 ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL, &ipdrops_ah_nomem, 3809 &ah_dropper); 3810 return (IPSEC_STATUS_FAILED); 3811 } 3812 phdr_mp->b_wptr += hdr_size; 3813 3814 /* 3815 * Form the basic IP header first. We always assign every bit 3816 * of the v6 basic header, so a separate bzero is unneeded. 3817 */ 3818 ip6h = (ip6_t *)phdr_mp->b_rptr; 3819 ip6h->ip6_vcf = oip6h->ip6_vcf; 3820 ip6h->ip6_hlim = oip6h->ip6_hlim; 3821 ip6h->ip6_src = oip6h->ip6_src; 3822 ip6h->ip6_dst = oip6h->ip6_dst; 3823 /* 3824 * Include the size of AH and authentication data. 3825 * This is how our recipient would compute the 3826 * authentication data. Look at what we do in the 3827 * inbound case below. 3828 */ 3829 ip6h->ip6_plen = htons(ntohs(oip6h->ip6_plen) + sizeof (ah_t) + 3830 ah_align_sz); 3831 3832 /* 3833 * Insert pseudo header: 3834 * IPSEC_INFO -> [IP6, LLH, ULP] => 3835 * IPSEC_INFO -> [IP, LLH, AH, ICV] -> ULP 3836 */ 3837 3838 if (option_length == 0) { 3839 /* Form the AH header */ 3840 ip6h->ip6_nxt = IPPROTO_AH; 3841 ((ah_t *)(ip6h + 1))->ah_nexthdr = oip6h->ip6_nxt; 3842 ah_offset = IPV6_HDR_LEN; 3843 } else { 3844 ip6h->ip6_nxt = oip6h->ip6_nxt; 3845 /* option_length does not include the AH header's size */ 3846 ah_offset = ah_fix_phdr_v6(ip6h, oip6h, B_TRUE, B_FALSE); 3847 if (ah_offset == 0) { 3848 freemsg(phdr_mp); 3849 ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL, 3850 &ipdrops_ah_bad_v6_hdrs, &ah_dropper); 3851 return (IPSEC_STATUS_FAILED); 3852 } 3853 } 3854 3855 phdr_mp->b_cont = mp; 3856 ipsec_mp->b_cont = phdr_mp; 3857 3858 /* skip original IP header in mp */ 3859 mp->b_rptr += IPV6_HDR_LEN + option_length; 3860 3861 /* initialize AH header */ 3862 ah_hdr = (ah_t *)(phdr_mp->b_rptr + IPV6_HDR_LEN + option_length); 3863 ah_hdr->ah_nexthdr = oip6h->ip6_nxt; 3864 3865 if (!ah_finish_up(((ah_t *)((uint8_t *)ip6h + ah_offset)), NULL, 3866 assoc, ah_data_sz, ah_align_sz)) { 3867 /* Only way this fails is if outbound replay counter wraps. */ 3868 ip_drop_packet(ipsec_mp, B_FALSE, NULL, NULL, 3869 &ipdrops_ah_replay, &ah_dropper); 3870 return (IPSEC_STATUS_FAILED); 3871 } 3872 3873 return (IPSEC_STATUS_SUCCESS); 3874 } 3875 3876 /* 3877 * Invoked after processing of an inbound packet by the 3878 * kernel crypto framework. Called by ah_submit_req() for a sync request, 3879 * or by the kcf callback for an async request. 3880 * Returns IPSEC_STATUS_SUCCESS on success, IPSEC_STATUS_FAILED on failure. 3881 * On failure, the mblk chain ipsec_in is freed by this function. 3882 */ 3883 static ipsec_status_t 3884 ah_auth_in_done(mblk_t *ipsec_in) 3885 { 3886 mblk_t *phdr_mp; 3887 ipha_t *ipha; 3888 uint_t ah_offset = 0; 3889 mblk_t *mp; 3890 int align_len; 3891 ah_t *ah; 3892 ipha_t *nipha; 3893 uint32_t length; 3894 ipsec_in_t *ii; 3895 boolean_t isv4; 3896 ip6_t *ip6h; 3897 ip6_t *nip6h; 3898 uint_t icv_len; 3899 ipsa_t *assoc; 3900 kstat_named_t *counter; 3901 3902 ii = (ipsec_in_t *)ipsec_in->b_rptr; 3903 isv4 = ii->ipsec_in_v4; 3904 assoc = ii->ipsec_in_ah_sa; 3905 icv_len = (uint_t)ii->ipsec_in_crypto_mac.cd_raw.iov_len; 3906 3907 phdr_mp = ipsec_in->b_cont; 3908 if (phdr_mp == NULL) { 3909 ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, &ipdrops_ah_nomem, 3910 &ah_dropper); 3911 return (IPSEC_STATUS_FAILED); 3912 } 3913 3914 mp = phdr_mp->b_cont; 3915 if (mp == NULL) { 3916 ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, &ipdrops_ah_nomem, 3917 &ah_dropper); 3918 return (IPSEC_STATUS_FAILED); 3919 } 3920 mp->b_rptr -= ii->ipsec_in_skip_len; 3921 3922 if (isv4) { 3923 ipha = (ipha_t *)mp->b_rptr; 3924 ah_offset = ipha->ipha_version_and_hdr_length - 3925 (uint8_t)((IP_VERSION << 4)); 3926 ah_offset <<= 2; 3927 align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1, 3928 IPV4_PADDING_ALIGN); 3929 } else { 3930 ip6h = (ip6_t *)mp->b_rptr; 3931 ah_offset = ipsec_ah_get_hdr_size_v6(mp, B_TRUE); 3932 ASSERT((mp->b_wptr - mp->b_rptr) >= ah_offset); 3933 align_len = P2ALIGN(icv_len + IPV6_PADDING_ALIGN - 1, 3934 IPV6_PADDING_ALIGN); 3935 } 3936 3937 ah = (ah_t *)(mp->b_rptr + ah_offset); 3938 3939 /* 3940 * We get here only when authentication passed. 3941 */ 3942 3943 ah3dbg(("AH succeeded, checking replay\n")); 3944 AH_BUMP_STAT(good_auth); 3945 3946 if (!sadb_replay_check(assoc, ah->ah_replay)) { 3947 int af; 3948 void *addr; 3949 3950 if (isv4) { 3951 addr = &ipha->ipha_dst; 3952 af = AF_INET; 3953 } else { 3954 addr = &ip6h->ip6_dst; 3955 af = AF_INET6; 3956 } 3957 3958 /* 3959 * Log the event. As of now we print out an event. 3960 * Do not print the replay failure number, or else 3961 * syslog cannot collate the error messages. Printing 3962 * the replay number that failed (or printing to the 3963 * console) opens a denial-of-service attack. 3964 */ 3965 AH_BUMP_STAT(replay_failures); 3966 ipsec_assocfailure(info.mi_idnum, 0, 0, 3967 SL_ERROR | SL_WARN, 3968 "Replay failed for AH spi %x, dst_addr %s", 3969 assoc->ipsa_spi, addr, af); 3970 counter = &ipdrops_ah_replay; 3971 goto ah_in_discard; 3972 } 3973 3974 /* 3975 * We need to remove the AH header from the original 3976 * datagram. Easy way to do this is to use phdr_mp 3977 * to hold the IP header and the orginal mp to hold 3978 * the rest of it. So, we copy the IP header on to 3979 * phdr_mp, and set the b_rptr in mp past AH header. 3980 */ 3981 if (isv4) { 3982 bcopy(mp->b_rptr, phdr_mp->b_rptr, ah_offset); 3983 phdr_mp->b_wptr = phdr_mp->b_rptr + ah_offset; 3984 nipha = (ipha_t *)phdr_mp->b_rptr; 3985 /* 3986 * Assign the right protocol, adjust the length as we 3987 * are removing the AH header and adjust the checksum to 3988 * account for the protocol and length. 3989 */ 3990 nipha->ipha_protocol = ah->ah_nexthdr; 3991 length = ntohs(nipha->ipha_length); 3992 if (!ah_age_bytes(assoc, length, B_TRUE)) { 3993 /* The ipsa has hit hard expiration, LOG and AUDIT. */ 3994 ipsec_assocfailure(info.mi_idnum, 0, 0, 3995 SL_ERROR | SL_WARN, 3996 "AH Association 0x%x, dst %s had bytes expire.\n", 3997 assoc->ipsa_spi, assoc->ipsa_dstaddr, 3998 AF_INET); 3999 AH_BUMP_STAT(bytes_expired); 4000 counter = &ipdrops_ah_bytes_expire; 4001 goto ah_in_discard; 4002 } 4003 length -= (sizeof (ah_t) + align_len); 4004 4005 nipha->ipha_length = htons((uint16_t)length); 4006 nipha->ipha_hdr_checksum = 0; 4007 nipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(nipha); 4008 /* 4009 * Skip IP,AH and the authentication data in the 4010 * original datagram. 4011 */ 4012 mp->b_rptr += (ah_offset + sizeof (ah_t) + align_len); 4013 } else { 4014 uchar_t *whereptr; 4015 int hdrlen; 4016 uint8_t *nexthdr; 4017 ip6_hbh_t *hbhhdr; 4018 ip6_dest_t *dsthdr; 4019 ip6_rthdr0_t *rthdr; 4020 4021 nip6h = (ip6_t *)phdr_mp->b_rptr; 4022 4023 /* 4024 * Make phdr_mp hold until the AH header and make 4025 * mp hold everything past AH header. 4026 */ 4027 length = ntohs(nip6h->ip6_plen); 4028 if (!ah_age_bytes(assoc, length + sizeof (ip6_t), B_TRUE)) { 4029 /* The ipsa has hit hard expiration, LOG and AUDIT. */ 4030 ipsec_assocfailure(info.mi_idnum, 0, 0, 4031 SL_ERROR | SL_WARN, 4032 "AH Association 0x%x, dst %s had bytes " 4033 "expire.\n", assoc->ipsa_spi, &ip6h->ip6_dst, 4034 AF_INET6); 4035 AH_BUMP_STAT(bytes_expired); 4036 counter = &ipdrops_ah_bytes_expire; 4037 goto ah_in_discard; 4038 } 4039 bcopy(ip6h, nip6h, ah_offset); 4040 phdr_mp->b_wptr = phdr_mp->b_rptr + ah_offset; 4041 mp->b_rptr += (ah_offset + sizeof (ah_t) + align_len); 4042 4043 /* 4044 * Update the next header field of the header preceding 4045 * AH with the next header field of AH. Start with the 4046 * IPv6 header and proceed with the extension headers 4047 * until we find what we're looking for. 4048 */ 4049 nexthdr = &nip6h->ip6_nxt; 4050 whereptr = (uchar_t *)nip6h; 4051 hdrlen = sizeof (ip6_t); 4052 4053 while (*nexthdr != IPPROTO_AH) { 4054 whereptr += hdrlen; 4055 /* Assume IP has already stripped it */ 4056 ASSERT(*nexthdr != IPPROTO_FRAGMENT && 4057 *nexthdr != IPPROTO_RAW); 4058 switch (*nexthdr) { 4059 case IPPROTO_HOPOPTS: 4060 hbhhdr = (ip6_hbh_t *)whereptr; 4061 nexthdr = &hbhhdr->ip6h_nxt; 4062 hdrlen = 8 * (hbhhdr->ip6h_len + 1); 4063 break; 4064 case IPPROTO_DSTOPTS: 4065 dsthdr = (ip6_dest_t *)whereptr; 4066 nexthdr = &dsthdr->ip6d_nxt; 4067 hdrlen = 8 * (dsthdr->ip6d_len + 1); 4068 break; 4069 case IPPROTO_ROUTING: 4070 rthdr = (ip6_rthdr0_t *)whereptr; 4071 nexthdr = &rthdr->ip6r0_nxt; 4072 hdrlen = 8 * (rthdr->ip6r0_len + 1); 4073 break; 4074 } 4075 } 4076 *nexthdr = ah->ah_nexthdr; 4077 4078 length -= (sizeof (ah_t) + align_len); 4079 nip6h->ip6_plen = htons((uint16_t)length); 4080 } 4081 4082 return (IPSEC_STATUS_SUCCESS); 4083 4084 ah_in_discard: 4085 IP_AH_BUMP_STAT(in_discards); 4086 ip_drop_packet(ipsec_in, B_TRUE, NULL, NULL, counter, &ah_dropper); 4087 return (IPSEC_STATUS_FAILED); 4088 } 4089 4090 /* 4091 * Invoked after processing of an outbound packet by the 4092 * kernel crypto framework, either by ah_submit_req() for a request 4093 * executed syncrhonously, or by the KEF callback for a request 4094 * executed asynchronously. 4095 */ 4096 static ipsec_status_t 4097 ah_auth_out_done(mblk_t *ipsec_out) 4098 { 4099 mblk_t *phdr_mp; 4100 mblk_t *mp; 4101 int align_len; 4102 uint32_t hdrs_length; 4103 uchar_t *ptr; 4104 uint32_t length; 4105 boolean_t isv4; 4106 ipsec_out_t *io; 4107 size_t icv_len; 4108 4109 io = (ipsec_out_t *)ipsec_out->b_rptr; 4110 isv4 = io->ipsec_out_v4; 4111 icv_len = io->ipsec_out_crypto_mac.cd_raw.iov_len; 4112 4113 phdr_mp = ipsec_out->b_cont; 4114 if (phdr_mp == NULL) { 4115 ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL, 4116 &ipdrops_ah_nomem, &ah_dropper); 4117 return (IPSEC_STATUS_FAILED); 4118 } 4119 4120 mp = phdr_mp->b_cont; 4121 if (mp == NULL) { 4122 ip_drop_packet(ipsec_out, B_FALSE, NULL, NULL, 4123 &ipdrops_ah_nomem, &ah_dropper); 4124 return (IPSEC_STATUS_FAILED); 4125 } 4126 mp->b_rptr -= io->ipsec_out_skip_len; 4127 4128 if (isv4) { 4129 ipha_t *ipha; 4130 ipha_t *nipha; 4131 4132 ipha = (ipha_t *)mp->b_rptr; 4133 hdrs_length = ipha->ipha_version_and_hdr_length - 4134 (uint8_t)((IP_VERSION << 4)); 4135 hdrs_length <<= 2; 4136 align_len = P2ALIGN(icv_len + IPV4_PADDING_ALIGN - 1, 4137 IPV4_PADDING_ALIGN); 4138 /* 4139 * phdr_mp must have the right amount of space for the 4140 * combined IP and AH header. Copy the IP header and 4141 * the ack_data onto AH. Note that the AH header was 4142 * already formed before the ICV calculation and hence 4143 * you don't have to copy it here. 4144 */ 4145 bcopy(mp->b_rptr, phdr_mp->b_rptr, hdrs_length); 4146 4147 ptr = phdr_mp->b_rptr + hdrs_length + sizeof (ah_t); 4148 bcopy(phdr_mp->b_wptr, ptr, icv_len); 4149 4150 /* 4151 * Compute the new header checksum as we are assigning 4152 * IPPROTO_AH and adjusting the length here. 4153 */ 4154 nipha = (ipha_t *)phdr_mp->b_rptr; 4155 4156 nipha->ipha_protocol = IPPROTO_AH; 4157 length = ntohs(nipha->ipha_length); 4158 length += (sizeof (ah_t) + align_len); 4159 nipha->ipha_length = htons((uint16_t)length); 4160 nipha->ipha_hdr_checksum = 0; 4161 nipha->ipha_hdr_checksum = (uint16_t)ip_csum_hdr(nipha); 4162 } else { 4163 ip6_t *ip6h; 4164 ip6_t *nip6h; 4165 uint_t ah_offset; 4166 4167 ip6h = (ip6_t *)mp->b_rptr; 4168 nip6h = (ip6_t *)phdr_mp->b_rptr; 4169 align_len = P2ALIGN(icv_len + IPV6_PADDING_ALIGN - 1, 4170 IPV6_PADDING_ALIGN); 4171 /* 4172 * phdr_mp must have the right amount of space for the 4173 * combined IP and AH header. Copy the IP header with 4174 * options into the pseudo header. When we constructed 4175 * a pseudo header, we did not copy some of the mutable 4176 * fields. We do it now by calling ah_fix_phdr_v6() 4177 * with the last argument B_TRUE. It returns the 4178 * ah_offset into the pseudo header. 4179 */ 4180 4181 bcopy(ip6h, nip6h, IPV6_HDR_LEN); 4182 ah_offset = ah_fix_phdr_v6(nip6h, ip6h, B_TRUE, B_TRUE); 4183 ASSERT(ah_offset != 0); 4184 /* 4185 * phdr_mp can hold exactly the whole IP header with options 4186 * plus the AH header also. Thus subtracting the AH header's 4187 * size should give exactly how much of the original header 4188 * should be skipped. 4189 */ 4190 hdrs_length = (phdr_mp->b_wptr - phdr_mp->b_rptr) - 4191 sizeof (ah_t) - icv_len; 4192 bcopy(phdr_mp->b_wptr, ((uint8_t *)nip6h + ah_offset + 4193 sizeof (ah_t)), icv_len); 4194 length = ntohs(nip6h->ip6_plen); 4195 length += (sizeof (ah_t) + align_len); 4196 nip6h->ip6_plen = htons((uint16_t)length); 4197 } 4198 4199 /* Skip the original IP header */ 4200 mp->b_rptr += hdrs_length; 4201 if (mp->b_rptr == mp->b_wptr) { 4202 phdr_mp->b_cont = mp->b_cont; 4203 freeb(mp); 4204 } 4205 4206 return (IPSEC_STATUS_SUCCESS); 4207 } 4208 4209 /* 4210 * Wrapper to allow IP to trigger an AH strlog message if an error 4211 * condition is found during SA selection. 4212 */ 4213 void 4214 ipsecah_rl_strlog(char level, ushort_t sl, char *fmt, ...) 4215 { 4216 va_list adx; 4217 4218 va_start(adx, fmt); 4219 ipsec_rl_strlog(info.mi_idnum, 0, level, sl, fmt, adx); 4220 va_end(adx); 4221 } 4222 4223 /* 4224 * Wrapper to allow IP to trigger an AH association failure message 4225 * during SA inbound selection. 4226 */ 4227 void 4228 ipsecah_in_assocfailure(mblk_t *mp, char level, ushort_t sl, char *fmt, 4229 uint32_t spi, void *addr, int af) 4230 { 4231 if (ipsecah_log_unknown_spi) { 4232 ipsec_assocfailure(info.mi_idnum, 0, level, sl, fmt, spi, 4233 addr, af); 4234 } 4235 4236 ip_drop_packet(mp, B_TRUE, NULL, NULL, &ipdrops_ah_no_sa, 4237 &ah_dropper); 4238 } 4239 4240 /* 4241 * Initialize the AH input and output processing functions. 4242 */ 4243 void 4244 ipsecah_init_funcs(ipsa_t *sa) 4245 { 4246 if (sa->ipsa_output_func == NULL) 4247 sa->ipsa_output_func = ah_outbound; 4248 if (sa->ipsa_input_func == NULL) 4249 sa->ipsa_input_func = ah_inbound; 4250 } 4251