xref: /freebsd/sys/netgraph/ng_bridge.c (revision 9c8c302fd01fe6dcb6d24d7b6a36e473809533dd)
1ed2dbd31SArchie Cobbs 
2ed2dbd31SArchie Cobbs /*
3ed2dbd31SArchie Cobbs  * ng_bridge.c
4ed2dbd31SArchie Cobbs  *
5ed2dbd31SArchie Cobbs  * Copyright (c) 2000 Whistle Communications, Inc.
6ed2dbd31SArchie Cobbs  * All rights reserved.
7ed2dbd31SArchie Cobbs  *
8ed2dbd31SArchie Cobbs  * Subject to the following obligations and disclaimer of warranty, use and
9ed2dbd31SArchie Cobbs  * redistribution of this software, in source or object code forms, with or
10ed2dbd31SArchie Cobbs  * without modifications are expressly permitted by Whistle Communications;
11ed2dbd31SArchie Cobbs  * provided, however, that:
12ed2dbd31SArchie Cobbs  * 1. Any and all reproductions of the source or object code must include the
13ed2dbd31SArchie Cobbs  *    copyright notice above and the following disclaimer of warranties; and
14ed2dbd31SArchie Cobbs  * 2. No rights are granted, in any manner or form, to use Whistle
15ed2dbd31SArchie Cobbs  *    Communications, Inc. trademarks, including the mark "WHISTLE
16ed2dbd31SArchie Cobbs  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17ed2dbd31SArchie Cobbs  *    such appears in the above copyright notice or in the software.
18ed2dbd31SArchie Cobbs  *
19ed2dbd31SArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20ed2dbd31SArchie Cobbs  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21ed2dbd31SArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22ed2dbd31SArchie Cobbs  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23ed2dbd31SArchie Cobbs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24ed2dbd31SArchie Cobbs  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25ed2dbd31SArchie Cobbs  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26ed2dbd31SArchie Cobbs  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27ed2dbd31SArchie Cobbs  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28ed2dbd31SArchie Cobbs  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29ed2dbd31SArchie Cobbs  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30ed2dbd31SArchie Cobbs  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31ed2dbd31SArchie Cobbs  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32ed2dbd31SArchie Cobbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33ed2dbd31SArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34ed2dbd31SArchie Cobbs  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35ed2dbd31SArchie Cobbs  * OF SUCH DAMAGE.
36ed2dbd31SArchie Cobbs  *
37ed2dbd31SArchie Cobbs  * Author: Archie Cobbs <archie@freebsd.org>
38ed2dbd31SArchie Cobbs  *
39ed2dbd31SArchie Cobbs  * $FreeBSD$
40ed2dbd31SArchie Cobbs  */
41ed2dbd31SArchie Cobbs 
42ed2dbd31SArchie Cobbs /*
43ed2dbd31SArchie Cobbs  * ng_bridge(4) netgraph node type
44ed2dbd31SArchie Cobbs  *
45ed2dbd31SArchie Cobbs  * The node performs standard intelligent Ethernet bridging over
46ed2dbd31SArchie Cobbs  * each of its connected hooks, or links.  A simple loop detection
47ed2dbd31SArchie Cobbs  * algorithm is included which disables a link for priv->conf.loopTimeout
48ed2dbd31SArchie Cobbs  * seconds when a host is seen to have jumped from one link to
49ed2dbd31SArchie Cobbs  * another within priv->conf.minStableAge seconds.
50ed2dbd31SArchie Cobbs  *
51ed2dbd31SArchie Cobbs  * We keep a hashtable that maps Ethernet addresses to host info,
52ed2dbd31SArchie Cobbs  * which is contained in struct ng_bridge_host's. These structures
53ed2dbd31SArchie Cobbs  * tell us on which link the host may be found. A host's entry will
54ed2dbd31SArchie Cobbs  * expire after priv->conf.maxStaleness seconds.
55ed2dbd31SArchie Cobbs  *
56ed2dbd31SArchie Cobbs  * This node is optimzed for stable networks, where machines jump
57ed2dbd31SArchie Cobbs  * from one port to the other only rarely.
58ed2dbd31SArchie Cobbs  */
59ed2dbd31SArchie Cobbs 
60ed2dbd31SArchie Cobbs #include <sys/param.h>
61ed2dbd31SArchie Cobbs #include <sys/systm.h>
62ed2dbd31SArchie Cobbs #include <sys/kernel.h>
63ed2dbd31SArchie Cobbs #include <sys/malloc.h>
64ed2dbd31SArchie Cobbs #include <sys/mbuf.h>
65ed2dbd31SArchie Cobbs #include <sys/errno.h>
66ed2dbd31SArchie Cobbs #include <sys/syslog.h>
67ed2dbd31SArchie Cobbs #include <sys/socket.h>
68ed2dbd31SArchie Cobbs #include <sys/ctype.h>
69ed2dbd31SArchie Cobbs 
70ed2dbd31SArchie Cobbs #include <net/if.h>
71ed2dbd31SArchie Cobbs #include <net/ethernet.h>
72ed2dbd31SArchie Cobbs 
73ed2dbd31SArchie Cobbs #include <netinet/in.h>
74ed2dbd31SArchie Cobbs #include <netinet/ip_fw.h>
75ed2dbd31SArchie Cobbs 
76ed2dbd31SArchie Cobbs #include <netgraph/ng_message.h>
77ed2dbd31SArchie Cobbs #include <netgraph/netgraph.h>
78ed2dbd31SArchie Cobbs #include <netgraph/ng_parse.h>
79ed2dbd31SArchie Cobbs #include <netgraph/ng_bridge.h>
80ed2dbd31SArchie Cobbs #include <netgraph/ng_ether.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);
131ed2dbd31SArchie Cobbs static void	ng_bridge_timeout(void *arg);
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 */
174ed2dbd31SArchie Cobbs static const struct ng_parse_struct_info ng_bridge_host_type_info
175ed2dbd31SArchie Cobbs 	= NG_BRIDGE_HOST_TYPE_INFO(&ng_ether_enaddr_type);
176ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_type = {
177ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
178ed2dbd31SArchie Cobbs 	&ng_bridge_host_type_info
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 };
188ed2dbd31SArchie Cobbs static const struct ng_parse_struct_info ng_bridge_host_ary_type_info
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,
192ed2dbd31SArchie Cobbs 	&ng_bridge_host_ary_type_info
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 };
204ed2dbd31SArchie Cobbs static const struct ng_parse_struct_info ng_bridge_config_type_info
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,
208ed2dbd31SArchie Cobbs 	&ng_bridge_config_type_info
209ed2dbd31SArchie Cobbs };
210ed2dbd31SArchie Cobbs 
211ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_link_stat */
212ed2dbd31SArchie Cobbs static const struct ng_parse_struct_info
213ed2dbd31SArchie Cobbs 	ng_bridge_stats_type_info = NG_BRIDGE_STATS_TYPE_INFO;
214ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_stats_type = {
215ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
216ed2dbd31SArchie Cobbs 	&ng_bridge_stats_type_info
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 = {
275589f6ed8SJulian Elischer 	NG_ABI_VERSION,
276ed2dbd31SArchie Cobbs 	NG_BRIDGE_NODE_TYPE,
277ed2dbd31SArchie Cobbs 	NULL,
278ed2dbd31SArchie Cobbs 	ng_bridge_constructor,
279ed2dbd31SArchie Cobbs 	ng_bridge_rcvmsg,
280069154d5SJulian Elischer 	ng_bridge_shutdown,
281ed2dbd31SArchie Cobbs 	ng_bridge_newhook,
282ed2dbd31SArchie Cobbs 	NULL,
283ed2dbd31SArchie Cobbs 	NULL,
284ed2dbd31SArchie Cobbs 	ng_bridge_rcvdata,
285ed2dbd31SArchie Cobbs 	ng_bridge_disconnect,
286ed2dbd31SArchie Cobbs 	ng_bridge_cmdlist,
287ed2dbd31SArchie Cobbs };
288a89effcdSArchie Cobbs NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
289ed2dbd31SArchie Cobbs 
290ed2dbd31SArchie Cobbs /* Depend on ng_ether so we can use the Ethernet parse type */
291ed2dbd31SArchie Cobbs MODULE_DEPEND(ng_bridge, ng_ether, 1, 1, 1);
292ed2dbd31SArchie Cobbs 
293ed2dbd31SArchie Cobbs /******************************************************************
294ed2dbd31SArchie Cobbs 		    NETGRAPH NODE METHODS
295ed2dbd31SArchie Cobbs ******************************************************************/
296ed2dbd31SArchie Cobbs 
297ed2dbd31SArchie Cobbs /*
298ed2dbd31SArchie Cobbs  * Node constructor
299ed2dbd31SArchie Cobbs  */
300ed2dbd31SArchie Cobbs static int
301069154d5SJulian Elischer ng_bridge_constructor(node_p node)
302ed2dbd31SArchie Cobbs {
303ed2dbd31SArchie Cobbs 	priv_p priv;
304ed2dbd31SArchie Cobbs 
305ed2dbd31SArchie Cobbs 	/* Allocate and initialize private info */
3069c8c302fSJulian Elischer 	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
307ed2dbd31SArchie Cobbs 	if (priv == NULL)
308ed2dbd31SArchie Cobbs 		return (ENOMEM);
309fe96e26dSJonathan Lemon 	callout_init(&priv->timer, 0);
310ed2dbd31SArchie Cobbs 
311ed2dbd31SArchie Cobbs 	/* Allocate and initialize hash table, etc. */
312ed2dbd31SArchie Cobbs 	MALLOC(priv->tab, struct ng_bridge_bucket *,
3139c8c302fSJulian Elischer 	    MIN_BUCKETS * sizeof(*priv->tab), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
314ed2dbd31SArchie Cobbs 	if (priv->tab == NULL) {
3159c8c302fSJulian Elischer 		FREE(priv, M_NETGRAPH_BRIDGE);
316ed2dbd31SArchie Cobbs 		return (ENOMEM);
317ed2dbd31SArchie Cobbs 	}
318ed2dbd31SArchie Cobbs 	priv->numBuckets = MIN_BUCKETS;
319ed2dbd31SArchie Cobbs 	priv->hashMask = MIN_BUCKETS - 1;
320ed2dbd31SArchie Cobbs 	priv->conf.debugLevel = 1;
321ed2dbd31SArchie Cobbs 	priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
322ed2dbd31SArchie Cobbs 	priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
323ed2dbd31SArchie Cobbs 	priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
324ed2dbd31SArchie Cobbs 
325069154d5SJulian Elischer 	/*
326069154d5SJulian Elischer 	 * This node has all kinds of stuff that could be screwed by SMP.
327069154d5SJulian Elischer 	 * Until it gets it's own internal protection, we go through in
328069154d5SJulian Elischer 	 * single file. This could hurt a machine bridging beteen two
329069154d5SJulian Elischer 	 * GB ethernets so it should be fixed.
330069154d5SJulian Elischer 	 * When it's fixed the process SHOULD NOT SLEEP, spinlocks please!
331069154d5SJulian Elischer 	 * (and atomic ops )
332069154d5SJulian Elischer 	 */
33330400f03SJulian Elischer 	NG_NODE_FORCE_WRITER(node);
33430400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
335069154d5SJulian Elischer 	priv->node = node;
336ed2dbd31SArchie Cobbs 
337ed2dbd31SArchie Cobbs 	/* Start timer by faking a timeout event */
33830400f03SJulian Elischer 	NG_NODE_REF(node); /* because the timeout will drop a reference */
339069154d5SJulian Elischer 	ng_bridge_timeout(node);
340ed2dbd31SArchie Cobbs 	return (0);
341ed2dbd31SArchie Cobbs }
342ed2dbd31SArchie Cobbs 
343ed2dbd31SArchie Cobbs /*
344ed2dbd31SArchie Cobbs  * Method for attaching a new hook
345ed2dbd31SArchie Cobbs  */
346ed2dbd31SArchie Cobbs static	int
347ed2dbd31SArchie Cobbs ng_bridge_newhook(node_p node, hook_p hook, const char *name)
348ed2dbd31SArchie Cobbs {
34930400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
350ed2dbd31SArchie Cobbs 
351ed2dbd31SArchie Cobbs 	/* Check for a link hook */
352ed2dbd31SArchie Cobbs 	if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX,
353ed2dbd31SArchie Cobbs 	    strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) {
354ed2dbd31SArchie Cobbs 		const char *cp;
355ed2dbd31SArchie Cobbs 		char *eptr;
356ed2dbd31SArchie Cobbs 		u_long linkNum;
357ed2dbd31SArchie Cobbs 
358ed2dbd31SArchie Cobbs 		cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX);
359ed2dbd31SArchie Cobbs 		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
360ed2dbd31SArchie Cobbs 			return (EINVAL);
361ed2dbd31SArchie Cobbs 		linkNum = strtoul(cp, &eptr, 10);
362ed2dbd31SArchie Cobbs 		if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS)
363ed2dbd31SArchie Cobbs 			return (EINVAL);
364ed2dbd31SArchie Cobbs 		if (priv->links[linkNum] != NULL)
365ed2dbd31SArchie Cobbs 			return (EISCONN);
366ed2dbd31SArchie Cobbs 		MALLOC(priv->links[linkNum], struct ng_bridge_link *,
3679c8c302fSJulian Elischer 		    sizeof(*priv->links[linkNum]), M_NETGRAPH_BRIDGE, M_NOWAIT|M_ZERO);
368ed2dbd31SArchie Cobbs 		if (priv->links[linkNum] == NULL)
369ed2dbd31SArchie Cobbs 			return (ENOMEM);
370ed2dbd31SArchie Cobbs 		priv->links[linkNum]->hook = hook;
37130400f03SJulian Elischer 		NG_HOOK_SET_PRIVATE(hook, (void *)linkNum);
372ed2dbd31SArchie Cobbs 		priv->numLinks++;
373ed2dbd31SArchie Cobbs 		return (0);
374ed2dbd31SArchie Cobbs 	}
375ed2dbd31SArchie Cobbs 
376ed2dbd31SArchie Cobbs 	/* Unknown hook name */
377ed2dbd31SArchie Cobbs 	return (EINVAL);
378ed2dbd31SArchie Cobbs }
379ed2dbd31SArchie Cobbs 
380ed2dbd31SArchie Cobbs /*
381ed2dbd31SArchie Cobbs  * Receive a control message
382ed2dbd31SArchie Cobbs  */
383ed2dbd31SArchie Cobbs static int
384069154d5SJulian Elischer ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
385ed2dbd31SArchie Cobbs {
38630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
387ed2dbd31SArchie Cobbs 	struct ng_mesg *resp = NULL;
388ed2dbd31SArchie Cobbs 	int error = 0;
389069154d5SJulian Elischer 	struct ng_mesg *msg;
390ed2dbd31SArchie Cobbs 
391069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
392ed2dbd31SArchie Cobbs 	switch (msg->header.typecookie) {
393ed2dbd31SArchie Cobbs 	case NGM_BRIDGE_COOKIE:
394ed2dbd31SArchie Cobbs 		switch (msg->header.cmd) {
395ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_CONFIG:
396ed2dbd31SArchie Cobbs 		    {
397ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
398ed2dbd31SArchie Cobbs 
399ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg,
400ed2dbd31SArchie Cobbs 			    sizeof(struct ng_bridge_config), M_NOWAIT);
401ed2dbd31SArchie Cobbs 			if (resp == NULL) {
402ed2dbd31SArchie Cobbs 				error = ENOMEM;
403ed2dbd31SArchie Cobbs 				break;
404ed2dbd31SArchie Cobbs 			}
405ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)resp->data;
406ed2dbd31SArchie Cobbs 			*conf = priv->conf;	/* no sanity checking needed */
407ed2dbd31SArchie Cobbs 			break;
408ed2dbd31SArchie Cobbs 		    }
409ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_SET_CONFIG:
410ed2dbd31SArchie Cobbs 		    {
411ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
412ed2dbd31SArchie Cobbs 			int i;
413ed2dbd31SArchie Cobbs 
414ed2dbd31SArchie Cobbs 			if (msg->header.arglen
415ed2dbd31SArchie Cobbs 			    != sizeof(struct ng_bridge_config)) {
416ed2dbd31SArchie Cobbs 				error = EINVAL;
417ed2dbd31SArchie Cobbs 				break;
418ed2dbd31SArchie Cobbs 			}
419ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)msg->data;
420ed2dbd31SArchie Cobbs 			priv->conf = *conf;
421ed2dbd31SArchie Cobbs 			for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++)
422ed2dbd31SArchie Cobbs 				priv->conf.ipfw[i] = !!priv->conf.ipfw[i];
423ed2dbd31SArchie Cobbs 			break;
424ed2dbd31SArchie Cobbs 		    }
425ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_RESET:
426ed2dbd31SArchie Cobbs 		    {
427ed2dbd31SArchie Cobbs 			int i;
428ed2dbd31SArchie Cobbs 
429ed2dbd31SArchie Cobbs 			/* Flush all entries in the hash table */
430ed2dbd31SArchie Cobbs 			ng_bridge_remove_hosts(priv, -1);
431ed2dbd31SArchie Cobbs 
432ed2dbd31SArchie Cobbs 			/* Reset all loop detection counters and stats */
433ed2dbd31SArchie Cobbs 			for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) {
434ed2dbd31SArchie Cobbs 				if (priv->links[i] == NULL)
435ed2dbd31SArchie Cobbs 					continue;
436ed2dbd31SArchie Cobbs 				priv->links[i]->loopCount = 0;
437ed2dbd31SArchie Cobbs 				bzero(&priv->links[i]->stats,
438ed2dbd31SArchie Cobbs 				    sizeof(priv->links[i]->stats));
439ed2dbd31SArchie Cobbs 			}
440ed2dbd31SArchie Cobbs 			break;
441ed2dbd31SArchie Cobbs 		    }
442ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_STATS:
443ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_CLR_STATS:
444ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GETCLR_STATS:
445ed2dbd31SArchie Cobbs 		    {
446ed2dbd31SArchie Cobbs 			struct ng_bridge_link *link;
447ed2dbd31SArchie Cobbs 			int linkNum;
448ed2dbd31SArchie Cobbs 
449ed2dbd31SArchie Cobbs 			/* Get link number */
450ed2dbd31SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
451ed2dbd31SArchie Cobbs 				error = EINVAL;
452ed2dbd31SArchie Cobbs 				break;
453ed2dbd31SArchie Cobbs 			}
454ed2dbd31SArchie Cobbs 			linkNum = *((u_int32_t *)msg->data);
455ed2dbd31SArchie Cobbs 			if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) {
456ed2dbd31SArchie Cobbs 				error = EINVAL;
457ed2dbd31SArchie Cobbs 				break;
458ed2dbd31SArchie Cobbs 			}
459ed2dbd31SArchie Cobbs 			if ((link = priv->links[linkNum]) == NULL) {
460ed2dbd31SArchie Cobbs 				error = ENOTCONN;
461ed2dbd31SArchie Cobbs 				break;
462ed2dbd31SArchie Cobbs 			}
463ed2dbd31SArchie Cobbs 
464ed2dbd31SArchie Cobbs 			/* Get/clear stats */
465ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
466ed2dbd31SArchie Cobbs 				NG_MKRESPONSE(resp, msg,
467ed2dbd31SArchie Cobbs 				    sizeof(link->stats), M_NOWAIT);
468ed2dbd31SArchie Cobbs 				if (resp == NULL) {
469ed2dbd31SArchie Cobbs 					error = ENOMEM;
470ed2dbd31SArchie Cobbs 					break;
471ed2dbd31SArchie Cobbs 				}
472ed2dbd31SArchie Cobbs 				bcopy(&link->stats,
473ed2dbd31SArchie Cobbs 				    resp->data, sizeof(link->stats));
474ed2dbd31SArchie Cobbs 			}
475ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
476ed2dbd31SArchie Cobbs 				bzero(&link->stats, sizeof(link->stats));
477ed2dbd31SArchie Cobbs 			break;
478ed2dbd31SArchie Cobbs 		    }
479ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_TABLE:
480ed2dbd31SArchie Cobbs 		    {
481ed2dbd31SArchie Cobbs 			struct ng_bridge_host_ary *ary;
482ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *hent;
483ed2dbd31SArchie Cobbs 			int i = 0, bucket;
484ed2dbd31SArchie Cobbs 
485ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*ary)
486ed2dbd31SArchie Cobbs 			    + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
487ed2dbd31SArchie Cobbs 			if (resp == NULL) {
488ed2dbd31SArchie Cobbs 				error = ENOMEM;
489ed2dbd31SArchie Cobbs 				break;
490ed2dbd31SArchie Cobbs 			}
491ed2dbd31SArchie Cobbs 			ary = (struct ng_bridge_host_ary *)resp->data;
492ed2dbd31SArchie Cobbs 			ary->numHosts = priv->numHosts;
493ed2dbd31SArchie Cobbs 			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
494ed2dbd31SArchie Cobbs 				SLIST_FOREACH(hent, &priv->tab[bucket], next)
495ed2dbd31SArchie Cobbs 					ary->hosts[i++] = hent->host;
496ed2dbd31SArchie Cobbs 			}
497ed2dbd31SArchie Cobbs 			break;
498ed2dbd31SArchie Cobbs 		    }
499ed2dbd31SArchie Cobbs 		default:
500ed2dbd31SArchie Cobbs 			error = EINVAL;
501ed2dbd31SArchie Cobbs 			break;
502ed2dbd31SArchie Cobbs 		}
503ed2dbd31SArchie Cobbs 		break;
504ed2dbd31SArchie Cobbs 	default:
505ed2dbd31SArchie Cobbs 		error = EINVAL;
506ed2dbd31SArchie Cobbs 		break;
507ed2dbd31SArchie Cobbs 	}
508ed2dbd31SArchie Cobbs 
509ed2dbd31SArchie Cobbs 	/* Done */
510069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
511069154d5SJulian Elischer 	NG_FREE_MSG(msg);
512ed2dbd31SArchie Cobbs 	return (error);
513ed2dbd31SArchie Cobbs }
514ed2dbd31SArchie Cobbs 
515ed2dbd31SArchie Cobbs /*
516ed2dbd31SArchie Cobbs  * Receive data on a hook
517ed2dbd31SArchie Cobbs  */
518ed2dbd31SArchie Cobbs static int
519069154d5SJulian Elischer ng_bridge_rcvdata(hook_p hook, item_p item)
520ed2dbd31SArchie Cobbs {
52130400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
52230400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
523ed2dbd31SArchie Cobbs 	struct ng_bridge_host *host;
524ed2dbd31SArchie Cobbs 	struct ng_bridge_link *link;
525ed2dbd31SArchie Cobbs 	struct ether_header *eh;
526ed2dbd31SArchie Cobbs 	int error = 0, linkNum;
527069154d5SJulian Elischer 	int manycast;
528069154d5SJulian Elischer 	struct mbuf *m;
529069154d5SJulian Elischer 	meta_p meta;
530069154d5SJulian Elischer 	struct ng_bridge_link *firstLink;
531ed2dbd31SArchie Cobbs 
532069154d5SJulian Elischer 	NGI_GET_M(item, m);
533ed2dbd31SArchie Cobbs 	/* Get link number */
53430400f03SJulian Elischer 	linkNum = (int)NG_HOOK_PRIVATE(hook);
535ed2dbd31SArchie Cobbs 	KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
536ed2dbd31SArchie Cobbs 	    ("%s: linkNum=%u", __FUNCTION__, linkNum));
537ed2dbd31SArchie Cobbs 	link = priv->links[linkNum];
538ed2dbd31SArchie Cobbs 	KASSERT(link != NULL, ("%s: link%d null", __FUNCTION__, linkNum));
539ed2dbd31SArchie Cobbs 
540ed2dbd31SArchie Cobbs 	/* Sanity check packet and pull up header */
541ed2dbd31SArchie Cobbs 	if (m->m_pkthdr.len < ETHER_HDR_LEN) {
542ed2dbd31SArchie Cobbs 		link->stats.recvRunts++;
543069154d5SJulian Elischer 		NG_FREE_ITEM(item);
544069154d5SJulian Elischer 		NG_FREE_M(m);
545ed2dbd31SArchie Cobbs 		return (EINVAL);
546ed2dbd31SArchie Cobbs 	}
547ed2dbd31SArchie Cobbs 	if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) {
548ed2dbd31SArchie Cobbs 		link->stats.memoryFailures++;
549069154d5SJulian Elischer 		NG_FREE_ITEM(item);
550ed2dbd31SArchie Cobbs 		return (ENOBUFS);
551ed2dbd31SArchie Cobbs 	}
552ed2dbd31SArchie Cobbs 	eh = mtod(m, struct ether_header *);
553ed2dbd31SArchie Cobbs 	if ((eh->ether_shost[0] & 1) != 0) {
554ed2dbd31SArchie Cobbs 		link->stats.recvInvalid++;
555069154d5SJulian Elischer 		NG_FREE_ITEM(item);
556069154d5SJulian Elischer 		NG_FREE_M(m);
557ed2dbd31SArchie Cobbs 		return (EINVAL);
558ed2dbd31SArchie Cobbs 	}
559ed2dbd31SArchie Cobbs 
560ed2dbd31SArchie Cobbs 	/* Is link disabled due to a loopback condition? */
561ed2dbd31SArchie Cobbs 	if (link->loopCount != 0) {
562ed2dbd31SArchie Cobbs 		link->stats.loopDrops++;
563069154d5SJulian Elischer 		NG_FREE_ITEM(item);
564069154d5SJulian Elischer 		NG_FREE_M(m);
565ed2dbd31SArchie Cobbs 		return (ELOOP);		/* XXX is this an appropriate error? */
566ed2dbd31SArchie Cobbs 	}
567ed2dbd31SArchie Cobbs 
568ed2dbd31SArchie Cobbs 	/* Update stats */
569ed2dbd31SArchie Cobbs 	link->stats.recvPackets++;
570ed2dbd31SArchie Cobbs 	link->stats.recvOctets += m->m_pkthdr.len;
571ed2dbd31SArchie Cobbs 	if ((manycast = (eh->ether_dhost[0] & 1)) != 0) {
572ed2dbd31SArchie Cobbs 		if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
573ed2dbd31SArchie Cobbs 			link->stats.recvBroadcasts++;
574ed2dbd31SArchie Cobbs 			manycast = 2;
575ed2dbd31SArchie Cobbs 		} else
576ed2dbd31SArchie Cobbs 			link->stats.recvMulticasts++;
577ed2dbd31SArchie Cobbs 	}
578ed2dbd31SArchie Cobbs 
579ed2dbd31SArchie Cobbs 	/* Look up packet's source Ethernet address in hashtable */
580ed2dbd31SArchie Cobbs 	if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
581ed2dbd31SArchie Cobbs 
582ed2dbd31SArchie Cobbs 		/* Update time since last heard from this host */
583ed2dbd31SArchie Cobbs 		host->staleness = 0;
584ed2dbd31SArchie Cobbs 
585ed2dbd31SArchie Cobbs 		/* Did host jump to a different link? */
586ed2dbd31SArchie Cobbs 		if (host->linkNum != linkNum) {
587ed2dbd31SArchie Cobbs 
588ed2dbd31SArchie Cobbs 			/*
589ed2dbd31SArchie Cobbs 			 * If the host's old link was recently established
590ed2dbd31SArchie Cobbs 			 * on the old link and it's already jumped to a new
591ed2dbd31SArchie Cobbs 			 * link, declare a loopback condition.
592ed2dbd31SArchie Cobbs 			 */
593ed2dbd31SArchie Cobbs 			if (host->age < priv->conf.minStableAge) {
594ed2dbd31SArchie Cobbs 
595ed2dbd31SArchie Cobbs 				/* Log the problem */
596ed2dbd31SArchie Cobbs 				if (priv->conf.debugLevel >= 2) {
597ed2dbd31SArchie Cobbs 					struct ifnet *ifp = m->m_pkthdr.rcvif;
598ed2dbd31SArchie Cobbs 					char suffix[32];
599ed2dbd31SArchie Cobbs 
600ed2dbd31SArchie Cobbs 					if (ifp != NULL)
601ed2dbd31SArchie Cobbs 						snprintf(suffix, sizeof(suffix),
602ed2dbd31SArchie Cobbs 						    " (%s%d)", ifp->if_name,
603ed2dbd31SArchie Cobbs 						    ifp->if_unit);
604ed2dbd31SArchie Cobbs 					else
605ed2dbd31SArchie Cobbs 						*suffix = '\0';
606ed2dbd31SArchie Cobbs 					log(LOG_WARNING, "ng_bridge: %s:"
607ed2dbd31SArchie Cobbs 					    " loopback detected on %s%s\n",
608ed2dbd31SArchie Cobbs 					    ng_bridge_nodename(node),
60930400f03SJulian Elischer 					    NG_HOOK_NAME(hook), suffix);
610ed2dbd31SArchie Cobbs 				}
611ed2dbd31SArchie Cobbs 
612ed2dbd31SArchie Cobbs 				/* Mark link as linka non grata */
613ed2dbd31SArchie Cobbs 				link->loopCount = priv->conf.loopTimeout;
614ed2dbd31SArchie Cobbs 				link->stats.loopDetects++;
615ed2dbd31SArchie Cobbs 
616ed2dbd31SArchie Cobbs 				/* Forget all hosts on this link */
617ed2dbd31SArchie Cobbs 				ng_bridge_remove_hosts(priv, linkNum);
618ed2dbd31SArchie Cobbs 
619ed2dbd31SArchie Cobbs 				/* Drop packet */
620ed2dbd31SArchie Cobbs 				link->stats.loopDrops++;
621069154d5SJulian Elischer 				NG_FREE_ITEM(item);
622069154d5SJulian Elischer 				NG_FREE_M(m);
623ed2dbd31SArchie Cobbs 				return (ELOOP);		/* XXX appropriate? */
624ed2dbd31SArchie Cobbs 			}
625ed2dbd31SArchie Cobbs 
626ed2dbd31SArchie Cobbs 			/* Move host over to new link */
627ed2dbd31SArchie Cobbs 			host->linkNum = linkNum;
628ed2dbd31SArchie Cobbs 			host->age = 0;
629ed2dbd31SArchie Cobbs 		}
630ed2dbd31SArchie Cobbs 	} else {
631ed2dbd31SArchie Cobbs 		if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) {
632ed2dbd31SArchie Cobbs 			link->stats.memoryFailures++;
633069154d5SJulian Elischer 			NG_FREE_ITEM(item);
634069154d5SJulian Elischer 			NG_FREE_M(m);
635ed2dbd31SArchie Cobbs 			return (ENOMEM);
636ed2dbd31SArchie Cobbs 		}
637ed2dbd31SArchie Cobbs 	}
638ed2dbd31SArchie Cobbs 
639ed2dbd31SArchie Cobbs 	/* Run packet through ipfw processing, if enabled */
640ed2dbd31SArchie Cobbs 	if (priv->conf.ipfw[linkNum] && fw_enable && ip_fw_chk_ptr != NULL) {
641ed2dbd31SArchie Cobbs 		/* XXX not implemented yet */
642ed2dbd31SArchie Cobbs 	}
643ed2dbd31SArchie Cobbs 
644ed2dbd31SArchie Cobbs 	/*
645ed2dbd31SArchie Cobbs 	 * If unicast and destination host known, deliver to host's link,
646ed2dbd31SArchie Cobbs 	 * unless it is the same link as the packet came in on.
647ed2dbd31SArchie Cobbs 	 */
648ed2dbd31SArchie Cobbs 	if (!manycast) {
649ed2dbd31SArchie Cobbs 
650ed2dbd31SArchie Cobbs 		/* Determine packet destination link */
651ed2dbd31SArchie Cobbs 		if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
652ed2dbd31SArchie Cobbs 			struct ng_bridge_link *const destLink
653ed2dbd31SArchie Cobbs 			    = priv->links[host->linkNum];
654ed2dbd31SArchie Cobbs 
655ed2dbd31SArchie Cobbs 			/* If destination same as incoming link, do nothing */
656ed2dbd31SArchie Cobbs 			KASSERT(destLink != NULL,
657ed2dbd31SArchie Cobbs 			    ("%s: link%d null", __FUNCTION__, host->linkNum));
658ed2dbd31SArchie Cobbs 			if (destLink == link) {
659069154d5SJulian Elischer 				NG_FREE_ITEM(item);
660069154d5SJulian Elischer 				NG_FREE_M(m);
661ed2dbd31SArchie Cobbs 				return (0);
662ed2dbd31SArchie Cobbs 			}
663ed2dbd31SArchie Cobbs 
664ed2dbd31SArchie Cobbs 			/* Deliver packet out the destination link */
665ed2dbd31SArchie Cobbs 			destLink->stats.xmitPackets++;
666ed2dbd31SArchie Cobbs 			destLink->stats.xmitOctets += m->m_pkthdr.len;
667069154d5SJulian Elischer 			NG_FWD_NEW_DATA(error, item, destLink->hook, m);
668ed2dbd31SArchie Cobbs 			return (error);
669ed2dbd31SArchie Cobbs 		}
670ed2dbd31SArchie Cobbs 
671ed2dbd31SArchie Cobbs 		/* Destination host is not known */
672ed2dbd31SArchie Cobbs 		link->stats.recvUnknown++;
673ed2dbd31SArchie Cobbs 	}
674ed2dbd31SArchie Cobbs 
675ed2dbd31SArchie Cobbs 	/* Distribute unknown, multicast, broadcast pkts to all other links */
676069154d5SJulian Elischer 	meta = NGI_META(item); /* peek.. */
677069154d5SJulian Elischer 	firstLink = NULL;
678069154d5SJulian Elischer 	for (linkNum = 0; linkNum <= priv->numLinks; linkNum++) {
679069154d5SJulian Elischer 		struct ng_bridge_link *destLink;
680ed2dbd31SArchie Cobbs 		meta_p meta2 = NULL;
681069154d5SJulian Elischer 		struct mbuf *m2 = NULL;
682ed2dbd31SArchie Cobbs 
683069154d5SJulian Elischer 		/*
684069154d5SJulian Elischer 		 * If we have checked all the links then now
685069154d5SJulian Elischer 		 * send the original on its reserved link
686069154d5SJulian Elischer 		 */
687069154d5SJulian Elischer 		if (linkNum == priv->numLinks) {
688069154d5SJulian Elischer 			/* If we never saw a good link, leave. */
689069154d5SJulian Elischer 			if (firstLink == NULL) {
690069154d5SJulian Elischer 				NG_FREE_ITEM(item);
691069154d5SJulian Elischer 				NG_FREE_M(m);
692069154d5SJulian Elischer 				return (0);
693069154d5SJulian Elischer 			}
694069154d5SJulian Elischer 			destLink = firstLink;
695ed2dbd31SArchie Cobbs 		} else {
696069154d5SJulian Elischer 			destLink = priv->links[linkNum];
697069154d5SJulian Elischer 			/* Skip incoming link and disconnected links */
698069154d5SJulian Elischer 			if (destLink == NULL || destLink == link) {
699069154d5SJulian Elischer 				continue;
700069154d5SJulian Elischer 			}
701069154d5SJulian Elischer 			if (firstLink == NULL) {
702069154d5SJulian Elischer 				/*
703069154d5SJulian Elischer 				 * This is the first usable link we have found.
704069154d5SJulian Elischer 				 * Reserve it for the originals.
705069154d5SJulian Elischer 				 * If we never find another we save a copy.
706069154d5SJulian Elischer 				 */
707069154d5SJulian Elischer 				firstLink = destLink;
708069154d5SJulian Elischer 				continue;
709069154d5SJulian Elischer 			}
710069154d5SJulian Elischer 
711069154d5SJulian Elischer 			/*
712069154d5SJulian Elischer 			 * It's usable link but not the reserved (first) one.
713069154d5SJulian Elischer 			 * Copy mbuf and meta info for sending.
714069154d5SJulian Elischer 			 */
715763fcb92SArchie Cobbs 			m2 = m_dup(m, M_NOWAIT);	/* XXX m_copypacket() */
716ed2dbd31SArchie Cobbs 			if (m2 == NULL) {
717ed2dbd31SArchie Cobbs 				link->stats.memoryFailures++;
718069154d5SJulian Elischer 				NG_FREE_ITEM(item);
719069154d5SJulian Elischer 				NG_FREE_M(m);
720ed2dbd31SArchie Cobbs 				return (ENOBUFS);
721ed2dbd31SArchie Cobbs 			}
722ed2dbd31SArchie Cobbs 			if (meta != NULL
723ed2dbd31SArchie Cobbs 			    && (meta2 = ng_copy_meta(meta)) == NULL) {
724ed2dbd31SArchie Cobbs 				link->stats.memoryFailures++;
725ed2dbd31SArchie Cobbs 				m_freem(m2);
726069154d5SJulian Elischer 				NG_FREE_ITEM(item);
727069154d5SJulian Elischer 				NG_FREE_M(m);
728ed2dbd31SArchie Cobbs 				return (ENOMEM);
729ed2dbd31SArchie Cobbs 			}
730ed2dbd31SArchie Cobbs 		}
731ed2dbd31SArchie Cobbs 
732ed2dbd31SArchie Cobbs 		/* Update stats */
733ed2dbd31SArchie Cobbs 		destLink->stats.xmitPackets++;
734ed2dbd31SArchie Cobbs 		destLink->stats.xmitOctets += m->m_pkthdr.len;
735ed2dbd31SArchie Cobbs 		switch (manycast) {
736ed2dbd31SArchie Cobbs 		case 0:					/* unicast */
737ed2dbd31SArchie Cobbs 			break;
738ed2dbd31SArchie Cobbs 		case 1:					/* multicast */
739ed2dbd31SArchie Cobbs 			destLink->stats.xmitMulticasts++;
740ed2dbd31SArchie Cobbs 			break;
741ed2dbd31SArchie Cobbs 		case 2:					/* broadcast */
742ed2dbd31SArchie Cobbs 			destLink->stats.xmitBroadcasts++;
743ed2dbd31SArchie Cobbs 			break;
744ed2dbd31SArchie Cobbs 		}
745ed2dbd31SArchie Cobbs 
746ed2dbd31SArchie Cobbs 		/* Send packet */
747069154d5SJulian Elischer 		if (destLink == firstLink) {
748069154d5SJulian Elischer 			/*
749069154d5SJulian Elischer 			 * If we've sent all the others, send the original
750069154d5SJulian Elischer 			 * on the first link we found.
751069154d5SJulian Elischer 			 */
752069154d5SJulian Elischer 			NG_FWD_NEW_DATA(error, item, destLink->hook, m);
753069154d5SJulian Elischer 			break; /* always done last - not really needed. */
754069154d5SJulian Elischer 		} else {
755ed2dbd31SArchie Cobbs 			NG_SEND_DATA(error, destLink->hook, m2, meta2);
756ed2dbd31SArchie Cobbs 		}
757069154d5SJulian Elischer 	}
758ed2dbd31SArchie Cobbs 	return (error);
759ed2dbd31SArchie Cobbs }
760ed2dbd31SArchie Cobbs 
761ed2dbd31SArchie Cobbs /*
762ed2dbd31SArchie Cobbs  * Shutdown node
763ed2dbd31SArchie Cobbs  */
764ed2dbd31SArchie Cobbs static int
765069154d5SJulian Elischer ng_bridge_shutdown(node_p node)
766ed2dbd31SArchie Cobbs {
76730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
768ed2dbd31SArchie Cobbs 
769ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
770ed2dbd31SArchie Cobbs 	    ("%s: numLinks=%d numHosts=%d",
771ed2dbd31SArchie Cobbs 	    __FUNCTION__, priv->numLinks, priv->numHosts));
7729c8c302fSJulian Elischer 	FREE(priv->tab, M_NETGRAPH_BRIDGE);
7739c8c302fSJulian Elischer 	FREE(priv, M_NETGRAPH_BRIDGE);
77430400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, NULL);
77530400f03SJulian Elischer 	NG_NODE_UNREF(node);
776ed2dbd31SArchie Cobbs 	return (0);
777ed2dbd31SArchie Cobbs }
778ed2dbd31SArchie Cobbs 
779ed2dbd31SArchie Cobbs /*
780ed2dbd31SArchie Cobbs  * Hook disconnection.
781ed2dbd31SArchie Cobbs  */
782ed2dbd31SArchie Cobbs static int
783ed2dbd31SArchie Cobbs ng_bridge_disconnect(hook_p hook)
784ed2dbd31SArchie Cobbs {
78530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
786ed2dbd31SArchie Cobbs 	int linkNum;
787ed2dbd31SArchie Cobbs 
788ed2dbd31SArchie Cobbs 	/* Get link number */
78930400f03SJulian Elischer 	linkNum = (int)NG_HOOK_PRIVATE(hook);
790ed2dbd31SArchie Cobbs 	KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
791ed2dbd31SArchie Cobbs 	    ("%s: linkNum=%u", __FUNCTION__, linkNum));
792ed2dbd31SArchie Cobbs 
793ed2dbd31SArchie Cobbs 	/* Remove all hosts associated with this link */
794ed2dbd31SArchie Cobbs 	ng_bridge_remove_hosts(priv, linkNum);
795ed2dbd31SArchie Cobbs 
796ed2dbd31SArchie Cobbs 	/* Free associated link information */
797ed2dbd31SArchie Cobbs 	KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __FUNCTION__));
7989c8c302fSJulian Elischer 	FREE(priv->links[linkNum], M_NETGRAPH_BRIDGE);
799ed2dbd31SArchie Cobbs 	priv->links[linkNum] = NULL;
800ed2dbd31SArchie Cobbs 	priv->numLinks--;
801ed2dbd31SArchie Cobbs 
802ed2dbd31SArchie Cobbs 	/* If no more hooks, go away */
80330400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
80430400f03SJulian Elischer 	&& (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))) {
80530400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));
80630400f03SJulian Elischer 	}
807ed2dbd31SArchie Cobbs 	return (0);
808ed2dbd31SArchie Cobbs }
809ed2dbd31SArchie Cobbs 
810ed2dbd31SArchie Cobbs /******************************************************************
811ed2dbd31SArchie Cobbs 		    HASH TABLE FUNCTIONS
812ed2dbd31SArchie Cobbs ******************************************************************/
813ed2dbd31SArchie Cobbs 
814ed2dbd31SArchie Cobbs /*
815ed2dbd31SArchie Cobbs  * Hash algorithm
816ed2dbd31SArchie Cobbs  *
817ed2dbd31SArchie Cobbs  * Only hashing bytes 3-6 of the Ethernet address is sufficient and fast.
818ed2dbd31SArchie Cobbs  */
819ed2dbd31SArchie Cobbs #define HASH(addr,mask)		( (((const u_int16_t *)(addr))[0] 	\
820ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[1] 	\
821ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
822ed2dbd31SArchie Cobbs 
823ed2dbd31SArchie Cobbs /*
824ed2dbd31SArchie Cobbs  * Find a host entry in the table.
825ed2dbd31SArchie Cobbs  */
826ed2dbd31SArchie Cobbs static struct ng_bridge_host *
827ed2dbd31SArchie Cobbs ng_bridge_get(priv_p priv, const u_char *addr)
828ed2dbd31SArchie Cobbs {
829ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
830ed2dbd31SArchie Cobbs 	struct ng_bridge_hent *hent;
831ed2dbd31SArchie Cobbs 
832ed2dbd31SArchie Cobbs 	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
833ed2dbd31SArchie Cobbs 		if (ETHER_EQUAL(hent->host.addr, addr))
834ed2dbd31SArchie Cobbs 			return (&hent->host);
835ed2dbd31SArchie Cobbs 	}
836ed2dbd31SArchie Cobbs 	return (NULL);
837ed2dbd31SArchie Cobbs }
838ed2dbd31SArchie Cobbs 
839ed2dbd31SArchie Cobbs /*
840ed2dbd31SArchie Cobbs  * Add a new host entry to the table. This assumes the host doesn't
841ed2dbd31SArchie Cobbs  * already exist in the table. Returns 1 on success, 0 if there
842ed2dbd31SArchie Cobbs  * was a memory allocation failure.
843ed2dbd31SArchie Cobbs  */
844ed2dbd31SArchie Cobbs static int
845ed2dbd31SArchie Cobbs ng_bridge_put(priv_p priv, const u_char *addr, int linkNum)
846ed2dbd31SArchie Cobbs {
847ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
848ed2dbd31SArchie Cobbs 	struct ng_bridge_hent *hent;
849ed2dbd31SArchie Cobbs 
850ed2dbd31SArchie Cobbs #ifdef INVARIANTS
851ed2dbd31SArchie Cobbs 	/* Assert that entry does not already exist in hashtable */
852ed2dbd31SArchie Cobbs 	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
853ed2dbd31SArchie Cobbs 		KASSERT(!ETHER_EQUAL(hent->host.addr, addr),
854ed2dbd31SArchie Cobbs 		    ("%s: entry %6D exists in table", __FUNCTION__, addr, ":"));
855ed2dbd31SArchie Cobbs 	}
856ed2dbd31SArchie Cobbs #endif
857ed2dbd31SArchie Cobbs 
858ed2dbd31SArchie Cobbs 	/* Allocate and initialize new hashtable entry */
859ed2dbd31SArchie Cobbs 	MALLOC(hent, struct ng_bridge_hent *,
8609c8c302fSJulian Elischer 	    sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT);
861ed2dbd31SArchie Cobbs 	if (hent == NULL)
862ed2dbd31SArchie Cobbs 		return (0);
863ed2dbd31SArchie Cobbs 	bcopy(addr, hent->host.addr, ETHER_ADDR_LEN);
864ed2dbd31SArchie Cobbs 	hent->host.linkNum = linkNum;
865ed2dbd31SArchie Cobbs 	hent->host.staleness = 0;
866ed2dbd31SArchie Cobbs 	hent->host.age = 0;
867ed2dbd31SArchie Cobbs 
868ed2dbd31SArchie Cobbs 	/* Add new element to hash bucket */
869ed2dbd31SArchie Cobbs 	SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next);
870ed2dbd31SArchie Cobbs 	priv->numHosts++;
871ed2dbd31SArchie Cobbs 
872ed2dbd31SArchie Cobbs 	/* Resize table if necessary */
873ed2dbd31SArchie Cobbs 	ng_bridge_rehash(priv);
874ed2dbd31SArchie Cobbs 	return (1);
875ed2dbd31SArchie Cobbs }
876ed2dbd31SArchie Cobbs 
877ed2dbd31SArchie Cobbs /*
878ed2dbd31SArchie Cobbs  * Resize the hash table. We try to maintain the number of buckets
879ed2dbd31SArchie Cobbs  * such that the load factor is in the range 0.25 to 1.0.
880ed2dbd31SArchie Cobbs  *
881ed2dbd31SArchie Cobbs  * If we can't get the new memory then we silently fail. This is OK
882ed2dbd31SArchie Cobbs  * because things will still work and we'll try again soon anyway.
883ed2dbd31SArchie Cobbs  */
884ed2dbd31SArchie Cobbs static void
885ed2dbd31SArchie Cobbs ng_bridge_rehash(priv_p priv)
886ed2dbd31SArchie Cobbs {
887ed2dbd31SArchie Cobbs 	struct ng_bridge_bucket *newTab;
888ed2dbd31SArchie Cobbs 	int oldBucket, newBucket;
889ed2dbd31SArchie Cobbs 	int newNumBuckets;
890ed2dbd31SArchie Cobbs 	u_int newMask;
891ed2dbd31SArchie Cobbs 
892ed2dbd31SArchie Cobbs 	/* Is table too full or too empty? */
893ed2dbd31SArchie Cobbs 	if (priv->numHosts > priv->numBuckets
894ed2dbd31SArchie Cobbs 	    && (priv->numBuckets << 1) <= MAX_BUCKETS)
895ed2dbd31SArchie Cobbs 		newNumBuckets = priv->numBuckets << 1;
896ed2dbd31SArchie Cobbs 	else if (priv->numHosts < (priv->numBuckets >> 2)
897ed2dbd31SArchie Cobbs 	    && (priv->numBuckets >> 2) >= MIN_BUCKETS)
898ed2dbd31SArchie Cobbs 		newNumBuckets = priv->numBuckets >> 2;
899ed2dbd31SArchie Cobbs 	else
900ed2dbd31SArchie Cobbs 		return;
901ed2dbd31SArchie Cobbs 	newMask = newNumBuckets - 1;
902ed2dbd31SArchie Cobbs 
903ed2dbd31SArchie Cobbs 	/* Allocate and initialize new table */
904ed2dbd31SArchie Cobbs 	MALLOC(newTab, struct ng_bridge_bucket *,
9059c8c302fSJulian Elischer 	    newNumBuckets * sizeof(*newTab), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
906ed2dbd31SArchie Cobbs 	if (newTab == NULL)
907ed2dbd31SArchie Cobbs 		return;
908ed2dbd31SArchie Cobbs 
909ed2dbd31SArchie Cobbs 	/* Move all entries from old table to new table */
910ed2dbd31SArchie Cobbs 	for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
911ed2dbd31SArchie Cobbs 		struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
912ed2dbd31SArchie Cobbs 
913ed2dbd31SArchie Cobbs 		while (!SLIST_EMPTY(oldList)) {
914ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent
915ed2dbd31SArchie Cobbs 			    = SLIST_FIRST(oldList);
916ed2dbd31SArchie Cobbs 
917ed2dbd31SArchie Cobbs 			SLIST_REMOVE_HEAD(oldList, next);
918ed2dbd31SArchie Cobbs 			newBucket = HASH(hent->host.addr, newMask);
919ed2dbd31SArchie Cobbs 			SLIST_INSERT_HEAD(&newTab[newBucket], hent, next);
920ed2dbd31SArchie Cobbs 		}
921ed2dbd31SArchie Cobbs 	}
922ed2dbd31SArchie Cobbs 
923ed2dbd31SArchie Cobbs 	/* Replace old table with new one */
924ed2dbd31SArchie Cobbs 	if (priv->conf.debugLevel >= 3) {
925ed2dbd31SArchie Cobbs 		log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
926ed2dbd31SArchie Cobbs 		    ng_bridge_nodename(priv->node),
927ed2dbd31SArchie Cobbs 		    priv->numBuckets, newNumBuckets);
928ed2dbd31SArchie Cobbs 	}
9299c8c302fSJulian Elischer 	FREE(priv->tab, M_NETGRAPH_BRIDGE);
930ed2dbd31SArchie Cobbs 	priv->numBuckets = newNumBuckets;
931ed2dbd31SArchie Cobbs 	priv->hashMask = newMask;
932ed2dbd31SArchie Cobbs 	priv->tab = newTab;
933ed2dbd31SArchie Cobbs 	return;
934ed2dbd31SArchie Cobbs }
935ed2dbd31SArchie Cobbs 
936ed2dbd31SArchie Cobbs /******************************************************************
937ed2dbd31SArchie Cobbs 		    MISC FUNCTIONS
938ed2dbd31SArchie Cobbs ******************************************************************/
939ed2dbd31SArchie Cobbs 
940ed2dbd31SArchie Cobbs /*
941ed2dbd31SArchie Cobbs  * Remove all hosts associated with a specific link from the hashtable.
942ed2dbd31SArchie Cobbs  * If linkNum == -1, then remove all hosts in the table.
943ed2dbd31SArchie Cobbs  */
944ed2dbd31SArchie Cobbs static void
945ed2dbd31SArchie Cobbs ng_bridge_remove_hosts(priv_p priv, int linkNum)
946ed2dbd31SArchie Cobbs {
947ed2dbd31SArchie Cobbs 	int bucket;
948ed2dbd31SArchie Cobbs 
949ed2dbd31SArchie Cobbs 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
950ed2dbd31SArchie Cobbs 		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
951ed2dbd31SArchie Cobbs 
952ed2dbd31SArchie Cobbs 		while (*hptr != NULL) {
953ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent = *hptr;
954ed2dbd31SArchie Cobbs 
955ed2dbd31SArchie Cobbs 			if (linkNum == -1 || hent->host.linkNum == linkNum) {
956ed2dbd31SArchie Cobbs 				*hptr = SLIST_NEXT(hent, next);
9579c8c302fSJulian Elischer 				FREE(hent, M_NETGRAPH_BRIDGE);
958ed2dbd31SArchie Cobbs 				priv->numHosts--;
959ed2dbd31SArchie Cobbs 			} else
960ed2dbd31SArchie Cobbs 				hptr = &SLIST_NEXT(hent, next);
961ed2dbd31SArchie Cobbs 		}
962ed2dbd31SArchie Cobbs 	}
963ed2dbd31SArchie Cobbs }
964ed2dbd31SArchie Cobbs 
965ed2dbd31SArchie Cobbs /*
966ed2dbd31SArchie Cobbs  * Handle our once-per-second timeout event. We do two things:
967ed2dbd31SArchie Cobbs  * we decrement link->loopCount for those links being muted due to
968ed2dbd31SArchie Cobbs  * a detected loopback condition, and we remove any hosts from
969ed2dbd31SArchie Cobbs  * the hashtable whom we haven't heard from in a long while.
970ed2dbd31SArchie Cobbs  */
971ed2dbd31SArchie Cobbs static void
972ed2dbd31SArchie Cobbs ng_bridge_timeout(void *arg)
973ed2dbd31SArchie Cobbs {
974ed2dbd31SArchie Cobbs 	const node_p node = arg;
97530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
976ed2dbd31SArchie Cobbs 	int s, bucket;
977ed2dbd31SArchie Cobbs 	int counter = 0;
978ed2dbd31SArchie Cobbs 	int linkNum;
979ed2dbd31SArchie Cobbs 
980ed2dbd31SArchie Cobbs 	/* Avoid race condition with ng_bridge_shutdown() */
981ed2dbd31SArchie Cobbs 	s = splnet();
98230400f03SJulian Elischer 	if ((NG_NODE_NOT_VALID(node)) || priv == NULL) {
98330400f03SJulian Elischer 		NG_NODE_UNREF(node);
984ed2dbd31SArchie Cobbs 		splx(s);
985ed2dbd31SArchie Cobbs 		return;
986ed2dbd31SArchie Cobbs 	}
987ed2dbd31SArchie Cobbs 
988ed2dbd31SArchie Cobbs 	/* Register a new timeout, keeping the existing node reference */
989ed2dbd31SArchie Cobbs 	callout_reset(&priv->timer, hz, ng_bridge_timeout, node);
990ed2dbd31SArchie Cobbs 
991ed2dbd31SArchie Cobbs 	/* Update host time counters and remove stale entries */
992ed2dbd31SArchie Cobbs 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
993ed2dbd31SArchie Cobbs 		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
994ed2dbd31SArchie Cobbs 
995ed2dbd31SArchie Cobbs 		while (*hptr != NULL) {
996ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent = *hptr;
997ed2dbd31SArchie Cobbs 
998ed2dbd31SArchie Cobbs 			/* Make sure host's link really exists */
999ed2dbd31SArchie Cobbs 			KASSERT(priv->links[hent->host.linkNum] != NULL,
1000ed2dbd31SArchie Cobbs 			    ("%s: host %6D on nonexistent link %d\n",
1001ed2dbd31SArchie Cobbs 			    __FUNCTION__, hent->host.addr, ":",
1002ed2dbd31SArchie Cobbs 			    hent->host.linkNum));
1003ed2dbd31SArchie Cobbs 
1004ed2dbd31SArchie Cobbs 			/* Remove hosts we haven't heard from in a while */
1005ed2dbd31SArchie Cobbs 			if (++hent->host.staleness >= priv->conf.maxStaleness) {
1006ed2dbd31SArchie Cobbs 				*hptr = SLIST_NEXT(hent, next);
10079c8c302fSJulian Elischer 				FREE(hent, M_NETGRAPH_BRIDGE);
1008ed2dbd31SArchie Cobbs 				priv->numHosts--;
1009ed2dbd31SArchie Cobbs 			} else {
1010ed2dbd31SArchie Cobbs 				if (hent->host.age < 0xffff)
1011ed2dbd31SArchie Cobbs 					hent->host.age++;
1012ed2dbd31SArchie Cobbs 				hptr = &SLIST_NEXT(hent, next);
1013ed2dbd31SArchie Cobbs 				counter++;
1014ed2dbd31SArchie Cobbs 			}
1015ed2dbd31SArchie Cobbs 		}
1016ed2dbd31SArchie Cobbs 	}
1017ed2dbd31SArchie Cobbs 	KASSERT(priv->numHosts == counter,
1018ed2dbd31SArchie Cobbs 	    ("%s: hosts: %d != %d", __FUNCTION__, priv->numHosts, counter));
1019ed2dbd31SArchie Cobbs 
1020ed2dbd31SArchie Cobbs 	/* Decrease table size if necessary */
1021ed2dbd31SArchie Cobbs 	ng_bridge_rehash(priv);
1022ed2dbd31SArchie Cobbs 
1023ed2dbd31SArchie Cobbs 	/* Decrease loop counter on muted looped back links */
1024ed2dbd31SArchie Cobbs 	for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) {
1025ed2dbd31SArchie Cobbs 		struct ng_bridge_link *const link = priv->links[linkNum];
1026ed2dbd31SArchie Cobbs 
1027ed2dbd31SArchie Cobbs 		if (link != NULL) {
1028ed2dbd31SArchie Cobbs 			if (link->loopCount != 0) {
1029ed2dbd31SArchie Cobbs 				link->loopCount--;
1030ed2dbd31SArchie Cobbs 				if (link->loopCount == 0
1031ed2dbd31SArchie Cobbs 				    && priv->conf.debugLevel >= 2) {
1032ed2dbd31SArchie Cobbs 					log(LOG_INFO, "ng_bridge: %s:"
1033ed2dbd31SArchie Cobbs 					    " restoring looped back link%d\n",
1034ed2dbd31SArchie Cobbs 					    ng_bridge_nodename(node), linkNum);
1035ed2dbd31SArchie Cobbs 				}
1036ed2dbd31SArchie Cobbs 			}
1037ed2dbd31SArchie Cobbs 			counter++;
1038ed2dbd31SArchie Cobbs 		}
1039ed2dbd31SArchie Cobbs 	}
1040ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == counter,
1041ed2dbd31SArchie Cobbs 	    ("%s: links: %d != %d", __FUNCTION__, priv->numLinks, counter));
1042ed2dbd31SArchie Cobbs 
1043ed2dbd31SArchie Cobbs 	/* Done */
1044ed2dbd31SArchie Cobbs 	splx(s);
1045ed2dbd31SArchie Cobbs }
1046ed2dbd31SArchie Cobbs 
1047ed2dbd31SArchie Cobbs /*
1048ed2dbd31SArchie Cobbs  * Return node's "name", even if it doesn't have one.
1049ed2dbd31SArchie Cobbs  */
1050ed2dbd31SArchie Cobbs static const char *
1051ed2dbd31SArchie Cobbs ng_bridge_nodename(node_p node)
1052ed2dbd31SArchie Cobbs {
1053ed2dbd31SArchie Cobbs 	static char name[NG_NODELEN+1];
1054ed2dbd31SArchie Cobbs 
105530400f03SJulian Elischer 	if (NG_NODE_NAME(node) != NULL)
105630400f03SJulian Elischer 		snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
1057ed2dbd31SArchie Cobbs 	else
1058ed2dbd31SArchie Cobbs 		snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
1059ed2dbd31SArchie Cobbs 	return name;
1060ed2dbd31SArchie Cobbs }
1061ed2dbd31SArchie Cobbs 
1062