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