10 #include <netinet/in.h>
12 #include <libubox/blobmsg_json.h>
15 #include "interface.h"
16 #include "interface-ip.h"
21 struct proto_shell_handler {
22 struct list_head list;
23 struct proto_handler proto;
24 struct config_param_list config;
29 struct proto_shell_state {
30 struct interface_proto_state proto;
31 struct proto_shell_handler *handler;
32 struct blob_attr *config;
34 struct device_user l3_dev;
36 struct uloop_timeout setup_timeout;
37 struct uloop_process setup_task;
38 struct uloop_process teardown_task;
39 bool teardown_pending;
43 run_script(const char **argv, struct uloop_process *proc)
47 if ((pid = fork()) < 0)
52 execvp(argv[0], (char **) argv);
60 uloop_process_add(proc);
66 proto_shell_handler(struct interface_proto_state *proto,
67 enum interface_proto_cmd cmd, bool force)
69 struct proto_shell_state *state;
70 struct proto_shell_handler *handler;
71 struct uloop_process *proc;
77 state = container_of(proto, struct proto_shell_state, proto);
78 handler = state->handler;
80 if (cmd == PROTO_CMD_SETUP) {
82 proc = &state->setup_task;
85 proc = &state->teardown_task;
86 if (state->setup_task.pending) {
87 uloop_timeout_set(&state->setup_timeout, 1000);
88 kill(state->setup_task.pid, SIGINT);
89 state->teardown_pending = true;
94 config = blobmsg_format_json(state->config, true);
98 argv[i++] = handler->script_name;
99 argv[i++] = handler->proto.name;
101 argv[i++] = proto->iface->name;
103 if (proto->iface->main_dev.dev)
104 argv[i++] = proto->iface->main_dev.dev->ifname;
107 ret = run_script(argv, proc);
114 proto_shell_setup_timeout_cb(struct uloop_timeout *timeout)
116 struct proto_shell_state *state;
118 state = container_of(timeout, struct proto_shell_state, setup_timeout);
119 kill(state->setup_task.pid, SIGKILL);
123 proto_shell_setup_cb(struct uloop_process *p, int ret)
125 struct proto_shell_state *state;
127 state = container_of(p, struct proto_shell_state, setup_task);
128 uloop_timeout_cancel(&state->setup_timeout);
129 if (state->teardown_pending) {
130 state->teardown_pending = false;
131 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
136 proto_shell_teardown_cb(struct uloop_process *p, int ret)
138 struct proto_shell_state *state;
140 state = container_of(p, struct proto_shell_state, teardown_task);
141 state->proto.proto_event(&state->proto, IFPEV_DOWN);
142 if (state->l3_dev.dev)
143 device_remove_user(&state->l3_dev);
147 proto_shell_free(struct interface_proto_state *proto)
149 struct proto_shell_state *state;
151 state = container_of(proto, struct proto_shell_state, proto);
157 proto_shell_parse_addr_list(struct interface *iface, struct blob_attr *attr,
158 bool v6, bool external)
160 struct device_addr *addr;
161 struct blob_attr *cur;
164 blobmsg_for_each_attr(cur, attr, rem) {
165 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
166 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur));
170 addr = proto_parse_ip_addr_string(blobmsg_data(cur), v6, v6 ? 32 : 128);
172 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur));
177 addr->flags |= DEVADDR_EXTERNAL;
179 vlist_add(&iface->proto_addr, &addr->node);
191 static const struct blobmsg_policy route_attr[__ROUTE_LAST] = {
192 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
193 [ROUTE_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_INT32 },
194 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
195 [ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
199 parse_route(struct interface *iface, struct blob_attr *attr, bool v6)
201 struct blob_attr *tb[__ROUTE_LAST], *cur;
202 struct device_route *route;
203 int af = v6 ? AF_INET6 : AF_INET;
205 blobmsg_parse(route_attr, __ROUTE_LAST, tb, blobmsg_data(attr), blobmsg_data_len(attr));
207 if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
210 route = calloc(1, sizeof(*route));
214 route->mask = v6 ? 128 : 32;
215 if ((cur = tb[ROUTE_MASK]) != NULL) {
216 route->mask = blobmsg_get_u32(cur);
217 if (route->mask > v6 ? 128 : 32)
221 if ((cur = tb[ROUTE_TARGET]) != NULL) {
222 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
223 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
228 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
229 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
230 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
235 if ((cur = tb[ROUTE_DEVICE]) != NULL)
236 route->device = device_get(blobmsg_data(cur), true);
238 vlist_add(&iface->proto_route, &route->node);
246 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
249 struct blob_attr *cur;
252 blobmsg_for_each_attr(cur, attr, rem) {
253 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
254 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
258 parse_route(iface, cur, v6);
274 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
275 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
276 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
277 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
278 [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
279 [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
280 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
281 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
285 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
287 struct proto_shell_state *state;
288 struct blob_attr *tb[__NOTIFY_LAST], *cur;
289 bool addr_ext = false;
292 state = container_of(proto, struct proto_shell_state, proto);
294 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
295 if (!tb[NOTIFY_LINK_UP])
296 return UBUS_STATUS_INVALID_ARGUMENT;
298 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
300 if (!tb[NOTIFY_IFNAME])
301 return UBUS_STATUS_INVALID_ARGUMENT;
303 if (!state->l3_dev.dev) {
304 device_add_user(&state->l3_dev,
305 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
306 device_claim(&state->l3_dev);
307 state->proto.iface->l3_dev = &state->l3_dev;
309 state->proto.proto_event(&state->proto, IFPEV_UP);
311 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
314 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL)
315 addr_ext = blobmsg_get_bool(cur);
317 if ((cur = tb[NOTIFY_IPADDR]) != NULL)
318 proto_shell_parse_addr_list(state->proto.iface, cur, false, addr_ext);
320 if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
321 proto_shell_parse_addr_list(state->proto.iface, cur, true, addr_ext);
323 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
324 proto_shell_parse_route_list(state->proto.iface, cur, false);
326 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
327 proto_shell_parse_route_list(state->proto.iface, cur, true);
332 struct interface_proto_state *
333 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
334 struct blob_attr *attr)
336 struct proto_shell_state *state;
338 state = calloc(1, sizeof(*state));
339 state->config = malloc(blob_pad_len(attr));
343 memcpy(state->config, attr, blob_pad_len(attr));
344 state->proto.free = proto_shell_free;
345 state->proto.notify = proto_shell_notify;
346 state->proto.cb = proto_shell_handler;
347 state->setup_timeout.cb = proto_shell_setup_timeout_cb;
348 state->setup_task.cb = proto_shell_setup_cb;
349 state->teardown_task.cb = proto_shell_teardown_cb;
350 state->handler = container_of(h, struct proto_shell_handler, proto);
352 return &state->proto;
360 check_type(json_object *obj, json_type type)
365 if (json_object_get_type(obj) != type)
371 static inline json_object *
372 get_field(json_object *obj, const char *name, json_type type)
374 return check_type(json_object_object_get(obj, name), type);
378 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
380 struct blobmsg_policy *attrs;
381 char *str_buf, *str_cur;
385 attrs = calloc(1, sizeof(*attrs));
389 config->n_params = json_object_array_length(obj);
390 config->params = attrs;
391 for (i = 0; i < config->n_params; i++) {
392 json_object *cur, *name, *type;
394 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
398 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
402 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
406 attrs[i].name = json_object_get_string(name);
407 attrs[i].type = json_object_get_int(type);
408 if (attrs[i].type > BLOBMSG_TYPE_LAST)
411 str_len += strlen(attrs[i].name + 1);
414 str_buf = malloc(str_len);
419 for (i = 0; i < config->n_params; i++) {
420 const char *name = attrs[i].name;
422 attrs[i].name = str_cur;
423 str_cur += sprintf(str_cur, "%s", name) + 1;
430 config->n_params = 0;
435 proto_shell_add_handler(const char *script, json_object *obj)
437 struct proto_shell_handler *handler;
438 struct proto_handler *proto;
439 json_object *config, *tmp;
443 if (!check_type(obj, json_type_object))
446 tmp = get_field(obj, "name", json_type_string);
450 name = json_object_get_string(tmp);
452 handler = calloc(1, sizeof(*handler) +
458 strcpy(handler->script_name, script);
460 str = handler->script_name + strlen(handler->script_name) + 1;
463 proto = &handler->proto;
465 proto->config_params = &handler->config;
466 proto->attach = proto_shell_attach;
468 tmp = get_field(obj, "no-device", json_type_boolean);
469 if (tmp && json_object_get_boolean(tmp))
470 handler->proto.flags |= PROTO_FLAG_NODEV;
472 config = get_field(obj, "config", json_type_array);
474 handler->config_buf = proto_shell_parse_config(&handler->config, config);
476 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
477 add_proto_handler(proto);
480 static void proto_shell_add_script(const char *name)
482 struct json_tokener *tok = NULL;
484 static char buf[512];
485 char *start, *end, *cmd;
489 #define DUMP_SUFFIX " '' dump"
491 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
492 sprintf(cmd, "%s" DUMP_SUFFIX, name);
499 buflen = fread(buf, 1, sizeof(buf) - 1, f);
506 end = memchr(start, '\n', len);
511 tok = json_tokener_new();
513 obj = json_tokener_parse_ex(tok, start, len);
514 if (!is_error(obj)) {
515 proto_shell_add_handler(name, obj);
516 json_object_put(obj);
517 json_tokener_free(tok);
523 len = buflen - (start - buf);
526 } while (!feof(f) && !ferror(f));
529 json_tokener_free(tok);
534 void __init proto_shell_init(void)
540 main_fd = open(".", O_RDONLY | O_DIRECTORY);
544 if (chdir(main_path)) {
545 perror("chdir(main path)");
549 if (chdir("./proto"))
552 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
556 glob("./*.sh", 0, NULL, &g);
557 for (i = 0; i < g.gl_pathc; i++)
558 proto_shell_add_script(g.gl_pathv[i]);