chiark / gitweb /
networkd: link - only reset transient hostname if it was set by us
[elogind.git] / src / network / networkd-link.c
index 867baa6dee78a8f2adf3b19a039726e28f42b957..aa23bfbc995900bce786fa951528184d5f93c696 100644 (file)
@@ -85,22 +85,33 @@ void link_free(Link *link) {
         free(link);
 }
 
+int link_get(Manager *m, int ifindex, Link **ret) {
+        Link *link;
+        uint64_t ifindex_64;
+
+        assert(m);
+        assert(m->links);
+        assert(ifindex);
+        assert(ret);
+
+        ifindex_64 = ifindex;
+        link = hashmap_get(m->links, &ifindex_64);
+        if (!link)
+                return -ENODEV;
+
+        *ret = link;
+
+        return 0;
+}
+
 int link_add(Manager *m, struct udev_device *device, Link **ret) {
         Link *link;
         Network *network;
         int r;
-        uint64_t ifindex;
 
         assert(m);
         assert(device);
 
-        ifindex = udev_device_get_ifindex(device);
-        link = hashmap_get(m->links, &ifindex);
-        if (link) {
-                *ret = link;
-                return -EEXIST;
-        }
-
         r = link_new(m, device, &link);
         if (r < 0)
                 return r;
@@ -426,7 +437,7 @@ static int set_mtu_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
 }
 
 static int link_set_mtu(Link *link, uint32_t mtu) {
-        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
+        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
         int r;
 
         assert(link);
@@ -435,7 +446,8 @@ static int link_set_mtu(Link *link, uint32_t mtu) {
 
         log_debug_link(link, "setting MTU: %" PRIu32, mtu);
 
-        r = sd_rtnl_message_link_new(RTM_SETLINK, link->ifindex, &req);
+        r = sd_rtnl_message_new_link(link->manager->rtnl, RTM_SETLINK,
+                                     link->ifindex, &req);
         if (r < 0) {
                 log_error_link(link, "Could not allocate RTM_SETLINK message");
                 return r;
@@ -495,9 +507,14 @@ static int dhcp_lease_lost(Link *link) {
         }
 
         if (link->network->dhcp_hostname) {
-                r = set_hostname(link->manager->bus, "");
-                if (r < 0)
-                        log_error("Failed to reset transient hostname");
+                const char *hostname = NULL;
+
+                r = sd_dhcp_lease_get_hostname(link->dhcp_lease, &hostname);
+                if (r >= 0 && hostname) {
+                        r = set_hostname(link->manager->bus, "");
+                        if (r < 0)
+                                log_error("Failed to reset transient hostname");
+                }
         }
 
         link->dhcp_lease = sd_dhcp_lease_unref(link->dhcp_lease);
@@ -562,6 +579,8 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
                          ADDRESS_FMT_VAL(gateway),
                          NULL);
 
+        link->dhcp_lease = lease;
+
         if (link->network->dhcp_dns) {
                 r = sd_dhcp_lease_get_dns(lease, &nameservers, &nameservers_size);
                 if (r >= 0) {
@@ -595,8 +614,6 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
                 }
         }
 
-        link->dhcp_lease = lease;
-
         link_enter_set_addresses(link);
 
         return 0;
@@ -626,10 +643,12 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
                                 return;
                         }
 
-                        r = dhcp_lease_lost(link);
-                        if (r < 0) {
-                                link_enter_failed(link);
-                                return;
+                        if (link->dhcp_lease) {
+                                r = dhcp_lease_lost(link);
+                                if (r < 0) {
+                                        link_enter_failed(link);
+                                        return;
+                                }
                         }
 
                         if (event == DHCP_EVENT_IP_CHANGE) {
@@ -783,7 +802,7 @@ static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
 }
 
 static int link_up(Link *link) {
-        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
+        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
         int r;
 
         assert(link);
@@ -792,7 +811,8 @@ static int link_up(Link *link) {
 
         log_debug_link(link, "bringing link up");
 
-        r = sd_rtnl_message_link_new(RTM_SETLINK, link->ifindex, &req);
+        r = sd_rtnl_message_new_link(link->manager->rtnl, RTM_SETLINK,
+                                     link->ifindex, &req);
         if (r < 0) {
                 log_error_link(link, "Could not allocate RTM_SETLINK message");
                 return r;
@@ -866,6 +886,8 @@ static int enslave_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
 }
 
 static int link_enter_enslave(Link *link) {
+        NetDev *vlan;
+        Iterator i;
         int r;
 
         assert(link);
@@ -874,7 +896,8 @@ static int link_enter_enslave(Link *link) {
 
         link->state = LINK_STATE_ENSLAVING;
 
-        if (!link->network->bridge && !link->network->bond && !link->network->vlan)
+        if (!link->network->bridge && !link->network->bond &&
+            hashmap_isempty(link->network->vlans))
                 return link_enslaved(link);
 
         if (link->network->bridge) {
@@ -898,20 +921,17 @@ static int link_enter_enslave(Link *link) {
                 link->enslaving ++;
         }
 
-        if (link->network->vlan) {
+        HASHMAP_FOREACH(vlan, link->network->vlans, i) {
                 log_struct_link(LOG_DEBUG, link,
                                 "MESSAGE=%s: enslaving by '%s'",
-                                link->ifname, link->network->vlan->name,
-                                NETDEV(link->network->vlan),
-                                NULL);
+                                link->ifname, vlan->name, NETDEV(vlan), NULL);
 
-                r = netdev_enslave(link->network->vlan, link, &enslave_handler);
+                r = netdev_enslave(vlan, link, &enslave_handler);
                 if (r < 0) {
                         log_struct_link(LOG_WARNING, link,
                                         "MESSAGE=%s: could not enslave by '%s': %s",
-                                        link->ifname, link->network->vlan->name,
-                                        strerror(-r), NETDEV(link->network->vlan),
-                                        NULL);
+                                        link->ifname, vlan->name, strerror(-r),
+                                        NETDEV(vlan), NULL);
                         link_enter_failed(link);
                         return r;
                 }
@@ -922,7 +942,8 @@ static int link_enter_enslave(Link *link) {
         return 0;
 }
 
-static int link_get_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+static int link_getlink_handler(sd_rtnl *rtnl, sd_rtnl_message *m,
+                                void *userdata) {
         Link *link = userdata;
         int r;
 
@@ -949,8 +970,8 @@ static int link_get_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         return 1;
 }
 
-static int link_get(Link *link) {
-        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
+static int link_getlink(Link *link) {
+        _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
         int r;
 
         assert(link);
@@ -959,13 +980,15 @@ static int link_get(Link *link) {
 
         log_debug_link(link, "requesting link status");
 
-        r = sd_rtnl_message_link_new(RTM_GETLINK, link->ifindex, &req);
+        r = sd_rtnl_message_new_link(link->manager->rtnl, RTM_GETLINK,
+                                     link->ifindex, &req);
         if (r < 0) {
                 log_error_link(link, "Could not allocate RTM_GETLINK message");
                 return r;
         }
 
-        r = sd_rtnl_call_async(link->manager->rtnl, req, link_get_handler, link, 0, NULL);
+        r = sd_rtnl_call_async(link->manager->rtnl, req, link_getlink_handler,
+                               link, 0, NULL);
         if (r < 0) {
                 log_error_link(link,
                                "Could not send rtnetlink message: %s", strerror(-r));
@@ -982,7 +1005,7 @@ int link_configure(Link *link) {
         assert(link->network);
         assert(link->state == _LINK_STATE_INVALID);
 
-        r = link_get(link);
+        r = link_getlink(link);
         if (r < 0) {
                 link_enter_failed(link);
                 return r;