gcc44: properly pass and report target machine and version
[dragonfly.git] / sys / netgraph7 / ng_frame_relay.c
blobf76ee6aa753df8e4619caad4ebef71a2ce333472
1 /*
2 * ng_frame_relay.c
3 */
5 /*-
6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * All rights reserved.
8 *
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * OF SUCH DAMAGE.
38 * Author: Julian Elischer <julian@freebsd.org>
40 * $FreeBSD: src/sys/netgraph/ng_frame_relay.c,v 1.25 2006/01/14 21:49:31 glebius Exp $
41 * $DragonFly: src/sys/netgraph7/ng_frame_relay.c,v 1.2 2008/06/26 23:05:35 dillon Exp $
42 * $Whistle: ng_frame_relay.c,v 1.20 1999/11/01 09:24:51 julian Exp $
46 * This node implements the frame relay protocol, not including
47 * the LMI line management. This means basically keeping track
48 * of which DLCI's are active, doing frame (de)multiplexing, etc.
50 * It has a 'downstream' hook that goes to the line, and a
51 * hook for each DLCI (eg, 'dlci16').
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/kernel.h>
57 #include <sys/errno.h>
58 #include <sys/malloc.h>
59 #include <sys/mbuf.h>
60 #include <sys/syslog.h>
61 #include <sys/ctype.h>
63 #include "ng_message.h"
64 #include "netgraph.h"
65 #include "ng_frame_relay.h"
68 * Line info, and status per channel.
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.. */
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 int datahooks; /* number of data hooks attached */
82 node_p node; /* netgraph node */
83 int addrlen; /* address header length */
84 int flags; /* state */
85 int mtu; /* guess */
86 u_char remote_seq; /* sequence number the remote sent */
87 u_char local_seq; /* sequence number the remote rcvd */
88 u_short ALT[1024]; /* map DLCIs to CTX */
89 #define CTX_VALID 0x8000 /* this bit means it's a valid CTX */
90 #define CTX_VALUE (MAX_CT - 1) /* mask for context part */
91 struct ctxinfo channel[MAX_CT];
92 struct ctxinfo downstream;
94 typedef struct frmrel_softc *sc_p;
96 #define BYTEX_EA 0x01 /* End Address. Always 0 on byte1 */
97 #define BYTE1_C_R 0x02
98 #define BYTE2_FECN 0x08 /* forwards congestion notification */
99 #define BYTE2_BECN 0x04 /* Backward congestion notification */
100 #define BYTE2_DE 0x02 /* Discard elligability */
101 #define LASTBYTE_D_C 0x02 /* last byte is dl_core or dlci info */
103 /* Used to do headers */
104 const static struct segment {
105 u_char mask;
106 u_char shift;
107 u_char width;
108 } makeup[] = {
109 { 0xfc, 2, 6 },
110 { 0xf0, 4, 4 },
111 { 0xfe, 1, 7 },
112 { 0xfc, 2, 6 }
115 #define SHIFTIN(segment, byte, dlci) \
117 (dlci) <<= (segment)->width; \
118 (dlci) |= \
119 (((byte) & (segment)->mask) >> (segment)->shift); \
122 #define SHIFTOUT(segment, byte, dlci) \
124 (byte) |= (((dlci) << (segment)->shift) & (segment)->mask); \
125 (dlci) >>= (segment)->width; \
128 /* Netgraph methods */
129 static ng_constructor_t ngfrm_constructor;
130 static ng_shutdown_t ngfrm_shutdown;
131 static ng_newhook_t ngfrm_newhook;
132 static ng_rcvdata_t ngfrm_rcvdata;
133 static ng_disconnect_t ngfrm_disconnect;
135 /* Other internal functions */
136 static int ngfrm_decode(node_p node, item_p item);
137 static int ngfrm_addrlen(char *hdr);
138 static int ngfrm_allocate_CTX(sc_p sc, int dlci);
140 /* Netgraph type */
141 static struct ng_type typestruct = {
142 .version = NG_ABI_VERSION,
143 .name = NG_FRAMERELAY_NODE_TYPE,
144 .constructor = ngfrm_constructor,
145 .shutdown = ngfrm_shutdown,
146 .newhook = ngfrm_newhook,
147 .rcvdata = ngfrm_rcvdata,
148 .disconnect = ngfrm_disconnect,
150 NETGRAPH_INIT(framerelay, &typestruct);
153 * Given a DLCI, return the index of the context table entry for it,
154 * Allocating a new one if needs be, or -1 if none available.
156 static int
157 ngfrm_allocate_CTX(sc_p sc, int dlci)
159 u_int ctxnum = -1; /* what ctx number we are using */
160 volatile struct ctxinfo *CTXp = NULL;
162 /* Sanity check the dlci value */
163 if (dlci > 1023)
164 return (-1);
166 /* Check to see if we already have an entry for this DLCI */
167 if (sc->ALT[dlci]) {
168 if ((ctxnum = sc->ALT[dlci] & CTX_VALUE) < MAX_CT) {
169 CTXp = sc->channel + ctxnum;
170 } else {
171 ctxnum = -1;
172 sc->ALT[dlci] = 0; /* paranoid but... */
177 * If the index has no valid entry yet, then we need to allocate a
178 * CTX number to it
180 if (CTXp == NULL) {
181 for (ctxnum = 0; ctxnum < MAX_CT; ctxnum++) {
183 * If the VALID flag is empty it is unused
185 if ((sc->channel[ctxnum].flags & CHAN_VALID) == 0) {
186 bzero(sc->channel + ctxnum,
187 sizeof(struct ctxinfo));
188 CTXp = sc->channel + ctxnum;
189 sc->ALT[dlci] = ctxnum | CTX_VALID;
190 sc->channel[ctxnum].dlci = dlci;
191 sc->channel[ctxnum].flags = CHAN_VALID;
192 break;
198 * If we still don't have a CTX pointer, then we never found a free
199 * spot so give up now..
201 if (!CTXp) {
202 log(LOG_ERR, "No CTX available for dlci %d\n", dlci);
203 return (-1);
205 return (ctxnum);
209 * Node constructor
211 static int
212 ngfrm_constructor(node_p node)
214 sc_p sc;
216 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_WAITOK | M_NULLOK | M_ZERO);
217 if (!sc)
218 return (ENOMEM);
219 sc->addrlen = 2; /* default */
221 /* Link the node and our private info */
222 NG_NODE_SET_PRIVATE(node, sc);
223 sc->node = node;
224 return (0);
228 * Add a new hook
230 * We allow hooks called "debug", "downstream" and dlci[0-1023]
231 * The hook's private info points to our stash of info about that
232 * channel. A NULL pointer is debug and a DLCI of -1 means downstream.
234 static int
235 ngfrm_newhook(node_p node, hook_p hook, const char *name)
237 const sc_p sc = NG_NODE_PRIVATE(node);
238 const char *cp;
239 char *eptr;
240 int dlci = 0;
241 int ctxnum;
243 /* Check if it's our friend the control hook */
244 if (strcmp(name, NG_FRAMERELAY_HOOK_DEBUG) == 0) {
245 NG_HOOK_SET_PRIVATE(hook, NULL); /* paranoid */
246 return (0);
250 * All other hooks either start with 'dlci' and have a decimal
251 * trailing channel number up to 4 digits, or are the downstream
252 * hook.
254 if (strncmp(name, NG_FRAMERELAY_HOOK_DLCI,
255 strlen(NG_FRAMERELAY_HOOK_DLCI)) != 0) {
257 /* It must be the downstream connection */
258 if (strcmp(name, NG_FRAMERELAY_HOOK_DOWNSTREAM) != 0)
259 return EINVAL;
261 /* Make sure we haven't already got one (paranoid) */
262 if (sc->downstream.hook)
263 return (EADDRINUSE);
265 /* OK add it */
266 NG_HOOK_SET_PRIVATE(hook, &sc->downstream);
267 sc->downstream.hook = hook;
268 sc->downstream.dlci = -1;
269 sc->downstream.flags |= CHAN_ACTIVE;
270 sc->datahooks++;
271 return (0);
274 /* Must be a dlci hook at this point */
275 cp = name + strlen(NG_FRAMERELAY_HOOK_DLCI);
276 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
277 return (EINVAL);
278 dlci = (int)strtoul(cp, &eptr, 10);
279 if (*eptr != '\0' || dlci < 0 || dlci > 1023)
280 return (EINVAL);
283 * We have a dlci, now either find it, or allocate it. It's possible
284 * that we might have seen packets for it already and made an entry
285 * for it.
287 ctxnum = ngfrm_allocate_CTX(sc, dlci);
288 if (ctxnum == -1)
289 return (ENOBUFS);
292 * Be paranoid: if it's got a hook already, that dlci is in use .
293 * Generic code can not catch all the synonyms (e.g. dlci016 vs
294 * dlci16)
296 if (sc->channel[ctxnum].hook != NULL)
297 return (EADDRINUSE);
300 * Put our hooks into it (pun not intended)
302 sc->channel[ctxnum].flags |= CHAN_ACTIVE;
303 NG_HOOK_SET_PRIVATE(hook, sc->channel + ctxnum);
304 sc->channel[ctxnum].hook = hook;
305 sc->datahooks++;
306 return (0);
310 * Count up the size of the address header if we don't already know
313 ngfrm_addrlen(char *hdr)
315 if (hdr[0] & BYTEX_EA)
316 return 0;
317 if (hdr[1] & BYTEX_EA)
318 return 2;
319 if (hdr[2] & BYTEX_EA)
320 return 3;
321 if (hdr[3] & BYTEX_EA)
322 return 4;
323 return 0;
327 * Receive data packet
329 static int
330 ngfrm_rcvdata(hook_p hook, item_p item)
332 struct ctxinfo *const ctxp = NG_HOOK_PRIVATE(hook);
333 struct mbuf *m = NULL;
334 int error = 0;
335 int dlci;
336 sc_p sc;
337 int alen;
338 char *data;
340 /* Data doesn't come in from just anywhere (e.g debug hook) */
341 if (ctxp == NULL) {
342 error = ENETDOWN;
343 goto bad;
346 /* If coming from downstream, decode it to a channel */
347 dlci = ctxp->dlci;
348 if (dlci == -1)
349 return (ngfrm_decode(NG_HOOK_NODE(hook), item));
351 NGI_GET_M(item, m);
352 /* Derive the softc we will need */
353 sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
355 /* If there is no live channel, throw it away */
356 if ((sc->downstream.hook == NULL)
357 || ((ctxp->flags & CHAN_ACTIVE) == 0)) {
358 error = ENETDOWN;
359 goto bad;
362 /* Store the DLCI on the front of the packet */
363 alen = sc->addrlen;
364 if (alen == 0)
365 alen = 2; /* default value for transmit */
366 M_PREPEND(m, alen, MB_DONTWAIT);
367 if (m == NULL) {
368 error = ENOBUFS;
369 goto bad;
371 data = mtod(m, char *);
374 * Shift the lowest bits into the address field untill we are done.
375 * First byte is MSBits of addr so work backwards.
377 switch (alen) {
378 case 2:
379 data[0] = data[1] = '\0';
380 SHIFTOUT(makeup + 1, data[1], dlci);
381 SHIFTOUT(makeup + 0, data[0], dlci);
382 data[1] |= BYTEX_EA;
383 break;
384 case 3:
385 data[0] = data[1] = data[2] = '\0';
386 SHIFTOUT(makeup + 3, data[2], dlci); /* 3 and 2 is correct */
387 SHIFTOUT(makeup + 1, data[1], dlci);
388 SHIFTOUT(makeup + 0, data[0], dlci);
389 data[2] |= BYTEX_EA;
390 break;
391 case 4:
392 data[0] = data[1] = data[2] = data[3] = '\0';
393 SHIFTOUT(makeup + 3, data[3], dlci);
394 SHIFTOUT(makeup + 2, data[2], dlci);
395 SHIFTOUT(makeup + 1, data[1], dlci);
396 SHIFTOUT(makeup + 0, data[0], dlci);
397 data[3] |= BYTEX_EA;
398 break;
399 default:
400 panic(__func__);
403 /* Send it */
404 NG_FWD_NEW_DATA(error, item, sc->downstream.hook, m);
405 return (error);
407 bad:
408 NG_FREE_ITEM(item);
409 NG_FREE_M(m);
410 return (error);
414 * Decode an incoming frame coming from the switch
416 static int
417 ngfrm_decode(node_p node, item_p item)
419 const sc_p sc = NG_NODE_PRIVATE(node);
420 char *data;
421 int alen;
422 u_int dlci = 0;
423 int error = 0;
424 int ctxnum;
425 struct mbuf *m;
427 NGI_GET_M(item, m);
428 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) {
429 error = ENOBUFS;
430 goto out;
432 data = mtod(m, char *);
433 if ((alen = sc->addrlen) == 0) {
434 sc->addrlen = alen = ngfrm_addrlen(data);
436 switch (alen) {
437 case 2:
438 SHIFTIN(makeup + 0, data[0], dlci);
439 SHIFTIN(makeup + 1, data[1], dlci);
440 break;
441 case 3:
442 SHIFTIN(makeup + 0, data[0], dlci);
443 SHIFTIN(makeup + 1, data[1], dlci);
444 SHIFTIN(makeup + 3, data[2], dlci); /* 3 and 2 is correct */
445 break;
446 case 4:
447 SHIFTIN(makeup + 0, data[0], dlci);
448 SHIFTIN(makeup + 1, data[1], dlci);
449 SHIFTIN(makeup + 2, data[2], dlci);
450 SHIFTIN(makeup + 3, data[3], dlci);
451 break;
452 default:
453 error = EINVAL;
454 goto out;
457 if (dlci > 1023) {
458 error = EINVAL;
459 goto out;
461 ctxnum = sc->ALT[dlci];
462 if ((ctxnum & CTX_VALID) && sc->channel[ctxnum &= CTX_VALUE].hook) {
463 /* Send it */
464 m_adj(m, alen);
465 NG_FWD_NEW_DATA(error, item, sc->channel[ctxnum].hook, m);
466 return (error);
467 } else {
468 error = ENETDOWN;
470 out:
471 NG_FREE_ITEM(item);
472 NG_FREE_M(m);
473 return (error);
477 * Shutdown node
479 static int
480 ngfrm_shutdown(node_p node)
482 const sc_p sc = NG_NODE_PRIVATE(node);
484 NG_NODE_SET_PRIVATE(node, NULL);
485 FREE(sc, M_NETGRAPH);
486 NG_NODE_UNREF(node);
487 return (0);
491 * Hook disconnection
493 * Invalidate the private data associated with this dlci.
494 * For this type, removal of the last link resets tries to destroy the node.
496 static int
497 ngfrm_disconnect(hook_p hook)
499 const sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
500 struct ctxinfo *const cp = NG_HOOK_PRIVATE(hook);
501 int dlci;
503 /* If it's a regular dlci hook, then free resources etc.. */
504 if (cp != NULL) {
505 cp->hook = NULL;
506 dlci = cp->dlci;
507 if (dlci != -1)
508 sc->ALT[dlci] = 0;
509 cp->flags = 0;
510 sc->datahooks--;
512 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
513 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))))
514 ng_rmnode_self(NG_HOOK_NODE(hook));
515 return (0);