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.
17 #include <sys/types.h>
22 #include "interface.h"
23 #include "interface-ip.h"
29 struct vlist_tree interfaces;
30 static LIST_HEAD(iface_all_users);
37 IFACE_ATTR_DEFAULTROUTE,
40 IFACE_ATTR_DNS_SEARCH,
41 IFACE_ATTR_DNS_METRIC,
50 IFACE_ATTR_IP6IFACEID,
51 IFACE_ATTR_FORCE_LINK,
56 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
57 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
58 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
59 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
60 [IFACE_ATTR_JAIL] = { .name = "jail", .type = BLOBMSG_TYPE_STRING },
61 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
62 [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
63 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
64 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
65 [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
66 [IFACE_ATTR_DNS_METRIC] = { .name = "dns_metric", .type = BLOBMSG_TYPE_INT32 },
67 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
68 [IFACE_ATTR_IP6ASSIGN] = { .name = "ip6assign", .type = BLOBMSG_TYPE_INT32 },
69 [IFACE_ATTR_IP6HINT] = { .name = "ip6hint", .type = BLOBMSG_TYPE_STRING },
70 [IFACE_ATTR_IP4TABLE] = { .name = "ip4table", .type = BLOBMSG_TYPE_STRING },
71 [IFACE_ATTR_IP6TABLE] = { .name = "ip6table", .type = BLOBMSG_TYPE_STRING },
72 [IFACE_ATTR_IP6CLASS] = { .name = "ip6class", .type = BLOBMSG_TYPE_ARRAY },
73 [IFACE_ATTR_DELEGATE] = { .name = "delegate", .type = BLOBMSG_TYPE_BOOL },
74 [IFACE_ATTR_IP6IFACEID] = { .name = "ip6ifaceid", .type = BLOBMSG_TYPE_STRING },
75 [IFACE_ATTR_FORCE_LINK] = { .name = "force_link", .type = BLOBMSG_TYPE_BOOL },
76 [IFACE_ATTR_IP6WEIGHT] = { .name = "ip6weight", .type = BLOBMSG_TYPE_INT32 },
79 const struct uci_blob_param_list interface_attr_list = {
80 .n_params = IFACE_ATTR_MAX,
81 .params = iface_attrs,
85 interface_set_main_dev(struct interface *iface, struct device *dev);
87 interface_event(struct interface *iface, enum interface_event ev);
90 interface_error_flush(struct interface *iface)
92 struct interface_error *error, *tmp;
94 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
95 list_del(&error->list);
101 interface_clear_errors(struct interface *iface)
103 /* don't flush the errors in case the configured protocol handler matches the
104 running protocol handler and is having the last error capability */
105 if (!(iface->proto &&
106 (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
107 (iface->proto->handler->name == iface->proto_handler->name)))
108 interface_error_flush(iface);
111 void interface_add_error(struct interface *iface, const char *subsystem,
112 const char *code, const char **data, int n_data)
114 struct interface_error *error;
117 char *dest, *d_subsys, *d_code;
119 /* if the configured protocol handler has the last error support capability,
120 errors should only be added if the running protocol handler matches the
123 (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
124 (iface->proto->handler->name != iface->proto_handler->name))
128 len = n_data * sizeof(char *);
129 datalen = alloca(len);
130 for (i = 0; i < n_data; i++) {
131 datalen[i] = strlen(data[i]) + 1;
136 error = calloc_a(sizeof(*error) + sizeof(char *) + len,
137 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
138 &d_code, code ? strlen(code) + 1 : 0);
142 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
143 protocol can't be established (e.g auth failure) */
144 if (iface->proto_handler->flags & PROTO_FLAG_LASTERROR)
145 interface_error_flush(iface);
147 list_add_tail(&error->list, &iface->errors);
149 dest = (char *) &error->data[n_data + 1];
150 for (i = 0; i < n_data; i++) {
151 error->data[i] = dest;
152 memcpy(dest, data[i], datalen[i]);
155 error->data[n_data] = NULL;
158 error->subsystem = strcpy(d_subsys, subsystem);
161 error->code = strcpy(d_code, code);
165 interface_data_del(struct interface *iface, struct interface_data *data)
167 avl_delete(&iface->data, &data->node);
172 interface_data_flush(struct interface *iface)
174 struct interface_data *d, *tmp;
176 avl_for_each_element_safe(&iface->data, d, node, tmp)
177 interface_data_del(iface, d);
181 interface_add_data(struct interface *iface, const struct blob_attr *data)
183 struct interface_data *n, *o;
185 if (!blobmsg_check_attr(data, true))
186 return UBUS_STATUS_INVALID_ARGUMENT;
188 const char *name = blobmsg_name(data);
189 unsigned len = blob_pad_len(data);
191 o = avl_find_element(&iface->data, name, o, node);
193 if (blob_pad_len(o->data) == len && !memcmp(o->data, data, len))
196 interface_data_del(iface, o);
199 n = calloc(1, sizeof(*n) + len);
201 return UBUS_STATUS_UNKNOWN_ERROR;
203 memcpy(n->data, data, len);
204 n->node.key = blobmsg_name(n->data);
205 avl_insert(&iface->data, &n->node);
207 iface->updated |= IUF_DATA;
211 int interface_parse_data(struct interface *iface, const struct blob_attr *attr)
213 struct blob_attr *cur;
218 blob_for_each_attr(cur, attr, rem) {
219 ret = interface_add_data(iface, cur);
224 if (iface->updated && iface->state == IFS_UP)
225 interface_event(iface, IFEV_UPDATE);
231 interface_event(struct interface *iface, enum interface_event ev)
233 struct interface_user *dep, *tmp;
234 struct device *adev = NULL;
236 list_for_each_entry_safe(dep, tmp, &iface->users, list)
237 dep->cb(dep, iface, ev);
239 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
240 dep->cb(dep, iface, ev);
244 interface_error_flush(iface);
245 adev = iface->l3_dev.dev;
249 alias_notify_device(iface->name, adev);
257 interface_flush_state(struct interface *iface)
259 if (iface->l3_dev.dev)
260 device_release(&iface->l3_dev);
261 interface_data_flush(iface);
265 mark_interface_down(struct interface *iface)
267 enum interface_state state = iface->state;
269 if (state == IFS_DOWN)
272 iface->link_up_event = false;
273 iface->state = IFS_DOWN;
277 interface_event(iface, IFEV_DOWN);
280 interface_event(iface, IFEV_UP_FAILED);
285 interface_ip_set_enabled(&iface->config_ip, false);
286 interface_ip_set_enabled(&iface->proto_ip, false);
287 interface_ip_flush(&iface->proto_ip);
288 interface_flush_state(iface);
289 system_flush_routes();
293 __set_config_state(struct interface *iface, enum interface_config_state s)
295 iface->config_state = s;
299 __interface_set_down(struct interface *iface, bool force)
301 enum interface_state state = iface->state;
305 iface->state = IFS_TEARDOWN;
307 __set_config_state(iface, IFC_REMOVE);
310 interface_event(iface, IFEV_DOWN);
312 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
314 interface_flush_state(iface);
318 if (iface->main_dev.dev)
319 device_release(&iface->main_dev);
327 __interface_set_up(struct interface *iface)
331 netifd_log_message(L_NOTICE, "Interface '%s' is setting up now\n", iface->name);
333 iface->state = IFS_SETUP;
334 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
336 mark_interface_down(iface);
342 interface_check_state(struct interface *iface)
344 bool link_state = iface->link_state || iface->force_link;
346 switch (iface->state) {
349 if (!iface->enabled || !link_state) {
350 iface->state = IFS_TEARDOWN;
352 __set_config_state(iface, IFC_REMOVE);
354 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
358 if (!iface->available)
361 if (iface->autostart && iface->enabled && link_state && !config_init)
362 __interface_set_up(iface);
370 interface_set_enabled(struct interface *iface, bool new_state)
372 if (iface->enabled == new_state)
375 netifd_log_message(L_NOTICE, "Interface '%s' is %s\n", iface->name, new_state ? "enabled" : "disabled");
376 iface->enabled = new_state;
377 interface_check_state(iface);
381 interface_set_link_state(struct interface *iface, bool new_state)
383 if (iface->link_state == new_state)
386 netifd_log_message(L_NOTICE, "Interface '%s' has link connectivity %s\n", iface->name, new_state ? "" : "loss");
387 iface->link_state = new_state;
388 interface_check_state(iface);
390 if (new_state && iface->force_link && iface->state == IFS_UP && !iface->link_up_event) {
391 interface_event(iface, IFEV_LINK_UP);
392 iface->link_up_event = true;
397 interface_ext_dev_cb(struct device_user *dep, enum device_event ev)
399 if (ev == DEV_EVENT_REMOVE)
400 device_remove_user(dep);
404 interface_main_dev_cb(struct device_user *dep, enum device_event ev)
406 struct interface *iface;
408 iface = container_of(dep, struct interface, main_dev);
411 interface_set_available(iface, true);
413 case DEV_EVENT_REMOVE:
414 interface_set_available(iface, false);
415 if (dep->dev && dep->dev->external)
416 interface_set_main_dev(iface, NULL);
419 interface_set_enabled(iface, true);
422 interface_set_enabled(iface, false);
424 case DEV_EVENT_LINK_UP:
425 interface_set_link_state(iface, true);
427 case DEV_EVENT_LINK_DOWN:
428 interface_set_link_state(iface, false);
430 case DEV_EVENT_TOPO_CHANGE:
431 interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
439 interface_l3_dev_cb(struct device_user *dep, enum device_event ev)
441 struct interface *iface;
443 iface = container_of(dep, struct interface, l3_dev);
444 if (iface->l3_dev.dev == iface->main_dev.dev)
448 case DEV_EVENT_LINK_DOWN:
449 if (iface->proto_handler->flags & PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN)
450 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
458 interface_set_available(struct interface *iface, bool new_state)
460 if (iface->available == new_state)
463 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
464 iface->available = new_state;
467 if (iface->autostart && !config_init)
468 interface_set_up(iface);
470 __interface_set_down(iface, true);
474 interface_add_user(struct interface_user *dep, struct interface *iface)
477 list_add(&dep->list, &iface_all_users);
482 list_add(&dep->list, &iface->users);
483 if (iface->state == IFS_UP)
484 dep->cb(dep, iface, IFEV_UP);
488 interface_remove_user(struct interface_user *dep)
490 list_del_init(&dep->list);
495 interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
497 struct blob_attr *cur;
500 blobmsg_for_each_attr(cur, list, rem) {
501 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
504 if (!blobmsg_check_attr(cur, false))
507 struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
508 memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
509 list_add(&c->head, &iface->assignment_classes);
514 interface_clear_assignment_classes(struct interface *iface)
516 while (!list_empty(&iface->assignment_classes)) {
517 struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
518 struct interface_assignment_class, head);
525 interface_merge_assignment_data(struct interface *old, struct interface *new)
527 bool changed = (old->assignment_hint != new->assignment_hint ||
528 old->assignment_length != new->assignment_length ||
529 old->assignment_iface_id_selection != new->assignment_iface_id_selection ||
530 old->assignment_weight != new->assignment_weight ||
531 (old->assignment_iface_id_selection == IFID_FIXED &&
532 memcmp(&old->assignment_fixed_iface_id, &new->assignment_fixed_iface_id,
533 sizeof(old->assignment_fixed_iface_id))) ||
534 list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
536 struct interface_assignment_class *c;
537 list_for_each_entry(c, &new->assignment_classes, head) {
538 /* Compare list entries one-by-one to see if there was a change */
539 if (list_empty(&old->assignment_classes)) /* The new list is longer */
545 struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
546 struct interface_assignment_class, head);
548 if (strcmp(c_old->name, c->name)) /* An entry didn't match */
551 list_del(&c_old->head);
555 /* The old list was longer than the new one or the last entry didn't match */
556 if (!list_empty(&old->assignment_classes)) {
557 interface_clear_assignment_classes(old);
561 list_splice_init(&new->assignment_classes, &old->assignment_classes);
564 old->assignment_hint = new->assignment_hint;
565 old->assignment_length = new->assignment_length;
566 old->assignment_iface_id_selection = new->assignment_iface_id_selection;
567 old->assignment_fixed_iface_id = new->assignment_fixed_iface_id;
568 old->assignment_weight = new->assignment_weight;
569 interface_refresh_assignments(true);
574 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
576 struct interface *alias = container_of(dep, struct interface, parent_iface);
577 struct device *dev = iface->l3_dev.dev;
584 interface_set_main_dev(alias, dev);
585 interface_set_available(alias, true);
589 interface_set_available(alias, false);
590 interface_set_main_dev(alias, NULL);
593 interface_remove_user(dep);
601 interface_set_device_config(struct interface *iface, struct device *dev)
603 if (!dev || !dev->default_config)
606 if (!iface->device_config &&
607 (!dev->iface_config || dev->config_iface != iface))
610 dev->config_iface = iface;
611 dev->iface_config = iface->device_config;
612 device_apply_config(dev, dev->type, iface->config);
616 interface_claim_device(struct interface *iface)
618 struct interface *parent;
619 struct device *dev = NULL;
621 if (iface->parent_iface.iface)
622 interface_remove_user(&iface->parent_iface);
626 if (iface->parent_ifname) {
627 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
628 iface->parent_iface.cb = interface_alias_cb;
629 interface_add_user(&iface->parent_iface, parent);
630 } else if (iface->ifname &&
631 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
632 dev = device_get(iface->ifname, true);
633 interface_set_device_config(iface, dev);
635 dev = iface->ext_dev.dev;
639 interface_set_main_dev(iface, dev);
643 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
644 interface_set_available(iface, true);
648 interface_cleanup_state(struct interface *iface)
650 interface_set_available(iface, false);
652 interface_flush_state(iface);
653 interface_clear_errors(iface);
654 interface_set_proto_state(iface, NULL);
656 interface_set_main_dev(iface, NULL);
657 interface_set_l3_dev(iface, NULL);
661 interface_cleanup(struct interface *iface)
663 struct interface_user *dep, *tmp;
665 uloop_timeout_cancel(&iface->remove_timer);
666 device_remove_user(&iface->ext_dev);
668 if (iface->parent_iface.iface)
669 interface_remove_user(&iface->parent_iface);
671 list_for_each_entry_safe(dep, tmp, &iface->users, list)
672 interface_remove_user(dep);
674 interface_clear_assignment_classes(iface);
675 interface_ip_flush(&iface->config_ip);
676 interface_cleanup_state(iface);
680 interface_do_free(struct interface *iface)
682 interface_event(iface, IFEV_FREE);
683 interface_cleanup(iface);
685 netifd_ubus_remove_interface(iface);
686 avl_delete(&interfaces.avl, &iface->node.avl);
691 interface_do_reload(struct interface *iface)
693 interface_event(iface, IFEV_RELOAD);
694 interface_cleanup_state(iface);
695 proto_init_interface(iface, iface->config);
696 interface_claim_device(iface);
700 interface_handle_config_change(struct interface *iface)
702 enum interface_config_state state = iface->config_state;
704 iface->config_state = IFC_NORMAL;
709 interface_do_reload(iface);
712 interface_do_free(iface);
715 if (iface->autostart)
716 interface_set_up(iface);
720 interface_proto_event_cb(struct interface_proto_state *state, enum interface_proto_event ev)
722 struct interface *iface = state->iface;
726 if (iface->state != IFS_SETUP) {
727 if (iface->state == IFS_UP && iface->updated)
728 interface_event(iface, IFEV_UPDATE);
732 if (!iface->l3_dev.dev)
733 interface_set_l3_dev(iface, iface->main_dev.dev);
735 interface_ip_set_enabled(&iface->config_ip, true);
736 interface_ip_set_enabled(&iface->proto_ip, true);
737 system_flush_routes();
738 iface->state = IFS_UP;
739 iface->start_time = system_get_rtime();
740 interface_event(iface, IFEV_UP);
741 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
744 if (iface->state == IFS_DOWN)
747 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
748 mark_interface_down(iface);
749 if (iface->main_dev.dev)
750 device_release(&iface->main_dev);
751 if (iface->l3_dev.dev)
752 device_remove_user(&iface->l3_dev);
753 interface_handle_config_change(iface);
755 case IFPEV_LINK_LOST:
756 if (iface->state != IFS_UP)
759 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
760 mark_interface_down(iface);
761 iface->state = IFS_SETUP;
767 interface_write_resolv_conf(iface->jail);
770 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
773 iface->proto->free(iface->proto);
776 iface->state = IFS_DOWN;
777 iface->proto = state;
781 state->proto_event = interface_proto_event_cb;
782 state->iface = iface;
786 interface_alloc(const char *name, struct blob_attr *config, bool dynamic)
788 struct interface *iface;
789 struct blob_attr *tb[IFACE_ATTR_MAX];
790 struct blob_attr *cur;
791 const char *proto_name = NULL;
793 bool force_link = false;
795 iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
796 iface->name = strcpy(iface_name, name);
797 INIT_LIST_HEAD(&iface->errors);
798 INIT_LIST_HEAD(&iface->users);
799 INIT_LIST_HEAD(&iface->hotplug_list);
800 INIT_LIST_HEAD(&iface->assignment_classes);
801 interface_ip_init(iface);
802 avl_init(&iface->data, avl_strcmp, false, NULL);
803 iface->config_ip.enabled = false;
805 iface->main_dev.cb = interface_main_dev_cb;
806 iface->l3_dev.cb = interface_l3_dev_cb;
807 iface->ext_dev.cb = interface_ext_dev_cb;
809 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
810 blob_data(config), blob_len(config));
812 if ((cur = tb[IFACE_ATTR_PROTO]))
813 proto_name = blobmsg_data(cur);
815 proto_attach_interface(iface, proto_name);
816 if (iface->proto_handler->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)
819 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
820 iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link);
821 iface->dynamic = dynamic;
822 iface->proto_ip.no_defaultroute =
823 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
824 iface->proto_ip.no_dns =
825 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
827 if ((cur = tb[IFACE_ATTR_DNS]))
828 interface_add_dns_server_list(&iface->config_ip, cur);
830 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
831 interface_add_dns_search_list(&iface->config_ip, cur);
833 if ((cur = tb[IFACE_ATTR_DNS_METRIC]))
834 iface->dns_metric = blobmsg_get_u32(cur);
836 if ((cur = tb[IFACE_ATTR_METRIC]))
837 iface->metric = blobmsg_get_u32(cur);
839 if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
840 iface->assignment_length = blobmsg_get_u32(cur);
843 iface->assignment_iface_id_selection = IFID_FIXED;
844 iface->assignment_fixed_iface_id = in6addr_any;
845 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
847 if ((cur = tb[IFACE_ATTR_IP6IFACEID])) {
848 const char *ifaceid = blobmsg_data(cur);
849 if (!strcmp(ifaceid, "random")) {
850 iface->assignment_iface_id_selection = IFID_RANDOM;
852 else if (!strcmp(ifaceid, "eui64")) {
853 iface->assignment_iface_id_selection = IFID_EUI64;
856 /* we expect an IPv6 address with network id zero here -> fixed iface id
857 if we cannot parse -> revert to iface id 1 */
858 if (inet_pton(AF_INET6,ifaceid,&iface->assignment_fixed_iface_id) != 1 ||
859 iface->assignment_fixed_iface_id.s6_addr32[0] != 0 ||
860 iface->assignment_fixed_iface_id.s6_addr32[1] != 0) {
861 iface->assignment_fixed_iface_id = in6addr_any;
862 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
863 netifd_log_message(L_WARNING, "Failed to parse ip6ifaceid for interface '%s', \
864 falling back to iface id 1.\n", iface->name);
869 iface->assignment_hint = -1;
870 if ((cur = tb[IFACE_ATTR_IP6HINT]))
871 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
872 ~((1 << (64 - iface->assignment_length)) - 1);
874 if ((cur = tb[IFACE_ATTR_IP6CLASS]))
875 interface_add_assignment_classes(iface, cur);
877 if ((cur = tb[IFACE_ATTR_IP6WEIGHT]))
878 iface->assignment_weight = blobmsg_get_u32(cur);
880 if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
881 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
882 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
885 if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
886 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
887 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
890 iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
892 iface->config_autostart = iface->autostart;
895 if ((cur = tb[IFACE_ATTR_JAIL])) {
896 iface->jail = blobmsg_get_string(cur);
897 iface->autostart = false;
903 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
905 struct blob_attr *tb[IFACE_ATTR_MAX];
906 struct blob_attr *cur;
909 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
910 blob_data(config), blob_len(config));
913 if ((cur = tb[IFACE_ATTR_INTERFACE]))
914 iface->parent_ifname = blobmsg_data(cur);
916 if (!iface->parent_ifname)
919 if ((cur = tb[IFACE_ATTR_IFNAME]))
920 iface->ifname = blobmsg_data(cur);
923 if (iface->dynamic) {
924 name = strdup(iface->name);
930 iface->config = config;
931 vlist_add(&interfaces, &iface->node, iface->name);
934 iface = vlist_find(&interfaces, name, iface, node);
937 /* Don't delete dynamic interface on reload */
939 iface->node.version = -1;
946 interface_add(struct interface *iface, struct blob_attr *config)
948 return __interface_add(iface, config, false);
952 interface_add_alias(struct interface *iface, struct blob_attr *config)
954 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
957 return __interface_add(iface, config, true);
961 interface_set_l3_dev(struct interface *iface, struct device *dev)
963 bool enabled = iface->config_ip.enabled;
964 bool claimed = iface->l3_dev.claimed;
966 if (iface->l3_dev.dev == dev)
969 interface_ip_set_enabled(&iface->config_ip, false);
970 interface_ip_set_enabled(&iface->proto_ip, false);
971 interface_ip_flush(&iface->proto_ip);
972 device_add_user(&iface->l3_dev, dev);
976 if (device_claim(&iface->l3_dev) < 0)
979 interface_ip_set_enabled(&iface->config_ip, enabled);
980 interface_ip_set_enabled(&iface->proto_ip, enabled);
985 interface_set_main_dev(struct interface *iface, struct device *dev)
987 bool claimed = iface->l3_dev.claimed;
989 if (iface->main_dev.dev == dev)
992 interface_set_available(iface, false);
993 device_add_user(&iface->main_dev, dev);
995 interface_set_link_state(iface, false);
1000 if (device_claim(&iface->l3_dev) < 0)
1004 if (!iface->l3_dev.dev)
1005 interface_set_l3_dev(iface, dev);
1009 interface_remove_link(struct interface *iface, struct device *dev)
1011 struct device *mdev = iface->main_dev.dev;
1013 if (mdev && mdev->hotplug_ops)
1014 return mdev->hotplug_ops->del(mdev, dev);
1016 if (dev == iface->ext_dev.dev)
1017 device_remove_user(&iface->ext_dev);
1019 if (!iface->main_dev.hotplug)
1020 return UBUS_STATUS_INVALID_ARGUMENT;
1022 if (dev != iface->main_dev.dev)
1023 return UBUS_STATUS_INVALID_ARGUMENT;
1025 interface_set_main_dev(iface, NULL);
1030 interface_add_link(struct interface *iface, struct device *dev, bool link_ext)
1032 struct device *mdev = iface->main_dev.dev;
1037 if (iface->main_dev.hotplug)
1038 device_remove_user(&iface->main_dev);
1041 if (mdev->hotplug_ops)
1042 return mdev->hotplug_ops->add(mdev, dev);
1044 return UBUS_STATUS_NOT_SUPPORTED;
1048 device_add_user(&iface->ext_dev, dev);
1050 interface_set_main_dev(iface, dev);
1051 iface->main_dev.hotplug = true;
1056 interface_handle_link(struct interface *iface, const char *name, bool add, bool link_ext)
1063 dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
1065 ret = UBUS_STATUS_NOT_FOUND;
1070 interface_set_device_config(iface, dev);
1071 device_set_present(dev, true);
1073 ret = interface_add_link(iface, dev, link_ext);
1075 ret = interface_remove_link(iface, dev);
1085 interface_set_up(struct interface *iface)
1088 const char *error = NULL;
1090 iface->autostart = true;
1092 if (iface->state != IFS_DOWN)
1095 interface_clear_errors(iface);
1096 if (iface->available) {
1097 if (iface->main_dev.dev) {
1098 ret = device_claim(&iface->main_dev);
1100 interface_check_state(iface);
1102 error = "DEVICE_CLAIM_FAILED";
1104 ret = __interface_set_up(iface);
1106 error = "SETUP_FAILED";
1109 error = "NO_DEVICE";
1112 interface_add_error(iface, "interface", error, NULL, 0);
1116 interface_set_down(struct interface *iface)
1119 vlist_for_each_element(&interfaces, iface, node)
1120 __interface_set_down(iface, false);
1122 iface->autostart = false;
1123 __interface_set_down(iface, false);
1128 interface_renew(struct interface *iface)
1130 if (iface->state == IFS_TEARDOWN || iface->state == IFS_DOWN)
1133 return interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
1137 interface_start_pending(void)
1139 struct interface *iface;
1141 vlist_for_each_element(&interfaces, iface, node) {
1142 if (iface->autostart)
1143 interface_set_up(iface);
1148 interface_start_jail(const char *jail, const pid_t netns_pid)
1150 struct interface *iface;
1155 netns_fd = system_netns_open(netns_pid);
1159 vlist_for_each_element(&interfaces, iface, node) {
1160 if (!iface->jail || strcmp(iface->jail, jail))
1163 system_link_netns_move(iface->ifname, netns_fd);
1168 waitpid(pr, &wstatus, WUNTRACED | WCONTINUED);
1173 system_netns_set(netns_fd);
1175 vlist_for_each_element(&interfaces, iface, node) {
1176 if (!iface->jail || strcmp(iface->jail, jail))
1179 interface_set_up(iface);
1185 interface_stop_jail(const char *jail, const pid_t netns_pid)
1187 struct interface *iface;
1188 int netns_fd, root_netns;
1192 netns_fd = system_netns_open(netns_pid);
1196 root_netns = system_netns_open(getpid());
1202 waitpid(pr, &wstatus, WUNTRACED | WCONTINUED);
1208 system_netns_set(netns_fd);
1210 vlist_for_each_element(&interfaces, iface, node) {
1211 if (!iface->jail || strcmp(iface->jail, jail))
1214 interface_set_down(iface);
1215 system_link_netns_move(iface->ifname, root_netns);
1221 set_config_state(struct interface *iface, enum interface_config_state s)
1223 __set_config_state(iface, s);
1224 if (iface->state == IFS_DOWN)
1225 interface_handle_config_change(iface);
1227 __interface_set_down(iface, false);
1231 interface_update_start(struct interface *iface, const bool keep_old)
1236 interface_ip_update_start(&iface->proto_ip);
1240 interface_update_complete(struct interface *iface)
1242 interface_ip_update_complete(&iface->proto_ip);
1246 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
1248 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
1249 vlist_simple_replace(&new->dns_search, &old->dns_search);
1253 interface_device_config_changed(struct interface *if_old, struct interface *if_new)
1255 struct blob_attr *ntb[__DEV_ATTR_MAX];
1256 struct blob_attr *otb[__DEV_ATTR_MAX];
1257 struct device *dev = if_old->main_dev.dev;
1258 unsigned long diff = 0;
1260 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
1265 if (if_old->device_config != if_new->device_config)
1268 if (!if_new->device_config)
1271 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb,
1272 blob_data(if_old->config), blob_len(if_old->config));
1274 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, ntb,
1275 blob_data(if_new->config), blob_len(if_new->config));
1277 uci_blob_diff(ntb, otb, &device_attr_list, &diff);
1282 interface_change_config(struct interface *if_old, struct interface *if_new)
1284 struct blob_attr *old_config = if_old->config;
1285 bool reload = false, reload_ip = false, update_prefix_delegation = false;
1287 #define FIELD_CHANGED_STR(field) \
1288 ((!!if_old->field != !!if_new->field) || \
1290 strcmp(if_old->field, if_new->field) != 0))
1292 if (FIELD_CHANGED_STR(parent_ifname)) {
1293 if (if_old->parent_iface.iface)
1294 interface_remove_user(&if_old->parent_iface);
1298 if (!reload && interface_device_config_changed(if_old, if_new))
1301 if (FIELD_CHANGED_STR(ifname) ||
1302 if_old->proto_handler != if_new->proto_handler)
1305 if (!if_old->proto_handler->config_params)
1306 D(INTERFACE, "No config parameters for interface '%s'\n",
1308 else if (!uci_blob_check_equal(if_old->config, if_new->config,
1309 if_old->proto_handler->config_params))
1312 #define UPDATE(field, __var) ({ \
1313 bool __changed = (if_old->field != if_new->field); \
1314 if_old->field = if_new->field; \
1315 __var |= __changed; \
1318 if_old->config = if_new->config;
1319 if (if_old->config_autostart != if_new->config_autostart) {
1320 if (if_old->config_autostart)
1323 if_old->autostart = if_new->config_autostart;
1326 if_old->device_config = if_new->device_config;
1327 if_old->config_autostart = if_new->config_autostart;
1328 if_old->jail = if_new->jail;
1330 if_old->autostart = false;
1332 if_old->ifname = if_new->ifname;
1333 if_old->parent_ifname = if_new->parent_ifname;
1334 if_old->dynamic = if_new->dynamic;
1335 if_old->proto_handler = if_new->proto_handler;
1336 if_old->force_link = if_new->force_link;
1337 if_old->dns_metric = if_new->dns_metric;
1339 if (if_old->proto_ip.no_delegation != if_new->proto_ip.no_delegation) {
1340 if_old->proto_ip.no_delegation = if_new->proto_ip.no_delegation;
1341 update_prefix_delegation = true;
1344 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
1345 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
1347 UPDATE(metric, reload_ip);
1348 UPDATE(proto_ip.no_defaultroute, reload_ip);
1349 UPDATE(ip4table, reload_ip);
1350 UPDATE(ip6table, reload_ip);
1351 interface_merge_assignment_data(if_old, if_new);
1356 D(INTERFACE, "Reload interface '%s' because of config changes\n",
1358 interface_clear_errors(if_old);
1359 set_config_state(if_old, IFC_RELOAD);
1364 bool config_ip_enabled = if_old->config_ip.enabled;
1365 bool proto_ip_enabled = if_old->proto_ip.enabled;
1367 interface_ip_set_enabled(&if_old->config_ip, false);
1368 interface_ip_set_enabled(&if_old->proto_ip, false);
1369 interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
1370 interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
1373 if (update_prefix_delegation)
1374 interface_update_prefix_delegation(&if_old->proto_ip);
1376 interface_write_resolv_conf(if_old->jail);
1377 if (if_old->main_dev.dev)
1378 interface_check_state(if_old);
1381 if_new->config = NULL;
1382 interface_cleanup(if_new);
1388 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1389 struct vlist_node *node_old)
1391 struct interface *if_old = container_of(node_old, struct interface, node);
1392 struct interface *if_new = container_of(node_new, struct interface, node);
1394 if (node_old && node_new) {
1395 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1396 interface_change_config(if_old, if_new);
1397 } else if (node_old) {
1398 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1399 set_config_state(if_old, IFC_REMOVE);
1400 } else if (node_new) {
1401 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1402 interface_event(if_new, IFEV_CREATE);
1403 proto_init_interface(if_new, if_new->config);
1404 interface_claim_device(if_new);
1405 netifd_ubus_add_interface(if_new);
1411 interface_init_list(void)
1413 vlist_init(&interfaces, avl_strcmp, interface_update);
1414 interfaces.keep_old = true;
1415 interfaces.no_delete = true;