1ed2dbd31SArchie Cobbs /* 2ed2dbd31SArchie Cobbs * ng_bridge.c 3c398230bSWarner Losh */ 4c398230bSWarner Losh 5c398230bSWarner Losh /*- 6ed2dbd31SArchie Cobbs * Copyright (c) 2000 Whistle Communications, Inc. 7ed2dbd31SArchie Cobbs * All rights reserved. 8ed2dbd31SArchie Cobbs * 9ed2dbd31SArchie Cobbs * Subject to the following obligations and disclaimer of warranty, use and 10ed2dbd31SArchie Cobbs * redistribution of this software, in source or object code forms, with or 11ed2dbd31SArchie Cobbs * without modifications are expressly permitted by Whistle Communications; 12ed2dbd31SArchie Cobbs * provided, however, that: 13ed2dbd31SArchie Cobbs * 1. Any and all reproductions of the source or object code must include the 14ed2dbd31SArchie Cobbs * copyright notice above and the following disclaimer of warranties; and 15ed2dbd31SArchie Cobbs * 2. No rights are granted, in any manner or form, to use Whistle 16ed2dbd31SArchie Cobbs * Communications, Inc. trademarks, including the mark "WHISTLE 17ed2dbd31SArchie Cobbs * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 18ed2dbd31SArchie Cobbs * such appears in the above copyright notice or in the software. 19ed2dbd31SArchie Cobbs * 20ed2dbd31SArchie Cobbs * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 21ed2dbd31SArchie Cobbs * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 22ed2dbd31SArchie Cobbs * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 23ed2dbd31SArchie Cobbs * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 24ed2dbd31SArchie Cobbs * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 25ed2dbd31SArchie Cobbs * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 26ed2dbd31SArchie Cobbs * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 27ed2dbd31SArchie Cobbs * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 28ed2dbd31SArchie Cobbs * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 29ed2dbd31SArchie Cobbs * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 30ed2dbd31SArchie Cobbs * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 31ed2dbd31SArchie Cobbs * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 32ed2dbd31SArchie Cobbs * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 33ed2dbd31SArchie Cobbs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34ed2dbd31SArchie Cobbs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35ed2dbd31SArchie Cobbs * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 36ed2dbd31SArchie Cobbs * OF SUCH DAMAGE. 37ed2dbd31SArchie Cobbs * 38ed2dbd31SArchie Cobbs * Author: Archie Cobbs <archie@freebsd.org> 39ed2dbd31SArchie Cobbs * 40ed2dbd31SArchie Cobbs * $FreeBSD$ 41ed2dbd31SArchie Cobbs */ 42ed2dbd31SArchie Cobbs 43ed2dbd31SArchie Cobbs /* 44ed2dbd31SArchie Cobbs * ng_bridge(4) netgraph node type 45ed2dbd31SArchie Cobbs * 46ed2dbd31SArchie Cobbs * The node performs standard intelligent Ethernet bridging over 47ed2dbd31SArchie Cobbs * each of its connected hooks, or links. A simple loop detection 48ed2dbd31SArchie Cobbs * algorithm is included which disables a link for priv->conf.loopTimeout 49ed2dbd31SArchie Cobbs * seconds when a host is seen to have jumped from one link to 50ed2dbd31SArchie Cobbs * another within priv->conf.minStableAge seconds. 51ed2dbd31SArchie Cobbs * 52ed2dbd31SArchie Cobbs * We keep a hashtable that maps Ethernet addresses to host info, 53ed2dbd31SArchie Cobbs * which is contained in struct ng_bridge_host's. These structures 54ed2dbd31SArchie Cobbs * tell us on which link the host may be found. A host's entry will 55ed2dbd31SArchie Cobbs * expire after priv->conf.maxStaleness seconds. 56ed2dbd31SArchie Cobbs * 57ed2dbd31SArchie Cobbs * This node is optimzed for stable networks, where machines jump 58ed2dbd31SArchie Cobbs * from one port to the other only rarely. 59ed2dbd31SArchie Cobbs */ 60ed2dbd31SArchie Cobbs 61ed2dbd31SArchie Cobbs #include <sys/param.h> 62ed2dbd31SArchie Cobbs #include <sys/systm.h> 63ed2dbd31SArchie Cobbs #include <sys/kernel.h> 64385195c0SMarko Zec #include <sys/lock.h> 65ed2dbd31SArchie Cobbs #include <sys/malloc.h> 66ed2dbd31SArchie Cobbs #include <sys/mbuf.h> 67ed2dbd31SArchie Cobbs #include <sys/errno.h> 68385195c0SMarko Zec #include <sys/rwlock.h> 69ed2dbd31SArchie Cobbs #include <sys/syslog.h> 70ed2dbd31SArchie Cobbs #include <sys/socket.h> 71ed2dbd31SArchie Cobbs #include <sys/ctype.h> 72ed2dbd31SArchie Cobbs 73ed2dbd31SArchie Cobbs #include <net/if.h> 74ed2dbd31SArchie Cobbs #include <net/ethernet.h> 75530c0060SRobert Watson #include <net/vnet.h> 76ed2dbd31SArchie Cobbs 77ed2dbd31SArchie Cobbs #include <netinet/in.h> 785f2e1642SLuigi Rizzo #if 0 /* not used yet */ 79ed2dbd31SArchie Cobbs #include <netinet/ip_fw.h> 805f2e1642SLuigi Rizzo #endif 81ed2dbd31SArchie Cobbs #include <netgraph/ng_message.h> 82ed2dbd31SArchie Cobbs #include <netgraph/netgraph.h> 83ed2dbd31SArchie Cobbs #include <netgraph/ng_parse.h> 84ed2dbd31SArchie Cobbs #include <netgraph/ng_bridge.h> 85ed2dbd31SArchie Cobbs 869c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC 879c8c302fSJulian Elischer MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge", "netgraph bridge node"); 889c8c302fSJulian Elischer #else 899c8c302fSJulian Elischer #define M_NETGRAPH_BRIDGE M_NETGRAPH 909c8c302fSJulian Elischer #endif 919c8c302fSJulian Elischer 92ed2dbd31SArchie Cobbs /* Per-link private data */ 93ed2dbd31SArchie Cobbs struct ng_bridge_link { 94ed2dbd31SArchie Cobbs hook_p hook; /* netgraph hook */ 95ed2dbd31SArchie Cobbs u_int16_t loopCount; /* loop ignore timer */ 96ed2dbd31SArchie Cobbs struct ng_bridge_link_stats stats; /* link stats */ 97ed2dbd31SArchie Cobbs }; 98ed2dbd31SArchie Cobbs 99ed2dbd31SArchie Cobbs /* Per-node private data */ 100ed2dbd31SArchie Cobbs struct ng_bridge_private { 101ed2dbd31SArchie Cobbs struct ng_bridge_bucket *tab; /* hash table bucket array */ 102ed2dbd31SArchie Cobbs struct ng_bridge_link *links[NG_BRIDGE_MAX_LINKS]; 103ed2dbd31SArchie Cobbs struct ng_bridge_config conf; /* node configuration */ 104ed2dbd31SArchie Cobbs node_p node; /* netgraph node */ 105ed2dbd31SArchie Cobbs u_int numHosts; /* num entries in table */ 106ed2dbd31SArchie Cobbs u_int numBuckets; /* num buckets in table */ 107ed2dbd31SArchie Cobbs u_int hashMask; /* numBuckets - 1 */ 108ed2dbd31SArchie Cobbs int numLinks; /* num connected links */ 109*f8aab721SMarko Zec int persistent; /* can exist w/o hooks */ 110ed2dbd31SArchie Cobbs struct callout timer; /* one second periodic timer */ 111ed2dbd31SArchie Cobbs }; 112ed2dbd31SArchie Cobbs typedef struct ng_bridge_private *priv_p; 113ed2dbd31SArchie Cobbs 114ed2dbd31SArchie Cobbs /* Information about a host, stored in a hash table entry */ 115ed2dbd31SArchie Cobbs struct ng_bridge_hent { 116ed2dbd31SArchie Cobbs struct ng_bridge_host host; /* actual host info */ 117ed2dbd31SArchie Cobbs SLIST_ENTRY(ng_bridge_hent) next; /* next entry in bucket */ 118ed2dbd31SArchie Cobbs }; 119ed2dbd31SArchie Cobbs 120ed2dbd31SArchie Cobbs /* Hash table bucket declaration */ 121ed2dbd31SArchie Cobbs SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent); 122ed2dbd31SArchie Cobbs 123ed2dbd31SArchie Cobbs /* Netgraph node methods */ 124ed2dbd31SArchie Cobbs static ng_constructor_t ng_bridge_constructor; 125ed2dbd31SArchie Cobbs static ng_rcvmsg_t ng_bridge_rcvmsg; 126069154d5SJulian Elischer static ng_shutdown_t ng_bridge_shutdown; 127ed2dbd31SArchie Cobbs static ng_newhook_t ng_bridge_newhook; 128ed2dbd31SArchie Cobbs static ng_rcvdata_t ng_bridge_rcvdata; 129ed2dbd31SArchie Cobbs static ng_disconnect_t ng_bridge_disconnect; 130ed2dbd31SArchie Cobbs 131ed2dbd31SArchie Cobbs /* Other internal functions */ 132ed2dbd31SArchie Cobbs static struct ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr); 133ed2dbd31SArchie Cobbs static int ng_bridge_put(priv_p priv, const u_char *addr, int linkNum); 134ed2dbd31SArchie Cobbs static void ng_bridge_rehash(priv_p priv); 135ed2dbd31SArchie Cobbs static void ng_bridge_remove_hosts(priv_p priv, int linkNum); 136e0d32af7SGleb Smirnoff static void ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2); 137ed2dbd31SArchie Cobbs static const char *ng_bridge_nodename(node_p node); 138ed2dbd31SArchie Cobbs 139ed2dbd31SArchie Cobbs /* Ethernet broadcast */ 140ed2dbd31SArchie Cobbs static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] = 141ed2dbd31SArchie Cobbs { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 142ed2dbd31SArchie Cobbs 143ed2dbd31SArchie Cobbs /* Store each hook's link number in the private field */ 144ed2dbd31SArchie Cobbs #define LINK_NUM(hook) (*(u_int16_t *)(&(hook)->private)) 145ed2dbd31SArchie Cobbs 146ed2dbd31SArchie Cobbs /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */ 147ed2dbd31SArchie Cobbs #define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \ 148ed2dbd31SArchie Cobbs == ((const u_int32_t *)(b))[0] \ 149ed2dbd31SArchie Cobbs && ((const u_int16_t *)(a))[2] \ 150ed2dbd31SArchie Cobbs == ((const u_int16_t *)(b))[2]) 151ed2dbd31SArchie Cobbs 152ed2dbd31SArchie Cobbs /* Minimum and maximum number of hash buckets. Must be a power of two. */ 153ed2dbd31SArchie Cobbs #define MIN_BUCKETS (1 << 5) /* 32 */ 154ed2dbd31SArchie Cobbs #define MAX_BUCKETS (1 << 14) /* 16384 */ 155ed2dbd31SArchie Cobbs 156ed2dbd31SArchie Cobbs /* Configuration default values */ 157ed2dbd31SArchie Cobbs #define DEFAULT_LOOP_TIMEOUT 60 158ed2dbd31SArchie Cobbs #define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */ 159ed2dbd31SArchie Cobbs #define DEFAULT_MIN_STABLE_AGE 1 160ed2dbd31SArchie Cobbs 161ed2dbd31SArchie Cobbs /****************************************************************** 162ed2dbd31SArchie Cobbs NETGRAPH PARSE TYPES 163ed2dbd31SArchie Cobbs ******************************************************************/ 164ed2dbd31SArchie Cobbs 165ed2dbd31SArchie Cobbs /* 166ed2dbd31SArchie Cobbs * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE 167ed2dbd31SArchie Cobbs */ 168ed2dbd31SArchie Cobbs static int 169ed2dbd31SArchie Cobbs ng_bridge_getTableLength(const struct ng_parse_type *type, 170ed2dbd31SArchie Cobbs const u_char *start, const u_char *buf) 171ed2dbd31SArchie Cobbs { 172ed2dbd31SArchie Cobbs const struct ng_bridge_host_ary *const hary 173ed2dbd31SArchie Cobbs = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t)); 174ed2dbd31SArchie Cobbs 175ed2dbd31SArchie Cobbs return hary->numHosts; 176ed2dbd31SArchie Cobbs } 177ed2dbd31SArchie Cobbs 178ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_host_ary */ 179f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_type_fields[] 1808c7e4101SRuslan Ermilov = NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type); 181ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_type = { 182ed2dbd31SArchie Cobbs &ng_parse_struct_type, 183f0184ff8SArchie Cobbs &ng_bridge_host_type_fields 184ed2dbd31SArchie Cobbs }; 185ed2dbd31SArchie Cobbs static const struct ng_parse_array_info ng_bridge_hary_type_info = { 186ed2dbd31SArchie Cobbs &ng_bridge_host_type, 187ed2dbd31SArchie Cobbs ng_bridge_getTableLength 188ed2dbd31SArchie Cobbs }; 189ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_hary_type = { 190ed2dbd31SArchie Cobbs &ng_parse_array_type, 191ed2dbd31SArchie Cobbs &ng_bridge_hary_type_info 192ed2dbd31SArchie Cobbs }; 193f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[] 194ed2dbd31SArchie Cobbs = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type); 195ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_ary_type = { 196ed2dbd31SArchie Cobbs &ng_parse_struct_type, 197f0184ff8SArchie Cobbs &ng_bridge_host_ary_type_fields 198ed2dbd31SArchie Cobbs }; 199ed2dbd31SArchie Cobbs 200ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_config */ 201ed2dbd31SArchie Cobbs static const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = { 202ed2dbd31SArchie Cobbs &ng_parse_uint8_type, 203ed2dbd31SArchie Cobbs NG_BRIDGE_MAX_LINKS 204ed2dbd31SArchie Cobbs }; 205ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_ipfwary_type = { 206ed2dbd31SArchie Cobbs &ng_parse_fixedarray_type, 207ed2dbd31SArchie Cobbs &ng_bridge_ipfwary_type_info 208ed2dbd31SArchie Cobbs }; 209f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_config_type_fields[] 210ed2dbd31SArchie Cobbs = NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type); 211ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_config_type = { 212ed2dbd31SArchie Cobbs &ng_parse_struct_type, 213f0184ff8SArchie Cobbs &ng_bridge_config_type_fields 214ed2dbd31SArchie Cobbs }; 215ed2dbd31SArchie Cobbs 216ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_link_stat */ 217f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_stats_type_fields[] 218f0184ff8SArchie Cobbs = NG_BRIDGE_STATS_TYPE_INFO; 219ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_stats_type = { 220ed2dbd31SArchie Cobbs &ng_parse_struct_type, 221f0184ff8SArchie Cobbs &ng_bridge_stats_type_fields 222ed2dbd31SArchie Cobbs }; 223ed2dbd31SArchie Cobbs 224ed2dbd31SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 225ed2dbd31SArchie Cobbs static const struct ng_cmdlist ng_bridge_cmdlist[] = { 226ed2dbd31SArchie Cobbs { 227ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 228ed2dbd31SArchie Cobbs NGM_BRIDGE_SET_CONFIG, 229ed2dbd31SArchie Cobbs "setconfig", 230ed2dbd31SArchie Cobbs &ng_bridge_config_type, 231ed2dbd31SArchie Cobbs NULL 232ed2dbd31SArchie Cobbs }, 233ed2dbd31SArchie Cobbs { 234ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 235ed2dbd31SArchie Cobbs NGM_BRIDGE_GET_CONFIG, 236ed2dbd31SArchie Cobbs "getconfig", 237ed2dbd31SArchie Cobbs NULL, 238ed2dbd31SArchie Cobbs &ng_bridge_config_type 239ed2dbd31SArchie Cobbs }, 240ed2dbd31SArchie Cobbs { 241ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 242ed2dbd31SArchie Cobbs NGM_BRIDGE_RESET, 243ed2dbd31SArchie Cobbs "reset", 244ed2dbd31SArchie Cobbs NULL, 245ed2dbd31SArchie Cobbs NULL 246ed2dbd31SArchie Cobbs }, 247ed2dbd31SArchie Cobbs { 248ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 249ed2dbd31SArchie Cobbs NGM_BRIDGE_GET_STATS, 250ed2dbd31SArchie Cobbs "getstats", 251ed2dbd31SArchie Cobbs &ng_parse_uint32_type, 252ed2dbd31SArchie Cobbs &ng_bridge_stats_type 253ed2dbd31SArchie Cobbs }, 254ed2dbd31SArchie Cobbs { 255ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 256ed2dbd31SArchie Cobbs NGM_BRIDGE_CLR_STATS, 257ed2dbd31SArchie Cobbs "clrstats", 258ed2dbd31SArchie Cobbs &ng_parse_uint32_type, 259ed2dbd31SArchie Cobbs NULL 260ed2dbd31SArchie Cobbs }, 261ed2dbd31SArchie Cobbs { 262ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 263ed2dbd31SArchie Cobbs NGM_BRIDGE_GETCLR_STATS, 264ed2dbd31SArchie Cobbs "getclrstats", 265ed2dbd31SArchie Cobbs &ng_parse_uint32_type, 266ed2dbd31SArchie Cobbs &ng_bridge_stats_type 267ed2dbd31SArchie Cobbs }, 268ed2dbd31SArchie Cobbs { 269ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 270ed2dbd31SArchie Cobbs NGM_BRIDGE_GET_TABLE, 271ed2dbd31SArchie Cobbs "gettable", 272ed2dbd31SArchie Cobbs NULL, 273ed2dbd31SArchie Cobbs &ng_bridge_host_ary_type 274ed2dbd31SArchie Cobbs }, 275*f8aab721SMarko Zec { 276*f8aab721SMarko Zec NGM_BRIDGE_COOKIE, 277*f8aab721SMarko Zec NGM_BRIDGE_SET_PERSISTENT, 278*f8aab721SMarko Zec "setpersistent", 279*f8aab721SMarko Zec NULL, 280*f8aab721SMarko Zec NULL 281*f8aab721SMarko Zec }, 282ed2dbd31SArchie Cobbs { 0 } 283ed2dbd31SArchie Cobbs }; 284ed2dbd31SArchie Cobbs 285ed2dbd31SArchie Cobbs /* Node type descriptor */ 286ed2dbd31SArchie Cobbs static struct ng_type ng_bridge_typestruct = { 287f8aae777SJulian Elischer .version = NG_ABI_VERSION, 288f8aae777SJulian Elischer .name = NG_BRIDGE_NODE_TYPE, 289f8aae777SJulian Elischer .constructor = ng_bridge_constructor, 290f8aae777SJulian Elischer .rcvmsg = ng_bridge_rcvmsg, 291f8aae777SJulian Elischer .shutdown = ng_bridge_shutdown, 292f8aae777SJulian Elischer .newhook = ng_bridge_newhook, 293f8aae777SJulian Elischer .rcvdata = ng_bridge_rcvdata, 294f8aae777SJulian Elischer .disconnect = ng_bridge_disconnect, 295f8aae777SJulian Elischer .cmdlist = ng_bridge_cmdlist, 296ed2dbd31SArchie Cobbs }; 297a89effcdSArchie Cobbs NETGRAPH_INIT(bridge, &ng_bridge_typestruct); 298ed2dbd31SArchie Cobbs 299ed2dbd31SArchie Cobbs /****************************************************************** 300ed2dbd31SArchie Cobbs NETGRAPH NODE METHODS 301ed2dbd31SArchie Cobbs ******************************************************************/ 302ed2dbd31SArchie Cobbs 303ed2dbd31SArchie Cobbs /* 304ed2dbd31SArchie Cobbs * Node constructor 305ed2dbd31SArchie Cobbs */ 306ed2dbd31SArchie Cobbs static int 307069154d5SJulian Elischer ng_bridge_constructor(node_p node) 308ed2dbd31SArchie Cobbs { 309ed2dbd31SArchie Cobbs priv_p priv; 310ed2dbd31SArchie Cobbs 311ed2dbd31SArchie Cobbs /* Allocate and initialize private info */ 3121ede983cSDag-Erling Smørgrav priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 313ed2dbd31SArchie Cobbs if (priv == NULL) 314ed2dbd31SArchie Cobbs return (ENOMEM); 315e0d32af7SGleb Smirnoff ng_callout_init(&priv->timer); 316ed2dbd31SArchie Cobbs 317ed2dbd31SArchie Cobbs /* Allocate and initialize hash table, etc. */ 318e11e3f18SDag-Erling Smørgrav priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab), 319e11e3f18SDag-Erling Smørgrav M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 320ed2dbd31SArchie Cobbs if (priv->tab == NULL) { 3211ede983cSDag-Erling Smørgrav free(priv, M_NETGRAPH_BRIDGE); 322ed2dbd31SArchie Cobbs return (ENOMEM); 323ed2dbd31SArchie Cobbs } 324ed2dbd31SArchie Cobbs priv->numBuckets = MIN_BUCKETS; 325ed2dbd31SArchie Cobbs priv->hashMask = MIN_BUCKETS - 1; 326ed2dbd31SArchie Cobbs priv->conf.debugLevel = 1; 327ed2dbd31SArchie Cobbs priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT; 328ed2dbd31SArchie Cobbs priv->conf.maxStaleness = DEFAULT_MAX_STALENESS; 329ed2dbd31SArchie Cobbs priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE; 330ed2dbd31SArchie Cobbs 331069154d5SJulian Elischer /* 332069154d5SJulian Elischer * This node has all kinds of stuff that could be screwed by SMP. 333069154d5SJulian Elischer * Until it gets it's own internal protection, we go through in 334069154d5SJulian Elischer * single file. This could hurt a machine bridging beteen two 335069154d5SJulian Elischer * GB ethernets so it should be fixed. 336069154d5SJulian Elischer * When it's fixed the process SHOULD NOT SLEEP, spinlocks please! 337069154d5SJulian Elischer * (and atomic ops ) 338069154d5SJulian Elischer */ 33930400f03SJulian Elischer NG_NODE_FORCE_WRITER(node); 34030400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, priv); 341069154d5SJulian Elischer priv->node = node; 342ed2dbd31SArchie Cobbs 3436c12c2b1SArchie Cobbs /* Start timer; timer is always running while node is alive */ 344e0d32af7SGleb Smirnoff ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 3456c12c2b1SArchie Cobbs 3466c12c2b1SArchie Cobbs /* Done */ 347ed2dbd31SArchie Cobbs return (0); 348ed2dbd31SArchie Cobbs } 349ed2dbd31SArchie Cobbs 350ed2dbd31SArchie Cobbs /* 351ed2dbd31SArchie Cobbs * Method for attaching a new hook 352ed2dbd31SArchie Cobbs */ 353ed2dbd31SArchie Cobbs static int 354ed2dbd31SArchie Cobbs ng_bridge_newhook(node_p node, hook_p hook, const char *name) 355ed2dbd31SArchie Cobbs { 35630400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 357ed2dbd31SArchie Cobbs 358ed2dbd31SArchie Cobbs /* Check for a link hook */ 359ed2dbd31SArchie Cobbs if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX, 360ed2dbd31SArchie Cobbs strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) { 361ed2dbd31SArchie Cobbs const char *cp; 362ed2dbd31SArchie Cobbs char *eptr; 363ed2dbd31SArchie Cobbs u_long linkNum; 364ed2dbd31SArchie Cobbs 365ed2dbd31SArchie Cobbs cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX); 366ed2dbd31SArchie Cobbs if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 367ed2dbd31SArchie Cobbs return (EINVAL); 368ed2dbd31SArchie Cobbs linkNum = strtoul(cp, &eptr, 10); 369ed2dbd31SArchie Cobbs if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS) 370ed2dbd31SArchie Cobbs return (EINVAL); 371ed2dbd31SArchie Cobbs if (priv->links[linkNum] != NULL) 372ed2dbd31SArchie Cobbs return (EISCONN); 373e11e3f18SDag-Erling Smørgrav priv->links[linkNum] = malloc(sizeof(*priv->links[linkNum]), 374e11e3f18SDag-Erling Smørgrav M_NETGRAPH_BRIDGE, M_NOWAIT|M_ZERO); 375ed2dbd31SArchie Cobbs if (priv->links[linkNum] == NULL) 376ed2dbd31SArchie Cobbs return (ENOMEM); 377ed2dbd31SArchie Cobbs priv->links[linkNum]->hook = hook; 37830400f03SJulian Elischer NG_HOOK_SET_PRIVATE(hook, (void *)linkNum); 379ed2dbd31SArchie Cobbs priv->numLinks++; 380ed2dbd31SArchie Cobbs return (0); 381ed2dbd31SArchie Cobbs } 382ed2dbd31SArchie Cobbs 383ed2dbd31SArchie Cobbs /* Unknown hook name */ 384ed2dbd31SArchie Cobbs return (EINVAL); 385ed2dbd31SArchie Cobbs } 386ed2dbd31SArchie Cobbs 387ed2dbd31SArchie Cobbs /* 388ed2dbd31SArchie Cobbs * Receive a control message 389ed2dbd31SArchie Cobbs */ 390ed2dbd31SArchie Cobbs static int 391069154d5SJulian Elischer ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook) 392ed2dbd31SArchie Cobbs { 39330400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 394ed2dbd31SArchie Cobbs struct ng_mesg *resp = NULL; 395ed2dbd31SArchie Cobbs int error = 0; 396069154d5SJulian Elischer struct ng_mesg *msg; 397ed2dbd31SArchie Cobbs 398069154d5SJulian Elischer NGI_GET_MSG(item, msg); 399ed2dbd31SArchie Cobbs switch (msg->header.typecookie) { 400ed2dbd31SArchie Cobbs case NGM_BRIDGE_COOKIE: 401ed2dbd31SArchie Cobbs switch (msg->header.cmd) { 402ed2dbd31SArchie Cobbs case NGM_BRIDGE_GET_CONFIG: 403ed2dbd31SArchie Cobbs { 404ed2dbd31SArchie Cobbs struct ng_bridge_config *conf; 405ed2dbd31SArchie Cobbs 406ed2dbd31SArchie Cobbs NG_MKRESPONSE(resp, msg, 407ed2dbd31SArchie Cobbs sizeof(struct ng_bridge_config), M_NOWAIT); 408ed2dbd31SArchie Cobbs if (resp == NULL) { 409ed2dbd31SArchie Cobbs error = ENOMEM; 410ed2dbd31SArchie Cobbs break; 411ed2dbd31SArchie Cobbs } 412ed2dbd31SArchie Cobbs conf = (struct ng_bridge_config *)resp->data; 413ed2dbd31SArchie Cobbs *conf = priv->conf; /* no sanity checking needed */ 414ed2dbd31SArchie Cobbs break; 415ed2dbd31SArchie Cobbs } 416ed2dbd31SArchie Cobbs case NGM_BRIDGE_SET_CONFIG: 417ed2dbd31SArchie Cobbs { 418ed2dbd31SArchie Cobbs struct ng_bridge_config *conf; 419ed2dbd31SArchie Cobbs int i; 420ed2dbd31SArchie Cobbs 421ed2dbd31SArchie Cobbs if (msg->header.arglen 422ed2dbd31SArchie Cobbs != sizeof(struct ng_bridge_config)) { 423ed2dbd31SArchie Cobbs error = EINVAL; 424ed2dbd31SArchie Cobbs break; 425ed2dbd31SArchie Cobbs } 426ed2dbd31SArchie Cobbs conf = (struct ng_bridge_config *)msg->data; 427ed2dbd31SArchie Cobbs priv->conf = *conf; 428ed2dbd31SArchie Cobbs for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) 429ed2dbd31SArchie Cobbs priv->conf.ipfw[i] = !!priv->conf.ipfw[i]; 430ed2dbd31SArchie Cobbs break; 431ed2dbd31SArchie Cobbs } 432ed2dbd31SArchie Cobbs case NGM_BRIDGE_RESET: 433ed2dbd31SArchie Cobbs { 434ed2dbd31SArchie Cobbs int i; 435ed2dbd31SArchie Cobbs 436ed2dbd31SArchie Cobbs /* Flush all entries in the hash table */ 437ed2dbd31SArchie Cobbs ng_bridge_remove_hosts(priv, -1); 438ed2dbd31SArchie Cobbs 439ed2dbd31SArchie Cobbs /* Reset all loop detection counters and stats */ 440ed2dbd31SArchie Cobbs for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) { 441ed2dbd31SArchie Cobbs if (priv->links[i] == NULL) 442ed2dbd31SArchie Cobbs continue; 443ed2dbd31SArchie Cobbs priv->links[i]->loopCount = 0; 444ed2dbd31SArchie Cobbs bzero(&priv->links[i]->stats, 445ed2dbd31SArchie Cobbs sizeof(priv->links[i]->stats)); 446ed2dbd31SArchie Cobbs } 447ed2dbd31SArchie Cobbs break; 448ed2dbd31SArchie Cobbs } 449ed2dbd31SArchie Cobbs case NGM_BRIDGE_GET_STATS: 450ed2dbd31SArchie Cobbs case NGM_BRIDGE_CLR_STATS: 451ed2dbd31SArchie Cobbs case NGM_BRIDGE_GETCLR_STATS: 452ed2dbd31SArchie Cobbs { 453ed2dbd31SArchie Cobbs struct ng_bridge_link *link; 454ed2dbd31SArchie Cobbs int linkNum; 455ed2dbd31SArchie Cobbs 456ed2dbd31SArchie Cobbs /* Get link number */ 457ed2dbd31SArchie Cobbs if (msg->header.arglen != sizeof(u_int32_t)) { 458ed2dbd31SArchie Cobbs error = EINVAL; 459ed2dbd31SArchie Cobbs break; 460ed2dbd31SArchie Cobbs } 461ed2dbd31SArchie Cobbs linkNum = *((u_int32_t *)msg->data); 462ed2dbd31SArchie Cobbs if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) { 463ed2dbd31SArchie Cobbs error = EINVAL; 464ed2dbd31SArchie Cobbs break; 465ed2dbd31SArchie Cobbs } 466ed2dbd31SArchie Cobbs if ((link = priv->links[linkNum]) == NULL) { 467ed2dbd31SArchie Cobbs error = ENOTCONN; 468ed2dbd31SArchie Cobbs break; 469ed2dbd31SArchie Cobbs } 470ed2dbd31SArchie Cobbs 471ed2dbd31SArchie Cobbs /* Get/clear stats */ 472ed2dbd31SArchie Cobbs if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) { 473ed2dbd31SArchie Cobbs NG_MKRESPONSE(resp, msg, 474ed2dbd31SArchie Cobbs sizeof(link->stats), M_NOWAIT); 475ed2dbd31SArchie Cobbs if (resp == NULL) { 476ed2dbd31SArchie Cobbs error = ENOMEM; 477ed2dbd31SArchie Cobbs break; 478ed2dbd31SArchie Cobbs } 479ed2dbd31SArchie Cobbs bcopy(&link->stats, 480ed2dbd31SArchie Cobbs resp->data, sizeof(link->stats)); 481ed2dbd31SArchie Cobbs } 482ed2dbd31SArchie Cobbs if (msg->header.cmd != NGM_BRIDGE_GET_STATS) 483ed2dbd31SArchie Cobbs bzero(&link->stats, sizeof(link->stats)); 484ed2dbd31SArchie Cobbs break; 485ed2dbd31SArchie Cobbs } 486ed2dbd31SArchie Cobbs case NGM_BRIDGE_GET_TABLE: 487ed2dbd31SArchie Cobbs { 488ed2dbd31SArchie Cobbs struct ng_bridge_host_ary *ary; 489ed2dbd31SArchie Cobbs struct ng_bridge_hent *hent; 490ed2dbd31SArchie Cobbs int i = 0, bucket; 491ed2dbd31SArchie Cobbs 492ed2dbd31SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(*ary) 493ed2dbd31SArchie Cobbs + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT); 494ed2dbd31SArchie Cobbs if (resp == NULL) { 495ed2dbd31SArchie Cobbs error = ENOMEM; 496ed2dbd31SArchie Cobbs break; 497ed2dbd31SArchie Cobbs } 498ed2dbd31SArchie Cobbs ary = (struct ng_bridge_host_ary *)resp->data; 499ed2dbd31SArchie Cobbs ary->numHosts = priv->numHosts; 500ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) { 501ed2dbd31SArchie Cobbs SLIST_FOREACH(hent, &priv->tab[bucket], next) 502ed2dbd31SArchie Cobbs ary->hosts[i++] = hent->host; 503ed2dbd31SArchie Cobbs } 504ed2dbd31SArchie Cobbs break; 505ed2dbd31SArchie Cobbs } 506*f8aab721SMarko Zec case NGM_BRIDGE_SET_PERSISTENT: 507*f8aab721SMarko Zec { 508*f8aab721SMarko Zec priv->persistent = 1; 509*f8aab721SMarko Zec break; 510*f8aab721SMarko Zec } 511ed2dbd31SArchie Cobbs default: 512ed2dbd31SArchie Cobbs error = EINVAL; 513ed2dbd31SArchie Cobbs break; 514ed2dbd31SArchie Cobbs } 515ed2dbd31SArchie Cobbs break; 516ed2dbd31SArchie Cobbs default: 517ed2dbd31SArchie Cobbs error = EINVAL; 518ed2dbd31SArchie Cobbs break; 519ed2dbd31SArchie Cobbs } 520ed2dbd31SArchie Cobbs 521ed2dbd31SArchie Cobbs /* Done */ 522069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp); 523069154d5SJulian Elischer NG_FREE_MSG(msg); 524ed2dbd31SArchie Cobbs return (error); 525ed2dbd31SArchie Cobbs } 526ed2dbd31SArchie Cobbs 527ed2dbd31SArchie Cobbs /* 528ed2dbd31SArchie Cobbs * Receive data on a hook 529ed2dbd31SArchie Cobbs */ 530ed2dbd31SArchie Cobbs static int 531069154d5SJulian Elischer ng_bridge_rcvdata(hook_p hook, item_p item) 532ed2dbd31SArchie Cobbs { 53330400f03SJulian Elischer const node_p node = NG_HOOK_NODE(hook); 53430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 535ed2dbd31SArchie Cobbs struct ng_bridge_host *host; 536ed2dbd31SArchie Cobbs struct ng_bridge_link *link; 537ed2dbd31SArchie Cobbs struct ether_header *eh; 538114bf091SBrian Feldman int error = 0, linkNum, linksSeen; 539069154d5SJulian Elischer int manycast; 540069154d5SJulian Elischer struct mbuf *m; 541069154d5SJulian Elischer struct ng_bridge_link *firstLink; 542ed2dbd31SArchie Cobbs 543069154d5SJulian Elischer NGI_GET_M(item, m); 544ed2dbd31SArchie Cobbs /* Get link number */ 5458214d60eSJohn Baldwin linkNum = (intptr_t)NG_HOOK_PRIVATE(hook); 546ed2dbd31SArchie Cobbs KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, 5476e551fb6SDavid E. O'Brien ("%s: linkNum=%u", __func__, linkNum)); 548ed2dbd31SArchie Cobbs link = priv->links[linkNum]; 5496e551fb6SDavid E. O'Brien KASSERT(link != NULL, ("%s: link%d null", __func__, linkNum)); 550ed2dbd31SArchie Cobbs 551ed2dbd31SArchie Cobbs /* Sanity check packet and pull up header */ 552ed2dbd31SArchie Cobbs if (m->m_pkthdr.len < ETHER_HDR_LEN) { 553ed2dbd31SArchie Cobbs link->stats.recvRunts++; 554069154d5SJulian Elischer NG_FREE_ITEM(item); 555069154d5SJulian Elischer NG_FREE_M(m); 556ed2dbd31SArchie Cobbs return (EINVAL); 557ed2dbd31SArchie Cobbs } 558ed2dbd31SArchie Cobbs if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) { 559ed2dbd31SArchie Cobbs link->stats.memoryFailures++; 560069154d5SJulian Elischer NG_FREE_ITEM(item); 561ed2dbd31SArchie Cobbs return (ENOBUFS); 562ed2dbd31SArchie Cobbs } 563ed2dbd31SArchie Cobbs eh = mtod(m, struct ether_header *); 564ed2dbd31SArchie Cobbs if ((eh->ether_shost[0] & 1) != 0) { 565ed2dbd31SArchie Cobbs link->stats.recvInvalid++; 566069154d5SJulian Elischer NG_FREE_ITEM(item); 567069154d5SJulian Elischer NG_FREE_M(m); 568ed2dbd31SArchie Cobbs return (EINVAL); 569ed2dbd31SArchie Cobbs } 570ed2dbd31SArchie Cobbs 571ed2dbd31SArchie Cobbs /* Is link disabled due to a loopback condition? */ 572ed2dbd31SArchie Cobbs if (link->loopCount != 0) { 573ed2dbd31SArchie Cobbs link->stats.loopDrops++; 574069154d5SJulian Elischer NG_FREE_ITEM(item); 575069154d5SJulian Elischer NG_FREE_M(m); 576ed2dbd31SArchie Cobbs return (ELOOP); /* XXX is this an appropriate error? */ 577ed2dbd31SArchie Cobbs } 578ed2dbd31SArchie Cobbs 579ed2dbd31SArchie Cobbs /* Update stats */ 580ed2dbd31SArchie Cobbs link->stats.recvPackets++; 581ed2dbd31SArchie Cobbs link->stats.recvOctets += m->m_pkthdr.len; 582ed2dbd31SArchie Cobbs if ((manycast = (eh->ether_dhost[0] & 1)) != 0) { 583ed2dbd31SArchie Cobbs if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) { 584ed2dbd31SArchie Cobbs link->stats.recvBroadcasts++; 585ed2dbd31SArchie Cobbs manycast = 2; 586ed2dbd31SArchie Cobbs } else 587ed2dbd31SArchie Cobbs link->stats.recvMulticasts++; 588ed2dbd31SArchie Cobbs } 589ed2dbd31SArchie Cobbs 590ed2dbd31SArchie Cobbs /* Look up packet's source Ethernet address in hashtable */ 591ed2dbd31SArchie Cobbs if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) { 592ed2dbd31SArchie Cobbs 593ed2dbd31SArchie Cobbs /* Update time since last heard from this host */ 594ed2dbd31SArchie Cobbs host->staleness = 0; 595ed2dbd31SArchie Cobbs 596ed2dbd31SArchie Cobbs /* Did host jump to a different link? */ 597ed2dbd31SArchie Cobbs if (host->linkNum != linkNum) { 598ed2dbd31SArchie Cobbs 599ed2dbd31SArchie Cobbs /* 600ed2dbd31SArchie Cobbs * If the host's old link was recently established 601ed2dbd31SArchie Cobbs * on the old link and it's already jumped to a new 602ed2dbd31SArchie Cobbs * link, declare a loopback condition. 603ed2dbd31SArchie Cobbs */ 604ed2dbd31SArchie Cobbs if (host->age < priv->conf.minStableAge) { 605ed2dbd31SArchie Cobbs 606ed2dbd31SArchie Cobbs /* Log the problem */ 607ed2dbd31SArchie Cobbs if (priv->conf.debugLevel >= 2) { 608ed2dbd31SArchie Cobbs struct ifnet *ifp = m->m_pkthdr.rcvif; 609ed2dbd31SArchie Cobbs char suffix[32]; 610ed2dbd31SArchie Cobbs 611ed2dbd31SArchie Cobbs if (ifp != NULL) 612ed2dbd31SArchie Cobbs snprintf(suffix, sizeof(suffix), 6139bf40edeSBrooks Davis " (%s)", ifp->if_xname); 614ed2dbd31SArchie Cobbs else 615ed2dbd31SArchie Cobbs *suffix = '\0'; 616ed2dbd31SArchie Cobbs log(LOG_WARNING, "ng_bridge: %s:" 617ed2dbd31SArchie Cobbs " loopback detected on %s%s\n", 618ed2dbd31SArchie Cobbs ng_bridge_nodename(node), 61930400f03SJulian Elischer NG_HOOK_NAME(hook), suffix); 620ed2dbd31SArchie Cobbs } 621ed2dbd31SArchie Cobbs 622ed2dbd31SArchie Cobbs /* Mark link as linka non grata */ 623ed2dbd31SArchie Cobbs link->loopCount = priv->conf.loopTimeout; 624ed2dbd31SArchie Cobbs link->stats.loopDetects++; 625ed2dbd31SArchie Cobbs 626ed2dbd31SArchie Cobbs /* Forget all hosts on this link */ 627ed2dbd31SArchie Cobbs ng_bridge_remove_hosts(priv, linkNum); 628ed2dbd31SArchie Cobbs 629ed2dbd31SArchie Cobbs /* Drop packet */ 630ed2dbd31SArchie Cobbs link->stats.loopDrops++; 631069154d5SJulian Elischer NG_FREE_ITEM(item); 632069154d5SJulian Elischer NG_FREE_M(m); 633ed2dbd31SArchie Cobbs return (ELOOP); /* XXX appropriate? */ 634ed2dbd31SArchie Cobbs } 635ed2dbd31SArchie Cobbs 636ed2dbd31SArchie Cobbs /* Move host over to new link */ 637ed2dbd31SArchie Cobbs host->linkNum = linkNum; 638ed2dbd31SArchie Cobbs host->age = 0; 639ed2dbd31SArchie Cobbs } 640ed2dbd31SArchie Cobbs } else { 641ed2dbd31SArchie Cobbs if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) { 642ed2dbd31SArchie Cobbs link->stats.memoryFailures++; 643069154d5SJulian Elischer NG_FREE_ITEM(item); 644069154d5SJulian Elischer NG_FREE_M(m); 645ed2dbd31SArchie Cobbs return (ENOMEM); 646ed2dbd31SArchie Cobbs } 647ed2dbd31SArchie Cobbs } 648ed2dbd31SArchie Cobbs 649ed2dbd31SArchie Cobbs /* Run packet through ipfw processing, if enabled */ 6509b932e9eSAndre Oppermann #if 0 6510b4b0b0fSJulian Elischer if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) { 652ed2dbd31SArchie Cobbs /* XXX not implemented yet */ 653ed2dbd31SArchie Cobbs } 6549b932e9eSAndre Oppermann #endif 655ed2dbd31SArchie Cobbs 656ed2dbd31SArchie Cobbs /* 657ed2dbd31SArchie Cobbs * If unicast and destination host known, deliver to host's link, 658ed2dbd31SArchie Cobbs * unless it is the same link as the packet came in on. 659ed2dbd31SArchie Cobbs */ 660ed2dbd31SArchie Cobbs if (!manycast) { 661ed2dbd31SArchie Cobbs 662ed2dbd31SArchie Cobbs /* Determine packet destination link */ 663ed2dbd31SArchie Cobbs if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) { 664ed2dbd31SArchie Cobbs struct ng_bridge_link *const destLink 665ed2dbd31SArchie Cobbs = priv->links[host->linkNum]; 666ed2dbd31SArchie Cobbs 667ed2dbd31SArchie Cobbs /* If destination same as incoming link, do nothing */ 668ed2dbd31SArchie Cobbs KASSERT(destLink != NULL, 6696e551fb6SDavid E. O'Brien ("%s: link%d null", __func__, host->linkNum)); 670ed2dbd31SArchie Cobbs if (destLink == link) { 671069154d5SJulian Elischer NG_FREE_ITEM(item); 672069154d5SJulian Elischer NG_FREE_M(m); 673ed2dbd31SArchie Cobbs return (0); 674ed2dbd31SArchie Cobbs } 675ed2dbd31SArchie Cobbs 676ed2dbd31SArchie Cobbs /* Deliver packet out the destination link */ 677ed2dbd31SArchie Cobbs destLink->stats.xmitPackets++; 678ed2dbd31SArchie Cobbs destLink->stats.xmitOctets += m->m_pkthdr.len; 679069154d5SJulian Elischer NG_FWD_NEW_DATA(error, item, destLink->hook, m); 680ed2dbd31SArchie Cobbs return (error); 681ed2dbd31SArchie Cobbs } 682ed2dbd31SArchie Cobbs 683ed2dbd31SArchie Cobbs /* Destination host is not known */ 684ed2dbd31SArchie Cobbs link->stats.recvUnknown++; 685ed2dbd31SArchie Cobbs } 686ed2dbd31SArchie Cobbs 687ed2dbd31SArchie Cobbs /* Distribute unknown, multicast, broadcast pkts to all other links */ 688069154d5SJulian Elischer firstLink = NULL; 689114bf091SBrian Feldman for (linkNum = linksSeen = 0; linksSeen <= priv->numLinks; linkNum++) { 690069154d5SJulian Elischer struct ng_bridge_link *destLink; 691069154d5SJulian Elischer struct mbuf *m2 = NULL; 692ed2dbd31SArchie Cobbs 693069154d5SJulian Elischer /* 694069154d5SJulian Elischer * If we have checked all the links then now 695069154d5SJulian Elischer * send the original on its reserved link 696069154d5SJulian Elischer */ 697114bf091SBrian Feldman if (linksSeen == priv->numLinks) { 698069154d5SJulian Elischer /* If we never saw a good link, leave. */ 699069154d5SJulian Elischer if (firstLink == NULL) { 700069154d5SJulian Elischer NG_FREE_ITEM(item); 701069154d5SJulian Elischer NG_FREE_M(m); 702069154d5SJulian Elischer return (0); 703069154d5SJulian Elischer } 704069154d5SJulian Elischer destLink = firstLink; 705ed2dbd31SArchie Cobbs } else { 706069154d5SJulian Elischer destLink = priv->links[linkNum]; 707114bf091SBrian Feldman if (destLink != NULL) 708114bf091SBrian Feldman linksSeen++; 709069154d5SJulian Elischer /* Skip incoming link and disconnected links */ 710069154d5SJulian Elischer if (destLink == NULL || destLink == link) { 711069154d5SJulian Elischer continue; 712069154d5SJulian Elischer } 713069154d5SJulian Elischer if (firstLink == NULL) { 714069154d5SJulian Elischer /* 715069154d5SJulian Elischer * This is the first usable link we have found. 716069154d5SJulian Elischer * Reserve it for the originals. 717069154d5SJulian Elischer * If we never find another we save a copy. 718069154d5SJulian Elischer */ 719069154d5SJulian Elischer firstLink = destLink; 720069154d5SJulian Elischer continue; 721069154d5SJulian Elischer } 722069154d5SJulian Elischer 723069154d5SJulian Elischer /* 724069154d5SJulian Elischer * It's usable link but not the reserved (first) one. 7253ca24c28SJulian Elischer * Copy mbuf info for sending. 726069154d5SJulian Elischer */ 727a163d034SWarner Losh m2 = m_dup(m, M_DONTWAIT); /* XXX m_copypacket() */ 728ed2dbd31SArchie Cobbs if (m2 == NULL) { 729ed2dbd31SArchie Cobbs link->stats.memoryFailures++; 730069154d5SJulian Elischer NG_FREE_ITEM(item); 731069154d5SJulian Elischer NG_FREE_M(m); 732ed2dbd31SArchie Cobbs return (ENOBUFS); 733ed2dbd31SArchie Cobbs } 734ed2dbd31SArchie Cobbs } 735ed2dbd31SArchie Cobbs 736ed2dbd31SArchie Cobbs /* Update stats */ 737ed2dbd31SArchie Cobbs destLink->stats.xmitPackets++; 738ed2dbd31SArchie Cobbs destLink->stats.xmitOctets += m->m_pkthdr.len; 739ed2dbd31SArchie Cobbs switch (manycast) { 740ed2dbd31SArchie Cobbs case 0: /* unicast */ 741ed2dbd31SArchie Cobbs break; 742ed2dbd31SArchie Cobbs case 1: /* multicast */ 743ed2dbd31SArchie Cobbs destLink->stats.xmitMulticasts++; 744ed2dbd31SArchie Cobbs break; 745ed2dbd31SArchie Cobbs case 2: /* broadcast */ 746ed2dbd31SArchie Cobbs destLink->stats.xmitBroadcasts++; 747ed2dbd31SArchie Cobbs break; 748ed2dbd31SArchie Cobbs } 749ed2dbd31SArchie Cobbs 750ed2dbd31SArchie Cobbs /* Send packet */ 751069154d5SJulian Elischer if (destLink == firstLink) { 752069154d5SJulian Elischer /* 753069154d5SJulian Elischer * If we've sent all the others, send the original 754069154d5SJulian Elischer * on the first link we found. 755069154d5SJulian Elischer */ 756069154d5SJulian Elischer NG_FWD_NEW_DATA(error, item, destLink->hook, m); 757069154d5SJulian Elischer break; /* always done last - not really needed. */ 758069154d5SJulian Elischer } else { 7593ca24c28SJulian Elischer NG_SEND_DATA_ONLY(error, destLink->hook, m2); 760ed2dbd31SArchie Cobbs } 761069154d5SJulian Elischer } 762ed2dbd31SArchie Cobbs return (error); 763ed2dbd31SArchie Cobbs } 764ed2dbd31SArchie Cobbs 765ed2dbd31SArchie Cobbs /* 766ed2dbd31SArchie Cobbs * Shutdown node 767ed2dbd31SArchie Cobbs */ 768ed2dbd31SArchie Cobbs static int 769069154d5SJulian Elischer ng_bridge_shutdown(node_p node) 770ed2dbd31SArchie Cobbs { 77130400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 772ed2dbd31SArchie Cobbs 7736c12c2b1SArchie Cobbs /* 774195cf617SRuslan Ermilov * Shut down everything including the timer. Even if the 775195cf617SRuslan Ermilov * callout has already been dequeued and is about to be 776195cf617SRuslan Ermilov * run, ng_bridge_timeout() won't be fired as the node 777195cf617SRuslan Ermilov * is already marked NGF_INVALID, so we're safe to free 778195cf617SRuslan Ermilov * the node now. 7796c12c2b1SArchie Cobbs */ 780ed2dbd31SArchie Cobbs KASSERT(priv->numLinks == 0 && priv->numHosts == 0, 781ed2dbd31SArchie Cobbs ("%s: numLinks=%d numHosts=%d", 7826e551fb6SDavid E. O'Brien __func__, priv->numLinks, priv->numHosts)); 783195cf617SRuslan Ermilov ng_uncallout(&priv->timer, node); 784195cf617SRuslan Ermilov NG_NODE_SET_PRIVATE(node, NULL); 785195cf617SRuslan Ermilov NG_NODE_UNREF(node); 7861ede983cSDag-Erling Smørgrav free(priv->tab, M_NETGRAPH_BRIDGE); 7871ede983cSDag-Erling Smørgrav free(priv, M_NETGRAPH_BRIDGE); 788ed2dbd31SArchie Cobbs return (0); 789ed2dbd31SArchie Cobbs } 790ed2dbd31SArchie Cobbs 791ed2dbd31SArchie Cobbs /* 792ed2dbd31SArchie Cobbs * Hook disconnection. 793ed2dbd31SArchie Cobbs */ 794ed2dbd31SArchie Cobbs static int 795ed2dbd31SArchie Cobbs ng_bridge_disconnect(hook_p hook) 796ed2dbd31SArchie Cobbs { 79730400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 798ed2dbd31SArchie Cobbs int linkNum; 799ed2dbd31SArchie Cobbs 800ed2dbd31SArchie Cobbs /* Get link number */ 8018214d60eSJohn Baldwin linkNum = (intptr_t)NG_HOOK_PRIVATE(hook); 802ed2dbd31SArchie Cobbs KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, 8036e551fb6SDavid E. O'Brien ("%s: linkNum=%u", __func__, linkNum)); 804ed2dbd31SArchie Cobbs 805ed2dbd31SArchie Cobbs /* Remove all hosts associated with this link */ 806ed2dbd31SArchie Cobbs ng_bridge_remove_hosts(priv, linkNum); 807ed2dbd31SArchie Cobbs 808ed2dbd31SArchie Cobbs /* Free associated link information */ 8096e551fb6SDavid E. O'Brien KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __func__)); 8101ede983cSDag-Erling Smørgrav free(priv->links[linkNum], M_NETGRAPH_BRIDGE); 811ed2dbd31SArchie Cobbs priv->links[linkNum] = NULL; 812ed2dbd31SArchie Cobbs priv->numLinks--; 813ed2dbd31SArchie Cobbs 814ed2dbd31SArchie Cobbs /* If no more hooks, go away */ 81530400f03SJulian Elischer if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 816*f8aab721SMarko Zec && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook))) 817*f8aab721SMarko Zec && !priv->persistent) { 81830400f03SJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook)); 81930400f03SJulian Elischer } 820ed2dbd31SArchie Cobbs return (0); 821ed2dbd31SArchie Cobbs } 822ed2dbd31SArchie Cobbs 823ed2dbd31SArchie Cobbs /****************************************************************** 824ed2dbd31SArchie Cobbs HASH TABLE FUNCTIONS 825ed2dbd31SArchie Cobbs ******************************************************************/ 826ed2dbd31SArchie Cobbs 827ed2dbd31SArchie Cobbs /* 828ed2dbd31SArchie Cobbs * Hash algorithm 829ed2dbd31SArchie Cobbs */ 830ed2dbd31SArchie Cobbs #define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \ 831ed2dbd31SArchie Cobbs ^ ((const u_int16_t *)(addr))[1] \ 832ed2dbd31SArchie Cobbs ^ ((const u_int16_t *)(addr))[2]) & (mask) ) 833ed2dbd31SArchie Cobbs 834ed2dbd31SArchie Cobbs /* 835ed2dbd31SArchie Cobbs * Find a host entry in the table. 836ed2dbd31SArchie Cobbs */ 837ed2dbd31SArchie Cobbs static struct ng_bridge_host * 838ed2dbd31SArchie Cobbs ng_bridge_get(priv_p priv, const u_char *addr) 839ed2dbd31SArchie Cobbs { 840ed2dbd31SArchie Cobbs const int bucket = HASH(addr, priv->hashMask); 841ed2dbd31SArchie Cobbs struct ng_bridge_hent *hent; 842ed2dbd31SArchie Cobbs 843ed2dbd31SArchie Cobbs SLIST_FOREACH(hent, &priv->tab[bucket], next) { 844ed2dbd31SArchie Cobbs if (ETHER_EQUAL(hent->host.addr, addr)) 845ed2dbd31SArchie Cobbs return (&hent->host); 846ed2dbd31SArchie Cobbs } 847ed2dbd31SArchie Cobbs return (NULL); 848ed2dbd31SArchie Cobbs } 849ed2dbd31SArchie Cobbs 850ed2dbd31SArchie Cobbs /* 851ed2dbd31SArchie Cobbs * Add a new host entry to the table. This assumes the host doesn't 852ed2dbd31SArchie Cobbs * already exist in the table. Returns 1 on success, 0 if there 853ed2dbd31SArchie Cobbs * was a memory allocation failure. 854ed2dbd31SArchie Cobbs */ 855ed2dbd31SArchie Cobbs static int 856ed2dbd31SArchie Cobbs ng_bridge_put(priv_p priv, const u_char *addr, int linkNum) 857ed2dbd31SArchie Cobbs { 858ed2dbd31SArchie Cobbs const int bucket = HASH(addr, priv->hashMask); 859ed2dbd31SArchie Cobbs struct ng_bridge_hent *hent; 860ed2dbd31SArchie Cobbs 861ed2dbd31SArchie Cobbs #ifdef INVARIANTS 862ed2dbd31SArchie Cobbs /* Assert that entry does not already exist in hashtable */ 863ed2dbd31SArchie Cobbs SLIST_FOREACH(hent, &priv->tab[bucket], next) { 864ed2dbd31SArchie Cobbs KASSERT(!ETHER_EQUAL(hent->host.addr, addr), 8656e551fb6SDavid E. O'Brien ("%s: entry %6D exists in table", __func__, addr, ":")); 866ed2dbd31SArchie Cobbs } 867ed2dbd31SArchie Cobbs #endif 868ed2dbd31SArchie Cobbs 869ed2dbd31SArchie Cobbs /* Allocate and initialize new hashtable entry */ 8701ede983cSDag-Erling Smørgrav hent = malloc(sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT); 871ed2dbd31SArchie Cobbs if (hent == NULL) 872ed2dbd31SArchie Cobbs return (0); 873ed2dbd31SArchie Cobbs bcopy(addr, hent->host.addr, ETHER_ADDR_LEN); 874ed2dbd31SArchie Cobbs hent->host.linkNum = linkNum; 875ed2dbd31SArchie Cobbs hent->host.staleness = 0; 876ed2dbd31SArchie Cobbs hent->host.age = 0; 877ed2dbd31SArchie Cobbs 878ed2dbd31SArchie Cobbs /* Add new element to hash bucket */ 879ed2dbd31SArchie Cobbs SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next); 880ed2dbd31SArchie Cobbs priv->numHosts++; 881ed2dbd31SArchie Cobbs 882ed2dbd31SArchie Cobbs /* Resize table if necessary */ 883ed2dbd31SArchie Cobbs ng_bridge_rehash(priv); 884ed2dbd31SArchie Cobbs return (1); 885ed2dbd31SArchie Cobbs } 886ed2dbd31SArchie Cobbs 887ed2dbd31SArchie Cobbs /* 888ed2dbd31SArchie Cobbs * Resize the hash table. We try to maintain the number of buckets 889ed2dbd31SArchie Cobbs * such that the load factor is in the range 0.25 to 1.0. 890ed2dbd31SArchie Cobbs * 891ed2dbd31SArchie Cobbs * If we can't get the new memory then we silently fail. This is OK 892ed2dbd31SArchie Cobbs * because things will still work and we'll try again soon anyway. 893ed2dbd31SArchie Cobbs */ 894ed2dbd31SArchie Cobbs static void 895ed2dbd31SArchie Cobbs ng_bridge_rehash(priv_p priv) 896ed2dbd31SArchie Cobbs { 897ed2dbd31SArchie Cobbs struct ng_bridge_bucket *newTab; 898ed2dbd31SArchie Cobbs int oldBucket, newBucket; 899ed2dbd31SArchie Cobbs int newNumBuckets; 900ed2dbd31SArchie Cobbs u_int newMask; 901ed2dbd31SArchie Cobbs 902ed2dbd31SArchie Cobbs /* Is table too full or too empty? */ 903ed2dbd31SArchie Cobbs if (priv->numHosts > priv->numBuckets 904ed2dbd31SArchie Cobbs && (priv->numBuckets << 1) <= MAX_BUCKETS) 905ed2dbd31SArchie Cobbs newNumBuckets = priv->numBuckets << 1; 906ed2dbd31SArchie Cobbs else if (priv->numHosts < (priv->numBuckets >> 2) 907ed2dbd31SArchie Cobbs && (priv->numBuckets >> 2) >= MIN_BUCKETS) 908ed2dbd31SArchie Cobbs newNumBuckets = priv->numBuckets >> 2; 909ed2dbd31SArchie Cobbs else 910ed2dbd31SArchie Cobbs return; 911ed2dbd31SArchie Cobbs newMask = newNumBuckets - 1; 912ed2dbd31SArchie Cobbs 913ed2dbd31SArchie Cobbs /* Allocate and initialize new table */ 914e11e3f18SDag-Erling Smørgrav newTab = malloc(newNumBuckets * sizeof(*newTab), 915e11e3f18SDag-Erling Smørgrav M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 916ed2dbd31SArchie Cobbs if (newTab == NULL) 917ed2dbd31SArchie Cobbs return; 918ed2dbd31SArchie Cobbs 919ed2dbd31SArchie Cobbs /* Move all entries from old table to new table */ 920ed2dbd31SArchie Cobbs for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) { 921ed2dbd31SArchie Cobbs struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket]; 922ed2dbd31SArchie Cobbs 923ed2dbd31SArchie Cobbs while (!SLIST_EMPTY(oldList)) { 924ed2dbd31SArchie Cobbs struct ng_bridge_hent *const hent 925ed2dbd31SArchie Cobbs = SLIST_FIRST(oldList); 926ed2dbd31SArchie Cobbs 927ed2dbd31SArchie Cobbs SLIST_REMOVE_HEAD(oldList, next); 928ed2dbd31SArchie Cobbs newBucket = HASH(hent->host.addr, newMask); 929ed2dbd31SArchie Cobbs SLIST_INSERT_HEAD(&newTab[newBucket], hent, next); 930ed2dbd31SArchie Cobbs } 931ed2dbd31SArchie Cobbs } 932ed2dbd31SArchie Cobbs 933ed2dbd31SArchie Cobbs /* Replace old table with new one */ 934ed2dbd31SArchie Cobbs if (priv->conf.debugLevel >= 3) { 935ed2dbd31SArchie Cobbs log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n", 936ed2dbd31SArchie Cobbs ng_bridge_nodename(priv->node), 937ed2dbd31SArchie Cobbs priv->numBuckets, newNumBuckets); 938ed2dbd31SArchie Cobbs } 9391ede983cSDag-Erling Smørgrav free(priv->tab, M_NETGRAPH_BRIDGE); 940ed2dbd31SArchie Cobbs priv->numBuckets = newNumBuckets; 941ed2dbd31SArchie Cobbs priv->hashMask = newMask; 942ed2dbd31SArchie Cobbs priv->tab = newTab; 943ed2dbd31SArchie Cobbs return; 944ed2dbd31SArchie Cobbs } 945ed2dbd31SArchie Cobbs 946ed2dbd31SArchie Cobbs /****************************************************************** 947ed2dbd31SArchie Cobbs MISC FUNCTIONS 948ed2dbd31SArchie Cobbs ******************************************************************/ 949ed2dbd31SArchie Cobbs 950ed2dbd31SArchie Cobbs /* 951ed2dbd31SArchie Cobbs * Remove all hosts associated with a specific link from the hashtable. 952ed2dbd31SArchie Cobbs * If linkNum == -1, then remove all hosts in the table. 953ed2dbd31SArchie Cobbs */ 954ed2dbd31SArchie Cobbs static void 955ed2dbd31SArchie Cobbs ng_bridge_remove_hosts(priv_p priv, int linkNum) 956ed2dbd31SArchie Cobbs { 957ed2dbd31SArchie Cobbs int bucket; 958ed2dbd31SArchie Cobbs 959ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) { 960ed2dbd31SArchie Cobbs struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 961ed2dbd31SArchie Cobbs 962ed2dbd31SArchie Cobbs while (*hptr != NULL) { 963ed2dbd31SArchie Cobbs struct ng_bridge_hent *const hent = *hptr; 964ed2dbd31SArchie Cobbs 965ed2dbd31SArchie Cobbs if (linkNum == -1 || hent->host.linkNum == linkNum) { 966ed2dbd31SArchie Cobbs *hptr = SLIST_NEXT(hent, next); 9671ede983cSDag-Erling Smørgrav free(hent, M_NETGRAPH_BRIDGE); 968ed2dbd31SArchie Cobbs priv->numHosts--; 969ed2dbd31SArchie Cobbs } else 970ed2dbd31SArchie Cobbs hptr = &SLIST_NEXT(hent, next); 971ed2dbd31SArchie Cobbs } 972ed2dbd31SArchie Cobbs } 973ed2dbd31SArchie Cobbs } 974ed2dbd31SArchie Cobbs 975ed2dbd31SArchie Cobbs /* 976ed2dbd31SArchie Cobbs * Handle our once-per-second timeout event. We do two things: 977ed2dbd31SArchie Cobbs * we decrement link->loopCount for those links being muted due to 978ed2dbd31SArchie Cobbs * a detected loopback condition, and we remove any hosts from 979ed2dbd31SArchie Cobbs * the hashtable whom we haven't heard from in a long while. 980ed2dbd31SArchie Cobbs */ 981ed2dbd31SArchie Cobbs static void 982e0d32af7SGleb Smirnoff ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2) 983ed2dbd31SArchie Cobbs { 98430400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 985e0d32af7SGleb Smirnoff int bucket; 986ed2dbd31SArchie Cobbs int counter = 0; 987ed2dbd31SArchie Cobbs int linkNum; 988ed2dbd31SArchie Cobbs 989ed2dbd31SArchie Cobbs /* Update host time counters and remove stale entries */ 990ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) { 991ed2dbd31SArchie Cobbs struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 992ed2dbd31SArchie Cobbs 993ed2dbd31SArchie Cobbs while (*hptr != NULL) { 994ed2dbd31SArchie Cobbs struct ng_bridge_hent *const hent = *hptr; 995ed2dbd31SArchie Cobbs 996ed2dbd31SArchie Cobbs /* Make sure host's link really exists */ 997ed2dbd31SArchie Cobbs KASSERT(priv->links[hent->host.linkNum] != NULL, 998ed2dbd31SArchie Cobbs ("%s: host %6D on nonexistent link %d\n", 9996e551fb6SDavid E. O'Brien __func__, hent->host.addr, ":", 1000ed2dbd31SArchie Cobbs hent->host.linkNum)); 1001ed2dbd31SArchie Cobbs 1002ed2dbd31SArchie Cobbs /* Remove hosts we haven't heard from in a while */ 1003ed2dbd31SArchie Cobbs if (++hent->host.staleness >= priv->conf.maxStaleness) { 1004ed2dbd31SArchie Cobbs *hptr = SLIST_NEXT(hent, next); 10051ede983cSDag-Erling Smørgrav free(hent, M_NETGRAPH_BRIDGE); 1006ed2dbd31SArchie Cobbs priv->numHosts--; 1007ed2dbd31SArchie Cobbs } else { 1008ed2dbd31SArchie Cobbs if (hent->host.age < 0xffff) 1009ed2dbd31SArchie Cobbs hent->host.age++; 1010ed2dbd31SArchie Cobbs hptr = &SLIST_NEXT(hent, next); 1011ed2dbd31SArchie Cobbs counter++; 1012ed2dbd31SArchie Cobbs } 1013ed2dbd31SArchie Cobbs } 1014ed2dbd31SArchie Cobbs } 1015ed2dbd31SArchie Cobbs KASSERT(priv->numHosts == counter, 10166e551fb6SDavid E. O'Brien ("%s: hosts: %d != %d", __func__, priv->numHosts, counter)); 1017ed2dbd31SArchie Cobbs 1018ed2dbd31SArchie Cobbs /* Decrease table size if necessary */ 1019ed2dbd31SArchie Cobbs ng_bridge_rehash(priv); 1020ed2dbd31SArchie Cobbs 1021ed2dbd31SArchie Cobbs /* Decrease loop counter on muted looped back links */ 1022ed2dbd31SArchie Cobbs for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) { 1023ed2dbd31SArchie Cobbs struct ng_bridge_link *const link = priv->links[linkNum]; 1024ed2dbd31SArchie Cobbs 1025ed2dbd31SArchie Cobbs if (link != NULL) { 1026ed2dbd31SArchie Cobbs if (link->loopCount != 0) { 1027ed2dbd31SArchie Cobbs link->loopCount--; 1028ed2dbd31SArchie Cobbs if (link->loopCount == 0 1029ed2dbd31SArchie Cobbs && priv->conf.debugLevel >= 2) { 1030ed2dbd31SArchie Cobbs log(LOG_INFO, "ng_bridge: %s:" 1031ed2dbd31SArchie Cobbs " restoring looped back link%d\n", 1032ed2dbd31SArchie Cobbs ng_bridge_nodename(node), linkNum); 1033ed2dbd31SArchie Cobbs } 1034ed2dbd31SArchie Cobbs } 1035ed2dbd31SArchie Cobbs counter++; 1036ed2dbd31SArchie Cobbs } 1037ed2dbd31SArchie Cobbs } 1038ed2dbd31SArchie Cobbs KASSERT(priv->numLinks == counter, 10396e551fb6SDavid E. O'Brien ("%s: links: %d != %d", __func__, priv->numLinks, counter)); 1040ed2dbd31SArchie Cobbs 1041e0d32af7SGleb Smirnoff /* Register a new timeout, keeping the existing node reference */ 1042e0d32af7SGleb Smirnoff ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 1043ed2dbd31SArchie Cobbs } 1044ed2dbd31SArchie Cobbs 1045ed2dbd31SArchie Cobbs /* 1046ed2dbd31SArchie Cobbs * Return node's "name", even if it doesn't have one. 1047ed2dbd31SArchie Cobbs */ 1048ed2dbd31SArchie Cobbs static const char * 1049ed2dbd31SArchie Cobbs ng_bridge_nodename(node_p node) 1050ed2dbd31SArchie Cobbs { 105187e2c66aSHartmut Brandt static char name[NG_NODESIZ]; 1052ed2dbd31SArchie Cobbs 105330400f03SJulian Elischer if (NG_NODE_NAME(node) != NULL) 105430400f03SJulian Elischer snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node)); 1055ed2dbd31SArchie Cobbs else 1056ed2dbd31SArchie Cobbs snprintf(name, sizeof(name), "[%x]", ng_node2ID(node)); 1057ed2dbd31SArchie Cobbs return name; 1058ed2dbd31SArchie Cobbs } 1059ed2dbd31SArchie Cobbs 1060