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