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 device_vlan_len(struct kvlist
*kv
, const void *data
)
114 return sizeof(unsigned int);
117 void device_vlan_update(bool done
)
121 avl_for_each_element(&devices
, dev
, avl
) {
122 if (!dev
->vlans
.update
)
126 if (dev
->vlan_aliases
.get_len
)
127 kvlist_free(&dev
->vlan_aliases
);
129 kvlist_init(&dev
->vlan_aliases
, device_vlan_len
);
130 vlist_update(&dev
->vlans
);
132 vlist_flush(&dev
->vlans
);
137 static int set_device_state(struct device
*dev
, bool state
)
140 /* Get ifindex for all devices being enabled so a valid */
141 /* ifindex is in place avoiding possible race conditions */
142 device_set_ifindex(dev
, system_if_resolve(dev
));
155 simple_device_set_state(struct device
*dev
, bool state
)
160 pdev
= dev
->parent
.dev
;
161 if (state
&& !pdev
) {
162 pdev
= system_if_get_parent(dev
);
164 device_add_user(&dev
->parent
, pdev
);
169 ret
= device_claim(&dev
->parent
);
171 device_release(&dev
->parent
);
176 return set_device_state(dev
, state
);
179 static struct device
*
180 simple_device_create(const char *name
, struct device_type
*devtype
,
181 struct blob_attr
*attr
)
183 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
184 struct device
*dev
= NULL
;
186 /* device type is unused for simple devices */
189 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
, blob_data(attr
), blob_len(attr
));
190 dev
= device_get(name
, true);
194 dev
->set_state
= simple_device_set_state
;
195 device_init_settings(dev
, tb
);
200 static void simple_device_free(struct device
*dev
)
203 device_remove_user(&dev
->parent
);
207 struct device_type simple_device_type
= {
208 .name
= "Network device",
209 .config_params
= &device_attr_list
,
211 .create
= simple_device_create
,
212 .check_state
= system_if_check
,
213 .free
= simple_device_free
,
217 device_merge_settings(struct device
*dev
, struct device_settings
*n
)
219 struct device_settings
*os
= &dev
->orig_settings
;
220 struct device_settings
*s
= &dev
->settings
;
222 memset(n
, 0, sizeof(*n
));
223 n
->mtu
= s
->flags
& DEV_OPT_MTU
? s
->mtu
: os
->mtu
;
224 n
->mtu6
= s
->flags
& DEV_OPT_MTU6
? s
->mtu6
: os
->mtu6
;
225 n
->txqueuelen
= s
->flags
& DEV_OPT_TXQUEUELEN
?
226 s
->txqueuelen
: os
->txqueuelen
;
228 (s
->flags
& DEV_OPT_MACADDR
? s
->macaddr
: os
->macaddr
),
230 n
->ipv6
= s
->flags
& DEV_OPT_IPV6
? s
->ipv6
: os
->ipv6
;
231 n
->promisc
= s
->flags
& DEV_OPT_PROMISC
? s
->promisc
: os
->promisc
;
232 n
->rpfilter
= s
->flags
& DEV_OPT_RPFILTER
? s
->rpfilter
: os
->rpfilter
;
233 n
->acceptlocal
= s
->flags
& DEV_OPT_ACCEPTLOCAL
? s
->acceptlocal
: os
->acceptlocal
;
234 n
->igmpversion
= s
->flags
& DEV_OPT_IGMPVERSION
? s
->igmpversion
: os
->igmpversion
;
235 n
->mldversion
= s
->flags
& DEV_OPT_MLDVERSION
? s
->mldversion
: os
->mldversion
;
236 n
->neigh4reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
237 s
->neigh4reachabletime
: os
->neigh4reachabletime
;
238 n
->neigh6reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
239 s
->neigh6reachabletime
: os
->neigh6reachabletime
;
240 n
->neigh4gcstaletime
= s
->flags
& DEV_OPT_NEIGHGCSTALETIME
?
241 s
->neigh4gcstaletime
: os
->neigh4gcstaletime
;
242 n
->neigh6gcstaletime
= s
->flags
& DEV_OPT_NEIGHGCSTALETIME
?
243 s
->neigh6gcstaletime
: os
->neigh6gcstaletime
;
244 n
->neigh4locktime
= s
->flags
& DEV_OPT_NEIGHLOCKTIME
?
245 s
->neigh4locktime
: os
->neigh4locktime
;
246 n
->dadtransmits
= s
->flags
& DEV_OPT_DADTRANSMITS
?
247 s
->dadtransmits
: os
->dadtransmits
;
248 n
->multicast
= s
->flags
& DEV_OPT_MULTICAST
?
249 s
->multicast
: os
->multicast
;
250 n
->multicast_to_unicast
= s
->multicast_to_unicast
;
251 n
->multicast_router
= s
->multicast_router
;
252 n
->multicast_fast_leave
= s
->multicast_fast_leave
;
253 n
->learning
= s
->learning
;
254 n
->unicast_flood
= s
->unicast_flood
;
255 n
->sendredirects
= s
->flags
& DEV_OPT_SENDREDIRECTS
?
256 s
->sendredirects
: os
->sendredirects
;
257 n
->flags
= s
->flags
| os
->flags
| os
->valid_flags
;
261 device_init_settings(struct device
*dev
, struct blob_attr
**tb
)
263 struct device_settings
*s
= &dev
->settings
;
264 struct blob_attr
*cur
;
265 struct ether_addr
*ea
;
266 bool disabled
= false;
269 if ((cur
= tb
[DEV_ATTR_ENABLED
]))
270 disabled
= !blobmsg_get_bool(cur
);
272 if ((cur
= tb
[DEV_ATTR_MTU
]) && blobmsg_get_u32(cur
) >= 68) {
273 s
->mtu
= blobmsg_get_u32(cur
);
274 s
->flags
|= DEV_OPT_MTU
;
277 if ((cur
= tb
[DEV_ATTR_MTU6
]) && blobmsg_get_u32(cur
) >= 1280) {
278 s
->mtu6
= blobmsg_get_u32(cur
);
279 s
->flags
|= DEV_OPT_MTU6
;
282 if ((cur
= tb
[DEV_ATTR_TXQUEUELEN
])) {
283 s
->txqueuelen
= blobmsg_get_u32(cur
);
284 s
->flags
|= DEV_OPT_TXQUEUELEN
;
287 if ((cur
= tb
[DEV_ATTR_MACADDR
])) {
288 ea
= ether_aton(blobmsg_data(cur
));
290 memcpy(s
->macaddr
, ea
, 6);
291 s
->flags
|= DEV_OPT_MACADDR
;
295 if ((cur
= tb
[DEV_ATTR_IPV6
])) {
296 s
->ipv6
= blobmsg_get_bool(cur
);
297 s
->flags
|= DEV_OPT_IPV6
;
300 if ((cur
= tb
[DEV_ATTR_PROMISC
])) {
301 s
->promisc
= blobmsg_get_bool(cur
);
302 s
->flags
|= DEV_OPT_PROMISC
;
305 if ((cur
= tb
[DEV_ATTR_RPFILTER
])) {
306 if (system_resolve_rpfilter(blobmsg_data(cur
), &s
->rpfilter
))
307 s
->flags
|= DEV_OPT_RPFILTER
;
309 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur
));
312 if ((cur
= tb
[DEV_ATTR_ACCEPTLOCAL
])) {
313 s
->acceptlocal
= blobmsg_get_bool(cur
);
314 s
->flags
|= DEV_OPT_ACCEPTLOCAL
;
317 if ((cur
= tb
[DEV_ATTR_IGMPVERSION
])) {
318 s
->igmpversion
= blobmsg_get_u32(cur
);
319 if (s
->igmpversion
>= 1 && s
->igmpversion
<= 3)
320 s
->flags
|= DEV_OPT_IGMPVERSION
;
322 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur
));
325 if ((cur
= tb
[DEV_ATTR_MLDVERSION
])) {
326 s
->mldversion
= blobmsg_get_u32(cur
);
327 if (s
->mldversion
>= 1 && s
->mldversion
<= 2)
328 s
->flags
|= DEV_OPT_MLDVERSION
;
330 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur
));
333 if ((cur
= tb
[DEV_ATTR_NEIGHREACHABLETIME
])) {
334 s
->neigh6reachabletime
= s
->neigh4reachabletime
= blobmsg_get_u32(cur
);
335 s
->flags
|= DEV_OPT_NEIGHREACHABLETIME
;
338 if ((cur
= tb
[DEV_ATTR_NEIGHGCSTALETIME
])) {
339 s
->neigh6gcstaletime
= s
->neigh4gcstaletime
= blobmsg_get_u32(cur
);
340 s
->flags
|= DEV_OPT_NEIGHGCSTALETIME
;
343 if ((cur
= tb
[DEV_ATTR_NEIGHLOCKTIME
])) {
344 s
->neigh4locktime
= blobmsg_get_u32(cur
);
345 s
->flags
|= DEV_OPT_NEIGHLOCKTIME
;
348 if ((cur
= tb
[DEV_ATTR_DADTRANSMITS
])) {
349 s
->dadtransmits
= blobmsg_get_u32(cur
);
350 s
->flags
|= DEV_OPT_DADTRANSMITS
;
353 if ((cur
= tb
[DEV_ATTR_MULTICAST_TO_UNICAST
])) {
354 s
->multicast_to_unicast
= blobmsg_get_bool(cur
);
355 s
->flags
|= DEV_OPT_MULTICAST_TO_UNICAST
;
358 if ((cur
= tb
[DEV_ATTR_MULTICAST_ROUTER
])) {
359 s
->multicast_router
= blobmsg_get_u32(cur
);
360 if (s
->multicast_router
<= 2)
361 s
->flags
|= DEV_OPT_MULTICAST_ROUTER
;
363 DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur
));
366 if ((cur
= tb
[DEV_ATTR_MULTICAST_FAST_LEAVE
])) {
367 s
->multicast_fast_leave
= blobmsg_get_bool(cur
);
368 s
->flags
|= DEV_OPT_MULTICAST_FAST_LEAVE
;
371 if ((cur
= tb
[DEV_ATTR_MULTICAST
])) {
372 s
->multicast
= blobmsg_get_bool(cur
);
373 s
->flags
|= DEV_OPT_MULTICAST
;
376 if ((cur
= tb
[DEV_ATTR_LEARNING
])) {
377 s
->learning
= blobmsg_get_bool(cur
);
378 s
->flags
|= DEV_OPT_LEARNING
;
381 if ((cur
= tb
[DEV_ATTR_UNICAST_FLOOD
])) {
382 s
->unicast_flood
= blobmsg_get_bool(cur
);
383 s
->flags
|= DEV_OPT_UNICAST_FLOOD
;
386 if ((cur
= tb
[DEV_ATTR_SENDREDIRECTS
])) {
387 s
->sendredirects
= blobmsg_get_bool(cur
);
388 s
->flags
|= DEV_OPT_SENDREDIRECTS
;
391 if ((cur
= tb
[DEV_ATTR_ISOLATE
])) {
392 s
->isolate
= blobmsg_get_bool(cur
);
393 s
->flags
|= DEV_OPT_ISOLATE
;
396 device_set_disabled(dev
, disabled
);
399 static void __init
dev_init(void)
401 avl_init(&devices
, avl_strcmp
, true, NULL
);
404 static int device_broadcast_cb(void *ctx
, struct safe_list
*list
)
406 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
409 /* device might have been removed by an earlier callback */
418 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
422 safe_list_for_each(&dev
->aliases
, device_broadcast_cb
, &dev_ev
);
423 safe_list_for_each(&dev
->users
, device_broadcast_cb
, &dev_ev
);
426 int device_claim(struct device_user
*dep
)
428 struct device
*dev
= dep
->dev
;
438 D(DEVICE
, "Claim %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
439 if (++dev
->active
!= 1)
442 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
444 /* Get ifindex for external claimed devices so a valid */
445 /* ifindex is in place avoiding possible race conditions */
446 device_set_ifindex(dev
, system_if_resolve(dev
));
450 system_if_get_settings(dev
, &dev
->orig_settings
);
452 ret
= dev
->set_state(dev
, true);
455 device_broadcast_event(dev
, DEV_EVENT_UP
);
457 D(DEVICE
, "claim %s %s failed: %d\n", dev
->type
->name
, dev
->ifname
, ret
);
459 dep
->claimed
= false;
465 void device_release(struct device_user
*dep
)
467 struct device
*dev
= dep
->dev
;
472 dep
->claimed
= false;
474 D(DEVICE
, "Release %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
475 assert(dev
->active
>= 0);
480 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
482 dev
->set_state(dev
, false);
487 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
490 int device_check_state(struct device
*dev
)
492 if (!dev
->type
->check_state
)
493 return simple_device_type
.check_state(dev
);
495 return dev
->type
->check_state(dev
);
498 int device_init_virtual(struct device
*dev
, struct device_type
*type
, const char *name
)
503 D(DEVICE
, "Initialize device '%s'\n", name
? name
: "");
504 INIT_SAFE_LIST(&dev
->users
);
505 INIT_SAFE_LIST(&dev
->aliases
);
511 ret
= device_set_ifname(dev
, name
);
517 dev
->set_state
= set_device_state
;
522 int device_init(struct device
*dev
, struct device_type
*type
, const char *ifname
)
526 ret
= device_init_virtual(dev
, type
, ifname
);
530 dev
->avl
.key
= dev
->ifname
;
532 ret
= avl_insert(&devices
, &dev
->avl
);
536 system_if_clear_state(dev
);
541 static struct device
*
542 device_create_default(const char *name
, bool external
)
546 if (!external
&& system_if_force_external(name
))
549 D(DEVICE
, "Create simple device '%s'\n", name
);
550 dev
= calloc(1, sizeof(*dev
));
554 dev
->external
= external
;
555 dev
->set_state
= simple_device_set_state
;
557 if (device_init(dev
, &simple_device_type
, name
) < 0) {
563 dev
->default_config
= true;
565 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
567 device_check_state(dev
);
573 device_find(const char *name
)
577 return avl_find_element(&devices
, name
, dev
, avl
);
581 device_get(const char *name
, int create
)
585 dev
= avl_find_element(&devices
, name
, dev
, avl
);
587 if (!dev
&& strchr(name
, '.'))
588 return get_vlan_device_chain(name
, create
);
591 return device_alias_get(name
+ 1);
594 if (create
> 1 && !dev
->external
) {
595 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
596 dev
->external
= true;
597 device_set_present(dev
, true);
605 return device_create_default(name
, create
> 1);
609 device_delete(struct device
*dev
)
614 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
615 avl_delete(&devices
, &dev
->avl
);
619 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
621 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
623 dep
->cb(dep
, DEV_EVENT_REMOVE
);
629 void device_cleanup(struct device
*dev
)
631 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
632 safe_list_for_each(&dev
->users
, device_cleanup_cb
, NULL
);
633 safe_list_for_each(&dev
->aliases
, device_cleanup_cb
, NULL
);
637 static void __device_set_present(struct device
*dev
, bool state
)
639 if (dev
->present
== state
)
642 dev
->present
= state
;
643 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
647 device_refresh_present(struct device
*dev
)
649 bool state
= dev
->sys_present
;
651 if (dev
->disabled
|| dev
->deferred
)
654 __device_set_present(dev
, state
);
657 void device_set_present(struct device
*dev
, bool state
)
659 if (dev
->sys_present
== state
)
662 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
663 dev
->sys_present
= state
;
664 device_refresh_present(dev
);
667 void device_set_link(struct device
*dev
, bool state
)
669 if (dev
->link_active
== state
)
672 netifd_log_message(L_NOTICE
, "%s '%s' link is %s\n", dev
->type
->name
, dev
->ifname
, state
? "up" : "down" );
674 dev
->link_active
= state
;
675 device_broadcast_event(dev
, state
? DEV_EVENT_LINK_UP
: DEV_EVENT_LINK_DOWN
);
678 void device_set_ifindex(struct device
*dev
, int ifindex
)
680 if (dev
->ifindex
== ifindex
)
683 dev
->ifindex
= ifindex
;
684 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFINDEX
);
687 int device_set_ifname(struct device
*dev
, const char *name
)
691 if (!strcmp(dev
->ifname
, name
))
694 if (strlen(name
) > sizeof(dev
->ifname
) - 1)
698 avl_delete(&devices
, &dev
->avl
);
700 strcpy(dev
->ifname
, name
);
703 ret
= avl_insert(&devices
, &dev
->avl
);
706 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFNAME
);
711 static int device_refcount(struct device
*dev
)
713 struct list_head
*list
;
716 list_for_each(list
, &dev
->users
.list
)
719 list_for_each(list
, &dev
->aliases
.list
)
726 __device_add_user(struct device_user
*dep
, struct device
*dev
)
728 struct safe_list
*head
;
733 head
= &dev
->aliases
;
737 safe_list_add(&dep
->list
, head
);
738 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
740 if (dep
->cb
&& dev
->present
) {
741 dep
->cb(dep
, DEV_EVENT_ADD
);
743 dep
->cb(dep
, DEV_EVENT_UP
);
745 if (dev
->link_active
)
746 dep
->cb(dep
, DEV_EVENT_LINK_UP
);
750 void device_add_user(struct device_user
*dep
, struct device
*dev
)
756 device_remove_user(dep
);
761 __device_add_user(dep
, dev
);
765 device_free(struct device
*dev
)
770 dev
->type
->free(dev
);
775 __device_free_unused(struct device
*dev
)
777 if (!safe_list_empty(&dev
->users
) ||
778 !safe_list_empty(&dev
->aliases
) ||
779 dev
->current_config
|| __devlock
)
785 void device_remove_user(struct device_user
*dep
)
787 struct device
*dev
= dep
->dev
;
792 dep
->hotplug
= false;
796 safe_list_del(&dep
->list
);
798 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
799 __device_free_unused(dev
);
803 device_free_unused(struct device
*dev
)
808 return __device_free_unused(dev
);
810 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
811 __device_free_unused(dev
);
815 device_init_pending(void)
817 struct device
*dev
, *tmp
;
819 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
820 if (!dev
->config_pending
)
823 dev
->type
->config_init(dev
);
824 dev
->config_pending
= false;
825 device_check_state(dev
);
829 static enum dev_change_type
830 device_set_config(struct device
*dev
, struct device_type
*type
,
831 struct blob_attr
*attr
)
833 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
834 const struct uci_blob_param_list
*cfg
= type
->config_params
;
836 if (type
!= dev
->type
)
837 return DEV_CONFIG_RECREATE
;
839 if (dev
->type
->reload
)
840 return dev
->type
->reload(dev
, attr
);
842 if (uci_blob_check_equal(dev
->config
, attr
, cfg
))
843 return DEV_CONFIG_NO_CHANGE
;
845 if (cfg
== &device_attr_list
) {
846 memset(tb
, 0, sizeof(tb
));
849 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
850 blob_data(attr
), blob_len(attr
));
852 device_init_settings(dev
, tb
);
853 return DEV_CONFIG_RESTART
;
855 return DEV_CONFIG_RECREATE
;
859 device_apply_config(struct device
*dev
, struct device_type
*type
,
860 struct blob_attr
*config
)
862 enum dev_change_type change
;
864 change
= device_set_config(dev
, type
, config
);
866 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
867 change
= DEV_CONFIG_APPLIED
;
871 case DEV_CONFIG_RESTART
:
872 case DEV_CONFIG_APPLIED
:
873 D(DEVICE
, "Device '%s': config applied\n", dev
->ifname
);
874 config
= blob_memdup(config
);
876 dev
->config
= config
;
877 if (change
== DEV_CONFIG_RESTART
&& dev
->present
) {
880 device_set_present(dev
, false);
881 if (dev
->active
&& !dev
->external
) {
882 ret
= dev
->set_state(dev
, false);
884 ret
= dev
->set_state(dev
, true);
887 device_set_present(dev
, true);
890 case DEV_CONFIG_NO_CHANGE
:
891 D(DEVICE
, "Device '%s': no configuration change\n", dev
->ifname
);
893 case DEV_CONFIG_RECREATE
:
901 device_replace(struct device
*dev
, struct device
*odev
)
903 struct device_user
*dep
, *tmp
;
907 device_set_present(odev
, false);
909 list_for_each_entry_safe(dep
, tmp
, &odev
->users
.list
, list
.list
) {
911 safe_list_del(&dep
->list
);
912 __device_add_user(dep
, dev
);
920 device_reset_config(void)
924 avl_for_each_element(&devices
, dev
, avl
)
925 dev
->current_config
= false;
929 device_reset_old(void)
931 struct device
*dev
, *tmp
, *ndev
;
933 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
934 if (dev
->current_config
|| dev
->default_config
)
937 if (dev
->type
!= &simple_device_type
)
940 ndev
= device_create_default(dev
->ifname
, dev
->external
);
944 device_replace(ndev
, dev
);
949 device_create(const char *name
, struct device_type
*type
,
950 struct blob_attr
*config
)
952 struct device
*odev
= NULL
, *dev
;
953 enum dev_change_type change
;
955 odev
= device_find(name
);
957 odev
->current_config
= true;
958 change
= device_apply_config(odev
, type
, config
);
960 case DEV_CONFIG_RECREATE
:
961 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
968 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
970 config
= blob_memdup(config
);
974 dev
= type
->create(name
, type
, config
);
978 dev
->current_config
= true;
979 dev
->config
= config
;
981 device_replace(dev
, odev
);
983 if (!config_init
&& dev
->config_pending
) {
984 type
->config_init(dev
);
985 dev
->config_pending
= false;
988 device_check_state(dev
);
994 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
996 struct device_settings st
;
1000 avl_for_each_element(&devices
, dev
, avl
) {
1003 c
= blobmsg_open_table(b
, dev
->ifname
);
1004 device_dump_status(b
, dev
);
1005 blobmsg_close_table(b
, c
);
1011 blobmsg_add_u8(b
, "external", dev
->external
);
1012 blobmsg_add_u8(b
, "present", dev
->present
);
1013 blobmsg_add_string(b
, "type", dev
->type
->name
);
1018 blobmsg_add_u8(b
, "up", !!dev
->active
);
1019 blobmsg_add_u8(b
, "carrier", !!dev
->link_active
);
1021 if (dev
->type
->dump_info
)
1022 dev
->type
->dump_info(dev
, b
);
1024 system_if_dump_info(dev
, b
);
1027 device_merge_settings(dev
, &st
);
1028 if (st
.flags
& DEV_OPT_MTU
)
1029 blobmsg_add_u32(b
, "mtu", st
.mtu
);
1030 if (st
.flags
& DEV_OPT_MTU6
)
1031 blobmsg_add_u32(b
, "mtu6", st
.mtu6
);
1032 if (st
.flags
& DEV_OPT_MACADDR
)
1033 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
1034 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
1035 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
1036 if (st
.flags
& DEV_OPT_IPV6
)
1037 blobmsg_add_u8(b
, "ipv6", st
.ipv6
);
1038 if (st
.flags
& DEV_OPT_PROMISC
)
1039 blobmsg_add_u8(b
, "promisc", st
.promisc
);
1040 if (st
.flags
& DEV_OPT_RPFILTER
)
1041 blobmsg_add_u32(b
, "rpfilter", st
.rpfilter
);
1042 if (st
.flags
& DEV_OPT_ACCEPTLOCAL
)
1043 blobmsg_add_u8(b
, "acceptlocal", st
.acceptlocal
);
1044 if (st
.flags
& DEV_OPT_IGMPVERSION
)
1045 blobmsg_add_u32(b
, "igmpversion", st
.igmpversion
);
1046 if (st
.flags
& DEV_OPT_MLDVERSION
)
1047 blobmsg_add_u32(b
, "mldversion", st
.mldversion
);
1048 if (st
.flags
& DEV_OPT_NEIGHREACHABLETIME
) {
1049 blobmsg_add_u32(b
, "neigh4reachabletime", st
.neigh4reachabletime
);
1050 blobmsg_add_u32(b
, "neigh6reachabletime", st
.neigh6reachabletime
);
1052 if (st
.flags
& DEV_OPT_NEIGHGCSTALETIME
) {
1053 blobmsg_add_u32(b
, "neigh4gcstaletime", st
.neigh4gcstaletime
);
1054 blobmsg_add_u32(b
, "neigh6gcstaletime", st
.neigh6gcstaletime
);
1056 if (st
.flags
& DEV_OPT_NEIGHLOCKTIME
)
1057 blobmsg_add_u32(b
, "neigh4locktime", st
.neigh4locktime
);
1058 if (st
.flags
& DEV_OPT_DADTRANSMITS
)
1059 blobmsg_add_u32(b
, "dadtransmits", st
.dadtransmits
);
1060 if (st
.flags
& DEV_OPT_MULTICAST_TO_UNICAST
)
1061 blobmsg_add_u8(b
, "multicast_to_unicast", st
.multicast_to_unicast
);
1062 if (st
.flags
& DEV_OPT_MULTICAST_ROUTER
)
1063 blobmsg_add_u32(b
, "multicast_router", st
.multicast_router
);
1064 if (st
.flags
& DEV_OPT_MULTICAST_FAST_LEAVE
)
1065 blobmsg_add_u8(b
, "multicast_fast_leave", st
.multicast_fast_leave
);
1066 if (st
.flags
& DEV_OPT_MULTICAST
)
1067 blobmsg_add_u8(b
, "multicast", st
.multicast
);
1068 if (st
.flags
& DEV_OPT_LEARNING
)
1069 blobmsg_add_u8(b
, "learning", st
.learning
);
1070 if (st
.flags
& DEV_OPT_UNICAST_FLOOD
)
1071 blobmsg_add_u8(b
, "unicast_flood", st
.unicast_flood
);
1072 if (st
.flags
& DEV_OPT_SENDREDIRECTS
)
1073 blobmsg_add_u8(b
, "sendredirects", st
.sendredirects
);
1076 s
= blobmsg_open_table(b
, "statistics");
1077 if (dev
->type
->dump_stats
)
1078 dev
->type
->dump_stats(dev
, b
);
1080 system_if_dump_stats(dev
, b
);
1081 blobmsg_close_table(b
, s
);
1084 static void __init
simple_device_type_init(void)
1086 device_type_add(&simple_device_type
);