diff options
Diffstat (limited to 'src/network/networkd-dhcp6.c')
-rw-r--r-- | src/network/networkd-dhcp6.c | 131 |
1 files changed, 127 insertions, 4 deletions
diff --git a/src/network/networkd-dhcp6.c b/src/network/networkd-dhcp6.c index 1b119684dd..286b9071ae 100644 --- a/src/network/networkd-dhcp6.c +++ b/src/network/networkd-dhcp6.c @@ -21,6 +21,9 @@ #include "radv-internal.h" static int dhcp6_lease_address_acquired(sd_dhcp6_client *client, Link *link); +static Link *dhcp6_prefix_get(Manager *m, struct in6_addr *addr); +static int dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link); +static int dhcp6_prefix_remove_all(Manager *m, Link *link); static bool dhcp6_get_prefix_delegation(Link *link) { if (!link->network) @@ -91,7 +94,7 @@ static int dhcp6_pd_prefix_assign(Link *link, struct in6_addr *prefix, if (r < 0 && r != -EEXIST) return r; - r = manager_dhcp6_prefix_add(link->manager, prefix, link); + r = dhcp6_prefix_add(link->manager, prefix, link); if (r < 0) return r; @@ -105,7 +108,7 @@ static int dhcp6_route_remove_handler(sd_netlink *nl, sd_netlink_message *m, Lin r = sd_netlink_message_get_errno(m); if (r < 0) - log_link_debug_errno(link, r, "Received error on unreachable route removal for DHCPv6 delegated subnetl: %m"); + log_link_debug_errno(link, r, "Received error on unreachable route removal for DHCPv6 delegated subnet: %m"); return 1; } @@ -203,7 +206,7 @@ static int dhcp6_pd_prefix_distribute(Link *dhcp6_link, Iterator *i, if (!dhcp6_get_prefix_delegation(link)) continue; - assigned_link = manager_dhcp6_prefix_get(manager, &prefix.in6); + assigned_link = dhcp6_prefix_get(manager, &prefix.in6); if (assigned_link && assigned_link != link) continue; @@ -489,7 +492,7 @@ static void dhcp6_handler(sd_dhcp6_client *client, int event, void *userdata) { log_link_warning(link, "DHCPv6 lease lost"); (void) dhcp6_lease_pd_prefix_lost(client, link); - (void) manager_dhcp6_prefix_remove_all(link->manager, link); + (void) dhcp6_prefix_remove_all(link->manager, link); link->dhcp6_configured = false; break; @@ -686,3 +689,123 @@ int dhcp6_configure(Link *link) { return 0; } + +static Link *dhcp6_prefix_get(Manager *m, struct in6_addr *addr) { + assert_return(m, NULL); + assert_return(addr, NULL); + + return hashmap_get(m->dhcp6_prefixes, addr); +} + +static int dhcp6_route_add_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) { + int r; + + assert(link); + + r = sd_netlink_message_get_errno(m); + if (r < 0 && r != -EEXIST) + log_link_debug_errno(link, r, "Received error adding DHCPv6 Prefix Delegation route: %m"); + + return 0; +} + +static int dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) { + _cleanup_free_ struct in6_addr *a = NULL; + _cleanup_free_ char *buf = NULL; + Link *assigned_link; + Route *route; + int r; + + assert_return(m, -EINVAL); + assert_return(addr, -EINVAL); + + r = route_add(link, AF_INET6, (union in_addr_union *) addr, 64, + 0, 0, 0, &route); + if (r < 0) + return r; + + r = route_configure(route, link, dhcp6_route_add_handler); + if (r < 0) + return r; + + (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf); + log_link_debug(link, "Adding prefix route %s/64", strnull(buf)); + + assigned_link = hashmap_get(m->dhcp6_prefixes, addr); + if (assigned_link) { + assert(assigned_link == link); + return 0; + } + + a = newdup(struct in6_addr, addr, 1); + if (!a) + return -ENOMEM; + + r = hashmap_ensure_allocated(&m->dhcp6_prefixes, &in6_addr_hash_ops); + if (r < 0) + return r; + + r = hashmap_put(m->dhcp6_prefixes, a, link); + if (r < 0) + return r; + + TAKE_PTR(a); + link_ref(link); + return 0; +} + +static int dhcp6_prefix_remove_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) { + int r; + + assert(link); + + r = sd_netlink_message_get_errno(m); + if (r < 0) + log_link_debug_errno(link, r, "Received error on DHCPv6 Prefix Delegation route removal: %m"); + + return 1; +} + +int dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) { + _cleanup_free_ struct in6_addr *a = NULL; + _cleanup_(link_unrefp) Link *l = NULL; + _cleanup_free_ char *buf = NULL; + Route *route; + int r; + + assert_return(m, -EINVAL); + assert_return(addr, -EINVAL); + + l = hashmap_remove2(m->dhcp6_prefixes, addr, (void **) &a); + if (!l) + return -EINVAL; + + (void) sd_radv_remove_prefix(l->radv, addr, 64); + r = route_get(l, AF_INET6, (union in_addr_union *) addr, 64, 0, 0, 0, &route); + if (r < 0) + return r; + + r = route_remove(route, l, dhcp6_prefix_remove_handler); + if (r < 0) + return r; + + (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf); + log_link_debug(l, "Removing prefix route %s/64", strnull(buf)); + + return 0; +} + +static int dhcp6_prefix_remove_all(Manager *m, Link *link) { + struct in6_addr *addr; + Iterator i; + Link *l; + + assert_return(m, -EINVAL); + assert_return(link, -EINVAL); + + HASHMAP_FOREACH_KEY(l, addr, m->dhcp6_prefixes, i) + if (l == link) + (void) dhcp6_prefix_remove(m, addr); + + return 0; +} |