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