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