2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/capsicum.h>
35 #include <sys/module.h>
36 #include <sys/systm.h>
38 #include <sys/fcntl.h>
39 #include <sys/ioccom.h>
41 #include <sys/malloc.h>
42 #include <sys/file.h> /* Must come after sys/malloc.h */
43 #include <sys/filedesc.h>
47 #include <sys/select.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
52 #include <sys/vnode.h>
56 #include <netsmb/smb.h>
57 #include <netsmb/smb_conn.h>
58 #include <netsmb/smb_subr.h>
59 #include <netsmb/smb_dev.h>
61 static struct cdev *nsmb_dev;
63 static d_open_t nsmb_dev_open;
64 static d_ioctl_t nsmb_dev_ioctl;
66 MODULE_DEPEND(netsmb, libiconv, 1, 1, 2);
67 MODULE_VERSION(netsmb, NSMB_VERSION);
69 static int smb_version = NSMB_VERSION;
73 SYSCTL_DECL(_net_smb);
74 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
76 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
78 static struct cdevsw nsmb_cdevsw = {
79 .d_version = D_VERSION,
80 .d_open = nsmb_dev_open,
81 .d_ioctl = nsmb_dev_ioctl,
89 nsmb_dev = make_dev(&nsmb_cdevsw, 0, UID_ROOT, GID_OPERATOR,
97 nsmb_dev_destroy(void)
100 MPASS(nsmb_dev != NULL);
101 destroy_dev(nsmb_dev);
105 static struct smb_dev *
106 smbdev_alloc(struct cdev *dev)
110 sdp = malloc(sizeof(struct smb_dev), M_NSMBDEV, M_WAITOK | M_ZERO);
113 sdp->sd_flags |= NSMBFL_OPEN;
123 dev = (struct smb_dev *)arg;
130 nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
135 sdp = smbdev_alloc(dev);
136 error = devfs_set_cdevpriv(sdp, sdp_dtor);
138 free(sdp, M_NSMBDEV);
145 sdp_trydestroy(struct smb_dev *sdp)
148 struct smb_share *ssp;
149 struct smb_cred *scred;
153 panic("No smb_dev upon device close");
154 MPASS(sdp->refcount > 0);
158 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
159 smb_makescred(scred, curthread, NULL);
163 smb_share_rele(ssp, scred);
168 smb_vc_rele(vcp, scred);
170 free(scred, M_NSMBDEV);
171 free(sdp, M_NSMBDEV);
177 nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
181 struct smb_share *ssp;
182 struct smb_cred *scred;
185 error = devfs_get_cdevpriv((void **)&sdp);
188 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
190 smb_makescred(scred, td, NULL);
192 case SMBIOC_OPENSESSION:
197 error = smb_usr_opensession((struct smbioc_ossn*)data,
203 sdp->sd_level = SMBL_VC;
205 case SMBIOC_OPENSHARE:
210 if (sdp->sd_vc == NULL) {
214 error = smb_usr_openshare(sdp->sd_vc,
215 (struct smbioc_oshare*)data, scred, &ssp);
219 smb_share_unlock(ssp);
220 sdp->sd_level = SMBL_SHARE;
223 if (sdp->sd_share == NULL) {
227 error = smb_usr_simplerequest(sdp->sd_share,
228 (struct smbioc_rq*)data, scred);
231 if (sdp->sd_share == NULL) {
235 error = smb_usr_t2request(sdp->sd_share,
236 (struct smbioc_t2rq*)data, scred);
238 case SMBIOC_SETFLAGS: {
239 struct smbioc_flags *fl = (struct smbioc_flags*)data;
242 if (fl->ioc_level == SMBL_VC) {
243 if (fl->ioc_mask & SMBV_PERMANENT) {
244 on = fl->ioc_flags & SMBV_PERMANENT;
245 if ((vcp = sdp->sd_vc) == NULL) {
249 error = smb_vc_get(vcp, scred);
252 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
253 vcp->obj.co_flags |= SMBV_PERMANENT;
255 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
256 vcp->obj.co_flags &= ~SMBV_PERMANENT;
257 smb_vc_rele(vcp, scred);
259 smb_vc_put(vcp, scred);
262 } else if (fl->ioc_level == SMBL_SHARE) {
263 if (fl->ioc_mask & SMBS_PERMANENT) {
264 on = fl->ioc_flags & SMBS_PERMANENT;
265 if ((ssp = sdp->sd_share) == NULL) {
269 error = smb_share_get(ssp, scred);
272 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
273 ssp->obj.co_flags |= SMBS_PERMANENT;
275 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
276 ssp->obj.co_flags &= ~SMBS_PERMANENT;
277 smb_share_rele(ssp, scred);
279 smb_share_put(ssp, scred);
288 if (sdp->sd_vc || sdp->sd_share) {
294 error = smb_usr_lookup((struct smbioc_lookup*)data, scred, &vcp, &ssp);
300 sdp->sd_level = SMBL_VC;
304 smb_share_unlock(ssp);
305 sdp->sd_level = SMBL_SHARE;
308 case SMBIOC_READ: case SMBIOC_WRITE: {
309 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
313 if ((ssp = sdp->sd_share) == NULL) {
317 iov.iov_base = rwrq->ioc_base;
318 iov.iov_len = rwrq->ioc_cnt;
321 auio.uio_offset = rwrq->ioc_offset;
322 auio.uio_resid = rwrq->ioc_cnt;
323 auio.uio_segflg = UIO_USERSPACE;
324 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE;
326 if (cmd == SMBIOC_READ)
327 error = smb_read(ssp, rwrq->ioc_fh, &auio, scred);
329 error = smb_write(ssp, rwrq->ioc_fh, &auio, scred);
330 rwrq->ioc_cnt -= auio.uio_resid;
337 free(scred, M_NSMBDEV);
343 nsmb_dev_load(module_t mod, int cmd, void *arg)
349 error = smb_sm_init();
352 error = smb_iod_init();
357 error = nsmb_dev_init();
360 sx_init(&smb_lock, "samba device lock");
364 error = smb_sm_done();
368 sx_destroy(&smb_lock);
377 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
380 smb_dev2share(int fd, int mode, struct smb_cred *scred,
381 struct smb_share **sspp, struct smb_dev **ssdp)
384 struct file *fp, *fptmp;
386 struct smb_share *ssp;
391 error = fget(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
396 error = devfs_get_cdevpriv((void **)&sdp);
399 if (error || sdp == NULL)
408 error = smb_share_get(ssp, scred);