1 /* 2 * (MPSAFE) 3 * 4 * ng_tty.c 5 * 6 * Copyright (c) 1996-1999 Whistle Communications, Inc. 7 * All rights reserved. 8 * 9 * Subject to the following obligations and disclaimer of warranty, use and 10 * redistribution of this software, in source or object code forms, with or 11 * without modifications are expressly permitted by Whistle Communications; 12 * provided, however, that: 13 * 1. Any and all reproductions of the source or object code must include the 14 * copyright notice above and the following disclaimer of warranties; and 15 * 2. No rights are granted, in any manner or form, to use Whistle 16 * Communications, Inc. trademarks, including the mark "WHISTLE 17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 18 * such appears in the above copyright notice or in the software. 19 * 20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 36 * OF SUCH DAMAGE. 37 * 38 * Author: Archie Cobbs <archie@freebsd.org> 39 * 40 * $FreeBSD: src/sys/netgraph/ng_tty.c,v 1.7.2.3 2002/02/13 00:43:12 dillon Exp $ 41 * $Whistle: ng_tty.c,v 1.21 1999/11/01 09:24:52 julian Exp $ 42 */ 43 44 /* 45 * This file implements a terminal line discipline that is also a 46 * netgraph node. Installing this line discipline on a terminal device 47 * instantiates a new netgraph node of this type, which allows access 48 * to the device via the "hook" hook of the node. 49 * 50 * Once the line discipline is installed, you can find out the name 51 * of the corresponding netgraph node via a NGIOCGINFO ioctl(). 52 * 53 * Incoming characters are delievered to the hook one at a time, each 54 * in its own mbuf. You may optionally define a ``hotchar,'' which causes 55 * incoming characters to be buffered up until either the hotchar is 56 * seen or the mbuf is full (MHLEN bytes). Then all buffered characters 57 * are immediately delivered. 58 * 59 * NOTE: This node operates at spltty(). 60 */ 61 62 #include <sys/param.h> 63 #include <sys/systm.h> 64 #include <sys/kernel.h> 65 #include <sys/conf.h> 66 #include <sys/proc.h> 67 #include <sys/priv.h> 68 #include <sys/mbuf.h> 69 #include <sys/malloc.h> 70 #include <sys/fcntl.h> 71 #include <sys/tty.h> 72 #include <sys/ttycom.h> 73 #include <sys/syslog.h> 74 #include <sys/errno.h> 75 #include <sys/thread2.h> 76 77 #include <netgraph/ng_message.h> 78 #include <netgraph/netgraph.h> 79 #include "ng_tty.h" 80 81 /* Misc defs */ 82 #define MAX_MBUFQ 3 /* Max number of queued mbufs */ 83 #define NGT_HIWATER 400 /* High water mark on output */ 84 85 /* Per-node private info */ 86 struct ngt_sc { 87 struct tty *tp; /* Terminal device */ 88 node_p node; /* Netgraph node */ 89 hook_p hook; /* Netgraph hook */ 90 struct mbuf *m; /* Incoming data buffer */ 91 struct mbuf *qhead, **qtail; /* Queue of outgoing mbuf's */ 92 short qlen; /* Length of queue */ 93 short hotchar; /* Hotchar, or -1 if none */ 94 u_int flags; /* Flags */ 95 struct callout ctimeout; /* See man timeout(9) */ 96 }; 97 typedef struct ngt_sc *sc_p; 98 99 /* Flags */ 100 #define FLG_TIMEOUT 0x0001 /* A timeout is pending */ 101 #define FLG_DEBUG 0x0002 102 103 /* Debugging */ 104 #ifdef INVARIANTS 105 #define QUEUECHECK(sc) \ 106 do { \ 107 struct mbuf **mp; \ 108 int k; \ 109 \ 110 for (k = 0, mp = &sc->qhead; \ 111 k <= MAX_MBUFQ && *mp; \ 112 k++, mp = &(*mp)->m_nextpkt); \ 113 if (k != sc->qlen || k > MAX_MBUFQ || *mp || mp != sc->qtail) \ 114 panic("%s: queue", __func__); \ 115 } while (0) 116 #else 117 #define QUEUECHECK(sc) do {} while (0) 118 #endif 119 120 /* Line discipline methods */ 121 static int ngt_open(cdev_t dev, struct tty *tp); 122 static int ngt_close(struct tty *tp, int flag); 123 static int ngt_read(struct tty *tp, struct uio *uio, int flag); 124 static int ngt_write(struct tty *tp, struct uio *uio, int flag); 125 static int ngt_tioctl(struct tty *tp, 126 u_long cmd, caddr_t data, int flag, struct ucred *cred); 127 static int ngt_input(int c, struct tty *tp); 128 static int ngt_start(struct tty *tp); 129 130 /* Netgraph methods */ 131 static ng_constructor_t ngt_constructor; 132 static ng_rcvmsg_t ngt_rcvmsg; 133 static ng_shutdown_t ngt_shutdown; 134 static ng_newhook_t ngt_newhook; 135 static ng_rcvdata_t ngt_rcvdata; 136 static ng_disconnect_t ngt_disconnect; 137 static int ngt_mod_event(module_t mod, int event, void *data); 138 139 /* Other stuff */ 140 static void ngt_timeout(void *arg); 141 142 #define ERROUT(x) do { error = (x); goto done; } while (0) 143 144 /* Line discipline descriptor */ 145 static struct linesw ngt_disc = { 146 ngt_open, 147 ngt_close, 148 ngt_read, 149 ngt_write, 150 ngt_tioctl, 151 ngt_input, 152 ngt_start, 153 ttymodem, 154 NG_TTY_DFL_HOTCHAR /* XXX can't change this in serial driver */ 155 }; 156 157 /* Netgraph node type descriptor */ 158 static struct ng_type typestruct = { 159 NG_VERSION, 160 NG_TTY_NODE_TYPE, 161 ngt_mod_event, 162 ngt_constructor, 163 ngt_rcvmsg, 164 ngt_shutdown, 165 ngt_newhook, 166 NULL, 167 NULL, 168 ngt_rcvdata, 169 ngt_rcvdata, 170 ngt_disconnect, 171 NULL 172 }; 173 NETGRAPH_INIT(tty, &typestruct); 174 175 static int ngt_unit; 176 static int ngt_nodeop_ok; /* OK to create/remove node */ 177 static int ngt_ldisc; 178 179 /****************************************************************** 180 LINE DISCIPLINE METHODS 181 ******************************************************************/ 182 183 /* 184 * Set our line discipline on the tty. 185 * Called from device open routine or ttioctl() at >= splsofttty() 186 */ 187 static int 188 ngt_open(cdev_t dev, struct tty *tp) 189 { 190 struct thread *td = curthread; /* XXX */ 191 char name[sizeof(NG_TTY_NODE_TYPE) + 8]; 192 sc_p sc; 193 int error; 194 195 /* Super-user only */ 196 if ((error = priv_check(td, PRIV_ROOT))) 197 return (error); 198 lwkt_gettoken(&tp->t_token); 199 200 /* Already installed? */ 201 if (tp->t_line == NETGRAPHDISC) { 202 sc = (sc_p) tp->t_sc; 203 if (sc != NULL && sc->tp == tp) 204 goto done; 205 } 206 207 /* Initialize private struct */ 208 sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO); 209 sc->tp = tp; 210 sc->hotchar = NG_TTY_DFL_HOTCHAR; 211 sc->qtail = &sc->qhead; 212 QUEUECHECK(sc); 213 callout_init_mp(&sc->ctimeout); 214 215 /* Setup netgraph node */ 216 ngt_nodeop_ok = 1; 217 error = ng_make_node_common(&typestruct, &sc->node); 218 ngt_nodeop_ok = 0; 219 if (error) { 220 kfree(sc, M_NETGRAPH); 221 goto done; 222 } 223 ksnprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit++); 224 225 /* Set back pointers */ 226 sc->node->private = sc; 227 tp->t_sc = (caddr_t) sc; 228 229 /* Assign node its name */ 230 if ((error = ng_name_node(sc->node, name))) { 231 log(LOG_ERR, "%s: node name exists?\n", name); 232 ngt_nodeop_ok = 1; 233 ng_rmnode(sc->node); 234 ngt_nodeop_ok = 0; 235 goto done; 236 } 237 238 /* 239 * Pre-allocate cblocks to the an appropriate amount. 240 * I'm not sure what is appropriate. 241 */ 242 ttyflush(tp, FREAD | FWRITE); 243 clist_alloc_cblocks(&tp->t_canq, 0, 0); 244 clist_alloc_cblocks(&tp->t_rawq, 0, 0); 245 clist_alloc_cblocks(&tp->t_outq, 246 MLEN + NGT_HIWATER, MLEN + NGT_HIWATER); 247 248 done: 249 /* Done */ 250 lwkt_reltoken(&tp->t_token); 251 return (error); 252 } 253 254 /* 255 * Line specific close routine, called from device close routine 256 * and from ttioctl at >= splsofttty(). This causes the node to 257 * be destroyed as well. 258 */ 259 static int 260 ngt_close(struct tty *tp, int flag) 261 { 262 const sc_p sc = (sc_p) tp->t_sc; 263 264 lwkt_gettoken(&tp->t_token); 265 ttyflush(tp, FREAD | FWRITE); 266 clist_free_cblocks(&tp->t_outq); 267 tp->t_line = 0; 268 if (sc != NULL) { 269 if (sc->flags & FLG_TIMEOUT) { 270 callout_stop(&sc->ctimeout); 271 sc->flags &= ~FLG_TIMEOUT; 272 } 273 ngt_nodeop_ok = 1; 274 ng_rmnode(sc->node); 275 ngt_nodeop_ok = 0; 276 tp->t_sc = NULL; 277 } 278 lwkt_reltoken(&tp->t_token); 279 280 return (0); 281 } 282 283 /* 284 * Once the device has been turned into a node, we don't allow reading. 285 */ 286 static int 287 ngt_read(struct tty *tp, struct uio *uio, int flag) 288 { 289 return (EIO); 290 } 291 292 /* 293 * Once the device has been turned into a node, we don't allow writing. 294 */ 295 static int 296 ngt_write(struct tty *tp, struct uio *uio, int flag) 297 { 298 return (EIO); 299 } 300 301 /* 302 * We implement the NGIOCGINFO ioctl() defined in ng_message.h. 303 */ 304 static int 305 ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred) 306 { 307 const sc_p sc = (sc_p) tp->t_sc; 308 int error = 0; 309 310 lwkt_gettoken(&tp->t_token); 311 312 switch (cmd) { 313 case NGIOCGINFO: 314 { 315 struct nodeinfo *const ni = (struct nodeinfo *) data; 316 const node_p node = sc->node; 317 318 bzero(ni, sizeof(*ni)); 319 if (node->name) 320 strncpy(ni->name, node->name, sizeof(ni->name) - 1); 321 strncpy(ni->type, node->type->name, sizeof(ni->type) - 1); 322 ni->id = (u_int32_t)(uintptr_t)node; 323 ni->hooks = node->numhooks; 324 break; 325 } 326 default: 327 ERROUT(ENOIOCTL); 328 } 329 done: 330 lwkt_reltoken(&tp->t_token); 331 332 return (error); 333 } 334 335 /* 336 * Receive data coming from the device. We get one character at 337 * a time, which is kindof silly. 338 * Only guaranteed to be at splsofttty() or spltty(). 339 */ 340 static int 341 ngt_input(int c, struct tty *tp) 342 { 343 const sc_p sc = (sc_p) tp->t_sc; 344 const node_p node = sc ? sc->node : NULL; 345 struct mbuf *m; 346 int error = 0; 347 348 lwkt_gettoken(&tp->t_token); 349 if (!sc || tp != sc->tp) { 350 lwkt_reltoken(&tp->t_token); 351 return (0); 352 } 353 if (!sc->hook) 354 ERROUT(0); 355 356 /* Check for error conditions */ 357 if ((tp->t_state & TS_CONNECTED) == 0) { 358 if (sc->flags & FLG_DEBUG) 359 log(LOG_DEBUG, "%s: no carrier\n", node->name); 360 ERROUT(0); 361 } 362 if (c & TTY_ERRORMASK) { 363 /* framing error or overrun on this char */ 364 if (sc->flags & FLG_DEBUG) 365 log(LOG_DEBUG, "%s: line error %x\n", 366 node->name, c & TTY_ERRORMASK); 367 ERROUT(0); 368 } 369 c &= TTY_CHARMASK; 370 371 /* Get a new header mbuf if we need one */ 372 if (!(m = sc->m)) { 373 MGETHDR(m, M_NOWAIT, MT_DATA); 374 if (!m) { 375 if (sc->flags & FLG_DEBUG) 376 log(LOG_ERR, 377 "%s: can't get mbuf\n", node->name); 378 ERROUT(ENOBUFS); 379 } 380 m->m_len = m->m_pkthdr.len = 0; 381 m->m_pkthdr.rcvif = NULL; 382 sc->m = m; 383 } 384 385 /* Add char to mbuf */ 386 *mtod(m, u_char *) = c; 387 m->m_data++; 388 m->m_len++; 389 m->m_pkthdr.len++; 390 391 /* Ship off mbuf if it's time */ 392 if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) { 393 m->m_data = m->m_pktdat; 394 error = ng_queue_data(sc->hook, m, NULL); 395 sc->m = NULL; 396 } 397 done: 398 lwkt_reltoken(&tp->t_token); 399 400 return (error); 401 } 402 403 /* 404 * This is called when the device driver is ready for more output. 405 * Called from tty system at splsofttty() or spltty(). 406 * Also call from ngt_rcv_data() when a new mbuf is available for output. 407 */ 408 static int 409 ngt_start(struct tty *tp) 410 { 411 const sc_p sc = (sc_p) tp->t_sc; 412 413 lwkt_gettoken(&tp->t_token); 414 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */ 415 struct mbuf *m = sc->qhead; 416 417 /* Remove first mbuf from queue */ 418 if (!m) 419 break; 420 if ((sc->qhead = m->m_nextpkt) == NULL) 421 sc->qtail = &sc->qhead; 422 sc->qlen--; 423 QUEUECHECK(sc); 424 425 /* Send as much of it as possible */ 426 while (m) { 427 int sent; 428 429 sent = m->m_len 430 - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq); 431 m->m_data += sent; 432 m->m_len -= sent; 433 if (m->m_len > 0) 434 break; /* device can't take no more */ 435 m = m_free(m); 436 } 437 438 /* Put remainder of mbuf chain (if any) back on queue */ 439 if (m) { 440 m->m_nextpkt = sc->qhead; 441 sc->qhead = m; 442 if (sc->qtail == &sc->qhead) 443 sc->qtail = &m->m_nextpkt; 444 sc->qlen++; 445 QUEUECHECK(sc); 446 break; 447 } 448 } 449 450 /* Call output process whether or not there is any output. We are 451 * being called in lieu of ttstart and must do what it would. */ 452 if (tp->t_oproc != NULL) 453 (*tp->t_oproc) (tp); 454 455 /* This timeout is needed for operation on a pseudo-tty, because the 456 * pty code doesn't call pppstart after it has drained the t_outq. */ 457 if (sc->qhead && (sc->flags & FLG_TIMEOUT) == 0) { 458 callout_reset(&sc->ctimeout, 1, ngt_timeout, sc); 459 sc->flags |= FLG_TIMEOUT; 460 } 461 lwkt_reltoken(&tp->t_token); 462 463 return (0); 464 } 465 466 /* 467 * We still have data to output to the device, so try sending more. 468 */ 469 static void 470 ngt_timeout(void *arg) 471 { 472 const sc_p sc = (sc_p) arg; 473 struct tty *tp = sc->tp; 474 475 lwkt_gettoken(&tp->t_token); 476 sc->flags &= ~FLG_TIMEOUT; 477 ngt_start(tp); 478 lwkt_reltoken(&tp->t_token); 479 } 480 481 /****************************************************************** 482 NETGRAPH NODE METHODS 483 ******************************************************************/ 484 485 /* 486 * Initialize a new node of this type. 487 * 488 * We only allow nodes to be created as a result of setting 489 * the line discipline on a tty, so always return an error if not. 490 */ 491 static int 492 ngt_constructor(node_p *nodep) 493 { 494 if (!ngt_nodeop_ok) 495 return (EOPNOTSUPP); 496 return (ng_make_node_common(&typestruct, nodep)); 497 } 498 499 /* 500 * Add a new hook. There can only be one. 501 */ 502 static int 503 ngt_newhook(node_p node, hook_p hook, const char *name) 504 { 505 const sc_p sc = node->private; 506 struct tty *tp = sc->tp; 507 int error = 0; 508 509 if (strcmp(name, NG_TTY_HOOK)) 510 return (EINVAL); 511 lwkt_gettoken(&tp->t_token); 512 if (sc->hook) 513 ERROUT(EISCONN); 514 sc->hook = hook; 515 done: 516 lwkt_reltoken(&tp->t_token); 517 518 return (error); 519 } 520 521 /* 522 * Disconnect the hook 523 */ 524 static int 525 ngt_disconnect(hook_p hook) 526 { 527 const sc_p sc = hook->node->private; 528 struct tty *tp = sc->tp; 529 530 lwkt_gettoken(&tp->t_token); 531 if (hook != sc->hook) 532 panic(__func__); 533 sc->hook = NULL; 534 m_freem(sc->m); 535 sc->m = NULL; 536 lwkt_reltoken(&tp->t_token); 537 538 return (0); 539 } 540 541 /* 542 * Remove this node. The does the netgraph portion of the shutdown. 543 * This should only be called indirectly from ngt_close(). 544 */ 545 static int 546 ngt_shutdown(node_p node) 547 { 548 const sc_p sc = node->private; 549 struct tty *tp = sc->tp; 550 551 if (!ngt_nodeop_ok) 552 return (EOPNOTSUPP); 553 lwkt_gettoken(&tp->t_token); 554 ng_unname(node); 555 ng_cutlinks(node); 556 node->private = NULL; 557 ng_unref(sc->node); 558 m_freem(sc->qhead); 559 m_freem(sc->m); 560 bzero(sc, sizeof(*sc)); 561 kfree(sc, M_NETGRAPH); 562 lwkt_reltoken(&tp->t_token); 563 564 return (0); 565 } 566 567 /* 568 * Receive incoming data from netgraph system. Put it on our 569 * output queue and start output if necessary. 570 */ 571 static int 572 ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta) 573 { 574 const sc_p sc = hook->node->private; 575 struct tty *tp = sc->tp; 576 int error = 0; 577 578 if (hook != sc->hook) 579 panic(__func__); 580 NG_FREE_META(meta); 581 lwkt_gettoken(&tp->t_token); 582 if (sc->qlen >= MAX_MBUFQ) 583 ERROUT(ENOBUFS); 584 m->m_nextpkt = NULL; 585 *sc->qtail = m; 586 sc->qtail = &m->m_nextpkt; 587 sc->qlen++; 588 QUEUECHECK(sc); 589 m = NULL; 590 if (sc->qlen == 1) 591 ngt_start(sc->tp); 592 done: 593 lwkt_reltoken(&tp->t_token); 594 if (m) 595 m_freem(m); 596 return (error); 597 } 598 599 /* 600 * Receive control message 601 */ 602 static int 603 ngt_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr, 604 struct ng_mesg **rptr) 605 { 606 const sc_p sc = (sc_p) node->private; 607 struct tty *tp = sc->tp; 608 struct ng_mesg *resp = NULL; 609 int error = 0; 610 611 lwkt_gettoken(&tp->t_token); 612 switch (msg->header.typecookie) { 613 case NGM_TTY_COOKIE: 614 switch (msg->header.cmd) { 615 case NGM_TTY_SET_HOTCHAR: 616 { 617 int hotchar; 618 619 if (msg->header.arglen != sizeof(int)) 620 ERROUT(EINVAL); 621 hotchar = *((int *) msg->data); 622 if (hotchar != (u_char) hotchar && hotchar != -1) 623 ERROUT(EINVAL); 624 sc->hotchar = hotchar; /* race condition is OK */ 625 break; 626 } 627 case NGM_TTY_GET_HOTCHAR: 628 NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT); 629 if (!resp) 630 ERROUT(ENOMEM); 631 /* Race condition here is OK */ 632 *((int *) resp->data) = sc->hotchar; 633 break; 634 default: 635 ERROUT(EINVAL); 636 } 637 break; 638 default: 639 ERROUT(EINVAL); 640 } 641 if (rptr) 642 *rptr = resp; 643 else if (resp) 644 kfree(resp, M_NETGRAPH); 645 646 done: 647 kfree(msg, M_NETGRAPH); 648 lwkt_reltoken(&tp->t_token); 649 650 return (error); 651 } 652 653 /****************************************************************** 654 INITIALIZATION 655 ******************************************************************/ 656 657 /* 658 * Handle loading and unloading for this node type 659 */ 660 static int 661 ngt_mod_event(module_t mod, int event, void *data) 662 { 663 /* struct ng_type *const type = data;*/ 664 int error = 0; 665 666 switch (event) { 667 case MOD_LOAD: 668 /* Register line discipline */ 669 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) { 670 log(LOG_ERR, "%s: can't register line discipline", 671 __func__); 672 return (EIO); 673 } 674 break; 675 676 case MOD_UNLOAD: 677 678 /* Unregister line discipline */ 679 ldisc_deregister(ngt_ldisc); 680 break; 681 682 default: 683 error = EOPNOTSUPP; 684 break; 685 } 686 return (error); 687 } 688