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
;
36 static const struct blobmsg_policy dev_attrs
[__DEV_ATTR_MAX
] = {
37 [DEV_ATTR_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
38 [DEV_ATTR_MTU
] = { .name
= "mtu", .type
= BLOBMSG_TYPE_INT32
},
39 [DEV_ATTR_MTU6
] = { .name
= "mtu6", .type
= BLOBMSG_TYPE_INT32
},
40 [DEV_ATTR_MACADDR
] = { .name
= "macaddr", .type
= BLOBMSG_TYPE_STRING
},
41 [DEV_ATTR_TXQUEUELEN
] = { .name
= "txqueuelen", .type
= BLOBMSG_TYPE_INT32
},
42 [DEV_ATTR_ENABLED
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_BOOL
},
43 [DEV_ATTR_IPV6
] = { .name
= "ipv6", .type
= BLOBMSG_TYPE_BOOL
},
44 [DEV_ATTR_PROMISC
] = { .name
= "promisc", .type
= BLOBMSG_TYPE_BOOL
},
45 [DEV_ATTR_RPFILTER
] = { .name
= "rpfilter", .type
= BLOBMSG_TYPE_STRING
},
46 [DEV_ATTR_ACCEPTLOCAL
] = { .name
= "acceptlocal", .type
= BLOBMSG_TYPE_BOOL
},
47 [DEV_ATTR_IGMPVERSION
] = { .name
= "igmpversion", .type
= BLOBMSG_TYPE_INT32
},
48 [DEV_ATTR_MLDVERSION
] = { .name
= "mldversion", .type
= BLOBMSG_TYPE_INT32
},
49 [DEV_ATTR_NEIGHREACHABLETIME
] = { .name
= "neighreachabletime", .type
= BLOBMSG_TYPE_INT32
},
50 [DEV_ATTR_NEIGHGCSTALETIME
] = { .name
= "neighgcstaletime", .type
= BLOBMSG_TYPE_INT32
},
51 [DEV_ATTR_DADTRANSMITS
] = { .name
= "dadtransmits", .type
= BLOBMSG_TYPE_INT32
},
52 [DEV_ATTR_MULTICAST_TO_UNICAST
] = { .name
= "multicast_to_unicast", .type
= BLOBMSG_TYPE_BOOL
},
53 [DEV_ATTR_MULTICAST_ROUTER
] = { .name
= "multicast_router", .type
= BLOBMSG_TYPE_INT32
},
54 [DEV_ATTR_MULTICAST_FAST_LEAVE
] = { .name
= "multicast_fast_leave", . type
= BLOBMSG_TYPE_BOOL
},
55 [DEV_ATTR_MULTICAST
] = { .name
="multicast", .type
= BLOBMSG_TYPE_BOOL
},
56 [DEV_ATTR_LEARNING
] = { .name
="learning", .type
= BLOBMSG_TYPE_BOOL
},
57 [DEV_ATTR_UNICAST_FLOOD
] = { .name
="unicast_flood", .type
= BLOBMSG_TYPE_BOOL
},
58 [DEV_ATTR_SENDREDIRECTS
] = { .name
= "sendredirects", .type
= BLOBMSG_TYPE_BOOL
},
59 [DEV_ATTR_NEIGHLOCKTIME
] = { .name
= "neighlocktime", .type
= BLOBMSG_TYPE_INT32
},
60 [DEV_ATTR_ISOLATE
] = { .name
= "isolate", .type
= BLOBMSG_TYPE_BOOL
},
63 const struct uci_blob_param_list device_attr_list
= {
64 .n_params
= __DEV_ATTR_MAX
,
68 static int __devlock
= 0;
70 int device_type_add(struct device_type
*devtype
)
72 if (device_type_get(devtype
->name
)) {
73 netifd_log_message(L_WARNING
, "Device handler '%s' already exists\n",
78 netifd_log_message(L_NOTICE
, "Added device handler type: %s\n",
81 list_add(&devtype
->list
, &devtypes
);
85 /* Retrieve the device type for the given name. If 'bridge' is true, the type
86 * must have bridge capabilities
89 device_type_get(const char *tname
)
91 struct device_type
*cur
;
93 list_for_each_entry(cur
, &devtypes
, list
)
94 if (!strcmp(cur
->name
, tname
))
100 void device_lock(void)
105 void device_unlock(void)
109 device_free_unused(NULL
);
112 static int set_device_state(struct device
*dev
, bool state
)
115 /* Get ifindex for all devices being enabled so a valid */
116 /* ifindex is in place avoiding possible race conditions */
117 device_set_ifindex(dev
, system_if_resolve(dev
));
130 simple_device_set_state(struct device
*dev
, bool state
)
135 pdev
= dev
->parent
.dev
;
136 if (state
&& !pdev
) {
137 pdev
= system_if_get_parent(dev
);
139 device_add_user(&dev
->parent
, pdev
);
144 ret
= device_claim(&dev
->parent
);
146 device_release(&dev
->parent
);
151 return set_device_state(dev
, state
);
154 static struct device
*
155 simple_device_create(const char *name
, struct device_type
*devtype
,
156 struct blob_attr
*attr
)
158 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
159 struct device
*dev
= NULL
;
161 /* device type is unused for simple devices */
164 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
, blob_data(attr
), blob_len(attr
));
165 dev
= device_get(name
, true);
169 dev
->set_state
= simple_device_set_state
;
170 device_init_settings(dev
, tb
);
175 static void simple_device_free(struct device
*dev
)
178 device_remove_user(&dev
->parent
);
182 struct device_type simple_device_type
= {
183 .name
= "Network device",
184 .config_params
= &device_attr_list
,
186 .create
= simple_device_create
,
187 .check_state
= system_if_check
,
188 .free
= simple_device_free
,
192 device_merge_settings(struct device
*dev
, struct device_settings
*n
)
194 struct device_settings
*os
= &dev
->orig_settings
;
195 struct device_settings
*s
= &dev
->settings
;
197 memset(n
, 0, sizeof(*n
));
198 n
->mtu
= s
->flags
& DEV_OPT_MTU
? s
->mtu
: os
->mtu
;
199 n
->mtu6
= s
->flags
& DEV_OPT_MTU6
? s
->mtu6
: os
->mtu6
;
200 n
->txqueuelen
= s
->flags
& DEV_OPT_TXQUEUELEN
?
201 s
->txqueuelen
: os
->txqueuelen
;
203 (s
->flags
& DEV_OPT_MACADDR
? s
->macaddr
: os
->macaddr
),
205 n
->ipv6
= s
->flags
& DEV_OPT_IPV6
? s
->ipv6
: os
->ipv6
;
206 n
->promisc
= s
->flags
& DEV_OPT_PROMISC
? s
->promisc
: os
->promisc
;
207 n
->rpfilter
= s
->flags
& DEV_OPT_RPFILTER
? s
->rpfilter
: os
->rpfilter
;
208 n
->acceptlocal
= s
->flags
& DEV_OPT_ACCEPTLOCAL
? s
->acceptlocal
: os
->acceptlocal
;
209 n
->igmpversion
= s
->flags
& DEV_OPT_IGMPVERSION
? s
->igmpversion
: os
->igmpversion
;
210 n
->mldversion
= s
->flags
& DEV_OPT_MLDVERSION
? s
->mldversion
: os
->mldversion
;
211 n
->neigh4reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
212 s
->neigh4reachabletime
: os
->neigh4reachabletime
;
213 n
->neigh6reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
214 s
->neigh6reachabletime
: os
->neigh6reachabletime
;
215 n
->neigh4gcstaletime
= s
->flags
& DEV_OPT_NEIGHGCSTALETIME
?
216 s
->neigh4gcstaletime
: os
->neigh4gcstaletime
;
217 n
->neigh6gcstaletime
= s
->flags
& DEV_OPT_NEIGHGCSTALETIME
?
218 s
->neigh6gcstaletime
: os
->neigh6gcstaletime
;
219 n
->neigh4locktime
= s
->flags
& DEV_OPT_NEIGHLOCKTIME
?
220 s
->neigh4locktime
: os
->neigh4locktime
;
221 n
->dadtransmits
= s
->flags
& DEV_OPT_DADTRANSMITS
?
222 s
->dadtransmits
: os
->dadtransmits
;
223 n
->multicast
= s
->flags
& DEV_OPT_MULTICAST
?
224 s
->multicast
: os
->multicast
;
225 n
->multicast_to_unicast
= s
->multicast_to_unicast
;
226 n
->multicast_router
= s
->multicast_router
;
227 n
->multicast_fast_leave
= s
->multicast_fast_leave
;
228 n
->learning
= s
->learning
;
229 n
->unicast_flood
= s
->unicast_flood
;
230 n
->sendredirects
= s
->flags
& DEV_OPT_SENDREDIRECTS
?
231 s
->sendredirects
: os
->sendredirects
;
232 n
->flags
= s
->flags
| os
->flags
| os
->valid_flags
;
236 device_init_settings(struct device
*dev
, struct blob_attr
**tb
)
238 struct device_settings
*s
= &dev
->settings
;
239 struct blob_attr
*cur
;
240 struct ether_addr
*ea
;
241 bool disabled
= false;
244 if ((cur
= tb
[DEV_ATTR_ENABLED
]))
245 disabled
= !blobmsg_get_bool(cur
);
247 if ((cur
= tb
[DEV_ATTR_MTU
]) && blobmsg_get_u32(cur
) >= 68) {
248 s
->mtu
= blobmsg_get_u32(cur
);
249 s
->flags
|= DEV_OPT_MTU
;
252 if ((cur
= tb
[DEV_ATTR_MTU6
]) && blobmsg_get_u32(cur
) >= 1280) {
253 s
->mtu6
= blobmsg_get_u32(cur
);
254 s
->flags
|= DEV_OPT_MTU6
;
257 if ((cur
= tb
[DEV_ATTR_TXQUEUELEN
])) {
258 s
->txqueuelen
= blobmsg_get_u32(cur
);
259 s
->flags
|= DEV_OPT_TXQUEUELEN
;
262 if ((cur
= tb
[DEV_ATTR_MACADDR
])) {
263 ea
= ether_aton(blobmsg_data(cur
));
265 memcpy(s
->macaddr
, ea
, 6);
266 s
->flags
|= DEV_OPT_MACADDR
;
270 if ((cur
= tb
[DEV_ATTR_IPV6
])) {
271 s
->ipv6
= blobmsg_get_bool(cur
);
272 s
->flags
|= DEV_OPT_IPV6
;
275 if ((cur
= tb
[DEV_ATTR_PROMISC
])) {
276 s
->promisc
= blobmsg_get_bool(cur
);
277 s
->flags
|= DEV_OPT_PROMISC
;
280 if ((cur
= tb
[DEV_ATTR_RPFILTER
])) {
281 if (system_resolve_rpfilter(blobmsg_data(cur
), &s
->rpfilter
))
282 s
->flags
|= DEV_OPT_RPFILTER
;
284 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur
));
287 if ((cur
= tb
[DEV_ATTR_ACCEPTLOCAL
])) {
288 s
->acceptlocal
= blobmsg_get_bool(cur
);
289 s
->flags
|= DEV_OPT_ACCEPTLOCAL
;
292 if ((cur
= tb
[DEV_ATTR_IGMPVERSION
])) {
293 s
->igmpversion
= blobmsg_get_u32(cur
);
294 if (s
->igmpversion
>= 1 && s
->igmpversion
<= 3)
295 s
->flags
|= DEV_OPT_IGMPVERSION
;
297 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur
));
300 if ((cur
= tb
[DEV_ATTR_MLDVERSION
])) {
301 s
->mldversion
= blobmsg_get_u32(cur
);
302 if (s
->mldversion
>= 1 && s
->mldversion
<= 2)
303 s
->flags
|= DEV_OPT_MLDVERSION
;
305 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur
));
308 if ((cur
= tb
[DEV_ATTR_NEIGHREACHABLETIME
])) {
309 s
->neigh6reachabletime
= s
->neigh4reachabletime
= blobmsg_get_u32(cur
);
310 s
->flags
|= DEV_OPT_NEIGHREACHABLETIME
;
313 if ((cur
= tb
[DEV_ATTR_NEIGHGCSTALETIME
])) {
314 s
->neigh6gcstaletime
= s
->neigh4gcstaletime
= blobmsg_get_u32(cur
);
315 s
->flags
|= DEV_OPT_NEIGHGCSTALETIME
;
318 if ((cur
= tb
[DEV_ATTR_NEIGHLOCKTIME
])) {
319 s
->neigh4locktime
= blobmsg_get_u32(cur
);
320 s
->flags
|= DEV_OPT_NEIGHLOCKTIME
;
323 if ((cur
= tb
[DEV_ATTR_DADTRANSMITS
])) {
324 s
->dadtransmits
= blobmsg_get_u32(cur
);
325 s
->flags
|= DEV_OPT_DADTRANSMITS
;
328 if ((cur
= tb
[DEV_ATTR_MULTICAST_TO_UNICAST
])) {
329 s
->multicast_to_unicast
= blobmsg_get_bool(cur
);
330 s
->flags
|= DEV_OPT_MULTICAST_TO_UNICAST
;
333 if ((cur
= tb
[DEV_ATTR_MULTICAST_ROUTER
])) {
334 s
->multicast_router
= blobmsg_get_u32(cur
);
335 if (s
->multicast_router
<= 2)
336 s
->flags
|= DEV_OPT_MULTICAST_ROUTER
;
338 DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur
));
341 if ((cur
= tb
[DEV_ATTR_MULTICAST_FAST_LEAVE
])) {
342 s
->multicast_fast_leave
= blobmsg_get_bool(cur
);
343 s
->flags
|= DEV_OPT_MULTICAST_FAST_LEAVE
;
346 if ((cur
= tb
[DEV_ATTR_MULTICAST
])) {
347 s
->multicast
= blobmsg_get_bool(cur
);
348 s
->flags
|= DEV_OPT_MULTICAST
;
351 if ((cur
= tb
[DEV_ATTR_LEARNING
])) {
352 s
->learning
= blobmsg_get_bool(cur
);
353 s
->flags
|= DEV_OPT_LEARNING
;
356 if ((cur
= tb
[DEV_ATTR_UNICAST_FLOOD
])) {
357 s
->unicast_flood
= blobmsg_get_bool(cur
);
358 s
->flags
|= DEV_OPT_UNICAST_FLOOD
;
361 if ((cur
= tb
[DEV_ATTR_SENDREDIRECTS
])) {
362 s
->sendredirects
= blobmsg_get_bool(cur
);
363 s
->flags
|= DEV_OPT_SENDREDIRECTS
;
366 if ((cur
= tb
[DEV_ATTR_ISOLATE
])) {
367 s
->isolate
= blobmsg_get_bool(cur
);
368 s
->flags
|= DEV_OPT_ISOLATE
;
371 device_set_disabled(dev
, disabled
);
374 static void __init
dev_init(void)
376 avl_init(&devices
, avl_strcmp
, true, NULL
);
379 static int device_broadcast_cb(void *ctx
, struct safe_list
*list
)
381 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
384 /* device might have been removed by an earlier callback */
393 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
397 safe_list_for_each(&dev
->aliases
, device_broadcast_cb
, &dev_ev
);
398 safe_list_for_each(&dev
->users
, device_broadcast_cb
, &dev_ev
);
401 int device_claim(struct device_user
*dep
)
403 struct device
*dev
= dep
->dev
;
413 D(DEVICE
, "Claim %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
414 if (++dev
->active
!= 1)
417 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
419 /* Get ifindex for external claimed devices so a valid */
420 /* ifindex is in place avoiding possible race conditions */
421 device_set_ifindex(dev
, system_if_resolve(dev
));
425 system_if_get_settings(dev
, &dev
->orig_settings
);
427 ret
= dev
->set_state(dev
, true);
430 device_broadcast_event(dev
, DEV_EVENT_UP
);
432 D(DEVICE
, "claim %s %s failed: %d\n", dev
->type
->name
, dev
->ifname
, ret
);
434 dep
->claimed
= false;
440 void device_release(struct device_user
*dep
)
442 struct device
*dev
= dep
->dev
;
447 dep
->claimed
= false;
449 D(DEVICE
, "Release %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
450 assert(dev
->active
>= 0);
455 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
457 dev
->set_state(dev
, false);
462 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
465 int device_check_state(struct device
*dev
)
467 if (!dev
->type
->check_state
)
468 return simple_device_type
.check_state(dev
);
470 return dev
->type
->check_state(dev
);
473 int device_init_virtual(struct device
*dev
, struct device_type
*type
, const char *name
)
478 D(DEVICE
, "Initialize device '%s'\n", name
? name
: "");
479 INIT_SAFE_LIST(&dev
->users
);
480 INIT_SAFE_LIST(&dev
->aliases
);
486 ret
= device_set_ifname(dev
, name
);
492 dev
->set_state
= set_device_state
;
497 int device_init(struct device
*dev
, struct device_type
*type
, const char *ifname
)
501 ret
= device_init_virtual(dev
, type
, ifname
);
505 dev
->avl
.key
= dev
->ifname
;
507 ret
= avl_insert(&devices
, &dev
->avl
);
511 system_if_clear_state(dev
);
516 static struct device
*
517 device_create_default(const char *name
, bool external
)
521 if (!external
&& system_if_force_external(name
))
524 D(DEVICE
, "Create simple device '%s'\n", name
);
525 dev
= calloc(1, sizeof(*dev
));
529 dev
->external
= external
;
530 dev
->set_state
= simple_device_set_state
;
532 if (device_init(dev
, &simple_device_type
, name
) < 0) {
538 dev
->default_config
= true;
540 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
542 device_check_state(dev
);
548 device_find(const char *name
)
552 return avl_find_element(&devices
, name
, dev
, avl
);
556 device_get(const char *name
, int create
)
560 if (strchr(name
, '.'))
561 return get_vlan_device_chain(name
, create
);
564 return device_alias_get(name
+ 1);
566 dev
= avl_find_element(&devices
, name
, dev
, avl
);
568 if (create
> 1 && !dev
->external
) {
569 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
570 dev
->external
= true;
571 device_set_present(dev
, true);
579 return device_create_default(name
, create
> 1);
583 device_delete(struct device
*dev
)
588 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
589 avl_delete(&devices
, &dev
->avl
);
593 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
595 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
597 dep
->cb(dep
, DEV_EVENT_REMOVE
);
603 void device_cleanup(struct device
*dev
)
605 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
606 safe_list_for_each(&dev
->users
, device_cleanup_cb
, NULL
);
607 safe_list_for_each(&dev
->aliases
, device_cleanup_cb
, NULL
);
611 static void __device_set_present(struct device
*dev
, bool state
)
613 if (dev
->present
== state
)
616 dev
->present
= state
;
617 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
621 device_refresh_present(struct device
*dev
)
623 bool state
= dev
->sys_present
;
625 if (dev
->disabled
|| dev
->deferred
)
628 __device_set_present(dev
, state
);
631 void device_set_present(struct device
*dev
, bool state
)
633 if (dev
->sys_present
== state
)
636 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
637 dev
->sys_present
= state
;
638 device_refresh_present(dev
);
641 void device_set_link(struct device
*dev
, bool state
)
643 if (dev
->link_active
== state
)
646 netifd_log_message(L_NOTICE
, "%s '%s' link is %s\n", dev
->type
->name
, dev
->ifname
, state
? "up" : "down" );
648 dev
->link_active
= state
;
649 device_broadcast_event(dev
, state
? DEV_EVENT_LINK_UP
: DEV_EVENT_LINK_DOWN
);
652 void device_set_ifindex(struct device
*dev
, int ifindex
)
654 if (dev
->ifindex
== ifindex
)
657 dev
->ifindex
= ifindex
;
658 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFINDEX
);
661 int device_set_ifname(struct device
*dev
, const char *name
)
665 if (!strcmp(dev
->ifname
, name
))
668 if (strlen(name
) > sizeof(dev
->ifname
) - 1)
672 avl_delete(&devices
, &dev
->avl
);
674 strcpy(dev
->ifname
, name
);
677 ret
= avl_insert(&devices
, &dev
->avl
);
680 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFNAME
);
685 static int device_refcount(struct device
*dev
)
687 struct list_head
*list
;
690 list_for_each(list
, &dev
->users
.list
)
693 list_for_each(list
, &dev
->aliases
.list
)
700 __device_add_user(struct device_user
*dep
, struct device
*dev
)
702 struct safe_list
*head
;
707 head
= &dev
->aliases
;
711 safe_list_add(&dep
->list
, head
);
712 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
714 if (dep
->cb
&& dev
->present
) {
715 dep
->cb(dep
, DEV_EVENT_ADD
);
717 dep
->cb(dep
, DEV_EVENT_UP
);
719 if (dev
->link_active
)
720 dep
->cb(dep
, DEV_EVENT_LINK_UP
);
724 void device_add_user(struct device_user
*dep
, struct device
*dev
)
730 device_remove_user(dep
);
735 __device_add_user(dep
, dev
);
739 device_free(struct device
*dev
)
744 dev
->type
->free(dev
);
749 __device_free_unused(struct device
*dev
)
751 if (!safe_list_empty(&dev
->users
) ||
752 !safe_list_empty(&dev
->aliases
) ||
753 dev
->current_config
|| __devlock
)
759 void device_remove_user(struct device_user
*dep
)
761 struct device
*dev
= dep
->dev
;
766 dep
->hotplug
= false;
770 safe_list_del(&dep
->list
);
772 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
773 __device_free_unused(dev
);
777 device_free_unused(struct device
*dev
)
782 return __device_free_unused(dev
);
784 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
785 __device_free_unused(dev
);
789 device_init_pending(void)
791 struct device
*dev
, *tmp
;
793 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
794 if (!dev
->config_pending
)
797 dev
->type
->config_init(dev
);
798 dev
->config_pending
= false;
799 device_check_state(dev
);
803 static enum dev_change_type
804 device_set_config(struct device
*dev
, struct device_type
*type
,
805 struct blob_attr
*attr
)
807 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
808 const struct uci_blob_param_list
*cfg
= type
->config_params
;
810 if (type
!= dev
->type
)
811 return DEV_CONFIG_RECREATE
;
813 if (dev
->type
->reload
)
814 return dev
->type
->reload(dev
, attr
);
816 if (uci_blob_check_equal(dev
->config
, attr
, cfg
))
817 return DEV_CONFIG_NO_CHANGE
;
819 if (cfg
== &device_attr_list
) {
820 memset(tb
, 0, sizeof(tb
));
823 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
824 blob_data(attr
), blob_len(attr
));
826 device_init_settings(dev
, tb
);
827 return DEV_CONFIG_RESTART
;
829 return DEV_CONFIG_RECREATE
;
833 device_apply_config(struct device
*dev
, struct device_type
*type
,
834 struct blob_attr
*config
)
836 enum dev_change_type change
;
838 change
= device_set_config(dev
, type
, config
);
840 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
841 change
= DEV_CONFIG_APPLIED
;
845 case DEV_CONFIG_RESTART
:
846 case DEV_CONFIG_APPLIED
:
847 D(DEVICE
, "Device '%s': config applied\n", dev
->ifname
);
848 config
= blob_memdup(config
);
850 dev
->config
= config
;
851 if (change
== DEV_CONFIG_RESTART
&& dev
->present
) {
854 device_set_present(dev
, false);
855 if (dev
->active
&& !dev
->external
) {
856 ret
= dev
->set_state(dev
, false);
858 ret
= dev
->set_state(dev
, true);
861 device_set_present(dev
, true);
864 case DEV_CONFIG_NO_CHANGE
:
865 D(DEVICE
, "Device '%s': no configuration change\n", dev
->ifname
);
867 case DEV_CONFIG_RECREATE
:
875 device_replace(struct device
*dev
, struct device
*odev
)
877 struct device_user
*dep
, *tmp
;
881 device_set_present(odev
, false);
883 list_for_each_entry_safe(dep
, tmp
, &odev
->users
.list
, list
.list
) {
885 safe_list_del(&dep
->list
);
886 __device_add_user(dep
, dev
);
894 device_reset_config(void)
898 avl_for_each_element(&devices
, dev
, avl
)
899 dev
->current_config
= false;
903 device_reset_old(void)
905 struct device
*dev
, *tmp
, *ndev
;
907 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
908 if (dev
->current_config
|| dev
->default_config
)
911 if (dev
->type
!= &simple_device_type
)
914 ndev
= device_create_default(dev
->ifname
, dev
->external
);
918 device_replace(ndev
, dev
);
923 device_create(const char *name
, struct device_type
*type
,
924 struct blob_attr
*config
)
926 struct device
*odev
= NULL
, *dev
;
927 enum dev_change_type change
;
929 odev
= device_find(name
);
931 odev
->current_config
= true;
932 change
= device_apply_config(odev
, type
, config
);
934 case DEV_CONFIG_RECREATE
:
935 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
942 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
944 config
= blob_memdup(config
);
948 dev
= type
->create(name
, type
, config
);
952 dev
->current_config
= true;
953 dev
->config
= config
;
955 device_replace(dev
, odev
);
957 if (!config_init
&& dev
->config_pending
) {
958 type
->config_init(dev
);
959 dev
->config_pending
= false;
962 device_check_state(dev
);
968 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
970 struct device_settings st
;
974 avl_for_each_element(&devices
, dev
, avl
) {
977 c
= blobmsg_open_table(b
, dev
->ifname
);
978 device_dump_status(b
, dev
);
979 blobmsg_close_table(b
, c
);
985 blobmsg_add_u8(b
, "external", dev
->external
);
986 blobmsg_add_u8(b
, "present", dev
->present
);
987 blobmsg_add_string(b
, "type", dev
->type
->name
);
992 blobmsg_add_u8(b
, "up", !!dev
->active
);
993 blobmsg_add_u8(b
, "carrier", !!dev
->link_active
);
995 if (dev
->type
->dump_info
)
996 dev
->type
->dump_info(dev
, b
);
998 system_if_dump_info(dev
, b
);
1001 device_merge_settings(dev
, &st
);
1002 if (st
.flags
& DEV_OPT_MTU
)
1003 blobmsg_add_u32(b
, "mtu", st
.mtu
);
1004 if (st
.flags
& DEV_OPT_MTU6
)
1005 blobmsg_add_u32(b
, "mtu6", st
.mtu6
);
1006 if (st
.flags
& DEV_OPT_MACADDR
)
1007 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
1008 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
1009 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
1010 if (st
.flags
& DEV_OPT_IPV6
)
1011 blobmsg_add_u8(b
, "ipv6", st
.ipv6
);
1012 if (st
.flags
& DEV_OPT_PROMISC
)
1013 blobmsg_add_u8(b
, "promisc", st
.promisc
);
1014 if (st
.flags
& DEV_OPT_RPFILTER
)
1015 blobmsg_add_u32(b
, "rpfilter", st
.rpfilter
);
1016 if (st
.flags
& DEV_OPT_ACCEPTLOCAL
)
1017 blobmsg_add_u8(b
, "acceptlocal", st
.acceptlocal
);
1018 if (st
.flags
& DEV_OPT_IGMPVERSION
)
1019 blobmsg_add_u32(b
, "igmpversion", st
.igmpversion
);
1020 if (st
.flags
& DEV_OPT_MLDVERSION
)
1021 blobmsg_add_u32(b
, "mldversion", st
.mldversion
);
1022 if (st
.flags
& DEV_OPT_NEIGHREACHABLETIME
) {
1023 blobmsg_add_u32(b
, "neigh4reachabletime", st
.neigh4reachabletime
);
1024 blobmsg_add_u32(b
, "neigh6reachabletime", st
.neigh6reachabletime
);
1026 if (st
.flags
& DEV_OPT_NEIGHGCSTALETIME
) {
1027 blobmsg_add_u32(b
, "neigh4gcstaletime", st
.neigh4gcstaletime
);
1028 blobmsg_add_u32(b
, "neigh6gcstaletime", st
.neigh6gcstaletime
);
1030 if (st
.flags
& DEV_OPT_NEIGHLOCKTIME
)
1031 blobmsg_add_u32(b
, "neigh4locktime", st
.neigh4locktime
);
1032 if (st
.flags
& DEV_OPT_DADTRANSMITS
)
1033 blobmsg_add_u32(b
, "dadtransmits", st
.dadtransmits
);
1034 if (st
.flags
& DEV_OPT_MULTICAST_TO_UNICAST
)
1035 blobmsg_add_u8(b
, "multicast_to_unicast", st
.multicast_to_unicast
);
1036 if (st
.flags
& DEV_OPT_MULTICAST_ROUTER
)
1037 blobmsg_add_u32(b
, "multicast_router", st
.multicast_router
);
1038 if (st
.flags
& DEV_OPT_MULTICAST_FAST_LEAVE
)
1039 blobmsg_add_u8(b
, "multicast_fast_leave", st
.multicast_fast_leave
);
1040 if (st
.flags
& DEV_OPT_MULTICAST
)
1041 blobmsg_add_u8(b
, "multicast", st
.multicast
);
1042 if (st
.flags
& DEV_OPT_LEARNING
)
1043 blobmsg_add_u8(b
, "learning", st
.learning
);
1044 if (st
.flags
& DEV_OPT_UNICAST_FLOOD
)
1045 blobmsg_add_u8(b
, "unicast_flood", st
.unicast_flood
);
1046 if (st
.flags
& DEV_OPT_SENDREDIRECTS
)
1047 blobmsg_add_u8(b
, "sendredirects", st
.sendredirects
);
1050 s
= blobmsg_open_table(b
, "statistics");
1051 if (dev
->type
->dump_stats
)
1052 dev
->type
->dump_stats(dev
, b
);
1054 system_if_dump_stats(dev
, b
);
1055 blobmsg_close_table(b
, s
);
1058 static void __init
simple_device_type_init(void)
1060 device_type_add(&simple_device_type
);