1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2000-2001 Boris Popov
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
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>
35 #include <sys/conf.h>
36 #include <sys/fcntl.h>
37 #include <sys/ioccom.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/file.h> /* Must come after sys/malloc.h */
41 #include <sys/filedesc.h>
42 #include <sys/mbuf.h>
43 #include <sys/poll.h>
44 #include <sys/proc.h>
45 #include <sys/select.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
49 #include <sys/uio.h>
50 #include <sys/vnode.h>
51
52 #include <net/if.h>
53
54 #include <netsmb/smb.h>
55 #include <netsmb/smb_conn.h>
56 #include <netsmb/smb_subr.h>
57 #include <netsmb/smb_dev.h>
58
59 static struct cdev *nsmb_dev;
60
61 static d_open_t nsmb_dev_open;
62 static d_ioctl_t nsmb_dev_ioctl;
63
64 MODULE_DEPEND(netsmb, libiconv, 1, 1, 2);
65 MODULE_VERSION(netsmb, NSMB_VERSION);
66
67 static int smb_version = NSMB_VERSION;
68 struct sx smb_lock;
69
70 SYSCTL_DECL(_net_smb);
71 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, "");
72
73 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device");
74
75 static struct cdevsw nsmb_cdevsw = {
76 .d_version = D_VERSION,
77 .d_open = nsmb_dev_open,
78 .d_ioctl = nsmb_dev_ioctl,
79 .d_name = NSMB_NAME
80 };
81
82 static int
nsmb_dev_init(void)83 nsmb_dev_init(void)
84 {
85
86 nsmb_dev = make_dev(&nsmb_cdevsw, 0, UID_ROOT, GID_OPERATOR,
87 0600, "nsmb");
88 if (nsmb_dev == NULL)
89 return (ENOMEM);
90 return (0);
91 }
92
93 static void
nsmb_dev_destroy(void)94 nsmb_dev_destroy(void)
95 {
96
97 MPASS(nsmb_dev != NULL);
98 destroy_dev(nsmb_dev);
99 nsmb_dev = NULL;
100 }
101
102 static struct smb_dev *
smbdev_alloc(struct cdev * dev)103 smbdev_alloc(struct cdev *dev)
104 {
105 struct smb_dev *sdp;
106
107 sdp = malloc(sizeof(struct smb_dev), M_NSMBDEV, M_WAITOK | M_ZERO);
108 sdp->dev = dev;
109 sdp->sd_level = -1;
110 sdp->sd_flags |= NSMBFL_OPEN;
111 sdp->refcount = 1;
112 return (sdp);
113 }
114
115 void
sdp_dtor(void * arg)116 sdp_dtor(void *arg)
117 {
118 struct smb_dev *dev;
119
120 dev = (struct smb_dev *)arg;
121 SMB_LOCK();
122 sdp_trydestroy(dev);
123 SMB_UNLOCK();
124 }
125
126 static int
nsmb_dev_open(struct cdev * dev,int oflags,int devtype,struct thread * td)127 nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
128 {
129 struct smb_dev *sdp;
130 int error;
131
132 sdp = smbdev_alloc(dev);
133 error = devfs_set_cdevpriv(sdp, sdp_dtor);
134 if (error) {
135 free(sdp, M_NSMBDEV);
136 return (error);
137 }
138 return (0);
139 }
140
141 void
sdp_trydestroy(struct smb_dev * sdp)142 sdp_trydestroy(struct smb_dev *sdp)
143 {
144 struct smb_vc *vcp;
145 struct smb_share *ssp;
146 struct smb_cred *scred;
147
148 SMB_LOCKASSERT();
149 if (!sdp)
150 panic("No smb_dev upon device close");
151 MPASS(sdp->refcount > 0);
152 sdp->refcount--;
153 if (sdp->refcount)
154 return;
155 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
156 smb_makescred(scred, curthread, NULL);
157 ssp = sdp->sd_share;
158 if (ssp != NULL) {
159 smb_share_lock(ssp);
160 smb_share_rele(ssp, scred);
161 }
162 vcp = sdp->sd_vc;
163 if (vcp != NULL) {
164 smb_vc_lock(vcp);
165 smb_vc_rele(vcp, scred);
166 }
167 free(scred, M_NSMBDEV);
168 free(sdp, M_NSMBDEV);
169 return;
170 }
171
172 static int
nsmb_dev_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int flag,struct thread * td)173 nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
174 {
175 struct smb_dev *sdp;
176 struct smb_vc *vcp;
177 struct smb_share *ssp;
178 struct smb_cred *scred;
179 int error = 0;
180
181 error = devfs_get_cdevpriv((void **)&sdp);
182 if (error)
183 return (error);
184 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK);
185 SMB_LOCK();
186 smb_makescred(scred, td, NULL);
187 switch (cmd) {
188 case SMBIOC_OPENSESSION:
189 if (sdp->sd_vc) {
190 error = EISCONN;
191 goto out;
192 }
193 error = smb_usr_opensession((struct smbioc_ossn*)data,
194 scred, &vcp);
195 if (error)
196 break;
197 sdp->sd_vc = vcp;
198 smb_vc_unlock(vcp);
199 sdp->sd_level = SMBL_VC;
200 break;
201 case SMBIOC_OPENSHARE:
202 if (sdp->sd_share) {
203 error = EISCONN;
204 goto out;
205 }
206 if (sdp->sd_vc == NULL) {
207 error = ENOTCONN;
208 goto out;
209 }
210 error = smb_usr_openshare(sdp->sd_vc,
211 (struct smbioc_oshare*)data, scred, &ssp);
212 if (error)
213 break;
214 sdp->sd_share = ssp;
215 smb_share_unlock(ssp);
216 sdp->sd_level = SMBL_SHARE;
217 break;
218 case SMBIOC_REQUEST:
219 if (sdp->sd_share == NULL) {
220 error = ENOTCONN;
221 goto out;
222 }
223 error = smb_usr_simplerequest(sdp->sd_share,
224 (struct smbioc_rq*)data, scred);
225 break;
226 case SMBIOC_T2RQ:
227 if (sdp->sd_share == NULL) {
228 error = ENOTCONN;
229 goto out;
230 }
231 error = smb_usr_t2request(sdp->sd_share,
232 (struct smbioc_t2rq*)data, scred);
233 break;
234 case SMBIOC_SETFLAGS: {
235 struct smbioc_flags *fl = (struct smbioc_flags*)data;
236 int on;
237
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) {
242 error = ENOTCONN;
243 goto out;
244 }
245 error = smb_vc_get(vcp, scred);
246 if (error)
247 break;
248 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) {
249 vcp->obj.co_flags |= SMBV_PERMANENT;
250 smb_vc_ref(vcp);
251 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) {
252 vcp->obj.co_flags &= ~SMBV_PERMANENT;
253 smb_vc_rele(vcp, scred);
254 }
255 smb_vc_put(vcp, scred);
256 } else
257 error = EINVAL;
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) {
262 error = ENOTCONN;
263 goto out;
264 }
265 error = smb_share_get(ssp, scred);
266 if (error)
267 break;
268 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) {
269 ssp->obj.co_flags |= SMBS_PERMANENT;
270 smb_share_ref(ssp);
271 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) {
272 ssp->obj.co_flags &= ~SMBS_PERMANENT;
273 smb_share_rele(ssp, scred);
274 }
275 smb_share_put(ssp, scred);
276 } else
277 error = EINVAL;
278 break;
279 } else
280 error = EINVAL;
281 break;
282 }
283 case SMBIOC_LOOKUP:
284 if (sdp->sd_vc || sdp->sd_share) {
285 error = EISCONN;
286 goto out;
287 }
288 vcp = NULL;
289 ssp = NULL;
290 error = smb_usr_lookup((struct smbioc_lookup*)data, scred, &vcp, &ssp);
291 if (error)
292 break;
293 if (vcp) {
294 sdp->sd_vc = vcp;
295 smb_vc_unlock(vcp);
296 sdp->sd_level = SMBL_VC;
297 }
298 if (ssp) {
299 sdp->sd_share = ssp;
300 smb_share_unlock(ssp);
301 sdp->sd_level = SMBL_SHARE;
302 }
303 break;
304 case SMBIOC_READ: case SMBIOC_WRITE: {
305 struct smbioc_rw *rwrq = (struct smbioc_rw*)data;
306 struct uio auio;
307 struct iovec iov;
308
309 if ((ssp = sdp->sd_share) == NULL) {
310 error = ENOTCONN;
311 goto out;
312 }
313 iov.iov_base = rwrq->ioc_base;
314 iov.iov_len = rwrq->ioc_cnt;
315 auio.uio_iov = &iov;
316 auio.uio_iovcnt = 1;
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;
321 auio.uio_td = td;
322 if (cmd == SMBIOC_READ)
323 error = smb_read(ssp, rwrq->ioc_fh, &auio, scred);
324 else
325 error = smb_write(ssp, rwrq->ioc_fh, &auio, scred);
326 rwrq->ioc_cnt -= auio.uio_resid;
327 break;
328 }
329 default:
330 error = ENODEV;
331 }
332 out:
333 free(scred, M_NSMBDEV);
334 SMB_UNLOCK();
335 return error;
336 }
337
338 static int
nsmb_dev_load(module_t mod,int cmd,void * arg)339 nsmb_dev_load(module_t mod, int cmd, void *arg)
340 {
341 int error = 0;
342
343 switch (cmd) {
344 case MOD_LOAD:
345 error = smb_sm_init();
346 if (error)
347 break;
348 error = smb_iod_init();
349 if (error) {
350 smb_sm_done();
351 break;
352 }
353 error = nsmb_dev_init();
354 if (error)
355 break;
356 sx_init(&smb_lock, "samba device lock");
357 break;
358 case MOD_UNLOAD:
359 smb_iod_done();
360 error = smb_sm_done();
361 if (error)
362 break;
363 nsmb_dev_destroy();
364 sx_destroy(&smb_lock);
365 break;
366 default:
367 error = EINVAL;
368 break;
369 }
370 return error;
371 }
372
373 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0);
374
375 int
smb_dev2share(int fd,int mode,struct smb_cred * scred,struct smb_share ** sspp,struct smb_dev ** ssdp)376 smb_dev2share(int fd, int mode, struct smb_cred *scred,
377 struct smb_share **sspp, struct smb_dev **ssdp)
378 {
379 struct file *fp, *fptmp;
380 struct smb_dev *sdp;
381 struct smb_share *ssp;
382 struct thread *td;
383 int error;
384
385 td = curthread;
386 error = fget(td, fd, &cap_read_rights, &fp);
387 if (error)
388 return (error);
389 fptmp = td->td_fpop;
390 td->td_fpop = fp;
391 error = devfs_get_cdevpriv((void **)&sdp);
392 td->td_fpop = fptmp;
393 fdrop(fp, td);
394 if (error || sdp == NULL)
395 return (error);
396 SMB_LOCK();
397 *ssdp = sdp;
398 ssp = sdp->sd_share;
399 if (ssp == NULL) {
400 SMB_UNLOCK();
401 return (ENOTCONN);
402 }
403 error = smb_share_get(ssp, scred);
404 if (error == 0) {
405 sdp->refcount++;
406 *sspp = ssp;
407 }
408 SMB_UNLOCK();
409 return error;
410 }
411