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;
65 struct proto_shell_state {
66 struct interface_proto_state proto;
67 struct proto_shell_handler *handler;
68 struct blob_attr *config;
70 struct uloop_timeout teardown_timeout;
72 struct netifd_process script_task;
73 struct netifd_process proto_task;
75 enum proto_shell_sm sm;
76 bool proto_task_killed;
80 struct list_head deps;
84 proto_shell_check_dependencies(struct proto_shell_state *state)
86 struct proto_shell_dependency *dep;
87 bool available = true;
89 list_for_each_entry(dep, &state->deps, list) {
97 interface_set_available(state->proto.iface, available);
101 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
102 enum interface_event ev);
104 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
105 enum interface_event ev);
108 proto_shell_update_host_dep(struct proto_shell_dependency *dep)
110 struct interface *iface = NULL;
115 if (dep->interface[0])
116 iface = vlist_find(&interfaces, dep->interface, iface, node);
118 iface = interface_ip_add_target_route(&dep->host, dep->v6, iface);
122 interface_remove_user(&dep->dep);
123 dep->dep.cb = proto_shell_if_down_cb;
124 interface_add_user(&dep->dep, iface);
127 proto_shell_check_dependencies(dep->proto);
131 proto_shell_clear_host_dep(struct proto_shell_state *state)
133 struct proto_shell_dependency *dep, *tmp;
135 list_for_each_entry_safe(dep, tmp, &state->deps, list) {
136 interface_remove_user(&dep->dep);
137 list_del(&dep->list);
143 proto_shell_handler(struct interface_proto_state *proto,
144 enum interface_proto_cmd cmd, bool force)
146 struct proto_shell_state *state;
147 struct proto_shell_handler *handler;
148 struct netifd_process *proc;
149 static char error_buf[32];
154 int ret, i = 0, j = 0;
156 state = container_of(proto, struct proto_shell_state, proto);
157 handler = state->handler;
158 proc = &state->script_task;
160 if (cmd == PROTO_CMD_SETUP) {
162 state->last_error = -1;
163 proto_shell_clear_host_dep(state);
165 if (state->sm == S_TEARDOWN)
168 if (state->script_task.uloop.pending) {
169 if (state->sm != S_SETUP_ABORT) {
170 uloop_timeout_set(&state->teardown_timeout, 1000);
171 kill(state->script_task.uloop.pid, SIGTERM);
172 if (state->proto_task.uloop.pending)
173 kill(state->proto_task.uloop.pid, SIGTERM);
174 state->sm = S_SETUP_ABORT;
180 state->sm = S_TEARDOWN;
181 if (state->last_error >= 0) {
182 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
183 envp[j++] = error_buf;
185 uloop_timeout_set(&state->teardown_timeout, 5000);
188 config = blobmsg_format_json(state->config, true);
192 argv[i++] = handler->script_name;
193 argv[i++] = handler->proto.name;
195 argv[i++] = proto->iface->name;
197 if (proto->iface->main_dev.dev)
198 argv[i++] = proto->iface->main_dev.dev->ifname;
202 ret = netifd_start_process(argv, envp, proc);
209 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
210 enum interface_event ev)
212 struct proto_shell_dependency *pdep;
217 pdep = container_of(dep, struct proto_shell_dependency, dep);
218 proto_shell_update_host_dep(pdep);
222 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
223 enum interface_event ev)
225 struct proto_shell_dependency *pdep;
226 struct proto_shell_state *state;
231 pdep = container_of(dep, struct proto_shell_dependency, dep);
232 interface_remove_user(dep);
233 dep->cb = proto_shell_if_up_cb;
234 interface_add_user(dep, NULL);
237 if (state->sm == S_IDLE) {
238 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
239 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
244 proto_shell_task_finish(struct proto_shell_state *state,
245 struct netifd_process *task)
249 if (task == &state->proto_task)
250 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
253 if (task == &state->proto_task)
254 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN,
259 if (state->script_task.uloop.pending ||
260 state->proto_task.uloop.pending)
263 uloop_timeout_cancel(&state->teardown_timeout);
265 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
269 if (state->script_task.uloop.pending)
272 if (state->proto_task.uloop.pending) {
273 if (!state->proto_task_killed)
274 kill(state->proto_task.uloop.pid, SIGTERM);
278 uloop_timeout_cancel(&state->teardown_timeout);
280 state->proto.proto_event(&state->proto, IFPEV_DOWN);
286 proto_shell_teardown_timeout_cb(struct uloop_timeout *timeout)
288 struct proto_shell_state *state;
290 state = container_of(timeout, struct proto_shell_state, teardown_timeout);
292 netifd_kill_process(&state->script_task);
293 netifd_kill_process(&state->proto_task);
294 proto_shell_task_finish(state, NULL);
298 proto_shell_script_cb(struct netifd_process *p, int ret)
300 struct proto_shell_state *state;
302 state = container_of(p, struct proto_shell_state, script_task);
303 proto_shell_task_finish(state, p);
307 proto_shell_task_cb(struct netifd_process *p, int ret)
309 struct proto_shell_state *state;
311 state = container_of(p, struct proto_shell_state, proto_task);
313 if (state->sm == S_IDLE || state->sm == S_SETUP)
314 state->last_error = WEXITSTATUS(ret);
316 proto_shell_task_finish(state, p);
320 proto_shell_free(struct interface_proto_state *proto)
322 struct proto_shell_state *state;
324 state = container_of(proto, struct proto_shell_state, proto);
325 uloop_timeout_cancel(&state->teardown_timeout);
326 proto_shell_clear_host_dep(state);
327 netifd_kill_process(&state->script_task);
328 netifd_kill_process(&state->proto_task);
334 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
337 struct blob_attr *cur;
340 blobmsg_for_each_attr(cur, attr, rem) {
341 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
342 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
346 interface_ip_add_route(iface, cur, v6);
351 proto_shell_parse_data(struct interface *iface, struct blob_attr *attr)
353 struct blob_attr *cur;
356 blobmsg_for_each_attr(cur, attr, rem)
357 interface_add_data(iface, cur);
360 static struct device *
361 proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
366 memset(&b, 0, sizeof(b));
367 blob_buf_init(&b, 0);
368 blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
369 dev = device_create(name, &tunnel_device_type, blob_data(b.head));
396 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
397 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
398 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
399 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
400 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
401 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
402 [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
403 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
404 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
405 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
406 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
407 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
408 [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
409 [NOTIFY_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
410 [NOTIFY_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
411 [NOTIFY_HOST] = { .name = "host", .type = BLOBMSG_TYPE_STRING },
412 [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
413 [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
417 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
419 struct interface *iface = state->proto.iface;
420 struct blob_attr *cur;
424 bool addr_ext = false;
428 if (!tb[NOTIFY_LINK_UP])
429 return UBUS_STATUS_INVALID_ARGUMENT;
431 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
433 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
437 if ((cur = tb[NOTIFY_KEEP]) != NULL)
438 keep = blobmsg_get_bool(cur);
440 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
441 addr_ext = blobmsg_get_bool(cur);
446 if (!tb[NOTIFY_IFNAME]) {
447 if (!iface->main_dev.dev)
448 return UBUS_STATUS_INVALID_ARGUMENT;
449 } else if (!keep || iface->state != IFS_UP) {
451 devname = blobmsg_data(tb[NOTIFY_IFNAME]);
452 if (tb[NOTIFY_TUNNEL]) {
453 dev = proto_shell_create_tunnel(devname,
456 return UBUS_STATUS_INVALID_ARGUMENT;
458 dev = device_get(devname, dev_create);
460 return UBUS_STATUS_NOT_FOUND;
463 interface_set_l3_dev(iface, dev);
464 device_claim(&iface->l3_dev);
465 device_set_present(dev, true);
469 interface_update_start(iface);
471 proto_apply_ip_settings(iface, data, addr_ext);
473 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
474 proto_shell_parse_route_list(state->proto.iface, cur, false);
476 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
477 proto_shell_parse_route_list(state->proto.iface, cur, true);
479 if ((cur = tb[NOTIFY_DNS]))
480 interface_add_dns_server_list(&iface->proto_ip, cur);
482 if ((cur = tb[NOTIFY_DNS_SEARCH]))
483 interface_add_dns_search_list(&iface->proto_ip, cur);
485 interface_update_complete(state->proto.iface);
488 state->proto.proto_event(&state->proto, IFPEV_UP);
491 if ((cur = tb[NOTIFY_DATA]))
492 proto_shell_parse_data(state->proto.iface, cur);
498 fill_string_list(struct blob_attr *attr, char **argv, int max)
500 struct blob_attr *cur;
507 blobmsg_for_each_attr(cur, attr, rem) {
508 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
511 if (!blobmsg_check_attr(cur, NULL))
514 argv[argc++] = blobmsg_data(cur);
525 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
527 static char *argv[64];
528 static char *env[32];
530 if (!tb[NOTIFY_COMMAND])
533 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
536 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
539 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
544 return UBUS_STATUS_INVALID_ARGUMENT;
548 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
550 unsigned int signal = ~0;
552 if (tb[NOTIFY_SIGNAL])
553 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
558 if (state->proto_task.uloop.pending) {
559 state->proto_task_killed = true;
560 kill(state->proto_task.uloop.pid, signal);
567 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
569 struct blob_attr *cur;
574 if (!tb[NOTIFY_ERROR])
575 return UBUS_STATUS_INVALID_ARGUMENT;
577 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
578 if (n_data + 1 == ARRAY_SIZE(data))
581 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
584 if (!blobmsg_check_attr(cur, NULL))
587 data[n_data++] = blobmsg_data(cur);
593 interface_add_error(state->proto.iface, state->handler->proto.name,
594 data[0], (const char **) &data[1], n_data - 1);
599 return UBUS_STATUS_INVALID_ARGUMENT;
603 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
605 state->proto.iface->autostart = false;
610 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
612 if (!tb[NOTIFY_AVAILABLE])
613 return UBUS_STATUS_INVALID_ARGUMENT;
615 interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
620 proto_shell_add_host_dependency(struct proto_shell_state *state, struct blob_attr **tb)
622 struct proto_shell_dependency *dep;
623 struct blob_attr *host = tb[NOTIFY_HOST];
624 struct blob_attr *ifname = tb[NOTIFY_IFNAME];
625 size_t ifnamelen = (ifname) ? blobmsg_data_len(ifname) : 1;
628 return UBUS_STATUS_INVALID_ARGUMENT;
630 dep = calloc(1, sizeof(*dep) + ifnamelen);
631 if (inet_pton(AF_INET, blobmsg_data(host), &dep->host) < 1) {
632 if (inet_pton(AF_INET6, blobmsg_data(host), &dep->host) < 1) {
634 return UBUS_STATUS_INVALID_ARGUMENT;
642 memcpy(dep->interface, blobmsg_data(ifname), ifnamelen);
644 dep->interface[0] = 0;
646 dep->dep.cb = proto_shell_if_up_cb;
647 interface_add_user(&dep->dep, NULL);
648 list_add(&dep->list, &state->deps);
649 proto_shell_update_host_dep(dep);
651 return UBUS_STATUS_NOT_FOUND;
657 proto_shell_setup_failed(struct proto_shell_state *state)
661 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
664 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
673 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
675 struct proto_shell_state *state;
676 struct blob_attr *tb[__NOTIFY_LAST];
678 state = container_of(proto, struct proto_shell_state, proto);
680 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
681 if (!tb[NOTIFY_ACTION])
682 return UBUS_STATUS_INVALID_ARGUMENT;
684 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
686 return proto_shell_update_link(state, attr, tb);
688 return proto_shell_run_command(state, tb);
690 return proto_shell_kill_command(state, tb);
692 return proto_shell_notify_error(state, tb);
694 return proto_shell_block_restart(state, tb);
696 return proto_shell_set_available(state, tb);
698 return proto_shell_add_host_dependency(state, tb);
700 return proto_shell_setup_failed(state);
702 return UBUS_STATUS_INVALID_ARGUMENT;
706 static struct interface_proto_state *
707 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
708 struct blob_attr *attr)
710 struct proto_shell_state *state;
712 state = calloc(1, sizeof(*state));
713 INIT_LIST_HEAD(&state->deps);
715 state->config = malloc(blob_pad_len(attr));
719 memcpy(state->config, attr, blob_pad_len(attr));
720 state->proto.free = proto_shell_free;
721 state->proto.notify = proto_shell_notify;
722 state->proto.cb = proto_shell_handler;
723 state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
724 state->script_task.cb = proto_shell_script_cb;
725 state->script_task.dir_fd = proto_fd;
726 state->script_task.log_prefix = iface->name;
727 state->proto_task.cb = proto_shell_task_cb;
728 state->proto_task.dir_fd = proto_fd;
729 state->proto_task.log_prefix = iface->name;
730 state->handler = container_of(h, struct proto_shell_handler, proto);
732 return &state->proto;
740 check_type(json_object *obj, json_type type)
745 if (json_object_get_type(obj) != type)
751 static inline json_object *
752 get_field(json_object *obj, const char *name, json_type type)
754 return check_type(json_object_object_get(obj, name), type);
758 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
760 struct blobmsg_policy *attrs;
761 char *str_buf, *str_cur;
765 config->n_params = json_object_array_length(obj);
766 attrs = calloc(1, sizeof(*attrs) * config->n_params);
770 config->params = attrs;
771 for (i = 0; i < config->n_params; i++) {
772 json_object *cur, *name, *type;
774 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
778 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
782 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
786 attrs[i].name = json_object_get_string(name);
787 attrs[i].type = json_object_get_int(type);
788 if (attrs[i].type > BLOBMSG_TYPE_LAST)
791 str_len += strlen(attrs[i].name) + 1;
794 str_buf = malloc(str_len);
799 for (i = 0; i < config->n_params; i++) {
800 const char *name = attrs[i].name;
802 attrs[i].name = str_cur;
803 str_cur += sprintf(str_cur, "%s", name) + 1;
810 config->n_params = 0;
815 proto_shell_add_handler(const char *script, json_object *obj)
817 struct proto_shell_handler *handler;
818 struct proto_handler *proto;
819 json_object *config, *tmp;
823 if (!check_type(obj, json_type_object))
826 tmp = get_field(obj, "name", json_type_string);
830 name = json_object_get_string(tmp);
832 handler = calloc_a(sizeof(*handler) + strlen(script) + 1,
833 &str, strlen(name) + 1);
837 strcpy(handler->script_name, script);
840 proto = &handler->proto;
842 proto->config_params = &handler->config;
843 proto->attach = proto_shell_attach;
845 tmp = get_field(obj, "no-device", json_type_boolean);
846 if (tmp && json_object_get_boolean(tmp))
847 handler->proto.flags |= PROTO_FLAG_NODEV;
849 tmp = get_field(obj, "available", json_type_boolean);
850 if (tmp && json_object_get_boolean(tmp))
851 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
853 config = get_field(obj, "config", json_type_array);
855 handler->config_buf = proto_shell_parse_config(&handler->config, config);
857 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
858 add_proto_handler(proto);
861 static void proto_shell_add_script(const char *name)
863 struct json_tokener *tok = NULL;
865 static char buf[512];
870 #define DUMP_SUFFIX " '' dump"
872 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
873 sprintf(cmd, "%s" DUMP_SUFFIX, name);
880 start = fgets(buf, sizeof(buf), f);
887 tok = json_tokener_new();
889 obj = json_tokener_parse_ex(tok, start, len);
890 if (!is_error(obj)) {
891 proto_shell_add_handler(name, obj);
892 json_object_put(obj);
893 json_tokener_free(tok);
895 } else if (start[len - 1] == '\n') {
896 json_tokener_free(tok);
899 } while (!feof(f) && !ferror(f));
902 json_tokener_free(tok);
907 static void __init proto_shell_init(void)
913 main_fd = open(".", O_RDONLY | O_DIRECTORY);
917 if (chdir(main_path)) {
918 perror("chdir(main path)");
922 if (chdir("./proto"))
925 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
929 system_fd_set_cloexec(proto_fd);
930 glob("./*.sh", 0, NULL, &g);
931 for (i = 0; i < g.gl_pathc; i++)
932 proto_shell_add_script(g.gl_pathv[i]);