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