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