ng_bridge.c revision 87599
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 87599 2001-12-10 08:09:49Z obrien $
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#include <netgraph/ng_ether.h>
81
82#ifdef NG_SEPARATE_MALLOC
83MALLOC_DEFINE(M_NETGRAPH_BRIDGE, "netgraph_bridge", "netgraph bridge node ");
84#else
85#define M_NETGRAPH_BRIDGE M_NETGRAPH
86#endif
87
88/* Per-link private data */
89struct ng_bridge_link {
90	hook_p				hook;		/* netgraph hook */
91	u_int16_t			loopCount;	/* loop ignore timer */
92	struct ng_bridge_link_stats	stats;		/* link stats */
93};
94
95/* Per-node private data */
96struct ng_bridge_private {
97	struct ng_bridge_bucket	*tab;		/* hash table bucket array */
98	struct ng_bridge_link	*links[NG_BRIDGE_MAX_LINKS];
99	struct ng_bridge_config	conf;		/* node configuration */
100	node_p			node;		/* netgraph node */
101	u_int			numHosts;	/* num entries in table */
102	u_int			numBuckets;	/* num buckets in table */
103	u_int			hashMask;	/* numBuckets - 1 */
104	int			numLinks;	/* num connected links */
105	struct callout		timer;		/* one second periodic timer */
106};
107typedef struct ng_bridge_private *priv_p;
108
109/* Information about a host, stored in a hash table entry */
110struct ng_bridge_hent {
111	struct ng_bridge_host		host;	/* actual host info */
112	SLIST_ENTRY(ng_bridge_hent)	next;	/* next entry in bucket */
113};
114
115/* Hash table bucket declaration */
116SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent);
117
118/* Netgraph node methods */
119static ng_constructor_t	ng_bridge_constructor;
120static ng_rcvmsg_t	ng_bridge_rcvmsg;
121static ng_shutdown_t	ng_bridge_shutdown;
122static ng_newhook_t	ng_bridge_newhook;
123static ng_rcvdata_t	ng_bridge_rcvdata;
124static ng_disconnect_t	ng_bridge_disconnect;
125
126/* Other internal functions */
127static struct	ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr);
128static int	ng_bridge_put(priv_p priv, const u_char *addr, int linkNum);
129static void	ng_bridge_rehash(priv_p priv);
130static void	ng_bridge_remove_hosts(priv_p priv, int linkNum);
131static void	ng_bridge_timeout(void *arg);
132static const	char *ng_bridge_nodename(node_p node);
133
134/* Ethernet broadcast */
135static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
136    { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
137
138/* Store each hook's link number in the private field */
139#define LINK_NUM(hook)		(*(u_int16_t *)(&(hook)->private))
140
141/* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
142#define ETHER_EQUAL(a,b)	(((const u_int32_t *)(a))[0] \
143					== ((const u_int32_t *)(b))[0] \
144				    && ((const u_int16_t *)(a))[2] \
145					== ((const u_int16_t *)(b))[2])
146
147/* Minimum and maximum number of hash buckets. Must be a power of two. */
148#define MIN_BUCKETS		(1 << 5)	/* 32 */
149#define MAX_BUCKETS		(1 << 14)	/* 16384 */
150
151/* Configuration default values */
152#define DEFAULT_LOOP_TIMEOUT	60
153#define DEFAULT_MAX_STALENESS	(15 * 60)	/* same as ARP timeout */
154#define DEFAULT_MIN_STABLE_AGE	1
155
156/******************************************************************
157		    NETGRAPH PARSE TYPES
158******************************************************************/
159
160/*
161 * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
162 */
163static int
164ng_bridge_getTableLength(const struct ng_parse_type *type,
165	const u_char *start, const u_char *buf)
166{
167	const struct ng_bridge_host_ary *const hary
168	    = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
169
170	return hary->numHosts;
171}
172
173/* Parse type for struct ng_bridge_host_ary */
174static const struct ng_parse_struct_info ng_bridge_host_type_info
175	= NG_BRIDGE_HOST_TYPE_INFO(&ng_ether_enaddr_type);
176static const struct ng_parse_type ng_bridge_host_type = {
177	&ng_parse_struct_type,
178	&ng_bridge_host_type_info
179};
180static const struct ng_parse_array_info ng_bridge_hary_type_info = {
181	&ng_bridge_host_type,
182	ng_bridge_getTableLength
183};
184static const struct ng_parse_type ng_bridge_hary_type = {
185	&ng_parse_array_type,
186	&ng_bridge_hary_type_info
187};
188static const struct ng_parse_struct_info ng_bridge_host_ary_type_info
189	= NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
190static const struct ng_parse_type ng_bridge_host_ary_type = {
191	&ng_parse_struct_type,
192	&ng_bridge_host_ary_type_info
193};
194
195/* Parse type for struct ng_bridge_config */
196static const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = {
197	&ng_parse_uint8_type,
198	NG_BRIDGE_MAX_LINKS
199};
200static const struct ng_parse_type ng_bridge_ipfwary_type = {
201	&ng_parse_fixedarray_type,
202	&ng_bridge_ipfwary_type_info
203};
204static const struct ng_parse_struct_info ng_bridge_config_type_info
205	= NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type);
206static const struct ng_parse_type ng_bridge_config_type = {
207	&ng_parse_struct_type,
208	&ng_bridge_config_type_info
209};
210
211/* Parse type for struct ng_bridge_link_stat */
212static const struct ng_parse_struct_info
213	ng_bridge_stats_type_info = NG_BRIDGE_STATS_TYPE_INFO;
214static const struct ng_parse_type ng_bridge_stats_type = {
215	&ng_parse_struct_type,
216	&ng_bridge_stats_type_info
217};
218
219/* List of commands and how to convert arguments to/from ASCII */
220static const struct ng_cmdlist ng_bridge_cmdlist[] = {
221	{
222	  NGM_BRIDGE_COOKIE,
223	  NGM_BRIDGE_SET_CONFIG,
224	  "setconfig",
225	  &ng_bridge_config_type,
226	  NULL
227	},
228	{
229	  NGM_BRIDGE_COOKIE,
230	  NGM_BRIDGE_GET_CONFIG,
231	  "getconfig",
232	  NULL,
233	  &ng_bridge_config_type
234	},
235	{
236	  NGM_BRIDGE_COOKIE,
237	  NGM_BRIDGE_RESET,
238	  "reset",
239	  NULL,
240	  NULL
241	},
242	{
243	  NGM_BRIDGE_COOKIE,
244	  NGM_BRIDGE_GET_STATS,
245	  "getstats",
246	  &ng_parse_uint32_type,
247	  &ng_bridge_stats_type
248	},
249	{
250	  NGM_BRIDGE_COOKIE,
251	  NGM_BRIDGE_CLR_STATS,
252	  "clrstats",
253	  &ng_parse_uint32_type,
254	  NULL
255	},
256	{
257	  NGM_BRIDGE_COOKIE,
258	  NGM_BRIDGE_GETCLR_STATS,
259	  "getclrstats",
260	  &ng_parse_uint32_type,
261	  &ng_bridge_stats_type
262	},
263	{
264	  NGM_BRIDGE_COOKIE,
265	  NGM_BRIDGE_GET_TABLE,
266	  "gettable",
267	  NULL,
268	  &ng_bridge_host_ary_type
269	},
270	{ 0 }
271};
272
273/* Node type descriptor */
274static struct ng_type ng_bridge_typestruct = {
275	NG_ABI_VERSION,
276	NG_BRIDGE_NODE_TYPE,
277	NULL,
278	ng_bridge_constructor,
279	ng_bridge_rcvmsg,
280	ng_bridge_shutdown,
281	ng_bridge_newhook,
282	NULL,
283	NULL,
284	ng_bridge_rcvdata,
285	ng_bridge_disconnect,
286	ng_bridge_cmdlist,
287};
288NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
289
290/* Depend on ng_ether so we can use the Ethernet parse type */
291MODULE_DEPEND(ng_bridge, ng_ether, 1, 1, 1);
292
293/******************************************************************
294		    NETGRAPH NODE METHODS
295******************************************************************/
296
297/*
298 * Node constructor
299 */
300static int
301ng_bridge_constructor(node_p node)
302{
303	priv_p priv;
304
305	/* Allocate and initialize private info */
306	MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
307	if (priv == NULL)
308		return (ENOMEM);
309	callout_init(&priv->timer, 0);
310
311	/* Allocate and initialize hash table, etc. */
312	MALLOC(priv->tab, struct ng_bridge_bucket *,
313	    MIN_BUCKETS * sizeof(*priv->tab), M_NETGRAPH_BRIDGE, M_NOWAIT | M_ZERO);
314	if (priv->tab == NULL) {
315		FREE(priv, M_NETGRAPH_BRIDGE);
316		return (ENOMEM);
317	}
318	priv->numBuckets = MIN_BUCKETS;
319	priv->hashMask = MIN_BUCKETS - 1;
320	priv->conf.debugLevel = 1;
321	priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
322	priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
323	priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
324
325	/*
326	 * This node has all kinds of stuff that could be screwed by SMP.
327	 * Until it gets it's own internal protection, we go through in
328	 * single file. This could hurt a machine bridging beteen two
329	 * GB ethernets so it should be fixed.
330	 * When it's fixed the process SHOULD NOT SLEEP, spinlocks please!
331	 * (and atomic ops )
332	 */
333	NG_NODE_FORCE_WRITER(node);
334	NG_NODE_SET_PRIVATE(node, priv);
335	priv->node = node;
336
337	/* Start timer by faking a timeout event */
338	NG_NODE_REF(node); /* because the timeout will drop a reference */
339	ng_bridge_timeout(node);
340	return (0);
341}
342
343/*
344 * Method for attaching a new hook
345 */
346static	int
347ng_bridge_newhook(node_p node, hook_p hook, const char *name)
348{
349	const priv_p priv = NG_NODE_PRIVATE(node);
350
351	/* Check for a link hook */
352	if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX,
353	    strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) {
354		const char *cp;
355		char *eptr;
356		u_long linkNum;
357
358		cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX);
359		if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
360			return (EINVAL);
361		linkNum = strtoul(cp, &eptr, 10);
362		if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS)
363			return (EINVAL);
364		if (priv->links[linkNum] != NULL)
365			return (EISCONN);
366		MALLOC(priv->links[linkNum], struct ng_bridge_link *,
367		    sizeof(*priv->links[linkNum]), M_NETGRAPH_BRIDGE, M_NOWAIT|M_ZERO);
368		if (priv->links[linkNum] == NULL)
369			return (ENOMEM);
370		priv->links[linkNum]->hook = hook;
371		NG_HOOK_SET_PRIVATE(hook, (void *)linkNum);
372		priv->numLinks++;
373		return (0);
374	}
375
376	/* Unknown hook name */
377	return (EINVAL);
378}
379
380/*
381 * Receive a control message
382 */
383static int
384ng_bridge_rcvmsg(node_p node, item_p item, hook_p lasthook)
385{
386	const priv_p priv = NG_NODE_PRIVATE(node);
387	struct ng_mesg *resp = NULL;
388	int error = 0;
389	struct ng_mesg *msg;
390
391	NGI_GET_MSG(item, msg);
392	switch (msg->header.typecookie) {
393	case NGM_BRIDGE_COOKIE:
394		switch (msg->header.cmd) {
395		case NGM_BRIDGE_GET_CONFIG:
396		    {
397			struct ng_bridge_config *conf;
398
399			NG_MKRESPONSE(resp, msg,
400			    sizeof(struct ng_bridge_config), M_NOWAIT);
401			if (resp == NULL) {
402				error = ENOMEM;
403				break;
404			}
405			conf = (struct ng_bridge_config *)resp->data;
406			*conf = priv->conf;	/* no sanity checking needed */
407			break;
408		    }
409		case NGM_BRIDGE_SET_CONFIG:
410		    {
411			struct ng_bridge_config *conf;
412			int i;
413
414			if (msg->header.arglen
415			    != sizeof(struct ng_bridge_config)) {
416				error = EINVAL;
417				break;
418			}
419			conf = (struct ng_bridge_config *)msg->data;
420			priv->conf = *conf;
421			for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++)
422				priv->conf.ipfw[i] = !!priv->conf.ipfw[i];
423			break;
424		    }
425		case NGM_BRIDGE_RESET:
426		    {
427			int i;
428
429			/* Flush all entries in the hash table */
430			ng_bridge_remove_hosts(priv, -1);
431
432			/* Reset all loop detection counters and stats */
433			for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) {
434				if (priv->links[i] == NULL)
435					continue;
436				priv->links[i]->loopCount = 0;
437				bzero(&priv->links[i]->stats,
438				    sizeof(priv->links[i]->stats));
439			}
440			break;
441		    }
442		case NGM_BRIDGE_GET_STATS:
443		case NGM_BRIDGE_CLR_STATS:
444		case NGM_BRIDGE_GETCLR_STATS:
445		    {
446			struct ng_bridge_link *link;
447			int linkNum;
448
449			/* Get link number */
450			if (msg->header.arglen != sizeof(u_int32_t)) {
451				error = EINVAL;
452				break;
453			}
454			linkNum = *((u_int32_t *)msg->data);
455			if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) {
456				error = EINVAL;
457				break;
458			}
459			if ((link = priv->links[linkNum]) == NULL) {
460				error = ENOTCONN;
461				break;
462			}
463
464			/* Get/clear stats */
465			if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
466				NG_MKRESPONSE(resp, msg,
467				    sizeof(link->stats), M_NOWAIT);
468				if (resp == NULL) {
469					error = ENOMEM;
470					break;
471				}
472				bcopy(&link->stats,
473				    resp->data, sizeof(link->stats));
474			}
475			if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
476				bzero(&link->stats, sizeof(link->stats));
477			break;
478		    }
479		case NGM_BRIDGE_GET_TABLE:
480		    {
481			struct ng_bridge_host_ary *ary;
482			struct ng_bridge_hent *hent;
483			int i = 0, bucket;
484
485			NG_MKRESPONSE(resp, msg, sizeof(*ary)
486			    + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
487			if (resp == NULL) {
488				error = ENOMEM;
489				break;
490			}
491			ary = (struct ng_bridge_host_ary *)resp->data;
492			ary->numHosts = priv->numHosts;
493			for (bucket = 0; bucket < priv->numBuckets; bucket++) {
494				SLIST_FOREACH(hent, &priv->tab[bucket], next)
495					ary->hosts[i++] = hent->host;
496			}
497			break;
498		    }
499		default:
500			error = EINVAL;
501			break;
502		}
503		break;
504	default:
505		error = EINVAL;
506		break;
507	}
508
509	/* Done */
510	NG_RESPOND_MSG(error, node, item, resp);
511	NG_FREE_MSG(msg);
512	return (error);
513}
514
515/*
516 * Receive data on a hook
517 */
518static int
519ng_bridge_rcvdata(hook_p hook, item_p item)
520{
521	const node_p node = NG_HOOK_NODE(hook);
522	const priv_p priv = NG_NODE_PRIVATE(node);
523	struct ng_bridge_host *host;
524	struct ng_bridge_link *link;
525	struct ether_header *eh;
526	int error = 0, linkNum;
527	int manycast;
528	struct mbuf *m;
529	meta_p meta;
530	struct ng_bridge_link *firstLink;
531
532	NGI_GET_M(item, m);
533	/* Get link number */
534	linkNum = (int)NG_HOOK_PRIVATE(hook);
535	KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
536	    ("%s: linkNum=%u", __func__, linkNum));
537	link = priv->links[linkNum];
538	KASSERT(link != NULL, ("%s: link%d null", __func__, linkNum));
539
540	/* Sanity check packet and pull up header */
541	if (m->m_pkthdr.len < ETHER_HDR_LEN) {
542		link->stats.recvRunts++;
543		NG_FREE_ITEM(item);
544		NG_FREE_M(m);
545		return (EINVAL);
546	}
547	if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) {
548		link->stats.memoryFailures++;
549		NG_FREE_ITEM(item);
550		return (ENOBUFS);
551	}
552	eh = mtod(m, struct ether_header *);
553	if ((eh->ether_shost[0] & 1) != 0) {
554		link->stats.recvInvalid++;
555		NG_FREE_ITEM(item);
556		NG_FREE_M(m);
557		return (EINVAL);
558	}
559
560	/* Is link disabled due to a loopback condition? */
561	if (link->loopCount != 0) {
562		link->stats.loopDrops++;
563		NG_FREE_ITEM(item);
564		NG_FREE_M(m);
565		return (ELOOP);		/* XXX is this an appropriate error? */
566	}
567
568	/* Update stats */
569	link->stats.recvPackets++;
570	link->stats.recvOctets += m->m_pkthdr.len;
571	if ((manycast = (eh->ether_dhost[0] & 1)) != 0) {
572		if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
573			link->stats.recvBroadcasts++;
574			manycast = 2;
575		} else
576			link->stats.recvMulticasts++;
577	}
578
579	/* Look up packet's source Ethernet address in hashtable */
580	if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
581
582		/* Update time since last heard from this host */
583		host->staleness = 0;
584
585		/* Did host jump to a different link? */
586		if (host->linkNum != linkNum) {
587
588			/*
589			 * If the host's old link was recently established
590			 * on the old link and it's already jumped to a new
591			 * link, declare a loopback condition.
592			 */
593			if (host->age < priv->conf.minStableAge) {
594
595				/* Log the problem */
596				if (priv->conf.debugLevel >= 2) {
597					struct ifnet *ifp = m->m_pkthdr.rcvif;
598					char suffix[32];
599
600					if (ifp != NULL)
601						snprintf(suffix, sizeof(suffix),
602						    " (%s%d)", ifp->if_name,
603						    ifp->if_unit);
604					else
605						*suffix = '\0';
606					log(LOG_WARNING, "ng_bridge: %s:"
607					    " loopback detected on %s%s\n",
608					    ng_bridge_nodename(node),
609					    NG_HOOK_NAME(hook), suffix);
610				}
611
612				/* Mark link as linka non grata */
613				link->loopCount = priv->conf.loopTimeout;
614				link->stats.loopDetects++;
615
616				/* Forget all hosts on this link */
617				ng_bridge_remove_hosts(priv, linkNum);
618
619				/* Drop packet */
620				link->stats.loopDrops++;
621				NG_FREE_ITEM(item);
622				NG_FREE_M(m);
623				return (ELOOP);		/* XXX appropriate? */
624			}
625
626			/* Move host over to new link */
627			host->linkNum = linkNum;
628			host->age = 0;
629		}
630	} else {
631		if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) {
632			link->stats.memoryFailures++;
633			NG_FREE_ITEM(item);
634			NG_FREE_M(m);
635			return (ENOMEM);
636		}
637	}
638
639	/* Run packet through ipfw processing, if enabled */
640	if (priv->conf.ipfw[linkNum] && fw_enable && ip_fw_chk_ptr != NULL) {
641		/* XXX not implemented yet */
642	}
643
644	/*
645	 * If unicast and destination host known, deliver to host's link,
646	 * unless it is the same link as the packet came in on.
647	 */
648	if (!manycast) {
649
650		/* Determine packet destination link */
651		if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
652			struct ng_bridge_link *const destLink
653			    = priv->links[host->linkNum];
654
655			/* If destination same as incoming link, do nothing */
656			KASSERT(destLink != NULL,
657			    ("%s: link%d null", __func__, host->linkNum));
658			if (destLink == link) {
659				NG_FREE_ITEM(item);
660				NG_FREE_M(m);
661				return (0);
662			}
663
664			/* Deliver packet out the destination link */
665			destLink->stats.xmitPackets++;
666			destLink->stats.xmitOctets += m->m_pkthdr.len;
667			NG_FWD_NEW_DATA(error, item, destLink->hook, m);
668			return (error);
669		}
670
671		/* Destination host is not known */
672		link->stats.recvUnknown++;
673	}
674
675	/* Distribute unknown, multicast, broadcast pkts to all other links */
676	meta = NGI_META(item); /* peek.. */
677	firstLink = NULL;
678	for (linkNum = 0; linkNum <= priv->numLinks; linkNum++) {
679		struct ng_bridge_link *destLink;
680		meta_p meta2 = NULL;
681		struct mbuf *m2 = NULL;
682
683		/*
684		 * If we have checked all the links then now
685		 * send the original on its reserved link
686		 */
687		if (linkNum == priv->numLinks) {
688			/* If we never saw a good link, leave. */
689			if (firstLink == NULL) {
690				NG_FREE_ITEM(item);
691				NG_FREE_M(m);
692				return (0);
693			}
694			destLink = firstLink;
695		} else {
696			destLink = priv->links[linkNum];
697			/* Skip incoming link and disconnected links */
698			if (destLink == NULL || destLink == link) {
699				continue;
700			}
701			if (firstLink == NULL) {
702				/*
703				 * This is the first usable link we have found.
704				 * Reserve it for the originals.
705				 * If we never find another we save a copy.
706				 */
707				firstLink = destLink;
708				continue;
709			}
710
711			/*
712			 * It's usable link but not the reserved (first) one.
713			 * Copy mbuf and meta info for sending.
714			 */
715			m2 = m_dup(m, M_NOWAIT);	/* XXX m_copypacket() */
716			if (m2 == NULL) {
717				link->stats.memoryFailures++;
718				NG_FREE_ITEM(item);
719				NG_FREE_M(m);
720				return (ENOBUFS);
721			}
722			if (meta != NULL
723			    && (meta2 = ng_copy_meta(meta)) == NULL) {
724				link->stats.memoryFailures++;
725				m_freem(m2);
726				NG_FREE_ITEM(item);
727				NG_FREE_M(m);
728				return (ENOMEM);
729			}
730		}
731
732		/* Update stats */
733		destLink->stats.xmitPackets++;
734		destLink->stats.xmitOctets += m->m_pkthdr.len;
735		switch (manycast) {
736		case 0:					/* unicast */
737			break;
738		case 1:					/* multicast */
739			destLink->stats.xmitMulticasts++;
740			break;
741		case 2:					/* broadcast */
742			destLink->stats.xmitBroadcasts++;
743			break;
744		}
745
746		/* Send packet */
747		if (destLink == firstLink) {
748			/*
749			 * If we've sent all the others, send the original
750			 * on the first link we found.
751			 */
752			NG_FWD_NEW_DATA(error, item, destLink->hook, m);
753			break; /* always done last - not really needed. */
754		} else {
755			NG_SEND_DATA(error, destLink->hook, m2, meta2);
756		}
757	}
758	return (error);
759}
760
761/*
762 * Shutdown node
763 */
764static int
765ng_bridge_shutdown(node_p node)
766{
767	const priv_p priv = NG_NODE_PRIVATE(node);
768
769	KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
770	    ("%s: numLinks=%d numHosts=%d",
771	    __func__, priv->numLinks, priv->numHosts));
772	FREE(priv->tab, M_NETGRAPH_BRIDGE);
773	FREE(priv, M_NETGRAPH_BRIDGE);
774	NG_NODE_SET_PRIVATE(node, NULL);
775	NG_NODE_UNREF(node);
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 = (int)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 */
971static void
972ng_bridge_timeout(void *arg)
973{
974	const node_p node = arg;
975	const priv_p priv = NG_NODE_PRIVATE(node);
976	int s, bucket;
977	int counter = 0;
978	int linkNum;
979
980	/* Avoid race condition with ng_bridge_shutdown() */
981	s = splnet();
982	if ((NG_NODE_NOT_VALID(node)) || priv == NULL) {
983		NG_NODE_UNREF(node);
984		splx(s);
985		return;
986	}
987
988	/* Register a new timeout, keeping the existing node reference */
989	callout_reset(&priv->timer, hz, ng_bridge_timeout, node);
990
991	/* Update host time counters and remove stale entries */
992	for (bucket = 0; bucket < priv->numBuckets; bucket++) {
993		struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
994
995		while (*hptr != NULL) {
996			struct ng_bridge_hent *const hent = *hptr;
997
998			/* Make sure host's link really exists */
999			KASSERT(priv->links[hent->host.linkNum] != NULL,
1000			    ("%s: host %6D on nonexistent link %d\n",
1001			    __func__, hent->host.addr, ":",
1002			    hent->host.linkNum));
1003
1004			/* Remove hosts we haven't heard from in a while */
1005			if (++hent->host.staleness >= priv->conf.maxStaleness) {
1006				*hptr = SLIST_NEXT(hent, next);
1007				FREE(hent, M_NETGRAPH_BRIDGE);
1008				priv->numHosts--;
1009			} else {
1010				if (hent->host.age < 0xffff)
1011					hent->host.age++;
1012				hptr = &SLIST_NEXT(hent, next);
1013				counter++;
1014			}
1015		}
1016	}
1017	KASSERT(priv->numHosts == counter,
1018	    ("%s: hosts: %d != %d", __func__, priv->numHosts, counter));
1019
1020	/* Decrease table size if necessary */
1021	ng_bridge_rehash(priv);
1022
1023	/* Decrease loop counter on muted looped back links */
1024	for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) {
1025		struct ng_bridge_link *const link = priv->links[linkNum];
1026
1027		if (link != NULL) {
1028			if (link->loopCount != 0) {
1029				link->loopCount--;
1030				if (link->loopCount == 0
1031				    && priv->conf.debugLevel >= 2) {
1032					log(LOG_INFO, "ng_bridge: %s:"
1033					    " restoring looped back link%d\n",
1034					    ng_bridge_nodename(node), linkNum);
1035				}
1036			}
1037			counter++;
1038		}
1039	}
1040	KASSERT(priv->numLinks == counter,
1041	    ("%s: links: %d != %d", __func__, priv->numLinks, counter));
1042
1043	/* Done */
1044	splx(s);
1045}
1046
1047/*
1048 * Return node's "name", even if it doesn't have one.
1049 */
1050static const char *
1051ng_bridge_nodename(node_p node)
1052{
1053	static char name[NG_NODELEN+1];
1054
1055	if (NG_NODE_NAME(node) != NULL)
1056		snprintf(name, sizeof(name), "%s", NG_NODE_NAME(node));
1057	else
1058		snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
1059	return name;
1060}
1061
1062