f2c73b0108b74e9d6f459fe992920f520a62cf30
[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 vlist_tree members;
71 int n_present;
72 };
73
74 struct bridge_member {
75 struct vlist_node node;
76 struct bridge_state *bst;
77 struct device_user dev;
78 bool present;
79 char name[];
80 };
81
82 static int
83 bridge_disable_member(struct bridge_member *bm)
84 {
85 struct bridge_state *bst = bm->bst;
86
87 if (!bm->present)
88 return 0;
89
90 system_bridge_delif(&bst->dev, bm->dev.dev);
91 device_release(&bm->dev);
92
93 return 0;
94 }
95
96 static int
97 bridge_enable_member(struct bridge_member *bm)
98 {
99 struct bridge_state *bst = bm->bst;
100 int ret;
101
102 if (!bm->present)
103 return 0;
104
105 ret = device_claim(&bm->dev);
106 if (ret < 0)
107 goto error;
108
109 ret = system_bridge_addif(&bst->dev, bm->dev.dev);
110 if (ret < 0)
111 goto error;
112
113 return 0;
114
115 error:
116 bm->present = false;
117 bst->n_present--;
118 return ret;
119 }
120
121 static void
122 bridge_remove_member(struct bridge_member *bm)
123 {
124 struct bridge_state *bst = bm->bst;
125
126 if (!bm->present)
127 return;
128
129 bm->present = false;
130 bm->bst->n_present--;
131 if (bst->dev.active)
132 bridge_disable_member(bm);
133
134 bst->force_active = false;
135 if (bst->n_present == 0)
136 device_set_present(&bst->dev, false);
137 }
138
139 static void
140 bridge_member_cb(struct device_user *dev, enum device_event ev)
141 {
142 struct bridge_member *bm = container_of(dev, struct bridge_member, dev);
143 struct bridge_state *bst = bm->bst;
144
145 switch (ev) {
146 case DEV_EVENT_ADD:
147 assert(!bm->present);
148
149 bm->present = true;
150 bst->n_present++;
151
152 if (bst->dev.active)
153 bridge_enable_member(bm);
154 else if (bst->n_present == 1)
155 device_set_present(&bst->dev, true);
156
157 break;
158 case DEV_EVENT_REMOVE:
159 if (!bm->present)
160 return;
161
162 if (dev->hotplug)
163 vlist_delete(&bst->members, &bm->node);
164 else
165 bridge_remove_member(bm);
166
167 break;
168 default:
169 return;
170 }
171 }
172
173 static int
174 bridge_set_down(struct bridge_state *bst)
175 {
176 struct bridge_member *bm;
177
178 bst->set_state(&bst->dev, false);
179
180 vlist_for_each_element(&bst->members, bm, node)
181 bridge_disable_member(bm);
182
183 system_bridge_delbr(&bst->dev);
184
185 return 0;
186 }
187
188 static int
189 bridge_set_up(struct bridge_state *bst)
190 {
191 struct bridge_member *bm;
192 int ret;
193
194 if (!bst->force_active && !bst->n_present)
195 return -ENOENT;
196
197 ret = system_bridge_addbr(&bst->dev, &bst->config);
198 if (ret < 0)
199 goto out;
200
201 vlist_for_each_element(&bst->members, bm, node)
202 bridge_enable_member(bm);
203
204 if (!bst->force_active && !bst->n_present) {
205 /* initialization of all member interfaces failed */
206 system_bridge_delbr(&bst->dev);
207 device_set_present(&bst->dev, false);
208 return -ENOENT;
209 }
210
211 ret = bst->set_state(&bst->dev, true);
212 if (ret < 0)
213 bridge_set_down(bst);
214
215 out:
216 return ret;
217 }
218
219 static int
220 bridge_set_state(struct device *dev, bool up)
221 {
222 struct bridge_state *bst;
223
224 bst = container_of(dev, struct bridge_state, dev);
225
226 if (up)
227 return bridge_set_up(bst);
228 else
229 return bridge_set_down(bst);
230 }
231
232 static struct bridge_member *
233 bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
234 {
235 struct bridge_member *bm;
236
237 bm = calloc(1, sizeof(*bm) + strlen(dev->ifname) + 1);
238 bm->bst = bst;
239 bm->dev.cb = bridge_member_cb;
240 bm->dev.hotplug = hotplug;
241 strcpy(bm->name, dev->ifname);
242 bm->dev.dev = dev;
243 vlist_add(&bst->members, &bm->node, bm->name);
244
245 return bm;
246 }
247
248 static void
249 bridge_member_update(struct vlist_tree *tree, struct vlist_node *node_new,
250 struct vlist_node *node_old)
251 {
252 struct bridge_member *bm;
253 struct device *dev;
254
255 if (node_new) {
256 bm = container_of(node_new, struct bridge_member, node);
257
258 if (node_old) {
259 free(bm);
260 return;
261 }
262
263 dev = bm->dev.dev;
264 bm->dev.dev = NULL;
265 device_add_user(&bm->dev, dev);
266 }
267
268
269 if (node_old) {
270 bm = container_of(node_old, struct bridge_member, node);
271 bridge_remove_member(bm);
272 device_remove_user(&bm->dev);
273 free(bm);
274 }
275 }
276
277
278 static void
279 bridge_add_member(struct bridge_state *bst, const char *name)
280 {
281 struct device *dev;
282
283 dev = device_get(name, true);
284 if (!dev)
285 return;
286
287 bridge_create_member(bst, dev, false);
288 }
289
290 static int
291 bridge_hotplug_add(struct device *dev, struct device *member)
292 {
293 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
294
295 bridge_create_member(bst, member, true);
296
297 return 0;
298 }
299
300 static int
301 bridge_hotplug_del(struct device *dev, struct device *member)
302 {
303 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
304 struct bridge_member *bm;
305
306 bm = vlist_find(&bst->members, member->ifname, bm, node);
307 if (!bm)
308 return UBUS_STATUS_NOT_FOUND;
309
310 vlist_delete(&bst->members, &bm->node);
311 return 0;
312 }
313
314 static int
315 bridge_hotplug_prepare(struct device *dev)
316 {
317 struct bridge_state *bst;
318
319 bst = container_of(dev, struct bridge_state, dev);
320 bst->force_active = true;
321 device_set_present(&bst->dev, true);
322
323 return 0;
324 }
325
326 static const struct device_hotplug_ops bridge_ops = {
327 .prepare = bridge_hotplug_prepare,
328 .add = bridge_hotplug_add,
329 .del = bridge_hotplug_del
330 };
331
332 static void
333 bridge_free(struct device *dev)
334 {
335 struct bridge_state *bst;
336
337 device_cleanup(dev);
338 bst = container_of(dev, struct bridge_state, dev);
339 vlist_flush_all(&bst->members);
340 free(bst);
341 }
342
343 static void
344 bridge_dump_info(struct device *dev, struct blob_buf *b)
345 {
346 struct bridge_state *bst;
347 struct bridge_member *bm;
348 void *list;
349
350 bst = container_of(dev, struct bridge_state, dev);
351
352 system_if_dump_info(dev, b);
353 list = blobmsg_open_array(b, "bridge-members");
354
355 vlist_for_each_element(&bst->members, bm, node)
356 blobmsg_add_string(b, NULL, bm->dev.dev->ifname);
357
358 blobmsg_close_array(b, list);
359 }
360
361 static void
362 bridge_config_init(struct device *dev)
363 {
364 struct bridge_state *bst;
365 struct blob_attr *cur;
366 int rem;
367
368 bst = container_of(dev, struct bridge_state, dev);
369
370 if (!bst->ifnames)
371 return;
372
373 blobmsg_for_each_attr(cur, bst->ifnames, rem) {
374 bridge_add_member(bst, blobmsg_data(cur));
375 }
376 }
377
378 static void
379 bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
380 {
381 struct bridge_config *cfg = &bst->config;
382 struct blob_attr *cur;
383
384 /* defaults */
385 cfg->stp = true;
386 cfg->forward_delay = 1;
387 cfg->igmp_snoop = true;
388
389 if ((cur = tb[BRIDGE_ATTR_STP]))
390 cfg->stp = blobmsg_get_bool(cur);
391
392 if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
393 cfg->forward_delay = blobmsg_get_u32(cur);
394
395 if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
396 cfg->igmp_snoop = blobmsg_get_bool(cur);
397
398 if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
399 cfg->ageing_time = blobmsg_get_u32(cur);
400 cfg->flags |= BRIDGE_OPT_AGEING_TIME;
401 }
402
403 if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
404 cfg->hello_time = blobmsg_get_u32(cur);
405 cfg->flags |= BRIDGE_OPT_HELLO_TIME;
406 }
407
408 if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
409 cfg->max_age = blobmsg_get_u32(cur);
410 cfg->flags |= BRIDGE_OPT_MAX_AGE;
411 }
412 }
413
414 static struct device *
415 bridge_create(const char *name, struct blob_attr *attr)
416 {
417 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
418 struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
419 struct bridge_state *bst;
420 struct device *dev = NULL;
421
422 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
423 blob_data(attr), blob_len(attr));
424 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
425 blob_data(attr), blob_len(attr));
426
427 bst = calloc(1, sizeof(*bst));
428 if (!bst)
429 return NULL;
430
431 dev = &bst->dev;
432 device_init(dev, &bridge_device_type, name);
433 device_init_settings(dev, tb_dev);
434 dev->config_pending = true;
435 bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
436 bridge_apply_settings(bst, tb_br);
437
438 bst->set_state = dev->set_state;
439 dev->set_state = bridge_set_state;
440
441 dev->hotplug_ops = &bridge_ops;
442
443 vlist_init(&bst->members, avl_strcmp, bridge_member_update);
444 bst->members.keep_old = true;
445
446 return dev;
447 }
448
449