0c538baa8782885fd6f50c3ef5f58bd43a2a36c3
[project/netifd.git] / bridge.c
1 /*
2  * netifd - network interface daemon
3  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <assert.h>
18 #include <errno.h>
19
20 #include "netifd.h"
21 #include "device.h"
22 #include "interface.h"
23 #include "system.h"
24
25 enum {
26         BRIDGE_ATTR_IFNAME,
27         BRIDGE_ATTR_STP,
28         BRIDGE_ATTR_FORWARD_DELAY,
29         BRIDGE_ATTR_PRIORITY,
30         BRIDGE_ATTR_IGMP_SNOOP,
31         BRIDGE_ATTR_AGEING_TIME,
32         BRIDGE_ATTR_HELLO_TIME,
33         BRIDGE_ATTR_MAX_AGE,
34         BRIDGE_ATTR_BRIDGE_EMPTY,
35         BRIDGE_ATTR_MULTICAST_QUERIER,
36         BRIDGE_ATTR_HASH_MAX,
37         BRIDGE_ATTR_ROBUSTNESS,
38         BRIDGE_ATTR_QUERY_INTERVAL,
39         BRIDGE_ATTR_QUERY_RESPONSE_INTERVAL,
40         BRIDGE_ATTR_LAST_MEMBER_INTERVAL,
41         __BRIDGE_ATTR_MAX
42 };
43
44 static const struct blobmsg_policy bridge_attrs[__BRIDGE_ATTR_MAX] = {
45         [BRIDGE_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
46         [BRIDGE_ATTR_STP] = { "stp", BLOBMSG_TYPE_BOOL },
47         [BRIDGE_ATTR_FORWARD_DELAY] = { "forward_delay", BLOBMSG_TYPE_INT32 },
48         [BRIDGE_ATTR_PRIORITY] = { "priority", BLOBMSG_TYPE_INT32 },
49         [BRIDGE_ATTR_AGEING_TIME] = { "ageing_time", BLOBMSG_TYPE_INT32 },
50         [BRIDGE_ATTR_HELLO_TIME] = { "hello_time", BLOBMSG_TYPE_INT32 },
51         [BRIDGE_ATTR_MAX_AGE] = { "max_age", BLOBMSG_TYPE_INT32 },
52         [BRIDGE_ATTR_IGMP_SNOOP] = { "igmp_snooping", BLOBMSG_TYPE_BOOL },
53         [BRIDGE_ATTR_BRIDGE_EMPTY] = { "bridge_empty", BLOBMSG_TYPE_BOOL },
54         [BRIDGE_ATTR_MULTICAST_QUERIER] = { "multicast_querier", BLOBMSG_TYPE_BOOL },
55         [BRIDGE_ATTR_HASH_MAX] = { "hash_max", BLOBMSG_TYPE_INT32 },
56         [BRIDGE_ATTR_ROBUSTNESS] = { "robustness", BLOBMSG_TYPE_INT32 },
57         [BRIDGE_ATTR_QUERY_INTERVAL] = { "query_interval", BLOBMSG_TYPE_INT32 },
58         [BRIDGE_ATTR_QUERY_RESPONSE_INTERVAL] = { "query_response_interval", BLOBMSG_TYPE_INT32 },
59         [BRIDGE_ATTR_LAST_MEMBER_INTERVAL] = { "last_member_interval", BLOBMSG_TYPE_INT32 },
60 };
61
62 static const struct uci_blob_param_info bridge_attr_info[__BRIDGE_ATTR_MAX] = {
63         [BRIDGE_ATTR_IFNAME] = { .type = BLOBMSG_TYPE_STRING },
64 };
65
66 static const struct uci_blob_param_list bridge_attr_list = {
67         .n_params = __BRIDGE_ATTR_MAX,
68         .params = bridge_attrs,
69         .info = bridge_attr_info,
70
71         .n_next = 1,
72         .next = { &device_attr_list },
73 };
74
75 static struct device *bridge_create(const char *name, struct device_type *devtype,
76         struct blob_attr *attr);
77 static void bridge_config_init(struct device *dev);
78 static void bridge_free(struct device *dev);
79 static void bridge_dump_info(struct device *dev, struct blob_buf *b);
80 enum dev_change_type
81 bridge_reload(struct device *dev, struct blob_attr *attr);
82
83 struct device_type bridge_device_type = {
84         .name = "bridge",
85         .config_params = &bridge_attr_list,
86
87         .create = bridge_create,
88         .config_init = bridge_config_init,
89         .reload = bridge_reload,
90         .free = bridge_free,
91         .dump_info = bridge_dump_info,
92 };
93
94 struct bridge_state {
95         struct device dev;
96         device_state_cb set_state;
97
98         struct blob_attr *config_data;
99         struct bridge_config config;
100         struct blob_attr *ifnames;
101         bool active;
102         bool force_active;
103
104         struct uloop_timeout retry;
105         struct bridge_member *primary_port;
106         struct vlist_tree members;
107         int n_present;
108         int n_failed;
109 };
110
111 struct bridge_member {
112         struct vlist_node node;
113         struct bridge_state *bst;
114         struct device_user dev;
115         bool present;
116         char name[];
117 };
118
119 static void
120 bridge_reset_primary(struct bridge_state *bst)
121 {
122         struct bridge_member *bm;
123
124         if (!bst->primary_port &&
125             (bst->dev.settings.flags & DEV_OPT_MACADDR))
126                 return;
127
128         bst->primary_port = NULL;
129         bst->dev.settings.flags &= ~DEV_OPT_MACADDR;
130         vlist_for_each_element(&bst->members, bm, node) {
131                 uint8_t *macaddr;
132
133                 if (!bm->present)
134                         continue;
135
136                 bst->primary_port = bm;
137                 if (bm->dev.dev->settings.flags & DEV_OPT_MACADDR)
138                         macaddr = bm->dev.dev->settings.macaddr;
139                 else
140                         macaddr = bm->dev.dev->orig_settings.macaddr;
141                 memcpy(bst->dev.settings.macaddr, macaddr, 6);
142                 bst->dev.settings.flags |= DEV_OPT_MACADDR;
143                 return;
144         }
145 }
146
147 static int
148 bridge_disable_member(struct bridge_member *bm)
149 {
150         struct bridge_state *bst = bm->bst;
151
152         if (!bm->present)
153                 return 0;
154
155         system_bridge_delif(&bst->dev, bm->dev.dev);
156         device_release(&bm->dev);
157
158         device_broadcast_event(&bst->dev, DEV_EVENT_TOPO_CHANGE);
159
160         return 0;
161 }
162
163 static int
164 bridge_enable_interface(struct bridge_state *bst)
165 {
166         int ret;
167
168         if (bst->active)
169                 return 0;
170
171         ret = system_bridge_addbr(&bst->dev, &bst->config);
172         if (ret < 0)
173                 return ret;
174
175         bst->active = true;
176         return 0;
177 }
178
179 static void
180 bridge_disable_interface(struct bridge_state *bst)
181 {
182         if (!bst->active)
183                 return;
184
185         system_bridge_delbr(&bst->dev);
186         bst->active = false;
187 }
188
189 static int
190 bridge_enable_member(struct bridge_member *bm)
191 {
192         struct bridge_state *bst = bm->bst;
193         int ret;
194
195         if (!bm->present)
196                 return 0;
197
198         ret = bridge_enable_interface(bst);
199         if (ret)
200                 goto error;
201
202         /* Disable IPv6 for bridge members */
203         if (!(bm->dev.dev->settings.flags & DEV_OPT_IPV6)) {
204                 bm->dev.dev->settings.ipv6 = 0;
205                 bm->dev.dev->settings.flags |= DEV_OPT_IPV6;
206         }
207
208         ret = device_claim(&bm->dev);
209         if (ret < 0)
210                 goto error;
211
212         ret = system_bridge_addif(&bst->dev, bm->dev.dev);
213         if (ret < 0) {
214                 D(DEVICE, "Bridge device %s could not be added\n", bm->dev.dev->ifname);
215                 goto error;
216         }
217
218         device_set_present(&bst->dev, true);
219         device_broadcast_event(&bst->dev, DEV_EVENT_TOPO_CHANGE);
220
221         return 0;
222
223 error:
224         bst->n_failed++;
225         bm->present = false;
226         bst->n_present--;
227         device_release(&bm->dev);
228
229         return ret;
230 }
231
232 static void
233 bridge_remove_member(struct bridge_member *bm)
234 {
235         struct bridge_state *bst = bm->bst;
236
237         if (!bm->present)
238                 return;
239
240         if (bm == bst->primary_port)
241                 bridge_reset_primary(bst);
242
243         if (bst->dev.active)
244                 bridge_disable_member(bm);
245
246         bm->present = false;
247         bm->bst->n_present--;
248
249         if (bst->config.bridge_empty)
250                 return;
251
252         bst->force_active = false;
253         if (bst->n_present == 0)
254                 device_set_present(&bst->dev, false);
255 }
256
257 static void
258 bridge_free_member(struct bridge_member *bm)
259 {
260         struct device *dev = bm->dev.dev;
261
262         bridge_remove_member(bm);
263         device_remove_user(&bm->dev);
264
265         /*
266          * When reloading the config and moving a device from one bridge to
267          * another, the other bridge may have tried to claim this device
268          * before it was removed here.
269          * Ensure that claiming the device is retried by toggling its present
270          * state
271          */
272         if (dev->present) {
273                 device_set_present(dev, false);
274                 device_set_present(dev, true);
275         }
276
277         free(bm);
278 }
279
280 static void
281 bridge_check_retry(struct bridge_state *bst)
282 {
283         if (!bst->n_failed)
284                 return;
285
286         uloop_timeout_set(&bst->retry, 100);
287 }
288
289 static void
290 bridge_member_cb(struct device_user *dev, enum device_event ev)
291 {
292         struct bridge_member *bm = container_of(dev, struct bridge_member, dev);
293         struct bridge_state *bst = bm->bst;
294
295         switch (ev) {
296         case DEV_EVENT_ADD:
297                 assert(!bm->present);
298
299                 bm->present = true;
300                 bst->n_present++;
301
302                 if (bst->n_present == 1)
303                         device_set_present(&bst->dev, true);
304                 if (bst->dev.active && !bridge_enable_member(bm)) {
305                         /*
306                          * Adding a bridge member can overwrite the bridge mtu
307                          * in the kernel, apply the bridge settings in case the
308                          * bridge mtu is set
309                          */
310                         system_if_apply_settings(&bst->dev, &bst->dev.settings,
311                                                  DEV_OPT_MTU | DEV_OPT_MTU6);
312                 }
313
314                 break;
315         case DEV_EVENT_REMOVE:
316                 if (dev->hotplug) {
317                         vlist_delete(&bst->members, &bm->node);
318                         return;
319                 }
320
321                 if (bm->present)
322                         bridge_remove_member(bm);
323
324                 break;
325         default:
326                 return;
327         }
328 }
329
330 static int
331 bridge_set_down(struct bridge_state *bst)
332 {
333         struct bridge_member *bm;
334
335         bst->set_state(&bst->dev, false);
336
337         vlist_for_each_element(&bst->members, bm, node)
338                 bridge_disable_member(bm);
339
340         bridge_disable_interface(bst);
341
342         return 0;
343 }
344
345 static int
346 bridge_set_up(struct bridge_state *bst)
347 {
348         struct bridge_member *bm;
349         int ret;
350
351         if (!bst->n_present) {
352                 if (!bst->force_active)
353                         return -ENOENT;
354
355                 ret = bridge_enable_interface(bst);
356                 if (ret)
357                         return ret;
358         }
359
360         bst->n_failed = 0;
361         vlist_for_each_element(&bst->members, bm, node)
362                 bridge_enable_member(bm);
363         bridge_check_retry(bst);
364
365         if (!bst->force_active && !bst->n_present) {
366                 /* initialization of all member interfaces failed */
367                 bridge_disable_interface(bst);
368                 device_set_present(&bst->dev, false);
369                 return -ENOENT;
370         }
371
372         bridge_reset_primary(bst);
373         ret = bst->set_state(&bst->dev, true);
374         if (ret < 0)
375                 bridge_set_down(bst);
376
377         return ret;
378 }
379
380 static int
381 bridge_set_state(struct device *dev, bool up)
382 {
383         struct bridge_state *bst;
384
385         bst = container_of(dev, struct bridge_state, dev);
386
387         if (up)
388                 return bridge_set_up(bst);
389         else
390                 return bridge_set_down(bst);
391 }
392
393 static struct bridge_member *
394 bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
395 {
396         struct bridge_member *bm;
397
398         bm = calloc(1, sizeof(*bm) + strlen(dev->ifname) + 1);
399         if (!bm)
400                 return NULL;
401
402         bm->bst = bst;
403         bm->dev.cb = bridge_member_cb;
404         bm->dev.hotplug = hotplug;
405         strcpy(bm->name, dev->ifname);
406         bm->dev.dev = dev;
407         vlist_add(&bst->members, &bm->node, bm->name);
408         // Need to look up the bridge member again as the above
409         // created pointer will be freed in case the bridge member
410         // already existed
411         bm = vlist_find(&bst->members, dev->ifname, bm, node);
412         if (hotplug && bm)
413                 bm->node.version = -1;
414
415         return bm;
416 }
417
418 static void
419 bridge_member_update(struct vlist_tree *tree, struct vlist_node *node_new,
420                      struct vlist_node *node_old)
421 {
422         struct bridge_member *bm;
423         struct device *dev;
424
425         if (node_new) {
426                 bm = container_of(node_new, struct bridge_member, node);
427
428                 if (node_old) {
429                         free(bm);
430                         return;
431                 }
432
433                 dev = bm->dev.dev;
434                 bm->dev.dev = NULL;
435                 device_add_user(&bm->dev, dev);
436         }
437
438
439         if (node_old) {
440                 bm = container_of(node_old, struct bridge_member, node);
441                 bridge_free_member(bm);
442         }
443 }
444
445
446 static void
447 bridge_add_member(struct bridge_state *bst, const char *name)
448 {
449         struct device *dev;
450
451         dev = device_get(name, true);
452         if (!dev)
453                 return;
454
455         bridge_create_member(bst, dev, false);
456 }
457
458 static int
459 bridge_hotplug_add(struct device *dev, struct device *member)
460 {
461         struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
462
463         bridge_create_member(bst, member, true);
464
465         return 0;
466 }
467
468 static int
469 bridge_hotplug_del(struct device *dev, struct device *member)
470 {
471         struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
472         struct bridge_member *bm;
473
474         bm = vlist_find(&bst->members, member->ifname, bm, node);
475         if (!bm)
476                 return UBUS_STATUS_NOT_FOUND;
477
478         vlist_delete(&bst->members, &bm->node);
479         return 0;
480 }
481
482 static int
483 bridge_hotplug_prepare(struct device *dev)
484 {
485         struct bridge_state *bst;
486
487         bst = container_of(dev, struct bridge_state, dev);
488         bst->force_active = true;
489         device_set_present(&bst->dev, true);
490
491         return 0;
492 }
493
494 static const struct device_hotplug_ops bridge_ops = {
495         .prepare = bridge_hotplug_prepare,
496         .add = bridge_hotplug_add,
497         .del = bridge_hotplug_del
498 };
499
500 static void
501 bridge_free(struct device *dev)
502 {
503         struct bridge_state *bst;
504
505         bst = container_of(dev, struct bridge_state, dev);
506         vlist_flush_all(&bst->members);
507         free(bst->config_data);
508         free(bst);
509 }
510
511 static void
512 bridge_dump_info(struct device *dev, struct blob_buf *b)
513 {
514         struct bridge_state *bst;
515         struct bridge_member *bm;
516         void *list;
517
518         bst = container_of(dev, struct bridge_state, dev);
519
520         system_if_dump_info(dev, b);
521         list = blobmsg_open_array(b, "bridge-members");
522
523         vlist_for_each_element(&bst->members, bm, node)
524                 blobmsg_add_string(b, NULL, bm->dev.dev->ifname);
525
526         blobmsg_close_array(b, list);
527 }
528
529 static void
530 bridge_config_init(struct device *dev)
531 {
532         struct bridge_state *bst;
533         struct blob_attr *cur;
534         int rem;
535
536         bst = container_of(dev, struct bridge_state, dev);
537
538         if (bst->config.bridge_empty) {
539                 bst->force_active = true;
540                 device_set_present(&bst->dev, true);
541         }
542
543         bst->n_failed = 0;
544         vlist_update(&bst->members);
545         if (bst->ifnames) {
546                 blobmsg_for_each_attr(cur, bst->ifnames, rem) {
547                         bridge_add_member(bst, blobmsg_data(cur));
548                 }
549         }
550         vlist_flush(&bst->members);
551         bridge_check_retry(bst);
552 }
553
554 static void
555 bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
556 {
557         struct bridge_config *cfg = &bst->config;
558         struct blob_attr *cur;
559
560         /* defaults */
561         cfg->stp = false;
562         cfg->forward_delay = 2;
563         cfg->igmp_snoop = true;
564         cfg->multicast_querier = true;
565         cfg->robustness = 2;
566         cfg->query_interval = 12500;
567         cfg->query_response_interval = 1000;
568         cfg->last_member_interval = 100;
569         cfg->hash_max = 512;
570         cfg->bridge_empty = false;
571         cfg->priority = 0x7FFF;
572
573         if ((cur = tb[BRIDGE_ATTR_STP]))
574                 cfg->stp = blobmsg_get_bool(cur);
575
576         if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
577                 cfg->forward_delay = blobmsg_get_u32(cur);
578
579         if ((cur = tb[BRIDGE_ATTR_PRIORITY]))
580                 cfg->priority = blobmsg_get_u32(cur);
581
582         if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
583                 cfg->multicast_querier = cfg->igmp_snoop = blobmsg_get_bool(cur);
584
585         if ((cur = tb[BRIDGE_ATTR_MULTICAST_QUERIER]))
586                 cfg->multicast_querier = blobmsg_get_bool(cur);
587
588         if ((cur = tb[BRIDGE_ATTR_HASH_MAX]))
589                 cfg->hash_max = blobmsg_get_u32(cur);
590
591         if ((cur = tb[BRIDGE_ATTR_ROBUSTNESS])) {
592                 cfg->robustness = blobmsg_get_u32(cur);
593                 cfg->flags |= BRIDGE_OPT_ROBUSTNESS;
594         }
595
596         if ((cur = tb[BRIDGE_ATTR_QUERY_INTERVAL])) {
597                 cfg->query_interval = blobmsg_get_u32(cur);
598                 cfg->flags |= BRIDGE_OPT_QUERY_INTERVAL;
599         }
600
601         if ((cur = tb[BRIDGE_ATTR_QUERY_RESPONSE_INTERVAL])) {
602                 cfg->query_response_interval = blobmsg_get_u32(cur);
603                 cfg->flags |= BRIDGE_OPT_QUERY_RESPONSE_INTERVAL;
604         }
605
606         if ((cur = tb[BRIDGE_ATTR_LAST_MEMBER_INTERVAL])) {
607                 cfg->last_member_interval = blobmsg_get_u32(cur);
608                 cfg->flags |= BRIDGE_OPT_LAST_MEMBER_INTERVAL;
609         }
610
611         if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
612                 cfg->ageing_time = blobmsg_get_u32(cur);
613                 cfg->flags |= BRIDGE_OPT_AGEING_TIME;
614         }
615
616         if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
617                 cfg->hello_time = blobmsg_get_u32(cur);
618                 cfg->flags |= BRIDGE_OPT_HELLO_TIME;
619         }
620
621         if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
622                 cfg->max_age = blobmsg_get_u32(cur);
623                 cfg->flags |= BRIDGE_OPT_MAX_AGE;
624         }
625
626         if ((cur = tb[BRIDGE_ATTR_BRIDGE_EMPTY]))
627                 cfg->bridge_empty = blobmsg_get_bool(cur);
628 }
629
630 enum dev_change_type
631 bridge_reload(struct device *dev, struct blob_attr *attr)
632 {
633         struct blob_attr *tb_dev[__DEV_ATTR_MAX];
634         struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
635         enum dev_change_type ret = DEV_CONFIG_APPLIED;
636         unsigned long diff;
637         struct bridge_state *bst;
638
639         BUILD_BUG_ON(sizeof(diff) < __BRIDGE_ATTR_MAX / 8);
640         BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
641
642         bst = container_of(dev, struct bridge_state, dev);
643         attr = blob_memdup(attr);
644
645         blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
646                 blob_data(attr), blob_len(attr));
647         blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
648                 blob_data(attr), blob_len(attr));
649
650         bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
651         device_init_settings(dev, tb_dev);
652         bridge_apply_settings(bst, tb_br);
653
654         if (bst->config_data) {
655                 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
656                 struct blob_attr *otb_br[__BRIDGE_ATTR_MAX];
657
658                 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
659                         blob_data(bst->config_data), blob_len(bst->config_data));
660
661                 diff = 0;
662                 uci_blob_diff(tb_dev, otb_dev, &device_attr_list, &diff);
663                 if (diff)
664                     ret = DEV_CONFIG_RESTART;
665
666                 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, otb_br,
667                         blob_data(bst->config_data), blob_len(bst->config_data));
668
669                 diff = 0;
670                 uci_blob_diff(tb_br, otb_br, &bridge_attr_list, &diff);
671                 if (diff & ~(1 << BRIDGE_ATTR_IFNAME))
672                     ret = DEV_CONFIG_RESTART;
673
674                 bridge_config_init(dev);
675         }
676
677         free(bst->config_data);
678         bst->config_data = attr;
679         return ret;
680 }
681
682 static void
683 bridge_retry_members(struct uloop_timeout *timeout)
684 {
685         struct bridge_state *bst = container_of(timeout, struct bridge_state, retry);
686         struct bridge_member *bm;
687
688         bst->n_failed = 0;
689         vlist_for_each_element(&bst->members, bm, node) {
690                 if (bm->present)
691                         continue;
692
693                 if (!bm->dev.dev->present)
694                         continue;
695
696                 bm->present = true;
697                 bst->n_present++;
698                 bridge_enable_member(bm);
699         }
700 }
701
702 static struct device *
703 bridge_create(const char *name, struct device_type *devtype,
704         struct blob_attr *attr)
705 {
706         struct bridge_state *bst;
707         struct device *dev = NULL;
708
709         bst = calloc(1, sizeof(*bst));
710         if (!bst)
711                 return NULL;
712
713         dev = &bst->dev;
714         device_init(dev, devtype, name);
715         dev->config_pending = true;
716         bst->retry.cb = bridge_retry_members;
717
718         bst->set_state = dev->set_state;
719         dev->set_state = bridge_set_state;
720
721         dev->hotplug_ops = &bridge_ops;
722
723         vlist_init(&bst->members, avl_strcmp, bridge_member_update);
724         bst->members.keep_old = true;
725         bridge_reload(dev, attr);
726
727         return dev;
728 }