xref: /illumos-gate/usr/src/uts/common/fs/smbsrv/smb_init.c (revision c211fc479225fa54805cf480633bf6689ca9a2db)
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 flag, cred_t *cred,
198     int *retval)
199 {
200 	int		rc = 0;
201 	smb_io_t	smb_io;
202 	uint32_t	crc1;
203 	uint32_t	crc2;
204 
205 	if (ddi_copyin((smb_io_t *)argp, &smb_io, sizeof (smb_io), flag) ||
206 	    (smb_io.sio_version != SMB_IOC_VERSION))
207 		return (EFAULT);
208 
209 	crc1 = smb_io.sio_crc;
210 	smb_io.sio_crc = 0;
211 	crc2 = smb_crc_gen((uint8_t *)&smb_io, sizeof (smb_io_t));
212 
213 	if (crc1 != crc2)
214 		return (EFAULT);
215 
216 	switch (cmd) {
217 	case SMB_IOC_CONFIG:
218 		rc = smb_server_configure(&smb_io.sio_data.cfg);
219 		break;
220 	case SMB_IOC_START:
221 		rc = smb_server_start(&smb_io.sio_data.start);
222 		break;
223 	case SMB_IOC_NBT_LISTEN:
224 		rc = smb_server_nbt_listen(smb_io.sio_data.error);
225 		break;
226 	case SMB_IOC_TCP_LISTEN:
227 		rc = smb_server_tcp_listen(smb_io.sio_data.error);
228 		break;
229 	case SMB_IOC_NBT_RECEIVE:
230 		rc = smb_server_nbt_receive();
231 		break;
232 	case SMB_IOC_TCP_RECEIVE:
233 		rc = smb_server_tcp_receive();
234 		break;
235 	case SMB_IOC_GMTOFF:
236 		rc = smb_server_set_gmtoff(smb_io.sio_data.gmtoff);
237 		break;
238 	default:
239 		rc = ENOTTY;
240 		break;
241 	}
242 
243 	return (rc);
244 }
245 
246 /*
247  * ****************************************************************************
248  *				Pseudo Device Operations
249  * ****************************************************************************
250  */
251 static int
252 smb_drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
253 {
254 	if (cmd == DDI_ATTACH) {
255 		/* we only allow instance 0 to attach */
256 		if (ddi_get_instance(dip) == 0) {
257 			/* create the minor node */
258 			if (ddi_create_minor_node(dip, "smbsrv", S_IFCHR, 0,
259 			    DDI_PSEUDO, 0) == DDI_SUCCESS) {
260 				smb_drv_dip = dip;
261 				return (DDI_SUCCESS);
262 			} else {
263 				cmn_err(CE_WARN, "smb_drv_attach:"
264 				    " failed creating minor node");
265 			}
266 		}
267 	}
268 	return (DDI_FAILURE);
269 }
270 
271 static int
272 smb_drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
273 {
274 	if (cmd == DDI_DETACH) {
275 		ASSERT(dip == smb_drv_dip);
276 		ddi_remove_minor_node(dip, NULL);
277 		smb_drv_dip = NULL;
278 		return (DDI_SUCCESS);
279 	}
280 	return (DDI_FAILURE);
281 }
282 
283 /* ARGSUSED */
284 static int
285 smb_drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
286 {
287 	ulong_t instance = getminor((dev_t)arg);
288 
289 	switch (cmd) {
290 	case DDI_INFO_DEVT2DEVINFO:
291 		*result = smb_drv_dip;
292 		return (DDI_SUCCESS);
293 
294 	case DDI_INFO_DEVT2INSTANCE:
295 		*result = (void *)instance;
296 		return (DDI_SUCCESS);
297 
298 	default:
299 		break;
300 	}
301 
302 	return (DDI_FAILURE);
303 }
304