1 /* 2 * Copyright (c) 1995 Ugen J.S.Antsilevich 3 * 4 * Redistribution and use in source forms, with and without modification, 5 * are permitted provided that this entire comment appears intact. 6 * 7 * Redistribution in binary form may occur without any restrictions. 8 * Obviously, it would be nice if you gave credit where credit is due 9 * but requiring it would be too onerous. 10 * 11 * This software is provided ``AS IS'' without any warranties of any kind. 12 * 13 * Snoop stuff. 14 * 15 * $FreeBSD: src/sys/dev/snp/snp.c,v 1.69.2.2 2002/05/06 07:30:02 dd Exp $ 16 * $DragonFly: src/sys/dev/misc/snp/snp.c,v 1.19 2007/05/08 02:31:41 dillon Exp $ 17 */ 18 19 #include "use_snp.h" 20 #include <sys/param.h> 21 #include <sys/systm.h> 22 #include <sys/filio.h> 23 #include <sys/malloc.h> 24 #include <sys/tty.h> 25 #include <sys/conf.h> 26 #include <sys/event.h> 27 #include <sys/kernel.h> 28 #include <sys/queue.h> 29 #include <sys/snoop.h> 30 #include <sys/thread2.h> 31 #include <sys/vnode.h> 32 #include <sys/device.h> 33 #include <sys/devfs.h> 34 35 static l_close_t snplclose; 36 static l_write_t snplwrite; 37 static d_open_t snpopen; 38 static d_close_t snpclose; 39 static d_read_t snpread; 40 static d_write_t snpwrite; 41 static d_ioctl_t snpioctl; 42 static d_kqfilter_t snpkqfilter; 43 static d_clone_t snpclone; 44 DEVFS_DECLARE_CLONE_BITMAP(snp); 45 46 static void snpfilter_detach(struct knote *); 47 static int snpfilter_rd(struct knote *, long); 48 static int snpfilter_wr(struct knote *, long); 49 50 #if NSNP <= 1 51 #define SNP_PREALLOCATED_UNITS 4 52 #else 53 #define SNP_PREALLOCATED_UNITS NSNP 54 #endif 55 56 #define CDEV_MAJOR 53 57 static struct dev_ops snp_ops = { 58 { "snp", CDEV_MAJOR, D_KQFILTER }, 59 .d_open = snpopen, 60 .d_close = snpclose, 61 .d_read = snpread, 62 .d_write = snpwrite, 63 .d_ioctl = snpioctl, 64 .d_kqfilter = snpkqfilter 65 }; 66 67 static struct linesw snpdisc = { 68 ttyopen, snplclose, ttread, snplwrite, 69 l_nullioctl, ttyinput, ttstart, ttymodem 70 }; 71 72 /* 73 * This is the main snoop per-device structure. 74 */ 75 struct snoop { 76 LIST_ENTRY(snoop) snp_list; /* List glue. */ 77 cdev_t snp_target; /* Target tty device. */ 78 struct tty *snp_tty; /* Target tty pointer. */ 79 u_long snp_len; /* Possible length. */ 80 u_long snp_base; /* Data base. */ 81 u_long snp_blen; /* Used length. */ 82 caddr_t snp_buf; /* Allocation pointer. */ 83 int snp_flags; /* Flags. */ 84 struct selinfo snp_sel; /* Select info. */ 85 int snp_olddisc; /* Old line discipline. */ 86 }; 87 88 /* 89 * Possible flags. 90 */ 91 #define SNOOP_ASYNC 0x0002 92 #define SNOOP_OPEN 0x0004 93 #define SNOOP_RWAIT 0x0008 94 #define SNOOP_OFLOW 0x0010 95 #define SNOOP_DOWN 0x0020 96 97 /* 98 * Other constants. 99 */ 100 #define SNOOP_MINLEN (4*1024) /* This should be power of 2. 101 * 4K tested to be the minimum 102 * for which on normal tty 103 * usage there is no need to 104 * allocate more. 105 */ 106 #define SNOOP_MAXLEN (64*1024) /* This one also,64K enough 107 * If we grow more,something 108 * really bad in this world.. 109 */ 110 111 static MALLOC_DEFINE(M_SNP, "snp", "Snoop device data"); 112 /* 113 * The number of the "snoop" line discipline. This gets determined at 114 * module load time. 115 */ 116 static int snooplinedisc; 117 118 119 static LIST_HEAD(, snoop) snp_sclist = LIST_HEAD_INITIALIZER(&snp_sclist); 120 121 static struct tty *snpdevtotty (cdev_t dev); 122 static int snp_detach (struct snoop *snp); 123 static int snp_down (struct snoop *snp); 124 static int snp_in (struct snoop *snp, char *buf, int n); 125 static int snp_modevent (module_t mod, int what, void *arg); 126 127 static int 128 snplclose(struct tty *tp, int flag) 129 { 130 struct snoop *snp; 131 int error; 132 133 snp = tp->t_sc; 134 error = snp_down(snp); 135 if (error != 0) 136 return (error); 137 error = ttylclose(tp, flag); 138 return (error); 139 } 140 141 static int 142 snplwrite(struct tty *tp, struct uio *uio, int flag) 143 { 144 struct iovec iov; 145 struct uio uio2; 146 struct snoop *snp; 147 int error, ilen; 148 char *ibuf; 149 150 error = 0; 151 ibuf = NULL; 152 snp = tp->t_sc; 153 while (uio->uio_resid > 0) { 154 ilen = (int)szmin(512, uio->uio_resid); 155 ibuf = kmalloc(ilen, M_SNP, M_WAITOK); 156 error = uiomove(ibuf, (size_t)ilen, uio); 157 if (error != 0) 158 break; 159 snp_in(snp, ibuf, ilen); 160 /* Hackish, but probably the least of all evils. */ 161 iov.iov_base = ibuf; 162 iov.iov_len = ilen; 163 uio2.uio_iov = &iov; 164 uio2.uio_iovcnt = 1; 165 uio2.uio_offset = 0; 166 uio2.uio_resid = ilen; 167 uio2.uio_segflg = UIO_SYSSPACE; 168 uio2.uio_rw = UIO_WRITE; 169 uio2.uio_td = uio->uio_td; 170 error = ttwrite(tp, &uio2, flag); 171 if (error != 0) 172 break; 173 kfree(ibuf, M_SNP); 174 ibuf = NULL; 175 } 176 if (ibuf != NULL) 177 kfree(ibuf, M_SNP); 178 return (error); 179 } 180 181 static struct tty * 182 snpdevtotty(cdev_t dev) 183 { 184 if ((dev_dflags(dev) & D_TTY) == 0) 185 return (NULL); 186 return (dev->si_tty); 187 } 188 189 #define SNP_INPUT_BUF 5 /* This is even too much, the maximal 190 * interactive mode write is 3 bytes 191 * length for function keys... 192 */ 193 194 static int 195 snpwrite(struct dev_write_args *ap) 196 { 197 cdev_t dev = ap->a_head.a_dev; 198 struct uio *uio = ap->a_uio; 199 struct snoop *snp; 200 struct tty *tp; 201 int error, i, len; 202 unsigned char c[SNP_INPUT_BUF]; 203 204 snp = dev->si_drv1; 205 tp = snp->snp_tty; 206 if (tp == NULL) 207 return (EIO); 208 if ((tp->t_sc == snp) && (tp->t_state & TS_SNOOP) && 209 tp->t_line == snooplinedisc) 210 goto tty_input; 211 212 kprintf("Snoop: attempt to write to bad tty.\n"); 213 return (EIO); 214 215 tty_input: 216 if (!(tp->t_state & TS_ISOPEN)) 217 return (EIO); 218 219 while (uio->uio_resid > 0) { 220 len = (int)szmin(uio->uio_resid, SNP_INPUT_BUF); 221 if ((error = uiomove(c, (size_t)len, uio)) != 0) 222 return (error); 223 for (i=0; i < len; i++) { 224 if (ttyinput(c[i], tp)) 225 return (EIO); 226 } 227 } 228 return (0); 229 } 230 231 232 static int 233 snpread(struct dev_read_args *ap) 234 { 235 cdev_t dev = ap->a_head.a_dev; 236 struct uio *uio = ap->a_uio; 237 struct snoop *snp; 238 int error, len, n, nblen; 239 caddr_t from; 240 char *nbuf; 241 242 snp = dev->si_drv1; 243 KASSERT(snp->snp_len + snp->snp_base <= snp->snp_blen, 244 ("snoop buffer error")); 245 246 if (snp->snp_tty == NULL) 247 return (EIO); 248 249 snp->snp_flags &= ~SNOOP_RWAIT; 250 251 do { 252 if (snp->snp_len == 0) { 253 if (ap->a_ioflag & IO_NDELAY) 254 return (EWOULDBLOCK); 255 snp->snp_flags |= SNOOP_RWAIT; 256 error = tsleep((caddr_t)snp, PCATCH, "snprd", 0); 257 if (error != 0) 258 return (error); 259 } 260 } while (snp->snp_len == 0); 261 262 n = snp->snp_len; 263 264 error = 0; 265 while (snp->snp_len > 0 && uio->uio_resid > 0 && error == 0) { 266 len = (int)szmin(uio->uio_resid, snp->snp_len); 267 from = (caddr_t)(snp->snp_buf + snp->snp_base); 268 if (len == 0) 269 break; 270 271 error = uiomove(from, (size_t)len, uio); 272 snp->snp_base += len; 273 snp->snp_len -= len; 274 } 275 if ((snp->snp_flags & SNOOP_OFLOW) && (n < snp->snp_len)) { 276 snp->snp_flags &= ~SNOOP_OFLOW; 277 } 278 crit_enter(); 279 nblen = snp->snp_blen; 280 if (((nblen / 2) >= SNOOP_MINLEN) && (nblen / 2) >= snp->snp_len) { 281 while (nblen / 2 >= snp->snp_len && nblen / 2 >= SNOOP_MINLEN) 282 nblen = nblen / 2; 283 if ((nbuf = kmalloc(nblen, M_SNP, M_NOWAIT)) != NULL) { 284 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len); 285 kfree(snp->snp_buf, M_SNP); 286 snp->snp_buf = nbuf; 287 snp->snp_blen = nblen; 288 snp->snp_base = 0; 289 } 290 } 291 crit_exit(); 292 293 return (error); 294 } 295 296 static int 297 snp_in(struct snoop *snp, char *buf, int n) 298 { 299 int s_free, s_tail; 300 int len, nblen; 301 caddr_t from, to; 302 char *nbuf; 303 304 KASSERT(n >= 0, ("negative snoop char count")); 305 306 if (n == 0) 307 return (0); 308 309 if (snp->snp_flags & SNOOP_DOWN) { 310 kprintf("Snoop: more data to down interface.\n"); 311 return (0); 312 } 313 314 if (snp->snp_flags & SNOOP_OFLOW) { 315 kprintf("Snoop: buffer overflow.\n"); 316 /* 317 * On overflow we just repeat the standart close 318 * procedure...yes , this is waste of space but.. Then next 319 * read from device will fail if one would recall he is 320 * snooping and retry... 321 */ 322 323 return (snp_down(snp)); 324 } 325 s_tail = snp->snp_blen - (snp->snp_len + snp->snp_base); 326 s_free = snp->snp_blen - snp->snp_len; 327 328 329 if (n > s_free) { 330 crit_enter(); 331 nblen = snp->snp_blen; 332 while ((n > s_free) && ((nblen * 2) <= SNOOP_MAXLEN)) { 333 nblen = snp->snp_blen * 2; 334 s_free = nblen - (snp->snp_len + snp->snp_base); 335 } 336 if ((n <= s_free) && (nbuf = kmalloc(nblen, M_SNP, M_NOWAIT))) { 337 bcopy(snp->snp_buf + snp->snp_base, nbuf, snp->snp_len); 338 kfree(snp->snp_buf, M_SNP); 339 snp->snp_buf = nbuf; 340 snp->snp_blen = nblen; 341 snp->snp_base = 0; 342 } else { 343 snp->snp_flags |= SNOOP_OFLOW; 344 if (snp->snp_flags & SNOOP_RWAIT) { 345 snp->snp_flags &= ~SNOOP_RWAIT; 346 wakeup((caddr_t)snp); 347 } 348 crit_exit(); 349 return (0); 350 } 351 crit_exit(); 352 } 353 if (n > s_tail) { 354 from = (caddr_t)(snp->snp_buf + snp->snp_base); 355 to = (caddr_t)(snp->snp_buf); 356 len = snp->snp_len; 357 bcopy(from, to, len); 358 snp->snp_base = 0; 359 } 360 to = (caddr_t)(snp->snp_buf + snp->snp_base + snp->snp_len); 361 bcopy(buf, to, n); 362 snp->snp_len += n; 363 364 if (snp->snp_flags & SNOOP_RWAIT) { 365 snp->snp_flags &= ~SNOOP_RWAIT; 366 wakeup((caddr_t)snp); 367 } 368 selwakeup(&snp->snp_sel); 369 snp->snp_sel.si_pid = 0; 370 371 return (n); 372 } 373 374 static int 375 snpopen(struct dev_open_args *ap) 376 { 377 cdev_t dev = ap->a_head.a_dev; 378 struct snoop *snp; 379 380 if (dev->si_drv1 == NULL) { 381 #if 0 382 make_dev(&snp_ops, minor(dev), UID_ROOT, GID_WHEEL, 383 0600, "snp%d", minor(dev)); 384 #endif 385 dev->si_drv1 = snp = kmalloc(sizeof(*snp), M_SNP, 386 M_WAITOK | M_ZERO); 387 } else { 388 return (EBUSY); 389 } 390 391 /* 392 * We intentionally do not OR flags with SNOOP_OPEN, but set them so 393 * all previous settings (especially SNOOP_OFLOW) will be cleared. 394 */ 395 snp->snp_flags = SNOOP_OPEN; 396 397 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK); 398 snp->snp_blen = SNOOP_MINLEN; 399 snp->snp_base = 0; 400 snp->snp_len = 0; 401 402 /* 403 * snp_tty == NULL is for inactive snoop devices. 404 */ 405 snp->snp_tty = NULL; 406 snp->snp_target = NULL; 407 408 LIST_INSERT_HEAD(&snp_sclist, snp, snp_list); 409 return (0); 410 } 411 412 413 static int 414 snp_detach(struct snoop *snp) 415 { 416 struct tty *tp; 417 418 snp->snp_base = 0; 419 snp->snp_len = 0; 420 421 /* 422 * If line disc. changed we do not touch this pointer, SLIP/PPP will 423 * change it anyway. 424 */ 425 tp = snp->snp_tty; 426 if (tp == NULL) 427 goto detach_notty; 428 429 if (tp && (tp->t_sc == snp) && (tp->t_state & TS_SNOOP) && 430 tp->t_line == snooplinedisc) { 431 tp->t_sc = NULL; 432 tp->t_state &= ~TS_SNOOP; 433 tp->t_line = snp->snp_olddisc; 434 } else 435 kprintf("Snoop: bad attached tty data.\n"); 436 437 snp->snp_tty = NULL; 438 snp->snp_target = NULL; 439 440 detach_notty: 441 selwakeup(&snp->snp_sel); 442 snp->snp_sel.si_pid = 0; 443 if ((snp->snp_flags & SNOOP_OPEN) == 0) 444 kfree(snp, M_SNP); 445 446 return (0); 447 } 448 449 static int 450 snpclose(struct dev_close_args *ap) 451 { 452 cdev_t dev = ap->a_head.a_dev; 453 struct snoop *snp; 454 455 snp = dev->si_drv1; 456 snp->snp_blen = 0; 457 LIST_REMOVE(snp, snp_list); 458 kfree(snp->snp_buf, M_SNP); 459 snp->snp_flags &= ~SNOOP_OPEN; 460 dev->si_drv1 = NULL; 461 if (dev->si_uminor >= SNP_PREALLOCATED_UNITS) { 462 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(snp), dev->si_uminor); 463 destroy_dev(dev); 464 } 465 return (snp_detach(snp)); 466 } 467 468 static int 469 snp_down(struct snoop *snp) 470 { 471 472 if (snp->snp_blen != SNOOP_MINLEN) { 473 kfree(snp->snp_buf, M_SNP); 474 snp->snp_buf = kmalloc(SNOOP_MINLEN, M_SNP, M_WAITOK); 475 snp->snp_blen = SNOOP_MINLEN; 476 } 477 snp->snp_flags |= SNOOP_DOWN; 478 479 return (snp_detach(snp)); 480 } 481 482 static int 483 snpioctl(struct dev_ioctl_args *ap) 484 { 485 cdev_t dev = ap->a_head.a_dev; 486 struct snoop *snp; 487 struct tty *tp, *tpo; 488 cdev_t tdev; 489 490 snp = dev->si_drv1; 491 switch (ap->a_cmd) { 492 case SNPSTTY: 493 tdev = udev2dev(*((udev_t *)ap->a_data), 0); 494 if (tdev == NULL) 495 return (snp_down(snp)); 496 497 tp = snpdevtotty(tdev); 498 if (!tp) 499 return (EINVAL); 500 if (tp->t_state & TS_SNOOP) 501 return (EBUSY); 502 503 crit_enter(); 504 505 if (snp->snp_target == NULL) { 506 tpo = snp->snp_tty; 507 if (tpo) 508 tpo->t_state &= ~TS_SNOOP; 509 } 510 511 tp->t_sc = (caddr_t)snp; 512 tp->t_state |= TS_SNOOP; 513 snp->snp_olddisc = tp->t_line; 514 tp->t_line = snooplinedisc; 515 snp->snp_tty = tp; 516 snp->snp_target = tdev; 517 518 /* 519 * Clean overflow and down flags - 520 * we'll have a chance to get them in the future :))) 521 */ 522 snp->snp_flags &= ~SNOOP_OFLOW; 523 snp->snp_flags &= ~SNOOP_DOWN; 524 crit_exit(); 525 break; 526 527 case SNPGTTY: 528 /* 529 * We keep snp_target field specially to make 530 * SNPGTTY happy, else we can't know what is device 531 * major/minor for tty. 532 */ 533 *((cdev_t *)ap->a_data) = snp->snp_target; 534 break; 535 536 case FIOASYNC: 537 if (*(int *)ap->a_data) 538 snp->snp_flags |= SNOOP_ASYNC; 539 else 540 snp->snp_flags &= ~SNOOP_ASYNC; 541 break; 542 543 case FIONREAD: 544 crit_enter(); 545 if (snp->snp_tty != NULL) 546 *(int *)ap->a_data = snp->snp_len; 547 else 548 if (snp->snp_flags & SNOOP_DOWN) { 549 if (snp->snp_flags & SNOOP_OFLOW) 550 *(int *)ap->a_data = SNP_OFLOW; 551 else 552 *(int *)ap->a_data = SNP_TTYCLOSE; 553 } else { 554 *(int *)ap->a_data = SNP_DETACH; 555 } 556 crit_exit(); 557 break; 558 559 default: 560 return (ENOTTY); 561 } 562 return (0); 563 } 564 565 static struct filterops snpfiltops_rd = 566 { 1, NULL, snpfilter_detach, snpfilter_rd }; 567 static struct filterops snpfiltops_wr = 568 { 1, NULL, snpfilter_detach, snpfilter_wr }; 569 570 static int 571 snpkqfilter(struct dev_kqfilter_args *ap) 572 { 573 cdev_t dev = ap->a_head.a_dev; 574 struct snoop *snp = dev->si_drv1; 575 struct knote *kn = ap->a_kn; 576 struct klist *klist; 577 578 ap->a_result = 0; 579 580 switch (kn->kn_filter) { 581 case EVFILT_READ: 582 kn->kn_fop = &snpfiltops_rd; 583 kn->kn_hook = (caddr_t)snp; 584 break; 585 case EVFILT_WRITE: 586 kn->kn_fop = &snpfiltops_wr; 587 kn->kn_hook = (caddr_t)snp; 588 break; 589 default: 590 ap->a_result = EOPNOTSUPP; 591 return (0); 592 } 593 594 crit_enter(); 595 klist = &snp->snp_sel.si_note; 596 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 597 crit_exit(); 598 599 return (0); 600 } 601 602 static void 603 snpfilter_detach(struct knote *kn) 604 { 605 struct snoop *snp = (struct snoop *)kn->kn_hook; 606 struct klist *klist; 607 608 crit_enter(); 609 klist = &snp->snp_sel.si_note; 610 SLIST_REMOVE(klist, kn, knote, kn_selnext); 611 crit_exit(); 612 } 613 614 static int 615 snpfilter_rd(struct knote *kn, long hint) 616 { 617 struct snoop *snp = (struct snoop *)kn->kn_hook; 618 int ready = 0; 619 620 /* 621 * If snoop is down, we don't want to poll forever so we return 1. 622 * Caller should see if we down via FIONREAD ioctl(). The last should 623 * return -1 to indicate down state. 624 */ 625 if (snp->snp_flags & SNOOP_DOWN || snp->snp_len > 0) 626 ready = 1; 627 628 return (ready); 629 } 630 631 static int 632 snpfilter_wr(struct knote *kn, long hint) 633 { 634 /* Writing is always OK */ 635 return (1); 636 } 637 638 static int 639 snpclone(struct dev_clone_args *ap) 640 { 641 int unit; 642 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(snp), 0); 643 ap->a_dev = make_only_dev(&snp_ops, unit, UID_ROOT, GID_WHEEL, 0600, 644 "snp%d", unit); 645 646 return 0; 647 } 648 649 static int 650 snp_modevent(module_t mod, int type, void *data) 651 { 652 int i; 653 654 switch (type) { 655 case MOD_LOAD: 656 snooplinedisc = ldisc_register(LDISC_LOAD, &snpdisc); 657 make_autoclone_dev(&snp_ops, &DEVFS_CLONE_BITMAP(snp), 658 snpclone, UID_ROOT, GID_WHEEL, 0600, "snp"); 659 660 for (i = 0; i < SNP_PREALLOCATED_UNITS; i++) { 661 make_dev(&snp_ops, i, UID_ROOT, GID_WHEEL, 0600, "snp%d", i); 662 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(snp), i); 663 } 664 break; 665 case MOD_UNLOAD: 666 if (!LIST_EMPTY(&snp_sclist)) 667 return (EBUSY); 668 ldisc_deregister(snooplinedisc); 669 devfs_clone_handler_del("snp"); 670 dev_ops_remove_all(&snp_ops); 671 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(snp)); 672 break; 673 default: 674 break; 675 } 676 return (0); 677 } 678 679 static moduledata_t snp_mod = { 680 "snp", 681 snp_modevent, 682 NULL 683 }; 684 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR); 685