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);
192 memcpy(n->data, data, len);
193 n->node.key = blobmsg_name(n->data);
194 avl_insert(&iface->data, &n->node);
196 iface->updated |= IUF_DATA;
200 int interface_parse_data(struct interface *iface, const struct blob_attr *attr)
202 struct blob_attr *cur;
207 blob_for_each_attr(cur, attr, rem) {
208 ret = interface_add_data(iface, cur);
213 if (iface->updated && iface->state == IFS_UP)
214 interface_event(iface, IFEV_UPDATE);
220 interface_event(struct interface *iface, enum interface_event ev)
222 struct interface_user *dep, *tmp;
223 struct device *adev = NULL;
225 list_for_each_entry_safe(dep, tmp, &iface->users, list)
226 dep->cb(dep, iface, ev);
228 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
229 dep->cb(dep, iface, ev);
233 interface_error_flush(iface);
234 adev = iface->l3_dev.dev;
237 alias_notify_device(iface->name, adev);
245 interface_flush_state(struct interface *iface)
247 if (iface->l3_dev.dev)
248 device_release(&iface->l3_dev);
249 interface_data_flush(iface);
253 mark_interface_down(struct interface *iface)
255 enum interface_state state = iface->state;
257 if (state == IFS_DOWN)
260 iface->state = IFS_DOWN;
262 interface_event(iface, IFEV_DOWN);
263 interface_ip_set_enabled(&iface->config_ip, false);
264 interface_ip_set_enabled(&iface->proto_ip, false);
265 interface_ip_flush(&iface->proto_ip);
266 interface_flush_state(iface);
267 system_flush_routes();
271 __interface_set_down(struct interface *iface, bool force)
273 enum interface_state state = iface->state;
277 iface->state = IFS_TEARDOWN;
279 interface_event(iface, IFEV_DOWN);
281 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
283 interface_flush_state(iface);
287 if (iface->main_dev.dev)
288 device_release(&iface->main_dev);
296 __interface_set_up(struct interface *iface)
300 netifd_log_message(L_NOTICE, "Interface '%s' is setting up now\n", iface->name);
302 iface->state = IFS_SETUP;
303 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
305 mark_interface_down(iface);
311 interface_check_state(struct interface *iface)
313 bool link_state = iface->link_state || iface->force_link;
315 switch (iface->state) {
318 if (!iface->enabled || !link_state) {
319 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
320 mark_interface_down(iface);
324 if (!iface->available)
327 if (iface->autostart && iface->enabled && link_state && !config_init)
328 __interface_set_up(iface);
336 interface_set_enabled(struct interface *iface, bool new_state)
338 if (iface->enabled == new_state)
341 netifd_log_message(L_NOTICE, "Interface '%s' is %s\n", iface->name, new_state ? "enabled" : "disabled");
342 iface->enabled = new_state;
343 interface_check_state(iface);
347 interface_set_link_state(struct interface *iface, bool new_state)
349 if (iface->link_state == new_state)
352 netifd_log_message(L_NOTICE, "Interface '%s' has link connectivity %s\n", iface->name, new_state ? "" : "loss");
353 iface->link_state = new_state;
354 interface_check_state(iface);
358 interface_ext_dev_cb(struct device_user *dep, enum device_event ev)
360 if (ev == DEV_EVENT_REMOVE)
361 device_remove_user(dep);
365 interface_main_dev_cb(struct device_user *dep, enum device_event ev)
367 struct interface *iface;
368 bool new_state = false;
370 iface = container_of(dep, struct interface, main_dev);
374 case DEV_EVENT_REMOVE:
375 interface_set_available(iface, new_state);
376 if (!new_state && dep->dev && dep->dev->external)
377 interface_set_main_dev(iface, NULL);
382 interface_set_enabled(iface, new_state);
384 case DEV_EVENT_LINK_UP:
386 case DEV_EVENT_LINK_DOWN:
387 interface_set_link_state(iface, new_state);
389 case DEV_EVENT_TOPO_CHANGE:
390 interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
398 interface_l3_dev_cb(struct device_user *dep, enum device_event ev)
400 struct interface *iface;
402 iface = container_of(dep, struct interface, l3_dev);
403 if (iface->l3_dev.dev == iface->main_dev.dev)
407 case DEV_EVENT_LINK_DOWN:
408 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
416 interface_set_available(struct interface *iface, bool new_state)
418 if (iface->available == new_state)
421 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
422 iface->available = new_state;
425 if (iface->autostart && !config_init)
426 interface_set_up(iface);
428 __interface_set_down(iface, true);
432 interface_add_user(struct interface_user *dep, struct interface *iface)
435 list_add(&dep->list, &iface_all_users);
440 list_add(&dep->list, &iface->users);
441 if (iface->state == IFS_UP)
442 dep->cb(dep, iface, IFEV_UP);
446 interface_remove_user(struct interface_user *dep)
448 list_del_init(&dep->list);
453 interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
455 struct blob_attr *cur;
458 blobmsg_for_each_attr(cur, list, rem) {
459 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
462 if (!blobmsg_check_attr(cur, NULL))
465 struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
466 memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
467 list_add(&c->head, &iface->assignment_classes);
472 interface_clear_assignment_classes(struct interface *iface)
474 while (!list_empty(&iface->assignment_classes)) {
475 struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
476 struct interface_assignment_class, head);
483 interface_merge_assignment_data(struct interface *old, struct interface *new)
485 bool changed = (old->assignment_hint != new->assignment_hint ||
486 old->assignment_length != new->assignment_length ||
487 old->assignment_iface_id_selection != new->assignment_iface_id_selection ||
488 (old->assignment_iface_id_selection == IFID_FIXED &&
489 memcmp(&old->assignment_fixed_iface_id, &new->assignment_fixed_iface_id,
490 sizeof(old->assignment_fixed_iface_id))) ||
491 list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
493 struct interface_assignment_class *c;
494 list_for_each_entry(c, &new->assignment_classes, head) {
495 // Compare list entries one-by-one to see if there was a change
496 if (list_empty(&old->assignment_classes)) // The new list is longer
502 struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
503 struct interface_assignment_class, head);
505 if (strcmp(c_old->name, c->name)) // An entry didn't match
508 list_del(&c_old->head);
512 // The old list was longer than the new one or the last entry didn't match
513 if (!list_empty(&old->assignment_classes)) {
514 interface_clear_assignment_classes(old);
518 list_splice_init(&new->assignment_classes, &old->assignment_classes);
521 old->assignment_hint = new->assignment_hint;
522 old->assignment_length = new->assignment_length;
523 old->assignment_iface_id_selection = new->assignment_iface_id_selection;
524 old->assignment_fixed_iface_id = new->assignment_fixed_iface_id;
525 interface_refresh_assignments(true);
530 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
532 struct interface *alias = container_of(dep, struct interface, parent_iface);
533 struct device *dev = iface->l3_dev.dev;
540 interface_set_main_dev(alias, dev);
541 interface_set_available(alias, true);
544 interface_set_available(alias, false);
545 interface_set_main_dev(alias, NULL);
548 interface_remove_user(dep);
557 interface_set_device_config(struct interface *iface, struct device *dev)
559 if (!dev || !dev->default_config)
562 if (!iface->device_config &&
563 (!dev->iface_config || dev->config_iface != iface))
566 dev->config_iface = iface;
567 dev->iface_config = iface->device_config;
568 device_apply_config(dev, dev->type, iface->config);
572 interface_claim_device(struct interface *iface)
574 struct interface *parent;
575 struct device *dev = NULL;
577 if (iface->parent_iface.iface)
578 interface_remove_user(&iface->parent_iface);
580 if (iface->parent_ifname) {
581 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
582 iface->parent_iface.cb = interface_alias_cb;
583 interface_add_user(&iface->parent_iface, parent);
584 } else if (iface->ifname &&
585 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
586 dev = device_get(iface->ifname, true);
587 interface_set_device_config(iface, dev);
589 dev = iface->ext_dev.dev;
593 interface_set_main_dev(iface, dev);
595 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
596 interface_set_available(iface, true);
600 interface_cleanup_state(struct interface *iface)
602 interface_set_available(iface, false);
604 interface_flush_state(iface);
605 interface_clear_errors(iface);
606 interface_set_proto_state(iface, NULL);
608 interface_set_main_dev(iface, NULL);
609 interface_set_l3_dev(iface, NULL);
613 interface_cleanup(struct interface *iface)
615 struct interface_user *dep, *tmp;
617 uloop_timeout_cancel(&iface->remove_timer);
618 device_remove_user(&iface->ext_dev);
620 if (iface->parent_iface.iface)
621 interface_remove_user(&iface->parent_iface);
623 list_for_each_entry_safe(dep, tmp, &iface->users, list)
624 interface_remove_user(dep);
626 interface_clear_assignment_classes(iface);
627 interface_ip_flush(&iface->config_ip);
628 interface_cleanup_state(iface);
632 interface_do_free(struct interface *iface)
634 interface_event(iface, IFEV_FREE);
635 interface_cleanup(iface);
637 netifd_ubus_remove_interface(iface);
638 avl_delete(&interfaces.avl, &iface->node.avl);
643 interface_do_reload(struct interface *iface)
645 interface_event(iface, IFEV_RELOAD);
646 interface_cleanup_state(iface);
647 proto_init_interface(iface, iface->config);
648 interface_claim_device(iface);
652 interface_handle_config_change(struct interface *iface)
654 enum interface_config_state state = iface->config_state;
656 iface->config_state = IFC_NORMAL;
661 interface_do_reload(iface);
664 interface_do_free(iface);
667 if (iface->autostart && iface->available)
668 interface_set_up(iface);
669 else if (iface->dynamic)
670 set_config_state(iface, IFC_REMOVE);
674 interface_proto_event_cb(struct interface_proto_state *state, enum interface_proto_event ev)
676 struct interface *iface = state->iface;
680 if (iface->state != IFS_SETUP) {
681 interface_event(iface, IFEV_UPDATE);
685 if (!iface->l3_dev.dev)
686 interface_set_l3_dev(iface, iface->main_dev.dev);
688 interface_ip_set_enabled(&iface->config_ip, true);
689 interface_ip_set_enabled(&iface->proto_ip, true);
690 system_flush_routes();
691 iface->state = IFS_UP;
692 iface->start_time = system_get_rtime();
693 interface_event(iface, IFEV_UP);
694 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
697 if (iface->state == IFS_DOWN)
700 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
701 mark_interface_down(iface);
702 if (iface->main_dev.dev)
703 device_release(&iface->main_dev);
704 if (iface->l3_dev.dev)
705 device_remove_user(&iface->l3_dev);
706 interface_handle_config_change(iface);
708 case IFPEV_LINK_LOST:
709 if (iface->state != IFS_UP)
712 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
713 mark_interface_down(iface);
714 iface->state = IFS_SETUP;
720 interface_write_resolv_conf();
723 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
726 iface->proto->free(iface->proto);
729 iface->state = IFS_DOWN;
730 iface->proto = state;
734 state->proto_event = interface_proto_event_cb;
735 state->iface = iface;
739 interface_alloc(const char *name, struct blob_attr *config)
741 struct interface *iface;
742 struct blob_attr *tb[IFACE_ATTR_MAX];
743 struct blob_attr *cur;
744 const char *proto_name = NULL;
746 bool force_link = false;
748 iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
749 iface->name = strcpy(iface_name, name);
750 INIT_LIST_HEAD(&iface->errors);
751 INIT_LIST_HEAD(&iface->users);
752 INIT_LIST_HEAD(&iface->hotplug_list);
753 INIT_LIST_HEAD(&iface->assignment_classes);
754 interface_ip_init(iface);
755 avl_init(&iface->data, avl_strcmp, false, NULL);
756 iface->config_ip.enabled = false;
758 iface->main_dev.cb = interface_main_dev_cb;
759 iface->l3_dev.cb = interface_l3_dev_cb;
760 iface->ext_dev.cb = interface_ext_dev_cb;
762 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
763 blob_data(config), blob_len(config));
765 if ((cur = tb[IFACE_ATTR_PROTO]))
766 proto_name = blobmsg_data(cur);
768 proto_attach_interface(iface, proto_name);
769 if (iface->proto_handler->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)
772 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
773 iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link);
774 iface->proto_ip.no_defaultroute =
775 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
776 iface->proto_ip.no_dns =
777 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
779 if ((cur = tb[IFACE_ATTR_DNS]))
780 interface_add_dns_server_list(&iface->config_ip, cur);
782 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
783 interface_add_dns_search_list(&iface->config_ip, cur);
785 if ((cur = tb[IFACE_ATTR_METRIC]))
786 iface->metric = blobmsg_get_u32(cur);
788 if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
789 iface->assignment_length = blobmsg_get_u32(cur);
792 iface->assignment_iface_id_selection = IFID_FIXED;
793 iface->assignment_fixed_iface_id = in6addr_any;
794 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
796 if ((cur = tb[IFACE_ATTR_IP6IFACEID])) {
797 const char *ifaceid = blobmsg_data(cur);
798 if (!strcmp(ifaceid, "random")) {
799 iface->assignment_iface_id_selection = IFID_RANDOM;
801 else if (!strcmp(ifaceid, "eui64")) {
802 iface->assignment_iface_id_selection = IFID_EUI64;
805 /* we expect an IPv6 address with network id zero here -> fixed iface id
806 if we cannot parse -> revert to iface id 1 */
807 if (inet_pton(AF_INET6,ifaceid,&iface->assignment_fixed_iface_id) != 1 ||
808 iface->assignment_fixed_iface_id.s6_addr32[0] != 0 ||
809 iface->assignment_fixed_iface_id.s6_addr32[1] != 0) {
810 iface->assignment_fixed_iface_id = in6addr_any;
811 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
812 netifd_log_message(L_WARNING, "Failed to parse ip6ifaceid for interface '%s', \
813 falling back to iface id 1.\n", iface->name);
818 iface->assignment_hint = -1;
819 if ((cur = tb[IFACE_ATTR_IP6HINT]))
820 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
821 ~((1 << (64 - iface->assignment_length)) - 1);
823 if ((cur = tb[IFACE_ATTR_IP6CLASS]))
824 interface_add_assignment_classes(iface, cur);
827 if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
828 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
829 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
832 if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
833 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
834 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
837 iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
839 iface->config_autostart = iface->autostart;
843 void interface_set_dynamic(struct interface *iface)
845 iface->dynamic = true;
846 iface->autostart = true;
847 iface->node.version = -1; // Don't delete on reload
850 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
852 struct blob_attr *tb[IFACE_ATTR_MAX];
853 struct blob_attr *cur;
855 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
856 blob_data(config), blob_len(config));
859 if ((cur = tb[IFACE_ATTR_INTERFACE]))
860 iface->parent_ifname = blobmsg_data(cur);
862 if (!iface->parent_ifname)
865 if ((cur = tb[IFACE_ATTR_IFNAME]))
866 iface->ifname = blobmsg_data(cur);
869 iface->config = config;
870 vlist_add(&interfaces, &iface->node, iface->name);
875 interface_add(struct interface *iface, struct blob_attr *config)
877 __interface_add(iface, config, false);
881 interface_add_alias(struct interface *iface, struct blob_attr *config)
883 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
886 return __interface_add(iface, config, true);
890 interface_set_l3_dev(struct interface *iface, struct device *dev)
892 bool enabled = iface->config_ip.enabled;
893 bool claimed = iface->l3_dev.claimed;
895 if (iface->l3_dev.dev == dev)
898 interface_ip_set_enabled(&iface->config_ip, false);
899 interface_ip_set_enabled(&iface->proto_ip, false);
900 interface_ip_flush(&iface->proto_ip);
901 device_add_user(&iface->l3_dev, dev);
905 if (device_claim(&iface->l3_dev) < 0)
908 interface_ip_set_enabled(&iface->config_ip, enabled);
909 interface_ip_set_enabled(&iface->proto_ip, enabled);
914 interface_set_main_dev(struct interface *iface, struct device *dev)
916 bool claimed = iface->l3_dev.claimed;
918 if (iface->main_dev.dev == dev)
921 interface_set_available(iface, false);
922 device_add_user(&iface->main_dev, dev);
924 interface_set_link_state(iface, false);
929 if (device_claim(&iface->l3_dev) < 0)
933 if (!iface->l3_dev.dev)
934 interface_set_l3_dev(iface, dev);
938 interface_remove_link(struct interface *iface, struct device *dev)
940 struct device *mdev = iface->main_dev.dev;
942 if (mdev && mdev->hotplug_ops)
943 return mdev->hotplug_ops->del(mdev, dev);
945 if (dev == iface->ext_dev.dev)
946 device_remove_user(&iface->ext_dev);
948 if (!iface->main_dev.hotplug)
949 return UBUS_STATUS_INVALID_ARGUMENT;
951 if (dev != iface->main_dev.dev)
952 return UBUS_STATUS_INVALID_ARGUMENT;
954 interface_set_main_dev(iface, NULL);
959 interface_add_link(struct interface *iface, struct device *dev, bool link_ext)
961 struct device *mdev = iface->main_dev.dev;
966 if (iface->main_dev.hotplug)
967 device_remove_user(&iface->main_dev);
970 if (mdev->hotplug_ops)
971 return mdev->hotplug_ops->add(mdev, dev);
973 return UBUS_STATUS_NOT_SUPPORTED;
977 device_add_user(&iface->ext_dev, dev);
979 interface_set_main_dev(iface, dev);
980 iface->main_dev.hotplug = true;
985 interface_handle_link(struct interface *iface, const char *name, bool add, bool link_ext)
992 dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
994 ret = UBUS_STATUS_NOT_FOUND;
999 interface_set_device_config(iface, dev);
1000 device_set_present(dev, true);
1002 ret = interface_add_link(iface, dev, link_ext);
1004 ret = interface_remove_link(iface, dev);
1014 interface_set_up(struct interface *iface)
1018 iface->autostart = true;
1020 if (iface->state != IFS_DOWN)
1023 interface_clear_errors(iface);
1024 if (!iface->available) {
1025 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
1029 if (iface->main_dev.dev) {
1030 ret = device_claim(&iface->main_dev);
1032 interface_check_state(iface);
1035 ret = __interface_set_up(iface);
1041 interface_set_down(struct interface *iface)
1044 vlist_for_each_element(&interfaces, iface, node)
1045 __interface_set_down(iface, false);
1047 iface->autostart = false;
1048 __interface_set_down(iface, false);
1055 interface_start_pending(void)
1057 struct interface *iface;
1059 vlist_for_each_element(&interfaces, iface, node) {
1060 if (iface->available && iface->autostart)
1061 interface_set_up(iface);
1066 set_config_state(struct interface *iface, enum interface_config_state s)
1068 iface->config_state = s;
1069 if (iface->state == IFS_DOWN)
1070 interface_handle_config_change(iface);
1072 __interface_set_down(iface, false);
1076 interface_update_start(struct interface *iface)
1079 interface_ip_update_start(&iface->proto_ip);
1083 interface_update_complete(struct interface *iface)
1085 interface_ip_update_complete(&iface->proto_ip);
1089 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
1091 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
1092 vlist_simple_replace(&new->dns_search, &old->dns_search);
1096 interface_device_config_changed(struct interface *if_old, struct interface *if_new)
1098 struct blob_attr *ntb[__DEV_ATTR_MAX];
1099 struct blob_attr *otb[__DEV_ATTR_MAX];
1100 struct device *dev = if_old->main_dev.dev;
1101 unsigned long diff = 0;
1103 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
1108 if (if_old->device_config != if_new->device_config)
1111 if (!if_new->device_config)
1114 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb,
1115 blob_data(if_old->config), blob_len(if_old->config));
1117 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, ntb,
1118 blob_data(if_new->config), blob_len(if_new->config));
1120 uci_blob_diff(ntb, otb, &device_attr_list, &diff);
1125 interface_change_config(struct interface *if_old, struct interface *if_new)
1127 struct blob_attr *old_config = if_old->config;
1128 bool reload = false, reload_ip = false;
1130 #define FIELD_CHANGED_STR(field) \
1131 ((!!if_old->field != !!if_new->field) || \
1133 strcmp(if_old->field, if_new->field) != 0))
1135 if (FIELD_CHANGED_STR(parent_ifname)) {
1136 if (if_old->parent_iface.iface)
1137 interface_remove_user(&if_old->parent_iface);
1141 if (!reload && interface_device_config_changed(if_old, if_new))
1144 if (FIELD_CHANGED_STR(ifname) ||
1145 if_old->proto_handler != if_new->proto_handler)
1148 if (!if_old->proto_handler->config_params)
1149 D(INTERFACE, "No config parameters for interface '%s'\n",
1151 else if (!uci_blob_check_equal(if_old->config, if_new->config,
1152 if_old->proto_handler->config_params))
1155 #define UPDATE(field, __var) ({ \
1156 bool __changed = (if_old->field != if_new->field); \
1157 if_old->field = if_new->field; \
1158 __var |= __changed; \
1161 if_old->config = if_new->config;
1162 if (if_old->config_autostart != if_new->config_autostart) {
1163 if (if_old->config_autostart)
1166 if_old->autostart = if_new->config_autostart;
1169 if_old->device_config = if_new->device_config;
1170 if_old->config_autostart = if_new->config_autostart;
1171 if_old->ifname = if_new->ifname;
1172 if_old->parent_ifname = if_new->parent_ifname;
1173 if_old->proto_handler = if_new->proto_handler;
1174 if_old->force_link = if_new->force_link;
1176 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
1177 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
1179 UPDATE(metric, reload_ip);
1180 UPDATE(proto_ip.no_defaultroute, reload_ip);
1181 UPDATE(ip4table, reload_ip);
1182 UPDATE(ip6table, reload_ip);
1183 interface_merge_assignment_data(if_old, if_new);
1188 D(INTERFACE, "Reload interface '%s' because of config changes\n",
1190 interface_clear_errors(if_old);
1191 set_config_state(if_old, IFC_RELOAD);
1196 bool config_ip_enabled = if_old->config_ip.enabled;
1197 bool proto_ip_enabled = if_old->proto_ip.enabled;
1199 interface_ip_set_enabled(&if_old->config_ip, false);
1200 interface_ip_set_enabled(&if_old->proto_ip, false);
1201 interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
1202 interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
1205 interface_write_resolv_conf();
1206 if (if_old->main_dev.dev)
1207 interface_check_state(if_old);
1210 if_new->config = NULL;
1211 interface_cleanup(if_new);
1217 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1218 struct vlist_node *node_old)
1220 struct interface *if_old = container_of(node_old, struct interface, node);
1221 struct interface *if_new = container_of(node_new, struct interface, node);
1223 if (node_old && node_new) {
1224 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1225 interface_change_config(if_old, if_new);
1226 } else if (node_old) {
1227 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1228 set_config_state(if_old, IFC_REMOVE);
1229 } else if (node_new) {
1230 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1231 proto_init_interface(if_new, if_new->config);
1232 interface_claim_device(if_new);
1233 netifd_ubus_add_interface(if_new);
1239 interface_init_list(void)
1241 vlist_init(&interfaces, avl_strcmp, interface_update);
1242 interfaces.keep_old = true;
1243 interfaces.no_delete = true;