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
);
192 } else if (cmd
== PROTO_CMD_RENEW
) {
193 if (!(handler
->proto
.flags
& PROTO_FLAG_RENEW_AVAILABLE
))
196 if (state
->script_task
.uloop
.pending
) {
197 state
->renew_pending
= true;
201 state
->renew_pending
= false;
206 if (state
->script_task
.uloop
.pending
) {
207 uloop_timeout_set(&state
->teardown_timeout
, 1000);
208 kill(state
->script_task
.uloop
.pid
, SIGTERM
);
209 if (state
->proto_task
.uloop
.pending
)
210 kill(state
->proto_task
.uloop
.pid
, SIGTERM
);
211 state
->renew_pending
= false;
212 state
->sm
= S_SETUP_ABORT
;
215 /* if no script task is running */
219 state
->renew_pending
= false;
220 state
->sm
= S_TEARDOWN
;
221 if (state
->last_error
>= 0) {
222 snprintf(error_buf
, sizeof(error_buf
), "ERROR=%d", state
->last_error
);
223 envp
[j
++] = error_buf
;
225 uloop_timeout_set(&state
->teardown_timeout
, 5000);
236 D(INTERFACE
, "run %s for interface '%s'\n", action
, proto
->iface
->name
);
237 config
= blobmsg_format_json(state
->config
, true);
241 argv
[i
++] = handler
->script_name
;
242 argv
[i
++] = handler
->proto
.name
;
244 argv
[i
++] = proto
->iface
->name
;
246 if (proto
->iface
->main_dev
.dev
)
247 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
251 ret
= netifd_start_process(argv
, envp
, proc
);
258 proto_shell_if_up_cb(struct interface_user
*dep
, struct interface
*iface
,
259 enum interface_event ev
)
261 struct proto_shell_dependency
*pdep
;
263 if (ev
!= IFEV_UP
&& ev
!= IFEV_UPDATE
)
266 pdep
= container_of(dep
, struct proto_shell_dependency
, dep
);
267 proto_shell_update_host_dep(pdep
);
271 proto_shell_if_down_cb(struct interface_user
*dep
, struct interface
*iface
,
272 enum interface_event ev
)
274 struct proto_shell_dependency
*pdep
;
275 struct proto_shell_state
*state
;
277 if (ev
== IFEV_UP
|| ev
== IFEV_UPDATE
)
280 pdep
= container_of(dep
, struct proto_shell_dependency
, dep
);
281 interface_remove_user(dep
);
282 dep
->cb
= proto_shell_if_up_cb
;
283 interface_add_user(dep
, NULL
);
286 if (state
->sm
== S_IDLE
) {
287 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
288 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
293 proto_shell_task_finish(struct proto_shell_state
*state
,
294 struct netifd_process
*task
)
298 if (task
== &state
->proto_task
)
299 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
302 if (task
== &state
->proto_task
)
303 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
,
305 else if (task
== &state
->script_task
) {
306 if (state
->renew_pending
)
307 proto_shell_handler(&state
->proto
,
308 PROTO_CMD_RENEW
, false);
309 else if (!(state
->handler
->proto
.flags
& PROTO_FLAG_NO_TASK
) &&
310 !state
->proto_task
.uloop
.pending
&&
311 state
->sm
== S_SETUP
)
312 proto_shell_handler(&state
->proto
,
316 /* check up status after setup attempt by this script_task */
317 if (state
->sm
== S_SETUP
&& state
->checkup_interval
> 0) {
318 uloop_timeout_set(&state
->checkup_timeout
,
319 state
->checkup_interval
* 1000);
325 if (state
->script_task
.uloop
.pending
||
326 state
->proto_task
.uloop
.pending
)
329 /* completed aborting all tasks, now idle */
330 uloop_timeout_cancel(&state
->teardown_timeout
);
331 uloop_timeout_cancel(&state
->checkup_timeout
);
333 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
337 if (state
->script_task
.uloop
.pending
)
340 if (state
->proto_task
.uloop
.pending
) {
341 if (!state
->proto_task_killed
)
342 kill(state
->proto_task
.uloop
.pid
, SIGTERM
);
346 /* completed tearing down all tasks, now idle */
347 uloop_timeout_cancel(&state
->teardown_timeout
);
348 uloop_timeout_cancel(&state
->checkup_timeout
);
350 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
356 proto_shell_teardown_timeout_cb(struct uloop_timeout
*timeout
)
358 struct proto_shell_state
*state
;
360 state
= container_of(timeout
, struct proto_shell_state
, teardown_timeout
);
362 netifd_kill_process(&state
->script_task
);
363 netifd_kill_process(&state
->proto_task
);
364 proto_shell_task_finish(state
, NULL
);
368 proto_shell_script_cb(struct netifd_process
*p
, int ret
)
370 struct proto_shell_state
*state
;
372 state
= container_of(p
, struct proto_shell_state
, script_task
);
373 proto_shell_task_finish(state
, p
);
377 proto_shell_task_cb(struct netifd_process
*p
, int ret
)
379 struct proto_shell_state
*state
;
381 state
= container_of(p
, struct proto_shell_state
, proto_task
);
383 if (state
->sm
== S_IDLE
|| state
->sm
== S_SETUP
)
384 state
->last_error
= WEXITSTATUS(ret
);
386 proto_shell_task_finish(state
, p
);
390 proto_shell_free(struct interface_proto_state
*proto
)
392 struct proto_shell_state
*state
;
394 state
= container_of(proto
, struct proto_shell_state
, proto
);
395 uloop_timeout_cancel(&state
->teardown_timeout
);
396 uloop_timeout_cancel(&state
->checkup_timeout
);
397 proto_shell_clear_host_dep(state
);
398 netifd_kill_process(&state
->script_task
);
399 netifd_kill_process(&state
->proto_task
);
405 proto_shell_parse_route_list(struct interface
*iface
, struct blob_attr
*attr
,
408 struct blob_attr
*cur
;
411 blobmsg_for_each_attr(cur
, attr
, rem
) {
412 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
) {
413 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur
));
417 interface_ip_add_route(iface
, cur
, v6
);
422 proto_shell_parse_data(struct interface
*iface
, struct blob_attr
*attr
)
424 struct blob_attr
*cur
;
427 blobmsg_for_each_attr(cur
, attr
, rem
)
428 interface_add_data(iface
, cur
);
431 static struct device
*
432 proto_shell_create_tunnel(const char *name
, struct blob_attr
*attr
)
437 memset(&b
, 0, sizeof(b
));
438 blob_buf_init(&b
, 0);
439 blob_put(&b
, 0, blobmsg_data(attr
), blobmsg_data_len(attr
));
440 dev
= device_create(name
, &tunnel_device_type
, blob_data(b
.head
));
467 static const struct blobmsg_policy notify_attr
[__NOTIFY_LAST
] = {
468 [NOTIFY_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_INT32
},
469 [NOTIFY_ERROR
] = { .name
= "error", .type
= BLOBMSG_TYPE_ARRAY
},
470 [NOTIFY_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_ARRAY
},
471 [NOTIFY_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_ARRAY
},
472 [NOTIFY_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
473 [NOTIFY_AVAILABLE
] = { .name
= "available", .type
= BLOBMSG_TYPE_BOOL
},
474 [NOTIFY_LINK_UP
] = { .name
= "link-up", .type
= BLOBMSG_TYPE_BOOL
},
475 [NOTIFY_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
476 [NOTIFY_ADDR_EXT
] = { .name
= "address-external", .type
= BLOBMSG_TYPE_BOOL
},
477 [NOTIFY_ROUTES
] = { .name
= "routes", .type
= BLOBMSG_TYPE_ARRAY
},
478 [NOTIFY_ROUTES6
] = { .name
= "routes6", .type
= BLOBMSG_TYPE_ARRAY
},
479 [NOTIFY_TUNNEL
] = { .name
= "tunnel", .type
= BLOBMSG_TYPE_TABLE
},
480 [NOTIFY_DATA
] = { .name
= "data", .type
= BLOBMSG_TYPE_TABLE
},
481 [NOTIFY_KEEP
] = { .name
= "keep", .type
= BLOBMSG_TYPE_BOOL
},
482 [NOTIFY_HOST
] = { .name
= "host", .type
= BLOBMSG_TYPE_STRING
},
483 [NOTIFY_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
484 [NOTIFY_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
488 proto_shell_update_link(struct proto_shell_state
*state
, struct blob_attr
*data
, struct blob_attr
**tb
)
490 struct interface
*iface
= state
->proto
.iface
;
491 struct blob_attr
*cur
;
495 bool addr_ext
= false;
499 if (state
->sm
== S_TEARDOWN
|| state
->sm
== S_SETUP_ABORT
)
500 return UBUS_STATUS_PERMISSION_DENIED
;
502 if (!tb
[NOTIFY_LINK_UP
])
503 return UBUS_STATUS_INVALID_ARGUMENT
;
505 up
= blobmsg_get_bool(tb
[NOTIFY_LINK_UP
]);
507 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
511 if ((cur
= tb
[NOTIFY_KEEP
]) != NULL
)
512 keep
= blobmsg_get_bool(cur
);
514 if ((cur
= tb
[NOTIFY_ADDR_EXT
]) != NULL
) {
515 addr_ext
= blobmsg_get_bool(cur
);
520 if (iface
->state
!= IFS_UP
|| !iface
->l3_dev
.dev
)
524 dev
= iface
->main_dev
.dev
;
525 if (tb
[NOTIFY_IFNAME
]) {
527 devname
= blobmsg_data(tb
[NOTIFY_IFNAME
]);
528 if (tb
[NOTIFY_TUNNEL
])
529 dev
= proto_shell_create_tunnel(devname
, tb
[NOTIFY_TUNNEL
]);
531 dev
= device_get(devname
, dev_create
);
535 return UBUS_STATUS_INVALID_ARGUMENT
;
537 interface_set_l3_dev(iface
, dev
);
538 if (device_claim(&iface
->l3_dev
) < 0)
539 return UBUS_STATUS_UNKNOWN_ERROR
;
541 device_set_present(dev
, true);
544 interface_update_start(iface
, keep
);
546 proto_apply_ip_settings(iface
, data
, addr_ext
);
548 if ((cur
= tb
[NOTIFY_ROUTES
]) != NULL
)
549 proto_shell_parse_route_list(state
->proto
.iface
, cur
, false);
551 if ((cur
= tb
[NOTIFY_ROUTES6
]) != NULL
)
552 proto_shell_parse_route_list(state
->proto
.iface
, cur
, true);
554 if ((cur
= tb
[NOTIFY_DNS
]))
555 interface_add_dns_server_list(&iface
->proto_ip
, cur
);
557 if ((cur
= tb
[NOTIFY_DNS_SEARCH
]))
558 interface_add_dns_search_list(&iface
->proto_ip
, cur
);
560 if ((cur
= tb
[NOTIFY_DATA
]))
561 proto_shell_parse_data(state
->proto
.iface
, cur
);
563 interface_update_complete(state
->proto
.iface
);
565 if ((state
->sm
!= S_SETUP_ABORT
) && (state
->sm
!= S_TEARDOWN
)) {
566 state
->proto
.proto_event(&state
->proto
, IFPEV_UP
);
574 fill_string_list(struct blob_attr
*attr
, char **argv
, int max
)
576 struct blob_attr
*cur
;
583 blobmsg_for_each_attr(cur
, attr
, rem
) {
584 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
587 if (!blobmsg_check_attr(cur
, NULL
))
590 argv
[argc
++] = blobmsg_data(cur
);
601 proto_shell_run_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
603 static char *argv
[64];
604 static char *env
[32];
606 if (state
->sm
== S_TEARDOWN
|| state
->sm
== S_SETUP_ABORT
)
607 return UBUS_STATUS_PERMISSION_DENIED
;
609 if (!tb
[NOTIFY_COMMAND
])
612 if (!fill_string_list(tb
[NOTIFY_COMMAND
], argv
, ARRAY_SIZE(argv
)))
615 if (!fill_string_list(tb
[NOTIFY_ENV
], env
, ARRAY_SIZE(env
)))
618 netifd_start_process((const char **) argv
, (char **) env
, &state
->proto_task
);
623 return UBUS_STATUS_INVALID_ARGUMENT
;
627 proto_shell_kill_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
629 unsigned int signal
= ~0;
631 if (tb
[NOTIFY_SIGNAL
])
632 signal
= blobmsg_get_u32(tb
[NOTIFY_SIGNAL
]);
637 if (state
->proto_task
.uloop
.pending
) {
638 if (signal
== SIGTERM
|| signal
== SIGKILL
)
639 state
->proto_task_killed
= true;
640 kill(state
->proto_task
.uloop
.pid
, signal
);
647 proto_shell_notify_error(struct proto_shell_state
*state
, struct blob_attr
**tb
)
649 struct blob_attr
*cur
;
654 if (!tb
[NOTIFY_ERROR
])
655 return UBUS_STATUS_INVALID_ARGUMENT
;
657 blobmsg_for_each_attr(cur
, tb
[NOTIFY_ERROR
], rem
) {
658 if (n_data
+ 1 == ARRAY_SIZE(data
))
661 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
664 if (!blobmsg_check_attr(cur
, NULL
))
667 data
[n_data
++] = blobmsg_data(cur
);
673 interface_add_error(state
->proto
.iface
, state
->handler
->proto
.name
,
674 data
[0], (const char **) &data
[1], n_data
- 1);
679 return UBUS_STATUS_INVALID_ARGUMENT
;
683 proto_shell_block_restart(struct proto_shell_state
*state
, struct blob_attr
**tb
)
685 state
->proto
.iface
->autostart
= false;
690 proto_shell_set_available(struct proto_shell_state
*state
, struct blob_attr
**tb
)
692 if (!tb
[NOTIFY_AVAILABLE
])
693 return UBUS_STATUS_INVALID_ARGUMENT
;
695 interface_set_available(state
->proto
.iface
, blobmsg_get_bool(tb
[NOTIFY_AVAILABLE
]));
700 proto_shell_add_host_dependency(struct proto_shell_state
*state
, struct blob_attr
**tb
)
702 struct proto_shell_dependency
*dep
;
703 const char *ifname
= tb
[NOTIFY_IFNAME
] ? blobmsg_data(tb
[NOTIFY_IFNAME
]) : "";
704 const char *host
= tb
[NOTIFY_HOST
] ? blobmsg_data(tb
[NOTIFY_HOST
]) : "";
706 if (state
->sm
== S_TEARDOWN
|| state
->sm
== S_SETUP_ABORT
)
707 return UBUS_STATUS_PERMISSION_DENIED
;
709 dep
= calloc(1, sizeof(*dep
) + strlen(ifname
) + 1);
711 return UBUS_STATUS_UNKNOWN_ERROR
;
713 if (!host
[0] && ifname
[0]) {
715 } else if (inet_pton(AF_INET
, host
, &dep
->host
) < 1) {
716 if (inet_pton(AF_INET6
, host
, &dep
->host
) < 1) {
718 return UBUS_STATUS_INVALID_ARGUMENT
;
725 strcpy(dep
->interface
, ifname
);
727 dep
->dep
.cb
= proto_shell_if_up_cb
;
728 interface_add_user(&dep
->dep
, NULL
);
729 list_add(&dep
->list
, &state
->deps
);
730 proto_shell_update_host_dep(dep
);
732 return UBUS_STATUS_NOT_FOUND
;
738 proto_shell_setup_failed(struct proto_shell_state
*state
)
744 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
747 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
752 ret
= UBUS_STATUS_PERMISSION_DENIED
;
759 proto_shell_notify(struct interface_proto_state
*proto
, struct blob_attr
*attr
)
761 struct proto_shell_state
*state
;
762 struct blob_attr
*tb
[__NOTIFY_LAST
];
764 state
= container_of(proto
, struct proto_shell_state
, proto
);
766 blobmsg_parse(notify_attr
, __NOTIFY_LAST
, tb
, blob_data(attr
), blob_len(attr
));
767 if (!tb
[NOTIFY_ACTION
])
768 return UBUS_STATUS_INVALID_ARGUMENT
;
770 switch(blobmsg_get_u32(tb
[NOTIFY_ACTION
])) {
772 return proto_shell_update_link(state
, attr
, tb
);
774 return proto_shell_run_command(state
, tb
);
776 return proto_shell_kill_command(state
, tb
);
778 return proto_shell_notify_error(state
, tb
);
780 return proto_shell_block_restart(state
, tb
);
782 return proto_shell_set_available(state
, tb
);
784 return proto_shell_add_host_dependency(state
, tb
);
786 return proto_shell_setup_failed(state
);
788 return UBUS_STATUS_INVALID_ARGUMENT
;
793 proto_shell_checkup_timeout_cb(struct uloop_timeout
*timeout
)
795 struct proto_shell_state
*state
= container_of(timeout
, struct
796 proto_shell_state
, checkup_timeout
);
797 struct interface_proto_state
*proto
= &state
->proto
;
798 struct interface
*iface
= proto
->iface
;
800 if (!iface
->autostart
)
803 if (iface
->state
== IFS_UP
)
806 D(INTERFACE
, "Interface '%s' is not up after %d sec\n",
807 iface
->name
, state
->checkup_interval
);
808 proto_shell_handler(proto
, PROTO_CMD_TEARDOWN
, false);
812 proto_shell_checkup_attach(struct proto_shell_state
*state
,
813 const struct blob_attr
*attr
)
815 struct blob_attr
*tb
;
816 struct blobmsg_policy checkup_policy
= {
817 .name
= "checkup_interval",
818 .type
= BLOBMSG_TYPE_INT32
821 blobmsg_parse(&checkup_policy
, 1, &tb
, blob_data(attr
), blob_len(attr
));
823 state
->checkup_interval
= -1;
824 state
->checkup_timeout
.cb
= NULL
;
826 state
->checkup_interval
= blobmsg_get_u32(tb
);
827 state
->checkup_timeout
.cb
= proto_shell_checkup_timeout_cb
;
831 static struct interface_proto_state
*
832 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
833 struct blob_attr
*attr
)
835 struct proto_shell_state
*state
;
837 state
= calloc(1, sizeof(*state
));
841 INIT_LIST_HEAD(&state
->deps
);
843 state
->config
= malloc(blob_pad_len(attr
));
847 memcpy(state
->config
, attr
, blob_pad_len(attr
));
848 proto_shell_checkup_attach(state
, state
->config
);
849 state
->proto
.free
= proto_shell_free
;
850 state
->proto
.notify
= proto_shell_notify
;
851 state
->proto
.cb
= proto_shell_handler
;
852 state
->teardown_timeout
.cb
= proto_shell_teardown_timeout_cb
;
853 state
->script_task
.cb
= proto_shell_script_cb
;
854 state
->script_task
.dir_fd
= proto_fd
;
855 state
->script_task
.log_prefix
= iface
->name
;
856 state
->proto_task
.cb
= proto_shell_task_cb
;
857 state
->proto_task
.dir_fd
= proto_fd
;
858 state
->proto_task
.log_prefix
= iface
->name
;
859 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
861 return &state
->proto
;
869 proto_shell_add_handler(const char *script
, const char *name
, json_object
*obj
)
871 struct proto_shell_handler
*handler
;
872 struct proto_handler
*proto
;
873 json_object
*config
, *tmp
;
874 char *proto_name
, *script_name
;
876 handler
= calloc_a(sizeof(*handler
),
877 &proto_name
, strlen(name
) + 1,
878 &script_name
, strlen(script
) + 1);
882 handler
->script_name
= strcpy(script_name
, script
);
884 proto
= &handler
->proto
;
885 proto
->name
= strcpy(proto_name
, name
);
886 proto
->config_params
= &handler
->config
;
887 proto
->attach
= proto_shell_attach
;
889 tmp
= json_get_field(obj
, "no-device", json_type_boolean
);
890 if (tmp
&& json_object_get_boolean(tmp
))
891 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
893 tmp
= json_get_field(obj
, "no-proto-task", json_type_boolean
);
894 if (tmp
&& json_object_get_boolean(tmp
))
895 handler
->proto
.flags
|= PROTO_FLAG_NO_TASK
;
897 tmp
= json_get_field(obj
, "available", json_type_boolean
);
898 if (tmp
&& json_object_get_boolean(tmp
))
899 handler
->proto
.flags
|= PROTO_FLAG_INIT_AVAILABLE
;
901 tmp
= json_get_field(obj
, "renew-handler", json_type_boolean
);
902 if (tmp
&& json_object_get_boolean(tmp
))
903 handler
->proto
.flags
|= PROTO_FLAG_RENEW_AVAILABLE
;
905 tmp
= json_get_field(obj
, "lasterror", json_type_boolean
);
906 if (tmp
&& json_object_get_boolean(tmp
))
907 handler
->proto
.flags
|= PROTO_FLAG_LASTERROR
;
909 tmp
= json_get_field(obj
, "teardown-on-l3-link-down", json_type_boolean
);
910 if (tmp
&& json_object_get_boolean(tmp
))
911 handler
->proto
.flags
|= PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN
;
913 config
= json_get_field(obj
, "config", json_type_array
);
915 handler
->config_buf
= netifd_handler_parse_config(&handler
->config
, config
);
917 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
918 add_proto_handler(proto
);
921 void proto_shell_init(void)
923 proto_fd
= netifd_open_subdir("proto");
927 netifd_init_script_handlers(proto_fd
, proto_shell_add_handler
);