ng_bridge.c revision 200582
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 200582 2009-12-15 18:33:12Z luigi $
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>
64185895Szec#include <sys/lock.h>
6565310Sarchie#include <sys/malloc.h>
6665310Sarchie#include <sys/mbuf.h>
6765310Sarchie#include <sys/errno.h>
68185895Szec#include <sys/rwlock.h>
6965310Sarchie#include <sys/syslog.h>
7065310Sarchie#include <sys/socket.h>
7165310Sarchie#include <sys/ctype.h>
7265310Sarchie
7365310Sarchie#include <net/if.h>
7465310Sarchie#include <net/ethernet.h>
75196019Srwatson#include <net/vnet.h>
7665310Sarchie
7765310Sarchie#include <netinet/in.h>
78200582Sluigi#if 0	/* not used yet */
7965310Sarchie#include <netinet/ip_fw.h>
80200582Sluigi#endif
8165310Sarchie#include <netgraph/ng_message.h>
8265310Sarchie#include <netgraph/netgraph.h>
8365310Sarchie#include <netgraph/ng_parse.h>
8465310Sarchie#include <netgraph/ng_bridge.h>
8565310Sarchie
8670870Sjulian#ifdef NG_SEPARATE_MALLOC
8770870SjulianMALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge", "netgraph bridge node ");
8870870Sjulian#else
8970870Sjulian#define M_NETGRAPH_BRIDGE M_NETGRAPH
9070870Sjulian#endif
9170870Sjulian
9265310Sarchie/* Per-link private data */
9365310Sarchiestruct ng_bridge_link {
9465310Sarchie	hook_p				hook;		/* netgraph hook */
9565310Sarchie	u_int16_t			loopCount;	/* loop ignore timer */
9665310Sarchie	struct ng_bridge_link_stats	stats;		/* link stats */
9765310Sarchie};
9865310Sarchie
9965310Sarchie/* Per-node private data */
10065310Sarchiestruct ng_bridge_private {
10165310Sarchie	struct ng_bridge_bucket	*tab;		/* hash table bucket array */
10265310Sarchie	struct ng_bridge_link	*links[NG_BRIDGE_MAX_LINKS];
10365310Sarchie	struct ng_bridge_config	conf;		/* node configuration */
10465310Sarchie	node_p			node;		/* netgraph node */
10565310Sarchie	u_int			numHosts;	/* num entries in table */
10665310Sarchie	u_int			numBuckets;	/* num buckets in table */
10765310Sarchie	u_int			hashMask;	/* numBuckets - 1 */
10865310Sarchie	int			numLinks;	/* num connected links */
10965310Sarchie	struct callout		timer;		/* one second periodic timer */
11065310Sarchie};
11165310Sarchietypedef struct ng_bridge_private *priv_p;
11265310Sarchie
11365310Sarchie/* Information about a host, stored in a hash table entry */
11465310Sarchiestruct ng_bridge_hent {
11565310Sarchie	struct ng_bridge_host		host;	/* actual host info */
11665310Sarchie	SLIST_ENTRY(ng_bridge_hent)	next;	/* next entry in bucket */
11765310Sarchie};
11865310Sarchie
11965310Sarchie/* Hash table bucket declaration */
12065310SarchieSLIST_HEAD(ng_bridge_bucket, ng_bridge_hent);
12165310Sarchie
12265310Sarchie/* Netgraph node methods */
12365310Sarchiestatic ng_constructor_t	ng_bridge_constructor;
12465310Sarchiestatic ng_rcvmsg_t	ng_bridge_rcvmsg;
12570700Sjulianstatic ng_shutdown_t	ng_bridge_shutdown;
12665310Sarchiestatic ng_newhook_t	ng_bridge_newhook;
12765310Sarchiestatic ng_rcvdata_t	ng_bridge_rcvdata;
12865310Sarchiestatic ng_disconnect_t	ng_bridge_disconnect;
12965310Sarchie
13065310Sarchie/* Other internal functions */
13165310Sarchiestatic struct	ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr);
13265310Sarchiestatic int	ng_bridge_put(priv_p priv, const u_char *addr, int linkNum);
13365310Sarchiestatic void	ng_bridge_rehash(priv_p priv);
13465310Sarchiestatic void	ng_bridge_remove_hosts(priv_p priv, int linkNum);
135138834Sglebiusstatic void	ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2);
13665310Sarchiestatic const	char *ng_bridge_nodename(node_p node);
13765310Sarchie
13865310Sarchie/* Ethernet broadcast */
13965310Sarchiestatic const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
14065310Sarchie    { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
14165310Sarchie
14265310Sarchie/* Store each hook's link number in the private field */
14365310Sarchie#define LINK_NUM(hook)		(*(u_int16_t *)(&(hook)->private))
14465310Sarchie
14565310Sarchie/* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
14665310Sarchie#define ETHER_EQUAL(a,b)	(((const u_int32_t *)(a))[0] \
14765310Sarchie					== ((const u_int32_t *)(b))[0] \
14865310Sarchie				    && ((const u_int16_t *)(a))[2] \
14965310Sarchie					== ((const u_int16_t *)(b))[2])
15065310Sarchie
15165310Sarchie/* Minimum and maximum number of hash buckets. Must be a power of two. */
15265310Sarchie#define MIN_BUCKETS		(1 << 5)	/* 32 */
15365310Sarchie#define MAX_BUCKETS		(1 << 14)	/* 16384 */
15465310Sarchie
15565310Sarchie/* Configuration default values */
15665310Sarchie#define DEFAULT_LOOP_TIMEOUT	60
15765310Sarchie#define DEFAULT_MAX_STALENESS	(15 * 60)	/* same as ARP timeout */
15865310Sarchie#define DEFAULT_MIN_STABLE_AGE	1
15965310Sarchie
16065310Sarchie/******************************************************************
16165310Sarchie		    NETGRAPH PARSE TYPES
16265310Sarchie******************************************************************/
16365310Sarchie
16465310Sarchie/*
16565310Sarchie * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
16665310Sarchie */
16765310Sarchiestatic int
16865310Sarchieng_bridge_getTableLength(const struct ng_parse_type *type,
16965310Sarchie	const u_char *start, const u_char *buf)
17065310Sarchie{
17165310Sarchie	const struct ng_bridge_host_ary *const hary
17265310Sarchie	    = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
17365310Sarchie
17465310Sarchie	return hary->numHosts;
17565310Sarchie}
17665310Sarchie
17765310Sarchie/* Parse type for struct ng_bridge_host_ary */
17897685Sarchiestatic const struct ng_parse_struct_field ng_bridge_host_type_fields[]
179123600Sru	= NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
18065310Sarchiestatic const struct ng_parse_type ng_bridge_host_type = {
18165310Sarchie	&ng_parse_struct_type,
18297685Sarchie	&ng_bridge_host_type_fields
18365310Sarchie};
18465310Sarchiestatic const struct ng_parse_array_info ng_bridge_hary_type_info = {
18565310Sarchie	&ng_bridge_host_type,
18665310Sarchie	ng_bridge_getTableLength
18765310Sarchie};
18865310Sarchiestatic const struct ng_parse_type ng_bridge_hary_type = {
18965310Sarchie	&ng_parse_array_type,
19065310Sarchie	&ng_bridge_hary_type_info
19165310Sarchie};
19297685Sarchiestatic const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[]
19365310Sarchie	= NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
19465310Sarchiestatic const struct ng_parse_type ng_bridge_host_ary_type = {
19565310Sarchie	&ng_parse_struct_type,
19697685Sarchie	&ng_bridge_host_ary_type_fields
19765310Sarchie};
19865310Sarchie
19965310Sarchie/* Parse type for struct ng_bridge_config */
20065310Sarchiestatic const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = {
20165310Sarchie	&ng_parse_uint8_type,
20265310Sarchie	NG_BRIDGE_MAX_LINKS
20365310Sarchie};
20465310Sarchiestatic const struct ng_parse_type ng_bridge_ipfwary_type = {
20565310Sarchie	&ng_parse_fixedarray_type,
20665310Sarchie	&ng_bridge_ipfwary_type_info
20765310Sarchie};
20897685Sarchiestatic const struct ng_parse_struct_field ng_bridge_config_type_fields[]
20965310Sarchie	= NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type);
21065310Sarchiestatic const struct ng_parse_type ng_bridge_config_type = {
21165310Sarchie	&ng_parse_struct_type,
21297685Sarchie	&ng_bridge_config_type_fields
21365310Sarchie};
21465310Sarchie
21565310Sarchie/* Parse type for struct ng_bridge_link_stat */
21697685Sarchiestatic const struct ng_parse_struct_field ng_bridge_stats_type_fields[]
21797685Sarchie	= NG_BRIDGE_STATS_TYPE_INFO;
21865310Sarchiestatic const struct ng_parse_type ng_bridge_stats_type = {
21965310Sarchie	&ng_parse_struct_type,
22097685Sarchie	&ng_bridge_stats_type_fields
22165310Sarchie};
22265310Sarchie
22365310Sarchie/* List of commands and how to convert arguments to/from ASCII */
22465310Sarchiestatic const struct ng_cmdlist ng_bridge_cmdlist[] = {
22565310Sarchie	{
22665310Sarchie	  NGM_BRIDGE_COOKIE,
22765310Sarchie	  NGM_BRIDGE_SET_CONFIG,
22865310Sarchie	  "setconfig",
22965310Sarchie	  &ng_bridge_config_type,
23065310Sarchie	  NULL
23165310Sarchie	},
23265310Sarchie	{
23365310Sarchie	  NGM_BRIDGE_COOKIE,
23465310Sarchie	  NGM_BRIDGE_GET_CONFIG,
23565310Sarchie	  "getconfig",
23665310Sarchie	  NULL,
23765310Sarchie	  &ng_bridge_config_type
23865310Sarchie	},
23965310Sarchie	{
24065310Sarchie	  NGM_BRIDGE_COOKIE,
24165310Sarchie	  NGM_BRIDGE_RESET,
24265310Sarchie	  "reset",
24365310Sarchie	  NULL,
24465310Sarchie	  NULL
24565310Sarchie	},
24665310Sarchie	{
24765310Sarchie	  NGM_BRIDGE_COOKIE,
24865310Sarchie	  NGM_BRIDGE_GET_STATS,
24965310Sarchie	  "getstats",
25065310Sarchie	  &ng_parse_uint32_type,
25165310Sarchie	  &ng_bridge_stats_type
25265310Sarchie	},
25365310Sarchie	{
25465310Sarchie	  NGM_BRIDGE_COOKIE,
25565310Sarchie	  NGM_BRIDGE_CLR_STATS,
25665310Sarchie	  "clrstats",
25765310Sarchie	  &ng_parse_uint32_type,
25865310Sarchie	  NULL
25965310Sarchie	},
26065310Sarchie	{
26165310Sarchie	  NGM_BRIDGE_COOKIE,
26265310Sarchie	  NGM_BRIDGE_GETCLR_STATS,
26365310Sarchie	  "getclrstats",
26465310Sarchie	  &ng_parse_uint32_type,
26565310Sarchie	  &ng_bridge_stats_type
26665310Sarchie	},
26765310Sarchie	{
26865310Sarchie	  NGM_BRIDGE_COOKIE,
26965310Sarchie	  NGM_BRIDGE_GET_TABLE,
27065310Sarchie	  "gettable",
27165310Sarchie	  NULL,
27265310Sarchie	  &ng_bridge_host_ary_type
27365310Sarchie	},
27465310Sarchie	{ 0 }
27565310Sarchie};
27665310Sarchie
27765310Sarchie/* Node type descriptor */
27865310Sarchiestatic struct ng_type ng_bridge_typestruct = {
279129823Sjulian	.version =	NG_ABI_VERSION,
280129823Sjulian	.name =		NG_BRIDGE_NODE_TYPE,
281129823Sjulian	.constructor =	ng_bridge_constructor,
282129823Sjulian	.rcvmsg =	ng_bridge_rcvmsg,
283129823Sjulian	.shutdown =	ng_bridge_shutdown,
284129823Sjulian	.newhook =	ng_bridge_newhook,
285129823Sjulian	.rcvdata =	ng_bridge_rcvdata,
286129823Sjulian	.disconnect =	ng_bridge_disconnect,
287129823Sjulian	.cmdlist =	ng_bridge_cmdlist,
28865310Sarchie};
28966887SarchieNETGRAPH_INIT(bridge, &ng_bridge_typestruct);
29065310Sarchie
29165310Sarchie/******************************************************************
29265310Sarchie		    NETGRAPH NODE METHODS
29365310Sarchie******************************************************************/
29465310Sarchie
29565310Sarchie/*
29665310Sarchie * Node constructor
29765310Sarchie */
29865310Sarchiestatic int
29970700Sjulianng_bridge_constructor(node_p node)
30065310Sarchie{
30165310Sarchie	priv_p priv;
30265310Sarchie
30365310Sarchie	/* Allocate and initialize private info */
304184205Sdes	priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
30565310Sarchie	if (priv == NULL)
30665310Sarchie		return (ENOMEM);
307138834Sglebius	ng_callout_init(&priv->timer);
30865310Sarchie
30965310Sarchie	/* Allocate and initialize hash table, etc. */
310184214Sdes	priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab),
311184214Sdes	    M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
31265310Sarchie	if (priv->tab == NULL) {
313184205Sdes		free(priv, M_NETGRAPH_BRIDGE);
31465310Sarchie		return (ENOMEM);
31565310Sarchie	}
31665310Sarchie	priv->numBuckets = MIN_BUCKETS;
31765310Sarchie	priv->hashMask = MIN_BUCKETS - 1;
31865310Sarchie	priv->conf.debugLevel = 1;
31965310Sarchie	priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
32065310Sarchie	priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
32165310Sarchie	priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
32265310Sarchie
32370700Sjulian	/*
32470700Sjulian	 * This node has all kinds of stuff that could be screwed by SMP.
32570700Sjulian	 * Until it gets it's own internal protection, we go through in
32670700Sjulian	 * single file. This could hurt a machine bridging beteen two
32770700Sjulian	 * GB ethernets so it should be fixed.
32870700Sjulian	 * When it's fixed the process SHOULD NOT SLEEP, spinlocks please!
32970700Sjulian	 * (and atomic ops )
33070700Sjulian	 */
33170784Sjulian	NG_NODE_FORCE_WRITER(node);
33270784Sjulian	NG_NODE_SET_PRIVATE(node, priv);
33370700Sjulian	priv->node = node;
33465310Sarchie
33587997Sarchie	/* Start timer; timer is always running while node is alive */
336138834Sglebius	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
33787997Sarchie
33887997Sarchie	/* Done */
33965310Sarchie	return (0);
34065310Sarchie}
34165310Sarchie
34265310Sarchie/*
34365310Sarchie * Method for attaching a new hook
34465310Sarchie */
34565310Sarchiestatic	int
34665310Sarchieng_bridge_newhook(node_p node, hook_p hook, const char *name)
34765310Sarchie{
34870784Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
34965310Sarchie
35065310Sarchie	/* Check for a link hook */
35165310Sarchie	if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX,
35265310Sarchie	    strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) {
35365310Sarchie		const char *cp;
35465310Sarchie		char *eptr;
35565310Sarchie		u_long linkNum;
35665310Sarchie
35765310Sarchie		cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX);
35865310Sarchie		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
35965310Sarchie			return (EINVAL);
36065310Sarchie		linkNum = strtoul(cp, &eptr, 10);
36165310Sarchie		if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS)
36265310Sarchie			return (EINVAL);
36365310Sarchie		if (priv->links[linkNum] != NULL)
36465310Sarchie			return (EISCONN);
365184214Sdes		priv->links[linkNum] = malloc(sizeof(*priv->links[linkNum]),
366184214Sdes		    M_NETGRAPH_BRIDGE, M_NOWAIT|M_ZERO);
36765310Sarchie		if (priv->links[linkNum] == NULL)
36865310Sarchie			return (ENOMEM);
36965310Sarchie		priv->links[linkNum]->hook = hook;
37070784Sjulian		NG_HOOK_SET_PRIVATE(hook, (void *)linkNum);
37165310Sarchie		priv->numLinks++;
37265310Sarchie		return (0);
37365310Sarchie	}
37465310Sarchie
37565310Sarchie	/* Unknown hook name */
37665310Sarchie	return (EINVAL);
37765310Sarchie}
37865310Sarchie
37965310Sarchie/*
38065310Sarchie * Receive a control message
38165310Sarchie */
38265310Sarchiestatic int
38370700Sjulianng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
38465310Sarchie{
38570784Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
38665310Sarchie	struct ng_mesg *resp = NULL;
38765310Sarchie	int error = 0;
38870700Sjulian	struct ng_mesg *msg;
38965310Sarchie
39070700Sjulian	NGI_GET_MSG(item, msg);
39165310Sarchie	switch (msg->header.typecookie) {
39265310Sarchie	case NGM_BRIDGE_COOKIE:
39365310Sarchie		switch (msg->header.cmd) {
39465310Sarchie		case NGM_BRIDGE_GET_CONFIG:
39565310Sarchie		    {
39665310Sarchie			struct ng_bridge_config *conf;
39765310Sarchie
39865310Sarchie			NG_MKRESPONSE(resp, msg,
39965310Sarchie			    sizeof(struct ng_bridge_config), M_NOWAIT);
40065310Sarchie			if (resp == NULL) {
40165310Sarchie				error = ENOMEM;
40265310Sarchie				break;
40365310Sarchie			}
40465310Sarchie			conf = (struct ng_bridge_config *)resp->data;
40565310Sarchie			*conf = priv->conf;	/* no sanity checking needed */
40665310Sarchie			break;
40765310Sarchie		    }
40865310Sarchie		case NGM_BRIDGE_SET_CONFIG:
40965310Sarchie		    {
41065310Sarchie			struct ng_bridge_config *conf;
41165310Sarchie			int i;
41265310Sarchie
41365310Sarchie			if (msg->header.arglen
41465310Sarchie			    != sizeof(struct ng_bridge_config)) {
41565310Sarchie				error = EINVAL;
41665310Sarchie				break;
41765310Sarchie			}
41865310Sarchie			conf = (struct ng_bridge_config *)msg->data;
41965310Sarchie			priv->conf = *conf;
42065310Sarchie			for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++)
42165310Sarchie				priv->conf.ipfw[i] = !!priv->conf.ipfw[i];
42265310Sarchie			break;
42365310Sarchie		    }
42465310Sarchie		case NGM_BRIDGE_RESET:
42565310Sarchie		    {
42665310Sarchie			int i;
42765310Sarchie
42865310Sarchie			/* Flush all entries in the hash table */
42965310Sarchie			ng_bridge_remove_hosts(priv, -1);
43065310Sarchie
43165310Sarchie			/* Reset all loop detection counters and stats */
43265310Sarchie			for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) {
43365310Sarchie				if (priv->links[i] == NULL)
43465310Sarchie					continue;
43565310Sarchie				priv->links[i]->loopCount = 0;
43665310Sarchie				bzero(&priv->links[i]->stats,
43765310Sarchie				    sizeof(priv->links[i]->stats));
43865310Sarchie			}
43965310Sarchie			break;
44065310Sarchie		    }
44165310Sarchie		case NGM_BRIDGE_GET_STATS:
44265310Sarchie		case NGM_BRIDGE_CLR_STATS:
44365310Sarchie		case NGM_BRIDGE_GETCLR_STATS:
44465310Sarchie		    {
44565310Sarchie			struct ng_bridge_link *link;
44665310Sarchie			int linkNum;
44765310Sarchie
44865310Sarchie			/* Get link number */
44965310Sarchie			if (msg->header.arglen != sizeof(u_int32_t)) {
45065310Sarchie				error = EINVAL;
45165310Sarchie				break;
45265310Sarchie			}
45365310Sarchie			linkNum = *((u_int32_t *)msg->data);
45465310Sarchie			if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) {
45565310Sarchie				error = EINVAL;
45665310Sarchie				break;
45765310Sarchie			}
45865310Sarchie			if ((link = priv->links[linkNum]) == NULL) {
45965310Sarchie				error = ENOTCONN;
46065310Sarchie				break;
46165310Sarchie			}
46265310Sarchie
46365310Sarchie			/* Get/clear stats */
46465310Sarchie			if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
46565310Sarchie				NG_MKRESPONSE(resp, msg,
46665310Sarchie				    sizeof(link->stats), M_NOWAIT);
46765310Sarchie				if (resp == NULL) {
46865310Sarchie					error = ENOMEM;
46965310Sarchie					break;
47065310Sarchie				}
47165310Sarchie				bcopy(&link->stats,
47265310Sarchie				    resp->data, sizeof(link->stats));
47365310Sarchie			}
47465310Sarchie			if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
47565310Sarchie				bzero(&link->stats, sizeof(link->stats));
47665310Sarchie			break;
47765310Sarchie		    }
47865310Sarchie		case NGM_BRIDGE_GET_TABLE:
47965310Sarchie		    {
48065310Sarchie			struct ng_bridge_host_ary *ary;
48165310Sarchie			struct ng_bridge_hent *hent;
48265310Sarchie			int i = 0, bucket;
48365310Sarchie
48465310Sarchie			NG_MKRESPONSE(resp, msg, sizeof(*ary)
48565310Sarchie			    + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
48665310Sarchie			if (resp == NULL) {
48765310Sarchie				error = ENOMEM;
48865310Sarchie				break;
48965310Sarchie			}
49065310Sarchie			ary = (struct ng_bridge_host_ary *)resp->data;
49165310Sarchie			ary->numHosts = priv->numHosts;
49265310Sarchie			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
49365310Sarchie				SLIST_FOREACH(hent, &priv->tab[bucket], next)
49465310Sarchie					ary->hosts[i++] = hent->host;
49565310Sarchie			}
49665310Sarchie			break;
49765310Sarchie		    }
49865310Sarchie		default:
49965310Sarchie			error = EINVAL;
50065310Sarchie			break;
50165310Sarchie		}
50265310Sarchie		break;
50365310Sarchie	default:
50465310Sarchie		error = EINVAL;
50565310Sarchie		break;
50665310Sarchie	}
50765310Sarchie
50865310Sarchie	/* Done */
50970700Sjulian	NG_RESPOND_MSG(error, node, item, resp);
51070700Sjulian	NG_FREE_MSG(msg);
51165310Sarchie	return (error);
51265310Sarchie}
51365310Sarchie
51465310Sarchie/*
51565310Sarchie * Receive data on a hook
51665310Sarchie */
51765310Sarchiestatic int
51870700Sjulianng_bridge_rcvdata(hook_p hook, item_p item)
51965310Sarchie{
52070784Sjulian	const node_p node = NG_HOOK_NODE(hook);
52170784Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
52265310Sarchie	struct ng_bridge_host *host;
52365310Sarchie	struct ng_bridge_link *link;
52465310Sarchie	struct ether_header *eh;
525130931Sgreen	int error = 0, linkNum, linksSeen;
52670700Sjulian	int manycast;
52770700Sjulian	struct mbuf *m;
52870700Sjulian	struct ng_bridge_link *firstLink;
52965310Sarchie
53070700Sjulian	NGI_GET_M(item, m);
53165310Sarchie	/* Get link number */
532106665Sjhb	linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
53365310Sarchie	KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
53487599Sobrien	    ("%s: linkNum=%u", __func__, linkNum));
53565310Sarchie	link = priv->links[linkNum];
53687599Sobrien	KASSERT(link != NULL, ("%s: link%d null", __func__, linkNum));
53765310Sarchie
53865310Sarchie	/* Sanity check packet and pull up header */
53965310Sarchie	if (m->m_pkthdr.len < ETHER_HDR_LEN) {
54065310Sarchie		link->stats.recvRunts++;
54170700Sjulian		NG_FREE_ITEM(item);
54270700Sjulian		NG_FREE_M(m);
54365310Sarchie		return (EINVAL);
54465310Sarchie	}
54565310Sarchie	if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) {
54665310Sarchie		link->stats.memoryFailures++;
54770700Sjulian		NG_FREE_ITEM(item);
54865310Sarchie		return (ENOBUFS);
54965310Sarchie	}
55065310Sarchie	eh = mtod(m, struct ether_header *);
55165310Sarchie	if ((eh->ether_shost[0] & 1) != 0) {
55265310Sarchie		link->stats.recvInvalid++;
55370700Sjulian		NG_FREE_ITEM(item);
55470700Sjulian		NG_FREE_M(m);
55565310Sarchie		return (EINVAL);
55665310Sarchie	}
55765310Sarchie
55865310Sarchie	/* Is link disabled due to a loopback condition? */
55965310Sarchie	if (link->loopCount != 0) {
56065310Sarchie		link->stats.loopDrops++;
56170700Sjulian		NG_FREE_ITEM(item);
56270700Sjulian		NG_FREE_M(m);
56365310Sarchie		return (ELOOP);		/* XXX is this an appropriate error? */
56465310Sarchie	}
56565310Sarchie
56665310Sarchie	/* Update stats */
56765310Sarchie	link->stats.recvPackets++;
56865310Sarchie	link->stats.recvOctets += m->m_pkthdr.len;
56965310Sarchie	if ((manycast = (eh->ether_dhost[0] & 1)) != 0) {
57065310Sarchie		if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
57165310Sarchie			link->stats.recvBroadcasts++;
57265310Sarchie			manycast = 2;
57365310Sarchie		} else
57465310Sarchie			link->stats.recvMulticasts++;
57565310Sarchie	}
57665310Sarchie
57765310Sarchie	/* Look up packet's source Ethernet address in hashtable */
57865310Sarchie	if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
57965310Sarchie
58065310Sarchie		/* Update time since last heard from this host */
58165310Sarchie		host->staleness = 0;
58265310Sarchie
58365310Sarchie		/* Did host jump to a different link? */
58465310Sarchie		if (host->linkNum != linkNum) {
58565310Sarchie
58665310Sarchie			/*
58765310Sarchie			 * If the host's old link was recently established
58865310Sarchie			 * on the old link and it's already jumped to a new
58965310Sarchie			 * link, declare a loopback condition.
59065310Sarchie			 */
59165310Sarchie			if (host->age < priv->conf.minStableAge) {
59265310Sarchie
59365310Sarchie				/* Log the problem */
59465310Sarchie				if (priv->conf.debugLevel >= 2) {
59565310Sarchie					struct ifnet *ifp = m->m_pkthdr.rcvif;
59665310Sarchie					char suffix[32];
59765310Sarchie
59865310Sarchie					if (ifp != NULL)
59965310Sarchie						snprintf(suffix, sizeof(suffix),
600121816Sbrooks						    " (%s)", ifp->if_xname);
60165310Sarchie					else
60265310Sarchie						*suffix = '\0';
60365310Sarchie					log(LOG_WARNING, "ng_bridge: %s:"
60465310Sarchie					    " loopback detected on %s%s\n",
60565310Sarchie					    ng_bridge_nodename(node),
60670784Sjulian					    NG_HOOK_NAME(hook), suffix);
60765310Sarchie				}
60865310Sarchie
60965310Sarchie				/* Mark link as linka non grata */
61065310Sarchie				link->loopCount = priv->conf.loopTimeout;
61165310Sarchie				link->stats.loopDetects++;
61265310Sarchie
61365310Sarchie				/* Forget all hosts on this link */
61465310Sarchie				ng_bridge_remove_hosts(priv, linkNum);
61565310Sarchie
61665310Sarchie				/* Drop packet */
61765310Sarchie				link->stats.loopDrops++;
61870700Sjulian				NG_FREE_ITEM(item);
61970700Sjulian				NG_FREE_M(m);
62065310Sarchie				return (ELOOP);		/* XXX appropriate? */
62165310Sarchie			}
62265310Sarchie
62365310Sarchie			/* Move host over to new link */
62465310Sarchie			host->linkNum = linkNum;
62565310Sarchie			host->age = 0;
62665310Sarchie		}
62765310Sarchie	} else {
62865310Sarchie		if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) {
62965310Sarchie			link->stats.memoryFailures++;
63070700Sjulian			NG_FREE_ITEM(item);
63170700Sjulian			NG_FREE_M(m);
63265310Sarchie			return (ENOMEM);
63365310Sarchie		}
63465310Sarchie	}
63565310Sarchie
63665310Sarchie	/* Run packet through ipfw processing, if enabled */
637133920Sandre#if 0
638197952Sjulian	if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) {
63965310Sarchie		/* XXX not implemented yet */
64065310Sarchie	}
641133920Sandre#endif
64265310Sarchie
64365310Sarchie	/*
64465310Sarchie	 * If unicast and destination host known, deliver to host's link,
64565310Sarchie	 * unless it is the same link as the packet came in on.
64665310Sarchie	 */
64765310Sarchie	if (!manycast) {
64865310Sarchie
64965310Sarchie		/* Determine packet destination link */
65065310Sarchie		if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
65165310Sarchie			struct ng_bridge_link *const destLink
65265310Sarchie			    = priv->links[host->linkNum];
65365310Sarchie
65465310Sarchie			/* If destination same as incoming link, do nothing */
65565310Sarchie			KASSERT(destLink != NULL,
65687599Sobrien			    ("%s: link%d null", __func__, host->linkNum));
65765310Sarchie			if (destLink == link) {
65870700Sjulian				NG_FREE_ITEM(item);
65970700Sjulian				NG_FREE_M(m);
66065310Sarchie				return (0);
66165310Sarchie			}
66265310Sarchie
66365310Sarchie			/* Deliver packet out the destination link */
66465310Sarchie			destLink->stats.xmitPackets++;
66565310Sarchie			destLink->stats.xmitOctets += m->m_pkthdr.len;
66670700Sjulian			NG_FWD_NEW_DATA(error, item, destLink->hook, m);
66765310Sarchie			return (error);
66865310Sarchie		}
66965310Sarchie
67065310Sarchie		/* Destination host is not known */
67165310Sarchie		link->stats.recvUnknown++;
67265310Sarchie	}
67365310Sarchie
67465310Sarchie	/* Distribute unknown, multicast, broadcast pkts to all other links */
67570700Sjulian	firstLink = NULL;
676130931Sgreen	for (linkNum = linksSeen = 0; linksSeen <= priv->numLinks; linkNum++) {
67770700Sjulian		struct ng_bridge_link *destLink;
67870700Sjulian		struct mbuf *m2 = NULL;
67965310Sarchie
68070700Sjulian		/*
68170700Sjulian		 * If we have checked all the links then now
68270700Sjulian		 * send the original on its reserved link
68370700Sjulian		 */
684130931Sgreen		if (linksSeen == priv->numLinks) {
68570700Sjulian			/* If we never saw a good link, leave. */
68670700Sjulian			if (firstLink == NULL) {
68770700Sjulian				NG_FREE_ITEM(item);
68870700Sjulian				NG_FREE_M(m);
68970700Sjulian				return (0);
69070700Sjulian			}
69170700Sjulian			destLink = firstLink;
69270700Sjulian		} else {
69370700Sjulian			destLink = priv->links[linkNum];
694130931Sgreen			if (destLink != NULL)
695130931Sgreen				linksSeen++;
69670700Sjulian			/* Skip incoming link and disconnected links */
69770700Sjulian			if (destLink == NULL || destLink == link) {
69870700Sjulian				continue;
69970700Sjulian			}
70070700Sjulian			if (firstLink == NULL) {
70170700Sjulian				/*
70270700Sjulian				 * This is the first usable link we have found.
70370700Sjulian				 * Reserve it for the originals.
70470700Sjulian				 * If we never find another we save a copy.
70570700Sjulian				 */
70670700Sjulian				firstLink = destLink;
70770700Sjulian				continue;
70870700Sjulian			}
70965310Sarchie
71070700Sjulian			/*
71170700Sjulian			 * It's usable link but not the reserved (first) one.
712131155Sjulian			 * Copy mbuf info for sending.
71370700Sjulian			 */
714111119Simp			m2 = m_dup(m, M_DONTWAIT);	/* XXX m_copypacket() */
71565310Sarchie			if (m2 == NULL) {
71665310Sarchie				link->stats.memoryFailures++;
71770700Sjulian				NG_FREE_ITEM(item);
71870700Sjulian				NG_FREE_M(m);
71965310Sarchie				return (ENOBUFS);
72065310Sarchie			}
72165310Sarchie		}
72265310Sarchie
72365310Sarchie		/* Update stats */
72465310Sarchie		destLink->stats.xmitPackets++;
72565310Sarchie		destLink->stats.xmitOctets += m->m_pkthdr.len;
72665310Sarchie		switch (manycast) {
72765310Sarchie		case 0:					/* unicast */
72865310Sarchie			break;
72965310Sarchie		case 1:					/* multicast */
73065310Sarchie			destLink->stats.xmitMulticasts++;
73165310Sarchie			break;
73265310Sarchie		case 2:					/* broadcast */
73365310Sarchie			destLink->stats.xmitBroadcasts++;
73465310Sarchie			break;
73565310Sarchie		}
73665310Sarchie
73765310Sarchie		/* Send packet */
73870700Sjulian		if (destLink == firstLink) {
73970700Sjulian			/*
74070700Sjulian			 * If we've sent all the others, send the original
74170700Sjulian			 * on the first link we found.
74270700Sjulian			 */
74370700Sjulian			NG_FWD_NEW_DATA(error, item, destLink->hook, m);
74470700Sjulian			break; /* always done last - not really needed. */
74570700Sjulian		} else {
746131155Sjulian			NG_SEND_DATA_ONLY(error, destLink->hook, m2);
74770700Sjulian		}
74865310Sarchie	}
74965310Sarchie	return (error);
75065310Sarchie}
75165310Sarchie
75265310Sarchie/*
75365310Sarchie * Shutdown node
75465310Sarchie */
75565310Sarchiestatic int
75670700Sjulianng_bridge_shutdown(node_p node)
75765310Sarchie{
75870784Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
75965310Sarchie
76087997Sarchie	/*
761141574Sru	 * Shut down everything including the timer.  Even if the
762141574Sru	 * callout has already been dequeued and is about to be
763141574Sru	 * run, ng_bridge_timeout() won't be fired as the node
764141574Sru	 * is already marked NGF_INVALID, so we're safe to free
765141574Sru	 * the node now.
76687997Sarchie	 */
76765310Sarchie	KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
76865310Sarchie	    ("%s: numLinks=%d numHosts=%d",
76987599Sobrien	    __func__, priv->numLinks, priv->numHosts));
770141574Sru	ng_uncallout(&priv->timer, node);
771141574Sru	NG_NODE_SET_PRIVATE(node, NULL);
772141574Sru	NG_NODE_UNREF(node);
773184205Sdes	free(priv->tab, M_NETGRAPH_BRIDGE);
774184205Sdes	free(priv, M_NETGRAPH_BRIDGE);
77565310Sarchie	return (0);
77665310Sarchie}
77765310Sarchie
77865310Sarchie/*
77965310Sarchie * Hook disconnection.
78065310Sarchie */
78165310Sarchiestatic int
78265310Sarchieng_bridge_disconnect(hook_p hook)
78365310Sarchie{
78470784Sjulian	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
78565310Sarchie	int linkNum;
78665310Sarchie
78765310Sarchie	/* Get link number */
788106665Sjhb	linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
78965310Sarchie	KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
79087599Sobrien	    ("%s: linkNum=%u", __func__, linkNum));
79165310Sarchie
79265310Sarchie	/* Remove all hosts associated with this link */
79365310Sarchie	ng_bridge_remove_hosts(priv, linkNum);
79465310Sarchie
79565310Sarchie	/* Free associated link information */
79687599Sobrien	KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __func__));
797184205Sdes	free(priv->links[linkNum], M_NETGRAPH_BRIDGE);
79865310Sarchie	priv->links[linkNum] = NULL;
79965310Sarchie	priv->numLinks--;
80065310Sarchie
80165310Sarchie	/* If no more hooks, go away */
80270784Sjulian	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
80370784Sjulian	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
80470784Sjulian		ng_rmnode_self(NG_HOOK_NODE(hook));
80570784Sjulian	}
80665310Sarchie	return (0);
80765310Sarchie}
80865310Sarchie
80965310Sarchie/******************************************************************
81065310Sarchie		    HASH TABLE FUNCTIONS
81165310Sarchie******************************************************************/
81265310Sarchie
81365310Sarchie/*
81465310Sarchie * Hash algorithm
81565310Sarchie */
81665310Sarchie#define HASH(addr,mask)		( (((const u_int16_t *)(addr))[0] 	\
81765310Sarchie				 ^ ((const u_int16_t *)(addr))[1] 	\
81865310Sarchie				 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
81965310Sarchie
82065310Sarchie/*
82165310Sarchie * Find a host entry in the table.
82265310Sarchie */
82365310Sarchiestatic struct ng_bridge_host *
82465310Sarchieng_bridge_get(priv_p priv, const u_char *addr)
82565310Sarchie{
82665310Sarchie	const int bucket = HASH(addr, priv->hashMask);
82765310Sarchie	struct ng_bridge_hent *hent;
82865310Sarchie
82965310Sarchie	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
83065310Sarchie		if (ETHER_EQUAL(hent->host.addr, addr))
83165310Sarchie			return (&hent->host);
83265310Sarchie	}
83365310Sarchie	return (NULL);
83465310Sarchie}
83565310Sarchie
83665310Sarchie/*
83765310Sarchie * Add a new host entry to the table. This assumes the host doesn't
83865310Sarchie * already exist in the table. Returns 1 on success, 0 if there
83965310Sarchie * was a memory allocation failure.
84065310Sarchie */
84165310Sarchiestatic int
84265310Sarchieng_bridge_put(priv_p priv, const u_char *addr, int linkNum)
84365310Sarchie{
84465310Sarchie	const int bucket = HASH(addr, priv->hashMask);
84565310Sarchie	struct ng_bridge_hent *hent;
84665310Sarchie
84765310Sarchie#ifdef INVARIANTS
84865310Sarchie	/* Assert that entry does not already exist in hashtable */
84965310Sarchie	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
85065310Sarchie		KASSERT(!ETHER_EQUAL(hent->host.addr, addr),
85187599Sobrien		    ("%s: entry %6D exists in table", __func__, addr, ":"));
85265310Sarchie	}
85365310Sarchie#endif
85465310Sarchie
85565310Sarchie	/* Allocate and initialize new hashtable entry */
856184214Sdes	hent = malloc(sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT);
85765310Sarchie	if (hent == NULL)
85865310Sarchie		return (0);
85965310Sarchie	bcopy(addr, hent->host.addr, ETHER_ADDR_LEN);
86065310Sarchie	hent->host.linkNum = linkNum;
86165310Sarchie	hent->host.staleness = 0;
86265310Sarchie	hent->host.age = 0;
86365310Sarchie
86465310Sarchie	/* Add new element to hash bucket */
86565310Sarchie	SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next);
86665310Sarchie	priv->numHosts++;
86765310Sarchie
86865310Sarchie	/* Resize table if necessary */
86965310Sarchie	ng_bridge_rehash(priv);
87065310Sarchie	return (1);
87165310Sarchie}
87265310Sarchie
87365310Sarchie/*
87465310Sarchie * Resize the hash table. We try to maintain the number of buckets
87565310Sarchie * such that the load factor is in the range 0.25 to 1.0.
87665310Sarchie *
87765310Sarchie * If we can't get the new memory then we silently fail. This is OK
87865310Sarchie * because things will still work and we'll try again soon anyway.
87965310Sarchie */
88065310Sarchiestatic void
88165310Sarchieng_bridge_rehash(priv_p priv)
88265310Sarchie{
88365310Sarchie	struct ng_bridge_bucket *newTab;
88465310Sarchie	int oldBucket, newBucket;
88565310Sarchie	int newNumBuckets;
88665310Sarchie	u_int newMask;
88765310Sarchie
88865310Sarchie	/* Is table too full or too empty? */
88965310Sarchie	if (priv->numHosts > priv->numBuckets
89065310Sarchie	    && (priv->numBuckets << 1) <= MAX_BUCKETS)
89165310Sarchie		newNumBuckets = priv->numBuckets << 1;
89265310Sarchie	else if (priv->numHosts < (priv->numBuckets >> 2)
89365310Sarchie	    && (priv->numBuckets >> 2) >= MIN_BUCKETS)
89465310Sarchie		newNumBuckets = priv->numBuckets >> 2;
89565310Sarchie	else
89665310Sarchie		return;
89765310Sarchie	newMask = newNumBuckets - 1;
89865310Sarchie
89965310Sarchie	/* Allocate and initialize new table */
900184214Sdes	newTab = malloc(newNumBuckets * sizeof(*newTab),
901184214Sdes	    M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
90265310Sarchie	if (newTab == NULL)
90365310Sarchie		return;
90465310Sarchie
90565310Sarchie	/* Move all entries from old table to new table */
90665310Sarchie	for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
90765310Sarchie		struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
90865310Sarchie
90965310Sarchie		while (!SLIST_EMPTY(oldList)) {
91065310Sarchie			struct ng_bridge_hent *const hent
91165310Sarchie			    = SLIST_FIRST(oldList);
91265310Sarchie
91365310Sarchie			SLIST_REMOVE_HEAD(oldList, next);
91465310Sarchie			newBucket = HASH(hent->host.addr, newMask);
91565310Sarchie			SLIST_INSERT_HEAD(&newTab[newBucket], hent, next);
91665310Sarchie		}
91765310Sarchie	}
91865310Sarchie
91965310Sarchie	/* Replace old table with new one */
92065310Sarchie	if (priv->conf.debugLevel >= 3) {
92165310Sarchie		log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
92265310Sarchie		    ng_bridge_nodename(priv->node),
92365310Sarchie		    priv->numBuckets, newNumBuckets);
92465310Sarchie	}
925184205Sdes	free(priv->tab, M_NETGRAPH_BRIDGE);
92665310Sarchie	priv->numBuckets = newNumBuckets;
92765310Sarchie	priv->hashMask = newMask;
92865310Sarchie	priv->tab = newTab;
92965310Sarchie	return;
93065310Sarchie}
93165310Sarchie
93265310Sarchie/******************************************************************
93365310Sarchie		    MISC FUNCTIONS
93465310Sarchie******************************************************************/
93565310Sarchie
93665310Sarchie/*
93765310Sarchie * Remove all hosts associated with a specific link from the hashtable.
93865310Sarchie * If linkNum == -1, then remove all hosts in the table.
93965310Sarchie */
94065310Sarchiestatic void
94165310Sarchieng_bridge_remove_hosts(priv_p priv, int linkNum)
94265310Sarchie{
94365310Sarchie	int bucket;
94465310Sarchie
94565310Sarchie	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
94665310Sarchie		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
94765310Sarchie
94865310Sarchie		while (*hptr != NULL) {
94965310Sarchie			struct ng_bridge_hent *const hent = *hptr;
95065310Sarchie
95165310Sarchie			if (linkNum == -1 || hent->host.linkNum == linkNum) {
95265310Sarchie				*hptr = SLIST_NEXT(hent, next);
953184205Sdes				free(hent, M_NETGRAPH_BRIDGE);
95465310Sarchie				priv->numHosts--;
95565310Sarchie			} else
95665310Sarchie				hptr = &SLIST_NEXT(hent, next);
95765310Sarchie		}
95865310Sarchie	}
95965310Sarchie}
96065310Sarchie
96165310Sarchie/*
96265310Sarchie * Handle our once-per-second timeout event. We do two things:
96365310Sarchie * we decrement link->loopCount for those links being muted due to
96465310Sarchie * a detected loopback condition, and we remove any hosts from
96565310Sarchie * the hashtable whom we haven't heard from in a long while.
96665310Sarchie */
96765310Sarchiestatic void
968138834Sglebiusng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2)
96965310Sarchie{
97070784Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
971138834Sglebius	int bucket;
97265310Sarchie	int counter = 0;
97365310Sarchie	int linkNum;
97465310Sarchie
97565310Sarchie	/* Update host time counters and remove stale entries */
97665310Sarchie	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
97765310Sarchie		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
97865310Sarchie
97965310Sarchie		while (*hptr != NULL) {
98065310Sarchie			struct ng_bridge_hent *const hent = *hptr;
98165310Sarchie
98265310Sarchie			/* Make sure host's link really exists */
98365310Sarchie			KASSERT(priv->links[hent->host.linkNum] != NULL,
98465310Sarchie			    ("%s: host %6D on nonexistent link %d\n",
98587599Sobrien			    __func__, hent->host.addr, ":",
98665310Sarchie			    hent->host.linkNum));
98765310Sarchie
98865310Sarchie			/* Remove hosts we haven't heard from in a while */
98965310Sarchie			if (++hent->host.staleness >= priv->conf.maxStaleness) {
99065310Sarchie				*hptr = SLIST_NEXT(hent, next);
991184205Sdes				free(hent, M_NETGRAPH_BRIDGE);
99265310Sarchie				priv->numHosts--;
99365310Sarchie			} else {
99465310Sarchie				if (hent->host.age < 0xffff)
99565310Sarchie					hent->host.age++;
99665310Sarchie				hptr = &SLIST_NEXT(hent, next);
99765310Sarchie				counter++;
99865310Sarchie			}
99965310Sarchie		}
100065310Sarchie	}
100165310Sarchie	KASSERT(priv->numHosts == counter,
100287599Sobrien	    ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
100365310Sarchie
100465310Sarchie	/* Decrease table size if necessary */
100565310Sarchie	ng_bridge_rehash(priv);
100665310Sarchie
100765310Sarchie	/* Decrease loop counter on muted looped back links */
100865310Sarchie	for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) {
100965310Sarchie		struct ng_bridge_link *const link = priv->links[linkNum];
101065310Sarchie
101165310Sarchie		if (link != NULL) {
101265310Sarchie			if (link->loopCount != 0) {
101365310Sarchie				link->loopCount--;
101465310Sarchie				if (link->loopCount == 0
101565310Sarchie				    && priv->conf.debugLevel >= 2) {
101665310Sarchie					log(LOG_INFO, "ng_bridge: %s:"
101765310Sarchie					    " restoring looped back link%d\n",
101865310Sarchie					    ng_bridge_nodename(node), linkNum);
101965310Sarchie				}
102065310Sarchie			}
102165310Sarchie			counter++;
102265310Sarchie		}
102365310Sarchie	}
102465310Sarchie	KASSERT(priv->numLinks == counter,
102587599Sobrien	    ("%s: links: %d != %d", __func__, priv->numLinks, counter));
102665310Sarchie
1027138834Sglebius	/* Register a new timeout, keeping the existing node reference */
1028138834Sglebius	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
102965310Sarchie}
103065310Sarchie
103165310Sarchie/*
103265310Sarchie * Return node's "name", even if it doesn't have one.
103365310Sarchie */
103465310Sarchiestatic const char *
103565310Sarchieng_bridge_nodename(node_p node)
103665310Sarchie{
1037125028Sharti	static char name[NG_NODESIZ];
103865310Sarchie
103970784Sjulian	if (NG_NODE_NAME(node) != NULL)
104070784Sjulian		snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
104165310Sarchie	else
104265310Sarchie		snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
104365310Sarchie	return name;
104465310Sarchie}
104565310Sarchie
1046