ng_tty.c revision 130585
188314Sache 288314Sache/* 388314Sache * ng_tty.c 488314Sache * 588314Sache * Copyright (c) 1996-1999 Whistle Communications, Inc. 688314Sache * All rights reserved. 788314Sache * 888314Sache * Subject to the following obligations and disclaimer of warranty, use and 988314Sache * redistribution of this software, in source or object code forms, with or 1088314Sache * without modifications are expressly permitted by Whistle Communications; 1188314Sache * provided, however, that: 1288314Sache * 1. Any and all reproductions of the source or object code must include the 1388314Sache * copyright notice above and the following disclaimer of warranties; and 1488314Sache * 2. No rights are granted, in any manner or form, to use Whistle 1588314Sache * Communications, Inc. trademarks, including the mark "WHISTLE 1688314Sache * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 1788314Sache * such appears in the above copyright notice or in the software. 1888314Sache * 1988314Sache * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 2088314Sache * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 2188314Sache * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 2288314Sache * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 2388314Sache * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 2488314Sache * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 2588314Sache * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 2688314Sache * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 2788314Sache * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 2888314Sache * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 2988314Sache * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 3088314Sache * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 3188314Sache * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 3288314Sache * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3388314Sache * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3488314Sache * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 3588314Sache * OF SUCH DAMAGE. 3688314Sache * 37 * Author: Archie Cobbs <archie@freebsd.org> 38 * 39 * $FreeBSD: head/sys/netgraph/ng_tty.c 130585 2004-06-16 09:47:26Z 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 NG_TTY_DFL_HOTCHAR /* XXX can't change this in serial driver */ 153}; 154 155/* Netgraph node type descriptor */ 156static struct ng_type typestruct = { 157 .version = NG_ABI_VERSION, 158 .name = NG_TTY_NODE_TYPE, 159 .mod_event = ngt_mod_event, 160 .constructor = ngt_constructor, 161 .rcvmsg = ngt_rcvmsg, 162 .shutdown = ngt_shutdown, 163 .newhook = ngt_newhook, 164 .connect = ngt_connect, 165 .rcvdata = ngt_rcvdata, 166 .disconnect = ngt_disconnect, 167}; 168NETGRAPH_INIT(tty, &typestruct); 169 170static int ngt_unit; 171static int ngt_nodeop_ok; /* OK to create/remove node */ 172static int ngt_ldisc; 173 174/****************************************************************** 175 LINE DISCIPLINE METHODS 176******************************************************************/ 177 178/* 179 * Set our line discipline on the tty. 180 * Called from device open routine or ttioctl() at >= splsofttty() 181 */ 182static int 183ngt_open(struct cdev *dev, struct tty *tp) 184{ 185 struct thread *const td = curthread; /* XXX */ 186 char name[sizeof(NG_TTY_NODE_TYPE) + 8]; 187 sc_p sc; 188 int s, error; 189 190 /* Super-user only */ 191 if ((error = suser(td))) 192 return (error); 193 s = splnet(); 194 (void) spltty(); /* XXX is this necessary? */ 195 196 /* Already installed? */ 197 if (tp->t_line == NETGRAPHDISC) { 198 sc = (sc_p) tp->t_sc; 199 if (sc != NULL && sc->tp == tp) 200 goto done; 201 } 202 203 /* Initialize private struct */ 204 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO); 205 if (sc == NULL) { 206 error = ENOMEM; 207 goto done; 208 } 209 sc->tp = tp; 210 sc->hotchar = NG_TTY_DFL_HOTCHAR; 211 sc->qtail = &sc->qhead; 212 QUEUECHECK(sc); 213 callout_handle_init(&sc->chand); 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 FREE(sc, M_NETGRAPH); 221 goto done; 222 } 223 snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit++); 224 225 /* Assign node its name */ 226 if ((error = ng_name_node(sc->node, name))) { 227 log(LOG_ERR, "%s: node name exists?\n", name); 228 ngt_nodeop_ok = 1; 229 NG_NODE_UNREF(sc->node); 230 ngt_nodeop_ok = 0; 231 goto done; 232 } 233 234 /* Set back pointers */ 235 NG_NODE_SET_PRIVATE(sc->node, sc); 236 tp->t_sc = (caddr_t) sc; 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 248done: 249 /* Done */ 250 splx(s); 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 */ 259static int 260ngt_close(struct tty *tp, int flag) 261{ 262 const sc_p sc = (sc_p) tp->t_sc; 263 int s; 264 265 s = spltty(); 266 ttyflush(tp, FREAD | FWRITE); 267 clist_free_cblocks(&tp->t_outq); 268 tp->t_line = 0; 269 if (sc != NULL) { 270 if (sc->flags & FLG_TIMEOUT) { 271 untimeout(ngt_timeout, sc, sc->chand); 272 sc->flags &= ~FLG_TIMEOUT; 273 } 274 ngt_nodeop_ok = 1; 275 ng_rmnode_self(sc->node); 276 ngt_nodeop_ok = 0; 277 tp->t_sc = NULL; 278 } 279 splx(s); 280 return (0); 281} 282 283/* 284 * Once the device has been turned into a node, we don't allow reading. 285 */ 286static int 287ngt_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 */ 295static int 296ngt_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 */ 304static int 305ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *td) 306{ 307 const sc_p sc = (sc_p) tp->t_sc; 308 int s, error = 0; 309 310 s = spltty(); 311 switch (cmd) { 312 case NGIOCGINFO: 313 { 314 struct nodeinfo *const ni = (struct nodeinfo *) data; 315 const node_p node = sc->node; 316 317 bzero(ni, sizeof(*ni)); 318 if (NG_NODE_HAS_NAME(node)) 319 strncpy(ni->name, NG_NODE_NAME(node), sizeof(ni->name) - 1); 320 strncpy(ni->type, node->nd_type->name, sizeof(ni->type) - 1); 321 ni->id = (u_int32_t) ng_node2ID(node); 322 ni->hooks = NG_NODE_NUMHOOKS(node); 323 break; 324 } 325 default: 326 ERROUT(ENOIOCTL); 327 } 328done: 329 splx(s); 330 return (error); 331} 332 333/* 334 * Receive data coming from the device. We get one character at 335 * a time, which is kindof silly. 336 * Only guaranteed to be at splsofttty() or spltty(). 337 */ 338static int 339ngt_input(int c, struct tty *tp) 340{ 341 const sc_p sc = (sc_p) tp->t_sc; 342 const node_p node = sc->node; 343 struct mbuf *m; 344 int s, error = 0; 345 346 if (!sc || tp != sc->tp) 347 return (0); 348 s = spltty(); 349 if (!sc->hook) 350 ERROUT(0); 351 352 /* Check for error conditions */ 353 if ((tp->t_state & TS_CONNECTED) == 0) { 354 if (sc->flags & FLG_DEBUG) 355 log(LOG_DEBUG, "%s: no carrier\n", NG_NODE_NAME(node)); 356 ERROUT(0); 357 } 358 if (c & TTY_ERRORMASK) { 359 /* framing error or overrun on this char */ 360 if (sc->flags & FLG_DEBUG) 361 log(LOG_DEBUG, "%s: line error %x\n", 362 NG_NODE_NAME(node), c & TTY_ERRORMASK); 363 ERROUT(0); 364 } 365 c &= TTY_CHARMASK; 366 367 /* Get a new header mbuf if we need one */ 368 if (!(m = sc->m)) { 369 MGETHDR(m, M_DONTWAIT, MT_DATA); 370 if (!m) { 371 if (sc->flags & FLG_DEBUG) 372 log(LOG_ERR, 373 "%s: can't get mbuf\n", NG_NODE_NAME(node)); 374 ERROUT(ENOBUFS); 375 } 376 m->m_len = m->m_pkthdr.len = 0; 377 m->m_pkthdr.rcvif = NULL; 378 sc->m = m; 379 } 380 381 /* Add char to mbuf */ 382 *mtod(m, u_char *) = c; 383 m->m_data++; 384 m->m_len++; 385 m->m_pkthdr.len++; 386 387 /* Ship off mbuf if it's time */ 388 if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) { 389 m->m_data = m->m_pktdat; 390 NG_SEND_DATA_ONLY(error, sc->hook, m); 391 sc->m = NULL; 392 } 393done: 394 splx(s); 395 return (error); 396} 397 398/* 399 * This is called when the device driver is ready for more output. 400 * Called from tty system at splsofttty() or spltty(). 401 * Also call from ngt_rcv_data() when a new mbuf is available for output. 402 */ 403static int 404ngt_start(struct tty *tp) 405{ 406 const sc_p sc = (sc_p) tp->t_sc; 407 int s; 408 409 s = spltty(); 410 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */ 411 struct mbuf *m = sc->qhead; 412 413 /* Remove first mbuf from queue */ 414 if (!m) 415 break; 416 if ((sc->qhead = m->m_nextpkt) == NULL) 417 sc->qtail = &sc->qhead; 418 sc->qlen--; 419 QUEUECHECK(sc); 420 421 /* Send as much of it as possible */ 422 while (m) { 423 int sent; 424 425 sent = m->m_len 426 - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq); 427 m->m_data += sent; 428 m->m_len -= sent; 429 if (m->m_len > 0) 430 break; /* device can't take no more */ 431 m = m_free(m); 432 } 433 434 /* Put remainder of mbuf chain (if any) back on queue */ 435 if (m) { 436 m->m_nextpkt = sc->qhead; 437 sc->qhead = m; 438 if (sc->qtail == &sc->qhead) 439 sc->qtail = &m->m_nextpkt; 440 sc->qlen++; 441 QUEUECHECK(sc); 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 if (sc->qhead && (sc->flags & FLG_TIMEOUT) == 0) { 454 sc->chand = timeout(ngt_timeout, sc, 1); 455 sc->flags |= FLG_TIMEOUT; 456 } 457 splx(s); 458 return (0); 459} 460 461/* 462 * We still have data to output to the device, so try sending more. 463 */ 464static void 465ngt_timeout(void *arg) 466{ 467 const sc_p sc = (sc_p) arg; 468 int s; 469 470 s = spltty(); 471 sc->flags &= ~FLG_TIMEOUT; 472 ngt_start(sc->tp); 473 splx(s); 474} 475 476/****************************************************************** 477 NETGRAPH NODE METHODS 478******************************************************************/ 479 480/* 481 * Initialize a new node of this type. 482 * 483 * We only allow nodes to be created as a result of setting 484 * the line discipline on a tty, so always return an error if not. 485 */ 486static int 487ngt_constructor(node_p node) 488{ 489 return (EOPNOTSUPP); 490} 491 492/* 493 * Add a new hook. There can only be one. 494 */ 495static int 496ngt_newhook(node_p node, hook_p hook, const char *name) 497{ 498 const sc_p sc = NG_NODE_PRIVATE(node); 499 int s, error = 0; 500 501 if (strcmp(name, NG_TTY_HOOK)) 502 return (EINVAL); 503 s = spltty(); 504 if (sc->hook) 505 ERROUT(EISCONN); 506 sc->hook = hook; 507done: 508 splx(s); 509 return (error); 510} 511 512/* 513 * Set the hooks into queueing mode (for outgoing packets) 514 * Force single client at a time. 515 */ 516static int 517ngt_connect(hook_p hook) 518{ 519 /*NG_HOOK_FORCE_WRITER(hook); 520 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));*/ 521 return (0); 522} 523 524/* 525 * Disconnect the hook 526 */ 527static int 528ngt_disconnect(hook_p hook) 529{ 530 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 531 int s; 532 533 s = spltty(); 534 if (hook != sc->hook) 535 panic(__func__); 536 sc->hook = NULL; 537 m_freem(sc->m); 538 sc->m = NULL; 539 splx(s); 540 return (0); 541} 542 543/* 544 * Remove this node. The does the netgraph portion of the shutdown. 545 * This should only be called indirectly from ngt_close(). 546 */ 547static int 548ngt_shutdown(node_p node) 549{ 550 const sc_p sc = NG_NODE_PRIVATE(node); 551 552 if (!ngt_nodeop_ok) 553 return (EOPNOTSUPP); 554 NG_NODE_SET_PRIVATE(node, NULL); 555 NG_NODE_UNREF(sc->node); 556 m_freem(sc->qhead); 557 m_freem(sc->m); 558 bzero(sc, sizeof(*sc)); 559 FREE(sc, M_NETGRAPH); 560 return (0); 561} 562 563/* 564 * Receive incoming data from netgraph system. Put it on our 565 * output queue and start output if necessary. 566 */ 567static int 568ngt_rcvdata(hook_p hook, item_p item) 569{ 570 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 571 int s, error = 0; 572 struct mbuf *m; 573 574 if (hook != sc->hook) 575 panic(__func__); 576 577 NGI_GET_M(item, m); 578 NG_FREE_ITEM(item); 579 s = spltty(); 580 if (sc->qlen >= MAX_MBUFQ) 581 ERROUT(ENOBUFS); 582 m->m_nextpkt = NULL; 583 *sc->qtail = m; 584 sc->qtail = &m->m_nextpkt; 585 sc->qlen++; 586 QUEUECHECK(sc); 587 m = NULL; 588 if (sc->qlen == 1) 589 ngt_start(sc->tp); 590done: 591 splx(s); 592 if (m) 593 m_freem(m); 594 return (error); 595} 596 597/* 598 * Receive control message 599 */ 600static int 601ngt_rcvmsg(node_p node, item_p item, hook_p lasthook) 602{ 603 const sc_p sc = NG_NODE_PRIVATE(node); 604 struct ng_mesg *resp = NULL; 605 int error = 0; 606 struct ng_mesg *msg; 607 608 NGI_GET_MSG(item, msg); 609 switch (msg->header.typecookie) { 610 case NGM_TTY_COOKIE: 611 switch (msg->header.cmd) { 612 case NGM_TTY_SET_HOTCHAR: 613 { 614 int hotchar; 615 616 if (msg->header.arglen != sizeof(int)) 617 ERROUT(EINVAL); 618 hotchar = *((int *) msg->data); 619 if (hotchar != (u_char) hotchar && hotchar != -1) 620 ERROUT(EINVAL); 621 sc->hotchar = hotchar; /* race condition is OK */ 622 break; 623 } 624 case NGM_TTY_GET_HOTCHAR: 625 NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT); 626 if (!resp) 627 ERROUT(ENOMEM); 628 /* Race condition here is OK */ 629 *((int *) resp->data) = sc->hotchar; 630 break; 631 default: 632 ERROUT(EINVAL); 633 } 634 break; 635 default: 636 ERROUT(EINVAL); 637 } 638done: 639 NG_RESPOND_MSG(error, node, item, resp); 640 NG_FREE_MSG(msg); 641 return (error); 642} 643 644/****************************************************************** 645 INITIALIZATION 646******************************************************************/ 647 648/* 649 * Handle loading and unloading for this node type 650 */ 651static int 652ngt_mod_event(module_t mod, int event, void *data) 653{ 654 /* struct ng_type *const type = data;*/ 655 int s, error = 0; 656 657 switch (event) { 658 case MOD_LOAD: 659 660 /* Register line discipline */ 661 s = spltty(); 662 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) { 663 splx(s); 664 log(LOG_ERR, "%s: can't register line discipline", 665 __func__); 666 return (EIO); 667 } 668 splx(s); 669 break; 670 671 case MOD_UNLOAD: 672 673 /* Unregister line discipline */ 674 s = spltty(); 675 ldisc_deregister(ngt_ldisc); 676 splx(s); 677 break; 678 679 default: 680 error = EOPNOTSUPP; 681 break; 682 } 683 return (error); 684} 685 686