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