Add deprecation for static IPv6 prefixes
[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_MAX
37 };
38
39 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
40         [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
41         [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
42         [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
43         [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
44         [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
45         [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
46 };
47
48 const struct config_param_list route_attr_list = {
49         .n_params = __ROUTE_MAX,
50         .params = route_attr,
51 };
52
53
54 struct list_head prefixes = LIST_HEAD_INIT(prefixes);
55 static struct device_prefix *ula_prefix = NULL;
56
57
58 static void
59 clear_if_addr(union if_addr *a, int mask)
60 {
61         int m_bytes = (mask + 7) / 8;
62         uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
63         uint8_t *p = (uint8_t *) a;
64
65         if (m_bytes < sizeof(a))
66                 memset(p + m_bytes, 0, sizeof(a) - m_bytes);
67
68         p[m_bytes - 1] &= ~m_clear;
69 }
70
71 static bool
72 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
73 {
74         union if_addr *p1, *p2;
75
76         p1 = alloca(sizeof(*a1));
77         p2 = alloca(sizeof(*a2));
78
79         memcpy(p1, a1, sizeof(*a1));
80         clear_if_addr(p1, mask);
81         memcpy(p2, a2, sizeof(*a2));
82         clear_if_addr(p2, mask);
83
84         return !memcmp(p1, p2, sizeof(*p1));
85 }
86
87 static bool
88 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
89 {
90         struct device_addr *addr;
91
92         vlist_for_each_element(&ip->addr, addr, node) {
93                 if (!addr->enabled)
94                         continue;
95
96                 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
97                         continue;
98
99                 if (!match_if_addr(&addr->addr, a, addr->mask))
100                         continue;
101
102                 return true;
103         }
104
105         return false;
106 }
107
108 static void
109 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
110                        bool v6, struct device_route **res)
111 {
112         struct device_route *route;
113
114         vlist_for_each_element(&ip->route, route, node) {
115                 if (!route->enabled)
116                         continue;
117
118                 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
119                         continue;
120
121                 if (!match_if_addr(&route->addr, a, route->mask))
122                         continue;
123
124                 if (!*res || route->mask < (*res)->mask)
125                         *res = route;
126         }
127 }
128
129 static bool
130 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
131 {
132         return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
133                __find_ip_addr_target(&iface->config_ip, a, v6);
134 }
135
136 static void
137 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
138                                bool v6, struct device_route **route)
139 {
140         __find_ip_route_target(&iface->proto_ip, a, v6, route);
141         __find_ip_route_target(&iface->config_ip, a, v6, route);
142 }
143
144 struct interface *
145 interface_ip_add_target_route(union if_addr *addr, bool v6)
146 {
147         struct interface *iface;
148         struct device_route *route, *r_next = NULL;
149         bool defaultroute_target = false;
150         int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
151
152         route = calloc(1, sizeof(*route));
153         if (!route)
154                 return NULL;
155
156         route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
157         route->mask = v6 ? 128 : 32;
158         if (memcmp(&route->addr, addr, addrsize) == 0)
159                 defaultroute_target = true;
160         else
161                 memcpy(&route->addr, addr, addrsize);
162
163         vlist_for_each_element(&interfaces, iface, node) {
164                 /* look for locally addressable target first */
165                 if (interface_ip_find_addr_target(iface, addr, v6))
166                         goto done;
167
168                 /* do not stop at the first route, let the lookup compare
169                  * masks to find the best match */
170                 interface_ip_find_route_target(iface, addr, v6, &r_next);
171         }
172
173         if (!r_next) {
174                 free(route);
175                 return NULL;
176         }
177
178         iface = r_next->iface;
179         memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
180         route->mtu = r_next->mtu;
181         route->metric = r_next->metric;
182
183 done:
184         route->iface = iface;
185         if (defaultroute_target)
186                 free(route);
187         else
188                 vlist_add(&iface->host_routes, &route->node, &route->flags);
189         return iface;
190 }
191
192 void
193 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
194 {
195         struct interface_ip_settings *ip;
196         struct blob_attr *tb[__ROUTE_MAX], *cur;
197         struct device_route *route;
198         int af = v6 ? AF_INET6 : AF_INET;
199
200         blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
201
202         if (!iface) {
203                 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
204                         return;
205
206                 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
207                 if (!iface)
208                         return;
209
210                 ip = &iface->config_ip;
211         } else {
212                 ip = &iface->proto_ip;
213         }
214
215         route = calloc(1, sizeof(*route));
216         if (!route)
217                 return;
218
219         route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
220         route->mask = v6 ? 128 : 32;
221         if ((cur = tb[ROUTE_MASK]) != NULL) {
222                 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
223                 if (route->mask > (v6 ? 128 : 32))
224                         goto error;
225         }
226
227         if ((cur = tb[ROUTE_TARGET]) != NULL) {
228                 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
229                         DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
230                         goto error;
231                 }
232         }
233
234         if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
235                 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
236                         DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
237                         goto error;
238                 }
239         }
240
241         if ((cur = tb[ROUTE_METRIC]) != NULL) {
242                 route->metric = blobmsg_get_u32(cur);
243                 route->flags |= DEVROUTE_METRIC;
244         }
245
246         if ((cur = tb[ROUTE_MTU]) != NULL) {
247                 route->mtu = blobmsg_get_u32(cur);
248                 route->flags |= DEVROUTE_MTU;
249         }
250
251         vlist_add(&ip->route, &route->node, &route->flags);
252         return;
253
254 error:
255         free(route);
256 }
257
258 static int
259 addr_cmp(const void *k1, const void *k2, void *ptr)
260 {
261         return memcmp(k1, k2, sizeof(struct device_addr) -
262                       offsetof(struct device_addr, flags));
263 }
264
265 static int
266 route_cmp(const void *k1, const void *k2, void *ptr)
267 {
268         return memcmp(k1, k2, sizeof(struct device_route) -
269                       offsetof(struct device_route, flags));
270 }
271
272 static int
273 prefix_cmp(const void *k1, const void *k2, void *ptr)
274 {
275         return memcmp(k1, k2, sizeof(struct device_prefix) -
276                         offsetof(struct device_prefix, addr));
277 }
278
279 static void
280 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
281 {
282         struct device *dev = iface->l3_dev.dev;
283         struct device_route route;
284
285         memset(&route, 0, sizeof(route));
286         route.iface = iface;
287         route.flags = addr->flags;
288         route.mask = addr->mask;
289         memcpy(&route.addr, &addr->addr, sizeof(route.addr));
290         clear_if_addr(&route.addr, route.mask);
291
292         if (add) {
293                 route.flags |= DEVADDR_KERNEL;
294                 system_del_route(dev, &route);
295
296                 route.flags &= ~DEVADDR_KERNEL;
297                 route.metric = iface->metric;
298                 system_add_route(dev, &route);
299         } else {
300                 system_del_route(dev, &route);
301         }
302 }
303
304 static void
305 interface_update_proto_addr(struct vlist_tree *tree,
306                             struct vlist_node *node_new,
307                             struct vlist_node *node_old)
308 {
309         struct interface_ip_settings *ip;
310         struct interface *iface;
311         struct device *dev;
312         struct device_addr *a_new = NULL, *a_old = NULL;
313         bool keep = false;
314
315         ip = container_of(tree, struct interface_ip_settings, addr);
316         iface = ip->iface;
317         dev = iface->l3_dev.dev;
318
319         if (node_new) {
320                 a_new = container_of(node_new, struct device_addr, node);
321
322                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
323                     !a_new->broadcast) {
324
325                         uint32_t mask = ~0;
326                         uint32_t *a = (uint32_t *) &a_new->addr;
327
328                         mask >>= a_new->mask;
329                         a_new->broadcast = *a | htonl(mask);
330                 }
331         }
332
333         if (node_old)
334                 a_old = container_of(node_old, struct device_addr, node);
335
336         if (a_new && a_old) {
337                 keep = true;
338
339                 if (a_old->flags != a_new->flags)
340                         keep = false;
341
342                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
343                     a_new->broadcast != a_old->broadcast)
344                         keep = false;
345         }
346
347         if (node_old) {
348                 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep) {
349                         interface_handle_subnet_route(iface, a_old, false);
350                         system_del_address(dev, a_old);
351                 }
352                 free(a_old);
353         }
354
355         if (node_new) {
356                 a_new->enabled = true;
357                 if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep) {
358                         system_add_address(dev, a_new);
359                         if (iface->metric)
360                                 interface_handle_subnet_route(iface, a_new, true);
361                 }
362         }
363 }
364
365 static bool
366 enable_route(struct interface_ip_settings *ip, struct device_route *route)
367 {
368         if (ip->no_defaultroute && !route->mask)
369                 return false;
370
371         return ip->enabled;
372 }
373
374 static void
375 interface_update_proto_route(struct vlist_tree *tree,
376                              struct vlist_node *node_new,
377                              struct vlist_node *node_old)
378 {
379         struct interface_ip_settings *ip;
380         struct interface *iface;
381         struct device *dev;
382         struct device_route *route_old, *route_new;
383         bool keep = false;
384
385         ip = container_of(tree, struct interface_ip_settings, route);
386         iface = ip->iface;
387         dev = iface->l3_dev.dev;
388
389         route_old = container_of(node_old, struct device_route, node);
390         route_new = container_of(node_new, struct device_route, node);
391
392         if (node_old && node_new)
393                 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
394
395         if (node_old) {
396                 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
397                         system_del_route(dev, route_old);
398                 free(route_old);
399         }
400
401         if (node_new) {
402                 bool _enabled = enable_route(ip, route_new);
403
404                 if (!(route_new->flags & DEVROUTE_METRIC))
405                         route_new->metric = iface->metric;
406
407                 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
408                         system_add_route(dev, route_new);
409
410                 route_new->iface = iface;
411                 route_new->enabled = _enabled;
412         }
413 }
414
415 static void
416 interface_update_host_route(struct vlist_tree *tree,
417                              struct vlist_node *node_new,
418                              struct vlist_node *node_old)
419 {
420         struct interface *iface;
421         struct device *dev;
422         struct device_route *route_old, *route_new;
423
424         iface = container_of(tree, struct interface, host_routes);
425         dev = iface->l3_dev.dev;
426
427         route_old = container_of(node_old, struct device_route, node);
428         route_new = container_of(node_new, struct device_route, node);
429
430         if (node_old) {
431                 system_del_route(dev, route_old);
432                 free(route_old);
433         }
434
435         if (node_new)
436                 system_add_route(dev, route_new);
437 }
438
439
440 static void
441 interface_set_prefix_address(struct interface *iface, bool add,
442                 struct device_prefix_assignment *assignment)
443 {
444         struct interface *uplink = assignment->prefix->iface;
445         if (!iface->l3_dev.dev)
446                 return;
447
448         struct device *l3_downlink = iface->l3_dev.dev;
449
450         struct device_addr addr;
451         memset(&addr, 0, sizeof(addr));
452         addr.addr.in6 = assignment->addr;
453         addr.mask = assignment->length;
454         addr.flags = DEVADDR_INET6;
455         addr.preferred_until = assignment->prefix->preferred_until;
456         addr.valid_until = assignment->prefix->valid_until;
457
458         if (!add) {
459                 if (assignment->enabled) {
460                         time_t now = system_get_rtime();
461                         addr.preferred_until = now;
462                         if (!addr.valid_until || addr.valid_until - now > 7200)
463                                 addr.valid_until = now + 7200;
464                         system_add_address(l3_downlink, &addr);
465                 }
466         } else {
467                 system_add_address(l3_downlink, &addr);
468
469                 if (uplink && uplink->l3_dev.dev) {
470                         int mtu = system_update_ipv6_mtu(
471                                         uplink->l3_dev.dev, 0);
472                         if (mtu > 0)
473                                 system_update_ipv6_mtu(l3_downlink, mtu);
474                 }
475         }
476         assignment->enabled = add;
477 }
478
479
480 static void
481 interface_update_prefix_assignments(struct vlist_tree *tree,
482                              struct vlist_node *node_new,
483                              struct vlist_node *node_old)
484 {
485         struct device_prefix_assignment *old, *new;
486         old = container_of(node_old, struct device_prefix_assignment, node);
487         new = container_of(node_new, struct device_prefix_assignment, node);
488
489         // Assignments persist across interface reloads etc.
490         // so use indirection to avoid dangling pointers
491         struct interface *iface = vlist_find(&interfaces,
492                         (node_new) ? new->name : old->name, iface, node);
493
494         if (node_old && node_new) {
495                 new->addr = old->addr;
496                 new->length = old->length;
497         } else if (node_old) {
498                 if (iface)
499                         interface_set_prefix_address(iface, false, old);
500                 free(old);
501         } else if (node_new) {
502                 struct device_prefix *prefix = new->prefix;
503                 uint64_t want = 1ULL << (64 - new->length);
504                 prefix->avail &= ~(want - 1);
505                 prefix->avail -= want;
506
507                 // Invert assignment
508                 uint64_t assigned = ~prefix->avail;
509                 assigned &= (1ULL << (64 - prefix->length)) - 1;
510                 assigned &= ~(want - 1);
511
512                 // Assignment
513                 new->addr = prefix->addr;
514                 new->addr.s6_addr32[0] |=
515                                 htonl(assigned >> 32);
516                 new->addr.s6_addr32[1] |=
517                                 htonl(assigned & 0xffffffffU);
518                 new->addr.s6_addr[15] += 1;
519         }
520
521         if (node_new && (iface->state == IFS_UP || iface->state == IFS_SETUP))
522                 interface_set_prefix_address(iface, true, new);
523 }
524
525
526 void
527 interface_ip_set_prefix_assignment(struct device_prefix *prefix,
528                 struct interface *iface, uint8_t length)
529 {
530         struct device_prefix_assignment *assignment;
531
532         if (!length || length > 64) {
533                 assignment = vlist_find(prefix->assignments, iface->name, assignment, node);
534                 if (assignment)
535                         interface_set_prefix_address(iface, false, assignment);
536         } else {
537                 uint64_t want = 1ULL << (64 - length);
538                 char *name;
539
540                 if (prefix->avail < want && prefix->avail > 0) {
541                         do {
542                                 want = 1ULL << (64 - ++length);
543                         } while (want > prefix->avail);
544                 }
545
546                 if (prefix->avail < want)
547                         return;
548
549                 assignment = calloc_a(sizeof(*assignment),
550                         &name, strlen(iface->name) + 1);
551                 assignment->prefix = prefix;
552                 assignment->length = length;
553                 assignment->name = strcpy(name, iface->name);
554
555                 vlist_add(prefix->assignments, &assignment->node, assignment->name);
556         }
557 }
558
559 static void
560 interface_update_prefix(struct vlist_tree *tree,
561                              struct vlist_node *node_new,
562                              struct vlist_node *node_old)
563 {
564         struct device_prefix *prefix_old, *prefix_new;
565         prefix_old = container_of(node_old, struct device_prefix, node);
566         prefix_new = container_of(node_new, struct device_prefix, node);
567
568         struct device_route route;
569         memset(&route, 0, sizeof(route));
570         route.flags = DEVADDR_INET6;
571         route.metric = INT32_MAX;
572         route.mask = (node_new) ? prefix_new->length : prefix_old->length;
573         route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
574
575         if (node_old && node_new) {
576                 prefix_new->avail = prefix_old->avail;
577                 prefix_new->assignments = prefix_old->assignments;
578                 prefix_old->assignments = NULL;
579
580                 // Update all assignments
581                 struct device_prefix_assignment *assignment;
582                 struct vlist_tree *assignments = prefix_new->assignments;
583                 vlist_for_each_element(assignments, assignment, node) {
584                         assignment->prefix = prefix_new;
585                         assignments->update(assignments,
586                                         &assignment->node, &assignment->node);
587                 }
588         } else if (node_new) {
589                 prefix_new->avail = 1ULL << (64 - prefix_new->length);
590                 prefix_new->assignments = calloc(1, sizeof(*prefix_new->assignments));
591                 vlist_init(prefix_new->assignments, avl_strcmp,
592                                 interface_update_prefix_assignments);
593
594                 // Create initial assignments for interfaces
595                 struct interface *iface;
596                 vlist_for_each_element(&interfaces, iface, node)
597                         interface_ip_set_prefix_assignment(prefix_new, iface,
598                                         iface->proto_ip.assignment_length);
599
600                 // Set null-route to avoid routing loops
601                 system_add_route(NULL, &route);
602         }
603
604         if (node_old) {
605                 // Remove null-route
606                 system_del_route(NULL, &route);
607
608                 list_del(&prefix_old->head);
609
610                 if (prefix_old->assignments) {
611                         vlist_flush_all(prefix_old->assignments);
612                         free(prefix_old->assignments);
613                 }
614                 free(prefix_old);
615         }
616
617         if (node_new)
618                 list_add(&prefix_new->head, &prefixes);
619 }
620
621 void
622 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
623                 uint8_t length, time_t valid_until, time_t preferred_until)
624 {
625         struct device_prefix *prefix = calloc(1, sizeof(*prefix));
626         prefix->length = length;
627         prefix->addr = *addr;
628         prefix->preferred_until = preferred_until;
629         prefix->valid_until = valid_until;
630         prefix->iface = iface;
631
632         if (iface)
633                 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
634         else
635                 interface_update_prefix(NULL, &prefix->node, NULL);
636 }
637
638 void
639 interface_ip_set_ula_prefix(const char *prefix)
640 {
641         char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
642         if (prefix)
643                 strncpy(buf, prefix, sizeof(buf) - 1);
644         char *prefixaddr = strtok_r(buf, "/", &saveptr);
645
646         struct in6_addr addr;
647         if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
648                 if (ula_prefix) {
649                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
650                         ula_prefix = NULL;
651                 }
652                 return;
653         }
654
655         int length;
656         char *prefixlen = strtok_r(NULL, ",", &saveptr);
657         if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
658                 return;
659
660         if (ula_prefix && (!IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
661                         ula_prefix->length != length)) {
662                 interface_update_prefix(NULL, NULL, &ula_prefix->node);
663                 ula_prefix = NULL;
664         }
665
666         interface_ip_add_device_prefix(NULL, &addr, length, 0, 0);
667 }
668
669 void
670 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
671 {
672         struct dns_server *s;
673
674         s = calloc(1, sizeof(*s));
675         if (!s)
676                 return;
677
678         s->af = AF_INET;
679         if (inet_pton(s->af, str, &s->addr.in))
680                 goto add;
681
682         s->af = AF_INET6;
683         if (inet_pton(s->af, str, &s->addr.in))
684                 goto add;
685
686         free(s);
687         return;
688
689 add:
690         D(INTERFACE, "Add IPv%c DNS server: %s\n",
691           s->af == AF_INET6 ? '6' : '4', str);
692         vlist_simple_add(&ip->dns_servers, &s->node);
693 }
694
695 void
696 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
697 {
698         struct blob_attr *cur;
699         int rem;
700
701         blobmsg_for_each_attr(cur, list, rem) {
702                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
703                         continue;
704
705                 if (!blobmsg_check_attr(cur, NULL))
706                         continue;
707
708                 interface_add_dns_server(ip, blobmsg_data(cur));
709         }
710 }
711
712 static void
713 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
714 {
715         struct dns_search_domain *s;
716         int len = strlen(str);
717
718         s = calloc(1, sizeof(*s) + len + 1);
719         if (!s)
720                 return;
721
722         D(INTERFACE, "Add DNS search domain: %s\n", str);
723         memcpy(s->name, str, len);
724         vlist_simple_add(&ip->dns_search, &s->node);
725 }
726
727 void
728 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
729 {
730         struct blob_attr *cur;
731         int rem;
732
733         blobmsg_for_each_attr(cur, list, rem) {
734                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
735                         continue;
736
737                 if (!blobmsg_check_attr(cur, NULL))
738                         continue;
739
740                 interface_add_dns_search_domain(ip, blobmsg_data(cur));
741         }
742 }
743
744 static void
745 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
746 {
747         struct dns_server *s;
748         struct dns_search_domain *d;
749         const char *str;
750         char buf[32];
751
752         vlist_simple_for_each_element(&ip->dns_servers, s, node) {
753                 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
754                 if (!str)
755                         continue;
756
757                 fprintf(f, "nameserver %s\n", str);
758         }
759
760         vlist_simple_for_each_element(&ip->dns_search, d, node) {
761                 fprintf(f, "search %s\n", d->name);
762         }
763 }
764
765 void
766 interface_write_resolv_conf(void)
767 {
768         struct interface *iface;
769         char *path = alloca(strlen(resolv_conf) + 5);
770         FILE *f;
771
772         sprintf(path, "%s.tmp", resolv_conf);
773         unlink(path);
774         f = fopen(path, "w");
775         if (!f) {
776                 D(INTERFACE, "Failed to open %s for writing\n", path);
777                 return;
778         }
779
780         vlist_for_each_element(&interfaces, iface, node) {
781                 if (iface->state != IFS_UP)
782                         continue;
783
784                 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
785                     vlist_simple_empty(&iface->proto_ip.dns_servers) &&
786                         vlist_simple_empty(&iface->config_ip.dns_search) &&
787                     vlist_simple_empty(&iface->config_ip.dns_servers))
788                         continue;
789
790                 fprintf(f, "# Interface %s\n", iface->name);
791                 write_resolv_conf_entries(f, &iface->config_ip);
792                 if (!iface->proto_ip.no_dns)
793                         write_resolv_conf_entries(f, &iface->proto_ip);
794         }
795         fclose(f);
796         if (rename(path, resolv_conf) < 0) {
797                 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
798                 unlink(path);
799         }
800 }
801
802 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
803 {
804         struct device_addr *addr;
805         struct device_route *route;
806         struct device *dev;
807
808         ip->enabled = enabled;
809         dev = ip->iface->l3_dev.dev;
810         if (!dev)
811                 return;
812
813         vlist_for_each_element(&ip->addr, addr, node) {
814                 if (addr->enabled == enabled)
815                         continue;
816
817                 if (enabled)
818                         system_add_address(dev, addr);
819                 else
820                         system_del_address(dev, addr);
821                 addr->enabled = enabled;
822         }
823
824         vlist_for_each_element(&ip->route, route, node) {
825                 bool _enabled = enabled;
826
827                 if (!enable_route(ip, route))
828                         _enabled = false;
829
830                 if (route->enabled == _enabled)
831                         continue;
832
833                 if (_enabled) {
834                         if (!(route->flags & DEVROUTE_METRIC))
835                                 route->metric = ip->iface->metric;
836
837                         system_add_route(dev, route);
838                 } else
839                         system_del_route(dev, route);
840                 route->enabled = _enabled;
841         }
842 }
843
844 void
845 interface_ip_update_start(struct interface_ip_settings *ip)
846 {
847         if (ip != &ip->iface->config_ip) {
848                 vlist_simple_update(&ip->dns_servers);
849                 vlist_simple_update(&ip->dns_search);
850         }
851         vlist_update(&ip->route);
852         vlist_update(&ip->addr);
853         vlist_update(&ip->prefix);
854 }
855
856 void
857 interface_ip_update_complete(struct interface_ip_settings *ip)
858 {
859         vlist_simple_flush(&ip->dns_servers);
860         vlist_simple_flush(&ip->dns_search);
861         vlist_flush(&ip->route);
862         vlist_flush(&ip->addr);
863         vlist_flush(&ip->prefix);
864         interface_write_resolv_conf();
865 }
866
867 void
868 interface_ip_flush(struct interface_ip_settings *ip)
869 {
870         if (ip == &ip->iface->proto_ip)
871                 vlist_flush_all(&ip->iface->host_routes);
872         vlist_simple_flush_all(&ip->dns_servers);
873         vlist_simple_flush_all(&ip->dns_search);
874         vlist_flush_all(&ip->route);
875         vlist_flush_all(&ip->addr);
876         vlist_flush_all(&ip->prefix);
877 }
878
879 static void
880 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
881 {
882         ip->iface = iface;
883         ip->enabled = true;
884         vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
885         vlist_simple_init(&ip->dns_servers, struct dns_server, node);
886         vlist_init(&ip->route, route_cmp, interface_update_proto_route);
887         vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
888         vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
889 }
890
891 void
892 interface_ip_init(struct interface *iface)
893 {
894         __interface_ip_init(&iface->proto_ip, iface);
895         __interface_ip_init(&iface->config_ip, iface);
896         vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
897 }