Add time.h include
[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
69         struct list_head members;
70         int n_present;
71 };
72
73 struct bridge_member {
74         struct list_head list;
75         struct bridge_state *bst;
76         struct device_user dev;
77         bool present;
78 };
79
80 static void bridge_free_member(struct bridge_member *bm);
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_member_cb(struct device_user *dev, enum device_event ev)
123 {
124         struct bridge_member *bm = container_of(dev, struct bridge_member, dev);
125         struct bridge_state *bst = bm->bst;
126
127         switch (ev) {
128         case DEV_EVENT_ADD:
129                 assert(!bm->present);
130
131                 bm->present = true;
132                 bst->n_present++;
133
134                 if (bst->dev.active)
135                         bridge_enable_member(bm);
136                 else if (bst->n_present == 1)
137                         device_set_present(&bst->dev, true);
138
139                 break;
140         case DEV_EVENT_REMOVE:
141                 if (!bm->present)
142                         return;
143
144                 if (bst->dev.active)
145                         bridge_disable_member(bm);
146
147                 bm->present = false;
148                 bm->bst->n_present--;
149                 if (bst->n_present == 0)
150                         device_set_present(&bst->dev, false);
151
152                 if (dev->hotplug)
153                         bridge_free_member(bm);
154                 break;
155         default:
156                 return;
157         }
158 }
159
160 static int
161 bridge_set_down(struct bridge_state *bst)
162 {
163         struct bridge_member *bm;
164
165         bst->set_state(&bst->dev, false);
166
167         list_for_each_entry(bm, &bst->members, list)
168                 bridge_disable_member(bm);
169
170         system_bridge_delbr(&bst->dev);
171
172         return 0;
173 }
174
175 static int
176 bridge_set_up(struct bridge_state *bst)
177 {
178         struct bridge_member *bm;
179         int ret;
180
181         if (!bst->n_present)
182                 return -ENOENT;
183
184         ret = system_bridge_addbr(&bst->dev, &bst->config);
185         if (ret < 0)
186                 goto out;
187
188         list_for_each_entry(bm, &bst->members, list)
189                 bridge_enable_member(bm);
190
191         if (!bst->n_present) {
192                 /* initialization of all member interfaces failed */
193                 system_bridge_delbr(&bst->dev);
194                 device_set_present(&bst->dev, false);
195                 return -ENOENT;
196         }
197
198         ret = bst->set_state(&bst->dev, true);
199         if (ret < 0)
200                 bridge_set_down(bst);
201
202 out:
203         return ret;
204 }
205
206 static int
207 bridge_set_state(struct device *dev, bool up)
208 {
209         struct bridge_state *bst;
210
211         bst = container_of(dev, struct bridge_state, dev);
212
213         if (up)
214                 return bridge_set_up(bst);
215         else
216                 return bridge_set_down(bst);
217 }
218
219 static struct bridge_member *
220 bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
221 {
222         struct bridge_member *bm;
223
224         bm = calloc(1, sizeof(*bm));
225         bm->bst = bst;
226         bm->dev.cb = bridge_member_cb;
227         bm->dev.hotplug = hotplug;
228         list_add_tail(&bm->list, &bst->members);
229
230         device_add_user(&bm->dev, dev);
231
232         return bm;
233 }
234
235 static void
236 bridge_free_member(struct bridge_member *bm)
237 {
238         if (bm->present) {
239                 bridge_member_cb(&bm->dev, DEV_EVENT_REMOVE);
240                 bm->bst->n_present--;
241                 if (bm->bst->dev.active)
242                         bridge_disable_member(bm);
243         }
244
245         list_del(&bm->list);
246         device_remove_user(&bm->dev);
247         free(bm);
248 }
249
250 static void
251 bridge_add_member(struct bridge_state *bst, const char *name)
252 {
253         struct device *dev;
254
255         dev = device_get(name, true);
256         if (!dev)
257                 return;
258
259         bridge_create_member(bst, dev, false);
260 }
261
262 static int
263 bridge_hotplug_add(struct device *dev, struct device *member)
264 {
265         struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
266
267         bridge_create_member(bst, member, true);
268
269         return 0;
270 }
271
272 static int
273 bridge_hotplug_del(struct device *dev, struct device *member)
274 {
275         struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
276         struct bridge_member *bm;
277
278         list_for_each_entry(bm, &bst->members, list) {
279                 if (bm->dev.dev != member)
280                         continue;
281
282                 bridge_free_member(bm);
283                 return 0;
284         }
285
286         return -ENOENT;
287 }
288
289 static const struct device_hotplug_ops bridge_ops = {
290         .add = bridge_hotplug_add,
291         .del = bridge_hotplug_del
292 };
293
294 static void
295 bridge_free(struct device *dev)
296 {
297         struct bridge_state *bst;
298         struct bridge_member *bm;
299
300         device_cleanup(dev);
301         bst = container_of(dev, struct bridge_state, dev);
302         while (!list_empty(&bst->members)) {
303                 bm = list_first_entry(&bst->members, struct bridge_member, list);
304                 bridge_free_member(bm);
305         }
306         free(bst);
307 }
308
309 static void
310 bridge_dump_info(struct device *dev, struct blob_buf *b)
311 {
312         struct bridge_state *bst;
313         struct bridge_member *bm;
314         void *list;
315
316         bst = container_of(dev, struct bridge_state, dev);
317
318         list = blobmsg_open_array(b, "bridge-members");
319         list_for_each_entry(bm, &bst->members, list) {
320                 blobmsg_add_string(b, NULL, bm->dev.dev->ifname);
321         }
322         blobmsg_close_array(b, list);
323 }
324
325 static void
326 bridge_config_init(struct device *dev)
327 {
328         struct bridge_state *bst;
329         struct blob_attr *cur;
330         int rem;
331
332         bst = container_of(dev, struct bridge_state, dev);
333
334         if (!bst->ifnames)
335                 return;
336
337         blobmsg_for_each_attr(cur, bst->ifnames, rem) {
338                 bridge_add_member(bst, blobmsg_data(cur));
339         }
340 }
341
342 static void
343 bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
344 {
345         struct bridge_config *cfg = &bst->config;
346         struct blob_attr *cur;
347
348         /* defaults */
349         cfg->stp = true;
350         cfg->forward_delay = 1;
351         cfg->igmp_snoop = true;
352
353         if ((cur = tb[BRIDGE_ATTR_STP]))
354                 cfg->stp = blobmsg_get_bool(cur);
355
356         if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
357                 cfg->forward_delay = blobmsg_get_u32(cur);
358
359         if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
360                 cfg->igmp_snoop = blobmsg_get_bool(cur);
361
362         if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
363                 cfg->ageing_time = blobmsg_get_u32(cur);
364                 cfg->flags |= BRIDGE_OPT_AGEING_TIME;
365         }
366
367         if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
368                 cfg->hello_time = blobmsg_get_u32(cur);
369                 cfg->flags |= BRIDGE_OPT_HELLO_TIME;
370         }
371
372         if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
373                 cfg->max_age = blobmsg_get_u32(cur);
374                 cfg->flags |= BRIDGE_OPT_MAX_AGE;
375         }
376 }
377
378 static struct device *
379 bridge_create(const char *name, struct blob_attr *attr)
380 {
381         struct blob_attr *tb_dev[__DEV_ATTR_MAX];
382         struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
383         struct bridge_state *bst;
384         struct device *dev = NULL;
385
386         blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
387                 blob_data(attr), blob_len(attr));
388         blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
389                 blob_data(attr), blob_len(attr));
390
391         bst = calloc(1, sizeof(*bst));
392         if (!bst)
393                 return NULL;
394
395         dev = &bst->dev;
396         device_init(dev, &bridge_device_type, name);
397         device_init_settings(dev, tb_dev);
398         dev->config_pending = true;
399         bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
400         bridge_apply_settings(bst, tb_br);
401
402         bst->set_state = dev->set_state;
403         dev->set_state = bridge_set_state;
404
405         dev->hotplug_ops = &bridge_ops;
406
407         INIT_LIST_HEAD(&bst->members);
408
409         return dev;
410 }
411
412