Debian Codegraph @

src/libsystemd-network/test-ndisc-rs.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2014 Intel Corporation. All rights reserved.
4 ***/
5
6 #include <netinet/icmp6.h>
7 #include <unistd.h>
8
9 #include "sd-ndisc.h"
10
11 #include "alloc-util.h"
12 #include "fd-util.h"
13 #include "hexdecoct.h"
14 #include "icmp6-packet.h"
15 #include "icmp6-test-util.h"
16 #include "in-addr-util.h"
17 #include "ndisc-internal.h"
18 #include "strv.h"
19 #include "tests.h"
20
21 static struct ether_addr mac_addr = {
22 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}
23 };
24
25 static void router_dump(sd_ndisc_router *rt) {
26 struct in6_addr addr;
27 uint8_t hop_limit;
28 usec_t t, lifetime, retrans_time;
29 uint64_t flags;
30 uint32_t mtu;
31 uint8_t preference;
32 int r;
33
34 assert_se(rt);
35
36 log_info("--");
37 assert_se(sd_ndisc_router_get_sender_address(rt, &addr) >= 0);
38 log_info("Sender: %s", IN6_ADDR_TO_STRING(&addr));
39
40 assert_se(sd_ndisc_router_get_timestamp(rt, CLOCK_REALTIME, &t) >= 0);
41 log_info("Timestamp: %s", FORMAT_TIMESTAMP(t));
42
43 assert_se(sd_ndisc_router_get_timestamp(rt, CLOCK_MONOTONIC, &t) >= 0);
44 log_info("Monotonic: %" PRIu64, t);
45
46 if (sd_ndisc_router_get_hop_limit(rt, &hop_limit) < 0)
47 log_info("No hop limit set");
48 else
49 log_info("Hop limit: %u", hop_limit);
50
51 assert_se(sd_ndisc_router_get_flags(rt, &flags) >= 0);
52 log_info("Flags: <%s|%s>",
53 flags & ND_RA_FLAG_OTHER ? "OTHER" : "",
54 flags & ND_RA_FLAG_MANAGED ? "MANAGED" : "");
55
56 assert_se(sd_ndisc_router_get_preference(rt, &preference) >= 0);
57 log_info("Preference: %s",
58 preference == SD_NDISC_PREFERENCE_LOW ? "low" :
59 preference == SD_NDISC_PREFERENCE_HIGH ? "high" : "medium");
60
61 assert_se(sd_ndisc_router_get_lifetime(rt, &lifetime) >= 0);
62 assert_se(sd_ndisc_router_get_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0);
63 log_info("Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t));
64
65 assert_se(sd_ndisc_router_get_retransmission_time(rt, &retrans_time) >= 0);
66 log_info("Retransmission Time: %s", FORMAT_TIMESPAN(retrans_time, USEC_PER_SEC));
67
68 if (sd_ndisc_router_get_mtu(rt, &mtu) < 0)
69 log_info("No MTU set");
70 else
71 log_info("MTU: %" PRIu32, mtu);
72
73 r = sd_ndisc_router_option_rewind(rt);
74 for (;;) {
75 uint8_t type;
76
77 assert_se(r >= 0);
78
79 if (r == 0)
80 break;
81
82 assert_se(sd_ndisc_router_option_get_type(rt, &type) >= 0);
83
84 log_info(">> Option %u", type);
85
86 switch (type) {
87
88 case SD_NDISC_OPTION_SOURCE_LL_ADDRESS:
89 case SD_NDISC_OPTION_TARGET_LL_ADDRESS: {
90 _cleanup_free_ char *c = NULL;
91 const uint8_t *p;
92 size_t n;
93
94 assert_se(sd_ndisc_router_option_get_raw(rt, &p, &n) >= 0);
95 assert_se(n > 2);
96 assert_se(c = hexmem(p + 2, n - 2));
97
98 log_info("Address: %s", c);
99 break;
100 }
101
102 case SD_NDISC_OPTION_PREFIX_INFORMATION: {
103 uint8_t prefix_len, pfl;
104 struct in6_addr a;
105
106 assert_se(sd_ndisc_router_prefix_get_valid_lifetime(rt, &lifetime) >= 0);
107 assert_se(sd_ndisc_router_prefix_get_valid_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0);
108 log_info("Valid Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t));
109
110 assert_se(sd_ndisc_router_prefix_get_preferred_lifetime(rt, &lifetime) >= 0);
111 assert_se(sd_ndisc_router_prefix_get_preferred_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0);
112 log_info("Preferred Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t));
113
114 assert_se(sd_ndisc_router_prefix_get_flags(rt, &pfl) >= 0);
115 log_info("Flags: <%s|%s>",
116 pfl & ND_OPT_PI_FLAG_ONLINK ? "ONLINK" : "",
117 pfl & ND_OPT_PI_FLAG_AUTO ? "AUTO" : "");
118
119 assert_se(sd_ndisc_router_prefix_get_prefixlen(rt, &prefix_len) >= 0);
120 log_info("Prefix Length: %u", prefix_len);
121
122 assert_se(sd_ndisc_router_prefix_get_address(rt, &a) >= 0);
123 log_info("Prefix: %s", IN6_ADDR_TO_STRING(&a));
124
125 break;
126 }
127
128 case SD_NDISC_OPTION_RDNSS: {
129 const struct in6_addr *a;
130 int n, i;
131
132 n = sd_ndisc_router_rdnss_get_addresses(rt, &a);
133 assert_se(n > 0);
134
135 for (i = 0; i < n; i++)
136 log_info("DNS: %s", IN6_ADDR_TO_STRING(a + i));
137
138 assert_se(sd_ndisc_router_rdnss_get_lifetime(rt, &lifetime) >= 0);
139 assert_se(sd_ndisc_router_rdnss_get_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0);
140 log_info("Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t));
141 break;
142 }
143
144 case SD_NDISC_OPTION_DNSSL: {
145 char **l;
146
147 assert_se(sd_ndisc_router_dnssl_get_domains(rt, &l) >= 0);
148
149 STRV_FOREACH(s, l)
150 log_info("Domain: %s", *s);
151
152 assert_se(sd_ndisc_router_dnssl_get_lifetime(rt, &lifetime) >= 0);
153 assert_se(sd_ndisc_router_dnssl_get_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0);
154 log_info("Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t));
155 break;
156 }}
157
158 r = sd_ndisc_router_option_next(rt);
159 }
160 }
161
162 static int send_ra(uint8_t flags) {
163 uint8_t advertisement[] = {
164 /* struct nd_router_advert */
165 0x86, 0x00, 0xde, 0x83, 0x40, 0xc0, 0x00, 0xb4,
166 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
167 /* type = 0x03 (SD_NDISC_OPTION_PREFIX_INFORMATION), length = 32 */
168 0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x01, 0xf4,
169 0x00, 0x00, 0x01, 0xb8, 0x00, 0x00, 0x00, 0x00,
170 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef,
171 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
172 /* type = 0x19 (SD_NDISC_OPTION_RDNSS), length = 24 */
173 0x19, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c,
174 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef,
175 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
176 /* type = 0x1f (SD_NDISC_OPTION_DNSSL), length = 24 */
177 0x1f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c,
178 0x03, 0x6c, 0x61, 0x62, 0x05, 0x69, 0x6e, 0x74,
179 0x72, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
180 /* type = 0x01 (SD_NDISC_OPTION_SOURCE_LL_ADDRESS), length = 8 */
181 0x01, 0x01, 0x78, 0x2b, 0xcb, 0xb3, 0x6d, 0x53,
182 };
183
184 advertisement[5] = flags;
185
186 assert_se(write(test_fd[1], advertisement, sizeof(advertisement)) ==
187 sizeof(advertisement));
188
189 log_debug("%s: Sent RA with flag 0x%02x", __func__, flags);
190
191 return 0;
192 }
193
194 static void test_callback_ra(sd_ndisc *nd, sd_ndisc_event_t event, void *message, void *userdata) {
195 sd_event *e = userdata;
196 static unsigned idx = 0;
197 uint64_t flags_array[] = {
198 0,
199 0,
200 0,
201 ND_RA_FLAG_OTHER,
202 ND_RA_FLAG_MANAGED
203 };
204 uint64_t flags;
205
206 assert_se(nd);
207
208 if (event != SD_NDISC_EVENT_ROUTER)
209 return;
210
211 sd_ndisc_router *rt = ASSERT_PTR(message);
212
213 router_dump(rt);
214
215 assert_se(sd_ndisc_router_get_flags(rt, &flags) >= 0);
216 assert_se(flags == flags_array[idx]);
217 idx++;
218
219 log_debug("%s: Got event 0x%02" PRIx64, __func__, flags);
220
221 if (idx < ELEMENTSOF(flags_array)) {
222 send_ra(flags_array[idx]);
223 return;
224 }
225
226 idx = 0;
227 sd_event_exit(e, 0);
228 }
229
230 static void test_callback_count(
231 sd_ndisc *nd,
232 sd_ndisc_event_t event,
233 void *message,
234 void *userdata) {
235
236 unsigned *count = ASSERT_PTR(userdata);
237
238 if (event == SD_NDISC_EVENT_ROUTER)
239 (*count)++;
240 }
241
242 static int on_recv_rs(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
243 _cleanup_(icmp6_packet_unrefp) ICMP6Packet *packet = NULL;
244 assert_se(icmp6_packet_receive(fd, &packet) >= 0);
245
246 return send_ra(0);
247 }
248
249 TEST(rs_ifindex_mismatch) {
250 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
251 _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
252 unsigned count = 0;
253
254 assert_se(sd_event_new(&e) >= 0);
255 assert_se(sd_ndisc_new(&nd) >= 0);
256 assert_se(sd_ndisc_attach_event(nd, e, 0) >= 0);
257 assert_se(sd_ndisc_set_ifindex(nd, 42) >= 0);
258 assert_se(sd_ndisc_set_callback(nd, test_callback_count, &count) >= 0);
259 assert_se(sd_ndisc_start(nd) >= 0);
260
261 test_ifindex = 43;
262 send_ra(0);
263 assert_se(sd_event_run(e, UINT64_MAX) >= 0);
264 assert_se(count == 0);
265
266 assert_se(sd_ndisc_stop(nd) >= 0);
267 test_fd[1] = safe_close(test_fd[1]);
268 }
269
270 TEST(rs) {
271 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
272 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
273 _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
274
275 assert_se(sd_event_new(&e) >= 0);
276
277 assert_se(sd_ndisc_new(&nd) >= 0);
278 assert_se(nd);
279
280 assert_se(sd_ndisc_attach_event(nd, e, 0) >= 0);
281
282 assert_se(sd_ndisc_set_ifindex(nd, 42) >= 0);
283 assert_se(sd_ndisc_set_mac(nd, &mac_addr) >= 0);
284 assert_se(sd_ndisc_set_callback(nd, test_callback_ra, e) >= 0);
285
286 assert_se(sd_event_add_time_relative(e, NULL, CLOCK_BOOTTIME,
287 30 * USEC_PER_SEC, 0,
288 NULL, INT_TO_PTR(-ETIMEDOUT)) >= 0);
289
290 assert_se(sd_ndisc_stop(nd) >= 0);
291 assert_se(sd_ndisc_start(nd) >= 0);
292 assert_se(sd_ndisc_start(nd) >= 0);
293 assert_se(sd_ndisc_stop(nd) >= 0);
294 test_fd[1] = safe_close(test_fd[1]);
295
296 assert_se(sd_ndisc_start(nd) >= 0);
297
298 assert_se(sd_event_add_io(e, &s, test_fd[1], EPOLLIN, on_recv_rs, nd) >= 0);
299 assert_se(sd_event_source_set_io_fd_own(s, true) >= 0);
300
301 assert_se(sd_event_loop(e) >= 0);
302
303 test_fd[1] = -EBADF;
304 }
305
306 static int send_ra_invalid_domain(uint8_t flags) {
307 uint8_t advertisement[] = {
308 /* struct nd_router_advert */
309 0x86, 0x00, 0xde, 0x83, 0x40, 0xc0, 0x00, 0xb4,
310 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
311 /* type = 0x03 (SD_NDISC_OPTION_PREFIX_INFORMATION), length = 32 */
312 0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x01, 0xf4,
313 0x00, 0x00, 0x01, 0xb8, 0x00, 0x00, 0x00, 0x00,
314 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef,
315 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
316 /* type = 0x19 (SD_NDISC_OPTION_RDNSS), length = 24 */
317 0x19, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c,
318 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef,
319 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
320 /* type = 0x1f (SD_NDISC_OPTION_DNSSL), length = 112 */
321 0x1f, 0x0e, 0xee, 0x68, 0xb0, 0xf4, 0x36, 0x39,
322 0x2c, 0xbc, 0x0b, 0xbc, 0xa9, 0x97, 0x71, 0x37,
323 0xad, 0x86, 0x80, 0x14, 0x2e, 0x58, 0xaa, 0x8a,
324 0xb7, 0xa1, 0xbe, 0x91, 0x59, 0x00, 0xc4, 0xe8,
325 0xdd, 0xd8, 0x6d, 0xe5, 0x4a, 0x7a, 0x71, 0x42,
326 0x74, 0x45, 0x9e, 0x2e, 0xfd, 0x9d, 0x71, 0x1d,
327 0xd0, 0xc0, 0x54, 0x0c, 0x4d, 0x1f, 0xbf, 0x90,
328 0xd9, 0x79, 0x58, 0xc0, 0x1d, 0xa3, 0x39, 0xcf,
329 0xb8, 0xec, 0xd2, 0xe4, 0xcd, 0xb6, 0x13, 0x2f,
330 0xc0, 0x46, 0xe8, 0x07, 0x3f, 0xaa, 0x28, 0xa5,
331 0x23, 0xf1, 0xf0, 0xca, 0xd3, 0x19, 0x3f, 0xfa,
332 0x6c, 0x7c, 0xec, 0x1b, 0xcf, 0x71, 0xeb, 0xba,
333 0x68, 0x1b, 0x8e, 0x7d, 0x93, 0x7e, 0x0b, 0x9f,
334 0xdb, 0x12, 0x9c, 0x75, 0x22, 0x5f, 0x12, 0x00,
335 /* type = 0x01 (SD_NDISC_OPTION_SOURCE_LL_ADDRESS), length = 8 */
336 0x01, 0x01, 0x78, 0x2b, 0xcb, 0xb3, 0x6d, 0x53,
337 };
338
339 advertisement[5] = flags;
340
341 assert_se(write(test_fd[1], advertisement, sizeof(advertisement)) ==
342 sizeof(advertisement));
343
344 log_debug("%s: Sent RA with flag 0x%02x", __func__, flags);
345
346 return 0;
347 }
348
349 static int on_recv_rs_invalid_domain(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
350 _cleanup_(icmp6_packet_unrefp) ICMP6Packet *packet = NULL;
351 assert_se(icmp6_packet_receive(fd, &packet) >= 0);
352
353 return send_ra_invalid_domain(0);
354 }
355
356 TEST(invalid_domain) {
357 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
358 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
359 _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
360
361 assert_se(sd_event_new(&e) >= 0);
362
363 assert_se(sd_ndisc_new(&nd) >= 0);
364 assert_se(nd);
365
366 assert_se(sd_ndisc_attach_event(nd, e, 0) >= 0);
367
368 assert_se(sd_ndisc_set_ifindex(nd, 42) >= 0);
369 assert_se(sd_ndisc_set_mac(nd, &mac_addr) >= 0);
370 assert_se(sd_ndisc_set_callback(nd, test_callback_ra, e) >= 0);
371
372 assert_se(sd_event_add_time_relative(e, NULL, CLOCK_BOOTTIME,
373 30 * USEC_PER_SEC, 0,
374 NULL, INT_TO_PTR(-ETIMEDOUT)) >= 0);
375
376 assert_se(sd_ndisc_start(nd) >= 0);
377
378 assert_se(sd_event_add_io(e, &s, test_fd[1], EPOLLIN, on_recv_rs_invalid_domain, nd) >= 0);
379 assert_se(sd_event_source_set_io_fd_own(s, true) >= 0);
380
381 assert_se(sd_event_loop(e) >= 0);
382
383 test_fd[1] = -EBADF;
384 }
385
386 static void neighbor_dump(sd_ndisc_neighbor *na) {
387 struct in6_addr addr;
388 uint32_t flags;
389
390 assert_se(na);
391
392 log_info("--");
393 assert_se(sd_ndisc_neighbor_get_sender_address(na, &addr) >= 0);
394 log_info("Sender: %s", IN6_ADDR_TO_STRING(&addr));
395
396 assert_se(sd_ndisc_neighbor_get_flags(na, &flags) >= 0);
397 log_info("Flags: Router:%s, Solicited:%s, Override: %s",
398 yes_no(flags & ND_NA_FLAG_ROUTER),
399 yes_no(flags & ND_NA_FLAG_SOLICITED),
400 yes_no(flags & ND_NA_FLAG_OVERRIDE));
401
402 assert_se(sd_ndisc_neighbor_is_router(na) == FLAGS_SET(flags, ND_NA_FLAG_ROUTER));
403 assert_se(sd_ndisc_neighbor_is_solicited(na) == FLAGS_SET(flags, ND_NA_FLAG_SOLICITED));
404 assert_se(sd_ndisc_neighbor_is_override(na) == FLAGS_SET(flags, ND_NA_FLAG_OVERRIDE));
405 }
406
407 static int send_na(uint32_t flags) {
408 uint8_t advertisement[] = {
409 /* struct nd_neighbor_advert */
410 0x88, 0x00, 0xde, 0x83, 0x00, 0x00, 0x00, 0x00,
411 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
412 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
413 /* type = 0x02 (SD_NDISC_OPTION_TARGET_LL_ADDRESS), length = 8 */
414 0x01, 0x01, 'A', 'B', 'C', '1', '2', '3',
415 };
416
417 ((struct nd_neighbor_advert*) advertisement)->nd_na_flags_reserved = flags;
418
419 assert_se(write(test_fd[1], advertisement, sizeof(advertisement)) == sizeof(advertisement));
420
421 log_debug("%s: Sent NA with flag 0x%02x", __func__, flags);
422
423 return 0;
424 }
425
426 static void test_callback_na(sd_ndisc *nd, sd_ndisc_event_t event, void *message, void *userdata) {
427 sd_event *e = userdata;
428 static unsigned idx = 0;
429 uint32_t flags_array[] = {
430 0,
431 0,
432 ND_NA_FLAG_ROUTER,
433 ND_NA_FLAG_SOLICITED,
434 ND_NA_FLAG_SOLICITED | ND_NA_FLAG_OVERRIDE,
435 };
436 uint32_t flags;
437
438 assert_se(nd);
439
440 if (event != SD_NDISC_EVENT_NEIGHBOR)
441 return;
442
443 sd_ndisc_neighbor *rt = ASSERT_PTR(message);
444
445 neighbor_dump(rt);
446
447 assert_se(sd_ndisc_neighbor_get_flags(rt, &flags) >= 0);
448 assert_se(flags == flags_array[idx]);
449 idx++;
450
451 log_debug("%s: Got event 0x%02" PRIx32, __func__, flags);
452
453 if (idx < ELEMENTSOF(flags_array)) {
454 send_na(flags_array[idx]);
455 return;
456 }
457
458 idx = 0;
459 sd_event_exit(e, 0);
460 }
461
462 static int on_recv_rs_na(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
463 _cleanup_(icmp6_packet_unrefp) ICMP6Packet *packet = NULL;
464 assert_se(icmp6_packet_receive(fd, &packet) >= 0);
465
466 return send_na(0);
467 }
468
469 TEST(na) {
470 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
471 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
472 _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
473
474 assert_se(sd_event_new(&e) >= 0);
475
476 assert_se(sd_ndisc_new(&nd) >= 0);
477 assert_se(nd);
478
479 assert_se(sd_ndisc_attach_event(nd, e, 0) >= 0);
480
481 assert_se(sd_ndisc_set_ifindex(nd, 42) >= 0);
482 assert_se(sd_ndisc_set_mac(nd, &mac_addr) >= 0);
483 assert_se(sd_ndisc_set_callback(nd, test_callback_na, e) >= 0);
484
485 assert_se(sd_event_add_time_relative(e, NULL, CLOCK_BOOTTIME,
486 30 * USEC_PER_SEC, 0,
487 NULL, INT_TO_PTR(-ETIMEDOUT)) >= 0);
488
489 assert_se(sd_ndisc_start(nd) >= 0);
490
491 assert_se(sd_event_add_io(e, &s, test_fd[1], EPOLLIN, on_recv_rs_na, nd) >= 0);
492 assert_se(sd_event_source_set_io_fd_own(s, true) >= 0);
493
494 assert_se(sd_event_loop(e) >= 0);
495
496 test_fd[1] = -EBADF;
497 }
498
499 static int on_recv_rs_timeout(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
500 _cleanup_(icmp6_packet_unrefp) ICMP6Packet *packet = NULL;
501 sd_ndisc *nd = ASSERT_PTR(userdata);
502 static int count = 0;
503 static usec_t last = 0;
504 usec_t min, max;
505
506 assert_se(icmp6_packet_receive(fd, &packet) >= 0);
507
508 if (++count >= 20)
509 sd_event_exit(nd->event, 0);
510
511 if (last == 0) {
512 /* initial RT = IRT + RAND*IRT */
513 min = NDISC_ROUTER_SOLICITATION_INTERVAL -
514 NDISC_ROUTER_SOLICITATION_INTERVAL / 10;
515 max = NDISC_ROUTER_SOLICITATION_INTERVAL +
516 NDISC_ROUTER_SOLICITATION_INTERVAL / 10;
517 } else {
518 /* next RT = 2*RTprev + RAND*RTprev */
519 min = 2 * last - last / 10;
520 max = 2 * last + last / 10;
521 }
522
523 /* final RT > MRT */
524 if (last * 2 > NDISC_MAX_ROUTER_SOLICITATION_INTERVAL) {
525 min = NDISC_MAX_ROUTER_SOLICITATION_INTERVAL -
526 NDISC_MAX_ROUTER_SOLICITATION_INTERVAL / 10;
527 max = NDISC_MAX_ROUTER_SOLICITATION_INTERVAL +
528 NDISC_MAX_ROUTER_SOLICITATION_INTERVAL / 10;
529 }
530
531 log_info("backoff timeout interval %2d %s%s <= %s <= %s",
532 count,
533 last * 2 > NDISC_MAX_ROUTER_SOLICITATION_INTERVAL ? "(max) ": "",
534 FORMAT_TIMESPAN(min, USEC_PER_MSEC),
535 FORMAT_TIMESPAN(nd->retransmit_time, USEC_PER_MSEC),
536 FORMAT_TIMESPAN(max, USEC_PER_MSEC));
537
538 assert_se(min <= nd->retransmit_time);
539 assert_se(max >= nd->retransmit_time);
540
541 last = nd->retransmit_time;
542
543 assert_se(sd_event_source_set_time(nd->timeout_event_source, 0) >= 0);
544
545 return 0;
546 }
547
548 TEST(timeout) {
549 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
550 _cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
551 _cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
552
553 assert_se(sd_event_new(&e) >= 0);
554
555 assert_se(sd_ndisc_new(&nd) >= 0);
556 assert_se(nd);
557
558 assert_se(sd_ndisc_attach_event(nd, e, 0) >= 0);
559
560 assert_se(sd_ndisc_set_ifindex(nd, 42) >= 0);
561 assert_se(sd_ndisc_set_mac(nd, &mac_addr) >= 0);
562
563 assert_se(sd_event_add_time_relative(e, NULL, CLOCK_BOOTTIME,
564 30 * USEC_PER_SEC, 0,
565 NULL, INT_TO_PTR(-ETIMEDOUT)) >= 0);
566
567 assert_se(sd_ndisc_start(nd) >= 0);
568
569 assert_se(sd_event_add_io(e, &s, test_fd[1], EPOLLIN, on_recv_rs_timeout, nd) >= 0);
570 assert_se(sd_event_source_set_io_fd_own(s, true) >= 0);
571
572 assert_se(sd_event_loop(e) >= 0);
573
574 test_fd[1] = -EBADF;
575 }
576
577 DEFINE_TEST_MAIN(LOG_DEBUG);