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"
21 static struct netifd_fd proto_fd;
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 netifd_process setup_task;
40 struct netifd_process teardown_task;
41 bool teardown_pending;
42 bool teardown_wait_task;
44 struct netifd_process proto_task;
48 proto_shell_handler(struct interface_proto_state *proto,
49 enum interface_proto_cmd cmd, bool force)
51 struct proto_shell_state *state;
52 struct proto_shell_handler *handler;
53 struct netifd_process *proc;
59 state = container_of(proto, struct proto_shell_state, proto);
60 handler = state->handler;
62 if (cmd == PROTO_CMD_SETUP) {
64 proc = &state->setup_task;
67 proc = &state->teardown_task;
68 if (state->setup_task.uloop.pending && !state->teardown_wait_task) {
69 uloop_timeout_set(&state->setup_timeout, 1000);
70 kill(state->setup_task.uloop.pid, SIGTERM);
71 state->teardown_pending = true;
76 config = blobmsg_format_json(state->config, true);
80 argv[i++] = handler->script_name;
81 argv[i++] = handler->proto.name;
83 argv[i++] = proto->iface->name;
85 if (proto->iface->main_dev.dev)
86 argv[i++] = proto->iface->main_dev.dev->ifname;
89 ret = netifd_start_process(argv, NULL, proc);
96 proto_shell_setup_timeout_cb(struct uloop_timeout *timeout)
98 struct proto_shell_state *state;
100 state = container_of(timeout, struct proto_shell_state, setup_timeout);
101 kill(state->setup_task.uloop.pid, SIGKILL);
105 proto_shell_setup_cb(struct netifd_process *p, int ret)
107 struct proto_shell_state *state;
109 state = container_of(p, struct proto_shell_state, setup_task);
110 uloop_timeout_cancel(&state->setup_timeout);
111 if (state->teardown_pending) {
112 state->teardown_pending = false;
113 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
118 proto_shell_teardown_cb(struct netifd_process *p, int ret)
120 struct proto_shell_state *state;
122 state = container_of(p, struct proto_shell_state, teardown_task);
124 if (state->teardown_wait_task)
127 netifd_kill_process(&state->proto_task);
128 state->proto.proto_event(&state->proto, IFPEV_DOWN);
132 proto_shell_task_cb(struct netifd_process *p, int ret)
134 struct proto_shell_state *state;
135 bool teardown_wait_task;
137 state = container_of(p, struct proto_shell_state, proto_task);
139 teardown_wait_task = state->teardown_wait_task;
140 state->teardown_wait_task = false;
141 if (state->teardown_pending || state->teardown_task.uloop.pending)
144 if (teardown_wait_task) {
145 proto_shell_teardown_cb(&state->teardown_task, 0);
149 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
150 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
154 proto_shell_free(struct interface_proto_state *proto)
156 struct proto_shell_state *state;
158 state = container_of(proto, struct proto_shell_state, proto);
164 proto_shell_parse_addr_list(struct interface_ip_settings *ip, struct blob_attr *attr,
165 bool v6, bool external)
167 struct device_addr *addr;
168 struct blob_attr *cur;
171 blobmsg_for_each_attr(cur, attr, rem) {
172 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
173 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur));
177 addr = proto_parse_ip_addr_string(blobmsg_data(cur), v6, v6 ? 32 : 128);
179 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur));
184 addr->flags |= DEVADDR_EXTERNAL;
186 vlist_add(&ip->addr, &addr->node);
191 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
194 struct blob_attr *cur;
197 blobmsg_for_each_attr(cur, attr, rem) {
198 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
199 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
203 interface_ip_add_route(iface, cur, v6);
224 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
225 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
226 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
227 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
228 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
229 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
230 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
231 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
232 [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
233 [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
234 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
235 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
236 [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
237 [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
241 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
243 struct interface_ip_settings *ip;
244 struct blob_attr *cur;
246 bool addr_ext = false;
249 if (!tb[NOTIFY_LINK_UP])
250 return UBUS_STATUS_INVALID_ARGUMENT;
252 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
254 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
258 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
259 addr_ext = blobmsg_get_bool(cur);
264 if (!tb[NOTIFY_IFNAME]) {
265 if (!state->proto.iface->main_dev.dev)
266 return UBUS_STATUS_INVALID_ARGUMENT;
268 if (state->l3_dev.dev)
269 device_remove_user(&state->l3_dev);
271 device_add_user(&state->l3_dev,
272 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), dev_create));
273 state->proto.iface->l3_dev = &state->l3_dev;
274 device_claim(&state->l3_dev);
277 ip = &state->proto.iface->proto_ip;
278 interface_update_start(state->proto.iface);
280 if ((cur = tb[NOTIFY_IPADDR]) != NULL)
281 proto_shell_parse_addr_list(ip, cur, false, addr_ext);
283 if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
284 proto_shell_parse_addr_list(ip, cur, true, addr_ext);
286 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
287 proto_shell_parse_route_list(state->proto.iface, cur, false);
289 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
290 proto_shell_parse_route_list(state->proto.iface, cur, true);
292 if ((cur = tb[NOTIFY_DNS]) != NULL)
293 interface_add_dns_server_list(ip, cur);
295 if ((cur = tb[NOTIFY_DNS_SEARCH]) != NULL)
296 interface_add_dns_search_list(ip, cur);
298 interface_update_complete(state->proto.iface);
300 state->proto.proto_event(&state->proto, IFPEV_UP);
306 fill_string_list(struct blob_attr *attr, char **argv, int max)
308 struct blob_attr *cur;
315 blobmsg_for_each_attr(cur, attr, rem) {
316 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
319 if (!blobmsg_check_attr(cur, NULL))
322 argv[argc++] = blobmsg_data(cur);
333 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
338 if (!tb[NOTIFY_COMMAND])
341 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
344 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
347 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
352 return UBUS_STATUS_INVALID_ARGUMENT;
356 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
358 unsigned int signal = ~0;
360 if (tb[NOTIFY_SIGNAL])
361 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
366 if (state->proto_task.uloop.pending) {
367 kill(state->proto_task.uloop.pid, signal);
368 state->teardown_wait_task = true;
375 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
377 struct proto_shell_state *state;
378 struct blob_attr *tb[__NOTIFY_LAST];
380 state = container_of(proto, struct proto_shell_state, proto);
382 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
383 if (!tb[NOTIFY_ACTION])
384 return UBUS_STATUS_INVALID_ARGUMENT;
386 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
388 return proto_shell_update_link(state, tb);
390 return proto_shell_run_command(state, tb);
392 return proto_shell_kill_command(state, tb);
394 return UBUS_STATUS_INVALID_ARGUMENT;
398 static struct interface_proto_state *
399 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
400 struct blob_attr *attr)
402 struct proto_shell_state *state;
404 state = calloc(1, sizeof(*state));
405 state->config = malloc(blob_pad_len(attr));
409 memcpy(state->config, attr, blob_pad_len(attr));
410 state->proto.free = proto_shell_free;
411 state->proto.notify = proto_shell_notify;
412 state->proto.cb = proto_shell_handler;
413 state->setup_timeout.cb = proto_shell_setup_timeout_cb;
414 state->setup_task.cb = proto_shell_setup_cb;
415 state->setup_task.dir_fd = proto_fd.fd;
416 state->setup_task.log_prefix = iface->name;
417 state->teardown_task.cb = proto_shell_teardown_cb;
418 state->teardown_task.dir_fd = proto_fd.fd;
419 state->teardown_task.log_prefix = iface->name;
420 state->proto_task.cb = proto_shell_task_cb;
421 state->proto_task.dir_fd = proto_fd.fd;
422 state->proto_task.log_prefix = iface->name;
423 state->handler = container_of(h, struct proto_shell_handler, proto);
425 return &state->proto;
433 check_type(json_object *obj, json_type type)
438 if (json_object_get_type(obj) != type)
444 static inline json_object *
445 get_field(json_object *obj, const char *name, json_type type)
447 return check_type(json_object_object_get(obj, name), type);
451 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
453 struct blobmsg_policy *attrs;
454 char *str_buf, *str_cur;
458 config->n_params = json_object_array_length(obj);
459 attrs = calloc(1, sizeof(*attrs) * config->n_params);
463 config->params = attrs;
464 for (i = 0; i < config->n_params; i++) {
465 json_object *cur, *name, *type;
467 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
471 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
475 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
479 attrs[i].name = json_object_get_string(name);
480 attrs[i].type = json_object_get_int(type);
481 if (attrs[i].type > BLOBMSG_TYPE_LAST)
484 str_len += strlen(attrs[i].name) + 1;
487 str_buf = malloc(str_len);
492 for (i = 0; i < config->n_params; i++) {
493 const char *name = attrs[i].name;
495 attrs[i].name = str_cur;
496 str_cur += sprintf(str_cur, "%s", name) + 1;
503 config->n_params = 0;
508 proto_shell_add_handler(const char *script, json_object *obj)
510 struct proto_shell_handler *handler;
511 struct proto_handler *proto;
512 json_object *config, *tmp;
516 if (!check_type(obj, json_type_object))
519 tmp = get_field(obj, "name", json_type_string);
523 name = json_object_get_string(tmp);
525 handler = calloc(1, sizeof(*handler) +
531 strcpy(handler->script_name, script);
533 str = handler->script_name + strlen(handler->script_name) + 1;
536 proto = &handler->proto;
538 proto->config_params = &handler->config;
539 proto->attach = proto_shell_attach;
541 tmp = get_field(obj, "no-device", json_type_boolean);
542 if (tmp && json_object_get_boolean(tmp))
543 handler->proto.flags |= PROTO_FLAG_NODEV;
545 config = get_field(obj, "config", json_type_array);
547 handler->config_buf = proto_shell_parse_config(&handler->config, config);
549 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
550 add_proto_handler(proto);
553 static void proto_shell_add_script(const char *name)
555 struct json_tokener *tok = NULL;
557 static char buf[512];
562 #define DUMP_SUFFIX " '' dump"
564 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
565 sprintf(cmd, "%s" DUMP_SUFFIX, name);
572 start = fgets(buf, sizeof(buf), f);
579 tok = json_tokener_new();
581 obj = json_tokener_parse_ex(tok, start, len);
582 if (!is_error(obj)) {
583 proto_shell_add_handler(name, obj);
584 json_object_put(obj);
585 json_tokener_free(tok);
587 } else if (start[len - 1] == '\n') {
588 json_tokener_free(tok);
591 } while (!feof(f) && !ferror(f));
594 json_tokener_free(tok);
599 static void __init proto_shell_init(void)
605 main_fd = open(".", O_RDONLY | O_DIRECTORY);
609 if (chdir(main_path)) {
610 perror("chdir(main path)");
614 if (chdir("./proto"))
617 proto_fd.fd = open(".", O_RDONLY | O_DIRECTORY);
621 netifd_fd_add(&proto_fd);
622 glob("./*.sh", 0, NULL, &g);
623 for (i = 0; i < g.gl_pathc; i++)
624 proto_shell_add_script(g.gl_pathv[i]);