/*1* ng_frame_relay.c2*/34/*-5* Copyright (c) 1996-1999 Whistle Communications, Inc.6* All rights reserved.7*8* Subject to the following obligations and disclaimer of warranty, use and9* redistribution of this software, in source or object code forms, with or10* 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 the13* copyright notice above and the following disclaimer of warranties; and14* 2. No rights are granted, in any manner or form, to use Whistle15* Communications, Inc. trademarks, including the mark "WHISTLE16* COMMUNICATIONS" on advertising, endorsements, or otherwise except as17* such appears in the above copyright notice or in the software.18*19* THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND20* TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO21* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,22* INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF23* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.24* WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY25* REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS26* SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.27* IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES28* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING29* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,30* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR31* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY32* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT33* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF34* THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY35* OF SUCH DAMAGE.36*37* Author: Julian Elischer <[email protected]>38* $Whistle: ng_frame_relay.c,v 1.20 1999/11/01 09:24:51 julian Exp $39*/4041/*42* This node implements the frame relay protocol, not including43* the LMI line management. This means basically keeping track44* of which DLCI's are active, doing frame (de)multiplexing, etc.45*46* It has a 'downstream' hook that goes to the line, and a47* hook for each DLCI (eg, 'dlci16').48*/4950#include <sys/param.h>51#include <sys/systm.h>52#include <sys/kernel.h>53#include <sys/errno.h>54#include <sys/malloc.h>55#include <sys/mbuf.h>56#include <sys/syslog.h>57#include <sys/ctype.h>5859#include <netgraph/ng_message.h>60#include <netgraph/netgraph.h>61#include <netgraph/ng_frame_relay.h>6263/*64* Line info, and status per channel.65*/66struct ctxinfo { /* one per active hook */67u_int flags;68#define CHAN_VALID 0x01 /* assigned to a channel */69#define CHAN_ACTIVE 0x02 /* bottom level active */70int dlci; /* the dlci assigned to this context */71hook_p hook; /* if there's a hook assigned.. */72};7374#define MAX_CT 16 /* # of dlci's active at a time (POWER OF 2!) */75struct frmrel_softc {76int unit; /* which card are we? */77int datahooks; /* number of data hooks attached */78node_p node; /* netgraph node */79int addrlen; /* address header length */80int flags; /* state */81int mtu; /* guess */82u_char remote_seq; /* sequence number the remote sent */83u_char local_seq; /* sequence number the remote rcvd */84u_short ALT[1024]; /* map DLCIs to CTX */85#define CTX_VALID 0x8000 /* this bit means it's a valid CTX */86#define CTX_VALUE (MAX_CT - 1) /* mask for context part */87struct ctxinfo channel[MAX_CT];88struct ctxinfo downstream;89};90typedef struct frmrel_softc *sc_p;9192#define BYTEX_EA 0x01 /* End Address. Always 0 on byte1 */93#define BYTE1_C_R 0x0294#define BYTE2_FECN 0x08 /* forwards congestion notification */95#define BYTE2_BECN 0x04 /* Backward congestion notification */96#define BYTE2_DE 0x02 /* Discard elligability */97#define LASTBYTE_D_C 0x02 /* last byte is dl_core or dlci info */9899/* Used to do headers */100const static struct segment {101u_char mask;102u_char shift;103u_char width;104} makeup[] = {105{ 0xfc, 2, 6 },106{ 0xf0, 4, 4 },107{ 0xfe, 1, 7 },108{ 0xfc, 2, 6 }109};110111#define SHIFTIN(segment, byte, dlci) \112{ \113(dlci) <<= (segment)->width; \114(dlci) |= \115(((byte) & (segment)->mask) >> (segment)->shift); \116}117118#define SHIFTOUT(segment, byte, dlci) \119{ \120(byte) |= (((dlci) << (segment)->shift) & (segment)->mask); \121(dlci) >>= (segment)->width; \122}123124/* Netgraph methods */125static ng_constructor_t ngfrm_constructor;126static ng_shutdown_t ngfrm_shutdown;127static ng_newhook_t ngfrm_newhook;128static ng_rcvdata_t ngfrm_rcvdata;129static ng_disconnect_t ngfrm_disconnect;130131/* Other internal functions */132static int ngfrm_decode(node_p node, item_p item);133static int ngfrm_addrlen(char *hdr);134static int ngfrm_allocate_CTX(sc_p sc, int dlci);135136/* Netgraph type */137static struct ng_type typestruct = {138.version = NG_ABI_VERSION,139.name = NG_FRAMERELAY_NODE_TYPE,140.constructor = ngfrm_constructor,141.shutdown = ngfrm_shutdown,142.newhook = ngfrm_newhook,143.rcvdata = ngfrm_rcvdata,144.disconnect = ngfrm_disconnect,145};146NETGRAPH_INIT(framerelay, &typestruct);147148#define ERROUT(x) do { error = (x); goto done; } while (0)149150/*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*/154static int155ngfrm_allocate_CTX(sc_p sc, int dlci)156{157u_int ctxnum = -1; /* what ctx number we are using */158volatile struct ctxinfo *CTXp = NULL;159160/* Sanity check the dlci value */161if (dlci > 1023)162return (-1);163164/* Check to see if we already have an entry for this DLCI */165if (sc->ALT[dlci]) {166if ((ctxnum = sc->ALT[dlci] & CTX_VALUE) < MAX_CT) {167CTXp = sc->channel + ctxnum;168} else {169ctxnum = -1;170sc->ALT[dlci] = 0; /* paranoid but... */171}172}173174/*175* If the index has no valid entry yet, then we need to allocate a176* CTX number to it177*/178if (CTXp == NULL) {179for (ctxnum = 0; ctxnum < MAX_CT; ctxnum++) {180/*181* If the VALID flag is empty it is unused182*/183if ((sc->channel[ctxnum].flags & CHAN_VALID) == 0) {184bzero(sc->channel + ctxnum,185sizeof(struct ctxinfo));186CTXp = sc->channel + ctxnum;187sc->ALT[dlci] = ctxnum | CTX_VALID;188sc->channel[ctxnum].dlci = dlci;189sc->channel[ctxnum].flags = CHAN_VALID;190break;191}192}193}194195/*196* If we still don't have a CTX pointer, then we never found a free197* spot so give up now..198*/199if (!CTXp) {200log(LOG_ERR, "No CTX available for dlci %d\n", dlci);201return (-1);202}203return (ctxnum);204}205206/*207* Node constructor208*/209static int210ngfrm_constructor(node_p node)211{212sc_p sc;213214sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);215sc->addrlen = 2; /* default */216217/* Link the node and our private info */218NG_NODE_SET_PRIVATE(node, sc);219sc->node = node;220return (0);221}222223/*224* Add a new hook225*226* We allow hooks called "debug", "downstream" and dlci[0-1023]227* The hook's private info points to our stash of info about that228* channel. A NULL pointer is debug and a DLCI of -1 means downstream.229*/230static int231ngfrm_newhook(node_p node, hook_p hook, const char *name)232{233const sc_p sc = NG_NODE_PRIVATE(node);234const char *cp;235char *eptr;236int dlci = 0;237int ctxnum;238239/* Check if it's our friend the control hook */240if (strcmp(name, NG_FRAMERELAY_HOOK_DEBUG) == 0) {241NG_HOOK_SET_PRIVATE(hook, NULL); /* paranoid */242return (0);243}244245/*246* All other hooks either start with 'dlci' and have a decimal247* trailing channel number up to 4 digits, or are the downstream248* hook.249*/250if (strncmp(name, NG_FRAMERELAY_HOOK_DLCI,251strlen(NG_FRAMERELAY_HOOK_DLCI)) != 0) {252/* It must be the downstream connection */253if (strcmp(name, NG_FRAMERELAY_HOOK_DOWNSTREAM) != 0)254return EINVAL;255256/* Make sure we haven't already got one (paranoid) */257if (sc->downstream.hook)258return (EADDRINUSE);259260/* OK add it */261NG_HOOK_SET_PRIVATE(hook, &sc->downstream);262sc->downstream.hook = hook;263sc->downstream.dlci = -1;264sc->downstream.flags |= CHAN_ACTIVE;265sc->datahooks++;266return (0);267}268269/* Must be a dlci hook at this point */270cp = name + strlen(NG_FRAMERELAY_HOOK_DLCI);271if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))272return (EINVAL);273dlci = (int)strtoul(cp, &eptr, 10);274if (*eptr != '\0' || dlci < 0 || dlci > 1023)275return (EINVAL);276277/*278* We have a dlci, now either find it, or allocate it. It's possible279* that we might have seen packets for it already and made an entry280* for it.281*/282ctxnum = ngfrm_allocate_CTX(sc, dlci);283if (ctxnum == -1)284return (ENOBUFS);285286/*287* Be paranoid: if it's got a hook already, that dlci is in use .288* Generic code can not catch all the synonyms (e.g. dlci016 vs289* dlci16)290*/291if (sc->channel[ctxnum].hook != NULL)292return (EADDRINUSE);293294/*295* Put our hooks into it (pun not intended)296*/297sc->channel[ctxnum].flags |= CHAN_ACTIVE;298NG_HOOK_SET_PRIVATE(hook, sc->channel + ctxnum);299sc->channel[ctxnum].hook = hook;300sc->datahooks++;301return (0);302}303304/*305* Count up the size of the address header if we don't already know306*/307int308ngfrm_addrlen(char *hdr)309{310if (hdr[0] & BYTEX_EA)311return 0;312if (hdr[1] & BYTEX_EA)313return 2;314if (hdr[2] & BYTEX_EA)315return 3;316if (hdr[3] & BYTEX_EA)317return 4;318return 0;319}320321/*322* Receive data packet323*/324static int325ngfrm_rcvdata(hook_p hook, item_p item)326{327struct ctxinfo *const ctxp = NG_HOOK_PRIVATE(hook);328struct mbuf *m = NULL;329int error = 0;330int dlci;331sc_p sc;332int alen;333char *data;334335/* Data doesn't come in from just anywhere (e.g debug hook) */336if (ctxp == NULL)337ERROUT(ENETDOWN);338339/* If coming from downstream, decode it to a channel */340dlci = ctxp->dlci;341if (dlci == -1)342return (ngfrm_decode(NG_HOOK_NODE(hook), item));343344NGI_GET_M(item, m);345/* Derive the softc we will need */346sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));347348/* If there is no live channel, throw it away */349if ((sc->downstream.hook == NULL)350|| ((ctxp->flags & CHAN_ACTIVE) == 0))351ERROUT(ENETDOWN);352353/* Store the DLCI on the front of the packet */354alen = sc->addrlen;355if (alen == 0)356alen = 2; /* default value for transmit */357M_PREPEND(m, alen, M_NOWAIT);358if (m == NULL)359ERROUT(ENOBUFS);360data = mtod(m, char *);361362/*363* Shift the lowest bits into the address field until we are done.364* First byte is MSBits of addr so work backwards.365*/366switch (alen) {367case 2:368data[0] = data[1] = '\0';369SHIFTOUT(makeup + 1, data[1], dlci);370SHIFTOUT(makeup + 0, data[0], dlci);371data[1] |= BYTEX_EA;372break;373case 3:374data[0] = data[1] = data[2] = '\0';375SHIFTOUT(makeup + 3, data[2], dlci); /* 3 and 2 is correct */376SHIFTOUT(makeup + 1, data[1], dlci);377SHIFTOUT(makeup + 0, data[0], dlci);378data[2] |= BYTEX_EA;379break;380case 4:381data[0] = data[1] = data[2] = data[3] = '\0';382SHIFTOUT(makeup + 3, data[3], dlci);383SHIFTOUT(makeup + 2, data[2], dlci);384SHIFTOUT(makeup + 1, data[1], dlci);385SHIFTOUT(makeup + 0, data[0], dlci);386data[3] |= BYTEX_EA;387break;388default:389panic("%s", __func__);390}391392/* Send it */393NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);394return (error);395396done:397NG_FREE_ITEM(item);398NG_FREE_M(m);399return (error);400}401402/*403* Decode an incoming frame coming from the switch404*/405static int406ngfrm_decode(node_p node, item_p item)407{408const sc_p sc = NG_NODE_PRIVATE(node);409char *data;410int alen;411u_int dlci = 0;412int error = 0;413int ctxnum;414struct mbuf *m;415416NGI_GET_M(item, m);417if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL)418ERROUT(ENOBUFS);419data = mtod(m, char *);420if ((alen = sc->addrlen) == 0) {421sc->addrlen = alen = ngfrm_addrlen(data);422}423switch (alen) {424case 2:425SHIFTIN(makeup + 0, data[0], dlci);426SHIFTIN(makeup + 1, data[1], dlci);427break;428case 3:429SHIFTIN(makeup + 0, data[0], dlci);430SHIFTIN(makeup + 1, data[1], dlci);431SHIFTIN(makeup + 3, data[2], dlci); /* 3 and 2 is correct */432break;433case 4:434SHIFTIN(makeup + 0, data[0], dlci);435SHIFTIN(makeup + 1, data[1], dlci);436SHIFTIN(makeup + 2, data[2], dlci);437SHIFTIN(makeup + 3, data[3], dlci);438break;439default:440ERROUT(EINVAL);441}442443if (dlci > 1023)444ERROUT(EINVAL);445ctxnum = sc->ALT[dlci];446if ((ctxnum & CTX_VALID) && sc->channel[ctxnum &= CTX_VALUE].hook) {447/* Send it */448m_adj(m, alen);449NG_FWD_NEW_DATA(error, item, sc->channel[ctxnum].hook, m);450return (error);451} else {452error = ENETDOWN;453}454done:455NG_FREE_ITEM(item);456NG_FREE_M(m);457return (error);458}459460/*461* Shutdown node462*/463static int464ngfrm_shutdown(node_p node)465{466const sc_p sc = NG_NODE_PRIVATE(node);467468NG_NODE_SET_PRIVATE(node, NULL);469free(sc, M_NETGRAPH);470NG_NODE_UNREF(node);471return (0);472}473474/*475* Hook disconnection476*477* Invalidate the private data associated with this dlci.478* For this type, removal of the last link resets tries to destroy the node.479*/480static int481ngfrm_disconnect(hook_p hook)482{483const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));484struct ctxinfo *const cp = NG_HOOK_PRIVATE(hook);485int dlci;486487/* If it's a regular dlci hook, then free resources etc.. */488if (cp != NULL) {489cp->hook = NULL;490dlci = cp->dlci;491if (dlci != -1)492sc->ALT[dlci] = 0;493cp->flags = 0;494sc->datahooks--;495}496if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)497&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))498ng_rmnode_self(NG_HOOK_NODE(hook));499return (0);500}501502503