summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBeniamino Galvani <bgalvani@redhat.com>2022-05-03 09:14:06 +0200
committerBeniamino Galvani <bgalvani@redhat.com>2022-05-03 09:14:06 +0200
commitee7240783a4ba1e1c7f904ea9265fe368993356a (patch)
treeadb591510b5fb10f7ff8eb20edee2304e5088b2f
parente5d4194673073e6897a2514f326f245b8688bcc2 (diff)
parent6ab5c4e578b8d18e16c2d17c9d7f009b804ccc5b (diff)
downloadNetworkManager-ee7240783a4ba1e1c7f904ea9265fe368993356a.tar.gz
dhcp: merge branch 'bg/dhcp-lease-rundir'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1190
-rw-r--r--NEWS3
-rw-r--r--src/core/devices/nm-device.c1
-rw-r--r--src/core/dhcp/nm-dhcp-client.c9
-rw-r--r--src/core/dhcp/nm-dhcp-utils.c33
-rw-r--r--src/core/nm-config.c46
-rw-r--r--src/core/nm-config.h5
-rw-r--r--src/core/nm-dhcp-config.c23
-rw-r--r--src/core/nm-dhcp-config.h3
-rw-r--r--src/core/nm-manager.c18
9 files changed, 82 insertions, 59 deletions
diff --git a/NEWS b/NEWS
index 4bf6269ac8..9db05c5b7f 100644
--- a/NEWS
+++ b/NEWS
@@ -20,6 +20,9 @@ USE AT YOUR OWN RISK. NOT RECOMMENDED FOR PRODUCTION USE!
* Static IPv6 addresses from "ipv6.addresses" are now interpreted with
first address being preferred. Their order got inverted. This is now
consistent with IPv4.
+* The device state file /run/NetworkManager/devices/$ifindex now has
+ new sections [dhcp4] and [dhcp6] containing the DHCP options for the
+ current lease.
=============================================
NetworkManager-1.38
diff --git a/src/core/devices/nm-device.c b/src/core/devices/nm-device.c
index d1702f23a5..d77f3fa501 100644
--- a/src/core/devices/nm-device.c
+++ b/src/core/devices/nm-device.c
@@ -10064,6 +10064,7 @@ _dev_ipdhcpx_notify(NMDhcpClient *client, const NMDhcpClientNotifyData *notify_d
FALSE);
if (notify_data->lease_update.accepted) {
+ nm_manager_write_device_state(priv->manager, self, NULL);
if (priv->ipdhcp_data_x[IS_IPv4].state != NM_DEVICE_IP_STATE_READY) {
_dev_ipdhcpx_set_state(self, addr_family, NM_DEVICE_IP_STATE_READY);
nm_dispatcher_call_device(NM_DISPATCHER_ACTION_DHCP_CHANGE_X(IS_IPv4),
diff --git a/src/core/dhcp/nm-dhcp-client.c b/src/core/dhcp/nm-dhcp-client.c
index 29eeae186a..e71590858a 100644
--- a/src/core/dhcp/nm-dhcp-client.c
+++ b/src/core/dhcp/nm-dhcp-client.c
@@ -291,6 +291,7 @@ nm_dhcp_client_set_state(NMDhcpClient *self, NMDhcpState new_state, const NML3Co
if (!IS_IPv4 && l3cd) {
if (nm_dhcp_utils_merge_new_dhcp6_lease(priv->l3cd, l3cd, &l3cd_merged)) {
+ _LOGD("lease merged with existing one");
l3cd = nm_l3_config_data_seal(l3cd_merged);
}
}
@@ -327,14 +328,6 @@ nm_dhcp_client_set_state(NMDhcpClient *self, NMDhcpState new_state, const NML3Co
keys[i],
(char *) g_hash_table_lookup(options, keys[i]));
}
-
- if (priv->config.addr_family == AF_INET6) {
- gs_free char *event_id = NULL;
-
- event_id = nm_dhcp_utils_get_dhcp6_event_id(options);
- if (event_id)
- _LOGT("event-id: \"%s\"", event_id);
- }
}
}
diff --git a/src/core/dhcp/nm-dhcp-utils.c b/src/core/dhcp/nm-dhcp-utils.c
index a0eec6e099..ecb6eea67a 100644
--- a/src/core/dhcp/nm-dhcp-utils.c
+++ b/src/core/dhcp/nm-dhcp-utils.c
@@ -295,7 +295,7 @@ process_classful_routes(const char *iface,
return;
if ((NM_PTRARRAY_LEN(searches) % 2) != 0) {
- _LOG2I(LOGD_DHCP, iface, " static routes provided, but invalid");
+ _LOG2I(LOGD_DHCP4, iface, " static routes provided, but invalid");
return;
}
@@ -305,11 +305,11 @@ process_classful_routes(const char *iface,
guint32 rt_addr, rt_route;
if (inet_pton(AF_INET, *s, &rt_addr) <= 0) {
- _LOG2W(LOGD_DHCP, iface, "DHCP provided invalid static route address: '%s'", *s);
+ _LOG2W(LOGD_DHCP4, iface, "DHCP provided invalid static route address: '%s'", *s);
continue;
}
if (inet_pton(AF_INET, *(s + 1), &rt_route) <= 0) {
- _LOG2W(LOGD_DHCP, iface, "DHCP provided invalid static route gateway: '%s'", *(s + 1));
+ _LOG2W(LOGD_DHCP4, iface, "DHCP provided invalid static route gateway: '%s'", *(s + 1));
continue;
}
@@ -340,7 +340,7 @@ process_classful_routes(const char *iface,
nm_l3_config_data_add_route_4(l3cd, &route);
- _LOG2I(LOGD_DHCP,
+ _LOG2I(LOGD_DHCP4,
iface,
" static route %s",
nm_platform_ip4_route_to_string(&route, sbuf, sizeof(sbuf)));
@@ -352,6 +352,7 @@ process_domain_search(int addr_family, const char *iface, const char *str, NML3C
{
gs_free const char **searches = NULL;
gs_free char *unescaped = NULL;
+ NMLogDomain logd = NM_IS_IPv4(addr_family) ? LOGD_DHCP4 : LOGD_DHCP6;
const char **s;
char *p;
int i;
@@ -373,13 +374,13 @@ process_domain_search(int addr_family, const char *iface, const char *str, NML3C
} while (*p++);
if (strchr(unescaped, '\\')) {
- _LOG2W(LOGD_DHCP, iface, " invalid domain search: '%s'", unescaped);
+ _LOG2W(logd, iface, " invalid domain search: '%s'", unescaped);
return;
}
searches = nm_strsplit_set(unescaped, " ");
for (s = searches; searches && *s; s++) {
- _LOG2I(LOGD_DHCP, iface, " domain search '%s'", *s);
+ _LOG2I(logd, iface, " domain search '%s'", *s);
nm_l3_config_data_add_search(l3cd, addr_family, *s);
}
}
@@ -820,26 +821,6 @@ nm_dhcp_utils_get_leasefile_path(int addr_family,
return FALSE;
}
-char *
-nm_dhcp_utils_get_dhcp6_event_id(GHashTable *lease)
-{
- const char *start;
- const char *iaid;
-
- if (!lease)
- return NULL;
-
- iaid = g_hash_table_lookup(lease, "iaid");
- if (!iaid)
- return NULL;
-
- start = g_hash_table_lookup(lease, "life_starts");
- if (!start)
- return NULL;
-
- return g_strdup_printf("%s|%s", iaid, start);
-}
-
gboolean
nm_dhcp_utils_merge_new_dhcp6_lease(const NML3ConfigData *l3cd_old,
const NML3ConfigData *l3cd_new,
diff --git a/src/core/nm-config.c b/src/core/nm-config.c
index ea1c2ab300..cdc070900f 100644
--- a/src/core/nm-config.c
+++ b/src/core/nm-config.c
@@ -11,7 +11,9 @@
#include <stdio.h>
#include "nm-utils.h"
+#include "nm-dhcp-config.h"
#include "devices/nm-device.h"
+#include "dhcp/nm-dhcp-options.h"
#include "NetworkManagerUtils.h"
#include "libnm-core-intern/nm-core-internal.h"
#include "libnm-core-intern/nm-keyfile-internal.h"
@@ -2573,13 +2575,16 @@ nm_config_device_state_write(int ifindex,
NMTernary nm_owned,
guint32 route_metric_default_aspired,
guint32 route_metric_default_effective,
- const char *next_server,
- const char *root_path,
- const char *dhcp_bootfile)
+ NMDhcpConfig *dhcp4_config,
+ NMDhcpConfig *dhcp6_config)
{
char path[NM_STRLEN(NM_CONFIG_DEVICE_STATE_DIR "/") + DEVICE_STATE_FILENAME_LEN_MAX + 1];
- GError *local = NULL;
- nm_auto_unref_keyfile GKeyFile *kf = NULL;
+ GError *local = NULL;
+ nm_auto_unref_keyfile GKeyFile *kf = NULL;
+ const char *root_path = NULL;
+ const char *next_server = NULL;
+ const char *dhcp_bootfile = NULL;
+ int IS_IPv4;
g_return_val_if_fail(ifindex > 0, FALSE);
g_return_val_if_fail(!connection_uuid || *connection_uuid, FALSE);
@@ -2630,6 +2635,15 @@ nm_config_device_state_write(int ifindex,
route_metric_default_aspired);
}
}
+
+ if (dhcp4_config) {
+ next_server = nm_dhcp_config_get_option(dhcp4_config, "next_server");
+ root_path = nm_dhcp_config_get_option(dhcp4_config, "root_path");
+ dhcp_bootfile = nm_dhcp_config_get_option(dhcp4_config, "filename");
+ if (!dhcp_bootfile)
+ dhcp_bootfile = nm_dhcp_config_get_option(dhcp4_config, "bootfile_name");
+ }
+
if (next_server) {
g_key_file_set_string(kf,
DEVICE_RUN_STATE_KEYFILE_GROUP_DEVICE,
@@ -2649,6 +2663,28 @@ nm_config_device_state_write(int ifindex,
dhcp_bootfile);
}
+ for (IS_IPv4 = 1; IS_IPv4 >= 0; IS_IPv4--) {
+ NMDhcpConfig *dhcp_config = IS_IPv4 ? dhcp4_config : dhcp6_config;
+ gs_free NMUtilsNamedValue *values = NULL;
+ guint i;
+ guint num;
+
+ if (!dhcp_config)
+ continue;
+
+ values = nm_dhcp_config_get_option_values(dhcp_config, &num);
+ for (i = 0; i < num; i++) {
+ gs_free char *name_full = NULL;
+ const char *prefix = IS_IPv4 ? "dhcp4" : "dhcp6";
+
+ if (NM_STR_HAS_PREFIX(values[i].name, NM_DHCP_OPTION_REQPREFIX))
+ continue;
+
+ name_full = g_strdup_printf("%s.%s", prefix, values[i].name);
+ g_key_file_set_string(kf, prefix, name_full, values[i].value_str);
+ }
+ }
+
if (!g_key_file_save_to_file(kf, path, &local)) {
_LOGW("device-state: write #%d (%s) failed: %s", ifindex, path, local->message);
g_error_free(local);
diff --git a/src/core/nm-config.h b/src/core/nm-config.h
index 2c23ff200e..d7498f92be 100644
--- a/src/core/nm-config.h
+++ b/src/core/nm-config.h
@@ -183,9 +183,8 @@ gboolean nm_config_device_state_write(int
NMTernary nm_owned,
guint32 route_metric_default_aspired,
guint32 route_metric_default_effective,
- const char *next_server,
- const char *root_path,
- const char *dhcp_bootfile);
+ NMDhcpConfig *dhcp4_config,
+ NMDhcpConfig *dhcp6_config);
void nm_config_device_state_prune_stale(GHashTable *preserve_ifindexes,
NMPlatform *preserve_in_platform);
diff --git a/src/core/nm-dhcp-config.c b/src/core/nm-dhcp-config.c
index b32c9b7056..394a3a4924 100644
--- a/src/core/nm-dhcp-config.c
+++ b/src/core/nm-dhcp-config.c
@@ -125,6 +125,29 @@ nm_dhcp_config_set_lease(NMDhcpConfig *self, const NML3ConfigData *l3cd)
_notify(self, PROP_OPTIONS);
}
+NMUtilsNamedValue *
+nm_dhcp_config_get_option_values(NMDhcpConfig *self, guint *num)
+{
+ NMDhcpConfigPrivate *priv = NM_DHCP_CONFIG_GET_PRIVATE(self);
+ NMDhcpLease *lease;
+ NMUtilsNamedValue *buffer = NULL;
+
+ if (!priv->l3cd) {
+ NM_SET_OUT(num, 0);
+ return NULL;
+ }
+
+ lease = nm_l3_config_data_get_dhcp_lease(priv->l3cd, nm_dhcp_config_get_addr_family(self));
+ nm_utils_named_values_from_strdict_full(nm_dhcp_lease_get_options(lease),
+ num,
+ nm_strcmp_p_with_data,
+ NULL,
+ NULL,
+ 0,
+ &buffer);
+ return buffer;
+}
+
const char *
nm_dhcp_config_get_option(NMDhcpConfig *self, const char *key)
{
diff --git a/src/core/nm-dhcp-config.h b/src/core/nm-dhcp-config.h
index cae0e11aa2..cdca4ef1fb 100644
--- a/src/core/nm-dhcp-config.h
+++ b/src/core/nm-dhcp-config.h
@@ -30,7 +30,8 @@ int nm_dhcp_config_get_addr_family(NMDhcpConfig *self);
void nm_dhcp_config_set_lease(NMDhcpConfig *self, const NML3ConfigData *l3cd);
-const char *nm_dhcp_config_get_option(NMDhcpConfig *self, const char *option);
+NMUtilsNamedValue *nm_dhcp_config_get_option_values(NMDhcpConfig *self, guint *num);
+const char *nm_dhcp_config_get_option(NMDhcpConfig *self, const char *option);
GVariant *nm_dhcp_config_get_options(NMDhcpConfig *self);
diff --git a/src/core/nm-manager.c b/src/core/nm-manager.c
index 191d65bf65..ee661e499e 100644
--- a/src/core/nm-manager.c
+++ b/src/core/nm-manager.c
@@ -7039,10 +7039,6 @@ nm_manager_write_device_state(NMManager *self, NMDevice *device, int *out_ifinde
guint32 route_metric_default_aspired;
guint32 route_metric_default_effective;
NMTernary nm_owned;
- NMDhcpConfig *dhcp_config;
- const char *next_server = NULL;
- const char *root_path = NULL;
- const char *dhcp_bootfile = NULL;
NM_SET_OUT(out_ifindex, 0);
@@ -7084,15 +7080,6 @@ nm_manager_write_device_state(NMManager *self, NMDevice *device, int *out_ifinde
TRUE,
&route_metric_default_aspired);
- dhcp_config = nm_device_get_dhcp_config(device, AF_INET);
- if (dhcp_config) {
- root_path = nm_dhcp_config_get_option(dhcp_config, "root_path");
- next_server = nm_dhcp_config_get_option(dhcp_config, "next_server");
- dhcp_bootfile = nm_dhcp_config_get_option(dhcp_config, "filename");
- if (!dhcp_bootfile)
- dhcp_bootfile = nm_dhcp_config_get_option(dhcp_config, "bootfile_name");
- }
-
if (!nm_config_device_state_write(ifindex,
managed_type,
perm_hw_addr_fake,
@@ -7100,9 +7087,8 @@ nm_manager_write_device_state(NMManager *self, NMDevice *device, int *out_ifinde
nm_owned,
route_metric_default_aspired,
route_metric_default_effective,
- next_server,
- root_path,
- dhcp_bootfile))
+ nm_device_get_dhcp_config(device, AF_INET),
+ nm_device_get_dhcp_config(device, AF_INET6)))
return FALSE;
NM_SET_OUT(out_ifindex, ifindex);