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$
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
87227293Sedstatic MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge",
88227293Sed    "netgraph bridge node");
8970870Sjulian#else
9070870Sjulian#define M_NETGRAPH_BRIDGE M_NETGRAPH
9170870Sjulian#endif
9270870Sjulian
9365310Sarchie/* Per-link private data */
9465310Sarchiestruct ng_bridge_link {
9565310Sarchie	hook_p				hook;		/* netgraph hook */
9665310Sarchie	u_int16_t			loopCount;	/* loop ignore timer */
9765310Sarchie	struct ng_bridge_link_stats	stats;		/* link stats */
9865310Sarchie};
9965310Sarchie
10065310Sarchie/* Per-node private data */
10165310Sarchiestruct ng_bridge_private {
10265310Sarchie	struct ng_bridge_bucket	*tab;		/* hash table bucket array */
10365310Sarchie	struct ng_bridge_link	*links[NG_BRIDGE_MAX_LINKS];
10465310Sarchie	struct ng_bridge_config	conf;		/* node configuration */
10565310Sarchie	node_p			node;		/* netgraph node */
10665310Sarchie	u_int			numHosts;	/* num entries in table */
10765310Sarchie	u_int			numBuckets;	/* num buckets in table */
10865310Sarchie	u_int			hashMask;	/* numBuckets - 1 */
10965310Sarchie	int			numLinks;	/* num connected links */
110207680Szec	int			persistent;	/* can exist w/o hooks */
11165310Sarchie	struct callout		timer;		/* one second periodic timer */
11265310Sarchie};
11365310Sarchietypedef struct ng_bridge_private *priv_p;
11465310Sarchie
11565310Sarchie/* Information about a host, stored in a hash table entry */
11665310Sarchiestruct ng_bridge_hent {
11765310Sarchie	struct ng_bridge_host		host;	/* actual host info */
11865310Sarchie	SLIST_ENTRY(ng_bridge_hent)	next;	/* next entry in bucket */
11965310Sarchie};
12065310Sarchie
12165310Sarchie/* Hash table bucket declaration */
12265310SarchieSLIST_HEAD(ng_bridge_bucket, ng_bridge_hent);
12365310Sarchie
12465310Sarchie/* Netgraph node methods */
12565310Sarchiestatic ng_constructor_t	ng_bridge_constructor;
12665310Sarchiestatic ng_rcvmsg_t	ng_bridge_rcvmsg;
12770700Sjulianstatic ng_shutdown_t	ng_bridge_shutdown;
12865310Sarchiestatic ng_newhook_t	ng_bridge_newhook;
12965310Sarchiestatic ng_rcvdata_t	ng_bridge_rcvdata;
13065310Sarchiestatic ng_disconnect_t	ng_bridge_disconnect;
13165310Sarchie
13265310Sarchie/* Other internal functions */
13365310Sarchiestatic struct	ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr);
13465310Sarchiestatic int	ng_bridge_put(priv_p priv, const u_char *addr, int linkNum);
13565310Sarchiestatic void	ng_bridge_rehash(priv_p priv);
13665310Sarchiestatic void	ng_bridge_remove_hosts(priv_p priv, int linkNum);
137138834Sglebiusstatic void	ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2);
13865310Sarchiestatic const	char *ng_bridge_nodename(node_p node);
13965310Sarchie
14065310Sarchie/* Ethernet broadcast */
14165310Sarchiestatic const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
14265310Sarchie    { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
14365310Sarchie
14465310Sarchie/* Store each hook's link number in the private field */
14565310Sarchie#define LINK_NUM(hook)		(*(u_int16_t *)(&(hook)->private))
14665310Sarchie
14765310Sarchie/* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
14865310Sarchie#define ETHER_EQUAL(a,b)	(((const u_int32_t *)(a))[0] \
14965310Sarchie					== ((const u_int32_t *)(b))[0] \
15065310Sarchie				    && ((const u_int16_t *)(a))[2] \
15165310Sarchie					== ((const u_int16_t *)(b))[2])
15265310Sarchie
15365310Sarchie/* Minimum and maximum number of hash buckets. Must be a power of two. */
15465310Sarchie#define MIN_BUCKETS		(1 << 5)	/* 32 */
15565310Sarchie#define MAX_BUCKETS		(1 << 14)	/* 16384 */
15665310Sarchie
15765310Sarchie/* Configuration default values */
15865310Sarchie#define DEFAULT_LOOP_TIMEOUT	60
15965310Sarchie#define DEFAULT_MAX_STALENESS	(15 * 60)	/* same as ARP timeout */
16065310Sarchie#define DEFAULT_MIN_STABLE_AGE	1
16165310Sarchie
16265310Sarchie/******************************************************************
16365310Sarchie		    NETGRAPH PARSE TYPES
16465310Sarchie******************************************************************/
16565310Sarchie
16665310Sarchie/*
16765310Sarchie * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
16865310Sarchie */
16965310Sarchiestatic int
17065310Sarchieng_bridge_getTableLength(const struct ng_parse_type *type,
17165310Sarchie	const u_char *start, const u_char *buf)
17265310Sarchie{
17365310Sarchie	const struct ng_bridge_host_ary *const hary
17465310Sarchie	    = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
17565310Sarchie
17665310Sarchie	return hary->numHosts;
17765310Sarchie}
17865310Sarchie
17965310Sarchie/* Parse type for struct ng_bridge_host_ary */
18097685Sarchiestatic const struct ng_parse_struct_field ng_bridge_host_type_fields[]
181123600Sru	= NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
18265310Sarchiestatic const struct ng_parse_type ng_bridge_host_type = {
18365310Sarchie	&ng_parse_struct_type,
18497685Sarchie	&ng_bridge_host_type_fields
18565310Sarchie};
18665310Sarchiestatic const struct ng_parse_array_info ng_bridge_hary_type_info = {
18765310Sarchie	&ng_bridge_host_type,
18865310Sarchie	ng_bridge_getTableLength
18965310Sarchie};
19065310Sarchiestatic const struct ng_parse_type ng_bridge_hary_type = {
19165310Sarchie	&ng_parse_array_type,
19265310Sarchie	&ng_bridge_hary_type_info
19365310Sarchie};
19497685Sarchiestatic const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[]
19565310Sarchie	= NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
19665310Sarchiestatic const struct ng_parse_type ng_bridge_host_ary_type = {
19765310Sarchie	&ng_parse_struct_type,
19897685Sarchie	&ng_bridge_host_ary_type_fields
19965310Sarchie};
20065310Sarchie
20165310Sarchie/* Parse type for struct ng_bridge_config */
20265310Sarchiestatic const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = {
20365310Sarchie	&ng_parse_uint8_type,
20465310Sarchie	NG_BRIDGE_MAX_LINKS
20565310Sarchie};
20665310Sarchiestatic const struct ng_parse_type ng_bridge_ipfwary_type = {
20765310Sarchie	&ng_parse_fixedarray_type,
20865310Sarchie	&ng_bridge_ipfwary_type_info
20965310Sarchie};
21097685Sarchiestatic const struct ng_parse_struct_field ng_bridge_config_type_fields[]
21165310Sarchie	= NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type);
21265310Sarchiestatic const struct ng_parse_type ng_bridge_config_type = {
21365310Sarchie	&ng_parse_struct_type,
21497685Sarchie	&ng_bridge_config_type_fields
21565310Sarchie};
21665310Sarchie
21765310Sarchie/* Parse type for struct ng_bridge_link_stat */
21897685Sarchiestatic const struct ng_parse_struct_field ng_bridge_stats_type_fields[]
21997685Sarchie	= NG_BRIDGE_STATS_TYPE_INFO;
22065310Sarchiestatic const struct ng_parse_type ng_bridge_stats_type = {
22165310Sarchie	&ng_parse_struct_type,
22297685Sarchie	&ng_bridge_stats_type_fields
22365310Sarchie};
22465310Sarchie
22565310Sarchie/* List of commands and how to convert arguments to/from ASCII */
22665310Sarchiestatic const struct ng_cmdlist ng_bridge_cmdlist[] = {
22765310Sarchie	{
22865310Sarchie	  NGM_BRIDGE_COOKIE,
22965310Sarchie	  NGM_BRIDGE_SET_CONFIG,
23065310Sarchie	  "setconfig",
23165310Sarchie	  &ng_bridge_config_type,
23265310Sarchie	  NULL
23365310Sarchie	},
23465310Sarchie	{
23565310Sarchie	  NGM_BRIDGE_COOKIE,
23665310Sarchie	  NGM_BRIDGE_GET_CONFIG,
23765310Sarchie	  "getconfig",
23865310Sarchie	  NULL,
23965310Sarchie	  &ng_bridge_config_type
24065310Sarchie	},
24165310Sarchie	{
24265310Sarchie	  NGM_BRIDGE_COOKIE,
24365310Sarchie	  NGM_BRIDGE_RESET,
24465310Sarchie	  "reset",
24565310Sarchie	  NULL,
24665310Sarchie	  NULL
24765310Sarchie	},
24865310Sarchie	{
24965310Sarchie	  NGM_BRIDGE_COOKIE,
25065310Sarchie	  NGM_BRIDGE_GET_STATS,
25165310Sarchie	  "getstats",
25265310Sarchie	  &ng_parse_uint32_type,
25365310Sarchie	  &ng_bridge_stats_type
25465310Sarchie	},
25565310Sarchie	{
25665310Sarchie	  NGM_BRIDGE_COOKIE,
25765310Sarchie	  NGM_BRIDGE_CLR_STATS,
25865310Sarchie	  "clrstats",
25965310Sarchie	  &ng_parse_uint32_type,
26065310Sarchie	  NULL
26165310Sarchie	},
26265310Sarchie	{
26365310Sarchie	  NGM_BRIDGE_COOKIE,
26465310Sarchie	  NGM_BRIDGE_GETCLR_STATS,
26565310Sarchie	  "getclrstats",
26665310Sarchie	  &ng_parse_uint32_type,
26765310Sarchie	  &ng_bridge_stats_type
26865310Sarchie	},
26965310Sarchie	{
27065310Sarchie	  NGM_BRIDGE_COOKIE,
27165310Sarchie	  NGM_BRIDGE_GET_TABLE,
27265310Sarchie	  "gettable",
27365310Sarchie	  NULL,
27465310Sarchie	  &ng_bridge_host_ary_type
27565310Sarchie	},
276207680Szec	{
277207680Szec	  NGM_BRIDGE_COOKIE,
278207680Szec	  NGM_BRIDGE_SET_PERSISTENT,
279207680Szec	  "setpersistent",
280207680Szec	  NULL,
281207680Szec	  NULL
282207680Szec	},
28365310Sarchie	{ 0 }
28465310Sarchie};
28565310Sarchie
28665310Sarchie/* Node type descriptor */
28765310Sarchiestatic struct ng_type ng_bridge_typestruct = {
288129823Sjulian	.version =	NG_ABI_VERSION,
289129823Sjulian	.name =		NG_BRIDGE_NODE_TYPE,
290129823Sjulian	.constructor =	ng_bridge_constructor,
291129823Sjulian	.rcvmsg =	ng_bridge_rcvmsg,
292129823Sjulian	.shutdown =	ng_bridge_shutdown,
293129823Sjulian	.newhook =	ng_bridge_newhook,
294129823Sjulian	.rcvdata =	ng_bridge_rcvdata,
295129823Sjulian	.disconnect =	ng_bridge_disconnect,
296129823Sjulian	.cmdlist =	ng_bridge_cmdlist,
29765310Sarchie};
29866887SarchieNETGRAPH_INIT(bridge, &ng_bridge_typestruct);
29965310Sarchie
30065310Sarchie/******************************************************************
30165310Sarchie		    NETGRAPH NODE METHODS
30265310Sarchie******************************************************************/
30365310Sarchie
30465310Sarchie/*
30565310Sarchie * Node constructor
30665310Sarchie */
30765310Sarchiestatic int
30870700Sjulianng_bridge_constructor(node_p node)
30965310Sarchie{
31065310Sarchie	priv_p priv;
31165310Sarchie
31265310Sarchie	/* Allocate and initialize private info */
313220768Sglebius	priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
314138834Sglebius	ng_callout_init(&priv->timer);
31565310Sarchie
31665310Sarchie	/* Allocate and initialize hash table, etc. */
317184214Sdes	priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab),
318220768Sglebius	    M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
31965310Sarchie	priv->numBuckets = MIN_BUCKETS;
32065310Sarchie	priv->hashMask = MIN_BUCKETS - 1;
32165310Sarchie	priv->conf.debugLevel = 1;
32265310Sarchie	priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
32365310Sarchie	priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
32465310Sarchie	priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
32565310Sarchie
32670700Sjulian	/*
32770700Sjulian	 * This node has all kinds of stuff that could be screwed by SMP.
32870700Sjulian	 * Until it gets it's own internal protection, we go through in
32970700Sjulian	 * single file. This could hurt a machine bridging beteen two
33070700Sjulian	 * GB ethernets so it should be fixed.
33170700Sjulian	 * When it's fixed the process SHOULD NOT SLEEP, spinlocks please!
33270700Sjulian	 * (and atomic ops )
33370700Sjulian	 */
33470784Sjulian	NG_NODE_FORCE_WRITER(node);
33570784Sjulian	NG_NODE_SET_PRIVATE(node, priv);
33670700Sjulian	priv->node = node;
33765310Sarchie
33887997Sarchie	/* Start timer; timer is always running while node is alive */
339138834Sglebius	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
34087997Sarchie
34187997Sarchie	/* Done */
34265310Sarchie	return (0);
34365310Sarchie}
34465310Sarchie
34565310Sarchie/*
34665310Sarchie * Method for attaching a new hook
34765310Sarchie */
34865310Sarchiestatic	int
34965310Sarchieng_bridge_newhook(node_p node, hook_p hook, const char *name)
35065310Sarchie{
35170784Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
35265310Sarchie
35365310Sarchie	/* Check for a link hook */
35465310Sarchie	if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX,
35565310Sarchie	    strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) {
35665310Sarchie		const char *cp;
35765310Sarchie		char *eptr;
35865310Sarchie		u_long linkNum;
35965310Sarchie
36065310Sarchie		cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX);
36165310Sarchie		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
36265310Sarchie			return (EINVAL);
36365310Sarchie		linkNum = strtoul(cp, &eptr, 10);
36465310Sarchie		if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS)
36565310Sarchie			return (EINVAL);
36665310Sarchie		if (priv->links[linkNum] != NULL)
36765310Sarchie			return (EISCONN);
368184214Sdes		priv->links[linkNum] = malloc(sizeof(*priv->links[linkNum]),
369184214Sdes		    M_NETGRAPH_BRIDGE, M_NOWAIT|M_ZERO);
37065310Sarchie		if (priv->links[linkNum] == NULL)
37165310Sarchie			return (ENOMEM);
37265310Sarchie		priv->links[linkNum]->hook = hook;
37370784Sjulian		NG_HOOK_SET_PRIVATE(hook, (void *)linkNum);
37465310Sarchie		priv->numLinks++;
37565310Sarchie		return (0);
37665310Sarchie	}
37765310Sarchie
37865310Sarchie	/* Unknown hook name */
37965310Sarchie	return (EINVAL);
38065310Sarchie}
38165310Sarchie
38265310Sarchie/*
38365310Sarchie * Receive a control message
38465310Sarchie */
38565310Sarchiestatic int
38670700Sjulianng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
38765310Sarchie{
38870784Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
38965310Sarchie	struct ng_mesg *resp = NULL;
39065310Sarchie	int error = 0;
39170700Sjulian	struct ng_mesg *msg;
39265310Sarchie
39370700Sjulian	NGI_GET_MSG(item, msg);
39465310Sarchie	switch (msg->header.typecookie) {
39565310Sarchie	case NGM_BRIDGE_COOKIE:
39665310Sarchie		switch (msg->header.cmd) {
39765310Sarchie		case NGM_BRIDGE_GET_CONFIG:
39865310Sarchie		    {
39965310Sarchie			struct ng_bridge_config *conf;
40065310Sarchie
40165310Sarchie			NG_MKRESPONSE(resp, msg,
40265310Sarchie			    sizeof(struct ng_bridge_config), M_NOWAIT);
40365310Sarchie			if (resp == NULL) {
40465310Sarchie				error = ENOMEM;
40565310Sarchie				break;
40665310Sarchie			}
40765310Sarchie			conf = (struct ng_bridge_config *)resp->data;
40865310Sarchie			*conf = priv->conf;	/* no sanity checking needed */
40965310Sarchie			break;
41065310Sarchie		    }
41165310Sarchie		case NGM_BRIDGE_SET_CONFIG:
41265310Sarchie		    {
41365310Sarchie			struct ng_bridge_config *conf;
41465310Sarchie			int i;
41565310Sarchie
41665310Sarchie			if (msg->header.arglen
41765310Sarchie			    != sizeof(struct ng_bridge_config)) {
41865310Sarchie				error = EINVAL;
41965310Sarchie				break;
42065310Sarchie			}
42165310Sarchie			conf = (struct ng_bridge_config *)msg->data;
42265310Sarchie			priv->conf = *conf;
42365310Sarchie			for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++)
42465310Sarchie				priv->conf.ipfw[i] = !!priv->conf.ipfw[i];
42565310Sarchie			break;
42665310Sarchie		    }
42765310Sarchie		case NGM_BRIDGE_RESET:
42865310Sarchie		    {
42965310Sarchie			int i;
43065310Sarchie
43165310Sarchie			/* Flush all entries in the hash table */
43265310Sarchie			ng_bridge_remove_hosts(priv, -1);
43365310Sarchie
43465310Sarchie			/* Reset all loop detection counters and stats */
43565310Sarchie			for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) {
43665310Sarchie				if (priv->links[i] == NULL)
43765310Sarchie					continue;
43865310Sarchie				priv->links[i]->loopCount = 0;
43965310Sarchie				bzero(&priv->links[i]->stats,
44065310Sarchie				    sizeof(priv->links[i]->stats));
44165310Sarchie			}
44265310Sarchie			break;
44365310Sarchie		    }
44465310Sarchie		case NGM_BRIDGE_GET_STATS:
44565310Sarchie		case NGM_BRIDGE_CLR_STATS:
44665310Sarchie		case NGM_BRIDGE_GETCLR_STATS:
44765310Sarchie		    {
44865310Sarchie			struct ng_bridge_link *link;
44965310Sarchie			int linkNum;
45065310Sarchie
45165310Sarchie			/* Get link number */
45265310Sarchie			if (msg->header.arglen != sizeof(u_int32_t)) {
45365310Sarchie				error = EINVAL;
45465310Sarchie				break;
45565310Sarchie			}
45665310Sarchie			linkNum = *((u_int32_t *)msg->data);
45765310Sarchie			if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) {
45865310Sarchie				error = EINVAL;
45965310Sarchie				break;
46065310Sarchie			}
46165310Sarchie			if ((link = priv->links[linkNum]) == NULL) {
46265310Sarchie				error = ENOTCONN;
46365310Sarchie				break;
46465310Sarchie			}
46565310Sarchie
46665310Sarchie			/* Get/clear stats */
46765310Sarchie			if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
46865310Sarchie				NG_MKRESPONSE(resp, msg,
46965310Sarchie				    sizeof(link->stats), M_NOWAIT);
47065310Sarchie				if (resp == NULL) {
47165310Sarchie					error = ENOMEM;
47265310Sarchie					break;
47365310Sarchie				}
47465310Sarchie				bcopy(&link->stats,
47565310Sarchie				    resp->data, sizeof(link->stats));
47665310Sarchie			}
47765310Sarchie			if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
47865310Sarchie				bzero(&link->stats, sizeof(link->stats));
47965310Sarchie			break;
48065310Sarchie		    }
48165310Sarchie		case NGM_BRIDGE_GET_TABLE:
48265310Sarchie		    {
48365310Sarchie			struct ng_bridge_host_ary *ary;
48465310Sarchie			struct ng_bridge_hent *hent;
48565310Sarchie			int i = 0, bucket;
48665310Sarchie
48765310Sarchie			NG_MKRESPONSE(resp, msg, sizeof(*ary)
48865310Sarchie			    + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
48965310Sarchie			if (resp == NULL) {
49065310Sarchie				error = ENOMEM;
49165310Sarchie				break;
49265310Sarchie			}
49365310Sarchie			ary = (struct ng_bridge_host_ary *)resp->data;
49465310Sarchie			ary->numHosts = priv->numHosts;
49565310Sarchie			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
49665310Sarchie				SLIST_FOREACH(hent, &priv->tab[bucket], next)
49765310Sarchie					ary->hosts[i++] = hent->host;
49865310Sarchie			}
49965310Sarchie			break;
50065310Sarchie		    }
501207680Szec		case NGM_BRIDGE_SET_PERSISTENT:
502207680Szec		    {
503207680Szec			priv->persistent = 1;
504207680Szec			break;
505207680Szec		    }
50665310Sarchie		default:
50765310Sarchie			error = EINVAL;
50865310Sarchie			break;
50965310Sarchie		}
51065310Sarchie		break;
51165310Sarchie	default:
51265310Sarchie		error = EINVAL;
51365310Sarchie		break;
51465310Sarchie	}
51565310Sarchie
51665310Sarchie	/* Done */
51770700Sjulian	NG_RESPOND_MSG(error, node, item, resp);
51870700Sjulian	NG_FREE_MSG(msg);
51965310Sarchie	return (error);
52065310Sarchie}
52165310Sarchie
52265310Sarchie/*
52365310Sarchie * Receive data on a hook
52465310Sarchie */
52565310Sarchiestatic int
52670700Sjulianng_bridge_rcvdata(hook_p hook, item_p item)
52765310Sarchie{
52870784Sjulian	const node_p node = NG_HOOK_NODE(hook);
52970784Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
53065310Sarchie	struct ng_bridge_host *host;
53165310Sarchie	struct ng_bridge_link *link;
53265310Sarchie	struct ether_header *eh;
533130931Sgreen	int error = 0, linkNum, linksSeen;
53470700Sjulian	int manycast;
53570700Sjulian	struct mbuf *m;
53670700Sjulian	struct ng_bridge_link *firstLink;
53765310Sarchie
53870700Sjulian	NGI_GET_M(item, m);
53965310Sarchie	/* Get link number */
540106665Sjhb	linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
54165310Sarchie	KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
54287599Sobrien	    ("%s: linkNum=%u", __func__, linkNum));
54365310Sarchie	link = priv->links[linkNum];
54487599Sobrien	KASSERT(link != NULL, ("%s: link%d null", __func__, linkNum));
54565310Sarchie
54665310Sarchie	/* Sanity check packet and pull up header */
54765310Sarchie	if (m->m_pkthdr.len < ETHER_HDR_LEN) {
54865310Sarchie		link->stats.recvRunts++;
54970700Sjulian		NG_FREE_ITEM(item);
55070700Sjulian		NG_FREE_M(m);
55165310Sarchie		return (EINVAL);
55265310Sarchie	}
55365310Sarchie	if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) {
55465310Sarchie		link->stats.memoryFailures++;
55570700Sjulian		NG_FREE_ITEM(item);
55665310Sarchie		return (ENOBUFS);
55765310Sarchie	}
55865310Sarchie	eh = mtod(m, struct ether_header *);
55965310Sarchie	if ((eh->ether_shost[0] & 1) != 0) {
56065310Sarchie		link->stats.recvInvalid++;
56170700Sjulian		NG_FREE_ITEM(item);
56270700Sjulian		NG_FREE_M(m);
56365310Sarchie		return (EINVAL);
56465310Sarchie	}
56565310Sarchie
56665310Sarchie	/* Is link disabled due to a loopback condition? */
56765310Sarchie	if (link->loopCount != 0) {
56865310Sarchie		link->stats.loopDrops++;
56970700Sjulian		NG_FREE_ITEM(item);
57070700Sjulian		NG_FREE_M(m);
57165310Sarchie		return (ELOOP);		/* XXX is this an appropriate error? */
57265310Sarchie	}
57365310Sarchie
57465310Sarchie	/* Update stats */
57565310Sarchie	link->stats.recvPackets++;
57665310Sarchie	link->stats.recvOctets += m->m_pkthdr.len;
57765310Sarchie	if ((manycast = (eh->ether_dhost[0] & 1)) != 0) {
57865310Sarchie		if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
57965310Sarchie			link->stats.recvBroadcasts++;
58065310Sarchie			manycast = 2;
58165310Sarchie		} else
58265310Sarchie			link->stats.recvMulticasts++;
58365310Sarchie	}
58465310Sarchie
58565310Sarchie	/* Look up packet's source Ethernet address in hashtable */
58665310Sarchie	if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
58765310Sarchie
58865310Sarchie		/* Update time since last heard from this host */
58965310Sarchie		host->staleness = 0;
59065310Sarchie
59165310Sarchie		/* Did host jump to a different link? */
59265310Sarchie		if (host->linkNum != linkNum) {
59365310Sarchie
59465310Sarchie			/*
59565310Sarchie			 * If the host's old link was recently established
59665310Sarchie			 * on the old link and it's already jumped to a new
59765310Sarchie			 * link, declare a loopback condition.
59865310Sarchie			 */
59965310Sarchie			if (host->age < priv->conf.minStableAge) {
60065310Sarchie
60165310Sarchie				/* Log the problem */
60265310Sarchie				if (priv->conf.debugLevel >= 2) {
60365310Sarchie					struct ifnet *ifp = m->m_pkthdr.rcvif;
60465310Sarchie					char suffix[32];
60565310Sarchie
60665310Sarchie					if (ifp != NULL)
60765310Sarchie						snprintf(suffix, sizeof(suffix),
608121816Sbrooks						    " (%s)", ifp->if_xname);
60965310Sarchie					else
61065310Sarchie						*suffix = '\0';
61165310Sarchie					log(LOG_WARNING, "ng_bridge: %s:"
61265310Sarchie					    " loopback detected on %s%s\n",
61365310Sarchie					    ng_bridge_nodename(node),
61470784Sjulian					    NG_HOOK_NAME(hook), suffix);
61565310Sarchie				}
61665310Sarchie
61765310Sarchie				/* Mark link as linka non grata */
61865310Sarchie				link->loopCount = priv->conf.loopTimeout;
61965310Sarchie				link->stats.loopDetects++;
62065310Sarchie
62165310Sarchie				/* Forget all hosts on this link */
62265310Sarchie				ng_bridge_remove_hosts(priv, linkNum);
62365310Sarchie
62465310Sarchie				/* Drop packet */
62565310Sarchie				link->stats.loopDrops++;
62670700Sjulian				NG_FREE_ITEM(item);
62770700Sjulian				NG_FREE_M(m);
62865310Sarchie				return (ELOOP);		/* XXX appropriate? */
62965310Sarchie			}
63065310Sarchie
63165310Sarchie			/* Move host over to new link */
63265310Sarchie			host->linkNum = linkNum;
63365310Sarchie			host->age = 0;
63465310Sarchie		}
63565310Sarchie	} else {
63665310Sarchie		if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) {
63765310Sarchie			link->stats.memoryFailures++;
63870700Sjulian			NG_FREE_ITEM(item);
63970700Sjulian			NG_FREE_M(m);
64065310Sarchie			return (ENOMEM);
64165310Sarchie		}
64265310Sarchie	}
64365310Sarchie
64465310Sarchie	/* Run packet through ipfw processing, if enabled */
645133920Sandre#if 0
646197952Sjulian	if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) {
64765310Sarchie		/* XXX not implemented yet */
64865310Sarchie	}
649133920Sandre#endif
65065310Sarchie
65165310Sarchie	/*
65265310Sarchie	 * If unicast and destination host known, deliver to host's link,
65365310Sarchie	 * unless it is the same link as the packet came in on.
65465310Sarchie	 */
65565310Sarchie	if (!manycast) {
65665310Sarchie
65765310Sarchie		/* Determine packet destination link */
65865310Sarchie		if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
65965310Sarchie			struct ng_bridge_link *const destLink
66065310Sarchie			    = priv->links[host->linkNum];
66165310Sarchie
66265310Sarchie			/* If destination same as incoming link, do nothing */
66365310Sarchie			KASSERT(destLink != NULL,
66487599Sobrien			    ("%s: link%d null", __func__, host->linkNum));
66565310Sarchie			if (destLink == link) {
66670700Sjulian				NG_FREE_ITEM(item);
66770700Sjulian				NG_FREE_M(m);
66865310Sarchie				return (0);
66965310Sarchie			}
67065310Sarchie
67165310Sarchie			/* Deliver packet out the destination link */
67265310Sarchie			destLink->stats.xmitPackets++;
67365310Sarchie			destLink->stats.xmitOctets += m->m_pkthdr.len;
67470700Sjulian			NG_FWD_NEW_DATA(error, item, destLink->hook, m);
67565310Sarchie			return (error);
67665310Sarchie		}
67765310Sarchie
67865310Sarchie		/* Destination host is not known */
67965310Sarchie		link->stats.recvUnknown++;
68065310Sarchie	}
68165310Sarchie
68265310Sarchie	/* Distribute unknown, multicast, broadcast pkts to all other links */
68370700Sjulian	firstLink = NULL;
684130931Sgreen	for (linkNum = linksSeen = 0; linksSeen <= priv->numLinks; linkNum++) {
68570700Sjulian		struct ng_bridge_link *destLink;
68670700Sjulian		struct mbuf *m2 = NULL;
68765310Sarchie
68870700Sjulian		/*
68970700Sjulian		 * If we have checked all the links then now
69070700Sjulian		 * send the original on its reserved link
69170700Sjulian		 */
692130931Sgreen		if (linksSeen == priv->numLinks) {
69370700Sjulian			/* If we never saw a good link, leave. */
69470700Sjulian			if (firstLink == NULL) {
69570700Sjulian				NG_FREE_ITEM(item);
69670700Sjulian				NG_FREE_M(m);
69770700Sjulian				return (0);
69870700Sjulian			}
69970700Sjulian			destLink = firstLink;
70070700Sjulian		} else {
70170700Sjulian			destLink = priv->links[linkNum];
702130931Sgreen			if (destLink != NULL)
703130931Sgreen				linksSeen++;
70470700Sjulian			/* Skip incoming link and disconnected links */
70570700Sjulian			if (destLink == NULL || destLink == link) {
70670700Sjulian				continue;
70770700Sjulian			}
70870700Sjulian			if (firstLink == NULL) {
70970700Sjulian				/*
71070700Sjulian				 * This is the first usable link we have found.
71170700Sjulian				 * Reserve it for the originals.
71270700Sjulian				 * If we never find another we save a copy.
71370700Sjulian				 */
71470700Sjulian				firstLink = destLink;
71570700Sjulian				continue;
71670700Sjulian			}
71765310Sarchie
71870700Sjulian			/*
71970700Sjulian			 * It's usable link but not the reserved (first) one.
720131155Sjulian			 * Copy mbuf info for sending.
72170700Sjulian			 */
722243882Sglebius			m2 = m_dup(m, M_NOWAIT);	/* XXX m_copypacket() */
72365310Sarchie			if (m2 == NULL) {
72465310Sarchie				link->stats.memoryFailures++;
72570700Sjulian				NG_FREE_ITEM(item);
72670700Sjulian				NG_FREE_M(m);
72765310Sarchie				return (ENOBUFS);
72865310Sarchie			}
72965310Sarchie		}
73065310Sarchie
73165310Sarchie		/* Update stats */
73265310Sarchie		destLink->stats.xmitPackets++;
73365310Sarchie		destLink->stats.xmitOctets += m->m_pkthdr.len;
73465310Sarchie		switch (manycast) {
73565310Sarchie		case 0:					/* unicast */
73665310Sarchie			break;
73765310Sarchie		case 1:					/* multicast */
73865310Sarchie			destLink->stats.xmitMulticasts++;
73965310Sarchie			break;
74065310Sarchie		case 2:					/* broadcast */
74165310Sarchie			destLink->stats.xmitBroadcasts++;
74265310Sarchie			break;
74365310Sarchie		}
74465310Sarchie
74565310Sarchie		/* Send packet */
74670700Sjulian		if (destLink == firstLink) {
74770700Sjulian			/*
74870700Sjulian			 * If we've sent all the others, send the original
74970700Sjulian			 * on the first link we found.
75070700Sjulian			 */
75170700Sjulian			NG_FWD_NEW_DATA(error, item, destLink->hook, m);
75270700Sjulian			break; /* always done last - not really needed. */
75370700Sjulian		} else {
754131155Sjulian			NG_SEND_DATA_ONLY(error, destLink->hook, m2);
75570700Sjulian		}
75665310Sarchie	}
75765310Sarchie	return (error);
75865310Sarchie}
75965310Sarchie
76065310Sarchie/*
76165310Sarchie * Shutdown node
76265310Sarchie */
76365310Sarchiestatic int
76470700Sjulianng_bridge_shutdown(node_p node)
76565310Sarchie{
76670784Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
76765310Sarchie
76887997Sarchie	/*
769141574Sru	 * Shut down everything including the timer.  Even if the
770141574Sru	 * callout has already been dequeued and is about to be
771141574Sru	 * run, ng_bridge_timeout() won't be fired as the node
772141574Sru	 * is already marked NGF_INVALID, so we're safe to free
773141574Sru	 * the node now.
77487997Sarchie	 */
77565310Sarchie	KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
77665310Sarchie	    ("%s: numLinks=%d numHosts=%d",
77787599Sobrien	    __func__, priv->numLinks, priv->numHosts));
778141574Sru	ng_uncallout(&priv->timer, node);
779141574Sru	NG_NODE_SET_PRIVATE(node, NULL);
780141574Sru	NG_NODE_UNREF(node);
781184205Sdes	free(priv->tab, M_NETGRAPH_BRIDGE);
782184205Sdes	free(priv, M_NETGRAPH_BRIDGE);
78365310Sarchie	return (0);
78465310Sarchie}
78565310Sarchie
78665310Sarchie/*
78765310Sarchie * Hook disconnection.
78865310Sarchie */
78965310Sarchiestatic int
79065310Sarchieng_bridge_disconnect(hook_p hook)
79165310Sarchie{
79270784Sjulian	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
79365310Sarchie	int linkNum;
79465310Sarchie
79565310Sarchie	/* Get link number */
796106665Sjhb	linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
79765310Sarchie	KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
79887599Sobrien	    ("%s: linkNum=%u", __func__, linkNum));
79965310Sarchie
80065310Sarchie	/* Remove all hosts associated with this link */
80165310Sarchie	ng_bridge_remove_hosts(priv, linkNum);
80265310Sarchie
80365310Sarchie	/* Free associated link information */
80487599Sobrien	KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __func__));
805184205Sdes	free(priv->links[linkNum], M_NETGRAPH_BRIDGE);
80665310Sarchie	priv->links[linkNum] = NULL;
80765310Sarchie	priv->numLinks--;
80865310Sarchie
80965310Sarchie	/* If no more hooks, go away */
81070784Sjulian	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
811207680Szec	    && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
812207680Szec	    && !priv->persistent) {
81370784Sjulian		ng_rmnode_self(NG_HOOK_NODE(hook));
81470784Sjulian	}
81565310Sarchie	return (0);
81665310Sarchie}
81765310Sarchie
81865310Sarchie/******************************************************************
81965310Sarchie		    HASH TABLE FUNCTIONS
82065310Sarchie******************************************************************/
82165310Sarchie
82265310Sarchie/*
82365310Sarchie * Hash algorithm
82465310Sarchie */
82565310Sarchie#define HASH(addr,mask)		( (((const u_int16_t *)(addr))[0] 	\
82665310Sarchie				 ^ ((const u_int16_t *)(addr))[1] 	\
82765310Sarchie				 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
82865310Sarchie
82965310Sarchie/*
83065310Sarchie * Find a host entry in the table.
83165310Sarchie */
83265310Sarchiestatic struct ng_bridge_host *
83365310Sarchieng_bridge_get(priv_p priv, const u_char *addr)
83465310Sarchie{
83565310Sarchie	const int bucket = HASH(addr, priv->hashMask);
83665310Sarchie	struct ng_bridge_hent *hent;
83765310Sarchie
83865310Sarchie	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
83965310Sarchie		if (ETHER_EQUAL(hent->host.addr, addr))
84065310Sarchie			return (&hent->host);
84165310Sarchie	}
84265310Sarchie	return (NULL);
84365310Sarchie}
84465310Sarchie
84565310Sarchie/*
84665310Sarchie * Add a new host entry to the table. This assumes the host doesn't
84765310Sarchie * already exist in the table. Returns 1 on success, 0 if there
84865310Sarchie * was a memory allocation failure.
84965310Sarchie */
85065310Sarchiestatic int
85165310Sarchieng_bridge_put(priv_p priv, const u_char *addr, int linkNum)
85265310Sarchie{
85365310Sarchie	const int bucket = HASH(addr, priv->hashMask);
85465310Sarchie	struct ng_bridge_hent *hent;
85565310Sarchie
85665310Sarchie#ifdef INVARIANTS
85765310Sarchie	/* Assert that entry does not already exist in hashtable */
85865310Sarchie	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
85965310Sarchie		KASSERT(!ETHER_EQUAL(hent->host.addr, addr),
86087599Sobrien		    ("%s: entry %6D exists in table", __func__, addr, ":"));
86165310Sarchie	}
86265310Sarchie#endif
86365310Sarchie
86465310Sarchie	/* Allocate and initialize new hashtable entry */
865184214Sdes	hent = malloc(sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT);
86665310Sarchie	if (hent == NULL)
86765310Sarchie		return (0);
86865310Sarchie	bcopy(addr, hent->host.addr, ETHER_ADDR_LEN);
86965310Sarchie	hent->host.linkNum = linkNum;
87065310Sarchie	hent->host.staleness = 0;
87165310Sarchie	hent->host.age = 0;
87265310Sarchie
87365310Sarchie	/* Add new element to hash bucket */
87465310Sarchie	SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next);
87565310Sarchie	priv->numHosts++;
87665310Sarchie
87765310Sarchie	/* Resize table if necessary */
87865310Sarchie	ng_bridge_rehash(priv);
87965310Sarchie	return (1);
88065310Sarchie}
88165310Sarchie
88265310Sarchie/*
88365310Sarchie * Resize the hash table. We try to maintain the number of buckets
88465310Sarchie * such that the load factor is in the range 0.25 to 1.0.
88565310Sarchie *
88665310Sarchie * If we can't get the new memory then we silently fail. This is OK
88765310Sarchie * because things will still work and we'll try again soon anyway.
88865310Sarchie */
88965310Sarchiestatic void
89065310Sarchieng_bridge_rehash(priv_p priv)
89165310Sarchie{
89265310Sarchie	struct ng_bridge_bucket *newTab;
89365310Sarchie	int oldBucket, newBucket;
89465310Sarchie	int newNumBuckets;
89565310Sarchie	u_int newMask;
89665310Sarchie
89765310Sarchie	/* Is table too full or too empty? */
89865310Sarchie	if (priv->numHosts > priv->numBuckets
89965310Sarchie	    && (priv->numBuckets << 1) <= MAX_BUCKETS)
90065310Sarchie		newNumBuckets = priv->numBuckets << 1;
90165310Sarchie	else if (priv->numHosts < (priv->numBuckets >> 2)
90265310Sarchie	    && (priv->numBuckets >> 2) >= MIN_BUCKETS)
90365310Sarchie		newNumBuckets = priv->numBuckets >> 2;
90465310Sarchie	else
90565310Sarchie		return;
90665310Sarchie	newMask = newNumBuckets - 1;
90765310Sarchie
90865310Sarchie	/* Allocate and initialize new table */
909184214Sdes	newTab = malloc(newNumBuckets * sizeof(*newTab),
910184214Sdes	    M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
91165310Sarchie	if (newTab == NULL)
91265310Sarchie		return;
91365310Sarchie
91465310Sarchie	/* Move all entries from old table to new table */
91565310Sarchie	for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
91665310Sarchie		struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
91765310Sarchie
91865310Sarchie		while (!SLIST_EMPTY(oldList)) {
91965310Sarchie			struct ng_bridge_hent *const hent
92065310Sarchie			    = SLIST_FIRST(oldList);
92165310Sarchie
92265310Sarchie			SLIST_REMOVE_HEAD(oldList, next);
92365310Sarchie			newBucket = HASH(hent->host.addr, newMask);
92465310Sarchie			SLIST_INSERT_HEAD(&newTab[newBucket], hent, next);
92565310Sarchie		}
92665310Sarchie	}
92765310Sarchie
92865310Sarchie	/* Replace old table with new one */
92965310Sarchie	if (priv->conf.debugLevel >= 3) {
93065310Sarchie		log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
93165310Sarchie		    ng_bridge_nodename(priv->node),
93265310Sarchie		    priv->numBuckets, newNumBuckets);
93365310Sarchie	}
934184205Sdes	free(priv->tab, M_NETGRAPH_BRIDGE);
93565310Sarchie	priv->numBuckets = newNumBuckets;
93665310Sarchie	priv->hashMask = newMask;
93765310Sarchie	priv->tab = newTab;
93865310Sarchie	return;
93965310Sarchie}
94065310Sarchie
94165310Sarchie/******************************************************************
94265310Sarchie		    MISC FUNCTIONS
94365310Sarchie******************************************************************/
94465310Sarchie
94565310Sarchie/*
94665310Sarchie * Remove all hosts associated with a specific link from the hashtable.
94765310Sarchie * If linkNum == -1, then remove all hosts in the table.
94865310Sarchie */
94965310Sarchiestatic void
95065310Sarchieng_bridge_remove_hosts(priv_p priv, int linkNum)
95165310Sarchie{
95265310Sarchie	int bucket;
95365310Sarchie
95465310Sarchie	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
95565310Sarchie		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
95665310Sarchie
95765310Sarchie		while (*hptr != NULL) {
95865310Sarchie			struct ng_bridge_hent *const hent = *hptr;
95965310Sarchie
96065310Sarchie			if (linkNum == -1 || hent->host.linkNum == linkNum) {
96165310Sarchie				*hptr = SLIST_NEXT(hent, next);
962184205Sdes				free(hent, M_NETGRAPH_BRIDGE);
96365310Sarchie				priv->numHosts--;
96465310Sarchie			} else
96565310Sarchie				hptr = &SLIST_NEXT(hent, next);
96665310Sarchie		}
96765310Sarchie	}
96865310Sarchie}
96965310Sarchie
97065310Sarchie/*
97165310Sarchie * Handle our once-per-second timeout event. We do two things:
97265310Sarchie * we decrement link->loopCount for those links being muted due to
97365310Sarchie * a detected loopback condition, and we remove any hosts from
97465310Sarchie * the hashtable whom we haven't heard from in a long while.
97565310Sarchie */
97665310Sarchiestatic void
977138834Sglebiusng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2)
97865310Sarchie{
97970784Sjulian	const priv_p priv = NG_NODE_PRIVATE(node);
980138834Sglebius	int bucket;
98165310Sarchie	int counter = 0;
98265310Sarchie	int linkNum;
98365310Sarchie
98465310Sarchie	/* Update host time counters and remove stale entries */
98565310Sarchie	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
98665310Sarchie		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
98765310Sarchie
98865310Sarchie		while (*hptr != NULL) {
98965310Sarchie			struct ng_bridge_hent *const hent = *hptr;
99065310Sarchie
99165310Sarchie			/* Make sure host's link really exists */
99265310Sarchie			KASSERT(priv->links[hent->host.linkNum] != NULL,
99365310Sarchie			    ("%s: host %6D on nonexistent link %d\n",
99487599Sobrien			    __func__, hent->host.addr, ":",
99565310Sarchie			    hent->host.linkNum));
99665310Sarchie
99765310Sarchie			/* Remove hosts we haven't heard from in a while */
99865310Sarchie			if (++hent->host.staleness >= priv->conf.maxStaleness) {
99965310Sarchie				*hptr = SLIST_NEXT(hent, next);
1000184205Sdes				free(hent, M_NETGRAPH_BRIDGE);
100165310Sarchie				priv->numHosts--;
100265310Sarchie			} else {
100365310Sarchie				if (hent->host.age < 0xffff)
100465310Sarchie					hent->host.age++;
100565310Sarchie				hptr = &SLIST_NEXT(hent, next);
100665310Sarchie				counter++;
100765310Sarchie			}
100865310Sarchie		}
100965310Sarchie	}
101065310Sarchie	KASSERT(priv->numHosts == counter,
101187599Sobrien	    ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
101265310Sarchie
101365310Sarchie	/* Decrease table size if necessary */
101465310Sarchie	ng_bridge_rehash(priv);
101565310Sarchie
101665310Sarchie	/* Decrease loop counter on muted looped back links */
101765310Sarchie	for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) {
101865310Sarchie		struct ng_bridge_link *const link = priv->links[linkNum];
101965310Sarchie
102065310Sarchie		if (link != NULL) {
102165310Sarchie			if (link->loopCount != 0) {
102265310Sarchie				link->loopCount--;
102365310Sarchie				if (link->loopCount == 0
102465310Sarchie				    && priv->conf.debugLevel >= 2) {
102565310Sarchie					log(LOG_INFO, "ng_bridge: %s:"
102665310Sarchie					    " restoring looped back link%d\n",
102765310Sarchie					    ng_bridge_nodename(node), linkNum);
102865310Sarchie				}
102965310Sarchie			}
103065310Sarchie			counter++;
103165310Sarchie		}
103265310Sarchie	}
103365310Sarchie	KASSERT(priv->numLinks == counter,
103487599Sobrien	    ("%s: links: %d != %d", __func__, priv->numLinks, counter));
103565310Sarchie
1036138834Sglebius	/* Register a new timeout, keeping the existing node reference */
1037138834Sglebius	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
103865310Sarchie}
103965310Sarchie
104065310Sarchie/*
104165310Sarchie * Return node's "name", even if it doesn't have one.
104265310Sarchie */
104365310Sarchiestatic const char *
104465310Sarchieng_bridge_nodename(node_p node)
104565310Sarchie{
1046125028Sharti	static char name[NG_NODESIZ];
104765310Sarchie
104870784Sjulian	if (NG_NODE_NAME(node) != NULL)
104970784Sjulian		snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
105065310Sarchie	else
105165310Sarchie		snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
105265310Sarchie	return name;
105365310Sarchie}
105465310Sarchie
1055