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.
21 #include <arpa/inet.h>
25 #include "interface.h"
26 #include "interface-ip.h"
43 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
44 [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
45 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
46 [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
47 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
48 [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
49 [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
50 [ROUTE_TABLE] = { .name = "table", .type = BLOBMSG_TYPE_STRING },
51 [ROUTE_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
54 const struct config_param_list route_attr_list = {
55 .n_params = __ROUTE_MAX,
60 struct list_head prefixes = LIST_HEAD_INIT(prefixes);
61 static struct device_prefix *ula_prefix = NULL;
62 static struct uloop_timeout valid_until_timeout;
66 clear_if_addr(union if_addr *a, int mask)
68 int m_bytes = (mask + 7) / 8;
69 uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
70 uint8_t *p = (uint8_t *) a;
72 if (m_bytes < sizeof(a))
73 memset(p + m_bytes, 0, sizeof(a) - m_bytes);
75 p[m_bytes - 1] &= ~m_clear;
79 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
81 union if_addr *p1, *p2;
83 p1 = alloca(sizeof(*a1));
84 p2 = alloca(sizeof(*a2));
86 memcpy(p1, a1, sizeof(*a1));
87 clear_if_addr(p1, mask);
88 memcpy(p2, a2, sizeof(*a2));
89 clear_if_addr(p2, mask);
91 return !memcmp(p1, p2, sizeof(*p1));
94 static int set_ip_source_policy(bool add, bool v6, unsigned int priority,
95 const union if_addr *addr, uint8_t mask, struct interface *iface)
99 struct iprule rule = {
100 .flags = IPRULE_SRC | IPRULE_LOOKUP | IPRULE_PRIORITY,
101 .priority = priority,
102 .lookup = (v6) ? iface->ip6table : iface->ip4table,
110 rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
112 return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
115 static int set_ip_lo_policy(bool add, bool v6, struct interface *iface)
117 struct iprule rule = {
118 .flags = IPRULE_IN | IPRULE_LOOKUP | IPRULE_PRIORITY,
119 .priority = IPRULE_PRIORITY_NW + iface->l3_dev.dev->ifindex,
120 .lookup = (v6) ? iface->ip6table : iface->ip4table,
127 rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
129 return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
133 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
135 struct device_addr *addr;
137 vlist_for_each_element(&ip->addr, addr, node) {
141 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
144 // Handle offlink addresses correctly
145 unsigned int mask = addr->mask;
146 if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6 &&
147 (addr->flags & DEVADDR_OFFLINK))
150 if (!match_if_addr(&addr->addr, a, mask))
160 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
161 bool v6, struct device_route **res)
163 struct device_route *route;
165 vlist_for_each_element(&ip->route, route, node) {
169 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
172 if (!match_if_addr(&route->addr, a, route->mask))
175 if (route->flags & DEVROUTE_TABLE)
178 if (!*res || route->mask < (*res)->mask)
184 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
186 return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
187 __find_ip_addr_target(&iface->config_ip, a, v6);
191 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
192 bool v6, struct device_route **route)
194 __find_ip_route_target(&iface->proto_ip, a, v6, route);
195 __find_ip_route_target(&iface->config_ip, a, v6, route);
199 interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *iface)
201 struct device_route *route, *r_next = NULL;
202 bool defaultroute_target = false;
203 int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
205 route = calloc(1, sizeof(*route));
209 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
210 route->mask = v6 ? 128 : 32;
211 if (memcmp(&route->addr, addr, addrsize) == 0)
212 defaultroute_target = true;
214 memcpy(&route->addr, addr, addrsize);
217 /* look for locally addressable target first */
218 if (interface_ip_find_addr_target(iface, addr, v6))
221 /* do not stop at the first route, let the lookup compare
222 * masks to find the best match */
223 interface_ip_find_route_target(iface, addr, v6, &r_next);
225 vlist_for_each_element(&interfaces, iface, node) {
226 /* look for locally addressable target first */
227 if (interface_ip_find_addr_target(iface, addr, v6))
230 /* do not stop at the first route, let the lookup compare
231 * masks to find the best match */
232 interface_ip_find_route_target(iface, addr, v6, &r_next);
241 iface = r_next->iface;
242 memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
243 route->mtu = r_next->mtu;
244 route->metric = r_next->metric;
245 route->table = r_next->table;
248 route->iface = iface;
249 if (defaultroute_target)
252 vlist_add(&iface->host_routes, &route->node, route);
257 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
259 struct interface_ip_settings *ip;
260 struct blob_attr *tb[__ROUTE_MAX], *cur;
261 struct device_route *route;
262 int af = v6 ? AF_INET6 : AF_INET;
263 bool is_proto_route = !!iface;
265 blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
268 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
271 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
275 ip = &iface->config_ip;
277 ip = &iface->proto_ip;
280 route = calloc(1, sizeof(*route));
284 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
285 route->mask = v6 ? 128 : 32;
286 if ((cur = tb[ROUTE_MASK]) != NULL) {
287 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
288 if (route->mask > (v6 ? 128 : 32))
292 if ((cur = tb[ROUTE_TARGET]) != NULL) {
293 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
294 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
299 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
300 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
301 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
306 if ((cur = tb[ROUTE_METRIC]) != NULL) {
307 route->metric = blobmsg_get_u32(cur);
308 route->flags |= DEVROUTE_METRIC;
311 if ((cur = tb[ROUTE_MTU]) != NULL) {
312 route->mtu = blobmsg_get_u32(cur);
313 route->flags |= DEVROUTE_MTU;
316 // Use source-based routing
317 if (is_proto_route) {
318 route->table = (v6) ? iface->ip6table : iface->ip4table;
319 route->flags |= DEVROUTE_SRCTABLE;
322 if ((cur = tb[ROUTE_TABLE]) != NULL) {
323 if (!system_resolve_rt_table(blobmsg_data(cur), &route->table)) {
324 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
329 route->flags |= DEVROUTE_TABLE;
332 if ((cur = tb[ROUTE_VALID]) != NULL) {
333 int64_t valid = blobmsg_get_u32(cur);
334 int64_t valid_until = valid + (int64_t)system_get_rtime();
335 if (valid_until <= LONG_MAX && valid != 0xffffffffLL) // Catch overflow
336 route->valid_until = valid_until;
339 vlist_add(&ip->route, &route->node, route);
347 addr_cmp(const void *k1, const void *k2, void *ptr)
349 return memcmp(k1, k2, sizeof(struct device_addr) -
350 offsetof(struct device_addr, flags));
354 route_cmp(const void *k1, const void *k2, void *ptr)
356 const struct device_route *r1 = k1, *r2 = k2;
358 if (r1->mask != r2->mask)
359 return r2->mask - r1->mask;
361 if (r1->metric != r2->metric)
362 return r1->metric - r2->metric;
364 if (r1->flags != r2->flags)
365 return r2->flags - r1->flags;
367 return memcmp(&r1->addr, &r2->addr, sizeof(r1->addr));
371 prefix_cmp(const void *k1, const void *k2, void *ptr)
373 return memcmp(k1, k2, sizeof(struct device_prefix) -
374 offsetof(struct device_prefix, addr));
378 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
380 struct device *dev = iface->l3_dev.dev;
381 struct device_route route;
383 memset(&route, 0, sizeof(route));
385 route.flags = addr->flags;
386 route.mask = addr->mask;
387 memcpy(&route.addr, &addr->addr, sizeof(route.addr));
388 clear_if_addr(&route.addr, route.mask);
391 route.flags |= DEVADDR_KERNEL;
392 system_del_route(dev, &route);
394 if (!(addr->flags & DEVADDR_OFFLINK)) {
395 route.flags &= ~DEVADDR_KERNEL;
396 route.metric = iface->metric;
397 system_add_route(dev, &route);
400 if (!(addr->flags & DEVADDR_OFFLINK))
401 system_del_route(dev, &route);
406 interface_update_proto_addr(struct vlist_tree *tree,
407 struct vlist_node *node_new,
408 struct vlist_node *node_old)
410 struct interface_ip_settings *ip;
411 struct interface *iface;
413 struct device_addr *a_new = NULL, *a_old = NULL;
417 ip = container_of(tree, struct interface_ip_settings, addr);
419 dev = iface->l3_dev.dev;
422 a_new = container_of(node_new, struct device_addr, node);
424 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
428 uint32_t *a = (uint32_t *) &a_new->addr;
430 mask >>= a_new->mask;
431 a_new->broadcast = *a | htonl(mask);
436 a_old = container_of(node_old, struct device_addr, node);
438 if (a_new && a_old) {
441 if (a_old->flags != a_new->flags ||
442 a_old->valid_until != a_new->valid_until ||
443 a_old->preferred_until != a_new->preferred_until)
446 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
447 a_new->broadcast != a_old->broadcast)
452 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep) {
453 interface_handle_subnet_route(iface, a_old, false);
455 if ((a_old->flags & DEVADDR_FAMILY) == DEVADDR_INET6)
458 //This is needed for source routing to work correctly. If a device
459 //has two connections to a network using the same subnet, adding
460 //only the network-rule will cause packets to be routed through the
461 //first matching network (source IP matches both masks).
462 set_ip_source_policy(false, v6, IPRULE_PRIORITY_ADDR, &a_old->addr,
463 (v6) ? 128 : 32, iface);
464 set_ip_source_policy(false, v6, IPRULE_PRIORITY_NW, &a_old->addr,
467 system_del_address(dev, a_old);
473 a_new->enabled = true;
474 if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep) {
475 system_add_address(dev, a_new);
477 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET6)
480 set_ip_source_policy(true, v6, IPRULE_PRIORITY_ADDR, &a_new->addr,
481 (v6) ? 128 : 32, iface);
482 set_ip_source_policy(true, v6, IPRULE_PRIORITY_NW, &a_new->addr,
485 if ((a_new->flags & DEVADDR_OFFLINK) || iface->metric)
486 interface_handle_subnet_route(iface, a_new, true);
492 enable_route(struct interface_ip_settings *ip, struct device_route *route)
494 if (ip->no_defaultroute && !route->mask)
501 interface_update_proto_route(struct vlist_tree *tree,
502 struct vlist_node *node_new,
503 struct vlist_node *node_old)
505 struct interface_ip_settings *ip;
506 struct interface *iface;
508 struct device_route *route_old, *route_new;
511 ip = container_of(tree, struct interface_ip_settings, route);
513 dev = iface->l3_dev.dev;
515 route_old = container_of(node_old, struct device_route, node);
516 route_new = container_of(node_new, struct device_route, node);
518 if (node_old && node_new)
519 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
522 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
523 system_del_route(dev, route_old);
528 bool _enabled = enable_route(ip, route_new);
530 if (!(route_new->flags & DEVROUTE_METRIC))
531 route_new->metric = iface->metric;
533 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
534 system_add_route(dev, route_new);
536 route_new->iface = iface;
537 route_new->enabled = _enabled;
542 interface_update_host_route(struct vlist_tree *tree,
543 struct vlist_node *node_new,
544 struct vlist_node *node_old)
546 struct interface *iface;
548 struct device_route *route_old, *route_new;
550 iface = container_of(tree, struct interface, host_routes);
551 dev = iface->l3_dev.dev;
553 route_old = container_of(node_old, struct device_route, node);
554 route_new = container_of(node_new, struct device_route, node);
557 system_del_route(dev, route_old);
562 system_add_route(dev, route_new);
567 interface_set_prefix_address(struct device_prefix_assignment *assignment,
568 const struct device_prefix *prefix, struct interface *iface, bool add);
570 static void interface_trigger_ula_prefix(struct interface *iface,
571 const struct device_prefix *prefix, bool enable)
573 if (prefix == ula_prefix || (prefix->addr.s6_addr[0] & 0xfe) != 0xfc)
576 bool external_ula = false;
577 struct device_prefix_assignment *ula_assign = NULL;
578 struct device_prefix *c;
579 list_for_each_entry(c, &prefixes, head) {
580 if (c != ula_prefix && (c->addr.s6_addr[0] & 0xfe) != 0xfc)
583 struct device_prefix_assignment *a;
584 list_for_each_entry(a, &c->assignments, head) {
585 if (!strcmp(a->name, iface->name)) {
595 // Remove ULA assignment if there is an externally managed ULA and vice versa
596 if (ula_assign && ((enable && !external_ula) || (!enable && external_ula)))
597 interface_set_prefix_address(ula_assign, ula_prefix, iface, enable);
602 interface_set_prefix_address(struct device_prefix_assignment *assignment,
603 const struct device_prefix *prefix, struct interface *iface, bool add)
605 const struct interface *uplink = prefix->iface;
606 if (!iface->l3_dev.dev)
609 struct device *l3_downlink = iface->l3_dev.dev;
611 struct device_addr addr;
612 memset(&addr, 0, sizeof(addr));
613 addr.addr.in6 = prefix->addr;
614 addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
615 addr.addr.in6.s6_addr[15] += 1;
616 addr.mask = assignment->length;
617 addr.flags = DEVADDR_INET6;
618 addr.preferred_until = prefix->preferred_until;
619 addr.valid_until = prefix->valid_until;
621 if (!add && assignment->enabled) {
622 time_t now = system_get_rtime();
623 addr.preferred_until = now;
624 if (!addr.valid_until || addr.valid_until - now > 7200)
625 addr.valid_until = now + 7200;
626 system_add_address(l3_downlink, &addr);
627 assignment->enabled = false;
629 interface_trigger_ula_prefix(iface, prefix, true);
630 } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP)) {
631 system_add_address(l3_downlink, &addr);
632 if (uplink && uplink->l3_dev.dev) {
633 int mtu = system_update_ipv6_mtu(
634 uplink->l3_dev.dev, 0);
636 system_update_ipv6_mtu(l3_downlink, mtu);
638 assignment->enabled = true;
640 interface_trigger_ula_prefix(iface, prefix, false);
644 static bool interface_prefix_assign(struct list_head *list,
645 struct device_prefix_assignment *assign)
647 int32_t current = 0, asize = (1 << (64 - assign->length)) - 1;
648 struct device_prefix_assignment *c;
649 list_for_each_entry(c, list, head) {
650 if (assign->assigned != -1) {
651 if (assign->assigned > current && assign->assigned + asize < c->assigned) {
652 list_add_tail(&assign->head, &c->head);
655 } else if (assign->assigned == -1) {
656 current = (current + asize) & (~asize);
657 if (current + asize < c->assigned) {
658 assign->assigned = current;
659 list_add_tail(&assign->head, &c->head);
663 current = (c->assigned + (1 << (64 - c->length)));
668 static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup)
670 struct device_prefix_assignment *c;
671 struct interface *iface;
673 // Delete all assignments
674 while (!list_empty(&prefix->assignments)) {
675 c = list_first_entry(&prefix->assignments,
676 struct device_prefix_assignment, head);
677 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
678 interface_set_prefix_address(c, prefix, iface, false);
686 // End-of-assignment sentinel
687 c = malloc(sizeof(*c) + 1);
688 c->assigned = 1 << (64 - prefix->length);
691 list_add(&c->head, &prefix->assignments);
694 if (prefix->excl_length > 0) {
695 const char name[] = "!excluded";
696 c = malloc(sizeof(*c) + sizeof(name));
697 c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
698 ((1 << (64 - prefix->length)) - 1);
699 c->length = prefix->excl_length;
700 memcpy(c->name, name, sizeof(name));
701 list_add(&c->head, &prefix->assignments);
704 struct list_head assign_later = LIST_HEAD_INIT(assign_later);
705 vlist_for_each_element(&interfaces, iface, node) {
706 if (iface->config_ip.assignment_length < 48 ||
707 iface->config_ip.assignment_length > 64)
710 size_t namelen = strlen(iface->name) + 1;
711 c = malloc(sizeof(*c) + namelen);
712 c->length = iface->config_ip.assignment_length;
713 c->assigned = iface->config_ip.assignment_hint;
715 memcpy(c->name, iface->name, namelen);
717 // First process all custom assignments, put all others in later-list
718 if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
719 if (c->assigned != -1) {
721 netifd_log_message(L_WARNING, "Failed to assign requested subprefix "
722 "of size %hhu for %s, trying other\n", c->length, c->name);
724 list_add_tail(&c->head, &assign_later);
728 // Then try to assign all other + failed custom assignments
729 while (!list_empty(&assign_later)) {
730 c = list_first_entry(&assign_later, struct device_prefix_assignment, head);
733 bool assigned = false;
735 assigned = interface_prefix_assign(&prefix->assignments, c);
736 } while (!assigned && ++c->length <= 64);
739 netifd_log_message(L_WARNING, "Failed to assign subprefix "
740 "of size %hhu for %s\n", c->length, c->name);
745 list_for_each_entry(c, &prefix->assignments, head)
746 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
747 interface_set_prefix_address(c, prefix, iface, true);
751 void interface_refresh_assignments(bool hint)
753 static bool refresh = false;
754 if (!hint && refresh) {
755 struct device_prefix *p;
756 list_for_each_entry(p, &prefixes, head)
757 interface_update_prefix_assignments(p, true);
764 interface_update_prefix(struct vlist_tree *tree,
765 struct vlist_node *node_new,
766 struct vlist_node *node_old)
768 struct device_prefix *prefix_old, *prefix_new;
769 prefix_old = container_of(node_old, struct device_prefix, node);
770 prefix_new = container_of(node_new, struct device_prefix, node);
772 struct device_route route;
773 memset(&route, 0, sizeof(route));
774 route.flags = DEVADDR_INET6;
775 route.metric = INT32_MAX;
776 route.mask = (node_new) ? prefix_new->length : prefix_old->length;
777 route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
780 struct device_prefix_assignment *c;
781 struct interface *iface;
783 if (node_old && node_new) {
784 // Move assignments and refresh addresses to update valid times
785 list_splice(&prefix_old->assignments, &prefix_new->assignments);
787 list_for_each_entry(c, &prefix_new->assignments, head)
788 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
789 interface_set_prefix_address(c, prefix_new, iface, true);
790 } else if (node_new) {
791 // Set null-route to avoid routing loops and set routing policy
792 system_add_route(NULL, &route);
793 if (prefix_new->iface)
794 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW, &route.addr,
795 route.mask, prefix_new->iface);
798 interface_update_prefix_assignments(prefix_new, true);
799 } else if (node_old) {
800 interface_update_prefix_assignments(prefix_old, false);
803 if (prefix_old->iface)
804 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW, &route.addr,
805 route.mask, prefix_old->iface);
806 system_del_route(NULL, &route);
810 list_del(&prefix_old->head);
815 list_add(&prefix_new->head, &prefixes);
819 struct device_prefix*
820 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
821 uint8_t length, time_t valid_until, time_t preferred_until,
822 struct in6_addr *excl_addr, uint8_t excl_length)
824 struct device_prefix *prefix = calloc(1, sizeof(*prefix));
825 prefix->length = length;
826 prefix->addr = *addr;
827 prefix->preferred_until = preferred_until;
828 prefix->valid_until = valid_until;
829 prefix->iface = iface;
830 INIT_LIST_HEAD(&prefix->assignments);
833 prefix->excl_addr = *excl_addr;
834 prefix->excl_length = excl_length;
838 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
840 interface_update_prefix(NULL, &prefix->node, NULL);
846 interface_ip_set_ula_prefix(const char *prefix)
848 char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
850 strncpy(buf, prefix, sizeof(buf) - 1);
851 char *prefixaddr = strtok_r(buf, "/", &saveptr);
853 struct in6_addr addr;
854 if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
856 interface_update_prefix(NULL, NULL, &ula_prefix->node);
863 char *prefixlen = strtok_r(NULL, ",", &saveptr);
864 if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
867 if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
868 ula_prefix->length != length) {
870 interface_update_prefix(NULL, NULL, &ula_prefix->node);
872 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
878 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
880 struct dns_server *s;
882 s = calloc(1, sizeof(*s));
887 if (inet_pton(s->af, str, &s->addr.in))
891 if (inet_pton(s->af, str, &s->addr.in))
898 D(INTERFACE, "Add IPv%c DNS server: %s\n",
899 s->af == AF_INET6 ? '6' : '4', str);
900 vlist_simple_add(&ip->dns_servers, &s->node);
904 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
906 struct blob_attr *cur;
909 blobmsg_for_each_attr(cur, list, rem) {
910 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
913 if (!blobmsg_check_attr(cur, NULL))
916 interface_add_dns_server(ip, blobmsg_data(cur));
921 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
923 struct dns_search_domain *s;
924 int len = strlen(str);
926 s = calloc(1, sizeof(*s) + len + 1);
930 D(INTERFACE, "Add DNS search domain: %s\n", str);
931 memcpy(s->name, str, len);
932 vlist_simple_add(&ip->dns_search, &s->node);
936 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
938 struct blob_attr *cur;
941 blobmsg_for_each_attr(cur, list, rem) {
942 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
945 if (!blobmsg_check_attr(cur, NULL))
948 interface_add_dns_search_domain(ip, blobmsg_data(cur));
953 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
955 struct dns_server *s;
956 struct dns_search_domain *d;
958 char buf[INET6_ADDRSTRLEN];
960 vlist_simple_for_each_element(&ip->dns_servers, s, node) {
961 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
965 fprintf(f, "nameserver %s\n", str);
968 vlist_simple_for_each_element(&ip->dns_search, d, node) {
969 fprintf(f, "search %s\n", d->name);
974 interface_write_resolv_conf(void)
976 struct interface *iface;
977 char *path = alloca(strlen(resolv_conf) + 5);
979 uint32_t crcold, crcnew;
981 sprintf(path, "%s.tmp", resolv_conf);
983 f = fopen(path, "w+");
985 D(INTERFACE, "Failed to open %s for writing\n", path);
989 vlist_for_each_element(&interfaces, iface, node) {
990 if (iface->state != IFS_UP)
993 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
994 vlist_simple_empty(&iface->proto_ip.dns_servers) &&
995 vlist_simple_empty(&iface->config_ip.dns_search) &&
996 vlist_simple_empty(&iface->config_ip.dns_servers))
999 fprintf(f, "# Interface %s\n", iface->name);
1000 write_resolv_conf_entries(f, &iface->config_ip);
1001 if (!iface->proto_ip.no_dns)
1002 write_resolv_conf_entries(f, &iface->proto_ip);
1006 crcnew = crc32_file(f);
1009 crcold = crcnew + 1;
1010 f = fopen(resolv_conf, "r");
1012 crcold = crc32_file(f);
1016 if (crcold == crcnew) {
1018 } else if (rename(path, resolv_conf) < 0) {
1019 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
1024 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
1026 struct device_addr *addr;
1027 struct device_route *route;
1030 ip->enabled = enabled;
1031 dev = ip->iface->l3_dev.dev;
1035 vlist_for_each_element(&ip->addr, addr, node) {
1036 if (addr->enabled == enabled)
1040 system_add_address(dev, addr);
1042 system_del_address(dev, addr);
1043 addr->enabled = enabled;
1046 vlist_for_each_element(&ip->route, route, node) {
1047 bool _enabled = enabled;
1049 if (!enable_route(ip, route))
1052 if (route->enabled == _enabled)
1056 if (!(route->flags & DEVROUTE_METRIC))
1057 route->metric = ip->iface->metric;
1059 system_add_route(dev, route);
1061 system_del_route(dev, route);
1062 route->enabled = _enabled;
1065 struct device_prefix *c;
1066 struct device_prefix_assignment *a;
1067 list_for_each_entry(c, &prefixes, head)
1068 list_for_each_entry(a, &c->assignments, head)
1069 if (!strcmp(a->name, ip->iface->name))
1070 interface_set_prefix_address(a, c, ip->iface, enabled);
1072 set_ip_lo_policy(enabled, true, ip->iface);
1073 set_ip_lo_policy(enabled, false, ip->iface);
1077 interface_ip_update_start(struct interface_ip_settings *ip)
1079 if (ip != &ip->iface->config_ip) {
1080 vlist_simple_update(&ip->dns_servers);
1081 vlist_simple_update(&ip->dns_search);
1083 vlist_update(&ip->route);
1084 vlist_update(&ip->addr);
1085 vlist_update(&ip->prefix);
1089 interface_ip_update_complete(struct interface_ip_settings *ip)
1091 vlist_simple_flush(&ip->dns_servers);
1092 vlist_simple_flush(&ip->dns_search);
1093 vlist_flush(&ip->route);
1094 vlist_flush(&ip->addr);
1095 vlist_flush(&ip->prefix);
1096 interface_write_resolv_conf();
1100 interface_ip_flush(struct interface_ip_settings *ip)
1102 if (ip == &ip->iface->proto_ip)
1103 vlist_flush_all(&ip->iface->host_routes);
1104 vlist_simple_flush_all(&ip->dns_servers);
1105 vlist_simple_flush_all(&ip->dns_search);
1106 vlist_flush_all(&ip->route);
1107 vlist_flush_all(&ip->addr);
1108 vlist_flush_all(&ip->prefix);
1112 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
1116 vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
1117 vlist_simple_init(&ip->dns_servers, struct dns_server, node);
1118 vlist_init(&ip->route, route_cmp, interface_update_proto_route);
1119 vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
1120 vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
1124 interface_ip_init(struct interface *iface)
1126 __interface_ip_init(&iface->proto_ip, iface);
1127 __interface_ip_init(&iface->config_ip, iface);
1128 vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
1133 interface_ip_valid_until_handler(struct uloop_timeout *t)
1135 time_t now = system_get_rtime();
1136 struct interface *iface;
1137 vlist_for_each_element(&interfaces, iface, node) {
1138 if (iface->state != IFS_UP)
1141 struct device_addr *addr, *addrp;
1142 struct device_route *route, *routep;
1143 struct device_prefix *pref, *prefp;
1145 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
1146 if (addr->valid_until && addr->valid_until < now)
1147 vlist_delete(&iface->proto_ip.addr, &addr->node);
1149 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
1150 if (route->valid_until && route->valid_until < now)
1151 vlist_delete(&iface->proto_ip.route, &route->node);
1153 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
1154 if (pref->valid_until && pref->valid_until < now)
1155 vlist_delete(&iface->proto_ip.prefix, &pref->node);
1159 uloop_timeout_set(t, 1000);
1163 interface_ip_init_worker(void)
1165 valid_until_timeout.cb = interface_ip_valid_until_handler;
1166 uloop_timeout_set(&valid_until_timeout, 1000);