1 /* main.c - Application main entry point */
2
3 /*
4 * Copyright (c) 2016 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <zephyr/logging/log.h>
10 LOG_MODULE_REGISTER(net_test, CONFIG_NET_ROUTE_LOG_LEVEL);
11
12 #include <zephyr/types.h>
13 #include <zephyr/ztest.h>
14 #include <stdbool.h>
15 #include <stddef.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <zephyr/sys/printk.h>
19 #include <zephyr/linker/sections.h>
20 #include <zephyr/random/random.h>
21
22 #include <zephyr/tc_util.h>
23
24 #include <zephyr/net/ethernet.h>
25 #include <zephyr/net/dummy.h>
26 #include <zephyr/net_buf.h>
27 #include <zephyr/net/net_ip.h>
28 #include <zephyr/net/net_if.h>
29 #include <zephyr/net/net_context.h>
30
31 #define NET_LOG_ENABLED 1
32 #include "net_private.h"
33 #include "icmpv6.h"
34 #include "ipv6.h"
35 #include "nbr.h"
36 #include "route.h"
37
38 #if defined(CONFIG_NET_ROUTE_LOG_LEVEL_DBG)
39 #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
40 #else
41 #define DBG(fmt, ...)
42 #endif
43
44 static struct net_context *udp_ctx;
45
46 /* Interface 1 is the default host and it has my_addr assigned to it */
47 static struct in6_addr my_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
49
50 /* Interface 2 is the secondary host for peer device with address peer_addr */
51 static struct in6_addr peer_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
52 0, 0, 0, 0, 0x0b, 0x0e, 0x0e, 0x3 } } };
53
54 /* Alternate next hop address for dest_addr */
55 static struct in6_addr peer_addr_alt = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
56 0, 0, 0, 0, 0x0b, 0x0e, 0x0e, 0x4 } } };
57
58 /* The dest_addr is only reachable via peer_addr */
59 static struct in6_addr dest_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
60 0, 0, 0, 0, 0xd, 0xe, 0x5, 0x7 } } };
61
62 /* Extra address is assigned to ll_addr */
63 static struct in6_addr ll_addr = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
64 0, 0, 0, 0xf2, 0xaa, 0x29, 0x02,
65 0x04 } } };
66
67 static struct in6_addr in6addr_mcast = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
68 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
69
70 /* Generic address that we are using to generate some more addresses */
71 static struct in6_addr generic_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
72 0, 0, 0, 0, 0xbe, 0xef, 0, 0 } } };
73
74 static struct net_if *recipient;
75 static struct net_if *my_iface;
76 static struct net_if *peer_iface;
77
78 static struct net_route_entry *route_entry;
79
80 #define MAX_ROUTES CONFIG_NET_MAX_ROUTES
81 static const int max_routes = MAX_ROUTES;
82 static struct net_route_entry *test_routes[MAX_ROUTES];
83 static struct in6_addr dest_addresses[MAX_ROUTES];
84
85 static bool test_failed;
86 static bool data_failure;
87 static bool feed_data; /* feed data back to IP stack */
88
89 static int msg_sending;
90
91 K_SEM_DEFINE(wait_data, 0, UINT_MAX);
92
93 #define WAIT_TIME K_MSEC(250)
94
95 struct net_route_test {
96 uint8_t mac_addr[sizeof(struct net_eth_addr)];
97 struct net_linkaddr ll_addr;
98 };
99
net_route_dev_init(const struct device * dev)100 int net_route_dev_init(const struct device *dev)
101 {
102 return 0;
103 }
104
net_route_get_mac(const struct device * dev)105 static uint8_t *net_route_get_mac(const struct device *dev)
106 {
107 struct net_route_test *route = dev->data;
108
109 if (route->mac_addr[2] == 0x00) {
110 /* 00-00-5E-00-53-xx Documentation RFC 7042 */
111 route->mac_addr[0] = 0x00;
112 route->mac_addr[1] = 0x00;
113 route->mac_addr[2] = 0x5E;
114 route->mac_addr[3] = 0x00;
115 route->mac_addr[4] = 0x53;
116 route->mac_addr[5] = sys_rand8_get();
117 }
118
119 route->ll_addr.addr = route->mac_addr;
120 route->ll_addr.len = 6U;
121
122 return route->mac_addr;
123 }
124
net_route_iface_init(struct net_if * iface)125 static void net_route_iface_init(struct net_if *iface)
126 {
127 uint8_t *mac = net_route_get_mac(net_if_get_device(iface));
128
129 net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr),
130 NET_LINK_ETHERNET);
131 }
132
tester_send(const struct device * dev,struct net_pkt * pkt)133 static int tester_send(const struct device *dev, struct net_pkt *pkt)
134 {
135 if (!pkt->frags) {
136 TC_ERROR("No data to send!\n");
137 return -ENODATA;
138 }
139
140 /* By default we assume that the test is ok */
141 data_failure = false;
142
143 if (feed_data) {
144 DBG("Received at iface %p and feeding it into iface %p\n",
145 net_pkt_iface(pkt), recipient);
146
147 net_pkt_ref(pkt);
148
149 if (net_recv_data(recipient, pkt) < 0) {
150 TC_ERROR("Data receive failed.");
151 net_pkt_unref(pkt);
152 test_failed = true;
153 }
154
155 goto out;
156 }
157
158 DBG("pkt %p to be sent len %lu\n", pkt, net_pkt_get_len(pkt));
159
160 if (data_failure) {
161 test_failed = true;
162 }
163
164 msg_sending = 0;
165 out:
166 k_sem_give(&wait_data);
167
168 return 0;
169 }
170
tester_send_peer(const struct device * dev,struct net_pkt * pkt)171 static int tester_send_peer(const struct device *dev, struct net_pkt *pkt)
172 {
173 if (!pkt->frags) {
174 TC_ERROR("No data to send!\n");
175 return -ENODATA;
176 }
177
178 /* By default we assume that the test is ok */
179 data_failure = false;
180
181 if (feed_data) {
182 DBG("Received at iface %p and feeding it into iface %p\n",
183 net_pkt_iface(pkt), recipient);
184
185 net_pkt_ref(pkt);
186 if (net_recv_data(recipient, pkt) < 0) {
187 TC_ERROR("Data receive failed.");
188 net_pkt_unref(pkt);
189 test_failed = true;
190 }
191
192 goto out;
193 }
194
195 DBG("pkt %p to be sent len %lu\n", pkt, net_pkt_get_len(pkt));
196
197 if (data_failure) {
198 test_failed = true;
199 }
200
201 msg_sending = 0;
202 out:
203 k_sem_give(&wait_data);
204
205 return 0;
206 }
207
208 struct net_route_test net_route_data;
209 struct net_route_test net_route_data_peer;
210
211 static struct dummy_api net_route_if_api = {
212 .iface_api.init = net_route_iface_init,
213 .send = tester_send,
214 };
215
216 static struct dummy_api net_route_if_api_peer = {
217 .iface_api.init = net_route_iface_init,
218 .send = tester_send_peer,
219 };
220
221 #define _ETH_L2_LAYER DUMMY_L2
222 #define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2)
223
224 NET_DEVICE_INIT_INSTANCE(net_route_test, "net_route_test", host,
225 net_route_dev_init, NULL,
226 &net_route_data, NULL,
227 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
228 &net_route_if_api, _ETH_L2_LAYER,
229 _ETH_L2_CTX_TYPE, 127);
230
231 NET_DEVICE_INIT_INSTANCE(net_route_test_peer, "net_route_test_peer", peer,
232 net_route_dev_init, NULL,
233 &net_route_data_peer, NULL,
234 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
235 &net_route_if_api_peer, _ETH_L2_LAYER,
236 _ETH_L2_CTX_TYPE, 127);
237
test_init(void)238 static void test_init(void)
239 {
240 struct net_if_mcast_addr *maddr;
241 struct net_if_addr *ifaddr;
242 int i;
243
244 my_iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY));
245 peer_iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY)) + 1;
246
247 DBG("Interfaces: [%d] my %p, [%d] peer %p\n",
248 net_if_get_by_iface(my_iface), my_iface,
249 net_if_get_by_iface(peer_iface), peer_iface);
250
251 zassert_not_null(my_iface,
252 "Interface is NULL");
253
254 zassert_not_null(peer_iface,
255 "Interface is NULL");
256
257 ifaddr = net_if_ipv6_addr_add(my_iface, &my_addr,
258 NET_ADDR_MANUAL, 0);
259 zassert_not_null(ifaddr,
260 "Cannot add IPv6 address");
261
262 /* For testing purposes we need to set the addresses preferred */
263 ifaddr->addr_state = NET_ADDR_PREFERRED;
264
265 ifaddr = net_if_ipv6_addr_add(my_iface, &ll_addr,
266 NET_ADDR_MANUAL, 0);
267 zassert_not_null(ifaddr,
268 "Cannot add IPv6 address");
269
270 ifaddr->addr_state = NET_ADDR_PREFERRED;
271
272 net_ipv6_addr_create(&in6addr_mcast, 0xff02, 0, 0, 0, 0, 0, 0, 0x0001);
273
274 maddr = net_if_ipv6_maddr_add(my_iface, &in6addr_mcast);
275 zassert_not_null(maddr,
276 "Cannot add multicast IPv6 address");
277
278 /* The peer and dest interfaces are just simulated, they are not
279 * really used so we should not add IP addresses for them.
280 */
281
282 /* Some test addresses are generated */
283 for (i = 0; i < max_routes; i++) {
284 memcpy(&dest_addresses[i], &generic_addr,
285 sizeof(struct in6_addr));
286
287 dest_addresses[i].s6_addr[14] = i + 1;
288 dest_addresses[i].s6_addr[15] = sys_rand8_get();
289 }
290 }
291
test_net_ctx_create(void)292 static void test_net_ctx_create(void)
293 {
294 int ret;
295
296 ret = net_context_get(AF_INET6, SOCK_DGRAM, IPPROTO_UDP,
297 &udp_ctx);
298 zassert_equal(ret, 0,
299 "Context create IPv6 UDP test failed");
300 }
301
net_test_send_ns(struct net_if * iface,struct in6_addr * addr)302 static bool net_test_send_ns(struct net_if *iface,
303 struct in6_addr *addr)
304 {
305 int ret;
306
307 ret = net_ipv6_send_ns(iface,
308 NULL,
309 addr,
310 &my_addr,
311 &my_addr,
312 false);
313 if (ret < 0) {
314 TC_ERROR("Cannot send NS (%d)\n", ret);
315 return false;
316 }
317
318 return true;
319 }
320
net_test_nbr_lookup_ok(struct net_if * iface,struct in6_addr * addr)321 static bool net_test_nbr_lookup_ok(struct net_if *iface,
322 struct in6_addr *addr)
323 {
324 struct net_nbr *nbr;
325
326 nbr = net_ipv6_nbr_lookup(iface, addr);
327 if (!nbr) {
328 TC_ERROR("Neighbor %s not found in cache\n",
329 net_sprint_ipv6_addr(addr));
330 return false;
331 }
332
333 return true;
334 }
335
test_populate_nbr_cache(void)336 static void test_populate_nbr_cache(void)
337 {
338 struct net_nbr *nbr;
339
340 msg_sending = NET_ICMPV6_NS;
341 feed_data = true;
342 data_failure = false;
343
344 recipient = my_iface;
345
346 zassert_true(net_test_send_ns(peer_iface, &peer_addr));
347
348 nbr = net_ipv6_nbr_add(net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY)),
349 &peer_addr,
350 &net_route_data_peer.ll_addr,
351 false,
352 NET_IPV6_NBR_STATE_REACHABLE);
353 zassert_not_null(nbr, "Cannot add peer to neighbor cache");
354
355 zassert_true(net_test_send_ns(peer_iface, &peer_addr_alt));
356
357 nbr = net_ipv6_nbr_add(net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY)),
358 &peer_addr_alt,
359 &net_route_data_peer.ll_addr,
360 false,
361 NET_IPV6_NBR_STATE_REACHABLE);
362 zassert_not_null(nbr, "Cannot add peer to neighbor cache");
363
364 k_sem_take(&wait_data, WAIT_TIME);
365
366 feed_data = false;
367
368 zassert_false(data_failure, "data failure");
369
370 data_failure = false;
371
372 zassert_true(net_test_nbr_lookup_ok(my_iface, &peer_addr));
373 }
374
test_route_add(void)375 static void test_route_add(void)
376 {
377 route_entry = net_route_add(my_iface,
378 &dest_addr, 128,
379 &peer_addr,
380 NET_IPV6_ND_INFINITE_LIFETIME,
381 NET_ROUTE_PREFERENCE_LOW);
382
383 zassert_not_null(route_entry, "Route add failed");
384 }
385
test_route_update(void)386 static void test_route_update(void)
387 {
388 struct net_route_entry *update_entry;
389
390 update_entry = net_route_add(my_iface,
391 &dest_addr, 128,
392 &peer_addr,
393 NET_IPV6_ND_INFINITE_LIFETIME,
394 NET_ROUTE_PREFERENCE_LOW);
395 zassert_equal_ptr(update_entry, route_entry,
396 "Route add again failed");
397 }
398
test_route_del(void)399 static void test_route_del(void)
400 {
401 int ret;
402
403 ret = net_route_del(route_entry);
404 if (ret < 0) {
405 zassert_true(0, "Route del failed");
406 }
407 }
408
test_route_del_again(void)409 static void test_route_del_again(void)
410 {
411 int ret;
412
413 ret = net_route_del(route_entry);
414 if (ret >= 0) {
415 zassert_true(0, "Route del again failed");
416 }
417 }
418
test_route_get_nexthop(void)419 static void test_route_get_nexthop(void)
420 {
421 struct in6_addr *nexthop;
422
423 nexthop = net_route_get_nexthop(route_entry);
424
425 zassert_not_null(nexthop, "Route get nexthop failed");
426
427 zassert_true(net_ipv6_addr_cmp(nexthop, &peer_addr),
428 "Route nexthop does not match");
429 }
430
test_route_lookup_ok(void)431 static void test_route_lookup_ok(void)
432 {
433 struct net_route_entry *entry;
434
435 entry = net_route_lookup(my_iface, &dest_addr);
436 zassert_not_null(entry,
437 "Route lookup failed");
438 }
439
test_route_lookup_fail(void)440 static void test_route_lookup_fail(void)
441 {
442 struct net_route_entry *entry;
443
444 entry = net_route_lookup(my_iface, &peer_addr);
445 zassert_is_null(entry,
446 "Route lookup failed for peer address");
447 }
448
test_route_del_nexthop(void)449 static void test_route_del_nexthop(void)
450 {
451 struct in6_addr *nexthop = &peer_addr;
452 int ret;
453
454 ret = net_route_del_by_nexthop(my_iface, nexthop);
455 zassert_false((ret <= 0), "Route del nexthop failed");
456 }
457
test_route_del_nexthop_again(void)458 static void test_route_del_nexthop_again(void)
459 {
460 struct in6_addr *nexthop = &peer_addr;
461 int ret;
462
463 ret = net_route_del_by_nexthop(my_iface, nexthop);
464 zassert_false((ret >= 0), "Route del again nexthop failed");
465 }
466
test_route_add_many(void)467 static void test_route_add_many(void)
468 {
469 int i;
470
471 for (i = 0; i < max_routes; i++) {
472 DBG("Adding route %d addr %s\n", i + 1,
473 net_sprint_ipv6_addr(&dest_addresses[i]));
474 test_routes[i] = net_route_add(my_iface,
475 &dest_addresses[i], 128,
476 &peer_addr,
477 NET_IPV6_ND_INFINITE_LIFETIME,
478 NET_ROUTE_PREFERENCE_LOW);
479 zassert_not_null(test_routes[i], "Route add failed");
480 }
481 }
482
test_route_del_many(void)483 static void test_route_del_many(void)
484 {
485 int i;
486
487 for (i = 0; i < max_routes; i++) {
488 DBG("Deleting route %d addr %s\n", i + 1,
489 net_sprint_ipv6_addr(&dest_addresses[i]));
490 zassert_false(net_route_del(test_routes[i]),
491 " Route del failed");
492 }
493 }
494
test_route_lifetime(void)495 static void test_route_lifetime(void)
496 {
497 route_entry = net_route_add(my_iface,
498 &dest_addr, 128,
499 &peer_addr,
500 NET_IPV6_ND_INFINITE_LIFETIME,
501 NET_ROUTE_PREFERENCE_LOW);
502
503 zassert_not_null(route_entry, "Route add failed");
504
505 route_entry = net_route_lookup(my_iface, &dest_addr);
506 zassert_not_null(route_entry, "Route not found");
507
508 net_route_update_lifetime(route_entry, 1);
509
510 k_sleep(K_MSEC(1200));
511
512 route_entry = net_route_lookup(my_iface, &dest_addr);
513 zassert_is_null(route_entry, "Route did not expire");
514
515 }
516
test_route_preference(void)517 static void test_route_preference(void)
518 {
519 struct net_route_entry *update_entry;
520
521 route_entry = net_route_add(my_iface,
522 &dest_addr, 128,
523 &peer_addr,
524 NET_IPV6_ND_INFINITE_LIFETIME,
525 NET_ROUTE_PREFERENCE_LOW);
526 zassert_not_null(route_entry, "Route add failed");
527
528 update_entry = net_route_add(my_iface,
529 &dest_addr, 128,
530 &peer_addr_alt,
531 NET_IPV6_ND_INFINITE_LIFETIME,
532 NET_ROUTE_PREFERENCE_MEDIUM);
533 zassert_equal_ptr(update_entry, route_entry,
534 "Route add again failed");
535
536 update_entry = net_route_add(my_iface,
537 &dest_addr, 128,
538 &peer_addr,
539 NET_IPV6_ND_INFINITE_LIFETIME,
540 NET_ROUTE_PREFERENCE_LOW);
541 zassert_is_null(update_entry,
542 "Low preference route overwritten medium one");
543
544 net_route_del(route_entry);
545 }
546
547
548 /*test case main entry*/
ZTEST(route_test_suite,test_route)549 ZTEST(route_test_suite, test_route)
550 {
551 test_init();
552 test_net_ctx_create();
553 test_populate_nbr_cache();
554 test_route_add();
555 test_route_update();
556 test_route_get_nexthop();
557 test_route_lookup_ok();
558 test_route_lookup_fail();
559 test_route_del();
560 test_route_add();
561 test_route_del_nexthop();
562 test_route_del_again();
563 test_route_del_nexthop_again();
564 test_populate_nbr_cache();
565 test_route_add_many();
566 test_route_del_many();
567 test_route_lifetime();
568 test_route_preference();
569 }
570
571 ZTEST_SUITE(route_test_suite, NULL, NULL, NULL, NULL, NULL);
572