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
;
43 struct uloop_process proto_task
;
47 kill_process(struct uloop_process
*proc
)
52 kill(proc
->pid
, SIGTERM
);
53 uloop_process_delete(proc
);
57 start_process(const char **argv
, struct uloop_process
*proc
)
63 if ((pid
= fork()) < 0)
68 execvp(argv
[0], (char **) argv
);
76 uloop_process_add(proc
);
82 proto_shell_handler(struct interface_proto_state
*proto
,
83 enum interface_proto_cmd cmd
, bool force
)
85 struct proto_shell_state
*state
;
86 struct proto_shell_handler
*handler
;
87 struct uloop_process
*proc
;
93 state
= container_of(proto
, struct proto_shell_state
, proto
);
94 handler
= state
->handler
;
96 if (cmd
== PROTO_CMD_SETUP
) {
98 proc
= &state
->setup_task
;
101 proc
= &state
->teardown_task
;
102 if (state
->setup_task
.pending
) {
103 uloop_timeout_set(&state
->setup_timeout
, 1000);
104 kill(state
->setup_task
.pid
, SIGINT
);
105 state
->teardown_pending
= true;
110 config
= blobmsg_format_json(state
->config
, true);
114 argv
[i
++] = handler
->script_name
;
115 argv
[i
++] = handler
->proto
.name
;
117 argv
[i
++] = proto
->iface
->name
;
119 if (proto
->iface
->main_dev
.dev
)
120 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
123 ret
= start_process(argv
, proc
);
130 proto_shell_setup_timeout_cb(struct uloop_timeout
*timeout
)
132 struct proto_shell_state
*state
;
134 state
= container_of(timeout
, struct proto_shell_state
, setup_timeout
);
135 kill(state
->setup_task
.pid
, SIGKILL
);
139 proto_shell_setup_cb(struct uloop_process
*p
, int ret
)
141 struct proto_shell_state
*state
;
143 state
= container_of(p
, struct proto_shell_state
, setup_task
);
144 uloop_timeout_cancel(&state
->setup_timeout
);
145 if (state
->teardown_pending
) {
146 state
->teardown_pending
= false;
147 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
152 proto_shell_teardown_cb(struct uloop_process
*p
, int ret
)
154 struct proto_shell_state
*state
;
156 state
= container_of(p
, struct proto_shell_state
, teardown_task
);
158 kill_process(&state
->proto_task
);
160 if (state
->l3_dev
.dev
)
161 device_remove_user(&state
->l3_dev
);
163 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
167 proto_shell_task_cb(struct uloop_process
*p
, int ret
)
169 struct proto_shell_state
*state
;
171 state
= container_of(p
, struct proto_shell_state
, proto_task
);
172 if (state
->teardown_pending
|| state
->teardown_task
.pending
)
175 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
176 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
180 proto_shell_free(struct interface_proto_state
*proto
)
182 struct proto_shell_state
*state
;
184 state
= container_of(proto
, struct proto_shell_state
, proto
);
190 proto_shell_parse_addr_list(struct interface
*iface
, struct blob_attr
*attr
,
191 bool v6
, bool external
)
193 struct device_addr
*addr
;
194 struct blob_attr
*cur
;
197 blobmsg_for_each_attr(cur
, attr
, rem
) {
198 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
) {
199 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur
));
203 addr
= proto_parse_ip_addr_string(blobmsg_data(cur
), v6
, v6
? 32 : 128);
205 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur
));
210 addr
->flags
|= DEVADDR_EXTERNAL
;
212 vlist_add(&iface
->proto_addr
, &addr
->node
);
224 static const struct blobmsg_policy route_attr
[__ROUTE_LAST
] = {
225 [ROUTE_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
226 [ROUTE_MASK
] = { .name
= "mask", .type
= BLOBMSG_TYPE_INT32
},
227 [ROUTE_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
228 [ROUTE_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
232 parse_route(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
234 struct blob_attr
*tb
[__ROUTE_LAST
], *cur
;
235 struct device_route
*route
;
236 int af
= v6
? AF_INET6
: AF_INET
;
238 blobmsg_parse(route_attr
, __ROUTE_LAST
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
240 if (!tb
[ROUTE_GATEWAY
] && !tb
[ROUTE_DEVICE
])
243 route
= calloc(1, sizeof(*route
));
247 route
->mask
= v6
? 128 : 32;
248 if ((cur
= tb
[ROUTE_MASK
]) != NULL
) {
249 route
->mask
= blobmsg_get_u32(cur
);
250 if (route
->mask
> v6
? 128 : 32)
254 if ((cur
= tb
[ROUTE_TARGET
]) != NULL
) {
255 if (!inet_pton(af
, blobmsg_data(cur
), &route
->addr
)) {
256 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur
));
261 if ((cur
= tb
[ROUTE_GATEWAY
]) != NULL
) {
262 if (!inet_pton(af
, blobmsg_data(cur
), &route
->nexthop
)) {
263 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur
));
268 if ((cur
= tb
[ROUTE_DEVICE
]) != NULL
)
269 route
->device
= device_get(blobmsg_data(cur
), true);
271 vlist_add(&iface
->proto_route
, &route
->node
);
279 proto_shell_parse_route_list(struct interface
*iface
, struct blob_attr
*attr
,
282 struct blob_attr
*cur
;
285 blobmsg_for_each_attr(cur
, attr
, rem
) {
286 if (blobmsg_type(cur
) != BLOBMSG_TYPE_TABLE
) {
287 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur
));
291 parse_route(iface
, cur
, v6
);
310 static const struct blobmsg_policy notify_attr
[__NOTIFY_LAST
] = {
311 [NOTIFY_ACTION
] = { .name
= "action", .type
= BLOBMSG_TYPE_INT32
},
312 [NOTIFY_COMMAND
] = { .name
= "command", .type
= BLOBMSG_TYPE_ARRAY
},
313 [NOTIFY_LINK_UP
] = { .name
= "link-up", .type
= BLOBMSG_TYPE_BOOL
},
314 [NOTIFY_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
315 [NOTIFY_ADDR_EXT
] = { .name
= "address-external", .type
= BLOBMSG_TYPE_BOOL
},
316 [NOTIFY_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_ARRAY
},
317 [NOTIFY_IP6ADDR
] = { .name
= "ip6addr", .type
= BLOBMSG_TYPE_ARRAY
},
318 [NOTIFY_ROUTES
] = { .name
= "routes", .type
= BLOBMSG_TYPE_ARRAY
},
319 [NOTIFY_ROUTES6
] = { .name
= "routes6", .type
= BLOBMSG_TYPE_ARRAY
},
320 [NOTIFY_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
324 proto_shell_update_link(struct proto_shell_state
*state
, struct blob_attr
**tb
)
326 struct blob_attr
*cur
;
327 bool addr_ext
= false;
330 if (!tb
[NOTIFY_LINK_UP
])
331 return UBUS_STATUS_INVALID_ARGUMENT
;
333 up
= blobmsg_get_bool(tb
[NOTIFY_LINK_UP
]);
335 if (!tb
[NOTIFY_IFNAME
])
336 return UBUS_STATUS_INVALID_ARGUMENT
;
338 if (!state
->l3_dev
.dev
) {
339 device_add_user(&state
->l3_dev
,
340 device_get(blobmsg_data(tb
[NOTIFY_IFNAME
]), true));
341 device_claim(&state
->l3_dev
);
342 state
->proto
.iface
->l3_dev
= &state
->l3_dev
;
344 state
->proto
.proto_event(&state
->proto
, IFPEV_UP
);
346 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
349 if ((cur
= tb
[NOTIFY_ADDR_EXT
]) != NULL
)
350 addr_ext
= blobmsg_get_bool(cur
);
352 if ((cur
= tb
[NOTIFY_IPADDR
]) != NULL
)
353 proto_shell_parse_addr_list(state
->proto
.iface
, cur
, false, addr_ext
);
355 if ((cur
= tb
[NOTIFY_IP6ADDR
]) != NULL
)
356 proto_shell_parse_addr_list(state
->proto
.iface
, cur
, true, addr_ext
);
358 if ((cur
= tb
[NOTIFY_ROUTES
]) != NULL
)
359 proto_shell_parse_route_list(state
->proto
.iface
, cur
, false);
361 if ((cur
= tb
[NOTIFY_ROUTES6
]) != NULL
)
362 proto_shell_parse_route_list(state
->proto
.iface
, cur
, true);
364 if ((cur
= tb
[NOTIFY_DNS
]) != NULL
)
365 interface_add_dns_server_list(state
->proto
.iface
, cur
);
371 proto_shell_run_command(struct proto_shell_state
*state
, struct blob_attr
**tb
)
373 struct blob_attr
*cur
;
378 if (!tb
[NOTIFY_COMMAND
])
381 blobmsg_for_each_attr(cur
, tb
[NOTIFY_COMMAND
], rem
) {
382 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
385 if (!blobmsg_check_attr(cur
, NULL
))
388 argv
[argc
++] = blobmsg_data(cur
);
389 if (argc
== ARRAY_SIZE(argv
) - 1)
393 start_process((const char **) argv
, &state
->proto_task
);
398 return UBUS_STATUS_INVALID_ARGUMENT
;
402 proto_shell_notify(struct interface_proto_state
*proto
, struct blob_attr
*attr
)
404 struct proto_shell_state
*state
;
405 struct blob_attr
*tb
[__NOTIFY_LAST
];
407 state
= container_of(proto
, struct proto_shell_state
, proto
);
409 blobmsg_parse(notify_attr
, __NOTIFY_LAST
, tb
, blob_data(attr
), blob_len(attr
));
410 if (!tb
[NOTIFY_ACTION
])
411 return UBUS_STATUS_INVALID_ARGUMENT
;
413 switch(blobmsg_get_u32(tb
[NOTIFY_ACTION
])) {
415 return proto_shell_update_link(state
, tb
);
417 return proto_shell_run_command(state
, tb
);
419 return UBUS_STATUS_INVALID_ARGUMENT
;
423 struct interface_proto_state
*
424 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
425 struct blob_attr
*attr
)
427 struct proto_shell_state
*state
;
429 state
= calloc(1, sizeof(*state
));
430 state
->config
= malloc(blob_pad_len(attr
));
434 memcpy(state
->config
, attr
, blob_pad_len(attr
));
435 state
->proto
.free
= proto_shell_free
;
436 state
->proto
.notify
= proto_shell_notify
;
437 state
->proto
.cb
= proto_shell_handler
;
438 state
->setup_timeout
.cb
= proto_shell_setup_timeout_cb
;
439 state
->setup_task
.cb
= proto_shell_setup_cb
;
440 state
->teardown_task
.cb
= proto_shell_teardown_cb
;
441 state
->proto_task
.cb
= proto_shell_task_cb
;
442 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
444 return &state
->proto
;
452 check_type(json_object
*obj
, json_type type
)
457 if (json_object_get_type(obj
) != type
)
463 static inline json_object
*
464 get_field(json_object
*obj
, const char *name
, json_type type
)
466 return check_type(json_object_object_get(obj
, name
), type
);
470 proto_shell_parse_config(struct config_param_list
*config
, json_object
*obj
)
472 struct blobmsg_policy
*attrs
;
473 char *str_buf
, *str_cur
;
477 config
->n_params
= json_object_array_length(obj
);
478 attrs
= calloc(1, sizeof(*attrs
) * config
->n_params
);
482 config
->params
= attrs
;
483 for (i
= 0; i
< config
->n_params
; i
++) {
484 json_object
*cur
, *name
, *type
;
486 cur
= check_type(json_object_array_get_idx(obj
, i
), json_type_array
);
490 name
= check_type(json_object_array_get_idx(cur
, 0), json_type_string
);
494 type
= check_type(json_object_array_get_idx(cur
, 1), json_type_int
);
498 attrs
[i
].name
= json_object_get_string(name
);
499 attrs
[i
].type
= json_object_get_int(type
);
500 if (attrs
[i
].type
> BLOBMSG_TYPE_LAST
)
503 str_len
+= strlen(attrs
[i
].name
) + 1;
506 str_buf
= malloc(str_len
);
511 for (i
= 0; i
< config
->n_params
; i
++) {
512 const char *name
= attrs
[i
].name
;
514 attrs
[i
].name
= str_cur
;
515 str_cur
+= sprintf(str_cur
, "%s", name
) + 1;
522 config
->n_params
= 0;
527 proto_shell_add_handler(const char *script
, json_object
*obj
)
529 struct proto_shell_handler
*handler
;
530 struct proto_handler
*proto
;
531 json_object
*config
, *tmp
;
535 if (!check_type(obj
, json_type_object
))
538 tmp
= get_field(obj
, "name", json_type_string
);
542 name
= json_object_get_string(tmp
);
544 handler
= calloc(1, sizeof(*handler
) +
550 strcpy(handler
->script_name
, script
);
552 str
= handler
->script_name
+ strlen(handler
->script_name
) + 1;
555 proto
= &handler
->proto
;
557 proto
->config_params
= &handler
->config
;
558 proto
->attach
= proto_shell_attach
;
560 tmp
= get_field(obj
, "no-device", json_type_boolean
);
561 if (tmp
&& json_object_get_boolean(tmp
))
562 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
564 config
= get_field(obj
, "config", json_type_array
);
566 handler
->config_buf
= proto_shell_parse_config(&handler
->config
, config
);
568 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
569 add_proto_handler(proto
);
572 static void proto_shell_add_script(const char *name
)
574 struct json_tokener
*tok
= NULL
;
576 static char buf
[512];
577 char *start
, *end
, *cmd
;
581 #define DUMP_SUFFIX " '' dump"
583 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_SUFFIX
));
584 sprintf(cmd
, "%s" DUMP_SUFFIX
, name
);
591 buflen
= fread(buf
, 1, sizeof(buf
) - 1, f
);
598 end
= memchr(start
, '\n', len
);
603 tok
= json_tokener_new();
605 obj
= json_tokener_parse_ex(tok
, start
, len
);
606 if (!is_error(obj
)) {
607 proto_shell_add_handler(name
, obj
);
608 json_object_put(obj
);
609 json_tokener_free(tok
);
615 len
= buflen
- (start
- buf
);
618 } while (!feof(f
) && !ferror(f
));
621 json_tokener_free(tok
);
626 void __init
proto_shell_init(void)
632 main_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
636 if (chdir(main_path
)) {
637 perror("chdir(main path)");
641 if (chdir("./proto"))
644 proto_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
648 glob("./*.sh", 0, NULL
, &g
);
649 for (i
= 0; i
< g
.gl_pathc
; i
++)
650 proto_shell_add_script(g
.gl_pathv
[i
]);