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