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