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