Skip to content

Commit 504cef7

Browse files
authored
Merge pull request #6436 from juga0/features/rfc7844
Features/rfc7844
2 parents 1de2a9a + dbb35f1 commit 504cef7

9 files changed

Lines changed: 219 additions & 26 deletions

File tree

‎man/systemd.network.xml‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,27 @@
997997
Defaults to false.</para>
998998
</listitem>
999999
</varlistentry>
1000+
<varlistentry>
1001+
<term><varname>Anonymize=</varname></term>
1002+
<listitem>
1003+
<para>Takes a boolean argument. When true, the options sent to the DHCP server will
1004+
follow the <ulink url="https://tools.ietf.org/html/rfc7844">RFC 7844</ulink>
1005+
(Anonymity Profiles for DHCP Clients) to minimize disclosure of identifying information.
1006+
Defaults to false.</para>
1007+
1008+
<para>This option should only be set to true when
1009+
<varname>MACAddressPolicy=</varname> is set to <literal>random</literal>
1010+
(see <citerefentry
1011+
project='man-pages'><refentrytitle>systemd.link</refentrytitle><manvolnum>5</manvolnum></citerefentry>).</para>
1012+
1013+
<para>Note that this configuration will overwrite others.
1014+
In concrete, the following variables will be ignored:
1015+
<varname>SendHostname=</varname>, <varname>ClientIdentifier=</varname>,
1016+
<varname>UseRoutes=</varname>, <varname>SendHostname=</varname>,
1017+
<varname>UseMTU=</varname>, <varname>VendorClassIdentifier=</varname>,
1018+
<varname>UseTimezone=</varname>.</para>
1019+
</listitem>
1020+
</varlistentry>
10001021
<varlistentry>
10011022
<term><varname>SendHostname=</varname></term>
10021023
<listitem>

