2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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
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.
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <net/ethernet.h>
24 #include <netinet/ether.h>
27 #include <libubox/list.h>
33 static struct list_head devtypes = LIST_HEAD_INIT(devtypes);
34 static struct avl_tree devices;
35 static bool default_ps = true;
37 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
38 [DEV_ATTR_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
39 [DEV_ATTR_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
40 [DEV_ATTR_MTU6] = { .name = "mtu6", .type = BLOBMSG_TYPE_INT32 },
41 [DEV_ATTR_MACADDR] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING },
42 [DEV_ATTR_TXQUEUELEN] = { .name = "txqueuelen", .type = BLOBMSG_TYPE_INT32 },
43 [DEV_ATTR_ENABLED] = { .name = "enabled", .type = BLOBMSG_TYPE_BOOL },
44 [DEV_ATTR_IPV6] = { .name = "ipv6", .type = BLOBMSG_TYPE_BOOL },
45 [DEV_ATTR_PROMISC] = { .name = "promisc", .type = BLOBMSG_TYPE_BOOL },
46 [DEV_ATTR_RPFILTER] = { .name = "rpfilter", .type = BLOBMSG_TYPE_STRING },
47 [DEV_ATTR_ACCEPTLOCAL] = { .name = "acceptlocal", .type = BLOBMSG_TYPE_BOOL },
48 [DEV_ATTR_IGMPVERSION] = { .name = "igmpversion", .type = BLOBMSG_TYPE_INT32 },
49 [DEV_ATTR_MLDVERSION] = { .name = "mldversion", .type = BLOBMSG_TYPE_INT32 },
50 [DEV_ATTR_NEIGHREACHABLETIME] = { .name = "neighreachabletime", .type = BLOBMSG_TYPE_INT32 },
51 [DEV_ATTR_NEIGHGCSTALETIME] = { .name = "neighgcstaletime", .type = BLOBMSG_TYPE_INT32 },
52 [DEV_ATTR_RPS] = { .name = "rps", .type = BLOBMSG_TYPE_BOOL },
53 [DEV_ATTR_XPS] = { .name = "xps", .type = BLOBMSG_TYPE_BOOL },
54 [DEV_ATTR_DADTRANSMITS] = { .name = "dadtransmits", .type = BLOBMSG_TYPE_INT32 },
55 [DEV_ATTR_MULTICAST_TO_UNICAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL },
56 [DEV_ATTR_MULTICAST_ROUTER] = { .name = "multicast_router", .type = BLOBMSG_TYPE_INT32 },
57 [DEV_ATTR_MULTICAST] = { .name ="multicast", .type = BLOBMSG_TYPE_BOOL },
58 [DEV_ATTR_LEARNING] = { .name ="learning", .type = BLOBMSG_TYPE_BOOL },
59 [DEV_ATTR_UNICAST_FLOOD] = { .name ="unicast_flood", .type = BLOBMSG_TYPE_BOOL },
62 const struct uci_blob_param_list device_attr_list = {
63 .n_params = __DEV_ATTR_MAX,
67 static int __devlock = 0;
69 int device_type_add(struct device_type *devtype)
71 if (device_type_get(devtype->name)) {
72 netifd_log_message(L_WARNING, "Device handler '%s' already exists\n",
77 netifd_log_message(L_NOTICE, "Added device handler type: %s\n",
80 list_add(&devtype->list, &devtypes);
84 /* initialize device type list and add known types */
85 static void __init devtypes_init(void)
87 device_type_add(&simple_device_type);
88 device_type_add(&bridge_device_type);
89 device_type_add(&tunnel_device_type);
90 device_type_add(&macvlan_device_type);
91 device_type_add(&vlandev_device_type);
94 /* Retrieve the device type for the given name. If 'bridge' is true, the type
95 * must have bridge capabilities
98 device_type_get(const char *tname)
100 struct device_type *cur;
102 list_for_each_entry(cur, &devtypes, list)
103 if (!strcmp(cur->name, tname))
109 void device_lock(void)
114 void device_unlock(void)
118 device_free_unused(NULL);
121 static int set_device_state(struct device *dev, bool state)
124 /* Get ifindex for all devices being enabled so a valid */
125 /* ifindex is in place avoiding possible race conditions */
126 device_set_ifindex(dev, system_if_resolve(dev));
139 simple_device_set_state(struct device *dev, bool state)
144 pdev = dev->parent.dev;
145 if (state && !pdev) {
146 pdev = system_if_get_parent(dev);
148 device_add_user(&dev->parent, pdev);
153 ret = device_claim(&dev->parent);
155 device_release(&dev->parent);
160 return set_device_state(dev, state);
163 static struct device *
164 simple_device_create(const char *name, struct device_type *devtype,
165 struct blob_attr *attr)
167 struct blob_attr *tb[__DEV_ATTR_MAX];
168 struct device *dev = NULL;
170 /* device type is unused for simple devices */
173 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
174 dev = device_get(name, true);
178 dev->set_state = simple_device_set_state;
179 device_init_settings(dev, tb);
184 static void simple_device_free(struct device *dev)
187 device_remove_user(&dev->parent);
191 struct device_type simple_device_type = {
192 .name = "Network device",
193 .config_params = &device_attr_list,
195 .create = simple_device_create,
196 .check_state = system_if_check,
197 .free = simple_device_free,
201 device_merge_settings(struct device *dev, struct device_settings *n)
203 struct device_settings *os = &dev->orig_settings;
204 struct device_settings *s = &dev->settings;
206 memset(n, 0, sizeof(*n));
207 n->mtu = s->flags & DEV_OPT_MTU ? s->mtu : os->mtu;
208 n->mtu6 = s->flags & DEV_OPT_MTU6 ? s->mtu6 : os->mtu6;
209 n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ?
210 s->txqueuelen : os->txqueuelen;
212 (s->flags & DEV_OPT_MACADDR ? s->macaddr : os->macaddr),
214 n->ipv6 = s->flags & DEV_OPT_IPV6 ? s->ipv6 : os->ipv6;
215 n->promisc = s->flags & DEV_OPT_PROMISC ? s->promisc : os->promisc;
216 n->rpfilter = s->flags & DEV_OPT_RPFILTER ? s->rpfilter : os->rpfilter;
217 n->acceptlocal = s->flags & DEV_OPT_ACCEPTLOCAL ? s->acceptlocal : os->acceptlocal;
218 n->igmpversion = s->flags & DEV_OPT_IGMPVERSION ? s->igmpversion : os->igmpversion;
219 n->mldversion = s->flags & DEV_OPT_MLDVERSION ? s->mldversion : os->mldversion;
220 n->neigh4reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
221 s->neigh4reachabletime : os->neigh4reachabletime;
222 n->neigh6reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
223 s->neigh6reachabletime : os->neigh6reachabletime;
224 n->neigh4gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ?
225 s->neigh4gcstaletime : os->neigh4gcstaletime;
226 n->neigh6gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ?
227 s->neigh6gcstaletime : os->neigh6gcstaletime;
228 n->dadtransmits = s->flags & DEV_OPT_DADTRANSMITS ?
229 s->dadtransmits : os->dadtransmits;
230 n->multicast = s->flags & DEV_OPT_MULTICAST ?
231 s->multicast : os->multicast;
232 n->multicast_to_unicast = s->multicast_to_unicast;
233 n->multicast_router = s->multicast_router;
234 n->learning = s->learning;
235 n->unicast_flood = s->unicast_flood;
236 n->flags = s->flags | os->flags | os->valid_flags;
240 device_init_settings(struct device *dev, struct blob_attr **tb)
242 struct device_settings *s = &dev->settings;
243 struct blob_attr *cur;
244 struct ether_addr *ea;
245 bool disabled = false;
248 if ((cur = tb[DEV_ATTR_ENABLED]))
249 disabled = !blobmsg_get_bool(cur);
251 if ((cur = tb[DEV_ATTR_MTU])) {
252 s->mtu = blobmsg_get_u32(cur);
253 s->flags |= DEV_OPT_MTU;
256 if ((cur = tb[DEV_ATTR_MTU6])) {
257 s->mtu6 = blobmsg_get_u32(cur);
258 s->flags |= DEV_OPT_MTU6;
261 if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
262 s->txqueuelen = blobmsg_get_u32(cur);
263 s->flags |= DEV_OPT_TXQUEUELEN;
266 if ((cur = tb[DEV_ATTR_MACADDR])) {
267 ea = ether_aton(blobmsg_data(cur));
269 memcpy(s->macaddr, ea, 6);
270 s->flags |= DEV_OPT_MACADDR;
274 if ((cur = tb[DEV_ATTR_IPV6])) {
275 s->ipv6 = blobmsg_get_bool(cur);
276 s->flags |= DEV_OPT_IPV6;
279 if ((cur = tb[DEV_ATTR_PROMISC])) {
280 s->promisc = blobmsg_get_bool(cur);
281 s->flags |= DEV_OPT_PROMISC;
284 if ((cur = tb[DEV_ATTR_RPFILTER])) {
285 if (system_resolve_rpfilter(blobmsg_data(cur), &s->rpfilter))
286 s->flags |= DEV_OPT_RPFILTER;
288 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur));
291 if ((cur = tb[DEV_ATTR_ACCEPTLOCAL])) {
292 s->acceptlocal = blobmsg_get_bool(cur);
293 s->flags |= DEV_OPT_ACCEPTLOCAL;
296 if ((cur = tb[DEV_ATTR_IGMPVERSION])) {
297 s->igmpversion = blobmsg_get_u32(cur);
298 if (s->igmpversion >= 1 && s->igmpversion <= 3)
299 s->flags |= DEV_OPT_IGMPVERSION;
301 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur));
304 if ((cur = tb[DEV_ATTR_MLDVERSION])) {
305 s->mldversion = blobmsg_get_u32(cur);
306 if (s->mldversion >= 1 && s->mldversion <= 2)
307 s->flags |= DEV_OPT_MLDVERSION;
309 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur));
312 if ((cur = tb[DEV_ATTR_NEIGHREACHABLETIME])) {
313 s->neigh6reachabletime = s->neigh4reachabletime = blobmsg_get_u32(cur);
314 s->flags |= DEV_OPT_NEIGHREACHABLETIME;
317 if ((cur = tb[DEV_ATTR_NEIGHGCSTALETIME])) {
318 s->neigh6gcstaletime = s->neigh4gcstaletime = blobmsg_get_u32(cur);
319 s->flags |= DEV_OPT_NEIGHGCSTALETIME;
322 if ((cur = tb[DEV_ATTR_RPS])) {
323 s->rps = blobmsg_get_bool(cur);
324 s->flags |= DEV_OPT_RPS;
329 if ((cur = tb[DEV_ATTR_XPS])) {
330 s->xps = blobmsg_get_bool(cur);
331 s->flags |= DEV_OPT_XPS;
336 if ((cur = tb[DEV_ATTR_DADTRANSMITS])) {
337 s->dadtransmits = blobmsg_get_u32(cur);
338 s->flags |= DEV_OPT_DADTRANSMITS;
341 if ((cur = tb[DEV_ATTR_MULTICAST_TO_UNICAST])) {
342 s->multicast_to_unicast = blobmsg_get_bool(cur);
343 s->flags |= DEV_OPT_MULTICAST_TO_UNICAST;
346 if ((cur = tb[DEV_ATTR_MULTICAST_ROUTER])) {
347 s->multicast_router = blobmsg_get_u32(cur);
348 if (s->multicast_router <= 2)
349 s->flags |= DEV_OPT_MULTICAST_ROUTER;
351 DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur));
354 if ((cur = tb[DEV_ATTR_MULTICAST])) {
355 s->multicast = blobmsg_get_bool(cur);
356 s->flags |= DEV_OPT_MULTICAST;
359 if ((cur = tb[DEV_ATTR_LEARNING])) {
360 s->learning = blobmsg_get_bool(cur);
361 s->flags |= DEV_OPT_LEARNING;
364 if ((cur = tb[DEV_ATTR_UNICAST_FLOOD])) {
365 s->unicast_flood = blobmsg_get_bool(cur);
366 s->flags |= DEV_OPT_UNICAST_FLOOD;
369 device_set_disabled(dev, disabled);
372 static void __init dev_init(void)
374 avl_init(&devices, avl_strcmp, true, NULL);
377 static int device_broadcast_cb(void *ctx, struct safe_list *list)
379 struct device_user *dep = container_of(list, struct device_user, list);
382 /* device might have been removed by an earlier callback */
391 void device_broadcast_event(struct device *dev, enum device_event ev)
395 safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev);
396 safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev);
399 int device_claim(struct device_user *dep)
401 struct device *dev = dep->dev;
411 D(DEVICE, "Claim %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active + 1);
412 if (++dev->active != 1)
415 device_broadcast_event(dev, DEV_EVENT_SETUP);
417 /* Get ifindex for external claimed devices so a valid */
418 /* ifindex is in place avoiding possible race conditions */
419 device_set_ifindex(dev, system_if_resolve(dev));
423 system_if_get_settings(dev, &dev->orig_settings);
425 ret = dev->set_state(dev, true);
428 device_broadcast_event(dev, DEV_EVENT_UP);
430 D(DEVICE, "claim %s %s failed: %d\n", dev->type->name, dev->ifname, ret);
432 dep->claimed = false;
438 void device_release(struct device_user *dep)
440 struct device *dev = dep->dev;
445 dep->claimed = false;
447 D(DEVICE, "Release %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active);
448 assert(dev->active >= 0);
453 device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
455 dev->set_state(dev, false);
456 device_broadcast_event(dev, DEV_EVENT_DOWN);
459 int device_check_state(struct device *dev)
461 if (!dev->type->check_state)
462 return simple_device_type.check_state(dev);
464 return dev->type->check_state(dev);
467 void device_init_virtual(struct device *dev, struct device_type *type, const char *name)
472 D(DEVICE, "Initialize device '%s'\n", name ? name : "");
473 INIT_SAFE_LIST(&dev->users);
474 INIT_SAFE_LIST(&dev->aliases);
478 device_set_ifname(dev, name);
481 dev->set_state = set_device_state;
484 int device_init(struct device *dev, struct device_type *type, const char *ifname)
488 device_init_virtual(dev, type, ifname);
490 dev->avl.key = dev->ifname;
492 ret = avl_insert(&devices, &dev->avl);
496 system_if_clear_state(dev);
497 device_check_state(dev);
498 dev->settings.rps = default_ps;
499 dev->settings.xps = default_ps;
504 static struct device *
505 device_create_default(const char *name, bool external)
509 if (!external && system_if_force_external(name))
512 D(DEVICE, "Create simple device '%s'\n", name);
513 dev = calloc(1, sizeof(*dev));
517 dev->external = external;
518 dev->set_state = simple_device_set_state;
519 device_init(dev, &simple_device_type, name);
520 dev->default_config = true;
522 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
527 device_find(const char *name)
531 return avl_find_element(&devices, name, dev, avl);
535 device_get(const char *name, int create)
539 if (strchr(name, '.'))
540 return get_vlan_device_chain(name, create);
543 return device_alias_get(name + 1);
545 dev = avl_find_element(&devices, name, dev, avl);
547 if (create > 1 && !dev->external) {
548 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
549 dev->external = true;
550 device_set_present(dev, true);
558 return device_create_default(name, create > 1);
562 device_delete(struct device *dev)
567 D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
568 avl_delete(&devices, &dev->avl);
572 static int device_cleanup_cb(void *ctx, struct safe_list *list)
574 struct device_user *dep = container_of(list, struct device_user, list);
576 dep->cb(dep, DEV_EVENT_REMOVE);
582 void device_cleanup(struct device *dev)
584 D(DEVICE, "Clean up device '%s'\n", dev->ifname);
585 safe_list_for_each(&dev->users, device_cleanup_cb, NULL);
586 safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL);
590 static void __device_set_present(struct device *dev, bool state)
592 if (dev->present == state)
595 dev->present = state;
596 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
600 device_refresh_present(struct device *dev)
602 bool state = dev->sys_present;
604 if (dev->disabled || dev->deferred)
607 __device_set_present(dev, state);
610 void device_set_present(struct device *dev, bool state)
612 if (dev->sys_present == state)
615 D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
616 dev->sys_present = state;
617 device_refresh_present(dev);
620 void device_set_link(struct device *dev, bool state)
622 if (dev->link_active == state)
625 netifd_log_message(L_NOTICE, "%s '%s' link is %s\n", dev->type->name, dev->ifname, state ? "up" : "down" );
627 dev->link_active = state;
628 device_broadcast_event(dev, state ? DEV_EVENT_LINK_UP : DEV_EVENT_LINK_DOWN);
631 void device_set_ifindex(struct device *dev, int ifindex)
633 if (dev->ifindex == ifindex)
636 dev->ifindex = ifindex;
637 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX);
640 int device_set_ifname(struct device *dev, const char *name)
644 if (!strcmp(dev->ifname, name))
648 avl_delete(&devices, &dev->avl);
650 strncpy(dev->ifname, name, IFNAMSIZ);
653 ret = avl_insert(&devices, &dev->avl);
656 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFNAME);
661 static int device_refcount(struct device *dev)
663 struct list_head *list;
666 list_for_each(list, &dev->users.list)
669 list_for_each(list, &dev->aliases.list)
676 __device_add_user(struct device_user *dep, struct device *dev)
678 struct safe_list *head;
683 head = &dev->aliases;
687 safe_list_add(&dep->list, head);
688 D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
690 if (dep->cb && dev->present) {
691 dep->cb(dep, DEV_EVENT_ADD);
693 dep->cb(dep, DEV_EVENT_UP);
695 if (dev->link_active)
696 dep->cb(dep, DEV_EVENT_LINK_UP);
700 void device_add_user(struct device_user *dep, struct device *dev)
706 device_remove_user(dep);
711 __device_add_user(dep, dev);
715 device_free(struct device *dev)
720 dev->type->free(dev);
725 __device_free_unused(struct device *dev)
727 if (!safe_list_empty(&dev->users) ||
728 !safe_list_empty(&dev->aliases) ||
729 dev->current_config || __devlock)
735 void device_remove_user(struct device_user *dep)
737 struct device *dev = dep->dev;
742 dep->hotplug = false;
746 safe_list_del(&dep->list);
748 D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
749 __device_free_unused(dev);
753 device_free_unused(struct device *dev)
758 return __device_free_unused(dev);
760 avl_for_each_element_safe(&devices, dev, avl, tmp)
761 __device_free_unused(dev);
765 device_init_pending(void)
767 struct device *dev, *tmp;
769 avl_for_each_element_safe(&devices, dev, avl, tmp) {
770 if (!dev->config_pending)
773 dev->type->config_init(dev);
774 dev->config_pending = false;
778 static enum dev_change_type
779 device_set_config(struct device *dev, struct device_type *type,
780 struct blob_attr *attr)
782 struct blob_attr *tb[__DEV_ATTR_MAX];
783 const struct uci_blob_param_list *cfg = type->config_params;
785 if (type != dev->type)
786 return DEV_CONFIG_RECREATE;
788 if (dev->type->reload)
789 return dev->type->reload(dev, attr);
791 if (uci_blob_check_equal(dev->config, attr, cfg))
792 return DEV_CONFIG_NO_CHANGE;
794 if (cfg == &device_attr_list) {
795 memset(tb, 0, sizeof(tb));
798 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
799 blob_data(attr), blob_len(attr));
801 device_init_settings(dev, tb);
802 return DEV_CONFIG_RESTART;
804 return DEV_CONFIG_RECREATE;
808 device_apply_config(struct device *dev, struct device_type *type,
809 struct blob_attr *config)
811 enum dev_change_type change;
813 change = device_set_config(dev, type, config);
815 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
816 change = DEV_CONFIG_APPLIED;
820 case DEV_CONFIG_RESTART:
821 case DEV_CONFIG_APPLIED:
822 D(DEVICE, "Device '%s': config applied\n", dev->ifname);
823 config = blob_memdup(config);
825 dev->config = config;
826 if (change == DEV_CONFIG_RESTART && dev->present) {
829 device_set_present(dev, false);
830 if (dev->active && !dev->external) {
831 ret = dev->set_state(dev, false);
833 ret = dev->set_state(dev, true);
836 device_set_present(dev, true);
839 case DEV_CONFIG_NO_CHANGE:
840 D(DEVICE, "Device '%s': no configuration change\n", dev->ifname);
842 case DEV_CONFIG_RECREATE:
850 device_replace(struct device *dev, struct device *odev)
852 struct device_user *dep, *tmp;
856 device_set_present(odev, false);
858 list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) {
860 safe_list_del(&dep->list);
861 __device_add_user(dep, dev);
869 device_reset_config(void)
873 avl_for_each_element(&devices, dev, avl)
874 dev->current_config = false;
878 device_reset_old(void)
880 struct device *dev, *tmp, *ndev;
882 avl_for_each_element_safe(&devices, dev, avl, tmp) {
883 if (dev->current_config || dev->default_config)
886 if (dev->type != &simple_device_type)
889 ndev = device_create_default(dev->ifname, dev->external);
893 device_replace(ndev, dev);
898 device_set_default_ps(bool state)
902 if (state == default_ps)
907 avl_for_each_element(&devices, dev, avl) {
908 struct device_settings *s = &dev->settings;
909 unsigned int apply_mask = 0;
911 if (!(s->flags & DEV_OPT_RPS)) {
913 apply_mask |= DEV_OPT_RPS;
916 if (!(s->flags & DEV_OPT_XPS)) {
918 apply_mask |= DEV_OPT_XPS;
924 if (!(dev->external || (dev->present && dev->active)) ||
928 system_if_apply_settings(dev, s, apply_mask);
933 device_create(const char *name, struct device_type *type,
934 struct blob_attr *config)
936 struct device *odev = NULL, *dev;
937 enum dev_change_type change;
939 odev = device_get(name, false);
941 odev->current_config = true;
942 change = device_apply_config(odev, type, config);
944 case DEV_CONFIG_RECREATE:
945 D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
952 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
954 config = blob_memdup(config);
958 dev = type->create(name, type, config);
962 dev->current_config = true;
963 dev->config = config;
965 device_replace(dev, odev);
967 if (!config_init && dev->config_pending) {
968 type->config_init(dev);
969 dev->config_pending = false;
976 device_dump_status(struct blob_buf *b, struct device *dev)
978 struct device_settings st;
982 avl_for_each_element(&devices, dev, avl) {
985 c = blobmsg_open_table(b, dev->ifname);
986 device_dump_status(b, dev);
987 blobmsg_close_table(b, c);
993 blobmsg_add_u8(b, "external", dev->external);
994 blobmsg_add_u8(b, "present", dev->present);
995 blobmsg_add_string(b, "type", dev->type->name);
1000 blobmsg_add_u8(b, "up", !!dev->active);
1001 blobmsg_add_u8(b, "carrier", !!dev->link_active);
1003 if (dev->type->dump_info)
1004 dev->type->dump_info(dev, b);
1006 system_if_dump_info(dev, b);
1009 device_merge_settings(dev, &st);
1010 if (st.flags & DEV_OPT_MTU)
1011 blobmsg_add_u32(b, "mtu", st.mtu);
1012 if (st.flags & DEV_OPT_MTU6)
1013 blobmsg_add_u32(b, "mtu6", st.mtu6);
1014 if (st.flags & DEV_OPT_MACADDR)
1015 blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
1016 if (st.flags & DEV_OPT_TXQUEUELEN)
1017 blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
1018 if (st.flags & DEV_OPT_IPV6)
1019 blobmsg_add_u8(b, "ipv6", st.ipv6);
1020 if (st.flags & DEV_OPT_PROMISC)
1021 blobmsg_add_u8(b, "promisc", st.promisc);
1022 if (st.flags & DEV_OPT_RPFILTER)
1023 blobmsg_add_u32(b, "rpfilter", st.rpfilter);
1024 if (st.flags & DEV_OPT_ACCEPTLOCAL)
1025 blobmsg_add_u8(b, "acceptlocal", st.acceptlocal);
1026 if (st.flags & DEV_OPT_IGMPVERSION)
1027 blobmsg_add_u32(b, "igmpversion", st.igmpversion);
1028 if (st.flags & DEV_OPT_MLDVERSION)
1029 blobmsg_add_u32(b, "mldversion", st.mldversion);
1030 if (st.flags & DEV_OPT_NEIGHREACHABLETIME) {
1031 blobmsg_add_u32(b, "neigh4reachabletime", st.neigh4reachabletime);
1032 blobmsg_add_u32(b, "neigh6reachabletime", st.neigh6reachabletime);
1034 if (st.flags & DEV_OPT_NEIGHGCSTALETIME) {
1035 blobmsg_add_u32(b, "neigh4gcstaletime", st.neigh4gcstaletime);
1036 blobmsg_add_u32(b, "neigh6gcstaletime", st.neigh6gcstaletime);
1038 if (st.flags & DEV_OPT_DADTRANSMITS)
1039 blobmsg_add_u32(b, "dadtransmits", st.dadtransmits);
1040 if (st.flags & DEV_OPT_MULTICAST_TO_UNICAST)
1041 blobmsg_add_u8(b, "multicast_to_unicast", st.multicast_to_unicast);
1042 if (st.flags & DEV_OPT_MULTICAST_ROUTER)
1043 blobmsg_add_u32(b, "multicast_router", st.multicast_router);
1044 if (st.flags & DEV_OPT_MULTICAST)
1045 blobmsg_add_u8(b, "multicast", st.multicast);
1046 if (st.flags & DEV_OPT_LEARNING)
1047 blobmsg_add_u8(b, "learning", st.learning);
1048 if (st.flags & DEV_OPT_UNICAST_FLOOD)
1049 blobmsg_add_u8(b, "unicast_flood", st.unicast_flood);
1052 s = blobmsg_open_table(b, "statistics");
1053 if (dev->type->dump_stats)
1054 dev->type->dump_stats(dev, b);
1056 system_if_dump_stats(dev, b);
1057 blobmsg_close_table(b, s);