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