‎src/libsystemd-network/sd-dhcp-client.c‎

Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ struct sd_dhcp_client {
6262
uint8_t *req_opts;
6363
size_t req_opts_allocated;
6464
size_t req_opts_size;
65+
bool anonymize;
6566
be32_t last_addr;
6667
uint8_t mac_addr[MAX_MAC_ADDR_LEN];
6768
size_t mac_addr_len;
@@ -116,6 +117,32 @@ static const uint8_t default_req_opts[] = {
116117
SD_DHCP_OPTION_DOMAIN_NAME_SERVER,
117118
};
118119

120+
/* RFC7844 section 3:
121+
MAY contain the Parameter Request List option.
122+
RFC7844 section 3.6:
123+
The client intending to protect its privacy SHOULD only request a
124+
minimal number of options in the PRL and SHOULD also randomly shuffle
125+
the ordering of option codes in the PRL. If this random ordering
126+
cannot be implemented, the client MAY order the option codes in the
127+
PRL by option code number (lowest to highest).
128+
*/
129+
/* NOTE: using PRL options that Windows 10 RFC7844 implementation uses */
130+
static const uint8_t default_req_opts_anonymize[] = {
131+
SD_DHCP_OPTION_SUBNET_MASK, /* 1 */
132+
SD_DHCP_OPTION_ROUTER, /* 3 */
133+
SD_DHCP_OPTION_DOMAIN_NAME_SERVER, /* 6 */
134+
SD_DHCP_OPTION_DOMAIN_NAME, /* 15 */
135+
SD_DHCP_OPTION_ROUTER_DISCOVER, /* 31 */
136+
SD_DHCP_OPTION_STATIC_ROUTE, /* 33 */
137+
SD_DHCP_OPTION_VENDOR_SPECIFIC, /* 43 */
138+
SD_DHCP_OPTION_NETBIOS_NAMESERVER, /* 44 */
139+
SD_DHCP_OPTION_NETBIOS_NODETYPE, /* 46 */
140+
SD_DHCP_OPTION_NETBIOS_SCOPE, /* 47 */
141+
SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE, /* 121 */
142+
SD_DHCP_OPTION_PRIVATE_CLASSLESS_STATIC_ROUTE, /* 249 */
143+
SD_DHCP_OPTION_PRIVATE_PROXY_AUTODISCOVERY, /* 252 */
144+
};
145+
119146
static int client_receive_message_raw(
120147
sd_event_source *s,
121148
int fd,
@@ -588,11 +615,18 @@ static int client_message_init(
588615
it MUST include that list in any subsequent DHCPREQUEST
589616
messages.
590617
*/
591-
r = dhcp_option_append(&packet->dhcp, optlen, &optoffset, 0,
592-
SD_DHCP_OPTION_PARAMETER_REQUEST_LIST,
593-
client->req_opts_size, client->req_opts);
594-
if (r < 0)
595-
return r;
618+
619+
/* RFC7844 section 3:
620+
MAY contain the Parameter Request List option. */
621+
/* NOTE: in case that there would be an option to do not send
622+
* any PRL at all, the size should be checked before sending */
623+
if (client->req_opts_size > 0) {
624+
r = dhcp_option_append(&packet->dhcp, optlen, &optoffset, 0,
625+
SD_DHCP_OPTION_PARAMETER_REQUEST_LIST,
626+
client->req_opts_size, client->req_opts);
627+
if (r < 0)
628+
return r;
629+
}
596630

597631
/* RFC2131 section 3.5:
598632
The client SHOULD include the ’maximum DHCP message size’ option to
@@ -616,12 +650,16 @@ static int client_message_init(
616650
Maximum DHCP Message Size option is the total maximum packet size,
617651
including IP and UDP headers.)
618652
*/
619-
max_size = htobe16(size);
620-
r = dhcp_option_append(&packet->dhcp, client->mtu, &optoffset, 0,
621-
SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
622-
2, &max_size);
623-
if (r < 0)
624-
return r;
653+
/* RFC7844 section 3:
654+
SHOULD NOT contain any other option. */
655+
if (!client->anonymize) {
656+
max_size = htobe16(size);
657+
r = dhcp_option_append(&packet->dhcp, client->mtu, &optoffset, 0,
658+
SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
659+
2, &max_size);
660+
if (r < 0)
661+
return r;
662+
}
625663

626664
*_optlen = optlen;
627665
*_optoffset = optoffset;
@@ -1783,7 +1821,14 @@ int sd_dhcp_client_start(sd_dhcp_client *client) {
17831821
if (r < 0)
17841822
return r;
17851823

1786-
if (client->last_addr)
1824+
/* RFC7844 section 3.3:
1825+
SHOULD perform a complete four-way handshake, starting with a
1826+
DHCPDISCOVER, to obtain a new address lease. If the client can
1827+
ascertain that this is exactly the same network to which it was
1828+
previously connected, and if the link-layer address did not change,
1829+
the client MAY issue a DHCPREQUEST to try to reclaim the current
1830+
address. */
1831+
if (client->last_addr && !client->anonymize)
17871832
client->state = DHCP_STATE_INIT_REBOOT;
17881833

17891834
r = client_start(client);
@@ -1875,7 +1920,7 @@ sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client) {
18751920
return mfree(client);
18761921
}
18771922

1878-
int sd_dhcp_client_new(sd_dhcp_client **ret) {
1923+
int sd_dhcp_client_new(sd_dhcp_client **ret, int anonymize) {
18791924
_cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL;
18801925

18811926
assert_return(ret, -EINVAL);
@@ -1892,8 +1937,15 @@ int sd_dhcp_client_new(sd_dhcp_client **ret) {
18921937
client->mtu = DHCP_DEFAULT_MIN_SIZE;
18931938
client->port = DHCP_PORT_CLIENT;
18941939

1895-
client->req_opts_size = ELEMENTSOF(default_req_opts);
1896-
client->req_opts = memdup(default_req_opts, client->req_opts_size);
1940+
client->anonymize = !!anonymize;
1941+
/* NOTE: this could be moved to a function. */
1942+
if (anonymize) {
1943+
client->req_opts_size = ELEMENTSOF(default_req_opts_anonymize);
1944+
client->req_opts = memdup(default_req_opts_anonymize, client->req_opts_size);
1945+
} else {
1946+
client->req_opts_size = ELEMENTSOF(default_req_opts);
1947+
client->req_opts = memdup(default_req_opts, client->req_opts_size);
1948+
}
18971949
if (!client->req_opts)
18981950
return -ENOMEM;
18991951

‎src/libsystemd-network/test-dhcp-client.c‎

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ static void test_request_basic(sd_event *e) {
5656
if (verbose)
5757
printf("* %s\n", __FUNCTION__);
5858

59-
r = sd_dhcp_client_new(&client);
59+
/* Initialize client without Anonymize settings. */
60+
r = sd_dhcp_client_new(&client, false);
6061

6162
assert_se(r >= 0);
6263
assert_se(client);
@@ -78,6 +79,8 @@ static void test_request_basic(sd_event *e) {
7879
SD_DHCP_OPTION_SUBNET_MASK) == -EEXIST);
7980
assert_se(sd_dhcp_client_set_request_option(client,
8081
SD_DHCP_OPTION_ROUTER) == -EEXIST);
82+
/* This PRL option is not set when using Anonymize, but in this test
83+
* Anonymize settings are not being used. */
8184
assert_se(sd_dhcp_client_set_request_option(client,
8285
SD_DHCP_OPTION_HOST_NAME) == -EEXIST);
8386
assert_se(sd_dhcp_client_set_request_option(client,
@@ -97,10 +100,49 @@ static void test_request_basic(sd_event *e) {
97100
SD_DHCP_OPTION_PARAMETER_REQUEST_LIST)
98101
== -EINVAL);
99102

100-
assert_se(sd_dhcp_client_set_request_option(client, 33) == 0);
101-
assert_se(sd_dhcp_client_set_request_option(client, 33) == -EEXIST);
102-
assert_se(sd_dhcp_client_set_request_option(client, 44) == 0);
103-
assert_se(sd_dhcp_client_set_request_option(client, 33) == -EEXIST);
103+
/* RFC7844: option 33 (SD_DHCP_OPTION_STATIC_ROUTE) is set in the
104+
* default PRL when using Anonymize, so it is changed to other option
105+
* that is not set by default, to check that it succed setting it.
106+
* Ooptions not set by default (using or not anonymize) are option 17
107+
* (SD_DHCP_OPTION_ROOT_PATH) and 42 (SD_DHCP_OPTION_NTP_SERVER) */
108+
assert_se(sd_dhcp_client_set_request_option(client, 17) == 0);
109+
assert_se(sd_dhcp_client_set_request_option(client, 17) == -EEXIST);
110+
assert_se(sd_dhcp_client_set_request_option(client, 42) == 0);
111+
assert_se(sd_dhcp_client_set_request_option(client, 17) == -EEXIST);
112+
113+
sd_dhcp_client_unref(client);
114+
}
115+
116+
static void test_request_anonymize(sd_event *e) {
117+
int r;
118+
119+
sd_dhcp_client *client;
120+
121+
if (verbose)
122+
printf("* %s\n", __FUNCTION__);
123+
124+
/* Initialize client with Anonymize settings. */
125+
r = sd_dhcp_client_new(&client, true);
126+
127+
assert_se(r >= 0);
128+
assert_se(client);
129+
130+
r = sd_dhcp_client_attach_event(client, e, 0);
131+
assert_se(r >= 0);
132+
133+
assert_se(sd_dhcp_client_set_request_option(client,
134+
SD_DHCP_OPTION_NETBIOS_NAMESERVER) == -EEXIST);
135+
/* This PRL option is not set when using Anonymize */
136+
assert_se(sd_dhcp_client_set_request_option(client,
137+
SD_DHCP_OPTION_HOST_NAME) == 0);
138+
assert_se(sd_dhcp_client_set_request_option(client,
139+
SD_DHCP_OPTION_PARAMETER_REQUEST_LIST)
140+
== -EINVAL);
141+
142+
/* RFC7844: option 101 (SD_DHCP_OPTION_NEW_TZDB_TIMEZONE) is not set in the
143+
* default PRL when using Anonymize, */
144+
assert_se(sd_dhcp_client_set_request_option(client, 101) == 0);
145+
assert_se(sd_dhcp_client_set_request_option(client, 101) == -EEXIST);
104146

105147
sd_dhcp_client_unref(client);
106148
}
@@ -236,7 +278,7 @@ static void test_discover_message(sd_event *e) {
236278
if (verbose)
237279
printf("* %s\n", __FUNCTION__);
238280

239-
r = sd_dhcp_client_new(&client);
281+
r = sd_dhcp_client_new(&client, false);
240282
assert_se(r >= 0);
241283
assert_se(client);
242284

@@ -451,7 +493,7 @@ static void test_addr_acq(sd_event *e) {
451493
if (verbose)
452494
printf("* %s\n", __FUNCTION__);
453495

454-
r = sd_dhcp_client_new(&client);
496+
r = sd_dhcp_client_new(&client, false);
455497
assert_se(r >= 0);
456498
assert_se(client);
457499

@@ -497,6 +539,7 @@ int main(int argc, char *argv[]) {
497539
assert_se(sd_event_new(&e) >= 0);
498540

499541
test_request_basic(e);
542+
test_request_anonymize(e);
500543
test_checksum();
501544

502545
test_discover_message(e);

‎src/network/networkd-dhcp4.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ int dhcp4_configure(Link *link) {
583583
assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
584584

585585
if (!link->dhcp_client) {
586-
r = sd_dhcp_client_new(&link->dhcp_client);
586+
r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
587587
if (r < 0)
588588
return r;
589589
}
@@ -624,7 +624,12 @@ int dhcp4_configure(Link *link) {
624624
return r;
625625
}
626626

627-
if (link->network->dhcp_use_routes) {
627+
/* NOTE: when using Anonymity Profiles, routes PRL options are sent
628+
* by default, so they should not be added again here. */
629+
/* NOTE: even if this variable is called "use", it also "sends" PRL
630+
* options, maybe there should be a different configuration variable
631+
* to send or not route options?. */
632+
if (link->network->dhcp_use_routes && !link->network->dhcp_anonymize) {
628633
r = sd_dhcp_client_set_request_option(link->dhcp_client,
629634
SD_DHCP_OPTION_STATIC_ROUTE);
630635
if (r < 0)

‎src/network/networkd-link.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2916,7 +2916,7 @@ static int link_load(Link *link) {
29162916
goto dhcp4_address_fail;
29172917
}
29182918

2919-
r = sd_dhcp_client_new(&link->dhcp_client);
2919+
r = sd_dhcp_client_new(&link->dhcp_client, link->network->dhcp_anonymize);
29202920
if (r < 0)
29212921
return log_link_error_errno(link, r, "Failed to create DHCPv4 client: %m");
29222922

‎src/network/networkd-network-gperf.gperf‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ DHCP.UseMTU, config_parse_bool,
101101
DHCP.UseHostname, config_parse_bool, 0, offsetof(Network, dhcp_use_hostname)
102102
DHCP.UseDomains, config_parse_dhcp_use_domains, 0, offsetof(Network, dhcp_use_domains)
103103
DHCP.UseRoutes, config_parse_bool, 0, offsetof(Network, dhcp_use_routes)
104+
DHCP.Anonymize, config_parse_bool, 0, offsetof(Network, dhcp_anonymize)
104105
DHCP.SendHostname, config_parse_bool, 0, offsetof(Network, dhcp_send_hostname)
105106
DHCP.Hostname, config_parse_hostname, 0, offsetof(Network, dhcp_hostname)
106107
DHCP.RequestBroadcast, config_parse_bool, 0, offsetof(Network, dhcp_broadcast)

‎src/network/networkd-network.c‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,46 @@ void network_config_section_free(NetworkConfigSection *cs) {
7979
free(cs);
8080
}
8181

82+
/* Set defaults following RFC7844 */
83+
void network_apply_anonymize_if_set(Network *network) {
84+
if (!network->dhcp_anonymize)
85+
return;
86+
/* RFC7844 3.7
87+
SHOULD NOT send the Host Name option */
88+
network->dhcp_send_hostname = false;
89+
/* RFC7844 section 3.:
90+
MAY contain the Client Identifier option
91+
Section 3.5:
92+
clients MUST use client identifiers based solely
93+
on the link-layer address */
94+
/* NOTE: Using MAC, as it does not reveal extra information,
95+
* and some servers might not answer if this option is not sent */
96+
network->dhcp_client_identifier = DHCP_CLIENT_ID_MAC;
97+
/* RFC 7844 3.10:
98+
SHOULD NOT use the Vendor Class Identifier option */
99+
/* NOTE: it was not initiallized to any value in network_load_one. */
100+
network->dhcp_vendor_class_identifier = false;
101+
/* RFC7844 section 3.6.:
102+
The client intending to protect its privacy SHOULD only request a
103+
minimal number of options in the PRL and SHOULD also randomly shuffle
104+
the ordering of option codes in the PRL. If this random ordering
105+
cannot be implemented, the client MAY order the option codes in the
106+
PRL by option code number (lowest to highest).
107+
*/
108+
/* NOTE: dhcp_use_mtu is false by default,
109+
* though it was not initiallized to any value in network_load_one.
110+
* Maybe there should be another var called *send*?
111+
* (to use the MTU sent by the server but to do not send
112+
* the option in the PRL). */
113+
network->dhcp_use_mtu = false;
114+
/* RFC7844 section 3.6.
115+
* same comments as previous option */
116+
network->dhcp_use_routes = false;
117+
/* RFC7844 section 3.6.
118+
* same comments as previous option */
119+
network->dhcp_use_timezone = false;
120+
}
121+
82122
static int network_load_one(Manager *manager, const char *filename) {
83123
_cleanup_network_free_ Network *network = NULL;
84124
_cleanup_fclose_ FILE *file = NULL;
@@ -161,11 +201,24 @@ static int network_load_one(Manager *manager, const char *filename) {
161201
network->dhcp_use_ntp = true;
162202
network->dhcp_use_dns = true;
163203
network->dhcp_use_hostname = true;
204+
/* NOTE: this var might be overwriten by network_apply_anonymize_if_set */
164205
network->dhcp_use_routes = true;
206+
/* NOTE: this var might be overwriten by network_apply_anonymize_if_set */
165207
network->dhcp_send_hostname = true;
208+
/* To enable/disable RFC7844 Anonymity Profiles */
209+
network->dhcp_anonymize = false;
166210
network->dhcp_route_metric = DHCP_ROUTE_METRIC;
211+
/* NOTE: this var might be overwrite by network_apply_anonymize_if_set */
167212
network->dhcp_client_identifier = DHCP_CLIENT_ID_DUID;
168213
network->dhcp_route_table = RT_TABLE_MAIN;
214+
/* NOTE: the following vars were not set to any default,
215+
* even if they are commented in the man?
216+
* These vars might be overwriten by network_apply_anonymize_if_set */
217+
network->dhcp_vendor_class_identifier = false;
218+
/* NOTE: from man: UseMTU=... Defaults to false*/
219+
network->dhcp_use_mtu = false;
220+
/* NOTE: from man: UseTimezone=... Defaults to "no".*/
221+
network->dhcp_use_timezone = false;
169222

170223
network->dhcp_server_emit_dns = true;
171224
network->dhcp_server_emit_ntp = true;
@@ -220,6 +273,8 @@ static int network_load_one(Manager *manager, const char *filename) {
220273
if (r < 0)
221274
return r;
222275

276+
network_apply_anonymize_if_set(network);
277+
223278
/* IPMasquerade=yes implies IPForward=yes */
224279
if (network->ip_masquerade)
225280
network->ip_forward |= ADDRESS_FAMILY_IPV4;

‎src/network/networkd-network.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ struct Network {
128128
unsigned dhcp_route_metric;
129129
uint32_t dhcp_route_table;
130130
uint16_t dhcp_client_port;
131+
bool dhcp_anonymize;
131132
bool dhcp_send_hostname;
132133
bool dhcp_broadcast;
133134
bool dhcp_critical;
@@ -251,6 +252,7 @@ int network_load(Manager *manager);
251252
int network_get_by_name(Manager *manager, const char *name, Network **ret);
252253
int network_get(Manager *manager, struct udev_device *device, const char *ifname, const struct ether_addr *mac, Network **ret);
253254
int network_apply(Network *network, Link *link);
255+
void network_apply_anonymize_if_set(Network *network);
254256

255257
bool network_has_static_ipv6_addresses(Network *network);
256258

0 commit comments

Comments
 (0)