ng_bridge.c revision 70870
1141098Sdes 2141098Sdes/* 3117610Sdes * ng_bridge.c 4141098Sdes * 5141098Sdes * Copyright (c) 2000 Whistle Communications, Inc. 6117610Sdes * All rights reserved. 7117610Sdes * 8117610Sdes * Subject to the following obligations and disclaimer of warranty, use and 9117610Sdes * redistribution of this software, in source or object code forms, with or 10117610Sdes * without modifications are expressly permitted by Whistle Communications; 11117610Sdes * provided, however, that: 12117610Sdes * 1. Any and all reproductions of the source or object code must include the 13117610Sdes * copyright notice above and the following disclaimer of warranties; and 14117610Sdes * 2. No rights are granted, in any manner or form, to use Whistle 15117610Sdes * Communications, Inc. trademarks, including the mark "WHISTLE 16117610Sdes * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 17147455Sdes * such appears in the above copyright notice or in the software. 18147455Sdes * 19141098Sdes * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 20117610Sdes * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 21117610Sdes * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 22117610Sdes * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 23117610Sdes * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 24117610Sdes * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 25117610Sdes * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 26117610Sdes * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 27141098Sdes * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 28117610Sdes * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 29141098Sdes * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 30141098Sdes * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 31141098Sdes * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 32117610Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33141098Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34117610Sdes * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 35117610Sdes * OF SUCH DAMAGE. 36117610Sdes * 37117610Sdes * Author: Archie Cobbs <archie@freebsd.org> 38117610Sdes * 39117610Sdes * $FreeBSD: head/sys/netgraph/ng_bridge.c 70870 2001-01-10 07:13:58Z julian $ 40117610Sdes */ 41117610Sdes 42117610Sdes/* 43141098Sdes * ng_bridge(4) netgraph node type 44141098Sdes * 45141098Sdes * The node performs standard intelligent Ethernet bridging over 46141098Sdes * each of its connected hooks, or links. A simple loop detection 47141098Sdes * algorithm is included which disables a link for priv->conf.loopTimeout 48141098Sdes * seconds when a host is seen to have jumped from one link to 49141098Sdes * another within priv->conf.minStableAge seconds. 50141098Sdes * 51141098Sdes * We keep a hashtable that maps Ethernet addresses to host info, 52141098Sdes * which is contained in struct ng_bridge_host's. These structures 53141098Sdes * tell us on which link the host may be found. A host's entry will 54141098Sdes * expire after priv->conf.maxStaleness seconds. 55141098Sdes * 56141098Sdes * This node is optimzed for stable networks, where machines jump 57141098Sdes * from one port to the other only rarely. 58141098Sdes */ 59141098Sdes 60141098Sdes#include <sys/param.h> 61141098Sdes#include <sys/systm.h> 62141098Sdes#include <sys/kernel.h> 63141098Sdes#include <sys/malloc.h> 64141098Sdes#include <sys/mbuf.h> 65141098Sdes#include <sys/errno.h> 66141098Sdes#include <sys/syslog.h> 67141098Sdes#include <sys/socket.h> 68141098Sdes#include <sys/ctype.h> 69141098Sdes 70141098Sdes#include <net/if.h> 71141098Sdes#include <net/ethernet.h> 72141098Sdes 73141098Sdes#include <netinet/in.h> 74141098Sdes#include <netinet/ip_fw.h> 75141098Sdes 76141098Sdes#include <netgraph/ng_message.h> 77141098Sdes#include <netgraph/netgraph.h> 78141098Sdes#include <netgraph/ng_parse.h> 79141098Sdes#include <netgraph/ng_bridge.h> 80141098Sdes#include <netgraph/ng_ether.h> 81117610Sdes 82141098Sdes#ifdef NG_SEPARATE_MALLOC 83141098SdesMALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge", "netgraph bridge node "); 84141098Sdes#else 85141098Sdes#define M_NETGRAPH_BRIDGE M_NETGRAPH 86117610Sdes#endif 87117610Sdes 88141098Sdes/* Per-link private data */ 89117610Sdesstruct ng_bridge_link { 90141098Sdes hook_p hook; /* netgraph hook */ 91117610Sdes u_int16_t loopCount; /* loop ignore timer */ 92117610Sdes struct ng_bridge_link_stats stats; /* link stats */ 93141098Sdes}; 94141098Sdes 95141098Sdes/* Per-node private data */ 96141098Sdesstruct ng_bridge_private { 97141098Sdes struct ng_bridge_bucket *tab; /* hash table bucket array */ 98141098Sdes struct ng_bridge_link *links[NG_BRIDGE_MAX_LINKS]; 99117610Sdes struct ng_bridge_config conf; /* node configuration */ 100117610Sdes node_p node; /* netgraph node */ 101141098Sdes u_int numHosts; /* num entries in table */ 102141098Sdes u_int numBuckets; /* num buckets in table */ 103141098Sdes u_int hashMask; /* numBuckets - 1 */ 104141098Sdes int numLinks; /* num connected links */ 105141098Sdes struct callout timer; /* one second periodic timer */ 106117610Sdes}; 107141098Sdestypedef struct ng_bridge_private *priv_p; 108141098Sdes 109141098Sdes/* Information about a host, stored in a hash table entry */ 110141098Sdesstruct ng_bridge_hent { 111141098Sdes struct ng_bridge_host host; /* actual host info */ 112117610Sdes SLIST_ENTRY(ng_bridge_hent) next; /* next entry in bucket */ 113141098Sdes}; 114141098Sdes 115141098Sdes/* Hash table bucket declaration */ 116117610SdesSLIST_HEAD(ng_bridge_bucket, ng_bridge_hent); 117117610Sdes 118117610Sdes/* Netgraph node methods */ 119141098Sdesstatic ng_constructor_t ng_bridge_constructor; 120141098Sdesstatic ng_rcvmsg_t ng_bridge_rcvmsg; 121117610Sdesstatic ng_shutdown_t ng_bridge_shutdown; 122117610Sdesstatic ng_newhook_t ng_bridge_newhook; 123117610Sdesstatic ng_rcvdata_t ng_bridge_rcvdata; 124141098Sdesstatic ng_disconnect_t ng_bridge_disconnect; 125141098Sdes 126141098Sdes/* Other internal functions */ 127141098Sdesstatic struct ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr); 128141098Sdesstatic int ng_bridge_put(priv_p priv, const u_char *addr, int linkNum); 129141098Sdesstatic void ng_bridge_rehash(priv_p priv); 130117610Sdesstatic void ng_bridge_remove_hosts(priv_p priv, int linkNum); 131141098Sdesstatic void ng_bridge_timeout(void *arg); 132141098Sdesstatic const char *ng_bridge_nodename(node_p node); 133141098Sdes 134117610Sdes/* Ethernet broadcast */ 135141098Sdesstatic const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] = 136141098Sdes { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 137141098Sdes 138141098Sdes/* Store each hook's link number in the private field */ 139141098Sdes#define LINK_NUM(hook) (*(u_int16_t *)(&(hook)->private)) 140141098Sdes 141141098Sdes/* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */ 142141098Sdes#define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \ 143141098Sdes == ((const u_int32_t *)(b))[0] \ 144141098Sdes && ((const u_int16_t *)(a))[2] \ 145141098Sdes == ((const u_int16_t *)(b))[2]) 146141098Sdes 147141098Sdes/* Minimum and maximum number of hash buckets. Must be a power of two. */ 148141098Sdes#define MIN_BUCKETS (1 << 5) /* 32 */ 149117610Sdes#define MAX_BUCKETS (1 << 14) /* 16384 */ 150141098Sdes 151117610Sdes/* Configuration default values */ 152141098Sdes#define DEFAULT_LOOP_TIMEOUT 60 153141098Sdes#define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */ 154141098Sdes#define DEFAULT_MIN_STABLE_AGE 1 155141098Sdes 156141098Sdes/****************************************************************** 157141098Sdes NETGRAPH PARSE TYPES 158141098Sdes******************************************************************/ 159141098Sdes 160141098Sdes/* 161141098Sdes * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE 162141098Sdes */ 163141098Sdesstatic int 164141098Sdesng_bridge_getTableLength(const struct ng_parse_type *type, 165141098Sdes const u_char *start, const u_char *buf) 166141098Sdes{ 167117610Sdes const struct ng_bridge_host_ary *const hary 168141098Sdes = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t)); 169141098Sdes 170141098Sdes return hary->numHosts; 171141098Sdes} 172141098Sdes 173141098Sdes/* Parse type for struct ng_bridge_host_ary */ 174141098Sdesstatic const struct ng_parse_struct_info ng_bridge_host_type_info 175141098Sdes = NG_BRIDGE_HOST_TYPE_INFO(&ng_ether_enaddr_type); 176141098Sdesstatic const struct ng_parse_type ng_bridge_host_type = { 177141098Sdes &ng_parse_struct_type, 178141098Sdes &ng_bridge_host_type_info 179141098Sdes}; 180141098Sdesstatic const struct ng_parse_array_info ng_bridge_hary_type_info = { 181141098Sdes &ng_bridge_host_type, 182141098Sdes ng_bridge_getTableLength 183141098Sdes}; 184117610Sdesstatic const struct ng_parse_type ng_bridge_hary_type = { 185117610Sdes &ng_parse_array_type, 186117610Sdes &ng_bridge_hary_type_info 187117610Sdes}; 188117610Sdesstatic const struct ng_parse_struct_info ng_bridge_host_ary_type_info 189117610Sdes = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type); 190117610Sdesstatic const struct ng_parse_type ng_bridge_host_ary_type = { 191117610Sdes &ng_parse_struct_type, 192117610Sdes &ng_bridge_host_ary_type_info 193141098Sdes}; 194141098Sdes 195141098Sdes/* Parse type for struct ng_bridge_config */ 196141098Sdesstatic const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = { 197141098Sdes &ng_parse_uint8_type, 198141098Sdes NG_BRIDGE_MAX_LINKS 199141098Sdes}; 200141098Sdesstatic const struct ng_parse_type ng_bridge_ipfwary_type = { 201141098Sdes &ng_parse_fixedarray_type, 202141098Sdes &ng_bridge_ipfwary_type_info 203141098Sdes}; 204141098Sdesstatic const struct ng_parse_struct_info ng_bridge_config_type_info 205141098Sdes = NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type); 206141098Sdesstatic const struct ng_parse_type ng_bridge_config_type = { 207141098Sdes &ng_parse_struct_type, 208141098Sdes &ng_bridge_config_type_info 209141098Sdes}; 210141098Sdes 211141098Sdes/* Parse type for struct ng_bridge_link_stat */ 212141098Sdesstatic const struct ng_parse_struct_info 213141098Sdes ng_bridge_stats_type_info = NG_BRIDGE_STATS_TYPE_INFO; 214117610Sdesstatic const struct ng_parse_type ng_bridge_stats_type = { 215141098Sdes &ng_parse_struct_type, 216141098Sdes &ng_bridge_stats_type_info 217117610Sdes}; 218141098Sdes 219141098Sdes/* List of commands and how to convert arguments to/from ASCII */ 220141098Sdesstatic const struct ng_cmdlist ng_bridge_cmdlist[] = { 221141098Sdes { 222117610Sdes NGM_BRIDGE_COOKIE, 223117610Sdes NGM_BRIDGE_SET_CONFIG, 224141098Sdes "setconfig", 225117610Sdes &ng_bridge_config_type, 226117610Sdes NULL 227141098Sdes }, 228141098Sdes { 229141098Sdes NGM_BRIDGE_COOKIE, 230117610Sdes NGM_BRIDGE_GET_CONFIG, 231117610Sdes "getconfig", 232117610Sdes NULL, 233117610Sdes &ng_bridge_config_type 234117610Sdes }, 235117610Sdes { 236141098Sdes NGM_BRIDGE_COOKIE, 237141098Sdes NGM_BRIDGE_RESET, 238141098Sdes "reset", 239117610Sdes NULL, 240117610Sdes NULL 241117610Sdes }, 242117610Sdes { 243141098Sdes NGM_BRIDGE_COOKIE, 244141098Sdes NGM_BRIDGE_GET_STATS, 245141098Sdes "getstats", 246141098Sdes &ng_parse_uint32_type, 247141098Sdes &ng_bridge_stats_type 248141098Sdes }, 249117610Sdes { 250117610Sdes NGM_BRIDGE_COOKIE, 251117610Sdes NGM_BRIDGE_CLR_STATS, 252117610Sdes "clrstats", 253141098Sdes &ng_parse_uint32_type, 254117610Sdes NULL 255117610Sdes }, 256117610Sdes { 257117610Sdes NGM_BRIDGE_COOKIE, 258141098Sdes NGM_BRIDGE_GETCLR_STATS, 259117610Sdes "getclrstats", 260117610Sdes &ng_parse_uint32_type, 261141098Sdes &ng_bridge_stats_type 262141098Sdes }, 263141098Sdes { 264141098Sdes NGM_BRIDGE_COOKIE, 265141098Sdes NGM_BRIDGE_GET_TABLE, 266141098Sdes "gettable", 267117610Sdes NULL, 268117610Sdes &ng_bridge_host_ary_type 269141098Sdes }, 270141098Sdes { 0 } 271141098Sdes}; 272141098Sdes 273141098Sdes/* Node type descriptor */ 274141098Sdesstatic struct ng_type ng_bridge_typestruct = { 275117610Sdes NG_ABI_VERSION, 276117610Sdes NG_BRIDGE_NODE_TYPE, 277141098Sdes NULL, 278141098Sdes ng_bridge_constructor, 279141098Sdes ng_bridge_rcvmsg, 280141098Sdes ng_bridge_shutdown, 281141098Sdes ng_bridge_newhook, 282141098Sdes NULL, 283141098Sdes NULL, 284141098Sdes ng_bridge_rcvdata, 285141098Sdes ng_bridge_disconnect, 286141098Sdes ng_bridge_cmdlist, 287141098Sdes}; 288141098SdesNETGRAPH_INIT(bridge, &ng_bridge_typestruct); 289141098Sdes 290141098Sdes/* Depend on ng_ether so we can use the Ethernet parse type */ 291141098SdesMODULE_DEPEND(ng_bridge, ng_ether, 1, 1, 1); 292117610Sdes 293117610Sdes/****************************************************************** 294117610Sdes NETGRAPH NODE METHODS 295141098Sdes******************************************************************/ 296117610Sdes 297117610Sdes/* 298117610Sdes * Node constructor 299117610Sdes */ 300117610Sdesstatic int 301141098Sdesng_bridge_constructor(node_p node) 302141098Sdes{ 303117610Sdes priv_p priv; 304117610Sdes 305117610Sdes /* Allocate and initialize private info */ 306117610Sdes MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 307117610Sdes if (priv == NULL) 308141098Sdes return (ENOMEM); 309117610Sdes callout_init(&priv->timer, 0); 310117610Sdes 311117610Sdes /* Allocate and initialize hash table, etc. */ 312117610Sdes MALLOC(priv->tab, struct ng_bridge_bucket *, 313117610Sdes MIN_BUCKETS * sizeof(*priv->tab), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 314141098Sdes if (priv->tab == NULL) { 315141098Sdes FREE(priv, M_NETGRAPH_BRIDGE); 316141098Sdes return (ENOMEM); 317141098Sdes } 318141098Sdes priv->numBuckets = MIN_BUCKETS; 319141098Sdes priv->hashMask = MIN_BUCKETS - 1; 320141098Sdes priv->conf.debugLevel = 1; 321141098Sdes priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT; 322141098Sdes priv->conf.maxStaleness = DEFAULT_MAX_STALENESS; 323141098Sdes priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE; 324141098Sdes 325141098Sdes /* 326141098Sdes * This node has all kinds of stuff that could be screwed by SMP. 327141098Sdes * Until it gets it's own internal protection, we go through in 328141098Sdes * single file. This could hurt a machine bridging beteen two 329141098Sdes * GB ethernets so it should be fixed. 330141098Sdes * When it's fixed the process SHOULD NOT SLEEP, spinlocks please! 331141098Sdes * (and atomic ops ) 332141098Sdes */ 333117610Sdes NG_NODE_FORCE_WRITER(node); 334117610Sdes NG_NODE_SET_PRIVATE(node, priv); 335141098Sdes priv->node = node; 336117610Sdes 337117610Sdes /* Start timer by faking a timeout event */ 338117610Sdes NG_NODE_REF(node); /* because the timeout will drop a reference */ 339117610Sdes ng_bridge_timeout(node); 340141098Sdes return (0); 341117610Sdes} 342117610Sdes 343141098Sdes/* 344141098Sdes * Method for attaching a new hook 345141098Sdes */ 346141098Sdesstatic int 347141098Sdesng_bridge_newhook(node_p node, hook_p hook, const char *name) 348141098Sdes{ 349141098Sdes const priv_p priv = NG_NODE_PRIVATE(node); 350141098Sdes 351117610Sdes /* Check for a link hook */ 352117610Sdes if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX, 353141098Sdes strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) { 354141098Sdes const char *cp; 355141098Sdes char *eptr; 356141098Sdes u_long linkNum; 357117610Sdes 358117610Sdes cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX); 359141098Sdes if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 360141098Sdes return (EINVAL); 361141098Sdes linkNum = strtoul(cp, &eptr, 10); 362141098Sdes if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS) 363117610Sdes return (EINVAL); 364117610Sdes if (priv->links[linkNum] != NULL) 365117610Sdes return (EISCONN); 366117610Sdes MALLOC(priv->links[linkNum], struct ng_bridge_link *, 367117610Sdes sizeof(*priv->links[linkNum]), M_NETGRAPH_BRIDGE, M_NOWAIT|M_ZERO); 368117610Sdes if (priv->links[linkNum] == NULL) 369117610Sdes return (ENOMEM); 370117610Sdes priv->links[linkNum]->hook = hook; 371117610Sdes NG_HOOK_SET_PRIVATE(hook, (void *)linkNum); 372117610Sdes priv->numLinks++; 373141098Sdes return (0); 374141098Sdes } 375141098Sdes 376117610Sdes /* Unknown hook name */ 377117610Sdes return (EINVAL); 378117610Sdes} 379117610Sdes 380117610Sdes/* 381117610Sdes * Receive a control message 382117610Sdes */ 383117610Sdesstatic int 384117610Sdesng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook) 385117610Sdes{ 386117610Sdes const priv_p priv = NG_NODE_PRIVATE(node); 387141098Sdes struct ng_mesg *resp = NULL; 388117610Sdes int error = 0; 389117610Sdes struct ng_mesg *msg; 390117610Sdes 391117610Sdes NGI_GET_MSG(item, msg); 392117610Sdes switch (msg->header.typecookie) { 393117610Sdes case NGM_BRIDGE_COOKIE: 394117610Sdes switch (msg->header.cmd) { 395141098Sdes case NGM_BRIDGE_GET_CONFIG: 396117610Sdes { 397117610Sdes struct ng_bridge_config *conf; 398117610Sdes 399117610Sdes NG_MKRESPONSE(resp, msg, 400117610Sdes sizeof(struct ng_bridge_config), M_NOWAIT); 401117610Sdes if (resp == NULL) { 402117610Sdes error = ENOMEM; 403117610Sdes break; 404117610Sdes } 405117610Sdes conf = (struct ng_bridge_config *)resp->data; 406141098Sdes *conf = priv->conf; /* no sanity checking needed */ 407141098Sdes break; 408141098Sdes } 409141098Sdes case NGM_BRIDGE_SET_CONFIG: 410117610Sdes { 411117610Sdes struct ng_bridge_config *conf; 412117610Sdes int i; 413117610Sdes 414117610Sdes if (msg->header.arglen 415141098Sdes != sizeof(struct ng_bridge_config)) { 416141098Sdes error = EINVAL; 417117610Sdes break; 418117610Sdes } 419117610Sdes conf = (struct ng_bridge_config *)msg->data; 420117610Sdes priv->conf = *conf; 421117610Sdes for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) 422117610Sdes priv->conf.ipfw[i] = !!priv->conf.ipfw[i]; 423117610Sdes break; 424117610Sdes } 425117610Sdes case NGM_BRIDGE_RESET: 426117610Sdes { 427117610Sdes int i; 428117610Sdes 429117610Sdes /* Flush all entries in the hash table */ 430117610Sdes ng_bridge_remove_hosts(priv, -1); 431117610Sdes 432141098Sdes /* Reset all loop detection counters and stats */ 433141098Sdes for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) { 434117610Sdes if (priv->links[i] == NULL) 435117610Sdes continue; 436117610Sdes priv->links[i]->loopCount = 0; 437117610Sdes bzero(&priv->links[i]->stats, 438117610Sdes sizeof(priv->links[i]->stats)); 439117610Sdes } 440117610Sdes break; 441141098Sdes } 442141098Sdes case NGM_BRIDGE_GET_STATS: 443141098Sdes case NGM_BRIDGE_CLR_STATS: 444141098Sdes case NGM_BRIDGE_GETCLR_STATS: 445141098Sdes { 446141098Sdes struct ng_bridge_link *link; 447141098Sdes int linkNum; 448141098Sdes 449117610Sdes /* Get link number */ 450117610Sdes if (msg->header.arglen != sizeof(u_int32_t)) { 451141098Sdes error = EINVAL; 452141098Sdes break; 453141098Sdes } 454141098Sdes linkNum = *((u_int32_t *)msg->data); 455141098Sdes if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) { 456141098Sdes error = EINVAL; 457141098Sdes break; 458141098Sdes } 459117610Sdes if ((link = priv->links[linkNum]) == NULL) { 460141098Sdes error = ENOTCONN; 461141098Sdes break; 462141098Sdes } 463117610Sdes 464117610Sdes /* Get/clear stats */ 465117610Sdes if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) { 466117610Sdes NG_MKRESPONSE(resp, msg, 467 sizeof(link->stats), M_NOWAIT); 468 if (resp == NULL) { 469 error = ENOMEM; 470 break; 471 } 472 bcopy(&link->stats, 473 resp->data, sizeof(link->stats)); 474 } 475 if (msg->header.cmd != NGM_BRIDGE_GET_STATS) 476 bzero(&link->stats, sizeof(link->stats)); 477 break; 478 } 479 case NGM_BRIDGE_GET_TABLE: 480 { 481 struct ng_bridge_host_ary *ary; 482 struct ng_bridge_hent *hent; 483 int i = 0, bucket; 484 485 NG_MKRESPONSE(resp, msg, sizeof(*ary) 486 + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT); 487 if (resp == NULL) { 488 error = ENOMEM; 489 break; 490 } 491 ary = (struct ng_bridge_host_ary *)resp->data; 492 ary->numHosts = priv->numHosts; 493 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 494 SLIST_FOREACH(hent, &priv->tab[bucket], next) 495 ary->hosts[i++] = hent->host; 496 } 497 break; 498 } 499 default: 500 error = EINVAL; 501 break; 502 } 503 break; 504 default: 505 error = EINVAL; 506 break; 507 } 508 509 /* Done */ 510 NG_RESPOND_MSG(error, node, item, resp); 511 NG_FREE_MSG(msg); 512 return (error); 513} 514 515/* 516 * Receive data on a hook 517 */ 518static int 519ng_bridge_rcvdata(hook_p hook, item_p item) 520{ 521 const node_p node = NG_HOOK_NODE(hook); 522 const priv_p priv = NG_NODE_PRIVATE(node); 523 struct ng_bridge_host *host; 524 struct ng_bridge_link *link; 525 struct ether_header *eh; 526 int error = 0, linkNum; 527 int manycast; 528 struct mbuf *m; 529 meta_p meta; 530 struct ng_bridge_link *firstLink; 531 532 NGI_GET_M(item, m); 533 /* Get link number */ 534 linkNum = (int)NG_HOOK_PRIVATE(hook); 535 KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, 536 ("%s: linkNum=%u", __FUNCTION__, linkNum)); 537 link = priv->links[linkNum]; 538 KASSERT(link != NULL, ("%s: link%d null", __FUNCTION__, linkNum)); 539 540 /* Sanity check packet and pull up header */ 541 if (m->m_pkthdr.len < ETHER_HDR_LEN) { 542 link->stats.recvRunts++; 543 NG_FREE_ITEM(item); 544 NG_FREE_M(m); 545 return (EINVAL); 546 } 547 if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) { 548 link->stats.memoryFailures++; 549 NG_FREE_ITEM(item); 550 return (ENOBUFS); 551 } 552 eh = mtod(m, struct ether_header *); 553 if ((eh->ether_shost[0] & 1) != 0) { 554 link->stats.recvInvalid++; 555 NG_FREE_ITEM(item); 556 NG_FREE_M(m); 557 return (EINVAL); 558 } 559 560 /* Is link disabled due to a loopback condition? */ 561 if (link->loopCount != 0) { 562 link->stats.loopDrops++; 563 NG_FREE_ITEM(item); 564 NG_FREE_M(m); 565 return (ELOOP); /* XXX is this an appropriate error? */ 566 } 567 568 /* Update stats */ 569 link->stats.recvPackets++; 570 link->stats.recvOctets += m->m_pkthdr.len; 571 if ((manycast = (eh->ether_dhost[0] & 1)) != 0) { 572 if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) { 573 link->stats.recvBroadcasts++; 574 manycast = 2; 575 } else 576 link->stats.recvMulticasts++; 577 } 578 579 /* Look up packet's source Ethernet address in hashtable */ 580 if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) { 581 582 /* Update time since last heard from this host */ 583 host->staleness = 0; 584 585 /* Did host jump to a different link? */ 586 if (host->linkNum != linkNum) { 587 588 /* 589 * If the host's old link was recently established 590 * on the old link and it's already jumped to a new 591 * link, declare a loopback condition. 592 */ 593 if (host->age < priv->conf.minStableAge) { 594 595 /* Log the problem */ 596 if (priv->conf.debugLevel >= 2) { 597 struct ifnet *ifp = m->m_pkthdr.rcvif; 598 char suffix[32]; 599 600 if (ifp != NULL) 601 snprintf(suffix, sizeof(suffix), 602 " (%s%d)", ifp->if_name, 603 ifp->if_unit); 604 else 605 *suffix = '\0'; 606 log(LOG_WARNING, "ng_bridge: %s:" 607 " loopback detected on %s%s\n", 608 ng_bridge_nodename(node), 609 NG_HOOK_NAME(hook), suffix); 610 } 611 612 /* Mark link as linka non grata */ 613 link->loopCount = priv->conf.loopTimeout; 614 link->stats.loopDetects++; 615 616 /* Forget all hosts on this link */ 617 ng_bridge_remove_hosts(priv, linkNum); 618 619 /* Drop packet */ 620 link->stats.loopDrops++; 621 NG_FREE_ITEM(item); 622 NG_FREE_M(m); 623 return (ELOOP); /* XXX appropriate? */ 624 } 625 626 /* Move host over to new link */ 627 host->linkNum = linkNum; 628 host->age = 0; 629 } 630 } else { 631 if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) { 632 link->stats.memoryFailures++; 633 NG_FREE_ITEM(item); 634 NG_FREE_M(m); 635 return (ENOMEM); 636 } 637 } 638 639 /* Run packet through ipfw processing, if enabled */ 640 if (priv->conf.ipfw[linkNum] && fw_enable && ip_fw_chk_ptr != NULL) { 641 /* XXX not implemented yet */ 642 } 643 644 /* 645 * If unicast and destination host known, deliver to host's link, 646 * unless it is the same link as the packet came in on. 647 */ 648 if (!manycast) { 649 650 /* Determine packet destination link */ 651 if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) { 652 struct ng_bridge_link *const destLink 653 = priv->links[host->linkNum]; 654 655 /* If destination same as incoming link, do nothing */ 656 KASSERT(destLink != NULL, 657 ("%s: link%d null", __FUNCTION__, host->linkNum)); 658 if (destLink == link) { 659 NG_FREE_ITEM(item); 660 NG_FREE_M(m); 661 return (0); 662 } 663 664 /* Deliver packet out the destination link */ 665 destLink->stats.xmitPackets++; 666 destLink->stats.xmitOctets += m->m_pkthdr.len; 667 NG_FWD_NEW_DATA(error, item, destLink->hook, m); 668 return (error); 669 } 670 671 /* Destination host is not known */ 672 link->stats.recvUnknown++; 673 } 674 675 /* Distribute unknown, multicast, broadcast pkts to all other links */ 676 meta = NGI_META(item); /* peek.. */ 677 firstLink = NULL; 678 for (linkNum = 0; linkNum <= priv->numLinks; linkNum++) { 679 struct ng_bridge_link *destLink; 680 meta_p meta2 = NULL; 681 struct mbuf *m2 = NULL; 682 683 /* 684 * If we have checked all the links then now 685 * send the original on its reserved link 686 */ 687 if (linkNum == priv->numLinks) { 688 /* If we never saw a good link, leave. */ 689 if (firstLink == NULL) { 690 NG_FREE_ITEM(item); 691 NG_FREE_M(m); 692 return (0); 693 } 694 destLink = firstLink; 695 } else { 696 destLink = priv->links[linkNum]; 697 /* Skip incoming link and disconnected links */ 698 if (destLink == NULL || destLink == link) { 699 continue; 700 } 701 if (firstLink == NULL) { 702 /* 703 * This is the first usable link we have found. 704 * Reserve it for the originals. 705 * If we never find another we save a copy. 706 */ 707 firstLink = destLink; 708 continue; 709 } 710 711 /* 712 * It's usable link but not the reserved (first) one. 713 * Copy mbuf and meta info for sending. 714 */ 715 m2 = m_dup(m, M_NOWAIT); /* XXX m_copypacket() */ 716 if (m2 == NULL) { 717 link->stats.memoryFailures++; 718 NG_FREE_ITEM(item); 719 NG_FREE_M(m); 720 return (ENOBUFS); 721 } 722 if (meta != NULL 723 && (meta2 = ng_copy_meta(meta)) == NULL) { 724 link->stats.memoryFailures++; 725 m_freem(m2); 726 NG_FREE_ITEM(item); 727 NG_FREE_M(m); 728 return (ENOMEM); 729 } 730 } 731 732 /* Update stats */ 733 destLink->stats.xmitPackets++; 734 destLink->stats.xmitOctets += m->m_pkthdr.len; 735 switch (manycast) { 736 case 0: /* unicast */ 737 break; 738 case 1: /* multicast */ 739 destLink->stats.xmitMulticasts++; 740 break; 741 case 2: /* broadcast */ 742 destLink->stats.xmitBroadcasts++; 743 break; 744 } 745 746 /* Send packet */ 747 if (destLink == firstLink) { 748 /* 749 * If we've sent all the others, send the original 750 * on the first link we found. 751 */ 752 NG_FWD_NEW_DATA(error, item, destLink->hook, m); 753 break; /* always done last - not really needed. */ 754 } else { 755 NG_SEND_DATA(error, destLink->hook, m2, meta2); 756 } 757 } 758 return (error); 759} 760 761/* 762 * Shutdown node 763 */ 764static int 765ng_bridge_shutdown(node_p node) 766{ 767 const priv_p priv = NG_NODE_PRIVATE(node); 768 769 KASSERT(priv->numLinks == 0 && priv->numHosts == 0, 770 ("%s: numLinks=%d numHosts=%d", 771 __FUNCTION__, priv->numLinks, priv->numHosts)); 772 FREE(priv->tab, M_NETGRAPH_BRIDGE); 773 FREE(priv, M_NETGRAPH_BRIDGE); 774 NG_NODE_SET_PRIVATE(node, NULL); 775 NG_NODE_UNREF(node); 776 return (0); 777} 778 779/* 780 * Hook disconnection. 781 */ 782static int 783ng_bridge_disconnect(hook_p hook) 784{ 785 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 786 int linkNum; 787 788 /* Get link number */ 789 linkNum = (int)NG_HOOK_PRIVATE(hook); 790 KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, 791 ("%s: linkNum=%u", __FUNCTION__, linkNum)); 792 793 /* Remove all hosts associated with this link */ 794 ng_bridge_remove_hosts(priv, linkNum); 795 796 /* Free associated link information */ 797 KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __FUNCTION__)); 798 FREE(priv->links[linkNum], M_NETGRAPH_BRIDGE); 799 priv->links[linkNum] = NULL; 800 priv->numLinks--; 801 802 /* If no more hooks, go away */ 803 if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 804 && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) { 805 ng_rmnode_self(NG_HOOK_NODE(hook)); 806 } 807 return (0); 808} 809 810/****************************************************************** 811 HASH TABLE FUNCTIONS 812******************************************************************/ 813 814/* 815 * Hash algorithm 816 * 817 * Only hashing bytes 3-6 of the Ethernet address is sufficient and fast. 818 */ 819#define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \ 820 ^ ((const u_int16_t *)(addr))[1] \ 821 ^ ((const u_int16_t *)(addr))[2]) & (mask) ) 822 823/* 824 * Find a host entry in the table. 825 */ 826static struct ng_bridge_host * 827ng_bridge_get(priv_p priv, const u_char *addr) 828{ 829 const int bucket = HASH(addr, priv->hashMask); 830 struct ng_bridge_hent *hent; 831 832 SLIST_FOREACH(hent, &priv->tab[bucket], next) { 833 if (ETHER_EQUAL(hent->host.addr, addr)) 834 return (&hent->host); 835 } 836 return (NULL); 837} 838 839/* 840 * Add a new host entry to the table. This assumes the host doesn't 841 * already exist in the table. Returns 1 on success, 0 if there 842 * was a memory allocation failure. 843 */ 844static int 845ng_bridge_put(priv_p priv, const u_char *addr, int linkNum) 846{ 847 const int bucket = HASH(addr, priv->hashMask); 848 struct ng_bridge_hent *hent; 849 850#ifdef INVARIANTS 851 /* Assert that entry does not already exist in hashtable */ 852 SLIST_FOREACH(hent, &priv->tab[bucket], next) { 853 KASSERT(!ETHER_EQUAL(hent->host.addr, addr), 854 ("%s: entry %6D exists in table", __FUNCTION__, addr, ":")); 855 } 856#endif 857 858 /* Allocate and initialize new hashtable entry */ 859 MALLOC(hent, struct ng_bridge_hent *, 860 sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT); 861 if (hent == NULL) 862 return (0); 863 bcopy(addr, hent->host.addr, ETHER_ADDR_LEN); 864 hent->host.linkNum = linkNum; 865 hent->host.staleness = 0; 866 hent->host.age = 0; 867 868 /* Add new element to hash bucket */ 869 SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next); 870 priv->numHosts++; 871 872 /* Resize table if necessary */ 873 ng_bridge_rehash(priv); 874 return (1); 875} 876 877/* 878 * Resize the hash table. We try to maintain the number of buckets 879 * such that the load factor is in the range 0.25 to 1.0. 880 * 881 * If we can't get the new memory then we silently fail. This is OK 882 * because things will still work and we'll try again soon anyway. 883 */ 884static void 885ng_bridge_rehash(priv_p priv) 886{ 887 struct ng_bridge_bucket *newTab; 888 int oldBucket, newBucket; 889 int newNumBuckets; 890 u_int newMask; 891 892 /* Is table too full or too empty? */ 893 if (priv->numHosts > priv->numBuckets 894 && (priv->numBuckets << 1) <= MAX_BUCKETS) 895 newNumBuckets = priv->numBuckets << 1; 896 else if (priv->numHosts < (priv->numBuckets >> 2) 897 && (priv->numBuckets >> 2) >= MIN_BUCKETS) 898 newNumBuckets = priv->numBuckets >> 2; 899 else 900 return; 901 newMask = newNumBuckets - 1; 902 903 /* Allocate and initialize new table */ 904 MALLOC(newTab, struct ng_bridge_bucket *, 905 newNumBuckets * sizeof(*newTab), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 906 if (newTab == NULL) 907 return; 908 909 /* Move all entries from old table to new table */ 910 for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) { 911 struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket]; 912 913 while (!SLIST_EMPTY(oldList)) { 914 struct ng_bridge_hent *const hent 915 = SLIST_FIRST(oldList); 916 917 SLIST_REMOVE_HEAD(oldList, next); 918 newBucket = HASH(hent->host.addr, newMask); 919 SLIST_INSERT_HEAD(&newTab[newBucket], hent, next); 920 } 921 } 922 923 /* Replace old table with new one */ 924 if (priv->conf.debugLevel >= 3) { 925 log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n", 926 ng_bridge_nodename(priv->node), 927 priv->numBuckets, newNumBuckets); 928 } 929 FREE(priv->tab, M_NETGRAPH_BRIDGE); 930 priv->numBuckets = newNumBuckets; 931 priv->hashMask = newMask; 932 priv->tab = newTab; 933 return; 934} 935 936/****************************************************************** 937 MISC FUNCTIONS 938******************************************************************/ 939 940/* 941 * Remove all hosts associated with a specific link from the hashtable. 942 * If linkNum == -1, then remove all hosts in the table. 943 */ 944static void 945ng_bridge_remove_hosts(priv_p priv, int linkNum) 946{ 947 int bucket; 948 949 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 950 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 951 952 while (*hptr != NULL) { 953 struct ng_bridge_hent *const hent = *hptr; 954 955 if (linkNum == -1 || hent->host.linkNum == linkNum) { 956 *hptr = SLIST_NEXT(hent, next); 957 FREE(hent, M_NETGRAPH_BRIDGE); 958 priv->numHosts--; 959 } else 960 hptr = &SLIST_NEXT(hent, next); 961 } 962 } 963} 964 965/* 966 * Handle our once-per-second timeout event. We do two things: 967 * we decrement link->loopCount for those links being muted due to 968 * a detected loopback condition, and we remove any hosts from 969 * the hashtable whom we haven't heard from in a long while. 970 */ 971static void 972ng_bridge_timeout(void *arg) 973{ 974 const node_p node = arg; 975 const priv_p priv = NG_NODE_PRIVATE(node); 976 int s, bucket; 977 int counter = 0; 978 int linkNum; 979 980 /* Avoid race condition with ng_bridge_shutdown() */ 981 s = splnet(); 982 if ((NG_NODE_NOT_VALID(node)) || priv == NULL) { 983 NG_NODE_UNREF(node); 984 splx(s); 985 return; 986 } 987 988 /* Register a new timeout, keeping the existing node reference */ 989 callout_reset(&priv->timer, hz, ng_bridge_timeout, node); 990 991 /* Update host time counters and remove stale entries */ 992 for (bucket = 0; bucket < priv->numBuckets; bucket++) { 993 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 994 995 while (*hptr != NULL) { 996 struct ng_bridge_hent *const hent = *hptr; 997 998 /* Make sure host's link really exists */ 999 KASSERT(priv->links[hent->host.linkNum] != NULL, 1000 ("%s: host %6D on nonexistent link %d\n", 1001 __FUNCTION__, hent->host.addr, ":", 1002 hent->host.linkNum)); 1003 1004 /* Remove hosts we haven't heard from in a while */ 1005 if (++hent->host.staleness >= priv->conf.maxStaleness) { 1006 *hptr = SLIST_NEXT(hent, next); 1007 FREE(hent, M_NETGRAPH_BRIDGE); 1008 priv->numHosts--; 1009 } else { 1010 if (hent->host.age < 0xffff) 1011 hent->host.age++; 1012 hptr = &SLIST_NEXT(hent, next); 1013 counter++; 1014 } 1015 } 1016 } 1017 KASSERT(priv->numHosts == counter, 1018 ("%s: hosts: %d != %d", __FUNCTION__, priv->numHosts, counter)); 1019 1020 /* Decrease table size if necessary */ 1021 ng_bridge_rehash(priv); 1022 1023 /* Decrease loop counter on muted looped back links */ 1024 for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) { 1025 struct ng_bridge_link *const link = priv->links[linkNum]; 1026 1027 if (link != NULL) { 1028 if (link->loopCount != 0) { 1029 link->loopCount--; 1030 if (link->loopCount == 0 1031 && priv->conf.debugLevel >= 2) { 1032 log(LOG_INFO, "ng_bridge: %s:" 1033 " restoring looped back link%d\n", 1034 ng_bridge_nodename(node), linkNum); 1035 } 1036 } 1037 counter++; 1038 } 1039 } 1040 KASSERT(priv->numLinks == counter, 1041 ("%s: links: %d != %d", __FUNCTION__, priv->numLinks, counter)); 1042 1043 /* Done */ 1044 splx(s); 1045} 1046 1047/* 1048 * Return node's "name", even if it doesn't have one. 1049 */ 1050static const char * 1051ng_bridge_nodename(node_p node) 1052{ 1053 static char name[NG_NODELEN+1]; 1054 1055 if (NG_NODE_NAME(node) != NULL) 1056 snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node)); 1057 else 1058 snprintf(name, sizeof(name), "[%x]", ng_node2ID(node)); 1059 return name; 1060} 1061 1062