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