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