+
+static void
+set_config_state(struct interface *iface, enum interface_config_state s)
+{
+ iface->config_state = s;
+ if (iface->state == IFS_DOWN)
+ interface_handle_config_change(iface);
+ else
+ __interface_set_down(iface, false);
+}
+
+void
+interface_update_start(struct interface *iface, const bool keep_old)
+{
+ iface->updated = 0;
+
+ if (!keep_old)
+ interface_ip_update_start(&iface->proto_ip);
+}
+
+void
+interface_update_complete(struct interface *iface)
+{
+ interface_ip_update_complete(&iface->proto_ip);
+}
+
+static void
+interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
+{
+ vlist_simple_replace(&new->dns_servers, &old->dns_servers);
+ vlist_simple_replace(&new->dns_search, &old->dns_search);
+}
+
+static bool
+interface_device_config_changed(struct interface *if_old, struct interface *if_new)
+{
+ struct blob_attr *ntb[__DEV_ATTR_MAX];
+ struct blob_attr *otb[__DEV_ATTR_MAX];
+ struct device *dev = if_old->main_dev.dev;
+ unsigned long diff = 0;
+
+ BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
+
+ if (!dev)
+ return false;
+
+ if (if_old->device_config != if_new->device_config)
+ return true;
+
+ if (!if_new->device_config)
+ return false;
+
+ blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb,
+ blob_data(if_old->config), blob_len(if_old->config));
+
+ blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, ntb,
+ blob_data(if_new->config), blob_len(if_new->config));
+
+ uci_blob_diff(ntb, otb, &device_attr_list, &diff);
+ return diff;
+}
+
+static void
+interface_change_config(struct interface *if_old, struct interface *if_new)
+{
+ struct blob_attr *old_config = if_old->config;
+ bool reload = false, reload_ip = false;
+
+#define FIELD_CHANGED_STR(field) \
+ ((!!if_old->field != !!if_new->field) || \
+ (if_old->field && \
+ strcmp(if_old->field, if_new->field) != 0))
+
+ if (FIELD_CHANGED_STR(parent_ifname)) {
+ if (if_old->parent_iface.iface)
+ interface_remove_user(&if_old->parent_iface);
+ reload = true;
+ }
+
+ if (!reload && interface_device_config_changed(if_old, if_new))
+ reload = true;
+
+ if (FIELD_CHANGED_STR(ifname) ||
+ if_old->proto_handler != if_new->proto_handler)
+ reload = true;
+
+ if (!if_old->proto_handler->config_params)
+ D(INTERFACE, "No config parameters for interface '%s'\n",
+ if_old->name);
+ else if (!uci_blob_check_equal(if_old->config, if_new->config,
+ if_old->proto_handler->config_params))
+ reload = true;
+
+#define UPDATE(field, __var) ({ \
+ bool __changed = (if_old->field != if_new->field); \
+ if_old->field = if_new->field; \
+ __var |= __changed; \
+ })
+
+ if_old->config = if_new->config;
+ if (if_old->config_autostart != if_new->config_autostart) {
+ if (if_old->config_autostart)
+ reload = true;
+
+ if_old->autostart = if_new->config_autostart;
+ }
+
+ if_old->device_config = if_new->device_config;
+ if_old->config_autostart = if_new->config_autostart;
+ if_old->ifname = if_new->ifname;
+ if_old->parent_ifname = if_new->parent_ifname;
+ if_old->proto_handler = if_new->proto_handler;
+ if_old->force_link = if_new->force_link;
+ if_old->dns_metric = if_new->dns_metric;
+
+ if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
+ interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
+
+ UPDATE(metric, reload_ip);
+ UPDATE(proto_ip.no_defaultroute, reload_ip);
+ UPDATE(ip4table, reload_ip);
+ UPDATE(ip6table, reload_ip);
+ interface_merge_assignment_data(if_old, if_new);
+
+#undef UPDATE
+
+ if (reload) {
+ D(INTERFACE, "Reload interface '%s' because of config changes\n",
+ if_old->name);
+ interface_clear_errors(if_old);
+ set_config_state(if_old, IFC_RELOAD);
+ goto out;
+ }
+
+ if (reload_ip) {
+ bool config_ip_enabled = if_old->config_ip.enabled;
+ bool proto_ip_enabled = if_old->proto_ip.enabled;
+
+ interface_ip_set_enabled(&if_old->config_ip, false);
+ interface_ip_set_enabled(&if_old->proto_ip, false);
+ interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
+ interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
+ }
+
+ interface_write_resolv_conf();
+ if (if_old->main_dev.dev)
+ interface_check_state(if_old);
+
+out:
+ if_new->config = NULL;
+ interface_cleanup(if_new);
+ free(old_config);
+ free(if_new);
+}
+
+static void
+interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
+ struct vlist_node *node_old)
+{
+ struct interface *if_old = container_of(node_old, struct interface, node);
+ struct interface *if_new = container_of(node_new, struct interface, node);
+
+ if (node_old && node_new) {
+ D(INTERFACE, "Update interface '%s'\n", if_new->name);
+ interface_change_config(if_old, if_new);
+ } else if (node_old) {
+ D(INTERFACE, "Remove interface '%s'\n", if_old->name);
+ set_config_state(if_old, IFC_REMOVE);
+ } else if (node_new) {
+ D(INTERFACE, "Create interface '%s'\n", if_new->name);
+ proto_init_interface(if_new, if_new->config);
+ interface_claim_device(if_new);
+ netifd_ubus_add_interface(if_new);
+ }
+}
+
+
+static void __init
+interface_init_list(void)
+{
+ vlist_init(&interfaces, avl_strcmp, interface_update);
+ interfaces.keep_old = true;
+ interfaces.no_delete = true;
+}