2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2012 Steven Barth <steven@midlink.org>
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
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.
20 #include <arpa/inet.h>
21 #include <netinet/in.h>
25 #include "interface.h"
26 #include "interface-ip.h"
44 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
45 [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
46 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
47 [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
48 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
49 [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
50 [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
51 [ROUTE_TABLE] = { .name = "table", .type = BLOBMSG_TYPE_STRING },
52 [ROUTE_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
53 [ROUTE_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_STRING },
56 const struct uci_blob_param_list route_attr_list = {
57 .n_params = __ROUTE_MAX,
62 struct list_head prefixes = LIST_HEAD_INIT(prefixes);
63 static struct device_prefix *ula_prefix = NULL;
64 static struct uloop_timeout valid_until_timeout;
68 clear_if_addr(union if_addr *a, int mask)
70 int m_bytes = (mask + 7) / 8;
71 uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
72 uint8_t *p = (uint8_t *) a;
74 if (m_bytes < sizeof(a))
75 memset(p + m_bytes, 0, sizeof(a) - m_bytes);
77 p[m_bytes - 1] &= ~m_clear;
81 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
83 union if_addr *p1, *p2;
85 p1 = alloca(sizeof(*a1));
86 p2 = alloca(sizeof(*a2));
88 memcpy(p1, a1, sizeof(*a1));
89 clear_if_addr(p1, mask);
90 memcpy(p2, a2, sizeof(*a2));
91 clear_if_addr(p2, mask);
93 return !memcmp(p1, p2, sizeof(*p1));
96 static int set_ip_source_policy(bool add, bool v6, unsigned int priority,
97 const union if_addr *addr, uint8_t mask, unsigned int table,
98 struct interface *in_iface, const char *action)
100 struct iprule rule = {
101 .flags = IPRULE_PRIORITY,
106 rule.flags |= IPRULE_SRC;
107 rule.src_addr = *addr;
108 rule.src_mask = mask;
112 rule.flags |= IPRULE_LOOKUP;
118 rule.flags |= IPRULE_ACTION;
119 system_resolve_iprule_action(action, &rule.action);
122 if (in_iface && in_iface->l3_dev.dev) {
123 rule.flags |= IPRULE_IN;
124 strcpy(rule.in_dev, in_iface->l3_dev.dev->ifname);
127 rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
129 return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
132 static int set_ip_lo_policy(bool add, bool v6, struct interface *iface)
134 struct iprule rule = {
135 .flags = IPRULE_IN | IPRULE_LOOKUP | IPRULE_PRIORITY,
136 .priority = IPRULE_PRIORITY_NW + iface->l3_dev.dev->ifindex,
137 .lookup = (v6) ? iface->ip6table : iface->ip4table,
144 rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
146 return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
150 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
152 struct device_addr *addr;
154 vlist_for_each_element(&ip->addr, addr, node) {
158 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
161 // Handle offlink addresses correctly
162 unsigned int mask = addr->mask;
163 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6 &&
164 (addr->flags & DEVADDR_OFFLINK))
167 if (!match_if_addr(&addr->addr, a, mask))
177 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
178 bool v6, struct device_route **res)
180 struct device_route *route;
182 vlist_for_each_element(&ip->route, route, node) {
186 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
189 if (!match_if_addr(&route->addr, a, route->mask))
192 if (route->flags & DEVROUTE_TABLE)
195 if (!*res || route->mask < (*res)->mask)
201 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
203 return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
204 __find_ip_addr_target(&iface->config_ip, a, v6);
208 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
209 bool v6, struct device_route **route)
211 __find_ip_route_target(&iface->proto_ip, a, v6, route);
212 __find_ip_route_target(&iface->config_ip, a, v6, route);
216 interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *iface)
218 struct device_route *route, *r_next = NULL;
219 bool defaultroute_target = false;
220 int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
222 route = calloc(1, sizeof(*route));
226 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
227 route->mask = v6 ? 128 : 32;
228 if (memcmp(&route->addr, addr, addrsize) == 0)
229 defaultroute_target = true;
231 memcpy(&route->addr, addr, addrsize);
234 /* look for locally addressable target first */
235 if (interface_ip_find_addr_target(iface, addr, v6))
238 /* do not stop at the first route, let the lookup compare
239 * masks to find the best match */
240 interface_ip_find_route_target(iface, addr, v6, &r_next);
242 vlist_for_each_element(&interfaces, iface, node) {
243 /* look for locally addressable target first */
244 if (interface_ip_find_addr_target(iface, addr, v6))
247 /* do not stop at the first route, let the lookup compare
248 * masks to find the best match */
249 interface_ip_find_route_target(iface, addr, v6, &r_next);
258 iface = r_next->iface;
259 memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
260 route->mtu = r_next->mtu;
261 route->metric = r_next->metric;
262 route->table = r_next->table;
265 route->iface = iface;
266 if (defaultroute_target)
269 vlist_add(&iface->host_routes, &route->node, route);
274 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
276 struct interface_ip_settings *ip;
277 struct blob_attr *tb[__ROUTE_MAX], *cur;
278 struct device_route *route;
279 int af = v6 ? AF_INET6 : AF_INET;
280 bool is_proto_route = !!iface;
282 blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
285 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
288 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
292 ip = &iface->config_ip;
294 ip = &iface->proto_ip;
297 route = calloc(1, sizeof(*route));
301 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
302 route->mask = v6 ? 128 : 32;
303 if ((cur = tb[ROUTE_MASK]) != NULL) {
304 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
305 if (route->mask > (v6 ? 128 : 32))
309 if ((cur = tb[ROUTE_TARGET]) != NULL) {
310 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
311 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
316 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
317 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
318 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
323 if ((cur = tb[ROUTE_METRIC]) != NULL) {
324 route->metric = blobmsg_get_u32(cur);
325 route->flags |= DEVROUTE_METRIC;
327 route->metric = iface->metric;
329 if ((cur = tb[ROUTE_MTU]) != NULL) {
330 route->mtu = blobmsg_get_u32(cur);
331 route->flags |= DEVROUTE_MTU;
334 // Use source-based routing
335 if ((cur = tb[ROUTE_SOURCE]) != NULL) {
336 char *saveptr, *source = alloca(blobmsg_data_len(cur));
337 memcpy(source, blobmsg_data(cur), blobmsg_data_len(cur));
339 const char *addr = strtok_r(source, "/", &saveptr);
340 const char *mask = strtok_r(NULL, "/", &saveptr);
342 if (!addr || inet_pton(af, addr, &route->source) < 1) {
343 DPRINTF("Failed to parse route source: %s\n", addr);
347 route->sourcemask = (mask) ? atoi(mask) : ((af == AF_INET6) ? 128 : 32);
350 if (is_proto_route) {
351 route->table = (v6) ? iface->ip6table : iface->ip4table;
352 route->flags |= DEVROUTE_SRCTABLE;
355 if ((cur = tb[ROUTE_TABLE]) != NULL) {
356 if (!system_resolve_rt_table(blobmsg_data(cur), &route->table)) {
357 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
362 route->flags |= DEVROUTE_TABLE;
365 if ((cur = tb[ROUTE_VALID]) != NULL) {
366 int64_t valid = blobmsg_get_u32(cur);
367 int64_t valid_until = valid + (int64_t)system_get_rtime();
368 if (valid_until <= LONG_MAX && valid != 0xffffffffLL) // Catch overflow
369 route->valid_until = valid_until;
372 vlist_add(&ip->route, &route->node, route);
380 addr_cmp(const void *k1, const void *k2, void *ptr)
382 return memcmp(k1, k2, sizeof(struct device_addr) -
383 offsetof(struct device_addr, flags));
387 route_cmp(const void *k1, const void *k2, void *ptr)
389 const struct device_route *r1 = k1, *r2 = k2;
391 if (r1->mask != r2->mask)
392 return r2->mask - r1->mask;
394 if (r1->metric != r2->metric)
395 return r1->metric - r2->metric;
397 if (r1->flags != r2->flags)
398 return r2->flags - r1->flags;
400 if (r1->sourcemask != r2->sourcemask)
401 return r1->sourcemask - r2->sourcemask;
403 int maskcmp = memcmp(&r1->source, &r2->source, sizeof(r1->source));
407 return memcmp(&r1->addr, &r2->addr, sizeof(r1->addr));
411 prefix_cmp(const void *k1, const void *k2, void *ptr)
413 return memcmp(k1, k2, offsetof(struct device_prefix, pclass) -
414 offsetof(struct device_prefix, addr));
418 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
420 struct device *dev = iface->l3_dev.dev;
421 struct device_route route;
423 memset(&route, 0, sizeof(route));
425 route.flags = addr->flags;
426 route.mask = addr->mask;
427 memcpy(&route.addr, &addr->addr, sizeof(route.addr));
428 clear_if_addr(&route.addr, route.mask);
431 route.flags |= DEVADDR_KERNEL;
432 system_del_route(dev, &route);
434 if (!(addr->flags & DEVADDR_OFFLINK)) {
435 route.flags &= ~DEVADDR_KERNEL;
436 route.metric = iface->metric;
437 system_add_route(dev, &route);
440 if (!(addr->flags & DEVADDR_OFFLINK))
441 system_del_route(dev, &route);
446 interface_update_proto_addr(struct vlist_tree *tree,
447 struct vlist_node *node_new,
448 struct vlist_node *node_old)
450 struct interface_ip_settings *ip;
451 struct interface *iface;
453 struct device_addr *a_new = NULL, *a_old = NULL;
454 bool replace = false;
458 ip = container_of(tree, struct interface_ip_settings, addr);
460 dev = iface->l3_dev.dev;
462 if (!node_new || !node_old)
463 iface->updated |= IUF_ADDRESS;
466 a_new = container_of(node_new, struct device_addr, node);
468 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
472 uint32_t *a = (uint32_t *) &a_new->addr;
474 mask >>= a_new->mask;
475 a_new->broadcast = *a | htonl(mask);
480 a_old = container_of(node_old, struct device_addr, node);
482 if (a_new && a_old) {
485 if (a_old->flags != a_new->flags || a_old->failed)
488 if (a_old->valid_until != a_new->valid_until ||
489 a_old->preferred_until != a_new->preferred_until)
492 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
493 a_new->broadcast != a_old->broadcast)
498 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep) {
499 interface_handle_subnet_route(iface, a_old, false);
501 if ((a_old->flags & DEVADDR_FAMILY) == DEVADDR_INET6)
504 unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
506 //This is needed for source routing to work correctly. If a device
507 //has two connections to a network using the same subnet, adding
508 //only the network-rule will cause packets to be routed through the
509 //first matching network (source IP matches both masks).
511 set_ip_source_policy(false, v6, IPRULE_PRIORITY_ADDR, &a_old->addr,
512 (v6) ? 128 : 32, table, NULL, NULL);
513 set_ip_source_policy(false, v6, IPRULE_PRIORITY_NW, &a_old->addr,
514 a_old->mask, table, NULL, NULL);
517 system_del_address(dev, a_old);
524 a_new->enabled = true;
525 if (!(a_new->flags & DEVADDR_EXTERNAL) && (!keep || replace)) {
526 if (system_add_address(dev, a_new))
527 a_new->failed = true;
530 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET6)
533 unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
536 set_ip_source_policy(true, v6, IPRULE_PRIORITY_ADDR, &a_new->addr,
537 (v6) ? 128 : 32, table, NULL, NULL);
538 set_ip_source_policy(true, v6, IPRULE_PRIORITY_NW, &a_new->addr,
539 a_new->mask, table, NULL, NULL);
543 if ((a_new->flags & DEVADDR_OFFLINK) || iface->metric)
544 interface_handle_subnet_route(iface, a_new, true);
550 enable_route(struct interface_ip_settings *ip, struct device_route *route)
552 if (ip->no_defaultroute && !route->mask)
559 interface_update_proto_route(struct vlist_tree *tree,
560 struct vlist_node *node_new,
561 struct vlist_node *node_old)
563 struct interface_ip_settings *ip;
564 struct interface *iface;
566 struct device_route *route_old, *route_new;
569 ip = container_of(tree, struct interface_ip_settings, route);
571 dev = iface->l3_dev.dev;
573 if (!node_new || !node_old)
574 iface->updated |= IUF_ROUTE;
576 route_old = container_of(node_old, struct device_route, node);
577 route_new = container_of(node_new, struct device_route, node);
579 if (node_old && node_new)
580 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop)) &&
581 (route_old->table == route_new->table) && !route_old->failed;
584 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
585 system_del_route(dev, route_old);
591 bool _enabled = enable_route(ip, route_new);
593 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
594 if (system_add_route(dev, route_new))
595 route_new->failed = true;
597 route_new->iface = iface;
598 route_new->enabled = _enabled;
603 interface_update_host_route(struct vlist_tree *tree,
604 struct vlist_node *node_new,
605 struct vlist_node *node_old)
607 struct interface *iface;
609 struct device_route *route_old, *route_new;
611 iface = container_of(tree, struct interface, host_routes);
612 dev = iface->l3_dev.dev;
614 route_old = container_of(node_old, struct device_route, node);
615 route_new = container_of(node_new, struct device_route, node);
618 system_del_route(dev, route_old);
623 system_add_route(dev, route_new);
628 interface_set_prefix_address(struct device_prefix_assignment *assignment,
629 const struct device_prefix *prefix, struct interface *iface, bool add)
631 const struct interface *uplink = prefix->iface;
632 if (!iface->l3_dev.dev)
635 struct device *l3_downlink = iface->l3_dev.dev;
637 struct device_addr addr;
638 memset(&addr, 0, sizeof(addr));
639 addr.addr.in6 = prefix->addr;
640 addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
641 addr.addr.in6.s6_addr[15] += 1;
642 addr.mask = assignment->length;
643 addr.flags = DEVADDR_INET6;
644 addr.preferred_until = prefix->preferred_until;
645 addr.valid_until = prefix->valid_until;
647 if (!add && assignment->enabled) {
648 time_t now = system_get_rtime();
649 addr.preferred_until = now;
650 if (!addr.valid_until || addr.valid_until - now > 7200)
651 addr.valid_until = now + 7200;
652 system_del_address(l3_downlink, &addr); // Work around dangling prefix routes
653 system_add_address(l3_downlink, &addr);
655 if (prefix->iface->ip6table)
656 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW, &addr.addr,
657 addr.mask, prefix->iface->ip6table, iface, NULL);
659 set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT, &addr.addr,
660 addr.mask, 0, iface, "unreachable");
663 assignment->enabled = false;
664 } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP)) {
665 system_add_address(l3_downlink, &addr);
666 if (prefix->iface && !assignment->enabled) {
667 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT, &addr.addr,
668 addr.mask, 0, iface, "unreachable");
670 if (prefix->iface->ip6table)
671 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW, &addr.addr,
672 addr.mask, prefix->iface->ip6table, iface, NULL);
674 if (uplink && uplink->l3_dev.dev) {
675 int mtu = system_update_ipv6_mtu(
676 uplink->l3_dev.dev, 0);
678 system_update_ipv6_mtu(l3_downlink, mtu);
680 assignment->enabled = true;
684 static bool interface_prefix_assign(struct list_head *list,
685 struct device_prefix_assignment *assign)
687 int32_t current = 0, asize = (1 << (64 - assign->length)) - 1;
688 struct device_prefix_assignment *c;
689 list_for_each_entry(c, list, head) {
690 if (assign->assigned != -1) {
691 if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
692 list_add_tail(&assign->head, &c->head);
695 } else if (assign->assigned == -1) {
696 current = (current + asize) & (~asize);
697 if (current + asize < c->assigned) {
698 assign->assigned = current;
699 list_add_tail(&assign->head, &c->head);
703 current = (c->assigned + (1 << (64 - c->length)));
708 static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup)
710 struct device_prefix_assignment *c;
711 struct interface *iface;
713 // Delete all assignments
714 while (!list_empty(&prefix->assignments)) {
715 c = list_first_entry(&prefix->assignments,
716 struct device_prefix_assignment, head);
717 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
718 interface_set_prefix_address(c, prefix, iface, false);
726 // End-of-assignment sentinel
727 c = malloc(sizeof(*c) + 1);
728 c->assigned = 1 << (64 - prefix->length);
731 list_add(&c->head, &prefix->assignments);
734 if (prefix->excl_length > 0) {
735 const char name[] = "!excluded";
736 c = malloc(sizeof(*c) + sizeof(name));
737 c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
738 ((1 << (64 - prefix->length)) - 1);
739 c->length = prefix->excl_length;
740 memcpy(c->name, name, sizeof(name));
741 list_add(&c->head, &prefix->assignments);
744 bool assigned_any = false;
745 struct list_head assign_later = LIST_HEAD_INIT(assign_later);
746 vlist_for_each_element(&interfaces, iface, node) {
747 if (iface->assignment_length < 48 ||
748 iface->assignment_length > 64)
751 // Test whether there is a matching class
752 if (!list_empty(&iface->assignment_classes)) {
755 struct interface_assignment_class *c;
756 list_for_each_entry(c, &iface->assignment_classes, head) {
757 if (!strcmp(c->name, prefix->pclass)) {
767 size_t namelen = strlen(iface->name) + 1;
768 c = malloc(sizeof(*c) + namelen);
769 c->length = iface->assignment_length;
770 c->assigned = iface->assignment_hint;
772 memcpy(c->name, iface->name, namelen);
774 // First process all custom assignments, put all others in later-list
775 if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
776 if (c->assigned != -1) {
778 netifd_log_message(L_WARNING, "Failed to assign requested subprefix "
779 "of size %hhu for %s, trying other\n", c->length, c->name);
782 struct list_head *next = &assign_later;
783 struct device_prefix_assignment *n;
784 list_for_each_entry(n, &assign_later, head) {
785 if (n->length < c->length) {
790 list_add_tail(&c->head, next);
793 if (c->assigned != -1)
797 // Then try to assign all other + failed custom assignments
798 while (!list_empty(&assign_later)) {
799 c = list_first_entry(&assign_later, struct device_prefix_assignment, head);
802 bool assigned = false;
804 assigned = interface_prefix_assign(&prefix->assignments, c);
805 } while (!assigned && ++c->length <= 64);
808 netifd_log_message(L_WARNING, "Failed to assign subprefix "
809 "of size %hhu for %s\n", c->length, c->name);
816 list_for_each_entry(c, &prefix->assignments, head)
817 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
818 interface_set_prefix_address(c, prefix, iface, true);
821 netifd_log_message(L_WARNING, "You have delegated IPv6-prefixes but haven't assigned them "
822 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
826 void interface_refresh_assignments(bool hint)
828 static bool refresh = false;
829 if (!hint && refresh) {
830 struct device_prefix *p;
831 list_for_each_entry(p, &prefixes, head)
832 interface_update_prefix_assignments(p, true);
839 interface_update_prefix(struct vlist_tree *tree,
840 struct vlist_node *node_new,
841 struct vlist_node *node_old)
843 struct device_prefix *prefix_old, *prefix_new;
844 prefix_old = container_of(node_old, struct device_prefix, node);
845 prefix_new = container_of(node_new, struct device_prefix, node);
847 struct interface_ip_settings *ip = container_of(tree, struct interface_ip_settings, prefix);
848 if (tree && (!node_new || !node_old))
849 ip->iface->updated |= IUF_PREFIX;
851 struct device_route route;
852 memset(&route, 0, sizeof(route));
853 route.flags = DEVADDR_INET6;
854 route.metric = INT32_MAX;
855 route.mask = (node_new) ? prefix_new->length : prefix_old->length;
856 route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
859 struct device_prefix_assignment *c;
860 struct interface *iface;
862 if (node_old && node_new) {
863 // Move assignments and refresh addresses to update valid times
864 list_splice(&prefix_old->assignments, &prefix_new->assignments);
866 list_for_each_entry(c, &prefix_new->assignments, head)
867 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
868 interface_set_prefix_address(c, prefix_new, iface, true);
869 } else if (node_new) {
870 // Set null-route to avoid routing loops
871 system_add_route(NULL, &route);
873 if (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation)
874 interface_update_prefix_assignments(prefix_new, true);
875 } else if (node_old) {
877 interface_update_prefix_assignments(prefix_old, false);
878 system_del_route(NULL, &route);
882 if (prefix_old->head.next)
883 list_del(&prefix_old->head);
887 if (node_new && (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation))
888 list_add(&prefix_new->head, &prefixes);
892 struct device_prefix*
893 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
894 uint8_t length, time_t valid_until, time_t preferred_until,
895 struct in6_addr *excl_addr, uint8_t excl_length, const char *pclass)
898 pclass = (iface) ? iface->name : "local";
900 struct device_prefix *prefix = calloc(1, sizeof(*prefix) + strlen(pclass) + 1);
901 prefix->length = length;
902 prefix->addr = *addr;
903 prefix->preferred_until = preferred_until;
904 prefix->valid_until = valid_until;
905 prefix->iface = iface;
906 INIT_LIST_HEAD(&prefix->assignments);
909 prefix->excl_addr = *excl_addr;
910 prefix->excl_length = excl_length;
913 strcpy(prefix->pclass, pclass);
916 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
918 interface_update_prefix(NULL, &prefix->node, NULL);
924 interface_ip_set_ula_prefix(const char *prefix)
926 char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
928 strncpy(buf, prefix, sizeof(buf) - 1);
929 char *prefixaddr = strtok_r(buf, "/", &saveptr);
931 struct in6_addr addr;
932 if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
934 interface_update_prefix(NULL, NULL, &ula_prefix->node);
941 char *prefixlen = strtok_r(NULL, ",", &saveptr);
942 if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
945 if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
946 ula_prefix->length != length) {
948 interface_update_prefix(NULL, NULL, &ula_prefix->node);
950 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
951 0, 0, NULL, 0, NULL);
956 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
958 struct dns_server *s;
960 s = calloc(1, sizeof(*s));
965 if (inet_pton(s->af, str, &s->addr.in))
969 if (inet_pton(s->af, str, &s->addr.in))
976 D(INTERFACE, "Add IPv%c DNS server: %s\n",
977 s->af == AF_INET6 ? '6' : '4', str);
978 vlist_simple_add(&ip->dns_servers, &s->node);
982 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
984 struct blob_attr *cur;
987 blobmsg_for_each_attr(cur, list, rem) {
988 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
991 if (!blobmsg_check_attr(cur, NULL))
994 interface_add_dns_server(ip, blobmsg_data(cur));
999 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
1001 struct dns_search_domain *s;
1002 int len = strlen(str);
1004 s = calloc(1, sizeof(*s) + len + 1);
1008 D(INTERFACE, "Add DNS search domain: %s\n", str);
1009 memcpy(s->name, str, len);
1010 vlist_simple_add(&ip->dns_search, &s->node);
1014 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
1016 struct blob_attr *cur;
1019 blobmsg_for_each_attr(cur, list, rem) {
1020 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
1023 if (!blobmsg_check_attr(cur, NULL))
1026 interface_add_dns_search_domain(ip, blobmsg_data(cur));
1031 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip, const char *dev)
1033 struct dns_server *s;
1034 struct dns_search_domain *d;
1036 char buf[INET6_ADDRSTRLEN];
1038 vlist_simple_for_each_element(&ip->dns_servers, s, node) {
1039 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
1043 if (s->af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&s->addr))
1044 fprintf(f, "nameserver %s%%%s\n", str, dev);
1046 fprintf(f, "nameserver %s\n", str);
1049 vlist_simple_for_each_element(&ip->dns_search, d, node) {
1050 fprintf(f, "search %s\n", d->name);
1055 interface_write_resolv_conf(void)
1057 struct interface *iface;
1058 char *path = alloca(strlen(resolv_conf) + 5);
1060 uint32_t crcold, crcnew;
1062 sprintf(path, "%s.tmp", resolv_conf);
1064 f = fopen(path, "w+");
1066 D(INTERFACE, "Failed to open %s for writing\n", path);
1070 vlist_for_each_element(&interfaces, iface, node) {
1071 if (iface->state != IFS_UP)
1074 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
1075 vlist_simple_empty(&iface->proto_ip.dns_servers) &&
1076 vlist_simple_empty(&iface->config_ip.dns_search) &&
1077 vlist_simple_empty(&iface->config_ip.dns_servers))
1080 fprintf(f, "# Interface %s\n", iface->name);
1081 write_resolv_conf_entries(f, &iface->config_ip, iface->ifname);
1082 if (!iface->proto_ip.no_dns)
1083 write_resolv_conf_entries(f, &iface->proto_ip, iface->ifname);
1087 crcnew = crc32_file(f);
1090 crcold = crcnew + 1;
1091 f = fopen(resolv_conf, "r");
1093 crcold = crc32_file(f);
1097 if (crcold == crcnew) {
1099 } else if (rename(path, resolv_conf) < 0) {
1100 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
1105 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
1107 struct device_addr *addr;
1108 struct device_route *route;
1111 ip->enabled = enabled;
1112 dev = ip->iface->l3_dev.dev;
1116 vlist_for_each_element(&ip->addr, addr, node) {
1117 if (addr->enabled == enabled)
1121 system_add_address(dev, addr);
1123 system_del_address(dev, addr);
1124 addr->enabled = enabled;
1127 vlist_for_each_element(&ip->route, route, node) {
1128 bool _enabled = enabled;
1130 if (!enable_route(ip, route))
1133 if (route->enabled == _enabled)
1137 if (!(route->flags & DEVROUTE_METRIC))
1138 route->metric = ip->iface->metric;
1140 system_add_route(dev, route);
1142 system_del_route(dev, route);
1143 route->enabled = _enabled;
1146 struct device_prefix *c;
1147 struct device_prefix_assignment *a;
1148 list_for_each_entry(c, &prefixes, head)
1149 list_for_each_entry(a, &c->assignments, head)
1150 if (!strcmp(a->name, ip->iface->name))
1151 interface_set_prefix_address(a, c, ip->iface, enabled);
1153 if (ip->iface && ip->iface->l3_dev.dev) {
1154 set_ip_lo_policy(enabled, true, ip->iface);
1155 set_ip_lo_policy(enabled, false, ip->iface);
1157 set_ip_source_policy(enabled, true, IPRULE_PRIORITY_REJECT + ip->iface->l3_dev.dev->ifindex,
1158 NULL, 0, 0, ip->iface, "failed_policy");
1163 interface_ip_update_start(struct interface_ip_settings *ip)
1165 if (ip != &ip->iface->config_ip) {
1166 vlist_simple_update(&ip->dns_servers);
1167 vlist_simple_update(&ip->dns_search);
1169 vlist_update(&ip->route);
1170 vlist_update(&ip->addr);
1171 vlist_update(&ip->prefix);
1175 interface_ip_update_complete(struct interface_ip_settings *ip)
1177 vlist_simple_flush(&ip->dns_servers);
1178 vlist_simple_flush(&ip->dns_search);
1179 vlist_flush(&ip->route);
1180 vlist_flush(&ip->addr);
1181 vlist_flush(&ip->prefix);
1182 interface_write_resolv_conf();
1186 interface_ip_flush(struct interface_ip_settings *ip)
1188 if (ip == &ip->iface->proto_ip)
1189 vlist_flush_all(&ip->iface->host_routes);
1190 vlist_simple_flush_all(&ip->dns_servers);
1191 vlist_simple_flush_all(&ip->dns_search);
1192 vlist_flush_all(&ip->route);
1193 vlist_flush_all(&ip->addr);
1194 vlist_flush_all(&ip->prefix);
1198 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
1202 vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
1203 vlist_simple_init(&ip->dns_servers, struct dns_server, node);
1204 vlist_init(&ip->route, route_cmp, interface_update_proto_route);
1205 vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
1206 vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
1210 interface_ip_init(struct interface *iface)
1212 __interface_ip_init(&iface->proto_ip, iface);
1213 __interface_ip_init(&iface->config_ip, iface);
1214 vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
1219 interface_ip_valid_until_handler(struct uloop_timeout *t)
1221 time_t now = system_get_rtime();
1222 struct interface *iface;
1223 vlist_for_each_element(&interfaces, iface, node) {
1224 if (iface->state != IFS_UP)
1227 struct device_addr *addr, *addrp;
1228 struct device_route *route, *routep;
1229 struct device_prefix *pref, *prefp;
1231 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
1232 if (addr->valid_until && addr->valid_until < now)
1233 vlist_delete(&iface->proto_ip.addr, &addr->node);
1235 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
1236 if (route->valid_until && route->valid_until < now)
1237 vlist_delete(&iface->proto_ip.route, &route->node);
1239 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
1240 if (pref->valid_until && pref->valid_until < now)
1241 vlist_delete(&iface->proto_ip.prefix, &pref->node);
1245 uloop_timeout_set(t, 1000);
1249 interface_ip_init_worker(void)
1251 valid_until_timeout.cb = interface_ip_valid_until_handler;
1252 uloop_timeout_set(&valid_until_timeout, 1000);