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 setup_task;
49 struct netifd_process teardown_task;
50 struct netifd_process proto_task;
52 enum proto_shell_sm sm;
53 bool proto_task_killed;
59 proto_shell_handler(struct interface_proto_state *proto,
60 enum interface_proto_cmd cmd, bool force)
62 struct proto_shell_state *state;
63 struct proto_shell_handler *handler;
64 struct netifd_process *proc;
65 static char error_buf[32];
70 int ret, i = 0, j = 0;
72 state = container_of(proto, struct proto_shell_state, proto);
73 handler = state->handler;
75 if (cmd == PROTO_CMD_SETUP) {
77 proc = &state->setup_task;
78 state->last_error = -1;
80 if (state->sm == S_TEARDOWN)
83 if (state->setup_task.uloop.pending) {
84 if (state->sm != S_SETUP_ABORT) {
85 uloop_timeout_set(&state->teardown_timeout, 1000);
86 kill(state->setup_task.uloop.pid, SIGTERM);
87 if (state->proto_task.uloop.pending)
88 kill(state->proto_task.uloop.pid, SIGTERM);
89 state->sm = S_SETUP_ABORT;
95 proc = &state->teardown_task;
96 state->sm = S_TEARDOWN;
97 if (state->last_error >= 0) {
98 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
99 envp[j++] = error_buf;
101 uloop_timeout_set(&state->teardown_timeout, 5000);
104 config = blobmsg_format_json(state->config, true);
108 argv[i++] = handler->script_name;
109 argv[i++] = handler->proto.name;
111 argv[i++] = proto->iface->name;
113 if (proto->iface->main_dev.dev)
114 argv[i++] = proto->iface->main_dev.dev->ifname;
118 ret = netifd_start_process(argv, envp, proc);
125 proto_shell_task_finish(struct proto_shell_state *state,
126 struct netifd_process *task)
130 if (task == &state->proto_task)
131 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
134 if (task == &state->proto_task)
135 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN,
140 if (state->setup_task.uloop.pending ||
141 state->proto_task.uloop.pending)
144 uloop_timeout_cancel(&state->teardown_timeout);
146 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
150 if (state->teardown_task.uloop.pending)
153 if (state->proto_task.uloop.pending) {
154 if (!state->proto_task_killed)
155 kill(state->proto_task.uloop.pid, SIGTERM);
159 uloop_timeout_cancel(&state->teardown_timeout);
161 state->proto.proto_event(&state->proto, IFPEV_DOWN);
167 proto_shell_teardown_timeout_cb(struct uloop_timeout *timeout)
169 struct proto_shell_state *state;
171 state = container_of(timeout, struct proto_shell_state, teardown_timeout);
173 netifd_kill_process(&state->setup_task);
174 netifd_kill_process(&state->proto_task);
175 netifd_kill_process(&state->teardown_task);
176 proto_shell_task_finish(state, NULL);
180 proto_shell_setup_cb(struct netifd_process *p, int ret)
182 struct proto_shell_state *state;
184 state = container_of(p, struct proto_shell_state, setup_task);
185 proto_shell_task_finish(state, p);
189 proto_shell_teardown_cb(struct netifd_process *p, int ret)
191 struct proto_shell_state *state;
193 state = container_of(p, struct proto_shell_state, teardown_task);
194 proto_shell_task_finish(state, p);
198 proto_shell_task_cb(struct netifd_process *p, int ret)
200 struct proto_shell_state *state;
202 state = container_of(p, struct proto_shell_state, proto_task);
204 if (state->sm == S_IDLE || state->sm == S_SETUP)
205 state->last_error = WEXITSTATUS(ret);
207 proto_shell_task_finish(state, p);
211 proto_shell_free(struct interface_proto_state *proto)
213 struct proto_shell_state *state;
215 state = container_of(proto, struct proto_shell_state, proto);
221 proto_shell_parse_addr_list(struct interface_ip_settings *ip, struct blob_attr *attr,
222 bool v6, bool external)
224 struct device_addr *addr;
225 struct blob_attr *cur;
228 blobmsg_for_each_attr(cur, attr, rem) {
229 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
230 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur));
234 addr = proto_parse_ip_addr_string(blobmsg_data(cur), v6, v6 ? 32 : 128);
236 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur));
241 addr->flags |= DEVADDR_EXTERNAL;
243 vlist_add(&ip->addr, &addr->node);
248 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
251 struct blob_attr *cur;
254 blobmsg_for_each_attr(cur, attr, rem) {
255 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
256 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
260 interface_ip_add_route(iface, cur, v6);
283 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
284 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
285 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
286 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
287 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
288 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
289 [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
290 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
291 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
292 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
293 [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
294 [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
295 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
296 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
297 [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
298 [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
302 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
304 struct interface_ip_settings *ip;
305 struct blob_attr *cur;
307 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_ADDR_EXT]) != NULL) {
320 addr_ext = blobmsg_get_bool(cur);
325 if (!tb[NOTIFY_IFNAME]) {
326 if (!state->proto.iface->main_dev.dev)
327 return UBUS_STATUS_INVALID_ARGUMENT;
329 if (state->l3_dev.dev)
330 device_remove_user(&state->l3_dev);
332 device_add_user(&state->l3_dev,
333 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), dev_create));
334 state->proto.iface->l3_dev = &state->l3_dev;
335 device_claim(&state->l3_dev);
338 ip = &state->proto.iface->proto_ip;
339 interface_update_start(state->proto.iface);
341 if ((cur = tb[NOTIFY_IPADDR]) != NULL)
342 proto_shell_parse_addr_list(ip, cur, false, addr_ext);
344 if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
345 proto_shell_parse_addr_list(ip, cur, true, addr_ext);
347 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
348 proto_shell_parse_route_list(state->proto.iface, cur, false);
350 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
351 proto_shell_parse_route_list(state->proto.iface, cur, true);
353 if ((cur = tb[NOTIFY_DNS]) != NULL)
354 interface_add_dns_server_list(ip, cur);
356 if ((cur = tb[NOTIFY_DNS_SEARCH]) != NULL)
357 interface_add_dns_search_list(ip, cur);
359 interface_update_complete(state->proto.iface);
361 state->proto.proto_event(&state->proto, IFPEV_UP);
367 fill_string_list(struct blob_attr *attr, char **argv, int max)
369 struct blob_attr *cur;
376 blobmsg_for_each_attr(cur, attr, rem) {
377 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
380 if (!blobmsg_check_attr(cur, NULL))
383 argv[argc++] = blobmsg_data(cur);
394 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
396 static char *argv[64];
397 static char *env[32];
399 if (!tb[NOTIFY_COMMAND])
402 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
405 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
408 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
413 return UBUS_STATUS_INVALID_ARGUMENT;
417 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
419 unsigned int signal = ~0;
421 if (tb[NOTIFY_SIGNAL])
422 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
427 if (state->proto_task.uloop.pending) {
428 state->proto_task_killed = true;
429 kill(state->proto_task.uloop.pid, signal);
436 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
438 struct blob_attr *cur;
443 if (!tb[NOTIFY_ERROR])
444 return UBUS_STATUS_INVALID_ARGUMENT;
446 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
447 if (n_data + 1 == ARRAY_SIZE(data))
450 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
453 if (!blobmsg_check_attr(cur, NULL))
456 data[n_data++] = blobmsg_data(cur);
462 interface_add_error(state->proto.iface, state->handler->proto.name,
463 data[0], (const char **) &data[1], n_data - 1);
468 return UBUS_STATUS_INVALID_ARGUMENT;
472 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
474 state->proto.iface->autostart = false;
479 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
481 if (!tb[NOTIFY_AVAILABLE])
482 return UBUS_STATUS_INVALID_ARGUMENT;
484 interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
489 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
491 struct proto_shell_state *state;
492 struct blob_attr *tb[__NOTIFY_LAST];
494 state = container_of(proto, struct proto_shell_state, proto);
496 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
497 if (!tb[NOTIFY_ACTION])
498 return UBUS_STATUS_INVALID_ARGUMENT;
500 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
502 return proto_shell_update_link(state, tb);
504 return proto_shell_run_command(state, tb);
506 return proto_shell_kill_command(state, tb);
508 return proto_shell_notify_error(state, tb);
510 return proto_shell_block_restart(state, tb);
512 return proto_shell_set_available(state, tb);
514 return UBUS_STATUS_INVALID_ARGUMENT;
518 static struct interface_proto_state *
519 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
520 struct blob_attr *attr)
522 struct proto_shell_state *state;
524 state = calloc(1, sizeof(*state));
525 state->config = malloc(blob_pad_len(attr));
529 memcpy(state->config, attr, blob_pad_len(attr));
530 state->proto.free = proto_shell_free;
531 state->proto.notify = proto_shell_notify;
532 state->proto.cb = proto_shell_handler;
533 state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
534 state->setup_task.cb = proto_shell_setup_cb;
535 state->setup_task.dir_fd = proto_fd.fd;
536 state->setup_task.log_prefix = iface->name;
537 state->teardown_task.cb = proto_shell_teardown_cb;
538 state->teardown_task.dir_fd = proto_fd.fd;
539 state->teardown_task.log_prefix = iface->name;
540 state->proto_task.cb = proto_shell_task_cb;
541 state->proto_task.dir_fd = proto_fd.fd;
542 state->proto_task.log_prefix = iface->name;
543 state->handler = container_of(h, struct proto_shell_handler, proto);
545 return &state->proto;
553 check_type(json_object *obj, json_type type)
558 if (json_object_get_type(obj) != type)
564 static inline json_object *
565 get_field(json_object *obj, const char *name, json_type type)
567 return check_type(json_object_object_get(obj, name), type);
571 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
573 struct blobmsg_policy *attrs;
574 char *str_buf, *str_cur;
578 config->n_params = json_object_array_length(obj);
579 attrs = calloc(1, sizeof(*attrs) * config->n_params);
583 config->params = attrs;
584 for (i = 0; i < config->n_params; i++) {
585 json_object *cur, *name, *type;
587 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
591 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
595 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
599 attrs[i].name = json_object_get_string(name);
600 attrs[i].type = json_object_get_int(type);
601 if (attrs[i].type > BLOBMSG_TYPE_LAST)
604 str_len += strlen(attrs[i].name) + 1;
607 str_buf = malloc(str_len);
612 for (i = 0; i < config->n_params; i++) {
613 const char *name = attrs[i].name;
615 attrs[i].name = str_cur;
616 str_cur += sprintf(str_cur, "%s", name) + 1;
623 config->n_params = 0;
628 proto_shell_add_handler(const char *script, json_object *obj)
630 struct proto_shell_handler *handler;
631 struct proto_handler *proto;
632 json_object *config, *tmp;
636 if (!check_type(obj, json_type_object))
639 tmp = get_field(obj, "name", json_type_string);
643 name = json_object_get_string(tmp);
645 handler = calloc(1, sizeof(*handler) +
651 strcpy(handler->script_name, script);
653 str = handler->script_name + strlen(handler->script_name) + 1;
656 proto = &handler->proto;
658 proto->config_params = &handler->config;
659 proto->attach = proto_shell_attach;
661 tmp = get_field(obj, "no-device", json_type_boolean);
662 if (tmp && json_object_get_boolean(tmp))
663 handler->proto.flags |= PROTO_FLAG_NODEV;
665 tmp = get_field(obj, "available", json_type_boolean);
666 if (tmp && json_object_get_boolean(tmp))
667 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
669 config = get_field(obj, "config", json_type_array);
671 handler->config_buf = proto_shell_parse_config(&handler->config, config);
673 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
674 add_proto_handler(proto);
677 static void proto_shell_add_script(const char *name)
679 struct json_tokener *tok = NULL;
681 static char buf[512];
686 #define DUMP_SUFFIX " '' dump"
688 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
689 sprintf(cmd, "%s" DUMP_SUFFIX, name);
696 start = fgets(buf, sizeof(buf), f);
703 tok = json_tokener_new();
705 obj = json_tokener_parse_ex(tok, start, len);
706 if (!is_error(obj)) {
707 proto_shell_add_handler(name, obj);
708 json_object_put(obj);
709 json_tokener_free(tok);
711 } else if (start[len - 1] == '\n') {
712 json_tokener_free(tok);
715 } while (!feof(f) && !ferror(f));
718 json_tokener_free(tok);
723 static void __init proto_shell_init(void)
729 main_fd = open(".", O_RDONLY | O_DIRECTORY);
733 if (chdir(main_path)) {
734 perror("chdir(main path)");
738 if (chdir("./proto"))
741 proto_fd.fd = open(".", O_RDONLY | O_DIRECTORY);
745 netifd_fd_add(&proto_fd);
746 glob("./*.sh", 0, NULL, &g);
747 for (i = 0; i < g.gl_pathc; i++)
748 proto_shell_add_script(g.gl_pathv[i]);