Improved IPv6 featureset
[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 interface *iface, bool add,
453                 struct device_prefix_assignment *assignment)
454 {
455         struct interface *uplink = assignment->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 = assignment->addr;
464         addr.mask = assignment->length;
465         addr.flags = DEVADDR_INET6;
466         addr.preferred_until = assignment->prefix->preferred_until;
467         addr.valid_until = assignment->prefix->valid_until;
468
469         if (!add) {
470                 if (assignment->enabled) {
471                         time_t now = system_get_rtime();
472                         addr.preferred_until = now;
473                         if (!addr.valid_until || addr.valid_until - now > 7200)
474                                 addr.valid_until = now + 7200;
475                         system_add_address(l3_downlink, &addr);
476                 }
477         } else {
478                 system_add_address(l3_downlink, &addr);
479
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         }
487         assignment->enabled = add;
488 }
489
490
491 static void
492 interface_update_prefix_assignments(struct vlist_tree *tree,
493                              struct vlist_node *node_new,
494                              struct vlist_node *node_old)
495 {
496         struct device_prefix_assignment *old, *new;
497         old = container_of(node_old, struct device_prefix_assignment, node);
498         new = container_of(node_new, struct device_prefix_assignment, node);
499
500         // Assignments persist across interface reloads etc.
501         // so use indirection to avoid dangling pointers
502         struct interface *iface = vlist_find(&interfaces,
503                         (node_new) ? new->name : old->name, iface, node);
504
505         if (node_old && node_new) {
506                 new->addr = old->addr;
507                 new->length = old->length;
508         } else if (node_old) {
509                 if (iface)
510                         interface_set_prefix_address(iface, false, old);
511                 free(old);
512         } else if (node_new) {
513                 struct device_prefix *prefix = new->prefix;
514                 uint64_t want = 1ULL << (64 - new->length);
515                 prefix->avail &= ~(want - 1);
516                 prefix->avail -= want;
517
518                 // Invert assignment
519                 uint64_t assigned = ~prefix->avail;
520                 assigned &= (1ULL << (64 - prefix->length)) - 1;
521                 assigned &= ~(want - 1);
522
523                 // Assignment
524                 new->addr = prefix->addr;
525                 new->addr.s6_addr32[0] |=
526                                 htonl(assigned >> 32);
527                 new->addr.s6_addr32[1] |=
528                                 htonl(assigned & 0xffffffffU);
529                 new->addr.s6_addr[15] += 1;
530         }
531
532         if (node_new && (iface->state == IFS_UP || iface->state == IFS_SETUP))
533                 interface_set_prefix_address(iface, true, new);
534 }
535
536
537 void
538 interface_ip_set_prefix_assignment(struct device_prefix *prefix,
539                 struct interface *iface, uint8_t length)
540 {
541         struct device_prefix_assignment *assignment;
542
543         if (!length || length > 64) {
544                 assignment = vlist_find(prefix->assignments, iface->name, assignment, node);
545                 if (assignment)
546                         interface_set_prefix_address(iface, false, assignment);
547         } else {
548                 uint64_t want = 1ULL << (64 - length);
549                 char *name;
550
551                 if (prefix->avail < want && prefix->avail > 0) {
552                         do {
553                                 want = 1ULL << (64 - ++length);
554                         } while (want > prefix->avail);
555                 }
556
557                 if (prefix->avail < want)
558                         return;
559
560                 assignment = calloc_a(sizeof(*assignment),
561                         &name, strlen(iface->name) + 1);
562                 assignment->prefix = prefix;
563                 assignment->length = length;
564                 assignment->name = strcpy(name, iface->name);
565
566                 vlist_add(prefix->assignments, &assignment->node, assignment->name);
567         }
568 }
569
570 static void
571 interface_update_prefix(struct vlist_tree *tree,
572                              struct vlist_node *node_new,
573                              struct vlist_node *node_old)
574 {
575         struct device_prefix *prefix_old, *prefix_new;
576         prefix_old = container_of(node_old, struct device_prefix, node);
577         prefix_new = container_of(node_new, struct device_prefix, node);
578
579         struct device_route route;
580         memset(&route, 0, sizeof(route));
581         route.flags = DEVADDR_INET6;
582         route.metric = INT32_MAX;
583         route.mask = (node_new) ? prefix_new->length : prefix_old->length;
584         route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
585
586         if (node_old && node_new) {
587                 prefix_new->avail = prefix_old->avail;
588                 prefix_new->assignments = prefix_old->assignments;
589                 prefix_old->assignments = NULL;
590
591                 // Update all assignments
592                 struct device_prefix_assignment *assignment;
593                 struct vlist_tree *assignments = prefix_new->assignments;
594                 vlist_for_each_element(assignments, assignment, node) {
595                         assignment->prefix = prefix_new;
596                         assignments->update(assignments,
597                                         &assignment->node, &assignment->node);
598                 }
599         } else if (node_new) {
600                 prefix_new->avail = 1ULL << (64 - prefix_new->length);
601                 prefix_new->assignments = calloc(1, sizeof(*prefix_new->assignments));
602                 vlist_init(prefix_new->assignments, avl_strcmp,
603                                 interface_update_prefix_assignments);
604
605                 // Create initial assignments for interfaces
606                 struct interface *iface;
607                 vlist_for_each_element(&interfaces, iface, node)
608                         interface_ip_set_prefix_assignment(prefix_new, iface,
609                                         iface->proto_ip.assignment_length);
610
611                 // Set null-route to avoid routing loops
612                 system_add_route(NULL, &route);
613         }
614
615         if (node_old) {
616                 // Remove null-route
617                 system_del_route(NULL, &route);
618
619                 list_del(&prefix_old->head);
620
621                 if (prefix_old->assignments) {
622                         vlist_flush_all(prefix_old->assignments);
623                         free(prefix_old->assignments);
624                 }
625                 free(prefix_old);
626         }
627
628         if (node_new)
629                 list_add(&prefix_new->head, &prefixes);
630 }
631
632 struct device_prefix*
633 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
634                 uint8_t length, time_t valid_until, time_t preferred_until)
635 {
636         struct device_prefix *prefix = calloc(1, sizeof(*prefix));
637         prefix->length = length;
638         prefix->addr = *addr;
639         prefix->preferred_until = preferred_until;
640         prefix->valid_until = valid_until;
641         prefix->iface = iface;
642
643         if (iface)
644                 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
645         else
646                 interface_update_prefix(NULL, &prefix->node, NULL);
647
648         return prefix;
649 }
650
651 void
652 interface_ip_set_ula_prefix(const char *prefix)
653 {
654         char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
655         if (prefix)
656                 strncpy(buf, prefix, sizeof(buf) - 1);
657         char *prefixaddr = strtok_r(buf, "/", &saveptr);
658
659         struct in6_addr addr;
660         if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
661                 if (ula_prefix) {
662                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
663                         ula_prefix = NULL;
664                 }
665                 return;
666         }
667
668         int length;
669         char *prefixlen = strtok_r(NULL, ",", &saveptr);
670         if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
671                 return;
672
673         if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
674                         ula_prefix->length != length) {
675                 if (ula_prefix)
676                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
677
678                 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length, 0, 0);
679         }
680 }
681
682 void
683 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
684 {
685         struct dns_server *s;
686
687         s = calloc(1, sizeof(*s));
688         if (!s)
689                 return;
690
691         s->af = AF_INET;
692         if (inet_pton(s->af, str, &s->addr.in))
693                 goto add;
694
695         s->af = AF_INET6;
696         if (inet_pton(s->af, str, &s->addr.in))
697                 goto add;
698
699         free(s);
700         return;
701
702 add:
703         D(INTERFACE, "Add IPv%c DNS server: %s\n",
704           s->af == AF_INET6 ? '6' : '4', str);
705         vlist_simple_add(&ip->dns_servers, &s->node);
706 }
707
708 void
709 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
710 {
711         struct blob_attr *cur;
712         int rem;
713
714         blobmsg_for_each_attr(cur, list, rem) {
715                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
716                         continue;
717
718                 if (!blobmsg_check_attr(cur, NULL))
719                         continue;
720
721                 interface_add_dns_server(ip, blobmsg_data(cur));
722         }
723 }
724
725 static void
726 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
727 {
728         struct dns_search_domain *s;
729         int len = strlen(str);
730
731         s = calloc(1, sizeof(*s) + len + 1);
732         if (!s)
733                 return;
734
735         D(INTERFACE, "Add DNS search domain: %s\n", str);
736         memcpy(s->name, str, len);
737         vlist_simple_add(&ip->dns_search, &s->node);
738 }
739
740 void
741 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
742 {
743         struct blob_attr *cur;
744         int rem;
745
746         blobmsg_for_each_attr(cur, list, rem) {
747                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
748                         continue;
749
750                 if (!blobmsg_check_attr(cur, NULL))
751                         continue;
752
753                 interface_add_dns_search_domain(ip, blobmsg_data(cur));
754         }
755 }
756
757 static void
758 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
759 {
760         struct dns_server *s;
761         struct dns_search_domain *d;
762         const char *str;
763         char buf[32];
764
765         vlist_simple_for_each_element(&ip->dns_servers, s, node) {
766                 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
767                 if (!str)
768                         continue;
769
770                 fprintf(f, "nameserver %s\n", str);
771         }
772
773         vlist_simple_for_each_element(&ip->dns_search, d, node) {
774                 fprintf(f, "search %s\n", d->name);
775         }
776 }
777
778 void
779 interface_write_resolv_conf(void)
780 {
781         struct interface *iface;
782         char *path = alloca(strlen(resolv_conf) + 5);
783         FILE *f;
784
785         sprintf(path, "%s.tmp", resolv_conf);
786         unlink(path);
787         f = fopen(path, "w");
788         if (!f) {
789                 D(INTERFACE, "Failed to open %s for writing\n", path);
790                 return;
791         }
792
793         vlist_for_each_element(&interfaces, iface, node) {
794                 if (iface->state != IFS_UP)
795                         continue;
796
797                 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
798                     vlist_simple_empty(&iface->proto_ip.dns_servers) &&
799                         vlist_simple_empty(&iface->config_ip.dns_search) &&
800                     vlist_simple_empty(&iface->config_ip.dns_servers))
801                         continue;
802
803                 fprintf(f, "# Interface %s\n", iface->name);
804                 write_resolv_conf_entries(f, &iface->config_ip);
805                 if (!iface->proto_ip.no_dns)
806                         write_resolv_conf_entries(f, &iface->proto_ip);
807         }
808         fclose(f);
809         if (rename(path, resolv_conf) < 0) {
810                 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
811                 unlink(path);
812         }
813 }
814
815 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
816 {
817         struct device_addr *addr;
818         struct device_route *route;
819         struct device *dev;
820
821         ip->enabled = enabled;
822         dev = ip->iface->l3_dev.dev;
823         if (!dev)
824                 return;
825
826         vlist_for_each_element(&ip->addr, addr, node) {
827                 if (addr->enabled == enabled)
828                         continue;
829
830                 if (enabled)
831                         system_add_address(dev, addr);
832                 else
833                         system_del_address(dev, addr);
834                 addr->enabled = enabled;
835         }
836
837         vlist_for_each_element(&ip->route, route, node) {
838                 bool _enabled = enabled;
839
840                 if (!enable_route(ip, route))
841                         _enabled = false;
842
843                 if (route->enabled == _enabled)
844                         continue;
845
846                 if (_enabled) {
847                         if (!(route->flags & DEVROUTE_METRIC))
848                                 route->metric = ip->iface->metric;
849
850                         system_add_route(dev, route);
851                 } else
852                         system_del_route(dev, route);
853                 route->enabled = _enabled;
854         }
855 }
856
857 void
858 interface_ip_update_start(struct interface_ip_settings *ip)
859 {
860         if (ip != &ip->iface->config_ip) {
861                 vlist_simple_update(&ip->dns_servers);
862                 vlist_simple_update(&ip->dns_search);
863         }
864         vlist_update(&ip->route);
865         vlist_update(&ip->addr);
866         vlist_update(&ip->prefix);
867 }
868
869 void
870 interface_ip_update_complete(struct interface_ip_settings *ip)
871 {
872         vlist_simple_flush(&ip->dns_servers);
873         vlist_simple_flush(&ip->dns_search);
874         vlist_flush(&ip->route);
875         vlist_flush(&ip->addr);
876         vlist_flush(&ip->prefix);
877         interface_write_resolv_conf();
878 }
879
880 void
881 interface_ip_flush(struct interface_ip_settings *ip)
882 {
883         if (ip == &ip->iface->proto_ip)
884                 vlist_flush_all(&ip->iface->host_routes);
885         vlist_simple_flush_all(&ip->dns_servers);
886         vlist_simple_flush_all(&ip->dns_search);
887         vlist_flush_all(&ip->route);
888         vlist_flush_all(&ip->addr);
889         vlist_flush_all(&ip->prefix);
890 }
891
892 static void
893 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
894 {
895         ip->iface = iface;
896         ip->enabled = true;
897         vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
898         vlist_simple_init(&ip->dns_servers, struct dns_server, node);
899         vlist_init(&ip->route, route_cmp, interface_update_proto_route);
900         vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
901         vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
902 }
903
904 void
905 interface_ip_init(struct interface *iface)
906 {
907         __interface_ip_init(&iface->proto_ip, iface);
908         __interface_ip_init(&iface->config_ip, iface);
909         vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
910
911 }
912
913 static void
914 interface_ip_valid_until_handler(struct uloop_timeout *t)
915 {
916         time_t now = system_get_rtime();
917         struct interface *iface;
918         vlist_for_each_element(&interfaces, iface, node) {
919                 if (iface->state != IFS_UP)
920                         continue;
921
922                 struct device_addr *addr, *addrp;
923                 struct device_route *route, *routep;
924                 struct device_prefix *pref, *prefp;
925
926                 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
927                         if (addr->valid_until && addr->valid_until < now)
928                                 vlist_delete(&iface->proto_ip.addr, &addr->node);
929
930                 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
931                         if (route->valid_until && route->valid_until < now)
932                                 vlist_delete(&iface->proto_ip.route, &route->node);
933
934                 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
935                         if (pref->valid_until && pref->valid_until < now)
936                                 vlist_delete(&iface->proto_ip.prefix, &pref->node);
937
938         }
939
940         uloop_timeout_set(t, 1000);
941 }
942
943 static void __init
944 interface_ip_init_worker(void)
945 {
946         valid_until_timeout.cb = interface_ip_valid_until_handler;
947         uloop_timeout_set(&valid_until_timeout, 1000);
948 }