1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /***
3 Copyright © 2017 Intel Corporation. All rights reserved.
4 ***/
5
6 #include <netinet/icmp6.h>
7 #include <arpa/inet.h>
8 #include <unistd.h>
9
10 #include "sd-radv.h"
11
12 #include "alloc-util.h"
13 #include "hexdecoct.h"
14 #include "icmp6-util.h"
15 #include "socket-util.h"
16 #include "strv.h"
17 #include "tests.h"
18
19 static struct ether_addr mac_addr = {
20 .ether_addr_octet = { 0x78, 0x2b, 0xcb, 0xb3, 0x6d, 0x53 }
21 };
22
23 static uint8_t advertisement[] = {
24 /* ICMPv6 Router Advertisement, no checksum */
25 0x86, 0x00, 0x00, 0x00, 0x40, 0xc0, 0x00, 0xb4,
26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27 /* Source Link Layer Address Option */
28 0x01, 0x01, 0x78, 0x2b, 0xcb, 0xb3, 0x6d, 0x53,
29 /* Prefix Information Option */
30 0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x01, 0xf4,
31 0x00, 0x00, 0x01, 0xb8, 0x00, 0x00, 0x00, 0x00,
32 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef,
33 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34 /* Prefix Information Option */
35 0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x0e, 0x10,
36 0x00, 0x00, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00,
37 0x20, 0x01, 0x0d, 0xb8, 0x0b, 0x16, 0xd0, 0x0d,
38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39 /* Prefix Information Option */
40 0x03, 0x04, 0x30, 0xc0, 0x00, 0x00, 0x0e, 0x10,
41 0x00, 0x00, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00,
42 0x20, 0x01, 0x0d, 0xb8, 0xc0, 0x01, 0x0d, 0xad,
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 /* Recursive DNS Server Option */
45 0x19, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c,
46 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
48 /* DNS Search List Option */
49 0x1f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c,
50 0x03, 0x6c, 0x61, 0x62, 0x05, 0x69, 0x6e, 0x74,
51 0x72, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 };
53
54 static bool test_stopped;
55 static int test_fd[2];
56 static sd_event_source *recv_router_advertisement;
57 static struct {
58 struct in6_addr address;
59 unsigned char prefixlen;
60 uint32_t valid;
61 uint32_t preferred;
62 bool successful;
63 } prefix[] = {
64 { { { { 0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } }, 64,
66 500, 440, true },
67 { { { { 0x20, 0x01, 0x0d, 0xb8, 0x0b, 0x16, 0xd0, 0x0d,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } }, 64,
69 /* indicate default valid and preferred lifetimes for the test code */
70 0, 0, true },
71 { { { { 0x20, 0x01, 0x0d, 0xb8, 0x0b, 0x16, 0xd0, 0x0d,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } }, 58,
73 0, 0,
74 /* indicate that this prefix already exists */
75 false },
76 { { { { 0x20, 0x01, 0x0d, 0xb8, 0x0b, 0x16, 0xd0, 0x0d,
77 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } }, 120,
78 0, 0,
79 /* indicate that this prefix already exists */
80 false },
81 { { { { 0x20, 0x01, 0x0d, 0xb8, 0x0b, 0x16, 0xd0, 0x0d,
82 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } }, 12,
83 0, 0,
84 /* indicate that this prefix already exists */
85 false },
86 { { { { 0x20, 0x01, 0x0d, 0xb8, 0xc0, 0x01, 0x0d, 0xad,
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } }, 48,
88 0, 0, true },
89 { { { { 0x20, 0x01, 0x0d, 0xb8, 0xc0, 0x01, 0x0d, 0xad,
90 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } } }, 60,
91 0, 0,
92 /* indicate that this prefix already exists */
93 false },
94 };
95
96 static const struct in6_addr test_rdnss = { { { 0x20, 0x01, 0x0d, 0xb8,
97 0xde, 0xad, 0xbe, 0xef,
98 0x00, 0x00, 0x00, 0x00,
99 0x00, 0x00, 0x00, 0x01 } } };
100 static const char *test_dnssl[] = { "lab.intra",
101 NULL };
102
TEST(radv_prefix)103 TEST(radv_prefix) {
104 sd_radv_prefix *p;
105
106 assert_se(sd_radv_prefix_new(&p) >= 0);
107
108 assert_se(sd_radv_prefix_set_onlink(NULL, true) < 0);
109 assert_se(sd_radv_prefix_set_onlink(p, true) >= 0);
110 assert_se(sd_radv_prefix_set_onlink(p, false) >= 0);
111
112 assert_se(sd_radv_prefix_set_address_autoconfiguration(NULL, true) < 0);
113 assert_se(sd_radv_prefix_set_address_autoconfiguration(p, true) >= 0);
114 assert_se(sd_radv_prefix_set_address_autoconfiguration(p, false) >= 0);
115
116 assert_se(sd_radv_prefix_set_valid_lifetime(NULL, 1, 1) < 0);
117 assert_se(sd_radv_prefix_set_valid_lifetime(p, 0, 0) >= 0);
118 assert_se(sd_radv_prefix_set_valid_lifetime(p, 300 * USEC_PER_SEC, USEC_INFINITY) >= 0);
119 assert_se(sd_radv_prefix_set_valid_lifetime(p, 300 * USEC_PER_SEC, USEC_PER_YEAR) >= 0);
120
121 assert_se(sd_radv_prefix_set_preferred_lifetime(NULL, 1, 1) < 0);
122 assert_se(sd_radv_prefix_set_preferred_lifetime(p, 0, 0) >= 0);
123 assert_se(sd_radv_prefix_set_preferred_lifetime(p, 300 * USEC_PER_SEC, USEC_INFINITY) >= 0);
124 assert_se(sd_radv_prefix_set_preferred_lifetime(p, 300 * USEC_PER_SEC, USEC_PER_YEAR) >= 0);
125
126 assert_se(sd_radv_prefix_set_prefix(NULL, NULL, 0) < 0);
127 assert_se(sd_radv_prefix_set_prefix(p, NULL, 0) < 0);
128
129 assert_se(sd_radv_prefix_set_prefix(p, &prefix[0].address, 64) >= 0);
130 assert_se(sd_radv_prefix_set_prefix(p, &prefix[0].address, 0) < 0);
131 assert_se(sd_radv_prefix_set_prefix(p, &prefix[0].address, 1) < 0);
132 assert_se(sd_radv_prefix_set_prefix(p, &prefix[0].address, 2) < 0);
133 assert_se(sd_radv_prefix_set_prefix(p, &prefix[0].address, 3) >= 0);
134 assert_se(sd_radv_prefix_set_prefix(p, &prefix[0].address, 125) >= 0);
135 assert_se(sd_radv_prefix_set_prefix(p, &prefix[0].address, 128) >= 0);
136 assert_se(sd_radv_prefix_set_prefix(p, &prefix[0].address, 129) < 0);
137 assert_se(sd_radv_prefix_set_prefix(p, &prefix[0].address, 255) < 0);
138
139 p = sd_radv_prefix_unref(p);
140 assert_se(!p);
141 }
142
TEST(radv)143 TEST(radv) {
144 sd_radv *ra;
145
146 assert_se(sd_radv_new(&ra) >= 0);
147 assert_se(ra);
148
149 assert_se(sd_radv_set_ifindex(NULL, 0) < 0);
150 assert_se(sd_radv_set_ifindex(ra, 0) < 0);
151 assert_se(sd_radv_set_ifindex(ra, -1) < 0);
152 assert_se(sd_radv_set_ifindex(ra, -2) < 0);
153 assert_se(sd_radv_set_ifindex(ra, 42) >= 0);
154
155 assert_se(sd_radv_set_mac(NULL, NULL) < 0);
156 assert_se(sd_radv_set_mac(ra, NULL) >= 0);
157 assert_se(sd_radv_set_mac(ra, &mac_addr) >= 0);
158
159 assert_se(sd_radv_set_mtu(NULL, 0) < 0);
160 assert_se(sd_radv_set_mtu(ra, 0) < 0);
161 assert_se(sd_radv_set_mtu(ra, 1279) < 0);
162 assert_se(sd_radv_set_mtu(ra, 1280) >= 0);
163 assert_se(sd_radv_set_mtu(ra, ~0) >= 0);
164
165 assert_se(sd_radv_set_hop_limit(NULL, 0) < 0);
166 assert_se(sd_radv_set_hop_limit(ra, 0) >= 0);
167 assert_se(sd_radv_set_hop_limit(ra, ~0) >= 0);
168
169 assert_se(sd_radv_set_router_lifetime(NULL, 0) < 0);
170 assert_se(sd_radv_set_router_lifetime(ra, 0) >= 0);
171 assert_se(sd_radv_set_router_lifetime(ra, USEC_INFINITY) < 0);
172 assert_se(sd_radv_set_router_lifetime(ra, USEC_PER_YEAR) < 0);
173 assert_se(sd_radv_set_router_lifetime(ra, 300 * USEC_PER_SEC) >= 0);
174
175 assert_se(sd_radv_set_preference(NULL, 0) < 0);
176 assert_se(sd_radv_set_preference(ra, SD_NDISC_PREFERENCE_LOW) >= 0);
177 assert_se(sd_radv_set_preference(ra, SD_NDISC_PREFERENCE_MEDIUM) >= 0);
178 assert_se(sd_radv_set_preference(ra, SD_NDISC_PREFERENCE_HIGH) >= 0);
179 assert_se(sd_radv_set_preference(ra, ~0) < 0);
180
181 assert_se(sd_radv_set_preference(ra, SD_NDISC_PREFERENCE_HIGH) >= 0);
182 assert_se(sd_radv_set_router_lifetime(ra, 300 * USEC_PER_SEC) >= 0);
183 assert_se(sd_radv_set_router_lifetime(ra, 0) < 0);
184 assert_se(sd_radv_set_preference(ra, SD_NDISC_PREFERENCE_MEDIUM) >= 0);
185 assert_se(sd_radv_set_router_lifetime(ra, 0) >= 0);
186
187 assert_se(sd_radv_set_managed_information(NULL, true) < 0);
188 assert_se(sd_radv_set_managed_information(ra, true) >= 0);
189 assert_se(sd_radv_set_managed_information(ra, false) >= 0);
190
191 assert_se(sd_radv_set_other_information(NULL, true) < 0);
192 assert_se(sd_radv_set_other_information(ra, true) >= 0);
193 assert_se(sd_radv_set_other_information(ra, false) >= 0);
194
195 assert_se(sd_radv_set_rdnss(NULL, 0, NULL, 0) < 0);
196 assert_se(sd_radv_set_rdnss(ra, 0, NULL, 0) >= 0);
197 assert_se(sd_radv_set_rdnss(ra, 0, NULL, 128) < 0);
198 assert_se(sd_radv_set_rdnss(ra, 600, &test_rdnss, 0) >= 0);
199 assert_se(sd_radv_set_rdnss(ra, 600, &test_rdnss, 1) >= 0);
200 assert_se(sd_radv_set_rdnss(ra, 0, &test_rdnss, 1) >= 0);
201 assert_se(sd_radv_set_rdnss(ra, 0, NULL, 0) >= 0);
202
203 assert_se(sd_radv_set_dnssl(ra, 0, NULL) >= 0);
204 assert_se(sd_radv_set_dnssl(ra, 600, NULL) >= 0);
205 assert_se(sd_radv_set_dnssl(ra, 0, (char **)test_dnssl) >= 0);
206 assert_se(sd_radv_set_dnssl(ra, 600, (char **)test_dnssl) >= 0);
207
208 ra = sd_radv_unref(ra);
209 assert_se(!ra);
210 }
211
icmp6_bind_router_solicitation(int ifindex)212 int icmp6_bind_router_solicitation(int ifindex) {
213 return -ENOSYS;
214 }
215
icmp6_bind_router_advertisement(int ifindex)216 int icmp6_bind_router_advertisement(int ifindex) {
217 assert_se(ifindex == 42);
218
219 return test_fd[1];
220 }
221
icmp6_send_router_solicitation(int s,const struct ether_addr * ether_addr)222 int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) {
223
224 return 0;
225 }
226
icmp6_receive(int fd,void * iov_base,size_t iov_len,struct in6_addr * dst,triple_timestamp * timestamp)227 int icmp6_receive(int fd, void *iov_base, size_t iov_len,
228 struct in6_addr *dst, triple_timestamp *timestamp) {
229 assert_se(read (fd, iov_base, iov_len) == (ssize_t)iov_len);
230
231 if (timestamp)
232 triple_timestamp_get(timestamp);
233
234 return 0;
235 }
236
radv_recv(sd_event_source * s,int fd,uint32_t revents,void * userdata)237 static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
238 sd_radv *ra = userdata;
239 unsigned char buf[168];
240 size_t i;
241
242 assert_se(read(test_fd[0], &buf, sizeof(buf)) == sizeof(buf));
243
244 /* router lifetime must be zero when test is stopped */
245 if (test_stopped) {
246 advertisement[6] = 0x00;
247 advertisement[7] = 0x00;
248 }
249
250 printf ("Received Router Advertisement with lifetime %u\n",
251 (advertisement[6] << 8) + advertisement[7]);
252
253 /* test only up to buf size, rest is not yet implemented */
254 for (i = 0; i < sizeof(buf); i++) {
255 if (!(i % 8))
256 printf("%3zd: ", i);
257
258 printf("0x%02x", buf[i]);
259
260 assert_se(buf[i] == advertisement[i]);
261
262 if ((i + 1) % 8)
263 printf(", ");
264 else
265 printf("\n");
266 }
267
268 if (test_stopped) {
269 sd_event *e;
270
271 e = sd_radv_get_event(ra);
272 sd_event_exit(e, 0);
273
274 return 0;
275 }
276
277 assert_se(sd_radv_stop(ra) >= 0);
278 test_stopped = true;
279
280 return 0;
281 }
282
TEST(ra)283 TEST(ra) {
284 sd_event *e;
285 sd_radv *ra;
286 unsigned i;
287
288 assert_se(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) >= 0);
289
290 assert_se(sd_event_new(&e) >= 0);
291
292 assert_se(sd_radv_new(&ra) >= 0);
293 assert_se(ra);
294
295 assert_se(sd_radv_attach_event(ra, e, 0) >= 0);
296
297 assert_se(sd_radv_set_ifindex(ra, 42) >= 0);
298 assert_se(sd_radv_set_mac(ra, &mac_addr) >= 0);
299 assert_se(sd_radv_set_router_lifetime(ra, 180 * USEC_PER_SEC) >= 0);
300 assert_se(sd_radv_set_hop_limit(ra, 64) >= 0);
301 assert_se(sd_radv_set_managed_information(ra, true) >= 0);
302 assert_se(sd_radv_set_other_information(ra, true) >= 0);
303 assert_se(sd_radv_set_rdnss(ra, 60, &test_rdnss, 1) >= 0);
304 assert_se(sd_radv_set_dnssl(ra, 60, (char **)test_dnssl) >= 0);
305
306 for (i = 0; i < ELEMENTSOF(prefix); i++) {
307 sd_radv_prefix *p;
308
309 printf("Test prefix %u\n", i);
310 assert_se(sd_radv_prefix_new(&p) >= 0);
311
312 assert_se(sd_radv_prefix_set_prefix(p, &prefix[i].address,
313 prefix[i].prefixlen) >= 0);
314 if (prefix[i].valid > 0)
315 assert_se(sd_radv_prefix_set_valid_lifetime(p, prefix[i].valid * USEC_PER_SEC, USEC_INFINITY) >= 0);
316 if (prefix[i].preferred > 0)
317 assert_se(sd_radv_prefix_set_preferred_lifetime(p, prefix[i].preferred * USEC_PER_SEC, USEC_INFINITY) >= 0);
318
319 assert_se((sd_radv_add_prefix(ra, p) >= 0) == prefix[i].successful);
320 /* If the previous sd_radv_add_prefix() succeeds, then also the second call should also succeed. */
321 assert_se((sd_radv_add_prefix(ra, p) >= 0) == prefix[i].successful);
322
323 p = sd_radv_prefix_unref(p);
324 assert_se(!p);
325 }
326
327 assert_se(sd_event_add_io(e, &recv_router_advertisement, test_fd[0],
328 EPOLLIN, radv_recv, ra) >= 0);
329
330 assert_se(sd_event_add_time_relative(e, NULL, CLOCK_BOOTTIME,
331 2 * USEC_PER_SEC, 0,
332 NULL, INT_TO_PTR(-ETIMEDOUT)) >= 0);
333
334 assert_se(sd_radv_start(ra) >= 0);
335
336 assert_se(sd_event_loop(e) >= 0);
337
338 ra = sd_radv_unref(ra);
339 assert_se(!ra);
340
341 close(test_fd[0]);
342
343 sd_event_unref(e);
344 }
345
346 DEFINE_TEST_MAIN(LOG_DEBUG);
347