device: Resolve ifindex for external claimed devices
[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 <signal.h>
20
21 #include <arpa/inet.h>
22 #include <netinet/in.h>
23
24
25 #include "netifd.h"
26 #include "interface.h"
27 #include "interface-ip.h"
28 #include "proto.h"
29 #include "system.h"
30 #include "handler.h"
31
32 static int proto_fd = -1;
33
34 enum proto_shell_sm {
35 S_IDLE,
36 S_SETUP,
37 S_SETUP_ABORT,
38 S_TEARDOWN,
39 };
40
41 struct proto_shell_handler {
42 struct list_head list;
43 struct proto_handler proto;
44 char *config_buf;
45 char *script_name;
46 bool init_available;
47 bool no_proto_task;
48
49 struct uci_blob_param_list config;
50 };
51
52 struct proto_shell_dependency {
53 struct list_head list;
54
55 struct proto_shell_state *proto;
56 struct interface_user dep;
57
58 union if_addr host;
59 bool v6;
60 bool any;
61
62 char interface[];
63 };
64
65 struct proto_shell_state {
66 struct interface_proto_state proto;
67 struct proto_shell_handler *handler;
68 struct blob_attr *config;
69
70 struct uloop_timeout teardown_timeout;
71
72 /*
73 * Teardown and setup interface again if it is still not up (IFS_UP)
74 * after checkup_interval seconds since previous attempt. This check
75 * will be disabled when the config option "checkup_interval" is
76 * missing or has a negative value
77 */
78 int checkup_interval;
79 struct uloop_timeout checkup_timeout;
80
81 struct netifd_process script_task;
82 struct netifd_process proto_task;
83
84 enum proto_shell_sm sm;
85 bool proto_task_killed;
86 bool renew_pending;
87
88 int last_error;
89
90 struct list_head deps;
91 };
92
93 static void
94 proto_shell_check_dependencies(struct proto_shell_state *state)
95 {
96 struct proto_shell_dependency *dep;
97 bool available = true;
98
99 list_for_each_entry(dep, &state->deps, list) {
100 if (dep->dep.iface)
101 continue;
102
103 available = false;
104 break;
105 }
106
107 interface_set_available(state->proto.iface, available);
108 }
109
110 static void
111 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
112 enum interface_event ev);
113 static void
114 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
115 enum interface_event ev);
116
117 static void
118 proto_shell_update_host_dep(struct proto_shell_dependency *dep)
119 {
120 struct interface *iface = NULL;
121
122 if (dep->dep.iface)
123 goto out;
124
125 if (dep->interface[0]) {
126 iface = vlist_find(&interfaces, dep->interface, iface, node);
127
128 if (!iface || iface->state != IFS_UP)
129 goto out;
130 }
131
132 if (!dep->any)
133 iface = interface_ip_add_target_route(&dep->host, dep->v6, iface);
134
135 if (!iface)
136 goto out;
137
138 interface_remove_user(&dep->dep);
139 dep->dep.cb = proto_shell_if_down_cb;
140 interface_add_user(&dep->dep, iface);
141
142 out:
143 proto_shell_check_dependencies(dep->proto);
144 }
145
146 static void
147 proto_shell_clear_host_dep(struct proto_shell_state *state)
148 {
149 struct proto_shell_dependency *dep, *tmp;
150
151 list_for_each_entry_safe(dep, tmp, &state->deps, list) {
152 interface_remove_user(&dep->dep);
153 list_del(&dep->list);
154 free(dep);
155 }
156 }
157
158 static int
159 proto_shell_handler(struct interface_proto_state *proto,
160 enum interface_proto_cmd cmd, bool force)
161 {
162 struct proto_shell_state *state;
163 struct proto_shell_handler *handler;
164 struct netifd_process *proc;
165 static char error_buf[32];
166 const char *argv[7];
167 char *envp[2];
168 const char *action;
169 char *config;
170 int ret, i = 0, j = 0;
171
172 state = container_of(proto, struct proto_shell_state, proto);
173 handler = state->handler;
174 proc = &state->script_task;
175
176 if (cmd == PROTO_CMD_SETUP) {
177 switch (state->sm) {
178 case S_IDLE:
179 action = "setup";
180 state->last_error = -1;
181 proto_shell_clear_host_dep(state);
182 state->sm = S_SETUP;
183 break;
184
185 case S_SETUP_ABORT:
186 case S_TEARDOWN:
187 case S_SETUP:
188 return 0;
189
190 default:
191 return -1;
192 }
193 } else if (cmd == PROTO_CMD_RENEW) {
194 if (!(handler->proto.flags & PROTO_FLAG_RENEW_AVAILABLE))
195 return 0;
196
197 if (state->script_task.uloop.pending) {
198 state->renew_pending = true;
199 return 0;
200 }
201
202 state->renew_pending = false;
203 action = "renew";
204 } else {
205 switch (state->sm) {
206 case S_SETUP:
207 if (state->script_task.uloop.pending) {
208 uloop_timeout_set(&state->teardown_timeout, 1000);
209 kill(state->script_task.uloop.pid, SIGTERM);
210 if (state->proto_task.uloop.pending)
211 kill(state->proto_task.uloop.pid, SIGTERM);
212 state->renew_pending = false;
213 state->sm = S_SETUP_ABORT;
214 return 0;
215 }
216 /* fall through if no script task is running */
217 case S_IDLE:
218 action = "teardown";
219 state->renew_pending = false;
220 state->sm = S_TEARDOWN;
221 if (state->last_error >= 0) {
222 snprintf(error_buf, sizeof(error_buf), "ERROR=%d", state->last_error);
223 envp[j++] = error_buf;
224 }
225 uloop_timeout_set(&state->teardown_timeout, 5000);
226 break;
227
228 case S_TEARDOWN:
229 return 0;
230
231 default:
232 return -1;
233 }
234 }
235
236 D(INTERFACE, "run %s for interface '%s'\n", action, proto->iface->name);
237 config = blobmsg_format_json(state->config, true);
238 if (!config)
239 return -1;
240
241 argv[i++] = handler->script_name;
242 argv[i++] = handler->proto.name;
243 argv[i++] = action;
244 argv[i++] = proto->iface->name;
245 argv[i++] = config;
246 if (proto->iface->main_dev.dev)
247 argv[i++] = proto->iface->main_dev.dev->ifname;
248 argv[i] = NULL;
249 envp[j] = NULL;
250
251 ret = netifd_start_process(argv, envp, proc);
252 free(config);
253
254 return ret;
255 }
256
257 static void
258 proto_shell_if_up_cb(struct interface_user *dep, struct interface *iface,
259 enum interface_event ev)
260 {
261 struct proto_shell_dependency *pdep;
262
263 if (ev != IFEV_UP && ev != IFEV_UPDATE)
264 return;
265
266 pdep = container_of(dep, struct proto_shell_dependency, dep);
267 proto_shell_update_host_dep(pdep);
268 }
269
270 static void
271 proto_shell_if_down_cb(struct interface_user *dep, struct interface *iface,
272 enum interface_event ev)
273 {
274 struct proto_shell_dependency *pdep;
275 struct proto_shell_state *state;
276
277 if (ev == IFEV_UP || ev == IFEV_UPDATE)
278 return;
279
280 pdep = container_of(dep, struct proto_shell_dependency, dep);
281 interface_remove_user(dep);
282 dep->cb = proto_shell_if_up_cb;
283 interface_add_user(dep, NULL);
284
285 state = pdep->proto;
286 if (state->sm == S_IDLE) {
287 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
288 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
289 }
290 }
291
292 static void
293 proto_shell_task_finish(struct proto_shell_state *state,
294 struct netifd_process *task)
295 {
296 switch (state->sm) {
297 case S_IDLE:
298 if (task == &state->proto_task)
299 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
300 /* fall through */
301 case S_SETUP:
302 if (task == &state->proto_task)
303 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN,
304 false);
305 else if (task == &state->script_task) {
306 if (state->renew_pending)
307 proto_shell_handler(&state->proto,
308 PROTO_CMD_RENEW, false);
309 else if (!state->handler->no_proto_task &&
310 !state->proto_task.uloop.pending &&
311 state->sm == S_SETUP)
312 proto_shell_handler(&state->proto,
313 PROTO_CMD_TEARDOWN,
314 false);
315
316 /* check up status after setup attempt by this script_task */
317 if (state->sm == S_SETUP && state->checkup_interval > 0) {
318 uloop_timeout_set(&state->checkup_timeout,
319 state->checkup_interval * 1000);
320 }
321 }
322 break;
323
324 case S_SETUP_ABORT:
325 if (state->script_task.uloop.pending ||
326 state->proto_task.uloop.pending)
327 break;
328
329 /* completed aborting all tasks, now idle */
330 uloop_timeout_cancel(&state->teardown_timeout);
331 uloop_timeout_cancel(&state->checkup_timeout);
332 state->sm = S_IDLE;
333 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
334 break;
335
336 case S_TEARDOWN:
337 if (state->script_task.uloop.pending)
338 break;
339
340 if (state->proto_task.uloop.pending) {
341 if (!state->proto_task_killed)
342 kill(state->proto_task.uloop.pid, SIGTERM);
343 break;
344 }
345
346 /* completed tearing down all tasks, now idle */
347 uloop_timeout_cancel(&state->teardown_timeout);
348 uloop_timeout_cancel(&state->checkup_timeout);
349 state->sm = S_IDLE;
350 state->proto.proto_event(&state->proto, IFPEV_DOWN);
351 break;
352 }
353 }
354
355 static void
356 proto_shell_teardown_timeout_cb(struct uloop_timeout *timeout)
357 {
358 struct proto_shell_state *state;
359
360 state = container_of(timeout, struct proto_shell_state, teardown_timeout);
361
362 netifd_kill_process(&state->script_task);
363 netifd_kill_process(&state->proto_task);
364 proto_shell_task_finish(state, NULL);
365 }
366
367 static void
368 proto_shell_script_cb(struct netifd_process *p, int ret)
369 {
370 struct proto_shell_state *state;
371
372 state = container_of(p, struct proto_shell_state, script_task);
373 proto_shell_task_finish(state, p);
374 }
375
376 static void
377 proto_shell_task_cb(struct netifd_process *p, int ret)
378 {
379 struct proto_shell_state *state;
380
381 state = container_of(p, struct proto_shell_state, proto_task);
382
383 if (state->sm == S_IDLE || state->sm == S_SETUP)
384 state->last_error = WEXITSTATUS(ret);
385
386 proto_shell_task_finish(state, p);
387 }
388
389 static void
390 proto_shell_free(struct interface_proto_state *proto)
391 {
392 struct proto_shell_state *state;
393
394 state = container_of(proto, struct proto_shell_state, proto);
395 uloop_timeout_cancel(&state->teardown_timeout);
396 uloop_timeout_cancel(&state->checkup_timeout);
397 proto_shell_clear_host_dep(state);
398 netifd_kill_process(&state->script_task);
399 netifd_kill_process(&state->proto_task);
400 free(state->config);
401 free(state);
402 }
403
404 static void
405 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
406 bool v6)
407 {
408 struct blob_attr *cur;
409 int rem;
410
411 blobmsg_for_each_attr(cur, attr, rem) {
412 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
413 DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
414 continue;
415 }
416
417 interface_ip_add_route(iface, cur, v6);
418 }
419 }
420
421 static void
422 proto_shell_parse_data(struct interface *iface, struct blob_attr *attr)
423 {
424 struct blob_attr *cur;
425 int rem;
426
427 blobmsg_for_each_attr(cur, attr, rem)
428 interface_add_data(iface, cur);
429 }
430
431 static struct device *
432 proto_shell_create_tunnel(const char *name, struct blob_attr *attr)
433 {
434 struct device *dev;
435 struct blob_buf b;
436
437 memset(&b, 0, sizeof(b));
438 blob_buf_init(&b, 0);
439 blob_put(&b, 0, blobmsg_data(attr), blobmsg_data_len(attr));
440 dev = device_create(name, &tunnel_device_type, blob_data(b.head));
441 blob_buf_free(&b);
442
443 return dev;
444 }
445
446 enum {
447 NOTIFY_ACTION,
448 NOTIFY_ERROR,
449 NOTIFY_COMMAND,
450 NOTIFY_ENV,
451 NOTIFY_SIGNAL,
452 NOTIFY_AVAILABLE,
453 NOTIFY_LINK_UP,
454 NOTIFY_IFNAME,
455 NOTIFY_ADDR_EXT,
456 NOTIFY_ROUTES,
457 NOTIFY_ROUTES6,
458 NOTIFY_TUNNEL,
459 NOTIFY_DATA,
460 NOTIFY_KEEP,
461 NOTIFY_HOST,
462 NOTIFY_DNS,
463 NOTIFY_DNS_SEARCH,
464 __NOTIFY_LAST
465 };
466
467 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
468 [NOTIFY_ACTION] = { .name = "action", .type = BLOBMSG_TYPE_INT32 },
469 [NOTIFY_ERROR] = { .name = "error", .type = BLOBMSG_TYPE_ARRAY },
470 [NOTIFY_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_ARRAY },
471 [NOTIFY_ENV] = { .name = "env", .type = BLOBMSG_TYPE_ARRAY },
472 [NOTIFY_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
473 [NOTIFY_AVAILABLE] = { .name = "available", .type = BLOBMSG_TYPE_BOOL },
474 [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
475 [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
476 [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
477 [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
478 [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
479 [NOTIFY_TUNNEL] = { .name = "tunnel", .type = BLOBMSG_TYPE_TABLE },
480 [NOTIFY_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
481 [NOTIFY_KEEP] = { .name = "keep", .type = BLOBMSG_TYPE_BOOL },
482 [NOTIFY_HOST] = { .name = "host", .type = BLOBMSG_TYPE_STRING },
483 [NOTIFY_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
484 [NOTIFY_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
485 };
486
487 static int
488 proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data, struct blob_attr **tb)
489 {
490 struct interface *iface = state->proto.iface;
491 struct blob_attr *cur;
492 struct device *dev;
493 const char *devname;
494 int dev_create = 1;
495 bool addr_ext = false;
496 bool keep = false;
497 bool up;
498
499 if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
500 return UBUS_STATUS_PERMISSION_DENIED;
501
502 if (!tb[NOTIFY_LINK_UP])
503 return UBUS_STATUS_INVALID_ARGUMENT;
504
505 up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
506 if (!up) {
507 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
508 return 0;
509 }
510
511 if ((cur = tb[NOTIFY_KEEP]) != NULL)
512 keep = blobmsg_get_bool(cur);
513
514 if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL) {
515 addr_ext = blobmsg_get_bool(cur);
516 if (addr_ext)
517 dev_create = 2;
518 }
519
520 if (iface->state != IFS_UP || !iface->l3_dev.dev)
521 keep = false;
522
523 if (!keep) {
524 dev = iface->main_dev.dev;
525 if (tb[NOTIFY_IFNAME]) {
526 keep = false;
527 devname = blobmsg_data(tb[NOTIFY_IFNAME]);
528 if (tb[NOTIFY_TUNNEL])
529 dev = proto_shell_create_tunnel(devname, tb[NOTIFY_TUNNEL]);
530 else
531 dev = device_get(devname, dev_create);
532 }
533
534 if (!dev)
535 return UBUS_STATUS_INVALID_ARGUMENT;
536
537 interface_set_l3_dev(iface, dev);
538 if (device_claim(&iface->l3_dev) < 0)
539 return UBUS_STATUS_UNKNOWN_ERROR;
540
541 device_set_present(dev, true);
542
543 interface_update_start(iface);
544 }
545
546 proto_apply_ip_settings(iface, data, addr_ext);
547
548 if ((cur = tb[NOTIFY_ROUTES]) != NULL)
549 proto_shell_parse_route_list(state->proto.iface, cur, false);
550
551 if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
552 proto_shell_parse_route_list(state->proto.iface, cur, true);
553
554 if ((cur = tb[NOTIFY_DNS]))
555 interface_add_dns_server_list(&iface->proto_ip, cur);
556
557 if ((cur = tb[NOTIFY_DNS_SEARCH]))
558 interface_add_dns_search_list(&iface->proto_ip, cur);
559
560 if ((cur = tb[NOTIFY_DATA]))
561 proto_shell_parse_data(state->proto.iface, cur);
562
563 interface_update_complete(state->proto.iface);
564
565 if ((state->sm != S_SETUP_ABORT) && (state->sm != S_TEARDOWN)) {
566 if (!keep)
567 state->proto.proto_event(&state->proto, IFPEV_UP);
568 state->sm = S_IDLE;
569 }
570
571 return 0;
572 }
573
574 static bool
575 fill_string_list(struct blob_attr *attr, char **argv, int max)
576 {
577 struct blob_attr *cur;
578 int argc = 0;
579 int rem;
580
581 if (!attr)
582 goto out;
583
584 blobmsg_for_each_attr(cur, attr, rem) {
585 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
586 return false;
587
588 if (!blobmsg_check_attr(cur, NULL))
589 return false;
590
591 argv[argc++] = blobmsg_data(cur);
592 if (argc == max - 1)
593 return false;
594 }
595
596 out:
597 argv[argc] = NULL;
598 return true;
599 }
600
601 static int
602 proto_shell_run_command(struct proto_shell_state *state, struct blob_attr **tb)
603 {
604 static char *argv[64];
605 static char *env[32];
606
607 if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
608 return UBUS_STATUS_PERMISSION_DENIED;
609
610 if (!tb[NOTIFY_COMMAND])
611 goto error;
612
613 if (!fill_string_list(tb[NOTIFY_COMMAND], argv, ARRAY_SIZE(argv)))
614 goto error;
615
616 if (!fill_string_list(tb[NOTIFY_ENV], env, ARRAY_SIZE(env)))
617 goto error;
618
619 netifd_start_process((const char **) argv, (char **) env, &state->proto_task);
620
621 return 0;
622
623 error:
624 return UBUS_STATUS_INVALID_ARGUMENT;
625 }
626
627 static int
628 proto_shell_kill_command(struct proto_shell_state *state, struct blob_attr **tb)
629 {
630 unsigned int signal = ~0;
631
632 if (tb[NOTIFY_SIGNAL])
633 signal = blobmsg_get_u32(tb[NOTIFY_SIGNAL]);
634
635 if (signal > 31)
636 signal = SIGTERM;
637
638 if (state->proto_task.uloop.pending) {
639 if (signal == SIGTERM || signal == SIGKILL)
640 state->proto_task_killed = true;
641 kill(state->proto_task.uloop.pid, signal);
642 }
643
644 return 0;
645 }
646
647 static int
648 proto_shell_notify_error(struct proto_shell_state *state, struct blob_attr **tb)
649 {
650 struct blob_attr *cur;
651 char *data[16];
652 int n_data = 0;
653 int rem;
654
655 if (!tb[NOTIFY_ERROR])
656 return UBUS_STATUS_INVALID_ARGUMENT;
657
658 blobmsg_for_each_attr(cur, tb[NOTIFY_ERROR], rem) {
659 if (n_data + 1 == ARRAY_SIZE(data))
660 goto error;
661
662 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
663 goto error;
664
665 if (!blobmsg_check_attr(cur, NULL))
666 goto error;
667
668 data[n_data++] = blobmsg_data(cur);
669 }
670
671 if (!n_data)
672 goto error;
673
674 interface_add_error(state->proto.iface, state->handler->proto.name,
675 data[0], (const char **) &data[1], n_data - 1);
676
677 return 0;
678
679 error:
680 return UBUS_STATUS_INVALID_ARGUMENT;
681 }
682
683 static int
684 proto_shell_block_restart(struct proto_shell_state *state, struct blob_attr **tb)
685 {
686 state->proto.iface->autostart = false;
687 return 0;
688 }
689
690 static int
691 proto_shell_set_available(struct proto_shell_state *state, struct blob_attr **tb)
692 {
693 if (!tb[NOTIFY_AVAILABLE])
694 return UBUS_STATUS_INVALID_ARGUMENT;
695
696 interface_set_available(state->proto.iface, blobmsg_get_bool(tb[NOTIFY_AVAILABLE]));
697 return 0;
698 }
699
700 static int
701 proto_shell_add_host_dependency(struct proto_shell_state *state, struct blob_attr **tb)
702 {
703 struct proto_shell_dependency *dep;
704 const char *ifname = tb[NOTIFY_IFNAME] ? blobmsg_data(tb[NOTIFY_IFNAME]) : "";
705 const char *host = tb[NOTIFY_HOST] ? blobmsg_data(tb[NOTIFY_HOST]) : "";
706
707 if (state->sm == S_TEARDOWN || state->sm == S_SETUP_ABORT)
708 return UBUS_STATUS_PERMISSION_DENIED;
709
710 dep = calloc(1, sizeof(*dep) + strlen(ifname) + 1);
711
712 if (!host[0] && ifname[0]) {
713 dep->any = true;
714 } else if (inet_pton(AF_INET, host, &dep->host) < 1) {
715 if (inet_pton(AF_INET6, host, &dep->host) < 1) {
716 free(dep);
717 return UBUS_STATUS_INVALID_ARGUMENT;
718 } else {
719 dep->v6 = true;
720 }
721 }
722
723 dep->proto = state;
724 strcpy(dep->interface, ifname);
725
726 dep->dep.cb = proto_shell_if_up_cb;
727 interface_add_user(&dep->dep, NULL);
728 list_add(&dep->list, &state->deps);
729 proto_shell_update_host_dep(dep);
730 if (!dep->dep.iface)
731 return UBUS_STATUS_NOT_FOUND;
732
733 return 0;
734 }
735
736 static int
737 proto_shell_setup_failed(struct proto_shell_state *state)
738 {
739 int ret = 0;
740
741 switch (state->sm) {
742 case S_IDLE:
743 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
744 /* fall through */
745 case S_SETUP:
746 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
747 break;
748 case S_SETUP_ABORT:
749 case S_TEARDOWN:
750 default:
751 ret = UBUS_STATUS_PERMISSION_DENIED;
752 break;
753 }
754 return ret;
755 }
756
757 static int
758 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
759 {
760 struct proto_shell_state *state;
761 struct blob_attr *tb[__NOTIFY_LAST];
762
763 state = container_of(proto, struct proto_shell_state, proto);
764
765 blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
766 if (!tb[NOTIFY_ACTION])
767 return UBUS_STATUS_INVALID_ARGUMENT;
768
769 switch(blobmsg_get_u32(tb[NOTIFY_ACTION])) {
770 case 0:
771 return proto_shell_update_link(state, attr, tb);
772 case 1:
773 return proto_shell_run_command(state, tb);
774 case 2:
775 return proto_shell_kill_command(state, tb);
776 case 3:
777 return proto_shell_notify_error(state, tb);
778 case 4:
779 return proto_shell_block_restart(state, tb);
780 case 5:
781 return proto_shell_set_available(state, tb);
782 case 6:
783 return proto_shell_add_host_dependency(state, tb);
784 case 7:
785 return proto_shell_setup_failed(state);
786 default:
787 return UBUS_STATUS_INVALID_ARGUMENT;
788 }
789 }
790
791 static void
792 proto_shell_checkup_timeout_cb(struct uloop_timeout *timeout)
793 {
794 struct proto_shell_state *state = container_of(timeout, struct
795 proto_shell_state, checkup_timeout);
796 struct interface_proto_state *proto = &state->proto;
797 struct interface *iface = proto->iface;
798
799 if (!iface->autostart)
800 return;
801
802 if (iface->state == IFS_UP)
803 return;
804
805 D(INTERFACE, "Interface '%s' is not up after %d sec\n",
806 iface->name, state->checkup_interval);
807 proto_shell_handler(proto, PROTO_CMD_TEARDOWN, false);
808 }
809
810 static void
811 proto_shell_checkup_attach(struct proto_shell_state *state,
812 const struct blob_attr *attr)
813 {
814 struct blob_attr *tb;
815 struct blobmsg_policy checkup_policy = {
816 .name = "checkup_interval",
817 .type = BLOBMSG_TYPE_INT32
818 };
819
820 blobmsg_parse(&checkup_policy, 1, &tb, blob_data(attr), blob_len(attr));
821 if (!tb) {
822 state->checkup_interval = -1;
823 state->checkup_timeout.cb = NULL;
824 } else {
825 state->checkup_interval = blobmsg_get_u32(tb);
826 state->checkup_timeout.cb = proto_shell_checkup_timeout_cb;
827 }
828 }
829
830 static struct interface_proto_state *
831 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
832 struct blob_attr *attr)
833 {
834 struct proto_shell_state *state;
835
836 state = calloc(1, sizeof(*state));
837 INIT_LIST_HEAD(&state->deps);
838
839 state->config = malloc(blob_pad_len(attr));
840 if (!state->config)
841 goto error;
842
843 memcpy(state->config, attr, blob_pad_len(attr));
844 proto_shell_checkup_attach(state, state->config);
845 state->proto.free = proto_shell_free;
846 state->proto.notify = proto_shell_notify;
847 state->proto.cb = proto_shell_handler;
848 state->teardown_timeout.cb = proto_shell_teardown_timeout_cb;
849 state->script_task.cb = proto_shell_script_cb;
850 state->script_task.dir_fd = proto_fd;
851 state->script_task.log_prefix = iface->name;
852 state->proto_task.cb = proto_shell_task_cb;
853 state->proto_task.dir_fd = proto_fd;
854 state->proto_task.log_prefix = iface->name;
855 state->handler = container_of(h, struct proto_shell_handler, proto);
856
857 return &state->proto;
858
859 error:
860 free(state);
861 return NULL;
862 }
863
864 static void
865 proto_shell_add_handler(const char *script, const char *name, json_object *obj)
866 {
867 struct proto_shell_handler *handler;
868 struct proto_handler *proto;
869 json_object *config, *tmp;
870 char *proto_name, *script_name;
871
872 handler = calloc_a(sizeof(*handler),
873 &proto_name, strlen(name) + 1,
874 &script_name, strlen(script) + 1);
875 if (!handler)
876 return;
877
878 handler->script_name = strcpy(script_name, script);
879
880 proto = &handler->proto;
881 proto->name = strcpy(proto_name, name);
882 proto->config_params = &handler->config;
883 proto->attach = proto_shell_attach;
884
885 tmp = json_get_field(obj, "no-device", json_type_boolean);
886 if (tmp && json_object_get_boolean(tmp))
887 handler->proto.flags |= PROTO_FLAG_NODEV;
888
889 tmp = json_get_field(obj, "no-proto-task", json_type_boolean);
890 handler->no_proto_task = tmp && json_object_get_boolean(tmp);
891
892 tmp = json_get_field(obj, "available", json_type_boolean);
893 if (tmp && json_object_get_boolean(tmp))
894 handler->proto.flags |= PROTO_FLAG_INIT_AVAILABLE;
895
896 tmp = json_get_field(obj, "renew-handler", json_type_boolean);
897 if (tmp && json_object_get_boolean(tmp))
898 handler->proto.flags |= PROTO_FLAG_RENEW_AVAILABLE;
899
900 tmp = json_get_field(obj, "lasterror", json_type_boolean);
901 if (tmp && json_object_get_boolean(tmp))
902 handler->proto.flags |= PROTO_FLAG_LASTERROR;
903
904 config = json_get_field(obj, "config", json_type_array);
905 if (config)
906 handler->config_buf = netifd_handler_parse_config(&handler->config, config);
907
908 DPRINTF("Add handler for script %s: %s\n", script, proto->name);
909 add_proto_handler(proto);
910 }
911
912 void proto_shell_init(void)
913 {
914 proto_fd = netifd_open_subdir("proto");
915 if (proto_fd < 0)
916 return;
917
918 netifd_init_script_handlers(proto_fd, proto_shell_add_handler);
919 }