xref: /freebsd/sys/netsmb/smb_dev.c (revision 7afc53b8dfcc7d5897920ce6cc7e842fbb4ab813)
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 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/systm.h>
40 #include <sys/conf.h>
41 #include <sys/fcntl.h>
42 #include <sys/ioccom.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/file.h>		/* Must come after sys/malloc.h */
46 #include <sys/filedesc.h>
47 #include <sys/mbuf.h>
48 #include <sys/poll.h>
49 #include <sys/proc.h>
50 #include <sys/select.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/uio.h>
55 #include <sys/vnode.h>
56 
57 #include <net/if.h>
58 
59 #include <netsmb/smb.h>
60 #include <netsmb/smb_conn.h>
61 #include <netsmb/smb_subr.h>
62 #include <netsmb/smb_dev.h>
63 
64 #define SMB_GETDEV(dev)		((struct smb_dev*)(dev)->si_drv1)
65 #define	SMB_CHECKMINOR(dev)	do { \
66 				    sdp = SMB_GETDEV(dev); \
67 				    if (sdp == NULL) return ENXIO; \
68 				} while(0)
69 
70 static d_open_t	 nsmb_dev_open;
71 static d_close_t nsmb_dev_close;
72 static d_ioctl_t nsmb_dev_ioctl;
73 
74 MODULE_DEPEND(netsmb, libiconv, 1, 1, 2);
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 	.d_version =	D_VERSION,
92 	.d_flags =	D_NEEDGIANT,
93 	.d_open =	nsmb_dev_open,
94 	.d_close =	nsmb_dev_close,
95 	.d_ioctl =	nsmb_dev_ioctl,
96 	.d_name =	NSMB_NAME
97 };
98 
99 static eventhandler_tag nsmb_dev_tag;
100 
101 static void
102 nsmb_dev_clone(void *arg, char *name, int namelen, struct cdev **dev)
103 {
104 	int u;
105 
106 	if (*dev != NULL)
107 		return;
108 	if (dev_stdclone(name, NULL, NSMB_NAME, &u) != 1)
109 		return;
110 	*dev = make_dev(&nsmb_cdevsw, unit2minor(u), 0, 0, 0600,
111 	    NSMB_NAME"%d", u);
112 	dev_ref(*dev);
113 }
114 
115 static int
116 nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
117 {
118 	struct smb_dev *sdp;
119 	struct ucred *cred = td->td_ucred;
120 	int s;
121 
122 	sdp = SMB_GETDEV(dev);
123 	if (sdp && (sdp->sd_flags & NSMBFL_OPEN))
124 		return EBUSY;
125 	if (sdp == NULL) {
126 		sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK);
127 		dev->si_drv1 = (void*)sdp;
128 	}
129 	/*
130 	 * XXX: this is just crazy - make a device for an already passed device...
131 	 * someone should take care of it.
132 	 */
133 	if ((dev->si_flags & SI_NAMED) == 0)
134 		make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid, 0700,
135 		    NSMB_NAME"%d", dev2unit(dev));
136 	bzero(sdp, sizeof(*sdp));
137 /*
138 	STAILQ_INIT(&sdp->sd_rqlist);
139 	STAILQ_INIT(&sdp->sd_rplist);
140 	bzero(&sdp->sd_pollinfo, sizeof(struct selinfo));
141 */
142 	s = splimp();
143 	sdp->sd_level = -1;
144 	sdp->sd_flags |= NSMBFL_OPEN;
145 	splx(s);
146 	return 0;
147 }
148 
149 static int
150 nsmb_dev_close(struct cdev *dev, int flag, int fmt, struct thread *td)
151 {
152 	struct smb_dev *sdp;
153 	struct smb_vc *vcp;
154 	struct smb_share *ssp;
155 	struct smb_cred scred;
156 	int s;
157 
158 	SMB_CHECKMINOR(dev);
159 	s = splimp();
160 	if ((sdp->sd_flags & NSMBFL_OPEN) == 0) {
161 		splx(s);
162 		return EBADF;
163 	}
164 	smb_makescred(&scred, td, NULL);
165 	ssp = sdp->sd_share;
166 	if (ssp != NULL)
167 		smb_share_rele(ssp, &scred);
168 	vcp = sdp->sd_vc;
169 	if (vcp != NULL)
170 		smb_vc_rele(vcp, &scred);
171 /*
172 	smb_flushq(&sdp->sd_rqlist);
173 	smb_flushq(&sdp->sd_rplist);
174 */
175 	dev->si_drv1 = NULL;
176 	free(sdp, M_NSMBDEV);
177 	destroy_dev(dev);
178 	splx(s);
179 	return 0;
180 }
181 
182 
183 static int
184 nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
185 {
186 	struct smb_dev *sdp;
187 	struct smb_vc *vcp;
188 	struct smb_share *ssp;
189 	struct smb_cred scred;
190 	int error = 0;
191 
192 	SMB_CHECKMINOR(dev);
193 	if ((sdp->sd_flags & NSMBFL_OPEN) == 0)
194 		return EBADF;
195 
196 	smb_makescred(&scred, td, NULL);
197 	switch (cmd) {
198 	    case SMBIOC_OPENSESSION:
199 		if (sdp->sd_vc)
200 			return EISCONN;
201 		error = smb_usr_opensession((struct smbioc_ossn*)data,
202 		    &scred, &vcp);
203 		if (error)
204 			break;
205 		sdp->sd_vc = vcp;
206 		smb_vc_unlock(vcp, 0, td);
207 		sdp->sd_level = SMBL_VC;
208 		break;
209 	    case SMBIOC_OPENSHARE:
210 		if (sdp->sd_share)
211 			return EISCONN;
212 		if (sdp->sd_vc == NULL)
213 			return ENOTCONN;
214 		error = smb_usr_openshare(sdp->sd_vc,
215 		    (struct smbioc_oshare*)data, &scred, &ssp);
216 		if (error)
217 			break;
218 		sdp->sd_share = ssp;
219 		smb_share_unlock(ssp, 0, td);
220 		sdp->sd_level = SMBL_SHARE;
221 		break;
222 	    case SMBIOC_REQUEST:
223 		if (sdp->sd_share == NULL)
224 			return ENOTCONN;
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 			return ENOTCONN;
231 		error = smb_usr_t2request(sdp->sd_share,
232 		    (struct smbioc_t2rq*)data, &scred);
233 		break;
234 	    case SMBIOC_SETFLAGS: {
235 		struct smbioc_flags *fl = (struct smbioc_flags*)data;
236 		int on;
237 
238 		if (fl->ioc_level == SMBL_VC) {
239 			if (fl->ioc_mask & SMBV_PERMANENT) {
240 				on = fl->ioc_flags & SMBV_PERMANENT;
241 				if ((vcp = sdp->sd_vc) == NULL)
242 					return ENOTCONN;
243 				error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred);
244 				if (error)
245 					break;
246 				if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
247 					vcp->obj.co_flags |= SMBV_PERMANENT;
248 					smb_vc_ref(vcp);
249 				} else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
250 					vcp->obj.co_flags &= ~SMBV_PERMANENT;
251 					smb_vc_rele(vcp, &scred);
252 				}
253 				smb_vc_put(vcp, &scred);
254 			} else
255 				error = EINVAL;
256 		} else if (fl->ioc_level == SMBL_SHARE) {
257 			if (fl->ioc_mask & SMBS_PERMANENT) {
258 				on = fl->ioc_flags & SMBS_PERMANENT;
259 				if ((ssp = sdp->sd_share) == NULL)
260 					return ENOTCONN;
261 				error = smb_share_get(ssp, LK_EXCLUSIVE, &scred);
262 				if (error)
263 					break;
264 				if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
265 					ssp->obj.co_flags |= SMBS_PERMANENT;
266 					smb_share_ref(ssp);
267 				} else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
268 					ssp->obj.co_flags &= ~SMBS_PERMANENT;
269 					smb_share_rele(ssp, &scred);
270 				}
271 				smb_share_put(ssp, &scred);
272 			} else
273 				error = EINVAL;
274 			break;
275 		} else
276 			error = EINVAL;
277 		break;
278 	    }
279 	    case SMBIOC_LOOKUP:
280 		if (sdp->sd_vc || sdp->sd_share)
281 			return EISCONN;
282 		vcp = NULL;
283 		ssp = NULL;
284 		error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp);
285 		if (error)
286 			break;
287 		if (vcp) {
288 			sdp->sd_vc = vcp;
289 			smb_vc_unlock(vcp, 0, td);
290 			sdp->sd_level = SMBL_VC;
291 		}
292 		if (ssp) {
293 			sdp->sd_share = ssp;
294 			smb_share_unlock(ssp, 0, td);
295 			sdp->sd_level = SMBL_SHARE;
296 		}
297 		break;
298 	    case SMBIOC_READ: case SMBIOC_WRITE: {
299 		struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
300 		struct uio auio;
301 		struct iovec iov;
302 
303 		if ((ssp = sdp->sd_share) == NULL)
304 			return ENOTCONN;
305 		iov.iov_base = rwrq->ioc_base;
306 		iov.iov_len = rwrq->ioc_cnt;
307 		auio.uio_iov = &iov;
308 		auio.uio_iovcnt = 1;
309 		auio.uio_offset = rwrq->ioc_offset;
310 		auio.uio_resid = rwrq->ioc_cnt;
311 		auio.uio_segflg = UIO_USERSPACE;
312 		auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
313 		auio.uio_td = td;
314 		if (cmd == SMBIOC_READ)
315 			error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred);
316 		else
317 			error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred);
318 		rwrq->ioc_cnt -= auio.uio_resid;
319 		break;
320 	    }
321 	    default:
322 		error = ENODEV;
323 	}
324 	return error;
325 }
326 
327 static int
328 nsmb_dev_load(module_t mod, int cmd, void *arg)
329 {
330 	int error = 0;
331 
332 	switch (cmd) {
333 	    case MOD_LOAD:
334 		error = smb_sm_init();
335 		if (error)
336 			break;
337 		error = smb_iod_init();
338 		if (error) {
339 			smb_sm_done();
340 			break;
341 		}
342 		nsmb_dev_tag = EVENTHANDLER_REGISTER(dev_clone, nsmb_dev_clone, 0, 1000);
343 		printf("netsmb_dev: loaded\n");
344 		break;
345 	    case MOD_UNLOAD:
346 		smb_iod_done();
347 		error = smb_sm_done();
348 		error = 0;
349 		EVENTHANDLER_DEREGISTER(dev_clone, nsmb_dev_tag);
350 		printf("netsmb_dev: unloaded\n");
351 		break;
352 	    default:
353 		error = EINVAL;
354 		break;
355 	}
356 	return error;
357 }
358 
359 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
360 
361 /*
362  * Convert a file descriptor to appropriate smb_share pointer
363  */
364 static struct file*
365 nsmb_getfp(struct filedesc* fdp, int fd, int flag)
366 {
367 	struct file* fp;
368 
369 	FILEDESC_LOCK(fdp);
370 	if (((u_int)fd) >= fdp->fd_nfiles ||
371 	    (fp = fdp->fd_ofiles[fd]) == NULL ||
372 	    (fp->f_flag & flag) == 0) {
373 		FILEDESC_UNLOCK(fdp);
374 		return (NULL);
375 	}
376 	fhold(fp);
377 	FILEDESC_UNLOCK(fdp);
378 	return (fp);
379 }
380 
381 int
382 smb_dev2share(int fd, int mode, struct smb_cred *scred,
383 	struct smb_share **sspp)
384 {
385 	struct file *fp;
386 	struct vnode *vp;
387 	struct smb_dev *sdp;
388 	struct smb_share *ssp;
389 	struct cdev *dev;
390 	int error;
391 
392 	fp = nsmb_getfp(scred->scr_td->td_proc->p_fd, fd, FREAD | FWRITE);
393 	if (fp == NULL)
394 		return EBADF;
395 	vp = fp->f_vnode;
396 	if (vp == NULL) {
397 		fdrop(fp, curthread);
398 		return EBADF;
399 	}
400 	if (vp->v_type != VCHR) {
401 		fdrop(fp, curthread);
402 		return EBADF;
403 	}
404 	dev = vp->v_rdev;
405 	SMB_CHECKMINOR(dev);
406 	ssp = sdp->sd_share;
407 	if (ssp == NULL) {
408 		fdrop(fp, curthread);
409 		return ENOTCONN;
410 	}
411 	error = smb_share_get(ssp, LK_EXCLUSIVE, scred);
412 	if (error == 0)
413 		*sspp = ssp;
414 	fdrop(fp, curthread);
415 	return error;
416 }
417 
418