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 69922 2000-12-12 18:52:14Z julian $
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/* Per-link private data */
83struct ng_bridge_link {
84 hook_p hook; /* netgraph hook */
85 u_int16_t loopCount; /* loop ignore timer */
86 struct ng_bridge_link_stats stats; /* link stats */
87};
88
89/* Per-node private data */
90struct ng_bridge_private {
91 struct ng_bridge_bucket *tab; /* hash table bucket array */
92 struct ng_bridge_link *links[NG_BRIDGE_MAX_LINKS];
93 struct ng_bridge_config conf; /* node configuration */
94 node_p node; /* netgraph node */
95 u_int numHosts; /* num entries in table */
96 u_int numBuckets; /* num buckets in table */
97 u_int hashMask; /* numBuckets - 1 */
98 int numLinks; /* num connected links */
99 struct callout timer; /* one second periodic timer */
100};
101typedef struct ng_bridge_private *priv_p;
102
103/* Information about a host, stored in a hash table entry */
104struct ng_bridge_hent {
105 struct ng_bridge_host host; /* actual host info */
106 SLIST_ENTRY(ng_bridge_hent) next; /* next entry in bucket */
107};
108
109/* Hash table bucket declaration */
110SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent);
111
112/* Netgraph node methods */
113static ng_constructor_t ng_bridge_constructor;
114static ng_rcvmsg_t ng_bridge_rcvmsg;
115static ng_shutdown_t ng_bridge_rmnode;
116static ng_newhook_t ng_bridge_newhook;
117static ng_rcvdata_t ng_bridge_rcvdata;
118static ng_disconnect_t ng_bridge_disconnect;
119
120/* Other internal functions */
121static struct ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr);
122static int ng_bridge_put(priv_p priv, const u_char *addr, int linkNum);
123static void ng_bridge_rehash(priv_p priv);
124static void ng_bridge_remove_hosts(priv_p priv, int linkNum);
125static void ng_bridge_timeout(void *arg);
126static const char *ng_bridge_nodename(node_p node);
127
128/* Ethernet broadcast */
129static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
130 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
131
132/* Store each hook's link number in the private field */
133#define LINK_NUM(hook) (*(u_int16_t *)(&(hook)->private))
134
135/* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
136#define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \
137 == ((const u_int32_t *)(b))[0] \
138 && ((const u_int16_t *)(a))[2] \
139 == ((const u_int16_t *)(b))[2])
140
141/* Minimum and maximum number of hash buckets. Must be a power of two. */
142#define MIN_BUCKETS (1 << 5) /* 32 */
143#define MAX_BUCKETS (1 << 14) /* 16384 */
144
145/* Configuration default values */
146#define DEFAULT_LOOP_TIMEOUT 60
147#define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */
148#define DEFAULT_MIN_STABLE_AGE 1
149
150/******************************************************************
151 NETGRAPH PARSE TYPES
152******************************************************************/
153
154/*
155 * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
156 */
157static int
158ng_bridge_getTableLength(const struct ng_parse_type *type,
159 const u_char *start, const u_char *buf)
160{
161 const struct ng_bridge_host_ary *const hary
162 = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
163
164 return hary->numHosts;
165}
166
167/* Parse type for struct ng_bridge_host_ary */
168static const struct ng_parse_struct_info ng_bridge_host_type_info
169 = NG_BRIDGE_HOST_TYPE_INFO(&ng_ether_enaddr_type);
170static const struct ng_parse_type ng_bridge_host_type = {
171 &ng_parse_struct_type,
172 &ng_bridge_host_type_info
173};
174static const struct ng_parse_array_info ng_bridge_hary_type_info = {
175 &ng_bridge_host_type,
176 ng_bridge_getTableLength
177};
178static const struct ng_parse_type ng_bridge_hary_type = {
179 &ng_parse_array_type,
180 &ng_bridge_hary_type_info
181};
182static const struct ng_parse_struct_info ng_bridge_host_ary_type_info
183 = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
184static const struct ng_parse_type ng_bridge_host_ary_type = {
185 &ng_parse_struct_type,
186 &ng_bridge_host_ary_type_info
187};
188
189/* Parse type for struct ng_bridge_config */
190static const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = {
191 &ng_parse_uint8_type,
192 NG_BRIDGE_MAX_LINKS
193};
194static const struct ng_parse_type ng_bridge_ipfwary_type = {
195 &ng_parse_fixedarray_type,
196 &ng_bridge_ipfwary_type_info
197};
198static const struct ng_parse_struct_info ng_bridge_config_type_info
199 = NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type);
200static const struct ng_parse_type ng_bridge_config_type = {
201 &ng_parse_struct_type,
202 &ng_bridge_config_type_info
203};
204
205/* Parse type for struct ng_bridge_link_stat */
206static const struct ng_parse_struct_info
207 ng_bridge_stats_type_info = NG_BRIDGE_STATS_TYPE_INFO;
208static const struct ng_parse_type ng_bridge_stats_type = {
209 &ng_parse_struct_type,
210 &ng_bridge_stats_type_info
211};
212
213/* List of commands and how to convert arguments to/from ASCII */
214static const struct ng_cmdlist ng_bridge_cmdlist[] = {
215 {
216 NGM_BRIDGE_COOKIE,
217 NGM_BRIDGE_SET_CONFIG,
218 "setconfig",
219 &ng_bridge_config_type,
220 NULL
221 },
222 {
223 NGM_BRIDGE_COOKIE,
224 NGM_BRIDGE_GET_CONFIG,
225 "getconfig",
226 NULL,
227 &ng_bridge_config_type
228 },
229 {
230 NGM_BRIDGE_COOKIE,
231 NGM_BRIDGE_RESET,
232 "reset",
233 NULL,
234 NULL
235 },
236 {
237 NGM_BRIDGE_COOKIE,
238 NGM_BRIDGE_GET_STATS,
239 "getstats",
240 &ng_parse_uint32_type,
241 &ng_bridge_stats_type
242 },
243 {
244 NGM_BRIDGE_COOKIE,
245 NGM_BRIDGE_CLR_STATS,
246 "clrstats",
247 &ng_parse_uint32_type,
248 NULL
249 },
250 {
251 NGM_BRIDGE_COOKIE,
252 NGM_BRIDGE_GETCLR_STATS,
253 "getclrstats",
254 &ng_parse_uint32_type,
255 &ng_bridge_stats_type
256 },
257 {
258 NGM_BRIDGE_COOKIE,
259 NGM_BRIDGE_GET_TABLE,
260 "gettable",
261 NULL,
262 &ng_bridge_host_ary_type
263 },
264 { 0 }
265};
266
267/* Node type descriptor */
268static struct ng_type ng_bridge_typestruct = {
269 NG_VERSION,
270 NG_BRIDGE_NODE_TYPE,
271 NULL,
272 ng_bridge_constructor,
273 ng_bridge_rcvmsg,
274 ng_bridge_rmnode,
275 ng_bridge_newhook,
276 NULL,
277 NULL,
278 ng_bridge_rcvdata,
279 ng_bridge_disconnect,
280 ng_bridge_cmdlist,
281};
282NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
283
284/* Depend on ng_ether so we can use the Ethernet parse type */
285MODULE_DEPEND(ng_bridge, ng_ether, 1, 1, 1);
286
287/******************************************************************
288 NETGRAPH NODE METHODS
289******************************************************************/
290
291/*
292 * Node constructor
293 */
294static int
295ng_bridge_constructor(node_p *nodep)
296{
297 priv_p priv;
298 int error;
299
300 /* Allocate and initialize private info */
301 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
302 if (priv == NULL)
303 return (ENOMEM);
304 callout_init(&priv->timer, 0);
305
306 /* Allocate and initialize hash table, etc. */
307 MALLOC(priv->tab, struct ng_bridge_bucket *,
308 MIN_BUCKETS * sizeof(*priv->tab), M_NETGRAPH, M_NOWAIT | M_ZERO);
309 if (priv->tab == NULL) {
310 FREE(priv, M_NETGRAPH);
311 return (ENOMEM);
312 }
313 priv->numBuckets = MIN_BUCKETS;
314 priv->hashMask = MIN_BUCKETS - 1;
315 priv->conf.debugLevel = 1;
316 priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
317 priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
318 priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
319
320 /* Call superclass constructor */
321 if ((error = ng_make_node_common(&ng_bridge_typestruct, nodep))) {
322 FREE(priv, M_NETGRAPH);
323 return (error);
324 }
325 (*nodep)->private = priv;
326 priv->node = *nodep;
327
328 /* Start timer by faking a timeout event */
329 (*nodep)->refs++;
330 ng_bridge_timeout(*nodep);
331 return (0);
332}
333
334/*
335 * Method for attaching a new hook
336 */
337static int
338ng_bridge_newhook(node_p node, hook_p hook, const char *name)
339{
340 const priv_p priv = node->private;
341
342 /* Check for a link hook */
343 if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX,
344 strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) {
345 const char *cp;
346 char *eptr;
347 u_long linkNum;
348
349 cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX);
350 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
351 return (EINVAL);
352 linkNum = strtoul(cp, &eptr, 10);
353 if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS)
354 return (EINVAL);
355 if (priv->links[linkNum] != NULL)
356 return (EISCONN);
357 MALLOC(priv->links[linkNum], struct ng_bridge_link *,
358 sizeof(*priv->links[linkNum]), M_NETGRAPH, M_NOWAIT|M_ZERO);
359 if (priv->links[linkNum] == NULL)
360 return (ENOMEM);
361 priv->links[linkNum]->hook = hook;
362 LINK_NUM(hook) = linkNum;
363 priv->numLinks++;
364 return (0);
365 }
366
367 /* Unknown hook name */
368 return (EINVAL);
369}
370
371/*
372 * Receive a control message
373 */
374static int
375ng_bridge_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
376 struct ng_mesg **rptr, hook_p lasthook)
377{
378 const priv_p priv = node->private;
379 struct ng_mesg *resp = NULL;
380 int error = 0;
381
382 switch (msg->header.typecookie) {
383 case NGM_BRIDGE_COOKIE:
384 switch (msg->header.cmd) {
385 case NGM_BRIDGE_GET_CONFIG:
386 {
387 struct ng_bridge_config *conf;
388
389 NG_MKRESPONSE(resp, msg,
390 sizeof(struct ng_bridge_config), M_NOWAIT);
391 if (resp == NULL) {
392 error = ENOMEM;
393 break;
394 }
395 conf = (struct ng_bridge_config *)resp->data;
396 *conf = priv->conf; /* no sanity checking needed */
397 break;
398 }
399 case NGM_BRIDGE_SET_CONFIG:
400 {
401 struct ng_bridge_config *conf;
402 int i;
403
404 if (msg->header.arglen
405 != sizeof(struct ng_bridge_config)) {
406 error = EINVAL;
407 break;
408 }
409 conf = (struct ng_bridge_config *)msg->data;
410 priv->conf = *conf;
411 for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++)
412 priv->conf.ipfw[i] = !!priv->conf.ipfw[i];
413 break;
414 }
415 case NGM_BRIDGE_RESET:
416 {
417 int i;
418
419 /* Flush all entries in the hash table */
420 ng_bridge_remove_hosts(priv, -1);
421
422 /* Reset all loop detection counters and stats */
423 for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) {
424 if (priv->links[i] == NULL)
425 continue;
426 priv->links[i]->loopCount = 0;
427 bzero(&priv->links[i]->stats,
428 sizeof(priv->links[i]->stats));
429 }
430 break;
431 }
432 case NGM_BRIDGE_GET_STATS:
433 case NGM_BRIDGE_CLR_STATS:
434 case NGM_BRIDGE_GETCLR_STATS:
435 {
436 struct ng_bridge_link *link;
437 int linkNum;
438
439 /* Get link number */
440 if (msg->header.arglen != sizeof(u_int32_t)) {
441 error = EINVAL;
442 break;
443 }
444 linkNum = *((u_int32_t *)msg->data);
445 if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) {
446 error = EINVAL;
447 break;
448 }
449 if ((link = priv->links[linkNum]) == NULL) {
450 error = ENOTCONN;
451 break;
452 }
453
454 /* Get/clear stats */
455 if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
456 NG_MKRESPONSE(resp, msg,
457 sizeof(link->stats), M_NOWAIT);
458 if (resp == NULL) {
459 error = ENOMEM;
460 break;
461 }
462 bcopy(&link->stats,
463 resp->data, sizeof(link->stats));
464 }
465 if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
466 bzero(&link->stats, sizeof(link->stats));
467 break;
468 }
469 case NGM_BRIDGE_GET_TABLE:
470 {
471 struct ng_bridge_host_ary *ary;
472 struct ng_bridge_hent *hent;
473 int i = 0, bucket;
474
475 NG_MKRESPONSE(resp, msg, sizeof(*ary)
476 + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
477 if (resp == NULL) {
478 error = ENOMEM;
479 break;
480 }
481 ary = (struct ng_bridge_host_ary *)resp->data;
482 ary->numHosts = priv->numHosts;
483 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
484 SLIST_FOREACH(hent, &priv->tab[bucket], next)
485 ary->hosts[i++] = hent->host;
486 }
487 break;
488 }
489 default:
490 error = EINVAL;
491 break;
492 }
493 break;
494 default:
495 error = EINVAL;
496 break;
497 }
498
499 /* Done */
500 if (rptr)
501 *rptr = resp;
502 else if (resp != NULL)
503 FREE(resp, M_NETGRAPH);
504 FREE(msg, M_NETGRAPH);
505 return (error);
506}
507
508/*
509 * Receive data on a hook
510 */
511static int
512ng_bridge_rcvdata(hook_p hook, struct mbuf *m, meta_p meta,
513 struct mbuf **ret_m, meta_p *ret_meta, struct ng_mesg **resp)
514{
515 const node_p node = hook->node;
516 const priv_p priv = node->private;
517 struct ng_bridge_host *host;
518 struct ng_bridge_link *link;
519 struct ether_header *eh;
520 int error = 0, linkNum;
521 int i, manycast;
522
523 /* Get link number */
524 linkNum = LINK_NUM(hook);
525 KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
526 ("%s: linkNum=%u", __FUNCTION__, linkNum));
527 link = priv->links[linkNum];
528 KASSERT(link != NULL, ("%s: link%d null", __FUNCTION__, linkNum));
529
530 /* Sanity check packet and pull up header */
531 if (m->m_pkthdr.len < ETHER_HDR_LEN) {
532 link->stats.recvRunts++;
533 NG_FREE_DATA(m, meta);
534 return (EINVAL);
535 }
536 if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) {
537 link->stats.memoryFailures++;
538 NG_FREE_META(meta);
539 return (ENOBUFS);
540 }
541 eh = mtod(m, struct ether_header *);
542 if ((eh->ether_shost[0] & 1) != 0) {
543 link->stats.recvInvalid++;
544 NG_FREE_DATA(m, meta);
545 return (EINVAL);
546 }
547
548 /* Is link disabled due to a loopback condition? */
549 if (link->loopCount != 0) {
550 link->stats.loopDrops++;
551 NG_FREE_DATA(m, meta);
552 return (ELOOP); /* XXX is this an appropriate error? */
553 }
554
555 /* Update stats */
556 link->stats.recvPackets++;
557 link->stats.recvOctets += m->m_pkthdr.len;
558 if ((manycast = (eh->ether_dhost[0] & 1)) != 0) {
559 if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
560 link->stats.recvBroadcasts++;
561 manycast = 2;
562 } else
563 link->stats.recvMulticasts++;
564 }
565
566 /* Look up packet's source Ethernet address in hashtable */
567 if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
568
569 /* Update time since last heard from this host */
570 host->staleness = 0;
571
572 /* Did host jump to a different link? */
573 if (host->linkNum != linkNum) {
574
575 /*
576 * If the host's old link was recently established
577 * on the old link and it's already jumped to a new
578 * link, declare a loopback condition.
579 */
580 if (host->age < priv->conf.minStableAge) {
581
582 /* Log the problem */
583 if (priv->conf.debugLevel >= 2) {
584 struct ifnet *ifp = m->m_pkthdr.rcvif;
585 char suffix[32];
586
587 if (ifp != NULL)
588 snprintf(suffix, sizeof(suffix),
589 " (%s%d)", ifp->if_name,
590 ifp->if_unit);
591 else
592 *suffix = '\0';
593 log(LOG_WARNING, "ng_bridge: %s:"
594 " loopback detected on %s%s\n",
595 ng_bridge_nodename(node),
596 hook->name, suffix);
597 }
598
599 /* Mark link as linka non grata */
600 link->loopCount = priv->conf.loopTimeout;
601 link->stats.loopDetects++;
602
603 /* Forget all hosts on this link */
604 ng_bridge_remove_hosts(priv, linkNum);
605
606 /* Drop packet */
607 link->stats.loopDrops++;
608 NG_FREE_DATA(m, meta);
609 return (ELOOP); /* XXX appropriate? */
610 }
611
612 /* Move host over to new link */
613 host->linkNum = linkNum;
614 host->age = 0;
615 }
616 } else {
617 if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) {
618 link->stats.memoryFailures++;
619 NG_FREE_DATA(m, meta);
620 return (ENOMEM);
621 }
622 }
623
624 /* Run packet through ipfw processing, if enabled */
625 if (priv->conf.ipfw[linkNum] && fw_enable && ip_fw_chk_ptr != NULL) {
626 /* XXX not implemented yet */
627 }
628
629 /*
630 * If unicast and destination host known, deliver to host's link,
631 * unless it is the same link as the packet came in on.
632 */
633 if (!manycast) {
634
635 /* Determine packet destination link */
636 if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
637 struct ng_bridge_link *const destLink
638 = priv->links[host->linkNum];
639
640 /* If destination same as incoming link, do nothing */
641 KASSERT(destLink != NULL,
642 ("%s: link%d null", __FUNCTION__, host->linkNum));
643 if (destLink == link) {
644 NG_FREE_DATA(m, meta);
645 return (0);
646 }
647
648 /* Deliver packet out the destination link */
649 destLink->stats.xmitPackets++;
650 destLink->stats.xmitOctets += m->m_pkthdr.len;
651 NG_SEND_DATA(error, destLink->hook, m, meta);
652 return (error);
653 }
654
655 /* Destination host is not known */
656 link->stats.recvUnknown++;
657 }
658
659 /* Distribute unknown, multicast, broadcast pkts to all other links */
660 for (linkNum = i = 0; i < priv->numLinks - 1; linkNum++) {
661 struct ng_bridge_link *const destLink = priv->links[linkNum];
662 meta_p meta2 = NULL;
663 struct mbuf *m2;
664
665 /* Skip incoming link and disconnected links */
666 if (destLink == NULL || destLink == link)
667 continue;
668
669 /* Copy mbuf and meta info */
670 if (++i == priv->numLinks - 1) { /* last link */
671 m2 = m;
672 meta2 = meta;
673 } else {
674 m2 = m_dup(m, M_NOWAIT); /* XXX m_copypacket() */
675 if (m2 == NULL) {
676 link->stats.memoryFailures++;
677 NG_FREE_DATA(m, meta);
678 return (ENOBUFS);
679 }
680 if (meta != NULL
681 && (meta2 = ng_copy_meta(meta)) == NULL) {
682 link->stats.memoryFailures++;
683 m_freem(m2);
684 NG_FREE_DATA(m, meta);
685 return (ENOMEM);
686 }
687 }
688
689 /* Update stats */
690 destLink->stats.xmitPackets++;
691 destLink->stats.xmitOctets += m->m_pkthdr.len;
692 switch (manycast) {
693 case 0: /* unicast */
694 break;
695 case 1: /* multicast */
696 destLink->stats.xmitMulticasts++;
697 break;
698 case 2: /* broadcast */
699 destLink->stats.xmitBroadcasts++;
700 break;
701 }
702
703 /* Send packet */
704 NG_SEND_DATA(error, destLink->hook, m2, meta2);
705 }
706 return (error);
707}
708
709/*
710 * Shutdown node
711 */
712static int
713ng_bridge_rmnode(node_p node)
714{
715 const priv_p priv = node->private;
716
717 ng_unname(node);
718 ng_cutlinks(node); /* frees all link and host info */
719 KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
720 ("%s: numLinks=%d numHosts=%d",
721 __FUNCTION__, priv->numLinks, priv->numHosts));
722 FREE(priv->tab, M_NETGRAPH);
723 FREE(priv, M_NETGRAPH);
724 node->private = NULL;
725 ng_unref(node);
726 return (0);
727}
728
729/*
730 * Hook disconnection.
731 */
732static int
733ng_bridge_disconnect(hook_p hook)
734{
735 const priv_p priv = hook->node->private;
736 int linkNum;
737
738 /* Get link number */
739 linkNum = LINK_NUM(hook);
740 KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
741 ("%s: linkNum=%u", __FUNCTION__, linkNum));
742
743 /* Remove all hosts associated with this link */
744 ng_bridge_remove_hosts(priv, linkNum);
745
746 /* Free associated link information */
747 KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __FUNCTION__));
748 FREE(priv->links[linkNum], M_NETGRAPH);
749 priv->links[linkNum] = NULL;
750 priv->numLinks--;
751
752 /* If no more hooks, go away */
753 if (hook->node->numhooks == 0)
754 ng_rmnode(hook->node);
755 return (0);
756}
757
758/******************************************************************
759 HASH TABLE FUNCTIONS
760******************************************************************/
761
762/*
763 * Hash algorithm
764 *
765 * Only hashing bytes 3-6 of the Ethernet address is sufficient and fast.
766 */
767#define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \
768 ^ ((const u_int16_t *)(addr))[1] \
769 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
770
771/*
772 * Find a host entry in the table.
773 */
774static struct ng_bridge_host *
775ng_bridge_get(priv_p priv, const u_char *addr)
776{
777 const int bucket = HASH(addr, priv->hashMask);
778 struct ng_bridge_hent *hent;
779
780 SLIST_FOREACH(hent, &priv->tab[bucket], next) {
781 if (ETHER_EQUAL(hent->host.addr, addr))
782 return (&hent->host);
783 }
784 return (NULL);
785}
786
787/*
788 * Add a new host entry to the table. This assumes the host doesn't
789 * already exist in the table. Returns 1 on success, 0 if there
790 * was a memory allocation failure.
791 */
792static int
793ng_bridge_put(priv_p priv, const u_char *addr, int linkNum)
794{
795 const int bucket = HASH(addr, priv->hashMask);
796 struct ng_bridge_hent *hent;
797
798#ifdef INVARIANTS
799 /* Assert that entry does not already exist in hashtable */
800 SLIST_FOREACH(hent, &priv->tab[bucket], next) {
801 KASSERT(!ETHER_EQUAL(hent->host.addr, addr),
802 ("%s: entry %6D exists in table", __FUNCTION__, addr, ":"));
803 }
804#endif
805
806 /* Allocate and initialize new hashtable entry */
807 MALLOC(hent, struct ng_bridge_hent *,
808 sizeof(*hent), M_NETGRAPH, M_NOWAIT);
809 if (hent == NULL)
810 return (0);
811 bcopy(addr, hent->host.addr, ETHER_ADDR_LEN);
812 hent->host.linkNum = linkNum;
813 hent->host.staleness = 0;
814 hent->host.age = 0;
815
816 /* Add new element to hash bucket */
817 SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next);
818 priv->numHosts++;
819
820 /* Resize table if necessary */
821 ng_bridge_rehash(priv);
822 return (1);
823}
824
825/*
826 * Resize the hash table. We try to maintain the number of buckets
827 * such that the load factor is in the range 0.25 to 1.0.
828 *
829 * If we can't get the new memory then we silently fail. This is OK
830 * because things will still work and we'll try again soon anyway.
831 */
832static void
833ng_bridge_rehash(priv_p priv)
834{
835 struct ng_bridge_bucket *newTab;
836 int oldBucket, newBucket;
837 int newNumBuckets;
838 u_int newMask;
839
840 /* Is table too full or too empty? */
841 if (priv->numHosts > priv->numBuckets
842 && (priv->numBuckets << 1) <= MAX_BUCKETS)
843 newNumBuckets = priv->numBuckets << 1;
844 else if (priv->numHosts < (priv->numBuckets >> 2)
845 && (priv->numBuckets >> 2) >= MIN_BUCKETS)
846 newNumBuckets = priv->numBuckets >> 2;
847 else
848 return;
849 newMask = newNumBuckets - 1;
850
851 /* Allocate and initialize new table */
852 MALLOC(newTab, struct ng_bridge_bucket *,
853 newNumBuckets * sizeof(*newTab), M_NETGRAPH, M_NOWAIT | M_ZERO);
854 if (newTab == NULL)
855 return;
856
857 /* Move all entries from old table to new table */
858 for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
859 struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
860
861 while (!SLIST_EMPTY(oldList)) {
862 struct ng_bridge_hent *const hent
863 = SLIST_FIRST(oldList);
864
865 SLIST_REMOVE_HEAD(oldList, next);
866 newBucket = HASH(hent->host.addr, newMask);
867 SLIST_INSERT_HEAD(&newTab[newBucket], hent, next);
868 }
869 }
870
871 /* Replace old table with new one */
872 if (priv->conf.debugLevel >= 3) {
873 log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
874 ng_bridge_nodename(priv->node),
875 priv->numBuckets, newNumBuckets);
876 }
877 FREE(priv->tab, M_NETGRAPH);
878 priv->numBuckets = newNumBuckets;
879 priv->hashMask = newMask;
880 priv->tab = newTab;
881 return;
882}
883
884/******************************************************************
885 MISC FUNCTIONS
886******************************************************************/
887
888/*
889 * Remove all hosts associated with a specific link from the hashtable.
890 * If linkNum == -1, then remove all hosts in the table.
891 */
892static void
893ng_bridge_remove_hosts(priv_p priv, int linkNum)
894{
895 int bucket;
896
897 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
898 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
899
900 while (*hptr != NULL) {
901 struct ng_bridge_hent *const hent = *hptr;
902
903 if (linkNum == -1 || hent->host.linkNum == linkNum) {
904 *hptr = SLIST_NEXT(hent, next);
905 FREE(hent, M_NETGRAPH);
906 priv->numHosts--;
907 } else
908 hptr = &SLIST_NEXT(hent, next);
909 }
910 }
911}
912
913/*
914 * Handle our once-per-second timeout event. We do two things:
915 * we decrement link->loopCount for those links being muted due to
916 * a detected loopback condition, and we remove any hosts from
917 * the hashtable whom we haven't heard from in a long while.
918 */
919static void
920ng_bridge_timeout(void *arg)
921{
922 const node_p node = arg;
923 const priv_p priv = node->private;
924 int s, bucket;
925 int counter = 0;
926 int linkNum;
927
928 /* Avoid race condition with ng_bridge_shutdown() */
929 s = splnet();
930 if ((node->flags & NG_INVALID) != 0 || priv == NULL) {
931 ng_unref(node);
932 splx(s);
933 return;
934 }
935
936 /* Register a new timeout, keeping the existing node reference */
937 callout_reset(&priv->timer, hz, ng_bridge_timeout, node);
938
939 /* Update host time counters and remove stale entries */
940 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
941 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
942
943 while (*hptr != NULL) {
944 struct ng_bridge_hent *const hent = *hptr;
945
946 /* Make sure host's link really exists */
947 KASSERT(priv->links[hent->host.linkNum] != NULL,
948 ("%s: host %6D on nonexistent link %d\n",
949 __FUNCTION__, hent->host.addr, ":",
950 hent->host.linkNum));
951
952 /* Remove hosts we haven't heard from in a while */
953 if (++hent->host.staleness >= priv->conf.maxStaleness) {
954 *hptr = SLIST_NEXT(hent, next);
955 FREE(hent, M_NETGRAPH);
956 priv->numHosts--;
957 } else {
958 if (hent->host.age < 0xffff)
959 hent->host.age++;
960 hptr = &SLIST_NEXT(hent, next);
961 counter++;
962 }
963 }
964 }
965 KASSERT(priv->numHosts == counter,
966 ("%s: hosts: %d != %d", __FUNCTION__, priv->numHosts, counter));
967
968 /* Decrease table size if necessary */
969 ng_bridge_rehash(priv);
970
971 /* Decrease loop counter on muted looped back links */
972 for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) {
973 struct ng_bridge_link *const link = priv->links[linkNum];
974
975 if (link != NULL) {
976 if (link->loopCount != 0) {
977 link->loopCount--;
978 if (link->loopCount == 0
979 && priv->conf.debugLevel >= 2) {
980 log(LOG_INFO, "ng_bridge: %s:"
981 " restoring looped back link%d\n",
982 ng_bridge_nodename(node), linkNum);
983 }
984 }
985 counter++;
986 }
987 }
988 KASSERT(priv->numLinks == counter,
989 ("%s: links: %d != %d", __FUNCTION__, priv->numLinks, counter));
990
991 /* Done */
992 splx(s);
993}
994
995/*
996 * Return node's "name", even if it doesn't have one.
997 */
998static const char *
999ng_bridge_nodename(node_p node)
1000{
1001 static char name[NG_NODELEN+1];
1002
1003 if (node->name != NULL)
1004 snprintf(name, sizeof(name), "%s", node->name);
1005 else
1006 snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
1007 return name;
1008}
1009
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 69922 2000-12-12 18:52:14Z julian $
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/* Per-link private data */
83struct ng_bridge_link {
84 hook_p hook; /* netgraph hook */
85 u_int16_t loopCount; /* loop ignore timer */
86 struct ng_bridge_link_stats stats; /* link stats */
87};
88
89/* Per-node private data */
90struct ng_bridge_private {
91 struct ng_bridge_bucket *tab; /* hash table bucket array */
92 struct ng_bridge_link *links[NG_BRIDGE_MAX_LINKS];
93 struct ng_bridge_config conf; /* node configuration */
94 node_p node; /* netgraph node */
95 u_int numHosts; /* num entries in table */
96 u_int numBuckets; /* num buckets in table */
97 u_int hashMask; /* numBuckets - 1 */
98 int numLinks; /* num connected links */
99 struct callout timer; /* one second periodic timer */
100};
101typedef struct ng_bridge_private *priv_p;
102
103/* Information about a host, stored in a hash table entry */
104struct ng_bridge_hent {
105 struct ng_bridge_host host; /* actual host info */
106 SLIST_ENTRY(ng_bridge_hent) next; /* next entry in bucket */
107};
108
109/* Hash table bucket declaration */
110SLIST_HEAD(ng_bridge_bucket, ng_bridge_hent);
111
112/* Netgraph node methods */
113static ng_constructor_t ng_bridge_constructor;
114static ng_rcvmsg_t ng_bridge_rcvmsg;
115static ng_shutdown_t ng_bridge_rmnode;
116static ng_newhook_t ng_bridge_newhook;
117static ng_rcvdata_t ng_bridge_rcvdata;
118static ng_disconnect_t ng_bridge_disconnect;
119
120/* Other internal functions */
121static struct ng_bridge_host *ng_bridge_get(priv_p priv, const u_char *addr);
122static int ng_bridge_put(priv_p priv, const u_char *addr, int linkNum);
123static void ng_bridge_rehash(priv_p priv);
124static void ng_bridge_remove_hosts(priv_p priv, int linkNum);
125static void ng_bridge_timeout(void *arg);
126static const char *ng_bridge_nodename(node_p node);
127
128/* Ethernet broadcast */
129static const u_char ng_bridge_bcast_addr[ETHER_ADDR_LEN] =
130 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
131
132/* Store each hook's link number in the private field */
133#define LINK_NUM(hook) (*(u_int16_t *)(&(hook)->private))
134
135/* Compare Ethernet addresses using 32 and 16 bit words instead of bytewise */
136#define ETHER_EQUAL(a,b) (((const u_int32_t *)(a))[0] \
137 == ((const u_int32_t *)(b))[0] \
138 && ((const u_int16_t *)(a))[2] \
139 == ((const u_int16_t *)(b))[2])
140
141/* Minimum and maximum number of hash buckets. Must be a power of two. */
142#define MIN_BUCKETS (1 << 5) /* 32 */
143#define MAX_BUCKETS (1 << 14) /* 16384 */
144
145/* Configuration default values */
146#define DEFAULT_LOOP_TIMEOUT 60
147#define DEFAULT_MAX_STALENESS (15 * 60) /* same as ARP timeout */
148#define DEFAULT_MIN_STABLE_AGE 1
149
150/******************************************************************
151 NETGRAPH PARSE TYPES
152******************************************************************/
153
154/*
155 * How to determine the length of the table returned by NGM_BRIDGE_GET_TABLE
156 */
157static int
158ng_bridge_getTableLength(const struct ng_parse_type *type,
159 const u_char *start, const u_char *buf)
160{
161 const struct ng_bridge_host_ary *const hary
162 = (const struct ng_bridge_host_ary *)(buf - sizeof(u_int32_t));
163
164 return hary->numHosts;
165}
166
167/* Parse type for struct ng_bridge_host_ary */
168static const struct ng_parse_struct_info ng_bridge_host_type_info
169 = NG_BRIDGE_HOST_TYPE_INFO(&ng_ether_enaddr_type);
170static const struct ng_parse_type ng_bridge_host_type = {
171 &ng_parse_struct_type,
172 &ng_bridge_host_type_info
173};
174static const struct ng_parse_array_info ng_bridge_hary_type_info = {
175 &ng_bridge_host_type,
176 ng_bridge_getTableLength
177};
178static const struct ng_parse_type ng_bridge_hary_type = {
179 &ng_parse_array_type,
180 &ng_bridge_hary_type_info
181};
182static const struct ng_parse_struct_info ng_bridge_host_ary_type_info
183 = NG_BRIDGE_HOST_ARY_TYPE_INFO(&ng_bridge_hary_type);
184static const struct ng_parse_type ng_bridge_host_ary_type = {
185 &ng_parse_struct_type,
186 &ng_bridge_host_ary_type_info
187};
188
189/* Parse type for struct ng_bridge_config */
190static const struct ng_parse_fixedarray_info ng_bridge_ipfwary_type_info = {
191 &ng_parse_uint8_type,
192 NG_BRIDGE_MAX_LINKS
193};
194static const struct ng_parse_type ng_bridge_ipfwary_type = {
195 &ng_parse_fixedarray_type,
196 &ng_bridge_ipfwary_type_info
197};
198static const struct ng_parse_struct_info ng_bridge_config_type_info
199 = NG_BRIDGE_CONFIG_TYPE_INFO(&ng_bridge_ipfwary_type);
200static const struct ng_parse_type ng_bridge_config_type = {
201 &ng_parse_struct_type,
202 &ng_bridge_config_type_info
203};
204
205/* Parse type for struct ng_bridge_link_stat */
206static const struct ng_parse_struct_info
207 ng_bridge_stats_type_info = NG_BRIDGE_STATS_TYPE_INFO;
208static const struct ng_parse_type ng_bridge_stats_type = {
209 &ng_parse_struct_type,
210 &ng_bridge_stats_type_info
211};
212
213/* List of commands and how to convert arguments to/from ASCII */
214static const struct ng_cmdlist ng_bridge_cmdlist[] = {
215 {
216 NGM_BRIDGE_COOKIE,
217 NGM_BRIDGE_SET_CONFIG,
218 "setconfig",
219 &ng_bridge_config_type,
220 NULL
221 },
222 {
223 NGM_BRIDGE_COOKIE,
224 NGM_BRIDGE_GET_CONFIG,
225 "getconfig",
226 NULL,
227 &ng_bridge_config_type
228 },
229 {
230 NGM_BRIDGE_COOKIE,
231 NGM_BRIDGE_RESET,
232 "reset",
233 NULL,
234 NULL
235 },
236 {
237 NGM_BRIDGE_COOKIE,
238 NGM_BRIDGE_GET_STATS,
239 "getstats",
240 &ng_parse_uint32_type,
241 &ng_bridge_stats_type
242 },
243 {
244 NGM_BRIDGE_COOKIE,
245 NGM_BRIDGE_CLR_STATS,
246 "clrstats",
247 &ng_parse_uint32_type,
248 NULL
249 },
250 {
251 NGM_BRIDGE_COOKIE,
252 NGM_BRIDGE_GETCLR_STATS,
253 "getclrstats",
254 &ng_parse_uint32_type,
255 &ng_bridge_stats_type
256 },
257 {
258 NGM_BRIDGE_COOKIE,
259 NGM_BRIDGE_GET_TABLE,
260 "gettable",
261 NULL,
262 &ng_bridge_host_ary_type
263 },
264 { 0 }
265};
266
267/* Node type descriptor */
268static struct ng_type ng_bridge_typestruct = {
269 NG_VERSION,
270 NG_BRIDGE_NODE_TYPE,
271 NULL,
272 ng_bridge_constructor,
273 ng_bridge_rcvmsg,
274 ng_bridge_rmnode,
275 ng_bridge_newhook,
276 NULL,
277 NULL,
278 ng_bridge_rcvdata,
279 ng_bridge_disconnect,
280 ng_bridge_cmdlist,
281};
282NETGRAPH_INIT(bridge, &ng_bridge_typestruct);
283
284/* Depend on ng_ether so we can use the Ethernet parse type */
285MODULE_DEPEND(ng_bridge, ng_ether, 1, 1, 1);
286
287/******************************************************************
288 NETGRAPH NODE METHODS
289******************************************************************/
290
291/*
292 * Node constructor
293 */
294static int
295ng_bridge_constructor(node_p *nodep)
296{
297 priv_p priv;
298 int error;
299
300 /* Allocate and initialize private info */
301 MALLOC(priv, priv_p, sizeof(*priv), M_NETGRAPH, M_NOWAIT | M_ZERO);
302 if (priv == NULL)
303 return (ENOMEM);
304 callout_init(&priv->timer, 0);
305
306 /* Allocate and initialize hash table, etc. */
307 MALLOC(priv->tab, struct ng_bridge_bucket *,
308 MIN_BUCKETS * sizeof(*priv->tab), M_NETGRAPH, M_NOWAIT | M_ZERO);
309 if (priv->tab == NULL) {
310 FREE(priv, M_NETGRAPH);
311 return (ENOMEM);
312 }
313 priv->numBuckets = MIN_BUCKETS;
314 priv->hashMask = MIN_BUCKETS - 1;
315 priv->conf.debugLevel = 1;
316 priv->conf.loopTimeout = DEFAULT_LOOP_TIMEOUT;
317 priv->conf.maxStaleness = DEFAULT_MAX_STALENESS;
318 priv->conf.minStableAge = DEFAULT_MIN_STABLE_AGE;
319
320 /* Call superclass constructor */
321 if ((error = ng_make_node_common(&ng_bridge_typestruct, nodep))) {
322 FREE(priv, M_NETGRAPH);
323 return (error);
324 }
325 (*nodep)->private = priv;
326 priv->node = *nodep;
327
328 /* Start timer by faking a timeout event */
329 (*nodep)->refs++;
330 ng_bridge_timeout(*nodep);
331 return (0);
332}
333
334/*
335 * Method for attaching a new hook
336 */
337static int
338ng_bridge_newhook(node_p node, hook_p hook, const char *name)
339{
340 const priv_p priv = node->private;
341
342 /* Check for a link hook */
343 if (strncmp(name, NG_BRIDGE_HOOK_LINK_PREFIX,
344 strlen(NG_BRIDGE_HOOK_LINK_PREFIX)) == 0) {
345 const char *cp;
346 char *eptr;
347 u_long linkNum;
348
349 cp = name + strlen(NG_BRIDGE_HOOK_LINK_PREFIX);
350 if (!isdigit(*cp) || (cp[0] == '0' && cp[1] != '\0'))
351 return (EINVAL);
352 linkNum = strtoul(cp, &eptr, 10);
353 if (*eptr != '\0' || linkNum >= NG_BRIDGE_MAX_LINKS)
354 return (EINVAL);
355 if (priv->links[linkNum] != NULL)
356 return (EISCONN);
357 MALLOC(priv->links[linkNum], struct ng_bridge_link *,
358 sizeof(*priv->links[linkNum]), M_NETGRAPH, M_NOWAIT|M_ZERO);
359 if (priv->links[linkNum] == NULL)
360 return (ENOMEM);
361 priv->links[linkNum]->hook = hook;
362 LINK_NUM(hook) = linkNum;
363 priv->numLinks++;
364 return (0);
365 }
366
367 /* Unknown hook name */
368 return (EINVAL);
369}
370
371/*
372 * Receive a control message
373 */
374static int
375ng_bridge_rcvmsg(node_p node, struct ng_mesg *msg, const char *retaddr,
376 struct ng_mesg **rptr, hook_p lasthook)
377{
378 const priv_p priv = node->private;
379 struct ng_mesg *resp = NULL;
380 int error = 0;
381
382 switch (msg->header.typecookie) {
383 case NGM_BRIDGE_COOKIE:
384 switch (msg->header.cmd) {
385 case NGM_BRIDGE_GET_CONFIG:
386 {
387 struct ng_bridge_config *conf;
388
389 NG_MKRESPONSE(resp, msg,
390 sizeof(struct ng_bridge_config), M_NOWAIT);
391 if (resp == NULL) {
392 error = ENOMEM;
393 break;
394 }
395 conf = (struct ng_bridge_config *)resp->data;
396 *conf = priv->conf; /* no sanity checking needed */
397 break;
398 }
399 case NGM_BRIDGE_SET_CONFIG:
400 {
401 struct ng_bridge_config *conf;
402 int i;
403
404 if (msg->header.arglen
405 != sizeof(struct ng_bridge_config)) {
406 error = EINVAL;
407 break;
408 }
409 conf = (struct ng_bridge_config *)msg->data;
410 priv->conf = *conf;
411 for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++)
412 priv->conf.ipfw[i] = !!priv->conf.ipfw[i];
413 break;
414 }
415 case NGM_BRIDGE_RESET:
416 {
417 int i;
418
419 /* Flush all entries in the hash table */
420 ng_bridge_remove_hosts(priv, -1);
421
422 /* Reset all loop detection counters and stats */
423 for (i = 0; i < NG_BRIDGE_MAX_LINKS; i++) {
424 if (priv->links[i] == NULL)
425 continue;
426 priv->links[i]->loopCount = 0;
427 bzero(&priv->links[i]->stats,
428 sizeof(priv->links[i]->stats));
429 }
430 break;
431 }
432 case NGM_BRIDGE_GET_STATS:
433 case NGM_BRIDGE_CLR_STATS:
434 case NGM_BRIDGE_GETCLR_STATS:
435 {
436 struct ng_bridge_link *link;
437 int linkNum;
438
439 /* Get link number */
440 if (msg->header.arglen != sizeof(u_int32_t)) {
441 error = EINVAL;
442 break;
443 }
444 linkNum = *((u_int32_t *)msg->data);
445 if (linkNum < 0 || linkNum >= NG_BRIDGE_MAX_LINKS) {
446 error = EINVAL;
447 break;
448 }
449 if ((link = priv->links[linkNum]) == NULL) {
450 error = ENOTCONN;
451 break;
452 }
453
454 /* Get/clear stats */
455 if (msg->header.cmd != NGM_BRIDGE_CLR_STATS) {
456 NG_MKRESPONSE(resp, msg,
457 sizeof(link->stats), M_NOWAIT);
458 if (resp == NULL) {
459 error = ENOMEM;
460 break;
461 }
462 bcopy(&link->stats,
463 resp->data, sizeof(link->stats));
464 }
465 if (msg->header.cmd != NGM_BRIDGE_GET_STATS)
466 bzero(&link->stats, sizeof(link->stats));
467 break;
468 }
469 case NGM_BRIDGE_GET_TABLE:
470 {
471 struct ng_bridge_host_ary *ary;
472 struct ng_bridge_hent *hent;
473 int i = 0, bucket;
474
475 NG_MKRESPONSE(resp, msg, sizeof(*ary)
476 + (priv->numHosts * sizeof(*ary->hosts)), M_NOWAIT);
477 if (resp == NULL) {
478 error = ENOMEM;
479 break;
480 }
481 ary = (struct ng_bridge_host_ary *)resp->data;
482 ary->numHosts = priv->numHosts;
483 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
484 SLIST_FOREACH(hent, &priv->tab[bucket], next)
485 ary->hosts[i++] = hent->host;
486 }
487 break;
488 }
489 default:
490 error = EINVAL;
491 break;
492 }
493 break;
494 default:
495 error = EINVAL;
496 break;
497 }
498
499 /* Done */
500 if (rptr)
501 *rptr = resp;
502 else if (resp != NULL)
503 FREE(resp, M_NETGRAPH);
504 FREE(msg, M_NETGRAPH);
505 return (error);
506}
507
508/*
509 * Receive data on a hook
510 */
511static int
512ng_bridge_rcvdata(hook_p hook, struct mbuf *m, meta_p meta,
513 struct mbuf **ret_m, meta_p *ret_meta, struct ng_mesg **resp)
514{
515 const node_p node = hook->node;
516 const priv_p priv = node->private;
517 struct ng_bridge_host *host;
518 struct ng_bridge_link *link;
519 struct ether_header *eh;
520 int error = 0, linkNum;
521 int i, manycast;
522
523 /* Get link number */
524 linkNum = LINK_NUM(hook);
525 KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
526 ("%s: linkNum=%u", __FUNCTION__, linkNum));
527 link = priv->links[linkNum];
528 KASSERT(link != NULL, ("%s: link%d null", __FUNCTION__, linkNum));
529
530 /* Sanity check packet and pull up header */
531 if (m->m_pkthdr.len < ETHER_HDR_LEN) {
532 link->stats.recvRunts++;
533 NG_FREE_DATA(m, meta);
534 return (EINVAL);
535 }
536 if (m->m_len < ETHER_HDR_LEN && !(m = m_pullup(m, ETHER_HDR_LEN))) {
537 link->stats.memoryFailures++;
538 NG_FREE_META(meta);
539 return (ENOBUFS);
540 }
541 eh = mtod(m, struct ether_header *);
542 if ((eh->ether_shost[0] & 1) != 0) {
543 link->stats.recvInvalid++;
544 NG_FREE_DATA(m, meta);
545 return (EINVAL);
546 }
547
548 /* Is link disabled due to a loopback condition? */
549 if (link->loopCount != 0) {
550 link->stats.loopDrops++;
551 NG_FREE_DATA(m, meta);
552 return (ELOOP); /* XXX is this an appropriate error? */
553 }
554
555 /* Update stats */
556 link->stats.recvPackets++;
557 link->stats.recvOctets += m->m_pkthdr.len;
558 if ((manycast = (eh->ether_dhost[0] & 1)) != 0) {
559 if (ETHER_EQUAL(eh->ether_dhost, ng_bridge_bcast_addr)) {
560 link->stats.recvBroadcasts++;
561 manycast = 2;
562 } else
563 link->stats.recvMulticasts++;
564 }
565
566 /* Look up packet's source Ethernet address in hashtable */
567 if ((host = ng_bridge_get(priv, eh->ether_shost)) != NULL) {
568
569 /* Update time since last heard from this host */
570 host->staleness = 0;
571
572 /* Did host jump to a different link? */
573 if (host->linkNum != linkNum) {
574
575 /*
576 * If the host's old link was recently established
577 * on the old link and it's already jumped to a new
578 * link, declare a loopback condition.
579 */
580 if (host->age < priv->conf.minStableAge) {
581
582 /* Log the problem */
583 if (priv->conf.debugLevel >= 2) {
584 struct ifnet *ifp = m->m_pkthdr.rcvif;
585 char suffix[32];
586
587 if (ifp != NULL)
588 snprintf(suffix, sizeof(suffix),
589 " (%s%d)", ifp->if_name,
590 ifp->if_unit);
591 else
592 *suffix = '\0';
593 log(LOG_WARNING, "ng_bridge: %s:"
594 " loopback detected on %s%s\n",
595 ng_bridge_nodename(node),
596 hook->name, suffix);
597 }
598
599 /* Mark link as linka non grata */
600 link->loopCount = priv->conf.loopTimeout;
601 link->stats.loopDetects++;
602
603 /* Forget all hosts on this link */
604 ng_bridge_remove_hosts(priv, linkNum);
605
606 /* Drop packet */
607 link->stats.loopDrops++;
608 NG_FREE_DATA(m, meta);
609 return (ELOOP); /* XXX appropriate? */
610 }
611
612 /* Move host over to new link */
613 host->linkNum = linkNum;
614 host->age = 0;
615 }
616 } else {
617 if (!ng_bridge_put(priv, eh->ether_shost, linkNum)) {
618 link->stats.memoryFailures++;
619 NG_FREE_DATA(m, meta);
620 return (ENOMEM);
621 }
622 }
623
624 /* Run packet through ipfw processing, if enabled */
625 if (priv->conf.ipfw[linkNum] && fw_enable && ip_fw_chk_ptr != NULL) {
626 /* XXX not implemented yet */
627 }
628
629 /*
630 * If unicast and destination host known, deliver to host's link,
631 * unless it is the same link as the packet came in on.
632 */
633 if (!manycast) {
634
635 /* Determine packet destination link */
636 if ((host = ng_bridge_get(priv, eh->ether_dhost)) != NULL) {
637 struct ng_bridge_link *const destLink
638 = priv->links[host->linkNum];
639
640 /* If destination same as incoming link, do nothing */
641 KASSERT(destLink != NULL,
642 ("%s: link%d null", __FUNCTION__, host->linkNum));
643 if (destLink == link) {
644 NG_FREE_DATA(m, meta);
645 return (0);
646 }
647
648 /* Deliver packet out the destination link */
649 destLink->stats.xmitPackets++;
650 destLink->stats.xmitOctets += m->m_pkthdr.len;
651 NG_SEND_DATA(error, destLink->hook, m, meta);
652 return (error);
653 }
654
655 /* Destination host is not known */
656 link->stats.recvUnknown++;
657 }
658
659 /* Distribute unknown, multicast, broadcast pkts to all other links */
660 for (linkNum = i = 0; i < priv->numLinks - 1; linkNum++) {
661 struct ng_bridge_link *const destLink = priv->links[linkNum];
662 meta_p meta2 = NULL;
663 struct mbuf *m2;
664
665 /* Skip incoming link and disconnected links */
666 if (destLink == NULL || destLink == link)
667 continue;
668
669 /* Copy mbuf and meta info */
670 if (++i == priv->numLinks - 1) { /* last link */
671 m2 = m;
672 meta2 = meta;
673 } else {
674 m2 = m_dup(m, M_NOWAIT); /* XXX m_copypacket() */
675 if (m2 == NULL) {
676 link->stats.memoryFailures++;
677 NG_FREE_DATA(m, meta);
678 return (ENOBUFS);
679 }
680 if (meta != NULL
681 && (meta2 = ng_copy_meta(meta)) == NULL) {
682 link->stats.memoryFailures++;
683 m_freem(m2);
684 NG_FREE_DATA(m, meta);
685 return (ENOMEM);
686 }
687 }
688
689 /* Update stats */
690 destLink->stats.xmitPackets++;
691 destLink->stats.xmitOctets += m->m_pkthdr.len;
692 switch (manycast) {
693 case 0: /* unicast */
694 break;
695 case 1: /* multicast */
696 destLink->stats.xmitMulticasts++;
697 break;
698 case 2: /* broadcast */
699 destLink->stats.xmitBroadcasts++;
700 break;
701 }
702
703 /* Send packet */
704 NG_SEND_DATA(error, destLink->hook, m2, meta2);
705 }
706 return (error);
707}
708
709/*
710 * Shutdown node
711 */
712static int
713ng_bridge_rmnode(node_p node)
714{
715 const priv_p priv = node->private;
716
717 ng_unname(node);
718 ng_cutlinks(node); /* frees all link and host info */
719 KASSERT(priv->numLinks == 0 && priv->numHosts == 0,
720 ("%s: numLinks=%d numHosts=%d",
721 __FUNCTION__, priv->numLinks, priv->numHosts));
722 FREE(priv->tab, M_NETGRAPH);
723 FREE(priv, M_NETGRAPH);
724 node->private = NULL;
725 ng_unref(node);
726 return (0);
727}
728
729/*
730 * Hook disconnection.
731 */
732static int
733ng_bridge_disconnect(hook_p hook)
734{
735 const priv_p priv = hook->node->private;
736 int linkNum;
737
738 /* Get link number */
739 linkNum = LINK_NUM(hook);
740 KASSERT(linkNum >= 0 && linkNum < NG_BRIDGE_MAX_LINKS,
741 ("%s: linkNum=%u", __FUNCTION__, linkNum));
742
743 /* Remove all hosts associated with this link */
744 ng_bridge_remove_hosts(priv, linkNum);
745
746 /* Free associated link information */
747 KASSERT(priv->links[linkNum] != NULL, ("%s: no link", __FUNCTION__));
748 FREE(priv->links[linkNum], M_NETGRAPH);
749 priv->links[linkNum] = NULL;
750 priv->numLinks--;
751
752 /* If no more hooks, go away */
753 if (hook->node->numhooks == 0)
754 ng_rmnode(hook->node);
755 return (0);
756}
757
758/******************************************************************
759 HASH TABLE FUNCTIONS
760******************************************************************/
761
762/*
763 * Hash algorithm
764 *
765 * Only hashing bytes 3-6 of the Ethernet address is sufficient and fast.
766 */
767#define HASH(addr,mask) ( (((const u_int16_t *)(addr))[0] \
768 ^ ((const u_int16_t *)(addr))[1] \
769 ^ ((const u_int16_t *)(addr))[2]) & (mask) )
770
771/*
772 * Find a host entry in the table.
773 */
774static struct ng_bridge_host *
775ng_bridge_get(priv_p priv, const u_char *addr)
776{
777 const int bucket = HASH(addr, priv->hashMask);
778 struct ng_bridge_hent *hent;
779
780 SLIST_FOREACH(hent, &priv->tab[bucket], next) {
781 if (ETHER_EQUAL(hent->host.addr, addr))
782 return (&hent->host);
783 }
784 return (NULL);
785}
786
787/*
788 * Add a new host entry to the table. This assumes the host doesn't
789 * already exist in the table. Returns 1 on success, 0 if there
790 * was a memory allocation failure.
791 */
792static int
793ng_bridge_put(priv_p priv, const u_char *addr, int linkNum)
794{
795 const int bucket = HASH(addr, priv->hashMask);
796 struct ng_bridge_hent *hent;
797
798#ifdef INVARIANTS
799 /* Assert that entry does not already exist in hashtable */
800 SLIST_FOREACH(hent, &priv->tab[bucket], next) {
801 KASSERT(!ETHER_EQUAL(hent->host.addr, addr),
802 ("%s: entry %6D exists in table", __FUNCTION__, addr, ":"));
803 }
804#endif
805
806 /* Allocate and initialize new hashtable entry */
807 MALLOC(hent, struct ng_bridge_hent *,
808 sizeof(*hent), M_NETGRAPH, M_NOWAIT);
809 if (hent == NULL)
810 return (0);
811 bcopy(addr, hent->host.addr, ETHER_ADDR_LEN);
812 hent->host.linkNum = linkNum;
813 hent->host.staleness = 0;
814 hent->host.age = 0;
815
816 /* Add new element to hash bucket */
817 SLIST_INSERT_HEAD(&priv->tab[bucket], hent, next);
818 priv->numHosts++;
819
820 /* Resize table if necessary */
821 ng_bridge_rehash(priv);
822 return (1);
823}
824
825/*
826 * Resize the hash table. We try to maintain the number of buckets
827 * such that the load factor is in the range 0.25 to 1.0.
828 *
829 * If we can't get the new memory then we silently fail. This is OK
830 * because things will still work and we'll try again soon anyway.
831 */
832static void
833ng_bridge_rehash(priv_p priv)
834{
835 struct ng_bridge_bucket *newTab;
836 int oldBucket, newBucket;
837 int newNumBuckets;
838 u_int newMask;
839
840 /* Is table too full or too empty? */
841 if (priv->numHosts > priv->numBuckets
842 && (priv->numBuckets << 1) <= MAX_BUCKETS)
843 newNumBuckets = priv->numBuckets << 1;
844 else if (priv->numHosts < (priv->numBuckets >> 2)
845 && (priv->numBuckets >> 2) >= MIN_BUCKETS)
846 newNumBuckets = priv->numBuckets >> 2;
847 else
848 return;
849 newMask = newNumBuckets - 1;
850
851 /* Allocate and initialize new table */
852 MALLOC(newTab, struct ng_bridge_bucket *,
853 newNumBuckets * sizeof(*newTab), M_NETGRAPH, M_NOWAIT | M_ZERO);
854 if (newTab == NULL)
855 return;
856
857 /* Move all entries from old table to new table */
858 for (oldBucket = 0; oldBucket < priv->numBuckets; oldBucket++) {
859 struct ng_bridge_bucket *const oldList = &priv->tab[oldBucket];
860
861 while (!SLIST_EMPTY(oldList)) {
862 struct ng_bridge_hent *const hent
863 = SLIST_FIRST(oldList);
864
865 SLIST_REMOVE_HEAD(oldList, next);
866 newBucket = HASH(hent->host.addr, newMask);
867 SLIST_INSERT_HEAD(&newTab[newBucket], hent, next);
868 }
869 }
870
871 /* Replace old table with new one */
872 if (priv->conf.debugLevel >= 3) {
873 log(LOG_INFO, "ng_bridge: %s: table size %d -> %d\n",
874 ng_bridge_nodename(priv->node),
875 priv->numBuckets, newNumBuckets);
876 }
877 FREE(priv->tab, M_NETGRAPH);
878 priv->numBuckets = newNumBuckets;
879 priv->hashMask = newMask;
880 priv->tab = newTab;
881 return;
882}
883
884/******************************************************************
885 MISC FUNCTIONS
886******************************************************************/
887
888/*
889 * Remove all hosts associated with a specific link from the hashtable.
890 * If linkNum == -1, then remove all hosts in the table.
891 */
892static void
893ng_bridge_remove_hosts(priv_p priv, int linkNum)
894{
895 int bucket;
896
897 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
898 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
899
900 while (*hptr != NULL) {
901 struct ng_bridge_hent *const hent = *hptr;
902
903 if (linkNum == -1 || hent->host.linkNum == linkNum) {
904 *hptr = SLIST_NEXT(hent, next);
905 FREE(hent, M_NETGRAPH);
906 priv->numHosts--;
907 } else
908 hptr = &SLIST_NEXT(hent, next);
909 }
910 }
911}
912
913/*
914 * Handle our once-per-second timeout event. We do two things:
915 * we decrement link->loopCount for those links being muted due to
916 * a detected loopback condition, and we remove any hosts from
917 * the hashtable whom we haven't heard from in a long while.
918 */
919static void
920ng_bridge_timeout(void *arg)
921{
922 const node_p node = arg;
923 const priv_p priv = node->private;
924 int s, bucket;
925 int counter = 0;
926 int linkNum;
927
928 /* Avoid race condition with ng_bridge_shutdown() */
929 s = splnet();
930 if ((node->flags & NG_INVALID) != 0 || priv == NULL) {
931 ng_unref(node);
932 splx(s);
933 return;
934 }
935
936 /* Register a new timeout, keeping the existing node reference */
937 callout_reset(&priv->timer, hz, ng_bridge_timeout, node);
938
939 /* Update host time counters and remove stale entries */
940 for (bucket = 0; bucket < priv->numBuckets; bucket++) {
941 struct ng_bridge_hent **hptr = &SLIST_FIRST(&priv->tab[bucket]);
942
943 while (*hptr != NULL) {
944 struct ng_bridge_hent *const hent = *hptr;
945
946 /* Make sure host's link really exists */
947 KASSERT(priv->links[hent->host.linkNum] != NULL,
948 ("%s: host %6D on nonexistent link %d\n",
949 __FUNCTION__, hent->host.addr, ":",
950 hent->host.linkNum));
951
952 /* Remove hosts we haven't heard from in a while */
953 if (++hent->host.staleness >= priv->conf.maxStaleness) {
954 *hptr = SLIST_NEXT(hent, next);
955 FREE(hent, M_NETGRAPH);
956 priv->numHosts--;
957 } else {
958 if (hent->host.age < 0xffff)
959 hent->host.age++;
960 hptr = &SLIST_NEXT(hent, next);
961 counter++;
962 }
963 }
964 }
965 KASSERT(priv->numHosts == counter,
966 ("%s: hosts: %d != %d", __FUNCTION__, priv->numHosts, counter));
967
968 /* Decrease table size if necessary */
969 ng_bridge_rehash(priv);
970
971 /* Decrease loop counter on muted looped back links */
972 for (counter = linkNum = 0; linkNum < NG_BRIDGE_MAX_LINKS; linkNum++) {
973 struct ng_bridge_link *const link = priv->links[linkNum];
974
975 if (link != NULL) {
976 if (link->loopCount != 0) {
977 link->loopCount--;
978 if (link->loopCount == 0
979 && priv->conf.debugLevel >= 2) {
980 log(LOG_INFO, "ng_bridge: %s:"
981 " restoring looped back link%d\n",
982 ng_bridge_nodename(node), linkNum);
983 }
984 }
985 counter++;
986 }
987 }
988 KASSERT(priv->numLinks == counter,
989 ("%s: links: %d != %d", __FUNCTION__, priv->numLinks, counter));
990
991 /* Done */
992 splx(s);
993}
994
995/*
996 * Return node's "name", even if it doesn't have one.
997 */
998static const char *
999ng_bridge_nodename(node_p node)
1000{
1001 static char name[NG_NODELEN+1];
1002
1003 if (node->name != NULL)
1004 snprintf(name, sizeof(name), "%s", node->name);
1005 else
1006 snprintf(name, sizeof(name), "[%x]", ng_node2ID(node));
1007 return name;
1008}
1009