xref: /freebsd/sys/netgraph/ng_bridge.c (revision c398230b64aea809cb7c5cea8db580af7097920c)
1ed2dbd31SArchie Cobbs /*
2ed2dbd31SArchie Cobbs  * ng_bridge.c
3c398230bSWarner Losh  */
4c398230bSWarner Losh 
5c398230bSWarner Losh /*-
6ed2dbd31SArchie Cobbs  * Copyright (c) 2000 Whistle Communications, Inc.
7ed2dbd31SArchie Cobbs  * All rights reserved.
8ed2dbd31SArchie Cobbs  *
9ed2dbd31SArchie Cobbs  * Subject to the following obligations and disclaimer of warranty, use and
10ed2dbd31SArchie Cobbs  * redistribution of this software, in source or object code forms, with or
11ed2dbd31SArchie Cobbs  * without modifications are expressly permitted by Whistle Communications;
12ed2dbd31SArchie Cobbs  * provided, however, that:
13ed2dbd31SArchie Cobbs  * 1. Any and all reproductions of the source or object code must include the
14ed2dbd31SArchie Cobbs  *    copyright notice above and the following disclaimer of warranties; and
15ed2dbd31SArchie Cobbs  * 2. No rights are granted, in any manner or form, to use Whistle
16ed2dbd31SArchie Cobbs  *    Communications, Inc. trademarks, including the mark "WHISTLE
17ed2dbd31SArchie Cobbs  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18ed2dbd31SArchie Cobbs  *    such appears in the above copyright notice or in the software.
19ed2dbd31SArchie Cobbs  *
20ed2dbd31SArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21ed2dbd31SArchie Cobbs  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22ed2dbd31SArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23ed2dbd31SArchie Cobbs  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24ed2dbd31SArchie Cobbs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25ed2dbd31SArchie Cobbs  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26ed2dbd31SArchie Cobbs  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27ed2dbd31SArchie Cobbs  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28ed2dbd31SArchie Cobbs  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29ed2dbd31SArchie Cobbs  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30ed2dbd31SArchie Cobbs  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31ed2dbd31SArchie Cobbs  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32ed2dbd31SArchie Cobbs  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33ed2dbd31SArchie Cobbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34ed2dbd31SArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35ed2dbd31SArchie Cobbs  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36ed2dbd31SArchie Cobbs  * OF SUCH DAMAGE.
37ed2dbd31SArchie Cobbs  *
38ed2dbd31SArchie Cobbs  * Author: Archie Cobbs <archie@freebsd.org>
39ed2dbd31SArchie Cobbs  *
40ed2dbd31SArchie Cobbs  * $FreeBSD$
41ed2dbd31SArchie Cobbs  */
42ed2dbd31SArchie Cobbs 
43ed2dbd31SArchie Cobbs /*
44ed2dbd31SArchie Cobbs  * ng_bridge(4) netgraph node type
45ed2dbd31SArchie Cobbs  *
46ed2dbd31SArchie Cobbs  * The node performs standard intelligent Ethernet bridging over
47ed2dbd31SArchie Cobbs  * each of its connected hooks, or links.  A simple loop detection
48ed2dbd31SArchie Cobbs  * algorithm is included which disables a link for priv->conf.loopTimeout
49ed2dbd31SArchie Cobbs  * seconds when a host is seen to have jumped from one link to
50ed2dbd31SArchie Cobbs  * another within priv->conf.minStableAge seconds.
51ed2dbd31SArchie Cobbs  *
52ed2dbd31SArchie Cobbs  * We keep a hashtable that maps Ethernet addresses to host info,
53ed2dbd31SArchie Cobbs  * which is contained in struct ng_bridge_host's. These structures
54ed2dbd31SArchie Cobbs  * tell us on which link the host may be found. A host's entry will
55ed2dbd31SArchie Cobbs  * expire after priv->conf.maxStaleness seconds.
56ed2dbd31SArchie Cobbs  *
57ed2dbd31SArchie Cobbs  * This node is optimzed for stable networks, where machines jump
58ed2dbd31SArchie Cobbs  * from one port to the other only rarely.
59ed2dbd31SArchie Cobbs  */
60ed2dbd31SArchie Cobbs 
61ed2dbd31SArchie Cobbs #include <sys/param.h>
62ed2dbd31SArchie Cobbs #include <sys/systm.h>
63ed2dbd31SArchie Cobbs #include <sys/kernel.h>
64ed2dbd31SArchie Cobbs #include <sys/malloc.h>
65ed2dbd31SArchie Cobbs #include <sys/mbuf.h>
66ed2dbd31SArchie Cobbs #include <sys/errno.h>
67ed2dbd31SArchie Cobbs #include <sys/syslog.h>
68ed2dbd31SArchie Cobbs #include <sys/socket.h>
69ed2dbd31SArchie Cobbs #include <sys/ctype.h>
70ed2dbd31SArchie Cobbs 
71ed2dbd31SArchie Cobbs #include <net/if.h>
72ed2dbd31SArchie Cobbs #include <net/ethernet.h>
73ed2dbd31SArchie Cobbs 
74ed2dbd31SArchie Cobbs #include <netinet/in.h>
75ed2dbd31SArchie Cobbs #include <netinet/ip_fw.h>
76ed2dbd31SArchie Cobbs 
77ed2dbd31SArchie Cobbs #include <netgraph/ng_message.h>
78ed2dbd31SArchie Cobbs #include <netgraph/netgraph.h>
79ed2dbd31SArchie Cobbs #include <netgraph/ng_parse.h>
80ed2dbd31SArchie Cobbs #include <netgraph/ng_bridge.h>
81ed2dbd31SArchie Cobbs 
829c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC
839c8c302fSJulian Elischer MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge", "netgraph bridge node ");
849c8c302fSJulian Elischer #else
859c8c302fSJulian Elischer #define M_NETGRAPH_BRIDGE M_NETGRAPH
869c8c302fSJulian Elischer #endif
879c8c302fSJulian Elischer 
88ed2dbd31SArchie Cobbs /* Per-link private data */
89ed2dbd31SArchie Cobbs struct ng_bridge_link {
90ed2dbd31SArchie Cobbs 	hook_p				hook;		/* netgraph hook */
91ed2dbd31SArchie Cobbs 	u_int16_t			loopCount;	/* loop ignore timer */
92ed2dbd31SArchie Cobbs 	struct ng_bridge_link_stats	stats;		/* link stats */
93ed2dbd31SArchie Cobbs };
94ed2dbd31SArchie Cobbs 
95ed2dbd31SArchie Cobbs /* Per-node private data */
96ed2dbd31SArchie Cobbs struct ng_bridge_private {
97ed2dbd31SArchie Cobbs 	struct ng_bridge_bucket	*tab;		/* hash table bucket array */
98ed2dbd31SArchie Cobbs 	struct ng_bridge_link	*links[NG_BRIDGE_MAX_LINKS];
99ed2dbd31SArchie Cobbs 	struct ng_bridge_config	conf;		/* node configuration */
100ed2dbd31SArchie Cobbs 	node_p			node;		/* netgraph node */
101ed2dbd31SArchie Cobbs 	u_int			numHosts;	/* num entries in table */
102ed2dbd31SArchie Cobbs 	u_int			numBuckets;	/* num buckets in table */
103ed2dbd31SArchie Cobbs 	u_int			hashMask;	/* numBuckets - 1 */
104ed2dbd31SArchie Cobbs 	int			numLinks;	/* num connected links */
105ed2dbd31SArchie Cobbs 	struct callout		timer;		/* one second periodic timer */
106ed2dbd31SArchie Cobbs };
107ed2dbd31SArchie Cobbs typedef struct ng_bridge_private *priv_p;
108ed2dbd31SArchie Cobbs 
109ed2dbd31SArchie Cobbs /* Information about a host, stored in a hash table entry */
110ed2dbd31SArchie Cobbs struct ng_bridge_hent {
111ed2dbd31SArchie Cobbs 	struct ng_bridge_host		host;	/* actual host info */
112ed2dbd31SArchie Cobbs 	SLIST_ENTRY(ng_bridge_hent)	next;	/* next entry in bucket */
113ed2dbd31SArchie Cobbs };
114ed2dbd31SArchie Cobbs 
115ed2dbd31SArchie Cobbs /* Hash table bucket declaration */
116ed2dbd31SArchie Cobbs SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent);
117ed2dbd31SArchie Cobbs 
118ed2dbd31SArchie Cobbs /* Netgraph node methods */
119ed2dbd31SArchie Cobbs static ng_constructor_t	ng_bridge_constructor;
120ed2dbd31SArchie Cobbs static ng_rcvmsg_t	ng_bridge_rcvmsg;
121069154d5SJulian Elischer static ng_shutdown_t	ng_bridge_shutdown;
122ed2dbd31SArchie Cobbs static ng_newhook_t	ng_bridge_newhook;
123ed2dbd31SArchie Cobbs static ng_rcvdata_t	ng_bridge_rcvdata;
124ed2dbd31SArchie Cobbs static ng_disconnect_t	ng_bridge_disconnect;
125ed2dbd31SArchie Cobbs 
126ed2dbd31SArchie Cobbs /* Other internal functions */
127ed2dbd31SArchie Cobbs static struct	ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr);
128ed2dbd31SArchie Cobbs static int	ng_bridge_put(priv_p priv, const u_char *addr, int linkNum);
129ed2dbd31SArchie Cobbs static void	ng_bridge_rehash(priv_p priv);
130ed2dbd31SArchie Cobbs static void	ng_bridge_remove_hosts(priv_p priv, int linkNum);
131e0d32af7SGleb Smirnoff static void	ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2);
132ed2dbd31SArchie Cobbs static const	char *ng_bridge_nodename(node_p node);
133ed2dbd31SArchie Cobbs 
134ed2dbd31SArchie Cobbs /* Ethernet broadcast */
135ed2dbd31SArchie Cobbs static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
136ed2dbd31SArchie Cobbs     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
137ed2dbd31SArchie Cobbs 
138ed2dbd31SArchie Cobbs /* Store each hook's link number in the private field */
139ed2dbd31SArchie Cobbs #define LINK_NUM(hook)		(*(u_int16_t *)(&(hook)->private))
140ed2dbd31SArchie Cobbs 
141ed2dbd31SArchie Cobbs /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
142ed2dbd31SArchie Cobbs #define ETHER_EQUAL(a,b)	(((const u_int32_t *)(a))[0] \
143ed2dbd31SArchie Cobbs 					== ((const u_int32_t *)(b))[0] \
144ed2dbd31SArchie Cobbs 				    && ((const u_int16_t *)(a))[2] \
145ed2dbd31SArchie Cobbs 					== ((const u_int16_t *)(b))[2])
146ed2dbd31SArchie Cobbs 
147ed2dbd31SArchie Cobbs /* Minimum and maximum number of hash buckets. Must be a power of two. */
148ed2dbd31SArchie Cobbs #define MIN_BUCKETS		(1 << 5)	/* 32 */
149ed2dbd31SArchie Cobbs #define MAX_BUCKETS		(1 << 14)	/* 16384 */
150ed2dbd31SArchie Cobbs 
151ed2dbd31SArchie Cobbs /* Configuration default values */
152ed2dbd31SArchie Cobbs #define DEFAULT_LOOP_TIMEOUT	60
153ed2dbd31SArchie Cobbs #define DEFAULT_MAX_STALENESS	(15 * 60)	/* same as ARP timeout */
154ed2dbd31SArchie Cobbs #define DEFAULT_MIN_STABLE_AGE	1
155ed2dbd31SArchie Cobbs 
156ed2dbd31SArchie Cobbs /******************************************************************
157ed2dbd31SArchie Cobbs 		    NETGRAPH PARSE TYPES
158ed2dbd31SArchie Cobbs ******************************************************************/
159ed2dbd31SArchie Cobbs 
160ed2dbd31SArchie Cobbs /*
161ed2dbd31SArchie Cobbs  * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
162ed2dbd31SArchie Cobbs  */
163ed2dbd31SArchie Cobbs static int
164ed2dbd31SArchie Cobbs ng_bridge_getTableLength(const struct ng_parse_type *type,
165ed2dbd31SArchie Cobbs 	const u_char *start, const u_char *buf)
166ed2dbd31SArchie Cobbs {
167ed2dbd31SArchie Cobbs 	const struct ng_bridge_host_ary *const hary
168ed2dbd31SArchie Cobbs 	    = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
169ed2dbd31SArchie Cobbs 
170ed2dbd31SArchie Cobbs 	return hary->numHosts;
171ed2dbd31SArchie Cobbs }
172ed2dbd31SArchie Cobbs 
173ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_host_ary */
174f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_type_fields[]
1758c7e4101SRuslan Ermilov 	= NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
176ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_type = {
177ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
178f0184ff8SArchie Cobbs 	&ng_bridge_host_type_fields
179ed2dbd31SArchie Cobbs };
180ed2dbd31SArchie Cobbs static const struct ng_parse_array_info ng_bridge_hary_type_info = {
181ed2dbd31SArchie Cobbs 	&ng_bridge_host_type,
182ed2dbd31SArchie Cobbs 	ng_bridge_getTableLength
183ed2dbd31SArchie Cobbs };
184ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_hary_type = {
185ed2dbd31SArchie Cobbs 	&ng_parse_array_type,
186ed2dbd31SArchie Cobbs 	&ng_bridge_hary_type_info
187ed2dbd31SArchie Cobbs };
188f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[]
189ed2dbd31SArchie Cobbs 	= NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
190ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_ary_type = {
191ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
192f0184ff8SArchie Cobbs 	&ng_bridge_host_ary_type_fields
193ed2dbd31SArchie Cobbs };
194ed2dbd31SArchie Cobbs 
195ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_config */
196ed2dbd31SArchie Cobbs static const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = {
197ed2dbd31SArchie Cobbs 	&ng_parse_uint8_type,
198ed2dbd31SArchie Cobbs 	NG_BRIDGE_MAX_LINKS
199ed2dbd31SArchie Cobbs };
200ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_ipfwary_type = {
201ed2dbd31SArchie Cobbs 	&ng_parse_fixedarray_type,
202ed2dbd31SArchie Cobbs 	&ng_bridge_ipfwary_type_info
203ed2dbd31SArchie Cobbs };
204f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_config_type_fields[]
205ed2dbd31SArchie Cobbs 	= NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type);
206ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_config_type = {
207ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
208f0184ff8SArchie Cobbs 	&ng_bridge_config_type_fields
209ed2dbd31SArchie Cobbs };
210ed2dbd31SArchie Cobbs 
211ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_link_stat */
212f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_stats_type_fields[]
213f0184ff8SArchie Cobbs 	= NG_BRIDGE_STATS_TYPE_INFO;
214ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_stats_type = {
215ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
216f0184ff8SArchie Cobbs 	&ng_bridge_stats_type_fields
217ed2dbd31SArchie Cobbs };
218ed2dbd31SArchie Cobbs 
219ed2dbd31SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
220ed2dbd31SArchie Cobbs static const struct ng_cmdlist ng_bridge_cmdlist[] = {
221ed2dbd31SArchie Cobbs 	{
222ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
223ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_SET_CONFIG,
224ed2dbd31SArchie Cobbs 	  "setconfig",
225ed2dbd31SArchie Cobbs 	  &ng_bridge_config_type,
226ed2dbd31SArchie Cobbs 	  NULL
227ed2dbd31SArchie Cobbs 	},
228ed2dbd31SArchie Cobbs 	{
229ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
230ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_CONFIG,
231ed2dbd31SArchie Cobbs 	  "getconfig",
232ed2dbd31SArchie Cobbs 	  NULL,
233ed2dbd31SArchie Cobbs 	  &ng_bridge_config_type
234ed2dbd31SArchie Cobbs 	},
235ed2dbd31SArchie Cobbs 	{
236ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
237ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_RESET,
238ed2dbd31SArchie Cobbs 	  "reset",
239ed2dbd31SArchie Cobbs 	  NULL,
240ed2dbd31SArchie Cobbs 	  NULL
241ed2dbd31SArchie Cobbs 	},
242ed2dbd31SArchie Cobbs 	{
243ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
244ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_STATS,
245ed2dbd31SArchie Cobbs 	  "getstats",
246ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
247ed2dbd31SArchie Cobbs 	  &ng_bridge_stats_type
248ed2dbd31SArchie Cobbs 	},
249ed2dbd31SArchie Cobbs 	{
250ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
251ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_CLR_STATS,
252ed2dbd31SArchie Cobbs 	  "clrstats",
253ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
254ed2dbd31SArchie Cobbs 	  NULL
255ed2dbd31SArchie Cobbs 	},
256ed2dbd31SArchie Cobbs 	{
257ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
258ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GETCLR_STATS,
259ed2dbd31SArchie Cobbs 	  "getclrstats",
260ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
261ed2dbd31SArchie Cobbs 	  &ng_bridge_stats_type
262ed2dbd31SArchie Cobbs 	},
263ed2dbd31SArchie Cobbs 	{
264ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
265ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_TABLE,
266ed2dbd31SArchie Cobbs 	  "gettable",
267ed2dbd31SArchie Cobbs 	  NULL,
268ed2dbd31SArchie Cobbs 	  &ng_bridge_host_ary_type
269ed2dbd31SArchie Cobbs 	},
270ed2dbd31SArchie Cobbs 	{ 0 }
271ed2dbd31SArchie Cobbs };
272ed2dbd31SArchie Cobbs 
273ed2dbd31SArchie Cobbs /* Node type descriptor */
274ed2dbd31SArchie Cobbs static struct ng_type ng_bridge_typestruct = {
275f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
276f8aae777SJulian Elischer 	.name =		NG_BRIDGE_NODE_TYPE,
277f8aae777SJulian Elischer 	.constructor =	ng_bridge_constructor,
278f8aae777SJulian Elischer 	.rcvmsg =	ng_bridge_rcvmsg,
279f8aae777SJulian Elischer 	.shutdown =	ng_bridge_shutdown,
280f8aae777SJulian Elischer 	.newhook =	ng_bridge_newhook,
281f8aae777SJulian Elischer 	.rcvdata =	ng_bridge_rcvdata,
282f8aae777SJulian Elischer 	.disconnect =	ng_bridge_disconnect,
283f8aae777SJulian Elischer 	.cmdlist =	ng_bridge_cmdlist,
284ed2dbd31SArchie Cobbs };
285a89effcdSArchie Cobbs NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
286ed2dbd31SArchie Cobbs 
287ed2dbd31SArchie Cobbs /******************************************************************
288ed2dbd31SArchie Cobbs 		    NETGRAPH NODE METHODS
289ed2dbd31SArchie Cobbs ******************************************************************/
290ed2dbd31SArchie Cobbs 
291ed2dbd31SArchie Cobbs /*
292ed2dbd31SArchie Cobbs  * Node constructor
293ed2dbd31SArchie Cobbs  */
294ed2dbd31SArchie Cobbs static int
295069154d5SJulian Elischer ng_bridge_constructor(node_p node)
296ed2dbd31SArchie Cobbs {
297ed2dbd31SArchie Cobbs 	priv_p priv;
298ed2dbd31SArchie Cobbs 
299ed2dbd31SArchie Cobbs 	/* Allocate and initialize private info */
3009c8c302fSJulian Elischer 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
301ed2dbd31SArchie Cobbs 	if (priv == NULL)
302ed2dbd31SArchie Cobbs 		return (ENOMEM);
303e0d32af7SGleb Smirnoff 	ng_callout_init(&priv->timer);
304ed2dbd31SArchie Cobbs 
305ed2dbd31SArchie Cobbs 	/* Allocate and initialize hash table, etc. */
306ed2dbd31SArchie Cobbs 	MALLOC(priv->tab, struct ng_bridge_bucket *,
3079c8c302fSJulian Elischer 	    MIN_BUCKETS * sizeof(*priv->tab), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
308ed2dbd31SArchie Cobbs 	if (priv->tab == NULL) {
3099c8c302fSJulian Elischer 		FREE(priv, M_NETGRAPH_BRIDGE);
310ed2dbd31SArchie Cobbs 		return (ENOMEM);
311ed2dbd31SArchie Cobbs 	}
312ed2dbd31SArchie Cobbs 	priv->numBuckets = MIN_BUCKETS;
313ed2dbd31SArchie Cobbs 	priv->hashMask = MIN_BUCKETS - 1;
314ed2dbd31SArchie Cobbs 	priv->conf.debugLevel = 1;
315ed2dbd31SArchie Cobbs 	priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
316ed2dbd31SArchie Cobbs 	priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
317ed2dbd31SArchie Cobbs 	priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
318ed2dbd31SArchie Cobbs 
319069154d5SJulian Elischer 	/*
320069154d5SJulian Elischer 	 * This node has all kinds of stuff that could be screwed by SMP.
321069154d5SJulian Elischer 	 * Until it gets it's own internal protection, we go through in
322069154d5SJulian Elischer 	 * single file. This could hurt a machine bridging beteen two
323069154d5SJulian Elischer 	 * GB ethernets so it should be fixed.
324069154d5SJulian Elischer 	 * When it's fixed the process SHOULD NOT SLEEP, spinlocks please!
325069154d5SJulian Elischer 	 * (and atomic ops )
326069154d5SJulian Elischer 	 */
32730400f03SJulian Elischer 	NG_NODE_FORCE_WRITER(node);
32830400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
329069154d5SJulian Elischer 	priv->node = node;
330ed2dbd31SArchie Cobbs 
3316c12c2b1SArchie Cobbs 	/* Start timer; timer is always running while node is alive */
332e0d32af7SGleb Smirnoff 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
3336c12c2b1SArchie Cobbs 
3346c12c2b1SArchie Cobbs 	/* Done */
335ed2dbd31SArchie Cobbs 	return (0);
336ed2dbd31SArchie Cobbs }
337ed2dbd31SArchie Cobbs 
338ed2dbd31SArchie Cobbs /*
339ed2dbd31SArchie Cobbs  * Method for attaching a new hook
340ed2dbd31SArchie Cobbs  */
341ed2dbd31SArchie Cobbs static	int
342ed2dbd31SArchie Cobbs ng_bridge_newhook(node_p node, hook_p hook, const char *name)
343ed2dbd31SArchie Cobbs {
34430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
345ed2dbd31SArchie Cobbs 
346ed2dbd31SArchie Cobbs 	/* Check for a link hook */
347ed2dbd31SArchie Cobbs 	if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX,
348ed2dbd31SArchie Cobbs 	    strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) {
349ed2dbd31SArchie Cobbs 		const char *cp;
350ed2dbd31SArchie Cobbs 		char *eptr;
351ed2dbd31SArchie Cobbs 		u_long linkNum;
352ed2dbd31SArchie Cobbs 
353ed2dbd31SArchie Cobbs 		cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX);
354ed2dbd31SArchie Cobbs 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
355ed2dbd31SArchie Cobbs 			return (EINVAL);
356ed2dbd31SArchie Cobbs 		linkNum = strtoul(cp, &eptr, 10);
357ed2dbd31SArchie Cobbs 		if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS)
358ed2dbd31SArchie Cobbs 			return (EINVAL);
359ed2dbd31SArchie Cobbs 		if (priv->links[linkNum] != NULL)
360ed2dbd31SArchie Cobbs 			return (EISCONN);
361ed2dbd31SArchie Cobbs 		MALLOC(priv->links[linkNum], struct ng_bridge_link *,
3629c8c302fSJulian Elischer 		    sizeof(*priv->links[linkNum]), M_NETGRAPH_BRIDGE, M_NOWAIT|M_ZERO);
363ed2dbd31SArchie Cobbs 		if (priv->links[linkNum] == NULL)
364ed2dbd31SArchie Cobbs 			return (ENOMEM);
365ed2dbd31SArchie Cobbs 		priv->links[linkNum]->hook = hook;
36630400f03SJulian Elischer 		NG_HOOK_SET_PRIVATE(hook, (void *)linkNum);
367ed2dbd31SArchie Cobbs 		priv->numLinks++;
368ed2dbd31SArchie Cobbs 		return (0);
369ed2dbd31SArchie Cobbs 	}
370ed2dbd31SArchie Cobbs 
371ed2dbd31SArchie Cobbs 	/* Unknown hook name */
372ed2dbd31SArchie Cobbs 	return (EINVAL);
373ed2dbd31SArchie Cobbs }
374ed2dbd31SArchie Cobbs 
375ed2dbd31SArchie Cobbs /*
376ed2dbd31SArchie Cobbs  * Receive a control message
377ed2dbd31SArchie Cobbs  */
378ed2dbd31SArchie Cobbs static int
379069154d5SJulian Elischer ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
380ed2dbd31SArchie Cobbs {
38130400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
382ed2dbd31SArchie Cobbs 	struct ng_mesg *resp = NULL;
383ed2dbd31SArchie Cobbs 	int error = 0;
384069154d5SJulian Elischer 	struct ng_mesg *msg;
385ed2dbd31SArchie Cobbs 
386069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
387ed2dbd31SArchie Cobbs 	switch (msg->header.typecookie) {
388ed2dbd31SArchie Cobbs 	case NGM_BRIDGE_COOKIE:
389ed2dbd31SArchie Cobbs 		switch (msg->header.cmd) {
390ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_CONFIG:
391ed2dbd31SArchie Cobbs 		    {
392ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
393ed2dbd31SArchie Cobbs 
394ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg,
395ed2dbd31SArchie Cobbs 			    sizeof(struct ng_bridge_config), M_NOWAIT);
396ed2dbd31SArchie Cobbs 			if (resp == NULL) {
397ed2dbd31SArchie Cobbs 				error = ENOMEM;
398ed2dbd31SArchie Cobbs 				break;
399ed2dbd31SArchie Cobbs 			}
400ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)resp->data;
401ed2dbd31SArchie Cobbs 			*conf = priv->conf;	/* no sanity checking needed */
402ed2dbd31SArchie Cobbs 			break;
403ed2dbd31SArchie Cobbs 		    }
404ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_SET_CONFIG:
405ed2dbd31SArchie Cobbs 		    {
406ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
407ed2dbd31SArchie Cobbs 			int i;
408ed2dbd31SArchie Cobbs 
409ed2dbd31SArchie Cobbs 			if (msg->header.arglen
410ed2dbd31SArchie Cobbs 			    != sizeof(struct ng_bridge_config)) {
411ed2dbd31SArchie Cobbs 				error = EINVAL;
412ed2dbd31SArchie Cobbs 				break;
413ed2dbd31SArchie Cobbs 			}
414ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)msg->data;
415ed2dbd31SArchie Cobbs 			priv->conf = *conf;
416ed2dbd31SArchie Cobbs 			for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++)
417ed2dbd31SArchie Cobbs 				priv->conf.ipfw[i] = !!priv->conf.ipfw[i];
418ed2dbd31SArchie Cobbs 			break;
419ed2dbd31SArchie Cobbs 		    }
420ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_RESET:
421ed2dbd31SArchie Cobbs 		    {
422ed2dbd31SArchie Cobbs 			int i;
423ed2dbd31SArchie Cobbs 
424ed2dbd31SArchie Cobbs 			/* Flush all entries in the hash table */
425ed2dbd31SArchie Cobbs 			ng_bridge_remove_hosts(priv, -1);
426ed2dbd31SArchie Cobbs 
427ed2dbd31SArchie Cobbs 			/* Reset all loop detection counters and stats */
428ed2dbd31SArchie Cobbs 			for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) {
429ed2dbd31SArchie Cobbs 				if (priv->links[i] == NULL)
430ed2dbd31SArchie Cobbs 					continue;
431ed2dbd31SArchie Cobbs 				priv->links[i]->loopCount = 0;
432ed2dbd31SArchie Cobbs 				bzero(&priv->links[i]->stats,
433ed2dbd31SArchie Cobbs 				    sizeof(priv->links[i]->stats));
434ed2dbd31SArchie Cobbs 			}
435ed2dbd31SArchie Cobbs 			break;
436ed2dbd31SArchie Cobbs 		    }
437ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_STATS:
438ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_CLR_STATS:
439ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GETCLR_STATS:
440ed2dbd31SArchie Cobbs 		    {
441ed2dbd31SArchie Cobbs 			struct ng_bridge_link *link;
442ed2dbd31SArchie Cobbs 			int linkNum;
443ed2dbd31SArchie Cobbs 
444ed2dbd31SArchie Cobbs 			/* Get link number */
445ed2dbd31SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
446ed2dbd31SArchie Cobbs 				error = EINVAL;
447ed2dbd31SArchie Cobbs 				break;
448ed2dbd31SArchie Cobbs 			}
449ed2dbd31SArchie Cobbs 			linkNum = *((u_int32_t *)msg->data);
450ed2dbd31SArchie Cobbs 			if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) {
451ed2dbd31SArchie Cobbs 				error = EINVAL;
452ed2dbd31SArchie Cobbs 				break;
453ed2dbd31SArchie Cobbs 			}
454ed2dbd31SArchie Cobbs 			if ((link = priv->links[linkNum]) == NULL) {
455ed2dbd31SArchie Cobbs 				error = ENOTCONN;
456ed2dbd31SArchie Cobbs 				break;
457ed2dbd31SArchie Cobbs 			}
458ed2dbd31SArchie Cobbs 
459ed2dbd31SArchie Cobbs 			/* Get/clear stats */
460ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
461ed2dbd31SArchie Cobbs 				NG_MKRESPONSE(resp, msg,
462ed2dbd31SArchie Cobbs 				    sizeof(link->stats), M_NOWAIT);
463ed2dbd31SArchie Cobbs 				if (resp == NULL) {
464ed2dbd31SArchie Cobbs 					error = ENOMEM;
465ed2dbd31SArchie Cobbs 					break;
466ed2dbd31SArchie Cobbs 				}
467ed2dbd31SArchie Cobbs 				bcopy(&link->stats,
468ed2dbd31SArchie Cobbs 				    resp->data, sizeof(link->stats));
469ed2dbd31SArchie Cobbs 			}
470ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
471ed2dbd31SArchie Cobbs 				bzero(&link->stats, sizeof(link->stats));
472ed2dbd31SArchie Cobbs 			break;
473ed2dbd31SArchie Cobbs 		    }
474ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_TABLE:
475ed2dbd31SArchie Cobbs 		    {
476ed2dbd31SArchie Cobbs 			struct ng_bridge_host_ary *ary;
477ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *hent;
478ed2dbd31SArchie Cobbs 			int i = 0, bucket;
479ed2dbd31SArchie Cobbs 
480ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*ary)
481ed2dbd31SArchie Cobbs 			    + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
482ed2dbd31SArchie Cobbs 			if (resp == NULL) {
483ed2dbd31SArchie Cobbs 				error = ENOMEM;
484ed2dbd31SArchie Cobbs 				break;
485ed2dbd31SArchie Cobbs 			}
486ed2dbd31SArchie Cobbs 			ary = (struct ng_bridge_host_ary *)resp->data;
487ed2dbd31SArchie Cobbs 			ary->numHosts = priv->numHosts;
488ed2dbd31SArchie Cobbs 			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
489ed2dbd31SArchie Cobbs 				SLIST_FOREACH(hent, &priv->tab[bucket], next)
490ed2dbd31SArchie Cobbs 					ary->hosts[i++] = hent->host;
491ed2dbd31SArchie Cobbs 			}
492ed2dbd31SArchie Cobbs 			break;
493ed2dbd31SArchie Cobbs 		    }
494ed2dbd31SArchie Cobbs 		default:
495ed2dbd31SArchie Cobbs 			error = EINVAL;
496ed2dbd31SArchie Cobbs 			break;
497ed2dbd31SArchie Cobbs 		}
498ed2dbd31SArchie Cobbs 		break;
499ed2dbd31SArchie Cobbs 	default:
500ed2dbd31SArchie Cobbs 		error = EINVAL;
501ed2dbd31SArchie Cobbs 		break;
502ed2dbd31SArchie Cobbs 	}
503ed2dbd31SArchie Cobbs 
504ed2dbd31SArchie Cobbs 	/* Done */
505069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
506069154d5SJulian Elischer 	NG_FREE_MSG(msg);
507ed2dbd31SArchie Cobbs 	return (error);
508ed2dbd31SArchie Cobbs }
509ed2dbd31SArchie Cobbs 
510ed2dbd31SArchie Cobbs /*
511ed2dbd31SArchie Cobbs  * Receive data on a hook
512ed2dbd31SArchie Cobbs  */
513ed2dbd31SArchie Cobbs static int
514069154d5SJulian Elischer ng_bridge_rcvdata(hook_p hook, item_p item)
515ed2dbd31SArchie Cobbs {
51630400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
51730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
518ed2dbd31SArchie Cobbs 	struct ng_bridge_host *host;
519ed2dbd31SArchie Cobbs 	struct ng_bridge_link *link;
520ed2dbd31SArchie Cobbs 	struct ether_header *eh;
521114bf091SBrian Feldman 	int error = 0, linkNum, linksSeen;
522069154d5SJulian Elischer 	int manycast;
523069154d5SJulian Elischer 	struct mbuf *m;
524069154d5SJulian Elischer 	struct ng_bridge_link *firstLink;
525ed2dbd31SArchie Cobbs 
526069154d5SJulian Elischer 	NGI_GET_M(item, m);
527ed2dbd31SArchie Cobbs 	/* Get link number */
5288214d60eSJohn Baldwin 	linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
529ed2dbd31SArchie Cobbs 	KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
5306e551fb6SDavid E. O'Brien 	    ("%s: linkNum=%u", __func__, linkNum));
531ed2dbd31SArchie Cobbs 	link = priv->links[linkNum];
5326e551fb6SDavid E. O'Brien 	KASSERT(link != NULL, ("%s: link%d null", __func__, linkNum));
533ed2dbd31SArchie Cobbs 
534ed2dbd31SArchie Cobbs 	/* Sanity check packet and pull up header */
535ed2dbd31SArchie Cobbs 	if (m->m_pkthdr.len < ETHER_HDR_LEN) {
536ed2dbd31SArchie Cobbs 		link->stats.recvRunts++;
537069154d5SJulian Elischer 		NG_FREE_ITEM(item);
538069154d5SJulian Elischer 		NG_FREE_M(m);
539ed2dbd31SArchie Cobbs 		return (EINVAL);
540ed2dbd31SArchie Cobbs 	}
541ed2dbd31SArchie Cobbs 	if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) {
542ed2dbd31SArchie Cobbs 		link->stats.memoryFailures++;
543069154d5SJulian Elischer 		NG_FREE_ITEM(item);
544ed2dbd31SArchie Cobbs 		return (ENOBUFS);
545ed2dbd31SArchie Cobbs 	}
546ed2dbd31SArchie Cobbs 	eh = mtod(m, struct ether_header *);
547ed2dbd31SArchie Cobbs 	if ((eh->ether_shost[0] & 1) != 0) {
548ed2dbd31SArchie Cobbs 		link->stats.recvInvalid++;
549069154d5SJulian Elischer 		NG_FREE_ITEM(item);
550069154d5SJulian Elischer 		NG_FREE_M(m);
551ed2dbd31SArchie Cobbs 		return (EINVAL);
552ed2dbd31SArchie Cobbs 	}
553ed2dbd31SArchie Cobbs 
554ed2dbd31SArchie Cobbs 	/* Is link disabled due to a loopback condition? */
555ed2dbd31SArchie Cobbs 	if (link->loopCount != 0) {
556ed2dbd31SArchie Cobbs 		link->stats.loopDrops++;
557069154d5SJulian Elischer 		NG_FREE_ITEM(item);
558069154d5SJulian Elischer 		NG_FREE_M(m);
559ed2dbd31SArchie Cobbs 		return (ELOOP);		/* XXX is this an appropriate error? */
560ed2dbd31SArchie Cobbs 	}
561ed2dbd31SArchie Cobbs 
562ed2dbd31SArchie Cobbs 	/* Update stats */
563ed2dbd31SArchie Cobbs 	link->stats.recvPackets++;
564ed2dbd31SArchie Cobbs 	link->stats.recvOctets += m->m_pkthdr.len;
565ed2dbd31SArchie Cobbs 	if ((manycast = (eh->ether_dhost[0] & 1)) != 0) {
566ed2dbd31SArchie Cobbs 		if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
567ed2dbd31SArchie Cobbs 			link->stats.recvBroadcasts++;
568ed2dbd31SArchie Cobbs 			manycast = 2;
569ed2dbd31SArchie Cobbs 		} else
570ed2dbd31SArchie Cobbs 			link->stats.recvMulticasts++;
571ed2dbd31SArchie Cobbs 	}
572ed2dbd31SArchie Cobbs 
573ed2dbd31SArchie Cobbs 	/* Look up packet's source Ethernet address in hashtable */
574ed2dbd31SArchie Cobbs 	if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
575ed2dbd31SArchie Cobbs 
576ed2dbd31SArchie Cobbs 		/* Update time since last heard from this host */
577ed2dbd31SArchie Cobbs 		host->staleness = 0;
578ed2dbd31SArchie Cobbs 
579ed2dbd31SArchie Cobbs 		/* Did host jump to a different link? */
580ed2dbd31SArchie Cobbs 		if (host->linkNum != linkNum) {
581ed2dbd31SArchie Cobbs 
582ed2dbd31SArchie Cobbs 			/*
583ed2dbd31SArchie Cobbs 			 * If the host's old link was recently established
584ed2dbd31SArchie Cobbs 			 * on the old link and it's already jumped to a new
585ed2dbd31SArchie Cobbs 			 * link, declare a loopback condition.
586ed2dbd31SArchie Cobbs 			 */
587ed2dbd31SArchie Cobbs 			if (host->age < priv->conf.minStableAge) {
588ed2dbd31SArchie Cobbs 
589ed2dbd31SArchie Cobbs 				/* Log the problem */
590ed2dbd31SArchie Cobbs 				if (priv->conf.debugLevel >= 2) {
591ed2dbd31SArchie Cobbs 					struct ifnet *ifp = m->m_pkthdr.rcvif;
592ed2dbd31SArchie Cobbs 					char suffix[32];
593ed2dbd31SArchie Cobbs 
594ed2dbd31SArchie Cobbs 					if (ifp != NULL)
595ed2dbd31SArchie Cobbs 						snprintf(suffix, sizeof(suffix),
5969bf40edeSBrooks Davis 						    " (%s)", ifp->if_xname);
597ed2dbd31SArchie Cobbs 					else
598ed2dbd31SArchie Cobbs 						*suffix = '\0';
599ed2dbd31SArchie Cobbs 					log(LOG_WARNING, "ng_bridge: %s:"
600ed2dbd31SArchie Cobbs 					    " loopback detected on %s%s\n",
601ed2dbd31SArchie Cobbs 					    ng_bridge_nodename(node),
60230400f03SJulian Elischer 					    NG_HOOK_NAME(hook), suffix);
603ed2dbd31SArchie Cobbs 				}
604ed2dbd31SArchie Cobbs 
605ed2dbd31SArchie Cobbs 				/* Mark link as linka non grata */
606ed2dbd31SArchie Cobbs 				link->loopCount = priv->conf.loopTimeout;
607ed2dbd31SArchie Cobbs 				link->stats.loopDetects++;
608ed2dbd31SArchie Cobbs 
609ed2dbd31SArchie Cobbs 				/* Forget all hosts on this link */
610ed2dbd31SArchie Cobbs 				ng_bridge_remove_hosts(priv, linkNum);
611ed2dbd31SArchie Cobbs 
612ed2dbd31SArchie Cobbs 				/* Drop packet */
613ed2dbd31SArchie Cobbs 				link->stats.loopDrops++;
614069154d5SJulian Elischer 				NG_FREE_ITEM(item);
615069154d5SJulian Elischer 				NG_FREE_M(m);
616ed2dbd31SArchie Cobbs 				return (ELOOP);		/* XXX appropriate? */
617ed2dbd31SArchie Cobbs 			}
618ed2dbd31SArchie Cobbs 
619ed2dbd31SArchie Cobbs 			/* Move host over to new link */
620ed2dbd31SArchie Cobbs 			host->linkNum = linkNum;
621ed2dbd31SArchie Cobbs 			host->age = 0;
622ed2dbd31SArchie Cobbs 		}
623ed2dbd31SArchie Cobbs 	} else {
624ed2dbd31SArchie Cobbs 		if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) {
625ed2dbd31SArchie Cobbs 			link->stats.memoryFailures++;
626069154d5SJulian Elischer 			NG_FREE_ITEM(item);
627069154d5SJulian Elischer 			NG_FREE_M(m);
628ed2dbd31SArchie Cobbs 			return (ENOMEM);
629ed2dbd31SArchie Cobbs 		}
630ed2dbd31SArchie Cobbs 	}
631ed2dbd31SArchie Cobbs 
632ed2dbd31SArchie Cobbs 	/* Run packet through ipfw processing, if enabled */
6339b932e9eSAndre Oppermann #if 0
634ed2dbd31SArchie Cobbs 	if (priv->conf.ipfw[linkNum] && fw_enable && ip_fw_chk_ptr != NULL) {
635ed2dbd31SArchie Cobbs 		/* XXX not implemented yet */
636ed2dbd31SArchie Cobbs 	}
6379b932e9eSAndre Oppermann #endif
638ed2dbd31SArchie Cobbs 
639ed2dbd31SArchie Cobbs 	/*
640ed2dbd31SArchie Cobbs 	 * If unicast and destination host known, deliver to host's link,
641ed2dbd31SArchie Cobbs 	 * unless it is the same link as the packet came in on.
642ed2dbd31SArchie Cobbs 	 */
643ed2dbd31SArchie Cobbs 	if (!manycast) {
644ed2dbd31SArchie Cobbs 
645ed2dbd31SArchie Cobbs 		/* Determine packet destination link */
646ed2dbd31SArchie Cobbs 		if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
647ed2dbd31SArchie Cobbs 			struct ng_bridge_link *const destLink
648ed2dbd31SArchie Cobbs 			    = priv->links[host->linkNum];
649ed2dbd31SArchie Cobbs 
650ed2dbd31SArchie Cobbs 			/* If destination same as incoming link, do nothing */
651ed2dbd31SArchie Cobbs 			KASSERT(destLink != NULL,
6526e551fb6SDavid E. O'Brien 			    ("%s: link%d null", __func__, host->linkNum));
653ed2dbd31SArchie Cobbs 			if (destLink == link) {
654069154d5SJulian Elischer 				NG_FREE_ITEM(item);
655069154d5SJulian Elischer 				NG_FREE_M(m);
656ed2dbd31SArchie Cobbs 				return (0);
657ed2dbd31SArchie Cobbs 			}
658ed2dbd31SArchie Cobbs 
659ed2dbd31SArchie Cobbs 			/* Deliver packet out the destination link */
660ed2dbd31SArchie Cobbs 			destLink->stats.xmitPackets++;
661ed2dbd31SArchie Cobbs 			destLink->stats.xmitOctets += m->m_pkthdr.len;
662069154d5SJulian Elischer 			NG_FWD_NEW_DATA(error, item, destLink->hook, m);
663ed2dbd31SArchie Cobbs 			return (error);
664ed2dbd31SArchie Cobbs 		}
665ed2dbd31SArchie Cobbs 
666ed2dbd31SArchie Cobbs 		/* Destination host is not known */
667ed2dbd31SArchie Cobbs 		link->stats.recvUnknown++;
668ed2dbd31SArchie Cobbs 	}
669ed2dbd31SArchie Cobbs 
670ed2dbd31SArchie Cobbs 	/* Distribute unknown, multicast, broadcast pkts to all other links */
671069154d5SJulian Elischer 	firstLink = NULL;
672114bf091SBrian Feldman 	for (linkNum = linksSeen = 0; linksSeen <= priv->numLinks; linkNum++) {
673069154d5SJulian Elischer 		struct ng_bridge_link *destLink;
674069154d5SJulian Elischer 		struct mbuf *m2 = NULL;
675ed2dbd31SArchie Cobbs 
676069154d5SJulian Elischer 		/*
677069154d5SJulian Elischer 		 * If we have checked all the links then now
678069154d5SJulian Elischer 		 * send the original on its reserved link
679069154d5SJulian Elischer 		 */
680114bf091SBrian Feldman 		if (linksSeen == priv->numLinks) {
681069154d5SJulian Elischer 			/* If we never saw a good link, leave. */
682069154d5SJulian Elischer 			if (firstLink == NULL) {
683069154d5SJulian Elischer 				NG_FREE_ITEM(item);
684069154d5SJulian Elischer 				NG_FREE_M(m);
685069154d5SJulian Elischer 				return (0);
686069154d5SJulian Elischer 			}
687069154d5SJulian Elischer 			destLink = firstLink;
688ed2dbd31SArchie Cobbs 		} else {
689069154d5SJulian Elischer 			destLink = priv->links[linkNum];
690114bf091SBrian Feldman 			if (destLink != NULL)
691114bf091SBrian Feldman 				linksSeen++;
692069154d5SJulian Elischer 			/* Skip incoming link and disconnected links */
693069154d5SJulian Elischer 			if (destLink == NULL || destLink == link) {
694069154d5SJulian Elischer 				continue;
695069154d5SJulian Elischer 			}
696069154d5SJulian Elischer 			if (firstLink == NULL) {
697069154d5SJulian Elischer 				/*
698069154d5SJulian Elischer 				 * This is the first usable link we have found.
699069154d5SJulian Elischer 				 * Reserve it for the originals.
700069154d5SJulian Elischer 				 * If we never find another we save a copy.
701069154d5SJulian Elischer 				 */
702069154d5SJulian Elischer 				firstLink = destLink;
703069154d5SJulian Elischer 				continue;
704069154d5SJulian Elischer 			}
705069154d5SJulian Elischer 
706069154d5SJulian Elischer 			/*
707069154d5SJulian Elischer 			 * It's usable link but not the reserved (first) one.
7083ca24c28SJulian Elischer 			 * Copy mbuf info for sending.
709069154d5SJulian Elischer 			 */
710a163d034SWarner Losh 			m2 = m_dup(m, M_DONTWAIT);	/* XXX m_copypacket() */
711ed2dbd31SArchie Cobbs 			if (m2 == NULL) {
712ed2dbd31SArchie Cobbs 				link->stats.memoryFailures++;
713069154d5SJulian Elischer 				NG_FREE_ITEM(item);
714069154d5SJulian Elischer 				NG_FREE_M(m);
715ed2dbd31SArchie Cobbs 				return (ENOBUFS);
716ed2dbd31SArchie Cobbs 			}
717ed2dbd31SArchie Cobbs 		}
718ed2dbd31SArchie Cobbs 
719ed2dbd31SArchie Cobbs 		/* Update stats */
720ed2dbd31SArchie Cobbs 		destLink->stats.xmitPackets++;
721ed2dbd31SArchie Cobbs 		destLink->stats.xmitOctets += m->m_pkthdr.len;
722ed2dbd31SArchie Cobbs 		switch (manycast) {
723ed2dbd31SArchie Cobbs 		case 0:					/* unicast */
724ed2dbd31SArchie Cobbs 			break;
725ed2dbd31SArchie Cobbs 		case 1:					/* multicast */
726ed2dbd31SArchie Cobbs 			destLink->stats.xmitMulticasts++;
727ed2dbd31SArchie Cobbs 			break;
728ed2dbd31SArchie Cobbs 		case 2:					/* broadcast */
729ed2dbd31SArchie Cobbs 			destLink->stats.xmitBroadcasts++;
730ed2dbd31SArchie Cobbs 			break;
731ed2dbd31SArchie Cobbs 		}
732ed2dbd31SArchie Cobbs 
733ed2dbd31SArchie Cobbs 		/* Send packet */
734069154d5SJulian Elischer 		if (destLink == firstLink) {
735069154d5SJulian Elischer 			/*
736069154d5SJulian Elischer 			 * If we've sent all the others, send the original
737069154d5SJulian Elischer 			 * on the first link we found.
738069154d5SJulian Elischer 			 */
739069154d5SJulian Elischer 			NG_FWD_NEW_DATA(error, item, destLink->hook, m);
740069154d5SJulian Elischer 			break; /* always done last - not really needed. */
741069154d5SJulian Elischer 		} else {
7423ca24c28SJulian Elischer 			NG_SEND_DATA_ONLY(error, destLink->hook, m2);
743ed2dbd31SArchie Cobbs 		}
744069154d5SJulian Elischer 	}
745ed2dbd31SArchie Cobbs 	return (error);
746ed2dbd31SArchie Cobbs }
747ed2dbd31SArchie Cobbs 
748ed2dbd31SArchie Cobbs /*
749ed2dbd31SArchie Cobbs  * Shutdown node
750ed2dbd31SArchie Cobbs  */
751ed2dbd31SArchie Cobbs static int
752069154d5SJulian Elischer ng_bridge_shutdown(node_p node)
753ed2dbd31SArchie Cobbs {
75430400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
755ed2dbd31SArchie Cobbs 
7566c12c2b1SArchie Cobbs 	/*
7576c12c2b1SArchie Cobbs 	 * Shut down everything except the timer. There's no way to
7586c12c2b1SArchie Cobbs 	 * avoid another possible timeout event (it may have already
7596c12c2b1SArchie Cobbs 	 * been dequeued), so we can't free the node yet.
7606c12c2b1SArchie Cobbs 	 */
761ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
762ed2dbd31SArchie Cobbs 	    ("%s: numLinks=%d numHosts=%d",
7636e551fb6SDavid E. O'Brien 	    __func__, priv->numLinks, priv->numHosts));
7649c8c302fSJulian Elischer 	FREE(priv->tab, M_NETGRAPH_BRIDGE);
7656c12c2b1SArchie Cobbs 
766be4252b3SJulian Elischer 	/* NGF_INVALID flag is now set so node will be freed at next timeout */
767ed2dbd31SArchie Cobbs 	return (0);
768ed2dbd31SArchie Cobbs }
769ed2dbd31SArchie Cobbs 
770ed2dbd31SArchie Cobbs /*
771ed2dbd31SArchie Cobbs  * Hook disconnection.
772ed2dbd31SArchie Cobbs  */
773ed2dbd31SArchie Cobbs static int
774ed2dbd31SArchie Cobbs ng_bridge_disconnect(hook_p hook)
775ed2dbd31SArchie Cobbs {
77630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
777ed2dbd31SArchie Cobbs 	int linkNum;
778ed2dbd31SArchie Cobbs 
779ed2dbd31SArchie Cobbs 	/* Get link number */
7808214d60eSJohn Baldwin 	linkNum = (intptr_t)NG_HOOK_PRIVATE(hook);
781ed2dbd31SArchie Cobbs 	KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
7826e551fb6SDavid E. O'Brien 	    ("%s: linkNum=%u", __func__, linkNum));
783ed2dbd31SArchie Cobbs 
784ed2dbd31SArchie Cobbs 	/* Remove all hosts associated with this link */
785ed2dbd31SArchie Cobbs 	ng_bridge_remove_hosts(priv, linkNum);
786ed2dbd31SArchie Cobbs 
787ed2dbd31SArchie Cobbs 	/* Free associated link information */
7886e551fb6SDavid E. O'Brien 	KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __func__));
7899c8c302fSJulian Elischer 	FREE(priv->links[linkNum], M_NETGRAPH_BRIDGE);
790ed2dbd31SArchie Cobbs 	priv->links[linkNum] = NULL;
791ed2dbd31SArchie Cobbs 	priv->numLinks--;
792ed2dbd31SArchie Cobbs 
793ed2dbd31SArchie Cobbs 	/* If no more hooks, go away */
79430400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
79530400f03SJulian Elischer 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
79630400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));
79730400f03SJulian Elischer 	}
798ed2dbd31SArchie Cobbs 	return (0);
799ed2dbd31SArchie Cobbs }
800ed2dbd31SArchie Cobbs 
801ed2dbd31SArchie Cobbs /******************************************************************
802ed2dbd31SArchie Cobbs 		    HASH TABLE FUNCTIONS
803ed2dbd31SArchie Cobbs ******************************************************************/
804ed2dbd31SArchie Cobbs 
805ed2dbd31SArchie Cobbs /*
806ed2dbd31SArchie Cobbs  * Hash algorithm
807ed2dbd31SArchie Cobbs  */
808ed2dbd31SArchie Cobbs #define HASH(addr,mask)		( (((const u_int16_t *)(addr))[0] 	\
809ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[1] 	\
810ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
811ed2dbd31SArchie Cobbs 
812ed2dbd31SArchie Cobbs /*
813ed2dbd31SArchie Cobbs  * Find a host entry in the table.
814ed2dbd31SArchie Cobbs  */
815ed2dbd31SArchie Cobbs static struct ng_bridge_host *
816ed2dbd31SArchie Cobbs ng_bridge_get(priv_p priv, const u_char *addr)
817ed2dbd31SArchie Cobbs {
818ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
819ed2dbd31SArchie Cobbs 	struct ng_bridge_hent *hent;
820ed2dbd31SArchie Cobbs 
821ed2dbd31SArchie Cobbs 	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
822ed2dbd31SArchie Cobbs 		if (ETHER_EQUAL(hent->host.addr, addr))
823ed2dbd31SArchie Cobbs 			return (&hent->host);
824ed2dbd31SArchie Cobbs 	}
825ed2dbd31SArchie Cobbs 	return (NULL);
826ed2dbd31SArchie Cobbs }
827ed2dbd31SArchie Cobbs 
828ed2dbd31SArchie Cobbs /*
829ed2dbd31SArchie Cobbs  * Add a new host entry to the table. This assumes the host doesn't
830ed2dbd31SArchie Cobbs  * already exist in the table. Returns 1 on success, 0 if there
831ed2dbd31SArchie Cobbs  * was a memory allocation failure.
832ed2dbd31SArchie Cobbs  */
833ed2dbd31SArchie Cobbs static int
834ed2dbd31SArchie Cobbs ng_bridge_put(priv_p priv, const u_char *addr, int linkNum)
835ed2dbd31SArchie Cobbs {
836ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
837ed2dbd31SArchie Cobbs 	struct ng_bridge_hent *hent;
838ed2dbd31SArchie Cobbs 
839ed2dbd31SArchie Cobbs #ifdef INVARIANTS
840ed2dbd31SArchie Cobbs 	/* Assert that entry does not already exist in hashtable */
841ed2dbd31SArchie Cobbs 	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
842ed2dbd31SArchie Cobbs 		KASSERT(!ETHER_EQUAL(hent->host.addr, addr),
8436e551fb6SDavid E. O'Brien 		    ("%s: entry %6D exists in table", __func__, addr, ":"));
844ed2dbd31SArchie Cobbs 	}
845ed2dbd31SArchie Cobbs #endif
846ed2dbd31SArchie Cobbs 
847ed2dbd31SArchie Cobbs 	/* Allocate and initialize new hashtable entry */
848ed2dbd31SArchie Cobbs 	MALLOC(hent, struct ng_bridge_hent *,
8499c8c302fSJulian Elischer 	    sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT);
850ed2dbd31SArchie Cobbs 	if (hent == NULL)
851ed2dbd31SArchie Cobbs 		return (0);
852ed2dbd31SArchie Cobbs 	bcopy(addr, hent->host.addr, ETHER_ADDR_LEN);
853ed2dbd31SArchie Cobbs 	hent->host.linkNum = linkNum;
854ed2dbd31SArchie Cobbs 	hent->host.staleness = 0;
855ed2dbd31SArchie Cobbs 	hent->host.age = 0;
856ed2dbd31SArchie Cobbs 
857ed2dbd31SArchie Cobbs 	/* Add new element to hash bucket */
858ed2dbd31SArchie Cobbs 	SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next);
859ed2dbd31SArchie Cobbs 	priv->numHosts++;
860ed2dbd31SArchie Cobbs 
861ed2dbd31SArchie Cobbs 	/* Resize table if necessary */
862ed2dbd31SArchie Cobbs 	ng_bridge_rehash(priv);
863ed2dbd31SArchie Cobbs 	return (1);
864ed2dbd31SArchie Cobbs }
865ed2dbd31SArchie Cobbs 
866ed2dbd31SArchie Cobbs /*
867ed2dbd31SArchie Cobbs  * Resize the hash table. We try to maintain the number of buckets
868ed2dbd31SArchie Cobbs  * such that the load factor is in the range 0.25 to 1.0.
869ed2dbd31SArchie Cobbs  *
870ed2dbd31SArchie Cobbs  * If we can't get the new memory then we silently fail. This is OK
871ed2dbd31SArchie Cobbs  * because things will still work and we'll try again soon anyway.
872ed2dbd31SArchie Cobbs  */
873ed2dbd31SArchie Cobbs static void
874ed2dbd31SArchie Cobbs ng_bridge_rehash(priv_p priv)
875ed2dbd31SArchie Cobbs {
876ed2dbd31SArchie Cobbs 	struct ng_bridge_bucket *newTab;
877ed2dbd31SArchie Cobbs 	int oldBucket, newBucket;
878ed2dbd31SArchie Cobbs 	int newNumBuckets;
879ed2dbd31SArchie Cobbs 	u_int newMask;
880ed2dbd31SArchie Cobbs 
881ed2dbd31SArchie Cobbs 	/* Is table too full or too empty? */
882ed2dbd31SArchie Cobbs 	if (priv->numHosts > priv->numBuckets
883ed2dbd31SArchie Cobbs 	    && (priv->numBuckets << 1) <= MAX_BUCKETS)
884ed2dbd31SArchie Cobbs 		newNumBuckets = priv->numBuckets << 1;
885ed2dbd31SArchie Cobbs 	else if (priv->numHosts < (priv->numBuckets >> 2)
886ed2dbd31SArchie Cobbs 	    && (priv->numBuckets >> 2) >= MIN_BUCKETS)
887ed2dbd31SArchie Cobbs 		newNumBuckets = priv->numBuckets >> 2;
888ed2dbd31SArchie Cobbs 	else
889ed2dbd31SArchie Cobbs 		return;
890ed2dbd31SArchie Cobbs 	newMask = newNumBuckets - 1;
891ed2dbd31SArchie Cobbs 
892ed2dbd31SArchie Cobbs 	/* Allocate and initialize new table */
893ed2dbd31SArchie Cobbs 	MALLOC(newTab, struct ng_bridge_bucket *,
8949c8c302fSJulian Elischer 	    newNumBuckets * sizeof(*newTab), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
895ed2dbd31SArchie Cobbs 	if (newTab == NULL)
896ed2dbd31SArchie Cobbs 		return;
897ed2dbd31SArchie Cobbs 
898ed2dbd31SArchie Cobbs 	/* Move all entries from old table to new table */
899ed2dbd31SArchie Cobbs 	for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
900ed2dbd31SArchie Cobbs 		struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
901ed2dbd31SArchie Cobbs 
902ed2dbd31SArchie Cobbs 		while (!SLIST_EMPTY(oldList)) {
903ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent
904ed2dbd31SArchie Cobbs 			    = SLIST_FIRST(oldList);
905ed2dbd31SArchie Cobbs 
906ed2dbd31SArchie Cobbs 			SLIST_REMOVE_HEAD(oldList, next);
907ed2dbd31SArchie Cobbs 			newBucket = HASH(hent->host.addr, newMask);
908ed2dbd31SArchie Cobbs 			SLIST_INSERT_HEAD(&newTab[newBucket], hent, next);
909ed2dbd31SArchie Cobbs 		}
910ed2dbd31SArchie Cobbs 	}
911ed2dbd31SArchie Cobbs 
912ed2dbd31SArchie Cobbs 	/* Replace old table with new one */
913ed2dbd31SArchie Cobbs 	if (priv->conf.debugLevel >= 3) {
914ed2dbd31SArchie Cobbs 		log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
915ed2dbd31SArchie Cobbs 		    ng_bridge_nodename(priv->node),
916ed2dbd31SArchie Cobbs 		    priv->numBuckets, newNumBuckets);
917ed2dbd31SArchie Cobbs 	}
9189c8c302fSJulian Elischer 	FREE(priv->tab, M_NETGRAPH_BRIDGE);
919ed2dbd31SArchie Cobbs 	priv->numBuckets = newNumBuckets;
920ed2dbd31SArchie Cobbs 	priv->hashMask = newMask;
921ed2dbd31SArchie Cobbs 	priv->tab = newTab;
922ed2dbd31SArchie Cobbs 	return;
923ed2dbd31SArchie Cobbs }
924ed2dbd31SArchie Cobbs 
925ed2dbd31SArchie Cobbs /******************************************************************
926ed2dbd31SArchie Cobbs 		    MISC FUNCTIONS
927ed2dbd31SArchie Cobbs ******************************************************************/
928ed2dbd31SArchie Cobbs 
929ed2dbd31SArchie Cobbs /*
930ed2dbd31SArchie Cobbs  * Remove all hosts associated with a specific link from the hashtable.
931ed2dbd31SArchie Cobbs  * If linkNum == -1, then remove all hosts in the table.
932ed2dbd31SArchie Cobbs  */
933ed2dbd31SArchie Cobbs static void
934ed2dbd31SArchie Cobbs ng_bridge_remove_hosts(priv_p priv, int linkNum)
935ed2dbd31SArchie Cobbs {
936ed2dbd31SArchie Cobbs 	int bucket;
937ed2dbd31SArchie Cobbs 
938ed2dbd31SArchie Cobbs 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
939ed2dbd31SArchie Cobbs 		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
940ed2dbd31SArchie Cobbs 
941ed2dbd31SArchie Cobbs 		while (*hptr != NULL) {
942ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent = *hptr;
943ed2dbd31SArchie Cobbs 
944ed2dbd31SArchie Cobbs 			if (linkNum == -1 || hent->host.linkNum == linkNum) {
945ed2dbd31SArchie Cobbs 				*hptr = SLIST_NEXT(hent, next);
9469c8c302fSJulian Elischer 				FREE(hent, M_NETGRAPH_BRIDGE);
947ed2dbd31SArchie Cobbs 				priv->numHosts--;
948ed2dbd31SArchie Cobbs 			} else
949ed2dbd31SArchie Cobbs 				hptr = &SLIST_NEXT(hent, next);
950ed2dbd31SArchie Cobbs 		}
951ed2dbd31SArchie Cobbs 	}
952ed2dbd31SArchie Cobbs }
953ed2dbd31SArchie Cobbs 
954ed2dbd31SArchie Cobbs /*
955ed2dbd31SArchie Cobbs  * Handle our once-per-second timeout event. We do two things:
956ed2dbd31SArchie Cobbs  * we decrement link->loopCount for those links being muted due to
957ed2dbd31SArchie Cobbs  * a detected loopback condition, and we remove any hosts from
958ed2dbd31SArchie Cobbs  * the hashtable whom we haven't heard from in a long while.
9596c12c2b1SArchie Cobbs  *
960be4252b3SJulian Elischer  * If the node has the NGF_INVALID flag set, our job is to kill it.
961ed2dbd31SArchie Cobbs  */
962ed2dbd31SArchie Cobbs static void
963e0d32af7SGleb Smirnoff ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2)
964ed2dbd31SArchie Cobbs {
96530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
966e0d32af7SGleb Smirnoff 	int bucket;
967ed2dbd31SArchie Cobbs 	int counter = 0;
968ed2dbd31SArchie Cobbs 	int linkNum;
969ed2dbd31SArchie Cobbs 
9706c12c2b1SArchie Cobbs 	/* If node was shut down, this is the final lingering timeout */
9716c12c2b1SArchie Cobbs 	if (NG_NODE_NOT_VALID(node)) {
97295f04defSJulian Elischer 		FREE(priv, M_NETGRAPH_BRIDGE);
9736c12c2b1SArchie Cobbs 		NG_NODE_SET_PRIVATE(node, NULL);
97430400f03SJulian Elischer 		NG_NODE_UNREF(node);
975ed2dbd31SArchie Cobbs 		return;
976ed2dbd31SArchie Cobbs 	}
977ed2dbd31SArchie Cobbs 
978ed2dbd31SArchie Cobbs 	/* Update host time counters and remove stale entries */
979ed2dbd31SArchie Cobbs 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
980ed2dbd31SArchie Cobbs 		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
981ed2dbd31SArchie Cobbs 
982ed2dbd31SArchie Cobbs 		while (*hptr != NULL) {
983ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent = *hptr;
984ed2dbd31SArchie Cobbs 
985ed2dbd31SArchie Cobbs 			/* Make sure host's link really exists */
986ed2dbd31SArchie Cobbs 			KASSERT(priv->links[hent->host.linkNum] != NULL,
987ed2dbd31SArchie Cobbs 			    ("%s: host %6D on nonexistent link %d\n",
9886e551fb6SDavid E. O'Brien 			    __func__, hent->host.addr, ":",
989ed2dbd31SArchie Cobbs 			    hent->host.linkNum));
990ed2dbd31SArchie Cobbs 
991ed2dbd31SArchie Cobbs 			/* Remove hosts we haven't heard from in a while */
992ed2dbd31SArchie Cobbs 			if (++hent->host.staleness >= priv->conf.maxStaleness) {
993ed2dbd31SArchie Cobbs 				*hptr = SLIST_NEXT(hent, next);
9949c8c302fSJulian Elischer 				FREE(hent, M_NETGRAPH_BRIDGE);
995ed2dbd31SArchie Cobbs 				priv->numHosts--;
996ed2dbd31SArchie Cobbs 			} else {
997ed2dbd31SArchie Cobbs 				if (hent->host.age < 0xffff)
998ed2dbd31SArchie Cobbs 					hent->host.age++;
999ed2dbd31SArchie Cobbs 				hptr = &SLIST_NEXT(hent, next);
1000ed2dbd31SArchie Cobbs 				counter++;
1001ed2dbd31SArchie Cobbs 			}
1002ed2dbd31SArchie Cobbs 		}
1003ed2dbd31SArchie Cobbs 	}
1004ed2dbd31SArchie Cobbs 	KASSERT(priv->numHosts == counter,
10056e551fb6SDavid E. O'Brien 	    ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
1006ed2dbd31SArchie Cobbs 
1007ed2dbd31SArchie Cobbs 	/* Decrease table size if necessary */
1008ed2dbd31SArchie Cobbs 	ng_bridge_rehash(priv);
1009ed2dbd31SArchie Cobbs 
1010ed2dbd31SArchie Cobbs 	/* Decrease loop counter on muted looped back links */
1011ed2dbd31SArchie Cobbs 	for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) {
1012ed2dbd31SArchie Cobbs 		struct ng_bridge_link *const link = priv->links[linkNum];
1013ed2dbd31SArchie Cobbs 
1014ed2dbd31SArchie Cobbs 		if (link != NULL) {
1015ed2dbd31SArchie Cobbs 			if (link->loopCount != 0) {
1016ed2dbd31SArchie Cobbs 				link->loopCount--;
1017ed2dbd31SArchie Cobbs 				if (link->loopCount == 0
1018ed2dbd31SArchie Cobbs 				    && priv->conf.debugLevel >= 2) {
1019ed2dbd31SArchie Cobbs 					log(LOG_INFO, "ng_bridge: %s:"
1020ed2dbd31SArchie Cobbs 					    " restoring looped back link%d\n",
1021ed2dbd31SArchie Cobbs 					    ng_bridge_nodename(node), linkNum);
1022ed2dbd31SArchie Cobbs 				}
1023ed2dbd31SArchie Cobbs 			}
1024ed2dbd31SArchie Cobbs 			counter++;
1025ed2dbd31SArchie Cobbs 		}
1026ed2dbd31SArchie Cobbs 	}
1027ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == counter,
10286e551fb6SDavid E. O'Brien 	    ("%s: links: %d != %d", __func__, priv->numLinks, counter));
1029ed2dbd31SArchie Cobbs 
1030e0d32af7SGleb Smirnoff 	/* Register a new timeout, keeping the existing node reference */
1031e0d32af7SGleb Smirnoff 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
1032ed2dbd31SArchie Cobbs }
1033ed2dbd31SArchie Cobbs 
1034ed2dbd31SArchie Cobbs /*
1035ed2dbd31SArchie Cobbs  * Return node's "name", even if it doesn't have one.
1036ed2dbd31SArchie Cobbs  */
1037ed2dbd31SArchie Cobbs static const char *
1038ed2dbd31SArchie Cobbs ng_bridge_nodename(node_p node)
1039ed2dbd31SArchie Cobbs {
104087e2c66aSHartmut Brandt 	static char name[NG_NODESIZ];
1041ed2dbd31SArchie Cobbs 
104230400f03SJulian Elischer 	if (NG_NODE_NAME(node) != NULL)
104330400f03SJulian Elischer 		snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
1044ed2dbd31SArchie Cobbs 	else
1045ed2dbd31SArchie Cobbs 		snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
1046ed2dbd31SArchie Cobbs 	return name;
1047ed2dbd31SArchie Cobbs }
1048ed2dbd31SArchie Cobbs 
1049