]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/network/networkd-dhcp4.c
basic/log: add the log_struct terminator to macro
[thirdparty/systemd.git] / src / network / networkd-dhcp4.c
index 0b46deb00936577339f8a94922d46ea19518cf50..3e7c7da5ac5b934684716d94b4aa66924db06b75 100644 (file)
@@ -3,19 +3,6 @@
   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>
@@ -35,7 +22,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);
@@ -71,8 +58,9 @@ static int route_scope_from_address(const Route *route, const struct in_addr *se
 }
 
 static int link_set_dhcp_routes(Link *link) {
-        struct in_addr gateway, address;
         _cleanup_free_ sd_dhcp_route **static_routes = NULL;
+        bool classless_route = false, static_route = false;
+        struct in_addr gateway, address;
         int r, n, i;
         uint32_t table;
 
@@ -102,7 +90,20 @@ static int link_set_dhcp_routes(Link *link) {
                 log_link_debug_errno(link, n, "DHCP error: could not get routes: %m");
 
         for (i = 0; i < n; i++) {
-                _cleanup_route_free_ Route *route = NULL;
+                if (static_routes[i]->option == SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE)
+                        classless_route = true;
+
+                if (static_routes[i]->option == SD_DHCP_OPTION_STATIC_ROUTE)
+                        static_route = true;
+        }
+
+        for (i = 0; i < n; i++) {
+                _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. */
+                if (classless_route && static_routes[i]->option == SD_DHCP_OPTION_STATIC_ROUTE)
+                        continue;
 
                 r = route_new(&route);
                 if (r < 0)
@@ -132,9 +133,12 @@ static int link_set_dhcp_routes(Link *link) {
 
         /* 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 (r >= 0 && link->dhcp4_messages <= 0) {
-                _cleanup_route_free_ Route *route = NULL;
-                _cleanup_route_free_ Route *route_gw = NULL;
+        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)
@@ -184,7 +188,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;
@@ -203,7 +207,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) {
@@ -223,8 +227,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) {
@@ -301,7 +305,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);
@@ -315,6 +319,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;
 }
 
@@ -322,7 +331,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;
 
@@ -435,8 +444,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),
@@ -444,8 +452,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);
@@ -739,6 +746,12 @@ int dhcp4_configure(Link *link) {
                         return r;
         }
 
+        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 r;
+        }
+
         if (link->network->dhcp_client_port) {
                 r = sd_dhcp_client_set_client_port(link->dhcp_client, link->network->dhcp_client_port);
                 if (r < 0)
@@ -759,6 +772,18 @@ int dhcp4_configure(Link *link) {
                         return r;
                 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 r;
+                break;
+        }
         case DHCP_CLIENT_ID_MAC:
                 r = sd_dhcp_client_set_client_id(link->dhcp_client,
                                                  ARPHRD_ETHER,