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_frame_relay.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 
    2 /*
    3  * ng_frame_relay.c
    4  *
    5  * Copyright (c) 1996-1999 Whistle Communications, Inc.
    6  * All rights reserved.
    7  * 
    8  * Subject to the following obligations and disclaimer of warranty, use and
    9  * redistribution of this software, in source or object code forms, with or
   10  * without modifications are expressly permitted by Whistle Communications;
   11  * provided, however, that:
   12  * 1. Any and all reproductions of the source or object code must include the
   13  *    copyright notice above and the following disclaimer of warranties; and
   14  * 2. No rights are granted, in any manner or form, to use Whistle
   15  *    Communications, Inc. trademarks, including the mark "WHISTLE
   16  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
   17  *    such appears in the above copyright notice or in the software.
   18  * 
   19  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
   20  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
   21  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
   22  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
   23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
   24  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
   25  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
   26  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
   27  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
   28  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
   29  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
   30  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
   31  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
   32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   34  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
   35  * OF SUCH DAMAGE.
   36  *
   37  * Author: Julian Elischer <julian@freebsd.org>
   38  *
   39  * $FreeBSD: releng/5.3/sys/netgraph/ng_frame_relay.c 136588 2004-10-16 08:43:07Z cvs2svn $
   40  * $Whistle: ng_frame_relay.c,v 1.20 1999/11/01 09:24:51 julian Exp $
   41  */
   42 
   43 /*
   44  * This node implements the frame relay protocol, not including
   45  * the LMI line management. This means basically keeping track
   46  * of which DLCI's are active, doing frame (de)multiplexing, etc.
   47  *
   48  * It has a 'downstream' hook that goes to the line, and a
   49  * hook for each DLCI (eg, 'dlci16').
   50  */
   51 
   52 #include <sys/param.h>
   53 #include <sys/systm.h>
   54 #include <sys/kernel.h>
   55 #include <sys/errno.h>
   56 #include <sys/malloc.h>
   57 #include <sys/mbuf.h>
   58 #include <sys/syslog.h>
   59 #include <sys/ctype.h>
   60 
   61 #include <netgraph/ng_message.h>
   62 #include <netgraph/netgraph.h>
   63 #include <netgraph/ng_frame_relay.h>
   64 
   65 /*
   66  * Line info, and status per channel.
   67  */
   68 struct ctxinfo {                /* one per active hook */
   69         u_int   flags;
   70 #define CHAN_VALID      0x01    /* assigned to a channel */
   71 #define CHAN_ACTIVE     0x02    /* bottom level active */
   72         int     dlci;           /* the dlci assigned to this context */
   73         hook_p  hook;           /* if there's a hook assigned.. */
   74 };
   75 
   76 #define MAX_CT 16               /* # of dlci's active at a time (POWER OF 2!) */
   77 struct frmrel_softc {
   78         int     unit;           /* which card are we? */
   79         int     datahooks;      /* number of data hooks attached */
   80         node_p  node;           /* netgraph node */
   81         int     addrlen;        /* address header length */
   82         int     flags;          /* state */
   83         int     mtu;            /* guess */
   84         u_char  remote_seq;     /* sequence number the remote sent */
   85         u_char  local_seq;      /* sequence number the remote rcvd */
   86         u_short ALT[1024];      /* map DLCIs to CTX */
   87 #define CTX_VALID       0x8000          /* this bit means it's a valid CTX */
   88 #define CTX_VALUE       (MAX_CT - 1)    /* mask for context part */
   89         struct  ctxinfo channel[MAX_CT];
   90         struct  ctxinfo downstream;
   91 };
   92 typedef struct frmrel_softc *sc_p;
   93 
   94 #define BYTEX_EA        0x01    /* End Address. Always 0 on byte1 */
   95 #define BYTE1_C_R       0x02
   96 #define BYTE2_FECN      0x08    /* forwards congestion notification */
   97 #define BYTE2_BECN      0x04    /* Backward congestion notification */
   98 #define BYTE2_DE        0x02    /* Discard elligability */
   99 #define LASTBYTE_D_C    0x02    /* last byte is dl_core or dlci info */
  100 
  101 /* Used to do headers */
  102 const static struct segment {
  103         u_char  mask;
  104         u_char  shift;
  105         u_char  width;
  106 } makeup[] = {
  107         { 0xfc, 2, 6 },
  108         { 0xf0, 4, 4 },
  109         { 0xfe, 1, 7 },
  110         { 0xfc, 2, 6 }
  111 };
  112 
  113 #define SHIFTIN(segment, byte, dlci)                                         \
  114         {                                                                    \
  115                 (dlci) <<= (segment)->width;                                 \
  116                 (dlci) |=                                                    \
  117                         (((byte) & (segment)->mask) >> (segment)->shift);    \
  118         }
  119 
  120 #define SHIFTOUT(segment, byte, dlci)                                        \
  121         {                                                                    \
  122                 (byte) |= (((dlci) << (segment)->shift) & (segment)->mask);  \
  123                 (dlci) >>= (segment)->width;                                 \
  124         }
  125 
  126 /* Netgraph methods */
  127 static ng_constructor_t ngfrm_constructor;
  128 static ng_shutdown_t    ngfrm_shutdown;
  129 static ng_newhook_t     ngfrm_newhook;
  130 static ng_rcvdata_t     ngfrm_rcvdata;
  131 static ng_disconnect_t  ngfrm_disconnect;
  132 
  133 /* Other internal functions */
  134 static int ngfrm_decode(node_p node, item_p item);
  135 static int ngfrm_addrlen(char *hdr);
  136 static int ngfrm_allocate_CTX(sc_p sc, int dlci);
  137 
  138 /* Netgraph type */
  139 static struct ng_type typestruct = {
  140         .version =      NG_ABI_VERSION,
  141         .name =         NG_FRAMERELAY_NODE_TYPE,
  142         .constructor =  ngfrm_constructor,
  143         .shutdown =     ngfrm_shutdown,
  144         .newhook =      ngfrm_newhook,
  145         .rcvdata =      ngfrm_rcvdata,
  146         .disconnect =   ngfrm_disconnect,
  147 };
  148 NETGRAPH_INIT(framerelay, &typestruct);
  149 
  150 /*
  151  * Given a DLCI, return the index of the  context table entry for it,
  152  * Allocating a new one if needs be, or -1 if none available.
  153  */
  154 static int
  155 ngfrm_allocate_CTX(sc_p sc, int dlci)
  156 {
  157         u_int   ctxnum = -1;    /* what ctx number we are using */
  158         volatile struct ctxinfo *CTXp = NULL;
  159 
  160         /* Sanity check the dlci value */
  161         if (dlci > 1023)
  162                 return (-1);
  163 
  164         /* Check to see if we already have an entry for this DLCI */
  165         if (sc->ALT[dlci]) {
  166                 if ((ctxnum = sc->ALT[dlci] & CTX_VALUE) < MAX_CT) {
  167                         CTXp = sc->channel + ctxnum;
  168                 } else {
  169                         ctxnum = -1;
  170                         sc->ALT[dlci] = 0;      /* paranoid but... */
  171                 }
  172         }
  173 
  174         /*
  175          * If the index has no valid entry yet, then we need to allocate a
  176          * CTX number to it
  177          */
  178         if (CTXp == NULL) {
  179                 for (ctxnum = 0; ctxnum < MAX_CT; ctxnum++) {
  180                         /*
  181                          * If the VALID flag is empty it is unused
  182                          */
  183                         if ((sc->channel[ctxnum].flags & CHAN_VALID) == 0) {
  184                                 bzero(sc->channel + ctxnum,
  185                                       sizeof(struct ctxinfo));
  186                                 CTXp = sc->channel + ctxnum;
  187                                 sc->ALT[dlci] = ctxnum | CTX_VALID;
  188                                 sc->channel[ctxnum].dlci = dlci;
  189                                 sc->channel[ctxnum].flags = CHAN_VALID;
  190                                 break;
  191                         }
  192                 }
  193         }
  194 
  195         /*
  196          * If we still don't have a CTX pointer, then we never found a free
  197          * spot so give up now..
  198          */
  199         if (!CTXp) {
  200                 log(LOG_ERR, "No CTX available for dlci %d\n", dlci);
  201                 return (-1);
  202         }
  203         return (ctxnum);
  204 }
  205 
  206 /*
  207  * Node constructor
  208  */
  209 static int
  210 ngfrm_constructor(node_p node)
  211 {
  212         sc_p sc;
  213 
  214         MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
  215         if (!sc)
  216                 return (ENOMEM);
  217         sc->addrlen = 2;        /* default */
  218 
  219         /* Link the node and our private info */
  220         NG_NODE_SET_PRIVATE(node, sc);
  221         sc->node = node;
  222         return (0);
  223 }
  224 
  225 /*
  226  * Add a new hook
  227  *
  228  * We allow hooks called "debug", "downstream" and dlci[0-1023]
  229  * The hook's private info points to our stash of info about that
  230  * channel. A NULL pointer is debug and a DLCI of -1 means downstream.
  231  */
  232 static int
  233 ngfrm_newhook(node_p node, hook_p hook, const char *name)
  234 {
  235         const sc_p sc = NG_NODE_PRIVATE(node);
  236         const char *cp;
  237         char *eptr;
  238         int dlci = 0;
  239         int ctxnum;
  240 
  241         /* Check if it's our friend the control hook */
  242         if (strcmp(name, NG_FRAMERELAY_HOOK_DEBUG) == 0) {
  243                 NG_HOOK_SET_PRIVATE(hook, NULL);        /* paranoid */
  244                 return (0);
  245         }
  246 
  247         /*
  248          * All other hooks either start with 'dlci' and have a decimal
  249          * trailing channel number up to 4 digits, or are the downstream
  250          * hook.
  251          */
  252         if (strncmp(name, NG_FRAMERELAY_HOOK_DLCI,
  253             strlen(NG_FRAMERELAY_HOOK_DLCI)) != 0) {
  254 
  255                 /* It must be the downstream connection */
  256                 if (strcmp(name, NG_FRAMERELAY_HOOK_DOWNSTREAM) != 0)
  257                         return EINVAL;
  258 
  259                 /* Make sure we haven't already got one (paranoid) */
  260                 if (sc->downstream.hook)
  261                         return (EADDRINUSE);
  262 
  263                 /* OK add it */
  264                 NG_HOOK_SET_PRIVATE(hook, &sc->downstream);
  265                 sc->downstream.hook = hook;
  266                 sc->downstream.dlci = -1;
  267                 sc->downstream.flags |= CHAN_ACTIVE;
  268                 sc->datahooks++;
  269                 return (0);
  270         }
  271 
  272         /* Must be a dlci hook at this point */
  273         cp = name + strlen(NG_FRAMERELAY_HOOK_DLCI);
  274         if (!isdigit(*cp) || (cp[0] == '' && cp[1] != '\0'))
  275                 return (EINVAL);
  276         dlci = (int)strtoul(cp, &eptr, 10);
  277         if (*eptr != '\0' || dlci < 0 || dlci > 1023)
  278                 return (EINVAL);
  279 
  280         /*
  281          * We have a dlci, now either find it, or allocate it. It's possible
  282          * that we might have seen packets for it already and made an entry
  283          * for it.
  284          */
  285         ctxnum = ngfrm_allocate_CTX(sc, dlci);
  286         if (ctxnum == -1)
  287                 return (ENOBUFS);
  288 
  289         /*
  290          * Be paranoid: if it's got a hook already, that dlci is in use .
  291          * Generic code can not catch all the synonyms (e.g. dlci016 vs
  292          * dlci16)
  293          */
  294         if (sc->channel[ctxnum].hook != NULL)
  295                 return (EADDRINUSE);
  296 
  297         /*
  298          * Put our hooks into it (pun not intended)
  299          */
  300         sc->channel[ctxnum].flags |= CHAN_ACTIVE;
  301         NG_HOOK_SET_PRIVATE(hook, sc->channel + ctxnum);
  302         sc->channel[ctxnum].hook = hook;
  303         sc->datahooks++;
  304         return (0);
  305 }
  306 
  307 /*
  308  * Count up the size of the address header if we don't already know
  309  */
  310 int
  311 ngfrm_addrlen(char *hdr)
  312 {
  313         if (hdr[0] & BYTEX_EA)
  314                 return 0;
  315         if (hdr[1] & BYTEX_EA)
  316                 return 2;
  317         if (hdr[2] & BYTEX_EA)
  318                 return 3;
  319         if (hdr[3] & BYTEX_EA)
  320                 return 4;
  321         return 0;
  322 }
  323 
  324 /*
  325  * Receive data packet
  326  */
  327 static int
  328 ngfrm_rcvdata(hook_p hook, item_p item)
  329 {
  330         struct  ctxinfo *const ctxp = NG_HOOK_PRIVATE(hook);
  331         int     error = 0;
  332         int     dlci;
  333         sc_p    sc;
  334         int     alen;
  335         char   *data;
  336         struct mbuf *m;
  337 
  338         /* Data doesn't come in from just anywhere (e.g debug hook) */
  339         if (ctxp == NULL) {
  340                 error = ENETDOWN;
  341                 goto bad;
  342         }
  343 
  344         /* If coming from downstream, decode it to a channel */
  345         dlci = ctxp->dlci;
  346         if (dlci == -1)
  347                 return (ngfrm_decode(NG_HOOK_NODE(hook), item));
  348 
  349         NGI_GET_M(item, m);
  350         /* Derive the softc we will need */
  351         sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  352 
  353         /* If there is no live channel, throw it away */
  354         if ((sc->downstream.hook == NULL)
  355             || ((ctxp->flags & CHAN_ACTIVE) == 0)) {
  356                 error = ENETDOWN;
  357                 goto bad;
  358         }
  359 
  360         /* Store the DLCI on the front of the packet */
  361         alen = sc->addrlen;
  362         if (alen == 0)
  363                 alen = 2;       /* default value for transmit */
  364         M_PREPEND(m, alen, M_DONTWAIT);
  365         if (m == NULL) {
  366                 error = ENOBUFS;
  367                 goto bad;
  368         }
  369         data = mtod(m, char *);
  370 
  371         /*
  372          * Shift the lowest bits into the address field untill we are done.
  373          * First byte is MSBits of addr so work backwards.
  374          */
  375         switch (alen) {
  376         case 2:
  377                 data[0] = data[1] = '\0';
  378                 SHIFTOUT(makeup + 1, data[1], dlci);
  379                 SHIFTOUT(makeup + 0, data[0], dlci);
  380                 data[1] |= BYTEX_EA;
  381                 break;
  382         case 3:
  383                 data[0] = data[1] = data[2] = '\0';
  384                 SHIFTOUT(makeup + 3, data[2], dlci);    /* 3 and 2 is correct */
  385                 SHIFTOUT(makeup + 1, data[1], dlci);
  386                 SHIFTOUT(makeup + 0, data[0], dlci);
  387                 data[2] |= BYTEX_EA;
  388                 break;
  389         case 4:
  390                 data[0] = data[1] = data[2] = data[3] = '\0';
  391                 SHIFTOUT(makeup + 3, data[3], dlci);
  392                 SHIFTOUT(makeup + 2, data[2], dlci);
  393                 SHIFTOUT(makeup + 1, data[1], dlci);
  394                 SHIFTOUT(makeup + 0, data[0], dlci);
  395                 data[3] |= BYTEX_EA;
  396                 break;
  397         default:
  398                 panic(__func__);
  399         }
  400 
  401         /* Send it */
  402         NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);
  403         return (error);
  404 
  405 bad:
  406         NG_FREE_ITEM(item);
  407         NG_FREE_M(m);
  408         return (error);
  409 }
  410 
  411 /*
  412  * Decode an incoming frame coming from the switch
  413  */
  414 static int
  415 ngfrm_decode(node_p node, item_p item)
  416 {
  417         const sc_p  sc = NG_NODE_PRIVATE(node);
  418         char       *data;
  419         int         alen;
  420         u_int       dlci = 0;
  421         int         error = 0;
  422         int         ctxnum;
  423         struct mbuf *m;
  424 
  425         NGI_GET_M(item, m);
  426         if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
  427                 error = ENOBUFS;
  428                 goto out;
  429         }
  430         data = mtod(m, char *);
  431         if ((alen = sc->addrlen) == 0) {
  432                 sc->addrlen = alen = ngfrm_addrlen(data);
  433         }
  434         switch (alen) {
  435         case 2:
  436                 SHIFTIN(makeup + 0, data[0], dlci);
  437                 SHIFTIN(makeup + 1, data[1], dlci);
  438                 break;
  439         case 3:
  440                 SHIFTIN(makeup + 0, data[0], dlci);
  441                 SHIFTIN(makeup + 1, data[1], dlci);
  442                 SHIFTIN(makeup + 3, data[2], dlci);     /* 3 and 2 is correct */
  443                 break;
  444         case 4:
  445                 SHIFTIN(makeup + 0, data[0], dlci);
  446                 SHIFTIN(makeup + 1, data[1], dlci);
  447                 SHIFTIN(makeup + 2, data[2], dlci);
  448                 SHIFTIN(makeup + 3, data[3], dlci);
  449                 break;
  450         default:
  451                 error = EINVAL;
  452                 goto out;
  453         }
  454 
  455         if (dlci > 1023) {
  456                 error = EINVAL;
  457                 goto out;
  458         }
  459         ctxnum = sc->ALT[dlci];
  460         if ((ctxnum & CTX_VALID) && sc->channel[ctxnum &= CTX_VALUE].hook) {
  461                 /* Send it */
  462                 m_adj(m, alen);
  463                 NG_FWD_NEW_DATA(error, item, sc->channel[ctxnum].hook, m);
  464                 return (error);
  465         } else {
  466                 error = ENETDOWN;
  467         }
  468 out:
  469         NG_FREE_ITEM(item);
  470         NG_FREE_M(m);
  471         return (error);
  472 }
  473 
  474 /*
  475  * Shutdown node
  476  */
  477 static int
  478 ngfrm_shutdown(node_p node)
  479 {
  480         const sc_p sc = NG_NODE_PRIVATE(node);
  481 
  482         NG_NODE_SET_PRIVATE(node, NULL);
  483         FREE(sc, M_NETGRAPH);
  484         NG_NODE_UNREF(node);
  485         return (0);
  486 }
  487 
  488 /*
  489  * Hook disconnection
  490  *
  491  * Invalidate the private data associated with this dlci.
  492  * For this type, removal of the last link resets tries to destroy the node.
  493  */
  494 static int
  495 ngfrm_disconnect(hook_p hook)
  496 {
  497         const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  498         struct ctxinfo *const cp = NG_HOOK_PRIVATE(hook);
  499         int dlci;
  500 
  501         /* If it's a regular dlci hook, then free resources etc.. */
  502         if (cp != NULL) {
  503                 cp->hook = NULL;
  504                 dlci = cp->dlci;
  505                 if (dlci != -1)
  506                         sc->ALT[dlci] = 0;
  507                 cp->flags = 0;
  508                 sc->datahooks--;
  509         }
  510         if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
  511         && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
  512                 ng_rmnode_self(NG_HOOK_NODE(hook));
  513         return (0);
  514 }

Cache object: 4b143a9ea60a870d79846bf6d52dd6e3


[ 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.