xref: /dragonfly/sys/netgraph/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.7.2.3 2002/02/13 00:43:12 dillon 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  * NOTE: This node operates at spltty().
60  */
61 
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/conf.h>
66 #include <sys/proc.h>
67 #include <sys/caps.h>
68 #include <sys/mbuf.h>
69 #include <sys/malloc.h>
70 #include <sys/fcntl.h>
71 #include <sys/tty.h>
72 #include <sys/ttycom.h>
73 #include <sys/syslog.h>
74 #include <sys/errno.h>
75 
76 #include <netgraph/ng_message.h>
77 #include <netgraph/netgraph.h>
78 #include "ng_tty.h"
79 
80 /* Misc defs */
81 #define MAX_MBUFQ		3	/* Max number of queued mbufs */
82 #define NGT_HIWATER		400	/* High water mark on output */
83 
84 /* Per-node private info */
85 struct ngt_sc {
86 	struct	tty *tp;		/* Terminal device */
87 	node_p	node;			/* Netgraph node */
88 	hook_p	hook;			/* Netgraph hook */
89 	struct	mbuf *m;		/* Incoming data buffer */
90 	struct	mbuf *qhead, **qtail;	/* Queue of outgoing mbuf's */
91 	short	qlen;			/* Length of queue */
92 	short	hotchar;		/* Hotchar, or -1 if none */
93 	u_int	flags;			/* Flags */
94 	struct	callout	ctimeout;	/* See man timeout(9) */
95 };
96 typedef struct ngt_sc *sc_p;
97 
98 /* Flags */
99 #define FLG_TIMEOUT		0x0001	/* A timeout is pending */
100 #define FLG_DEBUG		0x0002
101 
102 /* Debugging */
103 #ifdef INVARIANTS
104 #define QUEUECHECK(sc)							\
105     do {								\
106       struct mbuf	**mp;						\
107       int		k;						\
108 									\
109       for (k = 0, mp = &sc->qhead;					\
110 	k <= MAX_MBUFQ && *mp;						\
111 	k++, mp = &(*mp)->m_nextpkt);					\
112       if (k != sc->qlen || k > MAX_MBUFQ || *mp || mp != sc->qtail)	\
113 	panic("%s: queue", __func__);				\
114     } while (0)
115 #else
116 #define QUEUECHECK(sc)	do {} while (0)
117 #endif
118 
119 /* Line discipline methods */
120 static int	ngt_open(cdev_t dev, struct tty *tp);
121 static int	ngt_close(struct tty *tp, int flag);
122 static int	ngt_read(struct tty *tp, struct uio *uio, int flag);
123 static int	ngt_write(struct tty *tp, struct uio *uio, int flag);
124 static int	ngt_tioctl(struct tty *tp,
125 		    u_long cmd, caddr_t data, int flag, struct ucred *cred);
126 static int	ngt_input(int c, struct tty *tp);
127 static int	ngt_start(struct tty *tp);
128 
129 /* Netgraph methods */
130 static ng_constructor_t	ngt_constructor;
131 static ng_rcvmsg_t	ngt_rcvmsg;
132 static ng_shutdown_t	ngt_shutdown;
133 static ng_newhook_t	ngt_newhook;
134 static ng_rcvdata_t	ngt_rcvdata;
135 static ng_disconnect_t	ngt_disconnect;
136 static int	ngt_mod_event(module_t mod, int event, void *data);
137 
138 /* Other stuff */
139 static void	ngt_timeout(void *arg);
140 
141 #define ERROUT(x)		do { error = (x); goto done; } while (0)
142 
143 /* Line discipline descriptor */
144 static struct linesw ngt_disc = {
145 	ngt_open,
146 	ngt_close,
147 	ngt_read,
148 	ngt_write,
149 	ngt_tioctl,
150 	ngt_input,
151 	ngt_start,
152 	ttymodem,
153 	NG_TTY_DFL_HOTCHAR	/* XXX can't change this in serial driver */
154 };
155 
156 /* Netgraph node type descriptor */
157 static struct ng_type typestruct = {
158 	NG_VERSION,
159 	NG_TTY_NODE_TYPE,
160 	ngt_mod_event,
161 	ngt_constructor,
162 	ngt_rcvmsg,
163 	ngt_shutdown,
164 	ngt_newhook,
165 	NULL,
166 	NULL,
167 	ngt_rcvdata,
168 	ngt_rcvdata,
169 	ngt_disconnect,
170 	NULL
171 };
172 NETGRAPH_INIT(tty, &typestruct);
173 
174 static int ngt_unit;
175 static int ngt_nodeop_ok;	/* OK to create/remove node */
176 static int ngt_ldisc;
177 
178 /******************************************************************
179 		    LINE DISCIPLINE METHODS
180 ******************************************************************/
181 
182 /*
183  * Set our line discipline on the tty.
184  * Called from device open routine or ttioctl() at >= splsofttty()
185  */
186 static int
ngt_open(cdev_t dev,struct tty * tp)187 ngt_open(cdev_t dev, struct tty *tp)
188 {
189 	char name[sizeof(NG_TTY_NODE_TYPE) + 8];
190 	sc_p sc;
191 	int error;
192 
193 	/* Super-user only */
194 	if ((error = caps_priv_check_self(SYSCAP_RESTRICTEDROOT)))
195 		return (error);
196 	lwkt_gettoken(&tp->t_token);
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 	sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
207 	sc->tp = tp;
208 	sc->hotchar = NG_TTY_DFL_HOTCHAR;
209 	sc->qtail = &sc->qhead;
210 	QUEUECHECK(sc);
211 	callout_init_mp(&sc->ctimeout);
212 
213 	/* Setup netgraph node */
214 	ngt_nodeop_ok = 1;
215 	error = ng_make_node_common(&typestruct, &sc->node);
216 	ngt_nodeop_ok = 0;
217 	if (error) {
218 		kfree(sc, M_NETGRAPH);
219 		goto done;
220 	}
221 	ksnprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit++);
222 
223 	/* Set back pointers */
224 	sc->node->private = sc;
225 	tp->t_sc = (caddr_t) sc;
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_rmnode(sc->node);
232 		ngt_nodeop_ok = 0;
233 		goto done;
234 	}
235 
236 	/*
237 	 * Pre-allocate cblocks to the an appropriate amount.
238 	 * I'm not sure what is appropriate.
239 	 */
240 	ttyflush(tp, FREAD | FWRITE);
241 	clist_alloc_cblocks(&tp->t_canq, 0);
242 	clist_alloc_cblocks(&tp->t_rawq, 0);
243 	clist_alloc_cblocks(&tp->t_outq, MLEN + NGT_HIWATER);
244 
245 done:
246 	/* Done */
247 	lwkt_reltoken(&tp->t_token);
248 	return (error);
249 }
250 
251 /*
252  * Line specific close routine, called from device close routine
253  * and from ttioctl at >= splsofttty(). This causes the node to
254  * be destroyed as well.
255  */
256 static int
ngt_close(struct tty * tp,int flag)257 ngt_close(struct tty *tp, int flag)
258 {
259 	const sc_p sc = (sc_p) tp->t_sc;
260 
261 	lwkt_gettoken(&tp->t_token);
262 	ttyflush(tp, FREAD | FWRITE);
263 	clist_free_cblocks(&tp->t_outq);
264 	tp->t_line = 0;
265 	if (sc != NULL) {
266 		if (sc->flags & FLG_TIMEOUT) {
267 			callout_stop(&sc->ctimeout);
268 			sc->flags &= ~FLG_TIMEOUT;
269 		}
270 		ngt_nodeop_ok = 1;
271 		ng_rmnode(sc->node);
272 		ngt_nodeop_ok = 0;
273 		tp->t_sc = NULL;
274 	}
275 	lwkt_reltoken(&tp->t_token);
276 
277 	return (0);
278 }
279 
280 /*
281  * Once the device has been turned into a node, we don't allow reading.
282  */
283 static int
ngt_read(struct tty * tp,struct uio * uio,int flag)284 ngt_read(struct tty *tp, struct uio *uio, int flag)
285 {
286 	return (EIO);
287 }
288 
289 /*
290  * Once the device has been turned into a node, we don't allow writing.
291  */
292 static int
ngt_write(struct tty * tp,struct uio * uio,int flag)293 ngt_write(struct tty *tp, struct uio *uio, int flag)
294 {
295 	return (EIO);
296 }
297 
298 /*
299  * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
300  */
301 static int
ngt_tioctl(struct tty * tp,u_long cmd,caddr_t data,int flag,struct ucred * cred)302 ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct ucred *cred)
303 {
304 	const sc_p sc = (sc_p) tp->t_sc;
305 	int error = 0;
306 
307 	lwkt_gettoken(&tp->t_token);
308 
309 	switch (cmd) {
310 	case NGIOCGINFO:
311 	    {
312 		struct nodeinfo *const ni = (struct nodeinfo *) data;
313 		const node_p node = sc->node;
314 
315 		bzero(ni, sizeof(*ni));
316 		if (node->name)
317 			strncpy(ni->name, node->name, sizeof(ni->name) - 1);
318 		strncpy(ni->type, node->type->name, sizeof(ni->type) - 1);
319 		ni->id = (u_int32_t)(uintptr_t)node;
320 		ni->hooks = node->numhooks;
321 		break;
322 	    }
323 	default:
324 		ERROUT(ENOIOCTL);
325 	}
326 done:
327 	lwkt_reltoken(&tp->t_token);
328 
329 	return (error);
330 }
331 
332 /*
333  * Receive data coming from the device. We get one character at
334  * a time, which is kindof silly.
335  * Only guaranteed to be at splsofttty() or spltty().
336  */
337 static int
ngt_input(int c,struct tty * tp)338 ngt_input(int c, struct tty *tp)
339 {
340 	const sc_p sc = (sc_p) tp->t_sc;
341 	const node_p node = sc ? sc->node : NULL;
342 	struct mbuf *m;
343 	int error = 0;
344 
345 	lwkt_gettoken(&tp->t_token);
346 	if (!sc || tp != sc->tp) {
347 		lwkt_reltoken(&tp->t_token);
348 		return (0);
349 	}
350 	if (!sc->hook)
351 		ERROUT(0);
352 
353 	/* Check for error conditions */
354 	if ((tp->t_state & TS_CONNECTED) == 0) {
355 		if (sc->flags & FLG_DEBUG)
356 			log(LOG_DEBUG, "%s: no carrier\n", node->name);
357 		ERROUT(0);
358 	}
359 	if (c & TTY_ERRORMASK) {
360 		/* framing error or overrun on this char */
361 		if (sc->flags & FLG_DEBUG)
362 			log(LOG_DEBUG, "%s: line error %x\n",
363 			    node->name, c & TTY_ERRORMASK);
364 		ERROUT(0);
365 	}
366 	c &= TTY_CHARMASK;
367 
368 	/* Get a new header mbuf if we need one */
369 	if (!(m = sc->m)) {
370 		MGETHDR(m, M_NOWAIT, MT_DATA);
371 		if (!m) {
372 			if (sc->flags & FLG_DEBUG)
373 				log(LOG_ERR,
374 				    "%s: can't get mbuf\n", node->name);
375 			ERROUT(ENOBUFS);
376 		}
377 		m->m_len = m->m_pkthdr.len = 0;
378 		m->m_pkthdr.rcvif = NULL;
379 		sc->m = m;
380 	}
381 
382 	/* Add char to mbuf */
383 	*mtod(m, u_char *) = c;
384 	m->m_data++;
385 	m->m_len++;
386 	m->m_pkthdr.len++;
387 
388 	/* Ship off mbuf if it's time */
389 	if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) {
390 		m->m_data = m->m_pktdat;
391 		error = ng_queue_data(sc->hook, m, NULL);
392 		sc->m = NULL;
393 	}
394 done:
395 	lwkt_reltoken(&tp->t_token);
396 
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
ngt_start(struct tty * tp)406 ngt_start(struct tty *tp)
407 {
408 	const sc_p sc = (sc_p) tp->t_sc;
409 
410 	lwkt_gettoken(&tp->t_token);
411 	while (tp->t_outq.c_cc < NGT_HIWATER) {	/* XXX 2.2 specific ? */
412 		struct mbuf *m = sc->qhead;
413 
414 		/* Remove first mbuf from queue */
415 		if (!m)
416 			break;
417 		if ((sc->qhead = m->m_nextpkt) == NULL)
418 			sc->qtail = &sc->qhead;
419 		sc->qlen--;
420 		QUEUECHECK(sc);
421 
422 		/* Send as much of it as possible */
423 		while (m) {
424 			int     sent;
425 
426 			sent = m->m_len - clist_btoq(mtod(m, u_char *),
427 						     m->m_len, &tp->t_outq);
428 			m->m_data += sent;
429 			m->m_len -= sent;
430 			if (m->m_len > 0)
431 				break;	/* device can't take no more */
432 			m = m_free(m);
433 		}
434 
435 		/* Put remainder of mbuf chain (if any) back on queue */
436 		if (m) {
437 			m->m_nextpkt = sc->qhead;
438 			sc->qhead = m;
439 			if (sc->qtail == &sc->qhead)
440 				sc->qtail = &m->m_nextpkt;
441 			sc->qlen++;
442 			QUEUECHECK(sc);
443 			break;
444 		}
445 	}
446 
447 	/* Call output process whether or not there is any output. We are
448 	 * being called in lieu of ttstart and must do what it would. */
449 	if (tp->t_oproc != NULL)
450 		(*tp->t_oproc) (tp);
451 
452 	/* This timeout is needed for operation on a pseudo-tty, because the
453 	 * pty code doesn't call pppstart after it has drained the t_outq. */
454 	if (sc->qhead && (sc->flags & FLG_TIMEOUT) == 0) {
455 		callout_reset(&sc->ctimeout, 1, ngt_timeout, sc);
456 		sc->flags |= FLG_TIMEOUT;
457 	}
458 	lwkt_reltoken(&tp->t_token);
459 
460 	return (0);
461 }
462 
463 /*
464  * We still have data to output to the device, so try sending more.
465  */
466 static void
ngt_timeout(void * arg)467 ngt_timeout(void *arg)
468 {
469 	const sc_p sc = (sc_p) arg;
470 	struct tty *tp = sc->tp;
471 
472 	lwkt_gettoken(&tp->t_token);
473 	sc->flags &= ~FLG_TIMEOUT;
474 	ngt_start(tp);
475 	lwkt_reltoken(&tp->t_token);
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
ngt_constructor(node_p * nodep)489 ngt_constructor(node_p *nodep)
490 {
491 	if (!ngt_nodeop_ok)
492 		return (EOPNOTSUPP);
493 	return (ng_make_node_common(&typestruct, nodep));
494 }
495 
496 /*
497  * Add a new hook. There can only be one.
498  */
499 static int
ngt_newhook(node_p node,hook_p hook,const char * name)500 ngt_newhook(node_p node, hook_p hook, const char *name)
501 {
502 	const sc_p sc = node->private;
503 	struct tty *tp = sc->tp;
504 	int error = 0;
505 
506 	if (strcmp(name, NG_TTY_HOOK))
507 		return (EINVAL);
508 	lwkt_gettoken(&tp->t_token);
509 	if (sc->hook)
510 		ERROUT(EISCONN);
511 	sc->hook = hook;
512 done:
513 	lwkt_reltoken(&tp->t_token);
514 
515 	return (error);
516 }
517 
518 /*
519  * Disconnect the hook
520  */
521 static int
ngt_disconnect(hook_p hook)522 ngt_disconnect(hook_p hook)
523 {
524 	const sc_p sc = hook->node->private;
525 	struct tty *tp = sc->tp;
526 
527 	lwkt_gettoken(&tp->t_token);
528 	if (hook != sc->hook)
529 		panic(__func__);
530 	sc->hook = NULL;
531 	m_freem(sc->m);
532 	sc->m = NULL;
533 	lwkt_reltoken(&tp->t_token);
534 
535 	return (0);
536 }
537 
538 /*
539  * Remove this node. The does the netgraph portion of the shutdown.
540  * This should only be called indirectly from ngt_close().
541  */
542 static int
ngt_shutdown(node_p node)543 ngt_shutdown(node_p node)
544 {
545 	const sc_p sc = node->private;
546 	struct tty *tp = sc->tp;
547 
548 	if (!ngt_nodeop_ok)
549 		return (EOPNOTSUPP);
550 	lwkt_gettoken(&tp->t_token);
551 	ng_unname(node);
552 	ng_cutlinks(node);
553 	node->private = NULL;
554 	ng_unref(sc->node);
555 	m_freem(sc->qhead);
556 	m_freem(sc->m);
557 	bzero(sc, sizeof(*sc));
558 	kfree(sc, M_NETGRAPH);
559 	lwkt_reltoken(&tp->t_token);
560 
561 	return (0);
562 }
563 
564 /*
565  * Receive incoming data from netgraph system. Put it on our
566  * output queue and start output if necessary.
567  */
568 static int
ngt_rcvdata(hook_p hook,struct mbuf * m,meta_p meta)569 ngt_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
570 {
571 	const sc_p sc = hook->node->private;
572 	struct tty *tp = sc->tp;
573 	int error = 0;
574 
575 	if (hook != sc->hook)
576 		panic(__func__);
577 	NG_FREE_META(meta);
578 	lwkt_gettoken(&tp->t_token);
579 	if (sc->qlen >= MAX_MBUFQ)
580 		ERROUT(ENOBUFS);
581 	m->m_nextpkt = NULL;
582 	*sc->qtail = m;
583 	sc->qtail = &m->m_nextpkt;
584 	sc->qlen++;
585 	QUEUECHECK(sc);
586 	m = NULL;
587 	if (sc->qlen == 1)
588 		ngt_start(sc->tp);
589 done:
590 	lwkt_reltoken(&tp->t_token);
591 	if (m)
592 		m_freem(m);
593 	return (error);
594 }
595 
596 /*
597  * Receive control message
598  */
599 static int
ngt_rcvmsg(node_p node,struct ng_mesg * msg,const char * retaddr,struct ng_mesg ** rptr)600 ngt_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
601 	   struct ng_mesg **rptr)
602 {
603 	const sc_p sc = (sc_p) node->private;
604 	struct tty *tp = sc->tp;
605 	struct ng_mesg *resp = NULL;
606 	int error = 0;
607 
608 	lwkt_gettoken(&tp->t_token);
609 	switch (msg->header.typecookie) {
610 	case NGM_TTY_COOKIE:
611 		switch (msg->header.cmd) {
612 		case NGM_TTY_SET_HOTCHAR:
613 		    {
614 			int     hotchar;
615 
616 			if (msg->header.arglen != sizeof(int))
617 				ERROUT(EINVAL);
618 			hotchar = *((int *) msg->data);
619 			if (hotchar != (u_char) hotchar && hotchar != -1)
620 				ERROUT(EINVAL);
621 			sc->hotchar = hotchar;	/* race condition is OK */
622 			break;
623 		    }
624 		case NGM_TTY_GET_HOTCHAR:
625 			NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT);
626 			if (!resp)
627 				ERROUT(ENOMEM);
628 			/* Race condition here is OK */
629 			*((int *) resp->data) = sc->hotchar;
630 			break;
631 		default:
632 			ERROUT(EINVAL);
633 		}
634 		break;
635 	default:
636 		ERROUT(EINVAL);
637 	}
638 	if (rptr)
639 		*rptr = resp;
640 	else if (resp)
641 		kfree(resp, M_NETGRAPH);
642 
643 done:
644 	kfree(msg, M_NETGRAPH);
645 	lwkt_reltoken(&tp->t_token);
646 
647 	return (error);
648 }
649 
650 /******************************************************************
651 		    	INITIALIZATION
652 ******************************************************************/
653 
654 /*
655  * Handle loading and unloading for this node type
656  */
657 static int
ngt_mod_event(module_t mod,int event,void * data)658 ngt_mod_event(module_t mod, int event, void *data)
659 {
660 	/* struct ng_type *const type = data;*/
661 	int error = 0;
662 
663 	switch (event) {
664 	case MOD_LOAD:
665 		/* Register line discipline */
666 		if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
667 			log(LOG_ERR, "%s: can't register line discipline",
668 			    __func__);
669 			return (EIO);
670 		}
671 		break;
672 
673 	case MOD_UNLOAD:
674 
675 		/* Unregister line discipline */
676 		ldisc_deregister(ngt_ldisc);
677 		break;
678 
679 	default:
680 		error = EOPNOTSUPP;
681 		break;
682 	}
683 	return (error);
684 }
685