chiark / gitweb /
networkd: split runtime config dir from state dir
[elogind.git] / src / network / networkd-link.c
index 34b8d616bb1cde9557805574097ef9f76ee97515..6e6fb8393e97ffca32d5b67418439e678c8c7bcd 100644 (file)
@@ -75,12 +75,12 @@ static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
         if (!link->ifname)
                 return -ENOMEM;
 
-        r = asprintf(&link->state_file, "/run/systemd/network/links/%"PRIu64,
+        r = asprintf(&link->state_file, "/run/systemd/netif/links/%"PRIu64,
                      link->ifindex);
         if (r < 0)
                 return -ENOMEM;
 
-        r = asprintf(&link->lease_file, "/run/systemd/network/leases/%"PRIu64,
+        r = asprintf(&link->lease_file, "/run/systemd/netif/leases/%"PRIu64,
                      link->ifindex);
         if (r < 0)
                 return -ENOMEM;
@@ -96,11 +96,18 @@ static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
 }
 
 static void link_free(Link *link) {
+        Address *address;
+
         if (!link)
                 return;
 
         assert(link->manager);
 
+        while ((address = link->addresses)) {
+                LIST_REMOVE(addresses, link->addresses, address);
+                address_free(address);
+        }
+
         sd_dhcp_client_unref(link->dhcp_client);
         sd_dhcp_lease_unref(link->dhcp_lease);
 
@@ -847,8 +854,6 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
         struct in_addr netmask;
         struct in_addr gateway;
         unsigned prefixlen;
-        struct in_addr *nameservers;
-        size_t nameservers_size;
         int r;
 
         assert(client);
@@ -913,15 +918,6 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
 
         link->dhcp_lease = lease;
 
-        if (link->network->dhcp_dns) {
-                r = sd_dhcp_lease_get_dns(lease, &nameservers, &nameservers_size);
-                if (r >= 0) {
-                        r = manager_update_resolv_conf(link->manager);
-                        if (r < 0)
-                                log_error_link(link, "Failed to update resolv.conf");
-                }
-        }
-
         if (link->network->dhcp_mtu) {
                 uint16_t mtu;
 
@@ -1257,9 +1253,9 @@ static int link_update_flags(Link *link, sd_rtnl_message *m) {
         if (r < 0)
                 /* if we got a message without operstate, take it to mean
                    the state was unchanged */
-                operstate = link->operstate;
+                operstate = link->kernel_operstate;
 
-        if ((link->flags == flags) && (link->operstate == operstate))
+        if ((link->flags == flags) && (link->kernel_operstate == operstate))
                 return 0;
 
         if (link->flags != flags) {
@@ -1303,13 +1299,13 @@ static int link_update_flags(Link *link, sd_rtnl_message *m) {
                                        unknown_flags_removed);
         }
 
-        carrier_gained = !link_has_carrier(link->flags, link->operstate) &&
+        carrier_gained = !link_has_carrier(link->flags, link->kernel_operstate) &&
                        link_has_carrier(flags, operstate);
-        carrier_lost = link_has_carrier(link->flags, link->operstate) &&
+        carrier_lost = link_has_carrier(link->flags, link->kernel_operstate) &&
                          !link_has_carrier(flags, operstate);
 
         link->flags = flags;
-        link->operstate = operstate;
+        link->kernel_operstate = operstate;
 
         link_save(link);
 
@@ -1667,7 +1663,7 @@ static int link_configure(Link *link) {
                 }
         }
 
-        if (link_has_carrier(link->flags, link->operstate)) {
+        if (link_has_carrier(link->flags, link->kernel_operstate)) {
                 r = link_acquire_conf(link);
                 if (r < 0)
                         return r;
@@ -1690,7 +1686,7 @@ int link_initialized(Link *link, struct udev_device *device) {
         if (device)
                 link->udev_device = udev_device_ref(device);
 
-        log_debug_link(link, "link initialized");
+        log_debug_link(link, "udev initialized link");
 
         r = network_get(link->manager, device, link->ifname, &link->mac, &network);
         if (r == -ENOENT) {
@@ -1715,7 +1711,9 @@ int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *use
         Link *link = NULL;
         uint16_t type;
         _cleanup_address_free_ Address *address = NULL;
+        Address *ad;
         char buf[INET6_ADDRSTRLEN];
+        bool address_dropped = false;
         int r, ifindex;
 
         assert(rtnl);
@@ -1752,7 +1750,13 @@ int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *use
 
         r = sd_rtnl_message_addr_get_prefixlen(message, &address->prefixlen);
         if (r < 0) {
-                log_warning_link(link, "rtnl: recevied address with invalid prefixlen, ignoring");
+                log_warning_link(link, "rtnl: received address with invalid prefixlen, ignoring");
+                return 0;
+        }
+
+        r = sd_rtnl_message_addr_get_scope(message, &address->scope);
+        if (r < 0) {
+                log_warning_link(link, "rtnl: received address with invalid scope, ignoring");
                 return 0;
         }
 
@@ -1784,15 +1788,38 @@ int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *use
                 return 0;
         }
 
+        LIST_FOREACH(addresses, ad, link->addresses) {
+                if (address_equal(ad, address)) {
+                        LIST_REMOVE(addresses, link->addresses, ad);
+
+                        address_free(ad);
+
+                        address_dropped = true;
+
+                        break;
+                }
+        }
+
         switch (type) {
         case RTM_NEWADDR:
-                log_info_link(link, "added address: %s/%u", buf,
-                              address->prefixlen);
-                break;
+                if (!address_dropped)
+                        log_debug_link(link, "added address: %s/%u", buf,
+                                      address->prefixlen);
 
+                LIST_PREPEND(addresses, link->addresses, address);
+                address = NULL;
+
+                link_save(link);
+
+                break;
         case RTM_DELADDR:
-                log_info_link(link, "removed address: %s/%u", buf,
-                              address->prefixlen);
+                if (address_dropped) {
+                        log_debug_link(link, "removed address: %s/%u", buf,
+                                      address->prefixlen);
+
+                        link_save(link);
+                }
+
                 break;
         default:
                 assert_not_reached("Received invalid RTNL message type");
@@ -1861,9 +1888,11 @@ int link_add(Manager *m, sd_rtnl_message *message, Link **ret) {
                         return -errno;
                 }
 
-                if (udev_device_get_is_initialized(device) <= 0)
+                if (udev_device_get_is_initialized(device) <= 0) {
                         /* not yet ready */
+                        log_debug_link(link, "udev initializing link...");
                         return 0;
+                }
         }
 
         r = link_initialized(link, device);
@@ -1947,10 +1976,60 @@ int link_update(Link *link, sd_rtnl_message *m) {
         return link_update_flags(link, m);
 }
 
+static void serialize_addresses(FILE *f, const char *key, Address *address) {
+        Address *ad;
+
+        assert(f);
+        assert(key);
+
+        if (!address)
+                return;
+
+        fprintf(f, "%s=", key);
+
+        LIST_FOREACH(addresses, ad, address) {
+                char buf[INET6_ADDRSTRLEN];
+
+                if (inet_ntop(ad->family, &ad->in_addr, buf, INET6_ADDRSTRLEN))
+                        fprintf(f, "%s%s", buf, (ad->addresses_next) ? " ": "");
+        }
+
+        fputs("\n", f);
+}
+
+static void link_update_operstate(Link *link) {
+
+        assert(link);
+
+        if (link->kernel_operstate == IF_OPER_DORMANT)
+                link->operstate = LINK_OPERSTATE_DORMANT;
+        else if (link_has_carrier(link->flags, link->kernel_operstate)) {
+                Address *address;
+                uint8_t scope = RT_SCOPE_NOWHERE;
+
+                /* if we have carrier, check what addresses we have */
+                LIST_FOREACH(addresses, address, link->addresses) {
+                        if (address->scope < scope)
+                                scope = address->scope;
+                }
+
+                if (scope < RT_SCOPE_SITE)
+                        /* universally accessible addresses found */
+                        link->operstate = LINK_OPERSTATE_ROUTABLE;
+                else if (scope < RT_SCOPE_HOST)
+                        /* only link or site local addresses found */
+                        link->operstate = LINK_OPERSTATE_DEGRADED;
+                else
+                        /* no useful addresses found */
+                        link->operstate = LINK_OPERSTATE_CARRIER;
+        } else
+                link->operstate = LINK_OPERSTATE_UNKNOWN;
+}
+
 int link_save(Link *link) {
         _cleanup_free_ char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
-        const char *admin_state, *oper_state = "unknown";
+        const char *admin_state, *oper_state;
         int r;
 
         assert(link);
@@ -1958,6 +2037,8 @@ int link_save(Link *link) {
         assert(link->lease_file);
         assert(link->manager);
 
+        link_update_operstate(link);
+
         r = manager_save(link->manager);
         if (r < 0)
                 return r;
@@ -1970,10 +2051,8 @@ int link_save(Link *link) {
         admin_state = link_state_to_string(link->state);
         assert(admin_state);
 
-        if (link->operstate == IF_OPER_DORMANT)
-                oper_state = "dormant";
-        else if (link_has_carrier(link->flags, link->operstate))
-                oper_state = "carrier";
+        oper_state = link_operstate_to_string(link->operstate);
+        assert(oper_state);
 
         r = fopen_temporary(link->state_file, &f, &temp_path);
         if (r < 0)
@@ -1988,12 +2067,23 @@ int link_save(Link *link) {
                 "FLAGS=%u\n",
                 admin_state, oper_state, link->flags);
 
+        if (link->network) {
+                serialize_addresses(f, "DNS", link->network->dns);
+                serialize_addresses(f, "NTP", link->network->ntp);
+        }
+
         if (link->dhcp_lease) {
                 r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
                 if (r < 0)
                         goto finish;
 
-                fprintf(f, "DHCP_LEASE=%s\n", link->lease_file);
+                fprintf(f,
+                        "DHCP_LEASE=%s\n"
+                        "DHCP_USE_DNS=%s\n"
+                        "DHCP_USE_NTP=%s\n",
+                        link->lease_file,
+                        yes_no(link->network->dhcp_dns),
+                        yes_no(link->network->dhcp_ntp));
         } else
                 unlink(link->lease_file);
 
@@ -2024,3 +2114,13 @@ static const char* const link_state_table[_LINK_STATE_MAX] = {
 };
 
 DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
+
+static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = {
+        [LINK_OPERSTATE_UNKNOWN] = "unknown",
+        [LINK_OPERSTATE_DORMANT] = "dormant",
+        [LINK_OPERSTATE_CARRIER] = "carrier",
+        [LINK_OPERSTATE_DEGRADED] = "degraded",
+        [LINK_OPERSTATE_ROUTABLE] = "routable",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState);