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 interface_error_flush(struct interface *iface)
79 struct interface_error *error, *tmp;
81 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
82 list_del(&error->list);
88 interface_clear_errors(struct interface *iface)
90 /* don't flush the errors in case the configured protocol handler matches the
91 running protocol handler and is having the last error capability */
93 (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
94 (iface->proto->handler->name == iface->proto_handler->name)))
95 interface_error_flush(iface);
98 void interface_add_error(struct interface *iface, const char *subsystem,
99 const char *code, const char **data, int n_data)
101 struct interface_error *error;
104 char *dest, *d_subsys, *d_code;
106 /* if the configured protocol handler has the last error support capability,
107 errors should only be added if the running protocol handler matches the
110 (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
111 (iface->proto->handler->name != iface->proto_handler->name))
115 len = n_data * sizeof(char *);
116 datalen = alloca(len);
117 for (i = 0; i < n_data; i++) {
118 datalen[i] = strlen(data[i]) + 1;
123 error = calloc_a(sizeof(*error) + sizeof(char *) + len,
124 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
125 &d_code, code ? strlen(code) + 1 : 0);
129 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
130 protocol can't be established (e.g auth failure) */
131 if (iface->proto_handler->flags & PROTO_FLAG_LASTERROR)
132 interface_error_flush(iface);
134 list_add_tail(&error->list, &iface->errors);
136 dest = (char *) &error->data[n_data + 1];
137 for (i = 0; i < n_data; i++) {
138 error->data[i] = dest;
139 memcpy(dest, data[i], datalen[i]);
142 error->data[n_data] = NULL;
145 error->subsystem = strcpy(d_subsys, subsystem);
148 error->code = strcpy(d_code, code);
152 interface_data_del(struct interface *iface, struct interface_data *data)
154 avl_delete(&iface->data, &data->node);
159 interface_data_flush(struct interface *iface)
161 struct interface_data *d, *tmp;
163 avl_for_each_element_safe(&iface->data, d, node, tmp)
164 interface_data_del(iface, d);
168 interface_add_data(struct interface *iface, const struct blob_attr *data)
170 struct interface_data *n, *o;
172 if (!blobmsg_check_attr(data, true))
173 return UBUS_STATUS_INVALID_ARGUMENT;
175 const char *name = blobmsg_name(data);
176 unsigned len = blob_pad_len(data);
178 o = avl_find_element(&iface->data, name, o, node);
180 if (blob_pad_len(o->data) == len && !memcmp(o->data, data, len))
183 interface_data_del(iface, o);
186 n = calloc(1, sizeof(*n) + len);
187 memcpy(n->data, data, len);
188 n->node.key = blobmsg_name(n->data);
189 avl_insert(&iface->data, &n->node);
191 iface->updated |= IUF_DATA;
196 interface_event(struct interface *iface, enum interface_event ev)
198 struct interface_user *dep, *tmp;
199 struct device *adev = NULL;
201 list_for_each_entry_safe(dep, tmp, &iface->users, list)
202 dep->cb(dep, iface, ev);
204 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
205 dep->cb(dep, iface, ev);
209 interface_error_flush(iface);
210 adev = iface->l3_dev.dev;
213 alias_notify_device(iface->name, adev);
221 interface_flush_state(struct interface *iface)
223 if (iface->l3_dev.dev)
224 device_release(&iface->l3_dev);
225 interface_data_flush(iface);
229 mark_interface_down(struct interface *iface)
231 enum interface_state state = iface->state;
233 if (state == IFS_DOWN)
236 iface->state = IFS_DOWN;
238 interface_event(iface, IFEV_DOWN);
239 interface_ip_set_enabled(&iface->config_ip, false);
240 interface_ip_flush(&iface->proto_ip);
241 interface_flush_state(iface);
242 system_flush_routes();
246 __interface_set_down(struct interface *iface, bool force)
248 enum interface_state state = iface->state;
252 iface->state = IFS_TEARDOWN;
254 interface_event(iface, IFEV_DOWN);
256 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
258 interface_flush_state(iface);
261 vlist_delete(&interfaces, &iface->node);
265 if (iface->main_dev.dev)
266 device_release(&iface->main_dev);
274 __interface_set_up(struct interface *iface)
278 netifd_log_message(L_NOTICE, "Interface '%s' is setting up now\n", iface->name);
280 iface->state = IFS_SETUP;
281 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
283 mark_interface_down(iface);
289 interface_check_state(struct interface *iface)
291 bool link_state = iface->link_state || iface->force_link;
293 switch (iface->state) {
296 if (!iface->enabled || !link_state) {
297 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
298 mark_interface_down(iface);
302 if (!iface->available)
305 if (iface->autostart && iface->enabled && link_state && !config_init)
306 __interface_set_up(iface);
314 interface_set_enabled(struct interface *iface, bool new_state)
316 if (iface->enabled == new_state)
319 netifd_log_message(L_NOTICE, "Interface '%s' is %s\n", iface->name, new_state ? "enabled" : "disabled");
320 iface->enabled = new_state;
321 interface_check_state(iface);
325 interface_set_link_state(struct interface *iface, bool new_state)
327 if (iface->link_state == new_state)
330 netifd_log_message(L_NOTICE, "Interface '%s' has link connectivity %s\n", iface->name, new_state ? "" : "loss");
331 iface->link_state = new_state;
332 interface_check_state(iface);
336 interface_ext_dev_cb(struct device_user *dep, enum device_event ev)
338 if (ev == DEV_EVENT_REMOVE)
339 device_remove_user(dep);
343 interface_main_dev_cb(struct device_user *dep, enum device_event ev)
345 struct interface *iface;
346 bool new_state = false;
348 iface = container_of(dep, struct interface, main_dev);
352 case DEV_EVENT_REMOVE:
353 interface_set_available(iface, new_state);
354 if (!new_state && dep->dev && dep->dev->external)
355 interface_set_main_dev(iface, NULL);
360 interface_set_enabled(iface, new_state);
362 case DEV_EVENT_LINK_UP:
364 case DEV_EVENT_LINK_DOWN:
365 interface_set_link_state(iface, new_state);
367 case DEV_EVENT_TOPO_CHANGE:
368 interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
376 interface_l3_dev_cb(struct device_user *dep, enum device_event ev)
378 struct interface *iface;
380 iface = container_of(dep, struct interface, l3_dev);
381 if (iface->l3_dev.dev == iface->main_dev.dev)
385 case DEV_EVENT_LINK_DOWN:
386 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
394 interface_set_available(struct interface *iface, bool new_state)
396 if (iface->available == new_state)
399 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
400 iface->available = new_state;
403 if (iface->autostart && !config_init)
404 interface_set_up(iface);
406 __interface_set_down(iface, true);
410 interface_add_user(struct interface_user *dep, struct interface *iface)
413 list_add(&dep->list, &iface_all_users);
418 list_add(&dep->list, &iface->users);
419 if (iface->state == IFS_UP)
420 dep->cb(dep, iface, IFEV_UP);
424 interface_remove_user(struct interface_user *dep)
426 list_del_init(&dep->list);
431 interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
433 struct blob_attr *cur;
436 blobmsg_for_each_attr(cur, list, rem) {
437 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
440 if (!blobmsg_check_attr(cur, NULL))
443 struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
444 memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
445 list_add(&c->head, &iface->assignment_classes);
450 interface_clear_assignment_classes(struct interface *iface)
452 while (!list_empty(&iface->assignment_classes)) {
453 struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
454 struct interface_assignment_class, head);
461 interface_merge_assignment_data(struct interface *old, struct interface *new)
463 bool changed = (old->assignment_hint != new->assignment_hint ||
464 old->assignment_length != new->assignment_length ||
465 old->assignment_iface_id_selection != new->assignment_iface_id_selection ||
466 (old->assignment_iface_id_selection == IFID_FIXED &&
467 memcmp(&old->assignment_fixed_iface_id, &new->assignment_fixed_iface_id,
468 sizeof(old->assignment_fixed_iface_id))) ||
469 list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
471 struct interface_assignment_class *c;
472 list_for_each_entry(c, &new->assignment_classes, head) {
473 // Compare list entries one-by-one to see if there was a change
474 if (list_empty(&old->assignment_classes)) // The new list is longer
480 struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
481 struct interface_assignment_class, head);
483 if (strcmp(c_old->name, c->name)) // An entry didn't match
486 list_del(&c_old->head);
490 // The old list was longer than the new one or the last entry didn't match
491 if (!list_empty(&old->assignment_classes)) {
492 interface_clear_assignment_classes(old);
496 list_splice_init(&new->assignment_classes, &old->assignment_classes);
499 old->assignment_hint = new->assignment_hint;
500 old->assignment_length = new->assignment_length;
501 old->assignment_iface_id_selection = new->assignment_iface_id_selection;
502 old->assignment_fixed_iface_id = new->assignment_fixed_iface_id;
503 interface_refresh_assignments(true);
508 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
510 struct interface *alias = container_of(dep, struct interface, parent_iface);
511 struct device *dev = iface->l3_dev.dev;
518 interface_set_main_dev(alias, dev);
519 interface_set_available(alias, true);
522 interface_set_available(alias, false);
523 interface_set_main_dev(alias, NULL);
526 interface_remove_user(dep);
535 interface_set_device_config(struct interface *iface, struct device *dev)
537 if (!dev || !dev->default_config)
540 if (!iface->device_config &&
541 (!dev->iface_config || dev->config_iface != iface))
544 dev->config_iface = iface;
545 dev->iface_config = iface->device_config;
546 device_apply_config(dev, dev->type, iface->config);
550 interface_claim_device(struct interface *iface)
552 struct interface *parent;
553 struct device *dev = NULL;
555 if (iface->parent_iface.iface)
556 interface_remove_user(&iface->parent_iface);
558 if (iface->parent_ifname) {
559 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
560 iface->parent_iface.cb = interface_alias_cb;
561 interface_add_user(&iface->parent_iface, parent);
562 } else if (iface->ifname &&
563 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
564 dev = device_get(iface->ifname, true);
565 interface_set_device_config(iface, dev);
567 dev = iface->ext_dev.dev;
571 interface_set_main_dev(iface, dev);
573 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
574 interface_set_available(iface, true);
578 interface_cleanup_state(struct interface *iface)
580 interface_set_available(iface, false);
582 interface_flush_state(iface);
583 interface_clear_errors(iface);
584 interface_set_proto_state(iface, NULL);
586 interface_set_main_dev(iface, NULL);
587 interface_set_l3_dev(iface, NULL);
591 interface_cleanup(struct interface *iface)
593 struct interface_user *dep, *tmp;
595 uloop_timeout_cancel(&iface->remove_timer);
596 device_remove_user(&iface->ext_dev);
598 if (iface->parent_iface.iface)
599 interface_remove_user(&iface->parent_iface);
601 list_for_each_entry_safe(dep, tmp, &iface->users, list)
602 interface_remove_user(dep);
604 interface_clear_assignment_classes(iface);
605 interface_ip_flush(&iface->config_ip);
606 interface_cleanup_state(iface);
610 interface_do_free(struct interface *iface)
612 interface_event(iface, IFEV_FREE);
613 interface_cleanup(iface);
615 netifd_ubus_remove_interface(iface);
616 avl_delete(&interfaces.avl, &iface->node.avl);
621 interface_do_reload(struct interface *iface)
623 interface_event(iface, IFEV_RELOAD);
624 interface_cleanup_state(iface);
625 proto_init_interface(iface, iface->config);
626 interface_claim_device(iface);
630 interface_handle_config_change(struct interface *iface)
632 enum interface_config_state state = iface->config_state;
634 iface->config_state = IFC_NORMAL;
639 interface_do_reload(iface);
642 interface_do_free(iface);
645 if (iface->autostart && iface->available)
646 interface_set_up(iface);
650 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
652 struct interface *iface = state->iface;
656 if (iface->state != IFS_SETUP) {
657 interface_event(iface, IFEV_UPDATE);
661 if (!iface->l3_dev.dev)
662 interface_set_l3_dev(iface, iface->main_dev.dev);
664 interface_ip_set_enabled(&iface->config_ip, true);
665 system_flush_routes();
666 iface->state = IFS_UP;
667 iface->start_time = system_get_rtime();
668 interface_event(iface, IFEV_UP);
669 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
672 if (iface->state == IFS_DOWN)
675 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
676 mark_interface_down(iface);
677 if (iface->main_dev.dev)
678 device_release(&iface->main_dev);
679 if (iface->l3_dev.dev)
680 device_remove_user(&iface->l3_dev);
681 interface_handle_config_change(iface);
683 case IFPEV_LINK_LOST:
684 if (iface->state != IFS_UP)
687 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
688 mark_interface_down(iface);
689 iface->state = IFS_SETUP;
695 interface_write_resolv_conf();
698 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
701 iface->proto->free(iface->proto);
704 iface->state = IFS_DOWN;
705 iface->proto = state;
709 state->proto_event = interface_proto_cb;
710 state->iface = iface;
714 interface_alloc(const char *name, struct blob_attr *config)
716 struct interface *iface;
717 struct blob_attr *tb[IFACE_ATTR_MAX];
718 struct blob_attr *cur;
719 const char *proto_name = NULL;
721 bool force_link = false;
723 iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
724 iface->name = strcpy(iface_name, name);
725 INIT_LIST_HEAD(&iface->errors);
726 INIT_LIST_HEAD(&iface->users);
727 INIT_LIST_HEAD(&iface->hotplug_list);
728 INIT_LIST_HEAD(&iface->assignment_classes);
729 interface_ip_init(iface);
730 avl_init(&iface->data, avl_strcmp, false, NULL);
731 iface->config_ip.enabled = false;
733 iface->main_dev.cb = interface_main_dev_cb;
734 iface->l3_dev.cb = interface_l3_dev_cb;
735 iface->ext_dev.cb = interface_ext_dev_cb;
737 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
738 blob_data(config), blob_len(config));
740 if ((cur = tb[IFACE_ATTR_PROTO]))
741 proto_name = blobmsg_data(cur);
743 proto_attach_interface(iface, proto_name);
744 if (iface->proto_handler->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)
747 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
748 iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link);
749 iface->proto_ip.no_defaultroute =
750 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
751 iface->proto_ip.no_dns =
752 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
754 if ((cur = tb[IFACE_ATTR_DNS]))
755 interface_add_dns_server_list(&iface->config_ip, cur);
757 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
758 interface_add_dns_search_list(&iface->config_ip, cur);
760 if ((cur = tb[IFACE_ATTR_METRIC]))
761 iface->metric = blobmsg_get_u32(cur);
763 if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
764 iface->assignment_length = blobmsg_get_u32(cur);
767 iface->assignment_iface_id_selection = IFID_FIXED;
768 iface->assignment_fixed_iface_id = in6addr_any;
769 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
771 if ((cur = tb[IFACE_ATTR_IP6IFACEID])) {
772 const char *ifaceid = blobmsg_data(cur);
773 if (!strcmp(ifaceid, "random")) {
774 iface->assignment_iface_id_selection = IFID_RANDOM;
776 else if (!strcmp(ifaceid, "eui64")) {
777 iface->assignment_iface_id_selection = IFID_EUI64;
780 /* we expect an IPv6 address with network id zero here -> fixed iface id
781 if we cannot parse -> revert to iface id 1 */
782 if (inet_pton(AF_INET6,ifaceid,&iface->assignment_fixed_iface_id) != 1 ||
783 iface->assignment_fixed_iface_id.s6_addr32[0] != 0 ||
784 iface->assignment_fixed_iface_id.s6_addr32[1] != 0) {
785 iface->assignment_fixed_iface_id = in6addr_any;
786 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
787 netifd_log_message(L_WARNING, "Failed to parse ip6ifaceid for interface '%s', \
788 falling back to iface id 1.\n", iface->name);
793 iface->assignment_hint = -1;
794 if ((cur = tb[IFACE_ATTR_IP6HINT]))
795 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
796 ~((1 << (64 - iface->assignment_length)) - 1);
798 if ((cur = tb[IFACE_ATTR_IP6CLASS]))
799 interface_add_assignment_classes(iface, cur);
802 if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
803 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
804 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
807 if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
808 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
809 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
812 iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
814 iface->config_autostart = iface->autostart;
818 void interface_set_dynamic(struct interface *iface)
820 iface->dynamic = true;
821 iface->autostart = true;
822 iface->node.version = -1; // Don't delete on reload
825 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
827 struct blob_attr *tb[IFACE_ATTR_MAX];
828 struct blob_attr *cur;
830 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
831 blob_data(config), blob_len(config));
834 if ((cur = tb[IFACE_ATTR_INTERFACE]))
835 iface->parent_ifname = blobmsg_data(cur);
837 if (!iface->parent_ifname)
840 if ((cur = tb[IFACE_ATTR_IFNAME]))
841 iface->ifname = blobmsg_data(cur);
844 iface->config = config;
845 vlist_add(&interfaces, &iface->node, iface->name);
850 interface_add(struct interface *iface, struct blob_attr *config)
852 __interface_add(iface, config, false);
856 interface_add_alias(struct interface *iface, struct blob_attr *config)
858 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
861 return __interface_add(iface, config, true);
865 interface_set_l3_dev(struct interface *iface, struct device *dev)
867 bool enabled = iface->config_ip.enabled;
868 bool claimed = iface->l3_dev.claimed;
870 if (iface->l3_dev.dev == dev)
873 interface_ip_set_enabled(&iface->config_ip, false);
874 interface_ip_flush(&iface->proto_ip);
875 device_add_user(&iface->l3_dev, dev);
879 if (device_claim(&iface->l3_dev) < 0)
882 interface_ip_set_enabled(&iface->config_ip, enabled);
887 interface_set_main_dev(struct interface *iface, struct device *dev)
889 bool claimed = iface->l3_dev.claimed;
891 if (iface->main_dev.dev == dev)
894 interface_set_available(iface, false);
895 device_add_user(&iface->main_dev, dev);
897 interface_set_link_state(iface, false);
902 if (device_claim(&iface->l3_dev) < 0)
906 if (!iface->l3_dev.dev)
907 interface_set_l3_dev(iface, dev);
911 interface_remove_link(struct interface *iface, struct device *dev)
913 struct device *mdev = iface->main_dev.dev;
915 if (mdev && mdev->hotplug_ops)
916 return mdev->hotplug_ops->del(mdev, dev);
918 if (dev == iface->ext_dev.dev)
919 device_remove_user(&iface->ext_dev);
921 if (!iface->main_dev.hotplug)
922 return UBUS_STATUS_INVALID_ARGUMENT;
924 if (dev != iface->main_dev.dev)
925 return UBUS_STATUS_INVALID_ARGUMENT;
927 interface_set_main_dev(iface, NULL);
932 interface_add_link(struct interface *iface, struct device *dev, bool link_ext)
934 struct device *mdev = iface->main_dev.dev;
939 if (iface->main_dev.hotplug)
940 device_remove_user(&iface->main_dev);
943 if (mdev->hotplug_ops)
944 return mdev->hotplug_ops->add(mdev, dev);
946 return UBUS_STATUS_NOT_SUPPORTED;
950 device_add_user(&iface->ext_dev, dev);
952 interface_set_main_dev(iface, dev);
953 iface->main_dev.hotplug = true;
958 interface_handle_link(struct interface *iface, const char *name, bool add, bool link_ext)
965 dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
967 ret = UBUS_STATUS_NOT_FOUND;
972 interface_set_device_config(iface, dev);
973 device_set_present(dev, true);
975 ret = interface_add_link(iface, dev, link_ext);
977 ret = interface_remove_link(iface, dev);
987 interface_set_up(struct interface *iface)
991 iface->autostart = true;
993 if (iface->state != IFS_DOWN)
996 interface_clear_errors(iface);
997 if (!iface->available) {
998 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
1002 if (iface->main_dev.dev) {
1003 ret = device_claim(&iface->main_dev);
1005 interface_check_state(iface);
1008 ret = __interface_set_up(iface);
1014 interface_set_down(struct interface *iface)
1017 vlist_for_each_element(&interfaces, iface, node)
1018 __interface_set_down(iface, false);
1020 iface->autostart = false;
1021 __interface_set_down(iface, false);
1028 interface_start_pending(void)
1030 struct interface *iface;
1032 vlist_for_each_element(&interfaces, iface, node) {
1033 if (iface->available && iface->autostart)
1034 interface_set_up(iface);
1039 set_config_state(struct interface *iface, enum interface_config_state s)
1041 iface->config_state = s;
1042 if (iface->state == IFS_DOWN)
1043 interface_handle_config_change(iface);
1045 __interface_set_down(iface, false);
1049 interface_update_start(struct interface *iface)
1052 interface_ip_update_start(&iface->proto_ip);
1056 interface_update_complete(struct interface *iface)
1058 interface_ip_update_complete(&iface->proto_ip);
1062 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
1064 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
1065 vlist_simple_replace(&new->dns_search, &old->dns_search);
1069 interface_device_config_changed(struct interface *if_old, struct interface *if_new)
1071 struct blob_attr *ntb[__DEV_ATTR_MAX];
1072 struct blob_attr *otb[__DEV_ATTR_MAX];
1073 struct device *dev = if_old->main_dev.dev;
1074 unsigned long diff = 0;
1076 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
1081 if (if_old->device_config != if_new->device_config)
1084 if (!if_new->device_config)
1087 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb,
1088 blob_data(if_old->config), blob_len(if_old->config));
1090 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, ntb,
1091 blob_data(if_new->config), blob_len(if_new->config));
1093 uci_blob_diff(ntb, otb, &device_attr_list, &diff);
1098 interface_change_config(struct interface *if_old, struct interface *if_new)
1100 struct blob_attr *old_config = if_old->config;
1101 bool reload = false, reload_ip = false;
1103 #define FIELD_CHANGED_STR(field) \
1104 ((!!if_old->field != !!if_new->field) || \
1106 strcmp(if_old->field, if_new->field) != 0))
1108 if (FIELD_CHANGED_STR(parent_ifname)) {
1109 if (if_old->parent_iface.iface)
1110 interface_remove_user(&if_old->parent_iface);
1114 if (!reload && interface_device_config_changed(if_old, if_new))
1117 if (FIELD_CHANGED_STR(ifname) ||
1118 if_old->proto_handler != if_new->proto_handler)
1121 if (!if_old->proto_handler->config_params)
1122 D(INTERFACE, "No config parameters for interface '%s'\n",
1124 else if (!uci_blob_check_equal(if_old->config, if_new->config,
1125 if_old->proto_handler->config_params))
1128 #define UPDATE(field, __var) ({ \
1129 bool __changed = (if_old->field != if_new->field); \
1130 if_old->field = if_new->field; \
1131 __var |= __changed; \
1134 if_old->config = if_new->config;
1135 if (if_old->config_autostart != if_new->config_autostart) {
1136 if (if_old->config_autostart)
1139 if_old->autostart = if_new->config_autostart;
1142 if_old->device_config = if_new->device_config;
1143 if_old->config_autostart = if_new->config_autostart;
1144 if_old->ifname = if_new->ifname;
1145 if_old->parent_ifname = if_new->parent_ifname;
1146 if_old->proto_handler = if_new->proto_handler;
1147 if_old->force_link = if_new->force_link;
1149 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
1150 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
1152 UPDATE(metric, reload_ip);
1153 UPDATE(proto_ip.no_defaultroute, reload_ip);
1154 UPDATE(ip4table, reload_ip);
1155 UPDATE(ip6table, reload_ip);
1156 interface_merge_assignment_data(if_old, if_new);
1161 D(INTERFACE, "Reload interface '%s' because of config changes\n",
1163 interface_clear_errors(if_old);
1164 set_config_state(if_old, IFC_RELOAD);
1169 bool config_ip_enabled = if_old->config_ip.enabled;
1170 bool proto_ip_enabled = if_old->proto_ip.enabled;
1172 interface_ip_set_enabled(&if_old->config_ip, false);
1173 interface_ip_set_enabled(&if_old->proto_ip, false);
1174 interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
1175 interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
1178 interface_write_resolv_conf();
1179 if (if_old->main_dev.dev)
1180 interface_check_state(if_old);
1183 if_new->config = NULL;
1184 interface_cleanup(if_new);
1190 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1191 struct vlist_node *node_old)
1193 struct interface *if_old = container_of(node_old, struct interface, node);
1194 struct interface *if_new = container_of(node_new, struct interface, node);
1196 if (node_old && node_new) {
1197 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1198 interface_change_config(if_old, if_new);
1199 } else if (node_old) {
1200 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1201 set_config_state(if_old, IFC_REMOVE);
1202 } else if (node_new) {
1203 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1204 proto_init_interface(if_new, if_new->config);
1205 interface_claim_device(if_new);
1206 netifd_ubus_add_interface(if_new);
1212 interface_init_list(void)
1214 vlist_init(&interfaces, avl_strcmp, interface_update);
1215 interfaces.keep_old = true;
1216 interfaces.no_delete = true;