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.
20 #include "interface.h"
21 #include "interface-ip.h"
27 struct vlist_tree interfaces;
28 static LIST_HEAD(iface_all_users);
34 IFACE_ATTR_DEFAULTROUTE,
37 IFACE_ATTR_DNS_SEARCH,
46 IFACE_ATTR_IP6IFACEID,
47 IFACE_ATTR_FORCE_LINK,
51 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
52 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
53 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
54 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
55 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
56 [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
57 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
58 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
59 [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
60 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
61 [IFACE_ATTR_IP6ASSIGN] = { .name = "ip6assign", .type = BLOBMSG_TYPE_INT32 },
62 [IFACE_ATTR_IP6HINT] = { .name = "ip6hint", .type = BLOBMSG_TYPE_STRING },
63 [IFACE_ATTR_IP4TABLE] = { .name = "ip4table", .type = BLOBMSG_TYPE_STRING },
64 [IFACE_ATTR_IP6TABLE] = { .name = "ip6table", .type = BLOBMSG_TYPE_STRING },
65 [IFACE_ATTR_IP6CLASS] = { .name = "ip6class", .type = BLOBMSG_TYPE_ARRAY },
66 [IFACE_ATTR_DELEGATE] = { .name = "delegate", .type = BLOBMSG_TYPE_BOOL },
67 [IFACE_ATTR_IP6IFACEID] = { .name = "ip6ifaceid", .type = BLOBMSG_TYPE_STRING },
68 [IFACE_ATTR_FORCE_LINK] = { .name = "force_link", .type = BLOBMSG_TYPE_BOOL },
71 const struct uci_blob_param_list interface_attr_list = {
72 .n_params = IFACE_ATTR_MAX,
73 .params = iface_attrs,
77 set_config_state(struct interface *iface, enum interface_config_state s);
79 interface_event(struct interface *iface, enum interface_event ev);
82 interface_error_flush(struct interface *iface)
84 struct interface_error *error, *tmp;
86 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
87 list_del(&error->list);
93 interface_clear_errors(struct interface *iface)
95 /* don't flush the errors in case the configured protocol handler matches the
96 running protocol handler and is having the last error capability */
98 (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
99 (iface->proto->handler->name == iface->proto_handler->name)))
100 interface_error_flush(iface);
103 void interface_add_error(struct interface *iface, const char *subsystem,
104 const char *code, const char **data, int n_data)
106 struct interface_error *error;
109 char *dest, *d_subsys, *d_code;
111 /* if the configured protocol handler has the last error support capability,
112 errors should only be added if the running protocol handler matches the
115 (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
116 (iface->proto->handler->name != iface->proto_handler->name))
120 len = n_data * sizeof(char *);
121 datalen = alloca(len);
122 for (i = 0; i < n_data; i++) {
123 datalen[i] = strlen(data[i]) + 1;
128 error = calloc_a(sizeof(*error) + sizeof(char *) + len,
129 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
130 &d_code, code ? strlen(code) + 1 : 0);
134 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
135 protocol can't be established (e.g auth failure) */
136 if (iface->proto_handler->flags & PROTO_FLAG_LASTERROR)
137 interface_error_flush(iface);
139 list_add_tail(&error->list, &iface->errors);
141 dest = (char *) &error->data[n_data + 1];
142 for (i = 0; i < n_data; i++) {
143 error->data[i] = dest;
144 memcpy(dest, data[i], datalen[i]);
147 error->data[n_data] = NULL;
150 error->subsystem = strcpy(d_subsys, subsystem);
153 error->code = strcpy(d_code, code);
157 interface_data_del(struct interface *iface, struct interface_data *data)
159 avl_delete(&iface->data, &data->node);
164 interface_data_flush(struct interface *iface)
166 struct interface_data *d, *tmp;
168 avl_for_each_element_safe(&iface->data, d, node, tmp)
169 interface_data_del(iface, d);
173 interface_add_data(struct interface *iface, const struct blob_attr *data)
175 struct interface_data *n, *o;
177 if (!blobmsg_check_attr(data, true))
178 return UBUS_STATUS_INVALID_ARGUMENT;
180 const char *name = blobmsg_name(data);
181 unsigned len = blob_pad_len(data);
183 o = avl_find_element(&iface->data, name, o, node);
185 if (blob_pad_len(o->data) == len && !memcmp(o->data, data, len))
188 interface_data_del(iface, o);
191 n = calloc(1, sizeof(*n) + len);
193 return UBUS_STATUS_UNKNOWN_ERROR;
195 memcpy(n->data, data, len);
196 n->node.key = blobmsg_name(n->data);
197 avl_insert(&iface->data, &n->node);
199 iface->updated |= IUF_DATA;
203 int interface_parse_data(struct interface *iface, const struct blob_attr *attr)
205 struct blob_attr *cur;
210 blob_for_each_attr(cur, attr, rem) {
211 ret = interface_add_data(iface, cur);
216 if (iface->updated && iface->state == IFS_UP)
217 interface_event(iface, IFEV_UPDATE);
223 interface_event(struct interface *iface, enum interface_event ev)
225 struct interface_user *dep, *tmp;
226 struct device *adev = NULL;
228 list_for_each_entry_safe(dep, tmp, &iface->users, list)
229 dep->cb(dep, iface, ev);
231 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
232 dep->cb(dep, iface, ev);
236 interface_error_flush(iface);
237 adev = iface->l3_dev.dev;
240 alias_notify_device(iface->name, adev);
248 interface_flush_state(struct interface *iface)
250 if (iface->l3_dev.dev)
251 device_release(&iface->l3_dev);
252 interface_data_flush(iface);
256 mark_interface_down(struct interface *iface)
258 enum interface_state state = iface->state;
260 if (state == IFS_DOWN)
263 iface->link_up_event = false;
264 iface->state = IFS_DOWN;
266 interface_event(iface, IFEV_DOWN);
267 interface_ip_set_enabled(&iface->config_ip, false);
268 interface_ip_set_enabled(&iface->proto_ip, false);
269 interface_ip_flush(&iface->proto_ip);
270 interface_flush_state(iface);
271 system_flush_routes();
275 __interface_set_down(struct interface *iface, bool force)
277 enum interface_state state = iface->state;
281 iface->state = IFS_TEARDOWN;
283 interface_event(iface, IFEV_DOWN);
285 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
287 interface_flush_state(iface);
291 if (iface->main_dev.dev)
292 device_release(&iface->main_dev);
300 __interface_set_up(struct interface *iface)
304 netifd_log_message(L_NOTICE, "Interface '%s' is setting up now\n", iface->name);
306 iface->state = IFS_SETUP;
307 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
309 mark_interface_down(iface);
315 interface_check_state(struct interface *iface)
317 bool link_state = iface->link_state || iface->force_link;
319 switch (iface->state) {
322 if (!iface->enabled || !link_state) {
323 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
324 mark_interface_down(iface);
328 if (!iface->available)
331 if (iface->autostart && iface->enabled && link_state && !config_init)
332 __interface_set_up(iface);
340 interface_set_enabled(struct interface *iface, bool new_state)
342 if (iface->enabled == new_state)
345 netifd_log_message(L_NOTICE, "Interface '%s' is %s\n", iface->name, new_state ? "enabled" : "disabled");
346 iface->enabled = new_state;
347 interface_check_state(iface);
351 interface_set_link_state(struct interface *iface, bool new_state)
353 if (iface->link_state == new_state)
356 netifd_log_message(L_NOTICE, "Interface '%s' has link connectivity %s\n", iface->name, new_state ? "" : "loss");
357 iface->link_state = new_state;
358 interface_check_state(iface);
360 if (new_state && iface->force_link && iface->state == IFS_UP && !iface->link_up_event) {
361 interface_event(iface, IFEV_LINK_UP);
362 iface->link_up_event = true;
367 interface_ext_dev_cb(struct device_user *dep, enum device_event ev)
369 if (ev == DEV_EVENT_REMOVE)
370 device_remove_user(dep);
374 interface_main_dev_cb(struct device_user *dep, enum device_event ev)
376 struct interface *iface;
377 bool new_state = false;
379 iface = container_of(dep, struct interface, main_dev);
383 case DEV_EVENT_REMOVE:
384 interface_set_available(iface, new_state);
385 if (!new_state && dep->dev && dep->dev->external)
386 interface_set_main_dev(iface, NULL);
391 interface_set_enabled(iface, new_state);
393 case DEV_EVENT_LINK_UP:
395 case DEV_EVENT_LINK_DOWN:
396 interface_set_link_state(iface, new_state);
398 case DEV_EVENT_TOPO_CHANGE:
399 interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
407 interface_l3_dev_cb(struct device_user *dep, enum device_event ev)
409 struct interface *iface;
411 iface = container_of(dep, struct interface, l3_dev);
412 if (iface->l3_dev.dev == iface->main_dev.dev)
416 case DEV_EVENT_LINK_DOWN:
417 if (iface->proto_handler->flags & PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN)
418 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
426 interface_set_available(struct interface *iface, bool new_state)
428 if (iface->available == new_state)
431 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
432 iface->available = new_state;
435 if (iface->autostart && !config_init)
436 interface_set_up(iface);
438 __interface_set_down(iface, true);
442 interface_add_user(struct interface_user *dep, struct interface *iface)
445 list_add(&dep->list, &iface_all_users);
450 list_add(&dep->list, &iface->users);
451 if (iface->state == IFS_UP)
452 dep->cb(dep, iface, IFEV_UP);
456 interface_remove_user(struct interface_user *dep)
458 list_del_init(&dep->list);
463 interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
465 struct blob_attr *cur;
468 blobmsg_for_each_attr(cur, list, rem) {
469 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
472 if (!blobmsg_check_attr(cur, NULL))
475 struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
476 memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
477 list_add(&c->head, &iface->assignment_classes);
482 interface_clear_assignment_classes(struct interface *iface)
484 while (!list_empty(&iface->assignment_classes)) {
485 struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
486 struct interface_assignment_class, head);
493 interface_merge_assignment_data(struct interface *old, struct interface *new)
495 bool changed = (old->assignment_hint != new->assignment_hint ||
496 old->assignment_length != new->assignment_length ||
497 old->assignment_iface_id_selection != new->assignment_iface_id_selection ||
498 (old->assignment_iface_id_selection == IFID_FIXED &&
499 memcmp(&old->assignment_fixed_iface_id, &new->assignment_fixed_iface_id,
500 sizeof(old->assignment_fixed_iface_id))) ||
501 list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
503 struct interface_assignment_class *c;
504 list_for_each_entry(c, &new->assignment_classes, head) {
505 // Compare list entries one-by-one to see if there was a change
506 if (list_empty(&old->assignment_classes)) // The new list is longer
512 struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
513 struct interface_assignment_class, head);
515 if (strcmp(c_old->name, c->name)) // An entry didn't match
518 list_del(&c_old->head);
522 // The old list was longer than the new one or the last entry didn't match
523 if (!list_empty(&old->assignment_classes)) {
524 interface_clear_assignment_classes(old);
528 list_splice_init(&new->assignment_classes, &old->assignment_classes);
531 old->assignment_hint = new->assignment_hint;
532 old->assignment_length = new->assignment_length;
533 old->assignment_iface_id_selection = new->assignment_iface_id_selection;
534 old->assignment_fixed_iface_id = new->assignment_fixed_iface_id;
535 interface_refresh_assignments(true);
540 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
542 struct interface *alias = container_of(dep, struct interface, parent_iface);
543 struct device *dev = iface->l3_dev.dev;
550 interface_set_main_dev(alias, dev);
551 interface_set_available(alias, true);
554 interface_set_available(alias, false);
555 interface_set_main_dev(alias, NULL);
558 interface_remove_user(dep);
566 interface_set_device_config(struct interface *iface, struct device *dev)
568 if (!dev || !dev->default_config)
571 if (!iface->device_config &&
572 (!dev->iface_config || dev->config_iface != iface))
575 dev->config_iface = iface;
576 dev->iface_config = iface->device_config;
577 device_apply_config(dev, dev->type, iface->config);
581 interface_claim_device(struct interface *iface)
583 struct interface *parent;
584 struct device *dev = NULL;
586 if (iface->parent_iface.iface)
587 interface_remove_user(&iface->parent_iface);
589 if (iface->parent_ifname) {
590 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
591 iface->parent_iface.cb = interface_alias_cb;
592 interface_add_user(&iface->parent_iface, parent);
593 } else if (iface->ifname &&
594 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
595 dev = device_get(iface->ifname, true);
596 interface_set_device_config(iface, dev);
598 dev = iface->ext_dev.dev;
602 interface_set_main_dev(iface, dev);
604 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
605 interface_set_available(iface, true);
609 interface_cleanup_state(struct interface *iface)
611 interface_set_available(iface, false);
613 interface_flush_state(iface);
614 interface_clear_errors(iface);
615 interface_set_proto_state(iface, NULL);
617 interface_set_main_dev(iface, NULL);
618 interface_set_l3_dev(iface, NULL);
622 interface_cleanup(struct interface *iface)
624 struct interface_user *dep, *tmp;
626 uloop_timeout_cancel(&iface->remove_timer);
627 device_remove_user(&iface->ext_dev);
629 if (iface->parent_iface.iface)
630 interface_remove_user(&iface->parent_iface);
632 list_for_each_entry_safe(dep, tmp, &iface->users, list)
633 interface_remove_user(dep);
635 interface_clear_assignment_classes(iface);
636 interface_ip_flush(&iface->config_ip);
637 interface_cleanup_state(iface);
641 interface_do_free(struct interface *iface)
643 interface_event(iface, IFEV_FREE);
644 interface_cleanup(iface);
646 netifd_ubus_remove_interface(iface);
647 avl_delete(&interfaces.avl, &iface->node.avl);
652 interface_do_reload(struct interface *iface)
654 interface_event(iface, IFEV_RELOAD);
655 interface_cleanup_state(iface);
656 proto_init_interface(iface, iface->config);
657 interface_claim_device(iface);
661 interface_handle_config_change(struct interface *iface)
663 enum interface_config_state state = iface->config_state;
665 iface->config_state = IFC_NORMAL;
670 interface_do_reload(iface);
673 interface_do_free(iface);
676 if (iface->autostart && iface->available)
677 interface_set_up(iface);
678 else if (iface->dynamic)
679 set_config_state(iface, IFC_REMOVE);
683 interface_proto_event_cb(struct interface_proto_state *state, enum interface_proto_event ev)
685 struct interface *iface = state->iface;
689 if (iface->state != IFS_SETUP) {
690 interface_event(iface, IFEV_UPDATE);
694 if (!iface->l3_dev.dev)
695 interface_set_l3_dev(iface, iface->main_dev.dev);
697 interface_ip_set_enabled(&iface->config_ip, true);
698 interface_ip_set_enabled(&iface->proto_ip, true);
699 system_flush_routes();
700 iface->state = IFS_UP;
701 iface->start_time = system_get_rtime();
702 interface_event(iface, IFEV_UP);
703 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
706 if (iface->state == IFS_DOWN)
709 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
710 mark_interface_down(iface);
711 if (iface->main_dev.dev)
712 device_release(&iface->main_dev);
713 if (iface->l3_dev.dev)
714 device_remove_user(&iface->l3_dev);
715 interface_handle_config_change(iface);
717 case IFPEV_LINK_LOST:
718 if (iface->state != IFS_UP)
721 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
722 mark_interface_down(iface);
723 iface->state = IFS_SETUP;
729 interface_write_resolv_conf();
732 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
735 iface->proto->free(iface->proto);
738 iface->state = IFS_DOWN;
739 iface->proto = state;
743 state->proto_event = interface_proto_event_cb;
744 state->iface = iface;
748 interface_alloc(const char *name, struct blob_attr *config)
750 struct interface *iface;
751 struct blob_attr *tb[IFACE_ATTR_MAX];
752 struct blob_attr *cur;
753 const char *proto_name = NULL;
755 bool force_link = false;
757 iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
758 iface->name = strcpy(iface_name, name);
759 INIT_LIST_HEAD(&iface->errors);
760 INIT_LIST_HEAD(&iface->users);
761 INIT_LIST_HEAD(&iface->hotplug_list);
762 INIT_LIST_HEAD(&iface->assignment_classes);
763 interface_ip_init(iface);
764 avl_init(&iface->data, avl_strcmp, false, NULL);
765 iface->config_ip.enabled = false;
767 iface->main_dev.cb = interface_main_dev_cb;
768 iface->l3_dev.cb = interface_l3_dev_cb;
769 iface->ext_dev.cb = interface_ext_dev_cb;
771 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
772 blob_data(config), blob_len(config));
774 if ((cur = tb[IFACE_ATTR_PROTO]))
775 proto_name = blobmsg_data(cur);
777 proto_attach_interface(iface, proto_name);
778 if (iface->proto_handler->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)
781 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
782 iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link);
783 iface->proto_ip.no_defaultroute =
784 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
785 iface->proto_ip.no_dns =
786 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
788 if ((cur = tb[IFACE_ATTR_DNS]))
789 interface_add_dns_server_list(&iface->config_ip, cur);
791 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
792 interface_add_dns_search_list(&iface->config_ip, cur);
794 if ((cur = tb[IFACE_ATTR_METRIC]))
795 iface->metric = blobmsg_get_u32(cur);
797 if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
798 iface->assignment_length = blobmsg_get_u32(cur);
801 iface->assignment_iface_id_selection = IFID_FIXED;
802 iface->assignment_fixed_iface_id = in6addr_any;
803 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
805 if ((cur = tb[IFACE_ATTR_IP6IFACEID])) {
806 const char *ifaceid = blobmsg_data(cur);
807 if (!strcmp(ifaceid, "random")) {
808 iface->assignment_iface_id_selection = IFID_RANDOM;
810 else if (!strcmp(ifaceid, "eui64")) {
811 iface->assignment_iface_id_selection = IFID_EUI64;
814 /* we expect an IPv6 address with network id zero here -> fixed iface id
815 if we cannot parse -> revert to iface id 1 */
816 if (inet_pton(AF_INET6,ifaceid,&iface->assignment_fixed_iface_id) != 1 ||
817 iface->assignment_fixed_iface_id.s6_addr32[0] != 0 ||
818 iface->assignment_fixed_iface_id.s6_addr32[1] != 0) {
819 iface->assignment_fixed_iface_id = in6addr_any;
820 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
821 netifd_log_message(L_WARNING, "Failed to parse ip6ifaceid for interface '%s', \
822 falling back to iface id 1.\n", iface->name);
827 iface->assignment_hint = -1;
828 if ((cur = tb[IFACE_ATTR_IP6HINT]))
829 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
830 ~((1 << (64 - iface->assignment_length)) - 1);
832 if ((cur = tb[IFACE_ATTR_IP6CLASS]))
833 interface_add_assignment_classes(iface, cur);
836 if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
837 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
838 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
841 if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
842 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
843 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
846 iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
848 iface->config_autostart = iface->autostart;
852 void interface_set_dynamic(struct interface *iface)
854 iface->dynamic = true;
855 iface->autostart = true;
856 iface->node.version = -1; // Don't delete on reload
859 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
861 struct blob_attr *tb[IFACE_ATTR_MAX];
862 struct blob_attr *cur;
864 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
865 blob_data(config), blob_len(config));
868 if ((cur = tb[IFACE_ATTR_INTERFACE]))
869 iface->parent_ifname = blobmsg_data(cur);
871 if (!iface->parent_ifname)
874 if ((cur = tb[IFACE_ATTR_IFNAME]))
875 iface->ifname = blobmsg_data(cur);
878 iface->config = config;
879 vlist_add(&interfaces, &iface->node, iface->name);
884 interface_add(struct interface *iface, struct blob_attr *config)
886 __interface_add(iface, config, false);
890 interface_add_alias(struct interface *iface, struct blob_attr *config)
892 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
895 return __interface_add(iface, config, true);
899 interface_set_l3_dev(struct interface *iface, struct device *dev)
901 bool enabled = iface->config_ip.enabled;
902 bool claimed = iface->l3_dev.claimed;
904 if (iface->l3_dev.dev == dev)
907 interface_ip_set_enabled(&iface->config_ip, false);
908 interface_ip_set_enabled(&iface->proto_ip, false);
909 interface_ip_flush(&iface->proto_ip);
910 device_add_user(&iface->l3_dev, dev);
914 if (device_claim(&iface->l3_dev) < 0)
917 interface_ip_set_enabled(&iface->config_ip, enabled);
918 interface_ip_set_enabled(&iface->proto_ip, enabled);
923 interface_set_main_dev(struct interface *iface, struct device *dev)
925 bool claimed = iface->l3_dev.claimed;
927 if (iface->main_dev.dev == dev)
930 interface_set_available(iface, false);
931 device_add_user(&iface->main_dev, dev);
933 interface_set_link_state(iface, false);
938 if (device_claim(&iface->l3_dev) < 0)
942 if (!iface->l3_dev.dev)
943 interface_set_l3_dev(iface, dev);
947 interface_remove_link(struct interface *iface, struct device *dev)
949 struct device *mdev = iface->main_dev.dev;
951 if (mdev && mdev->hotplug_ops)
952 return mdev->hotplug_ops->del(mdev, dev);
954 if (dev == iface->ext_dev.dev)
955 device_remove_user(&iface->ext_dev);
957 if (!iface->main_dev.hotplug)
958 return UBUS_STATUS_INVALID_ARGUMENT;
960 if (dev != iface->main_dev.dev)
961 return UBUS_STATUS_INVALID_ARGUMENT;
963 interface_set_main_dev(iface, NULL);
968 interface_add_link(struct interface *iface, struct device *dev, bool link_ext)
970 struct device *mdev = iface->main_dev.dev;
975 if (iface->main_dev.hotplug)
976 device_remove_user(&iface->main_dev);
979 if (mdev->hotplug_ops)
980 return mdev->hotplug_ops->add(mdev, dev);
982 return UBUS_STATUS_NOT_SUPPORTED;
986 device_add_user(&iface->ext_dev, dev);
988 interface_set_main_dev(iface, dev);
989 iface->main_dev.hotplug = true;
994 interface_handle_link(struct interface *iface, const char *name, bool add, bool link_ext)
1001 dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
1003 ret = UBUS_STATUS_NOT_FOUND;
1008 interface_set_device_config(iface, dev);
1009 device_set_present(dev, true);
1011 ret = interface_add_link(iface, dev, link_ext);
1013 ret = interface_remove_link(iface, dev);
1023 interface_set_up(struct interface *iface)
1027 iface->autostart = true;
1029 if (iface->state != IFS_DOWN)
1032 interface_clear_errors(iface);
1033 if (!iface->available) {
1034 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
1038 if (iface->main_dev.dev) {
1039 ret = device_claim(&iface->main_dev);
1041 interface_check_state(iface);
1044 ret = __interface_set_up(iface);
1050 interface_set_down(struct interface *iface)
1053 vlist_for_each_element(&interfaces, iface, node)
1054 __interface_set_down(iface, false);
1056 iface->autostart = false;
1057 __interface_set_down(iface, false);
1064 interface_start_pending(void)
1066 struct interface *iface;
1068 vlist_for_each_element(&interfaces, iface, node) {
1069 if (iface->available && iface->autostart)
1070 interface_set_up(iface);
1075 set_config_state(struct interface *iface, enum interface_config_state s)
1077 iface->config_state = s;
1078 if (iface->state == IFS_DOWN)
1079 interface_handle_config_change(iface);
1081 __interface_set_down(iface, false);
1085 interface_update_start(struct interface *iface)
1088 interface_ip_update_start(&iface->proto_ip);
1092 interface_update_complete(struct interface *iface)
1094 interface_ip_update_complete(&iface->proto_ip);
1098 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
1100 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
1101 vlist_simple_replace(&new->dns_search, &old->dns_search);
1105 interface_device_config_changed(struct interface *if_old, struct interface *if_new)
1107 struct blob_attr *ntb[__DEV_ATTR_MAX];
1108 struct blob_attr *otb[__DEV_ATTR_MAX];
1109 struct device *dev = if_old->main_dev.dev;
1110 unsigned long diff = 0;
1112 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
1117 if (if_old->device_config != if_new->device_config)
1120 if (!if_new->device_config)
1123 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb,
1124 blob_data(if_old->config), blob_len(if_old->config));
1126 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, ntb,
1127 blob_data(if_new->config), blob_len(if_new->config));
1129 uci_blob_diff(ntb, otb, &device_attr_list, &diff);
1134 interface_change_config(struct interface *if_old, struct interface *if_new)
1136 struct blob_attr *old_config = if_old->config;
1137 bool reload = false, reload_ip = false;
1139 #define FIELD_CHANGED_STR(field) \
1140 ((!!if_old->field != !!if_new->field) || \
1142 strcmp(if_old->field, if_new->field) != 0))
1144 if (FIELD_CHANGED_STR(parent_ifname)) {
1145 if (if_old->parent_iface.iface)
1146 interface_remove_user(&if_old->parent_iface);
1150 if (!reload && interface_device_config_changed(if_old, if_new))
1153 if (FIELD_CHANGED_STR(ifname) ||
1154 if_old->proto_handler != if_new->proto_handler)
1157 if (!if_old->proto_handler->config_params)
1158 D(INTERFACE, "No config parameters for interface '%s'\n",
1160 else if (!uci_blob_check_equal(if_old->config, if_new->config,
1161 if_old->proto_handler->config_params))
1164 #define UPDATE(field, __var) ({ \
1165 bool __changed = (if_old->field != if_new->field); \
1166 if_old->field = if_new->field; \
1167 __var |= __changed; \
1170 if_old->config = if_new->config;
1171 if (if_old->config_autostart != if_new->config_autostart) {
1172 if (if_old->config_autostart)
1175 if_old->autostart = if_new->config_autostart;
1178 if_old->device_config = if_new->device_config;
1179 if_old->config_autostart = if_new->config_autostart;
1180 if_old->ifname = if_new->ifname;
1181 if_old->parent_ifname = if_new->parent_ifname;
1182 if_old->proto_handler = if_new->proto_handler;
1183 if_old->force_link = if_new->force_link;
1185 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
1186 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
1188 UPDATE(metric, reload_ip);
1189 UPDATE(proto_ip.no_defaultroute, reload_ip);
1190 UPDATE(ip4table, reload_ip);
1191 UPDATE(ip6table, reload_ip);
1192 interface_merge_assignment_data(if_old, if_new);
1197 D(INTERFACE, "Reload interface '%s' because of config changes\n",
1199 interface_clear_errors(if_old);
1200 set_config_state(if_old, IFC_RELOAD);
1205 bool config_ip_enabled = if_old->config_ip.enabled;
1206 bool proto_ip_enabled = if_old->proto_ip.enabled;
1208 interface_ip_set_enabled(&if_old->config_ip, false);
1209 interface_ip_set_enabled(&if_old->proto_ip, false);
1210 interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
1211 interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
1214 interface_write_resolv_conf();
1215 if (if_old->main_dev.dev)
1216 interface_check_state(if_old);
1219 if_new->config = NULL;
1220 interface_cleanup(if_new);
1226 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1227 struct vlist_node *node_old)
1229 struct interface *if_old = container_of(node_old, struct interface, node);
1230 struct interface *if_new = container_of(node_new, struct interface, node);
1232 if (node_old && node_new) {
1233 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1234 interface_change_config(if_old, if_new);
1235 } else if (node_old) {
1236 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1237 set_config_state(if_old, IFC_REMOVE);
1238 } else if (node_new) {
1239 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1240 proto_init_interface(if_new, if_new->config);
1241 interface_claim_device(if_new);
1242 netifd_ubus_add_interface(if_new);
1248 interface_init_list(void)
1250 vlist_init(&interfaces, avl_strcmp, interface_update);
1251 interfaces.keep_old = true;
1252 interfaces.no_delete = true;