MFC r1.27:
[dragonfly.git] / sys / netgraph7 / ng_tty.c
blob01e2d61d1570e392f1bcfa3e2e66fa473d9d745d
1 /*
2 * ng_tty.c
3 */
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.
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.
38 * Author: Archie Cobbs <archie@freebsd.org>
40 * $FreeBSD: src/sys/netgraph/ng_tty.c,v 1.37 2006/11/06 13:42:03 rwatson Exp $
41 * $DragonFly: src/sys/netgraph7/ng_tty.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
42 * $Whistle: ng_tty.c,v 1.21 1999/11/01 09:24:52 julian Exp $
46 * This file implements a terminal line discipline that is also a
47 * netgraph node. Installing this line discipline on a terminal device
48 * instantiates a new netgraph node of this type, which allows access
49 * to the device via the "hook" hook of the node.
51 * Once the line discipline is installed, you can find out the name
52 * of the corresponding netgraph node via a NGIOCGINFO ioctl().
54 * Incoming characters are delievered to the hook one at a time, each
55 * in its own mbuf. You may optionally define a ``hotchar,'' which causes
56 * incoming characters to be buffered up until either the hotchar is
57 * seen or the mbuf is full (MHLEN bytes). Then all buffered characters
58 * are immediately delivered.
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/conf.h>
64 #include <sys/errno.h>
65 #include <sys/fcntl.h>
66 #include <sys/ioccom.h>
67 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/mbuf.h>
70 #include <sys/priv.h>
71 #include <sys/socket.h>
72 #include <sys/syslog.h>
73 #include <sys/tty.h>
74 #include <sys/ttycom.h>
76 #include <net/if.h>
77 #include <net/if_var.h>
79 #include "ng_message.h"
80 #include "netgraph.h"
81 #include "ng_tty.h"
83 /* Misc defs */
84 #define MAX_MBUFQ 3 /* Max number of queued mbufs */
85 #define NGT_HIWATER 400 /* High water mark on output */
87 /* Per-node private info */
88 struct ngt_sc {
89 struct tty *tp; /* Terminal device */
90 node_p node; /* Netgraph node */
91 hook_p hook; /* Netgraph hook */
92 struct ifqueue outq; /* Queue of outgoing data */
93 struct mbuf *m; /* Incoming data buffer */
94 short hotchar; /* Hotchar, or -1 if none */
95 u_int flags; /* Flags */
96 struct callout chand; /* See man timeout(9) */
98 typedef struct ngt_sc *sc_p;
100 /* Flags */
101 #define FLG_DEBUG 0x0002
102 #define FLG_DIE 0x0004
104 /* Line discipline methods */
105 static int ngt_open(struct cdev *dev, struct tty *tp);
106 static int ngt_close(struct tty *tp, int flag);
107 static int ngt_read(struct tty *tp, struct uio *uio, int flag);
108 static int ngt_write(struct tty *tp, struct uio *uio, int flag);
109 static int ngt_tioctl(struct tty *tp,
110 u_long cmd, caddr_t data, int flag, struct thread *);
111 static int ngt_input(int c, struct tty *tp);
112 static int ngt_start(struct tty *tp);
114 /* Netgraph methods */
115 static ng_constructor_t ngt_constructor;
116 static ng_rcvmsg_t ngt_rcvmsg;
117 static ng_shutdown_t ngt_shutdown;
118 static ng_newhook_t ngt_newhook;
119 static ng_connect_t ngt_connect;
120 static ng_rcvdata_t ngt_rcvdata;
121 static ng_disconnect_t ngt_disconnect;
122 static int ngt_mod_event(module_t mod, int event, void *data);
124 /* Other stuff */
125 static void ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2);
127 #define ERROUT(x) do { error = (x); goto done; } while (0)
129 /* Line discipline descriptor */
130 static struct linesw ngt_disc = {
131 .l_open = ngt_open,
132 .l_close = ngt_close,
133 .l_read = ngt_read,
134 .l_write = ngt_write,
135 .l_ioctl = ngt_tioctl,
136 .l_rint = ngt_input,
137 .l_start = ngt_start,
138 .l_modem = ttymodem,
141 /* Netgraph node type descriptor */
142 static struct ng_type typestruct = {
143 .version = NG_ABI_VERSION,
144 .name = NG_TTY_NODE_TYPE,
145 .mod_event = ngt_mod_event,
146 .constructor = ngt_constructor,
147 .rcvmsg = ngt_rcvmsg,
148 .shutdown = ngt_shutdown,
149 .newhook = ngt_newhook,
150 .connect = ngt_connect,
151 .rcvdata = ngt_rcvdata,
152 .disconnect = ngt_disconnect,
154 NETGRAPH_INIT(tty, &typestruct);
157 * Locking:
159 * - node private data and tp->t_lsc is protected by mutex in struct
160 * ifqueue, locking is done using IF_XXX() macros.
161 * - in all tty methods we should acquire node ifqueue mutex, when accessing
162 * private data.
163 * - in _rcvdata() we should use locked versions of IF_{EN,DE}QUEUE() since
164 * we may have multiple _rcvdata() threads.
165 * - when calling any of tty methods from netgraph methods, we should
166 * acquire tty locking (now Giant).
168 * - ngt_unit is incremented atomically.
171 #define NGTLOCK(sc) IF_LOCK(&sc->outq)
172 #define NGTUNLOCK(sc) IF_UNLOCK(&sc->outq)
174 static int ngt_unit;
175 static int ngt_ldisc;
177 /******************************************************************
178 LINE DISCIPLINE METHODS
179 ******************************************************************/
182 * Set our line discipline on the tty.
183 * Called from device open routine or ttioctl()
185 static int
186 ngt_open(struct cdev *dev, struct tty *tp)
188 struct thread *const td = curthread; /* XXX */
189 char name[sizeof(NG_TTY_NODE_TYPE) + 8];
190 sc_p sc;
191 int error;
193 /* Super-user only */
194 error = priv_check(td, PRIV_NETGRAPH_TTY);
195 if (error)
196 return (error);
198 /* Initialize private struct */
199 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
200 if (sc == NULL)
201 return (ENOMEM);
203 sc->tp = tp;
204 sc->hotchar = tp->t_hotchar = NG_TTY_DFL_HOTCHAR;
205 mtx_init(&sc->outq.ifq_mtx, "ng_tty node+queue", NULL, MTX_DEF);
206 IFQ_SET_MAXLEN(&sc->outq, MAX_MBUFQ);
208 NGTLOCK(sc);
210 /* Setup netgraph node */
211 error = ng_make_node_common(&typestruct, &sc->node);
212 if (error) {
213 NGTUNLOCK(sc);
214 FREE(sc, M_NETGRAPH);
215 return (error);
218 atomic_add_int(&ngt_unit, 1);
219 snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit);
221 /* Assign node its name */
222 if ((error = ng_name_node(sc->node, name))) {
223 sc->flags |= FLG_DIE;
224 NGTUNLOCK(sc);
225 NG_NODE_UNREF(sc->node);
226 log(LOG_ERR, "%s: node name exists?\n", name);
227 return (error);
230 /* Set back pointers */
231 NG_NODE_SET_PRIVATE(sc->node, sc);
232 tp->t_lsc = sc;
234 ng_callout_init(&sc->chand);
237 * Pre-allocate cblocks to the an appropriate amount.
238 * I'm not sure what is appropriate.
240 ttyflush(tp, FREAD | FWRITE);
241 clist_alloc_cblocks(&tp->t_canq, 0, 0);
242 clist_alloc_cblocks(&tp->t_rawq, 0, 0);
243 clist_alloc_cblocks(&tp->t_outq,
244 MLEN + NGT_HIWATER, MLEN + NGT_HIWATER);
246 NGTUNLOCK(sc);
248 return (0);
252 * Line specific close routine, called from device close routine
253 * and from ttioctl. This causes the node to be destroyed as well.
255 static int
256 ngt_close(struct tty *tp, int flag)
258 const sc_p sc = (sc_p) tp->t_lsc;
260 ttyflush(tp, FREAD | FWRITE);
261 clist_free_cblocks(&tp->t_outq);
262 if (sc != NULL) {
263 NGTLOCK(sc);
264 if (callout_pending(&sc->chand))
265 ng_uncallout(&sc->chand, sc->node);
266 tp->t_lsc = NULL;
267 sc->flags |= FLG_DIE;
268 NGTUNLOCK(sc);
269 ng_rmnode_self(sc->node);
271 return (0);
275 * Once the device has been turned into a node, we don't allow reading.
277 static int
278 ngt_read(struct tty *tp, struct uio *uio, int flag)
280 return (EIO);
284 * Once the device has been turned into a node, we don't allow writing.
286 static int
287 ngt_write(struct tty *tp, struct uio *uio, int flag)
289 return (EIO);
293 * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
295 static int
296 ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *td)
298 const sc_p sc = (sc_p) tp->t_lsc;
300 if (sc == NULL)
301 /* No node attached */
302 return (0);
304 switch (cmd) {
305 case NGIOCGINFO:
307 struct nodeinfo *const ni = (struct nodeinfo *) data;
308 const node_p node = sc->node;
310 bzero(ni, sizeof(*ni));
311 NGTLOCK(sc);
312 if (NG_NODE_HAS_NAME(node))
313 strncpy(ni->name, NG_NODE_NAME(node), sizeof(ni->name) - 1);
314 strncpy(ni->type, node->nd_type->name, sizeof(ni->type) - 1);
315 ni->id = (u_int32_t) ng_node2ID(node);
316 ni->hooks = NG_NODE_NUMHOOKS(node);
317 NGTUNLOCK(sc);
318 break;
320 default:
321 return (ENOIOCTL);
324 return (0);
328 * Receive data coming from the device. We get one character at
329 * a time, which is kindof silly.
331 * Full locking of softc is not required, since we are the only
332 * user of sc->m.
334 static int
335 ngt_input(int c, struct tty *tp)
337 sc_p sc;
338 node_p node;
339 struct mbuf *m;
340 int error = 0;
342 sc = (sc_p) tp->t_lsc;
343 if (sc == NULL)
344 /* No node attached */
345 return (0);
347 node = sc->node;
349 if (tp != sc->tp)
350 panic("ngt_input");
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 return (0);
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 return (0);
365 c &= TTY_CHARMASK;
367 /* Get a new header mbuf if we need one */
368 if (!(m = sc->m)) {
369 MGETHDR(m, MB_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 return (ENOBUFS);
376 m->m_len = m->m_pkthdr.len = 0;
377 m->m_pkthdr.rcvif = NULL;
378 sc->m = m;
381 /* Add char to mbuf */
382 *mtod(m, u_char *) = c;
383 m->m_data++;
384 m->m_len++;
385 m->m_pkthdr.len++;
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 sc->m = NULL;
393 * We have built our mbuf without checking that we actually
394 * have a hook to send it. This was done to avoid
395 * acquiring mutex on each character. Check now.
399 NGTLOCK(sc);
400 if (sc->hook == NULL) {
401 NGTUNLOCK(sc);
402 m_freem(m);
403 return (0); /* XXX: original behavior */
405 NG_SEND_DATA_ONLY(error, sc->hook, m); /* Will queue */
406 NGTUNLOCK(sc);
409 return (error);
413 * This is called when the device driver is ready for more output.
414 * Also called from ngt_rcv_data() when a new mbuf is available for output.
416 static int
417 ngt_start(struct tty *tp)
419 const sc_p sc = (sc_p) tp->t_lsc;
421 while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */
422 struct mbuf *m;
424 /* Remove first mbuf from queue */
425 IF_DEQUEUE(&sc->outq, m);
426 if (m == NULL)
427 break;
429 /* Send as much of it as possible */
430 while (m != NULL) {
431 int sent;
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 m = m_free(m);
442 /* Put remainder of mbuf chain (if any) back on queue */
443 if (m != NULL) {
444 IF_PREPEND(&sc->outq, m);
445 break;
449 /* Call output process whether or not there is any output. We are
450 * being called in lieu of ttstart and must do what it would. */
451 tt_oproc(tp);
453 /* This timeout is needed for operation on a pseudo-tty, because the
454 * pty code doesn't call pppstart after it has drained the t_outq. */
455 /* XXX: outq not locked */
456 if (!IFQ_IS_EMPTY(&sc->outq) && !callout_pending(&sc->chand))
457 ng_callout(&sc->chand, sc->node, NULL, 1, ngt_timeout, NULL, 0);
459 return (0);
463 * We still have data to output to the device, so try sending more.
465 static void
466 ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2)
468 const sc_p sc = NG_NODE_PRIVATE(node);
470 mtx_lock(&Giant);
471 ngt_start(sc->tp);
472 mtx_unlock(&Giant);
475 /******************************************************************
476 NETGRAPH NODE METHODS
477 ******************************************************************/
480 * Initialize a new node of this type.
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.
485 static int
486 ngt_constructor(node_p node)
488 return (EOPNOTSUPP);
492 * Add a new hook. There can only be one.
494 static int
495 ngt_newhook(node_p node, hook_p hook, const char *name)
497 const sc_p sc = NG_NODE_PRIVATE(node);
499 if (strcmp(name, NG_TTY_HOOK))
500 return (EINVAL);
502 if (sc->hook)
503 return (EISCONN);
505 NGTLOCK(sc);
506 sc->hook = hook;
507 NGTUNLOCK(sc);
509 return (0);
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.
517 static int
518 ngt_connect(hook_p hook)
520 NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
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.
526 NG_HOOK_FORCE_QUEUE(hook);
527 return (0);
531 * Disconnect the hook
533 static int
534 ngt_disconnect(hook_p hook)
536 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
538 if (hook != sc->hook)
539 panic(__func__);
541 NGTLOCK(sc);
542 sc->hook = NULL;
543 NGTUNLOCK(sc);
545 return (0);
549 * Remove this node. The does the netgraph portion of the shutdown.
550 * This should only be called indirectly from ngt_close().
552 * tp->t_lsc is already NULL, so we should be protected from
553 * tty calls now.
555 static int
556 ngt_shutdown(node_p node)
558 const sc_p sc = NG_NODE_PRIVATE(node);
560 NGTLOCK(sc);
561 if (!(sc->flags & FLG_DIE)) {
562 NGTUNLOCK(sc);
563 return (EOPNOTSUPP);
565 NGTUNLOCK(sc);
567 /* Free resources */
568 _IF_DRAIN(&sc->outq);
569 mtx_destroy(&(sc)->outq.ifq_mtx);
570 m_freem(sc->m);
571 NG_NODE_UNREF(sc->node);
572 FREE(sc, M_NETGRAPH);
574 return (0);
578 * Receive incoming data from netgraph system. Put it on our
579 * output queue and start output if necessary.
581 static int
582 ngt_rcvdata(hook_p hook, item_p item)
584 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
585 struct mbuf *m;
586 int qlen;
588 if (hook != sc->hook)
589 panic(__func__);
591 NGI_GET_M(item, m);
592 NG_FREE_ITEM(item);
594 IF_LOCK(&sc->outq);
595 if (_IF_QFULL(&sc->outq)) {
596 _IF_DROP(&sc->outq);
597 IF_UNLOCK(&sc->outq);
598 NG_FREE_M(m);
599 return (ENOBUFS);
602 _IF_ENQUEUE(&sc->outq, m);
603 qlen = sc->outq.ifq_len;
604 IF_UNLOCK(&sc->outq);
607 * If qlen > 1, then we should already have a scheduled callout.
609 if (qlen == 1) {
610 mtx_lock(&Giant);
611 ngt_start(sc->tp);
612 mtx_unlock(&Giant);
615 return (0);
619 * Receive control message
621 static int
622 ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
624 const sc_p sc = NG_NODE_PRIVATE(node);
625 struct ng_mesg *msg, *resp = NULL;
626 int error = 0;
628 NGI_GET_MSG(item, msg);
629 switch (msg->header.typecookie) {
630 case NGM_TTY_COOKIE:
631 switch (msg->header.cmd) {
632 case NGM_TTY_SET_HOTCHAR:
634 int hotchar;
636 if (msg->header.arglen != sizeof(int))
637 ERROUT(EINVAL);
638 hotchar = *((int *) msg->data);
639 if (hotchar != (u_char) hotchar && hotchar != -1)
640 ERROUT(EINVAL);
641 sc->hotchar = hotchar; /* race condition is OK */
642 break;
644 case NGM_TTY_GET_HOTCHAR:
645 NG_MKRESPONSE(resp, msg, sizeof(int), M_WAITOK | M_NULLOK);
646 if (!resp)
647 ERROUT(ENOMEM);
648 /* Race condition here is OK */
649 *((int *) resp->data) = sc->hotchar;
650 break;
651 default:
652 ERROUT(EINVAL);
654 break;
655 default:
656 ERROUT(EINVAL);
658 done:
659 NG_RESPOND_MSG(error, node, item, resp);
660 NG_FREE_MSG(msg);
661 return (error);
664 /******************************************************************
665 INITIALIZATION
666 ******************************************************************/
669 * Handle loading and unloading for this node type
671 static int
672 ngt_mod_event(module_t mod, int event, void *data)
674 int error = 0;
676 switch (event) {
677 case MOD_LOAD:
679 /* Register line discipline */
680 mtx_lock(&Giant);
681 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
682 mtx_unlock(&Giant);
683 log(LOG_ERR, "%s: can't register line discipline",
684 __func__);
685 return (EIO);
687 mtx_unlock(&Giant);
688 break;
690 case MOD_UNLOAD:
692 /* Unregister line discipline */
693 mtx_lock(&Giant);
694 ldisc_deregister(ngt_ldisc);
695 mtx_unlock(&Giant);
696 break;
698 default:
699 error = EOPNOTSUPP;
700 break;
702 return (error);