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 device_user l3_dev;
46 struct uloop_timeout teardown_timeout;
48 struct netifd_process script_task;
49 struct netifd_process proto_task;
51 enum proto_shell_sm sm;
52 bool proto_task_killed;
58 proto_shell_handler(struct interface_proto_state *proto,
59 enum interface_proto_cmd cmd, bool force)
61 struct proto_shell_state *state;
62 struct proto_shell_handler *handler;
63 struct netifd_process *proc;
64 static char error_buf[32];
69 int ret, i = 0, j = 0;
71 state = container_of(proto, struct proto_shell_state, proto);
72 handler = state->handler;
73 proc = &state->script_task;
75 if (cmd == PROTO_CMD_SETUP) {
77 state->last_error = -1;
79 if (state->sm == S_TEARDOWN)
82 if (state->script_task.uloop.pending) {
83 if (state->sm != S_SETUP_ABORT) {
84 uloop_timeout_set(&state->teardown_timeout, 1000);
85 kill(state->script_task.uloop.pid, SIGTERM);
86 if (state->proto_task.uloop.pending)
87 kill(state->proto_task.uloop.pid, SIGTERM);
88 state->sm = S_SETUP_ABORT;
94 state->sm = S_TEARDOWN;
95 if (state->last_error >= 0) {
96 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
97 envp[j++] = error_buf;
99 uloop_timeout_set(&state->teardown_timeout, 5000);
102 config = blobmsg_format_json(state->config, true);
106 argv[i++] = handler->script_name;
107 argv[i++] = handler->proto.name;
109 argv[i++] = proto->iface->name;
111 if (proto->iface->main_dev.dev)
112 argv[i++] = proto->iface->main_dev.dev->ifname;
116 ret = netifd_start_process(argv, envp, proc);
123 proto_shell_task_finish(struct proto_shell_state *state,
124 struct netifd_process *task)
128 if (task == &state->proto_task)
129 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
132 if (task == &state->proto_task)
133 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN,
138 if (state->script_task.uloop.pending ||
139 state->proto_task.uloop.pending)
142 uloop_timeout_cancel(&state->teardown_timeout);
144 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
148 if (state->script_task.uloop.pending)
151 if (state->proto_task.uloop.pending) {
152 if (!state->proto_task_killed)
153 kill(state->proto_task.uloop.pid, SIGTERM);
157 uloop_timeout_cancel(&state->teardown_timeout);
159 state->proto.proto_event(&state->proto, IFPEV_DOWN);
165 proto_shell_teardown_timeout_cb(struct uloop_timeout *timeout)
167 struct proto_shell_state *state;
169 state = container_of(timeout, struct proto_shell_state, teardown_timeout);
171 netifd_kill_process(&state->script_task);
172 netifd_kill_process(&state->proto_task);
173 proto_shell_task_finish(state, NULL);
177 proto_shell_script_cb(struct netifd_process *p, int ret)
179 struct proto_shell_state *state;
181 state = container_of(p, struct proto_shell_state, script_task);
182 proto_shell_task_finish(state, p);
186 proto_shell_task_cb(struct netifd_process *p, int ret)
188 struct proto_shell_state *state;
190 state = container_of(p, struct proto_shell_state, proto_task);
192 if (state->sm == S_IDLE || state->sm == S_SETUP)
193 state->last_error = WEXITSTATUS(ret);
195 proto_shell_task_finish(state, p);
199 proto_shell_free(struct interface_proto_state *proto)
201 struct proto_shell_state *state;
203 state = container_of(proto, struct proto_shell_state, proto);
204 netifd_kill_process(&state->script_task);
205 netifd_kill_process(&state->proto_task);
211 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
214 struct blob_attr *cur;
217 blobmsg_for_each_attr(cur, attr, rem) {
218 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
219 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
223 interface_ip_add_route(iface, cur, v6);
227 static struct device *
228 proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
233 memset(&b, 0, sizeof(b));
234 blob_buf_init(&b, 0);
235 blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
236 dev = device_create(name, &tunnel_device_type, blob_data(b.head));
258 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
259 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
260 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
261 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
262 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
263 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
264 [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
265 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
266 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
267 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
268 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
269 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
270 [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
274 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
276 struct interface *iface = state->proto.iface;
277 struct blob_attr *cur;
281 bool addr_ext = false;
284 if (!tb[NOTIFY_LINK_UP])
285 return UBUS_STATUS_INVALID_ARGUMENT;
287 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
289 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
293 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
294 addr_ext = blobmsg_get_bool(cur);
299 if (!tb[NOTIFY_IFNAME]) {
300 if (!iface->main_dev.dev)
301 return UBUS_STATUS_INVALID_ARGUMENT;
303 if (state->l3_dev.dev)
304 device_remove_user(&state->l3_dev);
306 devname = blobmsg_data(tb[NOTIFY_IFNAME]);
307 if (tb[NOTIFY_TUNNEL]) {
308 dev = proto_shell_create_tunnel(devname,
311 return UBUS_STATUS_INVALID_ARGUMENT;
313 dev = device_get(devname, dev_create);
315 return UBUS_STATUS_NOT_FOUND;
318 device_add_user(&state->l3_dev, dev);
319 iface->l3_dev = &state->l3_dev;
320 device_claim(&state->l3_dev);
323 interface_update_start(iface);
324 proto_apply_ip_settings(iface, data, addr_ext);
326 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
327 proto_shell_parse_route_list(state->proto.iface, cur, false);
329 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
330 proto_shell_parse_route_list(state->proto.iface, cur, true);
332 interface_update_complete(state->proto.iface);
334 state->proto.proto_event(&state->proto, IFPEV_UP);
340 fill_string_list(struct blob_attr *attr, char **argv, int max)
342 struct blob_attr *cur;
349 blobmsg_for_each_attr(cur, attr, rem) {
350 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
353 if (!blobmsg_check_attr(cur, NULL))
356 argv[argc++] = blobmsg_data(cur);
367 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
369 static char *argv[64];
370 static char *env[32];
372 if (!tb[NOTIFY_COMMAND])
375 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
378 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
381 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
386 return UBUS_STATUS_INVALID_ARGUMENT;
390 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
392 unsigned int signal = ~0;
394 if (tb[NOTIFY_SIGNAL])
395 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
400 if (state->proto_task.uloop.pending) {
401 state->proto_task_killed = true;
402 kill(state->proto_task.uloop.pid, signal);
409 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
411 struct blob_attr *cur;
416 if (!tb[NOTIFY_ERROR])
417 return UBUS_STATUS_INVALID_ARGUMENT;
419 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
420 if (n_data + 1 == ARRAY_SIZE(data))
423 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
426 if (!blobmsg_check_attr(cur, NULL))
429 data[n_data++] = blobmsg_data(cur);
435 interface_add_error(state->proto.iface, state->handler->proto.name,
436 data[0], (const char **) &data[1], n_data - 1);
441 return UBUS_STATUS_INVALID_ARGUMENT;
445 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
447 state->proto.iface->autostart = false;
452 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
454 if (!tb[NOTIFY_AVAILABLE])
455 return UBUS_STATUS_INVALID_ARGUMENT;
457 interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
462 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
464 struct proto_shell_state *state;
465 struct blob_attr *tb[__NOTIFY_LAST];
467 state = container_of(proto, struct proto_shell_state, proto);
469 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
470 if (!tb[NOTIFY_ACTION])
471 return UBUS_STATUS_INVALID_ARGUMENT;
473 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
475 return proto_shell_update_link(state, attr, tb);
477 return proto_shell_run_command(state, tb);
479 return proto_shell_kill_command(state, tb);
481 return proto_shell_notify_error(state, tb);
483 return proto_shell_block_restart(state, tb);
485 return proto_shell_set_available(state, tb);
487 return UBUS_STATUS_INVALID_ARGUMENT;
491 static struct interface_proto_state *
492 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
493 struct blob_attr *attr)
495 struct proto_shell_state *state;
497 state = calloc(1, sizeof(*state));
498 state->config = malloc(blob_pad_len(attr));
502 memcpy(state->config, attr, blob_pad_len(attr));
503 state->proto.free = proto_shell_free;
504 state->proto.notify = proto_shell_notify;
505 state->proto.cb = proto_shell_handler;
506 state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
507 state->script_task.cb = proto_shell_script_cb;
508 state->script_task.dir_fd = proto_fd.fd;
509 state->script_task.log_prefix = iface->name;
510 state->proto_task.cb = proto_shell_task_cb;
511 state->proto_task.dir_fd = proto_fd.fd;
512 state->proto_task.log_prefix = iface->name;
513 state->handler = container_of(h, struct proto_shell_handler, proto);
515 return &state->proto;
523 check_type(json_object *obj, json_type type)
528 if (json_object_get_type(obj) != type)
534 static inline json_object *
535 get_field(json_object *obj, const char *name, json_type type)
537 return check_type(json_object_object_get(obj, name), type);
541 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
543 struct blobmsg_policy *attrs;
544 char *str_buf, *str_cur;
548 config->n_params = json_object_array_length(obj);
549 attrs = calloc(1, sizeof(*attrs) * config->n_params);
553 config->params = attrs;
554 for (i = 0; i < config->n_params; i++) {
555 json_object *cur, *name, *type;
557 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
561 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
565 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
569 attrs[i].name = json_object_get_string(name);
570 attrs[i].type = json_object_get_int(type);
571 if (attrs[i].type > BLOBMSG_TYPE_LAST)
574 str_len += strlen(attrs[i].name) + 1;
577 str_buf = malloc(str_len);
582 for (i = 0; i < config->n_params; i++) {
583 const char *name = attrs[i].name;
585 attrs[i].name = str_cur;
586 str_cur += sprintf(str_cur, "%s", name) + 1;
593 config->n_params = 0;
598 proto_shell_add_handler(const char *script, json_object *obj)
600 struct proto_shell_handler *handler;
601 struct proto_handler *proto;
602 json_object *config, *tmp;
606 if (!check_type(obj, json_type_object))
609 tmp = get_field(obj, "name", json_type_string);
613 name = json_object_get_string(tmp);
615 handler = calloc(1, sizeof(*handler) +
621 strcpy(handler->script_name, script);
623 str = handler->script_name + strlen(handler->script_name) + 1;
626 proto = &handler->proto;
628 proto->config_params = &handler->config;
629 proto->attach = proto_shell_attach;
631 tmp = get_field(obj, "no-device", json_type_boolean);
632 if (tmp && json_object_get_boolean(tmp))
633 handler->proto.flags |= PROTO_FLAG_NODEV;
635 tmp = get_field(obj, "available", json_type_boolean);
636 if (tmp && json_object_get_boolean(tmp))
637 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
639 config = get_field(obj, "config", json_type_array);
641 handler->config_buf = proto_shell_parse_config(&handler->config, config);
643 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
644 add_proto_handler(proto);
647 static void proto_shell_add_script(const char *name)
649 struct json_tokener *tok = NULL;
651 static char buf[512];
656 #define DUMP_SUFFIX " '' dump"
658 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
659 sprintf(cmd, "%s" DUMP_SUFFIX, name);
666 start = fgets(buf, sizeof(buf), f);
673 tok = json_tokener_new();
675 obj = json_tokener_parse_ex(tok, start, len);
676 if (!is_error(obj)) {
677 proto_shell_add_handler(name, obj);
678 json_object_put(obj);
679 json_tokener_free(tok);
681 } else if (start[len - 1] == '\n') {
682 json_tokener_free(tok);
685 } while (!feof(f) && !ferror(f));
688 json_tokener_free(tok);
693 static void __init proto_shell_init(void)
699 main_fd = open(".", O_RDONLY | O_DIRECTORY);
703 if (chdir(main_path)) {
704 perror("chdir(main path)");
708 if (chdir("./proto"))
711 proto_fd.fd = open(".", O_RDONLY | O_DIRECTORY);
715 netifd_fd_add(&proto_fd);
716 glob("./*.sh", 0, NULL, &g);
717 for (i = 0; i < g.gl_pathc; i++)
718 proto_shell_add_script(g.gl_pathv[i]);