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;
45 run_script(const char **argv, struct uloop_process *proc)
49 if ((pid = fork()) < 0)
54 execvp(argv[0], (char **) argv);
62 uloop_process_add(proc);
68 proto_shell_handler(struct interface_proto_state *proto,
69 enum interface_proto_cmd cmd, bool force)
71 struct proto_shell_state *state;
72 struct proto_shell_handler *handler;
73 struct uloop_process *proc;
79 state = container_of(proto, struct proto_shell_state, proto);
80 handler = state->handler;
82 if (cmd == PROTO_CMD_SETUP) {
84 proc = &state->setup_task;
87 proc = &state->teardown_task;
88 if (state->setup_task.pending) {
89 uloop_timeout_set(&state->setup_timeout, 1000);
90 kill(state->setup_task.pid, SIGINT);
91 state->teardown_pending = true;
96 config = blobmsg_format_json(state->config, true);
100 argv[i++] = handler->script_name;
101 argv[i++] = handler->proto.name;
103 argv[i++] = proto->iface->name;
105 if (proto->iface->main_dev.dev)
106 argv[i++] = proto->iface->main_dev.dev->ifname;
109 ret = run_script(argv, proc);
116 proto_shell_setup_timeout_cb(struct uloop_timeout *timeout)
118 struct proto_shell_state *state;
120 state = container_of(timeout, struct proto_shell_state, setup_timeout);
121 kill(state->setup_task.pid, SIGKILL);
125 proto_shell_setup_cb(struct uloop_process *p, int ret)
127 struct proto_shell_state *state;
129 state = container_of(p, struct proto_shell_state, setup_task);
130 uloop_timeout_cancel(&state->setup_timeout);
131 if (state->teardown_pending) {
132 state->teardown_pending = false;
133 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
138 proto_shell_teardown_cb(struct uloop_process *p, int ret)
140 struct proto_shell_state *state;
142 state = container_of(p, struct proto_shell_state, teardown_task);
144 if (state->l3_dev.dev)
145 device_remove_user(&state->l3_dev);
147 state->proto.proto_event(&state->proto, IFPEV_DOWN);
151 proto_shell_free(struct interface_proto_state *proto)
153 struct proto_shell_state *state;
155 state = container_of(proto, struct proto_shell_state, proto);
161 proto_shell_parse_addr_list(struct interface *iface, struct blob_attr *attr,
162 bool v6, bool external)
164 struct device_addr *addr;
165 struct blob_attr *cur;
168 blobmsg_for_each_attr(cur, attr, rem) {
169 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
170 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur));
174 addr = proto_parse_ip_addr_string(blobmsg_data(cur), v6, v6 ? 32 : 128);
176 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur));
181 addr->flags |= DEVADDR_EXTERNAL;
183 vlist_add(&iface->proto_addr, &addr->node);
195 static const struct blobmsg_policy route_attr[__ROUTE_LAST] = {
196 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
197 [ROUTE_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_INT32 },
198 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
199 [ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
203 parse_route(struct interface *iface, struct blob_attr *attr, bool v6)
205 struct blob_attr *tb[__ROUTE_LAST], *cur;
206 struct device_route *route;
207 int af = v6 ? AF_INET6 : AF_INET;
209 blobmsg_parse(route_attr, __ROUTE_LAST, tb, blobmsg_data(attr), blobmsg_data_len(attr));
211 if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
214 route = calloc(1, sizeof(*route));
218 route->mask = v6 ? 128 : 32;
219 if ((cur = tb[ROUTE_MASK]) != NULL) {
220 route->mask = blobmsg_get_u32(cur);
221 if (route->mask > v6 ? 128 : 32)
225 if ((cur = tb[ROUTE_TARGET]) != NULL) {
226 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
227 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
232 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
233 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
234 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
239 if ((cur = tb[ROUTE_DEVICE]) != NULL)
240 route->device = device_get(blobmsg_data(cur), true);
242 vlist_add(&iface->proto_route, &route->node);
250 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
253 struct blob_attr *cur;
256 blobmsg_for_each_attr(cur, attr, rem) {
257 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
258 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
262 parse_route(iface, cur, v6);
278 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
279 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
280 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
281 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
282 [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
283 [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
284 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
285 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
289 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
291 struct proto_shell_state *state;
292 struct blob_attr *tb[__NOTIFY_LAST], *cur;
293 bool addr_ext = false;
296 state = container_of(proto, struct proto_shell_state, proto);
298 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
299 if (!tb[NOTIFY_LINK_UP])
300 return UBUS_STATUS_INVALID_ARGUMENT;
302 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
304 if (!tb[NOTIFY_IFNAME])
305 return UBUS_STATUS_INVALID_ARGUMENT;
307 if (!state->l3_dev.dev) {
308 device_add_user(&state->l3_dev,
309 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
310 device_claim(&state->l3_dev);
311 state->proto.iface->l3_dev = &state->l3_dev;
313 state->proto.proto_event(&state->proto, IFPEV_UP);
315 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
318 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL)
319 addr_ext = blobmsg_get_bool(cur);
321 if ((cur = tb[NOTIFY_IPADDR]) != NULL)
322 proto_shell_parse_addr_list(state->proto.iface, cur, false, addr_ext);
324 if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
325 proto_shell_parse_addr_list(state->proto.iface, cur, true, addr_ext);
327 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
328 proto_shell_parse_route_list(state->proto.iface, cur, false);
330 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
331 proto_shell_parse_route_list(state->proto.iface, cur, true);
336 struct interface_proto_state *
337 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
338 struct blob_attr *attr)
340 struct proto_shell_state *state;
342 state = calloc(1, sizeof(*state));
343 state->config = malloc(blob_pad_len(attr));
347 memcpy(state->config, attr, blob_pad_len(attr));
348 state->proto.free = proto_shell_free;
349 state->proto.notify = proto_shell_notify;
350 state->proto.cb = proto_shell_handler;
351 state->setup_timeout.cb = proto_shell_setup_timeout_cb;
352 state->setup_task.cb = proto_shell_setup_cb;
353 state->teardown_task.cb = proto_shell_teardown_cb;
354 state->handler = container_of(h, struct proto_shell_handler, proto);
356 return &state->proto;
364 check_type(json_object *obj, json_type type)
369 if (json_object_get_type(obj) != type)
375 static inline json_object *
376 get_field(json_object *obj, const char *name, json_type type)
378 return check_type(json_object_object_get(obj, name), type);
382 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
384 struct blobmsg_policy *attrs;
385 char *str_buf, *str_cur;
389 config->n_params = json_object_array_length(obj);
390 attrs = calloc(1, sizeof(*attrs) * config->n_params);
394 config->params = attrs;
395 for (i = 0; i < config->n_params; i++) {
396 json_object *cur, *name, *type;
398 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
402 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
406 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
410 attrs[i].name = json_object_get_string(name);
411 attrs[i].type = json_object_get_int(type);
412 if (attrs[i].type > BLOBMSG_TYPE_LAST)
415 str_len += strlen(attrs[i].name) + 1;
418 str_buf = malloc(str_len);
423 for (i = 0; i < config->n_params; i++) {
424 const char *name = attrs[i].name;
426 attrs[i].name = str_cur;
427 str_cur += sprintf(str_cur, "%s", name) + 1;
434 config->n_params = 0;
439 proto_shell_add_handler(const char *script, json_object *obj)
441 struct proto_shell_handler *handler;
442 struct proto_handler *proto;
443 json_object *config, *tmp;
447 if (!check_type(obj, json_type_object))
450 tmp = get_field(obj, "name", json_type_string);
454 name = json_object_get_string(tmp);
456 handler = calloc(1, sizeof(*handler) +
462 strcpy(handler->script_name, script);
464 str = handler->script_name + strlen(handler->script_name) + 1;
467 proto = &handler->proto;
469 proto->config_params = &handler->config;
470 proto->attach = proto_shell_attach;
472 tmp = get_field(obj, "no-device", json_type_boolean);
473 if (tmp && json_object_get_boolean(tmp))
474 handler->proto.flags |= PROTO_FLAG_NODEV;
476 config = get_field(obj, "config", json_type_array);
478 handler->config_buf = proto_shell_parse_config(&handler->config, config);
480 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
481 add_proto_handler(proto);
484 static void proto_shell_add_script(const char *name)
486 struct json_tokener *tok = NULL;
488 static char buf[512];
489 char *start, *end, *cmd;
493 #define DUMP_SUFFIX " '' dump"
495 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
496 sprintf(cmd, "%s" DUMP_SUFFIX, name);
503 buflen = fread(buf, 1, sizeof(buf) - 1, f);
510 end = memchr(start, '\n', len);
515 tok = json_tokener_new();
517 obj = json_tokener_parse_ex(tok, start, len);
518 if (!is_error(obj)) {
519 proto_shell_add_handler(name, obj);
520 json_object_put(obj);
521 json_tokener_free(tok);
527 len = buflen - (start - buf);
530 } while (!feof(f) && !ferror(f));
533 json_tokener_free(tok);
538 void __init proto_shell_init(void)
544 main_fd = open(".", O_RDONLY | O_DIRECTORY);
548 if (chdir(main_path)) {
549 perror("chdir(main path)");
553 if (chdir("./proto"))
556 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
560 glob("./*.sh", 0, NULL, &g);
561 for (i = 0; i < g.gl_pathc; i++)
562 proto_shell_add_script(g.gl_pathv[i]);