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"
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 uloop_process setup_task
;
40 struct uloop_process teardown_task
;
41 bool teardown_pending
;
42 bool teardown_wait_task
;
44 struct uloop_process proto_task
;
48 kill_process(struct uloop_process
*proc
)
53 kill(proc
->pid
, SIGTERM
);
54 uloop_process_delete(proc
);
58 start_process(const char **argv
, char **env
, struct uloop_process
*proc
)
64 if ((pid
= fork()) < 0)
75 execvp(argv
[0], (char **) argv
);
83 uloop_process_add(proc
);
89 proto_shell_handler(struct interface_proto_state
*proto
,
90 enum interface_proto_cmd cmd
, bool force
)
92 struct proto_shell_state
*state
;
93 struct proto_shell_handler
*handler
;
94 struct uloop_process
*proc
;
100 state
= container_of(proto
, struct proto_shell_state
, proto
);
101 handler
= state
->handler
;
103 if (cmd
== PROTO_CMD_SETUP
) {
105 proc
= &state
->setup_task
;
108 proc
= &state
->teardown_task
;
109 if (state
->setup_task
.pending
&& !state
->teardown_wait_task
) {
110 uloop_timeout_set(&state
->setup_timeout
, 1000);
111 kill(state
->setup_task
.pid
, SIGTERM
);
112 state
->teardown_pending
= true;
117 config
= blobmsg_format_json(state
->config
, true);
121 argv
[i
++] = handler
->script_name
;
122 argv
[i
++] = handler
->proto
.name
;
124 argv
[i
++] = proto
->iface
->name
;
126 if (proto
->iface
->main_dev
.dev
)
127 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
130 ret
= start_process(argv
, NULL
, proc
);
137 proto_shell_setup_timeout_cb(struct uloop_timeout
*timeout
)
139 struct proto_shell_state
*state
;
141 state
= container_of(timeout
, struct proto_shell_state
, setup_timeout
);
142 kill(state
->setup_task
.pid
, SIGKILL
);
146 proto_shell_setup_cb(struct uloop_process
*p
, int ret
)
148 struct proto_shell_state
*state
;
150 state
= container_of(p
, struct proto_shell_state
, setup_task
);
151 uloop_timeout_cancel(&state
->setup_timeout
);
152 if (state
->teardown_pending
) {
153 state
->teardown_pending
= false;
154 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
159 proto_shell_teardown_cb(struct uloop_process
*p
, int ret
)
161 struct proto_shell_state
*state
;
163 state
= container_of(p
, struct proto_shell_state
, teardown_task
);
165 if (state
->teardown_wait_task
)
168 kill_process(&state
->proto_task
);
170 if (state
->l3_dev
.dev
)
171 device_remove_user(&state
->l3_dev
);
173 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
177 proto_shell_task_cb(struct uloop_process
*p
, int ret
)
179 struct proto_shell_state
*state
;
180 bool teardown_wait_task
;
182 state
= container_of(p
, struct proto_shell_state
, proto_task
);
184 teardown_wait_task
= state
->teardown_wait_task
;
185 state
->teardown_wait_task
= false;
186 if (state
->teardown_pending
|| state
->teardown_task
.pending
)
189 if (teardown_wait_task
) {
190 proto_shell_teardown_cb(&state
->teardown_task
, 0);
194 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
195 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
199 proto_shell_free(struct interface_proto_state
*proto
)
201 struct proto_shell_state
*state
;
203 state
= container_of(proto
, struct proto_shell_state
, proto
);
209 proto_shell_parse_addr_list(struct interface
*iface
, struct blob_attr
*attr
,
210 bool v6
, bool external
)
212 struct device_addr
*addr
;
213 struct blob_attr
*cur
;
216 blobmsg_for_each_attr(cur
, attr
, rem
) {
217 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
) {
218 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur
));
222 addr
= proto_parse_ip_addr_string(blobmsg_data(cur
), v6
, v6
? 32 : 128);
224 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur
));
229 addr
->flags
|= DEVADDR_EXTERNAL
;
231 vlist_add(&iface
->proto_addr
, &addr
->node
);
243 static const struct blobmsg_policy route_attr
[__ROUTE_LAST
] = {
244 [ROUTE_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
245 [ROUTE_MASK
] = { .name
= "mask", .type
= BLOBMSG_TYPE_STRING
},
246 [ROUTE_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
247 [ROUTE_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
251 parse_route(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
253 struct blob_attr
*tb
[__ROUTE_LAST
], *cur
;
254 struct device_route
*route
;
255 int af
= v6
? AF_INET6
: AF_INET
;
257 blobmsg_parse(route_attr
, __ROUTE_LAST
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
259 if (!tb
[ROUTE_GATEWAY
] && !tb
[ROUTE_DEVICE
])
262 route
= calloc(1, sizeof(*route
));
266 route
->mask
= v6
? 128 : 32;
267 if ((cur
= tb
[ROUTE_MASK
]) != NULL
) {
268 route
->mask
= blobmsg_get_u32(cur
);
269 if (route
->mask
> v6
? 128 : 32)
273 if ((cur
= tb
[ROUTE_TARGET
]) != NULL
) {
274 if (!inet_pton(af
, blobmsg_data(cur
), &route
->addr
)) {
275 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur
));
280 if ((cur
= tb
[ROUTE_GATEWAY
]) != NULL
) {
281 if (!inet_pton(af
, blobmsg_data(cur
), &route
->nexthop
)) {
282 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur
));
287 if ((cur
= tb
[ROUTE_DEVICE
]) != NULL
)
288 route
->device
= device_get(blobmsg_data(cur
), true);
290 vlist_add(&iface
->proto_route
, &route
->node
);
298 proto_shell_parse_route_list(struct interface
*iface
, struct blob_attr
*attr
,
301 struct blob_attr
*cur
;
304 blobmsg_for_each_attr(cur
, attr
, rem
) {
305 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
) {
306 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur
));
310 parse_route(iface
, cur
, v6
);
331 static const struct blobmsg_policy notify_attr
[__NOTIFY_LAST
] = {
332 [NOTIFY_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_INT32
},
333 [NOTIFY_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_ARRAY
},
334 [NOTIFY_ENV
] = { .name
= "env", .type
= BLOBMSG_TYPE_ARRAY
},
335 [NOTIFY_SIGNAL
] = { .name
= "signal", .type
= BLOBMSG_TYPE_INT32
},
336 [NOTIFY_LINK_UP
] = { .name
= "link-up", .type
= BLOBMSG_TYPE_BOOL
},
337 [NOTIFY_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
338 [NOTIFY_ADDR_EXT
] = { .name
= "address-external", .type
= BLOBMSG_TYPE_BOOL
},
339 [NOTIFY_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_ARRAY
},
340 [NOTIFY_IP6ADDR
] = { .name
= "ip6addr", .type
= BLOBMSG_TYPE_ARRAY
},
341 [NOTIFY_ROUTES
] = { .name
= "routes", .type
= BLOBMSG_TYPE_ARRAY
},
342 [NOTIFY_ROUTES6
] = { .name
= "routes6", .type
= BLOBMSG_TYPE_ARRAY
},
343 [NOTIFY_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
344 [NOTIFY_DNS_SEARCH
] = { .name
= "dns_search", .type
= BLOBMSG_TYPE_ARRAY
},
348 proto_shell_update_link(struct proto_shell_state
*state
, struct blob_attr
**tb
)
350 struct blob_attr
*cur
;
351 bool addr_ext
= false;
354 if (!tb
[NOTIFY_LINK_UP
])
355 return UBUS_STATUS_INVALID_ARGUMENT
;
357 up
= blobmsg_get_bool(tb
[NOTIFY_LINK_UP
]);
359 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
363 if (!tb
[NOTIFY_IFNAME
]) {
364 if (!state
->proto
.iface
->main_dev
.dev
)
365 return UBUS_STATUS_INVALID_ARGUMENT
;
366 } else if (!state
->l3_dev
.dev
) {
367 device_add_user(&state
->l3_dev
,
368 device_get(blobmsg_data(tb
[NOTIFY_IFNAME
]), true));
369 device_claim(&state
->l3_dev
);
370 state
->proto
.iface
->l3_dev
= &state
->l3_dev
;
373 interface_ip_update_start(state
->proto
.iface
);
375 if ((cur
= tb
[NOTIFY_ADDR_EXT
]) != NULL
)
376 addr_ext
= blobmsg_get_bool(cur
);
378 if ((cur
= tb
[NOTIFY_IPADDR
]) != NULL
)
379 proto_shell_parse_addr_list(state
->proto
.iface
, cur
, false, addr_ext
);
381 if ((cur
= tb
[NOTIFY_IP6ADDR
]) != NULL
)
382 proto_shell_parse_addr_list(state
->proto
.iface
, cur
, true, addr_ext
);
384 if ((cur
= tb
[NOTIFY_ROUTES
]) != NULL
)
385 proto_shell_parse_route_list(state
->proto
.iface
, cur
, false);
387 if ((cur
= tb
[NOTIFY_ROUTES6
]) != NULL
)
388 proto_shell_parse_route_list(state
->proto
.iface
, cur
, true);
390 if ((cur
= tb
[NOTIFY_DNS
]) != NULL
)
391 interface_add_dns_server_list(state
->proto
.iface
, cur
);
393 if ((cur
= tb
[NOTIFY_DNS_SEARCH
]) != NULL
)
394 interface_add_dns_search_list(state
->proto
.iface
, cur
);
396 interface_ip_update_complete(state
->proto
.iface
);
398 state
->proto
.proto_event(&state
->proto
, IFPEV_UP
);
404 fill_string_list(struct blob_attr
*attr
, char **argv
, int max
)
406 struct blob_attr
*cur
;
413 blobmsg_for_each_attr(cur
, attr
, rem
) {
414 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
417 if (!blobmsg_check_attr(cur
, NULL
))
420 argv
[argc
++] = blobmsg_data(cur
);
431 proto_shell_run_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
436 if (!tb
[NOTIFY_COMMAND
])
439 if (!fill_string_list(tb
[NOTIFY_COMMAND
], argv
, ARRAY_SIZE(argv
)))
442 if (!fill_string_list(tb
[NOTIFY_ENV
], env
, ARRAY_SIZE(env
)))
445 start_process((const char **) argv
, (char **) env
, &state
->proto_task
);
450 return UBUS_STATUS_INVALID_ARGUMENT
;
454 proto_shell_kill_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
456 unsigned int signal
= ~0;
458 if (tb
[NOTIFY_SIGNAL
])
459 signal
= blobmsg_get_u32(tb
[NOTIFY_SIGNAL
]);
464 if (state
->proto_task
.pending
) {
465 kill(state
->proto_task
.pid
, signal
);
466 state
->teardown_wait_task
= true;
473 proto_shell_notify(struct interface_proto_state
*proto
, struct blob_attr
*attr
)
475 struct proto_shell_state
*state
;
476 struct blob_attr
*tb
[__NOTIFY_LAST
];
478 state
= container_of(proto
, struct proto_shell_state
, proto
);
480 blobmsg_parse(notify_attr
, __NOTIFY_LAST
, tb
, blob_data(attr
), blob_len(attr
));
481 if (!tb
[NOTIFY_ACTION
])
482 return UBUS_STATUS_INVALID_ARGUMENT
;
484 switch(blobmsg_get_u32(tb
[NOTIFY_ACTION
])) {
486 return proto_shell_update_link(state
, tb
);
488 return proto_shell_run_command(state
, tb
);
490 return proto_shell_kill_command(state
, tb
);
492 return UBUS_STATUS_INVALID_ARGUMENT
;
496 struct interface_proto_state
*
497 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
498 struct blob_attr
*attr
)
500 struct proto_shell_state
*state
;
502 state
= calloc(1, sizeof(*state
));
503 state
->config
= malloc(blob_pad_len(attr
));
507 memcpy(state
->config
, attr
, blob_pad_len(attr
));
508 state
->proto
.free
= proto_shell_free
;
509 state
->proto
.notify
= proto_shell_notify
;
510 state
->proto
.cb
= proto_shell_handler
;
511 state
->setup_timeout
.cb
= proto_shell_setup_timeout_cb
;
512 state
->setup_task
.cb
= proto_shell_setup_cb
;
513 state
->teardown_task
.cb
= proto_shell_teardown_cb
;
514 state
->proto_task
.cb
= proto_shell_task_cb
;
515 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
517 return &state
->proto
;
525 check_type(json_object
*obj
, json_type type
)
530 if (json_object_get_type(obj
) != type
)
536 static inline json_object
*
537 get_field(json_object
*obj
, const char *name
, json_type type
)
539 return check_type(json_object_object_get(obj
, name
), type
);
543 proto_shell_parse_config(struct config_param_list
*config
, json_object
*obj
)
545 struct blobmsg_policy
*attrs
;
546 char *str_buf
, *str_cur
;
550 config
->n_params
= json_object_array_length(obj
);
551 attrs
= calloc(1, sizeof(*attrs
) * config
->n_params
);
555 config
->params
= attrs
;
556 for (i
= 0; i
< config
->n_params
; i
++) {
557 json_object
*cur
, *name
, *type
;
559 cur
= check_type(json_object_array_get_idx(obj
, i
), json_type_array
);
563 name
= check_type(json_object_array_get_idx(cur
, 0), json_type_string
);
567 type
= check_type(json_object_array_get_idx(cur
, 1), json_type_int
);
571 attrs
[i
].name
= json_object_get_string(name
);
572 attrs
[i
].type
= json_object_get_int(type
);
573 if (attrs
[i
].type
> BLOBMSG_TYPE_LAST
)
576 str_len
+= strlen(attrs
[i
].name
) + 1;
579 str_buf
= malloc(str_len
);
584 for (i
= 0; i
< config
->n_params
; i
++) {
585 const char *name
= attrs
[i
].name
;
587 attrs
[i
].name
= str_cur
;
588 str_cur
+= sprintf(str_cur
, "%s", name
) + 1;
595 config
->n_params
= 0;
600 proto_shell_add_handler(const char *script
, json_object
*obj
)
602 struct proto_shell_handler
*handler
;
603 struct proto_handler
*proto
;
604 json_object
*config
, *tmp
;
608 if (!check_type(obj
, json_type_object
))
611 tmp
= get_field(obj
, "name", json_type_string
);
615 name
= json_object_get_string(tmp
);
617 handler
= calloc(1, sizeof(*handler
) +
623 strcpy(handler
->script_name
, script
);
625 str
= handler
->script_name
+ strlen(handler
->script_name
) + 1;
628 proto
= &handler
->proto
;
630 proto
->config_params
= &handler
->config
;
631 proto
->attach
= proto_shell_attach
;
633 tmp
= get_field(obj
, "no-device", json_type_boolean
);
634 if (tmp
&& json_object_get_boolean(tmp
))
635 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
637 config
= get_field(obj
, "config", json_type_array
);
639 handler
->config_buf
= proto_shell_parse_config(&handler
->config
, config
);
641 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
642 add_proto_handler(proto
);
645 static void proto_shell_add_script(const char *name
)
647 struct json_tokener
*tok
= NULL
;
649 static char buf
[512];
650 char *start
, *end
, *cmd
;
654 #define DUMP_SUFFIX " '' dump"
656 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_SUFFIX
));
657 sprintf(cmd
, "%s" DUMP_SUFFIX
, name
);
664 buflen
= fread(buf
, 1, sizeof(buf
) - 1, f
);
671 end
= memchr(start
, '\n', len
);
676 tok
= json_tokener_new();
678 obj
= json_tokener_parse_ex(tok
, start
, len
);
679 if (!is_error(obj
)) {
680 proto_shell_add_handler(name
, obj
);
681 json_object_put(obj
);
682 json_tokener_free(tok
);
688 len
= buflen
- (start
- buf
);
691 } while (!feof(f
) && !ferror(f
));
694 json_tokener_free(tok
);
699 void __init
proto_shell_init(void)
705 main_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
709 if (chdir(main_path
)) {
710 perror("chdir(main path)");
714 if (chdir("./proto"))
717 proto_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
721 glob("./*.sh", 0, NULL
, &g
);
722 for (i
= 0; i
< g
.gl_pathc
; i
++)
723 proto_shell_add_script(g
.gl_pathv
[i
]);