xref: /freebsd/sys/netsmb/smb_dev.c (revision 848ee2a3a8b47c9905fc51fefcf60eb371edbb98)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000-2001 Boris Popov
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/capsicum.h>
35 #include <sys/module.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/fcntl.h>
39 #include <sys/ioccom.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/file.h>		/* Must come after sys/malloc.h */
43 #include <sys/filedesc.h>
44 #include <sys/mbuf.h>
45 #include <sys/poll.h>
46 #include <sys/proc.h>
47 #include <sys/select.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/uio.h>
52 #include <sys/vnode.h>
53 
54 #include <net/if.h>
55 
56 #include <netsmb/smb.h>
57 #include <netsmb/smb_conn.h>
58 #include <netsmb/smb_subr.h>
59 #include <netsmb/smb_dev.h>
60 
61 static struct cdev *nsmb_dev;
62 
63 static d_open_t	 nsmb_dev_open;
64 static d_ioctl_t nsmb_dev_ioctl;
65 
66 MODULE_DEPEND(netsmb, libiconv, 1, 1, 2);
67 MODULE_VERSION(netsmb, NSMB_VERSION);
68 
69 static int smb_version = NSMB_VERSION;
70 struct sx smb_lock;
71 
72 SYSCTL_DECL(_net_smb);
73 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
74 
75 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
76 
77 static struct cdevsw nsmb_cdevsw = {
78 	.d_version =	D_VERSION,
79 	.d_open =	nsmb_dev_open,
80 	.d_ioctl =	nsmb_dev_ioctl,
81 	.d_name =	NSMB_NAME
82 };
83 
84 static int
85 nsmb_dev_init(void)
86 {
87 
88 	nsmb_dev = make_dev(&nsmb_cdevsw, 0, UID_ROOT, GID_OPERATOR,
89 	    0600, "nsmb");
90 	if (nsmb_dev == NULL)
91 		return (ENOMEM);
92 	return (0);
93 }
94 
95 static void
96 nsmb_dev_destroy(void)
97 {
98 
99 	MPASS(nsmb_dev != NULL);
100 	destroy_dev(nsmb_dev);
101 	nsmb_dev = NULL;
102 }
103 
104 static struct smb_dev *
105 smbdev_alloc(struct cdev *dev)
106 {
107 	struct smb_dev *sdp;
108 
109 	sdp = malloc(sizeof(struct smb_dev), M_NSMBDEV, M_WAITOK | M_ZERO);
110 	sdp->dev = dev;
111 	sdp->sd_level = -1;
112 	sdp->sd_flags |= NSMBFL_OPEN;
113 	sdp->refcount = 1;
114 	return (sdp);
115 }
116 
117 void
118 sdp_dtor(void *arg)
119 {
120 	struct smb_dev *dev;
121 
122 	dev = (struct smb_dev *)arg;
123 	SMB_LOCK();
124 	sdp_trydestroy(dev);
125 	SMB_UNLOCK();
126 }
127 
128 static int
129 nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
130 {
131 	struct smb_dev *sdp;
132 	int error;
133 
134 	sdp = smbdev_alloc(dev);
135 	error = devfs_set_cdevpriv(sdp, sdp_dtor);
136 	if (error) {
137 		free(sdp, M_NSMBDEV);
138 		return (error);
139 	}
140 	return (0);
141 }
142 
143 void
144 sdp_trydestroy(struct smb_dev *sdp)
145 {
146 	struct smb_vc *vcp;
147 	struct smb_share *ssp;
148 	struct smb_cred *scred;
149 
150 	SMB_LOCKASSERT();
151 	if (!sdp)
152 		panic("No smb_dev upon device close");
153 	MPASS(sdp->refcount > 0);
154 	sdp->refcount--;
155 	if (sdp->refcount)
156 		return;
157 	scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
158 	smb_makescred(scred, curthread, NULL);
159 	ssp = sdp->sd_share;
160 	if (ssp != NULL) {
161 		smb_share_lock(ssp);
162 		smb_share_rele(ssp, scred);
163 	}
164 	vcp = sdp->sd_vc;
165 	if (vcp != NULL) {
166 		smb_vc_lock(vcp);
167 		smb_vc_rele(vcp, scred);
168 	}
169 	free(scred, M_NSMBDEV);
170 	free(sdp, M_NSMBDEV);
171 	return;
172 }
173 
174 static int
175 nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
176 {
177 	struct smb_dev *sdp;
178 	struct smb_vc *vcp;
179 	struct smb_share *ssp;
180 	struct smb_cred *scred;
181 	int error = 0;
182 
183 	error = devfs_get_cdevpriv((void **)&sdp);
184 	if (error)
185 		return (error);
186 	scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
187 	SMB_LOCK();
188 	smb_makescred(scred, td, NULL);
189 	switch (cmd) {
190 	    case SMBIOC_OPENSESSION:
191 		if (sdp->sd_vc) {
192 			error = EISCONN;
193 			goto out;
194 		}
195 		error = smb_usr_opensession((struct smbioc_ossn*)data,
196 		    scred, &vcp);
197 		if (error)
198 			break;
199 		sdp->sd_vc = vcp;
200 		smb_vc_unlock(vcp);
201 		sdp->sd_level = SMBL_VC;
202 		break;
203 	    case SMBIOC_OPENSHARE:
204 		if (sdp->sd_share) {
205 			error = EISCONN;
206 			goto out;
207 		}
208 		if (sdp->sd_vc == NULL) {
209 			error = ENOTCONN;
210 			goto out;
211 		}
212 		error = smb_usr_openshare(sdp->sd_vc,
213 		    (struct smbioc_oshare*)data, scred, &ssp);
214 		if (error)
215 			break;
216 		sdp->sd_share = ssp;
217 		smb_share_unlock(ssp);
218 		sdp->sd_level = SMBL_SHARE;
219 		break;
220 	    case SMBIOC_REQUEST:
221 		if (sdp->sd_share == NULL) {
222 			error = ENOTCONN;
223 			goto out;
224 		}
225 		error = smb_usr_simplerequest(sdp->sd_share,
226 		    (struct smbioc_rq*)data, scred);
227 		break;
228 	    case SMBIOC_T2RQ:
229 		if (sdp->sd_share == NULL) {
230 			error = ENOTCONN;
231 			goto out;
232 		}
233 		error = smb_usr_t2request(sdp->sd_share,
234 		    (struct smbioc_t2rq*)data, scred);
235 		break;
236 	    case SMBIOC_SETFLAGS: {
237 		struct smbioc_flags *fl = (struct smbioc_flags*)data;
238 		int on;
239 
240 		if (fl->ioc_level == SMBL_VC) {
241 			if (fl->ioc_mask & SMBV_PERMANENT) {
242 				on = fl->ioc_flags & SMBV_PERMANENT;
243 				if ((vcp = sdp->sd_vc) == NULL) {
244 					error = ENOTCONN;
245 					goto out;
246 				}
247 				error = smb_vc_get(vcp, scred);
248 				if (error)
249 					break;
250 				if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
251 					vcp->obj.co_flags |= SMBV_PERMANENT;
252 					smb_vc_ref(vcp);
253 				} else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
254 					vcp->obj.co_flags &= ~SMBV_PERMANENT;
255 					smb_vc_rele(vcp, scred);
256 				}
257 				smb_vc_put(vcp, scred);
258 			} else
259 				error = EINVAL;
260 		} else if (fl->ioc_level == SMBL_SHARE) {
261 			if (fl->ioc_mask & SMBS_PERMANENT) {
262 				on = fl->ioc_flags & SMBS_PERMANENT;
263 				if ((ssp = sdp->sd_share) == NULL) {
264 					error = ENOTCONN;
265 					goto out;
266 				}
267 				error = smb_share_get(ssp, scred);
268 				if (error)
269 					break;
270 				if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
271 					ssp->obj.co_flags |= SMBS_PERMANENT;
272 					smb_share_ref(ssp);
273 				} else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
274 					ssp->obj.co_flags &= ~SMBS_PERMANENT;
275 					smb_share_rele(ssp, scred);
276 				}
277 				smb_share_put(ssp, scred);
278 			} else
279 				error = EINVAL;
280 			break;
281 		} else
282 			error = EINVAL;
283 		break;
284 	    }
285 	    case SMBIOC_LOOKUP:
286 		if (sdp->sd_vc || sdp->sd_share) {
287 			error = EISCONN;
288 			goto out;
289 		}
290 		vcp = NULL;
291 		ssp = NULL;
292 		error = smb_usr_lookup((struct smbioc_lookup*)data, scred, &vcp, &ssp);
293 		if (error)
294 			break;
295 		if (vcp) {
296 			sdp->sd_vc = vcp;
297 			smb_vc_unlock(vcp);
298 			sdp->sd_level = SMBL_VC;
299 		}
300 		if (ssp) {
301 			sdp->sd_share = ssp;
302 			smb_share_unlock(ssp);
303 			sdp->sd_level = SMBL_SHARE;
304 		}
305 		break;
306 	    case SMBIOC_READ: case SMBIOC_WRITE: {
307 		struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
308 		struct uio auio;
309 		struct iovec iov;
310 
311 		if ((ssp = sdp->sd_share) == NULL) {
312 			error = ENOTCONN;
313 			goto out;
314 	 	}
315 		iov.iov_base = rwrq->ioc_base;
316 		iov.iov_len = rwrq->ioc_cnt;
317 		auio.uio_iov = &iov;
318 		auio.uio_iovcnt = 1;
319 		auio.uio_offset = rwrq->ioc_offset;
320 		auio.uio_resid = rwrq->ioc_cnt;
321 		auio.uio_segflg = UIO_USERSPACE;
322 		auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
323 		auio.uio_td = td;
324 		if (cmd == SMBIOC_READ)
325 			error = smb_read(ssp, rwrq->ioc_fh, &auio, scred);
326 		else
327 			error = smb_write(ssp, rwrq->ioc_fh, &auio, scred);
328 		rwrq->ioc_cnt -= auio.uio_resid;
329 		break;
330 	    }
331 	    default:
332 		error = ENODEV;
333 	}
334 out:
335 	free(scred, M_NSMBDEV);
336 	SMB_UNLOCK();
337 	return error;
338 }
339 
340 static int
341 nsmb_dev_load(module_t mod, int cmd, void *arg)
342 {
343 	int error = 0;
344 
345 	switch (cmd) {
346 	    case MOD_LOAD:
347 		error = smb_sm_init();
348 		if (error)
349 			break;
350 		error = smb_iod_init();
351 		if (error) {
352 			smb_sm_done();
353 			break;
354 		}
355 		error = nsmb_dev_init();
356 		if (error)
357 			break;
358 		sx_init(&smb_lock, "samba device lock");
359 		break;
360 	    case MOD_UNLOAD:
361 		smb_iod_done();
362 		error = smb_sm_done();
363 		if (error)
364 			break;
365 		nsmb_dev_destroy();
366 		sx_destroy(&smb_lock);
367 		break;
368 	    default:
369 		error = EINVAL;
370 		break;
371 	}
372 	return error;
373 }
374 
375 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
376 
377 int
378 smb_dev2share(int fd, int mode, struct smb_cred *scred,
379 	struct smb_share **sspp, struct smb_dev **ssdp)
380 {
381 	struct file *fp, *fptmp;
382 	struct smb_dev *sdp;
383 	struct smb_share *ssp;
384 	struct thread *td;
385 	int error;
386 
387 	td = curthread;
388 	error = fget(td, fd, &cap_read_rights, &fp);
389 	if (error)
390 		return (error);
391 	fptmp = td->td_fpop;
392 	td->td_fpop = fp;
393 	error = devfs_get_cdevpriv((void **)&sdp);
394 	td->td_fpop = fptmp;
395 	fdrop(fp, td);
396 	if (error || sdp == NULL)
397 		return (error);
398 	SMB_LOCK();
399 	*ssdp = sdp;
400 	ssp = sdp->sd_share;
401 	if (ssp == NULL) {
402 		SMB_UNLOCK();
403 		return (ENOTCONN);
404 	}
405 	error = smb_share_get(ssp, scred);
406 	if (error == 0) {
407 		sdp->refcount++;
408 		*sspp = ssp;
409 	}
410 	SMB_UNLOCK();
411 	return error;
412 }
413