chiark / gitweb /
networkd: set static addresses immediately
[elogind.git] / src / network / networkd-link.c
index 55274893644ee66208f262772472f88a59b43219..4c75fa3d38acef0e9641a63b827d69b5babc9d11 100644 (file)
@@ -29,6 +29,7 @@
 #include "virt.h"
 #include "bus-util.h"
 #include "network-internal.h"
+#include "conf-parser.h"
 
 #include "network-util.h"
 #include "dhcp-lease-internal.h"
@@ -205,7 +206,7 @@ static int link_stop_clients(Link *link) {
         if (!link->network)
                 return 0;
 
-        if (link->network->dhcp) {
+        if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V6)) {
                 assert(link->dhcp_client);
 
                 k = sd_dhcp_client_stop(link->dhcp_client);
@@ -235,7 +236,7 @@ static int link_stop_clients(Link *link) {
                 }
         }
 
-        if (link->network->dhcp6) {
+        if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V6)) {
                 assert(link->icmp6_router_discovery);
 
                 if (link->dhcp6_client) {
@@ -401,6 +402,51 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         return 1;
 }
 
+static int link_set_dhcp_routes(Link *link) {
+        struct sd_dhcp_route *static_routes;
+        size_t static_routes_size;
+        int r;
+        unsigned i;
+
+        assert(link);
+
+        r = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes, &static_routes_size);
+        if (r < 0) {
+                if (r != -ENOENT)
+                        log_warning_link(link, "DHCP error: %s", strerror(-r));
+                return r;
+        }
+
+        for (i = 0; i < static_routes_size; i++) {
+                _cleanup_route_free_ Route *route = NULL;
+
+                r = route_new_dynamic(&route);
+                if (r < 0) {
+                        log_error_link(link, "Could not allocate route: %s",
+                                       strerror(-r));
+                        return r;
+                }
+
+                route->family = AF_INET;
+                route->in_addr.in = static_routes[i].gw_addr;
+                route->dst_addr.in = static_routes[i].dst_addr;
+                route->dst_prefixlen = static_routes[i].dst_prefixlen;
+                route->metrics = DHCP_STATIC_ROUTE_METRIC;
+
+                r = route_configure(route, link, &route_handler);
+                if (r < 0) {
+                        log_warning_link(link,
+                                         "could not set host route: %s", strerror(-r));
+                        return r;
+                }
+
+                link_ref(link);
+                link->route_messages ++;
+        }
+
+        return 0;
+}
+
 static int link_enter_set_routes(Link *link) {
         Route *rt;
         int r;
@@ -524,6 +570,9 @@ static int link_enter_set_routes(Link *link) {
                         link_ref(link);
                         link->route_messages ++;
                 }
+
+                if (link->network->dhcp_routes)
+                        link_set_dhcp_routes(link);
         }
 
         if (link->route_messages == 0) {
@@ -600,6 +649,7 @@ static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
 static int link_enter_set_addresses(Link *link) {
         Address *ad;
         int r;
+        uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
 
         assert(link);
         assert(link->network);
@@ -676,6 +726,16 @@ static int link_enter_set_addresses(Link *link) {
                         return r;
                 }
 
+                if (!link->network->dhcp_critical) {
+                        r = sd_dhcp_lease_get_lifetime(link->dhcp_lease,
+                                                       &lifetime);
+                        if (r < 0) {
+                                log_warning_link(link, "DHCP error: no lifetime: %s",
+                                                 strerror(-r));
+                                return r;
+                        }
+                }
+
                 r = sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
                 if (r < 0) {
                         log_warning_link(link, "DHCP error: no netmask: %s",
@@ -694,6 +754,8 @@ static int link_enter_set_addresses(Link *link) {
 
                 address->family = AF_INET;
                 address->in_addr.in = addr;
+                address->cinfo.ifa_prefered = lifetime;
+                address->cinfo.ifa_valid = lifetime;
                 address->prefixlen = prefixlen;
                 address->broadcast.s_addr = addr.s_addr | ~netmask.s_addr;
 
@@ -887,12 +949,11 @@ static int link_set_mtu(Link *link, uint32_t mtu) {
 
 static int dhcp_lease_lost(Link *link) {
         _cleanup_address_free_ Address *address = NULL;
-        _cleanup_route_free_ Route *route_gw = NULL;
-        _cleanup_route_free_ Route *route = NULL;
         struct in_addr addr;
         struct in_addr netmask;
         struct in_addr gateway;
         unsigned prefixlen;
+        unsigned i;
         int r;
 
         assert(link);
@@ -900,10 +961,36 @@ static int dhcp_lease_lost(Link *link) {
 
         log_warning_link(link, "DHCP lease lost");
 
+        if (link->network->dhcp_routes) {
+                struct sd_dhcp_route *routes;
+                size_t routes_size;
+
+                r = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes, &routes_size);
+                if (r >= 0) {
+                        for (i = 0; i < routes_size; i++) {
+                                _cleanup_route_free_ Route *route = NULL;
+
+                                r = route_new_dynamic(&route);
+                                if (r >= 0) {
+                                        route->family = AF_INET;
+                                        route->in_addr.in = routes[i].gw_addr;
+                                        route->dst_addr.in = routes[i].dst_addr;
+                                        route->dst_prefixlen = routes[i].dst_prefixlen;
+
+                                        route_drop(route, link, &route_drop_handler);
+                                        link_ref(link);
+                                }
+                        }
+                }
+        }
+
         r = address_new_dynamic(&address);
         if (r >= 0) {
                 r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
                 if (r >= 0) {
+                        _cleanup_route_free_ Route *route_gw = NULL;
+                        _cleanup_route_free_ Route *route = NULL;
+
                         r = route_new_dynamic(&route_gw);
                         if (r >= 0) {
                                 route_gw->family = AF_INET;
@@ -967,6 +1054,25 @@ static int dhcp_lease_lost(Link *link) {
         return 0;
 }
 
+static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
+        sd_dhcp_lease *lease;
+        int r;
+
+        r = sd_dhcp_client_get_lease(client, &lease);
+        if (r < 0) {
+                log_warning_link(link, "DHCP error: no lease %s",
+                                 strerror(-r));
+                return r;
+        }
+
+        sd_dhcp_lease_unref(link->dhcp_lease);
+        link->dhcp_lease = lease;
+
+        link_enter_set_addresses(link);
+
+        return 0;
+}
+
 static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
         sd_dhcp_lease *lease;
         struct in_addr address;
@@ -1117,6 +1223,13 @@ static void dhcp_handler(sd_dhcp_client *client, int event, void *userdata) {
                                 }
                         }
 
+                        break;
+                case DHCP_EVENT_RENEW:
+                        r = dhcp_lease_renew(client, link);
+                        if (r < 0) {
+                                link_enter_failed(link);
+                                return;
+                        }
                         break;
                 case DHCP_EVENT_IP_ACQUIRE:
                         r = dhcp_lease_acquired(client, link);
@@ -1417,7 +1530,7 @@ static int link_acquire_conf(Link *link) {
                 }
         }
 
-        if (link->network->dhcp) {
+        if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V4)) {
                 assert(link->dhcp_client);
 
                 log_debug_link(link, "acquiring DHCPv4 lease");
@@ -1430,7 +1543,7 @@ static int link_acquire_conf(Link *link) {
                 }
         }
 
-        if (link->network->dhcp6) {
+        if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V6)) {
                 assert(link->icmp6_router_discovery);
 
                 log_debug_link(link, "discovering IPv6 routers");
@@ -1643,10 +1756,7 @@ static int link_enslaved(Link *link) {
                 }
         }
 
-        if (!link->network->dhcp && !link->network->ipv4ll)
-                return link_enter_set_addresses(link);
-
-        return 0;
+        return link_enter_set_addresses(link);
 }
 
 static int enslave_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
@@ -1886,7 +1996,7 @@ static int link_configure(Link *link) {
                         return r;
         }
 
-        if (link->network->dhcp) {
+        if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V4)) {
                 r = sd_dhcp_client_new(&link->dhcp_client);
                 if (r < 0)
                         return r;
@@ -1912,6 +2022,14 @@ static int link_configure(Link *link) {
                         if (r < 0)
                                 return r;
                 }
+                if (link->network->dhcp_routes) {
+                        r = sd_dhcp_client_set_request_option(link->dhcp_client, DHCP_OPTION_STATIC_ROUTE);
+                        if (r < 0)
+                                return r;
+                        r = sd_dhcp_client_set_request_option(link->dhcp_client, DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
+                        if (r < 0)
+                                return r;
+                }
         }
 
         if (link->network->dhcp_server) {
@@ -1924,7 +2042,7 @@ static int link_configure(Link *link) {
                         return r;
         }
 
-        if (link->network->dhcp6) {
+        if (IN_SET(link->network->dhcp, DHCP_SUPPORT_BOTH, DHCP_SUPPORT_V6)) {
                 r = sd_icmp6_nd_new(&link->icmp6_router_discovery);
                 if (r < 0)
                         return r;
@@ -2457,3 +2575,55 @@ static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = {
 };
 
 DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState);
+
+static const char* const dhcp_support_table[_DHCP_SUPPORT_MAX] = {
+        [DHCP_SUPPORT_NONE] = "none",
+        [DHCP_SUPPORT_BOTH] = "both",
+        [DHCP_SUPPORT_V4] = "v4",
+        [DHCP_SUPPORT_V6] = "v6",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(dhcp_support, DHCPSupport);
+
+int config_parse_dhcp(
+                const char* unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
+                unsigned section_line,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
+
+        DHCPSupport *dhcp = data;
+        int k;
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        /* Our enum shall be a superset of booleans, hence first try
+         * to parse as boolean, and then as enum */
+
+        k = parse_boolean(rvalue);
+        if (k > 0)
+                *dhcp = DHCP_SUPPORT_BOTH;
+        else if (k == 0)
+                *dhcp = DHCP_SUPPORT_NONE;
+        else {
+                DHCPSupport s;
+
+                s = dhcp_support_from_string(rvalue);
+                if (s < 0){
+                        log_syntax(unit, LOG_ERR, filename, line, -s, "Failed to parse DHCP option, ignoring: %s", rvalue);
+                        return 0;
+                }
+
+                *dhcp = s;
+        }
+
+        return 0;
+}