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