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  * ng_frame_relay.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: Julian Elischer <julian@freebsd.org>
   39  *
   40  * $FreeBSD$
   41  * $Whistle: ng_frame_relay.c,v 1.20 1999/11/01 09:24:51 julian Exp $
   42  */
   43 
   44 /*
   45  * This node implements the frame relay protocol, not including
   46  * the LMI line management. This means basically keeping track
   47  * of which DLCI's are active, doing frame (de)multiplexing, etc.
   48  *
   49  * It has a 'downstream' hook that goes to the line, and a
   50  * hook for each DLCI (eg, 'dlci16').
   51  */
   52 
   53 #include <sys/param.h>
   54 #include <sys/systm.h>
   55 #include <sys/kernel.h>
   56 #include <sys/errno.h>
   57 #include <sys/malloc.h>
   58 #include <sys/mbuf.h>
   59 #include <sys/syslog.h>
   60 #include <sys/ctype.h>
   61 
   62 #include <netgraph/ng_message.h>
   63 #include <netgraph/netgraph.h>
   64 #include <netgraph/ng_frame_relay.h>
   65 
   66 /*
   67  * Line info, and status per channel.
   68  */
   69 struct ctxinfo {                /* one per active hook */
   70         u_int   flags;
   71 #define CHAN_VALID      0x01    /* assigned to a channel */
   72 #define CHAN_ACTIVE     0x02    /* bottom level active */
   73         int     dlci;           /* the dlci assigned to this context */
   74         hook_p  hook;           /* if there's a hook assigned.. */
   75 };
   76 
   77 #define MAX_CT 16               /* # of dlci's active at a time (POWER OF 2!) */
   78 struct frmrel_softc {
   79         int     unit;           /* which card are we? */
   80         int     datahooks;      /* number of data hooks attached */
   81         node_p  node;           /* netgraph node */
   82         int     addrlen;        /* address header length */
   83         int     flags;          /* state */
   84         int     mtu;            /* guess */
   85         u_char  remote_seq;     /* sequence number the remote sent */
   86         u_char  local_seq;      /* sequence number the remote rcvd */
   87         u_short ALT[1024];      /* map DLCIs to CTX */
   88 #define CTX_VALID       0x8000          /* this bit means it's a valid CTX */
   89 #define CTX_VALUE       (MAX_CT - 1)    /* mask for context part */
   90         struct  ctxinfo channel[MAX_CT];
   91         struct  ctxinfo downstream;
   92 };
   93 typedef struct frmrel_softc *sc_p;
   94 
   95 #define BYTEX_EA        0x01    /* End Address. Always 0 on byte1 */
   96 #define BYTE1_C_R       0x02
   97 #define BYTE2_FECN      0x08    /* forwards congestion notification */
   98 #define BYTE2_BECN      0x04    /* Backward congestion notification */
   99 #define BYTE2_DE        0x02    /* Discard elligability */
  100 #define LASTBYTE_D_C    0x02    /* last byte is dl_core or dlci info */
  101 
  102 /* Used to do headers */
  103 const static struct segment {
  104         u_char  mask;
  105         u_char  shift;
  106         u_char  width;
  107 } makeup[] = {
  108         { 0xfc, 2, 6 },
  109         { 0xf0, 4, 4 },
  110         { 0xfe, 1, 7 },
  111         { 0xfc, 2, 6 }
  112 };
  113 
  114 #define SHIFTIN(segment, byte, dlci)                                         \
  115         {                                                                    \
  116                 (dlci) <<= (segment)->width;                                 \
  117                 (dlci) |=                                                    \
  118                         (((byte) & (segment)->mask) >> (segment)->shift);    \
  119         }
  120 
  121 #define SHIFTOUT(segment, byte, dlci)                                        \
  122         {                                                                    \
  123                 (byte) |= (((dlci) << (segment)->shift) & (segment)->mask);  \
  124                 (dlci) >>= (segment)->width;                                 \
  125         }
  126 
  127 /* Netgraph methods */
  128 static ng_constructor_t ngfrm_constructor;
  129 static ng_shutdown_t    ngfrm_shutdown;
  130 static ng_newhook_t     ngfrm_newhook;
  131 static ng_rcvdata_t     ngfrm_rcvdata;
  132 static ng_disconnect_t  ngfrm_disconnect;
  133 
  134 /* Other internal functions */
  135 static int ngfrm_decode(node_p node, item_p item);
  136 static int ngfrm_addrlen(char *hdr);
  137 static int ngfrm_allocate_CTX(sc_p sc, int dlci);
  138 
  139 /* Netgraph type */
  140 static struct ng_type typestruct = {
  141         .version =      NG_ABI_VERSION,
  142         .name =         NG_FRAMERELAY_NODE_TYPE,
  143         .constructor =  ngfrm_constructor,
  144         .shutdown =     ngfrm_shutdown,
  145         .newhook =      ngfrm_newhook,
  146         .rcvdata =      ngfrm_rcvdata,
  147         .disconnect =   ngfrm_disconnect,
  148 };
  149 NETGRAPH_INIT(framerelay, &typestruct);
  150 
  151 #define ERROUT(x)               do { error = (x); goto done; } while (0)
  152 
  153 /*
  154  * Given a DLCI, return the index of the  context table entry for it,
  155  * Allocating a new one if needs be, or -1 if none available.
  156  */
  157 static int
  158 ngfrm_allocate_CTX(sc_p sc, int dlci)
  159 {
  160         u_int   ctxnum = -1;    /* what ctx number we are using */
  161         volatile struct ctxinfo *CTXp = NULL;
  162 
  163         /* Sanity check the dlci value */
  164         if (dlci > 1023)
  165                 return (-1);
  166 
  167         /* Check to see if we already have an entry for this DLCI */
  168         if (sc->ALT[dlci]) {
  169                 if ((ctxnum = sc->ALT[dlci] & CTX_VALUE) < MAX_CT) {
  170                         CTXp = sc->channel + ctxnum;
  171                 } else {
  172                         ctxnum = -1;
  173                         sc->ALT[dlci] = 0;      /* paranoid but... */
  174                 }
  175         }
  176 
  177         /*
  178          * If the index has no valid entry yet, then we need to allocate a
  179          * CTX number to it
  180          */
  181         if (CTXp == NULL) {
  182                 for (ctxnum = 0; ctxnum < MAX_CT; ctxnum++) {
  183                         /*
  184                          * If the VALID flag is empty it is unused
  185                          */
  186                         if ((sc->channel[ctxnum].flags & CHAN_VALID) == 0) {
  187                                 bzero(sc->channel + ctxnum,
  188                                       sizeof(struct ctxinfo));
  189                                 CTXp = sc->channel + ctxnum;
  190                                 sc->ALT[dlci] = ctxnum | CTX_VALID;
  191                                 sc->channel[ctxnum].dlci = dlci;
  192                                 sc->channel[ctxnum].flags = CHAN_VALID;
  193                                 break;
  194                         }
  195                 }
  196         }
  197 
  198         /*
  199          * If we still don't have a CTX pointer, then we never found a free
  200          * spot so give up now..
  201          */
  202         if (!CTXp) {
  203                 log(LOG_ERR, "No CTX available for dlci %d\n", dlci);
  204                 return (-1);
  205         }
  206         return (ctxnum);
  207 }
  208 
  209 /*
  210  * Node constructor
  211  */
  212 static int
  213 ngfrm_constructor(node_p node)
  214 {
  215         sc_p sc;
  216 
  217         sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
  218         sc->addrlen = 2;        /* default */
  219 
  220         /* Link the node and our private info */
  221         NG_NODE_SET_PRIVATE(node, sc);
  222         sc->node = node;
  223         return (0);
  224 }
  225 
  226 /*
  227  * Add a new hook
  228  *
  229  * We allow hooks called "debug", "downstream" and dlci[0-1023]
  230  * The hook's private info points to our stash of info about that
  231  * channel. A NULL pointer is debug and a DLCI of -1 means downstream.
  232  */
  233 static int
  234 ngfrm_newhook(node_p node, hook_p hook, const char *name)
  235 {
  236         const sc_p sc = NG_NODE_PRIVATE(node);
  237         const char *cp;
  238         char *eptr;
  239         int dlci = 0;
  240         int ctxnum;
  241 
  242         /* Check if it's our friend the control hook */
  243         if (strcmp(name, NG_FRAMERELAY_HOOK_DEBUG) == 0) {
  244                 NG_HOOK_SET_PRIVATE(hook, NULL);        /* paranoid */
  245                 return (0);
  246         }
  247 
  248         /*
  249          * All other hooks either start with 'dlci' and have a decimal
  250          * trailing channel number up to 4 digits, or are the downstream
  251          * hook.
  252          */
  253         if (strncmp(name, NG_FRAMERELAY_HOOK_DLCI,
  254             strlen(NG_FRAMERELAY_HOOK_DLCI)) != 0) {
  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         struct  mbuf *m = NULL;
  332         int     error = 0;
  333         int     dlci;
  334         sc_p    sc;
  335         int     alen;
  336         char   *data;
  337 
  338         /* Data doesn't come in from just anywhere (e.g debug hook) */
  339         if (ctxp == NULL)
  340                 ERROUT(ENETDOWN);
  341 
  342         /* If coming from downstream, decode it to a channel */
  343         dlci = ctxp->dlci;
  344         if (dlci == -1)
  345                 return (ngfrm_decode(NG_HOOK_NODE(hook), item));
  346 
  347         NGI_GET_M(item, m);
  348         /* Derive the softc we will need */
  349         sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  350 
  351         /* If there is no live channel, throw it away */
  352         if ((sc->downstream.hook == NULL)
  353             || ((ctxp->flags & CHAN_ACTIVE) == 0))
  354                 ERROUT(ENETDOWN);
  355 
  356         /* Store the DLCI on the front of the packet */
  357         alen = sc->addrlen;
  358         if (alen == 0)
  359                 alen = 2;       /* default value for transmit */
  360         M_PREPEND(m, alen, M_NOWAIT);
  361         if (m == NULL)
  362                 ERROUT(ENOBUFS);
  363         data = mtod(m, char *);
  364 
  365         /*
  366          * Shift the lowest bits into the address field until we are done.
  367          * First byte is MSBits of addr so work backwards.
  368          */
  369         switch (alen) {
  370         case 2:
  371                 data[0] = data[1] = '\0';
  372                 SHIFTOUT(makeup + 1, data[1], dlci);
  373                 SHIFTOUT(makeup + 0, data[0], dlci);
  374                 data[1] |= BYTEX_EA;
  375                 break;
  376         case 3:
  377                 data[0] = data[1] = data[2] = '\0';
  378                 SHIFTOUT(makeup + 3, data[2], dlci);    /* 3 and 2 is correct */
  379                 SHIFTOUT(makeup + 1, data[1], dlci);
  380                 SHIFTOUT(makeup + 0, data[0], dlci);
  381                 data[2] |= BYTEX_EA;
  382                 break;
  383         case 4:
  384                 data[0] = data[1] = data[2] = data[3] = '\0';
  385                 SHIFTOUT(makeup + 3, data[3], dlci);
  386                 SHIFTOUT(makeup + 2, data[2], dlci);
  387                 SHIFTOUT(makeup + 1, data[1], dlci);
  388                 SHIFTOUT(makeup + 0, data[0], dlci);
  389                 data[3] |= BYTEX_EA;
  390                 break;
  391         default:
  392                 panic("%s", __func__);
  393         }
  394 
  395         /* Send it */
  396         NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);
  397         return (error);
  398 
  399 done:
  400         NG_FREE_ITEM(item);
  401         NG_FREE_M(m);
  402         return (error);
  403 }
  404 
  405 /*
  406  * Decode an incoming frame coming from the switch
  407  */
  408 static int
  409 ngfrm_decode(node_p node, item_p item)
  410 {
  411         const sc_p  sc = NG_NODE_PRIVATE(node);
  412         char       *data;
  413         int         alen;
  414         u_int       dlci = 0;
  415         int         error = 0;
  416         int         ctxnum;
  417         struct mbuf *m;
  418 
  419         NGI_GET_M(item, m);
  420         if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)
  421                 ERROUT(ENOBUFS);
  422         data = mtod(m, char *);
  423         if ((alen = sc->addrlen) == 0) {
  424                 sc->addrlen = alen = ngfrm_addrlen(data);
  425         }
  426         switch (alen) {
  427         case 2:
  428                 SHIFTIN(makeup + 0, data[0], dlci);
  429                 SHIFTIN(makeup + 1, data[1], dlci);
  430                 break;
  431         case 3:
  432                 SHIFTIN(makeup + 0, data[0], dlci);
  433                 SHIFTIN(makeup + 1, data[1], dlci);
  434                 SHIFTIN(makeup + 3, data[2], dlci);     /* 3 and 2 is correct */
  435                 break;
  436         case 4:
  437                 SHIFTIN(makeup + 0, data[0], dlci);
  438                 SHIFTIN(makeup + 1, data[1], dlci);
  439                 SHIFTIN(makeup + 2, data[2], dlci);
  440                 SHIFTIN(makeup + 3, data[3], dlci);
  441                 break;
  442         default:
  443                 ERROUT(EINVAL);
  444         }
  445 
  446         if (dlci > 1023)
  447                 ERROUT(EINVAL);
  448         ctxnum = sc->ALT[dlci];
  449         if ((ctxnum & CTX_VALID) && sc->channel[ctxnum &= CTX_VALUE].hook) {
  450                 /* Send it */
  451                 m_adj(m, alen);
  452                 NG_FWD_NEW_DATA(error, item, sc->channel[ctxnum].hook, m);
  453                 return (error);
  454         } else {
  455                 error = ENETDOWN;
  456         }
  457 done:
  458         NG_FREE_ITEM(item);
  459         NG_FREE_M(m);
  460         return (error);
  461 }
  462 
  463 /*
  464  * Shutdown node
  465  */
  466 static int
  467 ngfrm_shutdown(node_p node)
  468 {
  469         const sc_p sc = NG_NODE_PRIVATE(node);
  470 
  471         NG_NODE_SET_PRIVATE(node, NULL);
  472         free(sc, M_NETGRAPH);
  473         NG_NODE_UNREF(node);
  474         return (0);
  475 }
  476 
  477 /*
  478  * Hook disconnection
  479  *
  480  * Invalidate the private data associated with this dlci.
  481  * For this type, removal of the last link resets tries to destroy the node.
  482  */
  483 static int
  484 ngfrm_disconnect(hook_p hook)
  485 {
  486         const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
  487         struct ctxinfo *const cp = NG_HOOK_PRIVATE(hook);
  488         int dlci;
  489 
  490         /* If it's a regular dlci hook, then free resources etc.. */
  491         if (cp != NULL) {
  492                 cp->hook = NULL;
  493                 dlci = cp->dlci;
  494                 if (dlci != -1)
  495                         sc->ALT[dlci] = 0;
  496                 cp->flags = 0;
  497                 sc->datahooks--;
  498         }
  499         if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
  500         && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
  501                 ng_rmnode_self(NG_HOOK_NODE(hook));
  502         return (0);
  503 }

Cache object: a8d15882539008b006eff65eefe6abcf


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