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 start_process(const char **argv, struct uloop_process *proc)
52 kill(proc->pid, SIGTERM);
53 uloop_process_delete(proc);
56 if ((pid = fork()) < 0)
61 execvp(argv[0], (char **) argv);
69 uloop_process_add(proc);
75 proto_shell_handler(struct interface_proto_state *proto,
76 enum interface_proto_cmd cmd, bool force)
78 struct proto_shell_state *state;
79 struct proto_shell_handler *handler;
80 struct uloop_process *proc;
86 state = container_of(proto, struct proto_shell_state, proto);
87 handler = state->handler;
89 if (cmd == PROTO_CMD_SETUP) {
91 proc = &state->setup_task;
94 proc = &state->teardown_task;
95 if (state->setup_task.pending) {
96 uloop_timeout_set(&state->setup_timeout, 1000);
97 kill(state->setup_task.pid, SIGINT);
98 state->teardown_pending = true;
103 config = blobmsg_format_json(state->config, true);
107 argv[i++] = handler->script_name;
108 argv[i++] = handler->proto.name;
110 argv[i++] = proto->iface->name;
112 if (proto->iface->main_dev.dev)
113 argv[i++] = proto->iface->main_dev.dev->ifname;
116 ret = start_process(argv, proc);
123 proto_shell_setup_timeout_cb(struct uloop_timeout *timeout)
125 struct proto_shell_state *state;
127 state = container_of(timeout, struct proto_shell_state, setup_timeout);
128 kill(state->setup_task.pid, SIGKILL);
132 proto_shell_setup_cb(struct uloop_process *p, int ret)
134 struct proto_shell_state *state;
136 state = container_of(p, struct proto_shell_state, setup_task);
137 uloop_timeout_cancel(&state->setup_timeout);
138 if (state->teardown_pending) {
139 state->teardown_pending = false;
140 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
145 proto_shell_teardown_cb(struct uloop_process *p, int ret)
147 struct proto_shell_state *state;
149 state = container_of(p, struct proto_shell_state, teardown_task);
151 if (state->l3_dev.dev)
152 device_remove_user(&state->l3_dev);
154 state->proto.proto_event(&state->proto, IFPEV_DOWN);
158 proto_shell_task_cb(struct uloop_process *p, int ret)
160 struct proto_shell_state *state;
162 state = container_of(p, struct proto_shell_state, proto_task);
164 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
165 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
169 proto_shell_free(struct interface_proto_state *proto)
171 struct proto_shell_state *state;
173 state = container_of(proto, struct proto_shell_state, proto);
179 proto_shell_parse_addr_list(struct interface *iface, struct blob_attr *attr,
180 bool v6, bool external)
182 struct device_addr *addr;
183 struct blob_attr *cur;
186 blobmsg_for_each_attr(cur, attr, rem) {
187 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
188 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur));
192 addr = proto_parse_ip_addr_string(blobmsg_data(cur), v6, v6 ? 32 : 128);
194 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur));
199 addr->flags |= DEVADDR_EXTERNAL;
201 vlist_add(&iface->proto_addr, &addr->node);
213 static const struct blobmsg_policy route_attr[__ROUTE_LAST] = {
214 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
215 [ROUTE_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_INT32 },
216 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
217 [ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
221 parse_route(struct interface *iface, struct blob_attr *attr, bool v6)
223 struct blob_attr *tb[__ROUTE_LAST], *cur;
224 struct device_route *route;
225 int af = v6 ? AF_INET6 : AF_INET;
227 blobmsg_parse(route_attr, __ROUTE_LAST, tb, blobmsg_data(attr), blobmsg_data_len(attr));
229 if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
232 route = calloc(1, sizeof(*route));
236 route->mask = v6 ? 128 : 32;
237 if ((cur = tb[ROUTE_MASK]) != NULL) {
238 route->mask = blobmsg_get_u32(cur);
239 if (route->mask > v6 ? 128 : 32)
243 if ((cur = tb[ROUTE_TARGET]) != NULL) {
244 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
245 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
250 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
251 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
252 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
257 if ((cur = tb[ROUTE_DEVICE]) != NULL)
258 route->device = device_get(blobmsg_data(cur), true);
260 vlist_add(&iface->proto_route, &route->node);
268 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
271 struct blob_attr *cur;
274 blobmsg_for_each_attr(cur, attr, rem) {
275 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
276 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
280 parse_route(iface, cur, v6);
298 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
299 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
300 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
301 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
302 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
303 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
304 [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
305 [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
306 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
307 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
311 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
313 struct blob_attr *cur;
314 bool addr_ext = false;
317 if (!tb[NOTIFY_LINK_UP])
318 return UBUS_STATUS_INVALID_ARGUMENT;
320 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
322 if (!tb[NOTIFY_IFNAME])
323 return UBUS_STATUS_INVALID_ARGUMENT;
325 if (!state->l3_dev.dev) {
326 device_add_user(&state->l3_dev,
327 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
328 device_claim(&state->l3_dev);
329 state->proto.iface->l3_dev = &state->l3_dev;
331 state->proto.proto_event(&state->proto, IFPEV_UP);
333 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
336 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL)
337 addr_ext = blobmsg_get_bool(cur);
339 if ((cur = tb[NOTIFY_IPADDR]) != NULL)
340 proto_shell_parse_addr_list(state->proto.iface, cur, false, addr_ext);
342 if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
343 proto_shell_parse_addr_list(state->proto.iface, cur, true, addr_ext);
345 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
346 proto_shell_parse_route_list(state->proto.iface, cur, false);
348 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
349 proto_shell_parse_route_list(state->proto.iface, cur, true);
355 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
357 struct blob_attr *cur;
362 if (!tb[NOTIFY_COMMAND])
365 blobmsg_for_each_attr(cur, tb[NOTIFY_COMMAND], rem) {
366 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
369 if (!blobmsg_check_attr(cur, NULL))
372 argv[argc++] = blobmsg_data(cur);
373 if (argc == ARRAY_SIZE(argv) - 1)
377 start_process((const char **) argv, &state->proto_task);
382 return UBUS_STATUS_INVALID_ARGUMENT;
386 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
388 struct proto_shell_state *state;
389 struct blob_attr *tb[__NOTIFY_LAST];
391 state = container_of(proto, struct proto_shell_state, proto);
393 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
394 if (!tb[NOTIFY_ACTION])
395 return UBUS_STATUS_INVALID_ARGUMENT;
397 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
399 return proto_shell_update_link(state, tb);
401 return proto_shell_run_command(state, tb);
403 return UBUS_STATUS_INVALID_ARGUMENT;
407 struct interface_proto_state *
408 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
409 struct blob_attr *attr)
411 struct proto_shell_state *state;
413 state = calloc(1, sizeof(*state));
414 state->config = malloc(blob_pad_len(attr));
418 memcpy(state->config, attr, blob_pad_len(attr));
419 state->proto.free = proto_shell_free;
420 state->proto.notify = proto_shell_notify;
421 state->proto.cb = proto_shell_handler;
422 state->setup_timeout.cb = proto_shell_setup_timeout_cb;
423 state->setup_task.cb = proto_shell_setup_cb;
424 state->teardown_task.cb = proto_shell_teardown_cb;
425 state->proto_task.cb = proto_shell_task_cb;
426 state->handler = container_of(h, struct proto_shell_handler, proto);
428 return &state->proto;
436 check_type(json_object *obj, json_type type)
441 if (json_object_get_type(obj) != type)
447 static inline json_object *
448 get_field(json_object *obj, const char *name, json_type type)
450 return check_type(json_object_object_get(obj, name), type);
454 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
456 struct blobmsg_policy *attrs;
457 char *str_buf, *str_cur;
461 config->n_params = json_object_array_length(obj);
462 attrs = calloc(1, sizeof(*attrs) * config->n_params);
466 config->params = attrs;
467 for (i = 0; i < config->n_params; i++) {
468 json_object *cur, *name, *type;
470 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
474 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
478 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
482 attrs[i].name = json_object_get_string(name);
483 attrs[i].type = json_object_get_int(type);
484 if (attrs[i].type > BLOBMSG_TYPE_LAST)
487 str_len += strlen(attrs[i].name) + 1;
490 str_buf = malloc(str_len);
495 for (i = 0; i < config->n_params; i++) {
496 const char *name = attrs[i].name;
498 attrs[i].name = str_cur;
499 str_cur += sprintf(str_cur, "%s", name) + 1;
506 config->n_params = 0;
511 proto_shell_add_handler(const char *script, json_object *obj)
513 struct proto_shell_handler *handler;
514 struct proto_handler *proto;
515 json_object *config, *tmp;
519 if (!check_type(obj, json_type_object))
522 tmp = get_field(obj, "name", json_type_string);
526 name = json_object_get_string(tmp);
528 handler = calloc(1, sizeof(*handler) +
534 strcpy(handler->script_name, script);
536 str = handler->script_name + strlen(handler->script_name) + 1;
539 proto = &handler->proto;
541 proto->config_params = &handler->config;
542 proto->attach = proto_shell_attach;
544 tmp = get_field(obj, "no-device", json_type_boolean);
545 if (tmp && json_object_get_boolean(tmp))
546 handler->proto.flags |= PROTO_FLAG_NODEV;
548 config = get_field(obj, "config", json_type_array);
550 handler->config_buf = proto_shell_parse_config(&handler->config, config);
552 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
553 add_proto_handler(proto);
556 static void proto_shell_add_script(const char *name)
558 struct json_tokener *tok = NULL;
560 static char buf[512];
561 char *start, *end, *cmd;
565 #define DUMP_SUFFIX " '' dump"
567 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
568 sprintf(cmd, "%s" DUMP_SUFFIX, name);
575 buflen = fread(buf, 1, sizeof(buf) - 1, f);
582 end = memchr(start, '\n', len);
587 tok = json_tokener_new();
589 obj = json_tokener_parse_ex(tok, start, len);
590 if (!is_error(obj)) {
591 proto_shell_add_handler(name, obj);
592 json_object_put(obj);
593 json_tokener_free(tok);
599 len = buflen - (start - buf);
602 } while (!feof(f) && !ferror(f));
605 json_tokener_free(tok);
610 void __init proto_shell_init(void)
616 main_fd = open(".", O_RDONLY | O_DIRECTORY);
620 if (chdir(main_path)) {
621 perror("chdir(main path)");
625 if (chdir("./proto"))
628 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
632 glob("./*.sh", 0, NULL, &g);
633 for (i = 0; i < g.gl_pathc; i++)
634 proto_shell_add_script(g.gl_pathv[i]);