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
;
33 static const struct blobmsg_policy dev_attrs
[__DEV_ATTR_MAX
] = {
34 [DEV_ATTR_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
35 [DEV_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_ARRAY
},
36 [DEV_ATTR_MTU
] = { .name
= "mtu", .type
= BLOBMSG_TYPE_INT32
},
37 [DEV_ATTR_MACADDR
] = { .name
= "macaddr", .type
= BLOBMSG_TYPE_STRING
},
38 [DEV_ATTR_TXQUEUELEN
] = { .name
= "txqueuelen", .type
= BLOBMSG_TYPE_INT32
},
39 [DEV_ATTR_ENABLED
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_BOOL
},
40 [DEV_ATTR_IPV6
] = { .name
= "ipv6", .type
= BLOBMSG_TYPE_BOOL
},
41 [DEV_ATTR_PROMISC
] = { .name
= "promisc", .type
= BLOBMSG_TYPE_BOOL
},
44 const struct uci_blob_param_list device_attr_list
= {
45 .n_params
= __DEV_ATTR_MAX
,
49 static int __devlock
= 0;
51 void device_lock(void)
56 void device_unlock(void)
60 device_free_unused(NULL
);
63 static int set_device_state(struct device
*dev
, bool state
)
66 /* Set ifindex for all devices being enabled so a valid */
67 /* ifindex is in place avoiding possible race conditions */
68 device_set_ifindex(dev
, system_if_resolve(dev
));
85 simple_device_set_state(struct device
*dev
, bool state
)
90 pdev
= dev
->parent
.dev
;
92 pdev
= system_if_get_parent(dev
);
94 device_add_user(&dev
->parent
, pdev
);
99 ret
= device_claim(&dev
->parent
);
101 device_release(&dev
->parent
);
106 return set_device_state(dev
, state
);
109 static struct device
*
110 simple_device_create(const char *name
, struct blob_attr
*attr
)
112 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
113 struct device
*dev
= NULL
;
115 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
, blob_data(attr
), blob_len(attr
));
116 dev
= device_get(name
, true);
120 dev
->set_state
= simple_device_set_state
;
121 device_init_settings(dev
, tb
);
126 static void simple_device_free(struct device
*dev
)
129 device_remove_user(&dev
->parent
);
133 const struct device_type simple_device_type
= {
134 .name
= "Network device",
135 .config_params
= &device_attr_list
,
137 .create
= simple_device_create
,
138 .check_state
= system_if_check
,
139 .free
= simple_device_free
,
143 device_merge_settings(struct device
*dev
, struct device_settings
*n
)
145 struct device_settings
*os
= &dev
->orig_settings
;
146 struct device_settings
*s
= &dev
->settings
;
148 memset(n
, 0, sizeof(*n
));
149 n
->mtu
= s
->flags
& DEV_OPT_MTU
? s
->mtu
: os
->mtu
;
150 n
->txqueuelen
= s
->flags
& DEV_OPT_TXQUEUELEN
?
151 s
->txqueuelen
: os
->txqueuelen
;
153 (s
->flags
& DEV_OPT_MACADDR
? s
->macaddr
: os
->macaddr
),
155 n
->ipv6
= s
->flags
& DEV_OPT_IPV6
? s
->ipv6
: os
->ipv6
;
156 n
->promisc
= s
->flags
& DEV_OPT_PROMISC
? s
->promisc
: os
->promisc
;
157 n
->flags
= s
->flags
| os
->flags
;
161 device_init_settings(struct device
*dev
, struct blob_attr
**tb
)
163 struct device_settings
*s
= &dev
->settings
;
164 struct blob_attr
*cur
;
165 struct ether_addr
*ea
;
166 bool disabled
= false;
169 if ((cur
= tb
[DEV_ATTR_ENABLED
]))
170 disabled
= !blobmsg_get_bool(cur
);
172 if ((cur
= tb
[DEV_ATTR_MTU
])) {
173 s
->mtu
= blobmsg_get_u32(cur
);
174 s
->flags
|= DEV_OPT_MTU
;
177 if ((cur
= tb
[DEV_ATTR_TXQUEUELEN
])) {
178 s
->txqueuelen
= blobmsg_get_u32(cur
);
179 s
->flags
|= DEV_OPT_TXQUEUELEN
;
182 if ((cur
= tb
[DEV_ATTR_MACADDR
])) {
183 ea
= ether_aton(blobmsg_data(cur
));
185 memcpy(s
->macaddr
, ea
, 6);
186 s
->flags
|= DEV_OPT_MACADDR
;
190 if ((cur
= tb
[DEV_ATTR_IPV6
])) {
191 s
->ipv6
= blobmsg_get_bool(cur
);
192 s
->flags
|= DEV_OPT_IPV6
;
195 if ((cur
= tb
[DEV_ATTR_PROMISC
])) {
196 s
->promisc
= blobmsg_get_bool(cur
);
197 s
->flags
|= DEV_OPT_PROMISC
;
200 device_set_disabled(dev
, disabled
);
203 static void __init
dev_init(void)
205 avl_init(&devices
, avl_strcmp
, true, NULL
);
208 static int device_broadcast_cb(void *ctx
, struct safe_list
*list
)
210 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
213 /* device might have been removed by an earlier callback */
222 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
226 safe_list_for_each(&dev
->aliases
, device_broadcast_cb
, &dev_ev
);
227 safe_list_for_each(&dev
->users
, device_broadcast_cb
, &dev_ev
);
230 int device_claim(struct device_user
*dep
)
232 struct device
*dev
= dep
->dev
;
239 D(DEVICE
, "Claim %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
240 if (++dev
->active
!= 1)
243 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
244 ret
= dev
->set_state(dev
, true);
246 device_broadcast_event(dev
, DEV_EVENT_UP
);
248 D(DEVICE
, "claim %s %s failed: %d\n", dev
->type
->name
, dev
->ifname
, ret
);
250 dep
->claimed
= false;
256 void device_release(struct device_user
*dep
)
258 struct device
*dev
= dep
->dev
;
263 dep
->claimed
= false;
265 D(DEVICE
, "Release %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
266 assert(dev
->active
>= 0);
271 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
273 dev
->set_state(dev
, false);
274 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
277 int device_check_state(struct device
*dev
)
279 if (!dev
->type
->check_state
)
280 return simple_device_type
.check_state(dev
);
282 return dev
->type
->check_state(dev
);
285 void device_init_virtual(struct device
*dev
, const struct device_type
*type
, const char *name
)
291 strncpy(dev
->ifname
, name
, IFNAMSIZ
);
293 D(DEVICE
, "Initialize device '%s'\n", dev
->ifname
);
294 INIT_SAFE_LIST(&dev
->users
);
295 INIT_SAFE_LIST(&dev
->aliases
);
299 dev
->set_state
= set_device_state
;
302 int device_init(struct device
*dev
, const struct device_type
*type
, const char *ifname
)
306 device_init_virtual(dev
, type
, ifname
);
308 dev
->avl
.key
= dev
->ifname
;
310 ret
= avl_insert(&devices
, &dev
->avl
);
314 system_if_clear_state(dev
);
315 device_check_state(dev
);
320 static struct device
*
321 device_create_default(const char *name
, bool external
)
325 if (!external
&& system_if_force_external(name
))
328 D(DEVICE
, "Create simple device '%s'\n", name
);
329 dev
= calloc(1, sizeof(*dev
));
330 dev
->external
= external
;
331 dev
->set_state
= simple_device_set_state
;
332 device_init(dev
, &simple_device_type
, name
);
333 dev
->default_config
= true;
338 device_get(const char *name
, int create
)
342 if (strchr(name
, '.'))
343 return get_vlan_device_chain(name
, create
);
346 return device_alias_get(name
+ 1);
348 dev
= avl_find_element(&devices
, name
, dev
, avl
);
350 if (create
> 1 && !dev
->external
) {
351 dev
->external
= true;
352 device_set_present(dev
, true);
360 return device_create_default(name
, create
> 1);
364 device_delete(struct device
*dev
)
369 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
370 avl_delete(&devices
, &dev
->avl
);
374 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
376 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
378 dep
->cb(dep
, DEV_EVENT_REMOVE
);
384 void device_cleanup(struct device
*dev
)
386 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
387 safe_list_for_each(&dev
->users
, device_cleanup_cb
, NULL
);
388 safe_list_for_each(&dev
->aliases
, device_cleanup_cb
, NULL
);
392 static void __device_set_present(struct device
*dev
, bool state
)
394 if (dev
->present
== state
)
397 dev
->present
= state
;
398 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
402 device_refresh_present(struct device
*dev
)
404 bool state
= dev
->sys_present
;
406 if (dev
->disabled
|| dev
->deferred
)
409 __device_set_present(dev
, state
);
412 void device_set_present(struct device
*dev
, bool state
)
414 if (dev
->sys_present
== state
)
417 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
418 dev
->sys_present
= state
;
419 device_refresh_present(dev
);
422 void device_set_link(struct device
*dev
, bool state
)
424 if (dev
->link_active
== state
)
427 netifd_log_message(L_NOTICE
, "%s '%s' link is %s\n", dev
->type
->name
, dev
->ifname
, state
? "up" : "down" );
429 dev
->link_active
= state
;
430 device_broadcast_event(dev
, state
? DEV_EVENT_LINK_UP
: DEV_EVENT_LINK_DOWN
);
433 void device_set_ifindex(struct device
*dev
, int ifindex
)
435 if (dev
->ifindex
== ifindex
)
438 dev
->ifindex
= ifindex
;
439 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFINDEX
);
442 static int device_refcount(struct device
*dev
)
444 struct list_head
*list
;
447 list_for_each(list
, &dev
->users
.list
)
450 list_for_each(list
, &dev
->aliases
.list
)
456 void device_add_user(struct device_user
*dep
, struct device
*dev
)
458 struct safe_list
*head
;
464 device_remove_user(dep
);
472 head
= &dev
->aliases
;
476 safe_list_add(&dep
->list
, head
);
477 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
479 if (dep
->cb
&& dev
->present
) {
480 dep
->cb(dep
, DEV_EVENT_ADD
);
482 dep
->cb(dep
, DEV_EVENT_UP
);
484 if (dev
->link_active
)
485 dep
->cb(dep
, DEV_EVENT_LINK_UP
);
490 device_free(struct device
*dev
)
495 dev
->type
->free(dev
);
500 __device_free_unused(struct device
*dev
)
502 if (!safe_list_empty(&dev
->users
) ||
503 !safe_list_empty(&dev
->aliases
) ||
504 dev
->current_config
|| __devlock
)
510 void device_remove_user(struct device_user
*dep
)
512 struct device
*dev
= dep
->dev
;
517 dep
->hotplug
= false;
521 safe_list_del(&dep
->list
);
523 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
524 __device_free_unused(dev
);
528 device_free_unused(struct device
*dev
)
533 return __device_free_unused(dev
);
535 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
536 __device_free_unused(dev
);
540 device_init_pending(void)
542 struct device
*dev
, *tmp
;
544 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
545 if (!dev
->config_pending
)
548 dev
->type
->config_init(dev
);
549 dev
->config_pending
= false;
553 static enum dev_change_type
554 device_reload_config(struct device
*dev
, struct blob_attr
*attr
)
556 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
557 const struct uci_blob_param_list
*cfg
= dev
->type
->config_params
;
559 if (uci_blob_check_equal(dev
->config
, attr
, cfg
))
560 return DEV_CONFIG_NO_CHANGE
;
562 if (cfg
== &device_attr_list
) {
563 memset(tb
, 0, sizeof(tb
));
566 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
567 blob_data(attr
), blob_len(attr
));
569 device_init_settings(dev
, tb
);
570 return DEV_CONFIG_RESTART
;
572 return DEV_CONFIG_RECREATE
;
576 device_set_config(struct device
*dev
, const struct device_type
*type
,
577 struct blob_attr
*attr
)
579 if (type
!= dev
->type
)
580 return DEV_CONFIG_RECREATE
;
582 if (dev
->type
->reload
)
583 return dev
->type
->reload(dev
, attr
);
585 return device_reload_config(dev
, attr
);
589 device_replace(struct device
*dev
, struct device
*odev
)
591 struct device_user
*dep
, *tmp
;
592 bool present
= odev
->present
;
595 device_set_present(odev
, false);
597 list_for_each_entry_safe(dep
, tmp
, &odev
->users
.list
, list
.list
) {
599 safe_list_del(&dep
->list
);
600 safe_list_add(&dep
->list
, &dev
->users
);
606 device_set_present(dev
, true);
610 device_reset_config(void)
614 avl_for_each_element(&devices
, dev
, avl
)
615 dev
->current_config
= false;
619 device_reset_old(void)
621 struct device
*dev
, *tmp
, *ndev
;
623 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
624 if (dev
->current_config
|| dev
->default_config
)
627 if (dev
->type
!= &simple_device_type
)
630 ndev
= device_create_default(dev
->ifname
, dev
->external
);
631 device_replace(ndev
, dev
);
636 device_create(const char *name
, const struct device_type
*type
,
637 struct blob_attr
*config
)
639 struct device
*odev
= NULL
, *dev
;
640 enum dev_change_type change
;
642 config
= blob_memdup(config
);
646 odev
= device_get(name
, false);
648 odev
->current_config
= true;
649 change
= device_set_config(odev
, type
, config
);
650 if (odev
->external
) {
651 system_if_apply_settings(odev
, &odev
->settings
, odev
->settings
.flags
);
652 change
= DEV_CONFIG_APPLIED
;
655 case DEV_CONFIG_RESTART
:
656 case DEV_CONFIG_APPLIED
:
657 D(DEVICE
, "Device '%s': config applied\n", odev
->ifname
);
659 odev
->config
= config
;
660 if (change
== DEV_CONFIG_RESTART
&& odev
->present
) {
661 device_set_present(odev
, false);
662 device_set_present(odev
, true);
665 case DEV_CONFIG_NO_CHANGE
:
666 D(DEVICE
, "Device '%s': no configuration change\n", odev
->ifname
);
669 case DEV_CONFIG_RECREATE
:
670 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
675 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
677 dev
= type
->create(name
, config
);
681 dev
->current_config
= true;
682 dev
->config
= config
;
684 device_replace(dev
, odev
);
686 if (!config_init
&& dev
->config_pending
)
687 type
->config_init(dev
);
693 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
695 struct device_settings st
;
699 avl_for_each_element(&devices
, dev
, avl
) {
702 c
= blobmsg_open_table(b
, dev
->ifname
);
703 device_dump_status(b
, dev
);
704 blobmsg_close_table(b
, c
);
710 blobmsg_add_u8(b
, "external", dev
->external
);
711 blobmsg_add_u8(b
, "present", dev
->present
);
712 blobmsg_add_string(b
, "type", dev
->type
->name
);
717 blobmsg_add_u8(b
, "up", !!dev
->active
);
718 blobmsg_add_u8(b
, "carrier", !!dev
->link_active
);
720 if (dev
->type
->dump_info
)
721 dev
->type
->dump_info(dev
, b
);
723 system_if_dump_info(dev
, b
);
726 device_merge_settings(dev
, &st
);
727 if (st
.flags
& DEV_OPT_MTU
)
728 blobmsg_add_u32(b
, "mtu", st
.mtu
);
729 if (st
.flags
& DEV_OPT_MACADDR
)
730 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
731 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
732 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
733 if (st
.flags
& DEV_OPT_IPV6
)
734 blobmsg_add_u8(b
, "ipv6", st
.ipv6
);
735 if (st
.flags
& DEV_OPT_PROMISC
)
736 blobmsg_add_u8(b
, "promisc", st
.promisc
);
739 s
= blobmsg_open_table(b
, "statistics");
740 if (dev
->type
->dump_stats
)
741 dev
->type
->dump_stats(dev
, b
);
743 system_if_dump_stats(dev
, b
);
744 blobmsg_close_table(b
, s
);