ng_bridge.c revision 184214
165310Sarchie/* 265310Sarchie * ng_bridge.c 3139823Simp */ 4139823Simp 5139823Simp/*- 665310Sarchie * Copyright (c) 2000 Whistle Communications, Inc. 765310Sarchie * All rights reserved. 865310Sarchie * 965310Sarchie * Subject to the following obligations and disclaimer of warranty, use and 1065310Sarchie * redistribution of this software, in source or object code forms, with or 1165310Sarchie * without modifications are expressly permitted by Whistle Communications; 1265310Sarchie * provided, however, that: 1365310Sarchie * 1. Any and all reproductions of the source or object code must include the 1465310Sarchie * copyright notice above and the following disclaimer of warranties; and 1565310Sarchie * 2. No rights are granted, in any manner or form, to use Whistle 1665310Sarchie * Communications, Inc. trademarks, including the mark "WHISTLE 1765310Sarchie * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 1865310Sarchie * such appears in the above copyright notice or in the software. 1965310Sarchie * 2065310Sarchie * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 2165310Sarchie * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 2265310Sarchie * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 2365310Sarchie * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 2465310Sarchie * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 2565310Sarchie * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 2665310Sarchie * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 2765310Sarchie * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 2865310Sarchie * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 2965310Sarchie * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 3065310Sarchie * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 3165310Sarchie * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 3265310Sarchie * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 3365310Sarchie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3465310Sarchie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3565310Sarchie * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 3665310Sarchie * OF SUCH DAMAGE. 3765310Sarchie * 3865310Sarchie * Author: Archie Cobbs <archie@freebsd.org> 3965310Sarchie * 4065310Sarchie * $FreeBSD: head/sys/netgraph/ng_bridge.c 184214 2008-10-23 20:26:15Z des $ 4165310Sarchie */ 4265310Sarchie 4365310Sarchie/* 4465310Sarchie * ng_bridge(4) netgraph node type 4565310Sarchie * 4665310Sarchie * The node performs standard intelligent Ethernet bridging over 4765310Sarchie * each of its connected hooks, or links. A simple loop detection 4865310Sarchie * algorithm is included which disables a link for priv->conf.loopTimeout 4965310Sarchie * seconds when a host is seen to have jumped from one link to 5065310Sarchie * another within priv->conf.minStableAge seconds. 5165310Sarchie * 5265310Sarchie * We keep a hashtable that maps Ethernet addresses to host info, 5365310Sarchie * which is contained in struct ng_bridge_host's. These structures 5465310Sarchie * tell us on which link the host may be found. A host's entry will 5565310Sarchie * expire after priv->conf.maxStaleness seconds. 5665310Sarchie * 5765310Sarchie * This node is optimzed for stable networks, where machines jump 5865310Sarchie * from one port to the other only rarely. 5965310Sarchie */ 6065310Sarchie 6165310Sarchie#include <sys/param.h> 6265310Sarchie#include <sys/systm.h> 6365310Sarchie#include <sys/kernel.h> 6465310Sarchie#include <sys/malloc.h> 6565310Sarchie#include <sys/mbuf.h> 6665310Sarchie#include <sys/errno.h> 6765310Sarchie#include <sys/syslog.h> 6865310Sarchie#include <sys/socket.h> 6965310Sarchie#include <sys/ctype.h> 70181803Sbz#include <sys/vimage.h> 7165310Sarchie 7265310Sarchie#include <net/if.h> 7365310Sarchie#include <net/ethernet.h> 7465310Sarchie 7565310Sarchie#include <netinet/in.h> 7665310Sarchie#include <netinet/ip_fw.h> 7765310Sarchie 7865310Sarchie#include <netgraph/ng_message.h> 7965310Sarchie#include <netgraph/netgraph.h> 8065310Sarchie#include <netgraph/ng_parse.h> 8165310Sarchie#include <netgraph/ng_bridge.h> 8265310Sarchie 8370870Sjulian#ifdef NG_SEPARATE_MALLOC 8470870SjulianMALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge", "netgraph bridge node "); 8570870Sjulian#else 8670870Sjulian#define M_NETGRAPH_BRIDGE M_NETGRAPH 8770870Sjulian#endif 8870870Sjulian 8965310Sarchie/* Per-link private data */ 9065310Sarchiestruct ng_bridge_link { 9165310Sarchie hook_p hook; /* netgraph hook */ 9265310Sarchie u_int16_t loopCount; /* loop ignore timer */ 9365310Sarchie struct ng_bridge_link_stats stats; /* link stats */ 9465310Sarchie}; 9565310Sarchie 9665310Sarchie/* Per-node private data */ 9765310Sarchiestruct ng_bridge_private { 9865310Sarchie struct ng_bridge_bucket *tab; /* hash table bucket array */ 9965310Sarchie struct ng_bridge_link *links[NG_BRIDGE_MAX_LINKS]; 10065310Sarchie struct ng_bridge_config conf; /* node configuration */ 10165310Sarchie node_p node; /* netgraph node */ 10265310Sarchie u_int numHosts; /* num entries in table */ 10365310Sarchie u_int numBuckets; /* num buckets in table */ 10465310Sarchie u_int hashMask; /* numBuckets - 1 */ 10565310Sarchie int numLinks; /* num connected links */ 10665310Sarchie struct callout timer; /* one second periodic timer */ 10765310Sarchie}; 10865310Sarchietypedef struct ng_bridge_private *priv_p; 10965310Sarchie 11065310Sarchie/* Information about a host, stored in a hash table entry */ 11165310Sarchiestruct ng_bridge_hent { 11265310Sarchie struct ng_bridge_host host; /* actual host info */ 11365310Sarchie SLIST_ENTRY(ng_bridge_hent) next; /* next entry in bucket */ 11465310Sarchie}; 11565310Sarchie 11665310Sarchie/* Hash table bucket declaration */ 11765310SarchieSLIST_HEAD(ng_bridge_bucket, ng_bridge_hent); 11865310Sarchie 11965310Sarchie/* Netgraph node methods */ 12065310Sarchiestatic ng_constructor_t ng_bridge_constructor; 12165310Sarchiestatic ng_rcvmsg_t ng_bridge_rcvmsg; 12270700Sjulianstatic ng_shutdown_t ng_bridge_shutdown; 12365310Sarchiestatic ng_newhook_t ng_bridge_newhook; 12465310Sarchiestatic ng_rcvdata_t ng_bridge_rcvdata; 12565310Sarchiestatic ng_disconnect_t ng_bridge_disconnect; 12665310Sarchie 12765310Sarchie/* Other internal functions */ 12865310Sarchiestatic struct ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr); 12965310Sarchiestatic int ng_bridge_put(priv_p priv, const u_char *addr, int linkNum); 13065310Sarchiestatic void ng_bridge_rehash(priv_p priv); 13165310Sarchiestatic void ng_bridge_remove_hosts(priv_p priv, int linkNum); 132138834Sglebiusstatic void ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2); 13365310Sarchiestatic const char *ng_bridge_nodename(node_p node); 13465310Sarchie 13565310Sarchie/* Ethernet broadcast */ 13665310Sarchiestatic const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] = 13765310Sarchie { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 13865310Sarchie 13965310Sarchie/* Store each hook's link number in the private field */ 14065310Sarchie#define LINK_NUM(hook) (*(u_int16_t *)(&(hook)->private)) 14165310Sarchie 14265310Sarchie/* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */ 14365310Sarchie#define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \ 14465310Sarchie == ((const u_int32_t *)(b))[0] \ 14565310Sarchie && ((const u_int16_t *)(a))[2] \ 14665310Sarchie == ((const u_int16_t *)(b))[2]) 14765310Sarchie 14865310Sarchie/* Minimum and maximum number of hash buckets. Must be a power of two. */ 14965310Sarchie#define MIN_BUCKETS (1 << 5) /* 32 */ 15065310Sarchie#define MAX_BUCKETS (1 << 14) /* 16384 */ 15165310Sarchie 15265310Sarchie/* Configuration default values */ 15365310Sarchie#define DEFAULT_LOOP_TIMEOUT 60 15465310Sarchie#define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */ 15565310Sarchie#define DEFAULT_MIN_STABLE_AGE 1 15665310Sarchie 15765310Sarchie/****************************************************************** 15865310Sarchie NETGRAPH PARSE TYPES 15965310Sarchie******************************************************************/ 16065310Sarchie 16165310Sarchie/* 16265310Sarchie * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE 16365310Sarchie */ 16465310Sarchiestatic int 16565310Sarchieng_bridge_getTableLength(const struct ng_parse_type *type, 16665310Sarchie const u_char *start, const u_char *buf) 16765310Sarchie{ 16865310Sarchie const struct ng_bridge_host_ary *const hary 16965310Sarchie = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t)); 17065310Sarchie 17165310Sarchie return hary->numHosts; 17265310Sarchie} 17365310Sarchie 17465310Sarchie/* Parse type for struct ng_bridge_host_ary */ 17597685Sarchiestatic const struct ng_parse_struct_field ng_bridge_host_type_fields[] 176123600Sru = NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type); 17765310Sarchiestatic const struct ng_parse_type ng_bridge_host_type = { 17865310Sarchie &ng_parse_struct_type, 17997685Sarchie &ng_bridge_host_type_fields 18065310Sarchie}; 18165310Sarchiestatic const struct ng_parse_array_info ng_bridge_hary_type_info = { 18265310Sarchie &ng_bridge_host_type, 18365310Sarchie ng_bridge_getTableLength 18465310Sarchie}; 18565310Sarchiestatic const struct ng_parse_type ng_bridge_hary_type = { 18665310Sarchie &ng_parse_array_type, 18765310Sarchie &ng_bridge_hary_type_info 18865310Sarchie}; 18997685Sarchiestatic const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[] 19065310Sarchie = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type); 19165310Sarchiestatic const struct ng_parse_type ng_bridge_host_ary_type = { 19265310Sarchie &ng_parse_struct_type, 19397685Sarchie &ng_bridge_host_ary_type_fields 19465310Sarchie}; 19565310Sarchie 19665310Sarchie/* Parse type for struct ng_bridge_config */ 19765310Sarchiestatic const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = { 19865310Sarchie &ng_parse_uint8_type, 19965310Sarchie NG_BRIDGE_MAX_LINKS 20065310Sarchie}; 20165310Sarchiestatic const struct ng_parse_type ng_bridge_ipfwary_type = { 20265310Sarchie &ng_parse_fixedarray_type, 20365310Sarchie &ng_bridge_ipfwary_type_info 20465310Sarchie}; 20597685Sarchiestatic const struct ng_parse_struct_field ng_bridge_config_type_fields[] 20665310Sarchie = NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type); 20765310Sarchiestatic const struct ng_parse_type ng_bridge_config_type = { 20865310Sarchie &ng_parse_struct_type, 20997685Sarchie &ng_bridge_config_type_fields 21065310Sarchie}; 21165310Sarchie 21265310Sarchie/* Parse type for struct ng_bridge_link_stat */ 21397685Sarchiestatic const struct ng_parse_struct_field ng_bridge_stats_type_fields[] 21497685Sarchie = NG_BRIDGE_STATS_TYPE_INFO; 21565310Sarchiestatic const struct ng_parse_type ng_bridge_stats_type = { 21665310Sarchie &ng_parse_struct_type, 21797685Sarchie &ng_bridge_stats_type_fields 21865310Sarchie}; 21965310Sarchie 22065310Sarchie/* List of commands and how to convert arguments to/from ASCII */ 22165310Sarchiestatic const struct ng_cmdlist ng_bridge_cmdlist[] = { 22265310Sarchie { 22365310Sarchie NGM_BRIDGE_COOKIE, 22465310Sarchie NGM_BRIDGE_SET_CONFIG, 22565310Sarchie "setconfig", 22665310Sarchie &ng_bridge_config_type, 22765310Sarchie NULL 22865310Sarchie }, 22965310Sarchie { 23065310Sarchie NGM_BRIDGE_COOKIE, 23165310Sarchie NGM_BRIDGE_GET_CONFIG, 23265310Sarchie "getconfig", 23365310Sarchie NULL, 23465310Sarchie &ng_bridge_config_type 23565310Sarchie }, 23665310Sarchie { 23765310Sarchie NGM_BRIDGE_COOKIE, 23865310Sarchie NGM_BRIDGE_RESET, 23965310Sarchie "reset", 24065310Sarchie NULL, 24165310Sarchie NULL 24265310Sarchie }, 24365310Sarchie { 24465310Sarchie NGM_BRIDGE_COOKIE, 24565310Sarchie NGM_BRIDGE_GET_STATS, 24665310Sarchie "getstats", 24765310Sarchie &ng_parse_uint32_type, 24865310Sarchie &ng_bridge_stats_type 24965310Sarchie }, 25065310Sarchie { 25165310Sarchie NGM_BRIDGE_COOKIE, 25265310Sarchie NGM_BRIDGE_CLR_STATS, 25365310Sarchie "clrstats", 25465310Sarchie &ng_parse_uint32_type, 25565310Sarchie NULL 25665310Sarchie }, 25765310Sarchie { 25865310Sarchie NGM_BRIDGE_COOKIE, 25965310Sarchie NGM_BRIDGE_GETCLR_STATS, 26065310Sarchie "getclrstats", 26165310Sarchie &ng_parse_uint32_type, 26265310Sarchie &ng_bridge_stats_type 26365310Sarchie }, 26465310Sarchie { 26565310Sarchie NGM_BRIDGE_COOKIE, 26665310Sarchie NGM_BRIDGE_GET_TABLE, 26765310Sarchie "gettable", 26865310Sarchie NULL, 26965310Sarchie &ng_bridge_host_ary_type 27065310Sarchie }, 27165310Sarchie { 0 } 27265310Sarchie}; 27365310Sarchie 27465310Sarchie/* Node type descriptor */ 27565310Sarchiestatic struct ng_type ng_bridge_typestruct = { 276129823Sjulian .version = NG_ABI_VERSION, 277129823Sjulian .name = NG_BRIDGE_NODE_TYPE, 278129823Sjulian .constructor = ng_bridge_constructor, 279129823Sjulian .rcvmsg = ng_bridge_rcvmsg, 280129823Sjulian .shutdown = ng_bridge_shutdown, 281129823Sjulian .newhook = ng_bridge_newhook, 282129823Sjulian .rcvdata = ng_bridge_rcvdata, 283129823Sjulian .disconnect = ng_bridge_disconnect, 284129823Sjulian .cmdlist = ng_bridge_cmdlist, 28565310Sarchie}; 28666887SarchieNETGRAPH_INIT(bridge, &ng_bridge_typestruct); 28765310Sarchie 28865310Sarchie/****************************************************************** 28965310Sarchie NETGRAPH NODE METHODS 29065310Sarchie******************************************************************/ 29165310Sarchie 29265310Sarchie/* 29365310Sarchie * Node constructor 29465310Sarchie */ 29565310Sarchiestatic int 29670700Sjulianng_bridge_constructor(node_p node) 29765310Sarchie{ 29865310Sarchie priv_p priv; 29965310Sarchie 30065310Sarchie /* Allocate and initialize private info */ 301184205Sdes priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 30265310Sarchie if (priv == NULL) 30365310Sarchie return (ENOMEM); 304138834Sglebius ng_callout_init(&priv->timer); 30565310Sarchie 30665310Sarchie /* Allocate and initialize hash table, etc. */ 307184214Sdes priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab), 308184214Sdes M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 30965310Sarchie if (priv->tab == NULL) { 310184205Sdes free(priv, M_NETGRAPH_BRIDGE); 31165310Sarchie return (ENOMEM); 31265310Sarchie } 31365310Sarchie priv->numBuckets = MIN_BUCKETS; 31465310Sarchie priv->hashMask = MIN_BUCKETS - 1; 31565310Sarchie priv->conf.debugLevel = 1; 31665310Sarchie priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT; 31765310Sarchie priv->conf.maxStaleness = DEFAULT_MAX_STALENESS; 31865310Sarchie priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE; 31965310Sarchie 32070700Sjulian /* 32170700Sjulian * This node has all kinds of stuff that could be screwed by SMP. 32270700Sjulian * Until it gets it's own internal protection, we go through in 32370700Sjulian * single file. This could hurt a machine bridging beteen two 32470700Sjulian * GB ethernets so it should be fixed. 32570700Sjulian * When it's fixed the process SHOULD NOT SLEEP, spinlocks please! 32670700Sjulian * (and atomic ops ) 32770700Sjulian */ 32870784Sjulian NG_NODE_FORCE_WRITER(node); 32970784Sjulian NG_NODE_SET_PRIVATE(node, priv); 33070700Sjulian priv->node = node; 33165310Sarchie 33287997Sarchie /* Start timer; timer is always running while node is alive */ 333138834Sglebius ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 33487997Sarchie 33587997Sarchie /* Done */ 33665310Sarchie return (0); 33765310Sarchie} 33865310Sarchie 33965310Sarchie/* 34065310Sarchie * Method for attaching a new hook 34165310Sarchie */ 34265310Sarchiestatic int 34365310Sarchieng_bridge_newhook(node_p node, hook_p hook, const char *name) 34465310Sarchie{ 34570784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 34665310Sarchie 34765310Sarchie /* Check for a link hook */ 34865310Sarchie if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX, 34965310Sarchie strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) { 35065310Sarchie const char *cp; 35165310Sarchie char *eptr; 35265310Sarchie u_long linkNum; 35365310Sarchie 35465310Sarchie cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX); 35565310Sarchie if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 35665310Sarchie return (EINVAL); 35765310Sarchie linkNum = strtoul(cp, &eptr, 10); 35865310Sarchie if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS) 35965310Sarchie return (EINVAL); 36065310Sarchie if (priv->links[linkNum] != NULL) 36165310Sarchie return (EISCONN); 362184214Sdes priv->links[linkNum] = malloc(sizeof(*priv->links[linkNum]), 363184214Sdes M_NETGRAPH_BRIDGE, M_NOWAIT|M_ZERO); 36465310Sarchie if (priv->links[linkNum] == NULL) 36565310Sarchie return (ENOMEM); 36665310Sarchie priv->links[linkNum]->hook = hook; 36770784Sjulian NG_HOOK_SET_PRIVATE(hook, (void *)linkNum); 36865310Sarchie priv->numLinks++; 36965310Sarchie return (0); 37065310Sarchie } 37165310Sarchie 37265310Sarchie /* Unknown hook name */ 37365310Sarchie return (EINVAL); 37465310Sarchie} 37565310Sarchie 37665310Sarchie/* 37765310Sarchie * Receive a control message 37865310Sarchie */ 37965310Sarchiestatic int 38070700Sjulianng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook) 38165310Sarchie{ 38270784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 38365310Sarchie struct ng_mesg *resp = NULL; 38465310Sarchie int error = 0; 38570700Sjulian struct ng_mesg *msg; 38665310Sarchie 38770700Sjulian NGI_GET_MSG(item, msg); 38865310Sarchie switch (msg->header.typecookie) { 38965310Sarchie case NGM_BRIDGE_COOKIE: 39065310Sarchie switch (msg->header.cmd) { 39165310Sarchie case NGM_BRIDGE_GET_CONFIG: 39265310Sarchie { 39365310Sarchie struct ng_bridge_config *conf; 39465310Sarchie 39565310Sarchie NG_MKRESPONSE(resp, msg, 39665310Sarchie sizeof(struct ng_bridge_config), M_NOWAIT); 39765310Sarchie if (resp == NULL) { 39865310Sarchie error = ENOMEM; 39965310Sarchie break; 40065310Sarchie } 40165310Sarchie conf = (struct ng_bridge_config *)resp->data; 40265310Sarchie *conf = priv->conf; /* no sanity checking needed */ 40365310Sarchie break; 40465310Sarchie } 40565310Sarchie case NGM_BRIDGE_SET_CONFIG: 40665310Sarchie { 40765310Sarchie struct ng_bridge_config *conf; 40865310Sarchie int i; 40965310Sarchie 41065310Sarchie if (msg->header.arglen 41165310Sarchie != sizeof(struct ng_bridge_config)) { 41265310Sarchie error = EINVAL; 41365310Sarchie break; 41465310Sarchie } 41565310Sarchie conf = (struct ng_bridge_config *)msg->data; 41665310Sarchie priv->conf = *conf; 41765310Sarchie for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) 41865310Sarchie priv->conf.ipfw[i] = !!priv->conf.ipfw[i]; 41965310Sarchie break; 42065310Sarchie } 42165310Sarchie case NGM_BRIDGE_RESET: 42265310Sarchie { 42365310Sarchie int i; 42465310Sarchie 42565310Sarchie /* Flush all entries in the hash table */ 42665310Sarchie ng_bridge_remove_hosts(priv, -1); 42765310Sarchie 42865310Sarchie /* Reset all loop detection counters and stats */ 42965310Sarchie for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) { 43065310Sarchie if (priv->links[i] == NULL) 43165310Sarchie continue; 43265310Sarchie priv->links[i]->loopCount = 0; 43365310Sarchie bzero(&priv->links[i]->stats, 43465310Sarchie sizeof(priv->links[i]->stats)); 43565310Sarchie } 43665310Sarchie break; 43765310Sarchie } 43865310Sarchie case NGM_BRIDGE_GET_STATS: 43965310Sarchie case NGM_BRIDGE_CLR_STATS: 44065310Sarchie case NGM_BRIDGE_GETCLR_STATS: 44165310Sarchie { 44265310Sarchie struct ng_bridge_link *link; 44365310Sarchie int linkNum; 44465310Sarchie 44565310Sarchie /* Get link number */ 44665310Sarchie if (msg->header.arglen != sizeof(u_int32_t)) { 44765310Sarchie error = EINVAL; 44865310Sarchie break; 44965310Sarchie } 45065310Sarchie linkNum = *((u_int32_t *)msg->data); 45165310Sarchie if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) { 45265310Sarchie error = EINVAL; 45365310Sarchie break; 45465310Sarchie } 45565310Sarchie if ((link = priv->links[linkNum]) == NULL) { 45665310Sarchie error = ENOTCONN; 45765310Sarchie break; 45865310Sarchie } 45965310Sarchie 46065310Sarchie /* Get/clear stats */ 46165310Sarchie if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) { 46265310Sarchie NG_MKRESPONSE(resp, msg, 46365310Sarchie sizeof(link->stats), M_NOWAIT); 46465310Sarchie if (resp == NULL) { 46565310Sarchie error = ENOMEM; 46665310Sarchie break; 46765310Sarchie } 46865310Sarchie bcopy(&link->stats, 46965310Sarchie resp->data, sizeof(link->stats)); 47065310Sarchie } 47165310Sarchie if (msg->header.cmd != NGM_BRIDGE_GET_STATS) 47265310Sarchie bzero(&link->stats, sizeof(link->stats)); 47365310Sarchie break; 47465310Sarchie } 47565310Sarchie case NGM_BRIDGE_GET_TABLE: 47665310Sarchie { 47765310Sarchie struct ng_bridge_host_ary *ary; 47865310Sarchie struct ng_bridge_hent *hent; 47965310Sarchie int i = 0, bucket; 48065310Sarchie 48165310Sarchie NG_MKRESPONSE(resp, msg, sizeof(*ary) 48265310Sarchie + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT); 48365310Sarchie if (resp == NULL) { 48465310Sarchie error = ENOMEM; 48565310Sarchie break; 48665310Sarchie } 48765310Sarchie ary = (struct ng_bridge_host_ary *)resp->data; 48865310Sarchie ary->numHosts = priv->numHosts; 48965310Sarchie for (bucket = 0; bucket < priv->numBuckets; bucket++) { 49065310Sarchie SLIST_FOREACH(hent, &priv->tab[bucket], next) 49165310Sarchie ary->hosts[i++] = hent->host; 49265310Sarchie } 49365310Sarchie break; 49465310Sarchie } 49565310Sarchie default: 49665310Sarchie error = EINVAL; 49765310Sarchie break; 49865310Sarchie } 49965310Sarchie break; 50065310Sarchie default: 50165310Sarchie error = EINVAL; 50265310Sarchie break; 50365310Sarchie } 50465310Sarchie 50565310Sarchie /* Done */ 50670700Sjulian NG_RESPOND_MSG(error, node, item, resp); 50770700Sjulian NG_FREE_MSG(msg); 50865310Sarchie return (error); 50965310Sarchie} 51065310Sarchie 51165310Sarchie/* 51265310Sarchie * Receive data on a hook 51365310Sarchie */ 51465310Sarchiestatic int 51570700Sjulianng_bridge_rcvdata(hook_p hook, item_p item) 51665310Sarchie{ 51770784Sjulian const node_p node = NG_HOOK_NODE(hook); 51870784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 51965310Sarchie struct ng_bridge_host *host; 52065310Sarchie struct ng_bridge_link *link; 52165310Sarchie struct ether_header *eh; 522130931Sgreen int error = 0, linkNum, linksSeen; 52370700Sjulian int manycast; 52470700Sjulian struct mbuf *m; 52570700Sjulian struct ng_bridge_link *firstLink; 52665310Sarchie 52770700Sjulian NGI_GET_M(item, m); 52865310Sarchie /* Get link number */ 529106665Sjhb linkNum = (intptr_t)NG_HOOK_PRIVATE(hook); 53065310Sarchie KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, 53187599Sobrien ("%s: linkNum=%u", __func__, linkNum)); 53265310Sarchie link = priv->links[linkNum]; 53387599Sobrien KASSERT(link != NULL, ("%s: link%d null", __func__, linkNum)); 53465310Sarchie 53565310Sarchie /* Sanity check packet and pull up header */ 53665310Sarchie if (m->m_pkthdr.len < ETHER_HDR_LEN) { 53765310Sarchie link->stats.recvRunts++; 53870700Sjulian NG_FREE_ITEM(item); 53970700Sjulian NG_FREE_M(m); 54065310Sarchie return (EINVAL); 54165310Sarchie } 54265310Sarchie if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) { 54365310Sarchie link->stats.memoryFailures++; 54470700Sjulian NG_FREE_ITEM(item); 54565310Sarchie return (ENOBUFS); 54665310Sarchie } 54765310Sarchie eh = mtod(m, struct ether_header *); 54865310Sarchie if ((eh->ether_shost[0] & 1) != 0) { 54965310Sarchie link->stats.recvInvalid++; 55070700Sjulian NG_FREE_ITEM(item); 55170700Sjulian NG_FREE_M(m); 55265310Sarchie return (EINVAL); 55365310Sarchie } 55465310Sarchie 55565310Sarchie /* Is link disabled due to a loopback condition? */ 55665310Sarchie if (link->loopCount != 0) { 55765310Sarchie link->stats.loopDrops++; 55870700Sjulian NG_FREE_ITEM(item); 55970700Sjulian NG_FREE_M(m); 56065310Sarchie return (ELOOP); /* XXX is this an appropriate error? */ 56165310Sarchie } 56265310Sarchie 56365310Sarchie /* Update stats */ 56465310Sarchie link->stats.recvPackets++; 56565310Sarchie link->stats.recvOctets += m->m_pkthdr.len; 56665310Sarchie if ((manycast = (eh->ether_dhost[0] & 1)) != 0) { 56765310Sarchie if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) { 56865310Sarchie link->stats.recvBroadcasts++; 56965310Sarchie manycast = 2; 57065310Sarchie } else 57165310Sarchie link->stats.recvMulticasts++; 57265310Sarchie } 57365310Sarchie 57465310Sarchie /* Look up packet's source Ethernet address in hashtable */ 57565310Sarchie if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) { 57665310Sarchie 57765310Sarchie /* Update time since last heard from this host */ 57865310Sarchie host->staleness = 0; 57965310Sarchie 58065310Sarchie /* Did host jump to a different link? */ 58165310Sarchie if (host->linkNum != linkNum) { 58265310Sarchie 58365310Sarchie /* 58465310Sarchie * If the host's old link was recently established 58565310Sarchie * on the old link and it's already jumped to a new 58665310Sarchie * link, declare a loopback condition. 58765310Sarchie */ 58865310Sarchie if (host->age < priv->conf.minStableAge) { 58965310Sarchie 59065310Sarchie /* Log the problem */ 59165310Sarchie if (priv->conf.debugLevel >= 2) { 59265310Sarchie struct ifnet *ifp = m->m_pkthdr.rcvif; 59365310Sarchie char suffix[32]; 59465310Sarchie 59565310Sarchie if (ifp != NULL) 59665310Sarchie snprintf(suffix, sizeof(suffix), 597121816Sbrooks " (%s)", ifp->if_xname); 59865310Sarchie else 59965310Sarchie *suffix = '\0'; 60065310Sarchie log(LOG_WARNING, "ng_bridge: %s:" 60165310Sarchie " loopback detected on %s%s\n", 60265310Sarchie ng_bridge_nodename(node), 60370784Sjulian NG_HOOK_NAME(hook), suffix); 60465310Sarchie } 60565310Sarchie 60665310Sarchie /* Mark link as linka non grata */ 60765310Sarchie link->loopCount = priv->conf.loopTimeout; 60865310Sarchie link->stats.loopDetects++; 60965310Sarchie 61065310Sarchie /* Forget all hosts on this link */ 61165310Sarchie ng_bridge_remove_hosts(priv, linkNum); 61265310Sarchie 61365310Sarchie /* Drop packet */ 61465310Sarchie link->stats.loopDrops++; 61570700Sjulian NG_FREE_ITEM(item); 61670700Sjulian NG_FREE_M(m); 61765310Sarchie return (ELOOP); /* XXX appropriate? */ 61865310Sarchie } 61965310Sarchie 62065310Sarchie /* Move host over to new link */ 62165310Sarchie host->linkNum = linkNum; 62265310Sarchie host->age = 0; 62365310Sarchie } 62465310Sarchie } else { 62565310Sarchie if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) { 62665310Sarchie link->stats.memoryFailures++; 62770700Sjulian NG_FREE_ITEM(item); 62870700Sjulian NG_FREE_M(m); 62965310Sarchie return (ENOMEM); 63065310Sarchie } 63165310Sarchie } 63265310Sarchie 63365310Sarchie /* Run packet through ipfw processing, if enabled */ 634133920Sandre#if 0 635181803Sbz if (priv->conf.ipfw[linkNum] && V_fw_enable && ip_fw_chk_ptr != NULL) { 63665310Sarchie /* XXX not implemented yet */ 63765310Sarchie } 638133920Sandre#endif 63965310Sarchie 64065310Sarchie /* 64165310Sarchie * If unicast and destination host known, deliver to host's link, 64265310Sarchie * unless it is the same link as the packet came in on. 64365310Sarchie */ 64465310Sarchie if (!manycast) { 64565310Sarchie 64665310Sarchie /* Determine packet destination link */ 64765310Sarchie if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) { 64865310Sarchie struct ng_bridge_link *const destLink 64965310Sarchie = priv->links[host->linkNum]; 65065310Sarchie 65165310Sarchie /* If destination same as incoming link, do nothing */ 65265310Sarchie KASSERT(destLink != NULL, 65387599Sobrien ("%s: link%d null", __func__, host->linkNum)); 65465310Sarchie if (destLink == link) { 65570700Sjulian NG_FREE_ITEM(item); 65670700Sjulian NG_FREE_M(m); 65765310Sarchie return (0); 65865310Sarchie } 65965310Sarchie 66065310Sarchie /* Deliver packet out the destination link */ 66165310Sarchie destLink->stats.xmitPackets++; 66265310Sarchie destLink->stats.xmitOctets += m->m_pkthdr.len; 66370700Sjulian NG_FWD_NEW_DATA(error, item, destLink->hook, m); 66465310Sarchie return (error); 66565310Sarchie } 66665310Sarchie 66765310Sarchie /* Destination host is not known */ 66865310Sarchie link->stats.recvUnknown++; 66965310Sarchie } 67065310Sarchie 67165310Sarchie /* Distribute unknown, multicast, broadcast pkts to all other links */ 67270700Sjulian firstLink = NULL; 673130931Sgreen for (linkNum = linksSeen = 0; linksSeen <= priv->numLinks; linkNum++) { 67470700Sjulian struct ng_bridge_link *destLink; 67570700Sjulian struct mbuf *m2 = NULL; 67665310Sarchie 67770700Sjulian /* 67870700Sjulian * If we have checked all the links then now 67970700Sjulian * send the original on its reserved link 68070700Sjulian */ 681130931Sgreen if (linksSeen == priv->numLinks) { 68270700Sjulian /* If we never saw a good link, leave. */ 68370700Sjulian if (firstLink == NULL) { 68470700Sjulian NG_FREE_ITEM(item); 68570700Sjulian NG_FREE_M(m); 68670700Sjulian return (0); 68770700Sjulian } 68870700Sjulian destLink = firstLink; 68970700Sjulian } else { 69070700Sjulian destLink = priv->links[linkNum]; 691130931Sgreen if (destLink != NULL) 692130931Sgreen linksSeen++; 69370700Sjulian /* Skip incoming link and disconnected links */ 69470700Sjulian if (destLink == NULL || destLink == link) { 69570700Sjulian continue; 69670700Sjulian } 69770700Sjulian if (firstLink == NULL) { 69870700Sjulian /* 69970700Sjulian * This is the first usable link we have found. 70070700Sjulian * Reserve it for the originals. 70170700Sjulian * If we never find another we save a copy. 70270700Sjulian */ 70370700Sjulian firstLink = destLink; 70470700Sjulian continue; 70570700Sjulian } 70665310Sarchie 70770700Sjulian /* 70870700Sjulian * It's usable link but not the reserved (first) one. 709131155Sjulian * Copy mbuf info for sending. 71070700Sjulian */ 711111119Simp m2 = m_dup(m, M_DONTWAIT); /* XXX m_copypacket() */ 71265310Sarchie if (m2 == NULL) { 71365310Sarchie link->stats.memoryFailures++; 71470700Sjulian NG_FREE_ITEM(item); 71570700Sjulian NG_FREE_M(m); 71665310Sarchie return (ENOBUFS); 71765310Sarchie } 71865310Sarchie } 71965310Sarchie 72065310Sarchie /* Update stats */ 72165310Sarchie destLink->stats.xmitPackets++; 72265310Sarchie destLink->stats.xmitOctets += m->m_pkthdr.len; 72365310Sarchie switch (manycast) { 72465310Sarchie case 0: /* unicast */ 72565310Sarchie break; 72665310Sarchie case 1: /* multicast */ 72765310Sarchie destLink->stats.xmitMulticasts++; 72865310Sarchie break; 72965310Sarchie case 2: /* broadcast */ 73065310Sarchie destLink->stats.xmitBroadcasts++; 73165310Sarchie break; 73265310Sarchie } 73365310Sarchie 73465310Sarchie /* Send packet */ 73570700Sjulian if (destLink == firstLink) { 73670700Sjulian /* 73770700Sjulian * If we've sent all the others, send the original 73870700Sjulian * on the first link we found. 73970700Sjulian */ 74070700Sjulian NG_FWD_NEW_DATA(error, item, destLink->hook, m); 74170700Sjulian break; /* always done last - not really needed. */ 74270700Sjulian } else { 743131155Sjulian NG_SEND_DATA_ONLY(error, destLink->hook, m2); 74470700Sjulian } 74565310Sarchie } 74665310Sarchie return (error); 74765310Sarchie} 74865310Sarchie 74965310Sarchie/* 75065310Sarchie * Shutdown node 75165310Sarchie */ 75265310Sarchiestatic int 75370700Sjulianng_bridge_shutdown(node_p node) 75465310Sarchie{ 75570784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 75665310Sarchie 75787997Sarchie /* 758141574Sru * Shut down everything including the timer. Even if the 759141574Sru * callout has already been dequeued and is about to be 760141574Sru * run, ng_bridge_timeout() won't be fired as the node 761141574Sru * is already marked NGF_INVALID, so we're safe to free 762141574Sru * the node now. 76387997Sarchie */ 76465310Sarchie KASSERT(priv->numLinks == 0 && priv->numHosts == 0, 76565310Sarchie ("%s: numLinks=%d numHosts=%d", 76687599Sobrien __func__, priv->numLinks, priv->numHosts)); 767141574Sru ng_uncallout(&priv->timer, node); 768141574Sru NG_NODE_SET_PRIVATE(node, NULL); 769141574Sru NG_NODE_UNREF(node); 770184205Sdes free(priv->tab, M_NETGRAPH_BRIDGE); 771184205Sdes free(priv, M_NETGRAPH_BRIDGE); 77265310Sarchie return (0); 77365310Sarchie} 77465310Sarchie 77565310Sarchie/* 77665310Sarchie * Hook disconnection. 77765310Sarchie */ 77865310Sarchiestatic int 77965310Sarchieng_bridge_disconnect(hook_p hook) 78065310Sarchie{ 78170784Sjulian const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook)); 78265310Sarchie int linkNum; 78365310Sarchie 78465310Sarchie /* Get link number */ 785106665Sjhb linkNum = (intptr_t)NG_HOOK_PRIVATE(hook); 78665310Sarchie KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, 78787599Sobrien ("%s: linkNum=%u", __func__, linkNum)); 78865310Sarchie 78965310Sarchie /* Remove all hosts associated with this link */ 79065310Sarchie ng_bridge_remove_hosts(priv, linkNum); 79165310Sarchie 79265310Sarchie /* Free associated link information */ 79387599Sobrien KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __func__)); 794184205Sdes free(priv->links[linkNum], M_NETGRAPH_BRIDGE); 79565310Sarchie priv->links[linkNum] = NULL; 79665310Sarchie priv->numLinks--; 79765310Sarchie 79865310Sarchie /* If no more hooks, go away */ 79970784Sjulian if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0) 80070784Sjulian && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) { 80170784Sjulian ng_rmnode_self(NG_HOOK_NODE(hook)); 80270784Sjulian } 80365310Sarchie return (0); 80465310Sarchie} 80565310Sarchie 80665310Sarchie/****************************************************************** 80765310Sarchie HASH TABLE FUNCTIONS 80865310Sarchie******************************************************************/ 80965310Sarchie 81065310Sarchie/* 81165310Sarchie * Hash algorithm 81265310Sarchie */ 81365310Sarchie#define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \ 81465310Sarchie ^ ((const u_int16_t *)(addr))[1] \ 81565310Sarchie ^ ((const u_int16_t *)(addr))[2]) & (mask) ) 81665310Sarchie 81765310Sarchie/* 81865310Sarchie * Find a host entry in the table. 81965310Sarchie */ 82065310Sarchiestatic struct ng_bridge_host * 82165310Sarchieng_bridge_get(priv_p priv, const u_char *addr) 82265310Sarchie{ 82365310Sarchie const int bucket = HASH(addr, priv->hashMask); 82465310Sarchie struct ng_bridge_hent *hent; 82565310Sarchie 82665310Sarchie SLIST_FOREACH(hent, &priv->tab[bucket], next) { 82765310Sarchie if (ETHER_EQUAL(hent->host.addr, addr)) 82865310Sarchie return (&hent->host); 82965310Sarchie } 83065310Sarchie return (NULL); 83165310Sarchie} 83265310Sarchie 83365310Sarchie/* 83465310Sarchie * Add a new host entry to the table. This assumes the host doesn't 83565310Sarchie * already exist in the table. Returns 1 on success, 0 if there 83665310Sarchie * was a memory allocation failure. 83765310Sarchie */ 83865310Sarchiestatic int 83965310Sarchieng_bridge_put(priv_p priv, const u_char *addr, int linkNum) 84065310Sarchie{ 84165310Sarchie const int bucket = HASH(addr, priv->hashMask); 84265310Sarchie struct ng_bridge_hent *hent; 84365310Sarchie 84465310Sarchie#ifdef INVARIANTS 84565310Sarchie /* Assert that entry does not already exist in hashtable */ 84665310Sarchie SLIST_FOREACH(hent, &priv->tab[bucket], next) { 84765310Sarchie KASSERT(!ETHER_EQUAL(hent->host.addr, addr), 84887599Sobrien ("%s: entry %6D exists in table", __func__, addr, ":")); 84965310Sarchie } 85065310Sarchie#endif 85165310Sarchie 85265310Sarchie /* Allocate and initialize new hashtable entry */ 853184214Sdes hent = malloc(sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT); 85465310Sarchie if (hent == NULL) 85565310Sarchie return (0); 85665310Sarchie bcopy(addr, hent->host.addr, ETHER_ADDR_LEN); 85765310Sarchie hent->host.linkNum = linkNum; 85865310Sarchie hent->host.staleness = 0; 85965310Sarchie hent->host.age = 0; 86065310Sarchie 86165310Sarchie /* Add new element to hash bucket */ 86265310Sarchie SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next); 86365310Sarchie priv->numHosts++; 86465310Sarchie 86565310Sarchie /* Resize table if necessary */ 86665310Sarchie ng_bridge_rehash(priv); 86765310Sarchie return (1); 86865310Sarchie} 86965310Sarchie 87065310Sarchie/* 87165310Sarchie * Resize the hash table. We try to maintain the number of buckets 87265310Sarchie * such that the load factor is in the range 0.25 to 1.0. 87365310Sarchie * 87465310Sarchie * If we can't get the new memory then we silently fail. This is OK 87565310Sarchie * because things will still work and we'll try again soon anyway. 87665310Sarchie */ 87765310Sarchiestatic void 87865310Sarchieng_bridge_rehash(priv_p priv) 87965310Sarchie{ 88065310Sarchie struct ng_bridge_bucket *newTab; 88165310Sarchie int oldBucket, newBucket; 88265310Sarchie int newNumBuckets; 88365310Sarchie u_int newMask; 88465310Sarchie 88565310Sarchie /* Is table too full or too empty? */ 88665310Sarchie if (priv->numHosts > priv->numBuckets 88765310Sarchie && (priv->numBuckets << 1) <= MAX_BUCKETS) 88865310Sarchie newNumBuckets = priv->numBuckets << 1; 88965310Sarchie else if (priv->numHosts < (priv->numBuckets >> 2) 89065310Sarchie && (priv->numBuckets >> 2) >= MIN_BUCKETS) 89165310Sarchie newNumBuckets = priv->numBuckets >> 2; 89265310Sarchie else 89365310Sarchie return; 89465310Sarchie newMask = newNumBuckets - 1; 89565310Sarchie 89665310Sarchie /* Allocate and initialize new table */ 897184214Sdes newTab = malloc(newNumBuckets * sizeof(*newTab), 898184214Sdes M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO); 89965310Sarchie if (newTab == NULL) 90065310Sarchie return; 90165310Sarchie 90265310Sarchie /* Move all entries from old table to new table */ 90365310Sarchie for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) { 90465310Sarchie struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket]; 90565310Sarchie 90665310Sarchie while (!SLIST_EMPTY(oldList)) { 90765310Sarchie struct ng_bridge_hent *const hent 90865310Sarchie = SLIST_FIRST(oldList); 90965310Sarchie 91065310Sarchie SLIST_REMOVE_HEAD(oldList, next); 91165310Sarchie newBucket = HASH(hent->host.addr, newMask); 91265310Sarchie SLIST_INSERT_HEAD(&newTab[newBucket], hent, next); 91365310Sarchie } 91465310Sarchie } 91565310Sarchie 91665310Sarchie /* Replace old table with new one */ 91765310Sarchie if (priv->conf.debugLevel >= 3) { 91865310Sarchie log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n", 91965310Sarchie ng_bridge_nodename(priv->node), 92065310Sarchie priv->numBuckets, newNumBuckets); 92165310Sarchie } 922184205Sdes free(priv->tab, M_NETGRAPH_BRIDGE); 92365310Sarchie priv->numBuckets = newNumBuckets; 92465310Sarchie priv->hashMask = newMask; 92565310Sarchie priv->tab = newTab; 92665310Sarchie return; 92765310Sarchie} 92865310Sarchie 92965310Sarchie/****************************************************************** 93065310Sarchie MISC FUNCTIONS 93165310Sarchie******************************************************************/ 93265310Sarchie 93365310Sarchie/* 93465310Sarchie * Remove all hosts associated with a specific link from the hashtable. 93565310Sarchie * If linkNum == -1, then remove all hosts in the table. 93665310Sarchie */ 93765310Sarchiestatic void 93865310Sarchieng_bridge_remove_hosts(priv_p priv, int linkNum) 93965310Sarchie{ 94065310Sarchie int bucket; 94165310Sarchie 94265310Sarchie for (bucket = 0; bucket < priv->numBuckets; bucket++) { 94365310Sarchie struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 94465310Sarchie 94565310Sarchie while (*hptr != NULL) { 94665310Sarchie struct ng_bridge_hent *const hent = *hptr; 94765310Sarchie 94865310Sarchie if (linkNum == -1 || hent->host.linkNum == linkNum) { 94965310Sarchie *hptr = SLIST_NEXT(hent, next); 950184205Sdes free(hent, M_NETGRAPH_BRIDGE); 95165310Sarchie priv->numHosts--; 95265310Sarchie } else 95365310Sarchie hptr = &SLIST_NEXT(hent, next); 95465310Sarchie } 95565310Sarchie } 95665310Sarchie} 95765310Sarchie 95865310Sarchie/* 95965310Sarchie * Handle our once-per-second timeout event. We do two things: 96065310Sarchie * we decrement link->loopCount for those links being muted due to 96165310Sarchie * a detected loopback condition, and we remove any hosts from 96265310Sarchie * the hashtable whom we haven't heard from in a long while. 96365310Sarchie */ 96465310Sarchiestatic void 965138834Sglebiusng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2) 96665310Sarchie{ 96770784Sjulian const priv_p priv = NG_NODE_PRIVATE(node); 968138834Sglebius int bucket; 96965310Sarchie int counter = 0; 97065310Sarchie int linkNum; 97165310Sarchie 97265310Sarchie /* Update host time counters and remove stale entries */ 97365310Sarchie for (bucket = 0; bucket < priv->numBuckets; bucket++) { 97465310Sarchie struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 97565310Sarchie 97665310Sarchie while (*hptr != NULL) { 97765310Sarchie struct ng_bridge_hent *const hent = *hptr; 97865310Sarchie 97965310Sarchie /* Make sure host's link really exists */ 98065310Sarchie KASSERT(priv->links[hent->host.linkNum] != NULL, 98165310Sarchie ("%s: host %6D on nonexistent link %d\n", 98287599Sobrien __func__, hent->host.addr, ":", 98365310Sarchie hent->host.linkNum)); 98465310Sarchie 98565310Sarchie /* Remove hosts we haven't heard from in a while */ 98665310Sarchie if (++hent->host.staleness >= priv->conf.maxStaleness) { 98765310Sarchie *hptr = SLIST_NEXT(hent, next); 988184205Sdes free(hent, M_NETGRAPH_BRIDGE); 98965310Sarchie priv->numHosts--; 99065310Sarchie } else { 99165310Sarchie if (hent->host.age < 0xffff) 99265310Sarchie hent->host.age++; 99365310Sarchie hptr = &SLIST_NEXT(hent, next); 99465310Sarchie counter++; 99565310Sarchie } 99665310Sarchie } 99765310Sarchie } 99865310Sarchie KASSERT(priv->numHosts == counter, 99987599Sobrien ("%s: hosts: %d != %d", __func__, priv->numHosts, counter)); 100065310Sarchie 100165310Sarchie /* Decrease table size if necessary */ 100265310Sarchie ng_bridge_rehash(priv); 100365310Sarchie 100465310Sarchie /* Decrease loop counter on muted looped back links */ 100565310Sarchie for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) { 100665310Sarchie struct ng_bridge_link *const link = priv->links[linkNum]; 100765310Sarchie 100865310Sarchie if (link != NULL) { 100965310Sarchie if (link->loopCount != 0) { 101065310Sarchie link->loopCount--; 101165310Sarchie if (link->loopCount == 0 101265310Sarchie && priv->conf.debugLevel >= 2) { 101365310Sarchie log(LOG_INFO, "ng_bridge: %s:" 101465310Sarchie " restoring looped back link%d\n", 101565310Sarchie ng_bridge_nodename(node), linkNum); 101665310Sarchie } 101765310Sarchie } 101865310Sarchie counter++; 101965310Sarchie } 102065310Sarchie } 102165310Sarchie KASSERT(priv->numLinks == counter, 102287599Sobrien ("%s: links: %d != %d", __func__, priv->numLinks, counter)); 102365310Sarchie 1024138834Sglebius /* Register a new timeout, keeping the existing node reference */ 1025138834Sglebius ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0); 102665310Sarchie} 102765310Sarchie 102865310Sarchie/* 102965310Sarchie * Return node's "name", even if it doesn't have one. 103065310Sarchie */ 103165310Sarchiestatic const char * 103265310Sarchieng_bridge_nodename(node_p node) 103365310Sarchie{ 1034125028Sharti static char name[NG_NODESIZ]; 103565310Sarchie 103670784Sjulian if (NG_NODE_NAME(node) != NULL) 103770784Sjulian snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node)); 103865310Sarchie else 103965310Sarchie snprintf(name, sizeof(name), "[%x]", ng_node2ID(node)); 104065310Sarchie return name; 104165310Sarchie} 104265310Sarchie 1043