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