1 /* $NetBSD: putter.c,v 1.37 2017/11/30 20:25:55 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2006, 2007 Antti Kantee. All Rights Reserved. 5 * 6 * Development of this software was supported by the 7 * Ulla Tuominen Foundation and the Finnish Cultural Foundation and the 8 * Research Foundation of Helsinki University of Technology 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Pass-to-Userspace TransporTER: generic kernel-user request-response 34 * transport interface. 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: putter.c,v 1.37 2017/11/30 20:25:55 christos Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/conf.h> 43 #include <sys/file.h> 44 #include <sys/filedesc.h> 45 #include <sys/kmem.h> 46 #include <sys/poll.h> 47 #include <sys/stat.h> 48 #include <sys/socketvar.h> 49 #include <sys/module.h> 50 #include <sys/kauth.h> 51 52 #include <dev/putter/putter_sys.h> 53 54 /* 55 * Device routines. These are for when /dev/putter is initially 56 * opened before it has been cloned. 57 */ 58 59 dev_type_open(puttercdopen); 60 dev_type_close(puttercdclose); 61 dev_type_ioctl(puttercdioctl); 62 63 /* dev */ 64 const struct cdevsw putter_cdevsw = { 65 .d_open = puttercdopen, 66 .d_close = puttercdclose, 67 .d_read = noread, 68 .d_write = nowrite, 69 .d_ioctl = noioctl, 70 .d_stop = nostop, 71 .d_tty = notty, 72 .d_poll = nopoll, 73 .d_mmap = nommap, 74 .d_kqfilter = nokqfilter, 75 .d_discard = nodiscard, 76 .d_flag = D_OTHER 77 }; 78 79 /* 80 * Configuration data. 81 * 82 * This is static-size for now. Will be redone for devfs. 83 */ 84 85 #define PUTTER_CONFSIZE 16 86 87 static struct putter_config { 88 int pc_minor; 89 int (*pc_config)(int, int, int); 90 } putterconf[PUTTER_CONFSIZE]; 91 92 static int 93 putter_configure(dev_t dev, int flags, int fmt, int fd) 94 { 95 struct putter_config *pc; 96 97 /* are we the catch-all node? */ 98 if (minor(dev) == PUTTER_MINOR_WILDCARD 99 || minor(dev) == PUTTER_MINOR_COMPAT) 100 return 0; 101 102 /* nopes? try to configure us */ 103 for (pc = putterconf; pc->pc_config; pc++) 104 if (minor(dev) == pc->pc_minor) 105 return pc->pc_config(fd, flags, fmt); 106 return ENXIO; 107 } 108 109 int 110 putter_register(putter_config_fn pcfn, int minor) 111 { 112 int i; 113 114 for (i = 0; i < PUTTER_CONFSIZE; i++) 115 if (putterconf[i].pc_config == NULL) 116 break; 117 if (i == PUTTER_CONFSIZE) 118 return EBUSY; 119 120 putterconf[i].pc_minor = minor; 121 putterconf[i].pc_config = pcfn; 122 return 0; 123 } 124 125 /* 126 * putter instance structures. these are always allocated and freed 127 * from the context of the transport user. 128 */ 129 struct putter_instance { 130 pid_t pi_pid; 131 int pi_idx; 132 int pi_fd; 133 struct selinfo pi_sel; 134 135 void *pi_private; 136 struct putter_ops *pi_pop; 137 138 uint8_t *pi_curput; 139 size_t pi_curres; 140 void *pi_curopaq; 141 struct timespec pi_atime; 142 struct timespec pi_mtime; 143 struct timespec pi_btime; 144 145 TAILQ_ENTRY(putter_instance) pi_entries; 146 }; 147 #define PUTTER_EMBRYO ((void *)-1) /* before attach */ 148 #define PUTTER_DEAD ((void *)-2) /* after detach */ 149 150 static TAILQ_HEAD(, putter_instance) putter_ilist 151 = TAILQ_HEAD_INITIALIZER(putter_ilist); 152 153 static int get_pi_idx(struct putter_instance *); 154 155 #ifdef DEBUG 156 #ifndef PUTTERDEBUG 157 #define PUTTERDEBUG 158 #endif 159 #endif 160 161 #ifdef PUTTERDEBUG 162 int putterdebug = 0; 163 #define DPRINTF(x) if (putterdebug > 0) printf x 164 #define DPRINTF_VERBOSE(x) if (putterdebug > 1) printf x 165 #else 166 #define DPRINTF(x) 167 #define DPRINTF_VERBOSE(x) 168 #endif 169 170 /* 171 * public init / deinit 172 */ 173 174 /* protects both the list and the contents of the list elements */ 175 static kmutex_t pi_mtx; 176 177 void putterattach(void); 178 179 void 180 putterattach(void) 181 { 182 183 mutex_init(&pi_mtx, MUTEX_DEFAULT, IPL_NONE); 184 } 185 186 #if 0 187 void 188 putter_destroy(void) 189 { 190 191 mutex_destroy(&pi_mtx); 192 } 193 #endif 194 195 /* 196 * fd routines, for cloner 197 */ 198 static int putter_fop_read(file_t *, off_t *, struct uio *, 199 kauth_cred_t, int); 200 static int putter_fop_write(file_t *, off_t *, struct uio *, 201 kauth_cred_t, int); 202 static int putter_fop_ioctl(file_t*, u_long, void *); 203 static int putter_fop_poll(file_t *, int); 204 static int putter_fop_stat(file_t *, struct stat *); 205 static int putter_fop_close(file_t *); 206 static int putter_fop_kqfilter(file_t *, struct knote *); 207 208 209 static const struct fileops putter_fileops = { 210 .fo_name = "putter", 211 .fo_read = putter_fop_read, 212 .fo_write = putter_fop_write, 213 .fo_ioctl = putter_fop_ioctl, 214 .fo_fcntl = fnullop_fcntl, 215 .fo_poll = putter_fop_poll, 216 .fo_stat = putter_fop_stat, 217 .fo_close = putter_fop_close, 218 .fo_kqfilter = putter_fop_kqfilter, 219 .fo_restart = fnullop_restart, 220 }; 221 222 static int 223 putter_fop_read(file_t *fp, off_t *off, struct uio *uio, 224 kauth_cred_t cred, int flags) 225 { 226 struct putter_instance *pi = fp->f_data; 227 size_t origres, moved; 228 int error; 229 230 KERNEL_LOCK(1, NULL); 231 getnanotime(&pi->pi_atime); 232 233 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) { 234 printf("putter_fop_read: private %d not inited\n", pi->pi_idx); 235 KERNEL_UNLOCK_ONE(NULL); 236 return ENOENT; 237 } 238 239 if (pi->pi_curput == NULL) { 240 error = pi->pi_pop->pop_getout(pi->pi_private, uio->uio_resid, 241 fp->f_flag & O_NONBLOCK, &pi->pi_curput, 242 &pi->pi_curres, &pi->pi_curopaq); 243 if (error) { 244 KERNEL_UNLOCK_ONE(NULL); 245 return error; 246 } 247 } 248 249 origres = uio->uio_resid; 250 error = uiomove(pi->pi_curput, pi->pi_curres, uio); 251 moved = origres - uio->uio_resid; 252 DPRINTF(("putter_fop_read (%p): moved %zu bytes from %p, error %d\n", 253 pi, moved, pi->pi_curput, error)); 254 255 KASSERT(pi->pi_curres >= moved); 256 pi->pi_curres -= moved; 257 pi->pi_curput += moved; 258 259 if (pi->pi_curres == 0) { 260 pi->pi_pop->pop_releaseout(pi->pi_private, 261 pi->pi_curopaq, error); 262 pi->pi_curput = NULL; 263 } 264 265 KERNEL_UNLOCK_ONE(NULL); 266 return error; 267 } 268 269 static int 270 putter_fop_write(file_t *fp, off_t *off, struct uio *uio, 271 kauth_cred_t cred, int flags) 272 { 273 struct putter_instance *pi = fp->f_data; 274 struct putter_hdr pth; 275 uint8_t *buf; 276 size_t frsize; 277 int error; 278 279 KERNEL_LOCK(1, NULL); 280 getnanotime(&pi->pi_mtime); 281 282 DPRINTF(("putter_fop_write (%p): writing response, resid %zu\n", 283 pi->pi_private, uio->uio_resid)); 284 285 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) { 286 printf("putter_fop_write: putter %d not inited\n", pi->pi_idx); 287 KERNEL_UNLOCK_ONE(NULL); 288 return ENOENT; 289 } 290 291 error = uiomove(&pth, sizeof(struct putter_hdr), uio); 292 if (error) { 293 KERNEL_UNLOCK_ONE(NULL); 294 return error; 295 } 296 297 /* Sorry mate, the kernel doesn't buffer. */ 298 frsize = pth.pth_framelen - sizeof(struct putter_hdr); 299 if (uio->uio_resid < frsize) { 300 KERNEL_UNLOCK_ONE(NULL); 301 return EINVAL; 302 } 303 304 buf = kmem_alloc(frsize + sizeof(struct putter_hdr), KM_SLEEP); 305 memcpy(buf, &pth, sizeof(pth)); 306 error = uiomove(buf+sizeof(struct putter_hdr), frsize, uio); 307 if (error == 0) { 308 pi->pi_pop->pop_dispatch(pi->pi_private, 309 (struct putter_hdr *)buf); 310 } 311 kmem_free(buf, frsize + sizeof(struct putter_hdr)); 312 313 KERNEL_UNLOCK_ONE(NULL); 314 return error; 315 } 316 317 /* 318 * Poll query interface. The question is only if an event 319 * can be read from us. 320 */ 321 #define PUTTERPOLL_EVSET (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI) 322 static int 323 putter_fop_poll(file_t *fp, int events) 324 { 325 struct putter_instance *pi = fp->f_data; 326 int revents; 327 328 KERNEL_LOCK(1, NULL); 329 330 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) { 331 printf("putter_fop_ioctl: putter %d not inited\n", pi->pi_idx); 332 KERNEL_UNLOCK_ONE(NULL); 333 return ENOENT; 334 } 335 336 revents = events & (POLLOUT | POLLWRNORM | POLLWRBAND); 337 if ((events & PUTTERPOLL_EVSET) == 0) { 338 KERNEL_UNLOCK_ONE(NULL); 339 return revents; 340 } 341 342 /* check queue */ 343 if (pi->pi_pop->pop_waitcount(pi->pi_private)) 344 revents |= PUTTERPOLL_EVSET; 345 else 346 selrecord(curlwp, &pi->pi_sel); 347 348 KERNEL_UNLOCK_ONE(NULL); 349 return revents; 350 } 351 352 /* 353 * device close = forced unmount. 354 * 355 * unmounting is a frightfully complex operation to avoid races 356 */ 357 static int 358 putter_fop_close(file_t *fp) 359 { 360 struct putter_instance *pi = fp->f_data; 361 int rv; 362 363 DPRINTF(("putter_fop_close: device closed\n")); 364 365 KERNEL_LOCK(1, NULL); 366 367 restart: 368 mutex_enter(&pi_mtx); 369 /* 370 * First check if the driver was never born. In that case 371 * remove the instance from the list. If mount is attempted later, 372 * it will simply fail. 373 */ 374 if (pi->pi_private == PUTTER_EMBRYO) { 375 TAILQ_REMOVE(&putter_ilist, pi, pi_entries); 376 mutex_exit(&pi_mtx); 377 378 DPRINTF(("putter_fop_close: data associated with fp %p was " 379 "embryonic\n", fp)); 380 381 goto out; 382 } 383 384 /* 385 * Next, analyze if unmount was called and the instance is dead. 386 * In this case we can just free the structure and go home, it 387 * was removed from the list by putter_rmprivate(). 388 */ 389 if (pi->pi_private == PUTTER_DEAD) { 390 mutex_exit(&pi_mtx); 391 392 DPRINTF(("putter_fop_close: putter associated with fp %p (%d) " 393 "dead, freeing\n", fp, pi->pi_idx)); 394 395 goto out; 396 } 397 398 /* 399 * So we have a reference. Proceed to unravel the 400 * underlying driver. 401 */ 402 mutex_exit(&pi_mtx); 403 404 /* hmm? suspicious locking? */ 405 if (pi->pi_curput != NULL) { 406 pi->pi_pop->pop_releaseout(pi->pi_private, pi->pi_curopaq, 407 ENXIO); 408 pi->pi_curput = NULL; 409 } 410 while ((rv = pi->pi_pop->pop_close(pi->pi_private)) == ERESTART) 411 goto restart; 412 413 out: 414 KERNEL_UNLOCK_ONE(NULL); 415 /* 416 * Finally, release the instance information. It was already 417 * removed from the list by putter_rmprivate() and we know it's 418 * dead, so no need to lock. 419 */ 420 kmem_free(pi, sizeof(struct putter_instance)); 421 422 return 0; 423 } 424 425 static int 426 putter_fop_stat(file_t *fp, struct stat *st) 427 { 428 struct putter_instance *pi = fp->f_data; 429 430 (void)memset(st, 0, sizeof(*st)); 431 KERNEL_LOCK(1, NULL); 432 st->st_dev = makedev(cdevsw_lookup_major(&putter_cdevsw), pi->pi_idx); 433 st->st_atimespec = pi->pi_atime; 434 st->st_mtimespec = pi->pi_mtime; 435 st->st_ctimespec = st->st_birthtimespec = pi->pi_btime; 436 st->st_uid = kauth_cred_geteuid(fp->f_cred); 437 st->st_gid = kauth_cred_getegid(fp->f_cred); 438 st->st_mode = S_IFCHR; 439 KERNEL_UNLOCK_ONE(NULL); 440 return 0; 441 } 442 443 static int 444 putter_fop_ioctl(file_t *fp, u_long cmd, void *data) 445 { 446 447 /* 448 * work already done in sys_ioctl(). skip sanity checks to enable 449 * setting non-blocking fd on an embryotic driver. 450 */ 451 if (cmd == FIONBIO) 452 return 0; 453 454 return EINVAL; 455 } 456 457 /* kqueue stuff */ 458 459 static void 460 filt_putterdetach(struct knote *kn) 461 { 462 struct putter_instance *pi = kn->kn_hook; 463 464 KERNEL_LOCK(1, NULL); 465 mutex_enter(&pi_mtx); 466 SLIST_REMOVE(&pi->pi_sel.sel_klist, kn, knote, kn_selnext); 467 mutex_exit(&pi_mtx); 468 KERNEL_UNLOCK_ONE(NULL); 469 } 470 471 static int 472 filt_putter(struct knote *kn, long hint) 473 { 474 struct putter_instance *pi = kn->kn_hook; 475 int error, rv; 476 477 KERNEL_LOCK(1, NULL); 478 error = 0; 479 mutex_enter(&pi_mtx); 480 if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) 481 error = 1; 482 mutex_exit(&pi_mtx); 483 if (error) { 484 KERNEL_UNLOCK_ONE(NULL); 485 return 0; 486 } 487 488 kn->kn_data = pi->pi_pop->pop_waitcount(pi->pi_private); 489 rv = kn->kn_data != 0; 490 KERNEL_UNLOCK_ONE(NULL); 491 return rv; 492 } 493 494 static const struct filterops putter_filtops = { 495 .f_isfd = 1, 496 .f_attach = NULL, 497 .f_detach = filt_putterdetach, 498 .f_event = filt_putter, 499 }; 500 501 static int 502 putter_fop_kqfilter(file_t *fp, struct knote *kn) 503 { 504 struct putter_instance *pi = fp->f_data; 505 struct klist *klist; 506 507 KERNEL_LOCK(1, NULL); 508 509 switch (kn->kn_filter) { 510 case EVFILT_READ: 511 klist = &pi->pi_sel.sel_klist; 512 kn->kn_fop = &putter_filtops; 513 kn->kn_hook = pi; 514 515 mutex_enter(&pi_mtx); 516 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 517 mutex_exit(&pi_mtx); 518 519 break; 520 case EVFILT_WRITE: 521 kn->kn_fop = &seltrue_filtops; 522 break; 523 default: 524 KERNEL_UNLOCK_ONE(NULL); 525 return EINVAL; 526 } 527 528 KERNEL_UNLOCK_ONE(NULL); 529 return 0; 530 } 531 532 int 533 puttercdopen(dev_t dev, int flags, int fmt, struct lwp *l) 534 { 535 struct putter_instance *pi; 536 file_t *fp; 537 int error, fd, idx; 538 proc_t *p; 539 540 p = curproc; 541 pi = kmem_alloc(sizeof(struct putter_instance), KM_SLEEP); 542 mutex_enter(&pi_mtx); 543 idx = get_pi_idx(pi); 544 545 pi->pi_pid = p->p_pid; 546 pi->pi_idx = idx; 547 pi->pi_curput = NULL; 548 pi->pi_curres = 0; 549 pi->pi_curopaq = NULL; 550 getnanotime(&pi->pi_btime); 551 pi->pi_atime = pi->pi_mtime = pi->pi_btime; 552 selinit(&pi->pi_sel); 553 mutex_exit(&pi_mtx); 554 555 if ((error = fd_allocfile(&fp, &fd)) != 0) 556 goto bad1; 557 558 if ((error = putter_configure(dev, flags, fmt, fd)) != 0) 559 goto bad2; 560 561 DPRINTF(("puttercdopen: registered embryonic pmp for pid: %d\n", 562 pi->pi_pid)); 563 564 error = fd_clone(fp, fd, FREAD|FWRITE, &putter_fileops, pi); 565 KASSERT(error == EMOVEFD); 566 return error; 567 568 bad2: 569 fd_abort(p, fp, fd); 570 bad1: 571 putter_detach(pi); 572 kmem_free(pi, sizeof(struct putter_instance)); 573 return error; 574 } 575 576 int 577 puttercdclose(dev_t dev, int flags, int fmt, struct lwp *l) 578 { 579 580 panic("puttercdclose impossible\n"); 581 582 return 0; 583 } 584 585 586 /* 587 * Set the private structure for the file descriptor. This is 588 * typically done immediately when the counterpart has knowledge 589 * about the private structure's address and the file descriptor 590 * (e.g. vfs mount routine). 591 * 592 * We only want to make sure that the caller had the right to open the 593 * device, we don't so much care about which context it gets in case 594 * the same process opened multiple (since they are equal at this point). 595 */ 596 struct putter_instance * 597 putter_attach(pid_t pid, int fd, void *ppriv, struct putter_ops *pop) 598 { 599 struct putter_instance *pi = NULL; 600 601 mutex_enter(&pi_mtx); 602 TAILQ_FOREACH(pi, &putter_ilist, pi_entries) { 603 if (pi->pi_pid == pid && pi->pi_private == PUTTER_EMBRYO) { 604 pi->pi_private = ppriv; 605 pi->pi_fd = fd; 606 pi->pi_pop = pop; 607 break; 608 } 609 } 610 mutex_exit(&pi_mtx); 611 612 DPRINTF(("putter_setprivate: pi at %p (%d/%d)\n", pi, 613 pi ? pi->pi_pid : 0, pi ? pi->pi_fd : 0)); 614 615 return pi; 616 } 617 618 /* 619 * Remove fp <-> private mapping. 620 */ 621 void 622 putter_detach(struct putter_instance *pi) 623 { 624 625 mutex_enter(&pi_mtx); 626 TAILQ_REMOVE(&putter_ilist, pi, pi_entries); 627 pi->pi_private = PUTTER_DEAD; 628 mutex_exit(&pi_mtx); 629 seldestroy(&pi->pi_sel); 630 631 DPRINTF(("putter_nukebypmp: nuked %p\n", pi)); 632 } 633 634 void 635 putter_notify(struct putter_instance *pi) 636 { 637 638 selnotify(&pi->pi_sel, 0, 0); 639 } 640 641 /* search sorted list of instances for free minor, sorted insert arg */ 642 static int 643 get_pi_idx(struct putter_instance *pi_i) 644 { 645 struct putter_instance *pi; 646 int i; 647 648 KASSERT(mutex_owned(&pi_mtx)); 649 650 i = 0; 651 TAILQ_FOREACH(pi, &putter_ilist, pi_entries) { 652 if (i != pi->pi_idx) 653 break; 654 i++; 655 } 656 657 pi_i->pi_private = PUTTER_EMBRYO; 658 659 if (pi == NULL) 660 TAILQ_INSERT_TAIL(&putter_ilist, pi_i, pi_entries); 661 else 662 TAILQ_INSERT_BEFORE(pi, pi_i, pi_entries); 663 664 return i; 665 } 666 667 MODULE(MODULE_CLASS_DRIVER, putter, NULL); 668 669 static int 670 putter_modcmd(modcmd_t cmd, void *arg) 671 { 672 #ifdef _MODULE 673 devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR; 674 675 switch (cmd) { 676 case MODULE_CMD_INIT: 677 putterattach(); 678 return devsw_attach("putter", NULL, &bmajor, 679 &putter_cdevsw, &cmajor); 680 case MODULE_CMD_FINI: 681 return ENOTTY; /* XXX: putterdetach */ 682 default: 683 return ENOTTY; 684 } 685 #else 686 if (cmd == MODULE_CMD_INIT) 687 return 0; 688 return ENOTTY; 689 #endif 690 } 691