device: Don't process link events anymore in device user specific callback handlers
[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 static bool default_ps = true;
33
34 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
35 [DEV_ATTR_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
36 [DEV_ATTR_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
37 [DEV_ATTR_MTU6] = { .name = "mtu6", .type = BLOBMSG_TYPE_INT32 },
38 [DEV_ATTR_MACADDR] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING },
39 [DEV_ATTR_TXQUEUELEN] = { .name = "txqueuelen", .type = BLOBMSG_TYPE_INT32 },
40 [DEV_ATTR_ENABLED] = { .name = "enabled", .type = BLOBMSG_TYPE_BOOL },
41 [DEV_ATTR_IPV6] = { .name = "ipv6", .type = BLOBMSG_TYPE_BOOL },
42 [DEV_ATTR_PROMISC] = { .name = "promisc", .type = BLOBMSG_TYPE_BOOL },
43 [DEV_ATTR_RPFILTER] = { .name = "rpfilter", .type = BLOBMSG_TYPE_STRING },
44 [DEV_ATTR_ACCEPTLOCAL] = { .name = "acceptlocal", .type = BLOBMSG_TYPE_BOOL },
45 [DEV_ATTR_IGMPVERSION] = { .name = "igmpversion", .type = BLOBMSG_TYPE_INT32 },
46 [DEV_ATTR_MLDVERSION] = { .name = "mldversion", .type = BLOBMSG_TYPE_INT32 },
47 [DEV_ATTR_NEIGHREACHABLETIME] = { .name = "neighreachabletime", .type = BLOBMSG_TYPE_INT32 },
48 [DEV_ATTR_RPS] = { .name = "rps", .type = BLOBMSG_TYPE_BOOL },
49 [DEV_ATTR_XPS] = { .name = "xps", .type = BLOBMSG_TYPE_BOOL },
50 [DEV_ATTR_DADTRANSMITS] = { .name = "dadtransmits", .type = BLOBMSG_TYPE_INT32 },
51 [DEV_ATTR_MULTICAST_TO_UNICAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL },
52 [DEV_ATTR_MULTICAST_ROUTER] = { .name = "multicast_router", .type = BLOBMSG_TYPE_INT32 },
53 };
54
55 const struct uci_blob_param_list device_attr_list = {
56 .n_params = __DEV_ATTR_MAX,
57 .params = dev_attrs,
58 };
59
60 static int __devlock = 0;
61
62 void device_lock(void)
63 {
64 __devlock++;
65 }
66
67 void device_unlock(void)
68 {
69 __devlock--;
70 if (!__devlock)
71 device_free_unused(NULL);
72 }
73
74 static int set_device_state(struct device *dev, bool state)
75 {
76 if (state) {
77 /* Get ifindex for all devices being enabled so a valid */
78 /* ifindex is in place avoiding possible race conditions */
79 device_set_ifindex(dev, system_if_resolve(dev));
80 if (!dev->ifindex)
81 return -1;
82
83 system_if_up(dev);
84 }
85 else
86 system_if_down(dev);
87
88 return 0;
89 }
90
91 static int
92 simple_device_set_state(struct device *dev, bool state)
93 {
94 struct device *pdev;
95 int ret = 0;
96
97 pdev = dev->parent.dev;
98 if (state && !pdev) {
99 pdev = system_if_get_parent(dev);
100 if (pdev)
101 device_add_user(&dev->parent, pdev);
102 }
103
104 if (pdev) {
105 if (state)
106 ret = device_claim(&dev->parent);
107 else
108 device_release(&dev->parent);
109
110 if (ret < 0)
111 return ret;
112 }
113 return set_device_state(dev, state);
114 }
115
116 static struct device *
117 simple_device_create(const char *name, struct blob_attr *attr)
118 {
119 struct blob_attr *tb[__DEV_ATTR_MAX];
120 struct device *dev = NULL;
121
122 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
123 dev = device_get(name, true);
124 if (!dev)
125 return NULL;
126
127 dev->set_state = simple_device_set_state;
128 device_init_settings(dev, tb);
129
130 return dev;
131 }
132
133 static void simple_device_free(struct device *dev)
134 {
135 if (dev->parent.dev)
136 device_remove_user(&dev->parent);
137 free(dev);
138 }
139
140 const struct device_type simple_device_type = {
141 .name = "Network device",
142 .config_params = &device_attr_list,
143
144 .create = simple_device_create,
145 .check_state = system_if_check,
146 .free = simple_device_free,
147 };
148
149 static void
150 device_merge_settings(struct device *dev, struct device_settings *n)
151 {
152 struct device_settings *os = &dev->orig_settings;
153 struct device_settings *s = &dev->settings;
154
155 memset(n, 0, sizeof(*n));
156 n->mtu = s->flags & DEV_OPT_MTU ? s->mtu : os->mtu;
157 n->mtu6 = s->flags & DEV_OPT_MTU6 ? s->mtu6 : os->mtu6;
158 n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ?
159 s->txqueuelen : os->txqueuelen;
160 memcpy(n->macaddr,
161 (s->flags & DEV_OPT_MACADDR ? s->macaddr : os->macaddr),
162 sizeof(n->macaddr));
163 n->ipv6 = s->flags & DEV_OPT_IPV6 ? s->ipv6 : os->ipv6;
164 n->promisc = s->flags & DEV_OPT_PROMISC ? s->promisc : os->promisc;
165 n->rpfilter = s->flags & DEV_OPT_RPFILTER ? s->rpfilter : os->rpfilter;
166 n->acceptlocal = s->flags & DEV_OPT_ACCEPTLOCAL ? s->acceptlocal : os->acceptlocal;
167 n->igmpversion = s->flags & DEV_OPT_IGMPVERSION ? s->igmpversion : os->igmpversion;
168 n->mldversion = s->flags & DEV_OPT_MLDVERSION ? s->mldversion : os->mldversion;
169 n->neigh4reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
170 s->neigh4reachabletime : os->neigh4reachabletime;
171 n->neigh6reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
172 s->neigh6reachabletime : os->neigh6reachabletime;
173 n->dadtransmits = s->flags & DEV_OPT_DADTRANSMITS ?
174 s->dadtransmits : os->dadtransmits;
175 n->multicast_to_unicast = s->multicast_to_unicast;
176 n->multicast_router = s->multicast_router;
177 n->flags = s->flags | os->flags;
178 }
179
180 void
181 device_init_settings(struct device *dev, struct blob_attr **tb)
182 {
183 struct device_settings *s = &dev->settings;
184 struct blob_attr *cur;
185 struct ether_addr *ea;
186 bool disabled = false;
187
188 s->flags = 0;
189 if ((cur = tb[DEV_ATTR_ENABLED]))
190 disabled = !blobmsg_get_bool(cur);
191
192 if ((cur = tb[DEV_ATTR_MTU])) {
193 s->mtu = blobmsg_get_u32(cur);
194 s->flags |= DEV_OPT_MTU;
195 }
196
197 if ((cur = tb[DEV_ATTR_MTU6])) {
198 s->mtu6 = blobmsg_get_u32(cur);
199 s->flags |= DEV_OPT_MTU6;
200 }
201
202 if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
203 s->txqueuelen = blobmsg_get_u32(cur);
204 s->flags |= DEV_OPT_TXQUEUELEN;
205 }
206
207 if ((cur = tb[DEV_ATTR_MACADDR])) {
208 ea = ether_aton(blobmsg_data(cur));
209 if (ea) {
210 memcpy(s->macaddr, ea, 6);
211 s->flags |= DEV_OPT_MACADDR;
212 }
213 }
214
215 if ((cur = tb[DEV_ATTR_IPV6])) {
216 s->ipv6 = blobmsg_get_bool(cur);
217 s->flags |= DEV_OPT_IPV6;
218 }
219
220 if ((cur = tb[DEV_ATTR_PROMISC])) {
221 s->promisc = blobmsg_get_bool(cur);
222 s->flags |= DEV_OPT_PROMISC;
223 }
224
225 if ((cur = tb[DEV_ATTR_RPFILTER])) {
226 if (system_resolve_rpfilter(blobmsg_data(cur), &s->rpfilter))
227 s->flags |= DEV_OPT_RPFILTER;
228 else
229 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur));
230 }
231
232 if ((cur = tb[DEV_ATTR_ACCEPTLOCAL])) {
233 s->acceptlocal = blobmsg_get_bool(cur);
234 s->flags |= DEV_OPT_ACCEPTLOCAL;
235 }
236
237 if ((cur = tb[DEV_ATTR_IGMPVERSION])) {
238 s->igmpversion = blobmsg_get_u32(cur);
239 if (s->igmpversion >= 1 && s->igmpversion <= 3)
240 s->flags |= DEV_OPT_IGMPVERSION;
241 else
242 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur));
243 }
244
245 if ((cur = tb[DEV_ATTR_MLDVERSION])) {
246 s->mldversion = blobmsg_get_u32(cur);
247 if (s->mldversion >= 1 && s->mldversion <= 2)
248 s->flags |= DEV_OPT_MLDVERSION;
249 else
250 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur));
251 }
252
253 if ((cur = tb[DEV_ATTR_NEIGHREACHABLETIME])) {
254 s->neigh6reachabletime = s->neigh4reachabletime = blobmsg_get_u32(cur);
255 s->flags |= DEV_OPT_NEIGHREACHABLETIME;
256 }
257
258 if ((cur = tb[DEV_ATTR_RPS])) {
259 s->rps = blobmsg_get_bool(cur);
260 s->flags |= DEV_OPT_RPS;
261 }
262 else
263 s->rps = default_ps;
264
265 if ((cur = tb[DEV_ATTR_XPS])) {
266 s->xps = blobmsg_get_bool(cur);
267 s->flags |= DEV_OPT_XPS;
268 }
269 else
270 s->xps = default_ps;
271
272 if ((cur = tb[DEV_ATTR_DADTRANSMITS])) {
273 s->dadtransmits = blobmsg_get_u32(cur);
274 s->flags |= DEV_OPT_DADTRANSMITS;
275 }
276
277 if ((cur = tb[DEV_ATTR_MULTICAST_TO_UNICAST])) {
278 s->multicast_to_unicast = blobmsg_get_bool(cur);
279 s->flags |= DEV_OPT_MULTICAST_TO_UNICAST;
280 }
281
282 if ((cur = tb[DEV_ATTR_MULTICAST_ROUTER])) {
283 s->multicast_router = blobmsg_get_u32(cur);
284 if (s->multicast_router <= 2)
285 s->flags |= DEV_OPT_MULTICAST_ROUTER;
286 else
287 DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur));
288 }
289
290 device_set_disabled(dev, disabled);
291 }
292
293 static void __init dev_init(void)
294 {
295 avl_init(&devices, avl_strcmp, true, NULL);
296 }
297
298 static int device_broadcast_cb(void *ctx, struct safe_list *list)
299 {
300 struct device_user *dep = container_of(list, struct device_user, list);
301 int *ev = ctx;
302
303 /* device might have been removed by an earlier callback */
304 if (!dep->dev)
305 return 0;
306
307 if (dep->cb)
308 dep->cb(dep, *ev);
309 return 0;
310 }
311
312 void device_broadcast_event(struct device *dev, enum device_event ev)
313 {
314 int dev_ev = ev;
315
316 safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev);
317 safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev);
318 }
319
320 int device_claim(struct device_user *dep)
321 {
322 struct device *dev = dep->dev;
323 int ret = 0;
324
325 if (dep->claimed)
326 return 0;
327
328 dep->claimed = true;
329 D(DEVICE, "Claim %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active + 1);
330 if (++dev->active != 1)
331 return 0;
332
333 device_broadcast_event(dev, DEV_EVENT_SETUP);
334 if (dev->external) {
335 /* Get ifindex for external claimed devices so a valid */
336 /* ifindex is in place avoiding possible race conditions */
337 device_set_ifindex(dev, system_if_resolve(dev));
338 if (!dev->ifindex)
339 ret = -1;
340 } else
341 ret = dev->set_state(dev, true);
342
343 if (ret == 0)
344 device_broadcast_event(dev, DEV_EVENT_UP);
345 else {
346 D(DEVICE, "claim %s %s failed: %d\n", dev->type->name, dev->ifname, ret);
347 dev->active = 0;
348 dep->claimed = false;
349 }
350
351 return ret;
352 }
353
354 void device_release(struct device_user *dep)
355 {
356 struct device *dev = dep->dev;
357
358 if (!dep->claimed)
359 return;
360
361 dep->claimed = false;
362 dev->active--;
363 D(DEVICE, "Release %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active);
364 assert(dev->active >= 0);
365
366 if (dev->active)
367 return;
368
369 device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
370 if (!dev->external)
371 dev->set_state(dev, false);
372 device_broadcast_event(dev, DEV_EVENT_DOWN);
373 }
374
375 int device_check_state(struct device *dev)
376 {
377 if (!dev->type->check_state)
378 return simple_device_type.check_state(dev);
379
380 return dev->type->check_state(dev);
381 }
382
383 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name)
384 {
385 assert(dev);
386 assert(type);
387
388 if (name)
389 strncpy(dev->ifname, name, IFNAMSIZ);
390
391 D(DEVICE, "Initialize device '%s'\n", dev->ifname);
392 INIT_SAFE_LIST(&dev->users);
393 INIT_SAFE_LIST(&dev->aliases);
394 dev->type = type;
395
396 if (!dev->set_state)
397 dev->set_state = set_device_state;
398 }
399
400 int device_init(struct device *dev, const struct device_type *type, const char *ifname)
401 {
402 int ret;
403
404 device_init_virtual(dev, type, ifname);
405
406 dev->avl.key = dev->ifname;
407
408 ret = avl_insert(&devices, &dev->avl);
409 if (ret < 0)
410 return ret;
411
412 system_if_clear_state(dev);
413 device_check_state(dev);
414 dev->settings.rps = default_ps;
415 dev->settings.xps = default_ps;
416
417 return 0;
418 }
419
420 static struct device *
421 device_create_default(const char *name, bool external)
422 {
423 struct device *dev;
424
425 if (!external && system_if_force_external(name))
426 return NULL;
427
428 D(DEVICE, "Create simple device '%s'\n", name);
429 dev = calloc(1, sizeof(*dev));
430 dev->external = external;
431 dev->set_state = simple_device_set_state;
432 device_init(dev, &simple_device_type, name);
433 dev->default_config = true;
434 if (external)
435 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
436 return dev;
437 }
438
439 struct device *
440 device_get(const char *name, int create)
441 {
442 struct device *dev;
443
444 if (strchr(name, '.'))
445 return get_vlan_device_chain(name, create);
446
447 if (name[0] == '@')
448 return device_alias_get(name + 1);
449
450 dev = avl_find_element(&devices, name, dev, avl);
451 if (dev) {
452 if (create > 1 && !dev->external) {
453 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
454 dev->external = true;
455 device_set_present(dev, true);
456 }
457 return dev;
458 }
459
460 if (!create)
461 return NULL;
462
463 return device_create_default(name, create > 1);
464 }
465
466 static void
467 device_delete(struct device *dev)
468 {
469 if (!dev->avl.key)
470 return;
471
472 D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
473 avl_delete(&devices, &dev->avl);
474 dev->avl.key = NULL;
475 }
476
477 static int device_cleanup_cb(void *ctx, struct safe_list *list)
478 {
479 struct device_user *dep = container_of(list, struct device_user, list);
480 if (dep->cb)
481 dep->cb(dep, DEV_EVENT_REMOVE);
482
483 device_release(dep);
484 return 0;
485 }
486
487 void device_cleanup(struct device *dev)
488 {
489 D(DEVICE, "Clean up device '%s'\n", dev->ifname);
490 safe_list_for_each(&dev->users, device_cleanup_cb, NULL);
491 safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL);
492 device_delete(dev);
493 }
494
495 static void __device_set_present(struct device *dev, bool state)
496 {
497 if (dev->present == state)
498 return;
499
500 dev->present = state;
501 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
502 }
503
504 void
505 device_refresh_present(struct device *dev)
506 {
507 bool state = dev->sys_present;
508
509 if (dev->disabled || dev->deferred)
510 state = false;
511
512 __device_set_present(dev, state);
513 }
514
515 void device_set_present(struct device *dev, bool state)
516 {
517 if (dev->sys_present == state)
518 return;
519
520 D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
521 dev->sys_present = state;
522 device_refresh_present(dev);
523 }
524
525 void device_set_link(struct device *dev, bool state)
526 {
527 if (dev->link_active == state)
528 return;
529
530 netifd_log_message(L_NOTICE, "%s '%s' link is %s\n", dev->type->name, dev->ifname, state ? "up" : "down" );
531
532 dev->link_active = state;
533 device_broadcast_event(dev, state ? DEV_EVENT_LINK_UP : DEV_EVENT_LINK_DOWN);
534 }
535
536 void device_set_ifindex(struct device *dev, int ifindex)
537 {
538 if (dev->ifindex == ifindex)
539 return;
540
541 dev->ifindex = ifindex;
542 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX);
543 }
544
545 static int device_refcount(struct device *dev)
546 {
547 struct list_head *list;
548 int count = 0;
549
550 list_for_each(list, &dev->users.list)
551 count++;
552
553 list_for_each(list, &dev->aliases.list)
554 count++;
555
556 return count;
557 }
558
559 static void
560 __device_add_user(struct device_user *dep, struct device *dev)
561 {
562 struct safe_list *head;
563
564 dep->dev = dev;
565
566 if (dep->alias)
567 head = &dev->aliases;
568 else
569 head = &dev->users;
570
571 safe_list_add(&dep->list, head);
572 D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
573
574 if (dep->cb && dev->present) {
575 dep->cb(dep, DEV_EVENT_ADD);
576 if (dev->active)
577 dep->cb(dep, DEV_EVENT_UP);
578
579 if (dev->link_active)
580 dep->cb(dep, DEV_EVENT_LINK_UP);
581 }
582 }
583
584 void device_add_user(struct device_user *dep, struct device *dev)
585 {
586 if (dep->dev == dev)
587 return;
588
589 if (dep->dev)
590 device_remove_user(dep);
591
592 if (!dev)
593 return;
594
595 __device_add_user(dep, dev);
596 }
597
598 void
599 device_free(struct device *dev)
600 {
601 __devlock++;
602 free(dev->config);
603 device_cleanup(dev);
604 dev->type->free(dev);
605 __devlock--;
606 }
607
608 static void
609 __device_free_unused(struct device *dev)
610 {
611 if (!safe_list_empty(&dev->users) ||
612 !safe_list_empty(&dev->aliases) ||
613 dev->current_config || __devlock)
614 return;
615
616 device_free(dev);
617 }
618
619 void device_remove_user(struct device_user *dep)
620 {
621 struct device *dev = dep->dev;
622
623 if (!dep->dev)
624 return;
625
626 dep->hotplug = false;
627 if (dep->claimed)
628 device_release(dep);
629
630 safe_list_del(&dep->list);
631 dep->dev = NULL;
632 D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
633 __device_free_unused(dev);
634 }
635
636 void
637 device_free_unused(struct device *dev)
638 {
639 struct device *tmp;
640
641 if (dev)
642 return __device_free_unused(dev);
643
644 avl_for_each_element_safe(&devices, dev, avl, tmp)
645 __device_free_unused(dev);
646 }
647
648 void
649 device_init_pending(void)
650 {
651 struct device *dev, *tmp;
652
653 avl_for_each_element_safe(&devices, dev, avl, tmp) {
654 if (!dev->config_pending)
655 continue;
656
657 dev->type->config_init(dev);
658 dev->config_pending = false;
659 }
660 }
661
662 static enum dev_change_type
663 device_set_config(struct device *dev, const struct device_type *type,
664 struct blob_attr *attr)
665 {
666 struct blob_attr *tb[__DEV_ATTR_MAX];
667 const struct uci_blob_param_list *cfg = type->config_params;
668
669 if (type != dev->type)
670 return DEV_CONFIG_RECREATE;
671
672 if (dev->type->reload)
673 return dev->type->reload(dev, attr);
674
675 if (uci_blob_check_equal(dev->config, attr, cfg))
676 return DEV_CONFIG_NO_CHANGE;
677
678 if (cfg == &device_attr_list) {
679 memset(tb, 0, sizeof(tb));
680
681 if (attr)
682 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
683 blob_data(attr), blob_len(attr));
684
685 device_init_settings(dev, tb);
686 return DEV_CONFIG_RESTART;
687 } else
688 return DEV_CONFIG_RECREATE;
689 }
690
691 enum dev_change_type
692 device_apply_config(struct device *dev, const struct device_type *type,
693 struct blob_attr *config)
694 {
695 enum dev_change_type change;
696
697 change = device_set_config(dev, type, config);
698 if (dev->external) {
699 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
700 change = DEV_CONFIG_APPLIED;
701 }
702
703 switch (change) {
704 case DEV_CONFIG_RESTART:
705 case DEV_CONFIG_APPLIED:
706 D(DEVICE, "Device '%s': config applied\n", dev->ifname);
707 config = blob_memdup(config);
708 free(dev->config);
709 dev->config = config;
710 if (change == DEV_CONFIG_RESTART && dev->present) {
711 device_set_present(dev, false);
712 if (dev->active && !dev->external) {
713 dev->set_state(dev, false);
714 dev->set_state(dev, true);
715 }
716 device_set_present(dev, true);
717 }
718 break;
719 case DEV_CONFIG_NO_CHANGE:
720 D(DEVICE, "Device '%s': no configuration change\n", dev->ifname);
721 break;
722 case DEV_CONFIG_RECREATE:
723 break;
724 }
725
726 return change;
727 }
728
729 static void
730 device_replace(struct device *dev, struct device *odev)
731 {
732 struct device_user *dep, *tmp;
733
734 __devlock++;
735 if (odev->present)
736 device_set_present(odev, false);
737
738 list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) {
739 device_release(dep);
740 safe_list_del(&dep->list);
741 __device_add_user(dep, dev);
742 }
743 __devlock--;
744
745 device_free(odev);
746 }
747
748 void
749 device_reset_config(void)
750 {
751 struct device *dev;
752
753 avl_for_each_element(&devices, dev, avl)
754 dev->current_config = false;
755 }
756
757 void
758 device_reset_old(void)
759 {
760 struct device *dev, *tmp, *ndev;
761
762 avl_for_each_element_safe(&devices, dev, avl, tmp) {
763 if (dev->current_config || dev->default_config)
764 continue;
765
766 if (dev->type != &simple_device_type)
767 continue;
768
769 ndev = device_create_default(dev->ifname, dev->external);
770 if (!ndev)
771 continue;
772
773 device_replace(ndev, dev);
774 }
775 }
776
777 void
778 device_set_default_ps(bool state)
779 {
780 struct device *dev;
781
782 if (state == default_ps)
783 return;
784
785 default_ps = state;
786
787 avl_for_each_element(&devices, dev, avl) {
788 struct device_settings *s = &dev->settings;
789 unsigned int apply_mask = 0;
790
791 if (!(s->flags & DEV_OPT_RPS)) {
792 s->rps = default_ps;
793 apply_mask |= DEV_OPT_RPS;
794 }
795
796 if (!(s->flags & DEV_OPT_XPS)) {
797 s->xps = default_ps;
798 apply_mask |= DEV_OPT_XPS;
799 }
800
801 if (!apply_mask)
802 continue;
803
804 if (!(dev->external || (dev->present && dev->active)) ||
805 dev->config_pending)
806 continue;
807
808 system_if_apply_settings(dev, s, apply_mask);
809 }
810 }
811
812 struct device *
813 device_create(const char *name, const struct device_type *type,
814 struct blob_attr *config)
815 {
816 struct device *odev = NULL, *dev;
817 enum dev_change_type change;
818
819 odev = device_get(name, false);
820 if (odev) {
821 odev->current_config = true;
822 change = device_apply_config(odev, type, config);
823 switch (change) {
824 case DEV_CONFIG_RECREATE:
825 D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
826 device_delete(odev);
827 break;
828 default:
829 return odev;
830 }
831 } else
832 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
833
834 config = blob_memdup(config);
835 if (!config)
836 return NULL;
837
838 dev = type->create(name, config);
839 if (!dev)
840 return NULL;
841
842 dev->current_config = true;
843 dev->config = config;
844 if (odev)
845 device_replace(dev, odev);
846
847 if (!config_init && dev->config_pending) {
848 type->config_init(dev);
849 dev->config_pending = false;
850 }
851
852 return dev;
853 }
854
855 void
856 device_dump_status(struct blob_buf *b, struct device *dev)
857 {
858 struct device_settings st;
859 void *c, *s;
860
861 if (!dev) {
862 avl_for_each_element(&devices, dev, avl) {
863 if (!dev->present)
864 continue;
865 c = blobmsg_open_table(b, dev->ifname);
866 device_dump_status(b, dev);
867 blobmsg_close_table(b, c);
868 }
869
870 return;
871 }
872
873 blobmsg_add_u8(b, "external", dev->external);
874 blobmsg_add_u8(b, "present", dev->present);
875 blobmsg_add_string(b, "type", dev->type->name);
876
877 if (!dev->present)
878 return;
879
880 blobmsg_add_u8(b, "up", !!dev->active);
881 blobmsg_add_u8(b, "carrier", !!dev->link_active);
882
883 if (dev->type->dump_info)
884 dev->type->dump_info(dev, b);
885 else
886 system_if_dump_info(dev, b);
887
888 if (dev->active) {
889 device_merge_settings(dev, &st);
890 if (st.flags & DEV_OPT_MTU)
891 blobmsg_add_u32(b, "mtu", st.mtu);
892 if (st.flags & DEV_OPT_MTU6)
893 blobmsg_add_u32(b, "mtu6", st.mtu6);
894 if (st.flags & DEV_OPT_MACADDR)
895 blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
896 if (st.flags & DEV_OPT_TXQUEUELEN)
897 blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
898 if (st.flags & DEV_OPT_IPV6)
899 blobmsg_add_u8(b, "ipv6", st.ipv6);
900 if (st.flags & DEV_OPT_PROMISC)
901 blobmsg_add_u8(b, "promisc", st.promisc);
902 if (st.flags & DEV_OPT_RPFILTER)
903 blobmsg_add_u32(b, "rpfilter", st.rpfilter);
904 if (st.flags & DEV_OPT_ACCEPTLOCAL)
905 blobmsg_add_u8(b, "acceptlocal", st.acceptlocal);
906 if (st.flags & DEV_OPT_IGMPVERSION)
907 blobmsg_add_u32(b, "igmpversion", st.igmpversion);
908 if (st.flags & DEV_OPT_MLDVERSION)
909 blobmsg_add_u32(b, "mldversion", st.mldversion);
910 if (st.flags & DEV_OPT_NEIGHREACHABLETIME) {
911 blobmsg_add_u32(b, "neigh4reachabletime", st.neigh4reachabletime);
912 blobmsg_add_u32(b, "neigh6reachabletime", st.neigh6reachabletime);
913 }
914 if (st.flags & DEV_OPT_DADTRANSMITS)
915 blobmsg_add_u32(b, "dadtransmits", st.dadtransmits);
916 if (st.flags & DEV_OPT_MULTICAST_TO_UNICAST)
917 blobmsg_add_u8(b, "multicast_to_unicast", st.multicast_to_unicast);
918 if (st.flags & DEV_OPT_MULTICAST_ROUTER)
919 blobmsg_add_u32(b, "multicast_router", st.multicast_router);
920 }
921
922 s = blobmsg_open_table(b, "statistics");
923 if (dev->type->dump_stats)
924 dev->type->dump_stats(dev, b);
925 else
926 system_if_dump_stats(dev, b);
927 blobmsg_close_table(b, s);
928 }