]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/network/networkd-dhcp4.c
network: add more log messages in configuring DHCP4 client
[thirdparty/systemd.git] / src / network / networkd-dhcp4.c
index ecb96cdb57afb348402ac6600137e410b0d37ce6..5616046f4f62c350ae8bfa567981906291aab2d6 100644 (file)
@@ -1,22 +1,4 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
-/***
-  This file is part of systemd.
-
-  Copyright 2013-2014 Tom Gundersen <teg@jklm.no>
-
-  systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU Lesser General Public License as published by
-  the Free Software Foundation; either version 2.1 of the License, or
-  (at your option) any later version.
-
-  systemd is distributed in the hope that it will be useful, but
-  WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public License
-  along with systemd; If not, see <http://www.gnu.org/licenses/>.
-***/
 
 #include <netinet/ether.h>
 #include <linux/if.h>
@@ -35,7 +17,7 @@
 
 static int dhcp4_route_handler(sd_netlink *rtnl, sd_netlink_message *m,
                                void *userdata) {
-        _cleanup_link_unref_ Link *link = userdata;
+        _cleanup_(link_unrefp) Link *link = userdata;
         int r;
 
         assert(link);
@@ -99,7 +81,9 @@ static int link_set_dhcp_routes(Link *link) {
                 return log_link_warning_errno(link, r, "DHCP error: could not get address: %m");
 
         n = sd_dhcp_lease_get_routes(link->dhcp_lease, &static_routes);
-        if (n < 0)
+        if (n == -ENODATA)
+                log_link_debug_errno(link, n, "DHCP: No routes received from DHCP server: %m");
+        else if (n < 0)
                 log_link_debug_errno(link, n, "DHCP error: could not get routes: %m");
 
         for (i = 0; i < n; i++) {
@@ -111,7 +95,7 @@ static int link_set_dhcp_routes(Link *link) {
         }
 
         for (i = 0; i < n; i++) {
-                _cleanup_route_free_ Route *route = NULL;
+                _cleanup_(route_freep) Route *route = NULL;
 
                 /* if the DHCP server returns both a Classless Static Routes option and a Static Routes option,
                    the DHCP client MUST ignore the Static Routes option. */
@@ -140,7 +124,7 @@ static int link_set_dhcp_routes(Link *link) {
 
         r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway);
         if (r == -ENODATA)
-                log_link_info_errno(link, r, "DHCP: No routes received from DHCP server: %m");
+                log_link_info_errno(link, r, "DHCP: No gateway received from DHCP server: %m");
         else if (r < 0)
                 log_link_warning_errno(link, r, "DHCP error: could not get gateway: %m");
 
@@ -150,8 +134,8 @@ static int link_set_dhcp_routes(Link *link) {
                 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_free_ Route *route = NULL;
-                _cleanup_route_free_ Route *route_gw = NULL;
+                _cleanup_(route_freep) Route *route = NULL;
+                _cleanup_(route_freep) Route *route_gw = NULL;
 
                 r = route_new(&route);
                 if (r < 0)
@@ -201,7 +185,7 @@ static int link_set_dhcp_routes(Link *link) {
 }
 
 static int dhcp_lease_lost(Link *link) {
-        _cleanup_address_free_ Address *address = NULL;
+        _cleanup_(address_freep) Address *address = NULL;
         struct in_addr addr;
         struct in_addr netmask;
         struct in_addr gateway;
@@ -220,7 +204,7 @@ static int dhcp_lease_lost(Link *link) {
                 n = sd_dhcp_lease_get_routes(link->dhcp_lease, &routes);
                 if (n >= 0) {
                         for (i = 0; i < n; i++) {
-                                _cleanup_route_free_ Route *route = NULL;
+                                _cleanup_(route_freep) Route *route = NULL;
 
                                 r = route_new(&route);
                                 if (r >= 0) {
@@ -240,8 +224,8 @@ static int dhcp_lease_lost(Link *link) {
         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;
+                        _cleanup_(route_freep) Route *route_gw = NULL;
+                        _cleanup_(route_freep) Route *route = NULL;
 
                         r = route_new(&route_gw);
                         if (r >= 0) {
@@ -318,7 +302,7 @@ static int dhcp_lease_lost(Link *link) {
 
 static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m,
                                  void *userdata) {
-        _cleanup_link_unref_ Link *link = userdata;
+        _cleanup_(link_unrefp) Link *link = userdata;
         int r;
 
         assert(link);
@@ -332,6 +316,11 @@ static int dhcp4_address_handler(sd_netlink *rtnl, sd_netlink_message *m,
 
         link_set_dhcp_routes(link);
 
+        if (link->dhcp4_messages == 0) {
+                link->dhcp4_configured = true;
+                link_check_ready(link);
+        }
+
         return 1;
 }
 
@@ -339,7 +328,7 @@ static int dhcp4_update_address(Link *link,
                                 struct in_addr *address,
                                 struct in_addr *netmask,
                                 uint32_t lifetime) {
-        _cleanup_address_free_ Address *addr = NULL;
+        _cleanup_(address_freep) Address *addr = NULL;
         unsigned prefixlen;
         int r;
 
@@ -452,8 +441,7 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
                                             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);
+                           "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(gateway));
         else
                 log_struct(LOG_INFO,
                            LOG_LINK_INTERFACE(link),
@@ -461,8 +449,7 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) {
                                             ADDRESS_FMT_VAL(address),
                                             prefixlen),
                            "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address),
-                           "PREFIXLEN=%u", prefixlen,
-                           NULL);
+                           "PREFIXLEN=%u", prefixlen);
 
         link->dhcp_lease = sd_dhcp_lease_ref(lease);
         link_dirty(link);
@@ -677,44 +664,46 @@ int dhcp4_configure(Link *link) {
 
         if (!link->dhcp_client) {
                 r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
+                if (r == -ENOMEM)
+                        return log_oom();
                 if (r < 0)
-                        return r;
+                        return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to create DHCP4 client: %m");
         }
 
         r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
         if (r < 0)
-                return r;
+                return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to attach event: %m");
 
         r = sd_dhcp_client_set_mac(link->dhcp_client,
                                    (const uint8_t *) &link->mac,
                                    sizeof (link->mac), ARPHRD_ETHER);
         if (r < 0)
-                return r;
+                return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MAC address: %m");
 
         r = sd_dhcp_client_set_ifindex(link->dhcp_client, link->ifindex);
         if (r < 0)
-                return r;
+                return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set ifindex: %m");
 
         r = sd_dhcp_client_set_callback(link->dhcp_client, dhcp4_handler, link);
         if (r < 0)
-                return r;
+                return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set callback: %m");
 
         r = sd_dhcp_client_set_request_broadcast(link->dhcp_client,
                                                  link->network->dhcp_broadcast);
         if (r < 0)
-                return r;
+                return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for broadcast: %m");
 
         if (link->mtu) {
                 r = sd_dhcp_client_set_mtu(link->dhcp_client, link->mtu);
                 if (r < 0)
-                        return r;
+                        return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set MTU: %m");
         }
 
         if (link->network->dhcp_use_mtu) {
                 r = sd_dhcp_client_set_request_option(link->dhcp_client,
                                                       SD_DHCP_OPTION_INTERFACE_MTU);
                 if (r < 0)
-                        return r;
+                        return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for MTU: %m");
         }
 
         /* NOTE: even if this variable is called "use", it also "sends" PRL
@@ -726,40 +715,47 @@ int dhcp4_configure(Link *link) {
                 r = sd_dhcp_client_set_request_option(link->dhcp_client,
                                                       SD_DHCP_OPTION_STATIC_ROUTE);
                 if (r < 0)
-                        return r;
+                        return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for static route: %m");
+
                 r = sd_dhcp_client_set_request_option(link->dhcp_client,
                                                       SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE);
                 if (r < 0)
-                        return r;
+                        return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for classless static route: %m");
         }
 
         if (link->network->dhcp_use_ntp) {
                 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NTP_SERVER);
                 if (r < 0)
-                        return r;
+                        return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for NTP server: %m");
         }
 
         if (link->network->dhcp_use_timezone) {
                 r = sd_dhcp_client_set_request_option(link->dhcp_client, SD_DHCP_OPTION_NEW_TZDB_TIMEZONE);
                 if (r < 0)
-                        return r;
+                        return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set request flag for timezone: %m");
         }
 
         r = dhcp4_set_hostname(link);
         if (r < 0)
-                return r;
+                return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set hostname: %m");
 
         if (link->network->dhcp_vendor_class_identifier) {
                 r = sd_dhcp_client_set_vendor_class_identifier(link->dhcp_client,
                                                                link->network->dhcp_vendor_class_identifier);
                 if (r < 0)
-                        return r;
+                        return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set vendor class identifier: %m");
+        }
+
+        if (link->network->dhcp_user_class) {
+                r = sd_dhcp_client_set_user_class(link->dhcp_client, (const char **) link->network->dhcp_user_class);
+                if (r < 0)
+                        return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set user class: %m");
         }
 
         if (link->network->dhcp_client_port) {
                 r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
                 if (r < 0)
-                        return r;
+                        return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set listen port: %m");
         }
 
         switch (link->network->dhcp_client_identifier) {
@@ -773,7 +769,19 @@ int dhcp4_configure(Link *link) {
                                                  duid->raw_data_len > 0 ? duid->raw_data : NULL,
                                                  duid->raw_data_len);
                 if (r < 0)
-                        return r;
+                        return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m");
+                break;
+        }
+        case DHCP_CLIENT_ID_DUID_ONLY: {
+                /* If configured, apply user specified DUID */
+                const DUID *duid = link_duid(link);
+
+                r = sd_dhcp_client_set_duid(link->dhcp_client,
+                                            duid->type,
+                                            duid->raw_data_len > 0 ? duid->raw_data : NULL,
+                                            duid->raw_data_len);
+                if (r < 0)
+                        return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set DUID: %m");
                 break;
         }
         case DHCP_CLIENT_ID_MAC:
@@ -782,7 +790,7 @@ int dhcp4_configure(Link *link) {
                                                  (const uint8_t *) &link->mac,
                                                  sizeof(link->mac));
                 if (r < 0)
-                        return r;
+                        return log_link_error_errno(link, r, "DHCP4 CLIENT: Failed to set client ID: %m");
                 break;
         default:
                 assert_not_reached("Unknown client identifier type.");