xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_init.c (revision 34bbc83afbf22a6f8e504cb99d76c97c017cb5f4)
1da6c28aaSamw /*
2da6c28aaSamw  * CDDL HEADER START
3da6c28aaSamw  *
4da6c28aaSamw  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
7da6c28aaSamw  *
8da6c28aaSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aaSamw  * or http://www.opensolaris.org/os/licensing.
10da6c28aaSamw  * See the License for the specific language governing permissions
11da6c28aaSamw  * and limitations under the License.
12da6c28aaSamw  *
13da6c28aaSamw  * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aaSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aaSamw  * If applicable, add the following below this CDDL HEADER, with the
16da6c28aaSamw  * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aaSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aaSamw  *
19da6c28aaSamw  * CDDL HEADER END
20da6c28aaSamw  */
21da6c28aaSamw /*
22148c5f43SAlan Wright  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
2312b65585SGordon Ross  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
24a02a2451SGordon Ross  * Copyright 2015-2023 RackTop Systems, Inc.
25e83c5562SJason King  * Copyright 2019 Joyent, Inc.
26da6c28aaSamw  */
27da6c28aaSamw 
28da6c28aaSamw #include <sys/types.h>
29b819cea2SGordon Ross #include <sys/conf.h>
30da6c28aaSamw #include <sys/ddi.h>
31da6c28aaSamw #include <sys/modctl.h>
32da6c28aaSamw #include <sys/cred.h>
3308344b29SGordon Ross #include <sys/disp.h>
34*34bbc83aSGordon Ross #include <sys/id_space.h>
35da6c28aaSamw #include <sys/ioccom.h>
36da6c28aaSamw #include <sys/policy.h>
3794fff790SAlan Wright #include <sys/cmn_err.h>
38bbf6f00cSJordan Brown #include <smbsrv/smb_kproto.h>
39da6c28aaSamw #include <smbsrv/smb_ioctl.h>
40faa1795aSjb150015 
41b819cea2SGordon Ross #ifdef	_FAKE_KERNEL
42b819cea2SGordon Ross #error	"See libfksmbsrv"
43b819cea2SGordon Ross #endif	/* _FAKE_KERNEL */
44b819cea2SGordon Ross 
45faa1795aSjb150015 static int smb_drv_open(dev_t *, int, int, cred_t *);
46*34bbc83aSGordon Ross static int smb_drv_open_ctl(dev_t *, int, int, cred_t *);
47*34bbc83aSGordon Ross static int smb_drv_open_lib(dev_t *, int, int, cred_t *);
48faa1795aSjb150015 static int smb_drv_close(dev_t, int, int, cred_t *);
49faa1795aSjb150015 static int smb_drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
50da6c28aaSamw static int smb_drv_attach(dev_info_t *, ddi_attach_cmd_t);
51da6c28aaSamw static int smb_drv_detach(dev_info_t *, ddi_detach_cmd_t);
52da6c28aaSamw static int smb_drv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
53da6c28aaSamw 
54*34bbc83aSGordon Ross 
55*34bbc83aSGordon Ross 
56da6c28aaSamw /*
57faa1795aSjb150015  * *****************************************************************************
58faa1795aSjb150015  * ****************************** Global Variables *****************************
59faa1795aSjb150015  * *****************************************************************************
60faa1795aSjb150015  *
61faa1795aSjb150015  * These variables can only be changed through the /etc/system file.
62da6c28aaSamw  */
63faa1795aSjb150015 
64faa1795aSjb150015 /*
65faa1795aSjb150015  * Maximum buffer size for NT: configurable based on the client environment.
66faa1795aSjb150015  * IR104720 Experiments with Windows 2000 indicate that we achieve better
67faa1795aSjb150015  * SmbWriteX performance with a buffer size of 64KB instead of the 37KB used
68faa1795aSjb150015  * with Windows NT4.0. Previous experiments with NT4.0 resulted in directory
69faa1795aSjb150015  * listing problems so this buffer size is configurable based on the end-user
70faa1795aSjb150015  * environment. When in doubt use 37KB.
71faa1795aSjb150015  */
72faa1795aSjb150015 int	smb_maxbufsize = SMB_NT_MAXBUF;
73faa1795aSjb150015 int	smb_flush_required = 1;
74faa1795aSjb150015 int	smb_dirsymlink_enable = 1;
75c8ec8eeaSjose borrego int	smb_sign_debug = 0;
762c2961f8Sjose borrego uint_t	smb_audit_flags =
772c2961f8Sjose borrego #ifdef	DEBUG
782c2961f8Sjose borrego     SMB_AUDIT_NODE;
792c2961f8Sjose borrego #else
802c2961f8Sjose borrego     0;
812c2961f8Sjose borrego #endif
82faa1795aSjb150015 
83e3c4f75eSGordon Ross int smb_allow_advisory_locks = 0;	/* See smb_vops.c */
84e3c4f75eSGordon Ross 
85faa1795aSjb150015 /*
86cb174861Sjoyce mcintosh  * Maximum number of simultaneous authentication, share mapping, pipe open
87cb174861Sjoyce mcintosh  * requests to be processed.
88cb174861Sjoyce mcintosh  */
8912b65585SGordon Ross int	smb_ssetup_threshold = SMB_AUTHSVC_MAXTHREAD;
90cb174861Sjoyce mcintosh int	smb_tcon_threshold = 1024;
91cb174861Sjoyce mcintosh int	smb_opipe_threshold = 1024;
92a9609934SGordon Ross int	smb_logoff_threshold = 1024;
93cb174861Sjoyce mcintosh 
94cb174861Sjoyce mcintosh /*
95cb174861Sjoyce mcintosh  * Number of milliseconds that a request will be stalled if it comes in after
96cb174861Sjoyce mcintosh  * the maximum number of inflight operations are being proccessed.
97cb174861Sjoyce mcintosh  */
98cb174861Sjoyce mcintosh int	smb_ssetup_timeout = (30 * 1000);
99cb174861Sjoyce mcintosh int	smb_tcon_timeout = (30 * 1000);
100cb174861Sjoyce mcintosh int	smb_opipe_timeout = (30 * 1000);
101a9609934SGordon Ross int	smb_logoff_timeout = (600 * 1000);
102cb174861Sjoyce mcintosh 
10308344b29SGordon Ross /*
104a02a2451SGordon Ross  * Thread priorities used in smbsrv.
105a02a2451SGordon Ross  *
106a02a2451SGordon Ross  * The SMB server runs at a priority a little below the maximum for
107a02a2451SGordon Ross  * user-level process threads so it won't monopolize the CPU.
108a02a2451SGordon Ross  * Todo: make this configurable
109a02a2451SGordon Ross  *
110a02a2451SGordon Ross  * Aside from that, we want these relative priorities: (a) timers,
111a02a2451SGordon Ross  * (b) notify + oplock completions, (c) workers, (d) receivers, etc.
112a02a2451SGordon Ross  * The "base" is somewhat arbirary, and what shows up in prstat
113a02a2451SGordon Ross  * because it's used for the main thread in newproc().
11408344b29SGordon Ross  */
115a02a2451SGordon Ross int smbsrv_timer_pri	= MINCLSYSPRI;		/* smb_server_timers */
116a02a2451SGordon Ross int smbsrv_base_pri	= MINCLSYSPRI - 1;	/* kshare thread, newproc */
117a02a2451SGordon Ross int smbsrv_notify_pri	= MINCLSYSPRI - 1;	/* oplocks, notify */
118a02a2451SGordon Ross /* Gap in which user-level administrative stuff runs. */
119a02a2451SGordon Ross int smbsrv_worker_pri	= MINCLSYSPRI - 7;
120a02a2451SGordon Ross int smbsrv_receive_pri	= MINCLSYSPRI - 8;
121a02a2451SGordon Ross int smbsrv_listen_pri	= MINCLSYSPRI - 9;
12208344b29SGordon Ross 
123cb174861Sjoyce mcintosh 
124cb174861Sjoyce mcintosh /*
125faa1795aSjb150015  * *****************************************************************************
126faa1795aSjb150015  * ********************** Static Variables / Module Linkage ********************
127faa1795aSjb150015  * *****************************************************************************
128faa1795aSjb150015  */
129faa1795aSjb150015 
130da6c28aaSamw static struct cb_ops cbops = {
131da6c28aaSamw 	smb_drv_open,		/* cb_open */
132da6c28aaSamw 	smb_drv_close,		/* cb_close */
133da6c28aaSamw 	nodev,			/* cb_strategy */
134da6c28aaSamw 	nodev,			/* cb_print */
135da6c28aaSamw 	nodev,			/* cb_dump */
136da6c28aaSamw 	nodev,			/* cb_read */
137da6c28aaSamw 	nodev,			/* cb_write */
138da6c28aaSamw 	smb_drv_ioctl,		/* cb_ioctl */
139da6c28aaSamw 	nodev,			/* cb_devmap */
140da6c28aaSamw 	nodev,			/* cb_mmap */
141da6c28aaSamw 	nodev,			/* cb_segmap */
142da6c28aaSamw 	nochpoll,		/* cb_chpoll */
143da6c28aaSamw 	ddi_prop_op,		/* cb_prop_op */
144da6c28aaSamw 	NULL,			/* cb_streamtab */
145da6c28aaSamw 	D_MP,			/* cb_flag */
146da6c28aaSamw 	CB_REV,			/* cb_rev */
147da6c28aaSamw 	nodev,			/* cb_aread */
148da6c28aaSamw 	nodev,			/* cb_awrite */
149da6c28aaSamw };
150da6c28aaSamw 
151da6c28aaSamw static struct dev_ops devops = {
152da6c28aaSamw 	DEVO_REV,		/* devo_rev */
153da6c28aaSamw 	0,			/* devo_refcnt */
154da6c28aaSamw 	smb_drv_getinfo,	/* devo_getinfo */
155da6c28aaSamw 	nulldev,		/* devo_identify */
156da6c28aaSamw 	nulldev,		/* devo_probe */
157da6c28aaSamw 	smb_drv_attach,		/* devo_attach */
158da6c28aaSamw 	smb_drv_detach,		/* devo_detach */
159da6c28aaSamw 	nodev,			/* devo_reset */
160da6c28aaSamw 	&cbops,			/* devo_cb_ops */
161da6c28aaSamw 	NULL,			/* devo_bus_ops */
162da6c28aaSamw 	NULL,			/* devo_power */
16319397407SSherry Moore 	ddi_quiesce_not_needed,		/* devo_quiesce */
164da6c28aaSamw };
165da6c28aaSamw 
166da6c28aaSamw static struct modldrv modldrv = {
167da6c28aaSamw 	&mod_driverops,					/* drv_modops */
16819397407SSherry Moore 	"CIFS Server Protocol",				/* drv_linkinfo */
169da6c28aaSamw 	&devops,
170da6c28aaSamw };
171da6c28aaSamw 
172da6c28aaSamw static struct modlinkage modlinkage = {
173da6c28aaSamw 	MODREV_1,	/* revision of the module, must be: MODREV_1	*/
174da6c28aaSamw 	&modldrv,	/* ptr to linkage structures			*/
175da6c28aaSamw 	NULL,
176da6c28aaSamw };
177da6c28aaSamw 
178da6c28aaSamw static dev_info_t *smb_drv_dip = NULL;
179*34bbc83aSGordon Ross static id_space_t *smb_drv_minors = NULL;
180da6c28aaSamw 
181da6c28aaSamw /*
182faa1795aSjb150015  * ****************************************************************************
183faa1795aSjb150015  *				    Module Interface
184faa1795aSjb150015  * ****************************************************************************
185da6c28aaSamw  */
186da6c28aaSamw 
187da6c28aaSamw int
_init(void)188da6c28aaSamw _init(void)
189da6c28aaSamw {
190f9a15d2cSjose borrego 	int rc;
191f9a15d2cSjose borrego 
1928622ec45SGordon Ross 	if ((rc = smb_server_g_init()) != 0) {
193148c5f43SAlan Wright 		return (rc);
194148c5f43SAlan Wright 	}
195148c5f43SAlan Wright 
196148c5f43SAlan Wright 	if ((rc = mod_install(&modlinkage)) != 0) {
197a90cf9f2SGordon Ross 		smb_server_g_fini();
198f9a15d2cSjose borrego 	}
199148c5f43SAlan Wright 
200f9a15d2cSjose borrego 	return (rc);
201da6c28aaSamw }
202da6c28aaSamw 
203da6c28aaSamw int
_info(struct modinfo * modinfop)204da6c28aaSamw _info(struct modinfo *modinfop)
205da6c28aaSamw {
206da6c28aaSamw 	return (mod_info(&modlinkage, modinfop));
207da6c28aaSamw }
208da6c28aaSamw 
209da6c28aaSamw int
_fini(void)210da6c28aaSamw _fini(void)
211da6c28aaSamw {
212f9a15d2cSjose borrego 	int	rc;
213f9a15d2cSjose borrego 
214a90cf9f2SGordon Ross 	if (smb_server_get_count() != 0)
215a90cf9f2SGordon Ross 		return (EBUSY);
216a90cf9f2SGordon Ross 
217148c5f43SAlan Wright 	if ((rc = mod_remove(&modlinkage)) == 0) {
218a90cf9f2SGordon Ross 		smb_server_g_fini();
219148c5f43SAlan Wright 	}
220148c5f43SAlan Wright 
221f9a15d2cSjose borrego 	return (rc);
222da6c28aaSamw }
223da6c28aaSamw 
224faa1795aSjb150015 /*
225faa1795aSjb150015  * ****************************************************************************
226faa1795aSjb150015  *				Pseudo Device Entry Points
227faa1795aSjb150015  * ****************************************************************************
228faa1795aSjb150015  */
229faa1795aSjb150015 /* ARGSUSED */
230faa1795aSjb150015 static int
smb_drv_open(dev_t * devp,int flag,int otyp,cred_t * cr)2318622ec45SGordon Ross smb_drv_open(dev_t *devp, int flag, int otyp, cred_t *cr)
232faa1795aSjb150015 {
233*34bbc83aSGordon Ross 	int rc;
234*34bbc83aSGordon Ross 	minor_t m = getminor(*devp);
235*34bbc83aSGordon Ross 
236*34bbc83aSGordon Ross 	/* See ddi_create_minor_node below */
237*34bbc83aSGordon Ross 	switch (m) {
238*34bbc83aSGordon Ross 	case 0: /* smbsrv (smbd only) */
239*34bbc83aSGordon Ross 		rc = smb_drv_open_ctl(devp, flag, otyp, cr);
240*34bbc83aSGordon Ross 		break;
241*34bbc83aSGordon Ross 	case 1: /* smbsrv1 (lib access) */
242*34bbc83aSGordon Ross 		rc = smb_drv_open_lib(devp, flag, otyp, cr);
243*34bbc83aSGordon Ross 		break;
244*34bbc83aSGordon Ross 	default:
245*34bbc83aSGordon Ross 		rc = ENXIO;
246*34bbc83aSGordon Ross 		break;
247*34bbc83aSGordon Ross 	}
248*34bbc83aSGordon Ross 	return (rc);
249*34bbc83aSGordon Ross }
250*34bbc83aSGordon Ross 
251*34bbc83aSGordon Ross /*
252*34bbc83aSGordon Ross  * The smbsrvctl device is exclusively for smbd.
253*34bbc83aSGordon Ross  * On open, this creates an smb_server_t instance.
254*34bbc83aSGordon Ross  * Always exclusive open here.
255*34bbc83aSGordon Ross  */
256*34bbc83aSGordon Ross static int
smb_drv_open_ctl(dev_t * devp,int flag,int otyp,cred_t * cr)257*34bbc83aSGordon Ross smb_drv_open_ctl(dev_t *devp, int flag, int otyp, cred_t *cr)
258*34bbc83aSGordon Ross {
259*34bbc83aSGordon Ross 	dev_t clone;
260*34bbc83aSGordon Ross 	minor_t mi;
261*34bbc83aSGordon Ross 	int rc;
2628622ec45SGordon Ross 
263faa1795aSjb150015 	/*
264faa1795aSjb150015 	 * Check caller's privileges.
265faa1795aSjb150015 	 */
2668622ec45SGordon Ross 	if (secpolicy_smb(cr) != 0)
267*34bbc83aSGordon Ross 		return (SET_ERROR(EPERM));
268faa1795aSjb150015 
269*34bbc83aSGordon Ross 	mi = id_allocff(smb_drv_minors);
270*34bbc83aSGordon Ross 	clone = makedevice(getmajor(*devp), mi);
2718622ec45SGordon Ross 
2728622ec45SGordon Ross 	/*
273faa1795aSjb150015 	 * Start SMB service state machine
274*34bbc83aSGordon Ross 	 * Note: sets sv->sv_dev = clone
275faa1795aSjb150015 	 */
276*34bbc83aSGordon Ross 	rc = smb_server_create(clone);
277*34bbc83aSGordon Ross 	if (rc == 0) {
278*34bbc83aSGordon Ross 		*devp = clone;
279*34bbc83aSGordon Ross 	} else {
280*34bbc83aSGordon Ross 		/* Open fails, eg EBUSY */
281*34bbc83aSGordon Ross 		id_free(smb_drv_minors, mi);
282faa1795aSjb150015 	}
283faa1795aSjb150015 
284*34bbc83aSGordon Ross 	return (rc);
285*34bbc83aSGordon Ross }
286*34bbc83aSGordon Ross 
287*34bbc83aSGordon Ross /*
288*34bbc83aSGordon Ross  * The smbsrv device is for library access to smbsrv state.
289*34bbc83aSGordon Ross  * Multiple open instances are allowed (clone-open).
290*34bbc83aSGordon Ross  */
291*34bbc83aSGordon Ross static int
smb_drv_open_lib(dev_t * devp,int flag,int otyp,cred_t * cr)292*34bbc83aSGordon Ross smb_drv_open_lib(dev_t *devp, int flag, int otyp, cred_t *cr)
293*34bbc83aSGordon Ross {
294*34bbc83aSGordon Ross 	minor_t mi;
295*34bbc83aSGordon Ross 
296*34bbc83aSGordon Ross 	mi = id_allocff(smb_drv_minors);
297*34bbc83aSGordon Ross 	*devp = makedevice(getmajor(*devp), mi);
298*34bbc83aSGordon Ross 
299*34bbc83aSGordon Ross 	return (0);
300*34bbc83aSGordon Ross }
301*34bbc83aSGordon Ross 
302*34bbc83aSGordon Ross /*
303*34bbc83aSGordon Ross  * Close on unit zero (detected as: sv->sv_dev == dev)
304*34bbc83aSGordon Ross  * destroys the smb_server_t instance.
305*34bbc83aSGordon Ross  */
306*34bbc83aSGordon Ross /*
307*34bbc83aSGordon Ross  * The smbd process keeps the control device open for the life of
308*34bbc83aSGordon Ross  * smbd (service process).  We know the control device is closing
309*34bbc83aSGordon Ross  * when the device passed to close matches the server sv_dev.
310*34bbc83aSGordon Ross  * When the control device closes, destroy the kernel smb_server_t
311*34bbc83aSGordon Ross  */
312faa1795aSjb150015 /* ARGSUSED */
313faa1795aSjb150015 static int
smb_drv_close(dev_t dev,int flag,int otyp,cred_t * credp)314faa1795aSjb150015 smb_drv_close(dev_t dev, int flag, int otyp, cred_t *credp)
315faa1795aSjb150015 {
3168d94f651SGordon Ross 	smb_server_t	*sv;
3178d94f651SGordon Ross 
318*34bbc83aSGordon Ross 	if (smb_server_lookup(&sv) == 0) {
319*34bbc83aSGordon Ross 		if (sv->sv_dev == dev) {
320*34bbc83aSGordon Ross 			/* Note releases the ref on sv. */
321*34bbc83aSGordon Ross 			(void) smb_server_delete(sv);
322*34bbc83aSGordon Ross 		} else {
323*34bbc83aSGordon Ross 			smb_server_release(sv);
324*34bbc83aSGordon Ross 		}
325*34bbc83aSGordon Ross 	}
326*34bbc83aSGordon Ross 	id_free(smb_drv_minors, getminor(dev));
3278d94f651SGordon Ross 
328*34bbc83aSGordon Ross 	return (0);
329faa1795aSjb150015 }
330faa1795aSjb150015 
331faa1795aSjb150015 /* ARGSUSED */
332faa1795aSjb150015 static int
smb_drv_ioctl(dev_t dev,int cmd,intptr_t argp,int flags,cred_t * cred,int * retval)333*34bbc83aSGordon Ross smb_drv_ioctl(dev_t dev, int cmd, intptr_t argp, int flags, cred_t *cred,
334faa1795aSjb150015     int *retval)
335faa1795aSjb150015 {
33629bd2886SAlan Wright 	smb_ioc_header_t ioc_hdr;
337*34bbc83aSGordon Ross 	smb_ioc_t	*ioc;
338*34bbc83aSGordon Ross 	smb_server_t	*sv = NULL;
33929bd2886SAlan Wright 	uint32_t	crc;
34029bd2886SAlan Wright 	boolean_t	copyout = B_FALSE;
341faa1795aSjb150015 	int		rc = 0;
3425f3b52abSGordon Ross 	size_t		alloclen;
343faa1795aSjb150015 
3445f3b52abSGordon Ross 	if (ddi_copyin((void *)argp, &ioc_hdr, sizeof (ioc_hdr), flags))
345*34bbc83aSGordon Ross 		return (SET_ERROR(EFAULT));
346faa1795aSjb150015 
3475f3b52abSGordon Ross 	/*
3485f3b52abSGordon Ross 	 * Check version and length.
3495f3b52abSGordon Ross 	 *
3505f3b52abSGordon Ross 	 * Note that some ioctls (i.e. SMB_IOC_SVCENUM) have payload
3515f3b52abSGordon Ross 	 * data after the ioctl struct, in which case they specify a
3525f3b52abSGordon Ross 	 * length much larger than sizeof smb_ioc_t.  The theoretical
3535f3b52abSGordon Ross 	 * largest ioctl data is therefore the size of the union plus
3545f3b52abSGordon Ross 	 * the max size of the payload (which is SMB_IOC_DATA_SIZE).
3555f3b52abSGordon Ross 	 */
3565f3b52abSGordon Ross 	if (ioc_hdr.version != SMB_IOC_VERSION ||
3575f3b52abSGordon Ross 	    ioc_hdr.len < sizeof (ioc_hdr) ||
3585f3b52abSGordon Ross 	    ioc_hdr.len > (sizeof (*ioc) + SMB_IOC_DATA_SIZE))
359*34bbc83aSGordon Ross 		return (SET_ERROR(EINVAL));
3605f3b52abSGordon Ross 
36129bd2886SAlan Wright 	crc = ioc_hdr.crc;
36229bd2886SAlan Wright 	ioc_hdr.crc = 0;
36329bd2886SAlan Wright 	if (smb_crc_gen((uint8_t *)&ioc_hdr, sizeof (ioc_hdr)) != crc)
364*34bbc83aSGordon Ross 		return (SET_ERROR(EINVAL));
36594fff790SAlan Wright 
3665f3b52abSGordon Ross 	/*
3675f3b52abSGordon Ross 	 * Note that smb_ioc_t is a union, and callers set ioc_hdr.len
3685f3b52abSGordon Ross 	 * to the size of the actual union arm.  If some caller were to
3695f3b52abSGordon Ross 	 * set that size too small, we could end up passing under-sized
3705f3b52abSGordon Ross 	 * memory to one of the type-specific handler functions.  Avoid
3715f3b52abSGordon Ross 	 * that problem by allocating at least the size of the union,
3725f3b52abSGordon Ross 	 * (zeroed out) and then copy in the caller specified length.
3735f3b52abSGordon Ross 	 */
3745f3b52abSGordon Ross 	alloclen = MAX(ioc_hdr.len, sizeof (*ioc));
3755f3b52abSGordon Ross 	ioc = kmem_zalloc(alloclen, KM_SLEEP);
3765f3b52abSGordon Ross 	if (ddi_copyin((void *)argp, ioc, ioc_hdr.len, flags)) {
377*34bbc83aSGordon Ross 		rc = SET_ERROR(EFAULT);
378*34bbc83aSGordon Ross 		goto out;
37929bd2886SAlan Wright 	}
38029bd2886SAlan Wright 
381e83c5562SJason King 	/* Don't allow the request size to change mid-ioctl */
382e83c5562SJason King 	if (ioc_hdr.len != ioc->ioc_hdr.len) {
383*34bbc83aSGordon Ross 		rc = SET_ERROR(EINVAL);
384*34bbc83aSGordon Ross 		goto out;
385e83c5562SJason King 	}
386e83c5562SJason King 
387*34bbc83aSGordon Ross 	rc = smb_server_lookup(&sv);
388*34bbc83aSGordon Ross 	if (rc != 0) {
389*34bbc83aSGordon Ross 		sv = NULL;
390*34bbc83aSGordon Ross 		goto out;
391*34bbc83aSGordon Ross 	}
392*34bbc83aSGordon Ross 
393*34bbc83aSGordon Ross 	/*
394*34bbc83aSGordon Ross 	 * Access control by category of ioctl codes, based on
395*34bbc83aSGordon Ross 	 * which device was opened, and privilege checks.
396*34bbc83aSGordon Ross 	 */
397*34bbc83aSGordon Ross 	switch (cmd) {
398*34bbc83aSGordon Ross 	case SMB_IOC_NUMOPEN:
399*34bbc83aSGordon Ross 	case SMB_IOC_SVCENUM:
400*34bbc83aSGordon Ross 		/*
401*34bbc83aSGordon Ross 		 * Non-modifying ops. no special priv.
402*34bbc83aSGordon Ross 		 * beyond dev open permissions.
403*34bbc83aSGordon Ross 		 */
404*34bbc83aSGordon Ross 		break;
405*34bbc83aSGordon Ross 
406*34bbc83aSGordon Ross 	case SMB_IOC_FILE_CLOSE:
407*34bbc83aSGordon Ross 	case SMB_IOC_SESSION_CLOSE:
408*34bbc83aSGordon Ross 		/*
409*34bbc83aSGordon Ross 		 * Modifying ops. Require privilege
410*34bbc83aSGordon Ross 		 * (chose one smbd normally has)
411*34bbc83aSGordon Ross 		 */
412*34bbc83aSGordon Ross 		if ((rc = secpolicy_basic_proc(cred)) != 0)
413*34bbc83aSGordon Ross 			goto out;
414*34bbc83aSGordon Ross 		break;
415*34bbc83aSGordon Ross 	default:
416*34bbc83aSGordon Ross 		/*
417*34bbc83aSGordon Ross 		 * The rest are only allowed on the control device.
418*34bbc83aSGordon Ross 		 * Note: secpolicy_smb checked in open.
419*34bbc83aSGordon Ross 		 */
420*34bbc83aSGordon Ross 		if (sv->sv_dev != dev) {
421*34bbc83aSGordon Ross 			rc = SET_ERROR(EPERM);
422*34bbc83aSGordon Ross 			goto out;
423*34bbc83aSGordon Ross 		}
424*34bbc83aSGordon Ross 		break;
425*34bbc83aSGordon Ross 	}
426*34bbc83aSGordon Ross 
427*34bbc83aSGordon Ross 	/*
428*34bbc83aSGordon Ross 	 * See similar in libfksmbrv fksmbsrv_drv_ioctl()
429*34bbc83aSGordon Ross 	 */
430faa1795aSjb150015 	switch (cmd) {
431faa1795aSjb150015 	case SMB_IOC_CONFIG:
432*34bbc83aSGordon Ross 		rc = smb_server_configure(sv, &ioc->ioc_cfg);
433faa1795aSjb150015 		break;
434faa1795aSjb150015 	case SMB_IOC_START:
435*34bbc83aSGordon Ross 		rc = smb_server_start(sv, &ioc->ioc_start);
436faa1795aSjb150015 		break;
4379fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	case SMB_IOC_STOP:
438*34bbc83aSGordon Ross 		rc = smb_server_stop(sv);
4399fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		break;
4409fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	case SMB_IOC_EVENT:
441*34bbc83aSGordon Ross 		rc = smb_server_notify_event(sv, &ioc->ioc_event);
4429fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 		break;
443faa1795aSjb150015 	case SMB_IOC_GMTOFF:
444*34bbc83aSGordon Ross 		rc = smb_server_set_gmtoff(sv, &ioc->ioc_gmt);
44529bd2886SAlan Wright 		break;
44629bd2886SAlan Wright 	case SMB_IOC_SHARE:
447*34bbc83aSGordon Ross 		rc = smb_kshare_export_list(sv, &ioc->ioc_share);
44829bd2886SAlan Wright 		break;
44929bd2886SAlan Wright 	case SMB_IOC_UNSHARE:
450*34bbc83aSGordon Ross 		rc = smb_kshare_unexport_list(sv, &ioc->ioc_share);
45129bd2886SAlan Wright 		break;
452cb174861Sjoyce mcintosh 	case SMB_IOC_SHAREINFO:
453*34bbc83aSGordon Ross 		rc = smb_kshare_info(sv, &ioc->ioc_shareinfo);
454cb174861Sjoyce mcintosh 		copyout = B_TRUE;
455cb174861Sjoyce mcintosh 		break;
45697264293SGordon Ross 	case SMB_IOC_SHAREACCESS:
457*34bbc83aSGordon Ross 		rc = smb_kshare_access(sv, &ioc->ioc_shareaccess);
45897264293SGordon Ross 		break;
4591fcced4cSJordan Brown 	case SMB_IOC_NUMOPEN:
460*34bbc83aSGordon Ross 		rc = smb_server_numopen(sv, &ioc->ioc_opennum);
46129bd2886SAlan Wright 		copyout = B_TRUE;
46229bd2886SAlan Wright 		break;
4631fcced4cSJordan Brown 	case SMB_IOC_SVCENUM:
464*34bbc83aSGordon Ross 		rc = smb_server_enum(sv, &ioc->ioc_svcenum);
46529bd2886SAlan Wright 		copyout = B_TRUE;
466faa1795aSjb150015 		break;
4671fcced4cSJordan Brown 	case SMB_IOC_SESSION_CLOSE:
468*34bbc83aSGordon Ross 		rc = smb_server_session_close(sv, &ioc->ioc_session);
4691fcced4cSJordan Brown 		break;
4701fcced4cSJordan Brown 	case SMB_IOC_FILE_CLOSE:
471*34bbc83aSGordon Ross 		rc = smb_server_file_close(sv, &ioc->ioc_fileid);
4721fcced4cSJordan Brown 		break;
473cb174861Sjoyce mcintosh 	case SMB_IOC_SPOOLDOC:
474*34bbc83aSGordon Ross 		rc = smb_server_spooldoc(sv, &ioc->ioc_spooldoc);
475cb174861Sjoyce mcintosh 		copyout = B_TRUE;
476cb174861Sjoyce mcintosh 		break;
477faa1795aSjb150015 	default:
478*34bbc83aSGordon Ross 		rc = SET_ERROR(ENOTTY);
479faa1795aSjb150015 		break;
480faa1795aSjb150015 	}
48129bd2886SAlan Wright 	if ((rc == 0) && copyout) {
4825f3b52abSGordon Ross 		if (ddi_copyout(ioc, (void *)argp, ioc_hdr.len, flags))
483*34bbc83aSGordon Ross 			rc = SET_ERROR(EFAULT);
48429bd2886SAlan Wright 	}
485*34bbc83aSGordon Ross out:
486*34bbc83aSGordon Ross 	if (sv != NULL)
487*34bbc83aSGordon Ross 		smb_server_release(sv);
4885f3b52abSGordon Ross 	kmem_free(ioc, alloclen);
489da6c28aaSamw 	return (rc);
490da6c28aaSamw }
491da6c28aaSamw 
492da6c28aaSamw /*
493faa1795aSjb150015  * ****************************************************************************
494faa1795aSjb150015  *				Pseudo Device Operations
495faa1795aSjb150015  * ****************************************************************************
496da6c28aaSamw  */
497faa1795aSjb150015 static int
smb_drv_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)498faa1795aSjb150015 smb_drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
499faa1795aSjb150015 {
500*34bbc83aSGordon Ross 	if (cmd != DDI_ATTACH)
501faa1795aSjb150015 		return (DDI_FAILURE);
502*34bbc83aSGordon Ross 
503*34bbc83aSGordon Ross 	/* we only allow instance 0 to attach */
504*34bbc83aSGordon Ross 	if (ddi_get_instance(dip) != 0)
505*34bbc83aSGordon Ross 		return (DDI_FAILURE);
506*34bbc83aSGordon Ross 
507*34bbc83aSGordon Ross 	/* Create the minor nodes.  See smb_drv_open */
508*34bbc83aSGordon Ross 	if (ddi_create_minor_node(dip, "smbsrv", S_IFCHR, 0,
509*34bbc83aSGordon Ross 	    DDI_PSEUDO, 0) != DDI_SUCCESS) {
510*34bbc83aSGordon Ross 		cmn_err(CE_WARN, "smb_drv_attach:"
511*34bbc83aSGordon Ross 		    " failed creating minor node 0");
512*34bbc83aSGordon Ross 		return (DDI_FAILURE);
513*34bbc83aSGordon Ross 	}
514*34bbc83aSGordon Ross 	if (ddi_create_minor_node(dip, "smbsrv1", S_IFCHR, 1,
515*34bbc83aSGordon Ross 	    DDI_PSEUDO, 0) != DDI_SUCCESS) {
516*34bbc83aSGordon Ross 		cmn_err(CE_WARN, "smb_drv_attach:"
517*34bbc83aSGordon Ross 		    " failed creating minor node 1");
518*34bbc83aSGordon Ross 		ddi_remove_minor_node(dip, NULL);
519*34bbc83aSGordon Ross 		return (DDI_FAILURE);
520*34bbc83aSGordon Ross 	}
521*34bbc83aSGordon Ross 
522*34bbc83aSGordon Ross 	/* Reserved: control dev = 0, library dev = 1 */
523*34bbc83aSGordon Ross 	smb_drv_minors = id_space_create("smbsrv drv minors", 2, INT32_MAX);
524*34bbc83aSGordon Ross 	smb_drv_dip = dip;
525*34bbc83aSGordon Ross 
526*34bbc83aSGordon Ross 	return (DDI_SUCCESS);
527faa1795aSjb150015 }
528faa1795aSjb150015 
529faa1795aSjb150015 static int
smb_drv_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)530faa1795aSjb150015 smb_drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
531faa1795aSjb150015 {
532*34bbc83aSGordon Ross 	if (cmd != DDI_DETACH)
533faa1795aSjb150015 		return (DDI_FAILURE);
534*34bbc83aSGordon Ross 
535*34bbc83aSGordon Ross 	ASSERT(dip == smb_drv_dip);
536*34bbc83aSGordon Ross 	smb_drv_dip = NULL;
537*34bbc83aSGordon Ross 
538*34bbc83aSGordon Ross 	id_space_destroy(smb_drv_minors);
539*34bbc83aSGordon Ross 	smb_drv_minors = NULL;
540*34bbc83aSGordon Ross 
541*34bbc83aSGordon Ross 	ddi_remove_minor_node(dip, NULL);
542*34bbc83aSGordon Ross 
543*34bbc83aSGordon Ross 	return (DDI_SUCCESS);
544faa1795aSjb150015 }
545da6c28aaSamw 
546da6c28aaSamw /* ARGSUSED */
547da6c28aaSamw static int
smb_drv_getinfo(dev_info_t * dip,ddi_info_cmd_t cmd,void * arg,void ** result)548da6c28aaSamw smb_drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
549da6c28aaSamw {
550da6c28aaSamw 	ulong_t instance = getminor((dev_t)arg);
551da6c28aaSamw 
552da6c28aaSamw 	switch (cmd) {
553da6c28aaSamw 	case DDI_INFO_DEVT2DEVINFO:
554da6c28aaSamw 		*result = smb_drv_dip;
555da6c28aaSamw 		return (DDI_SUCCESS);
556da6c28aaSamw 
557da6c28aaSamw 	case DDI_INFO_DEVT2INSTANCE:
558da6c28aaSamw 		*result = (void *)instance;
559da6c28aaSamw 		return (DDI_SUCCESS);
560da6c28aaSamw 
561da6c28aaSamw 	default:
562da6c28aaSamw 		break;
563da6c28aaSamw 	}
564da6c28aaSamw 
565da6c28aaSamw 	return (DDI_FAILURE);
566da6c28aaSamw }
567