xref: /freebsd/sys/netgraph/ng_frame_relay.c (revision f9218d3d4fd34f082473b3a021c6d4d109fb47cf)
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 Elischer <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_shutdown;
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, item_p item);
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_ABI_VERSION,
141 	NG_FRAMERELAY_NODE_TYPE,
142 	NULL,
143 	ngfrm_constructor,
144 	NULL,
145 	ngfrm_shutdown,
146 	ngfrm_newhook,
147 	NULL,
148 	NULL,
149 	ngfrm_rcvdata,
150 	ngfrm_disconnect,
151 	NULL
152 };
153 NETGRAPH_INIT(framerelay, &typestruct);
154 
155 /*
156  * Given a DLCI, return the index of the  context table entry for it,
157  * Allocating a new one if needs be, or -1 if none available.
158  */
159 static int
160 ngfrm_allocate_CTX(sc_p sc, int dlci)
161 {
162 	u_int   ctxnum = -1;	/* what ctx number we are using */
163 	volatile struct ctxinfo *CTXp = NULL;
164 
165 	/* Sanity check the dlci value */
166 	if (dlci > 1023)
167 		return (-1);
168 
169 	/* Check to see if we already have an entry for this DLCI */
170 	if (sc->ALT[dlci]) {
171 		if ((ctxnum = sc->ALT[dlci] & CTX_VALUE) < MAX_CT) {
172 			CTXp = sc->channel + ctxnum;
173 		} else {
174 			ctxnum = -1;
175 			sc->ALT[dlci] = 0;	/* paranoid but... */
176 		}
177 	}
178 
179 	/*
180 	 * If the index has no valid entry yet, then we need to allocate a
181 	 * CTX number to it
182 	 */
183 	if (CTXp == NULL) {
184 		for (ctxnum = 0; ctxnum < MAX_CT; ctxnum++) {
185 			/*
186 			 * If the VALID flag is empty it is unused
187 			 */
188 			if ((sc->channel[ctxnum].flags & CHAN_VALID) == 0) {
189 				bzero(sc->channel + ctxnum,
190 				      sizeof(struct ctxinfo));
191 				CTXp = sc->channel + ctxnum;
192 				sc->ALT[dlci] = ctxnum | CTX_VALID;
193 				sc->channel[ctxnum].dlci = dlci;
194 				sc->channel[ctxnum].flags = CHAN_VALID;
195 				break;
196 			}
197 		}
198 	}
199 
200 	/*
201 	 * If we still don't have a CTX pointer, then we never found a free
202 	 * spot so give up now..
203 	 */
204 	if (!CTXp) {
205 		log(LOG_ERR, "No CTX available for dlci %d\n", dlci);
206 		return (-1);
207 	}
208 	return (ctxnum);
209 }
210 
211 /*
212  * Node constructor
213  */
214 static int
215 ngfrm_constructor(node_p node)
216 {
217 	sc_p sc;
218 
219 	MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO);
220 	if (!sc)
221 		return (ENOMEM);
222 	sc->addrlen = 2;	/* default */
223 
224 	/* Link the node and our private info */
225 	NG_NODE_SET_PRIVATE(node, sc);
226 	sc->node = node;
227 	return (0);
228 }
229 
230 /*
231  * Add a new hook
232  *
233  * We allow hooks called "debug", "downstream" and dlci[0-1023]
234  * The hook's private info points to our stash of info about that
235  * channel. A NULL pointer is debug and a DLCI of -1 means downstream.
236  */
237 static int
238 ngfrm_newhook(node_p node, hook_p hook, const char *name)
239 {
240 	const sc_p sc = NG_NODE_PRIVATE(node);
241 	const char *cp;
242 	char *eptr;
243 	int dlci = 0;
244 	int ctxnum;
245 
246 	/* Check if it's our friend the control hook */
247 	if (strcmp(name, NG_FRAMERELAY_HOOK_DEBUG) == 0) {
248 		NG_HOOK_SET_PRIVATE(hook, NULL);	/* paranoid */
249 		return (0);
250 	}
251 
252 	/*
253 	 * All other hooks either start with 'dlci' and have a decimal
254 	 * trailing channel number up to 4 digits, or are the downstream
255 	 * hook.
256 	 */
257 	if (strncmp(name, NG_FRAMERELAY_HOOK_DLCI,
258 	    strlen(NG_FRAMERELAY_HOOK_DLCI)) != 0) {
259 
260 		/* It must be the downstream connection */
261 		if (strcmp(name, NG_FRAMERELAY_HOOK_DOWNSTREAM) != 0)
262 			return EINVAL;
263 
264 		/* Make sure we haven't already got one (paranoid) */
265 		if (sc->downstream.hook)
266 			return (EADDRINUSE);
267 
268 		/* OK add it */
269 		NG_HOOK_SET_PRIVATE(hook, &sc->downstream);
270 		sc->downstream.hook = hook;
271 		sc->downstream.dlci = -1;
272 		sc->downstream.flags |= CHAN_ACTIVE;
273 		sc->datahooks++;
274 		return (0);
275 	}
276 
277 	/* Must be a dlci hook at this point */
278 	cp = name + strlen(NG_FRAMERELAY_HOOK_DLCI);
279 	if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
280 		return (EINVAL);
281 	dlci = (int)strtoul(cp, &eptr, 10);
282 	if (*eptr != '\0' || dlci < 0 || dlci > 1023)
283 		return (EINVAL);
284 
285 	/*
286 	 * We have a dlci, now either find it, or allocate it. It's possible
287 	 * that we might have seen packets for it already and made an entry
288 	 * for it.
289 	 */
290 	ctxnum = ngfrm_allocate_CTX(sc, dlci);
291 	if (ctxnum == -1)
292 		return (ENOBUFS);
293 
294 	/*
295 	 * Be paranoid: if it's got a hook already, that dlci is in use .
296 	 * Generic code can not catch all the synonyms (e.g. dlci016 vs
297 	 * dlci16)
298 	 */
299 	if (sc->channel[ctxnum].hook != NULL)
300 		return (EADDRINUSE);
301 
302 	/*
303 	 * Put our hooks into it (pun not intended)
304 	 */
305 	sc->channel[ctxnum].flags |= CHAN_ACTIVE;
306 	NG_HOOK_SET_PRIVATE(hook, sc->channel + ctxnum);
307 	sc->channel[ctxnum].hook = hook;
308 	sc->datahooks++;
309 	return (0);
310 }
311 
312 /*
313  * Count up the size of the address header if we don't already know
314  */
315 int
316 ngfrm_addrlen(char *hdr)
317 {
318 	if (hdr[0] & BYTEX_EA)
319 		return 0;
320 	if (hdr[1] & BYTEX_EA)
321 		return 2;
322 	if (hdr[2] & BYTEX_EA)
323 		return 3;
324 	if (hdr[3] & BYTEX_EA)
325 		return 4;
326 	return 0;
327 }
328 
329 /*
330  * Receive data packet
331  */
332 static int
333 ngfrm_rcvdata(hook_p hook, item_p item)
334 {
335 	struct	ctxinfo *const ctxp = NG_HOOK_PRIVATE(hook);
336 	int     error = 0;
337 	int     dlci;
338 	sc_p    sc;
339 	int     alen;
340 	char   *data;
341 	struct mbuf *m;
342 
343 	/* Data doesn't come in from just anywhere (e.g debug hook) */
344 	if (ctxp == NULL) {
345 		error = ENETDOWN;
346 		goto bad;
347 	}
348 
349 	/* If coming from downstream, decode it to a channel */
350 	dlci = ctxp->dlci;
351 	if (dlci == -1)
352 		return (ngfrm_decode(NG_HOOK_NODE(hook), item));
353 
354 	NGI_GET_M(item, m);
355 	/* Derive the softc we will need */
356 	sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
357 
358 	/* If there is no live channel, throw it away */
359 	if ((sc->downstream.hook == NULL)
360 	    || ((ctxp->flags & CHAN_ACTIVE) == 0)) {
361 		error = ENETDOWN;
362 		goto bad;
363 	}
364 
365 	/* Store the DLCI on the front of the packet */
366 	alen = sc->addrlen;
367 	if (alen == 0)
368 		alen = 2;	/* default value for transmit */
369 	M_PREPEND(m, alen, M_DONTWAIT);
370 	if (m == NULL) {
371 		error = ENOBUFS;
372 		goto bad;
373 	}
374 	data = mtod(m, char *);
375 
376 	/*
377 	 * Shift the lowest bits into the address field untill we are done.
378 	 * First byte is MSBits of addr so work backwards.
379 	 */
380 	switch (alen) {
381 	case 2:
382 		data[0] = data[1] = '\0';
383 		SHIFTOUT(makeup + 1, data[1], dlci);
384 		SHIFTOUT(makeup + 0, data[0], dlci);
385 		data[1] |= BYTEX_EA;
386 		break;
387 	case 3:
388 		data[0] = data[1] = data[2] = '\0';
389 		SHIFTOUT(makeup + 3, data[2], dlci);	/* 3 and 2 is correct */
390 		SHIFTOUT(makeup + 1, data[1], dlci);
391 		SHIFTOUT(makeup + 0, data[0], dlci);
392 		data[2] |= BYTEX_EA;
393 		break;
394 	case 4:
395 		data[0] = data[1] = data[2] = data[3] = '\0';
396 		SHIFTOUT(makeup + 3, data[3], dlci);
397 		SHIFTOUT(makeup + 2, data[2], dlci);
398 		SHIFTOUT(makeup + 1, data[1], dlci);
399 		SHIFTOUT(makeup + 0, data[0], dlci);
400 		data[3] |= BYTEX_EA;
401 		break;
402 	default:
403 		panic(__func__);
404 	}
405 
406 	/* Send it */
407 	NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);
408 	return (error);
409 
410 bad:
411 	NG_FREE_ITEM(item);
412 	NG_FREE_M(m);
413 	return (error);
414 }
415 
416 /*
417  * Decode an incoming frame coming from the switch
418  */
419 static int
420 ngfrm_decode(node_p node, item_p item)
421 {
422 	const sc_p  sc = NG_NODE_PRIVATE(node);
423 	char       *data;
424 	int         alen;
425 	u_int	    dlci = 0;
426 	int	    error = 0;
427 	int	    ctxnum;
428 	struct mbuf *m;
429 
430 	NGI_GET_M(item, m);
431 	if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
432 		error = ENOBUFS;
433 		goto out;
434 	}
435 	data = mtod(m, char *);
436 	if ((alen = sc->addrlen) == 0) {
437 		sc->addrlen = alen = ngfrm_addrlen(data);
438 	}
439 	switch (alen) {
440 	case 2:
441 		SHIFTIN(makeup + 0, data[0], dlci);
442 		SHIFTIN(makeup + 1, data[1], dlci);
443 		break;
444 	case 3:
445 		SHIFTIN(makeup + 0, data[0], dlci);
446 		SHIFTIN(makeup + 1, data[1], dlci);
447 		SHIFTIN(makeup + 3, data[2], dlci);	/* 3 and 2 is correct */
448 		break;
449 	case 4:
450 		SHIFTIN(makeup + 0, data[0], dlci);
451 		SHIFTIN(makeup + 1, data[1], dlci);
452 		SHIFTIN(makeup + 2, data[2], dlci);
453 		SHIFTIN(makeup + 3, data[3], dlci);
454 		break;
455 	default:
456 		error = EINVAL;
457 		goto out;
458 	}
459 
460 	if (dlci > 1023) {
461 		error = EINVAL;
462 		goto out;
463 	}
464 	ctxnum = sc->ALT[dlci];
465 	if ((ctxnum & CTX_VALID) && sc->channel[ctxnum &= CTX_VALUE].hook) {
466 		/* Send it */
467 		m_adj(m, alen);
468 		NG_FWD_NEW_DATA(error, item, sc->channel[ctxnum].hook, m);
469 		return (error);
470 	} else {
471 		error = ENETDOWN;
472 	}
473 out:
474 	NG_FREE_ITEM(item);
475 	NG_FREE_M(m);
476 	return (error);
477 }
478 
479 /*
480  * Shutdown node
481  */
482 static int
483 ngfrm_shutdown(node_p node)
484 {
485 	const sc_p sc = NG_NODE_PRIVATE(node);
486 
487 	NG_NODE_SET_PRIVATE(node, NULL);
488 	FREE(sc, M_NETGRAPH);
489 	NG_NODE_UNREF(node);
490 	return (0);
491 }
492 
493 /*
494  * Hook disconnection
495  *
496  * Invalidate the private data associated with this dlci.
497  * For this type, removal of the last link resets tries to destroy the node.
498  */
499 static int
500 ngfrm_disconnect(hook_p hook)
501 {
502 	const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
503 	struct ctxinfo *const cp = NG_HOOK_PRIVATE(hook);
504 	int dlci;
505 
506 	/* If it's a regular dlci hook, then free resources etc.. */
507 	if (cp != NULL) {
508 		cp->hook = NULL;
509 		dlci = cp->dlci;
510 		if (dlci != -1)
511 			sc->ALT[dlci] = 0;
512 		cp->flags = 0;
513 		sc->datahooks--;
514 	}
515 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
516 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
517 		ng_rmnode_self(NG_HOOK_NODE(hook));
518 	return (0);
519 }
520