[systemd-devel] [PATCH] networkd: dont configure route if lease doesn't have one

Umut Tezduyar Lindskog umut.tezduyar at axis.com
Tue Apr 29 13:40:38 PDT 2014


---
 src/libsystemd-network/sd-dhcp-lease.c |   34 ++++---
 src/network/networkd-link.c            |  160 ++++++++++++++++++--------------
 2 files changed, 110 insertions(+), 84 deletions(-)

diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c
index aa479ff..cb708c6 100644
--- a/src/libsystemd-network/sd-dhcp-lease.c
+++ b/src/libsystemd-network/sd-dhcp-lease.c
@@ -112,7 +112,10 @@ int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, struct in_addr *addr) {
         assert_return(lease, -EINVAL);
         assert_return(addr, -EINVAL);
 
-        addr->s_addr = lease->router;
+        if (lease->router != INADDR_ANY)
+                addr->s_addr = lease->router;
+        else
+                return -ENOENT;
 
         return 0;
 }
@@ -277,6 +280,7 @@ int dhcp_lease_new(sd_dhcp_lease **ret) {
         if (!lease)
                 return -ENOMEM;
 
+        lease->router = INADDR_ANY;
         lease->n_ref = REFCNT_INIT;
 
         *ret = lease;
@@ -316,17 +320,19 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) {
                 "ADDRESS=%s\n", string);
 
         r = sd_dhcp_lease_get_router(lease, &address);
-        if (r < 0)
+        if (r < 0 && r != -ENOENT)
                 goto finish;
 
-        string = inet_ntop(AF_INET, &address, buf, INET_ADDRSTRLEN);
-        if (!string) {
-                r = -errno;
-                goto finish;
-        }
+        if (r >= 0) {
+                string = inet_ntop(AF_INET, &address, buf, INET_ADDRSTRLEN);
+                if (!string) {
+                        r = -errno;
+                        goto finish;
+                }
 
-        fprintf(f,
-                "ROUTER=%s\n", string);
+                fprintf(f,
+                        "ROUTER=%s\n", string);
+        }
 
         r = sd_dhcp_lease_get_netmask(lease, &address);
         if (r < 0)
