1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Data-Link Driver 28 */ 29 30 #include <sys/conf.h> 31 #include <sys/mkdev.h> 32 #include <sys/modctl.h> 33 #include <sys/stat.h> 34 #include <sys/vlan.h> 35 #include <sys/mac.h> 36 #include <sys/dld_impl.h> 37 #include <sys/dls_impl.h> 38 #include <sys/softmac.h> 39 #include <sys/vlan.h> 40 #include <sys/policy.h> 41 #include <inet/common.h> 42 43 static void drv_init(void); 44 static int drv_fini(void); 45 46 static int drv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 47 static int drv_attach(dev_info_t *, ddi_attach_cmd_t); 48 static int drv_detach(dev_info_t *, ddi_detach_cmd_t); 49 50 /* 51 * Secure objects declarations 52 */ 53 #define SECOBJ_WEP_HASHSZ 67 54 static krwlock_t drv_secobj_lock; 55 static kmem_cache_t *drv_secobj_cachep; 56 static mod_hash_t *drv_secobj_hash; 57 static void drv_secobj_init(void); 58 static void drv_secobj_fini(void); 59 static int drv_ioc_setap(datalink_id_t, struct dlautopush *); 60 static int drv_ioc_getap(datalink_id_t, struct dlautopush *); 61 static int drv_ioc_clrap(datalink_id_t); 62 63 64 /* 65 * The following entry points are private to dld and are used for control 66 * operations only. The entry points exported to mac drivers are defined 67 * in dld_str.c. Refer to the comment on top of dld_str.c for details. 68 */ 69 static int drv_open(dev_t *, int, int, cred_t *); 70 static int drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 71 72 static dev_info_t *dld_dip; /* dev_info_t for the driver */ 73 uint32_t dld_opt = 0; /* Global options */ 74 75 #define NAUTOPUSH 32 76 static mod_hash_t *dld_ap_hashp; 77 static krwlock_t dld_ap_hash_lock; 78 79 static struct cb_ops drv_cb_ops = { 80 drv_open, /* open */ 81 nulldev, /* close */ 82 nulldev, /* strategy */ 83 nulldev, /* print */ 84 nodev, /* dump */ 85 nodev, /* read */ 86 nodev, /* write */ 87 drv_ioctl, /* ioctl */ 88 nodev, /* devmap */ 89 nodev, /* mmap */ 90 nodev, /* segmap */ 91 nochpoll, /* poll */ 92 ddi_prop_op, /* cb_prop_op */ 93 0, /* streamtab */ 94 D_MP /* Driver compatibility flag */ 95 }; 96 97 static struct dev_ops drv_ops = { 98 DEVO_REV, /* devo_rev */ 99 0, /* refcnt */ 100 drv_getinfo, /* get_dev_info */ 101 nulldev, /* identify */ 102 nulldev, /* probe */ 103 drv_attach, /* attach */ 104 drv_detach, /* detach */ 105 nodev, /* reset */ 106 &drv_cb_ops, /* driver operations */ 107 NULL, /* bus operations */ 108 nodev, /* dev power */ 109 ddi_quiesce_not_supported, /* dev quiesce */ 110 }; 111 112 /* 113 * Module linkage information for the kernel. 114 */ 115 static struct modldrv drv_modldrv = { 116 &mod_driverops, 117 DLD_INFO, 118 &drv_ops 119 }; 120 121 static struct modlinkage drv_modlinkage = { 122 MODREV_1, 123 &drv_modldrv, 124 NULL 125 }; 126 127 int 128 _init(void) 129 { 130 return (mod_install(&drv_modlinkage)); 131 } 132 133 int 134 _fini(void) 135 { 136 return (mod_remove(&drv_modlinkage)); 137 } 138 139 int 140 _info(struct modinfo *modinfop) 141 { 142 return (mod_info(&drv_modlinkage, modinfop)); 143 } 144 145 /* 146 * Initialize component modules. 147 */ 148 static void 149 drv_init(void) 150 { 151 drv_secobj_init(); 152 dld_str_init(); 153 /* 154 * Create a hash table for autopush configuration. 155 */ 156 dld_ap_hashp = mod_hash_create_idhash("dld_autopush_hash", 157 NAUTOPUSH, mod_hash_null_valdtor); 158 159 ASSERT(dld_ap_hashp != NULL); 160 rw_init(&dld_ap_hash_lock, NULL, RW_DRIVER, NULL); 161 } 162 163 /* ARGSUSED */ 164 static uint_t 165 drv_ap_exist(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 166 { 167 boolean_t *pexist = arg; 168 169 *pexist = B_TRUE; 170 return (MH_WALK_TERMINATE); 171 } 172 173 static int 174 drv_fini(void) 175 { 176 int err; 177 boolean_t exist = B_FALSE; 178 179 rw_enter(&dld_ap_hash_lock, RW_READER); 180 mod_hash_walk(dld_ap_hashp, drv_ap_exist, &exist); 181 rw_exit(&dld_ap_hash_lock); 182 183 if (exist) 184 return (EBUSY); 185 186 if ((err = dld_str_fini()) != 0) 187 return (err); 188 189 drv_secobj_fini(); 190 mod_hash_destroy_idhash(dld_ap_hashp); 191 rw_destroy(&dld_ap_hash_lock); 192 return (0); 193 } 194 195 /* 196 * devo_getinfo: getinfo(9e) 197 */ 198 /*ARGSUSED*/ 199 static int 200 drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resp) 201 { 202 if (dld_dip == NULL) 203 return (DDI_FAILURE); 204 205 switch (cmd) { 206 case DDI_INFO_DEVT2INSTANCE: 207 *resp = 0; 208 break; 209 case DDI_INFO_DEVT2DEVINFO: 210 *resp = dld_dip; 211 break; 212 default: 213 return (DDI_FAILURE); 214 } 215 216 return (DDI_SUCCESS); 217 } 218 219 /* 220 * Check properties to set options. (See dld.h for property definitions). 221 */ 222 static void 223 drv_set_opt(dev_info_t *dip) 224 { 225 if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 226 DLD_PROP_NO_FASTPATH, 0) != 0) { 227 dld_opt |= DLD_OPT_NO_FASTPATH; 228 } 229 230 if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 231 DLD_PROP_NO_POLL, 0) != 0) { 232 dld_opt |= DLD_OPT_NO_POLL; 233 } 234 235 if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 236 DLD_PROP_NO_ZEROCOPY, 0) != 0) { 237 dld_opt |= DLD_OPT_NO_ZEROCOPY; 238 } 239 240 if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 241 DLD_PROP_NO_SOFTRING, 0) != 0) { 242 dld_opt |= DLD_OPT_NO_SOFTRING; 243 } 244 } 245 246 /* 247 * devo_attach: attach(9e) 248 */ 249 static int 250 drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 251 { 252 if (cmd != DDI_ATTACH) 253 return (DDI_FAILURE); 254 255 ASSERT(ddi_get_instance(dip) == 0); 256 drv_init(); 257 drv_set_opt(dip); 258 259 /* 260 * Create control node. DLPI provider nodes will be created on demand. 261 */ 262 if (ddi_create_minor_node(dip, DLD_CONTROL_MINOR_NAME, S_IFCHR, 263 DLD_CONTROL_MINOR, DDI_PSEUDO, 0) != DDI_SUCCESS) 264 return (DDI_FAILURE); 265 266 dld_dip = dip; 267 268 /* 269 * Log the fact that the driver is now attached. 270 */ 271 ddi_report_dev(dip); 272 return (DDI_SUCCESS); 273 } 274 275 /* 276 * devo_detach: detach(9e) 277 */ 278 static int 279 drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 280 { 281 if (cmd != DDI_DETACH) 282 return (DDI_FAILURE); 283 284 ASSERT(dld_dip == dip); 285 if (drv_fini() != 0) 286 return (DDI_FAILURE); 287 288 /* 289 * Remove the control node. 290 */ 291 ddi_remove_minor_node(dip, DLD_CONTROL_MINOR_NAME); 292 dld_dip = NULL; 293 294 return (DDI_SUCCESS); 295 } 296 297 /* 298 * dld control node open procedure. 299 */ 300 /*ARGSUSED*/ 301 static int 302 drv_open(dev_t *devp, int flag, int sflag, cred_t *credp) 303 { 304 /* 305 * Only the control node can be opened. 306 */ 307 if (getminor(*devp) != DLD_CONTROL_MINOR) 308 return (ENODEV); 309 return (0); 310 } 311 312 /* 313 * DLDIOC_ATTR 314 */ 315 /* ARGSUSED */ 316 static int 317 drv_ioc_attr(void *karg, intptr_t arg, int mode, cred_t *cred) 318 { 319 dld_ioc_attr_t *diap = karg; 320 dls_dl_handle_t dlh; 321 dls_vlan_t *dvp; 322 int err; 323 324 if ((err = dls_devnet_hold_tmp(diap->dia_linkid, &dlh)) != 0) 325 return (err); 326 327 if ((err = dls_vlan_hold(dls_devnet_mac(dlh), 328 dls_devnet_vid(dlh), &dvp, B_FALSE, B_FALSE)) != 0) { 329 dls_devnet_rele_tmp(dlh); 330 return (err); 331 } 332 mac_sdu_get(dvp->dv_dlp->dl_mh, NULL, &diap->dia_max_sdu); 333 334 dls_vlan_rele(dvp); 335 dls_devnet_rele_tmp(dlh); 336 337 return (0); 338 } 339 340 /* 341 * DLDIOC_PHYS_ATTR 342 */ 343 /* ARGSUSED */ 344 static int 345 drv_ioc_phys_attr(void *karg, intptr_t arg, int mode, cred_t *cred) 346 { 347 dld_ioc_phys_attr_t *dipp = karg; 348 int err; 349 dls_dl_handle_t dlh; 350 dls_dev_handle_t ddh; 351 dev_t phydev; 352 353 /* 354 * Every physical link should have its physical dev_t kept in the 355 * daemon. If not, it is not a valid physical link. 356 */ 357 if (dls_mgmt_get_phydev(dipp->dip_linkid, &phydev) != 0) 358 return (EINVAL); 359 360 /* 361 * Although this is a valid physical link, it might already be removed 362 * by DR or during system shutdown. softmac_hold_device() would return 363 * ENOENT in this case. 364 */ 365 if ((err = softmac_hold_device(phydev, &ddh)) != 0) 366 return (err); 367 368 if (dls_devnet_hold_tmp(dipp->dip_linkid, &dlh) != 0) { 369 /* 370 * Although this is an active physical link, its link type is 371 * not supported by GLDv3, and therefore it does not have 372 * vanity naming support. 373 */ 374 dipp->dip_novanity = B_TRUE; 375 } else { 376 dipp->dip_novanity = B_FALSE; 377 dls_devnet_rele_tmp(dlh); 378 } 379 /* 380 * Get the physical device name from the major number and the instance 381 * number derived from phydev. 382 */ 383 (void) snprintf(dipp->dip_dev, MAXLINKNAMELEN, "%s%d", 384 ddi_major_to_name(getmajor(phydev)), getminor(phydev) - 1); 385 386 softmac_rele_device(ddh); 387 return (0); 388 } 389 390 /* 391 * DLDIOC_SETPROP 392 */ 393 static int 394 drv_ioc_prop_common(dld_ioc_macprop_t *dipp, intptr_t arg, boolean_t set, 395 int mode) 396 { 397 int err = EINVAL; 398 size_t dsize; 399 dld_ioc_macprop_t *kdipp; 400 dls_dl_handle_t dlh; 401 dls_vlan_t *dvp; 402 datalink_id_t linkid; 403 mac_prop_t macprop; 404 uchar_t *cp; 405 struct dlautopush *dlap; 406 dld_ioc_zid_t *dzp; 407 408 /* 409 * We only use pr_valsize from dipp, as the caller only did a 410 * copyin() for sizeof (dld_ioc_prop_t), which doesn't cover 411 * the property data. We copyin the full dld_ioc_prop_t 412 * including the data into kdipp down below. 413 */ 414 dsize = sizeof (dld_ioc_macprop_t) + dipp->pr_valsize - 1; 415 if (dsize < dipp->pr_valsize) 416 return (EINVAL); 417 418 /* 419 * The property data is variable size, so we need to allocate 420 * a buffer for kernel use as this data was not part of the 421 * dipp allocation and copyin() done by the framework. 422 */ 423 if ((kdipp = kmem_alloc(dsize, KM_NOSLEEP)) == NULL) 424 return (ENOMEM); 425 if (ddi_copyin((void *)arg, kdipp, dsize, mode) != 0) { 426 err = EFAULT; 427 goto done; 428 } 429 430 linkid = kdipp->pr_linkid; 431 432 switch (dipp->pr_num) { 433 case MAC_PROP_ZONE: 434 if (set) { 435 dzp = (dld_ioc_zid_t *)kdipp->pr_val; 436 err = dls_devnet_setzid(dzp->diz_link, dzp->diz_zid); 437 goto done; 438 } else { 439 cp = (uchar_t *)kdipp->pr_val; 440 err = dls_devnet_getzid(linkid, (zoneid_t *)cp); 441 goto done; 442 } 443 case MAC_PROP_AUTOPUSH: 444 if (set) { 445 if (dipp->pr_valsize != 0) { 446 dlap = (struct dlautopush *)kdipp->pr_val; 447 err = drv_ioc_setap(linkid, dlap); 448 goto done; 449 } else { 450 err = drv_ioc_clrap(linkid); 451 goto done; 452 } 453 } else { 454 dlap = (struct dlautopush *)kdipp->pr_val; 455 err = drv_ioc_getap(linkid, dlap); 456 goto done; 457 } 458 459 default: 460 break; 461 } 462 463 if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0) 464 goto done; 465 466 if ((err = dls_vlan_hold(dls_devnet_mac(dlh), 467 dls_devnet_vid(dlh), &dvp, B_FALSE, B_FALSE)) != 0) { 468 dls_devnet_rele_tmp(dlh); 469 goto done; 470 } 471 472 macprop.mp_name = kdipp->pr_name; 473 macprop.mp_id = kdipp->pr_num; 474 macprop.mp_flags = kdipp->pr_flags; 475 476 if (set) { 477 err = mac_set_prop(dvp->dv_dlp->dl_mh, &macprop, 478 kdipp->pr_val, kdipp->pr_valsize); 479 } else { 480 err = mac_get_prop(dvp->dv_dlp->dl_mh, &macprop, 481 kdipp->pr_val, kdipp->pr_valsize); 482 } 483 484 dls_vlan_rele(dvp); 485 dls_devnet_rele_tmp(dlh); 486 done: 487 if (!set && err == 0 && 488 ddi_copyout(kdipp, (void *)arg, dsize, mode) != 0) 489 err = EFAULT; 490 kmem_free(kdipp, dsize); 491 return (err); 492 } 493 494 /* ARGSUSED */ 495 static int 496 drv_ioc_setprop(void *karg, intptr_t arg, int mode, cred_t *cred) 497 { 498 return (drv_ioc_prop_common(karg, arg, B_TRUE, mode)); 499 } 500 501 /* ARGSUSED */ 502 static int 503 drv_ioc_getprop(void *karg, intptr_t arg, int mode, cred_t *cred) 504 { 505 return (drv_ioc_prop_common(karg, arg, B_FALSE, mode)); 506 } 507 508 /* 509 * DLDIOC_CREATE_VLAN 510 */ 511 /* ARGSUSED */ 512 static int 513 drv_ioc_create_vlan(void *karg, intptr_t arg, int mode, cred_t *cred) 514 { 515 dld_ioc_create_vlan_t *dicp = karg; 516 517 return (dls_devnet_create_vlan(dicp->dic_vlanid, dicp->dic_linkid, 518 dicp->dic_vid, dicp->dic_force)); 519 } 520 521 /* 522 * DLDIOC_DELETE_VLAN 523 */ 524 /* ARGSUSED */ 525 static int 526 drv_ioc_delete_vlan(void *karg, intptr_t arg, int mode, cred_t *cred) 527 { 528 dld_ioc_delete_vlan_t *didp = karg; 529 530 return (dls_devnet_destroy_vlan(didp->did_linkid)); 531 } 532 533 /* 534 * DLDIOC_VLAN_ATTR 535 */ 536 /* ARGSUSED */ 537 static int 538 drv_ioc_vlan_attr(void *karg, intptr_t arg, int mode, cred_t *cred) 539 { 540 dld_ioc_vlan_attr_t *divp = karg; 541 dls_dl_handle_t dlh; 542 uint16_t vid; 543 dls_vlan_t *dvp; 544 int err; 545 546 /* 547 * Hold this link to prevent it from being deleted. 548 */ 549 if ((err = dls_devnet_hold_tmp(divp->div_vlanid, &dlh)) != 0) 550 return (err); 551 552 if ((vid = dls_devnet_vid(dlh)) == VLAN_ID_NONE) { 553 dls_devnet_rele_tmp(dlh); 554 return (EINVAL); 555 } 556 557 err = dls_vlan_hold(dls_devnet_mac(dlh), vid, &dvp, B_FALSE, B_FALSE); 558 if (err != 0) { 559 dls_devnet_rele_tmp(dlh); 560 return (err); 561 } 562 563 divp->div_linkid = dls_devnet_linkid(dlh); 564 divp->div_implicit = !dls_devnet_is_explicit(dlh); 565 divp->div_vid = vid; 566 divp->div_force = dvp->dv_force; 567 568 dls_vlan_rele(dvp); 569 dls_devnet_rele_tmp(dlh); 570 return (0); 571 } 572 573 /* 574 * DLDIOC_RENAME. 575 * 576 * This function handles two cases of link renaming. See more in comments above 577 * dls_datalink_rename(). 578 */ 579 /* ARGSUSED */ 580 static int 581 drv_ioc_rename(void *karg, intptr_t arg, int mode, cred_t *cred) 582 { 583 dld_ioc_rename_t *dir = karg; 584 mod_hash_key_t key; 585 mod_hash_val_t val; 586 int err; 587 588 if ((err = dls_devnet_rename(dir->dir_linkid1, dir->dir_linkid2, 589 dir->dir_link)) != 0) 590 return (err); 591 592 if (dir->dir_linkid2 == DATALINK_INVALID_LINKID) 593 return (0); 594 595 /* 596 * if dir_linkid2 is not DATALINK_INVALID_LINKID, it means this 597 * renaming request is to rename a valid physical link (dir_linkid1) 598 * to a "removed" physical link (dir_linkid2, which is removed by DR 599 * or during system shutdown). In this case, the link (specified by 600 * dir_linkid1) would inherit all the configuration of dir_linkid2, 601 * and dir_linkid1 and its configuration would be lost. 602 * 603 * Remove per-link autopush configuration of dir_linkid1 in this case. 604 */ 605 key = (mod_hash_key_t)(uintptr_t)dir->dir_linkid1; 606 rw_enter(&dld_ap_hash_lock, RW_WRITER); 607 if (mod_hash_find(dld_ap_hashp, key, &val) != 0) { 608 rw_exit(&dld_ap_hash_lock); 609 return (0); 610 } 611 612 VERIFY(mod_hash_remove(dld_ap_hashp, key, &val) == 0); 613 kmem_free(val, sizeof (dld_ap_t)); 614 rw_exit(&dld_ap_hash_lock); 615 return (0); 616 } 617 618 static int 619 drv_ioc_setap(datalink_id_t linkid, struct dlautopush *dlap) 620 { 621 dld_ap_t *dap; 622 int i; 623 mod_hash_key_t key; 624 625 if (dlap->dap_npush == 0 || dlap->dap_npush > MAXAPUSH) 626 return (EINVAL); 627 628 /* 629 * Validate that the specified list of modules exist. 630 */ 631 for (i = 0; i < dlap->dap_npush; i++) { 632 if (fmodsw_find(dlap->dap_aplist[i], FMODSW_LOAD) == NULL) 633 return (EINVAL); 634 } 635 636 637 key = (mod_hash_key_t)(uintptr_t)linkid; 638 639 rw_enter(&dld_ap_hash_lock, RW_WRITER); 640 if (mod_hash_find(dld_ap_hashp, key, (mod_hash_val_t *)&dap) != 0) { 641 dap = kmem_zalloc(sizeof (dld_ap_t), KM_NOSLEEP); 642 if (dap == NULL) { 643 rw_exit(&dld_ap_hash_lock); 644 return (ENOMEM); 645 } 646 647 dap->da_linkid = linkid; 648 VERIFY(mod_hash_insert(dld_ap_hashp, key, 649 (mod_hash_val_t)dap) == 0); 650 } 651 652 /* 653 * Update the configuration. 654 */ 655 dap->da_anchor = dlap->dap_anchor; 656 dap->da_npush = dlap->dap_npush; 657 for (i = 0; i < dlap->dap_npush; i++) { 658 (void) strlcpy(dap->da_aplist[i], dlap->dap_aplist[i], 659 FMNAMESZ + 1); 660 } 661 rw_exit(&dld_ap_hash_lock); 662 663 return (0); 664 } 665 666 static int 667 drv_ioc_getap(datalink_id_t linkid, struct dlautopush *dlap) 668 { 669 dld_ap_t *dap; 670 int i; 671 672 rw_enter(&dld_ap_hash_lock, RW_READER); 673 if (mod_hash_find(dld_ap_hashp, 674 (mod_hash_key_t)(uintptr_t)linkid, 675 (mod_hash_val_t *)&dap) != 0) { 676 rw_exit(&dld_ap_hash_lock); 677 return (ENOENT); 678 } 679 680 /* 681 * Retrieve the configuration. 682 */ 683 dlap->dap_anchor = dap->da_anchor; 684 dlap->dap_npush = dap->da_npush; 685 for (i = 0; i < dap->da_npush; i++) { 686 (void) strlcpy(dlap->dap_aplist[i], dap->da_aplist[i], 687 FMNAMESZ + 1); 688 } 689 rw_exit(&dld_ap_hash_lock); 690 691 return (0); 692 } 693 694 static int 695 drv_ioc_clrap(datalink_id_t linkid) 696 { 697 mod_hash_val_t val; 698 mod_hash_key_t key; 699 700 key = (mod_hash_key_t)(uintptr_t)linkid; 701 702 rw_enter(&dld_ap_hash_lock, RW_WRITER); 703 if (mod_hash_find(dld_ap_hashp, key, &val) != 0) { 704 rw_exit(&dld_ap_hash_lock); 705 return (0); 706 } 707 708 VERIFY(mod_hash_remove(dld_ap_hashp, key, &val) == 0); 709 kmem_free(val, sizeof (dld_ap_t)); 710 rw_exit(&dld_ap_hash_lock); 711 return (0); 712 } 713 714 /* 715 * DLDIOC_DOORSERVER 716 */ 717 /* ARGSUSED */ 718 static int 719 drv_ioc_doorserver(void *karg, intptr_t arg, int mode, cred_t *cred) 720 { 721 dld_ioc_door_t *did = karg; 722 723 return (dls_mgmt_door_set(did->did_start_door)); 724 } 725 726 /* 727 * Check for GLDv3 autopush information. There are three cases: 728 * 729 * 1. If devp points to a GLDv3 datalink and it has autopush configuration, 730 * fill dlap in with that information and return 0. 731 * 732 * 2. If devp points to a GLDv3 datalink but it doesn't have autopush 733 * configuration, then replace devp with the physical device (if one 734 * exists) and return 1. This allows stropen() to find the old-school 735 * per-driver autopush configuration. (For softmac, the result is that 736 * the softmac dev_t is replaced with the legacy device's dev_t). 737 * 738 * 3. If neither of the above apply, don't touch the args and return -1. 739 */ 740 int 741 dld_autopush(dev_t *devp, struct dlautopush *dlap) 742 { 743 dld_ap_t *dap; 744 datalink_id_t linkid; 745 dev_t phydev; 746 747 if (!GLDV3_DRV(getmajor(*devp))) 748 return (-1); 749 750 /* 751 * Find the linkid by the link's dev_t. 752 */ 753 if (dls_devnet_dev2linkid(*devp, &linkid) != 0) 754 return (-1); 755 756 /* 757 * Find the autopush configuration associated with the linkid. 758 */ 759 rw_enter(&dld_ap_hash_lock, RW_READER); 760 if (mod_hash_find(dld_ap_hashp, (mod_hash_key_t)(uintptr_t)linkid, 761 (mod_hash_val_t *)&dap) == 0) { 762 *dlap = dap->da_ap; 763 rw_exit(&dld_ap_hash_lock); 764 return (0); 765 } 766 rw_exit(&dld_ap_hash_lock); 767 768 if (dls_devnet_phydev(linkid, &phydev) != 0) 769 return (-1); 770 771 *devp = phydev; 772 return (1); 773 } 774 775 /* 776 * Secure objects implementation 777 */ 778 779 /* ARGSUSED */ 780 static int 781 drv_secobj_ctor(void *buf, void *arg, int kmflag) 782 { 783 bzero(buf, sizeof (dld_secobj_t)); 784 return (0); 785 } 786 787 static void 788 drv_secobj_init(void) 789 { 790 rw_init(&drv_secobj_lock, NULL, RW_DEFAULT, NULL); 791 drv_secobj_cachep = kmem_cache_create("drv_secobj_cache", 792 sizeof (dld_secobj_t), 0, drv_secobj_ctor, NULL, 793 NULL, NULL, NULL, 0); 794 drv_secobj_hash = mod_hash_create_extended("drv_secobj_hash", 795 SECOBJ_WEP_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor, 796 mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP); 797 } 798 799 static void 800 drv_secobj_fini(void) 801 { 802 mod_hash_destroy_hash(drv_secobj_hash); 803 kmem_cache_destroy(drv_secobj_cachep); 804 rw_destroy(&drv_secobj_lock); 805 } 806 807 /* ARGSUSED */ 808 static int 809 drv_ioc_secobj_set(void *karg, intptr_t arg, int mode, cred_t *cred) 810 { 811 dld_ioc_secobj_set_t *ssp = karg; 812 dld_secobj_t *sobjp, *objp; 813 int err; 814 815 sobjp = &ssp->ss_obj; 816 817 if (sobjp->so_class != DLD_SECOBJ_CLASS_WEP && 818 sobjp->so_class != DLD_SECOBJ_CLASS_WPA) 819 return (EINVAL); 820 821 if (sobjp->so_name[DLD_SECOBJ_NAME_MAX - 1] != '\0' || 822 sobjp->so_len > DLD_SECOBJ_VAL_MAX) 823 return (EINVAL); 824 825 rw_enter(&drv_secobj_lock, RW_WRITER); 826 err = mod_hash_find(drv_secobj_hash, (mod_hash_key_t)sobjp->so_name, 827 (mod_hash_val_t *)&objp); 828 if (err == 0) { 829 if ((ssp->ss_flags & DLD_SECOBJ_OPT_CREATE) != 0) { 830 rw_exit(&drv_secobj_lock); 831 return (EEXIST); 832 } 833 } else { 834 ASSERT(err == MH_ERR_NOTFOUND); 835 if ((ssp->ss_flags & DLD_SECOBJ_OPT_CREATE) == 0) { 836 rw_exit(&drv_secobj_lock); 837 return (ENOENT); 838 } 839 objp = kmem_cache_alloc(drv_secobj_cachep, KM_SLEEP); 840 (void) strlcpy(objp->so_name, sobjp->so_name, 841 DLD_SECOBJ_NAME_MAX); 842 843 VERIFY(mod_hash_insert(drv_secobj_hash, 844 (mod_hash_key_t)objp->so_name, (mod_hash_val_t)objp) == 0); 845 } 846 bcopy(sobjp->so_val, objp->so_val, sobjp->so_len); 847 objp->so_len = sobjp->so_len; 848 objp->so_class = sobjp->so_class; 849 rw_exit(&drv_secobj_lock); 850 return (0); 851 } 852 853 typedef struct dld_secobj_state { 854 uint_t ss_free; 855 uint_t ss_count; 856 int ss_rc; 857 int ss_mode; 858 dld_secobj_t *ss_objp; 859 } dld_secobj_state_t; 860 861 /* ARGSUSED */ 862 static uint_t 863 drv_secobj_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 864 { 865 dld_secobj_state_t *statep = arg; 866 dld_secobj_t *sobjp = (dld_secobj_t *)val; 867 868 if (statep->ss_free < sizeof (dld_secobj_t)) { 869 statep->ss_rc = ENOSPC; 870 return (MH_WALK_TERMINATE); 871 } 872 if (ddi_copyout(sobjp, statep->ss_objp, sizeof (*sobjp), 873 statep->ss_mode) != 0) { 874 statep->ss_rc = EFAULT; 875 return (MH_WALK_TERMINATE); 876 } 877 statep->ss_objp++; 878 statep->ss_free -= sizeof (dld_secobj_t); 879 statep->ss_count++; 880 return (MH_WALK_CONTINUE); 881 } 882 883 /* ARGSUSED */ 884 static int 885 drv_ioc_secobj_get(void *karg, intptr_t arg, int mode, cred_t *cred) 886 { 887 dld_ioc_secobj_get_t *sgp = karg; 888 dld_secobj_t *sobjp, *objp; 889 int err; 890 891 sobjp = &sgp->sg_obj; 892 893 if (sobjp->so_name[DLD_SECOBJ_NAME_MAX - 1] != '\0') 894 return (EINVAL); 895 896 rw_enter(&drv_secobj_lock, RW_READER); 897 if (sobjp->so_name[0] != '\0') { 898 err = mod_hash_find(drv_secobj_hash, 899 (mod_hash_key_t)sobjp->so_name, (mod_hash_val_t *)&objp); 900 if (err != 0) { 901 ASSERT(err == MH_ERR_NOTFOUND); 902 rw_exit(&drv_secobj_lock); 903 return (ENOENT); 904 } 905 bcopy(objp->so_val, sobjp->so_val, objp->so_len); 906 sobjp->so_len = objp->so_len; 907 sobjp->so_class = objp->so_class; 908 sgp->sg_count = 1; 909 } else { 910 dld_secobj_state_t state; 911 912 state.ss_free = sgp->sg_size - sizeof (dld_ioc_secobj_get_t); 913 state.ss_count = 0; 914 state.ss_rc = 0; 915 state.ss_mode = mode; 916 state.ss_objp = (dld_secobj_t *)((uchar_t *)arg + 917 sizeof (dld_ioc_secobj_get_t)); 918 919 mod_hash_walk(drv_secobj_hash, drv_secobj_walker, &state); 920 if (state.ss_rc != 0) { 921 rw_exit(&drv_secobj_lock); 922 return (state.ss_rc); 923 } 924 sgp->sg_count = state.ss_count; 925 } 926 rw_exit(&drv_secobj_lock); 927 return (0); 928 } 929 930 /* ARGSUSED */ 931 static int 932 drv_ioc_secobj_unset(void *karg, intptr_t arg, int mode, cred_t *cred) 933 { 934 dld_ioc_secobj_unset_t *sup = karg; 935 dld_secobj_t *objp; 936 mod_hash_val_t val; 937 int err; 938 939 if (sup->su_name[DLD_SECOBJ_NAME_MAX - 1] != '\0') 940 return (EINVAL); 941 942 rw_enter(&drv_secobj_lock, RW_WRITER); 943 err = mod_hash_find(drv_secobj_hash, (mod_hash_key_t)sup->su_name, 944 (mod_hash_val_t *)&objp); 945 if (err != 0) { 946 ASSERT(err == MH_ERR_NOTFOUND); 947 rw_exit(&drv_secobj_lock); 948 return (ENOENT); 949 } 950 VERIFY(mod_hash_remove(drv_secobj_hash, (mod_hash_key_t)sup->su_name, 951 (mod_hash_val_t *)&val) == 0); 952 ASSERT(objp == (dld_secobj_t *)val); 953 954 kmem_cache_free(drv_secobj_cachep, objp); 955 rw_exit(&drv_secobj_lock); 956 return (0); 957 } 958 959 static dld_ioc_info_t drv_ioc_list[] = { 960 {DLDIOC_ATTR, DLDCOPYINOUT, sizeof (dld_ioc_attr_t), 961 drv_ioc_attr}, 962 {DLDIOC_PHYS_ATTR, DLDCOPYINOUT, sizeof (dld_ioc_phys_attr_t), 963 drv_ioc_phys_attr}, 964 {DLDIOC_SECOBJ_SET, DLDCOPYIN | DLDDLCONFIG, 965 sizeof (dld_ioc_secobj_set_t), drv_ioc_secobj_set}, 966 {DLDIOC_SECOBJ_GET, DLDCOPYINOUT | DLDDLCONFIG, 967 sizeof (dld_ioc_secobj_get_t), drv_ioc_secobj_get}, 968 {DLDIOC_SECOBJ_UNSET, DLDCOPYIN | DLDDLCONFIG, 969 sizeof (dld_ioc_secobj_unset_t), drv_ioc_secobj_unset}, 970 {DLDIOC_CREATE_VLAN, DLDCOPYIN | DLDDLCONFIG, 971 sizeof (dld_ioc_create_vlan_t), drv_ioc_create_vlan}, 972 {DLDIOC_DELETE_VLAN, DLDCOPYIN | DLDDLCONFIG, 973 sizeof (dld_ioc_delete_vlan_t), 974 drv_ioc_delete_vlan}, 975 {DLDIOC_VLAN_ATTR, DLDCOPYINOUT, sizeof (dld_ioc_vlan_attr_t), 976 drv_ioc_vlan_attr}, 977 {DLDIOC_DOORSERVER, DLDCOPYIN | DLDDLCONFIG, sizeof (dld_ioc_door_t), 978 drv_ioc_doorserver}, 979 {DLDIOC_RENAME, DLDCOPYIN | DLDDLCONFIG, sizeof (dld_ioc_rename_t), 980 drv_ioc_rename}, 981 {DLDIOC_GETMACPROP, DLDCOPYIN, sizeof (dld_ioc_macprop_t), 982 drv_ioc_getprop}, 983 {DLDIOC_SETMACPROP, DLDCOPYIN | DLDDLCONFIG, sizeof (dld_ioc_macprop_t), 984 drv_ioc_setprop} 985 }; 986 987 typedef struct dld_ioc_modentry { 988 uint16_t dim_modid; /* Top 16 bits of ioctl command */ 989 char *dim_modname; /* Module to be loaded */ 990 dld_ioc_info_t *dim_list; /* array of ioctl structures */ 991 uint_t dim_count; /* number of elements in dim_list */ 992 } dld_ioc_modentry_t; 993 994 /* 995 * For all modules except for dld, dim_list and dim_count are assigned 996 * when the modules register their ioctls in dld_ioc_register(). We 997 * can statically initialize dld's ioctls in-line here; there's no 998 * need for it to call dld_ioc_register() itself. 999 */ 1000 static dld_ioc_modentry_t dld_ioc_modtable[] = { 1001 {DLD_IOC, "dld", drv_ioc_list, DLDIOCCNT(drv_ioc_list)}, 1002 {AGGR_IOC, "aggr", NULL, 0}, 1003 {VNIC_IOC, "vnic", NULL, 0} 1004 }; 1005 #define DLDIOC_CNT \ 1006 (sizeof (dld_ioc_modtable) / sizeof (dld_ioc_modentry_t)) 1007 1008 static dld_ioc_modentry_t * 1009 dld_ioc_findmod(uint16_t modid) 1010 { 1011 int i; 1012 1013 for (i = 0; i < DLDIOC_CNT; i++) { 1014 if (modid == dld_ioc_modtable[i].dim_modid) 1015 return (&dld_ioc_modtable[i]); 1016 } 1017 return (NULL); 1018 } 1019 1020 int 1021 dld_ioc_register(uint16_t modid, dld_ioc_info_t *list, uint_t count) 1022 { 1023 dld_ioc_modentry_t *dim = dld_ioc_findmod(modid); 1024 1025 if (dim == NULL) 1026 return (ENOENT); 1027 1028 dim->dim_list = list; 1029 dim->dim_count = count; 1030 return (0); 1031 } 1032 1033 void 1034 dld_ioc_unregister(uint16_t modid) 1035 { 1036 VERIFY(dld_ioc_register(modid, NULL, 0) == 0); 1037 } 1038 1039 /* 1040 * The general design with GLDv3 ioctls is that all ioctls issued 1041 * through /dev/dld go through this drv_ioctl() function. This 1042 * function handles all ioctls on behalf of modules listed in 1043 * dld_ioc_modtable. 1044 * 1045 * When an ioctl is received, this function looks for the associated 1046 * module-id-specific ioctl information using dld_ioc_findmod(). The 1047 * call to ddi_hold_devi_by_instance() on the associated device will 1048 * cause the kernel module responsible for the ioctl to be loaded if 1049 * it's not already loaded, which should result in that module calling 1050 * dld_ioc_register(), thereby filling in the dim_list containing the 1051 * details for the ioctl being processed. 1052 * 1053 * This function can then perform operations such as copyin() data and 1054 * do credential checks based on the registered ioctl information, 1055 * then issue the callback function di_func() registered by the 1056 * responsible module. Upon return, the appropriate copyout() 1057 * operation can be performed and the operation completes. 1058 */ 1059 /* ARGSUSED */ 1060 static int 1061 drv_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cred, int *rvalp) 1062 { 1063 dld_ioc_modentry_t *dim; 1064 dld_ioc_info_t *info; 1065 dev_info_t *dip = NULL; 1066 void *buf = NULL; 1067 size_t sz; 1068 int i, err; 1069 1070 if ((dim = dld_ioc_findmod(DLD_IOC_MODID(cmd))) == NULL) 1071 return (ENOTSUP); 1072 1073 dip = ddi_hold_devi_by_instance(ddi_name_to_major(dim->dim_modname), 1074 0, 0); 1075 if (dip == NULL || dim->dim_list == NULL) { 1076 err = ENODEV; 1077 goto done; 1078 } 1079 1080 for (i = 0; i < dim->dim_count; i++) { 1081 if (cmd == dim->dim_list[i].di_cmd) 1082 break; 1083 } 1084 if (i == dim->dim_count) { 1085 err = ENOTSUP; 1086 goto done; 1087 } 1088 1089 info = &dim->dim_list[i]; 1090 1091 if ((info->di_flags & DLDDLCONFIG) && secpolicy_dl_config(cred) != 0) { 1092 err = EPERM; 1093 goto done; 1094 } 1095 1096 sz = info->di_argsize; 1097 if ((buf = kmem_zalloc(sz, KM_NOSLEEP)) == NULL) { 1098 err = ENOMEM; 1099 goto done; 1100 } 1101 1102 if ((info->di_flags & DLDCOPYIN) && 1103 ddi_copyin((void *)arg, buf, sz, mode) != 0) { 1104 err = EFAULT; 1105 goto done; 1106 } 1107 1108 err = info->di_func(buf, arg, mode, cred); 1109 1110 if ((info->di_flags & DLDCOPYOUT) && 1111 ddi_copyout(buf, (void *)arg, sz, mode) != 0 && err == 0) 1112 err = EFAULT; 1113 1114 done: 1115 if (buf != NULL) 1116 kmem_free(buf, sz); 1117 if (dip != NULL) 1118 ddi_release_devi(dip); 1119 return (err); 1120 } 1121