Debian Codegraph @

src/libsystemd-network/test-dhcp6-client.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2014 Intel Corporation. All rights reserved.
4 ***/
5
6 #include <net/ethernet.h>
7 #include <net/if_arp.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 #include "sd-dhcp6-client.h"
13 #include "sd-dhcp6-protocol.h"
14 #include "sd-event.h"
15
16 #include "dhcp6-internal.h"
17 #include "dhcp6-lease-internal.h"
18 #include "dhcp6-protocol.h"
19 #include "fd-util.h"
20 #include "in-addr-util.h"
21 #include "memory-util.h"
22 #include "strv.h"
23 #include "tests.h"
24 #include "time-util.h"
25 #include "unaligned.h"
26
27 #define DHCP6_CLIENT_EVENT_TEST_ADVERTISED 77
28 #define IA_ID_BYTES \
29 0x0e, 0xcf, 0xa3, 0x7d
30 #define IA_NA_ADDRESS1_BYTES \
31 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x78, 0xee, 0x1c, 0xf3, 0x09, 0x3c, 0x55, 0xad
32 #define IA_NA_ADDRESS2_BYTES \
33 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x78, 0xee, 0x1c, 0xf3, 0x09, 0x3c, 0x55, 0xae
34 #define IA_PD_PREFIX1_BYTES \
35 0x2a, 0x02, 0x81, 0x0d, 0x98, 0x80, 0x37, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
36 #define IA_PD_PREFIX2_BYTES \
37 0x2a, 0x02, 0x81, 0x0d, 0x98, 0x80, 0x37, 0xc1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
38 #define DNS1_BYTES \
39 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
40 #define DNS2_BYTES \
41 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02
42 #define SNTP1_BYTES \
43 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03
44 #define SNTP2_BYTES \
45 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04
46 #define NTP1_BYTES \
47 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05
48 #define NTP2_BYTES \
49 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06
50 #define SIP1_BYTES \
51 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07
52 #define SIP2_BYTES \
53 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08
54 #define CLIENT_ID_BYTES \
55 0x00, 0x02, 0x00, 0x00, 0xab, 0x11, 0x61, 0x77, 0x40, 0xde, 0x13, 0x42, 0xc3, 0xa2
56 #define SERVER_ID_BYTES \
57 0x00, 0x01, 0x00, 0x01, 0x19, 0x40, 0x5c, 0x53, 0x78, 0x2b, 0xcb, 0xb3, 0x6d, 0x53
58 #define VENDOR_SUBOPTION_BYTES \
59 0x01
60
61 static const struct in6_addr local_address =
62 { { { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, } } };
63 static const struct in6_addr mcast_address = IN6_ADDR_ALL_DHCP6_RELAY_AGENTS_AND_SERVERS;
64 static const struct in6_addr ia_na_address1 = { { { IA_NA_ADDRESS1_BYTES } } };
65 static const struct in6_addr ia_na_address2 = { { { IA_NA_ADDRESS2_BYTES } } };
66 static const struct in6_addr ia_pd_prefix1 = { { { IA_PD_PREFIX1_BYTES } } };
67 static const struct in6_addr ia_pd_prefix2 = { { { IA_PD_PREFIX2_BYTES } } };
68 static const struct in6_addr dns1 = { { { DNS1_BYTES } } };
69 static const struct in6_addr dns2 = { { { DNS2_BYTES } } };
70 static const struct in6_addr sntp1 = { { { SNTP1_BYTES } } };
71 static const struct in6_addr sntp2 = { { { SNTP2_BYTES } } };
72 static const struct in6_addr ntp1 = { { { NTP1_BYTES } } };
73 static const struct in6_addr ntp2 = { { { NTP2_BYTES } } };
74 static const struct in6_addr sip1 = { { { SIP1_BYTES } } };
75 static const struct in6_addr sip2 = { { { SIP2_BYTES } } };
76 static const uint8_t client_id[] = { CLIENT_ID_BYTES };
77 static const uint8_t server_id[] = { SERVER_ID_BYTES };
78 static uint8_t vendor_suboption_data[] = { VENDOR_SUBOPTION_BYTES };
79 static const struct ether_addr mac = {
80 .ether_addr_octet = { 'A', 'B', 'C', '1', '2', '3' },
81 };
82 static int test_fd[2] = EBADF_PAIR;
83 static sd_dhcp6_option vendor_suboption = {
84 .n_ref = 1,
85 .enterprise_identifier = 32,
86 .option = 247,
87 .data = vendor_suboption_data,
88 .length = 1,
89 };
90 static int test_ifindex = 42;
91 static unsigned test_client_sent_message_count = 0;
92 static sd_dhcp6_client *client_ref = NULL;
93
94 TEST(client_basic) {
95 _cleanup_(sd_dhcp6_client_unrefp) sd_dhcp6_client *client = NULL;
96 int v;
97
98 assert_se(sd_dhcp6_client_new(&client) >= 0);
99 assert_se(client);
100
101 assert_se(sd_dhcp6_client_set_ifindex(client, 15) == 0);
102 assert_se(sd_dhcp6_client_set_ifindex(client, 42) >= 0);
103
104 assert_se(sd_dhcp6_client_set_mac(client, mac.ether_addr_octet, sizeof(mac), ARPHRD_ETHER) >= 0);
105
106 assert_se(sd_dhcp6_client_set_fqdn(client, "host") == 1);
107 assert_se(sd_dhcp6_client_set_fqdn(client, "host.domain") == 1);
108 assert_se(sd_dhcp6_client_set_fqdn(client, NULL) == 1);
109 assert_se(sd_dhcp6_client_set_fqdn(client, "~host") == -EINVAL);
110 assert_se(sd_dhcp6_client_set_fqdn(client, "~host.domain") == -EINVAL);
111
112 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_CLIENTID) == -EINVAL);
113 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_DNS_SERVER) >= 0);
114 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_NTP_SERVER) >= 0);
115 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_SNTP_SERVER) >= 0);
116 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_SIP_SERVER_ADDRESS) >= 0);
117 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_SIP_SERVER_DOMAIN_NAME) >= 0);
118 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_VENDOR_OPTS) >= 0);
119 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_DOMAIN) >= 0);
120 assert_se(sd_dhcp6_client_set_request_option(client, 10) == -EINVAL);
121 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_NIS_SERVER) >= 0);
122 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_NISP_SERVER) >= 0);
123 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_NIS_SERVER) == -EEXIST);
124 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_NISP_SERVER) == -EEXIST);
125
126 assert_se(sd_dhcp6_client_set_information_request(client, 1) >= 0);
127 v = 0;
128 assert_se(sd_dhcp6_client_get_information_request(client, &v) >= 0);
129 assert_se(v);
130 assert_se(sd_dhcp6_client_set_information_request(client, 0) >= 0);
131 v = 42;
132 assert_se(sd_dhcp6_client_get_information_request(client, &v) >= 0);
133 assert_se(v == 0);
134
135 v = 0;
136 assert_se(sd_dhcp6_client_get_address_request(client, &v) >= 0);
137 assert_se(v);
138 v = 0;
139 assert_se(sd_dhcp6_client_set_address_request(client, 1) >= 0);
140 assert_se(sd_dhcp6_client_get_address_request(client, &v) >= 0);
141 assert_se(v);
142 v = 42;
143 assert_se(sd_dhcp6_client_set_address_request(client, 1) >= 0);
144 assert_se(sd_dhcp6_client_get_address_request(client, &v) >= 0);
145 assert_se(v);
146
147 assert_se(sd_dhcp6_client_set_address_request(client, 1) >= 0);
148 assert_se(sd_dhcp6_client_set_prefix_delegation(client, 1) >= 0);
149 v = 0;
150 assert_se(sd_dhcp6_client_get_address_request(client, &v) >= 0);
151 assert_se(v);
152 v = 0;
153 assert_se(sd_dhcp6_client_get_prefix_delegation(client, &v) >= 0);
154 assert_se(v);
155
156 assert_se(sd_dhcp6_client_set_callback(client, NULL, NULL) >= 0);
157
158 assert_se(sd_dhcp6_client_detach_event(client) >= 0);
159 }
160
161 TEST(parse_domain) {
162 _cleanup_free_ char *domain = NULL;
163 _cleanup_strv_free_ char **list = NULL;
164 uint8_t *data;
165
166 data = (uint8_t []) { 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0 };
167 assert_se(dhcp6_option_parse_domainname(data, 13, &domain) >= 0);
168 assert_se(domain);
169 assert_se(streq(domain, "example.com"));
170 domain = mfree(domain);
171
172 data = (uint8_t []) { 4, 't', 'e', 's', 't' };
173 ASSERT_OK(dhcp6_option_parse_domainname(data, 5, &domain));
174 ASSERT_STREQ(domain, "test");
175 domain = mfree(domain);
176
177 data = (uint8_t []) { 0 };
178 assert_se(dhcp6_option_parse_domainname(data, 1, &domain) < 0);
179
180 data = (uint8_t []) { 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0,
181 6, 'f', 'o', 'o', 'b', 'a', 'r', 0 };
182 assert_se(dhcp6_option_parse_domainname_list(data, 21, &list) >= 0);
183 assert_se(list);
184 assert_se(streq(list[0], "example.com"));
185 assert_se(streq(list[1], "foobar"));
186 assert_se(!list[2]);
187 list = strv_free(list);
188
189 data = (uint8_t []) { 1, 'a', 0, 20, 'b', 'c' };
190 assert_se(dhcp6_option_parse_domainname_list(data, 6, &list) < 0);
191
192 data = (uint8_t []) { 0 , 0 };
193 assert_se(dhcp6_option_parse_domainname_list(data, 2, &list) < 0);
194 }
195
196 TEST(option) {
197 static const uint8_t packet[] = {
198 'F', 'O', 'O', 'H', 'O', 'G', 'E',
199 0x00, SD_DHCP6_OPTION_ORO, 0x00, 0x07,
200 'A', 'B', 'C', 'D', 'E', 'F', 'G',
201 0x00, SD_DHCP6_OPTION_VENDOR_CLASS, 0x00, 0x09,
202 '1', '2', '3', '4', '5', '6', '7', '8', '9',
203 'B', 'A', 'R',
204 };
205 static const uint8_t result[] = {
206 'F', 'O', 'O', 'H', 'O', 'G', 'E',
207 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
208 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
209 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
210 'B', 'A', 'R',
211 };
212 _cleanup_free_ uint8_t *buf = NULL;
213 size_t offset, pos, optlen;
214 const uint8_t *optval;
215 uint16_t optcode;
216
217 assert_se(sizeof(packet) == sizeof(result));
218
219 offset = 0;
220 assert_se(dhcp6_option_parse(packet, 0, &offset, &optcode, &optlen, &optval) == -EBADMSG);
221
222 offset = 3;
223 assert_se(dhcp6_option_parse(packet, 0, &offset, &optcode, &optlen, &optval) == -EBADMSG);
224
225 /* Tests for reading unaligned data. */
226 assert_se(buf = new(uint8_t, sizeof(packet)));
227 for (size_t i = 0; i <= 7; i++) {
228 memcpy(buf, packet + i, sizeof(packet) - i);
229 offset = 7 - i;
230 assert_se(dhcp6_option_parse(buf, sizeof(packet), &offset, &optcode, &optlen, &optval) >= 0);
231
232 assert_se(optcode == SD_DHCP6_OPTION_ORO);
233 assert_se(optlen == 7);
234 assert_se(optval == buf + 11 - i);
235 }
236
237 offset = 7;
238 assert_se(dhcp6_option_parse(packet, sizeof(packet), &offset, &optcode, &optlen, &optval) >= 0);
239
240 assert_se(optcode == SD_DHCP6_OPTION_ORO);
241 assert_se(optlen == 7);
242 assert_se(optval == packet + 11);
243
244 free(buf);
245 assert_se(buf = memdup(result, sizeof(result)));
246 pos = 7;
247 assert_se(dhcp6_option_append(&buf, &pos, optcode, optlen, optval) >= 0);
248
249 assert_se(dhcp6_option_parse(packet, sizeof(packet), &offset, &optcode, &optlen, &optval) >= 0);
250
251 assert_se(optcode == SD_DHCP6_OPTION_VENDOR_CLASS);
252 assert_se(optlen == 9);
253 assert_se(optval == packet + 22);
254
255 assert_se(dhcp6_option_append(&buf, &pos, optcode, optlen, optval) >= 0);
256
257 assert_se(memcmp(packet, buf, sizeof(packet)) == 0);
258 }
259
260 TEST(option_status) {
261 uint8_t option1[] = {
262 /* IA NA */
263 0x00, 0x03, 0x00, 0x12, 0x1a, 0x1d, 0x1a, 0x1d,
264 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0x00, 0x02,
265 /* status option */
266 0x00, 0x0d, 0x00, 0x02, 0x00, 0x01,
267 };
268 static const uint8_t option2[] = {
269 /* IA NA */
270 0x00, 0x03, 0x00, 0x2e, 0x1a, 0x1d, 0x1a, 0x1d,
271 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0x00, 0x02,
272 /* IA Addr */
273 0x00, 0x05, 0x00, 0x1e,
274 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
275 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
276 0x01, 0x02, 0x03, 0x04, 0x0a, 0x0b, 0x0c, 0x0d,
277 /* IA address status option */
278 0x00, 0x0d, 0x00, 0x02, 0x00, 0x01,
279 };
280 static const uint8_t option3[] = {
281 /* IA NA */
282 0x00, 0x03, 0x00, 0x34, 0x1a, 0x1d, 0x1a, 0x1d,
283 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0x00, 0x02,
284 /* IA Addr */
285 0x00, 0x05, 0x00, 0x24,
286 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
287 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
288 0x01, 0x02, 0x03, 0x04, 0x0a, 0x0b, 0x0c, 0x0d,
289 /* IA address status option */
290 0x00, 0x0d, 0x00, 0x08, 0x00, 0x00, 'f', 'o',
291 'o', 'b', 'a', 'r',
292 };
293 static const uint8_t option4[] = {
294 /* IA PD */
295 0x00, 0x19, 0x00, 0x2f, 0x1a, 0x1d, 0x1a, 0x1d,
296 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0x00, 0x02,
297 /* IA PD Prefix */
298 0x00, 0x1a, 0x00, 0x1f,
299 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
300 0x80, 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe,
301 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
302 0x00,
303 /* PD prefix status option */
304 0x00, 0x0d, 0x00, 0x02, 0x00, 0x00,
305 };
306 static const uint8_t option5[] = {
307 /* IA PD */
308 0x00, 0x19, 0x00, 0x52, 0x1a, 0x1d, 0x1a, 0x1d,
309 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0x00, 0x02,
310 /* IA PD Prefix #1 */
311 0x00, 0x1a, 0x00, 0x1f,
312 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
313 0x80, 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe,
314 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
315 0x00,
316 /* PD prefix status option */
317 0x00, 0x0d, 0x00, 0x02, 0x00, 0x00,
318 /* IA PD Prefix #2 */
319 0x00, 0x1a, 0x00, 0x1f,
320 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
321 0x80, 0x20, 0x01, 0x0d, 0xb8, 0xc0, 0x0l, 0xd0,
322 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
323 0x00,
324 /* PD prefix status option */
325 0x00, 0x0d, 0x00, 0x02, 0x00, 0x00,
326 };
327 _cleanup_(dhcp6_ia_freep) DHCP6IA *ia = NULL;
328 DHCP6Option *option;
329 be32_t iaid;
330 int r;
331
332 memcpy(&iaid, option1 + 4, sizeof(iaid));
333
334 option = (DHCP6Option*) option1;
335 assert_se(sizeof(option1) == sizeof(DHCP6Option) + be16toh(option->len));
336
337 r = dhcp6_option_parse_ia(NULL, 0, be16toh(option->code), be16toh(option->len), option->data, &ia);
338 assert_se(r == -ENOANO);
339
340 r = dhcp6_option_parse_ia(NULL, iaid, be16toh(option->code), be16toh(option->len), option->data, &ia);
341 assert_se(r == -EINVAL);
342
343 option->len = htobe16(17);
344 r = dhcp6_option_parse_ia(NULL, iaid, be16toh(option->code), be16toh(option->len), option->data, &ia);
345 assert_se(r == -EBADMSG);
346
347 option->len = htobe16(sizeof(DHCP6Option));
348 r = dhcp6_option_parse_ia(NULL, iaid, be16toh(option->code), be16toh(option->len), option->data, &ia);
349 assert_se(r == -EBADMSG);
350
351 option = (DHCP6Option*) option2;
352 assert_se(sizeof(option2) == sizeof(DHCP6Option) + be16toh(option->len));
353 r = dhcp6_option_parse_ia(NULL, iaid, be16toh(option->code), be16toh(option->len), option->data, &ia);
354 assert_se(r == -ENODATA);
355
356 option = (DHCP6Option*) option3;
357 assert_se(sizeof(option3) == sizeof(DHCP6Option) + be16toh(option->len));
358 r = dhcp6_option_parse_ia(NULL, iaid, be16toh(option->code), be16toh(option->len), option->data, &ia);
359 assert_se(r >= 0);
360 assert_se(ia);
361 assert_se(ia->addresses);
362 ia = dhcp6_ia_free(ia);
363
364 option = (DHCP6Option*) option4;
365 assert_se(sizeof(option4) == sizeof(DHCP6Option) + be16toh(option->len));
366 r = dhcp6_option_parse_ia(NULL, iaid, be16toh(option->code), be16toh(option->len), option->data, &ia);
367 assert_se(r >= 0);
368 assert_se(ia);
369 assert_se(ia->addresses);
370 assert_se(memcmp(&ia->header.id, &option4[4], 4) == 0);
371 assert_se(memcmp(&ia->header.lifetime_t1, &option4[8], 4) == 0);
372 assert_se(memcmp(&ia->header.lifetime_t2, &option4[12], 4) == 0);
373 ia = dhcp6_ia_free(ia);
374
375 option = (DHCP6Option*) option5;
376 assert_se(sizeof(option5) == sizeof(DHCP6Option) + be16toh(option->len));
377 r = dhcp6_option_parse_ia(NULL, iaid, be16toh(option->code), be16toh(option->len), option->data, &ia);
378 assert_se(r >= 0);
379 assert_se(ia);
380 assert_se(ia->addresses);
381 ia = dhcp6_ia_free(ia);
382 }
383
384 TEST(client_parse_message_issue_22099) {
385 static const uint8_t msg[] = {
386 /* Message type */
387 DHCP6_MESSAGE_REPLY,
388 /* Transaction ID */
389 0x7c, 0x4c, 0x16,
390 /* Rapid commit */
391 0x00, SD_DHCP6_OPTION_RAPID_COMMIT, 0x00, 0x00,
392 /* NTP servers */
393 0x00, SD_DHCP6_OPTION_NTP_SERVER, 0x00, 0x14,
394 /* NTP server (broken sub option and sub option length) */
395 0x01, 0x00, 0x10, 0x00,
396 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xde, 0x15, 0xc8, 0xff, 0xfe, 0xef, 0x1e, 0x4e,
397 /* Client ID */
398 0x00, SD_DHCP6_OPTION_CLIENTID, 0x00, 0x0e,
399 0x00, 0x02, /* DUID-EN */
400 0x00, 0x00, 0xab, 0x11, /* pen */
401 0x5c, 0x6b, 0x90, 0xec, 0xda, 0x95, 0x15, 0x45, /* id */
402 /* Server ID */
403 0x00, SD_DHCP6_OPTION_SERVERID, 0x00, 0x0a,
404 0x00, 0x03, /* DUID-LL */
405 0x00, 0x01, /* htype */
406 0xdc, 0x15, 0xc8, 0xef, 0x1e, 0x4e, /* haddr */
407 /* preference */
408 0x00, SD_DHCP6_OPTION_PREFERENCE, 0x00, 0x01,
409 0x00,
410 /* DNS servers */
411 0x00, SD_DHCP6_OPTION_DNS_SERVER, 0x00, 0x10,
412 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xde, 0x15, 0xc8, 0xff, 0xfe, 0xef, 0x1e, 0x4e,
413 /* v6 pcp server */
414 0x00, SD_DHCP6_OPTION_V6_PCP_SERVER, 0x00, 0x10,
415 0x2a, 0x02, 0x81, 0x0d, 0x98, 0x80, 0x37, 0x00, 0xde, 0x15, 0xc8, 0xff, 0xfe, 0xef, 0x1e, 0x4e,
416 /* IA_NA */
417 0x00, SD_DHCP6_OPTION_IA_NA, 0x00, 0x28,
418 0xcc, 0x59, 0x11, 0x7b, /* iaid */
419 0x00, 0x00, 0x07, 0x08, /* lifetime T1 */
420 0x00, 0x00, 0x0b, 0x40, /* lifetime T2 */
421 /* IA_NA (iaaddr suboption) */
422 0x00, SD_DHCP6_OPTION_IAADDR, 0x00, 0x18,
423 0x2a, 0x02, 0x81, 0x0d, 0x98, 0x80, 0x37, 0x00, 0x6a, 0x05, 0xca, 0xff, 0xfe, 0xf1, 0x51, 0x53, /* address */
424 0x00, 0x00, 0x0e, 0x10, /* preferred lifetime */
425 0x00, 0x00, 0x1c, 0x20, /* valid lifetime */
426 /* IA_PD */
427 0x00, SD_DHCP6_OPTION_IA_PD, 0x00, 0x29,
428 0xcc, 0x59, 0x11, 0x7b, /* iaid */
429 0x00, 0x00, 0x07, 0x08, /* lifetime T1 */
430 0x00, 0x00, 0x0b, 0x40, /* lifetime T2 */
431 /* IA_PD (iaprefix suboption) */
432 0x00, SD_DHCP6_OPTION_IA_PD_PREFIX, 0x00, 0x19,
433 0x00, 0x00, 0x0e, 0x10, /* preferred lifetime */
434 0x00, 0x00, 0x1c, 0x20, /* valid lifetime */
435 0x3a, /* prefixlen */
436 0x2a, 0x02, 0x81, 0x0d, 0x98, 0x80, 0x37, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* prefix */
437 };
438 static const uint8_t duid[] = {
439 0x00, 0x00, 0xab, 0x11, 0x5c, 0x6b, 0x90, 0xec, 0xda, 0x95, 0x15, 0x45,
440 };
441 _cleanup_(sd_dhcp6_client_unrefp) sd_dhcp6_client *client = NULL;
442 _cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL;
443
444 assert_se(sd_dhcp6_client_new(&client) >= 0);
445 assert_se(sd_dhcp6_client_set_iaid(client, 0xcc59117b) >= 0);
446 assert_se(sd_dhcp6_client_set_duid_raw(client, 2, duid, sizeof(duid)) >= 0);
447
448 assert_se(dhcp6_lease_new_from_message(client, (const DHCP6Message*) msg, sizeof(msg), NULL, NULL, &lease) >= 0);
449 }
450
451 TEST(client_parse_message_issue_24002) {
452 static const uint8_t msg[] = {
453 /* Message Type */
454 0x07,
455 /* Transaction ID */
456 0x0e, 0xa5, 0x7c,
457 /* Client ID */
458 0x00, SD_DHCP6_OPTION_CLIENTID, 0x00, 0x0e,
459 0x00, 0x02, /* DUID-EN */
460 0x00, 0x00, 0xab, 0x11, /* pen */
461 0x5c, 0x6b, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, /* id */
462 /* Server ID */
463 0x00, 0x02, 0x00, 0x1a,
464 0x00, 0x02, 0x00, 0x00, 0x05, 0x83, 0x30, 0x63, 0x3a, 0x38, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55,
465 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
466 /* IA_PD */
467 0x00, 0x19, 0x00, 0x29,
468 0xaa, 0xbb, 0xcc, 0xdd, /* iaid */
469 0x00, 0x00, 0x03, 0x84, /* lifetime (T1) */
470 0x00, 0x00, 0x05, 0xa0, /* lifetime (T2) */
471 /* IA_PD (iaprefix suboption) */
472 0x00, 0x1a, 0x00, 0x19,
473 0x00, 0x00, 0x07, 0x08, /* preferred lifetime */
474 0x00, 0x00, 0x38, 0x40, /* valid lifetime */
475 0x38, /* prefixlen */
476 0x20, 0x03, 0x00, 0xff, 0xaa, 0xbb, 0xcc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* prefix */
477 /* Rapid commit */
478 0x00, 0x0e, 0x00, 0x00,
479 /* Trailing invalid byte at the end. See issue #28183. */
480 00,
481 };
482 static const uint8_t duid[] = {
483 0x00, 0x00, 0xab, 0x11, 0x5c, 0x6b, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
484 };
485 _cleanup_(sd_dhcp6_client_unrefp) sd_dhcp6_client *client = NULL;
486 _cleanup_(sd_dhcp6_lease_unrefp) sd_dhcp6_lease *lease = NULL;
487
488 assert_se(sd_dhcp6_client_new(&client) >= 0);
489 assert_se(sd_dhcp6_client_set_iaid(client, 0xaabbccdd) >= 0);
490 assert_se(sd_dhcp6_client_set_duid_raw(client, 2, duid, sizeof(duid)) >= 0);
491
492 assert_se(dhcp6_lease_new_from_message(client, (const DHCP6Message*) msg, sizeof(msg), NULL, NULL, &lease) >= 0);
493 }
494
495 static const uint8_t msg_information_request[] = {
496 /* Message type */
497 DHCP6_MESSAGE_INFORMATION_REQUEST,
498 /* Transaction ID */
499 0x0f, 0xb4, 0xe5,
500 /* MUD URL */
501 /* ORO */
502 0x00, SD_DHCP6_OPTION_ORO, 0x00, 0x0c,
503 0x00, SD_DHCP6_OPTION_DNS_SERVER,
504 0x00, SD_DHCP6_OPTION_DOMAIN,
505 0x00, SD_DHCP6_OPTION_SNTP_SERVER,
506 0x00, SD_DHCP6_OPTION_INFORMATION_REFRESH_TIME,
507 0x00, SD_DHCP6_OPTION_NTP_SERVER,
508 0x00, SD_DHCP6_OPTION_INF_MAX_RT,
509 /* Client ID */
510 0x00, SD_DHCP6_OPTION_CLIENTID, 0x00, 0x0e,
511 CLIENT_ID_BYTES,
512 /* Extra options */
513 /* Elapsed time */
514 0x00, SD_DHCP6_OPTION_ELAPSED_TIME, 0x00, 0x02,
515 0x00, 0x00,
516 };
517
518 static const uint8_t msg_solicit[] = {
519 /* Message type */
520 DHCP6_MESSAGE_SOLICIT,
521 /* Transaction ID */
522 0x0f, 0xb4, 0xe5,
523 /* Rapid commit */
524 0x00, SD_DHCP6_OPTION_RAPID_COMMIT, 0x00, 0x00,
525 /* IA_NA */
526 0x00, SD_DHCP6_OPTION_IA_NA, 0x00, 0x0c,
527 IA_ID_BYTES,
528 0x00, 0x00, 0x00, 0x00, /* lifetime T1 */
529 0x00, 0x00, 0x00, 0x00, /* lifetime T2 */
530 /* IA_PD */
531 0x00, SD_DHCP6_OPTION_IA_PD, 0x00, 0x0c,
532 IA_ID_BYTES,
533 0x00, 0x00, 0x00, 0x00, /* lifetime T1 */
534 0x00, 0x00, 0x00, 0x00, /* lifetime T2 */
535 /* Client FQDN */
536 0x00, SD_DHCP6_OPTION_CLIENT_FQDN, 0x00, 0x11,
537 DHCP6_FQDN_FLAG_S,
538 0x04, 'h', 'o', 's', 't', 0x03, 'l', 'a', 'b', 0x05, 'i', 'n', 't', 'r', 'a', 0x00,
539 /* User Class */
540 /* Vendor Class */
541 /* Vendor Options */
542 /* MUD URL */
543 /* ORO */
544 0x00, SD_DHCP6_OPTION_ORO, 0x00, 0x0a,
545 0x00, SD_DHCP6_OPTION_DNS_SERVER,
546 0x00, SD_DHCP6_OPTION_DOMAIN,
547 0x00, SD_DHCP6_OPTION_SNTP_SERVER,
548 0x00, SD_DHCP6_OPTION_NTP_SERVER,
549 0x00, SD_DHCP6_OPTION_SOL_MAX_RT,
550 /* Client ID */
551 0x00, SD_DHCP6_OPTION_CLIENTID, 0x00, 0x0e,
552 CLIENT_ID_BYTES,
553 /* Extra options */
554 /* Elapsed time */
555 0x00, SD_DHCP6_OPTION_ELAPSED_TIME, 0x00, 0x02,
556 0x00, 0x00,
557 };
558
559 static const uint8_t msg_request[] = {
560 /* Message type */
561 DHCP6_MESSAGE_REQUEST,
562 /* Transaction ID */
563 0x00, 0x00, 0x00,
564 /* Server ID */
565 0x00, SD_DHCP6_OPTION_SERVERID, 0x00, 0x0e,
566 SERVER_ID_BYTES,
567 /* IA_NA */
568 0x00, SD_DHCP6_OPTION_IA_NA, 0x00, 0x44,
569 IA_ID_BYTES,
570 0x00, 0x00, 0x00, 0x00, /* lifetime T1 */
571 0x00, 0x00, 0x00, 0x00, /* lifetime T2 */
572 /* IA_NA (IAADDR suboption) */
573 0x00, SD_DHCP6_OPTION_IAADDR, 0x00, 0x18,
574 IA_NA_ADDRESS1_BYTES,
575 0x00, 0x00, 0x00, 0x00, /* preferred lifetime */
576 0x00, 0x00, 0x00, 0x00, /* valid lifetime */
577 /* IA_NA (IAADDR suboption) */
578 0x00, SD_DHCP6_OPTION_IAADDR, 0x00, 0x18,
579 IA_NA_ADDRESS2_BYTES,
580 0x00, 0x00, 0x00, 0x00, /* preferred lifetime */
581 0x00, 0x00, 0x00, 0x00, /* valid lifetime */
582 /* IA_PD */
583 0x00, SD_DHCP6_OPTION_IA_PD, 0x00, 0x46,
584 IA_ID_BYTES,
585 0x00, 0x00, 0x00, 0x00, /* lifetime T1 */
586 0x00, 0x00, 0x00, 0x00, /* lifetime T2 */
587 /* IA_PD (IA_PD_PREFIX suboption) */
588 0x00, SD_DHCP6_OPTION_IA_PD_PREFIX, 0x00, 0x19,
589 0x00, 0x00, 0x00, 0x00, /* preferred lifetime */
590 0x00, 0x00, 0x00, 0x00, /* valid lifetime */
591 0x40, /* prefixlen */
592 IA_PD_PREFIX1_BYTES,
593 /* IA_PD (IA_PD_PREFIX suboption) */
594 0x00, SD_DHCP6_OPTION_IA_PD_PREFIX, 0x00, 0x19,
595 0x00, 0x00, 0x00, 0x00, /* preferred lifetime */
596 0x00, 0x00, 0x00, 0x00, /* valid lifetime */
597 0x40, /* prefixlen */
598 IA_PD_PREFIX2_BYTES,
599 /* Client FQDN */
600 0x00, SD_DHCP6_OPTION_CLIENT_FQDN, 0x00, 0x11,
601 DHCP6_FQDN_FLAG_S,
602 0x04, 'h', 'o', 's', 't', 0x03, 'l', 'a', 'b', 0x05, 'i', 'n', 't', 'r', 'a', 0x00,
603 /* User Class */
604 /* Vendor Class */
605 /* Vendor Options */
606 /* MUD URL */
607 /* ORO */
608 0x00, SD_DHCP6_OPTION_ORO, 0x00, 0x08,
609 0x00, SD_DHCP6_OPTION_DNS_SERVER,
610 0x00, SD_DHCP6_OPTION_DOMAIN,
611 0x00, SD_DHCP6_OPTION_SNTP_SERVER,
612 0x00, SD_DHCP6_OPTION_NTP_SERVER,
613 /* Client ID */
614 0x00, SD_DHCP6_OPTION_CLIENTID, 0x00, 0x0e,
615 CLIENT_ID_BYTES,
616 /* Extra options */
617 /* Elapsed time */
618 0x00, SD_DHCP6_OPTION_ELAPSED_TIME, 0x00, 0x02,
619 0x00, 0x00,
620 };
621
622 /* RFC 3315 section 18.1.6. The DHCP6 Release message must include:
623 - transaction id
624 - server identifier
625 - client identifier
626 - all released IA with addresses included
627 - elapsed time (required for all messages).
628 All other options aren't required. */
629 static const uint8_t msg_release[] = {
630 /* Message type */
631 DHCP6_MESSAGE_RELEASE,
632 /* Transaction ID */
633 0x00, 0x00, 0x00,
634 /* Server ID */
635 0x00, SD_DHCP6_OPTION_SERVERID, 0x00, 0x0e,
636 SERVER_ID_BYTES,
637 /* IA_NA */
638 0x00, SD_DHCP6_OPTION_IA_NA, 0x00, 0x44,
639 IA_ID_BYTES,
640 0x00, 0x00, 0x00, 0x00, /* lifetime T1 */
641 0x00, 0x00, 0x00, 0x00, /* lifetime T2 */
642 /* IA_NA (IAADDR suboption) */
643 0x00, SD_DHCP6_OPTION_IAADDR, 0x00, 0x18,
644 IA_NA_ADDRESS1_BYTES,
645 0x00, 0x00, 0x00, 0x00, /* preferred lifetime */
646 0x00, 0x00, 0x00, 0x00, /* valid lifetime */
647 /* IA_NA (IAADDR suboption) */
648 0x00, SD_DHCP6_OPTION_IAADDR, 0x00, 0x18,
649 IA_NA_ADDRESS2_BYTES,
650 0x00, 0x00, 0x00, 0x00, /* preferred lifetime */
651 0x00, 0x00, 0x00, 0x00, /* valid lifetime */
652 /* IA_PD */
653 0x00, SD_DHCP6_OPTION_IA_PD, 0x00, 0x46,
654 IA_ID_BYTES,
655 0x00, 0x00, 0x00, 0x00, /* lifetime T1 */
656 0x00, 0x00, 0x00, 0x00, /* lifetime T2 */
657 /* IA_PD (IA_PD_PREFIX suboption) */
658 0x00, SD_DHCP6_OPTION_IA_PD_PREFIX, 0x00, 0x19,
659 0x00, 0x00, 0x00, 0x00, /* preferred lifetime */
660 0x00, 0x00, 0x00, 0x00, /* valid lifetime */
661 0x40, /* prefixlen */
662 IA_PD_PREFIX1_BYTES,
663 /* IA_PD (IA_PD_PREFIX suboption) */
664 0x00, SD_DHCP6_OPTION_IA_PD_PREFIX, 0x00, 0x19,
665 0x00, 0x00, 0x00, 0x00, /* preferred lifetime */
666 0x00, 0x00, 0x00, 0x00, /* valid lifetime */
667 0x40, /* prefixlen */
668 IA_PD_PREFIX2_BYTES,
669 /* Client ID */
670 0x00, SD_DHCP6_OPTION_CLIENTID, 0x00, 0x0e,
671 CLIENT_ID_BYTES,
672 /* Extra options */
673 /* Elapsed time */
674 0x00, SD_DHCP6_OPTION_ELAPSED_TIME, 0x00, 0x02,
675 0x00, 0x00,
676 };
677
678 static const uint8_t msg_reply[] = {
679 /* Message type */
680 DHCP6_MESSAGE_REPLY,
681 /* Transaction ID */
682 0x0f, 0xb4, 0xe5,
683 /* Client ID */
684 0x00, SD_DHCP6_OPTION_CLIENTID, 0x00, 0x0e,
685 CLIENT_ID_BYTES,
686 /* Server ID */
687 0x00, SD_DHCP6_OPTION_SERVERID, 0x00, 0x0e,
688 SERVER_ID_BYTES,
689 /* Rapid commit */
690 0x00, SD_DHCP6_OPTION_RAPID_COMMIT, 0x00, 0x01,
691 0x00,
692 /* IA_NA */
693 0x00, SD_DHCP6_OPTION_IA_NA, 0x00, 0x66,
694 IA_ID_BYTES,
695 0x00, 0x00, 0x00, 0x50, /* lifetime T1 */
696 0x00, 0x00, 0x00, 0x78, /* lifetime T2 */
697 /* IA_NA (IAADDR suboption) */
698 0x00, SD_DHCP6_OPTION_IAADDR, 0x00, 0x18,
699 IA_NA_ADDRESS2_BYTES,
700 0x00, 0x00, 0x00, 0x96, /* preferred lifetime */
701 0x00, 0x00, 0x00, 0xb4, /* valid lifetime */
702 /* IA_NA (IAADDR suboption) */
703 0x00, SD_DHCP6_OPTION_IAADDR, 0x00, 0x18,
704 IA_NA_ADDRESS1_BYTES,
705 0x00, 0x00, 0x00, 0x96, /* preferred lifetime */
706 0x00, 0x00, 0x00, 0xb4, /* valid lifetime */
707 /* IA_NA (status code suboption) */
708 0x00, SD_DHCP6_OPTION_STATUS_CODE, 0x00, 0x1e,
709 0x00, 0x00, /* status code */
710 0x41, 0x6c, 0x6c, 0x20, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x65, 0x73, 0x20, 0x77, 0x65,
711 0x72, 0x65, 0x20, 0x61, 0x73, 0x73, 0x69, 0x67, 0x6e, 0x65, 0x64, 0x2e, /* status message */
712 /* IA_PD */
713 0x00, SD_DHCP6_OPTION_IA_PD, 0x00, 0x46,
714 IA_ID_BYTES,
715 0x00, 0x00, 0x00, 0x50, /* lifetime T1 */
716 0x00, 0x00, 0x00, 0x78, /* lifetime T2 */
717 /* IA_PD (IA_PD_PREFIX suboption) */
718 0x00, SD_DHCP6_OPTION_IA_PD_PREFIX, 0x00, 0x19,
719 0x00, 0x00, 0x00, 0x96, /* preferred lifetime */
720 0x00, 0x00, 0x00, 0xb4, /* valid lifetime */
721 0x40, /* prefixlen */
722 IA_PD_PREFIX2_BYTES,
723 /* IA_PD (IA_PD_PREFIX suboption) */
724 0x00, SD_DHCP6_OPTION_IA_PD_PREFIX, 0x00, 0x19,
725 0x00, 0x00, 0x00, 0x96, /* preferred lifetime */
726 0x00, 0x00, 0x00, 0xb4, /* valid lifetime */
727 0x40, /* prefixlen */
728 IA_PD_PREFIX1_BYTES,
729 /* DNS servers */
730 0x00, SD_DHCP6_OPTION_DNS_SERVER, 0x00, 0x20,
731 DNS1_BYTES,
732 DNS2_BYTES,
733 /* SNTP servers */
734 0x00, SD_DHCP6_OPTION_SNTP_SERVER, 0x00, 0x20,
735 SNTP1_BYTES,
736 SNTP2_BYTES,
737 /* NTP servers */
738 0x00, SD_DHCP6_OPTION_NTP_SERVER, 0x00, 0x37,
739 /* NTP server (address suboption) */
740 0x00, DHCP6_NTP_SUBOPTION_SRV_ADDR, 0x00, 0x10,
741 NTP1_BYTES,
742 /* NTP server (address suboption) */
743 0x00, DHCP6_NTP_SUBOPTION_SRV_ADDR, 0x00, 0x10,
744 NTP2_BYTES,
745 /* NTP server (fqdn suboption) */
746 0x00, DHCP6_NTP_SUBOPTION_SRV_FQDN, 0x00, 0x0b,
747 0x03, 'n', 't', 'p', 0x05, 'i', 'n', 't', 'r', 'a', 0x00,
748 /* SIP server addresses */
749 0x00, SD_DHCP6_OPTION_SIP_SERVER_ADDRESS, 0x00, 0x20,
750 SIP1_BYTES,
751 SIP2_BYTES,
752 /* SIP server domains */
753 0x00, SD_DHCP6_OPTION_SIP_SERVER_DOMAIN_NAME, 0x00, 0x0b,
754 0x03, 's', 'i', 'p', 0x05, 'i', 'n', 't', 'r', 'a', 0x00,
755 /* Domain list */
756 0x00, SD_DHCP6_OPTION_DOMAIN, 0x00, 0x0b,
757 0x03, 'l', 'a', 'b', 0x05, 'i', 'n', 't', 'r', 'a', 0x00,
758 /* Client FQDN */
759 0x00, SD_DHCP6_OPTION_CLIENT_FQDN, 0x00, 0x13,
760 0x01, 0x06, 'c', 'l', 'i', 'e', 'n', 't', 0x03, 'l', 'a', 'b', 0x05, 'i', 'n', 't', 'r', 'a', 0x00,
761 /* Vendor specific options */
762 0x00, SD_DHCP6_OPTION_VENDOR_OPTS, 0x00, 0x09,
763 0x00, 0x00, 0x00, 0x20, 0x00, 0xf7, 0x00, 0x01, VENDOR_SUBOPTION_BYTES,
764 };
765
766 static const uint8_t msg_advertise[] = {
767 /* Message type */
768 DHCP6_MESSAGE_ADVERTISE,
769 /* Transaction ID */
770 0x0f, 0xb4, 0xe5,
771 /* Client ID */
772 0x00, SD_DHCP6_OPTION_CLIENTID, 0x00, 0x0e,
773 CLIENT_ID_BYTES,
774 /* Server ID */
775 0x00, SD_DHCP6_OPTION_SERVERID, 0x00, 0x0e,
776 SERVER_ID_BYTES,
777 /* Preference */
778 0x00, SD_DHCP6_OPTION_PREFERENCE, 0x00, 0x01,
779 0xff,
780 /* IA_NA */
781 0x00, SD_DHCP6_OPTION_IA_NA, 0x00, 0x7a,
782 IA_ID_BYTES,
783 0x00, 0x00, 0x00, 0x50, /* lifetime T1 */
784 0x00, 0x00, 0x00, 0x78, /* lifetime T2 */
785 /* IA_NA (IAADDR suboption) */
786 0x00, SD_DHCP6_OPTION_IAADDR, 0x00, 0x18,
787 IA_NA_ADDRESS2_BYTES, /* address */
788 0x00, 0x00, 0x00, 0x96, /* preferred lifetime */
789 0x00, 0x00, 0x00, 0xb4, /* valid lifetime */
790 /* IA_NA (IAADDR suboption) */
791 0x00, SD_DHCP6_OPTION_IAADDR, 0x00, 0x18,
792 IA_NA_ADDRESS1_BYTES, /* address */
793 0x00, 0x00, 0x00, 0x96, /* preferred lifetime */
794 0x00, 0x00, 0x00, 0xb4, /* valid lifetime */
795 /* IA_NA (status code suboption) */
796 0x00, SD_DHCP6_OPTION_STATUS_CODE, 0x00, 0x32,
797 0x00, 0x00, /* status code */
798 0x41, 0x64, 0x64, 0x72, 0x65, 0x73, 0x73, 0x28, 0x65, 0x73, 0x29, 0x20, 0x72, 0x65, 0x6e, 0x65,
799 0x77, 0x65, 0x64, 0x2e, 0x20, 0x47, 0x72, 0x65, 0x65, 0x74, 0x69, 0x6e, 0x67, 0x73, 0x20, 0x66,
800 0x72, 0x6f, 0x6d, 0x20, 0x70, 0x6c, 0x61, 0x6e, 0x65, 0x74, 0x20, 0x45, 0x61, 0x72, 0x74, 0x68, /* status message */
801 /* IA_PD */
802 0x00, SD_DHCP6_OPTION_IA_PD, 0x00, 0x46,
803 IA_ID_BYTES,
804 0x00, 0x00, 0x00, 0x50, /* lifetime T1 */
805 0x00, 0x00, 0x00, 0x78, /* lifetime T2 */
806 /* IA_PD (IA_PD_PREFIX suboption) */
807 0x00, SD_DHCP6_OPTION_IA_PD_PREFIX, 0x00, 0x19,
808 0x00, 0x00, 0x00, 0x96, /* preferred lifetime */
809 0x00, 0x00, 0x00, 0xb4, /* valid lifetime */
810 0x40, /* prefixlen */
811 IA_PD_PREFIX2_BYTES,
812 /* IA_PD (IA_PD_PREFIX suboption) */
813 0x00, SD_DHCP6_OPTION_IA_PD_PREFIX, 0x00, 0x19,
814 0x00, 0x00, 0x00, 0x96, /* preferred lifetime */
815 0x00, 0x00, 0x00, 0xb4, /* valid lifetime */
816 0x40, /* prefixlen */
817 IA_PD_PREFIX1_BYTES,
818 /* DNS servers */
819 0x00, SD_DHCP6_OPTION_DNS_SERVER, 0x00, 0x20,
820 DNS1_BYTES,
821 DNS2_BYTES,
822 /* SNTP servers */
823 0x00, SD_DHCP6_OPTION_SNTP_SERVER, 0x00, 0x20,
824 SNTP1_BYTES,
825 SNTP2_BYTES,
826 /* NTP servers */
827 0x00, SD_DHCP6_OPTION_NTP_SERVER, 0x00, 0x37,
828 /* NTP server (address suboption) */
829 0x00, DHCP6_NTP_SUBOPTION_SRV_ADDR, 0x00, 0x10,
830 NTP1_BYTES,
831 /* NTP server (address suboption) */
832 0x00, DHCP6_NTP_SUBOPTION_SRV_ADDR, 0x00, 0x10,
833 NTP2_BYTES,
834 /* NTP server (fqdn suboption) */
835 0x00, DHCP6_NTP_SUBOPTION_SRV_FQDN, 0x00, 0x0b,
836 0x03, 'n', 't', 'p', 0x05, 'i', 'n', 't', 'r', 'a', 0x00,
837 /* SIP server addresses */
838 0x00, SD_DHCP6_OPTION_SIP_SERVER_ADDRESS, 0x00, 0x20,
839 SIP1_BYTES,
840 SIP2_BYTES,
841 /* SIP server domains */
842 0x00, SD_DHCP6_OPTION_SIP_SERVER_DOMAIN_NAME, 0x00, 0x0b,
843 0x03, 's', 'i', 'p', 0x05, 'i', 'n', 't', 'r', 'a', 0x00,
844 /* Domain list */
845 0x00, SD_DHCP6_OPTION_DOMAIN, 0x00, 0x0b,
846 0x03, 'l', 'a', 'b', 0x05, 'i', 'n', 't', 'r', 'a', 0x00,
847 /* Client FQDN */
848 0x00, SD_DHCP6_OPTION_CLIENT_FQDN, 0x00, 0x13,
849 0x01, 0x06, 'c', 'l', 'i', 'e', 'n', 't', 0x03, 'l', 'a', 'b', 0x05, 'i', 'n', 't', 'r', 'a', 0x00,
850 /* Vendor specific options */
851 0x00, SD_DHCP6_OPTION_VENDOR_OPTS, 0x00, 0x09,
852 0x00, 0x00, 0x00, 0x20, 0x00, 0xf7, 0x00, 0x01, VENDOR_SUBOPTION_BYTES,
853 };
854
855 static void test_client_verify_information_request(const DHCP6Message *msg, size_t len) {
856 log_debug("/* %s */", __func__);
857
858 assert_se(len == sizeof(msg_information_request));
859 /* The elapsed time value is not deterministic. Skip it. */
860 assert_se(memcmp(msg, msg_information_request, len - sizeof(be16_t)) == 0);
861 }
862
863 static void test_client_verify_solicit(const DHCP6Message *msg, size_t len) {
864 log_debug("/* %s */", __func__);
865
866 assert_se(len == sizeof(msg_solicit));
867 /* The elapsed time value is not deterministic. Skip it. */
868 assert_se(memcmp(msg, msg_solicit, len - sizeof(be16_t)) == 0);
869 }
870
871 static void test_client_verify_release(const DHCP6Message *msg, size_t len) {
872 log_debug("/* %s */", __func__);
873
874 assert_se(len == sizeof(msg_release));
875 assert_se(msg->type == DHCP6_MESSAGE_RELEASE);
876 /* The transaction ID and elapsed time value are not deterministic. Skip them. */
877 assert_se(memcmp(msg->options, msg_release + offsetof(DHCP6Message, options),
878 len - offsetof(DHCP6Message, options) - sizeof(be16_t)) == 0);
879 }
880
881 static void test_client_verify_request(const DHCP6Message *msg, size_t len) {
882 log_debug("/* %s */", __func__);
883
884 assert_se(len == sizeof(msg_request));
885 assert_se(msg->type == DHCP6_MESSAGE_REQUEST);
886 /* The transaction ID and elapsed time value are not deterministic. Skip them. */
887 assert_se(memcmp(msg->options, msg_request + offsetof(DHCP6Message, options),
888 len - offsetof(DHCP6Message, options) - sizeof(be16_t)) == 0);
889 }
890
891 static void test_lease_common(sd_dhcp6_client *client) {
892 sd_dhcp6_lease *lease;
893 sd_dhcp6_option **suboption;
894 const struct in6_addr *addrs;
895 const char *str;
896 char **strv;
897 uint8_t *id;
898 size_t len;
899
900 assert_se(sd_dhcp6_client_get_lease(client, &lease) >= 0);
901
902 assert_se(dhcp6_lease_get_clientid(lease, &id, &len) >= 0);
903 assert_se(memcmp_nn(id, len, client_id, sizeof(client_id)) == 0);
904
905 assert_se(sd_dhcp6_lease_get_domains(lease, &strv) == 1);
906 assert_se(streq(strv[0], "lab.intra"));
907 assert_se(!strv[1]);
908
909 assert_se(sd_dhcp6_lease_get_fqdn(lease, &str) >= 0);
910 assert_se(streq(str, "client.lab.intra"));
911
912 assert_se(sd_dhcp6_lease_get_dns(lease, &addrs) == 2);
913 assert_se(in6_addr_equal(&addrs[0], &dns1));
914 assert_se(in6_addr_equal(&addrs[1], &dns2));
915
916 assert_se(sd_dhcp6_lease_get_ntp_addrs(lease, &addrs) == 2);
917 assert_se(in6_addr_equal(&addrs[0], &ntp1));
918 assert_se(in6_addr_equal(&addrs[1], &ntp2));
919
920 assert_se(sd_dhcp6_lease_get_ntp_fqdn(lease, &strv) == 1);
921 assert_se(streq(strv[0], "ntp.intra"));
922 assert_se(!strv[1]);
923
924 assert_se(sd_dhcp6_lease_get_sip_addrs(lease, &addrs) == 2);
925 assert_se(in6_addr_equal(&addrs[0], &sip1));
926 assert_se(in6_addr_equal(&addrs[1], &sip2));
927
928 assert_se(sd_dhcp6_lease_get_sip_domains(lease, &strv) == 1);
929 assert_se(streq(strv[0], "sip.intra"));
930 assert_se(!strv[1]);
931
932 assert_se(lease->sntp_count == 2);
933 assert_se(in6_addr_equal(&lease->sntp[0], &sntp1));
934 assert_se(in6_addr_equal(&lease->sntp[1], &sntp2));
935
936 assert_se(sd_dhcp6_lease_get_vendor_options(lease, &suboption) > 0);
937 assert_se((*suboption)->enterprise_identifier == vendor_suboption.enterprise_identifier);
938 assert_se((*suboption)->option == vendor_suboption.option);
939 assert_se(*(uint8_t*)(*suboption)->data == *(uint8_t*)vendor_suboption.data);
940 }
941
942 static void test_lease_managed(sd_dhcp6_client *client) {
943 sd_dhcp6_lease *lease;
944 struct in6_addr addr;
945 usec_t lt_pref, lt_valid;
946 uint8_t *id, prefixlen;
947 size_t len;
948
949 assert_se(sd_dhcp6_client_get_lease(client, &lease) >= 0);
950
951 assert_se(dhcp6_lease_get_serverid(lease, &id, &len) >= 0);
952 assert_se(memcmp_nn(id, len, server_id, sizeof(server_id)) == 0);
953
954 assert_se(sd_dhcp6_lease_has_address(lease));
955 assert_se(sd_dhcp6_lease_has_pd_prefix(lease));
956
957 for (unsigned i = 0; i < 2; i++) {
958 assert_se(sd_dhcp6_lease_address_iterator_reset(lease));
959 assert_se(sd_dhcp6_lease_get_address(lease, &addr) >= 0);
960 assert_se(sd_dhcp6_lease_get_address_lifetime(lease, &lt_pref, &lt_valid) >= 0);
961 assert_se(in6_addr_equal(&addr, &ia_na_address1));
962 assert_se(lt_pref == 150 * USEC_PER_SEC);
963 assert_se(lt_valid == 180 * USEC_PER_SEC);
964 assert_se(sd_dhcp6_lease_address_iterator_next(lease));
965 assert_se(sd_dhcp6_lease_get_address(lease, &addr) >= 0);
966 assert_se(sd_dhcp6_lease_get_address_lifetime(lease, &lt_pref, &lt_valid) >= 0);
967 assert_se(in6_addr_equal(&addr, &ia_na_address2));
968 assert_se(lt_pref == 150 * USEC_PER_SEC);
969 assert_se(lt_valid == 180 * USEC_PER_SEC);
970 assert_se(!sd_dhcp6_lease_address_iterator_next(lease));
971
972 assert_se(sd_dhcp6_lease_pd_iterator_reset(lease));
973 assert_se(sd_dhcp6_lease_get_pd_prefix(lease, &addr, &prefixlen) >= 0);
974 assert_se(sd_dhcp6_lease_get_pd_lifetime(lease, &lt_pref, &lt_valid) >= 0);
975 assert_se(in6_addr_equal(&addr, &ia_pd_prefix1));
976 assert_se(prefixlen == 64);
977 assert_se(lt_pref == 150 * USEC_PER_SEC);
978 assert_se(lt_valid == 180 * USEC_PER_SEC);
979 assert_se(sd_dhcp6_lease_pd_iterator_next(lease));
980 assert_se(sd_dhcp6_lease_get_pd_prefix(lease, &addr, &prefixlen) >= 0);
981 assert_se(sd_dhcp6_lease_get_pd_lifetime(lease, &lt_pref, &lt_valid) >= 0);
982 assert_se(in6_addr_equal(&addr, &ia_pd_prefix2));
983 assert_se(prefixlen == 64);
984 assert_se(lt_pref == 150 * USEC_PER_SEC);
985 assert_se(lt_valid == 180 * USEC_PER_SEC);
986 assert_se(!sd_dhcp6_lease_pd_iterator_next(lease));
987 }
988
989 test_lease_common(client);
990 }
991
992 static void test_client_callback(sd_dhcp6_client *client, int event, void *userdata) {
993 switch (event) {
994 case SD_DHCP6_CLIENT_EVENT_STOP:
995 log_debug("/* %s (event=stop) */", __func__);
996 return;
997
998 case SD_DHCP6_CLIENT_EVENT_INFORMATION_REQUEST:
999 log_debug("/* %s (event=information-request) */", __func__);
1000
1001 assert_se(test_client_sent_message_count == 1);
1002
1003 test_lease_common(client);
1004
1005 assert_se(sd_dhcp6_client_set_information_request(client, false) >= 0);
1006 assert_se(sd_dhcp6_client_start(client) >= 0);
1007 assert_se(dhcp6_client_set_transaction_id(client, ((const DHCP6Message*) msg_advertise)->transaction_id) >= 0);
1008 break;
1009
1010 case SD_DHCP6_CLIENT_EVENT_IP_ACQUIRE:
1011 log_debug("/* %s (event=ip-acquire) */", __func__);
1012
1013 assert_se(IN_SET(test_client_sent_message_count, 3, 5));
1014
1015 test_lease_managed(client);
1016
1017 switch (test_client_sent_message_count) {
1018 case 3:
1019 assert_se(sd_dhcp6_client_stop(client) >= 0);
1020 assert_se(sd_dhcp6_client_start(client) >= 0);
1021 assert_se(dhcp6_client_set_transaction_id(client, ((const DHCP6Message*) msg_reply)->transaction_id) >= 0);
1022 break;
1023
1024 case 5:
1025 assert_se(sd_event_exit(sd_dhcp6_client_get_event(client), 0) >= 0);
1026 break;
1027
1028 default:
1029 assert_not_reached();
1030 }
1031
1032 break;
1033
1034 case DHCP6_CLIENT_EVENT_TEST_ADVERTISED: {
1035 sd_dhcp6_lease *lease;
1036 uint8_t preference;
1037
1038 log_debug("/* %s (event=test-advertised) */", __func__);
1039
1040 assert_se(test_client_sent_message_count == 2);
1041
1042 test_lease_managed(client);
1043
1044 assert_se(sd_dhcp6_client_get_lease(client, &lease) >= 0);
1045 assert_se(dhcp6_lease_get_preference(lease, &preference) >= 0);
1046 assert_se(preference == 0xff);
1047
1048 assert_se(dhcp6_client_set_transaction_id(client, ((const DHCP6Message*) msg_reply)->transaction_id) >= 0);
1049 break;
1050 }
1051 default:
1052 assert_not_reached();
1053 }
1054 }
1055
1056 int dhcp6_network_send_udp_socket(int s, const struct in6_addr *a, const void *packet, size_t len) {
1057 log_debug("/* %s(count=%u) */", __func__, test_client_sent_message_count);
1058
1059 assert_se(a);
1060 assert_se(in6_addr_equal(a, &mcast_address));
1061 assert_se(packet);
1062 assert_se(len >= sizeof(DHCP6Message));
1063
1064 switch (test_client_sent_message_count) {
1065 case 0:
1066 test_client_verify_information_request(packet, len);
1067 assert_se(write(test_fd[1], msg_reply, sizeof(msg_reply)) == sizeof(msg_reply));
1068 break;
1069
1070 case 1:
1071 test_client_verify_solicit(packet, len);
1072 assert_se(write(test_fd[1], msg_advertise, sizeof(msg_advertise)) == sizeof(msg_advertise));
1073 break;
1074
1075 case 2:
1076 test_client_callback(client_ref, DHCP6_CLIENT_EVENT_TEST_ADVERTISED, NULL);
1077 test_client_verify_request(packet, len);
1078 assert_se(write(test_fd[1], msg_reply, sizeof(msg_reply)) == sizeof(msg_reply));
1079 break;
1080
1081 case 3:
1082 test_client_verify_release(packet, len);
1083 /* when stopping, dhcp6 client doesn't wait for release server reply */
1084 assert_se(write(test_fd[1], msg_reply, sizeof(msg_reply)) == sizeof(msg_reply));
1085 break;
1086
1087 case 4:
1088 test_client_verify_solicit(packet, len);
1089 assert_se(write(test_fd[1], msg_reply, sizeof(msg_reply)) == sizeof(msg_reply));
1090 break;
1091
1092 default:
1093 assert_not_reached();
1094 }
1095
1096 test_client_sent_message_count++;
1097 return len;
1098 }
1099
1100 int dhcp6_network_bind_udp_socket(int ifindex, const struct in6_addr *a) {
1101 assert_se(ifindex == test_ifindex);
1102 assert_se(a);
1103 assert_se(in6_addr_equal(a, &local_address));
1104
1105 assert_se(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) >= 0);
1106 return TAKE_FD(test_fd[0]);
1107 }
1108
1109 TEST(dhcp6_client) {
1110 _cleanup_(sd_dhcp6_client_unrefp) sd_dhcp6_client *client = NULL;
1111 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
1112
1113 assert_se(sd_event_new(&e) >= 0);
1114 assert_se(sd_event_add_time_relative(e, NULL, CLOCK_BOOTTIME,
1115 30 * USEC_PER_SEC, 0,
1116 NULL, INT_TO_PTR(-ETIMEDOUT)) >= 0);
1117
1118 assert_se(sd_dhcp6_client_new(&client) >= 0);
1119 assert_se(sd_dhcp6_client_attach_event(client, e, 0) >= 0);
1120 assert_se(sd_dhcp6_client_set_ifindex(client, test_ifindex) == 0);
1121 assert_se(sd_dhcp6_client_set_local_address(client, &local_address) >= 0);
1122 assert_se(sd_dhcp6_client_set_fqdn(client, "host.lab.intra") >= 0);
1123 assert_se(sd_dhcp6_client_set_iaid(client, unaligned_read_be32((uint8_t[]) { IA_ID_BYTES })) >= 0);
1124 assert_se(sd_dhcp6_client_set_send_release(client, true) >= 0);
1125
1126 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_DNS_SERVER) >= 0);
1127 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_DOMAIN) >= 0);
1128 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_NTP_SERVER) >= 0);
1129 assert_se(sd_dhcp6_client_set_request_option(client, SD_DHCP6_OPTION_SNTP_SERVER) >= 0);
1130
1131 assert_se(sd_dhcp6_client_set_information_request(client, true) >= 0);
1132 assert_se(sd_dhcp6_client_set_callback(client, test_client_callback, NULL) >= 0);
1133
1134 assert_se(sd_dhcp6_client_start(client) >= 0);
1135
1136 assert_se(dhcp6_client_set_transaction_id(client, ((const DHCP6Message*) msg_reply)->transaction_id) >= 0);
1137
1138 assert_se(client_ref = sd_dhcp6_client_ref(client));
1139
1140 assert_se(sd_event_loop(e) >= 0);
1141
1142 assert_se(test_client_sent_message_count == 5);
1143
1144 assert_se(!sd_dhcp6_client_unref(client_ref));
1145 test_fd[1] = safe_close(test_fd[1]);
1146 }
1147
1148 static int intro(void) {
1149 assert_se(setenv("SYSTEMD_NETWORK_TEST_MODE", "1", 1) >= 0);
1150 return 0;
1151 }
1152
1153 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);