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
},
42 [DEV_ATTR_RPFILTER
] = { .name
= "rpfilter", .type
= BLOBMSG_TYPE_STRING
},
43 [DEV_ATTR_ACCEPTLOCAL
] = { .name
= "acceptlocal", .type
= BLOBMSG_TYPE_BOOL
},
44 [DEV_ATTR_IGMPVERSION
] = { .name
= "igmpversion", .type
= BLOBMSG_TYPE_INT32
},
45 [DEV_ATTR_MLDVERSION
] = { .name
= "mldversion", .type
= BLOBMSG_TYPE_INT32
},
46 [DEV_ATTR_NEIGHREACHABLETIME
] = { .name
= "neighreachabletime", .type
= BLOBMSG_TYPE_INT32
},
47 [DEV_ATTR_RPS
] = { .name
= "rps", .type
= BLOBMSG_TYPE_BOOL
},
48 [DEV_ATTR_XPS
] = { .name
= "xps", .type
= BLOBMSG_TYPE_BOOL
},
51 const struct uci_blob_param_list device_attr_list
= {
52 .n_params
= __DEV_ATTR_MAX
,
56 static int __devlock
= 0;
58 void device_lock(void)
63 void device_unlock(void)
67 device_free_unused(NULL
);
70 static int set_device_state(struct device
*dev
, bool state
)
73 /* Set ifindex for all devices being enabled so a valid */
74 /* ifindex is in place avoiding possible race conditions */
75 device_set_ifindex(dev
, system_if_resolve(dev
));
92 simple_device_set_state(struct device
*dev
, bool state
)
97 pdev
= dev
->parent
.dev
;
99 pdev
= system_if_get_parent(dev
);
101 device_add_user(&dev
->parent
, pdev
);
106 ret
= device_claim(&dev
->parent
);
108 device_release(&dev
->parent
);
113 return set_device_state(dev
, state
);
116 static struct device
*
117 simple_device_create(const char *name
, struct blob_attr
*attr
)
119 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
120 struct device
*dev
= NULL
;
122 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
, blob_data(attr
), blob_len(attr
));
123 dev
= device_get(name
, true);
127 dev
->set_state
= simple_device_set_state
;
128 device_init_settings(dev
, tb
);
133 static void simple_device_free(struct device
*dev
)
136 device_remove_user(&dev
->parent
);
140 const struct device_type simple_device_type
= {
141 .name
= "Network device",
142 .config_params
= &device_attr_list
,
144 .create
= simple_device_create
,
145 .check_state
= system_if_check
,
146 .free
= simple_device_free
,
150 device_merge_settings(struct device
*dev
, struct device_settings
*n
)
152 struct device_settings
*os
= &dev
->orig_settings
;
153 struct device_settings
*s
= &dev
->settings
;
155 memset(n
, 0, sizeof(*n
));
156 n
->mtu
= s
->flags
& DEV_OPT_MTU
? s
->mtu
: os
->mtu
;
157 n
->txqueuelen
= s
->flags
& DEV_OPT_TXQUEUELEN
?
158 s
->txqueuelen
: os
->txqueuelen
;
160 (s
->flags
& DEV_OPT_MACADDR
? s
->macaddr
: os
->macaddr
),
162 n
->ipv6
= s
->flags
& DEV_OPT_IPV6
? s
->ipv6
: os
->ipv6
;
163 n
->promisc
= s
->flags
& DEV_OPT_PROMISC
? s
->promisc
: os
->promisc
;
164 n
->rpfilter
= s
->flags
& DEV_OPT_RPFILTER
? s
->rpfilter
: os
->rpfilter
;
165 n
->acceptlocal
= s
->flags
& DEV_OPT_ACCEPTLOCAL
? s
->acceptlocal
: os
->acceptlocal
;
166 n
->igmpversion
= s
->flags
& DEV_OPT_IGMPVERSION
? s
->igmpversion
: os
->igmpversion
;
167 n
->mldversion
= s
->flags
& DEV_OPT_MLDVERSION
? s
->mldversion
: os
->mldversion
;
168 n
->neigh4reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
169 s
->neigh4reachabletime
: os
->neigh4reachabletime
;
170 n
->neigh6reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
171 s
->neigh6reachabletime
: os
->neigh6reachabletime
;
172 n
->flags
= s
->flags
| os
->flags
;
176 device_init_settings(struct device
*dev
, struct blob_attr
**tb
)
178 struct device_settings
*s
= &dev
->settings
;
179 struct blob_attr
*cur
;
180 struct ether_addr
*ea
;
181 bool disabled
= false;
184 if ((cur
= tb
[DEV_ATTR_ENABLED
]))
185 disabled
= !blobmsg_get_bool(cur
);
187 if ((cur
= tb
[DEV_ATTR_MTU
])) {
188 s
->mtu
= blobmsg_get_u32(cur
);
189 s
->flags
|= DEV_OPT_MTU
;
192 if ((cur
= tb
[DEV_ATTR_TXQUEUELEN
])) {
193 s
->txqueuelen
= blobmsg_get_u32(cur
);
194 s
->flags
|= DEV_OPT_TXQUEUELEN
;
197 if ((cur
= tb
[DEV_ATTR_MACADDR
])) {
198 ea
= ether_aton(blobmsg_data(cur
));
200 memcpy(s
->macaddr
, ea
, 6);
201 s
->flags
|= DEV_OPT_MACADDR
;
205 if ((cur
= tb
[DEV_ATTR_IPV6
])) {
206 s
->ipv6
= blobmsg_get_bool(cur
);
207 s
->flags
|= DEV_OPT_IPV6
;
210 if ((cur
= tb
[DEV_ATTR_PROMISC
])) {
211 s
->promisc
= blobmsg_get_bool(cur
);
212 s
->flags
|= DEV_OPT_PROMISC
;
215 if ((cur
= tb
[DEV_ATTR_RPFILTER
])) {
216 if (system_resolve_rpfilter(blobmsg_data(cur
), &s
->rpfilter
))
217 s
->flags
|= DEV_OPT_RPFILTER
;
219 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur
));
222 if ((cur
= tb
[DEV_ATTR_ACCEPTLOCAL
])) {
223 s
->acceptlocal
= blobmsg_get_bool(cur
);
224 s
->flags
|= DEV_OPT_ACCEPTLOCAL
;
227 if ((cur
= tb
[DEV_ATTR_IGMPVERSION
])) {
228 s
->igmpversion
= blobmsg_get_u32(cur
);
229 if (s
->igmpversion
>= 1 && s
->igmpversion
<= 3)
230 s
->flags
|= DEV_OPT_IGMPVERSION
;
232 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur
));
235 if ((cur
= tb
[DEV_ATTR_MLDVERSION
])) {
236 s
->mldversion
= blobmsg_get_u32(cur
);
237 if (s
->mldversion
>= 1 && s
->mldversion
<= 2)
238 s
->flags
|= DEV_OPT_MLDVERSION
;
240 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur
));
243 if ((cur
= tb
[DEV_ATTR_NEIGHREACHABLETIME
])) {
244 s
->neigh6reachabletime
= s
->neigh4reachabletime
= blobmsg_get_u32(cur
);
245 s
->flags
|= DEV_OPT_NEIGHREACHABLETIME
;
248 if ((cur
= tb
[DEV_ATTR_RPS
]))
249 s
->rps
= blobmsg_get_bool(cur
);
253 if ((cur
= tb
[DEV_ATTR_XPS
]))
254 s
->xps
= blobmsg_get_bool(cur
);
258 device_set_disabled(dev
, disabled
);
261 static void __init
dev_init(void)
263 avl_init(&devices
, avl_strcmp
, true, NULL
);
266 static int device_broadcast_cb(void *ctx
, struct safe_list
*list
)
268 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
271 /* device might have been removed by an earlier callback */
280 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
284 safe_list_for_each(&dev
->aliases
, device_broadcast_cb
, &dev_ev
);
285 safe_list_for_each(&dev
->users
, device_broadcast_cb
, &dev_ev
);
288 int device_claim(struct device_user
*dep
)
290 struct device
*dev
= dep
->dev
;
297 D(DEVICE
, "Claim %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
298 if (++dev
->active
!= 1)
301 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
302 ret
= dev
->set_state(dev
, true);
304 device_broadcast_event(dev
, DEV_EVENT_UP
);
306 D(DEVICE
, "claim %s %s failed: %d\n", dev
->type
->name
, dev
->ifname
, ret
);
308 dep
->claimed
= false;
314 void device_release(struct device_user
*dep
)
316 struct device
*dev
= dep
->dev
;
321 dep
->claimed
= false;
323 D(DEVICE
, "Release %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
324 assert(dev
->active
>= 0);
329 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
331 dev
->set_state(dev
, false);
332 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
335 int device_check_state(struct device
*dev
)
337 if (!dev
->type
->check_state
)
338 return simple_device_type
.check_state(dev
);
340 return dev
->type
->check_state(dev
);
343 void device_init_virtual(struct device
*dev
, const struct device_type
*type
, const char *name
)
349 strncpy(dev
->ifname
, name
, IFNAMSIZ
);
351 D(DEVICE
, "Initialize device '%s'\n", dev
->ifname
);
352 INIT_SAFE_LIST(&dev
->users
);
353 INIT_SAFE_LIST(&dev
->aliases
);
357 dev
->set_state
= set_device_state
;
360 int device_init(struct device
*dev
, const struct device_type
*type
, const char *ifname
)
364 device_init_virtual(dev
, type
, ifname
);
366 dev
->avl
.key
= dev
->ifname
;
368 ret
= avl_insert(&devices
, &dev
->avl
);
372 system_if_clear_state(dev
);
373 device_check_state(dev
);
378 static struct device
*
379 device_create_default(const char *name
, bool external
)
383 if (!external
&& system_if_force_external(name
))
386 D(DEVICE
, "Create simple device '%s'\n", name
);
387 dev
= calloc(1, sizeof(*dev
));
388 dev
->external
= external
;
389 dev
->set_state
= simple_device_set_state
;
390 device_init(dev
, &simple_device_type
, name
);
391 dev
->default_config
= true;
396 device_get(const char *name
, int create
)
400 if (strchr(name
, '.'))
401 return get_vlan_device_chain(name
, create
);
404 return device_alias_get(name
+ 1);
406 dev
= avl_find_element(&devices
, name
, dev
, avl
);
408 if (create
> 1 && !dev
->external
) {
409 dev
->external
= true;
410 device_set_present(dev
, true);
418 return device_create_default(name
, create
> 1);
422 device_delete(struct device
*dev
)
427 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
428 avl_delete(&devices
, &dev
->avl
);
432 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
434 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
436 dep
->cb(dep
, DEV_EVENT_REMOVE
);
442 void device_cleanup(struct device
*dev
)
444 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
445 safe_list_for_each(&dev
->users
, device_cleanup_cb
, NULL
);
446 safe_list_for_each(&dev
->aliases
, device_cleanup_cb
, NULL
);
450 static void __device_set_present(struct device
*dev
, bool state
)
452 if (dev
->present
== state
)
455 dev
->present
= state
;
456 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
460 device_refresh_present(struct device
*dev
)
462 bool state
= dev
->sys_present
;
464 if (dev
->disabled
|| dev
->deferred
)
467 __device_set_present(dev
, state
);
470 void device_set_present(struct device
*dev
, bool state
)
472 if (dev
->sys_present
== state
)
475 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
476 dev
->sys_present
= state
;
477 device_refresh_present(dev
);
480 void device_set_link(struct device
*dev
, bool state
)
482 if (dev
->link_active
== state
)
485 netifd_log_message(L_NOTICE
, "%s '%s' link is %s\n", dev
->type
->name
, dev
->ifname
, state
? "up" : "down" );
487 dev
->link_active
= state
;
488 device_broadcast_event(dev
, state
? DEV_EVENT_LINK_UP
: DEV_EVENT_LINK_DOWN
);
491 void device_set_ifindex(struct device
*dev
, int ifindex
)
493 if (dev
->ifindex
== ifindex
)
496 dev
->ifindex
= ifindex
;
497 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFINDEX
);
500 static int device_refcount(struct device
*dev
)
502 struct list_head
*list
;
505 list_for_each(list
, &dev
->users
.list
)
508 list_for_each(list
, &dev
->aliases
.list
)
514 void device_add_user(struct device_user
*dep
, struct device
*dev
)
516 struct safe_list
*head
;
522 device_remove_user(dep
);
530 head
= &dev
->aliases
;
534 safe_list_add(&dep
->list
, head
);
535 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
537 if (dep
->cb
&& dev
->present
) {
538 dep
->cb(dep
, DEV_EVENT_ADD
);
540 dep
->cb(dep
, DEV_EVENT_UP
);
542 if (dev
->link_active
)
543 dep
->cb(dep
, DEV_EVENT_LINK_UP
);
548 device_free(struct device
*dev
)
553 dev
->type
->free(dev
);
558 __device_free_unused(struct device
*dev
)
560 if (!safe_list_empty(&dev
->users
) ||
561 !safe_list_empty(&dev
->aliases
) ||
562 dev
->current_config
|| __devlock
)
568 void device_remove_user(struct device_user
*dep
)
570 struct device
*dev
= dep
->dev
;
575 dep
->hotplug
= false;
579 safe_list_del(&dep
->list
);
581 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
582 __device_free_unused(dev
);
586 device_free_unused(struct device
*dev
)
591 return __device_free_unused(dev
);
593 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
594 __device_free_unused(dev
);
598 device_init_pending(void)
600 struct device
*dev
, *tmp
;
602 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
603 if (!dev
->config_pending
)
606 dev
->type
->config_init(dev
);
607 dev
->config_pending
= false;
611 static enum dev_change_type
612 device_set_config(struct device
*dev
, const struct device_type
*type
,
613 struct blob_attr
*attr
)
615 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
616 const struct uci_blob_param_list
*cfg
= type
->config_params
;
618 if (type
!= dev
->type
)
619 return DEV_CONFIG_RECREATE
;
621 if (dev
->type
->reload
)
622 return dev
->type
->reload(dev
, attr
);
624 if (uci_blob_check_equal(dev
->config
, attr
, cfg
))
625 return DEV_CONFIG_NO_CHANGE
;
627 if (cfg
== &device_attr_list
) {
628 memset(tb
, 0, sizeof(tb
));
631 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
632 blob_data(attr
), blob_len(attr
));
634 device_init_settings(dev
, tb
);
635 return DEV_CONFIG_RESTART
;
637 return DEV_CONFIG_RECREATE
;
641 device_apply_config(struct device
*dev
, const struct device_type
*type
,
642 struct blob_attr
*config
)
644 enum dev_change_type change
;
646 change
= device_set_config(dev
, type
, config
);
648 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
649 change
= DEV_CONFIG_APPLIED
;
653 case DEV_CONFIG_RESTART
:
654 case DEV_CONFIG_APPLIED
:
655 D(DEVICE
, "Device '%s': config applied\n", dev
->ifname
);
656 config
= blob_memdup(config
);
658 dev
->config
= config
;
659 if (change
== DEV_CONFIG_RESTART
&& dev
->present
) {
660 device_set_present(dev
, false);
661 if (dev
->active
&& !dev
->external
) {
662 dev
->set_state(dev
, false);
663 dev
->set_state(dev
, true);
665 device_set_present(dev
, true);
668 case DEV_CONFIG_NO_CHANGE
:
669 D(DEVICE
, "Device '%s': no configuration change\n", dev
->ifname
);
671 case DEV_CONFIG_RECREATE
:
679 device_replace(struct device
*dev
, struct device
*odev
)
681 struct device_user
*dep
, *tmp
;
682 bool present
= odev
->present
;
685 device_set_present(odev
, false);
687 list_for_each_entry_safe(dep
, tmp
, &odev
->users
.list
, list
.list
) {
689 safe_list_del(&dep
->list
);
690 safe_list_add(&dep
->list
, &dev
->users
);
696 device_set_present(dev
, true);
700 device_reset_config(void)
704 avl_for_each_element(&devices
, dev
, avl
)
705 dev
->current_config
= false;
709 device_reset_old(void)
711 struct device
*dev
, *tmp
, *ndev
;
713 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
714 if (dev
->current_config
|| dev
->default_config
)
717 if (dev
->type
!= &simple_device_type
)
720 ndev
= device_create_default(dev
->ifname
, dev
->external
);
721 device_replace(ndev
, dev
);
726 device_create(const char *name
, const struct device_type
*type
,
727 struct blob_attr
*config
)
729 struct device
*odev
= NULL
, *dev
;
730 enum dev_change_type change
;
732 odev
= device_get(name
, false);
734 odev
->current_config
= true;
735 change
= device_apply_config(odev
, type
, config
);
737 case DEV_CONFIG_RECREATE
:
738 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
745 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
747 config
= blob_memdup(config
);
751 dev
= type
->create(name
, config
);
755 dev
->current_config
= true;
756 dev
->config
= config
;
758 device_replace(dev
, odev
);
760 if (!config_init
&& dev
->config_pending
)
761 type
->config_init(dev
);
767 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
769 struct device_settings st
;
773 avl_for_each_element(&devices
, dev
, avl
) {
776 c
= blobmsg_open_table(b
, dev
->ifname
);
777 device_dump_status(b
, dev
);
778 blobmsg_close_table(b
, c
);
784 blobmsg_add_u8(b
, "external", dev
->external
);
785 blobmsg_add_u8(b
, "present", dev
->present
);
786 blobmsg_add_string(b
, "type", dev
->type
->name
);
791 blobmsg_add_u8(b
, "up", !!dev
->active
);
792 blobmsg_add_u8(b
, "carrier", !!dev
->link_active
);
794 if (dev
->type
->dump_info
)
795 dev
->type
->dump_info(dev
, b
);
797 system_if_dump_info(dev
, b
);
800 device_merge_settings(dev
, &st
);
801 if (st
.flags
& DEV_OPT_MTU
)
802 blobmsg_add_u32(b
, "mtu", st
.mtu
);
803 if (st
.flags
& DEV_OPT_MACADDR
)
804 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
805 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
806 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
807 if (st
.flags
& DEV_OPT_IPV6
)
808 blobmsg_add_u8(b
, "ipv6", st
.ipv6
);
809 if (st
.flags
& DEV_OPT_PROMISC
)
810 blobmsg_add_u8(b
, "promisc", st
.promisc
);
811 if (st
.flags
& DEV_OPT_RPFILTER
)
812 blobmsg_add_u32(b
, "rpfilter", st
.rpfilter
);
813 if (st
.flags
& DEV_OPT_ACCEPTLOCAL
)
814 blobmsg_add_u8(b
, "acceptlocal", st
.acceptlocal
);
815 if (st
.flags
& DEV_OPT_IGMPVERSION
)
816 blobmsg_add_u32(b
, "igmpversion", st
.igmpversion
);
817 if (st
.flags
& DEV_OPT_MLDVERSION
)
818 blobmsg_add_u32(b
, "mldversion", st
.mldversion
);
819 if (st
.flags
& DEV_OPT_NEIGHREACHABLETIME
) {
820 blobmsg_add_u32(b
, "neigh4reachabletime", st
.neigh4reachabletime
);
821 blobmsg_add_u32(b
, "neigh6reachabletime", st
.neigh6reachabletime
);
825 s
= blobmsg_open_table(b
, "statistics");
826 if (dev
->type
->dump_stats
)
827 dev
->type
->dump_stats(dev
, b
);
829 system_if_dump_stats(dev
, b
);
830 blobmsg_close_table(b
, s
);