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 u_int64_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 typedef struct ng_bridge_link const *link_cp; /* read only access */ 119 120 /* Per-node private data */ 121 struct ng_bridge_private { 122 struct ng_bridge_bucket *tab; /* hash table bucket array */ 123 struct ng_bridge_config conf; /* node configuration */ 124 node_p node; /* netgraph node */ 125 u_int numHosts; /* num entries in table */ 126 u_int numBuckets; /* num buckets in table */ 127 u_int hashMask; /* numBuckets - 1 */ 128 int numLinks; /* num connected links */ 129 unsigned int persistent : 1, /* can exist w/o hooks */ 130 sendUnknown : 1;/* links receive unknowns by default */ 131 struct callout timer; /* one second periodic timer */ 132 }; 133 typedef struct ng_bridge_private *priv_p; 134 typedef struct ng_bridge_private const *priv_cp; /* read only access */ 135 136 /* Information about a host, stored in a hash table entry */ 137 struct ng_bridge_host { 138 u_char addr[6]; /* ethernet address */ 139 link_p link; /* link where addr can be found */ 140 u_int16_t age; /* seconds ago entry was created */ 141 u_int16_t staleness; /* seconds ago host last heard from */ 142 SLIST_ENTRY(ng_bridge_host) next; /* next entry in bucket */ 143 }; 144 145 /* Hash table bucket declaration */ 146 SLIST_HEAD(ng_bridge_bucket, ng_bridge_host); 147 148 /* Netgraph node methods */ 149 static ng_constructor_t ng_bridge_constructor; 150 static ng_rcvmsg_t ng_bridge_rcvmsg; 151 static ng_shutdown_t ng_bridge_shutdown; 152 static ng_newhook_t ng_bridge_newhook; 153 static ng_rcvdata_t ng_bridge_rcvdata; 154 static ng_disconnect_t ng_bridge_disconnect; 155 156 /* Other internal functions */ 157 static struct ng_bridge_host *ng_bridge_get(priv_cp priv, const u_char *addr); 158 static int ng_bridge_put(priv_p priv, const u_char *addr, link_p link); 159 static void ng_bridge_rehash(priv_p priv); 160 static void ng_bridge_remove_hosts(priv_p priv, link_p link); 161 static void ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2); 162 static const char *ng_bridge_nodename(node_cp node); 163 164 /* Ethernet broadcast */ 165 static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] = 166 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 167 168 /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */ 169 #define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \ 170 == ((const u_int32_t *)(b))[0] \ 171 && ((const u_int16_t *)(a))[2] \ 172 == ((const u_int16_t *)(b))[2]) 173 174 /* Minimum and maximum number of hash buckets. Must be a power of two. */ 175 #define MIN_BUCKETS (1 << 5) /* 32 */ 176 #define MAX_BUCKETS (1 << 14) /* 16384 */ 177 178 /* Configuration default values */ 179 #define DEFAULT_LOOP_TIMEOUT 60 180 #define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */ 181 #define DEFAULT_MIN_STABLE_AGE 1 182 183 /****************************************************************** 184 NETGRAPH PARSE TYPES 185 ******************************************************************/ 186 187 /* 188 * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE 189 */ 190 static int 191 ng_bridge_getTableLength(const struct ng_parse_type *type, 192 const u_char *start, const u_char *buf) 193 { 194 const struct ng_bridge_host_ary *const hary 195 = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t)); 196 197 return hary->numHosts; 198 } 199 200 /* Parse type for struct ng_bridge_host_ary */ 201 static const struct ng_parse_struct_field ng_bridge_host_type_fields[] 202 = NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type); 203 static const struct ng_parse_type ng_bridge_host_type = { 204 &ng_parse_struct_type, 205 &ng_bridge_host_type_fields 206 }; 207 static const struct ng_parse_array_info ng_bridge_hary_type_info = { 208 &ng_bridge_host_type, 209 ng_bridge_getTableLength 210 }; 211 static const struct ng_parse_type ng_bridge_hary_type = { 212 &ng_parse_array_type, 213 &ng_bridge_hary_type_info 214 }; 215 static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[] 216 = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type); 217 static const struct ng_parse_type ng_bridge_host_ary_type = { 218 &ng_parse_struct_type, 219 &ng_bridge_host_ary_type_fields 220 }; 221 222 /* Parse type for struct ng_bridge_config */ 223 static const struct ng_parse_struct_field ng_bridge_config_type_fields[] 224 = NG_BRIDGE_CONFIG_TYPE_INFO; 225 static const struct ng_parse_type ng_bridge_config_type = { 226 &ng_parse_struct_type, 227 &ng_bridge_config_type_fields 228 }; 229 230 /* Parse type for struct ng_bridge_link_stat */ 231 static const struct ng_parse_struct_field ng_bridge_stats_type_fields[] 232 = NG_BRIDGE_STATS_TYPE_INFO; 233 static const struct ng_parse_type ng_bridge_stats_type = { 234 &ng_parse_struct_type, 235 &ng_bridge_stats_type_fields 236 }; 237 /* Parse type for struct ng_bridge_move_host */ 238 static const struct ng_parse_struct_field ng_bridge_move_host_type_fields[] 239 = NG_BRIDGE_MOVE_HOST_TYPE_INFO(&ng_parse_enaddr_type); 240 static const struct ng_parse_type ng_bridge_move_host_type = { 241 &ng_parse_struct_type, 242 &ng_bridge_move_host_type_fields 243 }; 244 245 /* List of commands and how to convert arguments to/from ASCII */ 246 static const struct ng_cmdlist ng_bridge_cmdlist[] = { 247 { 248 NGM_BRIDGE_COOKIE, 249 NGM_BRIDGE_SET_CONFIG, 250 "setconfig", 251 &ng_bridge_config_type, 252 NULL 253 }, 254 { 255 NGM_BRIDGE_COOKIE, 256 NGM_BRIDGE_GET_CONFIG, 257 "getconfig", 258 NULL, 259 &ng_bridge_config_type 260 }, 261 { 262 NGM_BRIDGE_COOKIE, 263 NGM_BRIDGE_RESET, 264 "reset", 265 NULL, 266 NULL 267 }, 268 { 269 NGM_BRIDGE_COOKIE, 270 NGM_BRIDGE_GET_STATS, 271 "getstats", 272 &ng_parse_uint32_type, 273 &ng_bridge_stats_type 274 }, 275 { 276 NGM_BRIDGE_COOKIE, 277 NGM_BRIDGE_CLR_STATS, 278 "clrstats", 279 &ng_parse_uint32_type, 280 NULL 281 }, 282 { 283 NGM_BRIDGE_COOKIE, 284 NGM_BRIDGE_GETCLR_STATS, 285 "getclrstats", 286 &ng_parse_uint32_type, 287 &ng_bridge_stats_type 288 }, 289 { 290 NGM_BRIDGE_COOKIE, 291 NGM_BRIDGE_GET_TABLE, 292 "gettable", 293 NULL, 294 &ng_bridge_host_ary_type 295 }, 296 { 297 NGM_BRIDGE_COOKIE, 298 NGM_BRIDGE_SET_PERSISTENT, 299 "setpersistent", 300 NULL, 301 NULL 302 }, 303 { 304 NGM_BRIDGE_COOKIE, 305 NGM_BRIDGE_MOVE_HOST, 306 "movehost", 307 &ng_bridge_move_host_type, 308 NULL 309 }, 310 { 0 } 311 }; 312 313 /* Node type descriptor */ 314 static struct ng_type ng_bridge_typestruct = { 315 .version = NG_ABI_VERSION, 316 .name = NG_BRIDGE_NODE_TYPE, 317 .constructor = ng_bridge_constructor, 318 .rcvmsg = ng_bridge_rcvmsg, 319 .shutdown = ng_bridge_shutdown, 320 .newhook = ng_bridge_newhook, 321 .rcvdata = ng_bridge_rcvdata, 322 .disconnect = ng_bridge_disconnect, 323 .cmdlist = ng_bridge_cmdlist, 324 }; 325 NETGRAPH_INIT(bridge, &ng_bridge_typestruct); 326 327 /****************************************************************** 328 NETGRAPH NODE METHODS 329 ******************************************************************/ 330 331 /* 332 * Node constructor 333 */ 334 static int 335 ng_bridge_constructor(node_p node) 336 { 337 priv_p priv; 338 339 /* Allocate and initialize private info */ 340 priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO); 341 ng_callout_init(&priv->timer); 342 343 /* Allocate and initialize hash table, etc. */ 344 priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab), 345 M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO); 346 priv->numBuckets = MIN_BUCKETS; 347 priv->hashMask = MIN_BUCKETS - 1; 348 priv->conf.debugLevel = 1; 349 priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT; 350 priv->conf.maxStaleness = DEFAULT_MAX_STALENESS; 351 priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE; 352 priv->sendUnknown = 1; /* classic bridge */ 353 354 NG_NODE_SET_PRIVATE(node, priv); 355 priv->node = node; 356 357 /* Start timer; timer is always running while node is alive */ 358 ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 359 360 /* Done */ 361 return (0); 362 } 363 364 /* 365 * Method for attaching a new hook 366 */ 367 static int 368 ng_bridge_newhook(node_p node, hook_p hook, const char *name) 369 { 370 const priv_p priv = NG_NODE_PRIVATE(node); 371 char linkName[NG_HOOKSIZ]; 372 u_int32_t linkNum; 373 link_p link; 374 const char *prefix = NG_BRIDGE_HOOK_LINK_PREFIX; 375 bool isUplink; 376 377 /* Check for a link hook */ 378 if (strlen(name) <= strlen(prefix)) 379 return (EINVAL); /* Unknown hook name */ 380 381 isUplink = (name[0] == 'u'); 382 if (isUplink) 383 prefix = NG_BRIDGE_HOOK_UPLINK_PREFIX; 384 385 /* primitive parsing */ 386 linkNum = strtoul(name + strlen(prefix), NULL, 10); 387 /* validation by comparing against the reconstucted name */ 388 snprintf(linkName, sizeof(linkName), "%s%u", prefix, linkNum); 389 if (strcmp(linkName, name) != 0) 390 return (EINVAL); 391 392 if (linkNum == 0 && isUplink) 393 return (EINVAL); 394 395 if(NG_PEER_NODE(hook) == node) 396 return (ELOOP); 397 398 link = malloc(sizeof(*link), M_NETGRAPH_BRIDGE, M_ZERO); 399 if (link == NULL) 400 return (ENOMEM); 401 402 link->stats.recvOctets = counter_u64_alloc(M_WAITOK); 403 link->stats.recvPackets = counter_u64_alloc(M_WAITOK); 404 link->stats.recvMulticasts = counter_u64_alloc(M_WAITOK); 405 link->stats.recvBroadcasts = counter_u64_alloc(M_WAITOK); 406 link->stats.recvUnknown = counter_u64_alloc(M_WAITOK); 407 link->stats.recvRunts = counter_u64_alloc(M_WAITOK); 408 link->stats.recvInvalid = counter_u64_alloc(M_WAITOK); 409 link->stats.xmitOctets = counter_u64_alloc(M_WAITOK); 410 link->stats.xmitPackets = counter_u64_alloc(M_WAITOK); 411 link->stats.xmitMulticasts = counter_u64_alloc(M_WAITOK); 412 link->stats.xmitBroadcasts = counter_u64_alloc(M_WAITOK); 413 link->stats.loopDrops = counter_u64_alloc(M_WAITOK); 414 link->stats.memoryFailures = counter_u64_alloc(M_WAITOK); 415 416 link->hook = hook; 417 if (isUplink) { 418 link->learnMac = 0; 419 link->sendUnknown = 1; 420 if (priv->numLinks == 0) /* if the first link is an uplink */ 421 priv->sendUnknown = 0; /* switch to restrictive mode */ 422 } else { 423 link->learnMac = 1; 424 link->sendUnknown = priv->sendUnknown; 425 } 426 427 NG_HOOK_SET_PRIVATE(hook, link); 428 priv->numLinks++; 429 return (0); 430 } 431 432 /* 433 * Receive a control message 434 */ 435 static void ng_bridge_clear_link_stats(struct ng_bridge_link_kernel_stats * p) 436 { 437 counter_u64_zero(p->recvOctets); 438 counter_u64_zero(p->recvPackets); 439 counter_u64_zero(p->recvMulticasts); 440 counter_u64_zero(p->recvBroadcasts); 441 counter_u64_zero(p->recvUnknown); 442 counter_u64_zero(p->recvRunts); 443 counter_u64_zero(p->recvInvalid); 444 counter_u64_zero(p->xmitOctets); 445 counter_u64_zero(p->xmitPackets); 446 counter_u64_zero(p->xmitMulticasts); 447 counter_u64_zero(p->xmitBroadcasts); 448 counter_u64_zero(p->loopDrops); 449 p->loopDetects = 0; 450 counter_u64_zero(p->memoryFailures); 451 }; 452 453 static int 454 ng_bridge_reset_link(hook_p hook, void *arg __unused) 455 { 456 link_p priv = NG_HOOK_PRIVATE(hook); 457 458 priv->loopCount = 0; 459 ng_bridge_clear_link_stats(&priv->stats); 460 return (1); 461 } 462 463 static int 464 ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook) 465 { 466 const priv_p priv = NG_NODE_PRIVATE(node); 467 struct ng_mesg *resp = NULL; 468 int error = 0; 469 struct ng_mesg *msg; 470 471 NGI_GET_MSG(item, msg); 472 switch (msg->header.typecookie) { 473 case NGM_BRIDGE_COOKIE: 474 switch (msg->header.cmd) { 475 case NGM_BRIDGE_GET_CONFIG: 476 { 477 struct ng_bridge_config *conf; 478 479 NG_MKRESPONSE(resp, msg, 480 sizeof(struct ng_bridge_config), M_NOWAIT); 481 if (resp == NULL) { 482 error = ENOMEM; 483 break; 484 } 485 conf = (struct ng_bridge_config *)resp->data; 486 *conf = priv->conf; /* no sanity checking needed */ 487 break; 488 } 489 case NGM_BRIDGE_SET_CONFIG: 490 { 491 struct ng_bridge_config *conf; 492 493 if (msg->header.arglen 494 != sizeof(struct ng_bridge_config)) { 495 error = EINVAL; 496 break; 497 } 498 conf = (struct ng_bridge_config *)msg->data; 499 priv->conf = *conf; 500 break; 501 } 502 case NGM_BRIDGE_RESET: 503 { 504 hook_p rethook; 505 506 /* Flush all entries in the hash table */ 507 ng_bridge_remove_hosts(priv, NULL); 508 509 /* Reset all loop detection counters and stats */ 510 NG_NODE_FOREACH_HOOK(node, ng_bridge_reset_link, NULL, 511 rethook); 512 break; 513 } 514 case NGM_BRIDGE_GET_STATS: 515 case NGM_BRIDGE_CLR_STATS: 516 case NGM_BRIDGE_GETCLR_STATS: 517 { 518 hook_p hook; 519 link_p link; 520 char linkName[NG_HOOKSIZ]; 521 int linkNum; 522 523 /* Get link number */ 524 if (msg->header.arglen != sizeof(u_int32_t)) { 525 error = EINVAL; 526 break; 527 } 528 linkNum = *((int32_t *)msg->data); 529 if (linkNum < 0) 530 snprintf(linkName, sizeof(linkName), 531 "%s%u", NG_BRIDGE_HOOK_UPLINK_PREFIX, -linkNum); 532 else 533 snprintf(linkName, sizeof(linkName), 534 "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX, linkNum); 535 536 if ((hook = ng_findhook(node, linkName)) == NULL) { 537 error = ENOTCONN; 538 break; 539 } 540 link = NG_HOOK_PRIVATE(hook); 541 542 /* Get/clear stats */ 543 if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) { 544 struct ng_bridge_link_stats *rs; 545 546 NG_MKRESPONSE(resp, msg, 547 sizeof(link->stats), M_NOWAIT); 548 if (resp == NULL) { 549 error = ENOMEM; 550 break; 551 } 552 rs = (struct ng_bridge_link_stats *)resp->data; 553 #define FETCH(x) rs->x = counter_u64_fetch(link->stats.x) 554 FETCH(recvOctets); 555 FETCH(recvPackets); 556 FETCH(recvMulticasts); 557 FETCH(recvBroadcasts); 558 FETCH(recvUnknown); 559 FETCH(recvRunts); 560 FETCH(recvInvalid); 561 FETCH(xmitOctets); 562 FETCH(xmitPackets); 563 FETCH(xmitMulticasts); 564 FETCH(xmitBroadcasts); 565 FETCH(loopDrops); 566 rs->loopDetects = link->stats.loopDetects; 567 FETCH(memoryFailures); 568 #undef FETCH 569 } 570 if (msg->header.cmd != NGM_BRIDGE_GET_STATS) 571 ng_bridge_clear_link_stats(&link->stats); 572 break; 573 } 574 case NGM_BRIDGE_GET_TABLE: 575 { 576 struct ng_bridge_host_ary *ary; 577 struct ng_bridge_host *host; 578 int i = 0, bucket; 579 580 NG_MKRESPONSE(resp, msg, sizeof(*ary) 581 + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT); 582 if (resp == NULL) { 583 error = ENOMEM; 584 break; 585 } 586 ary = (struct ng_bridge_host_ary *)resp->data; 587 ary->numHosts = priv->numHosts; 588 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 589 SLIST_FOREACH(host, &priv->tab[bucket], next) { 590 memcpy(ary->hosts[i].addr, 591 host->addr, 592 sizeof(ary->hosts[i].addr)); 593 ary->hosts[i].age = host->age; 594 ary->hosts[i].staleness = host->staleness; 595 strncpy(ary->hosts[i].hook, 596 NG_HOOK_NAME(host->link->hook), 597 sizeof(ary->hosts[i].hook)); 598 i++; 599 } 600 } 601 break; 602 } 603 case NGM_BRIDGE_SET_PERSISTENT: 604 { 605 priv->persistent = 1; 606 break; 607 } 608 case NGM_BRIDGE_MOVE_HOST: 609 { 610 struct ng_bridge_move_host *mh; 611 hook_p hook; 612 613 if (msg->header.arglen < sizeof(*mh)) { 614 error = EINVAL; 615 break; 616 } 617 mh = (struct ng_bridge_move_host *)msg->data; 618 hook = (mh->hook[0] == 0) 619 ? lasthook 620 : ng_findhook(node, mh->hook); 621 if (hook == NULL) { 622 error = ENOENT; 623 break; 624 } 625 error = ng_bridge_put(priv, mh->addr, NG_HOOK_PRIVATE(hook)); 626 break; 627 } 628 default: 629 error = EINVAL; 630 break; 631 } 632 break; 633 default: 634 error = EINVAL; 635 break; 636 } 637 638 /* Done */ 639 NG_RESPOND_MSG(error, node, item, resp); 640 NG_FREE_MSG(msg); 641 return (error); 642 } 643 644 /* 645 * Receive data on a hook 646 */ 647 struct ng_bridge_send_ctx { 648 link_p foundFirst, incoming; 649 struct mbuf * m; 650 int manycast, error; 651 }; 652 653 /* 654 * Update stats and send out 655 */ 656 static inline int 657 ng_bridge_send_data(link_cp dst, int manycast, struct mbuf *m, item_p item) { 658 int error = 0; 659 size_t len = m->m_pkthdr.len; 660 661 if(item != NULL) 662 NG_FWD_NEW_DATA(error, item, dst->hook, m); 663 else 664 NG_SEND_DATA_ONLY(error, dst->hook, m); 665 666 if (error) { 667 /* The packet is still ours */ 668 if (item != NULL) 669 NG_FREE_ITEM(item); 670 if (m != NULL) 671 NG_FREE_M(m); 672 return (error); 673 } 674 675 counter_u64_add(dst->stats.xmitPackets, 1); 676 counter_u64_add(dst->stats.xmitOctets, len); 677 switch (manycast) { 678 default: /* unknown unicast */ 679 break; 680 case 1: /* multicast */ 681 counter_u64_add(dst->stats.xmitMulticasts, 1); 682 break; 683 case 2: /* broadcast */ 684 counter_u64_add(dst->stats.xmitBroadcasts, 1); 685 break; 686 } 687 return (0); 688 } 689 690 /* 691 * Loop body for sending to multiple destinations 692 * return 0 to stop looping 693 */ 694 static int 695 ng_bridge_send_ctx(hook_p dst, void *arg) 696 { 697 struct ng_bridge_send_ctx *ctx = arg; 698 link_p destLink = NG_HOOK_PRIVATE(dst); 699 struct mbuf *m2 = NULL; 700 int error = 0; 701 702 /* Skip incoming link */ 703 if (destLink == ctx->incoming) { 704 return (1); 705 } 706 707 /* Skip sending unknowns to undesired links */ 708 if (!ctx->manycast && !destLink->sendUnknown) 709 return (1); 710 711 if (ctx->foundFirst == NULL) { 712 /* 713 * This is the first usable link we have found. 714 * Reserve it for the originals. 715 * If we never find another we save a copy. 716 */ 717 ctx->foundFirst = destLink; 718 return (1); 719 } 720 721 /* 722 * It's usable link but not the reserved (first) one. 723 * Copy mbuf info for sending. 724 */ 725 m2 = m_dup(ctx->m, M_NOWAIT); 726 if (m2 == NULL) { 727 counter_u64_add(ctx->incoming->stats.memoryFailures, 1); 728 ctx->error = ENOBUFS; 729 return (0); /* abort loop, do not try again and again */ 730 } 731 732 /* Send packet */ 733 error = ng_bridge_send_data(destLink, ctx->manycast, m2, NULL); 734 if (error) 735 ctx->error = error; 736 return (1); 737 } 738 739 static int 740 ng_bridge_rcvdata(hook_p hook, item_p item) 741 { 742 const node_p node = NG_HOOK_NODE(hook); 743 const priv_p priv = NG_NODE_PRIVATE(node); 744 struct ng_bridge_host *host; 745 struct ether_header *eh; 746 struct ng_bridge_send_ctx ctx = { 0 }; 747 hook_p ret; 748 749 NGI_GET_M(item, ctx.m); 750 751 ctx.incoming = NG_HOOK_PRIVATE(hook); 752 /* Sanity check packet and pull up header */ 753 if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) { 754 counter_u64_add(ctx.incoming->stats.recvRunts, 1); 755 NG_FREE_ITEM(item); 756 NG_FREE_M(ctx.m); 757 return (EINVAL); 758 } 759 if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) { 760 counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 761 NG_FREE_ITEM(item); 762 return (ENOBUFS); 763 } 764 eh = mtod(ctx.m, struct ether_header *); 765 if ((eh->ether_shost[0] & 1) != 0) { 766 counter_u64_add(ctx.incoming->stats.recvInvalid, 1); 767 NG_FREE_ITEM(item); 768 NG_FREE_M(ctx.m); 769 return (EINVAL); 770 } 771 772 /* Is link disabled due to a loopback condition? */ 773 if (ctx.incoming->loopCount != 0) { 774 counter_u64_add(ctx.incoming->stats.loopDrops, 1); 775 NG_FREE_ITEM(item); 776 NG_FREE_M(ctx.m); 777 return (ELOOP); 778 } 779 780 /* Update stats */ 781 counter_u64_add(ctx.incoming->stats.recvPackets, 1); 782 counter_u64_add(ctx.incoming->stats.recvOctets, ctx.m->m_pkthdr.len); 783 if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) { 784 if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) { 785 counter_u64_add(ctx.incoming->stats.recvBroadcasts, 1); 786 ctx.manycast = 2; 787 } else 788 counter_u64_add(ctx.incoming->stats.recvMulticasts, 1); 789 } 790 791 /* Look up packet's source Ethernet address in hashtable */ 792 if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) 793 /* Update time since last heard from this host. 794 * This is safe without locking, because it's 795 * the only operation during shared access. 796 */ 797 if (__predict_false(host->staleness > 0)) 798 host->staleness = 0; 799 800 if ((host == NULL && ctx.incoming->learnMac) || 801 (host != NULL && host->link != ctx.incoming)) { 802 struct ng_mesg *msg; 803 struct ng_bridge_move_host *mh; 804 int error = 0; 805 806 NG_MKMESSAGE(msg, NGM_BRIDGE_COOKIE, NGM_BRIDGE_MOVE_HOST, 807 sizeof(*mh), M_NOWAIT); 808 if (msg == NULL) { 809 counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 810 NG_FREE_ITEM(item); 811 NG_FREE_M(ctx.m); 812 return (ENOMEM); 813 } 814 mh = (struct ng_bridge_move_host *)msg->data; 815 strncpy(mh->hook, NG_HOOK_NAME(ctx.incoming->hook), 816 sizeof(mh->hook)); 817 memcpy(mh->addr, eh->ether_shost, sizeof(mh->addr)); 818 NG_SEND_MSG_ID(error, node, msg, NG_NODE_ID(node), 819 NG_NODE_ID(node)); 820 if (error) 821 counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 822 } 823 824 if (host != NULL && host->link != ctx.incoming) { 825 if (host->age < priv->conf.minStableAge) { 826 /* Drop packet on instable links */ 827 counter_u64_add(ctx.incoming->stats.loopDrops, 1); 828 NG_FREE_ITEM(item); 829 NG_FREE_M(ctx.m); 830 return (ELOOP); 831 } 832 } 833 834 /* Run packet through ipfw processing, if enabled */ 835 #if 0 836 if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) { 837 /* XXX not implemented yet */ 838 } 839 #endif 840 841 /* 842 * If unicast and destination host known, deliver to host's link, 843 * unless it is the same link as the packet came in on. 844 */ 845 if (!ctx.manycast) { 846 /* Determine packet destination link */ 847 if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) { 848 link_p destLink = host->link; 849 850 /* If destination same as incoming link, do nothing */ 851 if (destLink == ctx.incoming) { 852 NG_FREE_ITEM(item); 853 NG_FREE_M(ctx.m); 854 return (0); 855 } 856 857 /* Deliver packet out the destination link */ 858 return (ng_bridge_send_data(destLink, ctx.manycast, ctx.m, item)); 859 } 860 861 /* Destination host is not known */ 862 counter_u64_add(ctx.incoming->stats.recvUnknown, 1); 863 } 864 865 /* Distribute unknown, multicast, broadcast pkts to all other links */ 866 NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx, ret); 867 868 /* Finally send out on the first link found */ 869 if (ctx.foundFirst != NULL) { 870 int error = ng_bridge_send_data(ctx.foundFirst, ctx.manycast, ctx.m, item); 871 if (error) 872 ctx.error = error; 873 } else { /* nothing to send at all */ 874 NG_FREE_ITEM(item); 875 NG_FREE_M(ctx.m); 876 } 877 878 return (ctx.error); 879 } 880 881 /* 882 * Shutdown node 883 */ 884 static int 885 ng_bridge_shutdown(node_p node) 886 { 887 const priv_p priv = NG_NODE_PRIVATE(node); 888 889 /* 890 * Shut down everything including the timer. Even if the 891 * callout has already been dequeued and is about to be 892 * run, ng_bridge_timeout() won't be fired as the node 893 * is already marked NGF_INVALID, so we're safe to free 894 * the node now. 895 */ 896 KASSERT(priv->numLinks == 0 && priv->numHosts == 0, 897 ("%s: numLinks=%d numHosts=%d", 898 __func__, priv->numLinks, priv->numHosts)); 899 ng_uncallout(&priv->timer, node); 900 NG_NODE_SET_PRIVATE(node, NULL); 901 NG_NODE_UNREF(node); 902 free(priv->tab, M_NETGRAPH_BRIDGE); 903 free(priv, M_NETGRAPH_BRIDGE); 904 return (0); 905 } 906 907 /* 908 * Hook disconnection. 909 */ 910 static int 911 ng_bridge_disconnect(hook_p hook) 912 { 913 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 914 link_p link = NG_HOOK_PRIVATE(hook); 915 916 /* Remove all hosts associated with this link */ 917 ng_bridge_remove_hosts(priv, link); 918 919 /* Free associated link information */ 920 counter_u64_free(link->stats.recvOctets); 921 counter_u64_free(link->stats.recvPackets); 922 counter_u64_free(link->stats.recvMulticasts); 923 counter_u64_free(link->stats.recvBroadcasts); 924 counter_u64_free(link->stats.recvUnknown); 925 counter_u64_free(link->stats.recvRunts); 926 counter_u64_free(link->stats.recvInvalid); 927 counter_u64_free(link->stats.xmitOctets); 928 counter_u64_free(link->stats.xmitPackets); 929 counter_u64_free(link->stats.xmitMulticasts); 930 counter_u64_free(link->stats.xmitBroadcasts); 931 counter_u64_free(link->stats.loopDrops); 932 counter_u64_free(link->stats.memoryFailures); 933 free(link, M_NETGRAPH_BRIDGE); 934 priv->numLinks--; 935 936 /* If no more hooks, go away */ 937 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 938 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) 939 && !priv->persistent) { 940 ng_rmnode_self(NG_HOOK_NODE(hook)); 941 } 942 return (0); 943 } 944 945 /****************************************************************** 946 HASH TABLE FUNCTIONS 947 ******************************************************************/ 948 949 /* 950 * Hash algorithm 951 */ 952 #define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \ 953 ^ ((const u_int16_t *)(addr))[1] \ 954 ^ ((const u_int16_t *)(addr))[2]) & (mask) ) 955 956 /* 957 * Find a host entry in the table. 958 */ 959 static struct ng_bridge_host * 960 ng_bridge_get(priv_cp priv, const u_char *addr) 961 { 962 const int bucket = HASH(addr, priv->hashMask); 963 struct ng_bridge_host *host; 964 965 SLIST_FOREACH(host, &priv->tab[bucket], next) { 966 if (ETHER_EQUAL(host->addr, addr)) 967 return (host); 968 } 969 return (NULL); 970 } 971 972 /* 973 * Add a host entry to the table. If it already exists, move it 974 * to the new link. Returns 0 on success. 975 */ 976 static int 977 ng_bridge_put(priv_p priv, const u_char *addr, link_p link) 978 { 979 const int bucket = HASH(addr, priv->hashMask); 980 struct ng_bridge_host *host; 981 982 if ((host = ng_bridge_get(priv, addr)) != NULL) { 983 /* Host already on the correct link? */ 984 if (host->link == link) 985 return 0; 986 987 /* Move old host over to new link */ 988 if (host->age >= priv->conf.minStableAge) { 989 host->link = link; 990 host->age = 0; 991 return (0); 992 } 993 /* 994 * If the host was recently moved to the old link and 995 * it's now jumping to a new link, declare a loopback 996 * condition. 997 */ 998 if (priv->conf.debugLevel >= 2) 999 log(LOG_WARNING, "ng_bridge: %s:" 1000 " loopback detected on %s\n", 1001 ng_bridge_nodename(priv->node), 1002 NG_HOOK_NAME(link->hook)); 1003 1004 /* Mark link as linka non grata */ 1005 link->loopCount = priv->conf.loopTimeout; 1006 link->stats.loopDetects++; 1007 1008 /* Forget all hosts on this link */ 1009 ng_bridge_remove_hosts(priv, link); 1010 return (ELOOP); 1011 } 1012 1013 /* Allocate and initialize new hashtable entry */ 1014 host = malloc(sizeof(*host), M_NETGRAPH_BRIDGE, M_NOWAIT); 1015 if (host == NULL) 1016 return (ENOMEM); 1017 bcopy(addr, host->addr, ETHER_ADDR_LEN); 1018 host->link = link; 1019 host->staleness = 0; 1020 host->age = 0; 1021 1022 /* Add new element to hash bucket */ 1023 SLIST_INSERT_HEAD(&priv->tab[bucket], host, next); 1024 priv->numHosts++; 1025 1026 /* Resize table if necessary */ 1027 ng_bridge_rehash(priv); 1028 return (0); 1029 } 1030 1031 /* 1032 * Resize the hash table. We try to maintain the number of buckets 1033 * such that the load factor is in the range 0.25 to 1.0. 1034 * 1035 * If we can't get the new memory then we silently fail. This is OK 1036 * because things will still work and we'll try again soon anyway. 1037 */ 1038 static void 1039 ng_bridge_rehash(priv_p priv) 1040 { 1041 struct ng_bridge_bucket *newTab; 1042 int oldBucket, newBucket; 1043 int newNumBuckets; 1044 u_int newMask; 1045 1046 /* Is table too full or too empty? */ 1047 if (priv->numHosts > priv->numBuckets 1048 && (priv->numBuckets << 1) <= MAX_BUCKETS) 1049 newNumBuckets = priv->numBuckets << 1; 1050 else if (priv->numHosts < (priv->numBuckets >> 2) 1051 && (priv->numBuckets >> 2) >= MIN_BUCKETS) 1052 newNumBuckets = priv->numBuckets >> 2; 1053 else 1054 return; 1055 newMask = newNumBuckets - 1; 1056 1057 /* Allocate and initialize new table */ 1058 newTab = malloc(newNumBuckets * sizeof(*newTab), 1059 M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 1060 if (newTab == NULL) 1061 return; 1062 1063 /* Move all entries from old table to new table */ 1064 for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) { 1065 struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket]; 1066 1067 while (!SLIST_EMPTY(oldList)) { 1068 struct ng_bridge_host *const host 1069 = SLIST_FIRST(oldList); 1070 1071 SLIST_REMOVE_HEAD(oldList, next); 1072 newBucket = HASH(host->addr, newMask); 1073 SLIST_INSERT_HEAD(&newTab[newBucket], host, next); 1074 } 1075 } 1076 1077 /* Replace old table with new one */ 1078 if (priv->conf.debugLevel >= 3) { 1079 log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n", 1080 ng_bridge_nodename(priv->node), 1081 priv->numBuckets, newNumBuckets); 1082 } 1083 free(priv->tab, M_NETGRAPH_BRIDGE); 1084 priv->numBuckets = newNumBuckets; 1085 priv->hashMask = newMask; 1086 priv->tab = newTab; 1087 return; 1088 } 1089 1090 /****************************************************************** 1091 MISC FUNCTIONS 1092 ******************************************************************/ 1093 1094 /* 1095 * Remove all hosts associated with a specific link from the hashtable. 1096 * If linkNum == -1, then remove all hosts in the table. 1097 */ 1098 static void 1099 ng_bridge_remove_hosts(priv_p priv, link_p link) 1100 { 1101 int bucket; 1102 1103 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 1104 struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]); 1105 1106 while (*hptr != NULL) { 1107 struct ng_bridge_host *const host = *hptr; 1108 1109 if (link == NULL || host->link == link) { 1110 *hptr = SLIST_NEXT(host, next); 1111 free(host, M_NETGRAPH_BRIDGE); 1112 priv->numHosts--; 1113 } else 1114 hptr = &SLIST_NEXT(host, next); 1115 } 1116 } 1117 } 1118 1119 /* 1120 * Handle our once-per-second timeout event. We do two things: 1121 * we decrement link->loopCount for those links being muted due to 1122 * a detected loopback condition, and we remove any hosts from 1123 * the hashtable whom we haven't heard from in a long while. 1124 */ 1125 static int 1126 ng_bridge_unmute(hook_p hook, void *arg) 1127 { 1128 link_p link = NG_HOOK_PRIVATE(hook); 1129 node_p node = NG_HOOK_NODE(hook); 1130 priv_p priv = NG_NODE_PRIVATE(node); 1131 int *counter = arg; 1132 1133 if (link->loopCount != 0) { 1134 link->loopCount--; 1135 if (link->loopCount == 0 && priv->conf.debugLevel >= 2) { 1136 log(LOG_INFO, "ng_bridge: %s:" 1137 " restoring looped back %s\n", 1138 ng_bridge_nodename(node), NG_HOOK_NAME(hook)); 1139 } 1140 } 1141 (*counter)++; 1142 return (1); 1143 } 1144 1145 static void 1146 ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2) 1147 { 1148 const priv_p priv = NG_NODE_PRIVATE(node); 1149 int bucket; 1150 int counter = 0; 1151 hook_p ret; 1152 1153 /* Update host time counters and remove stale entries */ 1154 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 1155 struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]); 1156 1157 while (*hptr != NULL) { 1158 struct ng_bridge_host *const host = *hptr; 1159 1160 /* Remove hosts we haven't heard from in a while */ 1161 if (++host->staleness >= priv->conf.maxStaleness) { 1162 *hptr = SLIST_NEXT(host, next); 1163 free(host, M_NETGRAPH_BRIDGE); 1164 priv->numHosts--; 1165 } else { 1166 if (host->age < 0xffff) 1167 host->age++; 1168 hptr = &SLIST_NEXT(host, next); 1169 counter++; 1170 } 1171 } 1172 } 1173 KASSERT(priv->numHosts == counter, 1174 ("%s: hosts: %d != %d", __func__, priv->numHosts, counter)); 1175 1176 /* Decrease table size if necessary */ 1177 ng_bridge_rehash(priv); 1178 1179 /* Decrease loop counter on muted looped back links */ 1180 counter = 0; 1181 NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter, ret); 1182 KASSERT(priv->numLinks == counter, 1183 ("%s: links: %d != %d", __func__, priv->numLinks, counter)); 1184 1185 /* Register a new timeout, keeping the existing node reference */ 1186 ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 1187 } 1188 1189 /* 1190 * Return node's "name", even if it doesn't have one. 1191 */ 1192 static const char * 1193 ng_bridge_nodename(node_cp node) 1194 { 1195 static char name[NG_NODESIZ]; 1196 1197 if (NG_NODE_HAS_NAME(node)) 1198 snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node)); 1199 else 1200 snprintf(name, sizeof(name), "[%x]", ng_node2ID(node)); 1201 return name; 1202 } 1203