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: src/sys/netgraph/ng_frame_relay.c,v 1.9.2.2 2000/10/24 18:36:45 julian Exp $ 40 * $DragonFly: src/sys/netgraph/frame_relay/ng_frame_relay.c,v 1.5 2005/02/17 13:59:59 joerg Exp $ 41 * $Whistle: ng_frame_relay.c,v 1.20 1999/11/01 09:24:51 julian Exp $ 42 */ 43 44 /* 45 * This node implements the frame relay protocol, not including 46 * the LMI line management. This means basically keeping track 47 * of which DLCI's are active, doing frame (de)multiplexing, etc. 48 * 49 * It has a 'downstream' hook that goes to the line, and a 50 * hook for each DLCI (eg, 'dlci16'). 51 */ 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/kernel.h> 56 #include <sys/errno.h> 57 #include <sys/malloc.h> 58 #include <sys/mbuf.h> 59 #include <sys/syslog.h> 60 #include <sys/ctype.h> 61 #include <machine/clock.h> 62 63 #include <netgraph/ng_message.h> 64 #include <netgraph/netgraph.h> 65 #include "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 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; 93 }; 94 typedef struct frmrel_softc *sc_p; 95 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 */ 102 103 /* Used to do headers */ 104 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 } 113 }; 114 115 #define SHIFTIN(segment, byte, dlci) \ 116 { \ 117 (dlci) <<= (segment)->width; \ 118 (dlci) |= \ 119 (((byte) & (segment)->mask) >> (segment)->shift); \ 120 } 121 122 #define SHIFTOUT(segment, byte, dlci) \ 123 { \ 124 (byte) |= (((dlci) << (segment)->shift) & (segment)->mask); \ 125 (dlci) >>= (segment)->width; \ 126 } 127 128 /* Netgraph methods */ 129 static ng_constructor_t ngfrm_constructor; 130 static ng_shutdown_t ngfrm_rmnode; 131 static ng_newhook_t ngfrm_newhook; 132 static ng_rcvdata_t ngfrm_rcvdata; 133 static ng_disconnect_t ngfrm_disconnect; 134 135 /* Other internal functions */ 136 static int ngfrm_decode(node_p node, struct mbuf * m, meta_p meta); 137 static int ngfrm_addrlen(char *hdr); 138 static int ngfrm_allocate_CTX(sc_p sc, int dlci); 139 140 /* Netgraph type */ 141 static struct ng_type typestruct = { 142 NG_VERSION, 143 NG_FRAMERELAY_NODE_TYPE, 144 NULL, 145 ngfrm_constructor, 146 NULL, 147 ngfrm_rmnode, 148 ngfrm_newhook, 149 NULL, 150 NULL, 151 ngfrm_rcvdata, 152 ngfrm_rcvdata, 153 ngfrm_disconnect, 154 NULL 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 *eptr; 252 int dlci = 0; 253 int ctxnum; 254 255 /* Check if it's our friend the control hook */ 256 if (strcmp(name, NG_FRAMERELAY_HOOK_DEBUG) == 0) { 257 hook->private = NULL; /* paranoid */ 258 return (0); 259 } 260 261 /* 262 * All other hooks either start with 'dlci' and have a decimal 263 * trailing channel number up to 4 digits, or are the downstream 264 * hook. 265 */ 266 if (strncmp(name, NG_FRAMERELAY_HOOK_DLCI, 267 strlen(NG_FRAMERELAY_HOOK_DLCI)) != 0) { 268 269 /* It must be the downstream connection */ 270 if (strcmp(name, NG_FRAMERELAY_HOOK_DOWNSTREAM) != 0) 271 return EINVAL; 272 273 /* Make sure we haven't already got one (paranoid) */ 274 if (sc->downstream.hook) 275 return (EADDRINUSE); 276 277 /* OK add it */ 278 hook->private = &sc->downstream; 279 sc->downstream.hook = hook; 280 sc->downstream.dlci = -1; 281 sc->downstream.flags |= CHAN_ACTIVE; 282 sc->datahooks++; 283 return (0); 284 } 285 286 /* Must be a dlci hook at this point */ 287 cp = name + strlen(NG_FRAMERELAY_HOOK_DLCI); 288 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 289 return (EINVAL); 290 dlci = (int)strtoul(cp, &eptr, 10); 291 if (*eptr != '\0' || dlci < 0 || dlci > 1023) 292 return (EINVAL); 293 294 /* 295 * We have a dlci, now either find it, or allocate it. It's possible 296 * that we might have seen packets for it already and made an entry 297 * for it. 298 */ 299 ctxnum = ngfrm_allocate_CTX(sc, dlci); 300 if (ctxnum == -1) 301 return (ENOBUFS); 302 303 /* 304 * Be paranoid: if it's got a hook already, that dlci is in use . 305 * Generic code can not catch all the synonyms (e.g. dlci016 vs 306 * dlci16) 307 */ 308 if (sc->channel[ctxnum].hook != NULL) 309 return (EADDRINUSE); 310 311 /* 312 * Put our hooks into it (pun not intended) 313 */ 314 sc->channel[ctxnum].flags |= CHAN_ACTIVE; 315 hook->private = sc->channel + ctxnum; 316 sc->channel[ctxnum].hook = hook; 317 sc->datahooks++; 318 return (0); 319 } 320 321 /* 322 * Count up the size of the address header if we don't already know 323 */ 324 int 325 ngfrm_addrlen(char *hdr) 326 { 327 if (hdr[0] & BYTEX_EA) 328 return 0; 329 if (hdr[1] & BYTEX_EA) 330 return 2; 331 if (hdr[2] & BYTEX_EA) 332 return 3; 333 if (hdr[3] & BYTEX_EA) 334 return 4; 335 return 0; 336 } 337 338 /* 339 * Receive data packet 340 */ 341 static int 342 ngfrm_rcvdata(hook_p hook, struct mbuf *m, meta_p meta) 343 { 344 struct ctxinfo *const ctxp = hook->private; 345 int error = 0; 346 int dlci; 347 sc_p sc; 348 int alen; 349 char *data; 350 351 /* Data doesn't come in from just anywhere (e.g debug hook) */ 352 if (ctxp == NULL) { 353 error = ENETDOWN; 354 goto bad; 355 } 356 357 /* If coming from downstream, decode it to a channel */ 358 dlci = ctxp->dlci; 359 if (dlci == -1) 360 return (ngfrm_decode(hook->node, m, meta)); 361 362 /* Derive the softc we will need */ 363 sc = hook->node->private; 364 365 /* If there is no live channel, throw it away */ 366 if ((sc->downstream.hook == NULL) 367 || ((ctxp->flags & CHAN_ACTIVE) == 0)) { 368 error = ENETDOWN; 369 goto bad; 370 } 371 372 /* Store the DLCI on the front of the packet */ 373 alen = sc->addrlen; 374 if (alen == 0) 375 alen = 2; /* default value for transmit */ 376 M_PREPEND(m, alen, MB_DONTWAIT); 377 if (m == NULL) { 378 error = ENOBUFS; 379 goto bad; 380 } 381 data = mtod(m, char *); 382 383 /* 384 * Shift the lowest bits into the address field untill we are done. 385 * First byte is MSBits of addr so work backwards. 386 */ 387 switch (alen) { 388 case 2: 389 data[0] = data[1] = '\0'; 390 SHIFTOUT(makeup + 1, data[1], dlci); 391 SHIFTOUT(makeup + 0, data[0], dlci); 392 data[1] |= BYTEX_EA; 393 break; 394 case 3: 395 data[0] = data[1] = data[2] = '\0'; 396 SHIFTOUT(makeup + 3, data[2], dlci); /* 3 and 2 is correct */ 397 SHIFTOUT(makeup + 1, data[1], dlci); 398 SHIFTOUT(makeup + 0, data[0], dlci); 399 data[2] |= BYTEX_EA; 400 break; 401 case 4: 402 data[0] = data[1] = data[2] = data[3] = '\0'; 403 SHIFTOUT(makeup + 3, data[3], dlci); 404 SHIFTOUT(makeup + 2, data[2], dlci); 405 SHIFTOUT(makeup + 1, data[1], dlci); 406 SHIFTOUT(makeup + 0, data[0], dlci); 407 data[3] |= BYTEX_EA; 408 break; 409 default: 410 panic(__func__); 411 } 412 413 /* Send it */ 414 NG_SEND_DATA(error, sc->downstream.hook, m, meta); 415 return (error); 416 417 bad: 418 NG_FREE_DATA(m, meta); 419 return (error); 420 } 421 422 /* 423 * Decode an incoming frame coming from the switch 424 */ 425 static int 426 ngfrm_decode(node_p node, struct mbuf *m, meta_p meta) 427 { 428 const sc_p sc = node->private; 429 char *data; 430 int alen; 431 u_int dlci = 0; 432 int error = 0; 433 int ctxnum; 434 435 if (m->m_len < 4 && (m = m_pullup(m, 4)) == NULL) { 436 error = ENOBUFS; 437 goto out; 438 } 439 data = mtod(m, char *); 440 if ((alen = sc->addrlen) == 0) { 441 sc->addrlen = alen = ngfrm_addrlen(data); 442 } 443 switch (alen) { 444 case 2: 445 SHIFTIN(makeup + 0, data[0], dlci); 446 SHIFTIN(makeup + 1, data[1], dlci); 447 break; 448 case 3: 449 SHIFTIN(makeup + 0, data[0], dlci); 450 SHIFTIN(makeup + 1, data[1], dlci); 451 SHIFTIN(makeup + 3, data[2], dlci); /* 3 and 2 is correct */ 452 break; 453 case 4: 454 SHIFTIN(makeup + 0, data[0], dlci); 455 SHIFTIN(makeup + 1, data[1], dlci); 456 SHIFTIN(makeup + 2, data[2], dlci); 457 SHIFTIN(makeup + 3, data[3], dlci); 458 break; 459 default: 460 error = EINVAL; 461 goto out; 462 } 463 464 if (dlci > 1023) { 465 error = EINVAL; 466 goto out; 467 } 468 ctxnum = sc->ALT[dlci]; 469 if ((ctxnum & CTX_VALID) && sc->channel[ctxnum &= CTX_VALUE].hook) { 470 /* Send it */ 471 m_adj(m, alen); 472 NG_SEND_DATA(error, sc->channel[ctxnum].hook, m, meta); 473 return (error); 474 } else { 475 error = ENETDOWN; 476 } 477 out: 478 NG_FREE_DATA(m, meta); 479 return (error); 480 } 481 482 /* 483 * Shutdown node 484 */ 485 static int 486 ngfrm_rmnode(node_p node) 487 { 488 const sc_p sc = node->private; 489 490 node->flags |= NG_INVALID; 491 ng_cutlinks(node); 492 ng_unname(node); 493 node->private = NULL; 494 FREE(sc, M_NETGRAPH); 495 ng_unref(node); 496 return (0); 497 } 498 499 /* 500 * Hook disconnection 501 * 502 * Invalidate the private data associated with this dlci. 503 * For this type, removal of the last link resets tries to destroy the node. 504 */ 505 static int 506 ngfrm_disconnect(hook_p hook) 507 { 508 const sc_p sc = hook->node->private; 509 struct ctxinfo *const cp = hook->private; 510 int dlci; 511 512 /* If it's a regular dlci hook, then free resources etc.. */ 513 if (cp != NULL) { 514 cp->hook = NULL; 515 dlci = cp->dlci; 516 if (dlci != -1) 517 sc->ALT[dlci] = 0; 518 cp->flags = 0; 519 sc->datahooks--; 520 } 521 if (hook->node->numhooks == 0) 522 ng_rmnode(hook->node); 523 return (0); 524 } 525