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