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_WAITOK | M_ZERO); 399 400 link->stats.recvOctets = counter_u64_alloc(M_WAITOK); 401 link->stats.recvPackets = counter_u64_alloc(M_WAITOK); 402 link->stats.recvMulticasts = counter_u64_alloc(M_WAITOK); 403 link->stats.recvBroadcasts = counter_u64_alloc(M_WAITOK); 404 link->stats.recvUnknown = counter_u64_alloc(M_WAITOK); 405 link->stats.recvRunts = counter_u64_alloc(M_WAITOK); 406 link->stats.recvInvalid = counter_u64_alloc(M_WAITOK); 407 link->stats.xmitOctets = counter_u64_alloc(M_WAITOK); 408 link->stats.xmitPackets = counter_u64_alloc(M_WAITOK); 409 link->stats.xmitMulticasts = counter_u64_alloc(M_WAITOK); 410 link->stats.xmitBroadcasts = counter_u64_alloc(M_WAITOK); 411 link->stats.loopDrops = counter_u64_alloc(M_WAITOK); 412 link->stats.memoryFailures = counter_u64_alloc(M_WAITOK); 413 414 link->hook = hook; 415 if (isUplink) { 416 link->learnMac = 0; 417 link->sendUnknown = 1; 418 if (priv->numLinks == 0) /* if the first link is an uplink */ 419 priv->sendUnknown = 0; /* switch to restrictive mode */ 420 } else { 421 link->learnMac = 1; 422 link->sendUnknown = priv->sendUnknown; 423 } 424 425 NG_HOOK_SET_PRIVATE(hook, link); 426 priv->numLinks++; 427 return (0); 428 } 429 430 /* 431 * Receive a control message 432 */ 433 static void ng_bridge_clear_link_stats(struct ng_bridge_link_kernel_stats * p) 434 { 435 counter_u64_zero(p->recvOctets); 436 counter_u64_zero(p->recvPackets); 437 counter_u64_zero(p->recvMulticasts); 438 counter_u64_zero(p->recvBroadcasts); 439 counter_u64_zero(p->recvUnknown); 440 counter_u64_zero(p->recvRunts); 441 counter_u64_zero(p->recvInvalid); 442 counter_u64_zero(p->xmitOctets); 443 counter_u64_zero(p->xmitPackets); 444 counter_u64_zero(p->xmitMulticasts); 445 counter_u64_zero(p->xmitBroadcasts); 446 counter_u64_zero(p->loopDrops); 447 p->loopDetects = 0; 448 counter_u64_zero(p->memoryFailures); 449 }; 450 451 static int 452 ng_bridge_reset_link(hook_p hook, void *arg __unused) 453 { 454 link_p priv = NG_HOOK_PRIVATE(hook); 455 456 priv->loopCount = 0; 457 ng_bridge_clear_link_stats(&priv->stats); 458 return (1); 459 } 460 461 static int 462 ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook) 463 { 464 const priv_p priv = NG_NODE_PRIVATE(node); 465 struct ng_mesg *resp = NULL; 466 int error = 0; 467 struct ng_mesg *msg; 468 469 NGI_GET_MSG(item, msg); 470 switch (msg->header.typecookie) { 471 case NGM_BRIDGE_COOKIE: 472 switch (msg->header.cmd) { 473 case NGM_BRIDGE_GET_CONFIG: 474 { 475 struct ng_bridge_config *conf; 476 477 NG_MKRESPONSE(resp, msg, 478 sizeof(struct ng_bridge_config), M_NOWAIT); 479 if (resp == NULL) { 480 error = ENOMEM; 481 break; 482 } 483 conf = (struct ng_bridge_config *)resp->data; 484 *conf = priv->conf; /* no sanity checking needed */ 485 break; 486 } 487 case NGM_BRIDGE_SET_CONFIG: 488 { 489 struct ng_bridge_config *conf; 490 491 if (msg->header.arglen 492 != sizeof(struct ng_bridge_config)) { 493 error = EINVAL; 494 break; 495 } 496 conf = (struct ng_bridge_config *)msg->data; 497 priv->conf = *conf; 498 break; 499 } 500 case NGM_BRIDGE_RESET: 501 { 502 hook_p rethook; 503 504 /* Flush all entries in the hash table */ 505 ng_bridge_remove_hosts(priv, NULL); 506 507 /* Reset all loop detection counters and stats */ 508 NG_NODE_FOREACH_HOOK(node, ng_bridge_reset_link, NULL, 509 rethook); 510 break; 511 } 512 case NGM_BRIDGE_GET_STATS: 513 case NGM_BRIDGE_CLR_STATS: 514 case NGM_BRIDGE_GETCLR_STATS: 515 { 516 hook_p hook; 517 link_p link; 518 char linkName[NG_HOOKSIZ]; 519 int linkNum; 520 521 /* Get link number */ 522 if (msg->header.arglen != sizeof(u_int32_t)) { 523 error = EINVAL; 524 break; 525 } 526 linkNum = *((int32_t *)msg->data); 527 if (linkNum < 0) 528 snprintf(linkName, sizeof(linkName), 529 "%s%u", NG_BRIDGE_HOOK_UPLINK_PREFIX, -linkNum); 530 else 531 snprintf(linkName, sizeof(linkName), 532 "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX, linkNum); 533 534 if ((hook = ng_findhook(node, linkName)) == NULL) { 535 error = ENOTCONN; 536 break; 537 } 538 link = NG_HOOK_PRIVATE(hook); 539 540 /* Get/clear stats */ 541 if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) { 542 struct ng_bridge_link_stats *rs; 543 544 NG_MKRESPONSE(resp, msg, 545 sizeof(link->stats), M_NOWAIT); 546 if (resp == NULL) { 547 error = ENOMEM; 548 break; 549 } 550 rs = (struct ng_bridge_link_stats *)resp->data; 551 #define FETCH(x) rs->x = counter_u64_fetch(link->stats.x) 552 FETCH(recvOctets); 553 FETCH(recvPackets); 554 FETCH(recvMulticasts); 555 FETCH(recvBroadcasts); 556 FETCH(recvUnknown); 557 FETCH(recvRunts); 558 FETCH(recvInvalid); 559 FETCH(xmitOctets); 560 FETCH(xmitPackets); 561 FETCH(xmitMulticasts); 562 FETCH(xmitBroadcasts); 563 FETCH(loopDrops); 564 rs->loopDetects = link->stats.loopDetects; 565 FETCH(memoryFailures); 566 #undef FETCH 567 } 568 if (msg->header.cmd != NGM_BRIDGE_GET_STATS) 569 ng_bridge_clear_link_stats(&link->stats); 570 break; 571 } 572 case NGM_BRIDGE_GET_TABLE: 573 { 574 struct ng_bridge_host_ary *ary; 575 struct ng_bridge_host *host; 576 int i = 0, bucket; 577 578 NG_MKRESPONSE(resp, msg, sizeof(*ary) 579 + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT); 580 if (resp == NULL) { 581 error = ENOMEM; 582 break; 583 } 584 ary = (struct ng_bridge_host_ary *)resp->data; 585 ary->numHosts = priv->numHosts; 586 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 587 SLIST_FOREACH(host, &priv->tab[bucket], next) { 588 memcpy(ary->hosts[i].addr, 589 host->addr, 590 sizeof(ary->hosts[i].addr)); 591 ary->hosts[i].age = host->age; 592 ary->hosts[i].staleness = host->staleness; 593 strncpy(ary->hosts[i].hook, 594 NG_HOOK_NAME(host->link->hook), 595 sizeof(ary->hosts[i].hook)); 596 i++; 597 } 598 } 599 break; 600 } 601 case NGM_BRIDGE_SET_PERSISTENT: 602 { 603 priv->persistent = 1; 604 break; 605 } 606 case NGM_BRIDGE_MOVE_HOST: 607 { 608 struct ng_bridge_move_host *mh; 609 hook_p hook; 610 611 if (msg->header.arglen < sizeof(*mh)) { 612 error = EINVAL; 613 break; 614 } 615 mh = (struct ng_bridge_move_host *)msg->data; 616 hook = (mh->hook[0] == 0) 617 ? lasthook 618 : ng_findhook(node, mh->hook); 619 if (hook == NULL) { 620 error = ENOENT; 621 break; 622 } 623 error = ng_bridge_put(priv, mh->addr, NG_HOOK_PRIVATE(hook)); 624 break; 625 } 626 default: 627 error = EINVAL; 628 break; 629 } 630 break; 631 default: 632 error = EINVAL; 633 break; 634 } 635 636 /* Done */ 637 NG_RESPOND_MSG(error, node, item, resp); 638 NG_FREE_MSG(msg); 639 return (error); 640 } 641 642 /* 643 * Receive data on a hook 644 */ 645 struct ng_bridge_send_ctx { 646 link_p foundFirst, incoming; 647 struct mbuf * m; 648 int manycast, error; 649 }; 650 651 /* 652 * Update stats and send out 653 */ 654 static inline int 655 ng_bridge_send_data(link_cp dst, int manycast, struct mbuf *m, item_p item) { 656 int error = 0; 657 size_t len = m->m_pkthdr.len; 658 659 if(item != NULL) 660 NG_FWD_NEW_DATA(error, item, dst->hook, m); 661 else 662 NG_SEND_DATA_ONLY(error, dst->hook, m); 663 664 if (error) { 665 /* The packet is still ours */ 666 if (item != NULL) 667 NG_FREE_ITEM(item); 668 if (m != NULL) 669 NG_FREE_M(m); 670 return (error); 671 } 672 673 counter_u64_add(dst->stats.xmitPackets, 1); 674 counter_u64_add(dst->stats.xmitOctets, len); 675 switch (manycast) { 676 default: /* unknown unicast */ 677 break; 678 case 1: /* multicast */ 679 counter_u64_add(dst->stats.xmitMulticasts, 1); 680 break; 681 case 2: /* broadcast */ 682 counter_u64_add(dst->stats.xmitBroadcasts, 1); 683 break; 684 } 685 return (0); 686 } 687 688 /* 689 * Loop body for sending to multiple destinations 690 * return 0 to stop looping 691 */ 692 static int 693 ng_bridge_send_ctx(hook_p dst, void *arg) 694 { 695 struct ng_bridge_send_ctx *ctx = arg; 696 link_p destLink = NG_HOOK_PRIVATE(dst); 697 struct mbuf *m2 = NULL; 698 int error = 0; 699 700 /* Skip incoming link */ 701 if (destLink == ctx->incoming) { 702 return (1); 703 } 704 705 /* Skip sending unknowns to undesired links */ 706 if (!ctx->manycast && !destLink->sendUnknown) 707 return (1); 708 709 if (ctx->foundFirst == NULL) { 710 /* 711 * This is the first usable link we have found. 712 * Reserve it for the originals. 713 * If we never find another we save a copy. 714 */ 715 ctx->foundFirst = destLink; 716 return (1); 717 } 718 719 /* 720 * It's usable link but not the reserved (first) one. 721 * Copy mbuf info for sending. 722 */ 723 m2 = m_dup(ctx->m, M_NOWAIT); 724 if (m2 == NULL) { 725 counter_u64_add(ctx->incoming->stats.memoryFailures, 1); 726 ctx->error = ENOBUFS; 727 return (0); /* abort loop, do not try again and again */ 728 } 729 730 /* Send packet */ 731 error = ng_bridge_send_data(destLink, ctx->manycast, m2, NULL); 732 if (error) 733 ctx->error = error; 734 return (1); 735 } 736 737 static int 738 ng_bridge_rcvdata(hook_p hook, item_p item) 739 { 740 const node_p node = NG_HOOK_NODE(hook); 741 const priv_p priv = NG_NODE_PRIVATE(node); 742 struct ng_bridge_host *host; 743 struct ether_header *eh; 744 struct ng_bridge_send_ctx ctx = { 0 }; 745 hook_p ret; 746 747 NGI_GET_M(item, ctx.m); 748 749 ctx.incoming = NG_HOOK_PRIVATE(hook); 750 /* Sanity check packet and pull up header */ 751 if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) { 752 counter_u64_add(ctx.incoming->stats.recvRunts, 1); 753 NG_FREE_ITEM(item); 754 NG_FREE_M(ctx.m); 755 return (EINVAL); 756 } 757 if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) { 758 counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 759 NG_FREE_ITEM(item); 760 return (ENOBUFS); 761 } 762 eh = mtod(ctx.m, struct ether_header *); 763 if ((eh->ether_shost[0] & 1) != 0) { 764 counter_u64_add(ctx.incoming->stats.recvInvalid, 1); 765 NG_FREE_ITEM(item); 766 NG_FREE_M(ctx.m); 767 return (EINVAL); 768 } 769 770 /* Is link disabled due to a loopback condition? */ 771 if (ctx.incoming->loopCount != 0) { 772 counter_u64_add(ctx.incoming->stats.loopDrops, 1); 773 NG_FREE_ITEM(item); 774 NG_FREE_M(ctx.m); 775 return (ELOOP); 776 } 777 778 /* Update stats */ 779 counter_u64_add(ctx.incoming->stats.recvPackets, 1); 780 counter_u64_add(ctx.incoming->stats.recvOctets, ctx.m->m_pkthdr.len); 781 if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) { 782 if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) { 783 counter_u64_add(ctx.incoming->stats.recvBroadcasts, 1); 784 ctx.manycast = 2; 785 } else 786 counter_u64_add(ctx.incoming->stats.recvMulticasts, 1); 787 } 788 789 /* Look up packet's source Ethernet address in hashtable */ 790 if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) 791 /* Update time since last heard from this host. 792 * This is safe without locking, because it's 793 * the only operation during shared access. 794 */ 795 if (__predict_false(host->staleness > 0)) 796 host->staleness = 0; 797 798 if ((host == NULL && ctx.incoming->learnMac) || 799 (host != NULL && host->link != ctx.incoming)) { 800 struct ng_mesg *msg; 801 struct ng_bridge_move_host *mh; 802 int error = 0; 803 804 NG_MKMESSAGE(msg, NGM_BRIDGE_COOKIE, NGM_BRIDGE_MOVE_HOST, 805 sizeof(*mh), M_NOWAIT); 806 if (msg == NULL) { 807 counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 808 NG_FREE_ITEM(item); 809 NG_FREE_M(ctx.m); 810 return (ENOMEM); 811 } 812 mh = (struct ng_bridge_move_host *)msg->data; 813 strncpy(mh->hook, NG_HOOK_NAME(ctx.incoming->hook), 814 sizeof(mh->hook)); 815 memcpy(mh->addr, eh->ether_shost, sizeof(mh->addr)); 816 NG_SEND_MSG_ID(error, node, msg, NG_NODE_ID(node), 817 NG_NODE_ID(node)); 818 if (error) 819 counter_u64_add(ctx.incoming->stats.memoryFailures, 1); 820 } 821 822 if (host != NULL && host->link != ctx.incoming) { 823 if (host->age < priv->conf.minStableAge) { 824 /* Drop packet on instable links */ 825 counter_u64_add(ctx.incoming->stats.loopDrops, 1); 826 NG_FREE_ITEM(item); 827 NG_FREE_M(ctx.m); 828 return (ELOOP); 829 } 830 } 831 832 /* Run packet through ipfw processing, if enabled */ 833 #if 0 834 if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) { 835 /* XXX not implemented yet */ 836 } 837 #endif 838 839 /* 840 * If unicast and destination host known, deliver to host's link, 841 * unless it is the same link as the packet came in on. 842 */ 843 if (!ctx.manycast) { 844 /* Determine packet destination link */ 845 if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) { 846 link_p destLink = host->link; 847 848 /* If destination same as incoming link, do nothing */ 849 if (destLink == ctx.incoming) { 850 NG_FREE_ITEM(item); 851 NG_FREE_M(ctx.m); 852 return (0); 853 } 854 855 /* Deliver packet out the destination link */ 856 return (ng_bridge_send_data(destLink, ctx.manycast, ctx.m, item)); 857 } 858 859 /* Destination host is not known */ 860 counter_u64_add(ctx.incoming->stats.recvUnknown, 1); 861 } 862 863 /* Distribute unknown, multicast, broadcast pkts to all other links */ 864 NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx, ret); 865 866 /* Finally send out on the first link found */ 867 if (ctx.foundFirst != NULL) { 868 int error = ng_bridge_send_data(ctx.foundFirst, ctx.manycast, ctx.m, item); 869 if (error) 870 ctx.error = error; 871 } else { /* nothing to send at all */ 872 NG_FREE_ITEM(item); 873 NG_FREE_M(ctx.m); 874 } 875 876 return (ctx.error); 877 } 878 879 /* 880 * Shutdown node 881 */ 882 static int 883 ng_bridge_shutdown(node_p node) 884 { 885 const priv_p priv = NG_NODE_PRIVATE(node); 886 887 /* 888 * Shut down everything including the timer. Even if the 889 * callout has already been dequeued and is about to be 890 * run, ng_bridge_timeout() won't be fired as the node 891 * is already marked NGF_INVALID, so we're safe to free 892 * the node now. 893 */ 894 KASSERT(priv->numLinks == 0 && priv->numHosts == 0, 895 ("%s: numLinks=%d numHosts=%d", 896 __func__, priv->numLinks, priv->numHosts)); 897 ng_uncallout(&priv->timer, node); 898 NG_NODE_SET_PRIVATE(node, NULL); 899 NG_NODE_UNREF(node); 900 free(priv->tab, M_NETGRAPH_BRIDGE); 901 free(priv, M_NETGRAPH_BRIDGE); 902 return (0); 903 } 904 905 /* 906 * Hook disconnection. 907 */ 908 static int 909 ng_bridge_disconnect(hook_p hook) 910 { 911 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 912 link_p link = NG_HOOK_PRIVATE(hook); 913 914 /* Remove all hosts associated with this link */ 915 ng_bridge_remove_hosts(priv, link); 916 917 /* Free associated link information */ 918 counter_u64_free(link->stats.recvOctets); 919 counter_u64_free(link->stats.recvPackets); 920 counter_u64_free(link->stats.recvMulticasts); 921 counter_u64_free(link->stats.recvBroadcasts); 922 counter_u64_free(link->stats.recvUnknown); 923 counter_u64_free(link->stats.recvRunts); 924 counter_u64_free(link->stats.recvInvalid); 925 counter_u64_free(link->stats.xmitOctets); 926 counter_u64_free(link->stats.xmitPackets); 927 counter_u64_free(link->stats.xmitMulticasts); 928 counter_u64_free(link->stats.xmitBroadcasts); 929 counter_u64_free(link->stats.loopDrops); 930 counter_u64_free(link->stats.memoryFailures); 931 free(link, M_NETGRAPH_BRIDGE); 932 priv->numLinks--; 933 934 /* If no more hooks, go away */ 935 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 936 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) 937 && !priv->persistent) { 938 ng_rmnode_self(NG_HOOK_NODE(hook)); 939 } 940 return (0); 941 } 942 943 /****************************************************************** 944 HASH TABLE FUNCTIONS 945 ******************************************************************/ 946 947 /* 948 * Hash algorithm 949 */ 950 #define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \ 951 ^ ((const u_int16_t *)(addr))[1] \ 952 ^ ((const u_int16_t *)(addr))[2]) & (mask) ) 953 954 /* 955 * Find a host entry in the table. 956 */ 957 static struct ng_bridge_host * 958 ng_bridge_get(priv_cp priv, const u_char *addr) 959 { 960 const int bucket = HASH(addr, priv->hashMask); 961 struct ng_bridge_host *host; 962 963 SLIST_FOREACH(host, &priv->tab[bucket], next) { 964 if (ETHER_EQUAL(host->addr, addr)) 965 return (host); 966 } 967 return (NULL); 968 } 969 970 /* 971 * Add a host entry to the table. If it already exists, move it 972 * to the new link. Returns 0 on success. 973 */ 974 static int 975 ng_bridge_put(priv_p priv, const u_char *addr, link_p link) 976 { 977 const int bucket = HASH(addr, priv->hashMask); 978 struct ng_bridge_host *host; 979 980 if ((host = ng_bridge_get(priv, addr)) != NULL) { 981 /* Host already on the correct link? */ 982 if (host->link == link) 983 return 0; 984 985 /* Move old host over to new link */ 986 if (host->age >= priv->conf.minStableAge) { 987 host->link = link; 988 host->age = 0; 989 return (0); 990 } 991 /* 992 * If the host was recently moved to the old link and 993 * it's now jumping to a new link, declare a loopback 994 * condition. 995 */ 996 if (priv->conf.debugLevel >= 2) 997 log(LOG_WARNING, "ng_bridge: %s:" 998 " loopback detected on %s\n", 999 ng_bridge_nodename(priv->node), 1000 NG_HOOK_NAME(link->hook)); 1001 1002 /* Mark link as linka non grata */ 1003 link->loopCount = priv->conf.loopTimeout; 1004 link->stats.loopDetects++; 1005 1006 /* Forget all hosts on this link */ 1007 ng_bridge_remove_hosts(priv, link); 1008 return (ELOOP); 1009 } 1010 1011 /* Allocate and initialize new hashtable entry */ 1012 host = malloc(sizeof(*host), M_NETGRAPH_BRIDGE, M_NOWAIT); 1013 if (host == NULL) 1014 return (ENOMEM); 1015 bcopy(addr, host->addr, ETHER_ADDR_LEN); 1016 host->link = link; 1017 host->staleness = 0; 1018 host->age = 0; 1019 1020 /* Add new element to hash bucket */ 1021 SLIST_INSERT_HEAD(&priv->tab[bucket], host, next); 1022 priv->numHosts++; 1023 1024 /* Resize table if necessary */ 1025 ng_bridge_rehash(priv); 1026 return (0); 1027 } 1028 1029 /* 1030 * Resize the hash table. We try to maintain the number of buckets 1031 * such that the load factor is in the range 0.25 to 1.0. 1032 * 1033 * If we can't get the new memory then we silently fail. This is OK 1034 * because things will still work and we'll try again soon anyway. 1035 */ 1036 static void 1037 ng_bridge_rehash(priv_p priv) 1038 { 1039 struct ng_bridge_bucket *newTab; 1040 int oldBucket, newBucket; 1041 int newNumBuckets; 1042 u_int newMask; 1043 1044 /* Is table too full or too empty? */ 1045 if (priv->numHosts > priv->numBuckets 1046 && (priv->numBuckets << 1) <= MAX_BUCKETS) 1047 newNumBuckets = priv->numBuckets << 1; 1048 else if (priv->numHosts < (priv->numBuckets >> 2) 1049 && (priv->numBuckets >> 2) >= MIN_BUCKETS) 1050 newNumBuckets = priv->numBuckets >> 2; 1051 else 1052 return; 1053 newMask = newNumBuckets - 1; 1054 1055 /* Allocate and initialize new table */ 1056 newTab = malloc(newNumBuckets * sizeof(*newTab), 1057 M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 1058 if (newTab == NULL) 1059 return; 1060 1061 /* Move all entries from old table to new table */ 1062 for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) { 1063 struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket]; 1064 1065 while (!SLIST_EMPTY(oldList)) { 1066 struct ng_bridge_host *const host 1067 = SLIST_FIRST(oldList); 1068 1069 SLIST_REMOVE_HEAD(oldList, next); 1070 newBucket = HASH(host->addr, newMask); 1071 SLIST_INSERT_HEAD(&newTab[newBucket], host, next); 1072 } 1073 } 1074 1075 /* Replace old table with new one */ 1076 if (priv->conf.debugLevel >= 3) { 1077 log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n", 1078 ng_bridge_nodename(priv->node), 1079 priv->numBuckets, newNumBuckets); 1080 } 1081 free(priv->tab, M_NETGRAPH_BRIDGE); 1082 priv->numBuckets = newNumBuckets; 1083 priv->hashMask = newMask; 1084 priv->tab = newTab; 1085 return; 1086 } 1087 1088 /****************************************************************** 1089 MISC FUNCTIONS 1090 ******************************************************************/ 1091 1092 /* 1093 * Remove all hosts associated with a specific link from the hashtable. 1094 * If linkNum == -1, then remove all hosts in the table. 1095 */ 1096 static void 1097 ng_bridge_remove_hosts(priv_p priv, link_p link) 1098 { 1099 int bucket; 1100 1101 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 1102 struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]); 1103 1104 while (*hptr != NULL) { 1105 struct ng_bridge_host *const host = *hptr; 1106 1107 if (link == NULL || host->link == link) { 1108 *hptr = SLIST_NEXT(host, next); 1109 free(host, M_NETGRAPH_BRIDGE); 1110 priv->numHosts--; 1111 } else 1112 hptr = &SLIST_NEXT(host, next); 1113 } 1114 } 1115 } 1116 1117 /* 1118 * Handle our once-per-second timeout event. We do two things: 1119 * we decrement link->loopCount for those links being muted due to 1120 * a detected loopback condition, and we remove any hosts from 1121 * the hashtable whom we haven't heard from in a long while. 1122 */ 1123 static int 1124 ng_bridge_unmute(hook_p hook, void *arg) 1125 { 1126 link_p link = NG_HOOK_PRIVATE(hook); 1127 node_p node = NG_HOOK_NODE(hook); 1128 priv_p priv = NG_NODE_PRIVATE(node); 1129 int *counter = arg; 1130 1131 if (link->loopCount != 0) { 1132 link->loopCount--; 1133 if (link->loopCount == 0 && priv->conf.debugLevel >= 2) { 1134 log(LOG_INFO, "ng_bridge: %s:" 1135 " restoring looped back %s\n", 1136 ng_bridge_nodename(node), NG_HOOK_NAME(hook)); 1137 } 1138 } 1139 (*counter)++; 1140 return (1); 1141 } 1142 1143 static void 1144 ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2) 1145 { 1146 const priv_p priv = NG_NODE_PRIVATE(node); 1147 int bucket; 1148 int counter = 0; 1149 hook_p ret; 1150 1151 /* Update host time counters and remove stale entries */ 1152 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 1153 struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]); 1154 1155 while (*hptr != NULL) { 1156 struct ng_bridge_host *const host = *hptr; 1157 1158 /* Remove hosts we haven't heard from in a while */ 1159 if (++host->staleness >= priv->conf.maxStaleness) { 1160 *hptr = SLIST_NEXT(host, next); 1161 free(host, M_NETGRAPH_BRIDGE); 1162 priv->numHosts--; 1163 } else { 1164 if (host->age < 0xffff) 1165 host->age++; 1166 hptr = &SLIST_NEXT(host, next); 1167 counter++; 1168 } 1169 } 1170 } 1171 KASSERT(priv->numHosts == counter, 1172 ("%s: hosts: %d != %d", __func__, priv->numHosts, counter)); 1173 1174 /* Decrease table size if necessary */ 1175 ng_bridge_rehash(priv); 1176 1177 /* Decrease loop counter on muted looped back links */ 1178 counter = 0; 1179 NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter, ret); 1180 KASSERT(priv->numLinks == counter, 1181 ("%s: links: %d != %d", __func__, priv->numLinks, counter)); 1182 1183 /* Register a new timeout, keeping the existing node reference */ 1184 ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 1185 } 1186 1187 /* 1188 * Return node's "name", even if it doesn't have one. 1189 */ 1190 static const char * 1191 ng_bridge_nodename(node_cp node) 1192 { 1193 static char name[NG_NODESIZ]; 1194 1195 if (NG_NODE_HAS_NAME(node)) 1196 snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node)); 1197 else 1198 snprintf(name, sizeof(name), "[%x]", ng_node2ID(node)); 1199 return name; 1200 } 1201