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
;
49 struct uci_blob_param_list config
;
52 struct proto_shell_dependency
{
53 struct list_head list
;
55 struct proto_shell_state
*proto
;
56 struct interface_user dep
;
65 struct proto_shell_state
{
66 struct interface_proto_state proto
;
67 struct proto_shell_handler
*handler
;
68 struct blob_attr
*config
;
70 struct uloop_timeout teardown_timeout
;
72 struct netifd_process script_task
;
73 struct netifd_process proto_task
;
75 enum proto_shell_sm sm
;
76 bool proto_task_killed
;
81 struct list_head deps
;
85 proto_shell_check_dependencies(struct proto_shell_state
*state
)
87 struct proto_shell_dependency
*dep
;
88 bool available
= true;
90 list_for_each_entry(dep
, &state
->deps
, list
) {
98 interface_set_available(state
->proto
.iface
, available
);
102 proto_shell_if_up_cb(struct interface_user
*dep
, struct interface
*iface
,
103 enum interface_event ev
);
105 proto_shell_if_down_cb(struct interface_user
*dep
, struct interface
*iface
,
106 enum interface_event ev
);
109 proto_shell_update_host_dep(struct proto_shell_dependency
*dep
)
111 struct interface
*iface
= NULL
;
116 if (dep
->interface
[0]) {
117 iface
= vlist_find(&interfaces
, dep
->interface
, iface
, node
);
119 if (!iface
|| iface
->state
!= IFS_UP
)
124 iface
= interface_ip_add_target_route(&dep
->host
, dep
->v6
, iface
);
129 interface_remove_user(&dep
->dep
);
130 dep
->dep
.cb
= proto_shell_if_down_cb
;
131 interface_add_user(&dep
->dep
, iface
);
134 proto_shell_check_dependencies(dep
->proto
);
138 proto_shell_clear_host_dep(struct proto_shell_state
*state
)
140 struct proto_shell_dependency
*dep
, *tmp
;
142 list_for_each_entry_safe(dep
, tmp
, &state
->deps
, list
) {
143 interface_remove_user(&dep
->dep
);
144 list_del(&dep
->list
);
150 proto_shell_handler(struct interface_proto_state
*proto
,
151 enum interface_proto_cmd cmd
, bool force
)
153 struct proto_shell_state
*state
;
154 struct proto_shell_handler
*handler
;
155 struct netifd_process
*proc
;
156 static char error_buf
[32];
161 int ret
, i
= 0, j
= 0;
163 state
= container_of(proto
, struct proto_shell_state
, proto
);
164 handler
= state
->handler
;
165 proc
= &state
->script_task
;
167 if (cmd
== PROTO_CMD_SETUP
) {
171 state
->last_error
= -1;
172 proto_shell_clear_host_dep(state
);
184 } else if (cmd
== PROTO_CMD_RENEW
) {
185 if (!(handler
->proto
.flags
& PROTO_FLAG_RENEW_AVAILABLE
))
188 if (state
->script_task
.uloop
.pending
) {
189 state
->renew_pending
= true;
193 state
->renew_pending
= false;
198 if (state
->script_task
.uloop
.pending
) {
199 uloop_timeout_set(&state
->teardown_timeout
, 1000);
200 kill(state
->script_task
.uloop
.pid
, SIGTERM
);
201 if (state
->proto_task
.uloop
.pending
)
202 kill(state
->proto_task
.uloop
.pid
, SIGTERM
);
203 state
->renew_pending
= false;
204 state
->sm
= S_SETUP_ABORT
;
207 /* fall through if no script task is running */
210 state
->renew_pending
= false;
211 state
->sm
= S_TEARDOWN
;
212 if (state
->last_error
>= 0) {
213 snprintf(error_buf
, sizeof(error_buf
), "ERROR=%d", state
->last_error
);
214 envp
[j
++] = error_buf
;
216 uloop_timeout_set(&state
->teardown_timeout
, 5000);
227 D(INTERFACE
, "run %s for interface '%s'\n", action
, proto
->iface
->name
);
228 config
= blobmsg_format_json(state
->config
, true);
232 argv
[i
++] = handler
->script_name
;
233 argv
[i
++] = handler
->proto
.name
;
235 argv
[i
++] = proto
->iface
->name
;
237 if (proto
->iface
->main_dev
.dev
)
238 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
242 ret
= netifd_start_process(argv
, envp
, proc
);
249 proto_shell_if_up_cb(struct interface_user
*dep
, struct interface
*iface
,
250 enum interface_event ev
)
252 struct proto_shell_dependency
*pdep
;
254 if (ev
!= IFEV_UP
&& ev
!= IFEV_UPDATE
)
257 pdep
= container_of(dep
, struct proto_shell_dependency
, dep
);
258 proto_shell_update_host_dep(pdep
);
262 proto_shell_if_down_cb(struct interface_user
*dep
, struct interface
*iface
,
263 enum interface_event ev
)
265 struct proto_shell_dependency
*pdep
;
266 struct proto_shell_state
*state
;
268 if (ev
== IFEV_UP
|| ev
== IFEV_UPDATE
)
271 pdep
= container_of(dep
, struct proto_shell_dependency
, dep
);
272 interface_remove_user(dep
);
273 dep
->cb
= proto_shell_if_up_cb
;
274 interface_add_user(dep
, NULL
);
277 if (state
->sm
== S_IDLE
) {
278 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
279 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
284 proto_shell_task_finish(struct proto_shell_state
*state
,
285 struct netifd_process
*task
)
289 if (task
== &state
->proto_task
)
290 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
293 if (task
== &state
->proto_task
)
294 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
,
296 else if (task
== &state
->script_task
) {
297 if (state
->renew_pending
)
298 proto_shell_handler(&state
->proto
,
299 PROTO_CMD_RENEW
, false);
300 else if (!state
->handler
->no_proto_task
&&
301 !state
->proto_task
.uloop
.pending
&&
302 state
->sm
== S_SETUP
)
303 proto_shell_handler(&state
->proto
,
310 if (state
->script_task
.uloop
.pending
||
311 state
->proto_task
.uloop
.pending
)
314 uloop_timeout_cancel(&state
->teardown_timeout
);
316 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
320 if (state
->script_task
.uloop
.pending
)
323 if (state
->proto_task
.uloop
.pending
) {
324 if (!state
->proto_task_killed
)
325 kill(state
->proto_task
.uloop
.pid
, SIGTERM
);
329 uloop_timeout_cancel(&state
->teardown_timeout
);
331 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
337 proto_shell_teardown_timeout_cb(struct uloop_timeout
*timeout
)
339 struct proto_shell_state
*state
;
341 state
= container_of(timeout
, struct proto_shell_state
, teardown_timeout
);
343 netifd_kill_process(&state
->script_task
);
344 netifd_kill_process(&state
->proto_task
);
345 proto_shell_task_finish(state
, NULL
);
349 proto_shell_script_cb(struct netifd_process
*p
, int ret
)
351 struct proto_shell_state
*state
;
353 state
= container_of(p
, struct proto_shell_state
, script_task
);
354 proto_shell_task_finish(state
, p
);
358 proto_shell_task_cb(struct netifd_process
*p
, int ret
)
360 struct proto_shell_state
*state
;
362 state
= container_of(p
, struct proto_shell_state
, proto_task
);
364 if (state
->sm
== S_IDLE
|| state
->sm
== S_SETUP
)
365 state
->last_error
= WEXITSTATUS(ret
);
367 proto_shell_task_finish(state
, p
);
371 proto_shell_free(struct interface_proto_state
*proto
)
373 struct proto_shell_state
*state
;
375 state
= container_of(proto
, struct proto_shell_state
, proto
);
376 uloop_timeout_cancel(&state
->teardown_timeout
);
377 proto_shell_clear_host_dep(state
);
378 netifd_kill_process(&state
->script_task
);
379 netifd_kill_process(&state
->proto_task
);
385 proto_shell_parse_route_list(struct interface
*iface
, struct blob_attr
*attr
,
388 struct blob_attr
*cur
;
391 blobmsg_for_each_attr(cur
, attr
, rem
) {
392 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
) {
393 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur
));
397 interface_ip_add_route(iface
, cur
, v6
);
402 proto_shell_parse_data(struct interface
*iface
, struct blob_attr
*attr
)
404 struct blob_attr
*cur
;
407 blobmsg_for_each_attr(cur
, attr
, rem
)
408 interface_add_data(iface
, cur
);
411 static struct device
*
412 proto_shell_create_tunnel(const char *name
, struct blob_attr
*attr
)
417 memset(&b
, 0, sizeof(b
));
418 blob_buf_init(&b
, 0);
419 blob_put(&b
, 0, blobmsg_data(attr
), blobmsg_data_len(attr
));
420 dev
= device_create(name
, &tunnel_device_type
, blob_data(b
.head
));
447 static const struct blobmsg_policy notify_attr
[__NOTIFY_LAST
] = {
448 [NOTIFY_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_INT32
},
449 [NOTIFY_ERROR
] = { .name
= "error", .type
= BLOBMSG_TYPE_ARRAY
},
450 [NOTIFY_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_ARRAY
},
451 [NOTIFY_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_ARRAY
},
452 [NOTIFY_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
453 [NOTIFY_AVAILABLE
] = { .name
= "available", .type
= BLOBMSG_TYPE_BOOL
},
454 [NOTIFY_LINK_UP
] = { .name
= "link-up", .type
= BLOBMSG_TYPE_BOOL
},
455 [NOTIFY_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
456 [NOTIFY_ADDR_EXT
] = { .name
= "address-external", .type
= BLOBMSG_TYPE_BOOL
},
457 [NOTIFY_ROUTES
] = { .name
= "routes", .type
= BLOBMSG_TYPE_ARRAY
},
458 [NOTIFY_ROUTES6
] = { .name
= "routes6", .type
= BLOBMSG_TYPE_ARRAY
},
459 [NOTIFY_TUNNEL
] = { .name
= "tunnel", .type
= BLOBMSG_TYPE_TABLE
},
460 [NOTIFY_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
461 [NOTIFY_KEEP
] = { .name
= "keep", .type
= BLOBMSG_TYPE_BOOL
},
462 [NOTIFY_HOST
] = { .name
= "host", .type
= BLOBMSG_TYPE_STRING
},
463 [NOTIFY_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
464 [NOTIFY_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
468 proto_shell_update_link(struct proto_shell_state
*state
, struct blob_attr
*data
, struct blob_attr
**tb
)
470 struct interface
*iface
= state
->proto
.iface
;
471 struct blob_attr
*cur
;
475 bool addr_ext
= false;
479 if (state
->sm
== S_TEARDOWN
|| state
->sm
== S_SETUP_ABORT
)
480 return UBUS_STATUS_PERMISSION_DENIED
;
482 if (!tb
[NOTIFY_LINK_UP
])
483 return UBUS_STATUS_INVALID_ARGUMENT
;
485 up
= blobmsg_get_bool(tb
[NOTIFY_LINK_UP
]);
487 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
491 if ((cur
= tb
[NOTIFY_KEEP
]) != NULL
)
492 keep
= blobmsg_get_bool(cur
);
494 if ((cur
= tb
[NOTIFY_ADDR_EXT
]) != NULL
) {
495 addr_ext
= blobmsg_get_bool(cur
);
500 if (iface
->state
!= IFS_UP
|| !iface
->l3_dev
.dev
)
504 dev
= iface
->main_dev
.dev
;
505 if (tb
[NOTIFY_IFNAME
]) {
507 devname
= blobmsg_data(tb
[NOTIFY_IFNAME
]);
508 if (tb
[NOTIFY_TUNNEL
])
509 dev
= proto_shell_create_tunnel(devname
, tb
[NOTIFY_TUNNEL
]);
511 dev
= device_get(devname
, dev_create
);
515 return UBUS_STATUS_INVALID_ARGUMENT
;
517 interface_set_l3_dev(iface
, dev
);
518 if (device_claim(&iface
->l3_dev
) < 0)
519 return UBUS_STATUS_UNKNOWN_ERROR
;
521 device_set_present(dev
, true);
523 interface_update_start(iface
);
526 proto_apply_ip_settings(iface
, data
, addr_ext
);
528 if ((cur
= tb
[NOTIFY_ROUTES
]) != NULL
)
529 proto_shell_parse_route_list(state
->proto
.iface
, cur
, false);
531 if ((cur
= tb
[NOTIFY_ROUTES6
]) != NULL
)
532 proto_shell_parse_route_list(state
->proto
.iface
, cur
, true);
534 if ((cur
= tb
[NOTIFY_DNS
]))
535 interface_add_dns_server_list(&iface
->proto_ip
, cur
);
537 if ((cur
= tb
[NOTIFY_DNS_SEARCH
]))
538 interface_add_dns_search_list(&iface
->proto_ip
, cur
);
540 if ((cur
= tb
[NOTIFY_DATA
]))
541 proto_shell_parse_data(state
->proto
.iface
, cur
);
543 interface_update_complete(state
->proto
.iface
);
545 if ((state
->sm
!= S_SETUP_ABORT
) && (state
->sm
!= S_TEARDOWN
)) {
547 state
->proto
.proto_event(&state
->proto
, IFPEV_UP
);
555 fill_string_list(struct blob_attr
*attr
, char **argv
, int max
)
557 struct blob_attr
*cur
;
564 blobmsg_for_each_attr(cur
, attr
, rem
) {
565 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
568 if (!blobmsg_check_attr(cur
, NULL
))
571 argv
[argc
++] = blobmsg_data(cur
);
582 proto_shell_run_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
584 static char *argv
[64];
585 static char *env
[32];
587 if (state
->sm
== S_TEARDOWN
|| state
->sm
== S_SETUP_ABORT
)
588 return UBUS_STATUS_PERMISSION_DENIED
;
590 if (!tb
[NOTIFY_COMMAND
])
593 if (!fill_string_list(tb
[NOTIFY_COMMAND
], argv
, ARRAY_SIZE(argv
)))
596 if (!fill_string_list(tb
[NOTIFY_ENV
], env
, ARRAY_SIZE(env
)))
599 netifd_start_process((const char **) argv
, (char **) env
, &state
->proto_task
);
604 return UBUS_STATUS_INVALID_ARGUMENT
;
608 proto_shell_kill_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
610 unsigned int signal
= ~0;
612 if (tb
[NOTIFY_SIGNAL
])
613 signal
= blobmsg_get_u32(tb
[NOTIFY_SIGNAL
]);
618 if (state
->proto_task
.uloop
.pending
) {
619 if (signal
== SIGTERM
|| signal
== SIGKILL
)
620 state
->proto_task_killed
= true;
621 kill(state
->proto_task
.uloop
.pid
, signal
);
628 proto_shell_notify_error(struct proto_shell_state
*state
, struct blob_attr
**tb
)
630 struct blob_attr
*cur
;
635 if (!tb
[NOTIFY_ERROR
])
636 return UBUS_STATUS_INVALID_ARGUMENT
;
638 blobmsg_for_each_attr(cur
, tb
[NOTIFY_ERROR
], rem
) {
639 if (n_data
+ 1 == ARRAY_SIZE(data
))
642 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
645 if (!blobmsg_check_attr(cur
, NULL
))
648 data
[n_data
++] = blobmsg_data(cur
);
654 interface_add_error(state
->proto
.iface
, state
->handler
->proto
.name
,
655 data
[0], (const char **) &data
[1], n_data
- 1);
660 return UBUS_STATUS_INVALID_ARGUMENT
;
664 proto_shell_block_restart(struct proto_shell_state
*state
, struct blob_attr
**tb
)
666 state
->proto
.iface
->autostart
= false;
671 proto_shell_set_available(struct proto_shell_state
*state
, struct blob_attr
**tb
)
673 if (!tb
[NOTIFY_AVAILABLE
])
674 return UBUS_STATUS_INVALID_ARGUMENT
;
676 interface_set_available(state
->proto
.iface
, blobmsg_get_bool(tb
[NOTIFY_AVAILABLE
]));
681 proto_shell_add_host_dependency(struct proto_shell_state
*state
, struct blob_attr
**tb
)
683 struct proto_shell_dependency
*dep
;
684 const char *ifname
= tb
[NOTIFY_IFNAME
] ? blobmsg_data(tb
[NOTIFY_IFNAME
]) : "";
685 const char *host
= tb
[NOTIFY_HOST
] ? blobmsg_data(tb
[NOTIFY_HOST
]) : "";
687 if (state
->sm
== S_TEARDOWN
|| state
->sm
== S_SETUP_ABORT
)
688 return UBUS_STATUS_PERMISSION_DENIED
;
690 dep
= calloc(1, sizeof(*dep
) + strlen(ifname
) + 1);
692 if (!host
[0] && ifname
[0]) {
694 } else if (inet_pton(AF_INET
, host
, &dep
->host
) < 1) {
695 if (inet_pton(AF_INET6
, host
, &dep
->host
) < 1) {
697 return UBUS_STATUS_INVALID_ARGUMENT
;
704 strcpy(dep
->interface
, ifname
);
706 dep
->dep
.cb
= proto_shell_if_up_cb
;
707 interface_add_user(&dep
->dep
, NULL
);
708 list_add(&dep
->list
, &state
->deps
);
709 proto_shell_update_host_dep(dep
);
711 return UBUS_STATUS_NOT_FOUND
;
717 proto_shell_setup_failed(struct proto_shell_state
*state
)
723 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
726 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
731 ret
= UBUS_STATUS_PERMISSION_DENIED
;
738 proto_shell_notify(struct interface_proto_state
*proto
, struct blob_attr
*attr
)
740 struct proto_shell_state
*state
;
741 struct blob_attr
*tb
[__NOTIFY_LAST
];
743 state
= container_of(proto
, struct proto_shell_state
, proto
);
745 blobmsg_parse(notify_attr
, __NOTIFY_LAST
, tb
, blob_data(attr
), blob_len(attr
));
746 if (!tb
[NOTIFY_ACTION
])
747 return UBUS_STATUS_INVALID_ARGUMENT
;
749 switch(blobmsg_get_u32(tb
[NOTIFY_ACTION
])) {
751 return proto_shell_update_link(state
, attr
, tb
);
753 return proto_shell_run_command(state
, tb
);
755 return proto_shell_kill_command(state
, tb
);
757 return proto_shell_notify_error(state
, tb
);
759 return proto_shell_block_restart(state
, tb
);
761 return proto_shell_set_available(state
, tb
);
763 return proto_shell_add_host_dependency(state
, tb
);
765 return proto_shell_setup_failed(state
);
767 return UBUS_STATUS_INVALID_ARGUMENT
;
771 static struct interface_proto_state
*
772 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
773 struct blob_attr
*attr
)
775 struct proto_shell_state
*state
;
777 state
= calloc(1, sizeof(*state
));
778 INIT_LIST_HEAD(&state
->deps
);
780 state
->config
= malloc(blob_pad_len(attr
));
784 memcpy(state
->config
, attr
, blob_pad_len(attr
));
785 state
->proto
.free
= proto_shell_free
;
786 state
->proto
.notify
= proto_shell_notify
;
787 state
->proto
.cb
= proto_shell_handler
;
788 state
->teardown_timeout
.cb
= proto_shell_teardown_timeout_cb
;
789 state
->script_task
.cb
= proto_shell_script_cb
;
790 state
->script_task
.dir_fd
= proto_fd
;
791 state
->script_task
.log_prefix
= iface
->name
;
792 state
->proto_task
.cb
= proto_shell_task_cb
;
793 state
->proto_task
.dir_fd
= proto_fd
;
794 state
->proto_task
.log_prefix
= iface
->name
;
795 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
797 return &state
->proto
;
805 proto_shell_add_handler(const char *script
, const char *name
, json_object
*obj
)
807 struct proto_shell_handler
*handler
;
808 struct proto_handler
*proto
;
809 json_object
*config
, *tmp
;
810 char *proto_name
, *script_name
;
812 handler
= calloc_a(sizeof(*handler
),
813 &proto_name
, strlen(name
) + 1,
814 &script_name
, strlen(script
) + 1);
818 handler
->script_name
= strcpy(script_name
, script
);
820 proto
= &handler
->proto
;
821 proto
->name
= strcpy(proto_name
, name
);
822 proto
->config_params
= &handler
->config
;
823 proto
->attach
= proto_shell_attach
;
825 tmp
= json_get_field(obj
, "no-device", json_type_boolean
);
826 if (tmp
&& json_object_get_boolean(tmp
))
827 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
829 tmp
= json_get_field(obj
, "no-proto-task", json_type_boolean
);
830 handler
->no_proto_task
= tmp
&& json_object_get_boolean(tmp
);
832 tmp
= json_get_field(obj
, "available", json_type_boolean
);
833 if (tmp
&& json_object_get_boolean(tmp
))
834 handler
->proto
.flags
|= PROTO_FLAG_INIT_AVAILABLE
;
836 tmp
= json_get_field(obj
, "renew-handler", json_type_boolean
);
837 if (tmp
&& json_object_get_boolean(tmp
))
838 handler
->proto
.flags
|= PROTO_FLAG_RENEW_AVAILABLE
;
840 tmp
= json_get_field(obj
, "lasterror", json_type_boolean
);
841 if (tmp
&& json_object_get_boolean(tmp
))
842 handler
->proto
.flags
|= PROTO_FLAG_LASTERROR
;
844 config
= json_get_field(obj
, "config", json_type_array
);
846 handler
->config_buf
= netifd_handler_parse_config(&handler
->config
, config
);
848 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
849 add_proto_handler(proto
);
852 void proto_shell_init(void)
854 proto_fd
= netifd_open_subdir("proto");
858 netifd_init_script_handlers(proto_fd
, proto_shell_add_handler
);