kernel: Remove NULL checks after kmalloc() with M_WAITOK.
[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  * $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/priv.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 "ng_message.h"
78 #include "netgraph.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 thread *);
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 #define NGTLOCK(sc)     IF_LOCK(&sc->outq)
170 #define NGTUNLOCK(sc)   IF_UNLOCK(&sc->outq)
171
172 static int ngt_unit;
173 static int ngt_ldisc;
174
175 /******************************************************************
176                     LINE DISCIPLINE METHODS
177 ******************************************************************/
178
179 /*
180  * Set our line discipline on the tty.
181  * Called from device open routine or ttioctl()
182  */
183 static int
184 ngt_open(struct cdev *dev, struct tty *tp)
185 {
186         struct thread *const td = curthread;    /* XXX */
187         char name[sizeof(NG_TTY_NODE_TYPE) + 8];
188         sc_p sc;
189         int error;
190
191         /* Super-user only */
192         error = priv_check(td, PRIV_NETGRAPH_TTY);
193         if (error)
194                 return (error);
195
196         /* Initialize private struct */
197         sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
198
199         lwkt_gettoken(&tty_token);
200         sc->tp = tp;
201         sc->hotchar = tp->t_hotchar = NG_TTY_DFL_HOTCHAR;
202         mtx_init(&sc->outq.ifq_mtx, "ng_tty node+queue", NULL, MTX_DEF);
203         IFQ_SET_MAXLEN(&sc->outq, MAX_MBUFQ);
204
205         NGTLOCK(sc);
206
207         /* Setup netgraph node */
208         error = ng_make_node_common(&typestruct, &sc->node);
209         if (error) {
210                 NGTUNLOCK(sc);
211                 kfree(sc, M_NETGRAPH);
212                 lwkt_reltoken(&tty_token);
213                 return (error);
214         }
215
216         atomic_add_int(&ngt_unit, 1);
217         snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit);
218
219         /* Assign node its name */
220         if ((error = ng_name_node(sc->node, name))) {
221                 sc->flags |= FLG_DIE;
222                 NGTUNLOCK(sc);
223                 NG_NODE_UNREF(sc->node);
224                 log(LOG_ERR, "%s: node name exists?\n", name);
225                 lwkt_reltoken(&tty_token);
226                 return (error);
227         }
228
229         /* Set back pointers */
230         NG_NODE_SET_PRIVATE(sc->node, sc);
231         tp->t_lsc = sc;
232
233         ng_callout_init_mp(&sc->chand);
234
235         /*
236          * Pre-allocate cblocks to the an appropriate amount.
237          * I'm not sure what is appropriate.
238          */
239         ttyflush(tp, FREAD | FWRITE);
240         clist_alloc_cblocks(&tp->t_canq, 0, 0);
241         clist_alloc_cblocks(&tp->t_rawq, 0, 0);
242         clist_alloc_cblocks(&tp->t_outq,
243             MLEN + NGT_HIWATER, MLEN + NGT_HIWATER);
244
245         NGTUNLOCK(sc);
246
247         lwkt_reltoken(&tty_token);
248         return (0);
249 }
250
251 /*
252  * Line specific close routine, called from device close routine
253  * and from ttioctl. This causes the node to be destroyed as well.
254  */
255 static int
256 ngt_close(struct tty *tp, int flag)
257 {
258         const sc_p sc = (sc_p) tp->t_lsc;
259
260         lwkt_gettoken(&tty_token);
261         ttyflush(tp, FREAD | FWRITE);
262         clist_free_cblocks(&tp->t_outq);
263         if (sc != NULL) {
264                 NGTLOCK(sc);
265                 if (callout_pending(&sc->chand))
266                         ng_uncallout(&sc->chand, sc->node);
267                 tp->t_lsc = NULL;
268                 sc->flags |= FLG_DIE;
269                 NGTUNLOCK(sc);
270                 ng_rmnode_self(sc->node);
271         }
272         lwkt_reltoken(&tty_token);
273         return (0);
274 }
275
276 /*
277  * Once the device has been turned into a node, we don't allow reading.
278  */
279 static int
280 ngt_read(struct tty *tp, struct uio *uio, int flag)
281 {
282         return (EIO);
283 }
284
285 /*
286  * Once the device has been turned into a node, we don't allow writing.
287  */
288 static int
289 ngt_write(struct tty *tp, struct uio *uio, int flag)
290 {
291         return (EIO);
292 }
293
294 /*
295  * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
296  */
297 static int
298 ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *td)
299 {
300         const sc_p sc = (sc_p) tp->t_lsc;
301
302         if (sc == NULL)
303                 /* No node attached */
304                 return (0);
305
306         lwkt_gettoken(&tty_token);
307         switch (cmd) {
308         case NGIOCGINFO:
309             {
310                 struct nodeinfo *const ni = (struct nodeinfo *) data;
311                 const node_p node = sc->node;
312
313                 bzero(ni, sizeof(*ni));
314                 NGTLOCK(sc);
315                 if (NG_NODE_HAS_NAME(node))
316                         strncpy(ni->name, NG_NODE_NAME(node), sizeof(ni->name) - 1);
317                 strncpy(ni->type, node->nd_type->name, sizeof(ni->type) - 1);
318                 ni->id = (u_int32_t) ng_node2ID(node);
319                 ni->hooks = NG_NODE_NUMHOOKS(node);
320                 NGTUNLOCK(sc);
321                 break;
322             }
323         default:
324                 lwkt_reltoken(&tty_token);
325                 return (ENOIOCTL);
326         }
327
328         lwkt_reltoken(&tty_token);
329         return (0);
330 }
331
332 /*
333  * Receive data coming from the device. We get one character at
334  * a time, which is kindof silly.
335  *
336  * Full locking of softc is not required, since we are the only
337  * user of sc->m.
338  */
339 static int
340 ngt_input(int c, struct tty *tp)
341 {
342         sc_p sc;
343         node_p node;
344         struct mbuf *m;
345         int error = 0;
346
347         lwkt_gettoken(&tty_token);
348         sc = (sc_p) tp->t_lsc;
349         if (sc == NULL) {
350                 /* No node attached */
351                 lwkt_reltoken(&tty_token);
352                 return (0);
353         }
354
355         node = sc->node;
356
357         if (tp != sc->tp)
358                 panic("ngt_input");
359
360         /* Check for error conditions */
361         if ((tp->t_state & TS_CONNECTED) == 0) {
362                 if (sc->flags & FLG_DEBUG)
363                         log(LOG_DEBUG, "%s: no carrier\n", NG_NODE_NAME(node));
364                 lwkt_reltoken(&tty_token);
365                 return (0);
366         }
367         if (c & TTY_ERRORMASK) {
368                 /* framing error or overrun on this char */
369                 if (sc->flags & FLG_DEBUG)
370                         log(LOG_DEBUG, "%s: line error %x\n",
371                             NG_NODE_NAME(node), c & TTY_ERRORMASK);
372                 lwkt_reltoken(&tty_token);
373                 return (0);
374         }
375         c &= TTY_CHARMASK;
376
377         /* Get a new header mbuf if we need one */
378         if (!(m = sc->m)) {
379                 MGETHDR(m, MB_DONTWAIT, MT_DATA);
380                 if (!m) {
381                         if (sc->flags & FLG_DEBUG)
382                                 log(LOG_ERR,
383                                     "%s: can't get mbuf\n", NG_NODE_NAME(node));
384                         lwkt_reltoken(&tty_token);
385                         return (ENOBUFS);
386                 }
387                 m->m_len = m->m_pkthdr.len = 0;
388                 m->m_pkthdr.rcvif = NULL;
389                 sc->m = m;
390         }
391
392         /* Add char to mbuf */
393         *mtod(m, u_char *) = c;
394         m->m_data++;
395         m->m_len++;
396         m->m_pkthdr.len++;
397
398         /* Ship off mbuf if it's time */
399         if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) {
400                 m->m_data = m->m_pktdat;
401                 sc->m = NULL;
402
403                 /*
404                  * We have built our mbuf without checking that we actually
405                  * have a hook to send it. This was done to avoid
406                  * acquiring mutex on each character. Check now.
407                  *
408                  */
409
410                 NGTLOCK(sc);
411                 if (sc->hook == NULL) {
412                         NGTUNLOCK(sc);
413                         m_freem(m);
414                         lwkt_reltoken(&tty_token);
415                         return (0);             /* XXX: original behavior */
416                 }
417                 NG_SEND_DATA_ONLY(error, sc->hook, m);  /* Will queue */
418                 NGTUNLOCK(sc);
419         }
420
421         lwkt_reltoken(&tty_token);
422         return (error);
423 }
424
425 /*
426  * This is called when the device driver is ready for more output.
427  * Also called from ngt_rcv_data() when a new mbuf is available for output.
428  */
429 static int
430 ngt_start(struct tty *tp)
431 {
432         const sc_p sc = (sc_p) tp->t_lsc;
433
434         lwkt_gettoken(&tty_token);
435         while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */
436                 struct mbuf *m;
437
438                 /* Remove first mbuf from queue */
439                 IF_DEQUEUE(&sc->outq, m);
440                 if (m == NULL)
441                         break;
442
443                 /* Send as much of it as possible */
444                 while (m != NULL) {
445                         int     sent;
446
447                         sent = m->m_len
448                             - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq);
449                         m->m_data += sent;
450                         m->m_len -= sent;
451                         if (m->m_len > 0)
452                                 break;  /* device can't take no more */
453                         m = m_free(m);
454                 }
455
456                 /* Put remainder of mbuf chain (if any) back on queue */
457                 if (m != NULL) {
458                         IF_PREPEND(&sc->outq, m);
459                         break;
460                 }
461         }
462
463         /* Call output process whether or not there is any output. We are
464          * being called in lieu of ttstart and must do what it would. */
465         tt_oproc(tp);
466
467         /* This timeout is needed for operation on a pseudo-tty, because the
468          * pty code doesn't call pppstart after it has drained the t_outq. */
469         /* XXX: outq not locked */
470         if (!IFQ_IS_EMPTY(&sc->outq) && !callout_pending(&sc->chand))
471                 ng_callout(&sc->chand, sc->node, NULL, 1, ngt_timeout, NULL, 0);
472
473         lwkt_reltoken(&tty_token);
474         return (0);
475 }
476
477 /*
478  * We still have data to output to the device, so try sending more.
479  */
480 static void
481 ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2)
482 {
483         const sc_p sc = NG_NODE_PRIVATE(node);
484
485         mtx_lock(&Giant);
486         ngt_start(sc->tp);
487         mtx_unlock(&Giant);
488 }
489
490 /******************************************************************
491                     NETGRAPH NODE METHODS
492 ******************************************************************/
493
494 /*
495  * Initialize a new node of this type.
496  *
497  * We only allow nodes to be created as a result of setting
498  * the line discipline on a tty, so always return an error if not.
499  */
500 static int
501 ngt_constructor(node_p node)
502 {
503         return (EOPNOTSUPP);
504 }
505
506 /*
507  * Add a new hook. There can only be one.
508  */
509 static int
510 ngt_newhook(node_p node, hook_p hook, const char *name)
511 {
512         const sc_p sc = NG_NODE_PRIVATE(node);
513
514         if (strcmp(name, NG_TTY_HOOK))
515                 return (EINVAL);
516
517         if (sc->hook)
518                 return (EISCONN);
519
520         NGTLOCK(sc);
521         sc->hook = hook;
522         NGTUNLOCK(sc);
523
524         return (0);
525 }
526
527 /*
528  * Set the hook into queueing mode (for outgoing packets),
529  * so that we wont deliver mbuf thru the whole graph holding
530  * tty locks.
531  */
532 static int
533 ngt_connect(hook_p hook)
534 {
535         NG_HOOK_FORCE_QUEUE(NG_HOOK_PEER(hook));
536         /*
537          * XXX: While ngt_start() is Giant-locked, queue incoming
538          * packets, too. Otherwise we acquire Giant holding some
539          * IP stack locks, e.g. divinp, and this makes WITNESS scream.
540          */
541         NG_HOOK_FORCE_QUEUE(hook);
542         return (0);
543 }
544
545 /*
546  * Disconnect the hook
547  */
548 static int
549 ngt_disconnect(hook_p hook)
550 {
551         const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
552
553         if (hook != sc->hook)
554                 panic(__func__);
555
556         NGTLOCK(sc);
557         sc->hook = NULL;
558         NGTUNLOCK(sc);
559
560         return (0);
561 }
562
563 /*
564  * Remove this node. The does the netgraph portion of the shutdown.
565  * This should only be called indirectly from ngt_close().
566  *
567  * tp->t_lsc is already NULL, so we should be protected from
568  * tty calls now.
569  */
570 static int
571 ngt_shutdown(node_p node)
572 {
573         const sc_p sc = NG_NODE_PRIVATE(node);
574
575         NGTLOCK(sc);
576         if (!(sc->flags & FLG_DIE)) {
577                 NGTUNLOCK(sc);
578                 return (EOPNOTSUPP);
579         }
580         NGTUNLOCK(sc);
581
582         /* Free resources */
583         _IF_DRAIN(&sc->outq);
584         mtx_destroy(&(sc)->outq.ifq_mtx);
585         m_freem(sc->m);
586         NG_NODE_UNREF(sc->node);
587         kfree(sc, M_NETGRAPH);
588
589         return (0);
590 }
591
592 /*
593  * Receive incoming data from netgraph system. Put it on our
594  * output queue and start output if necessary.
595  */
596 static int
597 ngt_rcvdata(hook_p hook, item_p item)
598 {
599         const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
600         struct mbuf *m;
601         int qlen;
602
603         if (hook != sc->hook)
604                 panic(__func__);
605
606         NGI_GET_M(item, m);
607         NG_FREE_ITEM(item);
608
609         IF_LOCK(&sc->outq);
610         if (_IF_QFULL(&sc->outq)) {
611                 _IF_DROP(&sc->outq);
612                 IF_UNLOCK(&sc->outq);
613                 NG_FREE_M(m);
614                 return (ENOBUFS);
615         }
616
617         _IF_ENQUEUE(&sc->outq, m);
618         qlen = sc->outq.ifq_len;
619         IF_UNLOCK(&sc->outq);
620
621         /*
622          * If qlen > 1, then we should already have a scheduled callout.
623          */
624         if (qlen == 1) {
625                 mtx_lock(&Giant);
626                 ngt_start(sc->tp);
627                 mtx_unlock(&Giant);
628         }
629
630         return (0);
631 }
632
633 /*
634  * Receive control message
635  */
636 static int
637 ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
638 {
639         const sc_p sc = NG_NODE_PRIVATE(node);
640         struct ng_mesg *msg, *resp = NULL;
641         int error = 0;
642
643         NGI_GET_MSG(item, msg);
644         switch (msg->header.typecookie) {
645         case NGM_TTY_COOKIE:
646                 switch (msg->header.cmd) {
647                 case NGM_TTY_SET_HOTCHAR:
648                     {
649                         int     hotchar;
650
651                         if (msg->header.arglen != sizeof(int))
652                                 ERROUT(EINVAL);
653                         hotchar = *((int *) msg->data);
654                         if (hotchar != (u_char) hotchar && hotchar != -1)
655                                 ERROUT(EINVAL);
656                         sc->hotchar = hotchar;  /* race condition is OK */
657                         break;
658                     }
659                 case NGM_TTY_GET_HOTCHAR:
660                         NG_MKRESPONSE(resp, msg, sizeof(int), M_WAITOK | M_NULLOK);
661                         if (!resp)
662                                 ERROUT(ENOMEM);
663                         /* Race condition here is OK */
664                         *((int *) resp->data) = sc->hotchar;
665                         break;
666                 default:
667                         ERROUT(EINVAL);
668                 }
669                 break;
670         default:
671                 ERROUT(EINVAL);
672         }
673 done:
674         NG_RESPOND_MSG(error, node, item, resp);
675         NG_FREE_MSG(msg);
676         return (error);
677 }
678
679 /******************************************************************
680                         INITIALIZATION
681 ******************************************************************/
682
683 /*
684  * Handle loading and unloading for this node type
685  */
686 static int
687 ngt_mod_event(module_t mod, int event, void *data)
688 {
689         int error = 0;
690
691         switch (event) {
692         case MOD_LOAD:
693
694                 /* Register line discipline */
695                 mtx_lock(&Giant);
696                 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
697                         mtx_unlock(&Giant);
698                         log(LOG_ERR, "%s: can't register line discipline",
699                             __func__);
700                         return (EIO);
701                 }
702                 mtx_unlock(&Giant);
703                 break;
704
705         case MOD_UNLOAD:
706
707                 /* Unregister line discipline */
708                 mtx_lock(&Giant);
709                 ldisc_deregister(ngt_ldisc);
710                 mtx_unlock(&Giant);
711                 break;
712
713         default:
714                 error = EOPNOTSUPP;
715                 break;
716         }
717         return (error);
718 }