add copyright headers
[project/netifd.git] / proto-shell.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14 #define _GNU_SOURCE
15
16 #include <string.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <glob.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <signal.h>
23
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
26
27 #include <libubox/blobmsg_json.h>
28
29 #include "netifd.h"
30 #include "interface.h"
31 #include "interface-ip.h"
32 #include "proto.h"
33
34 static struct netifd_fd proto_fd;
35
36 enum proto_shell_sm {
37 S_IDLE,
38 S_SETUP,
39 S_SETUP_ABORT,
40 S_TEARDOWN,
41 };
42
43 struct proto_shell_handler {
44 struct list_head list;
45 struct proto_handler proto;
46 struct config_param_list config;
47 char *config_buf;
48 bool init_available;
49 char script_name[];
50 };
51
52 struct proto_shell_state {
53 struct interface_proto_state proto;
54 struct proto_shell_handler *handler;
55 struct blob_attr *config;
56
57 struct uloop_timeout teardown_timeout;
58
59 struct netifd_process script_task;
60 struct netifd_process proto_task;
61
62 enum proto_shell_sm sm;
63 bool proto_task_killed;
64
65 int last_error;
66 };
67
68 static int
69 proto_shell_handler(struct interface_proto_state *proto,
70 enum interface_proto_cmd cmd, bool force)
71 {
72 struct proto_shell_state *state;
73 struct proto_shell_handler *handler;
74 struct netifd_process *proc;
75 static char error_buf[32];
76 const char *argv[7];
77 char *envp[2];
78 const char *action;
79 char *config;
80 int ret, i = 0, j = 0;
81
82 state = container_of(proto, struct proto_shell_state, proto);
83 handler = state->handler;
84 proc = &state->script_task;
85
86 if (cmd == PROTO_CMD_SETUP) {
87 action = "setup";
88 state->last_error = -1;
89 } else {
90 if (state->sm == S_TEARDOWN)
91 return 0;
92
93 if (state->script_task.uloop.pending) {
94 if (state->sm != S_SETUP_ABORT) {
95 uloop_timeout_set(&state->teardown_timeout, 1000);
96 kill(state->script_task.uloop.pid, SIGTERM);
97 if (state->proto_task.uloop.pending)
98 kill(state->proto_task.uloop.pid, SIGTERM);
99 state->sm = S_SETUP_ABORT;
100 }
101 return 0;
102 }
103
104 action = "teardown";
105 state->sm = S_TEARDOWN;
106 if (state->last_error >= 0) {
107 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
108 envp[j++] = error_buf;
109 }
110 uloop_timeout_set(&state->teardown_timeout, 5000);
111 }
112
113 config = blobmsg_format_json(state->config, true);
114 if (!config)
115 return -1;
116
117 argv[i++] = handler->script_name;
118 argv[i++] = handler->proto.name;
119 argv[i++] = action;
120 argv[i++] = proto->iface->name;
121 argv[i++] = config;
122 if (proto->iface->main_dev.dev)
123 argv[i++] = proto->iface->main_dev.dev->ifname;
124 argv[i] = NULL;
125 envp[j] = NULL;
126
127 ret = netifd_start_process(argv, envp, proc);
128 free(config);
129
130 return ret;
131 }
132
133 static void
134 proto_shell_task_finish(struct proto_shell_state *state,
135 struct netifd_process *task)
136 {
137 switch (state->sm) {
138 case S_IDLE:
139 if (task == &state->proto_task)
140 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
141 /* fall through */
142 case S_SETUP:
143 if (task == &state->proto_task)
144 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN,
145 false);
146 break;
147
148 case S_SETUP_ABORT:
149 if (state->script_task.uloop.pending ||
150 state->proto_task.uloop.pending)
151 break;
152
153 uloop_timeout_cancel(&state->teardown_timeout);
154 state->sm = S_IDLE;
155 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
156 break;
157
158 case S_TEARDOWN:
159 if (state->script_task.uloop.pending)
160 break;
161
162 if (state->proto_task.uloop.pending) {
163 if (!state->proto_task_killed)
164 kill(state->proto_task.uloop.pid, SIGTERM);
165 break;
166 }
167
168 uloop_timeout_cancel(&state->teardown_timeout);
169 state->sm = S_IDLE;
170 state->proto.proto_event(&state->proto, IFPEV_DOWN);
171 break;
172 }
173 }
174
175 static void
176 proto_shell_teardown_timeout_cb(struct uloop_timeout *timeout)
177 {
178 struct proto_shell_state *state;
179
180 state = container_of(timeout, struct proto_shell_state, teardown_timeout);
181
182 netifd_kill_process(&state->script_task);
183 netifd_kill_process(&state->proto_task);
184 proto_shell_task_finish(state, NULL);
185 }
186
187 static void
188 proto_shell_script_cb(struct netifd_process *p, int ret)
189 {
190 struct proto_shell_state *state;
191
192 state = container_of(p, struct proto_shell_state, script_task);
193 proto_shell_task_finish(state, p);
194 }
195
196 static void
197 proto_shell_task_cb(struct netifd_process *p, int ret)
198 {
199 struct proto_shell_state *state;
200
201 state = container_of(p, struct proto_shell_state, proto_task);
202
203 if (state->sm == S_IDLE || state->sm == S_SETUP)
204 state->last_error = WEXITSTATUS(ret);
205
206 proto_shell_task_finish(state, p);
207 }
208
209 static void
210 proto_shell_free(struct interface_proto_state *proto)
211 {
212 struct proto_shell_state *state;
213
214 state = container_of(proto, struct proto_shell_state, proto);
215 netifd_kill_process(&state->script_task);
216 netifd_kill_process(&state->proto_task);
217 free(state->config);
218 free(state);
219 }
220
221 static void
222 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
223 bool v6)
224 {
225 struct blob_attr *cur;
226 int rem;
227
228 blobmsg_for_each_attr(cur, attr, rem) {
229 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
230 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
231 continue;
232 }
233
234 interface_ip_add_route(iface, cur, v6);
235 }
236 }
237
238 static void
239 proto_shell_parse_data(struct interface *iface, struct blob_attr *attr)
240 {
241 struct blob_attr *cur;
242 int rem;
243
244 blobmsg_for_each_attr(cur, attr, rem)
245 interface_add_data(iface, cur);
246 }
247
248 static struct device *
249 proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
250 {
251 struct device *dev;
252 struct blob_buf b;
253
254 memset(&b, 0, sizeof(b));
255 blob_buf_init(&b, 0);
256 blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
257 dev = device_create(name, &tunnel_device_type, blob_data(b.head));
258 blob_buf_free(&b);
259
260 return dev;
261 }
262
263 enum {
264 NOTIFY_ACTION,
265 NOTIFY_ERROR,
266 NOTIFY_COMMAND,
267 NOTIFY_ENV,
268 NOTIFY_SIGNAL,
269 NOTIFY_AVAILABLE,
270 NOTIFY_LINK_UP,
271 NOTIFY_IFNAME,
272 NOTIFY_ADDR_EXT,
273 NOTIFY_ROUTES,
274 NOTIFY_ROUTES6,
275 NOTIFY_TUNNEL,
276 NOTIFY_DATA,
277 NOTIFY_KEEP,
278 __NOTIFY_LAST
279 };
280
281 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
282 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
283 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
284 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
285 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
286 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
287 [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
288 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
289 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
290 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
291 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
292 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
293 [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
294 [NOTIFY_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
295 [NOTIFY_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
296 };
297
298 static int
299 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
300 {
301 struct interface *iface = state->proto.iface;
302 struct blob_attr *cur;
303 struct device *dev;
304 const char *devname;
305 int dev_create = 1;
306 bool addr_ext = false;
307 bool keep = false;
308 bool up;
309
310 if (!tb[NOTIFY_LINK_UP])
311 return UBUS_STATUS_INVALID_ARGUMENT;
312
313 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
314 if (!up) {
315 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
316 return 0;
317 }
318
319 if ((cur = tb[NOTIFY_KEEP]) != NULL)
320 keep = blobmsg_get_bool(cur);
321
322 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
323 addr_ext = blobmsg_get_bool(cur);
324 if (addr_ext)
325 dev_create = 2;
326 }
327
328 if (!tb[NOTIFY_IFNAME]) {
329 if (!iface->main_dev.dev)
330 return UBUS_STATUS_INVALID_ARGUMENT;
331 } else if (!keep || iface->state != IFS_UP) {
332 keep = false;
333 devname = blobmsg_data(tb[NOTIFY_IFNAME]);
334 if (tb[NOTIFY_TUNNEL]) {
335 dev = proto_shell_create_tunnel(devname,
336 tb[NOTIFY_TUNNEL]);
337 if (!dev)
338 return UBUS_STATUS_INVALID_ARGUMENT;
339 } else {
340 dev = device_get(devname, dev_create);
341 if (!dev)
342 return UBUS_STATUS_NOT_FOUND;
343 }
344
345 interface_set_l3_dev(iface, dev);
346 device_claim(&iface->l3_dev);
347 }
348
349 if (!keep)
350 interface_update_start(iface);
351
352 proto_apply_ip_settings(iface, data, addr_ext);
353
354 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
355 proto_shell_parse_route_list(state->proto.iface, cur, false);
356
357 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
358 proto_shell_parse_route_list(state->proto.iface, cur, true);
359
360 interface_update_complete(state->proto.iface);
361
362 if (!keep)
363 state->proto.proto_event(&state->proto, IFPEV_UP);
364 state->sm = S_IDLE;
365
366 if ((cur = tb[NOTIFY_DATA]))
367 proto_shell_parse_data(state->proto.iface, cur);
368
369 return 0;
370 }
371
372 static bool
373 fill_string_list(struct blob_attr *attr, char **argv, int max)
374 {
375 struct blob_attr *cur;
376 int argc = 0;
377 int rem;
378
379 if (!attr)
380 goto out;
381
382 blobmsg_for_each_attr(cur, attr, rem) {
383 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
384 return false;
385
386 if (!blobmsg_check_attr(cur, NULL))
387 return false;
388
389 argv[argc++] = blobmsg_data(cur);
390 if (argc == max - 1)
391 return false;
392 }
393
394 out:
395 argv[argc] = NULL;
396 return true;
397 }
398
399 static int
400 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
401 {
402 static char *argv[64];
403 static char *env[32];
404
405 if (!tb[NOTIFY_COMMAND])
406 goto error;
407
408 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
409 goto error;
410
411 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
412 goto error;
413
414 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
415
416 return 0;
417
418 error:
419 return UBUS_STATUS_INVALID_ARGUMENT;
420 }
421
422 static int
423 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
424 {
425 unsigned int signal = ~0;
426
427 if (tb[NOTIFY_SIGNAL])
428 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
429
430 if (signal > 31)
431 signal = SIGTERM;
432
433 if (state->proto_task.uloop.pending) {
434 state->proto_task_killed = true;
435 kill(state->proto_task.uloop.pid, signal);
436 }
437
438 return 0;
439 }
440
441 static int
442 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
443 {
444 struct blob_attr *cur;
445 char *data[16];
446 int n_data = 0;
447 int rem;
448
449 if (!tb[NOTIFY_ERROR])
450 return UBUS_STATUS_INVALID_ARGUMENT;
451
452 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
453 if (n_data + 1 == ARRAY_SIZE(data))
454 goto error;
455
456 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
457 goto error;
458
459 if (!blobmsg_check_attr(cur, NULL))
460 goto error;
461
462 data[n_data++] = blobmsg_data(cur);
463 }
464
465 if (!n_data)
466 goto error;
467
468 interface_add_error(state->proto.iface, state->handler->proto.name,
469 data[0], (const char **) &data[1], n_data - 1);
470
471 return 0;
472
473 error:
474 return UBUS_STATUS_INVALID_ARGUMENT;
475 }
476
477 static int
478 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
479 {
480 state->proto.iface->autostart = false;
481 return 0;
482 }
483
484 static int
485 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
486 {
487 if (!tb[NOTIFY_AVAILABLE])
488 return UBUS_STATUS_INVALID_ARGUMENT;
489
490 interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
491 return 0;
492 }
493
494 static int
495 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
496 {
497 struct proto_shell_state *state;
498 struct blob_attr *tb[__NOTIFY_LAST];
499
500 state = container_of(proto, struct proto_shell_state, proto);
501
502 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
503 if (!tb[NOTIFY_ACTION])
504 return UBUS_STATUS_INVALID_ARGUMENT;
505
506 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
507 case 0:
508 return proto_shell_update_link(state, attr, tb);
509 case 1:
510 return proto_shell_run_command(state, tb);
511 case 2:
512 return proto_shell_kill_command(state, tb);
513 case 3:
514 return proto_shell_notify_error(state, tb);
515 case 4:
516 return proto_shell_block_restart(state, tb);
517 case 5:
518 return proto_shell_set_available(state, tb);
519 default:
520 return UBUS_STATUS_INVALID_ARGUMENT;
521 }
522 }
523
524 static struct interface_proto_state *
525 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
526 struct blob_attr *attr)
527 {
528 struct proto_shell_state *state;
529
530 state = calloc(1, sizeof(*state));
531 state->config = malloc(blob_pad_len(attr));
532 if (!state->config)
533 goto error;
534
535 memcpy(state->config, attr, blob_pad_len(attr));
536 state->proto.free = proto_shell_free;
537 state->proto.notify = proto_shell_notify;
538 state->proto.cb = proto_shell_handler;
539 state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
540 state->script_task.cb = proto_shell_script_cb;
541 state->script_task.dir_fd = proto_fd.fd;
542 state->script_task.log_prefix = iface->name;
543 state->proto_task.cb = proto_shell_task_cb;
544 state->proto_task.dir_fd = proto_fd.fd;
545 state->proto_task.log_prefix = iface->name;
546 state->handler = container_of(h, struct proto_shell_handler, proto);
547
548 return &state->proto;
549
550 error:
551 free(state);
552 return NULL;
553 }
554
555 static json_object *
556 check_type(json_object *obj, json_type type)
557 {
558 if (!obj)
559 return NULL;
560
561 if (json_object_get_type(obj) != type)
562 return NULL;
563
564 return obj;
565 }
566
567 static inline json_object *
568 get_field(json_object *obj, const char *name, json_type type)
569 {
570 return check_type(json_object_object_get(obj, name), type);
571 }
572
573 static char *
574 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
575 {
576 struct blobmsg_policy *attrs;
577 char *str_buf, *str_cur;
578 int str_len = 0;
579 int i;
580
581 config->n_params = json_object_array_length(obj);
582 attrs = calloc(1, sizeof(*attrs) * config->n_params);
583 if (!attrs)
584 return NULL;
585
586 config->params = attrs;
587 for (i = 0; i < config->n_params; i++) {
588 json_object *cur, *name, *type;
589
590 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
591 if (!cur)
592 goto error;
593
594 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
595 if (!name)
596 goto error;
597
598 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
599 if (!type)
600 goto error;
601
602 attrs[i].name = json_object_get_string(name);
603 attrs[i].type = json_object_get_int(type);
604 if (attrs[i].type > BLOBMSG_TYPE_LAST)
605 goto error;
606
607 str_len += strlen(attrs[i].name) + 1;
608 }
609
610 str_buf = malloc(str_len);
611 if (!str_buf)
612 goto error;
613
614 str_cur = str_buf;
615 for (i = 0; i < config->n_params; i++) {
616 const char *name = attrs[i].name;
617
618 attrs[i].name = str_cur;
619 str_cur += sprintf(str_cur, "%s", name) + 1;
620 }
621
622 return str_buf;
623
624 error:
625 free(attrs);
626 config->n_params = 0;
627 return NULL;
628 }
629
630 static void
631 proto_shell_add_handler(const char *script, json_object *obj)
632 {
633 struct proto_shell_handler *handler;
634 struct proto_handler *proto;
635 json_object *config, *tmp;
636 const char *name;
637 char *str;
638
639 if (!check_type(obj, json_type_object))
640 return;
641
642 tmp = get_field(obj, "name", json_type_string);
643 if (!tmp)
644 return;
645
646 name = json_object_get_string(tmp);
647
648 handler = calloc(1, sizeof(*handler) +
649 strlen(script) + 1 +
650 strlen(name) + 1);
651 if (!handler)
652 return;
653
654 strcpy(handler->script_name, script);
655
656 str = handler->script_name + strlen(handler->script_name) + 1;
657 strcpy(str, name);
658
659 proto = &handler->proto;
660 proto->name = str;
661 proto->config_params = &handler->config;
662 proto->attach = proto_shell_attach;
663
664 tmp = get_field(obj, "no-device", json_type_boolean);
665 if (tmp && json_object_get_boolean(tmp))
666 handler->proto.flags |= PROTO_FLAG_NODEV;
667
668 tmp = get_field(obj, "available", json_type_boolean);
669 if (tmp && json_object_get_boolean(tmp))
670 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
671
672 config = get_field(obj, "config", json_type_array);
673 if (config)
674 handler->config_buf = proto_shell_parse_config(&handler->config, config);
675
676 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
677 add_proto_handler(proto);
678 }
679
680 static void proto_shell_add_script(const char *name)
681 {
682 struct json_tokener *tok = NULL;
683 json_object *obj;
684 static char buf[512];
685 char *start, *cmd;
686 FILE *f;
687 int len;
688
689 #define DUMP_SUFFIX " '' dump"
690
691 cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
692 sprintf(cmd, "%s" DUMP_SUFFIX, name);
693
694 f = popen(cmd, "r");
695 if (!f)
696 return;
697
698 do {
699 start = fgets(buf, sizeof(buf), f);
700 if (!start)
701 continue;
702
703 len = strlen(start);
704
705 if (!tok)
706 tok = json_tokener_new();
707
708 obj = json_tokener_parse_ex(tok, start, len);
709 if (!is_error(obj)) {
710 proto_shell_add_handler(name, obj);
711 json_object_put(obj);
712 json_tokener_free(tok);
713 tok = NULL;
714 } else if (start[len - 1] == '\n') {
715 json_tokener_free(tok);
716 tok = NULL;
717 }
718 } while (!feof(f) && !ferror(f));
719
720 if (tok)
721 json_tokener_free(tok);
722
723 pclose(f);
724 }
725
726 static void __init proto_shell_init(void)
727 {
728 glob_t g;
729 int main_fd;
730 int i;
731
732 main_fd = open(".", O_RDONLY | O_DIRECTORY);
733 if (main_fd < 0)
734 return;
735
736 if (chdir(main_path)) {
737 perror("chdir(main path)");
738 goto close_cur;
739 }
740
741 if (chdir("./proto"))
742 goto close_cur;
743
744 proto_fd.fd = open(".", O_RDONLY | O_DIRECTORY);
745 if (proto_fd.fd < 0)
746 goto close_cur;
747
748 netifd_fd_add(&proto_fd);
749 glob("./*.sh", 0, NULL, &g);
750 for (i = 0; i < g.gl_pathc; i++)
751 proto_shell_add_script(g.gl_pathv[i]);
752
753 close_cur:
754 fchdir(main_fd);
755 close(main_fd);
756 }