]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/network/networkd-dhcp4.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / network / networkd-dhcp4.c
index 31dbea94205ef9059998686bbd7d942ee10ec618..c8eca6654582390d3b199bc14a403c54009d9acf 100644 (file)
@@ -6,7 +6,6 @@
 #include "alloc-util.h"
 #include "hostname-util.h"
 #include "parse-util.h"
-#include "netdev/vrf.h"
 #include "network-internal.h"
 #include "networkd-link.h"
 #include "networkd-manager.h"
@@ -52,7 +51,8 @@ static int route_scope_from_address(const Route *route, const struct in_addr *se
 static int link_set_dhcp_routes(Link *link) {
         _cleanup_free_ sd_dhcp_route **static_routes = NULL;
         bool classless_route = false, static_route = false;
-        struct in_addr gateway, address;
+        const struct in_addr *router;
+        struct in_addr address;
         int r, n, i;
         uint32_t table;
 
@@ -67,11 +67,7 @@ static int link_set_dhcp_routes(Link *link) {
         if (!link->network->dhcp_use_routes)
                 return 0;
 
-        /* When the interface is part of an VRF use the VRFs routing table, unless
-         * there is a another table specified. */
-        table = link->network->dhcp_route_table;
-        if (!link->network->dhcp_route_table_set && link->network->vrf != NULL)
-                table = VRF(link->network->vrf)->table;
+        table = link_get_dhcp_route_table(link);
 
         r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
         if (r < 0)
@@ -123,26 +119,21 @@ static int link_set_dhcp_routes(Link *link) {
                 link->dhcp4_messages++;
         }
 
-        r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
-        if (r == -ENODATA)
-                log_link_info_errno(link, r, "DHCP: No gateway received from DHCP server: %m");
+        r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
+        if (IN_SET(r, 0, -ENODATA))
+                log_link_info(link, "DHCP: No gateway received from DHCP server.");
         else if (r < 0)
                 log_link_warning_errno(link, r, "DHCP error: could not get gateway: %m");
+        else if (in4_addr_is_null(&router[0]))
+                log_link_info(link, "DHCP: Received gateway is null.");
 
         /* According to RFC 3442: If the DHCP server returns both a Classless Static Routes option and
            a Router option, the DHCP client MUST ignore the Router option. */
         if (classless_route && static_route)
                 log_link_warning(link, "Classless static routes received from DHCP server: ignoring static-route option and router option");
 
-        if (r >= 0 && !classless_route) {
-                _cleanup_(route_freep) Route *route = NULL;
-                _cleanup_(route_freep) Route *route_gw = NULL;
-
-                r = route_new(&route);
-                if (r < 0)
-                        return log_link_error_errno(link, r, "Could not allocate route: %m");
-
-                route->protocol = RTPROT_DHCP;
+        if (r > 0 && !classless_route && !in4_addr_is_null(&router[0])) {
+                _cleanup_(route_freep) Route *route = NULL, *route_gw = NULL;
 
                 r = route_new(&route_gw);
                 if (r < 0)
@@ -152,7 +143,7 @@ static int link_set_dhcp_routes(Link *link) {
                  * 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.in = gateway;
+                route_gw->dst.in = router[0];
                 route_gw->dst_prefixlen = 32;
                 route_gw->prefsrc.in = address;
                 route_gw->scope = RT_SCOPE_LINK;
@@ -166,9 +157,14 @@ static int link_set_dhcp_routes(Link *link) {
 
                 link->dhcp4_messages++;
 
+                r = route_new(&route);
+                if (r < 0)
+                        return log_link_error_errno(link, r, "Could not allocate route: %m");
+
                 route->family = AF_INET;
-                route->gw.in = gateway;
+                route->gw.in = router[0];
                 route->prefsrc.in = address;
+                route->protocol = RTPROT_DHCP;
                 route->priority = link->network->dhcp_route_metric;
                 route->table = table;
 
@@ -187,9 +183,9 @@ static int link_set_dhcp_routes(Link *link) {
 
 static int dhcp_lease_lost(Link *link) {
         _cleanup_(address_freep) Address *address = NULL;
+        const struct in_addr *router;
         struct in_addr addr;
         struct in_addr netmask;
-        struct in_addr gateway;
         unsigned prefixlen = 0;
         int r;
 
@@ -222,15 +218,15 @@ static int dhcp_lease_lost(Link *link) {
 
         r = address_new(&address);
         if (r >= 0) {
-                r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
-                if (r >= 0) {
+                r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
+                if (r > 0 && !in4_addr_is_null(&router[0])) {
                         _cleanup_(route_freep) Route *route_gw = NULL;
                         _cleanup_(route_freep) Route *route = NULL;
 
                         r = route_new(&route_gw);
                         if (r >= 0) {
                                 route_gw->family = AF_INET;
-                                route_gw->dst.in = gateway;
+                                route_gw->dst.in = router[0];
                                 route_gw->dst_prefixlen = 32;
                                 route_gw->scope = RT_SCOPE_LINK;
 
@@ -240,7 +236,7 @@ static int dhcp_lease_lost(Link *link) {
                         r = route_new(&route);
                         if (r >= 0) {
                                 route->family = AF_INET;
-                                route->gw.in = gateway;
+                                route->gw.in = router[0];
 
                                 route_remove(route, link, NULL);
                         }
@@ -399,10 +395,10 @@ static int dhcp_lease_renew(sd_dhcp_client *client, Link *link) {
 }
 
 static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
+        const struct in_addr *router;
         sd_dhcp_lease *lease;
         struct in_addr address;
         struct in_addr netmask;
-        struct in_addr gateway;
         unsigned prefixlen;
         uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME;
         int r;
@@ -424,20 +420,20 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
 
         prefixlen = in4_addr_netmask_to_prefixlen(&netmask);
 
-        r = sd_dhcp_lease_get_router(lease, &gateway);
+        r = sd_dhcp_lease_get_router(lease, &router);
         if (r < 0 && r != -ENODATA)
                 return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m");
 
-        if (r >= 0)
+        if (r > 0 && !in4_addr_is_null(&router[0]))
                 log_struct(LOG_INFO,
                            LOG_LINK_INTERFACE(link),
                            LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u",
                                             ADDRESS_FMT_VAL(address),
                                             prefixlen,
-                                            ADDRESS_FMT_VAL(gateway)),
+                                            ADDRESS_FMT_VAL(router[0])),
                            "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
                            "PREFIXLEN=%u", prefixlen,
-                           "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(gateway));
+                           "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router[0]));
         else
                 log_struct(LOG_INFO,
                            LOG_LINK_INTERFACE(link),
@@ -670,10 +666,12 @@ int dhcp4_set_client_identifier(Link *link) {
 
                 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
                         r = sd_dhcp_client_set_iaid_duid_llt(link->dhcp_client,
+                                                             link->network->iaid_set,
                                                              link->network->iaid,
                                                              duid->llt_time);
                 else
                         r = sd_dhcp_client_set_iaid_duid(link->dhcp_client,
+                                                         link->network->iaid_set,
                                                          link->network->iaid,
                                                          duid->type,
                                                          duid->raw_data_len > 0 ? duid->raw_data : NULL,
@@ -688,7 +686,7 @@ int dhcp4_set_client_identifier(Link *link) {
 
                 if (duid->type == DUID_TYPE_LLT && duid->raw_data_len == 0)
                         r = sd_dhcp_client_set_duid_llt(link->dhcp_client,
-                                                             duid->llt_time);
+                                                        duid->llt_time);
                 else
                         r = sd_dhcp_client_set_duid(link->dhcp_client,
                                                     duid->type,