1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/param.h> 27 #include <sys/types.h> 28 #include <sys/stream.h> 29 #include <sys/strsubr.h> 30 #include <sys/strsun.h> 31 #include <sys/stropts.h> 32 #include <sys/zone.h> 33 #include <sys/vnode.h> 34 #include <sys/sysmacros.h> 35 #define _SUN_TPI_VERSION 2 36 #include <sys/tihdr.h> 37 #include <sys/ddi.h> 38 #include <sys/sunddi.h> 39 #include <sys/mkdev.h> 40 #include <sys/debug.h> 41 #include <sys/kmem.h> 42 #include <sys/cmn_err.h> 43 #include <sys/suntpi.h> 44 #include <sys/policy.h> 45 46 #include <sys/socket.h> 47 #include <netinet/in.h> 48 #include <net/pfkeyv2.h> 49 #include <net/pfpolicy.h> 50 51 #include <inet/common.h> 52 #include <netinet/ip6.h> 53 #include <inet/ip.h> 54 #include <inet/ip6.h> 55 #include <inet/mi.h> 56 #include <inet/proto_set.h> 57 #include <inet/nd.h> 58 #include <inet/ip_if.h> 59 #include <inet/tun.h> 60 #include <inet/optcom.h> 61 #include <inet/ipsec_info.h> 62 #include <inet/ipsec_impl.h> 63 #include <inet/spdsock.h> 64 #include <inet/sadb.h> 65 66 #include <sys/isa_defs.h> 67 68 #include <c2/audit.h> 69 70 /* 71 * This is a transport provider for the PF_POLICY IPsec policy 72 * management socket, which provides a management interface into the 73 * SPD, allowing policy rules to be added, deleted, and queried. 74 * 75 * This effectively replaces the old private SIOC*IPSECONFIG ioctls 76 * with an extensible interface which will hopefully be public some 77 * day. 78 * 79 * See <net/pfpolicy.h> for more details on the protocol. 80 * 81 * We link against drv/ip and call directly into it to manipulate the 82 * SPD; see ipsec_impl.h for the policy data structures and spd.c for 83 * the code which maintains them. 84 * 85 * The MT model of this is QPAIR with the addition of some explicit 86 * locking to protect system-wide policy data structures. 87 */ 88 89 static vmem_t *spdsock_vmem; /* for minor numbers. */ 90 91 #define ALIGNED64(x) IS_P2ALIGNED((x), sizeof (uint64_t)) 92 93 /* Default structure copied into T_INFO_ACK messages (from rts.c...) */ 94 static struct T_info_ack spdsock_g_t_info_ack = { 95 T_INFO_ACK, 96 T_INFINITE, /* TSDU_size. Maximum size messages. */ 97 T_INVALID, /* ETSDU_size. No expedited data. */ 98 T_INVALID, /* CDATA_size. No connect data. */ 99 T_INVALID, /* DDATA_size. No disconnect data. */ 100 0, /* ADDR_size. */ 101 0, /* OPT_size. No user-settable options */ 102 64 * 1024, /* TIDU_size. spdsock allows maximum size messages. */ 103 T_COTS, /* SERV_type. spdsock supports connection oriented. */ 104 TS_UNBND, /* CURRENT_state. This is set from spdsock_state. */ 105 (XPG4_1) /* Provider flags */ 106 }; 107 108 /* Named Dispatch Parameter Management Structure */ 109 typedef struct spdsockparam_s { 110 uint_t spdsock_param_min; 111 uint_t spdsock_param_max; 112 uint_t spdsock_param_value; 113 char *spdsock_param_name; 114 } spdsockparam_t; 115 116 /* 117 * Table of NDD variables supported by spdsock. These are loaded into 118 * spdsock_g_nd in spdsock_init_nd. 119 * All of these are alterable, within the min/max values given, at run time. 120 */ 121 static spdsockparam_t lcl_param_arr[] = { 122 /* min max value name */ 123 { 4096, 65536, 8192, "spdsock_xmit_hiwat"}, 124 { 0, 65536, 1024, "spdsock_xmit_lowat"}, 125 { 4096, 65536, 8192, "spdsock_recv_hiwat"}, 126 { 65536, 1024*1024*1024, 256*1024, "spdsock_max_buf"}, 127 { 0, 3, 0, "spdsock_debug"}, 128 }; 129 #define spds_xmit_hiwat spds_params[0].spdsock_param_value 130 #define spds_xmit_lowat spds_params[1].spdsock_param_value 131 #define spds_recv_hiwat spds_params[2].spdsock_param_value 132 #define spds_max_buf spds_params[3].spdsock_param_value 133 #define spds_debug spds_params[4].spdsock_param_value 134 135 #define ss0dbg(a) printf a 136 /* NOTE: != 0 instead of > 0 so lint doesn't complain. */ 137 #define ss1dbg(spds, a) if (spds->spds_debug != 0) printf a 138 #define ss2dbg(spds, a) if (spds->spds_debug > 1) printf a 139 #define ss3dbg(spds, a) if (spds->spds_debug > 2) printf a 140 141 #define RESET_SPDSOCK_DUMP_POLHEAD(ss, iph) { \ 142 ASSERT(RW_READ_HELD(&(iph)->iph_lock)); \ 143 (ss)->spdsock_dump_head = (iph); \ 144 (ss)->spdsock_dump_gen = (iph)->iph_gen; \ 145 (ss)->spdsock_dump_cur_type = 0; \ 146 (ss)->spdsock_dump_cur_af = IPSEC_AF_V4; \ 147 (ss)->spdsock_dump_cur_rule = NULL; \ 148 (ss)->spdsock_dump_count = 0; \ 149 (ss)->spdsock_dump_cur_chain = 0; \ 150 } 151 152 static int spdsock_close(queue_t *); 153 static int spdsock_open(queue_t *, dev_t *, int, int, cred_t *); 154 static void spdsock_wput(queue_t *, mblk_t *); 155 static void spdsock_wsrv(queue_t *); 156 static void spdsock_rsrv(queue_t *); 157 static void *spdsock_stack_init(netstackid_t stackid, netstack_t *ns); 158 static void spdsock_stack_fini(netstackid_t stackid, void *arg); 159 static void spdsock_loadcheck(void *); 160 static void spdsock_merge_algs(spd_stack_t *); 161 static void spdsock_flush_one(ipsec_policy_head_t *, netstack_t *); 162 static mblk_t *spdsock_dump_next_record(spdsock_t *); 163 164 static struct module_info info = { 165 5138, "spdsock", 1, INFPSZ, 512, 128 166 }; 167 168 static struct qinit rinit = { 169 NULL, (pfi_t)spdsock_rsrv, spdsock_open, spdsock_close, 170 NULL, &info 171 }; 172 173 static struct qinit winit = { 174 (pfi_t)spdsock_wput, (pfi_t)spdsock_wsrv, NULL, NULL, NULL, &info 175 }; 176 177 struct streamtab spdsockinfo = { 178 &rinit, &winit 179 }; 180 181 /* mapping from alg type to protocol number, as per RFC 2407 */ 182 static const uint_t algproto[] = { 183 PROTO_IPSEC_AH, 184 PROTO_IPSEC_ESP, 185 }; 186 187 #define NALGPROTOS (sizeof (algproto) / sizeof (algproto[0])) 188 189 /* mapping from kernel exec mode to spdsock exec mode */ 190 static const uint_t execmodes[] = { 191 SPD_ALG_EXEC_MODE_SYNC, 192 SPD_ALG_EXEC_MODE_ASYNC 193 }; 194 195 #define NEXECMODES (sizeof (execmodes) / sizeof (execmodes[0])) 196 197 #define ALL_ACTIVE_POLHEADS ((ipsec_policy_head_t *)-1) 198 #define ALL_INACTIVE_POLHEADS ((ipsec_policy_head_t *)-2) 199 200 #define ITP_NAME(itp) (itp != NULL ? itp->itp_name : NULL) 201 202 /* ARGSUSED */ 203 static int 204 spdsock_param_get(q, mp, cp, cr) 205 queue_t *q; 206 mblk_t *mp; 207 caddr_t cp; 208 cred_t *cr; 209 { 210 spdsockparam_t *spdsockpa = (spdsockparam_t *)cp; 211 uint_t value; 212 spdsock_t *ss = (spdsock_t *)q->q_ptr; 213 spd_stack_t *spds = ss->spdsock_spds; 214 215 mutex_enter(&spds->spds_param_lock); 216 value = spdsockpa->spdsock_param_value; 217 mutex_exit(&spds->spds_param_lock); 218 219 (void) mi_mpprintf(mp, "%u", value); 220 return (0); 221 } 222 223 /* This routine sets an NDD variable in a spdsockparam_t structure. */ 224 /* ARGSUSED */ 225 static int 226 spdsock_param_set(q, mp, value, cp, cr) 227 queue_t *q; 228 mblk_t *mp; 229 char *value; 230 caddr_t cp; 231 cred_t *cr; 232 { 233 ulong_t new_value; 234 spdsockparam_t *spdsockpa = (spdsockparam_t *)cp; 235 spdsock_t *ss = (spdsock_t *)q->q_ptr; 236 spd_stack_t *spds = ss->spdsock_spds; 237 238 /* Convert the value from a string into a long integer. */ 239 if (ddi_strtoul(value, NULL, 10, &new_value) != 0) 240 return (EINVAL); 241 242 mutex_enter(&spds->spds_param_lock); 243 /* 244 * Fail the request if the new value does not lie within the 245 * required bounds. 246 */ 247 if (new_value < spdsockpa->spdsock_param_min || 248 new_value > spdsockpa->spdsock_param_max) { 249 mutex_exit(&spds->spds_param_lock); 250 return (EINVAL); 251 } 252 253 /* Set the new value */ 254 spdsockpa->spdsock_param_value = new_value; 255 mutex_exit(&spds->spds_param_lock); 256 257 return (0); 258 } 259 260 /* 261 * Initialize at module load time 262 */ 263 boolean_t 264 spdsock_ddi_init(void) 265 { 266 spdsock_max_optsize = optcom_max_optsize( 267 spdsock_opt_obj.odb_opt_des_arr, spdsock_opt_obj.odb_opt_arr_cnt); 268 269 spdsock_vmem = vmem_create("spdsock", (void *)1, MAXMIN, 1, 270 NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER); 271 272 /* 273 * We want to be informed each time a stack is created or 274 * destroyed in the kernel, so we can maintain the 275 * set of spd_stack_t's. 276 */ 277 netstack_register(NS_SPDSOCK, spdsock_stack_init, NULL, 278 spdsock_stack_fini); 279 280 return (B_TRUE); 281 } 282 283 /* 284 * Walk through the param array specified registering each element with the 285 * named dispatch handler. 286 */ 287 static boolean_t 288 spdsock_param_register(IDP *ndp, spdsockparam_t *ssp, int cnt) 289 { 290 for (; cnt-- > 0; ssp++) { 291 if (ssp->spdsock_param_name != NULL && 292 ssp->spdsock_param_name[0]) { 293 if (!nd_load(ndp, 294 ssp->spdsock_param_name, 295 spdsock_param_get, spdsock_param_set, 296 (caddr_t)ssp)) { 297 nd_free(ndp); 298 return (B_FALSE); 299 } 300 } 301 } 302 return (B_TRUE); 303 } 304 305 /* 306 * Initialize for each stack instance 307 */ 308 /* ARGSUSED */ 309 static void * 310 spdsock_stack_init(netstackid_t stackid, netstack_t *ns) 311 { 312 spd_stack_t *spds; 313 spdsockparam_t *ssp; 314 315 spds = (spd_stack_t *)kmem_zalloc(sizeof (*spds), KM_SLEEP); 316 spds->spds_netstack = ns; 317 318 ASSERT(spds->spds_g_nd == NULL); 319 320 ssp = (spdsockparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP); 321 spds->spds_params = ssp; 322 bcopy(lcl_param_arr, ssp, sizeof (lcl_param_arr)); 323 324 (void) spdsock_param_register(&spds->spds_g_nd, ssp, 325 A_CNT(lcl_param_arr)); 326 327 mutex_init(&spds->spds_param_lock, NULL, MUTEX_DEFAULT, NULL); 328 mutex_init(&spds->spds_alg_lock, NULL, MUTEX_DEFAULT, NULL); 329 330 return (spds); 331 } 332 333 void 334 spdsock_ddi_destroy(void) 335 { 336 vmem_destroy(spdsock_vmem); 337 338 netstack_unregister(NS_SPDSOCK); 339 } 340 341 /* ARGSUSED */ 342 static void 343 spdsock_stack_fini(netstackid_t stackid, void *arg) 344 { 345 spd_stack_t *spds = (spd_stack_t *)arg; 346 347 freemsg(spds->spds_mp_algs); 348 mutex_destroy(&spds->spds_param_lock); 349 mutex_destroy(&spds->spds_alg_lock); 350 nd_free(&spds->spds_g_nd); 351 kmem_free(spds->spds_params, sizeof (lcl_param_arr)); 352 spds->spds_params = NULL; 353 354 kmem_free(spds, sizeof (*spds)); 355 } 356 357 /* 358 * NOTE: large quantities of this should be shared with keysock. 359 * Would be nice to combine some of this into a common module, but 360 * not possible given time pressures. 361 */ 362 363 /* 364 * High-level reality checking of extensions. 365 */ 366 /* ARGSUSED */ /* XXX */ 367 static boolean_t 368 ext_check(spd_ext_t *ext) 369 { 370 spd_if_t *tunname = (spd_if_t *)ext; 371 int i; 372 char *idstr; 373 374 if (ext->spd_ext_type == SPD_EXT_TUN_NAME) { 375 /* (NOTE: Modified from SADB_EXT_IDENTITY..) */ 376 377 /* 378 * Make sure the strings in these identities are 379 * null-terminated. Let's "proactively" null-terminate the 380 * string at the last byte if it's not terminated sooner. 381 */ 382 i = SPD_64TO8(tunname->spd_if_len) - sizeof (spd_if_t); 383 idstr = (char *)(tunname + 1); 384 while (*idstr != '\0' && i > 0) { 385 i--; 386 idstr++; 387 } 388 if (i == 0) { 389 /* 390 * I.e., if the bozo user didn't NULL-terminate the 391 * string... 392 */ 393 idstr--; 394 *idstr = '\0'; 395 } 396 } 397 return (B_TRUE); /* For now... */ 398 } 399 400 401 402 /* Return values for spdsock_get_ext(). */ 403 #define KGE_OK 0 404 #define KGE_DUP 1 405 #define KGE_UNK 2 406 #define KGE_LEN 3 407 #define KGE_CHK 4 408 409 /* 410 * Parse basic extension headers and return in the passed-in pointer vector. 411 * Return values include: 412 * 413 * KGE_OK Everything's nice and parsed out. 414 * If there are no extensions, place NULL in extv[0]. 415 * KGE_DUP There is a duplicate extension. 416 * First instance in appropriate bin. First duplicate in 417 * extv[0]. 418 * KGE_UNK Unknown extension type encountered. extv[0] contains 419 * unknown header. 420 * KGE_LEN Extension length error. 421 * KGE_CHK High-level reality check failed on specific extension. 422 * 423 * My apologies for some of the pointer arithmetic in here. I'm thinking 424 * like an assembly programmer, yet trying to make the compiler happy. 425 */ 426 static int 427 spdsock_get_ext(spd_ext_t *extv[], spd_msg_t *basehdr, uint_t msgsize) 428 { 429 bzero(extv, sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1)); 430 431 /* Use extv[0] as the "current working pointer". */ 432 433 extv[0] = (spd_ext_t *)(basehdr + 1); 434 435 while (extv[0] < (spd_ext_t *)(((uint8_t *)basehdr) + msgsize)) { 436 /* Check for unknown headers. */ 437 if (extv[0]->spd_ext_type == 0 || 438 extv[0]->spd_ext_type > SPD_EXT_MAX) 439 return (KGE_UNK); 440 441 /* 442 * Check length. Use uint64_t because extlen is in units 443 * of 64-bit words. If length goes beyond the msgsize, 444 * return an error. (Zero length also qualifies here.) 445 */ 446 if (extv[0]->spd_ext_len == 0 || 447 (void *)((uint64_t *)extv[0] + extv[0]->spd_ext_len) > 448 (void *)((uint8_t *)basehdr + msgsize)) 449 return (KGE_LEN); 450 451 /* Check for redundant headers. */ 452 if (extv[extv[0]->spd_ext_type] != NULL) 453 return (KGE_DUP); 454 455 /* 456 * Reality check the extension if possible at the spdsock 457 * level. 458 */ 459 if (!ext_check(extv[0])) 460 return (KGE_CHK); 461 462 /* If I make it here, assign the appropriate bin. */ 463 extv[extv[0]->spd_ext_type] = extv[0]; 464 465 /* Advance pointer (See above for uint64_t ptr reasoning.) */ 466 extv[0] = (spd_ext_t *) 467 ((uint64_t *)extv[0] + extv[0]->spd_ext_len); 468 } 469 470 /* Everything's cool. */ 471 472 /* 473 * If extv[0] == NULL, then there are no extension headers in this 474 * message. Ensure that this is the case. 475 */ 476 if (extv[0] == (spd_ext_t *)(basehdr + 1)) 477 extv[0] = NULL; 478 479 return (KGE_OK); 480 } 481 482 static const int bad_ext_diag[] = { 483 SPD_DIAGNOSTIC_MALFORMED_LCLPORT, 484 SPD_DIAGNOSTIC_MALFORMED_REMPORT, 485 SPD_DIAGNOSTIC_MALFORMED_PROTO, 486 SPD_DIAGNOSTIC_MALFORMED_LCLADDR, 487 SPD_DIAGNOSTIC_MALFORMED_REMADDR, 488 SPD_DIAGNOSTIC_MALFORMED_ACTION, 489 SPD_DIAGNOSTIC_MALFORMED_RULE, 490 SPD_DIAGNOSTIC_MALFORMED_RULESET, 491 SPD_DIAGNOSTIC_MALFORMED_ICMP_TYPECODE 492 }; 493 494 static const int dup_ext_diag[] = { 495 SPD_DIAGNOSTIC_DUPLICATE_LCLPORT, 496 SPD_DIAGNOSTIC_DUPLICATE_REMPORT, 497 SPD_DIAGNOSTIC_DUPLICATE_PROTO, 498 SPD_DIAGNOSTIC_DUPLICATE_LCLADDR, 499 SPD_DIAGNOSTIC_DUPLICATE_REMADDR, 500 SPD_DIAGNOSTIC_DUPLICATE_ACTION, 501 SPD_DIAGNOSTIC_DUPLICATE_RULE, 502 SPD_DIAGNOSTIC_DUPLICATE_RULESET, 503 SPD_DIAGNOSTIC_DUPLICATE_ICMP_TYPECODE 504 }; 505 506 /* 507 * Transmit a PF_POLICY error message to the instance either pointed to 508 * by ks, the instance with serial number serial, or more, depending. 509 * 510 * The faulty message (or a reasonable facsimile thereof) is in mp. 511 * This function will free mp or recycle it for delivery, thereby causing 512 * the stream head to free it. 513 */ 514 static void 515 spdsock_error(queue_t *q, mblk_t *mp, int error, int diagnostic) 516 { 517 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 518 519 ASSERT(mp->b_datap->db_type == M_DATA); 520 521 if (spmsg->spd_msg_type < SPD_MIN || 522 spmsg->spd_msg_type > SPD_MAX) 523 spmsg->spd_msg_type = SPD_RESERVED; 524 525 /* 526 * Strip out extension headers. 527 */ 528 ASSERT(mp->b_rptr + sizeof (*spmsg) <= mp->b_datap->db_lim); 529 mp->b_wptr = mp->b_rptr + sizeof (*spmsg); 530 spmsg->spd_msg_len = SPD_8TO64(sizeof (spd_msg_t)); 531 spmsg->spd_msg_errno = (uint8_t)error; 532 spmsg->spd_msg_diagnostic = (uint16_t)diagnostic; 533 534 qreply(q, mp); 535 } 536 537 static void 538 spdsock_diag(queue_t *q, mblk_t *mp, int diagnostic) 539 { 540 spdsock_error(q, mp, EINVAL, diagnostic); 541 } 542 543 static void 544 spd_echo(queue_t *q, mblk_t *mp) 545 { 546 qreply(q, mp); 547 } 548 549 /* 550 * Do NOT consume a reference to itp. 551 */ 552 /*ARGSUSED*/ 553 static void 554 spdsock_flush_node(ipsec_tun_pol_t *itp, void *cookie, netstack_t *ns) 555 { 556 boolean_t active = (boolean_t)cookie; 557 ipsec_policy_head_t *iph; 558 559 iph = active ? itp->itp_policy : itp->itp_inactive; 560 IPPH_REFHOLD(iph); 561 mutex_enter(&itp->itp_lock); 562 spdsock_flush_one(iph, ns); 563 if (active) 564 itp->itp_flags &= ~ITPF_PFLAGS; 565 else 566 itp->itp_flags &= ~ITPF_IFLAGS; 567 mutex_exit(&itp->itp_lock); 568 } 569 570 /* 571 * Clear out one polhead. 572 */ 573 static void 574 spdsock_flush_one(ipsec_policy_head_t *iph, netstack_t *ns) 575 { 576 rw_enter(&iph->iph_lock, RW_WRITER); 577 ipsec_polhead_flush(iph, ns); 578 rw_exit(&iph->iph_lock); 579 IPPH_REFRELE(iph, ns); 580 } 581 582 static void 583 spdsock_flush(queue_t *q, ipsec_policy_head_t *iph, ipsec_tun_pol_t *itp, 584 mblk_t *mp) 585 { 586 boolean_t active; 587 spdsock_t *ss = (spdsock_t *)q->q_ptr; 588 netstack_t *ns = ss->spdsock_spds->spds_netstack; 589 590 if (iph != ALL_ACTIVE_POLHEADS && iph != ALL_INACTIVE_POLHEADS) { 591 spdsock_flush_one(iph, ns); 592 if (audit_active) { 593 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 594 cred_t *cr; 595 pid_t cpid; 596 597 cr = msg_getcred(mp, &cpid); 598 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 599 audit_pf_policy(SPD_FLUSH, cr, ns, 600 ITP_NAME(itp), active, 0, cpid); 601 } 602 } else { 603 active = (iph == ALL_ACTIVE_POLHEADS); 604 605 /* First flush the global policy. */ 606 spdsock_flush_one(active ? ipsec_system_policy(ns) : 607 ipsec_inactive_policy(ns), ns); 608 if (audit_active) { 609 cred_t *cr; 610 pid_t cpid; 611 612 cr = msg_getcred(mp, &cpid); 613 audit_pf_policy(SPD_FLUSH, cr, ns, NULL, 614 active, 0, cpid); 615 } 616 /* Then flush every tunnel's appropriate one. */ 617 itp_walk(spdsock_flush_node, (void *)active, ns); 618 if (audit_active) { 619 cred_t *cr; 620 pid_t cpid; 621 622 cr = msg_getcred(mp, &cpid); 623 audit_pf_policy(SPD_FLUSH, cr, ns, 624 "all tunnels", active, 0, cpid); 625 } 626 } 627 628 spd_echo(q, mp); 629 } 630 631 static boolean_t 632 spdsock_ext_to_sel(spd_ext_t **extv, ipsec_selkey_t *sel, int *diag) 633 { 634 bzero(sel, sizeof (*sel)); 635 636 if (extv[SPD_EXT_PROTO] != NULL) { 637 struct spd_proto *pr = 638 (struct spd_proto *)extv[SPD_EXT_PROTO]; 639 sel->ipsl_proto = pr->spd_proto_number; 640 sel->ipsl_valid |= IPSL_PROTOCOL; 641 } 642 if (extv[SPD_EXT_LCLPORT] != NULL) { 643 struct spd_portrange *pr = 644 (struct spd_portrange *)extv[SPD_EXT_LCLPORT]; 645 sel->ipsl_lport = pr->spd_ports_minport; 646 sel->ipsl_valid |= IPSL_LOCAL_PORT; 647 } 648 if (extv[SPD_EXT_REMPORT] != NULL) { 649 struct spd_portrange *pr = 650 (struct spd_portrange *)extv[SPD_EXT_REMPORT]; 651 sel->ipsl_rport = pr->spd_ports_minport; 652 sel->ipsl_valid |= IPSL_REMOTE_PORT; 653 } 654 655 if (extv[SPD_EXT_ICMP_TYPECODE] != NULL) { 656 struct spd_typecode *tc= 657 (struct spd_typecode *)extv[SPD_EXT_ICMP_TYPECODE]; 658 659 sel->ipsl_valid |= IPSL_ICMP_TYPE; 660 sel->ipsl_icmp_type = tc->spd_typecode_type; 661 if (tc->spd_typecode_type_end < tc->spd_typecode_type) 662 sel->ipsl_icmp_type_end = tc->spd_typecode_type; 663 else 664 sel->ipsl_icmp_type_end = tc->spd_typecode_type_end; 665 666 if (tc->spd_typecode_code != 255) { 667 sel->ipsl_valid |= IPSL_ICMP_CODE; 668 sel->ipsl_icmp_code = tc->spd_typecode_code; 669 if (tc->spd_typecode_code_end < tc->spd_typecode_code) 670 sel->ipsl_icmp_code_end = tc->spd_typecode_code; 671 else 672 sel->ipsl_icmp_code_end = 673 tc->spd_typecode_code_end; 674 } 675 } 676 #define ADDR2SEL(sel, extv, field, pfield, extn, bit) \ 677 if ((extv)[(extn)] != NULL) { \ 678 uint_t addrlen; \ 679 struct spd_address *ap = \ 680 (struct spd_address *)((extv)[(extn)]); \ 681 addrlen = (ap->spd_address_af == AF_INET6) ? \ 682 IPV6_ADDR_LEN : IP_ADDR_LEN; \ 683 if (SPD_64TO8(ap->spd_address_len) < \ 684 (addrlen + sizeof (*ap))) { \ 685 *diag = SPD_DIAGNOSTIC_BAD_ADDR_LEN; \ 686 return (B_FALSE); \ 687 } \ 688 bcopy((ap+1), &((sel)->field), addrlen); \ 689 (sel)->pfield = ap->spd_address_prefixlen; \ 690 (sel)->ipsl_valid |= (bit); \ 691 (sel)->ipsl_valid |= (ap->spd_address_af == AF_INET6) ? \ 692 IPSL_IPV6 : IPSL_IPV4; \ 693 } 694 695 ADDR2SEL(sel, extv, ipsl_local, ipsl_local_pfxlen, 696 SPD_EXT_LCLADDR, IPSL_LOCAL_ADDR); 697 ADDR2SEL(sel, extv, ipsl_remote, ipsl_remote_pfxlen, 698 SPD_EXT_REMADDR, IPSL_REMOTE_ADDR); 699 700 if ((sel->ipsl_valid & (IPSL_IPV6|IPSL_IPV4)) == 701 (IPSL_IPV6|IPSL_IPV4)) { 702 *diag = SPD_DIAGNOSTIC_MIXED_AF; 703 return (B_FALSE); 704 } 705 706 #undef ADDR2SEL 707 708 return (B_TRUE); 709 } 710 711 static boolean_t 712 spd_convert_type(uint32_t type, ipsec_act_t *act) 713 { 714 switch (type) { 715 case SPD_ACTTYPE_DROP: 716 act->ipa_type = IPSEC_ACT_DISCARD; 717 return (B_TRUE); 718 719 case SPD_ACTTYPE_PASS: 720 act->ipa_type = IPSEC_ACT_CLEAR; 721 return (B_TRUE); 722 723 case SPD_ACTTYPE_IPSEC: 724 act->ipa_type = IPSEC_ACT_APPLY; 725 return (B_TRUE); 726 } 727 return (B_FALSE); 728 } 729 730 static boolean_t 731 spd_convert_flags(uint32_t flags, ipsec_act_t *act) 732 { 733 /* 734 * Note use of !! for boolean canonicalization. 735 */ 736 act->ipa_apply.ipp_use_ah = !!(flags & SPD_APPLY_AH); 737 act->ipa_apply.ipp_use_esp = !!(flags & SPD_APPLY_ESP); 738 act->ipa_apply.ipp_use_espa = !!(flags & SPD_APPLY_ESPA); 739 act->ipa_apply.ipp_use_se = !!(flags & SPD_APPLY_SE); 740 act->ipa_apply.ipp_use_unique = !!(flags & SPD_APPLY_UNIQUE); 741 return (B_TRUE); 742 } 743 744 static void 745 spdsock_reset_act(ipsec_act_t *act) 746 { 747 bzero(act, sizeof (*act)); 748 act->ipa_apply.ipp_espe_maxbits = IPSEC_MAX_KEYBITS; 749 act->ipa_apply.ipp_espa_maxbits = IPSEC_MAX_KEYBITS; 750 act->ipa_apply.ipp_ah_maxbits = IPSEC_MAX_KEYBITS; 751 } 752 753 /* 754 * Sanity check action against reality, and shrink-wrap key sizes.. 755 */ 756 static boolean_t 757 spdsock_check_action(ipsec_act_t *act, boolean_t tunnel_polhead, int *diag, 758 spd_stack_t *spds) 759 { 760 if (tunnel_polhead && act->ipa_apply.ipp_use_unique) { 761 *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 762 return (B_FALSE); 763 } 764 if ((act->ipa_type != IPSEC_ACT_APPLY) && 765 (act->ipa_apply.ipp_use_ah || 766 act->ipa_apply.ipp_use_esp || 767 act->ipa_apply.ipp_use_espa || 768 act->ipa_apply.ipp_use_se || 769 act->ipa_apply.ipp_use_unique)) { 770 *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 771 return (B_FALSE); 772 } 773 if ((act->ipa_type == IPSEC_ACT_APPLY) && 774 !act->ipa_apply.ipp_use_ah && 775 !act->ipa_apply.ipp_use_esp) { 776 *diag = SPD_DIAGNOSTIC_ADD_INCON_FLAGS; 777 return (B_FALSE); 778 } 779 return (ipsec_check_action(act, diag, spds->spds_netstack)); 780 } 781 782 /* 783 * We may be short a few error checks here.. 784 */ 785 static boolean_t 786 spdsock_ext_to_actvec(spd_ext_t **extv, ipsec_act_t **actpp, uint_t *nactp, 787 int *diag, spd_stack_t *spds) 788 { 789 struct spd_ext_actions *sactp = 790 (struct spd_ext_actions *)extv[SPD_EXT_ACTION]; 791 ipsec_act_t act, *actp, *endactp; 792 struct spd_attribute *attrp, *endattrp; 793 uint64_t *endp; 794 int nact; 795 boolean_t tunnel_polhead; 796 797 tunnel_polhead = (extv[SPD_EXT_TUN_NAME] != NULL && 798 (((struct spd_rule *)extv[SPD_EXT_RULE])->spd_rule_flags & 799 SPD_RULE_FLAG_TUNNEL)); 800 801 *actpp = NULL; 802 *nactp = 0; 803 804 if (sactp == NULL) { 805 *diag = SPD_DIAGNOSTIC_NO_ACTION_EXT; 806 return (B_FALSE); 807 } 808 809 /* 810 * Parse the "action" extension and convert into an action chain. 811 */ 812 813 nact = sactp->spd_actions_count; 814 815 endp = (uint64_t *)sactp; 816 endp += sactp->spd_actions_len; 817 endattrp = (struct spd_attribute *)endp; 818 819 actp = kmem_alloc(sizeof (*actp) * nact, KM_NOSLEEP); 820 if (actp == NULL) { 821 *diag = SPD_DIAGNOSTIC_ADD_NO_MEM; 822 return (B_FALSE); 823 } 824 *actpp = actp; 825 *nactp = nact; 826 endactp = actp + nact; 827 828 spdsock_reset_act(&act); 829 attrp = (struct spd_attribute *)(&sactp[1]); 830 831 for (; attrp < endattrp; attrp++) { 832 switch (attrp->spd_attr_tag) { 833 case SPD_ATTR_NOP: 834 break; 835 836 case SPD_ATTR_EMPTY: 837 spdsock_reset_act(&act); 838 break; 839 840 case SPD_ATTR_END: 841 attrp = endattrp; 842 /* FALLTHRU */ 843 case SPD_ATTR_NEXT: 844 if (actp >= endactp) { 845 *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT; 846 goto fail; 847 } 848 if (!spdsock_check_action(&act, tunnel_polhead, 849 diag, spds)) 850 goto fail; 851 *actp++ = act; 852 spdsock_reset_act(&act); 853 break; 854 855 case SPD_ATTR_TYPE: 856 if (!spd_convert_type(attrp->spd_attr_value, &act)) { 857 *diag = SPD_DIAGNOSTIC_ADD_BAD_TYPE; 858 goto fail; 859 } 860 break; 861 862 case SPD_ATTR_FLAGS: 863 if (!tunnel_polhead && extv[SPD_EXT_TUN_NAME] != NULL) { 864 /* 865 * Set "sa unique" for transport-mode 866 * tunnels whether we want to or not. 867 */ 868 attrp->spd_attr_value |= SPD_APPLY_UNIQUE; 869 } 870 if (!spd_convert_flags(attrp->spd_attr_value, &act)) { 871 *diag = SPD_DIAGNOSTIC_ADD_BAD_FLAGS; 872 goto fail; 873 } 874 break; 875 876 case SPD_ATTR_AH_AUTH: 877 if (attrp->spd_attr_value == 0) { 878 *diag = SPD_DIAGNOSTIC_UNSUPP_AH_ALG; 879 goto fail; 880 } 881 act.ipa_apply.ipp_auth_alg = attrp->spd_attr_value; 882 break; 883 884 case SPD_ATTR_ESP_ENCR: 885 if (attrp->spd_attr_value == 0) { 886 *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_ENCR_ALG; 887 goto fail; 888 } 889 act.ipa_apply.ipp_encr_alg = attrp->spd_attr_value; 890 break; 891 892 case SPD_ATTR_ESP_AUTH: 893 if (attrp->spd_attr_value == 0) { 894 *diag = SPD_DIAGNOSTIC_UNSUPP_ESP_AUTH_ALG; 895 goto fail; 896 } 897 act.ipa_apply.ipp_esp_auth_alg = attrp->spd_attr_value; 898 break; 899 900 case SPD_ATTR_ENCR_MINBITS: 901 act.ipa_apply.ipp_espe_minbits = attrp->spd_attr_value; 902 break; 903 904 case SPD_ATTR_ENCR_MAXBITS: 905 act.ipa_apply.ipp_espe_maxbits = attrp->spd_attr_value; 906 break; 907 908 case SPD_ATTR_AH_MINBITS: 909 act.ipa_apply.ipp_ah_minbits = attrp->spd_attr_value; 910 break; 911 912 case SPD_ATTR_AH_MAXBITS: 913 act.ipa_apply.ipp_ah_maxbits = attrp->spd_attr_value; 914 break; 915 916 case SPD_ATTR_ESPA_MINBITS: 917 act.ipa_apply.ipp_espa_minbits = attrp->spd_attr_value; 918 break; 919 920 case SPD_ATTR_ESPA_MAXBITS: 921 act.ipa_apply.ipp_espa_maxbits = attrp->spd_attr_value; 922 break; 923 924 case SPD_ATTR_LIFE_SOFT_TIME: 925 case SPD_ATTR_LIFE_HARD_TIME: 926 case SPD_ATTR_LIFE_SOFT_BYTES: 927 case SPD_ATTR_LIFE_HARD_BYTES: 928 break; 929 930 case SPD_ATTR_KM_PROTO: 931 act.ipa_apply.ipp_km_proto = attrp->spd_attr_value; 932 break; 933 934 case SPD_ATTR_KM_COOKIE: 935 act.ipa_apply.ipp_km_cookie = attrp->spd_attr_value; 936 break; 937 938 case SPD_ATTR_REPLAY_DEPTH: 939 act.ipa_apply.ipp_replay_depth = attrp->spd_attr_value; 940 break; 941 } 942 } 943 if (actp != endactp) { 944 *diag = SPD_DIAGNOSTIC_ADD_WRONG_ACT_COUNT; 945 goto fail; 946 } 947 948 return (B_TRUE); 949 fail: 950 ipsec_actvec_free(*actpp, nact); 951 *actpp = NULL; 952 return (B_FALSE); 953 } 954 955 typedef struct 956 { 957 ipsec_policy_t *pol; 958 int dir; 959 } tmprule_t; 960 961 static int 962 mkrule(ipsec_policy_head_t *iph, struct spd_rule *rule, 963 ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t af, 964 tmprule_t **rp, uint64_t *index, spd_stack_t *spds) 965 { 966 ipsec_policy_t *pol; 967 968 sel->ipsl_valid &= ~(IPSL_IPV6|IPSL_IPV4); 969 sel->ipsl_valid |= af; 970 971 pol = ipsec_policy_create(sel, actp, nact, rule->spd_rule_priority, 972 index, spds->spds_netstack); 973 if (pol == NULL) 974 return (ENOMEM); 975 976 (*rp)->pol = pol; 977 (*rp)->dir = dir; 978 (*rp)++; 979 980 if (!ipsec_check_policy(iph, pol, dir)) 981 return (EEXIST); 982 983 rule->spd_rule_index = pol->ipsp_index; 984 return (0); 985 } 986 987 static int 988 mkrulepair(ipsec_policy_head_t *iph, struct spd_rule *rule, 989 ipsec_selkey_t *sel, ipsec_act_t *actp, int nact, uint_t dir, uint_t afs, 990 tmprule_t **rp, uint64_t *index, spd_stack_t *spds) 991 { 992 int error; 993 994 if (afs & IPSL_IPV4) { 995 error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV4, rp, 996 index, spds); 997 if (error != 0) 998 return (error); 999 } 1000 if (afs & IPSL_IPV6) { 1001 error = mkrule(iph, rule, sel, actp, nact, dir, IPSL_IPV6, rp, 1002 index, spds); 1003 if (error != 0) 1004 return (error); 1005 } 1006 return (0); 1007 } 1008 1009 1010 static void 1011 spdsock_addrule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 1012 spd_ext_t **extv, ipsec_tun_pol_t *itp) 1013 { 1014 ipsec_selkey_t sel; 1015 ipsec_act_t *actp; 1016 uint_t nact; 1017 int diag = 0, error, afs; 1018 struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE]; 1019 tmprule_t rules[4], *rulep = &rules[0]; 1020 boolean_t tunnel_mode, empty_itp, active; 1021 uint64_t *index = (itp == NULL) ? NULL : &itp->itp_next_policy_index; 1022 spdsock_t *ss = (spdsock_t *)q->q_ptr; 1023 spd_stack_t *spds = ss->spdsock_spds; 1024 1025 if (rule == NULL) { 1026 spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT); 1027 if (audit_active) { 1028 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1029 cred_t *cr; 1030 pid_t cpid; 1031 1032 cr = msg_getcred(mp, &cpid); 1033 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1034 audit_pf_policy(SPD_ADDRULE, cr, 1035 spds->spds_netstack, ITP_NAME(itp), active, 1036 SPD_DIAGNOSTIC_NO_RULE_EXT, cpid); 1037 } 1038 return; 1039 } 1040 1041 tunnel_mode = (rule->spd_rule_flags & SPD_RULE_FLAG_TUNNEL); 1042 1043 if (itp != NULL) { 1044 mutex_enter(&itp->itp_lock); 1045 ASSERT(itp->itp_policy == iph || itp->itp_inactive == iph); 1046 active = (itp->itp_policy == iph); 1047 if (ITP_P_ISACTIVE(itp, iph)) { 1048 /* Check for mix-and-match of tunnel/transport. */ 1049 if ((tunnel_mode && !ITP_P_ISTUNNEL(itp, iph)) || 1050 (!tunnel_mode && ITP_P_ISTUNNEL(itp, iph))) { 1051 mutex_exit(&itp->itp_lock); 1052 spdsock_error(q, mp, EBUSY, 0); 1053 return; 1054 } 1055 empty_itp = B_FALSE; 1056 } else { 1057 empty_itp = B_TRUE; 1058 itp->itp_flags = active ? ITPF_P_ACTIVE : ITPF_I_ACTIVE; 1059 if (tunnel_mode) 1060 itp->itp_flags |= active ? ITPF_P_TUNNEL : 1061 ITPF_I_TUNNEL; 1062 } 1063 } else { 1064 empty_itp = B_FALSE; 1065 } 1066 1067 if (rule->spd_rule_index != 0) { 1068 diag = SPD_DIAGNOSTIC_INVALID_RULE_INDEX; 1069 error = EINVAL; 1070 goto fail2; 1071 } 1072 1073 if (!spdsock_ext_to_sel(extv, &sel, &diag)) { 1074 error = EINVAL; 1075 goto fail2; 1076 } 1077 1078 if (itp != NULL) { 1079 if (tunnel_mode) { 1080 if (sel.ipsl_valid & 1081 (IPSL_REMOTE_PORT | IPSL_LOCAL_PORT)) { 1082 itp->itp_flags |= active ? 1083 ITPF_P_PER_PORT_SECURITY : 1084 ITPF_I_PER_PORT_SECURITY; 1085 } 1086 } else { 1087 /* 1088 * For now, we don't allow transport-mode on a tunnel 1089 * with ANY specific selectors. Bail if we have such 1090 * a request. 1091 */ 1092 if (sel.ipsl_valid & IPSL_WILDCARD) { 1093 diag = SPD_DIAGNOSTIC_NO_TUNNEL_SELECTORS; 1094 error = EINVAL; 1095 goto fail2; 1096 } 1097 } 1098 } 1099 1100 if (!spdsock_ext_to_actvec(extv, &actp, &nact, &diag, spds)) { 1101 error = EINVAL; 1102 goto fail2; 1103 } 1104 /* 1105 * If no addresses were specified, add both. 1106 */ 1107 afs = sel.ipsl_valid & (IPSL_IPV6|IPSL_IPV4); 1108 if (afs == 0) 1109 afs = (IPSL_IPV6|IPSL_IPV4); 1110 1111 rw_enter(&iph->iph_lock, RW_WRITER); 1112 1113 if (rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) { 1114 error = mkrulepair(iph, rule, &sel, actp, nact, 1115 IPSEC_TYPE_OUTBOUND, afs, &rulep, index, spds); 1116 if (error != 0) 1117 goto fail; 1118 } 1119 1120 if (rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) { 1121 error = mkrulepair(iph, rule, &sel, actp, nact, 1122 IPSEC_TYPE_INBOUND, afs, &rulep, index, spds); 1123 if (error != 0) 1124 goto fail; 1125 } 1126 1127 while ((--rulep) >= &rules[0]) { 1128 ipsec_enter_policy(iph, rulep->pol, rulep->dir, 1129 spds->spds_netstack); 1130 } 1131 rw_exit(&iph->iph_lock); 1132 if (itp != NULL) 1133 mutex_exit(&itp->itp_lock); 1134 1135 ipsec_actvec_free(actp, nact); 1136 spd_echo(q, mp); 1137 if (audit_active) { 1138 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1139 cred_t *cr; 1140 pid_t cpid; 1141 1142 cr = msg_getcred(mp, &cpid); 1143 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1144 audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack, 1145 ITP_NAME(itp), active, 0, cpid); 1146 } 1147 return; 1148 1149 fail: 1150 rw_exit(&iph->iph_lock); 1151 while ((--rulep) >= &rules[0]) { 1152 IPPOL_REFRELE(rulep->pol, spds->spds_netstack); 1153 } 1154 ipsec_actvec_free(actp, nact); 1155 fail2: 1156 if (itp != NULL) { 1157 if (empty_itp) 1158 itp->itp_flags = 0; 1159 mutex_exit(&itp->itp_lock); 1160 } 1161 spdsock_error(q, mp, error, diag); 1162 if (audit_active) { 1163 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1164 cred_t *cr; 1165 pid_t cpid; 1166 1167 cr = msg_getcred(mp, &cpid); 1168 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1169 audit_pf_policy(SPD_ADDRULE, cr, spds->spds_netstack, 1170 ITP_NAME(itp), active, error, cpid); 1171 } 1172 } 1173 1174 void 1175 spdsock_deleterule(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 1176 spd_ext_t **extv, ipsec_tun_pol_t *itp) 1177 { 1178 ipsec_selkey_t sel; 1179 struct spd_rule *rule = (struct spd_rule *)extv[SPD_EXT_RULE]; 1180 int err, diag = 0; 1181 spdsock_t *ss = (spdsock_t *)q->q_ptr; 1182 netstack_t *ns = ss->spdsock_spds->spds_netstack; 1183 1184 if (rule == NULL) { 1185 spdsock_diag(q, mp, SPD_DIAGNOSTIC_NO_RULE_EXT); 1186 if (audit_active) { 1187 boolean_t active; 1188 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1189 cred_t *cr; 1190 pid_t cpid; 1191 1192 cr = msg_getcred(mp, &cpid); 1193 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1194 audit_pf_policy(SPD_DELETERULE, cr, ns, 1195 ITP_NAME(itp), active, SPD_DIAGNOSTIC_NO_RULE_EXT, 1196 cpid); 1197 } 1198 return; 1199 } 1200 1201 /* 1202 * Must enter itp_lock first to avoid deadlock. See tun.c's 1203 * set_sec_simple() for the other case of itp_lock and iph_lock. 1204 */ 1205 if (itp != NULL) 1206 mutex_enter(&itp->itp_lock); 1207 1208 if (rule->spd_rule_index != 0) { 1209 if (ipsec_policy_delete_index(iph, rule->spd_rule_index, ns) != 1210 0) { 1211 err = ESRCH; 1212 goto fail; 1213 } 1214 } else { 1215 if (!spdsock_ext_to_sel(extv, &sel, &diag)) { 1216 err = EINVAL; /* diag already set... */ 1217 goto fail; 1218 } 1219 1220 if ((rule->spd_rule_flags & SPD_RULE_FLAG_INBOUND) && 1221 !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_INBOUND, ns)) { 1222 err = ESRCH; 1223 goto fail; 1224 } 1225 1226 if ((rule->spd_rule_flags & SPD_RULE_FLAG_OUTBOUND) && 1227 !ipsec_policy_delete(iph, &sel, IPSEC_TYPE_OUTBOUND, ns)) { 1228 err = ESRCH; 1229 goto fail; 1230 } 1231 } 1232 1233 if (itp != NULL) { 1234 ASSERT(iph == itp->itp_policy || iph == itp->itp_inactive); 1235 rw_enter(&iph->iph_lock, RW_READER); 1236 if (avl_numnodes(&iph->iph_rulebyid) == 0) { 1237 if (iph == itp->itp_policy) 1238 itp->itp_flags &= ~ITPF_PFLAGS; 1239 else 1240 itp->itp_flags &= ~ITPF_IFLAGS; 1241 } 1242 /* Can exit locks in any order. */ 1243 rw_exit(&iph->iph_lock); 1244 mutex_exit(&itp->itp_lock); 1245 } 1246 spd_echo(q, mp); 1247 if (audit_active) { 1248 boolean_t active; 1249 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1250 cred_t *cr; 1251 pid_t cpid; 1252 1253 cr = msg_getcred(mp, &cpid); 1254 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1255 audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp), 1256 active, 0, cpid); 1257 } 1258 return; 1259 fail: 1260 if (itp != NULL) 1261 mutex_exit(&itp->itp_lock); 1262 spdsock_error(q, mp, err, diag); 1263 if (audit_active) { 1264 boolean_t active; 1265 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1266 cred_t *cr; 1267 pid_t cpid; 1268 1269 cr = msg_getcred(mp, &cpid); 1270 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1271 audit_pf_policy(SPD_DELETERULE, cr, ns, ITP_NAME(itp), 1272 active, err, cpid); 1273 } 1274 } 1275 1276 /* Do NOT consume a reference to itp. */ 1277 /* ARGSUSED */ 1278 static void 1279 spdsock_flip_node(ipsec_tun_pol_t *itp, void *ignoreme, netstack_t *ns) 1280 { 1281 mutex_enter(&itp->itp_lock); 1282 ITPF_SWAP(itp->itp_flags); 1283 ipsec_swap_policy(itp->itp_policy, itp->itp_inactive, ns); 1284 mutex_exit(&itp->itp_lock); 1285 } 1286 1287 void 1288 spdsock_flip(queue_t *q, mblk_t *mp, spd_if_t *tunname) 1289 { 1290 char *tname; 1291 ipsec_tun_pol_t *itp; 1292 spdsock_t *ss = (spdsock_t *)q->q_ptr; 1293 netstack_t *ns = ss->spdsock_spds->spds_netstack; 1294 1295 if (tunname != NULL) { 1296 tname = (char *)tunname->spd_if_name; 1297 if (*tname == '\0') { 1298 /* can't fail */ 1299 ipsec_swap_global_policy(ns); 1300 if (audit_active) { 1301 boolean_t active; 1302 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1303 cred_t *cr; 1304 pid_t cpid; 1305 1306 cr = msg_getcred(mp, &cpid); 1307 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1308 audit_pf_policy(SPD_FLIP, cr, ns, 1309 NULL, active, 0, cpid); 1310 } 1311 itp_walk(spdsock_flip_node, NULL, ns); 1312 if (audit_active) { 1313 boolean_t active; 1314 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1315 cred_t *cr; 1316 pid_t cpid; 1317 1318 cr = msg_getcred(mp, &cpid); 1319 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1320 audit_pf_policy(SPD_FLIP, cr, ns, 1321 "all tunnels", active, 0, cpid); 1322 } 1323 } else { 1324 itp = get_tunnel_policy(tname, ns); 1325 if (itp == NULL) { 1326 /* Better idea for "tunnel not found"? */ 1327 spdsock_error(q, mp, ESRCH, 0); 1328 if (audit_active) { 1329 boolean_t active; 1330 spd_msg_t *spmsg = 1331 (spd_msg_t *)mp->b_rptr; 1332 cred_t *cr; 1333 pid_t cpid; 1334 1335 cr = msg_getcred(mp, &cpid); 1336 active = (spmsg->spd_msg_spdid == 1337 SPD_ACTIVE); 1338 audit_pf_policy(SPD_FLIP, cr, ns, 1339 ITP_NAME(itp), active, 1340 ESRCH, cpid); 1341 } 1342 return; 1343 } 1344 spdsock_flip_node(itp, NULL, NULL); 1345 if (audit_active) { 1346 boolean_t active; 1347 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1348 cred_t *cr; 1349 pid_t cpid; 1350 1351 cr = msg_getcred(mp, &cpid); 1352 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1353 audit_pf_policy(SPD_FLIP, cr, ns, 1354 ITP_NAME(itp), active, 0, cpid); 1355 } 1356 ITP_REFRELE(itp, ns); 1357 } 1358 } else { 1359 ipsec_swap_global_policy(ns); /* can't fail */ 1360 if (audit_active) { 1361 boolean_t active; 1362 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 1363 cred_t *cr; 1364 pid_t cpid; 1365 1366 cr = msg_getcred(mp, &cpid); 1367 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 1368 audit_pf_policy(SPD_FLIP, cr, 1369 ns, NULL, active, 0, cpid); 1370 } 1371 } 1372 spd_echo(q, mp); 1373 } 1374 1375 /* 1376 * Unimplemented feature 1377 */ 1378 /* ARGSUSED */ 1379 static void 1380 spdsock_lookup(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp, 1381 spd_ext_t **extv, ipsec_tun_pol_t *itp) 1382 { 1383 spdsock_error(q, mp, EINVAL, 0); 1384 } 1385 1386 1387 static mblk_t * 1388 spdsock_dump_ruleset(mblk_t *req, ipsec_policy_head_t *iph, 1389 uint32_t count, uint16_t error) 1390 { 1391 size_t len = sizeof (spd_ruleset_ext_t) + sizeof (spd_msg_t); 1392 spd_msg_t *msg; 1393 spd_ruleset_ext_t *ruleset; 1394 mblk_t *m = allocb(len, BPRI_HI); 1395 1396 ASSERT(RW_READ_HELD(&iph->iph_lock)); 1397 1398 if (m == NULL) { 1399 return (NULL); 1400 } 1401 msg = (spd_msg_t *)m->b_rptr; 1402 ruleset = (spd_ruleset_ext_t *)(&msg[1]); 1403 1404 m->b_wptr = (uint8_t *)&ruleset[1]; 1405 1406 *msg = *(spd_msg_t *)(req->b_rptr); 1407 msg->spd_msg_len = SPD_8TO64(len); 1408 msg->spd_msg_errno = error; 1409 1410 ruleset->spd_ruleset_len = SPD_8TO64(sizeof (*ruleset)); 1411 ruleset->spd_ruleset_type = SPD_EXT_RULESET; 1412 ruleset->spd_ruleset_count = count; 1413 ruleset->spd_ruleset_version = iph->iph_gen; 1414 return (m); 1415 } 1416 1417 static mblk_t * 1418 spdsock_dump_finish(spdsock_t *ss, int error) 1419 { 1420 mblk_t *m; 1421 ipsec_policy_head_t *iph = ss->spdsock_dump_head; 1422 mblk_t *req = ss->spdsock_dump_req; 1423 1424 rw_enter(&iph->iph_lock, RW_READER); 1425 m = spdsock_dump_ruleset(req, iph, ss->spdsock_dump_count, error); 1426 rw_exit(&iph->iph_lock); 1427 IPPH_REFRELE(iph, ss->spdsock_spds->spds_netstack); 1428 ss->spdsock_dump_req = NULL; 1429 freemsg(req); 1430 1431 return (m); 1432 } 1433 1434 /* 1435 * Rule encoding functions. 1436 * We do a two-pass encode. 1437 * If base != NULL, fill in encoded rule part starting at base+offset. 1438 * Always return "offset" plus length of to-be-encoded data. 1439 */ 1440 static uint_t 1441 spdsock_encode_typecode(uint8_t *base, uint_t offset, uint8_t type, 1442 uint8_t type_end, uint8_t code, uint8_t code_end) 1443 { 1444 struct spd_typecode *tcp; 1445 1446 ASSERT(ALIGNED64(offset)); 1447 1448 if (base != NULL) { 1449 tcp = (struct spd_typecode *)(base + offset); 1450 tcp->spd_typecode_len = SPD_8TO64(sizeof (*tcp)); 1451 tcp->spd_typecode_exttype = SPD_EXT_ICMP_TYPECODE; 1452 tcp->spd_typecode_code = code; 1453 tcp->spd_typecode_type = type; 1454 tcp->spd_typecode_type_end = type_end; 1455 tcp->spd_typecode_code_end = code_end; 1456 } 1457 offset += sizeof (*tcp); 1458 1459 ASSERT(ALIGNED64(offset)); 1460 1461 return (offset); 1462 } 1463 1464 static uint_t 1465 spdsock_encode_proto(uint8_t *base, uint_t offset, uint8_t proto) 1466 { 1467 struct spd_proto *spp; 1468 1469 ASSERT(ALIGNED64(offset)); 1470 1471 if (base != NULL) { 1472 spp = (struct spd_proto *)(base + offset); 1473 spp->spd_proto_len = SPD_8TO64(sizeof (*spp)); 1474 spp->spd_proto_exttype = SPD_EXT_PROTO; 1475 spp->spd_proto_number = proto; 1476 spp->spd_proto_reserved1 = 0; 1477 spp->spd_proto_reserved2 = 0; 1478 } 1479 offset += sizeof (*spp); 1480 1481 ASSERT(ALIGNED64(offset)); 1482 1483 return (offset); 1484 } 1485 1486 static uint_t 1487 spdsock_encode_port(uint8_t *base, uint_t offset, uint16_t ext, uint16_t port) 1488 { 1489 struct spd_portrange *spp; 1490 1491 ASSERT(ALIGNED64(offset)); 1492 1493 if (base != NULL) { 1494 spp = (struct spd_portrange *)(base + offset); 1495 spp->spd_ports_len = SPD_8TO64(sizeof (*spp)); 1496 spp->spd_ports_exttype = ext; 1497 spp->spd_ports_minport = port; 1498 spp->spd_ports_maxport = port; 1499 } 1500 offset += sizeof (*spp); 1501 1502 ASSERT(ALIGNED64(offset)); 1503 1504 return (offset); 1505 } 1506 1507 static uint_t 1508 spdsock_encode_addr(uint8_t *base, uint_t offset, uint16_t ext, 1509 const ipsec_selkey_t *sel, const ipsec_addr_t *addr, uint_t pfxlen) 1510 { 1511 struct spd_address *sae; 1512 ipsec_addr_t *spdaddr; 1513 uint_t start = offset; 1514 uint_t addrlen; 1515 uint_t af; 1516 1517 if (sel->ipsl_valid & IPSL_IPV4) { 1518 af = AF_INET; 1519 addrlen = IP_ADDR_LEN; 1520 } else { 1521 af = AF_INET6; 1522 addrlen = IPV6_ADDR_LEN; 1523 } 1524 1525 ASSERT(ALIGNED64(offset)); 1526 1527 if (base != NULL) { 1528 sae = (struct spd_address *)(base + offset); 1529 sae->spd_address_exttype = ext; 1530 sae->spd_address_af = af; 1531 sae->spd_address_prefixlen = pfxlen; 1532 sae->spd_address_reserved2 = 0; 1533 1534 spdaddr = (ipsec_addr_t *)(&sae[1]); 1535 bcopy(addr, spdaddr, addrlen); 1536 } 1537 offset += sizeof (*sae); 1538 addrlen = roundup(addrlen, sizeof (uint64_t)); 1539 offset += addrlen; 1540 1541 ASSERT(ALIGNED64(offset)); 1542 1543 if (base != NULL) 1544 sae->spd_address_len = SPD_8TO64(offset - start); 1545 return (offset); 1546 } 1547 1548 static uint_t 1549 spdsock_encode_sel(uint8_t *base, uint_t offset, const ipsec_sel_t *sel) 1550 { 1551 const ipsec_selkey_t *selkey = &sel->ipsl_key; 1552 1553 if (selkey->ipsl_valid & IPSL_PROTOCOL) 1554 offset = spdsock_encode_proto(base, offset, selkey->ipsl_proto); 1555 if (selkey->ipsl_valid & IPSL_LOCAL_PORT) 1556 offset = spdsock_encode_port(base, offset, SPD_EXT_LCLPORT, 1557 selkey->ipsl_lport); 1558 if (selkey->ipsl_valid & IPSL_REMOTE_PORT) 1559 offset = spdsock_encode_port(base, offset, SPD_EXT_REMPORT, 1560 selkey->ipsl_rport); 1561 if (selkey->ipsl_valid & IPSL_REMOTE_ADDR) 1562 offset = spdsock_encode_addr(base, offset, SPD_EXT_REMADDR, 1563 selkey, &selkey->ipsl_remote, selkey->ipsl_remote_pfxlen); 1564 if (selkey->ipsl_valid & IPSL_LOCAL_ADDR) 1565 offset = spdsock_encode_addr(base, offset, SPD_EXT_LCLADDR, 1566 selkey, &selkey->ipsl_local, selkey->ipsl_local_pfxlen); 1567 if (selkey->ipsl_valid & IPSL_ICMP_TYPE) { 1568 offset = spdsock_encode_typecode(base, offset, 1569 selkey->ipsl_icmp_type, selkey->ipsl_icmp_type_end, 1570 (selkey->ipsl_valid & IPSL_ICMP_CODE) ? 1571 selkey->ipsl_icmp_code : 255, 1572 (selkey->ipsl_valid & IPSL_ICMP_CODE) ? 1573 selkey->ipsl_icmp_code_end : 255); 1574 } 1575 return (offset); 1576 } 1577 1578 static uint_t 1579 spdsock_encode_actattr(uint8_t *base, uint_t offset, uint32_t tag, 1580 uint32_t value) 1581 { 1582 struct spd_attribute *attr; 1583 1584 ASSERT(ALIGNED64(offset)); 1585 1586 if (base != NULL) { 1587 attr = (struct spd_attribute *)(base + offset); 1588 attr->spd_attr_tag = tag; 1589 attr->spd_attr_value = value; 1590 } 1591 offset += sizeof (struct spd_attribute); 1592 1593 ASSERT(ALIGNED64(offset)); 1594 1595 return (offset); 1596 } 1597 1598 1599 #define EMIT(t, v) offset = spdsock_encode_actattr(base, offset, (t), (v)) 1600 1601 static uint_t 1602 spdsock_encode_action(uint8_t *base, uint_t offset, const ipsec_action_t *ap) 1603 { 1604 const struct ipsec_act *act = &(ap->ipa_act); 1605 uint_t flags; 1606 1607 EMIT(SPD_ATTR_EMPTY, 0); 1608 switch (act->ipa_type) { 1609 case IPSEC_ACT_DISCARD: 1610 case IPSEC_ACT_REJECT: 1611 EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_DROP); 1612 break; 1613 case IPSEC_ACT_BYPASS: 1614 case IPSEC_ACT_CLEAR: 1615 EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_PASS); 1616 break; 1617 1618 case IPSEC_ACT_APPLY: 1619 EMIT(SPD_ATTR_TYPE, SPD_ACTTYPE_IPSEC); 1620 flags = 0; 1621 if (act->ipa_apply.ipp_use_ah) 1622 flags |= SPD_APPLY_AH; 1623 if (act->ipa_apply.ipp_use_esp) 1624 flags |= SPD_APPLY_ESP; 1625 if (act->ipa_apply.ipp_use_espa) 1626 flags |= SPD_APPLY_ESPA; 1627 if (act->ipa_apply.ipp_use_se) 1628 flags |= SPD_APPLY_SE; 1629 if (act->ipa_apply.ipp_use_unique) 1630 flags |= SPD_APPLY_UNIQUE; 1631 EMIT(SPD_ATTR_FLAGS, flags); 1632 if (flags & SPD_APPLY_AH) { 1633 EMIT(SPD_ATTR_AH_AUTH, act->ipa_apply.ipp_auth_alg); 1634 EMIT(SPD_ATTR_AH_MINBITS, 1635 act->ipa_apply.ipp_ah_minbits); 1636 EMIT(SPD_ATTR_AH_MAXBITS, 1637 act->ipa_apply.ipp_ah_maxbits); 1638 } 1639 if (flags & SPD_APPLY_ESP) { 1640 EMIT(SPD_ATTR_ESP_ENCR, act->ipa_apply.ipp_encr_alg); 1641 EMIT(SPD_ATTR_ENCR_MINBITS, 1642 act->ipa_apply.ipp_espe_minbits); 1643 EMIT(SPD_ATTR_ENCR_MAXBITS, 1644 act->ipa_apply.ipp_espe_maxbits); 1645 if (flags & SPD_APPLY_ESPA) { 1646 EMIT(SPD_ATTR_ESP_AUTH, 1647 act->ipa_apply.ipp_esp_auth_alg); 1648 EMIT(SPD_ATTR_ESPA_MINBITS, 1649 act->ipa_apply.ipp_espa_minbits); 1650 EMIT(SPD_ATTR_ESPA_MAXBITS, 1651 act->ipa_apply.ipp_espa_maxbits); 1652 } 1653 } 1654 if (act->ipa_apply.ipp_km_proto != 0) 1655 EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_proto); 1656 if (act->ipa_apply.ipp_km_cookie != 0) 1657 EMIT(SPD_ATTR_KM_PROTO, act->ipa_apply.ipp_km_cookie); 1658 if (act->ipa_apply.ipp_replay_depth != 0) 1659 EMIT(SPD_ATTR_REPLAY_DEPTH, 1660 act->ipa_apply.ipp_replay_depth); 1661 /* Add more here */ 1662 break; 1663 } 1664 1665 return (offset); 1666 } 1667 1668 static uint_t 1669 spdsock_encode_action_list(uint8_t *base, uint_t offset, 1670 const ipsec_action_t *ap) 1671 { 1672 struct spd_ext_actions *act; 1673 uint_t nact = 0; 1674 uint_t start = offset; 1675 1676 ASSERT(ALIGNED64(offset)); 1677 1678 if (base != NULL) { 1679 act = (struct spd_ext_actions *)(base + offset); 1680 act->spd_actions_len = 0; 1681 act->spd_actions_exttype = SPD_EXT_ACTION; 1682 act->spd_actions_count = 0; 1683 act->spd_actions_reserved = 0; 1684 } 1685 1686 offset += sizeof (*act); 1687 1688 ASSERT(ALIGNED64(offset)); 1689 1690 while (ap != NULL) { 1691 offset = spdsock_encode_action(base, offset, ap); 1692 ap = ap->ipa_next; 1693 nact++; 1694 if (ap != NULL) { 1695 EMIT(SPD_ATTR_NEXT, 0); 1696 } 1697 } 1698 EMIT(SPD_ATTR_END, 0); 1699 1700 ASSERT(ALIGNED64(offset)); 1701 1702 if (base != NULL) { 1703 act->spd_actions_count = nact; 1704 act->spd_actions_len = SPD_8TO64(offset - start); 1705 } 1706 1707 return (offset); 1708 } 1709 1710 #undef EMIT 1711 1712 /* ARGSUSED */ 1713 static uint_t 1714 spdsock_rule_flags(uint_t dir, uint_t af) 1715 { 1716 uint_t flags = 0; 1717 1718 if (dir == IPSEC_TYPE_INBOUND) 1719 flags |= SPD_RULE_FLAG_INBOUND; 1720 if (dir == IPSEC_TYPE_OUTBOUND) 1721 flags |= SPD_RULE_FLAG_OUTBOUND; 1722 1723 return (flags); 1724 } 1725 1726 1727 static uint_t 1728 spdsock_encode_rule_head(uint8_t *base, uint_t offset, spd_msg_t *req, 1729 const ipsec_policy_t *rule, uint_t dir, uint_t af, char *name, 1730 boolean_t tunnel) 1731 { 1732 struct spd_msg *spmsg; 1733 struct spd_rule *spr; 1734 spd_if_t *sid; 1735 1736 uint_t start = offset; 1737 1738 ASSERT(ALIGNED64(offset)); 1739 1740 if (base != NULL) { 1741 spmsg = (struct spd_msg *)(base + offset); 1742 bzero(spmsg, sizeof (*spmsg)); 1743 spmsg->spd_msg_version = PF_POLICY_V1; 1744 spmsg->spd_msg_type = SPD_DUMP; 1745 spmsg->spd_msg_seq = req->spd_msg_seq; 1746 spmsg->spd_msg_pid = req->spd_msg_pid; 1747 } 1748 offset += sizeof (struct spd_msg); 1749 1750 ASSERT(ALIGNED64(offset)); 1751 1752 if (base != NULL) { 1753 spr = (struct spd_rule *)(base + offset); 1754 spr->spd_rule_type = SPD_EXT_RULE; 1755 spr->spd_rule_priority = rule->ipsp_prio; 1756 spr->spd_rule_flags = spdsock_rule_flags(dir, af); 1757 if (tunnel) 1758 spr->spd_rule_flags |= SPD_RULE_FLAG_TUNNEL; 1759 spr->spd_rule_unused = 0; 1760 spr->spd_rule_len = SPD_8TO64(sizeof (*spr)); 1761 spr->spd_rule_index = rule->ipsp_index; 1762 } 1763 offset += sizeof (struct spd_rule); 1764 1765 /* 1766 * If we have an interface name (i.e. if this policy head came from 1767 * a tunnel), add the SPD_EXT_TUN_NAME extension. 1768 */ 1769 if (name != NULL) { 1770 1771 ASSERT(ALIGNED64(offset)); 1772 1773 if (base != NULL) { 1774 sid = (spd_if_t *)(base + offset); 1775 sid->spd_if_exttype = SPD_EXT_TUN_NAME; 1776 sid->spd_if_len = SPD_8TO64(sizeof (spd_if_t) + 1777 roundup((strlen(name) - 4), 8)); 1778 (void) strlcpy((char *)sid->spd_if_name, name, 1779 LIFNAMSIZ); 1780 } 1781 1782 offset += sizeof (spd_if_t) + roundup((strlen(name) - 4), 8); 1783 } 1784 1785 offset = spdsock_encode_sel(base, offset, rule->ipsp_sel); 1786 offset = spdsock_encode_action_list(base, offset, rule->ipsp_act); 1787 1788 ASSERT(ALIGNED64(offset)); 1789 1790 if (base != NULL) { 1791 spmsg->spd_msg_len = SPD_8TO64(offset - start); 1792 } 1793 return (offset); 1794 } 1795 1796 /* ARGSUSED */ 1797 static mblk_t * 1798 spdsock_encode_rule(mblk_t *req, const ipsec_policy_t *rule, 1799 uint_t dir, uint_t af, char *name, boolean_t tunnel) 1800 { 1801 mblk_t *m; 1802 uint_t len; 1803 spd_msg_t *mreq = (spd_msg_t *)req->b_rptr; 1804 1805 /* 1806 * Figure out how much space we'll need. 1807 */ 1808 len = spdsock_encode_rule_head(NULL, 0, mreq, rule, dir, af, name, 1809 tunnel); 1810 1811 /* 1812 * Allocate mblk. 1813 */ 1814 m = allocb(len, BPRI_HI); 1815 if (m == NULL) 1816 return (NULL); 1817 1818 /* 1819 * Fill it in.. 1820 */ 1821 m->b_wptr = m->b_rptr + len; 1822 bzero(m->b_rptr, len); 1823 (void) spdsock_encode_rule_head(m->b_rptr, 0, mreq, rule, dir, af, 1824 name, tunnel); 1825 return (m); 1826 } 1827 1828 static ipsec_policy_t * 1829 spdsock_dump_next_in_chain(spdsock_t *ss, ipsec_policy_head_t *iph, 1830 ipsec_policy_t *cur) 1831 { 1832 ASSERT(RW_READ_HELD(&iph->iph_lock)); 1833 1834 ss->spdsock_dump_count++; 1835 ss->spdsock_dump_cur_rule = cur->ipsp_hash.hash_next; 1836 return (cur); 1837 } 1838 1839 static ipsec_policy_t * 1840 spdsock_dump_next_rule(spdsock_t *ss, ipsec_policy_head_t *iph) 1841 { 1842 ipsec_policy_t *cur; 1843 ipsec_policy_root_t *ipr; 1844 int chain, nchains, type, af; 1845 1846 ASSERT(RW_READ_HELD(&iph->iph_lock)); 1847 1848 cur = ss->spdsock_dump_cur_rule; 1849 1850 if (cur != NULL) 1851 return (spdsock_dump_next_in_chain(ss, iph, cur)); 1852 1853 type = ss->spdsock_dump_cur_type; 1854 1855 next: 1856 chain = ss->spdsock_dump_cur_chain; 1857 ipr = &iph->iph_root[type]; 1858 nchains = ipr->ipr_nchains; 1859 1860 while (chain < nchains) { 1861 cur = ipr->ipr_hash[chain].hash_head; 1862 chain++; 1863 if (cur != NULL) { 1864 ss->spdsock_dump_cur_chain = chain; 1865 return (spdsock_dump_next_in_chain(ss, iph, cur)); 1866 } 1867 } 1868 ss->spdsock_dump_cur_chain = nchains; 1869 1870 af = ss->spdsock_dump_cur_af; 1871 while (af < IPSEC_NAF) { 1872 cur = ipr->ipr_nonhash[af]; 1873 af++; 1874 if (cur != NULL) { 1875 ss->spdsock_dump_cur_af = af; 1876 return (spdsock_dump_next_in_chain(ss, iph, cur)); 1877 } 1878 } 1879 1880 type++; 1881 if (type >= IPSEC_NTYPES) 1882 return (NULL); 1883 1884 ss->spdsock_dump_cur_chain = 0; 1885 ss->spdsock_dump_cur_type = type; 1886 ss->spdsock_dump_cur_af = IPSEC_AF_V4; 1887 goto next; 1888 1889 } 1890 1891 /* 1892 * If we're done with one policy head, but have more to go, we iterate through 1893 * another IPsec tunnel policy head (itp). Return NULL if it is an error 1894 * worthy of returning EAGAIN via PF_POLICY. 1895 */ 1896 static ipsec_tun_pol_t * 1897 spdsock_dump_iterate_next_tunnel(spdsock_t *ss, ipsec_stack_t *ipss) 1898 { 1899 ipsec_tun_pol_t *itp; 1900 1901 ASSERT(RW_READ_HELD(&ipss->ipsec_tunnel_policy_lock)); 1902 if (ipss->ipsec_tunnel_policy_gen > ss->spdsock_dump_tun_gen) { 1903 /* Oops, state of the tunnel polheads changed. */ 1904 itp = NULL; 1905 } else if (ss->spdsock_itp == NULL) { 1906 /* Just finished global, find first node. */ 1907 itp = avl_first(&ipss->ipsec_tunnel_policies); 1908 } else { 1909 /* We just finished current polhead, find the next one. */ 1910 itp = AVL_NEXT(&ipss->ipsec_tunnel_policies, ss->spdsock_itp); 1911 } 1912 if (itp != NULL) { 1913 ITP_REFHOLD(itp); 1914 } 1915 if (ss->spdsock_itp != NULL) { 1916 ITP_REFRELE(ss->spdsock_itp, ipss->ipsec_netstack); 1917 } 1918 ss->spdsock_itp = itp; 1919 return (itp); 1920 } 1921 1922 static mblk_t * 1923 spdsock_dump_next_record(spdsock_t *ss) 1924 { 1925 ipsec_policy_head_t *iph; 1926 ipsec_policy_t *rule; 1927 mblk_t *m; 1928 ipsec_tun_pol_t *itp; 1929 netstack_t *ns = ss->spdsock_spds->spds_netstack; 1930 ipsec_stack_t *ipss = ns->netstack_ipsec; 1931 1932 iph = ss->spdsock_dump_head; 1933 1934 ASSERT(iph != NULL); 1935 1936 rw_enter(&iph->iph_lock, RW_READER); 1937 1938 if (iph->iph_gen != ss->spdsock_dump_gen) { 1939 rw_exit(&iph->iph_lock); 1940 return (spdsock_dump_finish(ss, EAGAIN)); 1941 } 1942 1943 while ((rule = spdsock_dump_next_rule(ss, iph)) == NULL) { 1944 rw_exit(&iph->iph_lock); 1945 if (--(ss->spdsock_dump_remaining_polheads) == 0) 1946 return (spdsock_dump_finish(ss, 0)); 1947 1948 1949 /* 1950 * If we reach here, we have more policy heads (tunnel 1951 * entries) to dump. Let's reset to a new policy head 1952 * and get some more rules. 1953 * 1954 * An empty policy head will have spdsock_dump_next_rule() 1955 * return NULL, and we loop (while dropping the number of 1956 * remaining polheads). If we loop to 0, we finish. We 1957 * keep looping until we hit 0 or until we have a rule to 1958 * encode. 1959 * 1960 * NOTE: No need for ITP_REF*() macros here as we're only 1961 * going after and refholding the policy head itself. 1962 */ 1963 rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER); 1964 itp = spdsock_dump_iterate_next_tunnel(ss, ipss); 1965 if (itp == NULL) { 1966 rw_exit(&ipss->ipsec_tunnel_policy_lock); 1967 return (spdsock_dump_finish(ss, EAGAIN)); 1968 } 1969 1970 /* Reset other spdsock_dump thingies. */ 1971 IPPH_REFRELE(ss->spdsock_dump_head, ns); 1972 if (ss->spdsock_dump_active) { 1973 ss->spdsock_dump_tunnel = 1974 itp->itp_flags & ITPF_P_TUNNEL; 1975 iph = itp->itp_policy; 1976 } else { 1977 ss->spdsock_dump_tunnel = 1978 itp->itp_flags & ITPF_I_TUNNEL; 1979 iph = itp->itp_inactive; 1980 } 1981 IPPH_REFHOLD(iph); 1982 rw_exit(&ipss->ipsec_tunnel_policy_lock); 1983 1984 rw_enter(&iph->iph_lock, RW_READER); 1985 RESET_SPDSOCK_DUMP_POLHEAD(ss, iph); 1986 } 1987 1988 m = spdsock_encode_rule(ss->spdsock_dump_req, rule, 1989 ss->spdsock_dump_cur_type, ss->spdsock_dump_cur_af, 1990 (ss->spdsock_itp == NULL) ? NULL : ss->spdsock_itp->itp_name, 1991 ss->spdsock_dump_tunnel); 1992 rw_exit(&iph->iph_lock); 1993 1994 if (m == NULL) 1995 return (spdsock_dump_finish(ss, ENOMEM)); 1996 return (m); 1997 } 1998 1999 /* 2000 * Dump records until we run into flow-control back-pressure. 2001 */ 2002 static void 2003 spdsock_dump_some(queue_t *q, spdsock_t *ss) 2004 { 2005 mblk_t *m, *dataind; 2006 2007 while ((ss->spdsock_dump_req != NULL) && canputnext(q)) { 2008 m = spdsock_dump_next_record(ss); 2009 if (m == NULL) 2010 return; 2011 dataind = allocb(sizeof (struct T_data_req), BPRI_HI); 2012 if (dataind == NULL) { 2013 freemsg(m); 2014 return; 2015 } 2016 dataind->b_cont = m; 2017 dataind->b_wptr += sizeof (struct T_data_req); 2018 ((struct T_data_ind *)dataind->b_rptr)->PRIM_type = T_DATA_IND; 2019 ((struct T_data_ind *)dataind->b_rptr)->MORE_flag = 0; 2020 dataind->b_datap->db_type = M_PROTO; 2021 putnext(q, dataind); 2022 } 2023 } 2024 2025 /* 2026 * Start dumping. 2027 * Format a start-of-dump record, and set up the stream and kick the rsrv 2028 * procedure to continue the job.. 2029 */ 2030 /* ARGSUSED */ 2031 static void 2032 spdsock_dump(queue_t *q, ipsec_policy_head_t *iph, mblk_t *mp) 2033 { 2034 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2035 netstack_t *ns = ss->spdsock_spds->spds_netstack; 2036 ipsec_stack_t *ipss = ns->netstack_ipsec; 2037 mblk_t *mr; 2038 2039 /* spdsock_open() already set spdsock_itp to NULL. */ 2040 if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) { 2041 rw_enter(&ipss->ipsec_tunnel_policy_lock, RW_READER); 2042 ss->spdsock_dump_remaining_polheads = 1 + 2043 avl_numnodes(&ipss->ipsec_tunnel_policies); 2044 ss->spdsock_dump_tun_gen = ipss->ipsec_tunnel_policy_gen; 2045 rw_exit(&ipss->ipsec_tunnel_policy_lock); 2046 if (iph == ALL_ACTIVE_POLHEADS) { 2047 iph = ipsec_system_policy(ns); 2048 ss->spdsock_dump_active = B_TRUE; 2049 } else { 2050 iph = ipsec_inactive_policy(ns); 2051 ss->spdsock_dump_active = B_FALSE; 2052 } 2053 ASSERT(ss->spdsock_itp == NULL); 2054 } else { 2055 ss->spdsock_dump_remaining_polheads = 1; 2056 } 2057 2058 rw_enter(&iph->iph_lock, RW_READER); 2059 2060 mr = spdsock_dump_ruleset(mp, iph, 0, 0); 2061 2062 if (!mr) { 2063 rw_exit(&iph->iph_lock); 2064 spdsock_error(q, mp, ENOMEM, 0); 2065 return; 2066 } 2067 2068 ss->spdsock_dump_req = mp; 2069 RESET_SPDSOCK_DUMP_POLHEAD(ss, iph); 2070 2071 rw_exit(&iph->iph_lock); 2072 2073 qreply(q, mr); 2074 qenable(OTHERQ(q)); 2075 } 2076 2077 /* Do NOT consume a reference to ITP. */ 2078 void 2079 spdsock_clone_node(ipsec_tun_pol_t *itp, void *ep, netstack_t *ns) 2080 { 2081 int *errptr = (int *)ep; 2082 2083 if (*errptr != 0) 2084 return; /* We've failed already for some reason. */ 2085 mutex_enter(&itp->itp_lock); 2086 ITPF_CLONE(itp->itp_flags); 2087 *errptr = ipsec_copy_polhead(itp->itp_policy, itp->itp_inactive, ns); 2088 mutex_exit(&itp->itp_lock); 2089 } 2090 2091 void 2092 spdsock_clone(queue_t *q, mblk_t *mp, spd_if_t *tunname) 2093 { 2094 int error; 2095 char *tname; 2096 ipsec_tun_pol_t *itp; 2097 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2098 netstack_t *ns = ss->spdsock_spds->spds_netstack; 2099 2100 if (tunname != NULL) { 2101 tname = (char *)tunname->spd_if_name; 2102 if (*tname == '\0') { 2103 error = ipsec_clone_system_policy(ns); 2104 if (audit_active) { 2105 boolean_t active; 2106 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 2107 cred_t *cr; 2108 pid_t cpid; 2109 2110 cr = msg_getcred(mp, &cpid); 2111 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 2112 audit_pf_policy(SPD_CLONE, cr, ns, 2113 NULL, active, error, cpid); 2114 } 2115 if (error == 0) { 2116 itp_walk(spdsock_clone_node, &error, ns); 2117 if (audit_active) { 2118 boolean_t active; 2119 spd_msg_t *spmsg = 2120 (spd_msg_t *)mp->b_rptr; 2121 cred_t *cr; 2122 pid_t cpid; 2123 2124 cr = msg_getcred(mp, &cpid); 2125 active = (spmsg->spd_msg_spdid == 2126 SPD_ACTIVE); 2127 audit_pf_policy(SPD_CLONE, cr, 2128 ns, "all tunnels", active, 0, 2129 cpid); 2130 } 2131 } 2132 } else { 2133 itp = get_tunnel_policy(tname, ns); 2134 if (itp == NULL) { 2135 spdsock_error(q, mp, ENOENT, 0); 2136 if (audit_active) { 2137 boolean_t active; 2138 spd_msg_t *spmsg = 2139 (spd_msg_t *)mp->b_rptr; 2140 cred_t *cr; 2141 pid_t cpid; 2142 2143 cr = msg_getcred(mp, &cpid); 2144 active = (spmsg->spd_msg_spdid == 2145 SPD_ACTIVE); 2146 audit_pf_policy(SPD_CLONE, cr, 2147 ns, ITP_NAME(itp), active, ENOENT, 2148 cpid); 2149 } 2150 return; 2151 } 2152 spdsock_clone_node(itp, &error, NULL); 2153 ITP_REFRELE(itp, ns); 2154 if (audit_active) { 2155 boolean_t active; 2156 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 2157 cred_t *cr; 2158 pid_t cpid; 2159 2160 cr = msg_getcred(mp, &cpid); 2161 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 2162 audit_pf_policy(SPD_CLONE, cr, ns, 2163 ITP_NAME(itp), active, error, cpid); 2164 } 2165 } 2166 } else { 2167 error = ipsec_clone_system_policy(ns); 2168 if (audit_active) { 2169 boolean_t active; 2170 spd_msg_t *spmsg = (spd_msg_t *)mp->b_rptr; 2171 cred_t *cr; 2172 pid_t cpid; 2173 2174 cr = msg_getcred(mp, &cpid); 2175 active = (spmsg->spd_msg_spdid == SPD_ACTIVE); 2176 audit_pf_policy(SPD_CLONE, cr, ns, NULL, 2177 active, error, cpid); 2178 } 2179 } 2180 2181 if (error != 0) 2182 spdsock_error(q, mp, error, 0); 2183 else 2184 spd_echo(q, mp); 2185 } 2186 2187 /* 2188 * Process a SPD_ALGLIST request. The caller expects separate alg entries 2189 * for AH authentication, ESP authentication, and ESP encryption. 2190 * The same distinction is then used when setting the min and max key 2191 * sizes when defining policies. 2192 */ 2193 2194 #define SPDSOCK_AH_AUTH 0 2195 #define SPDSOCK_ESP_AUTH 1 2196 #define SPDSOCK_ESP_ENCR 2 2197 #define SPDSOCK_NTYPES 3 2198 2199 static const uint_t algattr[SPDSOCK_NTYPES] = { 2200 SPD_ATTR_AH_AUTH, 2201 SPD_ATTR_ESP_AUTH, 2202 SPD_ATTR_ESP_ENCR 2203 }; 2204 static const uint_t minbitsattr[SPDSOCK_NTYPES] = { 2205 SPD_ATTR_AH_MINBITS, 2206 SPD_ATTR_ESPA_MINBITS, 2207 SPD_ATTR_ENCR_MINBITS 2208 }; 2209 static const uint_t maxbitsattr[SPDSOCK_NTYPES] = { 2210 SPD_ATTR_AH_MAXBITS, 2211 SPD_ATTR_ESPA_MAXBITS, 2212 SPD_ATTR_ENCR_MAXBITS 2213 }; 2214 static const uint_t defbitsattr[SPDSOCK_NTYPES] = { 2215 SPD_ATTR_AH_DEFBITS, 2216 SPD_ATTR_ESPA_DEFBITS, 2217 SPD_ATTR_ENCR_DEFBITS 2218 }; 2219 static const uint_t incrbitsattr[SPDSOCK_NTYPES] = { 2220 SPD_ATTR_AH_INCRBITS, 2221 SPD_ATTR_ESPA_INCRBITS, 2222 SPD_ATTR_ENCR_INCRBITS 2223 }; 2224 2225 #define ATTRPERALG 6 /* fixed attributes per algs */ 2226 2227 void 2228 spdsock_alglist(queue_t *q, mblk_t *mp) 2229 { 2230 uint_t algtype; 2231 uint_t algidx; 2232 uint_t algcount; 2233 uint_t size; 2234 mblk_t *m; 2235 uint8_t *cur; 2236 spd_msg_t *msg; 2237 struct spd_ext_actions *act; 2238 struct spd_attribute *attr; 2239 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2240 ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec; 2241 2242 mutex_enter(&ipss->ipsec_alg_lock); 2243 /* 2244 * The SPD client expects to receive separate entries for 2245 * AH authentication and ESP authentication supported algorithms. 2246 * 2247 * Don't return the "any" algorithms, if defined, as no 2248 * kernel policies can be set for these algorithms. 2249 */ 2250 algcount = 2 * ipss->ipsec_nalgs[IPSEC_ALG_AUTH] + 2251 ipss->ipsec_nalgs[IPSEC_ALG_ENCR]; 2252 2253 if (ipss->ipsec_alglists[IPSEC_ALG_AUTH][SADB_AALG_NONE] != NULL) 2254 algcount--; 2255 if (ipss->ipsec_alglists[IPSEC_ALG_ENCR][SADB_EALG_NONE] != NULL) 2256 algcount--; 2257 2258 /* 2259 * For each algorithm, we encode: 2260 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT} 2261 */ 2262 2263 size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions) + 2264 ATTRPERALG * sizeof (struct spd_attribute) * algcount; 2265 2266 ASSERT(ALIGNED64(size)); 2267 2268 m = allocb(size, BPRI_HI); 2269 if (m == NULL) { 2270 mutex_exit(&ipss->ipsec_alg_lock); 2271 spdsock_error(q, mp, ENOMEM, 0); 2272 return; 2273 } 2274 2275 m->b_wptr = m->b_rptr + size; 2276 cur = m->b_rptr; 2277 2278 msg = (spd_msg_t *)cur; 2279 bcopy(mp->b_rptr, cur, sizeof (*msg)); 2280 2281 msg->spd_msg_len = SPD_8TO64(size); 2282 msg->spd_msg_errno = 0; 2283 msg->spd_msg_diagnostic = 0; 2284 2285 cur += sizeof (*msg); 2286 2287 act = (struct spd_ext_actions *)cur; 2288 cur += sizeof (*act); 2289 2290 act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t)); 2291 act->spd_actions_exttype = SPD_EXT_ACTION; 2292 act->spd_actions_count = algcount; 2293 act->spd_actions_reserved = 0; 2294 2295 attr = (struct spd_attribute *)cur; 2296 2297 #define EMIT(tag, value) { \ 2298 attr->spd_attr_tag = (tag); \ 2299 attr->spd_attr_value = (value); \ 2300 attr++; \ 2301 } 2302 2303 /* 2304 * If you change the number of EMIT's here, change 2305 * ATTRPERALG above to match 2306 */ 2307 #define EMITALGATTRS(_type) { \ 2308 EMIT(algattr[_type], algid); /* 1 */ \ 2309 EMIT(minbitsattr[_type], minbits); /* 2 */ \ 2310 EMIT(maxbitsattr[_type], maxbits); /* 3 */ \ 2311 EMIT(defbitsattr[_type], defbits); /* 4 */ \ 2312 EMIT(incrbitsattr[_type], incr); /* 5 */ \ 2313 EMIT(SPD_ATTR_NEXT, 0); /* 6 */ \ 2314 } 2315 2316 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 2317 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 2318 algidx++) { 2319 int algid = ipss->ipsec_sortlist[algtype][algidx]; 2320 ipsec_alginfo_t *alg = 2321 ipss->ipsec_alglists[algtype][algid]; 2322 uint_t minbits = alg->alg_minbits; 2323 uint_t maxbits = alg->alg_maxbits; 2324 uint_t defbits = alg->alg_default_bits; 2325 uint_t incr = alg->alg_increment; 2326 2327 if (algtype == IPSEC_ALG_AUTH) { 2328 if (algid == SADB_AALG_NONE) 2329 continue; 2330 EMITALGATTRS(SPDSOCK_AH_AUTH); 2331 EMITALGATTRS(SPDSOCK_ESP_AUTH); 2332 } else { 2333 if (algid == SADB_EALG_NONE) 2334 continue; 2335 ASSERT(algtype == IPSEC_ALG_ENCR); 2336 EMITALGATTRS(SPDSOCK_ESP_ENCR); 2337 } 2338 } 2339 } 2340 2341 mutex_exit(&ipss->ipsec_alg_lock); 2342 2343 #undef EMITALGATTRS 2344 #undef EMIT 2345 #undef ATTRPERALG 2346 2347 attr--; 2348 attr->spd_attr_tag = SPD_ATTR_END; 2349 2350 freemsg(mp); 2351 qreply(q, m); 2352 } 2353 2354 /* 2355 * Process a SPD_DUMPALGS request. 2356 */ 2357 2358 #define ATTRPERALG 7 /* fixed attributes per algs */ 2359 2360 void 2361 spdsock_dumpalgs(queue_t *q, mblk_t *mp) 2362 { 2363 uint_t algtype; 2364 uint_t algidx; 2365 uint_t size; 2366 mblk_t *m; 2367 uint8_t *cur; 2368 spd_msg_t *msg; 2369 struct spd_ext_actions *act; 2370 struct spd_attribute *attr; 2371 ipsec_alginfo_t *alg; 2372 uint_t algid; 2373 uint_t i; 2374 uint_t alg_size; 2375 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2376 ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec; 2377 2378 mutex_enter(&ipss->ipsec_alg_lock); 2379 2380 /* 2381 * For each algorithm, we encode: 2382 * ALG / MINBITS / MAXBITS / DEFBITS / INCRBITS / {END, NEXT} 2383 * 2384 * ALG_ID / ALG_PROTO / ALG_INCRBITS / ALG_NKEYSIZES / ALG_KEYSIZE* 2385 * ALG_NBLOCKSIZES / ALG_BLOCKSIZE* / ALG_MECHNAME / {END, NEXT} 2386 */ 2387 2388 /* 2389 * Compute the size of the SPD message. 2390 */ 2391 size = sizeof (spd_msg_t) + sizeof (struct spd_ext_actions); 2392 2393 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 2394 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 2395 algidx++) { 2396 algid = ipss->ipsec_sortlist[algtype][algidx]; 2397 alg = ipss->ipsec_alglists[algtype][algid]; 2398 alg_size = sizeof (struct spd_attribute) * 2399 (ATTRPERALG + alg->alg_nkey_sizes + 2400 alg->alg_nblock_sizes) + CRYPTO_MAX_MECH_NAME; 2401 size += alg_size; 2402 } 2403 } 2404 2405 ASSERT(ALIGNED64(size)); 2406 2407 m = allocb(size, BPRI_HI); 2408 if (m == NULL) { 2409 mutex_exit(&ipss->ipsec_alg_lock); 2410 spdsock_error(q, mp, ENOMEM, 0); 2411 return; 2412 } 2413 2414 m->b_wptr = m->b_rptr + size; 2415 cur = m->b_rptr; 2416 2417 msg = (spd_msg_t *)cur; 2418 bcopy(mp->b_rptr, cur, sizeof (*msg)); 2419 2420 msg->spd_msg_len = SPD_8TO64(size); 2421 msg->spd_msg_errno = 0; 2422 msg->spd_msg_diagnostic = 0; 2423 2424 cur += sizeof (*msg); 2425 2426 act = (struct spd_ext_actions *)cur; 2427 cur += sizeof (*act); 2428 2429 act->spd_actions_len = SPD_8TO64(size - sizeof (spd_msg_t)); 2430 act->spd_actions_exttype = SPD_EXT_ACTION; 2431 act->spd_actions_count = ipss->ipsec_nalgs[IPSEC_ALG_AUTH] + 2432 ipss->ipsec_nalgs[IPSEC_ALG_ENCR]; 2433 act->spd_actions_reserved = 0; 2434 2435 attr = (struct spd_attribute *)cur; 2436 2437 #define EMIT(tag, value) { \ 2438 attr->spd_attr_tag = (tag); \ 2439 attr->spd_attr_value = (value); \ 2440 attr++; \ 2441 } 2442 2443 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 2444 for (algidx = 0; algidx < ipss->ipsec_nalgs[algtype]; 2445 algidx++) { 2446 2447 algid = ipss->ipsec_sortlist[algtype][algidx]; 2448 alg = ipss->ipsec_alglists[algtype][algid]; 2449 2450 /* 2451 * If you change the number of EMIT's here, change 2452 * ATTRPERALG above to match 2453 */ 2454 EMIT(SPD_ATTR_ALG_ID, algid); 2455 EMIT(SPD_ATTR_ALG_PROTO, algproto[algtype]); 2456 EMIT(SPD_ATTR_ALG_INCRBITS, alg->alg_increment); 2457 2458 EMIT(SPD_ATTR_ALG_NKEYSIZES, alg->alg_nkey_sizes); 2459 for (i = 0; i < alg->alg_nkey_sizes; i++) 2460 EMIT(SPD_ATTR_ALG_KEYSIZE, 2461 alg->alg_key_sizes[i]); 2462 2463 EMIT(SPD_ATTR_ALG_NBLOCKSIZES, alg->alg_nblock_sizes); 2464 for (i = 0; i < alg->alg_nblock_sizes; i++) 2465 EMIT(SPD_ATTR_ALG_BLOCKSIZE, 2466 alg->alg_block_sizes[i]); 2467 2468 EMIT(SPD_ATTR_ALG_MECHNAME, CRYPTO_MAX_MECH_NAME); 2469 bcopy(alg->alg_mech_name, attr, CRYPTO_MAX_MECH_NAME); 2470 attr = (struct spd_attribute *)((char *)attr + 2471 CRYPTO_MAX_MECH_NAME); 2472 2473 EMIT(SPD_ATTR_NEXT, 0); 2474 } 2475 } 2476 2477 mutex_exit(&ipss->ipsec_alg_lock); 2478 2479 #undef EMITALGATTRS 2480 #undef EMIT 2481 #undef ATTRPERALG 2482 2483 attr--; 2484 attr->spd_attr_tag = SPD_ATTR_END; 2485 2486 freemsg(mp); 2487 qreply(q, m); 2488 } 2489 2490 /* 2491 * Do the actual work of processing an SPD_UPDATEALGS request. Can 2492 * be invoked either once IPsec is loaded on a cached request, or 2493 * when a request is received while IPsec is loaded. 2494 */ 2495 static void 2496 spdsock_do_updatealg(spd_ext_t *extv[], int *diag, spd_stack_t *spds) 2497 { 2498 struct spd_ext_actions *actp; 2499 struct spd_attribute *attr, *endattr; 2500 uint64_t *start, *end; 2501 ipsec_alginfo_t *alg = NULL; 2502 ipsec_algtype_t alg_type = 0; 2503 boolean_t skip_alg = B_TRUE, doing_proto = B_FALSE; 2504 uint_t i, cur_key, cur_block, algid; 2505 2506 *diag = -1; 2507 ASSERT(MUTEX_HELD(&spds->spds_alg_lock)); 2508 2509 /* parse the message, building the list of algorithms */ 2510 2511 actp = (struct spd_ext_actions *)extv[SPD_EXT_ACTION]; 2512 if (actp == NULL) { 2513 *diag = SPD_DIAGNOSTIC_NO_ACTION_EXT; 2514 return; 2515 } 2516 2517 start = (uint64_t *)actp; 2518 end = (start + actp->spd_actions_len); 2519 endattr = (struct spd_attribute *)end; 2520 attr = (struct spd_attribute *)&actp[1]; 2521 2522 bzero(spds->spds_algs, IPSEC_NALGTYPES * IPSEC_MAX_ALGS * 2523 sizeof (ipsec_alginfo_t *)); 2524 2525 alg = kmem_zalloc(sizeof (*alg), KM_SLEEP); 2526 2527 #define ALG_KEY_SIZES(a) (((a)->alg_nkey_sizes + 1) * sizeof (uint16_t)) 2528 #define ALG_BLOCK_SIZES(a) (((a)->alg_nblock_sizes + 1) * sizeof (uint16_t)) 2529 2530 while (attr < endattr) { 2531 switch (attr->spd_attr_tag) { 2532 case SPD_ATTR_NOP: 2533 case SPD_ATTR_EMPTY: 2534 break; 2535 case SPD_ATTR_END: 2536 attr = endattr; 2537 /* FALLTHRU */ 2538 case SPD_ATTR_NEXT: 2539 if (doing_proto) { 2540 doing_proto = B_FALSE; 2541 break; 2542 } 2543 if (skip_alg) { 2544 ipsec_alg_free(alg); 2545 } else { 2546 ipsec_alg_free( 2547 spds->spds_algs[alg_type][alg->alg_id]); 2548 spds->spds_algs[alg_type][alg->alg_id] = 2549 alg; 2550 } 2551 alg = kmem_zalloc(sizeof (*alg), KM_SLEEP); 2552 break; 2553 2554 case SPD_ATTR_ALG_ID: 2555 if (attr->spd_attr_value >= IPSEC_MAX_ALGS) { 2556 ss1dbg(spds, ("spdsock_do_updatealg: " 2557 "invalid alg id %d\n", 2558 attr->spd_attr_value)); 2559 *diag = SPD_DIAGNOSTIC_ALG_ID_RANGE; 2560 goto bail; 2561 } 2562 alg->alg_id = attr->spd_attr_value; 2563 break; 2564 2565 case SPD_ATTR_ALG_PROTO: 2566 /* find the alg type */ 2567 for (i = 0; i < NALGPROTOS; i++) 2568 if (algproto[i] == attr->spd_attr_value) 2569 break; 2570 skip_alg = (i == NALGPROTOS); 2571 if (!skip_alg) 2572 alg_type = i; 2573 break; 2574 2575 case SPD_ATTR_ALG_INCRBITS: 2576 alg->alg_increment = attr->spd_attr_value; 2577 break; 2578 2579 case SPD_ATTR_ALG_NKEYSIZES: 2580 if (alg->alg_key_sizes != NULL) { 2581 kmem_free(alg->alg_key_sizes, 2582 ALG_KEY_SIZES(alg)); 2583 } 2584 alg->alg_nkey_sizes = attr->spd_attr_value; 2585 /* 2586 * Allocate room for the trailing zero key size 2587 * value as well. 2588 */ 2589 alg->alg_key_sizes = kmem_zalloc(ALG_KEY_SIZES(alg), 2590 KM_SLEEP); 2591 cur_key = 0; 2592 break; 2593 2594 case SPD_ATTR_ALG_KEYSIZE: 2595 if (alg->alg_key_sizes == NULL || 2596 cur_key >= alg->alg_nkey_sizes) { 2597 ss1dbg(spds, ("spdsock_do_updatealg: " 2598 "too many key sizes\n")); 2599 *diag = SPD_DIAGNOSTIC_ALG_NUM_KEY_SIZES; 2600 goto bail; 2601 } 2602 alg->alg_key_sizes[cur_key++] = attr->spd_attr_value; 2603 break; 2604 2605 case SPD_ATTR_ALG_NBLOCKSIZES: 2606 if (alg->alg_block_sizes != NULL) { 2607 kmem_free(alg->alg_block_sizes, 2608 ALG_BLOCK_SIZES(alg)); 2609 } 2610 alg->alg_nblock_sizes = attr->spd_attr_value; 2611 /* 2612 * Allocate room for the trailing zero block size 2613 * value as well. 2614 */ 2615 alg->alg_block_sizes = kmem_zalloc(ALG_BLOCK_SIZES(alg), 2616 KM_SLEEP); 2617 cur_block = 0; 2618 break; 2619 2620 case SPD_ATTR_ALG_BLOCKSIZE: 2621 if (alg->alg_block_sizes == NULL || 2622 cur_block >= alg->alg_nblock_sizes) { 2623 ss1dbg(spds, ("spdsock_do_updatealg: " 2624 "too many block sizes\n")); 2625 *diag = SPD_DIAGNOSTIC_ALG_NUM_BLOCK_SIZES; 2626 goto bail; 2627 } 2628 alg->alg_block_sizes[cur_block++] = 2629 attr->spd_attr_value; 2630 break; 2631 2632 case SPD_ATTR_ALG_MECHNAME: { 2633 char *mech_name; 2634 2635 if (attr->spd_attr_value > CRYPTO_MAX_MECH_NAME) { 2636 ss1dbg(spds, ("spdsock_do_updatealg: " 2637 "mech name too long\n")); 2638 *diag = SPD_DIAGNOSTIC_ALG_MECH_NAME_LEN; 2639 goto bail; 2640 } 2641 mech_name = (char *)(attr + 1); 2642 bcopy(mech_name, alg->alg_mech_name, 2643 attr->spd_attr_value); 2644 alg->alg_mech_name[CRYPTO_MAX_MECH_NAME-1] = '\0'; 2645 attr = (struct spd_attribute *)((char *)attr + 2646 attr->spd_attr_value); 2647 break; 2648 } 2649 2650 case SPD_ATTR_PROTO_ID: 2651 doing_proto = B_TRUE; 2652 for (i = 0; i < NALGPROTOS; i++) { 2653 if (algproto[i] == attr->spd_attr_value) { 2654 alg_type = i; 2655 break; 2656 } 2657 } 2658 break; 2659 2660 case SPD_ATTR_PROTO_EXEC_MODE: 2661 if (!doing_proto) 2662 break; 2663 for (i = 0; i < NEXECMODES; i++) { 2664 if (execmodes[i] == attr->spd_attr_value) { 2665 spds->spds_algs_exec_mode[alg_type] = i; 2666 break; 2667 } 2668 } 2669 break; 2670 } 2671 attr++; 2672 } 2673 2674 #undef ALG_KEY_SIZES 2675 #undef ALG_BLOCK_SIZES 2676 2677 /* update the algorithm tables */ 2678 spdsock_merge_algs(spds); 2679 bail: 2680 /* cleanup */ 2681 ipsec_alg_free(alg); 2682 for (alg_type = 0; alg_type < IPSEC_NALGTYPES; alg_type++) 2683 for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) 2684 if (spds->spds_algs[alg_type][algid] != NULL) 2685 ipsec_alg_free(spds->spds_algs[alg_type][algid]); 2686 } 2687 2688 /* 2689 * Process an SPD_UPDATEALGS request. If IPsec is not loaded, queue 2690 * the request until IPsec loads. If IPsec is loaded, act on it 2691 * immediately. 2692 */ 2693 2694 static void 2695 spdsock_updatealg(queue_t *q, mblk_t *mp, spd_ext_t *extv[]) 2696 { 2697 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2698 spd_stack_t *spds = ss->spdsock_spds; 2699 ipsec_stack_t *ipss = spds->spds_netstack->netstack_ipsec; 2700 2701 if (!ipsec_loaded(ipss)) { 2702 /* 2703 * IPsec is not loaded, save request and return nicely, 2704 * the message will be processed once IPsec loads. 2705 */ 2706 mblk_t *new_mp; 2707 2708 /* last update message wins */ 2709 if ((new_mp = copymsg(mp)) == NULL) { 2710 spdsock_error(q, mp, ENOMEM, 0); 2711 return; 2712 } 2713 mutex_enter(&spds->spds_alg_lock); 2714 bcopy(extv, spds->spds_extv_algs, 2715 sizeof (spd_ext_t *) * (SPD_EXT_MAX + 1)); 2716 if (spds->spds_mp_algs != NULL) 2717 freemsg(spds->spds_mp_algs); 2718 spds->spds_mp_algs = mp; 2719 spds->spds_algs_pending = B_TRUE; 2720 mutex_exit(&spds->spds_alg_lock); 2721 if (audit_active) { 2722 cred_t *cr; 2723 pid_t cpid; 2724 2725 cr = msg_getcred(mp, &cpid); 2726 audit_pf_policy(SPD_UPDATEALGS, cr, 2727 spds->spds_netstack, NULL, B_TRUE, EAGAIN, 2728 cpid); 2729 } 2730 spd_echo(q, new_mp); 2731 } else { 2732 /* 2733 * IPsec is loaded, act on the message immediately. 2734 */ 2735 int diag; 2736 2737 mutex_enter(&spds->spds_alg_lock); 2738 spdsock_do_updatealg(extv, &diag, spds); 2739 mutex_exit(&spds->spds_alg_lock); 2740 if (diag == -1) { 2741 spd_echo(q, mp); 2742 if (audit_active) { 2743 cred_t *cr; 2744 pid_t cpid; 2745 2746 cr = msg_getcred(mp, &cpid); 2747 audit_pf_policy(SPD_UPDATEALGS, cr, 2748 spds->spds_netstack, NULL, B_TRUE, 0, 2749 cpid); 2750 } 2751 } else { 2752 spdsock_diag(q, mp, diag); 2753 if (audit_active) { 2754 cred_t *cr; 2755 pid_t cpid; 2756 2757 cr = msg_getcred(mp, &cpid); 2758 audit_pf_policy(SPD_UPDATEALGS, cr, 2759 spds->spds_netstack, NULL, B_TRUE, diag, 2760 cpid); 2761 } 2762 } 2763 } 2764 } 2765 2766 /* 2767 * With a reference-held ill, dig down and find an instance of "tun", and 2768 * assign its tunnel policy pointer, while reference-holding it. Also, 2769 * release ill's refrence when finished. 2770 * 2771 * We'll be messing with q_next, so be VERY careful. 2772 */ 2773 static void 2774 find_tun_and_set_itp(ill_t *ill, ipsec_tun_pol_t *itp) 2775 { 2776 queue_t *q; 2777 tun_t *tun; 2778 2779 /* Don't bother if this ill is going away. */ 2780 if (ill->ill_flags & ILL_CONDEMNED) { 2781 ill_refrele(ill); 2782 return; 2783 } 2784 2785 2786 q = ill->ill_wq; 2787 claimstr(q); /* Lighter-weight than freezestr(). */ 2788 2789 do { 2790 /* Use strcmp() because "tun" is bounded. */ 2791 if (strcmp(q->q_qinfo->qi_minfo->mi_idname, "tun") == 0) { 2792 /* Aha! Got it. */ 2793 tun = (tun_t *)q->q_ptr; 2794 if (tun != NULL) { 2795 mutex_enter(&tun->tun_lock); 2796 if (tun->tun_itp != itp) { 2797 ASSERT(tun->tun_itp == NULL); 2798 ITP_REFHOLD(itp); 2799 tun->tun_itp = itp; 2800 } 2801 mutex_exit(&tun->tun_lock); 2802 goto release_and_return; 2803 } 2804 /* 2805 * Else assume this is some other module named "tun" 2806 * and move on, hoping we find one that actually has 2807 * something in q_ptr. 2808 */ 2809 } 2810 q = q->q_next; 2811 } while (q != NULL); 2812 2813 release_and_return: 2814 releasestr(ill->ill_wq); 2815 ill_refrele(ill); 2816 } 2817 2818 /* 2819 * Sort through the mess of polhead options to retrieve an appropriate one. 2820 * Returns NULL if we send an spdsock error. Returns a valid pointer if we 2821 * found a valid polhead. Returns ALL_ACTIVE_POLHEADS (aka. -1) or 2822 * ALL_INACTIVE_POLHEADS (aka. -2) if the operation calls for the operation to 2823 * act on ALL policy heads. 2824 */ 2825 static ipsec_policy_head_t * 2826 get_appropriate_polhead(queue_t *q, mblk_t *mp, spd_if_t *tunname, int spdid, 2827 int msgtype, ipsec_tun_pol_t **itpp) 2828 { 2829 ipsec_tun_pol_t *itp; 2830 ipsec_policy_head_t *iph; 2831 int errno; 2832 char *tname; 2833 boolean_t active; 2834 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2835 netstack_t *ns = ss->spdsock_spds->spds_netstack; 2836 uint64_t gen; /* Placeholder */ 2837 ill_t *v4, *v6; 2838 2839 active = (spdid == SPD_ACTIVE); 2840 *itpp = NULL; 2841 if (!active && spdid != SPD_STANDBY) { 2842 spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_SPDID); 2843 return (NULL); 2844 } 2845 2846 if (tunname != NULL) { 2847 /* Acting on a tunnel's SPD. */ 2848 tname = (char *)tunname->spd_if_name; 2849 if (*tname == '\0') { 2850 /* Handle all-polhead cases here. */ 2851 if (msgtype != SPD_FLUSH && msgtype != SPD_DUMP) { 2852 spdsock_diag(q, mp, 2853 SPD_DIAGNOSTIC_NOT_GLOBAL_OP); 2854 return (NULL); 2855 } 2856 return (active ? ALL_ACTIVE_POLHEADS : 2857 ALL_INACTIVE_POLHEADS); 2858 } 2859 2860 itp = get_tunnel_policy(tname, ns); 2861 if (itp == NULL) { 2862 if (msgtype != SPD_ADDRULE) { 2863 /* "Tunnel not found" */ 2864 spdsock_error(q, mp, ENOENT, 0); 2865 return (NULL); 2866 } 2867 2868 errno = 0; 2869 itp = create_tunnel_policy(tname, &errno, &gen, ns); 2870 if (itp == NULL) { 2871 /* 2872 * Something very bad happened, most likely 2873 * ENOMEM. Return an indicator. 2874 */ 2875 spdsock_error(q, mp, errno, 0); 2876 return (NULL); 2877 } 2878 } 2879 /* 2880 * Troll the plumbed tunnels and see if we have a 2881 * match. We need to do this always in case we add 2882 * policy AFTER plumbing a tunnel. 2883 */ 2884 v4 = ill_lookup_on_name(tname, B_FALSE, B_FALSE, NULL, 2885 NULL, NULL, &errno, NULL, ns->netstack_ip); 2886 if (v4 != NULL) 2887 find_tun_and_set_itp(v4, itp); 2888 v6 = ill_lookup_on_name(tname, B_FALSE, B_TRUE, NULL, 2889 NULL, NULL, &errno, NULL, ns->netstack_ip); 2890 if (v6 != NULL) 2891 find_tun_and_set_itp(v6, itp); 2892 ASSERT(itp != NULL); 2893 *itpp = itp; 2894 /* For spdsock dump state, set the polhead's name. */ 2895 if (msgtype == SPD_DUMP) { 2896 ITP_REFHOLD(itp); 2897 ss->spdsock_itp = itp; 2898 ss->spdsock_dump_tunnel = itp->itp_flags & 2899 (active ? ITPF_P_TUNNEL : ITPF_I_TUNNEL); 2900 } 2901 } else { 2902 itp = NULL; 2903 /* For spdsock dump state, indicate it's global policy. */ 2904 if (msgtype == SPD_DUMP) 2905 ss->spdsock_itp = NULL; 2906 } 2907 2908 if (active) 2909 iph = (itp == NULL) ? ipsec_system_policy(ns) : itp->itp_policy; 2910 else 2911 iph = (itp == NULL) ? ipsec_inactive_policy(ns) : 2912 itp->itp_inactive; 2913 2914 ASSERT(iph != NULL); 2915 if (itp != NULL) { 2916 IPPH_REFHOLD(iph); 2917 } 2918 2919 return (iph); 2920 } 2921 2922 static void 2923 spdsock_parse(queue_t *q, mblk_t *mp) 2924 { 2925 spd_msg_t *spmsg; 2926 spd_ext_t *extv[SPD_EXT_MAX + 1]; 2927 uint_t msgsize; 2928 ipsec_policy_head_t *iph; 2929 ipsec_tun_pol_t *itp; 2930 spd_if_t *tunname; 2931 spdsock_t *ss = (spdsock_t *)q->q_ptr; 2932 spd_stack_t *spds = ss->spdsock_spds; 2933 netstack_t *ns = spds->spds_netstack; 2934 ipsec_stack_t *ipss = ns->netstack_ipsec; 2935 2936 /* Make sure nothing's below me. */ 2937 ASSERT(WR(q)->q_next == NULL); 2938 2939 spmsg = (spd_msg_t *)mp->b_rptr; 2940 2941 msgsize = SPD_64TO8(spmsg->spd_msg_len); 2942 2943 if (msgdsize(mp) != msgsize) { 2944 /* 2945 * Message len incorrect w.r.t. actual size. Send an error 2946 * (EMSGSIZE). It may be necessary to massage things a 2947 * bit. For example, if the spd_msg_type is hosed, 2948 * I need to set it to SPD_RESERVED to get delivery to 2949 * do the right thing. Then again, maybe just letting 2950 * the error delivery do the right thing. 2951 */ 2952 ss2dbg(spds, 2953 ("mblk (%lu) and base (%d) message sizes don't jibe.\n", 2954 msgdsize(mp), msgsize)); 2955 spdsock_error(q, mp, EMSGSIZE, SPD_DIAGNOSTIC_NONE); 2956 return; 2957 } 2958 2959 if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) { 2960 /* Get all message into one mblk. */ 2961 if (pullupmsg(mp, -1) == 0) { 2962 /* 2963 * Something screwy happened. 2964 */ 2965 ss3dbg(spds, ("spdsock_parse: pullupmsg() failed.\n")); 2966 return; 2967 } else { 2968 spmsg = (spd_msg_t *)mp->b_rptr; 2969 } 2970 } 2971 2972 switch (spdsock_get_ext(extv, spmsg, msgsize)) { 2973 case KGE_DUP: 2974 /* Handle duplicate extension. */ 2975 ss1dbg(spds, ("Got duplicate extension of type %d.\n", 2976 extv[0]->spd_ext_type)); 2977 spdsock_diag(q, mp, dup_ext_diag[extv[0]->spd_ext_type]); 2978 return; 2979 case KGE_UNK: 2980 /* Handle unknown extension. */ 2981 ss1dbg(spds, ("Got unknown extension of type %d.\n", 2982 extv[0]->spd_ext_type)); 2983 spdsock_diag(q, mp, SPD_DIAGNOSTIC_UNKNOWN_EXT); 2984 return; 2985 case KGE_LEN: 2986 /* Length error. */ 2987 ss1dbg(spds, ("Length %d on extension type %d overrun or 0.\n", 2988 extv[0]->spd_ext_len, extv[0]->spd_ext_type)); 2989 spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_EXTLEN); 2990 return; 2991 case KGE_CHK: 2992 /* Reality check failed. */ 2993 ss1dbg(spds, ("Reality check failed on extension type %d.\n", 2994 extv[0]->spd_ext_type)); 2995 spdsock_diag(q, mp, bad_ext_diag[extv[0]->spd_ext_type]); 2996 return; 2997 default: 2998 /* Default case is no errors. */ 2999 break; 3000 } 3001 3002 /* 3003 * Special-case SPD_UPDATEALGS so as not to load IPsec. 3004 */ 3005 if (!ipsec_loaded(ipss) && spmsg->spd_msg_type != SPD_UPDATEALGS) { 3006 spdsock_t *ss = (spdsock_t *)q->q_ptr; 3007 3008 ASSERT(ss != NULL); 3009 ipsec_loader_loadnow(ipss); 3010 ss->spdsock_timeout_arg = mp; 3011 ss->spdsock_timeout = qtimeout(q, spdsock_loadcheck, 3012 q, LOADCHECK_INTERVAL); 3013 return; 3014 } 3015 3016 /* First check for messages that need no polheads at all. */ 3017 switch (spmsg->spd_msg_type) { 3018 case SPD_UPDATEALGS: 3019 spdsock_updatealg(q, mp, extv); 3020 return; 3021 case SPD_ALGLIST: 3022 spdsock_alglist(q, mp); 3023 return; 3024 case SPD_DUMPALGS: 3025 spdsock_dumpalgs(q, mp); 3026 return; 3027 } 3028 3029 /* 3030 * Then check for ones that need both primary/secondary polheads, 3031 * finding the appropriate tunnel policy if need be. 3032 */ 3033 tunname = (spd_if_t *)extv[SPD_EXT_TUN_NAME]; 3034 switch (spmsg->spd_msg_type) { 3035 case SPD_FLIP: 3036 spdsock_flip(q, mp, tunname); 3037 return; 3038 case SPD_CLONE: 3039 spdsock_clone(q, mp, tunname); 3040 return; 3041 } 3042 3043 /* 3044 * Finally, find ones that operate on exactly one polhead, or 3045 * "all polheads" of a given type (active/inactive). 3046 */ 3047 iph = get_appropriate_polhead(q, mp, tunname, spmsg->spd_msg_spdid, 3048 spmsg->spd_msg_type, &itp); 3049 if (iph == NULL) 3050 return; 3051 3052 /* All-polheads-ready operations. */ 3053 switch (spmsg->spd_msg_type) { 3054 case SPD_FLUSH: 3055 if (itp != NULL) { 3056 mutex_enter(&itp->itp_lock); 3057 if (spmsg->spd_msg_spdid == SPD_ACTIVE) 3058 itp->itp_flags &= ~ITPF_PFLAGS; 3059 else 3060 itp->itp_flags &= ~ITPF_IFLAGS; 3061 mutex_exit(&itp->itp_lock); 3062 ITP_REFRELE(itp, ns); 3063 } 3064 spdsock_flush(q, iph, itp, mp); 3065 return; 3066 case SPD_DUMP: 3067 if (itp != NULL) 3068 ITP_REFRELE(itp, ns); 3069 spdsock_dump(q, iph, mp); 3070 return; 3071 } 3072 3073 if (iph == ALL_ACTIVE_POLHEADS || iph == ALL_INACTIVE_POLHEADS) { 3074 spdsock_diag(q, mp, SPD_DIAGNOSTIC_NOT_GLOBAL_OP); 3075 return; 3076 } 3077 3078 /* Single-polhead-only operations. */ 3079 switch (spmsg->spd_msg_type) { 3080 case SPD_ADDRULE: 3081 spdsock_addrule(q, iph, mp, extv, itp); 3082 break; 3083 case SPD_DELETERULE: 3084 spdsock_deleterule(q, iph, mp, extv, itp); 3085 break; 3086 case SPD_LOOKUP: 3087 spdsock_lookup(q, iph, mp, extv, itp); 3088 break; 3089 default: 3090 spdsock_diag(q, mp, SPD_DIAGNOSTIC_BAD_MSG_TYPE); 3091 break; 3092 } 3093 3094 IPPH_REFRELE(iph, ns); 3095 if (itp != NULL) 3096 ITP_REFRELE(itp, ns); 3097 } 3098 3099 /* 3100 * If an algorithm mapping was received before IPsec was loaded, process it. 3101 * Called from the IPsec loader. 3102 */ 3103 void 3104 spdsock_update_pending_algs(netstack_t *ns) 3105 { 3106 spd_stack_t *spds = ns->netstack_spdsock; 3107 3108 mutex_enter(&spds->spds_alg_lock); 3109 if (spds->spds_algs_pending) { 3110 int diag; 3111 3112 spdsock_do_updatealg(spds->spds_extv_algs, &diag, 3113 spds); 3114 spds->spds_algs_pending = B_FALSE; 3115 } 3116 mutex_exit(&spds->spds_alg_lock); 3117 } 3118 3119 static void 3120 spdsock_loadcheck(void *arg) 3121 { 3122 queue_t *q = (queue_t *)arg; 3123 spdsock_t *ss = (spdsock_t *)q->q_ptr; 3124 mblk_t *mp; 3125 ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec; 3126 3127 ASSERT(ss != NULL); 3128 3129 ss->spdsock_timeout = 0; 3130 mp = ss->spdsock_timeout_arg; 3131 ASSERT(mp != NULL); 3132 ss->spdsock_timeout_arg = NULL; 3133 if (ipsec_failed(ipss)) 3134 spdsock_error(q, mp, EPROTONOSUPPORT, 0); 3135 else 3136 spdsock_parse(q, mp); 3137 } 3138 3139 /* 3140 * Copy relevant state bits. 3141 */ 3142 static void 3143 spdsock_copy_info(struct T_info_ack *tap, spdsock_t *ss) 3144 { 3145 *tap = spdsock_g_t_info_ack; 3146 tap->CURRENT_state = ss->spdsock_state; 3147 tap->OPT_size = spdsock_max_optsize; 3148 } 3149 3150 /* 3151 * This routine responds to T_CAPABILITY_REQ messages. It is called by 3152 * spdsock_wput. Much of the T_CAPABILITY_ACK information is copied from 3153 * spdsock_g_t_info_ack. The current state of the stream is copied from 3154 * spdsock_state. 3155 */ 3156 static void 3157 spdsock_capability_req(queue_t *q, mblk_t *mp) 3158 { 3159 spdsock_t *ss = (spdsock_t *)q->q_ptr; 3160 t_uscalar_t cap_bits1; 3161 struct T_capability_ack *tcap; 3162 3163 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 3164 3165 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 3166 mp->b_datap->db_type, T_CAPABILITY_ACK); 3167 if (mp == NULL) 3168 return; 3169 3170 tcap = (struct T_capability_ack *)mp->b_rptr; 3171 tcap->CAP_bits1 = 0; 3172 3173 if (cap_bits1 & TC1_INFO) { 3174 spdsock_copy_info(&tcap->INFO_ack, ss); 3175 tcap->CAP_bits1 |= TC1_INFO; 3176 } 3177 3178 qreply(q, mp); 3179 } 3180 3181 /* 3182 * This routine responds to T_INFO_REQ messages. It is called by 3183 * spdsock_wput_other. 3184 * Most of the T_INFO_ACK information is copied from spdsock_g_t_info_ack. 3185 * The current state of the stream is copied from spdsock_state. 3186 */ 3187 static void 3188 spdsock_info_req(q, mp) 3189 queue_t *q; 3190 mblk_t *mp; 3191 { 3192 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 3193 T_INFO_ACK); 3194 if (mp == NULL) 3195 return; 3196 spdsock_copy_info((struct T_info_ack *)mp->b_rptr, 3197 (spdsock_t *)q->q_ptr); 3198 qreply(q, mp); 3199 } 3200 3201 /* 3202 * spdsock_err_ack. This routine creates a 3203 * T_ERROR_ACK message and passes it 3204 * upstream. 3205 */ 3206 static void 3207 spdsock_err_ack(q, mp, t_error, sys_error) 3208 queue_t *q; 3209 mblk_t *mp; 3210 int t_error; 3211 int sys_error; 3212 { 3213 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 3214 qreply(q, mp); 3215 } 3216 3217 /* 3218 * This routine retrieves the current status of socket options. 3219 * It returns the size of the option retrieved. 3220 */ 3221 /* ARGSUSED */ 3222 int 3223 spdsock_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 3224 { 3225 int *i1 = (int *)ptr; 3226 3227 switch (level) { 3228 case SOL_SOCKET: 3229 switch (name) { 3230 case SO_TYPE: 3231 *i1 = SOCK_RAW; 3232 break; 3233 /* 3234 * The following two items can be manipulated, 3235 * but changing them should do nothing. 3236 */ 3237 case SO_SNDBUF: 3238 *i1 = (int)q->q_hiwat; 3239 break; 3240 case SO_RCVBUF: 3241 *i1 = (int)(RD(q)->q_hiwat); 3242 break; 3243 } 3244 break; 3245 default: 3246 return (0); 3247 } 3248 return (sizeof (int)); 3249 } 3250 3251 /* 3252 * This routine sets socket options. 3253 */ 3254 /* ARGSUSED */ 3255 int 3256 spdsock_opt_set(queue_t *q, uint_t mgmt_flags, int level, int name, 3257 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 3258 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 3259 { 3260 int *i1 = (int *)invalp; 3261 spdsock_t *ss = (spdsock_t *)q->q_ptr; 3262 spd_stack_t *spds = ss->spdsock_spds; 3263 3264 switch (level) { 3265 case SOL_SOCKET: 3266 switch (name) { 3267 case SO_SNDBUF: 3268 if (*i1 > spds->spds_max_buf) 3269 return (ENOBUFS); 3270 q->q_hiwat = *i1; 3271 break; 3272 case SO_RCVBUF: 3273 if (*i1 > spds->spds_max_buf) 3274 return (ENOBUFS); 3275 RD(q)->q_hiwat = *i1; 3276 (void) proto_set_rx_hiwat(RD(q), NULL, *i1); 3277 break; 3278 } 3279 break; 3280 } 3281 return (0); 3282 } 3283 3284 3285 /* 3286 * Handle STREAMS messages. 3287 */ 3288 static void 3289 spdsock_wput_other(queue_t *q, mblk_t *mp) 3290 { 3291 struct iocblk *iocp; 3292 int error; 3293 spdsock_t *ss = (spdsock_t *)q->q_ptr; 3294 spd_stack_t *spds = ss->spdsock_spds; 3295 cred_t *cr; 3296 3297 switch (mp->b_datap->db_type) { 3298 case M_PROTO: 3299 case M_PCPROTO: 3300 if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) { 3301 ss3dbg(spds, ( 3302 "spdsock_wput_other: Not big enough M_PROTO\n")); 3303 freemsg(mp); 3304 return; 3305 } 3306 switch (((union T_primitives *)mp->b_rptr)->type) { 3307 case T_CAPABILITY_REQ: 3308 spdsock_capability_req(q, mp); 3309 break; 3310 case T_INFO_REQ: 3311 spdsock_info_req(q, mp); 3312 break; 3313 case T_SVR4_OPTMGMT_REQ: 3314 case T_OPTMGMT_REQ: 3315 /* 3316 * All Solaris components should pass a db_credp 3317 * for this TPI message, hence we ASSERT. 3318 * But in case there is some other M_PROTO that looks 3319 * like a TPI message sent by some other kernel 3320 * component, we check and return an error. 3321 */ 3322 cr = msg_getcred(mp, NULL); 3323 ASSERT(cr != NULL); 3324 if (cr == NULL) { 3325 spdsock_err_ack(q, mp, TSYSERR, EINVAL); 3326 return; 3327 } 3328 if (((union T_primitives *)mp->b_rptr)->type == 3329 T_SVR4_OPTMGMT_REQ) { 3330 (void) svr4_optcom_req(q, mp, cr, 3331 &spdsock_opt_obj, B_FALSE); 3332 } else { 3333 (void) tpi_optcom_req(q, mp, cr, 3334 &spdsock_opt_obj, B_FALSE); 3335 } 3336 break; 3337 case T_DATA_REQ: 3338 case T_EXDATA_REQ: 3339 case T_ORDREL_REQ: 3340 /* Illegal for spdsock. */ 3341 freemsg(mp); 3342 (void) putnextctl1(RD(q), M_ERROR, EPROTO); 3343 break; 3344 default: 3345 /* Not supported by spdsock. */ 3346 spdsock_err_ack(q, mp, TNOTSUPPORT, 0); 3347 break; 3348 } 3349 return; 3350 case M_IOCTL: 3351 iocp = (struct iocblk *)mp->b_rptr; 3352 error = EINVAL; 3353 3354 switch (iocp->ioc_cmd) { 3355 case ND_SET: 3356 case ND_GET: 3357 if (nd_getset(q, spds->spds_g_nd, mp)) { 3358 qreply(q, mp); 3359 return; 3360 } else 3361 error = ENOENT; 3362 /* FALLTHRU */ 3363 default: 3364 miocnak(q, mp, 0, error); 3365 return; 3366 } 3367 case M_FLUSH: 3368 if (*mp->b_rptr & FLUSHW) { 3369 flushq(q, FLUSHALL); 3370 *mp->b_rptr &= ~FLUSHW; 3371 } 3372 if (*mp->b_rptr & FLUSHR) { 3373 qreply(q, mp); 3374 return; 3375 } 3376 /* Else FALLTHRU */ 3377 } 3378 3379 /* If fell through, just black-hole the message. */ 3380 freemsg(mp); 3381 } 3382 3383 static void 3384 spdsock_wput(queue_t *q, mblk_t *mp) 3385 { 3386 uint8_t *rptr = mp->b_rptr; 3387 mblk_t *mp1; 3388 spdsock_t *ss = (spdsock_t *)q->q_ptr; 3389 spd_stack_t *spds = ss->spdsock_spds; 3390 3391 /* 3392 * If we're dumping, defer processing other messages until the 3393 * dump completes. 3394 */ 3395 if (ss->spdsock_dump_req != NULL) { 3396 if (!putq(q, mp)) 3397 freemsg(mp); 3398 return; 3399 } 3400 3401 switch (mp->b_datap->db_type) { 3402 case M_DATA: 3403 /* 3404 * Silently discard. 3405 */ 3406 ss2dbg(spds, ("raw M_DATA in spdsock.\n")); 3407 freemsg(mp); 3408 return; 3409 case M_PROTO: 3410 case M_PCPROTO: 3411 if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) { 3412 if (((union T_primitives *)rptr)->type == T_DATA_REQ) { 3413 if ((mp1 = mp->b_cont) == NULL) { 3414 /* No data after T_DATA_REQ. */ 3415 ss2dbg(spds, 3416 ("No data after DATA_REQ.\n")); 3417 freemsg(mp); 3418 return; 3419 } 3420 freeb(mp); 3421 mp = mp1; 3422 ss2dbg(spds, ("T_DATA_REQ\n")); 3423 break; /* Out of switch. */ 3424 } 3425 } 3426 /* FALLTHRU */ 3427 default: 3428 ss3dbg(spds, ("In default wput case (%d %d).\n", 3429 mp->b_datap->db_type, ((union T_primitives *)rptr)->type)); 3430 spdsock_wput_other(q, mp); 3431 return; 3432 } 3433 3434 /* I now have a PF_POLICY message in an M_DATA block. */ 3435 spdsock_parse(q, mp); 3436 } 3437 3438 /* 3439 * Device open procedure, called when new queue pair created. 3440 * We are passed the read-side queue. 3441 */ 3442 /* ARGSUSED */ 3443 static int 3444 spdsock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 3445 { 3446 spdsock_t *ss; 3447 queue_t *oq = OTHERQ(q); 3448 minor_t ssminor; 3449 netstack_t *ns; 3450 spd_stack_t *spds; 3451 3452 if (secpolicy_ip_config(credp, B_FALSE) != 0) 3453 return (EPERM); 3454 3455 if (q->q_ptr != NULL) 3456 return (0); /* Re-open of an already open instance. */ 3457 3458 if (sflag & MODOPEN) 3459 return (EINVAL); 3460 3461 ns = netstack_find_by_cred(credp); 3462 ASSERT(ns != NULL); 3463 spds = ns->netstack_spdsock; 3464 ASSERT(spds != NULL); 3465 3466 ss2dbg(spds, ("Made it into PF_POLICY socket open.\n")); 3467 3468 ssminor = (minor_t)(uintptr_t)vmem_alloc(spdsock_vmem, 1, VM_NOSLEEP); 3469 if (ssminor == 0) { 3470 netstack_rele(spds->spds_netstack); 3471 return (ENOMEM); 3472 } 3473 ss = kmem_zalloc(sizeof (spdsock_t), KM_NOSLEEP); 3474 if (ss == NULL) { 3475 vmem_free(spdsock_vmem, (void *)(uintptr_t)ssminor, 1); 3476 netstack_rele(spds->spds_netstack); 3477 return (ENOMEM); 3478 } 3479 3480 ss->spdsock_minor = ssminor; 3481 ss->spdsock_state = TS_UNBND; 3482 ss->spdsock_dump_req = NULL; 3483 3484 ss->spdsock_spds = spds; 3485 3486 q->q_ptr = ss; 3487 oq->q_ptr = ss; 3488 3489 q->q_hiwat = spds->spds_recv_hiwat; 3490 3491 oq->q_hiwat = spds->spds_xmit_hiwat; 3492 oq->q_lowat = spds->spds_xmit_lowat; 3493 3494 qprocson(q); 3495 (void) proto_set_rx_hiwat(q, NULL, spds->spds_recv_hiwat); 3496 3497 *devp = makedevice(getmajor(*devp), ss->spdsock_minor); 3498 return (0); 3499 } 3500 3501 /* 3502 * Read-side service procedure, invoked when we get back-enabled 3503 * when buffer space becomes available. 3504 * 3505 * Dump another chunk if we were dumping before; when we finish, kick 3506 * the write-side queue in case it's waiting for read queue space. 3507 */ 3508 void 3509 spdsock_rsrv(queue_t *q) 3510 { 3511 spdsock_t *ss = q->q_ptr; 3512 3513 if (ss->spdsock_dump_req != NULL) 3514 spdsock_dump_some(q, ss); 3515 3516 if (ss->spdsock_dump_req == NULL) 3517 qenable(OTHERQ(q)); 3518 } 3519 3520 /* 3521 * Write-side service procedure, invoked when we defer processing 3522 * if another message is received while a dump is in progress. 3523 */ 3524 void 3525 spdsock_wsrv(queue_t *q) 3526 { 3527 spdsock_t *ss = q->q_ptr; 3528 mblk_t *mp; 3529 ipsec_stack_t *ipss = ss->spdsock_spds->spds_netstack->netstack_ipsec; 3530 3531 if (ss->spdsock_dump_req != NULL) { 3532 qenable(OTHERQ(q)); 3533 return; 3534 } 3535 3536 while ((mp = getq(q)) != NULL) { 3537 if (ipsec_loaded(ipss)) { 3538 spdsock_wput(q, mp); 3539 if (ss->spdsock_dump_req != NULL) 3540 return; 3541 } else if (!ipsec_failed(ipss)) { 3542 (void) putq(q, mp); 3543 } else { 3544 spdsock_error(q, mp, EPFNOSUPPORT, 0); 3545 } 3546 } 3547 } 3548 3549 static int 3550 spdsock_close(queue_t *q) 3551 { 3552 spdsock_t *ss = q->q_ptr; 3553 spd_stack_t *spds = ss->spdsock_spds; 3554 3555 qprocsoff(q); 3556 3557 /* Safe assumption. */ 3558 ASSERT(ss != NULL); 3559 3560 if (ss->spdsock_timeout != 0) 3561 (void) quntimeout(q, ss->spdsock_timeout); 3562 3563 ss3dbg(spds, ("Driver close, PF_POLICY socket is going away.\n")); 3564 3565 vmem_free(spdsock_vmem, (void *)(uintptr_t)ss->spdsock_minor, 1); 3566 netstack_rele(ss->spdsock_spds->spds_netstack); 3567 3568 kmem_free(ss, sizeof (spdsock_t)); 3569 return (0); 3570 } 3571 3572 /* 3573 * Merge the IPsec algorithms tables with the received algorithm information. 3574 */ 3575 void 3576 spdsock_merge_algs(spd_stack_t *spds) 3577 { 3578 ipsec_alginfo_t *alg, *oalg; 3579 ipsec_algtype_t algtype; 3580 uint_t algidx, algid, nalgs; 3581 crypto_mech_name_t *mechs; 3582 uint_t mech_count, mech_idx; 3583 netstack_t *ns = spds->spds_netstack; 3584 ipsec_stack_t *ipss = ns->netstack_ipsec; 3585 3586 ASSERT(MUTEX_HELD(&spds->spds_alg_lock)); 3587 3588 /* 3589 * Get the list of supported mechanisms from the crypto framework. 3590 * If a mechanism is supported by KCF, resolve its mechanism 3591 * id and mark it as being valid. This operation must be done 3592 * without holding alg_lock, since it can cause a provider 3593 * module to be loaded and the provider notification callback to 3594 * be invoked. 3595 */ 3596 mechs = crypto_get_mech_list(&mech_count, KM_SLEEP); 3597 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 3598 for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) { 3599 int algflags = 0; 3600 crypto_mech_type_t mt = CRYPTO_MECHANISM_INVALID; 3601 3602 alg = spds->spds_algs[algtype][algid]; 3603 if (alg == NULL) 3604 continue; 3605 3606 /* 3607 * The NULL encryption algorithm is a special 3608 * case because there are no mechanisms, yet 3609 * the algorithm is still valid. 3610 */ 3611 if (alg->alg_id == SADB_EALG_NULL) { 3612 alg->alg_mech_type = CRYPTO_MECHANISM_INVALID; 3613 alg->alg_flags = ALG_FLAG_VALID; 3614 continue; 3615 } 3616 3617 for (mech_idx = 0; mech_idx < mech_count; mech_idx++) { 3618 if (strncmp(alg->alg_mech_name, mechs[mech_idx], 3619 CRYPTO_MAX_MECH_NAME) == 0) { 3620 mt = crypto_mech2id(alg->alg_mech_name); 3621 ASSERT(mt != CRYPTO_MECHANISM_INVALID); 3622 algflags = ALG_FLAG_VALID; 3623 break; 3624 } 3625 } 3626 alg->alg_mech_type = mt; 3627 alg->alg_flags = algflags; 3628 } 3629 } 3630 3631 mutex_enter(&ipss->ipsec_alg_lock); 3632 3633 /* 3634 * For each algorithm currently defined, check if it is 3635 * present in the new tables created from the SPD_UPDATEALGS 3636 * message received from user-space. 3637 * Delete the algorithm entries that are currently defined 3638 * but not part of the new tables. 3639 */ 3640 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 3641 nalgs = ipss->ipsec_nalgs[algtype]; 3642 for (algidx = 0; algidx < nalgs; algidx++) { 3643 algid = ipss->ipsec_sortlist[algtype][algidx]; 3644 if (spds->spds_algs[algtype][algid] == NULL) 3645 ipsec_alg_unreg(algtype, algid, ns); 3646 } 3647 } 3648 3649 /* 3650 * For each algorithm we just received, check if it is 3651 * present in the currently defined tables. If it is, swap 3652 * the entry with the one we just allocated. 3653 * If the new algorithm is not in the current tables, 3654 * add it. 3655 */ 3656 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 3657 for (algid = 0; algid < IPSEC_MAX_ALGS; algid++) { 3658 alg = spds->spds_algs[algtype][algid]; 3659 if (alg == NULL) 3660 continue; 3661 3662 if ((oalg = ipss->ipsec_alglists[algtype][algid]) == 3663 NULL) { 3664 /* 3665 * New algorithm, add it to the algorithm 3666 * table. 3667 */ 3668 ipsec_alg_reg(algtype, alg, ns); 3669 } else { 3670 /* 3671 * Algorithm is already in the table. Swap 3672 * the existing entry with the new one. 3673 */ 3674 ipsec_alg_fix_min_max(alg, algtype, ns); 3675 ipss->ipsec_alglists[algtype][algid] = alg; 3676 ipsec_alg_free(oalg); 3677 } 3678 spds->spds_algs[algtype][algid] = NULL; 3679 } 3680 } 3681 3682 for (algtype = 0; algtype < IPSEC_NALGTYPES; algtype++) { 3683 ipss->ipsec_algs_exec_mode[algtype] = 3684 spds->spds_algs_exec_mode[algtype]; 3685 } 3686 3687 mutex_exit(&ipss->ipsec_alg_lock); 3688 3689 crypto_free_mech_list(mechs, mech_count); 3690 3691 ipsecah_algs_changed(ns); 3692 ipsecesp_algs_changed(ns); 3693 } 3694