chiark / gitweb /
sd-dhcp-lease: expose load/save functions
[elogind.git] / src / network / networkd-link.c
index b5f5863e6b60ac79bbfe64fef635994aa0442605..fcfbd3e12373f95a948780d60543f8698d12427f 100644 (file)
 
 #include "dhcp-lease-internal.h"
 
+static bool link_dhcp6_enabled(Link *link) {
+        if (link->flags & IFF_LOOPBACK)
+                return false;
+
+        if (!link->network)
+                return false;
+
+        return IN_SET(link->network->dhcp, DHCP_SUPPORT_V6, DHCP_SUPPORT_BOTH);
+}
+
+static bool link_dhcp4_enabled(Link *link) {
+        if (link->flags & IFF_LOOPBACK)
+                return false;
+
+        if (!link->network)
+                return false;
+
+        return IN_SET(link->network->dhcp, DHCP_SUPPORT_V4, DHCP_SUPPORT_BOTH);
+}
+
+static bool link_dhcp4_server_enabled(Link *link) {
+        if (link->flags & IFF_LOOPBACK)
+                return false;
+
+        if (!link->network)
+                return false;
+
+        return link->network->dhcp_server;
+}
+
+static bool link_ipv4ll_enabled(Link *link) {
+        if (link->flags & IFF_LOOPBACK)
+                return false;
+
+        if (!link->network)
+                return false;
+
+        return link->network->ipv4ll;
+}
+
+#define FLAG_STRING(string, flag, old, new) \
+        (((old ^ new) & flag) \
+                ? ((old & flag) ? (" -" string) : (" +" string)) \
+                : "")
+
+static int link_update_flags(Link *link, sd_rtnl_message *m) {
+        unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags;
+        uint8_t operstate;
+        int r;
+
+        assert(link);
+
+        r = sd_rtnl_message_link_get_flags(m, &flags);
+        if (r < 0) {
+                log_warning_link(link, "Could not get link flags");
+                return r;
+        }
+
+        r = sd_rtnl_message_read_u8(m, IFLA_OPERSTATE, &operstate);
+        if (r < 0)
+                /* if we got a message without operstate, take it to mean
+                   the state was unchanged */
+                operstate = link->kernel_operstate;
+
+        if ((link->flags == flags) && (link->kernel_operstate == operstate))
+                return 0;
+
+        if (link->flags != flags) {
+                log_debug_link(link, "flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
+                               FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
+                               FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
+                               FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
+                               FLAG_STRING("UP", IFF_UP, link->flags, flags),
+                               FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
+                               FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
+                               FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
+                               FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
+                               FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
+                               FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
+                               FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
+                               FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
+                               FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
+                               FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
+                               FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
+                               FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
+                               FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
+                               FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
+                               FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
+
+                unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
+                                  IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
+                                  IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
+                                  IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
+                                  IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
+                                  IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
+                unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
+                unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
+
+                /* link flags are currently at most 18 bits, let's align to
+                 * printing 20 */
+                if (unknown_flags_added)
+                        log_debug_link(link,
+                                       "unknown link flags gained: %#.5x (ignoring)",
+                                       unknown_flags_added);
+
+                if (unknown_flags_removed)
+                        log_debug_link(link,
+                                       "unknown link flags lost: %#.5x (ignoring)",
+                                       unknown_flags_removed);
+        }
+
+        link->flags = flags;
+        link->kernel_operstate = operstate;
+
+        link_save(link);
+
+        return 0;
+}
+
 static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
         _cleanup_link_unref_ Link *link = NULL;
         uint16_t type;
@@ -87,7 +206,7 @@ static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
         if (r < 0)
                 return -ENOMEM;
 
-        r = hashmap_ensure_allocated(&manager->links, NULL, NULL);
+        r = hashmap_ensure_allocated(&manager->links, NULL);
         if (r < 0)
                 return r;
 
@@ -95,6 +214,10 @@ static int link_new(Manager *manager, sd_rtnl_message *message, Link **ret) {
         if (r < 0)
                 return r;
 
+        r = link_update_flags(link, message);
+        if (r < 0)
+                return r;
+
         *ret = link;
         link = NULL;
 
@@ -221,15 +344,6 @@ static int link_stop_clients(Link *link) {
                 }
         }
 
-        if (link->dhcp_server) {
-                k = sd_dhcp_server_stop(link->dhcp_server);
-                if (k < 0) {
-                        log_warning_link(link, "Could not stop DHCPv4 server: %s",
-                                         strerror(-r));
-                        r = k;
-                }
-        }
-
         if(link->icmp6_router_discovery) {
 
                 if (link->dhcp6_client) {
@@ -304,7 +418,7 @@ static int link_enter_configured(Link *link) {
         assert(link->network);
         assert(link->state == LINK_STATE_SETTING_ROUTES);
 
-        if (link->network->dhcp_server &&
+        if (link_dhcp4_server_enabled(link) &&
             !sd_dhcp_server_is_running(link->dhcp_server)) {
                 struct in_addr pool_start;
                 Address *address;
@@ -371,13 +485,12 @@ void link_client_handler(Link *link) {
         if (!link->static_configured)
                 return;
 
-        if (link->network->ipv4ll)
+        if (link_ipv4ll_enabled(link))
                 if (!link->ipv4ll_address ||
                     !link->ipv4ll_route)
                         return;
 
-        if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V4))
-                if (!link->dhcp4_configured)
+        if (link_dhcp4_enabled(link) && !link->dhcp4_configured)
                         return;
 
         if (link->state != LINK_STATE_CONFIGURED)
@@ -603,8 +716,6 @@ static int set_hostname_handler(sd_bus *bus, sd_bus_message *m, void *userdata,
                 return 1;
 
         r = sd_bus_message_get_errno(m);
-        if (r < 0)
-                r = -r;
         if (r > 0)
                 log_warning_link(link, "Could not set hostname: %s",
                                  strerror(r));
@@ -789,7 +900,9 @@ static void icmp6_router_handler(sd_icmp6_nd *nd, int event, void *userdata) {
                 return;
         }
 
-        r = sd_dhcp6_client_set_mac(link->dhcp6_client, &link->mac);
+        r = sd_dhcp6_client_set_mac(link->dhcp6_client,
+                                    (const uint8_t *) &link->mac,
+                                    sizeof (link->mac), ARPHRD_ETHER);
         if (r < 0) {
                 link->dhcp6_client = sd_dhcp6_client_unref(link->dhcp6_client);
                 return;
@@ -821,7 +934,7 @@ static int link_acquire_conf(Link *link) {
         assert(link->manager);
         assert(link->manager->event);
 
-        if (link->network->ipv4ll) {
+        if (link_ipv4ll_enabled(link)) {
                 assert(link->ipv4ll);
 
                 log_debug_link(link, "acquiring IPv4 link-local address");
@@ -834,7 +947,7 @@ static int link_acquire_conf(Link *link) {
                 }
         }
 
-        if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V4)) {
+        if (link_dhcp4_enabled(link)) {
                 assert(link->dhcp_client);
 
                 log_debug_link(link, "acquiring DHCPv4 lease");
@@ -847,7 +960,7 @@ static int link_acquire_conf(Link *link) {
                 }
         }
 
-        if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V6)) {
+        if (link_dhcp6_enabled(link)) {
                 assert(link->icmp6_router_discovery);
 
                 log_debug_link(link, "discovering IPv6 routers");
@@ -863,129 +976,20 @@ static int link_acquire_conf(Link *link) {
         return 0;
 }
 
-bool link_has_carrier(unsigned flags, uint8_t operstate) {
+bool link_has_carrier(Link *link) {
         /* see Documentation/networking/operstates.txt in the kernel sources */
 
-        if (operstate == IF_OPER_UP)
+        if (link->kernel_operstate == IF_OPER_UP)
                 return true;
 
-        if (operstate == IF_OPER_UNKNOWN)
+        if (link->kernel_operstate == IF_OPER_UNKNOWN)
                 /* operstate may not be implemented, so fall back to flags */
-                if ((flags & IFF_LOWER_UP) && !(flags & IFF_DORMANT))
+                if ((link->flags & IFF_LOWER_UP) && !(link->flags & IFF_DORMANT))
                         return true;
 
         return false;
 }
 
-#define FLAG_STRING(string, flag, old, new) \
-        (((old ^ new) & flag) \
-                ? ((old & flag) ? (" -" string) : (" +" string)) \
-                : "")
-
-static int link_update_flags(Link *link, sd_rtnl_message *m) {
-        unsigned flags, unknown_flags_added, unknown_flags_removed, unknown_flags;
-        uint8_t operstate;
-        bool carrier_gained = false, carrier_lost = false;
-        int r;
-
-        assert(link);
-
-        r = sd_rtnl_message_link_get_flags(m, &flags);
-        if (r < 0) {
-                log_warning_link(link, "Could not get link flags");
-                return r;
-        }
-
-        r = sd_rtnl_message_read_u8(m, IFLA_OPERSTATE, &operstate);
-        if (r < 0)
-                /* if we got a message without operstate, take it to mean
-                   the state was unchanged */
-                operstate = link->kernel_operstate;
-
-        if ((link->flags == flags) && (link->kernel_operstate == operstate))
-                return 0;
-
-        if (link->flags != flags) {
-                log_debug_link(link, "flags change:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
-                               FLAG_STRING("LOOPBACK", IFF_LOOPBACK, link->flags, flags),
-                               FLAG_STRING("MASTER", IFF_MASTER, link->flags, flags),
-                               FLAG_STRING("SLAVE", IFF_SLAVE, link->flags, flags),
-                               FLAG_STRING("UP", IFF_UP, link->flags, flags),
-                               FLAG_STRING("DORMANT", IFF_DORMANT, link->flags, flags),
-                               FLAG_STRING("LOWER_UP", IFF_LOWER_UP, link->flags, flags),
-                               FLAG_STRING("RUNNING", IFF_RUNNING, link->flags, flags),
-                               FLAG_STRING("MULTICAST", IFF_MULTICAST, link->flags, flags),
-                               FLAG_STRING("BROADCAST", IFF_BROADCAST, link->flags, flags),
-                               FLAG_STRING("POINTOPOINT", IFF_POINTOPOINT, link->flags, flags),
-                               FLAG_STRING("PROMISC", IFF_PROMISC, link->flags, flags),
-                               FLAG_STRING("ALLMULTI", IFF_ALLMULTI, link->flags, flags),
-                               FLAG_STRING("PORTSEL", IFF_PORTSEL, link->flags, flags),
-                               FLAG_STRING("AUTOMEDIA", IFF_AUTOMEDIA, link->flags, flags),
-                               FLAG_STRING("DYNAMIC", IFF_DYNAMIC, link->flags, flags),
-                               FLAG_STRING("NOARP", IFF_NOARP, link->flags, flags),
-                               FLAG_STRING("NOTRAILERS", IFF_NOTRAILERS, link->flags, flags),
-                               FLAG_STRING("DEBUG", IFF_DEBUG, link->flags, flags),
-                               FLAG_STRING("ECHO", IFF_ECHO, link->flags, flags));
-
-                unknown_flags = ~(IFF_LOOPBACK | IFF_MASTER | IFF_SLAVE | IFF_UP |
-                                  IFF_DORMANT | IFF_LOWER_UP | IFF_RUNNING |
-                                  IFF_MULTICAST | IFF_BROADCAST | IFF_POINTOPOINT |
-                                  IFF_PROMISC | IFF_ALLMULTI | IFF_PORTSEL |
-                                  IFF_AUTOMEDIA | IFF_DYNAMIC | IFF_NOARP |
-                                  IFF_NOTRAILERS | IFF_DEBUG | IFF_ECHO);
-                unknown_flags_added = ((link->flags ^ flags) & flags & unknown_flags);
-                unknown_flags_removed = ((link->flags ^ flags) & link->flags & unknown_flags);
-
-                /* link flags are currently at most 18 bits, let's align to
-                 * printing 20 */
-                if (unknown_flags_added)
-                        log_debug_link(link,
-                                       "unknown link flags gained: %#.5x (ignoring)",
-                                       unknown_flags_added);
-
-                if (unknown_flags_removed)
-                        log_debug_link(link,
-                                       "unknown link flags lost: %#.5x (ignoring)",
-                                       unknown_flags_removed);
-        }
-
-        carrier_gained = !link_has_carrier(link->flags, link->kernel_operstate) &&
-                       link_has_carrier(flags, operstate);
-        carrier_lost = link_has_carrier(link->flags, link->kernel_operstate) &&
-                         !link_has_carrier(flags, operstate);
-
-        link->flags = flags;
-        link->kernel_operstate = operstate;
-
-        link_save(link);
-
-        if (link->state == LINK_STATE_FAILED ||
-            link->state == LINK_STATE_UNMANAGED)
-                return 0;
-
-        if (carrier_gained) {
-                log_info_link(link, "gained carrier");
-
-                if (link->network) {
-                        r = link_acquire_conf(link);
-                        if (r < 0) {
-                                link_enter_failed(link);
-                                return r;
-                        }
-                }
-        } else if (carrier_lost) {
-                log_info_link(link, "lost carrier");
-
-                r = link_stop_clients(link);
-                if (r < 0) {
-                        link_enter_failed(link);
-                        return r;
-                }
-        }
-
-        return 0;
-}
-
 static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         _cleanup_link_unref_ Link *link = userdata;
         int r;
@@ -1196,19 +1200,19 @@ static int link_configure(Link *link) {
         assert(link->network);
         assert(link->state == LINK_STATE_PENDING);
 
-        if (link->network->ipv4ll) {
+        if (link_ipv4ll_enabled(link)) {
                 r = ipv4ll_configure(link);
                 if (r < 0)
                         return r;
         }
 
-        if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V4)) {
+        if (link_dhcp4_enabled(link)) {
                 r = dhcp4_configure(link);
                 if (r < 0)
                         return r;
         }
 
-        if (link->network->dhcp_server) {
+        if (link_dhcp4_server_enabled(link)) {
                 r = sd_dhcp_server_new(&link->dhcp_server, link->ifindex);
                 if (r < 0)
                         return r;
@@ -1218,7 +1222,7 @@ static int link_configure(Link *link) {
                         return r;
         }
 
-        if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V6)) {
+        if (link_dhcp6_enabled(link)) {
                 r = sd_icmp6_nd_new(&link->icmp6_router_discovery);
                 if (r < 0)
                         return r;
@@ -1244,7 +1248,7 @@ static int link_configure(Link *link) {
                         return r;
         }
 
-        if (link_has_carrier(link->flags, link->kernel_operstate)) {
+        if (link_has_carrier(link)) {
                 r = link_acquire_conf(link);
                 if (r < 0)
                         return r;
@@ -1276,6 +1280,17 @@ static int link_initialized_and_synced(sd_rtnl *rtnl, sd_rtnl_message *m,
         } else if (r < 0)
                 return r;
 
+        if (link->flags & IFF_LOOPBACK) {
+                if (network->ipv4ll)
+                        log_debug_link(link, "ignoring IPv4LL for loopback link");
+
+                if (network->dhcp != DHCP_SUPPORT_NONE)
+                        log_debug_link(link, "ignoring DHCP clients for loopback link");
+
+                if (network->dhcp_server)
+                        log_debug_link(link, "ignoring DHCP server for loopback link");
+        }
+
         r = network_apply(link->manager, network, link);
         if (r < 0)
                 return r;
@@ -1356,7 +1371,7 @@ int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message,
         } else {
                 r = link_get(m, ifindex, &link);
                 if (r < 0 || !link) {
-                        log_warning("rtnl: received address for a nonexistent link, ignoring");
+                        log_warning("rtnl: received address for a nonexistent link (%d), ignoring", ifindex);
                         return 0;
                 }
         }
@@ -1386,6 +1401,13 @@ int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message,
                 return 0;
         }
 
+        r = sd_rtnl_message_addr_get_flags(message, &address->flags);
+        if (r < 0) {
+                log_warning_link(link,
+                                 "rtnl: received address with invalid flags, ignoring");
+                return 0;
+        }
+
         switch (address->family) {
         case AF_INET:
                 r = sd_rtnl_message_read_in_addr(message, IFA_LOCAL,
@@ -1446,12 +1468,10 @@ int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message,
         case RTM_NEWADDR:
                 if (!address_dropped)
                         log_debug_link(link, "added address: %s/%u (valid for %s)",
-                                       buf, address->prefixlen,
-                                       strna(valid_str));
+                                       buf, address->prefixlen, valid_str);
                 else
                         log_debug_link(link, "updated address: %s/%u (valid for %s)",
-                                       buf, address->prefixlen,
-                                       strna(valid_str));
+                                       buf, address->prefixlen, valid_str);
 
                 LIST_PREPEND(addresses, link->addresses, address);
                 address = NULL;
@@ -1462,15 +1482,13 @@ int link_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message,
         case RTM_DELADDR:
                 if (address_dropped) {
                         log_debug_link(link, "removed address: %s/%u (valid for %s)",
-                                       buf, address->prefixlen,
-                                       strna(valid_str));
+                                       buf, address->prefixlen, valid_str);
 
                         link_save(link);
                 } else
                         log_warning_link(link,
                                          "removing non-existent address: %s/%u (valid for %s)",
-                                         buf, address->prefixlen,
-                                         strna(valid_str));
+                                         buf, address->prefixlen, valid_str);
 
                 break;
         default:
@@ -1547,6 +1565,7 @@ int link_update(Link *link, sd_rtnl_message *m) {
         struct ether_addr mac;
         const char *ifname;
         uint32_t mtu;
+        bool had_carrier, carrier_gained, carrier_lost;
         int r;
 
         assert(link);
@@ -1621,7 +1640,9 @@ int link_update(Link *link, sd_rtnl_message *m) {
 
                         if (link->dhcp_client) {
                                 r = sd_dhcp_client_set_mac(link->dhcp_client,
-                                                           &link->mac);
+                                                           (const uint8_t *) &link->mac,
+                                                           sizeof (link->mac),
+                                                           ARPHRD_ETHER);
                                 if (r < 0) {
                                         log_warning_link(link,
                                                          "Could not update MAC address in DHCP client: %s",
@@ -1632,7 +1653,9 @@ int link_update(Link *link, sd_rtnl_message *m) {
 
                         if (link->dhcp6_client) {
                                 r = sd_dhcp6_client_set_mac(link->dhcp6_client,
-                                                            &link->mac);
+                                                            (const uint8_t *) &link->mac,
+                                                            sizeof (link->mac),
+                                                            ARPHRD_ETHER);
                                 if (r < 0) {
                                         log_warning_link(link,
                                                          "Could not update MAC address in DHCPv6 client: %s",
@@ -1643,7 +1666,36 @@ int link_update(Link *link, sd_rtnl_message *m) {
                 }
         }
 
-        return link_update_flags(link, m);
+        had_carrier = link_has_carrier(link);
+
+        r = link_update_flags(link, m);
+        if (r < 0)
+                return r;
+
+        carrier_gained = !had_carrier && link_has_carrier(link);
+        carrier_lost = had_carrier && !link_has_carrier(link);
+
+        if (carrier_gained) {
+                log_info_link(link, "gained carrier");
+
+                if (link->network) {
+                        r = link_acquire_conf(link);
+                        if (r < 0) {
+                                link_enter_failed(link);
+                                return r;
+                        }
+                }
+        } else if (carrier_lost) {
+                log_info_link(link, "lost carrier");
+
+                r = link_stop_clients(link);
+                if (r < 0) {
+                        link_enter_failed(link);
+                        return r;
+                }
+        }
+
+        return 0;
 }
 
 static void link_update_operstate(Link *link) {
@@ -1652,12 +1704,15 @@ static void link_update_operstate(Link *link) {
 
         if (link->kernel_operstate == IF_OPER_DORMANT)
                 link->operstate = LINK_OPERSTATE_DORMANT;
-        else if (link_has_carrier(link->flags, link->kernel_operstate)) {
+        else if (link_has_carrier(link)) {
                 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->flags & (IFA_F_TENTATIVE | IFA_F_DEPRECATED))
+                                continue;
+
                         if (address->scope < scope)
                                 scope = address->scope;
                 }
@@ -1721,6 +1776,8 @@ int link_save(Link *link) {
                 char **address, **domain;
                 bool space;
 
+                fprintf(f, "NETWORK_FILE=%s\n", link->network->filename);
+
                 fputs("DNS=", f);
                 space = false;
                 STRV_FOREACH(address, link->network->dns) {
@@ -1800,7 +1857,7 @@ int link_save(Link *link) {
         if (link->dhcp_lease) {
                 assert(link->network);
 
-                r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
+                r = sd_dhcp_lease_save(link->dhcp_lease, link->lease_file);
                 if (r < 0)
                         goto fail;