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