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"
35 static int proto_fd
= -1;
44 struct proto_shell_handler
{
45 struct list_head list
;
46 struct proto_handler proto
;
47 struct config_param_list config
;
53 struct proto_shell_dependency
{
54 struct list_head list
;
56 struct proto_shell_state
*proto
;
57 struct interface_user dep
;
63 struct proto_shell_state
{
64 struct interface_proto_state proto
;
65 struct proto_shell_handler
*handler
;
66 struct blob_attr
*config
;
68 struct uloop_timeout teardown_timeout
;
70 struct netifd_process script_task
;
71 struct netifd_process proto_task
;
73 enum proto_shell_sm sm
;
74 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
;
113 iface
= interface_ip_add_target_route(&dep
->host
, dep
->v6
);
117 interface_remove_user(&dep
->dep
);
118 dep
->dep
.cb
= proto_shell_if_down_cb
;
119 interface_add_user(&dep
->dep
, iface
);
122 proto_shell_check_dependencies(dep
->proto
);
126 proto_shell_clear_host_dep(struct proto_shell_state
*state
)
128 struct proto_shell_dependency
*dep
, *tmp
;
130 list_for_each_entry_safe(dep
, tmp
, &state
->deps
, list
) {
131 interface_remove_user(&dep
->dep
);
132 list_del(&dep
->list
);
138 proto_shell_handler(struct interface_proto_state
*proto
,
139 enum interface_proto_cmd cmd
, bool force
)
141 struct proto_shell_state
*state
;
142 struct proto_shell_handler
*handler
;
143 struct netifd_process
*proc
;
144 static char error_buf
[32];
149 int ret
, i
= 0, j
= 0;
151 state
= container_of(proto
, struct proto_shell_state
, proto
);
152 handler
= state
->handler
;
153 proc
= &state
->script_task
;
155 if (cmd
== PROTO_CMD_SETUP
) {
157 state
->last_error
= -1;
158 proto_shell_clear_host_dep(state
);
160 if (state
->sm
== S_TEARDOWN
)
163 if (state
->script_task
.uloop
.pending
) {
164 if (state
->sm
!= S_SETUP_ABORT
) {
165 uloop_timeout_set(&state
->teardown_timeout
, 1000);
166 kill(state
->script_task
.uloop
.pid
, SIGTERM
);
167 if (state
->proto_task
.uloop
.pending
)
168 kill(state
->proto_task
.uloop
.pid
, SIGTERM
);
169 state
->sm
= S_SETUP_ABORT
;
175 state
->sm
= S_TEARDOWN
;
176 if (state
->last_error
>= 0) {
177 snprintf(error_buf
, sizeof(error_buf
), "ERROR=%d", state
->last_error
);
178 envp
[j
++] = error_buf
;
180 uloop_timeout_set(&state
->teardown_timeout
, 5000);
183 config
= blobmsg_format_json(state
->config
, true);
187 argv
[i
++] = handler
->script_name
;
188 argv
[i
++] = handler
->proto
.name
;
190 argv
[i
++] = proto
->iface
->name
;
192 if (proto
->iface
->main_dev
.dev
)
193 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
197 ret
= netifd_start_process(argv
, envp
, proc
);
204 proto_shell_if_up_cb(struct interface_user
*dep
, struct interface
*iface
,
205 enum interface_event ev
)
207 struct proto_shell_dependency
*pdep
;
212 pdep
= container_of(dep
, struct proto_shell_dependency
, dep
);
213 proto_shell_update_host_dep(pdep
);
217 proto_shell_if_down_cb(struct interface_user
*dep
, struct interface
*iface
,
218 enum interface_event ev
)
220 struct proto_shell_dependency
*pdep
;
221 struct proto_shell_state
*state
;
226 pdep
= container_of(dep
, struct proto_shell_dependency
, dep
);
227 interface_remove_user(dep
);
228 dep
->cb
= proto_shell_if_up_cb
;
229 interface_add_user(dep
, NULL
);
232 if (state
->sm
== S_IDLE
) {
233 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
234 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
239 proto_shell_task_finish(struct proto_shell_state
*state
,
240 struct netifd_process
*task
)
244 if (task
== &state
->proto_task
)
245 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
248 if (task
== &state
->proto_task
)
249 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
,
254 if (state
->script_task
.uloop
.pending
||
255 state
->proto_task
.uloop
.pending
)
258 uloop_timeout_cancel(&state
->teardown_timeout
);
260 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
264 if (state
->script_task
.uloop
.pending
)
267 if (state
->proto_task
.uloop
.pending
) {
268 if (!state
->proto_task_killed
)
269 kill(state
->proto_task
.uloop
.pid
, SIGTERM
);
273 uloop_timeout_cancel(&state
->teardown_timeout
);
275 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
281 proto_shell_teardown_timeout_cb(struct uloop_timeout
*timeout
)
283 struct proto_shell_state
*state
;
285 state
= container_of(timeout
, struct proto_shell_state
, teardown_timeout
);
287 netifd_kill_process(&state
->script_task
);
288 netifd_kill_process(&state
->proto_task
);
289 proto_shell_task_finish(state
, NULL
);
293 proto_shell_script_cb(struct netifd_process
*p
, int ret
)
295 struct proto_shell_state
*state
;
297 state
= container_of(p
, struct proto_shell_state
, script_task
);
298 proto_shell_task_finish(state
, p
);
302 proto_shell_task_cb(struct netifd_process
*p
, int ret
)
304 struct proto_shell_state
*state
;
306 state
= container_of(p
, struct proto_shell_state
, proto_task
);
308 if (state
->sm
== S_IDLE
|| state
->sm
== S_SETUP
)
309 state
->last_error
= WEXITSTATUS(ret
);
311 proto_shell_task_finish(state
, p
);
315 proto_shell_free(struct interface_proto_state
*proto
)
317 struct proto_shell_state
*state
;
319 state
= container_of(proto
, struct proto_shell_state
, proto
);
320 uloop_timeout_cancel(&state
->teardown_timeout
);
321 proto_shell_clear_host_dep(state
);
322 netifd_kill_process(&state
->script_task
);
323 netifd_kill_process(&state
->proto_task
);
329 proto_shell_parse_route_list(struct interface
*iface
, struct blob_attr
*attr
,
332 struct blob_attr
*cur
;
335 blobmsg_for_each_attr(cur
, attr
, rem
) {
336 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
) {
337 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur
));
341 interface_ip_add_route(iface
, cur
, v6
);
346 proto_shell_parse_data(struct interface
*iface
, struct blob_attr
*attr
)
348 struct blob_attr
*cur
;
351 blobmsg_for_each_attr(cur
, attr
, rem
)
352 interface_add_data(iface
, cur
);
355 static struct device
*
356 proto_shell_create_tunnel(const char *name
, struct blob_attr
*attr
)
361 memset(&b
, 0, sizeof(b
));
362 blob_buf_init(&b
, 0);
363 blob_put(&b
, 0, blobmsg_data(attr
), blobmsg_data_len(attr
));
364 dev
= device_create(name
, &tunnel_device_type
, blob_data(b
.head
));
391 static const struct blobmsg_policy notify_attr
[__NOTIFY_LAST
] = {
392 [NOTIFY_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_INT32
},
393 [NOTIFY_ERROR
] = { .name
= "error", .type
= BLOBMSG_TYPE_ARRAY
},
394 [NOTIFY_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_ARRAY
},
395 [NOTIFY_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_ARRAY
},
396 [NOTIFY_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
397 [NOTIFY_AVAILABLE
] = { .name
= "available", .type
= BLOBMSG_TYPE_BOOL
},
398 [NOTIFY_LINK_UP
] = { .name
= "link-up", .type
= BLOBMSG_TYPE_BOOL
},
399 [NOTIFY_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
400 [NOTIFY_ADDR_EXT
] = { .name
= "address-external", .type
= BLOBMSG_TYPE_BOOL
},
401 [NOTIFY_ROUTES
] = { .name
= "routes", .type
= BLOBMSG_TYPE_ARRAY
},
402 [NOTIFY_ROUTES6
] = { .name
= "routes6", .type
= BLOBMSG_TYPE_ARRAY
},
403 [NOTIFY_TUNNEL
] = { .name
= "tunnel", .type
= BLOBMSG_TYPE_TABLE
},
404 [NOTIFY_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
405 [NOTIFY_KEEP
] = { .name
= "keep", .type
= BLOBMSG_TYPE_BOOL
},
406 [NOTIFY_HOST
] = { .name
= "host", .type
= BLOBMSG_TYPE_STRING
},
407 [NOTIFY_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
408 [NOTIFY_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
412 proto_shell_update_link(struct proto_shell_state
*state
, struct blob_attr
*data
, struct blob_attr
**tb
)
414 struct interface
*iface
= state
->proto
.iface
;
415 struct blob_attr
*cur
;
419 bool addr_ext
= false;
423 if (!tb
[NOTIFY_LINK_UP
])
424 return UBUS_STATUS_INVALID_ARGUMENT
;
426 up
= blobmsg_get_bool(tb
[NOTIFY_LINK_UP
]);
428 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
432 if ((cur
= tb
[NOTIFY_KEEP
]) != NULL
)
433 keep
= blobmsg_get_bool(cur
);
435 if ((cur
= tb
[NOTIFY_ADDR_EXT
]) != NULL
) {
436 addr_ext
= blobmsg_get_bool(cur
);
441 if (!tb
[NOTIFY_IFNAME
]) {
442 if (!iface
->main_dev
.dev
)
443 return UBUS_STATUS_INVALID_ARGUMENT
;
444 } else if (!keep
|| iface
->state
!= IFS_UP
) {
446 devname
= blobmsg_data(tb
[NOTIFY_IFNAME
]);
447 if (tb
[NOTIFY_TUNNEL
]) {
448 dev
= proto_shell_create_tunnel(devname
,
451 return UBUS_STATUS_INVALID_ARGUMENT
;
453 dev
= device_get(devname
, dev_create
);
455 return UBUS_STATUS_NOT_FOUND
;
458 interface_set_l3_dev(iface
, dev
);
459 device_claim(&iface
->l3_dev
);
460 device_set_present(dev
, true);
464 interface_update_start(iface
);
466 proto_apply_ip_settings(iface
, data
, addr_ext
);
468 if ((cur
= tb
[NOTIFY_ROUTES
]) != NULL
)
469 proto_shell_parse_route_list(state
->proto
.iface
, cur
, false);
471 if ((cur
= tb
[NOTIFY_ROUTES6
]) != NULL
)
472 proto_shell_parse_route_list(state
->proto
.iface
, cur
, true);
474 if ((cur
= tb
[NOTIFY_DNS
]))
475 interface_add_dns_server_list(&iface
->proto_ip
, cur
);
477 if ((cur
= tb
[NOTIFY_DNS_SEARCH
]))
478 interface_add_dns_search_list(&iface
->proto_ip
, cur
);
480 interface_update_complete(state
->proto
.iface
);
483 state
->proto
.proto_event(&state
->proto
, IFPEV_UP
);
486 if ((cur
= tb
[NOTIFY_DATA
]))
487 proto_shell_parse_data(state
->proto
.iface
, cur
);
493 fill_string_list(struct blob_attr
*attr
, char **argv
, int max
)
495 struct blob_attr
*cur
;
502 blobmsg_for_each_attr(cur
, attr
, rem
) {
503 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
506 if (!blobmsg_check_attr(cur
, NULL
))
509 argv
[argc
++] = blobmsg_data(cur
);
520 proto_shell_run_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
522 static char *argv
[64];
523 static char *env
[32];
525 if (!tb
[NOTIFY_COMMAND
])
528 if (!fill_string_list(tb
[NOTIFY_COMMAND
], argv
, ARRAY_SIZE(argv
)))
531 if (!fill_string_list(tb
[NOTIFY_ENV
], env
, ARRAY_SIZE(env
)))
534 netifd_start_process((const char **) argv
, (char **) env
, &state
->proto_task
);
539 return UBUS_STATUS_INVALID_ARGUMENT
;
543 proto_shell_kill_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
545 unsigned int signal
= ~0;
547 if (tb
[NOTIFY_SIGNAL
])
548 signal
= blobmsg_get_u32(tb
[NOTIFY_SIGNAL
]);
553 if (state
->proto_task
.uloop
.pending
) {
554 state
->proto_task_killed
= true;
555 kill(state
->proto_task
.uloop
.pid
, signal
);
562 proto_shell_notify_error(struct proto_shell_state
*state
, struct blob_attr
**tb
)
564 struct blob_attr
*cur
;
569 if (!tb
[NOTIFY_ERROR
])
570 return UBUS_STATUS_INVALID_ARGUMENT
;
572 blobmsg_for_each_attr(cur
, tb
[NOTIFY_ERROR
], rem
) {
573 if (n_data
+ 1 == ARRAY_SIZE(data
))
576 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
579 if (!blobmsg_check_attr(cur
, NULL
))
582 data
[n_data
++] = blobmsg_data(cur
);
588 interface_add_error(state
->proto
.iface
, state
->handler
->proto
.name
,
589 data
[0], (const char **) &data
[1], n_data
- 1);
594 return UBUS_STATUS_INVALID_ARGUMENT
;
598 proto_shell_block_restart(struct proto_shell_state
*state
, struct blob_attr
**tb
)
600 state
->proto
.iface
->autostart
= false;
605 proto_shell_set_available(struct proto_shell_state
*state
, struct blob_attr
**tb
)
607 if (!tb
[NOTIFY_AVAILABLE
])
608 return UBUS_STATUS_INVALID_ARGUMENT
;
610 interface_set_available(state
->proto
.iface
, blobmsg_get_bool(tb
[NOTIFY_AVAILABLE
]));
615 proto_shell_add_host_dependency(struct proto_shell_state
*state
, struct blob_attr
**tb
)
617 struct proto_shell_dependency
*dep
;
618 struct blob_attr
*host
= tb
[NOTIFY_HOST
];
621 return UBUS_STATUS_INVALID_ARGUMENT
;
623 dep
= calloc(1, sizeof(*dep
));
624 if (!inet_pton(AF_INET
, blobmsg_data(host
), &dep
->host
)) {
626 return UBUS_STATUS_INVALID_ARGUMENT
;
630 dep
->dep
.cb
= proto_shell_if_up_cb
;
631 interface_add_user(&dep
->dep
, NULL
);
632 list_add(&dep
->list
, &state
->deps
);
633 proto_shell_update_host_dep(dep
);
635 return UBUS_STATUS_NOT_FOUND
;
641 proto_shell_setup_failed(struct proto_shell_state
*state
)
645 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
648 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
657 proto_shell_notify(struct interface_proto_state
*proto
, struct blob_attr
*attr
)
659 struct proto_shell_state
*state
;
660 struct blob_attr
*tb
[__NOTIFY_LAST
];
662 state
= container_of(proto
, struct proto_shell_state
, proto
);
664 blobmsg_parse(notify_attr
, __NOTIFY_LAST
, tb
, blob_data(attr
), blob_len(attr
));
665 if (!tb
[NOTIFY_ACTION
])
666 return UBUS_STATUS_INVALID_ARGUMENT
;
668 switch(blobmsg_get_u32(tb
[NOTIFY_ACTION
])) {
670 return proto_shell_update_link(state
, attr
, tb
);
672 return proto_shell_run_command(state
, tb
);
674 return proto_shell_kill_command(state
, tb
);
676 return proto_shell_notify_error(state
, tb
);
678 return proto_shell_block_restart(state
, tb
);
680 return proto_shell_set_available(state
, tb
);
682 return proto_shell_add_host_dependency(state
, tb
);
684 return proto_shell_setup_failed(state
);
686 return UBUS_STATUS_INVALID_ARGUMENT
;
690 static struct interface_proto_state
*
691 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
692 struct blob_attr
*attr
)
694 struct proto_shell_state
*state
;
696 state
= calloc(1, sizeof(*state
));
697 INIT_LIST_HEAD(&state
->deps
);
699 state
->config
= malloc(blob_pad_len(attr
));
703 memcpy(state
->config
, attr
, blob_pad_len(attr
));
704 state
->proto
.free
= proto_shell_free
;
705 state
->proto
.notify
= proto_shell_notify
;
706 state
->proto
.cb
= proto_shell_handler
;
707 state
->teardown_timeout
.cb
= proto_shell_teardown_timeout_cb
;
708 state
->script_task
.cb
= proto_shell_script_cb
;
709 state
->script_task
.dir_fd
= proto_fd
;
710 state
->script_task
.log_prefix
= iface
->name
;
711 state
->proto_task
.cb
= proto_shell_task_cb
;
712 state
->proto_task
.dir_fd
= proto_fd
;
713 state
->proto_task
.log_prefix
= iface
->name
;
714 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
716 return &state
->proto
;
724 check_type(json_object
*obj
, json_type type
)
729 if (json_object_get_type(obj
) != type
)
735 static inline json_object
*
736 get_field(json_object
*obj
, const char *name
, json_type type
)
738 return check_type(json_object_object_get(obj
, name
), type
);
742 proto_shell_parse_config(struct config_param_list
*config
, json_object
*obj
)
744 struct blobmsg_policy
*attrs
;
745 char *str_buf
, *str_cur
;
749 config
->n_params
= json_object_array_length(obj
);
750 attrs
= calloc(1, sizeof(*attrs
) * config
->n_params
);
754 config
->params
= attrs
;
755 for (i
= 0; i
< config
->n_params
; i
++) {
756 json_object
*cur
, *name
, *type
;
758 cur
= check_type(json_object_array_get_idx(obj
, i
), json_type_array
);
762 name
= check_type(json_object_array_get_idx(cur
, 0), json_type_string
);
766 type
= check_type(json_object_array_get_idx(cur
, 1), json_type_int
);
770 attrs
[i
].name
= json_object_get_string(name
);
771 attrs
[i
].type
= json_object_get_int(type
);
772 if (attrs
[i
].type
> BLOBMSG_TYPE_LAST
)
775 str_len
+= strlen(attrs
[i
].name
) + 1;
778 str_buf
= malloc(str_len
);
783 for (i
= 0; i
< config
->n_params
; i
++) {
784 const char *name
= attrs
[i
].name
;
786 attrs
[i
].name
= str_cur
;
787 str_cur
+= sprintf(str_cur
, "%s", name
) + 1;
794 config
->n_params
= 0;
799 proto_shell_add_handler(const char *script
, json_object
*obj
)
801 struct proto_shell_handler
*handler
;
802 struct proto_handler
*proto
;
803 json_object
*config
, *tmp
;
807 if (!check_type(obj
, json_type_object
))
810 tmp
= get_field(obj
, "name", json_type_string
);
814 name
= json_object_get_string(tmp
);
816 handler
= calloc_a(sizeof(*handler
) + strlen(script
) + 1,
817 &str
, strlen(name
) + 1);
821 strcpy(handler
->script_name
, script
);
824 proto
= &handler
->proto
;
826 proto
->config_params
= &handler
->config
;
827 proto
->attach
= proto_shell_attach
;
829 tmp
= get_field(obj
, "no-device", json_type_boolean
);
830 if (tmp
&& json_object_get_boolean(tmp
))
831 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
833 tmp
= get_field(obj
, "available", json_type_boolean
);
834 if (tmp
&& json_object_get_boolean(tmp
))
835 handler
->proto
.flags
|= PROTO_FLAG_INIT_AVAILABLE
;
837 config
= get_field(obj
, "config", json_type_array
);
839 handler
->config_buf
= proto_shell_parse_config(&handler
->config
, config
);
841 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
842 add_proto_handler(proto
);
845 static void proto_shell_add_script(const char *name
)
847 struct json_tokener
*tok
= NULL
;
849 static char buf
[512];
854 #define DUMP_SUFFIX " '' dump"
856 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_SUFFIX
));
857 sprintf(cmd
, "%s" DUMP_SUFFIX
, name
);
864 start
= fgets(buf
, sizeof(buf
), f
);
871 tok
= json_tokener_new();
873 obj
= json_tokener_parse_ex(tok
, start
, len
);
874 if (!is_error(obj
)) {
875 proto_shell_add_handler(name
, obj
);
876 json_object_put(obj
);
877 json_tokener_free(tok
);
879 } else if (start
[len
- 1] == '\n') {
880 json_tokener_free(tok
);
883 } while (!feof(f
) && !ferror(f
));
886 json_tokener_free(tok
);
891 static void __init
proto_shell_init(void)
897 main_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
901 if (chdir(main_path
)) {
902 perror("chdir(main path)");
906 if (chdir("./proto"))
909 proto_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
913 system_fd_set_cloexec(proto_fd
);
914 glob("./*.sh", 0, NULL
, &g
);
915 for (i
= 0; i
< g
.gl_pathc
; i
++)
916 proto_shell_add_script(g
.gl_pathv
[i
]);