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_IPV6_LOG_LEVEL);
11
12 #include <zephyr/types.h>
13 #include <stddef.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <zephyr/device.h>
17 #include <zephyr/init.h>
18 #include <zephyr/linker/sections.h>
19 #include <zephyr/random/random.h>
20
21 #include <zephyr/ztest.h>
22
23 #include <zephyr/net/net_core.h>
24 #include <zephyr/net/net_pkt.h>
25 #include <zephyr/net/net_ip.h>
26 #include <zephyr/net/dummy.h>
27
28 #define NET_LOG_ENABLED 1
29 #include "net_private.h"
30
31 #if defined(CONFIG_NET_IPV6_LOG_LEVEL) || defined(CONFIG_NET_IPV4_LOG_LEVEL)
32 #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
33 #else
34 #define DBG(fmt, ...)
35 #endif
36
37 static struct net_if *default_iface;
38 static struct net_if *second_iface;
39
40 #define TEST_BYTE_1(value, expected) \
41 do { \
42 char out[3]; \
43 net_byte_to_hex(out, value, 'A', true); \
44 zassert_false(strcmp(out, expected), \
45 "Test 0x%s failed.\n", expected); \
46 } while (0)
47
48 #define TEST_BYTE_2(value, expected) \
49 do { \
50 char out[3]; \
51 net_byte_to_hex(out, value, 'a', true); \
52 zassert_false(strcmp(out, expected), \
53 "Test 0x%s failed.\n", expected); \
54 } while (0)
55
56 #define TEST_LL_6(a, b, c, d, e, f, expected) \
57 do { \
58 uint8_t ll[] = { a, b, c, d, e, f }; \
59 zassert_false(strcmp(net_sprint_ll_addr(ll, sizeof(ll)),\
60 expected), \
61 "Test %s failed.\n", expected); \
62 } while (0)
63
64 #define TEST_LL_8(a, b, c, d, e, f, g, h, expected) \
65 do { \
66 uint8_t ll[] = { a, b, c, d, e, f, g, h }; \
67 zassert_false(strcmp(net_sprint_ll_addr(ll, sizeof(ll)), \
68 expected), \
69 "Test %s failed.\n", expected); \
70 } while (0)
71
72 #define LL_ADDR_STR_SIZE sizeof("xx:xx:xx:xx:xx:xx")
73
74 #define TEST_LL_6_TWO(a, b, c, d, e, f, expected) \
75 do { \
76 uint8_t ll1[] = { a, b, c, d, e, f }; \
77 uint8_t ll2[] = { f, e, d, c, b, a }; \
78 char out[2 * LL_ADDR_STR_SIZE + 1 + 1]; \
79 snprintk(out, sizeof(out), "%s ", \
80 net_sprint_ll_addr(ll1, sizeof(ll1))); \
81 snprintk(out + LL_ADDR_STR_SIZE, \
82 sizeof(out) - LL_ADDR_STR_SIZE, "%s", \
83 net_sprint_ll_addr(ll2, sizeof(ll2))); \
84 zassert_false(strcmp(out, expected), \
85 "Test %s failed, got %s\n", expected, \
86 out); \
87 } while (0)
88
89 #define TEST_IPV6(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, expected) \
90 do { \
91 struct in6_addr addr = { { { a, b, c, d, e, f, g, h, \
92 i, j, k, l, m, n, o, p } } }; \
93 char *ptr = net_sprint_ipv6_addr(&addr); \
94 zassert_false(strcmp(ptr, expected), \
95 "Test %s failed, got %s\n", expected, \
96 ptr); \
97 } while (0)
98
99 #define TEST_IPV4(a, b, c, d, expected) \
100 do { \
101 struct in_addr addr = { { { a, b, c, d } } }; \
102 char *ptr = net_sprint_ipv4_addr(&addr); \
103 zassert_false(strcmp(ptr, expected), \
104 "Test %s failed, got %s\n", expected, \
105 ptr); \
106 } while (0)
107
108 struct net_test_context {
109 uint8_t mac_addr[6];
110 struct net_linkaddr ll_addr;
111 };
112
net_test_init(const struct device * dev)113 int net_test_init(const struct device *dev)
114 {
115 struct net_test_context *net_test_context = dev->data;
116
117 net_test_context = net_test_context;
118
119 return 0;
120 }
121
net_test_get_mac(const struct device * dev)122 static uint8_t *net_test_get_mac(const struct device *dev)
123 {
124 struct net_test_context *context = dev->data;
125
126 if (context->mac_addr[2] == 0x00) {
127 /* 00-00-5E-00-53-xx Documentation RFC 7042 */
128 context->mac_addr[0] = 0x00;
129 context->mac_addr[1] = 0x00;
130 context->mac_addr[2] = 0x5E;
131 context->mac_addr[3] = 0x00;
132 context->mac_addr[4] = 0x53;
133 context->mac_addr[5] = sys_rand8_get();
134 }
135
136 return context->mac_addr;
137 }
138
net_test_iface_init(struct net_if * iface)139 static void net_test_iface_init(struct net_if *iface)
140 {
141 uint8_t *mac = net_test_get_mac(net_if_get_device(iface));
142
143 net_if_set_link_addr(iface, mac, 6, NET_LINK_ETHERNET);
144 }
145
tester_send(const struct device * dev,struct net_pkt * pkt)146 static int tester_send(const struct device *dev, struct net_pkt *pkt)
147 {
148 return 0;
149 }
150
151 struct net_test_context net_test_context_data;
152
153 static struct dummy_api net_test_if_api = {
154 .iface_api.init = net_test_iface_init,
155 .send = tester_send,
156 };
157
158 #define _ETH_L2_LAYER DUMMY_L2
159 #define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2)
160
161 NET_DEVICE_INIT_INSTANCE(net_addr_test1, "net_addr_test1", iface1,
162 net_test_init, NULL,
163 &net_test_context_data, NULL,
164 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
165 &net_test_if_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE,
166 127);
167
168 NET_DEVICE_INIT_INSTANCE(net_addr_test2, "net_addr_test2", iface2,
169 net_test_init, NULL,
170 &net_test_context_data, NULL,
171 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
172 &net_test_if_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE,
173 127);
174
ZTEST(ip_addr_fn,test_ip_addresses)175 ZTEST(ip_addr_fn, test_ip_addresses)
176 {
177 TEST_BYTE_1(0xde, "DE");
178 TEST_BYTE_1(0x09, "09");
179 TEST_BYTE_2(0xa9, "a9");
180 TEST_BYTE_2(0x80, "80");
181
182 TEST_LL_6(0x12, 0x9f, 0xe3, 0x01, 0x7f, 0x00, "12:9F:E3:01:7F:00");
183 TEST_LL_8(0x12, 0x9f, 0xe3, 0x01, 0x7f, 0x00, 0xff, 0x0f, \
184 "12:9F:E3:01:7F:00:FF:0F");
185
186 TEST_LL_6_TWO(0x12, 0x9f, 0xe3, 0x01, 0x7f, 0x00, \
187 "12:9F:E3:01:7F:00 00:7F:01:E3:9F:12");
188
189 TEST_IPV6(0x20, 1, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, \
190 "2001:db8::1");
191
192 TEST_IPV6(0x20, 0x01, 0x0d, 0xb8, 0x12, 0x34, 0x56, 0x78, \
193 0x9a, 0xbc, 0xde, 0xf0, 0x01, 0x02, 0x03, 0x04, \
194 "2001:db8:1234:5678:9abc:def0:102:304");
195
196 TEST_IPV6(0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
197 0x0c, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, \
198 "fe80::cb8:0:0:2");
199
200 TEST_IPV6(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
201 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, \
202 "::1");
203
204 TEST_IPV6(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
205 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
206 "::");
207
208 TEST_IPV4(192, 168, 0, 1, "192.168.0.1");
209 TEST_IPV4(0, 0, 0, 0, "0.0.0.0");
210 TEST_IPV4(127, 0, 0, 1, "127.0.0.1");
211 }
212
ZTEST(ip_addr_fn,test_ipv6_addresses)213 ZTEST(ip_addr_fn, test_ipv6_addresses)
214 {
215 struct in6_addr loopback = IN6ADDR_LOOPBACK_INIT;
216 struct in6_addr any = IN6ADDR_ANY_INIT;
217 struct in6_addr mcast = { { { 0xff, 0x84, 0, 0, 0, 0, 0, 0,
218 0, 0, 0, 0, 0, 0, 0, 0x2 } } };
219 struct in6_addr addr6 = { { { 0xfe, 0x80, 0, 0, 0, 0, 0, 0,
220 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
221 struct in6_addr addr6_pref1 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
222 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
223 struct in6_addr addr6_pref2 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
224 0, 0, 0, 0, 0, 0, 0, 0x2 } } };
225 struct in6_addr addr6_pref3 = { { { 0x20, 0x01, 0x0d, 0xb8, 0x64, 0, 0,
226 0, 0, 0, 0, 0, 0, 0, 0, 0x2 } } };
227 struct in6_addr *tmp;
228 const struct in6_addr *out;
229 struct net_if_addr *ifaddr1, *ifaddr2;
230 struct net_if_mcast_addr *ifmaddr1;
231 struct net_if *iface;
232 int i;
233
234 /**TESTPOINT: Check if the IPv6 address is a loopback address*/
235 zassert_true(net_ipv6_is_addr_loopback(&loopback),
236 "IPv6 loopback address check failed.");
237
238 /**TESTPOINT: Check if the IPv6 address is a multicast address*/
239 zassert_true(net_ipv6_is_addr_mcast(&mcast),
240 "IPv6 multicast address check failed.");
241
242 ifaddr1 = net_if_ipv6_addr_add(default_iface,
243 &addr6,
244 NET_ADDR_MANUAL,
245 0);
246 /**TESTPOINT: Check if IPv6 interface address is added*/
247 zassert_not_null(ifaddr1, "IPv6 interface address add failed");
248
249 ifaddr2 = net_if_ipv6_addr_lookup(&addr6, NULL);
250
251 /**TESTPOINT: Check if addresses match*/
252 zassert_equal_ptr(ifaddr1, ifaddr2,
253 "IPv6 interface address mismatch");
254
255 /**TESTPOINT: Check if the IPv6 address is a loopback address*/
256 zassert_false(net_ipv6_is_my_addr(&loopback),
257 "My IPv6 loopback address check failed");
258
259 /**TESTPOINT: Check IPv6 address*/
260 zassert_true(net_ipv6_is_my_addr(&addr6),
261 "My IPv6 address check failed");
262
263 /**TESTPOINTS: Check IPv6 prefix*/
264 zassert_true(net_ipv6_is_prefix((uint8_t *)&addr6_pref1,
265 (uint8_t *)&addr6_pref2, 64),
266 "Same IPv6 prefix test failed");
267
268 zassert_false(net_ipv6_is_prefix((uint8_t *)&addr6_pref1,
269 (uint8_t *)&addr6_pref3, 64),
270 "Different IPv6 prefix test failed");
271
272 zassert_false(net_ipv6_is_prefix((uint8_t *)&addr6_pref1,
273 (uint8_t *)&addr6_pref2, 128),
274 "Different full IPv6 prefix test failed");
275
276 zassert_false(net_ipv6_is_prefix((uint8_t *)&addr6_pref1,
277 (uint8_t *)&addr6_pref3, 255),
278 "Too long prefix test failed");
279
280 ifmaddr1 = net_if_ipv6_maddr_add(default_iface, &mcast);
281
282 /**TESTPOINTS: Check IPv6 addresses*/
283 zassert_not_null(ifmaddr1, "IPv6 multicast address add failed");
284
285 ifmaddr1 = net_if_ipv6_maddr_add(default_iface, &addr6);
286
287 zassert_is_null(ifmaddr1,
288 "IPv6 multicast address could be added failed");
289
290 zassert_false(memcmp(net_ipv6_unspecified_address(), &any, sizeof(any)),
291 "My IPv6 unspecified address check failed");
292
293 ifaddr2 = net_if_ipv6_addr_add(default_iface,
294 &addr6,
295 NET_ADDR_AUTOCONF,
296 0);
297 zassert_not_null(ifaddr2, "IPv6 ll address autoconf add failed");
298
299 ifaddr2->addr_state = NET_ADDR_PREFERRED;
300
301 tmp = net_if_ipv6_get_ll(default_iface, NET_ADDR_PREFERRED);
302 zassert_false(tmp && memcmp(tmp, &addr6.s6_addr,
303 sizeof(struct in6_addr)),
304 "IPv6 ll address fetch failed");
305
306 ifaddr2->addr_state = NET_ADDR_DEPRECATED;
307
308 tmp = net_if_ipv6_get_ll(default_iface, NET_ADDR_PREFERRED);
309 zassert_false(tmp && !memcmp(tmp, &any, sizeof(struct in6_addr)),
310 "IPv6 preferred ll address fetch failed");
311
312 ifaddr1 = net_if_ipv6_addr_add(default_iface,
313 &addr6_pref2,
314 NET_ADDR_AUTOCONF,
315 0);
316 zassert_not_null(ifaddr1, "IPv6 global address autoconf add failed");
317
318 ifaddr1->addr_state = NET_ADDR_PREFERRED;
319
320 /* Two tests for IPv6, first with interface given, then when
321 * iface is NULL
322 */
323 for (i = 0, iface = default_iface; i < 2; i++, iface = NULL) {
324 ifaddr2->addr_state = NET_ADDR_DEPRECATED;
325
326 out = net_if_ipv6_select_src_addr(iface, &addr6_pref1);
327 zassert_not_null(out,
328 "IPv6 src addr selection failed, iface %p\n",
329 iface);
330
331 DBG("Selected IPv6 address %s, iface %p\n",
332 net_sprint_ipv6_addr(out), iface);
333
334 zassert_false(memcmp(out->s6_addr, &addr6_pref2.s6_addr,
335 sizeof(struct in6_addr)),
336 "IPv6 wrong src address selected, iface %p\n",
337 iface);
338
339 /* Now we should get :: address */
340 out = net_if_ipv6_select_src_addr(iface, &addr6);
341 zassert_not_null(out, "IPv6 src any addr selection failed, "
342 "iface %p\n", iface);
343
344 DBG("Selected IPv6 address %s, iface %p\n",
345 net_sprint_ipv6_addr(out), iface);
346
347 zassert_false(memcmp(out->s6_addr, &any.s6_addr,
348 sizeof(struct in6_addr)),
349 "IPv6 wrong src any address selected, iface %p\n",
350 iface);
351
352 ifaddr2->addr_state = NET_ADDR_PREFERRED;
353
354 /* Now we should get ll address */
355 out = net_if_ipv6_select_src_addr(iface, &addr6);
356 zassert_not_null(out, "IPv6 src ll addr selection failed, "
357 "iface %p\n", iface);
358
359 DBG("Selected IPv6 address %s, iface %p\n",
360 net_sprint_ipv6_addr(out), iface);
361
362 zassert_false(memcmp(out->s6_addr, &addr6.s6_addr,
363 sizeof(struct in6_addr)),
364 "IPv6 wrong src ll address selected, iface %p\n",
365 iface);
366 }
367
368 zassert_true(net_if_ipv6_addr_rm(default_iface, &addr6),
369 "IPv6 removing address failed\n");
370 zassert_true(net_if_ipv6_addr_rm(default_iface, &addr6_pref2),
371 "IPv6 removing address failed\n");
372 }
373
ZTEST(ip_addr_fn,test_ipv4_ll_address_select_default_first)374 ZTEST(ip_addr_fn, test_ipv4_ll_address_select_default_first)
375 {
376 struct net_if *iface;
377 const struct in_addr *out;
378 struct net_if_addr *ifaddr;
379 struct in_addr lladdr4_1 = { { { 169, 254, 0, 1 } } };
380 struct in_addr lladdr4_2 = { { { 169, 254, 0, 3 } } };
381 struct in_addr netmask = { { { 255, 255, 0, 0 } } };
382 struct in_addr dst4 = { { { 169, 254, 0, 2 } } };
383
384 ifaddr = net_if_ipv4_addr_add(default_iface, &lladdr4_1, NET_ADDR_MANUAL, 0);
385 zassert_not_null(ifaddr, "IPv4 interface address add failed");
386 zassert_true(net_ipv4_is_my_addr(&lladdr4_1),
387 "My IPv4 address check failed");
388
389 net_if_ipv4_set_netmask_by_addr(default_iface, &lladdr4_1, &netmask);
390
391 ifaddr = net_if_ipv4_addr_add(second_iface, &lladdr4_2, NET_ADDR_MANUAL, 0);
392 zassert_not_null(ifaddr, "IPv4 interface address add failed");
393 zassert_true(net_ipv4_is_my_addr(&lladdr4_2),
394 "My IPv4 address check failed");
395
396 net_if_ipv4_set_netmask_by_addr(second_iface, &lladdr4_2, &netmask);
397
398 /* In case two network interfaces have two equally good addresses
399 * (same net mask), default interface should be selected.
400 */
401 out = net_if_ipv4_select_src_addr(NULL, &dst4);
402 iface = net_if_ipv4_select_src_iface(&dst4);
403 zassert_not_null(out, "IPv4 src addr selection failed, iface %p\n",
404 iface);
405
406 DBG("Selected IPv4 address %s, iface %p\n", net_sprint_ipv4_addr(out),
407 iface);
408
409 zassert_equal_ptr(iface, default_iface, "Wrong iface selected");
410 zassert_equal(out->s_addr, lladdr4_1.s_addr,
411 "IPv4 wrong src address selected, iface %p\n", iface);
412 }
413
ZTEST(ip_addr_fn,test_ipv4_ll_address_select)414 ZTEST(ip_addr_fn, test_ipv4_ll_address_select)
415 {
416 struct net_if *iface;
417 const struct in_addr *out;
418 struct net_if_addr *ifaddr;
419 struct in_addr lladdr4_1 = { { { 169, 254, 250, 1 } } };
420 struct in_addr lladdr4_2 = { { { 169, 254, 253, 1 } } };
421 struct in_addr netmask_1 = { { { 255, 255, 255, 0 } } };
422 struct in_addr netmask_2 = { { { 255, 255, 255, 252 } } };
423 struct in_addr dst4_1 = { { { 169, 254, 250, 2 } } };
424 struct in_addr dst4_2 = { { { 169, 254, 253, 2 } } };
425
426 ifaddr = net_if_ipv4_addr_add(default_iface, &lladdr4_1, NET_ADDR_MANUAL, 0);
427 zassert_not_null(ifaddr, "IPv4 interface address add failed");
428 zassert_true(net_ipv4_is_my_addr(&lladdr4_1),
429 "My IPv4 address check failed");
430
431 net_if_ipv4_set_netmask_by_addr(default_iface, &lladdr4_1, &netmask_1);
432
433 ifaddr = net_if_ipv4_addr_add(second_iface, &lladdr4_2, NET_ADDR_MANUAL, 0);
434 zassert_not_null(ifaddr, "IPv4 interface address add failed");
435 zassert_true(net_ipv4_is_my_addr(&lladdr4_2),
436 "My IPv4 address check failed");
437
438 net_if_ipv4_set_netmask_by_addr(second_iface, &lladdr4_2, &netmask_2);
439
440 out = net_if_ipv4_select_src_addr(NULL, &dst4_1);
441 iface = net_if_ipv4_select_src_iface(&dst4_1);
442 zassert_not_null(out, "IPv4 src addr selection failed, iface %p\n",
443 iface);
444
445 DBG("Selected IPv4 address %s, iface %p\n", net_sprint_ipv4_addr(out),
446 iface);
447
448 zassert_equal(out->s_addr, lladdr4_1.s_addr,
449 "IPv4 wrong src address selected, iface %p\n", iface);
450 zassert_equal_ptr(iface, default_iface, "Wrong iface selected");
451
452 out = net_if_ipv4_select_src_addr(NULL, &dst4_2);
453 iface = net_if_ipv4_select_src_iface(&dst4_2);
454 zassert_not_null(out, "IPv4 src addr selection failed, iface %p\n",
455 iface);
456
457 DBG("Selected IPv4 address %s, iface %p\n", net_sprint_ipv4_addr(out),
458 iface);
459
460 zassert_equal(out->s_addr, lladdr4_2.s_addr,
461 "IPv4 wrong src address selected, iface %p\n", iface);
462 zassert_equal_ptr(iface, second_iface, "Wrong iface selected");
463 }
464
ZTEST(ip_addr_fn,test_ipv4_addresses)465 ZTEST(ip_addr_fn, test_ipv4_addresses)
466 {
467 const struct in_addr *out;
468 struct net_if_addr *ifaddr1;
469 struct net_if_mcast_addr *ifmaddr1;
470 struct in_addr addr4 = { { { 192, 168, 0, 1 } } };
471 struct in_addr addr4b = { { { 192, 168, 1, 2 } } };
472 struct in_addr addr4_not_found = { { { 10, 20, 30, 40 } } };
473 struct in_addr lladdr4 = { { { 169, 254, 98, 203 } } };
474 struct in_addr maddr4a = { { { 224, 0, 0, 1 } } };
475 struct in_addr maddr4b = { { { 224, 0, 0, 2 } } };
476 struct in_addr match_addr = { { { 192, 168, 0, 2 } } };
477 struct in_addr fail_addr = { { { 10, 1, 0, 2 } } };
478 struct in_addr netmask = { { { 255, 255, 255, 0 } } };
479 struct in_addr netmask2 = { { { 255, 255, 0, 0 } } };
480 struct in_addr gw = { { { 192, 168, 0, 42 } } };
481 struct in_addr loopback4 = { { { 127, 0, 0, 1 } } };
482 struct in_addr bcast_addr1 = { { { 255, 255, 255, 255 } } };
483 struct in_addr bcast_addr2 = { { { 192, 168, 1, 255 } } };
484 struct in_addr bcast_addr3 = { { { 192, 168, 255, 255 } } };
485 struct in_addr bcast_addr4 = { { { 192, 0, 2, 255 } } };
486 struct in_addr bcast_addr5 = { { { 192, 168, 0, 255 } } };
487 struct net_if *iface, *iface1, *iface2;
488 int i, ret;
489
490 ifaddr1 = net_if_ipv4_addr_add(default_iface,
491 &addr4,
492 NET_ADDR_MANUAL,
493 0);
494 zassert_not_null(ifaddr1, "IPv4 interface address add failed");
495
496 zassert_true(net_ipv4_is_my_addr(&addr4),
497 "My IPv4 address check failed");
498
499 net_if_ipv4_set_netmask_by_addr(default_iface, &addr4, &netmask);
500
501 ifaddr1 = net_if_ipv4_addr_add(default_iface,
502 &lladdr4,
503 NET_ADDR_MANUAL,
504 0);
505 zassert_not_null(ifaddr1, "IPv4 interface address add failed");
506
507 net_if_ipv4_set_netmask_by_addr(default_iface, &lladdr4, &netmask2);
508
509 zassert_true(net_ipv4_is_my_addr(&lladdr4),
510 "My IPv4 address check failed");
511
512 zassert_false(net_ipv4_is_my_addr(&loopback4),
513 "My IPv4 loopback address check failed");
514
515 /* Two tests for IPv4, first with interface given, then when
516 * iface is NULL
517 */
518 for (i = 0, iface = default_iface; i < 2; i++, iface = NULL) {
519 out = net_if_ipv4_select_src_addr(iface, &addr4);
520 zassert_not_null(out, "IPv4 src addr selection failed, "
521 "iface %p\n", iface);
522
523 DBG("Selected IPv4 address %s, iface %p\n",
524 net_sprint_ipv4_addr(out), iface);
525
526 zassert_equal(out->s_addr, addr4.s_addr,
527 "IPv4 wrong src address selected, iface %p\n",
528 iface);
529
530 /* Now we should get ll address */
531 out = net_if_ipv4_select_src_addr(iface, &lladdr4);
532 zassert_not_null(out, "IPv4 src ll addr selection failed, "
533 "iface %p\n", iface);
534
535 DBG("Selected IPv4 address %s, iface %p\n",
536 net_sprint_ipv4_addr(out), iface);
537
538 zassert_equal(out->s_addr, lladdr4.s_addr,
539 "IPv4 wrong src ll address selected, iface %p\n",
540 iface);
541
542 /* Now we should get 192.168.0.1 address */
543 out = net_if_ipv4_select_src_addr(iface, &addr4b);
544 zassert_not_null(out, "IPv4 src any addr selection failed, "
545 "iface %p\n", iface);
546
547 DBG("Selected IPv4 address %s, iface %p\n",
548 net_sprint_ipv4_addr(out), iface);
549
550 zassert_equal(out->s_addr, addr4.s_addr,
551 "IPv4 wrong src address selected, iface %p\n",
552 iface);
553
554 /* Now we should get 192.168.0.1 address */
555 out = net_if_ipv4_select_src_addr(iface, &addr4_not_found);
556 zassert_not_null(out, "IPv4 src any addr selection failed, "
557 "iface %p\n", iface);
558
559 DBG("Selected IPv4 address %s, iface %p\n",
560 net_sprint_ipv4_addr(out), iface);
561
562 zassert_equal(out->s_addr, addr4.s_addr,
563 "IPv4 wrong src address selected, iface %p\n",
564 iface);
565 }
566
567 iface = default_iface;
568
569 net_if_ipv4_set_gw(iface, &gw);
570
571 zassert_false(net_ipv4_addr_mask_cmp(iface, &fail_addr),
572 "IPv4 wrong match failed");
573
574 zassert_true(net_ipv4_addr_mask_cmp(iface, &match_addr),
575 "IPv4 match failed");
576
577 zassert_true(net_ipv4_is_addr_mcast(&maddr4a),
578 "IPv4 multicast address");
579
580 zassert_true(net_ipv4_is_addr_mcast(&maddr4b),
581 "IPv4 multicast address");
582
583 zassert_false(net_ipv4_is_addr_mcast(&addr4), "IPv4 address");
584
585 zassert_false(net_ipv4_is_addr_mcast(&bcast_addr1), "IPv4 broadcast address");
586
587 ifmaddr1 = net_if_ipv4_maddr_add(default_iface, &maddr4a);
588 zassert_not_null(ifmaddr1, "IPv4 multicast address add failed");
589
590 ifmaddr1 = net_if_ipv4_maddr_add(default_iface, &maddr4b);
591 zassert_not_null(ifmaddr1, "IPv4 multicast address add failed");
592
593 iface = NULL;
594
595 iface1 = net_if_get_by_index(1);
596 iface2 = net_if_get_by_index(2);
597
598 ifmaddr1 = net_if_ipv4_maddr_lookup(&maddr4a, &iface);
599 zassert_not_null(ifmaddr1, "IPv4 multicast address lookup failed");
600 zassert_equal(iface, iface1, "Interface not found");
601
602 ifmaddr1 = net_if_ipv4_maddr_lookup(&maddr4b, &iface);
603 zassert_not_null(ifmaddr1, "IPv4 multicast address lookup failed");
604 zassert_equal(iface, iface1, "Interface not found");
605
606 ifmaddr1 = net_if_ipv4_maddr_lookup(&maddr4a, &iface2);
607 zassert_is_null(ifmaddr1, "IPv4 multicast address lookup succeed");
608
609 ret = net_if_ipv4_maddr_rm(iface2, &maddr4a);
610 zassert_false(ret, "IPv4 rm succeed");
611
612 ret = net_if_ipv4_maddr_rm(iface1, &maddr4a);
613 zassert_true(ret, "IPv4 rm failed");
614
615 ifmaddr1 = net_if_ipv4_maddr_lookup(&maddr4a, &iface1);
616 zassert_is_null(ifmaddr1, "IPv4 multicast address lookup succeed");
617
618 ret = net_if_ipv4_maddr_rm(iface1, &maddr4b);
619 zassert_true(ret, "IPv4 rm failed");
620
621 ifmaddr1 = net_if_ipv4_maddr_lookup(&maddr4b, &iface1);
622 zassert_is_null(ifmaddr1, "IPv4 multicast address lookup succeed");
623
624 ret = net_ipv4_is_addr_bcast(iface, &bcast_addr1);
625 zassert_true(ret, "IPv4 address 1 is not broadcast address");
626
627 ret = net_ipv4_is_addr_bcast(iface, &bcast_addr2);
628 zassert_false(ret, "IPv4 address 2 is broadcast address");
629
630 ret = net_ipv4_is_addr_bcast(iface, &bcast_addr4);
631 zassert_false(ret, "IPv4 address 4 is broadcast address");
632
633 ret = net_ipv4_is_addr_bcast(iface, &maddr4b);
634 zassert_false(ret, "IPv4 address is broadcast address");
635
636 ret = net_ipv4_is_addr_bcast(iface, &bcast_addr5);
637 zassert_true(ret, "IPv4 address 5 is not broadcast address");
638
639 ret = net_ipv4_is_addr_bcast(iface, &bcast_addr2);
640 zassert_false(ret, "IPv4 address 2 is broadcast address");
641
642 net_if_ipv4_set_netmask_by_addr(iface, &addr4, &netmask2);
643
644 ret = net_ipv4_is_addr_bcast(iface, &bcast_addr3);
645 zassert_true(ret, "IPv4 address 3 is not broadcast address");
646 }
647
ZTEST(ip_addr_fn,test_ipv6_mesh_addresses)648 ZTEST(ip_addr_fn, test_ipv6_mesh_addresses)
649 {
650 struct net_if_addr *ifaddr;
651 const struct in6_addr *out;
652 struct in6_addr lla = { { { 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x54, 0xdb,
653 0x88, 0x1c, 0x38, 0x45, 0x57, 0xf4 } } };
654 struct in6_addr ml_eid = { { { 0xfd, 0xe5, 0x8d, 0xba, 0x82, 0xe1, 0,
655 0x01, 0x40, 0x16, 0x99, 0x3c, 0x83, 0x99,
656 0x35, 0xab } } };
657 struct in6_addr ll_mcast = { { { 0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0,
658 0, 0, 0, 0, 0x1 } } };
659 struct in6_addr ml_mcast = { { { 0xff, 0x03, 0, 0, 0, 0, 0, 0, 0, 0, 0,
660 0, 0, 0, 0, 0x1 } } };
661 struct net_if *iface = default_iface;
662
663 ifaddr = net_if_ipv6_addr_add(iface, &lla, NET_ADDR_AUTOCONF, 0);
664 zassert_not_null(ifaddr, "IPv6 ll address autoconf add failed");
665
666 ifaddr = net_if_ipv6_addr_add(iface, &ml_eid, NET_ADDR_AUTOCONF, 0);
667 zassert_not_null(ifaddr, "IPv6 ll address autoconf add failed");
668
669 ifaddr->is_mesh_local = true;
670
671 zassert_true(net_ipv6_is_addr_mcast_mesh(&ml_mcast),
672 "IPv6 multicast mesh address check failed");
673
674 out = net_if_ipv6_select_src_addr(iface, &ll_mcast);
675 zassert_not_null(out, "IPv6 src addr selection failed\n");
676
677 DBG("IPv6: destination: %s - selected %s\n",
678 net_sprint_ipv6_addr(&ll_mcast), net_sprint_ipv6_addr(out));
679
680 zassert_false(memcmp(out->s6_addr, &lla.s6_addr,
681 sizeof(struct in6_addr)),
682 "IPv6 wrong src address selected\n");
683
684 out = net_if_ipv6_select_src_addr(iface, &ml_mcast);
685 zassert_not_null(out, "IPv6 src addr selection failed\n");
686
687 DBG("IPv6: destination: %s - selected %s\n",
688 net_sprint_ipv6_addr(&ml_mcast), net_sprint_ipv6_addr(out));
689
690 zassert_false(memcmp(out->s6_addr, &ml_eid.s6_addr,
691 sizeof(struct in6_addr)),
692 "IPv6 wrong src address selected\n");
693
694 zassert_true(net_if_ipv6_addr_rm(iface, &lla),
695 "IPv6 removing address failed\n");
696 zassert_true(net_if_ipv6_addr_rm(iface, &ml_eid),
697 "IPv6 removing address failed\n");
698 }
699
ZTEST(ip_addr_fn,test_private_ipv6_addresses)700 ZTEST(ip_addr_fn, test_private_ipv6_addresses)
701 {
702 bool ret;
703 struct {
704 struct in6_addr addr;
705 bool is_private;
706 } addrs[] = {
707 {
708 .addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
709 0, 0, 0, 0, 0, 0, 0x99, 0x1 } } },
710 .is_private = true,
711 },
712 {
713 .addr = { { { 0xfc, 0x01, 0, 0, 0, 0, 0, 0,
714 0, 0, 0, 0, 0, 0, 0, 1 } } },
715 .is_private = true,
716 },
717 {
718 .addr = { { { 0xfc, 0, 0, 0, 0, 0, 0, 0,
719 0, 0, 0, 0, 0, 0, 0, 2 } } },
720 .is_private = true,
721 },
722 {
723 .addr = { { { 0x20, 0x01, 0x1d, 0xb8, 0, 0, 0, 0,
724 0, 0, 0, 0, 0, 0, 0x99, 0x1 } } },
725 .is_private = false,
726 },
727 };
728
729 for (int i = 0; i < ARRAY_SIZE(addrs); i++) {
730 ret = net_ipv6_is_private_addr(&addrs[i].addr);
731 zassert_equal(ret, addrs[i].is_private, "Address %s check failed",
732 net_sprint_ipv6_addr(&addrs[i].addr));
733 }
734
735 }
736
ZTEST(ip_addr_fn,test_private_ipv4_addresses)737 ZTEST(ip_addr_fn, test_private_ipv4_addresses)
738 {
739 bool ret;
740 struct {
741 struct in_addr addr;
742 bool is_private;
743 } addrs[] = {
744 {
745 .addr = { { { 192, 0, 2, 1 } } },
746 .is_private = true,
747 },
748 {
749 .addr = { { { 10, 1, 2, 1 } } },
750 .is_private = true,
751 },
752 {
753 .addr = { { { 100, 124, 2, 1 } } },
754 .is_private = true,
755 },
756 {
757 .addr = { { { 172, 24, 100, 12 } } },
758 .is_private = true,
759 },
760 {
761 .addr = { { { 172, 15, 254, 255 } } },
762 .is_private = false,
763 },
764 {
765 .addr = { { { 172, 16, 0, 0 } } },
766 .is_private = true,
767 },
768 {
769 .addr = { { { 192, 168, 10, 122 } } },
770 .is_private = true,
771 },
772 {
773 .addr = { { { 192, 51, 100, 255 } } },
774 .is_private = true,
775 },
776 {
777 .addr = { { { 203, 0, 113, 122 } } },
778 .is_private = true,
779 },
780 {
781 .addr = { { { 1, 2, 3, 4 } } },
782 .is_private = false,
783 },
784 {
785 .addr = { { { 192, 1, 32, 4 } } },
786 .is_private = false,
787 },
788 };
789
790 for (int i = 0; i < ARRAY_SIZE(addrs); i++) {
791 ret = net_ipv4_is_private_addr(&addrs[i].addr);
792 zassert_equal(ret, addrs[i].is_private, "Address %s check failed",
793 net_sprint_ipv4_addr(&addrs[i].addr));
794 }
795
796 }
797
clear_addr4(struct net_if * iface,struct net_if_addr * addr,void * user_data)798 void clear_addr4(struct net_if *iface, struct net_if_addr *addr, void *user_data)
799 {
800 addr->is_used = false;
801 }
802
test_before(void * f)803 static void test_before(void *f)
804 {
805 ARG_UNUSED(f);
806
807 net_if_ipv4_addr_foreach(default_iface, clear_addr4, NULL);
808 net_if_ipv4_addr_foreach(second_iface, clear_addr4, NULL);
809 }
810
test_setup(void)811 void *test_setup(void)
812 {
813 default_iface = net_if_get_by_index(1);
814 second_iface = net_if_get_by_index(2);
815
816 return NULL;
817 }
818
819 ZTEST_SUITE(ip_addr_fn, NULL, test_setup, test_before, NULL, NULL);
820