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