add support for attaching arbitrary state data to interfaces
[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 struct device *
226 proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
227 {
228         struct device *dev;
229         struct blob_buf b;
230
231         memset(&b, 0, sizeof(b));
232         blob_buf_init(&b, 0);
233         blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
234         dev = device_create(name, &tunnel_device_type, blob_data(b.head));
235         blob_buf_free(&b);
236
237         return dev;
238 }
239
240 enum {
241         NOTIFY_ACTION,
242         NOTIFY_ERROR,
243         NOTIFY_COMMAND,
244         NOTIFY_ENV,
245         NOTIFY_SIGNAL,
246         NOTIFY_AVAILABLE,
247         NOTIFY_LINK_UP,
248         NOTIFY_IFNAME,
249         NOTIFY_ADDR_EXT,
250         NOTIFY_ROUTES,
251         NOTIFY_ROUTES6,
252         NOTIFY_TUNNEL,
253         __NOTIFY_LAST
254 };
255
256 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
257         [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
258         [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
259         [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
260         [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
261         [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
262         [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
263         [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
264         [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
265         [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
266         [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
267         [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
268         [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
269 };
270
271 static int
272 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
273 {
274         struct interface *iface = state->proto.iface;
275         struct blob_attr *cur;
276         struct device *dev;
277         const char *devname;
278         int dev_create = 1;
279         bool addr_ext = false;
280         bool up;
281
282         if (!tb[NOTIFY_LINK_UP])
283                 return UBUS_STATUS_INVALID_ARGUMENT;
284
285         up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
286         if (!up) {
287                 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
288                 return 0;
289         }
290
291         if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
292                 addr_ext = blobmsg_get_bool(cur);
293                 if (addr_ext)
294                         dev_create = 2;
295         }
296
297         if (!tb[NOTIFY_IFNAME]) {
298                 if (!iface->main_dev.dev)
299                         return UBUS_STATUS_INVALID_ARGUMENT;
300         } else {
301                 devname = blobmsg_data(tb[NOTIFY_IFNAME]);
302                 if (tb[NOTIFY_TUNNEL]) {
303                         dev = proto_shell_create_tunnel(devname,
304                                 tb[NOTIFY_TUNNEL]);
305                         if (!dev)
306                                 return UBUS_STATUS_INVALID_ARGUMENT;
307                 } else {
308                         dev = device_get(devname, dev_create);
309                         if (!dev)
310                                 return UBUS_STATUS_NOT_FOUND;
311                 }
312
313                 interface_set_l3_dev(iface, dev);
314                 device_claim(&iface->l3_dev);
315         }
316
317         interface_update_start(iface);
318         proto_apply_ip_settings(iface, data, addr_ext);
319
320         if ((cur = tb[NOTIFY_ROUTES]) != NULL)
321                 proto_shell_parse_route_list(state->proto.iface, cur, false);
322
323         if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
324                 proto_shell_parse_route_list(state->proto.iface, cur, true);
325
326         interface_update_complete(state->proto.iface);
327
328         state->proto.proto_event(&state->proto, IFPEV_UP);
329
330         return 0;
331 }
332
333 static bool
334 fill_string_list(struct blob_attr *attr, char **argv, int max)
335 {
336         struct blob_attr *cur;
337         int argc = 0;
338         int rem;
339
340         if (!attr)
341                 goto out;
342
343         blobmsg_for_each_attr(cur, attr, rem) {
344                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
345                         return false;
346
347                 if (!blobmsg_check_attr(cur, NULL))
348                         return false;
349
350                 argv[argc++] = blobmsg_data(cur);
351                 if (argc == max - 1)
352                         return false;
353         }
354
355 out:
356         argv[argc] = NULL;
357         return true;
358 }
359
360 static int
361 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
362 {
363         static char *argv[64];
364         static char *env[32];
365
366         if (!tb[NOTIFY_COMMAND])
367                 goto error;
368
369         if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
370                 goto error;
371
372         if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
373                 goto error;
374
375         netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
376
377         return 0;
378
379 error:
380         return UBUS_STATUS_INVALID_ARGUMENT;
381 }
382
383 static int
384 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
385 {
386         unsigned int signal = ~0;
387
388         if (tb[NOTIFY_SIGNAL])
389                 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
390
391         if (signal > 31)
392                 signal = SIGTERM;
393
394         if (state->proto_task.uloop.pending) {
395                 state->proto_task_killed = true;
396                 kill(state->proto_task.uloop.pid, signal);
397         }
398
399         return 0;
400 }
401
402 static int
403 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
404 {
405         struct blob_attr *cur;
406         char *data[16];
407         int n_data = 0;
408         int rem;
409
410         if (!tb[NOTIFY_ERROR])
411                 return UBUS_STATUS_INVALID_ARGUMENT;
412
413         blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
414                 if (n_data + 1 == ARRAY_SIZE(data))
415                         goto error;
416
417                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
418                         goto error;
419
420                 if (!blobmsg_check_attr(cur, NULL))
421                         goto error;
422
423                 data[n_data++] = blobmsg_data(cur);
424         }
425
426         if (!n_data)
427                 goto error;
428
429         interface_add_error(state->proto.iface, state->handler->proto.name,
430                         data[0], (const char **) &data[1], n_data - 1);
431
432         return 0;
433
434 error:
435         return UBUS_STATUS_INVALID_ARGUMENT;
436 }
437
438 static int
439 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
440 {
441         state->proto.iface->autostart = false;
442         return 0;
443 }
444
445 static int
446 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
447 {
448         if (!tb[NOTIFY_AVAILABLE])
449                 return UBUS_STATUS_INVALID_ARGUMENT;
450
451         interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
452         return 0;
453 }
454
455 static int
456 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
457 {
458         struct proto_shell_state *state;
459         struct blob_attr *tb[__NOTIFY_LAST];
460
461         state = container_of(proto, struct proto_shell_state, proto);
462
463         blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
464         if (!tb[NOTIFY_ACTION])
465                 return UBUS_STATUS_INVALID_ARGUMENT;
466
467         switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
468         case 0:
469                 return proto_shell_update_link(state, attr, tb);
470         case 1:
471                 return proto_shell_run_command(state, tb);
472         case 2:
473                 return proto_shell_kill_command(state, tb);
474         case 3:
475                 return proto_shell_notify_error(state, tb);
476         case 4:
477                 return proto_shell_block_restart(state, tb);
478         case 5:
479                 return proto_shell_set_available(state, tb);
480         default:
481                 return UBUS_STATUS_INVALID_ARGUMENT;
482         }
483 }
484
485 static struct interface_proto_state *
486 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
487                    struct blob_attr *attr)
488 {
489         struct proto_shell_state *state;
490
491         state = calloc(1, sizeof(*state));
492         state->config = malloc(blob_pad_len(attr));
493         if (!state->config)
494                 goto error;
495
496         memcpy(state->config, attr, blob_pad_len(attr));
497         state->proto.free = proto_shell_free;
498         state->proto.notify = proto_shell_notify;
499         state->proto.cb = proto_shell_handler;
500         state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
501         state->script_task.cb = proto_shell_script_cb;
502         state->script_task.dir_fd = proto_fd.fd;
503         state->script_task.log_prefix = iface->name;
504         state->proto_task.cb = proto_shell_task_cb;
505         state->proto_task.dir_fd = proto_fd.fd;
506         state->proto_task.log_prefix = iface->name;
507         state->handler = container_of(h, struct proto_shell_handler, proto);
508
509         return &state->proto;
510
511 error:
512         free(state);
513         return NULL;
514 }
515
516 static json_object *
517 check_type(json_object *obj, json_type type)
518 {
519         if (!obj)
520                 return NULL;
521
522         if (json_object_get_type(obj) != type)
523                 return NULL;
524
525         return obj;
526 }
527
528 static inline json_object *
529 get_field(json_object *obj, const char *name, json_type type)
530 {
531         return check_type(json_object_object_get(obj, name), type);
532 }
533
534 static char *
535 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
536 {
537         struct blobmsg_policy *attrs;
538         char *str_buf, *str_cur;
539         int str_len = 0;
540         int i;
541
542         config->n_params = json_object_array_length(obj);
543         attrs = calloc(1, sizeof(*attrs) * config->n_params);
544         if (!attrs)
545                 return NULL;
546
547         config->params = attrs;
548         for (i = 0; i < config->n_params; i++) {
549                 json_object *cur, *name, *type;
550
551                 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
552                 if (!cur)
553                         goto error;
554
555                 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
556                 if (!name)
557                         goto error;
558
559                 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
560                 if (!type)
561                         goto error;
562
563                 attrs[i].name = json_object_get_string(name);
564                 attrs[i].type = json_object_get_int(type);
565                 if (attrs[i].type > BLOBMSG_TYPE_LAST)
566                         goto error;
567
568                 str_len += strlen(attrs[i].name) + 1;
569         }
570
571         str_buf = malloc(str_len);
572         if (!str_buf)
573                 goto error;
574
575         str_cur = str_buf;
576         for (i = 0; i < config->n_params; i++) {
577                 const char *name = attrs[i].name;
578
579                 attrs[i].name = str_cur;
580                 str_cur += sprintf(str_cur, "%s", name) + 1;
581         }
582
583         return str_buf;
584
585 error:
586         free(attrs);
587         config->n_params = 0;
588         return NULL;
589 }
590
591 static void
592 proto_shell_add_handler(const char *script, json_object *obj)
593 {
594         struct proto_shell_handler *handler;
595         struct proto_handler *proto;
596         json_object *config, *tmp;
597         const char *name;
598         char *str;
599
600         if (!check_type(obj, json_type_object))
601                 return;
602
603         tmp = get_field(obj, "name", json_type_string);
604         if (!tmp)
605                 return;
606
607         name = json_object_get_string(tmp);
608
609         handler = calloc(1, sizeof(*handler) +
610                          strlen(script) + 1 +
611                          strlen(name) + 1);
612         if (!handler)
613                 return;
614
615         strcpy(handler->script_name, script);
616
617         str = handler->script_name + strlen(handler->script_name) + 1;
618         strcpy(str, name);
619
620         proto = &handler->proto;
621         proto->name = str;
622         proto->config_params = &handler->config;
623         proto->attach = proto_shell_attach;
624
625         tmp = get_field(obj, "no-device", json_type_boolean);
626         if (tmp && json_object_get_boolean(tmp))
627                 handler->proto.flags |= PROTO_FLAG_NODEV;
628
629         tmp = get_field(obj, "available", json_type_boolean);
630         if (tmp && json_object_get_boolean(tmp))
631                 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
632
633         config = get_field(obj, "config", json_type_array);
634         if (config)
635                 handler->config_buf = proto_shell_parse_config(&handler->config, config);
636
637         DPRINTF("Add handler for script %s: %s\n", script, proto->name);
638         add_proto_handler(proto);
639 }
640
641 static void proto_shell_add_script(const char *name)
642 {
643         struct json_tokener *tok = NULL;
644         json_object *obj;
645         static char buf[512];
646         char *start, *cmd;
647         FILE *f;
648         int len;
649
650 #define DUMP_SUFFIX     " '' dump"
651
652         cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
653         sprintf(cmd, "%s" DUMP_SUFFIX, name);
654
655         f = popen(cmd, "r");
656         if (!f)
657                 return;
658
659         do {
660                 start = fgets(buf, sizeof(buf), f);
661                 if (!start)
662                         continue;
663
664                 len = strlen(start);
665
666                 if (!tok)
667                         tok = json_tokener_new();
668
669                 obj = json_tokener_parse_ex(tok, start, len);
670                 if (!is_error(obj)) {
671                         proto_shell_add_handler(name, obj);
672                         json_object_put(obj);
673                         json_tokener_free(tok);
674                         tok = NULL;
675                 } else if (start[len - 1] == '\n') {
676                         json_tokener_free(tok);
677                         tok = NULL;
678                 }
679         } while (!feof(f) && !ferror(f));
680
681         if (tok)
682                 json_tokener_free(tok);
683
684         pclose(f);
685 }
686
687 static void __init proto_shell_init(void)
688 {
689         glob_t g;
690         int main_fd;
691         int i;
692
693         main_fd = open(".", O_RDONLY | O_DIRECTORY);
694         if (main_fd < 0)
695                 return;
696
697         if (chdir(main_path)) {
698                 perror("chdir(main path)");
699                 goto close_cur;
700         }
701
702         if (chdir("./proto"))
703                 goto close_cur;
704
705         proto_fd.fd = open(".", O_RDONLY | O_DIRECTORY);
706         if (proto_fd.fd < 0)
707                 goto close_cur;
708
709         netifd_fd_add(&proto_fd);
710         glob("./*.sh", 0, NULL, &g);
711         for (i = 0; i < g.gl_pathc; i++)
712                 proto_shell_add_script(g.gl_pathv[i]);
713
714 close_cur:
715         fchdir(main_fd);
716         close(main_fd);
717 }