netifd: bridge: set default value for igmp_snoop
[project/netifd.git] / vlandev.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2014 Gioacchino Mazzurco <gio@eigenlab.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
15 #include <string.h>
16 #include <inttypes.h>
17
18 #include "netifd.h"
19 #include "device.h"
20 #include "interface.h"
21 #include "system.h"
22 #include "utils.h"
23
24 enum {
25 VLANDEV_ATTR_IFNAME,
26 VLANDEV_ATTR_VID,
27 VLANDEV_ATTR_INGRESS_QOS_MAPPING,
28 VLANDEV_ATTR_EGRESS_QOS_MAPPING,
29 __VLANDEV_ATTR_MAX
30 };
31
32 static const struct blobmsg_policy vlandev_attrs[__VLANDEV_ATTR_MAX] = {
33 [VLANDEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_STRING },
34 [VLANDEV_ATTR_VID] = { "vid", BLOBMSG_TYPE_STRING },
35 [VLANDEV_ATTR_INGRESS_QOS_MAPPING] = { "ingress_qos_mapping", BLOBMSG_TYPE_ARRAY },
36 [VLANDEV_ATTR_EGRESS_QOS_MAPPING] = { "egress_qos_mapping", BLOBMSG_TYPE_ARRAY },
37 };
38
39 static const struct uci_blob_param_list vlandev_attr_list = {
40 .n_params = __VLANDEV_ATTR_MAX,
41 .params = vlandev_attrs,
42
43 .n_next = 1,
44 .next = { &device_attr_list },
45 };
46
47 static struct device_type vlan8021q_device_type;
48 static struct blob_buf b;
49
50 struct vlandev_device {
51 struct device dev;
52 struct device_user parent;
53
54 device_state_cb set_state;
55
56 struct blob_attr *config_data;
57 struct blob_attr *ifname;
58 struct blob_attr *vid;
59
60 struct vlandev_config config;
61 };
62
63 static int
64 vlandev_hotplug_add(struct device *dev, struct device *member, struct blob_attr *vlan)
65 {
66 struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, dev);
67 void *a;
68
69 dev = mvdev->parent.dev;
70 if (!dev || !dev->hotplug_ops)
71 return UBUS_STATUS_NOT_SUPPORTED;
72
73 blob_buf_init(&b, 0);
74 a = blobmsg_open_array(&b, "vlans");
75 blobmsg_printf(&b, NULL, "%d", mvdev->config.vid);
76 blobmsg_close_array(&b, a);
77
78 return dev->hotplug_ops->add(dev, member, blobmsg_data(b.head));
79 }
80
81 static int
82 vlandev_hotplug_del(struct device *dev, struct device *member)
83 {
84 struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, dev);
85
86 dev = mvdev->parent.dev;
87 if (!dev || !dev->hotplug_ops)
88 return UBUS_STATUS_NOT_SUPPORTED;
89
90 return dev->hotplug_ops->del(dev, member);
91 }
92
93 static int
94 vlandev_hotplug_prepare(struct device *dev, struct device **bridge_dev)
95 {
96 struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, dev);
97
98 dev = mvdev->parent.dev;
99 if (!dev || !dev->hotplug_ops)
100 return UBUS_STATUS_NOT_SUPPORTED;
101
102 return dev->hotplug_ops->prepare(dev, bridge_dev);
103 }
104
105 static void vlandev_hotplug_check(struct vlandev_device *mvdev)
106 {
107 static const struct device_hotplug_ops hotplug_ops = {
108 .prepare = vlandev_hotplug_prepare,
109 .add = vlandev_hotplug_add,
110 .del = vlandev_hotplug_del
111 };
112 struct device *dev = mvdev->parent.dev;
113
114 if (!dev || !dev->hotplug_ops || avl_is_empty(&dev->vlans.avl) ||
115 mvdev->dev.type != &vlan8021q_device_type) {
116 mvdev->dev.hotplug_ops = NULL;
117 return;
118 }
119
120 mvdev->dev.hotplug_ops = &hotplug_ops;
121 }
122
123
124 static void
125 vlandev_base_cb(struct device_user *dev, enum device_event ev)
126 {
127 struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, parent);
128
129 switch (ev) {
130 case DEV_EVENT_ADD:
131 device_set_present(&mvdev->dev, true);
132 break;
133 case DEV_EVENT_REMOVE:
134 device_set_present(&mvdev->dev, false);
135 break;
136 case DEV_EVENT_UPDATE_IFNAME:
137 vlandev_hotplug_check(mvdev);
138 break;
139 default:
140 return;
141 }
142 }
143
144 static int
145 vlandev_set_down(struct vlandev_device *mvdev)
146 {
147 mvdev->set_state(&mvdev->dev, false);
148 system_vlandev_del(&mvdev->dev);
149 device_release(&mvdev->parent);
150
151 return 0;
152 }
153
154 static int
155 vlandev_set_up(struct vlandev_device *mvdev)
156 {
157 int ret;
158
159 ret = device_claim(&mvdev->parent);
160 if (ret < 0)
161 return ret;
162
163 ret = system_vlandev_add(&mvdev->dev, mvdev->parent.dev, &mvdev->config);
164 if (ret < 0)
165 goto release;
166
167 ret = mvdev->set_state(&mvdev->dev, true);
168 if (ret)
169 goto delete;
170
171 return 0;
172
173 delete:
174 system_vlandev_del(&mvdev->dev);
175 release:
176 device_release(&mvdev->parent);
177 return ret;
178 }
179
180 static int
181 vlandev_set_state(struct device *dev, bool up)
182 {
183 struct vlandev_device *mvdev;
184
185 D(SYSTEM, "vlandev_set_state(%s, %u)\n", dev->ifname, up);
186
187 mvdev = container_of(dev, struct vlandev_device, dev);
188 if (up)
189 return vlandev_set_up(mvdev);
190 else
191 return vlandev_set_down(mvdev);
192 }
193
194 static void
195 vlandev_free(struct device *dev)
196 {
197 struct vlandev_device *mvdev;
198
199 mvdev = container_of(dev, struct vlandev_device, dev);
200 device_remove_user(&mvdev->parent);
201 free(mvdev->config_data);
202 vlist_simple_flush_all(&mvdev->config.ingress_qos_mapping_list);
203 vlist_simple_flush_all(&mvdev->config.egress_qos_mapping_list);
204 free(mvdev);
205 }
206
207 static void vlandev_qos_mapping_dump(struct blob_buf *b, const char *name, const struct vlist_simple_tree *qos_mapping_li)
208 {
209 const struct vlan_qos_mapping *elem;
210 void *a, *t;
211
212 a = blobmsg_open_array(b, name);
213
214 vlist_simple_for_each_element(qos_mapping_li, elem, node) {
215 t = blobmsg_open_table(b, NULL);
216
217 blobmsg_add_u32(b, "from", elem->from);
218 blobmsg_add_u32(b, "to", elem->to);
219
220 blobmsg_close_table(b, t);
221 }
222
223 blobmsg_close_array(b, a);
224 }
225
226 static void
227 vlandev_dump_info(struct device *dev, struct blob_buf *b)
228 {
229 struct vlandev_device *mvdev;
230
231 mvdev = container_of(dev, struct vlandev_device, dev);
232 blobmsg_add_string(b, "parent", mvdev->parent.dev->ifname);
233 system_if_dump_info(dev, b);
234 blobmsg_add_u32(b, "vid", mvdev->config.vid);
235 vlandev_qos_mapping_dump(b, "ingress_qos_mapping", &mvdev->config.ingress_qos_mapping_list);
236 vlandev_qos_mapping_dump(b, "egress_qos_mapping", &mvdev->config.egress_qos_mapping_list);
237 }
238
239 static uint16_t
240 vlandev_get_vid(struct device *dev, const char *id_str)
241 {
242 unsigned long id;
243 uint16_t *alias_id;
244 char *err;
245
246 id = strtoul(id_str, &err, 10);
247 if (err && *err) {
248 if (!dev)
249 return 1;
250
251 alias_id = kvlist_get(&dev->vlan_aliases, id_str);
252 if (!alias_id)
253 return 1;
254
255 id = *alias_id;
256 }
257
258 return (uint16_t)id;
259 }
260
261 static void
262 vlandev_config_init(struct device *dev)
263 {
264 struct vlandev_device *mvdev;
265 struct device *basedev = NULL;
266
267 mvdev = container_of(dev, struct vlandev_device, dev);
268 if (mvdev->ifname)
269 basedev = device_get(blobmsg_data(mvdev->ifname), true);
270
271 if (mvdev->vid)
272 mvdev->config.vid = vlandev_get_vid(basedev, blobmsg_get_string(mvdev->vid));
273 else
274 mvdev->config.vid = 1;
275
276 device_add_user(&mvdev->parent, basedev);
277 vlandev_hotplug_check(mvdev);
278 }
279
280 static void vlandev_qos_mapping_list_apply(struct vlist_simple_tree *qos_mapping_li, struct blob_attr *list)
281 {
282 struct blob_attr *cur;
283 struct vlan_qos_mapping *qos_mapping;
284 int rem, rc;
285
286 blobmsg_for_each_attr(cur, list, rem) {
287 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
288 continue;
289
290 if (!blobmsg_check_attr(cur, false))
291 continue;
292
293 qos_mapping = calloc(1, sizeof(*qos_mapping));
294 if (!qos_mapping)
295 continue;
296
297 rc = sscanf(blobmsg_data(cur), "%" PRIu32 ":%" PRIu32, &qos_mapping->from, &qos_mapping->to);
298 if (rc != 2) {
299 free(qos_mapping);
300 continue;
301 }
302 vlist_simple_add(qos_mapping_li, &qos_mapping->node);
303 }
304 }
305
306 static void
307 vlandev_apply_settings(struct vlandev_device *mvdev, struct blob_attr **tb)
308 {
309 struct vlandev_config *cfg = &mvdev->config;
310 struct blob_attr *cur;
311
312 cfg->proto = (mvdev->dev.type == &vlan8021q_device_type) ?
313 VLAN_PROTO_8021Q : VLAN_PROTO_8021AD;
314
315 vlist_simple_update(&cfg->ingress_qos_mapping_list);
316 vlist_simple_update(&cfg->egress_qos_mapping_list);
317
318 if ((cur = tb[VLANDEV_ATTR_INGRESS_QOS_MAPPING]))
319 vlandev_qos_mapping_list_apply(&cfg->ingress_qos_mapping_list, cur);
320
321 if ((cur = tb[VLANDEV_ATTR_EGRESS_QOS_MAPPING]))
322 vlandev_qos_mapping_list_apply(&cfg->egress_qos_mapping_list, cur);
323
324 vlist_simple_flush(&cfg->ingress_qos_mapping_list);
325 vlist_simple_flush(&cfg->egress_qos_mapping_list);
326 }
327
328 static enum dev_change_type
329 vlandev_reload(struct device *dev, struct blob_attr *attr)
330 {
331 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
332 struct blob_attr *tb_mv[__VLANDEV_ATTR_MAX];
333 enum dev_change_type ret = DEV_CONFIG_APPLIED;
334 struct vlandev_device *mvdev;
335
336 mvdev = container_of(dev, struct vlandev_device, dev);
337 attr = blob_memdup(attr);
338
339 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
340 blob_data(attr), blob_len(attr));
341 blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, tb_mv,
342 blob_data(attr), blob_len(attr));
343
344 device_init_settings(dev, tb_dev);
345 vlandev_apply_settings(mvdev, tb_mv);
346 mvdev->ifname = tb_mv[VLANDEV_ATTR_IFNAME];
347 mvdev->vid = tb_mv[VLANDEV_ATTR_VID];
348
349 if (mvdev->config_data) {
350 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
351 struct blob_attr *otb_mv[__VLANDEV_ATTR_MAX];
352
353 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
354 blob_data(mvdev->config_data), blob_len(mvdev->config_data));
355
356 if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL))
357 ret = DEV_CONFIG_RESTART;
358
359 blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, otb_mv,
360 blob_data(mvdev->config_data), blob_len(mvdev->config_data));
361
362 if (uci_blob_diff(tb_mv, otb_mv, &vlandev_attr_list, NULL))
363 ret = DEV_CONFIG_RESTART;
364
365 vlandev_config_init(dev);
366 }
367
368 free(mvdev->config_data);
369 mvdev->config_data = attr;
370 return ret;
371 }
372
373 static struct device *
374 vlandev_create(const char *name, struct device_type *devtype,
375 struct blob_attr *attr)
376 {
377 struct vlandev_device *mvdev;
378 struct device *dev = NULL;
379
380 mvdev = calloc(1, sizeof(*mvdev));
381 if (!mvdev)
382 return NULL;
383
384 vlist_simple_init(&mvdev->config.ingress_qos_mapping_list,
385 struct vlan_qos_mapping, node);
386 vlist_simple_init(&mvdev->config.egress_qos_mapping_list,
387 struct vlan_qos_mapping, node);
388
389 dev = &mvdev->dev;
390
391 if (device_init(dev, devtype, name) < 0) {
392 device_cleanup(dev);
393 free(mvdev);
394 return NULL;
395 }
396
397 dev->config_pending = true;
398
399 mvdev->set_state = dev->set_state;
400 dev->set_state = vlandev_set_state;
401
402 dev->hotplug_ops = NULL;
403 mvdev->parent.cb = vlandev_base_cb;
404
405 vlandev_reload(dev, attr);
406
407 return dev;
408 }
409
410 static struct device_type vlan8021ad_device_type = {
411 .name = "8021ad",
412 .config_params = &vlandev_attr_list,
413 .create = vlandev_create,
414 .config_init = vlandev_config_init,
415 .reload = vlandev_reload,
416 .free = vlandev_free,
417 .dump_info = vlandev_dump_info,
418 };
419
420 static struct device_type vlan8021q_device_type = {
421 .name = "8021q",
422 .config_params = &vlandev_attr_list,
423 .create = vlandev_create,
424 .config_init = vlandev_config_init,
425 .reload = vlandev_reload,
426 .free = vlandev_free,
427 .dump_info = vlandev_dump_info,
428 };
429
430 static void __init vlandev_device_type_init(void)
431 {
432 device_type_add(&vlan8021ad_device_type);
433 device_type_add(&vlan8021q_device_type);
434 }