1 /*- 2 * Copyright (c) 2000-2001 Boris Popov 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/module.h> 33 #include <sys/systm.h> 34 #include <sys/conf.h> 35 #include <sys/fcntl.h> 36 #include <sys/ioccom.h> 37 #include <sys/lock.h> 38 #include <sys/malloc.h> 39 #include <sys/file.h> /* Must come after sys/malloc.h */ 40 #include <sys/filedesc.h> 41 #include <sys/mbuf.h> 42 #include <sys/poll.h> 43 #include <sys/proc.h> 44 #include <sys/select.h> 45 #include <sys/socket.h> 46 #include <sys/socketvar.h> 47 #include <sys/sysctl.h> 48 #include <sys/uio.h> 49 #include <sys/vnode.h> 50 51 #include <net/if.h> 52 53 #include <netsmb/smb.h> 54 #include <netsmb/smb_conn.h> 55 #include <netsmb/smb_subr.h> 56 #include <netsmb/smb_dev.h> 57 58 #define SMB_GETDEV(dev) ((struct smb_dev*)(dev)->si_drv1) 59 #define SMB_CHECKMINOR(dev) do { \ 60 sdp = SMB_GETDEV(dev); \ 61 if (sdp == NULL) return ENXIO; \ 62 } while(0) 63 64 static d_open_t nsmb_dev_open; 65 static d_close_t nsmb_dev_close; 66 static d_ioctl_t nsmb_dev_ioctl; 67 68 MODULE_DEPEND(netsmb, libiconv, 1, 1, 2); 69 MODULE_VERSION(netsmb, NSMB_VERSION); 70 71 static int smb_version = NSMB_VERSION; 72 73 74 SYSCTL_DECL(_net_smb); 75 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, ""); 76 77 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device"); 78 79 80 /* 81 int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio); 82 */ 83 84 static struct cdevsw nsmb_cdevsw = { 85 .d_version = D_VERSION, 86 .d_flags = D_NEEDGIANT | D_NEEDMINOR, 87 .d_open = nsmb_dev_open, 88 .d_close = nsmb_dev_close, 89 .d_ioctl = nsmb_dev_ioctl, 90 .d_name = NSMB_NAME 91 }; 92 93 static eventhandler_tag nsmb_dev_tag; 94 static struct clonedevs *nsmb_clones; 95 96 static void 97 nsmb_dev_clone(void *arg, struct ucred *cred, char *name, int namelen, 98 struct cdev **dev) 99 { 100 int i, u; 101 102 if (*dev != NULL) 103 return; 104 105 if (strcmp(name, NSMB_NAME) == 0) 106 u = -1; 107 else if (dev_stdclone(name, NULL, NSMB_NAME, &u) != 1) 108 return; 109 i = clone_create(&nsmb_clones, &nsmb_cdevsw, &u, dev, 0); 110 if (i) 111 *dev = make_dev_credf(MAKEDEV_REF, &nsmb_cdevsw, u, cred, 112 UID_ROOT, GID_WHEEL, 0600, "%s%d", NSMB_NAME, u); 113 } 114 115 static int 116 nsmb_dev_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 117 { 118 struct smb_dev *sdp; 119 struct ucred *cred = td->td_ucred; 120 int s; 121 122 sdp = SMB_GETDEV(dev); 123 if (sdp && (sdp->sd_flags & NSMBFL_OPEN)) 124 return EBUSY; 125 if (sdp == NULL) { 126 sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK); 127 dev->si_drv1 = (void*)sdp; 128 } 129 /* 130 * XXX: this is just crazy - make a device for an already passed device... 131 * someone should take care of it. 132 */ 133 if ((dev->si_flags & SI_NAMED) == 0) 134 make_dev(&nsmb_cdevsw, dev2unit(dev), cred->cr_uid, 135 cred->cr_gid, 0700, NSMB_NAME"%d", dev2unit(dev)); 136 bzero(sdp, sizeof(*sdp)); 137 /* 138 STAILQ_INIT(&sdp->sd_rqlist); 139 STAILQ_INIT(&sdp->sd_rplist); 140 bzero(&sdp->sd_pollinfo, sizeof(struct selinfo)); 141 */ 142 s = splimp(); 143 sdp->sd_level = -1; 144 sdp->sd_flags |= NSMBFL_OPEN; 145 splx(s); 146 return 0; 147 } 148 149 static int 150 nsmb_dev_close(struct cdev *dev, int flag, int fmt, struct thread *td) 151 { 152 struct smb_dev *sdp; 153 struct smb_vc *vcp; 154 struct smb_share *ssp; 155 struct smb_cred *scred; 156 int s; 157 158 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK); 159 SMB_CHECKMINOR(dev); 160 s = splimp(); 161 if ((sdp->sd_flags & NSMBFL_OPEN) == 0) { 162 splx(s); 163 free(scred, M_NSMBDEV); 164 return EBADF; 165 } 166 smb_makescred(scred, td, NULL); 167 ssp = sdp->sd_share; 168 if (ssp != NULL) 169 smb_share_rele(ssp, scred); 170 vcp = sdp->sd_vc; 171 if (vcp != NULL) 172 smb_vc_rele(vcp, scred); 173 /* 174 smb_flushq(&sdp->sd_rqlist); 175 smb_flushq(&sdp->sd_rplist); 176 */ 177 dev->si_drv1 = NULL; 178 free(sdp, M_NSMBDEV); 179 destroy_dev_sched(dev); 180 splx(s); 181 free(scred, M_NSMBDEV); 182 return 0; 183 } 184 185 186 static int 187 nsmb_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 188 { 189 struct smb_dev *sdp; 190 struct smb_vc *vcp; 191 struct smb_share *ssp; 192 struct smb_cred *scred; 193 int error = 0; 194 195 SMB_CHECKMINOR(dev); 196 if ((sdp->sd_flags & NSMBFL_OPEN) == 0) 197 return EBADF; 198 199 scred = malloc(sizeof(struct smb_cred), M_NSMBDEV, M_WAITOK); 200 smb_makescred(scred, td, NULL); 201 switch (cmd) { 202 case SMBIOC_OPENSESSION: 203 if (sdp->sd_vc) { 204 error = EISCONN; 205 goto out; 206 } 207 error = smb_usr_opensession((struct smbioc_ossn*)data, 208 scred, &vcp); 209 if (error) 210 break; 211 sdp->sd_vc = vcp; 212 smb_vc_unlock(vcp, 0); 213 sdp->sd_level = SMBL_VC; 214 break; 215 case SMBIOC_OPENSHARE: 216 if (sdp->sd_share) { 217 error = EISCONN; 218 goto out; 219 } 220 if (sdp->sd_vc == NULL) { 221 error = ENOTCONN; 222 goto out; 223 } 224 error = smb_usr_openshare(sdp->sd_vc, 225 (struct smbioc_oshare*)data, scred, &ssp); 226 if (error) 227 break; 228 sdp->sd_share = ssp; 229 smb_share_unlock(ssp, 0); 230 sdp->sd_level = SMBL_SHARE; 231 break; 232 case SMBIOC_REQUEST: 233 if (sdp->sd_share == NULL) { 234 error = ENOTCONN; 235 goto out; 236 } 237 error = smb_usr_simplerequest(sdp->sd_share, 238 (struct smbioc_rq*)data, scred); 239 break; 240 case SMBIOC_T2RQ: 241 if (sdp->sd_share == NULL) { 242 error = ENOTCONN; 243 goto out; 244 } 245 error = smb_usr_t2request(sdp->sd_share, 246 (struct smbioc_t2rq*)data, scred); 247 break; 248 case SMBIOC_SETFLAGS: { 249 struct smbioc_flags *fl = (struct smbioc_flags*)data; 250 int on; 251 252 if (fl->ioc_level == SMBL_VC) { 253 if (fl->ioc_mask & SMBV_PERMANENT) { 254 on = fl->ioc_flags & SMBV_PERMANENT; 255 if ((vcp = sdp->sd_vc) == NULL) { 256 error = ENOTCONN; 257 goto out; 258 } 259 error = smb_vc_get(vcp, LK_EXCLUSIVE, scred); 260 if (error) 261 break; 262 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) { 263 vcp->obj.co_flags |= SMBV_PERMANENT; 264 smb_vc_ref(vcp); 265 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) { 266 vcp->obj.co_flags &= ~SMBV_PERMANENT; 267 smb_vc_rele(vcp, scred); 268 } 269 smb_vc_put(vcp, scred); 270 } else 271 error = EINVAL; 272 } else if (fl->ioc_level == SMBL_SHARE) { 273 if (fl->ioc_mask & SMBS_PERMANENT) { 274 on = fl->ioc_flags & SMBS_PERMANENT; 275 if ((ssp = sdp->sd_share) == NULL) { 276 error = ENOTCONN; 277 goto out; 278 } 279 error = smb_share_get(ssp, LK_EXCLUSIVE, scred); 280 if (error) 281 break; 282 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) { 283 ssp->obj.co_flags |= SMBS_PERMANENT; 284 smb_share_ref(ssp); 285 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) { 286 ssp->obj.co_flags &= ~SMBS_PERMANENT; 287 smb_share_rele(ssp, scred); 288 } 289 smb_share_put(ssp, scred); 290 } else 291 error = EINVAL; 292 break; 293 } else 294 error = EINVAL; 295 break; 296 } 297 case SMBIOC_LOOKUP: 298 if (sdp->sd_vc || sdp->sd_share) { 299 error = EISCONN; 300 goto out; 301 } 302 vcp = NULL; 303 ssp = NULL; 304 error = smb_usr_lookup((struct smbioc_lookup*)data, scred, &vcp, &ssp); 305 if (error) 306 break; 307 if (vcp) { 308 sdp->sd_vc = vcp; 309 smb_vc_unlock(vcp, 0); 310 sdp->sd_level = SMBL_VC; 311 } 312 if (ssp) { 313 sdp->sd_share = ssp; 314 smb_share_unlock(ssp, 0); 315 sdp->sd_level = SMBL_SHARE; 316 } 317 break; 318 case SMBIOC_READ: case SMBIOC_WRITE: { 319 struct smbioc_rw *rwrq = (struct smbioc_rw*)data; 320 struct uio auio; 321 struct iovec iov; 322 323 if ((ssp = sdp->sd_share) == NULL) { 324 error = ENOTCONN; 325 goto out; 326 } 327 iov.iov_base = rwrq->ioc_base; 328 iov.iov_len = rwrq->ioc_cnt; 329 auio.uio_iov = &iov; 330 auio.uio_iovcnt = 1; 331 auio.uio_offset = rwrq->ioc_offset; 332 auio.uio_resid = rwrq->ioc_cnt; 333 auio.uio_segflg = UIO_USERSPACE; 334 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE; 335 auio.uio_td = td; 336 if (cmd == SMBIOC_READ) 337 error = smb_read(ssp, rwrq->ioc_fh, &auio, scred); 338 else 339 error = smb_write(ssp, rwrq->ioc_fh, &auio, scred); 340 rwrq->ioc_cnt -= auio.uio_resid; 341 break; 342 } 343 default: 344 error = ENODEV; 345 } 346 out: 347 free(scred, M_NSMBDEV); 348 return error; 349 } 350 351 static int 352 nsmb_dev_load(module_t mod, int cmd, void *arg) 353 { 354 int error = 0; 355 356 switch (cmd) { 357 case MOD_LOAD: 358 error = smb_sm_init(); 359 if (error) 360 break; 361 error = smb_iod_init(); 362 if (error) { 363 smb_sm_done(); 364 break; 365 } 366 clone_setup(&nsmb_clones); 367 nsmb_dev_tag = EVENTHANDLER_REGISTER(dev_clone, nsmb_dev_clone, 0, 1000); 368 break; 369 case MOD_UNLOAD: 370 smb_iod_done(); 371 error = smb_sm_done(); 372 if (error) 373 break; 374 EVENTHANDLER_DEREGISTER(dev_clone, nsmb_dev_tag); 375 drain_dev_clone_events(); 376 clone_cleanup(&nsmb_clones); 377 destroy_dev_drain(&nsmb_cdevsw); 378 break; 379 default: 380 error = EINVAL; 381 break; 382 } 383 return error; 384 } 385 386 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0); 387 388 /* 389 * Convert a file descriptor to appropriate smb_share pointer 390 */ 391 static struct file* 392 nsmb_getfp(struct filedesc* fdp, int fd, int flag) 393 { 394 struct file* fp; 395 396 FILEDESC_SLOCK(fdp); 397 if ((fp = fget_locked(fdp, fd)) == NULL || (fp->f_flag & flag) == 0) { 398 FILEDESC_SUNLOCK(fdp); 399 return (NULL); 400 } 401 fhold(fp); 402 FILEDESC_SUNLOCK(fdp); 403 return (fp); 404 } 405 406 int 407 smb_dev2share(int fd, int mode, struct smb_cred *scred, 408 struct smb_share **sspp) 409 { 410 struct file *fp; 411 struct vnode *vp; 412 struct smb_dev *sdp; 413 struct smb_share *ssp; 414 struct cdev *dev; 415 int error; 416 417 fp = nsmb_getfp(scred->scr_td->td_proc->p_fd, fd, FREAD | FWRITE); 418 if (fp == NULL) 419 return EBADF; 420 vp = fp->f_vnode; 421 if (vp == NULL) { 422 fdrop(fp, curthread); 423 return EBADF; 424 } 425 if (vp->v_type != VCHR) { 426 fdrop(fp, curthread); 427 return EBADF; 428 } 429 dev = vp->v_rdev; 430 SMB_CHECKMINOR(dev); 431 ssp = sdp->sd_share; 432 if (ssp == NULL) { 433 fdrop(fp, curthread); 434 return ENOTCONN; 435 } 436 error = smb_share_get(ssp, LK_EXCLUSIVE, scred); 437 if (error == 0) 438 *sspp = ssp; 439 fdrop(fp, curthread); 440 return error; 441 } 442 443