1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23 */
24
25#include <sys/types.h>
26#include <sys/ddi.h>
27#include <sys/modctl.h>
28#include <sys/cred.h>
29#include <sys/ioccom.h>
30#include <sys/policy.h>
31#include <sys/cmn_err.h>
32#include <smbsrv/smb_kproto.h>
33#include <smbsrv/smb_ioctl.h>
34
35static int smb_drv_open(dev_t *, int, int, cred_t *);
36static int smb_drv_close(dev_t, int, int, cred_t *);
37static int smb_drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
38static int smb_drv_attach(dev_info_t *, ddi_attach_cmd_t);
39static int smb_drv_detach(dev_info_t *, ddi_detach_cmd_t);
40static int smb_drv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
41
42/*
43 * *****************************************************************************
44 * ****************************** Global Variables *****************************
45 * *****************************************************************************
46 *
47 * These variables can only be changed through the /etc/system file.
48 */
49
50/*
51 * Maximum buffer size for NT: configurable based on the client environment.
52 * IR104720 Experiments with Windows 2000 indicate that we achieve better
53 * SmbWriteX performance with a buffer size of 64KB instead of the 37KB used
54 * with Windows NT4.0. Previous experiments with NT4.0 resulted in directory
55 * listing problems so this buffer size is configurable based on the end-user
56 * environment. When in doubt use 37KB.
57 *
58 * smb_raw_mode: read_raw and write_raw supported (1) or NOT supported (0).
59 */
60int	smb_maxbufsize = SMB_NT_MAXBUF;
61int	smb_oplock_levelII = 1;
62int	smb_oplock_timeout = OPLOCK_STD_TIMEOUT;
63int	smb_oplock_min_timeout = OPLOCK_MIN_TIMEOUT;
64int	smb_flush_required = 1;
65int	smb_dirsymlink_enable = 1;
66int	smb_sign_debug = 0;
67int	smb_raw_mode = 1;
68int	smb_shortnames = 1;
69uint_t	smb_audit_flags =
70#ifdef	DEBUG
71    SMB_AUDIT_NODE;
72#else
73    0;
74#endif
75
76/*
77 * Maximum number of simultaneous authentication, share mapping, pipe open
78 * requests to be processed.
79 */
80int	smb_ssetup_threshold = 256;
81int	smb_tcon_threshold = 1024;
82int	smb_opipe_threshold = 1024;
83
84/*
85 * Number of milliseconds that a request will be stalled if it comes in after
86 * the maximum number of inflight operations are being proccessed.
87 */
88int	smb_ssetup_timeout = (30 * 1000);
89int	smb_tcon_timeout = (30 * 1000);
90int	smb_opipe_timeout = (30 * 1000);
91
92int	smb_threshold_debug = 0;
93
94
95/*
96 * *****************************************************************************
97 * ********************** Static Variables / Module Linkage ********************
98 * *****************************************************************************
99 */
100
101static struct cb_ops cbops = {
102	smb_drv_open,		/* cb_open */
103	smb_drv_close,		/* cb_close */
104	nodev,			/* cb_strategy */
105	nodev,			/* cb_print */
106	nodev,			/* cb_dump */
107	nodev,			/* cb_read */
108	nodev,			/* cb_write */
109	smb_drv_ioctl,		/* cb_ioctl */
110	nodev,			/* cb_devmap */
111	nodev,			/* cb_mmap */
112	nodev,			/* cb_segmap */
113	nochpoll,		/* cb_chpoll */
114	ddi_prop_op,		/* cb_prop_op */
115	NULL,			/* cb_streamtab */
116	D_MP,			/* cb_flag */
117	CB_REV,			/* cb_rev */
118	nodev,			/* cb_aread */
119	nodev,			/* cb_awrite */
120};
121
122static struct dev_ops devops = {
123	DEVO_REV,		/* devo_rev */
124	0,			/* devo_refcnt */
125	smb_drv_getinfo,	/* devo_getinfo */
126	nulldev,		/* devo_identify */
127	nulldev,		/* devo_probe */
128	smb_drv_attach,		/* devo_attach */
129	smb_drv_detach,		/* devo_detach */
130	nodev,			/* devo_reset */
131	&cbops,			/* devo_cb_ops */
132	NULL,			/* devo_bus_ops */
133	NULL,			/* devo_power */
134	ddi_quiesce_not_needed,		/* devo_quiesce */
135};
136
137static struct modldrv modldrv = {
138	&mod_driverops,					/* drv_modops */
139	"CIFS Server Protocol",				/* drv_linkinfo */
140	&devops,
141};
142
143static struct modlinkage modlinkage = {
144	MODREV_1,	/* revision of the module, must be: MODREV_1	*/
145	&modldrv,	/* ptr to linkage structures			*/
146	NULL,
147};
148
149static dev_info_t *smb_drv_dip = NULL;
150
151/*
152 * ****************************************************************************
153 *				    Module Interface
154 * ****************************************************************************
155 */
156
157int
158_init(void)
159{
160	int rc;
161
162	if ((rc = smb_kshare_init()) != 0)
163		return (rc);
164
165	if ((rc = smb_server_svc_init()) != 0) {
166		smb_kshare_fini();
167		return (rc);
168	}
169
170	if ((rc = mod_install(&modlinkage)) != 0) {
171		smb_kshare_fini();
172		(void) smb_server_svc_fini();
173	}
174
175	return (rc);
176}
177
178int
179_info(struct modinfo *modinfop)
180{
181	return (mod_info(&modlinkage, modinfop));
182}
183
184int
185_fini(void)
186{
187	int	rc;
188
189	if ((rc = mod_remove(&modlinkage)) == 0) {
190		rc = smb_server_svc_fini();
191		smb_kshare_fini();
192	}
193
194	return (rc);
195}
196
197/*
198 * ****************************************************************************
199 *				Pseudo Device Entry Points
200 * ****************************************************************************
201 */
202/* ARGSUSED */
203static int
204smb_drv_open(dev_t *devp, int flag, int otyp, cred_t *credp)
205{
206	/*
207	 * Check caller's privileges.
208	 */
209	if (secpolicy_smb(credp) != 0)
210		return (EPERM);
211
212	/*
213	 * Start SMB service state machine
214	 */
215	return (smb_server_create());
216}
217
218/* ARGSUSED */
219static int
220smb_drv_close(dev_t dev, int flag, int otyp, cred_t *credp)
221{
222	return (smb_server_delete());
223}
224
225/* ARGSUSED */
226static int
227smb_drv_ioctl(dev_t drv, int cmd, intptr_t argp, int flags, cred_t *cred,
228    int *retval)
229{
230	smb_ioc_t	*ioc;
231	smb_ioc_header_t ioc_hdr;
232	uint32_t	crc;
233	boolean_t	copyout = B_FALSE;
234	int		rc = 0;
235
236	if (ddi_copyin((const void *)argp, &ioc_hdr, sizeof (smb_ioc_header_t),
237	    flags) || (ioc_hdr.version != SMB_IOC_VERSION))
238		return (EFAULT);
239
240	crc = ioc_hdr.crc;
241	ioc_hdr.crc = 0;
242	if (smb_crc_gen((uint8_t *)&ioc_hdr, sizeof (ioc_hdr)) != crc)
243		return (EFAULT);
244
245	ioc = kmem_alloc(ioc_hdr.len, KM_SLEEP);
246	if (ddi_copyin((const void *)argp, ioc, ioc_hdr.len, flags)) {
247		kmem_free(ioc, ioc_hdr.len);
248		return (EFAULT);
249	}
250
251	switch (cmd) {
252	case SMB_IOC_CONFIG:
253		rc = smb_server_configure(&ioc->ioc_cfg);
254		break;
255	case SMB_IOC_START:
256		rc = smb_server_start(&ioc->ioc_start);
257		break;
258	case SMB_IOC_STOP:
259		rc = smb_server_stop();
260		break;
261	case SMB_IOC_EVENT:
262		rc = smb_server_notify_event(&ioc->ioc_event);
263		break;
264	case SMB_IOC_GMTOFF:
265		rc = smb_server_set_gmtoff(&ioc->ioc_gmt);
266		break;
267	case SMB_IOC_SHARE:
268		rc = smb_kshare_export_list(&ioc->ioc_share);
269		break;
270	case SMB_IOC_UNSHARE:
271		rc = smb_kshare_unexport_list(&ioc->ioc_share);
272		break;
273	case SMB_IOC_SHAREINFO:
274		rc = smb_kshare_info(&ioc->ioc_shareinfo);
275		copyout = B_TRUE;
276		break;
277	case SMB_IOC_NUMOPEN:
278		rc = smb_server_numopen(&ioc->ioc_opennum);
279		copyout = B_TRUE;
280		break;
281	case SMB_IOC_SVCENUM:
282		rc = smb_server_enum(&ioc->ioc_svcenum);
283		copyout = B_TRUE;
284		break;
285	case SMB_IOC_SESSION_CLOSE:
286		rc = smb_server_session_close(&ioc->ioc_session);
287		break;
288	case SMB_IOC_FILE_CLOSE:
289		rc = smb_server_file_close(&ioc->ioc_fileid);
290		break;
291	case SMB_IOC_SPOOLDOC:
292		rc = smb_server_spooldoc(&ioc->ioc_spooldoc);
293		copyout = B_TRUE;
294		break;
295	default:
296		rc = ENOTTY;
297		break;
298	}
299	if ((rc == 0) && copyout) {
300		if (ddi_copyout((const void *)ioc, (void *)argp, ioc_hdr.len,
301		    flags))
302			rc = EFAULT;
303	}
304	kmem_free(ioc, ioc_hdr.len);
305	return (rc);
306}
307
308/*
309 * ****************************************************************************
310 *				Pseudo Device Operations
311 * ****************************************************************************
312 */
313static int
314smb_drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
315{
316	if (cmd == DDI_ATTACH) {
317		/* we only allow instance 0 to attach */
318		if (ddi_get_instance(dip) == 0) {
319			/* create the minor node */
320			if (ddi_create_minor_node(dip, "smbsrv", S_IFCHR, 0,
321			    DDI_PSEUDO, 0) == DDI_SUCCESS) {
322				smb_drv_dip = dip;
323				return (DDI_SUCCESS);
324			} else {
325				cmn_err(CE_WARN, "smb_drv_attach:"
326				    " failed creating minor node");
327			}
328		}
329	}
330	return (DDI_FAILURE);
331}
332
333static int
334smb_drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
335{
336	if (cmd == DDI_DETACH) {
337		ASSERT(dip == smb_drv_dip);
338		ddi_remove_minor_node(dip, NULL);
339		smb_drv_dip = NULL;
340		return (DDI_SUCCESS);
341	}
342	return (DDI_FAILURE);
343}
344
345/* ARGSUSED */
346static int
347smb_drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
348{
349	ulong_t instance = getminor((dev_t)arg);
350
351	switch (cmd) {
352	case DDI_INFO_DEVT2DEVINFO:
353		*result = smb_drv_dip;
354		return (DDI_SUCCESS);
355
356	case DDI_INFO_DEVT2INSTANCE:
357		*result = (void *)instance;
358		return (DDI_SUCCESS);
359
360	default:
361		break;
362	}
363
364	return (DDI_FAILURE);
365}
366