557e5237e44203e6541e650e3121cf523f3e9dff
[project/netifd.git] / proto-shell.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <glob.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <signal.h>
8
9 #include <libubox/blobmsg_json.h>
10
11 #include "netifd.h"
12 #include "interface.h"
13 #include "interface-ip.h"
14 #include "proto.h"
15
16 static int proto_fd;
17
18 struct proto_shell_handler {
19 struct list_head list;
20 struct proto_handler proto;
21 struct config_param_list config;
22 char *config_buf;
23 char script_name[];
24 };
25
26 struct proto_shell_state {
27 struct interface_proto_state proto;
28 struct proto_shell_handler *handler;
29 struct blob_attr *config;
30
31 struct device_user l3_dev;
32
33 struct uloop_timeout setup_timeout;
34 struct uloop_process setup_task;
35 struct uloop_process teardown_task;
36 bool teardown_pending;
37 };
38
39 static int
40 run_script(const char **argv, struct uloop_process *proc)
41 {
42 int pid;
43
44 if ((pid = fork()) < 0)
45 return -1;
46
47 if (!pid) {
48 fchdir(proto_fd);
49 execvp(argv[0], (char **) argv);
50 exit(127);
51 }
52
53 if (pid < 0)
54 return -1;
55
56 proc->pid = pid;
57 uloop_process_add(proc);
58
59 return 0;
60 }
61
62 static int
63 proto_shell_handler(struct interface_proto_state *proto,
64 enum interface_proto_cmd cmd, bool force)
65 {
66 struct proto_shell_state *state;
67 struct proto_shell_handler *handler;
68 struct uloop_process *proc;
69 const char *argv[6];
70 const char *action;
71 char *config;
72 int ret, i = 0;
73
74 state = container_of(proto, struct proto_shell_state, proto);
75 handler = state->handler;
76
77 if (cmd == PROTO_CMD_SETUP) {
78 action = "setup";
79 proc = &state->setup_task;
80 } else {
81 action = "teardown";
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;
87 return 0;
88 }
89 }
90
91 config = blobmsg_format_json(state->config, true);
92 if (!config)
93 return -1;
94
95 argv[i++] = handler->script_name;
96 argv[i++] = handler->proto.name;
97 argv[i++] = action;
98 argv[i++] = proto->iface->name;
99 argv[i++] = config;
100 if (proto->iface->main_dev.dev)
101 argv[i++] = proto->iface->main_dev.dev->ifname;
102 argv[i] = NULL;
103
104 ret = run_script(argv, proc);
105 free(config);
106
107 return ret;
108 }
109
110 static void
111 proto_shell_setup_timeout_cb(struct uloop_timeout *timeout)
112 {
113 struct proto_shell_state *state;
114
115 state = container_of(timeout, struct proto_shell_state, setup_timeout);
116 kill(state->setup_task.pid, SIGKILL);
117 }
118
119 static void
120 proto_shell_setup_cb(struct uloop_process *p, int ret)
121 {
122 struct proto_shell_state *state;
123
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);
129 }
130 }
131
132 static void
133 proto_shell_teardown_cb(struct uloop_process *p, int ret)
134 {
135 struct proto_shell_state *state;
136
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);
141 }
142
143 static void
144 proto_shell_free(struct interface_proto_state *proto)
145 {
146 struct proto_shell_state *state;
147
148 state = container_of(proto, struct proto_shell_state, proto);
149 free(state->config);
150 free(state);
151 }
152
153 static void
154 proto_shell_parse_addr_list(struct interface *iface, struct blob_attr *attr,
155 bool v6, bool external)
156 {
157 struct device_addr *addr;
158 struct blob_attr *cur;
159 int rem;
160
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));
164 continue;
165 }
166
167 addr = proto_parse_ip_addr_string(blobmsg_data(cur), v6, v6 ? 32 : 128);
168 if (!addr) {
169 DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur));
170 continue;
171 }
172
173 if (external)
174 addr->flags |= DEVADDR_EXTERNAL;
175
176 vlist_add(&iface->proto_addr, &addr->node);
177 }
178 }
179
180
181 enum {
182 NOTIFY_LINK_UP,
183 NOTIFY_IFNAME,
184 NOTIFY_ADDR_EXT,
185 NOTIFY_IPADDR,
186 NOTIFY_IP6ADDR,
187 __NOTIFY_LAST
188 };
189
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 },
196 };
197
198 static int
199 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
200 {
201 struct proto_shell_state *state;
202 struct blob_attr *tb[__NOTIFY_LAST], *cur;
203 bool addr_ext = false;
204 bool up;
205
206 state = container_of(proto, struct proto_shell_state, proto);
207
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;
211
212 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
213 if (up) {
214 if (!tb[NOTIFY_IFNAME])
215 return UBUS_STATUS_INVALID_ARGUMENT;
216
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;
222 }
223 state->proto.proto_event(&state->proto, IFPEV_UP);
224 } else {
225 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
226 }
227
228 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL)
229 addr_ext = blobmsg_get_bool(cur);
230
231 if ((cur = tb[NOTIFY_IPADDR]) != NULL)
232 proto_shell_parse_addr_list(state->proto.iface, cur, false, addr_ext);
233
234 if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
235 proto_shell_parse_addr_list(state->proto.iface, cur, true, addr_ext);
236
237 return 0;
238 }
239
240 struct interface_proto_state *
241 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
242 struct blob_attr *attr)
243 {
244 struct proto_shell_state *state;
245
246 state = calloc(1, sizeof(*state));
247 state->config = malloc(blob_pad_len(attr));
248 if (!state->config)
249 goto error;
250
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);
259
260 return &state->proto;
261
262 error:
263 free(state);
264 return NULL;
265 }
266
267 static json_object *
268 check_type(json_object *obj, json_type type)
269 {
270 if (!obj)
271 return NULL;
272
273 if (json_object_get_type(obj) != type)
274 return NULL;
275
276 return obj;
277 }
278
279 static inline json_object *
280 get_field(json_object *obj, const char *name, json_type type)
281 {
282 return check_type(json_object_object_get(obj, name), type);
283 }
284
285 static char *
286 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
287 {
288 struct blobmsg_policy *attrs;
289 char *str_buf, *str_cur;
290 int str_len = 0;
291 int i;
292
293 attrs = calloc(1, sizeof(*attrs));
294 if (!attrs)
295 return NULL;
296
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;
301
302 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
303 if (!cur)
304 goto error;
305
306 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
307 if (!name)
308 goto error;
309
310 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
311 if (!type)
312 goto error;
313
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)
317 goto error;
318
319 str_len += strlen(attrs[i].name + 1);
320 }
321
322 str_buf = malloc(str_len);
323 if (!str_buf)
324 goto error;
325
326 str_cur = str_buf;
327 for (i = 0; i < config->n_params; i++) {
328 const char *name = attrs[i].name;
329
330 attrs[i].name = str_cur;
331 str_cur += sprintf(str_cur, "%s", name) + 1;
332 }
333
334 return str_buf;
335
336 error:
337 free(attrs);
338 config->n_params = 0;
339 return NULL;
340 }
341
342 static void
343 proto_shell_add_handler(const char *script, json_object *obj)
344 {
345 struct proto_shell_handler *handler;
346 struct proto_handler *proto;
347 json_object *config, *tmp;
348 const char *name;
349 char *str;
350
351 if (!check_type(obj, json_type_object))
352 return;
353
354 tmp = get_field(obj, "name", json_type_string);
355 if (!tmp)
356 return;
357
358 name = json_object_get_string(tmp);
359
360 handler = calloc(1, sizeof(*handler) +
361 strlen(script) + 1 +
362 strlen(name) + 1);
363 if (!handler)
364 return;
365
366 strcpy(handler->script_name, script);
367
368 str = handler->script_name + strlen(handler->script_name) + 1;
369 strcpy(str, name);
370
371 proto = &handler->proto;
372 proto->name = str;
373 proto->config_params = &handler->config;
374 proto->attach = proto_shell_attach;
375
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;
379
380 config = get_field(obj, "config", json_type_array);
381 if (config)
382 handler->config_buf = proto_shell_parse_config(&handler->config, config);
383
384 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
385 add_proto_handler(proto);
386 }
387
388 static void proto_shell_add_script(const char *name)
389 {
390 struct json_tokener *tok = NULL;
391 json_object *obj;
392 static char buf[512];
393 char *start, *end, *cmd;
394 FILE *f;
395 int buflen, len;
396
397 #define DUMP_SUFFIX " '' dump"
398
399 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
400 sprintf(cmd, "%s" DUMP_SUFFIX, name);
401
402 f = popen(cmd, "r");
403 if (!f)
404 return;
405
406 do {
407 buflen = fread(buf, 1, sizeof(buf) - 1, f);
408 if (buflen <= 0)
409 continue;
410
411 start = buf;
412 len = buflen;
413 do {
414 end = memchr(start, '\n', len);
415 if (end)
416 len = end - start;
417
418 if (!tok)
419 tok = json_tokener_new();
420
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);
426 tok = NULL;
427 }
428
429 if (end) {
430 start = end + 1;
431 len = buflen - (start - buf);
432 }
433 } while (len > 0);
434 } while (!feof(f) && !ferror(f));
435
436 if (tok)
437 json_tokener_free(tok);
438
439 pclose(f);
440 }
441
442 void __init proto_shell_init(void)
443 {
444 glob_t g;
445 int main_fd;
446 int i;
447
448 main_fd = open(".", O_RDONLY | O_DIRECTORY);
449 if (main_fd < 0)
450 return;
451
452 if (chdir(main_path)) {
453 perror("chdir(main path)");
454 goto close_cur;
455 }
456
457 if (chdir("./proto"))
458 goto close_cur;
459
460 proto_fd = open(".", O_RDONLY | O_DIRECTORY);
461 if (proto_fd < 0)
462 goto close_cur;
463
464 glob("./*.sh", 0, NULL, &g);
465 for (i = 0; i < g.gl_pathc; i++)
466 proto_shell_add_script(g.gl_pathv[i]);
467
468 close_cur:
469 fchdir(main_fd);
470 close(main_fd);
471 }