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