add mac address to device info
[project/netifd.git] / bridge.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <assert.h>
5 #include <errno.h>
6
7 #include "netifd.h"
8 #include "device.h"
9 #include "interface.h"
10 #include "system.h"
11
12 enum {
13         BRIDGE_ATTR_IFNAME,
14         BRIDGE_ATTR_STP,
15         BRIDGE_ATTR_FORWARD_DELAY,
16         BRIDGE_ATTR_IGMP_SNOOP,
17         BRIDGE_ATTR_AGEING_TIME,
18         BRIDGE_ATTR_HELLO_TIME,
19         BRIDGE_ATTR_MAX_AGE,
20         __BRIDGE_ATTR_MAX
21 };
22
23 static const struct blobmsg_policy bridge_attrs[__BRIDGE_ATTR_MAX] = {
24         [BRIDGE_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
25         [BRIDGE_ATTR_STP] = { "stp", BLOBMSG_TYPE_BOOL },
26         [BRIDGE_ATTR_FORWARD_DELAY] = { "forward_delay", BLOBMSG_TYPE_INT32 },
27         [BRIDGE_ATTR_AGEING_TIME] = { "ageing_time", BLOBMSG_TYPE_INT32 },
28         [BRIDGE_ATTR_HELLO_TIME] = { "hello_time", BLOBMSG_TYPE_INT32 },
29         [BRIDGE_ATTR_MAX_AGE] = { "max_age", BLOBMSG_TYPE_INT32 },
30         [BRIDGE_ATTR_IGMP_SNOOP] = { "igmp_snooping", BLOBMSG_TYPE_BOOL },
31 };
32
33 static const union config_param_info bridge_attr_info[__BRIDGE_ATTR_MAX] = {
34         [BRIDGE_ATTR_IFNAME] = { .type = BLOBMSG_TYPE_STRING },
35 };
36
37 static const struct config_param_list bridge_attr_list = {
38         .n_params = __BRIDGE_ATTR_MAX,
39         .params = bridge_attrs,
40         .info = bridge_attr_info,
41
42         .n_next = 1,
43         .next = { &device_attr_list },
44 };
45
46 static struct device *bridge_create(const char *name, struct blob_attr *attr);
47 static void bridge_config_init(struct device *dev);
48 static void bridge_free(struct device *dev);
49 static void bridge_dump_info(struct device *dev, struct blob_buf *b);
50
51 const struct device_type bridge_device_type = {
52         .name = "Bridge",
53         .config_params = &bridge_attr_list,
54
55         .create = bridge_create,
56         .config_init = bridge_config_init,
57         .free = bridge_free,
58         .dump_info = bridge_dump_info,
59 };
60
61 struct bridge_state {
62         struct device dev;
63         device_state_cb set_state;
64
65         struct bridge_config config;
66         struct blob_attr *ifnames;
67         bool active;
68         bool force_active;
69
70         struct list_head members;
71         int n_present;
72 };
73
74 struct bridge_member {
75         struct list_head list;
76         struct bridge_state *bst;
77         struct device_user dev;
78         bool present;
79 };
80
81 static void bridge_free_member(struct bridge_member *bm);
82
83 static int
84 bridge_disable_member(struct bridge_member *bm)
85 {
86         struct bridge_state *bst = bm->bst;
87
88         if (!bm->present)
89                 return 0;
90
91         system_bridge_delif(&bst->dev, bm->dev.dev);
92         device_release(&bm->dev);
93
94         return 0;
95 }
96
97 static int
98 bridge_enable_member(struct bridge_member *bm)
99 {
100         struct bridge_state *bst = bm->bst;
101         int ret;
102
103         if (!bm->present)
104                 return 0;
105
106         ret = device_claim(&bm->dev);
107         if (ret < 0)
108                 goto error;
109
110         ret = system_bridge_addif(&bst->dev, bm->dev.dev);
111         if (ret < 0)
112                 goto error;
113
114         return 0;
115
116 error:
117         bm->present = false;
118         bst->n_present--;
119         return ret;
120 }
121
122 static void
123 bridge_remove_member(struct bridge_member *bm)
124 {
125         struct bridge_state *bst = bm->bst;
126
127         if (!bm->present)
128                 return;
129
130         bm->present = false;
131         bm->bst->n_present--;
132         if (bst->dev.active)
133                 bridge_disable_member(bm);
134
135         bst->force_active = false;
136         if (bst->n_present == 0)
137                 device_set_present(&bst->dev, false);
138 }
139
140 static void
141 bridge_member_cb(struct device_user *dev, enum device_event ev)
142 {
143         struct bridge_member *bm = container_of(dev, struct bridge_member, dev);
144         struct bridge_state *bst = bm->bst;
145
146         switch (ev) {
147         case DEV_EVENT_ADD:
148                 assert(!bm->present);
149
150                 bm->present = true;
151                 bst->n_present++;
152
153                 if (bst->dev.active)
154                         bridge_enable_member(bm);
155                 else if (bst->n_present == 1)
156                         device_set_present(&bst->dev, true);
157
158                 break;
159         case DEV_EVENT_REMOVE:
160                 if (!bm->present)
161                         return;
162
163                 if (dev->hotplug)
164                         bridge_free_member(bm);
165                 else
166                         bridge_remove_member(bm);
167
168                 break;
169         default:
170                 return;
171         }
172 }
173
174 static int
175 bridge_set_down(struct bridge_state *bst)
176 {
177         struct bridge_member *bm;
178
179         bst->set_state(&bst->dev, false);
180
181         list_for_each_entry(bm, &bst->members, list)
182                 bridge_disable_member(bm);
183
184         system_bridge_delbr(&bst->dev);
185
186         return 0;
187 }
188
189 static int
190 bridge_set_up(struct bridge_state *bst)
191 {
192         struct bridge_member *bm;
193         int ret;
194
195         if (!bst->force_active && !bst->n_present)
196                 return -ENOENT;
197
198         ret = system_bridge_addbr(&bst->dev, &bst->config);
199         if (ret < 0)
200                 goto out;
201
202         list_for_each_entry(bm, &bst->members, list)
203                 bridge_enable_member(bm);
204
205         if (!bst->force_active && !bst->n_present) {
206                 /* initialization of all member interfaces failed */
207                 system_bridge_delbr(&bst->dev);
208                 device_set_present(&bst->dev, false);
209                 return -ENOENT;
210         }
211
212         ret = bst->set_state(&bst->dev, true);
213         if (ret < 0)
214                 bridge_set_down(bst);
215
216 out:
217         return ret;
218 }
219
220 static int
221 bridge_set_state(struct device *dev, bool up)
222 {
223         struct bridge_state *bst;
224
225         bst = container_of(dev, struct bridge_state, dev);
226
227         if (up)
228                 return bridge_set_up(bst);
229         else
230                 return bridge_set_down(bst);
231 }
232
233 static struct bridge_member *
234 bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
235 {
236         struct bridge_member *bm;
237
238         bm = calloc(1, sizeof(*bm));
239         bm->bst = bst;
240         bm->dev.cb = bridge_member_cb;
241         bm->dev.hotplug = hotplug;
242         list_add_tail(&bm->list, &bst->members);
243
244         device_add_user(&bm->dev, dev);
245
246         return bm;
247 }
248
249 static void
250 bridge_free_member(struct bridge_member *bm)
251 {
252         bridge_remove_member(bm);
253         device_remove_user(&bm->dev);
254         list_del(&bm->list);
255         free(bm);
256 }
257
258 static void
259 bridge_add_member(struct bridge_state *bst, const char *name)
260 {
261         struct device *dev;
262
263         dev = device_get(name, true);
264         if (!dev)
265                 return;
266
267         bridge_create_member(bst, dev, false);
268 }
269
270 static int
271 bridge_hotplug_add(struct device *dev, struct device *member)
272 {
273         struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
274
275         bridge_create_member(bst, member, true);
276
277         return 0;
278 }
279
280 static int
281 bridge_hotplug_del(struct device *dev, struct device *member)
282 {
283         struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
284         struct bridge_member *bm;
285
286         list_for_each_entry(bm, &bst->members, list) {
287                 if (bm->dev.dev != member)
288                         continue;
289
290                 bridge_free_member(bm);
291                 return 0;
292         }
293
294         return UBUS_STATUS_NOT_FOUND;
295 }
296
297 static int
298 bridge_hotplug_prepare(struct device *dev)
299 {
300         struct bridge_state *bst;
301
302         bst = container_of(dev, struct bridge_state, dev);
303         bst->force_active = true;
304         device_set_present(&bst->dev, true);
305
306         return 0;
307 }
308
309 static const struct device_hotplug_ops bridge_ops = {
310         .prepare = bridge_hotplug_prepare,
311         .add = bridge_hotplug_add,
312         .del = bridge_hotplug_del
313 };
314
315 static void
316 bridge_free(struct device *dev)
317 {
318         struct bridge_state *bst;
319         struct bridge_member *bm;
320
321         device_cleanup(dev);
322         bst = container_of(dev, struct bridge_state, dev);
323         while (!list_empty(&bst->members)) {
324                 bm = list_first_entry(&bst->members, struct bridge_member, list);
325                 bridge_free_member(bm);
326         }
327         free(bst);
328 }
329
330 static void
331 bridge_dump_info(struct device *dev, struct blob_buf *b)
332 {
333         struct bridge_state *bst;
334         struct bridge_member *bm;
335         void *list;
336
337         bst = container_of(dev, struct bridge_state, dev);
338
339         system_if_dump_info(dev, b);
340         list = blobmsg_open_array(b, "bridge-members");
341         list_for_each_entry(bm, &bst->members, list) {
342                 blobmsg_add_string(b, NULL, bm->dev.dev->ifname);
343         }
344         blobmsg_close_array(b, list);
345 }
346
347 static void
348 bridge_config_init(struct device *dev)
349 {
350         struct bridge_state *bst;
351         struct blob_attr *cur;
352         int rem;
353
354         bst = container_of(dev, struct bridge_state, dev);
355
356         if (!bst->ifnames)
357                 return;
358
359         blobmsg_for_each_attr(cur, bst->ifnames, rem) {
360                 bridge_add_member(bst, blobmsg_data(cur));
361         }
362 }
363
364 static void
365 bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
366 {
367         struct bridge_config *cfg = &bst->config;
368         struct blob_attr *cur;
369
370         /* defaults */
371         cfg->stp = true;
372         cfg->forward_delay = 1;
373         cfg->igmp_snoop = true;
374
375         if ((cur = tb[BRIDGE_ATTR_STP]))
376                 cfg->stp = blobmsg_get_bool(cur);
377
378         if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
379                 cfg->forward_delay = blobmsg_get_u32(cur);
380
381         if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
382                 cfg->igmp_snoop = blobmsg_get_bool(cur);
383
384         if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
385                 cfg->ageing_time = blobmsg_get_u32(cur);
386                 cfg->flags |= BRIDGE_OPT_AGEING_TIME;
387         }
388
389         if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
390                 cfg->hello_time = blobmsg_get_u32(cur);
391                 cfg->flags |= BRIDGE_OPT_HELLO_TIME;
392         }
393
394         if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
395                 cfg->max_age = blobmsg_get_u32(cur);
396                 cfg->flags |= BRIDGE_OPT_MAX_AGE;
397         }
398 }
399
400 static struct device *
401 bridge_create(const char *name, struct blob_attr *attr)
402 {
403         struct blob_attr *tb_dev[__DEV_ATTR_MAX];
404         struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
405         struct bridge_state *bst;
406         struct device *dev = NULL;
407
408         blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
409                 blob_data(attr), blob_len(attr));
410         blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
411                 blob_data(attr), blob_len(attr));
412
413         bst = calloc(1, sizeof(*bst));
414         if (!bst)
415                 return NULL;
416
417         dev = &bst->dev;
418         device_init(dev, &bridge_device_type, name);
419         device_init_settings(dev, tb_dev);
420         dev->config_pending = true;
421         bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
422         bridge_apply_settings(bst, tb_br);
423
424         bst->set_state = dev->set_state;
425         dev->set_state = bridge_set_state;
426
427         dev->hotplug_ops = &bridge_ops;
428
429         INIT_LIST_HEAD(&bst->members);
430
431         return dev;
432 }
433
434