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"
34 static struct netifd_fd proto_fd;
43 struct proto_shell_handler {
44 struct list_head list;
45 struct proto_handler proto;
46 struct config_param_list config;
52 struct proto_shell_state {
53 struct interface_proto_state proto;
54 struct proto_shell_handler *handler;
55 struct blob_attr *config;
57 struct uloop_timeout teardown_timeout;
59 struct netifd_process script_task;
60 struct netifd_process proto_task;
62 enum proto_shell_sm sm;
63 bool proto_task_killed;
69 proto_shell_handler(struct interface_proto_state *proto,
70 enum interface_proto_cmd cmd, bool force)
72 struct proto_shell_state *state;
73 struct proto_shell_handler *handler;
74 struct netifd_process *proc;
75 static char error_buf[32];
80 int ret, i = 0, j = 0;
82 state = container_of(proto, struct proto_shell_state, proto);
83 handler = state->handler;
84 proc = &state->script_task;
86 if (cmd == PROTO_CMD_SETUP) {
88 state->last_error = -1;
90 if (state->sm == S_TEARDOWN)
93 if (state->script_task.uloop.pending) {
94 if (state->sm != S_SETUP_ABORT) {
95 uloop_timeout_set(&state->teardown_timeout, 1000);
96 kill(state->script_task.uloop.pid, SIGTERM);
97 if (state->proto_task.uloop.pending)
98 kill(state->proto_task.uloop.pid, SIGTERM);
99 state->sm = S_SETUP_ABORT;
105 state->sm = S_TEARDOWN;
106 if (state->last_error >= 0) {
107 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
108 envp[j++] = error_buf;
110 uloop_timeout_set(&state->teardown_timeout, 5000);
113 config = blobmsg_format_json(state->config, true);
117 argv[i++] = handler->script_name;
118 argv[i++] = handler->proto.name;
120 argv[i++] = proto->iface->name;
122 if (proto->iface->main_dev.dev)
123 argv[i++] = proto->iface->main_dev.dev->ifname;
127 ret = netifd_start_process(argv, envp, proc);
134 proto_shell_task_finish(struct proto_shell_state *state,
135 struct netifd_process *task)
139 if (task == &state->proto_task)
140 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
143 if (task == &state->proto_task)
144 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN,
149 if (state->script_task.uloop.pending ||
150 state->proto_task.uloop.pending)
153 uloop_timeout_cancel(&state->teardown_timeout);
155 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
159 if (state->script_task.uloop.pending)
162 if (state->proto_task.uloop.pending) {
163 if (!state->proto_task_killed)
164 kill(state->proto_task.uloop.pid, SIGTERM);
168 uloop_timeout_cancel(&state->teardown_timeout);
170 state->proto.proto_event(&state->proto, IFPEV_DOWN);
176 proto_shell_teardown_timeout_cb(struct uloop_timeout *timeout)
178 struct proto_shell_state *state;
180 state = container_of(timeout, struct proto_shell_state, teardown_timeout);
182 netifd_kill_process(&state->script_task);
183 netifd_kill_process(&state->proto_task);
184 proto_shell_task_finish(state, NULL);
188 proto_shell_script_cb(struct netifd_process *p, int ret)
190 struct proto_shell_state *state;
192 state = container_of(p, struct proto_shell_state, script_task);
193 proto_shell_task_finish(state, p);
197 proto_shell_task_cb(struct netifd_process *p, int ret)
199 struct proto_shell_state *state;
201 state = container_of(p, struct proto_shell_state, proto_task);
203 if (state->sm == S_IDLE || state->sm == S_SETUP)
204 state->last_error = WEXITSTATUS(ret);
206 proto_shell_task_finish(state, p);
210 proto_shell_free(struct interface_proto_state *proto)
212 struct proto_shell_state *state;
214 state = container_of(proto, struct proto_shell_state, proto);
215 netifd_kill_process(&state->script_task);
216 netifd_kill_process(&state->proto_task);
222 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
225 struct blob_attr *cur;
228 blobmsg_for_each_attr(cur, attr, rem) {
229 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
230 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
234 interface_ip_add_route(iface, cur, v6);
239 proto_shell_parse_data(struct interface *iface, struct blob_attr *attr)
241 struct blob_attr *cur;
244 blobmsg_for_each_attr(cur, attr, rem)
245 interface_add_data(iface, cur);
248 static struct device *
249 proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
254 memset(&b, 0, sizeof(b));
255 blob_buf_init(&b, 0);
256 blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
257 dev = device_create(name, &tunnel_device_type, blob_data(b.head));
281 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
282 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
283 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
284 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
285 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
286 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
287 [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
288 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
289 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
290 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
291 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
292 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
293 [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
294 [NOTIFY_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
295 [NOTIFY_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
299 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
301 struct interface *iface = state->proto.iface;
302 struct blob_attr *cur;
306 bool addr_ext = false;
310 if (!tb[NOTIFY_LINK_UP])
311 return UBUS_STATUS_INVALID_ARGUMENT;
313 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
315 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
319 if ((cur = tb[NOTIFY_KEEP]) != NULL)
320 keep = blobmsg_get_bool(cur);
322 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
323 addr_ext = blobmsg_get_bool(cur);
328 if (!tb[NOTIFY_IFNAME]) {
329 if (!iface->main_dev.dev)
330 return UBUS_STATUS_INVALID_ARGUMENT;
331 } else if (!keep || iface->state != IFS_UP) {
333 devname = blobmsg_data(tb[NOTIFY_IFNAME]);
334 if (tb[NOTIFY_TUNNEL]) {
335 dev = proto_shell_create_tunnel(devname,
338 return UBUS_STATUS_INVALID_ARGUMENT;
340 dev = device_get(devname, dev_create);
342 return UBUS_STATUS_NOT_FOUND;
345 interface_set_l3_dev(iface, dev);
346 device_claim(&iface->l3_dev);
350 interface_update_start(iface);
352 proto_apply_ip_settings(iface, data, addr_ext);
354 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
355 proto_shell_parse_route_list(state->proto.iface, cur, false);
357 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
358 proto_shell_parse_route_list(state->proto.iface, cur, true);
360 interface_update_complete(state->proto.iface);
363 state->proto.proto_event(&state->proto, IFPEV_UP);
366 if ((cur = tb[NOTIFY_DATA]))
367 proto_shell_parse_data(state->proto.iface, cur);
373 fill_string_list(struct blob_attr *attr, char **argv, int max)
375 struct blob_attr *cur;
382 blobmsg_for_each_attr(cur, attr, rem) {
383 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
386 if (!blobmsg_check_attr(cur, NULL))
389 argv[argc++] = blobmsg_data(cur);
400 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
402 static char *argv[64];
403 static char *env[32];
405 if (!tb[NOTIFY_COMMAND])
408 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
411 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
414 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
419 return UBUS_STATUS_INVALID_ARGUMENT;
423 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
425 unsigned int signal = ~0;
427 if (tb[NOTIFY_SIGNAL])
428 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
433 if (state->proto_task.uloop.pending) {
434 state->proto_task_killed = true;
435 kill(state->proto_task.uloop.pid, signal);
442 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
444 struct blob_attr *cur;
449 if (!tb[NOTIFY_ERROR])
450 return UBUS_STATUS_INVALID_ARGUMENT;
452 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
453 if (n_data + 1 == ARRAY_SIZE(data))
456 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
459 if (!blobmsg_check_attr(cur, NULL))
462 data[n_data++] = blobmsg_data(cur);
468 interface_add_error(state->proto.iface, state->handler->proto.name,
469 data[0], (const char **) &data[1], n_data - 1);
474 return UBUS_STATUS_INVALID_ARGUMENT;
478 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
480 state->proto.iface->autostart = false;
485 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
487 if (!tb[NOTIFY_AVAILABLE])
488 return UBUS_STATUS_INVALID_ARGUMENT;
490 interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
495 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
497 struct proto_shell_state *state;
498 struct blob_attr *tb[__NOTIFY_LAST];
500 state = container_of(proto, struct proto_shell_state, proto);
502 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
503 if (!tb[NOTIFY_ACTION])
504 return UBUS_STATUS_INVALID_ARGUMENT;
506 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
508 return proto_shell_update_link(state, attr, tb);
510 return proto_shell_run_command(state, tb);
512 return proto_shell_kill_command(state, tb);
514 return proto_shell_notify_error(state, tb);
516 return proto_shell_block_restart(state, tb);
518 return proto_shell_set_available(state, tb);
520 return UBUS_STATUS_INVALID_ARGUMENT;
524 static struct interface_proto_state *
525 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
526 struct blob_attr *attr)
528 struct proto_shell_state *state;
530 state = calloc(1, sizeof(*state));
531 state->config = malloc(blob_pad_len(attr));
535 memcpy(state->config, attr, blob_pad_len(attr));
536 state->proto.free = proto_shell_free;
537 state->proto.notify = proto_shell_notify;
538 state->proto.cb = proto_shell_handler;
539 state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
540 state->script_task.cb = proto_shell_script_cb;
541 state->script_task.dir_fd = proto_fd.fd;
542 state->script_task.log_prefix = iface->name;
543 state->proto_task.cb = proto_shell_task_cb;
544 state->proto_task.dir_fd = proto_fd.fd;
545 state->proto_task.log_prefix = iface->name;
546 state->handler = container_of(h, struct proto_shell_handler, proto);
548 return &state->proto;
556 check_type(json_object *obj, json_type type)
561 if (json_object_get_type(obj) != type)
567 static inline json_object *
568 get_field(json_object *obj, const char *name, json_type type)
570 return check_type(json_object_object_get(obj, name), type);
574 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
576 struct blobmsg_policy *attrs;
577 char *str_buf, *str_cur;
581 config->n_params = json_object_array_length(obj);
582 attrs = calloc(1, sizeof(*attrs) * config->n_params);
586 config->params = attrs;
587 for (i = 0; i < config->n_params; i++) {
588 json_object *cur, *name, *type;
590 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
594 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
598 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
602 attrs[i].name = json_object_get_string(name);
603 attrs[i].type = json_object_get_int(type);
604 if (attrs[i].type > BLOBMSG_TYPE_LAST)
607 str_len += strlen(attrs[i].name) + 1;
610 str_buf = malloc(str_len);
615 for (i = 0; i < config->n_params; i++) {
616 const char *name = attrs[i].name;
618 attrs[i].name = str_cur;
619 str_cur += sprintf(str_cur, "%s", name) + 1;
626 config->n_params = 0;
631 proto_shell_add_handler(const char *script, json_object *obj)
633 struct proto_shell_handler *handler;
634 struct proto_handler *proto;
635 json_object *config, *tmp;
639 if (!check_type(obj, json_type_object))
642 tmp = get_field(obj, "name", json_type_string);
646 name = json_object_get_string(tmp);
648 handler = calloc(1, sizeof(*handler) +
654 strcpy(handler->script_name, script);
656 str = handler->script_name + strlen(handler->script_name) + 1;
659 proto = &handler->proto;
661 proto->config_params = &handler->config;
662 proto->attach = proto_shell_attach;
664 tmp = get_field(obj, "no-device", json_type_boolean);
665 if (tmp && json_object_get_boolean(tmp))
666 handler->proto.flags |= PROTO_FLAG_NODEV;
668 tmp = get_field(obj, "available", json_type_boolean);
669 if (tmp && json_object_get_boolean(tmp))
670 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
672 config = get_field(obj, "config", json_type_array);
674 handler->config_buf = proto_shell_parse_config(&handler->config, config);
676 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
677 add_proto_handler(proto);
680 static void proto_shell_add_script(const char *name)
682 struct json_tokener *tok = NULL;
684 static char buf[512];
689 #define DUMP_SUFFIX " '' dump"
691 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
692 sprintf(cmd, "%s" DUMP_SUFFIX, name);
699 start = fgets(buf, sizeof(buf), f);
706 tok = json_tokener_new();
708 obj = json_tokener_parse_ex(tok, start, len);
709 if (!is_error(obj)) {
710 proto_shell_add_handler(name, obj);
711 json_object_put(obj);
712 json_tokener_free(tok);
714 } else if (start[len - 1] == '\n') {
715 json_tokener_free(tok);
718 } while (!feof(f) && !ferror(f));
721 json_tokener_free(tok);
726 static void __init proto_shell_init(void)
732 main_fd = open(".", O_RDONLY | O_DIRECTORY);
736 if (chdir(main_path)) {
737 perror("chdir(main path)");
741 if (chdir("./proto"))
744 proto_fd.fd = open(".", O_RDONLY | O_DIRECTORY);
748 netifd_fd_add(&proto_fd);
749 glob("./*.sh", 0, NULL, &g);
750 for (i = 0; i < g.gl_pathc; i++)
751 proto_shell_add_script(g.gl_pathv[i]);