interface-ip: mask out host bits in IPv4 route targets
[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 /* Mask out IPv4 host bits to avoid "Invalid prefix for given prefix length" */
446 if (af == AF_INET && route->mask < 32)
447 route->addr.in.s_addr &= ((1u << route->mask) - 1);
448 }
449
450 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
451 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
452 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
453 goto error;
454 }
455 }
456
457 if ((cur = tb[ROUTE_METRIC]) != NULL) {
458 route->metric = blobmsg_get_u32(cur);
459 route->flags |= DEVROUTE_METRIC;
460 }
461
462 if ((cur = tb[ROUTE_MTU]) != NULL) {
463 route->mtu = blobmsg_get_u32(cur);
464 route->flags |= DEVROUTE_MTU;
465 }
466
467 /* Use source-based routing */
468 if ((cur = tb[ROUTE_SOURCE]) != NULL) {
469 char *saveptr, *source = alloca(blobmsg_data_len(cur));
470 memcpy(source, blobmsg_data(cur), blobmsg_data_len(cur));
471
472 const char *addr = strtok_r(source, "/", &saveptr);
473 const char *mask = strtok_r(NULL, "/", &saveptr);
474
475 if (!addr || inet_pton(af, addr, &route->source) < 1) {
476 DPRINTF("Failed to parse route source: %s\n", addr ? addr : "NULL");
477 goto error;
478 }
479
480 route->sourcemask = (mask) ? atoi(mask) : ((af == AF_INET6) ? 128 : 32);
481 }
482
483 if ((cur = tb[ROUTE_ONLINK]) != NULL && blobmsg_get_bool(cur))
484 route->flags |= DEVROUTE_ONLINK;
485
486 if ((cur = tb[ROUTE_TABLE]) != NULL) {
487 if (!system_resolve_rt_table(blobmsg_data(cur), &route->table)) {
488 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
489 goto error;
490 }
491
492 /* only set the table flag if not using the main (default) table */
493 if (system_is_default_rt_table(route->table))
494 route->table = 0;
495
496 if (route->table)
497 route->flags |= DEVROUTE_TABLE;
498 }
499
500 if ((cur = tb[ROUTE_VALID]) != NULL) {
501 int64_t valid = blobmsg_get_u32(cur);
502 int64_t valid_until = valid + (int64_t)system_get_rtime();
503 if (valid_until <= LONG_MAX && valid != 0xffffffffLL) /* Catch overflow */
504 route->valid_until = valid_until;
505 }
506
507 if ((cur = tb[ROUTE_TYPE]) != NULL) {
508 if (!system_resolve_rt_type(blobmsg_data(cur), &route->type)) {
509 DPRINTF("Failed to resolve routing type: %s\n", (char *) blobmsg_data(cur));
510 goto error;
511 }
512 route->flags |= DEVROUTE_TYPE;
513 }
514
515 if ((cur = tb[ROUTE_PROTO]) != NULL) {
516 if (!system_resolve_rt_proto(blobmsg_data(cur), &route->proto)) {
517 DPRINTF("Failed to resolve proto type: %s\n", (char *) blobmsg_data(cur));
518 goto error;
519 }
520 route->flags |= DEVROUTE_PROTO;
521 }
522
523 interface_set_route_info(iface, route);
524 vlist_add(&ip->route, &route->node, route);
525 return;
526
527 error:
528 free(route);
529 }
530
531 static int
532 addr_cmp(const void *k1, const void *k2, void *ptr)
533 {
534 const struct device_addr *a1 = k1;
535 const struct device_addr *a2 = k2;
536 const int cmp_offset = offsetof(struct device_addr, flags);
537 const int cmp_size = sizeof(struct device_addr) - cmp_offset;
538
539 if (a1->index != a2->index)
540 return a1->index - a2->index;
541 return memcmp(k1+cmp_offset, k2+cmp_offset, cmp_size);
542 }
543
544 static int
545 neighbor_cmp(const void *k1, const void *k2, void *ptr)
546 {
547 const struct device_neighbor *n1 = k1, *n2 = k2;
548
549 return memcmp(&n1->addr, &n2->addr, sizeof(n2->addr));
550 }
551
552 static int
553 route_cmp(const void *k1, const void *k2, void *ptr)
554 {
555 const struct device_route *r1 = k1, *r2 = k2;
556
557 if (r1->mask != r2->mask)
558 return r2->mask - r1->mask;
559
560 if (r1->metric != r2->metric)
561 return r1->metric - r2->metric;
562
563 if (r1->flags != r2->flags)
564 return r2->flags - r1->flags;
565
566 if (r1->sourcemask != r2->sourcemask)
567 return r1->sourcemask - r2->sourcemask;
568
569 if (r1->table != r2->table)
570 return r1->table - r2->table;
571
572 int maskcmp = memcmp(&r1->source, &r2->source, sizeof(r1->source));
573 if (maskcmp)
574 return maskcmp;
575
576 return memcmp(&r1->addr, &r2->addr, sizeof(r1->addr));
577 }
578
579 static int
580 prefix_cmp(const void *k1, const void *k2, void *ptr)
581 {
582 return memcmp(k1, k2, offsetof(struct device_prefix, pclass) -
583 offsetof(struct device_prefix, addr));
584 }
585
586 static void
587 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
588 {
589 struct device *dev = iface->l3_dev.dev;
590 struct device_route *r = &addr->subnet;
591
592 if (addr->flags & DEVADDR_OFFLINK)
593 return;
594
595 if (!add) {
596 if (!addr->subnet.iface)
597 return;
598
599 system_del_route(dev, r);
600 memset(r, 0, sizeof(*r));
601 return;
602 }
603
604 r->iface = iface;
605 r->flags = addr->flags;
606 r->mask = addr->mask;
607 memcpy(&r->addr, &addr->addr, sizeof(r->addr));
608 clear_if_addr(&r->addr, r->mask);
609
610 if (!system_resolve_rt_proto("kernel", &r->proto))
611 return;
612
613 r->flags |= DEVROUTE_PROTO;
614 system_del_route(dev, r);
615
616 r->flags &= ~DEVROUTE_PROTO;
617 interface_set_route_info(iface, r);
618
619 system_add_route(dev, r);
620 }
621
622 static void
623 interface_add_addr_rules(struct device_addr *addr, bool enabled)
624 {
625 bool v6 = (addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6;
626
627 set_ip_source_policy(enabled, v6, IPRULE_PRIORITY_ADDR, &addr->addr,
628 (v6) ? 128 : 32, addr->policy_table, NULL, NULL,
629 true);
630 set_ip_source_policy(enabled, v6, IPRULE_PRIORITY_ADDR_MASK,
631 &addr->addr, addr->mask, addr->policy_table, NULL,
632 NULL, false);
633 }
634
635 static void
636 interface_update_proto_addr(struct vlist_tree *tree,
637 struct vlist_node *node_new,
638 struct vlist_node *node_old)
639 {
640 struct interface_ip_settings *ip;
641 struct interface *iface;
642 struct device *dev;
643 struct device_addr *a_new = NULL, *a_old = NULL;
644 bool replace = false;
645 bool keep = false;
646 bool v6 = false;
647
648 ip = container_of(tree, struct interface_ip_settings, addr);
649 iface = ip->iface;
650 dev = iface->l3_dev.dev;
651
652 if (!node_new || !node_old)
653 iface->updated |= IUF_ADDRESS;
654
655 if (node_new) {
656 a_new = container_of(node_new, struct device_addr, node);
657
658 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
659 !a_new->broadcast) {
660
661 /* /31 and /32 addressing need 255.255.255.255
662 * as broadcast address. */
663 if (a_new->mask >= 31) {
664 a_new->broadcast = (uint32_t) ~0;
665 } else {
666 uint32_t mask = ~0;
667 uint32_t *a = (uint32_t *) &a_new->addr;
668
669 mask >>= a_new->mask;
670 a_new->broadcast = *a | htonl(mask);
671 }
672 }
673 }
674
675 if (node_old)
676 a_old = container_of(node_old, struct device_addr, node);
677
678 if (a_new && a_old) {
679 keep = true;
680
681 if (a_old->flags != a_new->flags || a_old->failed)
682 keep = false;
683
684 if (a_old->valid_until != a_new->valid_until ||
685 a_old->preferred_until != a_new->preferred_until)
686 replace = true;
687
688 if (((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4) &&
689 (a_new->broadcast != a_old->broadcast ||
690 a_new->point_to_point != a_old->point_to_point))
691 keep = false;
692 }
693
694 if (node_old) {
695 if (a_old->enabled && !keep) {
696 /*
697 * This is needed for source routing to work correctly. If a device
698 * has two connections to a network using the same subnet, adding
699 * only the network-rule will cause packets to be routed through the
700 * first matching network (source IP matches both masks)
701 */
702 if (a_old->policy_table)
703 interface_add_addr_rules(a_old, false);
704
705 if (!(a_old->flags & DEVADDR_EXTERNAL)) {
706 interface_handle_subnet_route(iface, a_old, false);
707 system_del_address(dev, a_old);
708
709 if ((a_old->flags & DEVADDR_OFFLINK) && (a_old->mask < (v6 ? 128 : 32))) {
710 struct device_route route;
711
712 memset(&route, 0, sizeof(route));
713 route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
714 route.metric = INT32_MAX;
715 route.mask = a_old->mask;
716 route.addr = a_old->addr;
717
718 clear_if_addr(&route.addr, route.mask);
719
720 /* Delete null-route */
721 system_del_route(NULL, &route);
722 }
723
724 }
725 }
726 free(a_old->pclass);
727 free(a_old);
728 }
729
730 if (node_new) {
731 a_new->enabled = true;
732
733 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET6)
734 v6 = true;
735
736 a_new->policy_table = (v6) ? iface->ip6table : iface->ip4table;
737
738 if (!keep || replace) {
739 if (!(a_new->flags & DEVADDR_EXTERNAL)) {
740 if (system_add_address(dev, a_new))
741 a_new->failed = true;
742
743 if (iface->metric || a_new->policy_table)
744 interface_handle_subnet_route(iface, a_new, true);
745 }
746
747 if (!keep) {
748 if (!(a_new->flags & DEVADDR_EXTERNAL) &&
749 (a_new->flags & DEVADDR_OFFLINK) &&
750 (a_new->mask < (v6 ? 128 : 32))) {
751 struct device_route route;
752
753 memset(&route, 0, sizeof(route));
754 route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
755 route.metric = INT32_MAX;
756 route.mask = a_new->mask;
757 route.addr = a_new->addr;
758
759 clear_if_addr(&route.addr, route.mask);
760
761 /*
762 * In case off link is specifed as address property
763 * add null-route to avoid routing loops
764 */
765 system_add_route(NULL, &route);
766 }
767
768 if (a_new->policy_table)
769 interface_add_addr_rules(a_new, true);
770 }
771 }
772 }
773 }
774
775 static bool
776 enable_route(struct interface_ip_settings *ip, struct device_route *route)
777 {
778 if (ip->no_defaultroute && !route->mask)
779 return false;
780
781 return ip->enabled;
782 }
783
784 static void
785 interface_update_proto_neighbor(struct vlist_tree *tree,
786 struct vlist_node * node_new,
787 struct vlist_node *node_old)
788 {
789 struct device *dev;
790 struct device_neighbor *neighbor_old, *neighbor_new;
791 struct interface_ip_settings *ip;
792 bool keep = false;
793
794 ip = container_of(tree, struct interface_ip_settings, neighbor);
795 dev = ip->iface->l3_dev.dev;
796
797 neighbor_old = container_of(node_old, struct device_neighbor, node);
798 neighbor_new = container_of(node_new, struct device_neighbor, node);
799
800 if (node_old && node_new) {
801 keep = (!memcmp(neighbor_old->macaddr, neighbor_new->macaddr, sizeof(neighbor_old->macaddr)) &&
802 (neighbor_old->proxy == neighbor_new->proxy) &&
803 (neighbor_old->router == neighbor_new->router));
804 }
805
806 if (node_old) {
807 if (!keep && neighbor_old->enabled)
808 system_del_neighbor(dev, neighbor_old);
809
810 free(neighbor_old);
811 }
812
813 if (node_new) {
814 if (!keep && ip->enabled)
815 if (system_add_neighbor(dev, neighbor_new))
816 neighbor_new->failed = true;
817
818 neighbor_new->enabled = ip->enabled;
819 }
820 }
821
822 static void
823 __interface_update_route(struct interface_ip_settings *ip,
824 struct vlist_node *node_new,
825 struct vlist_node *node_old)
826 {
827 struct interface *iface = ip->iface;
828 struct device *dev;
829 struct device_route *route_old, *route_new;
830 bool keep = false;
831
832 dev = iface->l3_dev.dev;
833
834 if (!node_new || !node_old)
835 iface->updated |= IUF_ROUTE;
836
837 route_old = container_of(node_old, struct device_route, node);
838 route_new = container_of(node_new, struct device_route, node);
839
840 if (node_old && node_new)
841 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop)) &&
842 (route_old->mtu == route_new->mtu) && (route_old->type == route_new->type) &&
843 (route_old->proto == route_new->proto) && !route_old->failed;
844
845 if (node_old) {
846 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
847 system_del_route(dev, route_old);
848
849 free(route_old);
850 }
851
852 if (node_new) {
853 bool _enabled = enable_route(ip, route_new);
854
855 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
856 if (system_add_route(dev, route_new))
857 route_new->failed = true;
858
859 route_new->iface = iface;
860 route_new->enabled = _enabled;
861 }
862 }
863
864 static void
865 interface_update_proto_route(struct vlist_tree *tree,
866 struct vlist_node *node_new,
867 struct vlist_node *node_old)
868 {
869 struct interface_ip_settings *ip;
870
871 ip = container_of(tree, struct interface_ip_settings, route);
872 __interface_update_route(ip, node_new, node_old);
873 }
874
875 static void
876 interface_update_host_route(struct vlist_tree *tree,
877 struct vlist_node *node_new,
878 struct vlist_node *node_old)
879 {
880 struct interface *iface;
881
882 iface = container_of(tree, struct interface, host_routes);
883 __interface_update_route(&iface->proto_ip, node_new, node_old);
884 }
885
886 static void
887 random_ifaceid(struct in6_addr *addr)
888 {
889 static bool initialized = false;
890 struct timeval t;
891
892 if (!initialized) {
893 long int seed = 0;
894 gettimeofday(&t, NULL);
895 seed = t.tv_sec ^ t.tv_usec ^ getpid();
896 srand48(seed);
897 initialized = true;
898 }
899 addr->s6_addr32[2] = (uint32_t)mrand48();
900 addr->s6_addr32[3] = (uint32_t)mrand48();
901 }
902
903 static bool
904 eui64_ifaceid(struct interface *iface, struct in6_addr *addr)
905 {
906 struct device_settings st;
907
908 device_merge_settings(iface->l3_dev.dev, &st);
909
910 if (!(st.flags & DEV_OPT_MACADDR))
911 return false;
912
913 /* get mac address */
914 uint8_t *ifaceid = addr->s6_addr + 8;
915 memcpy(ifaceid, st.macaddr, 3);
916 memcpy(ifaceid + 5, st.macaddr + 3, 3);
917 ifaceid[3] = 0xff;
918 ifaceid[4] = 0xfe;
919 ifaceid[0] ^= 0x02;
920
921 return true;
922 }
923
924 static bool
925 generate_ifaceid(struct interface *iface, struct in6_addr *addr)
926 {
927 bool ret = true;
928
929 /* generate new iface id */
930 switch (iface->assignment_iface_id_selection) {
931 case IFID_FIXED:
932 /* fixed */
933 /* copy host part from assignment_fixed_iface_id */
934 memcpy(addr->s6_addr + 8, iface->assignment_fixed_iface_id.s6_addr + 8, 8);
935 break;
936 case IFID_RANDOM:
937 /* randomize last 64 bits */
938 random_ifaceid(addr);
939 break;
940 case IFID_EUI64:
941 /* eui64 */
942 ret = eui64_ifaceid(iface, addr);
943 break;
944 default:
945 ret = false;
946 break;
947 }
948 return ret;
949 }
950
951 static void
952 interface_set_prefix_address(struct device_prefix_assignment *assignment,
953 const struct device_prefix *prefix, struct interface *iface, bool add)
954 {
955 const struct interface *uplink = prefix->iface;
956 if (!iface->l3_dev.dev)
957 return;
958
959 struct device *l3_downlink = iface->l3_dev.dev;
960
961 struct device_addr addr;
962 struct device_route route;
963 memset(&addr, 0, sizeof(addr));
964 memset(&route, 0, sizeof(route));
965
966 addr.addr.in6 = assignment->addr;
967 addr.mask = assignment->length;
968 addr.flags = DEVADDR_INET6;
969 addr.preferred_until = prefix->preferred_until;
970 addr.valid_until = prefix->valid_until;
971
972 route.flags = DEVADDR_INET6;
973 route.mask = addr.mask < 64 ? 64 : addr.mask;
974 route.addr = addr.addr;
975
976 if (!add && assignment->enabled) {
977 time_t now = system_get_rtime();
978
979 if (addr.valid_until && addr.valid_until - 1 <= now) {
980 addr.valid_until = 0;
981 addr.preferred_until = 0;
982 } else {
983 /* Address is still valid; pass its ownership to kernel (see L-14 RFC 7084). */
984 addr.preferred_until = now;
985
986 if (!addr.valid_until || addr.valid_until > now + 7200)
987 addr.valid_until = now + 7200;
988 }
989
990 if (iface->ip6table)
991 set_ip_source_policy(false, true, IPRULE_PRIORITY_ADDR_MASK, &addr.addr,
992 addr.mask < 64 ? 64 : addr.mask, iface->ip6table, NULL, NULL, false);
993
994 if (prefix->iface) {
995 if (prefix->iface->ip6table)
996 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW, &addr.addr,
997 addr.mask, prefix->iface->ip6table, iface, NULL, true);
998
999 set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT, &addr.addr,
1000 addr.mask, 0, iface, "unreachable", true);
1001 }
1002
1003 clear_if_addr(&route.addr, route.mask);
1004 interface_set_route_info(iface, &route);
1005
1006 system_del_route(l3_downlink, &route);
1007 if (addr.valid_until)
1008 system_add_address(l3_downlink, &addr);
1009 else
1010 system_del_address(l3_downlink, &addr);
1011
1012 assignment->addr = in6addr_any;
1013 assignment->enabled = false;
1014 } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP)) {
1015 if (IN6_IS_ADDR_UNSPECIFIED(&addr.addr.in6)) {
1016 addr.addr.in6 = prefix->addr;
1017 addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
1018 if (!generate_ifaceid(iface, &addr.addr.in6))
1019 return;
1020
1021 assignment->addr = addr.addr.in6;
1022 route.addr = addr.addr;
1023 }
1024
1025 addr.flags |= DEVADDR_OFFLINK;
1026 if (system_add_address(l3_downlink, &addr))
1027 return;
1028
1029 if (!assignment->enabled) {
1030 if (iface->ip6table)
1031 set_ip_source_policy(true, true, IPRULE_PRIORITY_ADDR_MASK, &addr.addr,
1032 addr.mask < 64 ? 64 : addr.mask, iface->ip6table, NULL, NULL, false);
1033
1034 if (prefix->iface) {
1035 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT, &addr.addr,
1036 addr.mask, 0, iface, "unreachable", true);
1037
1038 if (prefix->iface->ip6table)
1039 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW, &addr.addr,
1040 addr.mask, prefix->iface->ip6table, iface, NULL, true);
1041 }
1042 }
1043
1044 clear_if_addr(&route.addr, route.mask);
1045 interface_set_route_info(iface, &route);
1046
1047 system_add_route(l3_downlink, &route);
1048
1049 if (uplink && uplink->l3_dev.dev && !(l3_downlink->settings.flags & DEV_OPT_MTU6)) {
1050 int mtu = system_update_ipv6_mtu(uplink->l3_dev.dev, 0);
1051 int mtu_old = system_update_ipv6_mtu(l3_downlink, 0);
1052
1053 if (mtu > 0 && mtu_old != mtu) {
1054 if (system_update_ipv6_mtu(l3_downlink, mtu) < 0 && mtu < mtu_old)
1055 netifd_log_message(L_WARNING, "Failed to set IPv6 mtu to %d "
1056 "on interface '%s'\n", mtu, iface->name);
1057 }
1058 }
1059
1060 assignment->enabled = true;
1061 }
1062 }
1063
1064 static bool interface_prefix_assign(struct list_head *list,
1065 struct device_prefix_assignment *assign)
1066 {
1067 int32_t current = 0, asize = (1 << (64 - assign->length)) - 1;
1068 struct device_prefix_assignment *c;
1069 list_for_each_entry(c, list, head) {
1070 if (assign->assigned != -1) {
1071 if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
1072 list_add_tail(&assign->head, &c->head);
1073 return true;
1074 }
1075 } else if (assign->assigned == -1) {
1076 current = (current + asize) & (~asize);
1077 if (current + asize < c->assigned) {
1078 assign->assigned = current;
1079 list_add_tail(&assign->head, &c->head);
1080 return true;
1081 }
1082 }
1083 current = (c->assigned + (1 << (64 - c->length)));
1084 }
1085 return false;
1086 }
1087
1088 /*
1089 * Sorting of assignment entries:
1090 * Primary on assignment length: smallest assignment first
1091 * Secondary on assignment weight: highest weight first
1092 * Finally alphabetical order of interface names
1093 */
1094 static int prefix_assignment_cmp(const void *k1, const void *k2, void *ptr)
1095 {
1096 const struct device_prefix_assignment *a1 = k1, *a2 = k2;
1097
1098 if (a1->length != a2->length)
1099 return a1->length - a2->length;
1100
1101 if (a1->weight != a2->weight)
1102 return a2->weight - a1->weight;
1103
1104 return strcmp(a1->name, a2->name);
1105 }
1106
1107 static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup)
1108 {
1109 struct device_prefix_assignment *c;
1110 struct interface *iface;
1111
1112 /* Delete all assignments */
1113 while (!list_empty(&prefix->assignments)) {
1114 c = list_first_entry(&prefix->assignments,
1115 struct device_prefix_assignment, head);
1116 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1117 interface_set_prefix_address(c, prefix, iface, false);
1118 list_del(&c->head);
1119 free(c);
1120 }
1121
1122 if (!setup)
1123 return;
1124
1125 /* End-of-assignment sentinel */
1126 c = malloc(sizeof(*c) + 1);
1127 if (!c)
1128 return;
1129
1130 c->assigned = 1 << (64 - prefix->length);
1131 c->length = 64;
1132 c->name[0] = 0;
1133 c->addr = in6addr_any;
1134 list_add(&c->head, &prefix->assignments);
1135
1136 /* Excluded prefix */
1137 if (prefix->excl_length > 0) {
1138 const char name[] = "!excluded";
1139 c = malloc(sizeof(*c) + sizeof(name));
1140 if (c) {
1141 c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
1142 ((1 << (64 - prefix->length)) - 1);
1143 c->length = prefix->excl_length;
1144 c->addr = in6addr_any;
1145 memcpy(c->name, name, sizeof(name));
1146 list_add(&c->head, &prefix->assignments);
1147 }
1148 }
1149
1150 bool assigned_any = false;
1151 struct {
1152 struct avl_node node;
1153 } *entry, *n_entry;
1154 struct avl_tree assign_later;
1155
1156 avl_init(&assign_later, prefix_assignment_cmp, false, NULL);
1157
1158 vlist_for_each_element(&interfaces, iface, node) {
1159 if (iface->assignment_length < 48 ||
1160 iface->assignment_length > 64)
1161 continue;
1162
1163 /* Test whether there is a matching class */
1164 if (!list_empty(&iface->assignment_classes)) {
1165 bool found = false;
1166
1167 struct interface_assignment_class *c;
1168 list_for_each_entry(c, &iface->assignment_classes, head) {
1169 if (!strcmp(c->name, prefix->pclass)) {
1170 found = true;
1171 break;
1172 }
1173 }
1174
1175 if (!found)
1176 continue;
1177 }
1178
1179 size_t namelen = strlen(iface->name) + 1;
1180 c = malloc(sizeof(*c) + namelen);
1181 if (!c)
1182 continue;
1183
1184 c->length = iface->assignment_length;
1185 c->assigned = iface->assignment_hint;
1186 c->weight = iface->assignment_weight;
1187 c->addr = in6addr_any;
1188 c->enabled = false;
1189 memcpy(c->name, iface->name, namelen);
1190
1191 /* First process all custom assignments, put all others in later-list */
1192 if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
1193 if (c->assigned != -1) {
1194 c->assigned = -1;
1195 netifd_log_message(L_WARNING, "Failed to assign requested subprefix "
1196 "of size %hhu for %s, trying other\n", c->length, c->name);
1197 }
1198
1199 entry = calloc(1, sizeof(*entry));
1200 if (!entry) {
1201 free(c);
1202 continue;
1203 }
1204
1205 entry->node.key = c;
1206 avl_insert(&assign_later, &entry->node);
1207 }
1208
1209 if (c->assigned != -1)
1210 assigned_any = true;
1211 }
1212
1213 /* Then try to assign all other + failed custom assignments */
1214 avl_for_each_element_safe(&assign_later, entry, node, n_entry) {
1215 bool assigned = false;
1216
1217 c = (struct device_prefix_assignment *)entry->node.key;
1218 avl_delete(&assign_later, &entry->node);
1219
1220 do {
1221 assigned = interface_prefix_assign(&prefix->assignments, c);
1222 } while (!assigned && ++c->length <= 64);
1223
1224 if (!assigned) {
1225 netifd_log_message(L_WARNING, "Failed to assign subprefix "
1226 "of size %hhu for %s\n", c->length, c->name);
1227 free(c);
1228 } else
1229 assigned_any = true;
1230
1231 free(entry);
1232 }
1233
1234 list_for_each_entry(c, &prefix->assignments, head)
1235 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1236 interface_set_prefix_address(c, prefix, iface, true);
1237
1238 if (!assigned_any)
1239 netifd_log_message(L_WARNING, "You have delegated IPv6-prefixes but haven't assigned them "
1240 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
1241 }
1242
1243
1244 void interface_refresh_assignments(bool hint)
1245 {
1246 static bool refresh = false;
1247 if (!hint && refresh) {
1248 struct device_prefix *p;
1249 time_t now = system_get_rtime();
1250
1251 list_for_each_entry(p, &prefixes, head) {
1252 bool valid = !(p->valid_until && p->valid_until - 1 <= now);
1253
1254 interface_update_prefix_assignments(p, valid);
1255 }
1256 }
1257 refresh = hint;
1258 }
1259
1260 void interface_update_prefix_delegation(struct interface_ip_settings *ip)
1261 {
1262 struct device_prefix *prefix;
1263 time_t now = system_get_rtime();
1264
1265 vlist_for_each_element(&ip->prefix, prefix, node) {
1266 bool valid = !(prefix->valid_until && prefix->valid_until - 1 <= now);
1267
1268 interface_update_prefix_assignments(prefix, !ip->no_delegation && valid);
1269
1270 if (ip->no_delegation) {
1271 if (prefix->head.next)
1272 list_del(&prefix->head);
1273 } else
1274 list_add(&prefix->head, &prefixes);
1275 }
1276 }
1277
1278 static void
1279 interface_update_prefix(struct vlist_tree *tree,
1280 struct vlist_node *node_new,
1281 struct vlist_node *node_old)
1282 {
1283 struct device_prefix *prefix_old, *prefix_new;
1284 prefix_old = container_of(node_old, struct device_prefix, node);
1285 prefix_new = container_of(node_new, struct device_prefix, node);
1286
1287 struct interface_ip_settings *ip = container_of(tree, struct interface_ip_settings, prefix);
1288 if (tree && (!node_new || !node_old))
1289 ip->iface->updated |= IUF_PREFIX;
1290
1291 struct device_route route;
1292 memset(&route, 0, sizeof(route));
1293 route.flags = DEVADDR_INET6;
1294 route.metric = INT32_MAX;
1295 route.mask = (node_new) ? prefix_new->length : prefix_old->length;
1296 route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
1297
1298 struct device_prefix_assignment *c;
1299 struct interface *iface;
1300 bool new_valid = node_new && !(prefix_new->valid_until && prefix_new->valid_until - 1 <= system_get_rtime());
1301
1302 if (node_old && node_new) {
1303 /* Move assignments and refresh addresses to update valid times */
1304 list_splice(&prefix_old->assignments, &prefix_new->assignments);
1305
1306 list_for_each_entry(c, &prefix_new->assignments, head)
1307 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
1308 interface_set_prefix_address(c, prefix_new, iface, new_valid);
1309
1310 if (prefix_new->preferred_until != prefix_old->preferred_until ||
1311 prefix_new->valid_until != prefix_old->valid_until)
1312 ip->iface->updated |= IUF_PREFIX;
1313 } else if (node_new) {
1314 /* Set null-route to avoid routing loops */
1315 system_add_route(NULL, &route);
1316
1317 if (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation)
1318 interface_update_prefix_assignments(prefix_new, new_valid);
1319 } else if (node_old) {
1320 /* Remove null-route */
1321 interface_update_prefix_assignments(prefix_old, false);
1322 system_del_route(NULL, &route);
1323 }
1324
1325 if (node_old) {
1326 if (prefix_old->head.next)
1327 list_del(&prefix_old->head);
1328 free(prefix_old);
1329 }
1330
1331 if (node_new && (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation))
1332 list_add(&prefix_new->head, &prefixes);
1333
1334 }
1335
1336 struct device_prefix*
1337 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
1338 uint8_t length, time_t valid_until, time_t preferred_until,
1339 struct in6_addr *excl_addr, uint8_t excl_length, const char *pclass)
1340 {
1341 union if_addr a = { .in6 = *addr };
1342
1343 if (!pclass)
1344 pclass = (iface) ? iface->name : "local";
1345
1346 struct device_prefix *prefix = calloc(1, sizeof(*prefix) + strlen(pclass) + 1);
1347 if (!prefix)
1348 return NULL;
1349
1350 clear_if_addr(&a, length);
1351
1352 prefix->length = length;
1353 prefix->addr = a.in6;
1354 prefix->preferred_until = preferred_until;
1355 prefix->valid_until = valid_until;
1356 prefix->iface = iface;
1357 INIT_LIST_HEAD(&prefix->assignments);
1358
1359 if (excl_addr) {
1360 prefix->excl_addr = *excl_addr;
1361 prefix->excl_length = excl_length;
1362 }
1363
1364 strcpy(prefix->pclass, pclass);
1365
1366 if (iface)
1367 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
1368 else
1369 interface_update_prefix(NULL, &prefix->node, NULL);
1370
1371 return prefix;
1372 }
1373
1374 void
1375 interface_ip_set_ula_prefix(const char *prefix)
1376 {
1377 char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
1378 if (prefix)
1379 strncpy(buf, prefix, sizeof(buf) - 1);
1380 char *prefixaddr = strtok_r(buf, "/", &saveptr);
1381
1382 struct in6_addr addr;
1383 if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
1384 if (ula_prefix) {
1385 interface_update_prefix(NULL, NULL, &ula_prefix->node);
1386 ula_prefix = NULL;
1387 }
1388 return;
1389 }
1390
1391 int length;
1392 char *prefixlen = strtok_r(NULL, ",", &saveptr);
1393 if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
1394 return;
1395
1396 if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
1397 ula_prefix->length != length) {
1398 if (ula_prefix)
1399 interface_update_prefix(NULL, NULL, &ula_prefix->node);
1400
1401 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
1402 0, 0, NULL, 0, NULL);
1403 }
1404 }
1405
1406 static void
1407 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
1408 {
1409 struct dns_server *s;
1410
1411 s = calloc(1, sizeof(*s));
1412 if (!s)
1413 return;
1414
1415 s->af = AF_INET;
1416 if (inet_pton(s->af, str, &s->addr.in))
1417 goto add;
1418
1419 s->af = AF_INET6;
1420 if (inet_pton(s->af, str, &s->addr.in))
1421 goto add;
1422
1423 free(s);
1424 return;
1425
1426 add:
1427 D(INTERFACE, "Add IPv%c DNS server: %s\n",
1428 s->af == AF_INET6 ? '6' : '4', str);
1429 vlist_simple_add(&ip->dns_servers, &s->node);
1430 }
1431
1432 void
1433 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
1434 {
1435 struct blob_attr *cur;
1436 size_t rem;
1437
1438 blobmsg_for_each_attr(cur, list, rem) {
1439 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1440 continue;
1441
1442 if (!blobmsg_check_attr(cur, false))
1443 continue;
1444
1445 interface_add_dns_server(ip, blobmsg_data(cur));
1446 }
1447 }
1448
1449 static void
1450 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
1451 {
1452 struct dns_search_domain *s;
1453 int len = strlen(str);
1454
1455 s = calloc(1, sizeof(*s) + len + 1);
1456 if (!s)
1457 return;
1458
1459 D(INTERFACE, "Add DNS search domain: %s\n", str);
1460 memcpy(s->name, str, len);
1461 vlist_simple_add(&ip->dns_search, &s->node);
1462 }
1463
1464 void
1465 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
1466 {
1467 struct blob_attr *cur;
1468 size_t rem;
1469
1470 blobmsg_for_each_attr(cur, list, rem) {
1471 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1472 continue;
1473
1474 if (!blobmsg_check_attr(cur, false))
1475 continue;
1476
1477 interface_add_dns_search_domain(ip, blobmsg_data(cur));
1478 }
1479 }
1480
1481 static void
1482 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip, const char *dev)
1483 {
1484 struct dns_server *s;
1485 struct dns_search_domain *d;
1486 const char *str;
1487 char buf[INET6_ADDRSTRLEN];
1488
1489 vlist_simple_for_each_element(&ip->dns_servers, s, node) {
1490 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
1491 if (!str)
1492 continue;
1493
1494 if (s->af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&s->addr.in6))
1495 fprintf(f, "nameserver %s%%%s\n", str, dev);
1496 else
1497 fprintf(f, "nameserver %s\n", str);
1498 }
1499
1500 vlist_simple_for_each_element(&ip->dns_search, d, node) {
1501 fprintf(f, "search %s\n", d->name);
1502 }
1503 }
1504
1505 /* Sorting of interface resolver entries : */
1506 /* Primary on interface dns_metric : lowest metric first */
1507 /* Secondary on interface metric : lowest metric first */
1508 /* Finally alphabetical order of interface names */
1509 static int resolv_conf_iface_cmp(const void *k1, const void *k2, void *ptr)
1510 {
1511 const struct interface *iface1 = k1, *iface2 = k2;
1512
1513 if (iface1->dns_metric != iface2->dns_metric)
1514 return iface1->dns_metric - iface2->dns_metric;
1515
1516 if (iface1->metric != iface2->metric)
1517 return iface1->metric - iface2->metric;
1518
1519 return strcmp(iface1->name, iface2->name);
1520 }
1521
1522 static void
1523 __interface_write_dns_entries(FILE *f, const char *jail)
1524 {
1525 struct interface *iface;
1526 struct {
1527 struct avl_node node;
1528 } *entry, *n_entry;
1529 struct avl_tree resolv_conf_iface_entries;
1530
1531 avl_init(&resolv_conf_iface_entries, resolv_conf_iface_cmp, false, NULL);
1532
1533 vlist_for_each_element(&interfaces, iface, node) {
1534 if (iface->state != IFS_UP)
1535 continue;
1536
1537 if (jail && (!iface->jail || strcmp(jail, iface->jail)))
1538 continue;
1539
1540 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
1541 vlist_simple_empty(&iface->proto_ip.dns_servers) &&
1542 vlist_simple_empty(&iface->config_ip.dns_search) &&
1543 vlist_simple_empty(&iface->config_ip.dns_servers))
1544 continue;
1545
1546 entry = calloc(1, sizeof(*entry));
1547 if (!entry)
1548 continue;
1549
1550 entry->node.key = iface;
1551 avl_insert(&resolv_conf_iface_entries, &entry->node);
1552 }
1553
1554 avl_for_each_element(&resolv_conf_iface_entries, entry, node) {
1555 iface = (struct interface *)entry->node.key;
1556 struct device *dev = iface->l3_dev.dev;
1557
1558 fprintf(f, "# Interface %s\n", iface->name);
1559
1560 write_resolv_conf_entries(f, &iface->config_ip, dev->ifname);
1561
1562 if (!iface->proto_ip.no_dns)
1563 write_resolv_conf_entries(f, &iface->proto_ip, dev->ifname);
1564 }
1565
1566 avl_remove_all_elements(&resolv_conf_iface_entries, entry, node, n_entry)
1567 free(entry);
1568 }
1569
1570 void
1571 interface_write_resolv_conf(const char *jail)
1572 {
1573 size_t plen = (jail ? strlen(jail) + 1 : 0 ) +
1574 (strlen(resolv_conf) >= strlen(DEFAULT_RESOLV_CONF) ?
1575 strlen(resolv_conf) : strlen(DEFAULT_RESOLV_CONF) ) + 1;
1576 char *path = alloca(plen);
1577 char *dpath = alloca(plen);
1578 char *tmppath = alloca(plen + 4);
1579 FILE *f;
1580 uint32_t crcold, crcnew;
1581
1582 if (jail) {
1583 sprintf(path, "/tmp/resolv.conf-%s.d/resolv.conf.auto", jail);
1584 strcpy(dpath, path);
1585 dpath = dirname(dpath);
1586 mkdir(dpath, 0755);
1587 } else {
1588 strcpy(path, resolv_conf);
1589 }
1590
1591 sprintf(tmppath, "%s.tmp", path);
1592 unlink(tmppath);
1593 f = fopen(tmppath, "w+");
1594 if (!f) {
1595 D(INTERFACE, "Failed to open %s for writing\n", path);
1596 return;
1597 }
1598
1599 __interface_write_dns_entries(f, jail);
1600
1601 fflush(f);
1602 rewind(f);
1603 crcnew = crc32_file(f);
1604 fclose(f);
1605
1606 crcold = crcnew + 1;
1607 f = fopen(path, "r");
1608 if (f) {
1609 crcold = crc32_file(f);
1610 fclose(f);
1611 }
1612
1613 if (crcold == crcnew) {
1614 unlink(tmppath);
1615 } else if (rename(tmppath, path) < 0) {
1616 D(INTERFACE, "Failed to replace %s\n", path);
1617 unlink(tmppath);
1618 }
1619 }
1620
1621 static void
1622 interface_ip_set_route_enabled(struct interface_ip_settings *ip,
1623 struct device_route *route, bool enabled)
1624 {
1625 struct device *dev = ip->iface->l3_dev.dev;
1626
1627 if (route->flags & DEVADDR_EXTERNAL)
1628 return;
1629
1630 if (!enable_route(ip, route))
1631 enabled = false;
1632
1633 if (route->enabled == enabled)
1634 return;
1635
1636 if (enabled) {
1637 interface_set_route_info(ip->iface, route);
1638
1639 if (system_add_route(dev, route))
1640 route->failed = true;
1641 } else
1642 system_del_route(dev, route);
1643
1644 route->enabled = enabled;
1645 }
1646
1647 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
1648 {
1649 struct device_addr *addr;
1650 struct device_route *route;
1651 struct device_neighbor *neighbor;
1652 struct device *dev;
1653 struct interface *iface;
1654
1655 ip->enabled = enabled;
1656 iface = ip->iface;
1657 dev = iface->l3_dev.dev;
1658 if (!dev)
1659 return;
1660
1661 vlist_for_each_element(&ip->addr, addr, node) {
1662 bool v6 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6) ? true : false;
1663
1664 if (addr->flags & DEVADDR_EXTERNAL)
1665 continue;
1666
1667 if (addr->enabled == enabled)
1668 continue;
1669
1670 if (enabled) {
1671 system_add_address(dev, addr);
1672
1673 addr->policy_table = (v6) ? iface->ip6table : iface->ip4table;
1674 if (iface->metric || addr->policy_table)
1675 interface_handle_subnet_route(iface, addr, true);
1676
1677 if ((addr->flags & DEVADDR_OFFLINK) && (addr->mask < (v6 ? 128 : 32))) {
1678 struct device_route route;
1679
1680 memset(&route, 0, sizeof(route));
1681 route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
1682 route.metric = INT32_MAX;
1683 route.mask = addr->mask;
1684 route.addr = addr->addr;
1685
1686 clear_if_addr(&route.addr, route.mask);
1687
1688 /*
1689 * In case off link is specifed as address property
1690 * add null-route to avoid routing loops
1691 */
1692 system_add_route(NULL, &route);
1693 }
1694
1695 if (addr->policy_table)
1696 interface_add_addr_rules(addr, true);
1697 } else {
1698 interface_handle_subnet_route(iface, addr, false);
1699 system_del_address(dev, addr);
1700
1701 if ((addr->flags & DEVADDR_OFFLINK) && (addr->mask < (v6 ? 128 : 32))) {
1702 struct device_route route;
1703
1704 memset(&route, 0, sizeof(route));
1705 route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
1706 route.metric = INT32_MAX;
1707 route.mask = addr->mask;
1708 route.addr = addr->addr;
1709
1710 clear_if_addr(&route.addr, route.mask);
1711
1712 /* Delete null-route */
1713 system_del_route(NULL, &route);
1714 }
1715
1716 if (addr->policy_table)
1717 interface_add_addr_rules(addr, false);
1718 }
1719 addr->enabled = enabled;
1720 }
1721
1722 vlist_for_each_element(&ip->route, route, node)
1723 interface_ip_set_route_enabled(ip, route, enabled);
1724 if (ip == &iface->proto_ip)
1725 vlist_for_each_element(&iface->host_routes, route, node)
1726 interface_ip_set_route_enabled(ip, route, enabled);
1727
1728 vlist_for_each_element(&ip->neighbor, neighbor, node) {
1729 if (neighbor->enabled == enabled)
1730 continue;
1731
1732 if (enabled) {
1733 if(system_add_neighbor(dev, neighbor))
1734 neighbor->failed = true;
1735 } else
1736 system_del_neighbor(dev, neighbor);
1737
1738 neighbor->enabled = enabled;
1739 }
1740
1741 struct device_prefix *c;
1742 struct device_prefix_assignment *a;
1743 list_for_each_entry(c, &prefixes, head)
1744 list_for_each_entry(a, &c->assignments, head)
1745 if (!strcmp(a->name, ip->iface->name))
1746 interface_set_prefix_address(a, c, ip->iface, enabled);
1747
1748 if (ip->iface->policy_rules_set != enabled &&
1749 ip->iface->l3_dev.dev) {
1750 if (ip->iface->l3_dev.dev->settings.ipv6) {
1751 set_ip_lo_policy(enabled, true, ip->iface);
1752 set_ip_source_policy(enabled, true, IPRULE_PRIORITY_REJECT + ip->iface->l3_dev.dev->ifindex,
1753 NULL, 0, 0, ip->iface, "failed_policy", true);
1754 }
1755 set_ip_lo_policy(enabled, false, ip->iface);
1756
1757 ip->iface->policy_rules_set = enabled;
1758 }
1759 }
1760
1761 void
1762 interface_ip_update_start(struct interface_ip_settings *ip)
1763 {
1764 if (ip != &ip->iface->config_ip) {
1765 vlist_simple_update(&ip->dns_servers);
1766 vlist_simple_update(&ip->dns_search);
1767 }
1768 vlist_update(&ip->route);
1769 vlist_update(&ip->addr);
1770 vlist_update(&ip->prefix);
1771 vlist_update(&ip->neighbor);
1772 }
1773
1774 void
1775 interface_ip_update_complete(struct interface_ip_settings *ip)
1776 {
1777 vlist_simple_flush(&ip->dns_servers);
1778 vlist_simple_flush(&ip->dns_search);
1779 vlist_flush(&ip->route);
1780 vlist_flush(&ip->addr);
1781 vlist_flush(&ip->prefix);
1782 vlist_flush(&ip->neighbor);
1783 interface_write_resolv_conf(ip->iface->jail);
1784 }
1785
1786 void
1787 interface_ip_flush(struct interface_ip_settings *ip)
1788 {
1789 if (ip == &ip->iface->proto_ip)
1790 vlist_flush_all(&ip->iface->host_routes);
1791 vlist_simple_flush_all(&ip->dns_servers);
1792 vlist_simple_flush_all(&ip->dns_search);
1793 vlist_flush_all(&ip->route);
1794 vlist_flush_all(&ip->addr);
1795 vlist_flush_all(&ip->neighbor);
1796 vlist_flush_all(&ip->prefix);
1797 }
1798
1799 static void
1800 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
1801 {
1802 ip->iface = iface;
1803 ip->enabled = true;
1804 vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
1805 vlist_simple_init(&ip->dns_servers, struct dns_server, node);
1806 vlist_init(&ip->route, route_cmp, interface_update_proto_route);
1807 vlist_init(&ip->neighbor, neighbor_cmp, interface_update_proto_neighbor);
1808 vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
1809 vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
1810 }
1811
1812 void
1813 interface_ip_init(struct interface *iface)
1814 {
1815 __interface_ip_init(&iface->proto_ip, iface);
1816 __interface_ip_init(&iface->config_ip, iface);
1817 vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
1818 }
1819
1820 static void
1821 interface_ip_valid_until_handler(struct uloop_timeout *t)
1822 {
1823 time_t now = system_get_rtime();
1824 struct interface *iface;
1825 vlist_for_each_element(&interfaces, iface, node) {
1826 if (iface->state != IFS_UP)
1827 continue;
1828
1829 struct device_addr *addr, *addrp;
1830 struct device_route *route, *routep;
1831 struct device_prefix *pref, *prefp;
1832
1833 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
1834 if (addr->valid_until && addr->valid_until < now)
1835 vlist_delete(&iface->proto_ip.addr, &addr->node);
1836
1837 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
1838 if (route->valid_until && route->valid_until < now)
1839 vlist_delete(&iface->proto_ip.route, &route->node);
1840
1841 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
1842 if (pref->valid_until && pref->valid_until < now)
1843 vlist_delete(&iface->proto_ip.prefix, &pref->node);
1844
1845 }
1846
1847 uloop_timeout_set(t, 1000);
1848 }
1849
1850 static void __init
1851 interface_ip_init_worker(void)
1852 {
1853 valid_until_timeout.cb = interface_ip_valid_until_handler;
1854 uloop_timeout_set(&valid_until_timeout, 1000);
1855 }