11 #include <arpa/inet.h>
12 #include <netinet/in.h>
14 #include <libubox/blobmsg_json.h>
17 #include "interface.h"
18 #include "interface-ip.h"
21 static struct netifd_fd proto_fd
;
23 struct proto_shell_handler
{
24 struct list_head list
;
25 struct proto_handler proto
;
26 struct config_param_list config
;
32 struct proto_shell_state
{
33 struct interface_proto_state proto
;
34 struct proto_shell_handler
*handler
;
35 struct blob_attr
*config
;
37 struct device_user l3_dev
;
39 struct uloop_timeout setup_timeout
;
40 struct netifd_process setup_task
;
41 struct netifd_process teardown_task
;
42 bool teardown_pending
;
43 bool teardown_wait_task
;
45 struct netifd_process proto_task
;
50 proto_shell_handler(struct interface_proto_state
*proto
,
51 enum interface_proto_cmd cmd
, bool force
)
53 struct proto_shell_state
*state
;
54 struct proto_shell_handler
*handler
;
55 struct netifd_process
*proc
;
56 static char error_buf
[32];
61 int ret
, i
= 0, j
= 0;
63 state
= container_of(proto
, struct proto_shell_state
, proto
);
64 handler
= state
->handler
;
66 if (cmd
== PROTO_CMD_SETUP
) {
68 proc
= &state
->setup_task
;
69 state
->last_error
= -1;
72 proc
= &state
->teardown_task
;
73 if (state
->setup_task
.uloop
.pending
&& !state
->teardown_wait_task
) {
74 uloop_timeout_set(&state
->setup_timeout
, 1000);
75 kill(state
->setup_task
.uloop
.pid
, SIGTERM
);
76 state
->teardown_pending
= true;
79 if (state
->last_error
>= 0) {
80 snprintf(error_buf
, sizeof(error_buf
), "ERROR=%d", state
->last_error
);
81 envp
[j
++] = error_buf
;
85 config
= blobmsg_format_json(state
->config
, true);
89 argv
[i
++] = handler
->script_name
;
90 argv
[i
++] = handler
->proto
.name
;
92 argv
[i
++] = proto
->iface
->name
;
94 if (proto
->iface
->main_dev
.dev
)
95 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
99 ret
= netifd_start_process(argv
, envp
, proc
);
106 proto_shell_setup_timeout_cb(struct uloop_timeout
*timeout
)
108 struct proto_shell_state
*state
;
110 state
= container_of(timeout
, struct proto_shell_state
, setup_timeout
);
111 kill(state
->setup_task
.uloop
.pid
, SIGKILL
);
115 proto_shell_setup_cb(struct netifd_process
*p
, int ret
)
117 struct proto_shell_state
*state
;
119 state
= container_of(p
, struct proto_shell_state
, setup_task
);
120 uloop_timeout_cancel(&state
->setup_timeout
);
121 if (state
->teardown_pending
) {
122 state
->teardown_pending
= false;
123 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
128 proto_shell_teardown_cb(struct netifd_process
*p
, int ret
)
130 struct proto_shell_state
*state
;
132 state
= container_of(p
, struct proto_shell_state
, teardown_task
);
134 if (state
->teardown_wait_task
)
137 netifd_kill_process(&state
->proto_task
);
138 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
142 proto_shell_task_cb(struct netifd_process
*p
, int ret
)
144 struct proto_shell_state
*state
;
145 bool teardown_wait_task
;
147 state
= container_of(p
, struct proto_shell_state
, proto_task
);
149 teardown_wait_task
= state
->teardown_wait_task
;
150 state
->teardown_wait_task
= false;
151 if (state
->teardown_pending
|| state
->teardown_task
.uloop
.pending
)
154 if (teardown_wait_task
) {
155 proto_shell_teardown_cb(&state
->teardown_task
, 0);
159 state
->last_error
= WEXITSTATUS(ret
);
160 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
161 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
165 proto_shell_free(struct interface_proto_state
*proto
)
167 struct proto_shell_state
*state
;
169 state
= container_of(proto
, struct proto_shell_state
, proto
);
175 proto_shell_parse_addr_list(struct interface_ip_settings
*ip
, struct blob_attr
*attr
,
176 bool v6
, bool external
)
178 struct device_addr
*addr
;
179 struct blob_attr
*cur
;
182 blobmsg_for_each_attr(cur
, attr
, rem
) {
183 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
) {
184 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur
));
188 addr
= proto_parse_ip_addr_string(blobmsg_data(cur
), v6
, v6
? 32 : 128);
190 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur
));
195 addr
->flags
|= DEVADDR_EXTERNAL
;
197 vlist_add(&ip
->addr
, &addr
->node
);
202 proto_shell_parse_route_list(struct interface
*iface
, struct blob_attr
*attr
,
205 struct blob_attr
*cur
;
208 blobmsg_for_each_attr(cur
, attr
, rem
) {
209 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
) {
210 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur
));
214 interface_ip_add_route(iface
, cur
, v6
);
237 static const struct blobmsg_policy notify_attr
[__NOTIFY_LAST
] = {
238 [NOTIFY_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_INT32
},
239 [NOTIFY_ERROR
] = { .name
= "error", .type
= BLOBMSG_TYPE_ARRAY
},
240 [NOTIFY_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_ARRAY
},
241 [NOTIFY_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_ARRAY
},
242 [NOTIFY_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
243 [NOTIFY_AVAILABLE
] = { .name
= "available", .type
= BLOBMSG_TYPE_BOOL
},
244 [NOTIFY_LINK_UP
] = { .name
= "link-up", .type
= BLOBMSG_TYPE_BOOL
},
245 [NOTIFY_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
246 [NOTIFY_ADDR_EXT
] = { .name
= "address-external", .type
= BLOBMSG_TYPE_BOOL
},
247 [NOTIFY_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_ARRAY
},
248 [NOTIFY_IP6ADDR
] = { .name
= "ip6addr", .type
= BLOBMSG_TYPE_ARRAY
},
249 [NOTIFY_ROUTES
] = { .name
= "routes", .type
= BLOBMSG_TYPE_ARRAY
},
250 [NOTIFY_ROUTES6
] = { .name
= "routes6", .type
= BLOBMSG_TYPE_ARRAY
},
251 [NOTIFY_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
252 [NOTIFY_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
256 proto_shell_update_link(struct proto_shell_state
*state
, struct blob_attr
**tb
)
258 struct interface_ip_settings
*ip
;
259 struct blob_attr
*cur
;
261 bool addr_ext
= false;
264 if (!tb
[NOTIFY_LINK_UP
])
265 return UBUS_STATUS_INVALID_ARGUMENT
;
267 up
= blobmsg_get_bool(tb
[NOTIFY_LINK_UP
]);
269 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
273 if ((cur
= tb
[NOTIFY_ADDR_EXT
]) != NULL
) {
274 addr_ext
= blobmsg_get_bool(cur
);
279 if (!tb
[NOTIFY_IFNAME
]) {
280 if (!state
->proto
.iface
->main_dev
.dev
)
281 return UBUS_STATUS_INVALID_ARGUMENT
;
283 if (state
->l3_dev
.dev
)
284 device_remove_user(&state
->l3_dev
);
286 device_add_user(&state
->l3_dev
,
287 device_get(blobmsg_data(tb
[NOTIFY_IFNAME
]), dev_create
));
288 state
->proto
.iface
->l3_dev
= &state
->l3_dev
;
289 device_claim(&state
->l3_dev
);
292 ip
= &state
->proto
.iface
->proto_ip
;
293 interface_update_start(state
->proto
.iface
);
295 if ((cur
= tb
[NOTIFY_IPADDR
]) != NULL
)
296 proto_shell_parse_addr_list(ip
, cur
, false, addr_ext
);
298 if ((cur
= tb
[NOTIFY_IP6ADDR
]) != NULL
)
299 proto_shell_parse_addr_list(ip
, cur
, true, addr_ext
);
301 if ((cur
= tb
[NOTIFY_ROUTES
]) != NULL
)
302 proto_shell_parse_route_list(state
->proto
.iface
, cur
, false);
304 if ((cur
= tb
[NOTIFY_ROUTES6
]) != NULL
)
305 proto_shell_parse_route_list(state
->proto
.iface
, cur
, true);
307 if ((cur
= tb
[NOTIFY_DNS
]) != NULL
)
308 interface_add_dns_server_list(ip
, cur
);
310 if ((cur
= tb
[NOTIFY_DNS_SEARCH
]) != NULL
)
311 interface_add_dns_search_list(ip
, cur
);
313 interface_update_complete(state
->proto
.iface
);
315 state
->proto
.proto_event(&state
->proto
, IFPEV_UP
);
321 fill_string_list(struct blob_attr
*attr
, char **argv
, int max
)
323 struct blob_attr
*cur
;
330 blobmsg_for_each_attr(cur
, attr
, rem
) {
331 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
334 if (!blobmsg_check_attr(cur
, NULL
))
337 argv
[argc
++] = blobmsg_data(cur
);
348 proto_shell_run_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
350 static char *argv
[64];
351 static char *env
[32];
353 if (!tb
[NOTIFY_COMMAND
])
356 if (!fill_string_list(tb
[NOTIFY_COMMAND
], argv
, ARRAY_SIZE(argv
)))
359 if (!fill_string_list(tb
[NOTIFY_ENV
], env
, ARRAY_SIZE(env
)))
362 netifd_start_process((const char **) argv
, (char **) env
, &state
->proto_task
);
367 return UBUS_STATUS_INVALID_ARGUMENT
;
371 proto_shell_kill_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
373 unsigned int signal
= ~0;
375 if (tb
[NOTIFY_SIGNAL
])
376 signal
= blobmsg_get_u32(tb
[NOTIFY_SIGNAL
]);
381 if (state
->proto_task
.uloop
.pending
) {
382 kill(state
->proto_task
.uloop
.pid
, signal
);
383 state
->teardown_wait_task
= true;
390 proto_shell_notify_error(struct proto_shell_state
*state
, struct blob_attr
**tb
)
392 struct blob_attr
*cur
;
397 if (!tb
[NOTIFY_ERROR
])
398 return UBUS_STATUS_INVALID_ARGUMENT
;
400 blobmsg_for_each_attr(cur
, tb
[NOTIFY_ERROR
], rem
) {
401 if (n_data
+ 1 == ARRAY_SIZE(data
))
404 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
407 if (!blobmsg_check_attr(cur
, NULL
))
410 data
[n_data
++] = blobmsg_data(cur
);
416 interface_add_error(state
->proto
.iface
, state
->handler
->proto
.name
,
417 data
[0], (const char **) &data
[1], n_data
- 1);
422 return UBUS_STATUS_INVALID_ARGUMENT
;
426 proto_shell_block_restart(struct proto_shell_state
*state
, struct blob_attr
**tb
)
428 state
->proto
.iface
->autostart
= false;
433 proto_shell_set_available(struct proto_shell_state
*state
, struct blob_attr
**tb
)
435 if (!tb
[NOTIFY_AVAILABLE
])
436 return UBUS_STATUS_INVALID_ARGUMENT
;
438 interface_set_available(state
->proto
.iface
, blobmsg_get_bool(tb
[NOTIFY_AVAILABLE
]));
443 proto_shell_notify(struct interface_proto_state
*proto
, struct blob_attr
*attr
)
445 struct proto_shell_state
*state
;
446 struct blob_attr
*tb
[__NOTIFY_LAST
];
448 state
= container_of(proto
, struct proto_shell_state
, proto
);
450 blobmsg_parse(notify_attr
, __NOTIFY_LAST
, tb
, blob_data(attr
), blob_len(attr
));
451 if (!tb
[NOTIFY_ACTION
])
452 return UBUS_STATUS_INVALID_ARGUMENT
;
454 switch(blobmsg_get_u32(tb
[NOTIFY_ACTION
])) {
456 return proto_shell_update_link(state
, tb
);
458 return proto_shell_run_command(state
, tb
);
460 return proto_shell_kill_command(state
, tb
);
462 return proto_shell_notify_error(state
, tb
);
464 return proto_shell_block_restart(state
, tb
);
466 return proto_shell_set_available(state
, tb
);
468 return UBUS_STATUS_INVALID_ARGUMENT
;
472 static struct interface_proto_state
*
473 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
474 struct blob_attr
*attr
)
476 struct proto_shell_state
*state
;
478 state
= calloc(1, sizeof(*state
));
479 state
->config
= malloc(blob_pad_len(attr
));
483 memcpy(state
->config
, attr
, blob_pad_len(attr
));
484 state
->proto
.free
= proto_shell_free
;
485 state
->proto
.notify
= proto_shell_notify
;
486 state
->proto
.cb
= proto_shell_handler
;
487 state
->setup_timeout
.cb
= proto_shell_setup_timeout_cb
;
488 state
->setup_task
.cb
= proto_shell_setup_cb
;
489 state
->setup_task
.dir_fd
= proto_fd
.fd
;
490 state
->setup_task
.log_prefix
= iface
->name
;
491 state
->teardown_task
.cb
= proto_shell_teardown_cb
;
492 state
->teardown_task
.dir_fd
= proto_fd
.fd
;
493 state
->teardown_task
.log_prefix
= iface
->name
;
494 state
->proto_task
.cb
= proto_shell_task_cb
;
495 state
->proto_task
.dir_fd
= proto_fd
.fd
;
496 state
->proto_task
.log_prefix
= iface
->name
;
497 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
499 return &state
->proto
;
507 check_type(json_object
*obj
, json_type type
)
512 if (json_object_get_type(obj
) != type
)
518 static inline json_object
*
519 get_field(json_object
*obj
, const char *name
, json_type type
)
521 return check_type(json_object_object_get(obj
, name
), type
);
525 proto_shell_parse_config(struct config_param_list
*config
, json_object
*obj
)
527 struct blobmsg_policy
*attrs
;
528 char *str_buf
, *str_cur
;
532 config
->n_params
= json_object_array_length(obj
);
533 attrs
= calloc(1, sizeof(*attrs
) * config
->n_params
);
537 config
->params
= attrs
;
538 for (i
= 0; i
< config
->n_params
; i
++) {
539 json_object
*cur
, *name
, *type
;
541 cur
= check_type(json_object_array_get_idx(obj
, i
), json_type_array
);
545 name
= check_type(json_object_array_get_idx(cur
, 0), json_type_string
);
549 type
= check_type(json_object_array_get_idx(cur
, 1), json_type_int
);
553 attrs
[i
].name
= json_object_get_string(name
);
554 attrs
[i
].type
= json_object_get_int(type
);
555 if (attrs
[i
].type
> BLOBMSG_TYPE_LAST
)
558 str_len
+= strlen(attrs
[i
].name
) + 1;
561 str_buf
= malloc(str_len
);
566 for (i
= 0; i
< config
->n_params
; i
++) {
567 const char *name
= attrs
[i
].name
;
569 attrs
[i
].name
= str_cur
;
570 str_cur
+= sprintf(str_cur
, "%s", name
) + 1;
577 config
->n_params
= 0;
582 proto_shell_add_handler(const char *script
, json_object
*obj
)
584 struct proto_shell_handler
*handler
;
585 struct proto_handler
*proto
;
586 json_object
*config
, *tmp
;
590 if (!check_type(obj
, json_type_object
))
593 tmp
= get_field(obj
, "name", json_type_string
);
597 name
= json_object_get_string(tmp
);
599 handler
= calloc(1, sizeof(*handler
) +
605 strcpy(handler
->script_name
, script
);
607 str
= handler
->script_name
+ strlen(handler
->script_name
) + 1;
610 proto
= &handler
->proto
;
612 proto
->config_params
= &handler
->config
;
613 proto
->attach
= proto_shell_attach
;
615 tmp
= get_field(obj
, "no-device", json_type_boolean
);
616 if (tmp
&& json_object_get_boolean(tmp
))
617 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
619 tmp
= get_field(obj
, "available", json_type_boolean
);
620 if (tmp
&& json_object_get_boolean(tmp
))
621 handler
->proto
.flags
|= PROTO_FLAG_INIT_AVAILABLE
;
623 config
= get_field(obj
, "config", json_type_array
);
625 handler
->config_buf
= proto_shell_parse_config(&handler
->config
, config
);
627 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
628 add_proto_handler(proto
);
631 static void proto_shell_add_script(const char *name
)
633 struct json_tokener
*tok
= NULL
;
635 static char buf
[512];
640 #define DUMP_SUFFIX " '' dump"
642 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_SUFFIX
));
643 sprintf(cmd
, "%s" DUMP_SUFFIX
, name
);
650 start
= fgets(buf
, sizeof(buf
), f
);
657 tok
= json_tokener_new();
659 obj
= json_tokener_parse_ex(tok
, start
, len
);
660 if (!is_error(obj
)) {
661 proto_shell_add_handler(name
, obj
);
662 json_object_put(obj
);
663 json_tokener_free(tok
);
665 } else if (start
[len
- 1] == '\n') {
666 json_tokener_free(tok
);
669 } while (!feof(f
) && !ferror(f
));
672 json_tokener_free(tok
);
677 static void __init
proto_shell_init(void)
683 main_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
687 if (chdir(main_path
)) {
688 perror("chdir(main path)");
692 if (chdir("./proto"))
695 proto_fd
.fd
= open(".", O_RDONLY
| O_DIRECTORY
);
699 netifd_fd_add(&proto_fd
);
700 glob("./*.sh", 0, NULL
, &g
);
701 for (i
= 0; i
< g
.gl_pathc
; i
++)
702 proto_shell_add_script(g
.gl_pathv
[i
]);