9 #include <libubox/blobmsg_json.h>
12 #include "interface.h"
13 #include "interface-ip.h"
18 struct proto_shell_handler
{
19 struct list_head list
;
20 struct proto_handler proto
;
21 struct config_param_list config
;
26 struct proto_shell_state
{
27 struct interface_proto_state proto
;
28 struct proto_shell_handler
*handler
;
29 struct blob_attr
*config
;
31 struct device_user l3_dev
;
33 struct uloop_timeout setup_timeout
;
34 struct uloop_process setup_task
;
35 struct uloop_process teardown_task
;
36 bool teardown_pending
;
40 run_script(const char **argv
, struct uloop_process
*proc
)
44 if ((pid
= fork()) < 0)
49 execvp(argv
[0], (char **) argv
);
57 uloop_process_add(proc
);
63 proto_shell_handler(struct interface_proto_state
*proto
,
64 enum interface_proto_cmd cmd
, bool force
)
66 struct proto_shell_state
*state
;
67 struct proto_shell_handler
*handler
;
68 struct uloop_process
*proc
;
74 state
= container_of(proto
, struct proto_shell_state
, proto
);
75 handler
= state
->handler
;
77 if (cmd
== PROTO_CMD_SETUP
) {
79 proc
= &state
->setup_task
;
82 proc
= &state
->teardown_task
;
83 if (state
->setup_task
.pending
) {
84 uloop_timeout_set(&state
->setup_timeout
, 1000);
85 kill(state
->setup_task
.pid
, SIGINT
);
86 state
->teardown_pending
= true;
91 config
= blobmsg_format_json(state
->config
, true);
95 argv
[i
++] = handler
->script_name
;
96 argv
[i
++] = handler
->proto
.name
;
98 argv
[i
++] = proto
->iface
->name
;
100 if (proto
->iface
->main_dev
.dev
)
101 argv
[i
++] = proto
->iface
->main_dev
.dev
->ifname
;
104 ret
= run_script(argv
, proc
);
111 proto_shell_setup_timeout_cb(struct uloop_timeout
*timeout
)
113 struct proto_shell_state
*state
;
115 state
= container_of(timeout
, struct proto_shell_state
, setup_timeout
);
116 kill(state
->setup_task
.pid
, SIGKILL
);
120 proto_shell_setup_cb(struct uloop_process
*p
, int ret
)
122 struct proto_shell_state
*state
;
124 state
= container_of(p
, struct proto_shell_state
, setup_task
);
125 uloop_timeout_cancel(&state
->setup_timeout
);
126 if (state
->teardown_pending
) {
127 state
->teardown_pending
= false;
128 proto_shell_handler(&state
->proto
, PROTO_CMD_TEARDOWN
, false);
133 proto_shell_teardown_cb(struct uloop_process
*p
, int ret
)
135 struct proto_shell_state
*state
;
137 state
= container_of(p
, struct proto_shell_state
, teardown_task
);
138 state
->proto
.proto_event(&state
->proto
, IFPEV_DOWN
);
139 if (state
->l3_dev
.dev
)
140 device_remove_user(&state
->l3_dev
);
144 proto_shell_free(struct interface_proto_state
*proto
)
146 struct proto_shell_state
*state
;
148 state
= container_of(proto
, struct proto_shell_state
, proto
);
154 proto_shell_parse_addr_list(struct interface
*iface
, struct blob_attr
*attr
,
155 bool v6
, bool external
)
157 struct device_addr
*addr
;
158 struct blob_attr
*cur
;
161 blobmsg_for_each_attr(cur
, attr
, rem
) {
162 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
) {
163 DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur
));
167 addr
= proto_parse_ip_addr_string(blobmsg_data(cur
), v6
, v6
? 32 : 128);
169 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur
));
174 addr
->flags
|= DEVADDR_EXTERNAL
;
176 vlist_add(&iface
->proto_addr
, &addr
->node
);
190 static const struct blobmsg_policy notify_attr
[__NOTIFY_LAST
] = {
191 [NOTIFY_LINK_UP
] = { .name
= "link-up", .type
= BLOBMSG_TYPE_BOOL
},
192 [NOTIFY_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
193 [NOTIFY_ADDR_EXT
] = { .name
= "address-external", .type
= BLOBMSG_TYPE_BOOL
},
194 [NOTIFY_IPADDR
] = { .name
= "ipaddr", .type
= BLOBMSG_TYPE_ARRAY
},
195 [NOTIFY_IP6ADDR
] = { .name
= "ip6addr", .type
= BLOBMSG_TYPE_ARRAY
},
199 proto_shell_notify(struct interface_proto_state
*proto
, struct blob_attr
*attr
)
201 struct proto_shell_state
*state
;
202 struct blob_attr
*tb
[__NOTIFY_LAST
], *cur
;
203 bool addr_ext
= false;
206 state
= container_of(proto
, struct proto_shell_state
, proto
);
208 blobmsg_parse(notify_attr
, __NOTIFY_LAST
, tb
, blob_data(attr
), blob_len(attr
));
209 if (!tb
[NOTIFY_LINK_UP
])
210 return UBUS_STATUS_INVALID_ARGUMENT
;
212 up
= blobmsg_get_bool(tb
[NOTIFY_LINK_UP
]);
214 if (!tb
[NOTIFY_IFNAME
])
215 return UBUS_STATUS_INVALID_ARGUMENT
;
217 if (!state
->l3_dev
.dev
) {
218 device_add_user(&state
->l3_dev
,
219 device_get(blobmsg_data(tb
[NOTIFY_IFNAME
]), true));
220 device_claim(&state
->l3_dev
);
221 state
->proto
.iface
->l3_dev
= &state
->l3_dev
;
223 state
->proto
.proto_event(&state
->proto
, IFPEV_UP
);
225 state
->proto
.proto_event(&state
->proto
, IFPEV_LINK_LOST
);
228 if ((cur
= tb
[NOTIFY_ADDR_EXT
]) != NULL
)
229 addr_ext
= blobmsg_get_bool(cur
);
231 if ((cur
= tb
[NOTIFY_IPADDR
]) != NULL
)
232 proto_shell_parse_addr_list(state
->proto
.iface
, cur
, false, addr_ext
);
234 if ((cur
= tb
[NOTIFY_IP6ADDR
]) != NULL
)
235 proto_shell_parse_addr_list(state
->proto
.iface
, cur
, true, addr_ext
);
240 struct interface_proto_state
*
241 proto_shell_attach(const struct proto_handler
*h
, struct interface
*iface
,
242 struct blob_attr
*attr
)
244 struct proto_shell_state
*state
;
246 state
= calloc(1, sizeof(*state
));
247 state
->config
= malloc(blob_pad_len(attr
));
251 memcpy(state
->config
, attr
, blob_pad_len(attr
));
252 state
->proto
.free
= proto_shell_free
;
253 state
->proto
.notify
= proto_shell_notify
;
254 state
->proto
.cb
= proto_shell_handler
;
255 state
->setup_timeout
.cb
= proto_shell_setup_timeout_cb
;
256 state
->setup_task
.cb
= proto_shell_setup_cb
;
257 state
->teardown_task
.cb
= proto_shell_teardown_cb
;
258 state
->handler
= container_of(h
, struct proto_shell_handler
, proto
);
260 return &state
->proto
;
268 check_type(json_object
*obj
, json_type type
)
273 if (json_object_get_type(obj
) != type
)
279 static inline json_object
*
280 get_field(json_object
*obj
, const char *name
, json_type type
)
282 return check_type(json_object_object_get(obj
, name
), type
);
286 proto_shell_parse_config(struct config_param_list
*config
, json_object
*obj
)
288 struct blobmsg_policy
*attrs
;
289 char *str_buf
, *str_cur
;
293 attrs
= calloc(1, sizeof(*attrs
));
297 config
->n_params
= json_object_array_length(obj
);
298 config
->params
= attrs
;
299 for (i
= 0; i
< config
->n_params
; i
++) {
300 json_object
*cur
, *name
, *type
;
302 cur
= check_type(json_object_array_get_idx(obj
, i
), json_type_array
);
306 name
= check_type(json_object_array_get_idx(cur
, 0), json_type_string
);
310 type
= check_type(json_object_array_get_idx(cur
, 1), json_type_int
);
314 attrs
[i
].name
= json_object_get_string(name
);
315 attrs
[i
].type
= json_object_get_int(type
);
316 if (attrs
[i
].type
> BLOBMSG_TYPE_LAST
)
319 str_len
+= strlen(attrs
[i
].name
+ 1);
322 str_buf
= malloc(str_len
);
327 for (i
= 0; i
< config
->n_params
; i
++) {
328 const char *name
= attrs
[i
].name
;
330 attrs
[i
].name
= str_cur
;
331 str_cur
+= sprintf(str_cur
, "%s", name
) + 1;
338 config
->n_params
= 0;
343 proto_shell_add_handler(const char *script
, json_object
*obj
)
345 struct proto_shell_handler
*handler
;
346 struct proto_handler
*proto
;
347 json_object
*config
, *tmp
;
351 if (!check_type(obj
, json_type_object
))
354 tmp
= get_field(obj
, "name", json_type_string
);
358 name
= json_object_get_string(tmp
);
360 handler
= calloc(1, sizeof(*handler
) +
366 strcpy(handler
->script_name
, script
);
368 str
= handler
->script_name
+ strlen(handler
->script_name
) + 1;
371 proto
= &handler
->proto
;
373 proto
->config_params
= &handler
->config
;
374 proto
->attach
= proto_shell_attach
;
376 tmp
= get_field(obj
, "no-device", json_type_boolean
);
377 if (tmp
&& json_object_get_boolean(tmp
))
378 handler
->proto
.flags
|= PROTO_FLAG_NODEV
;
380 config
= get_field(obj
, "config", json_type_array
);
382 handler
->config_buf
= proto_shell_parse_config(&handler
->config
, config
);
384 DPRINTF("Add handler for script %s: %s\n", script
, proto
->name
);
385 add_proto_handler(proto
);
388 static void proto_shell_add_script(const char *name
)
390 struct json_tokener
*tok
= NULL
;
392 static char buf
[512];
393 char *start
, *end
, *cmd
;
397 #define DUMP_SUFFIX " '' dump"
399 cmd
= alloca(strlen(name
) + 1 + sizeof(DUMP_SUFFIX
));
400 sprintf(cmd
, "%s" DUMP_SUFFIX
, name
);
407 buflen
= fread(buf
, 1, sizeof(buf
) - 1, f
);
414 end
= memchr(start
, '\n', len
);
419 tok
= json_tokener_new();
421 obj
= json_tokener_parse_ex(tok
, start
, len
);
422 if (!is_error(obj
)) {
423 proto_shell_add_handler(name
, obj
);
424 json_object_put(obj
);
425 json_tokener_free(tok
);
431 len
= buflen
- (start
- buf
);
434 } while (!feof(f
) && !ferror(f
));
437 json_tokener_free(tok
);
442 void __init
proto_shell_init(void)
448 main_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
452 if (chdir(main_path
)) {
453 perror("chdir(main path)");
457 if (chdir("./proto"))
460 proto_fd
= open(".", O_RDONLY
| O_DIRECTORY
);
464 glob("./*.sh", 0, NULL
, &g
);
465 for (i
= 0; i
< g
.gl_pathc
; i
++)
466 proto_shell_add_script(g
.gl_pathv
[i
]);