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-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 /*
    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 Elisher <julian@freebsd.org>
   38  *
   39  * $FreeBSD: stable/3/sys/netgraph/ng_frame_relay.c 67532 2000-10-24 22:45:12Z julian $
   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 #include <machine/clock.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 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_rmnode;
  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, struct mbuf * m, meta_p meta);
  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         NG_VERSION,
  142         NG_FRAMERELAY_NODE_TYPE,
  143         NULL,
  144         ngfrm_constructor,
  145         NULL,
  146         ngfrm_rmnode,
  147         ngfrm_newhook,
  148         NULL,
  149         NULL,
  150         ngfrm_rcvdata,
  151         ngfrm_rcvdata,
  152         ngfrm_disconnect,
  153         NULL
  154 };
  155 NETGRAPH_INIT(framerelay, &typestruct);
  156 
  157 /*
  158  * Given a DLCI, return the index of the  context table entry for it,
  159  * Allocating a new one if needs be, or -1 if none available.
  160  */
  161 static int
  162 ngfrm_allocate_CTX(sc_p sc, int dlci)
  163 {
  164         u_int   ctxnum = -1;    /* what ctx number we are using */
  165         volatile struct ctxinfo *CTXp = NULL;
  166 
  167         /* Sanity check the dlci value */
  168         if (dlci > 1023)
  169                 return (-1);
  170 
  171         /* Check to see if we already have an entry for this DLCI */
  172         if (sc->ALT[dlci]) {
  173                 if ((ctxnum = sc->ALT[dlci] & CTX_VALUE) < MAX_CT) {
  174                         CTXp = sc->channel + ctxnum;
  175                 } else {
  176                         ctxnum = -1;
  177                         sc->ALT[dlci] = 0;      /* paranoid but... */
  178                 }
  179         }
  180 
  181         /*
  182          * If the index has no valid entry yet, then we need to allocate a
  183          * CTX number to it
  184          */
  185         if (CTXp == NULL) {
  186                 for (ctxnum = 0; ctxnum < MAX_CT; ctxnum++) {
  187                         /*
  188                          * If the VALID flag is empty it is unused
  189                          */
  190                         if ((sc->channel[ctxnum].flags & CHAN_VALID) == 0) {
  191                                 bzero(sc->channel + ctxnum,
  192                                       sizeof(struct ctxinfo));
  193                                 CTXp = sc->channel + ctxnum;
  194                                 sc->ALT[dlci] = ctxnum | CTX_VALID;
  195                                 sc->channel[ctxnum].dlci = dlci;
  196                                 sc->channel[ctxnum].flags = CHAN_VALID;
  197                                 break;
  198                         }
  199                 }
  200         }
  201 
  202         /*
  203          * If we still don't have a CTX pointer, then we never found a free
  204          * spot so give up now..
  205          */
  206         if (!CTXp) {
  207                 log(LOG_ERR, "No CTX available for dlci %d\n", dlci);
  208                 return (-1);
  209         }
  210         return (ctxnum);
  211 }
  212 
  213 /*
  214  * Node constructor
  215  */
  216 static int
  217 ngfrm_constructor(node_p *nodep)
  218 {
  219         sc_p sc;
  220         int error = 0;
  221 
  222         MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT);
  223         if (!sc)
  224                 return (ENOMEM);
  225         bzero(sc, sizeof(*sc));
  226         if ((error = ng_make_node_common(&typestruct, nodep))) {
  227                 FREE(sc, M_NETGRAPH);
  228                 return (error);
  229         }
  230         sc->addrlen = 2;        /* default */
  231 
  232         /* Link the node and our private info */
  233         (*nodep)->private = sc;
  234         sc->node = *nodep;
  235         return (0);
  236 }
  237 
  238 /*
  239  * Add a new hook
  240  *
  241  * We allow hooks called "debug", "downstream" and dlci[0-1023]
  242  * The hook's private info points to our stash of info about that
  243  * channel. A NULL pointer is debug and a DLCI of -1 means downstream.
  244  */
  245 static int
  246 ngfrm_newhook(node_p node, hook_p hook, const char *name)
  247 {
  248         const sc_p sc = node->private;
  249         const char *cp;
  250         char *eptr;
  251         int dlci = 0;
  252         int ctxnum;
  253 
  254         /* Check if it's our friend the control hook */
  255         if (strcmp(name, NG_FRAMERELAY_HOOK_DEBUG) == 0) {
  256                 hook->private = NULL;   /* paranoid */
  257                 return (0);
  258         }
  259 
  260         /*
  261          * All other hooks either start with 'dlci' and have a decimal
  262          * trailing channel number up to 4 digits, or are the downstream
  263          * hook.
  264          */
  265         if (strncmp(name, NG_FRAMERELAY_HOOK_DLCI,
  266             strlen(NG_FRAMERELAY_HOOK_DLCI)) != 0) {
  267 
  268                 /* It must be the downstream connection */
  269                 if (strcmp(name, NG_FRAMERELAY_HOOK_DOWNSTREAM) != 0)
  270                         return EINVAL;
  271 
  272                 /* Make sure we haven't already got one (paranoid) */
  273                 if (sc->downstream.hook)
  274                         return (EADDRINUSE);
  275 
  276                 /* OK add it */
  277                 hook->private = &sc->downstream;
  278                 sc->downstream.hook = hook;
  279                 sc->downstream.dlci = -1;
  280                 sc->downstream.flags |= CHAN_ACTIVE;
  281                 sc->datahooks++;
  282                 return (0);
  283         }
  284 
  285         /* Must be a dlci hook at this point */
  286         cp = name + strlen(NG_FRAMERELAY_HOOK_DLCI);
  287         if (!isdigit(*cp) || (cp[0] == '' && cp[1] != '\0'))
  288                 return (EINVAL);
  289         dlci = (int)strtoul(cp, &eptr, 10);
  290         if (*eptr != '\0' || dlci < 0 || dlci > 1023)
  291                 return (EINVAL);
  292 
  293         /*
  294          * We have a dlci, now either find it, or allocate it. It's possible
  295          * that we might have seen packets for it already and made an entry
  296          * for it.
  297          */
  298         ctxnum = ngfrm_allocate_CTX(sc, dlci);
  299         if (ctxnum == -1)
  300                 return (ENOBUFS);
  301 
  302         /*
  303          * Be paranoid: if it's got a hook already, that dlci is in use .
  304          * Generic code can not catch all the synonyms (e.g. dlci016 vs
  305          * dlci16)
  306          */
  307         if (sc->channel[ctxnum].hook != NULL)
  308                 return (EADDRINUSE);
  309 
  310         /*
  311          * Put our hooks into it (pun not intended)
  312          */
  313         sc->channel[ctxnum].flags |= CHAN_ACTIVE;
  314         hook->private = sc->channel + ctxnum;
  315         sc->channel[ctxnum].hook = hook;
  316         sc->datahooks++;
  317         return (0);
  318 }
  319 
  320 /*
  321  * Count up the size of the address header if we don't already know
  322  */
  323 int
  324 ngfrm_addrlen(char *hdr)
  325 {
  326         if (hdr[0] & BYTEX_EA)
  327                 return 0;
  328         if (hdr[1] & BYTEX_EA)
  329                 return 2;
  330         if (hdr[2] & BYTEX_EA)
  331                 return 3;
  332         if (hdr[3] & BYTEX_EA)
  333                 return 4;
  334         return 0;
  335 }
  336 
  337 /*
  338  * Receive data packet
  339  */
  340 static int
  341 ngfrm_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
  342 {
  343         struct  ctxinfo *const ctxp = hook->private;
  344         int     error = 0;
  345         int     dlci;
  346         sc_p    sc;
  347         int     alen;
  348         char   *data;
  349 
  350         /* Data doesn't come in from just anywhere (e.g debug hook) */
  351         if (ctxp == NULL) {
  352                 error = ENETDOWN;
  353                 goto bad;
  354         }
  355 
  356         /* If coming from downstream, decode it to a channel */
  357         dlci = ctxp->dlci;
  358         if (dlci == -1)
  359                 return (ngfrm_decode(hook->node, m, meta));
  360 
  361         /* Derive the softc we will need */
  362         sc = hook->node->private;
  363 
  364         /* If there is no live channel, throw it away */
  365         if ((sc->downstream.hook == NULL)
  366             || ((ctxp->flags & CHAN_ACTIVE) == 0)) {
  367                 error = ENETDOWN;
  368                 goto bad;
  369         }
  370 
  371         /* Store the DLCI on the front of the packet */
  372         alen = sc->addrlen;
  373         if (alen == 0)
  374                 alen = 2;       /* default value for transmit */
  375         M_PREPEND(m, alen, M_DONTWAIT);
  376         if (m == NULL) {
  377                 error = ENOBUFS;
  378                 goto bad;
  379         }
  380         data = mtod(m, char *);
  381 
  382         /*
  383          * Shift the lowest bits into the address field untill we are done.
  384          * First byte is MSBits of addr so work backwards.
  385          */
  386         switch (alen) {
  387         case 2:
  388                 data[0] = data[1] = '\0';
  389                 SHIFTOUT(makeup + 1, data[1], dlci);
  390                 SHIFTOUT(makeup + 0, data[0], dlci);
  391                 data[1] |= BYTEX_EA;
  392                 break;
  393         case 3:
  394                 data[0] = data[1] = data[2] = '\0';
  395                 SHIFTOUT(makeup + 3, data[2], dlci);    /* 3 and 2 is correct */
  396                 SHIFTOUT(makeup + 1, data[1], dlci);
  397                 SHIFTOUT(makeup + 0, data[0], dlci);
  398                 data[2] |= BYTEX_EA;
  399                 break;
  400         case 4:
  401                 data[0] = data[1] = data[2] = data[3] = '\0';
  402                 SHIFTOUT(makeup + 3, data[3], dlci);
  403                 SHIFTOUT(makeup + 2, data[2], dlci);
  404                 SHIFTOUT(makeup + 1, data[1], dlci);
  405                 SHIFTOUT(makeup + 0, data[0], dlci);
  406                 data[3] |= BYTEX_EA;
  407                 break;
  408         default:
  409                 panic(__FUNCTION__);
  410         }
  411 
  412         /* Send it */
  413         NG_SEND_DATA(error, sc->downstream.hook, m, meta);
  414         return (error);
  415 
  416 bad:
  417         NG_FREE_DATA(m, meta);
  418         return (error);
  419 }
  420 
  421 /*
  422  * Decode an incoming frame coming from the switch
  423  */
  424 static int
  425 ngfrm_decode(node_p node, struct mbuf *m, meta_p meta)
  426 {
  427         const sc_p  sc = node->private;
  428         char       *data;
  429         int         alen;
  430         u_int       dlci = 0;
  431         int         error = 0;
  432         int         ctxnum;
  433 
  434         if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
  435                 error = ENOBUFS;
  436                 goto out;
  437         }
  438         data = mtod(m, char *);
  439         if ((alen = sc->addrlen) == 0) {
  440                 sc->addrlen = alen = ngfrm_addrlen(data);
  441         }
  442         switch (alen) {
  443         case 2:
  444                 SHIFTIN(makeup + 0, data[0], dlci);
  445                 SHIFTIN(makeup + 1, data[1], dlci);
  446                 break;
  447         case 3:
  448                 SHIFTIN(makeup + 0, data[0], dlci);
  449                 SHIFTIN(makeup + 1, data[1], dlci);
  450                 SHIFTIN(makeup + 3, data[2], dlci);     /* 3 and 2 is correct */
  451                 break;
  452         case 4:
  453                 SHIFTIN(makeup + 0, data[0], dlci);
  454                 SHIFTIN(makeup + 1, data[1], dlci);
  455                 SHIFTIN(makeup + 2, data[2], dlci);
  456                 SHIFTIN(makeup + 3, data[3], dlci);
  457                 break;
  458         default:
  459                 error = EINVAL;
  460                 goto out;
  461         }
  462 
  463         if (dlci > 1023) {
  464                 error = EINVAL;
  465                 goto out;
  466         }
  467         ctxnum = sc->ALT[dlci];
  468         if ((ctxnum & CTX_VALID) && sc->channel[ctxnum &= CTX_VALUE].hook) {
  469                 /* Send it */
  470                 m_adj(m, alen);
  471                 NG_SEND_DATA(error, sc->channel[ctxnum].hook, m, meta);
  472                 return (error);
  473         } else {
  474                 error = ENETDOWN;
  475         }
  476 out:
  477         NG_FREE_DATA(m, meta);
  478         return (error);
  479 }
  480 
  481 /*
  482  * Shutdown node
  483  */
  484 static int
  485 ngfrm_rmnode(node_p node)
  486 {
  487         const sc_p sc = node->private;
  488 
  489         node->flags |= NG_INVALID;
  490         ng_cutlinks(node);
  491         ng_unname(node);
  492         node->private = NULL;
  493         FREE(sc, M_NETGRAPH);
  494         ng_unref(sc->node);
  495         return (0);
  496 }
  497 
  498 /*
  499  * Hook disconnection
  500  *
  501  * Invalidate the private data associated with this dlci.
  502  * For this type, removal of the last link resets tries to destroy the node.
  503  */
  504 static int
  505 ngfrm_disconnect(hook_p hook)
  506 {
  507         const sc_p sc = hook->node->private;
  508         struct ctxinfo *const cp = hook->private;
  509         int dlci;
  510 
  511         /* If it's a regular dlci hook, then free resources etc.. */
  512         if (cp != NULL) {
  513                 cp->hook = NULL;
  514                 dlci = cp->dlci;
  515                 if (dlci != -1)
  516                         sc->ALT[dlci] = 0;
  517                 cp->flags = 0;
  518                 sc->datahooks--;
  519         }
  520         if (hook->node->numhooks == 0)
  521                 ng_rmnode(hook->node);
  522         return (0);
  523 }

Cache object: fd64163ff004b398b65d639ffc4e8c00


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