2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2000-2001 Boris Popov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/capsicum.h>
32 #include <sys/module.h>
33 #include <sys/systm.h>
35 #include <sys/fcntl.h>
36 #include <sys/ioccom.h>
38 #include <sys/malloc.h>
39 #include <sys/file.h> /* Must come after sys/malloc.h */
40 #include <sys/filedesc.h>
44 #include <sys/select.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/sysctl.h>
49 #include <sys/vnode.h>
53 #include <netsmb/smb.h>
54 #include <netsmb/smb_conn.h>
55 #include <netsmb/smb_subr.h>
56 #include <netsmb/smb_dev.h>
58 static struct cdev *nsmb_dev;
60 static d_open_t nsmb_dev_open;
61 static d_ioctl_t nsmb_dev_ioctl;
63 MODULE_DEPEND(netsmb, libiconv, 1, 1, 2);
64 MODULE_VERSION(netsmb, NSMB_VERSION);
66 static int smb_version = NSMB_VERSION;
69 SYSCTL_DECL(_net_smb);
70 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
72 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
74 static struct cdevsw nsmb_cdevsw = {
75 .d_version = D_VERSION,
76 .d_open = nsmb_dev_open,
77 .d_ioctl = nsmb_dev_ioctl,
85 nsmb_dev = make_dev(&nsmb_cdevsw, 0, UID_ROOT, GID_OPERATOR,
93 nsmb_dev_destroy(void)
96 MPASS(nsmb_dev != NULL);
97 destroy_dev(nsmb_dev);
101 static struct smb_dev *
102 smbdev_alloc(struct cdev *dev)
106 sdp = malloc(sizeof(struct smb_dev), M_NSMBDEV, M_WAITOK | M_ZERO);
109 sdp->sd_flags |= NSMBFL_OPEN;
119 dev = (struct smb_dev *)arg;
126 nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
131 sdp = smbdev_alloc(dev);
132 error = devfs_set_cdevpriv(sdp, sdp_dtor);
134 free(sdp, M_NSMBDEV);
141 sdp_trydestroy(struct smb_dev *sdp)
144 struct smb_share *ssp;
145 struct smb_cred *scred;
149 panic("No smb_dev upon device close");
150 MPASS(sdp->refcount > 0);
154 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
155 smb_makescred(scred, curthread, NULL);
159 smb_share_rele(ssp, scred);
164 smb_vc_rele(vcp, scred);
166 free(scred, M_NSMBDEV);
167 free(sdp, M_NSMBDEV);
172 nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
176 struct smb_share *ssp;
177 struct smb_cred *scred;
180 error = devfs_get_cdevpriv((void **)&sdp);
183 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
185 smb_makescred(scred, td, NULL);
187 case SMBIOC_OPENSESSION:
192 error = smb_usr_opensession((struct smbioc_ossn*)data,
198 sdp->sd_level = SMBL_VC;
200 case SMBIOC_OPENSHARE:
205 if (sdp->sd_vc == NULL) {
209 error = smb_usr_openshare(sdp->sd_vc,
210 (struct smbioc_oshare*)data, scred, &ssp);
214 smb_share_unlock(ssp);
215 sdp->sd_level = SMBL_SHARE;
218 if (sdp->sd_share == NULL) {
222 error = smb_usr_simplerequest(sdp->sd_share,
223 (struct smbioc_rq*)data, scred);
226 if (sdp->sd_share == NULL) {
230 error = smb_usr_t2request(sdp->sd_share,
231 (struct smbioc_t2rq*)data, scred);
233 case SMBIOC_SETFLAGS: {
234 struct smbioc_flags *fl = (struct smbioc_flags*)data;
237 if (fl->ioc_level == SMBL_VC) {
238 if (fl->ioc_mask & SMBV_PERMANENT) {
239 on = fl->ioc_flags & SMBV_PERMANENT;
240 if ((vcp = sdp->sd_vc) == NULL) {
244 error = smb_vc_get(vcp, scred);
247 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
248 vcp->obj.co_flags |= SMBV_PERMANENT;
250 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
251 vcp->obj.co_flags &= ~SMBV_PERMANENT;
252 smb_vc_rele(vcp, scred);
254 smb_vc_put(vcp, scred);
257 } else if (fl->ioc_level == SMBL_SHARE) {
258 if (fl->ioc_mask & SMBS_PERMANENT) {
259 on = fl->ioc_flags & SMBS_PERMANENT;
260 if ((ssp = sdp->sd_share) == NULL) {
264 error = smb_share_get(ssp, scred);
267 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
268 ssp->obj.co_flags |= SMBS_PERMANENT;
270 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
271 ssp->obj.co_flags &= ~SMBS_PERMANENT;
272 smb_share_rele(ssp, scred);
274 smb_share_put(ssp, scred);
283 if (sdp->sd_vc || sdp->sd_share) {
289 error = smb_usr_lookup((struct smbioc_lookup*)data, scred, &vcp, &ssp);
295 sdp->sd_level = SMBL_VC;
299 smb_share_unlock(ssp);
300 sdp->sd_level = SMBL_SHARE;
303 case SMBIOC_READ: case SMBIOC_WRITE: {
304 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
308 if ((ssp = sdp->sd_share) == NULL) {
312 iov.iov_base = rwrq->ioc_base;
313 iov.iov_len = rwrq->ioc_cnt;
316 auio.uio_offset = rwrq->ioc_offset;
317 auio.uio_resid = rwrq->ioc_cnt;
318 auio.uio_segflg = UIO_USERSPACE;
319 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
321 if (cmd == SMBIOC_READ)
322 error = smb_read(ssp, rwrq->ioc_fh, &auio, scred);
324 error = smb_write(ssp, rwrq->ioc_fh, &auio, scred);
325 rwrq->ioc_cnt -= auio.uio_resid;
332 free(scred, M_NSMBDEV);
338 nsmb_dev_load(module_t mod, int cmd, void *arg)
344 error = smb_sm_init();
347 error = smb_iod_init();
352 error = nsmb_dev_init();
355 sx_init(&smb_lock, "samba device lock");
359 error = smb_sm_done();
363 sx_destroy(&smb_lock);
372 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
375 smb_dev2share(int fd, int mode, struct smb_cred *scred,
376 struct smb_share **sspp, struct smb_dev **ssdp)
378 struct file *fp, *fptmp;
380 struct smb_share *ssp;
385 error = fget(td, fd, &cap_read_rights, &fp);
390 error = devfs_get_cdevpriv((void **)&sdp);
393 if (error || sdp == NULL)
402 error = smb_share_get(ssp, scred);