add functions for adding dns servers to the proto list, hook them up in proto-static.c
[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_LAST
307 };
308
309 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
310         [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
311         [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
312         [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
313         [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
314         [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
315         [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
316         [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
317         [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
318         [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
319 };
320
321 static int
322 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr **tb)
323 {
324         struct blob_attr *cur;
325         bool addr_ext = false;
326         bool up;
327
328         if (!tb[NOTIFY_LINK_UP])
329                 return UBUS_STATUS_INVALID_ARGUMENT;
330
331         up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
332         if (up) {
333                 if (!tb[NOTIFY_IFNAME])
334                         return UBUS_STATUS_INVALID_ARGUMENT;
335
336                 if (!state->l3_dev.dev) {
337                         device_add_user(&state->l3_dev,
338                                 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
339                         device_claim(&state->l3_dev);
340                         state->proto.iface->l3_dev = &state->l3_dev;
341                 }
342                 state->proto.proto_event(&state->proto, IFPEV_UP);
343         } else {
344                 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
345         }
346
347         if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL)
348                 addr_ext = blobmsg_get_bool(cur);
349
350         if ((cur = tb[NOTIFY_IPADDR]) != NULL)
351                 proto_shell_parse_addr_list(state->proto.iface, cur, false, addr_ext);
352
353         if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
354                 proto_shell_parse_addr_list(state->proto.iface, cur, true, addr_ext);
355
356         if ((cur = tb[NOTIFY_ROUTES]) != NULL)
357                 proto_shell_parse_route_list(state->proto.iface, cur, false);
358
359         if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
360                 proto_shell_parse_route_list(state->proto.iface, cur, true);
361
362         return 0;
363 }
364
365 static int
366 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
367 {
368         struct blob_attr *cur;
369         char *argv[64];
370         int argc = 0;
371         int rem;
372
373         if (!tb[NOTIFY_COMMAND])
374                 goto error;
375
376         blobmsg_for_each_attr(cur, tb[NOTIFY_COMMAND], rem) {
377                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
378                         goto error;
379
380                 if (!blobmsg_check_attr(cur, NULL))
381                         goto error;
382
383                 argv[argc++] = blobmsg_data(cur);
384                 if (argc == ARRAY_SIZE(argv) - 1)
385                         goto error;
386         }
387         argv[argc] = NULL;
388         start_process((const char **) argv, &state->proto_task);
389
390         return 0;
391
392 error:
393         return UBUS_STATUS_INVALID_ARGUMENT;
394 }
395
396 static int
397 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
398 {
399         struct proto_shell_state *state;
400         struct blob_attr *tb[__NOTIFY_LAST];
401
402         state = container_of(proto, struct proto_shell_state, proto);
403
404         blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
405         if (!tb[NOTIFY_ACTION])
406                 return UBUS_STATUS_INVALID_ARGUMENT;
407
408         switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
409         case 0:
410                 return proto_shell_update_link(state, tb);
411         case 1:
412                 return proto_shell_run_command(state, tb);
413         default:
414                 return UBUS_STATUS_INVALID_ARGUMENT;
415         }
416 }
417
418 struct interface_proto_state *
419 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
420                    struct blob_attr *attr)
421 {
422         struct proto_shell_state *state;
423
424         state = calloc(1, sizeof(*state));
425         state->config = malloc(blob_pad_len(attr));
426         if (!state->config)
427                 goto error;
428
429         memcpy(state->config, attr, blob_pad_len(attr));
430         state->proto.free = proto_shell_free;
431         state->proto.notify = proto_shell_notify;
432         state->proto.cb = proto_shell_handler;
433         state->setup_timeout.cb = proto_shell_setup_timeout_cb;
434         state->setup_task.cb = proto_shell_setup_cb;
435         state->teardown_task.cb = proto_shell_teardown_cb;
436         state->proto_task.cb = proto_shell_task_cb;
437         state->handler = container_of(h, struct proto_shell_handler, proto);
438
439         return &state->proto;
440
441 error:
442         free(state);
443         return NULL;
444 }
445
446 static json_object *
447 check_type(json_object *obj, json_type type)
448 {
449         if (!obj)
450                 return NULL;
451
452         if (json_object_get_type(obj) != type)
453                 return NULL;
454
455         return obj;
456 }
457
458 static inline json_object *
459 get_field(json_object *obj, const char *name, json_type type)
460 {
461         return check_type(json_object_object_get(obj, name), type);
462 }
463
464 static char *
465 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
466 {
467         struct blobmsg_policy *attrs;
468         char *str_buf, *str_cur;
469         int str_len = 0;
470         int i;
471
472         config->n_params = json_object_array_length(obj);
473         attrs = calloc(1, sizeof(*attrs) * config->n_params);
474         if (!attrs)
475                 return NULL;
476
477         config->params = attrs;
478         for (i = 0; i < config->n_params; i++) {
479                 json_object *cur, *name, *type;
480
481                 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
482                 if (!cur)
483                         goto error;
484
485                 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
486                 if (!name)
487                         goto error;
488
489                 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
490                 if (!type)
491                         goto error;
492
493                 attrs[i].name = json_object_get_string(name);
494                 attrs[i].type = json_object_get_int(type);
495                 if (attrs[i].type > BLOBMSG_TYPE_LAST)
496                         goto error;
497
498                 str_len += strlen(attrs[i].name) + 1;
499         }
500
501         str_buf = malloc(str_len);
502         if (!str_buf)
503                 goto error;
504
505         str_cur = str_buf;
506         for (i = 0; i < config->n_params; i++) {
507                 const char *name = attrs[i].name;
508
509                 attrs[i].name = str_cur;
510                 str_cur += sprintf(str_cur, "%s", name) + 1;
511         }
512
513         return str_buf;
514
515 error:
516         free(attrs);
517         config->n_params = 0;
518         return NULL;
519 }
520
521 static void
522 proto_shell_add_handler(const char *script, json_object *obj)
523 {
524         struct proto_shell_handler *handler;
525         struct proto_handler *proto;
526         json_object *config, *tmp;
527         const char *name;
528         char *str;
529
530         if (!check_type(obj, json_type_object))
531                 return;
532
533         tmp = get_field(obj, "name", json_type_string);
534         if (!tmp)
535                 return;
536
537         name = json_object_get_string(tmp);
538
539         handler = calloc(1, sizeof(*handler) +
540                          strlen(script) + 1 +
541                          strlen(name) + 1);
542         if (!handler)
543                 return;
544
545         strcpy(handler->script_name, script);
546
547         str = handler->script_name + strlen(handler->script_name) + 1;
548         strcpy(str, name);
549
550         proto = &handler->proto;
551         proto->name = str;
552         proto->config_params = &handler->config;
553         proto->attach = proto_shell_attach;
554
555         tmp = get_field(obj, "no-device", json_type_boolean);
556         if (tmp && json_object_get_boolean(tmp))
557                 handler->proto.flags |= PROTO_FLAG_NODEV;
558
559         config = get_field(obj, "config", json_type_array);
560         if (config)
561                 handler->config_buf = proto_shell_parse_config(&handler->config, config);
562
563         DPRINTF("Add handler for script %s: %s\n", script, proto->name);
564         add_proto_handler(proto);
565 }
566
567 static void proto_shell_add_script(const char *name)
568 {
569         struct json_tokener *tok = NULL;
570         json_object *obj;
571         static char buf[512];
572         char *start, *end, *cmd;
573         FILE *f;
574         int buflen, len;
575
576 #define DUMP_SUFFIX     " '' dump"
577
578         cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
579         sprintf(cmd, "%s" DUMP_SUFFIX, name);
580
581         f = popen(cmd, "r");
582         if (!f)
583                 return;
584
585         do {
586                 buflen = fread(buf, 1, sizeof(buf) - 1, f);
587                 if (buflen <= 0)
588                         continue;
589
590                 start = buf;
591                 len = buflen;
592                 do {
593                         end = memchr(start, '\n', len);
594                         if (end)
595                                 len = end - start;
596
597                         if (!tok)
598                                 tok = json_tokener_new();
599
600                         obj = json_tokener_parse_ex(tok, start, len);
601                         if (!is_error(obj)) {
602                                 proto_shell_add_handler(name, obj);
603                                 json_object_put(obj);
604                                 json_tokener_free(tok);
605                                 tok = NULL;
606                         }
607
608                         if (end) {
609                                 start = end + 1;
610                                 len = buflen - (start - buf);
611                         }
612                 } while (len > 0);
613         } while (!feof(f) && !ferror(f));
614
615         if (tok)
616                 json_tokener_free(tok);
617
618         pclose(f);
619 }
620
621 void __init proto_shell_init(void)
622 {
623         glob_t g;
624         int main_fd;
625         int i;
626
627         main_fd = open(".", O_RDONLY | O_DIRECTORY);
628         if (main_fd < 0)
629                 return;
630
631         if (chdir(main_path)) {
632                 perror("chdir(main path)");
633                 goto close_cur;
634         }
635
636         if (chdir("./proto"))
637                 goto close_cur;
638
639         proto_fd = open(".", O_RDONLY | O_DIRECTORY);
640         if (proto_fd < 0)
641                 goto close_cur;
642
643         glob("./*.sh", 0, NULL, &g);
644         for (i = 0; i < g.gl_pathc; i++)
645                 proto_shell_add_script(g.gl_pathv[i]);
646
647 close_cur:
648         fchdir(main_fd);
649         close(main_fd);
650 }