xref: /freebsd/sys/netgraph/ng_bridge.c (revision 631cabba47716ccecae352bb2b4521a489b64bb2)
1c398230bSWarner Losh /*-
2ed2dbd31SArchie Cobbs  * Copyright (c) 2000 Whistle Communications, Inc.
3ed2dbd31SArchie Cobbs  * All rights reserved.
4ed2dbd31SArchie Cobbs  *
5ed2dbd31SArchie Cobbs  * Subject to the following obligations and disclaimer of warranty, use and
6ed2dbd31SArchie Cobbs  * redistribution of this software, in source or object code forms, with or
7ed2dbd31SArchie Cobbs  * without modifications are expressly permitted by Whistle Communications;
8ed2dbd31SArchie Cobbs  * provided, however, that:
9ed2dbd31SArchie Cobbs  * 1. Any and all reproductions of the source or object code must include the
10ed2dbd31SArchie Cobbs  *    copyright notice above and the following disclaimer of warranties; and
11ed2dbd31SArchie Cobbs  * 2. No rights are granted, in any manner or form, to use Whistle
12ed2dbd31SArchie Cobbs  *    Communications, Inc. trademarks, including the mark "WHISTLE
13ed2dbd31SArchie Cobbs  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
14ed2dbd31SArchie Cobbs  *    such appears in the above copyright notice or in the software.
15ed2dbd31SArchie Cobbs  *
16ed2dbd31SArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
17ed2dbd31SArchie Cobbs  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
18ed2dbd31SArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
19ed2dbd31SArchie Cobbs  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
20ed2dbd31SArchie Cobbs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
21ed2dbd31SArchie Cobbs  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
22ed2dbd31SArchie Cobbs  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
23ed2dbd31SArchie Cobbs  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
24ed2dbd31SArchie Cobbs  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
25ed2dbd31SArchie Cobbs  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
26ed2dbd31SArchie Cobbs  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27ed2dbd31SArchie Cobbs  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
28ed2dbd31SArchie Cobbs  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
29ed2dbd31SArchie Cobbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30ed2dbd31SArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31ed2dbd31SArchie Cobbs  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
32ed2dbd31SArchie Cobbs  * OF SUCH DAMAGE.
33ed2dbd31SArchie Cobbs  *
34ed2dbd31SArchie Cobbs  * Author: Archie Cobbs <archie@freebsd.org>
35ed2dbd31SArchie Cobbs  *
36ed2dbd31SArchie Cobbs  * $FreeBSD$
37ed2dbd31SArchie Cobbs  */
38ed2dbd31SArchie Cobbs 
39ed2dbd31SArchie Cobbs /*
40ed2dbd31SArchie Cobbs  * ng_bridge(4) netgraph node type
41ed2dbd31SArchie Cobbs  *
42ed2dbd31SArchie Cobbs  * The node performs standard intelligent Ethernet bridging over
43ed2dbd31SArchie Cobbs  * each of its connected hooks, or links.  A simple loop detection
44ed2dbd31SArchie Cobbs  * algorithm is included which disables a link for priv->conf.loopTimeout
45ed2dbd31SArchie Cobbs  * seconds when a host is seen to have jumped from one link to
46ed2dbd31SArchie Cobbs  * another within priv->conf.minStableAge seconds.
47ed2dbd31SArchie Cobbs  *
48ed2dbd31SArchie Cobbs  * We keep a hashtable that maps Ethernet addresses to host info,
49ed2dbd31SArchie Cobbs  * which is contained in struct ng_bridge_host's. These structures
50ed2dbd31SArchie Cobbs  * tell us on which link the host may be found. A host's entry will
51ed2dbd31SArchie Cobbs  * expire after priv->conf.maxStaleness seconds.
52ed2dbd31SArchie Cobbs  *
53ed2dbd31SArchie Cobbs  * This node is optimzed for stable networks, where machines jump
54ed2dbd31SArchie Cobbs  * from one port to the other only rarely.
55ed2dbd31SArchie Cobbs  */
56ed2dbd31SArchie Cobbs 
57ed2dbd31SArchie Cobbs #include <sys/param.h>
58ed2dbd31SArchie Cobbs #include <sys/systm.h>
59ed2dbd31SArchie Cobbs #include <sys/kernel.h>
60385195c0SMarko Zec #include <sys/lock.h>
61ed2dbd31SArchie Cobbs #include <sys/malloc.h>
62ed2dbd31SArchie Cobbs #include <sys/mbuf.h>
63ed2dbd31SArchie Cobbs #include <sys/errno.h>
64385195c0SMarko Zec #include <sys/rwlock.h>
65ed2dbd31SArchie Cobbs #include <sys/syslog.h>
66ed2dbd31SArchie Cobbs #include <sys/socket.h>
67ed2dbd31SArchie Cobbs #include <sys/ctype.h>
68ed2dbd31SArchie Cobbs 
69ed2dbd31SArchie Cobbs #include <net/if.h>
7076039bc8SGleb Smirnoff #include <net/if_var.h>
71ed2dbd31SArchie Cobbs #include <net/ethernet.h>
72530c0060SRobert Watson #include <net/vnet.h>
73ed2dbd31SArchie Cobbs 
74ed2dbd31SArchie Cobbs #include <netinet/in.h>
755f2e1642SLuigi Rizzo #if 0	/* not used yet */
76ed2dbd31SArchie Cobbs #include <netinet/ip_fw.h>
775f2e1642SLuigi Rizzo #endif
78ed2dbd31SArchie Cobbs #include <netgraph/ng_message.h>
79ed2dbd31SArchie Cobbs #include <netgraph/netgraph.h>
80ed2dbd31SArchie Cobbs #include <netgraph/ng_parse.h>
81ed2dbd31SArchie Cobbs #include <netgraph/ng_bridge.h>
82ed2dbd31SArchie Cobbs 
839c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC
84d745c852SEd Schouten static MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge",
85d745c852SEd Schouten     "netgraph bridge node");
869c8c302fSJulian Elischer #else
879c8c302fSJulian Elischer #define M_NETGRAPH_BRIDGE M_NETGRAPH
889c8c302fSJulian Elischer #endif
899c8c302fSJulian Elischer 
90ed2dbd31SArchie Cobbs /* Per-link private data */
91ed2dbd31SArchie Cobbs struct ng_bridge_link {
92ed2dbd31SArchie Cobbs 	hook_p				hook;		/* netgraph hook */
93ed2dbd31SArchie Cobbs 	u_int16_t			loopCount;	/* loop ignore timer */
94ed2dbd31SArchie Cobbs 	struct ng_bridge_link_stats	stats;		/* link stats */
95ed2dbd31SArchie Cobbs };
96ed2dbd31SArchie Cobbs 
97ed2dbd31SArchie Cobbs /* Per-node private data */
98ed2dbd31SArchie Cobbs struct ng_bridge_private {
99ed2dbd31SArchie Cobbs 	struct ng_bridge_bucket	*tab;		/* hash table bucket array */
100ed2dbd31SArchie Cobbs 	struct ng_bridge_config	conf;		/* node configuration */
101ed2dbd31SArchie Cobbs 	node_p			node;		/* netgraph node */
102ed2dbd31SArchie Cobbs 	u_int			numHosts;	/* num entries in table */
103ed2dbd31SArchie Cobbs 	u_int			numBuckets;	/* num buckets in table */
104ed2dbd31SArchie Cobbs 	u_int			hashMask;	/* numBuckets - 1 */
105ed2dbd31SArchie Cobbs 	int			numLinks;	/* num connected links */
106f8aab721SMarko Zec 	int			persistent;	/* can exist w/o hooks */
107ed2dbd31SArchie Cobbs 	struct callout		timer;		/* one second periodic timer */
108ed2dbd31SArchie Cobbs };
109ed2dbd31SArchie Cobbs typedef struct ng_bridge_private *priv_p;
110ed2dbd31SArchie Cobbs 
111ed2dbd31SArchie Cobbs /* Information about a host, stored in a hash table entry */
112ed2dbd31SArchie Cobbs struct ng_bridge_hent {
113ed2dbd31SArchie Cobbs 	struct ng_bridge_host		host;	/* actual host info */
114ed2dbd31SArchie Cobbs 	SLIST_ENTRY(ng_bridge_hent)	next;	/* next entry in bucket */
115ed2dbd31SArchie Cobbs };
116ed2dbd31SArchie Cobbs 
117ed2dbd31SArchie Cobbs /* Hash table bucket declaration */
118ed2dbd31SArchie Cobbs SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent);
119ed2dbd31SArchie Cobbs 
120ed2dbd31SArchie Cobbs /* Netgraph node methods */
121ed2dbd31SArchie Cobbs static ng_constructor_t	ng_bridge_constructor;
122ed2dbd31SArchie Cobbs static ng_rcvmsg_t	ng_bridge_rcvmsg;
123069154d5SJulian Elischer static ng_shutdown_t	ng_bridge_shutdown;
124ed2dbd31SArchie Cobbs static ng_newhook_t	ng_bridge_newhook;
125ed2dbd31SArchie Cobbs static ng_rcvdata_t	ng_bridge_rcvdata;
126ed2dbd31SArchie Cobbs static ng_disconnect_t	ng_bridge_disconnect;
127ed2dbd31SArchie Cobbs 
128ed2dbd31SArchie Cobbs /* Other internal functions */
129ed2dbd31SArchie Cobbs static struct	ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr);
130*631cabbaSGleb Smirnoff static int	ng_bridge_put(priv_p priv, const u_char *addr, link_p link);
131ed2dbd31SArchie Cobbs static void	ng_bridge_rehash(priv_p priv);
132*631cabbaSGleb Smirnoff static void	ng_bridge_remove_hosts(priv_p priv, link_p link);
133e0d32af7SGleb Smirnoff static void	ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2);
134ed2dbd31SArchie Cobbs static const	char *ng_bridge_nodename(node_p node);
135ed2dbd31SArchie Cobbs 
136ed2dbd31SArchie Cobbs /* Ethernet broadcast */
137ed2dbd31SArchie Cobbs static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
138ed2dbd31SArchie Cobbs     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
139ed2dbd31SArchie Cobbs 
140ed2dbd31SArchie Cobbs /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
141ed2dbd31SArchie Cobbs #define ETHER_EQUAL(a,b)	(((const u_int32_t *)(a))[0] \
142ed2dbd31SArchie Cobbs 					== ((const u_int32_t *)(b))[0] \
143ed2dbd31SArchie Cobbs 				    && ((const u_int16_t *)(a))[2] \
144ed2dbd31SArchie Cobbs 					== ((const u_int16_t *)(b))[2])
145ed2dbd31SArchie Cobbs 
146ed2dbd31SArchie Cobbs /* Minimum and maximum number of hash buckets. Must be a power of two. */
147ed2dbd31SArchie Cobbs #define MIN_BUCKETS		(1 << 5)	/* 32 */
148ed2dbd31SArchie Cobbs #define MAX_BUCKETS		(1 << 14)	/* 16384 */
149ed2dbd31SArchie Cobbs 
150ed2dbd31SArchie Cobbs /* Configuration default values */
151ed2dbd31SArchie Cobbs #define DEFAULT_LOOP_TIMEOUT	60
152ed2dbd31SArchie Cobbs #define DEFAULT_MAX_STALENESS	(15 * 60)	/* same as ARP timeout */
153ed2dbd31SArchie Cobbs #define DEFAULT_MIN_STABLE_AGE	1
154ed2dbd31SArchie Cobbs 
155ed2dbd31SArchie Cobbs /******************************************************************
156ed2dbd31SArchie Cobbs 		    NETGRAPH PARSE TYPES
157ed2dbd31SArchie Cobbs ******************************************************************/
158ed2dbd31SArchie Cobbs 
159ed2dbd31SArchie Cobbs /*
160ed2dbd31SArchie Cobbs  * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
161ed2dbd31SArchie Cobbs  */
162ed2dbd31SArchie Cobbs static int
163ed2dbd31SArchie Cobbs ng_bridge_getTableLength(const struct ng_parse_type *type,
164ed2dbd31SArchie Cobbs 	const u_char *start, const u_char *buf)
165ed2dbd31SArchie Cobbs {
166ed2dbd31SArchie Cobbs 	const struct ng_bridge_host_ary *const hary
167ed2dbd31SArchie Cobbs 	    = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
168ed2dbd31SArchie Cobbs 
169ed2dbd31SArchie Cobbs 	return hary->numHosts;
170ed2dbd31SArchie Cobbs }
171ed2dbd31SArchie Cobbs 
172ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_host_ary */
173f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_type_fields[]
1748c7e4101SRuslan Ermilov 	= NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
175ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_type = {
176ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
177f0184ff8SArchie Cobbs 	&ng_bridge_host_type_fields
178ed2dbd31SArchie Cobbs };
179ed2dbd31SArchie Cobbs static const struct ng_parse_array_info ng_bridge_hary_type_info = {
180ed2dbd31SArchie Cobbs 	&ng_bridge_host_type,
181ed2dbd31SArchie Cobbs 	ng_bridge_getTableLength
182ed2dbd31SArchie Cobbs };
183ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_hary_type = {
184ed2dbd31SArchie Cobbs 	&ng_parse_array_type,
185ed2dbd31SArchie Cobbs 	&ng_bridge_hary_type_info
186ed2dbd31SArchie Cobbs };
187f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[]
188ed2dbd31SArchie Cobbs 	= NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
189ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_ary_type = {
190ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
191f0184ff8SArchie Cobbs 	&ng_bridge_host_ary_type_fields
192ed2dbd31SArchie Cobbs };
193ed2dbd31SArchie Cobbs 
194ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_config */
195f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_config_type_fields[]
196*631cabbaSGleb Smirnoff 	= NG_BRIDGE_CONFIG_TYPE_INFO;
197ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_config_type = {
198ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
199f0184ff8SArchie Cobbs 	&ng_bridge_config_type_fields
200ed2dbd31SArchie Cobbs };
201ed2dbd31SArchie Cobbs 
202ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_link_stat */
203f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_stats_type_fields[]
204f0184ff8SArchie Cobbs 	= NG_BRIDGE_STATS_TYPE_INFO;
205ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_stats_type = {
206ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
207f0184ff8SArchie Cobbs 	&ng_bridge_stats_type_fields
208ed2dbd31SArchie Cobbs };
209ed2dbd31SArchie Cobbs 
210ed2dbd31SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
211ed2dbd31SArchie Cobbs static const struct ng_cmdlist ng_bridge_cmdlist[] = {
212ed2dbd31SArchie Cobbs 	{
213ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
214ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_SET_CONFIG,
215ed2dbd31SArchie Cobbs 	  "setconfig",
216ed2dbd31SArchie Cobbs 	  &ng_bridge_config_type,
217ed2dbd31SArchie Cobbs 	  NULL
218ed2dbd31SArchie Cobbs 	},
219ed2dbd31SArchie Cobbs 	{
220ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
221ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_CONFIG,
222ed2dbd31SArchie Cobbs 	  "getconfig",
223ed2dbd31SArchie Cobbs 	  NULL,
224ed2dbd31SArchie Cobbs 	  &ng_bridge_config_type
225ed2dbd31SArchie Cobbs 	},
226ed2dbd31SArchie Cobbs 	{
227ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
228ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_RESET,
229ed2dbd31SArchie Cobbs 	  "reset",
230ed2dbd31SArchie Cobbs 	  NULL,
231ed2dbd31SArchie Cobbs 	  NULL
232ed2dbd31SArchie Cobbs 	},
233ed2dbd31SArchie Cobbs 	{
234ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
235ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_STATS,
236ed2dbd31SArchie Cobbs 	  "getstats",
237ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
238ed2dbd31SArchie Cobbs 	  &ng_bridge_stats_type
239ed2dbd31SArchie Cobbs 	},
240ed2dbd31SArchie Cobbs 	{
241ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
242ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_CLR_STATS,
243ed2dbd31SArchie Cobbs 	  "clrstats",
244ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
245ed2dbd31SArchie Cobbs 	  NULL
246ed2dbd31SArchie Cobbs 	},
247ed2dbd31SArchie Cobbs 	{
248ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
249ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GETCLR_STATS,
250ed2dbd31SArchie Cobbs 	  "getclrstats",
251ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
252ed2dbd31SArchie Cobbs 	  &ng_bridge_stats_type
253ed2dbd31SArchie Cobbs 	},
254ed2dbd31SArchie Cobbs 	{
255ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
256ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_TABLE,
257ed2dbd31SArchie Cobbs 	  "gettable",
258ed2dbd31SArchie Cobbs 	  NULL,
259ed2dbd31SArchie Cobbs 	  &ng_bridge_host_ary_type
260ed2dbd31SArchie Cobbs 	},
261f8aab721SMarko Zec 	{
262f8aab721SMarko Zec 	  NGM_BRIDGE_COOKIE,
263f8aab721SMarko Zec 	  NGM_BRIDGE_SET_PERSISTENT,
264f8aab721SMarko Zec 	  "setpersistent",
265f8aab721SMarko Zec 	  NULL,
266f8aab721SMarko Zec 	  NULL
267f8aab721SMarko Zec 	},
268ed2dbd31SArchie Cobbs 	{ 0 }
269ed2dbd31SArchie Cobbs };
270ed2dbd31SArchie Cobbs 
271ed2dbd31SArchie Cobbs /* Node type descriptor */
272ed2dbd31SArchie Cobbs static struct ng_type ng_bridge_typestruct = {
273f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
274f8aae777SJulian Elischer 	.name =		NG_BRIDGE_NODE_TYPE,
275f8aae777SJulian Elischer 	.constructor =	ng_bridge_constructor,
276f8aae777SJulian Elischer 	.rcvmsg =	ng_bridge_rcvmsg,
277f8aae777SJulian Elischer 	.shutdown =	ng_bridge_shutdown,
278f8aae777SJulian Elischer 	.newhook =	ng_bridge_newhook,
279f8aae777SJulian Elischer 	.rcvdata =	ng_bridge_rcvdata,
280f8aae777SJulian Elischer 	.disconnect =	ng_bridge_disconnect,
281f8aae777SJulian Elischer 	.cmdlist =	ng_bridge_cmdlist,
282ed2dbd31SArchie Cobbs };
283a89effcdSArchie Cobbs NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
284ed2dbd31SArchie Cobbs 
285ed2dbd31SArchie Cobbs /******************************************************************
286ed2dbd31SArchie Cobbs 		    NETGRAPH NODE METHODS
287ed2dbd31SArchie Cobbs ******************************************************************/
288ed2dbd31SArchie Cobbs 
289ed2dbd31SArchie Cobbs /*
290ed2dbd31SArchie Cobbs  * Node constructor
291ed2dbd31SArchie Cobbs  */
292ed2dbd31SArchie Cobbs static int
293069154d5SJulian Elischer ng_bridge_constructor(node_p node)
294ed2dbd31SArchie Cobbs {
295ed2dbd31SArchie Cobbs 	priv_p priv;
296ed2dbd31SArchie Cobbs 
297ed2dbd31SArchie Cobbs 	/* Allocate and initialize private info */
298674d86bfSGleb Smirnoff 	priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
299e0d32af7SGleb Smirnoff 	ng_callout_init(&priv->timer);
300ed2dbd31SArchie Cobbs 
301ed2dbd31SArchie Cobbs 	/* Allocate and initialize hash table, etc. */
302e11e3f18SDag-Erling Smørgrav 	priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab),
303674d86bfSGleb Smirnoff 	    M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
304ed2dbd31SArchie Cobbs 	priv->numBuckets = MIN_BUCKETS;
305ed2dbd31SArchie Cobbs 	priv->hashMask = MIN_BUCKETS - 1;
306ed2dbd31SArchie Cobbs 	priv->conf.debugLevel = 1;
307ed2dbd31SArchie Cobbs 	priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
308ed2dbd31SArchie Cobbs 	priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
309ed2dbd31SArchie Cobbs 	priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
310ed2dbd31SArchie Cobbs 
311069154d5SJulian Elischer 	/*
312069154d5SJulian Elischer 	 * This node has all kinds of stuff that could be screwed by SMP.
313069154d5SJulian Elischer 	 * Until it gets it's own internal protection, we go through in
314053359b7SPedro F. Giffuni 	 * single file. This could hurt a machine bridging between two
315069154d5SJulian Elischer 	 * GB ethernets so it should be fixed.
316069154d5SJulian Elischer 	 * When it's fixed the process SHOULD NOT SLEEP, spinlocks please!
317069154d5SJulian Elischer 	 * (and atomic ops )
318069154d5SJulian Elischer 	 */
31930400f03SJulian Elischer 	NG_NODE_FORCE_WRITER(node);
32030400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
321069154d5SJulian Elischer 	priv->node = node;
322ed2dbd31SArchie Cobbs 
3236c12c2b1SArchie Cobbs 	/* Start timer; timer is always running while node is alive */
324e0d32af7SGleb Smirnoff 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
3256c12c2b1SArchie Cobbs 
3266c12c2b1SArchie Cobbs 	/* Done */
327ed2dbd31SArchie Cobbs 	return (0);
328ed2dbd31SArchie Cobbs }
329ed2dbd31SArchie Cobbs 
330ed2dbd31SArchie Cobbs /*
331ed2dbd31SArchie Cobbs  * Method for attaching a new hook
332ed2dbd31SArchie Cobbs  */
333ed2dbd31SArchie Cobbs static	int
334ed2dbd31SArchie Cobbs ng_bridge_newhook(node_p node, hook_p hook, const char *name)
335ed2dbd31SArchie Cobbs {
33630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
337ed2dbd31SArchie Cobbs 
338ed2dbd31SArchie Cobbs 	/* Check for a link hook */
339*631cabbaSGleb Smirnoff 	if (strlen(name) > strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) {
340*631cabbaSGleb Smirnoff 		char linkName[NG_HOOKSIZ];
341*631cabbaSGleb Smirnoff 		u_int32_t linkNum;
342*631cabbaSGleb Smirnoff 		link_p link;
343ed2dbd31SArchie Cobbs 
344*631cabbaSGleb Smirnoff 		/* primitive parsing */
345*631cabbaSGleb Smirnoff 		linkNum = strtoul(name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX),
346*631cabbaSGleb Smirnoff 				  NULL, 10);
347*631cabbaSGleb Smirnoff 		/* validation by comparing against the reconstucted name  */
348*631cabbaSGleb Smirnoff 		snprintf(linkName, sizeof(linkName),
349*631cabbaSGleb Smirnoff 			 "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX,
350*631cabbaSGleb Smirnoff 			 linkNum);
351*631cabbaSGleb Smirnoff 		if (strcmp(linkName, name) != 0)
352ed2dbd31SArchie Cobbs 			return (EINVAL);
353*631cabbaSGleb Smirnoff 
354*631cabbaSGleb Smirnoff 		if(NG_PEER_NODE(hook) == node)
355*631cabbaSGleb Smirnoff 		        return (ELOOP);
356*631cabbaSGleb Smirnoff 
357*631cabbaSGleb Smirnoff 		link = malloc(sizeof(*link), M_NETGRAPH_BRIDGE,
358*631cabbaSGleb Smirnoff 			      M_WAITOK|M_ZERO);
359*631cabbaSGleb Smirnoff 		if (link == NULL)
360ed2dbd31SArchie Cobbs 			return (ENOMEM);
361*631cabbaSGleb Smirnoff 		link->hook = hook;
362*631cabbaSGleb Smirnoff 		NG_HOOK_SET_PRIVATE(hook, link);
363ed2dbd31SArchie Cobbs 		priv->numLinks++;
364ed2dbd31SArchie Cobbs 		return (0);
365ed2dbd31SArchie Cobbs 	}
366ed2dbd31SArchie Cobbs 
367ed2dbd31SArchie Cobbs 	/* Unknown hook name */
368ed2dbd31SArchie Cobbs 	return (EINVAL);
369ed2dbd31SArchie Cobbs }
370ed2dbd31SArchie Cobbs 
371ed2dbd31SArchie Cobbs /*
372ed2dbd31SArchie Cobbs  * Receive a control message
373ed2dbd31SArchie Cobbs  */
374ed2dbd31SArchie Cobbs static int
375*631cabbaSGleb Smirnoff ng_bridge_reset_link(hook_p hook, int ret)
376*631cabbaSGleb Smirnoff {
377*631cabbaSGleb Smirnoff 	link_p priv = NG_HOOK_PRIVATE(hook);
378*631cabbaSGleb Smirnoff 
379*631cabbaSGleb Smirnoff 	priv->loopCount = 0;
380*631cabbaSGleb Smirnoff 	bzero(&priv->stats, sizeof(priv->stats));
381*631cabbaSGleb Smirnoff 	return (ret);
382*631cabbaSGleb Smirnoff }
383*631cabbaSGleb Smirnoff 
384*631cabbaSGleb Smirnoff 
385*631cabbaSGleb Smirnoff static int
386069154d5SJulian Elischer ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
387ed2dbd31SArchie Cobbs {
38830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
389ed2dbd31SArchie Cobbs 	struct ng_mesg *resp = NULL;
390ed2dbd31SArchie Cobbs 	int error = 0;
391069154d5SJulian Elischer 	struct ng_mesg *msg;
392ed2dbd31SArchie Cobbs 
393069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
394ed2dbd31SArchie Cobbs 	switch (msg->header.typecookie) {
395ed2dbd31SArchie Cobbs 	case NGM_BRIDGE_COOKIE:
396ed2dbd31SArchie Cobbs 		switch (msg->header.cmd) {
397ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_CONFIG:
398ed2dbd31SArchie Cobbs 		    {
399ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
400ed2dbd31SArchie Cobbs 
401ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg,
402ed2dbd31SArchie Cobbs 			    sizeof(struct ng_bridge_config), M_NOWAIT);
403ed2dbd31SArchie Cobbs 			if (resp == NULL) {
404ed2dbd31SArchie Cobbs 				error = ENOMEM;
405ed2dbd31SArchie Cobbs 				break;
406ed2dbd31SArchie Cobbs 			}
407ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)resp->data;
408ed2dbd31SArchie Cobbs 			*conf = priv->conf;	/* no sanity checking needed */
409ed2dbd31SArchie Cobbs 			break;
410ed2dbd31SArchie Cobbs 		    }
411ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_SET_CONFIG:
412ed2dbd31SArchie Cobbs 		    {
413ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
414ed2dbd31SArchie Cobbs 
415ed2dbd31SArchie Cobbs 			if (msg->header.arglen
416ed2dbd31SArchie Cobbs 			    != sizeof(struct ng_bridge_config)) {
417ed2dbd31SArchie Cobbs 				error = EINVAL;
418ed2dbd31SArchie Cobbs 				break;
419ed2dbd31SArchie Cobbs 			}
420ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)msg->data;
421ed2dbd31SArchie Cobbs 			priv->conf = *conf;
422ed2dbd31SArchie Cobbs 			break;
423ed2dbd31SArchie Cobbs 		    }
424ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_RESET:
425ed2dbd31SArchie Cobbs 		    {
426*631cabbaSGleb Smirnoff 			hook_p rethook;
427ed2dbd31SArchie Cobbs 
428ed2dbd31SArchie Cobbs 			/* Flush all entries in the hash table */
429*631cabbaSGleb Smirnoff 			ng_bridge_remove_hosts(priv, NULL);
430ed2dbd31SArchie Cobbs 
431ed2dbd31SArchie Cobbs 			/* Reset all loop detection counters and stats */
432*631cabbaSGleb Smirnoff 			NG_NODE_FOREACH_HOOK(node, ng_bridge_reset_link, 1, rethook);
433ed2dbd31SArchie Cobbs 			break;
434ed2dbd31SArchie Cobbs 		    }
435ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_STATS:
436ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_CLR_STATS:
437ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GETCLR_STATS:
438ed2dbd31SArchie Cobbs 		    {
439*631cabbaSGleb Smirnoff 			hook_p hook;
440*631cabbaSGleb Smirnoff 			link_p link;
441*631cabbaSGleb Smirnoff 			char linkName[NG_HOOKSIZ];
442ed2dbd31SArchie Cobbs 
443ed2dbd31SArchie Cobbs 			/* Get link number */
444ed2dbd31SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
445ed2dbd31SArchie Cobbs 				error = EINVAL;
446ed2dbd31SArchie Cobbs 				break;
447ed2dbd31SArchie Cobbs 			}
448*631cabbaSGleb Smirnoff 			snprintf(linkName, sizeof(linkName),
449*631cabbaSGleb Smirnoff 				 "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX,
450*631cabbaSGleb Smirnoff 				 *((u_int32_t *)msg->data));
451*631cabbaSGleb Smirnoff 
452*631cabbaSGleb Smirnoff 			if ((hook = ng_findhook(node, linkName)) == NULL) {
453ed2dbd31SArchie Cobbs 				error = ENOTCONN;
454ed2dbd31SArchie Cobbs 				break;
455ed2dbd31SArchie Cobbs 			}
456*631cabbaSGleb Smirnoff 			link = NG_HOOK_PRIVATE(hook);
457ed2dbd31SArchie Cobbs 
458ed2dbd31SArchie Cobbs 			/* Get/clear stats */
459ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
460ed2dbd31SArchie Cobbs 				NG_MKRESPONSE(resp, msg,
461ed2dbd31SArchie Cobbs 				    sizeof(link->stats), M_NOWAIT);
462ed2dbd31SArchie Cobbs 				if (resp == NULL) {
463ed2dbd31SArchie Cobbs 					error = ENOMEM;
464ed2dbd31SArchie Cobbs 					break;
465ed2dbd31SArchie Cobbs 				}
466ed2dbd31SArchie Cobbs 				bcopy(&link->stats,
467ed2dbd31SArchie Cobbs 				    resp->data, sizeof(link->stats));
468ed2dbd31SArchie Cobbs 			}
469ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
470ed2dbd31SArchie Cobbs 				bzero(&link->stats, sizeof(link->stats));
471ed2dbd31SArchie Cobbs 			break;
472ed2dbd31SArchie Cobbs 		    }
473ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_TABLE:
474ed2dbd31SArchie Cobbs 		    {
475ed2dbd31SArchie Cobbs 			struct ng_bridge_host_ary *ary;
476ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *hent;
477ed2dbd31SArchie Cobbs 			int i = 0, bucket;
478ed2dbd31SArchie Cobbs 
479ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*ary)
480ed2dbd31SArchie Cobbs 			    + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
481ed2dbd31SArchie Cobbs 			if (resp == NULL) {
482ed2dbd31SArchie Cobbs 				error = ENOMEM;
483ed2dbd31SArchie Cobbs 				break;
484ed2dbd31SArchie Cobbs 			}
485ed2dbd31SArchie Cobbs 			ary = (struct ng_bridge_host_ary *)resp->data;
486ed2dbd31SArchie Cobbs 			ary->numHosts = priv->numHosts;
487ed2dbd31SArchie Cobbs 			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
488*631cabbaSGleb Smirnoff 				SLIST_FOREACH(hent, &priv->tab[bucket], next) {
489*631cabbaSGleb Smirnoff 					memcpy(ary->hosts[i].addr,
490*631cabbaSGleb Smirnoff 					       hent->host.addr,
491*631cabbaSGleb Smirnoff 					       sizeof(ary->hosts[i].addr));
492*631cabbaSGleb Smirnoff 					ary->hosts[i].age       = hent->host.age;
493*631cabbaSGleb Smirnoff 					ary->hosts[i].staleness = hent->host.staleness;
494*631cabbaSGleb Smirnoff 					strncpy(ary->hosts[i].hook,
495*631cabbaSGleb Smirnoff 						NG_HOOK_NAME(hent->host.link->hook),
496*631cabbaSGleb Smirnoff 						sizeof(ary->hosts[i].hook));
497*631cabbaSGleb Smirnoff 					i++;
498*631cabbaSGleb Smirnoff 				}
499ed2dbd31SArchie Cobbs 			}
500ed2dbd31SArchie Cobbs 			break;
501ed2dbd31SArchie Cobbs 		    }
502f8aab721SMarko Zec 		case NGM_BRIDGE_SET_PERSISTENT:
503f8aab721SMarko Zec 		    {
504f8aab721SMarko Zec 			priv->persistent = 1;
505f8aab721SMarko Zec 			break;
506f8aab721SMarko Zec 		    }
507ed2dbd31SArchie Cobbs 		default:
508ed2dbd31SArchie Cobbs 			error = EINVAL;
509ed2dbd31SArchie Cobbs 			break;
510ed2dbd31SArchie Cobbs 		}
511ed2dbd31SArchie Cobbs 		break;
512ed2dbd31SArchie Cobbs 	default:
513ed2dbd31SArchie Cobbs 		error = EINVAL;
514ed2dbd31SArchie Cobbs 		break;
515ed2dbd31SArchie Cobbs 	}
516ed2dbd31SArchie Cobbs 
517ed2dbd31SArchie Cobbs 	/* Done */
518069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
519069154d5SJulian Elischer 	NG_FREE_MSG(msg);
520ed2dbd31SArchie Cobbs 	return (error);
521ed2dbd31SArchie Cobbs }
522ed2dbd31SArchie Cobbs 
523ed2dbd31SArchie Cobbs /*
524ed2dbd31SArchie Cobbs  * Receive data on a hook
525ed2dbd31SArchie Cobbs  */
526*631cabbaSGleb Smirnoff struct ng_bridge_send_ctx {
527*631cabbaSGleb Smirnoff 	link_p foundFirst, incoming;
528*631cabbaSGleb Smirnoff 	struct mbuf * m;
529*631cabbaSGleb Smirnoff 	int manycast, error;
530*631cabbaSGleb Smirnoff };
531*631cabbaSGleb Smirnoff 
532*631cabbaSGleb Smirnoff static int
533*631cabbaSGleb Smirnoff ng_bridge_send_ctx(hook_p dst, struct ng_bridge_send_ctx * ctx)
534*631cabbaSGleb Smirnoff {
535*631cabbaSGleb Smirnoff 	link_p destLink = NG_HOOK_PRIVATE(dst);
536*631cabbaSGleb Smirnoff 	struct mbuf *m2 = NULL;
537*631cabbaSGleb Smirnoff 	int error = 0;
538*631cabbaSGleb Smirnoff 
539*631cabbaSGleb Smirnoff 	/* Skip incoming link */
540*631cabbaSGleb Smirnoff 	if (destLink == ctx->incoming) {
541*631cabbaSGleb Smirnoff 		return (1);
542*631cabbaSGleb Smirnoff 	}
543*631cabbaSGleb Smirnoff 
544*631cabbaSGleb Smirnoff 	if (ctx->foundFirst == NULL) {
545*631cabbaSGleb Smirnoff 		/*
546*631cabbaSGleb Smirnoff 		 * This is the first usable link we have found.
547*631cabbaSGleb Smirnoff 		 * Reserve it for the originals.
548*631cabbaSGleb Smirnoff 		 * If we never find another we save a copy.
549*631cabbaSGleb Smirnoff 		 */
550*631cabbaSGleb Smirnoff 		ctx->foundFirst = destLink;
551*631cabbaSGleb Smirnoff 		return (1);
552*631cabbaSGleb Smirnoff 	}
553*631cabbaSGleb Smirnoff 
554*631cabbaSGleb Smirnoff 	/*
555*631cabbaSGleb Smirnoff 	 * It's usable link but not the reserved (first) one.
556*631cabbaSGleb Smirnoff 	 * Copy mbuf info for sending.
557*631cabbaSGleb Smirnoff 	 */
558*631cabbaSGleb Smirnoff 	m2 = m_dup(ctx->m, M_NOWAIT);	/* XXX m_copypacket() */
559*631cabbaSGleb Smirnoff 	if (m2 == NULL) {
560*631cabbaSGleb Smirnoff 		ctx->incoming->stats.memoryFailures++;
561*631cabbaSGleb Smirnoff 		ctx->error = ENOBUFS;
562*631cabbaSGleb Smirnoff 		return (0);	       /* abort loop */
563*631cabbaSGleb Smirnoff 	}
564*631cabbaSGleb Smirnoff 
565*631cabbaSGleb Smirnoff 
566*631cabbaSGleb Smirnoff 	/* Update stats */
567*631cabbaSGleb Smirnoff 	destLink->stats.xmitPackets++;
568*631cabbaSGleb Smirnoff 	destLink->stats.xmitOctets += m2->m_pkthdr.len;
569*631cabbaSGleb Smirnoff 	switch (ctx->manycast) {
570*631cabbaSGleb Smirnoff 	 default:					/* unknown unicast */
571*631cabbaSGleb Smirnoff 		break;
572*631cabbaSGleb Smirnoff 	 case 1:					/* multicast */
573*631cabbaSGleb Smirnoff 		destLink->stats.xmitMulticasts++;
574*631cabbaSGleb Smirnoff 		break;
575*631cabbaSGleb Smirnoff 	 case 2:					/* broadcast */
576*631cabbaSGleb Smirnoff 		destLink->stats.xmitBroadcasts++;
577*631cabbaSGleb Smirnoff 		break;
578*631cabbaSGleb Smirnoff 	}
579*631cabbaSGleb Smirnoff 
580*631cabbaSGleb Smirnoff 	/* Send packet */
581*631cabbaSGleb Smirnoff 	NG_SEND_DATA_ONLY(error, destLink->hook, m2);
582*631cabbaSGleb Smirnoff 	if(error)
583*631cabbaSGleb Smirnoff 	  ctx->error = error;
584*631cabbaSGleb Smirnoff 	return (1);
585*631cabbaSGleb Smirnoff }
586*631cabbaSGleb Smirnoff 
587ed2dbd31SArchie Cobbs static int
588069154d5SJulian Elischer ng_bridge_rcvdata(hook_p hook, item_p item)
589ed2dbd31SArchie Cobbs {
59030400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
59130400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
592ed2dbd31SArchie Cobbs 	struct ng_bridge_host *host;
593ed2dbd31SArchie Cobbs 	struct ether_header *eh;
594*631cabbaSGleb Smirnoff 	struct ng_bridge_send_ctx ctx = { 0 };
595*631cabbaSGleb Smirnoff 	hook_p ret;
596ed2dbd31SArchie Cobbs 
597*631cabbaSGleb Smirnoff 	NGI_GET_M(item, ctx.m);
598ed2dbd31SArchie Cobbs 
599*631cabbaSGleb Smirnoff 	ctx.incoming = NG_HOOK_PRIVATE(hook);
600ed2dbd31SArchie Cobbs 	/* Sanity check packet and pull up header */
601*631cabbaSGleb Smirnoff 	if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) {
602*631cabbaSGleb Smirnoff 		ctx.incoming->stats.recvRunts++;
603069154d5SJulian Elischer 		NG_FREE_ITEM(item);
604*631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
605ed2dbd31SArchie Cobbs 		return (EINVAL);
606ed2dbd31SArchie Cobbs 	}
607*631cabbaSGleb Smirnoff 	if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) {
608*631cabbaSGleb Smirnoff 		ctx.incoming->stats.memoryFailures++;
609069154d5SJulian Elischer 		NG_FREE_ITEM(item);
610ed2dbd31SArchie Cobbs 		return (ENOBUFS);
611ed2dbd31SArchie Cobbs 	}
612*631cabbaSGleb Smirnoff 	eh = mtod(ctx.m, struct ether_header *);
613ed2dbd31SArchie Cobbs 	if ((eh->ether_shost[0] & 1) != 0) {
614*631cabbaSGleb Smirnoff 		ctx.incoming->stats.recvInvalid++;
615069154d5SJulian Elischer 		NG_FREE_ITEM(item);
616*631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
617ed2dbd31SArchie Cobbs 		return (EINVAL);
618ed2dbd31SArchie Cobbs 	}
619ed2dbd31SArchie Cobbs 
620ed2dbd31SArchie Cobbs 	/* Is link disabled due to a loopback condition? */
621*631cabbaSGleb Smirnoff 	if (ctx.incoming->loopCount != 0) {
622*631cabbaSGleb Smirnoff 		ctx.incoming->stats.loopDrops++;
623069154d5SJulian Elischer 		NG_FREE_ITEM(item);
624*631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
625ed2dbd31SArchie Cobbs 		return (ELOOP);		/* XXX is this an appropriate error? */
626ed2dbd31SArchie Cobbs 	}
627ed2dbd31SArchie Cobbs 
628ed2dbd31SArchie Cobbs 	/* Update stats */
629*631cabbaSGleb Smirnoff 	ctx.incoming->stats.recvPackets++;
630*631cabbaSGleb Smirnoff 	ctx.incoming->stats.recvOctets += ctx.m->m_pkthdr.len;
631*631cabbaSGleb Smirnoff 	if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) {
632ed2dbd31SArchie Cobbs 		if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
633*631cabbaSGleb Smirnoff 			ctx.incoming->stats.recvBroadcasts++;
634*631cabbaSGleb Smirnoff 			ctx.manycast = 2;
635ed2dbd31SArchie Cobbs 		} else
636*631cabbaSGleb Smirnoff 			ctx.incoming->stats.recvMulticasts++;
637ed2dbd31SArchie Cobbs 	}
638ed2dbd31SArchie Cobbs 
639ed2dbd31SArchie Cobbs 	/* Look up packet's source Ethernet address in hashtable */
640ed2dbd31SArchie Cobbs 	if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
641ed2dbd31SArchie Cobbs 
642ed2dbd31SArchie Cobbs 		/* Update time since last heard from this host */
643ed2dbd31SArchie Cobbs 		host->staleness = 0;
644ed2dbd31SArchie Cobbs 
645ed2dbd31SArchie Cobbs 		/* Did host jump to a different link? */
646*631cabbaSGleb Smirnoff 		if (host->link != ctx.incoming) {
647ed2dbd31SArchie Cobbs 
648ed2dbd31SArchie Cobbs 			/*
649ed2dbd31SArchie Cobbs 			 * If the host's old link was recently established
650ed2dbd31SArchie Cobbs 			 * on the old link and it's already jumped to a new
651ed2dbd31SArchie Cobbs 			 * link, declare a loopback condition.
652ed2dbd31SArchie Cobbs 			 */
653ed2dbd31SArchie Cobbs 			if (host->age < priv->conf.minStableAge) {
654ed2dbd31SArchie Cobbs 
655ed2dbd31SArchie Cobbs 				/* Log the problem */
656ed2dbd31SArchie Cobbs 				if (priv->conf.debugLevel >= 2) {
657*631cabbaSGleb Smirnoff 					struct ifnet *ifp = ctx.m->m_pkthdr.rcvif;
658ed2dbd31SArchie Cobbs 					char suffix[32];
659ed2dbd31SArchie Cobbs 
660ed2dbd31SArchie Cobbs 					if (ifp != NULL)
661ed2dbd31SArchie Cobbs 						snprintf(suffix, sizeof(suffix),
6629bf40edeSBrooks Davis 						    " (%s)", ifp->if_xname);
663ed2dbd31SArchie Cobbs 					else
664ed2dbd31SArchie Cobbs 						*suffix = '\0';
665ed2dbd31SArchie Cobbs 					log(LOG_WARNING, "ng_bridge: %s:"
666ed2dbd31SArchie Cobbs 					    " loopback detected on %s%s\n",
667ed2dbd31SArchie Cobbs 					    ng_bridge_nodename(node),
66830400f03SJulian Elischer 					    NG_HOOK_NAME(hook), suffix);
669ed2dbd31SArchie Cobbs 				}
670ed2dbd31SArchie Cobbs 
671ed2dbd31SArchie Cobbs 				/* Mark link as linka non grata */
672*631cabbaSGleb Smirnoff 				ctx.incoming->loopCount = priv->conf.loopTimeout;
673*631cabbaSGleb Smirnoff 				ctx.incoming->stats.loopDetects++;
674ed2dbd31SArchie Cobbs 
675ed2dbd31SArchie Cobbs 				/* Forget all hosts on this link */
676*631cabbaSGleb Smirnoff 				ng_bridge_remove_hosts(priv, ctx.incoming);
677ed2dbd31SArchie Cobbs 
678ed2dbd31SArchie Cobbs 				/* Drop packet */
679*631cabbaSGleb Smirnoff 				ctx.incoming->stats.loopDrops++;
680069154d5SJulian Elischer 				NG_FREE_ITEM(item);
681*631cabbaSGleb Smirnoff 				NG_FREE_M(ctx.m);
682ed2dbd31SArchie Cobbs 				return (ELOOP);		/* XXX appropriate? */
683ed2dbd31SArchie Cobbs 			}
684ed2dbd31SArchie Cobbs 
685ed2dbd31SArchie Cobbs 			/* Move host over to new link */
686*631cabbaSGleb Smirnoff 			host->link = ctx.incoming;
687ed2dbd31SArchie Cobbs 			host->age = 0;
688ed2dbd31SArchie Cobbs 		}
689ed2dbd31SArchie Cobbs 	} else {
690*631cabbaSGleb Smirnoff 		if (!ng_bridge_put(priv, eh->ether_shost, ctx.incoming)) {
691*631cabbaSGleb Smirnoff 			ctx.incoming->stats.memoryFailures++;
692069154d5SJulian Elischer 			NG_FREE_ITEM(item);
693*631cabbaSGleb Smirnoff 			NG_FREE_M(ctx.m);
694ed2dbd31SArchie Cobbs 			return (ENOMEM);
695ed2dbd31SArchie Cobbs 		}
696ed2dbd31SArchie Cobbs 	}
697ed2dbd31SArchie Cobbs 
698ed2dbd31SArchie Cobbs 	/* Run packet through ipfw processing, if enabled */
6999b932e9eSAndre Oppermann #if 0
7000b4b0b0fSJulian Elischer 	if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) {
701ed2dbd31SArchie Cobbs 		/* XXX not implemented yet */
702ed2dbd31SArchie Cobbs 	}
7039b932e9eSAndre Oppermann #endif
704ed2dbd31SArchie Cobbs 
705ed2dbd31SArchie Cobbs 	/*
706ed2dbd31SArchie Cobbs 	 * If unicast and destination host known, deliver to host's link,
707ed2dbd31SArchie Cobbs 	 * unless it is the same link as the packet came in on.
708ed2dbd31SArchie Cobbs 	 */
709*631cabbaSGleb Smirnoff 	if (!ctx.manycast) {
710ed2dbd31SArchie Cobbs 
711ed2dbd31SArchie Cobbs 		/* Determine packet destination link */
712ed2dbd31SArchie Cobbs 		if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
713*631cabbaSGleb Smirnoff 			link_p destLink = host->link;
714ed2dbd31SArchie Cobbs 
715ed2dbd31SArchie Cobbs 			/* If destination same as incoming link, do nothing */
716*631cabbaSGleb Smirnoff 			if (destLink == ctx.incoming) {
717069154d5SJulian Elischer 				NG_FREE_ITEM(item);
718*631cabbaSGleb Smirnoff 				NG_FREE_M(ctx.m);
719ed2dbd31SArchie Cobbs 				return (0);
720ed2dbd31SArchie Cobbs 			}
721ed2dbd31SArchie Cobbs 
722ed2dbd31SArchie Cobbs 			/* Deliver packet out the destination link */
723ed2dbd31SArchie Cobbs 			destLink->stats.xmitPackets++;
724*631cabbaSGleb Smirnoff 			destLink->stats.xmitOctets += ctx.m->m_pkthdr.len;
725*631cabbaSGleb Smirnoff 			NG_FWD_NEW_DATA(ctx.error, item, destLink->hook, ctx.m);
726*631cabbaSGleb Smirnoff 			return (ctx.error);
727ed2dbd31SArchie Cobbs 		}
728ed2dbd31SArchie Cobbs 
729ed2dbd31SArchie Cobbs 		/* Destination host is not known */
730*631cabbaSGleb Smirnoff 		ctx.incoming->stats.recvUnknown++;
731ed2dbd31SArchie Cobbs 	}
732ed2dbd31SArchie Cobbs 
733ed2dbd31SArchie Cobbs 	/* Distribute unknown, multicast, broadcast pkts to all other links */
734*631cabbaSGleb Smirnoff 	NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx, ret);
735ed2dbd31SArchie Cobbs 
736069154d5SJulian Elischer 	/* If we never saw a good link, leave. */
737*631cabbaSGleb Smirnoff 	if (ctx.foundFirst == NULL || ctx.error != 0) {
738069154d5SJulian Elischer 		NG_FREE_ITEM(item);
739*631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
740*631cabbaSGleb Smirnoff 		return (ctx.error);
741069154d5SJulian Elischer 	}
742069154d5SJulian Elischer 
743069154d5SJulian Elischer 	/*
744069154d5SJulian Elischer 	 * If we've sent all the others, send the original
745069154d5SJulian Elischer 	 * on the first link we found.
746069154d5SJulian Elischer 	 */
747*631cabbaSGleb Smirnoff 	NG_FWD_NEW_DATA(ctx.error, item, ctx.foundFirst->hook, ctx.m);
748*631cabbaSGleb Smirnoff 	return (ctx.error);
749ed2dbd31SArchie Cobbs }
750ed2dbd31SArchie Cobbs 
751ed2dbd31SArchie Cobbs /*
752ed2dbd31SArchie Cobbs  * Shutdown node
753ed2dbd31SArchie Cobbs  */
754ed2dbd31SArchie Cobbs static int
755069154d5SJulian Elischer ng_bridge_shutdown(node_p node)
756ed2dbd31SArchie Cobbs {
75730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
758ed2dbd31SArchie Cobbs 
7596c12c2b1SArchie Cobbs 	/*
760195cf617SRuslan Ermilov 	 * Shut down everything including the timer.  Even if the
761195cf617SRuslan Ermilov 	 * callout has already been dequeued and is about to be
762195cf617SRuslan Ermilov 	 * run, ng_bridge_timeout() won't be fired as the node
763195cf617SRuslan Ermilov 	 * is already marked NGF_INVALID, so we're safe to free
764195cf617SRuslan Ermilov 	 * the node now.
7656c12c2b1SArchie Cobbs 	 */
766ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
767ed2dbd31SArchie Cobbs 	    ("%s: numLinks=%d numHosts=%d",
7686e551fb6SDavid E. O'Brien 	    __func__, priv->numLinks, priv->numHosts));
769195cf617SRuslan Ermilov 	ng_uncallout(&priv->timer, node);
770195cf617SRuslan Ermilov 	NG_NODE_SET_PRIVATE(node, NULL);
771195cf617SRuslan Ermilov 	NG_NODE_UNREF(node);
7721ede983cSDag-Erling Smørgrav 	free(priv->tab, M_NETGRAPH_BRIDGE);
7731ede983cSDag-Erling Smørgrav 	free(priv, M_NETGRAPH_BRIDGE);
774ed2dbd31SArchie Cobbs 	return (0);
775ed2dbd31SArchie Cobbs }
776ed2dbd31SArchie Cobbs 
777ed2dbd31SArchie Cobbs /*
778ed2dbd31SArchie Cobbs  * Hook disconnection.
779ed2dbd31SArchie Cobbs  */
780ed2dbd31SArchie Cobbs static int
781ed2dbd31SArchie Cobbs ng_bridge_disconnect(hook_p hook)
782ed2dbd31SArchie Cobbs {
78330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
784*631cabbaSGleb Smirnoff 	link_p link = NG_HOOK_PRIVATE(hook);
785ed2dbd31SArchie Cobbs 
786ed2dbd31SArchie Cobbs 	/* Remove all hosts associated with this link */
787*631cabbaSGleb Smirnoff 	ng_bridge_remove_hosts(priv, link);
788ed2dbd31SArchie Cobbs 
789ed2dbd31SArchie Cobbs 	/* Free associated link information */
790*631cabbaSGleb Smirnoff 	free(link, M_NETGRAPH_BRIDGE);
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)
795f8aab721SMarko Zec 	    && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
796f8aab721SMarko Zec 	    && !priv->persistent) {
79730400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));
79830400f03SJulian Elischer 	}
799ed2dbd31SArchie Cobbs 	return (0);
800ed2dbd31SArchie Cobbs }
801ed2dbd31SArchie Cobbs 
802ed2dbd31SArchie Cobbs /******************************************************************
803ed2dbd31SArchie Cobbs 		    HASH TABLE FUNCTIONS
804ed2dbd31SArchie Cobbs ******************************************************************/
805ed2dbd31SArchie Cobbs 
806ed2dbd31SArchie Cobbs /*
807ed2dbd31SArchie Cobbs  * Hash algorithm
808ed2dbd31SArchie Cobbs  */
809ed2dbd31SArchie Cobbs #define HASH(addr,mask)		( (((const u_int16_t *)(addr))[0] 	\
810ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[1] 	\
811ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
812ed2dbd31SArchie Cobbs 
813ed2dbd31SArchie Cobbs /*
814ed2dbd31SArchie Cobbs  * Find a host entry in the table.
815ed2dbd31SArchie Cobbs  */
816ed2dbd31SArchie Cobbs static struct ng_bridge_host *
817ed2dbd31SArchie Cobbs ng_bridge_get(priv_p priv, const u_char *addr)
818ed2dbd31SArchie Cobbs {
819ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
820ed2dbd31SArchie Cobbs 	struct ng_bridge_hent *hent;
821ed2dbd31SArchie Cobbs 
822ed2dbd31SArchie Cobbs 	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
823ed2dbd31SArchie Cobbs 		if (ETHER_EQUAL(hent->host.addr, addr))
824ed2dbd31SArchie Cobbs 			return (&hent->host);
825ed2dbd31SArchie Cobbs 	}
826ed2dbd31SArchie Cobbs 	return (NULL);
827ed2dbd31SArchie Cobbs }
828ed2dbd31SArchie Cobbs 
829ed2dbd31SArchie Cobbs /*
830ed2dbd31SArchie Cobbs  * Add a new host entry to the table. This assumes the host doesn't
831ed2dbd31SArchie Cobbs  * already exist in the table. Returns 1 on success, 0 if there
832ed2dbd31SArchie Cobbs  * was a memory allocation failure.
833ed2dbd31SArchie Cobbs  */
834ed2dbd31SArchie Cobbs static int
835*631cabbaSGleb Smirnoff ng_bridge_put(priv_p priv, const u_char *addr, link_p link)
836ed2dbd31SArchie Cobbs {
837ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
838ed2dbd31SArchie Cobbs 	struct ng_bridge_hent *hent;
839ed2dbd31SArchie Cobbs 
840ed2dbd31SArchie Cobbs #ifdef INVARIANTS
841ed2dbd31SArchie Cobbs 	/* Assert that entry does not already exist in hashtable */
842ed2dbd31SArchie Cobbs 	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
843ed2dbd31SArchie Cobbs 		KASSERT(!ETHER_EQUAL(hent->host.addr, addr),
8446e551fb6SDavid E. O'Brien 		    ("%s: entry %6D exists in table", __func__, addr, ":"));
845ed2dbd31SArchie Cobbs 	}
846ed2dbd31SArchie Cobbs #endif
847ed2dbd31SArchie Cobbs 
848ed2dbd31SArchie Cobbs 	/* Allocate and initialize new hashtable entry */
8491ede983cSDag-Erling Smørgrav 	hent = malloc(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);
853*631cabbaSGleb Smirnoff 	hent->host.link = link;
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 */
893ac2fffa4SPedro F. Giffuni 	newTab = malloc(newNumBuckets * sizeof(*newTab),
894e11e3f18SDag-Erling Smørgrav 	    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 	}
9181ede983cSDag-Erling Smørgrav 	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 
929*631cabbaSGleb Smirnoff 
930ed2dbd31SArchie Cobbs /*
931ed2dbd31SArchie Cobbs  * Remove all hosts associated with a specific link from the hashtable.
932ed2dbd31SArchie Cobbs  * If linkNum == -1, then remove all hosts in the table.
933ed2dbd31SArchie Cobbs  */
934ed2dbd31SArchie Cobbs static void
935*631cabbaSGleb Smirnoff ng_bridge_remove_hosts(priv_p priv, link_p link)
936ed2dbd31SArchie Cobbs {
937ed2dbd31SArchie Cobbs 	int bucket;
938ed2dbd31SArchie Cobbs 
939ed2dbd31SArchie Cobbs 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
940ed2dbd31SArchie Cobbs 		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
941ed2dbd31SArchie Cobbs 
942ed2dbd31SArchie Cobbs 		while (*hptr != NULL) {
943ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent = *hptr;
944ed2dbd31SArchie Cobbs 
945*631cabbaSGleb Smirnoff 			if (link == NULL || hent->host.link == link) {
946ed2dbd31SArchie Cobbs 				*hptr = SLIST_NEXT(hent, next);
9471ede983cSDag-Erling Smørgrav 				free(hent, M_NETGRAPH_BRIDGE);
948ed2dbd31SArchie Cobbs 				priv->numHosts--;
949ed2dbd31SArchie Cobbs 			} else
950ed2dbd31SArchie Cobbs 				hptr = &SLIST_NEXT(hent, next);
951ed2dbd31SArchie Cobbs 		}
952ed2dbd31SArchie Cobbs 	}
953ed2dbd31SArchie Cobbs }
954ed2dbd31SArchie Cobbs 
955ed2dbd31SArchie Cobbs /*
956ed2dbd31SArchie Cobbs  * Handle our once-per-second timeout event. We do two things:
957ed2dbd31SArchie Cobbs  * we decrement link->loopCount for those links being muted due to
958ed2dbd31SArchie Cobbs  * a detected loopback condition, and we remove any hosts from
959ed2dbd31SArchie Cobbs  * the hashtable whom we haven't heard from in a long while.
960ed2dbd31SArchie Cobbs  */
961*631cabbaSGleb Smirnoff static int
962*631cabbaSGleb Smirnoff ng_bridge_unmute(hook_p hook, int *counter)
963*631cabbaSGleb Smirnoff {
964*631cabbaSGleb Smirnoff 	link_p link = NG_HOOK_PRIVATE(hook);
965*631cabbaSGleb Smirnoff 	node_p node = NG_HOOK_NODE(hook);
966*631cabbaSGleb Smirnoff 	priv_p priv = NG_NODE_PRIVATE(node);
967*631cabbaSGleb Smirnoff 
968*631cabbaSGleb Smirnoff 	if (link->loopCount != 0) {
969*631cabbaSGleb Smirnoff 		link->loopCount--;
970*631cabbaSGleb Smirnoff 		if (link->loopCount == 0 && priv->conf.debugLevel >= 2) {
971*631cabbaSGleb Smirnoff 			log(LOG_INFO, "ng_bridge: %s:"
972*631cabbaSGleb Smirnoff 			    " restoring looped back %s\n",
973*631cabbaSGleb Smirnoff 			    ng_bridge_nodename(node), NG_HOOK_NAME(hook));
974*631cabbaSGleb Smirnoff 		}
975*631cabbaSGleb Smirnoff 	}
976*631cabbaSGleb Smirnoff 	counter++;
977*631cabbaSGleb Smirnoff 	return (1);
978*631cabbaSGleb Smirnoff }
979*631cabbaSGleb Smirnoff 
980ed2dbd31SArchie Cobbs static void
981e0d32af7SGleb Smirnoff ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2)
982ed2dbd31SArchie Cobbs {
98330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
984e0d32af7SGleb Smirnoff 	int bucket;
985ed2dbd31SArchie Cobbs 	int counter = 0;
986*631cabbaSGleb Smirnoff 	hook_p ret;
987ed2dbd31SArchie Cobbs 
988ed2dbd31SArchie Cobbs 	/* Update host time counters and remove stale entries */
989ed2dbd31SArchie Cobbs 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
990ed2dbd31SArchie Cobbs 		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
991ed2dbd31SArchie Cobbs 
992ed2dbd31SArchie Cobbs 		while (*hptr != NULL) {
993ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent = *hptr;
994ed2dbd31SArchie Cobbs 
995ed2dbd31SArchie Cobbs 			/* Remove hosts we haven't heard from in a while */
996ed2dbd31SArchie Cobbs 			if (++hent->host.staleness >= priv->conf.maxStaleness) {
997ed2dbd31SArchie Cobbs 				*hptr = SLIST_NEXT(hent, next);
9981ede983cSDag-Erling Smørgrav 				free(hent, M_NETGRAPH_BRIDGE);
999ed2dbd31SArchie Cobbs 				priv->numHosts--;
1000ed2dbd31SArchie Cobbs 			} else {
1001ed2dbd31SArchie Cobbs 				if (hent->host.age < 0xffff)
1002ed2dbd31SArchie Cobbs 					hent->host.age++;
1003ed2dbd31SArchie Cobbs 				hptr = &SLIST_NEXT(hent, next);
1004ed2dbd31SArchie Cobbs 				counter++;
1005ed2dbd31SArchie Cobbs 			}
1006ed2dbd31SArchie Cobbs 		}
1007ed2dbd31SArchie Cobbs 	}
1008ed2dbd31SArchie Cobbs 	KASSERT(priv->numHosts == counter,
10096e551fb6SDavid E. O'Brien 	    ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
1010ed2dbd31SArchie Cobbs 
1011ed2dbd31SArchie Cobbs 	/* Decrease table size if necessary */
1012ed2dbd31SArchie Cobbs 	ng_bridge_rehash(priv);
1013ed2dbd31SArchie Cobbs 
1014ed2dbd31SArchie Cobbs 	/* Decrease loop counter on muted looped back links */
1015*631cabbaSGleb Smirnoff 	counter = 0;
1016*631cabbaSGleb Smirnoff 	NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter, ret);
1017ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == counter,
10186e551fb6SDavid E. O'Brien 	    ("%s: links: %d != %d", __func__, priv->numLinks, counter));
1019ed2dbd31SArchie Cobbs 
1020e0d32af7SGleb Smirnoff 	/* Register a new timeout, keeping the existing node reference */
1021e0d32af7SGleb Smirnoff 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
1022ed2dbd31SArchie Cobbs }
1023ed2dbd31SArchie Cobbs 
1024ed2dbd31SArchie Cobbs /*
1025ed2dbd31SArchie Cobbs  * Return node's "name", even if it doesn't have one.
1026ed2dbd31SArchie Cobbs  */
1027ed2dbd31SArchie Cobbs static const char *
1028ed2dbd31SArchie Cobbs ng_bridge_nodename(node_p node)
1029ed2dbd31SArchie Cobbs {
103087e2c66aSHartmut Brandt 	static char name[NG_NODESIZ];
1031ed2dbd31SArchie Cobbs 
1032dd10c1e2SGleb Smirnoff 	if (NG_NODE_HAS_NAME(node))
103330400f03SJulian Elischer 		snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
1034ed2dbd31SArchie Cobbs 	else
1035ed2dbd31SArchie Cobbs 		snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
1036ed2dbd31SArchie Cobbs 	return name;
1037ed2dbd31SArchie Cobbs }
1038ed2dbd31SArchie Cobbs 
1039