xref: /freebsd/sys/netgraph/ng_bridge.c (revision 011b7317dbb5038a95b9b4fca050325a62f3991e)
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 */
10666c72859SLutz Donnerhacke 	counter_u64_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 };
237ed2dbd31SArchie Cobbs 
238ed2dbd31SArchie Cobbs /* List of commands and how to convert arguments to/from ASCII */
239ed2dbd31SArchie Cobbs static const struct ng_cmdlist ng_bridge_cmdlist[] = {
240ed2dbd31SArchie Cobbs 	{
241ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
242ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_SET_CONFIG,
243ed2dbd31SArchie Cobbs 	  "setconfig",
244ed2dbd31SArchie Cobbs 	  &ng_bridge_config_type,
245ed2dbd31SArchie Cobbs 	  NULL
246ed2dbd31SArchie Cobbs 	},
247ed2dbd31SArchie Cobbs 	{
248ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
249ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_CONFIG,
250ed2dbd31SArchie Cobbs 	  "getconfig",
251ed2dbd31SArchie Cobbs 	  NULL,
252ed2dbd31SArchie Cobbs 	  &ng_bridge_config_type
253ed2dbd31SArchie Cobbs 	},
254ed2dbd31SArchie Cobbs 	{
255ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
256ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_RESET,
257ed2dbd31SArchie Cobbs 	  "reset",
258ed2dbd31SArchie Cobbs 	  NULL,
259ed2dbd31SArchie Cobbs 	  NULL
260ed2dbd31SArchie Cobbs 	},
261ed2dbd31SArchie Cobbs 	{
262ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
263ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_STATS,
264ed2dbd31SArchie Cobbs 	  "getstats",
265ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
266ed2dbd31SArchie Cobbs 	  &ng_bridge_stats_type
267ed2dbd31SArchie Cobbs 	},
268ed2dbd31SArchie Cobbs 	{
269ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
270ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_CLR_STATS,
271ed2dbd31SArchie Cobbs 	  "clrstats",
272ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
273ed2dbd31SArchie Cobbs 	  NULL
274ed2dbd31SArchie Cobbs 	},
275ed2dbd31SArchie Cobbs 	{
276ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
277ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GETCLR_STATS,
278ed2dbd31SArchie Cobbs 	  "getclrstats",
279ed2dbd31SArchie Cobbs 	  &ng_parse_uint32_type,
280ed2dbd31SArchie Cobbs 	  &ng_bridge_stats_type
281ed2dbd31SArchie Cobbs 	},
282ed2dbd31SArchie Cobbs 	{
283ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_COOKIE,
284ed2dbd31SArchie Cobbs 	  NGM_BRIDGE_GET_TABLE,
285ed2dbd31SArchie Cobbs 	  "gettable",
286ed2dbd31SArchie Cobbs 	  NULL,
287ed2dbd31SArchie Cobbs 	  &ng_bridge_host_ary_type
288ed2dbd31SArchie Cobbs 	},
289f8aab721SMarko Zec 	{
290f8aab721SMarko Zec 	  NGM_BRIDGE_COOKIE,
291f8aab721SMarko Zec 	  NGM_BRIDGE_SET_PERSISTENT,
292f8aab721SMarko Zec 	  "setpersistent",
293f8aab721SMarko Zec 	  NULL,
294f8aab721SMarko Zec 	  NULL
295f8aab721SMarko Zec 	},
296ed2dbd31SArchie Cobbs 	{ 0 }
297ed2dbd31SArchie Cobbs };
298ed2dbd31SArchie Cobbs 
299ed2dbd31SArchie Cobbs /* Node type descriptor */
300ed2dbd31SArchie Cobbs static struct ng_type ng_bridge_typestruct = {
301f8aae777SJulian Elischer 	.version =	NG_ABI_VERSION,
302f8aae777SJulian Elischer 	.name =		NG_BRIDGE_NODE_TYPE,
303f8aae777SJulian Elischer 	.constructor =	ng_bridge_constructor,
304f8aae777SJulian Elischer 	.rcvmsg =	ng_bridge_rcvmsg,
305f8aae777SJulian Elischer 	.shutdown =	ng_bridge_shutdown,
306f8aae777SJulian Elischer 	.newhook =	ng_bridge_newhook,
307f8aae777SJulian Elischer 	.rcvdata =	ng_bridge_rcvdata,
308f8aae777SJulian Elischer 	.disconnect =	ng_bridge_disconnect,
309f8aae777SJulian Elischer 	.cmdlist =	ng_bridge_cmdlist,
310ed2dbd31SArchie Cobbs };
311a89effcdSArchie Cobbs NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
312ed2dbd31SArchie Cobbs 
313ed2dbd31SArchie Cobbs /******************************************************************
314ed2dbd31SArchie Cobbs 		    NETGRAPH NODE METHODS
315ed2dbd31SArchie Cobbs ******************************************************************/
316ed2dbd31SArchie Cobbs 
317ed2dbd31SArchie Cobbs /*
318ed2dbd31SArchie Cobbs  * Node constructor
319ed2dbd31SArchie Cobbs  */
320ed2dbd31SArchie Cobbs static int
321069154d5SJulian Elischer ng_bridge_constructor(node_p node)
322ed2dbd31SArchie Cobbs {
323ed2dbd31SArchie Cobbs 	priv_p priv;
324ed2dbd31SArchie Cobbs 
325ed2dbd31SArchie Cobbs 	/* Allocate and initialize private info */
326674d86bfSGleb Smirnoff 	priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
327e0d32af7SGleb Smirnoff 	ng_callout_init(&priv->timer);
328ed2dbd31SArchie Cobbs 
329ed2dbd31SArchie Cobbs 	/* Allocate and initialize hash table, etc. */
330e11e3f18SDag-Erling Smørgrav 	priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab),
331674d86bfSGleb Smirnoff 	    M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
332ed2dbd31SArchie Cobbs 	priv->numBuckets = MIN_BUCKETS;
333ed2dbd31SArchie Cobbs 	priv->hashMask = MIN_BUCKETS - 1;
334ed2dbd31SArchie Cobbs 	priv->conf.debugLevel = 1;
335ed2dbd31SArchie Cobbs 	priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
336ed2dbd31SArchie Cobbs 	priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
337ed2dbd31SArchie Cobbs 	priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
338c869d905SLutz Donnerhacke 	priv->sendUnknown = 1;	       /* classic bridge */
339ed2dbd31SArchie Cobbs 
340069154d5SJulian Elischer 	/*
341069154d5SJulian Elischer 	 * This node has all kinds of stuff that could be screwed by SMP.
342069154d5SJulian Elischer 	 * Until it gets it's own internal protection, we go through in
343053359b7SPedro F. Giffuni 	 * single file. This could hurt a machine bridging between two
344069154d5SJulian Elischer 	 * GB ethernets so it should be fixed.
345069154d5SJulian Elischer 	 * When it's fixed the process SHOULD NOT SLEEP, spinlocks please!
346069154d5SJulian Elischer 	 * (and atomic ops )
347069154d5SJulian Elischer 	 */
34830400f03SJulian Elischer 	NG_NODE_FORCE_WRITER(node);
34930400f03SJulian Elischer 	NG_NODE_SET_PRIVATE(node, priv);
350069154d5SJulian Elischer 	priv->node = node;
351ed2dbd31SArchie Cobbs 
3526c12c2b1SArchie Cobbs 	/* Start timer; timer is always running while node is alive */
353e0d32af7SGleb Smirnoff 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
3546c12c2b1SArchie Cobbs 
3556c12c2b1SArchie Cobbs 	/* Done */
356ed2dbd31SArchie Cobbs 	return (0);
357ed2dbd31SArchie Cobbs }
358ed2dbd31SArchie Cobbs 
359ed2dbd31SArchie Cobbs /*
360ed2dbd31SArchie Cobbs  * Method for attaching a new hook
361ed2dbd31SArchie Cobbs  */
362ed2dbd31SArchie Cobbs static	int
363ed2dbd31SArchie Cobbs ng_bridge_newhook(node_p node, hook_p hook, const char *name)
364ed2dbd31SArchie Cobbs {
36530400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
366631cabbaSGleb Smirnoff 	char linkName[NG_HOOKSIZ];
367631cabbaSGleb Smirnoff 	u_int32_t linkNum;
368631cabbaSGleb Smirnoff 	link_p link;
369f961caf2SLutz Donnerhacke 	const char *prefix = NG_BRIDGE_HOOK_LINK_PREFIX;
370f961caf2SLutz Donnerhacke 	bool isUplink;
371f961caf2SLutz Donnerhacke 
372f961caf2SLutz Donnerhacke 	/* Check for a link hook */
373f961caf2SLutz Donnerhacke 	if (strlen(name) <= strlen(prefix))
374f961caf2SLutz Donnerhacke 		return (EINVAL);       /* Unknown hook name */
375f961caf2SLutz Donnerhacke 
376f961caf2SLutz Donnerhacke 	isUplink = (name[0] == 'u');
377f961caf2SLutz Donnerhacke 	if (isUplink)
378f961caf2SLutz Donnerhacke 		prefix = NG_BRIDGE_HOOK_UPLINK_PREFIX;
379ed2dbd31SArchie Cobbs 
380631cabbaSGleb Smirnoff 	/* primitive parsing */
381f961caf2SLutz Donnerhacke 	linkNum = strtoul(name + strlen(prefix), NULL, 10);
382631cabbaSGleb Smirnoff 	/* validation by comparing against the reconstucted name  */
383f961caf2SLutz Donnerhacke 	snprintf(linkName, sizeof(linkName), "%s%u", prefix, linkNum);
384631cabbaSGleb Smirnoff 	if (strcmp(linkName, name) != 0)
385ed2dbd31SArchie Cobbs 		return (EINVAL);
386631cabbaSGleb Smirnoff 
387f961caf2SLutz Donnerhacke 	if (linkNum == 0 && isUplink)
388f961caf2SLutz Donnerhacke 		return (EINVAL);
389f961caf2SLutz Donnerhacke 
390631cabbaSGleb Smirnoff 	if(NG_PEER_NODE(hook) == node)
391631cabbaSGleb Smirnoff 	        return (ELOOP);
392631cabbaSGleb Smirnoff 
393f961caf2SLutz Donnerhacke 	link = malloc(sizeof(*link), M_NETGRAPH_BRIDGE, M_ZERO);
394631cabbaSGleb Smirnoff 	if (link == NULL)
395ed2dbd31SArchie Cobbs 		return (ENOMEM);
396f961caf2SLutz Donnerhacke 
39766c72859SLutz Donnerhacke 	link->stats.recvOctets = counter_u64_alloc(M_WAITOK);
39866c72859SLutz Donnerhacke 	link->stats.recvPackets = counter_u64_alloc(M_WAITOK);
39966c72859SLutz Donnerhacke 	link->stats.recvMulticasts = counter_u64_alloc(M_WAITOK);
40066c72859SLutz Donnerhacke 	link->stats.recvBroadcasts = counter_u64_alloc(M_WAITOK);
40166c72859SLutz Donnerhacke 	link->stats.recvUnknown = counter_u64_alloc(M_WAITOK);
40266c72859SLutz Donnerhacke 	link->stats.recvRunts = counter_u64_alloc(M_WAITOK);
40366c72859SLutz Donnerhacke 	link->stats.recvInvalid = counter_u64_alloc(M_WAITOK);
40466c72859SLutz Donnerhacke 	link->stats.xmitOctets = counter_u64_alloc(M_WAITOK);
40566c72859SLutz Donnerhacke 	link->stats.xmitPackets = counter_u64_alloc(M_WAITOK);
40666c72859SLutz Donnerhacke 	link->stats.xmitMulticasts = counter_u64_alloc(M_WAITOK);
40766c72859SLutz Donnerhacke 	link->stats.xmitBroadcasts = counter_u64_alloc(M_WAITOK);
40866c72859SLutz Donnerhacke 	link->stats.loopDrops = counter_u64_alloc(M_WAITOK);
40966c72859SLutz Donnerhacke 	link->stats.loopDetects = counter_u64_alloc(M_WAITOK);
41066c72859SLutz Donnerhacke 	link->stats.memoryFailures = counter_u64_alloc(M_WAITOK);
41166c72859SLutz Donnerhacke 
412631cabbaSGleb Smirnoff 	link->hook = hook;
413f961caf2SLutz Donnerhacke 	if (isUplink) {
414f961caf2SLutz Donnerhacke 		link->learnMac = 0;
415f961caf2SLutz Donnerhacke 		link->sendUnknown = 1;
416c869d905SLutz Donnerhacke 		if (priv->numLinks == 0)	/* if the first link is an uplink */
417c869d905SLutz Donnerhacke 		    priv->sendUnknown = 0;	/* switch to restrictive mode */
418f961caf2SLutz Donnerhacke 	} else {
419f961caf2SLutz Donnerhacke 		link->learnMac = 1;
420c869d905SLutz Donnerhacke 		link->sendUnknown = priv->sendUnknown;
421f961caf2SLutz Donnerhacke 	}
422f961caf2SLutz Donnerhacke 
423631cabbaSGleb Smirnoff 	NG_HOOK_SET_PRIVATE(hook, link);
424ed2dbd31SArchie Cobbs 	priv->numLinks++;
425ed2dbd31SArchie Cobbs 	return (0);
426ed2dbd31SArchie Cobbs }
427ed2dbd31SArchie Cobbs 
428ed2dbd31SArchie Cobbs /*
429ed2dbd31SArchie Cobbs  * Receive a control message
430ed2dbd31SArchie Cobbs  */
43166c72859SLutz Donnerhacke static void ng_bridge_clear_link_stats(struct ng_bridge_link_kernel_stats * p)
43266c72859SLutz Donnerhacke {
43366c72859SLutz Donnerhacke 	counter_u64_zero(p->recvOctets);
43466c72859SLutz Donnerhacke 	counter_u64_zero(p->recvPackets);
43566c72859SLutz Donnerhacke 	counter_u64_zero(p->recvMulticasts);
43666c72859SLutz Donnerhacke 	counter_u64_zero(p->recvBroadcasts);
43766c72859SLutz Donnerhacke 	counter_u64_zero(p->recvUnknown);
43866c72859SLutz Donnerhacke 	counter_u64_zero(p->recvRunts);
43966c72859SLutz Donnerhacke 	counter_u64_zero(p->recvInvalid);
44066c72859SLutz Donnerhacke 	counter_u64_zero(p->xmitOctets);
44166c72859SLutz Donnerhacke 	counter_u64_zero(p->xmitPackets);
44266c72859SLutz Donnerhacke 	counter_u64_zero(p->xmitMulticasts);
44366c72859SLutz Donnerhacke 	counter_u64_zero(p->xmitBroadcasts);
44466c72859SLutz Donnerhacke 	counter_u64_zero(p->loopDrops);
44566c72859SLutz Donnerhacke 	counter_u64_zero(p->loopDetects);
44666c72859SLutz Donnerhacke 	counter_u64_zero(p->memoryFailures);
44766c72859SLutz Donnerhacke };
44866c72859SLutz Donnerhacke 
449ed2dbd31SArchie Cobbs static int
4500b951c55SGleb Smirnoff ng_bridge_reset_link(hook_p hook, void *arg __unused)
451631cabbaSGleb Smirnoff {
452631cabbaSGleb Smirnoff 	link_p priv = NG_HOOK_PRIVATE(hook);
453631cabbaSGleb Smirnoff 
454631cabbaSGleb Smirnoff 	priv->loopCount = 0;
45566c72859SLutz Donnerhacke 	ng_bridge_clear_link_stats(&priv->stats);
4560b951c55SGleb Smirnoff 	return (1);
457631cabbaSGleb Smirnoff }
458631cabbaSGleb Smirnoff 
459631cabbaSGleb Smirnoff static int
460069154d5SJulian Elischer ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
461ed2dbd31SArchie Cobbs {
46230400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
463ed2dbd31SArchie Cobbs 	struct ng_mesg *resp = NULL;
464ed2dbd31SArchie Cobbs 	int error = 0;
465069154d5SJulian Elischer 	struct ng_mesg *msg;
466ed2dbd31SArchie Cobbs 
467069154d5SJulian Elischer 	NGI_GET_MSG(item, msg);
468ed2dbd31SArchie Cobbs 	switch (msg->header.typecookie) {
469ed2dbd31SArchie Cobbs 	case NGM_BRIDGE_COOKIE:
470ed2dbd31SArchie Cobbs 		switch (msg->header.cmd) {
471ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_CONFIG:
472ed2dbd31SArchie Cobbs 		    {
473ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
474ed2dbd31SArchie Cobbs 
475ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg,
476ed2dbd31SArchie Cobbs 			    sizeof(struct ng_bridge_config), M_NOWAIT);
477ed2dbd31SArchie Cobbs 			if (resp == NULL) {
478ed2dbd31SArchie Cobbs 				error = ENOMEM;
479ed2dbd31SArchie Cobbs 				break;
480ed2dbd31SArchie Cobbs 			}
481ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)resp->data;
482ed2dbd31SArchie Cobbs 			*conf = priv->conf;	/* no sanity checking needed */
483ed2dbd31SArchie Cobbs 			break;
484ed2dbd31SArchie Cobbs 		    }
485ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_SET_CONFIG:
486ed2dbd31SArchie Cobbs 		    {
487ed2dbd31SArchie Cobbs 			struct ng_bridge_config *conf;
488ed2dbd31SArchie Cobbs 
489ed2dbd31SArchie Cobbs 			if (msg->header.arglen
490ed2dbd31SArchie Cobbs 			    != sizeof(struct ng_bridge_config)) {
491ed2dbd31SArchie Cobbs 				error = EINVAL;
492ed2dbd31SArchie Cobbs 				break;
493ed2dbd31SArchie Cobbs 			}
494ed2dbd31SArchie Cobbs 			conf = (struct ng_bridge_config *)msg->data;
495ed2dbd31SArchie Cobbs 			priv->conf = *conf;
496ed2dbd31SArchie Cobbs 			break;
497ed2dbd31SArchie Cobbs 		    }
498ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_RESET:
499ed2dbd31SArchie Cobbs 		    {
500631cabbaSGleb Smirnoff 			hook_p rethook;
501ed2dbd31SArchie Cobbs 
502ed2dbd31SArchie Cobbs 			/* Flush all entries in the hash table */
503631cabbaSGleb Smirnoff 			ng_bridge_remove_hosts(priv, NULL);
504ed2dbd31SArchie Cobbs 
505ed2dbd31SArchie Cobbs 			/* Reset all loop detection counters and stats */
5060b951c55SGleb Smirnoff 			NG_NODE_FOREACH_HOOK(node, ng_bridge_reset_link, NULL,
5070b951c55SGleb Smirnoff 			    rethook);
508ed2dbd31SArchie Cobbs 			break;
509ed2dbd31SArchie Cobbs 		    }
510ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_STATS:
511ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_CLR_STATS:
512ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GETCLR_STATS:
513ed2dbd31SArchie Cobbs 		    {
514631cabbaSGleb Smirnoff 			hook_p hook;
515631cabbaSGleb Smirnoff 			link_p link;
516631cabbaSGleb Smirnoff 			char linkName[NG_HOOKSIZ];
517f961caf2SLutz Donnerhacke 			int linkNum;
518ed2dbd31SArchie Cobbs 
519ed2dbd31SArchie Cobbs 			/* Get link number */
520ed2dbd31SArchie Cobbs 			if (msg->header.arglen != sizeof(u_int32_t)) {
521ed2dbd31SArchie Cobbs 				error = EINVAL;
522ed2dbd31SArchie Cobbs 				break;
523ed2dbd31SArchie Cobbs 			}
524f961caf2SLutz Donnerhacke 			linkNum = *((int32_t *)msg->data);
525f961caf2SLutz Donnerhacke 			if (linkNum < 0)
526631cabbaSGleb Smirnoff 				snprintf(linkName, sizeof(linkName),
527f961caf2SLutz Donnerhacke 				    "%s%u", NG_BRIDGE_HOOK_UPLINK_PREFIX, -linkNum);
528f961caf2SLutz Donnerhacke 			else
529f961caf2SLutz Donnerhacke 				snprintf(linkName, sizeof(linkName),
530f961caf2SLutz Donnerhacke 				    "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX, linkNum);
531631cabbaSGleb Smirnoff 
532631cabbaSGleb Smirnoff 			if ((hook = ng_findhook(node, linkName)) == NULL) {
533ed2dbd31SArchie Cobbs 				error = ENOTCONN;
534ed2dbd31SArchie Cobbs 				break;
535ed2dbd31SArchie Cobbs 			}
536631cabbaSGleb Smirnoff 			link = NG_HOOK_PRIVATE(hook);
537ed2dbd31SArchie Cobbs 
538ed2dbd31SArchie Cobbs 			/* Get/clear stats */
539ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
54066c72859SLutz Donnerhacke 				struct ng_bridge_link_stats *rs;
54166c72859SLutz Donnerhacke 
542ed2dbd31SArchie Cobbs 				NG_MKRESPONSE(resp, msg,
543ed2dbd31SArchie Cobbs 				    sizeof(link->stats), M_NOWAIT);
544ed2dbd31SArchie Cobbs 				if (resp == NULL) {
545ed2dbd31SArchie Cobbs 					error = ENOMEM;
546ed2dbd31SArchie Cobbs 					break;
547ed2dbd31SArchie Cobbs 				}
54866c72859SLutz Donnerhacke 				rs = (struct ng_bridge_link_stats *)resp->data;
54966c72859SLutz Donnerhacke #define FETCH(x)	rs->x = counter_u64_fetch(link->stats.x)
55066c72859SLutz Donnerhacke 				FETCH(recvOctets);
55166c72859SLutz Donnerhacke 				FETCH(recvPackets);
55266c72859SLutz Donnerhacke 				FETCH(recvMulticasts);
55366c72859SLutz Donnerhacke 				FETCH(recvBroadcasts);
55466c72859SLutz Donnerhacke 				FETCH(recvUnknown);
55566c72859SLutz Donnerhacke 				FETCH(recvRunts);
55666c72859SLutz Donnerhacke 				FETCH(recvInvalid);
55766c72859SLutz Donnerhacke 				FETCH(xmitOctets);
55866c72859SLutz Donnerhacke 				FETCH(xmitPackets);
55966c72859SLutz Donnerhacke 				FETCH(xmitMulticasts);
56066c72859SLutz Donnerhacke 				FETCH(xmitBroadcasts);
56166c72859SLutz Donnerhacke 				FETCH(loopDrops);
56266c72859SLutz Donnerhacke 				FETCH(loopDetects);
56366c72859SLutz Donnerhacke 				FETCH(memoryFailures);
56466c72859SLutz Donnerhacke #undef FETCH
565ed2dbd31SArchie Cobbs 			}
566ed2dbd31SArchie Cobbs 			if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
56766c72859SLutz Donnerhacke 				ng_bridge_clear_link_stats(&link->stats);
568ed2dbd31SArchie Cobbs 			break;
569ed2dbd31SArchie Cobbs 		    }
570ed2dbd31SArchie Cobbs 		case NGM_BRIDGE_GET_TABLE:
571ed2dbd31SArchie Cobbs 		    {
572ed2dbd31SArchie Cobbs 			struct ng_bridge_host_ary *ary;
573ccf4cd2eSLutz Donnerhacke 			struct ng_bridge_host *host;
574ed2dbd31SArchie Cobbs 			int i = 0, bucket;
575ed2dbd31SArchie Cobbs 
576ed2dbd31SArchie Cobbs 			NG_MKRESPONSE(resp, msg, sizeof(*ary)
577ed2dbd31SArchie Cobbs 			    + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
578ed2dbd31SArchie Cobbs 			if (resp == NULL) {
579ed2dbd31SArchie Cobbs 				error = ENOMEM;
580ed2dbd31SArchie Cobbs 				break;
581ed2dbd31SArchie Cobbs 			}
582ed2dbd31SArchie Cobbs 			ary = (struct ng_bridge_host_ary *)resp->data;
583ed2dbd31SArchie Cobbs 			ary->numHosts = priv->numHosts;
584ed2dbd31SArchie Cobbs 			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
585ccf4cd2eSLutz Donnerhacke 				SLIST_FOREACH(host, &priv->tab[bucket], next) {
586631cabbaSGleb Smirnoff 					memcpy(ary->hosts[i].addr,
587ccf4cd2eSLutz Donnerhacke 					       host->addr,
588631cabbaSGleb Smirnoff 					       sizeof(ary->hosts[i].addr));
589ccf4cd2eSLutz Donnerhacke 					ary->hosts[i].age       = host->age;
590ccf4cd2eSLutz Donnerhacke 					ary->hosts[i].staleness = host->staleness;
591631cabbaSGleb Smirnoff 					strncpy(ary->hosts[i].hook,
592ccf4cd2eSLutz Donnerhacke 						NG_HOOK_NAME(host->link->hook),
593631cabbaSGleb Smirnoff 						sizeof(ary->hosts[i].hook));
594631cabbaSGleb Smirnoff 					i++;
595631cabbaSGleb Smirnoff 				}
596ed2dbd31SArchie Cobbs 			}
597ed2dbd31SArchie Cobbs 			break;
598ed2dbd31SArchie Cobbs 		    }
599f8aab721SMarko Zec 		case NGM_BRIDGE_SET_PERSISTENT:
600f8aab721SMarko Zec 		    {
601f8aab721SMarko Zec 			priv->persistent = 1;
602f8aab721SMarko Zec 			break;
603f8aab721SMarko Zec 		    }
604ed2dbd31SArchie Cobbs 		default:
605ed2dbd31SArchie Cobbs 			error = EINVAL;
606ed2dbd31SArchie Cobbs 			break;
607ed2dbd31SArchie Cobbs 		}
608ed2dbd31SArchie Cobbs 		break;
609ed2dbd31SArchie Cobbs 	default:
610ed2dbd31SArchie Cobbs 		error = EINVAL;
611ed2dbd31SArchie Cobbs 		break;
612ed2dbd31SArchie Cobbs 	}
613ed2dbd31SArchie Cobbs 
614ed2dbd31SArchie Cobbs 	/* Done */
615069154d5SJulian Elischer 	NG_RESPOND_MSG(error, node, item, resp);
616069154d5SJulian Elischer 	NG_FREE_MSG(msg);
617ed2dbd31SArchie Cobbs 	return (error);
618ed2dbd31SArchie Cobbs }
619ed2dbd31SArchie Cobbs 
620ed2dbd31SArchie Cobbs /*
621ed2dbd31SArchie Cobbs  * Receive data on a hook
622ed2dbd31SArchie Cobbs  */
623631cabbaSGleb Smirnoff struct ng_bridge_send_ctx {
624631cabbaSGleb Smirnoff 	link_p foundFirst, incoming;
625631cabbaSGleb Smirnoff 	struct mbuf * m;
626631cabbaSGleb Smirnoff 	int manycast, error;
627631cabbaSGleb Smirnoff };
628631cabbaSGleb Smirnoff 
629631cabbaSGleb Smirnoff static int
6300b951c55SGleb Smirnoff ng_bridge_send_ctx(hook_p dst, void *arg)
631631cabbaSGleb Smirnoff {
6320b951c55SGleb Smirnoff 	struct ng_bridge_send_ctx *ctx = arg;
633631cabbaSGleb Smirnoff 	link_p destLink = NG_HOOK_PRIVATE(dst);
634631cabbaSGleb Smirnoff 	struct mbuf *m2 = NULL;
635631cabbaSGleb Smirnoff 	int error = 0;
636631cabbaSGleb Smirnoff 
637631cabbaSGleb Smirnoff 	/* Skip incoming link */
638631cabbaSGleb Smirnoff 	if (destLink == ctx->incoming) {
639631cabbaSGleb Smirnoff 		return (1);
640631cabbaSGleb Smirnoff 	}
641631cabbaSGleb Smirnoff 
642f961caf2SLutz Donnerhacke 	/* Skip sending unknowns to undesired links  */
643f961caf2SLutz Donnerhacke 	if (!ctx->manycast && !destLink->sendUnknown)
644f961caf2SLutz Donnerhacke 		return (1);
645f961caf2SLutz Donnerhacke 
646631cabbaSGleb Smirnoff 	if (ctx->foundFirst == NULL) {
647631cabbaSGleb Smirnoff 		/*
648631cabbaSGleb Smirnoff 		 * This is the first usable link we have found.
649631cabbaSGleb Smirnoff 		 * Reserve it for the originals.
650631cabbaSGleb Smirnoff 		 * If we never find another we save a copy.
651631cabbaSGleb Smirnoff 		 */
652631cabbaSGleb Smirnoff 		ctx->foundFirst = destLink;
653631cabbaSGleb Smirnoff 		return (1);
654631cabbaSGleb Smirnoff 	}
655631cabbaSGleb Smirnoff 
656631cabbaSGleb Smirnoff 	/*
657631cabbaSGleb Smirnoff 	 * It's usable link but not the reserved (first) one.
658631cabbaSGleb Smirnoff 	 * Copy mbuf info for sending.
659631cabbaSGleb Smirnoff 	 */
660631cabbaSGleb Smirnoff 	m2 = m_dup(ctx->m, M_NOWAIT);	/* XXX m_copypacket() */
661631cabbaSGleb Smirnoff 	if (m2 == NULL) {
66266c72859SLutz Donnerhacke 		counter_u64_add(ctx->incoming->stats.memoryFailures, 1);
663631cabbaSGleb Smirnoff 		ctx->error = ENOBUFS;
664631cabbaSGleb Smirnoff 		return (0);	       /* abort loop */
665631cabbaSGleb Smirnoff 	}
666631cabbaSGleb Smirnoff 
667631cabbaSGleb Smirnoff 	/* Update stats */
66866c72859SLutz Donnerhacke 	counter_u64_add(destLink->stats.xmitPackets, 1);
66966c72859SLutz Donnerhacke 	counter_u64_add(destLink->stats.xmitOctets, m2->m_pkthdr.len);
670631cabbaSGleb Smirnoff 	switch (ctx->manycast) {
671631cabbaSGleb Smirnoff 	 default:					/* unknown unicast */
672631cabbaSGleb Smirnoff 		break;
673631cabbaSGleb Smirnoff 	 case 1:					/* multicast */
67466c72859SLutz Donnerhacke 		counter_u64_add(destLink->stats.xmitMulticasts, 1);
675631cabbaSGleb Smirnoff 		break;
676631cabbaSGleb Smirnoff 	 case 2:					/* broadcast */
67766c72859SLutz Donnerhacke 		counter_u64_add(destLink->stats.xmitBroadcasts, 1);
678631cabbaSGleb Smirnoff 		break;
679631cabbaSGleb Smirnoff 	}
680631cabbaSGleb Smirnoff 
681631cabbaSGleb Smirnoff 	/* Send packet */
682631cabbaSGleb Smirnoff 	NG_SEND_DATA_ONLY(error, destLink->hook, m2);
683631cabbaSGleb Smirnoff 	if(error)
684631cabbaSGleb Smirnoff 	  ctx->error = error;
685631cabbaSGleb Smirnoff 	return (1);
686631cabbaSGleb Smirnoff }
687631cabbaSGleb Smirnoff 
688ed2dbd31SArchie Cobbs static int
689069154d5SJulian Elischer ng_bridge_rcvdata(hook_p hook, item_p item)
690ed2dbd31SArchie Cobbs {
69130400f03SJulian Elischer 	const node_p node = NG_HOOK_NODE(hook);
69230400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
693ed2dbd31SArchie Cobbs 	struct ng_bridge_host *host;
694ed2dbd31SArchie Cobbs 	struct ether_header *eh;
695631cabbaSGleb Smirnoff 	struct ng_bridge_send_ctx ctx = { 0 };
696631cabbaSGleb Smirnoff 	hook_p ret;
697ed2dbd31SArchie Cobbs 
698631cabbaSGleb Smirnoff 	NGI_GET_M(item, ctx.m);
699ed2dbd31SArchie Cobbs 
700631cabbaSGleb Smirnoff 	ctx.incoming = NG_HOOK_PRIVATE(hook);
701ed2dbd31SArchie Cobbs 	/* Sanity check packet and pull up header */
702631cabbaSGleb Smirnoff 	if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) {
70366c72859SLutz Donnerhacke 		counter_u64_add(ctx.incoming->stats.recvRunts, 1);
704069154d5SJulian Elischer 		NG_FREE_ITEM(item);
705631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
706ed2dbd31SArchie Cobbs 		return (EINVAL);
707ed2dbd31SArchie Cobbs 	}
708631cabbaSGleb Smirnoff 	if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) {
70966c72859SLutz Donnerhacke 		counter_u64_add(ctx.incoming->stats.memoryFailures, 1);
710069154d5SJulian Elischer 		NG_FREE_ITEM(item);
711ed2dbd31SArchie Cobbs 		return (ENOBUFS);
712ed2dbd31SArchie Cobbs 	}
713631cabbaSGleb Smirnoff 	eh = mtod(ctx.m, struct ether_header *);
714ed2dbd31SArchie Cobbs 	if ((eh->ether_shost[0] & 1) != 0) {
71566c72859SLutz Donnerhacke 		counter_u64_add(ctx.incoming->stats.recvInvalid, 1);
716069154d5SJulian Elischer 		NG_FREE_ITEM(item);
717631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
718ed2dbd31SArchie Cobbs 		return (EINVAL);
719ed2dbd31SArchie Cobbs 	}
720ed2dbd31SArchie Cobbs 
721ed2dbd31SArchie Cobbs 	/* Is link disabled due to a loopback condition? */
722631cabbaSGleb Smirnoff 	if (ctx.incoming->loopCount != 0) {
72366c72859SLutz Donnerhacke 		counter_u64_add(ctx.incoming->stats.loopDrops, 1);
724069154d5SJulian Elischer 		NG_FREE_ITEM(item);
725631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
726ed2dbd31SArchie Cobbs 		return (ELOOP);		/* XXX is this an appropriate error? */
727ed2dbd31SArchie Cobbs 	}
728ed2dbd31SArchie Cobbs 
729ed2dbd31SArchie Cobbs 	/* Update stats */
73066c72859SLutz Donnerhacke 	counter_u64_add(ctx.incoming->stats.recvPackets, 1);
73166c72859SLutz Donnerhacke 	counter_u64_add(ctx.incoming->stats.recvOctets, ctx.m->m_pkthdr.len);
732631cabbaSGleb Smirnoff 	if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) {
733ed2dbd31SArchie Cobbs 		if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
73466c72859SLutz Donnerhacke 			counter_u64_add(ctx.incoming->stats.recvBroadcasts, 1);
735631cabbaSGleb Smirnoff 			ctx.manycast = 2;
736ed2dbd31SArchie Cobbs 		} else
73766c72859SLutz Donnerhacke 			counter_u64_add(ctx.incoming->stats.recvMulticasts, 1);
738ed2dbd31SArchie Cobbs 	}
739ed2dbd31SArchie Cobbs 
740ed2dbd31SArchie Cobbs 	/* Look up packet's source Ethernet address in hashtable */
741ed2dbd31SArchie Cobbs 	if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
742*011b7317SLutz Donnerhacke 		/* Update time since last heard from this host.
743*011b7317SLutz Donnerhacke 		 * This is safe without locking, because it's
744*011b7317SLutz Donnerhacke 		 * the only operation during shared access.
745*011b7317SLutz Donnerhacke 		 */
746ed2dbd31SArchie Cobbs 		host->staleness = 0;
747ed2dbd31SArchie Cobbs 
748ed2dbd31SArchie Cobbs 		/* Did host jump to a different link? */
749631cabbaSGleb Smirnoff 		if (host->link != ctx.incoming) {
750ed2dbd31SArchie Cobbs 			/*
751ed2dbd31SArchie Cobbs 			 * If the host's old link was recently established
752ed2dbd31SArchie Cobbs 			 * on the old link and it's already jumped to a new
753ed2dbd31SArchie Cobbs 			 * link, declare a loopback condition.
754ed2dbd31SArchie Cobbs 			 */
755ed2dbd31SArchie Cobbs 			if (host->age < priv->conf.minStableAge) {
756ed2dbd31SArchie Cobbs 				/* Log the problem */
757ed2dbd31SArchie Cobbs 				if (priv->conf.debugLevel >= 2) {
758631cabbaSGleb Smirnoff 					struct ifnet *ifp = ctx.m->m_pkthdr.rcvif;
759ed2dbd31SArchie Cobbs 					char suffix[32];
760ed2dbd31SArchie Cobbs 
761ed2dbd31SArchie Cobbs 					if (ifp != NULL)
762ed2dbd31SArchie Cobbs 						snprintf(suffix, sizeof(suffix),
7639bf40edeSBrooks Davis 						    " (%s)", ifp->if_xname);
764ed2dbd31SArchie Cobbs 					else
765ed2dbd31SArchie Cobbs 						*suffix = '\0';
766ed2dbd31SArchie Cobbs 					log(LOG_WARNING, "ng_bridge: %s:"
767ed2dbd31SArchie Cobbs 					    " loopback detected on %s%s\n",
768ed2dbd31SArchie Cobbs 					    ng_bridge_nodename(node),
76930400f03SJulian Elischer 					    NG_HOOK_NAME(hook), suffix);
770ed2dbd31SArchie Cobbs 				}
771ed2dbd31SArchie Cobbs 
772ed2dbd31SArchie Cobbs 				/* Mark link as linka non grata */
773631cabbaSGleb Smirnoff 				ctx.incoming->loopCount = priv->conf.loopTimeout;
77466c72859SLutz Donnerhacke 				counter_u64_add(ctx.incoming->stats.loopDetects, 1);
775ed2dbd31SArchie Cobbs 
776ed2dbd31SArchie Cobbs 				/* Forget all hosts on this link */
777631cabbaSGleb Smirnoff 				ng_bridge_remove_hosts(priv, ctx.incoming);
778ed2dbd31SArchie Cobbs 
779ed2dbd31SArchie Cobbs 				/* Drop packet */
78066c72859SLutz Donnerhacke 				counter_u64_add(ctx.incoming->stats.loopDrops, 1);
781069154d5SJulian Elischer 				NG_FREE_ITEM(item);
782631cabbaSGleb Smirnoff 				NG_FREE_M(ctx.m);
783ed2dbd31SArchie Cobbs 				return (ELOOP);		/* XXX appropriate? */
784ed2dbd31SArchie Cobbs 			}
785ed2dbd31SArchie Cobbs 
786ed2dbd31SArchie Cobbs 			/* Move host over to new link */
787631cabbaSGleb Smirnoff 			host->link = ctx.incoming;
788ed2dbd31SArchie Cobbs 			host->age = 0;
789ed2dbd31SArchie Cobbs 		}
790f961caf2SLutz Donnerhacke 	} else if (ctx.incoming->learnMac) {
791631cabbaSGleb Smirnoff 		if (!ng_bridge_put(priv, eh->ether_shost, ctx.incoming)) {
79266c72859SLutz Donnerhacke 			counter_u64_add(ctx.incoming->stats.memoryFailures, 1);
793069154d5SJulian Elischer 			NG_FREE_ITEM(item);
794631cabbaSGleb Smirnoff 			NG_FREE_M(ctx.m);
795ed2dbd31SArchie Cobbs 			return (ENOMEM);
796ed2dbd31SArchie Cobbs 		}
797ed2dbd31SArchie Cobbs 	}
798ed2dbd31SArchie Cobbs 
799ed2dbd31SArchie Cobbs 	/* Run packet through ipfw processing, if enabled */
8009b932e9eSAndre Oppermann #if 0
8010b4b0b0fSJulian Elischer 	if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) {
802ed2dbd31SArchie Cobbs 		/* XXX not implemented yet */
803ed2dbd31SArchie Cobbs 	}
8049b932e9eSAndre Oppermann #endif
805ed2dbd31SArchie Cobbs 
806ed2dbd31SArchie Cobbs 	/*
807ed2dbd31SArchie Cobbs 	 * If unicast and destination host known, deliver to host's link,
808ed2dbd31SArchie Cobbs 	 * unless it is the same link as the packet came in on.
809ed2dbd31SArchie Cobbs 	 */
810631cabbaSGleb Smirnoff 	if (!ctx.manycast) {
811ed2dbd31SArchie Cobbs 		/* Determine packet destination link */
812ed2dbd31SArchie Cobbs 		if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
813631cabbaSGleb Smirnoff 			link_p destLink = host->link;
814ed2dbd31SArchie Cobbs 
815ed2dbd31SArchie Cobbs 			/* If destination same as incoming link, do nothing */
816631cabbaSGleb Smirnoff 			if (destLink == ctx.incoming) {
817069154d5SJulian Elischer 				NG_FREE_ITEM(item);
818631cabbaSGleb Smirnoff 				NG_FREE_M(ctx.m);
819ed2dbd31SArchie Cobbs 				return (0);
820ed2dbd31SArchie Cobbs 			}
821ed2dbd31SArchie Cobbs 
822ed2dbd31SArchie Cobbs 			/* Deliver packet out the destination link */
82366c72859SLutz Donnerhacke 			counter_u64_add(destLink->stats.xmitPackets, 1);
82466c72859SLutz Donnerhacke 			counter_u64_add(destLink->stats.xmitOctets, ctx.m->m_pkthdr.len);
825631cabbaSGleb Smirnoff 			NG_FWD_NEW_DATA(ctx.error, item, destLink->hook, ctx.m);
826631cabbaSGleb Smirnoff 			return (ctx.error);
827ed2dbd31SArchie Cobbs 		}
828ed2dbd31SArchie Cobbs 
829ed2dbd31SArchie Cobbs 		/* Destination host is not known */
83066c72859SLutz Donnerhacke 		counter_u64_add(ctx.incoming->stats.recvUnknown, 1);
831ed2dbd31SArchie Cobbs 	}
832ed2dbd31SArchie Cobbs 
833ed2dbd31SArchie Cobbs 	/* Distribute unknown, multicast, broadcast pkts to all other links */
834631cabbaSGleb Smirnoff 	NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx, ret);
835ed2dbd31SArchie Cobbs 
836069154d5SJulian Elischer 	/* If we never saw a good link, leave. */
837631cabbaSGleb Smirnoff 	if (ctx.foundFirst == NULL || ctx.error != 0) {
838069154d5SJulian Elischer 		NG_FREE_ITEM(item);
839631cabbaSGleb Smirnoff 		NG_FREE_M(ctx.m);
840631cabbaSGleb Smirnoff 		return (ctx.error);
841069154d5SJulian Elischer 	}
842069154d5SJulian Elischer 
843069154d5SJulian Elischer 	/*
844069154d5SJulian Elischer 	 * If we've sent all the others, send the original
845069154d5SJulian Elischer 	 * on the first link we found.
846069154d5SJulian Elischer 	 */
847631cabbaSGleb Smirnoff 	NG_FWD_NEW_DATA(ctx.error, item, ctx.foundFirst->hook, ctx.m);
848631cabbaSGleb Smirnoff 	return (ctx.error);
849ed2dbd31SArchie Cobbs }
850ed2dbd31SArchie Cobbs 
851ed2dbd31SArchie Cobbs /*
852ed2dbd31SArchie Cobbs  * Shutdown node
853ed2dbd31SArchie Cobbs  */
854ed2dbd31SArchie Cobbs static int
855069154d5SJulian Elischer ng_bridge_shutdown(node_p node)
856ed2dbd31SArchie Cobbs {
85730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
858ed2dbd31SArchie Cobbs 
8596c12c2b1SArchie Cobbs 	/*
860195cf617SRuslan Ermilov 	 * Shut down everything including the timer.  Even if the
861195cf617SRuslan Ermilov 	 * callout has already been dequeued and is about to be
862195cf617SRuslan Ermilov 	 * run, ng_bridge_timeout() won't be fired as the node
863195cf617SRuslan Ermilov 	 * is already marked NGF_INVALID, so we're safe to free
864195cf617SRuslan Ermilov 	 * the node now.
8656c12c2b1SArchie Cobbs 	 */
866ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
867ed2dbd31SArchie Cobbs 	    ("%s: numLinks=%d numHosts=%d",
8686e551fb6SDavid E. O'Brien 	    __func__, priv->numLinks, priv->numHosts));
869195cf617SRuslan Ermilov 	ng_uncallout(&priv->timer, node);
870195cf617SRuslan Ermilov 	NG_NODE_SET_PRIVATE(node, NULL);
871195cf617SRuslan Ermilov 	NG_NODE_UNREF(node);
8721ede983cSDag-Erling Smørgrav 	free(priv->tab, M_NETGRAPH_BRIDGE);
8731ede983cSDag-Erling Smørgrav 	free(priv, M_NETGRAPH_BRIDGE);
874ed2dbd31SArchie Cobbs 	return (0);
875ed2dbd31SArchie Cobbs }
876ed2dbd31SArchie Cobbs 
877ed2dbd31SArchie Cobbs /*
878ed2dbd31SArchie Cobbs  * Hook disconnection.
879ed2dbd31SArchie Cobbs  */
880ed2dbd31SArchie Cobbs static int
881ed2dbd31SArchie Cobbs ng_bridge_disconnect(hook_p hook)
882ed2dbd31SArchie Cobbs {
88330400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
884631cabbaSGleb Smirnoff 	link_p link = NG_HOOK_PRIVATE(hook);
885ed2dbd31SArchie Cobbs 
886ed2dbd31SArchie Cobbs 	/* Remove all hosts associated with this link */
887631cabbaSGleb Smirnoff 	ng_bridge_remove_hosts(priv, link);
888ed2dbd31SArchie Cobbs 
889ed2dbd31SArchie Cobbs 	/* Free associated link information */
89066c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvOctets);
89166c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvPackets);
89266c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvMulticasts);
89366c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvBroadcasts);
89466c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvUnknown);
89566c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvRunts);
89666c72859SLutz Donnerhacke 	counter_u64_free(link->stats.recvInvalid);
89766c72859SLutz Donnerhacke 	counter_u64_free(link->stats.xmitOctets);
89866c72859SLutz Donnerhacke 	counter_u64_free(link->stats.xmitPackets);
89966c72859SLutz Donnerhacke 	counter_u64_free(link->stats.xmitMulticasts);
90066c72859SLutz Donnerhacke 	counter_u64_free(link->stats.xmitBroadcasts);
90166c72859SLutz Donnerhacke 	counter_u64_free(link->stats.loopDrops);
90266c72859SLutz Donnerhacke 	counter_u64_free(link->stats.loopDetects);
90366c72859SLutz Donnerhacke 	counter_u64_free(link->stats.memoryFailures);
904631cabbaSGleb Smirnoff 	free(link, M_NETGRAPH_BRIDGE);
905ed2dbd31SArchie Cobbs 	priv->numLinks--;
906ed2dbd31SArchie Cobbs 
907ed2dbd31SArchie Cobbs 	/* If no more hooks, go away */
90830400f03SJulian Elischer 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
909f8aab721SMarko Zec 	    && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
910f8aab721SMarko Zec 	    && !priv->persistent) {
91130400f03SJulian Elischer 		ng_rmnode_self(NG_HOOK_NODE(hook));
91230400f03SJulian Elischer 	}
913ed2dbd31SArchie Cobbs 	return (0);
914ed2dbd31SArchie Cobbs }
915ed2dbd31SArchie Cobbs 
916ed2dbd31SArchie Cobbs /******************************************************************
917ed2dbd31SArchie Cobbs 		    HASH TABLE FUNCTIONS
918ed2dbd31SArchie Cobbs ******************************************************************/
919ed2dbd31SArchie Cobbs 
920ed2dbd31SArchie Cobbs /*
921ed2dbd31SArchie Cobbs  * Hash algorithm
922ed2dbd31SArchie Cobbs  */
923ed2dbd31SArchie Cobbs #define HASH(addr,mask)		( (((const u_int16_t *)(addr))[0] 	\
924ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[1] 	\
925ed2dbd31SArchie Cobbs 				 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
926ed2dbd31SArchie Cobbs 
927ed2dbd31SArchie Cobbs /*
928ed2dbd31SArchie Cobbs  * Find a host entry in the table.
929ed2dbd31SArchie Cobbs  */
930ed2dbd31SArchie Cobbs static struct ng_bridge_host *
9316117aa58SLutz Donnerhacke ng_bridge_get(priv_cp priv, const u_char *addr)
932ed2dbd31SArchie Cobbs {
933ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
934ccf4cd2eSLutz Donnerhacke 	struct ng_bridge_host *host;
935ed2dbd31SArchie Cobbs 
936ccf4cd2eSLutz Donnerhacke 	SLIST_FOREACH(host, &priv->tab[bucket], next) {
937ccf4cd2eSLutz Donnerhacke 		if (ETHER_EQUAL(host->addr, addr))
938ccf4cd2eSLutz Donnerhacke 			return (host);
939ed2dbd31SArchie Cobbs 	}
940ed2dbd31SArchie Cobbs 	return (NULL);
941ed2dbd31SArchie Cobbs }
942ed2dbd31SArchie Cobbs 
943ed2dbd31SArchie Cobbs /*
944ed2dbd31SArchie Cobbs  * Add a new host entry to the table. This assumes the host doesn't
945ed2dbd31SArchie Cobbs  * already exist in the table. Returns 1 on success, 0 if there
946ed2dbd31SArchie Cobbs  * was a memory allocation failure.
947ed2dbd31SArchie Cobbs  */
948ed2dbd31SArchie Cobbs static int
949631cabbaSGleb Smirnoff ng_bridge_put(priv_p priv, const u_char *addr, link_p link)
950ed2dbd31SArchie Cobbs {
951ed2dbd31SArchie Cobbs 	const int bucket = HASH(addr, priv->hashMask);
952ccf4cd2eSLutz Donnerhacke 	struct ng_bridge_host *host;
953ed2dbd31SArchie Cobbs 
954ed2dbd31SArchie Cobbs #ifdef INVARIANTS
955ed2dbd31SArchie Cobbs 	/* Assert that entry does not already exist in hashtable */
956ccf4cd2eSLutz Donnerhacke 	SLIST_FOREACH(host, &priv->tab[bucket], next) {
957ccf4cd2eSLutz Donnerhacke 		KASSERT(!ETHER_EQUAL(host->addr, addr),
9586e551fb6SDavid E. O'Brien 		    ("%s: entry %6D exists in table", __func__, addr, ":"));
959ed2dbd31SArchie Cobbs 	}
960ed2dbd31SArchie Cobbs #endif
961ed2dbd31SArchie Cobbs 
962ed2dbd31SArchie Cobbs 	/* Allocate and initialize new hashtable entry */
963ccf4cd2eSLutz Donnerhacke 	host = malloc(sizeof(*host), M_NETGRAPH_BRIDGE, M_NOWAIT);
964ccf4cd2eSLutz Donnerhacke 	if (host == NULL)
965ed2dbd31SArchie Cobbs 		return (0);
966ccf4cd2eSLutz Donnerhacke 	bcopy(addr, host->addr, ETHER_ADDR_LEN);
967ccf4cd2eSLutz Donnerhacke 	host->link = link;
968ccf4cd2eSLutz Donnerhacke 	host->staleness = 0;
969ccf4cd2eSLutz Donnerhacke 	host->age = 0;
970ed2dbd31SArchie Cobbs 
971ed2dbd31SArchie Cobbs 	/* Add new element to hash bucket */
972ccf4cd2eSLutz Donnerhacke 	SLIST_INSERT_HEAD(&priv->tab[bucket], host, next);
973ed2dbd31SArchie Cobbs 	priv->numHosts++;
974ed2dbd31SArchie Cobbs 
975ed2dbd31SArchie Cobbs 	/* Resize table if necessary */
976ed2dbd31SArchie Cobbs 	ng_bridge_rehash(priv);
977ed2dbd31SArchie Cobbs 	return (1);
978ed2dbd31SArchie Cobbs }
979ed2dbd31SArchie Cobbs 
980ed2dbd31SArchie Cobbs /*
981ed2dbd31SArchie Cobbs  * Resize the hash table. We try to maintain the number of buckets
982ed2dbd31SArchie Cobbs  * such that the load factor is in the range 0.25 to 1.0.
983ed2dbd31SArchie Cobbs  *
984ed2dbd31SArchie Cobbs  * If we can't get the new memory then we silently fail. This is OK
985ed2dbd31SArchie Cobbs  * because things will still work and we'll try again soon anyway.
986ed2dbd31SArchie Cobbs  */
987ed2dbd31SArchie Cobbs static void
988ed2dbd31SArchie Cobbs ng_bridge_rehash(priv_p priv)
989ed2dbd31SArchie Cobbs {
990ed2dbd31SArchie Cobbs 	struct ng_bridge_bucket *newTab;
991ed2dbd31SArchie Cobbs 	int oldBucket, newBucket;
992ed2dbd31SArchie Cobbs 	int newNumBuckets;
993ed2dbd31SArchie Cobbs 	u_int newMask;
994ed2dbd31SArchie Cobbs 
995ed2dbd31SArchie Cobbs 	/* Is table too full or too empty? */
996ed2dbd31SArchie Cobbs 	if (priv->numHosts > priv->numBuckets
997ed2dbd31SArchie Cobbs 	    && (priv->numBuckets << 1) <= MAX_BUCKETS)
998ed2dbd31SArchie Cobbs 		newNumBuckets = priv->numBuckets << 1;
999ed2dbd31SArchie Cobbs 	else if (priv->numHosts < (priv->numBuckets >> 2)
1000ed2dbd31SArchie Cobbs 	    && (priv->numBuckets >> 2) >= MIN_BUCKETS)
1001ed2dbd31SArchie Cobbs 		newNumBuckets = priv->numBuckets >> 2;
1002ed2dbd31SArchie Cobbs 	else
1003ed2dbd31SArchie Cobbs 		return;
1004ed2dbd31SArchie Cobbs 	newMask = newNumBuckets - 1;
1005ed2dbd31SArchie Cobbs 
1006ed2dbd31SArchie Cobbs 	/* Allocate and initialize new table */
1007ac2fffa4SPedro F. Giffuni 	newTab = malloc(newNumBuckets * sizeof(*newTab),
1008e11e3f18SDag-Erling Smørgrav 	    M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
1009ed2dbd31SArchie Cobbs 	if (newTab == NULL)
1010ed2dbd31SArchie Cobbs 		return;
1011ed2dbd31SArchie Cobbs 
1012ed2dbd31SArchie Cobbs 	/* Move all entries from old table to new table */
1013ed2dbd31SArchie Cobbs 	for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
1014ed2dbd31SArchie Cobbs 		struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
1015ed2dbd31SArchie Cobbs 
1016ed2dbd31SArchie Cobbs 		while (!SLIST_EMPTY(oldList)) {
1017ccf4cd2eSLutz Donnerhacke 			struct ng_bridge_host *const host
1018ed2dbd31SArchie Cobbs 			    = SLIST_FIRST(oldList);
1019ed2dbd31SArchie Cobbs 
1020ed2dbd31SArchie Cobbs 			SLIST_REMOVE_HEAD(oldList, next);
1021ccf4cd2eSLutz Donnerhacke 			newBucket = HASH(host->addr, newMask);
1022ccf4cd2eSLutz Donnerhacke 			SLIST_INSERT_HEAD(&newTab[newBucket], host, next);
1023ed2dbd31SArchie Cobbs 		}
1024ed2dbd31SArchie Cobbs 	}
1025ed2dbd31SArchie Cobbs 
1026ed2dbd31SArchie Cobbs 	/* Replace old table with new one */
1027ed2dbd31SArchie Cobbs 	if (priv->conf.debugLevel >= 3) {
1028ed2dbd31SArchie Cobbs 		log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
1029ed2dbd31SArchie Cobbs 		    ng_bridge_nodename(priv->node),
1030ed2dbd31SArchie Cobbs 		    priv->numBuckets, newNumBuckets);
1031ed2dbd31SArchie Cobbs 	}
10321ede983cSDag-Erling Smørgrav 	free(priv->tab, M_NETGRAPH_BRIDGE);
1033ed2dbd31SArchie Cobbs 	priv->numBuckets = newNumBuckets;
1034ed2dbd31SArchie Cobbs 	priv->hashMask = newMask;
1035ed2dbd31SArchie Cobbs 	priv->tab = newTab;
1036ed2dbd31SArchie Cobbs 	return;
1037ed2dbd31SArchie Cobbs }
1038ed2dbd31SArchie Cobbs 
1039ed2dbd31SArchie Cobbs /******************************************************************
1040ed2dbd31SArchie Cobbs 		    MISC FUNCTIONS
1041ed2dbd31SArchie Cobbs ******************************************************************/
1042ed2dbd31SArchie Cobbs 
1043ed2dbd31SArchie Cobbs /*
1044ed2dbd31SArchie Cobbs  * Remove all hosts associated with a specific link from the hashtable.
1045ed2dbd31SArchie Cobbs  * If linkNum == -1, then remove all hosts in the table.
1046ed2dbd31SArchie Cobbs  */
1047ed2dbd31SArchie Cobbs static void
1048631cabbaSGleb Smirnoff ng_bridge_remove_hosts(priv_p priv, link_p link)
1049ed2dbd31SArchie Cobbs {
1050ed2dbd31SArchie Cobbs 	int bucket;
1051ed2dbd31SArchie Cobbs 
1052ed2dbd31SArchie Cobbs 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1053ccf4cd2eSLutz Donnerhacke 		struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1054ed2dbd31SArchie Cobbs 
1055ed2dbd31SArchie Cobbs 		while (*hptr != NULL) {
1056ccf4cd2eSLutz Donnerhacke 			struct ng_bridge_host *const host = *hptr;
1057ed2dbd31SArchie Cobbs 
1058ccf4cd2eSLutz Donnerhacke 			if (link == NULL || host->link == link) {
1059ccf4cd2eSLutz Donnerhacke 				*hptr = SLIST_NEXT(host, next);
1060ccf4cd2eSLutz Donnerhacke 				free(host, M_NETGRAPH_BRIDGE);
1061ed2dbd31SArchie Cobbs 				priv->numHosts--;
1062ed2dbd31SArchie Cobbs 			} else
1063ccf4cd2eSLutz Donnerhacke 				hptr = &SLIST_NEXT(host, next);
1064ed2dbd31SArchie Cobbs 		}
1065ed2dbd31SArchie Cobbs 	}
1066ed2dbd31SArchie Cobbs }
1067ed2dbd31SArchie Cobbs 
1068ed2dbd31SArchie Cobbs /*
1069ed2dbd31SArchie Cobbs  * Handle our once-per-second timeout event. We do two things:
1070ed2dbd31SArchie Cobbs  * we decrement link->loopCount for those links being muted due to
1071ed2dbd31SArchie Cobbs  * a detected loopback condition, and we remove any hosts from
1072ed2dbd31SArchie Cobbs  * the hashtable whom we haven't heard from in a long while.
1073ed2dbd31SArchie Cobbs  */
1074631cabbaSGleb Smirnoff static int
10750b951c55SGleb Smirnoff ng_bridge_unmute(hook_p hook, void *arg)
1076631cabbaSGleb Smirnoff {
1077631cabbaSGleb Smirnoff 	link_p link = NG_HOOK_PRIVATE(hook);
1078631cabbaSGleb Smirnoff 	node_p node = NG_HOOK_NODE(hook);
1079631cabbaSGleb Smirnoff 	priv_p priv = NG_NODE_PRIVATE(node);
10800b951c55SGleb Smirnoff 	int *counter = arg;
1081631cabbaSGleb Smirnoff 
1082631cabbaSGleb Smirnoff 	if (link->loopCount != 0) {
1083631cabbaSGleb Smirnoff 		link->loopCount--;
1084631cabbaSGleb Smirnoff 		if (link->loopCount == 0 && priv->conf.debugLevel >= 2) {
1085631cabbaSGleb Smirnoff 			log(LOG_INFO, "ng_bridge: %s:"
1086631cabbaSGleb Smirnoff 			    " restoring looped back %s\n",
1087631cabbaSGleb Smirnoff 			    ng_bridge_nodename(node), NG_HOOK_NAME(hook));
1088631cabbaSGleb Smirnoff 		}
1089631cabbaSGleb Smirnoff 	}
1090abc4b11cSGleb Smirnoff 	(*counter)++;
1091631cabbaSGleb Smirnoff 	return (1);
1092631cabbaSGleb Smirnoff }
1093631cabbaSGleb Smirnoff 
1094ed2dbd31SArchie Cobbs static void
1095e0d32af7SGleb Smirnoff ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1096ed2dbd31SArchie Cobbs {
109730400f03SJulian Elischer 	const priv_p priv = NG_NODE_PRIVATE(node);
1098e0d32af7SGleb Smirnoff 	int bucket;
1099ed2dbd31SArchie Cobbs 	int counter = 0;
1100631cabbaSGleb Smirnoff 	hook_p ret;
1101ed2dbd31SArchie Cobbs 
1102ed2dbd31SArchie Cobbs 	/* Update host time counters and remove stale entries */
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 
1109ed2dbd31SArchie Cobbs 			/* Remove hosts we haven't heard from in a while */
1110ccf4cd2eSLutz Donnerhacke 			if (++host->staleness >= priv->conf.maxStaleness) {
1111ccf4cd2eSLutz Donnerhacke 				*hptr = SLIST_NEXT(host, next);
1112ccf4cd2eSLutz Donnerhacke 				free(host, M_NETGRAPH_BRIDGE);
1113ed2dbd31SArchie Cobbs 				priv->numHosts--;
1114ed2dbd31SArchie Cobbs 			} else {
1115ccf4cd2eSLutz Donnerhacke 				if (host->age < 0xffff)
1116ccf4cd2eSLutz Donnerhacke 					host->age++;
1117ccf4cd2eSLutz Donnerhacke 				hptr = &SLIST_NEXT(host, next);
1118ed2dbd31SArchie Cobbs 				counter++;
1119ed2dbd31SArchie Cobbs 			}
1120ed2dbd31SArchie Cobbs 		}
1121ed2dbd31SArchie Cobbs 	}
1122ed2dbd31SArchie Cobbs 	KASSERT(priv->numHosts == counter,
11236e551fb6SDavid E. O'Brien 	    ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
1124ed2dbd31SArchie Cobbs 
1125ed2dbd31SArchie Cobbs 	/* Decrease table size if necessary */
1126ed2dbd31SArchie Cobbs 	ng_bridge_rehash(priv);
1127ed2dbd31SArchie Cobbs 
1128ed2dbd31SArchie Cobbs 	/* Decrease loop counter on muted looped back links */
1129631cabbaSGleb Smirnoff 	counter = 0;
1130631cabbaSGleb Smirnoff 	NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter, ret);
1131ed2dbd31SArchie Cobbs 	KASSERT(priv->numLinks == counter,
11326e551fb6SDavid E. O'Brien 	    ("%s: links: %d != %d", __func__, priv->numLinks, counter));
1133ed2dbd31SArchie Cobbs 
1134e0d32af7SGleb Smirnoff 	/* Register a new timeout, keeping the existing node reference */
1135e0d32af7SGleb Smirnoff 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
1136ed2dbd31SArchie Cobbs }
1137ed2dbd31SArchie Cobbs 
1138ed2dbd31SArchie Cobbs /*
1139ed2dbd31SArchie Cobbs  * Return node's "name", even if it doesn't have one.
1140ed2dbd31SArchie Cobbs  */
1141ed2dbd31SArchie Cobbs static const char *
11426117aa58SLutz Donnerhacke ng_bridge_nodename(node_cp node)
1143ed2dbd31SArchie Cobbs {
114487e2c66aSHartmut Brandt 	static char name[NG_NODESIZ];
1145ed2dbd31SArchie Cobbs 
1146dd10c1e2SGleb Smirnoff 	if (NG_NODE_HAS_NAME(node))
114730400f03SJulian Elischer 		snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
1148ed2dbd31SArchie Cobbs 	else
1149ed2dbd31SArchie Cobbs 		snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
1150ed2dbd31SArchie Cobbs 	return name;
1151ed2dbd31SArchie Cobbs }
1152