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