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
);
374 dev
->settings
.rps
= true;
375 dev
->settings
.xps
= true;
380 static struct device
*
381 device_create_default(const char *name
, bool external
)
385 if (!external
&& system_if_force_external(name
))
388 D(DEVICE
, "Create simple device '%s'\n", name
);
389 dev
= calloc(1, sizeof(*dev
));
390 dev
->external
= external
;
391 dev
->set_state
= simple_device_set_state
;
392 device_init(dev
, &simple_device_type
, name
);
393 dev
->default_config
= true;
398 device_get(const char *name
, int create
)
402 if (strchr(name
, '.'))
403 return get_vlan_device_chain(name
, create
);
406 return device_alias_get(name
+ 1);
408 dev
= avl_find_element(&devices
, name
, dev
, avl
);
410 if (create
> 1 && !dev
->external
) {
411 dev
->external
= true;
412 device_set_present(dev
, true);
420 return device_create_default(name
, create
> 1);
424 device_delete(struct device
*dev
)
429 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
430 avl_delete(&devices
, &dev
->avl
);
434 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
436 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
438 dep
->cb(dep
, DEV_EVENT_REMOVE
);
444 void device_cleanup(struct device
*dev
)
446 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
447 safe_list_for_each(&dev
->users
, device_cleanup_cb
, NULL
);
448 safe_list_for_each(&dev
->aliases
, device_cleanup_cb
, NULL
);
452 static void __device_set_present(struct device
*dev
, bool state
)
454 if (dev
->present
== state
)
457 dev
->present
= state
;
458 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
462 device_refresh_present(struct device
*dev
)
464 bool state
= dev
->sys_present
;
466 if (dev
->disabled
|| dev
->deferred
)
469 __device_set_present(dev
, state
);
472 void device_set_present(struct device
*dev
, bool state
)
474 if (dev
->sys_present
== state
)
477 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
478 dev
->sys_present
= state
;
479 device_refresh_present(dev
);
482 void device_set_link(struct device
*dev
, bool state
)
484 if (dev
->link_active
== state
)
487 netifd_log_message(L_NOTICE
, "%s '%s' link is %s\n", dev
->type
->name
, dev
->ifname
, state
? "up" : "down" );
489 dev
->link_active
= state
;
490 device_broadcast_event(dev
, state
? DEV_EVENT_LINK_UP
: DEV_EVENT_LINK_DOWN
);
493 void device_set_ifindex(struct device
*dev
, int ifindex
)
495 if (dev
->ifindex
== ifindex
)
498 dev
->ifindex
= ifindex
;
499 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFINDEX
);
502 static int device_refcount(struct device
*dev
)
504 struct list_head
*list
;
507 list_for_each(list
, &dev
->users
.list
)
510 list_for_each(list
, &dev
->aliases
.list
)
516 void device_add_user(struct device_user
*dep
, struct device
*dev
)
518 struct safe_list
*head
;
524 device_remove_user(dep
);
532 head
= &dev
->aliases
;
536 safe_list_add(&dep
->list
, head
);
537 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
539 if (dep
->cb
&& dev
->present
) {
540 dep
->cb(dep
, DEV_EVENT_ADD
);
542 dep
->cb(dep
, DEV_EVENT_UP
);
544 if (dev
->link_active
)
545 dep
->cb(dep
, DEV_EVENT_LINK_UP
);
550 device_free(struct device
*dev
)
555 dev
->type
->free(dev
);
560 __device_free_unused(struct device
*dev
)
562 if (!safe_list_empty(&dev
->users
) ||
563 !safe_list_empty(&dev
->aliases
) ||
564 dev
->current_config
|| __devlock
)
570 void device_remove_user(struct device_user
*dep
)
572 struct device
*dev
= dep
->dev
;
577 dep
->hotplug
= false;
581 safe_list_del(&dep
->list
);
583 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
584 __device_free_unused(dev
);
588 device_free_unused(struct device
*dev
)
593 return __device_free_unused(dev
);
595 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
596 __device_free_unused(dev
);
600 device_init_pending(void)
602 struct device
*dev
, *tmp
;
604 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
605 if (!dev
->config_pending
)
608 dev
->type
->config_init(dev
);
609 dev
->config_pending
= false;
613 static enum dev_change_type
614 device_set_config(struct device
*dev
, const struct device_type
*type
,
615 struct blob_attr
*attr
)
617 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
618 const struct uci_blob_param_list
*cfg
= type
->config_params
;
620 if (type
!= dev
->type
)
621 return DEV_CONFIG_RECREATE
;
623 if (dev
->type
->reload
)
624 return dev
->type
->reload(dev
, attr
);
626 if (uci_blob_check_equal(dev
->config
, attr
, cfg
))
627 return DEV_CONFIG_NO_CHANGE
;
629 if (cfg
== &device_attr_list
) {
630 memset(tb
, 0, sizeof(tb
));
633 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
634 blob_data(attr
), blob_len(attr
));
636 device_init_settings(dev
, tb
);
637 return DEV_CONFIG_RESTART
;
639 return DEV_CONFIG_RECREATE
;
643 device_apply_config(struct device
*dev
, const struct device_type
*type
,
644 struct blob_attr
*config
)
646 enum dev_change_type change
;
648 change
= device_set_config(dev
, type
, config
);
650 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
651 change
= DEV_CONFIG_APPLIED
;
655 case DEV_CONFIG_RESTART
:
656 case DEV_CONFIG_APPLIED
:
657 D(DEVICE
, "Device '%s': config applied\n", dev
->ifname
);
658 config
= blob_memdup(config
);
660 dev
->config
= config
;
661 if (change
== DEV_CONFIG_RESTART
&& dev
->present
) {
662 device_set_present(dev
, false);
663 if (dev
->active
&& !dev
->external
) {
664 dev
->set_state(dev
, false);
665 dev
->set_state(dev
, true);
667 device_set_present(dev
, true);
670 case DEV_CONFIG_NO_CHANGE
:
671 D(DEVICE
, "Device '%s': no configuration change\n", dev
->ifname
);
673 case DEV_CONFIG_RECREATE
:
681 device_replace(struct device
*dev
, struct device
*odev
)
683 struct device_user
*dep
, *tmp
;
684 bool present
= odev
->present
;
687 device_set_present(odev
, false);
689 list_for_each_entry_safe(dep
, tmp
, &odev
->users
.list
, list
.list
) {
691 safe_list_del(&dep
->list
);
692 safe_list_add(&dep
->list
, &dev
->users
);
698 device_set_present(dev
, true);
702 device_reset_config(void)
706 avl_for_each_element(&devices
, dev
, avl
)
707 dev
->current_config
= false;
711 device_reset_old(void)
713 struct device
*dev
, *tmp
, *ndev
;
715 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
716 if (dev
->current_config
|| dev
->default_config
)
719 if (dev
->type
!= &simple_device_type
)
722 ndev
= device_create_default(dev
->ifname
, dev
->external
);
723 device_replace(ndev
, dev
);
728 device_create(const char *name
, const struct device_type
*type
,
729 struct blob_attr
*config
)
731 struct device
*odev
= NULL
, *dev
;
732 enum dev_change_type change
;
734 odev
= device_get(name
, false);
736 odev
->current_config
= true;
737 change
= device_apply_config(odev
, type
, config
);
739 case DEV_CONFIG_RECREATE
:
740 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
747 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
749 config
= blob_memdup(config
);
753 dev
= type
->create(name
, config
);
757 dev
->current_config
= true;
758 dev
->config
= config
;
760 device_replace(dev
, odev
);
762 if (!config_init
&& dev
->config_pending
)
763 type
->config_init(dev
);
769 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
771 struct device_settings st
;
775 avl_for_each_element(&devices
, dev
, avl
) {
778 c
= blobmsg_open_table(b
, dev
->ifname
);
779 device_dump_status(b
, dev
);
780 blobmsg_close_table(b
, c
);
786 blobmsg_add_u8(b
, "external", dev
->external
);
787 blobmsg_add_u8(b
, "present", dev
->present
);
788 blobmsg_add_string(b
, "type", dev
->type
->name
);
793 blobmsg_add_u8(b
, "up", !!dev
->active
);
794 blobmsg_add_u8(b
, "carrier", !!dev
->link_active
);
796 if (dev
->type
->dump_info
)
797 dev
->type
->dump_info(dev
, b
);
799 system_if_dump_info(dev
, b
);
802 device_merge_settings(dev
, &st
);
803 if (st
.flags
& DEV_OPT_MTU
)
804 blobmsg_add_u32(b
, "mtu", st
.mtu
);
805 if (st
.flags
& DEV_OPT_MACADDR
)
806 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
807 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
808 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
809 if (st
.flags
& DEV_OPT_IPV6
)
810 blobmsg_add_u8(b
, "ipv6", st
.ipv6
);
811 if (st
.flags
& DEV_OPT_PROMISC
)
812 blobmsg_add_u8(b
, "promisc", st
.promisc
);
813 if (st
.flags
& DEV_OPT_RPFILTER
)
814 blobmsg_add_u32(b
, "rpfilter", st
.rpfilter
);
815 if (st
.flags
& DEV_OPT_ACCEPTLOCAL
)
816 blobmsg_add_u8(b
, "acceptlocal", st
.acceptlocal
);
817 if (st
.flags
& DEV_OPT_IGMPVERSION
)
818 blobmsg_add_u32(b
, "igmpversion", st
.igmpversion
);
819 if (st
.flags
& DEV_OPT_MLDVERSION
)
820 blobmsg_add_u32(b
, "mldversion", st
.mldversion
);
821 if (st
.flags
& DEV_OPT_NEIGHREACHABLETIME
) {
822 blobmsg_add_u32(b
, "neigh4reachabletime", st
.neigh4reachabletime
);
823 blobmsg_add_u32(b
, "neigh6reachabletime", st
.neigh6reachabletime
);
827 s
= blobmsg_open_table(b
, "statistics");
828 if (dev
->type
->dump_stats
)
829 dev
->type
->dump_stats(dev
, b
);
831 system_if_dump_stats(dev
, b
);
832 blobmsg_close_table(b
, s
);