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 *
|
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 */ 68struct 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!) */ 77struct 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}; 92typedef 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 */ 102static 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 */ 127static ng_constructor_t ngfrm_constructor; 128static ng_shutdown_t ngfrm_rmnode; 129static ng_newhook_t ngfrm_newhook; 130static ng_rcvdata_t ngfrm_rcvdata; 131static ng_disconnect_t ngfrm_disconnect; 132 133/* Other internal functions */ 134static int ngfrm_decode(node_p node, struct mbuf * m, meta_p meta); 135static int ngfrm_addrlen(char *hdr); 136static int ngfrm_allocate_CTX(sc_p sc, int dlci); 137 138/* Netgraph type */ 139static struct ng_type typestruct = {
|
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_disconnect, 151 NULL 152}; 153NETGRAPH_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 */ 159static int 160ngfrm_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 */ 214static int 215ngfrm_constructor(node_p *nodep) 216{ 217 sc_p sc; 218 int error = 0; 219 220 MALLOC(sc, sc_p, sizeof(*sc), M_NETGRAPH, M_NOWAIT | M_ZERO); 221 if (!sc) 222 return (ENOMEM); 223 if ((error = ng_make_node_common(&typestruct, nodep))) { 224 FREE(sc, M_NETGRAPH); 225 return (error); 226 } 227 sc->addrlen = 2; /* default */ 228 229 /* Link the node and our private info */ 230 (*nodep)->private = sc; 231 sc->node = *nodep; 232 return (0); 233} 234 235/* 236 * Add a new hook 237 * 238 * We allow hooks called "debug", "downstream" and dlci[0-1023] 239 * The hook's private info points to our stash of info about that 240 * channel. A NULL pointer is debug and a DLCI of -1 means downstream. 241 */ 242static int 243ngfrm_newhook(node_p node, hook_p hook, const char *name) 244{ 245 const sc_p sc = node->private; 246 const char *cp; 247 char *eptr; 248 int dlci = 0; 249 int ctxnum; 250 251 /* Check if it's our friend the control hook */ 252 if (strcmp(name, NG_FRAMERELAY_HOOK_DEBUG) == 0) { 253 hook->private = NULL; /* paranoid */ 254 return (0); 255 } 256 257 /* 258 * All other hooks either start with 'dlci' and have a decimal 259 * trailing channel number up to 4 digits, or are the downstream 260 * hook. 261 */ 262 if (strncmp(name, NG_FRAMERELAY_HOOK_DLCI, 263 strlen(NG_FRAMERELAY_HOOK_DLCI)) != 0) { 264 265 /* It must be the downstream connection */ 266 if (strcmp(name, NG_FRAMERELAY_HOOK_DOWNSTREAM) != 0) 267 return EINVAL; 268 269 /* Make sure we haven't already got one (paranoid) */ 270 if (sc->downstream.hook) 271 return (EADDRINUSE); 272 273 /* OK add it */ 274 hook->private = &sc->downstream; 275 sc->downstream.hook = hook; 276 sc->downstream.dlci = -1; 277 sc->downstream.flags |= CHAN_ACTIVE; 278 sc->datahooks++; 279 return (0); 280 } 281 282 /* Must be a dlci hook at this point */ 283 cp = name + strlen(NG_FRAMERELAY_HOOK_DLCI); 284 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 285 return (EINVAL); 286 dlci = (int)strtoul(cp, &eptr, 10); 287 if (*eptr != '\0' || dlci < 0 || dlci > 1023) 288 return (EINVAL); 289 290 /* 291 * We have a dlci, now either find it, or allocate it. It's possible 292 * that we might have seen packets for it already and made an entry 293 * for it. 294 */ 295 ctxnum = ngfrm_allocate_CTX(sc, dlci); 296 if (ctxnum == -1) 297 return (ENOBUFS); 298 299 /* 300 * Be paranoid: if it's got a hook already, that dlci is in use . 301 * Generic code can not catch all the synonyms (e.g. dlci016 vs 302 * dlci16) 303 */ 304 if (sc->channel[ctxnum].hook != NULL) 305 return (EADDRINUSE); 306 307 /* 308 * Put our hooks into it (pun not intended) 309 */ 310 sc->channel[ctxnum].flags |= CHAN_ACTIVE; 311 hook->private = sc->channel + ctxnum; 312 sc->channel[ctxnum].hook = hook; 313 sc->datahooks++; 314 return (0); 315} 316 317/* 318 * Count up the size of the address header if we don't already know 319 */ 320int 321ngfrm_addrlen(char *hdr) 322{ 323 if (hdr[0] & BYTEX_EA) 324 return 0; 325 if (hdr[1] & BYTEX_EA) 326 return 2; 327 if (hdr[2] & BYTEX_EA) 328 return 3; 329 if (hdr[3] & BYTEX_EA) 330 return 4; 331 return 0; 332} 333 334/* 335 * Receive data packet 336 */ 337static int 338ngfrm_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 339 struct mbuf **ret_m, meta_p *ret_meta, struct ng_mesg **resp) 340{ 341 struct ctxinfo *const ctxp = hook->private; 342 int error = 0; 343 int dlci; 344 sc_p sc; 345 int alen; 346 char *data; 347 348 /* Data doesn't come in from just anywhere (e.g debug hook) */ 349 if (ctxp == NULL) { 350 error = ENETDOWN; 351 goto bad; 352 } 353 354 /* If coming from downstream, decode it to a channel */ 355 dlci = ctxp->dlci; 356 if (dlci == -1) 357 return (ngfrm_decode(hook->node, m, meta)); 358 359 /* Derive the softc we will need */ 360 sc = hook->node->private; 361 362 /* If there is no live channel, throw it away */ 363 if ((sc->downstream.hook == NULL) 364 || ((ctxp->flags & CHAN_ACTIVE) == 0)) { 365 error = ENETDOWN; 366 goto bad; 367 } 368 369 /* Store the DLCI on the front of the packet */ 370 alen = sc->addrlen; 371 if (alen == 0) 372 alen = 2; /* default value for transmit */ 373 M_PREPEND(m, alen, M_DONTWAIT); 374 if (m == NULL) { 375 error = ENOBUFS; 376 goto bad; 377 } 378 data = mtod(m, char *); 379 380 /* 381 * Shift the lowest bits into the address field untill we are done. 382 * First byte is MSBits of addr so work backwards. 383 */ 384 switch (alen) { 385 case 2: 386 data[0] = data[1] = '\0'; 387 SHIFTOUT(makeup + 1, data[1], dlci); 388 SHIFTOUT(makeup + 0, data[0], dlci); 389 data[1] |= BYTEX_EA; 390 break; 391 case 3: 392 data[0] = data[1] = data[2] = '\0'; 393 SHIFTOUT(makeup + 3, data[2], dlci); /* 3 and 2 is correct */ 394 SHIFTOUT(makeup + 1, data[1], dlci); 395 SHIFTOUT(makeup + 0, data[0], dlci); 396 data[2] |= BYTEX_EA; 397 break; 398 case 4: 399 data[0] = data[1] = data[2] = data[3] = '\0'; 400 SHIFTOUT(makeup + 3, data[3], dlci); 401 SHIFTOUT(makeup + 2, data[2], dlci); 402 SHIFTOUT(makeup + 1, data[1], dlci); 403 SHIFTOUT(makeup + 0, data[0], dlci); 404 data[3] |= BYTEX_EA; 405 break; 406 default: 407 panic(__FUNCTION__); 408 } 409 410 /* Send it */ 411 NG_SEND_DATA(error, sc->downstream.hook, m, meta); 412 return (error); 413 414bad: 415 NG_FREE_DATA(m, meta); 416 return (error); 417} 418 419/* 420 * Decode an incoming frame coming from the switch 421 */ 422static int 423ngfrm_decode(node_p node, struct mbuf *m, meta_p meta) 424{ 425 const sc_p sc = node->private; 426 char *data; 427 int alen; 428 u_int dlci = 0; 429 int error = 0; 430 int ctxnum; 431 432 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) { 433 error = ENOBUFS; 434 goto out; 435 } 436 data = mtod(m, char *); 437 if ((alen = sc->addrlen) == 0) { 438 sc->addrlen = alen = ngfrm_addrlen(data); 439 } 440 switch (alen) { 441 case 2: 442 SHIFTIN(makeup + 0, data[0], dlci); 443 SHIFTIN(makeup + 1, data[1], dlci); 444 break; 445 case 3: 446 SHIFTIN(makeup + 0, data[0], dlci); 447 SHIFTIN(makeup + 1, data[1], dlci); 448 SHIFTIN(makeup + 3, data[2], dlci); /* 3 and 2 is correct */ 449 break; 450 case 4: 451 SHIFTIN(makeup + 0, data[0], dlci); 452 SHIFTIN(makeup + 1, data[1], dlci); 453 SHIFTIN(makeup + 2, data[2], dlci); 454 SHIFTIN(makeup + 3, data[3], dlci); 455 break; 456 default: 457 error = EINVAL; 458 goto out; 459 } 460 461 if (dlci > 1023) { 462 error = EINVAL; 463 goto out; 464 } 465 ctxnum = sc->ALT[dlci]; 466 if ((ctxnum & CTX_VALID) && sc->channel[ctxnum &= CTX_VALUE].hook) { 467 /* Send it */ 468 m_adj(m, alen); 469 NG_SEND_DATA(error, sc->channel[ctxnum].hook, m, meta); 470 return (error); 471 } else { 472 error = ENETDOWN; 473 } 474out: 475 NG_FREE_DATA(m, meta); 476 return (error); 477} 478 479/* 480 * Shutdown node 481 */ 482static int 483ngfrm_rmnode(node_p node) 484{ 485 const sc_p sc = node->private; 486 487 node->flags |= NG_INVALID; 488 ng_cutlinks(node); 489 ng_unname(node); 490 node->private = NULL; 491 FREE(sc, M_NETGRAPH); 492 ng_unref(node); 493 return (0); 494} 495 496/* 497 * Hook disconnection 498 * 499 * Invalidate the private data associated with this dlci. 500 * For this type, removal of the last link resets tries to destroy the node. 501 */ 502static int 503ngfrm_disconnect(hook_p hook) 504{ 505 const sc_p sc = hook->node->private; 506 struct ctxinfo *const cp = hook->private; 507 int dlci; 508 509 /* If it's a regular dlci hook, then free resources etc.. */ 510 if (cp != NULL) { 511 cp->hook = NULL; 512 dlci = cp->dlci; 513 if (dlci != -1) 514 sc->ALT[dlci] = 0; 515 cp->flags = 0; 516 sc->datahooks--; 517 } 518 if (hook->node->numhooks == 0) 519 ng_rmnode(hook->node); 520 return (0); 521}
|