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.
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
27 #include <libubox/blobmsg_json.h>
30 #include "interface.h"
31 #include "interface-ip.h"
34 static struct netifd_fd proto_fd
;
43 struct proto_shell_handler
{
44 struct list_head list
;
45 struct proto_handler proto
;
46 struct config_param_list config
;
52 struct proto_shell_dependency
{
53 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
;
112 iface
= interface_ip_add_target_route(&dep
->host
, dep
->v6
);
116 interface_remove_user(&dep
->dep
);
117 dep
->dep
.cb
= proto_shell_if_down_cb
;
118 interface_add_user(&dep
->dep
, iface
);
121 proto_shell_check_dependencies(dep
->proto
);
125 proto_shell_clear_host_dep(struct proto_shell_state
*state
)
127 struct proto_shell_dependency
*dep
, *tmp
;
129 list_for_each_entry_safe(dep
, tmp
, &state
->deps
, list
) {
130 interface_remove_user(&dep
->dep
);
131 list_del(&dep
->list
);
137 proto_shell_handler(struct interface_proto_state
*proto
,
138 enum interface_proto_cmd cmd
, bool force
)
140 struct proto_shell_state
*state
;
141 struct proto_shell_handler
*handler
;
142 struct netifd_process
*proc
;
143 static char error_buf
[32];
148 int ret
, i
= 0, j
= 0;
150 state
= container_of(proto
, struct proto_shell_state
, proto
);
151 handler
= state
->handler
;
152 proc
= &state
->script_task
;
154 if (cmd
== PROTO_CMD_SETUP
) {
156 state
->last_error
= -1;
157 proto_shell_clear_host_dep(state
);
159 if (state
->sm
== S_TEARDOWN
)
162 if (state
->script_task
.uloop
.pending
) {
163 if (state
->sm
!= S_SETUP_ABORT
) {
164 uloop_timeout_set(&state
->teardown_timeout
, 1000);
165 kill(state
->script_task
.uloop
.pid
, SIGTERM
);
166 if (state
->proto_task
.uloop
.pending
)
167 kill(state
->proto_task
.uloop
.pid
, SIGTERM
);
168 state
->sm
= S_SETUP_ABORT
;
174 state
->sm
= S_TEARDOWN
;
175 if (state
->last_error
>= 0) {
176 snprintf(error_buf
, sizeof(error_buf
), "ERROR=%d", state
->last_error
);
177 envp
[j
++] = error_buf
;
179 uloop_timeout_set(&state
->teardown_timeout
, 5000);
182 config
= blobmsg_format_json(state
->config
, true);
186 argv
[i
++] = handler
->script_name
;
187 argv
[i
++] = handler
->proto
.name
;
189 argv
[i
++] = proto
->iface
->name
;
191 if (proto
->iface
->main_dev
.dev
)
192 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
196 ret
= netifd_start_process(argv
, envp
, proc
);
203 proto_shell_if_up_cb(struct interface_user
*dep
, struct interface
*iface
,
204 enum interface_event ev
)
206 struct proto_shell_dependency
*pdep
;
211 pdep
= container_of(dep
, struct proto_shell_dependency
, dep
);
212 proto_shell_update_host_dep(pdep
);
216 proto_shell_if_down_cb(struct interface_user
*dep
, struct interface
*iface
,
217 enum interface_event ev
)
219 struct proto_shell_dependency
*pdep
;
220 struct proto_shell_state
*state
;
225 pdep
= container_of(dep
, struct proto_shell_dependency
, dep
);
226 interface_remove_user(dep
);
227 dep
->cb
= proto_shell_if_up_cb
;
228 interface_add_user(dep
, NULL
);
231 if (state
->sm
== S_IDLE
) {
232 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
233 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
238 proto_shell_task_finish(struct proto_shell_state
*state
,
239 struct netifd_process
*task
)
243 if (task
== &state
->proto_task
)
244 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
247 if (task
== &state
->proto_task
)
248 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
,
253 if (state
->script_task
.uloop
.pending
||
254 state
->proto_task
.uloop
.pending
)
257 uloop_timeout_cancel(&state
->teardown_timeout
);
259 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
263 if (state
->script_task
.uloop
.pending
)
266 if (state
->proto_task
.uloop
.pending
) {
267 if (!state
->proto_task_killed
)
268 kill(state
->proto_task
.uloop
.pid
, SIGTERM
);
272 uloop_timeout_cancel(&state
->teardown_timeout
);
274 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
280 proto_shell_teardown_timeout_cb(struct uloop_timeout
*timeout
)
282 struct proto_shell_state
*state
;
284 state
= container_of(timeout
, struct proto_shell_state
, teardown_timeout
);
286 netifd_kill_process(&state
->script_task
);
287 netifd_kill_process(&state
->proto_task
);
288 proto_shell_task_finish(state
, NULL
);
292 proto_shell_script_cb(struct netifd_process
*p
, int ret
)
294 struct proto_shell_state
*state
;
296 state
= container_of(p
, struct proto_shell_state
, script_task
);
297 proto_shell_task_finish(state
, p
);
301 proto_shell_task_cb(struct netifd_process
*p
, int ret
)
303 struct proto_shell_state
*state
;
305 state
= container_of(p
, struct proto_shell_state
, proto_task
);
307 if (state
->sm
== S_IDLE
|| state
->sm
== S_SETUP
)
308 state
->last_error
= WEXITSTATUS(ret
);
310 proto_shell_task_finish(state
, p
);
314 proto_shell_free(struct interface_proto_state
*proto
)
316 struct proto_shell_state
*state
;
318 state
= container_of(proto
, struct proto_shell_state
, proto
);
319 proto_shell_clear_host_dep(state
);
320 netifd_kill_process(&state
->script_task
);
321 netifd_kill_process(&state
->proto_task
);
327 proto_shell_parse_route_list(struct interface
*iface
, struct blob_attr
*attr
,
330 struct blob_attr
*cur
;
333 blobmsg_for_each_attr(cur
, attr
, rem
) {
334 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
) {
335 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur
));
339 interface_ip_add_route(iface
, cur
, v6
);
344 proto_shell_parse_data(struct interface
*iface
, struct blob_attr
*attr
)
346 struct blob_attr
*cur
;
349 blobmsg_for_each_attr(cur
, attr
, rem
)
350 interface_add_data(iface
, cur
);
353 static struct device
*
354 proto_shell_create_tunnel(const char *name
, struct blob_attr
*attr
)
359 memset(&b
, 0, sizeof(b
));
360 blob_buf_init(&b
, 0);
361 blob_put(&b
, 0, blobmsg_data(attr
), blobmsg_data_len(attr
));
362 dev
= device_create(name
, &tunnel_device_type
, blob_data(b
.head
));
389 static const struct blobmsg_policy notify_attr
[__NOTIFY_LAST
] = {
390 [NOTIFY_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_INT32
},
391 [NOTIFY_ERROR
] = { .name
= "error", .type
= BLOBMSG_TYPE_ARRAY
},
392 [NOTIFY_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_ARRAY
},
393 [NOTIFY_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_ARRAY
},
394 [NOTIFY_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
395 [NOTIFY_AVAILABLE
] = { .name
= "available", .type
= BLOBMSG_TYPE_BOOL
},
396 [NOTIFY_LINK_UP
] = { .name
= "link-up", .type
= BLOBMSG_TYPE_BOOL
},
397 [NOTIFY_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
398 [NOTIFY_ADDR_EXT
] = { .name
= "address-external", .type
= BLOBMSG_TYPE_BOOL
},
399 [NOTIFY_ROUTES
] = { .name
= "routes", .type
= BLOBMSG_TYPE_ARRAY
},
400 [NOTIFY_ROUTES6
] = { .name
= "routes6", .type
= BLOBMSG_TYPE_ARRAY
},
401 [NOTIFY_TUNNEL
] = { .name
= "tunnel", .type
= BLOBMSG_TYPE_TABLE
},
402 [NOTIFY_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
403 [NOTIFY_KEEP
] = { .name
= "keep", .type
= BLOBMSG_TYPE_BOOL
},
404 [NOTIFY_HOST
] = { .name
= "host", .type
= BLOBMSG_TYPE_STRING
},
405 [NOTIFY_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
406 [NOTIFY_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
410 proto_shell_update_link(struct proto_shell_state
*state
, struct blob_attr
*data
, struct blob_attr
**tb
)
412 struct interface
*iface
= state
->proto
.iface
;
413 struct blob_attr
*cur
;
417 bool addr_ext
= false;
421 if (!tb
[NOTIFY_LINK_UP
])
422 return UBUS_STATUS_INVALID_ARGUMENT
;
424 up
= blobmsg_get_bool(tb
[NOTIFY_LINK_UP
]);
426 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
430 if ((cur
= tb
[NOTIFY_KEEP
]) != NULL
)
431 keep
= blobmsg_get_bool(cur
);
433 if ((cur
= tb
[NOTIFY_ADDR_EXT
]) != NULL
) {
434 addr_ext
= blobmsg_get_bool(cur
);
439 if (!tb
[NOTIFY_IFNAME
]) {
440 if (!iface
->main_dev
.dev
)
441 return UBUS_STATUS_INVALID_ARGUMENT
;
442 } else if (!keep
|| iface
->state
!= IFS_UP
) {
444 devname
= blobmsg_data(tb
[NOTIFY_IFNAME
]);
445 if (tb
[NOTIFY_TUNNEL
]) {
446 dev
= proto_shell_create_tunnel(devname
,
449 return UBUS_STATUS_INVALID_ARGUMENT
;
451 dev
= device_get(devname
, dev_create
);
453 return UBUS_STATUS_NOT_FOUND
;
456 interface_set_l3_dev(iface
, dev
);
457 device_claim(&iface
->l3_dev
);
458 device_set_present(dev
, true);
462 interface_update_start(iface
);
464 proto_apply_ip_settings(iface
, data
, addr_ext
);
466 if ((cur
= tb
[NOTIFY_ROUTES
]) != NULL
)
467 proto_shell_parse_route_list(state
->proto
.iface
, cur
, false);
469 if ((cur
= tb
[NOTIFY_ROUTES6
]) != NULL
)
470 proto_shell_parse_route_list(state
->proto
.iface
, cur
, true);
472 if ((cur
= tb
[NOTIFY_DNS
]))
473 interface_add_dns_server_list(&iface
->proto_ip
, cur
);
475 if ((cur
= tb
[NOTIFY_DNS_SEARCH
]))
476 interface_add_dns_search_list(&iface
->proto_ip
, cur
);
478 interface_update_complete(state
->proto
.iface
);
481 state
->proto
.proto_event(&state
->proto
, IFPEV_UP
);
484 if ((cur
= tb
[NOTIFY_DATA
]))
485 proto_shell_parse_data(state
->proto
.iface
, cur
);
491 fill_string_list(struct blob_attr
*attr
, char **argv
, int max
)
493 struct blob_attr
*cur
;
500 blobmsg_for_each_attr(cur
, attr
, rem
) {
501 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
504 if (!blobmsg_check_attr(cur
, NULL
))
507 argv
[argc
++] = blobmsg_data(cur
);
518 proto_shell_run_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
520 static char *argv
[64];
521 static char *env
[32];
523 if (!tb
[NOTIFY_COMMAND
])
526 if (!fill_string_list(tb
[NOTIFY_COMMAND
], argv
, ARRAY_SIZE(argv
)))
529 if (!fill_string_list(tb
[NOTIFY_ENV
], env
, ARRAY_SIZE(env
)))
532 netifd_start_process((const char **) argv
, (char **) env
, &state
->proto_task
);
537 return UBUS_STATUS_INVALID_ARGUMENT
;
541 proto_shell_kill_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
543 unsigned int signal
= ~0;
545 if (tb
[NOTIFY_SIGNAL
])
546 signal
= blobmsg_get_u32(tb
[NOTIFY_SIGNAL
]);
551 if (state
->proto_task
.uloop
.pending
) {
552 state
->proto_task_killed
= true;
553 kill(state
->proto_task
.uloop
.pid
, signal
);
560 proto_shell_notify_error(struct proto_shell_state
*state
, struct blob_attr
**tb
)
562 struct blob_attr
*cur
;
567 if (!tb
[NOTIFY_ERROR
])
568 return UBUS_STATUS_INVALID_ARGUMENT
;
570 blobmsg_for_each_attr(cur
, tb
[NOTIFY_ERROR
], rem
) {
571 if (n_data
+ 1 == ARRAY_SIZE(data
))
574 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
577 if (!blobmsg_check_attr(cur
, NULL
))
580 data
[n_data
++] = blobmsg_data(cur
);
586 interface_add_error(state
->proto
.iface
, state
->handler
->proto
.name
,
587 data
[0], (const char **) &data
[1], n_data
- 1);
592 return UBUS_STATUS_INVALID_ARGUMENT
;
596 proto_shell_block_restart(struct proto_shell_state
*state
, struct blob_attr
**tb
)
598 state
->proto
.iface
->autostart
= false;
603 proto_shell_set_available(struct proto_shell_state
*state
, struct blob_attr
**tb
)
605 if (!tb
[NOTIFY_AVAILABLE
])
606 return UBUS_STATUS_INVALID_ARGUMENT
;
608 interface_set_available(state
->proto
.iface
, blobmsg_get_bool(tb
[NOTIFY_AVAILABLE
]));
613 proto_shell_add_host_dependency(struct proto_shell_state
*state
, struct blob_attr
**tb
)
615 struct proto_shell_dependency
*dep
;
616 struct blob_attr
*host
= tb
[NOTIFY_HOST
];
619 return UBUS_STATUS_INVALID_ARGUMENT
;
621 dep
= calloc(1, sizeof(*dep
));
622 if (!inet_pton(AF_INET
, blobmsg_data(host
), &dep
->host
)) {
624 return UBUS_STATUS_INVALID_ARGUMENT
;
628 dep
->dep
.cb
= proto_shell_if_up_cb
;
629 interface_add_user(&dep
->dep
, NULL
);
630 list_add(&dep
->list
, &state
->deps
);
631 proto_shell_update_host_dep(dep
);
633 return UBUS_STATUS_NOT_FOUND
;
639 proto_shell_setup_failed(struct proto_shell_state
*state
)
643 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
646 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
655 proto_shell_notify(struct interface_proto_state
*proto
, struct blob_attr
*attr
)
657 struct proto_shell_state
*state
;
658 struct blob_attr
*tb
[__NOTIFY_LAST
];
660 state
= container_of(proto
, struct proto_shell_state
, proto
);
662 blobmsg_parse(notify_attr
, __NOTIFY_LAST
, tb
, blob_data(attr
), blob_len(attr
));
663 if (!tb
[NOTIFY_ACTION
])
664 return UBUS_STATUS_INVALID_ARGUMENT
;
666 switch(blobmsg_get_u32(tb
[NOTIFY_ACTION
])) {
668 return proto_shell_update_link(state
, attr
, tb
);
670 return proto_shell_run_command(state
, tb
);
672 return proto_shell_kill_command(state
, tb
);
674 return proto_shell_notify_error(state
, tb
);
676 return proto_shell_block_restart(state
, tb
);
678 return proto_shell_set_available(state
, tb
);
680 return proto_shell_add_host_dependency(state
, tb
);
682 return proto_shell_setup_failed(state
);
684 return UBUS_STATUS_INVALID_ARGUMENT
;
688 static struct interface_proto_state
*
689 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
690 struct blob_attr
*attr
)
692 struct proto_shell_state
*state
;
694 state
= calloc(1, sizeof(*state
));
695 INIT_LIST_HEAD(&state
->deps
);
697 state
->config
= malloc(blob_pad_len(attr
));
701 memcpy(state
->config
, attr
, blob_pad_len(attr
));
702 state
->proto
.free
= proto_shell_free
;
703 state
->proto
.notify
= proto_shell_notify
;
704 state
->proto
.cb
= proto_shell_handler
;
705 state
->teardown_timeout
.cb
= proto_shell_teardown_timeout_cb
;
706 state
->script_task
.cb
= proto_shell_script_cb
;
707 state
->script_task
.dir_fd
= proto_fd
.fd
;
708 state
->script_task
.log_prefix
= iface
->name
;
709 state
->proto_task
.cb
= proto_shell_task_cb
;
710 state
->proto_task
.dir_fd
= proto_fd
.fd
;
711 state
->proto_task
.log_prefix
= iface
->name
;
712 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
714 return &state
->proto
;
722 check_type(json_object
*obj
, json_type type
)
727 if (json_object_get_type(obj
) != type
)
733 static inline json_object
*
734 get_field(json_object
*obj
, const char *name
, json_type type
)
736 return check_type(json_object_object_get(obj
, name
), type
);
740 proto_shell_parse_config(struct config_param_list
*config
, json_object
*obj
)
742 struct blobmsg_policy
*attrs
;
743 char *str_buf
, *str_cur
;
747 config
->n_params
= json_object_array_length(obj
);
748 attrs
= calloc(1, sizeof(*attrs
) * config
->n_params
);
752 config
->params
= attrs
;
753 for (i
= 0; i
< config
->n_params
; i
++) {
754 json_object
*cur
, *name
, *type
;
756 cur
= check_type(json_object_array_get_idx(obj
, i
), json_type_array
);
760 name
= check_type(json_object_array_get_idx(cur
, 0), json_type_string
);
764 type
= check_type(json_object_array_get_idx(cur
, 1), json_type_int
);
768 attrs
[i
].name
= json_object_get_string(name
);
769 attrs
[i
].type
= json_object_get_int(type
);
770 if (attrs
[i
].type
> BLOBMSG_TYPE_LAST
)
773 str_len
+= strlen(attrs
[i
].name
) + 1;
776 str_buf
= malloc(str_len
);
781 for (i
= 0; i
< config
->n_params
; i
++) {
782 const char *name
= attrs
[i
].name
;
784 attrs
[i
].name
= str_cur
;
785 str_cur
+= sprintf(str_cur
, "%s", name
) + 1;
792 config
->n_params
= 0;
797 proto_shell_add_handler(const char *script
, json_object
*obj
)
799 struct proto_shell_handler
*handler
;
800 struct proto_handler
*proto
;
801 json_object
*config
, *tmp
;
805 if (!check_type(obj
, json_type_object
))
808 tmp
= get_field(obj
, "name", json_type_string
);
812 name
= json_object_get_string(tmp
);
814 handler
= calloc(1, sizeof(*handler
) +
820 strcpy(handler
->script_name
, script
);
822 str
= handler
->script_name
+ strlen(handler
->script_name
) + 1;
825 proto
= &handler
->proto
;
827 proto
->config_params
= &handler
->config
;
828 proto
->attach
= proto_shell_attach
;
830 tmp
= get_field(obj
, "no-device", json_type_boolean
);
831 if (tmp
&& json_object_get_boolean(tmp
))
832 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
834 tmp
= get_field(obj
, "available", json_type_boolean
);
835 if (tmp
&& json_object_get_boolean(tmp
))
836 handler
->proto
.flags
|= PROTO_FLAG_INIT_AVAILABLE
;
838 config
= get_field(obj
, "config", json_type_array
);
840 handler
->config_buf
= proto_shell_parse_config(&handler
->config
, config
);
842 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
843 add_proto_handler(proto
);
846 static void proto_shell_add_script(const char *name
)
848 struct json_tokener
*tok
= NULL
;
850 static char buf
[512];
855 #define DUMP_SUFFIX " '' dump"
857 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_SUFFIX
));
858 sprintf(cmd
, "%s" DUMP_SUFFIX
, name
);
865 start
= fgets(buf
, sizeof(buf
), f
);
872 tok
= json_tokener_new();
874 obj
= json_tokener_parse_ex(tok
, start
, len
);
875 if (!is_error(obj
)) {
876 proto_shell_add_handler(name
, obj
);
877 json_object_put(obj
);
878 json_tokener_free(tok
);
880 } else if (start
[len
- 1] == '\n') {
881 json_tokener_free(tok
);
884 } while (!feof(f
) && !ferror(f
));
887 json_tokener_free(tok
);
892 static void __init
proto_shell_init(void)
898 main_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
902 if (chdir(main_path
)) {
903 perror("chdir(main path)");
907 if (chdir("./proto"))
910 proto_fd
.fd
= open(".", O_RDONLY
| O_DIRECTORY
);
914 netifd_fd_add(&proto_fd
);
915 glob("./*.sh", 0, NULL
, &g
);
916 for (i
= 0; i
< g
.gl_pathc
; i
++)
917 proto_shell_add_script(g
.gl_pathv
[i
]);