xref: /illumos-gate/usr/src/uts/common/io/dld/dld_drv.c (revision 0dc2366f7b9f9f36e10909b1e95edbf2a261c2ac)
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 /*
22*0dc2366fSVenugopal Iyer  * Copyright 2010 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>
3582a2fc47SJames 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>
462b24ab6bSSebastien Roy #include	<sys/zone.h>
47210db224Sericheng 
487c478bd9Sstevel@tonic-gate static void	drv_init(void);
497c478bd9Sstevel@tonic-gate static int	drv_fini(void);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static int	drv_getinfo(dev_info_t	*, ddi_info_cmd_t, void *, void **);
527c478bd9Sstevel@tonic-gate static int	drv_attach(dev_info_t *, ddi_attach_cmd_t);
537c478bd9Sstevel@tonic-gate static int	drv_detach(dev_info_t *, ddi_detach_cmd_t);
547c478bd9Sstevel@tonic-gate 
550ba2cbe9Sxc /*
560ba2cbe9Sxc  * Secure objects declarations
570ba2cbe9Sxc  */
580ba2cbe9Sxc #define	SECOBJ_WEP_HASHSZ	67
590ba2cbe9Sxc static krwlock_t	drv_secobj_lock;
600ba2cbe9Sxc static kmem_cache_t	*drv_secobj_cachep;
610ba2cbe9Sxc static mod_hash_t	*drv_secobj_hash;
620ba2cbe9Sxc static void		drv_secobj_init(void);
630ba2cbe9Sxc static void		drv_secobj_fini(void);
643bc21d0aSAruna Ramakrishna - Sun Microsystems static int		drv_ioc_setap(datalink_id_t, struct dlautopush *);
653bc21d0aSAruna Ramakrishna - Sun Microsystems static int		drv_ioc_getap(datalink_id_t, struct dlautopush *);
663bc21d0aSAruna Ramakrishna - Sun Microsystems static int		drv_ioc_clrap(datalink_id_t);
670ba2cbe9Sxc 
68eae72b5bSSebastien Roy 
69210db224Sericheng /*
70210db224Sericheng  * The following entry points are private to dld and are used for control
71210db224Sericheng  * operations only. The entry points exported to mac drivers are defined
72210db224Sericheng  * in dld_str.c. Refer to the comment on top of dld_str.c for details.
73210db224Sericheng  */
74eae72b5bSSebastien Roy static int	drv_open(dev_t *, int, int, cred_t *);
75eae72b5bSSebastien Roy static int	drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
767c478bd9Sstevel@tonic-gate 
77eae72b5bSSebastien Roy static dev_info_t	*dld_dip;	/* dev_info_t for the driver */
78eae72b5bSSebastien Roy uint32_t		dld_opt = 0;	/* Global options */
797c478bd9Sstevel@tonic-gate 
80d62bc4baSyz #define	NAUTOPUSH 32
81d62bc4baSyz static mod_hash_t *dld_ap_hashp;
82d62bc4baSyz static krwlock_t dld_ap_hash_lock;
83d62bc4baSyz 
84eae72b5bSSebastien Roy static struct cb_ops drv_cb_ops = {
85eae72b5bSSebastien Roy 	drv_open,		/* open */
86eae72b5bSSebastien Roy 	nulldev,		/* close */
87eae72b5bSSebastien Roy 	nulldev,		/* strategy */
88eae72b5bSSebastien Roy 	nulldev,		/* print */
89eae72b5bSSebastien Roy 	nodev,			/* dump */
90eae72b5bSSebastien Roy 	nodev,			/* read */
91eae72b5bSSebastien Roy 	nodev,			/* write */
92eae72b5bSSebastien Roy 	drv_ioctl,		/* ioctl */
93eae72b5bSSebastien Roy 	nodev,			/* devmap */
94eae72b5bSSebastien Roy 	nodev,			/* mmap */
95eae72b5bSSebastien Roy 	nodev,			/* segmap */
96eae72b5bSSebastien Roy 	nochpoll,		/* poll */
97eae72b5bSSebastien Roy 	ddi_prop_op,		/* cb_prop_op */
98eae72b5bSSebastien Roy 	0,			/* streamtab  */
99eae72b5bSSebastien Roy 	D_MP			/* Driver compatibility flag */
1007c478bd9Sstevel@tonic-gate };
1017c478bd9Sstevel@tonic-gate 
102eae72b5bSSebastien Roy static struct dev_ops drv_ops = {
103eae72b5bSSebastien Roy 	DEVO_REV,		/* devo_rev */
104eae72b5bSSebastien Roy 	0,			/* refcnt */
105eae72b5bSSebastien Roy 	drv_getinfo,		/* get_dev_info */
106eae72b5bSSebastien Roy 	nulldev,		/* identify */
107eae72b5bSSebastien Roy 	nulldev,		/* probe */
108eae72b5bSSebastien Roy 	drv_attach,		/* attach */
109eae72b5bSSebastien Roy 	drv_detach,		/* detach */
110eae72b5bSSebastien Roy 	nodev,			/* reset */
111eae72b5bSSebastien Roy 	&drv_cb_ops,		/* driver operations */
112eae72b5bSSebastien Roy 	NULL,			/* bus operations */
11319397407SSherry Moore 	nodev,			/* dev power */
11419397407SSherry Moore 	ddi_quiesce_not_supported,	/* dev quiesce */
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate static	struct modldrv		drv_modldrv = {
1217c478bd9Sstevel@tonic-gate 	&mod_driverops,
1227c478bd9Sstevel@tonic-gate 	DLD_INFO,
1237c478bd9Sstevel@tonic-gate 	&drv_ops
1247c478bd9Sstevel@tonic-gate };
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static	struct modlinkage	drv_modlinkage = {
1277c478bd9Sstevel@tonic-gate 	MODREV_1,
1287c478bd9Sstevel@tonic-gate 	&drv_modldrv,
1297c478bd9Sstevel@tonic-gate 	NULL
1307c478bd9Sstevel@tonic-gate };
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate int
1337c478bd9Sstevel@tonic-gate _init(void)
1347c478bd9Sstevel@tonic-gate {
135eae72b5bSSebastien Roy 	return (mod_install(&drv_modlinkage));
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate int
1397c478bd9Sstevel@tonic-gate _fini(void)
1407c478bd9Sstevel@tonic-gate {
141eae72b5bSSebastien Roy 	return (mod_remove(&drv_modlinkage));
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate int
1457c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	return (mod_info(&drv_modlinkage, modinfop));
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
151210db224Sericheng  * Initialize component modules.
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate static void
1547c478bd9Sstevel@tonic-gate drv_init(void)
1557c478bd9Sstevel@tonic-gate {
1560ba2cbe9Sxc 	drv_secobj_init();
1577c478bd9Sstevel@tonic-gate 	dld_str_init();
158da14cebeSEric Cheng 
159d62bc4baSyz 	/*
160d62bc4baSyz 	 * Create a hash table for autopush configuration.
161d62bc4baSyz 	 */
162d62bc4baSyz 	dld_ap_hashp = mod_hash_create_idhash("dld_autopush_hash",
163d62bc4baSyz 	    NAUTOPUSH, mod_hash_null_valdtor);
164d62bc4baSyz 
165d62bc4baSyz 	ASSERT(dld_ap_hashp != NULL);
166d62bc4baSyz 	rw_init(&dld_ap_hash_lock, NULL, RW_DRIVER, NULL);
167d62bc4baSyz }
168d62bc4baSyz 
169d62bc4baSyz /* ARGSUSED */
170d62bc4baSyz static uint_t
171d62bc4baSyz drv_ap_exist(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
172d62bc4baSyz {
173d62bc4baSyz 	boolean_t *pexist = arg;
174d62bc4baSyz 
175d62bc4baSyz 	*pexist = B_TRUE;
176d62bc4baSyz 	return (MH_WALK_TERMINATE);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate static int
1807c478bd9Sstevel@tonic-gate drv_fini(void)
1817c478bd9Sstevel@tonic-gate {
182d62bc4baSyz 	int		err;
183d62bc4baSyz 	boolean_t	exist = B_FALSE;
184d62bc4baSyz 
185d62bc4baSyz 	rw_enter(&dld_ap_hash_lock, RW_READER);
186d62bc4baSyz 	mod_hash_walk(dld_ap_hashp, drv_ap_exist, &exist);
187d62bc4baSyz 	rw_exit(&dld_ap_hash_lock);
188d62bc4baSyz 	if (exist)
189d62bc4baSyz 		return (EBUSY);
1907c478bd9Sstevel@tonic-gate 
191210db224Sericheng 	if ((err = dld_str_fini()) != 0)
1927c478bd9Sstevel@tonic-gate 		return (err);
1937c478bd9Sstevel@tonic-gate 
1940ba2cbe9Sxc 	drv_secobj_fini();
195d62bc4baSyz 	mod_hash_destroy_idhash(dld_ap_hashp);
196d62bc4baSyz 	rw_destroy(&dld_ap_hash_lock);
1977c478bd9Sstevel@tonic-gate 	return (0);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate  * devo_getinfo: getinfo(9e)
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2047c478bd9Sstevel@tonic-gate static int
2057c478bd9Sstevel@tonic-gate drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **resp)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	if (dld_dip == NULL)
2087c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	switch (cmd) {
2117c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
212eae72b5bSSebastien Roy 		*resp = 0;
2137c478bd9Sstevel@tonic-gate 		break;
2147c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
215eae72b5bSSebastien Roy 		*resp = dld_dip;
2167c478bd9Sstevel@tonic-gate 		break;
2177c478bd9Sstevel@tonic-gate 	default:
2187c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * Check properties to set options. (See dld.h for property definitions).
2267c478bd9Sstevel@tonic-gate  */
2277c478bd9Sstevel@tonic-gate static void
2287c478bd9Sstevel@tonic-gate drv_set_opt(dev_info_t *dip)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2317c478bd9Sstevel@tonic-gate 	    DLD_PROP_NO_FASTPATH, 0) != 0) {
2327c478bd9Sstevel@tonic-gate 		dld_opt |= DLD_OPT_NO_FASTPATH;
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2367c478bd9Sstevel@tonic-gate 	    DLD_PROP_NO_POLL, 0) != 0) {
2377c478bd9Sstevel@tonic-gate 		dld_opt |= DLD_OPT_NO_POLL;
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2417c478bd9Sstevel@tonic-gate 	    DLD_PROP_NO_ZEROCOPY, 0) != 0) {
2427c478bd9Sstevel@tonic-gate 		dld_opt |= DLD_OPT_NO_ZEROCOPY;
2437c478bd9Sstevel@tonic-gate 	}
2449c175129Sja 
2459c175129Sja 	if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
2469c175129Sja 	    DLD_PROP_NO_SOFTRING, 0) != 0) {
2479c175129Sja 		dld_opt |= DLD_OPT_NO_SOFTRING;
2489c175129Sja 	}
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * devo_attach: attach(9e)
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate static int
2557c478bd9Sstevel@tonic-gate drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
2587c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	ASSERT(ddi_get_instance(dip) == 0);
261eae72b5bSSebastien Roy 	drv_init();
2627c478bd9Sstevel@tonic-gate 	drv_set_opt(dip);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 * Create control node. DLPI provider nodes will be created on demand.
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, DLD_CONTROL_MINOR_NAME, S_IFCHR,
2687c478bd9Sstevel@tonic-gate 	    DLD_CONTROL_MINOR, DDI_PSEUDO, 0) != DDI_SUCCESS)
2697c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	dld_dip = dip;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/*
2747c478bd9Sstevel@tonic-gate 	 * Log the fact that the driver is now attached.
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	ddi_report_dev(dip);
2777c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * devo_detach: detach(9e)
2827c478bd9Sstevel@tonic-gate  */
2837c478bd9Sstevel@tonic-gate static int
2847c478bd9Sstevel@tonic-gate drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
2877c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	ASSERT(dld_dip == dip);
290eae72b5bSSebastien Roy 	if (drv_fini() != 0)
291eae72b5bSSebastien Roy 		return (DDI_FAILURE);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	/*
2947c478bd9Sstevel@tonic-gate 	 * Remove the control node.
2957c478bd9Sstevel@tonic-gate 	 */
2967c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(dip, DLD_CONTROL_MINOR_NAME);
2977c478bd9Sstevel@tonic-gate 	dld_dip = NULL;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
303210db224Sericheng  * dld control node open procedure.
3047c478bd9Sstevel@tonic-gate  */
3057c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3067c478bd9Sstevel@tonic-gate static int
307eae72b5bSSebastien Roy drv_open(dev_t *devp, int flag, int sflag, cred_t *credp)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	/*
310eae72b5bSSebastien Roy 	 * Only the control node can be opened.
3117c478bd9Sstevel@tonic-gate 	 */
312eae72b5bSSebastien Roy 	if (getminor(*devp) != DLD_CONTROL_MINOR)
313eae72b5bSSebastien Roy 		return (ENODEV);
3147c478bd9Sstevel@tonic-gate 	return (0);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3172b24ab6bSSebastien Roy /*
3182b24ab6bSSebastien Roy  * Verify if the caller is allowed to modify a link of the given class.
3192b24ab6bSSebastien Roy  */
3202b24ab6bSSebastien Roy static int
3212b24ab6bSSebastien Roy drv_ioc_checkprivs(datalink_class_t class, cred_t *cred)
3222b24ab6bSSebastien Roy {
3232b24ab6bSSebastien Roy 	if (class == DATALINK_CLASS_IPTUN)
3242b24ab6bSSebastien Roy 		return (secpolicy_iptun_config(cred));
3252b24ab6bSSebastien Roy 	return (secpolicy_dl_config(cred));
3262b24ab6bSSebastien Roy }
3272b24ab6bSSebastien Roy 
3287c478bd9Sstevel@tonic-gate /*
329d62bc4baSyz  * DLDIOC_ATTR
3307c478bd9Sstevel@tonic-gate  */
331eae72b5bSSebastien Roy /* ARGSUSED */
332eae72b5bSSebastien Roy static int
333da14cebeSEric Cheng drv_ioc_attr(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
3347c478bd9Sstevel@tonic-gate {
335eae72b5bSSebastien Roy 	dld_ioc_attr_t		*diap = karg;
336d62bc4baSyz 	dls_dl_handle_t		dlh;
337da14cebeSEric Cheng 	dls_link_t		*dlp;
3382b24ab6bSSebastien Roy 	zoneid_t		zoneid = crgetzoneid(cred);
339d62bc4baSyz 	int			err;
340da14cebeSEric Cheng 	mac_perim_handle_t	mph;
341210db224Sericheng 
3422b24ab6bSSebastien Roy 	if (zoneid != GLOBAL_ZONEID &&
3432b24ab6bSSebastien Roy 	    zone_check_datalink(&zoneid, diap->dia_linkid) != 0)
3442b24ab6bSSebastien Roy 		return (ENOENT);
3452b24ab6bSSebastien Roy 
346d62bc4baSyz 	if ((err = dls_devnet_hold_tmp(diap->dia_linkid, &dlh)) != 0)
347eae72b5bSSebastien Roy 		return (err);
348210db224Sericheng 
349da14cebeSEric Cheng 	if ((err = mac_perim_enter_by_macname(
350da14cebeSEric Cheng 	    dls_devnet_mac(dlh), &mph)) != 0) {
351d62bc4baSyz 		dls_devnet_rele_tmp(dlh);
352eae72b5bSSebastien Roy 		return (err);
353d62bc4baSyz 	}
354210db224Sericheng 
355da14cebeSEric Cheng 	if ((err = dls_link_hold(dls_devnet_mac(dlh), &dlp)) != 0) {
356da14cebeSEric Cheng 		mac_perim_exit(mph);
357da14cebeSEric Cheng 		dls_devnet_rele_tmp(dlh);
358da14cebeSEric Cheng 		return (err);
359da14cebeSEric Cheng 	}
360da14cebeSEric Cheng 
361da14cebeSEric Cheng 	mac_sdu_get(dlp->dl_mh, NULL, &diap->dia_max_sdu);
362da14cebeSEric Cheng 
363da14cebeSEric Cheng 	dls_link_rele(dlp);
364da14cebeSEric Cheng 	mac_perim_exit(mph);
365d62bc4baSyz 	dls_devnet_rele_tmp(dlh);
366d62bc4baSyz 
367eae72b5bSSebastien Roy 	return (0);
368d62bc4baSyz }
369210db224Sericheng 
370d62bc4baSyz /*
371d62bc4baSyz  * DLDIOC_PHYS_ATTR
372d62bc4baSyz  */
373eae72b5bSSebastien Roy /* ARGSUSED */
374eae72b5bSSebastien Roy static int
375da14cebeSEric Cheng drv_ioc_phys_attr(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
376d62bc4baSyz {
377eae72b5bSSebastien Roy 	dld_ioc_phys_attr_t	*dipp = karg;
378d62bc4baSyz 	int			err;
379d62bc4baSyz 	dls_dl_handle_t		dlh;
380d62bc4baSyz 	dls_dev_handle_t	ddh;
381d62bc4baSyz 	dev_t			phydev;
3822b24ab6bSSebastien Roy 	zoneid_t		zoneid = crgetzoneid(cred);
3832b24ab6bSSebastien Roy 
3842b24ab6bSSebastien Roy 	if (zoneid != GLOBAL_ZONEID &&
3852b24ab6bSSebastien Roy 	    zone_check_datalink(&zoneid, dipp->dip_linkid) != 0)
3862b24ab6bSSebastien Roy 		return (ENOENT);
387d62bc4baSyz 
388d62bc4baSyz 	/*
389d62bc4baSyz 	 * Every physical link should have its physical dev_t kept in the
390d62bc4baSyz 	 * daemon. If not, it is not a valid physical link.
391d62bc4baSyz 	 */
392eae72b5bSSebastien Roy 	if (dls_mgmt_get_phydev(dipp->dip_linkid, &phydev) != 0)
393eae72b5bSSebastien Roy 		return (EINVAL);
394d62bc4baSyz 
395d62bc4baSyz 	/*
396d62bc4baSyz 	 * Although this is a valid physical link, it might already be removed
397d62bc4baSyz 	 * by DR or during system shutdown. softmac_hold_device() would return
398d62bc4baSyz 	 * ENOENT in this case.
399d62bc4baSyz 	 */
400d62bc4baSyz 	if ((err = softmac_hold_device(phydev, &ddh)) != 0)
401eae72b5bSSebastien Roy 		return (err);
402d62bc4baSyz 
403d62bc4baSyz 	if (dls_devnet_hold_tmp(dipp->dip_linkid, &dlh) != 0) {
404210db224Sericheng 		/*
405d62bc4baSyz 		 * Although this is an active physical link, its link type is
406d62bc4baSyz 		 * not supported by GLDv3, and therefore it does not have
407d62bc4baSyz 		 * vanity naming support.
408210db224Sericheng 		 */
409d62bc4baSyz 		dipp->dip_novanity = B_TRUE;
410d62bc4baSyz 	} else {
411d62bc4baSyz 		dipp->dip_novanity = B_FALSE;
412d62bc4baSyz 		dls_devnet_rele_tmp(dlh);
413210db224Sericheng 	}
414d62bc4baSyz 	/*
415d62bc4baSyz 	 * Get the physical device name from the major number and the instance
416d62bc4baSyz 	 * number derived from phydev.
417d62bc4baSyz 	 */
418d62bc4baSyz 	(void) snprintf(dipp->dip_dev, MAXLINKNAMELEN, "%s%d",
419d62bc4baSyz 	    ddi_major_to_name(getmajor(phydev)), getminor(phydev) - 1);
420d62bc4baSyz 
421d62bc4baSyz 	softmac_rele_device(ddh);
422eae72b5bSSebastien Roy 	return (0);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
425da14cebeSEric Cheng /* ARGSUSED */
426da14cebeSEric Cheng static int
427da14cebeSEric Cheng drv_ioc_hwgrpget(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
428da14cebeSEric Cheng {
429da14cebeSEric Cheng 	dld_ioc_hwgrpget_t	*hwgrpp = karg;
430da14cebeSEric Cheng 	dld_hwgrpinfo_t		hwgrp, *hip;
431da14cebeSEric Cheng 	mac_handle_t		mh = NULL;
432*0dc2366fSVenugopal Iyer 	int			i, err, rgrpnum, tgrpnum;
433da14cebeSEric Cheng 	uint_t			bytes_left;
434*0dc2366fSVenugopal Iyer 	int			totgrps = 0;
4352b24ab6bSSebastien Roy 	zoneid_t		zoneid = crgetzoneid(cred);
4362b24ab6bSSebastien Roy 
4372b24ab6bSSebastien Roy 	if (zoneid != GLOBAL_ZONEID &&
4382b24ab6bSSebastien Roy 	    zone_check_datalink(&zoneid, hwgrpp->dih_linkid) != 0)
4392b24ab6bSSebastien Roy 		return (ENOENT);
440da14cebeSEric Cheng 
441da14cebeSEric Cheng 	hwgrpp->dih_n_groups = 0;
442da14cebeSEric Cheng 	err = mac_open_by_linkid(hwgrpp->dih_linkid, &mh);
443da14cebeSEric Cheng 	if (err != 0)
444da14cebeSEric Cheng 		goto done;
445da14cebeSEric Cheng 
446da14cebeSEric Cheng 	hip = (dld_hwgrpinfo_t *)
447da14cebeSEric Cheng 	    ((uchar_t *)arg + sizeof (dld_ioc_hwgrpget_t));
448da14cebeSEric Cheng 	bytes_left = hwgrpp->dih_size;
449*0dc2366fSVenugopal Iyer 
450*0dc2366fSVenugopal Iyer 	rgrpnum = mac_hwgrp_num(mh, MAC_RING_TYPE_RX);
451*0dc2366fSVenugopal Iyer 	/* display the default group information first */
452*0dc2366fSVenugopal Iyer 	if (rgrpnum > 0) {
453*0dc2366fSVenugopal Iyer 		if (sizeof (dld_hwgrpinfo_t) > bytes_left) {
454*0dc2366fSVenugopal Iyer 			err = ENOSPC;
455*0dc2366fSVenugopal Iyer 			goto done;
456*0dc2366fSVenugopal Iyer 		}
457*0dc2366fSVenugopal Iyer 
458*0dc2366fSVenugopal Iyer 		bzero(&hwgrp, sizeof (hwgrp));
459*0dc2366fSVenugopal Iyer 		bcopy(mac_name(mh), hwgrp.dhi_link_name,
460*0dc2366fSVenugopal Iyer 		    sizeof (hwgrp.dhi_link_name));
461*0dc2366fSVenugopal Iyer 		mac_get_hwrxgrp_info(mh, 0, &hwgrp.dhi_grp_num,
462*0dc2366fSVenugopal Iyer 		    &hwgrp.dhi_n_rings, hwgrp.dhi_rings, &hwgrp.dhi_grp_type,
463*0dc2366fSVenugopal Iyer 		    &hwgrp.dhi_n_clnts, hwgrp.dhi_clnts);
464*0dc2366fSVenugopal Iyer 		if (hwgrp.dhi_n_rings != 0) {
465*0dc2366fSVenugopal Iyer 			if (copyout(&hwgrp, hip, sizeof (hwgrp)) != 0) {
466*0dc2366fSVenugopal Iyer 				err = EFAULT;
467*0dc2366fSVenugopal Iyer 				goto done;
468*0dc2366fSVenugopal Iyer 			}
469*0dc2366fSVenugopal Iyer 		}
470*0dc2366fSVenugopal Iyer 		hip++;
471*0dc2366fSVenugopal Iyer 		totgrps++;
472*0dc2366fSVenugopal Iyer 		bytes_left -= sizeof (dld_hwgrpinfo_t);
473*0dc2366fSVenugopal Iyer 	}
474*0dc2366fSVenugopal Iyer 
475*0dc2366fSVenugopal Iyer 	tgrpnum = mac_hwgrp_num(mh, MAC_RING_TYPE_TX);
476*0dc2366fSVenugopal Iyer 	/* display the default group information first */
477*0dc2366fSVenugopal Iyer 	if (tgrpnum > 0) {
478*0dc2366fSVenugopal Iyer 		if (sizeof (dld_hwgrpinfo_t) > bytes_left) {
479*0dc2366fSVenugopal Iyer 			err = ENOSPC;
480*0dc2366fSVenugopal Iyer 			goto done;
481*0dc2366fSVenugopal Iyer 		}
482*0dc2366fSVenugopal Iyer 
483*0dc2366fSVenugopal Iyer 		bzero(&hwgrp, sizeof (hwgrp));
484*0dc2366fSVenugopal Iyer 		bcopy(mac_name(mh), hwgrp.dhi_link_name,
485*0dc2366fSVenugopal Iyer 		    sizeof (hwgrp.dhi_link_name));
486*0dc2366fSVenugopal Iyer 		mac_get_hwtxgrp_info(mh, tgrpnum - 1, &hwgrp.dhi_grp_num,
487*0dc2366fSVenugopal Iyer 		    &hwgrp.dhi_n_rings, hwgrp.dhi_rings, &hwgrp.dhi_grp_type,
488*0dc2366fSVenugopal Iyer 		    &hwgrp.dhi_n_clnts, hwgrp.dhi_clnts);
489*0dc2366fSVenugopal Iyer 		if (hwgrp.dhi_n_rings != 0) {
490*0dc2366fSVenugopal Iyer 			if (copyout(&hwgrp, hip, sizeof (hwgrp)) != 0) {
491*0dc2366fSVenugopal Iyer 				err = EFAULT;
492*0dc2366fSVenugopal Iyer 				goto done;
493*0dc2366fSVenugopal Iyer 			}
494*0dc2366fSVenugopal Iyer 		}
495*0dc2366fSVenugopal Iyer 		hip++;
496*0dc2366fSVenugopal Iyer 		totgrps++;
497*0dc2366fSVenugopal Iyer 		bytes_left -= sizeof (dld_hwgrpinfo_t);
498*0dc2366fSVenugopal Iyer 	}
499*0dc2366fSVenugopal Iyer 
500*0dc2366fSVenugopal Iyer 	/* Rest of the rx groups */
501*0dc2366fSVenugopal Iyer 	for (i = 1; i < rgrpnum; i++) {
502*0dc2366fSVenugopal Iyer 		if (sizeof (dld_hwgrpinfo_t) > bytes_left) {
503*0dc2366fSVenugopal Iyer 			err = ENOSPC;
504*0dc2366fSVenugopal Iyer 			goto done;
505*0dc2366fSVenugopal Iyer 		}
506*0dc2366fSVenugopal Iyer 
507*0dc2366fSVenugopal Iyer 		bzero(&hwgrp, sizeof (hwgrp));
508*0dc2366fSVenugopal Iyer 		bcopy(mac_name(mh), hwgrp.dhi_link_name,
509*0dc2366fSVenugopal Iyer 		    sizeof (hwgrp.dhi_link_name));
510*0dc2366fSVenugopal Iyer 		mac_get_hwrxgrp_info(mh, i, &hwgrp.dhi_grp_num,
511*0dc2366fSVenugopal Iyer 		    &hwgrp.dhi_n_rings, hwgrp.dhi_rings, &hwgrp.dhi_grp_type,
512*0dc2366fSVenugopal Iyer 		    &hwgrp.dhi_n_clnts, hwgrp.dhi_clnts);
513*0dc2366fSVenugopal Iyer 		if (hwgrp.dhi_n_rings == 0)
514*0dc2366fSVenugopal Iyer 			continue;
515*0dc2366fSVenugopal Iyer 		if (copyout(&hwgrp, hip, sizeof (hwgrp)) != 0) {
516*0dc2366fSVenugopal Iyer 			err = EFAULT;
517*0dc2366fSVenugopal Iyer 			goto done;
518*0dc2366fSVenugopal Iyer 		}
519*0dc2366fSVenugopal Iyer 
520*0dc2366fSVenugopal Iyer 		hip++;
521*0dc2366fSVenugopal Iyer 		totgrps++;
522*0dc2366fSVenugopal Iyer 		bytes_left -= sizeof (dld_hwgrpinfo_t);
523*0dc2366fSVenugopal Iyer 	}
524*0dc2366fSVenugopal Iyer 
525*0dc2366fSVenugopal Iyer 	/* Rest of the tx group */
526*0dc2366fSVenugopal Iyer 	tgrpnum = mac_hwgrp_num(mh, MAC_RING_TYPE_TX);
527*0dc2366fSVenugopal Iyer 	for (i = 0; i < tgrpnum - 1; i++) {
528da14cebeSEric Cheng 		if (sizeof (dld_hwgrpinfo_t) > bytes_left) {
529da14cebeSEric Cheng 			err = ENOSPC;
530da14cebeSEric Cheng 			goto done;
531da14cebeSEric Cheng 		}
532da14cebeSEric Cheng 
533da14cebeSEric Cheng 		bzero(&hwgrp, sizeof (hwgrp));
534da14cebeSEric Cheng 		bcopy(mac_name(mh), hwgrp.dhi_link_name,
535da14cebeSEric Cheng 		    sizeof (hwgrp.dhi_link_name));
536*0dc2366fSVenugopal Iyer 		mac_get_hwtxgrp_info(mh, i, &hwgrp.dhi_grp_num,
537*0dc2366fSVenugopal Iyer 		    &hwgrp.dhi_n_rings, hwgrp.dhi_rings, &hwgrp.dhi_grp_type,
538da14cebeSEric Cheng 		    &hwgrp.dhi_n_clnts, hwgrp.dhi_clnts);
539*0dc2366fSVenugopal Iyer 		if (hwgrp.dhi_n_rings == 0)
540*0dc2366fSVenugopal Iyer 			continue;
541da14cebeSEric Cheng 		if (copyout(&hwgrp, hip, sizeof (hwgrp)) != 0) {
542da14cebeSEric Cheng 			err = EFAULT;
543da14cebeSEric Cheng 			goto done;
544da14cebeSEric Cheng 		}
545da14cebeSEric Cheng 
546da14cebeSEric Cheng 		hip++;
547*0dc2366fSVenugopal Iyer 		totgrps++;
548da14cebeSEric Cheng 		bytes_left -= sizeof (dld_hwgrpinfo_t);
549da14cebeSEric Cheng 	}
550da14cebeSEric Cheng 
551da14cebeSEric Cheng done:
552da14cebeSEric Cheng 	if (mh != NULL)
553da14cebeSEric Cheng 		dld_mac_close(mh);
554da14cebeSEric Cheng 	if (err == 0)
555*0dc2366fSVenugopal Iyer 		hwgrpp->dih_n_groups = totgrps;
556da14cebeSEric Cheng 	return (err);
557da14cebeSEric Cheng }
558da14cebeSEric Cheng 
559da14cebeSEric Cheng /* ARGSUSED */
560da14cebeSEric Cheng static int
561da14cebeSEric Cheng drv_ioc_macaddrget(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
562da14cebeSEric Cheng {
563da14cebeSEric Cheng 	dld_ioc_macaddrget_t	*magp = karg;
564da14cebeSEric Cheng 	dld_macaddrinfo_t	mai, *maip;
565da14cebeSEric Cheng 	mac_handle_t		mh = NULL;
566da14cebeSEric Cheng 	int			i, err;
567da14cebeSEric Cheng 	uint_t			bytes_left;
568da14cebeSEric Cheng 	boolean_t		is_used;
5692b24ab6bSSebastien Roy 	zoneid_t		zoneid = crgetzoneid(cred);
5702b24ab6bSSebastien Roy 
5712b24ab6bSSebastien Roy 	if (zoneid != GLOBAL_ZONEID &&
5722b24ab6bSSebastien Roy 	    zone_check_datalink(&zoneid, magp->dig_linkid) != 0)
5732b24ab6bSSebastien Roy 		return (ENOENT);
574da14cebeSEric Cheng 
575da14cebeSEric Cheng 	magp->dig_count = 0;
576da14cebeSEric Cheng 	err = mac_open_by_linkid(magp->dig_linkid, &mh);
577da14cebeSEric Cheng 	if (err != 0)
578da14cebeSEric Cheng 		goto done;
579da14cebeSEric Cheng 
580da14cebeSEric Cheng 	maip = (dld_macaddrinfo_t *)
581da14cebeSEric Cheng 	    ((uchar_t *)arg + sizeof (dld_ioc_macaddrget_t));
582da14cebeSEric Cheng 	bytes_left = magp->dig_size;
583da14cebeSEric Cheng 
584da14cebeSEric Cheng 	for (i = 0; i < mac_addr_factory_num(mh) + 1; i++) {
585da14cebeSEric Cheng 		if (sizeof (dld_macaddrinfo_t) > bytes_left) {
586da14cebeSEric Cheng 			err = ENOSPC;
587da14cebeSEric Cheng 			goto done;
588da14cebeSEric Cheng 		}
589da14cebeSEric Cheng 
590da14cebeSEric Cheng 		bzero(&mai, sizeof (mai));
591da14cebeSEric Cheng 
592da14cebeSEric Cheng 		if (i == 0) {
593da14cebeSEric Cheng 			/* primary MAC address */
594da14cebeSEric Cheng 			mac_unicast_primary_get(mh, mai.dmi_addr);
595da14cebeSEric Cheng 			mai.dmi_addrlen = mac_addr_len(mh);
596da14cebeSEric Cheng 			mac_unicast_primary_info(mh, mai.dmi_client_name,
597da14cebeSEric Cheng 			    &is_used);
598da14cebeSEric Cheng 		} else {
599da14cebeSEric Cheng 			/* factory MAC address slot */
600da14cebeSEric Cheng 			mac_addr_factory_value(mh, i, mai.dmi_addr,
601da14cebeSEric Cheng 			    &mai.dmi_addrlen, mai.dmi_client_name, &is_used);
602da14cebeSEric Cheng 		}
603da14cebeSEric Cheng 
604da14cebeSEric Cheng 		mai.dmi_slot = i;
605da14cebeSEric Cheng 		if (is_used)
606da14cebeSEric Cheng 			mai.dmi_flags |= DLDIOCMACADDR_USED;
607da14cebeSEric Cheng 
608da14cebeSEric Cheng 		if (copyout(&mai, maip, sizeof (mai)) != 0) {
609da14cebeSEric Cheng 			err = EFAULT;
610da14cebeSEric Cheng 			goto done;
611da14cebeSEric Cheng 		}
612da14cebeSEric Cheng 
613da14cebeSEric Cheng 		maip++;
614da14cebeSEric Cheng 		bytes_left -= sizeof (dld_macaddrinfo_t);
615da14cebeSEric Cheng 	}
616da14cebeSEric Cheng 
617da14cebeSEric Cheng done:
618da14cebeSEric Cheng 	if (mh != NULL)
619da14cebeSEric Cheng 		dld_mac_close(mh);
620da14cebeSEric Cheng 	if (err == 0)
621da14cebeSEric Cheng 		magp->dig_count = mac_addr_factory_num(mh) + 1;
622da14cebeSEric Cheng 	return (err);
623da14cebeSEric Cheng }
624da14cebeSEric Cheng 
625e7801d59Ssowmini /*
626*0dc2366fSVenugopal Iyer  * DLDIOC_SET/GETMACPROP
627e7801d59Ssowmini  */
628eae72b5bSSebastien Roy static int
629da14cebeSEric Cheng drv_ioc_prop_common(dld_ioc_macprop_t *prop, intptr_t arg, boolean_t set,
6302b24ab6bSSebastien Roy     cred_t *cred, int mode)
631e7801d59Ssowmini {
632da14cebeSEric Cheng 	int			err = EINVAL;
633da14cebeSEric Cheng 	dls_dl_handle_t 	dlh = NULL;
634da14cebeSEric Cheng 	dls_link_t		*dlp = NULL;
635da14cebeSEric Cheng 	mac_perim_handle_t	mph = NULL;
636da14cebeSEric Cheng 	dld_ioc_macprop_t	*kprop;
637da14cebeSEric Cheng 	datalink_id_t		linkid;
6382b24ab6bSSebastien Roy 	datalink_class_t	class;
6392b24ab6bSSebastien Roy 	zoneid_t		zoneid = crgetzoneid(cred);
640da14cebeSEric Cheng 	uint_t			dsize;
641da14cebeSEric Cheng 
642eae72b5bSSebastien Roy 	/*
643da14cebeSEric Cheng 	 * We only use pr_valsize from prop, as the caller only did a
644eae72b5bSSebastien Roy 	 * copyin() for sizeof (dld_ioc_prop_t), which doesn't cover
645eae72b5bSSebastien Roy 	 * the property data.  We copyin the full dld_ioc_prop_t
646da14cebeSEric Cheng 	 * including the data into kprop down below.
647eae72b5bSSebastien Roy 	 */
648da14cebeSEric Cheng 	dsize = sizeof (dld_ioc_macprop_t) + prop->pr_valsize - 1;
649da14cebeSEric Cheng 	if (dsize < prop->pr_valsize)
650eae72b5bSSebastien Roy 		return (EINVAL);
651eae72b5bSSebastien Roy 
652eae72b5bSSebastien Roy 	/*
653eae72b5bSSebastien Roy 	 * The property data is variable size, so we need to allocate
654eae72b5bSSebastien Roy 	 * a buffer for kernel use as this data was not part of the
655da14cebeSEric Cheng 	 * prop allocation and copyin() done by the framework.
656eae72b5bSSebastien Roy 	 */
657da14cebeSEric Cheng 	if ((kprop = kmem_alloc(dsize, KM_NOSLEEP)) == NULL)
658eae72b5bSSebastien Roy 		return (ENOMEM);
659da14cebeSEric Cheng 
660da14cebeSEric Cheng 	if (ddi_copyin((void *)arg, kprop, dsize, mode) != 0) {
661eae72b5bSSebastien Roy 		err = EFAULT;
662e7801d59Ssowmini 		goto done;
663eae72b5bSSebastien Roy 	}
664e7801d59Ssowmini 
665da14cebeSEric Cheng 	linkid = kprop->pr_linkid;
666da14cebeSEric Cheng 
6672b24ab6bSSebastien Roy 	if (set) {
6682b24ab6bSSebastien Roy 		if ((err = dls_mgmt_get_linkinfo(linkid, NULL, &class, NULL,
6692b24ab6bSSebastien Roy 		    NULL)) != 0 || (err = drv_ioc_checkprivs(class, cred)) != 0)
6702b24ab6bSSebastien Roy 			goto done;
671da14cebeSEric Cheng 	}
672e7801d59Ssowmini 
6732b24ab6bSSebastien Roy 	if ((err = dls_devnet_hold_tmp(linkid, &dlh)) != 0)
6742b24ab6bSSebastien Roy 		goto done;
6752b24ab6bSSebastien Roy 	if ((err = mac_perim_enter_by_macname(dls_devnet_mac(dlh), &mph)) != 0)
6762b24ab6bSSebastien Roy 		goto done;
677e75f0919SSebastien Roy 	if ((err = dls_link_hold(dls_devnet_mac(dlh), &dlp)) != 0)
678e75f0919SSebastien Roy 		goto done;
679e75f0919SSebastien Roy 
6802b24ab6bSSebastien Roy 	/*
6812b24ab6bSSebastien Roy 	 * Don't allow a process to get or set properties of a link if that
6822b24ab6bSSebastien Roy 	 * link doesn't belong to that zone.
6832b24ab6bSSebastien Roy 	 */
6842b24ab6bSSebastien Roy 	if (zoneid != dls_devnet_getownerzid(dlh)) {
6852b24ab6bSSebastien Roy 		err = ENOENT;
6862b24ab6bSSebastien Roy 		goto done;
6872b24ab6bSSebastien Roy 	}
6882b24ab6bSSebastien Roy 
689*0dc2366fSVenugopal Iyer 	if (!mac_prop_check_size(kprop->pr_num, kprop->pr_valsize,
690*0dc2366fSVenugopal Iyer 	    kprop->pr_flags & DLD_PROP_POSSIBLE)) {
691*0dc2366fSVenugopal Iyer 		err = ENOBUFS;
692*0dc2366fSVenugopal Iyer 		goto done;
693*0dc2366fSVenugopal Iyer 	}
694*0dc2366fSVenugopal Iyer 
695da14cebeSEric Cheng 	switch (kprop->pr_num) {
696e75f0919SSebastien Roy 	case MAC_PROP_ZONE:
6973bc21d0aSAruna Ramakrishna - Sun Microsystems 		if (set) {
698e75f0919SSebastien Roy 			dld_ioc_zid_t *dzp = (dld_ioc_zid_t *)kprop->pr_val;
699da14cebeSEric Cheng 
7002b24ab6bSSebastien Roy 			if (zoneid != GLOBAL_ZONEID) {
7012b24ab6bSSebastien Roy 				err = EACCES;
7022b24ab6bSSebastien Roy 				goto done;
7032b24ab6bSSebastien Roy 			}
7042b24ab6bSSebastien Roy 			err = dls_devnet_setzid(dlh, dzp->diz_zid);
7053bc21d0aSAruna Ramakrishna - Sun Microsystems 		} else {
706da14cebeSEric Cheng 			kprop->pr_perm_flags = MAC_PROP_PERM_RW;
7072b24ab6bSSebastien Roy 			(*(zoneid_t *)kprop->pr_val) = dls_devnet_getzid(dlh);
7083bc21d0aSAruna Ramakrishna - Sun Microsystems 		}
709e75f0919SSebastien Roy 		break;
710da14cebeSEric Cheng 	case MAC_PROP_AUTOPUSH: {
711e75f0919SSebastien Roy 		struct dlautopush *dlap = (struct dlautopush *)kprop->pr_val;
712da14cebeSEric Cheng 
7133bc21d0aSAruna Ramakrishna - Sun Microsystems 		if (set) {
714e75f0919SSebastien Roy 			if (kprop->pr_valsize != 0)
7153bc21d0aSAruna Ramakrishna - Sun Microsystems 				err = drv_ioc_setap(linkid, dlap);
716e75f0919SSebastien Roy 			else
7173bc21d0aSAruna Ramakrishna - Sun Microsystems 				err = drv_ioc_clrap(linkid);
7183bc21d0aSAruna Ramakrishna - Sun Microsystems 		} else {
719*0dc2366fSVenugopal Iyer 			if (kprop->pr_valsize == 0)
720*0dc2366fSVenugopal Iyer 				return (ENOBUFS);
721*0dc2366fSVenugopal Iyer 
722da14cebeSEric Cheng 			kprop->pr_perm_flags = MAC_PROP_PERM_RW;
7233bc21d0aSAruna Ramakrishna - Sun Microsystems 			err = drv_ioc_getap(linkid, dlap);
7243bc21d0aSAruna Ramakrishna - Sun Microsystems 		}
7253bc21d0aSAruna Ramakrishna - Sun Microsystems 		break;
7263bc21d0aSAruna Ramakrishna - Sun Microsystems 	}
727e75f0919SSebastien Roy 	case MAC_PROP_TAGMODE:
728e75f0919SSebastien Roy 		if (set) {
729e75f0919SSebastien Roy 			link_tagmode_t mode = *(link_tagmode_t *)kprop->pr_val;
7303bc21d0aSAruna Ramakrishna - Sun Microsystems 
731e75f0919SSebastien Roy 			if (mode != LINK_TAGMODE_VLANONLY &&
732e75f0919SSebastien Roy 			    mode != LINK_TAGMODE_NORMAL) {
733e75f0919SSebastien Roy 				err = EINVAL;
734e75f0919SSebastien Roy 			} else {
735e75f0919SSebastien Roy 				dlp->dl_tagmode = mode;
736e75f0919SSebastien Roy 				err = 0;
737e75f0919SSebastien Roy 			}
738e75f0919SSebastien Roy 		} else {
739e75f0919SSebastien Roy 			*(link_tagmode_t *)kprop->pr_val = dlp->dl_tagmode;
740e75f0919SSebastien Roy 			kprop->pr_perm_flags = MAC_PROP_PERM_RW;
741e75f0919SSebastien Roy 			err = 0;
742e75f0919SSebastien Roy 		}
743e75f0919SSebastien Roy 		break;
744*0dc2366fSVenugopal Iyer 	default: {
745*0dc2366fSVenugopal Iyer 		mac_propval_range_t range, *rangep = NULL;
746*0dc2366fSVenugopal Iyer 		void *default_val = NULL;
747*0dc2366fSVenugopal Iyer 		uint_t default_size = 0;
748*0dc2366fSVenugopal Iyer 		void *val = kprop->pr_val;
749*0dc2366fSVenugopal Iyer 		uint_t val_size = kprop->pr_valsize;
750*0dc2366fSVenugopal Iyer 
751*0dc2366fSVenugopal Iyer 		/* set a property value */
752e75f0919SSebastien Roy 		if (set) {
753*0dc2366fSVenugopal Iyer 			err = mac_set_prop(dlp->dl_mh, kprop->pr_num,
754*0dc2366fSVenugopal Iyer 			    kprop->pr_name, kprop->pr_val, kprop->pr_valsize);
755*0dc2366fSVenugopal Iyer 			break;
756*0dc2366fSVenugopal Iyer 		}
757*0dc2366fSVenugopal Iyer 
758*0dc2366fSVenugopal Iyer 		/*
759*0dc2366fSVenugopal Iyer 		 * Get the property value, default, or possible value
760*0dc2366fSVenugopal Iyer 		 * depending on flags passed from the user.
761*0dc2366fSVenugopal Iyer 		 */
762*0dc2366fSVenugopal Iyer 
763*0dc2366fSVenugopal Iyer 		/* a property has RW permissions by default */
764*0dc2366fSVenugopal Iyer 		kprop->pr_perm_flags = MAC_PROP_PERM_RW;
765*0dc2366fSVenugopal Iyer 
766*0dc2366fSVenugopal Iyer 		if (kprop->pr_flags & DLD_PROP_POSSIBLE) {
767*0dc2366fSVenugopal Iyer 			rangep = &range;
768*0dc2366fSVenugopal Iyer 		} else if (kprop->pr_flags & DLD_PROP_DEFAULT) {
769*0dc2366fSVenugopal Iyer 			default_val = val;
770*0dc2366fSVenugopal Iyer 			default_size = val_size;
771e75f0919SSebastien Roy 		}
772*0dc2366fSVenugopal Iyer 
773*0dc2366fSVenugopal Iyer 		/*
774*0dc2366fSVenugopal Iyer 		 * Always return the permissions, and optionally return
775*0dc2366fSVenugopal Iyer 		 * the default value or possible values range.
776*0dc2366fSVenugopal Iyer 		 */
777*0dc2366fSVenugopal Iyer 		mac_prop_info(dlp->dl_mh, kprop->pr_num, kprop->pr_name,
778*0dc2366fSVenugopal Iyer 		    default_val, default_size, rangep, &kprop->pr_perm_flags);
779*0dc2366fSVenugopal Iyer 		err = 0;
780*0dc2366fSVenugopal Iyer 
781*0dc2366fSVenugopal Iyer 		if (default_val == NULL && rangep == NULL) {
782*0dc2366fSVenugopal Iyer 			err = mac_get_prop(dlp->dl_mh, kprop->pr_num,
783*0dc2366fSVenugopal Iyer 			    kprop->pr_name, kprop->pr_val, kprop->pr_valsize);
784*0dc2366fSVenugopal Iyer 		}
785*0dc2366fSVenugopal Iyer 
786*0dc2366fSVenugopal Iyer 		if (rangep != NULL)
787*0dc2366fSVenugopal Iyer 			bcopy(rangep, val, sizeof (range));
788*0dc2366fSVenugopal Iyer 	}
789eae72b5bSSebastien Roy 	}
790e7801d59Ssowmini 
791e7801d59Ssowmini done:
792f0f2c3a5SGirish Moodalbail 	if (!set && ddi_copyout(kprop, (void *)arg, dsize, mode) != 0)
793eae72b5bSSebastien Roy 		err = EFAULT;
794e7801d59Ssowmini 
795da14cebeSEric Cheng 	if (dlp != NULL)
796da14cebeSEric Cheng 		dls_link_rele(dlp);
797*0dc2366fSVenugopal Iyer 
798da14cebeSEric Cheng 	if (mph != NULL) {
799da14cebeSEric Cheng 		int32_t	cpuid;
800da14cebeSEric Cheng 		void	*mdip = NULL;
801e7801d59Ssowmini 
802da14cebeSEric Cheng 		if (dlp != NULL && set && err == 0) {
803da14cebeSEric Cheng 			cpuid = mac_client_intr_cpu(dlp->dl_mch);
804da14cebeSEric Cheng 			mdip = mac_get_devinfo(dlp->dl_mh);
805da14cebeSEric Cheng 		}
806d62bc4baSyz 
807da14cebeSEric Cheng 		mac_perim_exit(mph);
808da14cebeSEric Cheng 
809*0dc2366fSVenugopal Iyer 		if (mdip != NULL && cpuid != -1)
810da14cebeSEric Cheng 			mac_client_set_intr_cpu(mdip, dlp->dl_mch, cpuid);
811da14cebeSEric Cheng 	}
812*0dc2366fSVenugopal Iyer 
813da14cebeSEric Cheng 	if (dlh != NULL)
814da14cebeSEric Cheng 		dls_devnet_rele_tmp(dlh);
815da14cebeSEric Cheng 
816da14cebeSEric Cheng 	if (kprop != NULL)
817da14cebeSEric Cheng 		kmem_free(kprop, dsize);
818da14cebeSEric Cheng 	return (err);
819210db224Sericheng }
820210db224Sericheng 
821eae72b5bSSebastien Roy /* ARGSUSED */
822eae72b5bSSebastien Roy static int
823da14cebeSEric Cheng drv_ioc_setprop(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
8247c478bd9Sstevel@tonic-gate {
8252b24ab6bSSebastien Roy 	return (drv_ioc_prop_common(karg, arg, B_TRUE, cred, mode));
826d62bc4baSyz }
827d62bc4baSyz 
828eae72b5bSSebastien Roy /* ARGSUSED */
829eae72b5bSSebastien Roy static int
830da14cebeSEric Cheng drv_ioc_getprop(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
831d62bc4baSyz {
8322b24ab6bSSebastien Roy 	return (drv_ioc_prop_common(karg, arg, B_FALSE, cred, mode));
833210db224Sericheng }
834210db224Sericheng 
835f4b3ec61Sdh /*
836d62bc4baSyz  * DLDIOC_RENAME.
837d62bc4baSyz  *
838d62bc4baSyz  * This function handles two cases of link renaming. See more in comments above
839d62bc4baSyz  * dls_datalink_rename().
840f4b3ec61Sdh  */
841eae72b5bSSebastien Roy /* ARGSUSED */
842eae72b5bSSebastien Roy static int
843da14cebeSEric Cheng drv_ioc_rename(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
844f4b3ec61Sdh {
845eae72b5bSSebastien Roy 	dld_ioc_rename_t	*dir = karg;
846d62bc4baSyz 	mod_hash_key_t		key;
847d62bc4baSyz 	mod_hash_val_t		val;
8482b24ab6bSSebastien Roy 	zoneid_t		zoneid = crgetzoneid(cred);
8492b24ab6bSSebastien Roy 	datalink_class_t	class;
850d62bc4baSyz 	int			err;
851d62bc4baSyz 
8522b24ab6bSSebastien Roy 	if (zoneid != GLOBAL_ZONEID &&
8532b24ab6bSSebastien Roy 	    (zone_check_datalink(&zoneid, dir->dir_linkid1) != 0 ||
8542b24ab6bSSebastien Roy 	    dir->dir_linkid2 != DATALINK_INVALID_LINKID &&
8552b24ab6bSSebastien Roy 	    zone_check_datalink(&zoneid, dir->dir_linkid2) != 0))
8562b24ab6bSSebastien Roy 		return (ENOENT);
8572b24ab6bSSebastien Roy 
8582b24ab6bSSebastien Roy 	if ((err = dls_mgmt_get_linkinfo(dir->dir_linkid1, NULL, &class, NULL,
8592b24ab6bSSebastien Roy 	    NULL)) != 0)
8602b24ab6bSSebastien Roy 		return (err);
8612b24ab6bSSebastien Roy 
8622b24ab6bSSebastien Roy 	if ((err = drv_ioc_checkprivs(class, cred)) != 0)
8632b24ab6bSSebastien Roy 		return (err);
8642b24ab6bSSebastien Roy 
865d62bc4baSyz 	if ((err = dls_devnet_rename(dir->dir_linkid1, dir->dir_linkid2,
866eae72b5bSSebastien Roy 	    dir->dir_link)) != 0)
867eae72b5bSSebastien Roy 		return (err);
868d62bc4baSyz 
869d62bc4baSyz 	if (dir->dir_linkid2 == DATALINK_INVALID_LINKID)
870eae72b5bSSebastien Roy 		return (0);
871d62bc4baSyz 
872d62bc4baSyz 	/*
873d62bc4baSyz 	 * if dir_linkid2 is not DATALINK_INVALID_LINKID, it means this
874d62bc4baSyz 	 * renaming request is to rename a valid physical link (dir_linkid1)
875d62bc4baSyz 	 * to a "removed" physical link (dir_linkid2, which is removed by DR
876d62bc4baSyz 	 * or during system shutdown). In this case, the link (specified by
877d62bc4baSyz 	 * dir_linkid1) would inherit all the configuration of dir_linkid2,
878d62bc4baSyz 	 * and dir_linkid1 and its configuration would be lost.
879d62bc4baSyz 	 *
880d62bc4baSyz 	 * Remove per-link autopush configuration of dir_linkid1 in this case.
881d62bc4baSyz 	 */
882d62bc4baSyz 	key = (mod_hash_key_t)(uintptr_t)dir->dir_linkid1;
883d62bc4baSyz 	rw_enter(&dld_ap_hash_lock, RW_WRITER);
884d62bc4baSyz 	if (mod_hash_find(dld_ap_hashp, key, &val) != 0) {
885d62bc4baSyz 		rw_exit(&dld_ap_hash_lock);
886eae72b5bSSebastien Roy 		return (0);
887d62bc4baSyz 	}
888d62bc4baSyz 
889d62bc4baSyz 	VERIFY(mod_hash_remove(dld_ap_hashp, key, &val) == 0);
890d62bc4baSyz 	kmem_free(val, sizeof (dld_ap_t));
891d62bc4baSyz 	rw_exit(&dld_ap_hash_lock);
892eae72b5bSSebastien Roy 	return (0);
893d62bc4baSyz }
894d62bc4baSyz 
8953bc21d0aSAruna Ramakrishna - Sun Microsystems static int
8963bc21d0aSAruna Ramakrishna - Sun Microsystems drv_ioc_setap(datalink_id_t linkid, struct dlautopush *dlap)
897d62bc4baSyz {
898d62bc4baSyz 	dld_ap_t	*dap;
899eae72b5bSSebastien Roy 	int		i;
900d62bc4baSyz 	mod_hash_key_t	key;
901d62bc4baSyz 
902eae72b5bSSebastien Roy 	if (dlap->dap_npush == 0 || dlap->dap_npush > MAXAPUSH)
903eae72b5bSSebastien Roy 		return (EINVAL);
904f4b3ec61Sdh 
905789e0dbbSdh 	/*
906d62bc4baSyz 	 * Validate that the specified list of modules exist.
907789e0dbbSdh 	 */
9083bc21d0aSAruna Ramakrishna - Sun Microsystems 	for (i = 0; i < dlap->dap_npush; i++) {
909eae72b5bSSebastien Roy 		if (fmodsw_find(dlap->dap_aplist[i], FMODSW_LOAD) == NULL)
910eae72b5bSSebastien Roy 			return (EINVAL);
911d62bc4baSyz 	}
912d62bc4baSyz 
913eae72b5bSSebastien Roy 
9143bc21d0aSAruna Ramakrishna - Sun Microsystems 	key = (mod_hash_key_t)(uintptr_t)linkid;
915789e0dbbSdh 
916d62bc4baSyz 	rw_enter(&dld_ap_hash_lock, RW_WRITER);
917d62bc4baSyz 	if (mod_hash_find(dld_ap_hashp, key, (mod_hash_val_t *)&dap) != 0) {
918d62bc4baSyz 		dap = kmem_zalloc(sizeof (dld_ap_t), KM_NOSLEEP);
919d62bc4baSyz 		if (dap == NULL) {
920d62bc4baSyz 			rw_exit(&dld_ap_hash_lock);
921eae72b5bSSebastien Roy 			return (ENOMEM);
922d62bc4baSyz 		}
923d62bc4baSyz 
9243bc21d0aSAruna Ramakrishna - Sun Microsystems 		dap->da_linkid = linkid;
925eae72b5bSSebastien Roy 		VERIFY(mod_hash_insert(dld_ap_hashp, key,
926eae72b5bSSebastien Roy 		    (mod_hash_val_t)dap) == 0);
927f4b3ec61Sdh 	}
928f4b3ec61Sdh 
929d62bc4baSyz 	/*
930d62bc4baSyz 	 * Update the configuration.
931d62bc4baSyz 	 */
9323bc21d0aSAruna Ramakrishna - Sun Microsystems 	dap->da_anchor = dlap->dap_anchor;
9333bc21d0aSAruna Ramakrishna - Sun Microsystems 	dap->da_npush = dlap->dap_npush;
9343bc21d0aSAruna Ramakrishna - Sun Microsystems 	for (i = 0; i < dlap->dap_npush; i++) {
9353bc21d0aSAruna Ramakrishna - Sun Microsystems 		(void) strlcpy(dap->da_aplist[i], dlap->dap_aplist[i],
936d62bc4baSyz 		    FMNAMESZ + 1);
937d62bc4baSyz 	}
938d62bc4baSyz 	rw_exit(&dld_ap_hash_lock);
939789e0dbbSdh 
9403bc21d0aSAruna Ramakrishna - Sun Microsystems 	return (0);
941d62bc4baSyz }
942d62bc4baSyz 
9433bc21d0aSAruna Ramakrishna - Sun Microsystems static int
9443bc21d0aSAruna Ramakrishna - Sun Microsystems drv_ioc_getap(datalink_id_t linkid, struct dlautopush *dlap)
945d62bc4baSyz {
946d62bc4baSyz 	dld_ap_t	*dap;
947eae72b5bSSebastien Roy 	int		i;
948d62bc4baSyz 
949d62bc4baSyz 	rw_enter(&dld_ap_hash_lock, RW_READER);
950d62bc4baSyz 	if (mod_hash_find(dld_ap_hashp,
9513bc21d0aSAruna Ramakrishna - Sun Microsystems 	    (mod_hash_key_t)(uintptr_t)linkid,
952d62bc4baSyz 	    (mod_hash_val_t *)&dap) != 0) {
953d62bc4baSyz 		rw_exit(&dld_ap_hash_lock);
954*0dc2366fSVenugopal Iyer 		dlap->dap_npush = 0;
955*0dc2366fSVenugopal Iyer 		return (0);
956f595a68aSyz 	}
957d62bc4baSyz 
958d62bc4baSyz 	/*
959d62bc4baSyz 	 * Retrieve the configuration.
960d62bc4baSyz 	 */
9613bc21d0aSAruna Ramakrishna - Sun Microsystems 	dlap->dap_anchor = dap->da_anchor;
9623bc21d0aSAruna Ramakrishna - Sun Microsystems 	dlap->dap_npush = dap->da_npush;
963d62bc4baSyz 	for (i = 0; i < dap->da_npush; i++) {
9643bc21d0aSAruna Ramakrishna - Sun Microsystems 		(void) strlcpy(dlap->dap_aplist[i], dap->da_aplist[i],
965d62bc4baSyz 		    FMNAMESZ + 1);
966d62bc4baSyz 	}
967d62bc4baSyz 	rw_exit(&dld_ap_hash_lock);
968d62bc4baSyz 
9693bc21d0aSAruna Ramakrishna - Sun Microsystems 	return (0);
970f4b3ec61Sdh }
971f4b3ec61Sdh 
9723bc21d0aSAruna Ramakrishna - Sun Microsystems static int
9733bc21d0aSAruna Ramakrishna - Sun Microsystems drv_ioc_clrap(datalink_id_t linkid)
974f4b3ec61Sdh {
975d62bc4baSyz 	mod_hash_val_t	val;
976d62bc4baSyz 	mod_hash_key_t	key;
977f4b3ec61Sdh 
9783bc21d0aSAruna Ramakrishna - Sun Microsystems 	key = (mod_hash_key_t)(uintptr_t)linkid;
979f4b3ec61Sdh 
980d62bc4baSyz 	rw_enter(&dld_ap_hash_lock, RW_WRITER);
981d62bc4baSyz 	if (mod_hash_find(dld_ap_hashp, key, &val) != 0) {
982d62bc4baSyz 		rw_exit(&dld_ap_hash_lock);
9833bc21d0aSAruna Ramakrishna - Sun Microsystems 		return (0);
984f4b3ec61Sdh 	}
985f4b3ec61Sdh 
986d62bc4baSyz 	VERIFY(mod_hash_remove(dld_ap_hashp, key, &val) == 0);
987d62bc4baSyz 	kmem_free(val, sizeof (dld_ap_t));
988d62bc4baSyz 	rw_exit(&dld_ap_hash_lock);
9893bc21d0aSAruna Ramakrishna - Sun Microsystems 	return (0);
990f4b3ec61Sdh }
991f4b3ec61Sdh 
992f4b3ec61Sdh /*
993d62bc4baSyz  * DLDIOC_DOORSERVER
994f4b3ec61Sdh  */
995eae72b5bSSebastien Roy /* ARGSUSED */
996eae72b5bSSebastien Roy static int
997da14cebeSEric Cheng drv_ioc_doorserver(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
998210db224Sericheng {
999eae72b5bSSebastien Roy 	dld_ioc_door_t	*did = karg;
1000210db224Sericheng 
1001eae72b5bSSebastien Roy 	return (dls_mgmt_door_set(did->did_start_door));
10027c478bd9Sstevel@tonic-gate }
10030ba2cbe9Sxc 
1004da14cebeSEric Cheng /*
1005da14cebeSEric Cheng  * DLDIOC_USAGELOG
1006da14cebeSEric Cheng  */
1007da14cebeSEric Cheng /* ARGSUSED */
1008da14cebeSEric Cheng static int
1009da14cebeSEric Cheng drv_ioc_usagelog(void *karg, intptr_t arg, int mode, cred_t *cred,
1010da14cebeSEric Cheng     int *rvalp)
1011da14cebeSEric Cheng {
1012da14cebeSEric Cheng 	dld_ioc_usagelog_t	*log_info = (dld_ioc_usagelog_t *)karg;
10135d460eafSCathy Zhou 	int			err = 0;
1014da14cebeSEric Cheng 
1015da14cebeSEric Cheng 	if (log_info->ul_type < MAC_LOGTYPE_LINK ||
1016da14cebeSEric Cheng 	    log_info->ul_type > MAC_LOGTYPE_FLOW)
1017da14cebeSEric Cheng 		return (EINVAL);
1018da14cebeSEric Cheng 
10195d460eafSCathy Zhou 	if (log_info->ul_onoff) {
10205d460eafSCathy Zhou 		err = mac_start_logusage(log_info->ul_type,
10215d460eafSCathy Zhou 		    log_info->ul_interval);
10225d460eafSCathy Zhou 	} else {
1023da14cebeSEric Cheng 		mac_stop_logusage(log_info->ul_type);
10245d460eafSCathy Zhou 	}
10255d460eafSCathy Zhou 	return (err);
1026da14cebeSEric Cheng }
1027da14cebeSEric Cheng 
1028da14cebeSEric Cheng /*
1029da14cebeSEric Cheng  * Process a DLDIOC_ADDFLOW request.
1030da14cebeSEric Cheng  */
1031da14cebeSEric Cheng /* ARGSUSED */
1032da14cebeSEric Cheng static int
1033da14cebeSEric Cheng drv_ioc_addflow(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1034da14cebeSEric Cheng {
1035da14cebeSEric Cheng 	dld_ioc_addflow_t	*afp = karg;
1036da14cebeSEric Cheng 
1037da14cebeSEric Cheng 	return (dld_add_flow(afp->af_linkid, afp->af_name,
1038da14cebeSEric Cheng 	    &afp->af_flow_desc, &afp->af_resource_props));
1039da14cebeSEric Cheng }
1040da14cebeSEric Cheng 
1041da14cebeSEric Cheng /*
1042da14cebeSEric Cheng  * Process a DLDIOC_REMOVEFLOW request.
1043da14cebeSEric Cheng  */
1044da14cebeSEric Cheng /* ARGSUSED */
1045da14cebeSEric Cheng static int
1046da14cebeSEric Cheng drv_ioc_removeflow(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1047da14cebeSEric Cheng {
1048da14cebeSEric Cheng 	dld_ioc_removeflow_t	*rfp = karg;
1049da14cebeSEric Cheng 
1050da14cebeSEric Cheng 	return (dld_remove_flow(rfp->rf_name));
1051da14cebeSEric Cheng }
1052da14cebeSEric Cheng 
1053da14cebeSEric Cheng /*
1054da14cebeSEric Cheng  * Process a DLDIOC_MODIFYFLOW request.
1055da14cebeSEric Cheng  */
1056da14cebeSEric Cheng /* ARGSUSED */
1057da14cebeSEric Cheng static int
1058da14cebeSEric Cheng drv_ioc_modifyflow(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1059da14cebeSEric Cheng {
1060da14cebeSEric Cheng 	dld_ioc_modifyflow_t	*mfp = karg;
1061da14cebeSEric Cheng 
1062da14cebeSEric Cheng 	return (dld_modify_flow(mfp->mf_name, &mfp->mf_resource_props));
1063da14cebeSEric Cheng }
1064da14cebeSEric Cheng 
1065da14cebeSEric Cheng /*
1066da14cebeSEric Cheng  * Process a DLDIOC_WALKFLOW request.
1067da14cebeSEric Cheng  */
1068da14cebeSEric Cheng /* ARGSUSED */
1069da14cebeSEric Cheng static int
1070da14cebeSEric Cheng drv_ioc_walkflow(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1071da14cebeSEric Cheng {
1072da14cebeSEric Cheng 	dld_ioc_walkflow_t	*wfp = karg;
1073da14cebeSEric Cheng 
10742b24ab6bSSebastien Roy 	return (dld_walk_flow(wfp, arg, cred));
1075da14cebeSEric Cheng }
1076da14cebeSEric Cheng 
1077d62bc4baSyz /*
1078d62bc4baSyz  * Check for GLDv3 autopush information.  There are three cases:
1079d62bc4baSyz  *
1080d62bc4baSyz  *   1. If devp points to a GLDv3 datalink and it has autopush configuration,
1081d62bc4baSyz  *	fill dlap in with that information and return 0.
1082d62bc4baSyz  *
1083d62bc4baSyz  *   2. If devp points to a GLDv3 datalink but it doesn't have autopush
1084d62bc4baSyz  *	configuration, then replace devp with the physical device (if one
1085d62bc4baSyz  *	exists) and return 1.  This allows stropen() to find the old-school
1086d62bc4baSyz  *	per-driver autopush configuration.  (For softmac, the result is that
1087d62bc4baSyz  *	the softmac dev_t is replaced with the legacy device's dev_t).
1088d62bc4baSyz  *
1089d62bc4baSyz  *   3. If neither of the above apply, don't touch the args and return -1.
1090d62bc4baSyz  */
1091d62bc4baSyz int
1092d62bc4baSyz dld_autopush(dev_t *devp, struct dlautopush *dlap)
1093d62bc4baSyz {
1094d62bc4baSyz 	dld_ap_t	*dap;
1095d62bc4baSyz 	datalink_id_t	linkid;
1096d62bc4baSyz 	dev_t		phydev;
1097d62bc4baSyz 
1098d62bc4baSyz 	if (!GLDV3_DRV(getmajor(*devp)))
1099d62bc4baSyz 		return (-1);
1100d62bc4baSyz 
1101d62bc4baSyz 	/*
1102d62bc4baSyz 	 * Find the linkid by the link's dev_t.
1103d62bc4baSyz 	 */
1104d62bc4baSyz 	if (dls_devnet_dev2linkid(*devp, &linkid) != 0)
1105d62bc4baSyz 		return (-1);
1106d62bc4baSyz 
1107d62bc4baSyz 	/*
1108d62bc4baSyz 	 * Find the autopush configuration associated with the linkid.
1109d62bc4baSyz 	 */
1110d62bc4baSyz 	rw_enter(&dld_ap_hash_lock, RW_READER);
1111d62bc4baSyz 	if (mod_hash_find(dld_ap_hashp, (mod_hash_key_t)(uintptr_t)linkid,
1112d62bc4baSyz 	    (mod_hash_val_t *)&dap) == 0) {
1113d62bc4baSyz 		*dlap = dap->da_ap;
1114d62bc4baSyz 		rw_exit(&dld_ap_hash_lock);
1115d62bc4baSyz 		return (0);
1116d62bc4baSyz 	}
1117d62bc4baSyz 	rw_exit(&dld_ap_hash_lock);
1118d62bc4baSyz 
1119d62bc4baSyz 	if (dls_devnet_phydev(linkid, &phydev) != 0)
1120d62bc4baSyz 		return (-1);
1121d62bc4baSyz 
1122d62bc4baSyz 	*devp = phydev;
1123d62bc4baSyz 	return (1);
1124d62bc4baSyz }
1125d62bc4baSyz 
11260ba2cbe9Sxc /*
11270ba2cbe9Sxc  * Secure objects implementation
11280ba2cbe9Sxc  */
11290ba2cbe9Sxc 
11300ba2cbe9Sxc /* ARGSUSED */
11310ba2cbe9Sxc static int
11320ba2cbe9Sxc drv_secobj_ctor(void *buf, void *arg, int kmflag)
11330ba2cbe9Sxc {
11340ba2cbe9Sxc 	bzero(buf, sizeof (dld_secobj_t));
11350ba2cbe9Sxc 	return (0);
11360ba2cbe9Sxc }
11370ba2cbe9Sxc 
11380ba2cbe9Sxc static void
11390ba2cbe9Sxc drv_secobj_init(void)
11400ba2cbe9Sxc {
11410ba2cbe9Sxc 	rw_init(&drv_secobj_lock, NULL, RW_DEFAULT, NULL);
11420ba2cbe9Sxc 	drv_secobj_cachep = kmem_cache_create("drv_secobj_cache",
11430ba2cbe9Sxc 	    sizeof (dld_secobj_t), 0, drv_secobj_ctor, NULL,
11440ba2cbe9Sxc 	    NULL, NULL, NULL, 0);
11450ba2cbe9Sxc 	drv_secobj_hash = mod_hash_create_extended("drv_secobj_hash",
11460ba2cbe9Sxc 	    SECOBJ_WEP_HASHSZ, mod_hash_null_keydtor, mod_hash_null_valdtor,
11470ba2cbe9Sxc 	    mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP);
11480ba2cbe9Sxc }
11490ba2cbe9Sxc 
11500ba2cbe9Sxc static void
11510ba2cbe9Sxc drv_secobj_fini(void)
11520ba2cbe9Sxc {
11530ba2cbe9Sxc 	mod_hash_destroy_hash(drv_secobj_hash);
11540ba2cbe9Sxc 	kmem_cache_destroy(drv_secobj_cachep);
11550ba2cbe9Sxc 	rw_destroy(&drv_secobj_lock);
11560ba2cbe9Sxc }
11570ba2cbe9Sxc 
1158eae72b5bSSebastien Roy /* ARGSUSED */
1159eae72b5bSSebastien Roy static int
1160da14cebeSEric Cheng drv_ioc_secobj_set(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
11610ba2cbe9Sxc {
1162eae72b5bSSebastien Roy 	dld_ioc_secobj_set_t	*ssp = karg;
11630ba2cbe9Sxc 	dld_secobj_t		*sobjp, *objp;
1164eae72b5bSSebastien Roy 	int			err;
11650ba2cbe9Sxc 
11660ba2cbe9Sxc 	sobjp = &ssp->ss_obj;
11670ba2cbe9Sxc 
1168a399b765Szf 	if (sobjp->so_class != DLD_SECOBJ_CLASS_WEP &&
1169a399b765Szf 	    sobjp->so_class != DLD_SECOBJ_CLASS_WPA)
1170eae72b5bSSebastien Roy 		return (EINVAL);
11710ba2cbe9Sxc 
11720ba2cbe9Sxc 	if (sobjp->so_name[DLD_SECOBJ_NAME_MAX - 1] != '\0' ||
11730ba2cbe9Sxc 	    sobjp->so_len > DLD_SECOBJ_VAL_MAX)
1174eae72b5bSSebastien Roy 		return (EINVAL);
11750ba2cbe9Sxc 
11760ba2cbe9Sxc 	rw_enter(&drv_secobj_lock, RW_WRITER);
11770ba2cbe9Sxc 	err = mod_hash_find(drv_secobj_hash, (mod_hash_key_t)sobjp->so_name,
11780ba2cbe9Sxc 	    (mod_hash_val_t *)&objp);
11790ba2cbe9Sxc 	if (err == 0) {
11800ba2cbe9Sxc 		if ((ssp->ss_flags & DLD_SECOBJ_OPT_CREATE) != 0) {
11810ba2cbe9Sxc 			rw_exit(&drv_secobj_lock);
1182eae72b5bSSebastien Roy 			return (EEXIST);
11830ba2cbe9Sxc 		}
11840ba2cbe9Sxc 	} else {
11850ba2cbe9Sxc 		ASSERT(err == MH_ERR_NOTFOUND);
11860ba2cbe9Sxc 		if ((ssp->ss_flags & DLD_SECOBJ_OPT_CREATE) == 0) {
11870ba2cbe9Sxc 			rw_exit(&drv_secobj_lock);
1188eae72b5bSSebastien Roy 			return (ENOENT);
11890ba2cbe9Sxc 		}
11900ba2cbe9Sxc 		objp = kmem_cache_alloc(drv_secobj_cachep, KM_SLEEP);
11910ba2cbe9Sxc 		(void) strlcpy(objp->so_name, sobjp->so_name,
11920ba2cbe9Sxc 		    DLD_SECOBJ_NAME_MAX);
11930ba2cbe9Sxc 
1194eae72b5bSSebastien Roy 		VERIFY(mod_hash_insert(drv_secobj_hash,
1195eae72b5bSSebastien Roy 		    (mod_hash_key_t)objp->so_name, (mod_hash_val_t)objp) == 0);
11960ba2cbe9Sxc 	}
11970ba2cbe9Sxc 	bcopy(sobjp->so_val, objp->so_val, sobjp->so_len);
11980ba2cbe9Sxc 	objp->so_len = sobjp->so_len;
11990ba2cbe9Sxc 	objp->so_class = sobjp->so_class;
12000ba2cbe9Sxc 	rw_exit(&drv_secobj_lock);
1201eae72b5bSSebastien Roy 	return (0);
12020ba2cbe9Sxc }
12030ba2cbe9Sxc 
12040ba2cbe9Sxc typedef struct dld_secobj_state {
12050ba2cbe9Sxc 	uint_t		ss_free;
12060ba2cbe9Sxc 	uint_t		ss_count;
12070ba2cbe9Sxc 	int		ss_rc;
1208eae72b5bSSebastien Roy 	int		ss_mode;
12090ba2cbe9Sxc 	dld_secobj_t	*ss_objp;
12100ba2cbe9Sxc } dld_secobj_state_t;
12110ba2cbe9Sxc 
12120ba2cbe9Sxc /* ARGSUSED */
12130ba2cbe9Sxc static uint_t
12140ba2cbe9Sxc drv_secobj_walker(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
12150ba2cbe9Sxc {
12160ba2cbe9Sxc 	dld_secobj_state_t	*statep = arg;
12170ba2cbe9Sxc 	dld_secobj_t		*sobjp = (dld_secobj_t *)val;
12180ba2cbe9Sxc 
12190ba2cbe9Sxc 	if (statep->ss_free < sizeof (dld_secobj_t)) {
12200ba2cbe9Sxc 		statep->ss_rc = ENOSPC;
12210ba2cbe9Sxc 		return (MH_WALK_TERMINATE);
12220ba2cbe9Sxc 	}
1223eae72b5bSSebastien Roy 	if (ddi_copyout(sobjp, statep->ss_objp, sizeof (*sobjp),
1224eae72b5bSSebastien Roy 	    statep->ss_mode) != 0) {
1225eae72b5bSSebastien Roy 		statep->ss_rc = EFAULT;
1226eae72b5bSSebastien Roy 		return (MH_WALK_TERMINATE);
1227eae72b5bSSebastien Roy 	}
12280ba2cbe9Sxc 	statep->ss_objp++;
12290ba2cbe9Sxc 	statep->ss_free -= sizeof (dld_secobj_t);
12300ba2cbe9Sxc 	statep->ss_count++;
12310ba2cbe9Sxc 	return (MH_WALK_CONTINUE);
12320ba2cbe9Sxc }
12330ba2cbe9Sxc 
1234eae72b5bSSebastien Roy /* ARGSUSED */
1235eae72b5bSSebastien Roy static int
1236da14cebeSEric Cheng drv_ioc_secobj_get(void *karg, intptr_t arg, int mode, cred_t *cred, int *rvalp)
12370ba2cbe9Sxc {
1238eae72b5bSSebastien Roy 	dld_ioc_secobj_get_t	*sgp = karg;
12390ba2cbe9Sxc 	dld_secobj_t		*sobjp, *objp;
1240eae72b5bSSebastien Roy 	int			err;
12410ba2cbe9Sxc 
12420ba2cbe9Sxc 	sobjp = &sgp->sg_obj;
12430ba2cbe9Sxc 	if (sobjp->so_name[DLD_SECOBJ_NAME_MAX - 1] != '\0')
1244eae72b5bSSebastien Roy 		return (EINVAL);
12450ba2cbe9Sxc 
12460ba2cbe9Sxc 	rw_enter(&drv_secobj_lock, RW_READER);
12470ba2cbe9Sxc 	if (sobjp->so_name[0] != '\0') {
12480ba2cbe9Sxc 		err = mod_hash_find(drv_secobj_hash,
12490ba2cbe9Sxc 		    (mod_hash_key_t)sobjp->so_name, (mod_hash_val_t *)&objp);
12500ba2cbe9Sxc 		if (err != 0) {
12510ba2cbe9Sxc 			ASSERT(err == MH_ERR_NOTFOUND);
12520ba2cbe9Sxc 			rw_exit(&drv_secobj_lock);
1253eae72b5bSSebastien Roy 			return (ENOENT);
12540ba2cbe9Sxc 		}
12550ba2cbe9Sxc 		bcopy(objp->so_val, sobjp->so_val, objp->so_len);
12560ba2cbe9Sxc 		sobjp->so_len = objp->so_len;
12570ba2cbe9Sxc 		sobjp->so_class = objp->so_class;
12580ba2cbe9Sxc 		sgp->sg_count = 1;
12590ba2cbe9Sxc 	} else {
12600ba2cbe9Sxc 		dld_secobj_state_t	state;
12610ba2cbe9Sxc 
1262eae72b5bSSebastien Roy 		state.ss_free = sgp->sg_size - sizeof (dld_ioc_secobj_get_t);
12630ba2cbe9Sxc 		state.ss_count = 0;
12640ba2cbe9Sxc 		state.ss_rc = 0;
1265eae72b5bSSebastien Roy 		state.ss_mode = mode;
1266eae72b5bSSebastien Roy 		state.ss_objp = (dld_secobj_t *)((uchar_t *)arg +
1267eae72b5bSSebastien Roy 		    sizeof (dld_ioc_secobj_get_t));
1268eae72b5bSSebastien Roy 
12690ba2cbe9Sxc 		mod_hash_walk(drv_secobj_hash, drv_secobj_walker, &state);
12700ba2cbe9Sxc 		if (state.ss_rc != 0) {
12710ba2cbe9Sxc 			rw_exit(&drv_secobj_lock);
1272eae72b5bSSebastien Roy 			return (state.ss_rc);
12730ba2cbe9Sxc 		}
12740ba2cbe9Sxc 		sgp->sg_count = state.ss_count;
12750ba2cbe9Sxc 	}
12760ba2cbe9Sxc 	rw_exit(&drv_secobj_lock);
1277eae72b5bSSebastien Roy 	return (0);
12780ba2cbe9Sxc }
12790ba2cbe9Sxc 
1280eae72b5bSSebastien Roy /* ARGSUSED */
1281eae72b5bSSebastien Roy static int
1282da14cebeSEric Cheng drv_ioc_secobj_unset(void *karg, intptr_t arg, int mode, cred_t *cred,
1283da14cebeSEric Cheng     int *rvalp)
12840ba2cbe9Sxc {
1285eae72b5bSSebastien Roy 	dld_ioc_secobj_unset_t	*sup = karg;
12860ba2cbe9Sxc 	dld_secobj_t		*objp;
12870ba2cbe9Sxc 	mod_hash_val_t		val;
1288eae72b5bSSebastien Roy 	int			err;
12890ba2cbe9Sxc 
12900ba2cbe9Sxc 	if (sup->su_name[DLD_SECOBJ_NAME_MAX - 1] != '\0')
1291eae72b5bSSebastien Roy 		return (EINVAL);
12920ba2cbe9Sxc 
12930ba2cbe9Sxc 	rw_enter(&drv_secobj_lock, RW_WRITER);
12940ba2cbe9Sxc 	err = mod_hash_find(drv_secobj_hash, (mod_hash_key_t)sup->su_name,
12950ba2cbe9Sxc 	    (mod_hash_val_t *)&objp);
12960ba2cbe9Sxc 	if (err != 0) {
12970ba2cbe9Sxc 		ASSERT(err == MH_ERR_NOTFOUND);
12980ba2cbe9Sxc 		rw_exit(&drv_secobj_lock);
1299eae72b5bSSebastien Roy 		return (ENOENT);
13000ba2cbe9Sxc 	}
1301eae72b5bSSebastien Roy 	VERIFY(mod_hash_remove(drv_secobj_hash, (mod_hash_key_t)sup->su_name,
1302eae72b5bSSebastien Roy 	    (mod_hash_val_t *)&val) == 0);
13030ba2cbe9Sxc 	ASSERT(objp == (dld_secobj_t *)val);
13040ba2cbe9Sxc 
13050ba2cbe9Sxc 	kmem_cache_free(drv_secobj_cachep, objp);
13060ba2cbe9Sxc 	rw_exit(&drv_secobj_lock);
1307eae72b5bSSebastien Roy 	return (0);
1308eae72b5bSSebastien Roy }
1309eae72b5bSSebastien Roy 
13102b24ab6bSSebastien Roy /*
13112b24ab6bSSebastien Roy  * Note that ioctls that modify links have a NULL di_priv_func(), as
13122b24ab6bSSebastien Roy  * privileges can only be checked after we know the class of the link being
13132b24ab6bSSebastien Roy  * modified (due to class-specific fine-grained privileges such as
13142b24ab6bSSebastien Roy  * sys_iptun_config).
13152b24ab6bSSebastien Roy  */
1316eae72b5bSSebastien Roy static dld_ioc_info_t drv_ioc_list[] = {
1317eae72b5bSSebastien Roy 	{DLDIOC_ATTR, DLDCOPYINOUT, sizeof (dld_ioc_attr_t),
13182b24ab6bSSebastien Roy 	    drv_ioc_attr, NULL},
1319eae72b5bSSebastien Roy 	{DLDIOC_PHYS_ATTR, DLDCOPYINOUT, sizeof (dld_ioc_phys_attr_t),
13202b24ab6bSSebastien Roy 	    drv_ioc_phys_attr, NULL},
1321da14cebeSEric Cheng 	{DLDIOC_SECOBJ_SET, DLDCOPYIN, sizeof (dld_ioc_secobj_set_t),
13222b24ab6bSSebastien Roy 	    drv_ioc_secobj_set, secpolicy_dl_config},
1323da14cebeSEric Cheng 	{DLDIOC_SECOBJ_GET, DLDCOPYINOUT, sizeof (dld_ioc_secobj_get_t),
13242b24ab6bSSebastien Roy 	    drv_ioc_secobj_get, secpolicy_dl_config},
1325da14cebeSEric Cheng 	{DLDIOC_SECOBJ_UNSET, DLDCOPYIN, sizeof (dld_ioc_secobj_unset_t),
13262b24ab6bSSebastien Roy 	    drv_ioc_secobj_unset, secpolicy_dl_config},
1327da14cebeSEric Cheng 	{DLDIOC_DOORSERVER, DLDCOPYIN, sizeof (dld_ioc_door_t),
13282b24ab6bSSebastien Roy 	    drv_ioc_doorserver, secpolicy_dl_config},
1329da14cebeSEric Cheng 	{DLDIOC_RENAME, DLDCOPYIN, sizeof (dld_ioc_rename_t),
13302b24ab6bSSebastien Roy 	    drv_ioc_rename, NULL},
1331da14cebeSEric Cheng 	{DLDIOC_MACADDRGET, DLDCOPYINOUT, sizeof (dld_ioc_macaddrget_t),
13322b24ab6bSSebastien Roy 	    drv_ioc_macaddrget, NULL},
1333da14cebeSEric Cheng 	{DLDIOC_ADDFLOW, DLDCOPYIN, sizeof (dld_ioc_addflow_t),
13342b24ab6bSSebastien Roy 	    drv_ioc_addflow, secpolicy_dl_config},
1335da14cebeSEric Cheng 	{DLDIOC_REMOVEFLOW, DLDCOPYIN, sizeof (dld_ioc_removeflow_t),
13362b24ab6bSSebastien Roy 	    drv_ioc_removeflow, secpolicy_dl_config},
1337da14cebeSEric Cheng 	{DLDIOC_MODIFYFLOW, DLDCOPYIN, sizeof (dld_ioc_modifyflow_t),
13382b24ab6bSSebastien Roy 	    drv_ioc_modifyflow, secpolicy_dl_config},
1339da14cebeSEric Cheng 	{DLDIOC_WALKFLOW, DLDCOPYINOUT, sizeof (dld_ioc_walkflow_t),
13402b24ab6bSSebastien Roy 	    drv_ioc_walkflow, NULL},
1341da14cebeSEric Cheng 	{DLDIOC_USAGELOG, DLDCOPYIN, sizeof (dld_ioc_usagelog_t),
13422b24ab6bSSebastien Roy 	    drv_ioc_usagelog, secpolicy_dl_config},
1343da14cebeSEric Cheng 	{DLDIOC_SETMACPROP, DLDCOPYIN, sizeof (dld_ioc_macprop_t),
13442b24ab6bSSebastien Roy 	    drv_ioc_setprop, NULL},
1345eae72b5bSSebastien Roy 	{DLDIOC_GETMACPROP, DLDCOPYIN, sizeof (dld_ioc_macprop_t),
13462b24ab6bSSebastien Roy 	    drv_ioc_getprop, NULL},
1347da14cebeSEric Cheng 	{DLDIOC_GETHWGRP, DLDCOPYINOUT, sizeof (dld_ioc_hwgrpget_t),
1348*0dc2366fSVenugopal Iyer 	    drv_ioc_hwgrpget, NULL},
1349eae72b5bSSebastien Roy };
1350eae72b5bSSebastien Roy 
1351eae72b5bSSebastien Roy typedef struct dld_ioc_modentry {
1352eae72b5bSSebastien Roy 	uint16_t	dim_modid;	/* Top 16 bits of ioctl command */
1353eae72b5bSSebastien Roy 	char		*dim_modname;	/* Module to be loaded */
1354eae72b5bSSebastien Roy 	dld_ioc_info_t	*dim_list;	/* array of ioctl structures */
1355eae72b5bSSebastien Roy 	uint_t		dim_count;	/* number of elements in dim_list */
1356eae72b5bSSebastien Roy } dld_ioc_modentry_t;
1357eae72b5bSSebastien Roy 
1358eae72b5bSSebastien Roy /*
1359eae72b5bSSebastien Roy  * For all modules except for dld, dim_list and dim_count are assigned
1360eae72b5bSSebastien Roy  * when the modules register their ioctls in dld_ioc_register().  We
1361eae72b5bSSebastien Roy  * can statically initialize dld's ioctls in-line here; there's no
1362eae72b5bSSebastien Roy  * need for it to call dld_ioc_register() itself.
1363eae72b5bSSebastien Roy  */
1364eae72b5bSSebastien Roy static dld_ioc_modentry_t dld_ioc_modtable[] = {
13652b24ab6bSSebastien Roy 	{DLD_IOC,	"dld",	drv_ioc_list,	DLDIOCCNT(drv_ioc_list)},
1366eae72b5bSSebastien Roy 	{AGGR_IOC,	"aggr",	NULL, 0},
1367b509e89bSRishi Srivatsavai 	{VNIC_IOC,	"vnic",	NULL, 0},
13684eaa4710SRishi Srivatsavai 	{SIMNET_IOC,	"simnet", NULL, 0},
13692b24ab6bSSebastien Roy 	{BRIDGE_IOC,	"bridge", NULL, 0},
13702b24ab6bSSebastien Roy 	{IPTUN_IOC,	"iptun", NULL, 0}
1371eae72b5bSSebastien Roy };
1372eae72b5bSSebastien Roy #define	DLDIOC_CNT	\
1373eae72b5bSSebastien Roy 	(sizeof (dld_ioc_modtable) / sizeof (dld_ioc_modentry_t))
1374eae72b5bSSebastien Roy 
1375eae72b5bSSebastien Roy static dld_ioc_modentry_t *
1376eae72b5bSSebastien Roy dld_ioc_findmod(uint16_t modid)
1377eae72b5bSSebastien Roy {
1378eae72b5bSSebastien Roy 	int	i;
1379eae72b5bSSebastien Roy 
1380eae72b5bSSebastien Roy 	for (i = 0; i < DLDIOC_CNT; i++) {
1381eae72b5bSSebastien Roy 		if (modid == dld_ioc_modtable[i].dim_modid)
1382eae72b5bSSebastien Roy 			return (&dld_ioc_modtable[i]);
1383eae72b5bSSebastien Roy 	}
1384eae72b5bSSebastien Roy 	return (NULL);
1385eae72b5bSSebastien Roy }
1386eae72b5bSSebastien Roy 
1387eae72b5bSSebastien Roy int
1388eae72b5bSSebastien Roy dld_ioc_register(uint16_t modid, dld_ioc_info_t *list, uint_t count)
1389eae72b5bSSebastien Roy {
1390eae72b5bSSebastien Roy 	dld_ioc_modentry_t *dim = dld_ioc_findmod(modid);
1391eae72b5bSSebastien Roy 
1392eae72b5bSSebastien Roy 	if (dim == NULL)
1393eae72b5bSSebastien Roy 		return (ENOENT);
1394eae72b5bSSebastien Roy 
1395eae72b5bSSebastien Roy 	dim->dim_list = list;
1396eae72b5bSSebastien Roy 	dim->dim_count = count;
1397eae72b5bSSebastien Roy 	return (0);
1398eae72b5bSSebastien Roy }
1399eae72b5bSSebastien Roy 
1400eae72b5bSSebastien Roy void
1401eae72b5bSSebastien Roy dld_ioc_unregister(uint16_t modid)
1402eae72b5bSSebastien Roy {
1403eae72b5bSSebastien Roy 	VERIFY(dld_ioc_register(modid, NULL, 0) == 0);
1404eae72b5bSSebastien Roy }
14050ba2cbe9Sxc 
1406eae72b5bSSebastien Roy /*
1407eae72b5bSSebastien Roy  * The general design with GLDv3 ioctls is that all ioctls issued
1408eae72b5bSSebastien Roy  * through /dev/dld go through this drv_ioctl() function.  This
1409eae72b5bSSebastien Roy  * function handles all ioctls on behalf of modules listed in
1410eae72b5bSSebastien Roy  * dld_ioc_modtable.
1411eae72b5bSSebastien Roy  *
1412eae72b5bSSebastien Roy  * When an ioctl is received, this function looks for the associated
1413eae72b5bSSebastien Roy  * module-id-specific ioctl information using dld_ioc_findmod().  The
1414eae72b5bSSebastien Roy  * call to ddi_hold_devi_by_instance() on the associated device will
1415eae72b5bSSebastien Roy  * cause the kernel module responsible for the ioctl to be loaded if
1416eae72b5bSSebastien Roy  * it's not already loaded, which should result in that module calling
1417eae72b5bSSebastien Roy  * dld_ioc_register(), thereby filling in the dim_list containing the
1418eae72b5bSSebastien Roy  * details for the ioctl being processed.
1419eae72b5bSSebastien Roy  *
1420eae72b5bSSebastien Roy  * This function can then perform operations such as copyin() data and
1421eae72b5bSSebastien Roy  * do credential checks based on the registered ioctl information,
1422eae72b5bSSebastien Roy  * then issue the callback function di_func() registered by the
1423eae72b5bSSebastien Roy  * responsible module.  Upon return, the appropriate copyout()
1424eae72b5bSSebastien Roy  * operation can be performed and the operation completes.
1425eae72b5bSSebastien Roy  */
1426eae72b5bSSebastien Roy /* ARGSUSED */
1427eae72b5bSSebastien Roy static int
1428eae72b5bSSebastien Roy drv_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *cred, int *rvalp)
1429eae72b5bSSebastien Roy {
1430eae72b5bSSebastien Roy 	dld_ioc_modentry_t *dim;
1431eae72b5bSSebastien Roy 	dld_ioc_info_t	*info;
1432eae72b5bSSebastien Roy 	dev_info_t	*dip = NULL;
1433eae72b5bSSebastien Roy 	void		*buf = NULL;
1434eae72b5bSSebastien Roy 	size_t		sz;
1435eae72b5bSSebastien Roy 	int		i, err;
1436eae72b5bSSebastien Roy 
1437eae72b5bSSebastien Roy 	if ((dim = dld_ioc_findmod(DLD_IOC_MODID(cmd))) == NULL)
1438eae72b5bSSebastien Roy 		return (ENOTSUP);
1439eae72b5bSSebastien Roy 
1440eae72b5bSSebastien Roy 	dip = ddi_hold_devi_by_instance(ddi_name_to_major(dim->dim_modname),
1441eae72b5bSSebastien Roy 	    0, 0);
1442eae72b5bSSebastien Roy 	if (dip == NULL || dim->dim_list == NULL) {
1443eae72b5bSSebastien Roy 		err = ENODEV;
1444eae72b5bSSebastien Roy 		goto done;
1445eae72b5bSSebastien Roy 	}
1446eae72b5bSSebastien Roy 
1447eae72b5bSSebastien Roy 	for (i = 0; i < dim->dim_count; i++) {
1448eae72b5bSSebastien Roy 		if (cmd == dim->dim_list[i].di_cmd)
1449eae72b5bSSebastien Roy 			break;
1450eae72b5bSSebastien Roy 	}
1451eae72b5bSSebastien Roy 	if (i == dim->dim_count) {
1452eae72b5bSSebastien Roy 		err = ENOTSUP;
1453eae72b5bSSebastien Roy 		goto done;
1454eae72b5bSSebastien Roy 	}
1455eae72b5bSSebastien Roy 
1456eae72b5bSSebastien Roy 	info = &dim->dim_list[i];
14572b24ab6bSSebastien Roy 
14582b24ab6bSSebastien Roy 	if (info->di_priv_func != NULL &&
14592b24ab6bSSebastien Roy 	    (err = info->di_priv_func(cred)) != 0)
1460eae72b5bSSebastien Roy 		goto done;
1461eae72b5bSSebastien Roy 
1462eae72b5bSSebastien Roy 	sz = info->di_argsize;
1463eae72b5bSSebastien Roy 	if ((buf = kmem_zalloc(sz, KM_NOSLEEP)) == NULL) {
1464eae72b5bSSebastien Roy 		err = ENOMEM;
1465eae72b5bSSebastien Roy 		goto done;
1466eae72b5bSSebastien Roy 	}
1467eae72b5bSSebastien Roy 
1468eae72b5bSSebastien Roy 	if ((info->di_flags & DLDCOPYIN) &&
1469eae72b5bSSebastien Roy 	    ddi_copyin((void *)arg, buf, sz, mode) != 0) {
1470eae72b5bSSebastien Roy 		err = EFAULT;
1471eae72b5bSSebastien Roy 		goto done;
1472eae72b5bSSebastien Roy 	}
1473eae72b5bSSebastien Roy 
1474da14cebeSEric Cheng 	err = info->di_func(buf, arg, mode, cred, rvalp);
1475eae72b5bSSebastien Roy 
1476eae72b5bSSebastien Roy 	if ((info->di_flags & DLDCOPYOUT) &&
1477eae72b5bSSebastien Roy 	    ddi_copyout(buf, (void *)arg, sz, mode) != 0 && err == 0)
1478eae72b5bSSebastien Roy 		err = EFAULT;
1479eae72b5bSSebastien Roy 
1480eae72b5bSSebastien Roy done:
1481eae72b5bSSebastien Roy 	if (buf != NULL)
1482eae72b5bSSebastien Roy 		kmem_free(buf, sz);
1483eae72b5bSSebastien Roy 	if (dip != NULL)
1484eae72b5bSSebastien Roy 		ddi_release_devi(dip);
1485eae72b5bSSebastien Roy 	return (err);
14860ba2cbe9Sxc }
1487