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> 64ed2dbd31SArchie Cobbs #include <sys/malloc.h> 65ed2dbd31SArchie Cobbs #include <sys/mbuf.h> 66ed2dbd31SArchie Cobbs #include <sys/errno.h> 67ed2dbd31SArchie Cobbs #include <sys/syslog.h> 68ed2dbd31SArchie Cobbs #include <sys/socket.h> 69ed2dbd31SArchie Cobbs #include <sys/ctype.h> 70603724d3SBjoern A. Zeeb #include <sys/vimage.h> 71ed2dbd31SArchie Cobbs 72ed2dbd31SArchie Cobbs #include <net/if.h> 73ed2dbd31SArchie Cobbs #include <net/ethernet.h> 74ed2dbd31SArchie Cobbs 75ed2dbd31SArchie Cobbs #include <netinet/in.h> 76ed2dbd31SArchie Cobbs #include <netinet/ip_fw.h> 77ed2dbd31SArchie Cobbs 78ed2dbd31SArchie Cobbs #include <netgraph/ng_message.h> 79ed2dbd31SArchie Cobbs #include <netgraph/netgraph.h> 80ed2dbd31SArchie Cobbs #include <netgraph/ng_parse.h> 81ed2dbd31SArchie Cobbs #include <netgraph/ng_bridge.h> 82ed2dbd31SArchie Cobbs 839c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC 849c8c302fSJulian Elischer MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge", "netgraph bridge node "); 859c8c302fSJulian Elischer #else 869c8c302fSJulian Elischer #define M_NETGRAPH_BRIDGE M_NETGRAPH 879c8c302fSJulian Elischer #endif 889c8c302fSJulian Elischer 89ed2dbd31SArchie Cobbs /* Per-link private data */ 90ed2dbd31SArchie Cobbs struct ng_bridge_link { 91ed2dbd31SArchie Cobbs hook_p hook; /* netgraph hook */ 92ed2dbd31SArchie Cobbs u_int16_t loopCount; /* loop ignore timer */ 93ed2dbd31SArchie Cobbs struct ng_bridge_link_stats stats; /* link stats */ 94ed2dbd31SArchie Cobbs }; 95ed2dbd31SArchie Cobbs 96ed2dbd31SArchie Cobbs /* Per-node private data */ 97ed2dbd31SArchie Cobbs struct ng_bridge_private { 98ed2dbd31SArchie Cobbs struct ng_bridge_bucket *tab; /* hash table bucket array */ 99ed2dbd31SArchie Cobbs struct ng_bridge_link *links[NG_BRIDGE_MAX_LINKS]; 100ed2dbd31SArchie Cobbs struct ng_bridge_config conf; /* node configuration */ 101ed2dbd31SArchie Cobbs node_p node; /* netgraph node */ 102ed2dbd31SArchie Cobbs u_int numHosts; /* num entries in table */ 103ed2dbd31SArchie Cobbs u_int numBuckets; /* num buckets in table */ 104ed2dbd31SArchie Cobbs u_int hashMask; /* numBuckets - 1 */ 105ed2dbd31SArchie Cobbs int numLinks; /* num connected links */ 106ed2dbd31SArchie Cobbs struct callout timer; /* one second periodic timer */ 107ed2dbd31SArchie Cobbs }; 108ed2dbd31SArchie Cobbs typedef struct ng_bridge_private *priv_p; 109ed2dbd31SArchie Cobbs 110ed2dbd31SArchie Cobbs /* Information about a host, stored in a hash table entry */ 111ed2dbd31SArchie Cobbs struct ng_bridge_hent { 112ed2dbd31SArchie Cobbs struct ng_bridge_host host; /* actual host info */ 113ed2dbd31SArchie Cobbs SLIST_ENTRY(ng_bridge_hent) next; /* next entry in bucket */ 114ed2dbd31SArchie Cobbs }; 115ed2dbd31SArchie Cobbs 116ed2dbd31SArchie Cobbs /* Hash table bucket declaration */ 117ed2dbd31SArchie Cobbs SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent); 118ed2dbd31SArchie Cobbs 119ed2dbd31SArchie Cobbs /* Netgraph node methods */ 120ed2dbd31SArchie Cobbs static ng_constructor_t ng_bridge_constructor; 121ed2dbd31SArchie Cobbs static ng_rcvmsg_t ng_bridge_rcvmsg; 122069154d5SJulian Elischer static ng_shutdown_t ng_bridge_shutdown; 123ed2dbd31SArchie Cobbs static ng_newhook_t ng_bridge_newhook; 124ed2dbd31SArchie Cobbs static ng_rcvdata_t ng_bridge_rcvdata; 125ed2dbd31SArchie Cobbs static ng_disconnect_t ng_bridge_disconnect; 126ed2dbd31SArchie Cobbs 127ed2dbd31SArchie Cobbs /* Other internal functions */ 128ed2dbd31SArchie Cobbs static struct ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr); 129ed2dbd31SArchie Cobbs static int ng_bridge_put(priv_p priv, const u_char *addr, int linkNum); 130ed2dbd31SArchie Cobbs static void ng_bridge_rehash(priv_p priv); 131ed2dbd31SArchie Cobbs static void ng_bridge_remove_hosts(priv_p priv, int linkNum); 132e0d32af7SGleb Smirnoff static void ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2); 133ed2dbd31SArchie Cobbs static const char *ng_bridge_nodename(node_p node); 134ed2dbd31SArchie Cobbs 135ed2dbd31SArchie Cobbs /* Ethernet broadcast */ 136ed2dbd31SArchie Cobbs static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] = 137ed2dbd31SArchie Cobbs { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 138ed2dbd31SArchie Cobbs 139ed2dbd31SArchie Cobbs /* Store each hook's link number in the private field */ 140ed2dbd31SArchie Cobbs #define LINK_NUM(hook) (*(u_int16_t *)(&(hook)->private)) 141ed2dbd31SArchie Cobbs 142ed2dbd31SArchie Cobbs /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */ 143ed2dbd31SArchie Cobbs #define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \ 144ed2dbd31SArchie Cobbs == ((const u_int32_t *)(b))[0] \ 145ed2dbd31SArchie Cobbs && ((const u_int16_t *)(a))[2] \ 146ed2dbd31SArchie Cobbs == ((const u_int16_t *)(b))[2]) 147ed2dbd31SArchie Cobbs 148ed2dbd31SArchie Cobbs /* Minimum and maximum number of hash buckets. Must be a power of two. */ 149ed2dbd31SArchie Cobbs #define MIN_BUCKETS (1 << 5) /* 32 */ 150ed2dbd31SArchie Cobbs #define MAX_BUCKETS (1 << 14) /* 16384 */ 151ed2dbd31SArchie Cobbs 152ed2dbd31SArchie Cobbs /* Configuration default values */ 153ed2dbd31SArchie Cobbs #define DEFAULT_LOOP_TIMEOUT 60 154ed2dbd31SArchie Cobbs #define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */ 155ed2dbd31SArchie Cobbs #define DEFAULT_MIN_STABLE_AGE 1 156ed2dbd31SArchie Cobbs 157ed2dbd31SArchie Cobbs /****************************************************************** 158ed2dbd31SArchie Cobbs NETGRAPH PARSE TYPES 159ed2dbd31SArchie Cobbs ******************************************************************/ 160ed2dbd31SArchie Cobbs 161ed2dbd31SArchie Cobbs /* 162ed2dbd31SArchie Cobbs * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE 163ed2dbd31SArchie Cobbs */ 164ed2dbd31SArchie Cobbs static int 165ed2dbd31SArchie Cobbs ng_bridge_getTableLength(const struct ng_parse_type *type, 166ed2dbd31SArchie Cobbs const u_char *start, const u_char *buf) 167ed2dbd31SArchie Cobbs { 168ed2dbd31SArchie Cobbs const struct ng_bridge_host_ary *const hary 169ed2dbd31SArchie Cobbs = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t)); 170ed2dbd31SArchie Cobbs 171ed2dbd31SArchie Cobbs return hary->numHosts; 172ed2dbd31SArchie Cobbs } 173ed2dbd31SArchie Cobbs 174ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_host_ary */ 175f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_type_fields[] 1768c7e4101SRuslan Ermilov = NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type); 177ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_type = { 178ed2dbd31SArchie Cobbs &ng_parse_struct_type, 179f0184ff8SArchie Cobbs &ng_bridge_host_type_fields 180ed2dbd31SArchie Cobbs }; 181ed2dbd31SArchie Cobbs static const struct ng_parse_array_info ng_bridge_hary_type_info = { 182ed2dbd31SArchie Cobbs &ng_bridge_host_type, 183ed2dbd31SArchie Cobbs ng_bridge_getTableLength 184ed2dbd31SArchie Cobbs }; 185ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_hary_type = { 186ed2dbd31SArchie Cobbs &ng_parse_array_type, 187ed2dbd31SArchie Cobbs &ng_bridge_hary_type_info 188ed2dbd31SArchie Cobbs }; 189f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[] 190ed2dbd31SArchie Cobbs = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type); 191ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_ary_type = { 192ed2dbd31SArchie Cobbs &ng_parse_struct_type, 193f0184ff8SArchie Cobbs &ng_bridge_host_ary_type_fields 194ed2dbd31SArchie Cobbs }; 195ed2dbd31SArchie Cobbs 196ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_config */ 197ed2dbd31SArchie Cobbs static const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = { 198ed2dbd31SArchie Cobbs &ng_parse_uint8_type, 199ed2dbd31SArchie Cobbs NG_BRIDGE_MAX_LINKS 200ed2dbd31SArchie Cobbs }; 201ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_ipfwary_type = { 202ed2dbd31SArchie Cobbs &ng_parse_fixedarray_type, 203ed2dbd31SArchie Cobbs &ng_bridge_ipfwary_type_info 204ed2dbd31SArchie Cobbs }; 205f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_config_type_fields[] 206ed2dbd31SArchie Cobbs = NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type); 207ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_config_type = { 208ed2dbd31SArchie Cobbs &ng_parse_struct_type, 209f0184ff8SArchie Cobbs &ng_bridge_config_type_fields 210ed2dbd31SArchie Cobbs }; 211ed2dbd31SArchie Cobbs 212ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_link_stat */ 213f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_stats_type_fields[] 214f0184ff8SArchie Cobbs = NG_BRIDGE_STATS_TYPE_INFO; 215ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_stats_type = { 216ed2dbd31SArchie Cobbs &ng_parse_struct_type, 217f0184ff8SArchie Cobbs &ng_bridge_stats_type_fields 218ed2dbd31SArchie Cobbs }; 219ed2dbd31SArchie Cobbs 220ed2dbd31SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */ 221ed2dbd31SArchie Cobbs static const struct ng_cmdlist ng_bridge_cmdlist[] = { 222ed2dbd31SArchie Cobbs { 223ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 224ed2dbd31SArchie Cobbs NGM_BRIDGE_SET_CONFIG, 225ed2dbd31SArchie Cobbs "setconfig", 226ed2dbd31SArchie Cobbs &ng_bridge_config_type, 227ed2dbd31SArchie Cobbs NULL 228ed2dbd31SArchie Cobbs }, 229ed2dbd31SArchie Cobbs { 230ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 231ed2dbd31SArchie Cobbs NGM_BRIDGE_GET_CONFIG, 232ed2dbd31SArchie Cobbs "getconfig", 233ed2dbd31SArchie Cobbs NULL, 234ed2dbd31SArchie Cobbs &ng_bridge_config_type 235ed2dbd31SArchie Cobbs }, 236ed2dbd31SArchie Cobbs { 237ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 238ed2dbd31SArchie Cobbs NGM_BRIDGE_RESET, 239ed2dbd31SArchie Cobbs "reset", 240ed2dbd31SArchie Cobbs NULL, 241ed2dbd31SArchie Cobbs NULL 242ed2dbd31SArchie Cobbs }, 243ed2dbd31SArchie Cobbs { 244ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 245ed2dbd31SArchie Cobbs NGM_BRIDGE_GET_STATS, 246ed2dbd31SArchie Cobbs "getstats", 247ed2dbd31SArchie Cobbs &ng_parse_uint32_type, 248ed2dbd31SArchie Cobbs &ng_bridge_stats_type 249ed2dbd31SArchie Cobbs }, 250ed2dbd31SArchie Cobbs { 251ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 252ed2dbd31SArchie Cobbs NGM_BRIDGE_CLR_STATS, 253ed2dbd31SArchie Cobbs "clrstats", 254ed2dbd31SArchie Cobbs &ng_parse_uint32_type, 255ed2dbd31SArchie Cobbs NULL 256ed2dbd31SArchie Cobbs }, 257ed2dbd31SArchie Cobbs { 258ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 259ed2dbd31SArchie Cobbs NGM_BRIDGE_GETCLR_STATS, 260ed2dbd31SArchie Cobbs "getclrstats", 261ed2dbd31SArchie Cobbs &ng_parse_uint32_type, 262ed2dbd31SArchie Cobbs &ng_bridge_stats_type 263ed2dbd31SArchie Cobbs }, 264ed2dbd31SArchie Cobbs { 265ed2dbd31SArchie Cobbs NGM_BRIDGE_COOKIE, 266ed2dbd31SArchie Cobbs NGM_BRIDGE_GET_TABLE, 267ed2dbd31SArchie Cobbs "gettable", 268ed2dbd31SArchie Cobbs NULL, 269ed2dbd31SArchie Cobbs &ng_bridge_host_ary_type 270ed2dbd31SArchie Cobbs }, 271ed2dbd31SArchie Cobbs { 0 } 272ed2dbd31SArchie Cobbs }; 273ed2dbd31SArchie Cobbs 274ed2dbd31SArchie Cobbs /* Node type descriptor */ 275ed2dbd31SArchie Cobbs static struct ng_type ng_bridge_typestruct = { 276f8aae777SJulian Elischer .version = NG_ABI_VERSION, 277f8aae777SJulian Elischer .name = NG_BRIDGE_NODE_TYPE, 278f8aae777SJulian Elischer .constructor = ng_bridge_constructor, 279f8aae777SJulian Elischer .rcvmsg = ng_bridge_rcvmsg, 280f8aae777SJulian Elischer .shutdown = ng_bridge_shutdown, 281f8aae777SJulian Elischer .newhook = ng_bridge_newhook, 282f8aae777SJulian Elischer .rcvdata = ng_bridge_rcvdata, 283f8aae777SJulian Elischer .disconnect = ng_bridge_disconnect, 284f8aae777SJulian Elischer .cmdlist = ng_bridge_cmdlist, 285ed2dbd31SArchie Cobbs }; 286a89effcdSArchie Cobbs NETGRAPH_INIT(bridge, &ng_bridge_typestruct); 287ed2dbd31SArchie Cobbs 288ed2dbd31SArchie Cobbs /****************************************************************** 289ed2dbd31SArchie Cobbs NETGRAPH NODE METHODS 290ed2dbd31SArchie Cobbs ******************************************************************/ 291ed2dbd31SArchie Cobbs 292ed2dbd31SArchie Cobbs /* 293ed2dbd31SArchie Cobbs * Node constructor 294ed2dbd31SArchie Cobbs */ 295ed2dbd31SArchie Cobbs static int 296069154d5SJulian Elischer ng_bridge_constructor(node_p node) 297ed2dbd31SArchie Cobbs { 298ed2dbd31SArchie Cobbs priv_p priv; 299ed2dbd31SArchie Cobbs 300ed2dbd31SArchie Cobbs /* Allocate and initialize private info */ 3011ede983cSDag-Erling Smørgrav priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 302ed2dbd31SArchie Cobbs if (priv == NULL) 303ed2dbd31SArchie Cobbs return (ENOMEM); 304e0d32af7SGleb Smirnoff ng_callout_init(&priv->timer); 305ed2dbd31SArchie Cobbs 306ed2dbd31SArchie Cobbs /* Allocate and initialize hash table, etc. */ 307e11e3f18SDag-Erling Smørgrav priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab), 308e11e3f18SDag-Erling Smørgrav M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 309ed2dbd31SArchie Cobbs if (priv->tab == NULL) { 3101ede983cSDag-Erling Smørgrav free(priv, M_NETGRAPH_BRIDGE); 311ed2dbd31SArchie Cobbs return (ENOMEM); 312ed2dbd31SArchie Cobbs } 313ed2dbd31SArchie Cobbs priv->numBuckets = MIN_BUCKETS; 314ed2dbd31SArchie Cobbs priv->hashMask = MIN_BUCKETS - 1; 315ed2dbd31SArchie Cobbs priv->conf.debugLevel = 1; 316ed2dbd31SArchie Cobbs priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT; 317ed2dbd31SArchie Cobbs priv->conf.maxStaleness = DEFAULT_MAX_STALENESS; 318ed2dbd31SArchie Cobbs priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE; 319ed2dbd31SArchie Cobbs 320069154d5SJulian Elischer /* 321069154d5SJulian Elischer * This node has all kinds of stuff that could be screwed by SMP. 322069154d5SJulian Elischer * Until it gets it's own internal protection, we go through in 323069154d5SJulian Elischer * single file. This could hurt a machine bridging beteen two 324069154d5SJulian Elischer * GB ethernets so it should be fixed. 325069154d5SJulian Elischer * When it's fixed the process SHOULD NOT SLEEP, spinlocks please! 326069154d5SJulian Elischer * (and atomic ops ) 327069154d5SJulian Elischer */ 32830400f03SJulian Elischer NG_NODE_FORCE_WRITER(node); 32930400f03SJulian Elischer NG_NODE_SET_PRIVATE(node, priv); 330069154d5SJulian Elischer priv->node = node; 331ed2dbd31SArchie Cobbs 3326c12c2b1SArchie Cobbs /* Start timer; timer is always running while node is alive */ 333e0d32af7SGleb Smirnoff ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 3346c12c2b1SArchie Cobbs 3356c12c2b1SArchie Cobbs /* Done */ 336ed2dbd31SArchie Cobbs return (0); 337ed2dbd31SArchie Cobbs } 338ed2dbd31SArchie Cobbs 339ed2dbd31SArchie Cobbs /* 340ed2dbd31SArchie Cobbs * Method for attaching a new hook 341ed2dbd31SArchie Cobbs */ 342ed2dbd31SArchie Cobbs static int 343ed2dbd31SArchie Cobbs ng_bridge_newhook(node_p node, hook_p hook, const char *name) 344ed2dbd31SArchie Cobbs { 34530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 346ed2dbd31SArchie Cobbs 347ed2dbd31SArchie Cobbs /* Check for a link hook */ 348ed2dbd31SArchie Cobbs if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX, 349ed2dbd31SArchie Cobbs strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) { 350ed2dbd31SArchie Cobbs const char *cp; 351ed2dbd31SArchie Cobbs char *eptr; 352ed2dbd31SArchie Cobbs u_long linkNum; 353ed2dbd31SArchie Cobbs 354ed2dbd31SArchie Cobbs cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX); 355ed2dbd31SArchie Cobbs if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 356ed2dbd31SArchie Cobbs return (EINVAL); 357ed2dbd31SArchie Cobbs linkNum = strtoul(cp, &eptr, 10); 358ed2dbd31SArchie Cobbs if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS) 359ed2dbd31SArchie Cobbs return (EINVAL); 360ed2dbd31SArchie Cobbs if (priv->links[linkNum] != NULL) 361ed2dbd31SArchie Cobbs return (EISCONN); 362e11e3f18SDag-Erling Smørgrav priv->links[linkNum] = malloc(sizeof(*priv->links[linkNum]), 363e11e3f18SDag-Erling Smørgrav M_NETGRAPH_BRIDGE, M_NOWAIT|M_ZERO); 364ed2dbd31SArchie Cobbs if (priv->links[linkNum] == NULL) 365ed2dbd31SArchie Cobbs return (ENOMEM); 366ed2dbd31SArchie Cobbs priv->links[linkNum]->hook = hook; 36730400f03SJulian Elischer NG_HOOK_SET_PRIVATE(hook, (void *)linkNum); 368ed2dbd31SArchie Cobbs priv->numLinks++; 369ed2dbd31SArchie Cobbs return (0); 370ed2dbd31SArchie Cobbs } 371ed2dbd31SArchie Cobbs 372ed2dbd31SArchie Cobbs /* Unknown hook name */ 373ed2dbd31SArchie Cobbs return (EINVAL); 374ed2dbd31SArchie Cobbs } 375ed2dbd31SArchie Cobbs 376ed2dbd31SArchie Cobbs /* 377ed2dbd31SArchie Cobbs * Receive a control message 378ed2dbd31SArchie Cobbs */ 379ed2dbd31SArchie Cobbs static int 380069154d5SJulian Elischer ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook) 381ed2dbd31SArchie Cobbs { 38230400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 383ed2dbd31SArchie Cobbs struct ng_mesg *resp = NULL; 384ed2dbd31SArchie Cobbs int error = 0; 385069154d5SJulian Elischer struct ng_mesg *msg; 386ed2dbd31SArchie Cobbs 387069154d5SJulian Elischer NGI_GET_MSG(item, msg); 388ed2dbd31SArchie Cobbs switch (msg->header.typecookie) { 389ed2dbd31SArchie Cobbs case NGM_BRIDGE_COOKIE: 390ed2dbd31SArchie Cobbs switch (msg->header.cmd) { 391ed2dbd31SArchie Cobbs case NGM_BRIDGE_GET_CONFIG: 392ed2dbd31SArchie Cobbs { 393ed2dbd31SArchie Cobbs struct ng_bridge_config *conf; 394ed2dbd31SArchie Cobbs 395ed2dbd31SArchie Cobbs NG_MKRESPONSE(resp, msg, 396ed2dbd31SArchie Cobbs sizeof(struct ng_bridge_config), M_NOWAIT); 397ed2dbd31SArchie Cobbs if (resp == NULL) { 398ed2dbd31SArchie Cobbs error = ENOMEM; 399ed2dbd31SArchie Cobbs break; 400ed2dbd31SArchie Cobbs } 401ed2dbd31SArchie Cobbs conf = (struct ng_bridge_config *)resp->data; 402ed2dbd31SArchie Cobbs *conf = priv->conf; /* no sanity checking needed */ 403ed2dbd31SArchie Cobbs break; 404ed2dbd31SArchie Cobbs } 405ed2dbd31SArchie Cobbs case NGM_BRIDGE_SET_CONFIG: 406ed2dbd31SArchie Cobbs { 407ed2dbd31SArchie Cobbs struct ng_bridge_config *conf; 408ed2dbd31SArchie Cobbs int i; 409ed2dbd31SArchie Cobbs 410ed2dbd31SArchie Cobbs if (msg->header.arglen 411ed2dbd31SArchie Cobbs != sizeof(struct ng_bridge_config)) { 412ed2dbd31SArchie Cobbs error = EINVAL; 413ed2dbd31SArchie Cobbs break; 414ed2dbd31SArchie Cobbs } 415ed2dbd31SArchie Cobbs conf = (struct ng_bridge_config *)msg->data; 416ed2dbd31SArchie Cobbs priv->conf = *conf; 417ed2dbd31SArchie Cobbs for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) 418ed2dbd31SArchie Cobbs priv->conf.ipfw[i] = !!priv->conf.ipfw[i]; 419ed2dbd31SArchie Cobbs break; 420ed2dbd31SArchie Cobbs } 421ed2dbd31SArchie Cobbs case NGM_BRIDGE_RESET: 422ed2dbd31SArchie Cobbs { 423ed2dbd31SArchie Cobbs int i; 424ed2dbd31SArchie Cobbs 425ed2dbd31SArchie Cobbs /* Flush all entries in the hash table */ 426ed2dbd31SArchie Cobbs ng_bridge_remove_hosts(priv, -1); 427ed2dbd31SArchie Cobbs 428ed2dbd31SArchie Cobbs /* Reset all loop detection counters and stats */ 429ed2dbd31SArchie Cobbs for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) { 430ed2dbd31SArchie Cobbs if (priv->links[i] == NULL) 431ed2dbd31SArchie Cobbs continue; 432ed2dbd31SArchie Cobbs priv->links[i]->loopCount = 0; 433ed2dbd31SArchie Cobbs bzero(&priv->links[i]->stats, 434ed2dbd31SArchie Cobbs sizeof(priv->links[i]->stats)); 435ed2dbd31SArchie Cobbs } 436ed2dbd31SArchie Cobbs break; 437ed2dbd31SArchie Cobbs } 438ed2dbd31SArchie Cobbs case NGM_BRIDGE_GET_STATS: 439ed2dbd31SArchie Cobbs case NGM_BRIDGE_CLR_STATS: 440ed2dbd31SArchie Cobbs case NGM_BRIDGE_GETCLR_STATS: 441ed2dbd31SArchie Cobbs { 442ed2dbd31SArchie Cobbs struct ng_bridge_link *link; 443ed2dbd31SArchie Cobbs int linkNum; 444ed2dbd31SArchie Cobbs 445ed2dbd31SArchie Cobbs /* Get link number */ 446ed2dbd31SArchie Cobbs if (msg->header.arglen != sizeof(u_int32_t)) { 447ed2dbd31SArchie Cobbs error = EINVAL; 448ed2dbd31SArchie Cobbs break; 449ed2dbd31SArchie Cobbs } 450ed2dbd31SArchie Cobbs linkNum = *((u_int32_t *)msg->data); 451ed2dbd31SArchie Cobbs if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) { 452ed2dbd31SArchie Cobbs error = EINVAL; 453ed2dbd31SArchie Cobbs break; 454ed2dbd31SArchie Cobbs } 455ed2dbd31SArchie Cobbs if ((link = priv->links[linkNum]) == NULL) { 456ed2dbd31SArchie Cobbs error = ENOTCONN; 457ed2dbd31SArchie Cobbs break; 458ed2dbd31SArchie Cobbs } 459ed2dbd31SArchie Cobbs 460ed2dbd31SArchie Cobbs /* Get/clear stats */ 461ed2dbd31SArchie Cobbs if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) { 462ed2dbd31SArchie Cobbs NG_MKRESPONSE(resp, msg, 463ed2dbd31SArchie Cobbs sizeof(link->stats), M_NOWAIT); 464ed2dbd31SArchie Cobbs if (resp == NULL) { 465ed2dbd31SArchie Cobbs error = ENOMEM; 466ed2dbd31SArchie Cobbs break; 467ed2dbd31SArchie Cobbs } 468ed2dbd31SArchie Cobbs bcopy(&link->stats, 469ed2dbd31SArchie Cobbs resp->data, sizeof(link->stats)); 470ed2dbd31SArchie Cobbs } 471ed2dbd31SArchie Cobbs if (msg->header.cmd != NGM_BRIDGE_GET_STATS) 472ed2dbd31SArchie Cobbs bzero(&link->stats, sizeof(link->stats)); 473ed2dbd31SArchie Cobbs break; 474ed2dbd31SArchie Cobbs } 475ed2dbd31SArchie Cobbs case NGM_BRIDGE_GET_TABLE: 476ed2dbd31SArchie Cobbs { 477ed2dbd31SArchie Cobbs struct ng_bridge_host_ary *ary; 478ed2dbd31SArchie Cobbs struct ng_bridge_hent *hent; 479ed2dbd31SArchie Cobbs int i = 0, bucket; 480ed2dbd31SArchie Cobbs 481ed2dbd31SArchie Cobbs NG_MKRESPONSE(resp, msg, sizeof(*ary) 482ed2dbd31SArchie Cobbs + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT); 483ed2dbd31SArchie Cobbs if (resp == NULL) { 484ed2dbd31SArchie Cobbs error = ENOMEM; 485ed2dbd31SArchie Cobbs break; 486ed2dbd31SArchie Cobbs } 487ed2dbd31SArchie Cobbs ary = (struct ng_bridge_host_ary *)resp->data; 488ed2dbd31SArchie Cobbs ary->numHosts = priv->numHosts; 489ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) { 490ed2dbd31SArchie Cobbs SLIST_FOREACH(hent, &priv->tab[bucket], next) 491ed2dbd31SArchie Cobbs ary->hosts[i++] = hent->host; 492ed2dbd31SArchie Cobbs } 493ed2dbd31SArchie Cobbs break; 494ed2dbd31SArchie Cobbs } 495ed2dbd31SArchie Cobbs default: 496ed2dbd31SArchie Cobbs error = EINVAL; 497ed2dbd31SArchie Cobbs break; 498ed2dbd31SArchie Cobbs } 499ed2dbd31SArchie Cobbs break; 500ed2dbd31SArchie Cobbs default: 501ed2dbd31SArchie Cobbs error = EINVAL; 502ed2dbd31SArchie Cobbs break; 503ed2dbd31SArchie Cobbs } 504ed2dbd31SArchie Cobbs 505ed2dbd31SArchie Cobbs /* Done */ 506069154d5SJulian Elischer NG_RESPOND_MSG(error, node, item, resp); 507069154d5SJulian Elischer NG_FREE_MSG(msg); 508ed2dbd31SArchie Cobbs return (error); 509ed2dbd31SArchie Cobbs } 510ed2dbd31SArchie Cobbs 511ed2dbd31SArchie Cobbs /* 512ed2dbd31SArchie Cobbs * Receive data on a hook 513ed2dbd31SArchie Cobbs */ 514ed2dbd31SArchie Cobbs static int 515069154d5SJulian Elischer ng_bridge_rcvdata(hook_p hook, item_p item) 516ed2dbd31SArchie Cobbs { 51730400f03SJulian Elischer const node_p node = NG_HOOK_NODE(hook); 51830400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 519ed2dbd31SArchie Cobbs struct ng_bridge_host *host; 520ed2dbd31SArchie Cobbs struct ng_bridge_link *link; 521ed2dbd31SArchie Cobbs struct ether_header *eh; 522114bf091SBrian Feldman int error = 0, linkNum, linksSeen; 523069154d5SJulian Elischer int manycast; 524069154d5SJulian Elischer struct mbuf *m; 525069154d5SJulian Elischer struct ng_bridge_link *firstLink; 526ed2dbd31SArchie Cobbs 527069154d5SJulian Elischer NGI_GET_M(item, m); 528ed2dbd31SArchie Cobbs /* Get link number */ 5298214d60eSJohn Baldwin linkNum = (intptr_t)NG_HOOK_PRIVATE(hook); 530ed2dbd31SArchie Cobbs KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, 5316e551fb6SDavid E. O'Brien ("%s: linkNum=%u", __func__, linkNum)); 532ed2dbd31SArchie Cobbs link = priv->links[linkNum]; 5336e551fb6SDavid E. O'Brien KASSERT(link != NULL, ("%s: link%d null", __func__, linkNum)); 534ed2dbd31SArchie Cobbs 535ed2dbd31SArchie Cobbs /* Sanity check packet and pull up header */ 536ed2dbd31SArchie Cobbs if (m->m_pkthdr.len < ETHER_HDR_LEN) { 537ed2dbd31SArchie Cobbs link->stats.recvRunts++; 538069154d5SJulian Elischer NG_FREE_ITEM(item); 539069154d5SJulian Elischer NG_FREE_M(m); 540ed2dbd31SArchie Cobbs return (EINVAL); 541ed2dbd31SArchie Cobbs } 542ed2dbd31SArchie Cobbs if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) { 543ed2dbd31SArchie Cobbs link->stats.memoryFailures++; 544069154d5SJulian Elischer NG_FREE_ITEM(item); 545ed2dbd31SArchie Cobbs return (ENOBUFS); 546ed2dbd31SArchie Cobbs } 547ed2dbd31SArchie Cobbs eh = mtod(m, struct ether_header *); 548ed2dbd31SArchie Cobbs if ((eh->ether_shost[0] & 1) != 0) { 549ed2dbd31SArchie Cobbs link->stats.recvInvalid++; 550069154d5SJulian Elischer NG_FREE_ITEM(item); 551069154d5SJulian Elischer NG_FREE_M(m); 552ed2dbd31SArchie Cobbs return (EINVAL); 553ed2dbd31SArchie Cobbs } 554ed2dbd31SArchie Cobbs 555ed2dbd31SArchie Cobbs /* Is link disabled due to a loopback condition? */ 556ed2dbd31SArchie Cobbs if (link->loopCount != 0) { 557ed2dbd31SArchie Cobbs link->stats.loopDrops++; 558069154d5SJulian Elischer NG_FREE_ITEM(item); 559069154d5SJulian Elischer NG_FREE_M(m); 560ed2dbd31SArchie Cobbs return (ELOOP); /* XXX is this an appropriate error? */ 561ed2dbd31SArchie Cobbs } 562ed2dbd31SArchie Cobbs 563ed2dbd31SArchie Cobbs /* Update stats */ 564ed2dbd31SArchie Cobbs link->stats.recvPackets++; 565ed2dbd31SArchie Cobbs link->stats.recvOctets += m->m_pkthdr.len; 566ed2dbd31SArchie Cobbs if ((manycast = (eh->ether_dhost[0] & 1)) != 0) { 567ed2dbd31SArchie Cobbs if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) { 568ed2dbd31SArchie Cobbs link->stats.recvBroadcasts++; 569ed2dbd31SArchie Cobbs manycast = 2; 570ed2dbd31SArchie Cobbs } else 571ed2dbd31SArchie Cobbs link->stats.recvMulticasts++; 572ed2dbd31SArchie Cobbs } 573ed2dbd31SArchie Cobbs 574ed2dbd31SArchie Cobbs /* Look up packet's source Ethernet address in hashtable */ 575ed2dbd31SArchie Cobbs if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) { 576ed2dbd31SArchie Cobbs 577ed2dbd31SArchie Cobbs /* Update time since last heard from this host */ 578ed2dbd31SArchie Cobbs host->staleness = 0; 579ed2dbd31SArchie Cobbs 580ed2dbd31SArchie Cobbs /* Did host jump to a different link? */ 581ed2dbd31SArchie Cobbs if (host->linkNum != linkNum) { 582ed2dbd31SArchie Cobbs 583ed2dbd31SArchie Cobbs /* 584ed2dbd31SArchie Cobbs * If the host's old link was recently established 585ed2dbd31SArchie Cobbs * on the old link and it's already jumped to a new 586ed2dbd31SArchie Cobbs * link, declare a loopback condition. 587ed2dbd31SArchie Cobbs */ 588ed2dbd31SArchie Cobbs if (host->age < priv->conf.minStableAge) { 589ed2dbd31SArchie Cobbs 590ed2dbd31SArchie Cobbs /* Log the problem */ 591ed2dbd31SArchie Cobbs if (priv->conf.debugLevel >= 2) { 592ed2dbd31SArchie Cobbs struct ifnet *ifp = m->m_pkthdr.rcvif; 593ed2dbd31SArchie Cobbs char suffix[32]; 594ed2dbd31SArchie Cobbs 595ed2dbd31SArchie Cobbs if (ifp != NULL) 596ed2dbd31SArchie Cobbs snprintf(suffix, sizeof(suffix), 5979bf40edeSBrooks Davis " (%s)", ifp->if_xname); 598ed2dbd31SArchie Cobbs else 599ed2dbd31SArchie Cobbs *suffix = '\0'; 600ed2dbd31SArchie Cobbs log(LOG_WARNING, "ng_bridge: %s:" 601ed2dbd31SArchie Cobbs " loopback detected on %s%s\n", 602ed2dbd31SArchie Cobbs ng_bridge_nodename(node), 60330400f03SJulian Elischer NG_HOOK_NAME(hook), suffix); 604ed2dbd31SArchie Cobbs } 605ed2dbd31SArchie Cobbs 606ed2dbd31SArchie Cobbs /* Mark link as linka non grata */ 607ed2dbd31SArchie Cobbs link->loopCount = priv->conf.loopTimeout; 608ed2dbd31SArchie Cobbs link->stats.loopDetects++; 609ed2dbd31SArchie Cobbs 610ed2dbd31SArchie Cobbs /* Forget all hosts on this link */ 611ed2dbd31SArchie Cobbs ng_bridge_remove_hosts(priv, linkNum); 612ed2dbd31SArchie Cobbs 613ed2dbd31SArchie Cobbs /* Drop packet */ 614ed2dbd31SArchie Cobbs link->stats.loopDrops++; 615069154d5SJulian Elischer NG_FREE_ITEM(item); 616069154d5SJulian Elischer NG_FREE_M(m); 617ed2dbd31SArchie Cobbs return (ELOOP); /* XXX appropriate? */ 618ed2dbd31SArchie Cobbs } 619ed2dbd31SArchie Cobbs 620ed2dbd31SArchie Cobbs /* Move host over to new link */ 621ed2dbd31SArchie Cobbs host->linkNum = linkNum; 622ed2dbd31SArchie Cobbs host->age = 0; 623ed2dbd31SArchie Cobbs } 624ed2dbd31SArchie Cobbs } else { 625ed2dbd31SArchie Cobbs if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) { 626ed2dbd31SArchie Cobbs link->stats.memoryFailures++; 627069154d5SJulian Elischer NG_FREE_ITEM(item); 628069154d5SJulian Elischer NG_FREE_M(m); 629ed2dbd31SArchie Cobbs return (ENOMEM); 630ed2dbd31SArchie Cobbs } 631ed2dbd31SArchie Cobbs } 632ed2dbd31SArchie Cobbs 633ed2dbd31SArchie Cobbs /* Run packet through ipfw processing, if enabled */ 6349b932e9eSAndre Oppermann #if 0 635603724d3SBjoern A. Zeeb if (priv->conf.ipfw[linkNum] && V_fw_enable && ip_fw_chk_ptr != NULL) { 636ed2dbd31SArchie Cobbs /* XXX not implemented yet */ 637ed2dbd31SArchie Cobbs } 6389b932e9eSAndre Oppermann #endif 639ed2dbd31SArchie Cobbs 640ed2dbd31SArchie Cobbs /* 641ed2dbd31SArchie Cobbs * If unicast and destination host known, deliver to host's link, 642ed2dbd31SArchie Cobbs * unless it is the same link as the packet came in on. 643ed2dbd31SArchie Cobbs */ 644ed2dbd31SArchie Cobbs if (!manycast) { 645ed2dbd31SArchie Cobbs 646ed2dbd31SArchie Cobbs /* Determine packet destination link */ 647ed2dbd31SArchie Cobbs if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) { 648ed2dbd31SArchie Cobbs struct ng_bridge_link *const destLink 649ed2dbd31SArchie Cobbs = priv->links[host->linkNum]; 650ed2dbd31SArchie Cobbs 651ed2dbd31SArchie Cobbs /* If destination same as incoming link, do nothing */ 652ed2dbd31SArchie Cobbs KASSERT(destLink != NULL, 6536e551fb6SDavid E. O'Brien ("%s: link%d null", __func__, host->linkNum)); 654ed2dbd31SArchie Cobbs if (destLink == link) { 655069154d5SJulian Elischer NG_FREE_ITEM(item); 656069154d5SJulian Elischer NG_FREE_M(m); 657ed2dbd31SArchie Cobbs return (0); 658ed2dbd31SArchie Cobbs } 659ed2dbd31SArchie Cobbs 660ed2dbd31SArchie Cobbs /* Deliver packet out the destination link */ 661ed2dbd31SArchie Cobbs destLink->stats.xmitPackets++; 662ed2dbd31SArchie Cobbs destLink->stats.xmitOctets += m->m_pkthdr.len; 663069154d5SJulian Elischer NG_FWD_NEW_DATA(error, item, destLink->hook, m); 664ed2dbd31SArchie Cobbs return (error); 665ed2dbd31SArchie Cobbs } 666ed2dbd31SArchie Cobbs 667ed2dbd31SArchie Cobbs /* Destination host is not known */ 668ed2dbd31SArchie Cobbs link->stats.recvUnknown++; 669ed2dbd31SArchie Cobbs } 670ed2dbd31SArchie Cobbs 671ed2dbd31SArchie Cobbs /* Distribute unknown, multicast, broadcast pkts to all other links */ 672069154d5SJulian Elischer firstLink = NULL; 673114bf091SBrian Feldman for (linkNum = linksSeen = 0; linksSeen <= priv->numLinks; linkNum++) { 674069154d5SJulian Elischer struct ng_bridge_link *destLink; 675069154d5SJulian Elischer struct mbuf *m2 = NULL; 676ed2dbd31SArchie Cobbs 677069154d5SJulian Elischer /* 678069154d5SJulian Elischer * If we have checked all the links then now 679069154d5SJulian Elischer * send the original on its reserved link 680069154d5SJulian Elischer */ 681114bf091SBrian Feldman if (linksSeen == priv->numLinks) { 682069154d5SJulian Elischer /* If we never saw a good link, leave. */ 683069154d5SJulian Elischer if (firstLink == NULL) { 684069154d5SJulian Elischer NG_FREE_ITEM(item); 685069154d5SJulian Elischer NG_FREE_M(m); 686069154d5SJulian Elischer return (0); 687069154d5SJulian Elischer } 688069154d5SJulian Elischer destLink = firstLink; 689ed2dbd31SArchie Cobbs } else { 690069154d5SJulian Elischer destLink = priv->links[linkNum]; 691114bf091SBrian Feldman if (destLink != NULL) 692114bf091SBrian Feldman linksSeen++; 693069154d5SJulian Elischer /* Skip incoming link and disconnected links */ 694069154d5SJulian Elischer if (destLink == NULL || destLink == link) { 695069154d5SJulian Elischer continue; 696069154d5SJulian Elischer } 697069154d5SJulian Elischer if (firstLink == NULL) { 698069154d5SJulian Elischer /* 699069154d5SJulian Elischer * This is the first usable link we have found. 700069154d5SJulian Elischer * Reserve it for the originals. 701069154d5SJulian Elischer * If we never find another we save a copy. 702069154d5SJulian Elischer */ 703069154d5SJulian Elischer firstLink = destLink; 704069154d5SJulian Elischer continue; 705069154d5SJulian Elischer } 706069154d5SJulian Elischer 707069154d5SJulian Elischer /* 708069154d5SJulian Elischer * It's usable link but not the reserved (first) one. 7093ca24c28SJulian Elischer * Copy mbuf info for sending. 710069154d5SJulian Elischer */ 711a163d034SWarner Losh m2 = m_dup(m, M_DONTWAIT); /* XXX m_copypacket() */ 712ed2dbd31SArchie Cobbs if (m2 == NULL) { 713ed2dbd31SArchie Cobbs link->stats.memoryFailures++; 714069154d5SJulian Elischer NG_FREE_ITEM(item); 715069154d5SJulian Elischer NG_FREE_M(m); 716ed2dbd31SArchie Cobbs return (ENOBUFS); 717ed2dbd31SArchie Cobbs } 718ed2dbd31SArchie Cobbs } 719ed2dbd31SArchie Cobbs 720ed2dbd31SArchie Cobbs /* Update stats */ 721ed2dbd31SArchie Cobbs destLink->stats.xmitPackets++; 722ed2dbd31SArchie Cobbs destLink->stats.xmitOctets += m->m_pkthdr.len; 723ed2dbd31SArchie Cobbs switch (manycast) { 724ed2dbd31SArchie Cobbs case 0: /* unicast */ 725ed2dbd31SArchie Cobbs break; 726ed2dbd31SArchie Cobbs case 1: /* multicast */ 727ed2dbd31SArchie Cobbs destLink->stats.xmitMulticasts++; 728ed2dbd31SArchie Cobbs break; 729ed2dbd31SArchie Cobbs case 2: /* broadcast */ 730ed2dbd31SArchie Cobbs destLink->stats.xmitBroadcasts++; 731ed2dbd31SArchie Cobbs break; 732ed2dbd31SArchie Cobbs } 733ed2dbd31SArchie Cobbs 734ed2dbd31SArchie Cobbs /* Send packet */ 735069154d5SJulian Elischer if (destLink == firstLink) { 736069154d5SJulian Elischer /* 737069154d5SJulian Elischer * If we've sent all the others, send the original 738069154d5SJulian Elischer * on the first link we found. 739069154d5SJulian Elischer */ 740069154d5SJulian Elischer NG_FWD_NEW_DATA(error, item, destLink->hook, m); 741069154d5SJulian Elischer break; /* always done last - not really needed. */ 742069154d5SJulian Elischer } else { 7433ca24c28SJulian Elischer NG_SEND_DATA_ONLY(error, destLink->hook, m2); 744ed2dbd31SArchie Cobbs } 745069154d5SJulian Elischer } 746ed2dbd31SArchie Cobbs return (error); 747ed2dbd31SArchie Cobbs } 748ed2dbd31SArchie Cobbs 749ed2dbd31SArchie Cobbs /* 750ed2dbd31SArchie Cobbs * Shutdown node 751ed2dbd31SArchie Cobbs */ 752ed2dbd31SArchie Cobbs static int 753069154d5SJulian Elischer ng_bridge_shutdown(node_p node) 754ed2dbd31SArchie Cobbs { 75530400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 756ed2dbd31SArchie Cobbs 7576c12c2b1SArchie Cobbs /* 758195cf617SRuslan Ermilov * Shut down everything including the timer. Even if the 759195cf617SRuslan Ermilov * callout has already been dequeued and is about to be 760195cf617SRuslan Ermilov * run, ng_bridge_timeout() won't be fired as the node 761195cf617SRuslan Ermilov * is already marked NGF_INVALID, so we're safe to free 762195cf617SRuslan Ermilov * the node now. 7636c12c2b1SArchie Cobbs */ 764ed2dbd31SArchie Cobbs KASSERT(priv->numLinks == 0 && priv->numHosts == 0, 765ed2dbd31SArchie Cobbs ("%s: numLinks=%d numHosts=%d", 7666e551fb6SDavid E. O'Brien __func__, priv->numLinks, priv->numHosts)); 767195cf617SRuslan Ermilov ng_uncallout(&priv->timer, node); 768195cf617SRuslan Ermilov NG_NODE_SET_PRIVATE(node, NULL); 769195cf617SRuslan Ermilov NG_NODE_UNREF(node); 7701ede983cSDag-Erling Smørgrav free(priv->tab, M_NETGRAPH_BRIDGE); 7711ede983cSDag-Erling Smørgrav free(priv, M_NETGRAPH_BRIDGE); 772ed2dbd31SArchie Cobbs return (0); 773ed2dbd31SArchie Cobbs } 774ed2dbd31SArchie Cobbs 775ed2dbd31SArchie Cobbs /* 776ed2dbd31SArchie Cobbs * Hook disconnection. 777ed2dbd31SArchie Cobbs */ 778ed2dbd31SArchie Cobbs static int 779ed2dbd31SArchie Cobbs ng_bridge_disconnect(hook_p hook) 780ed2dbd31SArchie Cobbs { 78130400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 782ed2dbd31SArchie Cobbs int linkNum; 783ed2dbd31SArchie Cobbs 784ed2dbd31SArchie Cobbs /* Get link number */ 7858214d60eSJohn Baldwin linkNum = (intptr_t)NG_HOOK_PRIVATE(hook); 786ed2dbd31SArchie Cobbs KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, 7876e551fb6SDavid E. O'Brien ("%s: linkNum=%u", __func__, linkNum)); 788ed2dbd31SArchie Cobbs 789ed2dbd31SArchie Cobbs /* Remove all hosts associated with this link */ 790ed2dbd31SArchie Cobbs ng_bridge_remove_hosts(priv, linkNum); 791ed2dbd31SArchie Cobbs 792ed2dbd31SArchie Cobbs /* Free associated link information */ 7936e551fb6SDavid E. O'Brien KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __func__)); 7941ede983cSDag-Erling Smørgrav free(priv->links[linkNum], M_NETGRAPH_BRIDGE); 795ed2dbd31SArchie Cobbs priv->links[linkNum] = NULL; 796ed2dbd31SArchie Cobbs priv->numLinks--; 797ed2dbd31SArchie Cobbs 798ed2dbd31SArchie Cobbs /* If no more hooks, go away */ 79930400f03SJulian Elischer if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 80030400f03SJulian Elischer && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) { 80130400f03SJulian Elischer ng_rmnode_self(NG_HOOK_NODE(hook)); 80230400f03SJulian Elischer } 803ed2dbd31SArchie Cobbs return (0); 804ed2dbd31SArchie Cobbs } 805ed2dbd31SArchie Cobbs 806ed2dbd31SArchie Cobbs /****************************************************************** 807ed2dbd31SArchie Cobbs HASH TABLE FUNCTIONS 808ed2dbd31SArchie Cobbs ******************************************************************/ 809ed2dbd31SArchie Cobbs 810ed2dbd31SArchie Cobbs /* 811ed2dbd31SArchie Cobbs * Hash algorithm 812ed2dbd31SArchie Cobbs */ 813ed2dbd31SArchie Cobbs #define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \ 814ed2dbd31SArchie Cobbs ^ ((const u_int16_t *)(addr))[1] \ 815ed2dbd31SArchie Cobbs ^ ((const u_int16_t *)(addr))[2]) & (mask) ) 816ed2dbd31SArchie Cobbs 817ed2dbd31SArchie Cobbs /* 818ed2dbd31SArchie Cobbs * Find a host entry in the table. 819ed2dbd31SArchie Cobbs */ 820ed2dbd31SArchie Cobbs static struct ng_bridge_host * 821ed2dbd31SArchie Cobbs ng_bridge_get(priv_p priv, const u_char *addr) 822ed2dbd31SArchie Cobbs { 823ed2dbd31SArchie Cobbs const int bucket = HASH(addr, priv->hashMask); 824ed2dbd31SArchie Cobbs struct ng_bridge_hent *hent; 825ed2dbd31SArchie Cobbs 826ed2dbd31SArchie Cobbs SLIST_FOREACH(hent, &priv->tab[bucket], next) { 827ed2dbd31SArchie Cobbs if (ETHER_EQUAL(hent->host.addr, addr)) 828ed2dbd31SArchie Cobbs return (&hent->host); 829ed2dbd31SArchie Cobbs } 830ed2dbd31SArchie Cobbs return (NULL); 831ed2dbd31SArchie Cobbs } 832ed2dbd31SArchie Cobbs 833ed2dbd31SArchie Cobbs /* 834ed2dbd31SArchie Cobbs * Add a new host entry to the table. This assumes the host doesn't 835ed2dbd31SArchie Cobbs * already exist in the table. Returns 1 on success, 0 if there 836ed2dbd31SArchie Cobbs * was a memory allocation failure. 837ed2dbd31SArchie Cobbs */ 838ed2dbd31SArchie Cobbs static int 839ed2dbd31SArchie Cobbs ng_bridge_put(priv_p priv, const u_char *addr, int linkNum) 840ed2dbd31SArchie Cobbs { 841ed2dbd31SArchie Cobbs const int bucket = HASH(addr, priv->hashMask); 842ed2dbd31SArchie Cobbs struct ng_bridge_hent *hent; 843ed2dbd31SArchie Cobbs 844ed2dbd31SArchie Cobbs #ifdef INVARIANTS 845ed2dbd31SArchie Cobbs /* Assert that entry does not already exist in hashtable */ 846ed2dbd31SArchie Cobbs SLIST_FOREACH(hent, &priv->tab[bucket], next) { 847ed2dbd31SArchie Cobbs KASSERT(!ETHER_EQUAL(hent->host.addr, addr), 8486e551fb6SDavid E. O'Brien ("%s: entry %6D exists in table", __func__, addr, ":")); 849ed2dbd31SArchie Cobbs } 850ed2dbd31SArchie Cobbs #endif 851ed2dbd31SArchie Cobbs 852ed2dbd31SArchie Cobbs /* Allocate and initialize new hashtable entry */ 8531ede983cSDag-Erling Smørgrav hent = malloc(sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT); 854ed2dbd31SArchie Cobbs if (hent == NULL) 855ed2dbd31SArchie Cobbs return (0); 856ed2dbd31SArchie Cobbs bcopy(addr, hent->host.addr, ETHER_ADDR_LEN); 857ed2dbd31SArchie Cobbs hent->host.linkNum = linkNum; 858ed2dbd31SArchie Cobbs hent->host.staleness = 0; 859ed2dbd31SArchie Cobbs hent->host.age = 0; 860ed2dbd31SArchie Cobbs 861ed2dbd31SArchie Cobbs /* Add new element to hash bucket */ 862ed2dbd31SArchie Cobbs SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next); 863ed2dbd31SArchie Cobbs priv->numHosts++; 864ed2dbd31SArchie Cobbs 865ed2dbd31SArchie Cobbs /* Resize table if necessary */ 866ed2dbd31SArchie Cobbs ng_bridge_rehash(priv); 867ed2dbd31SArchie Cobbs return (1); 868ed2dbd31SArchie Cobbs } 869ed2dbd31SArchie Cobbs 870ed2dbd31SArchie Cobbs /* 871ed2dbd31SArchie Cobbs * Resize the hash table. We try to maintain the number of buckets 872ed2dbd31SArchie Cobbs * such that the load factor is in the range 0.25 to 1.0. 873ed2dbd31SArchie Cobbs * 874ed2dbd31SArchie Cobbs * If we can't get the new memory then we silently fail. This is OK 875ed2dbd31SArchie Cobbs * because things will still work and we'll try again soon anyway. 876ed2dbd31SArchie Cobbs */ 877ed2dbd31SArchie Cobbs static void 878ed2dbd31SArchie Cobbs ng_bridge_rehash(priv_p priv) 879ed2dbd31SArchie Cobbs { 880ed2dbd31SArchie Cobbs struct ng_bridge_bucket *newTab; 881ed2dbd31SArchie Cobbs int oldBucket, newBucket; 882ed2dbd31SArchie Cobbs int newNumBuckets; 883ed2dbd31SArchie Cobbs u_int newMask; 884ed2dbd31SArchie Cobbs 885ed2dbd31SArchie Cobbs /* Is table too full or too empty? */ 886ed2dbd31SArchie Cobbs if (priv->numHosts > priv->numBuckets 887ed2dbd31SArchie Cobbs && (priv->numBuckets << 1) <= MAX_BUCKETS) 888ed2dbd31SArchie Cobbs newNumBuckets = priv->numBuckets << 1; 889ed2dbd31SArchie Cobbs else if (priv->numHosts < (priv->numBuckets >> 2) 890ed2dbd31SArchie Cobbs && (priv->numBuckets >> 2) >= MIN_BUCKETS) 891ed2dbd31SArchie Cobbs newNumBuckets = priv->numBuckets >> 2; 892ed2dbd31SArchie Cobbs else 893ed2dbd31SArchie Cobbs return; 894ed2dbd31SArchie Cobbs newMask = newNumBuckets - 1; 895ed2dbd31SArchie Cobbs 896ed2dbd31SArchie Cobbs /* Allocate and initialize new table */ 897e11e3f18SDag-Erling Smørgrav newTab = malloc(newNumBuckets * sizeof(*newTab), 898e11e3f18SDag-Erling Smørgrav M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 899ed2dbd31SArchie Cobbs if (newTab == NULL) 900ed2dbd31SArchie Cobbs return; 901ed2dbd31SArchie Cobbs 902ed2dbd31SArchie Cobbs /* Move all entries from old table to new table */ 903ed2dbd31SArchie Cobbs for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) { 904ed2dbd31SArchie Cobbs struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket]; 905ed2dbd31SArchie Cobbs 906ed2dbd31SArchie Cobbs while (!SLIST_EMPTY(oldList)) { 907ed2dbd31SArchie Cobbs struct ng_bridge_hent *const hent 908ed2dbd31SArchie Cobbs = SLIST_FIRST(oldList); 909ed2dbd31SArchie Cobbs 910ed2dbd31SArchie Cobbs SLIST_REMOVE_HEAD(oldList, next); 911ed2dbd31SArchie Cobbs newBucket = HASH(hent->host.addr, newMask); 912ed2dbd31SArchie Cobbs SLIST_INSERT_HEAD(&newTab[newBucket], hent, next); 913ed2dbd31SArchie Cobbs } 914ed2dbd31SArchie Cobbs } 915ed2dbd31SArchie Cobbs 916ed2dbd31SArchie Cobbs /* Replace old table with new one */ 917ed2dbd31SArchie Cobbs if (priv->conf.debugLevel >= 3) { 918ed2dbd31SArchie Cobbs log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n", 919ed2dbd31SArchie Cobbs ng_bridge_nodename(priv->node), 920ed2dbd31SArchie Cobbs priv->numBuckets, newNumBuckets); 921ed2dbd31SArchie Cobbs } 9221ede983cSDag-Erling Smørgrav free(priv->tab, M_NETGRAPH_BRIDGE); 923ed2dbd31SArchie Cobbs priv->numBuckets = newNumBuckets; 924ed2dbd31SArchie Cobbs priv->hashMask = newMask; 925ed2dbd31SArchie Cobbs priv->tab = newTab; 926ed2dbd31SArchie Cobbs return; 927ed2dbd31SArchie Cobbs } 928ed2dbd31SArchie Cobbs 929ed2dbd31SArchie Cobbs /****************************************************************** 930ed2dbd31SArchie Cobbs MISC FUNCTIONS 931ed2dbd31SArchie Cobbs ******************************************************************/ 932ed2dbd31SArchie Cobbs 933ed2dbd31SArchie Cobbs /* 934ed2dbd31SArchie Cobbs * Remove all hosts associated with a specific link from the hashtable. 935ed2dbd31SArchie Cobbs * If linkNum == -1, then remove all hosts in the table. 936ed2dbd31SArchie Cobbs */ 937ed2dbd31SArchie Cobbs static void 938ed2dbd31SArchie Cobbs ng_bridge_remove_hosts(priv_p priv, int linkNum) 939ed2dbd31SArchie Cobbs { 940ed2dbd31SArchie Cobbs int bucket; 941ed2dbd31SArchie Cobbs 942ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) { 943ed2dbd31SArchie Cobbs struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 944ed2dbd31SArchie Cobbs 945ed2dbd31SArchie Cobbs while (*hptr != NULL) { 946ed2dbd31SArchie Cobbs struct ng_bridge_hent *const hent = *hptr; 947ed2dbd31SArchie Cobbs 948ed2dbd31SArchie Cobbs if (linkNum == -1 || hent->host.linkNum == linkNum) { 949ed2dbd31SArchie Cobbs *hptr = SLIST_NEXT(hent, next); 9501ede983cSDag-Erling Smørgrav free(hent, M_NETGRAPH_BRIDGE); 951ed2dbd31SArchie Cobbs priv->numHosts--; 952ed2dbd31SArchie Cobbs } else 953ed2dbd31SArchie Cobbs hptr = &SLIST_NEXT(hent, next); 954ed2dbd31SArchie Cobbs } 955ed2dbd31SArchie Cobbs } 956ed2dbd31SArchie Cobbs } 957ed2dbd31SArchie Cobbs 958ed2dbd31SArchie Cobbs /* 959ed2dbd31SArchie Cobbs * Handle our once-per-second timeout event. We do two things: 960ed2dbd31SArchie Cobbs * we decrement link->loopCount for those links being muted due to 961ed2dbd31SArchie Cobbs * a detected loopback condition, and we remove any hosts from 962ed2dbd31SArchie Cobbs * the hashtable whom we haven't heard from in a long while. 963ed2dbd31SArchie Cobbs */ 964ed2dbd31SArchie Cobbs static void 965e0d32af7SGleb Smirnoff ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2) 966ed2dbd31SArchie Cobbs { 96730400f03SJulian Elischer const priv_p priv = NG_NODE_PRIVATE(node); 968e0d32af7SGleb Smirnoff int bucket; 969ed2dbd31SArchie Cobbs int counter = 0; 970ed2dbd31SArchie Cobbs int linkNum; 971ed2dbd31SArchie Cobbs 972ed2dbd31SArchie Cobbs /* Update host time counters and remove stale entries */ 973ed2dbd31SArchie Cobbs for (bucket = 0; bucket < priv->numBuckets; bucket++) { 974ed2dbd31SArchie Cobbs struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 975ed2dbd31SArchie Cobbs 976ed2dbd31SArchie Cobbs while (*hptr != NULL) { 977ed2dbd31SArchie Cobbs struct ng_bridge_hent *const hent = *hptr; 978ed2dbd31SArchie Cobbs 979ed2dbd31SArchie Cobbs /* Make sure host's link really exists */ 980ed2dbd31SArchie Cobbs KASSERT(priv->links[hent->host.linkNum] != NULL, 981ed2dbd31SArchie Cobbs ("%s: host %6D on nonexistent link %d\n", 9826e551fb6SDavid E. O'Brien __func__, hent->host.addr, ":", 983ed2dbd31SArchie Cobbs hent->host.linkNum)); 984ed2dbd31SArchie Cobbs 985ed2dbd31SArchie Cobbs /* Remove hosts we haven't heard from in a while */ 986ed2dbd31SArchie Cobbs if (++hent->host.staleness >= priv->conf.maxStaleness) { 987ed2dbd31SArchie Cobbs *hptr = SLIST_NEXT(hent, next); 9881ede983cSDag-Erling Smørgrav free(hent, M_NETGRAPH_BRIDGE); 989ed2dbd31SArchie Cobbs priv->numHosts--; 990ed2dbd31SArchie Cobbs } else { 991ed2dbd31SArchie Cobbs if (hent->host.age < 0xffff) 992ed2dbd31SArchie Cobbs hent->host.age++; 993ed2dbd31SArchie Cobbs hptr = &SLIST_NEXT(hent, next); 994ed2dbd31SArchie Cobbs counter++; 995ed2dbd31SArchie Cobbs } 996ed2dbd31SArchie Cobbs } 997ed2dbd31SArchie Cobbs } 998ed2dbd31SArchie Cobbs KASSERT(priv->numHosts == counter, 9996e551fb6SDavid E. O'Brien ("%s: hosts: %d != %d", __func__, priv->numHosts, counter)); 1000ed2dbd31SArchie Cobbs 1001ed2dbd31SArchie Cobbs /* Decrease table size if necessary */ 1002ed2dbd31SArchie Cobbs ng_bridge_rehash(priv); 1003ed2dbd31SArchie Cobbs 1004ed2dbd31SArchie Cobbs /* Decrease loop counter on muted looped back links */ 1005ed2dbd31SArchie Cobbs for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) { 1006ed2dbd31SArchie Cobbs struct ng_bridge_link *const link = priv->links[linkNum]; 1007ed2dbd31SArchie Cobbs 1008ed2dbd31SArchie Cobbs if (link != NULL) { 1009ed2dbd31SArchie Cobbs if (link->loopCount != 0) { 1010ed2dbd31SArchie Cobbs link->loopCount--; 1011ed2dbd31SArchie Cobbs if (link->loopCount == 0 1012ed2dbd31SArchie Cobbs && priv->conf.debugLevel >= 2) { 1013ed2dbd31SArchie Cobbs log(LOG_INFO, "ng_bridge: %s:" 1014ed2dbd31SArchie Cobbs " restoring looped back link%d\n", 1015ed2dbd31SArchie Cobbs ng_bridge_nodename(node), linkNum); 1016ed2dbd31SArchie Cobbs } 1017ed2dbd31SArchie Cobbs } 1018ed2dbd31SArchie Cobbs counter++; 1019ed2dbd31SArchie Cobbs } 1020ed2dbd31SArchie Cobbs } 1021ed2dbd31SArchie Cobbs KASSERT(priv->numLinks == counter, 10226e551fb6SDavid E. O'Brien ("%s: links: %d != %d", __func__, priv->numLinks, counter)); 1023ed2dbd31SArchie Cobbs 1024e0d32af7SGleb Smirnoff /* Register a new timeout, keeping the existing node reference */ 1025e0d32af7SGleb Smirnoff ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 1026ed2dbd31SArchie Cobbs } 1027ed2dbd31SArchie Cobbs 1028ed2dbd31SArchie Cobbs /* 1029ed2dbd31SArchie Cobbs * Return node's "name", even if it doesn't have one. 1030ed2dbd31SArchie Cobbs */ 1031ed2dbd31SArchie Cobbs static const char * 1032ed2dbd31SArchie Cobbs ng_bridge_nodename(node_p node) 1033ed2dbd31SArchie Cobbs { 103487e2c66aSHartmut Brandt static char name[NG_NODESIZ]; 1035ed2dbd31SArchie Cobbs 103630400f03SJulian Elischer if (NG_NODE_NAME(node) != NULL) 103730400f03SJulian Elischer snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node)); 1038ed2dbd31SArchie Cobbs else 1039ed2dbd31SArchie Cobbs snprintf(name, sizeof(name), "[%x]", ng_node2ID(node)); 1040ed2dbd31SArchie Cobbs return name; 1041ed2dbd31SArchie Cobbs } 1042ed2dbd31SArchie Cobbs 1043