10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55072Smcpowers  * Common Development and Distribution License (the "License").
65072Smcpowers  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*10500SHai-May.Chao@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate  * The ioctl interface for administrative commands.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/modctl.h>
330Sstevel@tonic-gate #include <sys/conf.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <sys/ddi.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/errno.h>
390Sstevel@tonic-gate #include <sys/ksynch.h>
400Sstevel@tonic-gate #include <sys/file.h>
410Sstevel@tonic-gate #include <sys/open.h>
420Sstevel@tonic-gate #include <sys/cred.h>
430Sstevel@tonic-gate #include <sys/model.h>
440Sstevel@tonic-gate #include <sys/sysmacros.h>
450Sstevel@tonic-gate #include <sys/crypto/common.h>
460Sstevel@tonic-gate #include <sys/crypto/api.h>
470Sstevel@tonic-gate #include <sys/crypto/impl.h>
480Sstevel@tonic-gate #include <sys/crypto/sched_impl.h>
490Sstevel@tonic-gate #include <sys/crypto/ioctladmin.h>
500Sstevel@tonic-gate #include <c2/audit.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * DDI entry points.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate static int cryptoadm_attach(dev_info_t *, ddi_attach_cmd_t);
560Sstevel@tonic-gate static int cryptoadm_detach(dev_info_t *, ddi_detach_cmd_t);
570Sstevel@tonic-gate static int cryptoadm_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
580Sstevel@tonic-gate static int cryptoadm_open(dev_t *, int, int, cred_t *);
590Sstevel@tonic-gate static int cryptoadm_close(dev_t, int, int, cred_t *);
600Sstevel@tonic-gate static int cryptoadm_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate extern void audit_cryptoadm(int, char *, crypto_mech_name_t *, uint_t,
630Sstevel@tonic-gate     uint_t, uint32_t, int);
64*10500SHai-May.Chao@Sun.COM 
65*10500SHai-May.Chao@Sun.COM kmutex_t fips140_mode_lock;
66*10500SHai-May.Chao@Sun.COM extern uint32_t global_fips140_mode;
67*10500SHai-May.Chao@Sun.COM 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * Module linkage.
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate static struct cb_ops cbops = {
720Sstevel@tonic-gate 	cryptoadm_open,		/* cb_open */
730Sstevel@tonic-gate 	cryptoadm_close,	/* cb_close */
740Sstevel@tonic-gate 	nodev,			/* cb_strategy */
750Sstevel@tonic-gate 	nodev,			/* cb_print */
760Sstevel@tonic-gate 	nodev,			/* cb_dump */
770Sstevel@tonic-gate 	nodev,			/* cb_read */
780Sstevel@tonic-gate 	nodev,			/* cb_write */
790Sstevel@tonic-gate 	cryptoadm_ioctl,	/* cb_ioctl */
800Sstevel@tonic-gate 	nodev,			/* cb_devmap */
810Sstevel@tonic-gate 	nodev,			/* cb_mmap */
820Sstevel@tonic-gate 	nodev,			/* cb_segmap */
830Sstevel@tonic-gate 	nochpoll,		/* cb_chpoll */
840Sstevel@tonic-gate 	ddi_prop_op,		/* cb_prop_op */
850Sstevel@tonic-gate 	NULL,			/* cb_streamtab */
860Sstevel@tonic-gate 	D_MP,			/* cb_flag */
870Sstevel@tonic-gate 	CB_REV,			/* cb_rev */
880Sstevel@tonic-gate 	nodev,			/* cb_aread */
890Sstevel@tonic-gate 	nodev,			/* cb_awrite */
900Sstevel@tonic-gate };
910Sstevel@tonic-gate 
920Sstevel@tonic-gate static struct dev_ops devops = {
930Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev */
940Sstevel@tonic-gate 	0,			/* devo_refcnt */
950Sstevel@tonic-gate 	cryptoadm_getinfo,	/* devo_getinfo */
960Sstevel@tonic-gate 	nulldev,		/* devo_identify */
970Sstevel@tonic-gate 	nulldev,		/* devo_probe */
980Sstevel@tonic-gate 	cryptoadm_attach,	/* devo_attach */
990Sstevel@tonic-gate 	cryptoadm_detach,	/* devo_detach */
1000Sstevel@tonic-gate 	nodev,			/* devo_reset */
1010Sstevel@tonic-gate 	&cbops,			/* devo_cb_ops */
1020Sstevel@tonic-gate 	NULL,			/* devo_bus_ops */
1030Sstevel@tonic-gate 	NULL,			/* devo_power */
1047656SSherry.Moore@Sun.COM 	ddi_quiesce_not_needed,		/* devo_quiesce */
1050Sstevel@tonic-gate };
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static struct modldrv modldrv = {
1080Sstevel@tonic-gate 	&mod_driverops,					/* drv_modops */
1095072Smcpowers 	"Cryptographic Administrative Interface",	/* drv_linkinfo */
1100Sstevel@tonic-gate 	&devops,
1110Sstevel@tonic-gate };
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate static struct modlinkage modlinkage = {
1140Sstevel@tonic-gate 	MODREV_1,		/* ml_rev */
1150Sstevel@tonic-gate 	&modldrv,		/* ml_linkage */
1160Sstevel@tonic-gate 	NULL
1170Sstevel@tonic-gate };
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate static dev_info_t	*cryptoadm_dip = NULL;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate  * DDI entry points.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate int
1250Sstevel@tonic-gate _init(void)
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate int
1310Sstevel@tonic-gate _fini(void)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate int
1370Sstevel@tonic-gate _info(struct modinfo *modinfop)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /* ARGSUSED */
1430Sstevel@tonic-gate static int
1440Sstevel@tonic-gate cryptoadm_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	switch (cmd) {
1470Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
1480Sstevel@tonic-gate 		*result = (void *)cryptoadm_dip;
1490Sstevel@tonic-gate 		return (DDI_SUCCESS);
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1520Sstevel@tonic-gate 		*result = (void *)0;
1530Sstevel@tonic-gate 		return (DDI_SUCCESS);
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate 	return (DDI_FAILURE);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate static int
1590Sstevel@tonic-gate cryptoadm_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	if (cmd != DDI_ATTACH) {
1620Sstevel@tonic-gate 		return (DDI_FAILURE);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 	if (ddi_get_instance(dip) != 0) {
1650Sstevel@tonic-gate 		/* we only allow instance 0 to attach */
1660Sstevel@tonic-gate 		return (DDI_FAILURE);
1670Sstevel@tonic-gate 	}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	/* create the minor node */
1700Sstevel@tonic-gate 	if (ddi_create_minor_node(dip, "cryptoadm", S_IFCHR, 0,
1710Sstevel@tonic-gate 	    DDI_PSEUDO, 0) != DDI_SUCCESS) {
1720Sstevel@tonic-gate 		cmn_err(CE_WARN, "cryptoadm: failed creating minor node");
1730Sstevel@tonic-gate 		ddi_remove_minor_node(dip, NULL);
1740Sstevel@tonic-gate 		return (DDI_FAILURE);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
177*10500SHai-May.Chao@Sun.COM 	mutex_init(&fips140_mode_lock, NULL, MUTEX_DEFAULT, NULL);
1780Sstevel@tonic-gate 	cryptoadm_dip = dip;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	return (DDI_SUCCESS);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static int
1840Sstevel@tonic-gate cryptoadm_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
1870Sstevel@tonic-gate 		return (DDI_FAILURE);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	cryptoadm_dip = NULL;
1900Sstevel@tonic-gate 	ddi_remove_minor_node(dip, NULL);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	return (DDI_SUCCESS);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /* ARGSUSED */
1960Sstevel@tonic-gate static int
1970Sstevel@tonic-gate cryptoadm_open(dev_t *devp, int flag, int otyp, cred_t *credp)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate 	if (otyp != OTYP_CHR || cryptoadm_dip == NULL)
2000Sstevel@tonic-gate 		return (ENXIO);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	/* exclusive opens are not supported */
2030Sstevel@tonic-gate 	if (flag & FEXCL)
2040Sstevel@tonic-gate 		return (ENOTSUP);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), 0);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	kcf_sched_start();
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	return (0);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /* ARGSUSED */
2140Sstevel@tonic-gate static int
2150Sstevel@tonic-gate cryptoadm_close(dev_t dev, int flag, int otyp, cred_t *credp)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	return (0);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * Returns TRUE if array of size MAXNAMELEN contains a '\0'
2220Sstevel@tonic-gate  * termination character, otherwise, it returns FALSE.
2230Sstevel@tonic-gate  */
2240Sstevel@tonic-gate static boolean_t
2250Sstevel@tonic-gate null_terminated(char *array)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	int i;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	for (i = 0; i < MAXNAMELEN; i++)
2300Sstevel@tonic-gate 		if (array[i] == '\0')
2310Sstevel@tonic-gate 			return (B_TRUE);
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	return (B_FALSE);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate  * This ioctl returns an array of hardware providers.  Each entry
2380Sstevel@tonic-gate  * contains a device name, device instance, and number of
2390Sstevel@tonic-gate  * supported mechanisms.
2400Sstevel@tonic-gate  */
2410Sstevel@tonic-gate /* ARGSUSED */
2420Sstevel@tonic-gate static int
2430Sstevel@tonic-gate get_dev_list(dev_t dev, caddr_t arg, int mode, int *rval)
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate 	crypto_get_dev_list_t dev_list;
2460Sstevel@tonic-gate 	crypto_dev_list_entry_t *entries;
2470Sstevel@tonic-gate 	size_t copyout_size;
2480Sstevel@tonic-gate 	uint_t count;
2490Sstevel@tonic-gate 	ulong_t offset;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	if (copyin(arg, &dev_list, sizeof (dev_list)) != 0)
2520Sstevel@tonic-gate 		return (EFAULT);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/* get the list from the core module */
2550Sstevel@tonic-gate 	if (crypto_get_dev_list(&count, &entries) != 0) {
2560Sstevel@tonic-gate 		dev_list.dl_return_value = CRYPTO_FAILED;
2570Sstevel@tonic-gate 		if (copyout(&dev_list, arg, sizeof (dev_list)) != 0) {
2580Sstevel@tonic-gate 			return (EFAULT);
2590Sstevel@tonic-gate 		}
2600Sstevel@tonic-gate 		return (0);
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	/* check if buffer is too small */
2640Sstevel@tonic-gate 	if (count > dev_list.dl_dev_count) {
2650Sstevel@tonic-gate 		dev_list.dl_dev_count = count;
2660Sstevel@tonic-gate 		dev_list.dl_return_value = CRYPTO_BUFFER_TOO_SMALL;
2670Sstevel@tonic-gate 		crypto_free_dev_list(entries, count);
2680Sstevel@tonic-gate 		if (copyout(&dev_list, arg, sizeof (dev_list)) != 0) {
2690Sstevel@tonic-gate 			return (EFAULT);
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 		return (0);
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	dev_list.dl_dev_count = count;
2750Sstevel@tonic-gate 	dev_list.dl_return_value = CRYPTO_SUCCESS;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	copyout_size = count * sizeof (crypto_dev_list_entry_t);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	/* copyout the first stuff */
2800Sstevel@tonic-gate 	if (copyout(&dev_list, arg, sizeof (dev_list)) != 0) {
2810Sstevel@tonic-gate 		crypto_free_dev_list(entries, count);
2820Sstevel@tonic-gate 		return (EFAULT);
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 	/* copyout entries */
2860Sstevel@tonic-gate 	offset = offsetof(crypto_get_dev_list_t, dl_devs);
2870Sstevel@tonic-gate 	if (count > 0 && copyout(entries, arg + offset, copyout_size) != 0) {
2880Sstevel@tonic-gate 		crypto_free_dev_list(entries, count);
2890Sstevel@tonic-gate 		return (EFAULT);
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 	crypto_free_dev_list(entries, count);
2920Sstevel@tonic-gate 	return (0);
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate /*
2960Sstevel@tonic-gate  * This ioctl returns a buffer containing the null terminated names
2970Sstevel@tonic-gate  * of software providers.
2980Sstevel@tonic-gate  */
2990Sstevel@tonic-gate /* ARGSUSED */
3000Sstevel@tonic-gate static int
3010Sstevel@tonic-gate get_soft_list(dev_t dev, caddr_t arg, int mode, int *rval)
3020Sstevel@tonic-gate {
3030Sstevel@tonic-gate 	STRUCT_DECL(crypto_get_soft_list, soft_list);
3040Sstevel@tonic-gate 	char *names;
3050Sstevel@tonic-gate 	size_t len;
3060Sstevel@tonic-gate 	uint_t count;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	STRUCT_INIT(soft_list, mode);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	if (copyin(arg, STRUCT_BUF(soft_list), STRUCT_SIZE(soft_list)) != 0)
3110Sstevel@tonic-gate 		return (EFAULT);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/* get the list from the core module */
3140Sstevel@tonic-gate 	if (crypto_get_soft_list(&count, &names, &len) != 0) {
3150Sstevel@tonic-gate 		STRUCT_FSET(soft_list, sl_return_value, CRYPTO_FAILED);
3160Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(soft_list), arg,
3170Sstevel@tonic-gate 		    STRUCT_SIZE(soft_list)) != 0) {
3180Sstevel@tonic-gate 			return (EFAULT);
3190Sstevel@tonic-gate 		}
3200Sstevel@tonic-gate 		return (0);
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	/* check if buffer is too small */
3240Sstevel@tonic-gate 	if (len > STRUCT_FGET(soft_list, sl_soft_len)) {
3250Sstevel@tonic-gate 		STRUCT_FSET(soft_list, sl_soft_count, count);
3260Sstevel@tonic-gate 		STRUCT_FSET(soft_list, sl_soft_len, len);
3270Sstevel@tonic-gate 		STRUCT_FSET(soft_list, sl_return_value,
3280Sstevel@tonic-gate 		    CRYPTO_BUFFER_TOO_SMALL);
3290Sstevel@tonic-gate 		kmem_free(names, len);
3300Sstevel@tonic-gate 		if (copyout(STRUCT_BUF(soft_list), arg,
3310Sstevel@tonic-gate 		    STRUCT_SIZE(soft_list)) != 0) {
3320Sstevel@tonic-gate 			return (EFAULT);
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 		return (0);
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	STRUCT_FSET(soft_list, sl_soft_count, count);
3380Sstevel@tonic-gate 	STRUCT_FSET(soft_list, sl_soft_len, len);
3390Sstevel@tonic-gate 	STRUCT_FSET(soft_list, sl_return_value, CRYPTO_SUCCESS);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	if (count > 0 && copyout(names,
3420Sstevel@tonic-gate 	    STRUCT_FGETP(soft_list, sl_soft_names), len) != 0) {
3430Sstevel@tonic-gate 		kmem_free(names, len);
3440Sstevel@tonic-gate 		return (EFAULT);
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 	kmem_free(names, len);
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(soft_list), arg, STRUCT_SIZE(soft_list)) != 0) {
3490Sstevel@tonic-gate 		return (EFAULT);
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	return (0);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate /*
3560Sstevel@tonic-gate  * This ioctl returns an array of mechanisms supported by the
3570Sstevel@tonic-gate  * specified device.
3580Sstevel@tonic-gate  */
3590Sstevel@tonic-gate /* ARGSUSED */
3600Sstevel@tonic-gate static int
3610Sstevel@tonic-gate get_dev_info(dev_t dev, caddr_t arg, int mode, int *rval)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	crypto_get_dev_info_t dev_info;
3640Sstevel@tonic-gate 	crypto_mech_name_t *entries;
3650Sstevel@tonic-gate 	size_t copyout_size;
3660Sstevel@tonic-gate 	uint_t count;
3670Sstevel@tonic-gate 	ulong_t offset;
3680Sstevel@tonic-gate 	char *dev_name;
3690Sstevel@tonic-gate 	int rv;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	if (copyin(arg, &dev_info, sizeof (dev_info)) != 0)
3720Sstevel@tonic-gate 		return (EFAULT);
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	dev_name = dev_info.di_dev_name;
3750Sstevel@tonic-gate 	/* make sure the device name is null terminated */
3760Sstevel@tonic-gate 	if (!null_terminated(dev_name)) {
3770Sstevel@tonic-gate 		dev_info.di_return_value = CRYPTO_ARGUMENTS_BAD;
3780Sstevel@tonic-gate 		if (copyout(&dev_info, arg, sizeof (dev_info)) != 0) {
3790Sstevel@tonic-gate 			return (EFAULT);
3800Sstevel@tonic-gate 		}
3810Sstevel@tonic-gate 		return (0);
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	/* get mechanism names from the core module */
3850Sstevel@tonic-gate 	if ((rv = crypto_get_dev_info(dev_name, dev_info.di_dev_instance,
3860Sstevel@tonic-gate 	    &count, &entries)) != CRYPTO_SUCCESS) {
3870Sstevel@tonic-gate 		dev_info.di_return_value = rv;
3880Sstevel@tonic-gate 		if (copyout(&dev_info, arg, sizeof (dev_info)) != 0) {
3890Sstevel@tonic-gate 			return (EFAULT);
3900Sstevel@tonic-gate 		}
3910Sstevel@tonic-gate 		return (0);
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	/* check if buffer is too small */
3950Sstevel@tonic-gate 	if (count > dev_info.di_count) {
3960Sstevel@tonic-gate 		dev_info.di_count = count;
3970Sstevel@tonic-gate 		dev_info.di_return_value = CRYPTO_BUFFER_TOO_SMALL;
3980Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
3990Sstevel@tonic-gate 		if (copyout(&dev_info, arg, sizeof (dev_info)) != 0) {
4000Sstevel@tonic-gate 			return (EFAULT);
4010Sstevel@tonic-gate 		}
4020Sstevel@tonic-gate 		return (0);
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	dev_info.di_count = count;
4060Sstevel@tonic-gate 	dev_info.di_return_value = CRYPTO_SUCCESS;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	copyout_size = count * sizeof (crypto_mech_name_t);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	/* copyout the first stuff */
4110Sstevel@tonic-gate 	if (copyout(&dev_info, arg, sizeof (dev_info)) != 0) {
4120Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
4130Sstevel@tonic-gate 		return (EFAULT);
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	/* copyout entries */
4170Sstevel@tonic-gate 	offset = offsetof(crypto_get_dev_info_t, di_list);
4180Sstevel@tonic-gate 	if (copyout(entries, arg + offset, copyout_size) != 0) {
4190Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
4200Sstevel@tonic-gate 		return (EFAULT);
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 	crypto_free_mech_list(entries, count);
4230Sstevel@tonic-gate 	return (0);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate /*
4270Sstevel@tonic-gate  * This ioctl returns an array of mechanisms supported by the
4280Sstevel@tonic-gate  * specified cryptographic module.
4290Sstevel@tonic-gate  */
4300Sstevel@tonic-gate /* ARGSUSED */
4310Sstevel@tonic-gate static int
4320Sstevel@tonic-gate get_soft_info(dev_t dev, caddr_t arg, int mode, int *rval)
4330Sstevel@tonic-gate {
4340Sstevel@tonic-gate 	crypto_get_soft_info_t soft_info;
4350Sstevel@tonic-gate 	crypto_mech_name_t *entries;
4360Sstevel@tonic-gate 	size_t copyout_size;
4370Sstevel@tonic-gate 	uint_t count;
4380Sstevel@tonic-gate 	ulong_t offset;
4390Sstevel@tonic-gate 	char *name;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	if (copyin(arg, &soft_info, sizeof (soft_info)) != 0)
4420Sstevel@tonic-gate 		return (EFAULT);
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	name = soft_info.si_name;
4450Sstevel@tonic-gate 	/* make sure the provider name is null terminated */
4460Sstevel@tonic-gate 	if (!null_terminated(name)) {
4470Sstevel@tonic-gate 		soft_info.si_return_value = CRYPTO_ARGUMENTS_BAD;
4480Sstevel@tonic-gate 		if (copyout(&soft_info, arg, sizeof (soft_info)) != 0) {
4490Sstevel@tonic-gate 			return (EFAULT);
4500Sstevel@tonic-gate 		}
4510Sstevel@tonic-gate 		return (0);
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	/* get mechanism names from the core module */
4550Sstevel@tonic-gate 	if (crypto_get_soft_info(name, &count, &entries) != 0) {
4560Sstevel@tonic-gate 		soft_info.si_return_value = CRYPTO_FAILED;
4570Sstevel@tonic-gate 		if (copyout(&soft_info, arg, sizeof (soft_info)) != 0) {
4580Sstevel@tonic-gate 			return (EFAULT);
4590Sstevel@tonic-gate 		}
4600Sstevel@tonic-gate 		return (0);
4610Sstevel@tonic-gate 	}
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	/* check if buffer is too small */
4640Sstevel@tonic-gate 	if (count > soft_info.si_count) {
4650Sstevel@tonic-gate 		soft_info.si_count = count;
4660Sstevel@tonic-gate 		soft_info.si_return_value = CRYPTO_BUFFER_TOO_SMALL;
4670Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
4680Sstevel@tonic-gate 		if (copyout(&soft_info, arg, sizeof (soft_info)) != 0) {
4690Sstevel@tonic-gate 			return (EFAULT);
4700Sstevel@tonic-gate 		}
4710Sstevel@tonic-gate 		return (0);
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	soft_info.si_count = count;
4750Sstevel@tonic-gate 	soft_info.si_return_value = CRYPTO_SUCCESS;
4760Sstevel@tonic-gate 	copyout_size = count * sizeof (crypto_mech_name_t);
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	/* copyout the first stuff */
4790Sstevel@tonic-gate 	if (copyout(&soft_info, arg, sizeof (soft_info)) != 0) {
4800Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
4810Sstevel@tonic-gate 		return (EFAULT);
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	/* copyout entries */
4850Sstevel@tonic-gate 	offset = offsetof(crypto_get_soft_info_t, si_list);
4860Sstevel@tonic-gate 	if (copyout(entries, arg + offset, copyout_size) != 0) {
4870Sstevel@tonic-gate 		crypto_free_mech_list(entries, count);
4880Sstevel@tonic-gate 		return (EFAULT);
4890Sstevel@tonic-gate 	}
4900Sstevel@tonic-gate 	crypto_free_mech_list(entries, count);
4910Sstevel@tonic-gate 	return (0);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate /*
4950Sstevel@tonic-gate  * This ioctl disables mechanisms supported by the specified device.
4960Sstevel@tonic-gate  */
4970Sstevel@tonic-gate /* ARGSUSED */
4980Sstevel@tonic-gate static int
4990Sstevel@tonic-gate load_dev_disabled(dev_t dev, caddr_t arg, int mode, int *rval)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate 	crypto_load_dev_disabled_t dev_disabled;
5020Sstevel@tonic-gate 	crypto_mech_name_t *entries;
5030Sstevel@tonic-gate 	size_t size;
5040Sstevel@tonic-gate 	ulong_t offset;
5050Sstevel@tonic-gate 	uint_t count;
5060Sstevel@tonic-gate 	uint_t instance;
5070Sstevel@tonic-gate 	char *dev_name;
5080Sstevel@tonic-gate 	uint32_t rv;
5090Sstevel@tonic-gate 	int error = 0;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	if (copyin(arg, &dev_disabled, sizeof (dev_disabled)) != 0) {
5120Sstevel@tonic-gate 		error =  EFAULT;
5130Sstevel@tonic-gate 		goto out2;
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	dev_name = dev_disabled.dd_dev_name;
5170Sstevel@tonic-gate 	/* make sure the device name is null terminated */
5180Sstevel@tonic-gate 	if (!null_terminated(dev_name)) {
5190Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
5200Sstevel@tonic-gate 		goto out;
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	count = dev_disabled.dd_count;
5240Sstevel@tonic-gate 	instance = dev_disabled.dd_dev_instance;
5250Sstevel@tonic-gate 	if (count == 0) {
5260Sstevel@tonic-gate 		/* remove the entry */
5270Sstevel@tonic-gate 		if (crypto_load_dev_disabled(dev_name, instance, 0, NULL) != 0)
5280Sstevel@tonic-gate 			rv = CRYPTO_FAILED;
5290Sstevel@tonic-gate 		else
5300Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
5310Sstevel@tonic-gate 		goto out;
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	if (count > KCF_MAXMECHS) {
5350Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
5360Sstevel@tonic-gate 		goto out;
5370Sstevel@tonic-gate 	}
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	size = count * sizeof (crypto_mech_name_t);
5400Sstevel@tonic-gate 	entries = kmem_alloc(size, KM_SLEEP);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	offset = offsetof(crypto_load_dev_disabled_t, dd_list);
5430Sstevel@tonic-gate 	if (copyin(arg + offset, entries, size) != 0) {
5440Sstevel@tonic-gate 		kmem_free(entries, size);
5450Sstevel@tonic-gate 		error = EFAULT;
5460Sstevel@tonic-gate 		goto out2;
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	/* 'entries' consumed (but not freed) by crypto_load_dev_disabled() */
5500Sstevel@tonic-gate 	if (crypto_load_dev_disabled(dev_name, instance, count, entries) != 0) {
5510Sstevel@tonic-gate 		kmem_free(entries, size);
5520Sstevel@tonic-gate 		rv = CRYPTO_FAILED;
5530Sstevel@tonic-gate 		goto out;
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 	rv = CRYPTO_SUCCESS;
5560Sstevel@tonic-gate out:
5570Sstevel@tonic-gate 	dev_disabled.dd_return_value = rv;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	if (copyout(&dev_disabled, arg, sizeof (dev_disabled)) != 0) {
5600Sstevel@tonic-gate 		error = EFAULT;
5610Sstevel@tonic-gate 	}
5620Sstevel@tonic-gate out2:
5630Sstevel@tonic-gate 	if (audit_active)
5640Sstevel@tonic-gate 		audit_cryptoadm(CRYPTO_LOAD_DEV_DISABLED, dev_name, entries,
5650Sstevel@tonic-gate 		    count, instance, rv, error);
5660Sstevel@tonic-gate 	return (error);
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate /*
5700Sstevel@tonic-gate  * This ioctl disables mechanisms supported by the specified
5710Sstevel@tonic-gate  * cryptographic module.
5720Sstevel@tonic-gate  */
5730Sstevel@tonic-gate /* ARGSUSED */
5740Sstevel@tonic-gate static int
5750Sstevel@tonic-gate load_soft_disabled(dev_t dev, caddr_t arg, int mode, int *rval)
5760Sstevel@tonic-gate {
5770Sstevel@tonic-gate 	crypto_load_soft_disabled_t soft_disabled;
5780Sstevel@tonic-gate 	crypto_mech_name_t *entries;
5790Sstevel@tonic-gate 	size_t size;
5800Sstevel@tonic-gate 	uint_t count;
5810Sstevel@tonic-gate 	ulong_t offset;
5820Sstevel@tonic-gate 	char *name;
5830Sstevel@tonic-gate 	uint32_t rv;
5840Sstevel@tonic-gate 	int error = 0;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	if (copyin(arg, &soft_disabled, sizeof (soft_disabled)) != 0) {
5870Sstevel@tonic-gate 		error = EFAULT;
5880Sstevel@tonic-gate 		goto out2;
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	name = soft_disabled.sd_name;
5920Sstevel@tonic-gate 	/* make sure the name is null terminated */
5930Sstevel@tonic-gate 	if (!null_terminated(name)) {
5940Sstevel@tonic-gate 		soft_disabled.sd_return_value = CRYPTO_ARGUMENTS_BAD;
5950Sstevel@tonic-gate 		if (copyout(&soft_disabled, arg, sizeof (soft_disabled)) != 0) {
5960Sstevel@tonic-gate 			return (EFAULT);
5970Sstevel@tonic-gate 		}
5980Sstevel@tonic-gate 		return (0);
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	count = soft_disabled.sd_count;
6020Sstevel@tonic-gate 	if (count == 0) {
6030Sstevel@tonic-gate 		/* remove the entry */
6040Sstevel@tonic-gate 		if (crypto_load_soft_disabled(name, 0, NULL) != 0) {
6050Sstevel@tonic-gate 			rv = CRYPTO_FAILED;
6060Sstevel@tonic-gate 		} else {
6070Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
6080Sstevel@tonic-gate 		}
6090Sstevel@tonic-gate 		goto out;
6100Sstevel@tonic-gate 	}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	if (count > KCF_MAXMECHS) {
6130Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
6140Sstevel@tonic-gate 		goto out;
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	size = count * sizeof (crypto_mech_name_t);
6180Sstevel@tonic-gate 	entries = kmem_alloc(size, KM_SLEEP);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	offset = offsetof(crypto_load_soft_disabled_t, sd_list);
6210Sstevel@tonic-gate 	if (copyin(arg + offset, entries, size) != 0) {
6220Sstevel@tonic-gate 		kmem_free(entries, size);
6230Sstevel@tonic-gate 		error = EFAULT;
6240Sstevel@tonic-gate 		goto out2;
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/* 'entries' is consumed by crypto_load_soft_disabled() */
6280Sstevel@tonic-gate 	if (crypto_load_soft_disabled(name, count, entries) != 0) {
6290Sstevel@tonic-gate 		kmem_free(entries, size);
6300Sstevel@tonic-gate 		rv = CRYPTO_FAILED;
6310Sstevel@tonic-gate 		goto out;
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 	rv = CRYPTO_SUCCESS;
6340Sstevel@tonic-gate out:
6350Sstevel@tonic-gate 	soft_disabled.sd_return_value = rv;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	if (copyout(&soft_disabled, arg, sizeof (soft_disabled)) != 0) {
6380Sstevel@tonic-gate 		error = EFAULT;
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate out2:
6410Sstevel@tonic-gate 	if (audit_active)
6420Sstevel@tonic-gate 		audit_cryptoadm(CRYPTO_LOAD_SOFT_DISABLED, name, entries,
6430Sstevel@tonic-gate 		    count, 0, rv, error);
6440Sstevel@tonic-gate 	return (error);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate /*
6480Sstevel@tonic-gate  * This ioctl loads the supported mechanisms of the specfied cryptographic
6490Sstevel@tonic-gate  * module.  This is so, at boot time, all software providers do not
6500Sstevel@tonic-gate  * have to be opened in order to cause them to register their
6510Sstevel@tonic-gate  * supported mechanisms.
6520Sstevel@tonic-gate  */
6530Sstevel@tonic-gate /* ARGSUSED */
6540Sstevel@tonic-gate static int
6550Sstevel@tonic-gate load_soft_config(dev_t dev, caddr_t arg, int mode, int *rval)
6560Sstevel@tonic-gate {
6570Sstevel@tonic-gate 	crypto_load_soft_config_t soft_config;
6580Sstevel@tonic-gate 	crypto_mech_name_t *entries;
6590Sstevel@tonic-gate 	size_t size;
6600Sstevel@tonic-gate 	uint_t count;
6610Sstevel@tonic-gate 	ulong_t offset;
6620Sstevel@tonic-gate 	char *name;
6630Sstevel@tonic-gate 	uint32_t rv;
6640Sstevel@tonic-gate 	int error = 0;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	if (copyin(arg, &soft_config, sizeof (soft_config)) != 0) {
6670Sstevel@tonic-gate 		error = EFAULT;
6680Sstevel@tonic-gate 		goto out2;
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	name = soft_config.sc_name;
6720Sstevel@tonic-gate 	/* make sure the name is null terminated */
6730Sstevel@tonic-gate 	if (!null_terminated(name)) {
6740Sstevel@tonic-gate 		soft_config.sc_return_value = CRYPTO_ARGUMENTS_BAD;
6750Sstevel@tonic-gate 		if (copyout(&soft_config, arg, sizeof (soft_config)) != 0) {
6760Sstevel@tonic-gate 			return (EFAULT);
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 		return (0);
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	count = soft_config.sc_count;
6820Sstevel@tonic-gate 	if (count == 0) {
6830Sstevel@tonic-gate 		if (crypto_load_soft_config(name, 0, NULL) != 0) {
6840Sstevel@tonic-gate 			rv = CRYPTO_FAILED;
6850Sstevel@tonic-gate 		} else {
6860Sstevel@tonic-gate 			rv = CRYPTO_SUCCESS;
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 		goto out;
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	if (count > KCF_MAXMECHS) {
6920Sstevel@tonic-gate 		rv = CRYPTO_ARGUMENTS_BAD;
6930Sstevel@tonic-gate 		goto out;
6940Sstevel@tonic-gate 	}
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	size = count * sizeof (crypto_mech_name_t);
6970Sstevel@tonic-gate 	entries = kmem_alloc(size, KM_SLEEP);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	offset = offsetof(crypto_load_soft_config_t, sc_list);
7000Sstevel@tonic-gate 	if (copyin(arg + offset, entries, size) != 0) {
7010Sstevel@tonic-gate 		kmem_free(entries, size);
7020Sstevel@tonic-gate 		error = EFAULT;
7030Sstevel@tonic-gate 		goto out2;
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	/*
7070Sstevel@tonic-gate 	 * 'entries' is consumed (but not freed) by
7080Sstevel@tonic-gate 	 * crypto_load_soft_config()
7090Sstevel@tonic-gate 	 */
7100Sstevel@tonic-gate 	if (crypto_load_soft_config(name, count, entries) != 0) {
7110Sstevel@tonic-gate 		kmem_free(entries, size);
7120Sstevel@tonic-gate 		rv = CRYPTO_FAILED;
7130Sstevel@tonic-gate 		goto out;
7140Sstevel@tonic-gate 	}
7150Sstevel@tonic-gate 	rv = CRYPTO_SUCCESS;
7160Sstevel@tonic-gate out:
7170Sstevel@tonic-gate 	soft_config.sc_return_value = rv;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	if (copyout(&soft_config, arg, sizeof (soft_config)) != 0) {
7200Sstevel@tonic-gate 		error = EFAULT;
7210Sstevel@tonic-gate 	}
7220Sstevel@tonic-gate out2:
7230Sstevel@tonic-gate 	if (audit_active)
7240Sstevel@tonic-gate 		audit_cryptoadm(CRYPTO_LOAD_SOFT_CONFIG, name, entries, count,
7250Sstevel@tonic-gate 		    0, rv, error);
7260Sstevel@tonic-gate 	return (error);
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate /*
7300Sstevel@tonic-gate  * This ioctl unloads the specfied cryptographic module and removes
7310Sstevel@tonic-gate  * its table of supported mechanisms.
7320Sstevel@tonic-gate  */
7330Sstevel@tonic-gate /* ARGSUSED */
7340Sstevel@tonic-gate static int
7350Sstevel@tonic-gate unload_soft_module(dev_t dev, caddr_t arg, int mode, int *rval)
7360Sstevel@tonic-gate {
7370Sstevel@tonic-gate 	crypto_unload_soft_module_t unload_soft_module;
7380Sstevel@tonic-gate 	char *name;
7390Sstevel@tonic-gate 	uint32_t rv;
7400Sstevel@tonic-gate 	int error = 0;
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	if (copyin(arg, &unload_soft_module,
7430Sstevel@tonic-gate 	    sizeof (unload_soft_module)) != 0) {
7440Sstevel@tonic-gate 		error = EFAULT;
7450Sstevel@tonic-gate 		goto out2;
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	name = unload_soft_module.sm_name;
7490Sstevel@tonic-gate 	/* make sure the name is null terminated */
7500Sstevel@tonic-gate 	if (!null_terminated(name)) {
7510Sstevel@tonic-gate 		unload_soft_module.sm_return_value = CRYPTO_ARGUMENTS_BAD;
7520Sstevel@tonic-gate 		if (copyout(&unload_soft_module, arg,
7530Sstevel@tonic-gate 		    sizeof (unload_soft_module)) != 0) {
7540Sstevel@tonic-gate 			return (EFAULT);
7550Sstevel@tonic-gate 		}
7560Sstevel@tonic-gate 		return (0);
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	rv = crypto_unload_soft_module(name);
7600Sstevel@tonic-gate out:
7610Sstevel@tonic-gate 	unload_soft_module.sm_return_value = rv;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	if (copyout(&unload_soft_module, arg,
7640Sstevel@tonic-gate 	    sizeof (unload_soft_module)) != 0) {
7650Sstevel@tonic-gate 		error = EFAULT;
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate out2:
7680Sstevel@tonic-gate 	if (audit_active)
7690Sstevel@tonic-gate 		audit_cryptoadm(CRYPTO_UNLOAD_SOFT_MODULE, name, NULL, 0, 0,
7700Sstevel@tonic-gate 		    rv, error);
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	return (error);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate /*
7760Sstevel@tonic-gate  * This ioctl loads a door descriptor into the  kernel.  The descriptor
7770Sstevel@tonic-gate  * is used for module verification.
7780Sstevel@tonic-gate  */
7790Sstevel@tonic-gate /* ARGSUSED */
7800Sstevel@tonic-gate static int
7810Sstevel@tonic-gate load_door(dev_t dev, caddr_t arg, int mode, int *rval)
7820Sstevel@tonic-gate {
7830Sstevel@tonic-gate 	crypto_load_door_t load_door;
7840Sstevel@tonic-gate 	uint32_t rv;
7850Sstevel@tonic-gate 	int error = 0;
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	if (copyin(arg, &load_door, sizeof (crypto_load_door_t)) != 0) {
7880Sstevel@tonic-gate 		error = EFAULT;
7890Sstevel@tonic-gate 		goto out2;
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	if (crypto_load_door(load_door.ld_did) != 0) {
7930Sstevel@tonic-gate 		rv = CRYPTO_FAILED;
7940Sstevel@tonic-gate 		goto out;
7950Sstevel@tonic-gate 	}
7960Sstevel@tonic-gate 	rv = CRYPTO_SUCCESS;
7970Sstevel@tonic-gate out:
7980Sstevel@tonic-gate 	load_door.ld_return_value = rv;
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	if (copyout(&load_door, arg, sizeof (crypto_load_door_t)) != 0)
8010Sstevel@tonic-gate 		error = EFAULT;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate out2:
8040Sstevel@tonic-gate 	if (audit_active)
8050Sstevel@tonic-gate 		audit_cryptoadm(CRYPTO_LOAD_DOOR, NULL, NULL,
8060Sstevel@tonic-gate 		    0, 0, rv, error);
8070Sstevel@tonic-gate 	return (error);
8080Sstevel@tonic-gate }
8090Sstevel@tonic-gate 
810*10500SHai-May.Chao@Sun.COM /*
811*10500SHai-May.Chao@Sun.COM  * This function enables/disables FIPS140 mode or gets the  current
812*10500SHai-May.Chao@Sun.COM  * FIPS140 mode status.
813*10500SHai-May.Chao@Sun.COM  *
814*10500SHai-May.Chao@Sun.COM  * Enable or disable FIPS140 ioctl operation name:
815*10500SHai-May.Chao@Sun.COM  *	FIPS140_ENABLE or FIPS140_DISABLE
816*10500SHai-May.Chao@Sun.COM  *
817*10500SHai-May.Chao@Sun.COM  * Global fips140 mode status in kernel:
818*10500SHai-May.Chao@Sun.COM  *	FIPS140_MODE_ENABLED or FIPS140_MODE_DISABLED
819*10500SHai-May.Chao@Sun.COM  */
820*10500SHai-May.Chao@Sun.COM /* ARGSUSED */
821*10500SHai-May.Chao@Sun.COM static int
822*10500SHai-May.Chao@Sun.COM fips140_actions(dev_t dev, caddr_t arg, int mode, int *rval, int cmd)
823*10500SHai-May.Chao@Sun.COM {
824*10500SHai-May.Chao@Sun.COM 	crypto_fips140_t fips140_info;
825*10500SHai-May.Chao@Sun.COM 	uint32_t rv = CRYPTO_SUCCESS;
826*10500SHai-May.Chao@Sun.COM 	int error = 0;
827*10500SHai-May.Chao@Sun.COM 
828*10500SHai-May.Chao@Sun.COM 	if (copyin(arg, &fips140_info, sizeof (crypto_fips140_t)) != 0)
829*10500SHai-May.Chao@Sun.COM 		return (EFAULT);
830*10500SHai-May.Chao@Sun.COM 
831*10500SHai-May.Chao@Sun.COM 	switch (cmd) {
832*10500SHai-May.Chao@Sun.COM 	case CRYPTO_FIPS140_STATUS:
833*10500SHai-May.Chao@Sun.COM 		fips140_info.fips140_status = global_fips140_mode;
834*10500SHai-May.Chao@Sun.COM 		break;
835*10500SHai-May.Chao@Sun.COM 	case CRYPTO_FIPS140_SET:
836*10500SHai-May.Chao@Sun.COM 		mutex_enter(&fips140_mode_lock);
837*10500SHai-May.Chao@Sun.COM 		if (fips140_info.fips140_op == FIPS140_ENABLE)
838*10500SHai-May.Chao@Sun.COM 			global_fips140_mode = FIPS140_MODE_ENABLED;
839*10500SHai-May.Chao@Sun.COM 		else if (fips140_info.fips140_op == FIPS140_DISABLE)
840*10500SHai-May.Chao@Sun.COM 			global_fips140_mode = FIPS140_MODE_DISABLED;
841*10500SHai-May.Chao@Sun.COM 		else {
842*10500SHai-May.Chao@Sun.COM 			rv = CRYPTO_ARGUMENTS_BAD;
843*10500SHai-May.Chao@Sun.COM 			error = CRYPTO_FAILED;
844*10500SHai-May.Chao@Sun.COM 		}
845*10500SHai-May.Chao@Sun.COM 		mutex_exit(&fips140_mode_lock);
846*10500SHai-May.Chao@Sun.COM 		break;
847*10500SHai-May.Chao@Sun.COM 	}
848*10500SHai-May.Chao@Sun.COM 
849*10500SHai-May.Chao@Sun.COM 	fips140_info.fips140_return_value = rv;
850*10500SHai-May.Chao@Sun.COM 
851*10500SHai-May.Chao@Sun.COM 	if (copyout(&fips140_info, arg, sizeof (crypto_fips140_t)) != 0)
852*10500SHai-May.Chao@Sun.COM 		error = EFAULT;
853*10500SHai-May.Chao@Sun.COM 
854*10500SHai-May.Chao@Sun.COM 	return (error);
855*10500SHai-May.Chao@Sun.COM }
856*10500SHai-May.Chao@Sun.COM 
8570Sstevel@tonic-gate static int
8580Sstevel@tonic-gate cryptoadm_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, cred_t *c,
8590Sstevel@tonic-gate     int *rval)
8600Sstevel@tonic-gate {
8610Sstevel@tonic-gate 	int error;
8620Sstevel@tonic-gate #define	ARG	((caddr_t)arg)
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	switch (cmd) {
8650Sstevel@tonic-gate 	case CRYPTO_LOAD_DEV_DISABLED:
8660Sstevel@tonic-gate 	case CRYPTO_LOAD_SOFT_DISABLED:
8670Sstevel@tonic-gate 	case CRYPTO_LOAD_SOFT_CONFIG:
8680Sstevel@tonic-gate 	case CRYPTO_UNLOAD_SOFT_MODULE:
8690Sstevel@tonic-gate 	case CRYPTO_POOL_CREATE:
8700Sstevel@tonic-gate 	case CRYPTO_POOL_WAIT:
8710Sstevel@tonic-gate 	case CRYPTO_POOL_RUN:
8720Sstevel@tonic-gate 	case CRYPTO_LOAD_DOOR:
873*10500SHai-May.Chao@Sun.COM 	case CRYPTO_FIPS140_SET:
8740Sstevel@tonic-gate 		if ((error = drv_priv(c)) != 0)
8750Sstevel@tonic-gate 			return (error);
8760Sstevel@tonic-gate 	default:
8770Sstevel@tonic-gate 		break;
8780Sstevel@tonic-gate 	}
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	switch (cmd) {
8810Sstevel@tonic-gate 	case CRYPTO_GET_DEV_LIST:
8820Sstevel@tonic-gate 		return (get_dev_list(dev, ARG, mode, rval));
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	case CRYPTO_GET_DEV_INFO:
8850Sstevel@tonic-gate 		return (get_dev_info(dev, ARG, mode, rval));
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	case CRYPTO_GET_SOFT_LIST:
8880Sstevel@tonic-gate 		return (get_soft_list(dev, ARG, mode, rval));
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	case CRYPTO_GET_SOFT_INFO:
8910Sstevel@tonic-gate 		return (get_soft_info(dev, ARG, mode, rval));
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	case CRYPTO_LOAD_DEV_DISABLED:
8940Sstevel@tonic-gate 		return (load_dev_disabled(dev, ARG, mode, rval));
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	case CRYPTO_LOAD_SOFT_DISABLED:
8970Sstevel@tonic-gate 		return (load_soft_disabled(dev, ARG, mode, rval));
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	case CRYPTO_LOAD_SOFT_CONFIG:
9000Sstevel@tonic-gate 		return (load_soft_config(dev, ARG, mode, rval));
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	case CRYPTO_UNLOAD_SOFT_MODULE:
9030Sstevel@tonic-gate 		return (unload_soft_module(dev, ARG, mode, rval));
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	case CRYPTO_POOL_CREATE:
9060Sstevel@tonic-gate 		/*
9070Sstevel@tonic-gate 		 * The framework allocates and initializes the pool.
9080Sstevel@tonic-gate 		 * So, this is a no op. We are keeping this ioctl around
9090Sstevel@tonic-gate 		 * to be used for any future threadpool related work.
9100Sstevel@tonic-gate 		 */
9110Sstevel@tonic-gate 		if (audit_active)
9120Sstevel@tonic-gate 			audit_cryptoadm(CRYPTO_POOL_CREATE, NULL, NULL,
9130Sstevel@tonic-gate 			    0, 0, 0, 0);
9140Sstevel@tonic-gate 		return (0);
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	case CRYPTO_POOL_WAIT: {
9170Sstevel@tonic-gate 		int nthrs = 0, err;
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 		if ((err = kcf_svc_wait(&nthrs)) == 0) {
9200Sstevel@tonic-gate 			if (copyout((caddr_t)&nthrs, ARG, sizeof (int))
9210Sstevel@tonic-gate 			    == -1)
9220Sstevel@tonic-gate 				err = EFAULT;
9230Sstevel@tonic-gate 		}
9240Sstevel@tonic-gate 		if (audit_active)
9250Sstevel@tonic-gate 			audit_cryptoadm(CRYPTO_POOL_WAIT, NULL, NULL,
9260Sstevel@tonic-gate 			    0, 0, 0, err);
9270Sstevel@tonic-gate 		return (err);
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	case CRYPTO_POOL_RUN: {
9310Sstevel@tonic-gate 		int err;
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 		err = kcf_svc_do_run();
9340Sstevel@tonic-gate 		if (audit_active)
9350Sstevel@tonic-gate 			audit_cryptoadm(CRYPTO_POOL_RUN, NULL, NULL,
9360Sstevel@tonic-gate 			    0, 0, 0, err);
9370Sstevel@tonic-gate 		return (err);
9380Sstevel@tonic-gate 	}
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	case CRYPTO_LOAD_DOOR:
9410Sstevel@tonic-gate 		return (load_door(dev, ARG, mode, rval));
942*10500SHai-May.Chao@Sun.COM 	case CRYPTO_FIPS140_STATUS:
943*10500SHai-May.Chao@Sun.COM 		return (fips140_actions(dev, ARG, mode, rval, cmd));
944*10500SHai-May.Chao@Sun.COM 	case CRYPTO_FIPS140_SET: {
945*10500SHai-May.Chao@Sun.COM 		int err;
946*10500SHai-May.Chao@Sun.COM 
947*10500SHai-May.Chao@Sun.COM 		err = fips140_actions(dev, ARG, mode, rval, cmd);
948*10500SHai-May.Chao@Sun.COM 		if (audit_active)
949*10500SHai-May.Chao@Sun.COM 			audit_cryptoadm(CRYPTO_FIPS140_SET, NULL, NULL,
950*10500SHai-May.Chao@Sun.COM 			    0, 0, 0, err);
951*10500SHai-May.Chao@Sun.COM 		return (err);
9520Sstevel@tonic-gate 	}
953*10500SHai-May.Chao@Sun.COM 	}
954*10500SHai-May.Chao@Sun.COM 
9550Sstevel@tonic-gate 	return (EINVAL);
9560Sstevel@tonic-gate }
957