chiark / gitweb /
networkd: rewind rtnl message between passing it to functions
[elogind.git] / src / network / networkd-link.c
index fdc351fed31a8a03327e893ec58a569a2f0cd364..973755a3e3f8e7caee31198c93c20456cd49641d 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"
 
 int link_new(Manager *manager, struct udev_device *device, Link **ret) {
         _cleanup_link_free_ Link *link = NULL;
-        const char *mac;
-        struct ether_addr *mac_addr;
         const char *ifname;
         int r;
 
@@ -53,17 +51,10 @@ int link_new(Manager *manager, struct udev_device *device, Link **ret) {
         if (link->ifindex <= 0)
                 return -EINVAL;
 
-        r = asprintf(&link->state_file, "/run/systemd/network/links/%u",
-                     (unsigned) link->ifindex);
+        r = asprintf(&link->state_file, "/run/systemd/network/links/%"PRIu64,
+                     link->ifindex);
         if (r < 0)
-                return r;
-
-        mac = udev_device_get_sysattr_value(device, "address");
-        if (mac) {
-                mac_addr = ether_aton(mac);
-                if (mac_addr)
-                        memcpy(&link->mac, mac_addr, sizeof(struct ether_addr));
-        }
+                return -ENOMEM;
 
         ifname = udev_device_get_sysname(device);
         link->ifname = strdup(ifname);
@@ -72,6 +63,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;
 
@@ -94,6 +87,8 @@ void link_free(Link *link) {
         free(link->ifname);
         free(link->state_file);
 
+        udev_device_unref(link->udev_device);
+
         free(link);
 }
 
@@ -237,6 +232,7 @@ static int link_enter_set_routes(Link *link) {
 
         if (link->dhcp_lease) {
                 _cleanup_route_free_ Route *route = NULL;
+                _cleanup_route_free_ Route *route_gw = NULL;
                 struct in_addr gateway;
 
                 r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
@@ -253,6 +249,30 @@ static int link_enter_set_routes(Link *link) {
                         return r;
                 }
 
+                r = route_new_dynamic(&route_gw);
+                if (r < 0) {
+                        log_error_link(link, "Could not allocate route: %s",
+                                       strerror(-r));
+                        return r;
+                }
+
+                /* The dhcp netmask may mask out the gateway. Add an explicit
+                 * route for the gw host so that we can route no matter the
+                 * netmask or existing kernel route tables. */
+                route_gw->family = AF_INET;
+                route_gw->dst_addr.in = gateway;
+                route_gw->dst_prefixlen = 32;
+                route_gw->scope = RT_SCOPE_LINK;
+
+                r = route_configure(route_gw, link, &route_handler);
+                if (r < 0) {
+                        log_warning_link(link,
+                                         "could not set host route: %s", strerror(-r));
+                        return r;
+                }
+
+                link->route_messages ++;
+
                 route->family = AF_INET;
                 route->in_addr.in = gateway;
 
@@ -892,27 +912,7 @@ static int link_acquire_conf(Link *link) {
         assert(link->manager->event);
 
         if (link->network->ipv4ll) {
-                if (!link->ipv4ll) {
-                        r = sd_ipv4ll_new(&link->ipv4ll);
-                        if (r < 0)
-                                return r;
-
-                        r = sd_ipv4ll_attach_event(link->ipv4ll, NULL, 0);
-                        if (r < 0)
-                                return r;
-
-                        r = sd_ipv4ll_set_index(link->ipv4ll, link->ifindex);
-                        if (r < 0)
-                                return r;
-
-                        r = sd_ipv4ll_set_mac(link->ipv4ll, &link->mac);
-                        if (r < 0)
-                                return r;
-
-                        r = sd_ipv4ll_set_callback(link->ipv4ll, ipv4ll_handler, link);
-                        if (r < 0)
-                                return r;
-                }
+                assert(link->ipv4ll);
 
                 log_debug_link(link, "acquiring IPv4 link-local address");
 
@@ -922,33 +922,7 @@ static int link_acquire_conf(Link *link) {
         }
 
         if (link->network->dhcp) {
-                if (!link->dhcp_client) {
-                        r = sd_dhcp_client_new(&link->dhcp_client);
-                        if (r < 0)
-                                return r;
-
-                        r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
-                        if (r < 0)
-                                return r;
-
-                        r = sd_dhcp_client_set_index(link->dhcp_client, link->ifindex);
-                        if (r < 0)
-                                return r;
-
-                        r = sd_dhcp_client_set_mac(link->dhcp_client, &link->mac);
-                        if (r < 0)
-                                return r;
-
-                        r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp_handler, link);
-                        if (r < 0)
-                                return r;
-
-                        if (link->network->dhcp_mtu) {
-                                r = sd_dhcp_client_set_request_option(link->dhcp_client, 26);
-                                if (r < 0)
-                                        return r;
-                        }
-                }
+                assert(link->dhcp_client);
 
                 log_debug_link(link, "acquiring DHCPv4 lease");
 
@@ -972,6 +946,9 @@ static int link_update_flags(Link *link, unsigned flags) {
         if (link->flags == flags)
                 return 0;
 
+        log_debug_link(link, "link status updated: %#.8x -> %#.8x",
+                       link->flags, flags);
+
         if ((link->flags & IFF_UP) != (flags & IFF_UP))
                 log_info_link(link,
                               "link is %s", flags & IFF_UP ? "up": "down");
@@ -1011,9 +988,6 @@ static int link_update_flags(Link *link, unsigned flags) {
                 }
         }
 
-        log_debug_link(link, "link status updated: %#.8x -> %#.8x",
-                       link->flags, flags);
-
         link->flags = flags;
 
         return 0;
@@ -1312,6 +1286,56 @@ int link_add(Manager *m, struct udev_device *device, Link **ret) {
         if (r < 0)
                 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;
+
+                r = sd_ipv4ll_set_index(link->ipv4ll, link->ifindex);
+                if (r < 0)
+                        return r;
+
+                r = sd_ipv4ll_set_callback(link->ipv4ll, ipv4ll_handler, link);
+                if (r < 0)
+                        return r;
+        }
+
+        if (link->network->dhcp) {
+                r = sd_dhcp_client_new(&link->dhcp_client);
+                if (r < 0)
+                        return r;
+
+                r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
+                if (r < 0)
+                        return r;
+
+                r = sd_dhcp_client_set_index(link->dhcp_client, link->ifindex);
+                if (r < 0)
+                        return r;
+
+                r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp_handler, link);
+                if (r < 0)
+                        return r;
+
+                if (link->network->dhcp_mtu) {
+                        r = sd_dhcp_client_set_request_option(link->dhcp_client, 26);
+                        if (r < 0)
+                                return r;
+                }
+        }
+
         r = link_configure(link);
         if (r < 0)
                 return r;
@@ -1321,6 +1345,7 @@ int link_add(Manager *m, struct udev_device *device, Link **ret) {
 
 int link_update(Link *link, sd_rtnl_message *m) {
         unsigned flags;
+        struct ether_addr mac;
         int r;
 
         assert(link);
@@ -1338,16 +1363,43 @@ int link_update(Link *link, sd_rtnl_message *m) {
                                        PRIu16, link->original_mtu);
         }
 
-        r = sd_rtnl_message_read_ether_addr(m, IFLA_ADDRESS, &link->mac);
-        if (r >= 0) {
-                log_debug_link(link, "MAC address: "
-                               "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
-                                link->mac.ether_addr_octet[0],
-                                link->mac.ether_addr_octet[1],
-                                link->mac.ether_addr_octet[2],
-                                link->mac.ether_addr_octet[3],
-                                link->mac.ether_addr_octet[4],
-                                link->mac.ether_addr_octet[5]);
+        r = sd_rtnl_message_read_ether_addr(m, IFLA_ADDRESS, &mac);
+        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);
+
+                        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->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;
+                                }
+                        }
+                }
         }
 
         r = sd_rtnl_message_link_get_flags(m, &flags);
@@ -1367,10 +1419,6 @@ int link_save(Link *link) {
         assert(link);
         assert(link->state_file);
 
-        r = mkdir_safe_label("/run/systemd/network/links", 0755, 0, 0);
-        if (r < 0)
-                goto finish;
-
         r = fopen_temporary(link->state_file, &f, &temp_path);
         if (r < 0)
                 goto finish;
@@ -1383,7 +1431,12 @@ int link_save(Link *link) {
                 link_state_to_string(link->state));
 
         if (link->dhcp_lease) {
-                const char *lease_file = "/run/systemd/network/leases/test.lease";
+                _cleanup_free_ char *lease_file = NULL;
+
+                r = asprintf(&lease_file, "/run/systemd/network/leases/%"PRIu64,
+                             link->ifindex);
+                if (r < 0)
+                        return -ENOMEM;
 
                 r = dhcp_lease_save(link->dhcp_lease, lease_file);
                 if (r < 0)