ng_tty.c revision 70700
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 70700 2001-01-06 00:46:47Z julian $ 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#ifdef __i386__ /* fiddle with the spl locking */ 79#include <sys/bus.h> 80#include <machine/ipl.h> 81#endif 82 83/* Misc defs */ 84#define MAX_MBUFQ 3 /* Max number of queued mbufs */ 85#define NGT_HIWATER 400 /* High water mark on output */ 86 87/* Per-node private info */ 88struct ngt_sc { 89 struct tty *tp; /* Terminal device */ 90 node_p node; /* Netgraph node */ 91 hook_p hook; /* Netgraph hook */ 92 struct mbuf *m; /* Incoming data buffer */ 93 struct mbuf *qhead, **qtail; /* Queue of outgoing mbuf's */ 94 short qlen; /* Length of queue */ 95 short hotchar; /* Hotchar, or -1 if none */ 96 u_int flags; /* Flags */ 97 struct callout_handle chand; /* See man timeout(9) */ 98}; 99typedef struct ngt_sc *sc_p; 100 101/* Flags */ 102#define FLG_TIMEOUT 0x0001 /* A timeout is pending */ 103#define FLG_DEBUG 0x0002 104 105/* Debugging */ 106#ifdef INVARIANTS 107#define QUEUECHECK(sc) \ 108 do { \ 109 struct mbuf **mp; \ 110 int k; \ 111 \ 112 for (k = 0, mp = &sc->qhead; \ 113 k <= MAX_MBUFQ && *mp; \ 114 k++, mp = &(*mp)->m_nextpkt); \ 115 if (k != sc->qlen || k > MAX_MBUFQ || *mp || mp != sc->qtail) \ 116 panic(__FUNCTION__ ": queue"); \ 117 } while (0) 118#else 119#define QUEUECHECK(sc) do {} while (0) 120#endif 121 122/* Line discipline methods */ 123static int ngt_open(dev_t dev, struct tty *tp); 124static int ngt_close(struct tty *tp, int flag); 125static int ngt_read(struct tty *tp, struct uio *uio, int flag); 126static int ngt_write(struct tty *tp, struct uio *uio, int flag); 127static int ngt_tioctl(struct tty *tp, 128 u_long cmd, caddr_t data, int flag, struct proc *); 129static int ngt_input(int c, struct tty *tp); 130static int ngt_start(struct tty *tp); 131 132/* Netgraph methods */ 133static ng_constructor_t ngt_constructor; 134static ng_rcvmsg_t ngt_rcvmsg; 135static ng_shutdown_t ngt_shutdown; 136static ng_newhook_t ngt_newhook; 137static ng_connect_t ngt_connect; 138static ng_rcvdata_t ngt_rcvdata; 139static ng_disconnect_t ngt_disconnect; 140static int ngt_mod_event(module_t mod, int event, void *data); 141 142/* Other stuff */ 143static void ngt_timeout(void *arg); 144 145#define ERROUT(x) do { error = (x); goto done; } while (0) 146 147/* Line discipline descriptor */ 148static struct linesw ngt_disc = { 149 ngt_open, 150 ngt_close, 151 ngt_read, 152 ngt_write, 153 ngt_tioctl, 154 ngt_input, 155 ngt_start, 156 ttymodem, 157 NG_TTY_DFL_HOTCHAR /* XXX can't change this in serial driver */ 158}; 159 160/* Netgraph node type descriptor */ 161static struct ng_type typestruct = { 162 NG_ABI_VERSION, 163 NG_TTY_NODE_TYPE, 164 ngt_mod_event, 165 ngt_constructor, 166 ngt_rcvmsg, 167 ngt_shutdown, 168 ngt_newhook, 169 NULL, 170 ngt_connect, 171 ngt_rcvdata, 172 ngt_disconnect, 173 NULL 174}; 175NETGRAPH_INIT(tty, &typestruct); 176 177static int ngt_unit; 178static int ngt_nodeop_ok; /* OK to create/remove node */ 179static int ngt_ldisc; 180 181/****************************************************************** 182 LINE DISCIPLINE METHODS 183******************************************************************/ 184 185/* 186 * Set our line discipline on the tty. 187 * Called from device open routine or ttioctl() at >= splsofttty() 188 */ 189static int 190ngt_open(dev_t dev, struct tty *tp) 191{ 192 struct proc *const p = curproc; /* XXX */ 193 char name[sizeof(NG_TTY_NODE_TYPE) + 8]; 194 sc_p sc; 195 int s, error; 196 197 /* Super-user only */ 198 if ((error = suser(p))) 199 return (error); 200 s = splnet(); 201 (void) spltty(); /* XXX is this necessary? */ 202 203 /* Already installed? */ 204 if (tp->t_line == NETGRAPHDISC) { 205 sc = (sc_p) tp->t_sc; 206 if (sc != NULL && sc->tp == tp) 207 goto done; 208 } 209 210 /* Initialize private struct */ 211 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO); 212 if (sc == NULL) { 213 error = ENOMEM; 214 goto done; 215 } 216 sc->tp = tp; 217 sc->hotchar = NG_TTY_DFL_HOTCHAR; 218 sc->qtail = &sc->qhead; 219 QUEUECHECK(sc); 220 callout_handle_init(&sc->chand); 221 222 /* Setup netgraph node */ 223 ngt_nodeop_ok = 1; 224 error = ng_make_node_common(&typestruct, &sc->node); 225 ngt_nodeop_ok = 0; 226 if (error) { 227 FREE(sc, M_NETGRAPH); 228 goto done; 229 } 230 snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit++); 231 232 /* Assign node its name */ 233 if ((error = ng_name_node(sc->node, name))) { 234 log(LOG_ERR, "%s: node name exists?\n", name); 235 ngt_nodeop_ok = 1; 236 ng_unref(sc->node); 237 ngt_nodeop_ok = 0; 238 goto done; 239 } 240 241 /* Set back pointers */ 242 sc->node->private = sc; 243 tp->t_sc = (caddr_t) sc; 244 245 /* 246 * Pre-allocate cblocks to the an appropriate amount. 247 * I'm not sure what is appropriate. 248 */ 249 ttyflush(tp, FREAD | FWRITE); 250 clist_alloc_cblocks(&tp->t_canq, 0, 0); 251 clist_alloc_cblocks(&tp->t_rawq, 0, 0); 252 clist_alloc_cblocks(&tp->t_outq, 253 MLEN + NGT_HIWATER, MLEN + NGT_HIWATER); 254 255done: 256 /* Done */ 257 splx(s); 258 return (error); 259} 260 261/* 262 * Line specific close routine, called from device close routine 263 * and from ttioctl at >= splsofttty(). This causes the node to 264 * be destroyed as well. 265 */ 266static int 267ngt_close(struct tty *tp, int flag) 268{ 269 const sc_p sc = (sc_p) tp->t_sc; 270 int s; 271 272 s = spltty(); 273 ttyflush(tp, FREAD | FWRITE); 274 clist_free_cblocks(&tp->t_outq); 275 tp->t_line = 0; 276 if (sc != NULL) { 277 if (sc->flags & FLG_TIMEOUT) { 278 untimeout(ngt_timeout, sc, sc->chand); 279 sc->flags &= ~FLG_TIMEOUT; 280 } 281 ngt_nodeop_ok = 1; 282 ng_rmnode_self(sc->node); 283 ngt_nodeop_ok = 0; 284 tp->t_sc = NULL; 285 } 286 splx(s); 287 return (0); 288} 289 290/* 291 * Once the device has been turned into a node, we don't allow reading. 292 */ 293static int 294ngt_read(struct tty *tp, struct uio *uio, int flag) 295{ 296 return (EIO); 297} 298 299/* 300 * Once the device has been turned into a node, we don't allow writing. 301 */ 302static int 303ngt_write(struct tty *tp, struct uio *uio, int flag) 304{ 305 return (EIO); 306} 307 308/* 309 * We implement the NGIOCGINFO ioctl() defined in ng_message.h. 310 */ 311static int 312ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct proc *p) 313{ 314 const sc_p sc = (sc_p) tp->t_sc; 315 int s, error = 0; 316 317 s = spltty(); 318 switch (cmd) { 319 case NGIOCGINFO: 320 { 321 struct nodeinfo *const ni = (struct nodeinfo *) data; 322 const node_p node = sc->node; 323 324 bzero(ni, sizeof(*ni)); 325 if (node->name) 326 strncpy(ni->name, node->name, sizeof(ni->name) - 1); 327 strncpy(ni->type, node->type->name, sizeof(ni->type) - 1); 328 ni->id = (u_int32_t) node; 329 ni->hooks = node->numhooks; 330 break; 331 } 332 default: 333 ERROUT(ENOIOCTL); 334 } 335done: 336 splx(s); 337 return (error); 338} 339 340/* 341 * Receive data coming from the device. We get one character at 342 * a time, which is kindof silly. 343 * Only guaranteed to be at splsofttty() or spltty(). 344 */ 345static int 346ngt_input(int c, struct tty *tp) 347{ 348 const sc_p sc = (sc_p) tp->t_sc; 349 const node_p node = sc->node; 350 struct mbuf *m; 351 int s, error = 0; 352 353 if (!sc || tp != sc->tp) 354 return (0); 355 s = spltty(); 356 if (!sc->hook) 357 ERROUT(0); 358 359 /* Check for error conditions */ 360 if ((tp->t_state & TS_CONNECTED) == 0) { 361 if (sc->flags & FLG_DEBUG) 362 log(LOG_DEBUG, "%s: no carrier\n", node->name); 363 ERROUT(0); 364 } 365 if (c & TTY_ERRORMASK) { 366 /* framing error or overrun on this char */ 367 if (sc->flags & FLG_DEBUG) 368 log(LOG_DEBUG, "%s: line error %x\n", 369 node->name, c & TTY_ERRORMASK); 370 ERROUT(0); 371 } 372 c &= TTY_CHARMASK; 373 374 /* Get a new header mbuf if we need one */ 375 if (!(m = sc->m)) { 376 MGETHDR(m, M_DONTWAIT, MT_DATA); 377 if (!m) { 378 if (sc->flags & FLG_DEBUG) 379 log(LOG_ERR, 380 "%s: can't get mbuf\n", node->name); 381 ERROUT(ENOBUFS); 382 } 383 m->m_len = m->m_pkthdr.len = 0; 384 m->m_pkthdr.rcvif = NULL; 385 sc->m = m; 386 } 387 388 /* Add char to mbuf */ 389 *mtod(m, u_char *) = c; 390 m->m_data++; 391 m->m_len++; 392 m->m_pkthdr.len++; 393 394 /* Ship off mbuf if it's time */ 395 if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) { 396 m->m_data = m->m_pktdat; 397 NG_SEND_DATA_ONLY(error, sc->hook, m); 398 sc->m = NULL; 399 } 400done: 401 splx(s); 402 return (error); 403} 404 405/* 406 * This is called when the device driver is ready for more output. 407 * Called from tty system at splsofttty() or spltty(). 408 * Also call from ngt_rcv_data() when a new mbuf is available for output. 409 */ 410static int 411ngt_start(struct tty *tp) 412{ 413 const sc_p sc = (sc_p) tp->t_sc; 414 int s; 415 416 s = spltty(); 417 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */ 418 struct mbuf *m = sc->qhead; 419 420 /* Remove first mbuf from queue */ 421 if (!m) 422 break; 423 if ((sc->qhead = m->m_nextpkt) == NULL) 424 sc->qtail = &sc->qhead; 425 sc->qlen--; 426 QUEUECHECK(sc); 427 428 /* Send as much of it as possible */ 429 while (m) { 430 struct mbuf *m2; 431 int sent; 432 433 sent = m->m_len 434 - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq); 435 m->m_data += sent; 436 m->m_len -= sent; 437 if (m->m_len > 0) 438 break; /* device can't take no more */ 439 MFREE(m, m2); 440 m = m2; 441 } 442 443 /* Put remainder of mbuf chain (if any) back on queue */ 444 if (m) { 445 m->m_nextpkt = sc->qhead; 446 sc->qhead = m; 447 if (sc->qtail == &sc->qhead) 448 sc->qtail = &m->m_nextpkt; 449 sc->qlen++; 450 QUEUECHECK(sc); 451 break; 452 } 453 } 454 455 /* Call output process whether or not there is any output. We are 456 * being called in lieu of ttstart and must do what it would. */ 457 if (tp->t_oproc != NULL) 458 (*tp->t_oproc) (tp); 459 460 /* This timeout is needed for operation on a pseudo-tty, because the 461 * pty code doesn't call pppstart after it has drained the t_outq. */ 462 if (sc->qhead && (sc->flags & FLG_TIMEOUT) == 0) { 463 sc->chand = timeout(ngt_timeout, sc, 1); 464 sc->flags |= FLG_TIMEOUT; 465 } 466 splx(s); 467 return (0); 468} 469 470/* 471 * We still have data to output to the device, so try sending more. 472 */ 473static void 474ngt_timeout(void *arg) 475{ 476 const sc_p sc = (sc_p) arg; 477 int s; 478 479 s = spltty(); 480 sc->flags &= ~FLG_TIMEOUT; 481 ngt_start(sc->tp); 482 splx(s); 483} 484 485/****************************************************************** 486 NETGRAPH NODE METHODS 487******************************************************************/ 488 489/* 490 * Initialize a new node of this type. 491 * 492 * We only allow nodes to be created as a result of setting 493 * the line discipline on a tty, so always return an error if not. 494 */ 495static int 496ngt_constructor(node_p node) 497{ 498 return (EOPNOTSUPP); 499} 500 501/* 502 * Add a new hook. There can only be one. 503 */ 504static int 505ngt_newhook(node_p node, hook_p hook, const char *name) 506{ 507 const sc_p sc = node->private; 508 int s, error = 0; 509 510 if (strcmp(name, NG_TTY_HOOK)) 511 return (EINVAL); 512 s = spltty(); 513 if (sc->hook) 514 ERROUT(EISCONN); 515 sc->hook = hook; 516done: 517 splx(s); 518 return (error); 519} 520 521/* 522 * Set the hooks into queueing mode (for outgoing packets) 523 * Force single client at a time. 524 */ 525static int 526ngt_connect(hook_p hook) 527{ 528 hook->peer->flags |= HK_QUEUE|HK_FORCE_WRITER; 529 return (0); 530} 531 532/* 533 * Disconnect the hook 534 */ 535static int 536ngt_disconnect(hook_p hook) 537{ 538 const sc_p sc = hook->node->private; 539 int s; 540 541 s = spltty(); 542 if (hook != sc->hook) 543 panic(__FUNCTION__); 544 sc->hook = NULL; 545 m_freem(sc->m); 546 sc->m = NULL; 547 splx(s); 548 return (0); 549} 550 551/* 552 * Remove this node. The does the netgraph portion of the shutdown. 553 * This should only be called indirectly from ngt_close(). 554 */ 555static int 556ngt_shutdown(node_p node) 557{ 558 const sc_p sc = node->private; 559 560 if (!ngt_nodeop_ok) 561 return (EOPNOTSUPP); 562 node->private = NULL; 563 ng_unref(sc->node); 564 m_freem(sc->qhead); 565 m_freem(sc->m); 566 bzero(sc, sizeof(*sc)); 567 FREE(sc, M_NETGRAPH); 568 return (0); 569} 570 571/* 572 * Receive incoming data from netgraph system. Put it on our 573 * output queue and start output if necessary. 574 */ 575static int 576ngt_rcvdata(hook_p hook, item_p item) 577{ 578 const sc_p sc = hook->node->private; 579 int s, error = 0; 580 struct mbuf *m; 581 582 if (hook != sc->hook) 583 panic(__FUNCTION__); 584 585 NGI_GET_M(item, m); 586 NG_FREE_ITEM(item); 587 s = spltty(); 588 if (sc->qlen >= MAX_MBUFQ) 589 ERROUT(ENOBUFS); 590 m->m_nextpkt = NULL; 591 *sc->qtail = m; 592 sc->qtail = &m->m_nextpkt; 593 sc->qlen++; 594 QUEUECHECK(sc); 595 m = NULL; 596 if (sc->qlen == 1) 597 ngt_start(sc->tp); 598done: 599 splx(s); 600 if (m) 601 m_freem(m); 602 return (error); 603} 604 605/* 606 * Receive control message 607 */ 608static int 609ngt_rcvmsg(node_p node, item_p item, hook_p lasthook) 610{ 611 const sc_p sc = (sc_p) node->private; 612 struct ng_mesg *resp = NULL; 613 int error = 0; 614 struct ng_mesg *msg; 615 616 NGI_GET_MSG(item, msg); 617 switch (msg->header.typecookie) { 618 case NGM_TTY_COOKIE: 619 switch (msg->header.cmd) { 620 case NGM_TTY_SET_HOTCHAR: 621 { 622 int hotchar; 623 624 if (msg->header.arglen != sizeof(int)) 625 ERROUT(EINVAL); 626 hotchar = *((int *) msg->data); 627 if (hotchar != (u_char) hotchar && hotchar != -1) 628 ERROUT(EINVAL); 629 sc->hotchar = hotchar; /* race condition is OK */ 630 break; 631 } 632 case NGM_TTY_GET_HOTCHAR: 633 NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT); 634 if (!resp) 635 ERROUT(ENOMEM); 636 /* Race condition here is OK */ 637 *((int *) resp->data) = sc->hotchar; 638 break; 639 default: 640 ERROUT(EINVAL); 641 } 642 break; 643 default: 644 ERROUT(EINVAL); 645 } 646done: 647 NG_RESPOND_MSG(error, node, item, resp); 648 NG_FREE_MSG(msg); 649 return (error); 650} 651 652/****************************************************************** 653 INITIALIZATION 654******************************************************************/ 655 656/* 657 * Handle loading and unloading for this node type 658 */ 659static int 660ngt_mod_event(module_t mod, int event, void *data) 661{ 662 /* struct ng_type *const type = data;*/ 663 int s, error = 0; 664 665 switch (event) { 666 case MOD_LOAD: 667 668 /* Register line discipline */ 669 s = spltty(); 670 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) { 671 splx(s); 672 log(LOG_ERR, "%s: can't register line discipline", 673 __FUNCTION__); 674 return (EIO); 675 } 676 splx(s); 677 break; 678 679 case MOD_UNLOAD: 680 681 /* Unregister line discipline */ 682 s = spltty(); 683 ldisc_deregister(ngt_ldisc); 684 splx(s); 685 break; 686 687 default: 688 error = EOPNOTSUPP; 689 break; 690 } 691 return (error); 692} 693 694