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.6 2003/08/07 21:17:39 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 "smb.h" 55 #include "smb_conn.h" 56 #include "smb_subr.h" 57 #include "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 /* name */ NSMB_NAME, 91 /* maj */ NSMB_MAJOR, 92 /* flags */ 0, 93 /* port */ NULL, 94 /* autoq */ 0, 95 96 /* open */ nsmb_dev_open, 97 /* close */ nsmb_dev_close, 98 /* read */ nsmb_dev_read, 99 /* write */ nsmb_dev_write, 100 /* ioctl */ nsmb_dev_ioctl, 101 /* poll */ nsmb_dev_poll, 102 /* mmap */ nommap, 103 /* strategy */ nostrategy, 104 /* dump */ nodump, 105 /* psize */ nopsize 106 }; 107 108 109 static int 110 nsmb_dev_open(dev_t dev, int oflags, int devtype, d_thread_t *td) 111 { 112 struct proc *p = td->td_proc; 113 struct smb_dev *sdp; 114 struct ucred *cred; 115 int s; 116 117 KKASSERT(p != NULL); 118 cred = p->p_ucred; 119 120 sdp = SMB_GETDEV(dev); 121 if (sdp && (sdp->sd_flags & NSMBFL_OPEN)) 122 return EBUSY; 123 if (sdp == NULL) { 124 sdp = malloc(sizeof(*sdp), M_NSMBDEV, M_WAITOK); 125 dev->si_drv1 = (void*)sdp; 126 } 127 /* 128 * XXX: this is just crazy - make a device for an already passed device... 129 * someone should take care of it. 130 */ 131 if ((dev->si_flags & SI_NAMED) == 0) 132 make_dev(&nsmb_cdevsw, minor(dev), cred->cr_uid, cred->cr_gid, 0700, 133 NSMB_NAME"%d", lminor(dev)); 134 bzero(sdp, sizeof(*sdp)); 135 /* 136 STAILQ_INIT(&sdp->sd_rqlist); 137 STAILQ_INIT(&sdp->sd_rplist); 138 bzero(&sdp->sd_pollinfo, sizeof(struct selinfo)); 139 */ 140 s = splimp(); 141 sdp->sd_level = -1; 142 sdp->sd_flags |= NSMBFL_OPEN; 143 splx(s); 144 return 0; 145 } 146 147 static int 148 nsmb_dev_close(dev_t dev, int flag, int fmt, d_thread_t *td) 149 { 150 struct smb_dev *sdp; 151 struct smb_vc *vcp; 152 struct smb_share *ssp; 153 struct smb_cred scred; 154 int s; 155 156 SMB_CHECKMINOR(dev); 157 s = splimp(); 158 if ((sdp->sd_flags & NSMBFL_OPEN) == 0) { 159 splx(s); 160 return EBADF; 161 } 162 smb_makescred(&scred, td, NULL); 163 ssp = sdp->sd_share; 164 if (ssp != NULL) 165 smb_share_rele(ssp, &scred); 166 vcp = sdp->sd_vc; 167 if (vcp != NULL) 168 smb_vc_rele(vcp, &scred); 169 /* 170 smb_flushq(&sdp->sd_rqlist); 171 smb_flushq(&sdp->sd_rplist); 172 */ 173 dev->si_drv1 = NULL; 174 free(sdp, M_NSMBDEV); 175 destroy_dev(dev); 176 splx(s); 177 return 0; 178 } 179 180 181 static int 182 nsmb_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, d_thread_t *td) 183 { 184 struct smb_dev *sdp; 185 struct smb_vc *vcp; 186 struct smb_share *ssp; 187 struct smb_cred scred; 188 int error = 0; 189 190 SMB_CHECKMINOR(dev); 191 if ((sdp->sd_flags & NSMBFL_OPEN) == 0) 192 return EBADF; 193 194 smb_makescred(&scred, td, NULL); 195 switch (cmd) { 196 case SMBIOC_OPENSESSION: 197 if (sdp->sd_vc) 198 return EISCONN; 199 error = smb_usr_opensession((struct smbioc_ossn*)data, 200 &scred, &vcp); 201 if (error) 202 break; 203 sdp->sd_vc = vcp; 204 smb_vc_unlock(vcp, 0, td); 205 sdp->sd_level = SMBL_VC; 206 break; 207 case SMBIOC_OPENSHARE: 208 if (sdp->sd_share) 209 return EISCONN; 210 if (sdp->sd_vc == NULL) 211 return ENOTCONN; 212 error = smb_usr_openshare(sdp->sd_vc, 213 (struct smbioc_oshare*)data, &scred, &ssp); 214 if (error) 215 break; 216 sdp->sd_share = ssp; 217 smb_share_unlock(ssp, 0, td); 218 sdp->sd_level = SMBL_SHARE; 219 break; 220 case SMBIOC_REQUEST: 221 if (sdp->sd_share == NULL) 222 return ENOTCONN; 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 return ENOTCONN; 229 error = smb_usr_t2request(sdp->sd_share, 230 (struct smbioc_t2rq*)data, &scred); 231 break; 232 case SMBIOC_SETFLAGS: { 233 struct smbioc_flags *fl = (struct smbioc_flags*)data; 234 int on; 235 236 if (fl->ioc_level == SMBL_VC) { 237 if (fl->ioc_mask & SMBV_PERMANENT) { 238 on = fl->ioc_flags & SMBV_PERMANENT; 239 if ((vcp = sdp->sd_vc) == NULL) 240 return ENOTCONN; 241 error = smb_vc_get(vcp, LK_EXCLUSIVE, &scred); 242 if (error) 243 break; 244 if (on && (vcp->obj.co_flags & SMBV_PERMANENT) == 0) { 245 vcp->obj.co_flags |= SMBV_PERMANENT; 246 smb_vc_ref(vcp, td); 247 } else if (!on && (vcp->obj.co_flags & SMBV_PERMANENT)) { 248 vcp->obj.co_flags &= ~SMBV_PERMANENT; 249 smb_vc_rele(vcp, &scred); 250 } 251 smb_vc_put(vcp, &scred); 252 } else 253 error = EINVAL; 254 } else if (fl->ioc_level == SMBL_SHARE) { 255 if (fl->ioc_mask & SMBS_PERMANENT) { 256 on = fl->ioc_flags & SMBS_PERMANENT; 257 if ((ssp = sdp->sd_share) == NULL) 258 return ENOTCONN; 259 error = smb_share_get(ssp, LK_EXCLUSIVE, &scred); 260 if (error) 261 break; 262 if (on && (ssp->obj.co_flags & SMBS_PERMANENT) == 0) { 263 ssp->obj.co_flags |= SMBS_PERMANENT; 264 smb_share_ref(ssp, td); 265 } else if (!on && (ssp->obj.co_flags & SMBS_PERMANENT)) { 266 ssp->obj.co_flags &= ~SMBS_PERMANENT; 267 smb_share_rele(ssp, &scred); 268 } 269 smb_share_put(ssp, &scred); 270 } else 271 error = EINVAL; 272 break; 273 } else 274 error = EINVAL; 275 break; 276 } 277 case SMBIOC_LOOKUP: 278 if (sdp->sd_vc || sdp->sd_share) 279 return EISCONN; 280 vcp = NULL; 281 ssp = NULL; 282 error = smb_usr_lookup((struct smbioc_lookup*)data, &scred, &vcp, &ssp); 283 if (error) 284 break; 285 if (vcp) { 286 sdp->sd_vc = vcp; 287 smb_vc_unlock(vcp, 0, td); 288 sdp->sd_level = SMBL_VC; 289 } 290 if (ssp) { 291 sdp->sd_share = ssp; 292 smb_share_unlock(ssp, 0, td); 293 sdp->sd_level = SMBL_SHARE; 294 } 295 break; 296 case SMBIOC_READ: case SMBIOC_WRITE: { 297 struct smbioc_rw *rwrq = (struct smbioc_rw*)data; 298 struct uio auio; 299 struct iovec iov; 300 301 if ((ssp = sdp->sd_share) == NULL) 302 return ENOTCONN; 303 iov.iov_base = rwrq->ioc_base; 304 iov.iov_len = rwrq->ioc_cnt; 305 auio.uio_iov = &iov; 306 auio.uio_iovcnt = 1; 307 auio.uio_offset = rwrq->ioc_offset; 308 auio.uio_resid = rwrq->ioc_cnt; 309 auio.uio_segflg = UIO_USERSPACE; 310 auio.uio_rw = (cmd == SMBIOC_READ) ? UIO_READ : UIO_WRITE; 311 auio.uio_td = td; 312 if (cmd == SMBIOC_READ) 313 error = smb_read(ssp, rwrq->ioc_fh, &auio, &scred); 314 else 315 error = smb_write(ssp, rwrq->ioc_fh, &auio, &scred); 316 rwrq->ioc_cnt -= auio.uio_resid; 317 break; 318 } 319 default: 320 error = ENODEV; 321 } 322 return error; 323 } 324 325 static int 326 nsmb_dev_read(dev_t dev, struct uio *uio, int flag) 327 { 328 return EACCES; 329 } 330 331 static int 332 nsmb_dev_write(dev_t dev, struct uio *uio, int flag) 333 { 334 return EACCES; 335 } 336 337 static int 338 nsmb_dev_poll(dev_t dev, int events, d_thread_t *td) 339 { 340 return ENODEV; 341 } 342 343 static int 344 nsmb_dev_load(module_t mod, int cmd, void *arg) 345 { 346 int error = 0; 347 348 switch (cmd) { 349 case MOD_LOAD: 350 error = smb_checksmp(); 351 if (error) 352 break; 353 error = smb_sm_init(); 354 if (error) 355 break; 356 error = smb_iod_init(); 357 if (error) { 358 smb_sm_done(); 359 break; 360 } 361 cdevsw_add(&nsmb_cdevsw); 362 printf("netsmb_dev: loaded\n"); 363 break; 364 case MOD_UNLOAD: 365 smb_iod_done(); 366 error = smb_sm_done(); 367 error = 0; 368 cdevsw_remove(&nsmb_cdevsw); 369 printf("netsmb_dev: unloaded\n"); 370 break; 371 default: 372 error = EINVAL; 373 break; 374 } 375 return error; 376 } 377 378 DEV_MODULE (dev_netsmb, nsmb_dev_load, 0); 379 380 381 /* 382 * Convert a file descriptor to appropriate smb_share pointer 383 */ 384 static struct file* 385 nsmb_getfp(struct filedesc* fdp, int fd, int flag) 386 { 387 struct file* fp; 388 389 if (((u_int)fd) >= fdp->fd_nfiles || 390 (fp = fdp->fd_ofiles[fd]) == NULL || 391 (fp->f_flag & flag) == 0) 392 return (NULL); 393 return (fp); 394 } 395 396 int 397 smb_dev2share(int fd, int mode, struct smb_cred *scred, 398 struct smb_share **sspp) 399 { 400 struct file *fp; 401 struct vnode *vp; 402 struct smb_dev *sdp; 403 struct smb_share *ssp; 404 dev_t dev; 405 int error; 406 407 KKASSERT(scred->scr_td->td_proc); 408 409 if ((fp = nsmb_getfp(scred->scr_td->td_proc->p_fd, 410 fd, FREAD | FWRITE)) == NULL) { 411 return EBADF; 412 } 413 vp = (struct vnode*)fp->f_data; 414 if (vp == NULL) 415 return EBADF; 416 dev = vn_todev(vp); 417 if (dev == NODEV) 418 return EBADF; 419 SMB_CHECKMINOR(dev); 420 ssp = sdp->sd_share; 421 if (ssp == NULL) 422 return ENOTCONN; 423 error = smb_share_get(ssp, LK_EXCLUSIVE, scred); 424 if (error) 425 return error; 426 *sspp = ssp; 427 return 0; 428 } 429 430