IPv6: Remove local ULA if there is an external one
[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
184 static void __device_broadcast_event(struct list_head *head, enum device_event ev)
185 {
186 struct device_user *dep;
187 static uint8_t idx[__DEV_EVENT_MAX];
188 bool found;
189
190 idx[ev]++;
191 do {
192 found = false;
193
194 list_for_each_entry(dep, head, list) {
195 if (!dep->cb)
196 continue;
197
198 if (dep->ev_idx[ev] == idx[ev])
199 continue;
200
201 dep->cb(dep, ev);
202 dep->ev_idx[ev] = idx[ev];
203 found = true;
204 break;
205 }
206 } while (found);
207 }
208
209 void device_broadcast_event(struct device *dev, enum device_event ev)
210 {
211 __device_broadcast_event(&dev->aliases, ev);
212 __device_broadcast_event(&dev->users, ev);
213 }
214
215 int device_claim(struct device_user *dep)
216 {
217 struct device *dev = dep->dev;
218 int ret;
219
220 if (dep->claimed)
221 return 0;
222
223 dep->claimed = true;
224 D(DEVICE, "Claim %s %s, new refcount: %d\n", dev->type->name, dev->ifname, dev->active + 1);
225 if (++dev->active != 1)
226 return 0;
227
228 device_broadcast_event(dev, DEV_EVENT_SETUP);
229 ret = dev->set_state(dev, true);
230 if (ret == 0)
231 device_broadcast_event(dev, DEV_EVENT_UP);
232 else {
233 D(DEVICE, "claim device %s failed: %d\n", dev->ifname, ret);
234 dev->active = 0;
235 dep->claimed = false;
236 }
237
238 return ret;
239 }
240
241 void device_release(struct device_user *dep)
242 {
243 struct device *dev = dep->dev;
244
245 if (!dep->claimed)
246 return;
247
248 dep->claimed = false;
249 dev->active--;
250 D(DEVICE, "Release %s %s, new refcount: %d\n", dev->type->name, dev->ifname, dev->active);
251 assert(dev->active >= 0);
252
253 if (dev->active)
254 return;
255
256 device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
257 if (!dep->hotplug)
258 dev->set_state(dev, false);
259 device_broadcast_event(dev, DEV_EVENT_DOWN);
260 }
261
262 int device_check_state(struct device *dev)
263 {
264 if (!dev->type->check_state)
265 return 0;
266
267 return dev->type->check_state(dev);
268 }
269
270 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name)
271 {
272 assert(dev);
273 assert(type);
274
275 if (name)
276 strncpy(dev->ifname, name, IFNAMSIZ);
277
278 D(DEVICE, "Initialize device '%s'\n", dev->ifname);
279 INIT_LIST_HEAD(&dev->users);
280 INIT_LIST_HEAD(&dev->aliases);
281 dev->type = type;
282
283 if (!dev->set_state)
284 dev->set_state = set_device_state;
285 }
286
287 int device_init(struct device *dev, const struct device_type *type, const char *ifname)
288 {
289 int ret;
290
291 device_init_virtual(dev, type, ifname);
292
293 dev->avl.key = dev->ifname;
294
295 ret = avl_insert(&devices, &dev->avl);
296 if (ret < 0)
297 return ret;
298
299 system_if_clear_state(dev);
300 device_check_state(dev);
301
302 return 0;
303 }
304
305 static struct device *
306 device_create_default(const char *name, bool external)
307 {
308 struct device *dev;
309
310 if (!external && system_if_force_external(name))
311 return NULL;
312
313 D(DEVICE, "Create simple device '%s'\n", name);
314 dev = calloc(1, sizeof(*dev));
315 dev->external = external;
316 dev->set_state = simple_device_set_state;
317 device_init(dev, &simple_device_type, name);
318 dev->default_config = true;
319 return dev;
320 }
321
322 struct device *
323 device_get(const char *name, int create)
324 {
325 struct device *dev;
326
327 if (strchr(name, '.'))
328 return get_vlan_device_chain(name, create);
329
330 if (name[0] == '@')
331 return device_alias_get(name + 1);
332
333 dev = avl_find_element(&devices, name, dev, avl);
334 if (dev) {
335 if (create > 1 && !dev->external) {
336 dev->external = true;
337 device_set_present(dev, true);
338 }
339 return dev;
340 }
341
342 if (!create)
343 return NULL;
344
345 return device_create_default(name, create > 1);
346 }
347
348 static void
349 device_delete(struct device *dev)
350 {
351 if (!dev->avl.key)
352 return;
353
354 D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
355 avl_delete(&devices, &dev->avl);
356 dev->avl.key = NULL;
357 }
358
359 void device_cleanup(struct device *dev)
360 {
361 struct device_user *dep, *tmp;
362
363 D(DEVICE, "Clean up device '%s'\n", dev->ifname);
364 list_for_each_entry_safe(dep, tmp, &dev->users, list) {
365 if (!dep->cb)
366 continue;
367
368 dep->cb(dep, DEV_EVENT_REMOVE);
369 device_release(dep);
370 }
371
372 device_delete(dev);
373 }
374
375 static void __device_set_present(struct device *dev, bool state)
376 {
377 if (dev->present == state)
378 return;
379
380 dev->present = state;
381 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
382 }
383
384 void
385 device_refresh_present(struct device *dev)
386 {
387 bool state = dev->sys_present;
388
389 if (dev->disabled || dev->deferred)
390 state = false;
391
392 __device_set_present(dev, state);
393 }
394
395 void device_set_present(struct device *dev, bool state)
396 {
397 if (dev->sys_present == state)
398 return;
399
400 D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
401 dev->sys_present = state;
402 device_refresh_present(dev);
403 }
404
405 static int device_refcount(struct device *dev)
406 {
407 struct list_head *list;
408 int count = 0;
409
410 list_for_each(list, &dev->users)
411 count++;
412
413 return count;
414 }
415
416 void device_add_user(struct device_user *dep, struct device *dev)
417 {
418 struct list_head *head;
419
420 if (dep->dev)
421 device_remove_user(dep);
422
423 if (!dev)
424 return;
425
426 dep->dev = dev;
427
428 if (dep->alias)
429 head = &dev->aliases;
430 else
431 head = &dev->users;
432 list_add_tail(&dep->list, head);
433 D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
434
435 if (dep->cb && dev->present) {
436 dep->cb(dep, DEV_EVENT_ADD);
437 if (dev->active)
438 dep->cb(dep, DEV_EVENT_UP);
439 }
440 }
441
442 void
443 device_free(struct device *dev)
444 {
445 __devlock++;
446 free(dev->config);
447 device_cleanup(dev);
448 dev->type->free(dev);
449 __devlock--;
450 }
451
452 static void
453 __device_free_unused(struct device *dev)
454 {
455 if (!list_empty(&dev->users) || dev->current_config || __devlock)
456 return;
457
458 device_free(dev);
459 }
460
461 void device_remove_user(struct device_user *dep)
462 {
463 struct device *dev = dep->dev;
464
465 if (!dep->dev)
466 return;
467
468 dep->hotplug = false;
469 if (dep->claimed)
470 device_release(dep);
471
472 list_del(&dep->list);
473 dep->dev = NULL;
474 D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
475 __device_free_unused(dev);
476 }
477
478 void
479 device_free_unused(struct device *dev)
480 {
481 struct device *tmp;
482
483 if (dev)
484 return __device_free_unused(dev);
485
486 avl_for_each_element_safe(&devices, dev, avl, tmp)
487 __device_free_unused(dev);
488 }
489
490 void
491 device_init_pending(void)
492 {
493 struct device *dev, *tmp;
494
495 avl_for_each_element_safe(&devices, dev, avl, tmp) {
496 if (!dev->config_pending)
497 continue;
498
499 dev->type->config_init(dev);
500 dev->config_pending = false;
501 }
502 }
503
504 static enum dev_change_type
505 device_reload_config(struct device *dev, struct blob_attr *attr)
506 {
507 struct blob_attr *tb[__DEV_ATTR_MAX];
508 const struct config_param_list *cfg = dev->type->config_params;
509
510 if (config_check_equal(dev->config, attr, cfg))
511 return DEV_CONFIG_NO_CHANGE;
512
513 if (cfg == &device_attr_list) {
514 memset(tb, 0, sizeof(tb));
515
516 if (attr)
517 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
518 blob_data(attr), blob_len(attr));
519
520 device_init_settings(dev, tb);
521 return DEV_CONFIG_RESTART;
522 } else
523 return DEV_CONFIG_RECREATE;
524 }
525
526 enum dev_change_type
527 device_set_config(struct device *dev, const struct device_type *type,
528 struct blob_attr *attr)
529 {
530 if (type != dev->type)
531 return DEV_CONFIG_RECREATE;
532
533 if (dev->type->reload)
534 return dev->type->reload(dev, attr);
535
536 return device_reload_config(dev, attr);
537 }
538
539 static void
540 device_replace(struct device *dev, struct device *odev)
541 {
542 struct device_user *dep, *tmp;
543 bool present = odev->present;
544
545 if (present)
546 device_set_present(odev, false);
547
548 list_for_each_entry_safe(dep, tmp, &odev->users, list) {
549 device_release(dep);
550 list_move_tail(&dep->list, &dev->users);
551 dep->dev = dev;
552 }
553 device_free(odev);
554
555 if (present)
556 device_set_present(dev, true);
557 }
558
559 void
560 device_reset_config(void)
561 {
562 struct device *dev;
563
564 avl_for_each_element(&devices, dev, avl)
565 dev->current_config = false;
566 }
567
568 void
569 device_reset_old(void)
570 {
571 struct device *dev, *tmp, *ndev;
572
573 avl_for_each_element_safe(&devices, dev, avl, tmp) {
574 if (dev->current_config || dev->default_config)
575 continue;
576
577 if (dev->type != &simple_device_type)
578 continue;
579
580 ndev = device_create_default(dev->ifname, dev->external);
581 device_replace(ndev, dev);
582 }
583 }
584
585 struct device *
586 device_create(const char *name, const struct device_type *type,
587 struct blob_attr *config)
588 {
589 struct device *odev = NULL, *dev;
590 enum dev_change_type change;
591
592 config = config_memdup(config);
593 if (!config)
594 return NULL;
595
596 odev = device_get(name, false);
597 if (odev) {
598 odev->current_config = true;
599 change = device_set_config(odev, type, config);
600 if (odev->external) {
601 system_if_apply_settings(odev, &odev->settings);
602 change = DEV_CONFIG_APPLIED;
603 }
604 switch (change) {
605 case DEV_CONFIG_RESTART:
606 case DEV_CONFIG_APPLIED:
607 D(DEVICE, "Device '%s': config applied\n", odev->ifname);
608 free(odev->config);
609 odev->config = config;
610 if (change == DEV_CONFIG_RESTART && odev->present) {
611 device_set_present(odev, false);
612 device_set_present(odev, true);
613 }
614 return odev;
615 case DEV_CONFIG_NO_CHANGE:
616 D(DEVICE, "Device '%s': no configuration change\n", odev->ifname);
617 free(config);
618 return odev;
619 case DEV_CONFIG_RECREATE:
620 D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
621 device_delete(odev);
622 break;
623 }
624 } else
625 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
626
627 dev = type->create(name, config);
628 if (!dev)
629 return NULL;
630
631 dev->current_config = true;
632 dev->config = config;
633 if (odev)
634 device_replace(dev, odev);
635
636 if (!config_init && dev->config_pending)
637 type->config_init(dev);
638
639 return dev;
640 }
641
642 void
643 device_dump_status(struct blob_buf *b, struct device *dev)
644 {
645 struct device_settings st;
646 void *c, *s;
647
648 if (!dev) {
649 avl_for_each_element(&devices, dev, avl) {
650 if (!dev->present)
651 continue;
652 c = blobmsg_open_table(b, dev->ifname);
653 device_dump_status(b, dev);
654 blobmsg_close_table(b, c);
655 }
656
657 return;
658 }
659
660 blobmsg_add_u8(b, "external", dev->external);
661 blobmsg_add_u8(b, "present", dev->present);
662 blobmsg_add_string(b, "type", dev->type->name);
663
664 if (!dev->present)
665 return;
666
667 blobmsg_add_u8(b, "up", !!dev->active);
668 if (dev->type->dump_info)
669 dev->type->dump_info(dev, b);
670 else
671 system_if_dump_info(dev, b);
672
673 if (dev->active) {
674 device_merge_settings(dev, &st);
675 if (st.flags & DEV_OPT_MTU)
676 blobmsg_add_u32(b, "mtu", st.mtu);
677 if (st.flags & DEV_OPT_MACADDR)
678 blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
679 if (st.flags & DEV_OPT_TXQUEUELEN)
680 blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
681 }
682
683 s = blobmsg_open_table(b, "statistics");
684 if (dev->type->dump_stats)
685 dev->type->dump_stats(dev, b);
686 else
687 system_if_dump_stats(dev, b);
688 blobmsg_close_table(b, s);
689 }