IPv6: Set loopback-policy for output traffic
[project/netifd.git] / device.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
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <net/ethernet.h>
22
23 #ifdef linux
24 #include <netinet/ether.h>
25 #endif
26
27 #include "netifd.h"
28 #include "system.h"
29 #include "config.h"
30
31 static struct avl_tree devices;
32
33 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
34 [DEV_ATTR_TYPE] = { "type", BLOBMSG_TYPE_STRING },
35 [DEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
36 [DEV_ATTR_MTU] = { "mtu", BLOBMSG_TYPE_INT32 },
37 [DEV_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING },
38 [DEV_ATTR_TXQUEUELEN] = { "txqueuelen", BLOBMSG_TYPE_INT32 },
39 [DEV_ATTR_ENABLED] = { "enabled", BLOBMSG_TYPE_BOOL },
40 };
41
42 const struct config_param_list device_attr_list = {
43 .n_params = __DEV_ATTR_MAX,
44 .params = dev_attrs,
45 };
46
47 static int __devlock = 0;
48
49 void device_lock(void)
50 {
51 __devlock++;
52 }
53
54 void device_unlock(void)
55 {
56 __devlock--;
57 if (!__devlock)
58 device_free_unused(NULL);
59 }
60
61 static int set_device_state(struct device *dev, bool state)
62 {
63 if (state)
64 system_if_up(dev);
65 else
66 system_if_down(dev);
67
68 return 0;
69 }
70
71 static int
72 simple_device_set_state(struct device *dev, bool state)
73 {
74 struct device *pdev;
75 int ret = 0;
76
77 pdev = dev->parent.dev;
78 if (state && !pdev) {
79 pdev = system_if_get_parent(dev);
80 if (pdev)
81 device_add_user(&dev->parent, pdev);
82 }
83
84 if (pdev) {
85 if (state)
86 ret = device_claim(&dev->parent);
87 else
88 device_release(&dev->parent);
89
90 if (ret < 0)
91 return ret;
92 }
93 return set_device_state(dev, state);
94 }
95
96 static struct device *
97 simple_device_create(const char *name, struct blob_attr *attr)
98 {
99 struct blob_attr *tb[__DEV_ATTR_MAX];
100 struct device *dev = NULL;
101
102 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
103 dev = device_get(name, true);
104 if (!dev)
105 return NULL;
106
107 dev->set_state = simple_device_set_state;
108 device_init_settings(dev, tb);
109
110 return dev;
111 }
112
113 static void simple_device_free(struct device *dev)
114 {
115 if (dev->parent.dev)
116 device_remove_user(&dev->parent);
117 free(dev);
118 }
119
120 const struct device_type simple_device_type = {
121 .name = "Network device",
122 .config_params = &device_attr_list,
123
124 .create = simple_device_create,
125 .check_state = system_if_check,
126 .free = simple_device_free,
127 };
128
129 static void
130 device_merge_settings(struct device *dev, struct device_settings *n)
131 {
132 struct device_settings *os = &dev->orig_settings;
133 struct device_settings *s = &dev->settings;
134
135 memset(n, 0, sizeof(*n));
136 n->mtu = s->flags & DEV_OPT_MTU ? s->mtu : os->mtu;
137 n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ?
138 s->txqueuelen : os->txqueuelen;
139 memcpy(n->macaddr,
140 (s->flags & DEV_OPT_MACADDR ? s->macaddr : os->macaddr),
141 sizeof(n->macaddr));
142 n->flags = s->flags | os->flags;
143 }
144
145 void
146 device_init_settings(struct device *dev, struct blob_attr **tb)
147 {
148 struct device_settings *s = &dev->settings;
149 struct blob_attr *cur;
150 struct ether_addr *ea;
151 bool disabled = false;
152
153 s->flags = 0;
154 if ((cur = tb[DEV_ATTR_ENABLED]))
155 disabled = !blobmsg_get_bool(cur);
156
157 if ((cur = tb[DEV_ATTR_MTU])) {
158 s->mtu = blobmsg_get_u32(cur);
159 s->flags |= DEV_OPT_MTU;
160 }
161
162 if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
163 s->txqueuelen = blobmsg_get_u32(cur);
164 s->flags |= DEV_OPT_TXQUEUELEN;
165 }
166
167 if ((cur = tb[DEV_ATTR_MACADDR])) {
168 ea = ether_aton(blobmsg_data(cur));
169 if (ea) {
170 memcpy(s->macaddr, ea, 6);
171 s->flags |= DEV_OPT_MACADDR;
172 }
173 }
174
175 device_set_disabled(dev, disabled);
176 }
177
178 static void __init dev_init(void)
179 {
180 avl_init(&devices, avl_strcmp, true, NULL);
181 }
182
183 static int device_broadcast_cb(void *ctx, struct safe_list *list)
184 {
185 struct device_user *dep = container_of(list, struct device_user, list);
186 int *ev = ctx;
187
188 if (dep->cb)
189 dep->cb(dep, *ev);
190 return 0;
191 }
192
193 void device_broadcast_event(struct device *dev, enum device_event ev)
194 {
195 int dev_ev = ev;
196
197 safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev);
198 safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev);
199 }
200
201 int device_claim(struct device_user *dep)
202 {
203 struct device *dev = dep->dev;
204 int ret;
205
206 if (dep->claimed)
207 return 0;
208
209 dep->claimed = true;
210 D(DEVICE, "Claim %s %s, new refcount: %d\n", dev->type->name, dev->ifname, dev->active + 1);
211 if (++dev->active != 1)
212 return 0;
213
214 device_broadcast_event(dev, DEV_EVENT_SETUP);
215 ret = dev->set_state(dev, true);
216 if (ret == 0)
217 device_broadcast_event(dev, DEV_EVENT_UP);
218 else {
219 D(DEVICE, "claim device %s failed: %d\n", dev->ifname, ret);
220 dev->active = 0;
221 dep->claimed = false;
222 }
223
224 return ret;
225 }
226
227 void device_release(struct device_user *dep)
228 {
229 struct device *dev = dep->dev;
230
231 if (!dep->claimed)
232 return;
233
234 dep->claimed = false;
235 dev->active--;
236 D(DEVICE, "Release %s %s, new refcount: %d\n", dev->type->name, dev->ifname, dev->active);
237 assert(dev->active >= 0);
238
239 if (dev->active)
240 return;
241
242 device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
243 if (!dep->hotplug)
244 dev->set_state(dev, false);
245 device_broadcast_event(dev, DEV_EVENT_DOWN);
246 }
247
248 int device_check_state(struct device *dev)
249 {
250 if (!dev->type->check_state)
251 return 0;
252
253 return dev->type->check_state(dev);
254 }
255
256 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name)
257 {
258 assert(dev);
259 assert(type);
260
261 if (name)
262 strncpy(dev->ifname, name, IFNAMSIZ);
263
264 D(DEVICE, "Initialize device '%s'\n", dev->ifname);
265 INIT_SAFE_LIST(&dev->users);
266 INIT_SAFE_LIST(&dev->aliases);
267 dev->type = type;
268
269 if (!dev->set_state)
270 dev->set_state = set_device_state;
271 }
272
273 int device_init(struct device *dev, const struct device_type *type, const char *ifname)
274 {
275 int ret;
276
277 device_init_virtual(dev, type, ifname);
278
279 dev->avl.key = dev->ifname;
280
281 ret = avl_insert(&devices, &dev->avl);
282 if (ret < 0)
283 return ret;
284
285 system_if_clear_state(dev);
286 device_check_state(dev);
287
288 return 0;
289 }
290
291 static struct device *
292 device_create_default(const char *name, bool external)
293 {
294 struct device *dev;
295
296 if (!external && system_if_force_external(name))
297 return NULL;
298
299 D(DEVICE, "Create simple device '%s'\n", name);
300 dev = calloc(1, sizeof(*dev));
301 dev->external = external;
302 dev->set_state = simple_device_set_state;
303 device_init(dev, &simple_device_type, name);
304 dev->default_config = true;
305 return dev;
306 }
307
308 struct device *
309 device_get(const char *name, int create)
310 {
311 struct device *dev;
312
313 if (strchr(name, '.'))
314 return get_vlan_device_chain(name, create);
315
316 if (name[0] == '@')
317 return device_alias_get(name + 1);
318
319 dev = avl_find_element(&devices, name, dev, avl);
320 if (dev) {
321 if (create > 1 && !dev->external) {
322 dev->external = true;
323 device_set_present(dev, true);
324 }
325 return dev;
326 }
327
328 if (!create)
329 return NULL;
330
331 return device_create_default(name, create > 1);
332 }
333
334 static void
335 device_delete(struct device *dev)
336 {
337 if (!dev->avl.key)
338 return;
339
340 D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
341 avl_delete(&devices, &dev->avl);
342 dev->avl.key = NULL;
343 }
344
345 static int device_cleanup_cb(void *ctx, struct safe_list *list)
346 {
347 struct device_user *dep = container_of(list, struct device_user, list);
348 if (dep->cb)
349 dep->cb(dep, DEV_EVENT_REMOVE);
350
351 device_release(dep);
352 return 0;
353 }
354
355 void device_cleanup(struct device *dev)
356 {
357 D(DEVICE, "Clean up device '%s'\n", dev->ifname);
358 safe_list_for_each(&dev->users, device_cleanup_cb, NULL);
359 safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL);
360 device_delete(dev);
361 }
362
363 static void __device_set_present(struct device *dev, bool state)
364 {
365 if (dev->present == state)
366 return;
367
368 dev->present = state;
369 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
370 }
371
372 void
373 device_refresh_present(struct device *dev)
374 {
375 bool state = dev->sys_present;
376
377 if (dev->disabled || dev->deferred)
378 state = false;
379
380 __device_set_present(dev, state);
381 }
382
383 void device_set_present(struct device *dev, bool state)
384 {
385 if (dev->sys_present == state)
386 return;
387
388 D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
389 dev->sys_present = state;
390 device_refresh_present(dev);
391 }
392
393 static int device_refcount(struct device *dev)
394 {
395 struct list_head *list;
396 int count = 0;
397
398 list_for_each(list, &dev->users.list)
399 count++;
400
401 list_for_each(list, &dev->aliases.list)
402 count++;
403
404 return count;
405 }
406
407 void device_add_user(struct device_user *dep, struct device *dev)
408 {
409 struct safe_list *head;
410
411 if (dep->dev)
412 device_remove_user(dep);
413
414 if (!dev)
415 return;
416
417 dep->dev = dev;
418
419 if (dep->alias)
420 head = &dev->aliases;
421 else
422 head = &dev->users;
423
424 safe_list_add(&dep->list, head);
425 D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
426
427 if (dep->cb && dev->present) {
428 dep->cb(dep, DEV_EVENT_ADD);
429 if (dev->active)
430 dep->cb(dep, DEV_EVENT_UP);
431 }
432 }
433
434 void
435 device_free(struct device *dev)
436 {
437 __devlock++;
438 free(dev->config);
439 device_cleanup(dev);
440 dev->type->free(dev);
441 __devlock--;
442 }
443
444 static void
445 __device_free_unused(struct device *dev)
446 {
447 if (!safe_list_empty(&dev->users) ||
448 !safe_list_empty(&dev->aliases) ||
449 dev->current_config || __devlock)
450 return;
451
452 device_free(dev);
453 }
454
455 void device_remove_user(struct device_user *dep)
456 {
457 struct device *dev = dep->dev;
458
459 if (!dep->dev)
460 return;
461
462 dep->hotplug = false;
463 if (dep->claimed)
464 device_release(dep);
465
466 safe_list_del(&dep->list);
467 dep->dev = NULL;
468 D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
469 __device_free_unused(dev);
470 }
471
472 void
473 device_free_unused(struct device *dev)
474 {
475 struct device *tmp;
476
477 if (dev)
478 return __device_free_unused(dev);
479
480 avl_for_each_element_safe(&devices, dev, avl, tmp)
481 __device_free_unused(dev);
482 }
483
484 void
485 device_init_pending(void)
486 {
487 struct device *dev, *tmp;
488
489 avl_for_each_element_safe(&devices, dev, avl, tmp) {
490 if (!dev->config_pending)
491 continue;
492
493 dev->type->config_init(dev);
494 dev->config_pending = false;
495 }
496 }
497
498 static enum dev_change_type
499 device_reload_config(struct device *dev, struct blob_attr *attr)
500 {
501 struct blob_attr *tb[__DEV_ATTR_MAX];
502 const struct config_param_list *cfg = dev->type->config_params;
503
504 if (config_check_equal(dev->config, attr, cfg))
505 return DEV_CONFIG_NO_CHANGE;
506
507 if (cfg == &device_attr_list) {
508 memset(tb, 0, sizeof(tb));
509
510 if (attr)
511 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
512 blob_data(attr), blob_len(attr));
513
514 device_init_settings(dev, tb);
515 return DEV_CONFIG_RESTART;
516 } else
517 return DEV_CONFIG_RECREATE;
518 }
519
520 enum dev_change_type
521 device_set_config(struct device *dev, const struct device_type *type,
522 struct blob_attr *attr)
523 {
524 if (type != dev->type)
525 return DEV_CONFIG_RECREATE;
526
527 if (dev->type->reload)
528 return dev->type->reload(dev, attr);
529
530 return device_reload_config(dev, attr);
531 }
532
533 static void
534 device_replace(struct device *dev, struct device *odev)
535 {
536 struct device_user *dep, *tmp;
537 bool present = odev->present;
538
539 if (present)
540 device_set_present(odev, false);
541
542 list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) {
543 device_release(dep);
544 safe_list_del(&dep->list);
545 safe_list_add(&dep->list, &dev->users);
546 dep->dev = dev;
547 }
548 device_free(odev);
549
550 if (present)
551 device_set_present(dev, true);
552 }
553
554 void
555 device_reset_config(void)
556 {
557 struct device *dev;
558
559 avl_for_each_element(&devices, dev, avl)
560 dev->current_config = false;
561 }
562
563 void
564 device_reset_old(void)
565 {
566 struct device *dev, *tmp, *ndev;
567
568 avl_for_each_element_safe(&devices, dev, avl, tmp) {
569 if (dev->current_config || dev->default_config)
570 continue;
571
572 if (dev->type != &simple_device_type)
573 continue;
574
575 ndev = device_create_default(dev->ifname, dev->external);
576 device_replace(ndev, dev);
577 }
578 }
579
580 struct device *
581 device_create(const char *name, const struct device_type *type,
582 struct blob_attr *config)
583 {
584 struct device *odev = NULL, *dev;
585 enum dev_change_type change;
586
587 config = config_memdup(config);
588 if (!config)
589 return NULL;
590
591 odev = device_get(name, false);
592 if (odev) {
593 odev->current_config = true;
594 change = device_set_config(odev, type, config);
595 if (odev->external) {
596 system_if_apply_settings(odev, &odev->settings);
597 change = DEV_CONFIG_APPLIED;
598 }
599 switch (change) {
600 case DEV_CONFIG_RESTART:
601 case DEV_CONFIG_APPLIED:
602 D(DEVICE, "Device '%s': config applied\n", odev->ifname);
603 free(odev->config);
604 odev->config = config;
605 if (change == DEV_CONFIG_RESTART && odev->present) {
606 device_set_present(odev, false);
607 device_set_present(odev, true);
608 }
609 return odev;
610 case DEV_CONFIG_NO_CHANGE:
611 D(DEVICE, "Device '%s': no configuration change\n", odev->ifname);
612 free(config);
613 return odev;
614 case DEV_CONFIG_RECREATE:
615 D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
616 device_delete(odev);
617 break;
618 }
619 } else
620 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
621
622 dev = type->create(name, config);
623 if (!dev)
624 return NULL;
625
626 dev->current_config = true;
627 dev->config = config;
628 if (odev)
629 device_replace(dev, odev);
630
631 if (!config_init && dev->config_pending)
632 type->config_init(dev);
633
634 return dev;
635 }
636
637 void
638 device_dump_status(struct blob_buf *b, struct device *dev)
639 {
640 struct device_settings st;
641 void *c, *s;
642
643 if (!dev) {
644 avl_for_each_element(&devices, dev, avl) {
645 if (!dev->present)
646 continue;
647 c = blobmsg_open_table(b, dev->ifname);
648 device_dump_status(b, dev);
649 blobmsg_close_table(b, c);
650 }
651
652 return;
653 }
654
655 blobmsg_add_u8(b, "external", dev->external);
656 blobmsg_add_u8(b, "present", dev->present);
657 blobmsg_add_string(b, "type", dev->type->name);
658
659 if (!dev->present)
660 return;
661
662 blobmsg_add_u8(b, "up", !!dev->active);
663 if (dev->type->dump_info)
664 dev->type->dump_info(dev, b);
665 else
666 system_if_dump_info(dev, b);
667
668 if (dev->active) {
669 device_merge_settings(dev, &st);
670 if (st.flags & DEV_OPT_MTU)
671 blobmsg_add_u32(b, "mtu", st.mtu);
672 if (st.flags & DEV_OPT_MACADDR)
673 blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
674 if (st.flags & DEV_OPT_TXQUEUELEN)
675 blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
676 }
677
678 s = blobmsg_open_table(b, "statistics");
679 if (dev->type->dump_stats)
680 dev->type->dump_stats(dev, b);
681 else
682 system_if_dump_stats(dev, b);
683 blobmsg_close_table(b, s);
684 }