wireless: dynamically enable/disable virtual interfaces base on network interface...
[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 #include <libgen.h>
19 #include <sys/stat.h>
20
21 #include <limits.h>
22 #include <arpa/inet.h>
23 #include <netinet/in.h>
24
25 #include "netifd.h"
26 #include "device.h"
27 #include "interface.h"
28 #include "interface-ip.h"
29 #include "proto.h"
30 #include "ubus.h"
31 #include "system.h"
32
33 enum {
34 ROUTE_INTERFACE,
35 ROUTE_TARGET,
36 ROUTE_MASK,
37 ROUTE_GATEWAY,
38 ROUTE_METRIC,
39 ROUTE_MTU,
40 ROUTE_VALID,
41 ROUTE_TABLE,
42 ROUTE_SOURCE,
43 ROUTE_ONLINK,
44 ROUTE_TYPE,
45 ROUTE_PROTO,
46 ROUTE_DISABLED,
47 __ROUTE_MAX
48 };
49
50 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
51 [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
52 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
53 [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
54 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
55 [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
56 [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
57 [ROUTE_TABLE] = { .name = "table", .type = BLOBMSG_TYPE_STRING },
58 [ROUTE_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
59 [ROUTE_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_STRING },
60 [ROUTE_ONLINK] = { .name = "onlink", .type = BLOBMSG_TYPE_BOOL },
61 [ROUTE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
62 [ROUTE_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
63 [ROUTE_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
64 };
65
66 const struct uci_blob_param_list route_attr_list = {
67 .n_params = __ROUTE_MAX,
68 .params = route_attr,
69 };
70
71 enum {
72 NEIGHBOR_INTERFACE,
73 NEIGHBOR_ADDRESS,
74 NEIGHBOR_MAC,
75 NEIGHBOR_PROXY,
76 NEIGHBOR_ROUTER,
77 __NEIGHBOR_MAX
78 };
79
80 static const struct blobmsg_policy neighbor_attr[__NEIGHBOR_MAX]={
81 [NEIGHBOR_INTERFACE]= { .name = "interface", .type = BLOBMSG_TYPE_STRING},
82 [NEIGHBOR_ADDRESS]= { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING},
83 [NEIGHBOR_MAC]= { .name = "mac", .type = BLOBMSG_TYPE_STRING},
84 [NEIGHBOR_PROXY]= { .name = "proxy", .type = BLOBMSG_TYPE_BOOL},
85 [NEIGHBOR_ROUTER]= {.name = "router", .type = BLOBMSG_TYPE_BOOL},
86 };
87
88 const struct uci_blob_param_list neighbor_attr_list = {
89 .n_params = __NEIGHBOR_MAX,
90 .params = neighbor_attr,
91 };
92
93
94 struct list_head prefixes = LIST_HEAD_INIT(prefixes);
95 static struct device_prefix *ula_prefix = NULL;
96 static struct uloop_timeout valid_until_timeout;
97
98
99 static void
100 clear_if_addr(union if_addr *a, int mask)
101 {
102 size_t m_bytes = (mask + 7) / 8;
103 uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
104 uint8_t *p = (uint8_t *) a;
105
106 if (m_bytes < sizeof(*a))
107 memset(p + m_bytes, 0, sizeof(*a) - m_bytes);
108
109 p[m_bytes - 1] &= ~m_clear;
110 }
111
112 static bool
113 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
114 {
115 union if_addr *p1, *p2;
116
117 p1 = alloca(sizeof(*a1));
118 p2 = alloca(sizeof(*a2));
119
120 memcpy(p1, a1, sizeof(*a1));
121 clear_if_addr(p1, mask);
122 memcpy(p2, a2, sizeof(*a2));
123 clear_if_addr(p2, mask);
124
125 return !memcmp(p1, p2, sizeof(*p1));
126 }
127
128 static int set_ip_source_policy(bool add, bool v6, unsigned int priority,
129 const union if_addr *addr, uint8_t mask, unsigned int table,
130 struct interface *in_iface, const char *action, bool src)
131 {
132 struct iprule rule = {
133 .flags = IPRULE_PRIORITY,
134 .priority = priority
135 };
136
137 if (addr) {
138 if (src) {
139 rule.flags |= IPRULE_SRC;
140 rule.src_addr = *addr;
141 rule.src_mask = mask;
142 } else {
143 rule.flags |= IPRULE_DEST;
144 rule.dest_addr = *addr;
145 rule.dest_mask = mask;
146 }
147 }
148
149 if (table) {
150 rule.flags |= IPRULE_LOOKUP;
151 rule.lookup = table;
152
153 if (!rule.lookup)
154 return 0;
155 } else if (action) {
156 rule.flags |= IPRULE_ACTION;
157 system_resolve_iprule_action(action, &rule.action);
158 }
159
160 if (in_iface && in_iface->l3_dev.dev) {
161 rule.flags |= IPRULE_IN;
162 strcpy(rule.in_dev, in_iface->l3_dev.dev->ifname);
163 }
164
165 rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
166
167 return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
168 }
169
170 static int set_ip_lo_policy(bool add, bool v6, struct interface *iface)
171 {
172 struct iprule rule = {
173 .flags = IPRULE_IN | IPRULE_LOOKUP | IPRULE_PRIORITY,
174 .priority = IPRULE_PRIORITY_NW + iface->l3_dev.dev->ifindex,
175 .lookup = (v6) ? iface->ip6table : iface->ip4table,
176 .in_dev = "lo"
177 };
178
179 if (!rule.lookup)
180 return 0;
181
182 rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
183
184 return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
185 }
186
187 static bool
188 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
189 {
190 struct device_addr *addr;
191
192 vlist_for_each_element(&ip->addr, addr, node) {
193 if (!addr->enabled)
194 continue;
195
196 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
197 continue;
198
199 if (((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4) &&
200 addr->point_to_point && a->in.s_addr == addr->point_to_point)
201 return true;
202
203 /* Handle offlink addresses correctly */
204 unsigned int mask = addr->mask;
205 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6 &&
206 (addr->flags & DEVADDR_OFFLINK))
207 mask = 128;
208
209 if (!match_if_addr(&addr->addr, a, mask))
210 continue;
211
212 return true;
213 }
214
215 return false;
216 }
217
218 static void
219 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
220 bool v6, struct device_route **res)
221 {
222 struct device_route *route;
223
224 vlist_for_each_element(&ip->route, route, node) {
225 if (!route->enabled)
226 continue;
227
228 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
229 continue;
230
231 if (!match_if_addr(&route->addr, a, route->mask))
232 continue;
233
234 if (route->flags & DEVROUTE_TABLE)
235 continue;
236
237 if (!*res || route->mask > (*res)->mask ||
238 ((route->mask == (*res)->mask) && (route->flags & DEVROUTE_METRIC)
239 && (route->metric < (*res)->metric)))
240 *res = route;
241 }
242 }
243
244 static bool
245 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
246 {
247 return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
248 __find_ip_addr_target(&iface->config_ip, a, v6);
249 }
250
251 static void
252 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
253 bool v6, struct device_route **route)
254 {
255 __find_ip_route_target(&iface->proto_ip, a, v6, route);
256 __find_ip_route_target(&iface->config_ip, a, v6, route);
257 }
258
259 struct interface *
260 interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *iface,
261 bool exclude)
262 {
263 struct device_route *route, *r_next = NULL;
264 bool defaultroute_target = false;
265 union if_addr addr_zero;
266 int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
267 struct interface *exclude_iface = NULL;
268
269 if (exclude) {
270 exclude_iface = iface;
271 iface = NULL;
272 }
273
274 memset(&addr_zero, 0, sizeof(addr_zero));
275 if (memcmp(&addr_zero, addr, addrsize) == 0)
276 defaultroute_target = true;
277
278 if (iface) {
279 /* look for locally addressable target first */
280 if (interface_ip_find_addr_target(iface, addr, v6))
281 return iface;
282
283 /* do not stop at the first route, let the lookup compare
284 * masks to find the best match */
285 interface_ip_find_route_target(iface, addr, v6, &r_next);
286 } else {
287 vlist_for_each_element(&interfaces, iface, node) {
288 if (iface == exclude_iface)
289 continue;
290
291 /* look for locally addressable target first */
292 if (interface_ip_find_addr_target(iface, addr, v6))
293 return iface;
294
295 /* do not stop at the first route, let the lookup compare
296 * masks to find the best match */
297 interface_ip_find_route_target(iface, addr, v6, &r_next);
298 }
299 }
300
301 if (!r_next)
302 return NULL;
303
304 iface = r_next->iface;
305 if (defaultroute_target)
306 return iface;
307
308 route = calloc(1, sizeof(*route));
309 if (!route)
310 return NULL;
311
312 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
313 route->mask = v6 ? 128 : 32;
314 memcpy(&route->addr, addr, addrsize);
315 memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
316 route->mtu = r_next->mtu;
317 route->metric = r_next->metric;
318 route->table = r_next->table;
319 route->iface = iface;
320 vlist_add(&iface->host_routes, &route->node, route);
321
322 return iface;
323 }
324
325 static void
326 interface_set_route_info(struct interface *iface, struct device_route *route)
327 {
328 bool v6 = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6);
329
330 if (!iface)
331 return;
332
333 if (!(route->flags & DEVROUTE_METRIC))
334 route->metric = iface->metric;
335
336 if (!(route->flags & DEVROUTE_TABLE)) {
337 route->table = (v6) ? iface->ip6table : iface->ip4table;
338 if (route->table)
339 route->flags |= DEVROUTE_SRCTABLE;
340 }
341 }
342
343 void
344 interface_ip_add_neighbor(struct interface *iface, struct blob_attr *attr, bool v6)
345 {
346 struct interface_ip_settings *ip;
347 struct blob_attr *tb[__NEIGHBOR_MAX], *cur;
348 struct device_neighbor *neighbor;
349 int af = v6 ? AF_INET6: AF_INET;
350 struct ether_addr *ea;
351
352 blobmsg_parse(neighbor_attr, __NEIGHBOR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
353
354 if (!iface) {
355 if ((cur = tb[NEIGHBOR_INTERFACE]) == NULL)
356 return;
357
358 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
359
360 if (!iface)
361 return;
362
363 ip = &iface->config_ip;
364 } else
365 ip = &iface->proto_ip;
366
367 neighbor = calloc(1,sizeof(*neighbor));
368 if (!neighbor)
369 return;
370
371 neighbor->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
372
373 if ((cur = tb[NEIGHBOR_ADDRESS]) != NULL){
374 if (!inet_pton(af, blobmsg_data(cur), &neighbor->addr))
375 goto error;
376 } else
377 goto error;
378
379 if ((cur = tb[NEIGHBOR_MAC]) != NULL) {
380 neighbor->flags |= DEVNEIGH_MAC;
381 ea = ether_aton(blobmsg_data(cur));
382 if (!ea)
383 goto error;
384
385 memcpy(neighbor->macaddr, ea, 6);
386 }
387
388 if ((cur = tb[NEIGHBOR_PROXY]) != NULL)
389 neighbor->proxy = blobmsg_get_bool(cur);
390
391 if ((cur = tb[NEIGHBOR_ROUTER]) != NULL)
392 neighbor->router = blobmsg_get_bool(cur);
393
394 vlist_add(&ip->neighbor, &neighbor->node, neighbor);
395 return;
396
397 error:
398 free(neighbor);
399 }
400
401 void
402 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
403 {
404 struct interface_ip_settings *ip;
405 struct blob_attr *tb[__ROUTE_MAX], *cur;
406 struct device_route *route;
407 int af = v6 ? AF_INET6 : AF_INET;
408
409 blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
410
411 if ((cur = tb[ROUTE_DISABLED]) != NULL && blobmsg_get_bool(cur))
412 return;
413
414 if (!iface) {
415 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
416 return;
417
418 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
419 if (!iface)
420 return;
421
422 ip = &iface->config_ip;
423 } else {
424 ip = &iface->proto_ip;
425 }
426
427 route = calloc(1, sizeof(*route));
428 if (!route)
429 return;
430
431 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
432 route->mask = v6 ? 128 : 32;
433 if ((cur = tb[ROUTE_MASK]) != NULL) {
434 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
435 if (route->mask > (v6 ? 128 : 32))
436 goto error;
437 }
438
439 if ((cur = tb[ROUTE_TARGET]) != NULL) {
440 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
441 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
442 goto error;
443 }
444 }
445
446 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
447 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
448 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
449 goto error;
450 }
451 }
452
453 if ((cur = tb[ROUTE_METRIC]) != NULL) {
454 route->metric = blobmsg_get_u32(cur);
455 route->flags |= DEVROUTE_METRIC;
456 }
457
458 if ((cur = tb[ROUTE_MTU]) != NULL) {
459 route->mtu = blobmsg_get_u32(cur);
460 route->flags |= DEVROUTE_MTU;
461 }
462
463 /* Use source-based routing */
464 if ((cur = tb[ROUTE_SOURCE]) != NULL) {
465 char *saveptr, *source = alloca(blobmsg_data_len(cur));
466 memcpy(source, blobmsg_data(cur), blobmsg_data_len(cur));
467
468 const char *addr = strtok_r(source, "/", &saveptr);
469 const char *mask = strtok_r(NULL, "/", &saveptr);
470
471 if (!addr || inet_pton(af, addr, &route->source) < 1) {
472 DPRINTF("Failed to parse route source: %s\n", addr ? addr : "NULL");
473 goto error;
474 }
475
476 route->sourcemask = (mask) ? atoi(mask) : ((af == AF_INET6) ? 128 : 32);
477 }
478
479 if ((cur = tb[ROUTE_ONLINK]) != NULL && blobmsg_get_bool(cur))
480 route->flags |= DEVROUTE_ONLINK;
481
482 if ((cur = tb[ROUTE_TABLE]) != NULL) {
483 if (!system_resolve_rt_table(blobmsg_data(cur), &route->table)) {
484 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
485 goto error;
486 }
487
488 /* only set the table flag if not using the main (default) table */
489 if (system_is_default_rt_table(route->table))
490 route->table = 0;
491
492 if (route->table)
493 route->flags |= DEVROUTE_TABLE;
494 }
495
496 if ((cur = tb[ROUTE_VALID]) != NULL) {
497 int64_t valid = blobmsg_get_u32(cur);
498 int64_t valid_until = valid + (int64_t)system_get_rtime();
499 if (valid_until <= LONG_MAX && valid != 0xffffffffLL) /* Catch overflow */
500 route->valid_until = valid_until;
501 }
502
503 if ((cur = tb[ROUTE_TYPE]) != NULL) {
504 if (!system_resolve_rt_type(blobmsg_data(cur), &route->type)) {
505 DPRINTF("Failed to resolve routing type: %s\n", (char *) blobmsg_data(cur));
506 goto error;
507 }
508 route->flags |= DEVROUTE_TYPE;
509 }
510
511 if ((cur = tb[ROUTE_PROTO]) != NULL) {
512 if (!system_resolve_rt_proto(blobmsg_data(cur), &route->proto)) {
513 DPRINTF("Failed to resolve proto type: %s\n", (char *) blobmsg_data(cur));
514 goto error;
515 }
516 route->flags |= DEVROUTE_PROTO;
517 }
518
519 interface_set_route_info(iface, route);
520 vlist_add(&ip->route, &route->node, route);
521 return;
522
523 error:
524 free(route);
525 }
526
527 static int
528 addr_cmp(const void *k1, const void *k2, void *ptr)
529 {
530 const struct device_addr *a1 = k1;
531 const struct device_addr *a2 = k2;
532 const int cmp_offset = offsetof(struct device_addr, flags);
533 const int cmp_size = sizeof(struct device_addr) - cmp_offset;
534
535 if (a1->index != a2->index)
536 return a1->index - a2->index;
537 return memcmp(k1+cmp_offset, k2+cmp_offset, cmp_size);
538 }
539
540 static int
541 neighbor_cmp(const void *k1, const void *k2, void *ptr)
542 {
543 const struct device_neighbor *n1 = k1, *n2 = k2;
544
545 return memcmp(&n1->addr, &n2->addr, sizeof(n2->addr));
546 }
547
548 static int
549 route_cmp(const void *k1, const void *k2, void *ptr)
550 {
551 const struct device_route *r1 = k1, *r2 = k2;
552
553 if (r1->mask != r2->mask)
554 return r2->mask - r1->mask;
555
556 if (r1->metric != r2->metric)
557 return r1->metric - r2->metric;
558
559 if (r1->flags != r2->flags)
560 return r2->flags - r1->flags;
561
562 if (r1->sourcemask != r2->sourcemask)
563 return r1->sourcemask - r2->sourcemask;
564
565 if (r1->table != r2->table)
566 return r1->table - r2->table;
567
568 int maskcmp = memcmp(&r1->source, &r2->source, sizeof(r1->source));
569 if (maskcmp)
570 return maskcmp;
571
572 return memcmp(&r1->addr, &r2->addr, sizeof(r1->addr));
573 }
574
575 static int
576 prefix_cmp(const void *k1, const void *k2, void *ptr)
577 {
578 return memcmp(k1, k2, offsetof(struct device_prefix, pclass) -
579 offsetof(struct device_prefix, addr));
580 }
581
582 static void
583 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
584 {
585 struct device *dev = iface->l3_dev.dev;
586 struct device_route *r = &addr->subnet;
587
588 if (addr->flags & DEVADDR_OFFLINK)
589 return;
590
591 if (!add) {
592 if (!addr->subnet.iface)
593 return;
594
595 system_del_route(dev, r);
596 memset(r, 0, sizeof(*r));
597 return;
598 }
599
600 r->iface = iface;
601 r->flags = addr->flags;
602 r->mask = addr->mask;
603 memcpy(&r->addr, &addr->addr, sizeof(r->addr));
604 clear_if_addr(&r->addr, r->mask);
605
606 if (!system_resolve_rt_proto("kernel", &r->proto))
607 return;
608
609 r->flags |= DEVROUTE_PROTO;
610 system_del_route(dev, r);
611
612 r->flags &= ~DEVROUTE_PROTO;
613 interface_set_route_info(iface, r);
614
615 system_add_route(dev, r);
616 }
617
618 static void
619 interface_add_addr_rules(struct device_addr *addr, bool enabled)
620 {
621 bool v6 = (addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6;
622
623 set_ip_source_policy(enabled, v6, IPRULE_PRIORITY_ADDR, &addr->addr,
624 (v6) ? 128 : 32, addr->policy_table, NULL, NULL,
625 true);
626 set_ip_source_policy(enabled, v6, IPRULE_PRIORITY_ADDR_MASK,
627 &addr->addr, addr->mask, addr->policy_table, NULL,
628 NULL, false);
629 }
630
631 static void
632 interface_update_proto_addr(struct vlist_tree *tree,
633 struct vlist_node *node_new,
634 struct vlist_node *node_old)
635 {
636 struct interface_ip_settings *ip;
637 struct interface *iface;
638 struct device *dev;
639 struct device_addr *a_new = NULL, *a_old = NULL;
640 bool replace = false;
641 bool keep = false;
642 bool v6 = false;
643
644 ip = container_of(tree, struct interface_ip_settings, addr);
645 iface = ip->iface;
646 dev = iface->l3_dev.dev;
647
648 if (!node_new || !node_old)
649 iface->updated |= IUF_ADDRESS;
650
651 if (node_new) {
652 a_new = container_of(node_new, struct device_addr, node);
653
654 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
655 !a_new->broadcast) {
656
657 /* /31 and /32 addressing need 255.255.255.255
658 * as broadcast address. */
659 if (a_new->mask >= 31) {
660 a_new->broadcast = (uint32_t) ~0;
661 } else {
662 uint32_t mask = ~0;
663 uint32_t *a = (uint32_t *) &a_new->addr;
664
665 mask >>= a_new->mask;
666 a_new->broadcast = *a | htonl(mask);
667 }
668 }
669 }
670
671 if (node_old)
672 a_old = container_of(node_old, struct device_addr, node);
673
674 if (a_new && a_old) {
675 keep = true;
676
677 if (a_old->flags != a_new->flags || a_old->failed)
678 keep = false;
679
680 if (a_old->valid_until != a_new->valid_until ||
681 a_old->preferred_until != a_new->preferred_until)
682 replace = true;
683
684 if (((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4) &&
685 (a_new->broadcast != a_old->broadcast ||
686 a_new->point_to_point != a_old->point_to_point))
687 keep = false;
688 }
689
690 if (node_old) {
691 if (a_old->enabled && !keep) {
692 /*
693 * This is needed for source routing to work correctly. If a device
694 * has two connections to a network using the same subnet, adding
695 * only the network-rule will cause packets to be routed through the
696 * first matching network (source IP matches both masks)
697 */
698 if (a_old->policy_table)
699 interface_add_addr_rules(a_old, false);
700
701 if (!(a_old->flags & DEVADDR_EXTERNAL)) {
702 interface_handle_subnet_route(iface, a_old, false);
703 system_del_address(dev, a_old);
704
705 if ((a_old->flags & DEVADDR_OFFLINK) && (a_old->mask < (v6 ? 128 : 32))) {
706 struct device_route route;
707
708 memset(&route, 0, sizeof(route));
709 route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
710 route.metric = INT32_MAX;
711 route.mask = a_old->mask;
712 route.addr = a_old->addr;
713
714 clear_if_addr(&route.addr, route.mask);
715
716 /* Delete null-route */
717 system_del_route(NULL, &route);
718 }
719
720 }
721 }
722 free(a_old->pclass);
723 free(a_old);
724 }
725
726 if (node_new) {
727 a_new->enabled = true;
728
729 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET6)
730 v6 = true;
731
732 a_new->policy_table = (v6) ? iface->ip6table : iface->ip4table;
733
734 if (!keep || replace) {
735 if (!(a_new->flags & DEVADDR_EXTERNAL)) {
736 if (system_add_address(dev, a_new))
737 a_new->failed = true;
738
739 if (iface->metric || a_new->policy_table)
740 interface_handle_subnet_route(iface, a_new, true);
741 }
742
743 if (!keep) {
744 if (!(a_new->flags & DEVADDR_EXTERNAL) &&
745 (a_new->flags & DEVADDR_OFFLINK) &&
746 (a_new->mask < (v6 ? 128 : 32))) {
747 struct device_route route;
748
749 memset(&route, 0, sizeof(route));
750 route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
751 route.metric = INT32_MAX;
752 route.mask = a_new->mask;
753 route.addr = a_new->addr;
754
755 clear_if_addr(&route.addr, route.mask);
756
757 /*
758 * In case off link is specifed as address property
759 * add null-route to avoid routing loops
760 */
761 system_add_route(NULL, &route);
762 }
763
764 if (a_new->policy_table)
765 interface_add_addr_rules(a_new, true);
766 }
767 }
768 }
769 }
770
771 static bool
772 enable_route(struct interface_ip_settings *ip, struct device_route *route)
773 {
774 if (ip->no_defaultroute && !route->mask)
775 return false;
776
777 return ip->enabled;
778 }
779
780 static void
781 interface_update_proto_neighbor(struct vlist_tree *tree,
782 struct vlist_node * node_new,
783 struct vlist_node *node_old)
784 {
785 struct device *dev;
786 struct device_neighbor *neighbor_old, *neighbor_new;
787 struct interface_ip_settings *ip;
788 bool keep = false;
789
790 ip = container_of(tree, struct interface_ip_settings, neighbor);
791 dev = ip->iface->l3_dev.dev;
792
793 neighbor_old = container_of(node_old, struct device_neighbor, node);
794 neighbor_new = container_of(node_new, struct device_neighbor, node);
795
796 if (node_old && node_new) {
797 keep = (!memcmp(neighbor_old->macaddr, neighbor_new->macaddr, sizeof(neighbor_old->macaddr)) &&
798 (neighbor_old->proxy == neighbor_new->proxy) &&
799 (neighbor_old->router == neighbor_new->router));
800 }
801
802 if (node_old) {
803 if (!keep && neighbor_old->enabled)
804 system_del_neighbor(dev, neighbor_old);
805
806 free(neighbor_old);
807 }
808
809 if (node_new) {
810 if (!keep && ip->enabled)
811 if (system_add_neighbor(dev, neighbor_new))
812 neighbor_new->failed = true;
813
814 neighbor_new->enabled = ip->enabled;
815 }
816 }
817
818 static void
819 __interface_update_route(struct interface_ip_settings *ip,
820 struct vlist_node *node_new,
821 struct vlist_node *node_old)
822 {
823 struct interface *iface = ip->iface;
824 struct device *dev;
825 struct device_route *route_old, *route_new;
826 bool keep = false;
827
828 dev = iface->l3_dev.dev;
829
830 if (!node_new || !node_old)
831 iface->updated |= IUF_ROUTE;
832
833 route_old = container_of(node_old, struct device_route, node);
834 route_new = container_of(node_new, struct device_route, node);
835
836 if (node_old && node_new)
837 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop)) &&
838 (route_old->mtu == route_new->mtu) && (route_old->type == route_new->type) &&
839 (route_old->proto == route_new->proto) && !route_old->failed;
840
841 if (node_old) {
842 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
843 system_del_route(dev, route_old);
844
845 free(route_old);
846 }
847
848 if (node_new) {
849 bool _enabled = enable_route(ip, route_new);
850
851 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
852 if (system_add_route(dev, route_new))
853 route_new->failed = true;
854
855 route_new->iface = iface;
856 route_new->enabled = _enabled;
857 }
858 }
859
860 static void
861 interface_update_proto_route(struct vlist_tree *tree,
862 struct vlist_node *node_new,
863 struct vlist_node *node_old)
864 {
865 struct interface_ip_settings *ip;
866
867 ip = container_of(tree, struct interface_ip_settings, route);
868 __interface_update_route(ip, node_new, node_old);
869 }
870
871 static void
872 interface_update_host_route(struct vlist_tree *tree,
873 struct vlist_node *node_new,
874 struct vlist_node *node_old)
875 {
876 struct interface *iface;
877
878 iface = container_of(tree, struct interface, host_routes);
879 __interface_update_route(&iface->proto_ip, node_new, node_old);
880 }
881
882 static void
883 random_ifaceid(struct in6_addr *addr)
884 {
885 static bool initialized = false;
886 struct timeval t;
887
888 if (!initialized) {
889 long int seed = 0;
890 gettimeofday(&t, NULL);
891 seed = t.tv_sec ^ t.tv_usec ^ getpid();
892 srand48(seed);
893 initialized = true;
894 }
895 addr->s6_addr32[2] = (uint32_t)mrand48();
896 addr->s6_addr32[3] = (uint32_t)mrand48();
897 }
898
899 static bool
900 eui64_ifaceid(struct interface *iface, struct in6_addr *addr)
901 {
902 struct device_settings st;
903
904 device_merge_settings(iface->l3_dev.dev, &st);
905
906 if (!(st.flags & DEV_OPT_MACADDR))
907 return false;
908
909 /* get mac address */
910 uint8_t *ifaceid = addr->s6_addr + 8;
911 memcpy(ifaceid, st.macaddr, 3);
912 memcpy(ifaceid + 5, st.macaddr + 3, 3);
913 ifaceid[3] = 0xff;
914 ifaceid[4] = 0xfe;
915 ifaceid[0] ^= 0x02;
916
917 return true;
918 }
919
920 static bool
921 generate_ifaceid(struct interface *iface, struct in6_addr *addr)
922 {
923 bool ret = true;
924
925 /* generate new iface id */
926 switch (iface->assignment_iface_id_selection) {
927 case IFID_FIXED:
928 /* fixed */
929 /* copy host part from assignment_fixed_iface_id */
930 memcpy(addr->s6_addr + 8, iface->assignment_fixed_iface_id.s6_addr + 8, 8);
931 break;
932 case IFID_RANDOM:
933 /* randomize last 64 bits */
934 random_ifaceid(addr);
935 break;
936 case IFID_EUI64:
937 /* eui64 */
938 ret = eui64_ifaceid(iface, addr);
939 break;
940 default:
941 ret = false;
942 break;
943 }
944 return ret;
945 }
946
947 static void
948 interface_set_prefix_address(struct device_prefix_assignment *assignment,
949 const struct device_prefix *prefix, struct interface *iface, bool add)
950 {
951 const struct interface *uplink = prefix->iface;
952 if (!iface->l3_dev.dev)
953 return;
954
955 struct device *l3_downlink = iface->l3_dev.dev;
956
957 struct device_addr addr;
958 struct device_route route;
959 memset(&addr, 0, sizeof(addr));
960 memset(&route, 0, sizeof(route));
961
962 addr.addr.in6 = assignment->addr;
963 addr.mask = assignment->length;
964 addr.flags = DEVADDR_INET6;
965 addr.preferred_until = prefix->preferred_until;
966 addr.valid_until = prefix->valid_until;
967
968 route.flags = DEVADDR_INET6;
969 route.mask = addr.mask < 64 ? 64 : addr.mask;
970 route.addr = addr.addr;
971
972 if (!add && assignment->enabled) {
973 time_t now = system_get_rtime();
974
975 if (addr.valid_until && addr.valid_until - 1 <= now) {
976 addr.valid_until = 0;
977 addr.preferred_until = 0;
978 } else {
979 /* Address is still valid; pass its ownership to kernel (see L-14 RFC 7084). */
980 addr.preferred_until = now;
981
982 if (!addr.valid_until || addr.valid_until > now + 7200)
983 addr.valid_until = now + 7200;
984 }
985
986 if (iface->ip6table)
987 set_ip_source_policy(false, true, IPRULE_PRIORITY_ADDR_MASK, &addr.addr,
988 addr.mask < 64 ? 64 : addr.mask, iface->ip6table, NULL, NULL, false);
989
990 if (prefix->iface) {
991 if (prefix->iface->ip6table)
992 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW, &addr.addr,
993 addr.mask, prefix->iface->ip6table, iface, NULL, true);
994
995 set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT, &addr.addr,
996 addr.mask, 0, iface, "unreachable", true);
997 }
998
999 clear_if_addr(&route.addr, route.mask);
1000 interface_set_route_info(iface, &route);
1001
1002 system_del_route(l3_downlink, &route);
1003 if (addr.valid_until)
1004 system_add_address(l3_downlink, &addr);
1005 else
1006 system_del_address(l3_downlink, &addr);
1007
1008 assignment->addr = in6addr_any;
1009 assignment->enabled = false;
1010 } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP)) {
1011 if (IN6_IS_ADDR_UNSPECIFIED(&addr.addr.in6)) {
1012 addr.addr.in6 = prefix->addr;
1013 addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
1014 if (!generate_ifaceid(iface, &addr.addr.in6))
1015 return;
1016
1017 assignment->addr = addr.addr.in6;
1018 route.addr = addr.addr;
1019 }
1020
1021 addr.flags |= DEVADDR_OFFLINK;
1022 if (system_add_address(l3_downlink, &addr))
1023 return;
1024
1025 if (!assignment->enabled) {
1026 if (iface->ip6table)
1027 set_ip_source_policy(true, true, IPRULE_PRIORITY_ADDR_MASK, &addr.addr,
1028 addr.mask < 64 ? 64 : addr.mask, iface->ip6table, NULL, NULL, false);
1029
1030 if (prefix->iface) {
1031 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT, &addr.addr,
1032 addr.mask, 0, iface, "unreachable", true);
1033
1034 if (prefix->iface->ip6table)
1035 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW, &addr.addr,
1036 addr.mask, prefix->iface->ip6table, iface, NULL, true);
1037 }
1038 }
1039
1040 clear_if_addr(&route.addr, route.mask);
1041 interface_set_route_info(iface, &route);
1042
1043 system_add_route(l3_downlink, &route);
1044
1045 if (uplink && uplink->l3_dev.dev && !(l3_downlink->settings.flags & DEV_OPT_MTU6)) {
1046 int mtu = system_update_ipv6_mtu(uplink->l3_dev.dev, 0);
1047 int mtu_old = system_update_ipv6_mtu(l3_downlink, 0);
1048
1049 if (mtu > 0 && mtu_old != mtu) {
1050 if (system_update_ipv6_mtu(l3_downlink, mtu) < 0 && mtu < mtu_old)
1051 netifd_log_message(L_WARNING, "Failed to set IPv6 mtu to %d "
1052 "on interface '%s'\n", mtu, iface->name);
1053 }
1054 }
1055
1056 assignment->enabled = true;
1057 }
1058 }
1059
1060 static bool interface_prefix_assign(struct list_head *list,
1061 struct device_prefix_assignment *assign)
1062 {
1063 int32_t current = 0, asize = (1 << (64 - assign->length)) - 1;
1064 struct device_prefix_assignment *c;
1065 list_for_each_entry(c, list, head) {
1066 if (assign->assigned != -1) {
1067 if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
1068 list_add_tail(&assign->head, &c->head);
1069 return true;
1070 }
1071 } else if (assign->assigned == -1) {
1072 current = (current + asize) & (~asize);
1073 if (current + asize < c->assigned) {
1074 assign->assigned = current;
1075 list_add_tail(&assign->head, &c->head);
1076 return true;
1077 }
1078 }
1079 current = (c->assigned + (1 << (64 - c->length)));
1080 }
1081 return false;
1082 }
1083
1084 /*
1085 * Sorting of assignment entries:
1086 * Primary on assignment length: smallest assignment first
1087 * Secondary on assignment weight: highest weight first
1088 * Finally alphabetical order of interface names
1089 */
1090 static int prefix_assignment_cmp(const void *k1, const void *k2, void *ptr)
1091 {
1092 const struct device_prefix_assignment *a1 = k1, *a2 = k2;
1093
1094 if (a1->length != a2->length)
1095 return a1->length - a2->length;
1096
1097 if (a1->weight != a2->weight)
1098 return a2->weight - a1->weight;
1099
1100 return strcmp(a1->name, a2->name);
1101 }
1102
1103 static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup)
1104 {
1105 struct device_prefix_assignment *c;
1106 struct interface *iface;
1107
1108 /* Delete all assignments */
1109 while (!list_empty(&prefix->assignments)) {
1110 c = list_first_entry(&prefix->assignments,
1111 struct device_prefix_assignment, head);
1112 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1113 interface_set_prefix_address(c, prefix, iface, false);
1114 list_del(&c->head);
1115 free(c);
1116 }
1117
1118 if (!setup)
1119 return;
1120
1121 /* End-of-assignment sentinel */
1122 c = malloc(sizeof(*c) + 1);
1123 if (!c)
1124 return;
1125
1126 c->assigned = 1 << (64 - prefix->length);
1127 c->length = 64;
1128 c->name[0] = 0;
1129 c->addr = in6addr_any;
1130 list_add(&c->head, &prefix->assignments);
1131
1132 /* Excluded prefix */
1133 if (prefix->excl_length > 0) {
1134 const char name[] = "!excluded";
1135 c = malloc(sizeof(*c) + sizeof(name));
1136 if (c) {
1137 c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
1138 ((1 << (64 - prefix->length)) - 1);
1139 c->length = prefix->excl_length;
1140 c->addr = in6addr_any;
1141 memcpy(c->name, name, sizeof(name));
1142 list_add(&c->head, &prefix->assignments);
1143 }
1144 }
1145
1146 bool assigned_any = false;
1147 struct {
1148 struct avl_node node;
1149 } *entry, *n_entry;
1150 struct avl_tree assign_later;
1151
1152 avl_init(&assign_later, prefix_assignment_cmp, false, NULL);
1153
1154 vlist_for_each_element(&interfaces, iface, node) {
1155 if (iface->assignment_length < 48 ||
1156 iface->assignment_length > 64)
1157 continue;
1158
1159 /* Test whether there is a matching class */
1160 if (!list_empty(&iface->assignment_classes)) {
1161 bool found = false;
1162
1163 struct interface_assignment_class *c;
1164 list_for_each_entry(c, &iface->assignment_classes, head) {
1165 if (!strcmp(c->name, prefix->pclass)) {
1166 found = true;
1167 break;
1168 }
1169 }
1170
1171 if (!found)
1172 continue;
1173 }
1174
1175 size_t namelen = strlen(iface->name) + 1;
1176 c = malloc(sizeof(*c) + namelen);
1177 if (!c)
1178 continue;
1179
1180 c->length = iface->assignment_length;
1181 c->assigned = iface->assignment_hint;
1182 c->weight = iface->assignment_weight;
1183 c->addr = in6addr_any;
1184 c->enabled = false;
1185 memcpy(c->name, iface->name, namelen);
1186
1187 /* First process all custom assignments, put all others in later-list */
1188 if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
1189 if (c->assigned != -1) {
1190 c->assigned = -1;
1191 netifd_log_message(L_WARNING, "Failed to assign requested subprefix "
1192 "of size %hhu for %s, trying other\n", c->length, c->name);
1193 }
1194
1195 entry = calloc(1, sizeof(*entry));
1196 if (!entry) {
1197 free(c);
1198 continue;
1199 }
1200
1201 entry->node.key = c;
1202 avl_insert(&assign_later, &entry->node);
1203 }
1204
1205 if (c->assigned != -1)
1206 assigned_any = true;
1207 }
1208
1209 /* Then try to assign all other + failed custom assignments */
1210 avl_for_each_element_safe(&assign_later, entry, node, n_entry) {
1211 bool assigned = false;
1212
1213 c = (struct device_prefix_assignment *)entry->node.key;
1214 avl_delete(&assign_later, &entry->node);
1215
1216 do {
1217 assigned = interface_prefix_assign(&prefix->assignments, c);
1218 } while (!assigned && ++c->length <= 64);
1219
1220 if (!assigned) {
1221 netifd_log_message(L_WARNING, "Failed to assign subprefix "
1222 "of size %hhu for %s\n", c->length, c->name);
1223 free(c);
1224 } else
1225 assigned_any = true;
1226
1227 free(entry);
1228 }
1229
1230 list_for_each_entry(c, &prefix->assignments, head)
1231 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1232 interface_set_prefix_address(c, prefix, iface, true);
1233
1234 if (!assigned_any)
1235 netifd_log_message(L_WARNING, "You have delegated IPv6-prefixes but haven't assigned them "
1236 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
1237 }
1238
1239
1240 void interface_refresh_assignments(bool hint)
1241 {
1242 static bool refresh = false;
1243 if (!hint && refresh) {
1244 struct device_prefix *p;
1245 time_t now = system_get_rtime();
1246
1247 list_for_each_entry(p, &prefixes, head) {
1248 bool valid = !(p->valid_until && p->valid_until - 1 <= now);
1249
1250 interface_update_prefix_assignments(p, valid);
1251 }
1252 }
1253 refresh = hint;
1254 }
1255
1256 void interface_update_prefix_delegation(struct interface_ip_settings *ip)
1257 {
1258 struct device_prefix *prefix;
1259 time_t now = system_get_rtime();
1260
1261 vlist_for_each_element(&ip->prefix, prefix, node) {
1262 bool valid = !(prefix->valid_until && prefix->valid_until - 1 <= now);
1263
1264 interface_update_prefix_assignments(prefix, !ip->no_delegation && valid);
1265
1266 if (ip->no_delegation) {
1267 if (prefix->head.next)
1268 list_del(&prefix->head);
1269 } else
1270 list_add(&prefix->head, &prefixes);
1271 }
1272 }
1273
1274 static void
1275 interface_update_prefix(struct vlist_tree *tree,
1276 struct vlist_node *node_new,
1277 struct vlist_node *node_old)
1278 {
1279 struct device_prefix *prefix_old, *prefix_new;
1280 prefix_old = container_of(node_old, struct device_prefix, node);
1281 prefix_new = container_of(node_new, struct device_prefix, node);
1282
1283 struct interface_ip_settings *ip = container_of(tree, struct interface_ip_settings, prefix);
1284 if (tree && (!node_new || !node_old))
1285 ip->iface->updated |= IUF_PREFIX;
1286
1287 struct device_route route;
1288 memset(&route, 0, sizeof(route));
1289 route.flags = DEVADDR_INET6;
1290 route.metric = INT32_MAX;
1291 route.mask = (node_new) ? prefix_new->length : prefix_old->length;
1292 route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
1293
1294 struct device_prefix_assignment *c;
1295 struct interface *iface;
1296 bool new_valid = node_new && !(prefix_new->valid_until && prefix_new->valid_until - 1 <= system_get_rtime());
1297
1298 if (node_old && node_new) {
1299 /* Move assignments and refresh addresses to update valid times */
1300 list_splice(&prefix_old->assignments, &prefix_new->assignments);
1301
1302 list_for_each_entry(c, &prefix_new->assignments, head)
1303 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1304 interface_set_prefix_address(c, prefix_new, iface, new_valid);
1305
1306 if (prefix_new->preferred_until != prefix_old->preferred_until ||
1307 prefix_new->valid_until != prefix_old->valid_until)
1308 ip->iface->updated |= IUF_PREFIX;
1309 } else if (node_new) {
1310 /* Set null-route to avoid routing loops */
1311 system_add_route(NULL, &route);
1312
1313 if (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation)
1314 interface_update_prefix_assignments(prefix_new, new_valid);
1315 } else if (node_old) {
1316 /* Remove null-route */
1317 interface_update_prefix_assignments(prefix_old, false);
1318 system_del_route(NULL, &route);
1319 }
1320
1321 if (node_old) {
1322 if (prefix_old->head.next)
1323 list_del(&prefix_old->head);
1324 free(prefix_old);
1325 }
1326
1327 if (node_new && (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation))
1328 list_add(&prefix_new->head, &prefixes);
1329
1330 }
1331
1332 struct device_prefix*
1333 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
1334 uint8_t length, time_t valid_until, time_t preferred_until,
1335 struct in6_addr *excl_addr, uint8_t excl_length, const char *pclass)
1336 {
1337 union if_addr a = { .in6 = *addr };
1338
1339 if (!pclass)
1340 pclass = (iface) ? iface->name : "local";
1341
1342 struct device_prefix *prefix = calloc(1, sizeof(*prefix) + strlen(pclass) + 1);
1343 if (!prefix)
1344 return NULL;
1345
1346 clear_if_addr(&a, length);
1347
1348 prefix->length = length;
1349 prefix->addr = a.in6;
1350 prefix->preferred_until = preferred_until;
1351 prefix->valid_until = valid_until;
1352 prefix->iface = iface;
1353 INIT_LIST_HEAD(&prefix->assignments);
1354
1355 if (excl_addr) {
1356 prefix->excl_addr = *excl_addr;
1357 prefix->excl_length = excl_length;
1358 }
1359
1360 strcpy(prefix->pclass, pclass);
1361
1362 if (iface)
1363 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
1364 else
1365 interface_update_prefix(NULL, &prefix->node, NULL);
1366
1367 return prefix;
1368 }
1369
1370 void
1371 interface_ip_set_ula_prefix(const char *prefix)
1372 {
1373 char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
1374 if (prefix)
1375 strncpy(buf, prefix, sizeof(buf) - 1);
1376 char *prefixaddr = strtok_r(buf, "/", &saveptr);
1377
1378 struct in6_addr addr;
1379 if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
1380 if (ula_prefix) {
1381 interface_update_prefix(NULL, NULL, &ula_prefix->node);
1382 ula_prefix = NULL;
1383 }
1384 return;
1385 }
1386
1387 int length;
1388 char *prefixlen = strtok_r(NULL, ",", &saveptr);
1389 if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
1390 return;
1391
1392 if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
1393 ula_prefix->length != length) {
1394 if (ula_prefix)
1395 interface_update_prefix(NULL, NULL, &ula_prefix->node);
1396
1397 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
1398 0, 0, NULL, 0, NULL);
1399 }
1400 }
1401
1402 static void
1403 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
1404 {
1405 struct dns_server *s;
1406
1407 s = calloc(1, sizeof(*s));
1408 if (!s)
1409 return;
1410
1411 s->af = AF_INET;
1412 if (inet_pton(s->af, str, &s->addr.in))
1413 goto add;
1414
1415 s->af = AF_INET6;
1416 if (inet_pton(s->af, str, &s->addr.in))
1417 goto add;
1418
1419 free(s);
1420 return;
1421
1422 add:
1423 D(INTERFACE, "Add IPv%c DNS server: %s\n",
1424 s->af == AF_INET6 ? '6' : '4', str);
1425 vlist_simple_add(&ip->dns_servers, &s->node);
1426 }
1427
1428 void
1429 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
1430 {
1431 struct blob_attr *cur;
1432 size_t rem;
1433
1434 blobmsg_for_each_attr(cur, list, rem) {
1435 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1436 continue;
1437
1438 if (!blobmsg_check_attr(cur, false))
1439 continue;
1440
1441 interface_add_dns_server(ip, blobmsg_data(cur));
1442 }
1443 }
1444
1445 static void
1446 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
1447 {
1448 struct dns_search_domain *s;
1449 int len = strlen(str);
1450
1451 s = calloc(1, sizeof(*s) + len + 1);
1452 if (!s)
1453 return;
1454
1455 D(INTERFACE, "Add DNS search domain: %s\n", str);
1456 memcpy(s->name, str, len);
1457 vlist_simple_add(&ip->dns_search, &s->node);
1458 }
1459
1460 void
1461 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
1462 {
1463 struct blob_attr *cur;
1464 size_t rem;
1465
1466 blobmsg_for_each_attr(cur, list, rem) {
1467 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1468 continue;
1469
1470 if (!blobmsg_check_attr(cur, false))
1471 continue;
1472
1473 interface_add_dns_search_domain(ip, blobmsg_data(cur));
1474 }
1475 }
1476
1477 static void
1478 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip, const char *dev)
1479 {
1480 struct dns_server *s;
1481 struct dns_search_domain *d;
1482 const char *str;
1483 char buf[INET6_ADDRSTRLEN];
1484
1485 vlist_simple_for_each_element(&ip->dns_servers, s, node) {
1486 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
1487 if (!str)
1488 continue;
1489
1490 if (s->af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&s->addr.in6))
1491 fprintf(f, "nameserver %s%%%s\n", str, dev);
1492 else
1493 fprintf(f, "nameserver %s\n", str);
1494 }
1495
1496 vlist_simple_for_each_element(&ip->dns_search, d, node) {
1497 fprintf(f, "search %s\n", d->name);
1498 }
1499 }
1500
1501 /* Sorting of interface resolver entries : */
1502 /* Primary on interface dns_metric : lowest metric first */
1503 /* Secondary on interface metric : lowest metric first */
1504 /* Finally alphabetical order of interface names */
1505 static int resolv_conf_iface_cmp(const void *k1, const void *k2, void *ptr)
1506 {
1507 const struct interface *iface1 = k1, *iface2 = k2;
1508
1509 if (iface1->dns_metric != iface2->dns_metric)
1510 return iface1->dns_metric - iface2->dns_metric;
1511
1512 if (iface1->metric != iface2->metric)
1513 return iface1->metric - iface2->metric;
1514
1515 return strcmp(iface1->name, iface2->name);
1516 }
1517
1518 static void
1519 __interface_write_dns_entries(FILE *f, const char *jail)
1520 {
1521 struct interface *iface;
1522 struct {
1523 struct avl_node node;
1524 } *entry, *n_entry;
1525 struct avl_tree resolv_conf_iface_entries;
1526
1527 avl_init(&resolv_conf_iface_entries, resolv_conf_iface_cmp, false, NULL);
1528
1529 vlist_for_each_element(&interfaces, iface, node) {
1530 if (iface->state != IFS_UP)
1531 continue;
1532
1533 if (jail && (!iface->jail || strcmp(jail, iface->jail)))
1534 continue;
1535
1536 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
1537 vlist_simple_empty(&iface->proto_ip.dns_servers) &&
1538 vlist_simple_empty(&iface->config_ip.dns_search) &&
1539 vlist_simple_empty(&iface->config_ip.dns_servers))
1540 continue;
1541
1542 entry = calloc(1, sizeof(*entry));
1543 if (!entry)
1544 continue;
1545
1546 entry->node.key = iface;
1547 avl_insert(&resolv_conf_iface_entries, &entry->node);
1548 }
1549
1550 avl_for_each_element(&resolv_conf_iface_entries, entry, node) {
1551 iface = (struct interface *)entry->node.key;
1552 struct device *dev = iface->l3_dev.dev;
1553
1554 fprintf(f, "# Interface %s\n", iface->name);
1555
1556 write_resolv_conf_entries(f, &iface->config_ip, dev->ifname);
1557
1558 if (!iface->proto_ip.no_dns)
1559 write_resolv_conf_entries(f, &iface->proto_ip, dev->ifname);
1560 }
1561
1562 avl_remove_all_elements(&resolv_conf_iface_entries, entry, node, n_entry)
1563 free(entry);
1564 }
1565
1566 void
1567 interface_write_resolv_conf(const char *jail)
1568 {
1569 size_t plen = (jail ? strlen(jail) + 1 : 0 ) +
1570 (strlen(resolv_conf) >= strlen(DEFAULT_RESOLV_CONF) ?
1571 strlen(resolv_conf) : strlen(DEFAULT_RESOLV_CONF) ) + 1;
1572 char *path = alloca(plen);
1573 char *dpath = alloca(plen);
1574 char *tmppath = alloca(plen + 4);
1575 FILE *f;
1576 uint32_t crcold, crcnew;
1577
1578 if (jail) {
1579 sprintf(path, "/tmp/resolv.conf-%s.d/resolv.conf.auto", jail);
1580 strcpy(dpath, path);
1581 dpath = dirname(dpath);
1582 mkdir(dpath, 0755);
1583 } else {
1584 strcpy(path, resolv_conf);
1585 }
1586
1587 sprintf(tmppath, "%s.tmp", path);
1588 unlink(tmppath);
1589 f = fopen(tmppath, "w+");
1590 if (!f) {
1591 D(INTERFACE, "Failed to open %s for writing\n", path);
1592 return;
1593 }
1594
1595 __interface_write_dns_entries(f, jail);
1596
1597 fflush(f);
1598 rewind(f);
1599 crcnew = crc32_file(f);
1600 fclose(f);
1601
1602 crcold = crcnew + 1;
1603 f = fopen(path, "r");
1604 if (f) {
1605 crcold = crc32_file(f);
1606 fclose(f);
1607 }
1608
1609 if (crcold == crcnew) {
1610 unlink(tmppath);
1611 } else if (rename(tmppath, path) < 0) {
1612 D(INTERFACE, "Failed to replace %s\n", path);
1613 unlink(tmppath);
1614 }
1615 }
1616
1617 static void
1618 interface_ip_set_route_enabled(struct interface_ip_settings *ip,
1619 struct device_route *route, bool enabled)
1620 {
1621 struct device *dev = ip->iface->l3_dev.dev;
1622
1623 if (route->flags & DEVADDR_EXTERNAL)
1624 return;
1625
1626 if (!enable_route(ip, route))
1627 enabled = false;
1628
1629 if (route->enabled == enabled)
1630 return;
1631
1632 if (enabled) {
1633 interface_set_route_info(ip->iface, route);
1634
1635 if (system_add_route(dev, route))
1636 route->failed = true;
1637 } else
1638 system_del_route(dev, route);
1639
1640 route->enabled = enabled;
1641 }
1642
1643 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
1644 {
1645 struct device_addr *addr;
1646 struct device_route *route;
1647 struct device_neighbor *neighbor;
1648 struct device *dev;
1649 struct interface *iface;
1650
1651 ip->enabled = enabled;
1652 iface = ip->iface;
1653 dev = iface->l3_dev.dev;
1654 if (!dev)
1655 return;
1656
1657 vlist_for_each_element(&ip->addr, addr, node) {
1658 bool v6 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6) ? true : false;
1659
1660 if (addr->flags & DEVADDR_EXTERNAL)
1661 continue;
1662
1663 if (addr->enabled == enabled)
1664 continue;
1665
1666 if (enabled) {
1667 system_add_address(dev, addr);
1668
1669 addr->policy_table = (v6) ? iface->ip6table : iface->ip4table;
1670 if (iface->metric || addr->policy_table)
1671 interface_handle_subnet_route(iface, addr, true);
1672
1673 if ((addr->flags & DEVADDR_OFFLINK) && (addr->mask < (v6 ? 128 : 32))) {
1674 struct device_route route;
1675
1676 memset(&route, 0, sizeof(route));
1677 route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
1678 route.metric = INT32_MAX;
1679 route.mask = addr->mask;
1680 route.addr = addr->addr;
1681
1682 clear_if_addr(&route.addr, route.mask);
1683
1684 /*
1685 * In case off link is specifed as address property
1686 * add null-route to avoid routing loops
1687 */
1688 system_add_route(NULL, &route);
1689 }
1690
1691 if (addr->policy_table)
1692 interface_add_addr_rules(addr, true);
1693 } else {
1694 interface_handle_subnet_route(iface, addr, false);
1695 system_del_address(dev, addr);
1696
1697 if ((addr->flags & DEVADDR_OFFLINK) && (addr->mask < (v6 ? 128 : 32))) {
1698 struct device_route route;
1699
1700 memset(&route, 0, sizeof(route));
1701 route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
1702 route.metric = INT32_MAX;
1703 route.mask = addr->mask;
1704 route.addr = addr->addr;
1705
1706 clear_if_addr(&route.addr, route.mask);
1707
1708 /* Delete null-route */
1709 system_del_route(NULL, &route);
1710 }
1711
1712 if (addr->policy_table)
1713 interface_add_addr_rules(addr, false);
1714 }
1715 addr->enabled = enabled;
1716 }
1717
1718 vlist_for_each_element(&ip->route, route, node)
1719 interface_ip_set_route_enabled(ip, route, enabled);
1720 if (ip == &iface->proto_ip)
1721 vlist_for_each_element(&iface->host_routes, route, node)
1722 interface_ip_set_route_enabled(ip, route, enabled);
1723
1724 vlist_for_each_element(&ip->neighbor, neighbor, node) {
1725 if (neighbor->enabled == enabled)
1726 continue;
1727
1728 if (enabled) {
1729 if(system_add_neighbor(dev, neighbor))
1730 neighbor->failed = true;
1731 } else
1732 system_del_neighbor(dev, neighbor);
1733
1734 neighbor->enabled = enabled;
1735 }
1736
1737 struct device_prefix *c;
1738 struct device_prefix_assignment *a;
1739 list_for_each_entry(c, &prefixes, head)
1740 list_for_each_entry(a, &c->assignments, head)
1741 if (!strcmp(a->name, ip->iface->name))
1742 interface_set_prefix_address(a, c, ip->iface, enabled);
1743
1744 if (ip->iface->policy_rules_set != enabled &&
1745 ip->iface->l3_dev.dev) {
1746 if (ip->iface->l3_dev.dev->settings.ipv6) {
1747 set_ip_lo_policy(enabled, true, ip->iface);
1748 set_ip_source_policy(enabled, true, IPRULE_PRIORITY_REJECT + ip->iface->l3_dev.dev->ifindex,
1749 NULL, 0, 0, ip->iface, "failed_policy", true);
1750 }
1751 set_ip_lo_policy(enabled, false, ip->iface);
1752
1753 ip->iface->policy_rules_set = enabled;
1754 }
1755 }
1756
1757 void
1758 interface_ip_update_start(struct interface_ip_settings *ip)
1759 {
1760 if (ip != &ip->iface->config_ip) {
1761 vlist_simple_update(&ip->dns_servers);
1762 vlist_simple_update(&ip->dns_search);
1763 }
1764 vlist_update(&ip->route);
1765 vlist_update(&ip->addr);
1766 vlist_update(&ip->prefix);
1767 vlist_update(&ip->neighbor);
1768 }
1769
1770 void
1771 interface_ip_update_complete(struct interface_ip_settings *ip)
1772 {
1773 vlist_simple_flush(&ip->dns_servers);
1774 vlist_simple_flush(&ip->dns_search);
1775 vlist_flush(&ip->route);
1776 vlist_flush(&ip->addr);
1777 vlist_flush(&ip->prefix);
1778 vlist_flush(&ip->neighbor);
1779 interface_write_resolv_conf(ip->iface->jail);
1780 }
1781
1782 void
1783 interface_ip_flush(struct interface_ip_settings *ip)
1784 {
1785 if (ip == &ip->iface->proto_ip)
1786 vlist_flush_all(&ip->iface->host_routes);
1787 vlist_simple_flush_all(&ip->dns_servers);
1788 vlist_simple_flush_all(&ip->dns_search);
1789 vlist_flush_all(&ip->route);
1790 vlist_flush_all(&ip->addr);
1791 vlist_flush_all(&ip->neighbor);
1792 vlist_flush_all(&ip->prefix);
1793 }
1794
1795 static void
1796 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
1797 {
1798 ip->iface = iface;
1799 ip->enabled = true;
1800 vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
1801 vlist_simple_init(&ip->dns_servers, struct dns_server, node);
1802 vlist_init(&ip->route, route_cmp, interface_update_proto_route);
1803 vlist_init(&ip->neighbor, neighbor_cmp, interface_update_proto_neighbor);
1804 vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
1805 vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
1806 }
1807
1808 void
1809 interface_ip_init(struct interface *iface)
1810 {
1811 __interface_ip_init(&iface->proto_ip, iface);
1812 __interface_ip_init(&iface->config_ip, iface);
1813 vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
1814 }
1815
1816 static void
1817 interface_ip_valid_until_handler(struct uloop_timeout *t)
1818 {
1819 time_t now = system_get_rtime();
1820 struct interface *iface;
1821 vlist_for_each_element(&interfaces, iface, node) {
1822 if (iface->state != IFS_UP)
1823 continue;
1824
1825 struct device_addr *addr, *addrp;
1826 struct device_route *route, *routep;
1827 struct device_prefix *pref, *prefp;
1828
1829 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
1830 if (addr->valid_until && addr->valid_until < now)
1831 vlist_delete(&iface->proto_ip.addr, &addr->node);
1832
1833 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
1834 if (route->valid_until && route->valid_until < now)
1835 vlist_delete(&iface->proto_ip.route, &route->node);
1836
1837 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
1838 if (pref->valid_until && pref->valid_until < now)
1839 vlist_delete(&iface->proto_ip.prefix, &pref->node);
1840
1841 }
1842
1843 uloop_timeout_set(t, 1000);
1844 }
1845
1846 static void __init
1847 interface_ip_init_worker(void)
1848 {
1849 valid_until_timeout.cb = interface_ip_valid_until_handler;
1850 uloop_timeout_set(&valid_until_timeout, 1000);
1851 }