interface-ip: add missing IPv6 policy rule
[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         if (IN6_IS_ADDR_UNSPECIFIED(&assignment->addr)) {
765                 addr.addr.in6 = prefix->addr;
766                 addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
767                 generate_ifaceid(iface, &addr.addr.in6);
768                 assignment->addr = addr.addr.in6;
769         }
770         else
771                 addr.addr.in6 = assignment->addr;
772
773         addr.mask = assignment->length;
774         addr.flags = DEVADDR_INET6 | DEVADDR_OFFLINK;
775         addr.preferred_until = prefix->preferred_until;
776         addr.valid_until = prefix->valid_until;
777
778         route.flags = DEVADDR_INET6;
779         route.mask = addr.mask < 64 ? 64 : addr.mask;
780         route.addr = addr.addr;
781         clear_if_addr(&route.addr, route.mask);
782         interface_set_route_info(iface, &route);
783
784         if (!add && assignment->enabled) {
785                 time_t now = system_get_rtime();
786                 addr.preferred_until = now;
787                 if (!addr.valid_until || addr.valid_until - now > 7200)
788                         addr.valid_until = now + 7200;
789
790                 if (iface->ip6table)
791                         set_ip_source_policy(false, true, IPRULE_PRIORITY_ADDR_MASK, &addr.addr,
792                                         addr.mask < 64 ? 64 : addr.mask, iface->ip6table, NULL, NULL, false);
793
794                 if (prefix->iface) {
795                         if (prefix->iface->ip6table)
796                                 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW, &addr.addr,
797                                                 addr.mask, prefix->iface->ip6table, iface, NULL, true);
798
799                         set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT, &addr.addr,
800                                                         addr.mask, 0, iface, "unreachable", true);
801                 }
802
803                 system_del_route(l3_downlink, &route);
804                 system_add_address(l3_downlink, &addr);
805
806                 assignment->enabled = false;
807         } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP) &&
808                         !system_add_address(l3_downlink, &addr)) {
809
810                 if (!assignment->enabled) {
811                         if (iface->ip6table)
812                                 set_ip_source_policy(true, true, IPRULE_PRIORITY_ADDR_MASK, &addr.addr,
813                                                 addr.mask < 64 ? 64 : addr.mask, iface->ip6table, NULL, NULL, false);
814
815                         if (prefix->iface) {
816                                 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT, &addr.addr,
817                                                 addr.mask, 0, iface, "unreachable", true);
818
819                                 if (prefix->iface->ip6table)
820                                         set_ip_source_policy(true, true, IPRULE_PRIORITY_NW, &addr.addr,
821                                                         addr.mask, prefix->iface->ip6table, iface, NULL, true);
822                         }
823                 }
824
825                 route.metric = iface->metric;
826                 system_add_route(l3_downlink, &route);
827
828                 if (uplink && uplink->l3_dev.dev && !(l3_downlink->settings.flags & DEV_OPT_MTU6)) {
829                         int mtu = system_update_ipv6_mtu(uplink->l3_dev.dev, 0);
830                         int mtu_old = system_update_ipv6_mtu(l3_downlink, 0);
831
832                         if (mtu > 0 && mtu_old > mtu)
833                                 system_update_ipv6_mtu(l3_downlink, mtu);
834                 }
835
836                 assignment->enabled = true;
837         }
838 }
839
840 static bool interface_prefix_assign(struct list_head *list,
841                 struct device_prefix_assignment *assign)
842 {
843         int32_t current = 0, asize = (1 << (64 - assign->length)) - 1;
844         struct device_prefix_assignment *c;
845         list_for_each_entry(c, list, head) {
846                 if (assign->assigned != -1) {
847                         if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
848                                 list_add_tail(&assign->head, &c->head);
849                                 return true;
850                         }
851                 } else if (assign->assigned == -1) {
852                         current = (current + asize) & (~asize);
853                         if (current + asize < c->assigned) {
854                                 assign->assigned = current;
855                                 list_add_tail(&assign->head, &c->head);
856                                 return true;
857                         }
858                 }
859                 current = (c->assigned + (1 << (64 - c->length)));
860         }
861         return false;
862 }
863
864 /*
865  * Sorting of assignment entries:
866  * Primary on assignment length: smallest assignment first
867  * Secondary on assignment weight: highest weight first
868  * Finally alphabetical order of interface names
869  */
870 static int prefix_assignment_cmp(const void *k1, const void *k2, void *ptr)
871 {
872         const struct device_prefix_assignment *a1 = k1, *a2 = k2;
873
874         if (a1->length != a2->length)
875                 return a1->length - a2->length;
876
877         if (a1->weight != a2->weight)
878                 return a2->weight - a1->weight;
879
880         return strcmp(a1->name, a2->name);
881 }
882
883 static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup)
884 {
885         struct device_prefix_assignment *c;
886         struct interface *iface;
887
888         // Delete all assignments
889         while (!list_empty(&prefix->assignments)) {
890                 c = list_first_entry(&prefix->assignments,
891                                 struct device_prefix_assignment, head);
892                 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
893                         interface_set_prefix_address(c, prefix, iface, false);
894                 list_del(&c->head);
895                 free(c);
896         }
897
898         if (!setup)
899                 return;
900
901         // End-of-assignment sentinel
902         c = malloc(sizeof(*c) + 1);
903         if (!c)
904                 return;
905
906         c->assigned = 1 << (64 - prefix->length);
907         c->length = 64;
908         c->name[0] = 0;
909         c->addr = in6addr_any;
910         list_add(&c->head, &prefix->assignments);
911
912         // Excluded prefix
913         if (prefix->excl_length > 0) {
914                 const char name[] = "!excluded";
915                 c = malloc(sizeof(*c) + sizeof(name));
916                 if (c) {
917                         c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
918                                         ((1 << (64 - prefix->length)) - 1);
919                         c->length = prefix->excl_length;
920                         c->addr = in6addr_any;
921                         memcpy(c->name, name, sizeof(name));
922                         list_add(&c->head, &prefix->assignments);
923                 }
924         }
925
926         bool assigned_any = false;
927         struct {
928                 struct avl_node node;
929         } *entry, *n_entry;
930         struct avl_tree assign_later;
931
932         avl_init(&assign_later, prefix_assignment_cmp, false, NULL);
933
934         vlist_for_each_element(&interfaces, iface, node) {
935                 if (iface->assignment_length < 48 ||
936                                 iface->assignment_length > 64)
937                         continue;
938
939                 // Test whether there is a matching class
940                 if (!list_empty(&iface->assignment_classes)) {
941                         bool found = false;
942
943                         struct interface_assignment_class *c;
944                         list_for_each_entry(c, &iface->assignment_classes, head) {
945                                 if (!strcmp(c->name, prefix->pclass)) {
946                                         found = true;
947                                         break;
948                                 }
949                         }
950
951                         if (!found)
952                                 continue;
953                 }
954
955                 size_t namelen = strlen(iface->name) + 1;
956                 c = malloc(sizeof(*c) + namelen);
957                 if (!c)
958                         continue;
959
960                 c->length = iface->assignment_length;
961                 c->assigned = iface->assignment_hint;
962                 c->weight = iface->assignment_weight;
963                 c->addr = in6addr_any;
964                 c->enabled = false;
965                 memcpy(c->name, iface->name, namelen);
966
967                 // First process all custom assignments, put all others in later-list
968                 if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
969                         if (c->assigned != -1) {
970                                 c->assigned = -1;
971                                 netifd_log_message(L_WARNING, "Failed to assign requested subprefix "
972                                                 "of size %hhu for %s, trying other\n", c->length, c->name);
973                         }
974
975                         entry = calloc(1, sizeof(*entry));
976                         if (!entry)
977                                 continue;
978
979                         entry->node.key = c;
980                         avl_insert(&assign_later, &entry->node);
981                 }
982
983                 if (c->assigned != -1)
984                         assigned_any = true;
985         }
986
987         /* Then try to assign all other + failed custom assignments */
988         avl_for_each_element_safe(&assign_later, entry, node, n_entry) {
989                 bool assigned = false;
990
991                 c = (struct device_prefix_assignment *)entry->node.key;
992                 avl_delete(&assign_later, &entry->node);
993
994                 do {
995                         assigned = interface_prefix_assign(&prefix->assignments, c);
996                 } while (!assigned && ++c->length <= 64);
997
998                 if (!assigned) {
999                         netifd_log_message(L_WARNING, "Failed to assign subprefix "
1000                                         "of size %hhu for %s\n", c->length, c->name);
1001                         free(c);
1002                 } else
1003                         assigned_any = true;
1004
1005                 free(entry);
1006         }
1007
1008         list_for_each_entry(c, &prefix->assignments, head)
1009                 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1010                         interface_set_prefix_address(c, prefix, iface, true);
1011
1012         if (!assigned_any)
1013                 netifd_log_message(L_WARNING, "You have delegated IPv6-prefixes but haven't assigned them "
1014                                 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
1015 }
1016
1017
1018 void interface_refresh_assignments(bool hint)
1019 {
1020         static bool refresh = false;
1021         if (!hint && refresh) {
1022                 struct device_prefix *p;
1023                 list_for_each_entry(p, &prefixes, head)
1024                         interface_update_prefix_assignments(p, true);
1025         }
1026         refresh = hint;
1027 }
1028
1029
1030 static void
1031 interface_update_prefix(struct vlist_tree *tree,
1032                              struct vlist_node *node_new,
1033                              struct vlist_node *node_old)
1034 {
1035         struct device_prefix *prefix_old, *prefix_new;
1036         prefix_old = container_of(node_old, struct device_prefix, node);
1037         prefix_new = container_of(node_new, struct device_prefix, node);
1038
1039         struct interface_ip_settings *ip = container_of(tree, struct interface_ip_settings, prefix);
1040         if (tree && (!node_new || !node_old))
1041                 ip->iface->updated |= IUF_PREFIX;
1042
1043         struct device_route route;
1044         memset(&route, 0, sizeof(route));
1045         route.flags = DEVADDR_INET6;
1046         route.metric = INT32_MAX;
1047         route.mask = (node_new) ? prefix_new->length : prefix_old->length;
1048         route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
1049
1050
1051         struct device_prefix_assignment *c;
1052         struct interface *iface;
1053
1054         if (node_old && node_new) {
1055                 // Move assignments and refresh addresses to update valid times
1056                 list_splice(&prefix_old->assignments, &prefix_new->assignments);
1057
1058                 list_for_each_entry(c, &prefix_new->assignments, head)
1059                         if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1060                                 interface_set_prefix_address(c, prefix_new, iface, true);
1061
1062                 if (prefix_new->preferred_until != prefix_old->preferred_until ||
1063                                 prefix_new->valid_until != prefix_old->valid_until)
1064                         ip->iface->updated |= IUF_PREFIX;
1065         } else if (node_new) {
1066                 // Set null-route to avoid routing loops
1067                 system_add_route(NULL, &route);
1068
1069                 if (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation)
1070                         interface_update_prefix_assignments(prefix_new, true);
1071         } else if (node_old) {
1072                 // Remove null-route
1073                 interface_update_prefix_assignments(prefix_old, false);
1074                 system_del_route(NULL, &route);
1075         }
1076
1077         if (node_old) {
1078                 if (prefix_old->head.next)
1079                         list_del(&prefix_old->head);
1080                 free(prefix_old);
1081         }
1082
1083         if (node_new && (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation))
1084                 list_add(&prefix_new->head, &prefixes);
1085
1086 }
1087
1088 struct device_prefix*
1089 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
1090                 uint8_t length, time_t valid_until, time_t preferred_until,
1091                 struct in6_addr *excl_addr, uint8_t excl_length, const char *pclass)
1092 {
1093         if (!pclass)
1094                 pclass = (iface) ? iface->name : "local";
1095
1096         struct device_prefix *prefix = calloc(1, sizeof(*prefix) + strlen(pclass) + 1);
1097         if (!prefix)
1098                 return NULL;
1099
1100         prefix->length = length;
1101         prefix->addr = *addr;
1102         prefix->preferred_until = preferred_until;
1103         prefix->valid_until = valid_until;
1104         prefix->iface = iface;
1105         INIT_LIST_HEAD(&prefix->assignments);
1106
1107         if (excl_addr) {
1108                 prefix->excl_addr = *excl_addr;
1109                 prefix->excl_length = excl_length;
1110         }
1111
1112         strcpy(prefix->pclass, pclass);
1113
1114         if (iface)
1115                 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
1116         else
1117                 interface_update_prefix(NULL, &prefix->node, NULL);
1118
1119         return prefix;
1120 }
1121
1122 void
1123 interface_ip_set_ula_prefix(const char *prefix)
1124 {
1125         char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
1126         if (prefix)
1127                 strncpy(buf, prefix, sizeof(buf) - 1);
1128         char *prefixaddr = strtok_r(buf, "/", &saveptr);
1129
1130         struct in6_addr addr;
1131         if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
1132                 if (ula_prefix) {
1133                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
1134                         ula_prefix = NULL;
1135                 }
1136                 return;
1137         }
1138
1139         int length;
1140         char *prefixlen = strtok_r(NULL, ",", &saveptr);
1141         if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
1142                 return;
1143
1144         if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
1145                         ula_prefix->length != length) {
1146                 if (ula_prefix)
1147                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
1148
1149                 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
1150                                 0, 0, NULL, 0, NULL);
1151         }
1152 }
1153
1154 void
1155 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
1156 {
1157         struct dns_server *s;
1158
1159         s = calloc(1, sizeof(*s));
1160         if (!s)
1161                 return;
1162
1163         s->af = AF_INET;
1164         if (inet_pton(s->af, str, &s->addr.in))
1165                 goto add;
1166
1167         s->af = AF_INET6;
1168         if (inet_pton(s->af, str, &s->addr.in))
1169                 goto add;
1170
1171         free(s);
1172         return;
1173
1174 add:
1175         D(INTERFACE, "Add IPv%c DNS server: %s\n",
1176           s->af == AF_INET6 ? '6' : '4', str);
1177         vlist_simple_add(&ip->dns_servers, &s->node);
1178 }
1179
1180 void
1181 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
1182 {
1183         struct blob_attr *cur;
1184         int rem;
1185
1186         blobmsg_for_each_attr(cur, list, rem) {
1187                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1188                         continue;
1189
1190                 if (!blobmsg_check_attr(cur, NULL))
1191                         continue;
1192
1193                 interface_add_dns_server(ip, blobmsg_data(cur));
1194         }
1195 }
1196
1197 static void
1198 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
1199 {
1200         struct dns_search_domain *s;
1201         int len = strlen(str);
1202
1203         s = calloc(1, sizeof(*s) + len + 1);
1204         if (!s)
1205                 return;
1206
1207         D(INTERFACE, "Add DNS search domain: %s\n", str);
1208         memcpy(s->name, str, len);
1209         vlist_simple_add(&ip->dns_search, &s->node);
1210 }
1211
1212 void
1213 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
1214 {
1215         struct blob_attr *cur;
1216         int rem;
1217
1218         blobmsg_for_each_attr(cur, list, rem) {
1219                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1220                         continue;
1221
1222                 if (!blobmsg_check_attr(cur, NULL))
1223                         continue;
1224
1225                 interface_add_dns_search_domain(ip, blobmsg_data(cur));
1226         }
1227 }
1228
1229 static void
1230 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip, const char *dev)
1231 {
1232         struct dns_server *s;
1233         struct dns_search_domain *d;
1234         const char *str;
1235         char buf[INET6_ADDRSTRLEN];
1236
1237         vlist_simple_for_each_element(&ip->dns_servers, s, node) {
1238                 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
1239                 if (!str)
1240                         continue;
1241
1242                 if (s->af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&s->addr.in6))
1243                         fprintf(f, "nameserver %s%%%s\n", str, dev);
1244                 else
1245                         fprintf(f, "nameserver %s\n", str);
1246         }
1247
1248         vlist_simple_for_each_element(&ip->dns_search, d, node) {
1249                 fprintf(f, "search %s\n", d->name);
1250         }
1251 }
1252
1253 /* Sorting of interface resolver entries :               */
1254 /* Primary on interface dns_metric : lowest metric first */
1255 /* Secondary on interface metric : lowest metric first   */
1256 /* Finally alphabetical order of interface names         */
1257 static int resolv_conf_iface_cmp(const void *k1, const void *k2, void *ptr)
1258 {
1259         const struct interface *iface1 = k1, *iface2 = k2;
1260
1261         if (iface1->dns_metric != iface2->dns_metric)
1262                 return iface1->dns_metric - iface2->dns_metric;
1263
1264         if (iface1->metric != iface2->metric)
1265                 return iface1->metric - iface2->metric;
1266
1267         return strcmp(iface1->name, iface2->name);
1268 }
1269
1270 static void
1271 __interface_write_dns_entries(FILE *f)
1272 {
1273         struct interface *iface;
1274         struct {
1275                 struct avl_node node;
1276         } *entry, *n_entry;
1277         struct avl_tree resolv_conf_iface_entries;
1278
1279         avl_init(&resolv_conf_iface_entries, resolv_conf_iface_cmp, false, NULL);
1280
1281         vlist_for_each_element(&interfaces, iface, node) {
1282                 if (iface->state != IFS_UP)
1283                         continue;
1284
1285                 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
1286                     vlist_simple_empty(&iface->proto_ip.dns_servers) &&
1287                     vlist_simple_empty(&iface->config_ip.dns_search) &&
1288                     vlist_simple_empty(&iface->config_ip.dns_servers))
1289                         continue;
1290
1291                 entry = calloc(1, sizeof(*entry));
1292                 if (!entry)
1293                         continue;
1294
1295                 entry->node.key = iface;
1296                 avl_insert(&resolv_conf_iface_entries, &entry->node);
1297         }
1298
1299         avl_for_each_element(&resolv_conf_iface_entries, entry, node) {
1300                 iface = (struct interface *)entry->node.key;
1301                 struct device *dev = iface->l3_dev.dev;
1302
1303                 fprintf(f, "# Interface %s\n", iface->name);
1304
1305                 write_resolv_conf_entries(f, &iface->config_ip, dev->ifname);
1306
1307                 if (!iface->proto_ip.no_dns)
1308                         write_resolv_conf_entries(f, &iface->proto_ip, dev->ifname);
1309         }
1310
1311         avl_remove_all_elements(&resolv_conf_iface_entries, entry, node, n_entry)
1312                 free(entry);
1313 }
1314
1315 void
1316 interface_write_resolv_conf(void)
1317 {
1318         char *path = alloca(strlen(resolv_conf) + 5);
1319         FILE *f;
1320         uint32_t crcold, crcnew;
1321
1322         sprintf(path, "%s.tmp", resolv_conf);
1323         unlink(path);
1324         f = fopen(path, "w+");
1325         if (!f) {
1326                 D(INTERFACE, "Failed to open %s for writing\n", path);
1327                 return;
1328         }
1329
1330         __interface_write_dns_entries(f);
1331
1332         fflush(f);
1333         rewind(f);
1334         crcnew = crc32_file(f);
1335         fclose(f);
1336
1337         crcold = crcnew + 1;
1338         f = fopen(resolv_conf, "r");
1339         if (f) {
1340                 crcold = crc32_file(f);
1341                 fclose(f);
1342         }
1343
1344         if (crcold == crcnew) {
1345                 unlink(path);
1346         } else if (rename(path, resolv_conf) < 0) {
1347                 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
1348                 unlink(path);
1349         }
1350 }
1351
1352 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
1353 {
1354         struct device_addr *addr;
1355         struct device_route *route;
1356         struct device *dev;
1357         struct interface *iface;
1358
1359         ip->enabled = enabled;
1360         iface = ip->iface;
1361         dev = iface->l3_dev.dev;
1362         if (!dev)
1363                 return;
1364
1365         vlist_for_each_element(&ip->addr, addr, node) {
1366                 bool v6 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6) ? true : false;
1367
1368                 if (addr->flags & DEVADDR_EXTERNAL)
1369                         continue;
1370
1371                 if (addr->enabled == enabled)
1372                         continue;
1373
1374                 if (enabled) {
1375                         system_add_address(dev, addr);
1376
1377                         addr->policy_table = (v6) ? iface->ip6table : iface->ip4table;
1378                         if (iface->metric || addr->policy_table)
1379                                 interface_handle_subnet_route(iface, addr, true);
1380
1381                         if (addr->policy_table)
1382                                 interface_add_addr_rules(addr, true);
1383                 } else {
1384                         interface_handle_subnet_route(iface, addr, false);
1385                         system_del_address(dev, addr);
1386
1387                         if (addr->policy_table)
1388                                 interface_add_addr_rules(addr, false);
1389                 }
1390                 addr->enabled = enabled;
1391         }
1392
1393         vlist_for_each_element(&ip->route, route, node) {
1394                 bool _enabled = enabled;
1395
1396                 if (route->flags & DEVADDR_EXTERNAL)
1397                         continue;
1398
1399                 if (!enable_route(ip, route))
1400                         _enabled = false;
1401
1402                 if (route->enabled == _enabled)
1403                         continue;
1404
1405                 if (_enabled) {
1406                         interface_set_route_info(ip->iface, route);
1407
1408                         if (system_add_route(dev, route))
1409                                 route->failed = true;
1410                 } else
1411                         system_del_route(dev, route);
1412                 route->enabled = _enabled;
1413         }
1414
1415         struct device_prefix *c;
1416         struct device_prefix_assignment *a;
1417         list_for_each_entry(c, &prefixes, head)
1418                 list_for_each_entry(a, &c->assignments, head)
1419                         if (!strcmp(a->name, ip->iface->name))
1420                                 interface_set_prefix_address(a, c, ip->iface, enabled);
1421
1422         if (ip->iface && ip->iface->policy_rules_set != enabled &&
1423             ip->iface->l3_dev.dev) {
1424                 set_ip_lo_policy(enabled, true, ip->iface);
1425                 set_ip_lo_policy(enabled, false, ip->iface);
1426
1427                 set_ip_source_policy(enabled, true, IPRULE_PRIORITY_REJECT + ip->iface->l3_dev.dev->ifindex,
1428                         NULL, 0, 0, ip->iface, "failed_policy", true);
1429                 ip->iface->policy_rules_set = enabled;
1430         }
1431 }
1432
1433 void
1434 interface_ip_update_start(struct interface_ip_settings *ip)
1435 {
1436         if (ip != &ip->iface->config_ip) {
1437                 vlist_simple_update(&ip->dns_servers);
1438                 vlist_simple_update(&ip->dns_search);
1439         }
1440         vlist_update(&ip->route);
1441         vlist_update(&ip->addr);
1442         vlist_update(&ip->prefix);
1443 }
1444
1445 void
1446 interface_ip_update_complete(struct interface_ip_settings *ip)
1447 {
1448         vlist_simple_flush(&ip->dns_servers);
1449         vlist_simple_flush(&ip->dns_search);
1450         vlist_flush(&ip->route);
1451         vlist_flush(&ip->addr);
1452         vlist_flush(&ip->prefix);
1453         interface_write_resolv_conf();
1454 }
1455
1456 void
1457 interface_ip_flush(struct interface_ip_settings *ip)
1458 {
1459         if (ip == &ip->iface->proto_ip)
1460                 vlist_flush_all(&ip->iface->host_routes);
1461         vlist_simple_flush_all(&ip->dns_servers);
1462         vlist_simple_flush_all(&ip->dns_search);
1463         vlist_flush_all(&ip->route);
1464         vlist_flush_all(&ip->addr);
1465         vlist_flush_all(&ip->prefix);
1466 }
1467
1468 static void
1469 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
1470 {
1471         ip->iface = iface;
1472         ip->enabled = true;
1473         vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
1474         vlist_simple_init(&ip->dns_servers, struct dns_server, node);
1475         vlist_init(&ip->route, route_cmp, interface_update_proto_route);
1476         vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
1477         vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
1478 }
1479
1480 void
1481 interface_ip_init(struct interface *iface)
1482 {
1483         __interface_ip_init(&iface->proto_ip, iface);
1484         __interface_ip_init(&iface->config_ip, iface);
1485         vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
1486 }
1487
1488 static void
1489 interface_ip_valid_until_handler(struct uloop_timeout *t)
1490 {
1491         time_t now = system_get_rtime();
1492         struct interface *iface;
1493         vlist_for_each_element(&interfaces, iface, node) {
1494                 if (iface->state != IFS_UP)
1495                         continue;
1496
1497                 struct device_addr *addr, *addrp;
1498                 struct device_route *route, *routep;
1499                 struct device_prefix *pref, *prefp;
1500
1501                 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
1502                         if (addr->valid_until && addr->valid_until < now)
1503                                 vlist_delete(&iface->proto_ip.addr, &addr->node);
1504
1505                 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
1506                         if (route->valid_until && route->valid_until < now)
1507                                 vlist_delete(&iface->proto_ip.route, &route->node);
1508
1509                 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
1510                         if (pref->valid_until && pref->valid_until < now)
1511                                 vlist_delete(&iface->proto_ip.prefix, &pref->node);
1512
1513         }
1514
1515         uloop_timeout_set(t, 1000);
1516 }
1517
1518 static void __init
1519 interface_ip_init_worker(void)
1520 {
1521         valid_until_timeout.cb = interface_ip_valid_until_handler;
1522         uloop_timeout_set(&valid_until_timeout, 1000);
1523 }