xref: /freebsd/sys/netgraph/ng_bridge.c (revision a56e5ad6903037861457da754574b4903dcb0e7e)
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>
6866c72859SLutz Donnerhacke #include <sys/types.h>
6966c72859SLutz Donnerhacke #include <sys/counter.h>
70ed2dbd31SArchie Cobbs 
71ed2dbd31SArchie Cobbs #include <net/if.h>
7276039bc8SGleb Smirnoff #include <net/if_var.h>
73ed2dbd31SArchie Cobbs #include <net/ethernet.h>
74530c0060SRobert Watson #include <net/vnet.h>
75ed2dbd31SArchie Cobbs 
76ed2dbd31SArchie Cobbs #include <netinet/in.h>
775f2e1642SLuigi Rizzo #if 0	/* not used yet */
78ed2dbd31SArchie Cobbs #include <netinet/ip_fw.h>
795f2e1642SLuigi Rizzo #endif
80ed2dbd31SArchie Cobbs #include <netgraph/ng_message.h>
81ed2dbd31SArchie Cobbs #include <netgraph/netgraph.h>
82ed2dbd31SArchie Cobbs #include <netgraph/ng_parse.h>
83ed2dbd31SArchie Cobbs #include <netgraph/ng_bridge.h>
84ed2dbd31SArchie Cobbs 
859c8c302fSJulian Elischer #ifdef NG_SEPARATE_MALLOC
86d745c852SEd Schouten static MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge",
87d745c852SEd Schouten     "netgraph bridge node");
889c8c302fSJulian Elischer #else
899c8c302fSJulian Elischer #define M_NETGRAPH_BRIDGE M_NETGRAPH
909c8c302fSJulian Elischer #endif
919c8c302fSJulian Elischer 
9266c72859SLutz Donnerhacke /* Counter based stats */
9366c72859SLutz Donnerhacke struct ng_bridge_link_kernel_stats {
9466c72859SLutz Donnerhacke 	counter_u64_t	recvOctets;	/* total octets rec'd on link */
9566c72859SLutz Donnerhacke 	counter_u64_t	recvPackets;	/* total pkts rec'd on link */
9666c72859SLutz Donnerhacke 	counter_u64_t	recvMulticasts;	/* multicast pkts rec'd on link */
9766c72859SLutz Donnerhacke 	counter_u64_t	recvBroadcasts;	/* broadcast pkts rec'd on link */
9866c72859SLutz Donnerhacke 	counter_u64_t	recvUnknown;	/* pkts rec'd with unknown dest addr */
9966c72859SLutz Donnerhacke 	counter_u64_t	recvRunts;	/* pkts rec'd less than 14 bytes */
10066c72859SLutz Donnerhacke 	counter_u64_t	recvInvalid;	/* pkts rec'd with bogus source addr */
10166c72859SLutz Donnerhacke 	counter_u64_t	xmitOctets;	/* total octets xmit'd on link */
10266c72859SLutz Donnerhacke 	counter_u64_t	xmitPackets;	/* total pkts xmit'd on link */
10366c72859SLutz Donnerhacke 	counter_u64_t	xmitMulticasts;	/* multicast pkts xmit'd on link */
10466c72859SLutz Donnerhacke 	counter_u64_t	xmitBroadcasts;	/* broadcast pkts xmit'd on link */
10566c72859SLutz Donnerhacke 	counter_u64_t	loopDrops;	/* pkts dropped due to loopback */
106f6e0c471SLutz Donnerhacke 	u_int64_t	loopDetects;	/* number of loop detections */
10766c72859SLutz Donnerhacke 	counter_u64_t	memoryFailures;	/* times couldn't get mem or mbuf */
10866c72859SLutz Donnerhacke };
10966c72859SLutz Donnerhacke 
110ed2dbd31SArchie Cobbs /* Per-link private data */
111ed2dbd31SArchie Cobbs struct ng_bridge_link {
112ed2dbd31SArchie Cobbs 	hook_p				hook;		/* netgraph hook */
113ed2dbd31SArchie Cobbs 	u_int16_t			loopCount;	/* loop ignore timer */
114f961caf2SLutz Donnerhacke 	unsigned int			learnMac : 1,   /* autolearn macs */
115f961caf2SLutz Donnerhacke 					sendUnknown : 1;/* send unknown macs out */
11666c72859SLutz Donnerhacke 	struct ng_bridge_link_kernel_stats stats;	/* link stats */
117ed2dbd31SArchie Cobbs };
1186117aa58SLutz Donnerhacke typedef struct ng_bridge_link const *link_cp;	/* read only access */
119ed2dbd31SArchie Cobbs 
120ed2dbd31SArchie Cobbs /* Per-node private data */
121ed2dbd31SArchie Cobbs struct ng_bridge_private {
122ed2dbd31SArchie Cobbs 	struct ng_bridge_bucket	*tab;		/* hash table bucket array */
123ed2dbd31SArchie Cobbs 	struct ng_bridge_config	conf;		/* node configuration */
124ed2dbd31SArchie Cobbs 	node_p			node;		/* netgraph node */
125ed2dbd31SArchie Cobbs 	u_int			numHosts;	/* num entries in table */
126ed2dbd31SArchie Cobbs 	u_int			numBuckets;	/* num buckets in table */
127ed2dbd31SArchie Cobbs 	u_int			hashMask;	/* numBuckets - 1 */
128ed2dbd31SArchie Cobbs 	int			numLinks;	/* num connected links */
129c869d905SLutz Donnerhacke 	unsigned int		persistent : 1,	/* can exist w/o hooks */
130c869d905SLutz Donnerhacke 				sendUnknown : 1;/* links receive unknowns by default */
131ed2dbd31SArchie Cobbs 	struct callout		timer;		/* one second periodic timer */
132ed2dbd31SArchie Cobbs };
133ed2dbd31SArchie Cobbs typedef struct ng_bridge_private *priv_p;
1346117aa58SLutz Donnerhacke typedef struct ng_bridge_private const *priv_cp;	/* read only access */
135ed2dbd31SArchie Cobbs 
136ed2dbd31SArchie Cobbs /* Information about a host, stored in a hash table entry */
137ccf4cd2eSLutz Donnerhacke struct ng_bridge_host {
138ccf4cd2eSLutz Donnerhacke 	u_char		addr[6];	/* ethernet address */
139ccf4cd2eSLutz Donnerhacke 	link_p		link;		/* link where addr can be found */
140ccf4cd2eSLutz Donnerhacke 	u_int16_t	age;		/* seconds ago entry was created */
141ccf4cd2eSLutz Donnerhacke 	u_int16_t	staleness;	/* seconds ago host last heard from */
142ccf4cd2eSLutz Donnerhacke 	SLIST_ENTRY(ng_bridge_host)	next;	/* next entry in bucket */
143ed2dbd31SArchie Cobbs };
144ed2dbd31SArchie Cobbs 
145ed2dbd31SArchie Cobbs /* Hash table bucket declaration */
146ccf4cd2eSLutz Donnerhacke SLIST_HEAD(ng_bridge_bucket, ng_bridge_host);
147ed2dbd31SArchie Cobbs 
148ed2dbd31SArchie Cobbs /* Netgraph node methods */
149ed2dbd31SArchie Cobbs static ng_constructor_t	ng_bridge_constructor;
150ed2dbd31SArchie Cobbs static ng_rcvmsg_t	ng_bridge_rcvmsg;
151069154d5SJulian Elischer static ng_shutdown_t	ng_bridge_shutdown;
152ed2dbd31SArchie Cobbs static ng_newhook_t	ng_bridge_newhook;
153ed2dbd31SArchie Cobbs static ng_rcvdata_t	ng_bridge_rcvdata;
154ed2dbd31SArchie Cobbs static ng_disconnect_t	ng_bridge_disconnect;
155ed2dbd31SArchie Cobbs 
156ed2dbd31SArchie Cobbs /* Other internal functions */
1576117aa58SLutz Donnerhacke static struct	ng_bridge_host *ng_bridge_get(priv_cp priv, const u_char *addr);
158631cabbaSGleb Smirnoff static int	ng_bridge_put(priv_p priv, const u_char *addr, link_p link);
159ed2dbd31SArchie Cobbs static void	ng_bridge_rehash(priv_p priv);
160631cabbaSGleb Smirnoff static void	ng_bridge_remove_hosts(priv_p priv, link_p link);
161e0d32af7SGleb Smirnoff static void	ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2);
1626117aa58SLutz Donnerhacke static const	char *ng_bridge_nodename(node_cp node);
163ed2dbd31SArchie Cobbs 
164ed2dbd31SArchie Cobbs /* Ethernet broadcast */
165ed2dbd31SArchie Cobbs static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
166ed2dbd31SArchie Cobbs     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
167ed2dbd31SArchie Cobbs 
168ed2dbd31SArchie Cobbs /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
169ed2dbd31SArchie Cobbs #define ETHER_EQUAL(a,b)	(((const u_int32_t *)(a))[0] \
170ed2dbd31SArchie Cobbs 					== ((const u_int32_t *)(b))[0] \
171ed2dbd31SArchie Cobbs 				    && ((const u_int16_t *)(a))[2] \
172ed2dbd31SArchie Cobbs 					== ((const u_int16_t *)(b))[2])
173ed2dbd31SArchie Cobbs 
174ed2dbd31SArchie Cobbs /* Minimum and maximum number of hash buckets. Must be a power of two. */
175ed2dbd31SArchie Cobbs #define MIN_BUCKETS		(1 << 5)	/* 32 */
176ed2dbd31SArchie Cobbs #define MAX_BUCKETS		(1 << 14)	/* 16384 */
177ed2dbd31SArchie Cobbs 
178ed2dbd31SArchie Cobbs /* Configuration default values */
179ed2dbd31SArchie Cobbs #define DEFAULT_LOOP_TIMEOUT	60
180ed2dbd31SArchie Cobbs #define DEFAULT_MAX_STALENESS	(15 * 60)	/* same as ARP timeout */
181ed2dbd31SArchie Cobbs #define DEFAULT_MIN_STABLE_AGE	1
182ed2dbd31SArchie Cobbs 
183ed2dbd31SArchie Cobbs /******************************************************************
184ed2dbd31SArchie Cobbs 		    NETGRAPH PARSE TYPES
185ed2dbd31SArchie Cobbs ******************************************************************/
186ed2dbd31SArchie Cobbs 
187ed2dbd31SArchie Cobbs /*
188ed2dbd31SArchie Cobbs  * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
189ed2dbd31SArchie Cobbs  */
190ed2dbd31SArchie Cobbs static int
191ed2dbd31SArchie Cobbs ng_bridge_getTableLength(const struct ng_parse_type *type,
192ed2dbd31SArchie Cobbs 	const u_char *start, const u_char *buf)
193ed2dbd31SArchie Cobbs {
194ed2dbd31SArchie Cobbs 	const struct ng_bridge_host_ary *const hary
195ed2dbd31SArchie Cobbs 	    = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
196ed2dbd31SArchie Cobbs 
197ed2dbd31SArchie Cobbs 	return hary->numHosts;
198ed2dbd31SArchie Cobbs }
199ed2dbd31SArchie Cobbs 
200ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_host_ary */
201f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_type_fields[]
2028c7e4101SRuslan Ermilov 	= NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
203ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_type = {
204ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
205f0184ff8SArchie Cobbs 	&ng_bridge_host_type_fields
206ed2dbd31SArchie Cobbs };
207ed2dbd31SArchie Cobbs static const struct ng_parse_array_info ng_bridge_hary_type_info = {
208ed2dbd31SArchie Cobbs 	&ng_bridge_host_type,
209ed2dbd31SArchie Cobbs 	ng_bridge_getTableLength
210ed2dbd31SArchie Cobbs };
211ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_hary_type = {
212ed2dbd31SArchie Cobbs 	&ng_parse_array_type,
213ed2dbd31SArchie Cobbs 	&ng_bridge_hary_type_info
214ed2dbd31SArchie Cobbs };
215f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[]
216ed2dbd31SArchie Cobbs 	= NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
217ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_host_ary_type = {
218ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
219f0184ff8SArchie Cobbs 	&ng_bridge_host_ary_type_fields
220ed2dbd31SArchie Cobbs };
221ed2dbd31SArchie Cobbs 
222ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_config */
223f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_config_type_fields[]
224631cabbaSGleb Smirnoff 	= NG_BRIDGE_CONFIG_TYPE_INFO;
225ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_config_type = {
226ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
227f0184ff8SArchie Cobbs 	&ng_bridge_config_type_fields
228ed2dbd31SArchie Cobbs };
229ed2dbd31SArchie Cobbs 
230ed2dbd31SArchie Cobbs /* Parse type for struct ng_bridge_link_stat */
231f0184ff8SArchie Cobbs static const struct ng_parse_struct_field ng_bridge_stats_type_fields[]
232f0184ff8SArchie Cobbs 	= NG_BRIDGE_STATS_TYPE_INFO;
233ed2dbd31SArchie Cobbs static const struct ng_parse_type ng_bridge_stats_type = {
234ed2dbd31SArchie Cobbs 	&ng_parse_struct_type,
235f0184ff8SArchie Cobbs 	&ng_bridge_stats_type_fields
236ed2dbd31SArchie Cobbs };
237b1bd4473SLutz Donnerhacke /* Parse type for struct ng_bridge_move_host */
238b1bd4473SLutz Donnerhacke static const struct ng_parse_struct_field ng_bridge_move_host_type_fields[]
239b1bd4473SLutz Donnerhacke 	= NG_BRIDGE_MOVE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
240b1bd4473SLutz Donnerhacke static const struct ng_parse_type ng_bridge_move_host_type = {
241b1bd4473SLutz Donnerhacke 	&ng_parse_struct_type,
242b1bd4473SLutz Donnerhacke 	&ng_bridge_move_host_type_fields
243b1bd4473SLutz Donnerhacke };
244ed2dbd31SArchie Cobbs 
245ed2dbd31SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
246ed2dbd31SArchie Cobbs static const struct ng_cmdlist ng_bridge_cmdlist[] = {
247ed2dbd31SArchie Cobbs 	{
248ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
249ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_SET_CONFIG,
250ed2dbd31SArchie Cobbs 	  "setconfig",
251ed2dbd31SArchie Cobbs 	  &ng_bridge_config_type,
252ed2dbd31SArchie Cobbs 	  NULL
253ed2dbd31SArchie Cobbs 	},
254ed2dbd31SArchie Cobbs 	{
255ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
256ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_CONFIG,
257ed2dbd31SArchie Cobbs 	  "getconfig",
258ed2dbd31SArchie Cobbs 	  NULL,
259ed2dbd31SArchie Cobbs 	  &ng_bridge_config_type
260ed2dbd31SArchie Cobbs 	},
261ed2dbd31SArchie Cobbs 	{
262ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
263ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_RESET,
264ed2dbd31SArchie Cobbs 	  "reset",
265ed2dbd31SArchie Cobbs 	  NULL,
266ed2dbd31SArchie Cobbs 	  NULL
267ed2dbd31SArchie Cobbs 	},
268ed2dbd31SArchie Cobbs 	{
269ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
270ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_STATS,
271ed2dbd31SArchie Cobbs 	  "getstats",
272ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
273ed2dbd31SArchie Cobbs 	  &ng_bridge_stats_type
274ed2dbd31SArchie Cobbs 	},
275ed2dbd31SArchie Cobbs 	{
276ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
277ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_CLR_STATS,
278ed2dbd31SArchie Cobbs 	  "clrstats",
279ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
280ed2dbd31SArchie Cobbs 	  NULL
281ed2dbd31SArchie Cobbs 	},
282ed2dbd31SArchie Cobbs 	{
283ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
284ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GETCLR_STATS,
285ed2dbd31SArchie Cobbs 	  "getclrstats",
286ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
287ed2dbd31SArchie Cobbs 	  &ng_bridge_stats_type
288ed2dbd31SArchie Cobbs 	},
289ed2dbd31SArchie Cobbs 	{
290ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
291ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_TABLE,
292ed2dbd31SArchie Cobbs 	  "gettable",
293ed2dbd31SArchie Cobbs 	  NULL,
294ed2dbd31SArchie Cobbs 	  &ng_bridge_host_ary_type
295ed2dbd31SArchie Cobbs 	},
296f8aab721SMarko Zec 	{
297f8aab721SMarko Zec 	  NGM_BRIDGE_COOKIE,
298f8aab721SMarko Zec 	  NGM_BRIDGE_SET_PERSISTENT,
299f8aab721SMarko Zec 	  "setpersistent",
300f8aab721SMarko Zec 	  NULL,
301f8aab721SMarko Zec 	  NULL
302f8aab721SMarko Zec 	},
303b1bd4473SLutz Donnerhacke 	{
304b1bd4473SLutz Donnerhacke 	  NGM_BRIDGE_COOKIE,
305b1bd4473SLutz Donnerhacke 	  NGM_BRIDGE_MOVE_HOST,
306b1bd4473SLutz Donnerhacke 	  "movehost",
307b1bd4473SLutz Donnerhacke 	  &ng_bridge_move_host_type,
308b1bd4473SLutz Donnerhacke 	  NULL
309b1bd4473SLutz Donnerhacke 	},
310ed2dbd31SArchie Cobbs 	{ 0 }
311ed2dbd31SArchie Cobbs };
312ed2dbd31SArchie Cobbs 
313ed2dbd31SArchie Cobbs /* Node type descriptor */
314ed2dbd31SArchie Cobbs static struct ng_type ng_bridge_typestruct = {
315f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
316f8aae777SJulian Elischer 	.name =		NG_BRIDGE_NODE_TYPE,
317f8aae777SJulian Elischer 	.constructor =	ng_bridge_constructor,
318f8aae777SJulian Elischer 	.rcvmsg =	ng_bridge_rcvmsg,
319f8aae777SJulian Elischer 	.shutdown =	ng_bridge_shutdown,
320f8aae777SJulian Elischer 	.newhook =	ng_bridge_newhook,
321f8aae777SJulian Elischer 	.rcvdata =	ng_bridge_rcvdata,
322f8aae777SJulian Elischer 	.disconnect =	ng_bridge_disconnect,
323f8aae777SJulian Elischer 	.cmdlist =	ng_bridge_cmdlist,
324ed2dbd31SArchie Cobbs };
325a89effcdSArchie Cobbs NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
326ed2dbd31SArchie Cobbs 
327ed2dbd31SArchie Cobbs /******************************************************************
328ed2dbd31SArchie Cobbs 		    NETGRAPH NODE METHODS
329ed2dbd31SArchie Cobbs ******************************************************************/
330ed2dbd31SArchie Cobbs 
331ed2dbd31SArchie Cobbs /*
332ed2dbd31SArchie Cobbs  * Node constructor
333ed2dbd31SArchie Cobbs  */
334ed2dbd31SArchie Cobbs static int
335069154d5SJulian Elischer ng_bridge_constructor(node_p node)
336ed2dbd31SArchie Cobbs {
337ed2dbd31SArchie Cobbs 	priv_p priv;
338ed2dbd31SArchie Cobbs 
339ed2dbd31SArchie Cobbs 	/* Allocate and initialize private info */
340674d86bfSGleb Smirnoff 	priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
341e0d32af7SGleb Smirnoff 	ng_callout_init(&priv->timer);
342ed2dbd31SArchie Cobbs 
343ed2dbd31SArchie Cobbs 	/* Allocate and initialize hash table, etc. */
344e11e3f18SDag-Erling Smørgrav 	priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab),
345674d86bfSGleb Smirnoff 	    M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
346ed2dbd31SArchie Cobbs 	priv->numBuckets = MIN_BUCKETS;
347ed2dbd31SArchie Cobbs 	priv->hashMask = MIN_BUCKETS - 1;
348ed2dbd31SArchie Cobbs 	priv->conf.debugLevel = 1;
349ed2dbd31SArchie Cobbs 	priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
350ed2dbd31SArchie Cobbs 	priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
351ed2dbd31SArchie Cobbs 	priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
352c869d905SLutz Donnerhacke 	priv->sendUnknown = 1;	       /* classic bridge */
353ed2dbd31SArchie Cobbs 
35430400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
355069154d5SJulian Elischer 	priv->node = node;
356ed2dbd31SArchie Cobbs 
3576c12c2b1SArchie Cobbs 	/* Start timer; timer is always running while node is alive */
358e0d32af7SGleb Smirnoff 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
3596c12c2b1SArchie Cobbs 
3606c12c2b1SArchie Cobbs 	/* Done */
361ed2dbd31SArchie Cobbs 	return (0);
362ed2dbd31SArchie Cobbs }
363ed2dbd31SArchie Cobbs 
364ed2dbd31SArchie Cobbs /*
365ed2dbd31SArchie Cobbs  * Method for attaching a new hook
366ed2dbd31SArchie Cobbs  */
367ed2dbd31SArchie Cobbs static	int
368ed2dbd31SArchie Cobbs ng_bridge_newhook(node_p node, hook_p hook, const char *name)
369ed2dbd31SArchie Cobbs {
37030400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
371631cabbaSGleb Smirnoff 	char linkName[NG_HOOKSIZ];
372631cabbaSGleb Smirnoff 	u_int32_t linkNum;
373631cabbaSGleb Smirnoff 	link_p link;
374f961caf2SLutz Donnerhacke 	const char *prefix = NG_BRIDGE_HOOK_LINK_PREFIX;
375f961caf2SLutz Donnerhacke 	bool isUplink;
376f961caf2SLutz Donnerhacke 
377f961caf2SLutz Donnerhacke 	/* Check for a link hook */
378f961caf2SLutz Donnerhacke 	if (strlen(name) <= strlen(prefix))
379f961caf2SLutz Donnerhacke 		return (EINVAL);       /* Unknown hook name */
380f961caf2SLutz Donnerhacke 
381f961caf2SLutz Donnerhacke 	isUplink = (name[0] == 'u');
382f961caf2SLutz Donnerhacke 	if (isUplink)
383f961caf2SLutz Donnerhacke 		prefix = NG_BRIDGE_HOOK_UPLINK_PREFIX;
384ed2dbd31SArchie Cobbs 
385631cabbaSGleb Smirnoff 	/* primitive parsing */
386f961caf2SLutz Donnerhacke 	linkNum = strtoul(name + strlen(prefix), NULL, 10);
387631cabbaSGleb Smirnoff 	/* validation by comparing against the reconstucted name  */
388f961caf2SLutz Donnerhacke 	snprintf(linkName, sizeof(linkName), "%s%u", prefix, linkNum);
389631cabbaSGleb Smirnoff 	if (strcmp(linkName, name) != 0)
390ed2dbd31SArchie Cobbs 		return (EINVAL);
391631cabbaSGleb Smirnoff 
392f961caf2SLutz Donnerhacke 	if (linkNum == 0 && isUplink)
393f961caf2SLutz Donnerhacke 		return (EINVAL);
394f961caf2SLutz Donnerhacke 
395631cabbaSGleb Smirnoff 	if(NG_PEER_NODE(hook) == node)
396631cabbaSGleb Smirnoff 	        return (ELOOP);
397631cabbaSGleb Smirnoff 
398f961caf2SLutz Donnerhacke 	link = malloc(sizeof(*link), M_NETGRAPH_BRIDGE, M_ZERO);
399631cabbaSGleb Smirnoff 	if (link == NULL)
400ed2dbd31SArchie Cobbs 		return (ENOMEM);
401f961caf2SLutz Donnerhacke 
40266c72859SLutz Donnerhacke 	link->stats.recvOctets = counter_u64_alloc(M_WAITOK);
40366c72859SLutz Donnerhacke 	link->stats.recvPackets = counter_u64_alloc(M_WAITOK);
40466c72859SLutz Donnerhacke 	link->stats.recvMulticasts = counter_u64_alloc(M_WAITOK);
40566c72859SLutz Donnerhacke 	link->stats.recvBroadcasts = counter_u64_alloc(M_WAITOK);
40666c72859SLutz Donnerhacke 	link->stats.recvUnknown = counter_u64_alloc(M_WAITOK);
40766c72859SLutz Donnerhacke 	link->stats.recvRunts = counter_u64_alloc(M_WAITOK);
40866c72859SLutz Donnerhacke 	link->stats.recvInvalid = counter_u64_alloc(M_WAITOK);
40966c72859SLutz Donnerhacke 	link->stats.xmitOctets = counter_u64_alloc(M_WAITOK);
41066c72859SLutz Donnerhacke 	link->stats.xmitPackets = counter_u64_alloc(M_WAITOK);
41166c72859SLutz Donnerhacke 	link->stats.xmitMulticasts = counter_u64_alloc(M_WAITOK);
41266c72859SLutz Donnerhacke 	link->stats.xmitBroadcasts = counter_u64_alloc(M_WAITOK);
41366c72859SLutz Donnerhacke 	link->stats.loopDrops = counter_u64_alloc(M_WAITOK);
41466c72859SLutz Donnerhacke 	link->stats.memoryFailures = counter_u64_alloc(M_WAITOK);
41566c72859SLutz Donnerhacke 
416631cabbaSGleb Smirnoff 	link->hook = hook;
417f961caf2SLutz Donnerhacke 	if (isUplink) {
418f961caf2SLutz Donnerhacke 		link->learnMac = 0;
419f961caf2SLutz Donnerhacke 		link->sendUnknown = 1;
420c869d905SLutz Donnerhacke 		if (priv->numLinks == 0)	/* if the first link is an uplink */
421c869d905SLutz Donnerhacke 		    priv->sendUnknown = 0;	/* switch to restrictive mode */
422f961caf2SLutz Donnerhacke 	} else {
423f961caf2SLutz Donnerhacke 		link->learnMac = 1;
424c869d905SLutz Donnerhacke 		link->sendUnknown = priv->sendUnknown;
425f961caf2SLutz Donnerhacke 	}
426f961caf2SLutz Donnerhacke 
427631cabbaSGleb Smirnoff 	NG_HOOK_SET_PRIVATE(hook, link);
428ed2dbd31SArchie Cobbs 	priv->numLinks++;
429ed2dbd31SArchie Cobbs 	return (0);
430ed2dbd31SArchie Cobbs }
431ed2dbd31SArchie Cobbs 
432ed2dbd31SArchie Cobbs /*
433ed2dbd31SArchie Cobbs  * Receive a control message
434ed2dbd31SArchie Cobbs  */
43566c72859SLutz Donnerhacke static void ng_bridge_clear_link_stats(struct ng_bridge_link_kernel_stats * p)
43666c72859SLutz Donnerhacke {
43766c72859SLutz Donnerhacke 	counter_u64_zero(p->recvOctets);
43866c72859SLutz Donnerhacke 	counter_u64_zero(p->recvPackets);
43966c72859SLutz Donnerhacke 	counter_u64_zero(p->recvMulticasts);
44066c72859SLutz Donnerhacke 	counter_u64_zero(p->recvBroadcasts);
44166c72859SLutz Donnerhacke 	counter_u64_zero(p->recvUnknown);
44266c72859SLutz Donnerhacke 	counter_u64_zero(p->recvRunts);
44366c72859SLutz Donnerhacke 	counter_u64_zero(p->recvInvalid);
44466c72859SLutz Donnerhacke 	counter_u64_zero(p->xmitOctets);
44566c72859SLutz Donnerhacke 	counter_u64_zero(p->xmitPackets);
44666c72859SLutz Donnerhacke 	counter_u64_zero(p->xmitMulticasts);
44766c72859SLutz Donnerhacke 	counter_u64_zero(p->xmitBroadcasts);
44866c72859SLutz Donnerhacke 	counter_u64_zero(p->loopDrops);
449f6e0c471SLutz Donnerhacke 	p->loopDetects = 0;
45066c72859SLutz Donnerhacke 	counter_u64_zero(p->memoryFailures);
45166c72859SLutz Donnerhacke };
45266c72859SLutz Donnerhacke 
453ed2dbd31SArchie Cobbs static int
4540b951c55SGleb Smirnoff ng_bridge_reset_link(hook_p hook, void *arg __unused)
455631cabbaSGleb Smirnoff {
456631cabbaSGleb Smirnoff 	link_p priv = NG_HOOK_PRIVATE(hook);
457631cabbaSGleb Smirnoff 
458631cabbaSGleb Smirnoff 	priv->loopCount = 0;
45966c72859SLutz Donnerhacke 	ng_bridge_clear_link_stats(&priv->stats);
4600b951c55SGleb Smirnoff 	return (1);
461631cabbaSGleb Smirnoff }
462631cabbaSGleb Smirnoff 
463631cabbaSGleb Smirnoff static int
464069154d5SJulian Elischer ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
465ed2dbd31SArchie Cobbs {
46630400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
467ed2dbd31SArchie Cobbs 	struct ng_mesg *resp = NULL;
468ed2dbd31SArchie Cobbs 	int error = 0;
469069154d5SJulian Elischer 	struct ng_mesg *msg;
470ed2dbd31SArchie Cobbs 
471069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
472ed2dbd31SArchie Cobbs 	switch (msg->header.typecookie) {
473ed2dbd31SArchie Cobbs 	case NGM_BRIDGE_COOKIE:
474ed2dbd31SArchie Cobbs 		switch (msg->header.cmd) {
475ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_CONFIG:
476ed2dbd31SArchie Cobbs 		    {
477ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
478ed2dbd31SArchie Cobbs 
479ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg,
480ed2dbd31SArchie Cobbs 			    sizeof(struct ng_bridge_config), M_NOWAIT);
481ed2dbd31SArchie Cobbs 			if (resp == NULL) {
482ed2dbd31SArchie Cobbs 				error = ENOMEM;
483ed2dbd31SArchie Cobbs 				break;
484ed2dbd31SArchie Cobbs 			}
485ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)resp->data;
486ed2dbd31SArchie Cobbs 			*conf = priv->conf;	/* no sanity checking needed */
487ed2dbd31SArchie Cobbs 			break;
488ed2dbd31SArchie Cobbs 		    }
489ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_SET_CONFIG:
490ed2dbd31SArchie Cobbs 		    {
491ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
492ed2dbd31SArchie Cobbs 
493ed2dbd31SArchie Cobbs 			if (msg->header.arglen
494ed2dbd31SArchie Cobbs 			    != sizeof(struct ng_bridge_config)) {
495ed2dbd31SArchie Cobbs 				error = EINVAL;
496ed2dbd31SArchie Cobbs 				break;
497ed2dbd31SArchie Cobbs 			}
498ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)msg->data;
499ed2dbd31SArchie Cobbs 			priv->conf = *conf;
500ed2dbd31SArchie Cobbs 			break;
501ed2dbd31SArchie Cobbs 		    }
502ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_RESET:
503ed2dbd31SArchie Cobbs 		    {
504631cabbaSGleb Smirnoff 			hook_p rethook;
505ed2dbd31SArchie Cobbs 
506ed2dbd31SArchie Cobbs 			/* Flush all entries in the hash table */
507631cabbaSGleb Smirnoff 			ng_bridge_remove_hosts(priv, NULL);
508ed2dbd31SArchie Cobbs 
509ed2dbd31SArchie Cobbs 			/* Reset all loop detection counters and stats */
5100b951c55SGleb Smirnoff 			NG_NODE_FOREACH_HOOK(node, ng_bridge_reset_link, NULL,
5110b951c55SGleb Smirnoff 			    rethook);
512ed2dbd31SArchie Cobbs 			break;
513ed2dbd31SArchie Cobbs 		    }
514ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_STATS:
515ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_CLR_STATS:
516ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GETCLR_STATS:
517ed2dbd31SArchie Cobbs 		    {
518631cabbaSGleb Smirnoff 			hook_p hook;
519631cabbaSGleb Smirnoff 			link_p link;
520631cabbaSGleb Smirnoff 			char linkName[NG_HOOKSIZ];
521f961caf2SLutz Donnerhacke 			int linkNum;
522ed2dbd31SArchie Cobbs 
523ed2dbd31SArchie Cobbs 			/* Get link number */
524ed2dbd31SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
525ed2dbd31SArchie Cobbs 				error = EINVAL;
526ed2dbd31SArchie Cobbs 				break;
527ed2dbd31SArchie Cobbs 			}
528f961caf2SLutz Donnerhacke 			linkNum = *((int32_t *)msg->data);
529f961caf2SLutz Donnerhacke 			if (linkNum < 0)
530631cabbaSGleb Smirnoff 				snprintf(linkName, sizeof(linkName),
531f961caf2SLutz Donnerhacke 				    "%s%u", NG_BRIDGE_HOOK_UPLINK_PREFIX, -linkNum);
532f961caf2SLutz Donnerhacke 			else
533f961caf2SLutz Donnerhacke 				snprintf(linkName, sizeof(linkName),
534f961caf2SLutz Donnerhacke 				    "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX, linkNum);
535631cabbaSGleb Smirnoff 
536631cabbaSGleb Smirnoff 			if ((hook = ng_findhook(node, linkName)) == NULL) {
537ed2dbd31SArchie Cobbs 				error = ENOTCONN;
538ed2dbd31SArchie Cobbs 				break;
539ed2dbd31SArchie Cobbs 			}
540631cabbaSGleb Smirnoff 			link = NG_HOOK_PRIVATE(hook);
541ed2dbd31SArchie Cobbs 
542ed2dbd31SArchie Cobbs 			/* Get/clear stats */
543ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
54466c72859SLutz Donnerhacke 				struct ng_bridge_link_stats *rs;
54566c72859SLutz Donnerhacke 
546ed2dbd31SArchie Cobbs 				NG_MKRESPONSE(resp, msg,
547ed2dbd31SArchie Cobbs 				    sizeof(link->stats), M_NOWAIT);
548ed2dbd31SArchie Cobbs 				if (resp == NULL) {
549ed2dbd31SArchie Cobbs 					error = ENOMEM;
550ed2dbd31SArchie Cobbs 					break;
551ed2dbd31SArchie Cobbs 				}
55266c72859SLutz Donnerhacke 				rs = (struct ng_bridge_link_stats *)resp->data;
55366c72859SLutz Donnerhacke #define FETCH(x)	rs->x = counter_u64_fetch(link->stats.x)
55466c72859SLutz Donnerhacke 				FETCH(recvOctets);
55566c72859SLutz Donnerhacke 				FETCH(recvPackets);
55666c72859SLutz Donnerhacke 				FETCH(recvMulticasts);
55766c72859SLutz Donnerhacke 				FETCH(recvBroadcasts);
55866c72859SLutz Donnerhacke 				FETCH(recvUnknown);
55966c72859SLutz Donnerhacke 				FETCH(recvRunts);
56066c72859SLutz Donnerhacke 				FETCH(recvInvalid);
56166c72859SLutz Donnerhacke 				FETCH(xmitOctets);
56266c72859SLutz Donnerhacke 				FETCH(xmitPackets);
56366c72859SLutz Donnerhacke 				FETCH(xmitMulticasts);
56466c72859SLutz Donnerhacke 				FETCH(xmitBroadcasts);
56566c72859SLutz Donnerhacke 				FETCH(loopDrops);
566f6e0c471SLutz Donnerhacke 				rs->loopDetects = link->stats.loopDetects;
56766c72859SLutz Donnerhacke 				FETCH(memoryFailures);
56866c72859SLutz Donnerhacke #undef FETCH
569ed2dbd31SArchie Cobbs 			}
570ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
57166c72859SLutz Donnerhacke 				ng_bridge_clear_link_stats(&link->stats);
572ed2dbd31SArchie Cobbs 			break;
573ed2dbd31SArchie Cobbs 		    }
574ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_TABLE:
575ed2dbd31SArchie Cobbs 		    {
576ed2dbd31SArchie Cobbs 			struct ng_bridge_host_ary *ary;
577ccf4cd2eSLutz Donnerhacke 			struct ng_bridge_host *host;
578ed2dbd31SArchie Cobbs 			int i = 0, bucket;
579ed2dbd31SArchie Cobbs 
580ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*ary)
581ed2dbd31SArchie Cobbs 			    + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
582ed2dbd31SArchie Cobbs 			if (resp == NULL) {
583ed2dbd31SArchie Cobbs 				error = ENOMEM;
584ed2dbd31SArchie Cobbs 				break;
585ed2dbd31SArchie Cobbs 			}
586ed2dbd31SArchie Cobbs 			ary = (struct ng_bridge_host_ary *)resp->data;
587ed2dbd31SArchie Cobbs 			ary->numHosts = priv->numHosts;
588ed2dbd31SArchie Cobbs 			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
589ccf4cd2eSLutz Donnerhacke 				SLIST_FOREACH(host, &priv->tab[bucket], next) {
590631cabbaSGleb Smirnoff 					memcpy(ary->hosts[i].addr,
591ccf4cd2eSLutz Donnerhacke 					       host->addr,
592631cabbaSGleb Smirnoff 					       sizeof(ary->hosts[i].addr));
593ccf4cd2eSLutz Donnerhacke 					ary->hosts[i].age       = host->age;
594ccf4cd2eSLutz Donnerhacke 					ary->hosts[i].staleness = host->staleness;
595631cabbaSGleb Smirnoff 					strncpy(ary->hosts[i].hook,
596ccf4cd2eSLutz Donnerhacke 						NG_HOOK_NAME(host->link->hook),
597631cabbaSGleb Smirnoff 						sizeof(ary->hosts[i].hook));
598631cabbaSGleb Smirnoff 					i++;
599631cabbaSGleb Smirnoff 				}
600ed2dbd31SArchie Cobbs 			}
601ed2dbd31SArchie Cobbs 			break;
602ed2dbd31SArchie Cobbs 		    }
603f8aab721SMarko Zec 		case NGM_BRIDGE_SET_PERSISTENT:
604f8aab721SMarko Zec 		    {
605f8aab721SMarko Zec 			priv->persistent = 1;
606f8aab721SMarko Zec 			break;
607f8aab721SMarko Zec 		    }
608b1bd4473SLutz Donnerhacke 		case NGM_BRIDGE_MOVE_HOST:
609b1bd4473SLutz Donnerhacke 		{
610b1bd4473SLutz Donnerhacke 			struct ng_bridge_move_host *mh;
611b1bd4473SLutz Donnerhacke 			hook_p hook;
612b1bd4473SLutz Donnerhacke 
613b1bd4473SLutz Donnerhacke 			if (msg->header.arglen < sizeof(*mh)) {
614b1bd4473SLutz Donnerhacke 				error = EINVAL;
615b1bd4473SLutz Donnerhacke 				break;
616b1bd4473SLutz Donnerhacke 			}
617b1bd4473SLutz Donnerhacke 			mh = (struct ng_bridge_move_host *)msg->data;
618b1bd4473SLutz Donnerhacke 			hook = (mh->hook[0] == 0)
619b1bd4473SLutz Donnerhacke 			    ? lasthook
620b1bd4473SLutz Donnerhacke 			    : ng_findhook(node, mh->hook);
621b1bd4473SLutz Donnerhacke 			if (hook == NULL) {
622b1bd4473SLutz Donnerhacke 				error = ENOENT;
623b1bd4473SLutz Donnerhacke 				break;
624b1bd4473SLutz Donnerhacke 			}
625b1bd4473SLutz Donnerhacke 			error = ng_bridge_put(priv, mh->addr, NG_HOOK_PRIVATE(hook));
626b1bd4473SLutz Donnerhacke 			break;
627b1bd4473SLutz Donnerhacke 		}
628ed2dbd31SArchie Cobbs 		default:
629ed2dbd31SArchie Cobbs 			error = EINVAL;
630ed2dbd31SArchie Cobbs 			break;
631ed2dbd31SArchie Cobbs 		}
632ed2dbd31SArchie Cobbs 		break;
633ed2dbd31SArchie Cobbs 	default:
634ed2dbd31SArchie Cobbs 		error = EINVAL;
635ed2dbd31SArchie Cobbs 		break;
636ed2dbd31SArchie Cobbs 	}
637ed2dbd31SArchie Cobbs 
638ed2dbd31SArchie Cobbs 	/* Done */
639069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
640069154d5SJulian Elischer 	NG_FREE_MSG(msg);
641ed2dbd31SArchie Cobbs 	return (error);
642ed2dbd31SArchie Cobbs }
643ed2dbd31SArchie Cobbs 
644ed2dbd31SArchie Cobbs /*
645ed2dbd31SArchie Cobbs  * Receive data on a hook
646ed2dbd31SArchie Cobbs  */
647631cabbaSGleb Smirnoff struct ng_bridge_send_ctx {
648631cabbaSGleb Smirnoff 	link_p foundFirst, incoming;
649631cabbaSGleb Smirnoff 	struct mbuf * m;
650631cabbaSGleb Smirnoff 	int manycast, error;
651631cabbaSGleb Smirnoff };
652631cabbaSGleb Smirnoff 
6533c958f5fSLutz Donnerhacke /*
6543c958f5fSLutz Donnerhacke  * Update stats and send out
6553c958f5fSLutz Donnerhacke  */
6563c958f5fSLutz Donnerhacke static inline int
6573c958f5fSLutz Donnerhacke ng_bridge_send_data(link_cp dst, int manycast, struct mbuf *m, item_p item) {
6583c958f5fSLutz Donnerhacke 	int error = 0;
6593c958f5fSLutz Donnerhacke 	size_t len = m->m_pkthdr.len;
6603c958f5fSLutz Donnerhacke 
6613c958f5fSLutz Donnerhacke 	if(item != NULL)
6623c958f5fSLutz Donnerhacke 		NG_FWD_NEW_DATA(error, item, dst->hook, m);
6633c958f5fSLutz Donnerhacke 	else
6643c958f5fSLutz Donnerhacke 		NG_SEND_DATA_ONLY(error, dst->hook, m);
6653c958f5fSLutz Donnerhacke 
666*a56e5ad6SLutz Donnerhacke 	if (error) {
667*a56e5ad6SLutz Donnerhacke 		/* The packet is still ours */
668*a56e5ad6SLutz Donnerhacke 		if (item != NULL)
669*a56e5ad6SLutz Donnerhacke 			NG_FREE_ITEM(item);
670*a56e5ad6SLutz Donnerhacke 		if (m != NULL)
671*a56e5ad6SLutz Donnerhacke 			NG_FREE_M(m);
672*a56e5ad6SLutz Donnerhacke 		return (error);
673*a56e5ad6SLutz Donnerhacke 	}
674*a56e5ad6SLutz Donnerhacke 
6753c958f5fSLutz Donnerhacke 	counter_u64_add(dst->stats.xmitPackets, 1);
6763c958f5fSLutz Donnerhacke 	counter_u64_add(dst->stats.xmitOctets, len);
6773c958f5fSLutz Donnerhacke 	switch (manycast) {
6783c958f5fSLutz Donnerhacke 	default:		       /* unknown unicast */
6793c958f5fSLutz Donnerhacke 		break;
6803c958f5fSLutz Donnerhacke 	case 1:			       /* multicast */
6813c958f5fSLutz Donnerhacke 		counter_u64_add(dst->stats.xmitMulticasts, 1);
6823c958f5fSLutz Donnerhacke 		break;
6833c958f5fSLutz Donnerhacke 	case 2:			       /* broadcast */
6843c958f5fSLutz Donnerhacke 		counter_u64_add(dst->stats.xmitBroadcasts, 1);
6853c958f5fSLutz Donnerhacke 		break;
6863c958f5fSLutz Donnerhacke 	}
687*a56e5ad6SLutz Donnerhacke 	return (0);
6883c958f5fSLutz Donnerhacke }
6893c958f5fSLutz Donnerhacke 
6903c958f5fSLutz Donnerhacke /*
6913c958f5fSLutz Donnerhacke  * Loop body for sending to multiple destinations
6923c958f5fSLutz Donnerhacke  * return 0 to stop looping
6933c958f5fSLutz Donnerhacke  */
694631cabbaSGleb Smirnoff static int
6950b951c55SGleb Smirnoff ng_bridge_send_ctx(hook_p dst, void *arg)
696631cabbaSGleb Smirnoff {
6970b951c55SGleb Smirnoff 	struct ng_bridge_send_ctx *ctx = arg;
698631cabbaSGleb Smirnoff 	link_p destLink = NG_HOOK_PRIVATE(dst);
699631cabbaSGleb Smirnoff 	struct mbuf *m2 = NULL;
700631cabbaSGleb Smirnoff 	int error = 0;
701631cabbaSGleb Smirnoff 
702631cabbaSGleb Smirnoff 	/* Skip incoming link */
703631cabbaSGleb Smirnoff 	if (destLink == ctx->incoming) {
704631cabbaSGleb Smirnoff 		return (1);
705631cabbaSGleb Smirnoff 	}
706631cabbaSGleb Smirnoff 
707f961caf2SLutz Donnerhacke 	/* Skip sending unknowns to undesired links  */
708f961caf2SLutz Donnerhacke 	if (!ctx->manycast && !destLink->sendUnknown)
709f961caf2SLutz Donnerhacke 		return (1);
710f961caf2SLutz Donnerhacke 
711631cabbaSGleb Smirnoff 	if (ctx->foundFirst == NULL) {
712631cabbaSGleb Smirnoff 		/*
713631cabbaSGleb Smirnoff 		 * This is the first usable link we have found.
714631cabbaSGleb Smirnoff 		 * Reserve it for the originals.
715631cabbaSGleb Smirnoff 		 * If we never find another we save a copy.
716631cabbaSGleb Smirnoff 		 */
717631cabbaSGleb Smirnoff 		ctx->foundFirst = destLink;
718631cabbaSGleb Smirnoff 		return (1);
719631cabbaSGleb Smirnoff 	}
720631cabbaSGleb Smirnoff 
721631cabbaSGleb Smirnoff 	/*
722631cabbaSGleb Smirnoff 	 * It's usable link but not the reserved (first) one.
723631cabbaSGleb Smirnoff 	 * Copy mbuf info for sending.
724631cabbaSGleb Smirnoff 	 */
725*a56e5ad6SLutz Donnerhacke 	m2 = m_dup(ctx->m, M_NOWAIT);
726631cabbaSGleb Smirnoff 	if (m2 == NULL) {
72766c72859SLutz Donnerhacke 		counter_u64_add(ctx->incoming->stats.memoryFailures, 1);
728631cabbaSGleb Smirnoff 		ctx->error = ENOBUFS;
729*a56e5ad6SLutz Donnerhacke 		return (0);	       /* abort loop, do not try again and again */
730631cabbaSGleb Smirnoff 	}
731631cabbaSGleb Smirnoff 
732631cabbaSGleb Smirnoff 	/* Send packet */
7333c958f5fSLutz Donnerhacke 	error = ng_bridge_send_data(destLink, ctx->manycast, m2, NULL);
734631cabbaSGleb Smirnoff 	if (error)
735631cabbaSGleb Smirnoff 	  ctx->error = error;
736631cabbaSGleb Smirnoff 	return (1);
737631cabbaSGleb Smirnoff }
738631cabbaSGleb Smirnoff 
739ed2dbd31SArchie Cobbs static int
740069154d5SJulian Elischer ng_bridge_rcvdata(hook_p hook, item_p item)
741ed2dbd31SArchie Cobbs {
74230400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
74330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
744ed2dbd31SArchie Cobbs 	struct ng_bridge_host *host;
745ed2dbd31SArchie Cobbs 	struct ether_header *eh;
746631cabbaSGleb Smirnoff 	struct ng_bridge_send_ctx ctx = { 0 };
747631cabbaSGleb Smirnoff 	hook_p ret;
748ed2dbd31SArchie Cobbs 
749631cabbaSGleb Smirnoff 	NGI_GET_M(item, ctx.m);
750ed2dbd31SArchie Cobbs 
751631cabbaSGleb Smirnoff 	ctx.incoming = NG_HOOK_PRIVATE(hook);
752ed2dbd31SArchie Cobbs 	/* Sanity check packet and pull up header */
753631cabbaSGleb Smirnoff 	if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) {
75466c72859SLutz Donnerhacke 		counter_u64_add(ctx.incoming->stats.recvRunts, 1);
755069154d5SJulian Elischer 		NG_FREE_ITEM(item);
756631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
757ed2dbd31SArchie Cobbs 		return (EINVAL);
758ed2dbd31SArchie Cobbs 	}
759631cabbaSGleb Smirnoff 	if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) {
76066c72859SLutz Donnerhacke 		counter_u64_add(ctx.incoming->stats.memoryFailures, 1);
761069154d5SJulian Elischer 		NG_FREE_ITEM(item);
762ed2dbd31SArchie Cobbs 		return (ENOBUFS);
763ed2dbd31SArchie Cobbs 	}
764631cabbaSGleb Smirnoff 	eh = mtod(ctx.m, struct ether_header *);
765ed2dbd31SArchie Cobbs 	if ((eh->ether_shost[0] & 1) != 0) {
76666c72859SLutz Donnerhacke 		counter_u64_add(ctx.incoming->stats.recvInvalid, 1);
767069154d5SJulian Elischer 		NG_FREE_ITEM(item);
768631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
769ed2dbd31SArchie Cobbs 		return (EINVAL);
770ed2dbd31SArchie Cobbs 	}
771ed2dbd31SArchie Cobbs 
772ed2dbd31SArchie Cobbs 	/* Is link disabled due to a loopback condition? */
773631cabbaSGleb Smirnoff 	if (ctx.incoming->loopCount != 0) {
77466c72859SLutz Donnerhacke 		counter_u64_add(ctx.incoming->stats.loopDrops, 1);
775069154d5SJulian Elischer 		NG_FREE_ITEM(item);
776631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
777f6e0c471SLutz Donnerhacke 		return (ELOOP);
778ed2dbd31SArchie Cobbs 	}
779ed2dbd31SArchie Cobbs 
780ed2dbd31SArchie Cobbs 	/* Update stats */
78166c72859SLutz Donnerhacke 	counter_u64_add(ctx.incoming->stats.recvPackets, 1);
78266c72859SLutz Donnerhacke 	counter_u64_add(ctx.incoming->stats.recvOctets, ctx.m->m_pkthdr.len);
783631cabbaSGleb Smirnoff 	if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) {
784ed2dbd31SArchie Cobbs 		if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
78566c72859SLutz Donnerhacke 			counter_u64_add(ctx.incoming->stats.recvBroadcasts, 1);
786631cabbaSGleb Smirnoff 			ctx.manycast = 2;
787ed2dbd31SArchie Cobbs 		} else
78866c72859SLutz Donnerhacke 			counter_u64_add(ctx.incoming->stats.recvMulticasts, 1);
789ed2dbd31SArchie Cobbs 	}
790ed2dbd31SArchie Cobbs 
791ed2dbd31SArchie Cobbs 	/* Look up packet's source Ethernet address in hashtable */
792f6e0c471SLutz Donnerhacke 	if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL)
793011b7317SLutz Donnerhacke 		/* Update time since last heard from this host.
794011b7317SLutz Donnerhacke 		 * This is safe without locking, because it's
795011b7317SLutz Donnerhacke 		 * the only operation during shared access.
796011b7317SLutz Donnerhacke 		 */
7974dfe70fdSLutz Donnerhacke 		if (__predict_false(host->staleness > 0))
798ed2dbd31SArchie Cobbs 			host->staleness = 0;
799ed2dbd31SArchie Cobbs 
800f6e0c471SLutz Donnerhacke 	if ((host == NULL && ctx.incoming->learnMac) ||
801f6e0c471SLutz Donnerhacke 	    (host != NULL && host->link != ctx.incoming)) {
802b1bd4473SLutz Donnerhacke 		struct ng_mesg *msg;
803b1bd4473SLutz Donnerhacke 		struct ng_bridge_move_host *mh;
804b1bd4473SLutz Donnerhacke 		int error = 0;
805b1bd4473SLutz Donnerhacke 
806b1bd4473SLutz Donnerhacke 		NG_MKMESSAGE(msg, NGM_BRIDGE_COOKIE, NGM_BRIDGE_MOVE_HOST,
807b1bd4473SLutz Donnerhacke 		    sizeof(*mh), M_NOWAIT);
808b1bd4473SLutz Donnerhacke 		if (msg == NULL) {
80966c72859SLutz Donnerhacke 			counter_u64_add(ctx.incoming->stats.memoryFailures, 1);
810069154d5SJulian Elischer 			NG_FREE_ITEM(item);
811631cabbaSGleb Smirnoff 			NG_FREE_M(ctx.m);
812ed2dbd31SArchie Cobbs 			return (ENOMEM);
813ed2dbd31SArchie Cobbs 		}
814b1bd4473SLutz Donnerhacke 		mh = (struct ng_bridge_move_host *)msg->data;
815b1bd4473SLutz Donnerhacke 		strncpy(mh->hook, NG_HOOK_NAME(ctx.incoming->hook),
816b1bd4473SLutz Donnerhacke 		    sizeof(mh->hook));
817b1bd4473SLutz Donnerhacke 		memcpy(mh->addr, eh->ether_shost, sizeof(mh->addr));
818b1bd4473SLutz Donnerhacke 		NG_SEND_MSG_ID(error, node, msg, NG_NODE_ID(node),
819b1bd4473SLutz Donnerhacke 		    NG_NODE_ID(node));
820b1bd4473SLutz Donnerhacke 		if (error)
821b1bd4473SLutz Donnerhacke 			counter_u64_add(ctx.incoming->stats.memoryFailures, 1);
822ed2dbd31SArchie Cobbs 	}
823ed2dbd31SArchie Cobbs 
824f6e0c471SLutz Donnerhacke 	if (host != NULL && host->link != ctx.incoming) {
825f6e0c471SLutz Donnerhacke 		if (host->age < priv->conf.minStableAge) {
826f6e0c471SLutz Donnerhacke 			/* Drop packet on instable links */
827f6e0c471SLutz Donnerhacke 			counter_u64_add(ctx.incoming->stats.loopDrops, 1);
828f6e0c471SLutz Donnerhacke 			NG_FREE_ITEM(item);
829f6e0c471SLutz Donnerhacke 			NG_FREE_M(ctx.m);
830f6e0c471SLutz Donnerhacke 			return (ELOOP);
831f6e0c471SLutz Donnerhacke 		}
832f6e0c471SLutz Donnerhacke 	}
833f6e0c471SLutz Donnerhacke 
834ed2dbd31SArchie Cobbs 	/* Run packet through ipfw processing, if enabled */
8359b932e9eSAndre Oppermann #if 0
8360b4b0b0fSJulian Elischer 	if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) {
837ed2dbd31SArchie Cobbs 		/* XXX not implemented yet */
838ed2dbd31SArchie Cobbs 	}
8399b932e9eSAndre Oppermann #endif
840ed2dbd31SArchie Cobbs 
841ed2dbd31SArchie Cobbs 	/*
842ed2dbd31SArchie Cobbs 	 * If unicast and destination host known, deliver to host's link,
843ed2dbd31SArchie Cobbs 	 * unless it is the same link as the packet came in on.
844ed2dbd31SArchie Cobbs 	 */
845631cabbaSGleb Smirnoff 	if (!ctx.manycast) {
846ed2dbd31SArchie Cobbs 		/* Determine packet destination link */
847ed2dbd31SArchie Cobbs 		if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
848631cabbaSGleb Smirnoff 			link_p destLink = host->link;
849ed2dbd31SArchie Cobbs 
850ed2dbd31SArchie Cobbs 			/* If destination same as incoming link, do nothing */
851631cabbaSGleb Smirnoff 			if (destLink == ctx.incoming) {
852069154d5SJulian Elischer 				NG_FREE_ITEM(item);
853631cabbaSGleb Smirnoff 				NG_FREE_M(ctx.m);
854ed2dbd31SArchie Cobbs 				return (0);
855ed2dbd31SArchie Cobbs 			}
856ed2dbd31SArchie Cobbs 
857ed2dbd31SArchie Cobbs 			/* Deliver packet out the destination link */
8583c958f5fSLutz Donnerhacke 			return (ng_bridge_send_data(destLink, ctx.manycast, ctx.m, item));
859ed2dbd31SArchie Cobbs 		}
860ed2dbd31SArchie Cobbs 
861ed2dbd31SArchie Cobbs 		/* Destination host is not known */
86266c72859SLutz Donnerhacke 		counter_u64_add(ctx.incoming->stats.recvUnknown, 1);
863ed2dbd31SArchie Cobbs 	}
864ed2dbd31SArchie Cobbs 
865ed2dbd31SArchie Cobbs 	/* Distribute unknown, multicast, broadcast pkts to all other links */
866631cabbaSGleb Smirnoff 	NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx, ret);
867ed2dbd31SArchie Cobbs 
868*a56e5ad6SLutz Donnerhacke 	/* Finally send out on the first link found */
869*a56e5ad6SLutz Donnerhacke 	if (ctx.foundFirst != NULL) {
870*a56e5ad6SLutz Donnerhacke 		int error = ng_bridge_send_data(ctx.foundFirst, ctx.manycast, ctx.m, item);
871*a56e5ad6SLutz Donnerhacke 		if (error)
872*a56e5ad6SLutz Donnerhacke 			ctx.error = error;
873*a56e5ad6SLutz Donnerhacke 	} else {		       /* nothing to send at all */
874069154d5SJulian Elischer 		NG_FREE_ITEM(item);
875631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
876069154d5SJulian Elischer 	}
877069154d5SJulian Elischer 
878*a56e5ad6SLutz Donnerhacke 	return (ctx.error);
879ed2dbd31SArchie Cobbs }
880ed2dbd31SArchie Cobbs 
881ed2dbd31SArchie Cobbs /*
882ed2dbd31SArchie Cobbs  * Shutdown node
883ed2dbd31SArchie Cobbs  */
884ed2dbd31SArchie Cobbs static int
885069154d5SJulian Elischer ng_bridge_shutdown(node_p node)
886ed2dbd31SArchie Cobbs {
88730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
888ed2dbd31SArchie Cobbs 
8896c12c2b1SArchie Cobbs 	/*
890195cf617SRuslan Ermilov 	 * Shut down everything including the timer.  Even if the
891195cf617SRuslan Ermilov 	 * callout has already been dequeued and is about to be
892195cf617SRuslan Ermilov 	 * run, ng_bridge_timeout() won't be fired as the node
893195cf617SRuslan Ermilov 	 * is already marked NGF_INVALID, so we're safe to free
894195cf617SRuslan Ermilov 	 * the node now.
8956c12c2b1SArchie Cobbs 	 */
896ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
897ed2dbd31SArchie Cobbs 	    ("%s: numLinks=%d numHosts=%d",
8986e551fb6SDavid E. O'Brien 	    __func__, priv->numLinks, priv->numHosts));
899195cf617SRuslan Ermilov 	ng_uncallout(&priv->timer, node);
900195cf617SRuslan Ermilov 	NG_NODE_SET_PRIVATE(node, NULL);
901195cf617SRuslan Ermilov 	NG_NODE_UNREF(node);
9021ede983cSDag-Erling Smørgrav 	free(priv->tab, M_NETGRAPH_BRIDGE);
9031ede983cSDag-Erling Smørgrav 	free(priv, M_NETGRAPH_BRIDGE);
904ed2dbd31SArchie Cobbs 	return (0);
905ed2dbd31SArchie Cobbs }
906ed2dbd31SArchie Cobbs 
907ed2dbd31SArchie Cobbs /*
908ed2dbd31SArchie Cobbs  * Hook disconnection.
909ed2dbd31SArchie Cobbs  */
910ed2dbd31SArchie Cobbs static int
911ed2dbd31SArchie Cobbs ng_bridge_disconnect(hook_p hook)
912ed2dbd31SArchie Cobbs {
91330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
914631cabbaSGleb Smirnoff 	link_p link = NG_HOOK_PRIVATE(hook);
915ed2dbd31SArchie Cobbs 
916ed2dbd31SArchie Cobbs 	/* Remove all hosts associated with this link */
917631cabbaSGleb Smirnoff 	ng_bridge_remove_hosts(priv, link);
918ed2dbd31SArchie Cobbs 
919ed2dbd31SArchie Cobbs 	/* Free associated link information */
92066c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvOctets);
92166c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvPackets);
92266c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvMulticasts);
92366c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvBroadcasts);
92466c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvUnknown);
92566c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvRunts);
92666c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvInvalid);
92766c72859SLutz Donnerhacke 	counter_u64_free(link->stats.xmitOctets);
92866c72859SLutz Donnerhacke 	counter_u64_free(link->stats.xmitPackets);
92966c72859SLutz Donnerhacke 	counter_u64_free(link->stats.xmitMulticasts);
93066c72859SLutz Donnerhacke 	counter_u64_free(link->stats.xmitBroadcasts);
93166c72859SLutz Donnerhacke 	counter_u64_free(link->stats.loopDrops);
93266c72859SLutz Donnerhacke 	counter_u64_free(link->stats.memoryFailures);
933631cabbaSGleb Smirnoff 	free(link, M_NETGRAPH_BRIDGE);
934ed2dbd31SArchie Cobbs 	priv->numLinks--;
935ed2dbd31SArchie Cobbs 
936ed2dbd31SArchie Cobbs 	/* If no more hooks, go away */
93730400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
938f8aab721SMarko Zec 	    && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
939f8aab721SMarko Zec 	    && !priv->persistent) {
94030400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));
94130400f03SJulian Elischer 	}
942ed2dbd31SArchie Cobbs 	return (0);
943ed2dbd31SArchie Cobbs }
944ed2dbd31SArchie Cobbs 
945ed2dbd31SArchie Cobbs /******************************************************************
946ed2dbd31SArchie Cobbs 		    HASH TABLE FUNCTIONS
947ed2dbd31SArchie Cobbs ******************************************************************/
948ed2dbd31SArchie Cobbs 
949ed2dbd31SArchie Cobbs /*
950ed2dbd31SArchie Cobbs  * Hash algorithm
951ed2dbd31SArchie Cobbs  */
952ed2dbd31SArchie Cobbs #define HASH(addr,mask)		( (((const u_int16_t *)(addr))[0] 	\
953ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[1] 	\
954ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
955ed2dbd31SArchie Cobbs 
956ed2dbd31SArchie Cobbs /*
957ed2dbd31SArchie Cobbs  * Find a host entry in the table.
958ed2dbd31SArchie Cobbs  */
959ed2dbd31SArchie Cobbs static struct ng_bridge_host *
9606117aa58SLutz Donnerhacke ng_bridge_get(priv_cp priv, const u_char *addr)
961ed2dbd31SArchie Cobbs {
962ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
963ccf4cd2eSLutz Donnerhacke 	struct ng_bridge_host *host;
964ed2dbd31SArchie Cobbs 
965ccf4cd2eSLutz Donnerhacke 	SLIST_FOREACH(host, &priv->tab[bucket], next) {
966ccf4cd2eSLutz Donnerhacke 		if (ETHER_EQUAL(host->addr, addr))
967ccf4cd2eSLutz Donnerhacke 			return (host);
968ed2dbd31SArchie Cobbs 	}
969ed2dbd31SArchie Cobbs 	return (NULL);
970ed2dbd31SArchie Cobbs }
971ed2dbd31SArchie Cobbs 
972ed2dbd31SArchie Cobbs /*
973f6e0c471SLutz Donnerhacke  * Add a host entry to the table. If it already exists, move it
974f6e0c471SLutz Donnerhacke  * to the new link. Returns 0 on success.
975ed2dbd31SArchie Cobbs  */
976ed2dbd31SArchie Cobbs static int
977631cabbaSGleb Smirnoff ng_bridge_put(priv_p priv, const u_char *addr, link_p link)
978ed2dbd31SArchie Cobbs {
979ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
980ccf4cd2eSLutz Donnerhacke 	struct ng_bridge_host *host;
981ed2dbd31SArchie Cobbs 
982f6e0c471SLutz Donnerhacke 	if ((host = ng_bridge_get(priv, addr)) != NULL) {
983f6e0c471SLutz Donnerhacke 		/* Host already on the correct link? */
984f6e0c471SLutz Donnerhacke 		if (host->link == link)
985f6e0c471SLutz Donnerhacke 			return 0;
986f6e0c471SLutz Donnerhacke 
987f6e0c471SLutz Donnerhacke 		/* Move old host over to new link */
988f6e0c471SLutz Donnerhacke 		if (host->age >= priv->conf.minStableAge) {
989f6e0c471SLutz Donnerhacke 			host->link = link;
990f6e0c471SLutz Donnerhacke 			host->age = 0;
991f6e0c471SLutz Donnerhacke 			return (0);
992f6e0c471SLutz Donnerhacke 		}
993f6e0c471SLutz Donnerhacke 		/*
994f6e0c471SLutz Donnerhacke 		 * If the host was recently moved to the old link and
995f6e0c471SLutz Donnerhacke 		 * it's now jumping to a new link, declare a loopback
996f6e0c471SLutz Donnerhacke 		 * condition.
997f6e0c471SLutz Donnerhacke 		 */
998f6e0c471SLutz Donnerhacke 		if (priv->conf.debugLevel >= 2)
999f6e0c471SLutz Donnerhacke 		    log(LOG_WARNING, "ng_bridge: %s:"
1000f6e0c471SLutz Donnerhacke 			" loopback detected on %s\n",
1001f6e0c471SLutz Donnerhacke 			ng_bridge_nodename(priv->node),
1002f6e0c471SLutz Donnerhacke 			NG_HOOK_NAME(link->hook));
1003f6e0c471SLutz Donnerhacke 
1004f6e0c471SLutz Donnerhacke 		/* Mark link as linka non grata */
1005f6e0c471SLutz Donnerhacke 		link->loopCount = priv->conf.loopTimeout;
1006f6e0c471SLutz Donnerhacke 		link->stats.loopDetects++;
1007f6e0c471SLutz Donnerhacke 
1008f6e0c471SLutz Donnerhacke 		/* Forget all hosts on this link */
1009f6e0c471SLutz Donnerhacke 		ng_bridge_remove_hosts(priv, link);
1010f6e0c471SLutz Donnerhacke 		return (ELOOP);
1011f6e0c471SLutz Donnerhacke 	}
1012ed2dbd31SArchie Cobbs 
1013ed2dbd31SArchie Cobbs 	/* Allocate and initialize new hashtable entry */
1014ccf4cd2eSLutz Donnerhacke 	host = malloc(sizeof(*host), M_NETGRAPH_BRIDGE, M_NOWAIT);
1015ccf4cd2eSLutz Donnerhacke 	if (host == NULL)
1016b1bd4473SLutz Donnerhacke 		return (ENOMEM);
1017ccf4cd2eSLutz Donnerhacke 	bcopy(addr, host->addr, ETHER_ADDR_LEN);
1018ccf4cd2eSLutz Donnerhacke 	host->link = link;
1019ccf4cd2eSLutz Donnerhacke 	host->staleness = 0;
1020ccf4cd2eSLutz Donnerhacke 	host->age = 0;
1021ed2dbd31SArchie Cobbs 
1022ed2dbd31SArchie Cobbs 	/* Add new element to hash bucket */
1023ccf4cd2eSLutz Donnerhacke 	SLIST_INSERT_HEAD(&priv->tab[bucket], host, next);
1024ed2dbd31SArchie Cobbs 	priv->numHosts++;
1025ed2dbd31SArchie Cobbs 
1026ed2dbd31SArchie Cobbs 	/* Resize table if necessary */
1027ed2dbd31SArchie Cobbs 	ng_bridge_rehash(priv);
1028b1bd4473SLutz Donnerhacke 	return (0);
1029ed2dbd31SArchie Cobbs }
1030ed2dbd31SArchie Cobbs 
1031ed2dbd31SArchie Cobbs /*
1032ed2dbd31SArchie Cobbs  * Resize the hash table. We try to maintain the number of buckets
1033ed2dbd31SArchie Cobbs  * such that the load factor is in the range 0.25 to 1.0.
1034ed2dbd31SArchie Cobbs  *
1035ed2dbd31SArchie Cobbs  * If we can't get the new memory then we silently fail. This is OK
1036ed2dbd31SArchie Cobbs  * because things will still work and we'll try again soon anyway.
1037ed2dbd31SArchie Cobbs  */
1038ed2dbd31SArchie Cobbs static void
1039ed2dbd31SArchie Cobbs ng_bridge_rehash(priv_p priv)
1040ed2dbd31SArchie Cobbs {
1041ed2dbd31SArchie Cobbs 	struct ng_bridge_bucket *newTab;
1042ed2dbd31SArchie Cobbs 	int oldBucket, newBucket;
1043ed2dbd31SArchie Cobbs 	int newNumBuckets;
1044ed2dbd31SArchie Cobbs 	u_int newMask;
1045ed2dbd31SArchie Cobbs 
1046ed2dbd31SArchie Cobbs 	/* Is table too full or too empty? */
1047ed2dbd31SArchie Cobbs 	if (priv->numHosts > priv->numBuckets
1048ed2dbd31SArchie Cobbs 	    && (priv->numBuckets << 1) <= MAX_BUCKETS)
1049ed2dbd31SArchie Cobbs 		newNumBuckets = priv->numBuckets << 1;
1050ed2dbd31SArchie Cobbs 	else if (priv->numHosts < (priv->numBuckets >> 2)
1051ed2dbd31SArchie Cobbs 	    && (priv->numBuckets >> 2) >= MIN_BUCKETS)
1052ed2dbd31SArchie Cobbs 		newNumBuckets = priv->numBuckets >> 2;
1053ed2dbd31SArchie Cobbs 	else
1054ed2dbd31SArchie Cobbs 		return;
1055ed2dbd31SArchie Cobbs 	newMask = newNumBuckets - 1;
1056ed2dbd31SArchie Cobbs 
1057ed2dbd31SArchie Cobbs 	/* Allocate and initialize new table */
1058ac2fffa4SPedro F. Giffuni 	newTab = malloc(newNumBuckets * sizeof(*newTab),
1059e11e3f18SDag-Erling Smørgrav 	    M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
1060ed2dbd31SArchie Cobbs 	if (newTab == NULL)
1061ed2dbd31SArchie Cobbs 		return;
1062ed2dbd31SArchie Cobbs 
1063ed2dbd31SArchie Cobbs 	/* Move all entries from old table to new table */
1064ed2dbd31SArchie Cobbs 	for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
1065ed2dbd31SArchie Cobbs 		struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
1066ed2dbd31SArchie Cobbs 
1067ed2dbd31SArchie Cobbs 		while (!SLIST_EMPTY(oldList)) {
1068ccf4cd2eSLutz Donnerhacke 			struct ng_bridge_host *const host
1069ed2dbd31SArchie Cobbs 			    = SLIST_FIRST(oldList);
1070ed2dbd31SArchie Cobbs 
1071ed2dbd31SArchie Cobbs 			SLIST_REMOVE_HEAD(oldList, next);
1072ccf4cd2eSLutz Donnerhacke 			newBucket = HASH(host->addr, newMask);
1073ccf4cd2eSLutz Donnerhacke 			SLIST_INSERT_HEAD(&newTab[newBucket], host, next);
1074ed2dbd31SArchie Cobbs 		}
1075ed2dbd31SArchie Cobbs 	}
1076ed2dbd31SArchie Cobbs 
1077ed2dbd31SArchie Cobbs 	/* Replace old table with new one */
1078ed2dbd31SArchie Cobbs 	if (priv->conf.debugLevel >= 3) {
1079ed2dbd31SArchie Cobbs 		log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
1080ed2dbd31SArchie Cobbs 		    ng_bridge_nodename(priv->node),
1081ed2dbd31SArchie Cobbs 		    priv->numBuckets, newNumBuckets);
1082ed2dbd31SArchie Cobbs 	}
10831ede983cSDag-Erling Smørgrav 	free(priv->tab, M_NETGRAPH_BRIDGE);
1084ed2dbd31SArchie Cobbs 	priv->numBuckets = newNumBuckets;
1085ed2dbd31SArchie Cobbs 	priv->hashMask = newMask;
1086ed2dbd31SArchie Cobbs 	priv->tab = newTab;
1087ed2dbd31SArchie Cobbs 	return;
1088ed2dbd31SArchie Cobbs }
1089ed2dbd31SArchie Cobbs 
1090ed2dbd31SArchie Cobbs /******************************************************************
1091ed2dbd31SArchie Cobbs 		    MISC FUNCTIONS
1092ed2dbd31SArchie Cobbs ******************************************************************/
1093ed2dbd31SArchie Cobbs 
1094ed2dbd31SArchie Cobbs /*
1095ed2dbd31SArchie Cobbs  * Remove all hosts associated with a specific link from the hashtable.
1096ed2dbd31SArchie Cobbs  * If linkNum == -1, then remove all hosts in the table.
1097ed2dbd31SArchie Cobbs  */
1098ed2dbd31SArchie Cobbs static void
1099631cabbaSGleb Smirnoff ng_bridge_remove_hosts(priv_p priv, link_p link)
1100ed2dbd31SArchie Cobbs {
1101ed2dbd31SArchie Cobbs 	int bucket;
1102ed2dbd31SArchie Cobbs 
1103ed2dbd31SArchie Cobbs 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1104ccf4cd2eSLutz Donnerhacke 		struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1105ed2dbd31SArchie Cobbs 
1106ed2dbd31SArchie Cobbs 		while (*hptr != NULL) {
1107ccf4cd2eSLutz Donnerhacke 			struct ng_bridge_host *const host = *hptr;
1108ed2dbd31SArchie Cobbs 
1109ccf4cd2eSLutz Donnerhacke 			if (link == NULL || host->link == link) {
1110ccf4cd2eSLutz Donnerhacke 				*hptr = SLIST_NEXT(host, next);
1111ccf4cd2eSLutz Donnerhacke 				free(host, M_NETGRAPH_BRIDGE);
1112ed2dbd31SArchie Cobbs 				priv->numHosts--;
1113ed2dbd31SArchie Cobbs 			} else
1114ccf4cd2eSLutz Donnerhacke 				hptr = &SLIST_NEXT(host, next);
1115ed2dbd31SArchie Cobbs 		}
1116ed2dbd31SArchie Cobbs 	}
1117ed2dbd31SArchie Cobbs }
1118ed2dbd31SArchie Cobbs 
1119ed2dbd31SArchie Cobbs /*
1120ed2dbd31SArchie Cobbs  * Handle our once-per-second timeout event. We do two things:
1121ed2dbd31SArchie Cobbs  * we decrement link->loopCount for those links being muted due to
1122ed2dbd31SArchie Cobbs  * a detected loopback condition, and we remove any hosts from
1123ed2dbd31SArchie Cobbs  * the hashtable whom we haven't heard from in a long while.
1124ed2dbd31SArchie Cobbs  */
1125631cabbaSGleb Smirnoff static int
11260b951c55SGleb Smirnoff ng_bridge_unmute(hook_p hook, void *arg)
1127631cabbaSGleb Smirnoff {
1128631cabbaSGleb Smirnoff 	link_p link = NG_HOOK_PRIVATE(hook);
1129631cabbaSGleb Smirnoff 	node_p node = NG_HOOK_NODE(hook);
1130631cabbaSGleb Smirnoff 	priv_p priv = NG_NODE_PRIVATE(node);
11310b951c55SGleb Smirnoff 	int *counter = arg;
1132631cabbaSGleb Smirnoff 
1133631cabbaSGleb Smirnoff 	if (link->loopCount != 0) {
1134631cabbaSGleb Smirnoff 		link->loopCount--;
1135631cabbaSGleb Smirnoff 		if (link->loopCount == 0 && priv->conf.debugLevel >= 2) {
1136631cabbaSGleb Smirnoff 			log(LOG_INFO, "ng_bridge: %s:"
1137631cabbaSGleb Smirnoff 			    " restoring looped back %s\n",
1138631cabbaSGleb Smirnoff 			    ng_bridge_nodename(node), NG_HOOK_NAME(hook));
1139631cabbaSGleb Smirnoff 		}
1140631cabbaSGleb Smirnoff 	}
1141abc4b11cSGleb Smirnoff 	(*counter)++;
1142631cabbaSGleb Smirnoff 	return (1);
1143631cabbaSGleb Smirnoff }
1144631cabbaSGleb Smirnoff 
1145ed2dbd31SArchie Cobbs static void
1146e0d32af7SGleb Smirnoff ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1147ed2dbd31SArchie Cobbs {
114830400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1149e0d32af7SGleb Smirnoff 	int bucket;
1150ed2dbd31SArchie Cobbs 	int counter = 0;
1151631cabbaSGleb Smirnoff 	hook_p ret;
1152ed2dbd31SArchie Cobbs 
1153ed2dbd31SArchie Cobbs 	/* Update host time counters and remove stale entries */
1154ed2dbd31SArchie Cobbs 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1155ccf4cd2eSLutz Donnerhacke 		struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1156ed2dbd31SArchie Cobbs 
1157ed2dbd31SArchie Cobbs 		while (*hptr != NULL) {
1158ccf4cd2eSLutz Donnerhacke 			struct ng_bridge_host *const host = *hptr;
1159ed2dbd31SArchie Cobbs 
1160ed2dbd31SArchie Cobbs 			/* Remove hosts we haven't heard from in a while */
1161ccf4cd2eSLutz Donnerhacke 			if (++host->staleness >= priv->conf.maxStaleness) {
1162ccf4cd2eSLutz Donnerhacke 				*hptr = SLIST_NEXT(host, next);
1163ccf4cd2eSLutz Donnerhacke 				free(host, M_NETGRAPH_BRIDGE);
1164ed2dbd31SArchie Cobbs 				priv->numHosts--;
1165ed2dbd31SArchie Cobbs 			} else {
1166ccf4cd2eSLutz Donnerhacke 				if (host->age < 0xffff)
1167ccf4cd2eSLutz Donnerhacke 					host->age++;
1168ccf4cd2eSLutz Donnerhacke 				hptr = &SLIST_NEXT(host, next);
1169ed2dbd31SArchie Cobbs 				counter++;
1170ed2dbd31SArchie Cobbs 			}
1171ed2dbd31SArchie Cobbs 		}
1172ed2dbd31SArchie Cobbs 	}
1173ed2dbd31SArchie Cobbs 	KASSERT(priv->numHosts == counter,
11746e551fb6SDavid E. O'Brien 	    ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
1175ed2dbd31SArchie Cobbs 
1176ed2dbd31SArchie Cobbs 	/* Decrease table size if necessary */
1177ed2dbd31SArchie Cobbs 	ng_bridge_rehash(priv);
1178ed2dbd31SArchie Cobbs 
1179ed2dbd31SArchie Cobbs 	/* Decrease loop counter on muted looped back links */
1180631cabbaSGleb Smirnoff 	counter = 0;
1181631cabbaSGleb Smirnoff 	NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter, ret);
1182ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == counter,
11836e551fb6SDavid E. O'Brien 	    ("%s: links: %d != %d", __func__, priv->numLinks, counter));
1184ed2dbd31SArchie Cobbs 
1185e0d32af7SGleb Smirnoff 	/* Register a new timeout, keeping the existing node reference */
1186e0d32af7SGleb Smirnoff 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
1187ed2dbd31SArchie Cobbs }
1188ed2dbd31SArchie Cobbs 
1189ed2dbd31SArchie Cobbs /*
1190ed2dbd31SArchie Cobbs  * Return node's "name", even if it doesn't have one.
1191ed2dbd31SArchie Cobbs  */
1192ed2dbd31SArchie Cobbs static const char *
11936117aa58SLutz Donnerhacke ng_bridge_nodename(node_cp node)
1194ed2dbd31SArchie Cobbs {
119587e2c66aSHartmut Brandt 	static char name[NG_NODESIZ];
1196ed2dbd31SArchie Cobbs 
1197dd10c1e2SGleb Smirnoff 	if (NG_NODE_HAS_NAME(node))
119830400f03SJulian Elischer 		snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
1199ed2dbd31SArchie Cobbs 	else
1200ed2dbd31SArchie Cobbs 		snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
1201ed2dbd31SArchie Cobbs 	return name;
1202ed2dbd31SArchie Cobbs }
1203