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>
31 static struct avl_tree devices
;
32 static bool default_ps
= true;
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 [DEV_ATTR_MULTICAST
] = { .name
="multicast", .type
= BLOBMSG_TYPE_BOOL
},
54 [DEV_ATTR_LEARNING
] = { .name
="learning", .type
= BLOBMSG_TYPE_BOOL
},
55 [DEV_ATTR_UNICAST_FLOOD
] = { .name
="unicast_flood", .type
= BLOBMSG_TYPE_BOOL
},
58 const struct uci_blob_param_list device_attr_list
= {
59 .n_params
= __DEV_ATTR_MAX
,
63 static int __devlock
= 0;
65 void device_lock(void)
70 void device_unlock(void)
74 device_free_unused(NULL
);
77 static int set_device_state(struct device
*dev
, bool state
)
80 /* Get ifindex for all devices being enabled so a valid */
81 /* ifindex is in place avoiding possible race conditions */
82 device_set_ifindex(dev
, system_if_resolve(dev
));
95 simple_device_set_state(struct device
*dev
, bool state
)
100 pdev
= dev
->parent
.dev
;
101 if (state
&& !pdev
) {
102 pdev
= system_if_get_parent(dev
);
104 device_add_user(&dev
->parent
, pdev
);
109 ret
= device_claim(&dev
->parent
);
111 device_release(&dev
->parent
);
116 return set_device_state(dev
, state
);
119 static struct device
*
120 simple_device_create(const char *name
, struct blob_attr
*attr
)
122 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
123 struct device
*dev
= NULL
;
125 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
, blob_data(attr
), blob_len(attr
));
126 dev
= device_get(name
, true);
130 dev
->set_state
= simple_device_set_state
;
131 device_init_settings(dev
, tb
);
136 static void simple_device_free(struct device
*dev
)
139 device_remove_user(&dev
->parent
);
143 const struct device_type simple_device_type
= {
144 .name
= "Network device",
145 .config_params
= &device_attr_list
,
147 .create
= simple_device_create
,
148 .check_state
= system_if_check
,
149 .free
= simple_device_free
,
153 device_merge_settings(struct device
*dev
, struct device_settings
*n
)
155 struct device_settings
*os
= &dev
->orig_settings
;
156 struct device_settings
*s
= &dev
->settings
;
158 memset(n
, 0, sizeof(*n
));
159 n
->mtu
= s
->flags
& DEV_OPT_MTU
? s
->mtu
: os
->mtu
;
160 n
->mtu6
= s
->flags
& DEV_OPT_MTU6
? s
->mtu6
: os
->mtu6
;
161 n
->txqueuelen
= s
->flags
& DEV_OPT_TXQUEUELEN
?
162 s
->txqueuelen
: os
->txqueuelen
;
164 (s
->flags
& DEV_OPT_MACADDR
? s
->macaddr
: os
->macaddr
),
166 n
->ipv6
= s
->flags
& DEV_OPT_IPV6
? s
->ipv6
: os
->ipv6
;
167 n
->promisc
= s
->flags
& DEV_OPT_PROMISC
? s
->promisc
: os
->promisc
;
168 n
->rpfilter
= s
->flags
& DEV_OPT_RPFILTER
? s
->rpfilter
: os
->rpfilter
;
169 n
->acceptlocal
= s
->flags
& DEV_OPT_ACCEPTLOCAL
? s
->acceptlocal
: os
->acceptlocal
;
170 n
->igmpversion
= s
->flags
& DEV_OPT_IGMPVERSION
? s
->igmpversion
: os
->igmpversion
;
171 n
->mldversion
= s
->flags
& DEV_OPT_MLDVERSION
? s
->mldversion
: os
->mldversion
;
172 n
->neigh4reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
173 s
->neigh4reachabletime
: os
->neigh4reachabletime
;
174 n
->neigh6reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
175 s
->neigh6reachabletime
: os
->neigh6reachabletime
;
176 n
->dadtransmits
= s
->flags
& DEV_OPT_DADTRANSMITS
?
177 s
->dadtransmits
: os
->dadtransmits
;
178 n
->multicast
= s
->flags
& DEV_OPT_MULTICAST
?
179 s
->multicast
: os
->multicast
;
180 n
->multicast_to_unicast
= s
->multicast_to_unicast
;
181 n
->multicast_router
= s
->multicast_router
;
182 n
->learning
= s
->learning
;
183 n
->unicast_flood
= s
->unicast_flood
;
184 n
->flags
= s
->flags
| os
->flags
| os
->valid_flags
;
188 device_init_settings(struct device
*dev
, struct blob_attr
**tb
)
190 struct device_settings
*s
= &dev
->settings
;
191 struct blob_attr
*cur
;
192 struct ether_addr
*ea
;
193 bool disabled
= false;
196 if ((cur
= tb
[DEV_ATTR_ENABLED
]))
197 disabled
= !blobmsg_get_bool(cur
);
199 if ((cur
= tb
[DEV_ATTR_MTU
])) {
200 s
->mtu
= blobmsg_get_u32(cur
);
201 s
->flags
|= DEV_OPT_MTU
;
204 if ((cur
= tb
[DEV_ATTR_MTU6
])) {
205 s
->mtu6
= blobmsg_get_u32(cur
);
206 s
->flags
|= DEV_OPT_MTU6
;
209 if ((cur
= tb
[DEV_ATTR_TXQUEUELEN
])) {
210 s
->txqueuelen
= blobmsg_get_u32(cur
);
211 s
->flags
|= DEV_OPT_TXQUEUELEN
;
214 if ((cur
= tb
[DEV_ATTR_MACADDR
])) {
215 ea
= ether_aton(blobmsg_data(cur
));
217 memcpy(s
->macaddr
, ea
, 6);
218 s
->flags
|= DEV_OPT_MACADDR
;
222 if ((cur
= tb
[DEV_ATTR_IPV6
])) {
223 s
->ipv6
= blobmsg_get_bool(cur
);
224 s
->flags
|= DEV_OPT_IPV6
;
227 if ((cur
= tb
[DEV_ATTR_PROMISC
])) {
228 s
->promisc
= blobmsg_get_bool(cur
);
229 s
->flags
|= DEV_OPT_PROMISC
;
232 if ((cur
= tb
[DEV_ATTR_RPFILTER
])) {
233 if (system_resolve_rpfilter(blobmsg_data(cur
), &s
->rpfilter
))
234 s
->flags
|= DEV_OPT_RPFILTER
;
236 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur
));
239 if ((cur
= tb
[DEV_ATTR_ACCEPTLOCAL
])) {
240 s
->acceptlocal
= blobmsg_get_bool(cur
);
241 s
->flags
|= DEV_OPT_ACCEPTLOCAL
;
244 if ((cur
= tb
[DEV_ATTR_IGMPVERSION
])) {
245 s
->igmpversion
= blobmsg_get_u32(cur
);
246 if (s
->igmpversion
>= 1 && s
->igmpversion
<= 3)
247 s
->flags
|= DEV_OPT_IGMPVERSION
;
249 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur
));
252 if ((cur
= tb
[DEV_ATTR_MLDVERSION
])) {
253 s
->mldversion
= blobmsg_get_u32(cur
);
254 if (s
->mldversion
>= 1 && s
->mldversion
<= 2)
255 s
->flags
|= DEV_OPT_MLDVERSION
;
257 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur
));
260 if ((cur
= tb
[DEV_ATTR_NEIGHREACHABLETIME
])) {
261 s
->neigh6reachabletime
= s
->neigh4reachabletime
= blobmsg_get_u32(cur
);
262 s
->flags
|= DEV_OPT_NEIGHREACHABLETIME
;
265 if ((cur
= tb
[DEV_ATTR_RPS
])) {
266 s
->rps
= blobmsg_get_bool(cur
);
267 s
->flags
|= DEV_OPT_RPS
;
272 if ((cur
= tb
[DEV_ATTR_XPS
])) {
273 s
->xps
= blobmsg_get_bool(cur
);
274 s
->flags
|= DEV_OPT_XPS
;
279 if ((cur
= tb
[DEV_ATTR_DADTRANSMITS
])) {
280 s
->dadtransmits
= blobmsg_get_u32(cur
);
281 s
->flags
|= DEV_OPT_DADTRANSMITS
;
284 if ((cur
= tb
[DEV_ATTR_MULTICAST_TO_UNICAST
])) {
285 s
->multicast_to_unicast
= blobmsg_get_bool(cur
);
286 s
->flags
|= DEV_OPT_MULTICAST_TO_UNICAST
;
289 if ((cur
= tb
[DEV_ATTR_MULTICAST_ROUTER
])) {
290 s
->multicast_router
= blobmsg_get_u32(cur
);
291 if (s
->multicast_router
<= 2)
292 s
->flags
|= DEV_OPT_MULTICAST_ROUTER
;
294 DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur
));
297 if ((cur
= tb
[DEV_ATTR_MULTICAST
])) {
298 s
->multicast
= blobmsg_get_bool(cur
);
299 s
->flags
|= DEV_OPT_MULTICAST
;
302 if ((cur
= tb
[DEV_ATTR_LEARNING
])) {
303 s
->learning
= blobmsg_get_bool(cur
);
304 s
->flags
|= DEV_OPT_LEARNING
;
307 if ((cur
= tb
[DEV_ATTR_UNICAST_FLOOD
])) {
308 s
->unicast_flood
= blobmsg_get_bool(cur
);
309 s
->flags
|= DEV_OPT_UNICAST_FLOOD
;
312 device_set_disabled(dev
, disabled
);
315 static void __init
dev_init(void)
317 avl_init(&devices
, avl_strcmp
, true, NULL
);
320 static int device_broadcast_cb(void *ctx
, struct safe_list
*list
)
322 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
325 /* device might have been removed by an earlier callback */
334 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
338 safe_list_for_each(&dev
->aliases
, device_broadcast_cb
, &dev_ev
);
339 safe_list_for_each(&dev
->users
, device_broadcast_cb
, &dev_ev
);
342 int device_claim(struct device_user
*dep
)
344 struct device
*dev
= dep
->dev
;
354 D(DEVICE
, "Claim %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
355 if (++dev
->active
!= 1)
358 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
360 /* Get ifindex for external claimed devices so a valid */
361 /* ifindex is in place avoiding possible race conditions */
362 device_set_ifindex(dev
, system_if_resolve(dev
));
366 system_if_get_settings(dev
, &dev
->orig_settings
);
368 ret
= dev
->set_state(dev
, true);
371 device_broadcast_event(dev
, DEV_EVENT_UP
);
373 D(DEVICE
, "claim %s %s failed: %d\n", dev
->type
->name
, dev
->ifname
, ret
);
375 dep
->claimed
= false;
381 void device_release(struct device_user
*dep
)
383 struct device
*dev
= dep
->dev
;
388 dep
->claimed
= false;
390 D(DEVICE
, "Release %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
391 assert(dev
->active
>= 0);
396 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
398 dev
->set_state(dev
, false);
399 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
402 int device_check_state(struct device
*dev
)
404 if (!dev
->type
->check_state
)
405 return simple_device_type
.check_state(dev
);
407 return dev
->type
->check_state(dev
);
410 void device_init_virtual(struct device
*dev
, const struct device_type
*type
, const char *name
)
416 strncpy(dev
->ifname
, name
, IFNAMSIZ
);
418 D(DEVICE
, "Initialize device '%s'\n", dev
->ifname
);
419 INIT_SAFE_LIST(&dev
->users
);
420 INIT_SAFE_LIST(&dev
->aliases
);
424 dev
->set_state
= set_device_state
;
427 int device_init(struct device
*dev
, const struct device_type
*type
, const char *ifname
)
431 device_init_virtual(dev
, type
, ifname
);
433 dev
->avl
.key
= dev
->ifname
;
435 ret
= avl_insert(&devices
, &dev
->avl
);
439 system_if_clear_state(dev
);
440 device_check_state(dev
);
441 dev
->settings
.rps
= default_ps
;
442 dev
->settings
.xps
= default_ps
;
447 static struct device
*
448 device_create_default(const char *name
, bool external
)
452 if (!external
&& system_if_force_external(name
))
455 D(DEVICE
, "Create simple device '%s'\n", name
);
456 dev
= calloc(1, sizeof(*dev
));
460 dev
->external
= external
;
461 dev
->set_state
= simple_device_set_state
;
462 device_init(dev
, &simple_device_type
, name
);
463 dev
->default_config
= true;
465 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
470 device_get(const char *name
, int create
)
474 if (strchr(name
, '.'))
475 return get_vlan_device_chain(name
, create
);
478 return device_alias_get(name
+ 1);
480 dev
= avl_find_element(&devices
, name
, dev
, avl
);
482 if (create
> 1 && !dev
->external
) {
483 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
484 dev
->external
= true;
485 device_set_present(dev
, true);
493 return device_create_default(name
, create
> 1);
497 device_delete(struct device
*dev
)
502 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
503 avl_delete(&devices
, &dev
->avl
);
507 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
509 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
511 dep
->cb(dep
, DEV_EVENT_REMOVE
);
517 void device_cleanup(struct device
*dev
)
519 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
520 safe_list_for_each(&dev
->users
, device_cleanup_cb
, NULL
);
521 safe_list_for_each(&dev
->aliases
, device_cleanup_cb
, NULL
);
525 static void __device_set_present(struct device
*dev
, bool state
)
527 if (dev
->present
== state
)
530 dev
->present
= state
;
531 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
535 device_refresh_present(struct device
*dev
)
537 bool state
= dev
->sys_present
;
539 if (dev
->disabled
|| dev
->deferred
)
542 __device_set_present(dev
, state
);
545 void device_set_present(struct device
*dev
, bool state
)
547 if (dev
->sys_present
== state
)
550 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
551 dev
->sys_present
= state
;
552 device_refresh_present(dev
);
555 void device_set_link(struct device
*dev
, bool state
)
557 if (dev
->link_active
== state
)
560 netifd_log_message(L_NOTICE
, "%s '%s' link is %s\n", dev
->type
->name
, dev
->ifname
, state
? "up" : "down" );
562 dev
->link_active
= state
;
563 device_broadcast_event(dev
, state
? DEV_EVENT_LINK_UP
: DEV_EVENT_LINK_DOWN
);
566 void device_set_ifindex(struct device
*dev
, int ifindex
)
568 if (dev
->ifindex
== ifindex
)
571 dev
->ifindex
= ifindex
;
572 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFINDEX
);
575 static int device_refcount(struct device
*dev
)
577 struct list_head
*list
;
580 list_for_each(list
, &dev
->users
.list
)
583 list_for_each(list
, &dev
->aliases
.list
)
590 __device_add_user(struct device_user
*dep
, struct device
*dev
)
592 struct safe_list
*head
;
597 head
= &dev
->aliases
;
601 safe_list_add(&dep
->list
, head
);
602 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
604 if (dep
->cb
&& dev
->present
) {
605 dep
->cb(dep
, DEV_EVENT_ADD
);
607 dep
->cb(dep
, DEV_EVENT_UP
);
609 if (dev
->link_active
)
610 dep
->cb(dep
, DEV_EVENT_LINK_UP
);
614 void device_add_user(struct device_user
*dep
, struct device
*dev
)
620 device_remove_user(dep
);
625 __device_add_user(dep
, dev
);
629 device_free(struct device
*dev
)
634 dev
->type
->free(dev
);
639 __device_free_unused(struct device
*dev
)
641 if (!safe_list_empty(&dev
->users
) ||
642 !safe_list_empty(&dev
->aliases
) ||
643 dev
->current_config
|| __devlock
)
649 void device_remove_user(struct device_user
*dep
)
651 struct device
*dev
= dep
->dev
;
656 dep
->hotplug
= false;
660 safe_list_del(&dep
->list
);
662 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
663 __device_free_unused(dev
);
667 device_free_unused(struct device
*dev
)
672 return __device_free_unused(dev
);
674 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
675 __device_free_unused(dev
);
679 device_init_pending(void)
681 struct device
*dev
, *tmp
;
683 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
684 if (!dev
->config_pending
)
687 dev
->type
->config_init(dev
);
688 dev
->config_pending
= false;
692 static enum dev_change_type
693 device_set_config(struct device
*dev
, const struct device_type
*type
,
694 struct blob_attr
*attr
)
696 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
697 const struct uci_blob_param_list
*cfg
= type
->config_params
;
699 if (type
!= dev
->type
)
700 return DEV_CONFIG_RECREATE
;
702 if (dev
->type
->reload
)
703 return dev
->type
->reload(dev
, attr
);
705 if (uci_blob_check_equal(dev
->config
, attr
, cfg
))
706 return DEV_CONFIG_NO_CHANGE
;
708 if (cfg
== &device_attr_list
) {
709 memset(tb
, 0, sizeof(tb
));
712 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
713 blob_data(attr
), blob_len(attr
));
715 device_init_settings(dev
, tb
);
716 return DEV_CONFIG_RESTART
;
718 return DEV_CONFIG_RECREATE
;
722 device_apply_config(struct device
*dev
, const struct device_type
*type
,
723 struct blob_attr
*config
)
725 enum dev_change_type change
;
727 change
= device_set_config(dev
, type
, config
);
729 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
730 change
= DEV_CONFIG_APPLIED
;
734 case DEV_CONFIG_RESTART
:
735 case DEV_CONFIG_APPLIED
:
736 D(DEVICE
, "Device '%s': config applied\n", dev
->ifname
);
737 config
= blob_memdup(config
);
739 dev
->config
= config
;
740 if (change
== DEV_CONFIG_RESTART
&& dev
->present
) {
743 device_set_present(dev
, false);
744 if (dev
->active
&& !dev
->external
) {
745 ret
= dev
->set_state(dev
, false);
747 ret
= dev
->set_state(dev
, true);
750 device_set_present(dev
, true);
753 case DEV_CONFIG_NO_CHANGE
:
754 D(DEVICE
, "Device '%s': no configuration change\n", dev
->ifname
);
756 case DEV_CONFIG_RECREATE
:
764 device_replace(struct device
*dev
, struct device
*odev
)
766 struct device_user
*dep
, *tmp
;
770 device_set_present(odev
, false);
772 list_for_each_entry_safe(dep
, tmp
, &odev
->users
.list
, list
.list
) {
774 safe_list_del(&dep
->list
);
775 __device_add_user(dep
, dev
);
783 device_reset_config(void)
787 avl_for_each_element(&devices
, dev
, avl
)
788 dev
->current_config
= false;
792 device_reset_old(void)
794 struct device
*dev
, *tmp
, *ndev
;
796 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
797 if (dev
->current_config
|| dev
->default_config
)
800 if (dev
->type
!= &simple_device_type
)
803 ndev
= device_create_default(dev
->ifname
, dev
->external
);
807 device_replace(ndev
, dev
);
812 device_set_default_ps(bool state
)
816 if (state
== default_ps
)
821 avl_for_each_element(&devices
, dev
, avl
) {
822 struct device_settings
*s
= &dev
->settings
;
823 unsigned int apply_mask
= 0;
825 if (!(s
->flags
& DEV_OPT_RPS
)) {
827 apply_mask
|= DEV_OPT_RPS
;
830 if (!(s
->flags
& DEV_OPT_XPS
)) {
832 apply_mask
|= DEV_OPT_XPS
;
838 if (!(dev
->external
|| (dev
->present
&& dev
->active
)) ||
842 system_if_apply_settings(dev
, s
, apply_mask
);
847 device_create(const char *name
, const struct device_type
*type
,
848 struct blob_attr
*config
)
850 struct device
*odev
= NULL
, *dev
;
851 enum dev_change_type change
;
853 odev
= device_get(name
, false);
855 odev
->current_config
= true;
856 change
= device_apply_config(odev
, type
, config
);
858 case DEV_CONFIG_RECREATE
:
859 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
866 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
868 config
= blob_memdup(config
);
872 dev
= type
->create(name
, config
);
876 dev
->current_config
= true;
877 dev
->config
= config
;
879 device_replace(dev
, odev
);
881 if (!config_init
&& dev
->config_pending
) {
882 type
->config_init(dev
);
883 dev
->config_pending
= false;
890 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
892 struct device_settings st
;
896 avl_for_each_element(&devices
, dev
, avl
) {
899 c
= blobmsg_open_table(b
, dev
->ifname
);
900 device_dump_status(b
, dev
);
901 blobmsg_close_table(b
, c
);
907 blobmsg_add_u8(b
, "external", dev
->external
);
908 blobmsg_add_u8(b
, "present", dev
->present
);
909 blobmsg_add_string(b
, "type", dev
->type
->name
);
914 blobmsg_add_u8(b
, "up", !!dev
->active
);
915 blobmsg_add_u8(b
, "carrier", !!dev
->link_active
);
917 if (dev
->type
->dump_info
)
918 dev
->type
->dump_info(dev
, b
);
920 system_if_dump_info(dev
, b
);
923 device_merge_settings(dev
, &st
);
924 if (st
.flags
& DEV_OPT_MTU
)
925 blobmsg_add_u32(b
, "mtu", st
.mtu
);
926 if (st
.flags
& DEV_OPT_MTU6
)
927 blobmsg_add_u32(b
, "mtu6", st
.mtu6
);
928 if (st
.flags
& DEV_OPT_MACADDR
)
929 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
930 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
931 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
932 if (st
.flags
& DEV_OPT_IPV6
)
933 blobmsg_add_u8(b
, "ipv6", st
.ipv6
);
934 if (st
.flags
& DEV_OPT_PROMISC
)
935 blobmsg_add_u8(b
, "promisc", st
.promisc
);
936 if (st
.flags
& DEV_OPT_RPFILTER
)
937 blobmsg_add_u32(b
, "rpfilter", st
.rpfilter
);
938 if (st
.flags
& DEV_OPT_ACCEPTLOCAL
)
939 blobmsg_add_u8(b
, "acceptlocal", st
.acceptlocal
);
940 if (st
.flags
& DEV_OPT_IGMPVERSION
)
941 blobmsg_add_u32(b
, "igmpversion", st
.igmpversion
);
942 if (st
.flags
& DEV_OPT_MLDVERSION
)
943 blobmsg_add_u32(b
, "mldversion", st
.mldversion
);
944 if (st
.flags
& DEV_OPT_NEIGHREACHABLETIME
) {
945 blobmsg_add_u32(b
, "neigh4reachabletime", st
.neigh4reachabletime
);
946 blobmsg_add_u32(b
, "neigh6reachabletime", st
.neigh6reachabletime
);
948 if (st
.flags
& DEV_OPT_DADTRANSMITS
)
949 blobmsg_add_u32(b
, "dadtransmits", st
.dadtransmits
);
950 if (st
.flags
& DEV_OPT_MULTICAST_TO_UNICAST
)
951 blobmsg_add_u8(b
, "multicast_to_unicast", st
.multicast_to_unicast
);
952 if (st
.flags
& DEV_OPT_MULTICAST_ROUTER
)
953 blobmsg_add_u32(b
, "multicast_router", st
.multicast_router
);
954 if (st
.flags
& DEV_OPT_MULTICAST
)
955 blobmsg_add_u8(b
, "multicast", st
.multicast
);
956 if (st
.flags
& DEV_OPT_LEARNING
)
957 blobmsg_add_u8(b
, "learning", st
.learning
);
958 if (st
.flags
& DEV_OPT_UNICAST_FLOOD
)
959 blobmsg_add_u8(b
, "unicast_flood", st
.unicast_flood
);
962 s
= blobmsg_open_table(b
, "statistics");
963 if (dev
->type
->dump_stats
)
964 dev
->type
->dump_stats(dev
, b
);
966 system_if_dump_stats(dev
, b
);
967 blobmsg_close_table(b
, s
);