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
;
31 struct proto_shell_state
{
32 struct interface_proto_state proto
;
33 struct proto_shell_handler
*handler
;
34 struct blob_attr
*config
;
36 struct device_user l3_dev
;
38 struct uloop_timeout setup_timeout
;
39 struct netifd_process setup_task
;
40 struct netifd_process teardown_task
;
41 bool teardown_pending
;
42 bool teardown_wait_task
;
44 struct netifd_process proto_task
;
48 proto_shell_handler(struct interface_proto_state
*proto
,
49 enum interface_proto_cmd cmd
, bool force
)
51 struct proto_shell_state
*state
;
52 struct proto_shell_handler
*handler
;
53 struct netifd_process
*proc
;
59 state
= container_of(proto
, struct proto_shell_state
, proto
);
60 handler
= state
->handler
;
62 if (cmd
== PROTO_CMD_SETUP
) {
64 proc
= &state
->setup_task
;
67 proc
= &state
->teardown_task
;
68 if (state
->setup_task
.uloop
.pending
&& !state
->teardown_wait_task
) {
69 uloop_timeout_set(&state
->setup_timeout
, 1000);
70 kill(state
->setup_task
.uloop
.pid
, SIGTERM
);
71 state
->teardown_pending
= true;
76 config
= blobmsg_format_json(state
->config
, true);
80 argv
[i
++] = handler
->script_name
;
81 argv
[i
++] = handler
->proto
.name
;
83 argv
[i
++] = proto
->iface
->name
;
85 if (proto
->iface
->main_dev
.dev
)
86 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
89 ret
= netifd_start_process(argv
, NULL
, proc
);
96 proto_shell_setup_timeout_cb(struct uloop_timeout
*timeout
)
98 struct proto_shell_state
*state
;
100 state
= container_of(timeout
, struct proto_shell_state
, setup_timeout
);
101 kill(state
->setup_task
.uloop
.pid
, SIGKILL
);
105 proto_shell_setup_cb(struct netifd_process
*p
, int ret
)
107 struct proto_shell_state
*state
;
109 state
= container_of(p
, struct proto_shell_state
, setup_task
);
110 uloop_timeout_cancel(&state
->setup_timeout
);
111 if (state
->teardown_pending
) {
112 state
->teardown_pending
= false;
113 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
118 proto_shell_teardown_cb(struct netifd_process
*p
, int ret
)
120 struct proto_shell_state
*state
;
122 state
= container_of(p
, struct proto_shell_state
, teardown_task
);
124 if (state
->teardown_wait_task
)
127 netifd_kill_process(&state
->proto_task
);
128 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
132 proto_shell_task_cb(struct netifd_process
*p
, int ret
)
134 struct proto_shell_state
*state
;
135 bool teardown_wait_task
;
137 state
= container_of(p
, struct proto_shell_state
, proto_task
);
139 teardown_wait_task
= state
->teardown_wait_task
;
140 state
->teardown_wait_task
= false;
141 if (state
->teardown_pending
|| state
->teardown_task
.uloop
.pending
)
144 if (teardown_wait_task
) {
145 proto_shell_teardown_cb(&state
->teardown_task
, 0);
149 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
150 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
154 proto_shell_free(struct interface_proto_state
*proto
)
156 struct proto_shell_state
*state
;
158 state
= container_of(proto
, struct proto_shell_state
, proto
);
164 proto_shell_parse_addr_list(struct interface
*iface
, struct blob_attr
*attr
,
165 bool v6
, bool external
)
167 struct device_addr
*addr
;
168 struct blob_attr
*cur
;
171 blobmsg_for_each_attr(cur
, attr
, rem
) {
172 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
) {
173 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur
));
177 addr
= proto_parse_ip_addr_string(blobmsg_data(cur
), v6
, v6
? 32 : 128);
179 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur
));
184 addr
->flags
|= DEVADDR_EXTERNAL
;
186 vlist_add(&iface
->proto_addr
, &addr
->node
);
198 static const struct blobmsg_policy route_attr
[__ROUTE_LAST
] = {
199 [ROUTE_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
200 [ROUTE_MASK
] = { .name
= "mask", .type
= BLOBMSG_TYPE_STRING
},
201 [ROUTE_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
202 [ROUTE_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
206 parse_route(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
208 struct blob_attr
*tb
[__ROUTE_LAST
], *cur
;
209 struct device_route
*route
;
210 int af
= v6
? AF_INET6
: AF_INET
;
212 blobmsg_parse(route_attr
, __ROUTE_LAST
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
214 if (!tb
[ROUTE_GATEWAY
] && !tb
[ROUTE_DEVICE
])
217 route
= calloc(1, sizeof(*route
));
221 route
->mask
= v6
? 128 : 32;
222 if ((cur
= tb
[ROUTE_MASK
]) != NULL
) {
223 route
->mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
224 if (route
->mask
> (v6
? 128 : 32))
228 if ((cur
= tb
[ROUTE_TARGET
]) != NULL
) {
229 if (!inet_pton(af
, blobmsg_data(cur
), &route
->addr
)) {
230 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur
));
235 if ((cur
= tb
[ROUTE_GATEWAY
]) != NULL
) {
236 if (!inet_pton(af
, blobmsg_data(cur
), &route
->nexthop
)) {
237 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur
));
242 if ((cur
= tb
[ROUTE_DEVICE
]) != NULL
)
243 route
->device
= device_get(blobmsg_data(cur
), true);
245 vlist_add(&iface
->proto_route
, &route
->node
);
253 proto_shell_parse_route_list(struct interface
*iface
, struct blob_attr
*attr
,
256 struct blob_attr
*cur
;
259 blobmsg_for_each_attr(cur
, attr
, rem
) {
260 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
) {
261 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur
));
265 parse_route(iface
, cur
, v6
);
286 static const struct blobmsg_policy notify_attr
[__NOTIFY_LAST
] = {
287 [NOTIFY_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_INT32
},
288 [NOTIFY_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_ARRAY
},
289 [NOTIFY_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_ARRAY
},
290 [NOTIFY_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
291 [NOTIFY_LINK_UP
] = { .name
= "link-up", .type
= BLOBMSG_TYPE_BOOL
},
292 [NOTIFY_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
293 [NOTIFY_ADDR_EXT
] = { .name
= "address-external", .type
= BLOBMSG_TYPE_BOOL
},
294 [NOTIFY_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_ARRAY
},
295 [NOTIFY_IP6ADDR
] = { .name
= "ip6addr", .type
= BLOBMSG_TYPE_ARRAY
},
296 [NOTIFY_ROUTES
] = { .name
= "routes", .type
= BLOBMSG_TYPE_ARRAY
},
297 [NOTIFY_ROUTES6
] = { .name
= "routes6", .type
= BLOBMSG_TYPE_ARRAY
},
298 [NOTIFY_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
299 [NOTIFY_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
303 proto_shell_update_link(struct proto_shell_state
*state
, struct blob_attr
**tb
)
305 struct blob_attr
*cur
;
306 bool addr_ext
= false;
309 if (!tb
[NOTIFY_LINK_UP
])
310 return UBUS_STATUS_INVALID_ARGUMENT
;
312 up
= blobmsg_get_bool(tb
[NOTIFY_LINK_UP
]);
314 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
318 if (!tb
[NOTIFY_IFNAME
]) {
319 if (!state
->proto
.iface
->main_dev
.dev
)
320 return UBUS_STATUS_INVALID_ARGUMENT
;
321 } else if (!state
->l3_dev
.dev
) {
322 device_add_user(&state
->l3_dev
,
323 device_get(blobmsg_data(tb
[NOTIFY_IFNAME
]), true));
324 device_claim(&state
->l3_dev
);
325 state
->proto
.iface
->l3_dev
= &state
->l3_dev
;
328 interface_ip_update_start(state
->proto
.iface
);
330 if ((cur
= tb
[NOTIFY_ADDR_EXT
]) != NULL
)
331 addr_ext
= blobmsg_get_bool(cur
);
333 if ((cur
= tb
[NOTIFY_IPADDR
]) != NULL
)
334 proto_shell_parse_addr_list(state
->proto
.iface
, cur
, false, addr_ext
);
336 if ((cur
= tb
[NOTIFY_IP6ADDR
]) != NULL
)
337 proto_shell_parse_addr_list(state
->proto
.iface
, cur
, true, addr_ext
);
339 if ((cur
= tb
[NOTIFY_ROUTES
]) != NULL
)
340 proto_shell_parse_route_list(state
->proto
.iface
, cur
, false);
342 if ((cur
= tb
[NOTIFY_ROUTES6
]) != NULL
)
343 proto_shell_parse_route_list(state
->proto
.iface
, cur
, true);
345 if ((cur
= tb
[NOTIFY_DNS
]) != NULL
)
346 interface_add_dns_server_list(state
->proto
.iface
, cur
);
348 if ((cur
= tb
[NOTIFY_DNS_SEARCH
]) != NULL
)
349 interface_add_dns_search_list(state
->proto
.iface
, cur
);
351 interface_ip_update_complete(state
->proto
.iface
);
353 state
->proto
.proto_event(&state
->proto
, IFPEV_UP
);
359 fill_string_list(struct blob_attr
*attr
, char **argv
, int max
)
361 struct blob_attr
*cur
;
368 blobmsg_for_each_attr(cur
, attr
, rem
) {
369 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
372 if (!blobmsg_check_attr(cur
, NULL
))
375 argv
[argc
++] = blobmsg_data(cur
);
386 proto_shell_run_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
391 if (!tb
[NOTIFY_COMMAND
])
394 if (!fill_string_list(tb
[NOTIFY_COMMAND
], argv
, ARRAY_SIZE(argv
)))
397 if (!fill_string_list(tb
[NOTIFY_ENV
], env
, ARRAY_SIZE(env
)))
400 netifd_start_process((const char **) argv
, (char **) env
, &state
->proto_task
);
405 return UBUS_STATUS_INVALID_ARGUMENT
;
409 proto_shell_kill_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
411 unsigned int signal
= ~0;
413 if (tb
[NOTIFY_SIGNAL
])
414 signal
= blobmsg_get_u32(tb
[NOTIFY_SIGNAL
]);
419 if (state
->proto_task
.uloop
.pending
) {
420 kill(state
->proto_task
.uloop
.pid
, signal
);
421 state
->teardown_wait_task
= true;
428 proto_shell_notify(struct interface_proto_state
*proto
, struct blob_attr
*attr
)
430 struct proto_shell_state
*state
;
431 struct blob_attr
*tb
[__NOTIFY_LAST
];
433 state
= container_of(proto
, struct proto_shell_state
, proto
);
435 blobmsg_parse(notify_attr
, __NOTIFY_LAST
, tb
, blob_data(attr
), blob_len(attr
));
436 if (!tb
[NOTIFY_ACTION
])
437 return UBUS_STATUS_INVALID_ARGUMENT
;
439 switch(blobmsg_get_u32(tb
[NOTIFY_ACTION
])) {
441 return proto_shell_update_link(state
, tb
);
443 return proto_shell_run_command(state
, tb
);
445 return proto_shell_kill_command(state
, tb
);
447 return UBUS_STATUS_INVALID_ARGUMENT
;
451 struct interface_proto_state
*
452 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
453 struct blob_attr
*attr
)
455 struct proto_shell_state
*state
;
457 state
= calloc(1, sizeof(*state
));
458 state
->config
= malloc(blob_pad_len(attr
));
462 memcpy(state
->config
, attr
, blob_pad_len(attr
));
463 state
->proto
.free
= proto_shell_free
;
464 state
->proto
.notify
= proto_shell_notify
;
465 state
->proto
.cb
= proto_shell_handler
;
466 state
->setup_timeout
.cb
= proto_shell_setup_timeout_cb
;
467 state
->setup_task
.cb
= proto_shell_setup_cb
;
468 state
->setup_task
.dir_fd
= proto_fd
.fd
;
469 state
->setup_task
.log_prefix
= iface
->name
;
470 state
->teardown_task
.cb
= proto_shell_teardown_cb
;
471 state
->teardown_task
.dir_fd
= proto_fd
.fd
;
472 state
->teardown_task
.log_prefix
= iface
->name
;
473 state
->proto_task
.cb
= proto_shell_task_cb
;
474 state
->proto_task
.dir_fd
= proto_fd
.fd
;
475 state
->proto_task
.log_prefix
= iface
->name
;
476 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
478 return &state
->proto
;
486 check_type(json_object
*obj
, json_type type
)
491 if (json_object_get_type(obj
) != type
)
497 static inline json_object
*
498 get_field(json_object
*obj
, const char *name
, json_type type
)
500 return check_type(json_object_object_get(obj
, name
), type
);
504 proto_shell_parse_config(struct config_param_list
*config
, json_object
*obj
)
506 struct blobmsg_policy
*attrs
;
507 char *str_buf
, *str_cur
;
511 config
->n_params
= json_object_array_length(obj
);
512 attrs
= calloc(1, sizeof(*attrs
) * config
->n_params
);
516 config
->params
= attrs
;
517 for (i
= 0; i
< config
->n_params
; i
++) {
518 json_object
*cur
, *name
, *type
;
520 cur
= check_type(json_object_array_get_idx(obj
, i
), json_type_array
);
524 name
= check_type(json_object_array_get_idx(cur
, 0), json_type_string
);
528 type
= check_type(json_object_array_get_idx(cur
, 1), json_type_int
);
532 attrs
[i
].name
= json_object_get_string(name
);
533 attrs
[i
].type
= json_object_get_int(type
);
534 if (attrs
[i
].type
> BLOBMSG_TYPE_LAST
)
537 str_len
+= strlen(attrs
[i
].name
) + 1;
540 str_buf
= malloc(str_len
);
545 for (i
= 0; i
< config
->n_params
; i
++) {
546 const char *name
= attrs
[i
].name
;
548 attrs
[i
].name
= str_cur
;
549 str_cur
+= sprintf(str_cur
, "%s", name
) + 1;
556 config
->n_params
= 0;
561 proto_shell_add_handler(const char *script
, json_object
*obj
)
563 struct proto_shell_handler
*handler
;
564 struct proto_handler
*proto
;
565 json_object
*config
, *tmp
;
569 if (!check_type(obj
, json_type_object
))
572 tmp
= get_field(obj
, "name", json_type_string
);
576 name
= json_object_get_string(tmp
);
578 handler
= calloc(1, sizeof(*handler
) +
584 strcpy(handler
->script_name
, script
);
586 str
= handler
->script_name
+ strlen(handler
->script_name
) + 1;
589 proto
= &handler
->proto
;
591 proto
->config_params
= &handler
->config
;
592 proto
->attach
= proto_shell_attach
;
594 tmp
= get_field(obj
, "no-device", json_type_boolean
);
595 if (tmp
&& json_object_get_boolean(tmp
))
596 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
598 config
= get_field(obj
, "config", json_type_array
);
600 handler
->config_buf
= proto_shell_parse_config(&handler
->config
, config
);
602 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
603 add_proto_handler(proto
);
606 static void proto_shell_add_script(const char *name
)
608 struct json_tokener
*tok
= NULL
;
610 static char buf
[512];
611 char *start
, *end
, *cmd
;
615 #define DUMP_SUFFIX " '' dump"
617 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_SUFFIX
));
618 sprintf(cmd
, "%s" DUMP_SUFFIX
, name
);
625 buflen
= fread(buf
, 1, sizeof(buf
) - 1, f
);
632 end
= memchr(start
, '\n', len
);
637 tok
= json_tokener_new();
639 obj
= json_tokener_parse_ex(tok
, start
, len
);
640 if (!is_error(obj
)) {
641 proto_shell_add_handler(name
, obj
);
642 json_object_put(obj
);
643 json_tokener_free(tok
);
649 len
= buflen
- (start
- buf
);
652 } while (!feof(f
) && !ferror(f
));
655 json_tokener_free(tok
);
660 void __init
proto_shell_init(void)
666 main_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
670 if (chdir(main_path
)) {
671 perror("chdir(main path)");
675 if (chdir("./proto"))
678 proto_fd
.fd
= open(".", O_RDONLY
| O_DIRECTORY
);
682 netifd_fd_add(&proto_fd
);
683 glob("./*.sh", 0, NULL
, &g
);
684 for (i
= 0; i
< g
.gl_pathc
; i
++)
685 proto_shell_add_script(g
.gl_pathv
[i
]);