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