xref: /freebsd/sys/netsmb/smb_dev.c (revision 5f2e780d36dd2aff6e9a5660f9039ba08bddacab)
1 /*
2  * Copyright (c) 2000-2001 Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 #include <sys/param.h>
35 #include <sys/kernel.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 #define SMB_GETDEV(dev)		((struct smb_dev*)(dev)->si_drv1)
62 #define	SMB_CHECKMINOR(dev)	do { \
63 				    sdp = SMB_GETDEV(dev); \
64 				    if (sdp == NULL) return ENXIO; \
65 				} while(0)
66 
67 static d_open_t	 nsmb_dev_open;
68 static d_close_t nsmb_dev_close;
69 static d_read_t	 nsmb_dev_read;
70 static d_write_t nsmb_dev_write;
71 static d_ioctl_t nsmb_dev_ioctl;
72 static d_poll_t	 nsmb_dev_poll;
73 
74 MODULE_DEPEND(netsmb, libiconv, 1, 1, 1);
75 MODULE_VERSION(netsmb, NSMB_VERSION);
76 
77 static int smb_version = NSMB_VERSION;
78 
79 
80 SYSCTL_DECL(_net_smb);
81 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
82 
83 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
84 
85 
86 /*
87 int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio);
88 */
89 
90 static struct cdevsw nsmb_cdevsw = {
91 	/* open */	nsmb_dev_open,
92 	/* close */	nsmb_dev_close,
93 	/* read */	nsmb_dev_read,
94 	/* write */	nsmb_dev_write,
95 	/* ioctl */ 	nsmb_dev_ioctl,
96 	/* poll */	nsmb_dev_poll,
97 	/* mmap */	nommap,
98 	/* strategy */	nostrategy,
99 	/* name */	NSMB_NAME,
100 	/* maj */	NSMB_MAJOR,
101 	/* dump */	nodump,
102 	/* psize */	nopsize,
103 	/* flags */	0,
104 #ifndef FB_CURRENT
105 	/* bmaj */	-1
106 #endif
107 };
108 
109 static eventhandler_tag nsmb_dev_tag;
110 
111 static void
112 nsmb_dev_clone(void *arg, char *name, int namelen, dev_t *dev)
113 {
114 	int u;
115 
116 	if (*dev != NODEV)
117 		return;
118 	if (dev_stdclone(name, NULL, NSMB_NAME, &u) != 1)
119 		return;
120 	*dev = make_dev(&nsmb_cdevsw, unit2minor(u), 0, 0, 0600,
121 	    NSMB_NAME"%d", u);
122 }
123 
124 static int
125 nsmb_dev_open(dev_t dev, int oflags, int devtype, struct thread *td)
126 {
127 	struct smb_dev *sdp;
128 	struct ucred *cred = td->td_ucred;
129 	int s;
130 
131 	sdp = SMB_GETDEV(dev);
132 	if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
133 		return EBUSY;
134 	if (sdp == NULL) {
135 		sdp = malloc(sizeof(*sdp), M_NSMBDEV, 0);
136 		dev->si_drv1 = (void*)sdp;
137 	}
138 	/*
139 	 * XXX: this is just crazy - make a device for an already passed device...
140 	 * someone should take care of it.
141 	 */
142 	if ((dev->si_flags & SI_NAMED) == 0)
143 		make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid, 0700,
144 		    NSMB_NAME"%d", dev2unit(dev));
145 	bzero(sdp, sizeof(*sdp));
146 /*
147 	STAILQ_INIT(&sdp->sd_rqlist);
148 	STAILQ_INIT(&sdp->sd_rplist);
149 	bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
150 */
151 	s = splimp();
152 	sdp->sd_level = -1;
153 	sdp->sd_flags |= NSMBFL_OPEN;
154 	splx(s);
155 	return 0;
156 }
157 
158 static int
159 nsmb_dev_close(dev_t dev, int flag, int fmt, struct thread *td)
160 {
161 	struct smb_dev *sdp;
162 	struct smb_vc *vcp;
163 	struct smb_share *ssp;
164 	struct smb_cred scred;
165 	int s;
166 
167 	SMB_CHECKMINOR(dev);
168 	s = splimp();
169 	if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
170 		splx(s);
171 		return EBADF;
172 	}
173 	smb_makescred(&scred, td, NULL);
174 	ssp = sdp->sd_share;
175 	if (ssp != NULL)
176 		smb_share_rele(ssp, &scred);
177 	vcp = sdp->sd_vc;
178 	if (vcp != NULL)
179 		smb_vc_rele(vcp, &scred);
180 /*
181 	smb_flushq(&sdp->sd_rqlist);
182 	smb_flushq(&sdp->sd_rplist);
183 */
184 	dev->si_drv1 = NULL;
185 	free(sdp, M_NSMBDEV);
186 	destroy_dev(dev);
187 	splx(s);
188 	return 0;
189 }
190 
191 
192 static int
193 nsmb_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
194 {
195 	struct smb_dev *sdp;
196 	struct smb_vc *vcp;
197 	struct smb_share *ssp;
198 	struct smb_cred scred;
199 	int error = 0;
200 
201 	SMB_CHECKMINOR(dev);
202 	if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
203 		return EBADF;
204 
205 	smb_makescred(&scred, td, NULL);
206 	switch (cmd) {
207 	    case SMBIOC_OPENSESSION:
208 		if (sdp->sd_vc)
209 			return EISCONN;
210 		error = smb_usr_opensession((struct smbioc_ossn*)data,
211 		    &scred, &vcp);
212 		if (error)
213 			break;
214 		sdp->sd_vc = vcp;
215 		smb_vc_unlock(vcp, 0, td);
216 		sdp->sd_level = SMBL_VC;
217 		break;
218 	    case SMBIOC_OPENSHARE:
219 		if (sdp->sd_share)
220 			return EISCONN;
221 		if (sdp->sd_vc == NULL)
222 			return ENOTCONN;
223 		error = smb_usr_openshare(sdp->sd_vc,
224 		    (struct smbioc_oshare*)data, &scred, &ssp);
225 		if (error)
226 			break;
227 		sdp->sd_share = ssp;
228 		smb_share_unlock(ssp, 0, td);
229 		sdp->sd_level = SMBL_SHARE;
230 		break;
231 	    case SMBIOC_REQUEST:
232 		if (sdp->sd_share == NULL)
233 			return ENOTCONN;
234 		error = smb_usr_simplerequest(sdp->sd_share,
235 		    (struct smbioc_rq*)data, &scred);
236 		break;
237 	    case SMBIOC_T2RQ:
238 		if (sdp->sd_share == NULL)
239 			return ENOTCONN;
240 		error = smb_usr_t2request(sdp->sd_share,
241 		    (struct smbioc_t2rq*)data, &scred);
242 		break;
243 	    case SMBIOC_SETFLAGS: {
244 		struct smbioc_flags *fl = (struct smbioc_flags*)data;
245 		int on;
246 
247 		if (fl->ioc_level == SMBL_VC) {
248 			if (fl->ioc_mask & SMBV_PERMANENT) {
249 				on = fl->ioc_flags & SMBV_PERMANENT;
250 				if ((vcp = sdp->sd_vc) == NULL)
251 					return ENOTCONN;
252 				error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
253 				if (error)
254 					break;
255 				if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
256 					vcp->obj.co_flags |= SMBV_PERMANENT;
257 					smb_vc_ref(vcp);
258 				} else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
259 					vcp->obj.co_flags &= ~SMBV_PERMANENT;
260 					smb_vc_rele(vcp, &scred);
261 				}
262 				smb_vc_put(vcp, &scred);
263 			} else
264 				error = EINVAL;
265 		} else if (fl->ioc_level == SMBL_SHARE) {
266 			if (fl->ioc_mask & SMBS_PERMANENT) {
267 				on = fl->ioc_flags & SMBS_PERMANENT;
268 				if ((ssp = sdp->sd_share) == NULL)
269 					return ENOTCONN;
270 				error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
271 				if (error)
272 					break;
273 				if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
274 					ssp->obj.co_flags |= SMBS_PERMANENT;
275 					smb_share_ref(ssp);
276 				} else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
277 					ssp->obj.co_flags &= ~SMBS_PERMANENT;
278 					smb_share_rele(ssp, &scred);
279 				}
280 				smb_share_put(ssp, &scred);
281 			} else
282 				error = EINVAL;
283 			break;
284 		} else
285 			error = EINVAL;
286 		break;
287 	    }
288 	    case SMBIOC_LOOKUP:
289 		if (sdp->sd_vc || sdp->sd_share)
290 			return EISCONN;
291 		vcp = NULL;
292 		ssp = NULL;
293 		error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp);
294 		if (error)
295 			break;
296 		if (vcp) {
297 			sdp->sd_vc = vcp;
298 			smb_vc_unlock(vcp, 0, td);
299 			sdp->sd_level = SMBL_VC;
300 		}
301 		if (ssp) {
302 			sdp->sd_share = ssp;
303 			smb_share_unlock(ssp, 0, td);
304 			sdp->sd_level = SMBL_SHARE;
305 		}
306 		break;
307 	    case SMBIOC_READ: case SMBIOC_WRITE: {
308 		struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
309 		struct uio auio;
310 		struct iovec iov;
311 
312 		if ((ssp = sdp->sd_share) == NULL)
313 			return ENOTCONN;
314 		iov.iov_base = rwrq->ioc_base;
315 		iov.iov_len = rwrq->ioc_cnt;
316 		auio.uio_iov = &iov;
317 		auio.uio_iovcnt = 1;
318 		auio.uio_offset = rwrq->ioc_offset;
319 		auio.uio_resid = rwrq->ioc_cnt;
320 		auio.uio_segflg = UIO_USERSPACE;
321 		auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
322 		auio.uio_td = td;
323 		if (cmd == SMBIOC_READ)
324 			error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
325 		else
326 			error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred);
327 		rwrq->ioc_cnt -= auio.uio_resid;
328 		break;
329 	    }
330 	    default:
331 		error = ENODEV;
332 	}
333 	return error;
334 }
335 
336 static int
337 nsmb_dev_read(dev_t dev, struct uio *uio, int flag)
338 {
339 	return EACCES;
340 }
341 
342 static int
343 nsmb_dev_write(dev_t dev, struct uio *uio, int flag)
344 {
345 	return EACCES;
346 }
347 
348 static int
349 nsmb_dev_poll(dev_t dev, int events, struct thread *td)
350 {
351 	return ENODEV;
352 }
353 
354 static int
355 nsmb_dev_load(module_t mod, int cmd, void *arg)
356 {
357 	int error = 0;
358 
359 	switch (cmd) {
360 	    case MOD_LOAD:
361 		error = smb_sm_init();
362 		if (error)
363 			break;
364 		error = smb_iod_init();
365 		if (error) {
366 			smb_sm_done();
367 			break;
368 		}
369 		cdevsw_add(&nsmb_cdevsw);
370 		nsmb_dev_tag = EVENTHANDLER_REGISTER(dev_clone, nsmb_dev_clone, 0, 1000);
371 		printf("netsmb_dev: loaded\n");
372 		break;
373 	    case MOD_UNLOAD:
374 		smb_iod_done();
375 		error = smb_sm_done();
376 		error = 0;
377 		EVENTHANDLER_DEREGISTER(dev_clone, nsmb_dev_tag);
378 		cdevsw_remove(&nsmb_cdevsw);
379 		printf("netsmb_dev: unloaded\n");
380 		break;
381 	    default:
382 		error = EINVAL;
383 		break;
384 	}
385 	return error;
386 }
387 
388 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
389 
390 /*
391  * Convert a file descriptor to appropriate smb_share pointer
392  */
393 static struct file*
394 nsmb_getfp(struct filedesc* fdp, int fd, int flag)
395 {
396 	struct file* fp;
397 
398 	FILEDESC_LOCK(fdp);
399 	if (((u_int)fd) >= fdp->fd_nfiles ||
400 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
401 	    (fp->f_flag & flag) == 0) {
402 		FILEDESC_UNLOCK(fdp);
403 		return (NULL);
404 	}
405 	fhold(fp);
406 	FILEDESC_UNLOCK(fdp);
407 	return (fp);
408 }
409 
410 int
411 smb_dev2share(int fd, int mode, struct smb_cred *scred,
412 	struct smb_share **sspp)
413 {
414 	struct file *fp;
415 	struct vnode *vp;
416 	struct smb_dev *sdp;
417 	struct smb_share *ssp;
418 	dev_t dev;
419 	int error;
420 
421 	fp = nsmb_getfp(scred->scr_td->td_proc->p_fd, fd, FREAD | FWRITE);
422 	if (fp == NULL)
423 		return EBADF;
424 	vp = fp->f_data;
425 	if (vp == NULL) {
426 		fdrop(fp, curthread);
427 		return EBADF;
428 	}
429 	dev = vn_todev(vp);
430 	if (dev == NODEV) {
431 		fdrop(fp, curthread);
432 		return EBADF;
433 	}
434 	SMB_CHECKMINOR(dev);
435 	ssp = sdp->sd_share;
436 	if (ssp == NULL) {
437 		fdrop(fp, curthread);
438 		return ENOTCONN;
439 	}
440 	error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
441 	if (error == 0)
442 		*sspp = ssp;
443 	fdrop(fp, curthread);
444 	return error;
445 }
446 
447