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;
23 struct proto_shell_handler {
24 struct list_head list;
25 struct proto_handler proto;
26 struct config_param_list config;
31 struct proto_shell_state {
32 struct interface_proto_state proto;
33 struct proto_shell_handler *handler;
34 struct blob_attr *config;
36 struct device_user l3_dev;
38 struct uloop_timeout setup_timeout;
39 struct netifd_process setup_task;
40 struct netifd_process teardown_task;
41 bool teardown_pending;
42 bool teardown_wait_task;
44 struct netifd_process proto_task;
49 proto_shell_handler(struct interface_proto_state *proto,
50 enum interface_proto_cmd cmd, bool force)
52 struct proto_shell_state *state;
53 struct proto_shell_handler *handler;
54 struct netifd_process *proc;
55 static char error_buf[32];
60 int ret, i = 0, j = 0;
62 state = container_of(proto, struct proto_shell_state, proto);
63 handler = state->handler;
65 if (cmd == PROTO_CMD_SETUP) {
67 proc = &state->setup_task;
68 state->last_error = -1;
71 proc = &state->teardown_task;
72 if (state->setup_task.uloop.pending && !state->teardown_wait_task) {
73 uloop_timeout_set(&state->setup_timeout, 1000);
74 kill(state->setup_task.uloop.pid, SIGTERM);
75 state->teardown_pending = true;
78 if (state->last_error >= 0) {
79 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
80 envp[j++] = error_buf;
84 config = blobmsg_format_json(state->config, true);
88 argv[i++] = handler->script_name;
89 argv[i++] = handler->proto.name;
91 argv[i++] = proto->iface->name;
93 if (proto->iface->main_dev.dev)
94 argv[i++] = proto->iface->main_dev.dev->ifname;
98 ret = netifd_start_process(argv, envp, proc);
105 proto_shell_setup_timeout_cb(struct uloop_timeout *timeout)
107 struct proto_shell_state *state;
109 state = container_of(timeout, struct proto_shell_state, setup_timeout);
110 kill(state->setup_task.uloop.pid, SIGKILL);
114 proto_shell_setup_cb(struct netifd_process *p, int ret)
116 struct proto_shell_state *state;
118 state = container_of(p, struct proto_shell_state, setup_task);
119 uloop_timeout_cancel(&state->setup_timeout);
120 if (state->teardown_pending) {
121 state->teardown_pending = false;
122 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
127 proto_shell_teardown_cb(struct netifd_process *p, int ret)
129 struct proto_shell_state *state;
131 state = container_of(p, struct proto_shell_state, teardown_task);
133 if (state->teardown_wait_task)
136 netifd_kill_process(&state->proto_task);
137 state->proto.proto_event(&state->proto, IFPEV_DOWN);
141 proto_shell_task_cb(struct netifd_process *p, int ret)
143 struct proto_shell_state *state;
144 bool teardown_wait_task;
146 state = container_of(p, struct proto_shell_state, proto_task);
148 teardown_wait_task = state->teardown_wait_task;
149 state->teardown_wait_task = false;
150 if (state->teardown_pending || state->teardown_task.uloop.pending)
153 if (teardown_wait_task) {
154 proto_shell_teardown_cb(&state->teardown_task, 0);
158 state->last_error = WEXITSTATUS(ret);
159 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
160 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
164 proto_shell_free(struct interface_proto_state *proto)
166 struct proto_shell_state *state;
168 state = container_of(proto, struct proto_shell_state, proto);
174 proto_shell_parse_addr_list(struct interface_ip_settings *ip, struct blob_attr *attr,
175 bool v6, bool external)
177 struct device_addr *addr;
178 struct blob_attr *cur;
181 blobmsg_for_each_attr(cur, attr, rem) {
182 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
183 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur));
187 addr = proto_parse_ip_addr_string(blobmsg_data(cur), v6, v6 ? 32 : 128);
189 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur));
194 addr->flags |= DEVADDR_EXTERNAL;
196 vlist_add(&ip->addr, &addr->node);
201 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
204 struct blob_attr *cur;
207 blobmsg_for_each_attr(cur, attr, rem) {
208 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
209 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
213 interface_ip_add_route(iface, cur, v6);
235 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
236 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
237 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
238 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
239 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
240 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
241 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
242 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
243 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
244 [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
245 [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
246 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
247 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
248 [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
249 [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
253 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
255 struct interface_ip_settings *ip;
256 struct blob_attr *cur;
258 bool addr_ext = false;
261 if (!tb[NOTIFY_LINK_UP])
262 return UBUS_STATUS_INVALID_ARGUMENT;
264 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
266 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
270 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
271 addr_ext = blobmsg_get_bool(cur);
276 if (!tb[NOTIFY_IFNAME]) {
277 if (!state->proto.iface->main_dev.dev)
278 return UBUS_STATUS_INVALID_ARGUMENT;
280 if (state->l3_dev.dev)
281 device_remove_user(&state->l3_dev);
283 device_add_user(&state->l3_dev,
284 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), dev_create));
285 state->proto.iface->l3_dev = &state->l3_dev;
286 device_claim(&state->l3_dev);
289 ip = &state->proto.iface->proto_ip;
290 interface_update_start(state->proto.iface);
292 if ((cur = tb[NOTIFY_IPADDR]) != NULL)
293 proto_shell_parse_addr_list(ip, cur, false, addr_ext);
295 if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
296 proto_shell_parse_addr_list(ip, cur, true, addr_ext);
298 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
299 proto_shell_parse_route_list(state->proto.iface, cur, false);
301 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
302 proto_shell_parse_route_list(state->proto.iface, cur, true);
304 if ((cur = tb[NOTIFY_DNS]) != NULL)
305 interface_add_dns_server_list(ip, cur);
307 if ((cur = tb[NOTIFY_DNS_SEARCH]) != NULL)
308 interface_add_dns_search_list(ip, cur);
310 interface_update_complete(state->proto.iface);
312 state->proto.proto_event(&state->proto, IFPEV_UP);
318 fill_string_list(struct blob_attr *attr, char **argv, int max)
320 struct blob_attr *cur;
327 blobmsg_for_each_attr(cur, attr, rem) {
328 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
331 if (!blobmsg_check_attr(cur, NULL))
334 argv[argc++] = blobmsg_data(cur);
345 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
347 static char *argv[64];
348 static char *env[32];
350 if (!tb[NOTIFY_COMMAND])
353 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
356 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
359 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
364 return UBUS_STATUS_INVALID_ARGUMENT;
368 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
370 unsigned int signal = ~0;
372 if (tb[NOTIFY_SIGNAL])
373 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
378 if (state->proto_task.uloop.pending) {
379 kill(state->proto_task.uloop.pid, signal);
380 state->teardown_wait_task = true;
387 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
389 struct blob_attr *cur;
394 if (!tb[NOTIFY_ERROR])
395 return UBUS_STATUS_INVALID_ARGUMENT;
397 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
398 if (n_data + 1 == ARRAY_SIZE(data))
401 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
404 if (!blobmsg_check_attr(cur, NULL))
407 data[n_data++] = blobmsg_data(cur);
413 interface_add_error(state->proto.iface, state->handler->proto.name,
414 data[0], (const char **) &data[1], n_data - 1);
419 return UBUS_STATUS_INVALID_ARGUMENT;
423 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
425 state->proto.iface->autostart = false;
430 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
432 struct proto_shell_state *state;
433 struct blob_attr *tb[__NOTIFY_LAST];
435 state = container_of(proto, struct proto_shell_state, proto);
437 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
438 if (!tb[NOTIFY_ACTION])
439 return UBUS_STATUS_INVALID_ARGUMENT;
441 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
443 return proto_shell_update_link(state, tb);
445 return proto_shell_run_command(state, tb);
447 return proto_shell_kill_command(state, tb);
449 return proto_shell_notify_error(state, tb);
451 return proto_shell_block_restart(state, tb);
453 return UBUS_STATUS_INVALID_ARGUMENT;
457 static struct interface_proto_state *
458 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
459 struct blob_attr *attr)
461 struct proto_shell_state *state;
463 state = calloc(1, sizeof(*state));
464 state->config = malloc(blob_pad_len(attr));
468 memcpy(state->config, attr, blob_pad_len(attr));
469 state->proto.free = proto_shell_free;
470 state->proto.notify = proto_shell_notify;
471 state->proto.cb = proto_shell_handler;
472 state->setup_timeout.cb = proto_shell_setup_timeout_cb;
473 state->setup_task.cb = proto_shell_setup_cb;
474 state->setup_task.dir_fd = proto_fd.fd;
475 state->setup_task.log_prefix = iface->name;
476 state->teardown_task.cb = proto_shell_teardown_cb;
477 state->teardown_task.dir_fd = proto_fd.fd;
478 state->teardown_task.log_prefix = iface->name;
479 state->proto_task.cb = proto_shell_task_cb;
480 state->proto_task.dir_fd = proto_fd.fd;
481 state->proto_task.log_prefix = iface->name;
482 state->handler = container_of(h, struct proto_shell_handler, proto);
484 return &state->proto;
492 check_type(json_object *obj, json_type type)
497 if (json_object_get_type(obj) != type)
503 static inline json_object *
504 get_field(json_object *obj, const char *name, json_type type)
506 return check_type(json_object_object_get(obj, name), type);
510 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
512 struct blobmsg_policy *attrs;
513 char *str_buf, *str_cur;
517 config->n_params = json_object_array_length(obj);
518 attrs = calloc(1, sizeof(*attrs) * config->n_params);
522 config->params = attrs;
523 for (i = 0; i < config->n_params; i++) {
524 json_object *cur, *name, *type;
526 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
530 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
534 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
538 attrs[i].name = json_object_get_string(name);
539 attrs[i].type = json_object_get_int(type);
540 if (attrs[i].type > BLOBMSG_TYPE_LAST)
543 str_len += strlen(attrs[i].name) + 1;
546 str_buf = malloc(str_len);
551 for (i = 0; i < config->n_params; i++) {
552 const char *name = attrs[i].name;
554 attrs[i].name = str_cur;
555 str_cur += sprintf(str_cur, "%s", name) + 1;
562 config->n_params = 0;
567 proto_shell_add_handler(const char *script, json_object *obj)
569 struct proto_shell_handler *handler;
570 struct proto_handler *proto;
571 json_object *config, *tmp;
575 if (!check_type(obj, json_type_object))
578 tmp = get_field(obj, "name", json_type_string);
582 name = json_object_get_string(tmp);
584 handler = calloc(1, sizeof(*handler) +
590 strcpy(handler->script_name, script);
592 str = handler->script_name + strlen(handler->script_name) + 1;
595 proto = &handler->proto;
597 proto->config_params = &handler->config;
598 proto->attach = proto_shell_attach;
600 tmp = get_field(obj, "no-device", json_type_boolean);
601 if (tmp && json_object_get_boolean(tmp))
602 handler->proto.flags |= PROTO_FLAG_NODEV;
604 config = get_field(obj, "config", json_type_array);
606 handler->config_buf = proto_shell_parse_config(&handler->config, config);
608 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
609 add_proto_handler(proto);
612 static void proto_shell_add_script(const char *name)
614 struct json_tokener *tok = NULL;
616 static char buf[512];
621 #define DUMP_SUFFIX " '' dump"
623 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
624 sprintf(cmd, "%s" DUMP_SUFFIX, name);
631 start = fgets(buf, sizeof(buf), f);
638 tok = json_tokener_new();
640 obj = json_tokener_parse_ex(tok, start, len);
641 if (!is_error(obj)) {
642 proto_shell_add_handler(name, obj);
643 json_object_put(obj);
644 json_tokener_free(tok);
646 } else if (start[len - 1] == '\n') {
647 json_tokener_free(tok);
650 } while (!feof(f) && !ferror(f));
653 json_tokener_free(tok);
658 static void __init proto_shell_init(void)
664 main_fd = open(".", O_RDONLY | O_DIRECTORY);
668 if (chdir(main_path)) {
669 perror("chdir(main path)");
673 if (chdir("./proto"))
676 proto_fd.fd = open(".", O_RDONLY | O_DIRECTORY);
680 netifd_fd_add(&proto_fd);
681 glob("./*.sh", 0, NULL, &g);
682 for (i = 0; i < g.gl_pathc; i++)
683 proto_shell_add_script(g.gl_pathv[i]);