netifd: GRE tunnel support
[oweals/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                 /* only set the table flag if not using the main (default) table */
369                 if (system_is_default_rt_table(route->table))
370                         route->table = 0;
371
372                 if (route->table)
373                         route->flags |= DEVROUTE_TABLE;
374         }
375
376         if ((cur = tb[ROUTE_VALID]) != NULL) {
377                 int64_t valid = blobmsg_get_u32(cur);
378                 int64_t valid_until = valid + (int64_t)system_get_rtime();
379                 if (valid_until <= LONG_MAX && valid != 0xffffffffLL) // Catch overflow
380                         route->valid_until = valid_until;
381         }
382
383         if ((cur = tb[ROUTE_TYPE]) != NULL) {
384                 if (!system_resolve_rt_type(blobmsg_data(cur), &route->type)) {
385                         DPRINTF("Failed to resolve routing type: %s\n", (char *) blobmsg_data(cur));
386                         goto error;
387                 }
388                 route->flags |= DEVROUTE_TYPE;
389         }
390
391         vlist_add(&ip->route, &route->node, route);
392         return;
393
394 error:
395         free(route);
396 }
397
398 static int
399 addr_cmp(const void *k1, const void *k2, void *ptr)
400 {
401         return memcmp(k1, k2, sizeof(struct device_addr) -
402                       offsetof(struct device_addr, flags));
403 }
404
405 static int
406 route_cmp(const void *k1, const void *k2, void *ptr)
407 {
408         const struct device_route *r1 = k1, *r2 = k2;
409
410         if (r1->mask != r2->mask)
411                 return r2->mask - r1->mask;
412
413         if (r1->metric != r2->metric)
414                 return r1->metric - r2->metric;
415
416         if (r1->flags != r2->flags)
417                 return r2->flags - r1->flags;
418
419         if (r1->sourcemask != r2->sourcemask)
420                 return r1->sourcemask - r2->sourcemask;
421
422         if (r1->table != r2->table)
423                 return r1->table - r2->table;
424
425         int maskcmp = memcmp(&r1->source, &r2->source, sizeof(r1->source));
426         if (maskcmp)
427                 return maskcmp;
428
429         return memcmp(&r1->addr, &r2->addr, sizeof(r1->addr));
430 }
431
432 static int
433 prefix_cmp(const void *k1, const void *k2, void *ptr)
434 {
435         return memcmp(k1, k2, offsetof(struct device_prefix, pclass) -
436                         offsetof(struct device_prefix, addr));
437 }
438
439 static void
440 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
441 {
442         struct device *dev = iface->l3_dev.dev;
443         struct device_route route;
444
445         memset(&route, 0, sizeof(route));
446         route.iface = iface;
447         route.flags = addr->flags;
448         route.mask = addr->mask;
449         memcpy(&route.addr, &addr->addr, sizeof(route.addr));
450         clear_if_addr(&route.addr, route.mask);
451
452         if (add) {
453                 route.flags |= DEVADDR_KERNEL;
454                 system_del_route(dev, &route);
455
456                 if (!(addr->flags & DEVADDR_OFFLINK)) {
457                         route.flags &= ~DEVADDR_KERNEL;
458                         route.metric = iface->metric;
459                         system_add_route(dev, &route);
460                 }
461         } else {
462                 if (!(addr->flags & DEVADDR_OFFLINK))
463                         system_del_route(dev, &route);
464         }
465 }
466
467 static void
468 interface_update_proto_addr(struct vlist_tree *tree,
469                             struct vlist_node *node_new,
470                             struct vlist_node *node_old)
471 {
472         struct interface_ip_settings *ip;
473         struct interface *iface;
474         struct device *dev;
475         struct device_addr *a_new = NULL, *a_old = NULL;
476         bool replace = false;
477         bool keep = false;
478         bool v6 = false;
479
480         ip = container_of(tree, struct interface_ip_settings, addr);
481         iface = ip->iface;
482         dev = iface->l3_dev.dev;
483
484         if (!node_new || !node_old)
485                 iface->updated |= IUF_ADDRESS;
486
487         if (node_new) {
488                 a_new = container_of(node_new, struct device_addr, node);
489
490                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
491                     !a_new->broadcast) {
492
493                         uint32_t mask = ~0;
494                         uint32_t *a = (uint32_t *) &a_new->addr;
495
496                         mask >>= a_new->mask;
497                         a_new->broadcast = *a | htonl(mask);
498                 }
499         }
500
501         if (node_old)
502                 a_old = container_of(node_old, struct device_addr, node);
503
504         if (a_new && a_old) {
505                 keep = true;
506
507                 if (a_old->flags != a_new->flags || a_old->failed)
508                         keep = false;
509
510                 if (a_old->valid_until != a_new->valid_until ||
511                                 a_old->preferred_until != a_new->preferred_until)
512                         replace = true;
513
514                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
515                     a_new->broadcast != a_old->broadcast)
516                         keep = false;
517         }
518
519         if (node_old) {
520                 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep) {
521                         interface_handle_subnet_route(iface, a_old, false);
522
523                         if ((a_old->flags & DEVADDR_FAMILY) == DEVADDR_INET6)
524                                 v6 = true;
525
526                         unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
527
528                         //This is needed for source routing to work correctly. If a device
529                         //has two connections to a network using the same subnet, adding
530                         //only the network-rule will cause packets to be routed through the
531                         //first matching network (source IP matches both masks).
532                         if (table) {
533                                 set_ip_source_policy(false, v6, IPRULE_PRIORITY_ADDR, &a_old->addr,
534                                                 (v6) ? 128 : 32, table, NULL, NULL);
535                                 set_ip_source_policy(false, v6, IPRULE_PRIORITY_NW, &a_old->addr,
536                                                 a_old->mask, table, NULL, NULL);
537                         }
538
539                         system_del_address(dev, a_old);
540                 }
541                 free(a_old->pclass);
542                 free(a_old);
543         }
544
545         if (node_new) {
546                 a_new->enabled = true;
547                 if (!(a_new->flags & DEVADDR_EXTERNAL) && (!keep || replace)) {
548                         if (system_add_address(dev, a_new))
549                                 a_new->failed = true;
550
551                         if (!keep) {
552                                 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET6)
553                                         v6 = true;
554
555                                 unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
556
557                                 if (table) {
558                                         set_ip_source_policy(true, v6, IPRULE_PRIORITY_ADDR, &a_new->addr,
559                                                         (v6) ? 128 : 32, table, NULL, NULL);
560                                         set_ip_source_policy(true, v6, IPRULE_PRIORITY_NW, &a_new->addr,
561                                                         a_new->mask, table, NULL, NULL);
562                                 }
563                         }
564
565                         if ((a_new->flags & DEVADDR_OFFLINK) || iface->metric)
566                                 interface_handle_subnet_route(iface, a_new, true);
567                 }
568         }
569 }
570
571 static bool
572 enable_route(struct interface_ip_settings *ip, struct device_route *route)
573 {
574         if (ip->no_defaultroute && !route->mask)
575                 return false;
576
577         return ip->enabled;
578 }
579
580 static void
581 interface_update_proto_route(struct vlist_tree *tree,
582                              struct vlist_node *node_new,
583                              struct vlist_node *node_old)
584 {
585         struct interface_ip_settings *ip;
586         struct interface *iface;
587         struct device *dev;
588         struct device_route *route_old, *route_new;
589         bool keep = false;
590
591         ip = container_of(tree, struct interface_ip_settings, route);
592         iface = ip->iface;
593         dev = iface->l3_dev.dev;
594
595         if (!node_new || !node_old)
596                 iface->updated |= IUF_ROUTE;
597
598         route_old = container_of(node_old, struct device_route, node);
599         route_new = container_of(node_new, struct device_route, node);
600
601         if (node_old && node_new)
602                 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop)) &&
603                         (route_old->mtu == route_new->mtu) && (route_old->type == route_new->type) &&
604                         !route_old->failed;
605
606         if (node_old) {
607                 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
608                         system_del_route(dev, route_old);
609
610                 free(route_old);
611         }
612
613         if (node_new) {
614                 bool _enabled = enable_route(ip, route_new);
615
616                 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
617                         if (system_add_route(dev, route_new))
618                                 route_new->failed = true;
619
620                 route_new->iface = iface;
621                 route_new->enabled = _enabled;
622         }
623 }
624
625 static void
626 interface_update_host_route(struct vlist_tree *tree,
627                              struct vlist_node *node_new,
628                              struct vlist_node *node_old)
629 {
630         struct interface *iface;
631         struct device *dev;
632         struct device_route *route_old, *route_new;
633
634         iface = container_of(tree, struct interface, host_routes);
635         dev = iface->l3_dev.dev;
636
637         route_old = container_of(node_old, struct device_route, node);
638         route_new = container_of(node_new, struct device_route, node);
639
640         if (node_old) {
641                 system_del_route(dev, route_old);
642                 free(route_old);
643         }
644
645         if (node_new) {
646                 if (system_add_route(dev, route_new))
647                         route_new->failed = true;
648         }
649 }
650
651
652 static void
653 interface_set_prefix_address(struct device_prefix_assignment *assignment,
654                 const struct device_prefix *prefix, struct interface *iface, bool add)
655 {
656         const struct interface *uplink = prefix->iface;
657         if (!iface->l3_dev.dev)
658                 return;
659
660         struct device *l3_downlink = iface->l3_dev.dev;
661
662         struct device_addr addr;
663         memset(&addr, 0, sizeof(addr));
664         addr.addr.in6 = prefix->addr;
665         addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
666         addr.addr.in6.s6_addr[15] += 1;
667         addr.mask = assignment->length;
668         addr.flags = DEVADDR_INET6;
669         addr.preferred_until = prefix->preferred_until;
670         addr.valid_until = prefix->valid_until;
671
672         if (!add && assignment->enabled) {
673                 time_t now = system_get_rtime();
674                 addr.preferred_until = now;
675                 if (!addr.valid_until || addr.valid_until - now > 7200)
676                         addr.valid_until = now + 7200;
677                 system_del_address(l3_downlink, &addr); // Work around dangling prefix routes
678                 system_add_address(l3_downlink, &addr);
679                 if (prefix->iface) {
680                         if (prefix->iface->ip6table)
681                                 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW, &addr.addr,
682                                                 addr.mask, prefix->iface->ip6table, iface, NULL);
683
684                         set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT, &addr.addr,
685                                                         addr.mask, 0, iface, "unreachable");
686                 }
687
688                 assignment->enabled = false;
689         } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP) &&
690                         !system_add_address(l3_downlink, &addr)) {
691                 if (prefix->iface && !assignment->enabled) {
692                         set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT, &addr.addr,
693                                         addr.mask, 0, iface, "unreachable");
694
695                         if (prefix->iface->ip6table)
696                                 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW, &addr.addr,
697                                                 addr.mask, prefix->iface->ip6table, iface, NULL);
698                 }
699                 if (uplink && uplink->l3_dev.dev) {
700                         int mtu = system_update_ipv6_mtu(
701                                         uplink->l3_dev.dev, 0);
702                         if (mtu > 0)
703                                 system_update_ipv6_mtu(l3_downlink, mtu);
704                 }
705                 assignment->enabled = true;
706         }
707 }
708
709 static bool interface_prefix_assign(struct list_head *list,
710                 struct device_prefix_assignment *assign)
711 {
712         int32_t current = 0, asize = (1 << (64 - assign->length)) - 1;
713         struct device_prefix_assignment *c;
714         list_for_each_entry(c, list, head) {
715                 if (assign->assigned != -1) {
716                         if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
717                                 list_add_tail(&assign->head, &c->head);
718                                 return true;
719                         }
720                 } else if (assign->assigned == -1) {
721                         current = (current + asize) & (~asize);
722                         if (current + asize < c->assigned) {
723                                 assign->assigned = current;
724                                 list_add_tail(&assign->head, &c->head);
725                                 return true;
726                         }
727                 }
728                 current = (c->assigned + (1 << (64 - c->length)));
729         }
730         return false;
731 }
732
733 static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup)
734 {
735         struct device_prefix_assignment *c;
736         struct interface *iface;
737
738         // Delete all assignments
739         while (!list_empty(&prefix->assignments)) {
740                 c = list_first_entry(&prefix->assignments,
741                                 struct device_prefix_assignment, head);
742                 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
743                         interface_set_prefix_address(c, prefix, iface, false);
744                 list_del(&c->head);
745                 free(c);
746         }
747
748         if (!setup)
749                 return;
750
751         // End-of-assignment sentinel
752         c = malloc(sizeof(*c) + 1);
753         c->assigned = 1 << (64 - prefix->length);
754         c->length = 64;
755         c->name[0] = 0;
756         list_add(&c->head, &prefix->assignments);
757
758         // Excluded prefix
759         if (prefix->excl_length > 0) {
760                 const char name[] = "!excluded";
761                 c = malloc(sizeof(*c) + sizeof(name));
762                 c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
763                                 ((1 << (64 - prefix->length)) - 1);
764                 c->length = prefix->excl_length;
765                 memcpy(c->name, name, sizeof(name));
766                 list_add(&c->head, &prefix->assignments);
767         }
768
769         bool assigned_any = false;
770         struct list_head assign_later = LIST_HEAD_INIT(assign_later);
771         vlist_for_each_element(&interfaces, iface, node) {
772                 if (iface->assignment_length < 48 ||
773                                 iface->assignment_length > 64)
774                         continue;
775
776                 // Test whether there is a matching class
777                 if (!list_empty(&iface->assignment_classes)) {
778                         bool found = false;
779
780                         struct interface_assignment_class *c;
781                         list_for_each_entry(c, &iface->assignment_classes, head) {
782                                 if (!strcmp(c->name, prefix->pclass)) {
783                                         found = true;
784                                         break;
785                                 }
786                         }
787
788                         if (!found)
789                                 continue;
790                 }
791
792                 size_t namelen = strlen(iface->name) + 1;
793                 c = malloc(sizeof(*c) + namelen);
794                 c->length = iface->assignment_length;
795                 c->assigned = iface->assignment_hint;
796                 c->enabled = false;
797                 memcpy(c->name, iface->name, namelen);
798
799                 // First process all custom assignments, put all others in later-list
800                 if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
801                         if (c->assigned != -1) {
802                                 c->assigned = -1;
803                                 netifd_log_message(L_WARNING, "Failed to assign requested subprefix "
804                                                 "of size %hhu for %s, trying other\n", c->length, c->name);
805                         }
806
807                         struct list_head *next = &assign_later;
808                         struct device_prefix_assignment *n;
809                         list_for_each_entry(n, &assign_later, head) {
810                                 if (n->length < c->length) {
811                                         next = &n->head;
812                                         break;
813                                 }
814                         }
815                         list_add_tail(&c->head, next);
816                 }
817
818                 if (c->assigned != -1)
819                         assigned_any = true;
820         }
821
822         // Then try to assign all other + failed custom assignments
823         while (!list_empty(&assign_later)) {
824                 c = list_first_entry(&assign_later, struct device_prefix_assignment, head);
825                 list_del(&c->head);
826
827                 bool assigned = false;
828                 do {
829                         assigned = interface_prefix_assign(&prefix->assignments, c);
830                 } while (!assigned && ++c->length <= 64);
831
832                 if (!assigned) {
833                         netifd_log_message(L_WARNING, "Failed to assign subprefix "
834                                         "of size %hhu for %s\n", c->length, c->name);
835                         free(c);
836                 } else {
837                         assigned_any = true;
838                 }
839         }
840
841         list_for_each_entry(c, &prefix->assignments, head)
842                 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
843                         interface_set_prefix_address(c, prefix, iface, true);
844
845         if (!assigned_any)
846                 netifd_log_message(L_WARNING, "You have delegated IPv6-prefixes but haven't assigned them "
847                                 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
848 }
849
850
851 void interface_refresh_assignments(bool hint)
852 {
853         static bool refresh = false;
854         if (!hint && refresh) {
855                 struct device_prefix *p;
856                 list_for_each_entry(p, &prefixes, head)
857                         interface_update_prefix_assignments(p, true);
858         }
859         refresh = hint;
860 }
861
862
863 static void
864 interface_update_prefix(struct vlist_tree *tree,
865                              struct vlist_node *node_new,
866                              struct vlist_node *node_old)
867 {
868         struct device_prefix *prefix_old, *prefix_new;
869         prefix_old = container_of(node_old, struct device_prefix, node);
870         prefix_new = container_of(node_new, struct device_prefix, node);
871
872         struct interface_ip_settings *ip = container_of(tree, struct interface_ip_settings, prefix);
873         if (tree && (!node_new || !node_old))
874                 ip->iface->updated |= IUF_PREFIX;
875
876         struct device_route route;
877         memset(&route, 0, sizeof(route));
878         route.flags = DEVADDR_INET6;
879         route.metric = INT32_MAX;
880         route.mask = (node_new) ? prefix_new->length : prefix_old->length;
881         route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
882
883
884         struct device_prefix_assignment *c;
885         struct interface *iface;
886
887         if (node_old && node_new) {
888                 // Move assignments and refresh addresses to update valid times
889                 list_splice(&prefix_old->assignments, &prefix_new->assignments);
890
891                 list_for_each_entry(c, &prefix_new->assignments, head)
892                         if ((iface = vlist_find(&interfaces, c->name, iface, node)))
893                                 interface_set_prefix_address(c, prefix_new, iface, true);
894         } else if (node_new) {
895                 // Set null-route to avoid routing loops
896                 system_add_route(NULL, &route);
897
898                 if (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation)
899                         interface_update_prefix_assignments(prefix_new, true);
900         } else if (node_old) {
901                 // Remove null-route
902                 interface_update_prefix_assignments(prefix_old, false);
903                 system_del_route(NULL, &route);
904         }
905
906         if (node_old) {
907                 if (prefix_old->head.next)
908                         list_del(&prefix_old->head);
909                 free(prefix_old);
910         }
911
912         if (node_new && (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation))
913                 list_add(&prefix_new->head, &prefixes);
914
915 }
916
917 struct device_prefix*
918 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
919                 uint8_t length, time_t valid_until, time_t preferred_until,
920                 struct in6_addr *excl_addr, uint8_t excl_length, const char *pclass)
921 {
922         if (!pclass)
923                 pclass = (iface) ? iface->name : "local";
924
925         struct device_prefix *prefix = calloc(1, sizeof(*prefix) + strlen(pclass) + 1);
926         prefix->length = length;
927         prefix->addr = *addr;
928         prefix->preferred_until = preferred_until;
929         prefix->valid_until = valid_until;
930         prefix->iface = iface;
931         INIT_LIST_HEAD(&prefix->assignments);
932
933         if (excl_addr) {
934                 prefix->excl_addr = *excl_addr;
935                 prefix->excl_length = excl_length;
936         }
937
938         strcpy(prefix->pclass, pclass);
939
940         if (iface)
941                 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
942         else
943                 interface_update_prefix(NULL, &prefix->node, NULL);
944
945         return prefix;
946 }
947
948 void
949 interface_ip_set_ula_prefix(const char *prefix)
950 {
951         char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
952         if (prefix)
953                 strncpy(buf, prefix, sizeof(buf) - 1);
954         char *prefixaddr = strtok_r(buf, "/", &saveptr);
955
956         struct in6_addr addr;
957         if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
958                 if (ula_prefix) {
959                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
960                         ula_prefix = NULL;
961                 }
962                 return;
963         }
964
965         int length;
966         char *prefixlen = strtok_r(NULL, ",", &saveptr);
967         if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
968                 return;
969
970         if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
971                         ula_prefix->length != length) {
972                 if (ula_prefix)
973                         interface_update_prefix(NULL, NULL, &ula_prefix->node);
974
975                 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
976                                 0, 0, NULL, 0, NULL);
977         }
978 }
979
980 void
981 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
982 {
983         struct dns_server *s;
984
985         s = calloc(1, sizeof(*s));
986         if (!s)
987                 return;
988
989         s->af = AF_INET;
990         if (inet_pton(s->af, str, &s->addr.in))
991                 goto add;
992
993         s->af = AF_INET6;
994         if (inet_pton(s->af, str, &s->addr.in))
995                 goto add;
996
997         free(s);
998         return;
999
1000 add:
1001         D(INTERFACE, "Add IPv%c DNS server: %s\n",
1002           s->af == AF_INET6 ? '6' : '4', str);
1003         vlist_simple_add(&ip->dns_servers, &s->node);
1004 }
1005
1006 void
1007 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
1008 {
1009         struct blob_attr *cur;
1010         int rem;
1011
1012         blobmsg_for_each_attr(cur, list, rem) {
1013                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1014                         continue;
1015
1016                 if (!blobmsg_check_attr(cur, NULL))
1017                         continue;
1018
1019                 interface_add_dns_server(ip, blobmsg_data(cur));
1020         }
1021 }
1022
1023 static void
1024 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
1025 {
1026         struct dns_search_domain *s;
1027         int len = strlen(str);
1028
1029         s = calloc(1, sizeof(*s) + len + 1);
1030         if (!s)
1031                 return;
1032
1033         D(INTERFACE, "Add DNS search domain: %s\n", str);
1034         memcpy(s->name, str, len);
1035         vlist_simple_add(&ip->dns_search, &s->node);
1036 }
1037
1038 void
1039 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
1040 {
1041         struct blob_attr *cur;
1042         int rem;
1043
1044         blobmsg_for_each_attr(cur, list, rem) {
1045                 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1046                         continue;
1047
1048                 if (!blobmsg_check_attr(cur, NULL))
1049                         continue;
1050
1051                 interface_add_dns_search_domain(ip, blobmsg_data(cur));
1052         }
1053 }
1054
1055 static void
1056 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip, const char *dev)
1057 {
1058         struct dns_server *s;
1059         struct dns_search_domain *d;
1060         const char *str;
1061         char buf[INET6_ADDRSTRLEN];
1062
1063         vlist_simple_for_each_element(&ip->dns_servers, s, node) {
1064                 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
1065                 if (!str)
1066                         continue;
1067
1068                 if (s->af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&s->addr.in6))
1069                         fprintf(f, "nameserver %s%%%s\n", str, dev);
1070                 else
1071                         fprintf(f, "nameserver %s\n", str);
1072         }
1073
1074         vlist_simple_for_each_element(&ip->dns_search, d, node) {
1075                 fprintf(f, "search %s\n", d->name);
1076         }
1077 }
1078
1079 void
1080 interface_write_resolv_conf(void)
1081 {
1082         struct interface *iface;
1083         char *path = alloca(strlen(resolv_conf) + 5);
1084         FILE *f;
1085         uint32_t crcold, crcnew;
1086
1087         sprintf(path, "%s.tmp", resolv_conf);
1088         unlink(path);
1089         f = fopen(path, "w+");
1090         if (!f) {
1091                 D(INTERFACE, "Failed to open %s for writing\n", path);
1092                 return;
1093         }
1094
1095         vlist_for_each_element(&interfaces, iface, node) {
1096                 if (iface->state != IFS_UP)
1097                         continue;
1098
1099                 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
1100                     vlist_simple_empty(&iface->proto_ip.dns_servers) &&
1101                         vlist_simple_empty(&iface->config_ip.dns_search) &&
1102                     vlist_simple_empty(&iface->config_ip.dns_servers))
1103                         continue;
1104
1105                 fprintf(f, "# Interface %s\n", iface->name);
1106                 write_resolv_conf_entries(f, &iface->config_ip, iface->ifname);
1107                 if (!iface->proto_ip.no_dns)
1108                         write_resolv_conf_entries(f, &iface->proto_ip, iface->ifname);
1109         }
1110         fflush(f);
1111         rewind(f);
1112         crcnew = crc32_file(f);
1113         fclose(f);
1114
1115         crcold = crcnew + 1;
1116         f = fopen(resolv_conf, "r");
1117         if (f) {
1118                 crcold = crc32_file(f);
1119                 fclose(f);
1120         }
1121
1122         if (crcold == crcnew) {
1123                 unlink(path);
1124         } else if (rename(path, resolv_conf) < 0) {
1125                 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
1126                 unlink(path);
1127         }
1128 }
1129
1130 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
1131 {
1132         struct device_addr *addr;
1133         struct device_route *route;
1134         struct device *dev;
1135
1136         ip->enabled = enabled;
1137         dev = ip->iface->l3_dev.dev;
1138         if (!dev)
1139                 return;
1140
1141         vlist_for_each_element(&ip->addr, addr, node) {
1142                 if (addr->enabled == enabled)
1143                         continue;
1144
1145                 if (enabled)
1146                         system_add_address(dev, addr);
1147                 else
1148                         system_del_address(dev, addr);
1149                 addr->enabled = enabled;
1150         }
1151
1152         vlist_for_each_element(&ip->route, route, node) {
1153                 bool _enabled = enabled;
1154
1155                 if (!enable_route(ip, route))
1156                         _enabled = false;
1157
1158                 if (route->enabled == _enabled)
1159                         continue;
1160
1161                 if (_enabled) {
1162                         if (!(route->flags & DEVROUTE_METRIC))
1163                                 route->metric = ip->iface->metric;
1164
1165                         if (system_add_route(dev, route))
1166                                 route->failed = true;
1167                 } else
1168                         system_del_route(dev, route);
1169                 route->enabled = _enabled;
1170         }
1171
1172         struct device_prefix *c;
1173         struct device_prefix_assignment *a;
1174         list_for_each_entry(c, &prefixes, head)
1175                 list_for_each_entry(a, &c->assignments, head)
1176                         if (!strcmp(a->name, ip->iface->name))
1177                                 interface_set_prefix_address(a, c, ip->iface, enabled);
1178
1179         if (ip->iface && ip->iface->l3_dev.dev) {
1180                 set_ip_lo_policy(enabled, true, ip->iface);
1181                 set_ip_lo_policy(enabled, false, ip->iface);
1182
1183                 set_ip_source_policy(enabled, true, IPRULE_PRIORITY_REJECT + ip->iface->l3_dev.dev->ifindex,
1184                         NULL, 0, 0, ip->iface, "failed_policy");
1185         }
1186 }
1187
1188 void
1189 interface_ip_update_start(struct interface_ip_settings *ip)
1190 {
1191         if (ip != &ip->iface->config_ip) {
1192                 vlist_simple_update(&ip->dns_servers);
1193                 vlist_simple_update(&ip->dns_search);
1194         }
1195         vlist_update(&ip->route);
1196         vlist_update(&ip->addr);
1197         vlist_update(&ip->prefix);
1198 }
1199
1200 void
1201 interface_ip_update_complete(struct interface_ip_settings *ip)
1202 {
1203         vlist_simple_flush(&ip->dns_servers);
1204         vlist_simple_flush(&ip->dns_search);
1205         vlist_flush(&ip->route);
1206         vlist_flush(&ip->addr);
1207         vlist_flush(&ip->prefix);
1208         interface_write_resolv_conf();
1209 }
1210
1211 void
1212 interface_ip_flush(struct interface_ip_settings *ip)
1213 {
1214         if (ip == &ip->iface->proto_ip)
1215                 vlist_flush_all(&ip->iface->host_routes);
1216         vlist_simple_flush_all(&ip->dns_servers);
1217         vlist_simple_flush_all(&ip->dns_search);
1218         vlist_flush_all(&ip->route);
1219         vlist_flush_all(&ip->addr);
1220         vlist_flush_all(&ip->prefix);
1221 }
1222
1223 static void
1224 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
1225 {
1226         ip->iface = iface;
1227         ip->enabled = true;
1228         vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
1229         vlist_simple_init(&ip->dns_servers, struct dns_server, node);
1230         vlist_init(&ip->route, route_cmp, interface_update_proto_route);
1231         vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
1232         vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
1233 }
1234
1235 void
1236 interface_ip_init(struct interface *iface)
1237 {
1238         __interface_ip_init(&iface->proto_ip, iface);
1239         __interface_ip_init(&iface->config_ip, iface);
1240         vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
1241 }
1242
1243 static void
1244 interface_ip_valid_until_handler(struct uloop_timeout *t)
1245 {
1246         time_t now = system_get_rtime();
1247         struct interface *iface;
1248         vlist_for_each_element(&interfaces, iface, node) {
1249                 if (iface->state != IFS_UP)
1250                         continue;
1251
1252                 struct device_addr *addr, *addrp;
1253                 struct device_route *route, *routep;
1254                 struct device_prefix *pref, *prefp;
1255
1256                 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
1257                         if (addr->valid_until && addr->valid_until < now)
1258                                 vlist_delete(&iface->proto_ip.addr, &addr->node);
1259
1260                 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
1261                         if (route->valid_until && route->valid_until < now)
1262                                 vlist_delete(&iface->proto_ip.route, &route->node);
1263
1264                 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
1265                         if (pref->valid_until && pref->valid_until < now)
1266                                 vlist_delete(&iface->proto_ip.prefix, &pref->node);
1267
1268         }
1269
1270         uloop_timeout_set(t, 1000);
1271 }
1272
1273 static void __init
1274 interface_ip_init_worker(void)
1275 {
1276         valid_until_timeout.cb = interface_ip_valid_until_handler;
1277         uloop_timeout_set(&valid_until_timeout, 1000);
1278 }