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