interface-ip: add missing IPv6 policy rule
[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_PTPADDR,
37         OPT_GATEWAY,
38         OPT_IP6GW,
39         OPT_IP6PREFIX,
40         OPT_IP6DEPRECATED,
41         __OPT_MAX,
42 };
43
44 static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
45         [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
46         [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
47         [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
48         [OPT_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
49         [OPT_PTPADDR] = { .name = "ptpaddr", .type = BLOBMSG_TYPE_STRING },
50         [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
51         [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
52         [OPT_IP6PREFIX] = { .name = "ip6prefix", .type = BLOBMSG_TYPE_ARRAY },
53         [OPT_IP6DEPRECATED] = { .name = "ip6deprecated", .type = BLOBMSG_TYPE_BOOL },
54 };
55
56 static const struct uci_blob_param_info proto_ip_attr_info[__OPT_MAX] = {
57         [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
58         [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
59         [OPT_IP6PREFIX] = { .type = BLOBMSG_TYPE_STRING },
60 };
61
62 static const char * const proto_ip_validate[__OPT_MAX] = {
63         [OPT_IPADDR] = "ip4addr",
64         [OPT_IP6ADDR] = "ip6addr",
65         [OPT_NETMASK] = "netmask",
66         [OPT_BROADCAST] = "ipaddr",
67         [OPT_PTPADDR] = "ip4addr",
68         [OPT_GATEWAY] = "ip4addr",
69         [OPT_IP6GW] = "ip6addr",
70         [OPT_IP6PREFIX] = "ip6addr",
71 };
72
73 const struct uci_blob_param_list proto_ip_attr = {
74         .n_params = __OPT_MAX,
75         .params = proto_ip_attributes,
76         .validate = proto_ip_validate,
77         .info = proto_ip_attr_info,
78 };
79
80 enum {
81         ADDR_IPADDR,
82         ADDR_MASK,
83         ADDR_BROADCAST,
84         ADDR_PTP,
85         ADDR_PREFERRED,
86         ADDR_VALID,
87         ADDR_OFFLINK,
88         ADDR_CLASS,
89         __ADDR_MAX
90 };
91
92 static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
93         [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING },
94         [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
95         [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
96         [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING },
97         [ADDR_PREFERRED] = { .name = "preferred", .type = BLOBMSG_TYPE_INT32 },
98         [ADDR_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
99         [ADDR_OFFLINK] = { .name = "offlink", .type = BLOBMSG_TYPE_BOOL },
100         [ADDR_CLASS] = { .name = "class", .type = BLOBMSG_TYPE_STRING },
101 };
102
103 static struct device_addr *
104 alloc_device_addr(bool v6, bool ext)
105 {
106         struct device_addr *addr;
107
108         addr = calloc(1, sizeof(*addr));
109         if (!addr)
110                 return NULL;
111
112         addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
113         if (ext)
114                 addr->flags |= DEVADDR_EXTERNAL;
115
116         return addr;
117 }
118
119 static bool
120 parse_addr(struct interface *iface, const char *str, bool v6, int mask,
121            bool ext, uint32_t broadcast, uint32_t ptp, bool deprecated)
122 {
123         struct device_addr *addr;
124         int af = v6 ? AF_INET6 : AF_INET;
125
126         addr = alloc_device_addr(v6, ext);
127         if (!addr)
128                 return false;
129
130         addr->mask = mask;
131         if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask))
132                 goto error;
133
134         if (!v6) {
135                 if (IN_EXPERIMENTAL(ntohl(addr->addr.in.s_addr)))
136                         goto error;
137
138         } else if (IN6_IS_ADDR_MULTICAST(&addr->addr.in6))
139                 goto error;
140
141         if (broadcast)
142                 addr->broadcast = broadcast;
143
144         if (ptp)
145                 addr->point_to_point = ptp;
146
147         if (deprecated)
148                 addr->preferred_until = system_get_rtime();
149
150         vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
151         return true;
152
153 error:
154         interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
155         free(addr);
156
157         return false;
158 }
159
160 static int
161 parse_static_address_option(struct interface *iface, struct blob_attr *attr,
162                             bool v6, int netmask, bool ext, uint32_t broadcast,
163                             uint32_t ptp, bool deprecated)
164 {
165         struct blob_attr *cur;
166         int n_addr = 0;
167         int rem;
168
169         blobmsg_for_each_attr(cur, attr, rem) {
170                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
171                         return -1;
172
173                 n_addr++;
174                 if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
175                                 broadcast, ptp, deprecated))
176                         return -1;
177         }
178
179         return n_addr;
180 }
181
182 static struct device_addr *
183 parse_address_item(struct blob_attr *attr, bool v6, bool ext)
184 {
185         struct device_addr *addr;
186         struct blob_attr *tb[__ADDR_MAX];
187         struct blob_attr *cur;
188
189         if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
190                 return NULL;
191
192         addr = alloc_device_addr(v6, ext);
193         if (!addr)
194                 return NULL;
195
196         blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
197
198         addr->mask = v6 ? 128 : 32;
199         if ((cur = tb[ADDR_MASK])) {
200                 unsigned int new_mask;
201
202                 new_mask = parse_netmask_string(blobmsg_data(cur), v6);
203                 if (new_mask > addr->mask)
204                         goto error;
205
206                 addr->mask = new_mask;
207         }
208
209         cur = tb[ADDR_IPADDR];
210         if (!cur)
211                 goto error;
212
213         if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
214                 goto error;
215
216         if ((cur = tb[ADDR_OFFLINK]) && blobmsg_get_bool(cur))
217                 addr->flags |= DEVADDR_OFFLINK;
218
219         if (!v6) {
220                 if ((cur = tb[ADDR_BROADCAST]) &&
221                     !inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
222                         goto error;
223                 if ((cur = tb[ADDR_PTP]) &&
224                     !inet_pton(AF_INET, blobmsg_data(cur), &addr->point_to_point))
225                         goto error;
226         } else {
227                 time_t now = system_get_rtime();
228                 if ((cur = tb[ADDR_PREFERRED])) {
229                         int64_t preferred = blobmsg_get_u32(cur);
230                         int64_t preferred_until = preferred + (int64_t)now;
231                         if (preferred_until <= LONG_MAX && preferred != 0xffffffffLL)
232                                 addr->preferred_until = preferred_until;
233                 }
234
235                 if ((cur = tb[ADDR_VALID])) {
236                         int64_t valid = blobmsg_get_u32(cur);
237                         int64_t valid_until = valid + (int64_t)now;
238                         if (valid_until <= LONG_MAX && valid != 0xffffffffLL)
239                                 addr->valid_until = valid_until;
240
241                 }
242
243                 if (addr->valid_until) {
244                         if (!addr->preferred_until)
245                                 addr->preferred_until = addr->valid_until;
246                         else if (addr->preferred_until > addr->valid_until)
247                                 goto error;
248                 }
249
250                 if ((cur = tb[ADDR_CLASS]))
251                         addr->pclass = strdup(blobmsg_get_string(cur));
252         }
253
254         return addr;
255
256 error:
257         free(addr);
258         return NULL;
259 }
260
261 static int
262 parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
263                    bool ext)
264 {
265         struct device_addr *addr;
266         struct blob_attr *cur;
267         int n_addr = 0;
268         int rem;
269
270         blobmsg_for_each_attr(cur, attr, rem) {
271                 addr = parse_address_item(cur, v6, ext);
272                 if (!addr)
273                         return -1;
274
275                 n_addr++;
276                 vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
277         }
278
279         return n_addr;
280 }
281
282 static bool
283 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
284 {
285         struct device_route *route;
286         const char *str = blobmsg_data(attr);
287         int af = v6 ? AF_INET6 : AF_INET;
288
289         route = calloc(1, sizeof(*route));
290         if (!route)
291                 return NULL;
292
293         if (!inet_pton(af, str, &route->nexthop)) {
294                 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
295                 free(route);
296                 return false;
297         }
298
299         route->mask = 0;
300         route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
301         route->metric = iface->metric;
302
303         unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
304         if (table) {
305                 route->table = table;
306                 route->flags |= DEVROUTE_SRCTABLE;
307         }
308
309         vlist_add(&iface->proto_ip.route, &route->node, route);
310
311         return true;
312 }
313
314 static bool
315 parse_prefix_option(struct interface *iface, const char *str, size_t len)
316 {
317         char buf[128] = {0}, *saveptr;
318         if (len >= sizeof(buf))
319                 return false;
320
321         memcpy(buf, str, len);
322         char *addrstr = strtok_r(buf, "/", &saveptr);
323         if (!addrstr)
324                 return false;
325
326         char *lengthstr = strtok_r(NULL, ",", &saveptr);
327         if (!lengthstr)
328                 return false;
329
330         char *prefstr = strtok_r(NULL, ",", &saveptr);
331         char *validstr = (!prefstr) ? NULL : strtok_r(NULL, ",", &saveptr);
332         char *addstr = (!validstr) ? NULL : strtok_r(NULL, ",", &saveptr);
333         const char *pclass = NULL;
334
335         int64_t pref = (!prefstr) ? 0 : strtoul(prefstr, NULL, 10);
336         int64_t valid = (!validstr) ? 0 : strtoul(validstr, NULL, 10);
337
338         uint8_t length = strtoul(lengthstr, NULL, 10), excl_length = 0;
339         if (length < 1 || length > 64)
340                 return false;
341
342         struct in6_addr addr, excluded, *excludedp = NULL;
343         if (inet_pton(AF_INET6, addrstr, &addr) < 1)
344                 return false;
345
346         for (; addstr; addstr = strtok_r(NULL, ",", &saveptr)) {
347                 char *key = NULL, *val = NULL, *addsaveptr;
348                 if (!(key = strtok_r(addstr, "=", &addsaveptr)) ||
349                                 !(val = strtok_r(NULL, ",", &addsaveptr)))
350                         continue;
351
352                 if (!strcmp(key, "excluded")) {
353                         char *sep = strchr(val, '/');
354                         if (!sep)
355                                 return false;
356
357                         *sep = 0;
358                         excl_length = atoi(sep + 1);
359
360                         if (inet_pton(AF_INET6, val, &excluded) < 1)
361                                 return false;
362
363                         excludedp = &excluded;
364                 } else if (!strcmp(key, "class")) {
365                         pclass = val;
366                 }
367
368         }
369
370
371
372
373         int64_t now = system_get_rtime();
374         time_t preferred_until = 0;
375         if (prefstr && pref != 0xffffffffLL && pref + now <= LONG_MAX)
376                 preferred_until = pref + now;
377
378         time_t valid_until = 0;
379         if (validstr && valid != 0xffffffffLL && valid + now <= LONG_MAX)
380                 valid_until = valid + now;
381
382         interface_ip_add_device_prefix(iface, &addr, length,
383                         valid_until, preferred_until,
384                         excludedp, excl_length, pclass);
385         return true;
386 }
387
388 static int
389 parse_prefix_list(struct interface *iface, struct blob_attr *attr)
390 {
391         struct blob_attr *cur;
392         int n_addr = 0;
393         int rem;
394
395         blobmsg_for_each_attr(cur, attr, rem) {
396                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
397                         return -1;
398
399                 n_addr++;
400                 if (!parse_prefix_option(iface, blobmsg_data(cur),
401                                 blobmsg_data_len(cur)))
402                         return -1;
403         }
404
405         return n_addr;
406 }
407
408 int
409 proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
410 {
411         struct blob_attr *tb[__OPT_MAX];
412         struct blob_attr *cur;
413         const char *error;
414         unsigned int netmask = 32;
415         bool ip6deprecated;
416         int n_v4 = 0, n_v6 = 0;
417         struct in_addr bcast = {}, ptp = {};
418
419         blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
420
421         if ((cur = tb[OPT_NETMASK])) {
422                 netmask = parse_netmask_string(blobmsg_data(cur), false);
423                 if (netmask > 32) {
424                         error = "INVALID_NETMASK";
425                         goto error;
426                 }
427         }
428
429         if ((cur = tb[OPT_BROADCAST])) {
430                 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
431                         error = "INVALID_BROADCAST";
432                         goto error;
433                 }
434         }
435
436         if ((cur = tb[OPT_PTPADDR])) {
437                 if (!inet_pton(AF_INET, blobmsg_data(cur), &ptp)) {
438                         error = "INVALID_PTPADDR";
439                         goto error;
440                 }
441         }
442
443         ip6deprecated = blobmsg_get_bool_default(tb[OPT_IP6DEPRECATED], false);
444
445         if ((cur = tb[OPT_IPADDR]))
446                 n_v4 = parse_static_address_option(iface, cur, false,
447                         netmask, false, bcast.s_addr, ptp.s_addr, false);
448
449         if ((cur = tb[OPT_IP6ADDR]))
450                 n_v6 = parse_static_address_option(iface, cur, true,
451                         128, false, 0, 0, ip6deprecated);
452
453         if ((cur = tb[OPT_IP6PREFIX]))
454                 if (parse_prefix_list(iface, cur) < 0)
455                         goto out;
456
457         if (n_v4 < 0 || n_v6 < 0)
458                 goto out;
459
460         if ((cur = tb[OPT_GATEWAY])) {
461                 if (n_v4 && !parse_gateway_option(iface, cur, false))
462                         goto out;
463         }
464
465         if ((cur = tb[OPT_IP6GW])) {
466                 if (n_v6 && !parse_gateway_option(iface, cur, true))
467                         goto out;
468         }
469
470         return 0;
471
472 error:
473         interface_add_error(iface, "proto", error, NULL, 0);
474 out:
475         return -1;
476 }
477
478 int
479 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
480 {
481         struct blob_attr *tb[__OPT_MAX];
482         struct blob_attr *cur;
483         int n_v4 = 0, n_v6 = 0;
484
485         blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
486
487         if ((cur = tb[OPT_IPADDR]))
488                 n_v4 = parse_address_list(iface, cur, false, ext);
489
490         if ((cur = tb[OPT_IP6ADDR]))
491                 n_v6 = parse_address_list(iface, cur, true, ext);
492
493         if ((cur = tb[OPT_IP6PREFIX]))
494                 if (parse_prefix_list(iface, cur) < 0)
495                         goto out;
496
497         if (n_v4 < 0 || n_v6 < 0)
498                 goto out;
499
500         if ((cur = tb[OPT_GATEWAY])) {
501                 if (n_v4 && !parse_gateway_option(iface, cur, false))
502                         goto out;
503         }
504
505         if ((cur = tb[OPT_IP6GW])) {
506                 if (n_v6 && !parse_gateway_option(iface, cur, true))
507                         goto out;
508         }
509
510         return 0;
511
512 out:
513         return -1;
514 }
515
516 void add_proto_handler(struct proto_handler *p)
517 {
518         if (!handlers.comp)
519                 avl_init(&handlers, avl_strcmp, false, NULL);
520
521         if (p->avl.key)
522                 return;
523
524         p->avl.key = p->name;
525         avl_insert(&handlers, &p->avl);
526 }
527
528 static void
529 default_proto_free(struct interface_proto_state *proto)
530 {
531         free(proto);
532 }
533
534 static int
535 invalid_proto_handler(struct interface_proto_state *proto,
536                       enum interface_proto_cmd cmd, bool force)
537 {
538         return -1;
539 }
540
541 static int
542 no_proto_handler(struct interface_proto_state *proto,
543                  enum interface_proto_cmd cmd, bool force)
544 {
545         return 0;
546 }
547
548 static struct interface_proto_state *
549 default_proto_attach(const struct proto_handler *h,
550                      struct interface *iface, struct blob_attr *attr)
551 {
552         struct interface_proto_state *proto;
553
554         proto = calloc(1, sizeof(*proto));
555         if (!proto)
556                 return NULL;
557
558         proto->free = default_proto_free;
559         proto->cb = no_proto_handler;
560
561         return proto;
562 }
563
564 static const struct proto_handler no_proto = {
565         .name = "none",
566         .flags = PROTO_FLAG_IMMEDIATE,
567         .attach = default_proto_attach,
568 };
569
570 static const struct proto_handler *
571 get_proto_handler(const char *name)
572 {
573         struct proto_handler *proto;
574
575         if (!strcmp(name, "none"))
576             return &no_proto;
577
578         if (!handlers.comp)
579                 return NULL;
580
581         return avl_find_element(&handlers, name, proto, avl);
582 }
583
584 void
585 proto_dump_handlers(struct blob_buf *b)
586 {
587         struct proto_handler *p;
588         void *c;
589
590         avl_for_each_element(&handlers, p, avl) {
591                 void *v;
592
593                 c = blobmsg_open_table(b, p->name);
594                 if (p->config_params->validate) {
595                         int i;
596
597                         v = blobmsg_open_table(b, "validate");
598                         for (i = 0; i < p->config_params->n_params; i++)
599                                 blobmsg_add_string(b, p->config_params->params[i].name, uci_get_validate_string(p->config_params, i));
600                         blobmsg_close_table(b, v);
601                 }
602                 blobmsg_add_u8(b, "immediate", !!(p->flags & PROTO_FLAG_IMMEDIATE));
603                 blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV));
604                 blobmsg_add_u8(b, "init_available", !!(p->flags & PROTO_FLAG_INIT_AVAILABLE));
605                 blobmsg_add_u8(b, "renew_available", !!(p->flags & PROTO_FLAG_RENEW_AVAILABLE));
606                 blobmsg_add_u8(b, "force_link_default", !!(p->flags & PROTO_FLAG_FORCE_LINK_DEFAULT));
607                 blobmsg_add_u8(b, "last_error", !!(p->flags & PROTO_FLAG_LASTERROR));
608                 blobmsg_add_u8(b, "teardown_on_l3_link_down", !!(p->flags & PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN));
609                 blobmsg_add_u8(b, "no_task", !!(p->flags & PROTO_FLAG_NO_TASK));
610                 blobmsg_close_table(b, c);
611         }
612 }
613
614 void
615 proto_init_interface(struct interface *iface, struct blob_attr *attr)
616 {
617         const struct proto_handler *proto = iface->proto_handler;
618         struct interface_proto_state *state = NULL;
619
620         if (!proto)
621                 proto = &no_proto;
622
623         state = proto->attach(proto, iface, attr);
624         if (!state) {
625                 state = no_proto.attach(&no_proto, iface, attr);
626                 state->cb = invalid_proto_handler;
627         }
628
629         state->handler = proto;
630         interface_set_proto_state(iface, state);
631 }
632
633 void
634 proto_attach_interface(struct interface *iface, const char *proto_name)
635 {
636         const struct proto_handler *proto = &no_proto;
637         const char *error = NULL;
638
639         if (proto_name) {
640                 proto = get_proto_handler(proto_name);
641                 if (!proto) {
642                         error = "INVALID_PROTO";
643                         proto = &no_proto;
644                 }
645         }
646
647         iface->proto_handler = proto;
648
649         if (error)
650                 interface_add_error(iface, "proto", error, NULL, 0);
651 }
652
653 int
654 interface_proto_event(struct interface_proto_state *proto,
655                       enum interface_proto_cmd cmd, bool force)
656 {
657         enum interface_proto_event ev;
658         int ret;
659
660         ret = proto->cb(proto, cmd, force);
661         if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
662                 goto out;
663
664         switch(cmd) {
665         case PROTO_CMD_SETUP:
666                 ev = IFPEV_UP;
667                 break;
668         case PROTO_CMD_TEARDOWN:
669                 ev = IFPEV_DOWN;
670                 break;
671         case PROTO_CMD_RENEW:
672                 ev = IFPEV_RENEW;
673                 break;
674         default:
675                 return -EINVAL;
676         }
677         proto->proto_event(proto, ev);
678
679 out:
680         return ret;
681 }