2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
21 #include <arpa/inet.h>
22 #include <netinet/in.h>
26 #include "interface.h"
27 #include "interface-ip.h"
32 static int proto_fd = -1;
41 struct proto_shell_handler {
42 struct list_head list;
43 struct proto_handler proto;
48 struct uci_blob_param_list config;
51 struct proto_shell_dependency {
52 struct list_head list;
55 struct proto_shell_state *proto;
56 struct interface_user dep;
62 struct proto_shell_state {
63 struct interface_proto_state proto;
64 struct proto_shell_handler *handler;
65 struct blob_attr *config;
67 struct uloop_timeout teardown_timeout;
69 struct netifd_process script_task;
70 struct netifd_process proto_task;
72 enum proto_shell_sm sm;
73 bool proto_task_killed;
78 struct list_head deps;
82 proto_shell_check_dependencies(struct proto_shell_state *state)
84 struct proto_shell_dependency *dep;
85 bool available = true;
87 list_for_each_entry(dep, &state->deps, list) {
95 interface_set_available(state->proto.iface, available);
99 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
100 enum interface_event ev);
102 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
103 enum interface_event ev);
106 proto_shell_update_host_dep(struct proto_shell_dependency *dep)
108 struct interface *iface = NULL;
113 if (dep->interface[0])
114 iface = vlist_find(&interfaces, dep->interface, iface, node);
116 iface = interface_ip_add_target_route(&dep->host, dep->v6, iface);
120 interface_remove_user(&dep->dep);
121 dep->dep.cb = proto_shell_if_down_cb;
122 interface_add_user(&dep->dep, iface);
125 proto_shell_check_dependencies(dep->proto);
129 proto_shell_clear_host_dep(struct proto_shell_state *state)
131 struct proto_shell_dependency *dep, *tmp;
133 list_for_each_entry_safe(dep, tmp, &state->deps, list) {
134 interface_remove_user(&dep->dep);
135 list_del(&dep->list);
141 proto_shell_handler(struct interface_proto_state *proto,
142 enum interface_proto_cmd cmd, bool force)
144 struct proto_shell_state *state;
145 struct proto_shell_handler *handler;
146 struct netifd_process *proc;
147 static char error_buf[32];
152 int ret, i = 0, j = 0;
154 state = container_of(proto, struct proto_shell_state, proto);
155 handler = state->handler;
156 proc = &state->script_task;
158 if (cmd == PROTO_CMD_SETUP) {
160 state->last_error = -1;
161 proto_shell_clear_host_dep(state);
163 } else if (cmd == PROTO_CMD_RENEW) {
164 if (!(handler->proto.flags & PROTO_FLAG_RENEW_AVAILABLE))
167 if (state->script_task.uloop.pending) {
168 state->renew_pending = true;
172 state->renew_pending = false;
175 if (state->sm == S_TEARDOWN)
178 state->renew_pending = false;
179 if (state->script_task.uloop.pending) {
180 if (state->sm != S_SETUP_ABORT) {
181 uloop_timeout_set(&state->teardown_timeout, 1000);
182 kill(state->script_task.uloop.pid, SIGTERM);
183 if (state->proto_task.uloop.pending)
184 kill(state->proto_task.uloop.pid, SIGTERM);
185 state->sm = S_SETUP_ABORT;
191 state->sm = S_TEARDOWN;
192 if (state->last_error >= 0) {
193 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
194 envp[j++] = error_buf;
196 uloop_timeout_set(&state->teardown_timeout, 5000);
199 D(INTERFACE, "run %s for interface '%s'\n", action, proto->iface->name);
200 config = blobmsg_format_json(state->config, true);
204 argv[i++] = handler->script_name;
205 argv[i++] = handler->proto.name;
207 argv[i++] = proto->iface->name;
209 if (proto->iface->main_dev.dev)
210 argv[i++] = proto->iface->main_dev.dev->ifname;
214 ret = netifd_start_process(argv, envp, proc);
221 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
222 enum interface_event ev)
224 struct proto_shell_dependency *pdep;
226 if (ev != IFEV_UP && ev != IFEV_UPDATE)
229 pdep = container_of(dep, struct proto_shell_dependency, dep);
230 proto_shell_update_host_dep(pdep);
234 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
235 enum interface_event ev)
237 struct proto_shell_dependency *pdep;
238 struct proto_shell_state *state;
240 if (ev == IFEV_UP || ev == IFEV_UPDATE)
243 pdep = container_of(dep, struct proto_shell_dependency, dep);
244 interface_remove_user(dep);
245 dep->cb = proto_shell_if_up_cb;
246 interface_add_user(dep, NULL);
249 if (state->sm == S_IDLE) {
250 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
251 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
256 proto_shell_task_finish(struct proto_shell_state *state,
257 struct netifd_process *task)
261 if (task == &state->proto_task)
262 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
265 if (task == &state->proto_task)
266 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN,
268 if (task == &state->script_task && state->renew_pending)
269 proto_shell_handler(&state->proto, PROTO_CMD_RENEW,
274 if (state->script_task.uloop.pending ||
275 state->proto_task.uloop.pending)
278 uloop_timeout_cancel(&state->teardown_timeout);
280 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
284 if (state->script_task.uloop.pending)
287 if (state->proto_task.uloop.pending) {
288 if (!state->proto_task_killed)
289 kill(state->proto_task.uloop.pid, SIGTERM);
293 uloop_timeout_cancel(&state->teardown_timeout);
295 state->proto.proto_event(&state->proto, IFPEV_DOWN);
301 proto_shell_teardown_timeout_cb(struct uloop_timeout *timeout)
303 struct proto_shell_state *state;
305 state = container_of(timeout, struct proto_shell_state, teardown_timeout);
307 netifd_kill_process(&state->script_task);
308 netifd_kill_process(&state->proto_task);
309 proto_shell_task_finish(state, NULL);
313 proto_shell_script_cb(struct netifd_process *p, int ret)
315 struct proto_shell_state *state;
317 state = container_of(p, struct proto_shell_state, script_task);
318 proto_shell_task_finish(state, p);
322 proto_shell_task_cb(struct netifd_process *p, int ret)
324 struct proto_shell_state *state;
326 state = container_of(p, struct proto_shell_state, proto_task);
328 if (state->sm == S_IDLE || state->sm == S_SETUP)
329 state->last_error = WEXITSTATUS(ret);
331 proto_shell_task_finish(state, p);
335 proto_shell_free(struct interface_proto_state *proto)
337 struct proto_shell_state *state;
339 state = container_of(proto, struct proto_shell_state, proto);
340 uloop_timeout_cancel(&state->teardown_timeout);
341 proto_shell_clear_host_dep(state);
342 netifd_kill_process(&state->script_task);
343 netifd_kill_process(&state->proto_task);
349 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
352 struct blob_attr *cur;
355 blobmsg_for_each_attr(cur, attr, rem) {
356 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
357 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
361 interface_ip_add_route(iface, cur, v6);
366 proto_shell_parse_data(struct interface *iface, struct blob_attr *attr)
368 struct blob_attr *cur;
371 blobmsg_for_each_attr(cur, attr, rem)
372 interface_add_data(iface, cur);
375 static struct device *
376 proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
381 memset(&b, 0, sizeof(b));
382 blob_buf_init(&b, 0);
383 blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
384 dev = device_create(name, &tunnel_device_type, blob_data(b.head));
411 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
412 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
413 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
414 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
415 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
416 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
417 [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
418 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
419 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
420 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
421 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
422 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
423 [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
424 [NOTIFY_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
425 [NOTIFY_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
426 [NOTIFY_HOST] = { .name = "host", .type = BLOBMSG_TYPE_STRING },
427 [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
428 [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
432 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
434 struct interface *iface = state->proto.iface;
435 struct blob_attr *cur;
439 bool addr_ext = false;
443 if (!tb[NOTIFY_LINK_UP])
444 return UBUS_STATUS_INVALID_ARGUMENT;
446 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
448 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
452 if ((cur = tb[NOTIFY_KEEP]) != NULL)
453 keep = blobmsg_get_bool(cur);
455 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
456 addr_ext = blobmsg_get_bool(cur);
461 if (iface->state != IFS_UP || !iface->l3_dev.dev)
465 dev = iface->main_dev.dev;
466 if (tb[NOTIFY_IFNAME]) {
468 devname = blobmsg_data(tb[NOTIFY_IFNAME]);
469 if (tb[NOTIFY_TUNNEL])
470 dev = proto_shell_create_tunnel(devname, tb[NOTIFY_TUNNEL]);
472 dev = device_get(devname, dev_create);
476 return UBUS_STATUS_INVALID_ARGUMENT;
478 interface_set_l3_dev(iface, dev);
479 if (device_claim(&iface->l3_dev) < 0)
480 return UBUS_STATUS_UNKNOWN_ERROR;
482 device_set_present(dev, true);
484 interface_update_start(iface);
487 proto_apply_ip_settings(iface, data, addr_ext);
489 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
490 proto_shell_parse_route_list(state->proto.iface, cur, false);
492 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
493 proto_shell_parse_route_list(state->proto.iface, cur, true);
495 if ((cur = tb[NOTIFY_DNS]))
496 interface_add_dns_server_list(&iface->proto_ip, cur);
498 if ((cur = tb[NOTIFY_DNS_SEARCH]))
499 interface_add_dns_search_list(&iface->proto_ip, cur);
501 if ((cur = tb[NOTIFY_DATA]))
502 proto_shell_parse_data(state->proto.iface, cur);
504 interface_update_complete(state->proto.iface);
507 state->proto.proto_event(&state->proto, IFPEV_UP);
514 fill_string_list(struct blob_attr *attr, char **argv, int max)
516 struct blob_attr *cur;
523 blobmsg_for_each_attr(cur, attr, rem) {
524 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
527 if (!blobmsg_check_attr(cur, NULL))
530 argv[argc++] = blobmsg_data(cur);
541 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
543 static char *argv[64];
544 static char *env[32];
546 if (!tb[NOTIFY_COMMAND])
549 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
552 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
555 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
560 return UBUS_STATUS_INVALID_ARGUMENT;
564 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
566 unsigned int signal = ~0;
568 if (tb[NOTIFY_SIGNAL])
569 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
574 if (state->proto_task.uloop.pending) {
575 state->proto_task_killed = true;
576 kill(state->proto_task.uloop.pid, signal);
583 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
585 struct blob_attr *cur;
590 if (!tb[NOTIFY_ERROR])
591 return UBUS_STATUS_INVALID_ARGUMENT;
593 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
594 if (n_data + 1 == ARRAY_SIZE(data))
597 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
600 if (!blobmsg_check_attr(cur, NULL))
603 data[n_data++] = blobmsg_data(cur);
609 interface_add_error(state->proto.iface, state->handler->proto.name,
610 data[0], (const char **) &data[1], n_data - 1);
615 return UBUS_STATUS_INVALID_ARGUMENT;
619 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
621 state->proto.iface->autostart = false;
626 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
628 if (!tb[NOTIFY_AVAILABLE])
629 return UBUS_STATUS_INVALID_ARGUMENT;
631 interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
636 proto_shell_add_host_dependency(struct proto_shell_state *state, struct blob_attr **tb)
638 struct proto_shell_dependency *dep;
639 struct blob_attr *host = tb[NOTIFY_HOST];
640 struct blob_attr *ifname_a = tb[NOTIFY_IFNAME];
641 const char *ifname_str = ifname_a ? blobmsg_data(ifname_a) : "";
645 return UBUS_STATUS_INVALID_ARGUMENT;
647 dep = calloc_a(sizeof(*dep), &ifname, strlen(ifname_str) + 1);
648 if (inet_pton(AF_INET, blobmsg_data(host), &dep->host) < 1) {
649 if (inet_pton(AF_INET6, blobmsg_data(host), &dep->host) < 1) {
651 return UBUS_STATUS_INVALID_ARGUMENT;
658 dep->interface = strcpy(ifname, ifname_str);
660 dep->dep.cb = proto_shell_if_up_cb;
661 interface_add_user(&dep->dep, NULL);
662 list_add(&dep->list, &state->deps);
663 proto_shell_update_host_dep(dep);
665 return UBUS_STATUS_NOT_FOUND;
671 proto_shell_setup_failed(struct proto_shell_state *state)
675 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
678 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
687 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
689 struct proto_shell_state *state;
690 struct blob_attr *tb[__NOTIFY_LAST];
692 state = container_of(proto, struct proto_shell_state, proto);
694 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
695 if (!tb[NOTIFY_ACTION])
696 return UBUS_STATUS_INVALID_ARGUMENT;
698 if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
699 return UBUS_STATUS_PERMISSION_DENIED;
701 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
703 return proto_shell_update_link(state, attr, tb);
705 return proto_shell_run_command(state, tb);
707 return proto_shell_kill_command(state, tb);
709 return proto_shell_notify_error(state, tb);
711 return proto_shell_block_restart(state, tb);
713 return proto_shell_set_available(state, tb);
715 return proto_shell_add_host_dependency(state, tb);
717 return proto_shell_setup_failed(state);
719 return UBUS_STATUS_INVALID_ARGUMENT;
723 static struct interface_proto_state *
724 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
725 struct blob_attr *attr)
727 struct proto_shell_state *state;
729 state = calloc(1, sizeof(*state));
730 INIT_LIST_HEAD(&state->deps);
732 state->config = malloc(blob_pad_len(attr));
736 memcpy(state->config, attr, blob_pad_len(attr));
737 state->proto.free = proto_shell_free;
738 state->proto.notify = proto_shell_notify;
739 state->proto.cb = proto_shell_handler;
740 state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
741 state->script_task.cb = proto_shell_script_cb;
742 state->script_task.dir_fd = proto_fd;
743 state->script_task.log_prefix = iface->name;
744 state->proto_task.cb = proto_shell_task_cb;
745 state->proto_task.dir_fd = proto_fd;
746 state->proto_task.log_prefix = iface->name;
747 state->handler = container_of(h, struct proto_shell_handler, proto);
749 return &state->proto;
757 proto_shell_add_handler(const char *script, const char *name, json_object *obj)
759 struct proto_shell_handler *handler;
760 struct proto_handler *proto;
761 json_object *config, *tmp;
762 char *proto_name, *script_name;
764 handler = calloc_a(sizeof(*handler),
765 &proto_name, strlen(name) + 1,
766 &script_name, strlen(script) + 1);
770 handler->script_name = strcpy(script_name, script);
772 proto = &handler->proto;
773 proto->name = strcpy(proto_name, name);
774 proto->config_params = &handler->config;
775 proto->attach = proto_shell_attach;
777 tmp = json_get_field(obj, "no-device", json_type_boolean);
778 if (tmp && json_object_get_boolean(tmp))
779 handler->proto.flags |= PROTO_FLAG_NODEV;
781 tmp = json_get_field(obj, "available", json_type_boolean);
782 if (tmp && json_object_get_boolean(tmp))
783 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
785 tmp = json_get_field(obj, "renew-handler", json_type_boolean);
786 if (tmp && json_object_get_boolean(tmp))
787 handler->proto.flags |= PROTO_FLAG_RENEW_AVAILABLE;
789 config = json_get_field(obj, "config", json_type_array);
791 handler->config_buf = netifd_handler_parse_config(&handler->config, config);
793 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
794 add_proto_handler(proto);
797 void proto_shell_init(void)
799 proto_fd = netifd_open_subdir("proto");
803 netifd_init_script_handlers(proto_fd, proto_shell_add_handler);