From: Yu Watanabe Date: Thu, 9 Apr 2026 23:22:27 +0000 (+0900) Subject: sd-dhcp-client: split out setting anonymize parameter X-Git-Tag: v261-rc1~134^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e7c550895aa80e6865b3ceeb63bf8a027756fab6;p=thirdparty%2Fsystemd.git sd-dhcp-client: split out setting anonymize parameter No effective functional change. Just refactoring. --- diff --git a/src/libsystemd-network/fuzz-dhcp-client.c b/src/libsystemd-network/fuzz-dhcp-client.c index bc6be37be80..074b52b7c0b 100644 --- a/src/libsystemd-network/fuzz-dhcp-client.c +++ b/src/libsystemd-network/fuzz-dhcp-client.c @@ -54,7 +54,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { ASSERT_NOT_NULL(e); _cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; - ASSERT_OK(sd_dhcp_client_new(&client, /* anonymize= */ false)); + ASSERT_OK(sd_dhcp_client_new(&client)); ASSERT_NOT_NULL(client); ASSERT_OK(sd_dhcp_client_attach_event(client, e, /* priority= */ 0)); diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index 10161148a4d..e6e844550ee 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -45,32 +45,6 @@ static const uint8_t default_req_opts[] = { SD_DHCP_OPTION_DOMAIN_NAME_SERVER, }; -/* RFC7844 section 3: - MAY contain the Parameter Request List option. - RFC7844 section 3.6: - The client intending to protect its privacy SHOULD only request a - minimal number of options in the PRL and SHOULD also randomly shuffle - the ordering of option codes in the PRL. If this random ordering - cannot be implemented, the client MAY order the option codes in the - PRL by option code number (lowest to highest). -*/ -/* NOTE: using PRL options that Windows 10 RFC7844 implementation uses */ -static const uint8_t default_req_opts_anonymize[] = { - SD_DHCP_OPTION_SUBNET_MASK, /* 1 */ - SD_DHCP_OPTION_ROUTER, /* 3 */ - SD_DHCP_OPTION_DOMAIN_NAME_SERVER, /* 6 */ - SD_DHCP_OPTION_DOMAIN_NAME, /* 15 */ - SD_DHCP_OPTION_ROUTER_DISCOVERY, /* 31 */ - SD_DHCP_OPTION_STATIC_ROUTE, /* 33 */ - SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION, /* 43 */ - SD_DHCP_OPTION_NETBIOS_NAME_SERVER, /* 44 */ - SD_DHCP_OPTION_NETBIOS_NODE_TYPE, /* 46 */ - SD_DHCP_OPTION_NETBIOS_SCOPE, /* 47 */ - SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE, /* 121 */ - SD_DHCP_OPTION_PRIVATE_CLASSLESS_STATIC_ROUTE, /* 249 */ - SD_DHCP_OPTION_PRIVATE_PROXY_AUTODISCOVERY, /* 252 */ -}; - static void client_stop(sd_dhcp_client *client, int error); static int client_restart(sd_dhcp_client *client); @@ -100,6 +74,14 @@ int sd_dhcp_client_set_callback( return 0; } +int sd_dhcp_client_set_anonymize(sd_dhcp_client *client, int b) { + assert_return(client, -EINVAL); + assert_return(!sd_dhcp_client_is_running(client), -EBUSY); + + client->anonymize = !!b; + return 0; +} + int sd_dhcp_client_set_request_broadcast(sd_dhcp_client *client, int broadcast) { assert_return(client, -EINVAL); assert_return(!sd_dhcp_client_is_running(client), -EBUSY); @@ -1427,9 +1409,7 @@ static sd_dhcp_client* dhcp_client_free(sd_dhcp_client *client) { DEFINE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_client, sd_dhcp_client, dhcp_client_free); -int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize) { - const uint8_t *opts; - size_t n_opts; +int sd_dhcp_client_new(sd_dhcp_client **ret) { int r; assert_return(ret, -EINVAL); @@ -1444,21 +1424,12 @@ int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize) { .ifindex = -1, .port = DHCP_PORT_CLIENT, .server_port = DHCP_PORT_SERVER, - .anonymize = !!anonymize, .max_discover_attempts = UINT64_MAX, .ip_service_type = -1, }; - /* NOTE: this could be moved to a function. */ - if (anonymize) { - n_opts = ELEMENTSOF(default_req_opts_anonymize); - opts = default_req_opts_anonymize; - } else { - n_opts = ELEMENTSOF(default_req_opts); - opts = default_req_opts; - } - for (size_t i = 0; i < n_opts; i++) { - r = sd_dhcp_client_set_request_option(client, opts[i]); + FOREACH_ELEMENT(opt, default_req_opts) { + r = sd_dhcp_client_set_request_option(client, *opt); if (r < 0) return r; } diff --git a/src/libsystemd-network/test-dhcp-client.c b/src/libsystemd-network/test-dhcp-client.c index c62f46222ab..97803a7faf1 100644 --- a/src/libsystemd-network/test-dhcp-client.c +++ b/src/libsystemd-network/test-dhcp-client.c @@ -48,7 +48,7 @@ static be32_t xid; TEST(dhcp_client_setters) { /* Initialize client without Anonymize settings. */ _cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; - ASSERT_OK(sd_dhcp_client_new(&client, /* anonymize= */ false)); + ASSERT_OK(sd_dhcp_client_new(&client)); ASSERT_NOT_NULL(client); ASSERT_RETURN_EXPECTED_SE(sd_dhcp_client_set_request_option(NULL, 0) == -EINVAL); @@ -91,15 +91,15 @@ TEST(dhcp_client_setters) { ASSERT_OK_ZERO(sd_dhcp_client_set_request_option(client, 17)); } -TEST(dhcp_client_anonymize) { +TEST(dhcp_client_set_anonymize) { /* Initialize client with Anonymize settings. */ _cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; - ASSERT_OK(sd_dhcp_client_new(&client, /* anonymize= */ true)); + ASSERT_OK(sd_dhcp_client_new(&client)); + ASSERT_OK(sd_dhcp_client_set_anonymize(client, true)); ASSERT_NOT_NULL(client); - ASSERT_OK_ZERO(sd_dhcp_client_set_request_option(client, SD_DHCP_OPTION_NETBIOS_NAME_SERVER)); - /* This PRL option is not set when using Anonymize */ - ASSERT_OK_POSITIVE(sd_dhcp_client_set_request_option(client, SD_DHCP_OPTION_HOST_NAME)); + ASSERT_OK_POSITIVE(sd_dhcp_client_set_request_option(client, SD_DHCP_OPTION_NETBIOS_NAME_SERVER)); + ASSERT_OK_ZERO(sd_dhcp_client_set_request_option(client, SD_DHCP_OPTION_HOST_NAME)); ASSERT_ERROR(sd_dhcp_client_set_request_option(client, SD_DHCP_OPTION_PARAMETER_REQUEST_LIST), EINVAL); /* RFC7844: option 101 (SD_DHCP_OPTION_NEW_TZDB_TIMEZONE) is not set in the @@ -211,7 +211,7 @@ TEST(discover_message) { ASSERT_NOT_NULL(e); _cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; - ASSERT_OK(sd_dhcp_client_new(&client, /* anonymize= */ false)); + ASSERT_OK(sd_dhcp_client_new(&client)); ASSERT_NOT_NULL(client); ASSERT_OK(sd_dhcp_client_attach_event(client, e, /* priority= */ 0)); @@ -400,7 +400,7 @@ TEST(addr_acq) { ASSERT_NOT_NULL(e); _cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; - ASSERT_OK(sd_dhcp_client_new(&client, /* anonymize= */ false)); + ASSERT_OK(sd_dhcp_client_new(&client)); ASSERT_NOT_NULL(client); ASSERT_OK(sd_dhcp_client_attach_event(client, e, /* priority= */ 0)); @@ -575,7 +575,7 @@ static void test_bootp_one(void) { ASSERT_NOT_NULL(e); _cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; - ASSERT_OK(sd_dhcp_client_new(&client, /* anonymize= */ false)); + ASSERT_OK(sd_dhcp_client_new(&client)); ASSERT_NOT_NULL(client); ASSERT_OK(sd_dhcp_client_attach_event(client, e, /* priority= */ 0)); diff --git a/src/network/networkd-dhcp4.c b/src/network/networkd-dhcp4.c index b5210e9ddfc..909f41563ac 100644 --- a/src/network/networkd-dhcp4.c +++ b/src/network/networkd-dhcp4.c @@ -1486,10 +1486,14 @@ static int dhcp4_configure(Link *link) { if (link->dhcp_client) return log_link_debug_errno(link, SYNTHETIC_ERRNO(EBUSY), "DHCPv4 client is already configured."); - r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize); + r = sd_dhcp_client_new(&link->dhcp_client); if (r < 0) return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to allocate DHCPv4 client: %m"); + r = sd_dhcp_client_set_anonymize(link->dhcp_client, link->network->dhcp_anonymize); + if (r < 0) + return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to anonymize requests: %m"); + r = sd_dhcp_client_set_bootp(link->dhcp_client, link->network->dhcp_use_bootp); if (r < 0) return log_link_debug_errno(link, r, "DHCPv4 CLIENT: Failed to %s BOOTP: %m", diff --git a/src/systemd/sd-dhcp-client.h b/src/systemd/sd-dhcp-client.h index 4e7e79da656..324cc34156c 100644 --- a/src/systemd/sd-dhcp-client.h +++ b/src/systemd/sd-dhcp-client.h @@ -50,7 +50,7 @@ int sd_dhcp_client_set_callback( sd_dhcp_client *client, sd_dhcp_client_callback_t cb, void *userdata); - +int sd_dhcp_client_set_anonymize(sd_dhcp_client *client, int b); int sd_dhcp_client_set_request_option( sd_dhcp_client *client, uint8_t option); @@ -156,9 +156,7 @@ int sd_dhcp_client_is_waiting_for_ipv6_connectivity(sd_dhcp_client *client); _SD_DECLARE_TRIVIAL_REF_UNREF_FUNC(sd_dhcp_client); -/* NOTE: anonymize parameter is used to initialize PRL memory with different - * options when using RFC7844 Anonymity Profiles */ -int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize); +int sd_dhcp_client_new(sd_dhcp_client **ret); int sd_dhcp_client_attach_event( sd_dhcp_client *client,