xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_init.c (revision 129b3e6c5b0ac55b5021a4c38db6387b6acdaaf1)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/ddi.h>
28 #include <sys/modctl.h>
29 #include <sys/cred.h>
30 #include <sys/ioccom.h>
31 #include <sys/policy.h>
32 #include <sys/cmn_err.h>
33 #include <smbsrv/smb_incl.h>
34 #include <smbsrv/smb_door_svc.h>
35 #include <smbsrv/smb_ioctl.h>
36 #include <smbsrv/smb_kproto.h>
37 
38 static int smb_drv_open(dev_t *, int, int, cred_t *);
39 static int smb_drv_close(dev_t, int, int, cred_t *);
40 static int smb_drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
41 static int smb_drv_attach(dev_info_t *, ddi_attach_cmd_t);
42 static int smb_drv_detach(dev_info_t *, ddi_detach_cmd_t);
43 static int smb_drv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
44 
45 /*
46  * *****************************************************************************
47  * ****************************** Global Variables *****************************
48  * *****************************************************************************
49  *
50  * These variables can only be changed through the /etc/system file.
51  */
52 
53 /*
54  * Maximum buffer size for NT: configurable based on the client environment.
55  * IR104720 Experiments with Windows 2000 indicate that we achieve better
56  * SmbWriteX performance with a buffer size of 64KB instead of the 37KB used
57  * with Windows NT4.0. Previous experiments with NT4.0 resulted in directory
58  * listing problems so this buffer size is configurable based on the end-user
59  * environment. When in doubt use 37KB.
60  */
61 int	smb_maxbufsize = SMB_NT_MAXBUF;
62 int	smb_oplock_timeout = OPLOCK_STD_TIMEOUT;
63 int	smb_flush_required = 1;
64 int	smb_dirsymlink_enable = 1;
65 int	smb_announce_quota = 0;
66 int	smb_sign_debug = 0;
67 uint_t	smb_audit_flags =
68 #ifdef	DEBUG
69     SMB_AUDIT_NODE;
70 #else
71     0;
72 #endif
73 
74 /*
75  * *****************************************************************************
76  * ********************** Static Variables / Module Linkage ********************
77  * *****************************************************************************
78  */
79 
80 static struct cb_ops cbops = {
81 	smb_drv_open,		/* cb_open */
82 	smb_drv_close,		/* cb_close */
83 	nodev,			/* cb_strategy */
84 	nodev,			/* cb_print */
85 	nodev,			/* cb_dump */
86 	nodev,			/* cb_read */
87 	nodev,			/* cb_write */
88 	smb_drv_ioctl,		/* cb_ioctl */
89 	nodev,			/* cb_devmap */
90 	nodev,			/* cb_mmap */
91 	nodev,			/* cb_segmap */
92 	nochpoll,		/* cb_chpoll */
93 	ddi_prop_op,		/* cb_prop_op */
94 	NULL,			/* cb_streamtab */
95 	D_MP,			/* cb_flag */
96 	CB_REV,			/* cb_rev */
97 	nodev,			/* cb_aread */
98 	nodev,			/* cb_awrite */
99 };
100 
101 static struct dev_ops devops = {
102 	DEVO_REV,		/* devo_rev */
103 	0,			/* devo_refcnt */
104 	smb_drv_getinfo,	/* devo_getinfo */
105 	nulldev,		/* devo_identify */
106 	nulldev,		/* devo_probe */
107 	smb_drv_attach,		/* devo_attach */
108 	smb_drv_detach,		/* devo_detach */
109 	nodev,			/* devo_reset */
110 	&cbops,			/* devo_cb_ops */
111 	NULL,			/* devo_bus_ops */
112 	NULL,			/* devo_power */
113 	ddi_quiesce_not_needed,		/* devo_quiesce */
114 };
115 
116 static struct modldrv modldrv = {
117 	&mod_driverops,					/* drv_modops */
118 	"CIFS Server Protocol",				/* drv_linkinfo */
119 	&devops,
120 };
121 
122 static struct modlinkage modlinkage = {
123 	MODREV_1,	/* revision of the module, must be: MODREV_1	*/
124 	&modldrv,	/* ptr to linkage structures			*/
125 	NULL,
126 };
127 
128 static dev_info_t *smb_drv_dip = NULL;
129 
130 /*
131  * ****************************************************************************
132  *				    Module Interface
133  * ****************************************************************************
134  */
135 
136 int
137 _init(void)
138 {
139 	int	rc;
140 
141 	rc = smb_server_svc_init();
142 	if (rc == 0) {
143 		rc = mod_install(&modlinkage);
144 		if (rc != 0)
145 			(void) smb_server_svc_fini();
146 	}
147 	return (rc);
148 }
149 
150 int
151 _info(struct modinfo *modinfop)
152 {
153 	return (mod_info(&modlinkage, modinfop));
154 }
155 
156 int
157 _fini(void)
158 {
159 	int	rc;
160 
161 	rc = mod_remove(&modlinkage);
162 	if (rc == 0)
163 		rc = smb_server_svc_fini();
164 	return (rc);
165 }
166 
167 /*
168  * ****************************************************************************
169  *				Pseudo Device Entry Points
170  * ****************************************************************************
171  */
172 /* ARGSUSED */
173 static int
174 smb_drv_open(dev_t *devp, int flag, int otyp, cred_t *credp)
175 {
176 	/*
177 	 * Check caller's privileges.
178 	 */
179 	if (secpolicy_smb(credp) != 0)
180 		return (EPERM);
181 
182 	/*
183 	 * Start SMB service state machine
184 	 */
185 	return (smb_server_create());
186 }
187 
188 /* ARGSUSED */
189 static int
190 smb_drv_close(dev_t dev, int flag, int otyp, cred_t *credp)
191 {
192 	return (smb_server_delete());
193 }
194 
195 /* ARGSUSED */
196 static int
197 smb_drv_ioctl(dev_t drv, int cmd, intptr_t argp, int flags, cred_t *cred,
198     int *retval)
199 {
200 	smb_ioc_t	*ioc;
201 	smb_ioc_header_t ioc_hdr;
202 	uint32_t	crc;
203 	boolean_t	copyout = B_FALSE;
204 	int		rc = 0;
205 
206 	if (ddi_copyin((const void *)argp, &ioc_hdr, sizeof (smb_ioc_header_t),
207 	    flags) || (ioc_hdr.version != SMB_IOC_VERSION))
208 		return (EFAULT);
209 
210 	crc = ioc_hdr.crc;
211 	ioc_hdr.crc = 0;
212 	if (smb_crc_gen((uint8_t *)&ioc_hdr, sizeof (ioc_hdr)) != crc)
213 		return (EFAULT);
214 
215 	ioc = kmem_alloc(ioc_hdr.len, KM_SLEEP);
216 	if (ddi_copyin((const void *)argp, ioc, ioc_hdr.len, flags)) {
217 		kmem_free(ioc, ioc_hdr.len);
218 		return (EFAULT);
219 	}
220 
221 	switch (cmd) {
222 	case SMB_IOC_CONFIG:
223 		rc = smb_server_configure(&ioc->ioc_cfg);
224 		break;
225 	case SMB_IOC_START:
226 		rc = smb_server_start(&ioc->ioc_start);
227 		break;
228 	case SMB_IOC_NBT_LISTEN:
229 		rc = smb_server_nbt_listen(&ioc->ioc_listen);
230 		break;
231 	case SMB_IOC_TCP_LISTEN:
232 		rc = smb_server_tcp_listen(&ioc->ioc_listen);
233 		break;
234 	case SMB_IOC_NBT_RECEIVE:
235 		rc = smb_server_nbt_receive();
236 		break;
237 	case SMB_IOC_TCP_RECEIVE:
238 		rc = smb_server_tcp_receive();
239 		break;
240 	case SMB_IOC_GMTOFF:
241 		rc = smb_server_set_gmtoff(&ioc->ioc_gmt);
242 		break;
243 	case SMB_IOC_SHARE:
244 		rc = smb_server_share_export(&ioc->ioc_share);
245 		break;
246 	case SMB_IOC_UNSHARE:
247 		rc = smb_server_share_unexport(&ioc->ioc_share);
248 		break;
249 	case SMB_IOC_USER_NUMBER:
250 		rc = smb_server_user_number(&ioc->ioc_unum);
251 		copyout = B_TRUE;
252 		break;
253 	case SMB_IOC_USER_LIST:
254 		rc = smb_server_user_list(&ioc->ioc_ulist);
255 		copyout = B_TRUE;
256 		break;
257 	default:
258 		rc = ENOTTY;
259 		break;
260 	}
261 	if ((rc == 0) && copyout) {
262 		if (ddi_copyout((const void *)ioc, (void *)argp, ioc_hdr.len,
263 		    flags))
264 			rc = EFAULT;
265 	}
266 	kmem_free(ioc, ioc_hdr.len);
267 	return (rc);
268 }
269 
270 /*
271  * ****************************************************************************
272  *				Pseudo Device Operations
273  * ****************************************************************************
274  */
275 static int
276 smb_drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
277 {
278 	if (cmd == DDI_ATTACH) {
279 		/* we only allow instance 0 to attach */
280 		if (ddi_get_instance(dip) == 0) {
281 			/* create the minor node */
282 			if (ddi_create_minor_node(dip, "smbsrv", S_IFCHR, 0,
283 			    DDI_PSEUDO, 0) == DDI_SUCCESS) {
284 				smb_drv_dip = dip;
285 				return (DDI_SUCCESS);
286 			} else {
287 				cmn_err(CE_WARN, "smb_drv_attach:"
288 				    " failed creating minor node");
289 			}
290 		}
291 	}
292 	return (DDI_FAILURE);
293 }
294 
295 static int
296 smb_drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
297 {
298 	if (cmd == DDI_DETACH) {
299 		ASSERT(dip == smb_drv_dip);
300 		ddi_remove_minor_node(dip, NULL);
301 		smb_drv_dip = NULL;
302 		return (DDI_SUCCESS);
303 	}
304 	return (DDI_FAILURE);
305 }
306 
307 /* ARGSUSED */
308 static int
309 smb_drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
310 {
311 	ulong_t instance = getminor((dev_t)arg);
312 
313 	switch (cmd) {
314 	case DDI_INFO_DEVT2DEVINFO:
315 		*result = smb_drv_dip;
316 		return (DDI_SUCCESS);
317 
318 	case DDI_INFO_DEVT2INSTANCE:
319 		*result = (void *)instance;
320 		return (DDI_SUCCESS);
321 
322 	default:
323 		break;
324 	}
325 
326 	return (DDI_FAILURE);
327 }
328