10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52311Sseb * Common Development and Distribution License (the "License"). 62311Sseb * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 225895Syz147064 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate /* 270Sstevel@tonic-gate * Data-Link Driver 280Sstevel@tonic-gate */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #include <sys/conf.h> 31269Sericheng #include <sys/mkdev.h> 32269Sericheng #include <sys/modctl.h> 330Sstevel@tonic-gate #include <sys/stat.h> 345895Syz147064 #include <sys/vlan.h> 356789Sam223141 #include <sys/mac.h> 36269Sericheng #include <sys/dld_impl.h> 37269Sericheng #include <sys/dls_impl.h> 385895Syz147064 #include <sys/softmac.h> 393448Sdh155122 #include <sys/vlan.h> 40*7408SSebastien.Roy@Sun.COM #include <sys/policy.h> 410Sstevel@tonic-gate #include <inet/common.h> 420Sstevel@tonic-gate 430Sstevel@tonic-gate static void drv_init(void); 440Sstevel@tonic-gate static int drv_fini(void); 450Sstevel@tonic-gate 460Sstevel@tonic-gate static int drv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 470Sstevel@tonic-gate static int drv_attach(dev_info_t *, ddi_attach_cmd_t); 480Sstevel@tonic-gate static int drv_detach(dev_info_t *, ddi_detach_cmd_t); 490Sstevel@tonic-gate 50269Sericheng /* 513147Sxc151355 * Secure objects declarations 523147Sxc151355 */ 533147Sxc151355 #define SECOBJ_WEP_HASHSZ 67 543147Sxc151355 static krwlock_t drv_secobj_lock; 553147Sxc151355 static kmem_cache_t *drv_secobj_cachep; 563147Sxc151355 static mod_hash_t *drv_secobj_hash; 573147Sxc151355 static void drv_secobj_init(void); 583147Sxc151355 static void drv_secobj_fini(void); 597342SAruna.Ramakrishna@Sun.COM static int drv_ioc_setap(datalink_id_t, struct dlautopush *); 607342SAruna.Ramakrishna@Sun.COM static int drv_ioc_getap(datalink_id_t, struct dlautopush *); 617342SAruna.Ramakrishna@Sun.COM static int drv_ioc_clrap(datalink_id_t); 623147Sxc151355 63*7408SSebastien.Roy@Sun.COM 643147Sxc151355 /* 65269Sericheng * The following entry points are private to dld and are used for control 66269Sericheng * operations only. The entry points exported to mac drivers are defined 67269Sericheng * in dld_str.c. Refer to the comment on top of dld_str.c for details. 68269Sericheng */ 69*7408SSebastien.Roy@Sun.COM static int drv_open(dev_t *, int, int, cred_t *); 70*7408SSebastien.Roy@Sun.COM static int drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 710Sstevel@tonic-gate 72*7408SSebastien.Roy@Sun.COM static dev_info_t *dld_dip; /* dev_info_t for the driver */ 73*7408SSebastien.Roy@Sun.COM uint32_t dld_opt = 0; /* Global options */ 740Sstevel@tonic-gate 755895Syz147064 #define NAUTOPUSH 32 765895Syz147064 static mod_hash_t *dld_ap_hashp; 775895Syz147064 static krwlock_t dld_ap_hash_lock; 785895Syz147064 79*7408SSebastien.Roy@Sun.COM static struct cb_ops drv_cb_ops = { 80*7408SSebastien.Roy@Sun.COM drv_open, /* open */ 81*7408SSebastien.Roy@Sun.COM nulldev, /* close */ 82*7408SSebastien.Roy@Sun.COM nulldev, /* strategy */ 83*7408SSebastien.Roy@Sun.COM nulldev, /* print */ 84*7408SSebastien.Roy@Sun.COM nodev, /* dump */ 85*7408SSebastien.Roy@Sun.COM nodev, /* read */ 86*7408SSebastien.Roy@Sun.COM nodev, /* write */ 87*7408SSebastien.Roy@Sun.COM drv_ioctl, /* ioctl */ 88*7408SSebastien.Roy@Sun.COM nodev, /* devmap */ 89*7408SSebastien.Roy@Sun.COM nodev, /* mmap */ 90*7408SSebastien.Roy@Sun.COM nodev, /* segmap */ 91*7408SSebastien.Roy@Sun.COM nochpoll, /* poll */ 92*7408SSebastien.Roy@Sun.COM ddi_prop_op, /* cb_prop_op */ 93*7408SSebastien.Roy@Sun.COM 0, /* streamtab */ 94*7408SSebastien.Roy@Sun.COM D_MP /* Driver compatibility flag */ 950Sstevel@tonic-gate }; 960Sstevel@tonic-gate 97*7408SSebastien.Roy@Sun.COM static struct dev_ops drv_ops = { 98*7408SSebastien.Roy@Sun.COM DEVO_REV, /* devo_rev */ 99*7408SSebastien.Roy@Sun.COM 0, /* refcnt */ 100*7408SSebastien.Roy@Sun.COM drv_getinfo, /* get_dev_info */ 101*7408SSebastien.Roy@Sun.COM nulldev, /* identify */ 102*7408SSebastien.Roy@Sun.COM nulldev, /* probe */ 103*7408SSebastien.Roy@Sun.COM drv_attach, /* attach */ 104*7408SSebastien.Roy@Sun.COM drv_detach, /* detach */ 105*7408SSebastien.Roy@Sun.COM nodev, /* reset */ 106*7408SSebastien.Roy@Sun.COM &drv_cb_ops, /* driver operations */ 107*7408SSebastien.Roy@Sun.COM NULL, /* bus operations */ 108*7408SSebastien.Roy@Sun.COM nodev /* dev power */ 1090Sstevel@tonic-gate }; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /* 1120Sstevel@tonic-gate * Module linkage information for the kernel. 1130Sstevel@tonic-gate */ 1140Sstevel@tonic-gate static struct modldrv drv_modldrv = { 1150Sstevel@tonic-gate &mod_driverops, 1160Sstevel@tonic-gate DLD_INFO, 1170Sstevel@tonic-gate &drv_ops 1180Sstevel@tonic-gate }; 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate static struct modlinkage drv_modlinkage = { 1210Sstevel@tonic-gate MODREV_1, 1220Sstevel@tonic-gate &drv_modldrv, 1230Sstevel@tonic-gate NULL 1240Sstevel@tonic-gate }; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate int 1270Sstevel@tonic-gate _init(void) 1280Sstevel@tonic-gate { 129*7408SSebastien.Roy@Sun.COM return (mod_install(&drv_modlinkage)); 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate int 1330Sstevel@tonic-gate _fini(void) 1340Sstevel@tonic-gate { 135*7408SSebastien.Roy@Sun.COM return (mod_remove(&drv_modlinkage)); 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate int 1390Sstevel@tonic-gate _info(struct modinfo *modinfop) 1400Sstevel@tonic-gate { 1410Sstevel@tonic-gate return (mod_info(&drv_modlinkage, modinfop)); 1420Sstevel@tonic-gate } 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate /* 145269Sericheng * Initialize component modules. 1460Sstevel@tonic-gate */ 1470Sstevel@tonic-gate static void 1480Sstevel@tonic-gate drv_init(void) 1490Sstevel@tonic-gate { 1503147Sxc151355 drv_secobj_init(); 1510Sstevel@tonic-gate dld_str_init(); 1525895Syz147064 /* 1535895Syz147064 * Create a hash table for autopush configuration. 1545895Syz147064 */ 1555895Syz147064 dld_ap_hashp = mod_hash_create_idhash("dld_autopush_hash", 1565895Syz147064 NAUTOPUSH, mod_hash_null_valdtor); 1575895Syz147064 1585895Syz147064 ASSERT(dld_ap_hashp != NULL); 1595895Syz147064 rw_init(&dld_ap_hash_lock, NULL, RW_DRIVER, NULL); 1605895Syz147064 } 1615895Syz147064 1625895Syz147064 /* ARGSUSED */ 1635895Syz147064 static uint_t 1645895Syz147064 drv_ap_exist(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 1655895Syz147064 { 1665895Syz147064 boolean_t *pexist = arg; 1675895Syz147064 1685895Syz147064 *pexist = B_TRUE; 1695895Syz147064 return (MH_WALK_TERMINATE); 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate static int 1730Sstevel@tonic-gate drv_fini(void) 1740Sstevel@tonic-gate { 1755895Syz147064 int err; 1765895Syz147064 boolean_t exist = B_FALSE; 1775895Syz147064 1785895Syz147064 rw_enter(&dld_ap_hash_lock, RW_READER); 1795895Syz147064 mod_hash_walk(dld_ap_hashp, drv_ap_exist, &exist); 1805895Syz147064 rw_exit(&dld_ap_hash_lock); 1815895Syz147064 1825895Syz147064 if (exist) 1835895Syz147064 return (EBUSY); 1840Sstevel@tonic-gate 185269Sericheng if ((err = dld_str_fini()) != 0) 1860Sstevel@tonic-gate return (err); 1870Sstevel@tonic-gate 1883147Sxc151355 drv_secobj_fini(); 1895895Syz147064 mod_hash_destroy_idhash(dld_ap_hashp); 1905895Syz147064 rw_destroy(&dld_ap_hash_lock); 1910Sstevel@tonic-gate return (0); 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate /* 1950Sstevel@tonic-gate * devo_getinfo: getinfo(9e) 1960Sstevel@tonic-gate */ 1970Sstevel@tonic-gate /*ARGSUSED*/ 1980Sstevel@tonic-gate static int 1990Sstevel@tonic-gate drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resp) 2000Sstevel@tonic-gate { 2010Sstevel@tonic-gate if (dld_dip == NULL) 2020Sstevel@tonic-gate return (DDI_FAILURE); 2030Sstevel@tonic-gate 2040Sstevel@tonic-gate switch (cmd) { 2050Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 206*7408SSebastien.Roy@Sun.COM *resp = 0; 2070Sstevel@tonic-gate break; 2080Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 209*7408SSebastien.Roy@Sun.COM *resp = dld_dip; 2100Sstevel@tonic-gate break; 2110Sstevel@tonic-gate default: 2120Sstevel@tonic-gate return (DDI_FAILURE); 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate return (DDI_SUCCESS); 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate /* 2190Sstevel@tonic-gate * Check properties to set options. (See dld.h for property definitions). 2200Sstevel@tonic-gate */ 2210Sstevel@tonic-gate static void 2220Sstevel@tonic-gate drv_set_opt(dev_info_t *dip) 2230Sstevel@tonic-gate { 2240Sstevel@tonic-gate if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 2250Sstevel@tonic-gate DLD_PROP_NO_FASTPATH, 0) != 0) { 2260Sstevel@tonic-gate dld_opt |= DLD_OPT_NO_FASTPATH; 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 2300Sstevel@tonic-gate DLD_PROP_NO_POLL, 0) != 0) { 2310Sstevel@tonic-gate dld_opt |= DLD_OPT_NO_POLL; 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 2350Sstevel@tonic-gate DLD_PROP_NO_ZEROCOPY, 0) != 0) { 2360Sstevel@tonic-gate dld_opt |= DLD_OPT_NO_ZEROCOPY; 2370Sstevel@tonic-gate } 2384114Sja97890 2394114Sja97890 if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 2404114Sja97890 DLD_PROP_NO_SOFTRING, 0) != 0) { 2414114Sja97890 dld_opt |= DLD_OPT_NO_SOFTRING; 2424114Sja97890 } 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* 2460Sstevel@tonic-gate * devo_attach: attach(9e) 2470Sstevel@tonic-gate */ 2480Sstevel@tonic-gate static int 2490Sstevel@tonic-gate drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2500Sstevel@tonic-gate { 2510Sstevel@tonic-gate if (cmd != DDI_ATTACH) 2520Sstevel@tonic-gate return (DDI_FAILURE); 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate ASSERT(ddi_get_instance(dip) == 0); 255*7408SSebastien.Roy@Sun.COM drv_init(); 2560Sstevel@tonic-gate drv_set_opt(dip); 2570Sstevel@tonic-gate 2580Sstevel@tonic-gate /* 2590Sstevel@tonic-gate * Create control node. DLPI provider nodes will be created on demand. 2600Sstevel@tonic-gate */ 2610Sstevel@tonic-gate if (ddi_create_minor_node(dip, DLD_CONTROL_MINOR_NAME, S_IFCHR, 2620Sstevel@tonic-gate DLD_CONTROL_MINOR, DDI_PSEUDO, 0) != DDI_SUCCESS) 2630Sstevel@tonic-gate return (DDI_FAILURE); 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate dld_dip = dip; 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate /* 2680Sstevel@tonic-gate * Log the fact that the driver is now attached. 2690Sstevel@tonic-gate */ 2700Sstevel@tonic-gate ddi_report_dev(dip); 2710Sstevel@tonic-gate return (DDI_SUCCESS); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate /* 2750Sstevel@tonic-gate * devo_detach: detach(9e) 2760Sstevel@tonic-gate */ 2770Sstevel@tonic-gate static int 2780Sstevel@tonic-gate drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 2790Sstevel@tonic-gate { 2800Sstevel@tonic-gate if (cmd != DDI_DETACH) 2810Sstevel@tonic-gate return (DDI_FAILURE); 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate ASSERT(dld_dip == dip); 284*7408SSebastien.Roy@Sun.COM if (drv_fini() != 0) 285*7408SSebastien.Roy@Sun.COM return (DDI_FAILURE); 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate /* 2880Sstevel@tonic-gate * Remove the control node. 2890Sstevel@tonic-gate */ 2900Sstevel@tonic-gate ddi_remove_minor_node(dip, DLD_CONTROL_MINOR_NAME); 2910Sstevel@tonic-gate dld_dip = NULL; 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate return (DDI_SUCCESS); 2940Sstevel@tonic-gate } 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate /* 297269Sericheng * dld control node open procedure. 2980Sstevel@tonic-gate */ 2990Sstevel@tonic-gate /*ARGSUSED*/ 3000Sstevel@tonic-gate static int 301*7408SSebastien.Roy@Sun.COM drv_open(dev_t *devp, int flag, int sflag, cred_t *credp) 3020Sstevel@tonic-gate { 3030Sstevel@tonic-gate /* 304*7408SSebastien.Roy@Sun.COM * Only the control node can be opened. 3050Sstevel@tonic-gate */ 306*7408SSebastien.Roy@Sun.COM if (getminor(*devp) != DLD_CONTROL_MINOR) 307*7408SSebastien.Roy@Sun.COM return (ENODEV); 3080Sstevel@tonic-gate return (0); 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate /* 3125895Syz147064 * DLDIOC_ATTR 3130Sstevel@tonic-gate */ 314*7408SSebastien.Roy@Sun.COM /* ARGSUSED */ 315*7408SSebastien.Roy@Sun.COM static int 316*7408SSebastien.Roy@Sun.COM drv_ioc_attr(void *karg, intptr_t arg, int mode, cred_t *cred) 3170Sstevel@tonic-gate { 318*7408SSebastien.Roy@Sun.COM dld_ioc_attr_t *diap = karg; 3195895Syz147064 dls_dl_handle_t dlh; 3205895Syz147064 dls_vlan_t *dvp; 3215895Syz147064 int err; 3225895Syz147064 3235895Syz147064 if ((err = dls_devnet_hold_tmp(diap->dia_linkid, &dlh)) != 0) 324*7408SSebastien.Roy@Sun.COM return (err); 325269Sericheng 3265895Syz147064 if ((err = dls_vlan_hold(dls_devnet_mac(dlh), 3275895Syz147064 dls_devnet_vid(dlh), &dvp, B_FALSE, B_FALSE)) != 0) { 3285895Syz147064 dls_devnet_rele_tmp(dlh); 329*7408SSebastien.Roy@Sun.COM return (err); 330269Sericheng } 3315903Ssowmini mac_sdu_get(dvp->dv_dlp->dl_mh, NULL, &diap->dia_max_sdu); 332269Sericheng 333269Sericheng dls_vlan_rele(dvp); 3345895Syz147064 dls_devnet_rele_tmp(dlh); 3350Sstevel@tonic-gate 336*7408SSebastien.Roy@Sun.COM return (0); 337269Sericheng } 338269Sericheng 3393448Sdh155122 /* 3405895Syz147064 * DLDIOC_PHYS_ATTR 3413448Sdh155122 */ 342*7408SSebastien.Roy@Sun.COM /* ARGSUSED */ 343*7408SSebastien.Roy@Sun.COM static int 344*7408SSebastien.Roy@Sun.COM drv_ioc_phys_attr(void *karg, intptr_t arg, int mode, cred_t *cred) 3453448Sdh155122 { 346*7408SSebastien.Roy@Sun.COM dld_ioc_phys_attr_t *dipp = karg; 3475895Syz147064 int err; 3485895Syz147064 dls_dl_handle_t dlh; 3495895Syz147064 dls_dev_handle_t ddh; 3505895Syz147064 dev_t phydev; 3515895Syz147064 3525895Syz147064 /* 3535895Syz147064 * Every physical link should have its physical dev_t kept in the 3545895Syz147064 * daemon. If not, it is not a valid physical link. 3555895Syz147064 */ 356*7408SSebastien.Roy@Sun.COM if (dls_mgmt_get_phydev(dipp->dip_linkid, &phydev) != 0) 357*7408SSebastien.Roy@Sun.COM return (EINVAL); 3583448Sdh155122 3594049Sdh155122 /* 3605895Syz147064 * Although this is a valid physical link, it might already be removed 3615895Syz147064 * by DR or during system shutdown. softmac_hold_device() would return 3625895Syz147064 * ENOENT in this case. 3635895Syz147064 */ 3645895Syz147064 if ((err = softmac_hold_device(phydev, &ddh)) != 0) 365*7408SSebastien.Roy@Sun.COM return (err); 3665895Syz147064 3675895Syz147064 if (dls_devnet_hold_tmp(dipp->dip_linkid, &dlh) != 0) { 3685895Syz147064 /* 3695895Syz147064 * Although this is an active physical link, its link type is 3705895Syz147064 * not supported by GLDv3, and therefore it does not have 3715895Syz147064 * vanity naming support. 3725895Syz147064 */ 3735895Syz147064 dipp->dip_novanity = B_TRUE; 3745895Syz147064 } else { 3755895Syz147064 dipp->dip_novanity = B_FALSE; 3765895Syz147064 dls_devnet_rele_tmp(dlh); 3775895Syz147064 } 3785895Syz147064 /* 3795895Syz147064 * Get the physical device name from the major number and the instance 3805895Syz147064 * number derived from phydev. 3814049Sdh155122 */ 3825895Syz147064 (void) snprintf(dipp->dip_dev, MAXLINKNAMELEN, "%s%d", 3835895Syz147064 ddi_major_to_name(getmajor(phydev)), getminor(phydev) - 1); 3845895Syz147064 3855895Syz147064 softmac_rele_device(ddh); 386*7408SSebastien.Roy@Sun.COM return (0); 3875895Syz147064 } 3885895Syz147064 3895895Syz147064 /* 3906789Sam223141 * DLDIOC_SETPROP 3915903Ssowmini */ 392*7408SSebastien.Roy@Sun.COM static int 393*7408SSebastien.Roy@Sun.COM drv_ioc_prop_common(dld_ioc_macprop_t *dipp, intptr_t arg, boolean_t set, 394*7408SSebastien.Roy@Sun.COM int mode) 3955903Ssowmini { 396*7408SSebastien.Roy@Sun.COM int err = EINVAL; 397*7408SSebastien.Roy@Sun.COM size_t dsize; 398*7408SSebastien.Roy@Sun.COM dld_ioc_macprop_t *kdipp; 399*7408SSebastien.Roy@Sun.COM dls_dl_handle_t dlh; 4005903Ssowmini dls_vlan_t *dvp; 4015903Ssowmini datalink_id_t linkid; 4025903Ssowmini mac_prop_t macprop; 4037342SAruna.Ramakrishna@Sun.COM uchar_t *cp; 4047342SAruna.Ramakrishna@Sun.COM struct dlautopush *dlap; 4057342SAruna.Ramakrishna@Sun.COM dld_ioc_zid_t *dzp; 4065903Ssowmini 407*7408SSebastien.Roy@Sun.COM /* 408*7408SSebastien.Roy@Sun.COM * We only use pr_valsize from dipp, as the caller only did a 409*7408SSebastien.Roy@Sun.COM * copyin() for sizeof (dld_ioc_prop_t), which doesn't cover 410*7408SSebastien.Roy@Sun.COM * the property data. We copyin the full dld_ioc_prop_t 411*7408SSebastien.Roy@Sun.COM * including the data into kdipp down below. 412*7408SSebastien.Roy@Sun.COM */ 4136789Sam223141 dsize = sizeof (dld_ioc_macprop_t) + dipp->pr_valsize - 1; 414*7408SSebastien.Roy@Sun.COM if (dsize < dipp->pr_valsize) 415*7408SSebastien.Roy@Sun.COM return (EINVAL); 416*7408SSebastien.Roy@Sun.COM 417*7408SSebastien.Roy@Sun.COM /* 418*7408SSebastien.Roy@Sun.COM * The property data is variable size, so we need to allocate 419*7408SSebastien.Roy@Sun.COM * a buffer for kernel use as this data was not part of the 420*7408SSebastien.Roy@Sun.COM * dipp allocation and copyin() done by the framework. 421*7408SSebastien.Roy@Sun.COM */ 422*7408SSebastien.Roy@Sun.COM if ((kdipp = kmem_alloc(dsize, KM_NOSLEEP)) == NULL) 423*7408SSebastien.Roy@Sun.COM return (ENOMEM); 424*7408SSebastien.Roy@Sun.COM if (ddi_copyin((void *)arg, kdipp, dsize, mode) != 0) { 425*7408SSebastien.Roy@Sun.COM err = EFAULT; 4265903Ssowmini goto done; 427*7408SSebastien.Roy@Sun.COM } 4285903Ssowmini 429*7408SSebastien.Roy@Sun.COM linkid = kdipp->pr_linkid; 4305903Ssowmini 4317342SAruna.Ramakrishna@Sun.COM switch (dipp->pr_num) { 4327342SAruna.Ramakrishna@Sun.COM case MAC_PROP_ZONE: 4337342SAruna.Ramakrishna@Sun.COM if (set) { 434*7408SSebastien.Roy@Sun.COM dzp = (dld_ioc_zid_t *)kdipp->pr_val; 4357342SAruna.Ramakrishna@Sun.COM err = dls_devnet_setzid(dzp->diz_link, dzp->diz_zid); 4367342SAruna.Ramakrishna@Sun.COM goto done; 4377342SAruna.Ramakrishna@Sun.COM } else { 438*7408SSebastien.Roy@Sun.COM cp = (uchar_t *)kdipp->pr_val; 4397342SAruna.Ramakrishna@Sun.COM err = dls_devnet_getzid(linkid, (zoneid_t *)cp); 4407342SAruna.Ramakrishna@Sun.COM goto done; 4417342SAruna.Ramakrishna@Sun.COM } 4427342SAruna.Ramakrishna@Sun.COM case MAC_PROP_AUTOPUSH: 4437342SAruna.Ramakrishna@Sun.COM if (set) { 4447342SAruna.Ramakrishna@Sun.COM if (dipp->pr_valsize != 0) { 445*7408SSebastien.Roy@Sun.COM dlap = (struct dlautopush *)kdipp->pr_val; 4467342SAruna.Ramakrishna@Sun.COM err = drv_ioc_setap(linkid, dlap); 4477342SAruna.Ramakrishna@Sun.COM goto done; 4487342SAruna.Ramakrishna@Sun.COM } else { 4497342SAruna.Ramakrishna@Sun.COM err = drv_ioc_clrap(linkid); 4507342SAruna.Ramakrishna@Sun.COM goto done; 4517342SAruna.Ramakrishna@Sun.COM } 4527342SAruna.Ramakrishna@Sun.COM } else { 453*7408SSebastien.Roy@Sun.COM dlap = (struct dlautopush *)kdipp->pr_val; 4547342SAruna.Ramakrishna@Sun.COM err = drv_ioc_getap(linkid, dlap); 4557342SAruna.Ramakrishna@Sun.COM goto done; 4567342SAruna.Ramakrishna@Sun.COM } 4577342SAruna.Ramakrishna@Sun.COM 4587342SAruna.Ramakrishna@Sun.COM default: 4597342SAruna.Ramakrishna@Sun.COM break; 4607342SAruna.Ramakrishna@Sun.COM } 4617342SAruna.Ramakrishna@Sun.COM 4625903Ssowmini if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0) 4635903Ssowmini goto done; 4645903Ssowmini 4655903Ssowmini if ((err = dls_vlan_hold(dls_devnet_mac(dlh), 4665903Ssowmini dls_devnet_vid(dlh), &dvp, B_FALSE, B_FALSE)) != 0) { 4675903Ssowmini dls_devnet_rele_tmp(dlh); 4685903Ssowmini goto done; 4695903Ssowmini } 4705903Ssowmini 471*7408SSebastien.Roy@Sun.COM macprop.mp_name = kdipp->pr_name; 472*7408SSebastien.Roy@Sun.COM macprop.mp_id = kdipp->pr_num; 473*7408SSebastien.Roy@Sun.COM macprop.mp_flags = kdipp->pr_flags; 4745903Ssowmini 475*7408SSebastien.Roy@Sun.COM if (set) { 4765903Ssowmini err = mac_set_prop(dvp->dv_dlp->dl_mh, &macprop, 477*7408SSebastien.Roy@Sun.COM kdipp->pr_val, kdipp->pr_valsize); 478*7408SSebastien.Roy@Sun.COM } else { 4795903Ssowmini err = mac_get_prop(dvp->dv_dlp->dl_mh, &macprop, 480*7408SSebastien.Roy@Sun.COM kdipp->pr_val, kdipp->pr_valsize); 481*7408SSebastien.Roy@Sun.COM } 4825903Ssowmini 4835903Ssowmini dls_vlan_rele(dvp); 4845903Ssowmini dls_devnet_rele_tmp(dlh); 4855903Ssowmini done: 486*7408SSebastien.Roy@Sun.COM if (!set && err == 0 && 487*7408SSebastien.Roy@Sun.COM ddi_copyout(kdipp, (void *)arg, dsize, mode) != 0) 488*7408SSebastien.Roy@Sun.COM err = EFAULT; 489*7408SSebastien.Roy@Sun.COM kmem_free(kdipp, dsize); 490*7408SSebastien.Roy@Sun.COM return (err); 4915903Ssowmini } 4925903Ssowmini 493*7408SSebastien.Roy@Sun.COM /* ARGSUSED */ 494*7408SSebastien.Roy@Sun.COM static int 495*7408SSebastien.Roy@Sun.COM drv_ioc_setprop(void *karg, intptr_t arg, int mode, cred_t *cred) 4965903Ssowmini { 497*7408SSebastien.Roy@Sun.COM return (drv_ioc_prop_common(karg, arg, B_TRUE, mode)); 4985903Ssowmini } 4995903Ssowmini 500*7408SSebastien.Roy@Sun.COM /* ARGSUSED */ 501*7408SSebastien.Roy@Sun.COM static int 502*7408SSebastien.Roy@Sun.COM drv_ioc_getprop(void *karg, intptr_t arg, int mode, cred_t *cred) 5035903Ssowmini { 504*7408SSebastien.Roy@Sun.COM return (drv_ioc_prop_common(karg, arg, B_FALSE, mode)); 5055903Ssowmini } 5065903Ssowmini 5075903Ssowmini /* 5085895Syz147064 * DLDIOC_CREATE_VLAN 5095895Syz147064 */ 510*7408SSebastien.Roy@Sun.COM /* ARGSUSED */ 511*7408SSebastien.Roy@Sun.COM static int 512*7408SSebastien.Roy@Sun.COM drv_ioc_create_vlan(void *karg, intptr_t arg, int mode, cred_t *cred) 5135895Syz147064 { 514*7408SSebastien.Roy@Sun.COM dld_ioc_create_vlan_t *dicp = karg; 5154049Sdh155122 516*7408SSebastien.Roy@Sun.COM return (dls_devnet_create_vlan(dicp->dic_vlanid, dicp->dic_linkid, 517*7408SSebastien.Roy@Sun.COM dicp->dic_vid, dicp->dic_force)); 5185895Syz147064 } 5195895Syz147064 5205895Syz147064 /* 5215895Syz147064 * DLDIOC_DELETE_VLAN 5225895Syz147064 */ 523*7408SSebastien.Roy@Sun.COM /* ARGSUSED */ 524*7408SSebastien.Roy@Sun.COM static int 525*7408SSebastien.Roy@Sun.COM drv_ioc_delete_vlan(void *karg, intptr_t arg, int mode, cred_t *cred) 5265895Syz147064 { 527*7408SSebastien.Roy@Sun.COM dld_ioc_delete_vlan_t *didp = karg; 5285895Syz147064 529*7408SSebastien.Roy@Sun.COM return (dls_devnet_destroy_vlan(didp->did_linkid)); 5305895Syz147064 } 5315895Syz147064 5325895Syz147064 /* 5335895Syz147064 * DLDIOC_VLAN_ATTR 5345895Syz147064 */ 535*7408SSebastien.Roy@Sun.COM /* ARGSUSED */ 536*7408SSebastien.Roy@Sun.COM static int 537*7408SSebastien.Roy@Sun.COM drv_ioc_vlan_attr(void *karg, intptr_t arg, int mode, cred_t *cred) 5385895Syz147064 { 539*7408SSebastien.Roy@Sun.COM dld_ioc_vlan_attr_t *divp = karg; 5405895Syz147064 dls_dl_handle_t dlh; 5415895Syz147064 uint16_t vid; 5425895Syz147064 dls_vlan_t *dvp; 5435895Syz147064 int err; 5445895Syz147064 5455895Syz147064 /* 5465895Syz147064 * Hold this link to prevent it from being deleted. 5475895Syz147064 */ 548*7408SSebastien.Roy@Sun.COM if ((err = dls_devnet_hold_tmp(divp->div_vlanid, &dlh)) != 0) 549*7408SSebastien.Roy@Sun.COM return (err); 5504049Sdh155122 5515895Syz147064 if ((vid = dls_devnet_vid(dlh)) == VLAN_ID_NONE) { 5525895Syz147064 dls_devnet_rele_tmp(dlh); 553*7408SSebastien.Roy@Sun.COM return (EINVAL); 5545895Syz147064 } 5555895Syz147064 5565895Syz147064 err = dls_vlan_hold(dls_devnet_mac(dlh), vid, &dvp, B_FALSE, B_FALSE); 5575895Syz147064 if (err != 0) { 5585895Syz147064 dls_devnet_rele_tmp(dlh); 559*7408SSebastien.Roy@Sun.COM return (err); 5603871Syz147064 } 5615895Syz147064 5625895Syz147064 divp->div_linkid = dls_devnet_linkid(dlh); 5635895Syz147064 divp->div_implicit = !dls_devnet_is_explicit(dlh); 5645895Syz147064 divp->div_vid = vid; 5655895Syz147064 divp->div_force = dvp->dv_force; 5665895Syz147064 5675895Syz147064 dls_vlan_rele(dvp); 5685895Syz147064 dls_devnet_rele_tmp(dlh); 569*7408SSebastien.Roy@Sun.COM return (0); 5703448Sdh155122 } 5713448Sdh155122 5723448Sdh155122 /* 5735895Syz147064 * DLDIOC_RENAME. 5745895Syz147064 * 5755895Syz147064 * This function handles two cases of link renaming. See more in comments above 5765895Syz147064 * dls_datalink_rename(). 5775895Syz147064 */ 578*7408SSebastien.Roy@Sun.COM /* ARGSUSED */ 579*7408SSebastien.Roy@Sun.COM static int 580*7408SSebastien.Roy@Sun.COM drv_ioc_rename(void *karg, intptr_t arg, int mode, cred_t *cred) 5815895Syz147064 { 582*7408SSebastien.Roy@Sun.COM dld_ioc_rename_t *dir = karg; 5835895Syz147064 mod_hash_key_t key; 5845895Syz147064 mod_hash_val_t val; 5855895Syz147064 int err; 5865895Syz147064 5875895Syz147064 if ((err = dls_devnet_rename(dir->dir_linkid1, dir->dir_linkid2, 588*7408SSebastien.Roy@Sun.COM dir->dir_link)) != 0) 589*7408SSebastien.Roy@Sun.COM return (err); 5905895Syz147064 5915895Syz147064 if (dir->dir_linkid2 == DATALINK_INVALID_LINKID) 592*7408SSebastien.Roy@Sun.COM return (0); 5935895Syz147064 5945895Syz147064 /* 5955895Syz147064 * if dir_linkid2 is not DATALINK_INVALID_LINKID, it means this 5965895Syz147064 * renaming request is to rename a valid physical link (dir_linkid1) 5975895Syz147064 * to a "removed" physical link (dir_linkid2, which is removed by DR 5985895Syz147064 * or during system shutdown). In this case, the link (specified by 5995895Syz147064 * dir_linkid1) would inherit all the configuration of dir_linkid2, 6005895Syz147064 * and dir_linkid1 and its configuration would be lost. 6015895Syz147064 * 6025895Syz147064 * Remove per-link autopush configuration of dir_linkid1 in this case. 6035895Syz147064 */ 6045895Syz147064 key = (mod_hash_key_t)(uintptr_t)dir->dir_linkid1; 6055895Syz147064 rw_enter(&dld_ap_hash_lock, RW_WRITER); 6065895Syz147064 if (mod_hash_find(dld_ap_hashp, key, &val) != 0) { 6075895Syz147064 rw_exit(&dld_ap_hash_lock); 608*7408SSebastien.Roy@Sun.COM return (0); 6095895Syz147064 } 6105895Syz147064 6115895Syz147064 VERIFY(mod_hash_remove(dld_ap_hashp, key, &val) == 0); 6125895Syz147064 kmem_free(val, sizeof (dld_ap_t)); 6135895Syz147064 rw_exit(&dld_ap_hash_lock); 614*7408SSebastien.Roy@Sun.COM return (0); 6155895Syz147064 } 6165895Syz147064 6177342SAruna.Ramakrishna@Sun.COM static int 6187342SAruna.Ramakrishna@Sun.COM drv_ioc_setap(datalink_id_t linkid, struct dlautopush *dlap) 6193448Sdh155122 { 6205895Syz147064 dld_ap_t *dap; 621*7408SSebastien.Roy@Sun.COM int i; 6225895Syz147064 mod_hash_key_t key; 6233448Sdh155122 624*7408SSebastien.Roy@Sun.COM if (dlap->dap_npush == 0 || dlap->dap_npush > MAXAPUSH) 625*7408SSebastien.Roy@Sun.COM return (EINVAL); 6263448Sdh155122 6275895Syz147064 /* 6285895Syz147064 * Validate that the specified list of modules exist. 6295895Syz147064 */ 6307342SAruna.Ramakrishna@Sun.COM for (i = 0; i < dlap->dap_npush; i++) { 631*7408SSebastien.Roy@Sun.COM if (fmodsw_find(dlap->dap_aplist[i], FMODSW_LOAD) == NULL) 632*7408SSebastien.Roy@Sun.COM return (EINVAL); 6333448Sdh155122 } 6343448Sdh155122 635*7408SSebastien.Roy@Sun.COM 6367342SAruna.Ramakrishna@Sun.COM key = (mod_hash_key_t)(uintptr_t)linkid; 6375895Syz147064 6385895Syz147064 rw_enter(&dld_ap_hash_lock, RW_WRITER); 6395895Syz147064 if (mod_hash_find(dld_ap_hashp, key, (mod_hash_val_t *)&dap) != 0) { 6405895Syz147064 dap = kmem_zalloc(sizeof (dld_ap_t), KM_NOSLEEP); 6415895Syz147064 if (dap == NULL) { 6425895Syz147064 rw_exit(&dld_ap_hash_lock); 643*7408SSebastien.Roy@Sun.COM return (ENOMEM); 6445895Syz147064 } 6455895Syz147064 6467342SAruna.Ramakrishna@Sun.COM dap->da_linkid = linkid; 647*7408SSebastien.Roy@Sun.COM VERIFY(mod_hash_insert(dld_ap_hashp, key, 648*7408SSebastien.Roy@Sun.COM (mod_hash_val_t)dap) == 0); 6493448Sdh155122 } 6503448Sdh155122 6515895Syz147064 /* 6525895Syz147064 * Update the configuration. 6535895Syz147064 */ 6547342SAruna.Ramakrishna@Sun.COM dap->da_anchor = dlap->dap_anchor; 6557342SAruna.Ramakrishna@Sun.COM dap->da_npush = dlap->dap_npush; 6567342SAruna.Ramakrishna@Sun.COM for (i = 0; i < dlap->dap_npush; i++) { 6577342SAruna.Ramakrishna@Sun.COM (void) strlcpy(dap->da_aplist[i], dlap->dap_aplist[i], 6585895Syz147064 FMNAMESZ + 1); 6595895Syz147064 } 6605895Syz147064 rw_exit(&dld_ap_hash_lock); 6615895Syz147064 6627342SAruna.Ramakrishna@Sun.COM return (0); 6633448Sdh155122 } 6643448Sdh155122 6657342SAruna.Ramakrishna@Sun.COM static int 6667342SAruna.Ramakrishna@Sun.COM drv_ioc_getap(datalink_id_t linkid, struct dlautopush *dlap) 6675895Syz147064 { 6685895Syz147064 dld_ap_t *dap; 669*7408SSebastien.Roy@Sun.COM int i; 6705895Syz147064 6715895Syz147064 rw_enter(&dld_ap_hash_lock, RW_READER); 6725895Syz147064 if (mod_hash_find(dld_ap_hashp, 6737342SAruna.Ramakrishna@Sun.COM (mod_hash_key_t)(uintptr_t)linkid, 6745895Syz147064 (mod_hash_val_t *)&dap) != 0) { 6755895Syz147064 rw_exit(&dld_ap_hash_lock); 676*7408SSebastien.Roy@Sun.COM return (ENOENT); 6775895Syz147064 } 6785895Syz147064 6795895Syz147064 /* 6805895Syz147064 * Retrieve the configuration. 6815895Syz147064 */ 6827342SAruna.Ramakrishna@Sun.COM dlap->dap_anchor = dap->da_anchor; 6837342SAruna.Ramakrishna@Sun.COM dlap->dap_npush = dap->da_npush; 6845895Syz147064 for (i = 0; i < dap->da_npush; i++) { 6857342SAruna.Ramakrishna@Sun.COM (void) strlcpy(dlap->dap_aplist[i], dap->da_aplist[i], 6865895Syz147064 FMNAMESZ + 1); 6875895Syz147064 } 6885895Syz147064 rw_exit(&dld_ap_hash_lock); 6895895Syz147064 6907342SAruna.Ramakrishna@Sun.COM return (0); 6915895Syz147064 } 6925895Syz147064 6937342SAruna.Ramakrishna@Sun.COM static int 6947342SAruna.Ramakrishna@Sun.COM drv_ioc_clrap(datalink_id_t linkid) 6955895Syz147064 { 6965895Syz147064 mod_hash_val_t val; 6975895Syz147064 mod_hash_key_t key; 6985895Syz147064 6997342SAruna.Ramakrishna@Sun.COM key = (mod_hash_key_t)(uintptr_t)linkid; 7005895Syz147064 7015895Syz147064 rw_enter(&dld_ap_hash_lock, RW_WRITER); 7025895Syz147064 if (mod_hash_find(dld_ap_hashp, key, &val) != 0) { 7035895Syz147064 rw_exit(&dld_ap_hash_lock); 7047342SAruna.Ramakrishna@Sun.COM return (0); 7055895Syz147064 } 7065895Syz147064 7075895Syz147064 VERIFY(mod_hash_remove(dld_ap_hashp, key, &val) == 0); 7085895Syz147064 kmem_free(val, sizeof (dld_ap_t)); 7095895Syz147064 rw_exit(&dld_ap_hash_lock); 7107342SAruna.Ramakrishna@Sun.COM return (0); 7115895Syz147064 } 7125895Syz147064 7135895Syz147064 /* 7145895Syz147064 * DLDIOC_DOORSERVER 7155895Syz147064 */ 716*7408SSebastien.Roy@Sun.COM /* ARGSUSED */ 717*7408SSebastien.Roy@Sun.COM static int 718*7408SSebastien.Roy@Sun.COM drv_ioc_doorserver(void *karg, intptr_t arg, int mode, cred_t *cred) 7193448Sdh155122 { 720*7408SSebastien.Roy@Sun.COM dld_ioc_door_t *did = karg; 721269Sericheng 722*7408SSebastien.Roy@Sun.COM return (dls_mgmt_door_set(did->did_start_door)); 7230Sstevel@tonic-gate } 7243147Sxc151355 7253147Sxc151355 /* 7265895Syz147064 * Check for GLDv3 autopush information. There are three cases: 7275895Syz147064 * 7285895Syz147064 * 1. If devp points to a GLDv3 datalink and it has autopush configuration, 7295895Syz147064 * fill dlap in with that information and return 0. 7305895Syz147064 * 7315895Syz147064 * 2. If devp points to a GLDv3 datalink but it doesn't have autopush 7325895Syz147064 * configuration, then replace devp with the physical device (if one 7335895Syz147064 * exists) and return 1. This allows stropen() to find the old-school 7345895Syz147064 * per-driver autopush configuration. (For softmac, the result is that 7355895Syz147064 * the softmac dev_t is replaced with the legacy device's dev_t). 7365895Syz147064 * 7375895Syz147064 * 3. If neither of the above apply, don't touch the args and return -1. 7385895Syz147064 */ 7395895Syz147064 int 7405895Syz147064 dld_autopush(dev_t *devp, struct dlautopush *dlap) 7415895Syz147064 { 7425895Syz147064 dld_ap_t *dap; 7435895Syz147064 datalink_id_t linkid; 7445895Syz147064 dev_t phydev; 7455895Syz147064 7465895Syz147064 if (!GLDV3_DRV(getmajor(*devp))) 7475895Syz147064 return (-1); 7485895Syz147064 7495895Syz147064 /* 7505895Syz147064 * Find the linkid by the link's dev_t. 7515895Syz147064 */ 7525895Syz147064 if (dls_devnet_dev2linkid(*devp, &linkid) != 0) 7535895Syz147064 return (-1); 7545895Syz147064 7555895Syz147064 /* 7565895Syz147064 * Find the autopush configuration associated with the linkid. 7575895Syz147064 */ 7585895Syz147064 rw_enter(&dld_ap_hash_lock, RW_READER); 7595895Syz147064 if (mod_hash_find(dld_ap_hashp, (mod_hash_key_t)(uintptr_t)linkid, 7605895Syz147064 (mod_hash_val_t *)&dap) == 0) { 7615895Syz147064 *dlap = dap->da_ap; 7625895Syz147064 rw_exit(&dld_ap_hash_lock); 7635895Syz147064 return (0); 7645895Syz147064 } 7655895Syz147064 rw_exit(&dld_ap_hash_lock); 7665895Syz147064 7675895Syz147064 if (dls_devnet_phydev(linkid, &phydev) != 0) 7685895Syz147064 return (-1); 7695895Syz147064 7705895Syz147064 *devp = phydev; 7715895Syz147064 return (1); 7725895Syz147064 } 7735895Syz147064 7745895Syz147064 /* 7753147Sxc151355 * Secure objects implementation 7763147Sxc151355 */ 7773147Sxc151355 7783147Sxc151355 /* ARGSUSED */ 7793147Sxc151355 static int 7803147Sxc151355 drv_secobj_ctor(void *buf, void *arg, int kmflag) 7813147Sxc151355 { 7823147Sxc151355 bzero(buf, sizeof (dld_secobj_t)); 7833147Sxc151355 return (0); 7843147Sxc151355 } 7853147Sxc151355 7863147Sxc151355 static void 7873147Sxc151355 drv_secobj_init(void) 7883147Sxc151355 { 7893147Sxc151355 rw_init(&drv_secobj_lock, NULL, RW_DEFAULT, NULL); 7903147Sxc151355 drv_secobj_cachep = kmem_cache_create("drv_secobj_cache", 7913147Sxc151355 sizeof (dld_secobj_t), 0, drv_secobj_ctor, NULL, 7923147Sxc151355 NULL, NULL, NULL, 0); 7933147Sxc151355 drv_secobj_hash = mod_hash_create_extended("drv_secobj_hash", 7943147Sxc151355 SECOBJ_WEP_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor, 7953147Sxc151355 mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP); 7963147Sxc151355 } 7973147Sxc151355 7983147Sxc151355 static void 7993147Sxc151355 drv_secobj_fini(void) 8003147Sxc151355 { 8013147Sxc151355 mod_hash_destroy_hash(drv_secobj_hash); 8023147Sxc151355 kmem_cache_destroy(drv_secobj_cachep); 8033147Sxc151355 rw_destroy(&drv_secobj_lock); 8043147Sxc151355 } 8053147Sxc151355 806*7408SSebastien.Roy@Sun.COM /* ARGSUSED */ 807*7408SSebastien.Roy@Sun.COM static int 808*7408SSebastien.Roy@Sun.COM drv_ioc_secobj_set(void *karg, intptr_t arg, int mode, cred_t *cred) 8093147Sxc151355 { 810*7408SSebastien.Roy@Sun.COM dld_ioc_secobj_set_t *ssp = karg; 8113147Sxc151355 dld_secobj_t *sobjp, *objp; 812*7408SSebastien.Roy@Sun.COM int err; 8133147Sxc151355 8143147Sxc151355 sobjp = &ssp->ss_obj; 8153147Sxc151355 8164126Szf162725 if (sobjp->so_class != DLD_SECOBJ_CLASS_WEP && 8174126Szf162725 sobjp->so_class != DLD_SECOBJ_CLASS_WPA) 818*7408SSebastien.Roy@Sun.COM return (EINVAL); 8193147Sxc151355 8203147Sxc151355 if (sobjp->so_name[DLD_SECOBJ_NAME_MAX - 1] != '\0' || 8213147Sxc151355 sobjp->so_len > DLD_SECOBJ_VAL_MAX) 822*7408SSebastien.Roy@Sun.COM return (EINVAL); 8233147Sxc151355 8243147Sxc151355 rw_enter(&drv_secobj_lock, RW_WRITER); 8253147Sxc151355 err = mod_hash_find(drv_secobj_hash, (mod_hash_key_t)sobjp->so_name, 8263147Sxc151355 (mod_hash_val_t *)&objp); 8273147Sxc151355 if (err == 0) { 8283147Sxc151355 if ((ssp->ss_flags & DLD_SECOBJ_OPT_CREATE) != 0) { 8293147Sxc151355 rw_exit(&drv_secobj_lock); 830*7408SSebastien.Roy@Sun.COM return (EEXIST); 8313147Sxc151355 } 8323147Sxc151355 } else { 8333147Sxc151355 ASSERT(err == MH_ERR_NOTFOUND); 8343147Sxc151355 if ((ssp->ss_flags & DLD_SECOBJ_OPT_CREATE) == 0) { 8353147Sxc151355 rw_exit(&drv_secobj_lock); 836*7408SSebastien.Roy@Sun.COM return (ENOENT); 8373147Sxc151355 } 8383147Sxc151355 objp = kmem_cache_alloc(drv_secobj_cachep, KM_SLEEP); 8393147Sxc151355 (void) strlcpy(objp->so_name, sobjp->so_name, 8403147Sxc151355 DLD_SECOBJ_NAME_MAX); 8413147Sxc151355 842*7408SSebastien.Roy@Sun.COM VERIFY(mod_hash_insert(drv_secobj_hash, 843*7408SSebastien.Roy@Sun.COM (mod_hash_key_t)objp->so_name, (mod_hash_val_t)objp) == 0); 8443147Sxc151355 } 8453147Sxc151355 bcopy(sobjp->so_val, objp->so_val, sobjp->so_len); 8463147Sxc151355 objp->so_len = sobjp->so_len; 8473147Sxc151355 objp->so_class = sobjp->so_class; 8483147Sxc151355 rw_exit(&drv_secobj_lock); 849*7408SSebastien.Roy@Sun.COM return (0); 8503147Sxc151355 } 8513147Sxc151355 8523147Sxc151355 typedef struct dld_secobj_state { 8533147Sxc151355 uint_t ss_free; 8543147Sxc151355 uint_t ss_count; 8553147Sxc151355 int ss_rc; 856*7408SSebastien.Roy@Sun.COM int ss_mode; 8573147Sxc151355 dld_secobj_t *ss_objp; 8583147Sxc151355 } dld_secobj_state_t; 8593147Sxc151355 8603147Sxc151355 /* ARGSUSED */ 8613147Sxc151355 static uint_t 8623147Sxc151355 drv_secobj_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 8633147Sxc151355 { 8643147Sxc151355 dld_secobj_state_t *statep = arg; 8653147Sxc151355 dld_secobj_t *sobjp = (dld_secobj_t *)val; 8663147Sxc151355 8673147Sxc151355 if (statep->ss_free < sizeof (dld_secobj_t)) { 8683147Sxc151355 statep->ss_rc = ENOSPC; 8693147Sxc151355 return (MH_WALK_TERMINATE); 8703147Sxc151355 } 871*7408SSebastien.Roy@Sun.COM if (ddi_copyout(sobjp, statep->ss_objp, sizeof (*sobjp), 872*7408SSebastien.Roy@Sun.COM statep->ss_mode) != 0) { 873*7408SSebastien.Roy@Sun.COM statep->ss_rc = EFAULT; 874*7408SSebastien.Roy@Sun.COM return (MH_WALK_TERMINATE); 875*7408SSebastien.Roy@Sun.COM } 8763147Sxc151355 statep->ss_objp++; 8773147Sxc151355 statep->ss_free -= sizeof (dld_secobj_t); 8783147Sxc151355 statep->ss_count++; 8793147Sxc151355 return (MH_WALK_CONTINUE); 8803147Sxc151355 } 8813147Sxc151355 882*7408SSebastien.Roy@Sun.COM /* ARGSUSED */ 883*7408SSebastien.Roy@Sun.COM static int 884*7408SSebastien.Roy@Sun.COM drv_ioc_secobj_get(void *karg, intptr_t arg, int mode, cred_t *cred) 8853147Sxc151355 { 886*7408SSebastien.Roy@Sun.COM dld_ioc_secobj_get_t *sgp = karg; 8873147Sxc151355 dld_secobj_t *sobjp, *objp; 888*7408SSebastien.Roy@Sun.COM int err; 8893147Sxc151355 8903147Sxc151355 sobjp = &sgp->sg_obj; 8913147Sxc151355 8923147Sxc151355 if (sobjp->so_name[DLD_SECOBJ_NAME_MAX - 1] != '\0') 893*7408SSebastien.Roy@Sun.COM return (EINVAL); 8943147Sxc151355 8953147Sxc151355 rw_enter(&drv_secobj_lock, RW_READER); 8963147Sxc151355 if (sobjp->so_name[0] != '\0') { 8973147Sxc151355 err = mod_hash_find(drv_secobj_hash, 8983147Sxc151355 (mod_hash_key_t)sobjp->so_name, (mod_hash_val_t *)&objp); 8993147Sxc151355 if (err != 0) { 9003147Sxc151355 ASSERT(err == MH_ERR_NOTFOUND); 9013147Sxc151355 rw_exit(&drv_secobj_lock); 902*7408SSebastien.Roy@Sun.COM return (ENOENT); 9033147Sxc151355 } 9043147Sxc151355 bcopy(objp->so_val, sobjp->so_val, objp->so_len); 9053147Sxc151355 sobjp->so_len = objp->so_len; 9063147Sxc151355 sobjp->so_class = objp->so_class; 9073147Sxc151355 sgp->sg_count = 1; 9083147Sxc151355 } else { 9093147Sxc151355 dld_secobj_state_t state; 9103147Sxc151355 911*7408SSebastien.Roy@Sun.COM state.ss_free = sgp->sg_size - sizeof (dld_ioc_secobj_get_t); 9123147Sxc151355 state.ss_count = 0; 9133147Sxc151355 state.ss_rc = 0; 914*7408SSebastien.Roy@Sun.COM state.ss_mode = mode; 915*7408SSebastien.Roy@Sun.COM state.ss_objp = (dld_secobj_t *)((uchar_t *)arg + 916*7408SSebastien.Roy@Sun.COM sizeof (dld_ioc_secobj_get_t)); 917*7408SSebastien.Roy@Sun.COM 9183147Sxc151355 mod_hash_walk(drv_secobj_hash, drv_secobj_walker, &state); 9193147Sxc151355 if (state.ss_rc != 0) { 9203147Sxc151355 rw_exit(&drv_secobj_lock); 921*7408SSebastien.Roy@Sun.COM return (state.ss_rc); 9223147Sxc151355 } 9233147Sxc151355 sgp->sg_count = state.ss_count; 9243147Sxc151355 } 9253147Sxc151355 rw_exit(&drv_secobj_lock); 926*7408SSebastien.Roy@Sun.COM return (0); 9273147Sxc151355 } 9283147Sxc151355 929*7408SSebastien.Roy@Sun.COM /* ARGSUSED */ 930*7408SSebastien.Roy@Sun.COM static int 931*7408SSebastien.Roy@Sun.COM drv_ioc_secobj_unset(void *karg, intptr_t arg, int mode, cred_t *cred) 9323147Sxc151355 { 933*7408SSebastien.Roy@Sun.COM dld_ioc_secobj_unset_t *sup = karg; 9343147Sxc151355 dld_secobj_t *objp; 9353147Sxc151355 mod_hash_val_t val; 936*7408SSebastien.Roy@Sun.COM int err; 9373147Sxc151355 9383147Sxc151355 if (sup->su_name[DLD_SECOBJ_NAME_MAX - 1] != '\0') 939*7408SSebastien.Roy@Sun.COM return (EINVAL); 9403147Sxc151355 9413147Sxc151355 rw_enter(&drv_secobj_lock, RW_WRITER); 9423147Sxc151355 err = mod_hash_find(drv_secobj_hash, (mod_hash_key_t)sup->su_name, 9433147Sxc151355 (mod_hash_val_t *)&objp); 9443147Sxc151355 if (err != 0) { 9453147Sxc151355 ASSERT(err == MH_ERR_NOTFOUND); 9463147Sxc151355 rw_exit(&drv_secobj_lock); 947*7408SSebastien.Roy@Sun.COM return (ENOENT); 9483147Sxc151355 } 949*7408SSebastien.Roy@Sun.COM VERIFY(mod_hash_remove(drv_secobj_hash, (mod_hash_key_t)sup->su_name, 950*7408SSebastien.Roy@Sun.COM (mod_hash_val_t *)&val) == 0); 9513147Sxc151355 ASSERT(objp == (dld_secobj_t *)val); 9523147Sxc151355 9533147Sxc151355 kmem_cache_free(drv_secobj_cachep, objp); 9543147Sxc151355 rw_exit(&drv_secobj_lock); 955*7408SSebastien.Roy@Sun.COM return (0); 956*7408SSebastien.Roy@Sun.COM } 957*7408SSebastien.Roy@Sun.COM 958*7408SSebastien.Roy@Sun.COM static dld_ioc_info_t drv_ioc_list[] = { 959*7408SSebastien.Roy@Sun.COM {DLDIOC_ATTR, DLDCOPYINOUT, sizeof (dld_ioc_attr_t), 960*7408SSebastien.Roy@Sun.COM drv_ioc_attr}, 961*7408SSebastien.Roy@Sun.COM {DLDIOC_PHYS_ATTR, DLDCOPYINOUT, sizeof (dld_ioc_phys_attr_t), 962*7408SSebastien.Roy@Sun.COM drv_ioc_phys_attr}, 963*7408SSebastien.Roy@Sun.COM {DLDIOC_SECOBJ_SET, DLDCOPYIN | DLDDLCONFIG, 964*7408SSebastien.Roy@Sun.COM sizeof (dld_ioc_secobj_set_t), drv_ioc_secobj_set}, 965*7408SSebastien.Roy@Sun.COM {DLDIOC_SECOBJ_GET, DLDCOPYINOUT | DLDDLCONFIG, 966*7408SSebastien.Roy@Sun.COM sizeof (dld_ioc_secobj_get_t), drv_ioc_secobj_get}, 967*7408SSebastien.Roy@Sun.COM {DLDIOC_SECOBJ_UNSET, DLDCOPYIN | DLDDLCONFIG, 968*7408SSebastien.Roy@Sun.COM sizeof (dld_ioc_secobj_unset_t), drv_ioc_secobj_unset}, 969*7408SSebastien.Roy@Sun.COM {DLDIOC_CREATE_VLAN, DLDCOPYIN | DLDDLCONFIG, 970*7408SSebastien.Roy@Sun.COM sizeof (dld_ioc_create_vlan_t), drv_ioc_create_vlan}, 971*7408SSebastien.Roy@Sun.COM {DLDIOC_DELETE_VLAN, DLDCOPYIN | DLDDLCONFIG, 972*7408SSebastien.Roy@Sun.COM sizeof (dld_ioc_delete_vlan_t), 973*7408SSebastien.Roy@Sun.COM drv_ioc_delete_vlan}, 974*7408SSebastien.Roy@Sun.COM {DLDIOC_VLAN_ATTR, DLDCOPYINOUT, sizeof (dld_ioc_vlan_attr_t), 975*7408SSebastien.Roy@Sun.COM drv_ioc_vlan_attr}, 976*7408SSebastien.Roy@Sun.COM {DLDIOC_DOORSERVER, DLDCOPYIN | DLDDLCONFIG, sizeof (dld_ioc_door_t), 977*7408SSebastien.Roy@Sun.COM drv_ioc_doorserver}, 978*7408SSebastien.Roy@Sun.COM {DLDIOC_RENAME, DLDCOPYIN | DLDDLCONFIG, sizeof (dld_ioc_rename_t), 979*7408SSebastien.Roy@Sun.COM drv_ioc_rename}, 980*7408SSebastien.Roy@Sun.COM {DLDIOC_GETMACPROP, DLDCOPYIN, sizeof (dld_ioc_macprop_t), 981*7408SSebastien.Roy@Sun.COM drv_ioc_getprop}, 982*7408SSebastien.Roy@Sun.COM {DLDIOC_SETMACPROP, DLDCOPYIN | DLDDLCONFIG, sizeof (dld_ioc_macprop_t), 983*7408SSebastien.Roy@Sun.COM drv_ioc_setprop} 984*7408SSebastien.Roy@Sun.COM }; 985*7408SSebastien.Roy@Sun.COM 986*7408SSebastien.Roy@Sun.COM typedef struct dld_ioc_modentry { 987*7408SSebastien.Roy@Sun.COM uint16_t dim_modid; /* Top 16 bits of ioctl command */ 988*7408SSebastien.Roy@Sun.COM char *dim_modname; /* Module to be loaded */ 989*7408SSebastien.Roy@Sun.COM dld_ioc_info_t *dim_list; /* array of ioctl structures */ 990*7408SSebastien.Roy@Sun.COM uint_t dim_count; /* number of elements in dim_list */ 991*7408SSebastien.Roy@Sun.COM } dld_ioc_modentry_t; 992*7408SSebastien.Roy@Sun.COM 993*7408SSebastien.Roy@Sun.COM /* 994*7408SSebastien.Roy@Sun.COM * For all modules except for dld, dim_list and dim_count are assigned 995*7408SSebastien.Roy@Sun.COM * when the modules register their ioctls in dld_ioc_register(). We 996*7408SSebastien.Roy@Sun.COM * can statically initialize dld's ioctls in-line here; there's no 997*7408SSebastien.Roy@Sun.COM * need for it to call dld_ioc_register() itself. 998*7408SSebastien.Roy@Sun.COM */ 999*7408SSebastien.Roy@Sun.COM static dld_ioc_modentry_t dld_ioc_modtable[] = { 1000*7408SSebastien.Roy@Sun.COM {DLD_IOC, "dld", drv_ioc_list, DLDIOCCNT(drv_ioc_list)}, 1001*7408SSebastien.Roy@Sun.COM {AGGR_IOC, "aggr", NULL, 0}, 1002*7408SSebastien.Roy@Sun.COM {VNIC_IOC, "vnic", NULL, 0} 1003*7408SSebastien.Roy@Sun.COM }; 1004*7408SSebastien.Roy@Sun.COM #define DLDIOC_CNT \ 1005*7408SSebastien.Roy@Sun.COM (sizeof (dld_ioc_modtable) / sizeof (dld_ioc_modentry_t)) 1006*7408SSebastien.Roy@Sun.COM 1007*7408SSebastien.Roy@Sun.COM static dld_ioc_modentry_t * 1008*7408SSebastien.Roy@Sun.COM dld_ioc_findmod(uint16_t modid) 1009*7408SSebastien.Roy@Sun.COM { 1010*7408SSebastien.Roy@Sun.COM int i; 1011*7408SSebastien.Roy@Sun.COM 1012*7408SSebastien.Roy@Sun.COM for (i = 0; i < DLDIOC_CNT; i++) { 1013*7408SSebastien.Roy@Sun.COM if (modid == dld_ioc_modtable[i].dim_modid) 1014*7408SSebastien.Roy@Sun.COM return (&dld_ioc_modtable[i]); 1015*7408SSebastien.Roy@Sun.COM } 1016*7408SSebastien.Roy@Sun.COM return (NULL); 1017*7408SSebastien.Roy@Sun.COM } 1018*7408SSebastien.Roy@Sun.COM 1019*7408SSebastien.Roy@Sun.COM int 1020*7408SSebastien.Roy@Sun.COM dld_ioc_register(uint16_t modid, dld_ioc_info_t *list, uint_t count) 1021*7408SSebastien.Roy@Sun.COM { 1022*7408SSebastien.Roy@Sun.COM dld_ioc_modentry_t *dim = dld_ioc_findmod(modid); 1023*7408SSebastien.Roy@Sun.COM 1024*7408SSebastien.Roy@Sun.COM if (dim == NULL) 1025*7408SSebastien.Roy@Sun.COM return (ENOENT); 1026*7408SSebastien.Roy@Sun.COM 1027*7408SSebastien.Roy@Sun.COM dim->dim_list = list; 1028*7408SSebastien.Roy@Sun.COM dim->dim_count = count; 1029*7408SSebastien.Roy@Sun.COM return (0); 1030*7408SSebastien.Roy@Sun.COM } 1031*7408SSebastien.Roy@Sun.COM 1032*7408SSebastien.Roy@Sun.COM void 1033*7408SSebastien.Roy@Sun.COM dld_ioc_unregister(uint16_t modid) 1034*7408SSebastien.Roy@Sun.COM { 1035*7408SSebastien.Roy@Sun.COM VERIFY(dld_ioc_register(modid, NULL, 0) == 0); 1036*7408SSebastien.Roy@Sun.COM } 10373147Sxc151355 1038*7408SSebastien.Roy@Sun.COM /* 1039*7408SSebastien.Roy@Sun.COM * The general design with GLDv3 ioctls is that all ioctls issued 1040*7408SSebastien.Roy@Sun.COM * through /dev/dld go through this drv_ioctl() function. This 1041*7408SSebastien.Roy@Sun.COM * function handles all ioctls on behalf of modules listed in 1042*7408SSebastien.Roy@Sun.COM * dld_ioc_modtable. 1043*7408SSebastien.Roy@Sun.COM * 1044*7408SSebastien.Roy@Sun.COM * When an ioctl is received, this function looks for the associated 1045*7408SSebastien.Roy@Sun.COM * module-id-specific ioctl information using dld_ioc_findmod(). The 1046*7408SSebastien.Roy@Sun.COM * call to ddi_hold_devi_by_instance() on the associated device will 1047*7408SSebastien.Roy@Sun.COM * cause the kernel module responsible for the ioctl to be loaded if 1048*7408SSebastien.Roy@Sun.COM * it's not already loaded, which should result in that module calling 1049*7408SSebastien.Roy@Sun.COM * dld_ioc_register(), thereby filling in the dim_list containing the 1050*7408SSebastien.Roy@Sun.COM * details for the ioctl being processed. 1051*7408SSebastien.Roy@Sun.COM * 1052*7408SSebastien.Roy@Sun.COM * This function can then perform operations such as copyin() data and 1053*7408SSebastien.Roy@Sun.COM * do credential checks based on the registered ioctl information, 1054*7408SSebastien.Roy@Sun.COM * then issue the callback function di_func() registered by the 1055*7408SSebastien.Roy@Sun.COM * responsible module. Upon return, the appropriate copyout() 1056*7408SSebastien.Roy@Sun.COM * operation can be performed and the operation completes. 1057*7408SSebastien.Roy@Sun.COM */ 1058*7408SSebastien.Roy@Sun.COM /* ARGSUSED */ 1059*7408SSebastien.Roy@Sun.COM static int 1060*7408SSebastien.Roy@Sun.COM drv_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cred, int *rvalp) 1061*7408SSebastien.Roy@Sun.COM { 1062*7408SSebastien.Roy@Sun.COM dld_ioc_modentry_t *dim; 1063*7408SSebastien.Roy@Sun.COM dld_ioc_info_t *info; 1064*7408SSebastien.Roy@Sun.COM dev_info_t *dip = NULL; 1065*7408SSebastien.Roy@Sun.COM void *buf = NULL; 1066*7408SSebastien.Roy@Sun.COM size_t sz; 1067*7408SSebastien.Roy@Sun.COM int i, err; 1068*7408SSebastien.Roy@Sun.COM 1069*7408SSebastien.Roy@Sun.COM if ((dim = dld_ioc_findmod(DLD_IOC_MODID(cmd))) == NULL) 1070*7408SSebastien.Roy@Sun.COM return (ENOTSUP); 1071*7408SSebastien.Roy@Sun.COM 1072*7408SSebastien.Roy@Sun.COM dip = ddi_hold_devi_by_instance(ddi_name_to_major(dim->dim_modname), 1073*7408SSebastien.Roy@Sun.COM 0, 0); 1074*7408SSebastien.Roy@Sun.COM if (dip == NULL || dim->dim_list == NULL) { 1075*7408SSebastien.Roy@Sun.COM err = ENODEV; 1076*7408SSebastien.Roy@Sun.COM goto done; 1077*7408SSebastien.Roy@Sun.COM } 1078*7408SSebastien.Roy@Sun.COM 1079*7408SSebastien.Roy@Sun.COM for (i = 0; i < dim->dim_count; i++) { 1080*7408SSebastien.Roy@Sun.COM if (cmd == dim->dim_list[i].di_cmd) 1081*7408SSebastien.Roy@Sun.COM break; 1082*7408SSebastien.Roy@Sun.COM } 1083*7408SSebastien.Roy@Sun.COM if (i == dim->dim_count) { 1084*7408SSebastien.Roy@Sun.COM err = ENOTSUP; 1085*7408SSebastien.Roy@Sun.COM goto done; 1086*7408SSebastien.Roy@Sun.COM } 1087*7408SSebastien.Roy@Sun.COM 1088*7408SSebastien.Roy@Sun.COM info = &dim->dim_list[i]; 1089*7408SSebastien.Roy@Sun.COM 1090*7408SSebastien.Roy@Sun.COM if ((info->di_flags & DLDDLCONFIG) && secpolicy_dl_config(cred) != 0) { 1091*7408SSebastien.Roy@Sun.COM err = EPERM; 1092*7408SSebastien.Roy@Sun.COM goto done; 1093*7408SSebastien.Roy@Sun.COM } 1094*7408SSebastien.Roy@Sun.COM 1095*7408SSebastien.Roy@Sun.COM sz = info->di_argsize; 1096*7408SSebastien.Roy@Sun.COM if ((buf = kmem_zalloc(sz, KM_NOSLEEP)) == NULL) { 1097*7408SSebastien.Roy@Sun.COM err = ENOMEM; 1098*7408SSebastien.Roy@Sun.COM goto done; 1099*7408SSebastien.Roy@Sun.COM } 1100*7408SSebastien.Roy@Sun.COM 1101*7408SSebastien.Roy@Sun.COM if ((info->di_flags & DLDCOPYIN) && 1102*7408SSebastien.Roy@Sun.COM ddi_copyin((void *)arg, buf, sz, mode) != 0) { 1103*7408SSebastien.Roy@Sun.COM err = EFAULT; 1104*7408SSebastien.Roy@Sun.COM goto done; 1105*7408SSebastien.Roy@Sun.COM } 1106*7408SSebastien.Roy@Sun.COM 1107*7408SSebastien.Roy@Sun.COM err = info->di_func(buf, arg, mode, cred); 1108*7408SSebastien.Roy@Sun.COM 1109*7408SSebastien.Roy@Sun.COM if ((info->di_flags & DLDCOPYOUT) && 1110*7408SSebastien.Roy@Sun.COM ddi_copyout(buf, (void *)arg, sz, mode) != 0 && err == 0) 1111*7408SSebastien.Roy@Sun.COM err = EFAULT; 1112*7408SSebastien.Roy@Sun.COM 1113*7408SSebastien.Roy@Sun.COM done: 1114*7408SSebastien.Roy@Sun.COM if (buf != NULL) 1115*7408SSebastien.Roy@Sun.COM kmem_free(buf, sz); 1116*7408SSebastien.Roy@Sun.COM if (dip != NULL) 1117*7408SSebastien.Roy@Sun.COM ddi_release_devi(dip); 1118*7408SSebastien.Roy@Sun.COM return (err); 11193147Sxc151355 } 1120