ng_frame_relay.c revision 213794
152419Sjulian/* 252419Sjulian * ng_frame_relay.c 3139823Simp */ 4139823Simp 5139823Simp/*- 652419Sjulian * Copyright (c) 1996-1999 Whistle Communications, Inc. 752419Sjulian * All rights reserved. 852419Sjulian * 952419Sjulian * Subject to the following obligations and disclaimer of warranty, use and 1052419Sjulian * redistribution of this software, in source or object code forms, with or 1152419Sjulian * without modifications are expressly permitted by Whistle Communications; 1252419Sjulian * provided, however, that: 1352419Sjulian * 1. Any and all reproductions of the source or object code must include the 1452419Sjulian * copyright notice above and the following disclaimer of warranties; and 1552419Sjulian * 2. No rights are granted, in any manner or form, to use Whistle 1652419Sjulian * Communications, Inc. trademarks, including the mark "WHISTLE 1752419Sjulian * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 1852419Sjulian * such appears in the above copyright notice or in the software. 1952419Sjulian * 2052419Sjulian * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 2152419Sjulian * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 2252419Sjulian * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 2352419Sjulian * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 2452419Sjulian * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 2552419Sjulian * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 2652419Sjulian * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 2752419Sjulian * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 2852419Sjulian * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 2952419Sjulian * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 3052419Sjulian * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 3152419Sjulian * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 3252419Sjulian * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 3352419Sjulian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3452419Sjulian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3552419Sjulian * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 3652419Sjulian * OF SUCH DAMAGE. 3752419Sjulian * 3870700Sjulian * Author: Julian Elischer <julian@freebsd.org> 3952419Sjulian * 4052419Sjulian * $FreeBSD: head/sys/netgraph/ng_frame_relay.c 213794 2010-10-13 17:21:21Z rpaulo $ 4152752Sjulian * $Whistle: ng_frame_relay.c,v 1.20 1999/11/01 09:24:51 julian Exp $ 4252419Sjulian */ 4352419Sjulian 4452419Sjulian/* 4552419Sjulian * This node implements the frame relay protocol, not including 4652419Sjulian * the LMI line management. This means basically keeping track 4752419Sjulian * of which DLCI's are active, doing frame (de)multiplexing, etc. 4852419Sjulian * 4952419Sjulian * It has a 'downstream' hook that goes to the line, and a 5052419Sjulian * hook for each DLCI (eg, 'dlci16'). 5152419Sjulian */ 5252419Sjulian 5352419Sjulian#include <sys/param.h> 5452419Sjulian#include <sys/systm.h> 5552419Sjulian#include <sys/kernel.h> 5652419Sjulian#include <sys/errno.h> 5752419Sjulian#include <sys/malloc.h> 5852419Sjulian#include <sys/mbuf.h> 5952419Sjulian#include <sys/syslog.h> 6052843Sphk#include <sys/ctype.h> 6152419Sjulian 6252419Sjulian#include <netgraph/ng_message.h> 6352419Sjulian#include <netgraph/netgraph.h> 6452419Sjulian#include <netgraph/ng_frame_relay.h> 6552419Sjulian 6652419Sjulian/* 6752419Sjulian * Line info, and status per channel. 6852419Sjulian */ 6952419Sjulianstruct ctxinfo { /* one per active hook */ 7052419Sjulian u_int flags; 7152419Sjulian#define CHAN_VALID 0x01 /* assigned to a channel */ 7252419Sjulian#define CHAN_ACTIVE 0x02 /* bottom level active */ 7352419Sjulian int dlci; /* the dlci assigned to this context */ 7452419Sjulian hook_p hook; /* if there's a hook assigned.. */ 7552419Sjulian}; 7652419Sjulian 7752419Sjulian#define MAX_CT 16 /* # of dlci's active at a time (POWER OF 2!) */ 7852419Sjulianstruct frmrel_softc { 7952419Sjulian int unit; /* which card are we? */ 8052419Sjulian int datahooks; /* number of data hooks attached */ 8152419Sjulian node_p node; /* netgraph node */ 8252419Sjulian int addrlen; /* address header length */ 8352419Sjulian int flags; /* state */ 8452419Sjulian int mtu; /* guess */ 8552419Sjulian u_char remote_seq; /* sequence number the remote sent */ 8652419Sjulian u_char local_seq; /* sequence number the remote rcvd */ 8752419Sjulian u_short ALT[1024]; /* map DLCIs to CTX */ 8852419Sjulian#define CTX_VALID 0x8000 /* this bit means it's a valid CTX */ 8952419Sjulian#define CTX_VALUE (MAX_CT - 1) /* mask for context part */ 9052419Sjulian struct ctxinfo channel[MAX_CT]; 9152419Sjulian struct ctxinfo downstream; 9252419Sjulian}; 9352419Sjuliantypedef struct frmrel_softc *sc_p; 9452419Sjulian 9552419Sjulian#define BYTEX_EA 0x01 /* End Address. Always 0 on byte1 */ 9652419Sjulian#define BYTE1_C_R 0x02 9752419Sjulian#define BYTE2_FECN 0x08 /* forwards congestion notification */ 9852419Sjulian#define BYTE2_BECN 0x04 /* Backward congestion notification */ 9952419Sjulian#define BYTE2_DE 0x02 /* Discard elligability */ 10052419Sjulian#define LASTBYTE_D_C 0x02 /* last byte is dl_core or dlci info */ 10152419Sjulian 10252419Sjulian/* Used to do headers */ 103132010Srwatsonconst static struct segment { 10452419Sjulian u_char mask; 10552419Sjulian u_char shift; 10652419Sjulian u_char width; 10752419Sjulian} makeup[] = { 10852419Sjulian { 0xfc, 2, 6 }, 10952419Sjulian { 0xf0, 4, 4 }, 11052419Sjulian { 0xfe, 1, 7 }, 11152419Sjulian { 0xfc, 2, 6 } 11252419Sjulian}; 11352419Sjulian 11452419Sjulian#define SHIFTIN(segment, byte, dlci) \ 11552419Sjulian { \ 11652419Sjulian (dlci) <<= (segment)->width; \ 11752419Sjulian (dlci) |= \ 11852419Sjulian (((byte) & (segment)->mask) >> (segment)->shift); \ 11952419Sjulian } 12052419Sjulian 12152419Sjulian#define SHIFTOUT(segment, byte, dlci) \ 12252419Sjulian { \ 12352419Sjulian (byte) |= (((dlci) << (segment)->shift) & (segment)->mask); \ 12452419Sjulian (dlci) >>= (segment)->width; \ 12552419Sjulian } 12652419Sjulian 12752419Sjulian/* Netgraph methods */ 12852752Sjulianstatic ng_constructor_t ngfrm_constructor; 12970700Sjulianstatic ng_shutdown_t ngfrm_shutdown; 13052752Sjulianstatic ng_newhook_t ngfrm_newhook; 13152752Sjulianstatic ng_rcvdata_t ngfrm_rcvdata; 13252752Sjulianstatic ng_disconnect_t ngfrm_disconnect; 13352419Sjulian 13452419Sjulian/* Other internal functions */ 13570700Sjulianstatic int ngfrm_decode(node_p node, item_p item); 13652419Sjulianstatic int ngfrm_addrlen(char *hdr); 13752419Sjulianstatic int ngfrm_allocate_CTX(sc_p sc, int dlci); 13852419Sjulian 13952419Sjulian/* Netgraph type */ 14052419Sjulianstatic struct ng_type typestruct = { 141129823Sjulian .version = NG_ABI_VERSION, 142129823Sjulian .name = NG_FRAMERELAY_NODE_TYPE, 143129823Sjulian .constructor = ngfrm_constructor, 144129823Sjulian .shutdown = ngfrm_shutdown, 145129823Sjulian .newhook = ngfrm_newhook, 146129823Sjulian .rcvdata = ngfrm_rcvdata, 147129823Sjulian .disconnect = ngfrm_disconnect, 14852419Sjulian}; 14952419SjulianNETGRAPH_INIT(framerelay, &typestruct); 15052419Sjulian 15152419Sjulian/* 15252419Sjulian * Given a DLCI, return the index of the context table entry for it, 15352419Sjulian * Allocating a new one if needs be, or -1 if none available. 15452419Sjulian */ 15552419Sjulianstatic int 15652419Sjulianngfrm_allocate_CTX(sc_p sc, int dlci) 15752419Sjulian{ 15852419Sjulian u_int ctxnum = -1; /* what ctx number we are using */ 15952419Sjulian volatile struct ctxinfo *CTXp = NULL; 16052419Sjulian 16152419Sjulian /* Sanity check the dlci value */ 16252419Sjulian if (dlci > 1023) 16352419Sjulian return (-1); 16452419Sjulian 16552419Sjulian /* Check to see if we already have an entry for this DLCI */ 16652419Sjulian if (sc->ALT[dlci]) { 16752419Sjulian if ((ctxnum = sc->ALT[dlci] & CTX_VALUE) < MAX_CT) { 16852419Sjulian CTXp = sc->channel + ctxnum; 16952419Sjulian } else { 17052419Sjulian ctxnum = -1; 17152419Sjulian sc->ALT[dlci] = 0; /* paranoid but... */ 17252419Sjulian } 17352419Sjulian } 17452419Sjulian 17552419Sjulian /* 17652419Sjulian * If the index has no valid entry yet, then we need to allocate a 17752419Sjulian * CTX number to it 17852419Sjulian */ 17952419Sjulian if (CTXp == NULL) { 18052419Sjulian for (ctxnum = 0; ctxnum < MAX_CT; ctxnum++) { 18152419Sjulian /* 18252419Sjulian * If the VALID flag is empty it is unused 18352419Sjulian */ 18452419Sjulian if ((sc->channel[ctxnum].flags & CHAN_VALID) == 0) { 18552419Sjulian bzero(sc->channel + ctxnum, 18652419Sjulian sizeof(struct ctxinfo)); 18752419Sjulian CTXp = sc->channel + ctxnum; 18852419Sjulian sc->ALT[dlci] = ctxnum | CTX_VALID; 18952419Sjulian sc->channel[ctxnum].dlci = dlci; 19052419Sjulian sc->channel[ctxnum].flags = CHAN_VALID; 19152419Sjulian break; 19252419Sjulian } 19352419Sjulian } 19452419Sjulian } 19552419Sjulian 19652419Sjulian /* 19752419Sjulian * If we still don't have a CTX pointer, then we never found a free 19852419Sjulian * spot so give up now.. 19952419Sjulian */ 20052419Sjulian if (!CTXp) { 20152419Sjulian log(LOG_ERR, "No CTX available for dlci %d\n", dlci); 20252419Sjulian return (-1); 20352419Sjulian } 20452419Sjulian return (ctxnum); 20552419Sjulian} 20652419Sjulian 20752419Sjulian/* 20852419Sjulian * Node constructor 20952419Sjulian */ 21052419Sjulianstatic int 21170700Sjulianngfrm_constructor(node_p node) 21252419Sjulian{ 21352419Sjulian sc_p sc; 21452419Sjulian 215184205Sdes sc = malloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO); 21652419Sjulian if (!sc) 21752419Sjulian return (ENOMEM); 21852419Sjulian sc->addrlen = 2; /* default */ 21952419Sjulian 22052419Sjulian /* Link the node and our private info */ 22170784Sjulian NG_NODE_SET_PRIVATE(node, sc); 22270700Sjulian sc->node = node; 22352419Sjulian return (0); 22452419Sjulian} 22552419Sjulian 22652419Sjulian/* 22752419Sjulian * Add a new hook 22852419Sjulian * 22952419Sjulian * We allow hooks called "debug", "downstream" and dlci[0-1023] 23052419Sjulian * The hook's private info points to our stash of info about that 23152419Sjulian * channel. A NULL pointer is debug and a DLCI of -1 means downstream. 23252419Sjulian */ 23352419Sjulianstatic int 23452419Sjulianngfrm_newhook(node_p node, hook_p hook, const char *name) 23552419Sjulian{ 23670784Sjulian const sc_p sc = NG_NODE_PRIVATE(node); 23753648Sarchie const char *cp; 23853648Sarchie char *eptr; 23953648Sarchie int dlci = 0; 24053648Sarchie int ctxnum; 24152419Sjulian 24252419Sjulian /* Check if it's our friend the control hook */ 24352419Sjulian if (strcmp(name, NG_FRAMERELAY_HOOK_DEBUG) == 0) { 24470784Sjulian NG_HOOK_SET_PRIVATE(hook, NULL); /* paranoid */ 24552419Sjulian return (0); 24652419Sjulian } 24752419Sjulian 24852419Sjulian /* 24952419Sjulian * All other hooks either start with 'dlci' and have a decimal 25052419Sjulian * trailing channel number up to 4 digits, or are the downstream 25152419Sjulian * hook. 25252419Sjulian */ 25352419Sjulian if (strncmp(name, NG_FRAMERELAY_HOOK_DLCI, 25452419Sjulian strlen(NG_FRAMERELAY_HOOK_DLCI)) != 0) { 25552419Sjulian 25652419Sjulian /* It must be the downstream connection */ 25752419Sjulian if (strcmp(name, NG_FRAMERELAY_HOOK_DOWNSTREAM) != 0) 25852419Sjulian return EINVAL; 25952419Sjulian 26052419Sjulian /* Make sure we haven't already got one (paranoid) */ 26152419Sjulian if (sc->downstream.hook) 26252419Sjulian return (EADDRINUSE); 26352419Sjulian 26452419Sjulian /* OK add it */ 26570784Sjulian NG_HOOK_SET_PRIVATE(hook, &sc->downstream); 26652419Sjulian sc->downstream.hook = hook; 26752419Sjulian sc->downstream.dlci = -1; 26852419Sjulian sc->downstream.flags |= CHAN_ACTIVE; 26952419Sjulian sc->datahooks++; 27052419Sjulian return (0); 27152419Sjulian } 27252419Sjulian 27352419Sjulian /* Must be a dlci hook at this point */ 27452419Sjulian cp = name + strlen(NG_FRAMERELAY_HOOK_DLCI); 27552816Sarchie if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 27652419Sjulian return (EINVAL); 27752816Sarchie dlci = (int)strtoul(cp, &eptr, 10); 27852816Sarchie if (*eptr != '\0' || dlci < 0 || dlci > 1023) 27952816Sarchie return (EINVAL); 28052816Sarchie 28152419Sjulian /* 28252419Sjulian * We have a dlci, now either find it, or allocate it. It's possible 28352419Sjulian * that we might have seen packets for it already and made an entry 28452419Sjulian * for it. 28552419Sjulian */ 28652419Sjulian ctxnum = ngfrm_allocate_CTX(sc, dlci); 28752419Sjulian if (ctxnum == -1) 28852419Sjulian return (ENOBUFS); 28952419Sjulian 29052419Sjulian /* 29152419Sjulian * Be paranoid: if it's got a hook already, that dlci is in use . 29252419Sjulian * Generic code can not catch all the synonyms (e.g. dlci016 vs 29352419Sjulian * dlci16) 29452419Sjulian */ 29552419Sjulian if (sc->channel[ctxnum].hook != NULL) 29652419Sjulian return (EADDRINUSE); 29752419Sjulian 29852419Sjulian /* 29952419Sjulian * Put our hooks into it (pun not intended) 30052419Sjulian */ 30152419Sjulian sc->channel[ctxnum].flags |= CHAN_ACTIVE; 30270784Sjulian NG_HOOK_SET_PRIVATE(hook, sc->channel + ctxnum); 30352419Sjulian sc->channel[ctxnum].hook = hook; 30452419Sjulian sc->datahooks++; 30552419Sjulian return (0); 30652419Sjulian} 30752419Sjulian 30852419Sjulian/* 30952419Sjulian * Count up the size of the address header if we don't already know 31052419Sjulian */ 31152419Sjulianint 31252419Sjulianngfrm_addrlen(char *hdr) 31352419Sjulian{ 31452419Sjulian if (hdr[0] & BYTEX_EA) 31552419Sjulian return 0; 31652419Sjulian if (hdr[1] & BYTEX_EA) 31752419Sjulian return 2; 31852419Sjulian if (hdr[2] & BYTEX_EA) 31952419Sjulian return 3; 32052419Sjulian if (hdr[3] & BYTEX_EA) 32152419Sjulian return 4; 32252419Sjulian return 0; 32352419Sjulian} 32452419Sjulian 32552419Sjulian/* 32652419Sjulian * Receive data packet 32752419Sjulian */ 32852419Sjulianstatic int 32970700Sjulianngfrm_rcvdata(hook_p hook, item_p item) 33052419Sjulian{ 33170784Sjulian struct ctxinfo *const ctxp = NG_HOOK_PRIVATE(hook); 332154376Sglebius struct mbuf *m = NULL; 33352419Sjulian int error = 0; 33452419Sjulian int dlci; 33552419Sjulian sc_p sc; 33652419Sjulian int alen; 33752419Sjulian char *data; 33852419Sjulian 33952419Sjulian /* Data doesn't come in from just anywhere (e.g debug hook) */ 34052419Sjulian if (ctxp == NULL) { 34152419Sjulian error = ENETDOWN; 34252419Sjulian goto bad; 34352419Sjulian } 34452419Sjulian 34552419Sjulian /* If coming from downstream, decode it to a channel */ 34652419Sjulian dlci = ctxp->dlci; 34752419Sjulian if (dlci == -1) 34870784Sjulian return (ngfrm_decode(NG_HOOK_NODE(hook), item)); 34952419Sjulian 35070700Sjulian NGI_GET_M(item, m); 35152419Sjulian /* Derive the softc we will need */ 35270784Sjulian sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 35352419Sjulian 35452419Sjulian /* If there is no live channel, throw it away */ 35552419Sjulian if ((sc->downstream.hook == NULL) 35652419Sjulian || ((ctxp->flags & CHAN_ACTIVE) == 0)) { 35752419Sjulian error = ENETDOWN; 35852419Sjulian goto bad; 35952419Sjulian } 36052419Sjulian 36152419Sjulian /* Store the DLCI on the front of the packet */ 36252419Sjulian alen = sc->addrlen; 36352419Sjulian if (alen == 0) 36452419Sjulian alen = 2; /* default value for transmit */ 365111119Simp M_PREPEND(m, alen, M_DONTWAIT); 36652419Sjulian if (m == NULL) { 36752419Sjulian error = ENOBUFS; 36852419Sjulian goto bad; 36952419Sjulian } 37052419Sjulian data = mtod(m, char *); 37152419Sjulian 37252419Sjulian /* 37352419Sjulian * Shift the lowest bits into the address field untill we are done. 37452419Sjulian * First byte is MSBits of addr so work backwards. 37552419Sjulian */ 37652419Sjulian switch (alen) { 37752419Sjulian case 2: 37852419Sjulian data[0] = data[1] = '\0'; 37952419Sjulian SHIFTOUT(makeup + 1, data[1], dlci); 38052419Sjulian SHIFTOUT(makeup + 0, data[0], dlci); 38152419Sjulian data[1] |= BYTEX_EA; 38252419Sjulian break; 38352419Sjulian case 3: 38452419Sjulian data[0] = data[1] = data[2] = '\0'; 38552419Sjulian SHIFTOUT(makeup + 3, data[2], dlci); /* 3 and 2 is correct */ 38652419Sjulian SHIFTOUT(makeup + 1, data[1], dlci); 38752419Sjulian SHIFTOUT(makeup + 0, data[0], dlci); 38852419Sjulian data[2] |= BYTEX_EA; 38952419Sjulian break; 39052419Sjulian case 4: 39152419Sjulian data[0] = data[1] = data[2] = data[3] = '\0'; 39252419Sjulian SHIFTOUT(makeup + 3, data[3], dlci); 39352419Sjulian SHIFTOUT(makeup + 2, data[2], dlci); 39452419Sjulian SHIFTOUT(makeup + 1, data[1], dlci); 39552419Sjulian SHIFTOUT(makeup + 0, data[0], dlci); 39652419Sjulian data[3] |= BYTEX_EA; 39752419Sjulian break; 39852419Sjulian default: 399213794Srpaulo panic("%s", __func__); 40052419Sjulian } 40152419Sjulian 40252419Sjulian /* Send it */ 40370700Sjulian NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m); 40452419Sjulian return (error); 40552419Sjulian 40652419Sjulianbad: 40770700Sjulian NG_FREE_ITEM(item); 40870700Sjulian NG_FREE_M(m); 40952419Sjulian return (error); 41052419Sjulian} 41152419Sjulian 41252419Sjulian/* 41352419Sjulian * Decode an incoming frame coming from the switch 41452419Sjulian */ 41552419Sjulianstatic int 41670700Sjulianngfrm_decode(node_p node, item_p item) 41752419Sjulian{ 41870784Sjulian const sc_p sc = NG_NODE_PRIVATE(node); 41952419Sjulian char *data; 42052419Sjulian int alen; 42152419Sjulian u_int dlci = 0; 42252419Sjulian int error = 0; 42352419Sjulian int ctxnum; 42470700Sjulian struct mbuf *m; 42552419Sjulian 42670700Sjulian NGI_GET_M(item, m); 42752539Sjulian if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) { 42852419Sjulian error = ENOBUFS; 42952419Sjulian goto out; 43052419Sjulian } 43152419Sjulian data = mtod(m, char *); 43252419Sjulian if ((alen = sc->addrlen) == 0) { 43352419Sjulian sc->addrlen = alen = ngfrm_addrlen(data); 43452419Sjulian } 43552419Sjulian switch (alen) { 43652419Sjulian case 2: 43752419Sjulian SHIFTIN(makeup + 0, data[0], dlci); 43852419Sjulian SHIFTIN(makeup + 1, data[1], dlci); 43952419Sjulian break; 44052419Sjulian case 3: 44152419Sjulian SHIFTIN(makeup + 0, data[0], dlci); 44252419Sjulian SHIFTIN(makeup + 1, data[1], dlci); 44352419Sjulian SHIFTIN(makeup + 3, data[2], dlci); /* 3 and 2 is correct */ 44452419Sjulian break; 44552419Sjulian case 4: 44652419Sjulian SHIFTIN(makeup + 0, data[0], dlci); 44752419Sjulian SHIFTIN(makeup + 1, data[1], dlci); 44852419Sjulian SHIFTIN(makeup + 2, data[2], dlci); 44952419Sjulian SHIFTIN(makeup + 3, data[3], dlci); 45052419Sjulian break; 45152419Sjulian default: 45252419Sjulian error = EINVAL; 45352419Sjulian goto out; 45452419Sjulian } 45552419Sjulian 45652419Sjulian if (dlci > 1023) { 45752419Sjulian error = EINVAL; 45852419Sjulian goto out; 45952419Sjulian } 46052419Sjulian ctxnum = sc->ALT[dlci]; 46152419Sjulian if ((ctxnum & CTX_VALID) && sc->channel[ctxnum &= CTX_VALUE].hook) { 46252419Sjulian /* Send it */ 46352419Sjulian m_adj(m, alen); 46470700Sjulian NG_FWD_NEW_DATA(error, item, sc->channel[ctxnum].hook, m); 46552419Sjulian return (error); 46652419Sjulian } else { 46752419Sjulian error = ENETDOWN; 46852419Sjulian } 46952419Sjulianout: 47070700Sjulian NG_FREE_ITEM(item); 47170700Sjulian NG_FREE_M(m); 47252419Sjulian return (error); 47352419Sjulian} 47452419Sjulian 47552419Sjulian/* 47652419Sjulian * Shutdown node 47752419Sjulian */ 47852419Sjulianstatic int 47970700Sjulianngfrm_shutdown(node_p node) 48052419Sjulian{ 48170784Sjulian const sc_p sc = NG_NODE_PRIVATE(node); 48252419Sjulian 48370784Sjulian NG_NODE_SET_PRIVATE(node, NULL); 484184205Sdes free(sc, M_NETGRAPH); 48570784Sjulian NG_NODE_UNREF(node); 48652419Sjulian return (0); 48752419Sjulian} 48852419Sjulian 48952419Sjulian/* 49052419Sjulian * Hook disconnection 49152419Sjulian * 49252419Sjulian * Invalidate the private data associated with this dlci. 49352419Sjulian * For this type, removal of the last link resets tries to destroy the node. 49452419Sjulian */ 49552419Sjulianstatic int 49652419Sjulianngfrm_disconnect(hook_p hook) 49752419Sjulian{ 49870784Sjulian const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 49970784Sjulian struct ctxinfo *const cp = NG_HOOK_PRIVATE(hook); 50052419Sjulian int dlci; 50152419Sjulian 50252419Sjulian /* If it's a regular dlci hook, then free resources etc.. */ 50352419Sjulian if (cp != NULL) { 50452419Sjulian cp->hook = NULL; 50552419Sjulian dlci = cp->dlci; 50652419Sjulian if (dlci != -1) 50752419Sjulian sc->ALT[dlci] = 0; 50852419Sjulian cp->flags = 0; 50952419Sjulian sc->datahooks--; 51052419Sjulian } 51170784Sjulian if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 51270784Sjulian && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) 51370784Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); 51452419Sjulian return (0); 51552419Sjulian} 516