xref: /freebsd/sys/netgraph/ng_bridge.c (revision f961caf2184c94d6f59c8d522207156b3533d977)
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 */
94*f961caf2SLutz Donnerhacke 	unsigned int			learnMac : 1,   /* autolearn macs */
95*f961caf2SLutz Donnerhacke 					sendUnknown : 1;/* send unknown macs out */
96ed2dbd31SArchie Cobbs 	struct ng_bridge_link_stats	stats;		/* link stats */
97ed2dbd31SArchie Cobbs };
98ed2dbd31SArchie Cobbs 
99ed2dbd31SArchie Cobbs /* Per-node private data */
100ed2dbd31SArchie Cobbs struct ng_bridge_private {
101ed2dbd31SArchie Cobbs 	struct ng_bridge_bucket	*tab;		/* hash table bucket array */
102ed2dbd31SArchie Cobbs 	struct ng_bridge_config	conf;		/* node configuration */
103ed2dbd31SArchie Cobbs 	node_p			node;		/* netgraph node */
104ed2dbd31SArchie Cobbs 	u_int			numHosts;	/* num entries in table */
105ed2dbd31SArchie Cobbs 	u_int			numBuckets;	/* num buckets in table */
106ed2dbd31SArchie Cobbs 	u_int			hashMask;	/* numBuckets - 1 */
107ed2dbd31SArchie Cobbs 	int			numLinks;	/* num connected links */
108f8aab721SMarko Zec 	int			persistent;	/* can exist w/o hooks */
109ed2dbd31SArchie Cobbs 	struct callout		timer;		/* one second periodic timer */
110ed2dbd31SArchie Cobbs };
111ed2dbd31SArchie Cobbs typedef struct ng_bridge_private *priv_p;
112ed2dbd31SArchie Cobbs 
113ed2dbd31SArchie Cobbs /* Information about a host, stored in a hash table entry */
114ed2dbd31SArchie Cobbs struct ng_bridge_hent {
115ed2dbd31SArchie Cobbs 	struct ng_bridge_host		host;	/* actual host info */
116ed2dbd31SArchie Cobbs 	SLIST_ENTRY(ng_bridge_hent)	next;	/* next entry in bucket */
117ed2dbd31SArchie Cobbs };
118ed2dbd31SArchie Cobbs 
119ed2dbd31SArchie Cobbs /* Hash table bucket declaration */
120ed2dbd31SArchie Cobbs SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent);
121ed2dbd31SArchie Cobbs 
122ed2dbd31SArchie Cobbs /* Netgraph node methods */
123ed2dbd31SArchie Cobbs static ng_constructor_t	ng_bridge_constructor;
124ed2dbd31SArchie Cobbs static ng_rcvmsg_t	ng_bridge_rcvmsg;
125069154d5SJulian Elischer static ng_shutdown_t	ng_bridge_shutdown;
126ed2dbd31SArchie Cobbs static ng_newhook_t	ng_bridge_newhook;
127ed2dbd31SArchie Cobbs static ng_rcvdata_t	ng_bridge_rcvdata;
128ed2dbd31SArchie Cobbs static ng_disconnect_t	ng_bridge_disconnect;
129ed2dbd31SArchie Cobbs 
130ed2dbd31SArchie Cobbs /* Other internal functions */
131ed2dbd31SArchie Cobbs static struct	ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr);
132631cabbaSGleb Smirnoff static int	ng_bridge_put(priv_p priv, const u_char *addr, link_p link);
133ed2dbd31SArchie Cobbs static void	ng_bridge_rehash(priv_p priv);
134631cabbaSGleb Smirnoff static void	ng_bridge_remove_hosts(priv_p priv, link_p link);
135e0d32af7SGleb Smirnoff static void	ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2);
136ed2dbd31SArchie Cobbs static const	char *ng_bridge_nodename(node_p node);
137ed2dbd31SArchie Cobbs 
138ed2dbd31SArchie Cobbs /* Ethernet broadcast */
139ed2dbd31SArchie Cobbs static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
140ed2dbd31SArchie Cobbs     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
141ed2dbd31SArchie Cobbs 
142ed2dbd31SArchie Cobbs /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
143ed2dbd31SArchie Cobbs #define ETHER_EQUAL(a,b)	(((const u_int32_t *)(a))[0] \
144ed2dbd31SArchie Cobbs 					== ((const u_int32_t *)(b))[0] \
145ed2dbd31SArchie Cobbs 				    && ((const u_int16_t *)(a))[2] \
146ed2dbd31SArchie Cobbs 					== ((const u_int16_t *)(b))[2])
147ed2dbd31SArchie Cobbs 
148ed2dbd31SArchie Cobbs /* Minimum and maximum number of hash buckets. Must be a power of two. */
149ed2dbd31SArchie Cobbs #define MIN_BUCKETS		(1 << 5)	/* 32 */
150ed2dbd31SArchie Cobbs #define MAX_BUCKETS		(1 << 14)	/* 16384 */
151ed2dbd31SArchie Cobbs 
152ed2dbd31SArchie Cobbs /* Configuration default values */
153ed2dbd31SArchie Cobbs #define DEFAULT_LOOP_TIMEOUT	60
154ed2dbd31SArchie Cobbs #define DEFAULT_MAX_STALENESS	(15 * 60)	/* same as ARP timeout */
155ed2dbd31SArchie Cobbs #define DEFAULT_MIN_STABLE_AGE	1
156ed2dbd31SArchie Cobbs 
157ed2dbd31SArchie Cobbs /******************************************************************
158ed2dbd31SArchie Cobbs 		    NETGRAPH PARSE TYPES
159ed2dbd31SArchie Cobbs ******************************************************************/
160ed2dbd31SArchie Cobbs 
161ed2dbd31SArchie Cobbs /*
162ed2dbd31SArchie Cobbs  * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
163ed2dbd31SArchie Cobbs  */
164ed2dbd31SArchie Cobbs static int
165ed2dbd31SArchie Cobbs ng_bridge_getTableLength(const struct ng_parse_type *type,
166ed2dbd31SArchie Cobbs 	const u_char *start, const u_char *buf)
167ed2dbd31SArchie Cobbs {
168ed2dbd31SArchie Cobbs 	const struct ng_bridge_host_ary *const hary
169ed2dbd31SArchie Cobbs 	    = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
170ed2dbd31SArchie Cobbs 
171ed2dbd31SArchie Cobbs 	return hary->numHosts;
172ed2dbd31SArchie Cobbs }
173ed2dbd31SArchie Cobbs 
174ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_host_ary */
175f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_type_fields[]
1768c7e4101SRuslan Ermilov 	= NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
177ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_type = {
178ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
179f0184ff8SArchie Cobbs 	&ng_bridge_host_type_fields
180ed2dbd31SArchie Cobbs };
181ed2dbd31SArchie Cobbs static const struct ng_parse_array_info ng_bridge_hary_type_info = {
182ed2dbd31SArchie Cobbs 	&ng_bridge_host_type,
183ed2dbd31SArchie Cobbs 	ng_bridge_getTableLength
184ed2dbd31SArchie Cobbs };
185ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_hary_type = {
186ed2dbd31SArchie Cobbs 	&ng_parse_array_type,
187ed2dbd31SArchie Cobbs 	&ng_bridge_hary_type_info
188ed2dbd31SArchie Cobbs };
189f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[]
190ed2dbd31SArchie Cobbs 	= NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
191ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_ary_type = {
192ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
193f0184ff8SArchie Cobbs 	&ng_bridge_host_ary_type_fields
194ed2dbd31SArchie Cobbs };
195ed2dbd31SArchie Cobbs 
196ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_config */
197f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_config_type_fields[]
198631cabbaSGleb Smirnoff 	= NG_BRIDGE_CONFIG_TYPE_INFO;
199ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_config_type = {
200ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
201f0184ff8SArchie Cobbs 	&ng_bridge_config_type_fields
202ed2dbd31SArchie Cobbs };
203ed2dbd31SArchie Cobbs 
204ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_link_stat */
205f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_stats_type_fields[]
206f0184ff8SArchie Cobbs 	= NG_BRIDGE_STATS_TYPE_INFO;
207ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_stats_type = {
208ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
209f0184ff8SArchie Cobbs 	&ng_bridge_stats_type_fields
210ed2dbd31SArchie Cobbs };
211ed2dbd31SArchie Cobbs 
212ed2dbd31SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
213ed2dbd31SArchie Cobbs static const struct ng_cmdlist ng_bridge_cmdlist[] = {
214ed2dbd31SArchie Cobbs 	{
215ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
216ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_SET_CONFIG,
217ed2dbd31SArchie Cobbs 	  "setconfig",
218ed2dbd31SArchie Cobbs 	  &ng_bridge_config_type,
219ed2dbd31SArchie Cobbs 	  NULL
220ed2dbd31SArchie Cobbs 	},
221ed2dbd31SArchie Cobbs 	{
222ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
223ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_CONFIG,
224ed2dbd31SArchie Cobbs 	  "getconfig",
225ed2dbd31SArchie Cobbs 	  NULL,
226ed2dbd31SArchie Cobbs 	  &ng_bridge_config_type
227ed2dbd31SArchie Cobbs 	},
228ed2dbd31SArchie Cobbs 	{
229ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
230ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_RESET,
231ed2dbd31SArchie Cobbs 	  "reset",
232ed2dbd31SArchie Cobbs 	  NULL,
233ed2dbd31SArchie Cobbs 	  NULL
234ed2dbd31SArchie Cobbs 	},
235ed2dbd31SArchie Cobbs 	{
236ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
237ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_STATS,
238ed2dbd31SArchie Cobbs 	  "getstats",
239ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
240ed2dbd31SArchie Cobbs 	  &ng_bridge_stats_type
241ed2dbd31SArchie Cobbs 	},
242ed2dbd31SArchie Cobbs 	{
243ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
244ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_CLR_STATS,
245ed2dbd31SArchie Cobbs 	  "clrstats",
246ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
247ed2dbd31SArchie Cobbs 	  NULL
248ed2dbd31SArchie Cobbs 	},
249ed2dbd31SArchie Cobbs 	{
250ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
251ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GETCLR_STATS,
252ed2dbd31SArchie Cobbs 	  "getclrstats",
253ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
254ed2dbd31SArchie Cobbs 	  &ng_bridge_stats_type
255ed2dbd31SArchie Cobbs 	},
256ed2dbd31SArchie Cobbs 	{
257ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
258ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_TABLE,
259ed2dbd31SArchie Cobbs 	  "gettable",
260ed2dbd31SArchie Cobbs 	  NULL,
261ed2dbd31SArchie Cobbs 	  &ng_bridge_host_ary_type
262ed2dbd31SArchie Cobbs 	},
263f8aab721SMarko Zec 	{
264f8aab721SMarko Zec 	  NGM_BRIDGE_COOKIE,
265f8aab721SMarko Zec 	  NGM_BRIDGE_SET_PERSISTENT,
266f8aab721SMarko Zec 	  "setpersistent",
267f8aab721SMarko Zec 	  NULL,
268f8aab721SMarko Zec 	  NULL
269f8aab721SMarko Zec 	},
270ed2dbd31SArchie Cobbs 	{ 0 }
271ed2dbd31SArchie Cobbs };
272ed2dbd31SArchie Cobbs 
273ed2dbd31SArchie Cobbs /* Node type descriptor */
274ed2dbd31SArchie Cobbs static struct ng_type ng_bridge_typestruct = {
275f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
276f8aae777SJulian Elischer 	.name =		NG_BRIDGE_NODE_TYPE,
277f8aae777SJulian Elischer 	.constructor =	ng_bridge_constructor,
278f8aae777SJulian Elischer 	.rcvmsg =	ng_bridge_rcvmsg,
279f8aae777SJulian Elischer 	.shutdown =	ng_bridge_shutdown,
280f8aae777SJulian Elischer 	.newhook =	ng_bridge_newhook,
281f8aae777SJulian Elischer 	.rcvdata =	ng_bridge_rcvdata,
282f8aae777SJulian Elischer 	.disconnect =	ng_bridge_disconnect,
283f8aae777SJulian Elischer 	.cmdlist =	ng_bridge_cmdlist,
284ed2dbd31SArchie Cobbs };
285a89effcdSArchie Cobbs NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
286ed2dbd31SArchie Cobbs 
287ed2dbd31SArchie Cobbs /******************************************************************
288ed2dbd31SArchie Cobbs 		    NETGRAPH NODE METHODS
289ed2dbd31SArchie Cobbs ******************************************************************/
290ed2dbd31SArchie Cobbs 
291ed2dbd31SArchie Cobbs /*
292ed2dbd31SArchie Cobbs  * Node constructor
293ed2dbd31SArchie Cobbs  */
294ed2dbd31SArchie Cobbs static int
295069154d5SJulian Elischer ng_bridge_constructor(node_p node)
296ed2dbd31SArchie Cobbs {
297ed2dbd31SArchie Cobbs 	priv_p priv;
298ed2dbd31SArchie Cobbs 
299ed2dbd31SArchie Cobbs 	/* Allocate and initialize private info */
300674d86bfSGleb Smirnoff 	priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
301e0d32af7SGleb Smirnoff 	ng_callout_init(&priv->timer);
302ed2dbd31SArchie Cobbs 
303ed2dbd31SArchie Cobbs 	/* Allocate and initialize hash table, etc. */
304e11e3f18SDag-Erling Smørgrav 	priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab),
305674d86bfSGleb Smirnoff 	    M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
306ed2dbd31SArchie Cobbs 	priv->numBuckets = MIN_BUCKETS;
307ed2dbd31SArchie Cobbs 	priv->hashMask = MIN_BUCKETS - 1;
308ed2dbd31SArchie Cobbs 	priv->conf.debugLevel = 1;
309ed2dbd31SArchie Cobbs 	priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
310ed2dbd31SArchie Cobbs 	priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
311ed2dbd31SArchie Cobbs 	priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
312ed2dbd31SArchie Cobbs 
313069154d5SJulian Elischer 	/*
314069154d5SJulian Elischer 	 * This node has all kinds of stuff that could be screwed by SMP.
315069154d5SJulian Elischer 	 * Until it gets it's own internal protection, we go through in
316053359b7SPedro F. Giffuni 	 * single file. This could hurt a machine bridging between two
317069154d5SJulian Elischer 	 * GB ethernets so it should be fixed.
318069154d5SJulian Elischer 	 * When it's fixed the process SHOULD NOT SLEEP, spinlocks please!
319069154d5SJulian Elischer 	 * (and atomic ops )
320069154d5SJulian Elischer 	 */
32130400f03SJulian Elischer 	NG_NODE_FORCE_WRITER(node);
32230400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
323069154d5SJulian Elischer 	priv->node = node;
324ed2dbd31SArchie Cobbs 
3256c12c2b1SArchie Cobbs 	/* Start timer; timer is always running while node is alive */
326e0d32af7SGleb Smirnoff 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
3276c12c2b1SArchie Cobbs 
3286c12c2b1SArchie Cobbs 	/* Done */
329ed2dbd31SArchie Cobbs 	return (0);
330ed2dbd31SArchie Cobbs }
331ed2dbd31SArchie Cobbs 
332ed2dbd31SArchie Cobbs /*
333ed2dbd31SArchie Cobbs  * Method for attaching a new hook
334ed2dbd31SArchie Cobbs  */
335ed2dbd31SArchie Cobbs static	int
336ed2dbd31SArchie Cobbs ng_bridge_newhook(node_p node, hook_p hook, const char *name)
337ed2dbd31SArchie Cobbs {
33830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
339631cabbaSGleb Smirnoff 	char linkName[NG_HOOKSIZ];
340631cabbaSGleb Smirnoff 	u_int32_t linkNum;
341631cabbaSGleb Smirnoff 	link_p link;
342*f961caf2SLutz Donnerhacke 	const char *prefix = NG_BRIDGE_HOOK_LINK_PREFIX;
343*f961caf2SLutz Donnerhacke 	bool isUplink;
344*f961caf2SLutz Donnerhacke 
345*f961caf2SLutz Donnerhacke 	/* Check for a link hook */
346*f961caf2SLutz Donnerhacke 	if (strlen(name) <= strlen(prefix))
347*f961caf2SLutz Donnerhacke 		return (EINVAL);       /* Unknown hook name */
348*f961caf2SLutz Donnerhacke 
349*f961caf2SLutz Donnerhacke 	isUplink = (name[0] == 'u');
350*f961caf2SLutz Donnerhacke 	if (isUplink)
351*f961caf2SLutz Donnerhacke 		prefix = NG_BRIDGE_HOOK_UPLINK_PREFIX;
352ed2dbd31SArchie Cobbs 
353631cabbaSGleb Smirnoff 	/* primitive parsing */
354*f961caf2SLutz Donnerhacke 	linkNum = strtoul(name + strlen(prefix), NULL, 10);
355631cabbaSGleb Smirnoff 	/* validation by comparing against the reconstucted name  */
356*f961caf2SLutz Donnerhacke 	snprintf(linkName, sizeof(linkName), "%s%u", prefix, linkNum);
357631cabbaSGleb Smirnoff 	if (strcmp(linkName, name) != 0)
358ed2dbd31SArchie Cobbs 		return (EINVAL);
359631cabbaSGleb Smirnoff 
360*f961caf2SLutz Donnerhacke 	if (linkNum == 0 && isUplink)
361*f961caf2SLutz Donnerhacke 		return (EINVAL);
362*f961caf2SLutz Donnerhacke 
363631cabbaSGleb Smirnoff 	if(NG_PEER_NODE(hook) == node)
364631cabbaSGleb Smirnoff 	        return (ELOOP);
365631cabbaSGleb Smirnoff 
366*f961caf2SLutz Donnerhacke 	link = malloc(sizeof(*link), M_NETGRAPH_BRIDGE, M_ZERO);
367631cabbaSGleb Smirnoff 	if (link == NULL)
368ed2dbd31SArchie Cobbs 		return (ENOMEM);
369*f961caf2SLutz Donnerhacke 
370631cabbaSGleb Smirnoff 	link->hook = hook;
371*f961caf2SLutz Donnerhacke 	if (isUplink) {
372*f961caf2SLutz Donnerhacke 		link->learnMac = 0;
373*f961caf2SLutz Donnerhacke 		link->sendUnknown = 1;
374*f961caf2SLutz Donnerhacke 	} else {
375*f961caf2SLutz Donnerhacke 		link->learnMac = 1;
376*f961caf2SLutz Donnerhacke 		link->sendUnknown = 1;
377*f961caf2SLutz Donnerhacke 	}
378*f961caf2SLutz Donnerhacke 
379631cabbaSGleb Smirnoff 	NG_HOOK_SET_PRIVATE(hook, link);
380ed2dbd31SArchie Cobbs 	priv->numLinks++;
381ed2dbd31SArchie Cobbs 	return (0);
382ed2dbd31SArchie Cobbs }
383ed2dbd31SArchie Cobbs 
384ed2dbd31SArchie Cobbs /*
385ed2dbd31SArchie Cobbs  * Receive a control message
386ed2dbd31SArchie Cobbs  */
387ed2dbd31SArchie Cobbs static int
3880b951c55SGleb Smirnoff ng_bridge_reset_link(hook_p hook, void *arg __unused)
389631cabbaSGleb Smirnoff {
390631cabbaSGleb Smirnoff 	link_p priv = NG_HOOK_PRIVATE(hook);
391631cabbaSGleb Smirnoff 
392631cabbaSGleb Smirnoff 	priv->loopCount = 0;
393631cabbaSGleb Smirnoff 	bzero(&priv->stats, sizeof(priv->stats));
3940b951c55SGleb Smirnoff 
3950b951c55SGleb Smirnoff 	return (1);
396631cabbaSGleb Smirnoff }
397631cabbaSGleb Smirnoff 
398631cabbaSGleb Smirnoff static int
399069154d5SJulian Elischer ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
400ed2dbd31SArchie Cobbs {
40130400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
402ed2dbd31SArchie Cobbs 	struct ng_mesg *resp = NULL;
403ed2dbd31SArchie Cobbs 	int error = 0;
404069154d5SJulian Elischer 	struct ng_mesg *msg;
405ed2dbd31SArchie Cobbs 
406069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
407ed2dbd31SArchie Cobbs 	switch (msg->header.typecookie) {
408aeaef7d5SBjoern A. Zeeb #ifdef NGM_BRIDGE_TABLE_ABI
409aeaef7d5SBjoern A. Zeeb 	case NGM_BRIDGE_COOKIE_TBL:
410aeaef7d5SBjoern A. Zeeb 		switch (msg->header.cmd) {
411aeaef7d5SBjoern A. Zeeb 		case NGM_BRIDGE_GET_CONFIG:
412aeaef7d5SBjoern A. Zeeb 		    {
413aeaef7d5SBjoern A. Zeeb 			struct ng_bridge_config_tbl *conf;
414aeaef7d5SBjoern A. Zeeb 
415aeaef7d5SBjoern A. Zeeb 			NG_MKRESPONSE(resp, msg, sizeof(*conf),
416aeaef7d5SBjoern A. Zeeb 			    M_NOWAIT|M_ZERO);
417aeaef7d5SBjoern A. Zeeb 			if (resp == NULL) {
418aeaef7d5SBjoern A. Zeeb 				error = ENOMEM;
419aeaef7d5SBjoern A. Zeeb 				break;
420aeaef7d5SBjoern A. Zeeb 			}
421aeaef7d5SBjoern A. Zeeb 			conf = (struct ng_bridge_config_tbl *)resp->data;
422aeaef7d5SBjoern A. Zeeb 			conf->cfg = priv->conf;
423aeaef7d5SBjoern A. Zeeb 			break;
424aeaef7d5SBjoern A. Zeeb 		    }
425aeaef7d5SBjoern A. Zeeb 		case NGM_BRIDGE_SET_CONFIG:
426aeaef7d5SBjoern A. Zeeb 		    {
427aeaef7d5SBjoern A. Zeeb 			struct ng_bridge_config_tbl *conf;
428aeaef7d5SBjoern A. Zeeb 
429aeaef7d5SBjoern A. Zeeb 			if (msg->header.arglen != sizeof(*conf)) {
430aeaef7d5SBjoern A. Zeeb 				error = EINVAL;
431aeaef7d5SBjoern A. Zeeb 				break;
432aeaef7d5SBjoern A. Zeeb 			}
433aeaef7d5SBjoern A. Zeeb 			conf = (struct ng_bridge_config_tbl *)msg->data;
434aeaef7d5SBjoern A. Zeeb 			priv->conf = conf->cfg;
435aeaef7d5SBjoern A. Zeeb 			break;
436aeaef7d5SBjoern A. Zeeb 		    }
437aeaef7d5SBjoern A. Zeeb 		case NGM_BRIDGE_GET_TABLE:
438aeaef7d5SBjoern A. Zeeb 		    {
439aeaef7d5SBjoern A. Zeeb 			struct ng_bridge_host_tbl_ary *ary;
440aeaef7d5SBjoern A. Zeeb 			struct ng_bridge_hent *hent;
441aeaef7d5SBjoern A. Zeeb 			int i, bucket;
442aeaef7d5SBjoern A. Zeeb 
443aeaef7d5SBjoern A. Zeeb 			NG_MKRESPONSE(resp, msg, sizeof(*ary) +
444aeaef7d5SBjoern A. Zeeb 			    (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
445aeaef7d5SBjoern A. Zeeb 			if (resp == NULL) {
446aeaef7d5SBjoern A. Zeeb 				error = ENOMEM;
447aeaef7d5SBjoern A. Zeeb 				break;
448aeaef7d5SBjoern A. Zeeb 			}
449aeaef7d5SBjoern A. Zeeb 			ary = (struct ng_bridge_host_tbl_ary *)resp->data;
450aeaef7d5SBjoern A. Zeeb 			ary->numHosts = priv->numHosts;
451aeaef7d5SBjoern A. Zeeb 			i = 0;
452aeaef7d5SBjoern A. Zeeb 			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
453aeaef7d5SBjoern A. Zeeb 				SLIST_FOREACH(hent, &priv->tab[bucket], next) {
454*f961caf2SLutz Donnerhacke 					const char *name = NG_HOOK_NAME(hent->host.link->hook);
455*f961caf2SLutz Donnerhacke 					const char *prefix = name[0] == 'u' ?
456*f961caf2SLutz Donnerhacke 					    NG_BRIDGE_HOOK_UPLINK_PREFIX :
457*f961caf2SLutz Donnerhacke 					    NG_BRIDGE_HOOK_LINK_PREFIX;
458*f961caf2SLutz Donnerhacke 
459aeaef7d5SBjoern A. Zeeb 					memcpy(ary->hosts[i].addr,
460aeaef7d5SBjoern A. Zeeb 					    hent->host.addr,
461aeaef7d5SBjoern A. Zeeb 					    sizeof(ary->hosts[i].addr));
462aeaef7d5SBjoern A. Zeeb 					ary->hosts[i].age = hent->host.age;
463aeaef7d5SBjoern A. Zeeb 					ary->hosts[i].staleness =
464aeaef7d5SBjoern A. Zeeb 					     hent->host.staleness;
465aeaef7d5SBjoern A. Zeeb 				        ary->hosts[i].linkNum = strtol(
466*f961caf2SLutz Donnerhacke 					    name + strlen(prefix), NULL, 10);
467aeaef7d5SBjoern A. Zeeb 					i++;
468aeaef7d5SBjoern A. Zeeb 				}
469aeaef7d5SBjoern A. Zeeb 			}
470aeaef7d5SBjoern A. Zeeb 			break;
471aeaef7d5SBjoern A. Zeeb 		    }
472aeaef7d5SBjoern A. Zeeb 		}
473aeaef7d5SBjoern A. Zeeb 		/* If already handled break, otherwise use new ABI. */
474aeaef7d5SBjoern A. Zeeb 		if (resp != NULL || error != 0)
475aeaef7d5SBjoern A. Zeeb 		    break;
476aeaef7d5SBjoern A. Zeeb #endif /* NGM_BRIDGE_TABLE_ABI */
477ed2dbd31SArchie Cobbs 	case NGM_BRIDGE_COOKIE:
478ed2dbd31SArchie Cobbs 		switch (msg->header.cmd) {
479ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_CONFIG:
480ed2dbd31SArchie Cobbs 		    {
481ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
482ed2dbd31SArchie Cobbs 
483ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg,
484ed2dbd31SArchie Cobbs 			    sizeof(struct ng_bridge_config), M_NOWAIT);
485ed2dbd31SArchie Cobbs 			if (resp == NULL) {
486ed2dbd31SArchie Cobbs 				error = ENOMEM;
487ed2dbd31SArchie Cobbs 				break;
488ed2dbd31SArchie Cobbs 			}
489ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)resp->data;
490ed2dbd31SArchie Cobbs 			*conf = priv->conf;	/* no sanity checking needed */
491ed2dbd31SArchie Cobbs 			break;
492ed2dbd31SArchie Cobbs 		    }
493ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_SET_CONFIG:
494ed2dbd31SArchie Cobbs 		    {
495ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
496ed2dbd31SArchie Cobbs 
497ed2dbd31SArchie Cobbs 			if (msg->header.arglen
498ed2dbd31SArchie Cobbs 			    != sizeof(struct ng_bridge_config)) {
499ed2dbd31SArchie Cobbs 				error = EINVAL;
500ed2dbd31SArchie Cobbs 				break;
501ed2dbd31SArchie Cobbs 			}
502ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)msg->data;
503ed2dbd31SArchie Cobbs 			priv->conf = *conf;
504ed2dbd31SArchie Cobbs 			break;
505ed2dbd31SArchie Cobbs 		    }
506ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_RESET:
507ed2dbd31SArchie Cobbs 		    {
508631cabbaSGleb Smirnoff 			hook_p rethook;
509ed2dbd31SArchie Cobbs 
510ed2dbd31SArchie Cobbs 			/* Flush all entries in the hash table */
511631cabbaSGleb Smirnoff 			ng_bridge_remove_hosts(priv, NULL);
512ed2dbd31SArchie Cobbs 
513ed2dbd31SArchie Cobbs 			/* Reset all loop detection counters and stats */
5140b951c55SGleb Smirnoff 			NG_NODE_FOREACH_HOOK(node, ng_bridge_reset_link, NULL,
5150b951c55SGleb Smirnoff 			    rethook);
516ed2dbd31SArchie Cobbs 			break;
517ed2dbd31SArchie Cobbs 		    }
518ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_STATS:
519ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_CLR_STATS:
520ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GETCLR_STATS:
521ed2dbd31SArchie Cobbs 		    {
522631cabbaSGleb Smirnoff 			hook_p hook;
523631cabbaSGleb Smirnoff 			link_p link;
524631cabbaSGleb Smirnoff 			char linkName[NG_HOOKSIZ];
525*f961caf2SLutz Donnerhacke 			int linkNum;
526ed2dbd31SArchie Cobbs 
527ed2dbd31SArchie Cobbs 			/* Get link number */
528ed2dbd31SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
529ed2dbd31SArchie Cobbs 				error = EINVAL;
530ed2dbd31SArchie Cobbs 				break;
531ed2dbd31SArchie Cobbs 			}
532*f961caf2SLutz Donnerhacke 			linkNum = *((int32_t *)msg->data);
533*f961caf2SLutz Donnerhacke 			if (linkNum < 0)
534631cabbaSGleb Smirnoff 				snprintf(linkName, sizeof(linkName),
535*f961caf2SLutz Donnerhacke 				    "%s%u", NG_BRIDGE_HOOK_UPLINK_PREFIX, -linkNum);
536*f961caf2SLutz Donnerhacke 			else
537*f961caf2SLutz Donnerhacke 				snprintf(linkName, sizeof(linkName),
538*f961caf2SLutz Donnerhacke 				    "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX, linkNum);
539631cabbaSGleb Smirnoff 
540631cabbaSGleb Smirnoff 			if ((hook = ng_findhook(node, linkName)) == NULL) {
541ed2dbd31SArchie Cobbs 				error = ENOTCONN;
542ed2dbd31SArchie Cobbs 				break;
543ed2dbd31SArchie Cobbs 			}
544631cabbaSGleb Smirnoff 			link = NG_HOOK_PRIVATE(hook);
545ed2dbd31SArchie Cobbs 
546ed2dbd31SArchie Cobbs 			/* Get/clear stats */
547ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
548ed2dbd31SArchie Cobbs 				NG_MKRESPONSE(resp, msg,
549ed2dbd31SArchie Cobbs 				    sizeof(link->stats), M_NOWAIT);
550ed2dbd31SArchie Cobbs 				if (resp == NULL) {
551ed2dbd31SArchie Cobbs 					error = ENOMEM;
552ed2dbd31SArchie Cobbs 					break;
553ed2dbd31SArchie Cobbs 				}
554ed2dbd31SArchie Cobbs 				bcopy(&link->stats,
555ed2dbd31SArchie Cobbs 				    resp->data, sizeof(link->stats));
556ed2dbd31SArchie Cobbs 			}
557ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
558ed2dbd31SArchie Cobbs 				bzero(&link->stats, sizeof(link->stats));
559ed2dbd31SArchie Cobbs 			break;
560ed2dbd31SArchie Cobbs 		    }
561ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_TABLE:
562ed2dbd31SArchie Cobbs 		    {
563ed2dbd31SArchie Cobbs 			struct ng_bridge_host_ary *ary;
564ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *hent;
565ed2dbd31SArchie Cobbs 			int i = 0, bucket;
566ed2dbd31SArchie Cobbs 
567ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*ary)
568ed2dbd31SArchie Cobbs 			    + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
569ed2dbd31SArchie Cobbs 			if (resp == NULL) {
570ed2dbd31SArchie Cobbs 				error = ENOMEM;
571ed2dbd31SArchie Cobbs 				break;
572ed2dbd31SArchie Cobbs 			}
573ed2dbd31SArchie Cobbs 			ary = (struct ng_bridge_host_ary *)resp->data;
574ed2dbd31SArchie Cobbs 			ary->numHosts = priv->numHosts;
575ed2dbd31SArchie Cobbs 			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
576631cabbaSGleb Smirnoff 				SLIST_FOREACH(hent, &priv->tab[bucket], next) {
577631cabbaSGleb Smirnoff 					memcpy(ary->hosts[i].addr,
578631cabbaSGleb Smirnoff 					       hent->host.addr,
579631cabbaSGleb Smirnoff 					       sizeof(ary->hosts[i].addr));
580631cabbaSGleb Smirnoff 					ary->hosts[i].age       = hent->host.age;
581631cabbaSGleb Smirnoff 					ary->hosts[i].staleness = hent->host.staleness;
582631cabbaSGleb Smirnoff 					strncpy(ary->hosts[i].hook,
583631cabbaSGleb Smirnoff 						NG_HOOK_NAME(hent->host.link->hook),
584631cabbaSGleb Smirnoff 						sizeof(ary->hosts[i].hook));
585631cabbaSGleb Smirnoff 					i++;
586631cabbaSGleb Smirnoff 				}
587ed2dbd31SArchie Cobbs 			}
588ed2dbd31SArchie Cobbs 			break;
589ed2dbd31SArchie Cobbs 		    }
590f8aab721SMarko Zec 		case NGM_BRIDGE_SET_PERSISTENT:
591f8aab721SMarko Zec 		    {
592f8aab721SMarko Zec 			priv->persistent = 1;
593f8aab721SMarko Zec 			break;
594f8aab721SMarko Zec 		    }
595ed2dbd31SArchie Cobbs 		default:
596ed2dbd31SArchie Cobbs 			error = EINVAL;
597ed2dbd31SArchie Cobbs 			break;
598ed2dbd31SArchie Cobbs 		}
599ed2dbd31SArchie Cobbs 		break;
600ed2dbd31SArchie Cobbs 	default:
601ed2dbd31SArchie Cobbs 		error = EINVAL;
602ed2dbd31SArchie Cobbs 		break;
603ed2dbd31SArchie Cobbs 	}
604ed2dbd31SArchie Cobbs 
605ed2dbd31SArchie Cobbs 	/* Done */
606069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
607069154d5SJulian Elischer 	NG_FREE_MSG(msg);
608ed2dbd31SArchie Cobbs 	return (error);
609ed2dbd31SArchie Cobbs }
610ed2dbd31SArchie Cobbs 
611ed2dbd31SArchie Cobbs /*
612ed2dbd31SArchie Cobbs  * Receive data on a hook
613ed2dbd31SArchie Cobbs  */
614631cabbaSGleb Smirnoff struct ng_bridge_send_ctx {
615631cabbaSGleb Smirnoff 	link_p foundFirst, incoming;
616631cabbaSGleb Smirnoff 	struct mbuf * m;
617631cabbaSGleb Smirnoff 	int manycast, error;
618631cabbaSGleb Smirnoff };
619631cabbaSGleb Smirnoff 
620631cabbaSGleb Smirnoff static int
6210b951c55SGleb Smirnoff ng_bridge_send_ctx(hook_p dst, void *arg)
622631cabbaSGleb Smirnoff {
6230b951c55SGleb Smirnoff 	struct ng_bridge_send_ctx *ctx = arg;
624631cabbaSGleb Smirnoff 	link_p destLink = NG_HOOK_PRIVATE(dst);
625631cabbaSGleb Smirnoff 	struct mbuf *m2 = NULL;
626631cabbaSGleb Smirnoff 	int error = 0;
627631cabbaSGleb Smirnoff 
628631cabbaSGleb Smirnoff 	/* Skip incoming link */
629631cabbaSGleb Smirnoff 	if (destLink == ctx->incoming) {
630631cabbaSGleb Smirnoff 		return (1);
631631cabbaSGleb Smirnoff 	}
632631cabbaSGleb Smirnoff 
633*f961caf2SLutz Donnerhacke 	/* Skip sending unknowns to undesired links  */
634*f961caf2SLutz Donnerhacke 	if (!ctx->manycast && !destLink->sendUnknown)
635*f961caf2SLutz Donnerhacke 		return (1);
636*f961caf2SLutz Donnerhacke 
637631cabbaSGleb Smirnoff 	if (ctx->foundFirst == NULL) {
638631cabbaSGleb Smirnoff 		/*
639631cabbaSGleb Smirnoff 		 * This is the first usable link we have found.
640631cabbaSGleb Smirnoff 		 * Reserve it for the originals.
641631cabbaSGleb Smirnoff 		 * If we never find another we save a copy.
642631cabbaSGleb Smirnoff 		 */
643631cabbaSGleb Smirnoff 		ctx->foundFirst = destLink;
644631cabbaSGleb Smirnoff 		return (1);
645631cabbaSGleb Smirnoff 	}
646631cabbaSGleb Smirnoff 
647631cabbaSGleb Smirnoff 	/*
648631cabbaSGleb Smirnoff 	 * It's usable link but not the reserved (first) one.
649631cabbaSGleb Smirnoff 	 * Copy mbuf info for sending.
650631cabbaSGleb Smirnoff 	 */
651631cabbaSGleb Smirnoff 	m2 = m_dup(ctx->m, M_NOWAIT);	/* XXX m_copypacket() */
652631cabbaSGleb Smirnoff 	if (m2 == NULL) {
653631cabbaSGleb Smirnoff 		ctx->incoming->stats.memoryFailures++;
654631cabbaSGleb Smirnoff 		ctx->error = ENOBUFS;
655631cabbaSGleb Smirnoff 		return (0);	       /* abort loop */
656631cabbaSGleb Smirnoff 	}
657631cabbaSGleb Smirnoff 
658631cabbaSGleb Smirnoff 	/* Update stats */
659631cabbaSGleb Smirnoff 	destLink->stats.xmitPackets++;
660631cabbaSGleb Smirnoff 	destLink->stats.xmitOctets += m2->m_pkthdr.len;
661631cabbaSGleb Smirnoff 	switch (ctx->manycast) {
662631cabbaSGleb Smirnoff 	 default:					/* unknown unicast */
663631cabbaSGleb Smirnoff 		break;
664631cabbaSGleb Smirnoff 	 case 1:					/* multicast */
665631cabbaSGleb Smirnoff 		destLink->stats.xmitMulticasts++;
666631cabbaSGleb Smirnoff 		break;
667631cabbaSGleb Smirnoff 	 case 2:					/* broadcast */
668631cabbaSGleb Smirnoff 		destLink->stats.xmitBroadcasts++;
669631cabbaSGleb Smirnoff 		break;
670631cabbaSGleb Smirnoff 	}
671631cabbaSGleb Smirnoff 
672631cabbaSGleb Smirnoff 	/* Send packet */
673631cabbaSGleb Smirnoff 	NG_SEND_DATA_ONLY(error, destLink->hook, m2);
674631cabbaSGleb Smirnoff 	if(error)
675631cabbaSGleb Smirnoff 	  ctx->error = error;
676631cabbaSGleb Smirnoff 	return (1);
677631cabbaSGleb Smirnoff }
678631cabbaSGleb Smirnoff 
679ed2dbd31SArchie Cobbs static int
680069154d5SJulian Elischer ng_bridge_rcvdata(hook_p hook, item_p item)
681ed2dbd31SArchie Cobbs {
68230400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
68330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
684ed2dbd31SArchie Cobbs 	struct ng_bridge_host *host;
685ed2dbd31SArchie Cobbs 	struct ether_header *eh;
686631cabbaSGleb Smirnoff 	struct ng_bridge_send_ctx ctx = { 0 };
687631cabbaSGleb Smirnoff 	hook_p ret;
688ed2dbd31SArchie Cobbs 
689631cabbaSGleb Smirnoff 	NGI_GET_M(item, ctx.m);
690ed2dbd31SArchie Cobbs 
691631cabbaSGleb Smirnoff 	ctx.incoming = NG_HOOK_PRIVATE(hook);
692ed2dbd31SArchie Cobbs 	/* Sanity check packet and pull up header */
693631cabbaSGleb Smirnoff 	if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) {
694631cabbaSGleb Smirnoff 		ctx.incoming->stats.recvRunts++;
695069154d5SJulian Elischer 		NG_FREE_ITEM(item);
696631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
697ed2dbd31SArchie Cobbs 		return (EINVAL);
698ed2dbd31SArchie Cobbs 	}
699631cabbaSGleb Smirnoff 	if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) {
700631cabbaSGleb Smirnoff 		ctx.incoming->stats.memoryFailures++;
701069154d5SJulian Elischer 		NG_FREE_ITEM(item);
702ed2dbd31SArchie Cobbs 		return (ENOBUFS);
703ed2dbd31SArchie Cobbs 	}
704631cabbaSGleb Smirnoff 	eh = mtod(ctx.m, struct ether_header *);
705ed2dbd31SArchie Cobbs 	if ((eh->ether_shost[0] & 1) != 0) {
706631cabbaSGleb Smirnoff 		ctx.incoming->stats.recvInvalid++;
707069154d5SJulian Elischer 		NG_FREE_ITEM(item);
708631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
709ed2dbd31SArchie Cobbs 		return (EINVAL);
710ed2dbd31SArchie Cobbs 	}
711ed2dbd31SArchie Cobbs 
712ed2dbd31SArchie Cobbs 	/* Is link disabled due to a loopback condition? */
713631cabbaSGleb Smirnoff 	if (ctx.incoming->loopCount != 0) {
714631cabbaSGleb Smirnoff 		ctx.incoming->stats.loopDrops++;
715069154d5SJulian Elischer 		NG_FREE_ITEM(item);
716631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
717ed2dbd31SArchie Cobbs 		return (ELOOP);		/* XXX is this an appropriate error? */
718ed2dbd31SArchie Cobbs 	}
719ed2dbd31SArchie Cobbs 
720ed2dbd31SArchie Cobbs 	/* Update stats */
721631cabbaSGleb Smirnoff 	ctx.incoming->stats.recvPackets++;
722631cabbaSGleb Smirnoff 	ctx.incoming->stats.recvOctets += ctx.m->m_pkthdr.len;
723631cabbaSGleb Smirnoff 	if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) {
724ed2dbd31SArchie Cobbs 		if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
725631cabbaSGleb Smirnoff 			ctx.incoming->stats.recvBroadcasts++;
726631cabbaSGleb Smirnoff 			ctx.manycast = 2;
727ed2dbd31SArchie Cobbs 		} else
728631cabbaSGleb Smirnoff 			ctx.incoming->stats.recvMulticasts++;
729ed2dbd31SArchie Cobbs 	}
730ed2dbd31SArchie Cobbs 
731ed2dbd31SArchie Cobbs 	/* Look up packet's source Ethernet address in hashtable */
732ed2dbd31SArchie Cobbs 	if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
733ed2dbd31SArchie Cobbs 		/* Update time since last heard from this host */
734ed2dbd31SArchie Cobbs 		host->staleness = 0;
735ed2dbd31SArchie Cobbs 
736ed2dbd31SArchie Cobbs 		/* Did host jump to a different link? */
737631cabbaSGleb Smirnoff 		if (host->link != ctx.incoming) {
738ed2dbd31SArchie Cobbs 			/*
739ed2dbd31SArchie Cobbs 			 * If the host's old link was recently established
740ed2dbd31SArchie Cobbs 			 * on the old link and it's already jumped to a new
741ed2dbd31SArchie Cobbs 			 * link, declare a loopback condition.
742ed2dbd31SArchie Cobbs 			 */
743ed2dbd31SArchie Cobbs 			if (host->age < priv->conf.minStableAge) {
744ed2dbd31SArchie Cobbs 				/* Log the problem */
745ed2dbd31SArchie Cobbs 				if (priv->conf.debugLevel >= 2) {
746631cabbaSGleb Smirnoff 					struct ifnet *ifp = ctx.m->m_pkthdr.rcvif;
747ed2dbd31SArchie Cobbs 					char suffix[32];
748ed2dbd31SArchie Cobbs 
749ed2dbd31SArchie Cobbs 					if (ifp != NULL)
750ed2dbd31SArchie Cobbs 						snprintf(suffix, sizeof(suffix),
7519bf40edeSBrooks Davis 						    " (%s)", ifp->if_xname);
752ed2dbd31SArchie Cobbs 					else
753ed2dbd31SArchie Cobbs 						*suffix = '\0';
754ed2dbd31SArchie Cobbs 					log(LOG_WARNING, "ng_bridge: %s:"
755ed2dbd31SArchie Cobbs 					    " loopback detected on %s%s\n",
756ed2dbd31SArchie Cobbs 					    ng_bridge_nodename(node),
75730400f03SJulian Elischer 					    NG_HOOK_NAME(hook), suffix);
758ed2dbd31SArchie Cobbs 				}
759ed2dbd31SArchie Cobbs 
760ed2dbd31SArchie Cobbs 				/* Mark link as linka non grata */
761631cabbaSGleb Smirnoff 				ctx.incoming->loopCount = priv->conf.loopTimeout;
762631cabbaSGleb Smirnoff 				ctx.incoming->stats.loopDetects++;
763ed2dbd31SArchie Cobbs 
764ed2dbd31SArchie Cobbs 				/* Forget all hosts on this link */
765631cabbaSGleb Smirnoff 				ng_bridge_remove_hosts(priv, ctx.incoming);
766ed2dbd31SArchie Cobbs 
767ed2dbd31SArchie Cobbs 				/* Drop packet */
768631cabbaSGleb Smirnoff 				ctx.incoming->stats.loopDrops++;
769069154d5SJulian Elischer 				NG_FREE_ITEM(item);
770631cabbaSGleb Smirnoff 				NG_FREE_M(ctx.m);
771ed2dbd31SArchie Cobbs 				return (ELOOP);		/* XXX appropriate? */
772ed2dbd31SArchie Cobbs 			}
773ed2dbd31SArchie Cobbs 
774ed2dbd31SArchie Cobbs 			/* Move host over to new link */
775631cabbaSGleb Smirnoff 			host->link = ctx.incoming;
776ed2dbd31SArchie Cobbs 			host->age = 0;
777ed2dbd31SArchie Cobbs 		}
778*f961caf2SLutz Donnerhacke 	} else if (ctx.incoming->learnMac) {
779631cabbaSGleb Smirnoff 		if (!ng_bridge_put(priv, eh->ether_shost, ctx.incoming)) {
780631cabbaSGleb Smirnoff 			ctx.incoming->stats.memoryFailures++;
781069154d5SJulian Elischer 			NG_FREE_ITEM(item);
782631cabbaSGleb Smirnoff 			NG_FREE_M(ctx.m);
783ed2dbd31SArchie Cobbs 			return (ENOMEM);
784ed2dbd31SArchie Cobbs 		}
785ed2dbd31SArchie Cobbs 	}
786ed2dbd31SArchie Cobbs 
787ed2dbd31SArchie Cobbs 	/* Run packet through ipfw processing, if enabled */
7889b932e9eSAndre Oppermann #if 0
7890b4b0b0fSJulian Elischer 	if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) {
790ed2dbd31SArchie Cobbs 		/* XXX not implemented yet */
791ed2dbd31SArchie Cobbs 	}
7929b932e9eSAndre Oppermann #endif
793ed2dbd31SArchie Cobbs 
794ed2dbd31SArchie Cobbs 	/*
795ed2dbd31SArchie Cobbs 	 * If unicast and destination host known, deliver to host's link,
796ed2dbd31SArchie Cobbs 	 * unless it is the same link as the packet came in on.
797ed2dbd31SArchie Cobbs 	 */
798631cabbaSGleb Smirnoff 	if (!ctx.manycast) {
799ed2dbd31SArchie Cobbs 		/* Determine packet destination link */
800ed2dbd31SArchie Cobbs 		if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
801631cabbaSGleb Smirnoff 			link_p destLink = host->link;
802ed2dbd31SArchie Cobbs 
803ed2dbd31SArchie Cobbs 			/* If destination same as incoming link, do nothing */
804631cabbaSGleb Smirnoff 			if (destLink == ctx.incoming) {
805069154d5SJulian Elischer 				NG_FREE_ITEM(item);
806631cabbaSGleb Smirnoff 				NG_FREE_M(ctx.m);
807ed2dbd31SArchie Cobbs 				return (0);
808ed2dbd31SArchie Cobbs 			}
809ed2dbd31SArchie Cobbs 
810ed2dbd31SArchie Cobbs 			/* Deliver packet out the destination link */
811ed2dbd31SArchie Cobbs 			destLink->stats.xmitPackets++;
812631cabbaSGleb Smirnoff 			destLink->stats.xmitOctets += ctx.m->m_pkthdr.len;
813631cabbaSGleb Smirnoff 			NG_FWD_NEW_DATA(ctx.error, item, destLink->hook, ctx.m);
814631cabbaSGleb Smirnoff 			return (ctx.error);
815ed2dbd31SArchie Cobbs 		}
816ed2dbd31SArchie Cobbs 
817ed2dbd31SArchie Cobbs 		/* Destination host is not known */
818631cabbaSGleb Smirnoff 		ctx.incoming->stats.recvUnknown++;
819ed2dbd31SArchie Cobbs 	}
820ed2dbd31SArchie Cobbs 
821ed2dbd31SArchie Cobbs 	/* Distribute unknown, multicast, broadcast pkts to all other links */
822631cabbaSGleb Smirnoff 	NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx, ret);
823ed2dbd31SArchie Cobbs 
824069154d5SJulian Elischer 	/* If we never saw a good link, leave. */
825631cabbaSGleb Smirnoff 	if (ctx.foundFirst == NULL || ctx.error != 0) {
826069154d5SJulian Elischer 		NG_FREE_ITEM(item);
827631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
828631cabbaSGleb Smirnoff 		return (ctx.error);
829069154d5SJulian Elischer 	}
830069154d5SJulian Elischer 
831069154d5SJulian Elischer 	/*
832069154d5SJulian Elischer 	 * If we've sent all the others, send the original
833069154d5SJulian Elischer 	 * on the first link we found.
834069154d5SJulian Elischer 	 */
835631cabbaSGleb Smirnoff 	NG_FWD_NEW_DATA(ctx.error, item, ctx.foundFirst->hook, ctx.m);
836631cabbaSGleb Smirnoff 	return (ctx.error);
837ed2dbd31SArchie Cobbs }
838ed2dbd31SArchie Cobbs 
839ed2dbd31SArchie Cobbs /*
840ed2dbd31SArchie Cobbs  * Shutdown node
841ed2dbd31SArchie Cobbs  */
842ed2dbd31SArchie Cobbs static int
843069154d5SJulian Elischer ng_bridge_shutdown(node_p node)
844ed2dbd31SArchie Cobbs {
84530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
846ed2dbd31SArchie Cobbs 
8476c12c2b1SArchie Cobbs 	/*
848195cf617SRuslan Ermilov 	 * Shut down everything including the timer.  Even if the
849195cf617SRuslan Ermilov 	 * callout has already been dequeued and is about to be
850195cf617SRuslan Ermilov 	 * run, ng_bridge_timeout() won't be fired as the node
851195cf617SRuslan Ermilov 	 * is already marked NGF_INVALID, so we're safe to free
852195cf617SRuslan Ermilov 	 * the node now.
8536c12c2b1SArchie Cobbs 	 */
854ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
855ed2dbd31SArchie Cobbs 	    ("%s: numLinks=%d numHosts=%d",
8566e551fb6SDavid E. O'Brien 	    __func__, priv->numLinks, priv->numHosts));
857195cf617SRuslan Ermilov 	ng_uncallout(&priv->timer, node);
858195cf617SRuslan Ermilov 	NG_NODE_SET_PRIVATE(node, NULL);
859195cf617SRuslan Ermilov 	NG_NODE_UNREF(node);
8601ede983cSDag-Erling Smørgrav 	free(priv->tab, M_NETGRAPH_BRIDGE);
8611ede983cSDag-Erling Smørgrav 	free(priv, M_NETGRAPH_BRIDGE);
862ed2dbd31SArchie Cobbs 	return (0);
863ed2dbd31SArchie Cobbs }
864ed2dbd31SArchie Cobbs 
865ed2dbd31SArchie Cobbs /*
866ed2dbd31SArchie Cobbs  * Hook disconnection.
867ed2dbd31SArchie Cobbs  */
868ed2dbd31SArchie Cobbs static int
869ed2dbd31SArchie Cobbs ng_bridge_disconnect(hook_p hook)
870ed2dbd31SArchie Cobbs {
87130400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
872631cabbaSGleb Smirnoff 	link_p link = NG_HOOK_PRIVATE(hook);
873ed2dbd31SArchie Cobbs 
874ed2dbd31SArchie Cobbs 	/* Remove all hosts associated with this link */
875631cabbaSGleb Smirnoff 	ng_bridge_remove_hosts(priv, link);
876ed2dbd31SArchie Cobbs 
877ed2dbd31SArchie Cobbs 	/* Free associated link information */
878631cabbaSGleb Smirnoff 	free(link, M_NETGRAPH_BRIDGE);
879ed2dbd31SArchie Cobbs 	priv->numLinks--;
880ed2dbd31SArchie Cobbs 
881ed2dbd31SArchie Cobbs 	/* If no more hooks, go away */
88230400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
883f8aab721SMarko Zec 	    && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
884f8aab721SMarko Zec 	    && !priv->persistent) {
88530400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));
88630400f03SJulian Elischer 	}
887ed2dbd31SArchie Cobbs 	return (0);
888ed2dbd31SArchie Cobbs }
889ed2dbd31SArchie Cobbs 
890ed2dbd31SArchie Cobbs /******************************************************************
891ed2dbd31SArchie Cobbs 		    HASH TABLE FUNCTIONS
892ed2dbd31SArchie Cobbs ******************************************************************/
893ed2dbd31SArchie Cobbs 
894ed2dbd31SArchie Cobbs /*
895ed2dbd31SArchie Cobbs  * Hash algorithm
896ed2dbd31SArchie Cobbs  */
897ed2dbd31SArchie Cobbs #define HASH(addr,mask)		( (((const u_int16_t *)(addr))[0] 	\
898ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[1] 	\
899ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
900ed2dbd31SArchie Cobbs 
901ed2dbd31SArchie Cobbs /*
902ed2dbd31SArchie Cobbs  * Find a host entry in the table.
903ed2dbd31SArchie Cobbs  */
904ed2dbd31SArchie Cobbs static struct ng_bridge_host *
905ed2dbd31SArchie Cobbs ng_bridge_get(priv_p priv, const u_char *addr)
906ed2dbd31SArchie Cobbs {
907ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
908ed2dbd31SArchie Cobbs 	struct ng_bridge_hent *hent;
909ed2dbd31SArchie Cobbs 
910ed2dbd31SArchie Cobbs 	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
911ed2dbd31SArchie Cobbs 		if (ETHER_EQUAL(hent->host.addr, addr))
912ed2dbd31SArchie Cobbs 			return (&hent->host);
913ed2dbd31SArchie Cobbs 	}
914ed2dbd31SArchie Cobbs 	return (NULL);
915ed2dbd31SArchie Cobbs }
916ed2dbd31SArchie Cobbs 
917ed2dbd31SArchie Cobbs /*
918ed2dbd31SArchie Cobbs  * Add a new host entry to the table. This assumes the host doesn't
919ed2dbd31SArchie Cobbs  * already exist in the table. Returns 1 on success, 0 if there
920ed2dbd31SArchie Cobbs  * was a memory allocation failure.
921ed2dbd31SArchie Cobbs  */
922ed2dbd31SArchie Cobbs static int
923631cabbaSGleb Smirnoff ng_bridge_put(priv_p priv, const u_char *addr, link_p link)
924ed2dbd31SArchie Cobbs {
925ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
926ed2dbd31SArchie Cobbs 	struct ng_bridge_hent *hent;
927ed2dbd31SArchie Cobbs 
928ed2dbd31SArchie Cobbs #ifdef INVARIANTS
929ed2dbd31SArchie Cobbs 	/* Assert that entry does not already exist in hashtable */
930ed2dbd31SArchie Cobbs 	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
931ed2dbd31SArchie Cobbs 		KASSERT(!ETHER_EQUAL(hent->host.addr, addr),
9326e551fb6SDavid E. O'Brien 		    ("%s: entry %6D exists in table", __func__, addr, ":"));
933ed2dbd31SArchie Cobbs 	}
934ed2dbd31SArchie Cobbs #endif
935ed2dbd31SArchie Cobbs 
936ed2dbd31SArchie Cobbs 	/* Allocate and initialize new hashtable entry */
9371ede983cSDag-Erling Smørgrav 	hent = malloc(sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT);
938ed2dbd31SArchie Cobbs 	if (hent == NULL)
939ed2dbd31SArchie Cobbs 		return (0);
940ed2dbd31SArchie Cobbs 	bcopy(addr, hent->host.addr, ETHER_ADDR_LEN);
941631cabbaSGleb Smirnoff 	hent->host.link = link;
942ed2dbd31SArchie Cobbs 	hent->host.staleness = 0;
943ed2dbd31SArchie Cobbs 	hent->host.age = 0;
944ed2dbd31SArchie Cobbs 
945ed2dbd31SArchie Cobbs 	/* Add new element to hash bucket */
946ed2dbd31SArchie Cobbs 	SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next);
947ed2dbd31SArchie Cobbs 	priv->numHosts++;
948ed2dbd31SArchie Cobbs 
949ed2dbd31SArchie Cobbs 	/* Resize table if necessary */
950ed2dbd31SArchie Cobbs 	ng_bridge_rehash(priv);
951ed2dbd31SArchie Cobbs 	return (1);
952ed2dbd31SArchie Cobbs }
953ed2dbd31SArchie Cobbs 
954ed2dbd31SArchie Cobbs /*
955ed2dbd31SArchie Cobbs  * Resize the hash table. We try to maintain the number of buckets
956ed2dbd31SArchie Cobbs  * such that the load factor is in the range 0.25 to 1.0.
957ed2dbd31SArchie Cobbs  *
958ed2dbd31SArchie Cobbs  * If we can't get the new memory then we silently fail. This is OK
959ed2dbd31SArchie Cobbs  * because things will still work and we'll try again soon anyway.
960ed2dbd31SArchie Cobbs  */
961ed2dbd31SArchie Cobbs static void
962ed2dbd31SArchie Cobbs ng_bridge_rehash(priv_p priv)
963ed2dbd31SArchie Cobbs {
964ed2dbd31SArchie Cobbs 	struct ng_bridge_bucket *newTab;
965ed2dbd31SArchie Cobbs 	int oldBucket, newBucket;
966ed2dbd31SArchie Cobbs 	int newNumBuckets;
967ed2dbd31SArchie Cobbs 	u_int newMask;
968ed2dbd31SArchie Cobbs 
969ed2dbd31SArchie Cobbs 	/* Is table too full or too empty? */
970ed2dbd31SArchie Cobbs 	if (priv->numHosts > priv->numBuckets
971ed2dbd31SArchie Cobbs 	    && (priv->numBuckets << 1) <= MAX_BUCKETS)
972ed2dbd31SArchie Cobbs 		newNumBuckets = priv->numBuckets << 1;
973ed2dbd31SArchie Cobbs 	else if (priv->numHosts < (priv->numBuckets >> 2)
974ed2dbd31SArchie Cobbs 	    && (priv->numBuckets >> 2) >= MIN_BUCKETS)
975ed2dbd31SArchie Cobbs 		newNumBuckets = priv->numBuckets >> 2;
976ed2dbd31SArchie Cobbs 	else
977ed2dbd31SArchie Cobbs 		return;
978ed2dbd31SArchie Cobbs 	newMask = newNumBuckets - 1;
979ed2dbd31SArchie Cobbs 
980ed2dbd31SArchie Cobbs 	/* Allocate and initialize new table */
981ac2fffa4SPedro F. Giffuni 	newTab = malloc(newNumBuckets * sizeof(*newTab),
982e11e3f18SDag-Erling Smørgrav 	    M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
983ed2dbd31SArchie Cobbs 	if (newTab == NULL)
984ed2dbd31SArchie Cobbs 		return;
985ed2dbd31SArchie Cobbs 
986ed2dbd31SArchie Cobbs 	/* Move all entries from old table to new table */
987ed2dbd31SArchie Cobbs 	for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
988ed2dbd31SArchie Cobbs 		struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
989ed2dbd31SArchie Cobbs 
990ed2dbd31SArchie Cobbs 		while (!SLIST_EMPTY(oldList)) {
991ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent
992ed2dbd31SArchie Cobbs 			    = SLIST_FIRST(oldList);
993ed2dbd31SArchie Cobbs 
994ed2dbd31SArchie Cobbs 			SLIST_REMOVE_HEAD(oldList, next);
995ed2dbd31SArchie Cobbs 			newBucket = HASH(hent->host.addr, newMask);
996ed2dbd31SArchie Cobbs 			SLIST_INSERT_HEAD(&newTab[newBucket], hent, next);
997ed2dbd31SArchie Cobbs 		}
998ed2dbd31SArchie Cobbs 	}
999ed2dbd31SArchie Cobbs 
1000ed2dbd31SArchie Cobbs 	/* Replace old table with new one */
1001ed2dbd31SArchie Cobbs 	if (priv->conf.debugLevel >= 3) {
1002ed2dbd31SArchie Cobbs 		log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
1003ed2dbd31SArchie Cobbs 		    ng_bridge_nodename(priv->node),
1004ed2dbd31SArchie Cobbs 		    priv->numBuckets, newNumBuckets);
1005ed2dbd31SArchie Cobbs 	}
10061ede983cSDag-Erling Smørgrav 	free(priv->tab, M_NETGRAPH_BRIDGE);
1007ed2dbd31SArchie Cobbs 	priv->numBuckets = newNumBuckets;
1008ed2dbd31SArchie Cobbs 	priv->hashMask = newMask;
1009ed2dbd31SArchie Cobbs 	priv->tab = newTab;
1010ed2dbd31SArchie Cobbs 	return;
1011ed2dbd31SArchie Cobbs }
1012ed2dbd31SArchie Cobbs 
1013ed2dbd31SArchie Cobbs /******************************************************************
1014ed2dbd31SArchie Cobbs 		    MISC FUNCTIONS
1015ed2dbd31SArchie Cobbs ******************************************************************/
1016ed2dbd31SArchie Cobbs 
1017ed2dbd31SArchie Cobbs /*
1018ed2dbd31SArchie Cobbs  * Remove all hosts associated with a specific link from the hashtable.
1019ed2dbd31SArchie Cobbs  * If linkNum == -1, then remove all hosts in the table.
1020ed2dbd31SArchie Cobbs  */
1021ed2dbd31SArchie Cobbs static void
1022631cabbaSGleb Smirnoff ng_bridge_remove_hosts(priv_p priv, link_p link)
1023ed2dbd31SArchie Cobbs {
1024ed2dbd31SArchie Cobbs 	int bucket;
1025ed2dbd31SArchie Cobbs 
1026ed2dbd31SArchie Cobbs 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1027ed2dbd31SArchie Cobbs 		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1028ed2dbd31SArchie Cobbs 
1029ed2dbd31SArchie Cobbs 		while (*hptr != NULL) {
1030ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent = *hptr;
1031ed2dbd31SArchie Cobbs 
1032631cabbaSGleb Smirnoff 			if (link == NULL || hent->host.link == link) {
1033ed2dbd31SArchie Cobbs 				*hptr = SLIST_NEXT(hent, next);
10341ede983cSDag-Erling Smørgrav 				free(hent, M_NETGRAPH_BRIDGE);
1035ed2dbd31SArchie Cobbs 				priv->numHosts--;
1036ed2dbd31SArchie Cobbs 			} else
1037ed2dbd31SArchie Cobbs 				hptr = &SLIST_NEXT(hent, next);
1038ed2dbd31SArchie Cobbs 		}
1039ed2dbd31SArchie Cobbs 	}
1040ed2dbd31SArchie Cobbs }
1041ed2dbd31SArchie Cobbs 
1042ed2dbd31SArchie Cobbs /*
1043ed2dbd31SArchie Cobbs  * Handle our once-per-second timeout event. We do two things:
1044ed2dbd31SArchie Cobbs  * we decrement link->loopCount for those links being muted due to
1045ed2dbd31SArchie Cobbs  * a detected loopback condition, and we remove any hosts from
1046ed2dbd31SArchie Cobbs  * the hashtable whom we haven't heard from in a long while.
1047ed2dbd31SArchie Cobbs  */
1048631cabbaSGleb Smirnoff static int
10490b951c55SGleb Smirnoff ng_bridge_unmute(hook_p hook, void *arg)
1050631cabbaSGleb Smirnoff {
1051631cabbaSGleb Smirnoff 	link_p link = NG_HOOK_PRIVATE(hook);
1052631cabbaSGleb Smirnoff 	node_p node = NG_HOOK_NODE(hook);
1053631cabbaSGleb Smirnoff 	priv_p priv = NG_NODE_PRIVATE(node);
10540b951c55SGleb Smirnoff 	int *counter = arg;
1055631cabbaSGleb Smirnoff 
1056631cabbaSGleb Smirnoff 	if (link->loopCount != 0) {
1057631cabbaSGleb Smirnoff 		link->loopCount--;
1058631cabbaSGleb Smirnoff 		if (link->loopCount == 0 && priv->conf.debugLevel >= 2) {
1059631cabbaSGleb Smirnoff 			log(LOG_INFO, "ng_bridge: %s:"
1060631cabbaSGleb Smirnoff 			    " restoring looped back %s\n",
1061631cabbaSGleb Smirnoff 			    ng_bridge_nodename(node), NG_HOOK_NAME(hook));
1062631cabbaSGleb Smirnoff 		}
1063631cabbaSGleb Smirnoff 	}
1064abc4b11cSGleb Smirnoff 	(*counter)++;
1065631cabbaSGleb Smirnoff 	return (1);
1066631cabbaSGleb Smirnoff }
1067631cabbaSGleb Smirnoff 
1068ed2dbd31SArchie Cobbs static void
1069e0d32af7SGleb Smirnoff ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1070ed2dbd31SArchie Cobbs {
107130400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1072e0d32af7SGleb Smirnoff 	int bucket;
1073ed2dbd31SArchie Cobbs 	int counter = 0;
1074631cabbaSGleb Smirnoff 	hook_p ret;
1075ed2dbd31SArchie Cobbs 
1076ed2dbd31SArchie Cobbs 	/* Update host time counters and remove stale entries */
1077ed2dbd31SArchie Cobbs 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1078ed2dbd31SArchie Cobbs 		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1079ed2dbd31SArchie Cobbs 
1080ed2dbd31SArchie Cobbs 		while (*hptr != NULL) {
1081ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent = *hptr;
1082ed2dbd31SArchie Cobbs 
1083ed2dbd31SArchie Cobbs 			/* Remove hosts we haven't heard from in a while */
1084ed2dbd31SArchie Cobbs 			if (++hent->host.staleness >= priv->conf.maxStaleness) {
1085ed2dbd31SArchie Cobbs 				*hptr = SLIST_NEXT(hent, next);
10861ede983cSDag-Erling Smørgrav 				free(hent, M_NETGRAPH_BRIDGE);
1087ed2dbd31SArchie Cobbs 				priv->numHosts--;
1088ed2dbd31SArchie Cobbs 			} else {
1089ed2dbd31SArchie Cobbs 				if (hent->host.age < 0xffff)
1090ed2dbd31SArchie Cobbs 					hent->host.age++;
1091ed2dbd31SArchie Cobbs 				hptr = &SLIST_NEXT(hent, next);
1092ed2dbd31SArchie Cobbs 				counter++;
1093ed2dbd31SArchie Cobbs 			}
1094ed2dbd31SArchie Cobbs 		}
1095ed2dbd31SArchie Cobbs 	}
1096ed2dbd31SArchie Cobbs 	KASSERT(priv->numHosts == counter,
10976e551fb6SDavid E. O'Brien 	    ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
1098ed2dbd31SArchie Cobbs 
1099ed2dbd31SArchie Cobbs 	/* Decrease table size if necessary */
1100ed2dbd31SArchie Cobbs 	ng_bridge_rehash(priv);
1101ed2dbd31SArchie Cobbs 
1102ed2dbd31SArchie Cobbs 	/* Decrease loop counter on muted looped back links */
1103631cabbaSGleb Smirnoff 	counter = 0;
1104631cabbaSGleb Smirnoff 	NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter, ret);
1105ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == counter,
11066e551fb6SDavid E. O'Brien 	    ("%s: links: %d != %d", __func__, priv->numLinks, counter));
1107ed2dbd31SArchie Cobbs 
1108e0d32af7SGleb Smirnoff 	/* Register a new timeout, keeping the existing node reference */
1109e0d32af7SGleb Smirnoff 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
1110ed2dbd31SArchie Cobbs }
1111ed2dbd31SArchie Cobbs 
1112ed2dbd31SArchie Cobbs /*
1113ed2dbd31SArchie Cobbs  * Return node's "name", even if it doesn't have one.
1114ed2dbd31SArchie Cobbs  */
1115ed2dbd31SArchie Cobbs static const char *
1116ed2dbd31SArchie Cobbs ng_bridge_nodename(node_p node)
1117ed2dbd31SArchie Cobbs {
111887e2c66aSHartmut Brandt 	static char name[NG_NODESIZ];
1119ed2dbd31SArchie Cobbs 
1120dd10c1e2SGleb Smirnoff 	if (NG_NODE_HAS_NAME(node))
112130400f03SJulian Elischer 		snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
1122ed2dbd31SArchie Cobbs 	else
1123ed2dbd31SArchie Cobbs 		snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
1124ed2dbd31SArchie Cobbs 	return name;
1125ed2dbd31SArchie Cobbs }
1126