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