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