ng_bridge.c revision 70159
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 70159 2000-12-18 20:03:32Z julian $
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 = {
26970159Sjulian	NG_ABI_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_disconnect,
28065310Sarchie	ng_bridge_cmdlist,
28165310Sarchie};
28266887SarchieNETGRAPH_INIT(bridge, &ng_bridge_typestruct);
28365310Sarchie
28465310Sarchie/* Depend on ng_ether so we can use the Ethernet parse type */
28565310SarchieMODULE_DEPEND(ng_bridge, ng_ether, 1, 1, 1);
28665310Sarchie
28765310Sarchie/******************************************************************
28865310Sarchie		    NETGRAPH NODE METHODS
28965310Sarchie******************************************************************/
29065310Sarchie
29165310Sarchie/*
29265310Sarchie * Node constructor
29365310Sarchie */
29465310Sarchiestatic int
29565310Sarchieng_bridge_constructor(node_p *nodep)
29665310Sarchie{
29765310Sarchie	priv_p priv;
29865310Sarchie	int error;
29965310Sarchie
30065310Sarchie	/* Allocate and initialize private info */
30168876Sdwmalone	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
30265310Sarchie	if (priv == NULL)
30365310Sarchie		return (ENOMEM);
30469225Sjlemon	callout_init(&priv->timer, 0);
30565310Sarchie
30665310Sarchie	/* Allocate and initialize hash table, etc. */
30765310Sarchie	MALLOC(priv->tab, struct ng_bridge_bucket *,
30868876Sdwmalone	    MIN_BUCKETS * sizeof(*priv->tab), M_NETGRAPH, M_NOWAIT | M_ZERO);
30965310Sarchie	if (priv->tab == NULL) {
31065310Sarchie		FREE(priv, M_NETGRAPH);
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
32065310Sarchie	/* Call superclass constructor */
32165310Sarchie	if ((error = ng_make_node_common(&ng_bridge_typestruct, nodep))) {
32265310Sarchie		FREE(priv, M_NETGRAPH);
32365310Sarchie		return (error);
32465310Sarchie	}
32565310Sarchie	(*nodep)->private = priv;
32665310Sarchie	priv->node = *nodep;
32765310Sarchie
32865310Sarchie	/* Start timer by faking a timeout event */
32965310Sarchie	(*nodep)->refs++;
33065310Sarchie	ng_bridge_timeout(*nodep);
33165310Sarchie	return (0);
33265310Sarchie}
33365310Sarchie
33465310Sarchie/*
33565310Sarchie * Method for attaching a new hook
33665310Sarchie */
33765310Sarchiestatic	int
33865310Sarchieng_bridge_newhook(node_p node, hook_p hook, const char *name)
33965310Sarchie{
34065310Sarchie	const priv_p priv = node->private;
34165310Sarchie
34265310Sarchie	/* Check for a link hook */
34365310Sarchie	if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX,
34465310Sarchie	    strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) {
34565310Sarchie		const char *cp;
34665310Sarchie		char *eptr;
34765310Sarchie		u_long linkNum;
34865310Sarchie
34965310Sarchie		cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX);
35065310Sarchie		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
35165310Sarchie			return (EINVAL);
35265310Sarchie		linkNum = strtoul(cp, &eptr, 10);
35365310Sarchie		if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS)
35465310Sarchie			return (EINVAL);
35565310Sarchie		if (priv->links[linkNum] != NULL)
35665310Sarchie			return (EISCONN);
35765310Sarchie		MALLOC(priv->links[linkNum], struct ng_bridge_link *,
35868876Sdwmalone		    sizeof(*priv->links[linkNum]), M_NETGRAPH, M_NOWAIT|M_ZERO);
35965310Sarchie		if (priv->links[linkNum] == NULL)
36065310Sarchie			return (ENOMEM);
36165310Sarchie		priv->links[linkNum]->hook = hook;
36265310Sarchie		LINK_NUM(hook) = linkNum;
36365310Sarchie		priv->numLinks++;
36465310Sarchie		return (0);
36565310Sarchie	}
36665310Sarchie
36765310Sarchie	/* Unknown hook name */
36865310Sarchie	return (EINVAL);
36965310Sarchie}
37065310Sarchie
37165310Sarchie/*
37265310Sarchie * Receive a control message
37365310Sarchie */
37465310Sarchiestatic int
37565310Sarchieng_bridge_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
37665310Sarchie		struct ng_mesg **rptr, hook_p lasthook)
37765310Sarchie{
37865310Sarchie	const priv_p priv = node->private;
37965310Sarchie	struct ng_mesg *resp = NULL;
38065310Sarchie	int error = 0;
38165310Sarchie
38265310Sarchie	switch (msg->header.typecookie) {
38365310Sarchie	case NGM_BRIDGE_COOKIE:
38465310Sarchie		switch (msg->header.cmd) {
38565310Sarchie		case NGM_BRIDGE_GET_CONFIG:
38665310Sarchie		    {
38765310Sarchie			struct ng_bridge_config *conf;
38865310Sarchie
38965310Sarchie			NG_MKRESPONSE(resp, msg,
39065310Sarchie			    sizeof(struct ng_bridge_config), M_NOWAIT);
39165310Sarchie			if (resp == NULL) {
39265310Sarchie				error = ENOMEM;
39365310Sarchie				break;
39465310Sarchie			}
39565310Sarchie			conf = (struct ng_bridge_config *)resp->data;
39665310Sarchie			*conf = priv->conf;	/* no sanity checking needed */
39765310Sarchie			break;
39865310Sarchie		    }
39965310Sarchie		case NGM_BRIDGE_SET_CONFIG:
40065310Sarchie		    {
40165310Sarchie			struct ng_bridge_config *conf;
40265310Sarchie			int i;
40365310Sarchie
40465310Sarchie			if (msg->header.arglen
40565310Sarchie			    != sizeof(struct ng_bridge_config)) {
40665310Sarchie				error = EINVAL;
40765310Sarchie				break;
40865310Sarchie			}
40965310Sarchie			conf = (struct ng_bridge_config *)msg->data;
41065310Sarchie			priv->conf = *conf;
41165310Sarchie			for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++)
41265310Sarchie				priv->conf.ipfw[i] = !!priv->conf.ipfw[i];
41365310Sarchie			break;
41465310Sarchie		    }
41565310Sarchie		case NGM_BRIDGE_RESET:
41665310Sarchie		    {
41765310Sarchie			int i;
41865310Sarchie
41965310Sarchie			/* Flush all entries in the hash table */
42065310Sarchie			ng_bridge_remove_hosts(priv, -1);
42165310Sarchie
42265310Sarchie			/* Reset all loop detection counters and stats */
42365310Sarchie			for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) {
42465310Sarchie				if (priv->links[i] == NULL)
42565310Sarchie					continue;
42665310Sarchie				priv->links[i]->loopCount = 0;
42765310Sarchie				bzero(&priv->links[i]->stats,
42865310Sarchie				    sizeof(priv->links[i]->stats));
42965310Sarchie			}
43065310Sarchie			break;
43165310Sarchie		    }
43265310Sarchie		case NGM_BRIDGE_GET_STATS:
43365310Sarchie		case NGM_BRIDGE_CLR_STATS:
43465310Sarchie		case NGM_BRIDGE_GETCLR_STATS:
43565310Sarchie		    {
43665310Sarchie			struct ng_bridge_link *link;
43765310Sarchie			int linkNum;
43865310Sarchie
43965310Sarchie			/* Get link number */
44065310Sarchie			if (msg->header.arglen != sizeof(u_int32_t)) {
44165310Sarchie				error = EINVAL;
44265310Sarchie				break;
44365310Sarchie			}
44465310Sarchie			linkNum = *((u_int32_t *)msg->data);
44565310Sarchie			if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) {
44665310Sarchie				error = EINVAL;
44765310Sarchie				break;
44865310Sarchie			}
44965310Sarchie			if ((link = priv->links[linkNum]) == NULL) {
45065310Sarchie				error = ENOTCONN;
45165310Sarchie				break;
45265310Sarchie			}
45365310Sarchie
45465310Sarchie			/* Get/clear stats */
45565310Sarchie			if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
45665310Sarchie				NG_MKRESPONSE(resp, msg,
45765310Sarchie				    sizeof(link->stats), M_NOWAIT);
45865310Sarchie				if (resp == NULL) {
45965310Sarchie					error = ENOMEM;
46065310Sarchie					break;
46165310Sarchie				}
46265310Sarchie				bcopy(&link->stats,
46365310Sarchie				    resp->data, sizeof(link->stats));
46465310Sarchie			}
46565310Sarchie			if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
46665310Sarchie				bzero(&link->stats, sizeof(link->stats));
46765310Sarchie			break;
46865310Sarchie		    }
46965310Sarchie		case NGM_BRIDGE_GET_TABLE:
47065310Sarchie		    {
47165310Sarchie			struct ng_bridge_host_ary *ary;
47265310Sarchie			struct ng_bridge_hent *hent;
47365310Sarchie			int i = 0, bucket;
47465310Sarchie
47565310Sarchie			NG_MKRESPONSE(resp, msg, sizeof(*ary)
47665310Sarchie			    + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
47765310Sarchie			if (resp == NULL) {
47865310Sarchie				error = ENOMEM;
47965310Sarchie				break;
48065310Sarchie			}
48165310Sarchie			ary = (struct ng_bridge_host_ary *)resp->data;
48265310Sarchie			ary->numHosts = priv->numHosts;
48365310Sarchie			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
48465310Sarchie				SLIST_FOREACH(hent, &priv->tab[bucket], next)
48565310Sarchie					ary->hosts[i++] = hent->host;
48665310Sarchie			}
48765310Sarchie			break;
48865310Sarchie		    }
48965310Sarchie		default:
49065310Sarchie			error = EINVAL;
49165310Sarchie			break;
49265310Sarchie		}
49365310Sarchie		break;
49465310Sarchie	default:
49565310Sarchie		error = EINVAL;
49665310Sarchie		break;
49765310Sarchie	}
49865310Sarchie
49965310Sarchie	/* Done */
50065310Sarchie	if (rptr)
50165310Sarchie		*rptr = resp;
50265310Sarchie	else if (resp != NULL)
50365310Sarchie		FREE(resp, M_NETGRAPH);
50465310Sarchie	FREE(msg, M_NETGRAPH);
50565310Sarchie	return (error);
50665310Sarchie}
50765310Sarchie
50865310Sarchie/*
50965310Sarchie * Receive data on a hook
51065310Sarchie */
51165310Sarchiestatic int
51265310Sarchieng_bridge_rcvdata(hook_p hook, struct mbuf *m, meta_p meta,
51369922Sjulian		struct mbuf **ret_m, meta_p *ret_meta, struct ng_mesg **resp)
51465310Sarchie{
51565310Sarchie	const node_p node = hook->node;
51665310Sarchie	const priv_p priv = node->private;
51765310Sarchie	struct ng_bridge_host *host;
51865310Sarchie	struct ng_bridge_link *link;
51965310Sarchie	struct ether_header *eh;
52065310Sarchie	int error = 0, linkNum;
52165310Sarchie	int i, manycast;
52265310Sarchie
52365310Sarchie	/* Get link number */
52465310Sarchie	linkNum = LINK_NUM(hook);
52565310Sarchie	KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
52665310Sarchie	    ("%s: linkNum=%u", __FUNCTION__, linkNum));
52765310Sarchie	link = priv->links[linkNum];
52865310Sarchie	KASSERT(link != NULL, ("%s: link%d null", __FUNCTION__, linkNum));
52965310Sarchie
53065310Sarchie	/* Sanity check packet and pull up header */
53165310Sarchie	if (m->m_pkthdr.len < ETHER_HDR_LEN) {
53265310Sarchie		link->stats.recvRunts++;
53365310Sarchie		NG_FREE_DATA(m, meta);
53465310Sarchie		return (EINVAL);
53565310Sarchie	}
53665310Sarchie	if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) {
53765310Sarchie		link->stats.memoryFailures++;
53865310Sarchie		NG_FREE_META(meta);
53965310Sarchie		return (ENOBUFS);
54065310Sarchie	}
54165310Sarchie	eh = mtod(m, struct ether_header *);
54265310Sarchie	if ((eh->ether_shost[0] & 1) != 0) {
54365310Sarchie		link->stats.recvInvalid++;
54465310Sarchie		NG_FREE_DATA(m, meta);
54565310Sarchie		return (EINVAL);
54665310Sarchie	}
54765310Sarchie
54865310Sarchie	/* Is link disabled due to a loopback condition? */
54965310Sarchie	if (link->loopCount != 0) {
55065310Sarchie		link->stats.loopDrops++;
55165310Sarchie		NG_FREE_DATA(m, meta);
55265310Sarchie		return (ELOOP);		/* XXX is this an appropriate error? */
55365310Sarchie	}
55465310Sarchie
55565310Sarchie	/* Update stats */
55665310Sarchie	link->stats.recvPackets++;
55765310Sarchie	link->stats.recvOctets += m->m_pkthdr.len;
55865310Sarchie	if ((manycast = (eh->ether_dhost[0] & 1)) != 0) {
55965310Sarchie		if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
56065310Sarchie			link->stats.recvBroadcasts++;
56165310Sarchie			manycast = 2;
56265310Sarchie		} else
56365310Sarchie			link->stats.recvMulticasts++;
56465310Sarchie	}
56565310Sarchie
56665310Sarchie	/* Look up packet's source Ethernet address in hashtable */
56765310Sarchie	if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
56865310Sarchie
56965310Sarchie		/* Update time since last heard from this host */
57065310Sarchie		host->staleness = 0;
57165310Sarchie
57265310Sarchie		/* Did host jump to a different link? */
57365310Sarchie		if (host->linkNum != linkNum) {
57465310Sarchie
57565310Sarchie			/*
57665310Sarchie			 * If the host's old link was recently established
57765310Sarchie			 * on the old link and it's already jumped to a new
57865310Sarchie			 * link, declare a loopback condition.
57965310Sarchie			 */
58065310Sarchie			if (host->age < priv->conf.minStableAge) {
58165310Sarchie
58265310Sarchie				/* Log the problem */
58365310Sarchie				if (priv->conf.debugLevel >= 2) {
58465310Sarchie					struct ifnet *ifp = m->m_pkthdr.rcvif;
58565310Sarchie					char suffix[32];
58665310Sarchie
58765310Sarchie					if (ifp != NULL)
58865310Sarchie						snprintf(suffix, sizeof(suffix),
58965310Sarchie						    " (%s%d)", ifp->if_name,
59065310Sarchie						    ifp->if_unit);
59165310Sarchie					else
59265310Sarchie						*suffix = '\0';
59365310Sarchie					log(LOG_WARNING, "ng_bridge: %s:"
59465310Sarchie					    " loopback detected on %s%s\n",
59565310Sarchie					    ng_bridge_nodename(node),
59665310Sarchie					    hook->name, suffix);
59765310Sarchie				}
59865310Sarchie
59965310Sarchie				/* Mark link as linka non grata */
60065310Sarchie				link->loopCount = priv->conf.loopTimeout;
60165310Sarchie				link->stats.loopDetects++;
60265310Sarchie
60365310Sarchie				/* Forget all hosts on this link */
60465310Sarchie				ng_bridge_remove_hosts(priv, linkNum);
60565310Sarchie
60665310Sarchie				/* Drop packet */
60765310Sarchie				link->stats.loopDrops++;
60865310Sarchie				NG_FREE_DATA(m, meta);
60965310Sarchie				return (ELOOP);		/* XXX appropriate? */
61065310Sarchie			}
61165310Sarchie
61265310Sarchie			/* Move host over to new link */
61365310Sarchie			host->linkNum = linkNum;
61465310Sarchie			host->age = 0;
61565310Sarchie		}
61665310Sarchie	} else {
61765310Sarchie		if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) {
61865310Sarchie			link->stats.memoryFailures++;
61965310Sarchie			NG_FREE_DATA(m, meta);
62065310Sarchie			return (ENOMEM);
62165310Sarchie		}
62265310Sarchie	}
62365310Sarchie
62465310Sarchie	/* Run packet through ipfw processing, if enabled */
62565310Sarchie	if (priv->conf.ipfw[linkNum] && fw_enable && ip_fw_chk_ptr != NULL) {
62665310Sarchie		/* XXX not implemented yet */
62765310Sarchie	}
62865310Sarchie
62965310Sarchie	/*
63065310Sarchie	 * If unicast and destination host known, deliver to host's link,
63165310Sarchie	 * unless it is the same link as the packet came in on.
63265310Sarchie	 */
63365310Sarchie	if (!manycast) {
63465310Sarchie
63565310Sarchie		/* Determine packet destination link */
63665310Sarchie		if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
63765310Sarchie			struct ng_bridge_link *const destLink
63865310Sarchie			    = priv->links[host->linkNum];
63965310Sarchie
64065310Sarchie			/* If destination same as incoming link, do nothing */
64165310Sarchie			KASSERT(destLink != NULL,
64265310Sarchie			    ("%s: link%d null", __FUNCTION__, host->linkNum));
64365310Sarchie			if (destLink == link) {
64465310Sarchie				NG_FREE_DATA(m, meta);
64565310Sarchie				return (0);
64665310Sarchie			}
64765310Sarchie
64865310Sarchie			/* Deliver packet out the destination link */
64965310Sarchie			destLink->stats.xmitPackets++;
65065310Sarchie			destLink->stats.xmitOctets += m->m_pkthdr.len;
65165310Sarchie			NG_SEND_DATA(error, destLink->hook, m, meta);
65265310Sarchie			return (error);
65365310Sarchie		}
65465310Sarchie
65565310Sarchie		/* Destination host is not known */
65665310Sarchie		link->stats.recvUnknown++;
65765310Sarchie	}
65865310Sarchie
65965310Sarchie	/* Distribute unknown, multicast, broadcast pkts to all other links */
66065310Sarchie	for (linkNum = i = 0; i < priv->numLinks - 1; linkNum++) {
66165310Sarchie		struct ng_bridge_link *const destLink = priv->links[linkNum];
66265310Sarchie		meta_p meta2 = NULL;
66365310Sarchie		struct mbuf *m2;
66465310Sarchie
66565310Sarchie		/* Skip incoming link and disconnected links */
66665310Sarchie		if (destLink == NULL || destLink == link)
66765310Sarchie			continue;
66865310Sarchie
66965310Sarchie		/* Copy mbuf and meta info */
67065310Sarchie		if (++i == priv->numLinks - 1) {		/* last link */
67165310Sarchie			m2 = m;
67265310Sarchie			meta2 = meta;
67365310Sarchie		}  else {
67466313Sarchie			m2 = m_dup(m, M_NOWAIT);	/* XXX m_copypacket() */
67565310Sarchie			if (m2 == NULL) {
67665310Sarchie				link->stats.memoryFailures++;
67765310Sarchie				NG_FREE_DATA(m, meta);
67865310Sarchie				return (ENOBUFS);
67965310Sarchie			}
68065310Sarchie			if (meta != NULL
68165310Sarchie			    && (meta2 = ng_copy_meta(meta)) == NULL) {
68265310Sarchie				link->stats.memoryFailures++;
68365310Sarchie				m_freem(m2);
68465310Sarchie				NG_FREE_DATA(m, meta);
68565310Sarchie				return (ENOMEM);
68665310Sarchie			}
68765310Sarchie		}
68865310Sarchie
68965310Sarchie		/* Update stats */
69065310Sarchie		destLink->stats.xmitPackets++;
69165310Sarchie		destLink->stats.xmitOctets += m->m_pkthdr.len;
69265310Sarchie		switch (manycast) {
69365310Sarchie		case 0:					/* unicast */
69465310Sarchie			break;
69565310Sarchie		case 1:					/* multicast */
69665310Sarchie			destLink->stats.xmitMulticasts++;
69765310Sarchie			break;
69865310Sarchie		case 2:					/* broadcast */
69965310Sarchie			destLink->stats.xmitBroadcasts++;
70065310Sarchie			break;
70165310Sarchie		}
70265310Sarchie
70365310Sarchie		/* Send packet */
70465310Sarchie		NG_SEND_DATA(error, destLink->hook, m2, meta2);
70565310Sarchie	}
70665310Sarchie	return (error);
70765310Sarchie}
70865310Sarchie
70965310Sarchie/*
71065310Sarchie * Shutdown node
71165310Sarchie */
71265310Sarchiestatic int
71365310Sarchieng_bridge_rmnode(node_p node)
71465310Sarchie{
71565310Sarchie	const priv_p priv = node->private;
71665310Sarchie
71765310Sarchie	ng_unname(node);
71865310Sarchie	ng_cutlinks(node);		/* frees all link and host info */
71965310Sarchie	KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
72065310Sarchie	    ("%s: numLinks=%d numHosts=%d",
72165310Sarchie	    __FUNCTION__, priv->numLinks, priv->numHosts));
72265310Sarchie	FREE(priv->tab, M_NETGRAPH);
72365310Sarchie	FREE(priv, M_NETGRAPH);
72465310Sarchie	node->private = NULL;
72565310Sarchie	ng_unref(node);
72665310Sarchie	return (0);
72765310Sarchie}
72865310Sarchie
72965310Sarchie/*
73065310Sarchie * Hook disconnection.
73165310Sarchie */
73265310Sarchiestatic int
73365310Sarchieng_bridge_disconnect(hook_p hook)
73465310Sarchie{
73565310Sarchie	const priv_p priv = hook->node->private;
73665310Sarchie	int linkNum;
73765310Sarchie
73865310Sarchie	/* Get link number */
73965310Sarchie	linkNum = LINK_NUM(hook);
74065310Sarchie	KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
74165310Sarchie	    ("%s: linkNum=%u", __FUNCTION__, linkNum));
74265310Sarchie
74365310Sarchie	/* Remove all hosts associated with this link */
74465310Sarchie	ng_bridge_remove_hosts(priv, linkNum);
74565310Sarchie
74665310Sarchie	/* Free associated link information */
74765310Sarchie	KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __FUNCTION__));
74865310Sarchie	FREE(priv->links[linkNum], M_NETGRAPH);
74965310Sarchie	priv->links[linkNum] = NULL;
75065310Sarchie	priv->numLinks--;
75165310Sarchie
75265310Sarchie	/* If no more hooks, go away */
75365310Sarchie	if (hook->node->numhooks == 0)
75465310Sarchie		ng_rmnode(hook->node);
75565310Sarchie	return (0);
75665310Sarchie}
75765310Sarchie
75865310Sarchie/******************************************************************
75965310Sarchie		    HASH TABLE FUNCTIONS
76065310Sarchie******************************************************************/
76165310Sarchie
76265310Sarchie/*
76365310Sarchie * Hash algorithm
76465310Sarchie *
76565310Sarchie * Only hashing bytes 3-6 of the Ethernet address is sufficient and fast.
76665310Sarchie */
76765310Sarchie#define HASH(addr,mask)		( (((const u_int16_t *)(addr))[0] 	\
76865310Sarchie				 ^ ((const u_int16_t *)(addr))[1] 	\
76965310Sarchie				 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
77065310Sarchie
77165310Sarchie/*
77265310Sarchie * Find a host entry in the table.
77365310Sarchie */
77465310Sarchiestatic struct ng_bridge_host *
77565310Sarchieng_bridge_get(priv_p priv, const u_char *addr)
77665310Sarchie{
77765310Sarchie	const int bucket = HASH(addr, priv->hashMask);
77865310Sarchie	struct ng_bridge_hent *hent;
77965310Sarchie
78065310Sarchie	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
78165310Sarchie		if (ETHER_EQUAL(hent->host.addr, addr))
78265310Sarchie			return (&hent->host);
78365310Sarchie	}
78465310Sarchie	return (NULL);
78565310Sarchie}
78665310Sarchie
78765310Sarchie/*
78865310Sarchie * Add a new host entry to the table. This assumes the host doesn't
78965310Sarchie * already exist in the table. Returns 1 on success, 0 if there
79065310Sarchie * was a memory allocation failure.
79165310Sarchie */
79265310Sarchiestatic int
79365310Sarchieng_bridge_put(priv_p priv, const u_char *addr, int linkNum)
79465310Sarchie{
79565310Sarchie	const int bucket = HASH(addr, priv->hashMask);
79665310Sarchie	struct ng_bridge_hent *hent;
79765310Sarchie
79865310Sarchie#ifdef INVARIANTS
79965310Sarchie	/* Assert that entry does not already exist in hashtable */
80065310Sarchie	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
80165310Sarchie		KASSERT(!ETHER_EQUAL(hent->host.addr, addr),
80265310Sarchie		    ("%s: entry %6D exists in table", __FUNCTION__, addr, ":"));
80365310Sarchie	}
80465310Sarchie#endif
80565310Sarchie
80665310Sarchie	/* Allocate and initialize new hashtable entry */
80765310Sarchie	MALLOC(hent, struct ng_bridge_hent *,
80865310Sarchie	    sizeof(*hent), M_NETGRAPH, M_NOWAIT);
80965310Sarchie	if (hent == NULL)
81065310Sarchie		return (0);
81165310Sarchie	bcopy(addr, hent->host.addr, ETHER_ADDR_LEN);
81265310Sarchie	hent->host.linkNum = linkNum;
81365310Sarchie	hent->host.staleness = 0;
81465310Sarchie	hent->host.age = 0;
81565310Sarchie
81665310Sarchie	/* Add new element to hash bucket */
81765310Sarchie	SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next);
81865310Sarchie	priv->numHosts++;
81965310Sarchie
82065310Sarchie	/* Resize table if necessary */
82165310Sarchie	ng_bridge_rehash(priv);
82265310Sarchie	return (1);
82365310Sarchie}
82465310Sarchie
82565310Sarchie/*
82665310Sarchie * Resize the hash table. We try to maintain the number of buckets
82765310Sarchie * such that the load factor is in the range 0.25 to 1.0.
82865310Sarchie *
82965310Sarchie * If we can't get the new memory then we silently fail. This is OK
83065310Sarchie * because things will still work and we'll try again soon anyway.
83165310Sarchie */
83265310Sarchiestatic void
83365310Sarchieng_bridge_rehash(priv_p priv)
83465310Sarchie{
83565310Sarchie	struct ng_bridge_bucket *newTab;
83665310Sarchie	int oldBucket, newBucket;
83765310Sarchie	int newNumBuckets;
83865310Sarchie	u_int newMask;
83965310Sarchie
84065310Sarchie	/* Is table too full or too empty? */
84165310Sarchie	if (priv->numHosts > priv->numBuckets
84265310Sarchie	    && (priv->numBuckets << 1) <= MAX_BUCKETS)
84365310Sarchie		newNumBuckets = priv->numBuckets << 1;
84465310Sarchie	else if (priv->numHosts < (priv->numBuckets >> 2)
84565310Sarchie	    && (priv->numBuckets >> 2) >= MIN_BUCKETS)
84665310Sarchie		newNumBuckets = priv->numBuckets >> 2;
84765310Sarchie	else
84865310Sarchie		return;
84965310Sarchie	newMask = newNumBuckets - 1;
85065310Sarchie
85165310Sarchie	/* Allocate and initialize new table */
85265310Sarchie	MALLOC(newTab, struct ng_bridge_bucket *,
85368876Sdwmalone	    newNumBuckets * sizeof(*newTab), M_NETGRAPH, M_NOWAIT | M_ZERO);
85465310Sarchie	if (newTab == NULL)
85565310Sarchie		return;
85665310Sarchie
85765310Sarchie	/* Move all entries from old table to new table */
85865310Sarchie	for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
85965310Sarchie		struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
86065310Sarchie
86165310Sarchie		while (!SLIST_EMPTY(oldList)) {
86265310Sarchie			struct ng_bridge_hent *const hent
86365310Sarchie			    = SLIST_FIRST(oldList);
86465310Sarchie
86565310Sarchie			SLIST_REMOVE_HEAD(oldList, next);
86665310Sarchie			newBucket = HASH(hent->host.addr, newMask);
86765310Sarchie			SLIST_INSERT_HEAD(&newTab[newBucket], hent, next);
86865310Sarchie		}
86965310Sarchie	}
87065310Sarchie
87165310Sarchie	/* Replace old table with new one */
87265310Sarchie	if (priv->conf.debugLevel >= 3) {
87365310Sarchie		log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
87465310Sarchie		    ng_bridge_nodename(priv->node),
87565310Sarchie		    priv->numBuckets, newNumBuckets);
87665310Sarchie	}
87765310Sarchie	FREE(priv->tab, M_NETGRAPH);
87865310Sarchie	priv->numBuckets = newNumBuckets;
87965310Sarchie	priv->hashMask = newMask;
88065310Sarchie	priv->tab = newTab;
88165310Sarchie	return;
88265310Sarchie}
88365310Sarchie
88465310Sarchie/******************************************************************
88565310Sarchie		    MISC FUNCTIONS
88665310Sarchie******************************************************************/
88765310Sarchie
88865310Sarchie/*
88965310Sarchie * Remove all hosts associated with a specific link from the hashtable.
89065310Sarchie * If linkNum == -1, then remove all hosts in the table.
89165310Sarchie */
89265310Sarchiestatic void
89365310Sarchieng_bridge_remove_hosts(priv_p priv, int linkNum)
89465310Sarchie{
89565310Sarchie	int bucket;
89665310Sarchie
89765310Sarchie	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
89865310Sarchie		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
89965310Sarchie
90065310Sarchie		while (*hptr != NULL) {
90165310Sarchie			struct ng_bridge_hent *const hent = *hptr;
90265310Sarchie
90365310Sarchie			if (linkNum == -1 || hent->host.linkNum == linkNum) {
90465310Sarchie				*hptr = SLIST_NEXT(hent, next);
90565310Sarchie				FREE(hent, M_NETGRAPH);
90665310Sarchie				priv->numHosts--;
90765310Sarchie			} else
90865310Sarchie				hptr = &SLIST_NEXT(hent, next);
90965310Sarchie		}
91065310Sarchie	}
91165310Sarchie}
91265310Sarchie
91365310Sarchie/*
91465310Sarchie * Handle our once-per-second timeout event. We do two things:
91565310Sarchie * we decrement link->loopCount for those links being muted due to
91665310Sarchie * a detected loopback condition, and we remove any hosts from
91765310Sarchie * the hashtable whom we haven't heard from in a long while.
91865310Sarchie */
91965310Sarchiestatic void
92065310Sarchieng_bridge_timeout(void *arg)
92165310Sarchie{
92265310Sarchie	const node_p node = arg;
92365310Sarchie	const priv_p priv = node->private;
92465310Sarchie	int s, bucket;
92565310Sarchie	int counter = 0;
92665310Sarchie	int linkNum;
92765310Sarchie
92865310Sarchie	/* Avoid race condition with ng_bridge_shutdown() */
92965310Sarchie	s = splnet();
93065310Sarchie	if ((node->flags & NG_INVALID) != 0 || priv == NULL) {
93165310Sarchie		ng_unref(node);
93265310Sarchie		splx(s);
93365310Sarchie		return;
93465310Sarchie	}
93565310Sarchie
93665310Sarchie	/* Register a new timeout, keeping the existing node reference */
93765310Sarchie	callout_reset(&priv->timer, hz, ng_bridge_timeout, node);
93865310Sarchie
93965310Sarchie	/* Update host time counters and remove stale entries */
94065310Sarchie	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
94165310Sarchie		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
94265310Sarchie
94365310Sarchie		while (*hptr != NULL) {
94465310Sarchie			struct ng_bridge_hent *const hent = *hptr;
94565310Sarchie
94665310Sarchie			/* Make sure host's link really exists */
94765310Sarchie			KASSERT(priv->links[hent->host.linkNum] != NULL,
94865310Sarchie			    ("%s: host %6D on nonexistent link %d\n",
94965310Sarchie			    __FUNCTION__, hent->host.addr, ":",
95065310Sarchie			    hent->host.linkNum));
95165310Sarchie
95265310Sarchie			/* Remove hosts we haven't heard from in a while */
95365310Sarchie			if (++hent->host.staleness >= priv->conf.maxStaleness) {
95465310Sarchie				*hptr = SLIST_NEXT(hent, next);
95565310Sarchie				FREE(hent, M_NETGRAPH);
95665310Sarchie				priv->numHosts--;
95765310Sarchie			} else {
95865310Sarchie				if (hent->host.age < 0xffff)
95965310Sarchie					hent->host.age++;
96065310Sarchie				hptr = &SLIST_NEXT(hent, next);
96165310Sarchie				counter++;
96265310Sarchie			}
96365310Sarchie		}
96465310Sarchie	}
96565310Sarchie	KASSERT(priv->numHosts == counter,
96665310Sarchie	    ("%s: hosts: %d != %d", __FUNCTION__, priv->numHosts, counter));
96765310Sarchie
96865310Sarchie	/* Decrease table size if necessary */
96965310Sarchie	ng_bridge_rehash(priv);
97065310Sarchie
97165310Sarchie	/* Decrease loop counter on muted looped back links */
97265310Sarchie	for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) {
97365310Sarchie		struct ng_bridge_link *const link = priv->links[linkNum];
97465310Sarchie
97565310Sarchie		if (link != NULL) {
97665310Sarchie			if (link->loopCount != 0) {
97765310Sarchie				link->loopCount--;
97865310Sarchie				if (link->loopCount == 0
97965310Sarchie				    && priv->conf.debugLevel >= 2) {
98065310Sarchie					log(LOG_INFO, "ng_bridge: %s:"
98165310Sarchie					    " restoring looped back link%d\n",
98265310Sarchie					    ng_bridge_nodename(node), linkNum);
98365310Sarchie				}
98465310Sarchie			}
98565310Sarchie			counter++;
98665310Sarchie		}
98765310Sarchie	}
98865310Sarchie	KASSERT(priv->numLinks == counter,
98965310Sarchie	    ("%s: links: %d != %d", __FUNCTION__, priv->numLinks, counter));
99065310Sarchie
99165310Sarchie	/* Done */
99265310Sarchie	splx(s);
99365310Sarchie}
99465310Sarchie
99565310Sarchie/*
99665310Sarchie * Return node's "name", even if it doesn't have one.
99765310Sarchie */
99865310Sarchiestatic const char *
99965310Sarchieng_bridge_nodename(node_p node)
100065310Sarchie{
100165310Sarchie	static char name[NG_NODELEN+1];
100265310Sarchie
100365310Sarchie	if (node->name != NULL)
100465310Sarchie		snprintf(name, sizeof(name), "%s", node->name);
100565310Sarchie	else
100665310Sarchie		snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
100765310Sarchie	return name;
100865310Sarchie}
100965310Sarchie
1010