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