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