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