delete interfaces from the avl tree on free
[project/netifd.git] / proto-shell.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <glob.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <signal.h>
8
9 #include <arpa/inet.h>
10 #include <netinet/in.h>
11
12 #include <libubox/blobmsg_json.h>
13
14 #include "netifd.h"
15 #include "interface.h"
16 #include "interface-ip.h"
17 #include "proto.h"
18
19 static int proto_fd;
20
21 struct proto_shell_handler {
22         struct list_head list;
23         struct proto_handler proto;
24         struct config_param_list config;
25         char *config_buf;
26         char script_name[];
27 };
28
29 struct proto_shell_state {
30         struct interface_proto_state proto;
31         struct proto_shell_handler *handler;
32         struct blob_attr *config;
33
34         struct device_user l3_dev;
35
36         struct uloop_timeout setup_timeout;
37         struct uloop_process setup_task;
38         struct uloop_process teardown_task;
39         bool teardown_pending;
40 };
41
42 static int
43 run_script(const char **argv, struct uloop_process *proc)
44 {
45         int pid;
46
47         if ((pid = fork()) < 0)
48                 return -1;
49
50         if (!pid) {
51                 fchdir(proto_fd);
52                 execvp(argv[0], (char **) argv);
53                 exit(127);
54         }
55
56         if (pid < 0)
57                 return -1;
58
59         proc->pid = pid;
60         uloop_process_add(proc);
61
62         return 0;
63 }
64
65 static int
66 proto_shell_handler(struct interface_proto_state *proto,
67                     enum interface_proto_cmd cmd, bool force)
68 {
69         struct proto_shell_state *state;
70         struct proto_shell_handler *handler;
71         struct uloop_process *proc;
72         const char *argv[6];
73         const char *action;
74         char *config;
75         int ret, i = 0;
76
77         state = container_of(proto, struct proto_shell_state, proto);
78         handler = state->handler;
79
80         if (cmd == PROTO_CMD_SETUP) {
81                 action = "setup";
82                 proc = &state->setup_task;
83         } else {
84                 action = "teardown";
85                 proc = &state->teardown_task;
86                 if (state->setup_task.pending) {
87                         uloop_timeout_set(&state->setup_timeout, 1000);
88                         kill(state->setup_task.pid, SIGINT);
89                         state->teardown_pending = true;
90                         return 0;
91                 }
92         }
93
94         config = blobmsg_format_json(state->config, true);
95         if (!config)
96                 return -1;
97
98         argv[i++] = handler->script_name;
99         argv[i++] = handler->proto.name;
100         argv[i++] = action;
101         argv[i++] = proto->iface->name;
102         argv[i++] = config;
103         if (proto->iface->main_dev.dev)
104                 argv[i++] = proto->iface->main_dev.dev->ifname;
105         argv[i] = NULL;
106
107         ret = run_script(argv, proc);
108         free(config);
109
110         return ret;
111 }
112
113 static void
114 proto_shell_setup_timeout_cb(struct uloop_timeout *timeout)
115 {
116         struct proto_shell_state *state;
117
118         state = container_of(timeout, struct proto_shell_state, setup_timeout);
119         kill(state->setup_task.pid, SIGKILL);
120 }
121
122 static void
123 proto_shell_setup_cb(struct uloop_process *p, int ret)
124 {
125         struct proto_shell_state *state;
126
127         state = container_of(p, struct proto_shell_state, setup_task);
128         uloop_timeout_cancel(&state->setup_timeout);
129         if (state->teardown_pending) {
130                 state->teardown_pending = false;
131                 proto_shell_handler(&state->proto, PROTO_CMD_TEARDOWN, false);
132         }
133 }
134
135 static void
136 proto_shell_teardown_cb(struct uloop_process *p, int ret)
137 {
138         struct proto_shell_state *state;
139
140         state = container_of(p, struct proto_shell_state, teardown_task);
141
142         if (state->l3_dev.dev)
143                 device_remove_user(&state->l3_dev);
144
145         state->proto.proto_event(&state->proto, IFPEV_DOWN);
146 }
147
148 static void
149 proto_shell_free(struct interface_proto_state *proto)
150 {
151         struct proto_shell_state *state;
152
153         state = container_of(proto, struct proto_shell_state, proto);
154         free(state->config);
155         free(state);
156 }
157
158 static void
159 proto_shell_parse_addr_list(struct interface *iface, struct blob_attr *attr,
160                             bool v6, bool external)
161 {
162         struct device_addr *addr;
163         struct blob_attr *cur;
164         int rem;
165
166         blobmsg_for_each_attr(cur, attr, rem) {
167                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) {
168                         DPRINTF("Ignore wrong address type: %d\n", blobmsg_type(cur));
169                         continue;
170                 }
171
172                 addr = proto_parse_ip_addr_string(blobmsg_data(cur), v6, v6 ? 32 : 128);
173                 if (!addr) {
174                         DPRINTF("Failed to parse IP address string: %s\n", (char *) blobmsg_data(cur));
175                         continue;
176                 }
177
178                 if (external)
179                         addr->flags |= DEVADDR_EXTERNAL;
180
181                 vlist_add(&iface->proto_addr, &addr->node);
182         }
183 }
184
185 enum {
186         ROUTE_TARGET,
187         ROUTE_MASK,
188         ROUTE_GATEWAY,
189         ROUTE_DEVICE,
190         __ROUTE_LAST
191 };
192
193 static const struct blobmsg_policy route_attr[__ROUTE_LAST] = {
194         [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
195         [ROUTE_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_INT32 },
196         [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
197         [ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
198 };
199
200 static void
201 parse_route(struct interface *iface, struct blob_attr *attr, bool v6)
202 {
203         struct blob_attr *tb[__ROUTE_LAST], *cur;
204         struct device_route *route;
205         int af = v6 ? AF_INET6 : AF_INET;
206
207         blobmsg_parse(route_attr, __ROUTE_LAST, tb, blobmsg_data(attr), blobmsg_data_len(attr));
208
209         if (!tb[ROUTE_GATEWAY] && !tb[ROUTE_DEVICE])
210                 return;
211
212         route = calloc(1, sizeof(*route));
213         if (!route)
214                 return;
215
216         route->mask = v6 ? 128 : 32;
217         if ((cur = tb[ROUTE_MASK]) != NULL) {
218                 route->mask = blobmsg_get_u32(cur);
219                 if (route->mask > v6 ? 128 : 32)
220                         goto error;
221         }
222
223         if ((cur = tb[ROUTE_TARGET]) != NULL) {
224                 if (!inet_pton(af, blobmsg_data(cur), &route->addr)) {
225                         DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
226                         goto error;
227                 }
228         }
229
230         if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
231                 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
232                         DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
233                         goto error;
234                 }
235         }
236
237         if ((cur = tb[ROUTE_DEVICE]) != NULL)
238                 route->device = device_get(blobmsg_data(cur), true);
239
240         vlist_add(&iface->proto_route, &route->node);
241         return;
242
243 error:
244         free(route);
245 }
246
247 static void
248 proto_shell_parse_route_list(struct interface *iface, struct blob_attr *attr,
249                              bool v6)
250 {
251         struct blob_attr *cur;
252         int rem;
253
254         blobmsg_for_each_attr(cur, attr, rem) {
255                 if (blobmsg_type(cur) != BLOBMSG_TYPE_TABLE) {
256                         DPRINTF("Ignore wrong route type: %d\n", blobmsg_type(cur));
257                         continue;
258                 }
259
260                 parse_route(iface, cur, v6);
261         }
262 }
263
264
265 enum {
266         NOTIFY_LINK_UP,
267         NOTIFY_IFNAME,
268         NOTIFY_ADDR_EXT,
269         NOTIFY_IPADDR,
270         NOTIFY_IP6ADDR,
271         NOTIFY_ROUTES,
272         NOTIFY_ROUTES6,
273         __NOTIFY_LAST
274 };
275
276 static const struct blobmsg_policy notify_attr[__NOTIFY_LAST] = {
277         [NOTIFY_LINK_UP] = { .name = "link-up", .type = BLOBMSG_TYPE_BOOL },
278         [NOTIFY_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
279         [NOTIFY_ADDR_EXT] = { .name = "address-external", .type = BLOBMSG_TYPE_BOOL },
280         [NOTIFY_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
281         [NOTIFY_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
282         [NOTIFY_ROUTES] = { .name = "routes", .type = BLOBMSG_TYPE_ARRAY },
283         [NOTIFY_ROUTES6] = { .name = "routes6", .type = BLOBMSG_TYPE_ARRAY },
284 };
285
286 static int
287 proto_shell_notify(struct interface_proto_state *proto, struct blob_attr *attr)
288 {
289         struct proto_shell_state *state;
290         struct blob_attr *tb[__NOTIFY_LAST], *cur;
291         bool addr_ext = false;
292         bool up;
293
294         state = container_of(proto, struct proto_shell_state, proto);
295
296         blobmsg_parse(notify_attr, __NOTIFY_LAST, tb, blob_data(attr), blob_len(attr));
297         if (!tb[NOTIFY_LINK_UP])
298                 return UBUS_STATUS_INVALID_ARGUMENT;
299
300         up = blobmsg_get_bool(tb[NOTIFY_LINK_UP]);
301         if (up) {
302                 if (!tb[NOTIFY_IFNAME])
303                         return UBUS_STATUS_INVALID_ARGUMENT;
304
305                 if (!state->l3_dev.dev) {
306                         device_add_user(&state->l3_dev,
307                                 device_get(blobmsg_data(tb[NOTIFY_IFNAME]), true));
308                         device_claim(&state->l3_dev);
309                         state->proto.iface->l3_dev = &state->l3_dev;
310                 }
311                 state->proto.proto_event(&state->proto, IFPEV_UP);
312         } else {
313                 state->proto.proto_event(&state->proto, IFPEV_LINK_LOST);
314         }
315
316         if ((cur = tb[NOTIFY_ADDR_EXT]) != NULL)
317                 addr_ext = blobmsg_get_bool(cur);
318
319         if ((cur = tb[NOTIFY_IPADDR]) != NULL)
320                 proto_shell_parse_addr_list(state->proto.iface, cur, false, addr_ext);
321
322         if ((cur = tb[NOTIFY_IP6ADDR]) != NULL)
323                 proto_shell_parse_addr_list(state->proto.iface, cur, true, addr_ext);
324
325         if ((cur = tb[NOTIFY_ROUTES]) != NULL)
326                 proto_shell_parse_route_list(state->proto.iface, cur, false);
327
328         if ((cur = tb[NOTIFY_ROUTES6]) != NULL)
329                 proto_shell_parse_route_list(state->proto.iface, cur, true);
330
331         return 0;
332 }
333
334 struct interface_proto_state *
335 proto_shell_attach(const struct proto_handler *h, struct interface *iface,
336                    struct blob_attr *attr)
337 {
338         struct proto_shell_state *state;
339
340         state = calloc(1, sizeof(*state));
341         state->config = malloc(blob_pad_len(attr));
342         if (!state->config)
343                 goto error;
344
345         memcpy(state->config, attr, blob_pad_len(attr));
346         state->proto.free = proto_shell_free;
347         state->proto.notify = proto_shell_notify;
348         state->proto.cb = proto_shell_handler;
349         state->setup_timeout.cb = proto_shell_setup_timeout_cb;
350         state->setup_task.cb = proto_shell_setup_cb;
351         state->teardown_task.cb = proto_shell_teardown_cb;
352         state->handler = container_of(h, struct proto_shell_handler, proto);
353
354         return &state->proto;
355
356 error:
357         free(state);
358         return NULL;
359 }
360
361 static json_object *
362 check_type(json_object *obj, json_type type)
363 {
364         if (!obj)
365                 return NULL;
366
367         if (json_object_get_type(obj) != type)
368                 return NULL;
369
370         return obj;
371 }
372
373 static inline json_object *
374 get_field(json_object *obj, const char *name, json_type type)
375 {
376         return check_type(json_object_object_get(obj, name), type);
377 }
378
379 static char *
380 proto_shell_parse_config(struct config_param_list *config, json_object *obj)
381 {
382         struct blobmsg_policy *attrs;
383         char *str_buf, *str_cur;
384         int str_len = 0;
385         int i;
386
387         config->n_params = json_object_array_length(obj);
388         attrs = calloc(1, sizeof(*attrs) * config->n_params);
389         if (!attrs)
390                 return NULL;
391
392         config->params = attrs;
393         for (i = 0; i < config->n_params; i++) {
394                 json_object *cur, *name, *type;
395
396                 cur = check_type(json_object_array_get_idx(obj, i), json_type_array);
397                 if (!cur)
398                         goto error;
399
400                 name = check_type(json_object_array_get_idx(cur, 0), json_type_string);
401                 if (!name)
402                         goto error;
403
404                 type = check_type(json_object_array_get_idx(cur, 1), json_type_int);
405                 if (!type)
406                         goto error;
407
408                 attrs[i].name = json_object_get_string(name);
409                 attrs[i].type = json_object_get_int(type);
410                 if (attrs[i].type > BLOBMSG_TYPE_LAST)
411                         goto error;
412
413                 str_len += strlen(attrs[i].name) + 1;
414         }
415
416         str_buf = malloc(str_len);
417         if (!str_buf)
418                 goto error;
419
420         str_cur = str_buf;
421         for (i = 0; i < config->n_params; i++) {
422                 const char *name = attrs[i].name;
423
424                 attrs[i].name = str_cur;
425                 str_cur += sprintf(str_cur, "%s", name) + 1;
426         }
427
428         return str_buf;
429
430 error:
431         free(attrs);
432         config->n_params = 0;
433         return NULL;
434 }
435
436 static void
437 proto_shell_add_handler(const char *script, json_object *obj)
438 {
439         struct proto_shell_handler *handler;
440         struct proto_handler *proto;
441         json_object *config, *tmp;
442         const char *name;
443         char *str;
444
445         if (!check_type(obj, json_type_object))
446                 return;
447
448         tmp = get_field(obj, "name", json_type_string);
449         if (!tmp)
450                 return;
451
452         name = json_object_get_string(tmp);
453
454         handler = calloc(1, sizeof(*handler) +
455                          strlen(script) + 1 +
456                          strlen(name) + 1);
457         if (!handler)
458                 return;
459
460         strcpy(handler->script_name, script);
461
462         str = handler->script_name + strlen(handler->script_name) + 1;
463         strcpy(str, name);
464
465         proto = &handler->proto;
466         proto->name = str;
467         proto->config_params = &handler->config;
468         proto->attach = proto_shell_attach;
469
470         tmp = get_field(obj, "no-device", json_type_boolean);
471         if (tmp && json_object_get_boolean(tmp))
472                 handler->proto.flags |= PROTO_FLAG_NODEV;
473
474         config = get_field(obj, "config", json_type_array);
475         if (config)
476                 handler->config_buf = proto_shell_parse_config(&handler->config, config);
477
478         DPRINTF("Add handler for script %s: %s\n", script, proto->name);
479         add_proto_handler(proto);
480 }
481
482 static void proto_shell_add_script(const char *name)
483 {
484         struct json_tokener *tok = NULL;
485         json_object *obj;
486         static char buf[512];
487         char *start, *end, *cmd;
488         FILE *f;
489         int buflen, len;
490
491 #define DUMP_SUFFIX     " '' dump"
492
493         cmd = alloca(strlen(name) + 1 + sizeof(DUMP_SUFFIX));
494         sprintf(cmd, "%s" DUMP_SUFFIX, name);
495
496         f = popen(cmd, "r");
497         if (!f)
498                 return;
499
500         do {
501                 buflen = fread(buf, 1, sizeof(buf) - 1, f);
502                 if (buflen <= 0)
503                         continue;
504
505                 start = buf;
506                 len = buflen;
507                 do {
508                         end = memchr(start, '\n', len);
509                         if (end)
510                                 len = end - start;
511
512                         if (!tok)
513                                 tok = json_tokener_new();
514
515                         obj = json_tokener_parse_ex(tok, start, len);
516                         if (!is_error(obj)) {
517                                 proto_shell_add_handler(name, obj);
518                                 json_object_put(obj);
519                                 json_tokener_free(tok);
520                                 tok = NULL;
521                         }
522
523                         if (end) {
524                                 start = end + 1;
525                                 len = buflen - (start - buf);
526                         }
527                 } while (len > 0);
528         } while (!feof(f) && !ferror(f));
529
530         if (tok)
531                 json_tokener_free(tok);
532
533         pclose(f);
534 }
535
536 void __init proto_shell_init(void)
537 {
538         glob_t g;
539         int main_fd;
540         int i;
541
542         main_fd = open(".", O_RDONLY | O_DIRECTORY);
543         if (main_fd < 0)
544                 return;
545
546         if (chdir(main_path)) {
547                 perror("chdir(main path)");
548                 goto close_cur;
549         }
550
551         if (chdir("./proto"))
552                 goto close_cur;
553
554         proto_fd = open(".", O_RDONLY | O_DIRECTORY);
555         if (proto_fd < 0)
556                 goto close_cur;
557
558         glob("./*.sh", 0, NULL, &g);
559         for (i = 0; i < g.gl_pathc; i++)
560                 proto_shell_add_script(g.gl_pathv[i]);
561
562 close_cur:
563         fchdir(main_fd);
564         close(main_fd);
565 }