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