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.37 2006/11/06 13:42:03 rwatson 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 60 #include <sys/param.h> 61 #include <sys/systm.h> 62 #include <sys/conf.h> 63 #include <sys/errno.h> 64 #include <sys/fcntl.h> 65 #include <sys/kernel.h> 66 #include <sys/malloc.h> 67 #include <sys/mbuf.h> 68 #include <sys/priv.h> 69 #include <sys/socket.h> 70 #include <sys/syslog.h> 71 #include <sys/tty.h> 72 #include <sys/ttycom.h> 73 74 #include <net/if.h> 75 #include <net/if_var.h> 76 77 #include <netgraph7/netgraph.h> 78 #include <netgraph7/ng_message.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 ifqueue outq; /* Queue of outgoing data */ 91 struct mbuf *m; /* Incoming data buffer */ 92 short hotchar; /* Hotchar, or -1 if none */ 93 u_int flags; /* Flags */ 94 struct callout chand; /* See man timeout(9) */ 95 }; 96 typedef struct ngt_sc *sc_p; 97 98 /* Flags */ 99 #define FLG_DEBUG 0x0002 100 #define FLG_DIE 0x0004 101 102 /* Line discipline methods */ 103 static int ngt_open(struct cdev *dev, struct tty *tp); 104 static int ngt_close(struct tty *tp, int flag); 105 static int ngt_read(struct tty *tp, struct uio *uio, int flag); 106 static int ngt_write(struct tty *tp, struct uio *uio, int flag); 107 static int ngt_tioctl(struct tty *tp, 108 u_long cmd, caddr_t data, int flag, struct ucred *cred); 109 static int ngt_input(int c, struct tty *tp); 110 static int ngt_start(struct tty *tp); 111 112 /* Netgraph methods */ 113 static ng_constructor_t ngt_constructor; 114 static ng_rcvmsg_t ngt_rcvmsg; 115 static ng_shutdown_t ngt_shutdown; 116 static ng_newhook_t ngt_newhook; 117 static ng_connect_t ngt_connect; 118 static ng_rcvdata_t ngt_rcvdata; 119 static ng_disconnect_t ngt_disconnect; 120 static int ngt_mod_event(module_t mod, int event, void *data); 121 122 /* Other stuff */ 123 static void ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2); 124 125 #define ERROUT(x) do { error = (x); goto done; } while (0) 126 127 /* Line discipline descriptor */ 128 static struct linesw ngt_disc = { 129 .l_open = ngt_open, 130 .l_close = ngt_close, 131 .l_read = ngt_read, 132 .l_write = ngt_write, 133 .l_ioctl = ngt_tioctl, 134 .l_rint = ngt_input, 135 .l_start = ngt_start, 136 .l_modem = ttymodem, 137 }; 138 139 /* Netgraph node type descriptor */ 140 static struct ng_type typestruct = { 141 .version = NG_ABI_VERSION, 142 .name = NG_TTY_NODE_TYPE, 143 .mod_event = ngt_mod_event, 144 .constructor = ngt_constructor, 145 .rcvmsg = ngt_rcvmsg, 146 .shutdown = ngt_shutdown, 147 .newhook = ngt_newhook, 148 .connect = ngt_connect, 149 .rcvdata = ngt_rcvdata, 150 .disconnect = ngt_disconnect, 151 }; 152 NETGRAPH_INIT(tty, &typestruct); 153 154 /* 155 * Locking: 156 * 157 * - node private data and tp->t_lsc is protected by mutex in struct 158 * ifqueue, locking is done using IF_XXX() macros. 159 * - in all tty methods we should acquire node ifqueue mutex, when accessing 160 * private data. 161 * - in _rcvdata() we should use locked versions of IF_{EN,DE}QUEUE() since 162 * we may have multiple _rcvdata() threads. 163 * - when calling any of tty methods from netgraph methods, we should 164 * acquire tty locking (now Giant). 165 * 166 * - ngt_unit is incremented atomically. 167 */ 168 169 static int ngt_unit; 170 static int ngt_ldisc; 171 172 /****************************************************************** 173 LINE DISCIPLINE METHODS 174 ******************************************************************/ 175 176 /* 177 * Set our line discipline on the tty. 178 * Called from device open routine or ttioctl() 179 */ 180 static int 181 ngt_open(struct cdev *dev, struct tty *tp) 182 { 183 struct thread *const td = curthread; /* XXX */ 184 char name[sizeof(NG_TTY_NODE_TYPE) + 8]; 185 sc_p sc; 186 int error; 187 188 /* Super-user only */ 189 error = priv_check(td, PRIV_NETGRAPH_TTY); 190 if (error) 191 return (error); 192 193 /* Initialize private struct */ 194 sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO); 195 196 lwkt_gettoken(&tp->t_token); 197 sc->tp = tp; 198 sc->hotchar = NG_TTY_DFL_HOTCHAR; 199 sc->outq.ifq_maxlen = MAX_MBUFQ; 200 201 /* Setup netgraph node */ 202 error = ng_make_node_common(&typestruct, &sc->node); 203 if (error) { 204 kfree(sc, M_NETGRAPH); 205 lwkt_reltoken(&tp->t_token); 206 return (error); 207 } 208 209 atomic_add_int(&ngt_unit, 1); 210 ksnprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit); 211 212 /* Assign node its name */ 213 if ((error = ng_name_node(sc->node, name))) { 214 sc->flags |= FLG_DIE; 215 NG_NODE_UNREF(sc->node); 216 log(LOG_ERR, "%s: node name exists?\n", name); 217 lwkt_reltoken(&tp->t_token); 218 return (error); 219 } 220 221 /* Set back pointers */ 222 NG_NODE_SET_PRIVATE(sc->node, sc); 223 tp->t_sc = sc; 224 225 callout_init_mp(&sc->chand); 226 227 /* 228 * Pre-allocate cblocks to the an appropriate amount. 229 * I'm not sure what is appropriate. 230 */ 231 ttyflush(tp, FREAD | FWRITE); 232 clist_alloc_cblocks(&tp->t_canq, 0); 233 clist_alloc_cblocks(&tp->t_rawq, 0); 234 clist_alloc_cblocks(&tp->t_outq, MLEN + NGT_HIWATER); 235 236 lwkt_reltoken(&tp->t_token); 237 return (error); 238 } 239 240 /* 241 * Line specific close routine, called from device close routine 242 * and from ttioctl. This causes the node to be destroyed as well. 243 */ 244 static int 245 ngt_close(struct tty *tp, int flag) 246 { 247 const sc_p sc = (sc_p) tp->t_sc; 248 249 lwkt_gettoken(&tp->t_token); 250 ttyflush(tp, FREAD | FWRITE); 251 clist_free_cblocks(&tp->t_outq); 252 if (sc != NULL) { 253 if (callout_pending(&sc->chand)) 254 ng_uncallout(&sc->chand, sc->node); 255 tp->t_sc = NULL; 256 sc->flags |= FLG_DIE; 257 ng_rmnode_self(sc->node); 258 } 259 lwkt_reltoken(&tp->t_token); 260 return (0); 261 } 262 263 /* 264 * Once the device has been turned into a node, we don't allow reading. 265 */ 266 static int 267 ngt_read(struct tty *tp, struct uio *uio, int flag) 268 { 269 return (EIO); 270 } 271 272 /* 273 * Once the device has been turned into a node, we don't allow writing. 274 */ 275 static int 276 ngt_write(struct tty *tp, struct uio *uio, int flag) 277 { 278 return (EIO); 279 } 280 281 /* 282 * We implement the NGIOCGINFO ioctl() defined in ng_message.h. 283 */ 284 static int 285 ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred) 286 { 287 const sc_p sc = (sc_p) tp->t_sc; 288 289 if (sc == NULL) { 290 /* No node attached */ 291 return (0); 292 } 293 294 lwkt_gettoken(&tp->t_token); 295 switch (cmd) { 296 case NGIOCGINFO: 297 { 298 struct nodeinfo *const ni = (struct nodeinfo *) data; 299 const node_p node = sc->node; 300 301 bzero(ni, sizeof(*ni)); 302 if (NG_NODE_HAS_NAME(node)) 303 strncpy(ni->name, NG_NODE_NAME(node), sizeof(ni->name) - 1); 304 strncpy(ni->type, node->nd_type->name, sizeof(ni->type) - 1); 305 ni->id = (u_int32_t) ng_node2ID(node); 306 ni->hooks = NG_NODE_NUMHOOKS(node); 307 break; 308 } 309 default: 310 lwkt_reltoken(&tp->t_token); 311 return (ENOIOCTL); 312 } 313 314 lwkt_reltoken(&tp->t_token); 315 return (0); 316 } 317 318 /* 319 * Receive data coming from the device. We get one character at 320 * a time, which is kindof silly. 321 * 322 * Full locking of softc is not required, since we are the only 323 * user of sc->m. 324 */ 325 static int 326 ngt_input(int c, struct tty *tp) 327 { 328 sc_p sc; 329 node_p node; 330 struct mbuf *m; 331 int error = 0; 332 333 lwkt_gettoken(&tp->t_token); 334 sc = (sc_p) tp->t_sc; 335 if (sc == NULL) { 336 /* No node attached */ 337 lwkt_reltoken(&tp->t_token); 338 return (0); 339 } 340 341 node = sc->node; 342 343 if (tp != sc->tp) 344 panic("ngt_input"); 345 346 /* Check for error conditions */ 347 if ((tp->t_state & TS_CONNECTED) == 0) { 348 if (sc->flags & FLG_DEBUG) 349 log(LOG_DEBUG, "%s: no carrier\n", NG_NODE_NAME(node)); 350 lwkt_reltoken(&tp->t_token); 351 return (0); 352 } 353 if (c & TTY_ERRORMASK) { 354 /* framing error or overrun on this char */ 355 if (sc->flags & FLG_DEBUG) 356 log(LOG_DEBUG, "%s: line error %x\n", 357 NG_NODE_NAME(node), c & TTY_ERRORMASK); 358 lwkt_reltoken(&tp->t_token); 359 return (0); 360 } 361 c &= TTY_CHARMASK; 362 363 /* Get a new header mbuf if we need one */ 364 if (!(m = sc->m)) { 365 MGETHDR(m, M_NOWAIT, MT_DATA); 366 if (!m) { 367 if (sc->flags & FLG_DEBUG) 368 log(LOG_ERR, 369 "%s: can't get mbuf\n", NG_NODE_NAME(node)); 370 lwkt_reltoken(&tp->t_token); 371 return (ENOBUFS); 372 } 373 m->m_len = m->m_pkthdr.len = 0; 374 m->m_pkthdr.rcvif = NULL; 375 sc->m = m; 376 } 377 378 /* Add char to mbuf */ 379 *mtod(m, u_char *) = c; 380 m->m_data++; 381 m->m_len++; 382 m->m_pkthdr.len++; 383 384 /* Ship off mbuf if it's time */ 385 if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) { 386 m->m_data = m->m_pktdat; 387 sc->m = NULL; 388 389 /* 390 * We have built our mbuf without checking that we actually 391 * have a hook to send it. This was done to avoid 392 * acquiring mutex on each character. Check now. 393 * 394 */ 395 396 if (sc->hook == NULL) { 397 m_freem(m); 398 lwkt_reltoken(&tp->t_token); 399 return (0); /* XXX: original behavior */ 400 } 401 NG_SEND_DATA_ONLY(error, sc->hook, m); /* Will queue */ 402 } 403 404 lwkt_reltoken(&tp->t_token); 405 return (error); 406 } 407 408 /* 409 * This is called when the device driver is ready for more output. 410 * Also called from ngt_rcv_data() when a new mbuf is available for output. 411 */ 412 static int 413 ngt_start(struct tty *tp) 414 { 415 const sc_p sc = (sc_p) tp->t_sc; 416 417 lwkt_gettoken(&tp->t_token); 418 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */ 419 struct mbuf *m; 420 421 /* Remove first mbuf from queue */ 422 IF_DEQUEUE(&sc->outq, m); 423 if (m == NULL) 424 break; 425 426 /* Send as much of it as possible */ 427 while (m != NULL) { 428 int sent; 429 430 sent = m->m_len - clist_btoq(mtod(m, u_char *), 431 m->m_len, &tp->t_outq); 432 m->m_data += sent; 433 m->m_len -= sent; 434 if (m->m_len > 0) 435 break; /* device can't take no more */ 436 m = m_free(m); 437 } 438 439 /* Put remainder of mbuf chain (if any) back on queue */ 440 if (m != NULL) { 441 IF_PREPEND(&sc->outq, m); 442 break; 443 } 444 } 445 446 /* Call output process whether or not there is any output. We are 447 * being called in lieu of ttstart and must do what it would. */ 448 if (tp->t_oproc != NULL) 449 (*tp->t_oproc) (tp); 450 451 /* This timeout is needed for operation on a pseudo-tty, because the 452 * pty code doesn't call pppstart after it has drained the t_outq. */ 453 /* XXX: outq not locked */ 454 if (!IF_QEMPTY(&sc->outq) && !callout_pending(&sc->chand)) 455 ng_callout(&sc->chand, sc->node, NULL, 1, ngt_timeout, NULL, 0); 456 457 lwkt_reltoken(&tp->t_token); 458 return (0); 459 } 460 461 /* 462 * We still have data to output to the device, so try sending more. 463 */ 464 static void 465 ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2) 466 { 467 const sc_p sc = NG_NODE_PRIVATE(node); 468 struct tty *tp = sc->tp; 469 470 lwkt_gettoken(&tp->t_token); 471 ngt_start(sc->tp); 472 lwkt_reltoken(&tp->t_token); 473 } 474 475 /****************************************************************** 476 NETGRAPH NODE METHODS 477 ******************************************************************/ 478 479 /* 480 * Initialize a new node of this type. 481 * 482 * We only allow nodes to be created as a result of setting 483 * the line discipline on a tty, so always return an error if not. 484 */ 485 static int 486 ngt_constructor(node_p node) 487 { 488 return (EOPNOTSUPP); 489 } 490 491 /* 492 * Add a new hook. There can only be one. 493 */ 494 static int 495 ngt_newhook(node_p node, hook_p hook, const char *name) 496 { 497 const sc_p sc = NG_NODE_PRIVATE(node); 498 struct tty *tp = sc->tp; 499 500 if (strcmp(name, NG_TTY_HOOK)) 501 return (EINVAL); 502 lwkt_gettoken(&tp->t_token); 503 if (sc->hook) { 504 lwkt_reltoken(&tp->t_token); 505 return (EISCONN); 506 } 507 sc->hook = hook; 508 lwkt_reltoken(&tp->t_token); 509 return (0); 510 } 511 512 /* 513 * Set the hook into queueing mode (for outgoing packets), 514 * so that we wont deliver mbuf thru the whole graph holding 515 * tty locks. 516 */ 517 static int 518 ngt_connect(hook_p hook) 519 { 520 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook)); 521 /* 522 * XXX: While ngt_start() is Giant-locked, queue incoming 523 * packets, too. Otherwise we acquire Giant holding some 524 * IP stack locks, e.g. divinp, and this makes WITNESS scream. 525 */ 526 NG_HOOK_FORCE_QUEUE(hook); 527 return (0); 528 } 529 530 /* 531 * Disconnect the hook 532 */ 533 static int 534 ngt_disconnect(hook_p hook) 535 { 536 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 537 struct tty *tp = sc->tp; 538 539 lwkt_gettoken(&tp->t_token); 540 if (hook != sc->hook) 541 panic(__func__); 542 sc->hook = NULL; 543 lwkt_reltoken(&tp->t_token); 544 return (0); 545 } 546 547 /* 548 * Remove this node. The does the netgraph portion of the shutdown. 549 * This should only be called indirectly from ngt_close(). 550 * 551 * tp->t_lsc is already NULL, so we should be protected from 552 * tty calls now. 553 */ 554 static int 555 ngt_shutdown(node_p node) 556 { 557 const sc_p sc = NG_NODE_PRIVATE(node); 558 struct tty *tp = sc->tp; 559 560 lwkt_gettoken(&tp->t_token); 561 if (!(sc->flags & FLG_DIE)) { 562 lwkt_reltoken(&tp->t_token); 563 return (EOPNOTSUPP); 564 } 565 566 /* Free resources */ 567 IF_DRAIN(&sc->outq); 568 m_freem(sc->m); 569 kfree(sc, M_NETGRAPH); 570 lwkt_reltoken(&tp->t_token); 571 return (0); 572 } 573 574 /* 575 * Receive incoming data from netgraph system. Put it on our 576 * output queue and start output if necessary. 577 */ 578 static int 579 ngt_rcvdata(hook_p hook, item_p item) 580 { 581 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 582 struct tty *tp = sc->tp; 583 struct mbuf *m; 584 int qlen; 585 586 if (hook != sc->hook) 587 panic(__func__); 588 589 NGI_GET_M(item, m); 590 NG_FREE_ITEM(item); 591 592 if (IF_QFULL(&sc->outq)) { 593 IF_DROP(&sc->outq); 594 NG_FREE_M(m); 595 return (ENOBUFS); 596 } 597 598 IF_ENQUEUE(&sc->outq, m); 599 qlen = sc->outq.ifq_len; 600 601 /* 602 * If qlen > 1, then we should already have a scheduled callout. 603 */ 604 if (qlen == 1) { 605 lwkt_gettoken(&tp->t_token); 606 ngt_start(sc->tp); 607 lwkt_reltoken(&tp->t_token); 608 } 609 610 return (0); 611 } 612 613 /* 614 * Receive control message 615 */ 616 static int 617 ngt_rcvmsg(node_p node, item_p item, hook_p lasthook) 618 { 619 const sc_p sc = NG_NODE_PRIVATE(node); 620 struct ng_mesg *msg, *resp = NULL; 621 int error = 0; 622 623 NGI_GET_MSG(item, msg); 624 switch (msg->header.typecookie) { 625 case NGM_TTY_COOKIE: 626 switch (msg->header.cmd) { 627 case NGM_TTY_SET_HOTCHAR: 628 { 629 int hotchar; 630 631 if (msg->header.arglen != sizeof(int)) 632 ERROUT(EINVAL); 633 hotchar = *((int *) msg->data); 634 if (hotchar != (u_char) hotchar && hotchar != -1) 635 ERROUT(EINVAL); 636 sc->hotchar = hotchar; /* race condition is OK */ 637 break; 638 } 639 case NGM_TTY_GET_HOTCHAR: 640 NG_MKRESPONSE(resp, msg, sizeof(int), M_WAITOK | M_NULLOK); 641 if (!resp) 642 ERROUT(ENOMEM); 643 /* Race condition here is OK */ 644 *((int *) resp->data) = sc->hotchar; 645 break; 646 default: 647 ERROUT(EINVAL); 648 } 649 break; 650 default: 651 ERROUT(EINVAL); 652 } 653 done: 654 NG_RESPOND_MSG(error, node, item, resp); 655 NG_FREE_MSG(msg); 656 return (error); 657 } 658 659 /****************************************************************** 660 INITIALIZATION 661 ******************************************************************/ 662 663 /* 664 * Handle loading and unloading for this node type 665 */ 666 static int 667 ngt_mod_event(module_t mod, int event, void *data) 668 { 669 int error = 0; 670 671 switch (event) { 672 case MOD_LOAD: 673 /* Register line discipline */ 674 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) { 675 log(LOG_ERR, "%s: can't register line discipline", 676 __func__); 677 return (EIO); 678 } 679 break; 680 681 case MOD_UNLOAD: 682 683 /* Unregister line discipline */ 684 ldisc_deregister(ngt_ldisc); 685 break; 686 687 default: 688 error = EOPNOTSUPP; 689 break; 690 } 691 return (error); 692 } 693