1 2 /* 3 * ng_bridge.c 4 * 5 * Copyright (c) 2000 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: Archie Cobbs <archie@freebsd.org> 38 * 39 * $FreeBSD$ 40 */ 41 42 /* 43 * ng_bridge(4) netgraph node type 44 * 45 * The node performs standard intelligent Ethernet bridging over 46 * each of its connected hooks, or links. A simple loop detection 47 * algorithm is included which disables a link for priv->conf.loopTimeout 48 * seconds when a host is seen to have jumped from one link to 49 * another within priv->conf.minStableAge seconds. 50 * 51 * We keep a hashtable that maps Ethernet addresses to host info, 52 * which is contained in struct ng_bridge_host's. These structures 53 * tell us on which link the host may be found. A host's entry will 54 * expire after priv->conf.maxStaleness seconds. 55 * 56 * This node is optimzed for stable networks, where machines jump 57 * from one port to the other only rarely. 58 */ 59 60 #include <sys/param.h> 61 #include <sys/systm.h> 62 #include <sys/kernel.h> 63 #include <sys/malloc.h> 64 #include <sys/mbuf.h> 65 #include <sys/errno.h> 66 #include <sys/syslog.h> 67 #include <sys/socket.h> 68 #include <sys/ctype.h> 69 70 #include <net/if.h> 71 #include <net/ethernet.h> 72 73 #include <netinet/in.h> 74 #include <netinet/ip_fw.h> 75 76 #include <netgraph/ng_message.h> 77 #include <netgraph/netgraph.h> 78 #include <netgraph/ng_parse.h> 79 #include <netgraph/ng_bridge.h> 80 #include <netgraph/ng_ether.h> 81 82 /* Per-link private data */ 83 struct ng_bridge_link { 84 hook_p hook; /* netgraph hook */ 85 u_int16_t loopCount; /* loop ignore timer */ 86 struct ng_bridge_link_stats stats; /* link stats */ 87 }; 88 89 /* Per-node private data */ 90 struct ng_bridge_private { 91 struct ng_bridge_bucket *tab; /* hash table bucket array */ 92 struct ng_bridge_link *links[NG_BRIDGE_MAX_LINKS]; 93 struct ng_bridge_config conf; /* node configuration */ 94 node_p node; /* netgraph node */ 95 u_int numHosts; /* num entries in table */ 96 u_int numBuckets; /* num buckets in table */ 97 u_int hashMask; /* numBuckets - 1 */ 98 int numLinks; /* num connected links */ 99 struct callout timer; /* one second periodic timer */ 100 }; 101 typedef struct ng_bridge_private *priv_p; 102 103 /* Information about a host, stored in a hash table entry */ 104 struct ng_bridge_hent { 105 struct ng_bridge_host host; /* actual host info */ 106 SLIST_ENTRY(ng_bridge_hent) next; /* next entry in bucket */ 107 }; 108 109 /* Hash table bucket declaration */ 110 SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent); 111 112 /* Netgraph node methods */ 113 static ng_constructor_t ng_bridge_constructor; 114 static ng_rcvmsg_t ng_bridge_rcvmsg; 115 static ng_shutdown_t ng_bridge_rmnode; 116 static ng_newhook_t ng_bridge_newhook; 117 static ng_rcvdata_t ng_bridge_rcvdata; 118 static ng_disconnect_t ng_bridge_disconnect; 119 120 /* Other internal functions */ 121 static struct ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr); 122 static int ng_bridge_put(priv_p priv, const u_char *addr, int linkNum); 123 static void ng_bridge_rehash(priv_p priv); 124 static void ng_bridge_remove_hosts(priv_p priv, int linkNum); 125 static void ng_bridge_timeout(void *arg); 126 static const char *ng_bridge_nodename(node_p node); 127 128 /* Ethernet broadcast */ 129 static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] = 130 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 131 132 /* Store each hook's link number in the private field */ 133 #define LINK_NUM(hook) (*(u_int16_t *)(&(hook)->private)) 134 135 /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */ 136 #define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \ 137 == ((const u_int32_t *)(b))[0] \ 138 && ((const u_int16_t *)(a))[2] \ 139 == ((const u_int16_t *)(b))[2]) 140 141 /* Minimum and maximum number of hash buckets. Must be a power of two. */ 142 #define MIN_BUCKETS (1 << 5) /* 32 */ 143 #define MAX_BUCKETS (1 << 14) /* 16384 */ 144 145 /* Configuration default values */ 146 #define DEFAULT_LOOP_TIMEOUT 60 147 #define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */ 148 #define DEFAULT_MIN_STABLE_AGE 1 149 150 /****************************************************************** 151 NETGRAPH PARSE TYPES 152 ******************************************************************/ 153 154 /* 155 * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE 156 */ 157 static int 158 ng_bridge_getTableLength(const struct ng_parse_type *type, 159 const u_char *start, const u_char *buf) 160 { 161 const struct ng_bridge_host_ary *const hary 162 = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t)); 163 164 return hary->numHosts; 165 } 166 167 /* Parse type for struct ng_bridge_host_ary */ 168 static const struct ng_parse_struct_info ng_bridge_host_type_info 169 = NG_BRIDGE_HOST_TYPE_INFO(&ng_ether_enaddr_type); 170 static const struct ng_parse_type ng_bridge_host_type = { 171 &ng_parse_struct_type, 172 &ng_bridge_host_type_info 173 }; 174 static const struct ng_parse_array_info ng_bridge_hary_type_info = { 175 &ng_bridge_host_type, 176 ng_bridge_getTableLength 177 }; 178 static const struct ng_parse_type ng_bridge_hary_type = { 179 &ng_parse_array_type, 180 &ng_bridge_hary_type_info 181 }; 182 static const struct ng_parse_struct_info ng_bridge_host_ary_type_info 183 = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type); 184 static const struct ng_parse_type ng_bridge_host_ary_type = { 185 &ng_parse_struct_type, 186 &ng_bridge_host_ary_type_info 187 }; 188 189 /* Parse type for struct ng_bridge_config */ 190 static const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = { 191 &ng_parse_uint8_type, 192 NG_BRIDGE_MAX_LINKS 193 }; 194 static const struct ng_parse_type ng_bridge_ipfwary_type = { 195 &ng_parse_fixedarray_type, 196 &ng_bridge_ipfwary_type_info 197 }; 198 static const struct ng_parse_struct_info ng_bridge_config_type_info 199 = NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type); 200 static const struct ng_parse_type ng_bridge_config_type = { 201 &ng_parse_struct_type, 202 &ng_bridge_config_type_info 203 }; 204 205 /* Parse type for struct ng_bridge_link_stat */ 206 static const struct ng_parse_struct_info 207 ng_bridge_stats_type_info = NG_BRIDGE_STATS_TYPE_INFO; 208 static const struct ng_parse_type ng_bridge_stats_type = { 209 &ng_parse_struct_type, 210 &ng_bridge_stats_type_info 211 }; 212 213 /* List of commands and how to convert arguments to/from ASCII */ 214 static const struct ng_cmdlist ng_bridge_cmdlist[] = { 215 { 216 NGM_BRIDGE_COOKIE, 217 NGM_BRIDGE_SET_CONFIG, 218 "setconfig", 219 &ng_bridge_config_type, 220 NULL 221 }, 222 { 223 NGM_BRIDGE_COOKIE, 224 NGM_BRIDGE_GET_CONFIG, 225 "getconfig", 226 NULL, 227 &ng_bridge_config_type 228 }, 229 { 230 NGM_BRIDGE_COOKIE, 231 NGM_BRIDGE_RESET, 232 "reset", 233 NULL, 234 NULL 235 }, 236 { 237 NGM_BRIDGE_COOKIE, 238 NGM_BRIDGE_GET_STATS, 239 "getstats", 240 &ng_parse_uint32_type, 241 &ng_bridge_stats_type 242 }, 243 { 244 NGM_BRIDGE_COOKIE, 245 NGM_BRIDGE_CLR_STATS, 246 "clrstats", 247 &ng_parse_uint32_type, 248 NULL 249 }, 250 { 251 NGM_BRIDGE_COOKIE, 252 NGM_BRIDGE_GETCLR_STATS, 253 "getclrstats", 254 &ng_parse_uint32_type, 255 &ng_bridge_stats_type 256 }, 257 { 258 NGM_BRIDGE_COOKIE, 259 NGM_BRIDGE_GET_TABLE, 260 "gettable", 261 NULL, 262 &ng_bridge_host_ary_type 263 }, 264 { 0 } 265 }; 266 267 /* Node type descriptor */ 268 static struct ng_type ng_bridge_typestruct = { 269 NG_VERSION, 270 NG_BRIDGE_NODE_TYPE, 271 NULL, 272 ng_bridge_constructor, 273 ng_bridge_rcvmsg, 274 ng_bridge_rmnode, 275 ng_bridge_newhook, 276 NULL, 277 NULL, 278 ng_bridge_rcvdata, 279 ng_bridge_rcvdata, 280 ng_bridge_disconnect, 281 ng_bridge_cmdlist, 282 }; 283 NETGRAPH_INIT(bridge, &ng_bridge_typestruct); 284 285 /* Depend on ng_ether so we can use the Ethernet parse type */ 286 MODULE_DEPEND(ng_bridge, ng_ether, 1, 1, 1); 287 288 /****************************************************************** 289 NETGRAPH NODE METHODS 290 ******************************************************************/ 291 292 /* 293 * Node constructor 294 */ 295 static int 296 ng_bridge_constructor(node_p *nodep) 297 { 298 priv_p priv; 299 int error; 300 301 /* Allocate and initialize private info */ 302 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 303 if (priv == NULL) 304 return (ENOMEM); 305 callout_init(&priv->timer, 0); 306 307 /* Allocate and initialize hash table, etc. */ 308 MALLOC(priv->tab, struct ng_bridge_bucket *, 309 MIN_BUCKETS * sizeof(*priv->tab), M_NETGRAPH, M_NOWAIT | M_ZERO); 310 if (priv->tab == NULL) { 311 FREE(priv, M_NETGRAPH); 312 return (ENOMEM); 313 } 314 priv->numBuckets = MIN_BUCKETS; 315 priv->hashMask = MIN_BUCKETS - 1; 316 priv->conf.debugLevel = 1; 317 priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT; 318 priv->conf.maxStaleness = DEFAULT_MAX_STALENESS; 319 priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE; 320 321 /* Call superclass constructor */ 322 if ((error = ng_make_node_common(&ng_bridge_typestruct, nodep))) { 323 FREE(priv, M_NETGRAPH); 324 return (error); 325 } 326 (*nodep)->private = priv; 327 priv->node = *nodep; 328 329 /* Start timer by faking a timeout event */ 330 (*nodep)->refs++; 331 ng_bridge_timeout(*nodep); 332 return (0); 333 } 334 335 /* 336 * Method for attaching a new hook 337 */ 338 static int 339 ng_bridge_newhook(node_p node, hook_p hook, const char *name) 340 { 341 const priv_p priv = node->private; 342 343 /* Check for a link hook */ 344 if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX, 345 strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) { 346 const char *cp; 347 char *eptr; 348 u_long linkNum; 349 350 cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX); 351 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 352 return (EINVAL); 353 linkNum = strtoul(cp, &eptr, 10); 354 if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS) 355 return (EINVAL); 356 if (priv->links[linkNum] != NULL) 357 return (EISCONN); 358 MALLOC(priv->links[linkNum], struct ng_bridge_link *, 359 sizeof(*priv->links[linkNum]), M_NETGRAPH, M_NOWAIT|M_ZERO); 360 if (priv->links[linkNum] == NULL) 361 return (ENOMEM); 362 priv->links[linkNum]->hook = hook; 363 LINK_NUM(hook) = linkNum; 364 priv->numLinks++; 365 return (0); 366 } 367 368 /* Unknown hook name */ 369 return (EINVAL); 370 } 371 372 /* 373 * Receive a control message 374 */ 375 static int 376 ng_bridge_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr, 377 struct ng_mesg **rptr, hook_p lasthook) 378 { 379 const priv_p priv = node->private; 380 struct ng_mesg *resp = NULL; 381 int error = 0; 382 383 switch (msg->header.typecookie) { 384 case NGM_BRIDGE_COOKIE: 385 switch (msg->header.cmd) { 386 case NGM_BRIDGE_GET_CONFIG: 387 { 388 struct ng_bridge_config *conf; 389 390 NG_MKRESPONSE(resp, msg, 391 sizeof(struct ng_bridge_config), M_NOWAIT); 392 if (resp == NULL) { 393 error = ENOMEM; 394 break; 395 } 396 conf = (struct ng_bridge_config *)resp->data; 397 *conf = priv->conf; /* no sanity checking needed */ 398 break; 399 } 400 case NGM_BRIDGE_SET_CONFIG: 401 { 402 struct ng_bridge_config *conf; 403 int i; 404 405 if (msg->header.arglen 406 != sizeof(struct ng_bridge_config)) { 407 error = EINVAL; 408 break; 409 } 410 conf = (struct ng_bridge_config *)msg->data; 411 priv->conf = *conf; 412 for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) 413 priv->conf.ipfw[i] = !!priv->conf.ipfw[i]; 414 break; 415 } 416 case NGM_BRIDGE_RESET: 417 { 418 int i; 419 420 /* Flush all entries in the hash table */ 421 ng_bridge_remove_hosts(priv, -1); 422 423 /* Reset all loop detection counters and stats */ 424 for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) { 425 if (priv->links[i] == NULL) 426 continue; 427 priv->links[i]->loopCount = 0; 428 bzero(&priv->links[i]->stats, 429 sizeof(priv->links[i]->stats)); 430 } 431 break; 432 } 433 case NGM_BRIDGE_GET_STATS: 434 case NGM_BRIDGE_CLR_STATS: 435 case NGM_BRIDGE_GETCLR_STATS: 436 { 437 struct ng_bridge_link *link; 438 int linkNum; 439 440 /* Get link number */ 441 if (msg->header.arglen != sizeof(u_int32_t)) { 442 error = EINVAL; 443 break; 444 } 445 linkNum = *((u_int32_t *)msg->data); 446 if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) { 447 error = EINVAL; 448 break; 449 } 450 if ((link = priv->links[linkNum]) == NULL) { 451 error = ENOTCONN; 452 break; 453 } 454 455 /* Get/clear stats */ 456 if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) { 457 NG_MKRESPONSE(resp, msg, 458 sizeof(link->stats), M_NOWAIT); 459 if (resp == NULL) { 460 error = ENOMEM; 461 break; 462 } 463 bcopy(&link->stats, 464 resp->data, sizeof(link->stats)); 465 } 466 if (msg->header.cmd != NGM_BRIDGE_GET_STATS) 467 bzero(&link->stats, sizeof(link->stats)); 468 break; 469 } 470 case NGM_BRIDGE_GET_TABLE: 471 { 472 struct ng_bridge_host_ary *ary; 473 struct ng_bridge_hent *hent; 474 int i = 0, bucket; 475 476 NG_MKRESPONSE(resp, msg, sizeof(*ary) 477 + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT); 478 if (resp == NULL) { 479 error = ENOMEM; 480 break; 481 } 482 ary = (struct ng_bridge_host_ary *)resp->data; 483 ary->numHosts = priv->numHosts; 484 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 485 SLIST_FOREACH(hent, &priv->tab[bucket], next) 486 ary->hosts[i++] = hent->host; 487 } 488 break; 489 } 490 default: 491 error = EINVAL; 492 break; 493 } 494 break; 495 default: 496 error = EINVAL; 497 break; 498 } 499 500 /* Done */ 501 if (rptr) 502 *rptr = resp; 503 else if (resp != NULL) 504 FREE(resp, M_NETGRAPH); 505 FREE(msg, M_NETGRAPH); 506 return (error); 507 } 508 509 /* 510 * Receive data on a hook 511 */ 512 static int 513 ng_bridge_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 514 struct mbuf **ret_m, meta_p *ret_meta) 515 { 516 const node_p node = hook->node; 517 const priv_p priv = node->private; 518 struct ng_bridge_host *host; 519 struct ng_bridge_link *link; 520 struct ether_header *eh; 521 int error = 0, linkNum; 522 int i, manycast; 523 524 /* Get link number */ 525 linkNum = LINK_NUM(hook); 526 KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, 527 ("%s: linkNum=%u", __FUNCTION__, linkNum)); 528 link = priv->links[linkNum]; 529 KASSERT(link != NULL, ("%s: link%d null", __FUNCTION__, linkNum)); 530 531 /* Sanity check packet and pull up header */ 532 if (m->m_pkthdr.len < ETHER_HDR_LEN) { 533 link->stats.recvRunts++; 534 NG_FREE_DATA(m, meta); 535 return (EINVAL); 536 } 537 if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) { 538 link->stats.memoryFailures++; 539 NG_FREE_META(meta); 540 return (ENOBUFS); 541 } 542 eh = mtod(m, struct ether_header *); 543 if ((eh->ether_shost[0] & 1) != 0) { 544 link->stats.recvInvalid++; 545 NG_FREE_DATA(m, meta); 546 return (EINVAL); 547 } 548 549 /* Is link disabled due to a loopback condition? */ 550 if (link->loopCount != 0) { 551 link->stats.loopDrops++; 552 NG_FREE_DATA(m, meta); 553 return (ELOOP); /* XXX is this an appropriate error? */ 554 } 555 556 /* Update stats */ 557 link->stats.recvPackets++; 558 link->stats.recvOctets += m->m_pkthdr.len; 559 if ((manycast = (eh->ether_dhost[0] & 1)) != 0) { 560 if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) { 561 link->stats.recvBroadcasts++; 562 manycast = 2; 563 } else 564 link->stats.recvMulticasts++; 565 } 566 567 /* Look up packet's source Ethernet address in hashtable */ 568 if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) { 569 570 /* Update time since last heard from this host */ 571 host->staleness = 0; 572 573 /* Did host jump to a different link? */ 574 if (host->linkNum != linkNum) { 575 576 /* 577 * If the host's old link was recently established 578 * on the old link and it's already jumped to a new 579 * link, declare a loopback condition. 580 */ 581 if (host->age < priv->conf.minStableAge) { 582 583 /* Log the problem */ 584 if (priv->conf.debugLevel >= 2) { 585 struct ifnet *ifp = m->m_pkthdr.rcvif; 586 char suffix[32]; 587 588 if (ifp != NULL) 589 snprintf(suffix, sizeof(suffix), 590 " (%s%d)", ifp->if_name, 591 ifp->if_unit); 592 else 593 *suffix = '\0'; 594 log(LOG_WARNING, "ng_bridge: %s:" 595 " loopback detected on %s%s\n", 596 ng_bridge_nodename(node), 597 hook->name, suffix); 598 } 599 600 /* Mark link as linka non grata */ 601 link->loopCount = priv->conf.loopTimeout; 602 link->stats.loopDetects++; 603 604 /* Forget all hosts on this link */ 605 ng_bridge_remove_hosts(priv, linkNum); 606 607 /* Drop packet */ 608 link->stats.loopDrops++; 609 NG_FREE_DATA(m, meta); 610 return (ELOOP); /* XXX appropriate? */ 611 } 612 613 /* Move host over to new link */ 614 host->linkNum = linkNum; 615 host->age = 0; 616 } 617 } else { 618 if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) { 619 link->stats.memoryFailures++; 620 NG_FREE_DATA(m, meta); 621 return (ENOMEM); 622 } 623 } 624 625 /* Run packet through ipfw processing, if enabled */ 626 if (priv->conf.ipfw[linkNum] && fw_enable && ip_fw_chk_ptr != NULL) { 627 /* XXX not implemented yet */ 628 } 629 630 /* 631 * If unicast and destination host known, deliver to host's link, 632 * unless it is the same link as the packet came in on. 633 */ 634 if (!manycast) { 635 636 /* Determine packet destination link */ 637 if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) { 638 struct ng_bridge_link *const destLink 639 = priv->links[host->linkNum]; 640 641 /* If destination same as incoming link, do nothing */ 642 KASSERT(destLink != NULL, 643 ("%s: link%d null", __FUNCTION__, host->linkNum)); 644 if (destLink == link) { 645 NG_FREE_DATA(m, meta); 646 return (0); 647 } 648 649 /* Deliver packet out the destination link */ 650 destLink->stats.xmitPackets++; 651 destLink->stats.xmitOctets += m->m_pkthdr.len; 652 NG_SEND_DATA(error, destLink->hook, m, meta); 653 return (error); 654 } 655 656 /* Destination host is not known */ 657 link->stats.recvUnknown++; 658 } 659 660 /* Distribute unknown, multicast, broadcast pkts to all other links */ 661 for (linkNum = i = 0; i < priv->numLinks - 1; linkNum++) { 662 struct ng_bridge_link *const destLink = priv->links[linkNum]; 663 meta_p meta2 = NULL; 664 struct mbuf *m2; 665 666 /* Skip incoming link and disconnected links */ 667 if (destLink == NULL || destLink == link) 668 continue; 669 670 /* Copy mbuf and meta info */ 671 if (++i == priv->numLinks - 1) { /* last link */ 672 m2 = m; 673 meta2 = meta; 674 } else { 675 m2 = m_dup(m, M_NOWAIT); /* XXX m_copypacket() */ 676 if (m2 == NULL) { 677 link->stats.memoryFailures++; 678 NG_FREE_DATA(m, meta); 679 return (ENOBUFS); 680 } 681 if (meta != NULL 682 && (meta2 = ng_copy_meta(meta)) == NULL) { 683 link->stats.memoryFailures++; 684 m_freem(m2); 685 NG_FREE_DATA(m, meta); 686 return (ENOMEM); 687 } 688 } 689 690 /* Update stats */ 691 destLink->stats.xmitPackets++; 692 destLink->stats.xmitOctets += m->m_pkthdr.len; 693 switch (manycast) { 694 case 0: /* unicast */ 695 break; 696 case 1: /* multicast */ 697 destLink->stats.xmitMulticasts++; 698 break; 699 case 2: /* broadcast */ 700 destLink->stats.xmitBroadcasts++; 701 break; 702 } 703 704 /* Send packet */ 705 NG_SEND_DATA(error, destLink->hook, m2, meta2); 706 } 707 return (error); 708 } 709 710 /* 711 * Shutdown node 712 */ 713 static int 714 ng_bridge_rmnode(node_p node) 715 { 716 const priv_p priv = node->private; 717 718 ng_unname(node); 719 ng_cutlinks(node); /* frees all link and host info */ 720 KASSERT(priv->numLinks == 0 && priv->numHosts == 0, 721 ("%s: numLinks=%d numHosts=%d", 722 __FUNCTION__, priv->numLinks, priv->numHosts)); 723 FREE(priv->tab, M_NETGRAPH); 724 FREE(priv, M_NETGRAPH); 725 node->private = NULL; 726 ng_unref(node); 727 return (0); 728 } 729 730 /* 731 * Hook disconnection. 732 */ 733 static int 734 ng_bridge_disconnect(hook_p hook) 735 { 736 const priv_p priv = hook->node->private; 737 int linkNum; 738 739 /* Get link number */ 740 linkNum = LINK_NUM(hook); 741 KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, 742 ("%s: linkNum=%u", __FUNCTION__, linkNum)); 743 744 /* Remove all hosts associated with this link */ 745 ng_bridge_remove_hosts(priv, linkNum); 746 747 /* Free associated link information */ 748 KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __FUNCTION__)); 749 FREE(priv->links[linkNum], M_NETGRAPH); 750 priv->links[linkNum] = NULL; 751 priv->numLinks--; 752 753 /* If no more hooks, go away */ 754 if (hook->node->numhooks == 0) 755 ng_rmnode(hook->node); 756 return (0); 757 } 758 759 /****************************************************************** 760 HASH TABLE FUNCTIONS 761 ******************************************************************/ 762 763 /* 764 * Hash algorithm 765 * 766 * Only hashing bytes 3-6 of the Ethernet address is sufficient and fast. 767 */ 768 #define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \ 769 ^ ((const u_int16_t *)(addr))[1] \ 770 ^ ((const u_int16_t *)(addr))[2]) & (mask) ) 771 772 /* 773 * Find a host entry in the table. 774 */ 775 static struct ng_bridge_host * 776 ng_bridge_get(priv_p priv, const u_char *addr) 777 { 778 const int bucket = HASH(addr, priv->hashMask); 779 struct ng_bridge_hent *hent; 780 781 SLIST_FOREACH(hent, &priv->tab[bucket], next) { 782 if (ETHER_EQUAL(hent->host.addr, addr)) 783 return (&hent->host); 784 } 785 return (NULL); 786 } 787 788 /* 789 * Add a new host entry to the table. This assumes the host doesn't 790 * already exist in the table. Returns 1 on success, 0 if there 791 * was a memory allocation failure. 792 */ 793 static int 794 ng_bridge_put(priv_p priv, const u_char *addr, int linkNum) 795 { 796 const int bucket = HASH(addr, priv->hashMask); 797 struct ng_bridge_hent *hent; 798 799 #ifdef INVARIANTS 800 /* Assert that entry does not already exist in hashtable */ 801 SLIST_FOREACH(hent, &priv->tab[bucket], next) { 802 KASSERT(!ETHER_EQUAL(hent->host.addr, addr), 803 ("%s: entry %6D exists in table", __FUNCTION__, addr, ":")); 804 } 805 #endif 806 807 /* Allocate and initialize new hashtable entry */ 808 MALLOC(hent, struct ng_bridge_hent *, 809 sizeof(*hent), M_NETGRAPH, M_NOWAIT); 810 if (hent == NULL) 811 return (0); 812 bcopy(addr, hent->host.addr, ETHER_ADDR_LEN); 813 hent->host.linkNum = linkNum; 814 hent->host.staleness = 0; 815 hent->host.age = 0; 816 817 /* Add new element to hash bucket */ 818 SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next); 819 priv->numHosts++; 820 821 /* Resize table if necessary */ 822 ng_bridge_rehash(priv); 823 return (1); 824 } 825 826 /* 827 * Resize the hash table. We try to maintain the number of buckets 828 * such that the load factor is in the range 0.25 to 1.0. 829 * 830 * If we can't get the new memory then we silently fail. This is OK 831 * because things will still work and we'll try again soon anyway. 832 */ 833 static void 834 ng_bridge_rehash(priv_p priv) 835 { 836 struct ng_bridge_bucket *newTab; 837 int oldBucket, newBucket; 838 int newNumBuckets; 839 u_int newMask; 840 841 /* Is table too full or too empty? */ 842 if (priv->numHosts > priv->numBuckets 843 && (priv->numBuckets << 1) <= MAX_BUCKETS) 844 newNumBuckets = priv->numBuckets << 1; 845 else if (priv->numHosts < (priv->numBuckets >> 2) 846 && (priv->numBuckets >> 2) >= MIN_BUCKETS) 847 newNumBuckets = priv->numBuckets >> 2; 848 else 849 return; 850 newMask = newNumBuckets - 1; 851 852 /* Allocate and initialize new table */ 853 MALLOC(newTab, struct ng_bridge_bucket *, 854 newNumBuckets * sizeof(*newTab), M_NETGRAPH, M_NOWAIT | M_ZERO); 855 if (newTab == NULL) 856 return; 857 858 /* Move all entries from old table to new table */ 859 for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) { 860 struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket]; 861 862 while (!SLIST_EMPTY(oldList)) { 863 struct ng_bridge_hent *const hent 864 = SLIST_FIRST(oldList); 865 866 SLIST_REMOVE_HEAD(oldList, next); 867 newBucket = HASH(hent->host.addr, newMask); 868 SLIST_INSERT_HEAD(&newTab[newBucket], hent, next); 869 } 870 } 871 872 /* Replace old table with new one */ 873 if (priv->conf.debugLevel >= 3) { 874 log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n", 875 ng_bridge_nodename(priv->node), 876 priv->numBuckets, newNumBuckets); 877 } 878 FREE(priv->tab, M_NETGRAPH); 879 priv->numBuckets = newNumBuckets; 880 priv->hashMask = newMask; 881 priv->tab = newTab; 882 return; 883 } 884 885 /****************************************************************** 886 MISC FUNCTIONS 887 ******************************************************************/ 888 889 /* 890 * Remove all hosts associated with a specific link from the hashtable. 891 * If linkNum == -1, then remove all hosts in the table. 892 */ 893 static void 894 ng_bridge_remove_hosts(priv_p priv, int linkNum) 895 { 896 int bucket; 897 898 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 899 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 900 901 while (*hptr != NULL) { 902 struct ng_bridge_hent *const hent = *hptr; 903 904 if (linkNum == -1 || hent->host.linkNum == linkNum) { 905 *hptr = SLIST_NEXT(hent, next); 906 FREE(hent, M_NETGRAPH); 907 priv->numHosts--; 908 } else 909 hptr = &SLIST_NEXT(hent, next); 910 } 911 } 912 } 913 914 /* 915 * Handle our once-per-second timeout event. We do two things: 916 * we decrement link->loopCount for those links being muted due to 917 * a detected loopback condition, and we remove any hosts from 918 * the hashtable whom we haven't heard from in a long while. 919 */ 920 static void 921 ng_bridge_timeout(void *arg) 922 { 923 const node_p node = arg; 924 const priv_p priv = node->private; 925 int s, bucket; 926 int counter = 0; 927 int linkNum; 928 929 /* Avoid race condition with ng_bridge_shutdown() */ 930 s = splnet(); 931 if ((node->flags & NG_INVALID) != 0 || priv == NULL) { 932 ng_unref(node); 933 splx(s); 934 return; 935 } 936 937 /* Register a new timeout, keeping the existing node reference */ 938 callout_reset(&priv->timer, hz, ng_bridge_timeout, node); 939 940 /* Update host time counters and remove stale entries */ 941 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 942 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 943 944 while (*hptr != NULL) { 945 struct ng_bridge_hent *const hent = *hptr; 946 947 /* Make sure host's link really exists */ 948 KASSERT(priv->links[hent->host.linkNum] != NULL, 949 ("%s: host %6D on nonexistent link %d\n", 950 __FUNCTION__, hent->host.addr, ":", 951 hent->host.linkNum)); 952 953 /* Remove hosts we haven't heard from in a while */ 954 if (++hent->host.staleness >= priv->conf.maxStaleness) { 955 *hptr = SLIST_NEXT(hent, next); 956 FREE(hent, M_NETGRAPH); 957 priv->numHosts--; 958 } else { 959 if (hent->host.age < 0xffff) 960 hent->host.age++; 961 hptr = &SLIST_NEXT(hent, next); 962 counter++; 963 } 964 } 965 } 966 KASSERT(priv->numHosts == counter, 967 ("%s: hosts: %d != %d", __FUNCTION__, priv->numHosts, counter)); 968 969 /* Decrease table size if necessary */ 970 ng_bridge_rehash(priv); 971 972 /* Decrease loop counter on muted looped back links */ 973 for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) { 974 struct ng_bridge_link *const link = priv->links[linkNum]; 975 976 if (link != NULL) { 977 if (link->loopCount != 0) { 978 link->loopCount--; 979 if (link->loopCount == 0 980 && priv->conf.debugLevel >= 2) { 981 log(LOG_INFO, "ng_bridge: %s:" 982 " restoring looped back link%d\n", 983 ng_bridge_nodename(node), linkNum); 984 } 985 } 986 counter++; 987 } 988 } 989 KASSERT(priv->numLinks == counter, 990 ("%s: links: %d != %d", __FUNCTION__, priv->numLinks, counter)); 991 992 /* Done */ 993 splx(s); 994 } 995 996 /* 997 * Return node's "name", even if it doesn't have one. 998 */ 999 static const char * 1000 ng_bridge_nodename(node_p node) 1001 { 1002 static char name[NG_NODELEN+1]; 1003 1004 if (node->name != NULL) 1005 snprintf(name, sizeof(name), "%s", node->name); 1006 else 1007 snprintf(name, sizeof(name), "[%x]", ng_node2ID(node)); 1008 return name; 1009 } 1010 1011