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