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