chiark / gitweb /
networkd: rewind rtnl message between passing it to functions
[elogind.git] / src / network / networkd-link.c
index a7ba46692ca369a4baa27b0b031fdf86a68247dc..973755a3e3f8e7caee31198c93c20456cd49641d 100644 (file)
@@ -26,7 +26,7 @@
 #include "libudev-private.h"
 #include "util.h"
 #include "bus-util.h"
-#include "net-util.h"
+#include "network-internal.h"
 
 #include "dhcp-lease-internal.h"
 
@@ -63,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;
 
@@ -85,6 +87,8 @@ void link_free(Link *link) {
         free(link->ifname);
         free(link->state_file);
 
+        udev_device_unref(link->udev_device);
+
         free(link);
 }
 
@@ -228,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);
@@ -244,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;
 
@@ -1258,10 +1287,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;
@@ -1327,36 +1364,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;
+                                }
                         }
                 }
         }
@@ -1390,7 +1431,7 @@ int link_save(Link *link) {
                 link_state_to_string(link->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);