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