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