5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
37 * Author: Julian Elisher <julian@freebsd.org>
39 * $FreeBSD: src/sys/netgraph/ng_frame_relay.c,v 1.9.2.2 2000/10/24 18:36:45 julian Exp $
40 * $Whistle: ng_frame_relay.c,v 1.20 1999/11/01 09:24:51 julian Exp $
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.
48 * It has a 'downstream' hook that goes to the line, and a
49 * hook for each DLCI (eg, 'dlci16').
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>
58 #include <sys/syslog.h>
59 #include <sys/ctype.h>
60 #include <machine/clock.h>
62 #include <netgraph/ng_message.h>
63 #include <netgraph/netgraph.h>
64 #include "ng_frame_relay.h"
67 * Line info, and status per channel.
69 struct ctxinfo { /* one per active hook */
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.. */
77 #define MAX_CT 16 /* # of dlci's active at a time (POWER OF 2!) */
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 */
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;
93 typedef struct frmrel_softc *sc_p;
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 */
102 /* Used to do headers */
103 static struct segment {
114 #define SHIFTIN(segment, byte, dlci) \
116 (dlci) <<= (segment)->width; \
118 (((byte) & (segment)->mask) >> (segment)->shift); \
121 #define SHIFTOUT(segment, byte, dlci) \
123 (byte) |= (((dlci) << (segment)->shift) & (segment)->mask); \
124 (dlci) >>= (segment)->width; \
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;
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);
140 static struct ng_type typestruct = {
142 NG_FRAMERELAY_NODE_TYPE,
155 NETGRAPH_INIT(framerelay, &typestruct);
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.
162 ngfrm_allocate_CTX(sc_p sc, int dlci)
164 u_int ctxnum = -1; /* what ctx number we are using */
165 volatile struct ctxinfo *CTXp = NULL;
167 /* Sanity check the dlci value */
171 /* Check to see if we already have an entry for this DLCI */
173 if ((ctxnum = sc->ALT[dlci] & CTX_VALUE) < MAX_CT) {
174 CTXp = sc->channel + ctxnum;
177 sc->ALT[dlci] = 0; /* paranoid but... */
182 * If the index has no valid entry yet, then we need to allocate a
186 for (ctxnum = 0; ctxnum < MAX_CT; ctxnum++) {
188 * If the VALID flag is empty it is unused
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;
203 * If we still don't have a CTX pointer, then we never found a free
204 * spot so give up now..
207 log(LOG_ERR, "No CTX available for dlci %d\n", dlci);
217 ngfrm_constructor(node_p *nodep)
222 sc = kmalloc(sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
225 if ((error = ng_make_node_common(&typestruct, nodep))) {
226 kfree(sc, M_NETGRAPH);
229 sc->addrlen = 2; /* default */
231 /* Link the node and our private info */
232 (*nodep)->private = sc;
240 * We allow hooks called "debug", "downstream" and dlci[0-1023]
241 * The hook's private info points to our stash of info about that
242 * channel. A NULL pointer is debug and a DLCI of -1 means downstream.
245 ngfrm_newhook(node_p node, hook_p hook, const char *name)
247 const sc_p sc = node->private;
253 /* Check if it's our friend the control hook */
254 if (strcmp(name, NG_FRAMERELAY_HOOK_DEBUG) == 0) {
255 hook->private = NULL; /* paranoid */
260 * All other hooks either start with 'dlci' and have a decimal
261 * trailing channel number up to 4 digits, or are the downstream
264 if (strncmp(name, NG_FRAMERELAY_HOOK_DLCI,
265 strlen(NG_FRAMERELAY_HOOK_DLCI)) != 0) {
267 /* It must be the downstream connection */
268 if (strcmp(name, NG_FRAMERELAY_HOOK_DOWNSTREAM) != 0)
271 /* Make sure we haven't already got one (paranoid) */
272 if (sc->downstream.hook)
276 hook->private = &sc->downstream;
277 sc->downstream.hook = hook;
278 sc->downstream.dlci = -1;
279 sc->downstream.flags |= CHAN_ACTIVE;
284 /* Must be a dlci hook at this point */
285 cp = name + strlen(NG_FRAMERELAY_HOOK_DLCI);
286 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
288 dlci = (int)strtoul(cp, &eptr, 10);
289 if (*eptr != '\0' || dlci < 0 || dlci > 1023)
293 * We have a dlci, now either find it, or allocate it. It's possible
294 * that we might have seen packets for it already and made an entry
297 ctxnum = ngfrm_allocate_CTX(sc, dlci);
302 * Be paranoid: if it's got a hook already, that dlci is in use .
303 * Generic code can not catch all the synonyms (e.g. dlci016 vs
306 if (sc->channel[ctxnum].hook != NULL)
310 * Put our hooks into it (pun not intended)
312 sc->channel[ctxnum].flags |= CHAN_ACTIVE;
313 hook->private = sc->channel + ctxnum;
314 sc->channel[ctxnum].hook = hook;
320 * Count up the size of the address header if we don't already know
323 ngfrm_addrlen(char *hdr)
325 if (hdr[0] & BYTEX_EA)
327 if (hdr[1] & BYTEX_EA)
329 if (hdr[2] & BYTEX_EA)
331 if (hdr[3] & BYTEX_EA)
337 * Receive data packet
340 ngfrm_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
342 struct ctxinfo *const ctxp = hook->private;
349 /* Data doesn't come in from just anywhere (e.g debug hook) */
355 /* If coming from downstream, decode it to a channel */
358 return (ngfrm_decode(hook->node, m, meta));
360 /* Derive the softc we will need */
361 sc = hook->node->private;
363 /* If there is no live channel, throw it away */
364 if ((sc->downstream.hook == NULL)
365 || ((ctxp->flags & CHAN_ACTIVE) == 0)) {
370 /* Store the DLCI on the front of the packet */
373 alen = 2; /* default value for transmit */
374 M_PREPEND(m, alen, MB_DONTWAIT);
379 data = mtod(m, char *);
382 * Shift the lowest bits into the address field untill we are done.
383 * First byte is MSBits of addr so work backwards.
387 data[0] = data[1] = '\0';
388 SHIFTOUT(makeup + 1, data[1], dlci);
389 SHIFTOUT(makeup + 0, data[0], dlci);
393 data[0] = data[1] = data[2] = '\0';
394 SHIFTOUT(makeup + 3, data[2], dlci); /* 3 and 2 is correct */
395 SHIFTOUT(makeup + 1, data[1], dlci);
396 SHIFTOUT(makeup + 0, data[0], dlci);
400 data[0] = data[1] = data[2] = data[3] = '\0';
401 SHIFTOUT(makeup + 3, data[3], dlci);
402 SHIFTOUT(makeup + 2, data[2], dlci);
403 SHIFTOUT(makeup + 1, data[1], dlci);
404 SHIFTOUT(makeup + 0, data[0], dlci);
412 NG_SEND_DATA(error, sc->downstream.hook, m, meta);
416 NG_FREE_DATA(m, meta);
421 * Decode an incoming frame coming from the switch
424 ngfrm_decode(node_p node, struct mbuf *m, meta_p meta)
426 const sc_p sc = node->private;
433 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
437 data = mtod(m, char *);
438 if ((alen = sc->addrlen) == 0) {
439 sc->addrlen = alen = ngfrm_addrlen(data);
443 SHIFTIN(makeup + 0, data[0], dlci);
444 SHIFTIN(makeup + 1, data[1], dlci);
447 SHIFTIN(makeup + 0, data[0], dlci);
448 SHIFTIN(makeup + 1, data[1], dlci);
449 SHIFTIN(makeup + 3, data[2], dlci); /* 3 and 2 is correct */
452 SHIFTIN(makeup + 0, data[0], dlci);
453 SHIFTIN(makeup + 1, data[1], dlci);
454 SHIFTIN(makeup + 2, data[2], dlci);
455 SHIFTIN(makeup + 3, data[3], dlci);
466 ctxnum = sc->ALT[dlci];
467 if ((ctxnum & CTX_VALID) && sc->channel[ctxnum &= CTX_VALUE].hook) {
470 NG_SEND_DATA(error, sc->channel[ctxnum].hook, m, meta);
476 NG_FREE_DATA(m, meta);
484 ngfrm_rmnode(node_p node)
486 const sc_p sc = node->private;
488 node->flags |= NG_INVALID;
491 node->private = NULL;
492 kfree(sc, M_NETGRAPH);
500 * Invalidate the private data associated with this dlci.
501 * For this type, removal of the last link resets tries to destroy the node.
504 ngfrm_disconnect(hook_p hook)
506 const sc_p sc = hook->node->private;
507 struct ctxinfo *const cp = hook->private;
510 /* If it's a regular dlci hook, then free resources etc.. */
519 if (hook->node->numhooks == 0)
520 ng_rmnode(hook->node);