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
;
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
;
79 struct list_head deps
;
83 proto_shell_check_dependencies(struct proto_shell_state
*state
)
85 struct proto_shell_dependency
*dep
;
86 bool available
= true;
88 list_for_each_entry(dep
, &state
->deps
, list
) {
96 interface_set_available(state
->proto
.iface
, available
);
100 proto_shell_if_up_cb(struct interface_user
*dep
, struct interface
*iface
,
101 enum interface_event ev
);
103 proto_shell_if_down_cb(struct interface_user
*dep
, struct interface
*iface
,
104 enum interface_event ev
);
107 proto_shell_update_host_dep(struct proto_shell_dependency
*dep
)
109 struct interface
*iface
= NULL
;
114 if (dep
->interface
[0])
115 iface
= vlist_find(&interfaces
, dep
->interface
, iface
, node
);
117 iface
= interface_ip_add_target_route(&dep
->host
, dep
->v6
, iface
);
121 interface_remove_user(&dep
->dep
);
122 dep
->dep
.cb
= proto_shell_if_down_cb
;
123 interface_add_user(&dep
->dep
, iface
);
126 proto_shell_check_dependencies(dep
->proto
);
130 proto_shell_clear_host_dep(struct proto_shell_state
*state
)
132 struct proto_shell_dependency
*dep
, *tmp
;
134 list_for_each_entry_safe(dep
, tmp
, &state
->deps
, list
) {
135 interface_remove_user(&dep
->dep
);
136 list_del(&dep
->list
);
142 proto_shell_handler(struct interface_proto_state
*proto
,
143 enum interface_proto_cmd cmd
, bool force
)
145 struct proto_shell_state
*state
;
146 struct proto_shell_handler
*handler
;
147 struct netifd_process
*proc
;
148 static char error_buf
[32];
153 int ret
, i
= 0, j
= 0;
155 state
= container_of(proto
, struct proto_shell_state
, proto
);
156 handler
= state
->handler
;
157 proc
= &state
->script_task
;
159 if (cmd
== PROTO_CMD_SETUP
) {
163 state
->last_error
= -1;
164 proto_shell_clear_host_dep(state
);
176 } else if (cmd
== PROTO_CMD_RENEW
) {
177 if (!(handler
->proto
.flags
& PROTO_FLAG_RENEW_AVAILABLE
))
180 if (state
->script_task
.uloop
.pending
) {
181 state
->renew_pending
= true;
185 state
->renew_pending
= false;
190 if (state
->script_task
.uloop
.pending
) {
191 uloop_timeout_set(&state
->teardown_timeout
, 1000);
192 kill(state
->script_task
.uloop
.pid
, SIGTERM
);
193 if (state
->proto_task
.uloop
.pending
)
194 kill(state
->proto_task
.uloop
.pid
, SIGTERM
);
195 state
->renew_pending
= false;
196 state
->sm
= S_SETUP_ABORT
;
199 /* fall through if no script task is running */
202 state
->renew_pending
= false;
203 state
->sm
= S_TEARDOWN
;
204 if (state
->last_error
>= 0) {
205 snprintf(error_buf
, sizeof(error_buf
), "ERROR=%d", state
->last_error
);
206 envp
[j
++] = error_buf
;
208 uloop_timeout_set(&state
->teardown_timeout
, 5000);
219 D(INTERFACE
, "run %s for interface '%s'\n", action
, proto
->iface
->name
);
220 config
= blobmsg_format_json(state
->config
, true);
224 argv
[i
++] = handler
->script_name
;
225 argv
[i
++] = handler
->proto
.name
;
227 argv
[i
++] = proto
->iface
->name
;
229 if (proto
->iface
->main_dev
.dev
)
230 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
234 ret
= netifd_start_process(argv
, envp
, proc
);
241 proto_shell_if_up_cb(struct interface_user
*dep
, struct interface
*iface
,
242 enum interface_event ev
)
244 struct proto_shell_dependency
*pdep
;
246 if (ev
!= IFEV_UP
&& ev
!= IFEV_UPDATE
)
249 pdep
= container_of(dep
, struct proto_shell_dependency
, dep
);
250 proto_shell_update_host_dep(pdep
);
254 proto_shell_if_down_cb(struct interface_user
*dep
, struct interface
*iface
,
255 enum interface_event ev
)
257 struct proto_shell_dependency
*pdep
;
258 struct proto_shell_state
*state
;
260 if (ev
== IFEV_UP
|| ev
== IFEV_UPDATE
)
263 pdep
= container_of(dep
, struct proto_shell_dependency
, dep
);
264 interface_remove_user(dep
);
265 dep
->cb
= proto_shell_if_up_cb
;
266 interface_add_user(dep
, NULL
);
269 if (state
->sm
== S_IDLE
) {
270 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
271 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
276 proto_shell_task_finish(struct proto_shell_state
*state
,
277 struct netifd_process
*task
)
281 if (task
== &state
->proto_task
)
282 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
285 if (task
== &state
->proto_task
)
286 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
,
288 else if (task
== &state
->script_task
) {
289 if (state
->renew_pending
)
290 proto_shell_handler(&state
->proto
,
291 PROTO_CMD_RENEW
, false);
292 else if (!state
->handler
->no_proto_task
&&
293 !state
->proto_task
.uloop
.pending
&&
294 state
->sm
== S_SETUP
)
295 proto_shell_handler(&state
->proto
,
302 if (state
->script_task
.uloop
.pending
||
303 state
->proto_task
.uloop
.pending
)
306 uloop_timeout_cancel(&state
->teardown_timeout
);
308 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
312 if (state
->script_task
.uloop
.pending
)
315 if (state
->proto_task
.uloop
.pending
) {
316 if (!state
->proto_task_killed
)
317 kill(state
->proto_task
.uloop
.pid
, SIGTERM
);
321 uloop_timeout_cancel(&state
->teardown_timeout
);
323 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
329 proto_shell_teardown_timeout_cb(struct uloop_timeout
*timeout
)
331 struct proto_shell_state
*state
;
333 state
= container_of(timeout
, struct proto_shell_state
, teardown_timeout
);
335 netifd_kill_process(&state
->script_task
);
336 netifd_kill_process(&state
->proto_task
);
337 proto_shell_task_finish(state
, NULL
);
341 proto_shell_script_cb(struct netifd_process
*p
, int ret
)
343 struct proto_shell_state
*state
;
345 state
= container_of(p
, struct proto_shell_state
, script_task
);
346 proto_shell_task_finish(state
, p
);
350 proto_shell_task_cb(struct netifd_process
*p
, int ret
)
352 struct proto_shell_state
*state
;
354 state
= container_of(p
, struct proto_shell_state
, proto_task
);
356 if (state
->sm
== S_IDLE
|| state
->sm
== S_SETUP
)
357 state
->last_error
= WEXITSTATUS(ret
);
359 proto_shell_task_finish(state
, p
);
363 proto_shell_free(struct interface_proto_state
*proto
)
365 struct proto_shell_state
*state
;
367 state
= container_of(proto
, struct proto_shell_state
, proto
);
368 uloop_timeout_cancel(&state
->teardown_timeout
);
369 proto_shell_clear_host_dep(state
);
370 netifd_kill_process(&state
->script_task
);
371 netifd_kill_process(&state
->proto_task
);
377 proto_shell_parse_route_list(struct interface
*iface
, struct blob_attr
*attr
,
380 struct blob_attr
*cur
;
383 blobmsg_for_each_attr(cur
, attr
, rem
) {
384 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
) {
385 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur
));
389 interface_ip_add_route(iface
, cur
, v6
);
394 proto_shell_parse_data(struct interface
*iface
, struct blob_attr
*attr
)
396 struct blob_attr
*cur
;
399 blobmsg_for_each_attr(cur
, attr
, rem
)
400 interface_add_data(iface
, cur
);
403 static struct device
*
404 proto_shell_create_tunnel(const char *name
, struct blob_attr
*attr
)
409 memset(&b
, 0, sizeof(b
));
410 blob_buf_init(&b
, 0);
411 blob_put(&b
, 0, blobmsg_data(attr
), blobmsg_data_len(attr
));
412 dev
= device_create(name
, &tunnel_device_type
, blob_data(b
.head
));
439 static const struct blobmsg_policy notify_attr
[__NOTIFY_LAST
] = {
440 [NOTIFY_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_INT32
},
441 [NOTIFY_ERROR
] = { .name
= "error", .type
= BLOBMSG_TYPE_ARRAY
},
442 [NOTIFY_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_ARRAY
},
443 [NOTIFY_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_ARRAY
},
444 [NOTIFY_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
445 [NOTIFY_AVAILABLE
] = { .name
= "available", .type
= BLOBMSG_TYPE_BOOL
},
446 [NOTIFY_LINK_UP
] = { .name
= "link-up", .type
= BLOBMSG_TYPE_BOOL
},
447 [NOTIFY_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
448 [NOTIFY_ADDR_EXT
] = { .name
= "address-external", .type
= BLOBMSG_TYPE_BOOL
},
449 [NOTIFY_ROUTES
] = { .name
= "routes", .type
= BLOBMSG_TYPE_ARRAY
},
450 [NOTIFY_ROUTES6
] = { .name
= "routes6", .type
= BLOBMSG_TYPE_ARRAY
},
451 [NOTIFY_TUNNEL
] = { .name
= "tunnel", .type
= BLOBMSG_TYPE_TABLE
},
452 [NOTIFY_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
453 [NOTIFY_KEEP
] = { .name
= "keep", .type
= BLOBMSG_TYPE_BOOL
},
454 [NOTIFY_HOST
] = { .name
= "host", .type
= BLOBMSG_TYPE_STRING
},
455 [NOTIFY_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
456 [NOTIFY_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
460 proto_shell_update_link(struct proto_shell_state
*state
, struct blob_attr
*data
, struct blob_attr
**tb
)
462 struct interface
*iface
= state
->proto
.iface
;
463 struct blob_attr
*cur
;
467 bool addr_ext
= false;
471 if (state
->sm
== S_TEARDOWN
|| state
->sm
== S_SETUP_ABORT
)
472 return UBUS_STATUS_PERMISSION_DENIED
;
474 if (!tb
[NOTIFY_LINK_UP
])
475 return UBUS_STATUS_INVALID_ARGUMENT
;
477 up
= blobmsg_get_bool(tb
[NOTIFY_LINK_UP
]);
479 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
483 if ((cur
= tb
[NOTIFY_KEEP
]) != NULL
)
484 keep
= blobmsg_get_bool(cur
);
486 if ((cur
= tb
[NOTIFY_ADDR_EXT
]) != NULL
) {
487 addr_ext
= blobmsg_get_bool(cur
);
492 if (iface
->state
!= IFS_UP
|| !iface
->l3_dev
.dev
)
496 dev
= iface
->main_dev
.dev
;
497 if (tb
[NOTIFY_IFNAME
]) {
499 devname
= blobmsg_data(tb
[NOTIFY_IFNAME
]);
500 if (tb
[NOTIFY_TUNNEL
])
501 dev
= proto_shell_create_tunnel(devname
, tb
[NOTIFY_TUNNEL
]);
503 dev
= device_get(devname
, dev_create
);
507 return UBUS_STATUS_INVALID_ARGUMENT
;
509 interface_set_l3_dev(iface
, dev
);
510 if (device_claim(&iface
->l3_dev
) < 0)
511 return UBUS_STATUS_UNKNOWN_ERROR
;
513 device_set_present(dev
, true);
515 interface_update_start(iface
);
518 proto_apply_ip_settings(iface
, data
, addr_ext
);
520 if ((cur
= tb
[NOTIFY_ROUTES
]) != NULL
)
521 proto_shell_parse_route_list(state
->proto
.iface
, cur
, false);
523 if ((cur
= tb
[NOTIFY_ROUTES6
]) != NULL
)
524 proto_shell_parse_route_list(state
->proto
.iface
, cur
, true);
526 if ((cur
= tb
[NOTIFY_DNS
]))
527 interface_add_dns_server_list(&iface
->proto_ip
, cur
);
529 if ((cur
= tb
[NOTIFY_DNS_SEARCH
]))
530 interface_add_dns_search_list(&iface
->proto_ip
, cur
);
532 if ((cur
= tb
[NOTIFY_DATA
]))
533 proto_shell_parse_data(state
->proto
.iface
, cur
);
535 interface_update_complete(state
->proto
.iface
);
537 if ((state
->sm
!= S_SETUP_ABORT
) && (state
->sm
!= S_TEARDOWN
)) {
539 state
->proto
.proto_event(&state
->proto
, IFPEV_UP
);
547 fill_string_list(struct blob_attr
*attr
, char **argv
, int max
)
549 struct blob_attr
*cur
;
556 blobmsg_for_each_attr(cur
, attr
, rem
) {
557 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
560 if (!blobmsg_check_attr(cur
, NULL
))
563 argv
[argc
++] = blobmsg_data(cur
);
574 proto_shell_run_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
576 static char *argv
[64];
577 static char *env
[32];
579 if (state
->sm
== S_TEARDOWN
|| state
->sm
== S_SETUP_ABORT
)
580 return UBUS_STATUS_PERMISSION_DENIED
;
582 if (!tb
[NOTIFY_COMMAND
])
585 if (!fill_string_list(tb
[NOTIFY_COMMAND
], argv
, ARRAY_SIZE(argv
)))
588 if (!fill_string_list(tb
[NOTIFY_ENV
], env
, ARRAY_SIZE(env
)))
591 netifd_start_process((const char **) argv
, (char **) env
, &state
->proto_task
);
596 return UBUS_STATUS_INVALID_ARGUMENT
;
600 proto_shell_kill_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
602 unsigned int signal
= ~0;
604 if (tb
[NOTIFY_SIGNAL
])
605 signal
= blobmsg_get_u32(tb
[NOTIFY_SIGNAL
]);
610 if (state
->proto_task
.uloop
.pending
) {
611 if (signal
== SIGTERM
|| signal
== SIGKILL
)
612 state
->proto_task_killed
= true;
613 kill(state
->proto_task
.uloop
.pid
, signal
);
620 proto_shell_notify_error(struct proto_shell_state
*state
, struct blob_attr
**tb
)
622 struct blob_attr
*cur
;
627 if (!tb
[NOTIFY_ERROR
])
628 return UBUS_STATUS_INVALID_ARGUMENT
;
630 blobmsg_for_each_attr(cur
, tb
[NOTIFY_ERROR
], rem
) {
631 if (n_data
+ 1 == ARRAY_SIZE(data
))
634 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
637 if (!blobmsg_check_attr(cur
, NULL
))
640 data
[n_data
++] = blobmsg_data(cur
);
646 interface_add_error(state
->proto
.iface
, state
->handler
->proto
.name
,
647 data
[0], (const char **) &data
[1], n_data
- 1);
652 return UBUS_STATUS_INVALID_ARGUMENT
;
656 proto_shell_block_restart(struct proto_shell_state
*state
, struct blob_attr
**tb
)
658 state
->proto
.iface
->autostart
= false;
663 proto_shell_set_available(struct proto_shell_state
*state
, struct blob_attr
**tb
)
665 if (!tb
[NOTIFY_AVAILABLE
])
666 return UBUS_STATUS_INVALID_ARGUMENT
;
668 interface_set_available(state
->proto
.iface
, blobmsg_get_bool(tb
[NOTIFY_AVAILABLE
]));
673 proto_shell_add_host_dependency(struct proto_shell_state
*state
, struct blob_attr
**tb
)
675 struct proto_shell_dependency
*dep
;
676 struct blob_attr
*host
= tb
[NOTIFY_HOST
];
677 struct blob_attr
*ifname_a
= tb
[NOTIFY_IFNAME
];
678 const char *ifname_str
= ifname_a
? blobmsg_data(ifname_a
) : "";
681 if (state
->sm
== S_TEARDOWN
|| state
->sm
== S_SETUP_ABORT
)
682 return UBUS_STATUS_PERMISSION_DENIED
;
685 return UBUS_STATUS_INVALID_ARGUMENT
;
687 dep
= calloc_a(sizeof(*dep
), &ifname
, strlen(ifname_str
) + 1);
688 if (inet_pton(AF_INET
, blobmsg_data(host
), &dep
->host
) < 1) {
689 if (inet_pton(AF_INET6
, blobmsg_data(host
), &dep
->host
) < 1) {
691 return UBUS_STATUS_INVALID_ARGUMENT
;
698 dep
->interface
= strcpy(ifname
, ifname_str
);
700 dep
->dep
.cb
= proto_shell_if_up_cb
;
701 interface_add_user(&dep
->dep
, NULL
);
702 list_add(&dep
->list
, &state
->deps
);
703 proto_shell_update_host_dep(dep
);
705 return UBUS_STATUS_NOT_FOUND
;
711 proto_shell_setup_failed(struct proto_shell_state
*state
)
717 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
720 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
725 ret
= UBUS_STATUS_PERMISSION_DENIED
;
732 proto_shell_notify(struct interface_proto_state
*proto
, struct blob_attr
*attr
)
734 struct proto_shell_state
*state
;
735 struct blob_attr
*tb
[__NOTIFY_LAST
];
737 state
= container_of(proto
, struct proto_shell_state
, proto
);
739 blobmsg_parse(notify_attr
, __NOTIFY_LAST
, tb
, blob_data(attr
), blob_len(attr
));
740 if (!tb
[NOTIFY_ACTION
])
741 return UBUS_STATUS_INVALID_ARGUMENT
;
743 switch(blobmsg_get_u32(tb
[NOTIFY_ACTION
])) {
745 return proto_shell_update_link(state
, attr
, tb
);
747 return proto_shell_run_command(state
, tb
);
749 return proto_shell_kill_command(state
, tb
);
751 return proto_shell_notify_error(state
, tb
);
753 return proto_shell_block_restart(state
, tb
);
755 return proto_shell_set_available(state
, tb
);
757 return proto_shell_add_host_dependency(state
, tb
);
759 return proto_shell_setup_failed(state
);
761 return UBUS_STATUS_INVALID_ARGUMENT
;
765 static struct interface_proto_state
*
766 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
767 struct blob_attr
*attr
)
769 struct proto_shell_state
*state
;
771 state
= calloc(1, sizeof(*state
));
772 INIT_LIST_HEAD(&state
->deps
);
774 state
->config
= malloc(blob_pad_len(attr
));
778 memcpy(state
->config
, attr
, blob_pad_len(attr
));
779 state
->proto
.free
= proto_shell_free
;
780 state
->proto
.notify
= proto_shell_notify
;
781 state
->proto
.cb
= proto_shell_handler
;
782 state
->teardown_timeout
.cb
= proto_shell_teardown_timeout_cb
;
783 state
->script_task
.cb
= proto_shell_script_cb
;
784 state
->script_task
.dir_fd
= proto_fd
;
785 state
->script_task
.log_prefix
= iface
->name
;
786 state
->proto_task
.cb
= proto_shell_task_cb
;
787 state
->proto_task
.dir_fd
= proto_fd
;
788 state
->proto_task
.log_prefix
= iface
->name
;
789 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
791 return &state
->proto
;
799 proto_shell_add_handler(const char *script
, const char *name
, json_object
*obj
)
801 struct proto_shell_handler
*handler
;
802 struct proto_handler
*proto
;
803 json_object
*config
, *tmp
;
804 char *proto_name
, *script_name
;
806 handler
= calloc_a(sizeof(*handler
),
807 &proto_name
, strlen(name
) + 1,
808 &script_name
, strlen(script
) + 1);
812 handler
->script_name
= strcpy(script_name
, script
);
814 proto
= &handler
->proto
;
815 proto
->name
= strcpy(proto_name
, name
);
816 proto
->config_params
= &handler
->config
;
817 proto
->attach
= proto_shell_attach
;
819 tmp
= json_get_field(obj
, "no-device", json_type_boolean
);
820 if (tmp
&& json_object_get_boolean(tmp
))
821 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
823 tmp
= json_get_field(obj
, "no-proto-task", json_type_boolean
);
824 if (tmp
&& json_object_get_boolean(tmp
))
825 handler
->no_proto_task
= true;
827 handler
->no_proto_task
= false;
829 tmp
= json_get_field(obj
, "available", json_type_boolean
);
830 if (tmp
&& json_object_get_boolean(tmp
))
831 handler
->proto
.flags
|= PROTO_FLAG_INIT_AVAILABLE
;
833 tmp
= json_get_field(obj
, "renew-handler", json_type_boolean
);
834 if (tmp
&& json_object_get_boolean(tmp
))
835 handler
->proto
.flags
|= PROTO_FLAG_RENEW_AVAILABLE
;
837 tmp
= json_get_field(obj
, "lasterror", json_type_boolean
);
838 if (tmp
&& json_object_get_boolean(tmp
))
839 handler
->proto
.flags
|= PROTO_FLAG_LASTERROR
;
841 config
= json_get_field(obj
, "config", json_type_array
);
843 handler
->config_buf
= netifd_handler_parse_config(&handler
->config
, config
);
845 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
846 add_proto_handler(proto
);
849 void proto_shell_init(void)
851 proto_fd
= netifd_open_subdir("proto");
855 netifd_init_script_handlers(proto_fd
, proto_shell_add_handler
);