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_MTU
] = { .name
= "mtu", .type
= BLOBMSG_TYPE_INT32
},
36 [DEV_ATTR_MACADDR
] = { .name
= "macaddr", .type
= BLOBMSG_TYPE_STRING
},
37 [DEV_ATTR_TXQUEUELEN
] = { .name
= "txqueuelen", .type
= BLOBMSG_TYPE_INT32
},
38 [DEV_ATTR_ENABLED
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_BOOL
},
39 [DEV_ATTR_IPV6
] = { .name
= "ipv6", .type
= BLOBMSG_TYPE_BOOL
},
40 [DEV_ATTR_PROMISC
] = { .name
= "promisc", .type
= BLOBMSG_TYPE_BOOL
},
41 [DEV_ATTR_RPFILTER
] = { .name
= "rpfilter", .type
= BLOBMSG_TYPE_STRING
},
42 [DEV_ATTR_ACCEPTLOCAL
] = { .name
= "acceptlocal", .type
= BLOBMSG_TYPE_BOOL
},
43 [DEV_ATTR_IGMPVERSION
] = { .name
= "igmpversion", .type
= BLOBMSG_TYPE_INT32
},
44 [DEV_ATTR_MLDVERSION
] = { .name
= "mldversion", .type
= BLOBMSG_TYPE_INT32
},
45 [DEV_ATTR_NEIGHREACHABLETIME
] = { .name
= "neighreachabletime", .type
= BLOBMSG_TYPE_INT32
},
46 [DEV_ATTR_RPS
] = { .name
= "rps", .type
= BLOBMSG_TYPE_BOOL
},
47 [DEV_ATTR_XPS
] = { .name
= "xps", .type
= BLOBMSG_TYPE_BOOL
},
50 const struct uci_blob_param_list device_attr_list
= {
51 .n_params
= __DEV_ATTR_MAX
,
55 static int __devlock
= 0;
57 void device_lock(void)
62 void device_unlock(void)
66 device_free_unused(NULL
);
69 static int set_device_state(struct device
*dev
, bool state
)
72 /* Set ifindex for all devices being enabled so a valid */
73 /* ifindex is in place avoiding possible race conditions */
74 device_set_ifindex(dev
, system_if_resolve(dev
));
91 simple_device_set_state(struct device
*dev
, bool state
)
96 pdev
= dev
->parent
.dev
;
98 pdev
= system_if_get_parent(dev
);
100 device_add_user(&dev
->parent
, pdev
);
105 ret
= device_claim(&dev
->parent
);
107 device_release(&dev
->parent
);
112 return set_device_state(dev
, state
);
115 static struct device
*
116 simple_device_create(const char *name
, struct blob_attr
*attr
)
118 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
119 struct device
*dev
= NULL
;
121 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
, blob_data(attr
), blob_len(attr
));
122 dev
= device_get(name
, true);
126 dev
->set_state
= simple_device_set_state
;
127 device_init_settings(dev
, tb
);
132 static void simple_device_free(struct device
*dev
)
135 device_remove_user(&dev
->parent
);
139 const struct device_type simple_device_type
= {
140 .name
= "Network device",
141 .config_params
= &device_attr_list
,
143 .create
= simple_device_create
,
144 .check_state
= system_if_check
,
145 .free
= simple_device_free
,
149 device_merge_settings(struct device
*dev
, struct device_settings
*n
)
151 struct device_settings
*os
= &dev
->orig_settings
;
152 struct device_settings
*s
= &dev
->settings
;
154 memset(n
, 0, sizeof(*n
));
155 n
->mtu
= s
->flags
& DEV_OPT_MTU
? s
->mtu
: os
->mtu
;
156 n
->txqueuelen
= s
->flags
& DEV_OPT_TXQUEUELEN
?
157 s
->txqueuelen
: os
->txqueuelen
;
159 (s
->flags
& DEV_OPT_MACADDR
? s
->macaddr
: os
->macaddr
),
161 n
->ipv6
= s
->flags
& DEV_OPT_IPV6
? s
->ipv6
: os
->ipv6
;
162 n
->promisc
= s
->flags
& DEV_OPT_PROMISC
? s
->promisc
: os
->promisc
;
163 n
->rpfilter
= s
->flags
& DEV_OPT_RPFILTER
? s
->rpfilter
: os
->rpfilter
;
164 n
->acceptlocal
= s
->flags
& DEV_OPT_ACCEPTLOCAL
? s
->acceptlocal
: os
->acceptlocal
;
165 n
->igmpversion
= s
->flags
& DEV_OPT_IGMPVERSION
? s
->igmpversion
: os
->igmpversion
;
166 n
->mldversion
= s
->flags
& DEV_OPT_MLDVERSION
? s
->mldversion
: os
->mldversion
;
167 n
->neigh4reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
168 s
->neigh4reachabletime
: os
->neigh4reachabletime
;
169 n
->neigh6reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
170 s
->neigh6reachabletime
: os
->neigh6reachabletime
;
171 n
->flags
= s
->flags
| os
->flags
;
175 device_init_settings(struct device
*dev
, struct blob_attr
**tb
)
177 struct device_settings
*s
= &dev
->settings
;
178 struct blob_attr
*cur
;
179 struct ether_addr
*ea
;
180 bool disabled
= false;
183 if ((cur
= tb
[DEV_ATTR_ENABLED
]))
184 disabled
= !blobmsg_get_bool(cur
);
186 if ((cur
= tb
[DEV_ATTR_MTU
])) {
187 s
->mtu
= blobmsg_get_u32(cur
);
188 s
->flags
|= DEV_OPT_MTU
;
191 if ((cur
= tb
[DEV_ATTR_TXQUEUELEN
])) {
192 s
->txqueuelen
= blobmsg_get_u32(cur
);
193 s
->flags
|= DEV_OPT_TXQUEUELEN
;
196 if ((cur
= tb
[DEV_ATTR_MACADDR
])) {
197 ea
= ether_aton(blobmsg_data(cur
));
199 memcpy(s
->macaddr
, ea
, 6);
200 s
->flags
|= DEV_OPT_MACADDR
;
204 if ((cur
= tb
[DEV_ATTR_IPV6
])) {
205 s
->ipv6
= blobmsg_get_bool(cur
);
206 s
->flags
|= DEV_OPT_IPV6
;
209 if ((cur
= tb
[DEV_ATTR_PROMISC
])) {
210 s
->promisc
= blobmsg_get_bool(cur
);
211 s
->flags
|= DEV_OPT_PROMISC
;
214 if ((cur
= tb
[DEV_ATTR_RPFILTER
])) {
215 if (system_resolve_rpfilter(blobmsg_data(cur
), &s
->rpfilter
))
216 s
->flags
|= DEV_OPT_RPFILTER
;
218 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur
));
221 if ((cur
= tb
[DEV_ATTR_ACCEPTLOCAL
])) {
222 s
->acceptlocal
= blobmsg_get_bool(cur
);
223 s
->flags
|= DEV_OPT_ACCEPTLOCAL
;
226 if ((cur
= tb
[DEV_ATTR_IGMPVERSION
])) {
227 s
->igmpversion
= blobmsg_get_u32(cur
);
228 if (s
->igmpversion
>= 1 && s
->igmpversion
<= 3)
229 s
->flags
|= DEV_OPT_IGMPVERSION
;
231 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur
));
234 if ((cur
= tb
[DEV_ATTR_MLDVERSION
])) {
235 s
->mldversion
= blobmsg_get_u32(cur
);
236 if (s
->mldversion
>= 1 && s
->mldversion
<= 2)
237 s
->flags
|= DEV_OPT_MLDVERSION
;
239 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur
));
242 if ((cur
= tb
[DEV_ATTR_NEIGHREACHABLETIME
])) {
243 s
->neigh6reachabletime
= s
->neigh4reachabletime
= blobmsg_get_u32(cur
);
244 s
->flags
|= DEV_OPT_NEIGHREACHABLETIME
;
247 if ((cur
= tb
[DEV_ATTR_RPS
]))
248 s
->rps
= blobmsg_get_bool(cur
);
252 if ((cur
= tb
[DEV_ATTR_XPS
]))
253 s
->xps
= blobmsg_get_bool(cur
);
257 device_set_disabled(dev
, disabled
);
260 static void __init
dev_init(void)
262 avl_init(&devices
, avl_strcmp
, true, NULL
);
265 static int device_broadcast_cb(void *ctx
, struct safe_list
*list
)
267 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
270 /* device might have been removed by an earlier callback */
279 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
283 safe_list_for_each(&dev
->aliases
, device_broadcast_cb
, &dev_ev
);
284 safe_list_for_each(&dev
->users
, device_broadcast_cb
, &dev_ev
);
287 int device_claim(struct device_user
*dep
)
289 struct device
*dev
= dep
->dev
;
296 D(DEVICE
, "Claim %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
297 if (++dev
->active
!= 1)
300 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
301 ret
= dev
->set_state(dev
, true);
303 device_broadcast_event(dev
, DEV_EVENT_UP
);
305 D(DEVICE
, "claim %s %s failed: %d\n", dev
->type
->name
, dev
->ifname
, ret
);
307 dep
->claimed
= false;
313 void device_release(struct device_user
*dep
)
315 struct device
*dev
= dep
->dev
;
320 dep
->claimed
= false;
322 D(DEVICE
, "Release %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
323 assert(dev
->active
>= 0);
328 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
330 dev
->set_state(dev
, false);
331 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
334 int device_check_state(struct device
*dev
)
336 if (!dev
->type
->check_state
)
337 return simple_device_type
.check_state(dev
);
339 return dev
->type
->check_state(dev
);
342 void device_init_virtual(struct device
*dev
, const struct device_type
*type
, const char *name
)
348 strncpy(dev
->ifname
, name
, IFNAMSIZ
);
350 D(DEVICE
, "Initialize device '%s'\n", dev
->ifname
);
351 INIT_SAFE_LIST(&dev
->users
);
352 INIT_SAFE_LIST(&dev
->aliases
);
356 dev
->set_state
= set_device_state
;
359 int device_init(struct device
*dev
, const struct device_type
*type
, const char *ifname
)
363 device_init_virtual(dev
, type
, ifname
);
365 dev
->avl
.key
= dev
->ifname
;
367 ret
= avl_insert(&devices
, &dev
->avl
);
371 system_if_clear_state(dev
);
372 device_check_state(dev
);
373 dev
->settings
.rps
= true;
374 dev
->settings
.xps
= true;
379 static struct device
*
380 device_create_default(const char *name
, bool external
)
384 if (!external
&& system_if_force_external(name
))
387 D(DEVICE
, "Create simple device '%s'\n", name
);
388 dev
= calloc(1, sizeof(*dev
));
389 dev
->external
= external
;
390 dev
->set_state
= simple_device_set_state
;
391 device_init(dev
, &simple_device_type
, name
);
392 dev
->default_config
= true;
397 device_get(const char *name
, int create
)
401 if (strchr(name
, '.'))
402 return get_vlan_device_chain(name
, create
);
405 return device_alias_get(name
+ 1);
407 dev
= avl_find_element(&devices
, name
, dev
, avl
);
409 if (create
> 1 && !dev
->external
) {
410 dev
->external
= true;
411 device_set_present(dev
, true);
419 return device_create_default(name
, create
> 1);
423 device_delete(struct device
*dev
)
428 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
429 avl_delete(&devices
, &dev
->avl
);
433 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
435 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
437 dep
->cb(dep
, DEV_EVENT_REMOVE
);
443 void device_cleanup(struct device
*dev
)
445 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
446 safe_list_for_each(&dev
->users
, device_cleanup_cb
, NULL
);
447 safe_list_for_each(&dev
->aliases
, device_cleanup_cb
, NULL
);
451 static void __device_set_present(struct device
*dev
, bool state
)
453 if (dev
->present
== state
)
456 dev
->present
= state
;
457 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
461 device_refresh_present(struct device
*dev
)
463 bool state
= dev
->sys_present
;
465 if (dev
->disabled
|| dev
->deferred
)
468 __device_set_present(dev
, state
);
471 void device_set_present(struct device
*dev
, bool state
)
473 if (dev
->sys_present
== state
)
476 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
477 dev
->sys_present
= state
;
478 device_refresh_present(dev
);
481 void device_set_link(struct device
*dev
, bool state
)
483 if (dev
->link_active
== state
)
486 netifd_log_message(L_NOTICE
, "%s '%s' link is %s\n", dev
->type
->name
, dev
->ifname
, state
? "up" : "down" );
488 dev
->link_active
= state
;
489 device_broadcast_event(dev
, state
? DEV_EVENT_LINK_UP
: DEV_EVENT_LINK_DOWN
);
492 void device_set_ifindex(struct device
*dev
, int ifindex
)
494 if (dev
->ifindex
== ifindex
)
497 dev
->ifindex
= ifindex
;
498 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFINDEX
);
501 static int device_refcount(struct device
*dev
)
503 struct list_head
*list
;
506 list_for_each(list
, &dev
->users
.list
)
509 list_for_each(list
, &dev
->aliases
.list
)
515 void device_add_user(struct device_user
*dep
, struct device
*dev
)
517 struct safe_list
*head
;
523 device_remove_user(dep
);
531 head
= &dev
->aliases
;
535 safe_list_add(&dep
->list
, head
);
536 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
538 if (dep
->cb
&& dev
->present
) {
539 dep
->cb(dep
, DEV_EVENT_ADD
);
541 dep
->cb(dep
, DEV_EVENT_UP
);
543 if (dev
->link_active
)
544 dep
->cb(dep
, DEV_EVENT_LINK_UP
);
549 device_free(struct device
*dev
)
554 dev
->type
->free(dev
);
559 __device_free_unused(struct device
*dev
)
561 if (!safe_list_empty(&dev
->users
) ||
562 !safe_list_empty(&dev
->aliases
) ||
563 dev
->current_config
|| __devlock
)
569 void device_remove_user(struct device_user
*dep
)
571 struct device
*dev
= dep
->dev
;
576 dep
->hotplug
= false;
580 safe_list_del(&dep
->list
);
582 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
583 __device_free_unused(dev
);
587 device_free_unused(struct device
*dev
)
592 return __device_free_unused(dev
);
594 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
595 __device_free_unused(dev
);
599 device_init_pending(void)
601 struct device
*dev
, *tmp
;
603 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
604 if (!dev
->config_pending
)
607 dev
->type
->config_init(dev
);
608 dev
->config_pending
= false;
612 static enum dev_change_type
613 device_set_config(struct device
*dev
, const struct device_type
*type
,
614 struct blob_attr
*attr
)
616 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
617 const struct uci_blob_param_list
*cfg
= type
->config_params
;
619 if (type
!= dev
->type
)
620 return DEV_CONFIG_RECREATE
;
622 if (dev
->type
->reload
)
623 return dev
->type
->reload(dev
, attr
);
625 if (uci_blob_check_equal(dev
->config
, attr
, cfg
))
626 return DEV_CONFIG_NO_CHANGE
;
628 if (cfg
== &device_attr_list
) {
629 memset(tb
, 0, sizeof(tb
));
632 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
633 blob_data(attr
), blob_len(attr
));
635 device_init_settings(dev
, tb
);
636 return DEV_CONFIG_RESTART
;
638 return DEV_CONFIG_RECREATE
;
642 device_apply_config(struct device
*dev
, const struct device_type
*type
,
643 struct blob_attr
*config
)
645 enum dev_change_type change
;
647 change
= device_set_config(dev
, type
, config
);
649 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
650 change
= DEV_CONFIG_APPLIED
;
654 case DEV_CONFIG_RESTART
:
655 case DEV_CONFIG_APPLIED
:
656 D(DEVICE
, "Device '%s': config applied\n", dev
->ifname
);
657 config
= blob_memdup(config
);
659 dev
->config
= config
;
660 if (change
== DEV_CONFIG_RESTART
&& dev
->present
) {
661 device_set_present(dev
, false);
662 if (dev
->active
&& !dev
->external
) {
663 dev
->set_state(dev
, false);
664 dev
->set_state(dev
, true);
666 device_set_present(dev
, true);
669 case DEV_CONFIG_NO_CHANGE
:
670 D(DEVICE
, "Device '%s': no configuration change\n", dev
->ifname
);
672 case DEV_CONFIG_RECREATE
:
680 device_replace(struct device
*dev
, struct device
*odev
)
682 struct device_user
*dep
, *tmp
;
683 bool present
= odev
->present
;
686 device_set_present(odev
, false);
688 list_for_each_entry_safe(dep
, tmp
, &odev
->users
.list
, list
.list
) {
690 safe_list_del(&dep
->list
);
691 safe_list_add(&dep
->list
, &dev
->users
);
697 device_set_present(dev
, true);
701 device_reset_config(void)
705 avl_for_each_element(&devices
, dev
, avl
)
706 dev
->current_config
= false;
710 device_reset_old(void)
712 struct device
*dev
, *tmp
, *ndev
;
714 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
715 if (dev
->current_config
|| dev
->default_config
)
718 if (dev
->type
!= &simple_device_type
)
721 ndev
= device_create_default(dev
->ifname
, dev
->external
);
722 device_replace(ndev
, dev
);
727 device_create(const char *name
, const struct device_type
*type
,
728 struct blob_attr
*config
)
730 struct device
*odev
= NULL
, *dev
;
731 enum dev_change_type change
;
733 odev
= device_get(name
, false);
735 odev
->current_config
= true;
736 change
= device_apply_config(odev
, type
, config
);
738 case DEV_CONFIG_RECREATE
:
739 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
746 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
748 config
= blob_memdup(config
);
752 dev
= type
->create(name
, config
);
756 dev
->current_config
= true;
757 dev
->config
= config
;
759 device_replace(dev
, odev
);
761 if (!config_init
&& dev
->config_pending
)
762 type
->config_init(dev
);
768 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
770 struct device_settings st
;
774 avl_for_each_element(&devices
, dev
, avl
) {
777 c
= blobmsg_open_table(b
, dev
->ifname
);
778 device_dump_status(b
, dev
);
779 blobmsg_close_table(b
, c
);
785 blobmsg_add_u8(b
, "external", dev
->external
);
786 blobmsg_add_u8(b
, "present", dev
->present
);
787 blobmsg_add_string(b
, "type", dev
->type
->name
);
792 blobmsg_add_u8(b
, "up", !!dev
->active
);
793 blobmsg_add_u8(b
, "carrier", !!dev
->link_active
);
795 if (dev
->type
->dump_info
)
796 dev
->type
->dump_info(dev
, b
);
798 system_if_dump_info(dev
, b
);
801 device_merge_settings(dev
, &st
);
802 if (st
.flags
& DEV_OPT_MTU
)
803 blobmsg_add_u32(b
, "mtu", st
.mtu
);
804 if (st
.flags
& DEV_OPT_MACADDR
)
805 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
806 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
807 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
808 if (st
.flags
& DEV_OPT_IPV6
)
809 blobmsg_add_u8(b
, "ipv6", st
.ipv6
);
810 if (st
.flags
& DEV_OPT_PROMISC
)
811 blobmsg_add_u8(b
, "promisc", st
.promisc
);
812 if (st
.flags
& DEV_OPT_RPFILTER
)
813 blobmsg_add_u32(b
, "rpfilter", st
.rpfilter
);
814 if (st
.flags
& DEV_OPT_ACCEPTLOCAL
)
815 blobmsg_add_u8(b
, "acceptlocal", st
.acceptlocal
);
816 if (st
.flags
& DEV_OPT_IGMPVERSION
)
817 blobmsg_add_u32(b
, "igmpversion", st
.igmpversion
);
818 if (st
.flags
& DEV_OPT_MLDVERSION
)
819 blobmsg_add_u32(b
, "mldversion", st
.mldversion
);
820 if (st
.flags
& DEV_OPT_NEIGHREACHABLETIME
) {
821 blobmsg_add_u32(b
, "neigh4reachabletime", st
.neigh4reachabletime
);
822 blobmsg_add_u32(b
, "neigh6reachabletime", st
.neigh6reachabletime
);
826 s
= blobmsg_open_table(b
, "statistics");
827 if (dev
->type
->dump_stats
)
828 dev
->type
->dump_stats(dev
, b
);
830 system_if_dump_stats(dev
, b
);
831 blobmsg_close_table(b
, s
);