xref: /freebsd/sys/netgraph/ng_frame_relay.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
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@whistle.com>
38  *
39  * $FreeBSD$
40  * $Whistle: ng_frame_relay.c,v 1.16 1999/01/28 23:54:53 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/conf.h>
56 #include <sys/errno.h>
57 #include <sys/malloc.h>
58 #include <sys/mbuf.h>
59 #include <sys/socket.h>
60 #include <sys/syslog.h>
61 #include <machine/clock.h>
62 
63 #include <netgraph/ng_message.h>
64 #include <netgraph/netgraph.h>
65 #include <netgraph/ng_frame_relay.h>
66 
67 /*
68  * Line info, and status per channel.
69  */
70 struct ctxinfo {		/* one per active hook */
71 	u_int   flags;
72 #define CHAN_VALID	0x01	/* assigned to a channel */
73 #define CHAN_ACTIVE	0x02	/* bottom level active */
74 	int     dlci;		/* the dlci assigned to this context */
75 	hook_p  hook;		/* if there's a hook assigned.. */
76 };
77 
78 #define MAX_CT 16		/* # of dlci's active at a time (POWER OF 2!) */
79 struct frmrel_softc {
80 	int     unit;		/* which card are we? */
81 	char    nodename[NG_NODELEN + 1];	/* store our node name */
82 	int     datahooks;	/* number of data hooks attached */
83 	node_p  node;		/* netgraph node */
84 	int     addrlen;	/* address header length */
85 	int     flags;		/* state */
86 	int     mtu;		/* guess */
87 	u_char  remote_seq;	/* sequence number the remote sent */
88 	u_char  local_seq;	/* sequence number the remote rcvd */
89 	u_short ALT[1024];	/* map DLCIs to CTX */
90 #define	CTX_VALID	0x8000		/* this bit means it's a valid CTX */
91 #define	CTX_VALUE	(MAX_CT - 1)	/* mask for context part */
92 	struct	ctxinfo channel[MAX_CT];
93 	struct	ctxinfo downstream;
94 };
95 typedef struct frmrel_softc *sc_p;
96 
97 #define BYTEX_EA	0x01	/* End Address. Always 0 on byte1 */
98 #define BYTE1_C_R	0x02
99 #define BYTE2_FECN	0x08	/* forwards congestion notification */
100 #define BYTE2_BECN	0x04	/* Backward congestion notification */
101 #define BYTE2_DE	0x02	/* Discard elligability */
102 #define LASTBYTE_D_C	0x02	/* last byte is dl_core or dlci info */
103 
104 /* Used to do headers */
105 static struct segment {
106 	u_char  mask;
107 	u_char  shift;
108 	u_char  width;
109 } makeup[] = {
110 	{ 0xfc, 2, 6 },
111 	{ 0xf0, 4, 4 },
112 	{ 0xfe, 1, 7 },
113 	{ 0xfc, 2, 6 }
114 };
115 
116 #define SHIFTIN(segment, byte, dlci) 					     \
117 	{								     \
118 		(dlci) <<= (segment)->width;				     \
119 		(dlci) |=						     \
120 			(((byte) & (segment)->mask) >> (segment)->shift);    \
121 	}
122 
123 #define SHIFTOUT(segment, byte, dlci)					     \
124 	{								     \
125 		(byte) |= (((dlci) << (segment)->shift) & (segment)->mask);  \
126 		(dlci) >>= (segment)->width;				     \
127 	}
128 
129 /* Netgraph methods */
130 static int ngfrm_constructor(node_p *nodep);
131 static int ngfrm_rmnode(node_p node);
132 static int ngfrm_newhook(node_p node, hook_p hook, const char *name);
133 static int ngfrm_rcvdata(hook_p hook, struct mbuf *m, meta_p meta);
134 static int ngfrm_disconnect(hook_p hook);
135 
136 /* Other internal functions */
137 static int ngfrm_decode(node_p node, struct mbuf * m, meta_p meta);
138 static int ngfrm_addrlen(char *hdr);
139 static int ngfrm_allocate_CTX(sc_p sc, int dlci);
140 
141 /* Netgraph type */
142 static struct ng_type typestruct = {
143 	NG_VERSION,
144 	NG_FRAMERELAY_NODE_TYPE,
145 	NULL,
146 	ngfrm_constructor,
147 	NULL,
148 	ngfrm_rmnode,
149 	ngfrm_newhook,
150 	NULL,
151 	NULL,
152 	ngfrm_rcvdata,
153 	ngfrm_rcvdata,
154 	ngfrm_disconnect
155 };
156 NETGRAPH_INIT(framerelay, &typestruct);
157 
158 /*
159  * Given a DLCI, return the index of the  context table entry for it,
160  * Allocating a new one if needs be, or -1 if none available.
161  */
162 static int
163 ngfrm_allocate_CTX(sc_p sc, int dlci)
164 {
165 	u_int   ctxnum = -1;	/* what ctx number we are using */
166 	volatile struct ctxinfo *CTXp = NULL;
167 
168 	/* Sanity check the dlci value */
169 	if (dlci > 1023)
170 		return (-1);
171 
172 	/* Check to see if we already have an entry for this DLCI */
173 	if (sc->ALT[dlci]) {
174 		if ((ctxnum = sc->ALT[dlci] & CTX_VALUE) < MAX_CT) {
175 			CTXp = sc->channel + ctxnum;
176 		} else {
177 			ctxnum = -1;
178 			sc->ALT[dlci] = 0;	/* paranoid but... */
179 		}
180 	}
181 
182 	/*
183 	 * If the index has no valid entry yet, then we need to allocate a
184 	 * CTX number to it
185 	 */
186 	if (CTXp == NULL) {
187 		for (ctxnum = 0; ctxnum < MAX_CT; ctxnum++) {
188 			/*
189 			 * If the VALID flag is empty it is unused
190 			 */
191 			if ((sc->channel[ctxnum].flags & CHAN_VALID) == 0) {
192 				bzero(sc->channel + ctxnum,
193 				      sizeof(struct ctxinfo));
194 				CTXp = sc->channel + ctxnum;
195 				sc->ALT[dlci] = ctxnum | CTX_VALID;
196 				sc->channel[ctxnum].dlci = dlci;
197 				sc->channel[ctxnum].flags = CHAN_VALID;
198 				break;
199 			}
200 		}
201 	}
202 
203 	/*
204 	 * If we still don't have a CTX pointer, then we never found a free
205 	 * spot so give up now..
206 	 */
207 	if (!CTXp) {
208 		log(LOG_ERR, "No CTX available for dlci %d\n", dlci);
209 		return (-1);
210 	}
211 	return (ctxnum);
212 }
213 
214 /*
215  * Node constructor
216  */
217 static int
218 ngfrm_constructor(node_p *nodep)
219 {
220 	sc_p sc;
221 	int error = 0;
222 
223 	MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT);
224 	if (!sc)
225 		return (ENOMEM);
226 	bzero(sc, sizeof(*sc));
227 	if ((error = ng_make_node_common(&typestruct, nodep))) {
228 		FREE(sc, M_NETGRAPH);
229 		return (error);
230 	}
231 	sc->addrlen = 2;	/* default */
232 
233 	/* Link the node and our private info */
234 	(*nodep)->private = sc;
235 	sc->node = *nodep;
236 	return (0);
237 }
238 
239 /*
240  * Add a new hook
241  *
242  * We allow hooks called "debug", "downstream" and dlci[0-1023]
243  * The hook's private info points to our stash of info about that
244  * channel. A NULL pointer is debug and a DLCI of -1 means downstream.
245  */
246 static int
247 ngfrm_newhook(node_p node, hook_p hook, const char *name)
248 {
249 	const sc_p  sc = node->private;
250 	const char *cp;
251 	char	    c = '\0';
252 	int	    digits = 0;
253 	int	    dlci = 0;
254 	int	    ctxnum;
255 
256 	/* Check if it's our friend the control hook */
257 	if (strcmp(name, NG_FRAMERELAY_HOOK_DEBUG) == 0) {
258 		hook->private = NULL;	/* paranoid */
259 		return (0);
260 	}
261 
262 	/*
263 	 * All other hooks either start with 'dlci' and have a decimal
264 	 * trailing channel number up to 4 digits, or are the downstream
265 	 * hook.
266 	 */
267 	if (strncmp(name, NG_FRAMERELAY_HOOK_DLCI,
268 	    strlen(NG_FRAMERELAY_HOOK_DLCI)) != 0) {
269 
270 		/* It must be the downstream connection */
271 		if (strcmp(name, NG_FRAMERELAY_HOOK_DOWNSTREAM) != 0)
272 			return EINVAL;
273 
274 		/* Make sure we haven't already got one (paranoid) */
275 		if (sc->downstream.hook)
276 			return (EADDRINUSE);
277 
278 		/* OK add it */
279 		hook->private = &sc->downstream;
280 		sc->downstream.hook = hook;
281 		sc->downstream.dlci = -1;
282 		sc->downstream.flags |= CHAN_ACTIVE;
283 		sc->datahooks++;
284 		return (0);
285 	}
286 
287 	/* Must be a dlci hook at this point */
288 	cp = name + strlen(NG_FRAMERELAY_HOOK_DLCI);
289 	while ((digits < 5) && ((c = *cp++) >= '0') && (c <= '9')) {
290 		dlci *= 10;
291 		dlci += c - '0';
292 		digits++;
293 	}
294 	if ((c != 0) || (digits == 5) || (dlci < 0) || (dlci > 1023))
295 		return (EINVAL);
296 	/*
297 	 * We have a dlci, now either find it, or allocate it. It's possible
298 	 * that we might have seen packets for it already and made an entry
299 	 * for it.
300 	 */
301 	ctxnum = ngfrm_allocate_CTX(sc, dlci);
302 	if (ctxnum == -1)
303 		return (ENOBUFS);
304 
305 	/*
306 	 * Be paranoid: if it's got a hook already, that dlci is in use .
307 	 * Generic code can not catch all the synonyms (e.g. dlci016 vs
308 	 * dlci16)
309 	 */
310 	if (sc->channel[ctxnum].hook != NULL)
311 		return (EADDRINUSE);
312 
313 	/*
314 	 * Put our hooks into it (pun not intended)
315 	 */
316 	sc->channel[ctxnum].flags |= CHAN_ACTIVE;
317 	hook->private = sc->channel + ctxnum;
318 	sc->channel[ctxnum].hook = hook;
319 	sc->datahooks++;
320 	return (0);
321 }
322 
323 /*
324  * Count up the size of the address header if we don't already know
325  */
326 int
327 ngfrm_addrlen(char *hdr)
328 {
329 	if (hdr[0] & BYTEX_EA)
330 		return 0;
331 	if (hdr[1] & BYTEX_EA)
332 		return 2;
333 	if (hdr[2] & BYTEX_EA)
334 		return 3;
335 	if (hdr[3] & BYTEX_EA)
336 		return 4;
337 	return 0;
338 }
339 
340 /*
341  * Receive data packet
342  */
343 static int
344 ngfrm_rcvdata(hook_p hook, struct mbuf *m, meta_p meta)
345 {
346 	struct	ctxinfo *const ctxp = hook->private;
347 	int     error = 0;
348 	int     dlci;
349 	sc_p    sc;
350 	int     alen;
351 	char   *data;
352 
353 	/* Data doesn't come in from just anywhere (e.g debug hook) */
354 	if (ctxp == NULL) {
355 		error = ENETDOWN;
356 		goto bad;
357 	}
358 
359 	/* If coming from downstream, decode it to a channel */
360 	dlci = ctxp->dlci;
361 	if (dlci == -1)
362 		return (ngfrm_decode(hook->node, m, meta));
363 
364 	/* Derive the softc we will need */
365 	sc = hook->node->private;
366 
367 	/* If there is no live channel, throw it away */
368 	if ((sc->downstream.hook == NULL)
369 	    || ((ctxp->flags & CHAN_ACTIVE) == 0)) {
370 		error = ENETDOWN;
371 		goto bad;
372 	}
373 
374 	/* Store the DLCI on the front of the packet */
375 	alen = sc->addrlen;
376 	if (alen == 0)
377 		alen = 2;	/* default value for transmit */
378 	M_PREPEND(m, alen, M_DONTWAIT);
379 	if (m == NULL) {
380 		error = ENOBUFS;
381 		goto bad;
382 	}
383 	data = mtod(m, char *);
384 
385 	/*
386 	 * Shift the lowest bits into the address field untill we are done.
387 	 * First byte is MSBits of addr so work backwards.
388 	 */
389 	switch (alen) {
390 	case 2:
391 		data[0] = data[1] = '\0';
392 		SHIFTOUT(makeup + 1, data[1], dlci);
393 		SHIFTOUT(makeup + 0, data[0], dlci);
394 		data[1] |= BYTEX_EA;
395 		break;
396 	case 3:
397 		data[0] = data[1] = data[2] = '\0';
398 		SHIFTOUT(makeup + 3, data[2], dlci);	/* 3 and 2 is correct */
399 		SHIFTOUT(makeup + 1, data[1], dlci);
400 		SHIFTOUT(makeup + 0, data[0], dlci);
401 		data[2] |= BYTEX_EA;
402 		break;
403 	case 4:
404 		data[0] = data[1] = data[2] = data[3] = '\0';
405 		SHIFTOUT(makeup + 3, data[3], dlci);
406 		SHIFTOUT(makeup + 2, data[2], dlci);
407 		SHIFTOUT(makeup + 1, data[1], dlci);
408 		SHIFTOUT(makeup + 0, data[0], dlci);
409 		data[3] |= BYTEX_EA;
410 		break;
411 	default:
412 		panic(__FUNCTION__);
413 	}
414 
415 	/* Send it */
416 	NG_SEND_DATA(error, sc->downstream.hook, m, meta);
417 	return (error);
418 
419 bad:
420 	NG_FREE_DATA(m, meta);
421 	return (error);
422 }
423 
424 /*
425  * Decode an incoming frame coming from the switch
426  */
427 static int
428 ngfrm_decode(node_p node, struct mbuf *m, meta_p meta)
429 {
430 	const sc_p  sc = node->private;
431 	char       *data;
432 	int         alen;
433 	u_int	    dlci = 0;
434 	int	    error = 0;
435 	int	    ctxnum;
436 
437 	if ((m = m_pullup(m, 4)) == NULL) {
438 		error = ENOBUFS;
439 		goto out;
440 	}
441 	data = mtod(m, char *);
442 	if ((alen = sc->addrlen) == 0) {
443 		sc->addrlen = alen = ngfrm_addrlen(data);
444 	}
445 	switch (alen) {
446 	case 2:
447 		SHIFTIN(makeup + 0, data[0], dlci);
448 		SHIFTIN(makeup + 1, data[1], dlci);
449 		break;
450 	case 3:
451 		SHIFTIN(makeup + 0, data[0], dlci);
452 		SHIFTIN(makeup + 1, data[1], dlci);
453 		SHIFTIN(makeup + 3, data[2], dlci);	/* 3 and 2 is correct */
454 		break;
455 	case 4:
456 		SHIFTIN(makeup + 0, data[0], dlci);
457 		SHIFTIN(makeup + 1, data[1], dlci);
458 		SHIFTIN(makeup + 2, data[2], dlci);
459 		SHIFTIN(makeup + 3, data[3], dlci);
460 		break;
461 	default:
462 		error = EINVAL;
463 		goto out;
464 	}
465 
466 	if (dlci > 1023) {
467 		error = EINVAL;
468 		goto out;
469 	}
470 	ctxnum = sc->ALT[dlci];
471 	if ((ctxnum & CTX_VALID) && sc->channel[ctxnum &= CTX_VALUE].hook) {
472 		/* Send it */
473 		m_adj(m, alen);
474 		NG_SEND_DATA(error, sc->channel[ctxnum].hook, m, meta);
475 		return (error);
476 	} else {
477 		error = ENETDOWN;
478 	}
479 out:
480 	NG_FREE_DATA(m, meta);
481 	return (error);
482 }
483 
484 /*
485  * Shutdown node
486  */
487 static int
488 ngfrm_rmnode(node_p node)
489 {
490 	const sc_p sc = node->private;
491 
492 	node->flags |= NG_INVALID;
493 	ng_cutlinks(node);
494 	ng_unname(node);
495 	node->private = NULL;
496 	FREE(sc, M_NETGRAPH);
497 	ng_unref(sc->node);
498 	return (0);
499 }
500 
501 /*
502  * Hook disconnection
503  *
504  * Invalidate the private data associated with this dlci.
505  * For this type, removal of the last link resets tries to destroy the node.
506  */
507 static int
508 ngfrm_disconnect(hook_p hook)
509 {
510 	const sc_p sc = hook->node->private;
511 	struct ctxinfo *const cp = hook->private;
512 	int dlci;
513 
514 	/* If it's a regular dlci hook, then free resources etc.. */
515 	if (cp != NULL) {
516 		cp->hook = NULL;
517 		dlci = cp->dlci;
518 		if (dlci != -1)
519 			sc->ALT[dlci] = 0;
520 		cp->flags = 0;
521 		sc->datahooks--;
522 	}
523 	if (hook->node->numhooks == 0)
524 		ng_rmnode(hook->node);
525 	return (0);
526 }
527