2 * Copyright (c) 2000-2001 Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/capability.h>
33 #include <sys/module.h>
34 #include <sys/systm.h>
36 #include <sys/fcntl.h>
37 #include <sys/ioccom.h>
39 #include <sys/malloc.h>
40 #include <sys/file.h> /* Must come after sys/malloc.h */
41 #include <sys/filedesc.h>
45 #include <sys/select.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
50 #include <sys/vnode.h>
54 #include <netsmb/smb.h>
55 #include <netsmb/smb_conn.h>
56 #include <netsmb/smb_subr.h>
57 #include <netsmb/smb_dev.h>
59 static struct cdev *nsmb_dev;
61 static d_open_t nsmb_dev_open;
62 static d_ioctl_t nsmb_dev_ioctl;
64 MODULE_DEPEND(netsmb, libiconv, 1, 1, 2);
65 MODULE_VERSION(netsmb, NSMB_VERSION);
67 static int smb_version = NSMB_VERSION;
71 SYSCTL_DECL(_net_smb);
72 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
74 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
76 static struct cdevsw nsmb_cdevsw = {
77 .d_version = D_VERSION,
78 .d_open = nsmb_dev_open,
79 .d_ioctl = nsmb_dev_ioctl,
87 nsmb_dev = make_dev(&nsmb_cdevsw, 0, UID_ROOT, GID_OPERATOR,
95 nsmb_dev_destroy(void)
98 MPASS(nsmb_dev != NULL);
99 destroy_dev(nsmb_dev);
103 static struct smb_dev *
104 smbdev_alloc(struct cdev *dev)
108 sdp = malloc(sizeof(struct smb_dev), M_NSMBDEV, M_WAITOK | M_ZERO);
111 sdp->sd_flags |= NSMBFL_OPEN;
121 dev = (struct smb_dev *)arg;
128 nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
133 sdp = smbdev_alloc(dev);
134 error = devfs_set_cdevpriv(sdp, sdp_dtor);
136 free(sdp, M_NSMBDEV);
143 sdp_trydestroy(struct smb_dev *sdp)
146 struct smb_share *ssp;
147 struct smb_cred *scred;
151 panic("No smb_dev upon device close");
152 MPASS(sdp->refcount > 0);
156 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
157 smb_makescred(scred, curthread, NULL);
161 smb_share_rele(ssp, scred);
166 smb_vc_rele(vcp, scred);
168 free(scred, M_NSMBDEV);
169 free(sdp, M_NSMBDEV);
175 nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
179 struct smb_share *ssp;
180 struct smb_cred *scred;
183 error = devfs_get_cdevpriv((void **)&sdp);
186 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
188 smb_makescred(scred, td, NULL);
190 case SMBIOC_OPENSESSION:
195 error = smb_usr_opensession((struct smbioc_ossn*)data,
201 sdp->sd_level = SMBL_VC;
203 case SMBIOC_OPENSHARE:
208 if (sdp->sd_vc == NULL) {
212 error = smb_usr_openshare(sdp->sd_vc,
213 (struct smbioc_oshare*)data, scred, &ssp);
217 smb_share_unlock(ssp);
218 sdp->sd_level = SMBL_SHARE;
221 if (sdp->sd_share == NULL) {
225 error = smb_usr_simplerequest(sdp->sd_share,
226 (struct smbioc_rq*)data, scred);
229 if (sdp->sd_share == NULL) {
233 error = smb_usr_t2request(sdp->sd_share,
234 (struct smbioc_t2rq*)data, scred);
236 case SMBIOC_SETFLAGS: {
237 struct smbioc_flags *fl = (struct smbioc_flags*)data;
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) {
247 error = smb_vc_get(vcp, scred);
250 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
251 vcp->obj.co_flags |= SMBV_PERMANENT;
253 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
254 vcp->obj.co_flags &= ~SMBV_PERMANENT;
255 smb_vc_rele(vcp, scred);
257 smb_vc_put(vcp, scred);
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) {
267 error = smb_share_get(ssp, scred);
270 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
271 ssp->obj.co_flags |= SMBS_PERMANENT;
273 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
274 ssp->obj.co_flags &= ~SMBS_PERMANENT;
275 smb_share_rele(ssp, scred);
277 smb_share_put(ssp, scred);
286 if (sdp->sd_vc || sdp->sd_share) {
292 error = smb_usr_lookup((struct smbioc_lookup*)data, scred, &vcp, &ssp);
298 sdp->sd_level = SMBL_VC;
302 smb_share_unlock(ssp);
303 sdp->sd_level = SMBL_SHARE;
306 case SMBIOC_READ: case SMBIOC_WRITE: {
307 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
311 if ((ssp = sdp->sd_share) == NULL) {
315 iov.iov_base = rwrq->ioc_base;
316 iov.iov_len = rwrq->ioc_cnt;
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;
324 if (cmd == SMBIOC_READ)
325 error = smb_read(ssp, rwrq->ioc_fh, &auio, scred);
327 error = smb_write(ssp, rwrq->ioc_fh, &auio, scred);
328 rwrq->ioc_cnt -= auio.uio_resid;
335 free(scred, M_NSMBDEV);
341 nsmb_dev_load(module_t mod, int cmd, void *arg)
347 error = smb_sm_init();
350 error = smb_iod_init();
355 error = nsmb_dev_init();
358 sx_init(&smb_lock, "samba device lock");
362 error = smb_sm_done();
366 sx_destroy(&smb_lock);
375 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
378 smb_dev2share(int fd, int mode, struct smb_cred *scred,
379 struct smb_share **sspp, struct smb_dev **ssdp)
382 struct file *fp, *fptmp;
384 struct smb_share *ssp;
389 error = fget(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
394 error = devfs_get_cdevpriv((void **)&sdp);
397 if (error || sdp == NULL)
406 error = smb_share_get(ssp, scred);