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_JAIL_IFNAME,
38 IFACE_ATTR_DEFAULTROUTE,
41 IFACE_ATTR_DNS_SEARCH,
42 IFACE_ATTR_DNS_METRIC,
51 IFACE_ATTR_IP6IFACEID,
52 IFACE_ATTR_FORCE_LINK,
57 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
58 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
59 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
60 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
61 [IFACE_ATTR_JAIL] = { .name = "jail", .type = BLOBMSG_TYPE_STRING },
62 [IFACE_ATTR_JAIL_IFNAME] = { .name = "jail_ifname", .type = BLOBMSG_TYPE_STRING },
63 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
64 [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
65 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
66 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
67 [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
68 [IFACE_ATTR_DNS_METRIC] = { .name = "dns_metric", .type = BLOBMSG_TYPE_INT32 },
69 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
70 [IFACE_ATTR_IP6ASSIGN] = { .name = "ip6assign", .type = BLOBMSG_TYPE_INT32 },
71 [IFACE_ATTR_IP6HINT] = { .name = "ip6hint", .type = BLOBMSG_TYPE_STRING },
72 [IFACE_ATTR_IP4TABLE] = { .name = "ip4table", .type = BLOBMSG_TYPE_STRING },
73 [IFACE_ATTR_IP6TABLE] = { .name = "ip6table", .type = BLOBMSG_TYPE_STRING },
74 [IFACE_ATTR_IP6CLASS] = { .name = "ip6class", .type = BLOBMSG_TYPE_ARRAY },
75 [IFACE_ATTR_DELEGATE] = { .name = "delegate", .type = BLOBMSG_TYPE_BOOL },
76 [IFACE_ATTR_IP6IFACEID] = { .name = "ip6ifaceid", .type = BLOBMSG_TYPE_STRING },
77 [IFACE_ATTR_FORCE_LINK] = { .name = "force_link", .type = BLOBMSG_TYPE_BOOL },
78 [IFACE_ATTR_IP6WEIGHT] = { .name = "ip6weight", .type = BLOBMSG_TYPE_INT32 },
81 const struct uci_blob_param_list interface_attr_list = {
82 .n_params = IFACE_ATTR_MAX,
83 .params = iface_attrs,
87 interface_set_main_dev(struct interface *iface, struct device *dev);
89 interface_event(struct interface *iface, enum interface_event ev);
92 interface_error_flush(struct interface *iface)
94 struct interface_error *error, *tmp;
96 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
97 list_del(&error->list);
103 interface_clear_errors(struct interface *iface)
105 /* don't flush the errors in case the configured protocol handler matches the
106 running protocol handler and is having the last error capability */
107 if (!(iface->proto &&
108 (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
109 (iface->proto->handler->name == iface->proto_handler->name)))
110 interface_error_flush(iface);
113 void interface_add_error(struct interface *iface, const char *subsystem,
114 const char *code, const char **data, int n_data)
116 struct interface_error *error;
119 char *dest, *d_subsys, *d_code;
121 /* if the configured protocol handler has the last error support capability,
122 errors should only be added if the running protocol handler matches the
125 (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
126 (iface->proto->handler->name != iface->proto_handler->name))
130 len = n_data * sizeof(char *);
131 datalen = alloca(len);
132 for (i = 0; i < n_data; i++) {
133 datalen[i] = strlen(data[i]) + 1;
138 error = calloc_a(sizeof(*error) + sizeof(char *) + len,
139 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
140 &d_code, code ? strlen(code) + 1 : 0);
144 /* Only keep the last flagged error, prevent this list grows unlimitted in case the
145 protocol can't be established (e.g auth failure) */
146 if (iface->proto_handler->flags & PROTO_FLAG_LASTERROR)
147 interface_error_flush(iface);
149 list_add_tail(&error->list, &iface->errors);
151 dest = (char *) &error->data[n_data + 1];
152 for (i = 0; i < n_data; i++) {
153 error->data[i] = dest;
154 memcpy(dest, data[i], datalen[i]);
157 error->data[n_data] = NULL;
160 error->subsystem = strcpy(d_subsys, subsystem);
163 error->code = strcpy(d_code, code);
167 interface_data_del(struct interface *iface, struct interface_data *data)
169 avl_delete(&iface->data, &data->node);
174 interface_data_flush(struct interface *iface)
176 struct interface_data *d, *tmp;
178 avl_for_each_element_safe(&iface->data, d, node, tmp)
179 interface_data_del(iface, d);
183 interface_add_data(struct interface *iface, const struct blob_attr *data)
185 struct interface_data *n, *o;
187 if (!blobmsg_check_attr(data, true))
188 return UBUS_STATUS_INVALID_ARGUMENT;
190 const char *name = blobmsg_name(data);
191 unsigned len = blob_pad_len(data);
193 o = avl_find_element(&iface->data, name, o, node);
195 if (blob_pad_len(o->data) == len && !memcmp(o->data, data, len))
198 interface_data_del(iface, o);
201 n = calloc(1, sizeof(*n) + len);
203 return UBUS_STATUS_UNKNOWN_ERROR;
205 memcpy(n->data, data, len);
206 n->node.key = blobmsg_name(n->data);
207 avl_insert(&iface->data, &n->node);
209 iface->updated |= IUF_DATA;
213 int interface_parse_data(struct interface *iface, const struct blob_attr *attr)
215 struct blob_attr *cur;
220 blob_for_each_attr(cur, attr, rem) {
221 ret = interface_add_data(iface, cur);
226 if (iface->updated && iface->state == IFS_UP)
227 interface_event(iface, IFEV_UPDATE);
233 interface_event(struct interface *iface, enum interface_event ev)
235 struct interface_user *dep, *tmp;
236 struct device *adev = NULL;
238 list_for_each_entry_safe(dep, tmp, &iface->users, list)
239 dep->cb(dep, iface, ev);
241 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
242 dep->cb(dep, iface, ev);
246 interface_error_flush(iface);
247 adev = iface->l3_dev.dev;
251 alias_notify_device(iface->name, adev);
259 interface_flush_state(struct interface *iface)
261 if (iface->l3_dev.dev)
262 device_release(&iface->l3_dev);
263 interface_data_flush(iface);
267 mark_interface_down(struct interface *iface)
269 enum interface_state state = iface->state;
271 if (state == IFS_DOWN)
274 iface->link_up_event = false;
275 iface->state = IFS_DOWN;
279 interface_event(iface, IFEV_DOWN);
282 interface_event(iface, IFEV_UP_FAILED);
287 interface_ip_set_enabled(&iface->config_ip, false);
288 interface_ip_set_enabled(&iface->proto_ip, false);
289 interface_ip_flush(&iface->proto_ip);
290 interface_flush_state(iface);
291 system_flush_routes();
295 __set_config_state(struct interface *iface, enum interface_config_state s)
297 iface->config_state = s;
301 __interface_set_down(struct interface *iface, bool force)
303 enum interface_state state = iface->state;
307 iface->state = IFS_TEARDOWN;
309 __set_config_state(iface, IFC_REMOVE);
312 interface_event(iface, IFEV_DOWN);
314 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
316 interface_flush_state(iface);
320 if (iface->main_dev.dev)
321 device_release(&iface->main_dev);
329 __interface_set_up(struct interface *iface)
333 netifd_log_message(L_NOTICE, "Interface '%s' is setting up now\n", iface->name);
335 iface->state = IFS_SETUP;
336 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
338 mark_interface_down(iface);
344 interface_check_state(struct interface *iface)
346 bool link_state = iface->link_state || iface->force_link;
348 switch (iface->state) {
351 if (!iface->enabled || !link_state) {
352 iface->state = IFS_TEARDOWN;
354 __set_config_state(iface, IFC_REMOVE);
356 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
360 if (!iface->available)
363 if (iface->autostart && iface->enabled && link_state && !config_init)
364 __interface_set_up(iface);
372 interface_set_enabled(struct interface *iface, bool new_state)
374 if (iface->enabled == new_state)
377 netifd_log_message(L_NOTICE, "Interface '%s' is %s\n", iface->name, new_state ? "enabled" : "disabled");
378 iface->enabled = new_state;
379 interface_check_state(iface);
383 interface_set_link_state(struct interface *iface, bool new_state)
385 if (iface->link_state == new_state)
388 netifd_log_message(L_NOTICE, "Interface '%s' has link connectivity %s\n", iface->name, new_state ? "" : "loss");
389 iface->link_state = new_state;
390 interface_check_state(iface);
392 if (new_state && iface->force_link && iface->state == IFS_UP && !iface->link_up_event) {
393 interface_event(iface, IFEV_LINK_UP);
394 iface->link_up_event = true;
399 interface_ext_dev_cb(struct device_user *dep, enum device_event ev)
401 if (ev == DEV_EVENT_REMOVE)
402 device_remove_user(dep);
406 interface_main_dev_cb(struct device_user *dep, enum device_event ev)
408 struct interface *iface;
410 iface = container_of(dep, struct interface, main_dev);
413 interface_set_available(iface, true);
415 case DEV_EVENT_REMOVE:
416 interface_set_available(iface, false);
417 if (dep->dev && dep->dev->external)
418 interface_set_main_dev(iface, NULL);
421 interface_set_enabled(iface, true);
424 interface_set_enabled(iface, false);
426 case DEV_EVENT_LINK_UP:
427 interface_set_link_state(iface, true);
429 case DEV_EVENT_LINK_DOWN:
430 interface_set_link_state(iface, false);
432 case DEV_EVENT_TOPO_CHANGE:
433 interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
441 interface_l3_dev_cb(struct device_user *dep, enum device_event ev)
443 struct interface *iface;
445 iface = container_of(dep, struct interface, l3_dev);
446 if (iface->l3_dev.dev == iface->main_dev.dev)
450 case DEV_EVENT_LINK_DOWN:
451 if (iface->proto_handler->flags & PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN)
452 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
460 interface_set_available(struct interface *iface, bool new_state)
462 if (iface->available == new_state)
465 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
466 iface->available = new_state;
469 if (iface->autostart && !config_init)
470 interface_set_up(iface);
472 __interface_set_down(iface, true);
476 interface_add_user(struct interface_user *dep, struct interface *iface)
479 list_add(&dep->list, &iface_all_users);
484 list_add(&dep->list, &iface->users);
485 if (iface->state == IFS_UP)
486 dep->cb(dep, iface, IFEV_UP);
490 interface_remove_user(struct interface_user *dep)
492 list_del_init(&dep->list);
497 interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
499 struct blob_attr *cur;
502 blobmsg_for_each_attr(cur, list, rem) {
503 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
506 if (!blobmsg_check_attr(cur, false))
509 struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
510 memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
511 list_add(&c->head, &iface->assignment_classes);
516 interface_clear_assignment_classes(struct interface *iface)
518 while (!list_empty(&iface->assignment_classes)) {
519 struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
520 struct interface_assignment_class, head);
527 interface_merge_assignment_data(struct interface *old, struct interface *new)
529 bool changed = (old->assignment_hint != new->assignment_hint ||
530 old->assignment_length != new->assignment_length ||
531 old->assignment_iface_id_selection != new->assignment_iface_id_selection ||
532 old->assignment_weight != new->assignment_weight ||
533 (old->assignment_iface_id_selection == IFID_FIXED &&
534 memcmp(&old->assignment_fixed_iface_id, &new->assignment_fixed_iface_id,
535 sizeof(old->assignment_fixed_iface_id))) ||
536 list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
538 struct interface_assignment_class *c;
539 list_for_each_entry(c, &new->assignment_classes, head) {
540 /* Compare list entries one-by-one to see if there was a change */
541 if (list_empty(&old->assignment_classes)) /* The new list is longer */
547 struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
548 struct interface_assignment_class, head);
550 if (strcmp(c_old->name, c->name)) /* An entry didn't match */
553 list_del(&c_old->head);
557 /* The old list was longer than the new one or the last entry didn't match */
558 if (!list_empty(&old->assignment_classes)) {
559 interface_clear_assignment_classes(old);
563 list_splice_init(&new->assignment_classes, &old->assignment_classes);
566 old->assignment_hint = new->assignment_hint;
567 old->assignment_length = new->assignment_length;
568 old->assignment_iface_id_selection = new->assignment_iface_id_selection;
569 old->assignment_fixed_iface_id = new->assignment_fixed_iface_id;
570 old->assignment_weight = new->assignment_weight;
571 interface_refresh_assignments(true);
576 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
578 struct interface *alias = container_of(dep, struct interface, parent_iface);
579 struct device *dev = iface->l3_dev.dev;
586 interface_set_main_dev(alias, dev);
587 interface_set_available(alias, true);
591 interface_set_available(alias, false);
592 interface_set_main_dev(alias, NULL);
595 interface_remove_user(dep);
603 interface_set_device_config(struct interface *iface, struct device *dev)
605 if (!dev || !dev->default_config)
608 if (!iface->device_config &&
609 (!dev->iface_config || dev->config_iface != iface))
612 dev->config_iface = iface;
613 dev->iface_config = iface->device_config;
614 device_apply_config(dev, dev->type, iface->config);
618 interface_claim_device(struct interface *iface)
620 struct interface *parent;
621 struct device *dev = NULL;
623 if (iface->parent_iface.iface)
624 interface_remove_user(&iface->parent_iface);
628 if (iface->parent_ifname) {
629 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
630 iface->parent_iface.cb = interface_alias_cb;
631 interface_add_user(&iface->parent_iface, parent);
632 } else if (iface->ifname &&
633 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
634 dev = device_get(iface->ifname, true);
635 interface_set_device_config(iface, dev);
637 dev = iface->ext_dev.dev;
641 interface_set_main_dev(iface, dev);
645 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
646 interface_set_available(iface, true);
650 interface_cleanup_state(struct interface *iface)
652 interface_set_available(iface, false);
654 interface_flush_state(iface);
655 interface_clear_errors(iface);
656 interface_set_proto_state(iface, NULL);
658 interface_set_main_dev(iface, NULL);
659 interface_set_l3_dev(iface, NULL);
663 interface_cleanup(struct interface *iface)
665 struct interface_user *dep, *tmp;
667 uloop_timeout_cancel(&iface->remove_timer);
668 device_remove_user(&iface->ext_dev);
670 if (iface->parent_iface.iface)
671 interface_remove_user(&iface->parent_iface);
673 list_for_each_entry_safe(dep, tmp, &iface->users, list)
674 interface_remove_user(dep);
676 interface_clear_assignment_classes(iface);
677 interface_ip_flush(&iface->config_ip);
678 interface_cleanup_state(iface);
682 interface_do_free(struct interface *iface)
684 interface_event(iface, IFEV_FREE);
685 interface_cleanup(iface);
687 netifd_ubus_remove_interface(iface);
688 avl_delete(&interfaces.avl, &iface->node.avl);
691 if (iface->jail_ifname)
692 free(iface->jail_ifname);
698 interface_do_reload(struct interface *iface)
700 interface_event(iface, IFEV_RELOAD);
701 interface_cleanup_state(iface);
702 proto_init_interface(iface, iface->config);
703 interface_claim_device(iface);
707 interface_handle_config_change(struct interface *iface)
709 enum interface_config_state state = iface->config_state;
711 iface->config_state = IFC_NORMAL;
716 interface_do_reload(iface);
719 interface_do_free(iface);
722 if (iface->autostart)
723 interface_set_up(iface);
727 interface_proto_event_cb(struct interface_proto_state *state, enum interface_proto_event ev)
729 struct interface *iface = state->iface;
733 if (iface->state != IFS_SETUP) {
734 if (iface->state == IFS_UP && iface->updated)
735 interface_event(iface, IFEV_UPDATE);
739 if (!iface->l3_dev.dev)
740 interface_set_l3_dev(iface, iface->main_dev.dev);
742 interface_ip_set_enabled(&iface->config_ip, true);
743 interface_ip_set_enabled(&iface->proto_ip, true);
744 system_flush_routes();
745 iface->state = IFS_UP;
746 iface->start_time = system_get_rtime();
747 interface_event(iface, IFEV_UP);
748 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
751 if (iface->state == IFS_DOWN)
754 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
755 mark_interface_down(iface);
756 if (iface->main_dev.dev)
757 device_release(&iface->main_dev);
758 if (iface->l3_dev.dev)
759 device_remove_user(&iface->l3_dev);
760 interface_handle_config_change(iface);
762 case IFPEV_LINK_LOST:
763 if (iface->state != IFS_UP)
766 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
767 mark_interface_down(iface);
768 iface->state = IFS_SETUP;
774 interface_write_resolv_conf(iface->jail);
777 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
780 iface->proto->free(iface->proto);
783 iface->state = IFS_DOWN;
784 iface->proto = state;
788 state->proto_event = interface_proto_event_cb;
789 state->iface = iface;
793 interface_alloc(const char *name, struct blob_attr *config, bool dynamic)
795 struct interface *iface;
796 struct blob_attr *tb[IFACE_ATTR_MAX];
797 struct blob_attr *cur;
798 const char *proto_name = NULL;
800 bool force_link = false;
802 iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
803 iface->name = strcpy(iface_name, name);
804 INIT_LIST_HEAD(&iface->errors);
805 INIT_LIST_HEAD(&iface->users);
806 INIT_LIST_HEAD(&iface->hotplug_list);
807 INIT_LIST_HEAD(&iface->assignment_classes);
808 interface_ip_init(iface);
809 avl_init(&iface->data, avl_strcmp, false, NULL);
810 iface->config_ip.enabled = false;
812 iface->main_dev.cb = interface_main_dev_cb;
813 iface->l3_dev.cb = interface_l3_dev_cb;
814 iface->ext_dev.cb = interface_ext_dev_cb;
816 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
817 blob_data(config), blob_len(config));
819 if ((cur = tb[IFACE_ATTR_PROTO]))
820 proto_name = blobmsg_data(cur);
822 proto_attach_interface(iface, proto_name);
823 if (iface->proto_handler->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)
826 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
827 iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link);
828 iface->dynamic = dynamic;
829 iface->proto_ip.no_defaultroute =
830 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
831 iface->proto_ip.no_dns =
832 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
834 if ((cur = tb[IFACE_ATTR_DNS]))
835 interface_add_dns_server_list(&iface->config_ip, cur);
837 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
838 interface_add_dns_search_list(&iface->config_ip, cur);
840 if ((cur = tb[IFACE_ATTR_DNS_METRIC]))
841 iface->dns_metric = blobmsg_get_u32(cur);
843 if ((cur = tb[IFACE_ATTR_METRIC]))
844 iface->metric = blobmsg_get_u32(cur);
846 if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
847 iface->assignment_length = blobmsg_get_u32(cur);
850 iface->assignment_iface_id_selection = IFID_FIXED;
851 iface->assignment_fixed_iface_id = in6addr_any;
852 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
854 if ((cur = tb[IFACE_ATTR_IP6IFACEID])) {
855 const char *ifaceid = blobmsg_data(cur);
856 if (!strcmp(ifaceid, "random")) {
857 iface->assignment_iface_id_selection = IFID_RANDOM;
859 else if (!strcmp(ifaceid, "eui64")) {
860 iface->assignment_iface_id_selection = IFID_EUI64;
863 /* we expect an IPv6 address with network id zero here -> fixed iface id
864 if we cannot parse -> revert to iface id 1 */
865 if (inet_pton(AF_INET6,ifaceid,&iface->assignment_fixed_iface_id) != 1 ||
866 iface->assignment_fixed_iface_id.s6_addr32[0] != 0 ||
867 iface->assignment_fixed_iface_id.s6_addr32[1] != 0) {
868 iface->assignment_fixed_iface_id = in6addr_any;
869 iface->assignment_fixed_iface_id.s6_addr[15] = 1;
870 netifd_log_message(L_WARNING, "Failed to parse ip6ifaceid for interface '%s', \
871 falling back to iface id 1.\n", iface->name);
876 iface->assignment_hint = -1;
877 if ((cur = tb[IFACE_ATTR_IP6HINT]))
878 iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
879 ~((1 << (64 - iface->assignment_length)) - 1);
881 if ((cur = tb[IFACE_ATTR_IP6CLASS]))
882 interface_add_assignment_classes(iface, cur);
884 if ((cur = tb[IFACE_ATTR_IP6WEIGHT]))
885 iface->assignment_weight = blobmsg_get_u32(cur);
887 if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
888 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
889 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
892 if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
893 if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
894 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
897 iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
899 iface->config_autostart = iface->autostart;
902 if ((cur = tb[IFACE_ATTR_JAIL])) {
903 iface->jail = strdup(blobmsg_get_string(cur));
904 iface->autostart = false;
907 iface->jail_ifname = NULL;
908 if ((cur = tb[IFACE_ATTR_JAIL_IFNAME]))
909 iface->jail_ifname = strdup(blobmsg_get_string(cur));
914 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
916 struct blob_attr *tb[IFACE_ATTR_MAX];
917 struct blob_attr *cur;
920 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
921 blob_data(config), blob_len(config));
924 if ((cur = tb[IFACE_ATTR_INTERFACE]))
925 iface->parent_ifname = blobmsg_data(cur);
927 if (!iface->parent_ifname)
930 if ((cur = tb[IFACE_ATTR_IFNAME]))
931 iface->ifname = blobmsg_data(cur);
934 if (iface->dynamic) {
935 name = strdup(iface->name);
941 iface->config = config;
942 vlist_add(&interfaces, &iface->node, iface->name);
945 iface = vlist_find(&interfaces, name, iface, node);
948 /* Don't delete dynamic interface on reload */
950 iface->node.version = -1;
957 interface_add(struct interface *iface, struct blob_attr *config)
959 return __interface_add(iface, config, false);
963 interface_add_alias(struct interface *iface, struct blob_attr *config)
965 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
968 return __interface_add(iface, config, true);
972 interface_set_l3_dev(struct interface *iface, struct device *dev)
974 bool enabled = iface->config_ip.enabled;
975 bool claimed = iface->l3_dev.claimed;
977 if (iface->l3_dev.dev == dev)
980 interface_ip_set_enabled(&iface->config_ip, false);
981 interface_ip_set_enabled(&iface->proto_ip, false);
982 interface_ip_flush(&iface->proto_ip);
983 device_add_user(&iface->l3_dev, dev);
987 if (device_claim(&iface->l3_dev) < 0)
990 interface_ip_set_enabled(&iface->config_ip, enabled);
991 interface_ip_set_enabled(&iface->proto_ip, enabled);
996 interface_set_main_dev(struct interface *iface, struct device *dev)
998 bool claimed = iface->l3_dev.claimed;
1000 if (iface->main_dev.dev == dev)
1003 interface_set_available(iface, false);
1004 device_add_user(&iface->main_dev, dev);
1006 interface_set_link_state(iface, false);
1011 if (device_claim(&iface->l3_dev) < 0)
1015 if (!iface->l3_dev.dev)
1016 interface_set_l3_dev(iface, dev);
1020 interface_remove_link(struct interface *iface, struct device *dev)
1022 struct device *mdev = iface->main_dev.dev;
1024 if (mdev && mdev->hotplug_ops)
1025 return mdev->hotplug_ops->del(mdev, dev);
1027 if (dev == iface->ext_dev.dev)
1028 device_remove_user(&iface->ext_dev);
1030 if (!iface->main_dev.hotplug)
1031 return UBUS_STATUS_INVALID_ARGUMENT;
1033 if (dev != iface->main_dev.dev)
1034 return UBUS_STATUS_INVALID_ARGUMENT;
1036 interface_set_main_dev(iface, NULL);
1041 interface_add_link(struct interface *iface, struct device *dev, bool link_ext)
1043 struct device *mdev = iface->main_dev.dev;
1048 if (iface->main_dev.hotplug)
1049 device_remove_user(&iface->main_dev);
1052 if (mdev->hotplug_ops)
1053 return mdev->hotplug_ops->add(mdev, dev);
1055 return UBUS_STATUS_NOT_SUPPORTED;
1059 device_add_user(&iface->ext_dev, dev);
1061 interface_set_main_dev(iface, dev);
1062 iface->main_dev.hotplug = true;
1067 interface_handle_link(struct interface *iface, const char *name, bool add, bool link_ext)
1074 dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
1076 ret = UBUS_STATUS_NOT_FOUND;
1081 interface_set_device_config(iface, dev);
1082 device_set_present(dev, true);
1084 ret = interface_add_link(iface, dev, link_ext);
1086 ret = interface_remove_link(iface, dev);
1096 interface_set_up(struct interface *iface)
1099 const char *error = NULL;
1101 iface->autostart = true;
1103 if (iface->state != IFS_DOWN)
1106 interface_clear_errors(iface);
1107 if (iface->available) {
1108 if (iface->main_dev.dev) {
1109 ret = device_claim(&iface->main_dev);
1111 interface_check_state(iface);
1113 error = "DEVICE_CLAIM_FAILED";
1115 ret = __interface_set_up(iface);
1117 error = "SETUP_FAILED";
1120 error = "NO_DEVICE";
1123 interface_add_error(iface, "interface", error, NULL, 0);
1127 interface_set_down(struct interface *iface)
1130 vlist_for_each_element(&interfaces, iface, node)
1131 __interface_set_down(iface, false);
1133 iface->autostart = false;
1134 __interface_set_down(iface, false);
1139 interface_renew(struct interface *iface)
1141 if (iface->state == IFS_TEARDOWN || iface->state == IFS_DOWN)
1144 return interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
1148 interface_start_pending(void)
1150 struct interface *iface;
1152 vlist_for_each_element(&interfaces, iface, node) {
1153 if (iface->autostart)
1154 interface_set_up(iface);
1159 interface_start_jail(const char *jail, const pid_t netns_pid)
1161 struct interface *iface;
1166 netns_fd = system_netns_open(netns_pid);
1170 vlist_for_each_element(&interfaces, iface, node) {
1171 if (!iface->jail || strcmp(iface->jail, jail))
1174 system_link_netns_move(iface->main_dev.dev, netns_fd, iface->jail_ifname);
1181 waitpid(pr, &wstatus, WUNTRACED | WCONTINUED);
1186 netns_fd = system_netns_open(netns_pid);
1190 system_netns_set(netns_fd);
1192 vlist_for_each_element(&interfaces, iface, node) {
1193 if (!iface->jail || strcmp(iface->jail, jail))
1197 * The interface has already been renamed and is inside target
1198 * namespace, hence overwrite ifname with jail_ifname for
1199 * interface_set_up().
1200 * We are inside a fork which got it's own copy of the interfaces
1201 * list, so we can mess with it :)
1203 if (iface->jail_ifname)
1204 iface->ifname = iface->jail_ifname;
1206 interface_do_reload(iface);
1207 interface_set_up(iface);
1215 interface_stop_jail(const char *jail, const pid_t netns_pid)
1217 struct interface *iface;
1218 int netns_fd, root_netns;
1220 pid_t parent_pid = getpid();
1222 const char *orig_ifname;
1226 waitpid(pr, &wstatus, WUNTRACED | WCONTINUED);
1231 root_netns = system_netns_open(parent_pid);
1235 netns_fd = system_netns_open(netns_pid);
1239 system_netns_set(netns_fd);
1241 vlist_for_each_element(&interfaces, iface, node) {
1242 if (!iface->jail || strcmp(iface->jail, jail))
1245 orig_ifname = iface->ifname;
1246 if (iface->jail_ifname)
1247 iface->ifname = iface->jail_ifname;
1249 interface_do_reload(iface);
1250 interface_set_down(iface);
1251 system_link_netns_move(iface->main_dev.dev, root_netns, orig_ifname);
1260 set_config_state(struct interface *iface, enum interface_config_state s)
1262 __set_config_state(iface, s);
1263 if (iface->state == IFS_DOWN)
1264 interface_handle_config_change(iface);
1266 __interface_set_down(iface, false);
1270 interface_update_start(struct interface *iface, const bool keep_old)
1275 interface_ip_update_start(&iface->proto_ip);
1279 interface_update_complete(struct interface *iface)
1281 interface_ip_update_complete(&iface->proto_ip);
1285 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
1287 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
1288 vlist_simple_replace(&new->dns_search, &old->dns_search);
1292 interface_device_config_changed(struct interface *if_old, struct interface *if_new)
1294 struct blob_attr *ntb[__DEV_ATTR_MAX];
1295 struct blob_attr *otb[__DEV_ATTR_MAX];
1296 struct device *dev = if_old->main_dev.dev;
1297 unsigned long diff = 0;
1299 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
1304 if (if_old->device_config != if_new->device_config)
1307 if (!if_new->device_config)
1310 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb,
1311 blob_data(if_old->config), blob_len(if_old->config));
1313 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, ntb,
1314 blob_data(if_new->config), blob_len(if_new->config));
1316 uci_blob_diff(ntb, otb, &device_attr_list, &diff);
1321 interface_change_config(struct interface *if_old, struct interface *if_new)
1323 struct blob_attr *old_config = if_old->config;
1324 bool reload = false, reload_ip = false, update_prefix_delegation = false;
1326 #define FIELD_CHANGED_STR(field) \
1327 ((!!if_old->field != !!if_new->field) || \
1329 strcmp(if_old->field, if_new->field) != 0))
1331 if (FIELD_CHANGED_STR(parent_ifname)) {
1332 if (if_old->parent_iface.iface)
1333 interface_remove_user(&if_old->parent_iface);
1337 if (!reload && interface_device_config_changed(if_old, if_new))
1340 if (FIELD_CHANGED_STR(ifname) ||
1341 if_old->proto_handler != if_new->proto_handler)
1344 if (!if_old->proto_handler->config_params)
1345 D(INTERFACE, "No config parameters for interface '%s'\n",
1347 else if (!uci_blob_check_equal(if_old->config, if_new->config,
1348 if_old->proto_handler->config_params))
1351 #define UPDATE(field, __var) ({ \
1352 bool __changed = (if_old->field != if_new->field); \
1353 if_old->field = if_new->field; \
1354 __var |= __changed; \
1357 if_old->config = if_new->config;
1358 if (if_old->config_autostart != if_new->config_autostart) {
1359 if (if_old->config_autostart)
1362 if_old->autostart = if_new->config_autostart;
1365 if_old->device_config = if_new->device_config;
1366 if_old->config_autostart = if_new->config_autostart;
1370 if_old->jail = if_new->jail;
1372 if_old->autostart = false;
1374 if (if_old->jail_ifname)
1375 free(if_old->jail_ifname);
1377 if_old->jail_ifname = if_new->jail_ifname;
1379 if_old->ifname = if_new->ifname;
1380 if_old->parent_ifname = if_new->parent_ifname;
1381 if_old->dynamic = if_new->dynamic;
1382 if_old->proto_handler = if_new->proto_handler;
1383 if_old->force_link = if_new->force_link;
1384 if_old->dns_metric = if_new->dns_metric;
1386 if (if_old->proto_ip.no_delegation != if_new->proto_ip.no_delegation) {
1387 if_old->proto_ip.no_delegation = if_new->proto_ip.no_delegation;
1388 update_prefix_delegation = true;
1391 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
1392 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
1394 UPDATE(metric, reload_ip);
1395 UPDATE(proto_ip.no_defaultroute, reload_ip);
1396 UPDATE(ip4table, reload_ip);
1397 UPDATE(ip6table, reload_ip);
1398 interface_merge_assignment_data(if_old, if_new);
1403 D(INTERFACE, "Reload interface '%s' because of config changes\n",
1405 interface_clear_errors(if_old);
1406 set_config_state(if_old, IFC_RELOAD);
1411 bool config_ip_enabled = if_old->config_ip.enabled;
1412 bool proto_ip_enabled = if_old->proto_ip.enabled;
1414 interface_ip_set_enabled(&if_old->config_ip, false);
1415 interface_ip_set_enabled(&if_old->proto_ip, false);
1416 interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
1417 interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
1420 if (update_prefix_delegation)
1421 interface_update_prefix_delegation(&if_old->proto_ip);
1423 interface_write_resolv_conf(if_old->jail);
1424 if (if_old->main_dev.dev)
1425 interface_check_state(if_old);
1428 if_new->config = NULL;
1429 interface_cleanup(if_new);
1435 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
1436 struct vlist_node *node_old)
1438 struct interface *if_old = container_of(node_old, struct interface, node);
1439 struct interface *if_new = container_of(node_new, struct interface, node);
1441 if (node_old && node_new) {
1442 D(INTERFACE, "Update interface '%s'\n", if_new->name);
1443 interface_change_config(if_old, if_new);
1444 } else if (node_old) {
1445 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
1446 set_config_state(if_old, IFC_REMOVE);
1447 } else if (node_new) {
1448 D(INTERFACE, "Create interface '%s'\n", if_new->name);
1449 interface_event(if_new, IFEV_CREATE);
1450 proto_init_interface(if_new, if_new->config);
1451 interface_claim_device(if_new);
1452 netifd_ubus_add_interface(if_new);
1458 interface_init_list(void)
1460 vlist_init(&interfaces, avl_strcmp, interface_update);
1461 interfaces.keep_old = true;
1462 interfaces.no_delete = true;