6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
38 * Author: Archie Cobbs <archie@freebsd.org>
41 * $Whistle: ng_tty.c,v 1.21 1999/11/01 09:24:52 julian Exp $
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.
50 * Once the line discipline is installed, you can find out the name
51 * of the corresponding netgraph node via a NGIOCGINFO ioctl().
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.
60 #include <sys/param.h>
61 #include <sys/systm.h>
63 #include <sys/errno.h>
64 #include <sys/fcntl.h>
65 #include <sys/ioccom.h>
66 #include <sys/kernel.h>
67 #include <sys/malloc.h>
69 #include <sys/socket.h>
70 #include <sys/syslog.h>
72 #include <sys/ttycom.h>
75 #include <net/if_var.h>
77 #include <netgraph/ng_message.h>
78 #include <netgraph/netgraph.h>
79 #include <netgraph/ng_tty.h>
82 #define MAX_MBUFQ 3 /* Max number of queued mbufs */
83 #define NGT_HIWATER 400 /* High water mark on output */
85 /* Per-node private info */
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) */
96 typedef struct ngt_sc *sc_p;
99 #define FLG_DEBUG 0x0002
100 #define FLG_DIE 0x0004
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 thread *);
109 static int ngt_input(int c, struct tty *tp);
110 static int ngt_start(struct tty *tp);
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);
123 static void ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2);
125 #define ERROUT(x) do { error = (x); goto done; } while (0)
127 /* Line discipline descriptor */
128 static struct linesw ngt_disc = {
130 .l_close = ngt_close,
132 .l_write = ngt_write,
133 .l_ioctl = ngt_tioctl,
135 .l_start = ngt_start,
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,
152 NETGRAPH_INIT(tty, &typestruct);
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
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).
166 * - ngt_unit is incremented atomically.
169 #define NGTLOCK(sc) IF_LOCK(&sc->outq)
170 #define NGTUNLOCK(sc) IF_UNLOCK(&sc->outq)
173 static int ngt_ldisc;
175 /******************************************************************
176 LINE DISCIPLINE METHODS
177 ******************************************************************/
180 * Set our line discipline on the tty.
181 * Called from device open routine or ttioctl()
184 ngt_open(struct cdev *dev, struct tty *tp)
186 struct thread *const td = curthread; /* XXX */
187 char name[sizeof(NG_TTY_NODE_TYPE) + 8];
191 /* Super-user only */
192 if ((error = suser(td)))
195 /* Initialize private struct */
196 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
201 sc->hotchar = tp->t_hotchar = NG_TTY_DFL_HOTCHAR;
202 mtx_init(&sc->outq.ifq_mtx, "ng_tty node+queue", NULL, MTX_DEF);
203 IFQ_SET_MAXLEN(&sc->outq, MAX_MBUFQ);
207 /* Setup netgraph node */
208 error = ng_make_node_common(&typestruct, &sc->node);
211 FREE(sc, M_NETGRAPH);
215 atomic_add_int(&ngt_unit, 1);
216 snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit);
218 /* Assign node its name */
219 if ((error = ng_name_node(sc->node, name))) {
220 sc->flags |= FLG_DIE;
222 NG_NODE_UNREF(sc->node);
223 log(LOG_ERR, "%s: node name exists?\n", name);
227 /* Set back pointers */
228 NG_NODE_SET_PRIVATE(sc->node, sc);
231 ng_callout_init(&sc->chand);
234 * Pre-allocate cblocks to the an appropriate amount.
235 * I'm not sure what is appropriate.
237 ttyflush(tp, FREAD | FWRITE);
238 clist_alloc_cblocks(&tp->t_canq, 0, 0);
239 clist_alloc_cblocks(&tp->t_rawq, 0, 0);
240 clist_alloc_cblocks(&tp->t_outq,
241 MLEN + NGT_HIWATER, MLEN + NGT_HIWATER);
249 * Line specific close routine, called from device close routine
250 * and from ttioctl. This causes the node to be destroyed as well.
253 ngt_close(struct tty *tp, int flag)
255 const sc_p sc = (sc_p) tp->t_lsc;
257 ttyflush(tp, FREAD | FWRITE);
258 clist_free_cblocks(&tp->t_outq);
261 if (callout_pending(&sc->chand))
262 ng_uncallout(&sc->chand, sc->node);
264 sc->flags |= FLG_DIE;
266 ng_rmnode_self(sc->node);
272 * Once the device has been turned into a node, we don't allow reading.
275 ngt_read(struct tty *tp, struct uio *uio, int flag)
281 * Once the device has been turned into a node, we don't allow writing.
284 ngt_write(struct tty *tp, struct uio *uio, int flag)
290 * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
293 ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *td)
295 const sc_p sc = (sc_p) tp->t_lsc;
298 /* No node attached */
304 struct nodeinfo *const ni = (struct nodeinfo *) data;
305 const node_p node = sc->node;
307 bzero(ni, sizeof(*ni));
309 if (NG_NODE_HAS_NAME(node))
310 strncpy(ni->name, NG_NODE_NAME(node), sizeof(ni->name) - 1);
311 strncpy(ni->type, node->nd_type->name, sizeof(ni->type) - 1);
312 ni->id = (u_int32_t) ng_node2ID(node);
313 ni->hooks = NG_NODE_NUMHOOKS(node);
325 * Receive data coming from the device. We get one character at
326 * a time, which is kindof silly.
328 * Full locking of softc is not required, since we are the only
332 ngt_input(int c, struct tty *tp)
339 sc = (sc_p) tp->t_lsc;
341 /* No node attached */
349 /* Check for error conditions */
350 if ((tp->t_state & TS_CONNECTED) == 0) {
351 if (sc->flags & FLG_DEBUG)
352 log(LOG_DEBUG, "%s: no carrier\n", NG_NODE_NAME(node));
355 if (c & TTY_ERRORMASK) {
356 /* framing error or overrun on this char */
357 if (sc->flags & FLG_DEBUG)
358 log(LOG_DEBUG, "%s: line error %x\n",
359 NG_NODE_NAME(node), c & TTY_ERRORMASK);
364 /* Get a new header mbuf if we need one */
366 MGETHDR(m, M_DONTWAIT, MT_DATA);
368 if (sc->flags & FLG_DEBUG)
370 "%s: can't get mbuf\n", NG_NODE_NAME(node));
373 m->m_len = m->m_pkthdr.len = 0;
374 m->m_pkthdr.rcvif = NULL;
378 /* Add char to mbuf */
379 *mtod(m, u_char *) = c;
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;
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.
397 if (sc->hook == NULL) {
400 return (0); /* XXX: original behavior */
402 NG_SEND_DATA_ONLY(error, sc->hook, m); /* Will queue */
410 * This is called when the device driver is ready for more output.
411 * Also called from ngt_rcv_data() when a new mbuf is available for output.
414 ngt_start(struct tty *tp)
416 const sc_p sc = (sc_p) tp->t_lsc;
418 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */
421 /* Remove first mbuf from queue */
422 IF_DEQUEUE(&sc->outq, m);
426 /* Send as much of it as possible */
431 - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq);
435 break; /* device can't take no more */
439 /* Put remainder of mbuf chain (if any) back on queue */
441 IF_PREPEND(&sc->outq, m);
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. */
450 /* This timeout is needed for operation on a pseudo-tty, because the
451 * pty code doesn't call pppstart after it has drained the t_outq. */
452 /* XXX: outq not locked */
453 if (!IFQ_IS_EMPTY(&sc->outq) && !callout_pending(&sc->chand))
454 ng_callout(&sc->chand, sc->node, NULL, 1, ngt_timeout, NULL, 0);
460 * We still have data to output to the device, so try sending more.
463 ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2)
465 const sc_p sc = NG_NODE_PRIVATE(node);
472 /******************************************************************
473 NETGRAPH NODE METHODS
474 ******************************************************************/
477 * Initialize a new node of this type.
479 * We only allow nodes to be created as a result of setting
480 * the line discipline on a tty, so always return an error if not.
483 ngt_constructor(node_p node)
489 * Add a new hook. There can only be one.
492 ngt_newhook(node_p node, hook_p hook, const char *name)
494 const sc_p sc = NG_NODE_PRIVATE(node);
496 if (strcmp(name, NG_TTY_HOOK))
510 * Set the hook into queueing mode (for outgoing packets),
511 * so that we wont deliver mbuf thru the whole graph holding
515 ngt_connect(hook_p hook)
517 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
519 * XXX: While ngt_start() is Giant-locked, queue incoming
520 * packets, too. Otherwise we acquire Giant holding some
521 * IP stack locks, e.g. divinp, and this makes WITNESS scream.
523 NG_HOOK_FORCE_QUEUE(hook);
528 * Disconnect the hook
531 ngt_disconnect(hook_p hook)
533 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
535 if (hook != sc->hook)
546 * Remove this node. The does the netgraph portion of the shutdown.
547 * This should only be called indirectly from ngt_close().
549 * tp->t_lsc is already NULL, so we should be protected from
553 ngt_shutdown(node_p node)
555 const sc_p sc = NG_NODE_PRIVATE(node);
558 if (!(sc->flags & FLG_DIE)) {
565 _IF_DRAIN(&sc->outq);
566 mtx_destroy(&(sc)->outq.ifq_mtx);
568 NG_NODE_UNREF(sc->node);
569 FREE(sc, M_NETGRAPH);
575 * Receive incoming data from netgraph system. Put it on our
576 * output queue and start output if necessary.
579 ngt_rcvdata(hook_p hook, item_p item)
581 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
585 if (hook != sc->hook)
592 if (_IF_QFULL(&sc->outq)) {
594 IF_UNLOCK(&sc->outq);
599 _IF_ENQUEUE(&sc->outq, m);
600 qlen = sc->outq.ifq_len;
601 IF_UNLOCK(&sc->outq);
604 * If qlen > 1, then we should already have a scheduled callout.
616 * Receive control message
619 ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
621 const sc_p sc = NG_NODE_PRIVATE(node);
622 struct ng_mesg *msg, *resp = NULL;
625 NGI_GET_MSG(item, msg);
626 switch (msg->header.typecookie) {
628 switch (msg->header.cmd) {
629 case NGM_TTY_SET_HOTCHAR:
633 if (msg->header.arglen != sizeof(int))
635 hotchar = *((int *) msg->data);
636 if (hotchar != (u_char) hotchar && hotchar != -1)
638 sc->hotchar = hotchar; /* race condition is OK */
641 case NGM_TTY_GET_HOTCHAR:
642 NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT);
645 /* Race condition here is OK */
646 *((int *) resp->data) = sc->hotchar;
656 NG_RESPOND_MSG(error, node, item, resp);
661 /******************************************************************
663 ******************************************************************/
666 * Handle loading and unloading for this node type
669 ngt_mod_event(module_t mod, int event, void *data)
676 /* Register line discipline */
678 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
680 log(LOG_ERR, "%s: can't register line discipline",
689 /* Unregister line discipline */
691 ldisc_deregister(ngt_ldisc);