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,
43 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
44 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
45 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
46 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
47 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
48 [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
49 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
50 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
51 [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
52 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
55 static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = {
56 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
59 const struct config_param_list interface_attr_list = {
60 .n_params = IFACE_ATTR_MAX,
61 .params = iface_attrs,
62 .info = iface_attr_info,
66 interface_clear_errors(struct interface *iface)
68 struct interface_error *error, *tmp;
70 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
71 list_del(&error->list);
76 void interface_add_error(struct interface *iface, const char *subsystem,
77 const char *code, const char **data, int n_data)
79 struct interface_error *error;
82 char *dest, *d_subsys, *d_code;
85 len = n_data * sizeof(char *);
86 datalen = alloca(len);
87 for (i = 0; i < n_data; i++) {
88 datalen[i] = strlen(data[i]) + 1;
93 error = calloc_a(sizeof(*error) + sizeof(char *) + len,
94 &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
95 &d_code, code ? strlen(code) + 1 : 0);
99 list_add_tail(&error->list, &iface->errors);
101 dest = (char *) &error->data[n_data + 1];
102 for (i = 0; i < n_data; i++) {
103 error->data[i] = dest;
104 memcpy(dest, data[i], datalen[i]);
107 error->data[n_data++] = NULL;
110 error->subsystem = strcpy(d_subsys, subsystem);
113 error->code = strcpy(d_code, code);
117 interface_data_del(struct interface *iface, struct interface_data *data)
119 avl_delete(&iface->data, &data->node);
124 interface_data_flush(struct interface *iface)
126 struct interface_data *d, *tmp;
128 avl_for_each_element_safe(&iface->data, d, node, tmp)
129 interface_data_del(iface, d);
133 interface_add_data(struct interface *iface, const struct blob_attr *data)
135 struct interface_data *n, *o;
137 if (!blobmsg_check_attr(data, true))
138 return UBUS_STATUS_INVALID_ARGUMENT;
140 n = calloc(1, sizeof(*n) + blob_pad_len(data));
141 memcpy(n->data, data, blob_pad_len(data));
142 n->node.key = blobmsg_name(data);
144 o = avl_find_element(&iface->data, n->node.key, o, node);
146 interface_data_del(iface, o);
148 avl_insert(&iface->data, &n->node);
153 interface_event(struct interface *iface, enum interface_event ev)
155 struct interface_user *dep, *tmp;
156 struct device *adev = NULL;
158 list_for_each_entry_safe(dep, tmp, &iface->users, list)
159 dep->cb(dep, iface, ev);
161 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
162 dep->cb(dep, iface, ev);
166 adev = iface->l3_dev.dev;
169 alias_notify_device(iface->name, adev);
177 interface_flush_state(struct interface *iface)
179 if (iface->l3_dev.dev)
180 device_release(&iface->l3_dev);
181 interface_data_flush(iface);
185 mark_interface_down(struct interface *iface)
187 enum interface_state state = iface->state;
189 iface->state = IFS_DOWN;
191 interface_event(iface, IFEV_DOWN);
192 interface_ip_set_enabled(&iface->config_ip, false);
193 interface_ip_flush(&iface->proto_ip);
194 interface_flush_state(iface);
195 system_flush_routes();
199 __interface_set_down(struct interface *iface, bool force)
201 if (iface->state == IFS_DOWN ||
202 iface->state == IFS_TEARDOWN)
205 if (iface->state == IFS_UP)
206 interface_event(iface, IFEV_DOWN);
207 iface->state = IFS_TEARDOWN;
208 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
210 interface_flush_state(iface);
214 interface_cb(struct device_user *dep, enum device_event ev)
216 struct interface *iface;
219 iface = container_of(dep, struct interface, main_dev);
224 case DEV_EVENT_REMOVE:
231 interface_set_available(iface, new_state);
232 if (!new_state && dep->dev->external)
233 interface_set_main_dev(iface, NULL);
237 interface_set_available(struct interface *iface, bool new_state)
239 if (iface->available == new_state)
242 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
243 iface->available = new_state;
246 if (iface->autostart && !config_init)
247 interface_set_up(iface);
249 __interface_set_down(iface, true);
253 interface_add_user(struct interface_user *dep, struct interface *iface)
256 list_add(&dep->list, &iface_all_users);
261 list_add(&dep->list, &iface->users);
262 if (iface->state == IFS_UP)
263 dep->cb(dep, iface, IFEV_UP);
267 interface_remove_user(struct interface_user *dep)
269 list_del_init(&dep->list);
274 interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
276 struct interface *alias = container_of(dep, struct interface, parent_iface);
277 struct device *dev = iface->l3_dev.dev;
284 interface_set_main_dev(alias, dev);
285 interface_set_available(alias, true);
288 interface_set_available(alias, false);
289 interface_set_main_dev(alias, NULL);
292 interface_remove_user(dep);
300 interface_claim_device(struct interface *iface)
302 struct interface *parent;
303 struct device *dev = NULL;
305 if (iface->parent_iface.iface)
306 interface_remove_user(&iface->parent_iface);
308 if (iface->parent_ifname) {
309 parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
310 iface->parent_iface.cb = interface_alias_cb;
311 interface_add_user(&iface->parent_iface, parent);
312 } else if (iface->ifname &&
313 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
314 dev = device_get(iface->ifname, true);
318 interface_set_main_dev(iface, dev);
320 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
321 interface_set_available(iface, true);
326 interface_cleanup(struct interface *iface, bool reload)
328 struct interface_user *dep, *tmp;
330 if (iface->parent_iface.iface)
331 interface_remove_user(&iface->parent_iface);
333 list_for_each_entry_safe(dep, tmp, &iface->users, list)
334 interface_remove_user(dep);
336 interface_ip_flush(&iface->config_ip);
337 interface_flush_state(iface);
338 interface_clear_errors(iface);
340 if (iface->main_dev.dev && !reload)
341 interface_set_main_dev(iface, NULL);
342 interface_set_proto_state(iface, NULL);
346 interface_do_free(struct interface *iface)
348 interface_event(iface, IFEV_FREE);
349 interface_cleanup(iface, false);
351 netifd_ubus_remove_interface(iface);
352 avl_delete(&interfaces.avl, &iface->node.avl);
357 interface_do_reload(struct interface *iface)
359 interface_event(iface, IFEV_RELOAD);
360 interface_cleanup(iface, true);
361 proto_init_interface(iface, iface->config);
362 interface_claim_device(iface);
366 interface_handle_config_change(struct interface *iface)
368 enum interface_config_state state = iface->config_state;
370 iface->config_state = IFC_NORMAL;
375 interface_do_reload(iface);
378 interface_do_free(iface);
381 if (iface->autostart && iface->available)
382 interface_set_up(iface);
386 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
388 struct interface *iface = state->iface;
392 if (iface->state != IFS_SETUP)
395 interface_ip_set_enabled(&iface->config_ip, true);
396 system_flush_routes();
397 iface->state = IFS_UP;
398 iface->start_time = system_get_rtime();
399 interface_event(iface, IFEV_UP);
400 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
403 if (iface->state == IFS_DOWN)
406 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
407 mark_interface_down(iface);
408 if (iface->main_dev.dev)
409 device_release(&iface->main_dev);
410 interface_handle_config_change(iface);
412 case IFPEV_LINK_LOST:
413 if (iface->state != IFS_UP)
416 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
417 mark_interface_down(iface);
418 iface->state = IFS_SETUP;
422 interface_write_resolv_conf();
425 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
428 iface->proto->free(iface->proto);
431 iface->state = IFS_DOWN;
432 iface->proto = state;
436 state->proto_event = interface_proto_cb;
437 state->iface = iface;
441 interface_init(struct interface *iface, const char *name,
442 struct blob_attr *config)
444 struct blob_attr *tb[IFACE_ATTR_MAX];
445 struct blob_attr *cur;
446 const char *proto_name = NULL;
448 strncpy(iface->name, name, sizeof(iface->name) - 1);
449 INIT_LIST_HEAD(&iface->errors);
450 INIT_LIST_HEAD(&iface->users);
451 INIT_LIST_HEAD(&iface->hotplug_list);
452 interface_ip_init(iface);
453 avl_init(&iface->data, avl_strcmp, false, NULL);
454 iface->config_ip.enabled = false;
456 iface->main_dev.cb = interface_cb;
458 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
459 blob_data(config), blob_len(config));
461 if ((cur = tb[IFACE_ATTR_PROTO]))
462 proto_name = blobmsg_data(cur);
464 proto_attach_interface(iface, proto_name);
466 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
467 iface->proto_ip.no_defaultroute =
468 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
469 iface->proto_ip.no_dns =
470 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
472 if ((cur = tb[IFACE_ATTR_DNS]))
473 interface_add_dns_server_list(&iface->config_ip, cur);
475 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
476 interface_add_dns_search_list(&iface->config_ip, cur);
478 if ((cur = tb[IFACE_ATTR_METRIC]))
479 iface->metric = blobmsg_get_u32(cur);
481 iface->config_autostart = iface->autostart;
484 static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
486 struct blob_attr *tb[IFACE_ATTR_MAX];
487 struct blob_attr *cur;
489 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
490 blob_data(config), blob_len(config));
493 if ((cur = tb[IFACE_ATTR_INTERFACE]))
494 iface->parent_ifname = blobmsg_data(cur);
496 if (!iface->parent_ifname)
499 if ((cur = tb[IFACE_ATTR_IFNAME]))
500 iface->ifname = blobmsg_data(cur);
504 iface->config = config;
505 vlist_add(&interfaces, &iface->node, iface->name);
510 interface_add(struct interface *iface, struct blob_attr *config)
512 __interface_add(iface, config, false);
516 interface_add_alias(struct interface *iface, struct blob_attr *config)
518 if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
521 return __interface_add(iface, config, true);
525 interface_set_l3_dev(struct interface *iface, struct device *dev)
527 bool enabled = iface->config_ip.enabled;
528 bool claimed = iface->l3_dev.claimed;
530 if (iface->l3_dev.dev == dev)
533 interface_ip_set_enabled(&iface->config_ip, false);
534 interface_ip_flush(&iface->proto_ip);
535 device_add_user(&iface->l3_dev, dev);
539 device_claim(&iface->l3_dev);
540 interface_ip_set_enabled(&iface->config_ip, enabled);
545 interface_set_main_dev(struct interface *iface, struct device *dev)
547 bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
548 bool claimed = iface->l3_dev.claimed;
550 if (iface->main_dev.dev == dev)
554 interface_set_l3_dev(iface, dev);
556 device_add_user(&iface->main_dev, dev);
558 device_claim(&iface->l3_dev);
560 if (!iface->l3_dev.dev)
561 interface_set_l3_dev(iface, dev);
565 interface_remove_link(struct interface *iface, struct device *dev)
567 struct device *mdev = iface->main_dev.dev;
569 if (mdev && mdev->hotplug_ops)
570 return mdev->hotplug_ops->del(mdev, dev);
572 if (!iface->main_dev.hotplug)
573 return UBUS_STATUS_INVALID_ARGUMENT;
575 if (dev != iface->main_dev.dev)
576 return UBUS_STATUS_INVALID_ARGUMENT;
578 device_remove_user(&iface->main_dev);
583 interface_add_link(struct interface *iface, struct device *dev)
585 struct device *mdev = iface->main_dev.dev;
590 if (iface->main_dev.hotplug)
591 device_remove_user(&iface->main_dev);
594 if (mdev->hotplug_ops)
595 return mdev->hotplug_ops->add(mdev, dev);
597 return UBUS_STATUS_NOT_SUPPORTED;
600 interface_set_main_dev(iface, dev);
601 iface->main_dev.hotplug = true;
606 interface_set_up(struct interface *iface)
610 iface->autostart = true;
612 if (iface->state != IFS_DOWN)
615 interface_clear_errors(iface);
616 if (!iface->available) {
617 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
621 if (iface->main_dev.dev) {
622 ret = device_claim(&iface->main_dev);
627 iface->state = IFS_SETUP;
628 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
630 mark_interface_down(iface);
638 interface_set_down(struct interface *iface)
641 vlist_for_each_element(&interfaces, iface, node)
642 __interface_set_down(iface, false);
644 iface->autostart = false;
645 __interface_set_down(iface, false);
652 interface_start_pending(void)
654 struct interface *iface;
656 vlist_for_each_element(&interfaces, iface, node) {
657 if (iface->available && iface->autostart)
658 interface_set_up(iface);
663 set_config_state(struct interface *iface, enum interface_config_state s)
665 iface->config_state = s;
666 if (iface->state == IFS_DOWN)
667 interface_handle_config_change(iface);
669 __interface_set_down(iface, false);
673 interface_update_start(struct interface *iface)
675 interface_ip_update_start(&iface->proto_ip);
679 interface_update_complete(struct interface *iface)
681 interface_ip_update_complete(&iface->proto_ip);
685 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
687 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
688 vlist_simple_replace(&new->dns_search, &old->dns_search);
692 interface_change_config(struct interface *if_old, struct interface *if_new)
694 struct blob_attr *old_config = if_old->config;
695 const char *old_ifname = if_old->ifname;
696 const char *old_parent_ifname = if_old->parent_ifname;
697 const struct proto_handler *proto = if_old->proto_handler;
699 interface_clear_errors(if_old);
700 if_old->config = if_new->config;
701 if (!if_old->config_autostart && if_new->config_autostart)
702 if_old->autostart = true;
704 if_old->device_config = if_new->device_config;
705 if_old->config_autostart = if_new->config_autostart;
706 if_old->ifname = if_new->ifname;
707 if_old->parent_ifname = if_new->parent_ifname;
708 if_old->proto_handler = if_new->proto_handler;
710 #define FIELD_CHANGED_STR(field) \
711 ((!!if_old->field != !!old_ ## field) || \
713 strcmp(old_ ## field, if_old->field) != 0))
715 if (FIELD_CHANGED_STR(parent_ifname)) {
716 if (if_old->parent_iface.iface)
717 interface_remove_user(&if_old->parent_iface);
721 if (FIELD_CHANGED_STR(ifname) || proto != if_new->proto_handler) {
722 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
727 if (!proto->config_params)
728 D(INTERFACE, "No config parameters for interface '%s'\n",
730 else if (!config_check_equal(old_config, if_new->config,
731 proto->config_params)) {
732 D(INTERFACE, "Reload interface '%s because of config changes\n",
737 #define UPDATE(field) ({ \
738 bool __changed = (if_old->field != if_new->field); \
739 if_old->field = if_new->field; \
743 if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
744 interface_ip_set_enabled(&if_old->config_ip, false);
745 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
746 interface_ip_set_enabled(&if_old->proto_ip, false);
747 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
750 UPDATE(proto_ip.no_dns);
751 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
752 interface_write_resolv_conf();
759 set_config_state(if_old, IFC_RELOAD);
761 if_new->config = NULL;
762 interface_cleanup(if_new, false);
768 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
769 struct vlist_node *node_old)
771 struct interface *if_old = container_of(node_old, struct interface, node);
772 struct interface *if_new = container_of(node_new, struct interface, node);
774 if (node_old && node_new) {
775 D(INTERFACE, "Update interface '%s'\n", if_new->name);
776 interface_change_config(if_old, if_new);
777 } else if (node_old) {
778 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
779 set_config_state(if_old, IFC_REMOVE);
780 } else if (node_new) {
781 D(INTERFACE, "Create interface '%s'\n", if_new->name);
782 proto_init_interface(if_new, if_new->config);
783 interface_claim_device(if_new);
784 netifd_ubus_add_interface(if_new);
790 interface_init_list(void)
792 vlist_init(&interfaces, avl_strcmp, interface_update);
793 interfaces.keep_old = true;
794 interfaces.no_delete = true;