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