summary refs log tree commit diff
path: root/src/core/dhcp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/dhcp')
-rw-r--r--src/core/dhcp/nm-dhcp-client.c7
-rw-r--r--src/core/dhcp/nm-dhcp-client.h2
-rw-r--r--src/core/dhcp/nm-dhcp-dhcpcd.c42
-rw-r--r--src/core/dhcp/nm-dhcp-nettools.c29
-rw-r--r--src/core/dhcp/nm-dhcp-systemd.c46
-rw-r--r--src/core/dhcp/nm-dhcp-utils.c2
6 files changed, 69 insertions, 59 deletions
diff --git a/src/core/dhcp/nm-dhcp-client.c b/src/core/dhcp/nm-dhcp-client.c
index c38c814e..3b0343fd 100644
--- a/src/core/dhcp/nm-dhcp-client.c
+++ b/src/core/dhcp/nm-dhcp-client.c
@@ -84,6 +84,11 @@ G_DEFINE_ABSTRACT_TYPE(NMDhcpClient, nm_dhcp_client, G_TYPE_OBJECT)
 
 /*****************************************************************************/
 
+/* we use pid=-1 for invalid PIDs. Ensure that pid_t can hold negative values. */
+G_STATIC_ASSERT(!(((pid_t) -1) > 0));
+
+/*****************************************************************************/
+
 pid_t
 nm_dhcp_client_get_pid(NMDhcpClient *self)
 {
@@ -749,7 +754,7 @@ nm_dhcp_client_stop(NMDhcpClient *self, gboolean release)
         _LOGI("canceled DHCP transaction");
     nm_assert(priv->pid == -1);
 
-    nm_dhcp_client_set_state(self, NM_DHCP_STATE_DONE, NULL, NULL);
+    nm_dhcp_client_set_state(self, NM_DHCP_STATE_TERMINATED, NULL, NULL);
 }
 
 /*****************************************************************************/
diff --git a/src/core/dhcp/nm-dhcp-client.h b/src/core/dhcp/nm-dhcp-client.h
index 72ab477d..af3406cc 100644
--- a/src/core/dhcp/nm-dhcp-client.h
+++ b/src/core/dhcp/nm-dhcp-client.h
@@ -51,7 +51,7 @@ typedef enum {
     NM_DHCP_STATE_BOUND,      /* new lease */
     NM_DHCP_STATE_EXTENDED,   /* lease extended */
     NM_DHCP_STATE_TIMEOUT,    /* timed out contacting server */
-    NM_DHCP_STATE_DONE,       /* client quit or stopped */
+    NM_DHCP_STATE_DONE,       /* client reported it's stopping */
     NM_DHCP_STATE_EXPIRE,     /* lease expired or NAKed */
     NM_DHCP_STATE_FAIL,       /* failed for some reason */
     NM_DHCP_STATE_TERMINATED, /* client is no longer running */
diff --git a/src/core/dhcp/nm-dhcp-dhcpcd.c b/src/core/dhcp/nm-dhcp-dhcpcd.c
index cf9fe5c2..cdb266ed 100644
--- a/src/core/dhcp/nm-dhcp-dhcpcd.c
+++ b/src/core/dhcp/nm-dhcp-dhcpcd.c
@@ -169,27 +169,29 @@ stop(NMDhcpClient *client, gboolean release)
     int           sig, errsv;
 
     pid = nm_dhcp_client_get_pid(client);
-    sig = release ? SIGALRM : SIGTERM;
-    _LOGD("sending %s to dhcpcd pid %d", sig == SIGALRM ? "SIGALRM" : "SIGTERM", pid);
-
-    /* dhcpcd-9.x features privilege separation.
-     * It's not our job to track all these processes so we rely on dhcpcd
-     * to always cleanup after itself.
-     * Because it also re-parents itself to PID 1, the process cannot be
-     * reaped or waited for.
-     * As such, just send the correct signal.
-     */
-    if (kill(pid, sig) == -1) {
-        errsv = errno;
-        _LOGE("failed to kill dhcpcd %d:%s", errsv, strerror(errsv));
-    }
+    if (pid > 1) {
+        sig = release ? SIGALRM : SIGTERM;
+        _LOGD("sending %s to dhcpcd pid %d", sig == SIGALRM ? "SIGALRM" : "SIGTERM", pid);
+
+        /* dhcpcd-9.x features privilege separation.
+         * It's not our job to track all these processes so we rely on dhcpcd
+         * to always cleanup after itself.
+         * Because it also re-parents itself to PID 1, the process cannot be
+         * reaped or waited for.
+         * As such, just send the correct signal.
+         */
+        if (kill(pid, sig) == -1) {
+            errsv = errno;
+            _LOGE("failed to kill dhcpcd %d:%s", errsv, strerror(errsv));
+        }
 
-    /* When this function exits NM expects the PID to be -1.
-     * This means we also need to stop watching the pid.
-     * If we need to know the exit status then we need to refactor NM
-     * to allow a non -1 to mean we're waiting to exit still.
-     */
-    nm_dhcp_client_stop_watch_child(client, pid);
+        /* When this function exits NM expects the PID to be -1.
+         * This means we also need to stop watching the pid.
+         * If we need to know the exit status then we need to refactor NM
+         * to allow a non -1 to mean we're waiting to exit still.
+         */
+        nm_dhcp_client_stop_watch_child(client, pid);
+    }
 }
 
 /*****************************************************************************/
