The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netgraph/ng_tty.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * ng_tty.c
    3  */
    4 
    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$
   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/ioccom.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 <netgraph/ng_message.h>
   79 #include <netgraph/netgraph.h>
   80 #include <netgraph/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         sc->tp = tp;
  203         sc->hotchar = tp->t_hotchar = NG_TTY_DFL_HOTCHAR;
  204         mtx_init(&sc->outq.ifq_mtx, "ng_tty node+queue", NULL, MTX_DEF);
  205         IFQ_SET_MAXLEN(&sc->outq, MAX_MBUFQ);
  206 
  207         NGTLOCK(sc);
  208 
  209         /* Setup netgraph node */
  210         error = ng_make_node_common(&typestruct, &sc->node);
  211         if (error) {
  212                 NGTUNLOCK(sc);
  213                 FREE(sc, M_NETGRAPH);
  214                 return (error);
  215         }
  216 
  217         atomic_add_int(&ngt_unit, 1);
  218         snprintf(name, sizeof(name), "%s%d", typestruct.name, ngt_unit);
  219 
  220         /* Assign node its name */
  221         if ((error = ng_name_node(sc->node, name))) {
  222                 sc->flags |= FLG_DIE;
  223                 NGTUNLOCK(sc);
  224                 NG_NODE_UNREF(sc->node);
  225                 log(LOG_ERR, "%s: node name exists?\n", name);
  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(&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         return (0);
  248 }
  249 
  250 /*
  251  * Line specific close routine, called from device close routine
  252  * and from ttioctl. This causes the node to be destroyed as well.
  253  */
  254 static int
  255 ngt_close(struct tty *tp, int flag)
  256 {
  257         const sc_p sc = (sc_p) tp->t_lsc;
  258 
  259         ttyflush(tp, FREAD | FWRITE);
  260         clist_free_cblocks(&tp->t_outq);
  261         if (sc != NULL) {
  262                 NGTLOCK(sc);
  263                 if (callout_pending(&sc->chand))
  264                         ng_uncallout(&sc->chand, sc->node);
  265                 tp->t_lsc = NULL;
  266                 sc->flags |= FLG_DIE;
  267                 NGTUNLOCK(sc);
  268                 ng_rmnode_self(sc->node);
  269         }
  270         return (0);
  271 }
  272 
  273 /*
  274  * Once the device has been turned into a node, we don't allow reading.
  275  */
  276 static int
  277 ngt_read(struct tty *tp, struct uio *uio, int flag)
  278 {
  279         return (EIO);
  280 }
  281 
  282 /*
  283  * Once the device has been turned into a node, we don't allow writing.
  284  */
  285 static int
  286 ngt_write(struct tty *tp, struct uio *uio, int flag)
  287 {
  288         return (EIO);
  289 }
  290 
  291 /*
  292  * We implement the NGIOCGINFO ioctl() defined in ng_message.h.
  293  */
  294 static int
  295 ngt_tioctl(struct tty *tp, u_long cmd, caddr_t data, int flag, struct thread *td)
  296 {
  297         const sc_p sc = (sc_p) tp->t_lsc;
  298 
  299         if (sc == NULL)
  300                 /* No node attached */
  301                 return (0);
  302 
  303         switch (cmd) {
  304         case NGIOCGINFO:
  305             {
  306                 struct nodeinfo *const ni = (struct nodeinfo *) data;
  307                 const node_p node = sc->node;
  308 
  309                 bzero(ni, sizeof(*ni));
  310                 NGTLOCK(sc);
  311                 if (NG_NODE_HAS_NAME(node))
  312                         strncpy(ni->name, NG_NODE_NAME(node), sizeof(ni->name) - 1);
  313                 strncpy(ni->type, node->nd_type->name, sizeof(ni->type) - 1);
  314                 ni->id = (u_int32_t) ng_node2ID(node);
  315                 ni->hooks = NG_NODE_NUMHOOKS(node);
  316                 NGTUNLOCK(sc);
  317                 break;
  318             }
  319         default:
  320                 return (ENOIOCTL);
  321         }
  322 
  323         return (0);
  324 }
  325 
  326 /*
  327  * Receive data coming from the device. We get one character at
  328  * a time, which is kindof silly.
  329  *
  330  * Full locking of softc is not required, since we are the only
  331  * user of sc->m.
  332  */
  333 static int
  334 ngt_input(int c, struct tty *tp)
  335 {
  336         sc_p sc;
  337         node_p node;
  338         struct mbuf *m;
  339         int error = 0;
  340 
  341         sc = (sc_p) tp->t_lsc;
  342         if (sc == NULL)
  343                 /* No node attached */
  344                 return (0);
  345 
  346         node = sc->node;
  347 
  348         if (tp != sc->tp)
  349                 panic("ngt_input");
  350 
  351         /* Check for error conditions */
  352         if ((tp->t_state & TS_CONNECTED) == 0) {
  353                 if (sc->flags & FLG_DEBUG)
  354                         log(LOG_DEBUG, "%s: no carrier\n", NG_NODE_NAME(node));
  355                 return (0);
  356         }
  357         if (c & TTY_ERRORMASK) {
  358                 /* framing error or overrun on this char */
  359                 if (sc->flags & FLG_DEBUG)
  360                         log(LOG_DEBUG, "%s: line error %x\n",
  361                             NG_NODE_NAME(node), c & TTY_ERRORMASK);
  362                 return (0);
  363         }
  364         c &= TTY_CHARMASK;
  365 
  366         /* Get a new header mbuf if we need one */
  367         if (!(m = sc->m)) {
  368                 MGETHDR(m, M_DONTWAIT, MT_DATA);
  369                 if (!m) {
  370                         if (sc->flags & FLG_DEBUG)
  371                                 log(LOG_ERR,
  372                                     "%s: can't get mbuf\n", NG_NODE_NAME(node));
  373                         return (ENOBUFS);
  374                 }
  375                 m->m_len = m->m_pkthdr.len = 0;
  376                 m->m_pkthdr.rcvif = NULL;
  377                 sc->m = m;
  378         }
  379 
  380         /* Add char to mbuf */
  381         *mtod(m, u_char *) = c;
  382         m->m_data++;
  383         m->m_len++;
  384         m->m_pkthdr.len++;
  385 
  386         /* Ship off mbuf if it's time */
  387         if (sc->hotchar == -1 || c == sc->hotchar || m->m_len >= MHLEN) {
  388                 m->m_data = m->m_pktdat;
  389                 sc->m = NULL;
  390 
  391                 /*
  392                  * We have built our mbuf without checking that we actually
  393                  * have a hook to send it. This was done to avoid
  394                  * acquiring mutex on each character. Check now.
  395                  *
  396                  */
  397 
  398                 NGTLOCK(sc);
  399                 if (sc->hook == NULL) {
  400                         NGTUNLOCK(sc);
  401                         m_freem(m);
  402                         return (0);             /* XXX: original behavior */
  403                 }
  404                 NG_SEND_DATA_ONLY(error, sc->hook, m);  /* Will queue */
  405                 NGTUNLOCK(sc);
  406         }
  407 
  408         return (error);
  409 }
  410 
  411 /*
  412  * This is called when the device driver is ready for more output.
  413  * Also called from ngt_rcv_data() when a new mbuf is available for output.
  414  */
  415 static int
  416 ngt_start(struct tty *tp)
  417 {
  418         const sc_p sc = (sc_p) tp->t_lsc;
  419 
  420         while (tp->t_outq.c_cc < NGT_HIWATER) { /* XXX 2.2 specific ? */
  421                 struct mbuf *m;
  422 
  423                 /* Remove first mbuf from queue */
  424                 IF_DEQUEUE(&sc->outq, m);
  425                 if (m == NULL)
  426                         break;
  427 
  428                 /* Send as much of it as possible */
  429                 while (m != NULL) {
  430                         int     sent;
  431 
  432                         sent = m->m_len
  433                             - b_to_q(mtod(m, u_char *), m->m_len, &tp->t_outq);
  434                         m->m_data += sent;
  435                         m->m_len -= sent;
  436                         if (m->m_len > 0)
  437                                 break;  /* device can't take no more */
  438                         m = m_free(m);
  439                 }
  440 
  441                 /* Put remainder of mbuf chain (if any) back on queue */
  442                 if (m != NULL) {
  443                         IF_PREPEND(&sc->outq, m);
  444                         break;
  445                 }
  446         }
  447 
  448         /* Call output process whether or not there is any output. We are
  449          * being called in lieu of ttstart and must do what it would. */
  450         tt_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         /* XXX: outq not locked */
  455         if (!IFQ_IS_EMPTY(&sc->outq) && !callout_pending(&sc->chand))
  456                 ng_callout(&sc->chand, sc->node, NULL, 1, ngt_timeout, NULL, 0);
  457 
  458         return (0);
  459 }
  460 
  461 /*
  462  * We still have data to output to the device, so try sending more.
  463  */
  464 static void
  465 ngt_timeout(node_p node, hook_p hook, void *arg1, int arg2)
  466 {
  467         const sc_p sc = NG_NODE_PRIVATE(node);
  468 
  469         mtx_lock(&Giant);
  470         ngt_start(sc->tp);
  471         mtx_unlock(&Giant);
  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
  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
  494 ngt_newhook(node_p node, hook_p hook, const char *name)
  495 {
  496         const sc_p sc = NG_NODE_PRIVATE(node);
  497 
  498         if (strcmp(name, NG_TTY_HOOK))
  499                 return (EINVAL);
  500 
  501         if (sc->hook)
  502                 return (EISCONN);
  503 
  504         NGTLOCK(sc);
  505         sc->hook = hook;
  506         NGTUNLOCK(sc);
  507 
  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
  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
  533 ngt_disconnect(hook_p hook)
  534 {
  535         const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  536 
  537         if (hook != sc->hook)
  538                 panic(__func__);
  539 
  540         NGTLOCK(sc);
  541         sc->hook = NULL;
  542         NGTUNLOCK(sc);
  543 
  544         return (0);
  545 }
  546 
  547 /*
  548  * Remove this node. The does the netgraph portion of the shutdown.
  549  * This should only be called indirectly from ngt_close().
  550  *
  551  * tp->t_lsc is already NULL, so we should be protected from
  552  * tty calls now.
  553  */
  554 static int
  555 ngt_shutdown(node_p node)
  556 {
  557         const sc_p sc = NG_NODE_PRIVATE(node);
  558 
  559         NGTLOCK(sc);
  560         if (!(sc->flags & FLG_DIE)) {
  561                 NGTUNLOCK(sc);
  562                 return (EOPNOTSUPP);
  563         }
  564         NGTUNLOCK(sc);
  565 
  566         /* Free resources */
  567         _IF_DRAIN(&sc->outq);
  568         mtx_destroy(&(sc)->outq.ifq_mtx);
  569         m_freem(sc->m);
  570         NG_NODE_UNREF(sc->node);
  571         FREE(sc, M_NETGRAPH);
  572 
  573         return (0);
  574 }
  575 
  576 /*
  577  * Receive incoming data from netgraph system. Put it on our
  578  * output queue and start output if necessary.
  579  */
  580 static int
  581 ngt_rcvdata(hook_p hook, item_p item)
  582 {
  583         const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  584         struct mbuf *m;
  585         int qlen;
  586 
  587         if (hook != sc->hook)
  588                 panic(__func__);
  589 
  590         NGI_GET_M(item, m);
  591         NG_FREE_ITEM(item);
  592 
  593         IF_LOCK(&sc->outq);
  594         if (_IF_QFULL(&sc->outq)) {
  595                 _IF_DROP(&sc->outq);
  596                 IF_UNLOCK(&sc->outq);
  597                 NG_FREE_M(m);
  598                 return (ENOBUFS);
  599         }
  600 
  601         _IF_ENQUEUE(&sc->outq, m);
  602         qlen = sc->outq.ifq_len;
  603         IF_UNLOCK(&sc->outq);
  604 
  605         /*
  606          * If qlen > 1, then we should already have a scheduled callout.
  607          */
  608         if (qlen == 1) {
  609                 mtx_lock(&Giant);
  610                 ngt_start(sc->tp);
  611                 mtx_unlock(&Giant);
  612         }
  613 
  614         return (0);
  615 }
  616 
  617 /*
  618  * Receive control message
  619  */
  620 static int
  621 ngt_rcvmsg(node_p node, item_p item, hook_p lasthook)
  622 {
  623         const sc_p sc = NG_NODE_PRIVATE(node);
  624         struct ng_mesg *msg, *resp = NULL;
  625         int error = 0;
  626 
  627         NGI_GET_MSG(item, msg);
  628         switch (msg->header.typecookie) {
  629         case NGM_TTY_COOKIE:
  630                 switch (msg->header.cmd) {
  631                 case NGM_TTY_SET_HOTCHAR:
  632                     {
  633                         int     hotchar;
  634 
  635                         if (msg->header.arglen != sizeof(int))
  636                                 ERROUT(EINVAL);
  637                         hotchar = *((int *) msg->data);
  638                         if (hotchar != (u_char) hotchar && hotchar != -1)
  639                                 ERROUT(EINVAL);
  640                         sc->hotchar = hotchar;  /* race condition is OK */
  641                         break;
  642                     }
  643                 case NGM_TTY_GET_HOTCHAR:
  644                         NG_MKRESPONSE(resp, msg, sizeof(int), M_NOWAIT);
  645                         if (!resp)
  646                                 ERROUT(ENOMEM);
  647                         /* Race condition here is OK */
  648                         *((int *) resp->data) = sc->hotchar;
  649                         break;
  650                 default:
  651                         ERROUT(EINVAL);
  652                 }
  653                 break;
  654         default:
  655                 ERROUT(EINVAL);
  656         }
  657 done:
  658         NG_RESPOND_MSG(error, node, item, resp);
  659         NG_FREE_MSG(msg);
  660         return (error);
  661 }
  662 
  663 /******************************************************************
  664                         INITIALIZATION
  665 ******************************************************************/
  666 
  667 /*
  668  * Handle loading and unloading for this node type
  669  */
  670 static int
  671 ngt_mod_event(module_t mod, int event, void *data)
  672 {
  673         int error = 0;
  674 
  675         switch (event) {
  676         case MOD_LOAD:
  677 
  678                 /* Register line discipline */
  679                 mtx_lock(&Giant);
  680                 if ((ngt_ldisc = ldisc_register(NETGRAPHDISC, &ngt_disc)) < 0) {
  681                         mtx_unlock(&Giant);
  682                         log(LOG_ERR, "%s: can't register line discipline",
  683                             __func__);
  684                         return (EIO);
  685                 }
  686                 mtx_unlock(&Giant);
  687                 break;
  688 
  689         case MOD_UNLOAD:
  690 
  691                 /* Unregister line discipline */
  692                 mtx_lock(&Giant);
  693                 ldisc_deregister(ngt_ldisc);
  694                 mtx_unlock(&Giant);
  695                 break;
  696 
  697         default:
  698                 error = EOPNOTSUPP;
  699                 break;
  700         }
  701         return (error);
  702 }

Cache object: c20dd66a7c73cc809319a6d496e98b7a


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.