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: src/sys/netsmb/smb_dev.c,v 1.2.2.1 2001/05/22 08:32:33 bp Exp $ 33 * $DragonFly: src/sys/netproto/smb/smb_dev.c,v 1.2 2003/06/17 04:28:54 dillon Exp $ 34 */ 35 #include <sys/param.h> 36 #include <sys/kernel.h> 37 #include <sys/systm.h> 38 #include <sys/conf.h> 39 #include <sys/fcntl.h> 40 #include <sys/ioccom.h> 41 #include <sys/malloc.h> 42 #include <sys/file.h> /* Must come after sys/malloc.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 #define SMB_GETDEV(dev) ((struct smb_dev*)(dev)->si_drv1) 60 #define SMB_CHECKMINOR(dev) do { \ 61 sdp = SMB_GETDEV(dev); \ 62 if (sdp == NULL) return ENXIO; \ 63 } while(0) 64 65 static d_open_t nsmb_dev_open; 66 static d_close_t nsmb_dev_close; 67 static d_read_t nsmb_dev_read; 68 static d_write_t nsmb_dev_write; 69 static d_ioctl_t nsmb_dev_ioctl; 70 static d_poll_t nsmb_dev_poll; 71 72 MODULE_VERSION(netsmb, NSMB_VERSION); 73 74 #define SI_NAMED 0 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 /* bmaj */ -1 104 }; 105 106 107 static int 108 nsmb_dev_open(dev_t dev, int oflags, int devtype, struct proc *p) 109 { 110 struct smb_dev *sdp; 111 struct ucred *cred = p->p_ucred; 112 int s; 113 114 sdp = SMB_GETDEV(dev); 115 if (sdp && (sdp->sd_flags & NSMBFL_OPEN)) 116 return EBUSY; 117 if (sdp == NULL) { 118 sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK); 119 dev->si_drv1 = (void*)sdp; 120 } 121 /* 122 * XXX: this is just crazy - make a device for an already passed device... 123 * someone should take care of it. 124 */ 125 if ((dev->si_flags & SI_NAMED) == 0) 126 make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid, 0700, 127 NSMB_NAME"%d", lminor(dev)); 128 bzero(sdp, sizeof(*sdp)); 129 /* 130 STAILQ_INIT(&sdp->sd_rqlist); 131 STAILQ_INIT(&sdp->sd_rplist); 132 bzero(&sdp->sd_pollinfo, sizeof(struct selinfo)); 133 */ 134 s = splimp(); 135 sdp->sd_level = -1; 136 sdp->sd_flags |= NSMBFL_OPEN; 137 splx(s); 138 return 0; 139 } 140 141 static int 142 nsmb_dev_close(dev_t dev, int flag, int fmt, struct proc *p) 143 { 144 struct smb_dev *sdp; 145 struct smb_vc *vcp; 146 struct smb_share *ssp; 147 struct smb_cred scred; 148 int s; 149 150 SMB_CHECKMINOR(dev); 151 s = splimp(); 152 if ((sdp->sd_flags & NSMBFL_OPEN) == 0) { 153 splx(s); 154 return EBADF; 155 } 156 smb_makescred(&scred, p, NULL); 157 ssp = sdp->sd_share; 158 if (ssp != NULL) 159 smb_share_rele(ssp, &scred); 160 vcp = sdp->sd_vc; 161 if (vcp != NULL) 162 smb_vc_rele(vcp, &scred); 163 /* 164 smb_flushq(&sdp->sd_rqlist); 165 smb_flushq(&sdp->sd_rplist); 166 */ 167 dev->si_drv1 = NULL; 168 free(sdp, M_NSMBDEV); 169 destroy_dev(dev); 170 splx(s); 171 return 0; 172 } 173 174 175 static int 176 nsmb_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 177 { 178 struct smb_dev *sdp; 179 struct smb_vc *vcp; 180 struct smb_share *ssp; 181 struct smb_cred scred; 182 int error = 0; 183 184 SMB_CHECKMINOR(dev); 185 if ((sdp->sd_flags & NSMBFL_OPEN) == 0) 186 return EBADF; 187 188 smb_makescred(&scred, p, NULL); 189 switch (cmd) { 190 case SMBIOC_OPENSESSION: 191 if (sdp->sd_vc) 192 return EISCONN; 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, 0, p); 199 sdp->sd_level = SMBL_VC; 200 break; 201 case SMBIOC_OPENSHARE: 202 if (sdp->sd_share) 203 return EISCONN; 204 if (sdp->sd_vc == NULL) 205 return ENOTCONN; 206 error = smb_usr_openshare(sdp->sd_vc, 207 (struct smbioc_oshare*)data, &scred, &ssp); 208 if (error) 209 break; 210 sdp->sd_share = ssp; 211 smb_share_unlock(ssp, 0, p); 212 sdp->sd_level = SMBL_SHARE; 213 break; 214 case SMBIOC_REQUEST: 215 if (sdp->sd_share == NULL) 216 return ENOTCONN; 217 error = smb_usr_simplerequest(sdp->sd_share, 218 (struct smbioc_rq*)data, &scred); 219 break; 220 case SMBIOC_T2RQ: 221 if (sdp->sd_share == NULL) 222 return ENOTCONN; 223 error = smb_usr_t2request(sdp->sd_share, 224 (struct smbioc_t2rq*)data, &scred); 225 break; 226 case SMBIOC_SETFLAGS: { 227 struct smbioc_flags *fl = (struct smbioc_flags*)data; 228 int on; 229 230 if (fl->ioc_level == SMBL_VC) { 231 if (fl->ioc_mask & SMBV_PERMANENT) { 232 on = fl->ioc_flags & SMBV_PERMANENT; 233 if ((vcp = sdp->sd_vc) == NULL) 234 return ENOTCONN; 235 error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred); 236 if (error) 237 break; 238 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) { 239 vcp->obj.co_flags |= SMBV_PERMANENT; 240 smb_vc_ref(vcp, p); 241 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) { 242 vcp->obj.co_flags &= ~SMBV_PERMANENT; 243 smb_vc_rele(vcp, &scred); 244 } 245 smb_vc_put(vcp, &scred); 246 } else 247 error = EINVAL; 248 } else if (fl->ioc_level == SMBL_SHARE) { 249 if (fl->ioc_mask & SMBS_PERMANENT) { 250 on = fl->ioc_flags & SMBS_PERMANENT; 251 if ((ssp = sdp->sd_share) == NULL) 252 return ENOTCONN; 253 error = smb_share_get(ssp, LK_EXCLUSIVE, &scred); 254 if (error) 255 break; 256 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) { 257 ssp->obj.co_flags |= SMBS_PERMANENT; 258 smb_share_ref(ssp, p); 259 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) { 260 ssp->obj.co_flags &= ~SMBS_PERMANENT; 261 smb_share_rele(ssp, &scred); 262 } 263 smb_share_put(ssp, &scred); 264 } else 265 error = EINVAL; 266 break; 267 } else 268 error = EINVAL; 269 break; 270 } 271 case SMBIOC_LOOKUP: 272 if (sdp->sd_vc || sdp->sd_share) 273 return EISCONN; 274 vcp = NULL; 275 ssp = NULL; 276 error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp); 277 if (error) 278 break; 279 if (vcp) { 280 sdp->sd_vc = vcp; 281 smb_vc_unlock(vcp, 0, p); 282 sdp->sd_level = SMBL_VC; 283 } 284 if (ssp) { 285 sdp->sd_share = ssp; 286 smb_share_unlock(ssp, 0, p); 287 sdp->sd_level = SMBL_SHARE; 288 } 289 break; 290 case SMBIOC_READ: case SMBIOC_WRITE: { 291 struct smbioc_rw *rwrq = (struct smbioc_rw*)data; 292 struct uio auio; 293 struct iovec iov; 294 295 if ((ssp = sdp->sd_share) == NULL) 296 return ENOTCONN; 297 iov.iov_base = rwrq->ioc_base; 298 iov.iov_len = rwrq->ioc_cnt; 299 auio.uio_iov = &iov; 300 auio.uio_iovcnt = 1; 301 auio.uio_offset = rwrq->ioc_offset; 302 auio.uio_resid = rwrq->ioc_cnt; 303 auio.uio_segflg = UIO_USERSPACE; 304 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE; 305 auio.uio_procp = p; 306 if (cmd == SMBIOC_READ) 307 error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred); 308 else 309 error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred); 310 rwrq->ioc_cnt -= auio.uio_resid; 311 break; 312 } 313 default: 314 error = ENODEV; 315 } 316 return error; 317 } 318 319 static int 320 nsmb_dev_read(dev_t dev, struct uio *uio, int flag) 321 { 322 return EACCES; 323 } 324 325 static int 326 nsmb_dev_write(dev_t dev, struct uio *uio, int flag) 327 { 328 return EACCES; 329 } 330 331 static int 332 nsmb_dev_poll(dev_t dev, int events, struct proc *p) 333 { 334 return ENODEV; 335 } 336 337 static int 338 nsmb_dev_load(module_t mod, int cmd, void *arg) 339 { 340 int error = 0; 341 342 switch (cmd) { 343 case MOD_LOAD: 344 error = smb_checksmp(); 345 if (error) 346 break; 347 error = smb_sm_init(); 348 if (error) 349 break; 350 error = smb_iod_init(); 351 if (error) { 352 smb_sm_done(); 353 break; 354 } 355 cdevsw_add(&nsmb_cdevsw); 356 printf("netsmb_dev: loaded\n"); 357 break; 358 case MOD_UNLOAD: 359 smb_iod_done(); 360 error = smb_sm_done(); 361 error = 0; 362 cdevsw_remove(&nsmb_cdevsw); 363 printf("netsmb_dev: unloaded\n"); 364 break; 365 default: 366 error = EINVAL; 367 break; 368 } 369 return error; 370 } 371 372 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0); 373 374 375 /* 376 * Convert a file descriptor to appropriate smb_share pointer 377 */ 378 static struct file* 379 nsmb_getfp(struct filedesc* fdp, int fd, int flag) 380 { 381 struct file* fp; 382 383 if (((u_int)fd) >= fdp->fd_nfiles || 384 (fp = fdp->fd_ofiles[fd]) == NULL || 385 (fp->f_flag & flag) == 0) 386 return (NULL); 387 return (fp); 388 } 389 390 int 391 smb_dev2share(int fd, int mode, struct smb_cred *scred, 392 struct smb_share **sspp) 393 { 394 struct file *fp; 395 struct vnode *vp; 396 struct smb_dev *sdp; 397 struct smb_share *ssp; 398 dev_t dev; 399 int error; 400 401 if ((fp = nsmb_getfp(scred->scr_p->p_fd, fd, FREAD | FWRITE)) == NULL) 402 return EBADF; 403 vp = (struct vnode*)fp->f_data; 404 if (vp == NULL) 405 return EBADF; 406 dev = vn_todev(vp); 407 if (dev == NODEV) 408 return EBADF; 409 SMB_CHECKMINOR(dev); 410 ssp = sdp->sd_share; 411 if (ssp == NULL) 412 return ENOTCONN; 413 error = smb_share_get(ssp, LK_EXCLUSIVE, scred); 414 if (error) 415 return error; 416 *sspp = ssp; 417 return 0; 418 } 419 420