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);
142 if (state->l3_dev.dev)
143 device_remove_user(&state->l3_dev);
145 state->proto.proto_event(&state->proto, IFPEV_DOWN);
149 proto_shell_free(struct interface_proto_state *proto)
151 struct proto_shell_state *state;
153 state = container_of(proto, struct proto_shell_state, proto);
159 proto_shell_parse_addr_list(struct interface *iface, struct blob_attr *attr,
160 bool v6, bool external)
162 struct device_addr *addr;
163 struct blob_attr *cur;
166 blobmsg_for_each_attr(cur, attr, rem) {
167 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
168 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur));
172 addr = proto_parse_ip_addr_string(blobmsg_data(cur), v6, v6 ? 32 : 128);
174 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur));
179 addr->flags |= DEVADDR_EXTERNAL;
181 vlist_add(&iface->proto_addr, &addr->node);
193 static const struct blobmsg_policy route_attr[__ROUTE_LAST] = {
194 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
195 [ROUTE_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_INT32 },
196 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
197 [ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
201 parse_route(struct interface *iface, struct blob_attr *attr, bool v6)
203 struct blob_attr *tb[__ROUTE_LAST], *cur;
204 struct device_route *route;
205 int af = v6 ? AF_INET6 : AF_INET;
207 blobmsg_parse(route_attr, __ROUTE_LAST, tb, blobmsg_data(attr), blobmsg_data_len(attr));
209 if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
212 route = calloc(1, sizeof(*route));
216 route->mask = v6 ? 128 : 32;
217 if ((cur = tb[ROUTE_MASK]) != NULL) {
218 route->mask = blobmsg_get_u32(cur);
219 if (route->mask > v6 ? 128 : 32)
223 if ((cur = tb[ROUTE_TARGET]) != NULL) {
224 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
225 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
230 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
231 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
232 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
237 if ((cur = tb[ROUTE_DEVICE]) != NULL)
238 route->device = device_get(blobmsg_data(cur), true);
240 vlist_add(&iface->proto_route, &route->node);
248 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
251 struct blob_attr *cur;
254 blobmsg_for_each_attr(cur, attr, rem) {
255 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
256 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
260 parse_route(iface, cur, v6);
276 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
277 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
278 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
279 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
280 [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
281 [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
282 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
283 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
287 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
289 struct proto_shell_state *state;
290 struct blob_attr *tb[__NOTIFY_LAST], *cur;
291 bool addr_ext = false;
294 state = container_of(proto, struct proto_shell_state, proto);
296 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
297 if (!tb[NOTIFY_LINK_UP])
298 return UBUS_STATUS_INVALID_ARGUMENT;
300 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
302 if (!tb[NOTIFY_IFNAME])
303 return UBUS_STATUS_INVALID_ARGUMENT;
305 if (!state->l3_dev.dev) {
306 device_add_user(&state->l3_dev,
307 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
308 device_claim(&state->l3_dev);
309 state->proto.iface->l3_dev = &state->l3_dev;
311 state->proto.proto_event(&state->proto, IFPEV_UP);
313 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
316 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL)
317 addr_ext = blobmsg_get_bool(cur);
319 if ((cur = tb[NOTIFY_IPADDR]) != NULL)
320 proto_shell_parse_addr_list(state->proto.iface, cur, false, addr_ext);
322 if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
323 proto_shell_parse_addr_list(state->proto.iface, cur, true, addr_ext);
325 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
326 proto_shell_parse_route_list(state->proto.iface, cur, false);
328 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
329 proto_shell_parse_route_list(state->proto.iface, cur, true);
334 struct interface_proto_state *
335 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
336 struct blob_attr *attr)
338 struct proto_shell_state *state;
340 state = calloc(1, sizeof(*state));
341 state->config = malloc(blob_pad_len(attr));
345 memcpy(state->config, attr, blob_pad_len(attr));
346 state->proto.free = proto_shell_free;
347 state->proto.notify = proto_shell_notify;
348 state->proto.cb = proto_shell_handler;
349 state->setup_timeout.cb = proto_shell_setup_timeout_cb;
350 state->setup_task.cb = proto_shell_setup_cb;
351 state->teardown_task.cb = proto_shell_teardown_cb;
352 state->handler = container_of(h, struct proto_shell_handler, proto);
354 return &state->proto;
362 check_type(json_object *obj, json_type type)
367 if (json_object_get_type(obj) != type)
373 static inline json_object *
374 get_field(json_object *obj, const char *name, json_type type)
376 return check_type(json_object_object_get(obj, name), type);
380 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
382 struct blobmsg_policy *attrs;
383 char *str_buf, *str_cur;
387 config->n_params = json_object_array_length(obj);
388 attrs = calloc(1, sizeof(*attrs) * config->n_params);
392 config->params = attrs;
393 for (i = 0; i < config->n_params; i++) {
394 json_object *cur, *name, *type;
396 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
400 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
404 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
408 attrs[i].name = json_object_get_string(name);
409 attrs[i].type = json_object_get_int(type);
410 if (attrs[i].type > BLOBMSG_TYPE_LAST)
413 str_len += strlen(attrs[i].name) + 1;
416 str_buf = malloc(str_len);
421 for (i = 0; i < config->n_params; i++) {
422 const char *name = attrs[i].name;
424 attrs[i].name = str_cur;
425 str_cur += sprintf(str_cur, "%s", name) + 1;
432 config->n_params = 0;
437 proto_shell_add_handler(const char *script, json_object *obj)
439 struct proto_shell_handler *handler;
440 struct proto_handler *proto;
441 json_object *config, *tmp;
445 if (!check_type(obj, json_type_object))
448 tmp = get_field(obj, "name", json_type_string);
452 name = json_object_get_string(tmp);
454 handler = calloc(1, sizeof(*handler) +
460 strcpy(handler->script_name, script);
462 str = handler->script_name + strlen(handler->script_name) + 1;
465 proto = &handler->proto;
467 proto->config_params = &handler->config;
468 proto->attach = proto_shell_attach;
470 tmp = get_field(obj, "no-device", json_type_boolean);
471 if (tmp && json_object_get_boolean(tmp))
472 handler->proto.flags |= PROTO_FLAG_NODEV;
474 config = get_field(obj, "config", json_type_array);
476 handler->config_buf = proto_shell_parse_config(&handler->config, config);
478 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
479 add_proto_handler(proto);
482 static void proto_shell_add_script(const char *name)
484 struct json_tokener *tok = NULL;
486 static char buf[512];
487 char *start, *end, *cmd;
491 #define DUMP_SUFFIX " '' dump"
493 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
494 sprintf(cmd, "%s" DUMP_SUFFIX, name);
501 buflen = fread(buf, 1, sizeof(buf) - 1, f);
508 end = memchr(start, '\n', len);
513 tok = json_tokener_new();
515 obj = json_tokener_parse_ex(tok, start, len);
516 if (!is_error(obj)) {
517 proto_shell_add_handler(name, obj);
518 json_object_put(obj);
519 json_tokener_free(tok);
525 len = buflen - (start - buf);
528 } while (!feof(f) && !ferror(f));
531 json_tokener_free(tok);
536 void __init proto_shell_init(void)
542 main_fd = open(".", O_RDONLY | O_DIRECTORY);
546 if (chdir(main_path)) {
547 perror("chdir(main path)");
551 if (chdir("./proto"))
554 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
558 glob("./*.sh", 0, NULL, &g);
559 for (i = 0; i < g.gl_pathc; i++)
560 proto_shell_add_script(g.gl_pathv[i]);