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;
77 struct list_head deps;
81 proto_shell_check_dependencies(struct proto_shell_state *state)
83 struct proto_shell_dependency *dep;
84 bool available = true;
86 list_for_each_entry(dep, &state->deps, list) {
94 interface_set_available(state->proto.iface, available);
98 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
99 enum interface_event ev);
101 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
102 enum interface_event ev);
105 proto_shell_update_host_dep(struct proto_shell_dependency *dep)
107 struct interface *iface = NULL;
112 if (dep->interface[0])
113 iface = vlist_find(&interfaces, dep->interface, iface, node);
115 iface = interface_ip_add_target_route(&dep->host, dep->v6, iface);
119 interface_remove_user(&dep->dep);
120 dep->dep.cb = proto_shell_if_down_cb;
121 interface_add_user(&dep->dep, iface);
124 proto_shell_check_dependencies(dep->proto);
128 proto_shell_clear_host_dep(struct proto_shell_state *state)
130 struct proto_shell_dependency *dep, *tmp;
132 list_for_each_entry_safe(dep, tmp, &state->deps, list) {
133 interface_remove_user(&dep->dep);
134 list_del(&dep->list);
140 proto_shell_handler(struct interface_proto_state *proto,
141 enum interface_proto_cmd cmd, bool force)
143 struct proto_shell_state *state;
144 struct proto_shell_handler *handler;
145 struct netifd_process *proc;
146 static char error_buf[32];
151 int ret, i = 0, j = 0;
153 state = container_of(proto, struct proto_shell_state, proto);
154 handler = state->handler;
155 proc = &state->script_task;
157 if (cmd == PROTO_CMD_SETUP) {
159 state->last_error = -1;
160 proto_shell_clear_host_dep(state);
162 if (state->sm == S_TEARDOWN)
165 if (state->script_task.uloop.pending) {
166 if (state->sm != S_SETUP_ABORT) {
167 uloop_timeout_set(&state->teardown_timeout, 1000);
168 kill(state->script_task.uloop.pid, SIGTERM);
169 if (state->proto_task.uloop.pending)
170 kill(state->proto_task.uloop.pid, SIGTERM);
171 state->sm = S_SETUP_ABORT;
177 state->sm = S_TEARDOWN;
178 if (state->last_error >= 0) {
179 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
180 envp[j++] = error_buf;
182 uloop_timeout_set(&state->teardown_timeout, 5000);
185 config = blobmsg_format_json(state->config, true);
189 argv[i++] = handler->script_name;
190 argv[i++] = handler->proto.name;
192 argv[i++] = proto->iface->name;
194 if (proto->iface->main_dev.dev)
195 argv[i++] = proto->iface->main_dev.dev->ifname;
199 ret = netifd_start_process(argv, envp, proc);
206 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
207 enum interface_event ev)
209 struct proto_shell_dependency *pdep;
211 if (ev != IFEV_UP && ev != IFEV_UPDATE)
214 pdep = container_of(dep, struct proto_shell_dependency, dep);
215 proto_shell_update_host_dep(pdep);
219 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
220 enum interface_event ev)
222 struct proto_shell_dependency *pdep;
223 struct proto_shell_state *state;
225 if (ev == IFEV_UP || ev == IFEV_UPDATE)
228 pdep = container_of(dep, struct proto_shell_dependency, dep);
229 interface_remove_user(dep);
230 dep->cb = proto_shell_if_up_cb;
231 interface_add_user(dep, NULL);
234 if (state->sm == S_IDLE) {
235 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
236 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
241 proto_shell_task_finish(struct proto_shell_state *state,
242 struct netifd_process *task)
246 if (task == &state->proto_task)
247 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
250 if (task == &state->proto_task)
251 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN,
256 if (state->script_task.uloop.pending ||
257 state->proto_task.uloop.pending)
260 uloop_timeout_cancel(&state->teardown_timeout);
262 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
266 if (state->script_task.uloop.pending)
269 if (state->proto_task.uloop.pending) {
270 if (!state->proto_task_killed)
271 kill(state->proto_task.uloop.pid, SIGTERM);
275 uloop_timeout_cancel(&state->teardown_timeout);
277 state->proto.proto_event(&state->proto, IFPEV_DOWN);
283 proto_shell_teardown_timeout_cb(struct uloop_timeout *timeout)
285 struct proto_shell_state *state;
287 state = container_of(timeout, struct proto_shell_state, teardown_timeout);
289 netifd_kill_process(&state->script_task);
290 netifd_kill_process(&state->proto_task);
291 proto_shell_task_finish(state, NULL);
295 proto_shell_script_cb(struct netifd_process *p, int ret)
297 struct proto_shell_state *state;
299 state = container_of(p, struct proto_shell_state, script_task);
300 proto_shell_task_finish(state, p);
304 proto_shell_task_cb(struct netifd_process *p, int ret)
306 struct proto_shell_state *state;
308 state = container_of(p, struct proto_shell_state, proto_task);
310 if (state->sm == S_IDLE || state->sm == S_SETUP)
311 state->last_error = WEXITSTATUS(ret);
313 proto_shell_task_finish(state, p);
317 proto_shell_free(struct interface_proto_state *proto)
319 struct proto_shell_state *state;
321 state = container_of(proto, struct proto_shell_state, proto);
322 uloop_timeout_cancel(&state->teardown_timeout);
323 proto_shell_clear_host_dep(state);
324 netifd_kill_process(&state->script_task);
325 netifd_kill_process(&state->proto_task);
331 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
334 struct blob_attr *cur;
337 blobmsg_for_each_attr(cur, attr, rem) {
338 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
339 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
343 interface_ip_add_route(iface, cur, v6);
348 proto_shell_parse_data(struct interface *iface, struct blob_attr *attr)
350 struct blob_attr *cur;
353 blobmsg_for_each_attr(cur, attr, rem)
354 interface_add_data(iface, cur);
357 static struct device *
358 proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
363 memset(&b, 0, sizeof(b));
364 blob_buf_init(&b, 0);
365 blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
366 dev = device_create(name, &tunnel_device_type, blob_data(b.head));
393 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
394 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
395 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
396 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
397 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
398 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
399 [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
400 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
401 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
402 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
403 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
404 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
405 [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
406 [NOTIFY_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
407 [NOTIFY_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
408 [NOTIFY_HOST] = { .name = "host", .type = BLOBMSG_TYPE_STRING },
409 [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
410 [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
414 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
416 struct interface *iface = state->proto.iface;
417 struct blob_attr *cur;
421 bool addr_ext = false;
425 if (!tb[NOTIFY_LINK_UP])
426 return UBUS_STATUS_INVALID_ARGUMENT;
428 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
430 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
434 if ((cur = tb[NOTIFY_KEEP]) != NULL)
435 keep = blobmsg_get_bool(cur);
437 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
438 addr_ext = blobmsg_get_bool(cur);
443 if (!tb[NOTIFY_IFNAME]) {
444 if (!iface->main_dev.dev)
445 return UBUS_STATUS_INVALID_ARGUMENT;
446 } else if (!keep || iface->state != IFS_UP) {
448 devname = blobmsg_data(tb[NOTIFY_IFNAME]);
449 if (tb[NOTIFY_TUNNEL]) {
450 dev = proto_shell_create_tunnel(devname,
453 return UBUS_STATUS_INVALID_ARGUMENT;
455 dev = device_get(devname, dev_create);
457 return UBUS_STATUS_NOT_FOUND;
460 interface_set_l3_dev(iface, dev);
461 device_claim(&iface->l3_dev);
462 device_set_present(dev, true);
466 interface_update_start(iface);
468 proto_apply_ip_settings(iface, data, addr_ext);
470 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
471 proto_shell_parse_route_list(state->proto.iface, cur, false);
473 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
474 proto_shell_parse_route_list(state->proto.iface, cur, true);
476 if ((cur = tb[NOTIFY_DNS]))
477 interface_add_dns_server_list(&iface->proto_ip, cur);
479 if ((cur = tb[NOTIFY_DNS_SEARCH]))
480 interface_add_dns_search_list(&iface->proto_ip, cur);
482 if ((cur = tb[NOTIFY_DATA]))
483 proto_shell_parse_data(state->proto.iface, cur);
485 interface_update_complete(state->proto.iface);
488 state->proto.proto_event(&state->proto, IFPEV_UP);
495 fill_string_list(struct blob_attr *attr, char **argv, int max)
497 struct blob_attr *cur;
504 blobmsg_for_each_attr(cur, attr, rem) {
505 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
508 if (!blobmsg_check_attr(cur, NULL))
511 argv[argc++] = blobmsg_data(cur);
522 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
524 static char *argv[64];
525 static char *env[32];
527 if (!tb[NOTIFY_COMMAND])
530 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
533 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
536 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
541 return UBUS_STATUS_INVALID_ARGUMENT;
545 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
547 unsigned int signal = ~0;
549 if (tb[NOTIFY_SIGNAL])
550 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
555 if (state->proto_task.uloop.pending) {
556 state->proto_task_killed = true;
557 kill(state->proto_task.uloop.pid, signal);
564 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
566 struct blob_attr *cur;
571 if (!tb[NOTIFY_ERROR])
572 return UBUS_STATUS_INVALID_ARGUMENT;
574 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
575 if (n_data + 1 == ARRAY_SIZE(data))
578 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
581 if (!blobmsg_check_attr(cur, NULL))
584 data[n_data++] = blobmsg_data(cur);
590 interface_add_error(state->proto.iface, state->handler->proto.name,
591 data[0], (const char **) &data[1], n_data - 1);
596 return UBUS_STATUS_INVALID_ARGUMENT;
600 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
602 state->proto.iface->autostart = false;
607 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
609 if (!tb[NOTIFY_AVAILABLE])
610 return UBUS_STATUS_INVALID_ARGUMENT;
612 interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
617 proto_shell_add_host_dependency(struct proto_shell_state *state, struct blob_attr **tb)
619 struct proto_shell_dependency *dep;
620 struct blob_attr *host = tb[NOTIFY_HOST];
621 struct blob_attr *ifname_a = tb[NOTIFY_IFNAME];
622 const char *ifname_str = ifname_a ? blobmsg_data(ifname_a) : "";
626 return UBUS_STATUS_INVALID_ARGUMENT;
628 dep = calloc_a(sizeof(*dep), &ifname, strlen(ifname_str) + 1);
629 if (inet_pton(AF_INET, blobmsg_data(host), &dep->host) < 1) {
630 if (inet_pton(AF_INET6, blobmsg_data(host), &dep->host) < 1) {
632 return UBUS_STATUS_INVALID_ARGUMENT;
639 dep->interface = strcpy(ifname, ifname_str);
641 dep->dep.cb = proto_shell_if_up_cb;
642 interface_add_user(&dep->dep, NULL);
643 list_add(&dep->list, &state->deps);
644 proto_shell_update_host_dep(dep);
646 return UBUS_STATUS_NOT_FOUND;
652 proto_shell_setup_failed(struct proto_shell_state *state)
656 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
659 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
668 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
670 struct proto_shell_state *state;
671 struct blob_attr *tb[__NOTIFY_LAST];
673 state = container_of(proto, struct proto_shell_state, proto);
675 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
676 if (!tb[NOTIFY_ACTION])
677 return UBUS_STATUS_INVALID_ARGUMENT;
679 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
681 return proto_shell_update_link(state, attr, tb);
683 return proto_shell_run_command(state, tb);
685 return proto_shell_kill_command(state, tb);
687 return proto_shell_notify_error(state, tb);
689 return proto_shell_block_restart(state, tb);
691 return proto_shell_set_available(state, tb);
693 return proto_shell_add_host_dependency(state, tb);
695 return proto_shell_setup_failed(state);
697 return UBUS_STATUS_INVALID_ARGUMENT;
701 static struct interface_proto_state *
702 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
703 struct blob_attr *attr)
705 struct proto_shell_state *state;
707 state = calloc(1, sizeof(*state));
708 INIT_LIST_HEAD(&state->deps);
710 state->config = malloc(blob_pad_len(attr));
714 memcpy(state->config, attr, blob_pad_len(attr));
715 state->proto.free = proto_shell_free;
716 state->proto.notify = proto_shell_notify;
717 state->proto.cb = proto_shell_handler;
718 state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
719 state->script_task.cb = proto_shell_script_cb;
720 state->script_task.dir_fd = proto_fd;
721 state->script_task.log_prefix = iface->name;
722 state->proto_task.cb = proto_shell_task_cb;
723 state->proto_task.dir_fd = proto_fd;
724 state->proto_task.log_prefix = iface->name;
725 state->handler = container_of(h, struct proto_shell_handler, proto);
727 return &state->proto;
735 proto_shell_add_handler(const char *script, const char *name, json_object *obj)
737 struct proto_shell_handler *handler;
738 struct proto_handler *proto;
739 json_object *config, *tmp;
740 char *proto_name, *script_name;
742 handler = calloc_a(sizeof(*handler),
743 &proto_name, strlen(name) + 1,
744 &script_name, strlen(script) + 1);
748 handler->script_name = strcpy(script_name, script);
750 proto = &handler->proto;
751 proto->name = strcpy(proto_name, name);
752 proto->config_params = &handler->config;
753 proto->attach = proto_shell_attach;
755 tmp = json_get_field(obj, "no-device", json_type_boolean);
756 if (tmp && json_object_get_boolean(tmp))
757 handler->proto.flags |= PROTO_FLAG_NODEV;
759 tmp = json_get_field(obj, "available", json_type_boolean);
760 if (tmp && json_object_get_boolean(tmp))
761 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
763 config = json_get_field(obj, "config", json_type_array);
765 handler->config_buf = netifd_handler_parse_config(&handler->config, config);
767 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
768 add_proto_handler(proto);
771 static void __init proto_shell_init(void)
773 proto_fd = netifd_open_subdir("proto");
777 netifd_init_script_handlers(proto_fd, proto_shell_add_handler);