netifd: Export ip4table/ip6table via UBUS
[project/netifd.git] / proto.c
1 /*
2  * netifd - network interface daemon
3  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4  * Copyright (C) 2012 Steven Barth <steven@midlink.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <limits.h>
19
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
22
23 #include "netifd.h"
24 #include "system.h"
25 #include "interface.h"
26 #include "interface-ip.h"
27 #include "proto.h"
28
29 static struct avl_tree handlers;
30
31 enum {
32         OPT_IPADDR,
33         OPT_IP6ADDR,
34         OPT_NETMASK,
35         OPT_BROADCAST,
36         OPT_GATEWAY,
37         OPT_IP6GW,
38         OPT_IP6PREFIX,
39         __OPT_MAX,
40 };
41
42 static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
43         [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
44         [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
45         [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
46         [OPT_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
47         [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
48         [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
49         [OPT_IP6PREFIX] = { .name = "ip6prefix", .type = BLOBMSG_TYPE_ARRAY },
50 };
51
52 static const struct uci_blob_param_info proto_ip_attr_info[__OPT_MAX] = {
53         [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
54         [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
55         [OPT_IP6PREFIX] = { .type = BLOBMSG_TYPE_STRING },
56 };
57
58 const struct uci_blob_param_list proto_ip_attr = {
59         .n_params = __OPT_MAX,
60         .params = proto_ip_attributes,
61         .info = proto_ip_attr_info,
62 };
63
64 enum {
65         ADDR_IPADDR,
66         ADDR_MASK,
67         ADDR_BROADCAST,
68         ADDR_PTP,
69         ADDR_PREFERRED,
70         ADDR_VALID,
71         ADDR_OFFLINK,
72         ADDR_CLASS,
73         __ADDR_MAX
74 };
75
76 static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
77         [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING },
78         [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
79         [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
80         [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING },
81         [ADDR_PREFERRED] = { .name = "preferred", .type = BLOBMSG_TYPE_INT32 },
82         [ADDR_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
83         [ADDR_OFFLINK] = { .name = "offlink", .type = BLOBMSG_TYPE_BOOL },
84         [ADDR_CLASS] = { .name = "class", .type = BLOBMSG_TYPE_STRING },
85 };
86
87 static struct device_addr *
88 alloc_device_addr(bool v6, bool ext)
89 {
90         struct device_addr *addr;
91
92         addr = calloc(1, sizeof(*addr));
93         addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
94         if (ext)
95                 addr->flags |= DEVADDR_EXTERNAL;
96
97         return addr;
98 }
99
100 static bool
101 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
102            bool ext, uint32_t broadcast)
103 {
104         struct device_addr *addr;
105         int af = v6 ? AF_INET6 : AF_INET;
106
107         addr = alloc_device_addr(v6, ext);
108         if (!addr)
109                 return false;
110
111         addr->mask = mask;
112         if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) {
113                 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
114                 free(addr);
115                 return false;
116         }
117
118         if (broadcast)
119                 addr->broadcast = broadcast;
120
121         vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
122         return true;
123 }
124
125 static int
126 parse_static_address_option(struct interface *iface, struct blob_attr *attr,
127                             bool v6, int netmask, bool ext, uint32_t broadcast)
128 {
129         struct blob_attr *cur;
130         int n_addr = 0;
131         int rem;
132
133         blobmsg_for_each_attr(cur, attr, rem) {
134                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
135                         return -1;
136
137                 n_addr++;
138                 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
139                                 broadcast))
140                         return -1;
141         }
142
143         return n_addr;
144 }
145
146 static struct device_addr *
147 parse_address_item(struct blob_attr *attr, bool v6, bool ext)
148 {
149         struct device_addr *addr;
150         struct blob_attr *tb[__ADDR_MAX];
151         struct blob_attr *cur;
152
153         if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
154                 return NULL;
155
156         addr = alloc_device_addr(v6, ext);
157         if (!addr)
158                 return NULL;
159
160         blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
161
162         addr->mask = v6 ? 128 : 32;
163         if ((cur = tb[ADDR_MASK])) {
164                 unsigned int new_mask;
165
166                 new_mask = parse_netmask_string(blobmsg_data(cur), v6);
167                 if (new_mask > addr->mask)
168                         goto error;
169
170                 addr->mask = new_mask;
171         }
172
173         cur = tb[ADDR_IPADDR];
174         if (!cur)
175                 goto error;
176
177         if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
178                 goto error;
179
180         if ((cur = tb[ADDR_OFFLINK]) && blobmsg_get_bool(cur))
181                 addr->flags |= DEVADDR_OFFLINK;
182
183         if (!v6) {
184                 if ((cur = tb[ADDR_BROADCAST]) &&
185                     !inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
186                         goto error;
187                 if ((cur = tb[ADDR_PTP]) &&
188                     !inet_pton(AF_INET, blobmsg_data(cur), &addr->point_to_point))
189                         goto error;
190         } else {
191                 time_t now = system_get_rtime();
192                 if ((cur = tb[ADDR_PREFERRED])) {
193                         int64_t preferred = blobmsg_get_u32(cur);
194                         int64_t preferred_until = preferred + (int64_t)now;
195                         if (preferred_until <= LONG_MAX && preferred != 0xffffffffLL)
196                                 addr->preferred_until = preferred_until;
197                 }
198
199                 if ((cur = tb[ADDR_VALID])) {
200                         int64_t valid = blobmsg_get_u32(cur);
201                         int64_t valid_until = valid + (int64_t)now;
202                         if (valid_until <= LONG_MAX && valid != 0xffffffffLL)
203                                 addr->valid_until = valid_until;
204
205                 }
206
207                 if (addr->valid_until) {
208                         if (!addr->preferred_until)
209                                 addr->preferred_until = addr->valid_until;
210                         else if (addr->preferred_until > addr->valid_until)
211                                 goto error;
212                 }
213
214                 if ((cur = tb[ADDR_CLASS]))
215                         addr->pclass = strdup(blobmsg_get_string(cur));
216         }
217
218         return addr;
219
220 error:
221         free(addr);
222         return NULL;
223 }
224
225 static int
226 parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
227                    bool ext)
228 {
229         struct device_addr *addr;
230         struct blob_attr *cur;
231         int n_addr = 0;
232         int rem;
233
234         blobmsg_for_each_attr(cur, attr, rem) {
235                 addr = parse_address_item(cur, v6, ext);
236                 if (!addr)
237                         return -1;
238
239                 n_addr++;
240                 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
241         }
242
243         return n_addr;
244 }
245
246 static bool
247 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
248 {
249         struct device_route *route;
250         const char *str = blobmsg_data(attr);
251         int af = v6 ? AF_INET6 : AF_INET;
252
253         route = calloc(1, sizeof(*route));
254         if (!inet_pton(af, str, &route->nexthop)) {
255                 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
256                 free(route);
257                 return false;
258         }
259
260         route->mask = 0;
261         route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
262
263         unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
264         if (table) {
265                 route->table = table;
266                 route->flags |= DEVROUTE_SRCTABLE;
267         }
268
269         vlist_add(&iface->proto_ip.route, &route->node, route);
270
271         return true;
272 }
273
274 static bool
275 parse_prefix_option(struct interface *iface, const char *str, size_t len)
276 {
277         char buf[128] = {0}, *saveptr;
278         if (len > sizeof(buf))
279                 return false;
280
281         memcpy(buf, str, len);
282         char *addrstr = strtok_r(buf, "/", &saveptr);
283         if (!addrstr)
284                 return false;
285
286         char *lengthstr = strtok_r(NULL, ",", &saveptr);
287         if (!lengthstr)
288                 return false;
289
290         char *prefstr = strtok_r(NULL, ",", &saveptr);
291         char *validstr = (!prefstr) ? NULL : strtok_r(NULL, ",", &saveptr);
292         char *addstr = (!validstr) ? NULL : strtok_r(NULL, ",", &saveptr);
293         const char *pclass = NULL;
294
295         int64_t pref = (!prefstr) ? 0 : strtoul(prefstr, NULL, 10);
296         int64_t valid = (!validstr) ? 0 : strtoul(validstr, NULL, 10);
297
298         uint8_t length = strtoul(lengthstr, NULL, 10), excl_length = 0;
299         if (length < 1 || length > 64)
300                 return false;
301
302         struct in6_addr addr, excluded, *excludedp = NULL;
303         if (inet_pton(AF_INET6, addrstr, &addr) < 1)
304                 return false;
305
306         for (; addstr; addstr = strtok_r(NULL, ",", &saveptr)) {
307                 char *key = NULL, *val = NULL, *addsaveptr;
308                 if (!(key = strtok_r(addstr, "=", &addsaveptr)) ||
309                                 !(val = strtok_r(NULL, ",", &addsaveptr)))
310                         continue;
311
312                 if (!strcmp(key, "excluded")) {
313                         char *sep = strchr(val, '/');
314                         if (!sep)
315                                 return false;
316
317                         *sep = 0;
318                         excl_length = atoi(sep + 1);
319
320                         if (inet_pton(AF_INET6, val, &excluded) < 1)
321                                 return false;
322
323                         excludedp = &excluded;
324                 } else if (!strcmp(key, "class")) {
325                         pclass = val;
326                 }
327
328         }
329
330
331
332
333         int64_t now = system_get_rtime();
334         time_t preferred_until = 0;
335         if (prefstr && pref != 0xffffffffLL && pref + now <= LONG_MAX)
336                 preferred_until = pref + now;
337
338         time_t valid_until = 0;
339         if (validstr && valid != 0xffffffffLL && valid + now <= LONG_MAX)
340                 valid_until = valid + now;
341
342         interface_ip_add_device_prefix(iface, &addr, length,
343                         valid_until, preferred_until,
344                         excludedp, excl_length, pclass);
345         return true;
346 }
347
348 static int
349 parse_prefix_list(struct interface *iface, struct blob_attr *attr)
350 {
351         struct blob_attr *cur;
352         int n_addr = 0;
353         int rem;
354
355         blobmsg_for_each_attr(cur, attr, rem) {
356                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
357                         return -1;
358
359                 n_addr++;
360                 if (!parse_prefix_option(iface, blobmsg_data(cur),
361                                 blobmsg_data_len(cur)))
362                         return -1;
363         }
364
365         return n_addr;
366 }
367
368 int
369 proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
370 {
371         struct blob_attr *tb[__OPT_MAX];
372         struct blob_attr *cur;
373         const char *error;
374         unsigned int netmask = 32;
375         int n_v4 = 0, n_v6 = 0;
376         struct in_addr bcast = {};
377
378         blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
379
380         if ((cur = tb[OPT_NETMASK])) {
381                 netmask = parse_netmask_string(blobmsg_data(cur), false);
382                 if (netmask > 32) {
383                         error = "INVALID_NETMASK";
384                         goto error;
385                 }
386         }
387
388         if ((cur = tb[OPT_BROADCAST])) {
389                 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
390                         error = "INVALID_BROADCAST";
391                         goto error;
392                 }
393         }
394
395         if ((cur = tb[OPT_IPADDR]))
396                 n_v4 = parse_static_address_option(iface, cur, false,
397                         netmask, false, bcast.s_addr);
398
399         if ((cur = tb[OPT_IP6ADDR]))
400                 n_v6 = parse_static_address_option(iface, cur, true,
401                         128, false, 0);
402
403         if ((cur = tb[OPT_IP6PREFIX]))
404                 if (parse_prefix_list(iface, cur) < 0)
405                         goto out;
406
407         if (n_v4 < 0 || n_v6 < 0)
408                 goto out;
409
410         if ((cur = tb[OPT_GATEWAY])) {
411                 if (n_v4 && !parse_gateway_option(iface, cur, false))
412                         goto out;
413         }
414
415         if ((cur = tb[OPT_IP6GW])) {
416                 if (n_v6 && !parse_gateway_option(iface, cur, true))
417                         goto out;
418         }
419
420         return 0;
421
422 error:
423         interface_add_error(iface, "proto", error, NULL, 0);
424 out:
425         return -1;
426 }
427
428 int
429 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
430 {
431         struct blob_attr *tb[__OPT_MAX];
432         struct blob_attr *cur;
433         int n_v4 = 0, n_v6 = 0;
434
435         blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
436
437         if ((cur = tb[OPT_IPADDR]))
438                 n_v4 = parse_address_list(iface, cur, false, ext);
439
440         if ((cur = tb[OPT_IP6ADDR]))
441                 n_v6 = parse_address_list(iface, cur, true, ext);
442
443         if ((cur = tb[OPT_IP6PREFIX]))
444                 if (parse_prefix_list(iface, cur) < 0)
445                         goto out;
446
447         if (n_v4 < 0 || n_v6 < 0)
448                 goto out;
449
450         if ((cur = tb[OPT_GATEWAY])) {
451                 if (n_v4 && !parse_gateway_option(iface, cur, false))
452                         goto out;
453         }
454
455         if ((cur = tb[OPT_IP6GW])) {
456                 if (n_v6 && !parse_gateway_option(iface, cur, true))
457                         goto out;
458         }
459
460         return 0;
461
462 out:
463         return -1;
464 }
465
466 void add_proto_handler(struct proto_handler *p)
467 {
468         if (!handlers.comp)
469                 avl_init(&handlers, avl_strcmp, false, NULL);
470
471         if (p->avl.key)
472                 return;
473
474         p->avl.key = p->name;
475         avl_insert(&handlers, &p->avl);
476 }
477
478 static void
479 default_proto_free(struct interface_proto_state *proto)
480 {
481         free(proto);
482 }
483
484 static int
485 invalid_proto_handler(struct interface_proto_state *proto,
486                       enum interface_proto_cmd cmd, bool force)
487 {
488         return -1;
489 }
490
491 static int
492 no_proto_handler(struct interface_proto_state *proto,
493                  enum interface_proto_cmd cmd, bool force)
494 {
495         return 0;
496 }
497
498 static struct interface_proto_state *
499 default_proto_attach(const struct proto_handler *h,
500                      struct interface *iface, struct blob_attr *attr)
501 {
502         struct interface_proto_state *proto;
503
504         proto = calloc(1, sizeof(*proto));
505         proto->free = default_proto_free;
506         proto->cb = no_proto_handler;
507
508         return proto;
509 }
510
511 static const struct proto_handler no_proto = {
512         .name = "none",
513         .flags = PROTO_FLAG_IMMEDIATE,
514         .attach = default_proto_attach,
515 };
516
517 static const struct proto_handler *
518 get_proto_handler(const char *name)
519 {
520         struct proto_handler *proto;
521
522         if (!strcmp(name, "none"))
523             return &no_proto;
524
525         if (!handlers.comp)
526                 return NULL;
527
528         return avl_find_element(&handlers, name, proto, avl);
529 }
530
531 void
532 proto_dump_handlers(struct blob_buf *b)
533 {
534         struct proto_handler *p;
535         void *c;
536
537         avl_for_each_element(&handlers, p, avl) {
538                 c = blobmsg_open_table(b, p->name);
539                 blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV));
540                 blobmsg_close_table(b, c);
541         }
542 }
543
544 void
545 proto_init_interface(struct interface *iface, struct blob_attr *attr)
546 {
547         const struct proto_handler *proto = iface->proto_handler;
548         struct interface_proto_state *state = NULL;
549
550         if (!proto)
551                 proto = &no_proto;
552
553         state = proto->attach(proto, iface, attr);
554         if (!state) {
555                 state = no_proto.attach(&no_proto, iface, attr);
556                 state->cb = invalid_proto_handler;
557         }
558
559         state->handler = proto;
560         interface_set_proto_state(iface, state);
561 }
562
563 void
564 proto_attach_interface(struct interface *iface, const char *proto_name)
565 {
566         const struct proto_handler *proto = &no_proto;
567
568         if (proto_name) {
569                 proto = get_proto_handler(proto_name);
570                 if (!proto) {
571                         interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
572                         proto = &no_proto;
573                 }
574         }
575
576         iface->proto_handler = proto;
577 }
578
579 int
580 interface_proto_event(struct interface_proto_state *proto,
581                       enum interface_proto_cmd cmd, bool force)
582 {
583         enum interface_proto_event ev;
584         int ret;
585
586         ret = proto->cb(proto, cmd, force);
587         if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
588                 goto out;
589
590         switch(cmd) {
591         case PROTO_CMD_SETUP:
592                 ev = IFPEV_UP;
593                 break;
594         case PROTO_CMD_TEARDOWN:
595                 ev = IFPEV_DOWN;
596                 break;
597         default:
598                 return -EINVAL;
599         }
600         proto->proto_event(proto, ev);
601
602 out:
603         return ret;
604 }