@@ -440,11 +446,13 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) {
 
         lease->address = addr.s_addr;
 
-        r = inet_pton(AF_INET, router, &addr);
-        if (r < 0)
-                return r;
+        if (router) {
+                r = inet_pton(AF_INET, router, &addr);
+                if (r < 0)
+                        return r;
 
-        lease->router = addr.s_addr;
+                lease->router = addr.s_addr;
+        }
 
         r = inet_pton(AF_INET, netmask, &addr);
         if (r < 0)
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 015a82d..8af376d 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -301,55 +301,60 @@ static int link_enter_set_routes(Link *link) {
                 struct in_addr gateway;
 
                 r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
-                if (r < 0) {
-                        log_warning_link(link, "DHCP error: no router: %s",
-                                         strerror(-r));
+                if (r < 0 && r != -ENOENT) {
+                        log_warning_link(link, "DHCP error: %s", strerror(-r));
                         return r;
                 }
 
-                r = route_new_dynamic(&route);
-                if (r < 0) {
-                        log_error_link(link, "Could not allocate route: %s",
-                                       strerror(-r));
-                        return r;
-                }
+                if (r >= 0) {
+                        r = route_new_dynamic(&route);
+                        if (r < 0) {
+                                log_error_link(link, "Could not allocate route: %s",
+                                               strerror(-r));
+                                return r;
+                        }
 
-                r = route_new_dynamic(&route_gw);
-                if (r < 0) {
-                        log_error_link(link, "Could not allocate route: %s",
-                                       strerror(-r));
-                        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;
+                        /* 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;
-                }
+                        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 ++;
+                        link->route_messages ++;
 
-                route->family = AF_INET;
-                route->in_addr.in = gateway;
+                        route->family = AF_INET;
+                        route->in_addr.in = gateway;
 
-                r = route_configure(route, link, &route_handler);
-                if (r < 0) {
-                        log_warning_link(link,
-                                         "could not set routes: %s", strerror(-r));
-                        link_enter_failed(link);
-                        return r;
+                        r = route_configure(route, link, &route_handler);
+                        if (r < 0) {
+                                log_warning_link(link,
+                                                 "could not set routes: %s", strerror(-r));
+                                link_enter_failed(link);
+                                return r;
+                        }
+
+                        link->route_messages ++;
                 }
+        }
 
-                link->route_messages ++;
+        if (link->route_messages == 0) {
+                link_enter_configured(link);
         }
 
         return 0;
@@ -680,29 +685,31 @@ static int dhcp_lease_lost(Link *link) {
 
         r = address_new_dynamic(&address);
         if (r >= 0) {
-                sd_dhcp_lease_get_address(link->dhcp_lease, &addr);
-                sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
-                sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
-                prefixlen = net_netmask_to_prefixlen(&netmask);
-
-                r = route_new_dynamic(&route_gw);
+                r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
                 if (r >= 0) {
-                        route_gw->family = AF_INET;
-                        route_gw->dst_addr.in = gateway;
-                        route_gw->dst_prefixlen = 32;
-                        route_gw->scope = RT_SCOPE_LINK;
+                        r = route_new_dynamic(&route_gw);
+                        if (r >= 0) {
+                                route_gw->family = AF_INET;
+                                route_gw->dst_addr.in = gateway;
+                                route_gw->dst_prefixlen = 32;
+                                route_gw->scope = RT_SCOPE_LINK;
 
-                        route_drop(route_gw, link, &route_drop_handler);
-                }
+                                route_drop(route_gw, link, &route_drop_handler);
+                        }
 
-                r = route_new_dynamic(&route);
-                if (r >= 0) {
-                        route->family = AF_INET;
-                        route->in_addr.in = gateway;
+                        r = route_new_dynamic(&route);
+                        if (r >= 0) {
+                                route->family = AF_INET;
+                                route->in_addr.in = gateway;
 
-                        route_drop(route, link, &route_drop_handler);
+                                route_drop(route, link, &route_drop_handler);
+                        }
                 }
 
+                sd_dhcp_lease_get_address(link->dhcp_lease, &addr);
+                sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask);
+                prefixlen = net_netmask_to_prefixlen(&netmask);
+
                 address->family = AF_INET;
                 address->in_addr.in = addr;
                 address->prefixlen = prefixlen;
@@ -777,25 +784,36 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
         prefixlen = net_netmask_to_prefixlen(&netmask);
 
         r = sd_dhcp_lease_get_router(lease, &gateway);
-        if (r < 0) {
-                log_warning_link(link, "DHCP error: no router: %s",
-                                 strerror(-r));
+        if (r < 0 && r != -ENOENT) {
+                log_warning_link(link, "DHCP error: %s", strerror(-r));
                 return r;
         }
 
-        log_struct_link(LOG_INFO, link,
-                        "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
-                         link->ifname,
-                         ADDRESS_FMT_VAL(address),
-                         prefixlen,
-                         ADDRESS_FMT_VAL(gateway),
-                         "ADDRESS=%u.%u.%u.%u",
-                         ADDRESS_FMT_VAL(address),
-                         "PREFIXLEN=%u",
-                         prefixlen,
-                         "GATEWAY=%u.%u.%u.%u",
-                         ADDRESS_FMT_VAL(gateway),
-                         NULL);
+        if (r >= 0)
+                log_struct_link(LOG_INFO, link,
+                                "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
+                                 link->ifname,
+                                 ADDRESS_FMT_VAL(address),
+                                 prefixlen,
+                                 ADDRESS_FMT_VAL(gateway),
+                                 "ADDRESS=%u.%u.%u.%u",
+                                 ADDRESS_FMT_VAL(address),
+                                 "PREFIXLEN=%u",
+                                 prefixlen,
+                                 "GATEWAY=%u.%u.%u.%u",
+                                 ADDRESS_FMT_VAL(gateway),
+                                 NULL);
+        else
+                log_struct_link(LOG_INFO, link,
+                                "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u",
+                                 link->ifname,
+                                 ADDRESS_FMT_VAL(address),
+                                 prefixlen,
+                                 "ADDRESS=%u.%u.%u.%u",
+                                 ADDRESS_FMT_VAL(address),
+                                 "PREFIXLEN=%u",
+                                 prefixlen,
+                                 NULL);
 
         link->dhcp_lease = lease;
 
-- 
1.7.10.4



More information about the systemd-devel mailing list