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