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