ed95b2ee264f3360692ae8f408c7fef40aad07c6
[project/netifd.git] / interface-ip.c
1 /*
2  * netifd - network interface daemon
3  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <unistd.h>
18
19 #include <arpa/inet.h>
20
21 #include "netifd.h"
22 #include "device.h"
23 #include "interface.h"
24 #include "interface-ip.h"
25 #include "proto.h"
26 #include "ubus.h"
27 #include "system.h"
28
29 enum {
30         ROUTE_INTERFACE,
31         ROUTE_TARGET,
32         ROUTE_MASK,
33         ROUTE_GATEWAY,
34         ROUTE_METRIC,
35         ROUTE_MTU,
36         ROUTE_VALID,
37         __ROUTE_MAX
38 };
39
40 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
41         [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
42         [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
43         [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
44         [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
45         [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
46         [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
47         [ROUTE_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
48 };
49
50 const struct config_param_list route_attr_list = {
51         .n_params = __ROUTE_MAX,
52         .params = route_attr,
53 };
54
55
56 struct list_head prefixes = LIST_HEAD_INIT(prefixes);
57 static struct device_prefix *ula_prefix = NULL;
58 static struct uloop_timeout valid_until_timeout;
59
60
61 static void
62 clear_if_addr(union if_addr *a, int mask)
63 {
64         int m_bytes = (mask + 7) / 8;
65         uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
66         uint8_t *p = (uint8_t *) a;
67
68         if (m_bytes < sizeof(a))
69                 memset(p + m_bytes, 0, sizeof(a) - m_bytes);
70
71         p[m_bytes - 1] &= ~m_clear;
72 }
73
74 static bool
75 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
76 {
77         union if_addr *p1, *p2;
78
79         p1 = alloca(sizeof(*a1));
80         p2 = alloca(sizeof(*a2));
81
82         memcpy(p1, a1, sizeof(*a1));
83         clear_if_addr(p1, mask);
84         memcpy(p2, a2, sizeof(*a2));
85         clear_if_addr(p2, mask);
86
87         return !memcmp(p1, p2, sizeof(*p1));
88 }
89
90 static bool
91 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
92 {
93         struct device_addr *addr;
94
95         vlist_for_each_element(&ip->addr, addr, node) {
96                 if (!addr->enabled)
97                         continue;
98
99                 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
100                         continue;
101
102                 if (!match_if_addr(&addr->addr, a, addr->mask))
103                         continue;
104
105                 return true;
106         }
107
108         return false;
109 }
110
111 static void
112 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
113                        bool v6, struct device_route **res)
114 {
115         struct device_route *route;
116
117         vlist_for_each_element(&ip->route, route, node) {
118                 if (!route->enabled)
119                         continue;
120
121                 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
122                         continue;
123
124                 if (!match_if_addr(&route->addr, a, route->mask))
125                         continue;
126
127                 if (!*res || route->mask < (*res)->mask)
128                         *res = route;
129         }
130 }
131
132 static bool
133 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
134 {
135         return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
136                __find_ip_addr_target(&iface->config_ip, a, v6);
137 }
138
139 static void
140 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
141                                bool v6, struct device_route **route)
142 {
143         __find_ip_route_target(&iface->proto_ip, a, v6, route);
144         __find_ip_route_target(&iface->config_ip, a, v6, route);
145 }
146
147 struct interface *
148 interface_ip_add_target_route(union if_addr *addr, bool v6)
149 {
150         struct interface *iface;
151         struct device_route *route, *r_next = NULL;
152         bool defaultroute_target = false;
153         int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
154
155         route = calloc(1, sizeof(*route));
156         if (!route)
157                 return NULL;
158
159         route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
160         route->mask = v6 ? 128 : 32;
161         if (memcmp(&route->addr, addr, addrsize) == 0)
162                 defaultroute_target = true;
163         else
164                 memcpy(&route->addr, addr, addrsize);
165
166         vlist_for_each_element(&interfaces, iface, node) {
167                 /* look for locally addressable target first */
168                 if (interface_ip_find_addr_target(iface, addr, v6))
169                         goto done;
170
171                 /* do not stop at the first route, let the lookup compare
172                  * masks to find the best match */
173                 interface_ip_find_route_target(iface, addr, v6, &r_next);
174         }
175
176         if (!r_next) {
177                 free(route);
178                 return NULL;
179         }
180
181         iface = r_next->iface;
182         memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
183         route->mtu = r_next->mtu;
184         route->metric = r_next->metric;
185
186 done:
187         route->iface = iface;
188         if (defaultroute_target)
189                 free(route);
190         else
191                 vlist_add(&iface->host_routes, &route->node, &route->flags);
192         return iface;
193 }
194
195 void
196 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
197 {
198         struct interface_ip_settings *ip;
199         struct blob_attr *tb[__ROUTE_MAX], *cur;
200         struct device_route *route;
201         int af = v6 ? AF_INET6 : AF_INET;
202
203         blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
204
205         if (!iface) {
206                 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
207                         return;
208
209                 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
210                 if (!iface)
211                         return;
212
213                 ip = &iface->config_ip;
214         } else {
215                 ip = &iface->proto_ip;
216         }
217
218         route = calloc(1, sizeof(*route));
219         if (!route)
220                 return;
221
222         route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
223         route->mask = v6 ? 128 : 32;
224         if ((cur = tb[ROUTE_MASK]) != NULL) {
225                 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
226                 if (route->mask > (v6 ? 128 : 32))
227                         goto error;
228         }
229
230         if ((cur = tb[ROUTE_TARGET]) != NULL) {
231                 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
232                         DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
233                         goto error;
234                 }
235         }
236
237         if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
238                 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
239                         DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
240                         goto error;
241                 }
242         }
243
244         if ((cur = tb[ROUTE_METRIC]) != NULL) {
245                 route->metric = blobmsg_get_u32(cur);
246                 route->flags |= DEVROUTE_METRIC;
247         }
248
249         if ((cur = tb[ROUTE_MTU]) != NULL) {
250                 route->mtu = blobmsg_get_u32(cur);
251                 route->flags |= DEVROUTE_MTU;
252         }
253
254         if ((cur = tb[ROUTE_VALID]) != NULL)
255                 route->valid_until = system_get_rtime() + blobmsg_get_u32(cur);
256
257         vlist_add(&ip->route, &route->node, &route->flags);
258         return;
259
260 error:
261         free(route);
262 }
263
264 static int
265 addr_cmp(const void *k1, const void *k2, void *ptr)
266 {
267         return memcmp(k1, k2, sizeof(struct device_addr) -
268                       offsetof(struct device_addr, flags));
269 }
270
271 static int
272 route_cmp(const void *k1, const void *k2, void *ptr)
273 {
274         return memcmp(k1, k2, sizeof(struct device_route) -
275                       offsetof(struct device_route, flags));
276 }
277
278 static int
279 prefix_cmp(const void *k1, const void *k2, void *ptr)
280 {
281         return memcmp(k1, k2, sizeof(struct device_prefix) -
282                         offsetof(struct device_prefix, addr));
283 }
284
285 static void
286 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
287 {
288         struct device *dev = iface->l3_dev.dev;
289         struct device_route route;
290
291         memset(&route, 0, sizeof(route));
292         route.iface = iface;
293         route.flags = addr->flags;
294         route.mask = addr->mask;
295         memcpy(&route.addr, &addr->addr, sizeof(route.addr));
296         clear_if_addr(&route.addr, route.mask);
297
298         if (add) {
299                 route.flags |= DEVADDR_KERNEL;
300                 system_del_route(dev, &route);
301
302                 if (!(addr->flags & DEVADDR_OFFLINK)) {
303                         route.flags &= ~DEVADDR_KERNEL;
304                         route.metric = iface->metric;
305                         system_add_route(dev, &route);
306                 }
307         } else {
308                 if (!(addr->flags & DEVADDR_OFFLINK))
309                         system_del_route(dev, &route);
310         }
311 }
312
313 static void
314 interface_update_proto_addr(struct vlist_tree *tree,
315                             struct vlist_node *node_new,
316                             struct vlist_node *node_old)
317 {
318         struct interface_ip_settings *ip;
319         struct interface *iface;
320         struct device *dev;
321         struct device_addr *a_new = NULL, *a_old = NULL;
322         bool keep = false;
323
324         ip = container_of(tree, struct interface_ip_settings, addr);
325         iface = ip->iface;
326         dev = iface->l3_dev.dev;
327
328         if (node_new) {
329                 a_new = container_of(node_new, struct device_addr, node);
330
331                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
332                     !a_new->broadcast) {
333
334                         uint32_t mask = ~0;
335                         uint32_t *a = (uint32_t *) &a_new->addr;
336
337                         mask >>= a_new->mask;
338                         a_new->broadcast = *a | htonl(mask);
339                 }
340         }
341
342         if (node_old)
343                 a_old = container_of(node_old, struct device_addr, node);
344
345         if (a_new && a_old) {
346                 keep = true;
347
348                 if (a_old->flags != a_new->flags ||
349                                 a_old->valid_until != a_new->valid_until ||
350                                 a_old->preferred_until != a_new->preferred_until)
351                         keep = false;
352
353                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
354                     a_new->broadcast != a_old->broadcast)
355                         keep = false;
356         }
357
358         if (node_old) {
359                 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep) {
360                         interface_handle_subnet_route(iface, a_old, false);
361                         system_del_address(dev, a_old);
362                 }
363                 free(a_old);
364         }
365
366         if (node_new) {
367                 a_new->enabled = true;
368                 if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep) {
369                         system_add_address(dev, a_new);
370                         if ((a_new->flags & DEVADDR_OFFLINK) || iface->metric)
371                                 interface_handle_subnet_route(iface, a_new, true);
372                 }
373         }
374 }
375
376 static bool
377 enable_route(struct interface_ip_settings *ip, struct device_route *route)
378 {
379         if (ip->no_defaultroute && !route->mask)
380                 return false;
381
382         return ip->enabled;
383 }
384
385 static void
386 interface_update_proto_route(struct vlist_tree *tree,
387                              struct vlist_node *node_new,
388                              struct vlist_node *node_old)
389 {
390         struct interface_ip_settings *ip;
391         struct interface *iface;
392         struct device *dev;
393         struct device_route *route_old, *route_new;
394         bool keep = false;
395
396         ip = container_of(tree, struct interface_ip_settings, route);
397         iface = ip->iface;
398         dev = iface->l3_dev.dev;
399
400         route_old = container_of(node_old, struct device_route, node);
401         route_new = container_of(node_new, struct device_route, node);
402
403         if (node_old && node_new)
404                 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
405
406         if (node_old) {
407                 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
408                         system_del_route(dev, route_old);
409                 free(route_old);
410         }
411
412         if (node_new) {
413                 bool _enabled = enable_route(ip, route_new);
414
415                 if (!(route_new->flags & DEVROUTE_METRIC))
416                         route_new->metric = iface->metric;
417
418                 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
419                         system_add_route(dev, route_new);
420
421                 route_new->iface = iface;
422                 route_new->enabled = _enabled;
423         }
424 }
425
426 static void
427 interface_update_host_route(struct vlist_tree *tree,
428                              struct vlist_node *node_new,
429                              struct vlist_node *node_old)
430 {
431         struct interface *iface;
432         struct device *dev;
433         struct device_route *route_old, *route_new;
434
435         iface = container_of(tree, struct interface, host_routes);
436         dev = iface->l3_dev.dev;
437
438         route_old = container_of(node_old, struct device_route, node);
439         route_new = container_of(node_new, struct device_route, node);
440
441         if (node_old) {
442                 system_del_route(dev, route_old);
443                 free(route_old);
444         }
445
446         if (node_new)
447                 system_add_route(dev, route_new);
448 }
449
450
451 static void
452 interface_set_prefix_address(struct device_prefix_assignment *assignment,
453                 const struct device_prefix *prefix, struct interface *iface, bool add)
454 {
455         const struct interface *uplink = prefix->iface;
456         if (!iface->l3_dev.dev)
457                 return;
458
459         struct device *l3_downlink = iface->l3_dev.dev;
460
461         struct device_addr addr;
462         memset(&addr, 0, sizeof(addr));
463         addr.addr.in6 = prefix->addr;
464         addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
465         addr.addr.in6.s6_addr[15] += 1;
466         addr.mask = assignment->length;
467         addr.flags = DEVADDR_INET6;
468         addr.preferred_until = prefix->preferred_until;
469         addr.valid_until = prefix->valid_until;
470
471         if (!add && assignment->enabled) {
472                 time_t now = system_get_rtime();
473                 addr.preferred_until = now;
474                 if (!addr.valid_until || addr.valid_until - now > 7200)
475                         addr.valid_until = now + 7200;
476                 system_add_address(l3_downlink, &addr);
477                 assignment->enabled = false;
478         } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP)) {
479                 system_add_address(l3_downlink, &addr);
480                 if (uplink && uplink->l3_dev.dev) {
481                         int mtu = system_update_ipv6_mtu(
482                                         uplink->l3_dev.dev, 0);
483                         if (mtu > 0)
484                                 system_update_ipv6_mtu(l3_downlink, mtu);
485                 }
486                 assignment->enabled = true;
487         }
488 }
489
490 static bool interface_prefix_assign(struct list_head *list,
491                 struct device_prefix_assignment *assign)
492 {
493         int32_t current = 0, asize = (1 << (64 - assign->length)) - 1;
494         struct device_prefix_assignment *c;
495         list_for_each_entry(c, list, head) {
496                 if (assign->assigned != -1) {
497                         if (assign->assigned > current && assign->assigned + asize < c->assigned) {
498                                 list_add_tail(&assign->head, &c->head);
499                                 return true;
500                         }
501                 } else if (assign->assigned == -1) {
502                         current = (current + asize) & (~asize);
503                         if (current + asize < c->assigned) {
504                                 assign->assigned = current;
505                                 list_add_tail(&assign->head, &c->head);
506                                 return true;
507                         }
508                 }
509                 current = (c->assigned + (1 << (64 - c->length)));
510         }
511         return false;
512 }
513
514 static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup)
515 {
516         struct device_prefix_assignment *c;
517         struct interface *iface;
518
519         // Delete all assignments
520         while (!list_empty(&prefix->assignments)) {
521                 c = list_first_entry(&prefix->assignments,
522                                 struct device_prefix_assignment, head);
523                 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
524                         interface_set_prefix_address(c, prefix, iface, false);
525                 list_del(&c->head);
526                 free(c);
527         }
528
529         if (!setup)
530                 return;
531
532         // End-of-assignment sentinel
533         c = malloc(sizeof(*c) + 1);
534         c->assigned = 1 << (64 - prefix->length);
535         c->length = 64;
536         c->name[0] = 0;
537         list_add(&c->head, &prefix->assignments);
538
539         // Excluded prefix
540         if (prefix->excl_length > 0) {
541                 const char name[] = "!excluded";
542                 c = malloc(sizeof(*c) + sizeof(name));
543                 c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
544                                 ((1 << (64 - prefix->length)) - 1);
545                 c->length = prefix->excl_length;
546                 memcpy(c->name, name, sizeof(name));
547                 list_add(&c->head, &prefix->assignments);
548         }
549
550         struct list_head assign_later = LIST_HEAD_INIT(assign_later);
551         vlist_for_each_element(&interfaces, iface, node) {
552                 if (iface->config_ip.assignment_length < 48 ||
553                                 iface->config_ip.assignment_length > 64)
554                         continue;
555
556                 size_t namelen = strlen(iface->name) + 1;
557                 c = malloc(sizeof(*c) + namelen);
558                 c->length = iface->config_ip.assignment_length;
559                 c->assigned = iface->config_ip.assignment_hint;
560                 c->enabled = false;
561                 memcpy(c->name, iface->name, namelen);
562
563                 // First process all custom assignments, put all others in later-list
564                 if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
565                         if (c->assigned != -1) {
566                                 c->assigned = -1;
567                                 netifd_log_message(L_WARNING, "Failed to assign requested subprefix "
568                                                 "of size %hhu for %s, trying other\n", c->length, c->name);
569                         }
570                         list_add_tail(&c->head, &assign_later);
571                 }
572         }
573
574         // Then try to assign all other + failed custom assignments
575         while (!list_empty(&assign_later)) {
576                 c = list_first_entry(&assign_later, struct device_prefix_assignment, head);
577                 list_del(&c->head);
578                 if (!interface_prefix_assign(&prefix->assignments, c)) {
579                         netifd_log_message(L_WARNING, "Failed to assign subprefix "
580                                         "of size %hhu for %s\n", c->length, c->name);
581                         free(c);
582                 }
583         }
584
585         list_for_each_entry(c, &prefix->assignments, head)
586                 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
587                         interface_set_prefix_address(c, prefix, iface, true);
588 }
589
590
591 void interface_refresh_assignments(bool hint)
592 {
593         static bool refresh = false;
594         if (!hint && refresh) {
595                 struct device_prefix *p;
596                 list_for_each_entry(p, &prefixes, head)
597                         interface_update_prefix_assignments(p, true);
598         }
599         refresh = hint;
600 }
601
602
603 static void
604 interface_update_prefix(struct vlist_tree *tree,
605                              struct vlist_node *node_new,
606                              struct vlist_node *node_old)
607 {
608         struct device_prefix *prefix_old, *prefix_new;
609         prefix_old = container_of(node_old, struct device_prefix, node);
610         prefix_new = container_of(node_new, struct device_prefix, node);
611
612         struct device_route route;
613         memset(&route, 0, sizeof(route));
614         route.flags = DEVADDR_INET6;
615         route.metric = INT32_MAX;
616         route.mask = (node_new) ? prefix_new->length : prefix_old->length;
617         route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
618
619         struct device_prefix_assignment *c;
620         struct interface *iface;
621
622         if (node_old && node_new) {
623                 // Move assignments and refresh addresses to update valid times
624                 list_splice_init(&prefix_old->assignments, &prefix_new->assignments);
625
626                 list_for_each_entry(c, &prefix_new->assignments, head)
627                         if ((iface = vlist_find(&interfaces, c->name, iface, node)))
628                                 interface_set_prefix_address(c, prefix_new, iface, true);
629         } else if (node_new) {
630                 // Set null-route to avoid routing loops
631                 system_add_route(NULL, &route);
632
633                 INIT_LIST_HEAD(&prefix_new->assignments);
634                 interface_update_prefix_assignments(prefix_new, true);
635         } else if (node_old) {
636                 interface_update_prefix_assignments(prefix_old, false);
637
638                 // Remove null-route
639                 system_del_route(NULL, &route);
640         }
641
642         if (node_old) {
643                 list_del(&prefix_old->head);
644                 free(prefix_old);
645         }
646
647         if (node_new)
648                 list_add(&prefix_new->head, &prefixes);
649
650 }
651
652 struct device_prefix*
653 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
654                 uint8_t length, time_t valid_until, time_t preferred_until,
655                 struct in6_addr *excl_addr, uint8_t excl_length)
656 {
657         struct device_prefix *prefix = calloc(1, sizeof(*prefix));
658         prefix->length = length;
659         prefix->addr = *addr;
660         prefix->preferred_until = preferred_until;
661         prefix->valid_until = valid_until;
662         prefix->iface = iface;
663
664         if (excl_addr) {
665                 prefix->excl_addr = *excl_addr;
666                 prefix->excl_length = excl_length;
667         }
668
669         if (iface)
670                 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
671         else
672                 interface_update_prefix(NULL, &prefix->node, NULL);
673
674         return prefix;
675 }
676
677 void
678 interface_ip_set_ula_prefix(const char *prefix)
679 {
680         char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
681         if (prefix)
682                 strncpy(buf, prefix, sizeof(buf) - 1);
683         char *prefixaddr = strtok_r(buf, "/", &saveptr);
684
685         struct in6_addr addr;
686         if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
687                 if (ula_prefix) {
688                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
689                         ula_prefix = NULL;
690                 }
691                 return;
692         }
693
694         int length;
695         char *prefixlen = strtok_r(NULL, ",", &saveptr);
696         if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
697                 return;
698
699         if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
700                         ula_prefix->length != length) {
701                 if (ula_prefix)
702                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
703
704                 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
705                                 0, 0, NULL, 0);
706         }
707 }
708
709 void
710 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
711 {
712         struct dns_server *s;
713
714         s = calloc(1, sizeof(*s));
715         if (!s)
716                 return;
717
718         s->af = AF_INET;
719         if (inet_pton(s->af, str, &s->addr.in))
720                 goto add;
721
722         s->af = AF_INET6;
723         if (inet_pton(s->af, str, &s->addr.in))
724                 goto add;
725
726         free(s);
727         return;
728
729 add:
730         D(INTERFACE, "Add IPv%c DNS server: %s\n",
731           s->af == AF_INET6 ? '6' : '4', str);
732         vlist_simple_add(&ip->dns_servers, &s->node);
733 }
734
735 void
736 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
737 {
738         struct blob_attr *cur;
739         int rem;
740
741         blobmsg_for_each_attr(cur, list, rem) {
742                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
743                         continue;
744
745                 if (!blobmsg_check_attr(cur, NULL))
746                         continue;
747
748                 interface_add_dns_server(ip, blobmsg_data(cur));
749         }
750 }
751
752 static void
753 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
754 {
755         struct dns_search_domain *s;
756         int len = strlen(str);
757
758         s = calloc(1, sizeof(*s) + len + 1);
759         if (!s)
760                 return;
761
762         D(INTERFACE, "Add DNS search domain: %s\n", str);
763         memcpy(s->name, str, len);
764         vlist_simple_add(&ip->dns_search, &s->node);
765 }
766
767 void
768 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
769 {
770         struct blob_attr *cur;
771         int rem;
772
773         blobmsg_for_each_attr(cur, list, rem) {
774                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
775                         continue;
776
777                 if (!blobmsg_check_attr(cur, NULL))
778                         continue;
779
780                 interface_add_dns_search_domain(ip, blobmsg_data(cur));
781         }
782 }
783
784 static void
785 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
786 {
787         struct dns_server *s;
788         struct dns_search_domain *d;
789         const char *str;
790         char buf[INET6_ADDRSTRLEN];
791
792         vlist_simple_for_each_element(&ip->dns_servers, s, node) {
793                 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
794                 if (!str)
795                         continue;
796
797                 fprintf(f, "nameserver %s\n", str);
798         }
799
800         vlist_simple_for_each_element(&ip->dns_search, d, node) {
801                 fprintf(f, "search %s\n", d->name);
802         }
803 }
804
805 void
806 interface_write_resolv_conf(void)
807 {
808         struct interface *iface;
809         char *path = alloca(strlen(resolv_conf) + 5);
810         FILE *f;
811         uint32_t crcold, crcnew;
812
813         sprintf(path, "%s.tmp", resolv_conf);
814         unlink(path);
815         f = fopen(path, "w+");
816         if (!f) {
817                 D(INTERFACE, "Failed to open %s for writing\n", path);
818                 return;
819         }
820
821         vlist_for_each_element(&interfaces, iface, node) {
822                 if (iface->state != IFS_UP)
823                         continue;
824
825                 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
826                     vlist_simple_empty(&iface->proto_ip.dns_servers) &&
827                         vlist_simple_empty(&iface->config_ip.dns_search) &&
828                     vlist_simple_empty(&iface->config_ip.dns_servers))
829                         continue;
830
831                 fprintf(f, "# Interface %s\n", iface->name);
832                 write_resolv_conf_entries(f, &iface->config_ip);
833                 if (!iface->proto_ip.no_dns)
834                         write_resolv_conf_entries(f, &iface->proto_ip);
835         }
836         fflush(f);
837         rewind(f);
838         crcnew = crc32_file(f);
839         fclose(f);
840
841         crcold = crcnew + 1;
842         f = fopen(resolv_conf, "r");
843         if (f) {
844                 crcold = crc32_file(f);
845                 fclose(f);
846         }
847
848         if (crcold == crcnew) {
849                 unlink(path);
850         } else if (rename(path, resolv_conf) < 0) {
851                 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
852                 unlink(path);
853         }
854 }
855
856 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
857 {
858         struct device_addr *addr;
859         struct device_route *route;
860         struct device *dev;
861
862         ip->enabled = enabled;
863         dev = ip->iface->l3_dev.dev;
864         if (!dev)
865                 return;
866
867         vlist_for_each_element(&ip->addr, addr, node) {
868                 if (addr->enabled == enabled)
869                         continue;
870
871                 if (enabled)
872                         system_add_address(dev, addr);
873                 else
874                         system_del_address(dev, addr);
875                 addr->enabled = enabled;
876         }
877
878         vlist_for_each_element(&ip->route, route, node) {
879                 bool _enabled = enabled;
880
881                 if (!enable_route(ip, route))
882                         _enabled = false;
883
884                 if (route->enabled == _enabled)
885                         continue;
886
887                 if (_enabled) {
888                         if (!(route->flags & DEVROUTE_METRIC))
889                                 route->metric = ip->iface->metric;
890
891                         system_add_route(dev, route);
892                 } else
893                         system_del_route(dev, route);
894                 route->enabled = _enabled;
895         }
896
897         struct device_prefix *c;
898         struct device_prefix_assignment *a;
899         list_for_each_entry(c, &prefixes, head)
900                 list_for_each_entry(a, &c->assignments, head)
901                         if (!strcmp(a->name, ip->iface->name))
902                                 interface_set_prefix_address(a, c, ip->iface, enabled);
903 }
904
905 void
906 interface_ip_update_start(struct interface_ip_settings *ip)
907 {
908         if (ip != &ip->iface->config_ip) {
909                 vlist_simple_update(&ip->dns_servers);
910                 vlist_simple_update(&ip->dns_search);
911         }
912         vlist_update(&ip->route);
913         vlist_update(&ip->addr);
914         vlist_update(&ip->prefix);
915 }
916
917 void
918 interface_ip_update_complete(struct interface_ip_settings *ip)
919 {
920         vlist_simple_flush(&ip->dns_servers);
921         vlist_simple_flush(&ip->dns_search);
922         vlist_flush(&ip->route);
923         vlist_flush(&ip->addr);
924         vlist_flush(&ip->prefix);
925         interface_write_resolv_conf();
926 }
927
928 void
929 interface_ip_flush(struct interface_ip_settings *ip)
930 {
931         if (ip == &ip->iface->proto_ip)
932                 vlist_flush_all(&ip->iface->host_routes);
933         vlist_simple_flush_all(&ip->dns_servers);
934         vlist_simple_flush_all(&ip->dns_search);
935         vlist_flush_all(&ip->route);
936         vlist_flush_all(&ip->addr);
937         vlist_flush_all(&ip->prefix);
938 }
939
940 static void
941 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
942 {
943         ip->iface = iface;
944         ip->enabled = true;
945         vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
946         vlist_simple_init(&ip->dns_servers, struct dns_server, node);
947         vlist_init(&ip->route, route_cmp, interface_update_proto_route);
948         vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
949         vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
950 }
951
952 void
953 interface_ip_init(struct interface *iface)
954 {
955         __interface_ip_init(&iface->proto_ip, iface);
956         __interface_ip_init(&iface->config_ip, iface);
957         vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
958
959 }
960
961 static void
962 interface_ip_valid_until_handler(struct uloop_timeout *t)
963 {
964         time_t now = system_get_rtime();
965         struct interface *iface;
966         vlist_for_each_element(&interfaces, iface, node) {
967                 if (iface->state != IFS_UP)
968                         continue;
969
970                 struct device_addr *addr, *addrp;
971                 struct device_route *route, *routep;
972                 struct device_prefix *pref, *prefp;
973
974                 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
975                         if (addr->valid_until && addr->valid_until < now)
976                                 vlist_delete(&iface->proto_ip.addr, &addr->node);
977
978                 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
979                         if (route->valid_until && route->valid_until < now)
980                                 vlist_delete(&iface->proto_ip.route, &route->node);
981
982                 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
983                         if (pref->valid_until && pref->valid_until < now)
984                                 vlist_delete(&iface->proto_ip.prefix, &pref->node);
985
986         }
987
988         uloop_timeout_set(t, 1000);
989 }
990
991 static void __init
992 interface_ip_init_worker(void)
993 {
994         valid_until_timeout.cb = interface_ip_valid_until_handler;
995         uloop_timeout_set(&valid_until_timeout, 1000);
996 }