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