1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/***
Copyright © 2014 Intel Corporation. All rights reserved.
***/
#include <netinet/icmp6.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "sd-ndisc.h"
#include "alloc-util.h"
#include "fd-util.h"
#include "hexdecoct.h"
#include "icmp6-util-unix.h"
#include "socket-util.h"
#include "strv.h"
#include "ndisc-internal.h"
#include "tests.h"
static struct ether_addr mac_addr = {
.ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}
};
static bool verbose = false;
static sd_ndisc *test_timeout_nd;
static void router_dump(sd_ndisc_router *rt) {
struct in6_addr addr;
uint8_t hop_limit;
usec_t t, lifetime;
uint64_t flags;
uint32_t mtu;
unsigned preference;
int r;
assert_se(rt);
log_info("--");
assert_se(sd_ndisc_router_get_address(rt, &addr) >= 0);
log_info("Sender: %s", IN6_ADDR_TO_STRING(&addr));
assert_se(sd_ndisc_router_get_timestamp(rt, CLOCK_REALTIME, &t) >= 0);
log_info("Timestamp: %s", FORMAT_TIMESTAMP(t));
assert_se(sd_ndisc_router_get_timestamp(rt, CLOCK_MONOTONIC, &t) >= 0);
log_info("Monotonic: %" PRIu64, t);
if (sd_ndisc_router_get_hop_limit(rt, &hop_limit) < 0)
log_info("No hop limit set");
else
log_info("Hop limit: %u", hop_limit);
assert_se(sd_ndisc_router_get_flags(rt, &flags) >= 0);
log_info("Flags: <%s|%s>",
flags & ND_RA_FLAG_OTHER ? "OTHER" : "",
flags & ND_RA_FLAG_MANAGED ? "MANAGED" : "");
assert_se(sd_ndisc_router_get_preference(rt, &preference) >= 0);
log_info("Preference: %s",
preference == SD_NDISC_PREFERENCE_LOW ? "low" :
preference == SD_NDISC_PREFERENCE_HIGH ? "high" : "medium");
assert_se(sd_ndisc_router_get_lifetime(rt, &lifetime) >= 0);
assert_se(sd_ndisc_router_get_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0);
log_info("Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t));
if (sd_ndisc_router_get_mtu(rt, &mtu) < 0)
log_info("No MTU set");
else
log_info("MTU: %" PRIu32, mtu);
r = sd_ndisc_router_option_rewind(rt);
for (;;) {
uint8_t type;
assert_se(r >= 0);
if (r == 0)
break;
assert_se(sd_ndisc_router_option_get_type(rt, &type) >= 0);
log_info(">> Option %u", type);
switch (type) {
case SD_NDISC_OPTION_SOURCE_LL_ADDRESS:
case SD_NDISC_OPTION_TARGET_LL_ADDRESS: {
_cleanup_free_ char *c = NULL;
const void *p;
size_t n;
assert_se(sd_ndisc_router_option_get_raw(rt, &p, &n) >= 0);
assert_se(n > 2);
assert_se(c = hexmem((uint8_t*) p + 2, n - 2));
log_info("Address: %s", c);
break;
}
case SD_NDISC_OPTION_PREFIX_INFORMATION: {
unsigned prefix_len;
uint8_t pfl;
struct in6_addr a;
assert_se(sd_ndisc_router_prefix_get_valid_lifetime(rt, &lifetime) >= 0);
assert_se(sd_ndisc_router_prefix_get_valid_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0);
log_info("Valid Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t));
assert_se(sd_ndisc_router_prefix_get_preferred_lifetime(rt, &lifetime) >= 0);
assert_se(sd_ndisc_router_prefix_get_preferred_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0);
log_info("Preferred Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t));
assert_se(sd_ndisc_router_prefix_get_flags(rt, &pfl) >= 0);
log_info("Flags: <%s|%s>",
pfl & ND_OPT_PI_FLAG_ONLINK ? "ONLINK" : "",
pfl & ND_OPT_PI_FLAG_AUTO ? "AUTO" : "");
assert_se(sd_ndisc_router_prefix_get_prefixlen(rt, &prefix_len) >= 0);
log_info("Prefix Length: %u", prefix_len);
assert_se(sd_ndisc_router_prefix_get_address(rt, &a) >= 0);
log_info("Prefix: %s", IN6_ADDR_TO_STRING(&a));
break;
}
case SD_NDISC_OPTION_RDNSS: {
const struct in6_addr *a;
int n, i;
n = sd_ndisc_router_rdnss_get_addresses(rt, &a);
assert_se(n > 0);
for (i = 0; i < n; i++)
log_info("DNS: %s", IN6_ADDR_TO_STRING(a + i));
assert_se(sd_ndisc_router_rdnss_get_lifetime(rt, &lifetime) >= 0);
assert_se(sd_ndisc_router_rdnss_get_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0);
log_info("Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t));
break;
}
case SD_NDISC_OPTION_DNSSL: {
_cleanup_strv_free_ char **l = NULL;
int n, i;
n = sd_ndisc_router_dnssl_get_domains(rt, &l);
assert_se(n > 0);
for (i = 0; i < n; i++)
log_info("Domain: %s", l[i]);
assert_se(sd_ndisc_router_dnssl_get_lifetime(rt, &lifetime) >= 0);
assert_se(sd_ndisc_router_dnssl_get_lifetime_timestamp(rt, CLOCK_REALTIME, &t) >= 0);
log_info("Lifetime: %s (%s)", FORMAT_TIMESPAN(lifetime, USEC_PER_SEC), FORMAT_TIMESTAMP(t));
break;
}}
r = sd_ndisc_router_option_next(rt);
}
}
static int send_ra(uint8_t flags) {
uint8_t advertisement[] = {
0x86, 0x00, 0xde, 0x83, 0x40, 0xc0, 0x00, 0xb4,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x01, 0xf4,
0x00, 0x00, 0x01, 0xb8, 0x00, 0x00, 0x00, 0x00,
0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x19, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c,
0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
0x1f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c,
0x03, 0x6c, 0x61, 0x62, 0x05, 0x69, 0x6e, 0x74,
0x72, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x01, 0x78, 0x2b, 0xcb, 0xb3, 0x6d, 0x53,
};
advertisement[5] = flags;
assert_se(write(test_fd[1], advertisement, sizeof(advertisement)) ==
sizeof(advertisement));
if (verbose)
printf(" sent RA with flag 0x%02x\n", flags);
return 0;
}
static void test_callback(sd_ndisc *nd, sd_ndisc_event_t event, sd_ndisc_router *rt, void *userdata) {
sd_event *e = userdata;
static unsigned idx = 0;
uint64_t flags_array[] = {
0,
0,
0,
ND_RA_FLAG_OTHER,
ND_RA_FLAG_MANAGED
};
uint64_t flags;
assert_se(nd);
if (event != SD_NDISC_EVENT_ROUTER)
return;
router_dump(rt);
assert_se(sd_ndisc_router_get_flags(rt, &flags) >= 0);
assert_se(flags == flags_array[idx]);
idx++;
if (verbose)
printf(" got event 0x%02" PRIx64 "\n", flags);
if (idx < ELEMENTSOF(flags_array)) {
send_ra(flags_array[idx]);
return;
}
sd_event_exit(e, 0);
}
TEST(rs) {
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
_cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
send_ra_function = send_ra;
assert_se(sd_event_new(&e) >= 0);
assert_se(sd_ndisc_new(&nd) >= 0);
assert_se(nd);
assert_se(sd_ndisc_attach_event(nd, e, 0) >= 0);
assert_se(sd_ndisc_set_ifindex(nd, 42) >= 0);
assert_se(sd_ndisc_set_mac(nd, &mac_addr) >= 0);
assert_se(sd_ndisc_set_callback(nd, test_callback, e) >= 0);
assert_se(sd_event_add_time_relative(e, NULL, CLOCK_BOOTTIME,
30 * USEC_PER_SEC, 0,
NULL, INT_TO_PTR(-ETIMEDOUT)) >= 0);
assert_se(sd_ndisc_stop(nd) >= 0);
assert_se(sd_ndisc_start(nd) >= 0);
assert_se(sd_ndisc_start(nd) >= 0);
assert_se(sd_ndisc_stop(nd) >= 0);
test_fd[1] = safe_close(test_fd[1]);
assert_se(sd_ndisc_start(nd) >= 0);
assert_se(sd_event_loop(e) >= 0);
test_fd[1] = safe_close(test_fd[1]);
}
static int test_timeout_value(uint8_t flags) {
static int count = 0;
static usec_t last = 0;
sd_ndisc *nd = test_timeout_nd;
usec_t min, max;
assert_se(nd);
assert_se(nd->event);
if (++count >= 20)
sd_event_exit(nd->event, 0);
if (last == 0) {
/* initial RT = IRT + RAND*IRT */
min = NDISC_ROUTER_SOLICITATION_INTERVAL -
NDISC_ROUTER_SOLICITATION_INTERVAL / 10;
max = NDISC_ROUTER_SOLICITATION_INTERVAL +
NDISC_ROUTER_SOLICITATION_INTERVAL / 10;
} else {
/* next RT = 2*RTprev + RAND*RTprev */
min = 2 * last - last / 10;
max = 2 * last + last / 10;
}
/* final RT > MRT */
if (last * 2 > NDISC_MAX_ROUTER_SOLICITATION_INTERVAL) {
min = NDISC_MAX_ROUTER_SOLICITATION_INTERVAL -
NDISC_MAX_ROUTER_SOLICITATION_INTERVAL / 10;
max = NDISC_MAX_ROUTER_SOLICITATION_INTERVAL +
NDISC_MAX_ROUTER_SOLICITATION_INTERVAL / 10;
}
log_info("backoff timeout interval %2d %s%s <= %s <= %s",
count,
last * 2 > NDISC_MAX_ROUTER_SOLICITATION_INTERVAL ? "(max) ": "",
FORMAT_TIMESPAN(min, USEC_PER_MSEC),
FORMAT_TIMESPAN(nd->retransmit_time, USEC_PER_MSEC),
FORMAT_TIMESPAN(max, USEC_PER_MSEC));
assert_se(min <= nd->retransmit_time);
assert_se(max >= nd->retransmit_time);
last = nd->retransmit_time;
assert_se(sd_event_source_set_time(nd->timeout_event_source, 0) >= 0);
return 0;
}
TEST(timeout) {
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
_cleanup_(sd_ndisc_unrefp) sd_ndisc *nd = NULL;
send_ra_function = test_timeout_value;
assert_se(sd_event_new(&e) >= 0);
assert_se(sd_ndisc_new(&nd) >= 0);
assert_se(nd);
test_timeout_nd = nd;
assert_se(sd_ndisc_attach_event(nd, e, 0) >= 0);
assert_se(sd_ndisc_set_ifindex(nd, 42) >= 0);
assert_se(sd_ndisc_set_mac(nd, &mac_addr) >= 0);
assert_se(sd_event_add_time_relative(e, NULL, CLOCK_BOOTTIME,
30 * USEC_PER_SEC, 0,
NULL, INT_TO_PTR(-ETIMEDOUT)) >= 0);
assert_se(sd_ndisc_start(nd) >= 0);
assert_se(sd_event_loop(e) >= 0);
test_fd[1] = safe_close(test_fd[1]);
}
DEFINE_TEST_MAIN(LOG_DEBUG);
|