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,
45 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
46 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
47 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
48 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
49 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
50 [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
51 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
52 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
53 [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
54 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
55 [IFACE_ATTR_IP6ASSIGN] = { .name = "ip6assign", .type = BLOBMSG_TYPE_INT32 },
56 [IFACE_ATTR_IP6HINT] = { .name = "ip6hint", .type = BLOBMSG_TYPE_STRING },
59 static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = {
60 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
63 const struct config_param_list interface_attr_list = {
64 .n_params = IFACE_ATTR_MAX,
65 .params = iface_attrs,
66 .info = iface_attr_info,
70 interface_clear_errors(struct interface *iface)
72 struct interface_error *error, *tmp;
74 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
75 list_del(&error->list);
80 void interface_add_error(struct interface *iface, const char *subsystem,
81 const char *code, const char **data, int n_data)
83 struct interface_error *error;
86 char *dest, *d_subsys, *d_code;
89 len = n_data * sizeof(char *);
90 datalen = alloca(len);
91 for (i = 0; i < n_data; i++) {
92 datalen[i] = strlen(data[i]) + 1;
97 error = calloc_a(sizeof(*error) + sizeof(char *) + len,
98 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
99 &d_code, code ? strlen(code) + 1 : 0);
103 list_add_tail(&error->list, &iface->errors);
105 dest = (char *) &error->data[n_data + 1];
106 for (i = 0; i < n_data; i++) {
107 error->data[i] = dest;
108 memcpy(dest, data[i], datalen[i]);
111 error->data[n_data++] = NULL;
114 error->subsystem = strcpy(d_subsys, subsystem);
117 error->code = strcpy(d_code, code);
121 interface_data_del(struct interface *iface, struct interface_data *data)
123 avl_delete(&iface->data, &data->node);
128 interface_data_flush(struct interface *iface)
130 struct interface_data *d, *tmp;
132 avl_for_each_element_safe(&iface->data, d, node, tmp)
133 interface_data_del(iface, d);
137 interface_add_data(struct interface *iface, const struct blob_attr *data)
139 struct interface_data *n, *o;
141 if (!blobmsg_check_attr(data, true))
142 return UBUS_STATUS_INVALID_ARGUMENT;
144 n = calloc(1, sizeof(*n) + blob_pad_len(data));
145 memcpy(n->data, data, blob_pad_len(data));
146 n->node.key = blobmsg_name(data);
148 o = avl_find_element(&iface->data, n->node.key, o, node);
150 interface_data_del(iface, o);
152 avl_insert(&iface->data, &n->node);
157 interface_event(struct interface *iface, enum interface_event ev)
159 struct interface_user *dep, *tmp;
160 struct device *adev = NULL;
162 list_for_each_entry_safe(dep, tmp, &iface->users, list)
163 dep->cb(dep, iface, ev);
165 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
166 dep->cb(dep, iface, ev);
170 adev = iface->l3_dev.dev;
173 alias_notify_device(iface->name, adev);
181 interface_flush_state(struct interface *iface)
183 if (iface->l3_dev.dev)
184 device_release(&iface->l3_dev);
185 interface_data_flush(iface);
189 mark_interface_down(struct interface *iface)
191 enum interface_state state = iface->state;
193 iface->state = IFS_DOWN;
195 interface_event(iface, IFEV_DOWN);
196 interface_ip_set_enabled(&iface->config_ip, false);
197 interface_ip_flush(&iface->proto_ip);
198 interface_flush_state(iface);
199 system_flush_routes();
203 __interface_set_down(struct interface *iface, bool force)
205 if (iface->state == IFS_DOWN ||
206 iface->state == IFS_TEARDOWN)
209 if (iface->state == IFS_UP)
210 interface_event(iface, IFEV_DOWN);
211 iface->state = IFS_TEARDOWN;
212 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
214 interface_flush_state(iface);
218 interface_cb(struct device_user *dep, enum device_event ev)
220 struct interface *iface;
223 iface = container_of(dep, struct interface, main_dev);
228 case DEV_EVENT_REMOVE:
235 interface_set_available(iface, new_state);
236 if (!new_state && dep->dev->external)
237 interface_set_main_dev(iface, NULL);
241 interface_set_available(struct interface *iface, bool new_state)
243 if (iface->available == new_state)
246 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
247 iface->available = new_state;
250 if (iface->autostart && !config_init)
251 interface_set_up(iface);
253 __interface_set_down(iface, true);
257 interface_add_user(struct interface_user *dep, struct interface *iface)
260 list_add(&dep->list, &iface_all_users);
265 list_add(&dep->list, &iface->users);
266 if (iface->state == IFS_UP)
267 dep->cb(dep, iface, IFEV_UP);
271 interface_remove_user(struct interface_user *dep)
273 list_del_init(&dep->list);
278 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
280 struct interface *alias = container_of(dep, struct interface, parent_iface);
281 struct device *dev = iface->l3_dev.dev;
288 interface_set_main_dev(alias, dev);
289 interface_set_available(alias, true);
292 interface_set_available(alias, false);
293 interface_set_main_dev(alias, NULL);
296 interface_remove_user(dep);
304 interface_claim_device(struct interface *iface)
306 struct interface *parent;
307 struct device *dev = NULL;
309 if (iface->parent_iface.iface)
310 interface_remove_user(&iface->parent_iface);
312 if (iface->parent_ifname) {
313 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
314 iface->parent_iface.cb = interface_alias_cb;
315 interface_add_user(&iface->parent_iface, parent);
316 } else if (iface->ifname &&
317 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
318 dev = device_get(iface->ifname, true);
322 interface_set_main_dev(iface, dev);
324 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
325 interface_set_available(iface, true);
329 interface_cleanup_state(struct interface *iface)
331 interface_flush_state(iface);
332 interface_clear_errors(iface);
333 interface_set_proto_state(iface, NULL);
337 interface_cleanup(struct interface *iface)
339 struct interface_user *dep, *tmp;
341 if (iface->parent_iface.iface)
342 interface_remove_user(&iface->parent_iface);
344 list_for_each_entry_safe(dep, tmp, &iface->users, list)
345 interface_remove_user(dep);
347 interface_ip_flush(&iface->config_ip);
348 if (iface->main_dev.dev)
349 interface_set_main_dev(iface, NULL);
351 interface_cleanup_state(iface);
355 interface_do_free(struct interface *iface)
357 interface_event(iface, IFEV_FREE);
358 interface_cleanup(iface);
360 netifd_ubus_remove_interface(iface);
361 avl_delete(&interfaces.avl, &iface->node.avl);
366 interface_do_reload(struct interface *iface)
368 interface_event(iface, IFEV_RELOAD);
369 interface_cleanup_state(iface);
370 proto_init_interface(iface, iface->config);
371 interface_claim_device(iface);
375 interface_handle_config_change(struct interface *iface)
377 enum interface_config_state state = iface->config_state;
379 iface->config_state = IFC_NORMAL;
384 interface_do_reload(iface);
387 interface_do_free(iface);
390 if (iface->autostart && iface->available)
391 interface_set_up(iface);
395 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
397 struct interface *iface = state->iface;
401 if (iface->state != IFS_SETUP)
404 interface_ip_set_enabled(&iface->config_ip, true);
405 system_flush_routes();
406 iface->state = IFS_UP;
407 iface->start_time = system_get_rtime();
408 interface_event(iface, IFEV_UP);
409 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
412 if (iface->state == IFS_DOWN)
415 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
416 mark_interface_down(iface);
417 if (iface->main_dev.dev)
418 device_release(&iface->main_dev);
419 interface_handle_config_change(iface);
421 case IFPEV_LINK_LOST:
422 if (iface->state != IFS_UP)
425 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
426 mark_interface_down(iface);
427 iface->state = IFS_SETUP;
431 interface_write_resolv_conf();
434 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
437 iface->proto->free(iface->proto);
440 iface->state = IFS_DOWN;
441 iface->proto = state;
445 state->proto_event = interface_proto_cb;
446 state->iface = iface;
450 interface_init(struct interface *iface, const char *name,
451 struct blob_attr *config)
453 struct blob_attr *tb[IFACE_ATTR_MAX];
454 struct blob_attr *cur;
455 const char *proto_name = NULL;
457 strncpy(iface->name, name, sizeof(iface->name) - 1);
458 INIT_LIST_HEAD(&iface->errors);
459 INIT_LIST_HEAD(&iface->users);
460 INIT_LIST_HEAD(&iface->hotplug_list);
461 interface_ip_init(iface);
462 avl_init(&iface->data, avl_strcmp, false, NULL);
463 iface->config_ip.enabled = false;
465 iface->main_dev.cb = interface_cb;
467 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
468 blob_data(config), blob_len(config));
470 if ((cur = tb[IFACE_ATTR_PROTO]))
471 proto_name = blobmsg_data(cur);
473 proto_attach_interface(iface, proto_name);
475 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
476 iface->proto_ip.no_defaultroute =
477 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
478 iface->proto_ip.no_dns =
479 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
481 if ((cur = tb[IFACE_ATTR_DNS]))
482 interface_add_dns_server_list(&iface->config_ip, cur);
484 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
485 interface_add_dns_search_list(&iface->config_ip, cur);
487 if ((cur = tb[IFACE_ATTR_METRIC]))
488 iface->metric = blobmsg_get_u32(cur);
490 if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
491 iface->config_ip.assignment_length = blobmsg_get_u32(cur);
493 iface->config_ip.assignment_hint = -1;
494 if ((cur = tb[IFACE_ATTR_IP6HINT]))
495 iface->config_ip.assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
496 ~((1 << (64 - iface->config_ip.assignment_length)) - 1);
498 iface->config_autostart = iface->autostart;
501 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
503 struct blob_attr *tb[IFACE_ATTR_MAX];
504 struct blob_attr *cur;
506 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
507 blob_data(config), blob_len(config));
510 if ((cur = tb[IFACE_ATTR_INTERFACE]))
511 iface->parent_ifname = blobmsg_data(cur);
513 if (!iface->parent_ifname)
516 if ((cur = tb[IFACE_ATTR_IFNAME]))
517 iface->ifname = blobmsg_data(cur);
521 iface->config = config;
522 vlist_add(&interfaces, &iface->node, iface->name);
527 interface_add(struct interface *iface, struct blob_attr *config)
529 __interface_add(iface, config, false);
533 interface_add_alias(struct interface *iface, struct blob_attr *config)
535 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
538 return __interface_add(iface, config, true);
542 interface_set_l3_dev(struct interface *iface, struct device *dev)
544 bool enabled = iface->config_ip.enabled;
545 bool claimed = iface->l3_dev.claimed;
547 if (iface->l3_dev.dev == dev)
550 interface_ip_set_enabled(&iface->config_ip, false);
551 interface_ip_flush(&iface->proto_ip);
552 device_add_user(&iface->l3_dev, dev);
556 device_claim(&iface->l3_dev);
557 interface_ip_set_enabled(&iface->config_ip, enabled);
562 interface_set_main_dev(struct interface *iface, struct device *dev)
564 bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
565 bool claimed = iface->l3_dev.claimed;
567 if (iface->main_dev.dev == dev)
571 interface_set_l3_dev(iface, dev);
573 device_add_user(&iface->main_dev, dev);
575 device_claim(&iface->l3_dev);
577 if (!iface->l3_dev.dev)
578 interface_set_l3_dev(iface, dev);
582 interface_remove_link(struct interface *iface, struct device *dev)
584 struct device *mdev = iface->main_dev.dev;
586 if (mdev && mdev->hotplug_ops)
587 return mdev->hotplug_ops->del(mdev, dev);
589 if (!iface->main_dev.hotplug)
590 return UBUS_STATUS_INVALID_ARGUMENT;
592 if (dev != iface->main_dev.dev)
593 return UBUS_STATUS_INVALID_ARGUMENT;
595 device_remove_user(&iface->main_dev);
600 interface_add_link(struct interface *iface, struct device *dev)
602 struct device *mdev = iface->main_dev.dev;
607 if (iface->main_dev.hotplug)
608 device_remove_user(&iface->main_dev);
611 if (mdev->hotplug_ops)
612 return mdev->hotplug_ops->add(mdev, dev);
614 return UBUS_STATUS_NOT_SUPPORTED;
617 interface_set_main_dev(iface, dev);
618 iface->main_dev.hotplug = true;
623 interface_set_up(struct interface *iface)
627 iface->autostart = true;
629 if (iface->state != IFS_DOWN)
632 interface_clear_errors(iface);
633 if (!iface->available) {
634 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
638 if (iface->main_dev.dev) {
639 ret = device_claim(&iface->main_dev);
644 iface->state = IFS_SETUP;
645 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
647 mark_interface_down(iface);
655 interface_set_down(struct interface *iface)
658 vlist_for_each_element(&interfaces, iface, node)
659 __interface_set_down(iface, false);
661 iface->autostart = false;
662 __interface_set_down(iface, false);
669 interface_start_pending(void)
671 struct interface *iface;
673 vlist_for_each_element(&interfaces, iface, node) {
674 if (iface->available && iface->autostart)
675 interface_set_up(iface);
680 set_config_state(struct interface *iface, enum interface_config_state s)
682 iface->config_state = s;
683 if (iface->state == IFS_DOWN)
684 interface_handle_config_change(iface);
686 __interface_set_down(iface, false);
690 interface_update_start(struct interface *iface)
692 interface_ip_update_start(&iface->proto_ip);
696 interface_update_complete(struct interface *iface)
698 interface_ip_update_complete(&iface->proto_ip);
702 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
704 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
705 vlist_simple_replace(&new->dns_search, &old->dns_search);
709 interface_change_config(struct interface *if_old, struct interface *if_new)
711 struct blob_attr *old_config = if_old->config;
712 const char *old_ifname = if_old->ifname;
713 const char *old_parent_ifname = if_old->parent_ifname;
714 const struct proto_handler *proto = if_old->proto_handler;
716 interface_clear_errors(if_old);
717 if_old->config = if_new->config;
718 if (!if_old->config_autostart && if_new->config_autostart)
719 if_old->autostart = true;
721 if_old->device_config = if_new->device_config;
722 if_old->config_autostart = if_new->config_autostart;
723 if_old->ifname = if_new->ifname;
724 if_old->parent_ifname = if_new->parent_ifname;
725 if_old->proto_handler = if_new->proto_handler;
727 if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
728 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
730 #define FIELD_CHANGED_STR(field) \
731 ((!!if_old->field != !!old_ ## field) || \
733 strcmp(old_ ## field, if_old->field) != 0))
735 if (FIELD_CHANGED_STR(parent_ifname)) {
736 if (if_old->parent_iface.iface)
737 interface_remove_user(&if_old->parent_iface);
741 if (FIELD_CHANGED_STR(ifname) || proto != if_new->proto_handler) {
742 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
747 if (!proto->config_params)
748 D(INTERFACE, "No config parameters for interface '%s'\n",
750 else if (!config_check_equal(old_config, if_new->config,
751 proto->config_params)) {
752 D(INTERFACE, "Reload interface '%s because of config changes\n",
757 #define UPDATE(field) ({ \
758 bool __changed = (if_old->field != if_new->field); \
759 if_old->field = if_new->field; \
763 if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
764 interface_ip_set_enabled(&if_old->config_ip, false);
765 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
766 interface_ip_set_enabled(&if_old->proto_ip, false);
767 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
770 if (UPDATE(config_ip.assignment_length) || UPDATE(config_ip.assignment_hint))
771 interface_refresh_assignments(true);
773 interface_write_resolv_conf();
780 set_config_state(if_old, IFC_RELOAD);
782 if_new->config = NULL;
783 interface_cleanup(if_new);
789 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
790 struct vlist_node *node_old)
792 struct interface *if_old = container_of(node_old, struct interface, node);
793 struct interface *if_new = container_of(node_new, struct interface, node);
795 if (node_old && node_new) {
796 D(INTERFACE, "Update interface '%s'\n", if_new->name);
797 interface_change_config(if_old, if_new);
798 } else if (node_old) {
799 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
800 set_config_state(if_old, IFC_REMOVE);
801 } else if (node_new) {
802 D(INTERFACE, "Create interface '%s'\n", if_new->name);
803 proto_init_interface(if_new, if_new->config);
804 interface_claim_device(if_new);
805 netifd_ubus_add_interface(if_new);
811 interface_init_list(void)
813 vlist_init(&interfaces, avl_strcmp, interface_update);
814 interfaces.keep_old = true;
815 interfaces.no_delete = true;