xref: /freebsd/sys/netgraph/ng_bridge.c (revision a0409676120c1e558d0ade943019934e0f15118d)
1 /*-
2  * Copyright (c) 2000 Whistle Communications, Inc.
3  * All rights reserved.
4  *
5  * Subject to the following obligations and disclaimer of warranty, use and
6  * redistribution of this software, in source or object code forms, with or
7  * without modifications are expressly permitted by Whistle Communications;
8  * provided, however, that:
9  * 1. Any and all reproductions of the source or object code must include the
10  *    copyright notice above and the following disclaimer of warranties; and
11  * 2. No rights are granted, in any manner or form, to use Whistle
12  *    Communications, Inc. trademarks, including the mark "WHISTLE
13  *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
14  *    such appears in the above copyright notice or in the software.
15  *
16  * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
17  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
18  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
19  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
21  * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
22  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
23  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
24  * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
25  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
26  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
32  * OF SUCH DAMAGE.
33  *
34  * Author: Archie Cobbs <archie@freebsd.org>
35  *
36  * $FreeBSD$
37  */
38 
39 /*
40  * ng_bridge(4) netgraph node type
41  *
42  * The node performs standard intelligent Ethernet bridging over
43  * each of its connected hooks, or links.  A simple loop detection
44  * algorithm is included which disables a link for priv->conf.loopTimeout
45  * seconds when a host is seen to have jumped from one link to
46  * another within priv->conf.minStableAge seconds.
47  *
48  * We keep a hashtable that maps Ethernet addresses to host info,
49  * which is contained in struct ng_bridge_host's. These structures
50  * tell us on which link the host may be found. A host's entry will
51  * expire after priv->conf.maxStaleness seconds.
52  *
53  * This node is optimzed for stable networks, where machines jump
54  * from one port to the other only rarely.
55  */
56 
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/lock.h>
61 #include <sys/malloc.h>
62 #include <sys/mbuf.h>
63 #include <sys/errno.h>
64 #include <sys/rwlock.h>
65 #include <sys/syslog.h>
66 #include <sys/socket.h>
67 #include <sys/ctype.h>
68 #include <sys/types.h>
69 #include <sys/counter.h>
70 
71 #include <net/if.h>
72 #include <net/if_var.h>
73 #include <net/ethernet.h>
74 #include <net/vnet.h>
75 
76 #include <netinet/in.h>
77 #if 0	/* not used yet */
78 #include <netinet/ip_fw.h>
79 #endif
80 #include <netgraph/ng_message.h>
81 #include <netgraph/netgraph.h>
82 #include <netgraph/ng_parse.h>
83 #include <netgraph/ng_bridge.h>
84 
85 #ifdef NG_SEPARATE_MALLOC
86 static MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge",
87     "netgraph bridge node");
88 #else
89 #define M_NETGRAPH_BRIDGE M_NETGRAPH
90 #endif
91 
92 /* Counter based stats */
93 struct ng_bridge_link_kernel_stats {
94 	counter_u64_t	recvOctets;	/* total octets rec'd on link */
95 	counter_u64_t	recvPackets;	/* total pkts rec'd on link */
96 	counter_u64_t	recvMulticasts;	/* multicast pkts rec'd on link */
97 	counter_u64_t	recvBroadcasts;	/* broadcast pkts rec'd on link */
98 	counter_u64_t	recvUnknown;	/* pkts rec'd with unknown dest addr */
99 	counter_u64_t	recvRunts;	/* pkts rec'd less than 14 bytes */
100 	counter_u64_t	recvInvalid;	/* pkts rec'd with bogus source addr */
101 	counter_u64_t	xmitOctets;	/* total octets xmit'd on link */
102 	counter_u64_t	xmitPackets;	/* total pkts xmit'd on link */
103 	counter_u64_t	xmitMulticasts;	/* multicast pkts xmit'd on link */
104 	counter_u64_t	xmitBroadcasts;	/* broadcast pkts xmit'd on link */
105 	counter_u64_t	loopDrops;	/* pkts dropped due to loopback */
106 	counter_u64_t	loopDetects;	/* number of loop detections */
107 	counter_u64_t	memoryFailures;	/* times couldn't get mem or mbuf */
108 };
109 
110 /* Per-link private data */
111 struct ng_bridge_link {
112 	hook_p				hook;		/* netgraph hook */
113 	u_int16_t			loopCount;	/* loop ignore timer */
114 	unsigned int			learnMac : 1,   /* autolearn macs */
115 					sendUnknown : 1;/* send unknown macs out */
116 	struct ng_bridge_link_kernel_stats stats;	/* link stats */
117 };
118 typedef struct ng_bridge_link const *link_cp;	/* read only access */
119 
120 /* Per-node private data */
121 struct ng_bridge_private {
122 	struct ng_bridge_bucket	*tab;		/* hash table bucket array */
123 	struct ng_bridge_config	conf;		/* node configuration */
124 	node_p			node;		/* netgraph node */
125 	u_int			numHosts;	/* num entries in table */
126 	u_int			numBuckets;	/* num buckets in table */
127 	u_int			hashMask;	/* numBuckets - 1 */
128 	int			numLinks;	/* num connected links */
129 	unsigned int		persistent : 1,	/* can exist w/o hooks */
130 				sendUnknown : 1;/* links receive unknowns by default */
131 	struct callout		timer;		/* one second periodic timer */
132 };
133 typedef struct ng_bridge_private *priv_p;
134 typedef struct ng_bridge_private const *priv_cp;	/* read only access */
135 
136 /* Information about a host, stored in a hash table entry */
137 struct ng_bridge_host {
138 	u_char		addr[6];	/* ethernet address */
139 	link_p		link;		/* link where addr can be found */
140 	u_int16_t	age;		/* seconds ago entry was created */
141 	u_int16_t	staleness;	/* seconds ago host last heard from */
142 	SLIST_ENTRY(ng_bridge_host)	next;	/* next entry in bucket */
143 };
144 
145 /* Hash table bucket declaration */
146 SLIST_HEAD(ng_bridge_bucket, ng_bridge_host);
147 
148 /* Netgraph node methods */
149 static ng_constructor_t	ng_bridge_constructor;
150 static ng_rcvmsg_t	ng_bridge_rcvmsg;
151 static ng_shutdown_t	ng_bridge_shutdown;
152 static ng_newhook_t	ng_bridge_newhook;
153 static ng_rcvdata_t	ng_bridge_rcvdata;
154 static ng_disconnect_t	ng_bridge_disconnect;
155 
156 /* Other internal functions */
157 static struct	ng_bridge_host *ng_bridge_get(priv_cp priv, const u_char *addr);
158 static int	ng_bridge_put(priv_p priv, const u_char *addr, link_p link);
159 static void	ng_bridge_rehash(priv_p priv);
160 static void	ng_bridge_remove_hosts(priv_p priv, link_p link);
161 static void	ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2);
162 static const	char *ng_bridge_nodename(node_cp node);
163 
164 /* Ethernet broadcast */
165 static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
166     { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
167 
168 /* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
169 #define ETHER_EQUAL(a,b)	(((const u_int32_t *)(a))[0] \
170 					== ((const u_int32_t *)(b))[0] \
171 				    && ((const u_int16_t *)(a))[2] \
172 					== ((const u_int16_t *)(b))[2])
173 
174 /* Minimum and maximum number of hash buckets. Must be a power of two. */
175 #define MIN_BUCKETS		(1 << 5)	/* 32 */
176 #define MAX_BUCKETS		(1 << 14)	/* 16384 */
177 
178 /* Configuration default values */
179 #define DEFAULT_LOOP_TIMEOUT	60
180 #define DEFAULT_MAX_STALENESS	(15 * 60)	/* same as ARP timeout */
181 #define DEFAULT_MIN_STABLE_AGE	1
182 
183 /******************************************************************
184 		    NETGRAPH PARSE TYPES
185 ******************************************************************/
186 
187 /*
188  * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
189  */
190 static int
191 ng_bridge_getTableLength(const struct ng_parse_type *type,
192 	const u_char *start, const u_char *buf)
193 {
194 	const struct ng_bridge_host_ary *const hary
195 	    = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
196 
197 	return hary->numHosts;
198 }
199 
200 /* Parse type for struct ng_bridge_host_ary */
201 static const struct ng_parse_struct_field ng_bridge_host_type_fields[]
202 	= NG_BRIDGE_HOST_TYPE_INFO(&ng_parse_enaddr_type);
203 static const struct ng_parse_type ng_bridge_host_type = {
204 	&ng_parse_struct_type,
205 	&ng_bridge_host_type_fields
206 };
207 static const struct ng_parse_array_info ng_bridge_hary_type_info = {
208 	&ng_bridge_host_type,
209 	ng_bridge_getTableLength
210 };
211 static const struct ng_parse_type ng_bridge_hary_type = {
212 	&ng_parse_array_type,
213 	&ng_bridge_hary_type_info
214 };
215 static const struct ng_parse_struct_field ng_bridge_host_ary_type_fields[]
216 	= NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
217 static const struct ng_parse_type ng_bridge_host_ary_type = {
218 	&ng_parse_struct_type,
219 	&ng_bridge_host_ary_type_fields
220 };
221 
222 /* Parse type for struct ng_bridge_config */
223 static const struct ng_parse_struct_field ng_bridge_config_type_fields[]
224 	= NG_BRIDGE_CONFIG_TYPE_INFO;
225 static const struct ng_parse_type ng_bridge_config_type = {
226 	&ng_parse_struct_type,
227 	&ng_bridge_config_type_fields
228 };
229 
230 /* Parse type for struct ng_bridge_link_stat */
231 static const struct ng_parse_struct_field ng_bridge_stats_type_fields[]
232 	= NG_BRIDGE_STATS_TYPE_INFO;
233 static const struct ng_parse_type ng_bridge_stats_type = {
234 	&ng_parse_struct_type,
235 	&ng_bridge_stats_type_fields
236 };
237 
238 /* List of commands and how to convert arguments to/from ASCII */
239 static const struct ng_cmdlist ng_bridge_cmdlist[] = {
240 	{
241 	  NGM_BRIDGE_COOKIE,
242 	  NGM_BRIDGE_SET_CONFIG,
243 	  "setconfig",
244 	  &ng_bridge_config_type,
245 	  NULL
246 	},
247 	{
248 	  NGM_BRIDGE_COOKIE,
249 	  NGM_BRIDGE_GET_CONFIG,
250 	  "getconfig",
251 	  NULL,
252 	  &ng_bridge_config_type
253 	},
254 	{
255 	  NGM_BRIDGE_COOKIE,
256 	  NGM_BRIDGE_RESET,
257 	  "reset",
258 	  NULL,
259 	  NULL
260 	},
261 	{
262 	  NGM_BRIDGE_COOKIE,
263 	  NGM_BRIDGE_GET_STATS,
264 	  "getstats",
265 	  &ng_parse_uint32_type,
266 	  &ng_bridge_stats_type
267 	},
268 	{
269 	  NGM_BRIDGE_COOKIE,
270 	  NGM_BRIDGE_CLR_STATS,
271 	  "clrstats",
272 	  &ng_parse_uint32_type,
273 	  NULL
274 	},
275 	{
276 	  NGM_BRIDGE_COOKIE,
277 	  NGM_BRIDGE_GETCLR_STATS,
278 	  "getclrstats",
279 	  &ng_parse_uint32_type,
280 	  &ng_bridge_stats_type
281 	},
282 	{
283 	  NGM_BRIDGE_COOKIE,
284 	  NGM_BRIDGE_GET_TABLE,
285 	  "gettable",
286 	  NULL,
287 	  &ng_bridge_host_ary_type
288 	},
289 	{
290 	  NGM_BRIDGE_COOKIE,
291 	  NGM_BRIDGE_SET_PERSISTENT,
292 	  "setpersistent",
293 	  NULL,
294 	  NULL
295 	},
296 	{ 0 }
297 };
298 
299 /* Node type descriptor */
300 static struct ng_type ng_bridge_typestruct = {
301 	.version =	NG_ABI_VERSION,
302 	.name =		NG_BRIDGE_NODE_TYPE,
303 	.constructor =	ng_bridge_constructor,
304 	.rcvmsg =	ng_bridge_rcvmsg,
305 	.shutdown =	ng_bridge_shutdown,
306 	.newhook =	ng_bridge_newhook,
307 	.rcvdata =	ng_bridge_rcvdata,
308 	.disconnect =	ng_bridge_disconnect,
309 	.cmdlist =	ng_bridge_cmdlist,
310 };
311 NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
312 
313 /******************************************************************
314 		    NETGRAPH NODE METHODS
315 ******************************************************************/
316 
317 /*
318  * Node constructor
319  */
320 static int
321 ng_bridge_constructor(node_p node)
322 {
323 	priv_p priv;
324 
325 	/* Allocate and initialize private info */
326 	priv = malloc(sizeof(*priv), M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
327 	ng_callout_init(&priv->timer);
328 
329 	/* Allocate and initialize hash table, etc. */
330 	priv->tab = malloc(MIN_BUCKETS * sizeof(*priv->tab),
331 	    M_NETGRAPH_BRIDGE, M_WAITOK | M_ZERO);
332 	priv->numBuckets = MIN_BUCKETS;
333 	priv->hashMask = MIN_BUCKETS - 1;
334 	priv->conf.debugLevel = 1;
335 	priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
336 	priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
337 	priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
338 	priv->sendUnknown = 1;	       /* classic bridge */
339 
340 	/*
341 	 * This node has all kinds of stuff that could be screwed by SMP.
342 	 * Until it gets it's own internal protection, we go through in
343 	 * single file. This could hurt a machine bridging between two
344 	 * GB ethernets so it should be fixed.
345 	 * When it's fixed the process SHOULD NOT SLEEP, spinlocks please!
346 	 * (and atomic ops )
347 	 */
348 	NG_NODE_FORCE_WRITER(node);
349 	NG_NODE_SET_PRIVATE(node, priv);
350 	priv->node = node;
351 
352 	/* Start timer; timer is always running while node is alive */
353 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
354 
355 	/* Done */
356 	return (0);
357 }
358 
359 /*
360  * Method for attaching a new hook
361  */
362 static	int
363 ng_bridge_newhook(node_p node, hook_p hook, const char *name)
364 {
365 	const priv_p priv = NG_NODE_PRIVATE(node);
366 	char linkName[NG_HOOKSIZ];
367 	u_int32_t linkNum;
368 	link_p link;
369 	const char *prefix = NG_BRIDGE_HOOK_LINK_PREFIX;
370 	bool isUplink;
371 
372 	/* Check for a link hook */
373 	if (strlen(name) <= strlen(prefix))
374 		return (EINVAL);       /* Unknown hook name */
375 
376 	isUplink = (name[0] == 'u');
377 	if (isUplink)
378 		prefix = NG_BRIDGE_HOOK_UPLINK_PREFIX;
379 
380 	/* primitive parsing */
381 	linkNum = strtoul(name + strlen(prefix), NULL, 10);
382 	/* validation by comparing against the reconstucted name  */
383 	snprintf(linkName, sizeof(linkName), "%s%u", prefix, linkNum);
384 	if (strcmp(linkName, name) != 0)
385 		return (EINVAL);
386 
387 	if (linkNum == 0 && isUplink)
388 		return (EINVAL);
389 
390 	if(NG_PEER_NODE(hook) == node)
391 	        return (ELOOP);
392 
393 	link = malloc(sizeof(*link), M_NETGRAPH_BRIDGE, M_ZERO);
394 	if (link == NULL)
395 		return (ENOMEM);
396 
397 	link->stats.recvOctets = counter_u64_alloc(M_WAITOK);
398 	link->stats.recvPackets = counter_u64_alloc(M_WAITOK);
399 	link->stats.recvMulticasts = counter_u64_alloc(M_WAITOK);
400 	link->stats.recvBroadcasts = counter_u64_alloc(M_WAITOK);
401 	link->stats.recvUnknown = counter_u64_alloc(M_WAITOK);
402 	link->stats.recvRunts = counter_u64_alloc(M_WAITOK);
403 	link->stats.recvInvalid = counter_u64_alloc(M_WAITOK);
404 	link->stats.xmitOctets = counter_u64_alloc(M_WAITOK);
405 	link->stats.xmitPackets = counter_u64_alloc(M_WAITOK);
406 	link->stats.xmitMulticasts = counter_u64_alloc(M_WAITOK);
407 	link->stats.xmitBroadcasts = counter_u64_alloc(M_WAITOK);
408 	link->stats.loopDrops = counter_u64_alloc(M_WAITOK);
409 	link->stats.loopDetects = counter_u64_alloc(M_WAITOK);
410 	link->stats.memoryFailures = counter_u64_alloc(M_WAITOK);
411 
412 	link->hook = hook;
413 	if (isUplink) {
414 		link->learnMac = 0;
415 		link->sendUnknown = 1;
416 		if (priv->numLinks == 0)	/* if the first link is an uplink */
417 		    priv->sendUnknown = 0;	/* switch to restrictive mode */
418 	} else {
419 		link->learnMac = 1;
420 		link->sendUnknown = priv->sendUnknown;
421 	}
422 
423 	NG_HOOK_SET_PRIVATE(hook, link);
424 	priv->numLinks++;
425 	return (0);
426 }
427 
428 /*
429  * Receive a control message
430  */
431 static void ng_bridge_clear_link_stats(struct ng_bridge_link_kernel_stats * p)
432 {
433 	counter_u64_zero(p->recvOctets);
434 	counter_u64_zero(p->recvPackets);
435 	counter_u64_zero(p->recvMulticasts);
436 	counter_u64_zero(p->recvBroadcasts);
437 	counter_u64_zero(p->recvUnknown);
438 	counter_u64_zero(p->recvRunts);
439 	counter_u64_zero(p->recvInvalid);
440 	counter_u64_zero(p->xmitOctets);
441 	counter_u64_zero(p->xmitPackets);
442 	counter_u64_zero(p->xmitMulticasts);
443 	counter_u64_zero(p->xmitBroadcasts);
444 	counter_u64_zero(p->loopDrops);
445 	counter_u64_zero(p->loopDetects);
446 	counter_u64_zero(p->memoryFailures);
447 };
448 
449 static int
450 ng_bridge_reset_link(hook_p hook, void *arg __unused)
451 {
452 	link_p priv = NG_HOOK_PRIVATE(hook);
453 
454 	priv->loopCount = 0;
455 	ng_bridge_clear_link_stats(&priv->stats);
456 	return (1);
457 }
458 
459 static int
460 ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
461 {
462 	const priv_p priv = NG_NODE_PRIVATE(node);
463 	struct ng_mesg *resp = NULL;
464 	int error = 0;
465 	struct ng_mesg *msg;
466 
467 	NGI_GET_MSG(item, msg);
468 	switch (msg->header.typecookie) {
469 	case NGM_BRIDGE_COOKIE:
470 		switch (msg->header.cmd) {
471 		case NGM_BRIDGE_GET_CONFIG:
472 		    {
473 			struct ng_bridge_config *conf;
474 
475 			NG_MKRESPONSE(resp, msg,
476 			    sizeof(struct ng_bridge_config), M_NOWAIT);
477 			if (resp == NULL) {
478 				error = ENOMEM;
479 				break;
480 			}
481 			conf = (struct ng_bridge_config *)resp->data;
482 			*conf = priv->conf;	/* no sanity checking needed */
483 			break;
484 		    }
485 		case NGM_BRIDGE_SET_CONFIG:
486 		    {
487 			struct ng_bridge_config *conf;
488 
489 			if (msg->header.arglen
490 			    != sizeof(struct ng_bridge_config)) {
491 				error = EINVAL;
492 				break;
493 			}
494 			conf = (struct ng_bridge_config *)msg->data;
495 			priv->conf = *conf;
496 			break;
497 		    }
498 		case NGM_BRIDGE_RESET:
499 		    {
500 			hook_p rethook;
501 
502 			/* Flush all entries in the hash table */
503 			ng_bridge_remove_hosts(priv, NULL);
504 
505 			/* Reset all loop detection counters and stats */
506 			NG_NODE_FOREACH_HOOK(node, ng_bridge_reset_link, NULL,
507 			    rethook);
508 			break;
509 		    }
510 		case NGM_BRIDGE_GET_STATS:
511 		case NGM_BRIDGE_CLR_STATS:
512 		case NGM_BRIDGE_GETCLR_STATS:
513 		    {
514 			hook_p hook;
515 			link_p link;
516 			char linkName[NG_HOOKSIZ];
517 			int linkNum;
518 
519 			/* Get link number */
520 			if (msg->header.arglen != sizeof(u_int32_t)) {
521 				error = EINVAL;
522 				break;
523 			}
524 			linkNum = *((int32_t *)msg->data);
525 			if (linkNum < 0)
526 				snprintf(linkName, sizeof(linkName),
527 				    "%s%u", NG_BRIDGE_HOOK_UPLINK_PREFIX, -linkNum);
528 			else
529 				snprintf(linkName, sizeof(linkName),
530 				    "%s%u", NG_BRIDGE_HOOK_LINK_PREFIX, linkNum);
531 
532 			if ((hook = ng_findhook(node, linkName)) == NULL) {
533 				error = ENOTCONN;
534 				break;
535 			}
536 			link = NG_HOOK_PRIVATE(hook);
537 
538 			/* Get/clear stats */
539 			if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
540 				struct ng_bridge_link_stats *rs;
541 
542 				NG_MKRESPONSE(resp, msg,
543 				    sizeof(link->stats), M_NOWAIT);
544 				if (resp == NULL) {
545 					error = ENOMEM;
546 					break;
547 				}
548 				rs = (struct ng_bridge_link_stats *)resp->data;
549 #define FETCH(x)	rs->x = counter_u64_fetch(link->stats.x)
550 				FETCH(recvOctets);
551 				FETCH(recvPackets);
552 				FETCH(recvMulticasts);
553 				FETCH(recvBroadcasts);
554 				FETCH(recvUnknown);
555 				FETCH(recvRunts);
556 				FETCH(recvInvalid);
557 				FETCH(xmitOctets);
558 				FETCH(xmitPackets);
559 				FETCH(xmitMulticasts);
560 				FETCH(xmitBroadcasts);
561 				FETCH(loopDrops);
562 				FETCH(loopDetects);
563 				FETCH(memoryFailures);
564 #undef FETCH
565 			}
566 			if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
567 				ng_bridge_clear_link_stats(&link->stats);
568 			break;
569 		    }
570 		case NGM_BRIDGE_GET_TABLE:
571 		    {
572 			struct ng_bridge_host_ary *ary;
573 			struct ng_bridge_host *host;
574 			int i = 0, bucket;
575 
576 			NG_MKRESPONSE(resp, msg, sizeof(*ary)
577 			    + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
578 			if (resp == NULL) {
579 				error = ENOMEM;
580 				break;
581 			}
582 			ary = (struct ng_bridge_host_ary *)resp->data;
583 			ary->numHosts = priv->numHosts;
584 			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
585 				SLIST_FOREACH(host, &priv->tab[bucket], next) {
586 					memcpy(ary->hosts[i].addr,
587 					       host->addr,
588 					       sizeof(ary->hosts[i].addr));
589 					ary->hosts[i].age       = host->age;
590 					ary->hosts[i].staleness = host->staleness;
591 					strncpy(ary->hosts[i].hook,
592 						NG_HOOK_NAME(host->link->hook),
593 						sizeof(ary->hosts[i].hook));
594 					i++;
595 				}
596 			}
597 			break;
598 		    }
599 		case NGM_BRIDGE_SET_PERSISTENT:
600 		    {
601 			priv->persistent = 1;
602 			break;
603 		    }
604 		default:
605 			error = EINVAL;
606 			break;
607 		}
608 		break;
609 	default:
610 		error = EINVAL;
611 		break;
612 	}
613 
614 	/* Done */
615 	NG_RESPOND_MSG(error, node, item, resp);
616 	NG_FREE_MSG(msg);
617 	return (error);
618 }
619 
620 /*
621  * Receive data on a hook
622  */
623 struct ng_bridge_send_ctx {
624 	link_p foundFirst, incoming;
625 	struct mbuf * m;
626 	int manycast, error;
627 };
628 
629 /*
630  * Update stats and send out
631  */
632 static inline int
633 ng_bridge_send_data(link_cp dst, int manycast, struct mbuf *m, item_p item) {
634 	int error = 0;
635 	size_t len = m->m_pkthdr.len;
636 
637 	if(item != NULL)
638 		NG_FWD_NEW_DATA(error, item, dst->hook, m);
639 	else
640 		NG_SEND_DATA_ONLY(error, dst->hook, m);
641 
642 	if (error == 0) {
643 		counter_u64_add(dst->stats.xmitPackets, 1);
644 		counter_u64_add(dst->stats.xmitOctets, len);
645 		switch (manycast) {
646 		default:		       /* unknown unicast */
647 			break;
648 		case 1:			       /* multicast */
649 			counter_u64_add(dst->stats.xmitMulticasts, 1);
650 			break;
651 		case 2:			       /* broadcast */
652 			counter_u64_add(dst->stats.xmitBroadcasts, 1);
653 			break;
654 		}
655 	}
656 
657 	return (error);
658 }
659 
660 /*
661  * Loop body for sending to multiple destinations
662  * return 0 to stop looping
663  */
664 static int
665 ng_bridge_send_ctx(hook_p dst, void *arg)
666 {
667 	struct ng_bridge_send_ctx *ctx = arg;
668 	link_p destLink = NG_HOOK_PRIVATE(dst);
669 	struct mbuf *m2 = NULL;
670 	int error = 0;
671 
672 	/* Skip incoming link */
673 	if (destLink == ctx->incoming) {
674 		return (1);
675 	}
676 
677 	/* Skip sending unknowns to undesired links  */
678 	if (!ctx->manycast && !destLink->sendUnknown)
679 		return (1);
680 
681 	if (ctx->foundFirst == NULL) {
682 		/*
683 		 * This is the first usable link we have found.
684 		 * Reserve it for the originals.
685 		 * If we never find another we save a copy.
686 		 */
687 		ctx->foundFirst = destLink;
688 		return (1);
689 	}
690 
691 	/*
692 	 * It's usable link but not the reserved (first) one.
693 	 * Copy mbuf info for sending.
694 	 */
695 	m2 = m_dup(ctx->m, M_NOWAIT);	/* XXX m_copypacket() */
696 	if (m2 == NULL) {
697 		counter_u64_add(ctx->incoming->stats.memoryFailures, 1);
698 		ctx->error = ENOBUFS;
699 		return (0);	       /* abort loop */
700 	}
701 
702 	/* Send packet */
703 	error = ng_bridge_send_data(destLink, ctx->manycast, m2, NULL);
704 	if(error)
705 	  ctx->error = error;
706 	return (1);
707 }
708 
709 static int
710 ng_bridge_rcvdata(hook_p hook, item_p item)
711 {
712 	const node_p node = NG_HOOK_NODE(hook);
713 	const priv_p priv = NG_NODE_PRIVATE(node);
714 	struct ng_bridge_host *host;
715 	struct ether_header *eh;
716 	struct ng_bridge_send_ctx ctx = { 0 };
717 	hook_p ret;
718 
719 	NGI_GET_M(item, ctx.m);
720 
721 	ctx.incoming = NG_HOOK_PRIVATE(hook);
722 	/* Sanity check packet and pull up header */
723 	if (ctx.m->m_pkthdr.len < ETHER_HDR_LEN) {
724 		counter_u64_add(ctx.incoming->stats.recvRunts, 1);
725 		NG_FREE_ITEM(item);
726 		NG_FREE_M(ctx.m);
727 		return (EINVAL);
728 	}
729 	if (ctx.m->m_len < ETHER_HDR_LEN && !(ctx.m = m_pullup(ctx.m, ETHER_HDR_LEN))) {
730 		counter_u64_add(ctx.incoming->stats.memoryFailures, 1);
731 		NG_FREE_ITEM(item);
732 		return (ENOBUFS);
733 	}
734 	eh = mtod(ctx.m, struct ether_header *);
735 	if ((eh->ether_shost[0] & 1) != 0) {
736 		counter_u64_add(ctx.incoming->stats.recvInvalid, 1);
737 		NG_FREE_ITEM(item);
738 		NG_FREE_M(ctx.m);
739 		return (EINVAL);
740 	}
741 
742 	/* Is link disabled due to a loopback condition? */
743 	if (ctx.incoming->loopCount != 0) {
744 		counter_u64_add(ctx.incoming->stats.loopDrops, 1);
745 		NG_FREE_ITEM(item);
746 		NG_FREE_M(ctx.m);
747 		return (ELOOP);		/* XXX is this an appropriate error? */
748 	}
749 
750 	/* Update stats */
751 	counter_u64_add(ctx.incoming->stats.recvPackets, 1);
752 	counter_u64_add(ctx.incoming->stats.recvOctets, ctx.m->m_pkthdr.len);
753 	if ((ctx.manycast = (eh->ether_dhost[0] & 1)) != 0) {
754 		if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
755 			counter_u64_add(ctx.incoming->stats.recvBroadcasts, 1);
756 			ctx.manycast = 2;
757 		} else
758 			counter_u64_add(ctx.incoming->stats.recvMulticasts, 1);
759 	}
760 
761 	/* Look up packet's source Ethernet address in hashtable */
762 	if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
763 		/* Update time since last heard from this host.
764 		 * This is safe without locking, because it's
765 		 * the only operation during shared access.
766 		 */
767 		host->staleness = 0;
768 
769 		/* Did host jump to a different link? */
770 		if (host->link != ctx.incoming) {
771 			/*
772 			 * If the host's old link was recently established
773 			 * on the old link and it's already jumped to a new
774 			 * link, declare a loopback condition.
775 			 */
776 			if (host->age < priv->conf.minStableAge) {
777 				/* Log the problem */
778 				if (priv->conf.debugLevel >= 2) {
779 					struct ifnet *ifp = ctx.m->m_pkthdr.rcvif;
780 					char suffix[32];
781 
782 					if (ifp != NULL)
783 						snprintf(suffix, sizeof(suffix),
784 						    " (%s)", ifp->if_xname);
785 					else
786 						*suffix = '\0';
787 					log(LOG_WARNING, "ng_bridge: %s:"
788 					    " loopback detected on %s%s\n",
789 					    ng_bridge_nodename(node),
790 					    NG_HOOK_NAME(hook), suffix);
791 				}
792 
793 				/* Mark link as linka non grata */
794 				ctx.incoming->loopCount = priv->conf.loopTimeout;
795 				counter_u64_add(ctx.incoming->stats.loopDetects, 1);
796 
797 				/* Forget all hosts on this link */
798 				ng_bridge_remove_hosts(priv, ctx.incoming);
799 
800 				/* Drop packet */
801 				counter_u64_add(ctx.incoming->stats.loopDrops, 1);
802 				NG_FREE_ITEM(item);
803 				NG_FREE_M(ctx.m);
804 				return (ELOOP);		/* XXX appropriate? */
805 			}
806 
807 			/* Move host over to new link */
808 			host->link = ctx.incoming;
809 			host->age = 0;
810 		}
811 	} else if (ctx.incoming->learnMac) {
812 		if (!ng_bridge_put(priv, eh->ether_shost, ctx.incoming)) {
813 			counter_u64_add(ctx.incoming->stats.memoryFailures, 1);
814 			NG_FREE_ITEM(item);
815 			NG_FREE_M(ctx.m);
816 			return (ENOMEM);
817 		}
818 	}
819 
820 	/* Run packet through ipfw processing, if enabled */
821 #if 0
822 	if (priv->conf.ipfw[linkNum] && V_fw_enable && V_ip_fw_chk_ptr != NULL) {
823 		/* XXX not implemented yet */
824 	}
825 #endif
826 
827 	/*
828 	 * If unicast and destination host known, deliver to host's link,
829 	 * unless it is the same link as the packet came in on.
830 	 */
831 	if (!ctx.manycast) {
832 		/* Determine packet destination link */
833 		if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
834 			link_p destLink = host->link;
835 
836 			/* If destination same as incoming link, do nothing */
837 			if (destLink == ctx.incoming) {
838 				NG_FREE_ITEM(item);
839 				NG_FREE_M(ctx.m);
840 				return (0);
841 			}
842 
843 			/* Deliver packet out the destination link */
844 			return (ng_bridge_send_data(destLink, ctx.manycast, ctx.m, item));
845 		}
846 
847 		/* Destination host is not known */
848 		counter_u64_add(ctx.incoming->stats.recvUnknown, 1);
849 	}
850 
851 	/* Distribute unknown, multicast, broadcast pkts to all other links */
852 	NG_NODE_FOREACH_HOOK(node, ng_bridge_send_ctx, &ctx, ret);
853 
854 	/* If we never saw a good link, leave. */
855 	if (ctx.foundFirst == NULL || ctx.error != 0) {
856 		NG_FREE_ITEM(item);
857 		NG_FREE_M(ctx.m);
858 		return (ctx.error);
859 	}
860 
861 	/*
862 	 * If we've sent all the others, send the original
863 	 * on the first link we found.
864 	 */
865 	return (ng_bridge_send_data(ctx.foundFirst, ctx.manycast, ctx.m, item));
866 }
867 
868 /*
869  * Shutdown node
870  */
871 static int
872 ng_bridge_shutdown(node_p node)
873 {
874 	const priv_p priv = NG_NODE_PRIVATE(node);
875 
876 	/*
877 	 * Shut down everything including the timer.  Even if the
878 	 * callout has already been dequeued and is about to be
879 	 * run, ng_bridge_timeout() won't be fired as the node
880 	 * is already marked NGF_INVALID, so we're safe to free
881 	 * the node now.
882 	 */
883 	KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
884 	    ("%s: numLinks=%d numHosts=%d",
885 	    __func__, priv->numLinks, priv->numHosts));
886 	ng_uncallout(&priv->timer, node);
887 	NG_NODE_SET_PRIVATE(node, NULL);
888 	NG_NODE_UNREF(node);
889 	free(priv->tab, M_NETGRAPH_BRIDGE);
890 	free(priv, M_NETGRAPH_BRIDGE);
891 	return (0);
892 }
893 
894 /*
895  * Hook disconnection.
896  */
897 static int
898 ng_bridge_disconnect(hook_p hook)
899 {
900 	const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
901 	link_p link = NG_HOOK_PRIVATE(hook);
902 
903 	/* Remove all hosts associated with this link */
904 	ng_bridge_remove_hosts(priv, link);
905 
906 	/* Free associated link information */
907 	counter_u64_free(link->stats.recvOctets);
908 	counter_u64_free(link->stats.recvPackets);
909 	counter_u64_free(link->stats.recvMulticasts);
910 	counter_u64_free(link->stats.recvBroadcasts);
911 	counter_u64_free(link->stats.recvUnknown);
912 	counter_u64_free(link->stats.recvRunts);
913 	counter_u64_free(link->stats.recvInvalid);
914 	counter_u64_free(link->stats.xmitOctets);
915 	counter_u64_free(link->stats.xmitPackets);
916 	counter_u64_free(link->stats.xmitMulticasts);
917 	counter_u64_free(link->stats.xmitBroadcasts);
918 	counter_u64_free(link->stats.loopDrops);
919 	counter_u64_free(link->stats.loopDetects);
920 	counter_u64_free(link->stats.memoryFailures);
921 	free(link, M_NETGRAPH_BRIDGE);
922 	priv->numLinks--;
923 
924 	/* If no more hooks, go away */
925 	if ((NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0)
926 	    && (NG_NODE_IS_VALID(NG_HOOK_NODE(hook)))
927 	    && !priv->persistent) {
928 		ng_rmnode_self(NG_HOOK_NODE(hook));
929 	}
930 	return (0);
931 }
932 
933 /******************************************************************
934 		    HASH TABLE FUNCTIONS
935 ******************************************************************/
936 
937 /*
938  * Hash algorithm
939  */
940 #define HASH(addr,mask)		( (((const u_int16_t *)(addr))[0] 	\
941 				 ^ ((const u_int16_t *)(addr))[1] 	\
942 				 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
943 
944 /*
945  * Find a host entry in the table.
946  */
947 static struct ng_bridge_host *
948 ng_bridge_get(priv_cp priv, const u_char *addr)
949 {
950 	const int bucket = HASH(addr, priv->hashMask);
951 	struct ng_bridge_host *host;
952 
953 	SLIST_FOREACH(host, &priv->tab[bucket], next) {
954 		if (ETHER_EQUAL(host->addr, addr))
955 			return (host);
956 	}
957 	return (NULL);
958 }
959 
960 /*
961  * Add a new host entry to the table. This assumes the host doesn't
962  * already exist in the table. Returns 1 on success, 0 if there
963  * was a memory allocation failure.
964  */
965 static int
966 ng_bridge_put(priv_p priv, const u_char *addr, link_p link)
967 {
968 	const int bucket = HASH(addr, priv->hashMask);
969 	struct ng_bridge_host *host;
970 
971 #ifdef INVARIANTS
972 	/* Assert that entry does not already exist in hashtable */
973 	SLIST_FOREACH(host, &priv->tab[bucket], next) {
974 		KASSERT(!ETHER_EQUAL(host->addr, addr),
975 		    ("%s: entry %6D exists in table", __func__, addr, ":"));
976 	}
977 #endif
978 
979 	/* Allocate and initialize new hashtable entry */
980 	host = malloc(sizeof(*host), M_NETGRAPH_BRIDGE, M_NOWAIT);
981 	if (host == NULL)
982 		return (0);
983 	bcopy(addr, host->addr, ETHER_ADDR_LEN);
984 	host->link = link;
985 	host->staleness = 0;
986 	host->age = 0;
987 
988 	/* Add new element to hash bucket */
989 	SLIST_INSERT_HEAD(&priv->tab[bucket], host, next);
990 	priv->numHosts++;
991 
992 	/* Resize table if necessary */
993 	ng_bridge_rehash(priv);
994 	return (1);
995 }
996 
997 /*
998  * Resize the hash table. We try to maintain the number of buckets
999  * such that the load factor is in the range 0.25 to 1.0.
1000  *
1001  * If we can't get the new memory then we silently fail. This is OK
1002  * because things will still work and we'll try again soon anyway.
1003  */
1004 static void
1005 ng_bridge_rehash(priv_p priv)
1006 {
1007 	struct ng_bridge_bucket *newTab;
1008 	int oldBucket, newBucket;
1009 	int newNumBuckets;
1010 	u_int newMask;
1011 
1012 	/* Is table too full or too empty? */
1013 	if (priv->numHosts > priv->numBuckets
1014 	    && (priv->numBuckets << 1) <= MAX_BUCKETS)
1015 		newNumBuckets = priv->numBuckets << 1;
1016 	else if (priv->numHosts < (priv->numBuckets >> 2)
1017 	    && (priv->numBuckets >> 2) >= MIN_BUCKETS)
1018 		newNumBuckets = priv->numBuckets >> 2;
1019 	else
1020 		return;
1021 	newMask = newNumBuckets - 1;
1022 
1023 	/* Allocate and initialize new table */
1024 	newTab = malloc(newNumBuckets * sizeof(*newTab),
1025 	    M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
1026 	if (newTab == NULL)
1027 		return;
1028 
1029 	/* Move all entries from old table to new table */
1030 	for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
1031 		struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
1032 
1033 		while (!SLIST_EMPTY(oldList)) {
1034 			struct ng_bridge_host *const host
1035 			    = SLIST_FIRST(oldList);
1036 
1037 			SLIST_REMOVE_HEAD(oldList, next);
1038 			newBucket = HASH(host->addr, newMask);
1039 			SLIST_INSERT_HEAD(&newTab[newBucket], host, next);
1040 		}
1041 	}
1042 
1043 	/* Replace old table with new one */
1044 	if (priv->conf.debugLevel >= 3) {
1045 		log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
1046 		    ng_bridge_nodename(priv->node),
1047 		    priv->numBuckets, newNumBuckets);
1048 	}
1049 	free(priv->tab, M_NETGRAPH_BRIDGE);
1050 	priv->numBuckets = newNumBuckets;
1051 	priv->hashMask = newMask;
1052 	priv->tab = newTab;
1053 	return;
1054 }
1055 
1056 /******************************************************************
1057 		    MISC FUNCTIONS
1058 ******************************************************************/
1059 
1060 /*
1061  * Remove all hosts associated with a specific link from the hashtable.
1062  * If linkNum == -1, then remove all hosts in the table.
1063  */
1064 static void
1065 ng_bridge_remove_hosts(priv_p priv, link_p link)
1066 {
1067 	int bucket;
1068 
1069 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1070 		struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1071 
1072 		while (*hptr != NULL) {
1073 			struct ng_bridge_host *const host = *hptr;
1074 
1075 			if (link == NULL || host->link == link) {
1076 				*hptr = SLIST_NEXT(host, next);
1077 				free(host, M_NETGRAPH_BRIDGE);
1078 				priv->numHosts--;
1079 			} else
1080 				hptr = &SLIST_NEXT(host, next);
1081 		}
1082 	}
1083 }
1084 
1085 /*
1086  * Handle our once-per-second timeout event. We do two things:
1087  * we decrement link->loopCount for those links being muted due to
1088  * a detected loopback condition, and we remove any hosts from
1089  * the hashtable whom we haven't heard from in a long while.
1090  */
1091 static int
1092 ng_bridge_unmute(hook_p hook, void *arg)
1093 {
1094 	link_p link = NG_HOOK_PRIVATE(hook);
1095 	node_p node = NG_HOOK_NODE(hook);
1096 	priv_p priv = NG_NODE_PRIVATE(node);
1097 	int *counter = arg;
1098 
1099 	if (link->loopCount != 0) {
1100 		link->loopCount--;
1101 		if (link->loopCount == 0 && priv->conf.debugLevel >= 2) {
1102 			log(LOG_INFO, "ng_bridge: %s:"
1103 			    " restoring looped back %s\n",
1104 			    ng_bridge_nodename(node), NG_HOOK_NAME(hook));
1105 		}
1106 	}
1107 	(*counter)++;
1108 	return (1);
1109 }
1110 
1111 static void
1112 ng_bridge_timeout(node_p node, hook_p hook, void *arg1, int arg2)
1113 {
1114 	const priv_p priv = NG_NODE_PRIVATE(node);
1115 	int bucket;
1116 	int counter = 0;
1117 	hook_p ret;
1118 
1119 	/* Update host time counters and remove stale entries */
1120 	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
1121 		struct ng_bridge_host **hptr = &SLIST_FIRST(&priv->tab[bucket]);
1122 
1123 		while (*hptr != NULL) {
1124 			struct ng_bridge_host *const host = *hptr;
1125 
1126 			/* Remove hosts we haven't heard from in a while */
1127 			if (++host->staleness >= priv->conf.maxStaleness) {
1128 				*hptr = SLIST_NEXT(host, next);
1129 				free(host, M_NETGRAPH_BRIDGE);
1130 				priv->numHosts--;
1131 			} else {
1132 				if (host->age < 0xffff)
1133 					host->age++;
1134 				hptr = &SLIST_NEXT(host, next);
1135 				counter++;
1136 			}
1137 		}
1138 	}
1139 	KASSERT(priv->numHosts == counter,
1140 	    ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
1141 
1142 	/* Decrease table size if necessary */
1143 	ng_bridge_rehash(priv);
1144 
1145 	/* Decrease loop counter on muted looped back links */
1146 	counter = 0;
1147 	NG_NODE_FOREACH_HOOK(node, ng_bridge_unmute, &counter, ret);
1148 	KASSERT(priv->numLinks == counter,
1149 	    ("%s: links: %d != %d", __func__, priv->numLinks, counter));
1150 
1151 	/* Register a new timeout, keeping the existing node reference */
1152 	ng_callout(&priv->timer, node, NULL, hz, ng_bridge_timeout, NULL, 0);
1153 }
1154 
1155 /*
1156  * Return node's "name", even if it doesn't have one.
1157  */
1158 static const char *
1159 ng_bridge_nodename(node_cp node)
1160 {
1161 	static char name[NG_NODESIZ];
1162 
1163 	if (NG_NODE_HAS_NAME(node))
1164 		snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
1165 	else
1166 		snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
1167 	return name;
1168 }
1169