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