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,
42 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
43 [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
44 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
45 [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
46 [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
47 [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
48 [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
49 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
50 [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
53 static const union config_param_info iface_attr_info[IFACE_ATTR_MAX] = {
54 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
57 const struct config_param_list interface_attr_list = {
58 .n_params = IFACE_ATTR_MAX,
59 .params = iface_attrs,
60 .info = iface_attr_info,
64 interface_clear_errors(struct interface *iface)
66 struct interface_error *error, *tmp;
68 list_for_each_entry_safe(error, tmp, &iface->errors, list) {
69 list_del(&error->list);
74 void interface_add_error(struct interface *iface, const char *subsystem,
75 const char *code, const char **data, int n_data)
77 struct interface_error *error;
83 len = n_data * sizeof(char *);
84 datalen = alloca(len);
85 for (i = 0; i < n_data; i++) {
86 datalen[i] = strlen(data[i]) + 1;
91 error = calloc(1, sizeof(*error) + sizeof(char *) + len);
95 list_add_tail(&error->list, &iface->errors);
96 error->subsystem = subsystem;
99 dest = (char *) &error->data[n_data + 1];
100 for (i = 0; i < n_data; i++) {
101 error->data[i] = dest;
102 memcpy(dest, data[i], datalen[i]);
105 error->data[n_data] = NULL;
109 interface_data_del(struct interface *iface, struct interface_data *data)
111 avl_delete(&iface->data, &data->node);
116 interface_data_flush(struct interface *iface)
118 struct interface_data *d, *tmp;
120 avl_for_each_element_safe(&iface->data, d, node, tmp)
121 interface_data_del(iface, d);
125 interface_add_data(struct interface *iface, const struct blob_attr *data)
127 struct interface_data *n, *o;
129 if (!blobmsg_check_attr(data, true))
130 return UBUS_STATUS_INVALID_ARGUMENT;
132 n = calloc(1, sizeof(*n) + blob_pad_len(data));
133 memcpy(n->data, data, blob_pad_len(data));
134 n->node.key = blobmsg_name(data);
136 o = avl_find_element(&iface->data, n->node.key, o, node);
138 interface_data_del(iface, o);
140 avl_insert(&iface->data, &n->node);
145 interface_event(struct interface *iface, enum interface_event ev)
147 struct interface_user *dep, *tmp;
149 list_for_each_entry_safe(dep, tmp, &iface->users, list)
150 dep->cb(dep, iface, ev);
152 list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
153 dep->cb(dep, iface, ev);
157 interface_flush_state(struct interface *iface)
159 if (iface->main_dev.dev)
160 device_release(&iface->main_dev);
161 if (iface->l3_dev.dev)
162 device_release(&iface->l3_dev);
163 interface_data_flush(iface);
167 mark_interface_down(struct interface *iface)
169 if (iface->state == IFS_UP)
170 interface_event(iface, IFEV_DOWN);
171 interface_ip_set_enabled(&iface->config_ip, false);
172 interface_ip_flush(&iface->proto_ip);
173 interface_flush_state(iface);
174 system_flush_routes();
175 iface->state = IFS_DOWN;
179 __interface_set_down(struct interface *iface, bool force)
181 interface_clear_errors(iface);
183 if (iface->state == IFS_DOWN ||
184 iface->state == IFS_TEARDOWN)
187 if (iface->state == IFS_UP)
188 interface_event(iface, IFEV_DOWN);
189 iface->state = IFS_TEARDOWN;
190 interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
192 interface_flush_state(iface);
196 interface_cb(struct device_user *dep, enum device_event ev)
198 struct interface *iface;
201 iface = container_of(dep, struct interface, main_dev);
206 case DEV_EVENT_REMOVE:
213 interface_set_available(iface, new_state);
217 interface_set_available(struct interface *iface, bool new_state)
219 if (iface->available == new_state)
222 D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
223 iface->available = new_state;
226 if (iface->autostart && !config_init)
227 interface_set_up(iface);
229 __interface_set_down(iface, true);
233 interface_add_user(struct interface_user *dep, struct interface *iface)
236 list_add(&dep->list, &iface_all_users);
241 list_add(&dep->list, &iface->users);
242 if (iface->state == IFS_UP)
243 dep->cb(dep, iface, IFEV_UP);
247 interface_remove_user(struct interface_user *dep)
249 list_del_init(&dep->list);
254 interface_claim_device(struct interface *iface)
259 !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
260 dev = device_get(iface->ifname, true);
262 interface_set_main_dev(iface, dev);
264 if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
265 interface_set_available(iface, true);
270 interface_cleanup(struct interface *iface, bool reload)
272 struct interface_user *dep, *tmp;
274 list_for_each_entry_safe(dep, tmp, &iface->users, list)
275 interface_remove_user(dep);
277 interface_ip_flush(&iface->config_ip);
278 interface_flush_state(iface);
279 interface_clear_errors(iface);
280 if (iface->main_dev.dev &&
281 (!reload || !iface->main_dev.hotplug))
282 interface_set_main_dev(iface, NULL);
283 interface_set_proto_state(iface, NULL);
287 interface_do_free(struct interface *iface)
289 interface_event(iface, IFEV_FREE);
290 interface_cleanup(iface, false);
292 netifd_ubus_remove_interface(iface);
293 avl_delete(&interfaces.avl, &iface->node.avl);
298 interface_do_reload(struct interface *iface)
300 interface_event(iface, IFEV_RELOAD);
301 interface_cleanup(iface, true);
302 proto_init_interface(iface, iface->config);
303 interface_claim_device(iface);
307 interface_handle_config_change(struct interface *iface)
309 switch(iface->config_state) {
313 interface_do_reload(iface);
316 interface_do_free(iface);
319 if (iface->autostart && iface->available)
320 interface_set_up(iface);
324 interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
326 struct interface *iface = state->iface;
330 if (iface->state != IFS_SETUP)
333 interface_ip_set_enabled(&iface->config_ip, true);
334 system_flush_routes();
335 iface->state = IFS_UP;
336 iface->start_time = system_get_rtime();
337 interface_event(iface, IFEV_UP);
338 interface_write_resolv_conf();
339 netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
342 if (iface->state == IFS_DOWN)
345 netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
346 mark_interface_down(iface);
347 interface_handle_config_change(iface);
349 case IFPEV_LINK_LOST:
350 if (iface->state != IFS_UP)
353 netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
354 mark_interface_down(iface);
355 iface->state = IFS_SETUP;
360 void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
363 iface->proto->free(iface->proto);
366 iface->state = IFS_DOWN;
367 iface->proto = state;
371 state->proto_event = interface_proto_cb;
372 state->iface = iface;
376 interface_init(struct interface *iface, const char *name,
377 struct blob_attr *config)
379 struct blob_attr *tb[IFACE_ATTR_MAX];
380 struct blob_attr *cur;
381 const char *proto_name = NULL;
383 strncpy(iface->name, name, sizeof(iface->name) - 1);
384 INIT_LIST_HEAD(&iface->errors);
385 INIT_LIST_HEAD(&iface->users);
386 INIT_LIST_HEAD(&iface->hotplug_list);
387 interface_ip_init(iface);
388 avl_init(&iface->data, avl_strcmp, false, NULL);
389 iface->config_ip.enabled = false;
391 iface->main_dev.cb = interface_cb;
393 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
394 blob_data(config), blob_len(config));
396 if ((cur = tb[IFACE_ATTR_PROTO]))
397 proto_name = blobmsg_data(cur);
399 proto_attach_interface(iface, proto_name);
401 iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
402 iface->proto_ip.no_defaultroute =
403 !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
404 iface->proto_ip.no_dns =
405 !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
407 if ((cur = tb[IFACE_ATTR_DNS]))
408 interface_add_dns_server_list(&iface->config_ip, cur);
410 if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
411 interface_add_dns_search_list(&iface->config_ip, cur);
413 if ((cur = tb[IFACE_ATTR_METRIC]))
414 iface->metric = blobmsg_get_u32(cur);
416 iface->config_autostart = iface->autostart;
420 interface_add(struct interface *iface, struct blob_attr *config)
422 struct blob_attr *tb[IFACE_ATTR_MAX];
423 struct blob_attr *cur;
425 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
426 blob_data(config), blob_len(config));
428 if ((cur = tb[IFACE_ATTR_IFNAME]))
429 iface->ifname = blobmsg_data(cur);
431 iface->config = config;
432 vlist_add(&interfaces, &iface->node, iface->name);
436 interface_set_l3_dev(struct interface *iface, struct device *dev)
438 bool enabled = iface->config_ip.enabled;
439 bool claimed = iface->l3_dev.claimed;
441 if (iface->l3_dev.dev == dev)
444 interface_ip_set_enabled(&iface->config_ip, false);
445 interface_ip_flush(&iface->proto_ip);
446 device_add_user(&iface->l3_dev, dev);
450 device_claim(&iface->l3_dev);
451 interface_ip_set_enabled(&iface->config_ip, enabled);
456 interface_set_main_dev(struct interface *iface, struct device *dev)
458 bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
459 bool claimed = iface->l3_dev.claimed;
461 if (iface->main_dev.dev == dev)
465 interface_set_l3_dev(iface, dev);
467 device_add_user(&iface->main_dev, dev);
469 device_claim(&iface->l3_dev);
471 if (!iface->l3_dev.dev)
472 interface_set_l3_dev(iface, dev);
476 interface_remove_link(struct interface *iface, struct device *dev)
478 struct device *mdev = iface->main_dev.dev;
480 if (mdev && mdev->hotplug_ops)
481 return mdev->hotplug_ops->del(mdev, dev);
483 if (!iface->main_dev.hotplug)
484 return UBUS_STATUS_INVALID_ARGUMENT;
486 if (dev != iface->main_dev.dev)
487 return UBUS_STATUS_INVALID_ARGUMENT;
489 device_remove_user(&iface->main_dev);
494 interface_add_link(struct interface *iface, struct device *dev)
496 struct device *mdev = iface->main_dev.dev;
501 if (iface->main_dev.hotplug)
502 device_remove_user(&iface->main_dev);
505 if (mdev->hotplug_ops)
506 return mdev->hotplug_ops->add(mdev, dev);
508 return UBUS_STATUS_NOT_SUPPORTED;
511 interface_set_main_dev(iface, dev);
512 iface->main_dev.hotplug = true;
517 interface_set_up(struct interface *iface)
521 iface->autostart = true;
523 if (iface->state != IFS_DOWN)
526 interface_clear_errors(iface);
527 if (!iface->available) {
528 interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
532 if (iface->main_dev.dev) {
533 ret = device_claim(&iface->main_dev);
538 iface->state = IFS_SETUP;
539 ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
541 mark_interface_down(iface);
549 interface_set_down(struct interface *iface)
552 vlist_for_each_element(&interfaces, iface, node)
553 __interface_set_down(iface, false);
555 iface->autostart = false;
556 __interface_set_down(iface, false);
563 interface_start_pending(void)
565 struct interface *iface;
567 vlist_for_each_element(&interfaces, iface, node) {
568 if (iface->available && iface->autostart)
569 interface_set_up(iface);
574 set_config_state(struct interface *iface, enum interface_config_state s)
576 iface->config_state = s;
577 if (iface->state == IFS_DOWN)
578 interface_handle_config_change(iface);
580 __interface_set_down(iface, false);
584 interface_update_start(struct interface *iface)
586 interface_ip_update_start(&iface->proto_ip);
590 interface_update_complete(struct interface *iface)
592 interface_ip_update_complete(&iface->proto_ip);
596 interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
598 vlist_simple_replace(&new->dns_servers, &old->dns_servers);
599 vlist_simple_replace(&new->dns_search, &old->dns_search);
603 interface_change_config(struct interface *if_old, struct interface *if_new)
605 struct blob_attr *old_config = if_old->config;
606 const char *old_ifname = if_old->ifname;
607 const struct proto_handler *proto = if_old->proto_handler;
609 interface_clear_errors(if_old);
610 if_old->config = if_new->config;
611 if (!if_old->config_autostart && if_new->config_autostart)
612 if_old->autostart = true;
614 if_old->config_autostart = if_new->config_autostart;
615 if_old->ifname = if_new->ifname;
616 if_old->proto_handler = if_new->proto_handler;
618 if ((!!old_ifname != !!if_new->ifname) ||
619 (old_ifname && strcmp(old_ifname, if_new->ifname) != 0) ||
620 proto != if_new->proto_handler) {
621 D(INTERFACE, "Reload interface '%s' because of ifname/proto change\n",
626 if (!proto->config_params)
627 D(INTERFACE, "No config parameters for interface '%s'\n",
629 else if (!config_check_equal(old_config, if_new->config,
630 proto->config_params)) {
631 D(INTERFACE, "Reload interface '%s because of config changes\n",
636 #define UPDATE(field) ({ \
637 bool __changed = (if_old->field != if_new->field); \
638 if_old->field = if_new->field; \
642 if (UPDATE(metric) || UPDATE(proto_ip.no_defaultroute)) {
643 interface_ip_set_enabled(&if_old->config_ip, false);
644 interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
645 interface_ip_set_enabled(&if_old->proto_ip, false);
646 interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
649 UPDATE(proto_ip.no_dns);
650 interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
651 interface_write_resolv_conf();
658 set_config_state(if_old, IFC_RELOAD);
665 interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
666 struct vlist_node *node_old)
668 struct interface *if_old = container_of(node_old, struct interface, node);
669 struct interface *if_new = container_of(node_new, struct interface, node);
671 if (node_old && node_new) {
672 D(INTERFACE, "Update interface '%s'\n", if_new->name);
673 interface_change_config(if_old, if_new);
674 } else if (node_old) {
675 D(INTERFACE, "Remove interface '%s'\n", if_old->name);
676 set_config_state(if_old, IFC_REMOVE);
677 } else if (node_new) {
678 D(INTERFACE, "Create interface '%s'\n", if_new->name);
679 proto_init_interface(if_new, if_new->config);
680 interface_claim_device(if_new);
681 netifd_ubus_add_interface(if_new);
687 interface_init_list(void)
689 vlist_init(&interfaces, avl_strcmp, interface_update);
690 interfaces.keep_old = true;
691 interfaces.no_delete = true;