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