xref: /dflybsd-src/sys/netgraph7/tty/ng_tty.c (revision 2b3f93ea6d1f70880f3e87f3c2cbe0dc0bfc9332)
1e71e017bSNuno Antunes /*-
2e71e017bSNuno Antunes  * (MPSAFE)
3e71e017bSNuno Antunes  *
4e71e017bSNuno Antunes  * ng_tty.c
5e71e017bSNuno Antunes  *
6e71e017bSNuno Antunes  * Copyright (c) 1996-1999 Whistle Communications, Inc.
7e71e017bSNuno Antunes  * All rights reserved.
8e71e017bSNuno Antunes  *
9e71e017bSNuno Antunes  * Subject to the following obligations and disclaimer of warranty, use and
10e71e017bSNuno Antunes  * redistribution of this software, in source or object code forms, with or
11e71e017bSNuno Antunes  * without modifications are expressly permitted by Whistle Communications;
12e71e017bSNuno Antunes  * provided, however, that:
13e71e017bSNuno Antunes  * 1. Any and all reproductions of the source or object code must include the
14e71e017bSNuno Antunes  *    copyright notice above and the following disclaimer of warranties; and
15e71e017bSNuno Antunes  * 2. No rights are granted, in any manner or form, to use Whistle
16e71e017bSNuno Antunes  *    Communications, Inc. trademarks, including the mark "WHISTLE
17e71e017bSNuno Antunes  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18e71e017bSNuno Antunes  *    such appears in the above copyright notice or in the software.
19e71e017bSNuno Antunes  *
20e71e017bSNuno Antunes  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21e71e017bSNuno Antunes  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22e71e017bSNuno Antunes  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23e71e017bSNuno Antunes  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24e71e017bSNuno Antunes  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25e71e017bSNuno Antunes  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26e71e017bSNuno Antunes  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27e71e017bSNuno Antunes  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28e71e017bSNuno Antunes  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29e71e017bSNuno Antunes  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30e71e017bSNuno Antunes  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31e71e017bSNuno Antunes  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32e71e017bSNuno Antunes  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33e71e017bSNuno Antunes  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34e71e017bSNuno Antunes  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35e71e017bSNuno Antunes  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36e71e017bSNuno Antunes  * OF SUCH DAMAGE.
37e71e017bSNuno Antunes  *
38e71e017bSNuno Antunes  * Author: Archie Cobbs <archie@freebsd.org>
39e71e017bSNuno Antunes  *
40e71e017bSNuno Antunes  * $FreeBSD: src/sys/netgraph/ng_tty.c,v 1.37 2006/11/06 13:42:03 rwatson Exp $
41e71e017bSNuno Antunes  * $Whistle: ng_tty.c,v 1.21 1999/11/01 09:24:52 julian Exp $
42e71e017bSNuno Antunes  */
43e71e017bSNuno Antunes 
44e71e017bSNuno Antunes /*
45e71e017bSNuno Antunes  * This file implements a terminal line discipline that is also a
46e71e017bSNuno Antunes  * netgraph node. Installing this line discipline on a terminal device
47e71e017bSNuno Antunes  * instantiates a new netgraph node of this type, which allows access
48e71e017bSNuno Antunes  * to the device via the "hook" hook of the node.
49e71e017bSNuno Antunes  *
50e71e017bSNuno Antunes  * Once the line discipline is installed, you can find out the name
51e71e017bSNuno Antunes  * of the corresponding netgraph node via a NGIOCGINFO ioctl().
52e71e017bSNuno Antunes  *
53e71e017bSNuno Antunes  * Incoming characters are delievered to the hook one at a time, each
54e71e017bSNuno Antunes  * in its own mbuf. You may optionally define a ``hotchar,'' which causes
55e71e017bSNuno Antunes  * incoming characters to be buffered up until either the hotchar is
56e71e017bSNuno Antunes  * seen or the mbuf is full (MHLEN bytes). Then all buffered characters
57e71e017bSNuno Antunes  * are immediately delivered.
58e71e017bSNuno Antunes  */
59e71e017bSNuno Antunes 
60e71e017bSNuno Antunes #include <sys/param.h>
61e71e017bSNuno Antunes #include <sys/systm.h>
62e71e017bSNuno Antunes #include <sys/conf.h>
63e71e017bSNuno Antunes #include <sys/errno.h>
64e71e017bSNuno Antunes #include <sys/fcntl.h>
65e71e017bSNuno Antunes #include <sys/kernel.h>
66e71e017bSNuno Antunes #include <sys/malloc.h>
67e71e017bSNuno Antunes #include <sys/mbuf.h>
68*2b3f93eaSMatthew Dillon #include <sys/caps.h>
69e71e017bSNuno Antunes #include <sys/socket.h>
70e71e017bSNuno Antunes #include <sys/syslog.h>
71e71e017bSNuno Antunes #include <sys/tty.h>
72e71e017bSNuno Antunes #include <sys/ttycom.h>
73e71e017bSNuno Antunes 
74e71e017bSNuno Antunes #include <net/if.h>
75e71e017bSNuno Antunes #include <net/if_var.h>
76e71e017bSNuno Antunes 
77e71e017bSNuno Antunes #include <netgraph7/netgraph.h>
78e71e017bSNuno Antunes #include <netgraph7/ng_message.h>
79e71e017bSNuno Antunes #include "ng_tty.h"
80e71e017bSNuno Antunes 
81e71e017bSNuno Antunes /* Misc defs */
82e71e017bSNuno Antunes #define MAX_MBUFQ		3	/* Max number of queued mbufs */
83e71e017bSNuno Antunes #define NGT_HIWATER		400	/* High water mark on output */
84e71e017bSNuno Antunes 
85e71e017bSNuno Antunes /* Per-node private info */
86e71e017bSNuno Antunes struct ngt_sc {
87e71e017bSNuno Antunes 	struct	tty *tp;		/* Terminal device */
88e71e017bSNuno Antunes 	node_p	node;			/* Netgraph node */
89e71e017bSNuno Antunes 	hook_p	hook;			/* Netgraph hook */
90e71e017bSNuno Antunes 	struct	ifqueue outq;		/* Queue of outgoing data */
91e71e017bSNuno Antunes 	struct	mbuf *m;		/* Incoming data buffer */
92e71e017bSNuno Antunes 	short	hotchar;		/* Hotchar, or -1 if none */
93e71e017bSNuno Antunes 	u_int	flags;			/* Flags */
94e71e017bSNuno Antunes 	struct	callout	chand;		/* See man timeout(9) */
95e71e017bSNuno Antunes };
96e71e017bSNuno Antunes typedef struct ngt_sc *sc_p;
97e71e017bSNuno Antunes 
98e71e017bSNuno Antunes /* Flags */
99e71e017bSNuno Antunes #define FLG_DEBUG		0x0002
100e71e017bSNuno Antunes #define	FLG_DIE			0x0004
101e71e017bSNuno Antunes 
102e71e017bSNuno Antunes /* Line discipline methods */
103e71e017bSNuno Antunes static int	ngt_open(struct cdev *dev, struct tty *tp);
104e71e017bSNuno Antunes static int	ngt_close(struct tty *tp, int flag);
105e71e017bSNuno Antunes static int	ngt_read(struct tty *tp, struct uio *uio, int flag);
106e71e017bSNuno Antunes static int	ngt_write(struct tty *tp, struct uio *uio, int flag);
107e71e017bSNuno Antunes static int	ngt_tioctl(struct tty *tp,
108e71e017bSNuno Antunes 		    u_long cmd, caddr_t data, int flag, struct ucred *cred);
109e71e017bSNuno Antunes static int	ngt_input(int c, struct tty *tp);
110e71e017bSNuno Antunes static int	ngt_start(struct tty *tp);
111e71e017bSNuno Antunes 
112e71e017bSNuno Antunes /* Netgraph methods */
113e71e017bSNuno Antunes static ng_constructor_t	ngt_constructor;
114e71e017bSNuno Antunes static ng_rcvmsg_t	ngt_rcvmsg;
115e71e017bSNuno Antunes static ng_shutdown_t	ngt_shutdown;
116e71e017bSNuno Antunes static ng_newhook_t	ngt_newhook;
117e71e017bSNuno Antunes static ng_connect_t	ngt_connect;
118e71e017bSNuno Antunes static ng_rcvdata_t	ngt_rcvdata;
119e71e017bSNuno Antunes static ng_disconnect_t	ngt_disconnect;
120e71e017bSNuno Antunes static int		ngt_mod_event(module_t mod, int event, void *data);
121e71e017bSNuno Antunes 
122e71e017bSNuno Antunes /* Other stuff */
123e71e017bSNuno Antunes static void	ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2);
124e71e017bSNuno Antunes 
125e71e017bSNuno Antunes #define ERROUT(x)		do { error = (x); goto done; } while (0)
126e71e017bSNuno Antunes 
127e71e017bSNuno Antunes /* Line discipline descriptor */
128e71e017bSNuno Antunes static struct linesw ngt_disc = {
129e71e017bSNuno Antunes 	.l_open =	ngt_open,
130e71e017bSNuno Antunes 	.l_close =	ngt_close,
131e71e017bSNuno Antunes 	.l_read =	ngt_read,
132e71e017bSNuno Antunes 	.l_write =	ngt_write,
133e71e017bSNuno Antunes 	.l_ioctl =	ngt_tioctl,
134e71e017bSNuno Antunes 	.l_rint =	ngt_input,
135e71e017bSNuno Antunes 	.l_start =	ngt_start,
136e71e017bSNuno Antunes 	.l_modem =	ttymodem,
137e71e017bSNuno Antunes };
138e71e017bSNuno Antunes 
139e71e017bSNuno Antunes /* Netgraph node type descriptor */
140e71e017bSNuno Antunes static struct ng_type typestruct = {
141e71e017bSNuno Antunes 	.version =	NG_ABI_VERSION,
142e71e017bSNuno Antunes 	.name =		NG_TTY_NODE_TYPE,
143e71e017bSNuno Antunes 	.mod_event =	ngt_mod_event,
144e71e017bSNuno Antunes 	.constructor =	ngt_constructor,
145e71e017bSNuno Antunes 	.rcvmsg =	ngt_rcvmsg,
146e71e017bSNuno Antunes 	.shutdown =	ngt_shutdown,
147e71e017bSNuno Antunes 	.newhook =	ngt_newhook,
148e71e017bSNuno Antunes 	.connect =	ngt_connect,
149e71e017bSNuno Antunes 	.rcvdata =	ngt_rcvdata,
150e71e017bSNuno Antunes 	.disconnect =	ngt_disconnect,
151e71e017bSNuno Antunes };
152e71e017bSNuno Antunes NETGRAPH_INIT(tty, &typestruct);
153e71e017bSNuno Antunes 
154e71e017bSNuno Antunes /*
155e71e017bSNuno Antunes  * Locking:
156e71e017bSNuno Antunes  *
157e71e017bSNuno Antunes  * - node private data and tp->t_lsc is protected by mutex in struct
158e71e017bSNuno Antunes  *   ifqueue, locking is done using IF_XXX() macros.
159e71e017bSNuno Antunes  * - in all tty methods we should acquire node ifqueue mutex, when accessing
160e71e017bSNuno Antunes  *   private data.
161e71e017bSNuno Antunes  * - in _rcvdata() we should use locked versions of IF_{EN,DE}QUEUE() since
162e71e017bSNuno Antunes  *   we may have multiple _rcvdata() threads.
163e71e017bSNuno Antunes  * - when calling any of tty methods from netgraph methods, we should
164e71e017bSNuno Antunes  *   acquire tty locking (now Giant).
165e71e017bSNuno Antunes  *
166e71e017bSNuno Antunes  * - ngt_unit is incremented atomically.
167e71e017bSNuno Antunes  */
168e71e017bSNuno Antunes 
169e71e017bSNuno Antunes static int ngt_unit;
170e71e017bSNuno Antunes static int ngt_ldisc;
171e71e017bSNuno Antunes 
172e71e017bSNuno Antunes /******************************************************************
173e71e017bSNuno Antunes 		    LINE DISCIPLINE METHODS
174e71e017bSNuno Antunes ******************************************************************/
175e71e017bSNuno Antunes 
176e71e017bSNuno Antunes /*
177e71e017bSNuno Antunes  * Set our line discipline on the tty.
178e71e017bSNuno Antunes  * Called from device open routine or ttioctl()
179e71e017bSNuno Antunes  */
180e71e017bSNuno Antunes static int
ngt_open(struct cdev * dev,struct tty * tp)181e71e017bSNuno Antunes ngt_open(struct cdev *dev, struct tty *tp)
182e71e017bSNuno Antunes {
183e71e017bSNuno Antunes 	char name[sizeof(NG_TTY_NODE_TYPE) + 8];
184e71e017bSNuno Antunes 	sc_p sc;
185e71e017bSNuno Antunes 	int error;
186e71e017bSNuno Antunes 
187e71e017bSNuno Antunes 	/* Super-user only */
188*2b3f93eaSMatthew Dillon 	error = caps_priv_check_self(SYSCAP_NONET_NETGRAPH);
189e71e017bSNuno Antunes 	if (error)
190e71e017bSNuno Antunes 		return (error);
191e71e017bSNuno Antunes 
192e71e017bSNuno Antunes 	/* Initialize private struct */
193e71e017bSNuno Antunes 	sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
194e71e017bSNuno Antunes 
1952efb75f3SMatthew Dillon 	lwkt_gettoken(&tp->t_token);
196e71e017bSNuno Antunes 	sc->tp = tp;
197e71e017bSNuno Antunes 	sc->hotchar = NG_TTY_DFL_HOTCHAR;
198e71e017bSNuno Antunes 	sc->outq.ifq_maxlen = MAX_MBUFQ;
199e71e017bSNuno Antunes 
200e71e017bSNuno Antunes 	/* Setup netgraph node */
201e71e017bSNuno Antunes 	error = ng_make_node_common(&typestruct, &sc->node);
202e71e017bSNuno Antunes 	if (error) {
203e71e017bSNuno Antunes 		kfree(sc, M_NETGRAPH);
2042efb75f3SMatthew Dillon 		lwkt_reltoken(&tp->t_token);
205e71e017bSNuno Antunes 		return (error);
206e71e017bSNuno Antunes 	}
207e71e017bSNuno Antunes 
208e71e017bSNuno Antunes 	atomic_add_int(&ngt_unit, 1);
209a62226e4SSascha Wildner 	ksnprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit);
210e71e017bSNuno Antunes 
211e71e017bSNuno Antunes 	/* Assign node its name */
212e71e017bSNuno Antunes 	if ((error = ng_name_node(sc->node, name))) {
213e71e017bSNuno Antunes 		sc->flags |= FLG_DIE;
214e71e017bSNuno Antunes 		NG_NODE_UNREF(sc->node);
215e71e017bSNuno Antunes 		log(LOG_ERR, "%s: node name exists?\n", name);
2162efb75f3SMatthew Dillon 		lwkt_reltoken(&tp->t_token);
217e71e017bSNuno Antunes 		return (error);
218e71e017bSNuno Antunes 	}
219e71e017bSNuno Antunes 
220e71e017bSNuno Antunes 	/* Set back pointers */
221e71e017bSNuno Antunes 	NG_NODE_SET_PRIVATE(sc->node, sc);
222e71e017bSNuno Antunes 	tp->t_sc = sc;
223e71e017bSNuno Antunes 
224e71e017bSNuno Antunes 	callout_init_mp(&sc->chand);
225e71e017bSNuno Antunes 
226e71e017bSNuno Antunes 	/*
227e71e017bSNuno Antunes 	 * Pre-allocate cblocks to the an appropriate amount.
228e71e017bSNuno Antunes 	 * I'm not sure what is appropriate.
229e71e017bSNuno Antunes 	 */
230e71e017bSNuno Antunes 	ttyflush(tp, FREAD | FWRITE);
2314725869bSMatthew Dillon 	clist_alloc_cblocks(&tp->t_canq, 0);
2324725869bSMatthew Dillon 	clist_alloc_cblocks(&tp->t_rawq, 0);
2334725869bSMatthew Dillon 	clist_alloc_cblocks(&tp->t_outq, MLEN + NGT_HIWATER);
234e71e017bSNuno Antunes 
2352efb75f3SMatthew Dillon 	lwkt_reltoken(&tp->t_token);
236e71e017bSNuno Antunes 	return (error);
237e71e017bSNuno Antunes }
238e71e017bSNuno Antunes 
239e71e017bSNuno Antunes /*
240e71e017bSNuno Antunes  * Line specific close routine, called from device close routine
241e71e017bSNuno Antunes  * and from ttioctl. This causes the node to be destroyed as well.
242e71e017bSNuno Antunes  */
243e71e017bSNuno Antunes static int
ngt_close(struct tty * tp,int flag)244e71e017bSNuno Antunes ngt_close(struct tty *tp, int flag)
245e71e017bSNuno Antunes {
246e71e017bSNuno Antunes 	const sc_p sc = (sc_p) tp->t_sc;
247e71e017bSNuno Antunes 
2482efb75f3SMatthew Dillon 	lwkt_gettoken(&tp->t_token);
249e71e017bSNuno Antunes 	ttyflush(tp, FREAD | FWRITE);
250e71e017bSNuno Antunes 	clist_free_cblocks(&tp->t_outq);
251e71e017bSNuno Antunes 	if (sc != NULL) {
252e71e017bSNuno Antunes 		if (callout_pending(&sc->chand))
253e71e017bSNuno Antunes 			ng_uncallout(&sc->chand, sc->node);
254e71e017bSNuno Antunes 		tp->t_sc = NULL;
255e71e017bSNuno Antunes 		sc->flags |= FLG_DIE;
256e71e017bSNuno Antunes 		ng_rmnode_self(sc->node);
257e71e017bSNuno Antunes 	}
2582efb75f3SMatthew Dillon 	lwkt_reltoken(&tp->t_token);
259e71e017bSNuno Antunes 	return (0);
260e71e017bSNuno Antunes }
261e71e017bSNuno Antunes 
262e71e017bSNuno Antunes /*
263e71e017bSNuno Antunes  * Once the device has been turned into a node, we don't allow reading.
264e71e017bSNuno Antunes  */
265e71e017bSNuno Antunes static int
ngt_read(struct tty * tp,struct uio * uio,int flag)266e71e017bSNuno Antunes ngt_read(struct tty *tp, struct uio *uio, int flag)
267e71e017bSNuno Antunes {
268e71e017bSNuno Antunes 	return (EIO);
269e71e017bSNuno Antunes }
270e71e017bSNuno Antunes 
271e71e017bSNuno Antunes /*
272e71e017bSNuno Antunes  * Once the device has been turned into a node, we don't allow writing.
273e71e017bSNuno Antunes  */
274e71e017bSNuno Antunes static int
ngt_write(struct tty * tp,struct uio * uio,int flag)275e71e017bSNuno Antunes ngt_write(struct tty *tp, struct uio *uio, int flag)
276e71e017bSNuno Antunes {
277e71e017bSNuno Antunes 	return (EIO);
278e71e017bSNuno Antunes }
279e71e017bSNuno Antunes 
280e71e017bSNuno Antunes /*
281e71e017bSNuno Antunes  * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
282e71e017bSNuno Antunes  */
283e71e017bSNuno Antunes static int
ngt_tioctl(struct tty * tp,u_long cmd,caddr_t data,int flag,struct ucred * cred)284e71e017bSNuno Antunes ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
285e71e017bSNuno Antunes {
286e71e017bSNuno Antunes 	const sc_p sc = (sc_p) tp->t_sc;
287e71e017bSNuno Antunes 
288e71e017bSNuno Antunes 	if (sc == NULL) {
289e71e017bSNuno Antunes 		/* No node attached */
290e71e017bSNuno Antunes 		return (0);
291e71e017bSNuno Antunes 	}
292e71e017bSNuno Antunes 
2932efb75f3SMatthew Dillon 	lwkt_gettoken(&tp->t_token);
294e71e017bSNuno Antunes 	switch (cmd) {
295e71e017bSNuno Antunes 	case NGIOCGINFO:
296e71e017bSNuno Antunes 	    {
297e71e017bSNuno Antunes 		struct nodeinfo *const ni = (struct nodeinfo *) data;
298e71e017bSNuno Antunes 		const node_p node = sc->node;
299e71e017bSNuno Antunes 
300e71e017bSNuno Antunes 		bzero(ni, sizeof(*ni));
301e71e017bSNuno Antunes 		if (NG_NODE_HAS_NAME(node))
302e71e017bSNuno Antunes 			strncpy(ni->name, NG_NODE_NAME(node), sizeof(ni->name) - 1);
303e71e017bSNuno Antunes 		strncpy(ni->type, node->nd_type->name, sizeof(ni->type) - 1);
304e71e017bSNuno Antunes 		ni->id = (u_int32_t) ng_node2ID(node);
305e71e017bSNuno Antunes 		ni->hooks = NG_NODE_NUMHOOKS(node);
306e71e017bSNuno Antunes 		break;
307e71e017bSNuno Antunes 	    }
308e71e017bSNuno Antunes 	default:
3092efb75f3SMatthew Dillon 		lwkt_reltoken(&tp->t_token);
310e71e017bSNuno Antunes 		return (ENOIOCTL);
311e71e017bSNuno Antunes 	}
312e71e017bSNuno Antunes 
3132efb75f3SMatthew Dillon 	lwkt_reltoken(&tp->t_token);
314e71e017bSNuno Antunes 	return (0);
315e71e017bSNuno Antunes }
316e71e017bSNuno Antunes 
317e71e017bSNuno Antunes /*
318e71e017bSNuno Antunes  * Receive data coming from the device. We get one character at
319e71e017bSNuno Antunes  * a time, which is kindof silly.
320e71e017bSNuno Antunes  *
321e71e017bSNuno Antunes  * Full locking of softc is not required, since we are the only
322e71e017bSNuno Antunes  * user of sc->m.
323e71e017bSNuno Antunes  */
324e71e017bSNuno Antunes static int
ngt_input(int c,struct tty * tp)325e71e017bSNuno Antunes ngt_input(int c, struct tty *tp)
326e71e017bSNuno Antunes {
327e71e017bSNuno Antunes 	sc_p sc;
328e71e017bSNuno Antunes 	node_p node;
329e71e017bSNuno Antunes 	struct mbuf *m;
330e71e017bSNuno Antunes 	int error = 0;
331e71e017bSNuno Antunes 
3322efb75f3SMatthew Dillon 	lwkt_gettoken(&tp->t_token);
333e71e017bSNuno Antunes 	sc = (sc_p) tp->t_sc;
334e71e017bSNuno Antunes 	if (sc == NULL) {
335e71e017bSNuno Antunes 		/* No node attached */
3362efb75f3SMatthew Dillon 		lwkt_reltoken(&tp->t_token);
337e71e017bSNuno Antunes 		return (0);
338e71e017bSNuno Antunes 	}
339e71e017bSNuno Antunes 
340e71e017bSNuno Antunes 	node = sc->node;
341e71e017bSNuno Antunes 
342e71e017bSNuno Antunes 	if (tp != sc->tp)
343e71e017bSNuno Antunes 		panic("ngt_input");
344e71e017bSNuno Antunes 
345e71e017bSNuno Antunes 	/* Check for error conditions */
346e71e017bSNuno Antunes 	if ((tp->t_state & TS_CONNECTED) == 0) {
347e71e017bSNuno Antunes 		if (sc->flags & FLG_DEBUG)
348e71e017bSNuno Antunes 			log(LOG_DEBUG, "%s: no carrier\n", NG_NODE_NAME(node));
3492efb75f3SMatthew Dillon 		lwkt_reltoken(&tp->t_token);
350e71e017bSNuno Antunes 		return (0);
351e71e017bSNuno Antunes 	}
352e71e017bSNuno Antunes 	if (c & TTY_ERRORMASK) {
353e71e017bSNuno Antunes 		/* framing error or overrun on this char */
354e71e017bSNuno Antunes 		if (sc->flags & FLG_DEBUG)
355e71e017bSNuno Antunes 			log(LOG_DEBUG, "%s: line error %x\n",
356e71e017bSNuno Antunes 			    NG_NODE_NAME(node), c & TTY_ERRORMASK);
3572efb75f3SMatthew Dillon 		lwkt_reltoken(&tp->t_token);
358e71e017bSNuno Antunes 		return (0);
359e71e017bSNuno Antunes 	}
360e71e017bSNuno Antunes 	c &= TTY_CHARMASK;
361e71e017bSNuno Antunes 
362e71e017bSNuno Antunes 	/* Get a new header mbuf if we need one */
363e71e017bSNuno Antunes 	if (!(m = sc->m)) {
364b5523eacSSascha Wildner 		MGETHDR(m, M_NOWAIT, MT_DATA);
365e71e017bSNuno Antunes 		if (!m) {
366e71e017bSNuno Antunes 			if (sc->flags & FLG_DEBUG)
367e71e017bSNuno Antunes 				log(LOG_ERR,
368e71e017bSNuno Antunes 				    "%s: can't get mbuf\n", NG_NODE_NAME(node));
3692efb75f3SMatthew Dillon 			lwkt_reltoken(&tp->t_token);
370e71e017bSNuno Antunes 			return (ENOBUFS);
371e71e017bSNuno Antunes 		}
372e71e017bSNuno Antunes 		m->m_len = m->m_pkthdr.len = 0;
373e71e017bSNuno Antunes 		m->m_pkthdr.rcvif = NULL;
374e71e017bSNuno Antunes 		sc->m = m;
375e71e017bSNuno Antunes 	}
376e71e017bSNuno Antunes 
377e71e017bSNuno Antunes 	/* Add char to mbuf */
378e71e017bSNuno Antunes 	*mtod(m, u_char *) = c;
379e71e017bSNuno Antunes 	m->m_data++;
380e71e017bSNuno Antunes 	m->m_len++;
381e71e017bSNuno Antunes 	m->m_pkthdr.len++;
382e71e017bSNuno Antunes 
383e71e017bSNuno Antunes 	/* Ship off mbuf if it's time */
384e71e017bSNuno Antunes 	if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) {
385e71e017bSNuno Antunes 		m->m_data = m->m_pktdat;
386e71e017bSNuno Antunes 		sc->m = NULL;
387e71e017bSNuno Antunes 
388e71e017bSNuno Antunes 		/*
389e71e017bSNuno Antunes 		 * We have built our mbuf without checking that we actually
390e71e017bSNuno Antunes 		 * have a hook to send it. This was done to avoid
391e71e017bSNuno Antunes 		 * acquiring mutex on each character. Check now.
392e71e017bSNuno Antunes 		 *
393e71e017bSNuno Antunes 		 */
394e71e017bSNuno Antunes 
395e71e017bSNuno Antunes 		if (sc->hook == NULL) {
396e71e017bSNuno Antunes 			m_freem(m);
3972efb75f3SMatthew Dillon 			lwkt_reltoken(&tp->t_token);
398e71e017bSNuno Antunes 			return (0);		/* XXX: original behavior */
399e71e017bSNuno Antunes 		}
400e71e017bSNuno Antunes 		NG_SEND_DATA_ONLY(error, sc->hook, m);	/* Will queue */
401e71e017bSNuno Antunes 	}
402e71e017bSNuno Antunes 
4032efb75f3SMatthew Dillon 	lwkt_reltoken(&tp->t_token);
404e71e017bSNuno Antunes 	return (error);
405e71e017bSNuno Antunes }
406e71e017bSNuno Antunes 
407e71e017bSNuno Antunes /*
408e71e017bSNuno Antunes  * This is called when the device driver is ready for more output.
409e71e017bSNuno Antunes  * Also called from ngt_rcv_data() when a new mbuf is available for output.
410e71e017bSNuno Antunes  */
411e71e017bSNuno Antunes static int
ngt_start(struct tty * tp)412e71e017bSNuno Antunes ngt_start(struct tty *tp)
413e71e017bSNuno Antunes {
414e71e017bSNuno Antunes 	const sc_p sc = (sc_p) tp->t_sc;
415e71e017bSNuno Antunes 
4162efb75f3SMatthew Dillon 	lwkt_gettoken(&tp->t_token);
417e71e017bSNuno Antunes 	while (tp->t_outq.c_cc < NGT_HIWATER) {	/* XXX 2.2 specific ? */
418e71e017bSNuno Antunes 		struct mbuf *m;
419e71e017bSNuno Antunes 
420e71e017bSNuno Antunes 		/* Remove first mbuf from queue */
421e71e017bSNuno Antunes 		IF_DEQUEUE(&sc->outq, m);
422e71e017bSNuno Antunes 		if (m == NULL)
423e71e017bSNuno Antunes 			break;
424e71e017bSNuno Antunes 
425e71e017bSNuno Antunes 		/* Send as much of it as possible */
426e71e017bSNuno Antunes 		while (m != NULL) {
427e71e017bSNuno Antunes 			int     sent;
428e71e017bSNuno Antunes 
4294725869bSMatthew Dillon 			sent = m->m_len - clist_btoq(mtod(m, u_char *),
4304725869bSMatthew Dillon 						     m->m_len, &tp->t_outq);
431e71e017bSNuno Antunes 			m->m_data += sent;
432e71e017bSNuno Antunes 			m->m_len -= sent;
433e71e017bSNuno Antunes 			if (m->m_len > 0)
434e71e017bSNuno Antunes 				break;	/* device can't take no more */
435e71e017bSNuno Antunes 			m = m_free(m);
436e71e017bSNuno Antunes 		}
437e71e017bSNuno Antunes 
438e71e017bSNuno Antunes 		/* Put remainder of mbuf chain (if any) back on queue */
439e71e017bSNuno Antunes 		if (m != NULL) {
440e71e017bSNuno Antunes 			IF_PREPEND(&sc->outq, m);
441e71e017bSNuno Antunes 			break;
442e71e017bSNuno Antunes 		}
443e71e017bSNuno Antunes 	}
444e71e017bSNuno Antunes 
445e71e017bSNuno Antunes 	/* Call output process whether or not there is any output. We are
446e71e017bSNuno Antunes 	 * being called in lieu of ttstart and must do what it would. */
447e71e017bSNuno Antunes 	if (tp->t_oproc != NULL)
448e71e017bSNuno Antunes 		(*tp->t_oproc) (tp);
449e71e017bSNuno Antunes 
450e71e017bSNuno Antunes 	/* This timeout is needed for operation on a pseudo-tty, because the
451e71e017bSNuno Antunes 	 * pty code doesn't call pppstart after it has drained the t_outq. */
452e71e017bSNuno Antunes 	/* XXX: outq not locked */
453e71e017bSNuno Antunes 	if (!IF_QEMPTY(&sc->outq) && !callout_pending(&sc->chand))
454e71e017bSNuno Antunes 		ng_callout(&sc->chand, sc->node, NULL, 1, ngt_timeout, NULL, 0);
455e71e017bSNuno Antunes 
4562efb75f3SMatthew Dillon 	lwkt_reltoken(&tp->t_token);
457e71e017bSNuno Antunes 	return (0);
458e71e017bSNuno Antunes }
459e71e017bSNuno Antunes 
460e71e017bSNuno Antunes /*
461e71e017bSNuno Antunes  * We still have data to output to the device, so try sending more.
462e71e017bSNuno Antunes  */
463e71e017bSNuno Antunes static void
ngt_timeout(node_p node,hook_p hook,void * arg1,int arg2)464e71e017bSNuno Antunes ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2)
465e71e017bSNuno Antunes {
466e71e017bSNuno Antunes 	const sc_p sc = NG_NODE_PRIVATE(node);
4672efb75f3SMatthew Dillon 	struct tty *tp = sc->tp;
468e71e017bSNuno Antunes 
4692efb75f3SMatthew Dillon 	lwkt_gettoken(&tp->t_token);
470e71e017bSNuno Antunes 	ngt_start(sc->tp);
4712efb75f3SMatthew Dillon 	lwkt_reltoken(&tp->t_token);
472e71e017bSNuno Antunes }
473e71e017bSNuno Antunes 
474e71e017bSNuno Antunes /******************************************************************
475e71e017bSNuno Antunes 		    NETGRAPH NODE METHODS
476e71e017bSNuno Antunes ******************************************************************/
477e71e017bSNuno Antunes 
478e71e017bSNuno Antunes /*
479e71e017bSNuno Antunes  * Initialize a new node of this type.
480e71e017bSNuno Antunes  *
481e71e017bSNuno Antunes  * We only allow nodes to be created as a result of setting
482e71e017bSNuno Antunes  * the line discipline on a tty, so always return an error if not.
483e71e017bSNuno Antunes  */
484e71e017bSNuno Antunes static int
ngt_constructor(node_p node)485e71e017bSNuno Antunes ngt_constructor(node_p node)
486e71e017bSNuno Antunes {
487e71e017bSNuno Antunes 	return (EOPNOTSUPP);
488e71e017bSNuno Antunes }
489e71e017bSNuno Antunes 
490e71e017bSNuno Antunes /*
491e71e017bSNuno Antunes  * Add a new hook. There can only be one.
492e71e017bSNuno Antunes  */
493e71e017bSNuno Antunes static int
ngt_newhook(node_p node,hook_p hook,const char * name)494e71e017bSNuno Antunes ngt_newhook(node_p node, hook_p hook, const char *name)
495e71e017bSNuno Antunes {
496e71e017bSNuno Antunes 	const sc_p sc = NG_NODE_PRIVATE(node);
4972efb75f3SMatthew Dillon 	struct tty *tp = sc->tp;
498e71e017bSNuno Antunes 
499e71e017bSNuno Antunes 	if (strcmp(name, NG_TTY_HOOK))
500e71e017bSNuno Antunes 		return (EINVAL);
5012efb75f3SMatthew Dillon 	lwkt_gettoken(&tp->t_token);
502e71e017bSNuno Antunes 	if (sc->hook) {
5032efb75f3SMatthew Dillon 		lwkt_reltoken(&tp->t_token);
504e71e017bSNuno Antunes 		return (EISCONN);
505e71e017bSNuno Antunes 	}
506e71e017bSNuno Antunes 	sc->hook = hook;
5072efb75f3SMatthew Dillon 	lwkt_reltoken(&tp->t_token);
508e71e017bSNuno Antunes 	return (0);
509e71e017bSNuno Antunes }
510e71e017bSNuno Antunes 
511e71e017bSNuno Antunes /*
512e71e017bSNuno Antunes  * Set the hook into queueing mode (for outgoing packets),
513e71e017bSNuno Antunes  * so that we wont deliver mbuf thru the whole graph holding
514e71e017bSNuno Antunes  * tty locks.
515e71e017bSNuno Antunes  */
516e71e017bSNuno Antunes static int
ngt_connect(hook_p hook)517e71e017bSNuno Antunes ngt_connect(hook_p hook)
518e71e017bSNuno Antunes {
519e71e017bSNuno Antunes 	NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
520e71e017bSNuno Antunes 	/*
521e71e017bSNuno Antunes 	 * XXX: While ngt_start() is Giant-locked, queue incoming
522e71e017bSNuno Antunes 	 * packets, too. Otherwise we acquire Giant holding some
523e71e017bSNuno Antunes 	 * IP stack locks, e.g. divinp, and this makes WITNESS scream.
524e71e017bSNuno Antunes 	 */
525e71e017bSNuno Antunes 	NG_HOOK_FORCE_QUEUE(hook);
526e71e017bSNuno Antunes 	return (0);
527e71e017bSNuno Antunes }
528e71e017bSNuno Antunes 
529e71e017bSNuno Antunes /*
530e71e017bSNuno Antunes  * Disconnect the hook
531e71e017bSNuno Antunes  */
532e71e017bSNuno Antunes static int
ngt_disconnect(hook_p hook)533e71e017bSNuno Antunes ngt_disconnect(hook_p hook)
534e71e017bSNuno Antunes {
535e71e017bSNuno Antunes 	const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
5362efb75f3SMatthew Dillon 	struct tty *tp = sc->tp;
537e71e017bSNuno Antunes 
5382efb75f3SMatthew Dillon 	lwkt_gettoken(&tp->t_token);
539e71e017bSNuno Antunes 	if (hook != sc->hook)
540e71e017bSNuno Antunes 		panic(__func__);
541e71e017bSNuno Antunes 	sc->hook = NULL;
5422efb75f3SMatthew Dillon 	lwkt_reltoken(&tp->t_token);
543e71e017bSNuno Antunes 	return (0);
544e71e017bSNuno Antunes }
545e71e017bSNuno Antunes 
546e71e017bSNuno Antunes /*
547e71e017bSNuno Antunes  * Remove this node. The does the netgraph portion of the shutdown.
548e71e017bSNuno Antunes  * This should only be called indirectly from ngt_close().
549e71e017bSNuno Antunes  *
550e71e017bSNuno Antunes  * tp->t_lsc is already NULL, so we should be protected from
551e71e017bSNuno Antunes  * tty calls now.
552e71e017bSNuno Antunes  */
553e71e017bSNuno Antunes static int
ngt_shutdown(node_p node)554e71e017bSNuno Antunes ngt_shutdown(node_p node)
555e71e017bSNuno Antunes {
556e71e017bSNuno Antunes 	const sc_p sc = NG_NODE_PRIVATE(node);
5572efb75f3SMatthew Dillon 	struct tty *tp = sc->tp;
558e71e017bSNuno Antunes 
5592efb75f3SMatthew Dillon 	lwkt_gettoken(&tp->t_token);
560e71e017bSNuno Antunes 	if (!(sc->flags & FLG_DIE)) {
5612efb75f3SMatthew Dillon 		lwkt_reltoken(&tp->t_token);
562e71e017bSNuno Antunes 		return (EOPNOTSUPP);
563e71e017bSNuno Antunes 	}
564e71e017bSNuno Antunes 
565e71e017bSNuno Antunes 	/* Free resources */
566e71e017bSNuno Antunes 	IF_DRAIN(&sc->outq);
567e71e017bSNuno Antunes 	m_freem(sc->m);
568e71e017bSNuno Antunes 	kfree(sc, M_NETGRAPH);
5692efb75f3SMatthew Dillon 	lwkt_reltoken(&tp->t_token);
570e71e017bSNuno Antunes 	return (0);
571e71e017bSNuno Antunes }
572e71e017bSNuno Antunes 
573e71e017bSNuno Antunes /*
574e71e017bSNuno Antunes  * Receive incoming data from netgraph system. Put it on our
575e71e017bSNuno Antunes  * output queue and start output if necessary.
576e71e017bSNuno Antunes  */
577e71e017bSNuno Antunes static int
ngt_rcvdata(hook_p hook,item_p item)578e71e017bSNuno Antunes ngt_rcvdata(hook_p hook, item_p item)
579e71e017bSNuno Antunes {
580e71e017bSNuno Antunes 	const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
5812efb75f3SMatthew Dillon 	struct tty *tp = sc->tp;
582e71e017bSNuno Antunes 	struct mbuf *m;
583e71e017bSNuno Antunes 	int qlen;
584e71e017bSNuno Antunes 
585e71e017bSNuno Antunes 	if (hook != sc->hook)
586e71e017bSNuno Antunes 		panic(__func__);
587e71e017bSNuno Antunes 
588e71e017bSNuno Antunes 	NGI_GET_M(item, m);
589e71e017bSNuno Antunes 	NG_FREE_ITEM(item);
590e71e017bSNuno Antunes 
591e71e017bSNuno Antunes 	if (IF_QFULL(&sc->outq)) {
592e71e017bSNuno Antunes 		IF_DROP(&sc->outq);
593e71e017bSNuno Antunes 		NG_FREE_M(m);
594e71e017bSNuno Antunes 		return (ENOBUFS);
595e71e017bSNuno Antunes 	}
596e71e017bSNuno Antunes 
597e71e017bSNuno Antunes 	IF_ENQUEUE(&sc->outq, m);
598e71e017bSNuno Antunes 	qlen = sc->outq.ifq_len;
599e71e017bSNuno Antunes 
600e71e017bSNuno Antunes 	/*
601e71e017bSNuno Antunes 	 * If qlen > 1, then we should already have a scheduled callout.
602e71e017bSNuno Antunes 	 */
603e71e017bSNuno Antunes 	if (qlen == 1) {
6042efb75f3SMatthew Dillon 		lwkt_gettoken(&tp->t_token);
605e71e017bSNuno Antunes 		ngt_start(sc->tp);
6062efb75f3SMatthew Dillon 		lwkt_reltoken(&tp->t_token);
607e71e017bSNuno Antunes 	}
608e71e017bSNuno Antunes 
609e71e017bSNuno Antunes 	return (0);
610e71e017bSNuno Antunes }
611e71e017bSNuno Antunes 
612e71e017bSNuno Antunes /*
613e71e017bSNuno Antunes  * Receive control message
614e71e017bSNuno Antunes  */
615e71e017bSNuno Antunes static int
ngt_rcvmsg(node_p node,item_p item,hook_p lasthook)616e71e017bSNuno Antunes ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
617e71e017bSNuno Antunes {
618e71e017bSNuno Antunes 	const sc_p sc = NG_NODE_PRIVATE(node);
619e71e017bSNuno Antunes 	struct ng_mesg *msg, *resp = NULL;
620e71e017bSNuno Antunes 	int error = 0;
621e71e017bSNuno Antunes 
622e71e017bSNuno Antunes 	NGI_GET_MSG(item, msg);
623e71e017bSNuno Antunes 	switch (msg->header.typecookie) {
624e71e017bSNuno Antunes 	case NGM_TTY_COOKIE:
625e71e017bSNuno Antunes 		switch (msg->header.cmd) {
626e71e017bSNuno Antunes 		case NGM_TTY_SET_HOTCHAR:
627e71e017bSNuno Antunes 		    {
628e71e017bSNuno Antunes 			int     hotchar;
629e71e017bSNuno Antunes 
630e71e017bSNuno Antunes 			if (msg->header.arglen != sizeof(int))
631e71e017bSNuno Antunes 				ERROUT(EINVAL);
632e71e017bSNuno Antunes 			hotchar = *((int *) msg->data);
633e71e017bSNuno Antunes 			if (hotchar != (u_char) hotchar && hotchar != -1)
634e71e017bSNuno Antunes 				ERROUT(EINVAL);
635e71e017bSNuno Antunes 			sc->hotchar = hotchar;	/* race condition is OK */
636e71e017bSNuno Antunes 			break;
637e71e017bSNuno Antunes 		    }
638e71e017bSNuno Antunes 		case NGM_TTY_GET_HOTCHAR:
639e71e017bSNuno Antunes 			NG_MKRESPONSE(resp, msg, sizeof(int), M_WAITOK | M_NULLOK);
640e71e017bSNuno Antunes 			if (!resp)
641e71e017bSNuno Antunes 				ERROUT(ENOMEM);
642e71e017bSNuno Antunes 			/* Race condition here is OK */
643e71e017bSNuno Antunes 			*((int *) resp->data) = sc->hotchar;
644e71e017bSNuno Antunes 			break;
645e71e017bSNuno Antunes 		default:
646e71e017bSNuno Antunes 			ERROUT(EINVAL);
647e71e017bSNuno Antunes 		}
648e71e017bSNuno Antunes 		break;
649e71e017bSNuno Antunes 	default:
650e71e017bSNuno Antunes 		ERROUT(EINVAL);
651e71e017bSNuno Antunes 	}
652e71e017bSNuno Antunes done:
653e71e017bSNuno Antunes 	NG_RESPOND_MSG(error, node, item, resp);
654e71e017bSNuno Antunes 	NG_FREE_MSG(msg);
655e71e017bSNuno Antunes 	return (error);
656e71e017bSNuno Antunes }
657e71e017bSNuno Antunes 
658e71e017bSNuno Antunes /******************************************************************
659e71e017bSNuno Antunes 		    	INITIALIZATION
660e71e017bSNuno Antunes ******************************************************************/
661e71e017bSNuno Antunes 
662e71e017bSNuno Antunes /*
663e71e017bSNuno Antunes  * Handle loading and unloading for this node type
664e71e017bSNuno Antunes  */
665e71e017bSNuno Antunes static int
ngt_mod_event(module_t mod,int event,void * data)666e71e017bSNuno Antunes ngt_mod_event(module_t mod, int event, void *data)
667e71e017bSNuno Antunes {
668e71e017bSNuno Antunes 	int error = 0;
669e71e017bSNuno Antunes 
670e71e017bSNuno Antunes 	switch (event) {
671e71e017bSNuno Antunes 	case MOD_LOAD:
672e71e017bSNuno Antunes 		/* Register line discipline */
673e71e017bSNuno Antunes 		if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
674e71e017bSNuno Antunes 			log(LOG_ERR, "%s: can't register line discipline",
675e71e017bSNuno Antunes 			    __func__);
676e71e017bSNuno Antunes 			return (EIO);
677e71e017bSNuno Antunes 		}
678e71e017bSNuno Antunes 		break;
679e71e017bSNuno Antunes 
680e71e017bSNuno Antunes 	case MOD_UNLOAD:
681e71e017bSNuno Antunes 
682e71e017bSNuno Antunes 		/* Unregister line discipline */
683e71e017bSNuno Antunes 		ldisc_deregister(ngt_ldisc);
684e71e017bSNuno Antunes 		break;
685e71e017bSNuno Antunes 
686e71e017bSNuno Antunes 	default:
687e71e017bSNuno Antunes 		error = EOPNOTSUPP;
688e71e017bSNuno Antunes 		break;
689e71e017bSNuno Antunes 	}
690e71e017bSNuno Antunes 	return (error);
691e71e017bSNuno Antunes }
692