1 /*- 2 * Copyright (c) 2000 Whistle Communications, Inc. 3 * All rights reserved. 4 * 5 * Subject to the following obligations and disclaimer of warranty, use and 6 * redistribution of this software, in source or object code forms, with or 7 * without modifications are expressly permitted by Whistle Communications; 8 * provided, however, that: 9 * 1. Any and all reproductions of the source or object code must include the 10 * copyright notice above and the following disclaimer of warranties; and 11 * 2. No rights are granted, in any manner or form, to use Whistle 12 * Communications, Inc. trademarks, including the mark "WHISTLE 13 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 14 * such appears in the above copyright notice or in the software. 15 * 16 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 17 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 18 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 19 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 21 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 22 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 23 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 24 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 25 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 26 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 27 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 28 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 32 * OF SUCH DAMAGE. 33 * 34 * Author: Archie Cobbs <archie@freebsd.org> 35 * 36 * $FreeBSD$ 37 */ 38 39 /* 40 * ng_bridge(4) netgraph node type 41 * 42 * The node performs standard intelligent Ethernet bridging over 43 * each of its connected hooks, or links. A simple loop detection 44 * algorithm is included which disables a link for priv->conf.loopTimeout 45 * seconds when a host is seen to have jumped from one link to 46 * another within priv->conf.minStableAge seconds. 47 * 48 * We keep a hashtable that maps Ethernet addresses to host info, 49 * which is contained in struct ng_bridge_host's. These structures 50 * tell us on which link the host may be found. A host's entry will 51 * expire after priv->conf.maxStaleness seconds. 52 * 53 * This node is optimzed for stable networks, where machines jump 54 * from one port to the other only rarely. 55 */ 56 57 #include <sys/param.h> 58 #include <sys/systm.h> 59 #include <sys/kernel.h> 60 #include <sys/lock.h> 61 #include <sys/malloc.h> 62 #include <sys/mbuf.h> 63 #include <sys/errno.h> 64 #include <sys/rwlock.h> 65 #include <sys/syslog.h> 66 #include <sys/socket.h> 67 #include <sys/ctype.h> 68 #include <sys/types.h> 69 #include <sys/counter.h> 70 71 #include <net/if.h> 72 #include <net/if_var.h> 73 #include <net/ethernet.h> 74 #include <net/vnet.h> 75 76 #include <netinet/in.h> 77 #if 0 /* not used yet */ 78 #include <netinet/ip_fw.h> 79 #endif 80 #include <netgraph/ng_message.h> 81 #include <netgraph/netgraph.h> 82 #include <netgraph/ng_parse.h> 83 #include <netgraph/ng_bridge.h> 84 85 #ifdef NG_SEPARATE_MALLOC 86 static MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge", 87 "netgraph bridge node"); 88 #else 89 #define M_NETGRAPH_BRIDGE M_NETGRAPH 90 #endif 91 92 /* Counter based stats */ 93 struct ng_bridge_link_kernel_stats { 94 counter_u64_t recvOctets; /* total octets rec'd on link */ 95 counter_u64_t recvPackets; /* total pkts rec'd on link */ 96 counter_u64_t recvMulticasts; /* multicast pkts rec'd on link */ 97 counter_u64_t recvBroadcasts; /* broadcast pkts rec'd on link */ 98 counter_u64_t recvUnknown; /* pkts rec'd with unknown dest addr */ 99 counter_u64_t recvRunts; /* pkts rec'd less than 14 bytes */ 100 counter_u64_t recvInvalid; /* pkts rec'd with bogus source addr */ 101 counter_u64_t xmitOctets; /* total octets xmit'd on link */ 102 counter_u64_t xmitPackets; /* total pkts xmit'd on link */ 103 counter_u64_t xmitMulticasts; /* multicast pkts xmit'd on link */ 104 counter_u64_t xmitBroadcasts; /* broadcast pkts xmit'd on link */ 105 counter_u64_t loopDrops; /* pkts dropped due to loopback */ 106 counter_u64_t loopDetects; /* number of loop detections */ 107 counter_u64_t memoryFailures; /* times couldn't get mem or mbuf */ 108 }; 109 110 /* Per-link private data */ 111 struct ng_bridge_link { 112 hook_p hook; /* netgraph hook */ 113 u_int16_t loopCount; /* loop ignore timer */ 114 unsigned int learnMac : 1, /* autolearn macs */ 115 sendUnknown : 1;/* send unknown macs out */ 116 struct ng_bridge_link_kernel_stats stats; /* link stats */ 117 }; 118 119 /* Per-node private data */ 120 struct ng_bridge_private { 121 struct ng_bridge_bucket *tab; /* hash table bucket array */ 122 struct ng_bridge_config conf; /* node configuration */ 123 node_p node; /* netgraph node */ 124 u_int numHosts; /* num entries in table */ 125 u_int numBuckets; /* num buckets in table */ 126 u_int hashMask; /* numBuckets - 1 */ 127 int numLinks; /* num connected links */ 128 unsigned int persistent : 1, /* can exist w/o hooks */ 129 sendUnknown : 1;/* links receive unknowns by default */ 130 struct callout timer; /* one second periodic timer */ 131 }; 132 typedef struct ng_bridge_private *priv_p; 133 134 /* Information about a host, stored in a hash table entry */ 135 struct ng_bridge_hent { 136 struct ng_bridge_host host; /* actual host info */ 137 SLIST_ENTRY(ng_bridge_hent) next; /* next entry in bucket */ 138 }; 139 140 /* Hash table bucket declaration */ 141 SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent); 142 143 /* Netgraph node methods */ 144 static ng_constructor_t ng_bridge_constructor; 145 static ng_rcvmsg_t ng_bridge_rcvmsg; 146 static ng_shutdown_t ng_bridge_shutdown; 147 static ng_newhook_t ng_bridge_newhook; 148 static ng_rcvdata_t ng_bridge_rcvdata; 149 static ng_disconnect_t ng_bridge_disconnect; 150 151 /* Other internal functions */ 152 static struct ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr); 153 static int ng_bridge_put(priv_p priv, const u_char *addr, link_p link); 154 static void ng_bridge_rehash(priv_p priv); 155 static void ng_bridge_remove_hosts(priv_p priv, link_p link); 156 static void ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2); 157 static const char *ng_bridge_nodename(node_p node); 158 159 /* Ethernet broadcast */ 160 static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] = 161 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 162 163 /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */ 164 #define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \ 165 == ((const u_int32_t *)(b))[0] \ 166 && ((const u_int16_t *)(a))[2] \ 167 == ((const u_int16_t *)(b))[2]) 168 169 /* Minimum and maximum number of hash buckets. Must be a power of two. */ 170 #define MIN_BUCKETS (1 << 5) /* 32 */ 171 #define MAX_BUCKETS (1 << 14) /* 16384 */ 172 173 /* Configuration default values */ 174 #define DEFAULT_LOOP_TIMEOUT 60 175 #define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */ 176 #define DEFAULT_MIN_STABLE_AGE 1 177 178 /****************************************************************** 179 NETGRAPH PARSE TYPES 180 ******************************************************************/ 181 182 /* 183 * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE 184 */ 185 static int 186 ng_bridge_getTableLength(const struct ng_parse_type *type, 187 const u_char *start, const u_char *buf) 188 { 189 const struct ng_bridge_host_ary *const hary 190 = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t)); 191 192 return hary->numHosts; 193 } 194 195 /* Parse type for struct ng_bridge_host_ary */ 196 static const struct ng_parse_struct_field ng_bridge_host_type_fields[] 197 = NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type); 198 static const struct ng_parse_type ng_bridge_host_type = { 199 &ng_parse_struct_type, 200 &ng_bridge_host_type_fields 201 }; 202 static const struct ng_parse_array_info ng_bridge_hary_type_info = { 203 &ng_bridge_host_type, 204 ng_bridge_getTableLength 205 }; 206 static const struct ng_parse_type ng_bridge_hary_type = { 207 &ng_parse_array_type, 208 &ng_bridge_hary_type_info 209 }; 210 static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[] 211 = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type); 212 static const struct ng_parse_type ng_bridge_host_ary_type = { 213 &ng_parse_struct_type, 214 &ng_bridge_host_ary_type_fields 215 }; 216 217 /* Parse type for struct ng_bridge_config */ 218 static const struct ng_parse_struct_field ng_bridge_config_type_fields[] 219 = NG_BRIDGE_CONFIG_TYPE_INFO; 220 static const struct ng_parse_type ng_bridge_config_type = { 221 &ng_parse_struct_type, 222 &ng_bridge_config_type_fields 223 }; 224 225 /* Parse type for struct ng_bridge_link_stat */ 226 static const struct ng_parse_struct_field ng_bridge_stats_type_fields[] 227 = NG_BRIDGE_STATS_TYPE_INFO; 228 static const struct ng_parse_type ng_bridge_stats_type = { 229 &ng_parse_struct_type, 230 &ng_bridge_stats_type_fields 231 }; 232 233 /* List of commands and how to convert arguments to/from ASCII */ 234 static const struct ng_cmdlist ng_bridge_cmdlist[] = { 235 { 236 NGM_BRIDGE_COOKIE, 237 NGM_BRIDGE_SET_CONFIG, 238 "setconfig", 239 &ng_bridge_config_type, 240 NULL 241 }, 242 { 243 NGM_BRIDGE_COOKIE, 244 NGM_BRIDGE_GET_CONFIG, 245 "getconfig", 246 NULL, 247 &ng_bridge_config_type 248 }, 249 { 250 NGM_BRIDGE_COOKIE, 251 NGM_BRIDGE_RESET, 252 "reset", 253 NULL, 254 NULL 255 }, 256 { 257 NGM_BRIDGE_COOKIE, 258 NGM_BRIDGE_GET_STATS, 259 "getstats", 260 &ng_parse_uint32_type, 261 &ng_bridge_stats_type 262 }, 263 { 264 NGM_BRIDGE_COOKIE, 265 NGM_BRIDGE_CLR_STATS, 266 "clrstats", 267 &ng_parse_uint32_type, 268 NULL 269 }, 270 { 271 NGM_BRIDGE_COOKIE, 272 NGM_BRIDGE_GETCLR_STATS, 273 "getclrstats", 274 &ng_parse_uint32_type, 275 &ng_bridge_stats_type 276 }, 277 { 278 NGM_BRIDGE_COOKIE, 279 NGM_BRIDGE_GET_TABLE, 280 "gettable", 281 NULL, 282 &ng_bridge_host_ary_type 283 }, 284 { 285 NGM_BRIDGE_COOKIE, 286 NGM_BRIDGE_SET_PERSISTENT, 287 "setpersistent", 288 NULL, 289 NULL 290 }, 291 { 0 } 292 }; 293 294 /* Node type descriptor */ 295 static struct ng_type ng_bridge_typestruct = { 296 .version = NG_ABI_VERSION, 297 .name = NG_BRIDGE_NODE_TYPE, 298 .constructor = ng_bridge_constructor, 299 .rcvmsg = ng_bridge_rcvmsg, 300 .shutdown = ng_bridge_shutdown, 301 .newhook = ng_bridge_newhook, 302 .rcvdata = ng_bridge_rcvdata, 303 .disconnect = ng_bridge_disconnect, 304 .cmdlist = ng_bridge_cmdlist, 305 }; 306 NETGRAPH_INIT(bridge, &ng_bridge_typestruct); 307 308 /****************************************************************** 309 NETGRAPH NODE METHODS 310 ******************************************************************/ 311 312 /* 313 * Node constructor 314 */ 315 static int 316 ng_bridge_constructor(node_p node) 317 { 318 priv_p priv; 319 320 /* Allocate and initialize private info */ 321 priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO); 322 ng_callout_init(&priv->timer); 323 324 /* Allocate and initialize hash table, etc. */ 325 priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab), 326 M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO); 327 priv->numBuckets = MIN_BUCKETS; 328 priv->hashMask = MIN_BUCKETS - 1; 329 priv->conf.debugLevel = 1; 330 priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT; 331 priv->conf.maxStaleness = DEFAULT_MAX_STALENESS; 332 priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE; 333 priv->sendUnknown = 1; /* classic bridge */ 334 335 /* 336 * This node has all kinds of stuff that could be screwed by SMP. 337 * Until it gets it's own internal protection, we go through in 338 * single file. This could hurt a machine bridging between two 339 * GB ethernets so it should be fixed. 340 * When it's fixed the process SHOULD NOT SLEEP, spinlocks please! 341 * (and atomic ops ) 342 */ 343 NG_NODE_FORCE_WRITER(node); 344 NG_NODE_SET_PRIVATE(node, priv); 345 priv->node = node; 346 347 /* Start timer; timer is always running while node is alive */ 348 ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 349 350 /* Done */ 351 return (0); 352 } 353 354 /* 355 * Method for attaching a new hook 356 */ 357 static int 358 ng_bridge_newhook(node_p node, hook_p hook, const char *name) 359 { 360 const priv_p priv = NG_NODE_PRIVATE(node); 361 char linkName[NG_HOOKSIZ]; 362 u_int32_t linkNum; 363 link_p link; 364 const char *prefix = NG_BRIDGE_HOOK_LINK_PREFIX; 365 bool isUplink; 366 367 /* Check for a link hook */ 368 if (strlen(name) <= strlen(prefix)) 369 return (EINVAL); /* Unknown hook name */ 370 371 isUplink = (name[0] == 'u'); 372 if (isUplink) 373 prefix = NG_BRIDGE_HOOK_UPLINK_PREFIX; 374 375 /* primitive parsing */ 376 linkNum = strtoul(name + strlen(prefix), NULL, 10); 377 /* validation by comparing against the reconstucted name */ 378 snprintf(linkName, sizeof(linkName), "%s%u", prefix, linkNum); 379 if (strcmp(linkName, name) != 0) 380 return (EINVAL); 381 382 if (linkNum == 0 && isUplink) 383 return (EINVAL); 384 385 if(NG_PEER_NODE(hook) == node) 386 return (ELOOP); 387 388 link = malloc(sizeof(*link), M_NETGRAPH_BRIDGE, M_ZERO); 389 if (link == NULL) 390 return (ENOMEM); 391 392 link->stats.recvOctets = counter_u64_alloc(M_WAITOK); 393 link->stats.recvPackets = counter_u64_alloc(M_WAITOK); 394 link->stats.recvMulticasts = counter_u64_alloc(M_WAITOK); 395 link->stats.recvBroadcasts = counter_u64_alloc(M_WAITOK); 396 link->stats.recvUnknown = counter_u64_alloc(M_WAITOK); 397 link->stats.recvRunts = counter_u64_alloc(M_WAITOK); 398 link->stats.recvInvalid = counter_u64_alloc(M_WAITOK); 399 link->stats.xmitOctets = counter_u64_alloc(M_WAITOK); 400 link->stats.xmitPackets = counter_u64_alloc(M_WAITOK); 401 link->stats.xmitMulticasts = counter_u64_alloc(M_WAITOK); 402 link->stats.xmitBroadcasts = counter_u64_alloc(M_WAITOK); 403 link->stats.loopDrops = counter_u64_alloc(M_WAITOK); 404 link->stats.loopDetects = counter_u64_alloc(M_WAITOK); 405 link->stats.memoryFailures = counter_u64_alloc(M_WAITOK); 406 407 link->hook = hook; 408 if (isUplink) { 409 link->learnMac = 0; 410 link->sendUnknown = 1; 411 if (priv->numLinks == 0) /* if the first link is an uplink */ 412 priv->sendUnknown = 0; /* switch to restrictive mode */ 413 } else { 414 link->learnMac = 1; 415 link->sendUnknown = priv->sendUnknown; 416 } 417 418 NG_HOOK_SET_PRIVATE(hook, link); 419 priv->numLinks++; 420 return (0); 421 } 422 423 /* 424 * Receive a control message 425 */ 426 static void ng_bridge_clear_link_stats(struct ng_bridge_link_kernel_stats * p) 427 { 428 counter_u64_zero(p->recvOctets); 429 counter_u64_zero(p->recvPackets); 430 counter_u64_zero(p->recvMulticasts); 431 counter_u64_zero(p->recvBroadcasts); 432 counter_u64_zero(p->recvUnknown); 433 counter_u64_zero(p->recvRunts); 434 counter_u64_zero(p->recvInvalid); 435 counter_u64_zero(p->xmitOctets); 436 counter_u64_zero(p->xmitPackets); 437 counter_u64_zero(p->xmitMulticasts); 438 counter_u64_zero(p->xmitBroadcasts); 439 counter_u64_zero(p->loopDrops); 440 counter_u64_zero(p->loopDetects); 441 counter_u64_zero(p->memoryFailures); 442 }; 443 444 static int 445 ng_bridge_reset_link(hook_p hook, void *arg __unused) 446 { 447 link_p priv = NG_HOOK_PRIVATE(hook); 448 449 priv->loopCount = 0; 450 ng_bridge_clear_link_stats(&priv->stats); 451 return (1); 452 } 453 454 static int 455 ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook) 456 { 457 const priv_p priv = NG_NODE_PRIVATE(node); 458 struct ng_mesg *resp = NULL; 459 int error = 0; 460 struct ng_mesg *msg; 461 462 NGI_GET_MSG(item, msg); 463 switch (msg->header.typecookie) { 464 case NGM_BRIDGE_COOKIE: 465 switch (msg->header.cmd) { 466 case NGM_BRIDGE_GET_CONFIG: 467 { 468 struct ng_bridge_config *conf; 469 470 NG_MKRESPONSE(resp, msg, 471 sizeof(struct ng_bridge_config), M_NOWAIT); 472 if (resp == NULL) { 473 error = ENOMEM; 474 break; 475 } 476 conf = (struct ng_bridge_config *)resp->data; 477 *conf = priv->conf; /* no sanity checking needed */ 478 break; 479 } 480 case NGM_BRIDGE_SET_CONFIG: 481 { 482 struct ng_bridge_config *conf; 483 484 if (msg->header.arglen 485 != sizeof(struct ng_bridge_config)) { 486 error = EINVAL; 487 break; 488 } 489 conf = (struct ng_bridge_config *)msg->data; 490 priv->conf = *conf; 491 break; 492 } 493 case NGM_BRIDGE_RESET: 494 { 495 hook_p rethook; 496 497 /* Flush all entries in the hash table */ 498 ng_bridge_remove_hosts(priv, NULL); 499 500 /* Reset all loop detection counters and stats */ 501 NG_NODE_FOREACH_HOOK(node, ng_bridge_reset_link, NULL, 502 rethook); 503 break; 504 } 505 case NGM_BRIDGE_GET_STATS: 506 case NGM_BRIDGE_CLR_STATS: 507 case NGM_BRIDGE_GETCLR_STATS: 508 { 509 hook_p hook; 510 link_p link; 511 char linkName[NG_HOOKSIZ]; 512 int linkNum; 513 514 /* Get link number */ 515 if (msg->header.arglen != sizeof(u_int32_t)) { 516 error = EINVAL; 517 break; 518 } 519 linkNum = *((int32_t *)msg->data); 520 if (linkNum < 0) 521 snprintf(linkName, sizeof(linkName), 522 "%s%u", NG_BRIDGE_HOOK_UPLINK_PREFIX, -linkNum); 523 else 524 snprintf(linkName, sizeof(linkName), 525 "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX, linkNum); 526 527 if ((hook = ng_findhook(node, linkName)) == NULL) { 528 error = ENOTCONN; 529 break; 530 } 531 link = NG_HOOK_PRIVATE(hook); 532 533 /* Get/clear stats */ 534 if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) { 535 struct ng_bridge_link_stats *rs; 536 537 NG_MKRESPONSE(resp, msg, 538 sizeof(link->stats), M_NOWAIT); 539 if (resp == NULL) { 540 error = ENOMEM; 541 break; 542 } 543 rs = (struct ng_bridge_link_stats *)resp->data; 544 #define FETCH(x) rs->x = counter_u64_fetch(link->stats.x) 545 FETCH(recvOctets); 546 FETCH(recvPackets); 547 FETCH(recvMulticasts); 548 FETCH(recvBroadcasts); 549 FETCH(recvUnknown); 550 FETCH(recvRunts); 551 FETCH(recvInvalid); 552 FETCH(xmitOctets); 553 FETCH(xmitPackets); 554 FETCH(xmitMulticasts); 555 FETCH(xmitBroadcasts); 556 FETCH(loopDrops); 557 FETCH(loopDetects); 558 FETCH(memoryFailures); 559 #undef FETCH 560 } 561 if (msg->header.cmd != NGM_BRIDGE_GET_STATS) 562 ng_bridge_clear_link_stats(&link->stats); 563 break; 564 } 565 case NGM_BRIDGE_GET_TABLE: 566 { 567 struct ng_bridge_host_ary *ary; 568 struct ng_bridge_hent *hent; 569 int i = 0, bucket; 570 571 NG_MKRESPONSE(resp, msg, sizeof(*ary) 572 + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT); 573 if (resp == NULL) { 574 error = ENOMEM; 575 break; 576 } 577 ary = (struct ng_bridge_host_ary *)resp->data; 578 ary->numHosts = priv->numHosts; 579 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 580 SLIST_FOREACH(hent, &priv->tab[bucket], next) { 581 memcpy(ary->hosts[i].addr, 582 hent->host.addr, 583 sizeof(ary->hosts[i].addr)); 584 ary->hosts[i].age = hent->host.age; 585 ary->hosts[i].staleness = hent->host.staleness; 586 strncpy(ary->hosts[i].hook, 587 NG_HOOK_NAME(hent->host.link->hook), 588 sizeof(ary->hosts[i].hook)); 589 i++; 590 } 591 } 592 break; 593 } 594 case NGM_BRIDGE_SET_PERSISTENT: 595 { 596 priv->persistent = 1; 597 break; 598 } 599 default: 600 error = EINVAL; 601 break; 602 } 603 break; 604 default: 605 error = EINVAL; 606 break; 607 } 608 609 /* Done */ 610 NG_RESPOND_MSG(error, node, item, resp); 611 NG_FREE_MSG(msg); 612 return (error); 613 } 614 615 /* 616 * Receive data on a hook 617 */ 618 struct ng_bridge_send_ctx { 619 link_p foundFirst, incoming; 620 struct mbuf * m; 621 int manycast, error; 622 }; 623 624 static int 625 ng_bridge_send_ctx(hook_p dst, void *arg) 626 { 627 struct ng_bridge_send_ctx *ctx = arg; 628 link_p destLink = NG_HOOK_PRIVATE(dst); 629 struct mbuf *m2 = NULL; 630 int error = 0; 631 632 /* Skip incoming link */ 633 if (destLink == ctx->incoming) { 634 return (1); 635 } 636 637 /* Skip sending unknowns to undesired links */ 638 if (!ctx->manycast && !destLink->sendUnknown) 639 return (1); 640 641 if (ctx->foundFirst == NULL) { 642 /* 643 * This is the first usable link we have found. 644 * Reserve it for the originals. 645 * If we never find another we save a copy. 646 */ 647 ctx->foundFirst = destLink; 648 return (1); 649 } 650 651 /* 652 * It's usable link but not the reserved (first) one. 653 * Copy mbuf info for sending. 654 */ 655 m2 = m_dup(ctx->m, M_NOWAIT); /* XXX m_copypacket() */ 656 if (m2 == NULL) { 657 counter_u64_add(ctx->incoming->stats.memoryFailures, 1); 658 ctx->error = ENOBUFS; 659 return (0); /* abort loop */ 660 } 661 662 /* Update stats */ 663 counter_u64_add(destLink->stats.xmitPackets, 1); 664 counter_u64_add(destLink->stats.xmitOctets, m2->m_pkthdr.len); 665 switch (ctx->manycast) { 666 default: /* unknown unicast */ 667 break; 668 case 1: /* multicast */ 669 counter_u64_add(destLink->stats.xmitMulticasts, 1); 670 break; 671 case 2: /* broadcast */ 672 counter_u64_add(destLink->stats.xmitBroadcasts, 1); 673 break; 674 } 675 676 /* Send packet */ 677 NG_SEND_DATA_ONLY(error, destLink->hook, m2); 678 if(error) 679 ctx->error = error; 680 return (1); 681 } 682 683 static int 684 ng_bridge_rcvdata(hook_p hook, item_p item) 685 { 686 const node_p node = NG_HOOK_NODE(hook); 687 const priv_p priv = NG_NODE_PRIVATE(node); 688 struct ng_bridge_host *host; 689 struct ether_header *eh; 690 struct ng_bridge_send_ctx ctx = { 0 }; 691 hook_p ret; 692 693 NGI_GET_M(item, ctx.m); 694 695 ctx.incoming = NG_HOOK_PRIVATE(hook); 696 /* Sanity check packet and pull up header */ 697 if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) { 698 counter_u64_add(ctx.incoming->stats.recvRunts, 1); 699 NG_FREE_ITEM(item); 700 NG_FREE_M(ctx.m); 701 return (EINVAL); 702 } 703 if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) { 704 counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 705 NG_FREE_ITEM(item); 706 return (ENOBUFS); 707 } 708 eh = mtod(ctx.m, struct ether_header *); 709 if ((eh->ether_shost[0] & 1) != 0) { 710 counter_u64_add(ctx.incoming->stats.recvInvalid, 1); 711 NG_FREE_ITEM(item); 712 NG_FREE_M(ctx.m); 713 return (EINVAL); 714 } 715 716 /* Is link disabled due to a loopback condition? */ 717 if (ctx.incoming->loopCount != 0) { 718 counter_u64_add(ctx.incoming->stats.loopDrops, 1); 719 NG_FREE_ITEM(item); 720 NG_FREE_M(ctx.m); 721 return (ELOOP); /* XXX is this an appropriate error? */ 722 } 723 724 /* Update stats */ 725 counter_u64_add(ctx.incoming->stats.recvPackets, 1); 726 counter_u64_add(ctx.incoming->stats.recvOctets, ctx.m->m_pkthdr.len); 727 if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) { 728 if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) { 729 counter_u64_add(ctx.incoming->stats.recvBroadcasts, 1); 730 ctx.manycast = 2; 731 } else 732 counter_u64_add(ctx.incoming->stats.recvMulticasts, 1); 733 } 734 735 /* Look up packet's source Ethernet address in hashtable */ 736 if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) { 737 /* Update time since last heard from this host */ 738 host->staleness = 0; 739 740 /* Did host jump to a different link? */ 741 if (host->link != ctx.incoming) { 742 /* 743 * If the host's old link was recently established 744 * on the old link and it's already jumped to a new 745 * link, declare a loopback condition. 746 */ 747 if (host->age < priv->conf.minStableAge) { 748 /* Log the problem */ 749 if (priv->conf.debugLevel >= 2) { 750 struct ifnet *ifp = ctx.m->m_pkthdr.rcvif; 751 char suffix[32]; 752 753 if (ifp != NULL) 754 snprintf(suffix, sizeof(suffix), 755 " (%s)", ifp->if_xname); 756 else 757 *suffix = '\0'; 758 log(LOG_WARNING, "ng_bridge: %s:" 759 " loopback detected on %s%s\n", 760 ng_bridge_nodename(node), 761 NG_HOOK_NAME(hook), suffix); 762 } 763 764 /* Mark link as linka non grata */ 765 ctx.incoming->loopCount = priv->conf.loopTimeout; 766 counter_u64_add(ctx.incoming->stats.loopDetects, 1); 767 768 /* Forget all hosts on this link */ 769 ng_bridge_remove_hosts(priv, ctx.incoming); 770 771 /* Drop packet */ 772 counter_u64_add(ctx.incoming->stats.loopDrops, 1); 773 NG_FREE_ITEM(item); 774 NG_FREE_M(ctx.m); 775 return (ELOOP); /* XXX appropriate? */ 776 } 777 778 /* Move host over to new link */ 779 host->link = ctx.incoming; 780 host->age = 0; 781 } 782 } else if (ctx.incoming->learnMac) { 783 if (!ng_bridge_put(priv, eh->ether_shost, ctx.incoming)) { 784 counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 785 NG_FREE_ITEM(item); 786 NG_FREE_M(ctx.m); 787 return (ENOMEM); 788 } 789 } 790 791 /* Run packet through ipfw processing, if enabled */ 792 #if 0 793 if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) { 794 /* XXX not implemented yet */ 795 } 796 #endif 797 798 /* 799 * If unicast and destination host known, deliver to host's link, 800 * unless it is the same link as the packet came in on. 801 */ 802 if (!ctx.manycast) { 803 /* Determine packet destination link */ 804 if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) { 805 link_p destLink = host->link; 806 807 /* If destination same as incoming link, do nothing */ 808 if (destLink == ctx.incoming) { 809 NG_FREE_ITEM(item); 810 NG_FREE_M(ctx.m); 811 return (0); 812 } 813 814 /* Deliver packet out the destination link */ 815 counter_u64_add(destLink->stats.xmitPackets, 1); 816 counter_u64_add(destLink->stats.xmitOctets, ctx.m->m_pkthdr.len); 817 NG_FWD_NEW_DATA(ctx.error, item, destLink->hook, ctx.m); 818 return (ctx.error); 819 } 820 821 /* Destination host is not known */ 822 counter_u64_add(ctx.incoming->stats.recvUnknown, 1); 823 } 824 825 /* Distribute unknown, multicast, broadcast pkts to all other links */ 826 NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx, ret); 827 828 /* If we never saw a good link, leave. */ 829 if (ctx.foundFirst == NULL || ctx.error != 0) { 830 NG_FREE_ITEM(item); 831 NG_FREE_M(ctx.m); 832 return (ctx.error); 833 } 834 835 /* 836 * If we've sent all the others, send the original 837 * on the first link we found. 838 */ 839 NG_FWD_NEW_DATA(ctx.error, item, ctx.foundFirst->hook, ctx.m); 840 return (ctx.error); 841 } 842 843 /* 844 * Shutdown node 845 */ 846 static int 847 ng_bridge_shutdown(node_p node) 848 { 849 const priv_p priv = NG_NODE_PRIVATE(node); 850 851 /* 852 * Shut down everything including the timer. Even if the 853 * callout has already been dequeued and is about to be 854 * run, ng_bridge_timeout() won't be fired as the node 855 * is already marked NGF_INVALID, so we're safe to free 856 * the node now. 857 */ 858 KASSERT(priv->numLinks == 0 && priv->numHosts == 0, 859 ("%s: numLinks=%d numHosts=%d", 860 __func__, priv->numLinks, priv->numHosts)); 861 ng_uncallout(&priv->timer, node); 862 NG_NODE_SET_PRIVATE(node, NULL); 863 NG_NODE_UNREF(node); 864 free(priv->tab, M_NETGRAPH_BRIDGE); 865 free(priv, M_NETGRAPH_BRIDGE); 866 return (0); 867 } 868 869 /* 870 * Hook disconnection. 871 */ 872 static int 873 ng_bridge_disconnect(hook_p hook) 874 { 875 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 876 link_p link = NG_HOOK_PRIVATE(hook); 877 878 /* Remove all hosts associated with this link */ 879 ng_bridge_remove_hosts(priv, link); 880 881 /* Free associated link information */ 882 counter_u64_free(link->stats.recvOctets); 883 counter_u64_free(link->stats.recvPackets); 884 counter_u64_free(link->stats.recvMulticasts); 885 counter_u64_free(link->stats.recvBroadcasts); 886 counter_u64_free(link->stats.recvUnknown); 887 counter_u64_free(link->stats.recvRunts); 888 counter_u64_free(link->stats.recvInvalid); 889 counter_u64_free(link->stats.xmitOctets); 890 counter_u64_free(link->stats.xmitPackets); 891 counter_u64_free(link->stats.xmitMulticasts); 892 counter_u64_free(link->stats.xmitBroadcasts); 893 counter_u64_free(link->stats.loopDrops); 894 counter_u64_free(link->stats.loopDetects); 895 counter_u64_free(link->stats.memoryFailures); 896 free(link, M_NETGRAPH_BRIDGE); 897 priv->numLinks--; 898 899 /* If no more hooks, go away */ 900 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 901 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) 902 && !priv->persistent) { 903 ng_rmnode_self(NG_HOOK_NODE(hook)); 904 } 905 return (0); 906 } 907 908 /****************************************************************** 909 HASH TABLE FUNCTIONS 910 ******************************************************************/ 911 912 /* 913 * Hash algorithm 914 */ 915 #define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \ 916 ^ ((const u_int16_t *)(addr))[1] \ 917 ^ ((const u_int16_t *)(addr))[2]) & (mask) ) 918 919 /* 920 * Find a host entry in the table. 921 */ 922 static struct ng_bridge_host * 923 ng_bridge_get(priv_p priv, const u_char *addr) 924 { 925 const int bucket = HASH(addr, priv->hashMask); 926 struct ng_bridge_hent *hent; 927 928 SLIST_FOREACH(hent, &priv->tab[bucket], next) { 929 if (ETHER_EQUAL(hent->host.addr, addr)) 930 return (&hent->host); 931 } 932 return (NULL); 933 } 934 935 /* 936 * Add a new host entry to the table. This assumes the host doesn't 937 * already exist in the table. Returns 1 on success, 0 if there 938 * was a memory allocation failure. 939 */ 940 static int 941 ng_bridge_put(priv_p priv, const u_char *addr, link_p link) 942 { 943 const int bucket = HASH(addr, priv->hashMask); 944 struct ng_bridge_hent *hent; 945 946 #ifdef INVARIANTS 947 /* Assert that entry does not already exist in hashtable */ 948 SLIST_FOREACH(hent, &priv->tab[bucket], next) { 949 KASSERT(!ETHER_EQUAL(hent->host.addr, addr), 950 ("%s: entry %6D exists in table", __func__, addr, ":")); 951 } 952 #endif 953 954 /* Allocate and initialize new hashtable entry */ 955 hent = malloc(sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT); 956 if (hent == NULL) 957 return (0); 958 bcopy(addr, hent->host.addr, ETHER_ADDR_LEN); 959 hent->host.link = link; 960 hent->host.staleness = 0; 961 hent->host.age = 0; 962 963 /* Add new element to hash bucket */ 964 SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next); 965 priv->numHosts++; 966 967 /* Resize table if necessary */ 968 ng_bridge_rehash(priv); 969 return (1); 970 } 971 972 /* 973 * Resize the hash table. We try to maintain the number of buckets 974 * such that the load factor is in the range 0.25 to 1.0. 975 * 976 * If we can't get the new memory then we silently fail. This is OK 977 * because things will still work and we'll try again soon anyway. 978 */ 979 static void 980 ng_bridge_rehash(priv_p priv) 981 { 982 struct ng_bridge_bucket *newTab; 983 int oldBucket, newBucket; 984 int newNumBuckets; 985 u_int newMask; 986 987 /* Is table too full or too empty? */ 988 if (priv->numHosts > priv->numBuckets 989 && (priv->numBuckets << 1) <= MAX_BUCKETS) 990 newNumBuckets = priv->numBuckets << 1; 991 else if (priv->numHosts < (priv->numBuckets >> 2) 992 && (priv->numBuckets >> 2) >= MIN_BUCKETS) 993 newNumBuckets = priv->numBuckets >> 2; 994 else 995 return; 996 newMask = newNumBuckets - 1; 997 998 /* Allocate and initialize new table */ 999 newTab = malloc(newNumBuckets * sizeof(*newTab), 1000 M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 1001 if (newTab == NULL) 1002 return; 1003 1004 /* Move all entries from old table to new table */ 1005 for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) { 1006 struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket]; 1007 1008 while (!SLIST_EMPTY(oldList)) { 1009 struct ng_bridge_hent *const hent 1010 = SLIST_FIRST(oldList); 1011 1012 SLIST_REMOVE_HEAD(oldList, next); 1013 newBucket = HASH(hent->host.addr, newMask); 1014 SLIST_INSERT_HEAD(&newTab[newBucket], hent, next); 1015 } 1016 } 1017 1018 /* Replace old table with new one */ 1019 if (priv->conf.debugLevel >= 3) { 1020 log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n", 1021 ng_bridge_nodename(priv->node), 1022 priv->numBuckets, newNumBuckets); 1023 } 1024 free(priv->tab, M_NETGRAPH_BRIDGE); 1025 priv->numBuckets = newNumBuckets; 1026 priv->hashMask = newMask; 1027 priv->tab = newTab; 1028 return; 1029 } 1030 1031 /****************************************************************** 1032 MISC FUNCTIONS 1033 ******************************************************************/ 1034 1035 /* 1036 * Remove all hosts associated with a specific link from the hashtable. 1037 * If linkNum == -1, then remove all hosts in the table. 1038 */ 1039 static void 1040 ng_bridge_remove_hosts(priv_p priv, link_p link) 1041 { 1042 int bucket; 1043 1044 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 1045 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 1046 1047 while (*hptr != NULL) { 1048 struct ng_bridge_hent *const hent = *hptr; 1049 1050 if (link == NULL || hent->host.link == link) { 1051 *hptr = SLIST_NEXT(hent, next); 1052 free(hent, M_NETGRAPH_BRIDGE); 1053 priv->numHosts--; 1054 } else 1055 hptr = &SLIST_NEXT(hent, next); 1056 } 1057 } 1058 } 1059 1060 /* 1061 * Handle our once-per-second timeout event. We do two things: 1062 * we decrement link->loopCount for those links being muted due to 1063 * a detected loopback condition, and we remove any hosts from 1064 * the hashtable whom we haven't heard from in a long while. 1065 */ 1066 static int 1067 ng_bridge_unmute(hook_p hook, void *arg) 1068 { 1069 link_p link = NG_HOOK_PRIVATE(hook); 1070 node_p node = NG_HOOK_NODE(hook); 1071 priv_p priv = NG_NODE_PRIVATE(node); 1072 int *counter = arg; 1073 1074 if (link->loopCount != 0) { 1075 link->loopCount--; 1076 if (link->loopCount == 0 && priv->conf.debugLevel >= 2) { 1077 log(LOG_INFO, "ng_bridge: %s:" 1078 " restoring looped back %s\n", 1079 ng_bridge_nodename(node), NG_HOOK_NAME(hook)); 1080 } 1081 } 1082 (*counter)++; 1083 return (1); 1084 } 1085 1086 static void 1087 ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2) 1088 { 1089 const priv_p priv = NG_NODE_PRIVATE(node); 1090 int bucket; 1091 int counter = 0; 1092 hook_p ret; 1093 1094 /* Update host time counters and remove stale entries */ 1095 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 1096 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 1097 1098 while (*hptr != NULL) { 1099 struct ng_bridge_hent *const hent = *hptr; 1100 1101 /* Remove hosts we haven't heard from in a while */ 1102 if (++hent->host.staleness >= priv->conf.maxStaleness) { 1103 *hptr = SLIST_NEXT(hent, next); 1104 free(hent, M_NETGRAPH_BRIDGE); 1105 priv->numHosts--; 1106 } else { 1107 if (hent->host.age < 0xffff) 1108 hent->host.age++; 1109 hptr = &SLIST_NEXT(hent, next); 1110 counter++; 1111 } 1112 } 1113 } 1114 KASSERT(priv->numHosts == counter, 1115 ("%s: hosts: %d != %d", __func__, priv->numHosts, counter)); 1116 1117 /* Decrease table size if necessary */ 1118 ng_bridge_rehash(priv); 1119 1120 /* Decrease loop counter on muted looped back links */ 1121 counter = 0; 1122 NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter, ret); 1123 KASSERT(priv->numLinks == counter, 1124 ("%s: links: %d != %d", __func__, priv->numLinks, counter)); 1125 1126 /* Register a new timeout, keeping the existing node reference */ 1127 ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 1128 } 1129 1130 /* 1131 * Return node's "name", even if it doesn't have one. 1132 */ 1133 static const char * 1134 ng_bridge_nodename(node_p node) 1135 { 1136 static char name[NG_NODESIZ]; 1137 1138 if (NG_NODE_HAS_NAME(node)) 1139 snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node)); 1140 else 1141 snprintf(name, sizeof(name), "[%x]", ng_node2ID(node)); 1142 return name; 1143 } 1144