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