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;
43 struct uloop_process proto_task;
47 kill_process(struct uloop_process *proc)
52 kill(proc->pid, SIGTERM);
53 uloop_process_delete(proc);
57 start_process(const char **argv, struct uloop_process *proc)
63 if ((pid = fork()) < 0)
68 execvp(argv[0], (char **) argv);
76 uloop_process_add(proc);
82 proto_shell_handler(struct interface_proto_state *proto,
83 enum interface_proto_cmd cmd, bool force)
85 struct proto_shell_state *state;
86 struct proto_shell_handler *handler;
87 struct uloop_process *proc;
93 state = container_of(proto, struct proto_shell_state, proto);
94 handler = state->handler;
96 if (cmd == PROTO_CMD_SETUP) {
98 proc = &state->setup_task;
101 proc = &state->teardown_task;
102 if (state->setup_task.pending) {
103 uloop_timeout_set(&state->setup_timeout, 1000);
104 kill(state->setup_task.pid, SIGINT);
105 state->teardown_pending = true;
110 config = blobmsg_format_json(state->config, true);
114 argv[i++] = handler->script_name;
115 argv[i++] = handler->proto.name;
117 argv[i++] = proto->iface->name;
119 if (proto->iface->main_dev.dev)
120 argv[i++] = proto->iface->main_dev.dev->ifname;
123 ret = start_process(argv, proc);
130 proto_shell_setup_timeout_cb(struct uloop_timeout *timeout)
132 struct proto_shell_state *state;
134 state = container_of(timeout, struct proto_shell_state, setup_timeout);
135 kill(state->setup_task.pid, SIGKILL);
139 proto_shell_setup_cb(struct uloop_process *p, int ret)
141 struct proto_shell_state *state;
143 state = container_of(p, struct proto_shell_state, setup_task);
144 uloop_timeout_cancel(&state->setup_timeout);
145 if (state->teardown_pending) {
146 state->teardown_pending = false;
147 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
152 proto_shell_teardown_cb(struct uloop_process *p, int ret)
154 struct proto_shell_state *state;
156 state = container_of(p, struct proto_shell_state, teardown_task);
158 kill_process(&state->proto_task);
160 if (state->l3_dev.dev)
161 device_remove_user(&state->l3_dev);
163 state->proto.proto_event(&state->proto, IFPEV_DOWN);
167 proto_shell_task_cb(struct uloop_process *p, int ret)
169 struct proto_shell_state *state;
171 state = container_of(p, struct proto_shell_state, proto_task);
172 if (state->teardown_pending || state->teardown_task.pending)
175 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
176 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
180 proto_shell_free(struct interface_proto_state *proto)
182 struct proto_shell_state *state;
184 state = container_of(proto, struct proto_shell_state, proto);
190 proto_shell_parse_addr_list(struct interface *iface, struct blob_attr *attr,
191 bool v6, bool external)
193 struct device_addr *addr;
194 struct blob_attr *cur;
197 blobmsg_for_each_attr(cur, attr, rem) {
198 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
199 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur));
203 addr = proto_parse_ip_addr_string(blobmsg_data(cur), v6, v6 ? 32 : 128);
205 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur));
210 addr->flags |= DEVADDR_EXTERNAL;
212 vlist_add(&iface->proto_addr, &addr->node);
224 static const struct blobmsg_policy route_attr[__ROUTE_LAST] = {
225 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
226 [ROUTE_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_INT32 },
227 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
228 [ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
232 parse_route(struct interface *iface, struct blob_attr *attr, bool v6)
234 struct blob_attr *tb[__ROUTE_LAST], *cur;
235 struct device_route *route;
236 int af = v6 ? AF_INET6 : AF_INET;
238 blobmsg_parse(route_attr, __ROUTE_LAST, tb, blobmsg_data(attr), blobmsg_data_len(attr));
240 if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
243 route = calloc(1, sizeof(*route));
247 route->mask = v6 ? 128 : 32;
248 if ((cur = tb[ROUTE_MASK]) != NULL) {
249 route->mask = blobmsg_get_u32(cur);
250 if (route->mask > v6 ? 128 : 32)
254 if ((cur = tb[ROUTE_TARGET]) != NULL) {
255 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
256 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
261 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
262 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
263 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
268 if ((cur = tb[ROUTE_DEVICE]) != NULL)
269 route->device = device_get(blobmsg_data(cur), true);
271 vlist_add(&iface->proto_route, &route->node);
279 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
282 struct blob_attr *cur;
285 blobmsg_for_each_attr(cur, attr, rem) {
286 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
287 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
291 parse_route(iface, cur, v6);
309 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
310 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
311 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
312 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
313 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
314 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
315 [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
316 [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
317 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
318 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
322 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
324 struct blob_attr *cur;
325 bool addr_ext = false;
328 if (!tb[NOTIFY_LINK_UP])
329 return UBUS_STATUS_INVALID_ARGUMENT;
331 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
333 if (!tb[NOTIFY_IFNAME])
334 return UBUS_STATUS_INVALID_ARGUMENT;
336 if (!state->l3_dev.dev) {
337 device_add_user(&state->l3_dev,
338 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
339 device_claim(&state->l3_dev);
340 state->proto.iface->l3_dev = &state->l3_dev;
342 state->proto.proto_event(&state->proto, IFPEV_UP);
344 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
347 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL)
348 addr_ext = blobmsg_get_bool(cur);
350 if ((cur = tb[NOTIFY_IPADDR]) != NULL)
351 proto_shell_parse_addr_list(state->proto.iface, cur, false, addr_ext);
353 if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
354 proto_shell_parse_addr_list(state->proto.iface, cur, true, addr_ext);
356 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
357 proto_shell_parse_route_list(state->proto.iface, cur, false);
359 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
360 proto_shell_parse_route_list(state->proto.iface, cur, true);
366 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
368 struct blob_attr *cur;
373 if (!tb[NOTIFY_COMMAND])
376 blobmsg_for_each_attr(cur, tb[NOTIFY_COMMAND], rem) {
377 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
380 if (!blobmsg_check_attr(cur, NULL))
383 argv[argc++] = blobmsg_data(cur);
384 if (argc == ARRAY_SIZE(argv) - 1)
388 start_process((const char **) argv, &state->proto_task);
393 return UBUS_STATUS_INVALID_ARGUMENT;
397 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
399 struct proto_shell_state *state;
400 struct blob_attr *tb[__NOTIFY_LAST];
402 state = container_of(proto, struct proto_shell_state, proto);
404 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
405 if (!tb[NOTIFY_ACTION])
406 return UBUS_STATUS_INVALID_ARGUMENT;
408 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
410 return proto_shell_update_link(state, tb);
412 return proto_shell_run_command(state, tb);
414 return UBUS_STATUS_INVALID_ARGUMENT;
418 struct interface_proto_state *
419 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
420 struct blob_attr *attr)
422 struct proto_shell_state *state;
424 state = calloc(1, sizeof(*state));
425 state->config = malloc(blob_pad_len(attr));
429 memcpy(state->config, attr, blob_pad_len(attr));
430 state->proto.free = proto_shell_free;
431 state->proto.notify = proto_shell_notify;
432 state->proto.cb = proto_shell_handler;
433 state->setup_timeout.cb = proto_shell_setup_timeout_cb;
434 state->setup_task.cb = proto_shell_setup_cb;
435 state->teardown_task.cb = proto_shell_teardown_cb;
436 state->proto_task.cb = proto_shell_task_cb;
437 state->handler = container_of(h, struct proto_shell_handler, proto);
439 return &state->proto;
447 check_type(json_object *obj, json_type type)
452 if (json_object_get_type(obj) != type)
458 static inline json_object *
459 get_field(json_object *obj, const char *name, json_type type)
461 return check_type(json_object_object_get(obj, name), type);
465 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
467 struct blobmsg_policy *attrs;
468 char *str_buf, *str_cur;
472 config->n_params = json_object_array_length(obj);
473 attrs = calloc(1, sizeof(*attrs) * config->n_params);
477 config->params = attrs;
478 for (i = 0; i < config->n_params; i++) {
479 json_object *cur, *name, *type;
481 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
485 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
489 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
493 attrs[i].name = json_object_get_string(name);
494 attrs[i].type = json_object_get_int(type);
495 if (attrs[i].type > BLOBMSG_TYPE_LAST)
498 str_len += strlen(attrs[i].name) + 1;
501 str_buf = malloc(str_len);
506 for (i = 0; i < config->n_params; i++) {
507 const char *name = attrs[i].name;
509 attrs[i].name = str_cur;
510 str_cur += sprintf(str_cur, "%s", name) + 1;
517 config->n_params = 0;
522 proto_shell_add_handler(const char *script, json_object *obj)
524 struct proto_shell_handler *handler;
525 struct proto_handler *proto;
526 json_object *config, *tmp;
530 if (!check_type(obj, json_type_object))
533 tmp = get_field(obj, "name", json_type_string);
537 name = json_object_get_string(tmp);
539 handler = calloc(1, sizeof(*handler) +
545 strcpy(handler->script_name, script);
547 str = handler->script_name + strlen(handler->script_name) + 1;
550 proto = &handler->proto;
552 proto->config_params = &handler->config;
553 proto->attach = proto_shell_attach;
555 tmp = get_field(obj, "no-device", json_type_boolean);
556 if (tmp && json_object_get_boolean(tmp))
557 handler->proto.flags |= PROTO_FLAG_NODEV;
559 config = get_field(obj, "config", json_type_array);
561 handler->config_buf = proto_shell_parse_config(&handler->config, config);
563 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
564 add_proto_handler(proto);
567 static void proto_shell_add_script(const char *name)
569 struct json_tokener *tok = NULL;
571 static char buf[512];
572 char *start, *end, *cmd;
576 #define DUMP_SUFFIX " '' dump"
578 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
579 sprintf(cmd, "%s" DUMP_SUFFIX, name);
586 buflen = fread(buf, 1, sizeof(buf) - 1, f);
593 end = memchr(start, '\n', len);
598 tok = json_tokener_new();
600 obj = json_tokener_parse_ex(tok, start, len);
601 if (!is_error(obj)) {
602 proto_shell_add_handler(name, obj);
603 json_object_put(obj);
604 json_tokener_free(tok);
610 len = buflen - (start - buf);
613 } while (!feof(f) && !ferror(f));
616 json_tokener_free(tok);
621 void __init proto_shell_init(void)
627 main_fd = open(".", O_RDONLY | O_DIRECTORY);
631 if (chdir(main_path)) {
632 perror("chdir(main path)");
636 if (chdir("./proto"))
639 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
643 glob("./*.sh", 0, NULL, &g);
644 for (i = 0; i < g.gl_pathc; i++)
645 proto_shell_add_script(g.gl_pathv[i]);