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) {
162 state->last_error = -1;
163 proto_shell_clear_host_dep(state);
175 } else if (cmd == PROTO_CMD_RENEW) {
176 if (!(handler->proto.flags & PROTO_FLAG_RENEW_AVAILABLE))
179 if (state->script_task.uloop.pending) {
180 state->renew_pending = true;
184 state->renew_pending = false;
189 if (state->script_task.uloop.pending) {
190 uloop_timeout_set(&state->teardown_timeout, 1000);
191 kill(state->script_task.uloop.pid, SIGTERM);
192 if (state->proto_task.uloop.pending)
193 kill(state->proto_task.uloop.pid, SIGTERM);
194 state->renew_pending = false;
195 state->sm = S_SETUP_ABORT;
198 /* fall through if no script task is running */
201 state->renew_pending = false;
202 state->sm = S_TEARDOWN;
203 if (state->last_error >= 0) {
204 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
205 envp[j++] = error_buf;
207 uloop_timeout_set(&state->teardown_timeout, 5000);
218 D(INTERFACE, "run %s for interface '%s'\n", action, proto->iface->name);
219 config = blobmsg_format_json(state->config, true);
223 argv[i++] = handler->script_name;
224 argv[i++] = handler->proto.name;
226 argv[i++] = proto->iface->name;
228 if (proto->iface->main_dev.dev)
229 argv[i++] = proto->iface->main_dev.dev->ifname;
233 ret = netifd_start_process(argv, envp, proc);
240 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
241 enum interface_event ev)
243 struct proto_shell_dependency *pdep;
245 if (ev != IFEV_UP && ev != IFEV_UPDATE)
248 pdep = container_of(dep, struct proto_shell_dependency, dep);
249 proto_shell_update_host_dep(pdep);
253 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
254 enum interface_event ev)
256 struct proto_shell_dependency *pdep;
257 struct proto_shell_state *state;
259 if (ev == IFEV_UP || ev == IFEV_UPDATE)
262 pdep = container_of(dep, struct proto_shell_dependency, dep);
263 interface_remove_user(dep);
264 dep->cb = proto_shell_if_up_cb;
265 interface_add_user(dep, NULL);
268 if (state->sm == S_IDLE) {
269 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
270 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
275 proto_shell_task_finish(struct proto_shell_state *state,
276 struct netifd_process *task)
280 if (task == &state->proto_task)
281 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
284 if (task == &state->proto_task)
285 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN,
287 if (task == &state->script_task && state->renew_pending)
288 proto_shell_handler(&state->proto, PROTO_CMD_RENEW,
293 if (state->script_task.uloop.pending ||
294 state->proto_task.uloop.pending)
297 uloop_timeout_cancel(&state->teardown_timeout);
299 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
303 if (state->script_task.uloop.pending)
306 if (state->proto_task.uloop.pending) {
307 if (!state->proto_task_killed)
308 kill(state->proto_task.uloop.pid, SIGTERM);
312 uloop_timeout_cancel(&state->teardown_timeout);
314 state->proto.proto_event(&state->proto, IFPEV_DOWN);
320 proto_shell_teardown_timeout_cb(struct uloop_timeout *timeout)
322 struct proto_shell_state *state;
324 state = container_of(timeout, struct proto_shell_state, teardown_timeout);
326 netifd_kill_process(&state->script_task);
327 netifd_kill_process(&state->proto_task);
328 proto_shell_task_finish(state, NULL);
332 proto_shell_script_cb(struct netifd_process *p, int ret)
334 struct proto_shell_state *state;
336 state = container_of(p, struct proto_shell_state, script_task);
337 proto_shell_task_finish(state, p);
341 proto_shell_task_cb(struct netifd_process *p, int ret)
343 struct proto_shell_state *state;
345 state = container_of(p, struct proto_shell_state, proto_task);
347 if (state->sm == S_IDLE || state->sm == S_SETUP)
348 state->last_error = WEXITSTATUS(ret);
350 proto_shell_task_finish(state, p);
354 proto_shell_free(struct interface_proto_state *proto)
356 struct proto_shell_state *state;
358 state = container_of(proto, struct proto_shell_state, proto);
359 uloop_timeout_cancel(&state->teardown_timeout);
360 proto_shell_clear_host_dep(state);
361 netifd_kill_process(&state->script_task);
362 netifd_kill_process(&state->proto_task);
368 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
371 struct blob_attr *cur;
374 blobmsg_for_each_attr(cur, attr, rem) {
375 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
376 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
380 interface_ip_add_route(iface, cur, v6);
385 proto_shell_parse_data(struct interface *iface, struct blob_attr *attr)
387 struct blob_attr *cur;
390 blobmsg_for_each_attr(cur, attr, rem)
391 interface_add_data(iface, cur);
394 static struct device *
395 proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
400 memset(&b, 0, sizeof(b));
401 blob_buf_init(&b, 0);
402 blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
403 dev = device_create(name, &tunnel_device_type, blob_data(b.head));
430 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
431 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
432 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
433 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
434 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
435 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
436 [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
437 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
438 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
439 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
440 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
441 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
442 [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
443 [NOTIFY_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
444 [NOTIFY_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
445 [NOTIFY_HOST] = { .name = "host", .type = BLOBMSG_TYPE_STRING },
446 [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
447 [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
451 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
453 struct interface *iface = state->proto.iface;
454 struct blob_attr *cur;
458 bool addr_ext = false;
462 if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
463 return UBUS_STATUS_PERMISSION_DENIED;
465 if (!tb[NOTIFY_LINK_UP])
466 return UBUS_STATUS_INVALID_ARGUMENT;
468 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
470 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
474 if ((cur = tb[NOTIFY_KEEP]) != NULL)
475 keep = blobmsg_get_bool(cur);
477 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
478 addr_ext = blobmsg_get_bool(cur);
483 if (iface->state != IFS_UP || !iface->l3_dev.dev)
487 dev = iface->main_dev.dev;
488 if (tb[NOTIFY_IFNAME]) {
490 devname = blobmsg_data(tb[NOTIFY_IFNAME]);
491 if (tb[NOTIFY_TUNNEL])
492 dev = proto_shell_create_tunnel(devname, tb[NOTIFY_TUNNEL]);
494 dev = device_get(devname, dev_create);
498 return UBUS_STATUS_INVALID_ARGUMENT;
500 interface_set_l3_dev(iface, dev);
501 if (device_claim(&iface->l3_dev) < 0)
502 return UBUS_STATUS_UNKNOWN_ERROR;
504 device_set_present(dev, true);
506 interface_update_start(iface);
509 proto_apply_ip_settings(iface, data, addr_ext);
511 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
512 proto_shell_parse_route_list(state->proto.iface, cur, false);
514 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
515 proto_shell_parse_route_list(state->proto.iface, cur, true);
517 if ((cur = tb[NOTIFY_DNS]))
518 interface_add_dns_server_list(&iface->proto_ip, cur);
520 if ((cur = tb[NOTIFY_DNS_SEARCH]))
521 interface_add_dns_search_list(&iface->proto_ip, cur);
523 if ((cur = tb[NOTIFY_DATA]))
524 proto_shell_parse_data(state->proto.iface, cur);
526 interface_update_complete(state->proto.iface);
528 if ((state->sm != S_SETUP_ABORT) && (state->sm != S_TEARDOWN)) {
530 state->proto.proto_event(&state->proto, IFPEV_UP);
538 fill_string_list(struct blob_attr *attr, char **argv, int max)
540 struct blob_attr *cur;
547 blobmsg_for_each_attr(cur, attr, rem) {
548 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
551 if (!blobmsg_check_attr(cur, NULL))
554 argv[argc++] = blobmsg_data(cur);
565 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
567 static char *argv[64];
568 static char *env[32];
570 if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
571 return UBUS_STATUS_PERMISSION_DENIED;
573 if (!tb[NOTIFY_COMMAND])
576 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
579 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
582 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
587 return UBUS_STATUS_INVALID_ARGUMENT;
591 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
593 unsigned int signal = ~0;
595 if (tb[NOTIFY_SIGNAL])
596 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
601 if (state->proto_task.uloop.pending) {
602 if (signal == SIGTERM || signal == SIGKILL)
603 state->proto_task_killed = true;
604 kill(state->proto_task.uloop.pid, signal);
611 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
613 struct blob_attr *cur;
618 if (!tb[NOTIFY_ERROR])
619 return UBUS_STATUS_INVALID_ARGUMENT;
621 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
622 if (n_data + 1 == ARRAY_SIZE(data))
625 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
628 if (!blobmsg_check_attr(cur, NULL))
631 data[n_data++] = blobmsg_data(cur);
637 interface_add_error(state->proto.iface, state->handler->proto.name,
638 data[0], (const char **) &data[1], n_data - 1);
643 return UBUS_STATUS_INVALID_ARGUMENT;
647 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
649 state->proto.iface->autostart = false;
654 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
656 if (!tb[NOTIFY_AVAILABLE])
657 return UBUS_STATUS_INVALID_ARGUMENT;
659 interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
664 proto_shell_add_host_dependency(struct proto_shell_state *state, struct blob_attr **tb)
666 struct proto_shell_dependency *dep;
667 struct blob_attr *host = tb[NOTIFY_HOST];
668 struct blob_attr *ifname_a = tb[NOTIFY_IFNAME];
669 const char *ifname_str = ifname_a ? blobmsg_data(ifname_a) : "";
672 if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
673 return UBUS_STATUS_PERMISSION_DENIED;
676 return UBUS_STATUS_INVALID_ARGUMENT;
678 dep = calloc_a(sizeof(*dep), &ifname, strlen(ifname_str) + 1);
679 if (inet_pton(AF_INET, blobmsg_data(host), &dep->host) < 1) {
680 if (inet_pton(AF_INET6, blobmsg_data(host), &dep->host) < 1) {
682 return UBUS_STATUS_INVALID_ARGUMENT;
689 dep->interface = strcpy(ifname, ifname_str);
691 dep->dep.cb = proto_shell_if_up_cb;
692 interface_add_user(&dep->dep, NULL);
693 list_add(&dep->list, &state->deps);
694 proto_shell_update_host_dep(dep);
696 return UBUS_STATUS_NOT_FOUND;
702 proto_shell_setup_failed(struct proto_shell_state *state)
708 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
711 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
716 ret = UBUS_STATUS_PERMISSION_DENIED;
723 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
725 struct proto_shell_state *state;
726 struct blob_attr *tb[__NOTIFY_LAST];
728 state = container_of(proto, struct proto_shell_state, proto);
730 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
731 if (!tb[NOTIFY_ACTION])
732 return UBUS_STATUS_INVALID_ARGUMENT;
734 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
736 return proto_shell_update_link(state, attr, tb);
738 return proto_shell_run_command(state, tb);
740 return proto_shell_kill_command(state, tb);
742 return proto_shell_notify_error(state, tb);
744 return proto_shell_block_restart(state, tb);
746 return proto_shell_set_available(state, tb);
748 return proto_shell_add_host_dependency(state, tb);
750 return proto_shell_setup_failed(state);
752 return UBUS_STATUS_INVALID_ARGUMENT;
756 static struct interface_proto_state *
757 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
758 struct blob_attr *attr)
760 struct proto_shell_state *state;
762 state = calloc(1, sizeof(*state));
763 INIT_LIST_HEAD(&state->deps);
765 state->config = malloc(blob_pad_len(attr));
769 memcpy(state->config, attr, blob_pad_len(attr));
770 state->proto.free = proto_shell_free;
771 state->proto.notify = proto_shell_notify;
772 state->proto.cb = proto_shell_handler;
773 state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
774 state->script_task.cb = proto_shell_script_cb;
775 state->script_task.dir_fd = proto_fd;
776 state->script_task.log_prefix = iface->name;
777 state->proto_task.cb = proto_shell_task_cb;
778 state->proto_task.dir_fd = proto_fd;
779 state->proto_task.log_prefix = iface->name;
780 state->handler = container_of(h, struct proto_shell_handler, proto);
782 return &state->proto;
790 proto_shell_add_handler(const char *script, const char *name, json_object *obj)
792 struct proto_shell_handler *handler;
793 struct proto_handler *proto;
794 json_object *config, *tmp;
795 char *proto_name, *script_name;
797 handler = calloc_a(sizeof(*handler),
798 &proto_name, strlen(name) + 1,
799 &script_name, strlen(script) + 1);
803 handler->script_name = strcpy(script_name, script);
805 proto = &handler->proto;
806 proto->name = strcpy(proto_name, name);
807 proto->config_params = &handler->config;
808 proto->attach = proto_shell_attach;
810 tmp = json_get_field(obj, "no-device", json_type_boolean);
811 if (tmp && json_object_get_boolean(tmp))
812 handler->proto.flags |= PROTO_FLAG_NODEV;
814 tmp = json_get_field(obj, "available", json_type_boolean);
815 if (tmp && json_object_get_boolean(tmp))
816 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
818 tmp = json_get_field(obj, "renew-handler", json_type_boolean);
819 if (tmp && json_object_get_boolean(tmp))
820 handler->proto.flags |= PROTO_FLAG_RENEW_AVAILABLE;
822 config = json_get_field(obj, "config", json_type_array);
824 handler->config_buf = netifd_handler_parse_config(&handler->config, config);
826 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
827 add_proto_handler(proto);
830 void proto_shell_init(void)
832 proto_fd = netifd_open_subdir("proto");
836 netifd_init_script_handlers(proto_fd, proto_shell_add_handler);