chiark / gitweb /
sd-dhcp-client: Add reference counting for DHCP
[elogind.git] / src / network / networkd-link.c
index 14cc8715ce844224ae2cd30b3c865504e5a4bdcf..2630b4625a751a870943650a9a779cd20b9b6600 100644 (file)
 #include "libudev-private.h"
 #include "util.h"
 #include "bus-util.h"
-#include "net-util.h"
+#include "network-internal.h"
 
 #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;
@@ -63,6 +66,8 @@ int link_new(Manager *manager, struct udev_device *device, Link **ret) {
         if (r < 0)
                 return r;
 
+        link->udev_device = udev_device_ref(device);
+
         *ret = link;
         link = NULL;
 
@@ -75,7 +80,7 @@ void link_free(Link *link) {
 
         assert(link->manager);
 
-        sd_dhcp_client_free(link->dhcp_client);
+        sd_dhcp_client_unref(link->dhcp_client);
         sd_dhcp_lease_unref(link->dhcp_lease);
 
         sd_ipv4ll_free(link->ipv4ll);
@@ -85,6 +90,8 @@ void link_free(Link *link) {
         free(link->ifname);
         free(link->state_file);
 
+        udev_device_unref(link->udev_device);
+
         free(link);
 }
 
@@ -164,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);
@@ -174,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");
@@ -341,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);
@@ -351,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");
@@ -452,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;
@@ -575,8 +602,11 @@ static int link_set_mtu(Link *link, uint32_t mtu) {
 
 static int dhcp_lease_lost(Link *link) {
         _cleanup_address_free_ Address *address = NULL;
+        _cleanup_route_free_ Route *route_gw = NULL;
+        _cleanup_route_free_ Route *route = NULL;
         struct in_addr addr;
         struct in_addr netmask;
+        struct in_addr gateway;
         unsigned prefixlen;
         int r;
 
@@ -589,8 +619,27 @@ static int dhcp_lease_lost(Link *link) {
         if (r >= 0) {
                 sd_dhcp_lease_get_address(link->dhcp_lease, &addr);
                 sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
+                sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
                 prefixlen = net_netmask_to_prefixlen(&netmask);
 
+                r = route_new_dynamic(&route_gw);
+                if (r >= 0) {
+                        route_gw->family = AF_INET;
+                        route_gw->dst_addr.in = gateway;
+                        route_gw->dst_prefixlen = 32;
+                        route_gw->scope = RT_SCOPE_LINK;
+
+                        route_drop(route_gw, link, &route_drop_handler);
+                }
+
+                r = route_new_dynamic(&route);
+                if (r >= 0) {
+                        route->family = AF_INET;
+                        route->in_addr.in = gateway;
+
+                        route_drop(route, link, &route_drop_handler);
+                }
+
                 address->family = AF_INET;
                 address->in_addr.in = addr;
                 address->prefixlen = prefixlen;
@@ -727,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);
@@ -766,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;
@@ -781,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;
@@ -799,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);
@@ -844,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;
@@ -877,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;
@@ -1199,6 +1299,7 @@ static int link_getlink_handler(sd_rtnl *rtnl, sd_rtnl_message *m,
         int r;
 
         assert(link);
+        assert(link->ifname);
 
         if (link->state == LINK_STATE_FAILED)
                 return 1;
@@ -1283,10 +1384,18 @@ int link_add(Manager *m, struct udev_device *device, Link **ret) {
                 return r;
 
         if (link->network->ipv4ll) {
+                uint8_t seed[8];
                 r = sd_ipv4ll_new(&link->ipv4ll);
                 if (r < 0)
                         return r;
 
+                r = net_get_unique_predictable_data(link->udev_device, seed);
+                if (r >= 0) {
+                        r = sd_ipv4ll_set_address_seed(link->ipv4ll, seed);
+                        if (r < 0)
+                                return r;
+                }
+
                 r = sd_ipv4ll_attach_event(link->ipv4ll, NULL, 0);
                 if (r < 0)
                         return r;
@@ -1352,36 +1461,40 @@ int link_update(Link *link, sd_rtnl_message *m) {
         }
 
         r = sd_rtnl_message_read_ether_addr(m, IFLA_ADDRESS, &mac);
-        if (r >= 0 && memcmp(&link->mac.ether_addr_octet, &mac.ether_addr_octet, ETH_ALEN)) {
+        if (r < 0)
+                log_debug_link(link, "Could not get MAC address: %s", strerror(-r));
+        else {
+                if (memcmp(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN)) {
 
-                memcpy(&link->mac.ether_addr_octet, &mac.ether_addr_octet, ETH_ALEN);
+                        memcpy(link->mac.ether_addr_octet, mac.ether_addr_octet, ETH_ALEN);
 
-                log_debug_link(link, "MAC address: "
-                               "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
-                                mac.ether_addr_octet[0],
-                                mac.ether_addr_octet[1],
-                                mac.ether_addr_octet[2],
-                                mac.ether_addr_octet[3],
-                                mac.ether_addr_octet[4],
-                                mac.ether_addr_octet[5]);
+                        log_debug_link(link, "MAC address: "
+                                       "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
+                                       mac.ether_addr_octet[0],
+                                       mac.ether_addr_octet[1],
+                                       mac.ether_addr_octet[2],
+                                       mac.ether_addr_octet[3],
+                                       mac.ether_addr_octet[4],
+                                       mac.ether_addr_octet[5]);
 
-                if (link->ipv4ll) {
-                        r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
-                        if (r < 0) {
-                                log_warning_link(link, "Could not update MAC "
-                                                 "address in IPv4LL client: %s",
-                                                 strerror(-r));
-                                return r;
+                        if (link->ipv4ll) {
+                                r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
+                                if (r < 0) {
+                                        log_warning_link(link, "Could not update MAC "
+                                                         "address in IPv4LL client: %s",
+                                                         strerror(-r));
+                                        return r;
+                                }
                         }
-                }
 
-                if (link->dhcp_client) {
-                        r = sd_dhcp_client_set_mac(link->dhcp_client, &link->mac);
-                        if (r < 0) {
-                                log_warning_link(link, "Could not update MAC "
-                                                 "address in DHCP client: %s",
-                                                 strerror(-r));
-                                return r;
+                        if (link->dhcp_client) {
+                                r = sd_dhcp_client_set_mac(link->dhcp_client, &link->mac);
+                                if (r < 0) {
+                                        log_warning_link(link, "Could not update MAC "
+                                                         "address in DHCP client: %s",
+                                                         strerror(-r));
+                                        return r;
+                                }
                         }
                 }
         }
@@ -1398,11 +1511,16 @@ int link_update(Link *link, sd_rtnl_message *m) {
 int link_save(Link *link) {
         _cleanup_free_ char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
+        const char *state;
         int r;
 
         assert(link);
         assert(link->state_file);
 
+        state = link_state_to_string(link->state);
+        if (!state)
+                goto finish;
+
         r = fopen_temporary(link->state_file, &f, &temp_path);
         if (r < 0)
                 goto finish;
@@ -1411,11 +1529,10 @@ int link_save(Link *link) {
 
         fprintf(f,
                 "# This is private data. Do not parse.\n"
-                "STATE=%s\n",
-                link_state_to_string(link->state));
+                "STATE=%s\n", state);
 
         if (link->dhcp_lease) {
-                char *lease_file;
+                _cleanup_free_ char *lease_file = NULL;
 
                 r = asprintf(&lease_file, "/run/systemd/network/leases/%"PRIu64,
                              link->ifindex);