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.
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
27 #include <libubox/blobmsg_json.h>
30 #include "interface.h"
31 #include "interface-ip.h"
35 static int proto_fd = -1;
44 struct proto_shell_handler {
45 struct list_head list;
46 struct proto_handler proto;
47 struct config_param_list config;
53 struct proto_shell_dependency {
54 struct list_head list;
56 struct proto_shell_state *proto;
57 struct interface_user dep;
63 struct proto_shell_state {
64 struct interface_proto_state proto;
65 struct proto_shell_handler *handler;
66 struct blob_attr *config;
68 struct uloop_timeout teardown_timeout;
70 struct netifd_process script_task;
71 struct netifd_process proto_task;
73 enum proto_shell_sm sm;
74 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;
113 iface = interface_ip_add_target_route(&dep->host, dep->v6);
117 interface_remove_user(&dep->dep);
118 dep->dep.cb = proto_shell_if_down_cb;
119 interface_add_user(&dep->dep, iface);
122 proto_shell_check_dependencies(dep->proto);
126 proto_shell_clear_host_dep(struct proto_shell_state *state)
128 struct proto_shell_dependency *dep, *tmp;
130 list_for_each_entry_safe(dep, tmp, &state->deps, list) {
131 interface_remove_user(&dep->dep);
132 list_del(&dep->list);
138 proto_shell_handler(struct interface_proto_state *proto,
139 enum interface_proto_cmd cmd, bool force)
141 struct proto_shell_state *state;
142 struct proto_shell_handler *handler;
143 struct netifd_process *proc;
144 static char error_buf[32];
149 int ret, i = 0, j = 0;
151 state = container_of(proto, struct proto_shell_state, proto);
152 handler = state->handler;
153 proc = &state->script_task;
155 if (cmd == PROTO_CMD_SETUP) {
157 state->last_error = -1;
158 proto_shell_clear_host_dep(state);
160 if (state->sm == S_TEARDOWN)
163 if (state->script_task.uloop.pending) {
164 if (state->sm != S_SETUP_ABORT) {
165 uloop_timeout_set(&state->teardown_timeout, 1000);
166 kill(state->script_task.uloop.pid, SIGTERM);
167 if (state->proto_task.uloop.pending)
168 kill(state->proto_task.uloop.pid, SIGTERM);
169 state->sm = S_SETUP_ABORT;
175 state->sm = S_TEARDOWN;
176 if (state->last_error >= 0) {
177 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
178 envp[j++] = error_buf;
180 uloop_timeout_set(&state->teardown_timeout, 5000);
183 config = blobmsg_format_json(state->config, true);
187 argv[i++] = handler->script_name;
188 argv[i++] = handler->proto.name;
190 argv[i++] = proto->iface->name;
192 if (proto->iface->main_dev.dev)
193 argv[i++] = proto->iface->main_dev.dev->ifname;
197 ret = netifd_start_process(argv, envp, proc);
204 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
205 enum interface_event ev)
207 struct proto_shell_dependency *pdep;
212 pdep = container_of(dep, struct proto_shell_dependency, dep);
213 proto_shell_update_host_dep(pdep);
217 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
218 enum interface_event ev)
220 struct proto_shell_dependency *pdep;
221 struct proto_shell_state *state;
226 pdep = container_of(dep, struct proto_shell_dependency, dep);
227 interface_remove_user(dep);
228 dep->cb = proto_shell_if_up_cb;
229 interface_add_user(dep, NULL);
232 if (state->sm == S_IDLE) {
233 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
234 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
239 proto_shell_task_finish(struct proto_shell_state *state,
240 struct netifd_process *task)
244 if (task == &state->proto_task)
245 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
248 if (task == &state->proto_task)
249 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN,
254 if (state->script_task.uloop.pending ||
255 state->proto_task.uloop.pending)
258 uloop_timeout_cancel(&state->teardown_timeout);
260 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
264 if (state->script_task.uloop.pending)
267 if (state->proto_task.uloop.pending) {
268 if (!state->proto_task_killed)
269 kill(state->proto_task.uloop.pid, SIGTERM);
273 uloop_timeout_cancel(&state->teardown_timeout);
275 state->proto.proto_event(&state->proto, IFPEV_DOWN);
281 proto_shell_teardown_timeout_cb(struct uloop_timeout *timeout)
283 struct proto_shell_state *state;
285 state = container_of(timeout, struct proto_shell_state, teardown_timeout);
287 netifd_kill_process(&state->script_task);
288 netifd_kill_process(&state->proto_task);
289 proto_shell_task_finish(state, NULL);
293 proto_shell_script_cb(struct netifd_process *p, int ret)
295 struct proto_shell_state *state;
297 state = container_of(p, struct proto_shell_state, script_task);
298 proto_shell_task_finish(state, p);
302 proto_shell_task_cb(struct netifd_process *p, int ret)
304 struct proto_shell_state *state;
306 state = container_of(p, struct proto_shell_state, proto_task);
308 if (state->sm == S_IDLE || state->sm == S_SETUP)
309 state->last_error = WEXITSTATUS(ret);
311 proto_shell_task_finish(state, p);
315 proto_shell_free(struct interface_proto_state *proto)
317 struct proto_shell_state *state;
319 state = container_of(proto, struct proto_shell_state, proto);
320 proto_shell_clear_host_dep(state);
321 netifd_kill_process(&state->script_task);
322 netifd_kill_process(&state->proto_task);
328 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
331 struct blob_attr *cur;
334 blobmsg_for_each_attr(cur, attr, rem) {
335 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
336 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
340 interface_ip_add_route(iface, cur, v6);
345 proto_shell_parse_data(struct interface *iface, struct blob_attr *attr)
347 struct blob_attr *cur;
350 blobmsg_for_each_attr(cur, attr, rem)
351 interface_add_data(iface, cur);
354 static struct device *
355 proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
360 memset(&b, 0, sizeof(b));
361 blob_buf_init(&b, 0);
362 blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
363 dev = device_create(name, &tunnel_device_type, blob_data(b.head));
390 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
391 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
392 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
393 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
394 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
395 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
396 [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
397 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
398 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
399 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
400 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
401 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
402 [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
403 [NOTIFY_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
404 [NOTIFY_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
405 [NOTIFY_HOST] = { .name = "host", .type = BLOBMSG_TYPE_STRING },
406 [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
407 [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
411 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
413 struct interface *iface = state->proto.iface;
414 struct blob_attr *cur;
418 bool addr_ext = false;
422 if (!tb[NOTIFY_LINK_UP])
423 return UBUS_STATUS_INVALID_ARGUMENT;
425 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
427 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
431 if ((cur = tb[NOTIFY_KEEP]) != NULL)
432 keep = blobmsg_get_bool(cur);
434 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
435 addr_ext = blobmsg_get_bool(cur);
440 if (!tb[NOTIFY_IFNAME]) {
441 if (!iface->main_dev.dev)
442 return UBUS_STATUS_INVALID_ARGUMENT;
443 } else if (!keep || iface->state != IFS_UP) {
445 devname = blobmsg_data(tb[NOTIFY_IFNAME]);
446 if (tb[NOTIFY_TUNNEL]) {
447 dev = proto_shell_create_tunnel(devname,
450 return UBUS_STATUS_INVALID_ARGUMENT;
452 dev = device_get(devname, dev_create);
454 return UBUS_STATUS_NOT_FOUND;
457 interface_set_l3_dev(iface, dev);
458 device_claim(&iface->l3_dev);
459 device_set_present(dev, true);
463 interface_update_start(iface);
465 proto_apply_ip_settings(iface, data, addr_ext);
467 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
468 proto_shell_parse_route_list(state->proto.iface, cur, false);
470 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
471 proto_shell_parse_route_list(state->proto.iface, cur, true);
473 if ((cur = tb[NOTIFY_DNS]))
474 interface_add_dns_server_list(&iface->proto_ip, cur);
476 if ((cur = tb[NOTIFY_DNS_SEARCH]))
477 interface_add_dns_search_list(&iface->proto_ip, cur);
479 interface_update_complete(state->proto.iface);
482 state->proto.proto_event(&state->proto, IFPEV_UP);
485 if ((cur = tb[NOTIFY_DATA]))
486 proto_shell_parse_data(state->proto.iface, cur);
492 fill_string_list(struct blob_attr *attr, char **argv, int max)
494 struct blob_attr *cur;
501 blobmsg_for_each_attr(cur, attr, rem) {
502 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
505 if (!blobmsg_check_attr(cur, NULL))
508 argv[argc++] = blobmsg_data(cur);
519 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
521 static char *argv[64];
522 static char *env[32];
524 if (!tb[NOTIFY_COMMAND])
527 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
530 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
533 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
538 return UBUS_STATUS_INVALID_ARGUMENT;
542 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
544 unsigned int signal = ~0;
546 if (tb[NOTIFY_SIGNAL])
547 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
552 if (state->proto_task.uloop.pending) {
553 state->proto_task_killed = true;
554 kill(state->proto_task.uloop.pid, signal);
561 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
563 struct blob_attr *cur;
568 if (!tb[NOTIFY_ERROR])
569 return UBUS_STATUS_INVALID_ARGUMENT;
571 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
572 if (n_data + 1 == ARRAY_SIZE(data))
575 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
578 if (!blobmsg_check_attr(cur, NULL))
581 data[n_data++] = blobmsg_data(cur);
587 interface_add_error(state->proto.iface, state->handler->proto.name,
588 data[0], (const char **) &data[1], n_data - 1);
593 return UBUS_STATUS_INVALID_ARGUMENT;
597 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
599 state->proto.iface->autostart = false;
604 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
606 if (!tb[NOTIFY_AVAILABLE])
607 return UBUS_STATUS_INVALID_ARGUMENT;
609 interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
614 proto_shell_add_host_dependency(struct proto_shell_state *state, struct blob_attr **tb)
616 struct proto_shell_dependency *dep;
617 struct blob_attr *host = tb[NOTIFY_HOST];
620 return UBUS_STATUS_INVALID_ARGUMENT;
622 dep = calloc(1, sizeof(*dep));
623 if (!inet_pton(AF_INET, blobmsg_data(host), &dep->host)) {
625 return UBUS_STATUS_INVALID_ARGUMENT;
629 dep->dep.cb = proto_shell_if_up_cb;
630 interface_add_user(&dep->dep, NULL);
631 list_add(&dep->list, &state->deps);
632 proto_shell_update_host_dep(dep);
634 return UBUS_STATUS_NOT_FOUND;
640 proto_shell_setup_failed(struct proto_shell_state *state)
644 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
647 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
656 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
658 struct proto_shell_state *state;
659 struct blob_attr *tb[__NOTIFY_LAST];
661 state = container_of(proto, struct proto_shell_state, proto);
663 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
664 if (!tb[NOTIFY_ACTION])
665 return UBUS_STATUS_INVALID_ARGUMENT;
667 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
669 return proto_shell_update_link(state, attr, tb);
671 return proto_shell_run_command(state, tb);
673 return proto_shell_kill_command(state, tb);
675 return proto_shell_notify_error(state, tb);
677 return proto_shell_block_restart(state, tb);
679 return proto_shell_set_available(state, tb);
681 return proto_shell_add_host_dependency(state, tb);
683 return proto_shell_setup_failed(state);
685 return UBUS_STATUS_INVALID_ARGUMENT;
689 static struct interface_proto_state *
690 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
691 struct blob_attr *attr)
693 struct proto_shell_state *state;
695 state = calloc(1, sizeof(*state));
696 INIT_LIST_HEAD(&state->deps);
698 state->config = malloc(blob_pad_len(attr));
702 memcpy(state->config, attr, blob_pad_len(attr));
703 state->proto.free = proto_shell_free;
704 state->proto.notify = proto_shell_notify;
705 state->proto.cb = proto_shell_handler;
706 state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
707 state->script_task.cb = proto_shell_script_cb;
708 state->script_task.dir_fd = proto_fd;
709 state->script_task.log_prefix = iface->name;
710 state->proto_task.cb = proto_shell_task_cb;
711 state->proto_task.dir_fd = proto_fd;
712 state->proto_task.log_prefix = iface->name;
713 state->handler = container_of(h, struct proto_shell_handler, proto);
715 return &state->proto;
723 check_type(json_object *obj, json_type type)
728 if (json_object_get_type(obj) != type)
734 static inline json_object *
735 get_field(json_object *obj, const char *name, json_type type)
737 return check_type(json_object_object_get(obj, name), type);
741 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
743 struct blobmsg_policy *attrs;
744 char *str_buf, *str_cur;
748 config->n_params = json_object_array_length(obj);
749 attrs = calloc(1, sizeof(*attrs) * config->n_params);
753 config->params = attrs;
754 for (i = 0; i < config->n_params; i++) {
755 json_object *cur, *name, *type;
757 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
761 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
765 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
769 attrs[i].name = json_object_get_string(name);
770 attrs[i].type = json_object_get_int(type);
771 if (attrs[i].type > BLOBMSG_TYPE_LAST)
774 str_len += strlen(attrs[i].name) + 1;
777 str_buf = malloc(str_len);
782 for (i = 0; i < config->n_params; i++) {
783 const char *name = attrs[i].name;
785 attrs[i].name = str_cur;
786 str_cur += sprintf(str_cur, "%s", name) + 1;
793 config->n_params = 0;
798 proto_shell_add_handler(const char *script, json_object *obj)
800 struct proto_shell_handler *handler;
801 struct proto_handler *proto;
802 json_object *config, *tmp;
806 if (!check_type(obj, json_type_object))
809 tmp = get_field(obj, "name", json_type_string);
813 name = json_object_get_string(tmp);
815 handler = calloc(1, sizeof(*handler) +
821 strcpy(handler->script_name, script);
823 str = handler->script_name + strlen(handler->script_name) + 1;
826 proto = &handler->proto;
828 proto->config_params = &handler->config;
829 proto->attach = proto_shell_attach;
831 tmp = get_field(obj, "no-device", json_type_boolean);
832 if (tmp && json_object_get_boolean(tmp))
833 handler->proto.flags |= PROTO_FLAG_NODEV;
835 tmp = get_field(obj, "available", json_type_boolean);
836 if (tmp && json_object_get_boolean(tmp))
837 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
839 config = get_field(obj, "config", json_type_array);
841 handler->config_buf = proto_shell_parse_config(&handler->config, config);
843 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
844 add_proto_handler(proto);
847 static void proto_shell_add_script(const char *name)
849 struct json_tokener *tok = NULL;
851 static char buf[512];
856 #define DUMP_SUFFIX " '' dump"
858 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
859 sprintf(cmd, "%s" DUMP_SUFFIX, name);
866 start = fgets(buf, sizeof(buf), f);
873 tok = json_tokener_new();
875 obj = json_tokener_parse_ex(tok, start, len);
876 if (!is_error(obj)) {
877 proto_shell_add_handler(name, obj);
878 json_object_put(obj);
879 json_tokener_free(tok);
881 } else if (start[len - 1] == '\n') {
882 json_tokener_free(tok);
885 } while (!feof(f) && !ferror(f));
888 json_tokener_free(tok);
893 static void __init proto_shell_init(void)
899 main_fd = open(".", O_RDONLY | O_DIRECTORY);
903 if (chdir(main_path)) {
904 perror("chdir(main path)");
908 if (chdir("./proto"))
911 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
915 system_fd_set_cloexec(proto_fd);
916 glob("./*.sh", 0, NULL, &g);
917 for (i = 0; i < g.gl_pathc; i++)
918 proto_shell_add_script(g.gl_pathv[i]);