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