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