ng_bridge.c revision 69225
165310Sarchie 265310Sarchie/* 365310Sarchie * ng_bridge.c 465310Sarchie * 565310Sarchie * Copyright (c) 2000 Whistle Communications, Inc. 665310Sarchie * All rights reserved. 765310Sarchie * 865310Sarchie * Subject to the following obligations and disclaimer of warranty, use and 965310Sarchie * redistribution of this software, in source or object code forms, with or 1065310Sarchie * without modifications are expressly permitted by Whistle Communications; 1165310Sarchie * provided, however, that: 1265310Sarchie * 1. Any and all reproductions of the source or object code must include the 1365310Sarchie * copyright notice above and the following disclaimer of warranties; and 1465310Sarchie * 2. No rights are granted, in any manner or form, to use Whistle 1565310Sarchie * Communications, Inc. trademarks, including the mark "WHISTLE 1665310Sarchie * COMMUNICATIONS" on advertising, endorsements, or otherwise except as 1765310Sarchie * such appears in the above copyright notice or in the software. 1865310Sarchie * 1965310Sarchie * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND 2065310Sarchie * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO 2165310Sarchie * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, 2265310Sarchie * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF 2365310Sarchie * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. 2465310Sarchie * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY 2565310Sarchie * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS 2665310Sarchie * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE. 2765310Sarchie * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES 2865310Sarchie * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING 2965310Sarchie * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 3065310Sarchie * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR 3165310Sarchie * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY 3265310Sarchie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3365310Sarchie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3465310Sarchie * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY 3565310Sarchie * OF SUCH DAMAGE. 3665310Sarchie * 3765310Sarchie * Author: Archie Cobbs <archie@freebsd.org> 3865310Sarchie * 3965310Sarchie * $FreeBSD: head/sys/netgraph/ng_bridge.c 69225 2000-11-26 21:59:30Z jlemon $ 4065310Sarchie */ 4165310Sarchie 4265310Sarchie/* 4365310Sarchie * ng_bridge(4) netgraph node type 4465310Sarchie * 4565310Sarchie * The node performs standard intelligent Ethernet bridging over 4665310Sarchie * each of its connected hooks, or links. A simple loop detection 4765310Sarchie * algorithm is included which disables a link for priv->conf.loopTimeout 4865310Sarchie * seconds when a host is seen to have jumped from one link to 4965310Sarchie * another within priv->conf.minStableAge seconds. 5065310Sarchie * 5165310Sarchie * We keep a hashtable that maps Ethernet addresses to host info, 5265310Sarchie * which is contained in struct ng_bridge_host's. These structures 5365310Sarchie * tell us on which link the host may be found. A host's entry will 5465310Sarchie * expire after priv->conf.maxStaleness seconds. 5565310Sarchie * 5665310Sarchie * This node is optimzed for stable networks, where machines jump 5765310Sarchie * from one port to the other only rarely. 5865310Sarchie */ 5965310Sarchie 6065310Sarchie#include <sys/param.h> 6165310Sarchie#include <sys/systm.h> 6265310Sarchie#include <sys/kernel.h> 6365310Sarchie#include <sys/malloc.h> 6465310Sarchie#include <sys/mbuf.h> 6565310Sarchie#include <sys/errno.h> 6665310Sarchie#include <sys/syslog.h> 6765310Sarchie#include <sys/socket.h> 6865310Sarchie#include <sys/ctype.h> 6965310Sarchie 7065310Sarchie#include <net/if.h> 7165310Sarchie#include <net/ethernet.h> 7265310Sarchie 7365310Sarchie#include <netinet/in.h> 7465310Sarchie#include <netinet/ip_fw.h> 7565310Sarchie 7665310Sarchie#include <netgraph/ng_message.h> 7765310Sarchie#include <netgraph/netgraph.h> 7865310Sarchie#include <netgraph/ng_parse.h> 7965310Sarchie#include <netgraph/ng_bridge.h> 8065310Sarchie#include <netgraph/ng_ether.h> 8165310Sarchie 8265310Sarchie/* Per-link private data */ 8365310Sarchiestruct ng_bridge_link { 8465310Sarchie hook_p hook; /* netgraph hook */ 8565310Sarchie u_int16_t loopCount; /* loop ignore timer */ 8665310Sarchie struct ng_bridge_link_stats stats; /* link stats */ 8765310Sarchie}; 8865310Sarchie 8965310Sarchie/* Per-node private data */ 9065310Sarchiestruct ng_bridge_private { 9165310Sarchie struct ng_bridge_bucket *tab; /* hash table bucket array */ 9265310Sarchie struct ng_bridge_link *links[NG_BRIDGE_MAX_LINKS]; 9365310Sarchie struct ng_bridge_config conf; /* node configuration */ 9465310Sarchie node_p node; /* netgraph node */ 9565310Sarchie u_int numHosts; /* num entries in table */ 9665310Sarchie u_int numBuckets; /* num buckets in table */ 9765310Sarchie u_int hashMask; /* numBuckets - 1 */ 9865310Sarchie int numLinks; /* num connected links */ 9965310Sarchie struct callout timer; /* one second periodic timer */ 10065310Sarchie}; 10165310Sarchietypedef struct ng_bridge_private *priv_p; 10265310Sarchie 10365310Sarchie/* Information about a host, stored in a hash table entry */ 10465310Sarchiestruct ng_bridge_hent { 10565310Sarchie struct ng_bridge_host host; /* actual host info */ 10665310Sarchie SLIST_ENTRY(ng_bridge_hent) next; /* next entry in bucket */ 10765310Sarchie}; 10865310Sarchie 10965310Sarchie/* Hash table bucket declaration */ 11065310SarchieSLIST_HEAD(ng_bridge_bucket, ng_bridge_hent); 11165310Sarchie 11265310Sarchie/* Netgraph node methods */ 11365310Sarchiestatic ng_constructor_t ng_bridge_constructor; 11465310Sarchiestatic ng_rcvmsg_t ng_bridge_rcvmsg; 11565310Sarchiestatic ng_shutdown_t ng_bridge_rmnode; 11665310Sarchiestatic ng_newhook_t ng_bridge_newhook; 11765310Sarchiestatic ng_rcvdata_t ng_bridge_rcvdata; 11865310Sarchiestatic ng_disconnect_t ng_bridge_disconnect; 11965310Sarchie 12065310Sarchie/* Other internal functions */ 12165310Sarchiestatic struct ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr); 12265310Sarchiestatic int ng_bridge_put(priv_p priv, const u_char *addr, int linkNum); 12365310Sarchiestatic void ng_bridge_rehash(priv_p priv); 12465310Sarchiestatic void ng_bridge_remove_hosts(priv_p priv, int linkNum); 12565310Sarchiestatic void ng_bridge_timeout(void *arg); 12665310Sarchiestatic const char *ng_bridge_nodename(node_p node); 12765310Sarchie 12865310Sarchie/* Ethernet broadcast */ 12965310Sarchiestatic const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] = 13065310Sarchie { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 13165310Sarchie 13265310Sarchie/* Store each hook's link number in the private field */ 13365310Sarchie#define LINK_NUM(hook) (*(u_int16_t *)(&(hook)->private)) 13465310Sarchie 13565310Sarchie/* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */ 13665310Sarchie#define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \ 13765310Sarchie == ((const u_int32_t *)(b))[0] \ 13865310Sarchie && ((const u_int16_t *)(a))[2] \ 13965310Sarchie == ((const u_int16_t *)(b))[2]) 14065310Sarchie 14165310Sarchie/* Minimum and maximum number of hash buckets. Must be a power of two. */ 14265310Sarchie#define MIN_BUCKETS (1 << 5) /* 32 */ 14365310Sarchie#define MAX_BUCKETS (1 << 14) /* 16384 */ 14465310Sarchie 14565310Sarchie/* Configuration default values */ 14665310Sarchie#define DEFAULT_LOOP_TIMEOUT 60 14765310Sarchie#define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */ 14865310Sarchie#define DEFAULT_MIN_STABLE_AGE 1 14965310Sarchie 15065310Sarchie/****************************************************************** 15165310Sarchie NETGRAPH PARSE TYPES 15265310Sarchie******************************************************************/ 15365310Sarchie 15465310Sarchie/* 15565310Sarchie * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE 15665310Sarchie */ 15765310Sarchiestatic int 15865310Sarchieng_bridge_getTableLength(const struct ng_parse_type *type, 15965310Sarchie const u_char *start, const u_char *buf) 16065310Sarchie{ 16165310Sarchie const struct ng_bridge_host_ary *const hary 16265310Sarchie = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t)); 16365310Sarchie 16465310Sarchie return hary->numHosts; 16565310Sarchie} 16665310Sarchie 16765310Sarchie/* Parse type for struct ng_bridge_host_ary */ 16865310Sarchiestatic const struct ng_parse_struct_info ng_bridge_host_type_info 16965310Sarchie = NG_BRIDGE_HOST_TYPE_INFO(&ng_ether_enaddr_type); 17065310Sarchiestatic const struct ng_parse_type ng_bridge_host_type = { 17165310Sarchie &ng_parse_struct_type, 17265310Sarchie &ng_bridge_host_type_info 17365310Sarchie}; 17465310Sarchiestatic const struct ng_parse_array_info ng_bridge_hary_type_info = { 17565310Sarchie &ng_bridge_host_type, 17665310Sarchie ng_bridge_getTableLength 17765310Sarchie}; 17865310Sarchiestatic const struct ng_parse_type ng_bridge_hary_type = { 17965310Sarchie &ng_parse_array_type, 18065310Sarchie &ng_bridge_hary_type_info 18165310Sarchie}; 18265310Sarchiestatic const struct ng_parse_struct_info ng_bridge_host_ary_type_info 18365310Sarchie = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type); 18465310Sarchiestatic const struct ng_parse_type ng_bridge_host_ary_type = { 18565310Sarchie &ng_parse_struct_type, 18665310Sarchie &ng_bridge_host_ary_type_info 18765310Sarchie}; 18865310Sarchie 18965310Sarchie/* Parse type for struct ng_bridge_config */ 19065310Sarchiestatic const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = { 19165310Sarchie &ng_parse_uint8_type, 19265310Sarchie NG_BRIDGE_MAX_LINKS 19365310Sarchie}; 19465310Sarchiestatic const struct ng_parse_type ng_bridge_ipfwary_type = { 19565310Sarchie &ng_parse_fixedarray_type, 19665310Sarchie &ng_bridge_ipfwary_type_info 19765310Sarchie}; 19865310Sarchiestatic const struct ng_parse_struct_info ng_bridge_config_type_info 19965310Sarchie = NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type); 20065310Sarchiestatic const struct ng_parse_type ng_bridge_config_type = { 20165310Sarchie &ng_parse_struct_type, 20265310Sarchie &ng_bridge_config_type_info 20365310Sarchie}; 20465310Sarchie 20565310Sarchie/* Parse type for struct ng_bridge_link_stat */ 20665310Sarchiestatic const struct ng_parse_struct_info 20765310Sarchie ng_bridge_stats_type_info = NG_BRIDGE_STATS_TYPE_INFO; 20865310Sarchiestatic const struct ng_parse_type ng_bridge_stats_type = { 20965310Sarchie &ng_parse_struct_type, 21065310Sarchie &ng_bridge_stats_type_info 21165310Sarchie}; 21265310Sarchie 21365310Sarchie/* List of commands and how to convert arguments to/from ASCII */ 21465310Sarchiestatic const struct ng_cmdlist ng_bridge_cmdlist[] = { 21565310Sarchie { 21665310Sarchie NGM_BRIDGE_COOKIE, 21765310Sarchie NGM_BRIDGE_SET_CONFIG, 21865310Sarchie "setconfig", 21965310Sarchie &ng_bridge_config_type, 22065310Sarchie NULL 22165310Sarchie }, 22265310Sarchie { 22365310Sarchie NGM_BRIDGE_COOKIE, 22465310Sarchie NGM_BRIDGE_GET_CONFIG, 22565310Sarchie "getconfig", 22665310Sarchie NULL, 22765310Sarchie &ng_bridge_config_type 22865310Sarchie }, 22965310Sarchie { 23065310Sarchie NGM_BRIDGE_COOKIE, 23165310Sarchie NGM_BRIDGE_RESET, 23265310Sarchie "reset", 23365310Sarchie NULL, 23465310Sarchie NULL 23565310Sarchie }, 23665310Sarchie { 23765310Sarchie NGM_BRIDGE_COOKIE, 23865310Sarchie NGM_BRIDGE_GET_STATS, 23965310Sarchie "getstats", 24065310Sarchie &ng_parse_uint32_type, 24165310Sarchie &ng_bridge_stats_type 24265310Sarchie }, 24365310Sarchie { 24465310Sarchie NGM_BRIDGE_COOKIE, 24565310Sarchie NGM_BRIDGE_CLR_STATS, 24665310Sarchie "clrstats", 24765310Sarchie &ng_parse_uint32_type, 24865310Sarchie NULL 24965310Sarchie }, 25065310Sarchie { 25165310Sarchie NGM_BRIDGE_COOKIE, 25265310Sarchie NGM_BRIDGE_GETCLR_STATS, 25365310Sarchie "getclrstats", 25465310Sarchie &ng_parse_uint32_type, 25565310Sarchie &ng_bridge_stats_type 25665310Sarchie }, 25765310Sarchie { 25865310Sarchie NGM_BRIDGE_COOKIE, 25965310Sarchie NGM_BRIDGE_GET_TABLE, 26065310Sarchie "gettable", 26165310Sarchie NULL, 26265310Sarchie &ng_bridge_host_ary_type 26365310Sarchie }, 26465310Sarchie { 0 } 26565310Sarchie}; 26665310Sarchie 26765310Sarchie/* Node type descriptor */ 26865310Sarchiestatic struct ng_type ng_bridge_typestruct = { 26965310Sarchie NG_VERSION, 27065310Sarchie NG_BRIDGE_NODE_TYPE, 27165310Sarchie NULL, 27265310Sarchie ng_bridge_constructor, 27365310Sarchie ng_bridge_rcvmsg, 27465310Sarchie ng_bridge_rmnode, 27565310Sarchie ng_bridge_newhook, 27665310Sarchie NULL, 27765310Sarchie NULL, 27865310Sarchie ng_bridge_rcvdata, 27965310Sarchie ng_bridge_rcvdata, 28065310Sarchie ng_bridge_disconnect, 28165310Sarchie ng_bridge_cmdlist, 28265310Sarchie}; 28366887SarchieNETGRAPH_INIT(bridge, &ng_bridge_typestruct); 28465310Sarchie 28565310Sarchie/* Depend on ng_ether so we can use the Ethernet parse type */ 28665310SarchieMODULE_DEPEND(ng_bridge, ng_ether, 1, 1, 1); 28765310Sarchie 28865310Sarchie/****************************************************************** 28965310Sarchie NETGRAPH NODE METHODS 29065310Sarchie******************************************************************/ 29165310Sarchie 29265310Sarchie/* 29365310Sarchie * Node constructor 29465310Sarchie */ 29565310Sarchiestatic int 29665310Sarchieng_bridge_constructor(node_p *nodep) 29765310Sarchie{ 29865310Sarchie priv_p priv; 29965310Sarchie int error; 30065310Sarchie 30165310Sarchie /* Allocate and initialize private info */ 30268876Sdwmalone MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO); 30365310Sarchie if (priv == NULL) 30465310Sarchie return (ENOMEM); 30569225Sjlemon callout_init(&priv->timer, 0); 30665310Sarchie 30765310Sarchie /* Allocate and initialize hash table, etc. */ 30865310Sarchie MALLOC(priv->tab, struct ng_bridge_bucket *, 30968876Sdwmalone MIN_BUCKETS * sizeof(*priv->tab), M_NETGRAPH, M_NOWAIT | M_ZERO); 31065310Sarchie if (priv->tab == NULL) { 31165310Sarchie FREE(priv, M_NETGRAPH); 31265310Sarchie return (ENOMEM); 31365310Sarchie } 31465310Sarchie priv->numBuckets = MIN_BUCKETS; 31565310Sarchie priv->hashMask = MIN_BUCKETS - 1; 31665310Sarchie priv->conf.debugLevel = 1; 31765310Sarchie priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT; 31865310Sarchie priv->conf.maxStaleness = DEFAULT_MAX_STALENESS; 31965310Sarchie priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE; 32065310Sarchie 32165310Sarchie /* Call superclass constructor */ 32265310Sarchie if ((error = ng_make_node_common(&ng_bridge_typestruct, nodep))) { 32365310Sarchie FREE(priv, M_NETGRAPH); 32465310Sarchie return (error); 32565310Sarchie } 32665310Sarchie (*nodep)->private = priv; 32765310Sarchie priv->node = *nodep; 32865310Sarchie 32965310Sarchie /* Start timer by faking a timeout event */ 33065310Sarchie (*nodep)->refs++; 33165310Sarchie ng_bridge_timeout(*nodep); 33265310Sarchie return (0); 33365310Sarchie} 33465310Sarchie 33565310Sarchie/* 33665310Sarchie * Method for attaching a new hook 33765310Sarchie */ 33865310Sarchiestatic int 33965310Sarchieng_bridge_newhook(node_p node, hook_p hook, const char *name) 34065310Sarchie{ 34165310Sarchie const priv_p priv = node->private; 34265310Sarchie 34365310Sarchie /* Check for a link hook */ 34465310Sarchie if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX, 34565310Sarchie strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) { 34665310Sarchie const char *cp; 34765310Sarchie char *eptr; 34865310Sarchie u_long linkNum; 34965310Sarchie 35065310Sarchie cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX); 35165310Sarchie if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0')) 35265310Sarchie return (EINVAL); 35365310Sarchie linkNum = strtoul(cp, &eptr, 10); 35465310Sarchie if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS) 35565310Sarchie return (EINVAL); 35665310Sarchie if (priv->links[linkNum] != NULL) 35765310Sarchie return (EISCONN); 35865310Sarchie MALLOC(priv->links[linkNum], struct ng_bridge_link *, 35968876Sdwmalone sizeof(*priv->links[linkNum]), M_NETGRAPH, M_NOWAIT|M_ZERO); 36065310Sarchie if (priv->links[linkNum] == NULL) 36165310Sarchie return (ENOMEM); 36265310Sarchie priv->links[linkNum]->hook = hook; 36365310Sarchie LINK_NUM(hook) = linkNum; 36465310Sarchie priv->numLinks++; 36565310Sarchie return (0); 36665310Sarchie } 36765310Sarchie 36865310Sarchie /* Unknown hook name */ 36965310Sarchie return (EINVAL); 37065310Sarchie} 37165310Sarchie 37265310Sarchie/* 37365310Sarchie * Receive a control message 37465310Sarchie */ 37565310Sarchiestatic int 37665310Sarchieng_bridge_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr, 37765310Sarchie struct ng_mesg **rptr, hook_p lasthook) 37865310Sarchie{ 37965310Sarchie const priv_p priv = node->private; 38065310Sarchie struct ng_mesg *resp = NULL; 38165310Sarchie int error = 0; 38265310Sarchie 38365310Sarchie switch (msg->header.typecookie) { 38465310Sarchie case NGM_BRIDGE_COOKIE: 38565310Sarchie switch (msg->header.cmd) { 38665310Sarchie case NGM_BRIDGE_GET_CONFIG: 38765310Sarchie { 38865310Sarchie struct ng_bridge_config *conf; 38965310Sarchie 39065310Sarchie NG_MKRESPONSE(resp, msg, 39165310Sarchie sizeof(struct ng_bridge_config), M_NOWAIT); 39265310Sarchie if (resp == NULL) { 39365310Sarchie error = ENOMEM; 39465310Sarchie break; 39565310Sarchie } 39665310Sarchie conf = (struct ng_bridge_config *)resp->data; 39765310Sarchie *conf = priv->conf; /* no sanity checking needed */ 39865310Sarchie break; 39965310Sarchie } 40065310Sarchie case NGM_BRIDGE_SET_CONFIG: 40165310Sarchie { 40265310Sarchie struct ng_bridge_config *conf; 40365310Sarchie int i; 40465310Sarchie 40565310Sarchie if (msg->header.arglen 40665310Sarchie != sizeof(struct ng_bridge_config)) { 40765310Sarchie error = EINVAL; 40865310Sarchie break; 40965310Sarchie } 41065310Sarchie conf = (struct ng_bridge_config *)msg->data; 41165310Sarchie priv->conf = *conf; 41265310Sarchie for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) 41365310Sarchie priv->conf.ipfw[i] = !!priv->conf.ipfw[i]; 41465310Sarchie break; 41565310Sarchie } 41665310Sarchie case NGM_BRIDGE_RESET: 41765310Sarchie { 41865310Sarchie int i; 41965310Sarchie 42065310Sarchie /* Flush all entries in the hash table */ 42165310Sarchie ng_bridge_remove_hosts(priv, -1); 42265310Sarchie 42365310Sarchie /* Reset all loop detection counters and stats */ 42465310Sarchie for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) { 42565310Sarchie if (priv->links[i] == NULL) 42665310Sarchie continue; 42765310Sarchie priv->links[i]->loopCount = 0; 42865310Sarchie bzero(&priv->links[i]->stats, 42965310Sarchie sizeof(priv->links[i]->stats)); 43065310Sarchie } 43165310Sarchie break; 43265310Sarchie } 43365310Sarchie case NGM_BRIDGE_GET_STATS: 43465310Sarchie case NGM_BRIDGE_CLR_STATS: 43565310Sarchie case NGM_BRIDGE_GETCLR_STATS: 43665310Sarchie { 43765310Sarchie struct ng_bridge_link *link; 43865310Sarchie int linkNum; 43965310Sarchie 44065310Sarchie /* Get link number */ 44165310Sarchie if (msg->header.arglen != sizeof(u_int32_t)) { 44265310Sarchie error = EINVAL; 44365310Sarchie break; 44465310Sarchie } 44565310Sarchie linkNum = *((u_int32_t *)msg->data); 44665310Sarchie if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) { 44765310Sarchie error = EINVAL; 44865310Sarchie break; 44965310Sarchie } 45065310Sarchie if ((link = priv->links[linkNum]) == NULL) { 45165310Sarchie error = ENOTCONN; 45265310Sarchie break; 45365310Sarchie } 45465310Sarchie 45565310Sarchie /* Get/clear stats */ 45665310Sarchie if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) { 45765310Sarchie NG_MKRESPONSE(resp, msg, 45865310Sarchie sizeof(link->stats), M_NOWAIT); 45965310Sarchie if (resp == NULL) { 46065310Sarchie error = ENOMEM; 46165310Sarchie break; 46265310Sarchie } 46365310Sarchie bcopy(&link->stats, 46465310Sarchie resp->data, sizeof(link->stats)); 46565310Sarchie } 46665310Sarchie if (msg->header.cmd != NGM_BRIDGE_GET_STATS) 46765310Sarchie bzero(&link->stats, sizeof(link->stats)); 46865310Sarchie break; 46965310Sarchie } 47065310Sarchie case NGM_BRIDGE_GET_TABLE: 47165310Sarchie { 47265310Sarchie struct ng_bridge_host_ary *ary; 47365310Sarchie struct ng_bridge_hent *hent; 47465310Sarchie int i = 0, bucket; 47565310Sarchie 47665310Sarchie NG_MKRESPONSE(resp, msg, sizeof(*ary) 47765310Sarchie + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT); 47865310Sarchie if (resp == NULL) { 47965310Sarchie error = ENOMEM; 48065310Sarchie break; 48165310Sarchie } 48265310Sarchie ary = (struct ng_bridge_host_ary *)resp->data; 48365310Sarchie ary->numHosts = priv->numHosts; 48465310Sarchie for (bucket = 0; bucket < priv->numBuckets; bucket++) { 48565310Sarchie SLIST_FOREACH(hent, &priv->tab[bucket], next) 48665310Sarchie ary->hosts[i++] = hent->host; 48765310Sarchie } 48865310Sarchie break; 48965310Sarchie } 49065310Sarchie default: 49165310Sarchie error = EINVAL; 49265310Sarchie break; 49365310Sarchie } 49465310Sarchie break; 49565310Sarchie default: 49665310Sarchie error = EINVAL; 49765310Sarchie break; 49865310Sarchie } 49965310Sarchie 50065310Sarchie /* Done */ 50165310Sarchie if (rptr) 50265310Sarchie *rptr = resp; 50365310Sarchie else if (resp != NULL) 50465310Sarchie FREE(resp, M_NETGRAPH); 50565310Sarchie FREE(msg, M_NETGRAPH); 50665310Sarchie return (error); 50765310Sarchie} 50865310Sarchie 50965310Sarchie/* 51065310Sarchie * Receive data on a hook 51165310Sarchie */ 51265310Sarchiestatic int 51365310Sarchieng_bridge_rcvdata(hook_p hook, struct mbuf *m, meta_p meta, 51465310Sarchie struct mbuf **ret_m, meta_p *ret_meta) 51565310Sarchie{ 51665310Sarchie const node_p node = hook->node; 51765310Sarchie const priv_p priv = node->private; 51865310Sarchie struct ng_bridge_host *host; 51965310Sarchie struct ng_bridge_link *link; 52065310Sarchie struct ether_header *eh; 52165310Sarchie int error = 0, linkNum; 52265310Sarchie int i, manycast; 52365310Sarchie 52465310Sarchie /* Get link number */ 52565310Sarchie linkNum = LINK_NUM(hook); 52665310Sarchie KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, 52765310Sarchie ("%s: linkNum=%u", __FUNCTION__, linkNum)); 52865310Sarchie link = priv->links[linkNum]; 52965310Sarchie KASSERT(link != NULL, ("%s: link%d null", __FUNCTION__, linkNum)); 53065310Sarchie 53165310Sarchie /* Sanity check packet and pull up header */ 53265310Sarchie if (m->m_pkthdr.len < ETHER_HDR_LEN) { 53365310Sarchie link->stats.recvRunts++; 53465310Sarchie NG_FREE_DATA(m, meta); 53565310Sarchie return (EINVAL); 53665310Sarchie } 53765310Sarchie if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) { 53865310Sarchie link->stats.memoryFailures++; 53965310Sarchie NG_FREE_META(meta); 54065310Sarchie return (ENOBUFS); 54165310Sarchie } 54265310Sarchie eh = mtod(m, struct ether_header *); 54365310Sarchie if ((eh->ether_shost[0] & 1) != 0) { 54465310Sarchie link->stats.recvInvalid++; 54565310Sarchie NG_FREE_DATA(m, meta); 54665310Sarchie return (EINVAL); 54765310Sarchie } 54865310Sarchie 54965310Sarchie /* Is link disabled due to a loopback condition? */ 55065310Sarchie if (link->loopCount != 0) { 55165310Sarchie link->stats.loopDrops++; 55265310Sarchie NG_FREE_DATA(m, meta); 55365310Sarchie return (ELOOP); /* XXX is this an appropriate error? */ 55465310Sarchie } 55565310Sarchie 55665310Sarchie /* Update stats */ 55765310Sarchie link->stats.recvPackets++; 55865310Sarchie link->stats.recvOctets += m->m_pkthdr.len; 55965310Sarchie if ((manycast = (eh->ether_dhost[0] & 1)) != 0) { 56065310Sarchie if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) { 56165310Sarchie link->stats.recvBroadcasts++; 56265310Sarchie manycast = 2; 56365310Sarchie } else 56465310Sarchie link->stats.recvMulticasts++; 56565310Sarchie } 56665310Sarchie 56765310Sarchie /* Look up packet's source Ethernet address in hashtable */ 56865310Sarchie if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) { 56965310Sarchie 57065310Sarchie /* Update time since last heard from this host */ 57165310Sarchie host->staleness = 0; 57265310Sarchie 57365310Sarchie /* Did host jump to a different link? */ 57465310Sarchie if (host->linkNum != linkNum) { 57565310Sarchie 57665310Sarchie /* 57765310Sarchie * If the host's old link was recently established 57865310Sarchie * on the old link and it's already jumped to a new 57965310Sarchie * link, declare a loopback condition. 58065310Sarchie */ 58165310Sarchie if (host->age < priv->conf.minStableAge) { 58265310Sarchie 58365310Sarchie /* Log the problem */ 58465310Sarchie if (priv->conf.debugLevel >= 2) { 58565310Sarchie struct ifnet *ifp = m->m_pkthdr.rcvif; 58665310Sarchie char suffix[32]; 58765310Sarchie 58865310Sarchie if (ifp != NULL) 58965310Sarchie snprintf(suffix, sizeof(suffix), 59065310Sarchie " (%s%d)", ifp->if_name, 59165310Sarchie ifp->if_unit); 59265310Sarchie else 59365310Sarchie *suffix = '\0'; 59465310Sarchie log(LOG_WARNING, "ng_bridge: %s:" 59565310Sarchie " loopback detected on %s%s\n", 59665310Sarchie ng_bridge_nodename(node), 59765310Sarchie hook->name, suffix); 59865310Sarchie } 59965310Sarchie 60065310Sarchie /* Mark link as linka non grata */ 60165310Sarchie link->loopCount = priv->conf.loopTimeout; 60265310Sarchie link->stats.loopDetects++; 60365310Sarchie 60465310Sarchie /* Forget all hosts on this link */ 60565310Sarchie ng_bridge_remove_hosts(priv, linkNum); 60665310Sarchie 60765310Sarchie /* Drop packet */ 60865310Sarchie link->stats.loopDrops++; 60965310Sarchie NG_FREE_DATA(m, meta); 61065310Sarchie return (ELOOP); /* XXX appropriate? */ 61165310Sarchie } 61265310Sarchie 61365310Sarchie /* Move host over to new link */ 61465310Sarchie host->linkNum = linkNum; 61565310Sarchie host->age = 0; 61665310Sarchie } 61765310Sarchie } else { 61865310Sarchie if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) { 61965310Sarchie link->stats.memoryFailures++; 62065310Sarchie NG_FREE_DATA(m, meta); 62165310Sarchie return (ENOMEM); 62265310Sarchie } 62365310Sarchie } 62465310Sarchie 62565310Sarchie /* Run packet through ipfw processing, if enabled */ 62665310Sarchie if (priv->conf.ipfw[linkNum] && fw_enable && ip_fw_chk_ptr != NULL) { 62765310Sarchie /* XXX not implemented yet */ 62865310Sarchie } 62965310Sarchie 63065310Sarchie /* 63165310Sarchie * If unicast and destination host known, deliver to host's link, 63265310Sarchie * unless it is the same link as the packet came in on. 63365310Sarchie */ 63465310Sarchie if (!manycast) { 63565310Sarchie 63665310Sarchie /* Determine packet destination link */ 63765310Sarchie if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) { 63865310Sarchie struct ng_bridge_link *const destLink 63965310Sarchie = priv->links[host->linkNum]; 64065310Sarchie 64165310Sarchie /* If destination same as incoming link, do nothing */ 64265310Sarchie KASSERT(destLink != NULL, 64365310Sarchie ("%s: link%d null", __FUNCTION__, host->linkNum)); 64465310Sarchie if (destLink == link) { 64565310Sarchie NG_FREE_DATA(m, meta); 64665310Sarchie return (0); 64765310Sarchie } 64865310Sarchie 64965310Sarchie /* Deliver packet out the destination link */ 65065310Sarchie destLink->stats.xmitPackets++; 65165310Sarchie destLink->stats.xmitOctets += m->m_pkthdr.len; 65265310Sarchie NG_SEND_DATA(error, destLink->hook, m, meta); 65365310Sarchie return (error); 65465310Sarchie } 65565310Sarchie 65665310Sarchie /* Destination host is not known */ 65765310Sarchie link->stats.recvUnknown++; 65865310Sarchie } 65965310Sarchie 66065310Sarchie /* Distribute unknown, multicast, broadcast pkts to all other links */ 66165310Sarchie for (linkNum = i = 0; i < priv->numLinks - 1; linkNum++) { 66265310Sarchie struct ng_bridge_link *const destLink = priv->links[linkNum]; 66365310Sarchie meta_p meta2 = NULL; 66465310Sarchie struct mbuf *m2; 66565310Sarchie 66665310Sarchie /* Skip incoming link and disconnected links */ 66765310Sarchie if (destLink == NULL || destLink == link) 66865310Sarchie continue; 66965310Sarchie 67065310Sarchie /* Copy mbuf and meta info */ 67165310Sarchie if (++i == priv->numLinks - 1) { /* last link */ 67265310Sarchie m2 = m; 67365310Sarchie meta2 = meta; 67465310Sarchie } else { 67566313Sarchie m2 = m_dup(m, M_NOWAIT); /* XXX m_copypacket() */ 67665310Sarchie if (m2 == NULL) { 67765310Sarchie link->stats.memoryFailures++; 67865310Sarchie NG_FREE_DATA(m, meta); 67965310Sarchie return (ENOBUFS); 68065310Sarchie } 68165310Sarchie if (meta != NULL 68265310Sarchie && (meta2 = ng_copy_meta(meta)) == NULL) { 68365310Sarchie link->stats.memoryFailures++; 68465310Sarchie m_freem(m2); 68565310Sarchie NG_FREE_DATA(m, meta); 68665310Sarchie return (ENOMEM); 68765310Sarchie } 68865310Sarchie } 68965310Sarchie 69065310Sarchie /* Update stats */ 69165310Sarchie destLink->stats.xmitPackets++; 69265310Sarchie destLink->stats.xmitOctets += m->m_pkthdr.len; 69365310Sarchie switch (manycast) { 69465310Sarchie case 0: /* unicast */ 69565310Sarchie break; 69665310Sarchie case 1: /* multicast */ 69765310Sarchie destLink->stats.xmitMulticasts++; 69865310Sarchie break; 69965310Sarchie case 2: /* broadcast */ 70065310Sarchie destLink->stats.xmitBroadcasts++; 70165310Sarchie break; 70265310Sarchie } 70365310Sarchie 70465310Sarchie /* Send packet */ 70565310Sarchie NG_SEND_DATA(error, destLink->hook, m2, meta2); 70665310Sarchie } 70765310Sarchie return (error); 70865310Sarchie} 70965310Sarchie 71065310Sarchie/* 71165310Sarchie * Shutdown node 71265310Sarchie */ 71365310Sarchiestatic int 71465310Sarchieng_bridge_rmnode(node_p node) 71565310Sarchie{ 71665310Sarchie const priv_p priv = node->private; 71765310Sarchie 71865310Sarchie ng_unname(node); 71965310Sarchie ng_cutlinks(node); /* frees all link and host info */ 72065310Sarchie KASSERT(priv->numLinks == 0 && priv->numHosts == 0, 72165310Sarchie ("%s: numLinks=%d numHosts=%d", 72265310Sarchie __FUNCTION__, priv->numLinks, priv->numHosts)); 72365310Sarchie FREE(priv->tab, M_NETGRAPH); 72465310Sarchie FREE(priv, M_NETGRAPH); 72565310Sarchie node->private = NULL; 72665310Sarchie ng_unref(node); 72765310Sarchie return (0); 72865310Sarchie} 72965310Sarchie 73065310Sarchie/* 73165310Sarchie * Hook disconnection. 73265310Sarchie */ 73365310Sarchiestatic int 73465310Sarchieng_bridge_disconnect(hook_p hook) 73565310Sarchie{ 73665310Sarchie const priv_p priv = hook->node->private; 73765310Sarchie int linkNum; 73865310Sarchie 73965310Sarchie /* Get link number */ 74065310Sarchie linkNum = LINK_NUM(hook); 74165310Sarchie KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS, 74265310Sarchie ("%s: linkNum=%u", __FUNCTION__, linkNum)); 74365310Sarchie 74465310Sarchie /* Remove all hosts associated with this link */ 74565310Sarchie ng_bridge_remove_hosts(priv, linkNum); 74665310Sarchie 74765310Sarchie /* Free associated link information */ 74865310Sarchie KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __FUNCTION__)); 74965310Sarchie FREE(priv->links[linkNum], M_NETGRAPH); 75065310Sarchie priv->links[linkNum] = NULL; 75165310Sarchie priv->numLinks--; 75265310Sarchie 75365310Sarchie /* If no more hooks, go away */ 75465310Sarchie if (hook->node->numhooks == 0) 75565310Sarchie ng_rmnode(hook->node); 75665310Sarchie return (0); 75765310Sarchie} 75865310Sarchie 75965310Sarchie/****************************************************************** 76065310Sarchie HASH TABLE FUNCTIONS 76165310Sarchie******************************************************************/ 76265310Sarchie 76365310Sarchie/* 76465310Sarchie * Hash algorithm 76565310Sarchie * 76665310Sarchie * Only hashing bytes 3-6 of the Ethernet address is sufficient and fast. 76765310Sarchie */ 76865310Sarchie#define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \ 76965310Sarchie ^ ((const u_int16_t *)(addr))[1] \ 77065310Sarchie ^ ((const u_int16_t *)(addr))[2]) & (mask) ) 77165310Sarchie 77265310Sarchie/* 77365310Sarchie * Find a host entry in the table. 77465310Sarchie */ 77565310Sarchiestatic struct ng_bridge_host * 77665310Sarchieng_bridge_get(priv_p priv, const u_char *addr) 77765310Sarchie{ 77865310Sarchie const int bucket = HASH(addr, priv->hashMask); 77965310Sarchie struct ng_bridge_hent *hent; 78065310Sarchie 78165310Sarchie SLIST_FOREACH(hent, &priv->tab[bucket], next) { 78265310Sarchie if (ETHER_EQUAL(hent->host.addr, addr)) 78365310Sarchie return (&hent->host); 78465310Sarchie } 78565310Sarchie return (NULL); 78665310Sarchie} 78765310Sarchie 78865310Sarchie/* 78965310Sarchie * Add a new host entry to the table. This assumes the host doesn't 79065310Sarchie * already exist in the table. Returns 1 on success, 0 if there 79165310Sarchie * was a memory allocation failure. 79265310Sarchie */ 79365310Sarchiestatic int 79465310Sarchieng_bridge_put(priv_p priv, const u_char *addr, int linkNum) 79565310Sarchie{ 79665310Sarchie const int bucket = HASH(addr, priv->hashMask); 79765310Sarchie struct ng_bridge_hent *hent; 79865310Sarchie 79965310Sarchie#ifdef INVARIANTS 80065310Sarchie /* Assert that entry does not already exist in hashtable */ 80165310Sarchie SLIST_FOREACH(hent, &priv->tab[bucket], next) { 80265310Sarchie KASSERT(!ETHER_EQUAL(hent->host.addr, addr), 80365310Sarchie ("%s: entry %6D exists in table", __FUNCTION__, addr, ":")); 80465310Sarchie } 80565310Sarchie#endif 80665310Sarchie 80765310Sarchie /* Allocate and initialize new hashtable entry */ 80865310Sarchie MALLOC(hent, struct ng_bridge_hent *, 80965310Sarchie sizeof(*hent), M_NETGRAPH, M_NOWAIT); 81065310Sarchie if (hent == NULL) 81165310Sarchie return (0); 81265310Sarchie bcopy(addr, hent->host.addr, ETHER_ADDR_LEN); 81365310Sarchie hent->host.linkNum = linkNum; 81465310Sarchie hent->host.staleness = 0; 81565310Sarchie hent->host.age = 0; 81665310Sarchie 81765310Sarchie /* Add new element to hash bucket */ 81865310Sarchie SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next); 81965310Sarchie priv->numHosts++; 82065310Sarchie 82165310Sarchie /* Resize table if necessary */ 82265310Sarchie ng_bridge_rehash(priv); 82365310Sarchie return (1); 82465310Sarchie} 82565310Sarchie 82665310Sarchie/* 82765310Sarchie * Resize the hash table. We try to maintain the number of buckets 82865310Sarchie * such that the load factor is in the range 0.25 to 1.0. 82965310Sarchie * 83065310Sarchie * If we can't get the new memory then we silently fail. This is OK 83165310Sarchie * because things will still work and we'll try again soon anyway. 83265310Sarchie */ 83365310Sarchiestatic void 83465310Sarchieng_bridge_rehash(priv_p priv) 83565310Sarchie{ 83665310Sarchie struct ng_bridge_bucket *newTab; 83765310Sarchie int oldBucket, newBucket; 83865310Sarchie int newNumBuckets; 83965310Sarchie u_int newMask; 84065310Sarchie 84165310Sarchie /* Is table too full or too empty? */ 84265310Sarchie if (priv->numHosts > priv->numBuckets 84365310Sarchie && (priv->numBuckets << 1) <= MAX_BUCKETS) 84465310Sarchie newNumBuckets = priv->numBuckets << 1; 84565310Sarchie else if (priv->numHosts < (priv->numBuckets >> 2) 84665310Sarchie && (priv->numBuckets >> 2) >= MIN_BUCKETS) 84765310Sarchie newNumBuckets = priv->numBuckets >> 2; 84865310Sarchie else 84965310Sarchie return; 85065310Sarchie newMask = newNumBuckets - 1; 85165310Sarchie 85265310Sarchie /* Allocate and initialize new table */ 85365310Sarchie MALLOC(newTab, struct ng_bridge_bucket *, 85468876Sdwmalone newNumBuckets * sizeof(*newTab), M_NETGRAPH, M_NOWAIT | M_ZERO); 85565310Sarchie if (newTab == NULL) 85665310Sarchie return; 85765310Sarchie 85865310Sarchie /* Move all entries from old table to new table */ 85965310Sarchie for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) { 86065310Sarchie struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket]; 86165310Sarchie 86265310Sarchie while (!SLIST_EMPTY(oldList)) { 86365310Sarchie struct ng_bridge_hent *const hent 86465310Sarchie = SLIST_FIRST(oldList); 86565310Sarchie 86665310Sarchie SLIST_REMOVE_HEAD(oldList, next); 86765310Sarchie newBucket = HASH(hent->host.addr, newMask); 86865310Sarchie SLIST_INSERT_HEAD(&newTab[newBucket], hent, next); 86965310Sarchie } 87065310Sarchie } 87165310Sarchie 87265310Sarchie /* Replace old table with new one */ 87365310Sarchie if (priv->conf.debugLevel >= 3) { 87465310Sarchie log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n", 87565310Sarchie ng_bridge_nodename(priv->node), 87665310Sarchie priv->numBuckets, newNumBuckets); 87765310Sarchie } 87865310Sarchie FREE(priv->tab, M_NETGRAPH); 87965310Sarchie priv->numBuckets = newNumBuckets; 88065310Sarchie priv->hashMask = newMask; 88165310Sarchie priv->tab = newTab; 88265310Sarchie return; 88365310Sarchie} 88465310Sarchie 88565310Sarchie/****************************************************************** 88665310Sarchie MISC FUNCTIONS 88765310Sarchie******************************************************************/ 88865310Sarchie 88965310Sarchie/* 89065310Sarchie * Remove all hosts associated with a specific link from the hashtable. 89165310Sarchie * If linkNum == -1, then remove all hosts in the table. 89265310Sarchie */ 89365310Sarchiestatic void 89465310Sarchieng_bridge_remove_hosts(priv_p priv, int linkNum) 89565310Sarchie{ 89665310Sarchie int bucket; 89765310Sarchie 89865310Sarchie for (bucket = 0; bucket < priv->numBuckets; bucket++) { 89965310Sarchie struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 90065310Sarchie 90165310Sarchie while (*hptr != NULL) { 90265310Sarchie struct ng_bridge_hent *const hent = *hptr; 90365310Sarchie 90465310Sarchie if (linkNum == -1 || hent->host.linkNum == linkNum) { 90565310Sarchie *hptr = SLIST_NEXT(hent, next); 90665310Sarchie FREE(hent, M_NETGRAPH); 90765310Sarchie priv->numHosts--; 90865310Sarchie } else 90965310Sarchie hptr = &SLIST_NEXT(hent, next); 91065310Sarchie } 91165310Sarchie } 91265310Sarchie} 91365310Sarchie 91465310Sarchie/* 91565310Sarchie * Handle our once-per-second timeout event. We do two things: 91665310Sarchie * we decrement link->loopCount for those links being muted due to 91765310Sarchie * a detected loopback condition, and we remove any hosts from 91865310Sarchie * the hashtable whom we haven't heard from in a long while. 91965310Sarchie */ 92065310Sarchiestatic void 92165310Sarchieng_bridge_timeout(void *arg) 92265310Sarchie{ 92365310Sarchie const node_p node = arg; 92465310Sarchie const priv_p priv = node->private; 92565310Sarchie int s, bucket; 92665310Sarchie int counter = 0; 92765310Sarchie int linkNum; 92865310Sarchie 92965310Sarchie /* Avoid race condition with ng_bridge_shutdown() */ 93065310Sarchie s = splnet(); 93165310Sarchie if ((node->flags & NG_INVALID) != 0 || priv == NULL) { 93265310Sarchie ng_unref(node); 93365310Sarchie splx(s); 93465310Sarchie return; 93565310Sarchie } 93665310Sarchie 93765310Sarchie /* Register a new timeout, keeping the existing node reference */ 93865310Sarchie callout_reset(&priv->timer, hz, ng_bridge_timeout, node); 93965310Sarchie 94065310Sarchie /* Update host time counters and remove stale entries */ 94165310Sarchie for (bucket = 0; bucket < priv->numBuckets; bucket++) { 94265310Sarchie struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]); 94365310Sarchie 94465310Sarchie while (*hptr != NULL) { 94565310Sarchie struct ng_bridge_hent *const hent = *hptr; 94665310Sarchie 94765310Sarchie /* Make sure host's link really exists */ 94865310Sarchie KASSERT(priv->links[hent->host.linkNum] != NULL, 94965310Sarchie ("%s: host %6D on nonexistent link %d\n", 95065310Sarchie __FUNCTION__, hent->host.addr, ":", 95165310Sarchie hent->host.linkNum)); 95265310Sarchie 95365310Sarchie /* Remove hosts we haven't heard from in a while */ 95465310Sarchie if (++hent->host.staleness >= priv->conf.maxStaleness) { 95565310Sarchie *hptr = SLIST_NEXT(hent, next); 95665310Sarchie FREE(hent, M_NETGRAPH); 95765310Sarchie priv->numHosts--; 95865310Sarchie } else { 95965310Sarchie if (hent->host.age < 0xffff) 96065310Sarchie hent->host.age++; 96165310Sarchie hptr = &SLIST_NEXT(hent, next); 96265310Sarchie counter++; 96365310Sarchie } 96465310Sarchie } 96565310Sarchie } 96665310Sarchie KASSERT(priv->numHosts == counter, 96765310Sarchie ("%s: hosts: %d != %d", __FUNCTION__, priv->numHosts, counter)); 96865310Sarchie 96965310Sarchie /* Decrease table size if necessary */ 97065310Sarchie ng_bridge_rehash(priv); 97165310Sarchie 97265310Sarchie /* Decrease loop counter on muted looped back links */ 97365310Sarchie for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) { 97465310Sarchie struct ng_bridge_link *const link = priv->links[linkNum]; 97565310Sarchie 97665310Sarchie if (link != NULL) { 97765310Sarchie if (link->loopCount != 0) { 97865310Sarchie link->loopCount--; 97965310Sarchie if (link->loopCount == 0 98065310Sarchie && priv->conf.debugLevel >= 2) { 98165310Sarchie log(LOG_INFO, "ng_bridge: %s:" 98265310Sarchie " restoring looped back link%d\n", 98365310Sarchie ng_bridge_nodename(node), linkNum); 98465310Sarchie } 98565310Sarchie } 98665310Sarchie counter++; 98765310Sarchie } 98865310Sarchie } 98965310Sarchie KASSERT(priv->numLinks == counter, 99065310Sarchie ("%s: links: %d != %d", __FUNCTION__, priv->numLinks, counter)); 99165310Sarchie 99265310Sarchie /* Done */ 99365310Sarchie splx(s); 99465310Sarchie} 99565310Sarchie 99665310Sarchie/* 99765310Sarchie * Return node's "name", even if it doesn't have one. 99865310Sarchie */ 99965310Sarchiestatic const char * 100065310Sarchieng_bridge_nodename(node_p node) 100165310Sarchie{ 100265310Sarchie static char name[NG_NODELEN+1]; 100365310Sarchie 100465310Sarchie if (node->name != NULL) 100565310Sarchie snprintf(name, sizeof(name), "%s", node->name); 100665310Sarchie else 100765310Sarchie snprintf(name, sizeof(name), "[%x]", ng_node2ID(node)); 100865310Sarchie return name; 100965310Sarchie} 101065310Sarchie 1011