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