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$
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
215220768Sglebius	sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
21652419Sjulian	sc->addrlen = 2;	/* default */
21752419Sjulian
21852419Sjulian	/* Link the node and our private info */
21970784Sjulian	NG_NODE_SET_PRIVATE(node, sc);
22070700Sjulian	sc->node = node;
22152419Sjulian	return (0);
22252419Sjulian}
22352419Sjulian
22452419Sjulian/*
22552419Sjulian * Add a new hook
22652419Sjulian *
22752419Sjulian * We allow hooks called "debug", "downstream" and dlci[0-1023]
22852419Sjulian * The hook's private info points to our stash of info about that
22952419Sjulian * channel. A NULL pointer is debug and a DLCI of -1 means downstream.
23052419Sjulian */
23152419Sjulianstatic int
23252419Sjulianngfrm_newhook(node_p node, hook_p hook, const char *name)
23352419Sjulian{
23470784Sjulian	const sc_p sc = NG_NODE_PRIVATE(node);
23553648Sarchie	const char *cp;
23653648Sarchie	char *eptr;
23753648Sarchie	int dlci = 0;
23853648Sarchie	int ctxnum;
23952419Sjulian
24052419Sjulian	/* Check if it's our friend the control hook */
24152419Sjulian	if (strcmp(name, NG_FRAMERELAY_HOOK_DEBUG) == 0) {
24270784Sjulian		NG_HOOK_SET_PRIVATE(hook, NULL);	/* paranoid */
24352419Sjulian		return (0);
24452419Sjulian	}
24552419Sjulian
24652419Sjulian	/*
24752419Sjulian	 * All other hooks either start with 'dlci' and have a decimal
24852419Sjulian	 * trailing channel number up to 4 digits, or are the downstream
24952419Sjulian	 * hook.
25052419Sjulian	 */
25152419Sjulian	if (strncmp(name, NG_FRAMERELAY_HOOK_DLCI,
25252419Sjulian	    strlen(NG_FRAMERELAY_HOOK_DLCI)) != 0) {
25352419Sjulian
25452419Sjulian		/* It must be the downstream connection */
25552419Sjulian		if (strcmp(name, NG_FRAMERELAY_HOOK_DOWNSTREAM) != 0)
25652419Sjulian			return EINVAL;
25752419Sjulian
25852419Sjulian		/* Make sure we haven't already got one (paranoid) */
25952419Sjulian		if (sc->downstream.hook)
26052419Sjulian			return (EADDRINUSE);
26152419Sjulian
26252419Sjulian		/* OK add it */
26370784Sjulian		NG_HOOK_SET_PRIVATE(hook, &sc->downstream);
26452419Sjulian		sc->downstream.hook = hook;
26552419Sjulian		sc->downstream.dlci = -1;
26652419Sjulian		sc->downstream.flags |= CHAN_ACTIVE;
26752419Sjulian		sc->datahooks++;
26852419Sjulian		return (0);
26952419Sjulian	}
27052419Sjulian
27152419Sjulian	/* Must be a dlci hook at this point */
27252419Sjulian	cp = name + strlen(NG_FRAMERELAY_HOOK_DLCI);
27352816Sarchie	if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
27452419Sjulian		return (EINVAL);
27552816Sarchie	dlci = (int)strtoul(cp, &eptr, 10);
27652816Sarchie	if (*eptr != '\0' || dlci < 0 || dlci > 1023)
27752816Sarchie		return (EINVAL);
27852816Sarchie
27952419Sjulian	/*
28052419Sjulian	 * We have a dlci, now either find it, or allocate it. It's possible
28152419Sjulian	 * that we might have seen packets for it already and made an entry
28252419Sjulian	 * for it.
28352419Sjulian	 */
28452419Sjulian	ctxnum = ngfrm_allocate_CTX(sc, dlci);
28552419Sjulian	if (ctxnum == -1)
28652419Sjulian		return (ENOBUFS);
28752419Sjulian
28852419Sjulian	/*
28952419Sjulian	 * Be paranoid: if it's got a hook already, that dlci is in use .
29052419Sjulian	 * Generic code can not catch all the synonyms (e.g. dlci016 vs
29152419Sjulian	 * dlci16)
29252419Sjulian	 */
29352419Sjulian	if (sc->channel[ctxnum].hook != NULL)
29452419Sjulian		return (EADDRINUSE);
29552419Sjulian
29652419Sjulian	/*
29752419Sjulian	 * Put our hooks into it (pun not intended)
29852419Sjulian	 */
29952419Sjulian	sc->channel[ctxnum].flags |= CHAN_ACTIVE;
30070784Sjulian	NG_HOOK_SET_PRIVATE(hook, sc->channel + ctxnum);
30152419Sjulian	sc->channel[ctxnum].hook = hook;
30252419Sjulian	sc->datahooks++;
30352419Sjulian	return (0);
30452419Sjulian}
30552419Sjulian
30652419Sjulian/*
30752419Sjulian * Count up the size of the address header if we don't already know
30852419Sjulian */
30952419Sjulianint
31052419Sjulianngfrm_addrlen(char *hdr)
31152419Sjulian{
31252419Sjulian	if (hdr[0] & BYTEX_EA)
31352419Sjulian		return 0;
31452419Sjulian	if (hdr[1] & BYTEX_EA)
31552419Sjulian		return 2;
31652419Sjulian	if (hdr[2] & BYTEX_EA)
31752419Sjulian		return 3;
31852419Sjulian	if (hdr[3] & BYTEX_EA)
31952419Sjulian		return 4;
32052419Sjulian	return 0;
32152419Sjulian}
32252419Sjulian
32352419Sjulian/*
32452419Sjulian * Receive data packet
32552419Sjulian */
32652419Sjulianstatic int
32770700Sjulianngfrm_rcvdata(hook_p hook, item_p item)
32852419Sjulian{
32970784Sjulian	struct	ctxinfo *const ctxp = NG_HOOK_PRIVATE(hook);
330154376Sglebius	struct	mbuf *m = NULL;
33152419Sjulian	int     error = 0;
33252419Sjulian	int     dlci;
33352419Sjulian	sc_p    sc;
33452419Sjulian	int     alen;
33552419Sjulian	char   *data;
33652419Sjulian
33752419Sjulian	/* Data doesn't come in from just anywhere (e.g debug hook) */
33852419Sjulian	if (ctxp == NULL) {
33952419Sjulian		error = ENETDOWN;
34052419Sjulian		goto bad;
34152419Sjulian	}
34252419Sjulian
34352419Sjulian	/* If coming from downstream, decode it to a channel */
34452419Sjulian	dlci = ctxp->dlci;
34552419Sjulian	if (dlci == -1)
34670784Sjulian		return (ngfrm_decode(NG_HOOK_NODE(hook), item));
34752419Sjulian
34870700Sjulian	NGI_GET_M(item, m);
34952419Sjulian	/* Derive the softc we will need */
35070784Sjulian	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
35152419Sjulian
35252419Sjulian	/* If there is no live channel, throw it away */
35352419Sjulian	if ((sc->downstream.hook == NULL)
35452419Sjulian	    || ((ctxp->flags & CHAN_ACTIVE) == 0)) {
35552419Sjulian		error = ENETDOWN;
35652419Sjulian		goto bad;
35752419Sjulian	}
35852419Sjulian
35952419Sjulian	/* Store the DLCI on the front of the packet */
36052419Sjulian	alen = sc->addrlen;
36152419Sjulian	if (alen == 0)
36252419Sjulian		alen = 2;	/* default value for transmit */
363243882Sglebius	M_PREPEND(m, alen, M_NOWAIT);
36452419Sjulian	if (m == NULL) {
36552419Sjulian		error = ENOBUFS;
36652419Sjulian		goto bad;
36752419Sjulian	}
36852419Sjulian	data = mtod(m, char *);
36952419Sjulian
37052419Sjulian	/*
37152419Sjulian	 * Shift the lowest bits into the address field untill we are done.
37252419Sjulian	 * First byte is MSBits of addr so work backwards.
37352419Sjulian	 */
37452419Sjulian	switch (alen) {
37552419Sjulian	case 2:
37652419Sjulian		data[0] = data[1] = '\0';
37752419Sjulian		SHIFTOUT(makeup + 1, data[1], dlci);
37852419Sjulian		SHIFTOUT(makeup + 0, data[0], dlci);
37952419Sjulian		data[1] |= BYTEX_EA;
38052419Sjulian		break;
38152419Sjulian	case 3:
38252419Sjulian		data[0] = data[1] = data[2] = '\0';
38352419Sjulian		SHIFTOUT(makeup + 3, data[2], dlci);	/* 3 and 2 is correct */
38452419Sjulian		SHIFTOUT(makeup + 1, data[1], dlci);
38552419Sjulian		SHIFTOUT(makeup + 0, data[0], dlci);
38652419Sjulian		data[2] |= BYTEX_EA;
38752419Sjulian		break;
38852419Sjulian	case 4:
38952419Sjulian		data[0] = data[1] = data[2] = data[3] = '\0';
39052419Sjulian		SHIFTOUT(makeup + 3, data[3], dlci);
39152419Sjulian		SHIFTOUT(makeup + 2, data[2], dlci);
39252419Sjulian		SHIFTOUT(makeup + 1, data[1], dlci);
39352419Sjulian		SHIFTOUT(makeup + 0, data[0], dlci);
39452419Sjulian		data[3] |= BYTEX_EA;
39552419Sjulian		break;
39652419Sjulian	default:
397213794Srpaulo		panic("%s", __func__);
39852419Sjulian	}
39952419Sjulian
40052419Sjulian	/* Send it */
40170700Sjulian	NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);
40252419Sjulian	return (error);
40352419Sjulian
40452419Sjulianbad:
40570700Sjulian	NG_FREE_ITEM(item);
40670700Sjulian	NG_FREE_M(m);
40752419Sjulian	return (error);
40852419Sjulian}
40952419Sjulian
41052419Sjulian/*
41152419Sjulian * Decode an incoming frame coming from the switch
41252419Sjulian */
41352419Sjulianstatic int
41470700Sjulianngfrm_decode(node_p node, item_p item)
41552419Sjulian{
41670784Sjulian	const sc_p  sc = NG_NODE_PRIVATE(node);
41752419Sjulian	char       *data;
41852419Sjulian	int         alen;
41952419Sjulian	u_int	    dlci = 0;
42052419Sjulian	int	    error = 0;
42152419Sjulian	int	    ctxnum;
42270700Sjulian	struct mbuf *m;
42352419Sjulian
42470700Sjulian	NGI_GET_M(item, m);
42552539Sjulian	if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
42652419Sjulian		error = ENOBUFS;
42752419Sjulian		goto out;
42852419Sjulian	}
42952419Sjulian	data = mtod(m, char *);
43052419Sjulian	if ((alen = sc->addrlen) == 0) {
43152419Sjulian		sc->addrlen = alen = ngfrm_addrlen(data);
43252419Sjulian	}
43352419Sjulian	switch (alen) {
43452419Sjulian	case 2:
43552419Sjulian		SHIFTIN(makeup + 0, data[0], dlci);
43652419Sjulian		SHIFTIN(makeup + 1, data[1], dlci);
43752419Sjulian		break;
43852419Sjulian	case 3:
43952419Sjulian		SHIFTIN(makeup + 0, data[0], dlci);
44052419Sjulian		SHIFTIN(makeup + 1, data[1], dlci);
44152419Sjulian		SHIFTIN(makeup + 3, data[2], dlci);	/* 3 and 2 is correct */
44252419Sjulian		break;
44352419Sjulian	case 4:
44452419Sjulian		SHIFTIN(makeup + 0, data[0], dlci);
44552419Sjulian		SHIFTIN(makeup + 1, data[1], dlci);
44652419Sjulian		SHIFTIN(makeup + 2, data[2], dlci);
44752419Sjulian		SHIFTIN(makeup + 3, data[3], dlci);
44852419Sjulian		break;
44952419Sjulian	default:
45052419Sjulian		error = EINVAL;
45152419Sjulian		goto out;
45252419Sjulian	}
45352419Sjulian
45452419Sjulian	if (dlci > 1023) {
45552419Sjulian		error = EINVAL;
45652419Sjulian		goto out;
45752419Sjulian	}
45852419Sjulian	ctxnum = sc->ALT[dlci];
45952419Sjulian	if ((ctxnum & CTX_VALID) && sc->channel[ctxnum &= CTX_VALUE].hook) {
46052419Sjulian		/* Send it */
46152419Sjulian		m_adj(m, alen);
46270700Sjulian		NG_FWD_NEW_DATA(error, item, sc->channel[ctxnum].hook, m);
46352419Sjulian		return (error);
46452419Sjulian	} else {
46552419Sjulian		error = ENETDOWN;
46652419Sjulian	}
46752419Sjulianout:
46870700Sjulian	NG_FREE_ITEM(item);
46970700Sjulian	NG_FREE_M(m);
47052419Sjulian	return (error);
47152419Sjulian}
47252419Sjulian
47352419Sjulian/*
47452419Sjulian * Shutdown node
47552419Sjulian */
47652419Sjulianstatic int
47770700Sjulianngfrm_shutdown(node_p node)
47852419Sjulian{
47970784Sjulian	const sc_p sc = NG_NODE_PRIVATE(node);
48052419Sjulian
48170784Sjulian	NG_NODE_SET_PRIVATE(node, NULL);
482184205Sdes	free(sc, M_NETGRAPH);
48370784Sjulian	NG_NODE_UNREF(node);
48452419Sjulian	return (0);
48552419Sjulian}
48652419Sjulian
48752419Sjulian/*
48852419Sjulian * Hook disconnection
48952419Sjulian *
49052419Sjulian * Invalidate the private data associated with this dlci.
49152419Sjulian * For this type, removal of the last link resets tries to destroy the node.
49252419Sjulian */
49352419Sjulianstatic int
49452419Sjulianngfrm_disconnect(hook_p hook)
49552419Sjulian{
49670784Sjulian	const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
49770784Sjulian	struct ctxinfo *const cp = NG_HOOK_PRIVATE(hook);
49852419Sjulian	int dlci;
49952419Sjulian
50052419Sjulian	/* If it's a regular dlci hook, then free resources etc.. */
50152419Sjulian	if (cp != NULL) {
50252419Sjulian		cp->hook = NULL;
50352419Sjulian		dlci = cp->dlci;
50452419Sjulian		if (dlci != -1)
50552419Sjulian			sc->ALT[dlci] = 0;
50652419Sjulian		cp->flags = 0;
50752419Sjulian		sc->datahooks--;
50852419Sjulian	}
50970784Sjulian	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
51070784Sjulian	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
51170784Sjulian		ng_rmnode_self(NG_HOOK_NODE(hook));
51252419Sjulian	return (0);
51352419Sjulian}
514