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