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
;
54 struct proto_shell_state
*proto
;
55 struct interface_user dep
;
64 struct proto_shell_state
{
65 struct interface_proto_state proto
;
66 struct proto_shell_handler
*handler
;
67 struct blob_attr
*config
;
69 struct uloop_timeout teardown_timeout
;
72 * Teardown and setup interface again if it is still not up (IFS_UP)
73 * after checkup_interval seconds since previous attempt. This check
74 * will be disabled when the config option "checkup_interval" is
75 * missing or has a negative value
78 struct uloop_timeout checkup_timeout
;
80 struct netifd_process script_task
;
81 struct netifd_process proto_task
;
83 enum proto_shell_sm sm
;
84 bool proto_task_killed
;
89 struct list_head deps
;
93 proto_shell_check_dependencies(struct proto_shell_state
*state
)
95 struct proto_shell_dependency
*dep
;
96 bool available
= true;
98 list_for_each_entry(dep
, &state
->deps
, list
) {
106 interface_set_available(state
->proto
.iface
, available
);
110 proto_shell_if_up_cb(struct interface_user
*dep
, struct interface
*iface
,
111 enum interface_event ev
);
113 proto_shell_if_down_cb(struct interface_user
*dep
, struct interface
*iface
,
114 enum interface_event ev
);
117 proto_shell_update_host_dep(struct proto_shell_dependency
*dep
)
119 struct interface
*iface
= NULL
;
124 if (dep
->interface
[0]) {
125 iface
= vlist_find(&interfaces
, dep
->interface
, iface
, node
);
127 if (!iface
|| iface
->state
!= IFS_UP
)
132 iface
= interface_ip_add_target_route(&dep
->host
, dep
->v6
, iface
);
137 interface_remove_user(&dep
->dep
);
138 dep
->dep
.cb
= proto_shell_if_down_cb
;
139 interface_add_user(&dep
->dep
, iface
);
142 proto_shell_check_dependencies(dep
->proto
);
146 proto_shell_clear_host_dep(struct proto_shell_state
*state
)
148 struct proto_shell_dependency
*dep
, *tmp
;
150 list_for_each_entry_safe(dep
, tmp
, &state
->deps
, list
) {
151 interface_remove_user(&dep
->dep
);
152 list_del(&dep
->list
);
158 proto_shell_handler(struct interface_proto_state
*proto
,
159 enum interface_proto_cmd cmd
, bool force
)
161 struct proto_shell_state
*state
;
162 struct proto_shell_handler
*handler
;
163 struct netifd_process
*proc
;
164 static char error_buf
[32];
169 int ret
, i
= 0, j
= 0;
171 state
= container_of(proto
, struct proto_shell_state
, proto
);
172 handler
= state
->handler
;
173 proc
= &state
->script_task
;
175 if (cmd
== PROTO_CMD_SETUP
) {
179 state
->last_error
= -1;
180 proto_shell_clear_host_dep(state
);
187 } else if (cmd
== PROTO_CMD_RENEW
) {
188 if (!(handler
->proto
.flags
& PROTO_FLAG_RENEW_AVAILABLE
))
191 if (state
->script_task
.uloop
.pending
) {
192 state
->renew_pending
= true;
196 state
->renew_pending
= false;
201 if (state
->script_task
.uloop
.pending
) {
202 uloop_timeout_set(&state
->teardown_timeout
, 1000);
203 kill(state
->script_task
.uloop
.pid
, SIGTERM
);
204 if (state
->proto_task
.uloop
.pending
)
205 kill(state
->proto_task
.uloop
.pid
, SIGTERM
);
206 state
->renew_pending
= false;
207 state
->sm
= S_SETUP_ABORT
;
210 /* if no script task is running */
214 state
->renew_pending
= false;
215 state
->sm
= S_TEARDOWN
;
216 if (state
->last_error
>= 0) {
217 snprintf(error_buf
, sizeof(error_buf
), "ERROR=%d", state
->last_error
);
218 envp
[j
++] = error_buf
;
220 uloop_timeout_set(&state
->teardown_timeout
, 5000);
231 D(INTERFACE
, "run %s for interface '%s'\n", action
, proto
->iface
->name
);
232 config
= blobmsg_format_json(state
->config
, true);
236 argv
[i
++] = handler
->script_name
;
237 argv
[i
++] = handler
->proto
.name
;
239 argv
[i
++] = proto
->iface
->name
;
241 if (proto
->iface
->main_dev
.dev
)
242 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
246 ret
= netifd_start_process(argv
, envp
, proc
);
253 proto_shell_if_up_cb(struct interface_user
*dep
, struct interface
*iface
,
254 enum interface_event ev
)
256 struct proto_shell_dependency
*pdep
;
258 if (ev
!= IFEV_UP
&& ev
!= IFEV_UPDATE
)
261 pdep
= container_of(dep
, struct proto_shell_dependency
, dep
);
262 proto_shell_update_host_dep(pdep
);
266 proto_shell_if_down_cb(struct interface_user
*dep
, struct interface
*iface
,
267 enum interface_event ev
)
269 struct proto_shell_dependency
*pdep
;
270 struct proto_shell_state
*state
;
272 if (ev
== IFEV_UP
|| ev
== IFEV_UPDATE
)
275 pdep
= container_of(dep
, struct proto_shell_dependency
, dep
);
276 interface_remove_user(dep
);
277 dep
->cb
= proto_shell_if_up_cb
;
278 interface_add_user(dep
, NULL
);
281 if (state
->sm
== S_IDLE
) {
282 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
283 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
288 proto_shell_task_finish(struct proto_shell_state
*state
,
289 struct netifd_process
*task
)
293 if (task
== &state
->proto_task
)
294 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
297 if (task
== &state
->proto_task
)
298 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
,
300 else if (task
== &state
->script_task
) {
301 if (state
->renew_pending
)
302 proto_shell_handler(&state
->proto
,
303 PROTO_CMD_RENEW
, false);
304 else if (!(state
->handler
->proto
.flags
& PROTO_FLAG_NO_TASK
) &&
305 !state
->proto_task
.uloop
.pending
&&
306 state
->sm
== S_SETUP
)
307 proto_shell_handler(&state
->proto
,
311 /* check up status after setup attempt by this script_task */
312 if (state
->sm
== S_SETUP
&& state
->checkup_interval
> 0) {
313 uloop_timeout_set(&state
->checkup_timeout
,
314 state
->checkup_interval
* 1000);
320 if (state
->script_task
.uloop
.pending
||
321 state
->proto_task
.uloop
.pending
)
324 /* completed aborting all tasks, now idle */
325 uloop_timeout_cancel(&state
->teardown_timeout
);
326 uloop_timeout_cancel(&state
->checkup_timeout
);
328 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
332 if (state
->script_task
.uloop
.pending
)
335 if (state
->proto_task
.uloop
.pending
) {
336 if (!state
->proto_task_killed
)
337 kill(state
->proto_task
.uloop
.pid
, SIGTERM
);
341 /* completed tearing down all tasks, now idle */
342 uloop_timeout_cancel(&state
->teardown_timeout
);
343 uloop_timeout_cancel(&state
->checkup_timeout
);
345 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
351 proto_shell_teardown_timeout_cb(struct uloop_timeout
*timeout
)
353 struct proto_shell_state
*state
;
355 state
= container_of(timeout
, struct proto_shell_state
, teardown_timeout
);
357 netifd_kill_process(&state
->script_task
);
358 netifd_kill_process(&state
->proto_task
);
359 proto_shell_task_finish(state
, NULL
);
363 proto_shell_script_cb(struct netifd_process
*p
, int ret
)
365 struct proto_shell_state
*state
;
367 state
= container_of(p
, struct proto_shell_state
, script_task
);
368 proto_shell_task_finish(state
, p
);
372 proto_shell_task_cb(struct netifd_process
*p
, int ret
)
374 struct proto_shell_state
*state
;
376 state
= container_of(p
, struct proto_shell_state
, proto_task
);
378 if (state
->sm
== S_IDLE
|| state
->sm
== S_SETUP
)
379 state
->last_error
= WEXITSTATUS(ret
);
381 proto_shell_task_finish(state
, p
);
385 proto_shell_free(struct interface_proto_state
*proto
)
387 struct proto_shell_state
*state
;
389 state
= container_of(proto
, struct proto_shell_state
, proto
);
390 uloop_timeout_cancel(&state
->teardown_timeout
);
391 uloop_timeout_cancel(&state
->checkup_timeout
);
392 proto_shell_clear_host_dep(state
);
393 netifd_kill_process(&state
->script_task
);
394 netifd_kill_process(&state
->proto_task
);
400 proto_shell_parse_route_list(struct interface
*iface
, struct blob_attr
*attr
,
403 struct blob_attr
*cur
;
406 blobmsg_for_each_attr(cur
, attr
, rem
) {
407 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
) {
408 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur
));
412 interface_ip_add_route(iface
, cur
, v6
);
417 proto_shell_parse_neighbor_list(struct interface
*iface
, struct blob_attr
*attr
,
420 struct blob_attr
*cur
;
423 blobmsg_for_each_attr(cur
, attr
, rem
) {
424 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
) {
425 DPRINTF("Ignore wrong neighbor type: %d\n", blobmsg_type(cur
));
429 interface_ip_add_neighbor(iface
, cur
, v6
);
434 proto_shell_parse_data(struct interface
*iface
, struct blob_attr
*attr
)
436 struct blob_attr
*cur
;
439 blobmsg_for_each_attr(cur
, attr
, rem
)
440 interface_add_data(iface
, cur
);
443 static struct device
*
444 proto_shell_create_tunnel(const char *name
, struct blob_attr
*attr
)
449 memset(&b
, 0, sizeof(b
));
450 blob_buf_init(&b
, 0);
451 blob_put(&b
, 0, blobmsg_data(attr
), blobmsg_data_len(attr
));
452 dev
= device_create(name
, &tunnel_device_type
, blob_data(b
.head
));
481 static const struct blobmsg_policy notify_attr
[__NOTIFY_LAST
] = {
482 [NOTIFY_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_INT32
},
483 [NOTIFY_ERROR
] = { .name
= "error", .type
= BLOBMSG_TYPE_ARRAY
},
484 [NOTIFY_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_ARRAY
},
485 [NOTIFY_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_ARRAY
},
486 [NOTIFY_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
487 [NOTIFY_AVAILABLE
] = { .name
= "available", .type
= BLOBMSG_TYPE_BOOL
},
488 [NOTIFY_LINK_UP
] = { .name
= "link-up", .type
= BLOBMSG_TYPE_BOOL
},
489 [NOTIFY_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
490 [NOTIFY_ADDR_EXT
] = { .name
= "address-external", .type
= BLOBMSG_TYPE_BOOL
},
491 [NOTIFY_ROUTES
] = { .name
= "routes", .type
= BLOBMSG_TYPE_ARRAY
},
492 [NOTIFY_ROUTES6
] = { .name
= "routes6", .type
= BLOBMSG_TYPE_ARRAY
},
493 [NOTIFY_TUNNEL
] = { .name
= "tunnel", .type
= BLOBMSG_TYPE_TABLE
},
494 [NOTIFY_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
495 [NOTIFY_KEEP
] = { .name
= "keep", .type
= BLOBMSG_TYPE_BOOL
},
496 [NOTIFY_HOST
] = { .name
= "host", .type
= BLOBMSG_TYPE_STRING
},
497 [NOTIFY_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
498 [NOTIFY_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
499 [NOTIFY_NEIGHBORS
]= {.name
= "neighbor", .type
= BLOBMSG_TYPE_ARRAY
},
500 [NOTIFY_NEIGHBORS6
]= {.name
= "neighbor6", .type
= BLOBMSG_TYPE_ARRAY
},
504 proto_shell_update_link(struct proto_shell_state
*state
, struct blob_attr
*data
, struct blob_attr
**tb
)
506 struct interface
*iface
= state
->proto
.iface
;
507 struct blob_attr
*cur
;
511 bool addr_ext
= false;
515 if (state
->sm
== S_TEARDOWN
|| state
->sm
== S_SETUP_ABORT
)
516 return UBUS_STATUS_PERMISSION_DENIED
;
518 if (!tb
[NOTIFY_LINK_UP
])
519 return UBUS_STATUS_INVALID_ARGUMENT
;
521 up
= blobmsg_get_bool(tb
[NOTIFY_LINK_UP
]);
523 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
527 if ((cur
= tb
[NOTIFY_KEEP
]) != NULL
)
528 keep
= blobmsg_get_bool(cur
);
530 if ((cur
= tb
[NOTIFY_ADDR_EXT
]) != NULL
) {
531 addr_ext
= blobmsg_get_bool(cur
);
536 if (iface
->state
!= IFS_UP
|| !iface
->l3_dev
.dev
)
540 dev
= iface
->main_dev
.dev
;
541 if (tb
[NOTIFY_IFNAME
]) {
543 devname
= blobmsg_data(tb
[NOTIFY_IFNAME
]);
544 if (tb
[NOTIFY_TUNNEL
])
545 dev
= proto_shell_create_tunnel(devname
, tb
[NOTIFY_TUNNEL
]);
547 dev
= device_get(devname
, dev_create
);
551 return UBUS_STATUS_INVALID_ARGUMENT
;
553 interface_set_l3_dev(iface
, dev
);
554 if (device_claim(&iface
->l3_dev
) < 0)
555 return UBUS_STATUS_UNKNOWN_ERROR
;
557 device_set_present(dev
, true);
560 interface_update_start(iface
, keep
);
562 proto_apply_ip_settings(iface
, data
, addr_ext
);
564 if ((cur
= tb
[NOTIFY_ROUTES
]) != NULL
)
565 proto_shell_parse_route_list(state
->proto
.iface
, cur
, false);
567 if ((cur
= tb
[NOTIFY_ROUTES6
]) != NULL
)
568 proto_shell_parse_route_list(state
->proto
.iface
, cur
, true);
570 if ((cur
= tb
[NOTIFY_NEIGHBORS
]) != NULL
)
571 proto_shell_parse_neighbor_list(state
->proto
.iface
, cur
, false);
573 if ((cur
= tb
[NOTIFY_NEIGHBORS6
]) != NULL
)
574 proto_shell_parse_neighbor_list(state
->proto
.iface
, cur
, true);
576 if ((cur
= tb
[NOTIFY_DNS
]))
577 interface_add_dns_server_list(&iface
->proto_ip
, cur
);
579 if ((cur
= tb
[NOTIFY_DNS_SEARCH
]))
580 interface_add_dns_search_list(&iface
->proto_ip
, cur
);
582 if ((cur
= tb
[NOTIFY_DATA
]))
583 proto_shell_parse_data(state
->proto
.iface
, cur
);
585 interface_update_complete(state
->proto
.iface
);
587 if ((state
->sm
!= S_SETUP_ABORT
) && (state
->sm
!= S_TEARDOWN
)) {
588 state
->proto
.proto_event(&state
->proto
, IFPEV_UP
);
596 fill_string_list(struct blob_attr
*attr
, char **argv
, int max
)
598 struct blob_attr
*cur
;
605 blobmsg_for_each_attr(cur
, attr
, rem
) {
606 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
609 if (!blobmsg_check_attr(cur
, false))
612 argv
[argc
++] = blobmsg_data(cur
);
623 proto_shell_run_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
625 static char *argv
[64];
626 static char *env
[32];
628 if (state
->sm
== S_TEARDOWN
|| state
->sm
== S_SETUP_ABORT
)
629 return UBUS_STATUS_PERMISSION_DENIED
;
631 if (!tb
[NOTIFY_COMMAND
])
634 if (!fill_string_list(tb
[NOTIFY_COMMAND
], argv
, ARRAY_SIZE(argv
)))
637 if (!fill_string_list(tb
[NOTIFY_ENV
], env
, ARRAY_SIZE(env
)))
640 netifd_start_process((const char **) argv
, (char **) env
, &state
->proto_task
);
645 return UBUS_STATUS_INVALID_ARGUMENT
;
649 proto_shell_kill_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
651 unsigned int signal
= ~0;
653 if (tb
[NOTIFY_SIGNAL
])
654 signal
= blobmsg_get_u32(tb
[NOTIFY_SIGNAL
]);
659 if (state
->proto_task
.uloop
.pending
) {
660 if (signal
== SIGTERM
|| signal
== SIGKILL
)
661 state
->proto_task_killed
= true;
662 kill(state
->proto_task
.uloop
.pid
, signal
);
669 proto_shell_notify_error(struct proto_shell_state
*state
, struct blob_attr
**tb
)
671 struct blob_attr
*cur
;
676 if (!tb
[NOTIFY_ERROR
])
677 return UBUS_STATUS_INVALID_ARGUMENT
;
679 blobmsg_for_each_attr(cur
, tb
[NOTIFY_ERROR
], rem
) {
680 if (n_data
+ 1 == ARRAY_SIZE(data
))
683 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
686 if (!blobmsg_check_attr(cur
, false))
689 data
[n_data
++] = blobmsg_data(cur
);
695 interface_add_error(state
->proto
.iface
, state
->handler
->proto
.name
,
696 data
[0], (const char **) &data
[1], n_data
- 1);
701 return UBUS_STATUS_INVALID_ARGUMENT
;
705 proto_shell_block_restart(struct proto_shell_state
*state
, struct blob_attr
**tb
)
707 state
->proto
.iface
->autostart
= false;
712 proto_shell_set_available(struct proto_shell_state
*state
, struct blob_attr
**tb
)
714 if (!tb
[NOTIFY_AVAILABLE
])
715 return UBUS_STATUS_INVALID_ARGUMENT
;
717 interface_set_available(state
->proto
.iface
, blobmsg_get_bool(tb
[NOTIFY_AVAILABLE
]));
722 proto_shell_add_host_dependency(struct proto_shell_state
*state
, struct blob_attr
**tb
)
724 struct proto_shell_dependency
*dep
;
725 const char *ifname
= tb
[NOTIFY_IFNAME
] ? blobmsg_data(tb
[NOTIFY_IFNAME
]) : "";
726 const char *host
= tb
[NOTIFY_HOST
] ? blobmsg_data(tb
[NOTIFY_HOST
]) : "";
728 if (state
->sm
== S_TEARDOWN
|| state
->sm
== S_SETUP_ABORT
)
729 return UBUS_STATUS_PERMISSION_DENIED
;
731 dep
= calloc(1, sizeof(*dep
) + strlen(ifname
) + 1);
733 return UBUS_STATUS_UNKNOWN_ERROR
;
735 if (!host
[0] && ifname
[0]) {
737 } else if (inet_pton(AF_INET
, host
, &dep
->host
) < 1) {
738 if (inet_pton(AF_INET6
, host
, &dep
->host
) < 1) {
740 return UBUS_STATUS_INVALID_ARGUMENT
;
747 strcpy(dep
->interface
, ifname
);
749 dep
->dep
.cb
= proto_shell_if_up_cb
;
750 interface_add_user(&dep
->dep
, NULL
);
751 list_add(&dep
->list
, &state
->deps
);
752 proto_shell_update_host_dep(dep
);
754 return UBUS_STATUS_NOT_FOUND
;
760 proto_shell_setup_failed(struct proto_shell_state
*state
)
766 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
769 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
774 ret
= UBUS_STATUS_PERMISSION_DENIED
;
781 proto_shell_notify(struct interface_proto_state
*proto
, struct blob_attr
*attr
)
783 struct proto_shell_state
*state
;
784 struct blob_attr
*tb
[__NOTIFY_LAST
];
786 state
= container_of(proto
, struct proto_shell_state
, proto
);
788 blobmsg_parse(notify_attr
, __NOTIFY_LAST
, tb
, blob_data(attr
), blob_len(attr
));
789 if (!tb
[NOTIFY_ACTION
])
790 return UBUS_STATUS_INVALID_ARGUMENT
;
792 switch(blobmsg_get_u32(tb
[NOTIFY_ACTION
])) {
794 return proto_shell_update_link(state
, attr
, tb
);
796 return proto_shell_run_command(state
, tb
);
798 return proto_shell_kill_command(state
, tb
);
800 return proto_shell_notify_error(state
, tb
);
802 return proto_shell_block_restart(state
, tb
);
804 return proto_shell_set_available(state
, tb
);
806 return proto_shell_add_host_dependency(state
, tb
);
808 return proto_shell_setup_failed(state
);
810 return UBUS_STATUS_INVALID_ARGUMENT
;
815 proto_shell_checkup_timeout_cb(struct uloop_timeout
*timeout
)
817 struct proto_shell_state
*state
= container_of(timeout
, struct
818 proto_shell_state
, checkup_timeout
);
819 struct interface_proto_state
*proto
= &state
->proto
;
820 struct interface
*iface
= proto
->iface
;
822 if (!iface
->autostart
)
825 if (iface
->state
== IFS_UP
)
828 D(INTERFACE
, "Interface '%s' is not up after %d sec\n",
829 iface
->name
, state
->checkup_interval
);
830 proto_shell_handler(proto
, PROTO_CMD_TEARDOWN
, false);
834 proto_shell_checkup_attach(struct proto_shell_state
*state
,
835 const struct blob_attr
*attr
)
837 struct blob_attr
*tb
;
838 struct blobmsg_policy checkup_policy
= {
839 .name
= "checkup_interval",
840 .type
= BLOBMSG_TYPE_INT32
843 blobmsg_parse(&checkup_policy
, 1, &tb
, blob_data(attr
), blob_len(attr
));
845 state
->checkup_interval
= -1;
846 state
->checkup_timeout
.cb
= NULL
;
848 state
->checkup_interval
= blobmsg_get_u32(tb
);
849 state
->checkup_timeout
.cb
= proto_shell_checkup_timeout_cb
;
853 static struct interface_proto_state
*
854 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
855 struct blob_attr
*attr
)
857 struct proto_shell_state
*state
;
859 state
= calloc(1, sizeof(*state
));
863 INIT_LIST_HEAD(&state
->deps
);
865 state
->config
= malloc(blob_pad_len(attr
));
869 memcpy(state
->config
, attr
, blob_pad_len(attr
));
870 proto_shell_checkup_attach(state
, state
->config
);
871 state
->proto
.free
= proto_shell_free
;
872 state
->proto
.notify
= proto_shell_notify
;
873 state
->proto
.cb
= proto_shell_handler
;
874 state
->teardown_timeout
.cb
= proto_shell_teardown_timeout_cb
;
875 state
->script_task
.cb
= proto_shell_script_cb
;
876 state
->script_task
.dir_fd
= proto_fd
;
877 state
->script_task
.log_prefix
= iface
->name
;
878 state
->proto_task
.cb
= proto_shell_task_cb
;
879 state
->proto_task
.dir_fd
= proto_fd
;
880 state
->proto_task
.log_prefix
= iface
->name
;
881 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
883 return &state
->proto
;
891 proto_shell_add_handler(const char *script
, const char *name
, json_object
*obj
)
893 struct proto_shell_handler
*handler
;
894 struct proto_handler
*proto
;
895 json_object
*config
, *tmp
;
896 char *proto_name
, *script_name
;
898 handler
= calloc_a(sizeof(*handler
),
899 &proto_name
, strlen(name
) + 1,
900 &script_name
, strlen(script
) + 1);
904 handler
->script_name
= strcpy(script_name
, script
);
906 proto
= &handler
->proto
;
907 proto
->name
= strcpy(proto_name
, name
);
908 proto
->config_params
= &handler
->config
;
909 proto
->attach
= proto_shell_attach
;
911 tmp
= json_get_field(obj
, "no-device", json_type_boolean
);
912 if (tmp
&& json_object_get_boolean(tmp
))
913 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
915 tmp
= json_get_field(obj
, "no-proto-task", json_type_boolean
);
916 if (tmp
&& json_object_get_boolean(tmp
))
917 handler
->proto
.flags
|= PROTO_FLAG_NO_TASK
;
919 tmp
= json_get_field(obj
, "available", json_type_boolean
);
920 if (tmp
&& json_object_get_boolean(tmp
))
921 handler
->proto
.flags
|= PROTO_FLAG_INIT_AVAILABLE
;
923 tmp
= json_get_field(obj
, "renew-handler", json_type_boolean
);
924 if (tmp
&& json_object_get_boolean(tmp
))
925 handler
->proto
.flags
|= PROTO_FLAG_RENEW_AVAILABLE
;
927 tmp
= json_get_field(obj
, "lasterror", json_type_boolean
);
928 if (tmp
&& json_object_get_boolean(tmp
))
929 handler
->proto
.flags
|= PROTO_FLAG_LASTERROR
;
931 tmp
= json_get_field(obj
, "teardown-on-l3-link-down", json_type_boolean
);
932 if (tmp
&& json_object_get_boolean(tmp
))
933 handler
->proto
.flags
|= PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
;
935 config
= json_get_field(obj
, "config", json_type_array
);
937 handler
->config_buf
= netifd_handler_parse_config(&handler
->config
, config
);
939 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
940 add_proto_handler(proto
);
943 void proto_shell_init(void)
945 proto_fd
= netifd_open_subdir("proto");
949 netifd_init_script_handlers(proto_fd
, proto_shell_add_handler
);