ee2366f5ab8ad5b33e63cb59282db1f2716c9f9c
[project/netifd.git] / proto-shell.c
1 #define _GNU_SOURCE
2
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <glob.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <signal.h>
10
11 #include <arpa/inet.h>
12 #include <netinet/in.h>
13
14 #include <libubox/blobmsg_json.h>
15
16 #include "netifd.h"
17 #include "interface.h"
18 #include "interface-ip.h"
19 #include "proto.h"
20
21 static int proto_fd;
22
23 struct proto_shell_handler {
24         struct list_head list;
25         struct proto_handler proto;
26         struct config_param_list config;
27         char *config_buf;
28         char script_name[];
29 };
30
31 struct proto_shell_state {
32         struct interface_proto_state proto;
33         struct proto_shell_handler *handler;
34         struct blob_attr *config;
35
36         struct device_user l3_dev;
37
38         struct uloop_timeout setup_timeout;
39         struct uloop_process setup_task;
40         struct uloop_process teardown_task;
41         bool teardown_pending;
42
43         struct uloop_process proto_task;
44 };
45
46 static int
47 start_process(const char **argv, struct uloop_process *proc)
48 {
49         int pid;
50
51         if (proc->pending) {
52                 kill(proc->pid, SIGTERM);
53                 uloop_process_delete(proc);
54         }
55
56         if ((pid = fork()) < 0)
57                 return -1;
58
59         if (!pid) {
60                 fchdir(proto_fd);
61                 execvp(argv[0], (char **) argv);
62                 exit(127);
63         }
64
65         if (pid < 0)
66                 return -1;
67
68         proc->pid = pid;
69         uloop_process_add(proc);
70
71         return 0;
72 }
73
74 static int
75 proto_shell_handler(struct interface_proto_state *proto,
76                     enum interface_proto_cmd cmd, bool force)
77 {
78         struct proto_shell_state *state;
79         struct proto_shell_handler *handler;
80         struct uloop_process *proc;
81         const char *argv[6];
82         const char *action;
83         char *config;
84         int ret, i = 0;
85
86         state = container_of(proto, struct proto_shell_state, proto);
87         handler = state->handler;
88
89         if (cmd == PROTO_CMD_SETUP) {
90                 action = "setup";
91                 proc = &state->setup_task;
92         } else {
93                 action = "teardown";
94                 proc = &state->teardown_task;
95                 if (state->setup_task.pending) {
96                         uloop_timeout_set(&state->setup_timeout, 1000);
97                         kill(state->setup_task.pid, SIGINT);
98                         state->teardown_pending = true;
99                         return 0;
100                 }
101         }
102
103         config = blobmsg_format_json(state->config, true);
104         if (!config)
105                 return -1;
106
107         argv[i++] = handler->script_name;
108         argv[i++] = handler->proto.name;
109         argv[i++] = action;
110         argv[i++] = proto->iface->name;
111         argv[i++] = config;
112         if (proto->iface->main_dev.dev)
113                 argv[i++] = proto->iface->main_dev.dev->ifname;
114         argv[i] = NULL;
115
116         ret = start_process(argv, proc);
117         free(config);
118
119         return ret;
120 }
121
122 static void
123 proto_shell_setup_timeout_cb(struct uloop_timeout *timeout)
124 {
125         struct proto_shell_state *state;
126
127         state = container_of(timeout, struct proto_shell_state, setup_timeout);
128         kill(state->setup_task.pid, SIGKILL);
129 }
130
131 static void
132 proto_shell_setup_cb(struct uloop_process *p, int ret)
133 {
134         struct proto_shell_state *state;
135
136         state = container_of(p, struct proto_shell_state, setup_task);
137         uloop_timeout_cancel(&state->setup_timeout);
138         if (state->teardown_pending) {
139                 state->teardown_pending = false;
140                 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
141         }
142 }
143
144 static void
145 proto_shell_teardown_cb(struct uloop_process *p, int ret)
146 {
147         struct proto_shell_state *state;
148
149         state = container_of(p, struct proto_shell_state, teardown_task);
150
151         if (state->l3_dev.dev)
152                 device_remove_user(&state->l3_dev);
153
154         state->proto.proto_event(&state->proto, IFPEV_DOWN);
155 }
156
157 static void
158 proto_shell_task_cb(struct uloop_process *p, int ret)
159 {
160         struct proto_shell_state *state;
161
162         state = container_of(p, struct proto_shell_state, proto_task);
163
164         state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
165         proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
166 }
167
168 static void
169 proto_shell_free(struct interface_proto_state *proto)
170 {
171         struct proto_shell_state *state;
172
173         state = container_of(proto, struct proto_shell_state, proto);
174         free(state->config);
175         free(state);
176 }
177
178 static void
179 proto_shell_parse_addr_list(struct interface *iface, struct blob_attr *attr,
180                             bool v6, bool external)
181 {
182         struct device_addr *addr;
183         struct blob_attr *cur;
184         int rem;
185
186         blobmsg_for_each_attr(cur, attr, rem) {
187                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
188                         DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur));
189                         continue;
190                 }
191
192                 addr = proto_parse_ip_addr_string(blobmsg_data(cur), v6, v6 ? 32 : 128);
193                 if (!addr) {
194                         DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur));
195                         continue;
196                 }
197
198                 if (external)
199                         addr->flags |= DEVADDR_EXTERNAL;
200
201                 vlist_add(&iface->proto_addr, &addr->node);
202         }
203 }
204
205 enum {
206         ROUTE_TARGET,
207         ROUTE_MASK,
208         ROUTE_GATEWAY,
209         ROUTE_DEVICE,
210         __ROUTE_LAST
211 };
212
213 static const struct blobmsg_policy route_attr[__ROUTE_LAST] = {
214         [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
215         [ROUTE_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_INT32 },
216         [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
217         [ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
218 };
219
220 static void
221 parse_route(struct interface *iface, struct blob_attr *attr, bool v6)
222 {
223         struct blob_attr *tb[__ROUTE_LAST], *cur;
224         struct device_route *route;
225         int af = v6 ? AF_INET6 : AF_INET;
226
227         blobmsg_parse(route_attr, __ROUTE_LAST, tb, blobmsg_data(attr), blobmsg_data_len(attr));
228
229         if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
230                 return;
231
232         route = calloc(1, sizeof(*route));
233         if (!route)
234                 return;
235
236         route->mask = v6 ? 128 : 32;
237         if ((cur = tb[ROUTE_MASK]) != NULL) {
238                 route->mask = blobmsg_get_u32(cur);
239                 if (route->mask > v6 ? 128 : 32)
240                         goto error;
241         }
242
243         if ((cur = tb[ROUTE_TARGET]) != NULL) {
244                 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
245                         DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
246                         goto error;
247                 }
248         }
249
250         if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
251                 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
252                         DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
253                         goto error;
254                 }
255         }
256
257         if ((cur = tb[ROUTE_DEVICE]) != NULL)
258                 route->device = device_get(blobmsg_data(cur), true);
259
260         vlist_add(&iface->proto_route, &route->node);
261         return;
262
263 error:
264         free(route);
265 }
266
267 static void
268 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
269                              bool v6)
270 {
271         struct blob_attr *cur;
272         int rem;
273
274         blobmsg_for_each_attr(cur, attr, rem) {
275                 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
276                         DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
277                         continue;
278                 }
279
280                 parse_route(iface, cur, v6);
281         }
282 }
283
284
285 enum {
286         NOTIFY_ACTION,
287         NOTIFY_COMMAND,
288         NOTIFY_LINK_UP,
289         NOTIFY_IFNAME,
290         NOTIFY_ADDR_EXT,
291         NOTIFY_IPADDR,
292         NOTIFY_IP6ADDR,
293         NOTIFY_ROUTES,
294         NOTIFY_ROUTES6,
295         __NOTIFY_LAST
296 };
297
298 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
299         [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
300         [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
301         [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
302         [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
303         [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
304         [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
305         [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
306         [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
307         [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
308 };
309
310 static int
311 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
312 {
313         struct blob_attr *cur;
314         bool addr_ext = false;
315         bool up;
316
317         if (!tb[NOTIFY_LINK_UP])
318                 return UBUS_STATUS_INVALID_ARGUMENT;
319
320         up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
321         if (up) {
322                 if (!tb[NOTIFY_IFNAME])
323                         return UBUS_STATUS_INVALID_ARGUMENT;
324
325                 if (!state->l3_dev.dev) {
326                         device_add_user(&state->l3_dev,
327                                 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
328                         device_claim(&state->l3_dev);
329                         state->proto.iface->l3_dev = &state->l3_dev;
330                 }
331                 state->proto.proto_event(&state->proto, IFPEV_UP);
332         } else {
333                 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
334         }
335
336         if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL)
337                 addr_ext = blobmsg_get_bool(cur);
338
339         if ((cur = tb[NOTIFY_IPADDR]) != NULL)
340                 proto_shell_parse_addr_list(state->proto.iface, cur, false, addr_ext);
341
342         if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
343                 proto_shell_parse_addr_list(state->proto.iface, cur, true, addr_ext);
344
345         if ((cur = tb[NOTIFY_ROUTES]) != NULL)
346                 proto_shell_parse_route_list(state->proto.iface, cur, false);
347
348         if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
349                 proto_shell_parse_route_list(state->proto.iface, cur, true);
350
351         return 0;
352 }
353
354 static int
355 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
356 {
357         struct blob_attr *cur;
358         char *argv[64];
359         int argc = 0;
360         int rem;
361
362         if (!tb[NOTIFY_COMMAND])
363                 goto error;
364
365         blobmsg_for_each_attr(cur, tb[NOTIFY_COMMAND], rem) {
366                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
367                         goto error;
368
369                 if (!blobmsg_check_attr(cur, NULL))
370                         goto error;
371
372                 argv[argc++] = blobmsg_data(cur);
373                 if (argc == ARRAY_SIZE(argv) - 1)
374                         goto error;
375         }
376         argv[argc] = NULL;
377         start_process((const char **) argv, &state->proto_task);
378
379         return 0;
380
381 error:
382         return UBUS_STATUS_INVALID_ARGUMENT;
383 }
384
385 static int
386 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
387 {
388         struct proto_shell_state *state;
389         struct blob_attr *tb[__NOTIFY_LAST];
390
391         state = container_of(proto, struct proto_shell_state, proto);
392
393         blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
394         if (!tb[NOTIFY_ACTION])
395                 return UBUS_STATUS_INVALID_ARGUMENT;
396
397         switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
398         case 0:
399                 return proto_shell_update_link(state, tb);
400         case 1:
401                 return proto_shell_run_command(state, tb);
402         default:
403                 return UBUS_STATUS_INVALID_ARGUMENT;
404         }
405 }
406
407 struct interface_proto_state *
408 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
409                    struct blob_attr *attr)
410 {
411         struct proto_shell_state *state;
412
413         state = calloc(1, sizeof(*state));
414         state->config = malloc(blob_pad_len(attr));
415         if (!state->config)
416                 goto error;
417
418         memcpy(state->config, attr, blob_pad_len(attr));
419         state->proto.free = proto_shell_free;
420         state->proto.notify = proto_shell_notify;
421         state->proto.cb = proto_shell_handler;
422         state->setup_timeout.cb = proto_shell_setup_timeout_cb;
423         state->setup_task.cb = proto_shell_setup_cb;
424         state->teardown_task.cb = proto_shell_teardown_cb;
425         state->proto_task.cb = proto_shell_task_cb;
426         state->handler = container_of(h, struct proto_shell_handler, proto);
427
428         return &state->proto;
429
430 error:
431         free(state);
432         return NULL;
433 }
434
435 static json_object *
436 check_type(json_object *obj, json_type type)
437 {
438         if (!obj)
439                 return NULL;
440
441         if (json_object_get_type(obj) != type)
442                 return NULL;
443
444         return obj;
445 }
446
447 static inline json_object *
448 get_field(json_object *obj, const char *name, json_type type)
449 {
450         return check_type(json_object_object_get(obj, name), type);
451 }
452
453 static char *
454 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
455 {
456         struct blobmsg_policy *attrs;
457         char *str_buf, *str_cur;
458         int str_len = 0;
459         int i;
460
461         config->n_params = json_object_array_length(obj);
462         attrs = calloc(1, sizeof(*attrs) * config->n_params);
463         if (!attrs)
464                 return NULL;
465
466         config->params = attrs;
467         for (i = 0; i < config->n_params; i++) {
468                 json_object *cur, *name, *type;
469
470                 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
471                 if (!cur)
472                         goto error;
473
474                 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
475                 if (!name)
476                         goto error;
477
478                 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
479                 if (!type)
480                         goto error;
481
482                 attrs[i].name = json_object_get_string(name);
483                 attrs[i].type = json_object_get_int(type);
484                 if (attrs[i].type > BLOBMSG_TYPE_LAST)
485                         goto error;
486
487                 str_len += strlen(attrs[i].name) + 1;
488         }
489
490         str_buf = malloc(str_len);
491         if (!str_buf)
492                 goto error;
493
494         str_cur = str_buf;
495         for (i = 0; i < config->n_params; i++) {
496                 const char *name = attrs[i].name;
497
498                 attrs[i].name = str_cur;
499                 str_cur += sprintf(str_cur, "%s", name) + 1;
500         }
501
502         return str_buf;
503
504 error:
505         free(attrs);
506         config->n_params = 0;
507         return NULL;
508 }
509
510 static void
511 proto_shell_add_handler(const char *script, json_object *obj)
512 {
513         struct proto_shell_handler *handler;
514         struct proto_handler *proto;
515         json_object *config, *tmp;
516         const char *name;
517         char *str;
518
519         if (!check_type(obj, json_type_object))
520                 return;
521
522         tmp = get_field(obj, "name", json_type_string);
523         if (!tmp)
524                 return;
525
526         name = json_object_get_string(tmp);
527
528         handler = calloc(1, sizeof(*handler) +
529                          strlen(script) + 1 +
530                          strlen(name) + 1);
531         if (!handler)
532                 return;
533
534         strcpy(handler->script_name, script);
535
536         str = handler->script_name + strlen(handler->script_name) + 1;
537         strcpy(str, name);
538
539         proto = &handler->proto;
540         proto->name = str;
541         proto->config_params = &handler->config;
542         proto->attach = proto_shell_attach;
543
544         tmp = get_field(obj, "no-device", json_type_boolean);
545         if (tmp && json_object_get_boolean(tmp))
546                 handler->proto.flags |= PROTO_FLAG_NODEV;
547
548         config = get_field(obj, "config", json_type_array);
549         if (config)
550                 handler->config_buf = proto_shell_parse_config(&handler->config, config);
551
552         DPRINTF("Add handler for script %s: %s\n", script, proto->name);
553         add_proto_handler(proto);
554 }
555
556 static void proto_shell_add_script(const char *name)
557 {
558         struct json_tokener *tok = NULL;
559         json_object *obj;
560         static char buf[512];
561         char *start, *end, *cmd;
562         FILE *f;
563         int buflen, len;
564
565 #define DUMP_SUFFIX     " '' dump"
566
567         cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
568         sprintf(cmd, "%s" DUMP_SUFFIX, name);
569
570         f = popen(cmd, "r");
571         if (!f)
572                 return;
573
574         do {
575                 buflen = fread(buf, 1, sizeof(buf) - 1, f);
576                 if (buflen <= 0)
577                         continue;
578
579                 start = buf;
580                 len = buflen;
581                 do {
582                         end = memchr(start, '\n', len);
583                         if (end)
584                                 len = end - start;
585
586                         if (!tok)
587                                 tok = json_tokener_new();
588
589                         obj = json_tokener_parse_ex(tok, start, len);
590                         if (!is_error(obj)) {
591                                 proto_shell_add_handler(name, obj);
592                                 json_object_put(obj);
593                                 json_tokener_free(tok);
594                                 tok = NULL;
595                         }
596
597                         if (end) {
598                                 start = end + 1;
599                                 len = buflen - (start - buf);
600                         }
601                 } while (len > 0);
602         } while (!feof(f) && !ferror(f));
603
604         if (tok)
605                 json_tokener_free(tok);
606
607         pclose(f);
608 }
609
610 void __init proto_shell_init(void)
611 {
612         glob_t g;
613         int main_fd;
614         int i;
615
616         main_fd = open(".", O_RDONLY | O_DIRECTORY);
617         if (main_fd < 0)
618                 return;
619
620         if (chdir(main_path)) {
621                 perror("chdir(main path)");
622                 goto close_cur;
623         }
624
625         if (chdir("./proto"))
626                 goto close_cur;
627
628         proto_fd = open(".", O_RDONLY | O_DIRECTORY);
629         if (proto_fd < 0)
630                 goto close_cur;
631
632         glob("./*.sh", 0, NULL, &g);
633         for (i = 0; i < g.gl_pathc; i++)
634                 proto_shell_add_script(g.gl_pathv[i]);
635
636 close_cur:
637         fchdir(main_fd);
638         close(main_fd);
639 }