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