proto-static: ensure that the l3 device is properly set
[project/netifd.git] / interface-ip.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
19 #include <limits.h>
20 #include <arpa/inet.h>
21
22 #include "netifd.h"
23 #include "device.h"
24 #include "interface.h"
25 #include "interface-ip.h"
26 #include "proto.h"
27 #include "ubus.h"
28 #include "system.h"
29
30 enum {
31         ROUTE_INTERFACE,
32         ROUTE_TARGET,
33         ROUTE_MASK,
34         ROUTE_GATEWAY,
35         ROUTE_METRIC,
36         ROUTE_MTU,
37         ROUTE_VALID,
38         ROUTE_TABLE,
39         ROUTE_SOURCE,
40         __ROUTE_MAX
41 };
42
43 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
44         [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
45         [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
46         [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
47         [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
48         [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
49         [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
50         [ROUTE_TABLE] = { .name = "table", .type = BLOBMSG_TYPE_STRING },
51         [ROUTE_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
52         [ROUTE_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_STRING },
53 };
54
55 const struct uci_blob_param_list route_attr_list = {
56         .n_params = __ROUTE_MAX,
57         .params = route_attr,
58 };
59
60
61 struct list_head prefixes = LIST_HEAD_INIT(prefixes);
62 static struct device_prefix *ula_prefix = NULL;
63 static struct uloop_timeout valid_until_timeout;
64
65
66 static void
67 clear_if_addr(union if_addr *a, int mask)
68 {
69         int m_bytes = (mask + 7) / 8;
70         uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
71         uint8_t *p = (uint8_t *) a;
72
73         if (m_bytes < sizeof(a))
74                 memset(p + m_bytes, 0, sizeof(a) - m_bytes);
75
76         p[m_bytes - 1] &= ~m_clear;
77 }
78
79 static bool
80 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
81 {
82         union if_addr *p1, *p2;
83
84         p1 = alloca(sizeof(*a1));
85         p2 = alloca(sizeof(*a2));
86
87         memcpy(p1, a1, sizeof(*a1));
88         clear_if_addr(p1, mask);
89         memcpy(p2, a2, sizeof(*a2));
90         clear_if_addr(p2, mask);
91
92         return !memcmp(p1, p2, sizeof(*p1));
93 }
94
95 static int set_ip_source_policy(bool add, bool v6, unsigned int priority,
96                 const union if_addr *addr, uint8_t mask, unsigned int table,
97                 struct interface *in_iface, const char *action)
98 {
99         struct iprule rule = {
100                 .flags = IPRULE_PRIORITY,
101                 .priority = priority
102         };
103
104         if (addr) {
105                 rule.flags |= IPRULE_SRC;
106                 rule.src_addr = *addr;
107                 rule.src_mask = mask;
108         }
109
110         if (table) {
111                 rule.flags |= IPRULE_LOOKUP;
112                 rule.lookup = table;
113
114                 if (!rule.lookup)
115                         return 0;
116         } else if (action) {
117                 rule.flags |= IPRULE_ACTION;
118                 system_resolve_iprule_action(action, &rule.action);
119         }
120
121         if (in_iface && in_iface->l3_dev.dev) {
122                 rule.flags |= IPRULE_IN;
123                 strcpy(rule.in_dev, in_iface->l3_dev.dev->ifname);
124         }
125
126         rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
127
128         return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
129 }
130
131 static int set_ip_lo_policy(bool add, bool v6, struct interface *iface)
132 {
133         struct iprule rule = {
134                 .flags = IPRULE_IN | IPRULE_LOOKUP | IPRULE_PRIORITY,
135                 .priority = IPRULE_PRIORITY_NW + iface->l3_dev.dev->ifindex,
136                 .lookup = (v6) ? iface->ip6table : iface->ip4table,
137                 .in_dev = "lo"
138         };
139
140         if (!rule.lookup)
141                 return 0;
142
143         rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
144
145         return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
146 }
147
148 static bool
149 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
150 {
151         struct device_addr *addr;
152
153         vlist_for_each_element(&ip->addr, addr, node) {
154                 if (!addr->enabled)
155                         continue;
156
157                 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
158                         continue;
159
160                 // Handle offlink addresses correctly
161                 unsigned int mask = addr->mask;
162                 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6 &&
163                                 (addr->flags & DEVADDR_OFFLINK))
164                         mask = 128;
165
166                 if (!match_if_addr(&addr->addr, a, mask))
167                         continue;
168
169                 return true;
170         }
171
172         return false;
173 }
174
175 static void
176 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
177                        bool v6, struct device_route **res)
178 {
179         struct device_route *route;
180
181         vlist_for_each_element(&ip->route, route, node) {
182                 if (!route->enabled)
183                         continue;
184
185                 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
186                         continue;
187
188                 if (!match_if_addr(&route->addr, a, route->mask))
189                         continue;
190
191                 if (route->flags & DEVROUTE_TABLE)
192                         continue;
193
194                 if (!*res || route->mask < (*res)->mask)
195                         *res = route;
196         }
197 }
198
199 static bool
200 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
201 {
202         return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
203                __find_ip_addr_target(&iface->config_ip, a, v6);
204 }
205
206 static void
207 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
208                                bool v6, struct device_route **route)
209 {
210         __find_ip_route_target(&iface->proto_ip, a, v6, route);
211         __find_ip_route_target(&iface->config_ip, a, v6, route);
212 }
213
214 struct interface *
215 interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *iface)
216 {
217         struct device_route *route, *r_next = NULL;
218         bool defaultroute_target = false;
219         int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
220
221         route = calloc(1, sizeof(*route));
222         if (!route)
223                 return NULL;
224
225         route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
226         route->mask = v6 ? 128 : 32;
227         if (memcmp(&route->addr, addr, addrsize) == 0)
228                 defaultroute_target = true;
229         else
230                 memcpy(&route->addr, addr, addrsize);
231
232         if (iface) {
233                 /* look for locally addressable target first */
234                 if (interface_ip_find_addr_target(iface, addr, v6))
235                         goto done;
236
237                 /* do not stop at the first route, let the lookup compare
238                  * masks to find the best match */
239                 interface_ip_find_route_target(iface, addr, v6, &r_next);
240         } else {
241                 vlist_for_each_element(&interfaces, iface, node) {
242                         /* look for locally addressable target first */
243                         if (interface_ip_find_addr_target(iface, addr, v6))
244                                 goto done;
245
246                         /* do not stop at the first route, let the lookup compare
247                          * masks to find the best match */
248                         interface_ip_find_route_target(iface, addr, v6, &r_next);
249                 }
250         }
251
252         if (!r_next) {
253                 free(route);
254                 return NULL;
255         }
256
257         iface = r_next->iface;
258         memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
259         route->mtu = r_next->mtu;
260         route->metric = r_next->metric;
261         route->table = r_next->table;
262
263 done:
264         route->iface = iface;
265         if (defaultroute_target)
266                 free(route);
267         else
268                 vlist_add(&iface->host_routes, &route->node, route);
269         return iface;
270 }
271
272 void
273 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
274 {
275         struct interface_ip_settings *ip;
276         struct blob_attr *tb[__ROUTE_MAX], *cur;
277         struct device_route *route;
278         int af = v6 ? AF_INET6 : AF_INET;
279         bool is_proto_route = !!iface;
280
281         blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
282
283         if (!iface) {
284                 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
285                         return;
286
287                 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
288                 if (!iface)
289                         return;
290
291                 ip = &iface->config_ip;
292         } else {
293                 ip = &iface->proto_ip;
294         }
295
296         route = calloc(1, sizeof(*route));
297         if (!route)
298                 return;
299
300         route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
301         route->mask = v6 ? 128 : 32;
302         if ((cur = tb[ROUTE_MASK]) != NULL) {
303                 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
304                 if (route->mask > (v6 ? 128 : 32))
305                         goto error;
306         }
307
308         if ((cur = tb[ROUTE_TARGET]) != NULL) {
309                 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
310                         DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
311                         goto error;
312                 }
313         }
314
315         if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
316                 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
317                         DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
318                         goto error;
319                 }
320         }
321
322         if ((cur = tb[ROUTE_METRIC]) != NULL) {
323                 route->metric = blobmsg_get_u32(cur);
324                 route->flags |= DEVROUTE_METRIC;
325         } else
326                 route->metric = iface->metric;
327
328         if ((cur = tb[ROUTE_MTU]) != NULL) {
329                 route->mtu = blobmsg_get_u32(cur);
330                 route->flags |= DEVROUTE_MTU;
331         }
332
333         // Use source-based routing
334         if ((cur = tb[ROUTE_SOURCE]) != NULL) {
335                 char *saveptr, *source = alloca(blobmsg_data_len(cur));
336                 memcpy(source, blobmsg_data(cur), blobmsg_data_len(cur));
337
338                 const char *addr = strtok_r(source, "/", &saveptr);
339                 const char *mask = strtok_r(NULL, "/", &saveptr);
340
341                 if (!addr || inet_pton(af, addr, &route->source) < 1) {
342                         DPRINTF("Failed to parse route source: %s\n", addr);
343                         goto error;
344                 }
345
346                 route->sourcemask = (mask) ? atoi(mask) : ((af == AF_INET6) ? 128 : 32);
347         }
348
349         if (is_proto_route) {
350                 route->table = (v6) ? iface->ip6table : iface->ip4table;
351                 route->flags |= DEVROUTE_SRCTABLE;
352         }
353
354         if ((cur = tb[ROUTE_TABLE]) != NULL) {
355                 if (!system_resolve_rt_table(blobmsg_data(cur), &route->table)) {
356                         DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
357                         goto error;
358                 }
359
360                 if (route->table)
361                         route->flags |= DEVROUTE_TABLE;
362         }
363
364         if ((cur = tb[ROUTE_VALID]) != NULL) {
365                 int64_t valid = blobmsg_get_u32(cur);
366                 int64_t valid_until = valid + (int64_t)system_get_rtime();
367                 if (valid_until <= LONG_MAX && valid != 0xffffffffLL) // Catch overflow
368                         route->valid_until = valid_until;
369         }
370
371         vlist_add(&ip->route, &route->node, route);
372         return;
373
374 error:
375         free(route);
376 }
377
378 static int
379 addr_cmp(const void *k1, const void *k2, void *ptr)
380 {
381         return memcmp(k1, k2, sizeof(struct device_addr) -
382                       offsetof(struct device_addr, flags));
383 }
384
385 static int
386 route_cmp(const void *k1, const void *k2, void *ptr)
387 {
388         const struct device_route *r1 = k1, *r2 = k2;
389
390         if (r1->mask != r2->mask)
391                 return r2->mask - r1->mask;
392
393         if (r1->metric != r2->metric)
394                 return r1->metric - r2->metric;
395
396         if (r1->flags != r2->flags)
397                 return r2->flags - r1->flags;
398
399         if (r1->sourcemask != r2->sourcemask)
400                 return r1->sourcemask - r2->sourcemask;
401
402         int maskcmp = memcmp(&r1->source, &r2->source, sizeof(r1->source));
403         if (maskcmp)
404                 return maskcmp;
405
406         return memcmp(&r1->addr, &r2->addr, sizeof(r1->addr));
407 }
408
409 static int
410 prefix_cmp(const void *k1, const void *k2, void *ptr)
411 {
412         return memcmp(k1, k2, offsetof(struct device_prefix, pclass) -
413                         offsetof(struct device_prefix, addr));
414 }
415
416 static void
417 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
418 {
419         struct device *dev = iface->l3_dev.dev;
420         struct device_route route;
421
422         memset(&route, 0, sizeof(route));
423         route.iface = iface;
424         route.flags = addr->flags;
425         route.mask = addr->mask;
426         memcpy(&route.addr, &addr->addr, sizeof(route.addr));
427         clear_if_addr(&route.addr, route.mask);
428
429         if (add) {
430                 route.flags |= DEVADDR_KERNEL;
431                 system_del_route(dev, &route);
432
433                 if (!(addr->flags & DEVADDR_OFFLINK)) {
434                         route.flags &= ~DEVADDR_KERNEL;
435                         route.metric = iface->metric;
436                         system_add_route(dev, &route);
437                 }
438         } else {
439                 if (!(addr->flags & DEVADDR_OFFLINK))
440                         system_del_route(dev, &route);
441         }
442 }
443
444 static void
445 interface_update_proto_addr(struct vlist_tree *tree,
446                             struct vlist_node *node_new,
447                             struct vlist_node *node_old)
448 {
449         struct interface_ip_settings *ip;
450         struct interface *iface;
451         struct device *dev;
452         struct device_addr *a_new = NULL, *a_old = NULL;
453         bool replace = false;
454         bool keep = false;
455         bool v6 = false;
456
457         ip = container_of(tree, struct interface_ip_settings, addr);
458         iface = ip->iface;
459         dev = iface->l3_dev.dev;
460
461         if (!node_new || !node_old)
462                 iface->updated |= IUF_ADDRESS;
463
464         if (node_new) {
465                 a_new = container_of(node_new, struct device_addr, node);
466
467                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
468                     !a_new->broadcast) {
469
470                         uint32_t mask = ~0;
471                         uint32_t *a = (uint32_t *) &a_new->addr;
472
473                         mask >>= a_new->mask;
474                         a_new->broadcast = *a | htonl(mask);
475                 }
476         }
477
478         if (node_old)
479                 a_old = container_of(node_old, struct device_addr, node);
480
481         if (a_new && a_old) {
482                 keep = true;
483
484                 if (a_old->flags != a_new->flags || a_old->failed)
485                         keep = false;
486
487                 if (a_old->valid_until != a_new->valid_until ||
488                                 a_old->preferred_until != a_new->preferred_until)
489                         replace = true;
490
491                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
492                     a_new->broadcast != a_old->broadcast)
493                         keep = false;
494         }
495
496         if (node_old) {
497                 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep) {
498                         interface_handle_subnet_route(iface, a_old, false);
499
500                         if ((a_old->flags & DEVADDR_FAMILY) == DEVADDR_INET6)
501                                 v6 = true;
502
503                         unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
504
505                         //This is needed for source routing to work correctly. If a device
506                         //has two connections to a network using the same subnet, adding
507                         //only the network-rule will cause packets to be routed through the
508                         //first matching network (source IP matches both masks).
509                         if (table) {
510                                 set_ip_source_policy(false, v6, IPRULE_PRIORITY_ADDR, &a_old->addr,
511                                                 (v6) ? 128 : 32, table, NULL, NULL);
512                                 set_ip_source_policy(false, v6, IPRULE_PRIORITY_NW, &a_old->addr,
513                                                 a_old->mask, table, NULL, NULL);
514                         }
515
516                         system_del_address(dev, a_old);
517                 }
518                 free(a_old->pclass);
519                 free(a_old);
520         }
521
522         if (node_new) {
523                 a_new->enabled = true;
524                 if (!(a_new->flags & DEVADDR_EXTERNAL) && (!keep || replace)) {
525                         if (system_add_address(dev, a_new))
526                                 a_new->failed = true;
527
528                         if (!keep) {
529                                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET6)
530                                         v6 = true;
531
532                                 unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
533
534                                 if (table) {
535                                         set_ip_source_policy(true, v6, IPRULE_PRIORITY_ADDR, &a_new->addr,
536                                                         (v6) ? 128 : 32, table, NULL, NULL);
537                                         set_ip_source_policy(true, v6, IPRULE_PRIORITY_NW, &a_new->addr,
538                                                         a_new->mask, table, NULL, NULL);
539                                 }
540                         }
541
542                         if ((a_new->flags & DEVADDR_OFFLINK) || iface->metric)
543                                 interface_handle_subnet_route(iface, a_new, true);
544                 }
545         }
546 }
547
548 static bool
549 enable_route(struct interface_ip_settings *ip, struct device_route *route)
550 {
551         if (ip->no_defaultroute && !route->mask)
552                 return false;
553
554         return ip->enabled;
555 }
556
557 static void
558 interface_update_proto_route(struct vlist_tree *tree,
559                              struct vlist_node *node_new,
560                              struct vlist_node *node_old)
561 {
562         struct interface_ip_settings *ip;
563         struct interface *iface;
564         struct device *dev;
565         struct device_route *route_old, *route_new;
566         bool keep = false;
567
568         ip = container_of(tree, struct interface_ip_settings, route);
569         iface = ip->iface;
570         dev = iface->l3_dev.dev;
571
572         if (!node_new || !node_old)
573                 iface->updated |= IUF_ROUTE;
574
575         route_old = container_of(node_old, struct device_route, node);
576         route_new = container_of(node_new, struct device_route, node);
577
578         if (node_old && node_new)
579                 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop)) &&
580                         (route_old->table == route_new->table) && !route_old->failed;
581
582         if (node_old) {
583                 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
584                         system_del_route(dev, route_old);
585
586                 free(route_old);
587         }
588
589         if (node_new) {
590                 bool _enabled = enable_route(ip, route_new);
591
592                 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
593                         if (system_add_route(dev, route_new))
594                                 route_new->failed = true;
595
596                 route_new->iface = iface;
597                 route_new->enabled = _enabled;
598         }
599 }
600
601 static void
602 interface_update_host_route(struct vlist_tree *tree,
603                              struct vlist_node *node_new,
604                              struct vlist_node *node_old)
605 {
606         struct interface *iface;
607         struct device *dev;
608         struct device_route *route_old, *route_new;
609
610         iface = container_of(tree, struct interface, host_routes);
611         dev = iface->l3_dev.dev;
612
613         route_old = container_of(node_old, struct device_route, node);
614         route_new = container_of(node_new, struct device_route, node);
615
616         if (node_old) {
617                 system_del_route(dev, route_old);
618                 free(route_old);
619         }
620
621         if (node_new)
622                 system_add_route(dev, route_new);
623 }
624
625
626 static void
627 interface_set_prefix_address(struct device_prefix_assignment *assignment,
628                 const struct device_prefix *prefix, struct interface *iface, bool add)
629 {
630         const struct interface *uplink = prefix->iface;
631         if (!iface->l3_dev.dev)
632                 return;
633
634         struct device *l3_downlink = iface->l3_dev.dev;
635
636         struct device_addr addr;
637         memset(&addr, 0, sizeof(addr));
638         addr.addr.in6 = prefix->addr;
639         addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
640         addr.addr.in6.s6_addr[15] += 1;
641         addr.mask = assignment->length;
642         addr.flags = DEVADDR_INET6;
643         addr.preferred_until = prefix->preferred_until;
644         addr.valid_until = prefix->valid_until;
645
646         if (!add && assignment->enabled) {
647                 time_t now = system_get_rtime();
648                 addr.preferred_until = now;
649                 if (!addr.valid_until || addr.valid_until - now > 7200)
650                         addr.valid_until = now + 7200;
651                 system_del_address(l3_downlink, &addr); // Work around dangling prefix routes
652                 system_add_address(l3_downlink, &addr);
653                 if (prefix->iface) {
654                         if (prefix->iface->ip6table)
655                                 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW, &addr.addr,
656                                                 addr.mask, prefix->iface->ip6table, iface, NULL);
657
658                         set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT, &addr.addr,
659                                                         addr.mask, 0, iface, "unreachable");
660                 }
661
662                 assignment->enabled = false;
663         } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP)) {
664                 system_add_address(l3_downlink, &addr);
665                 if (prefix->iface && !assignment->enabled) {
666                         set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT, &addr.addr,
667                                         addr.mask, 0, iface, "unreachable");
668
669                         if (prefix->iface->ip6table)
670                                 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW, &addr.addr,
671                                                 addr.mask, prefix->iface->ip6table, iface, NULL);
672                 }
673                 if (uplink && uplink->l3_dev.dev) {
674                         int mtu = system_update_ipv6_mtu(
675                                         uplink->l3_dev.dev, 0);
676                         if (mtu > 0)
677                                 system_update_ipv6_mtu(l3_downlink, mtu);
678                 }
679                 assignment->enabled = true;
680         }
681 }
682
683 static bool interface_prefix_assign(struct list_head *list,
684                 struct device_prefix_assignment *assign)
685 {
686         int32_t current = 0, asize = (1 << (64 - assign->length)) - 1;
687         struct device_prefix_assignment *c;
688         list_for_each_entry(c, list, head) {
689                 if (assign->assigned != -1) {
690                         if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
691                                 list_add_tail(&assign->head, &c->head);
692                                 return true;
693                         }
694                 } else if (assign->assigned == -1) {
695                         current = (current + asize) & (~asize);
696                         if (current + asize < c->assigned) {
697                                 assign->assigned = current;
698                                 list_add_tail(&assign->head, &c->head);
699                                 return true;
700                         }
701                 }
702                 current = (c->assigned + (1 << (64 - c->length)));
703         }
704         return false;
705 }
706
707 static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup)
708 {
709         struct device_prefix_assignment *c;
710         struct interface *iface;
711
712         // Delete all assignments
713         while (!list_empty(&prefix->assignments)) {
714                 c = list_first_entry(&prefix->assignments,
715                                 struct device_prefix_assignment, head);
716                 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
717                         interface_set_prefix_address(c, prefix, iface, false);
718                 list_del(&c->head);
719                 free(c);
720         }
721
722         if (!setup)
723                 return;
724
725         // End-of-assignment sentinel
726         c = malloc(sizeof(*c) + 1);
727         c->assigned = 1 << (64 - prefix->length);
728         c->length = 64;
729         c->name[0] = 0;
730         list_add(&c->head, &prefix->assignments);
731
732         // Excluded prefix
733         if (prefix->excl_length > 0) {
734                 const char name[] = "!excluded";
735                 c = malloc(sizeof(*c) + sizeof(name));
736                 c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
737                                 ((1 << (64 - prefix->length)) - 1);
738                 c->length = prefix->excl_length;
739                 memcpy(c->name, name, sizeof(name));
740                 list_add(&c->head, &prefix->assignments);
741         }
742
743         bool assigned_any = false;
744         struct list_head assign_later = LIST_HEAD_INIT(assign_later);
745         vlist_for_each_element(&interfaces, iface, node) {
746                 if (iface->assignment_length < 48 ||
747                                 iface->assignment_length > 64)
748                         continue;
749
750                 // Test whether there is a matching class
751                 if (!list_empty(&iface->assignment_classes)) {
752                         bool found = false;
753
754                         struct interface_assignment_class *c;
755                         list_for_each_entry(c, &iface->assignment_classes, head) {
756                                 if (!strcmp(c->name, prefix->pclass)) {
757                                         found = true;
758                                         break;
759                                 }
760                         }
761
762                         if (!found)
763                                 continue;
764                 }
765
766                 size_t namelen = strlen(iface->name) + 1;
767                 c = malloc(sizeof(*c) + namelen);
768                 c->length = iface->assignment_length;
769                 c->assigned = iface->assignment_hint;
770                 c->enabled = false;
771                 memcpy(c->name, iface->name, namelen);
772
773                 // First process all custom assignments, put all others in later-list
774                 if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
775                         if (c->assigned != -1) {
776                                 c->assigned = -1;
777                                 netifd_log_message(L_WARNING, "Failed to assign requested subprefix "
778                                                 "of size %hhu for %s, trying other\n", c->length, c->name);
779                         }
780
781                         struct list_head *next = &assign_later;
782                         struct device_prefix_assignment *n;
783                         list_for_each_entry(n, &assign_later, head) {
784                                 if (n->length < c->length) {
785                                         next = &n->head;
786                                         break;
787                                 }
788                         }
789                         list_add_tail(&c->head, next);
790                 }
791
792                 if (c->assigned != -1)
793                         assigned_any = true;
794         }
795
796         // Then try to assign all other + failed custom assignments
797         while (!list_empty(&assign_later)) {
798                 c = list_first_entry(&assign_later, struct device_prefix_assignment, head);
799                 list_del(&c->head);
800
801                 bool assigned = false;
802                 do {
803                         assigned = interface_prefix_assign(&prefix->assignments, c);
804                 } while (!assigned && ++c->length <= 64);
805
806                 if (!assigned) {
807                         netifd_log_message(L_WARNING, "Failed to assign subprefix "
808                                         "of size %hhu for %s\n", c->length, c->name);
809                         free(c);
810                 } else {
811                         assigned_any = true;
812                 }
813         }
814
815         list_for_each_entry(c, &prefix->assignments, head)
816                 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
817                         interface_set_prefix_address(c, prefix, iface, true);
818
819         if (!assigned_any)
820                 netifd_log_message(L_WARNING, "You have delegated IPv6-prefixes but haven't assigned them "
821                                 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
822 }
823
824
825 void interface_refresh_assignments(bool hint)
826 {
827         static bool refresh = false;
828         if (!hint && refresh) {
829                 struct device_prefix *p;
830                 list_for_each_entry(p, &prefixes, head)
831                         interface_update_prefix_assignments(p, true);
832         }
833         refresh = hint;
834 }
835
836
837 static void
838 interface_update_prefix(struct vlist_tree *tree,
839                              struct vlist_node *node_new,
840                              struct vlist_node *node_old)
841 {
842         struct device_prefix *prefix_old, *prefix_new;
843         prefix_old = container_of(node_old, struct device_prefix, node);
844         prefix_new = container_of(node_new, struct device_prefix, node);
845
846         struct interface_ip_settings *ip = container_of(tree, struct interface_ip_settings, prefix);
847         if (tree && (!node_new || !node_old))
848                 ip->iface->updated |= IUF_PREFIX;
849
850         struct device_route route;
851         memset(&route, 0, sizeof(route));
852         route.flags = DEVADDR_INET6;
853         route.metric = INT32_MAX;
854         route.mask = (node_new) ? prefix_new->length : prefix_old->length;
855         route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
856
857
858         struct device_prefix_assignment *c;
859         struct interface *iface;
860
861         if (node_old && node_new) {
862                 // Move assignments and refresh addresses to update valid times
863                 list_splice(&prefix_old->assignments, &prefix_new->assignments);
864
865                 list_for_each_entry(c, &prefix_new->assignments, head)
866                         if ((iface = vlist_find(&interfaces, c->name, iface, node)))
867                                 interface_set_prefix_address(c, prefix_new, iface, true);
868         } else if (node_new) {
869                 // Set null-route to avoid routing loops
870                 system_add_route(NULL, &route);
871
872                 if (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation)
873                         interface_update_prefix_assignments(prefix_new, true);
874         } else if (node_old) {
875                 // Remove null-route
876                 interface_update_prefix_assignments(prefix_old, false);
877                 system_del_route(NULL, &route);
878         }
879
880         if (node_old) {
881                 if (prefix_old->head.next)
882                         list_del(&prefix_old->head);
883                 free(prefix_old);
884         }
885
886         if (node_new && (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation))
887                 list_add(&prefix_new->head, &prefixes);
888
889 }
890
891 struct device_prefix*
892 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
893                 uint8_t length, time_t valid_until, time_t preferred_until,
894                 struct in6_addr *excl_addr, uint8_t excl_length, const char *pclass)
895 {
896         if (!pclass)
897                 pclass = (iface) ? iface->name : "local";
898
899         struct device_prefix *prefix = calloc(1, sizeof(*prefix) + strlen(pclass) + 1);
900         prefix->length = length;
901         prefix->addr = *addr;
902         prefix->preferred_until = preferred_until;
903         prefix->valid_until = valid_until;
904         prefix->iface = iface;
905         INIT_LIST_HEAD(&prefix->assignments);
906
907         if (excl_addr) {
908                 prefix->excl_addr = *excl_addr;
909                 prefix->excl_length = excl_length;
910         }
911
912         strcpy(prefix->pclass, pclass);
913
914         if (iface)
915                 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
916         else
917                 interface_update_prefix(NULL, &prefix->node, NULL);
918
919         return prefix;
920 }
921
922 void
923 interface_ip_set_ula_prefix(const char *prefix)
924 {
925         char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
926         if (prefix)
927                 strncpy(buf, prefix, sizeof(buf) - 1);
928         char *prefixaddr = strtok_r(buf, "/", &saveptr);
929
930         struct in6_addr addr;
931         if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
932                 if (ula_prefix) {
933                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
934                         ula_prefix = NULL;
935                 }
936                 return;
937         }
938
939         int length;
940         char *prefixlen = strtok_r(NULL, ",", &saveptr);
941         if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
942                 return;
943
944         if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
945                         ula_prefix->length != length) {
946                 if (ula_prefix)
947                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
948
949                 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
950                                 0, 0, NULL, 0, NULL);
951         }
952 }
953
954 void
955 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
956 {
957         struct dns_server *s;
958
959         s = calloc(1, sizeof(*s));
960         if (!s)
961                 return;
962
963         s->af = AF_INET;
964         if (inet_pton(s->af, str, &s->addr.in))
965                 goto add;
966
967         s->af = AF_INET6;
968         if (inet_pton(s->af, str, &s->addr.in))
969                 goto add;
970
971         free(s);
972         return;
973
974 add:
975         D(INTERFACE, "Add IPv%c DNS server: %s\n",
976           s->af == AF_INET6 ? '6' : '4', str);
977         vlist_simple_add(&ip->dns_servers, &s->node);
978 }
979
980 void
981 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
982 {
983         struct blob_attr *cur;
984         int rem;
985
986         blobmsg_for_each_attr(cur, list, rem) {
987                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
988                         continue;
989
990                 if (!blobmsg_check_attr(cur, NULL))
991                         continue;
992
993                 interface_add_dns_server(ip, blobmsg_data(cur));
994         }
995 }
996
997 static void
998 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
999 {
1000         struct dns_search_domain *s;
1001         int len = strlen(str);
1002
1003         s = calloc(1, sizeof(*s) + len + 1);
1004         if (!s)
1005                 return;
1006
1007         D(INTERFACE, "Add DNS search domain: %s\n", str);
1008         memcpy(s->name, str, len);
1009         vlist_simple_add(&ip->dns_search, &s->node);
1010 }
1011
1012 void
1013 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
1014 {
1015         struct blob_attr *cur;
1016         int rem;
1017
1018         blobmsg_for_each_attr(cur, list, rem) {
1019                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1020                         continue;
1021
1022                 if (!blobmsg_check_attr(cur, NULL))
1023                         continue;
1024
1025                 interface_add_dns_search_domain(ip, blobmsg_data(cur));
1026         }
1027 }
1028
1029 static void
1030 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
1031 {
1032         struct dns_server *s;
1033         struct dns_search_domain *d;
1034         const char *str;
1035         char buf[INET6_ADDRSTRLEN];
1036
1037         vlist_simple_for_each_element(&ip->dns_servers, s, node) {
1038                 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
1039                 if (!str)
1040                         continue;
1041
1042                 fprintf(f, "nameserver %s\n", str);
1043         }
1044
1045         vlist_simple_for_each_element(&ip->dns_search, d, node) {
1046                 fprintf(f, "search %s\n", d->name);
1047         }
1048 }
1049
1050 void
1051 interface_write_resolv_conf(void)
1052 {
1053         struct interface *iface;
1054         char *path = alloca(strlen(resolv_conf) + 5);
1055         FILE *f;
1056         uint32_t crcold, crcnew;
1057
1058         sprintf(path, "%s.tmp", resolv_conf);
1059         unlink(path);
1060         f = fopen(path, "w+");
1061         if (!f) {
1062                 D(INTERFACE, "Failed to open %s for writing\n", path);
1063                 return;
1064         }
1065
1066         vlist_for_each_element(&interfaces, iface, node) {
1067                 if (iface->state != IFS_UP)
1068                         continue;
1069
1070                 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
1071                     vlist_simple_empty(&iface->proto_ip.dns_servers) &&
1072                         vlist_simple_empty(&iface->config_ip.dns_search) &&
1073                     vlist_simple_empty(&iface->config_ip.dns_servers))
1074                         continue;
1075
1076                 fprintf(f, "# Interface %s\n", iface->name);
1077                 write_resolv_conf_entries(f, &iface->config_ip);
1078                 if (!iface->proto_ip.no_dns)
1079                         write_resolv_conf_entries(f, &iface->proto_ip);
1080         }
1081         fflush(f);
1082         rewind(f);
1083         crcnew = crc32_file(f);
1084         fclose(f);
1085
1086         crcold = crcnew + 1;
1087         f = fopen(resolv_conf, "r");
1088         if (f) {
1089                 crcold = crc32_file(f);
1090                 fclose(f);
1091         }
1092
1093         if (crcold == crcnew) {
1094                 unlink(path);
1095         } else if (rename(path, resolv_conf) < 0) {
1096                 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
1097                 unlink(path);
1098         }
1099 }
1100
1101 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
1102 {
1103         struct device_addr *addr;
1104         struct device_route *route;
1105         struct device *dev;
1106
1107         ip->enabled = enabled;
1108         dev = ip->iface->l3_dev.dev;
1109         if (!dev)
1110                 return;
1111
1112         vlist_for_each_element(&ip->addr, addr, node) {
1113                 if (addr->enabled == enabled)
1114                         continue;
1115
1116                 if (enabled)
1117                         system_add_address(dev, addr);
1118                 else
1119                         system_del_address(dev, addr);
1120                 addr->enabled = enabled;
1121         }
1122
1123         vlist_for_each_element(&ip->route, route, node) {
1124                 bool _enabled = enabled;
1125
1126                 if (!enable_route(ip, route))
1127                         _enabled = false;
1128
1129                 if (route->enabled == _enabled)
1130                         continue;
1131
1132                 if (_enabled) {
1133                         if (!(route->flags & DEVROUTE_METRIC))
1134                                 route->metric = ip->iface->metric;
1135
1136                         system_add_route(dev, route);
1137                 } else
1138                         system_del_route(dev, route);
1139                 route->enabled = _enabled;
1140         }
1141
1142         struct device_prefix *c;
1143         struct device_prefix_assignment *a;
1144         list_for_each_entry(c, &prefixes, head)
1145                 list_for_each_entry(a, &c->assignments, head)
1146                         if (!strcmp(a->name, ip->iface->name))
1147                                 interface_set_prefix_address(a, c, ip->iface, enabled);
1148
1149         if (ip->iface && ip->iface->l3_dev.dev) {
1150                 set_ip_lo_policy(enabled, true, ip->iface);
1151                 set_ip_lo_policy(enabled, false, ip->iface);
1152
1153                 set_ip_source_policy(enabled, true, IPRULE_PRIORITY_REJECT + ip->iface->l3_dev.dev->ifindex,
1154                         NULL, 0, 0, ip->iface, "failed_policy");
1155         }
1156 }
1157
1158 void
1159 interface_ip_update_start(struct interface_ip_settings *ip)
1160 {
1161         if (ip != &ip->iface->config_ip) {
1162                 vlist_simple_update(&ip->dns_servers);
1163                 vlist_simple_update(&ip->dns_search);
1164         }
1165         vlist_update(&ip->route);
1166         vlist_update(&ip->addr);
1167         vlist_update(&ip->prefix);
1168 }
1169
1170 void
1171 interface_ip_update_complete(struct interface_ip_settings *ip)
1172 {
1173         vlist_simple_flush(&ip->dns_servers);
1174         vlist_simple_flush(&ip->dns_search);
1175         vlist_flush(&ip->route);
1176         vlist_flush(&ip->addr);
1177         vlist_flush(&ip->prefix);
1178         interface_write_resolv_conf();
1179 }
1180
1181 void
1182 interface_ip_flush(struct interface_ip_settings *ip)
1183 {
1184         if (ip == &ip->iface->proto_ip)
1185                 vlist_flush_all(&ip->iface->host_routes);
1186         vlist_simple_flush_all(&ip->dns_servers);
1187         vlist_simple_flush_all(&ip->dns_search);
1188         vlist_flush_all(&ip->route);
1189         vlist_flush_all(&ip->addr);
1190         vlist_flush_all(&ip->prefix);
1191 }
1192
1193 static void
1194 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
1195 {
1196         ip->iface = iface;
1197         ip->enabled = true;
1198         vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
1199         vlist_simple_init(&ip->dns_servers, struct dns_server, node);
1200         vlist_init(&ip->route, route_cmp, interface_update_proto_route);
1201         vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
1202         vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
1203 }
1204
1205 void
1206 interface_ip_init(struct interface *iface)
1207 {
1208         __interface_ip_init(&iface->proto_ip, iface);
1209         __interface_ip_init(&iface->config_ip, iface);
1210         vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
1211
1212 }
1213
1214 static void
1215 interface_ip_valid_until_handler(struct uloop_timeout *t)
1216 {
1217         time_t now = system_get_rtime();
1218         struct interface *iface;
1219         vlist_for_each_element(&interfaces, iface, node) {
1220                 if (iface->state != IFS_UP)
1221                         continue;
1222
1223                 struct device_addr *addr, *addrp;
1224                 struct device_route *route, *routep;
1225                 struct device_prefix *pref, *prefp;
1226
1227                 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
1228                         if (addr->valid_until && addr->valid_until < now)
1229                                 vlist_delete(&iface->proto_ip.addr, &addr->node);
1230
1231                 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
1232                         if (route->valid_until && route->valid_until < now)
1233                                 vlist_delete(&iface->proto_ip.route, &route->node);
1234
1235                 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
1236                         if (pref->valid_until && pref->valid_until < now)
1237                                 vlist_delete(&iface->proto_ip.prefix, &pref->node);
1238
1239         }
1240
1241         uloop_timeout_set(t, 1000);
1242 }
1243
1244 static void __init
1245 interface_ip_init_worker(void)
1246 {
1247         valid_until_timeout.cb = interface_ip_valid_until_handler;
1248         uloop_timeout_set(&valid_until_timeout, 1000);
1249 }