xref: /freebsd/sys/netgraph/ng_bridge.c (revision c869d905baa4e329dfd6793e7487b5985248ddb6)
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 */
94f961caf2SLutz Donnerhacke 	unsigned int			learnMac : 1,   /* autolearn macs */
95f961caf2SLutz 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 */
108*c869d905SLutz Donnerhacke 	unsigned int		persistent : 1,	/* can exist w/o hooks */
109*c869d905SLutz Donnerhacke 				sendUnknown : 1;/* links receive unknowns by default */
110ed2dbd31SArchie Cobbs 	struct callout		timer;		/* one second periodic timer */
111ed2dbd31SArchie Cobbs };
112ed2dbd31SArchie Cobbs typedef struct ng_bridge_private *priv_p;
113ed2dbd31SArchie Cobbs 
114ed2dbd31SArchie Cobbs /* Information about a host, stored in a hash table entry */
115ed2dbd31SArchie Cobbs struct ng_bridge_hent {
116ed2dbd31SArchie Cobbs 	struct ng_bridge_host		host;	/* actual host info */
117ed2dbd31SArchie Cobbs 	SLIST_ENTRY(ng_bridge_hent)	next;	/* next entry in bucket */
118ed2dbd31SArchie Cobbs };
119ed2dbd31SArchie Cobbs 
120ed2dbd31SArchie Cobbs /* Hash table bucket declaration */
121ed2dbd31SArchie Cobbs SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent);
122ed2dbd31SArchie Cobbs 
123ed2dbd31SArchie Cobbs /* Netgraph node methods */
124ed2dbd31SArchie Cobbs static ng_constructor_t	ng_bridge_constructor;
125ed2dbd31SArchie Cobbs static ng_rcvmsg_t	ng_bridge_rcvmsg;
126069154d5SJulian Elischer static ng_shutdown_t	ng_bridge_shutdown;
127ed2dbd31SArchie Cobbs static ng_newhook_t	ng_bridge_newhook;
128ed2dbd31SArchie Cobbs static ng_rcvdata_t	ng_bridge_rcvdata;
129ed2dbd31SArchie Cobbs static ng_disconnect_t	ng_bridge_disconnect;
130ed2dbd31SArchie Cobbs 
131ed2dbd31SArchie Cobbs /* Other internal functions */
132ed2dbd31SArchie Cobbs static struct	ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr);
133631cabbaSGleb Smirnoff static int	ng_bridge_put(priv_p priv, const u_char *addr, link_p link);
134ed2dbd31SArchie Cobbs static void	ng_bridge_rehash(priv_p priv);
135631cabbaSGleb Smirnoff static void	ng_bridge_remove_hosts(priv_p priv, link_p link);
136e0d32af7SGleb Smirnoff static void	ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2);
137ed2dbd31SArchie Cobbs static const	char *ng_bridge_nodename(node_p node);
138ed2dbd31SArchie Cobbs 
139ed2dbd31SArchie Cobbs /* Ethernet broadcast */
140ed2dbd31SArchie Cobbs static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
141ed2dbd31SArchie Cobbs     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
142ed2dbd31SArchie Cobbs 
143ed2dbd31SArchie Cobbs /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
144ed2dbd31SArchie Cobbs #define ETHER_EQUAL(a,b)	(((const u_int32_t *)(a))[0] \
145ed2dbd31SArchie Cobbs 					== ((const u_int32_t *)(b))[0] \
146ed2dbd31SArchie Cobbs 				    && ((const u_int16_t *)(a))[2] \
147ed2dbd31SArchie Cobbs 					== ((const u_int16_t *)(b))[2])
148ed2dbd31SArchie Cobbs 
149ed2dbd31SArchie Cobbs /* Minimum and maximum number of hash buckets. Must be a power of two. */
150ed2dbd31SArchie Cobbs #define MIN_BUCKETS		(1 << 5)	/* 32 */
151ed2dbd31SArchie Cobbs #define MAX_BUCKETS		(1 << 14)	/* 16384 */
152ed2dbd31SArchie Cobbs 
153ed2dbd31SArchie Cobbs /* Configuration default values */
154ed2dbd31SArchie Cobbs #define DEFAULT_LOOP_TIMEOUT	60
155ed2dbd31SArchie Cobbs #define DEFAULT_MAX_STALENESS	(15 * 60)	/* same as ARP timeout */
156ed2dbd31SArchie Cobbs #define DEFAULT_MIN_STABLE_AGE	1
157ed2dbd31SArchie Cobbs 
158ed2dbd31SArchie Cobbs /******************************************************************
159ed2dbd31SArchie Cobbs 		    NETGRAPH PARSE TYPES
160ed2dbd31SArchie Cobbs ******************************************************************/
161ed2dbd31SArchie Cobbs 
162ed2dbd31SArchie Cobbs /*
163ed2dbd31SArchie Cobbs  * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
164ed2dbd31SArchie Cobbs  */
165ed2dbd31SArchie Cobbs static int
166ed2dbd31SArchie Cobbs ng_bridge_getTableLength(const struct ng_parse_type *type,
167ed2dbd31SArchie Cobbs 	const u_char *start, const u_char *buf)
168ed2dbd31SArchie Cobbs {
169ed2dbd31SArchie Cobbs 	const struct ng_bridge_host_ary *const hary
170ed2dbd31SArchie Cobbs 	    = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
171ed2dbd31SArchie Cobbs 
172ed2dbd31SArchie Cobbs 	return hary->numHosts;
173ed2dbd31SArchie Cobbs }
174ed2dbd31SArchie Cobbs 
175ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_host_ary */
176f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_type_fields[]
1778c7e4101SRuslan Ermilov 	= NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
178ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_type = {
179ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
180f0184ff8SArchie Cobbs 	&ng_bridge_host_type_fields
181ed2dbd31SArchie Cobbs };
182ed2dbd31SArchie Cobbs static const struct ng_parse_array_info ng_bridge_hary_type_info = {
183ed2dbd31SArchie Cobbs 	&ng_bridge_host_type,
184ed2dbd31SArchie Cobbs 	ng_bridge_getTableLength
185ed2dbd31SArchie Cobbs };
186ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_hary_type = {
187ed2dbd31SArchie Cobbs 	&ng_parse_array_type,
188ed2dbd31SArchie Cobbs 	&ng_bridge_hary_type_info
189ed2dbd31SArchie Cobbs };
190f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[]
191ed2dbd31SArchie Cobbs 	= NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
192ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_ary_type = {
193ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
194f0184ff8SArchie Cobbs 	&ng_bridge_host_ary_type_fields
195ed2dbd31SArchie Cobbs };
196ed2dbd31SArchie Cobbs 
197ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_config */
198f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_config_type_fields[]
199631cabbaSGleb Smirnoff 	= NG_BRIDGE_CONFIG_TYPE_INFO;
200ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_config_type = {
201ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
202f0184ff8SArchie Cobbs 	&ng_bridge_config_type_fields
203ed2dbd31SArchie Cobbs };
204ed2dbd31SArchie Cobbs 
205ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_link_stat */
206f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_stats_type_fields[]
207f0184ff8SArchie Cobbs 	= NG_BRIDGE_STATS_TYPE_INFO;
208ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_stats_type = {
209ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
210f0184ff8SArchie Cobbs 	&ng_bridge_stats_type_fields
211ed2dbd31SArchie Cobbs };
212ed2dbd31SArchie Cobbs 
213ed2dbd31SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
214ed2dbd31SArchie Cobbs static const struct ng_cmdlist ng_bridge_cmdlist[] = {
215ed2dbd31SArchie Cobbs 	{
216ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
217ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_SET_CONFIG,
218ed2dbd31SArchie Cobbs 	  "setconfig",
219ed2dbd31SArchie Cobbs 	  &ng_bridge_config_type,
220ed2dbd31SArchie Cobbs 	  NULL
221ed2dbd31SArchie Cobbs 	},
222ed2dbd31SArchie Cobbs 	{
223ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
224ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_CONFIG,
225ed2dbd31SArchie Cobbs 	  "getconfig",
226ed2dbd31SArchie Cobbs 	  NULL,
227ed2dbd31SArchie Cobbs 	  &ng_bridge_config_type
228ed2dbd31SArchie Cobbs 	},
229ed2dbd31SArchie Cobbs 	{
230ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
231ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_RESET,
232ed2dbd31SArchie Cobbs 	  "reset",
233ed2dbd31SArchie Cobbs 	  NULL,
234ed2dbd31SArchie Cobbs 	  NULL
235ed2dbd31SArchie Cobbs 	},
236ed2dbd31SArchie Cobbs 	{
237ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
238ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_STATS,
239ed2dbd31SArchie Cobbs 	  "getstats",
240ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
241ed2dbd31SArchie Cobbs 	  &ng_bridge_stats_type
242ed2dbd31SArchie Cobbs 	},
243ed2dbd31SArchie Cobbs 	{
244ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
245ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_CLR_STATS,
246ed2dbd31SArchie Cobbs 	  "clrstats",
247ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
248ed2dbd31SArchie Cobbs 	  NULL
249ed2dbd31SArchie Cobbs 	},
250ed2dbd31SArchie Cobbs 	{
251ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
252ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GETCLR_STATS,
253ed2dbd31SArchie Cobbs 	  "getclrstats",
254ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
255ed2dbd31SArchie Cobbs 	  &ng_bridge_stats_type
256ed2dbd31SArchie Cobbs 	},
257ed2dbd31SArchie Cobbs 	{
258ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
259ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_TABLE,
260ed2dbd31SArchie Cobbs 	  "gettable",
261ed2dbd31SArchie Cobbs 	  NULL,
262ed2dbd31SArchie Cobbs 	  &ng_bridge_host_ary_type
263ed2dbd31SArchie Cobbs 	},
264f8aab721SMarko Zec 	{
265f8aab721SMarko Zec 	  NGM_BRIDGE_COOKIE,
266f8aab721SMarko Zec 	  NGM_BRIDGE_SET_PERSISTENT,
267f8aab721SMarko Zec 	  "setpersistent",
268f8aab721SMarko Zec 	  NULL,
269f8aab721SMarko Zec 	  NULL
270f8aab721SMarko Zec 	},
271ed2dbd31SArchie Cobbs 	{ 0 }
272ed2dbd31SArchie Cobbs };
273ed2dbd31SArchie Cobbs 
274ed2dbd31SArchie Cobbs /* Node type descriptor */
275ed2dbd31SArchie Cobbs static struct ng_type ng_bridge_typestruct = {
276f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
277f8aae777SJulian Elischer 	.name =		NG_BRIDGE_NODE_TYPE,
278f8aae777SJulian Elischer 	.constructor =	ng_bridge_constructor,
279f8aae777SJulian Elischer 	.rcvmsg =	ng_bridge_rcvmsg,
280f8aae777SJulian Elischer 	.shutdown =	ng_bridge_shutdown,
281f8aae777SJulian Elischer 	.newhook =	ng_bridge_newhook,
282f8aae777SJulian Elischer 	.rcvdata =	ng_bridge_rcvdata,
283f8aae777SJulian Elischer 	.disconnect =	ng_bridge_disconnect,
284f8aae777SJulian Elischer 	.cmdlist =	ng_bridge_cmdlist,
285ed2dbd31SArchie Cobbs };
286a89effcdSArchie Cobbs NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
287ed2dbd31SArchie Cobbs 
288ed2dbd31SArchie Cobbs /******************************************************************
289ed2dbd31SArchie Cobbs 		    NETGRAPH NODE METHODS
290ed2dbd31SArchie Cobbs ******************************************************************/
291ed2dbd31SArchie Cobbs 
292ed2dbd31SArchie Cobbs /*
293ed2dbd31SArchie Cobbs  * Node constructor
294ed2dbd31SArchie Cobbs  */
295ed2dbd31SArchie Cobbs static int
296069154d5SJulian Elischer ng_bridge_constructor(node_p node)
297ed2dbd31SArchie Cobbs {
298ed2dbd31SArchie Cobbs 	priv_p priv;
299ed2dbd31SArchie Cobbs 
300ed2dbd31SArchie Cobbs 	/* Allocate and initialize private info */
301674d86bfSGleb Smirnoff 	priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
302e0d32af7SGleb Smirnoff 	ng_callout_init(&priv->timer);
303ed2dbd31SArchie Cobbs 
304ed2dbd31SArchie Cobbs 	/* Allocate and initialize hash table, etc. */
305e11e3f18SDag-Erling Smørgrav 	priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab),
306674d86bfSGleb Smirnoff 	    M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
307ed2dbd31SArchie Cobbs 	priv->numBuckets = MIN_BUCKETS;
308ed2dbd31SArchie Cobbs 	priv->hashMask = MIN_BUCKETS - 1;
309ed2dbd31SArchie Cobbs 	priv->conf.debugLevel = 1;
310ed2dbd31SArchie Cobbs 	priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
311ed2dbd31SArchie Cobbs 	priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
312ed2dbd31SArchie Cobbs 	priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
313*c869d905SLutz Donnerhacke 	priv->sendUnknown = 1;	       /* classic bridge */
314ed2dbd31SArchie Cobbs 
315069154d5SJulian Elischer 	/*
316069154d5SJulian Elischer 	 * This node has all kinds of stuff that could be screwed by SMP.
317069154d5SJulian Elischer 	 * Until it gets it's own internal protection, we go through in
318053359b7SPedro F. Giffuni 	 * single file. This could hurt a machine bridging between two
319069154d5SJulian Elischer 	 * GB ethernets so it should be fixed.
320069154d5SJulian Elischer 	 * When it's fixed the process SHOULD NOT SLEEP, spinlocks please!
321069154d5SJulian Elischer 	 * (and atomic ops )
322069154d5SJulian Elischer 	 */
32330400f03SJulian Elischer 	NG_NODE_FORCE_WRITER(node);
32430400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
325069154d5SJulian Elischer 	priv->node = node;
326ed2dbd31SArchie Cobbs 
3276c12c2b1SArchie Cobbs 	/* Start timer; timer is always running while node is alive */
328e0d32af7SGleb Smirnoff 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
3296c12c2b1SArchie Cobbs 
3306c12c2b1SArchie Cobbs 	/* Done */
331ed2dbd31SArchie Cobbs 	return (0);
332ed2dbd31SArchie Cobbs }
333ed2dbd31SArchie Cobbs 
334ed2dbd31SArchie Cobbs /*
335ed2dbd31SArchie Cobbs  * Method for attaching a new hook
336ed2dbd31SArchie Cobbs  */
337ed2dbd31SArchie Cobbs static	int
338ed2dbd31SArchie Cobbs ng_bridge_newhook(node_p node, hook_p hook, const char *name)
339ed2dbd31SArchie Cobbs {
34030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
341631cabbaSGleb Smirnoff 	char linkName[NG_HOOKSIZ];
342631cabbaSGleb Smirnoff 	u_int32_t linkNum;
343631cabbaSGleb Smirnoff 	link_p link;
344f961caf2SLutz Donnerhacke 	const char *prefix = NG_BRIDGE_HOOK_LINK_PREFIX;
345f961caf2SLutz Donnerhacke 	bool isUplink;
346f961caf2SLutz Donnerhacke 
347f961caf2SLutz Donnerhacke 	/* Check for a link hook */
348f961caf2SLutz Donnerhacke 	if (strlen(name) <= strlen(prefix))
349f961caf2SLutz Donnerhacke 		return (EINVAL);       /* Unknown hook name */
350f961caf2SLutz Donnerhacke 
351f961caf2SLutz Donnerhacke 	isUplink = (name[0] == 'u');
352f961caf2SLutz Donnerhacke 	if (isUplink)
353f961caf2SLutz Donnerhacke 		prefix = NG_BRIDGE_HOOK_UPLINK_PREFIX;
354ed2dbd31SArchie Cobbs 
355631cabbaSGleb Smirnoff 	/* primitive parsing */
356f961caf2SLutz Donnerhacke 	linkNum = strtoul(name + strlen(prefix), NULL, 10);
357631cabbaSGleb Smirnoff 	/* validation by comparing against the reconstucted name  */
358f961caf2SLutz Donnerhacke 	snprintf(linkName, sizeof(linkName), "%s%u", prefix, linkNum);
359631cabbaSGleb Smirnoff 	if (strcmp(linkName, name) != 0)
360ed2dbd31SArchie Cobbs 		return (EINVAL);
361631cabbaSGleb Smirnoff 
362f961caf2SLutz Donnerhacke 	if (linkNum == 0 && isUplink)
363f961caf2SLutz Donnerhacke 		return (EINVAL);
364f961caf2SLutz Donnerhacke 
365631cabbaSGleb Smirnoff 	if(NG_PEER_NODE(hook) == node)
366631cabbaSGleb Smirnoff 	        return (ELOOP);
367631cabbaSGleb Smirnoff 
368f961caf2SLutz Donnerhacke 	link = malloc(sizeof(*link), M_NETGRAPH_BRIDGE, M_ZERO);
369631cabbaSGleb Smirnoff 	if (link == NULL)
370ed2dbd31SArchie Cobbs 		return (ENOMEM);
371f961caf2SLutz Donnerhacke 
372631cabbaSGleb Smirnoff 	link->hook = hook;
373f961caf2SLutz Donnerhacke 	if (isUplink) {
374f961caf2SLutz Donnerhacke 		link->learnMac = 0;
375f961caf2SLutz Donnerhacke 		link->sendUnknown = 1;
376*c869d905SLutz Donnerhacke 		if (priv->numLinks == 0)	/* if the first link is an uplink */
377*c869d905SLutz Donnerhacke 		    priv->sendUnknown = 0;	/* switch to restrictive mode */
378f961caf2SLutz Donnerhacke 	} else {
379f961caf2SLutz Donnerhacke 		link->learnMac = 1;
380*c869d905SLutz Donnerhacke 		link->sendUnknown = priv->sendUnknown;
381f961caf2SLutz Donnerhacke 	}
382f961caf2SLutz Donnerhacke 
383631cabbaSGleb Smirnoff 	NG_HOOK_SET_PRIVATE(hook, link);
384ed2dbd31SArchie Cobbs 	priv->numLinks++;
385ed2dbd31SArchie Cobbs 	return (0);
386ed2dbd31SArchie Cobbs }
387ed2dbd31SArchie Cobbs 
388ed2dbd31SArchie Cobbs /*
389ed2dbd31SArchie Cobbs  * Receive a control message
390ed2dbd31SArchie Cobbs  */
391ed2dbd31SArchie Cobbs static int
3920b951c55SGleb Smirnoff ng_bridge_reset_link(hook_p hook, void *arg __unused)
393631cabbaSGleb Smirnoff {
394631cabbaSGleb Smirnoff 	link_p priv = NG_HOOK_PRIVATE(hook);
395631cabbaSGleb Smirnoff 
396631cabbaSGleb Smirnoff 	priv->loopCount = 0;
397631cabbaSGleb Smirnoff 	bzero(&priv->stats, sizeof(priv->stats));
3980b951c55SGleb Smirnoff 
3990b951c55SGleb Smirnoff 	return (1);
400631cabbaSGleb Smirnoff }
401631cabbaSGleb Smirnoff 
402631cabbaSGleb Smirnoff static int
403069154d5SJulian Elischer ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
404ed2dbd31SArchie Cobbs {
40530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
406ed2dbd31SArchie Cobbs 	struct ng_mesg *resp = NULL;
407ed2dbd31SArchie Cobbs 	int error = 0;
408069154d5SJulian Elischer 	struct ng_mesg *msg;
409ed2dbd31SArchie Cobbs 
410069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
411ed2dbd31SArchie Cobbs 	switch (msg->header.typecookie) {
412aeaef7d5SBjoern A. Zeeb #ifdef NGM_BRIDGE_TABLE_ABI
413aeaef7d5SBjoern A. Zeeb 	case NGM_BRIDGE_COOKIE_TBL:
414aeaef7d5SBjoern A. Zeeb 		switch (msg->header.cmd) {
415aeaef7d5SBjoern A. Zeeb 		case NGM_BRIDGE_GET_CONFIG:
416aeaef7d5SBjoern A. Zeeb 		    {
417aeaef7d5SBjoern A. Zeeb 			struct ng_bridge_config_tbl *conf;
418aeaef7d5SBjoern A. Zeeb 
419aeaef7d5SBjoern A. Zeeb 			NG_MKRESPONSE(resp, msg, sizeof(*conf),
420aeaef7d5SBjoern A. Zeeb 			    M_NOWAIT|M_ZERO);
421aeaef7d5SBjoern A. Zeeb 			if (resp == NULL) {
422aeaef7d5SBjoern A. Zeeb 				error = ENOMEM;
423aeaef7d5SBjoern A. Zeeb 				break;
424aeaef7d5SBjoern A. Zeeb 			}
425aeaef7d5SBjoern A. Zeeb 			conf = (struct ng_bridge_config_tbl *)resp->data;
426aeaef7d5SBjoern A. Zeeb 			conf->cfg = priv->conf;
427aeaef7d5SBjoern A. Zeeb 			break;
428aeaef7d5SBjoern A. Zeeb 		    }
429aeaef7d5SBjoern A. Zeeb 		case NGM_BRIDGE_SET_CONFIG:
430aeaef7d5SBjoern A. Zeeb 		    {
431aeaef7d5SBjoern A. Zeeb 			struct ng_bridge_config_tbl *conf;
432aeaef7d5SBjoern A. Zeeb 
433aeaef7d5SBjoern A. Zeeb 			if (msg->header.arglen != sizeof(*conf)) {
434aeaef7d5SBjoern A. Zeeb 				error = EINVAL;
435aeaef7d5SBjoern A. Zeeb 				break;
436aeaef7d5SBjoern A. Zeeb 			}
437aeaef7d5SBjoern A. Zeeb 			conf = (struct ng_bridge_config_tbl *)msg->data;
438aeaef7d5SBjoern A. Zeeb 			priv->conf = conf->cfg;
439aeaef7d5SBjoern A. Zeeb 			break;
440aeaef7d5SBjoern A. Zeeb 		    }
441aeaef7d5SBjoern A. Zeeb 		case NGM_BRIDGE_GET_TABLE:
442aeaef7d5SBjoern A. Zeeb 		    {
443aeaef7d5SBjoern A. Zeeb 			struct ng_bridge_host_tbl_ary *ary;
444aeaef7d5SBjoern A. Zeeb 			struct ng_bridge_hent *hent;
445aeaef7d5SBjoern A. Zeeb 			int i, bucket;
446aeaef7d5SBjoern A. Zeeb 
447aeaef7d5SBjoern A. Zeeb 			NG_MKRESPONSE(resp, msg, sizeof(*ary) +
448aeaef7d5SBjoern A. Zeeb 			    (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
449aeaef7d5SBjoern A. Zeeb 			if (resp == NULL) {
450aeaef7d5SBjoern A. Zeeb 				error = ENOMEM;
451aeaef7d5SBjoern A. Zeeb 				break;
452aeaef7d5SBjoern A. Zeeb 			}
453aeaef7d5SBjoern A. Zeeb 			ary = (struct ng_bridge_host_tbl_ary *)resp->data;
454aeaef7d5SBjoern A. Zeeb 			ary->numHosts = priv->numHosts;
455aeaef7d5SBjoern A. Zeeb 			i = 0;
456aeaef7d5SBjoern A. Zeeb 			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
457aeaef7d5SBjoern A. Zeeb 				SLIST_FOREACH(hent, &priv->tab[bucket], next) {
458f961caf2SLutz Donnerhacke 					const char *name = NG_HOOK_NAME(hent->host.link->hook);
459f961caf2SLutz Donnerhacke 					const char *prefix = name[0] == 'u' ?
460f961caf2SLutz Donnerhacke 					    NG_BRIDGE_HOOK_UPLINK_PREFIX :
461f961caf2SLutz Donnerhacke 					    NG_BRIDGE_HOOK_LINK_PREFIX;
462f961caf2SLutz Donnerhacke 
463aeaef7d5SBjoern A. Zeeb 					memcpy(ary->hosts[i].addr,
464aeaef7d5SBjoern A. Zeeb 					    hent->host.addr,
465aeaef7d5SBjoern A. Zeeb 					    sizeof(ary->hosts[i].addr));
466aeaef7d5SBjoern A. Zeeb 					ary->hosts[i].age = hent->host.age;
467aeaef7d5SBjoern A. Zeeb 					ary->hosts[i].staleness =
468aeaef7d5SBjoern A. Zeeb 					     hent->host.staleness;
469aeaef7d5SBjoern A. Zeeb 				        ary->hosts[i].linkNum = strtol(
470f961caf2SLutz Donnerhacke 					    name + strlen(prefix), NULL, 10);
471aeaef7d5SBjoern A. Zeeb 					i++;
472aeaef7d5SBjoern A. Zeeb 				}
473aeaef7d5SBjoern A. Zeeb 			}
474aeaef7d5SBjoern A. Zeeb 			break;
475aeaef7d5SBjoern A. Zeeb 		    }
476aeaef7d5SBjoern A. Zeeb 		}
477aeaef7d5SBjoern A. Zeeb 		/* If already handled break, otherwise use new ABI. */
478aeaef7d5SBjoern A. Zeeb 		if (resp != NULL || error != 0)
479aeaef7d5SBjoern A. Zeeb 		    break;
480aeaef7d5SBjoern A. Zeeb #endif /* NGM_BRIDGE_TABLE_ABI */
481ed2dbd31SArchie Cobbs 	case NGM_BRIDGE_COOKIE:
482ed2dbd31SArchie Cobbs 		switch (msg->header.cmd) {
483ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_CONFIG:
484ed2dbd31SArchie Cobbs 		    {
485ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
486ed2dbd31SArchie Cobbs 
487ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg,
488ed2dbd31SArchie Cobbs 			    sizeof(struct ng_bridge_config), M_NOWAIT);
489ed2dbd31SArchie Cobbs 			if (resp == NULL) {
490ed2dbd31SArchie Cobbs 				error = ENOMEM;
491ed2dbd31SArchie Cobbs 				break;
492ed2dbd31SArchie Cobbs 			}
493ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)resp->data;
494ed2dbd31SArchie Cobbs 			*conf = priv->conf;	/* no sanity checking needed */
495ed2dbd31SArchie Cobbs 			break;
496ed2dbd31SArchie Cobbs 		    }
497ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_SET_CONFIG:
498ed2dbd31SArchie Cobbs 		    {
499ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
500ed2dbd31SArchie Cobbs 
501ed2dbd31SArchie Cobbs 			if (msg->header.arglen
502ed2dbd31SArchie Cobbs 			    != sizeof(struct ng_bridge_config)) {
503ed2dbd31SArchie Cobbs 				error = EINVAL;
504ed2dbd31SArchie Cobbs 				break;
505ed2dbd31SArchie Cobbs 			}
506ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)msg->data;
507ed2dbd31SArchie Cobbs 			priv->conf = *conf;
508ed2dbd31SArchie Cobbs 			break;
509ed2dbd31SArchie Cobbs 		    }
510ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_RESET:
511ed2dbd31SArchie Cobbs 		    {
512631cabbaSGleb Smirnoff 			hook_p rethook;
513ed2dbd31SArchie Cobbs 
514ed2dbd31SArchie Cobbs 			/* Flush all entries in the hash table */
515631cabbaSGleb Smirnoff 			ng_bridge_remove_hosts(priv, NULL);
516ed2dbd31SArchie Cobbs 
517ed2dbd31SArchie Cobbs 			/* Reset all loop detection counters and stats */
5180b951c55SGleb Smirnoff 			NG_NODE_FOREACH_HOOK(node, ng_bridge_reset_link, NULL,
5190b951c55SGleb Smirnoff 			    rethook);
520ed2dbd31SArchie Cobbs 			break;
521ed2dbd31SArchie Cobbs 		    }
522ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_STATS:
523ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_CLR_STATS:
524ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GETCLR_STATS:
525ed2dbd31SArchie Cobbs 		    {
526631cabbaSGleb Smirnoff 			hook_p hook;
527631cabbaSGleb Smirnoff 			link_p link;
528631cabbaSGleb Smirnoff 			char linkName[NG_HOOKSIZ];
529f961caf2SLutz Donnerhacke 			int linkNum;
530ed2dbd31SArchie Cobbs 
531ed2dbd31SArchie Cobbs 			/* Get link number */
532ed2dbd31SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
533ed2dbd31SArchie Cobbs 				error = EINVAL;
534ed2dbd31SArchie Cobbs 				break;
535ed2dbd31SArchie Cobbs 			}
536f961caf2SLutz Donnerhacke 			linkNum = *((int32_t *)msg->data);
537f961caf2SLutz Donnerhacke 			if (linkNum < 0)
538631cabbaSGleb Smirnoff 				snprintf(linkName, sizeof(linkName),
539f961caf2SLutz Donnerhacke 				    "%s%u", NG_BRIDGE_HOOK_UPLINK_PREFIX, -linkNum);
540f961caf2SLutz Donnerhacke 			else
541f961caf2SLutz Donnerhacke 				snprintf(linkName, sizeof(linkName),
542f961caf2SLutz Donnerhacke 				    "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX, linkNum);
543631cabbaSGleb Smirnoff 
544631cabbaSGleb Smirnoff 			if ((hook = ng_findhook(node, linkName)) == NULL) {
545ed2dbd31SArchie Cobbs 				error = ENOTCONN;
546ed2dbd31SArchie Cobbs 				break;
547ed2dbd31SArchie Cobbs 			}
548631cabbaSGleb Smirnoff 			link = NG_HOOK_PRIVATE(hook);
549ed2dbd31SArchie Cobbs 
550ed2dbd31SArchie Cobbs 			/* Get/clear stats */
551ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
552ed2dbd31SArchie Cobbs 				NG_MKRESPONSE(resp, msg,
553ed2dbd31SArchie Cobbs 				    sizeof(link->stats), M_NOWAIT);
554ed2dbd31SArchie Cobbs 				if (resp == NULL) {
555ed2dbd31SArchie Cobbs 					error = ENOMEM;
556ed2dbd31SArchie Cobbs 					break;
557ed2dbd31SArchie Cobbs 				}
558ed2dbd31SArchie Cobbs 				bcopy(&link->stats,
559ed2dbd31SArchie Cobbs 				    resp->data, sizeof(link->stats));
560ed2dbd31SArchie Cobbs 			}
561ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
562ed2dbd31SArchie Cobbs 				bzero(&link->stats, sizeof(link->stats));
563ed2dbd31SArchie Cobbs 			break;
564ed2dbd31SArchie Cobbs 		    }
565ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_TABLE:
566ed2dbd31SArchie Cobbs 		    {
567ed2dbd31SArchie Cobbs 			struct ng_bridge_host_ary *ary;
568ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *hent;
569ed2dbd31SArchie Cobbs 			int i = 0, bucket;
570ed2dbd31SArchie Cobbs 
571ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*ary)
572ed2dbd31SArchie Cobbs 			    + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
573ed2dbd31SArchie Cobbs 			if (resp == NULL) {
574ed2dbd31SArchie Cobbs 				error = ENOMEM;
575ed2dbd31SArchie Cobbs 				break;
576ed2dbd31SArchie Cobbs 			}
577ed2dbd31SArchie Cobbs 			ary = (struct ng_bridge_host_ary *)resp->data;
578ed2dbd31SArchie Cobbs 			ary->numHosts = priv->numHosts;
579ed2dbd31SArchie Cobbs 			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
580631cabbaSGleb Smirnoff 				SLIST_FOREACH(hent, &priv->tab[bucket], next) {
581631cabbaSGleb Smirnoff 					memcpy(ary->hosts[i].addr,
582631cabbaSGleb Smirnoff 					       hent->host.addr,
583631cabbaSGleb Smirnoff 					       sizeof(ary->hosts[i].addr));
584631cabbaSGleb Smirnoff 					ary->hosts[i].age       = hent->host.age;
585631cabbaSGleb Smirnoff 					ary->hosts[i].staleness = hent->host.staleness;
586631cabbaSGleb Smirnoff 					strncpy(ary->hosts[i].hook,
587631cabbaSGleb Smirnoff 						NG_HOOK_NAME(hent->host.link->hook),
588631cabbaSGleb Smirnoff 						sizeof(ary->hosts[i].hook));
589631cabbaSGleb Smirnoff 					i++;
590631cabbaSGleb Smirnoff 				}
591ed2dbd31SArchie Cobbs 			}
592ed2dbd31SArchie Cobbs 			break;
593ed2dbd31SArchie Cobbs 		    }
594f8aab721SMarko Zec 		case NGM_BRIDGE_SET_PERSISTENT:
595f8aab721SMarko Zec 		    {
596f8aab721SMarko Zec 			priv->persistent = 1;
597f8aab721SMarko Zec 			break;
598f8aab721SMarko Zec 		    }
599ed2dbd31SArchie Cobbs 		default:
600ed2dbd31SArchie Cobbs 			error = EINVAL;
601ed2dbd31SArchie Cobbs 			break;
602ed2dbd31SArchie Cobbs 		}
603ed2dbd31SArchie Cobbs 		break;
604ed2dbd31SArchie Cobbs 	default:
605ed2dbd31SArchie Cobbs 		error = EINVAL;
606ed2dbd31SArchie Cobbs 		break;
607ed2dbd31SArchie Cobbs 	}
608ed2dbd31SArchie Cobbs 
609ed2dbd31SArchie Cobbs 	/* Done */
610069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
611069154d5SJulian Elischer 	NG_FREE_MSG(msg);
612ed2dbd31SArchie Cobbs 	return (error);
613ed2dbd31SArchie Cobbs }
614ed2dbd31SArchie Cobbs 
615ed2dbd31SArchie Cobbs /*
616ed2dbd31SArchie Cobbs  * Receive data on a hook
617ed2dbd31SArchie Cobbs  */
618631cabbaSGleb Smirnoff struct ng_bridge_send_ctx {
619631cabbaSGleb Smirnoff 	link_p foundFirst, incoming;
620631cabbaSGleb Smirnoff 	struct mbuf * m;
621631cabbaSGleb Smirnoff 	int manycast, error;
622631cabbaSGleb Smirnoff };
623631cabbaSGleb Smirnoff 
624631cabbaSGleb Smirnoff static int
6250b951c55SGleb Smirnoff ng_bridge_send_ctx(hook_p dst, void *arg)
626631cabbaSGleb Smirnoff {
6270b951c55SGleb Smirnoff 	struct ng_bridge_send_ctx *ctx = arg;
628631cabbaSGleb Smirnoff 	link_p destLink = NG_HOOK_PRIVATE(dst);
629631cabbaSGleb Smirnoff 	struct mbuf *m2 = NULL;
630631cabbaSGleb Smirnoff 	int error = 0;
631631cabbaSGleb Smirnoff 
632631cabbaSGleb Smirnoff 	/* Skip incoming link */
633631cabbaSGleb Smirnoff 	if (destLink == ctx->incoming) {
634631cabbaSGleb Smirnoff 		return (1);
635631cabbaSGleb Smirnoff 	}
636631cabbaSGleb Smirnoff 
637f961caf2SLutz Donnerhacke 	/* Skip sending unknowns to undesired links  */
638f961caf2SLutz Donnerhacke 	if (!ctx->manycast && !destLink->sendUnknown)
639f961caf2SLutz Donnerhacke 		return (1);
640f961caf2SLutz Donnerhacke 
641631cabbaSGleb Smirnoff 	if (ctx->foundFirst == NULL) {
642631cabbaSGleb Smirnoff 		/*
643631cabbaSGleb Smirnoff 		 * This is the first usable link we have found.
644631cabbaSGleb Smirnoff 		 * Reserve it for the originals.
645631cabbaSGleb Smirnoff 		 * If we never find another we save a copy.
646631cabbaSGleb Smirnoff 		 */
647631cabbaSGleb Smirnoff 		ctx->foundFirst = destLink;
648631cabbaSGleb Smirnoff 		return (1);
649631cabbaSGleb Smirnoff 	}
650631cabbaSGleb Smirnoff 
651631cabbaSGleb Smirnoff 	/*
652631cabbaSGleb Smirnoff 	 * It's usable link but not the reserved (first) one.
653631cabbaSGleb Smirnoff 	 * Copy mbuf info for sending.
654631cabbaSGleb Smirnoff 	 */
655631cabbaSGleb Smirnoff 	m2 = m_dup(ctx->m, M_NOWAIT);	/* XXX m_copypacket() */
656631cabbaSGleb Smirnoff 	if (m2 == NULL) {
657631cabbaSGleb Smirnoff 		ctx->incoming->stats.memoryFailures++;
658631cabbaSGleb Smirnoff 		ctx->error = ENOBUFS;
659631cabbaSGleb Smirnoff 		return (0);	       /* abort loop */
660631cabbaSGleb Smirnoff 	}
661631cabbaSGleb Smirnoff 
662631cabbaSGleb Smirnoff 	/* Update stats */
663631cabbaSGleb Smirnoff 	destLink->stats.xmitPackets++;
664631cabbaSGleb Smirnoff 	destLink->stats.xmitOctets += m2->m_pkthdr.len;
665631cabbaSGleb Smirnoff 	switch (ctx->manycast) {
666631cabbaSGleb Smirnoff 	 default:					/* unknown unicast */
667631cabbaSGleb Smirnoff 		break;
668631cabbaSGleb Smirnoff 	 case 1:					/* multicast */
669631cabbaSGleb Smirnoff 		destLink->stats.xmitMulticasts++;
670631cabbaSGleb Smirnoff 		break;
671631cabbaSGleb Smirnoff 	 case 2:					/* broadcast */
672631cabbaSGleb Smirnoff 		destLink->stats.xmitBroadcasts++;
673631cabbaSGleb Smirnoff 		break;
674631cabbaSGleb Smirnoff 	}
675631cabbaSGleb Smirnoff 
676631cabbaSGleb Smirnoff 	/* Send packet */
677631cabbaSGleb Smirnoff 	NG_SEND_DATA_ONLY(error, destLink->hook, m2);
678631cabbaSGleb Smirnoff 	if(error)
679631cabbaSGleb Smirnoff 	  ctx->error = error;
680631cabbaSGleb Smirnoff 	return (1);
681631cabbaSGleb Smirnoff }
682631cabbaSGleb Smirnoff 
683ed2dbd31SArchie Cobbs static int
684069154d5SJulian Elischer ng_bridge_rcvdata(hook_p hook, item_p item)
685ed2dbd31SArchie Cobbs {
68630400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
68730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
688ed2dbd31SArchie Cobbs 	struct ng_bridge_host *host;
689ed2dbd31SArchie Cobbs 	struct ether_header *eh;
690631cabbaSGleb Smirnoff 	struct ng_bridge_send_ctx ctx = { 0 };
691631cabbaSGleb Smirnoff 	hook_p ret;
692ed2dbd31SArchie Cobbs 
693631cabbaSGleb Smirnoff 	NGI_GET_M(item, ctx.m);
694ed2dbd31SArchie Cobbs 
695631cabbaSGleb Smirnoff 	ctx.incoming = NG_HOOK_PRIVATE(hook);
696ed2dbd31SArchie Cobbs 	/* Sanity check packet and pull up header */
697631cabbaSGleb Smirnoff 	if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) {
698631cabbaSGleb Smirnoff 		ctx.incoming->stats.recvRunts++;
699069154d5SJulian Elischer 		NG_FREE_ITEM(item);
700631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
701ed2dbd31SArchie Cobbs 		return (EINVAL);
702ed2dbd31SArchie Cobbs 	}
703631cabbaSGleb Smirnoff 	if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) {
704631cabbaSGleb Smirnoff 		ctx.incoming->stats.memoryFailures++;
705069154d5SJulian Elischer 		NG_FREE_ITEM(item);
706ed2dbd31SArchie Cobbs 		return (ENOBUFS);
707ed2dbd31SArchie Cobbs 	}
708631cabbaSGleb Smirnoff 	eh = mtod(ctx.m, struct ether_header *);
709ed2dbd31SArchie Cobbs 	if ((eh->ether_shost[0] & 1) != 0) {
710631cabbaSGleb Smirnoff 		ctx.incoming->stats.recvInvalid++;
711069154d5SJulian Elischer 		NG_FREE_ITEM(item);
712631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
713ed2dbd31SArchie Cobbs 		return (EINVAL);
714ed2dbd31SArchie Cobbs 	}
715ed2dbd31SArchie Cobbs 
716ed2dbd31SArchie Cobbs 	/* Is link disabled due to a loopback condition? */
717631cabbaSGleb Smirnoff 	if (ctx.incoming->loopCount != 0) {
718631cabbaSGleb Smirnoff 		ctx.incoming->stats.loopDrops++;
719069154d5SJulian Elischer 		NG_FREE_ITEM(item);
720631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
721ed2dbd31SArchie Cobbs 		return (ELOOP);		/* XXX is this an appropriate error? */
722ed2dbd31SArchie Cobbs 	}
723ed2dbd31SArchie Cobbs 
724ed2dbd31SArchie Cobbs 	/* Update stats */
725631cabbaSGleb Smirnoff 	ctx.incoming->stats.recvPackets++;
726631cabbaSGleb Smirnoff 	ctx.incoming->stats.recvOctets += ctx.m->m_pkthdr.len;
727631cabbaSGleb Smirnoff 	if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) {
728ed2dbd31SArchie Cobbs 		if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
729631cabbaSGleb Smirnoff 			ctx.incoming->stats.recvBroadcasts++;
730631cabbaSGleb Smirnoff 			ctx.manycast = 2;
731ed2dbd31SArchie Cobbs 		} else
732631cabbaSGleb Smirnoff 			ctx.incoming->stats.recvMulticasts++;
733ed2dbd31SArchie Cobbs 	}
734ed2dbd31SArchie Cobbs 
735ed2dbd31SArchie Cobbs 	/* Look up packet's source Ethernet address in hashtable */
736ed2dbd31SArchie Cobbs 	if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
737ed2dbd31SArchie Cobbs 		/* Update time since last heard from this host */
738ed2dbd31SArchie Cobbs 		host->staleness = 0;
739ed2dbd31SArchie Cobbs 
740ed2dbd31SArchie Cobbs 		/* Did host jump to a different link? */
741631cabbaSGleb Smirnoff 		if (host->link != ctx.incoming) {
742ed2dbd31SArchie Cobbs 			/*
743ed2dbd31SArchie Cobbs 			 * If the host's old link was recently established
744ed2dbd31SArchie Cobbs 			 * on the old link and it's already jumped to a new
745ed2dbd31SArchie Cobbs 			 * link, declare a loopback condition.
746ed2dbd31SArchie Cobbs 			 */
747ed2dbd31SArchie Cobbs 			if (host->age < priv->conf.minStableAge) {
748ed2dbd31SArchie Cobbs 				/* Log the problem */
749ed2dbd31SArchie Cobbs 				if (priv->conf.debugLevel >= 2) {
750631cabbaSGleb Smirnoff 					struct ifnet *ifp = ctx.m->m_pkthdr.rcvif;
751ed2dbd31SArchie Cobbs 					char suffix[32];
752ed2dbd31SArchie Cobbs 
753ed2dbd31SArchie Cobbs 					if (ifp != NULL)
754ed2dbd31SArchie Cobbs 						snprintf(suffix, sizeof(suffix),
7559bf40edeSBrooks Davis 						    " (%s)", ifp->if_xname);
756ed2dbd31SArchie Cobbs 					else
757ed2dbd31SArchie Cobbs 						*suffix = '\0';
758ed2dbd31SArchie Cobbs 					log(LOG_WARNING, "ng_bridge: %s:"
759ed2dbd31SArchie Cobbs 					    " loopback detected on %s%s\n",
760ed2dbd31SArchie Cobbs 					    ng_bridge_nodename(node),
76130400f03SJulian Elischer 					    NG_HOOK_NAME(hook), suffix);
762ed2dbd31SArchie Cobbs 				}
763ed2dbd31SArchie Cobbs 
764ed2dbd31SArchie Cobbs 				/* Mark link as linka non grata */
765631cabbaSGleb Smirnoff 				ctx.incoming->loopCount = priv->conf.loopTimeout;
766631cabbaSGleb Smirnoff 				ctx.incoming->stats.loopDetects++;
767ed2dbd31SArchie Cobbs 
768ed2dbd31SArchie Cobbs 				/* Forget all hosts on this link */
769631cabbaSGleb Smirnoff 				ng_bridge_remove_hosts(priv, ctx.incoming);
770ed2dbd31SArchie Cobbs 
771ed2dbd31SArchie Cobbs 				/* Drop packet */
772631cabbaSGleb Smirnoff 				ctx.incoming->stats.loopDrops++;
773069154d5SJulian Elischer 				NG_FREE_ITEM(item);
774631cabbaSGleb Smirnoff 				NG_FREE_M(ctx.m);
775ed2dbd31SArchie Cobbs 				return (ELOOP);		/* XXX appropriate? */
776ed2dbd31SArchie Cobbs 			}
777ed2dbd31SArchie Cobbs 
778ed2dbd31SArchie Cobbs 			/* Move host over to new link */
779631cabbaSGleb Smirnoff 			host->link = ctx.incoming;
780ed2dbd31SArchie Cobbs 			host->age = 0;
781ed2dbd31SArchie Cobbs 		}
782f961caf2SLutz Donnerhacke 	} else if (ctx.incoming->learnMac) {
783631cabbaSGleb Smirnoff 		if (!ng_bridge_put(priv, eh->ether_shost, ctx.incoming)) {
784631cabbaSGleb Smirnoff 			ctx.incoming->stats.memoryFailures++;
785069154d5SJulian Elischer 			NG_FREE_ITEM(item);
786631cabbaSGleb Smirnoff 			NG_FREE_M(ctx.m);
787ed2dbd31SArchie Cobbs 			return (ENOMEM);
788ed2dbd31SArchie Cobbs 		}
789ed2dbd31SArchie Cobbs 	}
790ed2dbd31SArchie Cobbs 
791ed2dbd31SArchie Cobbs 	/* Run packet through ipfw processing, if enabled */
7929b932e9eSAndre Oppermann #if 0
7930b4b0b0fSJulian Elischer 	if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) {
794ed2dbd31SArchie Cobbs 		/* XXX not implemented yet */
795ed2dbd31SArchie Cobbs 	}
7969b932e9eSAndre Oppermann #endif
797ed2dbd31SArchie Cobbs 
798ed2dbd31SArchie Cobbs 	/*
799ed2dbd31SArchie Cobbs 	 * If unicast and destination host known, deliver to host's link,
800ed2dbd31SArchie Cobbs 	 * unless it is the same link as the packet came in on.
801ed2dbd31SArchie Cobbs 	 */
802631cabbaSGleb Smirnoff 	if (!ctx.manycast) {
803ed2dbd31SArchie Cobbs 		/* Determine packet destination link */
804ed2dbd31SArchie Cobbs 		if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
805631cabbaSGleb Smirnoff 			link_p destLink = host->link;
806ed2dbd31SArchie Cobbs 
807ed2dbd31SArchie Cobbs 			/* If destination same as incoming link, do nothing */
808631cabbaSGleb Smirnoff 			if (destLink == ctx.incoming) {
809069154d5SJulian Elischer 				NG_FREE_ITEM(item);
810631cabbaSGleb Smirnoff 				NG_FREE_M(ctx.m);
811ed2dbd31SArchie Cobbs 				return (0);
812ed2dbd31SArchie Cobbs 			}
813ed2dbd31SArchie Cobbs 
814ed2dbd31SArchie Cobbs 			/* Deliver packet out the destination link */
815ed2dbd31SArchie Cobbs 			destLink->stats.xmitPackets++;
816631cabbaSGleb Smirnoff 			destLink->stats.xmitOctets += ctx.m->m_pkthdr.len;
817631cabbaSGleb Smirnoff 			NG_FWD_NEW_DATA(ctx.error, item, destLink->hook, ctx.m);
818631cabbaSGleb Smirnoff 			return (ctx.error);
819ed2dbd31SArchie Cobbs 		}
820ed2dbd31SArchie Cobbs 
821ed2dbd31SArchie Cobbs 		/* Destination host is not known */
822631cabbaSGleb Smirnoff 		ctx.incoming->stats.recvUnknown++;
823ed2dbd31SArchie Cobbs 	}
824ed2dbd31SArchie Cobbs 
825ed2dbd31SArchie Cobbs 	/* Distribute unknown, multicast, broadcast pkts to all other links */
826631cabbaSGleb Smirnoff 	NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx, ret);
827ed2dbd31SArchie Cobbs 
828069154d5SJulian Elischer 	/* If we never saw a good link, leave. */
829631cabbaSGleb Smirnoff 	if (ctx.foundFirst == NULL || ctx.error != 0) {
830069154d5SJulian Elischer 		NG_FREE_ITEM(item);
831631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
832631cabbaSGleb Smirnoff 		return (ctx.error);
833069154d5SJulian Elischer 	}
834069154d5SJulian Elischer 
835069154d5SJulian Elischer 	/*
836069154d5SJulian Elischer 	 * If we've sent all the others, send the original
837069154d5SJulian Elischer 	 * on the first link we found.
838069154d5SJulian Elischer 	 */
839631cabbaSGleb Smirnoff 	NG_FWD_NEW_DATA(ctx.error, item, ctx.foundFirst->hook, ctx.m);
840631cabbaSGleb Smirnoff 	return (ctx.error);
841ed2dbd31SArchie Cobbs }
842ed2dbd31SArchie Cobbs 
843ed2dbd31SArchie Cobbs /*
844ed2dbd31SArchie Cobbs  * Shutdown node
845ed2dbd31SArchie Cobbs  */
846ed2dbd31SArchie Cobbs static int
847069154d5SJulian Elischer ng_bridge_shutdown(node_p node)
848ed2dbd31SArchie Cobbs {
84930400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
850ed2dbd31SArchie Cobbs 
8516c12c2b1SArchie Cobbs 	/*
852195cf617SRuslan Ermilov 	 * Shut down everything including the timer.  Even if the
853195cf617SRuslan Ermilov 	 * callout has already been dequeued and is about to be
854195cf617SRuslan Ermilov 	 * run, ng_bridge_timeout() won't be fired as the node
855195cf617SRuslan Ermilov 	 * is already marked NGF_INVALID, so we're safe to free
856195cf617SRuslan Ermilov 	 * the node now.
8576c12c2b1SArchie Cobbs 	 */
858ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
859ed2dbd31SArchie Cobbs 	    ("%s: numLinks=%d numHosts=%d",
8606e551fb6SDavid E. O'Brien 	    __func__, priv->numLinks, priv->numHosts));
861195cf617SRuslan Ermilov 	ng_uncallout(&priv->timer, node);
862195cf617SRuslan Ermilov 	NG_NODE_SET_PRIVATE(node, NULL);
863195cf617SRuslan Ermilov 	NG_NODE_UNREF(node);
8641ede983cSDag-Erling Smørgrav 	free(priv->tab, M_NETGRAPH_BRIDGE);
8651ede983cSDag-Erling Smørgrav 	free(priv, M_NETGRAPH_BRIDGE);
866ed2dbd31SArchie Cobbs 	return (0);
867ed2dbd31SArchie Cobbs }
868ed2dbd31SArchie Cobbs 
869ed2dbd31SArchie Cobbs /*
870ed2dbd31SArchie Cobbs  * Hook disconnection.
871ed2dbd31SArchie Cobbs  */
872ed2dbd31SArchie Cobbs static int
873ed2dbd31SArchie Cobbs ng_bridge_disconnect(hook_p hook)
874ed2dbd31SArchie Cobbs {
87530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
876631cabbaSGleb Smirnoff 	link_p link = NG_HOOK_PRIVATE(hook);
877ed2dbd31SArchie Cobbs 
878ed2dbd31SArchie Cobbs 	/* Remove all hosts associated with this link */
879631cabbaSGleb Smirnoff 	ng_bridge_remove_hosts(priv, link);
880ed2dbd31SArchie Cobbs 
881ed2dbd31SArchie Cobbs 	/* Free associated link information */
882631cabbaSGleb Smirnoff 	free(link, M_NETGRAPH_BRIDGE);
883ed2dbd31SArchie Cobbs 	priv->numLinks--;
884ed2dbd31SArchie Cobbs 
885ed2dbd31SArchie Cobbs 	/* If no more hooks, go away */
88630400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
887f8aab721SMarko Zec 	    && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
888f8aab721SMarko Zec 	    && !priv->persistent) {
88930400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));
89030400f03SJulian Elischer 	}
891ed2dbd31SArchie Cobbs 	return (0);
892ed2dbd31SArchie Cobbs }
893ed2dbd31SArchie Cobbs 
894ed2dbd31SArchie Cobbs /******************************************************************
895ed2dbd31SArchie Cobbs 		    HASH TABLE FUNCTIONS
896ed2dbd31SArchie Cobbs ******************************************************************/
897ed2dbd31SArchie Cobbs 
898ed2dbd31SArchie Cobbs /*
899ed2dbd31SArchie Cobbs  * Hash algorithm
900ed2dbd31SArchie Cobbs  */
901ed2dbd31SArchie Cobbs #define HASH(addr,mask)		( (((const u_int16_t *)(addr))[0] 	\
902ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[1] 	\
903ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
904ed2dbd31SArchie Cobbs 
905ed2dbd31SArchie Cobbs /*
906ed2dbd31SArchie Cobbs  * Find a host entry in the table.
907ed2dbd31SArchie Cobbs  */
908ed2dbd31SArchie Cobbs static struct ng_bridge_host *
909ed2dbd31SArchie Cobbs ng_bridge_get(priv_p priv, const u_char *addr)
910ed2dbd31SArchie Cobbs {
911ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
912ed2dbd31SArchie Cobbs 	struct ng_bridge_hent *hent;
913ed2dbd31SArchie Cobbs 
914ed2dbd31SArchie Cobbs 	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
915ed2dbd31SArchie Cobbs 		if (ETHER_EQUAL(hent->host.addr, addr))
916ed2dbd31SArchie Cobbs 			return (&hent->host);
917ed2dbd31SArchie Cobbs 	}
918ed2dbd31SArchie Cobbs 	return (NULL);
919ed2dbd31SArchie Cobbs }
920ed2dbd31SArchie Cobbs 
921ed2dbd31SArchie Cobbs /*
922ed2dbd31SArchie Cobbs  * Add a new host entry to the table. This assumes the host doesn't
923ed2dbd31SArchie Cobbs  * already exist in the table. Returns 1 on success, 0 if there
924ed2dbd31SArchie Cobbs  * was a memory allocation failure.
925ed2dbd31SArchie Cobbs  */
926ed2dbd31SArchie Cobbs static int
927631cabbaSGleb Smirnoff ng_bridge_put(priv_p priv, const u_char *addr, link_p link)
928ed2dbd31SArchie Cobbs {
929ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
930ed2dbd31SArchie Cobbs 	struct ng_bridge_hent *hent;
931ed2dbd31SArchie Cobbs 
932ed2dbd31SArchie Cobbs #ifdef INVARIANTS
933ed2dbd31SArchie Cobbs 	/* Assert that entry does not already exist in hashtable */
934ed2dbd31SArchie Cobbs 	SLIST_FOREACH(hent, &priv->tab[bucket], next) {
935ed2dbd31SArchie Cobbs 		KASSERT(!ETHER_EQUAL(hent->host.addr, addr),
9366e551fb6SDavid E. O'Brien 		    ("%s: entry %6D exists in table", __func__, addr, ":"));
937ed2dbd31SArchie Cobbs 	}
938ed2dbd31SArchie Cobbs #endif
939ed2dbd31SArchie Cobbs 
940ed2dbd31SArchie Cobbs 	/* Allocate and initialize new hashtable entry */
9411ede983cSDag-Erling Smørgrav 	hent = malloc(sizeof(*hent), M_NETGRAPH_BRIDGE, M_NOWAIT);
942ed2dbd31SArchie Cobbs 	if (hent == NULL)
943ed2dbd31SArchie Cobbs 		return (0);
944ed2dbd31SArchie Cobbs 	bcopy(addr, hent->host.addr, ETHER_ADDR_LEN);
945631cabbaSGleb Smirnoff 	hent->host.link = link;
946ed2dbd31SArchie Cobbs 	hent->host.staleness = 0;
947ed2dbd31SArchie Cobbs 	hent->host.age = 0;
948ed2dbd31SArchie Cobbs 
949ed2dbd31SArchie Cobbs 	/* Add new element to hash bucket */
950ed2dbd31SArchie Cobbs 	SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next);
951ed2dbd31SArchie Cobbs 	priv->numHosts++;
952ed2dbd31SArchie Cobbs 
953ed2dbd31SArchie Cobbs 	/* Resize table if necessary */
954ed2dbd31SArchie Cobbs 	ng_bridge_rehash(priv);
955ed2dbd31SArchie Cobbs 	return (1);
956ed2dbd31SArchie Cobbs }
957ed2dbd31SArchie Cobbs 
958ed2dbd31SArchie Cobbs /*
959ed2dbd31SArchie Cobbs  * Resize the hash table. We try to maintain the number of buckets
960ed2dbd31SArchie Cobbs  * such that the load factor is in the range 0.25 to 1.0.
961ed2dbd31SArchie Cobbs  *
962ed2dbd31SArchie Cobbs  * If we can't get the new memory then we silently fail. This is OK
963ed2dbd31SArchie Cobbs  * because things will still work and we'll try again soon anyway.
964ed2dbd31SArchie Cobbs  */
965ed2dbd31SArchie Cobbs static void
966ed2dbd31SArchie Cobbs ng_bridge_rehash(priv_p priv)
967ed2dbd31SArchie Cobbs {
968ed2dbd31SArchie Cobbs 	struct ng_bridge_bucket *newTab;
969ed2dbd31SArchie Cobbs 	int oldBucket, newBucket;
970ed2dbd31SArchie Cobbs 	int newNumBuckets;
971ed2dbd31SArchie Cobbs 	u_int newMask;
972ed2dbd31SArchie Cobbs 
973ed2dbd31SArchie Cobbs 	/* Is table too full or too empty? */
974ed2dbd31SArchie Cobbs 	if (priv->numHosts > priv->numBuckets
975ed2dbd31SArchie Cobbs 	    && (priv->numBuckets << 1) <= MAX_BUCKETS)
976ed2dbd31SArchie Cobbs 		newNumBuckets = priv->numBuckets << 1;
977ed2dbd31SArchie Cobbs 	else if (priv->numHosts < (priv->numBuckets >> 2)
978ed2dbd31SArchie Cobbs 	    && (priv->numBuckets >> 2) >= MIN_BUCKETS)
979ed2dbd31SArchie Cobbs 		newNumBuckets = priv->numBuckets >> 2;
980ed2dbd31SArchie Cobbs 	else
981ed2dbd31SArchie Cobbs 		return;
982ed2dbd31SArchie Cobbs 	newMask = newNumBuckets - 1;
983ed2dbd31SArchie Cobbs 
984ed2dbd31SArchie Cobbs 	/* Allocate and initialize new table */
985ac2fffa4SPedro F. Giffuni 	newTab = malloc(newNumBuckets * sizeof(*newTab),
986e11e3f18SDag-Erling Smørgrav 	    M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
987ed2dbd31SArchie Cobbs 	if (newTab == NULL)
988ed2dbd31SArchie Cobbs 		return;
989ed2dbd31SArchie Cobbs 
990ed2dbd31SArchie Cobbs 	/* Move all entries from old table to new table */
991ed2dbd31SArchie Cobbs 	for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
992ed2dbd31SArchie Cobbs 		struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
993ed2dbd31SArchie Cobbs 
994ed2dbd31SArchie Cobbs 		while (!SLIST_EMPTY(oldList)) {
995ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent
996ed2dbd31SArchie Cobbs 			    = SLIST_FIRST(oldList);
997ed2dbd31SArchie Cobbs 
998ed2dbd31SArchie Cobbs 			SLIST_REMOVE_HEAD(oldList, next);
999ed2dbd31SArchie Cobbs 			newBucket = HASH(hent->host.addr, newMask);
1000ed2dbd31SArchie Cobbs 			SLIST_INSERT_HEAD(&newTab[newBucket], hent, next);
1001ed2dbd31SArchie Cobbs 		}
1002ed2dbd31SArchie Cobbs 	}
1003ed2dbd31SArchie Cobbs 
1004ed2dbd31SArchie Cobbs 	/* Replace old table with new one */
1005ed2dbd31SArchie Cobbs 	if (priv->conf.debugLevel >= 3) {
1006ed2dbd31SArchie Cobbs 		log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
1007ed2dbd31SArchie Cobbs 		    ng_bridge_nodename(priv->node),
1008ed2dbd31SArchie Cobbs 		    priv->numBuckets, newNumBuckets);
1009ed2dbd31SArchie Cobbs 	}
10101ede983cSDag-Erling Smørgrav 	free(priv->tab, M_NETGRAPH_BRIDGE);
1011ed2dbd31SArchie Cobbs 	priv->numBuckets = newNumBuckets;
1012ed2dbd31SArchie Cobbs 	priv->hashMask = newMask;
1013ed2dbd31SArchie Cobbs 	priv->tab = newTab;
1014ed2dbd31SArchie Cobbs 	return;
1015ed2dbd31SArchie Cobbs }
1016ed2dbd31SArchie Cobbs 
1017ed2dbd31SArchie Cobbs /******************************************************************
1018ed2dbd31SArchie Cobbs 		    MISC FUNCTIONS
1019ed2dbd31SArchie Cobbs ******************************************************************/
1020ed2dbd31SArchie Cobbs 
1021ed2dbd31SArchie Cobbs /*
1022ed2dbd31SArchie Cobbs  * Remove all hosts associated with a specific link from the hashtable.
1023ed2dbd31SArchie Cobbs  * If linkNum == -1, then remove all hosts in the table.
1024ed2dbd31SArchie Cobbs  */
1025ed2dbd31SArchie Cobbs static void
1026631cabbaSGleb Smirnoff ng_bridge_remove_hosts(priv_p priv, link_p link)
1027ed2dbd31SArchie Cobbs {
1028ed2dbd31SArchie Cobbs 	int bucket;
1029ed2dbd31SArchie Cobbs 
1030ed2dbd31SArchie Cobbs 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1031ed2dbd31SArchie Cobbs 		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1032ed2dbd31SArchie Cobbs 
1033ed2dbd31SArchie Cobbs 		while (*hptr != NULL) {
1034ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent = *hptr;
1035ed2dbd31SArchie Cobbs 
1036631cabbaSGleb Smirnoff 			if (link == NULL || hent->host.link == link) {
1037ed2dbd31SArchie Cobbs 				*hptr = SLIST_NEXT(hent, next);
10381ede983cSDag-Erling Smørgrav 				free(hent, M_NETGRAPH_BRIDGE);
1039ed2dbd31SArchie Cobbs 				priv->numHosts--;
1040ed2dbd31SArchie Cobbs 			} else
1041ed2dbd31SArchie Cobbs 				hptr = &SLIST_NEXT(hent, next);
1042ed2dbd31SArchie Cobbs 		}
1043ed2dbd31SArchie Cobbs 	}
1044ed2dbd31SArchie Cobbs }
1045ed2dbd31SArchie Cobbs 
1046ed2dbd31SArchie Cobbs /*
1047ed2dbd31SArchie Cobbs  * Handle our once-per-second timeout event. We do two things:
1048ed2dbd31SArchie Cobbs  * we decrement link->loopCount for those links being muted due to
1049ed2dbd31SArchie Cobbs  * a detected loopback condition, and we remove any hosts from
1050ed2dbd31SArchie Cobbs  * the hashtable whom we haven't heard from in a long while.
1051ed2dbd31SArchie Cobbs  */
1052631cabbaSGleb Smirnoff static int
10530b951c55SGleb Smirnoff ng_bridge_unmute(hook_p hook, void *arg)
1054631cabbaSGleb Smirnoff {
1055631cabbaSGleb Smirnoff 	link_p link = NG_HOOK_PRIVATE(hook);
1056631cabbaSGleb Smirnoff 	node_p node = NG_HOOK_NODE(hook);
1057631cabbaSGleb Smirnoff 	priv_p priv = NG_NODE_PRIVATE(node);
10580b951c55SGleb Smirnoff 	int *counter = arg;
1059631cabbaSGleb Smirnoff 
1060631cabbaSGleb Smirnoff 	if (link->loopCount != 0) {
1061631cabbaSGleb Smirnoff 		link->loopCount--;
1062631cabbaSGleb Smirnoff 		if (link->loopCount == 0 && priv->conf.debugLevel >= 2) {
1063631cabbaSGleb Smirnoff 			log(LOG_INFO, "ng_bridge: %s:"
1064631cabbaSGleb Smirnoff 			    " restoring looped back %s\n",
1065631cabbaSGleb Smirnoff 			    ng_bridge_nodename(node), NG_HOOK_NAME(hook));
1066631cabbaSGleb Smirnoff 		}
1067631cabbaSGleb Smirnoff 	}
1068abc4b11cSGleb Smirnoff 	(*counter)++;
1069631cabbaSGleb Smirnoff 	return (1);
1070631cabbaSGleb Smirnoff }
1071631cabbaSGleb Smirnoff 
1072ed2dbd31SArchie Cobbs static void
1073e0d32af7SGleb Smirnoff ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1074ed2dbd31SArchie Cobbs {
107530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1076e0d32af7SGleb Smirnoff 	int bucket;
1077ed2dbd31SArchie Cobbs 	int counter = 0;
1078631cabbaSGleb Smirnoff 	hook_p ret;
1079ed2dbd31SArchie Cobbs 
1080ed2dbd31SArchie Cobbs 	/* Update host time counters and remove stale entries */
1081ed2dbd31SArchie Cobbs 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1082ed2dbd31SArchie Cobbs 		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1083ed2dbd31SArchie Cobbs 
1084ed2dbd31SArchie Cobbs 		while (*hptr != NULL) {
1085ed2dbd31SArchie Cobbs 			struct ng_bridge_hent *const hent = *hptr;
1086ed2dbd31SArchie Cobbs 
1087ed2dbd31SArchie Cobbs 			/* Remove hosts we haven't heard from in a while */
1088ed2dbd31SArchie Cobbs 			if (++hent->host.staleness >= priv->conf.maxStaleness) {
1089ed2dbd31SArchie Cobbs 				*hptr = SLIST_NEXT(hent, next);
10901ede983cSDag-Erling Smørgrav 				free(hent, M_NETGRAPH_BRIDGE);
1091ed2dbd31SArchie Cobbs 				priv->numHosts--;
1092ed2dbd31SArchie Cobbs 			} else {
1093ed2dbd31SArchie Cobbs 				if (hent->host.age < 0xffff)
1094ed2dbd31SArchie Cobbs 					hent->host.age++;
1095ed2dbd31SArchie Cobbs 				hptr = &SLIST_NEXT(hent, next);
1096ed2dbd31SArchie Cobbs 				counter++;
1097ed2dbd31SArchie Cobbs 			}
1098ed2dbd31SArchie Cobbs 		}
1099ed2dbd31SArchie Cobbs 	}
1100ed2dbd31SArchie Cobbs 	KASSERT(priv->numHosts == counter,
11016e551fb6SDavid E. O'Brien 	    ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
1102ed2dbd31SArchie Cobbs 
1103ed2dbd31SArchie Cobbs 	/* Decrease table size if necessary */
1104ed2dbd31SArchie Cobbs 	ng_bridge_rehash(priv);
1105ed2dbd31SArchie Cobbs 
1106ed2dbd31SArchie Cobbs 	/* Decrease loop counter on muted looped back links */
1107631cabbaSGleb Smirnoff 	counter = 0;
1108631cabbaSGleb Smirnoff 	NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter, ret);
1109ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == counter,
11106e551fb6SDavid E. O'Brien 	    ("%s: links: %d != %d", __func__, priv->numLinks, counter));
1111ed2dbd31SArchie Cobbs 
1112e0d32af7SGleb Smirnoff 	/* Register a new timeout, keeping the existing node reference */
1113e0d32af7SGleb Smirnoff 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
1114ed2dbd31SArchie Cobbs }
1115ed2dbd31SArchie Cobbs 
1116ed2dbd31SArchie Cobbs /*
1117ed2dbd31SArchie Cobbs  * Return node's "name", even if it doesn't have one.
1118ed2dbd31SArchie Cobbs  */
1119ed2dbd31SArchie Cobbs static const char *
1120ed2dbd31SArchie Cobbs ng_bridge_nodename(node_p node)
1121ed2dbd31SArchie Cobbs {
112287e2c66aSHartmut Brandt 	static char name[NG_NODESIZ];
1123ed2dbd31SArchie Cobbs 
1124dd10c1e2SGleb Smirnoff 	if (NG_NODE_HAS_NAME(node))
112530400f03SJulian Elischer 		snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
1126ed2dbd31SArchie Cobbs 	else
1127ed2dbd31SArchie Cobbs 		snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
1128ed2dbd31SArchie Cobbs 	return name;
1129ed2dbd31SArchie Cobbs }
1130