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"
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 uloop_process setup_task;
40 struct uloop_process teardown_task;
41 bool teardown_pending;
42 bool teardown_wait_task;
44 struct uloop_process proto_task;
48 kill_process(struct uloop_process *proc)
53 kill(proc->pid, SIGTERM);
54 uloop_process_delete(proc);
58 start_process(const char **argv, struct uloop_process *proc)
64 if ((pid = fork()) < 0)
69 execvp(argv[0], (char **) argv);
77 uloop_process_add(proc);
83 proto_shell_handler(struct interface_proto_state *proto,
84 enum interface_proto_cmd cmd, bool force)
86 struct proto_shell_state *state;
87 struct proto_shell_handler *handler;
88 struct uloop_process *proc;
94 state = container_of(proto, struct proto_shell_state, proto);
95 handler = state->handler;
97 if (cmd == PROTO_CMD_SETUP) {
99 proc = &state->setup_task;
102 proc = &state->teardown_task;
103 if (state->setup_task.pending && !state->teardown_wait_task) {
104 uloop_timeout_set(&state->setup_timeout, 1000);
105 kill(state->setup_task.pid, SIGTERM);
106 state->teardown_pending = true;
111 config = blobmsg_format_json(state->config, true);
115 argv[i++] = handler->script_name;
116 argv[i++] = handler->proto.name;
118 argv[i++] = proto->iface->name;
120 if (proto->iface->main_dev.dev)
121 argv[i++] = proto->iface->main_dev.dev->ifname;
124 ret = start_process(argv, proc);
131 proto_shell_setup_timeout_cb(struct uloop_timeout *timeout)
133 struct proto_shell_state *state;
135 state = container_of(timeout, struct proto_shell_state, setup_timeout);
136 kill(state->setup_task.pid, SIGKILL);
140 proto_shell_setup_cb(struct uloop_process *p, int ret)
142 struct proto_shell_state *state;
144 state = container_of(p, struct proto_shell_state, setup_task);
145 uloop_timeout_cancel(&state->setup_timeout);
146 if (state->teardown_pending) {
147 state->teardown_pending = false;
148 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
153 proto_shell_teardown_cb(struct uloop_process *p, int ret)
155 struct proto_shell_state *state;
157 state = container_of(p, struct proto_shell_state, teardown_task);
159 if (state->teardown_wait_task)
162 kill_process(&state->proto_task);
164 if (state->l3_dev.dev)
165 device_remove_user(&state->l3_dev);
167 state->proto.proto_event(&state->proto, IFPEV_DOWN);
171 proto_shell_task_cb(struct uloop_process *p, int ret)
173 struct proto_shell_state *state;
174 bool teardown_wait_task;
176 state = container_of(p, struct proto_shell_state, proto_task);
178 teardown_wait_task = state->teardown_wait_task;
179 state->teardown_wait_task = false;
180 if (state->teardown_pending || state->teardown_task.pending)
183 if (teardown_wait_task) {
184 proto_shell_teardown_cb(&state->teardown_task, 0);
188 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
189 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
193 proto_shell_free(struct interface_proto_state *proto)
195 struct proto_shell_state *state;
197 state = container_of(proto, struct proto_shell_state, proto);
203 proto_shell_parse_addr_list(struct interface *iface, struct blob_attr *attr,
204 bool v6, bool external)
206 struct device_addr *addr;
207 struct blob_attr *cur;
210 blobmsg_for_each_attr(cur, attr, rem) {
211 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
212 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur));
216 addr = proto_parse_ip_addr_string(blobmsg_data(cur), v6, v6 ? 32 : 128);
218 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur));
223 addr->flags |= DEVADDR_EXTERNAL;
225 vlist_add(&iface->proto_addr, &addr->node);
237 static const struct blobmsg_policy route_attr[__ROUTE_LAST] = {
238 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
239 [ROUTE_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_INT32 },
240 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
241 [ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
245 parse_route(struct interface *iface, struct blob_attr *attr, bool v6)
247 struct blob_attr *tb[__ROUTE_LAST], *cur;
248 struct device_route *route;
249 int af = v6 ? AF_INET6 : AF_INET;
251 blobmsg_parse(route_attr, __ROUTE_LAST, tb, blobmsg_data(attr), blobmsg_data_len(attr));
253 if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
256 route = calloc(1, sizeof(*route));
260 route->mask = v6 ? 128 : 32;
261 if ((cur = tb[ROUTE_MASK]) != NULL) {
262 route->mask = blobmsg_get_u32(cur);
263 if (route->mask > v6 ? 128 : 32)
267 if ((cur = tb[ROUTE_TARGET]) != NULL) {
268 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
269 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
274 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
275 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
276 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
281 if ((cur = tb[ROUTE_DEVICE]) != NULL)
282 route->device = device_get(blobmsg_data(cur), true);
284 vlist_add(&iface->proto_route, &route->node);
292 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
295 struct blob_attr *cur;
298 blobmsg_for_each_attr(cur, attr, rem) {
299 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
300 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
304 parse_route(iface, cur, v6);
324 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
325 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
326 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
327 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
328 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
329 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
330 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
331 [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
332 [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
333 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
334 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
335 [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
339 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
341 struct blob_attr *cur;
342 bool addr_ext = false;
345 if (!tb[NOTIFY_LINK_UP])
346 return UBUS_STATUS_INVALID_ARGUMENT;
348 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
350 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
354 if (!tb[NOTIFY_IFNAME]) {
355 if (!state->iface->main_dev.dev)
356 return UBUS_STATUS_INVALID_ARGUMENT;
357 } else if (!state->l3_dev.dev) {
358 device_add_user(&state->l3_dev,
359 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
360 device_claim(&state->l3_dev);
361 state->proto.iface->l3_dev = &state->l3_dev;
364 interface_ip_update_start(state->proto.iface);
366 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL)
367 addr_ext = blobmsg_get_bool(cur);
369 if ((cur = tb[NOTIFY_IPADDR]) != NULL)
370 proto_shell_parse_addr_list(state->proto.iface, cur, false, addr_ext);
372 if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
373 proto_shell_parse_addr_list(state->proto.iface, cur, true, addr_ext);
375 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
376 proto_shell_parse_route_list(state->proto.iface, cur, false);
378 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
379 proto_shell_parse_route_list(state->proto.iface, cur, true);
381 if ((cur = tb[NOTIFY_DNS]) != NULL)
382 interface_add_dns_server_list(state->proto.iface, cur);
384 interface_ip_update_complete(state->proto.iface);
386 state->proto.proto_event(&state->proto, IFPEV_UP);
392 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
394 struct blob_attr *cur;
399 if (!tb[NOTIFY_COMMAND])
402 blobmsg_for_each_attr(cur, tb[NOTIFY_COMMAND], rem) {
403 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
406 if (!blobmsg_check_attr(cur, NULL))
409 argv[argc++] = blobmsg_data(cur);
410 if (argc == ARRAY_SIZE(argv) - 1)
414 start_process((const char **) argv, &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.pending) {
434 kill(state->proto_task.pid, signal);
435 state->teardown_wait_task = true;
442 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
444 struct proto_shell_state *state;
445 struct blob_attr *tb[__NOTIFY_LAST];
447 state = container_of(proto, struct proto_shell_state, proto);
449 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
450 if (!tb[NOTIFY_ACTION])
451 return UBUS_STATUS_INVALID_ARGUMENT;
453 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
455 return proto_shell_update_link(state, tb);
457 return proto_shell_run_command(state, tb);
459 return proto_shell_kill_command(state, tb);
461 return UBUS_STATUS_INVALID_ARGUMENT;
465 struct interface_proto_state *
466 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
467 struct blob_attr *attr)
469 struct proto_shell_state *state;
471 state = calloc(1, sizeof(*state));
472 state->config = malloc(blob_pad_len(attr));
476 memcpy(state->config, attr, blob_pad_len(attr));
477 state->proto.free = proto_shell_free;
478 state->proto.notify = proto_shell_notify;
479 state->proto.cb = proto_shell_handler;
480 state->setup_timeout.cb = proto_shell_setup_timeout_cb;
481 state->setup_task.cb = proto_shell_setup_cb;
482 state->teardown_task.cb = proto_shell_teardown_cb;
483 state->proto_task.cb = proto_shell_task_cb;
484 state->handler = container_of(h, struct proto_shell_handler, proto);
486 return &state->proto;
494 check_type(json_object *obj, json_type type)
499 if (json_object_get_type(obj) != type)
505 static inline json_object *
506 get_field(json_object *obj, const char *name, json_type type)
508 return check_type(json_object_object_get(obj, name), type);
512 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
514 struct blobmsg_policy *attrs;
515 char *str_buf, *str_cur;
519 config->n_params = json_object_array_length(obj);
520 attrs = calloc(1, sizeof(*attrs) * config->n_params);
524 config->params = attrs;
525 for (i = 0; i < config->n_params; i++) {
526 json_object *cur, *name, *type;
528 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
532 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
536 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
540 attrs[i].name = json_object_get_string(name);
541 attrs[i].type = json_object_get_int(type);
542 if (attrs[i].type > BLOBMSG_TYPE_LAST)
545 str_len += strlen(attrs[i].name) + 1;
548 str_buf = malloc(str_len);
553 for (i = 0; i < config->n_params; i++) {
554 const char *name = attrs[i].name;
556 attrs[i].name = str_cur;
557 str_cur += sprintf(str_cur, "%s", name) + 1;
564 config->n_params = 0;
569 proto_shell_add_handler(const char *script, json_object *obj)
571 struct proto_shell_handler *handler;
572 struct proto_handler *proto;
573 json_object *config, *tmp;
577 if (!check_type(obj, json_type_object))
580 tmp = get_field(obj, "name", json_type_string);
584 name = json_object_get_string(tmp);
586 handler = calloc(1, sizeof(*handler) +
592 strcpy(handler->script_name, script);
594 str = handler->script_name + strlen(handler->script_name) + 1;
597 proto = &handler->proto;
599 proto->config_params = &handler->config;
600 proto->attach = proto_shell_attach;
602 tmp = get_field(obj, "no-device", json_type_boolean);
603 if (tmp && json_object_get_boolean(tmp))
604 handler->proto.flags |= PROTO_FLAG_NODEV;
606 config = get_field(obj, "config", json_type_array);
608 handler->config_buf = proto_shell_parse_config(&handler->config, config);
610 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
611 add_proto_handler(proto);
614 static void proto_shell_add_script(const char *name)
616 struct json_tokener *tok = NULL;
618 static char buf[512];
619 char *start, *end, *cmd;
623 #define DUMP_SUFFIX " '' dump"
625 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
626 sprintf(cmd, "%s" DUMP_SUFFIX, name);
633 buflen = fread(buf, 1, sizeof(buf) - 1, f);
640 end = memchr(start, '\n', len);
645 tok = json_tokener_new();
647 obj = json_tokener_parse_ex(tok, start, len);
648 if (!is_error(obj)) {
649 proto_shell_add_handler(name, obj);
650 json_object_put(obj);
651 json_tokener_free(tok);
657 len = buflen - (start - buf);
660 } while (!feof(f) && !ferror(f));
663 json_tokener_free(tok);
668 void __init proto_shell_init(void)
674 main_fd = open(".", O_RDONLY | O_DIRECTORY);
678 if (chdir(main_path)) {
679 perror("chdir(main path)");
683 if (chdir("./proto"))
686 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
690 glob("./*.sh", 0, NULL, &g);
691 for (i = 0; i < g.gl_pathc; i++)
692 proto_shell_add_script(g.gl_pathv[i]);