11 #include <arpa/inet.h>
12 #include <netinet/in.h>
14 #include <libubox/blobmsg_json.h>
17 #include "interface.h"
18 #include "interface-ip.h"
21 static struct netifd_fd proto_fd;
30 struct proto_shell_handler {
31 struct list_head list;
32 struct proto_handler proto;
33 struct config_param_list config;
39 struct proto_shell_state {
40 struct interface_proto_state proto;
41 struct proto_shell_handler *handler;
42 struct blob_attr *config;
44 struct uloop_timeout teardown_timeout;
46 struct netifd_process script_task;
47 struct netifd_process proto_task;
49 enum proto_shell_sm sm;
50 bool proto_task_killed;
56 proto_shell_handler(struct interface_proto_state *proto,
57 enum interface_proto_cmd cmd, bool force)
59 struct proto_shell_state *state;
60 struct proto_shell_handler *handler;
61 struct netifd_process *proc;
62 static char error_buf[32];
67 int ret, i = 0, j = 0;
69 state = container_of(proto, struct proto_shell_state, proto);
70 handler = state->handler;
71 proc = &state->script_task;
73 if (cmd == PROTO_CMD_SETUP) {
75 state->last_error = -1;
77 if (state->sm == S_TEARDOWN)
80 if (state->script_task.uloop.pending) {
81 if (state->sm != S_SETUP_ABORT) {
82 uloop_timeout_set(&state->teardown_timeout, 1000);
83 kill(state->script_task.uloop.pid, SIGTERM);
84 if (state->proto_task.uloop.pending)
85 kill(state->proto_task.uloop.pid, SIGTERM);
86 state->sm = S_SETUP_ABORT;
92 state->sm = S_TEARDOWN;
93 if (state->last_error >= 0) {
94 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
95 envp[j++] = error_buf;
97 uloop_timeout_set(&state->teardown_timeout, 5000);
100 config = blobmsg_format_json(state->config, true);
104 argv[i++] = handler->script_name;
105 argv[i++] = handler->proto.name;
107 argv[i++] = proto->iface->name;
109 if (proto->iface->main_dev.dev)
110 argv[i++] = proto->iface->main_dev.dev->ifname;
114 ret = netifd_start_process(argv, envp, proc);
121 proto_shell_task_finish(struct proto_shell_state *state,
122 struct netifd_process *task)
126 if (task == &state->proto_task)
127 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
130 if (task == &state->proto_task)
131 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN,
136 if (state->script_task.uloop.pending ||
137 state->proto_task.uloop.pending)
140 uloop_timeout_cancel(&state->teardown_timeout);
142 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
146 if (state->script_task.uloop.pending)
149 if (state->proto_task.uloop.pending) {
150 if (!state->proto_task_killed)
151 kill(state->proto_task.uloop.pid, SIGTERM);
155 uloop_timeout_cancel(&state->teardown_timeout);
157 state->proto.proto_event(&state->proto, IFPEV_DOWN);
163 proto_shell_teardown_timeout_cb(struct uloop_timeout *timeout)
165 struct proto_shell_state *state;
167 state = container_of(timeout, struct proto_shell_state, teardown_timeout);
169 netifd_kill_process(&state->script_task);
170 netifd_kill_process(&state->proto_task);
171 proto_shell_task_finish(state, NULL);
175 proto_shell_script_cb(struct netifd_process *p, int ret)
177 struct proto_shell_state *state;
179 state = container_of(p, struct proto_shell_state, script_task);
180 proto_shell_task_finish(state, p);
184 proto_shell_task_cb(struct netifd_process *p, int ret)
186 struct proto_shell_state *state;
188 state = container_of(p, struct proto_shell_state, proto_task);
190 if (state->sm == S_IDLE || state->sm == S_SETUP)
191 state->last_error = WEXITSTATUS(ret);
193 proto_shell_task_finish(state, p);
197 proto_shell_free(struct interface_proto_state *proto)
199 struct proto_shell_state *state;
201 state = container_of(proto, struct proto_shell_state, proto);
202 netifd_kill_process(&state->script_task);
203 netifd_kill_process(&state->proto_task);
209 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
212 struct blob_attr *cur;
215 blobmsg_for_each_attr(cur, attr, rem) {
216 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
217 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
221 interface_ip_add_route(iface, cur, v6);
226 proto_shell_parse_data(struct interface *iface, struct blob_attr *attr)
228 struct blob_attr *cur;
231 blobmsg_for_each_attr(cur, attr, rem)
232 interface_add_data(iface, cur);
235 static struct device *
236 proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
241 memset(&b, 0, sizeof(b));
242 blob_buf_init(&b, 0);
243 blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
244 dev = device_create(name, &tunnel_device_type, blob_data(b.head));
267 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
268 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
269 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
270 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
271 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
272 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
273 [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
274 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
275 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
276 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
277 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
278 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
279 [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
280 [NOTIFY_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
284 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
286 struct interface *iface = state->proto.iface;
287 struct blob_attr *cur;
291 bool addr_ext = false;
294 if (!tb[NOTIFY_LINK_UP])
295 return UBUS_STATUS_INVALID_ARGUMENT;
297 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
299 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
303 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
304 addr_ext = blobmsg_get_bool(cur);
309 if (!tb[NOTIFY_IFNAME]) {
310 if (!iface->main_dev.dev)
311 return UBUS_STATUS_INVALID_ARGUMENT;
313 devname = blobmsg_data(tb[NOTIFY_IFNAME]);
314 if (tb[NOTIFY_TUNNEL]) {
315 dev = proto_shell_create_tunnel(devname,
318 return UBUS_STATUS_INVALID_ARGUMENT;
320 dev = device_get(devname, dev_create);
322 return UBUS_STATUS_NOT_FOUND;
325 interface_set_l3_dev(iface, dev);
326 device_claim(&iface->l3_dev);
329 interface_update_start(iface);
330 proto_apply_ip_settings(iface, data, addr_ext);
332 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
333 proto_shell_parse_route_list(state->proto.iface, cur, false);
335 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
336 proto_shell_parse_route_list(state->proto.iface, cur, true);
338 interface_update_complete(state->proto.iface);
340 state->proto.proto_event(&state->proto, IFPEV_UP);
343 if ((cur = tb[NOTIFY_DATA]))
344 proto_shell_parse_data(state->proto.iface, cur);
350 fill_string_list(struct blob_attr *attr, char **argv, int max)
352 struct blob_attr *cur;
359 blobmsg_for_each_attr(cur, attr, rem) {
360 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
363 if (!blobmsg_check_attr(cur, NULL))
366 argv[argc++] = blobmsg_data(cur);
377 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
379 static char *argv[64];
380 static char *env[32];
382 if (!tb[NOTIFY_COMMAND])
385 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
388 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
391 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
396 return UBUS_STATUS_INVALID_ARGUMENT;
400 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
402 unsigned int signal = ~0;
404 if (tb[NOTIFY_SIGNAL])
405 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
410 if (state->proto_task.uloop.pending) {
411 state->proto_task_killed = true;
412 kill(state->proto_task.uloop.pid, signal);
419 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
421 struct blob_attr *cur;
426 if (!tb[NOTIFY_ERROR])
427 return UBUS_STATUS_INVALID_ARGUMENT;
429 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
430 if (n_data + 1 == ARRAY_SIZE(data))
433 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
436 if (!blobmsg_check_attr(cur, NULL))
439 data[n_data++] = blobmsg_data(cur);
445 interface_add_error(state->proto.iface, state->handler->proto.name,
446 data[0], (const char **) &data[1], n_data - 1);
451 return UBUS_STATUS_INVALID_ARGUMENT;
455 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
457 state->proto.iface->autostart = false;
462 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
464 if (!tb[NOTIFY_AVAILABLE])
465 return UBUS_STATUS_INVALID_ARGUMENT;
467 interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
472 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
474 struct proto_shell_state *state;
475 struct blob_attr *tb[__NOTIFY_LAST];
477 state = container_of(proto, struct proto_shell_state, proto);
479 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
480 if (!tb[NOTIFY_ACTION])
481 return UBUS_STATUS_INVALID_ARGUMENT;
483 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
485 return proto_shell_update_link(state, attr, tb);
487 return proto_shell_run_command(state, tb);
489 return proto_shell_kill_command(state, tb);
491 return proto_shell_notify_error(state, tb);
493 return proto_shell_block_restart(state, tb);
495 return proto_shell_set_available(state, tb);
497 return UBUS_STATUS_INVALID_ARGUMENT;
501 static struct interface_proto_state *
502 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
503 struct blob_attr *attr)
505 struct proto_shell_state *state;
507 state = calloc(1, sizeof(*state));
508 state->config = malloc(blob_pad_len(attr));
512 memcpy(state->config, attr, blob_pad_len(attr));
513 state->proto.free = proto_shell_free;
514 state->proto.notify = proto_shell_notify;
515 state->proto.cb = proto_shell_handler;
516 state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
517 state->script_task.cb = proto_shell_script_cb;
518 state->script_task.dir_fd = proto_fd.fd;
519 state->script_task.log_prefix = iface->name;
520 state->proto_task.cb = proto_shell_task_cb;
521 state->proto_task.dir_fd = proto_fd.fd;
522 state->proto_task.log_prefix = iface->name;
523 state->handler = container_of(h, struct proto_shell_handler, proto);
525 return &state->proto;
533 check_type(json_object *obj, json_type type)
538 if (json_object_get_type(obj) != type)
544 static inline json_object *
545 get_field(json_object *obj, const char *name, json_type type)
547 return check_type(json_object_object_get(obj, name), type);
551 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
553 struct blobmsg_policy *attrs;
554 char *str_buf, *str_cur;
558 config->n_params = json_object_array_length(obj);
559 attrs = calloc(1, sizeof(*attrs) * config->n_params);
563 config->params = attrs;
564 for (i = 0; i < config->n_params; i++) {
565 json_object *cur, *name, *type;
567 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
571 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
575 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
579 attrs[i].name = json_object_get_string(name);
580 attrs[i].type = json_object_get_int(type);
581 if (attrs[i].type > BLOBMSG_TYPE_LAST)
584 str_len += strlen(attrs[i].name) + 1;
587 str_buf = malloc(str_len);
592 for (i = 0; i < config->n_params; i++) {
593 const char *name = attrs[i].name;
595 attrs[i].name = str_cur;
596 str_cur += sprintf(str_cur, "%s", name) + 1;
603 config->n_params = 0;
608 proto_shell_add_handler(const char *script, json_object *obj)
610 struct proto_shell_handler *handler;
611 struct proto_handler *proto;
612 json_object *config, *tmp;
616 if (!check_type(obj, json_type_object))
619 tmp = get_field(obj, "name", json_type_string);
623 name = json_object_get_string(tmp);
625 handler = calloc(1, sizeof(*handler) +
631 strcpy(handler->script_name, script);
633 str = handler->script_name + strlen(handler->script_name) + 1;
636 proto = &handler->proto;
638 proto->config_params = &handler->config;
639 proto->attach = proto_shell_attach;
641 tmp = get_field(obj, "no-device", json_type_boolean);
642 if (tmp && json_object_get_boolean(tmp))
643 handler->proto.flags |= PROTO_FLAG_NODEV;
645 tmp = get_field(obj, "available", json_type_boolean);
646 if (tmp && json_object_get_boolean(tmp))
647 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
649 config = get_field(obj, "config", json_type_array);
651 handler->config_buf = proto_shell_parse_config(&handler->config, config);
653 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
654 add_proto_handler(proto);
657 static void proto_shell_add_script(const char *name)
659 struct json_tokener *tok = NULL;
661 static char buf[512];
666 #define DUMP_SUFFIX " '' dump"
668 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
669 sprintf(cmd, "%s" DUMP_SUFFIX, name);
676 start = fgets(buf, sizeof(buf), f);
683 tok = json_tokener_new();
685 obj = json_tokener_parse_ex(tok, start, len);
686 if (!is_error(obj)) {
687 proto_shell_add_handler(name, obj);
688 json_object_put(obj);
689 json_tokener_free(tok);
691 } else if (start[len - 1] == '\n') {
692 json_tokener_free(tok);
695 } while (!feof(f) && !ferror(f));
698 json_tokener_free(tok);
703 static void __init proto_shell_init(void)
709 main_fd = open(".", O_RDONLY | O_DIRECTORY);
713 if (chdir(main_path)) {
714 perror("chdir(main path)");
718 if (chdir("./proto"))
721 proto_fd.fd = open(".", O_RDONLY | O_DIRECTORY);
725 netifd_fd_add(&proto_fd);
726 glob("./*.sh", 0, NULL, &g);
727 for (i = 0; i < g.gl_pathc; i++)
728 proto_shell_add_script(g.gl_pathv[i]);