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_STRING },
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);
325 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
326 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
327 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
328 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
329 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
330 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
331 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
332 [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
333 [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
334 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
335 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
336 [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
337 [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
341 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
343 struct blob_attr *cur;
344 bool addr_ext = false;
347 if (!tb[NOTIFY_LINK_UP])
348 return UBUS_STATUS_INVALID_ARGUMENT;
350 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
352 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
356 if (!tb[NOTIFY_IFNAME]) {
357 if (!state->proto.iface->main_dev.dev)
358 return UBUS_STATUS_INVALID_ARGUMENT;
359 } else if (!state->l3_dev.dev) {
360 device_add_user(&state->l3_dev,
361 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
362 device_claim(&state->l3_dev);
363 state->proto.iface->l3_dev = &state->l3_dev;
366 interface_ip_update_start(state->proto.iface);
368 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL)
369 addr_ext = blobmsg_get_bool(cur);
371 if ((cur = tb[NOTIFY_IPADDR]) != NULL)
372 proto_shell_parse_addr_list(state->proto.iface, cur, false, addr_ext);
374 if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
375 proto_shell_parse_addr_list(state->proto.iface, cur, true, addr_ext);
377 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
378 proto_shell_parse_route_list(state->proto.iface, cur, false);
380 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
381 proto_shell_parse_route_list(state->proto.iface, cur, true);
383 if ((cur = tb[NOTIFY_DNS]) != NULL)
384 interface_add_dns_server_list(state->proto.iface, cur);
386 if ((cur = tb[NOTIFY_DNS_SEARCH]) != NULL)
387 interface_add_dns_search_list(state->proto.iface, cur);
389 interface_ip_update_complete(state->proto.iface);
391 state->proto.proto_event(&state->proto, IFPEV_UP);
397 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
399 struct blob_attr *cur;
404 if (!tb[NOTIFY_COMMAND])
407 blobmsg_for_each_attr(cur, tb[NOTIFY_COMMAND], rem) {
408 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
411 if (!blobmsg_check_attr(cur, NULL))
414 argv[argc++] = blobmsg_data(cur);
415 if (argc == ARRAY_SIZE(argv) - 1)
419 start_process((const char **) argv, &state->proto_task);
424 return UBUS_STATUS_INVALID_ARGUMENT;
428 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
430 unsigned int signal = ~0;
432 if (tb[NOTIFY_SIGNAL])
433 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
438 if (state->proto_task.pending) {
439 kill(state->proto_task.pid, signal);
440 state->teardown_wait_task = true;
447 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
449 struct proto_shell_state *state;
450 struct blob_attr *tb[__NOTIFY_LAST];
452 state = container_of(proto, struct proto_shell_state, proto);
454 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
455 if (!tb[NOTIFY_ACTION])
456 return UBUS_STATUS_INVALID_ARGUMENT;
458 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
460 return proto_shell_update_link(state, tb);
462 return proto_shell_run_command(state, tb);
464 return proto_shell_kill_command(state, tb);
466 return UBUS_STATUS_INVALID_ARGUMENT;
470 struct interface_proto_state *
471 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
472 struct blob_attr *attr)
474 struct proto_shell_state *state;
476 state = calloc(1, sizeof(*state));
477 state->config = malloc(blob_pad_len(attr));
481 memcpy(state->config, attr, blob_pad_len(attr));
482 state->proto.free = proto_shell_free;
483 state->proto.notify = proto_shell_notify;
484 state->proto.cb = proto_shell_handler;
485 state->setup_timeout.cb = proto_shell_setup_timeout_cb;
486 state->setup_task.cb = proto_shell_setup_cb;
487 state->teardown_task.cb = proto_shell_teardown_cb;
488 state->proto_task.cb = proto_shell_task_cb;
489 state->handler = container_of(h, struct proto_shell_handler, proto);
491 return &state->proto;
499 check_type(json_object *obj, json_type type)
504 if (json_object_get_type(obj) != type)
510 static inline json_object *
511 get_field(json_object *obj, const char *name, json_type type)
513 return check_type(json_object_object_get(obj, name), type);
517 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
519 struct blobmsg_policy *attrs;
520 char *str_buf, *str_cur;
524 config->n_params = json_object_array_length(obj);
525 attrs = calloc(1, sizeof(*attrs) * config->n_params);
529 config->params = attrs;
530 for (i = 0; i < config->n_params; i++) {
531 json_object *cur, *name, *type;
533 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
537 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
541 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
545 attrs[i].name = json_object_get_string(name);
546 attrs[i].type = json_object_get_int(type);
547 if (attrs[i].type > BLOBMSG_TYPE_LAST)
550 str_len += strlen(attrs[i].name) + 1;
553 str_buf = malloc(str_len);
558 for (i = 0; i < config->n_params; i++) {
559 const char *name = attrs[i].name;
561 attrs[i].name = str_cur;
562 str_cur += sprintf(str_cur, "%s", name) + 1;
569 config->n_params = 0;
574 proto_shell_add_handler(const char *script, json_object *obj)
576 struct proto_shell_handler *handler;
577 struct proto_handler *proto;
578 json_object *config, *tmp;
582 if (!check_type(obj, json_type_object))
585 tmp = get_field(obj, "name", json_type_string);
589 name = json_object_get_string(tmp);
591 handler = calloc(1, sizeof(*handler) +
597 strcpy(handler->script_name, script);
599 str = handler->script_name + strlen(handler->script_name) + 1;
602 proto = &handler->proto;
604 proto->config_params = &handler->config;
605 proto->attach = proto_shell_attach;
607 tmp = get_field(obj, "no-device", json_type_boolean);
608 if (tmp && json_object_get_boolean(tmp))
609 handler->proto.flags |= PROTO_FLAG_NODEV;
611 config = get_field(obj, "config", json_type_array);
613 handler->config_buf = proto_shell_parse_config(&handler->config, config);
615 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
616 add_proto_handler(proto);
619 static void proto_shell_add_script(const char *name)
621 struct json_tokener *tok = NULL;
623 static char buf[512];
624 char *start, *end, *cmd;
628 #define DUMP_SUFFIX " '' dump"
630 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
631 sprintf(cmd, "%s" DUMP_SUFFIX, name);
638 buflen = fread(buf, 1, sizeof(buf) - 1, f);
645 end = memchr(start, '\n', len);
650 tok = json_tokener_new();
652 obj = json_tokener_parse_ex(tok, start, len);
653 if (!is_error(obj)) {
654 proto_shell_add_handler(name, obj);
655 json_object_put(obj);
656 json_tokener_free(tok);
662 len = buflen - (start - buf);
665 } while (!feof(f) && !ferror(f));
668 json_tokener_free(tok);
673 void __init proto_shell_init(void)
679 main_fd = open(".", O_RDONLY | O_DIRECTORY);
683 if (chdir(main_path)) {
684 perror("chdir(main path)");
688 if (chdir("./proto"))
691 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
695 glob("./*.sh", 0, NULL, &g);
696 for (i = 0; i < g.gl_pathc; i++)
697 proto_shell_add_script(g.gl_pathv[i]);