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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Boris Popov. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 #include <sys/param.h> 35 #include <sys/kernel.h> 36 #include <sys/systm.h> 37 #include <sys/conf.h> 38 #include <sys/fcntl.h> 39 #include <sys/ioccom.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/file.h> /* Must come after sys/malloc.h */ 43 #include <sys/mbuf.h> 44 #include <sys/poll.h> 45 #include <sys/proc.h> 46 #include <sys/select.h> 47 #include <sys/socket.h> 48 #include <sys/socketvar.h> 49 #include <sys/sysctl.h> 50 #include <sys/uio.h> 51 #include <sys/vnode.h> 52 53 #include <net/if.h> 54 55 #include <netsmb/smb.h> 56 #include <netsmb/smb_conn.h> 57 #include <netsmb/smb_subr.h> 58 #include <netsmb/smb_dev.h> 59 60 #define SMB_GETDEV(dev) ((struct smb_dev*)(dev)->si_drv1) 61 #define SMB_CHECKMINOR(dev) do { \ 62 sdp = SMB_GETDEV(dev); \ 63 if (sdp == NULL) return ENXIO; \ 64 } while(0) 65 66 static d_open_t nsmb_dev_open; 67 static d_close_t nsmb_dev_close; 68 static d_read_t nsmb_dev_read; 69 static d_write_t nsmb_dev_write; 70 static d_ioctl_t nsmb_dev_ioctl; 71 static d_poll_t nsmb_dev_poll; 72 73 MODULE_DEPEND(netsmb, libiconv, 1, 1, 1); 74 MODULE_VERSION(netsmb, NSMB_VERSION); 75 76 static int smb_version = NSMB_VERSION; 77 78 79 SYSCTL_DECL(_net_smb); 80 SYSCTL_INT(_net_smb, OID_AUTO, version, CTLFLAG_RD, &smb_version, 0, ""); 81 82 static MALLOC_DEFINE(M_NSMBDEV, "NETSMBDEV", "NET/SMB device"); 83 84 85 /* 86 int smb_dev_queue(struct smb_dev *ndp, struct smb_rq *rqp, int prio); 87 */ 88 89 static struct cdevsw nsmb_cdevsw = { 90 /* open */ nsmb_dev_open, 91 /* close */ nsmb_dev_close, 92 /* read */ nsmb_dev_read, 93 /* write */ nsmb_dev_write, 94 /* ioctl */ nsmb_dev_ioctl, 95 /* poll */ nsmb_dev_poll, 96 /* mmap */ nommap, 97 /* strategy */ nostrategy, 98 /* name */ NSMB_NAME, 99 /* maj */ NSMB_MAJOR, 100 /* dump */ nodump, 101 /* psize */ nopsize, 102 /* flags */ 0, 103 #ifndef FB_CURRENT 104 /* bmaj */ -1 105 #endif 106 }; 107 108 static eventhandler_tag nsmb_dev_tag; 109 110 static void 111 nsmb_dev_clone(void *arg, char *name, int namelen, dev_t *dev) 112 { 113 int min; 114 115 if (*dev != NODEV) 116 return; 117 if (dev_stdclone(name, NULL, NSMB_NAME, &min) != 1) 118 return; 119 *dev = make_dev(&nsmb_cdevsw, min, 0, 0, 0600, NSMB_NAME"%d", min); 120 } 121 122 static int 123 nsmb_dev_open(dev_t dev, int oflags, int devtype, struct proc *p) 124 { 125 struct smb_dev *sdp; 126 struct ucred *cred = p->p_ucred; 127 int s; 128 129 sdp = SMB_GETDEV(dev); 130 if (sdp && (sdp->sd_flags & NSMBFL_OPEN)) 131 return EBUSY; 132 if (sdp == NULL) { 133 sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK); 134 dev->si_drv1 = (void*)sdp; 135 } 136 /* 137 * XXX: this is just crazy - make a device for an already passed device... 138 * someone should take care of it. 139 */ 140 if ((dev->si_flags & SI_NAMED) == 0) 141 make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid, 0700, 142 NSMB_NAME"%d", dev2unit(dev)); 143 bzero(sdp, sizeof(*sdp)); 144 /* 145 STAILQ_INIT(&sdp->sd_rqlist); 146 STAILQ_INIT(&sdp->sd_rplist); 147 bzero(&sdp->sd_pollinfo, sizeof(struct selinfo)); 148 */ 149 s = splimp(); 150 sdp->sd_level = -1; 151 sdp->sd_flags |= NSMBFL_OPEN; 152 splx(s); 153 return 0; 154 } 155 156 static int 157 nsmb_dev_close(dev_t dev, int flag, int fmt, struct proc *p) 158 { 159 struct smb_dev *sdp; 160 struct smb_vc *vcp; 161 struct smb_share *ssp; 162 struct smb_cred scred; 163 int s; 164 165 SMB_CHECKMINOR(dev); 166 s = splimp(); 167 if ((sdp->sd_flags & NSMBFL_OPEN) == 0) { 168 splx(s); 169 return EBADF; 170 } 171 smb_makescred(&scred, p, NULL); 172 ssp = sdp->sd_share; 173 if (ssp != NULL) 174 smb_share_rele(ssp, &scred); 175 vcp = sdp->sd_vc; 176 if (vcp != NULL) 177 smb_vc_rele(vcp, &scred); 178 /* 179 smb_flushq(&sdp->sd_rqlist); 180 smb_flushq(&sdp->sd_rplist); 181 */ 182 #if __FreeBSD_version > 400001 183 dev->si_drv1 = NULL; 184 free(sdp, M_NSMBDEV); 185 destroy_dev(dev); 186 #else 187 sdp->sd_flags &= ~NSMBFL_OPEN; 188 #endif 189 splx(s); 190 return 0; 191 } 192 193 194 static int 195 nsmb_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 196 { 197 struct smb_dev *sdp; 198 struct smb_vc *vcp; 199 struct smb_share *ssp; 200 struct smb_cred scred; 201 int error = 0; 202 203 SMB_CHECKMINOR(dev); 204 if ((sdp->sd_flags & NSMBFL_OPEN) == 0) 205 return EBADF; 206 207 smb_makescred(&scred, p, NULL); 208 switch (cmd) { 209 case SMBIOC_OPENSESSION: 210 if (sdp->sd_vc) 211 return EISCONN; 212 error = smb_usr_opensession((struct smbioc_ossn*)data, 213 &scred, &vcp); 214 if (error) 215 break; 216 sdp->sd_vc = vcp; 217 smb_vc_unlock(vcp, 0, p); 218 sdp->sd_level = SMBL_VC; 219 break; 220 case SMBIOC_OPENSHARE: 221 if (sdp->sd_share) 222 return EISCONN; 223 if (sdp->sd_vc == NULL) 224 return ENOTCONN; 225 error = smb_usr_openshare(sdp->sd_vc, 226 (struct smbioc_oshare*)data, &scred, &ssp); 227 if (error) 228 break; 229 sdp->sd_share = ssp; 230 smb_share_unlock(ssp, 0, p); 231 sdp->sd_level = SMBL_SHARE; 232 break; 233 case SMBIOC_REQUEST: 234 if (sdp->sd_share == NULL) 235 return ENOTCONN; 236 error = smb_usr_simplerequest(sdp->sd_share, 237 (struct smbioc_rq*)data, &scred); 238 break; 239 case SMBIOC_T2RQ: 240 if (sdp->sd_share == NULL) 241 return ENOTCONN; 242 error = smb_usr_t2request(sdp->sd_share, 243 (struct smbioc_t2rq*)data, &scred); 244 break; 245 case SMBIOC_SETFLAGS: { 246 struct smbioc_flags *fl = (struct smbioc_flags*)data; 247 int on; 248 249 if (fl->ioc_level == SMBL_VC) { 250 if (fl->ioc_mask & SMBV_PERMANENT) { 251 on = fl->ioc_flags & SMBV_PERMANENT; 252 if ((vcp = sdp->sd_vc) == NULL) 253 return ENOTCONN; 254 error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred); 255 if (error) 256 break; 257 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) { 258 vcp->obj.co_flags |= SMBV_PERMANENT; 259 smb_vc_ref(vcp, p); 260 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) { 261 vcp->obj.co_flags &= ~SMBV_PERMANENT; 262 smb_vc_rele(vcp, &scred); 263 } 264 smb_vc_put(vcp, &scred); 265 } else 266 error = EINVAL; 267 } else if (fl->ioc_level == SMBL_SHARE) { 268 if (fl->ioc_mask & SMBS_PERMANENT) { 269 on = fl->ioc_flags & SMBS_PERMANENT; 270 if ((ssp = sdp->sd_share) == NULL) 271 return ENOTCONN; 272 error = smb_share_get(ssp, LK_EXCLUSIVE, &scred); 273 if (error) 274 break; 275 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) { 276 ssp->obj.co_flags |= SMBS_PERMANENT; 277 smb_share_ref(ssp, p); 278 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) { 279 ssp->obj.co_flags &= ~SMBS_PERMANENT; 280 smb_share_rele(ssp, &scred); 281 } 282 smb_share_put(ssp, &scred); 283 } else 284 error = EINVAL; 285 break; 286 } else 287 error = EINVAL; 288 break; 289 } 290 case SMBIOC_LOOKUP: 291 if (sdp->sd_vc || sdp->sd_share) 292 return EISCONN; 293 vcp = NULL; 294 ssp = NULL; 295 error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp); 296 if (error) 297 break; 298 if (vcp) { 299 sdp->sd_vc = vcp; 300 smb_vc_unlock(vcp, 0, p); 301 sdp->sd_level = SMBL_VC; 302 } 303 if (ssp) { 304 sdp->sd_share = ssp; 305 smb_share_unlock(ssp, 0, p); 306 sdp->sd_level = SMBL_SHARE; 307 } 308 break; 309 case SMBIOC_READ: case SMBIOC_WRITE: { 310 struct smbioc_rw *rwrq = (struct smbioc_rw*)data; 311 struct uio auio; 312 struct iovec iov; 313 314 if ((ssp = sdp->sd_share) == NULL) 315 return ENOTCONN; 316 iov.iov_base = rwrq->ioc_base; 317 iov.iov_len = rwrq->ioc_cnt; 318 auio.uio_iov = &iov; 319 auio.uio_iovcnt = 1; 320 auio.uio_offset = rwrq->ioc_offset; 321 auio.uio_resid = rwrq->ioc_cnt; 322 auio.uio_segflg = UIO_USERSPACE; 323 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE; 324 auio.uio_procp = p; 325 if (cmd == SMBIOC_READ) 326 error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred); 327 else 328 error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred); 329 rwrq->ioc_cnt -= auio.uio_resid; 330 break; 331 } 332 default: 333 error = ENODEV; 334 } 335 return error; 336 } 337 338 static int 339 nsmb_dev_read(dev_t dev, struct uio *uio, int flag) 340 { 341 return EACCES; 342 } 343 344 static int 345 nsmb_dev_write(dev_t dev, struct uio *uio, int flag) 346 { 347 return EACCES; 348 } 349 350 static int 351 nsmb_dev_poll(dev_t dev, int events, struct proc *p) 352 { 353 return ENODEV; 354 } 355 356 static int 357 nsmb_dev_load(module_t mod, int cmd, void *arg) 358 { 359 int error = 0; 360 361 switch (cmd) { 362 case MOD_LOAD: 363 error = smb_sm_init(); 364 if (error) 365 break; 366 error = smb_iod_init(); 367 if (error) { 368 smb_sm_done(); 369 break; 370 } 371 #if __FreeBSD_version > 400001 372 cdevsw_add(&nsmb_cdevsw); 373 #endif 374 #if __FreeBSD_version > 500000 375 nsmb_dev_tag = EVENTHANDLER_REGISTER(dev_clone, nsmb_dev_clone, 0, 1000); 376 #endif 377 printf("netsmb_dev: loaded\n"); 378 break; 379 case MOD_UNLOAD: 380 smb_iod_done(); 381 error = smb_sm_done(); 382 error = 0; 383 #if __FreeBSD_version > 500000 384 EVENTHANDLER_DEREGISTER(dev_clone, nsmb_dev_tag); 385 #endif 386 #if __FreeBSD_version > 400001 387 cdevsw_remove(&nsmb_cdevsw); 388 #endif 389 printf("netsmb_dev: unloaded\n"); 390 break; 391 default: 392 error = EINVAL; 393 break; 394 } 395 return error; 396 } 397 398 #if __FreeBSD_version > 400000 399 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0); 400 #else 401 CDEV_MODULE(dev_netsmb, NSMB_MAJOR, nsmb_cdevsw, nsmb_dev_load, 0); 402 #endif 403 404 405 /* 406 * Convert a file descriptor to appropriate smb_share pointer 407 */ 408 static struct file* 409 nsmb_getfp(struct filedesc* fdp, int fd, int flag) 410 { 411 struct file* fp; 412 413 if (((u_int)fd) >= fdp->fd_nfiles || 414 (fp = fdp->fd_ofiles[fd]) == NULL || 415 (fp->f_flag & flag) == 0) 416 return (NULL); 417 return (fp); 418 } 419 420 int 421 smb_dev2share(int fd, int mode, struct smb_cred *scred, 422 struct smb_share **sspp) 423 { 424 struct file *fp; 425 struct vnode *vp; 426 struct smb_dev *sdp; 427 struct smb_share *ssp; 428 dev_t dev; 429 int error; 430 431 if ((fp = nsmb_getfp(scred->scr_p->p_fd, fd, FREAD | FWRITE)) == NULL) 432 return EBADF; 433 vp = (struct vnode*)fp->f_data; 434 if (vp == NULL) 435 return EBADF; 436 dev = vn_todev(vp); 437 if (dev == NODEV) 438 return EBADF; 439 SMB_CHECKMINOR(dev); 440 ssp = sdp->sd_share; 441 if (ssp == NULL) 442 return ENOTCONN; 443 error = smb_share_get(ssp, LK_EXCLUSIVE, scred); 444 if (error) 445 return error; 446 *sspp = ssp; 447 return 0; 448 } 449 450