xref: /illumos-gate/usr/src/uts/common/io/dld/dld_drv.c (revision 82a2fc4751cef28c0bdc327d02012bf8796083b9)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ba2e4443Sseb  * Common Development and Distribution License (the "License").
6ba2e4443Sseb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22e75f0919SSebastien Roy  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Data-Link Driver
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include	<sys/conf.h>
31210db224Sericheng #include	<sys/mkdev.h>
327c478bd9Sstevel@tonic-gate #include	<sys/modctl.h>
33210db224Sericheng #include	<sys/stat.h>
347c478bd9Sstevel@tonic-gate #include	<sys/dld_impl.h>
35*82a2fc47SJames Carlson #include	<sys/dld_ioc.h>
36210db224Sericheng #include	<sys/dls_impl.h>
37d62bc4baSyz #include	<sys/softmac.h>
38da14cebeSEric Cheng #include	<sys/mac.h>
39da14cebeSEric Cheng #include	<sys/mac_ether.h>
40da14cebeSEric Cheng #include	<sys/mac_client.h>
41da14cebeSEric Cheng #include	<sys/mac_client_impl.h>
42da14cebeSEric Cheng #include	<sys/mac_client_priv.h>
43210db224Sericheng #include	<inet/common.h>
44da14cebeSEric Cheng #include	<sys/policy.h>
45da14cebeSEric Cheng #include	<sys/priv_names.h>
46210db224Sericheng 
477c478bd9Sstevel@tonic-gate static void	drv_init(void);
487c478bd9Sstevel@tonic-gate static int	drv_fini(void);
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static int	drv_getinfo(dev_info_t	*, ddi_info_cmd_t, void *, void **);
517c478bd9Sstevel@tonic-gate static int	drv_attach(dev_info_t *, ddi_attach_cmd_t);
527c478bd9Sstevel@tonic-gate static int	drv_detach(dev_info_t *, ddi_detach_cmd_t);
537c478bd9Sstevel@tonic-gate 
540ba2cbe9Sxc /*
550ba2cbe9Sxc  * Secure objects declarations
560ba2cbe9Sxc  */
570ba2cbe9Sxc #define	SECOBJ_WEP_HASHSZ	67
580ba2cbe9Sxc static krwlock_t	drv_secobj_lock;
590ba2cbe9Sxc static kmem_cache_t	*drv_secobj_cachep;
600ba2cbe9Sxc static mod_hash_t	*drv_secobj_hash;
610ba2cbe9Sxc static void		drv_secobj_init(void);
620ba2cbe9Sxc static void		drv_secobj_fini(void);
633bc21d0aSAruna Ramakrishna - Sun Microsystems static int		drv_ioc_setap(datalink_id_t, struct dlautopush *);
643bc21d0aSAruna Ramakrishna - Sun Microsystems static int		drv_ioc_getap(datalink_id_t, struct dlautopush *);
653bc21d0aSAruna Ramakrishna - Sun Microsystems static int		drv_ioc_clrap(datalink_id_t);
660ba2cbe9Sxc 
67eae72b5bSSebastien Roy 
68210db224Sericheng /*
69210db224Sericheng  * The following entry points are private to dld and are used for control
70210db224Sericheng  * operations only. The entry points exported to mac drivers are defined
71210db224Sericheng  * in dld_str.c. Refer to the comment on top of dld_str.c for details.
72210db224Sericheng  */
73eae72b5bSSebastien Roy static int	drv_open(dev_t *, int, int, cred_t *);
74eae72b5bSSebastien Roy static int	drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
757c478bd9Sstevel@tonic-gate 
76eae72b5bSSebastien Roy static dev_info_t	*dld_dip;	/* dev_info_t for the driver */
77eae72b5bSSebastien Roy uint32_t		dld_opt = 0;	/* Global options */
787c478bd9Sstevel@tonic-gate 
79d62bc4baSyz #define	NAUTOPUSH 32
80d62bc4baSyz static mod_hash_t *dld_ap_hashp;
81d62bc4baSyz static krwlock_t dld_ap_hash_lock;
82d62bc4baSyz 
83eae72b5bSSebastien Roy static struct cb_ops drv_cb_ops = {
84eae72b5bSSebastien Roy 	drv_open,		/* open */
85eae72b5bSSebastien Roy 	nulldev,		/* close */
86eae72b5bSSebastien Roy 	nulldev,		/* strategy */
87eae72b5bSSebastien Roy 	nulldev,		/* print */
88eae72b5bSSebastien Roy 	nodev,			/* dump */
89eae72b5bSSebastien Roy 	nodev,			/* read */
90eae72b5bSSebastien Roy 	nodev,			/* write */
91eae72b5bSSebastien Roy 	drv_ioctl,		/* ioctl */
92eae72b5bSSebastien Roy 	nodev,			/* devmap */
93eae72b5bSSebastien Roy 	nodev,			/* mmap */
94eae72b5bSSebastien Roy 	nodev,			/* segmap */
95eae72b5bSSebastien Roy 	nochpoll,		/* poll */
96eae72b5bSSebastien Roy 	ddi_prop_op,		/* cb_prop_op */
97eae72b5bSSebastien Roy 	0,			/* streamtab  */
98eae72b5bSSebastien Roy 	D_MP			/* Driver compatibility flag */
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate 
101eae72b5bSSebastien Roy static struct dev_ops drv_ops = {
102eae72b5bSSebastien Roy 	DEVO_REV,		/* devo_rev */
103eae72b5bSSebastien Roy 	0,			/* refcnt */
104eae72b5bSSebastien Roy 	drv_getinfo,		/* get_dev_info */
105eae72b5bSSebastien Roy 	nulldev,		/* identify */
106eae72b5bSSebastien Roy 	nulldev,		/* probe */
107eae72b5bSSebastien Roy 	drv_attach,		/* attach */
108eae72b5bSSebastien Roy 	drv_detach,		/* detach */
109eae72b5bSSebastien Roy 	nodev,			/* reset */
110eae72b5bSSebastien Roy 	&drv_cb_ops,		/* driver operations */
111eae72b5bSSebastien Roy 	NULL,			/* bus operations */
11219397407SSherry Moore 	nodev,			/* dev power */
11319397407SSherry Moore 	ddi_quiesce_not_supported,	/* dev quiesce */
1147c478bd9Sstevel@tonic-gate };
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate static	struct modldrv		drv_modldrv = {
1207c478bd9Sstevel@tonic-gate 	&mod_driverops,
1217c478bd9Sstevel@tonic-gate 	DLD_INFO,
1227c478bd9Sstevel@tonic-gate 	&drv_ops
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static	struct modlinkage	drv_modlinkage = {
1267c478bd9Sstevel@tonic-gate 	MODREV_1,
1277c478bd9Sstevel@tonic-gate 	&drv_modldrv,
1287c478bd9Sstevel@tonic-gate 	NULL
1297c478bd9Sstevel@tonic-gate };
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate int
1327c478bd9Sstevel@tonic-gate _init(void)
1337c478bd9Sstevel@tonic-gate {
134eae72b5bSSebastien Roy 	return (mod_install(&drv_modlinkage));
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate int
1387c478bd9Sstevel@tonic-gate _fini(void)
1397c478bd9Sstevel@tonic-gate {
140eae72b5bSSebastien Roy 	return (mod_remove(&drv_modlinkage));
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate int
1447c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	return (mod_info(&drv_modlinkage, modinfop));
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /*
150210db224Sericheng  * Initialize component modules.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate static void
1537c478bd9Sstevel@tonic-gate drv_init(void)
1547c478bd9Sstevel@tonic-gate {
1550ba2cbe9Sxc 	drv_secobj_init();
1567c478bd9Sstevel@tonic-gate 	dld_str_init();
157da14cebeSEric Cheng 
158d62bc4baSyz 	/*
159d62bc4baSyz 	 * Create a hash table for autopush configuration.
160d62bc4baSyz 	 */
161d62bc4baSyz 	dld_ap_hashp = mod_hash_create_idhash("dld_autopush_hash",
162d62bc4baSyz 	    NAUTOPUSH, mod_hash_null_valdtor);
163d62bc4baSyz 
164d62bc4baSyz 	ASSERT(dld_ap_hashp != NULL);
165d62bc4baSyz 	rw_init(&dld_ap_hash_lock, NULL, RW_DRIVER, NULL);
166d62bc4baSyz }
167d62bc4baSyz 
168d62bc4baSyz /* ARGSUSED */
169d62bc4baSyz static uint_t
170d62bc4baSyz drv_ap_exist(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
171d62bc4baSyz {
172d62bc4baSyz 	boolean_t *pexist = arg;
173d62bc4baSyz 
174d62bc4baSyz 	*pexist = B_TRUE;
175d62bc4baSyz 	return (MH_WALK_TERMINATE);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static int
1797c478bd9Sstevel@tonic-gate drv_fini(void)
1807c478bd9Sstevel@tonic-gate {
181d62bc4baSyz 	int		err;
182d62bc4baSyz 	boolean_t	exist = B_FALSE;
183d62bc4baSyz 
184d62bc4baSyz 	rw_enter(&dld_ap_hash_lock, RW_READER);
185d62bc4baSyz 	mod_hash_walk(dld_ap_hashp, drv_ap_exist, &exist);
186d62bc4baSyz 	rw_exit(&dld_ap_hash_lock);
187d62bc4baSyz 	if (exist)
188d62bc4baSyz 		return (EBUSY);
1897c478bd9Sstevel@tonic-gate 
190210db224Sericheng 	if ((err = dld_str_fini()) != 0)
1917c478bd9Sstevel@tonic-gate 		return (err);
1927c478bd9Sstevel@tonic-gate 
1930ba2cbe9Sxc 	drv_secobj_fini();
194d62bc4baSyz 	mod_hash_destroy_idhash(dld_ap_hashp);
195d62bc4baSyz 	rw_destroy(&dld_ap_hash_lock);
1967c478bd9Sstevel@tonic-gate 	return (0);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate  * devo_getinfo: getinfo(9e)
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2037c478bd9Sstevel@tonic-gate static int
2047c478bd9Sstevel@tonic-gate drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resp)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	if (dld_dip == NULL)
2077c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	switch (cmd) {
2107c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
211eae72b5bSSebastien Roy 		*resp = 0;
2127c478bd9Sstevel@tonic-gate 		break;
2137c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
214eae72b5bSSebastien Roy 		*resp = dld_dip;
2157c478bd9Sstevel@tonic-gate 		break;
2167c478bd9Sstevel@tonic-gate 	default:
2177c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * Check properties to set options. (See dld.h for property definitions).
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate static void
2277c478bd9Sstevel@tonic-gate drv_set_opt(dev_info_t *dip)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2307c478bd9Sstevel@tonic-gate 	    DLD_PROP_NO_FASTPATH, 0) != 0) {
2317c478bd9Sstevel@tonic-gate 		dld_opt |= DLD_OPT_NO_FASTPATH;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2357c478bd9Sstevel@tonic-gate 	    DLD_PROP_NO_POLL, 0) != 0) {
2367c478bd9Sstevel@tonic-gate 		dld_opt |= DLD_OPT_NO_POLL;
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2407c478bd9Sstevel@tonic-gate 	    DLD_PROP_NO_ZEROCOPY, 0) != 0) {
2417c478bd9Sstevel@tonic-gate 		dld_opt |= DLD_OPT_NO_ZEROCOPY;
2427c478bd9Sstevel@tonic-gate 	}
2439c175129Sja 
2449c175129Sja 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2459c175129Sja 	    DLD_PROP_NO_SOFTRING, 0) != 0) {
2469c175129Sja 		dld_opt |= DLD_OPT_NO_SOFTRING;
2479c175129Sja 	}
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /*
2517c478bd9Sstevel@tonic-gate  * devo_attach: attach(9e)
2527c478bd9Sstevel@tonic-gate  */
2537c478bd9Sstevel@tonic-gate static int
2547c478bd9Sstevel@tonic-gate drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
2577c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	ASSERT(ddi_get_instance(dip) == 0);
260eae72b5bSSebastien Roy 	drv_init();
2617c478bd9Sstevel@tonic-gate 	drv_set_opt(dip);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * Create control node. DLPI provider nodes will be created on demand.
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, DLD_CONTROL_MINOR_NAME, S_IFCHR,
2677c478bd9Sstevel@tonic-gate 	    DLD_CONTROL_MINOR, DDI_PSEUDO, 0) != DDI_SUCCESS)
2687c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	dld_dip = dip;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/*
2737c478bd9Sstevel@tonic-gate 	 * Log the fact that the driver is now attached.
2747c478bd9Sstevel@tonic-gate 	 */
2757c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
2767c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * devo_detach: detach(9e)
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate static int
2837c478bd9Sstevel@tonic-gate drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
2867c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	ASSERT(dld_dip == dip);
289eae72b5bSSebastien Roy 	if (drv_fini() != 0)
290eae72b5bSSebastien Roy 		return (DDI_FAILURE);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/*
2937c478bd9Sstevel@tonic-gate 	 * Remove the control node.
2947c478bd9Sstevel@tonic-gate 	 */
2957c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, DLD_CONTROL_MINOR_NAME);
2967c478bd9Sstevel@tonic-gate 	dld_dip = NULL;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate /*
302210db224Sericheng  * dld control node open procedure.
3037c478bd9Sstevel@tonic-gate  */
3047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3057c478bd9Sstevel@tonic-gate static int
306eae72b5bSSebastien Roy drv_open(dev_t *devp, int flag, int sflag, cred_t *credp)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	/*
309eae72b5bSSebastien Roy 	 * Only the control node can be opened.
3107c478bd9Sstevel@tonic-gate 	 */
311eae72b5bSSebastien Roy 	if (getminor(*devp) != DLD_CONTROL_MINOR)
312eae72b5bSSebastien Roy 		return (ENODEV);
3137c478bd9Sstevel@tonic-gate 	return (0);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /*
317d62bc4baSyz  * DLDIOC_ATTR
3187c478bd9Sstevel@tonic-gate  */
319eae72b5bSSebastien Roy /* ARGSUSED */
320eae72b5bSSebastien Roy static int
321da14cebeSEric Cheng drv_ioc_attr(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
3227c478bd9Sstevel@tonic-gate {
323eae72b5bSSebastien Roy 	dld_ioc_attr_t		*diap = karg;
324d62bc4baSyz 	dls_dl_handle_t		dlh;
325da14cebeSEric Cheng 	dls_link_t		*dlp;
326d62bc4baSyz 	int			err;
327da14cebeSEric Cheng 	mac_perim_handle_t	mph;
328210db224Sericheng 
329d62bc4baSyz 	if ((err = dls_devnet_hold_tmp(diap->dia_linkid, &dlh)) != 0)
330eae72b5bSSebastien Roy 		return (err);
331210db224Sericheng 
332da14cebeSEric Cheng 	if ((err = mac_perim_enter_by_macname(
333da14cebeSEric Cheng 	    dls_devnet_mac(dlh), &mph)) != 0) {
334d62bc4baSyz 		dls_devnet_rele_tmp(dlh);
335eae72b5bSSebastien Roy 		return (err);
336d62bc4baSyz 	}
337210db224Sericheng 
338da14cebeSEric Cheng 	if ((err = dls_link_hold(dls_devnet_mac(dlh), &dlp)) != 0) {
339da14cebeSEric Cheng 		mac_perim_exit(mph);
340da14cebeSEric Cheng 		dls_devnet_rele_tmp(dlh);
341da14cebeSEric Cheng 		return (err);
342da14cebeSEric Cheng 	}
343da14cebeSEric Cheng 
344da14cebeSEric Cheng 	mac_sdu_get(dlp->dl_mh, NULL, &diap->dia_max_sdu);
345da14cebeSEric Cheng 
346da14cebeSEric Cheng 	dls_link_rele(dlp);
347da14cebeSEric Cheng 	mac_perim_exit(mph);
348d62bc4baSyz 	dls_devnet_rele_tmp(dlh);
349d62bc4baSyz 
350eae72b5bSSebastien Roy 	return (0);
351d62bc4baSyz }
352210db224Sericheng 
353d62bc4baSyz /*
354d62bc4baSyz  * DLDIOC_PHYS_ATTR
355d62bc4baSyz  */
356eae72b5bSSebastien Roy /* ARGSUSED */
357eae72b5bSSebastien Roy static int
358da14cebeSEric Cheng drv_ioc_phys_attr(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
359d62bc4baSyz {
360eae72b5bSSebastien Roy 	dld_ioc_phys_attr_t	*dipp = karg;
361d62bc4baSyz 	int			err;
362d62bc4baSyz 	dls_dl_handle_t		dlh;
363d62bc4baSyz 	dls_dev_handle_t	ddh;
364d62bc4baSyz 	dev_t			phydev;
365d62bc4baSyz 
366d62bc4baSyz 	/*
367d62bc4baSyz 	 * Every physical link should have its physical dev_t kept in the
368d62bc4baSyz 	 * daemon. If not, it is not a valid physical link.
369d62bc4baSyz 	 */
370eae72b5bSSebastien Roy 	if (dls_mgmt_get_phydev(dipp->dip_linkid, &phydev) != 0)
371eae72b5bSSebastien Roy 		return (EINVAL);
372d62bc4baSyz 
373d62bc4baSyz 	/*
374d62bc4baSyz 	 * Although this is a valid physical link, it might already be removed
375d62bc4baSyz 	 * by DR or during system shutdown. softmac_hold_device() would return
376d62bc4baSyz 	 * ENOENT in this case.
377d62bc4baSyz 	 */
378d62bc4baSyz 	if ((err = softmac_hold_device(phydev, &ddh)) != 0)
379eae72b5bSSebastien Roy 		return (err);
380d62bc4baSyz 
381d62bc4baSyz 	if (dls_devnet_hold_tmp(dipp->dip_linkid, &dlh) != 0) {
382210db224Sericheng 		/*
383d62bc4baSyz 		 * Although this is an active physical link, its link type is
384d62bc4baSyz 		 * not supported by GLDv3, and therefore it does not have
385d62bc4baSyz 		 * vanity naming support.
386210db224Sericheng 		 */
387d62bc4baSyz 		dipp->dip_novanity = B_TRUE;
388d62bc4baSyz 	} else {
389d62bc4baSyz 		dipp->dip_novanity = B_FALSE;
390d62bc4baSyz 		dls_devnet_rele_tmp(dlh);
391210db224Sericheng 	}
392d62bc4baSyz 	/*
393d62bc4baSyz 	 * Get the physical device name from the major number and the instance
394d62bc4baSyz 	 * number derived from phydev.
395d62bc4baSyz 	 */
396d62bc4baSyz 	(void) snprintf(dipp->dip_dev, MAXLINKNAMELEN, "%s%d",
397d62bc4baSyz 	    ddi_major_to_name(getmajor(phydev)), getminor(phydev) - 1);
398d62bc4baSyz 
399d62bc4baSyz 	softmac_rele_device(ddh);
400eae72b5bSSebastien Roy 	return (0);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
403da14cebeSEric Cheng /* ARGSUSED */
404da14cebeSEric Cheng static int
405da14cebeSEric Cheng drv_ioc_hwgrpget(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
406da14cebeSEric Cheng {
407da14cebeSEric Cheng 	dld_ioc_hwgrpget_t	*hwgrpp = karg;
408da14cebeSEric Cheng 	dld_hwgrpinfo_t		hwgrp, *hip;
409da14cebeSEric Cheng 	mac_handle_t		mh = NULL;
410da14cebeSEric Cheng 	int			i, err, grpnum;
411da14cebeSEric Cheng 	uint_t			bytes_left;
412da14cebeSEric Cheng 
413da14cebeSEric Cheng 	hwgrpp->dih_n_groups = 0;
414da14cebeSEric Cheng 	err = mac_open_by_linkid(hwgrpp->dih_linkid, &mh);
415da14cebeSEric Cheng 	if (err != 0)
416da14cebeSEric Cheng 		goto done;
417da14cebeSEric Cheng 
418da14cebeSEric Cheng 	hip = (dld_hwgrpinfo_t *)
419da14cebeSEric Cheng 	    ((uchar_t *)arg + sizeof (dld_ioc_hwgrpget_t));
420da14cebeSEric Cheng 	bytes_left = hwgrpp->dih_size;
421da14cebeSEric Cheng 	grpnum = mac_hwgrp_num(mh);
422da14cebeSEric Cheng 	for (i = 0; i < grpnum; i++) {
423da14cebeSEric Cheng 		if (sizeof (dld_hwgrpinfo_t) > bytes_left) {
424da14cebeSEric Cheng 			err = ENOSPC;
425da14cebeSEric Cheng 			goto done;
426da14cebeSEric Cheng 		}
427da14cebeSEric Cheng 
428da14cebeSEric Cheng 		bzero(&hwgrp, sizeof (hwgrp));
429da14cebeSEric Cheng 		bcopy(mac_name(mh), hwgrp.dhi_link_name,
430da14cebeSEric Cheng 		    sizeof (hwgrp.dhi_link_name));
431da14cebeSEric Cheng 		mac_get_hwgrp_info(mh, i, &hwgrp.dhi_grp_num,
432da14cebeSEric Cheng 		    &hwgrp.dhi_n_rings, &hwgrp.dhi_grp_type,
433da14cebeSEric Cheng 		    &hwgrp.dhi_n_clnts, hwgrp.dhi_clnts);
434da14cebeSEric Cheng 		if (copyout(&hwgrp, hip, sizeof (hwgrp)) != 0) {
435da14cebeSEric Cheng 			err = EFAULT;
436da14cebeSEric Cheng 			goto done;
437da14cebeSEric Cheng 		}
438da14cebeSEric Cheng 
439da14cebeSEric Cheng 		hip++;
440da14cebeSEric Cheng 		bytes_left -= sizeof (dld_hwgrpinfo_t);
441da14cebeSEric Cheng 	}
442da14cebeSEric Cheng 
443da14cebeSEric Cheng done:
444da14cebeSEric Cheng 	if (mh != NULL)
445da14cebeSEric Cheng 		dld_mac_close(mh);
446da14cebeSEric Cheng 	if (err == 0)
447da14cebeSEric Cheng 		hwgrpp->dih_n_groups = grpnum;
448da14cebeSEric Cheng 	return (err);
449da14cebeSEric Cheng }
450da14cebeSEric Cheng 
451da14cebeSEric Cheng /* ARGSUSED */
452da14cebeSEric Cheng static int
453da14cebeSEric Cheng drv_ioc_macaddrget(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
454da14cebeSEric Cheng {
455da14cebeSEric Cheng 	dld_ioc_macaddrget_t	*magp = karg;
456da14cebeSEric Cheng 	dld_macaddrinfo_t	mai, *maip;
457da14cebeSEric Cheng 	mac_handle_t		mh = NULL;
458da14cebeSEric Cheng 	int			i, err;
459da14cebeSEric Cheng 	uint_t			bytes_left;
460da14cebeSEric Cheng 	boolean_t		is_used;
461da14cebeSEric Cheng 
462da14cebeSEric Cheng 	magp->dig_count = 0;
463da14cebeSEric Cheng 	err = mac_open_by_linkid(magp->dig_linkid, &mh);
464da14cebeSEric Cheng 	if (err != 0)
465da14cebeSEric Cheng 		goto done;
466da14cebeSEric Cheng 
467da14cebeSEric Cheng 	maip = (dld_macaddrinfo_t *)
468da14cebeSEric Cheng 	    ((uchar_t *)arg + sizeof (dld_ioc_macaddrget_t));
469da14cebeSEric Cheng 	bytes_left = magp->dig_size;
470da14cebeSEric Cheng 
471da14cebeSEric Cheng 	for (i = 0; i < mac_addr_factory_num(mh) + 1; i++) {
472da14cebeSEric Cheng 		if (sizeof (dld_macaddrinfo_t) > bytes_left) {
473da14cebeSEric Cheng 			err = ENOSPC;
474da14cebeSEric Cheng 			goto done;
475da14cebeSEric Cheng 		}
476da14cebeSEric Cheng 
477da14cebeSEric Cheng 		bzero(&mai, sizeof (mai));
478da14cebeSEric Cheng 
479da14cebeSEric Cheng 		if (i == 0) {
480da14cebeSEric Cheng 			/* primary MAC address */
481da14cebeSEric Cheng 			mac_unicast_primary_get(mh, mai.dmi_addr);
482da14cebeSEric Cheng 			mai.dmi_addrlen = mac_addr_len(mh);
483da14cebeSEric Cheng 			mac_unicast_primary_info(mh, mai.dmi_client_name,
484da14cebeSEric Cheng 			    &is_used);
485da14cebeSEric Cheng 		} else {
486da14cebeSEric Cheng 			/* factory MAC address slot */
487da14cebeSEric Cheng 			mac_addr_factory_value(mh, i, mai.dmi_addr,
488da14cebeSEric Cheng 			    &mai.dmi_addrlen, mai.dmi_client_name, &is_used);
489da14cebeSEric Cheng 		}
490da14cebeSEric Cheng 
491da14cebeSEric Cheng 		mai.dmi_slot = i;
492da14cebeSEric Cheng 		if (is_used)
493da14cebeSEric Cheng 			mai.dmi_flags |= DLDIOCMACADDR_USED;
494da14cebeSEric Cheng 
495da14cebeSEric Cheng 		if (copyout(&mai, maip, sizeof (mai)) != 0) {
496da14cebeSEric Cheng 			err = EFAULT;
497da14cebeSEric Cheng 			goto done;
498da14cebeSEric Cheng 		}
499da14cebeSEric Cheng 
500da14cebeSEric Cheng 		maip++;
501da14cebeSEric Cheng 		bytes_left -= sizeof (dld_macaddrinfo_t);
502da14cebeSEric Cheng 	}
503da14cebeSEric Cheng 
504da14cebeSEric Cheng done:
505da14cebeSEric Cheng 	if (mh != NULL)
506da14cebeSEric Cheng 		dld_mac_close(mh);
507da14cebeSEric Cheng 	if (err == 0)
508da14cebeSEric Cheng 		magp->dig_count = mac_addr_factory_num(mh) + 1;
509da14cebeSEric Cheng 	return (err);
510da14cebeSEric Cheng }
511da14cebeSEric Cheng 
512e7801d59Ssowmini /*
513da14cebeSEric Cheng  * DLDIOC_SET/GETPROP
514e7801d59Ssowmini  */
515eae72b5bSSebastien Roy static int
516da14cebeSEric Cheng drv_ioc_prop_common(dld_ioc_macprop_t *prop, intptr_t arg, boolean_t set,
517eae72b5bSSebastien Roy     int mode)
518e7801d59Ssowmini {
519da14cebeSEric Cheng 	int			err = EINVAL;
520da14cebeSEric Cheng 	dls_dl_handle_t 	dlh = NULL;
521da14cebeSEric Cheng 	dls_link_t		*dlp = NULL;
522da14cebeSEric Cheng 	mac_perim_handle_t	mph = NULL;
523e7801d59Ssowmini 	mac_prop_t		macprop;
524da14cebeSEric Cheng 	dld_ioc_macprop_t	*kprop;
525da14cebeSEric Cheng 	datalink_id_t		linkid;
526da14cebeSEric Cheng 	uint_t			dsize;
527da14cebeSEric Cheng 
528e7801d59Ssowmini 
529eae72b5bSSebastien Roy 	/*
530da14cebeSEric Cheng 	 * We only use pr_valsize from prop, as the caller only did a
531eae72b5bSSebastien Roy 	 * copyin() for sizeof (dld_ioc_prop_t), which doesn't cover
532eae72b5bSSebastien Roy 	 * the property data.  We copyin the full dld_ioc_prop_t
533da14cebeSEric Cheng 	 * including the data into kprop down below.
534eae72b5bSSebastien Roy 	 */
535da14cebeSEric Cheng 	dsize = sizeof (dld_ioc_macprop_t) + prop->pr_valsize - 1;
536da14cebeSEric Cheng 	if (dsize < prop->pr_valsize)
537eae72b5bSSebastien Roy 		return (EINVAL);
538eae72b5bSSebastien Roy 
539eae72b5bSSebastien Roy 	/*
540eae72b5bSSebastien Roy 	 * The property data is variable size, so we need to allocate
541eae72b5bSSebastien Roy 	 * a buffer for kernel use as this data was not part of the
542da14cebeSEric Cheng 	 * prop allocation and copyin() done by the framework.
543eae72b5bSSebastien Roy 	 */
544da14cebeSEric Cheng 	if ((kprop = kmem_alloc(dsize, KM_NOSLEEP)) == NULL)
545eae72b5bSSebastien Roy 		return (ENOMEM);
546da14cebeSEric Cheng 
547da14cebeSEric Cheng 	if (ddi_copyin((void *)arg, kprop, dsize, mode) != 0) {
548eae72b5bSSebastien Roy 		err = EFAULT;
549e7801d59Ssowmini 		goto done;
550eae72b5bSSebastien Roy 	}
551e7801d59Ssowmini 
552da14cebeSEric Cheng 	linkid = kprop->pr_linkid;
553da14cebeSEric Cheng 	if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0)
554da14cebeSEric Cheng 		goto done;
555da14cebeSEric Cheng 
556da14cebeSEric Cheng 	if ((err = mac_perim_enter_by_macname(dls_devnet_mac(dlh),
557da14cebeSEric Cheng 	    &mph)) != 0) {
558da14cebeSEric Cheng 		goto done;
559da14cebeSEric Cheng 	}
560e7801d59Ssowmini 
561e75f0919SSebastien Roy 	if ((err = dls_link_hold(dls_devnet_mac(dlh), &dlp)) != 0)
562e75f0919SSebastien Roy 		goto done;
563e75f0919SSebastien Roy 
564da14cebeSEric Cheng 	switch (kprop->pr_num) {
565e75f0919SSebastien Roy 	case MAC_PROP_ZONE:
5663bc21d0aSAruna Ramakrishna - Sun Microsystems 		if (set) {
567e75f0919SSebastien Roy 			dld_ioc_zid_t *dzp = (dld_ioc_zid_t *)kprop->pr_val;
568da14cebeSEric Cheng 
5693bc21d0aSAruna Ramakrishna - Sun Microsystems 			err = dls_devnet_setzid(dzp->diz_link, dzp->diz_zid);
5703bc21d0aSAruna Ramakrishna - Sun Microsystems 		} else {
571da14cebeSEric Cheng 			kprop->pr_perm_flags = MAC_PROP_PERM_RW;
572da14cebeSEric Cheng 			err = dls_devnet_getzid(linkid,
573da14cebeSEric Cheng 			    (zoneid_t *)kprop->pr_val);
5743bc21d0aSAruna Ramakrishna - Sun Microsystems 		}
575e75f0919SSebastien Roy 		break;
576da14cebeSEric Cheng 	case MAC_PROP_AUTOPUSH: {
577e75f0919SSebastien Roy 		struct dlautopush *dlap = (struct dlautopush *)kprop->pr_val;
578da14cebeSEric Cheng 
5793bc21d0aSAruna Ramakrishna - Sun Microsystems 		if (set) {
580e75f0919SSebastien Roy 			if (kprop->pr_valsize != 0)
5813bc21d0aSAruna Ramakrishna - Sun Microsystems 				err = drv_ioc_setap(linkid, dlap);
582e75f0919SSebastien Roy 			else
5833bc21d0aSAruna Ramakrishna - Sun Microsystems 				err = drv_ioc_clrap(linkid);
5843bc21d0aSAruna Ramakrishna - Sun Microsystems 		} else {
585da14cebeSEric Cheng 			kprop->pr_perm_flags = MAC_PROP_PERM_RW;
5863bc21d0aSAruna Ramakrishna - Sun Microsystems 			err = drv_ioc_getap(linkid, dlap);
5873bc21d0aSAruna Ramakrishna - Sun Microsystems 		}
5883bc21d0aSAruna Ramakrishna - Sun Microsystems 		break;
5893bc21d0aSAruna Ramakrishna - Sun Microsystems 	}
590e75f0919SSebastien Roy 	case MAC_PROP_TAGMODE:
591e75f0919SSebastien Roy 		if (set) {
592e75f0919SSebastien Roy 			link_tagmode_t mode = *(link_tagmode_t *)kprop->pr_val;
5933bc21d0aSAruna Ramakrishna - Sun Microsystems 
594e75f0919SSebastien Roy 			if (mode != LINK_TAGMODE_VLANONLY &&
595e75f0919SSebastien Roy 			    mode != LINK_TAGMODE_NORMAL) {
596e75f0919SSebastien Roy 				err = EINVAL;
597e75f0919SSebastien Roy 			} else {
598e75f0919SSebastien Roy 				dlp->dl_tagmode = mode;
599e75f0919SSebastien Roy 				err = 0;
600e75f0919SSebastien Roy 			}
601e75f0919SSebastien Roy 		} else {
602e75f0919SSebastien Roy 			*(link_tagmode_t *)kprop->pr_val = dlp->dl_tagmode;
603e75f0919SSebastien Roy 			kprop->pr_perm_flags = MAC_PROP_PERM_RW;
604e75f0919SSebastien Roy 			err = 0;
605e75f0919SSebastien Roy 		}
606e75f0919SSebastien Roy 		break;
607e75f0919SSebastien Roy 	default:
608e75f0919SSebastien Roy 		macprop.mp_name = kprop->pr_name;
609e75f0919SSebastien Roy 		macprop.mp_id = kprop->pr_num;
610e75f0919SSebastien Roy 		macprop.mp_flags = kprop->pr_flags;
611e7801d59Ssowmini 
612e75f0919SSebastien Roy 		if (set) {
613e75f0919SSebastien Roy 			err = mac_set_prop(dlp->dl_mh, &macprop, kprop->pr_val,
614e75f0919SSebastien Roy 			    kprop->pr_valsize);
615e75f0919SSebastien Roy 		} else {
616e75f0919SSebastien Roy 			kprop->pr_perm_flags = MAC_PROP_PERM_RW;
617e75f0919SSebastien Roy 			err = mac_get_prop(dlp->dl_mh, &macprop, kprop->pr_val,
618e75f0919SSebastien Roy 			    kprop->pr_valsize, &kprop->pr_perm_flags);
619e75f0919SSebastien Roy 		}
620eae72b5bSSebastien Roy 	}
621e7801d59Ssowmini 
622e7801d59Ssowmini done:
623eae72b5bSSebastien Roy 	if (!set && err == 0 &&
624da14cebeSEric Cheng 	    ddi_copyout(kprop, (void *)arg, dsize, mode) != 0)
625eae72b5bSSebastien Roy 		err = EFAULT;
626e7801d59Ssowmini 
627da14cebeSEric Cheng 	if (dlp != NULL)
628da14cebeSEric Cheng 		dls_link_rele(dlp);
629e7801d59Ssowmini 
630da14cebeSEric Cheng 	if (mph != NULL) {
631da14cebeSEric Cheng 		int32_t	cpuid;
632da14cebeSEric Cheng 		void	*mdip = NULL;
633e7801d59Ssowmini 
634da14cebeSEric Cheng 		if (dlp != NULL && set && err == 0) {
635da14cebeSEric Cheng 			cpuid = mac_client_intr_cpu(dlp->dl_mch);
636da14cebeSEric Cheng 			mdip = mac_get_devinfo(dlp->dl_mh);
637da14cebeSEric Cheng 		}
638d62bc4baSyz 
639da14cebeSEric Cheng 		mac_perim_exit(mph);
640da14cebeSEric Cheng 
641da14cebeSEric Cheng 		if (mdip != NULL)
642da14cebeSEric Cheng 			mac_client_set_intr_cpu(mdip, dlp->dl_mch, cpuid);
643da14cebeSEric Cheng 	}
644da14cebeSEric Cheng 	if (dlh != NULL)
645da14cebeSEric Cheng 		dls_devnet_rele_tmp(dlh);
646da14cebeSEric Cheng 
647da14cebeSEric Cheng 	if (kprop != NULL)
648da14cebeSEric Cheng 		kmem_free(kprop, dsize);
649da14cebeSEric Cheng 	return (err);
650210db224Sericheng }
651210db224Sericheng 
652eae72b5bSSebastien Roy /* ARGSUSED */
653eae72b5bSSebastien Roy static int
654da14cebeSEric Cheng drv_ioc_setprop(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
6557c478bd9Sstevel@tonic-gate {
656da14cebeSEric Cheng 	return (drv_ioc_prop_common(karg, arg, B_TRUE, mode));
657d62bc4baSyz }
658d62bc4baSyz 
659eae72b5bSSebastien Roy /* ARGSUSED */
660eae72b5bSSebastien Roy static int
661da14cebeSEric Cheng drv_ioc_getprop(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
662d62bc4baSyz {
663da14cebeSEric Cheng 	return (drv_ioc_prop_common(karg, arg, B_FALSE, mode));
664210db224Sericheng }
665210db224Sericheng 
666f4b3ec61Sdh /*
667d62bc4baSyz  * DLDIOC_RENAME.
668d62bc4baSyz  *
669d62bc4baSyz  * This function handles two cases of link renaming. See more in comments above
670d62bc4baSyz  * dls_datalink_rename().
671f4b3ec61Sdh  */
672eae72b5bSSebastien Roy /* ARGSUSED */
673eae72b5bSSebastien Roy static int
674da14cebeSEric Cheng drv_ioc_rename(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
675f4b3ec61Sdh {
676eae72b5bSSebastien Roy 	dld_ioc_rename_t	*dir = karg;
677d62bc4baSyz 	mod_hash_key_t		key;
678d62bc4baSyz 	mod_hash_val_t		val;
679d62bc4baSyz 	int			err;
680d62bc4baSyz 
681d62bc4baSyz 	if ((err = dls_devnet_rename(dir->dir_linkid1, dir->dir_linkid2,
682eae72b5bSSebastien Roy 	    dir->dir_link)) != 0)
683eae72b5bSSebastien Roy 		return (err);
684d62bc4baSyz 
685d62bc4baSyz 	if (dir->dir_linkid2 == DATALINK_INVALID_LINKID)
686eae72b5bSSebastien Roy 		return (0);
687d62bc4baSyz 
688d62bc4baSyz 	/*
689d62bc4baSyz 	 * if dir_linkid2 is not DATALINK_INVALID_LINKID, it means this
690d62bc4baSyz 	 * renaming request is to rename a valid physical link (dir_linkid1)
691d62bc4baSyz 	 * to a "removed" physical link (dir_linkid2, which is removed by DR
692d62bc4baSyz 	 * or during system shutdown). In this case, the link (specified by
693d62bc4baSyz 	 * dir_linkid1) would inherit all the configuration of dir_linkid2,
694d62bc4baSyz 	 * and dir_linkid1 and its configuration would be lost.
695d62bc4baSyz 	 *
696d62bc4baSyz 	 * Remove per-link autopush configuration of dir_linkid1 in this case.
697d62bc4baSyz 	 */
698d62bc4baSyz 	key = (mod_hash_key_t)(uintptr_t)dir->dir_linkid1;
699d62bc4baSyz 	rw_enter(&dld_ap_hash_lock, RW_WRITER);
700d62bc4baSyz 	if (mod_hash_find(dld_ap_hashp, key, &val) != 0) {
701d62bc4baSyz 		rw_exit(&dld_ap_hash_lock);
702eae72b5bSSebastien Roy 		return (0);
703d62bc4baSyz 	}
704d62bc4baSyz 
705d62bc4baSyz 	VERIFY(mod_hash_remove(dld_ap_hashp, key, &val) == 0);
706d62bc4baSyz 	kmem_free(val, sizeof (dld_ap_t));
707d62bc4baSyz 	rw_exit(&dld_ap_hash_lock);
708eae72b5bSSebastien Roy 	return (0);
709d62bc4baSyz }
710d62bc4baSyz 
7113bc21d0aSAruna Ramakrishna - Sun Microsystems static int
7123bc21d0aSAruna Ramakrishna - Sun Microsystems drv_ioc_setap(datalink_id_t linkid, struct dlautopush *dlap)
713d62bc4baSyz {
714d62bc4baSyz 	dld_ap_t	*dap;
715eae72b5bSSebastien Roy 	int		i;
716d62bc4baSyz 	mod_hash_key_t	key;
717d62bc4baSyz 
718eae72b5bSSebastien Roy 	if (dlap->dap_npush == 0 || dlap->dap_npush > MAXAPUSH)
719eae72b5bSSebastien Roy 		return (EINVAL);
720f4b3ec61Sdh 
721789e0dbbSdh 	/*
722d62bc4baSyz 	 * Validate that the specified list of modules exist.
723789e0dbbSdh 	 */
7243bc21d0aSAruna Ramakrishna - Sun Microsystems 	for (i = 0; i < dlap->dap_npush; i++) {
725eae72b5bSSebastien Roy 		if (fmodsw_find(dlap->dap_aplist[i], FMODSW_LOAD) == NULL)
726eae72b5bSSebastien Roy 			return (EINVAL);
727d62bc4baSyz 	}
728d62bc4baSyz 
729eae72b5bSSebastien Roy 
7303bc21d0aSAruna Ramakrishna - Sun Microsystems 	key = (mod_hash_key_t)(uintptr_t)linkid;
731789e0dbbSdh 
732d62bc4baSyz 	rw_enter(&dld_ap_hash_lock, RW_WRITER);
733d62bc4baSyz 	if (mod_hash_find(dld_ap_hashp, key, (mod_hash_val_t *)&dap) != 0) {
734d62bc4baSyz 		dap = kmem_zalloc(sizeof (dld_ap_t), KM_NOSLEEP);
735d62bc4baSyz 		if (dap == NULL) {
736d62bc4baSyz 			rw_exit(&dld_ap_hash_lock);
737eae72b5bSSebastien Roy 			return (ENOMEM);
738d62bc4baSyz 		}
739d62bc4baSyz 
7403bc21d0aSAruna Ramakrishna - Sun Microsystems 		dap->da_linkid = linkid;
741eae72b5bSSebastien Roy 		VERIFY(mod_hash_insert(dld_ap_hashp, key,
742eae72b5bSSebastien Roy 		    (mod_hash_val_t)dap) == 0);
743f4b3ec61Sdh 	}
744f4b3ec61Sdh 
745d62bc4baSyz 	/*
746d62bc4baSyz 	 * Update the configuration.
747d62bc4baSyz 	 */
7483bc21d0aSAruna Ramakrishna - Sun Microsystems 	dap->da_anchor = dlap->dap_anchor;
7493bc21d0aSAruna Ramakrishna - Sun Microsystems 	dap->da_npush = dlap->dap_npush;
7503bc21d0aSAruna Ramakrishna - Sun Microsystems 	for (i = 0; i < dlap->dap_npush; i++) {
7513bc21d0aSAruna Ramakrishna - Sun Microsystems 		(void) strlcpy(dap->da_aplist[i], dlap->dap_aplist[i],
752d62bc4baSyz 		    FMNAMESZ + 1);
753d62bc4baSyz 	}
754d62bc4baSyz 	rw_exit(&dld_ap_hash_lock);
755789e0dbbSdh 
7563bc21d0aSAruna Ramakrishna - Sun Microsystems 	return (0);
757d62bc4baSyz }
758d62bc4baSyz 
7593bc21d0aSAruna Ramakrishna - Sun Microsystems static int
7603bc21d0aSAruna Ramakrishna - Sun Microsystems drv_ioc_getap(datalink_id_t linkid, struct dlautopush *dlap)
761d62bc4baSyz {
762d62bc4baSyz 	dld_ap_t	*dap;
763eae72b5bSSebastien Roy 	int		i;
764d62bc4baSyz 
765d62bc4baSyz 	rw_enter(&dld_ap_hash_lock, RW_READER);
766d62bc4baSyz 	if (mod_hash_find(dld_ap_hashp,
7673bc21d0aSAruna Ramakrishna - Sun Microsystems 	    (mod_hash_key_t)(uintptr_t)linkid,
768d62bc4baSyz 	    (mod_hash_val_t *)&dap) != 0) {
769d62bc4baSyz 		rw_exit(&dld_ap_hash_lock);
770eae72b5bSSebastien Roy 		return (ENOENT);
771f595a68aSyz 	}
772d62bc4baSyz 
773d62bc4baSyz 	/*
774d62bc4baSyz 	 * Retrieve the configuration.
775d62bc4baSyz 	 */
7763bc21d0aSAruna Ramakrishna - Sun Microsystems 	dlap->dap_anchor = dap->da_anchor;
7773bc21d0aSAruna Ramakrishna - Sun Microsystems 	dlap->dap_npush = dap->da_npush;
778d62bc4baSyz 	for (i = 0; i < dap->da_npush; i++) {
7793bc21d0aSAruna Ramakrishna - Sun Microsystems 		(void) strlcpy(dlap->dap_aplist[i], dap->da_aplist[i],
780d62bc4baSyz 		    FMNAMESZ + 1);
781d62bc4baSyz 	}
782d62bc4baSyz 	rw_exit(&dld_ap_hash_lock);
783d62bc4baSyz 
7843bc21d0aSAruna Ramakrishna - Sun Microsystems 	return (0);
785f4b3ec61Sdh }
786f4b3ec61Sdh 
7873bc21d0aSAruna Ramakrishna - Sun Microsystems static int
7883bc21d0aSAruna Ramakrishna - Sun Microsystems drv_ioc_clrap(datalink_id_t linkid)
789f4b3ec61Sdh {
790d62bc4baSyz 	mod_hash_val_t	val;
791d62bc4baSyz 	mod_hash_key_t	key;
792f4b3ec61Sdh 
7933bc21d0aSAruna Ramakrishna - Sun Microsystems 	key = (mod_hash_key_t)(uintptr_t)linkid;
794f4b3ec61Sdh 
795d62bc4baSyz 	rw_enter(&dld_ap_hash_lock, RW_WRITER);
796d62bc4baSyz 	if (mod_hash_find(dld_ap_hashp, key, &val) != 0) {
797d62bc4baSyz 		rw_exit(&dld_ap_hash_lock);
7983bc21d0aSAruna Ramakrishna - Sun Microsystems 		return (0);
799f4b3ec61Sdh 	}
800f4b3ec61Sdh 
801d62bc4baSyz 	VERIFY(mod_hash_remove(dld_ap_hashp, key, &val) == 0);
802d62bc4baSyz 	kmem_free(val, sizeof (dld_ap_t));
803d62bc4baSyz 	rw_exit(&dld_ap_hash_lock);
8043bc21d0aSAruna Ramakrishna - Sun Microsystems 	return (0);
805f4b3ec61Sdh }
806f4b3ec61Sdh 
807f4b3ec61Sdh /*
808d62bc4baSyz  * DLDIOC_DOORSERVER
809f4b3ec61Sdh  */
810eae72b5bSSebastien Roy /* ARGSUSED */
811eae72b5bSSebastien Roy static int
812da14cebeSEric Cheng drv_ioc_doorserver(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
813210db224Sericheng {
814eae72b5bSSebastien Roy 	dld_ioc_door_t	*did = karg;
815210db224Sericheng 
816eae72b5bSSebastien Roy 	return (dls_mgmt_door_set(did->did_start_door));
8177c478bd9Sstevel@tonic-gate }
8180ba2cbe9Sxc 
819da14cebeSEric Cheng /*
820da14cebeSEric Cheng  * DLDIOC_USAGELOG
821da14cebeSEric Cheng  */
822da14cebeSEric Cheng /* ARGSUSED */
823da14cebeSEric Cheng static int
824da14cebeSEric Cheng drv_ioc_usagelog(void *karg, intptr_t arg, int mode, cred_t *cred,
825da14cebeSEric Cheng     int *rvalp)
826da14cebeSEric Cheng {
827da14cebeSEric Cheng 	dld_ioc_usagelog_t	*log_info = (dld_ioc_usagelog_t *)karg;
8285d460eafSCathy Zhou 	int			err = 0;
829da14cebeSEric Cheng 
830da14cebeSEric Cheng 	if (log_info->ul_type < MAC_LOGTYPE_LINK ||
831da14cebeSEric Cheng 	    log_info->ul_type > MAC_LOGTYPE_FLOW)
832da14cebeSEric Cheng 		return (EINVAL);
833da14cebeSEric Cheng 
8345d460eafSCathy Zhou 	if (log_info->ul_onoff) {
8355d460eafSCathy Zhou 		err = mac_start_logusage(log_info->ul_type,
8365d460eafSCathy Zhou 		    log_info->ul_interval);
8375d460eafSCathy Zhou 	} else {
838da14cebeSEric Cheng 		mac_stop_logusage(log_info->ul_type);
8395d460eafSCathy Zhou 	}
8405d460eafSCathy Zhou 	return (err);
841da14cebeSEric Cheng }
842da14cebeSEric Cheng 
843da14cebeSEric Cheng /*
844da14cebeSEric Cheng  * Process a DLDIOC_ADDFLOW request.
845da14cebeSEric Cheng  */
846da14cebeSEric Cheng /* ARGSUSED */
847da14cebeSEric Cheng static int
848da14cebeSEric Cheng drv_ioc_addflow(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
849da14cebeSEric Cheng {
850da14cebeSEric Cheng 	dld_ioc_addflow_t	*afp = karg;
851da14cebeSEric Cheng 
852da14cebeSEric Cheng 	return (dld_add_flow(afp->af_linkid, afp->af_name,
853da14cebeSEric Cheng 	    &afp->af_flow_desc, &afp->af_resource_props));
854da14cebeSEric Cheng }
855da14cebeSEric Cheng 
856da14cebeSEric Cheng /*
857da14cebeSEric Cheng  * Process a DLDIOC_REMOVEFLOW request.
858da14cebeSEric Cheng  */
859da14cebeSEric Cheng /* ARGSUSED */
860da14cebeSEric Cheng static int
861da14cebeSEric Cheng drv_ioc_removeflow(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
862da14cebeSEric Cheng {
863da14cebeSEric Cheng 	dld_ioc_removeflow_t	*rfp = karg;
864da14cebeSEric Cheng 
865da14cebeSEric Cheng 	return (dld_remove_flow(rfp->rf_name));
866da14cebeSEric Cheng }
867da14cebeSEric Cheng 
868da14cebeSEric Cheng /*
869da14cebeSEric Cheng  * Process a DLDIOC_MODIFYFLOW request.
870da14cebeSEric Cheng  */
871da14cebeSEric Cheng /* ARGSUSED */
872da14cebeSEric Cheng static int
873da14cebeSEric Cheng drv_ioc_modifyflow(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
874da14cebeSEric Cheng {
875da14cebeSEric Cheng 	dld_ioc_modifyflow_t	*mfp = karg;
876da14cebeSEric Cheng 
877da14cebeSEric Cheng 	return (dld_modify_flow(mfp->mf_name, &mfp->mf_resource_props));
878da14cebeSEric Cheng }
879da14cebeSEric Cheng 
880da14cebeSEric Cheng /*
881da14cebeSEric Cheng  * Process a DLDIOC_WALKFLOW request.
882da14cebeSEric Cheng  */
883da14cebeSEric Cheng /* ARGSUSED */
884da14cebeSEric Cheng static int
885da14cebeSEric Cheng drv_ioc_walkflow(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
886da14cebeSEric Cheng {
887da14cebeSEric Cheng 	dld_ioc_walkflow_t	*wfp = karg;
888da14cebeSEric Cheng 
889da14cebeSEric Cheng 	return (dld_walk_flow(wfp, arg));
890da14cebeSEric Cheng }
891da14cebeSEric Cheng 
892d62bc4baSyz /*
893d62bc4baSyz  * Check for GLDv3 autopush information.  There are three cases:
894d62bc4baSyz  *
895d62bc4baSyz  *   1. If devp points to a GLDv3 datalink and it has autopush configuration,
896d62bc4baSyz  *	fill dlap in with that information and return 0.
897d62bc4baSyz  *
898d62bc4baSyz  *   2. If devp points to a GLDv3 datalink but it doesn't have autopush
899d62bc4baSyz  *	configuration, then replace devp with the physical device (if one
900d62bc4baSyz  *	exists) and return 1.  This allows stropen() to find the old-school
901d62bc4baSyz  *	per-driver autopush configuration.  (For softmac, the result is that
902d62bc4baSyz  *	the softmac dev_t is replaced with the legacy device's dev_t).
903d62bc4baSyz  *
904d62bc4baSyz  *   3. If neither of the above apply, don't touch the args and return -1.
905d62bc4baSyz  */
906d62bc4baSyz int
907d62bc4baSyz dld_autopush(dev_t *devp, struct dlautopush *dlap)
908d62bc4baSyz {
909d62bc4baSyz 	dld_ap_t	*dap;
910d62bc4baSyz 	datalink_id_t	linkid;
911d62bc4baSyz 	dev_t		phydev;
912d62bc4baSyz 
913d62bc4baSyz 	if (!GLDV3_DRV(getmajor(*devp)))
914d62bc4baSyz 		return (-1);
915d62bc4baSyz 
916d62bc4baSyz 	/*
917d62bc4baSyz 	 * Find the linkid by the link's dev_t.
918d62bc4baSyz 	 */
919d62bc4baSyz 	if (dls_devnet_dev2linkid(*devp, &linkid) != 0)
920d62bc4baSyz 		return (-1);
921d62bc4baSyz 
922d62bc4baSyz 	/*
923d62bc4baSyz 	 * Find the autopush configuration associated with the linkid.
924d62bc4baSyz 	 */
925d62bc4baSyz 	rw_enter(&dld_ap_hash_lock, RW_READER);
926d62bc4baSyz 	if (mod_hash_find(dld_ap_hashp, (mod_hash_key_t)(uintptr_t)linkid,
927d62bc4baSyz 	    (mod_hash_val_t *)&dap) == 0) {
928d62bc4baSyz 		*dlap = dap->da_ap;
929d62bc4baSyz 		rw_exit(&dld_ap_hash_lock);
930d62bc4baSyz 		return (0);
931d62bc4baSyz 	}
932d62bc4baSyz 	rw_exit(&dld_ap_hash_lock);
933d62bc4baSyz 
934d62bc4baSyz 	if (dls_devnet_phydev(linkid, &phydev) != 0)
935d62bc4baSyz 		return (-1);
936d62bc4baSyz 
937d62bc4baSyz 	*devp = phydev;
938d62bc4baSyz 	return (1);
939d62bc4baSyz }
940d62bc4baSyz 
9410ba2cbe9Sxc /*
9420ba2cbe9Sxc  * Secure objects implementation
9430ba2cbe9Sxc  */
9440ba2cbe9Sxc 
9450ba2cbe9Sxc /* ARGSUSED */
9460ba2cbe9Sxc static int
9470ba2cbe9Sxc drv_secobj_ctor(void *buf, void *arg, int kmflag)
9480ba2cbe9Sxc {
9490ba2cbe9Sxc 	bzero(buf, sizeof (dld_secobj_t));
9500ba2cbe9Sxc 	return (0);
9510ba2cbe9Sxc }
9520ba2cbe9Sxc 
9530ba2cbe9Sxc static void
9540ba2cbe9Sxc drv_secobj_init(void)
9550ba2cbe9Sxc {
9560ba2cbe9Sxc 	rw_init(&drv_secobj_lock, NULL, RW_DEFAULT, NULL);
9570ba2cbe9Sxc 	drv_secobj_cachep = kmem_cache_create("drv_secobj_cache",
9580ba2cbe9Sxc 	    sizeof (dld_secobj_t), 0, drv_secobj_ctor, NULL,
9590ba2cbe9Sxc 	    NULL, NULL, NULL, 0);
9600ba2cbe9Sxc 	drv_secobj_hash = mod_hash_create_extended("drv_secobj_hash",
9610ba2cbe9Sxc 	    SECOBJ_WEP_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
9620ba2cbe9Sxc 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
9630ba2cbe9Sxc }
9640ba2cbe9Sxc 
9650ba2cbe9Sxc static void
9660ba2cbe9Sxc drv_secobj_fini(void)
9670ba2cbe9Sxc {
9680ba2cbe9Sxc 	mod_hash_destroy_hash(drv_secobj_hash);
9690ba2cbe9Sxc 	kmem_cache_destroy(drv_secobj_cachep);
9700ba2cbe9Sxc 	rw_destroy(&drv_secobj_lock);
9710ba2cbe9Sxc }
9720ba2cbe9Sxc 
973eae72b5bSSebastien Roy /* ARGSUSED */
974eae72b5bSSebastien Roy static int
975da14cebeSEric Cheng drv_ioc_secobj_set(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
9760ba2cbe9Sxc {
977eae72b5bSSebastien Roy 	dld_ioc_secobj_set_t	*ssp = karg;
9780ba2cbe9Sxc 	dld_secobj_t		*sobjp, *objp;
979eae72b5bSSebastien Roy 	int			err;
9800ba2cbe9Sxc 
9810ba2cbe9Sxc 	sobjp = &ssp->ss_obj;
9820ba2cbe9Sxc 
983a399b765Szf 	if (sobjp->so_class != DLD_SECOBJ_CLASS_WEP &&
984a399b765Szf 	    sobjp->so_class != DLD_SECOBJ_CLASS_WPA)
985eae72b5bSSebastien Roy 		return (EINVAL);
9860ba2cbe9Sxc 
9870ba2cbe9Sxc 	if (sobjp->so_name[DLD_SECOBJ_NAME_MAX - 1] != '\0' ||
9880ba2cbe9Sxc 	    sobjp->so_len > DLD_SECOBJ_VAL_MAX)
989eae72b5bSSebastien Roy 		return (EINVAL);
9900ba2cbe9Sxc 
9910ba2cbe9Sxc 	rw_enter(&drv_secobj_lock, RW_WRITER);
9920ba2cbe9Sxc 	err = mod_hash_find(drv_secobj_hash, (mod_hash_key_t)sobjp->so_name,
9930ba2cbe9Sxc 	    (mod_hash_val_t *)&objp);
9940ba2cbe9Sxc 	if (err == 0) {
9950ba2cbe9Sxc 		if ((ssp->ss_flags & DLD_SECOBJ_OPT_CREATE) != 0) {
9960ba2cbe9Sxc 			rw_exit(&drv_secobj_lock);
997eae72b5bSSebastien Roy 			return (EEXIST);
9980ba2cbe9Sxc 		}
9990ba2cbe9Sxc 	} else {
10000ba2cbe9Sxc 		ASSERT(err == MH_ERR_NOTFOUND);
10010ba2cbe9Sxc 		if ((ssp->ss_flags & DLD_SECOBJ_OPT_CREATE) == 0) {
10020ba2cbe9Sxc 			rw_exit(&drv_secobj_lock);
1003eae72b5bSSebastien Roy 			return (ENOENT);
10040ba2cbe9Sxc 		}
10050ba2cbe9Sxc 		objp = kmem_cache_alloc(drv_secobj_cachep, KM_SLEEP);
10060ba2cbe9Sxc 		(void) strlcpy(objp->so_name, sobjp->so_name,
10070ba2cbe9Sxc 		    DLD_SECOBJ_NAME_MAX);
10080ba2cbe9Sxc 
1009eae72b5bSSebastien Roy 		VERIFY(mod_hash_insert(drv_secobj_hash,
1010eae72b5bSSebastien Roy 		    (mod_hash_key_t)objp->so_name, (mod_hash_val_t)objp) == 0);
10110ba2cbe9Sxc 	}
10120ba2cbe9Sxc 	bcopy(sobjp->so_val, objp->so_val, sobjp->so_len);
10130ba2cbe9Sxc 	objp->so_len = sobjp->so_len;
10140ba2cbe9Sxc 	objp->so_class = sobjp->so_class;
10150ba2cbe9Sxc 	rw_exit(&drv_secobj_lock);
1016eae72b5bSSebastien Roy 	return (0);
10170ba2cbe9Sxc }
10180ba2cbe9Sxc 
10190ba2cbe9Sxc typedef struct dld_secobj_state {
10200ba2cbe9Sxc 	uint_t		ss_free;
10210ba2cbe9Sxc 	uint_t		ss_count;
10220ba2cbe9Sxc 	int		ss_rc;
1023eae72b5bSSebastien Roy 	int		ss_mode;
10240ba2cbe9Sxc 	dld_secobj_t	*ss_objp;
10250ba2cbe9Sxc } dld_secobj_state_t;
10260ba2cbe9Sxc 
10270ba2cbe9Sxc /* ARGSUSED */
10280ba2cbe9Sxc static uint_t
10290ba2cbe9Sxc drv_secobj_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
10300ba2cbe9Sxc {
10310ba2cbe9Sxc 	dld_secobj_state_t	*statep = arg;
10320ba2cbe9Sxc 	dld_secobj_t		*sobjp = (dld_secobj_t *)val;
10330ba2cbe9Sxc 
10340ba2cbe9Sxc 	if (statep->ss_free < sizeof (dld_secobj_t)) {
10350ba2cbe9Sxc 		statep->ss_rc = ENOSPC;
10360ba2cbe9Sxc 		return (MH_WALK_TERMINATE);
10370ba2cbe9Sxc 	}
1038eae72b5bSSebastien Roy 	if (ddi_copyout(sobjp, statep->ss_objp, sizeof (*sobjp),
1039eae72b5bSSebastien Roy 	    statep->ss_mode) != 0) {
1040eae72b5bSSebastien Roy 		statep->ss_rc = EFAULT;
1041eae72b5bSSebastien Roy 		return (MH_WALK_TERMINATE);
1042eae72b5bSSebastien Roy 	}
10430ba2cbe9Sxc 	statep->ss_objp++;
10440ba2cbe9Sxc 	statep->ss_free -= sizeof (dld_secobj_t);
10450ba2cbe9Sxc 	statep->ss_count++;
10460ba2cbe9Sxc 	return (MH_WALK_CONTINUE);
10470ba2cbe9Sxc }
10480ba2cbe9Sxc 
1049eae72b5bSSebastien Roy /* ARGSUSED */
1050eae72b5bSSebastien Roy static int
1051da14cebeSEric Cheng drv_ioc_secobj_get(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
10520ba2cbe9Sxc {
1053eae72b5bSSebastien Roy 	dld_ioc_secobj_get_t	*sgp = karg;
10540ba2cbe9Sxc 	dld_secobj_t		*sobjp, *objp;
1055eae72b5bSSebastien Roy 	int			err;
10560ba2cbe9Sxc 
10570ba2cbe9Sxc 	sobjp = &sgp->sg_obj;
10580ba2cbe9Sxc 	if (sobjp->so_name[DLD_SECOBJ_NAME_MAX - 1] != '\0')
1059eae72b5bSSebastien Roy 		return (EINVAL);
10600ba2cbe9Sxc 
10610ba2cbe9Sxc 	rw_enter(&drv_secobj_lock, RW_READER);
10620ba2cbe9Sxc 	if (sobjp->so_name[0] != '\0') {
10630ba2cbe9Sxc 		err = mod_hash_find(drv_secobj_hash,
10640ba2cbe9Sxc 		    (mod_hash_key_t)sobjp->so_name, (mod_hash_val_t *)&objp);
10650ba2cbe9Sxc 		if (err != 0) {
10660ba2cbe9Sxc 			ASSERT(err == MH_ERR_NOTFOUND);
10670ba2cbe9Sxc 			rw_exit(&drv_secobj_lock);
1068eae72b5bSSebastien Roy 			return (ENOENT);
10690ba2cbe9Sxc 		}
10700ba2cbe9Sxc 		bcopy(objp->so_val, sobjp->so_val, objp->so_len);
10710ba2cbe9Sxc 		sobjp->so_len = objp->so_len;
10720ba2cbe9Sxc 		sobjp->so_class = objp->so_class;
10730ba2cbe9Sxc 		sgp->sg_count = 1;
10740ba2cbe9Sxc 	} else {
10750ba2cbe9Sxc 		dld_secobj_state_t	state;
10760ba2cbe9Sxc 
1077eae72b5bSSebastien Roy 		state.ss_free = sgp->sg_size - sizeof (dld_ioc_secobj_get_t);
10780ba2cbe9Sxc 		state.ss_count = 0;
10790ba2cbe9Sxc 		state.ss_rc = 0;
1080eae72b5bSSebastien Roy 		state.ss_mode = mode;
1081eae72b5bSSebastien Roy 		state.ss_objp = (dld_secobj_t *)((uchar_t *)arg +
1082eae72b5bSSebastien Roy 		    sizeof (dld_ioc_secobj_get_t));
1083eae72b5bSSebastien Roy 
10840ba2cbe9Sxc 		mod_hash_walk(drv_secobj_hash, drv_secobj_walker, &state);
10850ba2cbe9Sxc 		if (state.ss_rc != 0) {
10860ba2cbe9Sxc 			rw_exit(&drv_secobj_lock);
1087eae72b5bSSebastien Roy 			return (state.ss_rc);
10880ba2cbe9Sxc 		}
10890ba2cbe9Sxc 		sgp->sg_count = state.ss_count;
10900ba2cbe9Sxc 	}
10910ba2cbe9Sxc 	rw_exit(&drv_secobj_lock);
1092eae72b5bSSebastien Roy 	return (0);
10930ba2cbe9Sxc }
10940ba2cbe9Sxc 
1095eae72b5bSSebastien Roy /* ARGSUSED */
1096eae72b5bSSebastien Roy static int
1097da14cebeSEric Cheng drv_ioc_secobj_unset(void *karg, intptr_t arg, int mode, cred_t *cred,
1098da14cebeSEric Cheng     int *rvalp)
10990ba2cbe9Sxc {
1100eae72b5bSSebastien Roy 	dld_ioc_secobj_unset_t	*sup = karg;
11010ba2cbe9Sxc 	dld_secobj_t		*objp;
11020ba2cbe9Sxc 	mod_hash_val_t		val;
1103eae72b5bSSebastien Roy 	int			err;
11040ba2cbe9Sxc 
11050ba2cbe9Sxc 	if (sup->su_name[DLD_SECOBJ_NAME_MAX - 1] != '\0')
1106eae72b5bSSebastien Roy 		return (EINVAL);
11070ba2cbe9Sxc 
11080ba2cbe9Sxc 	rw_enter(&drv_secobj_lock, RW_WRITER);
11090ba2cbe9Sxc 	err = mod_hash_find(drv_secobj_hash, (mod_hash_key_t)sup->su_name,
11100ba2cbe9Sxc 	    (mod_hash_val_t *)&objp);
11110ba2cbe9Sxc 	if (err != 0) {
11120ba2cbe9Sxc 		ASSERT(err == MH_ERR_NOTFOUND);
11130ba2cbe9Sxc 		rw_exit(&drv_secobj_lock);
1114eae72b5bSSebastien Roy 		return (ENOENT);
11150ba2cbe9Sxc 	}
1116eae72b5bSSebastien Roy 	VERIFY(mod_hash_remove(drv_secobj_hash, (mod_hash_key_t)sup->su_name,
1117eae72b5bSSebastien Roy 	    (mod_hash_val_t *)&val) == 0);
11180ba2cbe9Sxc 	ASSERT(objp == (dld_secobj_t *)val);
11190ba2cbe9Sxc 
11200ba2cbe9Sxc 	kmem_cache_free(drv_secobj_cachep, objp);
11210ba2cbe9Sxc 	rw_exit(&drv_secobj_lock);
1122eae72b5bSSebastien Roy 	return (0);
1123eae72b5bSSebastien Roy }
1124eae72b5bSSebastien Roy 
1125da14cebeSEric Cheng static int
1126da14cebeSEric Cheng drv_check_policy(dld_ioc_info_t *info, cred_t *cred)
1127da14cebeSEric Cheng {
1128da14cebeSEric Cheng 	int	i, err = 0;
1129da14cebeSEric Cheng 
1130da14cebeSEric Cheng 	for (i = 0; info->di_priv[i] != NULL && i < DLD_MAX_PRIV; i++) {
1131da14cebeSEric Cheng 		if ((err = secpolicy_dld_ioctl(cred, info->di_priv[i],
1132da14cebeSEric Cheng 		    "dld ioctl")) != 0) {
1133da14cebeSEric Cheng 			break;
1134da14cebeSEric Cheng 		}
1135da14cebeSEric Cheng 	}
1136da14cebeSEric Cheng 	if (err == 0)
1137da14cebeSEric Cheng 		return (0);
1138da14cebeSEric Cheng 
1139da14cebeSEric Cheng 	return (secpolicy_net_config(cred, B_FALSE));
1140da14cebeSEric Cheng }
1141da14cebeSEric Cheng 
1142eae72b5bSSebastien Roy static dld_ioc_info_t drv_ioc_list[] = {
1143eae72b5bSSebastien Roy 	{DLDIOC_ATTR, DLDCOPYINOUT, sizeof (dld_ioc_attr_t),
1144da14cebeSEric Cheng 	    drv_ioc_attr, {NULL}},
1145eae72b5bSSebastien Roy 	{DLDIOC_PHYS_ATTR, DLDCOPYINOUT, sizeof (dld_ioc_phys_attr_t),
1146da14cebeSEric Cheng 	    drv_ioc_phys_attr, {NULL}},
1147da14cebeSEric Cheng 	{DLDIOC_SECOBJ_SET, DLDCOPYIN, sizeof (dld_ioc_secobj_set_t),
1148da14cebeSEric Cheng 	    drv_ioc_secobj_set, {PRIV_SYS_DL_CONFIG}},
1149da14cebeSEric Cheng 	{DLDIOC_SECOBJ_GET, DLDCOPYINOUT, sizeof (dld_ioc_secobj_get_t),
1150da14cebeSEric Cheng 	    drv_ioc_secobj_get, {PRIV_SYS_DL_CONFIG}},
1151da14cebeSEric Cheng 	{DLDIOC_SECOBJ_UNSET, DLDCOPYIN, sizeof (dld_ioc_secobj_unset_t),
1152da14cebeSEric Cheng 	    drv_ioc_secobj_unset, {PRIV_SYS_DL_CONFIG}},
1153da14cebeSEric Cheng 	{DLDIOC_DOORSERVER, DLDCOPYIN, sizeof (dld_ioc_door_t),
1154da14cebeSEric Cheng 	    drv_ioc_doorserver, {PRIV_SYS_DL_CONFIG}},
1155da14cebeSEric Cheng 	{DLDIOC_RENAME, DLDCOPYIN, sizeof (dld_ioc_rename_t),
1156da14cebeSEric Cheng 	    drv_ioc_rename, {PRIV_SYS_DL_CONFIG}},
1157da14cebeSEric Cheng 	{DLDIOC_MACADDRGET, DLDCOPYINOUT, sizeof (dld_ioc_macaddrget_t),
1158da14cebeSEric Cheng 	    drv_ioc_macaddrget, {PRIV_SYS_DL_CONFIG}},
1159da14cebeSEric Cheng 	{DLDIOC_ADDFLOW, DLDCOPYIN, sizeof (dld_ioc_addflow_t),
1160da14cebeSEric Cheng 	    drv_ioc_addflow, {PRIV_SYS_DL_CONFIG}},
1161da14cebeSEric Cheng 	{DLDIOC_REMOVEFLOW, DLDCOPYIN, sizeof (dld_ioc_removeflow_t),
1162da14cebeSEric Cheng 	    drv_ioc_removeflow, {PRIV_SYS_DL_CONFIG}},
1163da14cebeSEric Cheng 	{DLDIOC_MODIFYFLOW, DLDCOPYIN, sizeof (dld_ioc_modifyflow_t),
1164da14cebeSEric Cheng 	    drv_ioc_modifyflow, {PRIV_SYS_DL_CONFIG}},
1165da14cebeSEric Cheng 	{DLDIOC_WALKFLOW, DLDCOPYINOUT, sizeof (dld_ioc_walkflow_t),
1166da14cebeSEric Cheng 	    drv_ioc_walkflow, {NULL}},
1167da14cebeSEric Cheng 	{DLDIOC_USAGELOG, DLDCOPYIN, sizeof (dld_ioc_usagelog_t),
1168da14cebeSEric Cheng 	    drv_ioc_usagelog, {PRIV_SYS_DL_CONFIG}},
1169da14cebeSEric Cheng 	{DLDIOC_SETMACPROP, DLDCOPYIN, sizeof (dld_ioc_macprop_t),
1170da14cebeSEric Cheng 	    drv_ioc_setprop, {PRIV_SYS_DL_CONFIG}},
1171eae72b5bSSebastien Roy 	{DLDIOC_GETMACPROP, DLDCOPYIN, sizeof (dld_ioc_macprop_t),
1172da14cebeSEric Cheng 	    drv_ioc_getprop, {NULL}},
1173da14cebeSEric Cheng 	{DLDIOC_GETHWGRP, DLDCOPYINOUT, sizeof (dld_ioc_hwgrpget_t),
1174da14cebeSEric Cheng 	    drv_ioc_hwgrpget, {PRIV_SYS_DL_CONFIG}},
1175eae72b5bSSebastien Roy };
1176eae72b5bSSebastien Roy 
1177eae72b5bSSebastien Roy typedef struct dld_ioc_modentry {
1178eae72b5bSSebastien Roy 	uint16_t	dim_modid;	/* Top 16 bits of ioctl command */
1179eae72b5bSSebastien Roy 	char		*dim_modname;	/* Module to be loaded */
1180eae72b5bSSebastien Roy 	dld_ioc_info_t	*dim_list;	/* array of ioctl structures */
1181eae72b5bSSebastien Roy 	uint_t		dim_count;	/* number of elements in dim_list */
1182eae72b5bSSebastien Roy } dld_ioc_modentry_t;
1183eae72b5bSSebastien Roy 
1184eae72b5bSSebastien Roy /*
1185eae72b5bSSebastien Roy  * For all modules except for dld, dim_list and dim_count are assigned
1186eae72b5bSSebastien Roy  * when the modules register their ioctls in dld_ioc_register().  We
1187eae72b5bSSebastien Roy  * can statically initialize dld's ioctls in-line here; there's no
1188eae72b5bSSebastien Roy  * need for it to call dld_ioc_register() itself.
1189eae72b5bSSebastien Roy  */
1190eae72b5bSSebastien Roy static dld_ioc_modentry_t dld_ioc_modtable[] = {
1191eae72b5bSSebastien Roy 	{DLD_IOC,	"dld",	drv_ioc_list, DLDIOCCNT(drv_ioc_list)},
1192eae72b5bSSebastien Roy 	{AGGR_IOC,	"aggr",	NULL, 0},
1193eae72b5bSSebastien Roy 	{VNIC_IOC,	"vnic",	NULL, 0}
1194eae72b5bSSebastien Roy };
1195eae72b5bSSebastien Roy #define	DLDIOC_CNT	\
1196eae72b5bSSebastien Roy 	(sizeof (dld_ioc_modtable) / sizeof (dld_ioc_modentry_t))
1197eae72b5bSSebastien Roy 
1198eae72b5bSSebastien Roy static dld_ioc_modentry_t *
1199eae72b5bSSebastien Roy dld_ioc_findmod(uint16_t modid)
1200eae72b5bSSebastien Roy {
1201eae72b5bSSebastien Roy 	int	i;
1202eae72b5bSSebastien Roy 
1203eae72b5bSSebastien Roy 	for (i = 0; i < DLDIOC_CNT; i++) {
1204eae72b5bSSebastien Roy 		if (modid == dld_ioc_modtable[i].dim_modid)
1205eae72b5bSSebastien Roy 			return (&dld_ioc_modtable[i]);
1206eae72b5bSSebastien Roy 	}
1207eae72b5bSSebastien Roy 	return (NULL);
1208eae72b5bSSebastien Roy }
1209eae72b5bSSebastien Roy 
1210eae72b5bSSebastien Roy int
1211eae72b5bSSebastien Roy dld_ioc_register(uint16_t modid, dld_ioc_info_t *list, uint_t count)
1212eae72b5bSSebastien Roy {
1213eae72b5bSSebastien Roy 	dld_ioc_modentry_t *dim = dld_ioc_findmod(modid);
1214eae72b5bSSebastien Roy 
1215eae72b5bSSebastien Roy 	if (dim == NULL)
1216eae72b5bSSebastien Roy 		return (ENOENT);
1217eae72b5bSSebastien Roy 
1218eae72b5bSSebastien Roy 	dim->dim_list = list;
1219eae72b5bSSebastien Roy 	dim->dim_count = count;
1220eae72b5bSSebastien Roy 	return (0);
1221eae72b5bSSebastien Roy }
1222eae72b5bSSebastien Roy 
1223eae72b5bSSebastien Roy void
1224eae72b5bSSebastien Roy dld_ioc_unregister(uint16_t modid)
1225eae72b5bSSebastien Roy {
1226eae72b5bSSebastien Roy 	VERIFY(dld_ioc_register(modid, NULL, 0) == 0);
1227eae72b5bSSebastien Roy }
12280ba2cbe9Sxc 
1229eae72b5bSSebastien Roy /*
1230eae72b5bSSebastien Roy  * The general design with GLDv3 ioctls is that all ioctls issued
1231eae72b5bSSebastien Roy  * through /dev/dld go through this drv_ioctl() function.  This
1232eae72b5bSSebastien Roy  * function handles all ioctls on behalf of modules listed in
1233eae72b5bSSebastien Roy  * dld_ioc_modtable.
1234eae72b5bSSebastien Roy  *
1235eae72b5bSSebastien Roy  * When an ioctl is received, this function looks for the associated
1236eae72b5bSSebastien Roy  * module-id-specific ioctl information using dld_ioc_findmod().  The
1237eae72b5bSSebastien Roy  * call to ddi_hold_devi_by_instance() on the associated device will
1238eae72b5bSSebastien Roy  * cause the kernel module responsible for the ioctl to be loaded if
1239eae72b5bSSebastien Roy  * it's not already loaded, which should result in that module calling
1240eae72b5bSSebastien Roy  * dld_ioc_register(), thereby filling in the dim_list containing the
1241eae72b5bSSebastien Roy  * details for the ioctl being processed.
1242eae72b5bSSebastien Roy  *
1243eae72b5bSSebastien Roy  * This function can then perform operations such as copyin() data and
1244eae72b5bSSebastien Roy  * do credential checks based on the registered ioctl information,
1245eae72b5bSSebastien Roy  * then issue the callback function di_func() registered by the
1246eae72b5bSSebastien Roy  * responsible module.  Upon return, the appropriate copyout()
1247eae72b5bSSebastien Roy  * operation can be performed and the operation completes.
1248eae72b5bSSebastien Roy  */
1249eae72b5bSSebastien Roy /* ARGSUSED */
1250eae72b5bSSebastien Roy static int
1251eae72b5bSSebastien Roy drv_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1252eae72b5bSSebastien Roy {
1253eae72b5bSSebastien Roy 	dld_ioc_modentry_t *dim;
1254eae72b5bSSebastien Roy 	dld_ioc_info_t	*info;
1255eae72b5bSSebastien Roy 	dev_info_t	*dip = NULL;
1256eae72b5bSSebastien Roy 	void		*buf = NULL;
1257eae72b5bSSebastien Roy 	size_t		sz;
1258eae72b5bSSebastien Roy 	int		i, err;
1259eae72b5bSSebastien Roy 
1260eae72b5bSSebastien Roy 	if ((dim = dld_ioc_findmod(DLD_IOC_MODID(cmd))) == NULL)
1261eae72b5bSSebastien Roy 		return (ENOTSUP);
1262eae72b5bSSebastien Roy 
1263eae72b5bSSebastien Roy 	dip = ddi_hold_devi_by_instance(ddi_name_to_major(dim->dim_modname),
1264eae72b5bSSebastien Roy 	    0, 0);
1265eae72b5bSSebastien Roy 	if (dip == NULL || dim->dim_list == NULL) {
1266eae72b5bSSebastien Roy 		err = ENODEV;
1267eae72b5bSSebastien Roy 		goto done;
1268eae72b5bSSebastien Roy 	}
1269eae72b5bSSebastien Roy 
1270eae72b5bSSebastien Roy 	for (i = 0; i < dim->dim_count; i++) {
1271eae72b5bSSebastien Roy 		if (cmd == dim->dim_list[i].di_cmd)
1272eae72b5bSSebastien Roy 			break;
1273eae72b5bSSebastien Roy 	}
1274eae72b5bSSebastien Roy 	if (i == dim->dim_count) {
1275eae72b5bSSebastien Roy 		err = ENOTSUP;
1276eae72b5bSSebastien Roy 		goto done;
1277eae72b5bSSebastien Roy 	}
1278eae72b5bSSebastien Roy 
1279eae72b5bSSebastien Roy 	info = &dim->dim_list[i];
1280da14cebeSEric Cheng 	if ((err = drv_check_policy(info, cred)) != 0)
1281eae72b5bSSebastien Roy 		goto done;
1282eae72b5bSSebastien Roy 
1283eae72b5bSSebastien Roy 	sz = info->di_argsize;
1284eae72b5bSSebastien Roy 	if ((buf = kmem_zalloc(sz, KM_NOSLEEP)) == NULL) {
1285eae72b5bSSebastien Roy 		err = ENOMEM;
1286eae72b5bSSebastien Roy 		goto done;
1287eae72b5bSSebastien Roy 	}
1288eae72b5bSSebastien Roy 
1289eae72b5bSSebastien Roy 	if ((info->di_flags & DLDCOPYIN) &&
1290eae72b5bSSebastien Roy 	    ddi_copyin((void *)arg, buf, sz, mode) != 0) {
1291eae72b5bSSebastien Roy 		err = EFAULT;
1292eae72b5bSSebastien Roy 		goto done;
1293eae72b5bSSebastien Roy 	}
1294eae72b5bSSebastien Roy 
1295da14cebeSEric Cheng 	err = info->di_func(buf, arg, mode, cred, rvalp);
1296eae72b5bSSebastien Roy 
1297eae72b5bSSebastien Roy 	if ((info->di_flags & DLDCOPYOUT) &&
1298eae72b5bSSebastien Roy 	    ddi_copyout(buf, (void *)arg, sz, mode) != 0 && err == 0)
1299eae72b5bSSebastien Roy 		err = EFAULT;
1300eae72b5bSSebastien Roy 
1301eae72b5bSSebastien Roy done:
1302eae72b5bSSebastien Roy 	if (buf != NULL)
1303eae72b5bSSebastien Roy 		kmem_free(buf, sz);
1304eae72b5bSSebastien Roy 	if (dip != NULL)
1305eae72b5bSSebastien Roy 		ddi_release_devi(dip);
1306eae72b5bSSebastien Roy 	return (err);
13070ba2cbe9Sxc }
1308