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