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