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 29 #include <sys/param.h> 30 #include <sys/types.h> 31 #include <sys/stream.h> 32 #include <sys/strsubr.h> 33 #include <sys/strsun.h> 34 #include <sys/stropts.h> 35 #include <sys/vnode.h> 36 #include <sys/zone.h> 37 #include <sys/strlog.h> 38 #include <sys/sysmacros.h> 39 #define _SUN_TPI_VERSION 2 40 #include <sys/tihdr.h> 41 #include <sys/timod.h> 42 #include <sys/tiuser.h> 43 #include <sys/ddi.h> 44 #include <sys/sunddi.h> 45 #include <sys/sunldi.h> 46 #include <sys/file.h> 47 #include <sys/modctl.h> 48 #include <sys/debug.h> 49 #include <sys/kmem.h> 50 #include <sys/cmn_err.h> 51 #include <sys/proc.h> 52 #include <sys/suntpi.h> 53 #include <sys/atomic.h> 54 #include <sys/mkdev.h> 55 #include <sys/policy.h> 56 #include <sys/disp.h> 57 58 #include <sys/socket.h> 59 #include <netinet/in.h> 60 #include <net/pfkeyv2.h> 61 62 #include <inet/common.h> 63 #include <netinet/ip6.h> 64 #include <inet/ip.h> 65 #include <inet/mi.h> 66 #include <inet/nd.h> 67 #include <inet/optcom.h> 68 #include <inet/ipsec_info.h> 69 #include <inet/ipsec_impl.h> 70 #include <inet/keysock.h> 71 72 #include <sys/isa_defs.h> 73 74 /* 75 * This is a transport provider for the PF_KEY key mangement socket. 76 * (See RFC 2367 for details.) 77 * Downstream messages are wrapped in a keysock consumer interface KEYSOCK_IN 78 * messages (see ipsec_info.h), and passed to the appropriate consumer. 79 * Upstream messages are generated for all open PF_KEY sockets, when 80 * appropriate, as well as the sender (as long as SO_USELOOPBACK is enabled) 81 * in reply to downstream messages. 82 * 83 * Upstream messages must be created asynchronously for the following 84 * situations: 85 * 86 * 1.) A keysock consumer requires an SA, and there is currently none. 87 * 2.) An SA expires, either hard or soft lifetime. 88 * 3.) Other events a consumer deems fit. 89 * 90 * The MT model of this is PERMOD, with shared put procedures. Two types of 91 * messages, SADB_FLUSH and SADB_DUMP, need to lock down the perimeter to send 92 * down the *multiple* messages they create. 93 */ 94 95 static vmem_t *keysock_vmem; /* for minor numbers. */ 96 97 #define KEYSOCK_MAX_CONSUMERS 256 98 99 /* Default structure copied into T_INFO_ACK messages (from rts.c...) */ 100 static struct T_info_ack keysock_g_t_info_ack = { 101 T_INFO_ACK, 102 T_INFINITE, /* TSDU_size. Maximum size messages. */ 103 T_INVALID, /* ETSDU_size. No expedited data. */ 104 T_INVALID, /* CDATA_size. No connect data. */ 105 T_INVALID, /* DDATA_size. No disconnect data. */ 106 0, /* ADDR_size. */ 107 0, /* OPT_size. No user-settable options */ 108 64 * 1024, /* TIDU_size. keysock allows maximum size messages. */ 109 T_COTS, /* SERV_type. keysock supports connection oriented. */ 110 TS_UNBND, /* CURRENT_state. This is set from keysock_state. */ 111 (XPG4_1) /* Provider flags */ 112 }; 113 114 /* Named Dispatch Parameter Management Structure */ 115 typedef struct keysockparam_s { 116 uint_t keysock_param_min; 117 uint_t keysock_param_max; 118 uint_t keysock_param_value; 119 char *keysock_param_name; 120 } keysockparam_t; 121 122 /* 123 * Table of NDD variables supported by keysock. These are loaded into 124 * keysock_g_nd in keysock_init_nd. 125 * All of these are alterable, within the min/max values given, at run time. 126 */ 127 static keysockparam_t lcl_param_arr[] = { 128 /* min max value name */ 129 { 4096, 65536, 8192, "keysock_xmit_hiwat"}, 130 { 0, 65536, 1024, "keysock_xmit_lowat"}, 131 { 4096, 65536, 8192, "keysock_recv_hiwat"}, 132 { 65536, 1024*1024*1024, 256*1024, "keysock_max_buf"}, 133 { 0, 3, 0, "keysock_debug"}, 134 }; 135 #define keystack_xmit_hiwat keystack_params[0].keysock_param_value 136 #define keystack_xmit_lowat keystack_params[1].keysock_param_value 137 #define keystack_recv_hiwat keystack_params[2].keysock_param_value 138 #define keystack_max_buf keystack_params[3].keysock_param_value 139 #define keystack_debug keystack_params[4].keysock_param_value 140 141 #define ks0dbg(a) printf a 142 /* NOTE: != 0 instead of > 0 so lint doesn't complain. */ 143 #define ks1dbg(keystack, a) if (keystack->keystack_debug != 0) printf a 144 #define ks2dbg(keystack, a) if (keystack->keystack_debug > 1) printf a 145 #define ks3dbg(keystack, a) if (keystack->keystack_debug > 2) printf a 146 147 static int keysock_close(queue_t *); 148 static int keysock_open(queue_t *, dev_t *, int, int, cred_t *); 149 static void keysock_wput(queue_t *, mblk_t *); 150 static void keysock_rput(queue_t *, mblk_t *); 151 static void keysock_rsrv(queue_t *); 152 static void keysock_passup(mblk_t *, sadb_msg_t *, minor_t, 153 keysock_consumer_t *, boolean_t, keysock_stack_t *); 154 static void *keysock_stack_init(netstackid_t stackid, netstack_t *ns); 155 static void keysock_stack_fini(netstackid_t stackid, void *arg); 156 157 static struct module_info info = { 158 5138, "keysock", 1, INFPSZ, 512, 128 159 }; 160 161 static struct qinit rinit = { 162 (pfi_t)keysock_rput, (pfi_t)keysock_rsrv, keysock_open, keysock_close, 163 NULL, &info 164 }; 165 166 static struct qinit winit = { 167 (pfi_t)keysock_wput, NULL, NULL, NULL, NULL, &info 168 }; 169 170 struct streamtab keysockinfo = { 171 &rinit, &winit 172 }; 173 174 extern struct modlinkage *keysock_modlp; 175 176 /* 177 * Plumb IPsec. 178 * 179 * NOTE: New "default" modules will need to be loaded here if needed before 180 * boot time. 181 */ 182 183 /* Keep these in global space to keep the lint from complaining. */ 184 static char *IPSECESP = "ipsecesp"; 185 static char *IPSECESPDEV = "/devices/pseudo/ipsecesp@0:ipsecesp"; 186 static char *IPSECAH = "ipsecah"; 187 static char *IPSECAHDEV = "/devices/pseudo/ipsecah@0:ipsecah"; 188 static char *IP6DEV = "/devices/pseudo/ip6@0:ip6"; 189 static char *KEYSOCK = "keysock"; 190 static char *STRMOD = "strmod"; 191 192 /* 193 * Load the other ipsec modules and plumb them together. 194 */ 195 int 196 keysock_plumb_ipsec(netstack_t *ns) 197 { 198 ldi_handle_t lh, ip6_lh = NULL; 199 ldi_ident_t li = NULL; 200 int err = 0; 201 int muxid, rval; 202 boolean_t esp_present = B_TRUE; 203 cred_t *cr; 204 keysock_stack_t *keystack = ns->netstack_keysock; 205 206 #ifdef NS_DEBUG 207 (void) printf("keysock_plumb_ipsec(%d)\n", 208 ns->netstack_stackid); 209 #endif 210 211 keystack->keystack_plumbed = 0; /* we're trying again.. */ 212 213 cr = zone_get_kcred(netstackid_to_zoneid( 214 keystack->keystack_netstack->netstack_stackid)); 215 ASSERT(cr != NULL); 216 /* 217 * Load up the drivers (AH/ESP). 218 * 219 * I do this separately from the actual plumbing in case this function 220 * ever gets called from a diskless boot before the root filesystem is 221 * up. I don't have to worry about "keysock" because, well, if I'm 222 * here, keysock must've loaded successfully. 223 */ 224 if (i_ddi_attach_pseudo_node(IPSECAH) == NULL) { 225 ks0dbg(("IPsec: AH failed to attach.\n")); 226 goto bail; 227 } 228 if (i_ddi_attach_pseudo_node(IPSECESP) == NULL) { 229 ks0dbg(("IPsec: ESP failed to attach.\n")); 230 esp_present = B_FALSE; 231 } 232 233 /* 234 * Set up the IP streams for AH and ESP, as well as tacking keysock 235 * on top of them. Assume keysock has set the autopushes up already. 236 */ 237 238 /* Open IP. */ 239 err = ldi_ident_from_mod(keysock_modlp, &li); 240 if (err) { 241 ks0dbg(("IPsec: lid_ident_from_mod failed (err %d).\n", 242 err)); 243 goto bail; 244 } 245 246 err = ldi_open_by_name(IP6DEV, FREAD|FWRITE, cr, &ip6_lh, li); 247 if (err) { 248 ks0dbg(("IPsec: Open of IP6 failed (err %d).\n", err)); 249 goto bail; 250 } 251 252 /* PLINK KEYSOCK/AH */ 253 err = ldi_open_by_name(IPSECAHDEV, FREAD|FWRITE, cr, &lh, li); 254 if (err) { 255 ks0dbg(("IPsec: Open of AH failed (err %d).\n", err)); 256 goto bail; 257 } 258 err = ldi_ioctl(lh, 259 I_PUSH, (intptr_t)KEYSOCK, FKIOCTL, cr, &rval); 260 if (err) { 261 ks0dbg(("IPsec: Push of KEYSOCK onto AH failed (err %d).\n", 262 err)); 263 (void) ldi_close(lh, FREAD|FWRITE, cr); 264 goto bail; 265 } 266 err = ldi_ioctl(ip6_lh, I_PLINK, (intptr_t)lh, 267 FREAD+FWRITE+FNOCTTY+FKIOCTL, cr, &muxid); 268 if (err) { 269 ks0dbg(("IPsec: PLINK of KEYSOCK/AH failed (err %d).\n", err)); 270 (void) ldi_close(lh, FREAD|FWRITE, cr); 271 goto bail; 272 } 273 (void) ldi_close(lh, FREAD|FWRITE, cr); 274 275 /* PLINK KEYSOCK/ESP */ 276 if (esp_present) { 277 err = ldi_open_by_name(IPSECESPDEV, 278 FREAD|FWRITE, cr, &lh, li); 279 if (err) { 280 ks0dbg(("IPsec: Open of ESP failed (err %d).\n", err)); 281 goto bail; 282 } 283 err = ldi_ioctl(lh, 284 I_PUSH, (intptr_t)KEYSOCK, FKIOCTL, cr, &rval); 285 if (err) { 286 ks0dbg(("IPsec: " 287 "Push of KEYSOCK onto ESP failed (err %d).\n", 288 err)); 289 (void) ldi_close(lh, FREAD|FWRITE, cr); 290 goto bail; 291 } 292 err = ldi_ioctl(ip6_lh, I_PLINK, (intptr_t)lh, 293 FREAD+FWRITE+FNOCTTY+FKIOCTL, cr, &muxid); 294 if (err) { 295 ks0dbg(("IPsec: " 296 "PLINK of KEYSOCK/ESP failed (err %d).\n", err)); 297 (void) ldi_close(lh, FREAD|FWRITE, cr); 298 goto bail; 299 } 300 (void) ldi_close(lh, FREAD|FWRITE, cr); 301 } 302 303 bail: 304 keystack->keystack_plumbed = (err == 0) ? 1 : -1; 305 if (ip6_lh != NULL) { 306 (void) ldi_close(ip6_lh, FREAD|FWRITE, cr); 307 } 308 if (li != NULL) 309 ldi_ident_release(li); 310 #ifdef NS_DEBUG 311 (void) printf("keysock_plumb_ipsec -> %d\n", 312 keystack->keystack_plumbed); 313 #endif 314 crfree(cr); 315 return (err); 316 } 317 318 /* ARGSUSED */ 319 static int 320 keysock_param_get(q, mp, cp, cr) 321 queue_t *q; 322 mblk_t *mp; 323 caddr_t cp; 324 cred_t *cr; 325 { 326 keysockparam_t *keysockpa = (keysockparam_t *)cp; 327 uint_t value; 328 keysock_t *ks = (keysock_t *)q->q_ptr; 329 keysock_stack_t *keystack = ks->keysock_keystack; 330 331 mutex_enter(&keystack->keystack_param_lock); 332 value = keysockpa->keysock_param_value; 333 mutex_exit(&keystack->keystack_param_lock); 334 335 (void) mi_mpprintf(mp, "%u", value); 336 return (0); 337 } 338 339 /* This routine sets an NDD variable in a keysockparam_t structure. */ 340 /* ARGSUSED */ 341 static int 342 keysock_param_set(q, mp, value, cp, cr) 343 queue_t *q; 344 mblk_t *mp; 345 char *value; 346 caddr_t cp; 347 cred_t *cr; 348 { 349 ulong_t new_value; 350 keysockparam_t *keysockpa = (keysockparam_t *)cp; 351 keysock_t *ks = (keysock_t *)q->q_ptr; 352 keysock_stack_t *keystack = ks->keysock_keystack; 353 354 /* Convert the value from a string into a long integer. */ 355 if (ddi_strtoul(value, NULL, 10, &new_value) != 0) 356 return (EINVAL); 357 358 mutex_enter(&keystack->keystack_param_lock); 359 /* 360 * Fail the request if the new value does not lie within the 361 * required bounds. 362 */ 363 if (new_value < keysockpa->keysock_param_min || 364 new_value > keysockpa->keysock_param_max) { 365 mutex_exit(&keystack->keystack_param_lock); 366 return (EINVAL); 367 } 368 369 /* Set the new value */ 370 keysockpa->keysock_param_value = new_value; 371 mutex_exit(&keystack->keystack_param_lock); 372 373 return (0); 374 } 375 376 /* 377 * Initialize keysock at module load time 378 */ 379 boolean_t 380 keysock_ddi_init(void) 381 { 382 keysock_max_optsize = optcom_max_optsize( 383 keysock_opt_obj.odb_opt_des_arr, keysock_opt_obj.odb_opt_arr_cnt); 384 385 keysock_vmem = vmem_create("keysock", (void *)1, MAXMIN, 1, 386 NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER); 387 388 /* 389 * We want to be informed each time a stack is created or 390 * destroyed in the kernel, so we can maintain the 391 * set of keysock_stack_t's. 392 */ 393 netstack_register(NS_KEYSOCK, keysock_stack_init, NULL, 394 keysock_stack_fini); 395 396 return (B_TRUE); 397 } 398 399 /* 400 * Walk through the param array specified registering each element with the 401 * named dispatch handler. 402 */ 403 static boolean_t 404 keysock_param_register(IDP *ndp, keysockparam_t *ksp, int cnt) 405 { 406 for (; cnt-- > 0; ksp++) { 407 if (ksp->keysock_param_name != NULL && 408 ksp->keysock_param_name[0]) { 409 if (!nd_load(ndp, 410 ksp->keysock_param_name, 411 keysock_param_get, keysock_param_set, 412 (caddr_t)ksp)) { 413 nd_free(ndp); 414 return (B_FALSE); 415 } 416 } 417 } 418 return (B_TRUE); 419 } 420 421 /* 422 * Initialize keysock for one stack instance 423 */ 424 /* ARGSUSED */ 425 static void * 426 keysock_stack_init(netstackid_t stackid, netstack_t *ns) 427 { 428 keysock_stack_t *keystack; 429 keysockparam_t *ksp; 430 431 keystack = (keysock_stack_t *)kmem_zalloc(sizeof (*keystack), KM_SLEEP); 432 keystack->keystack_netstack = ns; 433 434 keystack->keystack_acquire_seq = 0xffffffff; 435 436 ksp = (keysockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP); 437 keystack->keystack_params = ksp; 438 bcopy(lcl_param_arr, ksp, sizeof (lcl_param_arr)); 439 440 (void) keysock_param_register(&keystack->keystack_g_nd, ksp, 441 A_CNT(lcl_param_arr)); 442 443 mutex_init(&keystack->keystack_list_lock, NULL, MUTEX_DEFAULT, NULL); 444 mutex_init(&keystack->keystack_consumers_lock, 445 NULL, MUTEX_DEFAULT, NULL); 446 mutex_init(&keystack->keystack_param_lock, NULL, MUTEX_DEFAULT, NULL); 447 return (keystack); 448 } 449 450 /* 451 * Free NDD variable space, and other destructors, for keysock. 452 */ 453 void 454 keysock_ddi_destroy(void) 455 { 456 netstack_unregister(NS_KEYSOCK); 457 vmem_destroy(keysock_vmem); 458 } 459 460 /* 461 * Remove one stack instance from keysock 462 */ 463 /* ARGSUSED */ 464 static void 465 keysock_stack_fini(netstackid_t stackid, void *arg) 466 { 467 keysock_stack_t *keystack = (keysock_stack_t *)arg; 468 469 nd_free(&keystack->keystack_g_nd); 470 kmem_free(keystack->keystack_params, sizeof (lcl_param_arr)); 471 keystack->keystack_params = NULL; 472 473 mutex_destroy(&keystack->keystack_list_lock); 474 mutex_destroy(&keystack->keystack_consumers_lock); 475 mutex_destroy(&keystack->keystack_param_lock); 476 477 kmem_free(keystack, sizeof (*keystack)); 478 } 479 480 /* 481 * Close routine for keysock. 482 */ 483 static int 484 keysock_close(queue_t *q) 485 { 486 keysock_t *ks; 487 keysock_consumer_t *kc; 488 void *ptr = q->q_ptr; 489 int size; 490 keysock_stack_t *keystack; 491 492 493 qprocsoff(q); 494 495 /* Safe assumption. */ 496 ASSERT(ptr != NULL); 497 498 if (WR(q)->q_next) { 499 kc = (keysock_consumer_t *)ptr; 500 keystack = kc->kc_keystack; 501 502 ks1dbg(keystack, ("Module close, removing a consumer (%d).\n", 503 kc->kc_sa_type)); 504 /* 505 * Because of PERMOD open/close exclusive perimeter, I 506 * can inspect KC_FLUSHING w/o locking down kc->kc_lock. 507 */ 508 if (kc->kc_flags & KC_FLUSHING) { 509 /* 510 * If this decrement was the last one, send 511 * down the next pending one, if any. 512 * 513 * With a PERMOD perimeter, the mutexes ops aren't 514 * really necessary, but if we ever loosen up, we will 515 * have this bit covered already. 516 */ 517 keystack->keystack_flushdump--; 518 if (keystack->keystack_flushdump == 0) { 519 /* 520 * The flush/dump terminated by having a 521 * consumer go away. I need to send up to the 522 * appropriate keysock all of the relevant 523 * information. Unfortunately, I don't 524 * have that handy. 525 */ 526 ks0dbg(("Consumer went away while flushing or" 527 " dumping.\n")); 528 } 529 } 530 size = sizeof (keysock_consumer_t); 531 mutex_enter(&keystack->keystack_consumers_lock); 532 keystack->keystack_consumers[kc->kc_sa_type] = NULL; 533 mutex_exit(&keystack->keystack_consumers_lock); 534 mutex_destroy(&kc->kc_lock); 535 netstack_rele(kc->kc_keystack->keystack_netstack); 536 } else { 537 ks = (keysock_t *)ptr; 538 keystack = ks->keysock_keystack; 539 540 ks3dbg(keystack, 541 ("Driver close, PF_KEY socket is going away.\n")); 542 if ((ks->keysock_flags & KEYSOCK_EXTENDED) != 0) 543 atomic_add_32(&keystack->keystack_num_extended, -1); 544 size = sizeof (keysock_t); 545 mutex_enter(&keystack->keystack_list_lock); 546 *(ks->keysock_ptpn) = ks->keysock_next; 547 if (ks->keysock_next != NULL) 548 ks->keysock_next->keysock_ptpn = ks->keysock_ptpn; 549 mutex_exit(&keystack->keystack_list_lock); 550 mutex_destroy(&ks->keysock_lock); 551 vmem_free(keysock_vmem, (void *)(uintptr_t)ks->keysock_serial, 552 1); 553 netstack_rele(ks->keysock_keystack->keystack_netstack); 554 } 555 556 /* Now I'm free. */ 557 kmem_free(ptr, size); 558 return (0); 559 } 560 /* 561 * Open routine for keysock. 562 */ 563 /* ARGSUSED */ 564 static int 565 keysock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 566 { 567 keysock_t *ks; 568 keysock_consumer_t *kc; 569 mblk_t *mp; 570 ipsec_info_t *ii; 571 netstack_t *ns; 572 keysock_stack_t *keystack; 573 574 if (secpolicy_ip_config(credp, B_FALSE) != 0) { 575 /* Privilege debugging will log the error */ 576 return (EPERM); 577 } 578 579 if (q->q_ptr != NULL) 580 return (0); /* Re-open of an already open instance. */ 581 582 ns = netstack_find_by_cred(credp); 583 ASSERT(ns != NULL); 584 keystack = ns->netstack_keysock; 585 ASSERT(keystack != NULL); 586 587 ks3dbg(keystack, ("Entering keysock open.\n")); 588 589 if (keystack->keystack_plumbed < 1) { 590 netstack_t *ns = keystack->keystack_netstack; 591 592 keystack->keystack_plumbed = 0; 593 #ifdef NS_DEBUG 594 printf("keysock_open(%d) - plumb\n", 595 keystack->keystack_netstack->netstack_stackid); 596 #endif 597 /* 598 * Don't worry about ipsec_failure being true here. 599 * (See ip.c). An open of keysock should try and force 600 * the issue. Maybe it was a transient failure. 601 */ 602 ipsec_loader_loadnow(ns->netstack_ipsec); 603 } 604 605 if (sflag & MODOPEN) { 606 /* Initialize keysock_consumer state here. */ 607 kc = kmem_zalloc(sizeof (keysock_consumer_t), KM_NOSLEEP); 608 if (kc == NULL) { 609 netstack_rele(keystack->keystack_netstack); 610 return (ENOMEM); 611 } 612 mutex_init(&kc->kc_lock, NULL, MUTEX_DEFAULT, 0); 613 kc->kc_rq = q; 614 kc->kc_wq = WR(q); 615 616 q->q_ptr = kc; 617 WR(q)->q_ptr = kc; 618 619 kc->kc_keystack = keystack; 620 qprocson(q); 621 622 /* 623 * Send down initial message to whatever I was pushed on top 624 * of asking for its consumer type. The reply will set it. 625 */ 626 627 /* Allocate it. */ 628 mp = allocb(sizeof (ipsec_info_t), BPRI_HI); 629 if (mp == NULL) { 630 ks1dbg(keystack, ( 631 "keysock_open: Cannot allocate KEYSOCK_HELLO.\n")); 632 /* Do I need to set these to null? */ 633 q->q_ptr = NULL; 634 WR(q)->q_ptr = NULL; 635 mutex_destroy(&kc->kc_lock); 636 kmem_free(kc, sizeof (*kc)); 637 netstack_rele(keystack->keystack_netstack); 638 return (ENOMEM); 639 } 640 641 /* If I allocated okay, putnext to what I was pushed atop. */ 642 mp->b_wptr += sizeof (ipsec_info_t); 643 mp->b_datap->db_type = M_CTL; 644 ii = (ipsec_info_t *)mp->b_rptr; 645 ii->ipsec_info_type = KEYSOCK_HELLO; 646 /* Length only of type/len. */ 647 ii->ipsec_info_len = sizeof (ii->ipsec_allu); 648 ks2dbg(keystack, ("Ready to putnext KEYSOCK_HELLO.\n")); 649 putnext(kc->kc_wq, mp); 650 } else { 651 minor_t ksminor; 652 653 /* Initialize keysock state. */ 654 655 ks2dbg(keystack, ("Made it into PF_KEY socket open.\n")); 656 657 ksminor = (minor_t)(uintptr_t) 658 vmem_alloc(keysock_vmem, 1, VM_NOSLEEP); 659 if (ksminor == 0) { 660 netstack_rele(keystack->keystack_netstack); 661 return (ENOMEM); 662 } 663 ks = kmem_zalloc(sizeof (keysock_t), KM_NOSLEEP); 664 if (ks == NULL) { 665 vmem_free(keysock_vmem, (void *)(uintptr_t)ksminor, 1); 666 netstack_rele(keystack->keystack_netstack); 667 return (ENOMEM); 668 } 669 670 mutex_init(&ks->keysock_lock, NULL, MUTEX_DEFAULT, 0); 671 ks->keysock_rq = q; 672 ks->keysock_wq = WR(q); 673 ks->keysock_state = TS_UNBND; 674 ks->keysock_serial = ksminor; 675 676 q->q_ptr = ks; 677 WR(q)->q_ptr = ks; 678 ks->keysock_keystack = keystack; 679 680 /* 681 * The receive hiwat is only looked at on the stream head 682 * queue. Store in q_hiwat in order to return on SO_RCVBUF 683 * getsockopts. 684 */ 685 686 q->q_hiwat = keystack->keystack_recv_hiwat; 687 688 /* 689 * The transmit hiwat/lowat is only looked at on IP's queue. 690 * Store in q_hiwat/q_lowat in order to return on 691 * SO_SNDBUF/SO_SNDLOWAT getsockopts. 692 */ 693 694 WR(q)->q_hiwat = keystack->keystack_xmit_hiwat; 695 WR(q)->q_lowat = keystack->keystack_xmit_lowat; 696 697 *devp = makedevice(getmajor(*devp), ksminor); 698 699 /* 700 * Thread keysock into the global keysock list. 701 */ 702 mutex_enter(&keystack->keystack_list_lock); 703 ks->keysock_next = keystack->keystack_list; 704 ks->keysock_ptpn = &keystack->keystack_list; 705 if (keystack->keystack_list != NULL) { 706 keystack->keystack_list->keysock_ptpn = 707 &ks->keysock_next; 708 } 709 keystack->keystack_list = ks; 710 mutex_exit(&keystack->keystack_list_lock); 711 712 qprocson(q); 713 (void) mi_set_sth_hiwat(q, keystack->keystack_recv_hiwat); 714 /* 715 * Wait outside the keysock module perimeter for IPsec 716 * plumbing to be completed. If it fails, keysock_close() 717 * undoes everything we just did. 718 */ 719 if (!ipsec_loader_wait(q, 720 keystack->keystack_netstack->netstack_ipsec)) { 721 (void) keysock_close(q); 722 return (EPFNOSUPPORT); 723 } 724 } 725 726 return (0); 727 } 728 729 /* BELOW THIS LINE ARE ROUTINES INCLUDING AND RELATED TO keysock_wput(). */ 730 731 /* 732 * Copy relevant state bits. 733 */ 734 static void 735 keysock_copy_info(struct T_info_ack *tap, keysock_t *ks) 736 { 737 *tap = keysock_g_t_info_ack; 738 tap->CURRENT_state = ks->keysock_state; 739 tap->OPT_size = keysock_max_optsize; 740 } 741 742 /* 743 * This routine responds to T_CAPABILITY_REQ messages. It is called by 744 * keysock_wput. Much of the T_CAPABILITY_ACK information is copied from 745 * keysock_g_t_info_ack. The current state of the stream is copied from 746 * keysock_state. 747 */ 748 static void 749 keysock_capability_req(queue_t *q, mblk_t *mp) 750 { 751 keysock_t *ks = (keysock_t *)q->q_ptr; 752 t_uscalar_t cap_bits1; 753 struct T_capability_ack *tcap; 754 755 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 756 757 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 758 mp->b_datap->db_type, T_CAPABILITY_ACK); 759 if (mp == NULL) 760 return; 761 762 tcap = (struct T_capability_ack *)mp->b_rptr; 763 tcap->CAP_bits1 = 0; 764 765 if (cap_bits1 & TC1_INFO) { 766 keysock_copy_info(&tcap->INFO_ack, ks); 767 tcap->CAP_bits1 |= TC1_INFO; 768 } 769 770 qreply(q, mp); 771 } 772 773 /* 774 * This routine responds to T_INFO_REQ messages. It is called by 775 * keysock_wput_other. 776 * Most of the T_INFO_ACK information is copied from keysock_g_t_info_ack. 777 * The current state of the stream is copied from keysock_state. 778 */ 779 static void 780 keysock_info_req(q, mp) 781 queue_t *q; 782 mblk_t *mp; 783 { 784 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 785 T_INFO_ACK); 786 if (mp == NULL) 787 return; 788 keysock_copy_info((struct T_info_ack *)mp->b_rptr, 789 (keysock_t *)q->q_ptr); 790 qreply(q, mp); 791 } 792 793 /* 794 * keysock_err_ack. This routine creates a 795 * T_ERROR_ACK message and passes it 796 * upstream. 797 */ 798 static void 799 keysock_err_ack(q, mp, t_error, sys_error) 800 queue_t *q; 801 mblk_t *mp; 802 int t_error; 803 int sys_error; 804 { 805 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 806 qreply(q, mp); 807 } 808 809 /* 810 * This routine retrieves the current status of socket options. 811 * It returns the size of the option retrieved. 812 */ 813 /* ARGSUSED */ 814 int 815 keysock_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 816 { 817 int *i1 = (int *)ptr; 818 keysock_t *ks = (keysock_t *)q->q_ptr; 819 820 switch (level) { 821 case SOL_SOCKET: 822 mutex_enter(&ks->keysock_lock); 823 switch (name) { 824 case SO_TYPE: 825 *i1 = SOCK_RAW; 826 break; 827 case SO_USELOOPBACK: 828 *i1 = (int)(!((ks->keysock_flags & KEYSOCK_NOLOOP) == 829 KEYSOCK_NOLOOP)); 830 break; 831 /* 832 * The following two items can be manipulated, 833 * but changing them should do nothing. 834 */ 835 case SO_SNDBUF: 836 *i1 = (int)q->q_hiwat; 837 break; 838 case SO_RCVBUF: 839 *i1 = (int)(RD(q)->q_hiwat); 840 break; 841 } 842 mutex_exit(&ks->keysock_lock); 843 break; 844 default: 845 return (0); 846 } 847 return (sizeof (int)); 848 } 849 850 /* 851 * This routine sets socket options. 852 */ 853 /* ARGSUSED */ 854 int 855 keysock_opt_set(queue_t *q, uint_t mgmt_flags, int level, 856 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 857 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 858 { 859 int *i1 = (int *)invalp; 860 keysock_t *ks = (keysock_t *)q->q_ptr; 861 keysock_stack_t *keystack = ks->keysock_keystack; 862 863 switch (level) { 864 case SOL_SOCKET: 865 mutex_enter(&ks->keysock_lock); 866 switch (name) { 867 case SO_USELOOPBACK: 868 if (!(*i1)) 869 ks->keysock_flags |= KEYSOCK_NOLOOP; 870 else ks->keysock_flags &= ~KEYSOCK_NOLOOP; 871 break; 872 case SO_SNDBUF: 873 if (*i1 > keystack->keystack_max_buf) 874 return (ENOBUFS); 875 q->q_hiwat = *i1; 876 break; 877 case SO_RCVBUF: 878 if (*i1 > keystack->keystack_max_buf) 879 return (ENOBUFS); 880 RD(q)->q_hiwat = *i1; 881 (void) mi_set_sth_hiwat(RD(q), *i1); 882 break; 883 } 884 mutex_exit(&ks->keysock_lock); 885 break; 886 } 887 return (0); 888 } 889 890 /* 891 * Handle STREAMS messages. 892 */ 893 static void 894 keysock_wput_other(queue_t *q, mblk_t *mp) 895 { 896 struct iocblk *iocp; 897 int error; 898 keysock_t *ks = (keysock_t *)q->q_ptr; 899 keysock_stack_t *keystack = ks->keysock_keystack; 900 cred_t *cr; 901 902 switch (mp->b_datap->db_type) { 903 case M_PROTO: 904 case M_PCPROTO: 905 if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) { 906 ks3dbg(keystack, ( 907 "keysock_wput_other: Not big enough M_PROTO\n")); 908 freemsg(mp); 909 return; 910 } 911 cr = zone_get_kcred(netstackid_to_zoneid( 912 keystack->keystack_netstack->netstack_stackid)); 913 ASSERT(cr != NULL); 914 915 switch (((union T_primitives *)mp->b_rptr)->type) { 916 case T_CAPABILITY_REQ: 917 keysock_capability_req(q, mp); 918 break; 919 case T_INFO_REQ: 920 keysock_info_req(q, mp); 921 break; 922 case T_SVR4_OPTMGMT_REQ: 923 (void) svr4_optcom_req(q, mp, DB_CREDDEF(mp, cr), 924 &keysock_opt_obj, B_FALSE); 925 break; 926 case T_OPTMGMT_REQ: 927 (void) tpi_optcom_req(q, mp, DB_CREDDEF(mp, cr), 928 &keysock_opt_obj, B_FALSE); 929 break; 930 case T_DATA_REQ: 931 case T_EXDATA_REQ: 932 case T_ORDREL_REQ: 933 /* Illegal for keysock. */ 934 freemsg(mp); 935 (void) putnextctl1(RD(q), M_ERROR, EPROTO); 936 break; 937 default: 938 /* Not supported by keysock. */ 939 keysock_err_ack(q, mp, TNOTSUPPORT, 0); 940 break; 941 } 942 crfree(cr); 943 return; 944 case M_IOCTL: 945 iocp = (struct iocblk *)mp->b_rptr; 946 error = EINVAL; 947 948 switch (iocp->ioc_cmd) { 949 case ND_SET: 950 case ND_GET: 951 if (nd_getset(q, keystack->keystack_g_nd, mp)) { 952 qreply(q, mp); 953 return; 954 } else 955 error = ENOENT; 956 /* FALLTHRU */ 957 default: 958 miocnak(q, mp, 0, error); 959 return; 960 } 961 case M_FLUSH: 962 if (*mp->b_rptr & FLUSHW) { 963 flushq(q, FLUSHALL); 964 *mp->b_rptr &= ~FLUSHW; 965 } 966 if (*mp->b_rptr & FLUSHR) { 967 qreply(q, mp); 968 return; 969 } 970 /* Else FALLTHRU */ 971 } 972 973 /* If fell through, just black-hole the message. */ 974 freemsg(mp); 975 } 976 977 /* 978 * Transmit a PF_KEY error message to the instance either pointed to 979 * by ks, the instance with serial number serial, or more, depending. 980 * 981 * The faulty message (or a reasonable facsimile thereof) is in mp. 982 * This function will free mp or recycle it for delivery, thereby causing 983 * the stream head to free it. 984 */ 985 static void 986 keysock_error(keysock_t *ks, mblk_t *mp, int error, int diagnostic) 987 { 988 sadb_msg_t *samsg = (sadb_msg_t *)mp->b_rptr; 989 keysock_stack_t *keystack = ks->keysock_keystack; 990 991 ASSERT(mp->b_datap->db_type == M_DATA); 992 993 if (samsg->sadb_msg_type < SADB_GETSPI || 994 samsg->sadb_msg_type > SADB_MAX) 995 samsg->sadb_msg_type = SADB_RESERVED; 996 997 /* 998 * Strip out extension headers. 999 */ 1000 ASSERT(mp->b_rptr + sizeof (*samsg) <= mp->b_datap->db_lim); 1001 mp->b_wptr = mp->b_rptr + sizeof (*samsg); 1002 samsg->sadb_msg_len = SADB_8TO64(sizeof (sadb_msg_t)); 1003 samsg->sadb_msg_errno = (uint8_t)error; 1004 samsg->sadb_x_msg_diagnostic = (uint16_t)diagnostic; 1005 1006 keysock_passup(mp, samsg, ks->keysock_serial, NULL, B_FALSE, keystack); 1007 } 1008 1009 /* 1010 * Pass down a message to a consumer. Wrap it in KEYSOCK_IN, and copy 1011 * in the extv if passed in. 1012 */ 1013 static void 1014 keysock_passdown(keysock_t *ks, mblk_t *mp, uint8_t satype, sadb_ext_t *extv[], 1015 boolean_t flushmsg) 1016 { 1017 keysock_consumer_t *kc; 1018 mblk_t *wrapper; 1019 keysock_in_t *ksi; 1020 int i; 1021 keysock_stack_t *keystack = ks->keysock_keystack; 1022 1023 wrapper = allocb(sizeof (ipsec_info_t), BPRI_HI); 1024 if (wrapper == NULL) { 1025 ks3dbg(keystack, ("keysock_passdown: allocb failed.\n")); 1026 if (extv[SADB_EXT_KEY_ENCRYPT] != NULL) 1027 bzero(extv[SADB_EXT_KEY_ENCRYPT], 1028 SADB_64TO8( 1029 extv[SADB_EXT_KEY_ENCRYPT]->sadb_ext_len)); 1030 if (extv[SADB_EXT_KEY_AUTH] != NULL) 1031 bzero(extv[SADB_EXT_KEY_AUTH], 1032 SADB_64TO8( 1033 extv[SADB_EXT_KEY_AUTH]->sadb_ext_len)); 1034 if (flushmsg) { 1035 ks0dbg(( 1036 "keysock: Downwards flush/dump message failed!\n")); 1037 /* If this is true, I hold the perimeter. */ 1038