]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/netgraph/ng_tty.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / netgraph / ng_tty.c
1 /*
2  * ng_tty.c
3  */
4
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.
19  * 
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.
37  *
38  * Author: Archie Cobbs <archie@freebsd.org>
39  *
40  * Updated by Andrew Thompson <thompsa@FreeBSD.org> for MPSAFE TTY.
41  *
42  * $FreeBSD$
43  * $Whistle: ng_tty.c,v 1.21 1999/11/01 09:24:52 julian Exp $
44  */
45
46 /*
47  * This file implements TTY hooks to link in to the netgraph system.  The node
48  * is created and then passed the callers opened TTY file descriptor number to
49  * NGM_TTY_SET_TTY, this will hook the tty via ttyhook_register().
50  *
51  * Incoming data is delivered directly to ng_tty via the TTY bypass hook as a
52  * buffer pointer and length, this is converted to a mbuf and passed to the
53  * peer.
54  *
55  * If the TTY device does not support bypass then incoming characters are
56  * delivered to the hook one at a time, each in its own mbuf. You may
57  * optionally define a ``hotchar,'' which causes incoming characters to be
58  * buffered up until either the hotchar is seen or the mbuf is full (MHLEN
59  * bytes). Then all buffered characters are immediately delivered.
60  */
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/conf.h>
65 #include <sys/errno.h>
66 #include <sys/fcntl.h>
67 #include <sys/ioccom.h>
68 #include <sys/kernel.h>
69 #include <sys/malloc.h>
70 #include <sys/mbuf.h>
71 #include <sys/priv.h>
72 #include <sys/socket.h>
73 #include <sys/syslog.h>
74 #include <sys/tty.h>
75 #include <sys/ttycom.h>
76 #include <sys/proc.h>
77
78 #include <net/if.h>
79 #include <net/if_var.h>
80
81 #include <netgraph/ng_message.h>
82 #include <netgraph/netgraph.h>
83 #include <netgraph/ng_tty.h>
84
85 /* Per-node private info */
86 struct ngt_softc {
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         size_t          outqlen;        /* Number of bytes in outq */
92         struct mbuf     *m;             /* Incoming non-bypass data buffer */
93         short           hotchar;        /* Hotchar, or -1 if none */
94         u_int           flags;          /* Flags */
95 };
96 typedef struct ngt_softc *sc_p;
97
98 /* Flags */
99 #define FLG_DEBUG               0x0002
100
101 /* Netgraph methods */
102 static ng_constructor_t         ngt_constructor;
103 static ng_rcvmsg_t              ngt_rcvmsg;
104 static ng_shutdown_t            ngt_shutdown;
105 static ng_newhook_t             ngt_newhook;
106 static ng_connect_t             ngt_connect;
107 static ng_rcvdata_t             ngt_rcvdata;
108 static ng_disconnect_t          ngt_disconnect;
109
110 #define ERROUT(x)               do { error = (x); goto done; } while (0)
111
112 static th_getc_inject_t         ngt_getc_inject;
113 static th_getc_poll_t           ngt_getc_poll;
114 static th_rint_t                ngt_rint;
115 static th_rint_bypass_t         ngt_rint_bypass;
116 static th_rint_poll_t           ngt_rint_poll;
117
118 static struct ttyhook ngt_hook = {
119         .th_getc_inject = ngt_getc_inject,
120         .th_getc_poll = ngt_getc_poll,
121         .th_rint = ngt_rint,
122         .th_rint_bypass = ngt_rint_bypass,
123         .th_rint_poll = ngt_rint_poll,
124 };
125
126 /* Netgraph node type descriptor */
127 static struct ng_type typestruct = {
128         .version =      NG_ABI_VERSION,
129         .name =         NG_TTY_NODE_TYPE,
130         .constructor =  ngt_constructor,
131         .rcvmsg =       ngt_rcvmsg,
132         .shutdown =     ngt_shutdown,
133         .newhook =      ngt_newhook,
134         .connect =      ngt_connect,
135         .rcvdata =      ngt_rcvdata,
136         .disconnect =   ngt_disconnect,
137 };
138 NETGRAPH_INIT(tty, &typestruct);
139
140 #define NGTLOCK(sc)     IF_LOCK(&sc->outq)
141 #define NGTUNLOCK(sc)   IF_UNLOCK(&sc->outq)
142
143 /******************************************************************
144                     NETGRAPH NODE METHODS
145 ******************************************************************/
146
147 /*
148  * Initialize a new node of this type.
149  *
150  * We only allow nodes to be created as a result of setting
151  * the line discipline on a tty, so always return an error if not.
152  */
153 static int
154 ngt_constructor(node_p node)
155 {
156         sc_p sc;
157
158         /* Allocate private structure */
159         sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
160         if (sc == NULL)
161                 return (ENOMEM);
162
163         NG_NODE_SET_PRIVATE(node, sc);
164         sc->node = node;
165
166         mtx_init(&sc->outq.ifq_mtx, "ng_tty node+queue", NULL, MTX_DEF);
167         IFQ_SET_MAXLEN(&sc->outq, IFQ_MAXLEN);
168
169         return (0);
170 }
171
172 /*
173  * Add a new hook. There can only be one.
174  */
175 static int
176 ngt_newhook(node_p node, hook_p hook, const char *name)
177 {
178         const sc_p sc = NG_NODE_PRIVATE(node);
179
180         if (strcmp(name, NG_TTY_HOOK))
181                 return (EINVAL);
182
183         if (sc->hook)
184                 return (EISCONN);
185
186         NGTLOCK(sc);
187         sc->hook = hook;
188         NGTUNLOCK(sc);
189
190         return (0);
191 }
192
193 /*
194  * Set the hook into queueing mode (for outgoing packets),
195  * so that we wont deliver mbuf thru the whole graph holding
196  * tty locks.
197  */
198 static int
199 ngt_connect(hook_p hook)
200 {
201         NG_HOOK_FORCE_QUEUE(hook);
202         return (0);
203 }
204
205 /*
206  * Disconnect the hook
207  */
208 static int
209 ngt_disconnect(hook_p hook)
210 {
211         const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
212
213         if (hook != sc->hook)
214                 panic(__func__);
215
216         NGTLOCK(sc);
217         sc->hook = NULL;
218         NGTUNLOCK(sc);
219
220         return (0);
221 }
222
223 /*
224  * Remove this node. The does the netgraph portion of the shutdown.
225  */
226 static int
227 ngt_shutdown(node_p node)
228 {
229         const sc_p sc = NG_NODE_PRIVATE(node);
230         struct tty *tp;
231
232         tp = sc->tp;
233         if (tp != NULL) {
234                 tty_lock(tp);
235                 ttyhook_unregister(tp);
236         }
237         /* Free resources */
238         IF_DRAIN(&sc->outq);
239         mtx_destroy(&(sc)->outq.ifq_mtx);
240         NG_NODE_UNREF(sc->node);
241         free(sc, M_NETGRAPH);
242
243         return (0);
244 }
245
246 /*
247  * Receive control message
248  */
249 static int
250 ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
251 {
252         struct proc *p;
253         const sc_p sc = NG_NODE_PRIVATE(node);
254         struct ng_mesg *msg, *resp = NULL;
255         int error = 0;
256
257         NGI_GET_MSG(item, msg);
258         switch (msg->header.typecookie) {
259         case NGM_TTY_COOKIE:
260                 switch (msg->header.cmd) {
261                 case NGM_TTY_SET_TTY:
262                         if (sc->tp != NULL)
263                                 return (EBUSY);
264                         
265                         p = pfind(((int *)msg->data)[0]);
266                         if (p == NULL || (p->p_flag & P_WEXIT))
267                                 return (ESRCH);
268                         _PHOLD(p);
269                         PROC_UNLOCK(p);
270                         error = ttyhook_register(&sc->tp, p, ((int *)msg->data)[1],
271                             &ngt_hook, sc);
272                         PRELE(p);
273                         if (error != 0)
274                                 return (error);
275                         break;
276                 case NGM_TTY_SET_HOTCHAR:
277                     {
278                         int     hotchar;
279
280                         if (msg->header.arglen != sizeof(int))
281                                 ERROUT(EINVAL);
282                         hotchar = *((int *) msg->data);
283                         if (hotchar != (u_char) hotchar && hotchar != -1)
284                                 ERROUT(EINVAL);
285                         sc->hotchar = hotchar;  /* race condition is OK */
286                         break;
287                     }
288                 case NGM_TTY_GET_HOTCHAR:
289                         NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT);
290                         if (!resp)
291                                 ERROUT(ENOMEM);
292                         /* Race condition here is OK */
293                         *((int *) resp->data) = sc->hotchar;
294                         break;
295                 default:
296                         ERROUT(EINVAL);
297                 }
298                 break;
299         default:
300                 ERROUT(EINVAL);
301         }
302 done:
303         NG_RESPOND_MSG(error, node, item, resp);
304         NG_FREE_MSG(msg);
305         return (error);
306 }
307
308 /*
309  * Receive incoming data from netgraph system. Put it on our
310  * output queue and start output if necessary.
311  */
312 static int
313 ngt_rcvdata(hook_p hook, item_p item)
314 {
315         const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
316         struct tty *tp = sc->tp;
317         struct mbuf *m;
318
319         if (hook != sc->hook)
320                 panic(__func__);
321
322         NGI_GET_M(item, m);
323         NG_FREE_ITEM(item);
324
325         if (tp == NULL) {
326                 NG_FREE_M(m);
327                 return (ENXIO);
328         }
329
330         IF_LOCK(&sc->outq);
331         if (_IF_QFULL(&sc->outq)) {
332                 _IF_DROP(&sc->outq);
333                 IF_UNLOCK(&sc->outq);
334                 NG_FREE_M(m);
335                 return (ENOBUFS);
336         }
337
338         _IF_ENQUEUE(&sc->outq, m);
339         sc->outqlen += m->m_pkthdr.len;
340         IF_UNLOCK(&sc->outq);
341
342         /* notify the TTY that data is ready */
343         tty_lock(tp);
344         if (!tty_gone(tp))
345                 ttydevsw_outwakeup(tp);
346         tty_unlock(tp);
347
348         return (0);
349 }
350
351 static size_t
352 ngt_getc_inject(struct tty *tp, void *buf, size_t len)
353 {
354         sc_p sc = ttyhook_softc(tp);
355         size_t total = 0;
356         int length;
357
358         while (len) {
359                 struct mbuf *m;
360
361                 /* Remove first mbuf from queue */
362                 IF_DEQUEUE(&sc->outq, m);
363                 if (m == NULL)
364                         break;
365
366                 /* Send as much of it as possible */
367                 while (m != NULL) {
368                         length = min(m->m_len, len);
369                         memcpy((char *)buf + total, mtod(m, char *), length);
370
371                         m->m_data += length;
372                         m->m_len -= length;
373                         total += length;
374                         len -= length;
375
376                         if (m->m_len > 0)
377                                 break;  /* device can't take any more */
378                         m = m_free(m);
379                 }
380
381                 /* Put remainder of mbuf chain (if any) back on queue */
382                 if (m != NULL) {
383                         IF_PREPEND(&sc->outq, m);
384                         break;
385                 }
386         }
387         IF_LOCK(&sc->outq);
388         sc->outqlen -= total;
389         IF_UNLOCK(&sc->outq);
390         MPASS(sc->outqlen >= 0);
391
392         return (total);
393 }
394
395 static size_t
396 ngt_getc_poll(struct tty *tp)
397 {
398         sc_p sc = ttyhook_softc(tp);
399
400         return (sc->outqlen);
401 }
402
403 /*
404  * Optimised TTY input.
405  *
406  * We get a buffer pointer to hopefully a complete data frame. Do not check for
407  * the hotchar, just pass it on.
408  */
409 static size_t
410 ngt_rint_bypass(struct tty *tp, const void *buf, size_t len)
411 {
412         sc_p sc = ttyhook_softc(tp);
413         node_p node = sc->node;
414         struct mbuf *m, *mb;
415         size_t total = 0;
416         int error = 0, length;
417
418         tty_lock_assert(tp, MA_OWNED);
419
420         if (sc->hook == NULL)
421                 return (0);
422
423         m = m_getm2(NULL, len, M_DONTWAIT, MT_DATA, M_PKTHDR);
424         if (m == NULL) {
425                 if (sc->flags & FLG_DEBUG)
426                         log(LOG_ERR,
427                             "%s: can't get mbuf\n", NG_NODE_NAME(node));
428                 return (0);
429         }
430         m->m_pkthdr.rcvif = NULL;
431
432         for (mb = m; mb != NULL; mb = mb->m_next) {
433                 length = min(M_TRAILINGSPACE(mb), len - total);
434
435                 memcpy(mtod(m, char *), (const char *)buf + total, length);
436                 mb->m_len = length;
437                 total += length;
438                 m->m_pkthdr.len += length;
439         }
440         if (sc->m != NULL) {
441                 /*
442                  * Odd, we have changed from non-bypass to bypass. It is
443                  * unlikely but not impossible, flush the data first.
444                  */
445                 sc->m->m_data = sc->m->m_pktdat;
446                 NG_SEND_DATA_ONLY(error, sc->hook, sc->m);
447                 sc->m = NULL;
448         }
449         NG_SEND_DATA_ONLY(error, sc->hook, m);
450
451         return (total);
452 }
453
454 /*
455  * Receive data coming from the device one char at a time, when it is not in
456  * bypass mode.
457  */
458 static int
459 ngt_rint(struct tty *tp, char c, int flags)
460 {
461         sc_p sc = ttyhook_softc(tp);
462         node_p node = sc->node;
463         struct mbuf *m;
464         int error = 0;
465
466         tty_lock_assert(tp, MA_OWNED);
467
468         if (sc->hook == NULL)
469                 return (0);
470
471         if (flags != 0) {
472                 /* framing error or overrun on this char */
473                 if (sc->flags & FLG_DEBUG)
474                         log(LOG_DEBUG, "%s: line error %x\n",
475                             NG_NODE_NAME(node), flags);
476                 return (0);
477         }
478
479         /* Get a new header mbuf if we need one */
480         if (!(m = sc->m)) {
481                 MGETHDR(m, M_DONTWAIT, MT_DATA);
482                 if (!m) {
483                         if (sc->flags & FLG_DEBUG)
484                                 log(LOG_ERR,
485                                     "%s: can't get mbuf\n", NG_NODE_NAME(node));
486                         return (ENOBUFS);
487                 }
488                 m->m_len = m->m_pkthdr.len = 0;
489                 m->m_pkthdr.rcvif = NULL;
490                 sc->m = m;
491         }
492
493         /* Add char to mbuf */
494         *mtod(m, u_char *) = c;
495         m->m_data++;
496         m->m_len++;
497         m->m_pkthdr.len++;
498
499         /* Ship off mbuf if it's time */
500         if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) {
501                 m->m_data = m->m_pktdat;
502                 sc->m = NULL;
503                 NG_SEND_DATA_ONLY(error, sc->hook, m);  /* Will queue */
504         }
505
506         return (error);
507 }
508
509 static size_t
510 ngt_rint_poll(struct tty *tp)
511 {
512         /* We can always accept input */
513         return (1);
514 }
515