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