diff --git a/src/core/dhcp/nm-dhcp-nettools.c b/src/core/dhcp/nm-dhcp-nettools.c
index 116e1bdb..c8b80ea6 100644
--- a/src/core/dhcp/nm-dhcp-nettools.c
+++ b/src/core/dhcp/nm-dhcp-nettools.c
@@ -342,16 +342,16 @@ lease_parse_routes(NDhcp4ClientLease *lease,
     const guint8 *l_data;
     gsize         l_data_len;
     int           r;
+    guint         i;
 
-    r = _client_lease_query(lease,
-                            NM_DHCP_OPTION_DHCP4_CLASSLESS_STATIC_ROUTE,
-                            &l_data,
-                            &l_data_len);
-    if (r == 0) {
-        nm_str_buf_reset(sbuf);
+    for (i = 0; i < 2; i++) {
+        const guint8 option_code = (i == 0) ? NM_DHCP_OPTION_DHCP4_CLASSLESS_STATIC_ROUTE
+                                            : NM_DHCP_OPTION_DHCP4_PRIVATE_CLASSLESS_STATIC_ROUTE;
 
-        has_classless = TRUE;
+        if (_client_lease_query(lease, option_code, &l_data, &l_data_len) != 0)
+            continue;
 
+        nm_str_buf_reset(sbuf);
         while (lease_option_consume_route(&l_data, &l_data_len, TRUE, &dest, &plen, &gateway)) {
             _nm_utils_inet4_ntop(dest, dest_str);
             _nm_utils_inet4_ntop(gateway, gateway_str);
@@ -359,6 +359,11 @@ lease_parse_routes(NDhcp4ClientLease *lease,
             nm_str_buf_append_required_delimiter(sbuf, ' ');
             nm_str_buf_append_printf(sbuf, "%s/%d %s", dest_str, (int) plen, gateway_str);
 
+            if (has_classless) {
+                /* Ignore private option if the standard one is present */
+                continue;
+            }
+
             if (plen == 0) {
                 /* if there are multiple default routes, we add them with differing
                  * metrics. */
@@ -384,10 +389,8 @@ lease_parse_routes(NDhcp4ClientLease *lease,
                 NULL);
         }
 
-        nm_dhcp_option_add_option(options,
-                                  AF_INET,
-                                  NM_DHCP_OPTION_DHCP4_CLASSLESS_STATIC_ROUTE,
-                                  nm_str_buf_get_str(sbuf));
+        has_classless = TRUE;
+        nm_dhcp_option_add_option(options, AF_INET, option_code, nm_str_buf_get_str(sbuf));
     }
 
     r = _client_lease_query(lease, NM_DHCP_OPTION_DHCP4_STATIC_ROUTE, &l_data, &l_data_len);
@@ -693,8 +696,8 @@ lease_to_ip4_config(NMDedupMultiIndex *multi_idx,
 
         v_str = nm_utils_buf_utf8safe_escape((char *) l_data, l_data_len, 0, &to_free);
 
-        nm_dhcp_option_add_option(options, AF_INET, NM_DHCP_OPTION_DHCP4_NIS_DOMAIN, v_str);
-        nm_ip4_config_set_nis_domain(ip4_config, v_str);
+        nm_dhcp_option_add_option(options, AF_INET, NM_DHCP_OPTION_DHCP4_NIS_DOMAIN, v_str ?: "");
+        nm_ip4_config_set_nis_domain(ip4_config, v_str ?: "");
     }
 
     lease_parse_address_list(lease, ip4_config, NM_DHCP_OPTION_DHCP4_NIS_SERVERS, options, &sbuf);
diff --git a/src/core/dhcp/nm-dhcp-systemd.c b/src/core/dhcp/nm-dhcp-systemd.c
index b92a9073..100807aa 100644
--- a/src/core/dhcp/nm-dhcp-systemd.c
+++ b/src/core/dhcp/nm-dhcp-systemd.c
@@ -85,29 +85,29 @@ lease_to_ip4_config(NMDedupMultiIndex *multi_idx,
     const struct in_addr *         addr_list;
     char                           addr_str[NM_UTILS_INET_ADDRSTRLEN];
     const char *                   s;
-    nm_auto_free_gstring GString *str      = NULL;
-    gs_free sd_dhcp_route **routes         = NULL;
-    const char *const *     search_domains = NULL;
-    guint16                 mtu;
-    int                     i, num;
-    const void *            data;
-    gsize                   data_len;
-    gboolean                metered                   = FALSE;
-    gboolean                has_router_from_classless = FALSE;
-    gboolean                has_classless_route       = FALSE;
-    gboolean                has_static_route          = FALSE;
-    const gint32            ts                        = nm_utils_get_monotonic_timestamp_sec();
-    gint64                  ts_time                   = time(NULL);
-    struct in_addr          a_address;
-    struct in_addr          a_netmask;
-    struct in_addr          a_next_server;
-    struct in_addr          server_id;
-    struct in_addr          broadcast;
-    const struct in_addr *  a_router;
-    guint32                 a_plen;
-    guint32                 a_lifetime;
-    guint32                 renewal;
-    guint32                 rebinding;
+    nm_auto_free_gstring GString *str           = NULL;
+    nm_auto_free sd_dhcp_route **routes         = NULL;
+    const char *const *          search_domains = NULL;
+    guint16                      mtu;
+    int                          i, num;
+    const void *                 data;
+    gsize                        data_len;
+    gboolean                     metered                   = FALSE;
+    gboolean                     has_router_from_classless = FALSE;
+    gboolean                     has_classless_route       = FALSE;
+    gboolean                     has_static_route          = FALSE;
+    const gint32                 ts                        = nm_utils_get_monotonic_timestamp_sec();
+    gint64                       ts_time                   = time(NULL);
+    struct in_addr               a_address;
+    struct in_addr               a_netmask;
+    struct in_addr               a_next_server;
+    struct in_addr               server_id;
+    struct in_addr               broadcast;
+    const struct in_addr *       a_router;
+    guint32                      a_plen;
+    guint32                      a_lifetime;
+    guint32                      renewal;
+    guint32                      rebinding;
     gs_free nm_sd_dhcp_option *private_options = NULL;
 
     nm_assert(lease != NULL);
diff --git a/src/core/dhcp/nm-dhcp-utils.c b/src/core/dhcp/nm-dhcp-utils.c
index 646411e2..63d90641 100644
--- a/src/core/dhcp/nm-dhcp-utils.c
+++ b/src/core/dhcp/nm-dhcp-utils.c
@@ -880,7 +880,7 @@ nm_dhcp_lease_data_parse_cstr(const guint8 *data, gsize n_data, gsize *out_new_l
         n_data--;
 
     if (n_data > 0) {
-        if (memchr(data, n_data, '\0')) {
+        if (memchr(data, '\0', n_data)) {
             /* we accept trailing NUL, but none in between.
              *
              * https://tools.ietf.org/html/rfc2132#section-2