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