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