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
));
146 system_if_get_settings(dev
, &dev
->orig_settings
);
147 /* Only keep orig settings based on what needs to be set */
148 dev
->orig_settings
.valid_flags
= dev
->orig_settings
.flags
;
149 dev
->orig_settings
.flags
&= dev
->settings
.flags
;
150 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
155 system_if_apply_settings(dev
, &dev
->orig_settings
, dev
->orig_settings
.flags
);
162 simple_device_set_state(struct device
*dev
, bool state
)
167 pdev
= dev
->parent
.dev
;
168 if (state
&& !pdev
) {
169 pdev
= system_if_get_parent(dev
);
171 device_add_user(&dev
->parent
, pdev
);
176 ret
= device_claim(&dev
->parent
);
178 device_release(&dev
->parent
);
183 return set_device_state(dev
, state
);
186 static struct device
*
187 simple_device_create(const char *name
, struct device_type
*devtype
,
188 struct blob_attr
*attr
)
190 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
191 struct device
*dev
= NULL
;
193 /* device type is unused for simple devices */
196 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
, blob_data(attr
), blob_len(attr
));
197 dev
= device_get(name
, true);
201 dev
->set_state
= simple_device_set_state
;
202 device_init_settings(dev
, tb
);
207 static void simple_device_free(struct device
*dev
)
210 device_remove_user(&dev
->parent
);
214 struct device_type simple_device_type
= {
215 .name
= "Network device",
216 .config_params
= &device_attr_list
,
218 .create
= simple_device_create
,
219 .check_state
= system_if_check
,
220 .free
= simple_device_free
,
224 device_merge_settings(struct device
*dev
, struct device_settings
*n
)
226 struct device_settings
*os
= &dev
->orig_settings
;
227 struct device_settings
*s
= &dev
->settings
;
229 memset(n
, 0, sizeof(*n
));
230 n
->mtu
= s
->flags
& DEV_OPT_MTU
? s
->mtu
: os
->mtu
;
231 n
->mtu6
= s
->flags
& DEV_OPT_MTU6
? s
->mtu6
: os
->mtu6
;
232 n
->txqueuelen
= s
->flags
& DEV_OPT_TXQUEUELEN
?
233 s
->txqueuelen
: os
->txqueuelen
;
235 (s
->flags
& DEV_OPT_MACADDR
? s
->macaddr
: os
->macaddr
),
237 n
->ipv6
= s
->flags
& DEV_OPT_IPV6
? s
->ipv6
: os
->ipv6
;
238 n
->promisc
= s
->flags
& DEV_OPT_PROMISC
? s
->promisc
: os
->promisc
;
239 n
->rpfilter
= s
->flags
& DEV_OPT_RPFILTER
? s
->rpfilter
: os
->rpfilter
;
240 n
->acceptlocal
= s
->flags
& DEV_OPT_ACCEPTLOCAL
? s
->acceptlocal
: os
->acceptlocal
;
241 n
->igmpversion
= s
->flags
& DEV_OPT_IGMPVERSION
? s
->igmpversion
: os
->igmpversion
;
242 n
->mldversion
= s
->flags
& DEV_OPT_MLDVERSION
? s
->mldversion
: os
->mldversion
;
243 n
->neigh4reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
244 s
->neigh4reachabletime
: os
->neigh4reachabletime
;
245 n
->neigh6reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
246 s
->neigh6reachabletime
: os
->neigh6reachabletime
;
247 n
->neigh4gcstaletime
= s
->flags
& DEV_OPT_NEIGHGCSTALETIME
?
248 s
->neigh4gcstaletime
: os
->neigh4gcstaletime
;
249 n
->neigh6gcstaletime
= s
->flags
& DEV_OPT_NEIGHGCSTALETIME
?
250 s
->neigh6gcstaletime
: os
->neigh6gcstaletime
;
251 n
->neigh4locktime
= s
->flags
& DEV_OPT_NEIGHLOCKTIME
?
252 s
->neigh4locktime
: os
->neigh4locktime
;
253 n
->dadtransmits
= s
->flags
& DEV_OPT_DADTRANSMITS
?
254 s
->dadtransmits
: os
->dadtransmits
;
255 n
->multicast
= s
->flags
& DEV_OPT_MULTICAST
?
256 s
->multicast
: os
->multicast
;
257 n
->multicast_to_unicast
= s
->multicast_to_unicast
;
258 n
->multicast_router
= s
->multicast_router
;
259 n
->multicast_fast_leave
= s
->multicast_fast_leave
;
260 n
->learning
= s
->learning
;
261 n
->unicast_flood
= s
->unicast_flood
;
262 n
->sendredirects
= s
->flags
& DEV_OPT_SENDREDIRECTS
?
263 s
->sendredirects
: os
->sendredirects
;
264 n
->flags
= s
->flags
| os
->flags
| os
->valid_flags
;
268 device_init_settings(struct device
*dev
, struct blob_attr
**tb
)
270 struct device_settings
*s
= &dev
->settings
;
271 struct blob_attr
*cur
;
272 struct ether_addr
*ea
;
273 bool disabled
= false;
276 if ((cur
= tb
[DEV_ATTR_ENABLED
]))
277 disabled
= !blobmsg_get_bool(cur
);
279 if ((cur
= tb
[DEV_ATTR_MTU
]) && blobmsg_get_u32(cur
) >= 68) {
280 s
->mtu
= blobmsg_get_u32(cur
);
281 s
->flags
|= DEV_OPT_MTU
;
284 if ((cur
= tb
[DEV_ATTR_MTU6
]) && blobmsg_get_u32(cur
) >= 1280) {
285 s
->mtu6
= blobmsg_get_u32(cur
);
286 s
->flags
|= DEV_OPT_MTU6
;
289 if ((cur
= tb
[DEV_ATTR_TXQUEUELEN
])) {
290 s
->txqueuelen
= blobmsg_get_u32(cur
);
291 s
->flags
|= DEV_OPT_TXQUEUELEN
;
294 if ((cur
= tb
[DEV_ATTR_MACADDR
])) {
295 ea
= ether_aton(blobmsg_data(cur
));
297 memcpy(s
->macaddr
, ea
, 6);
298 s
->flags
|= DEV_OPT_MACADDR
;
302 if ((cur
= tb
[DEV_ATTR_IPV6
])) {
303 s
->ipv6
= blobmsg_get_bool(cur
);
304 s
->flags
|= DEV_OPT_IPV6
;
307 if ((cur
= tb
[DEV_ATTR_PROMISC
])) {
308 s
->promisc
= blobmsg_get_bool(cur
);
309 s
->flags
|= DEV_OPT_PROMISC
;
312 if ((cur
= tb
[DEV_ATTR_RPFILTER
])) {
313 if (system_resolve_rpfilter(blobmsg_data(cur
), &s
->rpfilter
))
314 s
->flags
|= DEV_OPT_RPFILTER
;
316 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur
));
319 if ((cur
= tb
[DEV_ATTR_ACCEPTLOCAL
])) {
320 s
->acceptlocal
= blobmsg_get_bool(cur
);
321 s
->flags
|= DEV_OPT_ACCEPTLOCAL
;
324 if ((cur
= tb
[DEV_ATTR_IGMPVERSION
])) {
325 s
->igmpversion
= blobmsg_get_u32(cur
);
326 if (s
->igmpversion
>= 1 && s
->igmpversion
<= 3)
327 s
->flags
|= DEV_OPT_IGMPVERSION
;
329 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur
));
332 if ((cur
= tb
[DEV_ATTR_MLDVERSION
])) {
333 s
->mldversion
= blobmsg_get_u32(cur
);
334 if (s
->mldversion
>= 1 && s
->mldversion
<= 2)
335 s
->flags
|= DEV_OPT_MLDVERSION
;
337 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur
));
340 if ((cur
= tb
[DEV_ATTR_NEIGHREACHABLETIME
])) {
341 s
->neigh6reachabletime
= s
->neigh4reachabletime
= blobmsg_get_u32(cur
);
342 s
->flags
|= DEV_OPT_NEIGHREACHABLETIME
;
345 if ((cur
= tb
[DEV_ATTR_NEIGHGCSTALETIME
])) {
346 s
->neigh6gcstaletime
= s
->neigh4gcstaletime
= blobmsg_get_u32(cur
);
347 s
->flags
|= DEV_OPT_NEIGHGCSTALETIME
;
350 if ((cur
= tb
[DEV_ATTR_NEIGHLOCKTIME
])) {
351 s
->neigh4locktime
= blobmsg_get_u32(cur
);
352 s
->flags
|= DEV_OPT_NEIGHLOCKTIME
;
355 if ((cur
= tb
[DEV_ATTR_DADTRANSMITS
])) {
356 s
->dadtransmits
= blobmsg_get_u32(cur
);
357 s
->flags
|= DEV_OPT_DADTRANSMITS
;
360 if ((cur
= tb
[DEV_ATTR_MULTICAST_TO_UNICAST
])) {
361 s
->multicast_to_unicast
= blobmsg_get_bool(cur
);
362 s
->flags
|= DEV_OPT_MULTICAST_TO_UNICAST
;
365 if ((cur
= tb
[DEV_ATTR_MULTICAST_ROUTER
])) {
366 s
->multicast_router
= blobmsg_get_u32(cur
);
367 if (s
->multicast_router
<= 2)
368 s
->flags
|= DEV_OPT_MULTICAST_ROUTER
;
370 DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur
));
373 if ((cur
= tb
[DEV_ATTR_MULTICAST_FAST_LEAVE
])) {
374 s
->multicast_fast_leave
= blobmsg_get_bool(cur
);
375 s
->flags
|= DEV_OPT_MULTICAST_FAST_LEAVE
;
378 if ((cur
= tb
[DEV_ATTR_MULTICAST
])) {
379 s
->multicast
= blobmsg_get_bool(cur
);
380 s
->flags
|= DEV_OPT_MULTICAST
;
383 if ((cur
= tb
[DEV_ATTR_LEARNING
])) {
384 s
->learning
= blobmsg_get_bool(cur
);
385 s
->flags
|= DEV_OPT_LEARNING
;
388 if ((cur
= tb
[DEV_ATTR_UNICAST_FLOOD
])) {
389 s
->unicast_flood
= blobmsg_get_bool(cur
);
390 s
->flags
|= DEV_OPT_UNICAST_FLOOD
;
393 if ((cur
= tb
[DEV_ATTR_SENDREDIRECTS
])) {
394 s
->sendredirects
= blobmsg_get_bool(cur
);
395 s
->flags
|= DEV_OPT_SENDREDIRECTS
;
398 if ((cur
= tb
[DEV_ATTR_ISOLATE
])) {
399 s
->isolate
= blobmsg_get_bool(cur
);
400 s
->flags
|= DEV_OPT_ISOLATE
;
403 device_set_disabled(dev
, disabled
);
406 static void __init
dev_init(void)
408 avl_init(&devices
, avl_strcmp
, true, NULL
);
411 static int device_broadcast_cb(void *ctx
, struct safe_list
*list
)
413 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
416 /* device might have been removed by an earlier callback */
425 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
429 safe_list_for_each(&dev
->aliases
, device_broadcast_cb
, &dev_ev
);
430 safe_list_for_each(&dev
->users
, device_broadcast_cb
, &dev_ev
);
433 int device_claim(struct device_user
*dep
)
435 struct device
*dev
= dep
->dev
;
445 D(DEVICE
, "Claim %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
446 if (++dev
->active
!= 1)
449 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
451 /* Get ifindex for external claimed devices so a valid */
452 /* ifindex is in place avoiding possible race conditions */
453 device_set_ifindex(dev
, system_if_resolve(dev
));
457 system_if_get_settings(dev
, &dev
->orig_settings
);
459 ret
= dev
->set_state(dev
, true);
462 device_broadcast_event(dev
, DEV_EVENT_UP
);
464 D(DEVICE
, "claim %s %s failed: %d\n", dev
->type
->name
, dev
->ifname
, ret
);
466 dep
->claimed
= false;
472 void device_release(struct device_user
*dep
)
474 struct device
*dev
= dep
->dev
;
479 dep
->claimed
= false;
481 D(DEVICE
, "Release %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
482 assert(dev
->active
>= 0);
487 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
489 dev
->set_state(dev
, false);
494 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
497 int device_check_state(struct device
*dev
)
499 if (!dev
->type
->check_state
)
500 return simple_device_type
.check_state(dev
);
502 return dev
->type
->check_state(dev
);
505 int device_init_virtual(struct device
*dev
, struct device_type
*type
, const char *name
)
510 D(DEVICE
, "Initialize device '%s'\n", name
? name
: "");
511 INIT_SAFE_LIST(&dev
->users
);
512 INIT_SAFE_LIST(&dev
->aliases
);
518 ret
= device_set_ifname(dev
, name
);
524 dev
->set_state
= set_device_state
;
529 int device_init(struct device
*dev
, struct device_type
*type
, const char *ifname
)
533 ret
= device_init_virtual(dev
, type
, ifname
);
537 dev
->avl
.key
= dev
->ifname
;
539 ret
= avl_insert(&devices
, &dev
->avl
);
543 system_if_clear_state(dev
);
548 static struct device
*
549 device_create_default(const char *name
, bool external
)
553 if (!external
&& system_if_force_external(name
))
556 D(DEVICE
, "Create simple device '%s'\n", name
);
557 dev
= calloc(1, sizeof(*dev
));
561 dev
->external
= external
;
562 dev
->set_state
= simple_device_set_state
;
564 if (device_init(dev
, &simple_device_type
, name
) < 0) {
570 dev
->default_config
= true;
572 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
574 device_check_state(dev
);
580 device_find(const char *name
)
584 return avl_find_element(&devices
, name
, dev
, avl
);
588 device_get(const char *name
, int create
)
592 dev
= avl_find_element(&devices
, name
, dev
, avl
);
594 if (!dev
&& strchr(name
, '.'))
595 return get_vlan_device_chain(name
, create
);
598 return device_alias_get(name
+ 1);
601 if (create
> 1 && !dev
->external
) {
602 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
603 dev
->external
= true;
604 device_set_present(dev
, true);
612 return device_create_default(name
, create
> 1);
616 device_delete(struct device
*dev
)
621 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
622 avl_delete(&devices
, &dev
->avl
);
626 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
628 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
630 dep
->cb(dep
, DEV_EVENT_REMOVE
);
636 void device_cleanup(struct device
*dev
)
638 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
639 safe_list_for_each(&dev
->users
, device_cleanup_cb
, NULL
);
640 safe_list_for_each(&dev
->aliases
, device_cleanup_cb
, NULL
);
644 static void __device_set_present(struct device
*dev
, bool state
)
646 if (dev
->present
== state
)
649 dev
->present
= state
;
650 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
654 device_refresh_present(struct device
*dev
)
656 bool state
= dev
->sys_present
;
658 if (dev
->disabled
|| dev
->deferred
)
661 __device_set_present(dev
, state
);
664 void device_set_present(struct device
*dev
, bool state
)
666 if (dev
->sys_present
== state
)
669 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
670 dev
->sys_present
= state
;
671 device_refresh_present(dev
);
674 void device_set_link(struct device
*dev
, bool state
)
676 if (dev
->link_active
== state
)
679 netifd_log_message(L_NOTICE
, "%s '%s' link is %s\n", dev
->type
->name
, dev
->ifname
, state
? "up" : "down" );
681 dev
->link_active
= state
;
682 device_broadcast_event(dev
, state
? DEV_EVENT_LINK_UP
: DEV_EVENT_LINK_DOWN
);
685 void device_set_ifindex(struct device
*dev
, int ifindex
)
687 if (dev
->ifindex
== ifindex
)
690 dev
->ifindex
= ifindex
;
691 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFINDEX
);
694 int device_set_ifname(struct device
*dev
, const char *name
)
698 if (!strcmp(dev
->ifname
, name
))
701 if (strlen(name
) > sizeof(dev
->ifname
) - 1)
705 avl_delete(&devices
, &dev
->avl
);
707 strcpy(dev
->ifname
, name
);
710 ret
= avl_insert(&devices
, &dev
->avl
);
713 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFNAME
);
718 static int device_refcount(struct device
*dev
)
720 struct list_head
*list
;
723 list_for_each(list
, &dev
->users
.list
)
726 list_for_each(list
, &dev
->aliases
.list
)
733 __device_add_user(struct device_user
*dep
, struct device
*dev
)
735 struct safe_list
*head
;
740 head
= &dev
->aliases
;
744 safe_list_add(&dep
->list
, head
);
745 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
747 if (dep
->cb
&& dev
->present
) {
748 dep
->cb(dep
, DEV_EVENT_ADD
);
750 dep
->cb(dep
, DEV_EVENT_UP
);
752 if (dev
->link_active
)
753 dep
->cb(dep
, DEV_EVENT_LINK_UP
);
757 void device_add_user(struct device_user
*dep
, struct device
*dev
)
763 device_remove_user(dep
);
768 __device_add_user(dep
, dev
);
772 device_free(struct device
*dev
)
777 dev
->type
->free(dev
);
782 __device_free_unused(struct device
*dev
)
784 if (!safe_list_empty(&dev
->users
) ||
785 !safe_list_empty(&dev
->aliases
) ||
786 dev
->current_config
|| __devlock
)
792 void device_remove_user(struct device_user
*dep
)
794 struct device
*dev
= dep
->dev
;
799 dep
->hotplug
= false;
803 safe_list_del(&dep
->list
);
805 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
806 __device_free_unused(dev
);
810 device_free_unused(struct device
*dev
)
815 return __device_free_unused(dev
);
817 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
818 __device_free_unused(dev
);
822 device_init_pending(void)
824 struct device
*dev
, *tmp
;
826 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
827 if (!dev
->config_pending
)
830 dev
->type
->config_init(dev
);
831 dev
->config_pending
= false;
832 device_check_state(dev
);
836 static enum dev_change_type
837 device_set_config(struct device
*dev
, struct device_type
*type
,
838 struct blob_attr
*attr
)
840 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
841 const struct uci_blob_param_list
*cfg
= type
->config_params
;
843 if (type
!= dev
->type
)
844 return DEV_CONFIG_RECREATE
;
846 if (dev
->type
->reload
)
847 return dev
->type
->reload(dev
, attr
);
849 if (uci_blob_check_equal(dev
->config
, attr
, cfg
))
850 return DEV_CONFIG_NO_CHANGE
;
852 if (cfg
== &device_attr_list
) {
853 memset(tb
, 0, sizeof(tb
));
856 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
857 blob_data(attr
), blob_len(attr
));
859 device_init_settings(dev
, tb
);
860 return DEV_CONFIG_RESTART
;
862 return DEV_CONFIG_RECREATE
;
866 device_apply_config(struct device
*dev
, struct device_type
*type
,
867 struct blob_attr
*config
)
869 enum dev_change_type change
;
871 change
= device_set_config(dev
, type
, config
);
873 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
874 change
= DEV_CONFIG_APPLIED
;
878 case DEV_CONFIG_RESTART
:
879 case DEV_CONFIG_APPLIED
:
880 D(DEVICE
, "Device '%s': config applied\n", dev
->ifname
);
881 config
= blob_memdup(config
);
883 dev
->config
= config
;
884 if (change
== DEV_CONFIG_RESTART
&& dev
->present
) {
887 device_set_present(dev
, false);
888 if (dev
->active
&& !dev
->external
) {
889 ret
= dev
->set_state(dev
, false);
891 ret
= dev
->set_state(dev
, true);
894 device_set_present(dev
, true);
897 case DEV_CONFIG_NO_CHANGE
:
898 D(DEVICE
, "Device '%s': no configuration change\n", dev
->ifname
);
900 case DEV_CONFIG_RECREATE
:
908 device_replace(struct device
*dev
, struct device
*odev
)
910 struct device_user
*dep
, *tmp
;
914 device_set_present(odev
, false);
916 list_for_each_entry_safe(dep
, tmp
, &odev
->users
.list
, list
.list
) {
918 safe_list_del(&dep
->list
);
919 __device_add_user(dep
, dev
);
927 device_reset_config(void)
931 avl_for_each_element(&devices
, dev
, avl
)
932 dev
->current_config
= false;
936 device_reset_old(void)
938 struct device
*dev
, *tmp
, *ndev
;
940 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
941 if (dev
->current_config
|| dev
->default_config
)
944 if (dev
->type
!= &simple_device_type
)
947 ndev
= device_create_default(dev
->ifname
, dev
->external
);
951 device_replace(ndev
, dev
);
956 device_create(const char *name
, struct device_type
*type
,
957 struct blob_attr
*config
)
959 struct device
*odev
= NULL
, *dev
;
960 enum dev_change_type change
;
962 odev
= device_find(name
);
964 odev
->current_config
= true;
965 change
= device_apply_config(odev
, type
, config
);
967 case DEV_CONFIG_RECREATE
:
968 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
975 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
977 config
= blob_memdup(config
);
981 dev
= type
->create(name
, type
, config
);
985 dev
->current_config
= true;
986 dev
->config
= config
;
988 device_replace(dev
, odev
);
990 if (!config_init
&& dev
->config_pending
) {
991 type
->config_init(dev
);
992 dev
->config_pending
= false;
995 device_check_state(dev
);
1001 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
1003 struct device_settings st
;
1007 avl_for_each_element(&devices
, dev
, avl
) {
1010 c
= blobmsg_open_table(b
, dev
->ifname
);
1011 device_dump_status(b
, dev
);
1012 blobmsg_close_table(b
, c
);
1018 blobmsg_add_u8(b
, "external", dev
->external
);
1019 blobmsg_add_u8(b
, "present", dev
->present
);
1020 blobmsg_add_string(b
, "type", dev
->type
->name
);
1025 blobmsg_add_u8(b
, "up", !!dev
->active
);
1026 blobmsg_add_u8(b
, "carrier", !!dev
->link_active
);
1028 if (dev
->type
->dump_info
)
1029 dev
->type
->dump_info(dev
, b
);
1031 system_if_dump_info(dev
, b
);
1034 device_merge_settings(dev
, &st
);
1035 if (st
.flags
& DEV_OPT_MTU
)
1036 blobmsg_add_u32(b
, "mtu", st
.mtu
);
1037 if (st
.flags
& DEV_OPT_MTU6
)
1038 blobmsg_add_u32(b
, "mtu6", st
.mtu6
);
1039 if (st
.flags
& DEV_OPT_MACADDR
)
1040 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
1041 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
1042 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
1043 if (st
.flags
& DEV_OPT_IPV6
)
1044 blobmsg_add_u8(b
, "ipv6", st
.ipv6
);
1045 if (st
.flags
& DEV_OPT_PROMISC
)
1046 blobmsg_add_u8(b
, "promisc", st
.promisc
);
1047 if (st
.flags
& DEV_OPT_RPFILTER
)
1048 blobmsg_add_u32(b
, "rpfilter", st
.rpfilter
);
1049 if (st
.flags
& DEV_OPT_ACCEPTLOCAL
)
1050 blobmsg_add_u8(b
, "acceptlocal", st
.acceptlocal
);
1051 if (st
.flags
& DEV_OPT_IGMPVERSION
)
1052 blobmsg_add_u32(b
, "igmpversion", st
.igmpversion
);
1053 if (st
.flags
& DEV_OPT_MLDVERSION
)
1054 blobmsg_add_u32(b
, "mldversion", st
.mldversion
);
1055 if (st
.flags
& DEV_OPT_NEIGHREACHABLETIME
) {
1056 blobmsg_add_u32(b
, "neigh4reachabletime", st
.neigh4reachabletime
);
1057 blobmsg_add_u32(b
, "neigh6reachabletime", st
.neigh6reachabletime
);
1059 if (st
.flags
& DEV_OPT_NEIGHGCSTALETIME
) {
1060 blobmsg_add_u32(b
, "neigh4gcstaletime", st
.neigh4gcstaletime
);
1061 blobmsg_add_u32(b
, "neigh6gcstaletime", st
.neigh6gcstaletime
);
1063 if (st
.flags
& DEV_OPT_NEIGHLOCKTIME
)
1064 blobmsg_add_u32(b
, "neigh4locktime", st
.neigh4locktime
);
1065 if (st
.flags
& DEV_OPT_DADTRANSMITS
)
1066 blobmsg_add_u32(b
, "dadtransmits", st
.dadtransmits
);
1067 if (st
.flags
& DEV_OPT_MULTICAST_TO_UNICAST
)
1068 blobmsg_add_u8(b
, "multicast_to_unicast", st
.multicast_to_unicast
);
1069 if (st
.flags
& DEV_OPT_MULTICAST_ROUTER
)
1070 blobmsg_add_u32(b
, "multicast_router", st
.multicast_router
);
1071 if (st
.flags
& DEV_OPT_MULTICAST_FAST_LEAVE
)
1072 blobmsg_add_u8(b
, "multicast_fast_leave", st
.multicast_fast_leave
);
1073 if (st
.flags
& DEV_OPT_MULTICAST
)
1074 blobmsg_add_u8(b
, "multicast", st
.multicast
);
1075 if (st
.flags
& DEV_OPT_LEARNING
)
1076 blobmsg_add_u8(b
, "learning", st
.learning
);
1077 if (st
.flags
& DEV_OPT_UNICAST_FLOOD
)
1078 blobmsg_add_u8(b
, "unicast_flood", st
.unicast_flood
);
1079 if (st
.flags
& DEV_OPT_SENDREDIRECTS
)
1080 blobmsg_add_u8(b
, "sendredirects", st
.sendredirects
);
1083 s
= blobmsg_open_table(b
, "statistics");
1084 if (dev
->type
->dump_stats
)
1085 dev
->type
->dump_stats(dev
, b
);
1087 system_if_dump_stats(dev
, b
);
1088 blobmsg_close_table(b
, s
);
1091 static void __init
simple_device_type_init(void)
1093 device_type_add(&simple_device_type
);