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