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/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/capsicum.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;
70 SYSCTL_DECL(_net_smb);
71 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
73 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
75 static struct cdevsw nsmb_cdevsw = {
76 .d_version = D_VERSION,
77 .d_open = nsmb_dev_open,
78 .d_ioctl = nsmb_dev_ioctl,
86 nsmb_dev = make_dev(&nsmb_cdevsw, 0, UID_ROOT, GID_OPERATOR,
94 nsmb_dev_destroy(void)
97 MPASS(nsmb_dev != NULL);
98 destroy_dev(nsmb_dev);
102 static struct smb_dev *
103 smbdev_alloc(struct cdev *dev)
107 sdp = malloc(sizeof(struct smb_dev), M_NSMBDEV, M_WAITOK | M_ZERO);
110 sdp->sd_flags |= NSMBFL_OPEN;
120 dev = (struct smb_dev *)arg;
127 nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
132 sdp = smbdev_alloc(dev);
133 error = devfs_set_cdevpriv(sdp, sdp_dtor);
135 free(sdp, M_NSMBDEV);
142 sdp_trydestroy(struct smb_dev *sdp)
145 struct smb_share *ssp;
146 struct smb_cred *scred;
150 panic("No smb_dev upon device close");
151 MPASS(sdp->refcount > 0);
155 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
156 smb_makescred(scred, curthread, NULL);
160 smb_share_rele(ssp, scred);
165 smb_vc_rele(vcp, scred);
167 free(scred, M_NSMBDEV);
168 free(sdp, M_NSMBDEV);
173 nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
177 struct smb_share *ssp;
178 struct smb_cred *scred;
181 error = devfs_get_cdevpriv((void **)&sdp);
184 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
186 smb_makescred(scred, td, NULL);
188 case SMBIOC_OPENSESSION:
193 error = smb_usr_opensession((struct smbioc_ossn*)data,
199 sdp->sd_level = SMBL_VC;
201 case SMBIOC_OPENSHARE:
206 if (sdp->sd_vc == NULL) {
210 error = smb_usr_openshare(sdp->sd_vc,
211 (struct smbioc_oshare*)data, scred, &ssp);
215 smb_share_unlock(ssp);
216 sdp->sd_level = SMBL_SHARE;
219 if (sdp->sd_share == NULL) {
223 error = smb_usr_simplerequest(sdp->sd_share,
224 (struct smbioc_rq*)data, scred);
227 if (sdp->sd_share == NULL) {
231 error = smb_usr_t2request(sdp->sd_share,
232 (struct smbioc_t2rq*)data, scred);
234 case SMBIOC_SETFLAGS: {
235 struct smbioc_flags *fl = (struct smbioc_flags*)data;
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) {
245 error = smb_vc_get(vcp, scred);
248 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
249 vcp->obj.co_flags |= SMBV_PERMANENT;
251 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
252 vcp->obj.co_flags &= ~SMBV_PERMANENT;
253 smb_vc_rele(vcp, scred);
255 smb_vc_put(vcp, scred);
258 } else if (fl->ioc_level == SMBL_SHARE) {
259 if (fl->ioc_mask & SMBS_PERMANENT) {
260 on = fl->ioc_flags & SMBS_PERMANENT;
261 if ((ssp = sdp->sd_share) == NULL) {
265 error = smb_share_get(ssp, scred);
268 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
269 ssp->obj.co_flags |= SMBS_PERMANENT;
271 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
272 ssp->obj.co_flags &= ~SMBS_PERMANENT;
273 smb_share_rele(ssp, scred);
275 smb_share_put(ssp, scred);
284 if (sdp->sd_vc || sdp->sd_share) {
290 error = smb_usr_lookup((struct smbioc_lookup*)data, scred, &vcp, &ssp);
296 sdp->sd_level = SMBL_VC;
300 smb_share_unlock(ssp);
301 sdp->sd_level = SMBL_SHARE;
304 case SMBIOC_READ: case SMBIOC_WRITE: {
305 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
309 if ((ssp = sdp->sd_share) == NULL) {
313 iov.iov_base = rwrq->ioc_base;
314 iov.iov_len = rwrq->ioc_cnt;
317 auio.uio_offset = rwrq->ioc_offset;
318 auio.uio_resid = rwrq->ioc_cnt;
319 auio.uio_segflg = UIO_USERSPACE;
320 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
322 if (cmd == SMBIOC_READ)
323 error = smb_read(ssp, rwrq->ioc_fh, &auio, scred);
325 error = smb_write(ssp, rwrq->ioc_fh, &auio, scred);
326 rwrq->ioc_cnt -= auio.uio_resid;
333 free(scred, M_NSMBDEV);
339 nsmb_dev_load(module_t mod, int cmd, void *arg)
345 error = smb_sm_init();
348 error = smb_iod_init();
353 error = nsmb_dev_init();
356 sx_init(&smb_lock, "samba device lock");
360 error = smb_sm_done();
364 sx_destroy(&smb_lock);
373 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
376 smb_dev2share(int fd, int mode, struct smb_cred *scred,
377 struct smb_share **sspp, struct smb_dev **ssdp)
379 struct file *fp, *fptmp;
381 struct smb_share *ssp;
386 error = fget(td, fd, &cap_read_rights, &fp);
391 error = devfs_get_cdevpriv((void **)&sdp);
394 if (error || sdp == NULL)
403 error = smb_share_get(ssp, scred);