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