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