From aba496a58acf9d9c61314de71353550e579f85ee Mon Sep 17 00:00:00 2001 From: Umut Tezduyar Lindskog Date: Wed, 2 Apr 2014 21:31:12 +0200 Subject: networkd: smooth transition from ipv4ll to dhcp address Currently when both ipv4ll and dhcp are enabled, ipv4ll address (if one has been claimed) is removed when dhcp address is aquired. This is not the best thing to do since there might be clients unaware of the removal trying to communicate. This patch provides a smooth transition between ipv4ll and dhcp. If ipv4ll address was claimed [1] before dhcp, address is marked as deprecated. Deprecated address is still a valid address and packets can be received on it but address cannot be selected as a source address. If dhcp lease cannot be extended, then ipv4ll address is marked as valid again. [1] If there is no collision, claiming IPv4LL takes between 4 to 7 seconds. --- src/network/networkd-link.c | 94 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 10 deletions(-) (limited to 'src/network/networkd-link.c') diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 4e0fe0a271..63d253d941 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -30,6 +30,9 @@ #include "dhcp-lease-internal.h" +static int ipv4ll_address_update(Link *link, bool deprecate); +static bool ipv4ll_is_bound(sd_ipv4ll *ll); + int link_new(Manager *manager, struct udev_device *device, Link **ret) { _cleanup_link_free_ Link *link = NULL; const char *ifname; @@ -168,7 +171,6 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { static int link_enter_set_routes(Link *link) { Route *rt; - struct in_addr a; int r; assert(link); @@ -178,7 +180,7 @@ static int link_enter_set_routes(Link *link) { link->state = LINK_STATE_SETTING_ROUTES; if (!link->network->static_routes && !link->dhcp_lease && - (!link->ipv4ll || sd_ipv4ll_get_address(link->ipv4ll, &a) < 0)) + (!link->ipv4ll || ipv4ll_is_bound(link->ipv4ll) == false)) return link_enter_configured(link); log_debug_link(link, "setting routes"); @@ -345,7 +347,6 @@ static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { static int link_enter_set_addresses(Link *link) { Address *ad; - struct in_addr a; int r; assert(link); @@ -355,7 +356,7 @@ static int link_enter_set_addresses(Link *link) { link->state = LINK_STATE_SETTING_ADDRESSES; if (!link->network->static_addresses && !link->dhcp_lease && - (!link->ipv4ll || sd_ipv4ll_get_address(link->ipv4ll, &a) < 0)) + (!link->ipv4ll || ipv4ll_is_bound(link->ipv4ll) == false)) return link_enter_set_routes(link); log_debug_link(link, "setting addresses"); @@ -456,6 +457,28 @@ static int link_enter_set_addresses(Link *link) { return 0; } +static int address_update_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { + Link *link = userdata; + int r; + + assert(m); + assert(link); + assert(link->ifname); + + if (link->state == LINK_STATE_FAILED) + return 1; + + r = sd_rtnl_message_get_errno(m); + if (r < 0 && r != -ENOENT) + log_struct_link(LOG_WARNING, link, + "MESSAGE=%s: could not update address: %s", + link->ifname, strerror(-r), + "ERRNO=%d", -r, + NULL); + + return 0; +} + static int address_drop_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) { Link *link = userdata; int r; @@ -753,7 +776,7 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { Link *link = userdata; - int r; + int r = 0; assert(link); assert(link->network); @@ -792,7 +815,10 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { } if (event == DHCP_EVENT_EXPIRED && link->network->ipv4ll) { - r = sd_ipv4ll_start (link->ipv4ll); + if (!sd_ipv4ll_is_running(link->ipv4ll)) + r = sd_ipv4ll_start(link->ipv4ll); + else if (ipv4ll_is_bound(link->ipv4ll)) + r = ipv4ll_address_update(link, false); if (r < 0) { link_enter_failed(link); return; @@ -807,7 +833,10 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { return; } if (link->ipv4ll) { - r = sd_ipv4ll_stop(link->ipv4ll); + if (ipv4ll_is_bound(link->ipv4ll)) + r = ipv4ll_address_update(link, true); + else + r = sd_ipv4ll_stop(link->ipv4ll); if (r < 0) { link_enter_failed(link); return; @@ -825,11 +854,44 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) { return; } -static int ipv4ll_address_lost(sd_ipv4ll *ll, Link *link) { +static int ipv4ll_address_update(Link *link, bool deprecate) { + int r; + struct in_addr addr; + + assert(link); + + r = sd_ipv4ll_get_address(link->ipv4ll, &addr); + if (r >= 0) { + _cleanup_address_free_ Address *address = NULL; + + log_debug_link(link, "IPv4 link-local %s %u.%u.%u.%u", + deprecate ? "deprecate" : "approve", + ADDRESS_FMT_VAL(addr)); + + r = address_new_dynamic(&address); + if (r < 0) { + log_error_link(link, "Could not allocate address: %s", strerror(-r)); + return r; + } + + address->family = AF_INET; + address->in_addr.in = addr; + address->prefixlen = 16; + address->scope = RT_SCOPE_LINK; + address->cinfo.ifa_prefered = deprecate ? 0 : CACHE_INFO_INFINITY_LIFE_TIME; + address->broadcast.s_addr = address->in_addr.in.s_addr | htonl(0xfffffffflu >> address->prefixlen); + + address_update(address, link, &address_update_handler); + } + + return 0; + +} + +static int ipv4ll_address_lost(Link *link) { int r; struct in_addr addr; - assert(ll); assert(link); r = sd_ipv4ll_get_address(link->ipv4ll, &addr); @@ -870,6 +932,18 @@ static int ipv4ll_address_lost(sd_ipv4ll *ll, Link *link) { return 0; } +static bool ipv4ll_is_bound(sd_ipv4ll *ll) { + int r; + struct in_addr addr; + + assert(ll); + + r = sd_ipv4ll_get_address(ll, &addr); + if (r < 0) + return false; + return true; +} + static int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) { struct in_addr address; int r; @@ -903,7 +977,7 @@ static void ipv4ll_handler(sd_ipv4ll *ll, int event, void *userdata){ switch(event) { case IPV4LL_EVENT_STOP: case IPV4LL_EVENT_CONFLICT: - r = ipv4ll_address_lost(ll, link); + r = ipv4ll_address_lost(link); if (r < 0) { link_enter_failed(link); return; -- cgit v1.2.3-54-g00ecf