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