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.
21 #include <arpa/inet.h>
22 #include <netinet/in.h>
26 #include "interface.h"
27 #include "interface-ip.h"
32 static int proto_fd = -1;
41 struct proto_shell_handler {
42 struct list_head list;
43 struct proto_handler proto;
48 struct uci_blob_param_list config;
51 struct proto_shell_dependency {
52 struct list_head list;
55 struct proto_shell_state *proto;
56 struct interface_user dep;
62 struct proto_shell_state {
63 struct interface_proto_state proto;
64 struct proto_shell_handler *handler;
65 struct blob_attr *config;
67 struct uloop_timeout teardown_timeout;
69 struct netifd_process script_task;
70 struct netifd_process proto_task;
72 enum proto_shell_sm sm;
73 bool proto_task_killed;
78 struct list_head deps;
82 proto_shell_check_dependencies(struct proto_shell_state *state)
84 struct proto_shell_dependency *dep;
85 bool available = true;
87 list_for_each_entry(dep, &state->deps, list) {
95 interface_set_available(state->proto.iface, available);
99 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
100 enum interface_event ev);
102 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
103 enum interface_event ev);
106 proto_shell_update_host_dep(struct proto_shell_dependency *dep)
108 struct interface *iface = NULL;
113 if (dep->interface[0])
114 iface = vlist_find(&interfaces, dep->interface, iface, node);
116 iface = interface_ip_add_target_route(&dep->host, dep->v6, iface);
120 interface_remove_user(&dep->dep);
121 dep->dep.cb = proto_shell_if_down_cb;
122 interface_add_user(&dep->dep, iface);
125 proto_shell_check_dependencies(dep->proto);
129 proto_shell_clear_host_dep(struct proto_shell_state *state)
131 struct proto_shell_dependency *dep, *tmp;
133 list_for_each_entry_safe(dep, tmp, &state->deps, list) {
134 interface_remove_user(&dep->dep);
135 list_del(&dep->list);
141 proto_shell_handler(struct interface_proto_state *proto,
142 enum interface_proto_cmd cmd, bool force)
144 struct proto_shell_state *state;
145 struct proto_shell_handler *handler;
146 struct netifd_process *proc;
147 static char error_buf[32];
152 int ret, i = 0, j = 0;
154 state = container_of(proto, struct proto_shell_state, proto);
155 handler = state->handler;
156 proc = &state->script_task;
158 if (cmd == PROTO_CMD_SETUP) {
162 state->last_error = -1;
163 proto_shell_clear_host_dep(state);
175 } else if (cmd == PROTO_CMD_RENEW) {
176 if (!(handler->proto.flags & PROTO_FLAG_RENEW_AVAILABLE))
179 if (state->script_task.uloop.pending) {
180 state->renew_pending = true;
184 state->renew_pending = false;
189 if (state->script_task.uloop.pending) {
190 uloop_timeout_set(&state->teardown_timeout, 1000);
191 kill(state->script_task.uloop.pid, SIGTERM);
192 if (state->proto_task.uloop.pending)
193 kill(state->proto_task.uloop.pid, SIGTERM);
194 state->renew_pending = false;
195 state->sm = S_SETUP_ABORT;
198 /* fall through if no script task is running */
201 state->renew_pending = false;
202 state->sm = S_TEARDOWN;
203 if (state->last_error >= 0) {
204 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
205 envp[j++] = error_buf;
207 uloop_timeout_set(&state->teardown_timeout, 5000);
218 D(INTERFACE, "run %s for interface '%s'\n", action, proto->iface->name);
219 config = blobmsg_format_json(state->config, true);
223 argv[i++] = handler->script_name;
224 argv[i++] = handler->proto.name;
226 argv[i++] = proto->iface->name;
228 if (proto->iface->main_dev.dev)
229 argv[i++] = proto->iface->main_dev.dev->ifname;
233 ret = netifd_start_process(argv, envp, proc);
240 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
241 enum interface_event ev)
243 struct proto_shell_dependency *pdep;
245 if (ev != IFEV_UP && ev != IFEV_UPDATE)
248 pdep = container_of(dep, struct proto_shell_dependency, dep);
249 proto_shell_update_host_dep(pdep);
253 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
254 enum interface_event ev)
256 struct proto_shell_dependency *pdep;
257 struct proto_shell_state *state;
259 if (ev == IFEV_UP || ev == IFEV_UPDATE)
262 pdep = container_of(dep, struct proto_shell_dependency, dep);
263 interface_remove_user(dep);
264 dep->cb = proto_shell_if_up_cb;
265 interface_add_user(dep, NULL);
268 if (state->sm == S_IDLE) {
269 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
270 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
275 proto_shell_task_finish(struct proto_shell_state *state,
276 struct netifd_process *task)
280 if (task == &state->proto_task)
281 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
284 if (task == &state->proto_task)
285 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN,
287 if (task == &state->script_task) {
288 if (state->renew_pending)
289 proto_shell_handler(&state->proto,
290 PROTO_CMD_RENEW, false);
291 else if (!state->proto_task.uloop.pending &&
292 state->sm == S_SETUP)
293 proto_shell_handler(&state->proto,
300 if (state->script_task.uloop.pending ||
301 state->proto_task.uloop.pending)
304 uloop_timeout_cancel(&state->teardown_timeout);
306 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
310 if (state->script_task.uloop.pending)
313 if (state->proto_task.uloop.pending) {
314 if (!state->proto_task_killed)
315 kill(state->proto_task.uloop.pid, SIGTERM);
319 uloop_timeout_cancel(&state->teardown_timeout);
321 state->proto.proto_event(&state->proto, IFPEV_DOWN);
327 proto_shell_teardown_timeout_cb(struct uloop_timeout *timeout)
329 struct proto_shell_state *state;
331 state = container_of(timeout, struct proto_shell_state, teardown_timeout);
333 netifd_kill_process(&state->script_task);
334 netifd_kill_process(&state->proto_task);
335 proto_shell_task_finish(state, NULL);
339 proto_shell_script_cb(struct netifd_process *p, int ret)
341 struct proto_shell_state *state;
343 state = container_of(p, struct proto_shell_state, script_task);
344 proto_shell_task_finish(state, p);
348 proto_shell_task_cb(struct netifd_process *p, int ret)
350 struct proto_shell_state *state;
352 state = container_of(p, struct proto_shell_state, proto_task);
354 if (state->sm == S_IDLE || state->sm == S_SETUP)
355 state->last_error = WEXITSTATUS(ret);
357 proto_shell_task_finish(state, p);
361 proto_shell_free(struct interface_proto_state *proto)
363 struct proto_shell_state *state;
365 state = container_of(proto, struct proto_shell_state, proto);
366 uloop_timeout_cancel(&state->teardown_timeout);
367 proto_shell_clear_host_dep(state);
368 netifd_kill_process(&state->script_task);
369 netifd_kill_process(&state->proto_task);
375 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
378 struct blob_attr *cur;
381 blobmsg_for_each_attr(cur, attr, rem) {
382 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
383 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
387 interface_ip_add_route(iface, cur, v6);
392 proto_shell_parse_data(struct interface *iface, struct blob_attr *attr)
394 struct blob_attr *cur;
397 blobmsg_for_each_attr(cur, attr, rem)
398 interface_add_data(iface, cur);
401 static struct device *
402 proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
407 memset(&b, 0, sizeof(b));
408 blob_buf_init(&b, 0);
409 blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
410 dev = device_create(name, &tunnel_device_type, blob_data(b.head));
437 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
438 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
439 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
440 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
441 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
442 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
443 [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
444 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
445 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
446 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
447 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
448 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
449 [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
450 [NOTIFY_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
451 [NOTIFY_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
452 [NOTIFY_HOST] = { .name = "host", .type = BLOBMSG_TYPE_STRING },
453 [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
454 [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
458 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
460 struct interface *iface = state->proto.iface;
461 struct blob_attr *cur;
465 bool addr_ext = false;
469 if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
470 return UBUS_STATUS_PERMISSION_DENIED;
472 if (!tb[NOTIFY_LINK_UP])
473 return UBUS_STATUS_INVALID_ARGUMENT;
475 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
477 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
481 if ((cur = tb[NOTIFY_KEEP]) != NULL)
482 keep = blobmsg_get_bool(cur);
484 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
485 addr_ext = blobmsg_get_bool(cur);
490 if (iface->state != IFS_UP || !iface->l3_dev.dev)
494 dev = iface->main_dev.dev;
495 if (tb[NOTIFY_IFNAME]) {
497 devname = blobmsg_data(tb[NOTIFY_IFNAME]);
498 if (tb[NOTIFY_TUNNEL])
499 dev = proto_shell_create_tunnel(devname, tb[NOTIFY_TUNNEL]);
501 dev = device_get(devname, dev_create);
505 return UBUS_STATUS_INVALID_ARGUMENT;
507 interface_set_l3_dev(iface, dev);
508 if (device_claim(&iface->l3_dev) < 0)
509 return UBUS_STATUS_UNKNOWN_ERROR;
511 device_set_present(dev, true);
513 interface_update_start(iface);
516 proto_apply_ip_settings(iface, data, addr_ext);
518 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
519 proto_shell_parse_route_list(state->proto.iface, cur, false);
521 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
522 proto_shell_parse_route_list(state->proto.iface, cur, true);
524 if ((cur = tb[NOTIFY_DNS]))
525 interface_add_dns_server_list(&iface->proto_ip, cur);
527 if ((cur = tb[NOTIFY_DNS_SEARCH]))
528 interface_add_dns_search_list(&iface->proto_ip, cur);
530 if ((cur = tb[NOTIFY_DATA]))
531 proto_shell_parse_data(state->proto.iface, cur);
533 interface_update_complete(state->proto.iface);
535 if ((state->sm != S_SETUP_ABORT) && (state->sm != S_TEARDOWN)) {
537 state->proto.proto_event(&state->proto, IFPEV_UP);
545 fill_string_list(struct blob_attr *attr, char **argv, int max)
547 struct blob_attr *cur;
554 blobmsg_for_each_attr(cur, attr, rem) {
555 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
558 if (!blobmsg_check_attr(cur, NULL))
561 argv[argc++] = blobmsg_data(cur);
572 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
574 static char *argv[64];
575 static char *env[32];
577 if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
578 return UBUS_STATUS_PERMISSION_DENIED;
580 if (!tb[NOTIFY_COMMAND])
583 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
586 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
589 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
594 return UBUS_STATUS_INVALID_ARGUMENT;
598 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
600 unsigned int signal = ~0;
602 if (tb[NOTIFY_SIGNAL])
603 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
608 if (state->proto_task.uloop.pending) {
609 if (signal == SIGTERM || signal == SIGKILL)
610 state->proto_task_killed = true;
611 kill(state->proto_task.uloop.pid, signal);
618 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
620 struct blob_attr *cur;
625 if (!tb[NOTIFY_ERROR])
626 return UBUS_STATUS_INVALID_ARGUMENT;
628 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
629 if (n_data + 1 == ARRAY_SIZE(data))
632 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
635 if (!blobmsg_check_attr(cur, NULL))
638 data[n_data++] = blobmsg_data(cur);
644 interface_add_error(state->proto.iface, state->handler->proto.name,
645 data[0], (const char **) &data[1], n_data - 1);
650 return UBUS_STATUS_INVALID_ARGUMENT;
654 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
656 state->proto.iface->autostart = false;
661 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
663 if (!tb[NOTIFY_AVAILABLE])
664 return UBUS_STATUS_INVALID_ARGUMENT;
666 interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
671 proto_shell_add_host_dependency(struct proto_shell_state *state, struct blob_attr **tb)
673 struct proto_shell_dependency *dep;
674 struct blob_attr *host = tb[NOTIFY_HOST];
675 struct blob_attr *ifname_a = tb[NOTIFY_IFNAME];
676 const char *ifname_str = ifname_a ? blobmsg_data(ifname_a) : "";
679 if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
680 return UBUS_STATUS_PERMISSION_DENIED;
683 return UBUS_STATUS_INVALID_ARGUMENT;
685 dep = calloc_a(sizeof(*dep), &ifname, strlen(ifname_str) + 1);
686 if (inet_pton(AF_INET, blobmsg_data(host), &dep->host) < 1) {
687 if (inet_pton(AF_INET6, blobmsg_data(host), &dep->host) < 1) {
689 return UBUS_STATUS_INVALID_ARGUMENT;
696 dep->interface = strcpy(ifname, ifname_str);
698 dep->dep.cb = proto_shell_if_up_cb;
699 interface_add_user(&dep->dep, NULL);
700 list_add(&dep->list, &state->deps);
701 proto_shell_update_host_dep(dep);
703 return UBUS_STATUS_NOT_FOUND;
709 proto_shell_setup_failed(struct proto_shell_state *state)
715 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
718 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
723 ret = UBUS_STATUS_PERMISSION_DENIED;
730 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
732 struct proto_shell_state *state;
733 struct blob_attr *tb[__NOTIFY_LAST];
735 state = container_of(proto, struct proto_shell_state, proto);
737 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
738 if (!tb[NOTIFY_ACTION])
739 return UBUS_STATUS_INVALID_ARGUMENT;
741 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
743 return proto_shell_update_link(state, attr, tb);
745 return proto_shell_run_command(state, tb);
747 return proto_shell_kill_command(state, tb);
749 return proto_shell_notify_error(state, tb);
751 return proto_shell_block_restart(state, tb);
753 return proto_shell_set_available(state, tb);
755 return proto_shell_add_host_dependency(state, tb);
757 return proto_shell_setup_failed(state);
759 return UBUS_STATUS_INVALID_ARGUMENT;
763 static struct interface_proto_state *
764 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
765 struct blob_attr *attr)
767 struct proto_shell_state *state;
769 state = calloc(1, sizeof(*state));
770 INIT_LIST_HEAD(&state->deps);
772 state->config = malloc(blob_pad_len(attr));
776 memcpy(state->config, attr, blob_pad_len(attr));
777 state->proto.free = proto_shell_free;
778 state->proto.notify = proto_shell_notify;
779 state->proto.cb = proto_shell_handler;
780 state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
781 state->script_task.cb = proto_shell_script_cb;
782 state->script_task.dir_fd = proto_fd;
783 state->script_task.log_prefix = iface->name;
784 state->proto_task.cb = proto_shell_task_cb;
785 state->proto_task.dir_fd = proto_fd;
786 state->proto_task.log_prefix = iface->name;
787 state->handler = container_of(h, struct proto_shell_handler, proto);
789 return &state->proto;
797 proto_shell_add_handler(const char *script, const char *name, json_object *obj)
799 struct proto_shell_handler *handler;
800 struct proto_handler *proto;
801 json_object *config, *tmp;
802 char *proto_name, *script_name;
804 handler = calloc_a(sizeof(*handler),
805 &proto_name, strlen(name) + 1,
806 &script_name, strlen(script) + 1);
810 handler->script_name = strcpy(script_name, script);
812 proto = &handler->proto;
813 proto->name = strcpy(proto_name, name);
814 proto->config_params = &handler->config;
815 proto->attach = proto_shell_attach;
817 tmp = json_get_field(obj, "no-device", json_type_boolean);
818 if (tmp && json_object_get_boolean(tmp))
819 handler->proto.flags |= PROTO_FLAG_NODEV;
821 tmp = json_get_field(obj, "available", json_type_boolean);
822 if (tmp && json_object_get_boolean(tmp))
823 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
825 tmp = json_get_field(obj, "renew-handler", json_type_boolean);
826 if (tmp && json_object_get_boolean(tmp))
827 handler->proto.flags |= PROTO_FLAG_RENEW_AVAILABLE;
829 tmp = json_get_field(obj, "lasterror", json_type_boolean);
830 if (tmp && json_object_get_boolean(tmp))
831 handler->proto.flags |= PROTO_FLAG_LASTERROR;
833 config = json_get_field(obj, "config", json_type_array);
835 handler->config_buf = netifd_handler_parse_config(&handler->config, config);
837 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
838 add_proto_handler(proto);
841 void proto_shell_init(void)
843 proto_fd = netifd_open_subdir("proto");
847 netifd_init_script_handlers(proto_fd, proto_shell_add_handler);