Skip to content

Commit 7586047

Browse files
jukkarkartben
authored andcommitted
tests: net: ipv6: Fix deprecated address selection
Make sure the IPv6 address selection works as specified. Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
1 parent da13b51 commit 7586047

File tree

3 files changed

+144
-46
lines changed

3 files changed

+144
-46
lines changed

subsys/net/ip/net_if.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5286,6 +5286,15 @@ static struct net_if_addr *get_ifaddr(struct net_if *iface,
52865286
return ifaddr;
52875287
}
52885288

5289+
/* This helper function is used only in tests. */
5290+
#if defined(CONFIG_NET_TEST)
5291+
struct net_if_addr *net_if_ipv6_get_ifaddr(struct net_if *iface,
5292+
const void *addr)
5293+
{
5294+
return get_ifaddr(iface, AF_INET6, addr, NULL);
5295+
}
5296+
#endif /* CONFIG_NET_TEST */
5297+
52895298
static void remove_ipv6_ifaddr(struct net_if *iface,
52905299
struct net_if_addr *ifaddr,
52915300
unsigned int maddr_count)

tests/net/ip-addr/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CONFIG_NET_IF_MAX_IPV6_COUNT=2
1818
CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3
1919
CONFIG_NET_IF_UNICAST_IPV4_ADDR_COUNT=2
2020
CONFIG_NET_IF_MCAST_IPV4_ADDR_COUNT=3
21+
CONFIG_NET_IF_IPV6_PREFIX_COUNT=4
2122
CONFIG_NET_L2_DUMMY=y
2223
CONFIG_NET_L2_ETHERNET=n
2324
CONFIG_ZTEST=y

tests/net/ip-addr/src/main.c

Lines changed: 134 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ LOG_MODULE_REGISTER(net_test, CONFIG_NET_IPV6_LOG_LEVEL);
3434
#define DBG(fmt, ...)
3535
#endif
3636

37+
extern struct net_if_addr *net_if_ipv6_get_ifaddr(struct net_if *iface,
38+
const void *addr);
39+
3740
static struct net_if *default_iface;
3841
static struct net_if *second_iface;
3942

@@ -210,6 +213,43 @@ ZTEST(ip_addr_fn, test_ip_addresses)
210213
TEST_IPV4(127, 0, 0, 1, "127.0.0.1");
211214
}
212215

216+
#if defined(CONFIG_NET_IPV6_LOG_LEVEL) || defined(CONFIG_NET_IPV4_LOG_LEVEL)
217+
static const char *addr_state_to_str(enum net_addr_state state)
218+
{
219+
switch (state) {
220+
case NET_ADDR_PREFERRED:
221+
return "preferred";
222+
case NET_ADDR_DEPRECATED:
223+
return "deprecated";
224+
case NET_ADDR_TENTATIVE:
225+
return "tentative";
226+
case NET_ADDR_ANY_STATE:
227+
return "invalid";
228+
default:
229+
break;
230+
}
231+
232+
return "unknown";
233+
}
234+
235+
static const char *get_addr_state(struct net_if *iface,
236+
const struct in6_addr *addr)
237+
{
238+
struct net_if_addr *ifaddr;
239+
240+
if (iface == NULL) {
241+
return "<iface not set>";
242+
}
243+
244+
ifaddr = net_if_ipv6_get_ifaddr(iface, addr);
245+
if (ifaddr) {
246+
return addr_state_to_str(ifaddr->addr_state);
247+
}
248+
249+
return "<addr not found>";
250+
}
251+
#endif
252+
213253
ZTEST(ip_addr_fn, test_ipv6_addresses)
214254
{
215255
struct in6_addr loopback = IN6ADDR_LOOPBACK_INIT;
@@ -227,16 +267,16 @@ ZTEST(ip_addr_fn, test_ipv6_addresses)
227267
struct in6_addr ula = { { { 0xfc, 0x00, 0xaa, 0xaa, 0, 0, 0, 0,
228268
0, 0, 0, 0, 0xd1, 0xd2, 0xd3, 0xd4 } } };
229269
struct in6_addr ula2 = { { { 0xfc, 0x00, 0xaa, 0xaa, 0, 0, 0, 0,
230-
0, 0, 0, 0, 0x1, 0x2, 0x3, 0x4 } } };
270+
0, 0, 0, 0, 0xd1, 0xd2, 0xd3, 2 } } };
231271
struct in6_addr ula3 = { { { 0xfc, 0x00, 0xaa, 0xaa, 0, 0, 0, 0,
232-
0, 0, 0, 0, 0xf1, 0xf2, 0xf3, 0xf4 } } };
272+
0, 0, 0, 0, 0xd1, 0xd2, 0xf3, 3 } } };
233273
struct in6_addr ula4 = { { { 0xfc, 0x00, 0xaa, 0xaa, 0, 0, 0, 0,
234-
0, 0, 0, 0, 0xf1, 0xf2, 0xf3, 0xf5 } } };
274+
0, 0, 0, 0, 0xd1, 0xd2, 0xf3, 4 } } };
235275
struct in6_addr ula5 = { { { 0xfc, 0x00, 0xaa, 0xaa, 0, 0, 0, 0,
236-
0, 0, 0, 0, 0xf1, 0xf2, 0xf3, 0xf6 } } };
276+
0, 0, 0, 0, 0xd1, 0xd2, 0xd3, 0xd5 } } };
237277
struct in6_addr *tmp;
238278
const struct in6_addr *out;
239-
struct net_if_addr *ifaddr1, *ifaddr2;
279+
struct net_if_addr *ifaddr1, *ifaddr2, *ifaddr_ula, *ifaddr_ula3, *ifaddr_ula4;
240280
struct net_if_mcast_addr *ifmaddr1;
241281
struct net_if_ipv6_prefix *prefix;
242282
struct net_if *iface;
@@ -339,8 +379,10 @@ ZTEST(ip_addr_fn, test_ipv6_addresses)
339379
"IPv6 src addr selection failed, iface %p\n",
340380
iface);
341381

342-
DBG("Selected IPv6 address %s, iface %p\n",
343-
net_sprint_ipv6_addr(out), iface);
382+
DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
383+
net_sprint_ipv6_addr(out),
384+
get_addr_state(iface, out),
385+
iface);
344386

345387
zassert_false(memcmp(out->s6_addr, &addr6_pref2.s6_addr,
346388
sizeof(struct in6_addr)),
@@ -352,8 +394,9 @@ ZTEST(ip_addr_fn, test_ipv6_addresses)
352394
zassert_not_null(out, "IPv6 src any addr selection failed, "
353395
"iface %p\n", iface);
354396

355-
DBG("Selected IPv6 address %s, iface %p\n",
356-
net_sprint_ipv6_addr(out), iface);
397+
DBG("%d: Selected IPv6 address %s, iface %p\n", __LINE__,
398+
net_sprint_ipv6_addr(out),
399+
iface);
357400

358401
zassert_false(memcmp(out->s6_addr, &any.s6_addr,
359402
sizeof(struct in6_addr)),
@@ -367,8 +410,10 @@ ZTEST(ip_addr_fn, test_ipv6_addresses)
367410
zassert_not_null(out, "IPv6 src ll addr selection failed, "
368411
"iface %p\n", iface);
369412

370-
DBG("Selected IPv6 address %s, iface %p\n",
371-
net_sprint_ipv6_addr(out), iface);
413+
DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
414+
net_sprint_ipv6_addr(out),
415+
iface != NULL ? get_addr_state(iface, out) : "unknown",
416+
iface);
372417

373418
zassert_false(memcmp(out->s6_addr, &addr6.s6_addr,
374419
sizeof(struct in6_addr)),
@@ -384,72 +429,115 @@ ZTEST(ip_addr_fn, test_ipv6_addresses)
384429
/**TESTPOINTS: Check what IPv6 address is selected when some
385430
* addresses are in preferred state and some in deprecated state.
386431
*/
387-
ifaddr2 = net_if_ipv6_addr_add(default_iface, &ula,
388-
NET_ADDR_AUTOCONF, 0);
389-
zassert_not_null(ifaddr2, "IPv6 ula address add failed");
432+
prefix = net_if_ipv6_prefix_add(default_iface, &ula, 96, 3600);
433+
zassert_not_null(prefix, "IPv6 ula prefix add failed");
390434

391-
ifaddr2->addr_state = NET_ADDR_PREFERRED;
435+
prefix = net_if_ipv6_prefix_add(default_iface, &ula2, 64, 3600);
436+
zassert_not_null(prefix, "IPv6 ula prefix add failed");
437+
438+
ifaddr_ula = net_if_ipv6_addr_add(default_iface, &ula,
439+
NET_ADDR_AUTOCONF, 0);
440+
zassert_not_null(ifaddr_ula, "IPv6 ula address add failed");
441+
442+
ifaddr_ula->addr_state = NET_ADDR_PREFERRED;
392443

393444
out = net_if_ipv6_select_src_addr(default_iface, &ula2);
394445
zassert_not_null(out, "IPv6 src ula addr selection failed, "
395446
"iface %p\n", default_iface);
396447

397-
DBG("Selected IPv6 address %s, iface %p\n",
398-
net_sprint_ipv6_addr(out), iface);
448+
DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
449+
net_sprint_ipv6_addr(out), get_addr_state(default_iface, out),
450+
iface);
399451

400452
zassert_false(memcmp(out->s6_addr, &ula.s6_addr, sizeof(struct in6_addr)),
401453
"IPv6 wrong src ula address selected, iface %p\n", iface);
402454

403455
/* Allow selection of deprecated address if no other address
404456
* is available.
405457
*/
406-
ifaddr2->addr_state = NET_ADDR_DEPRECATED;
458+
ifaddr_ula->addr_state = NET_ADDR_DEPRECATED;
407459

408460
out = net_if_ipv6_select_src_addr(default_iface, &ula3);
409461
zassert_not_null(out, "IPv6 src ula addr selection failed, "
410462
"iface %p\n", default_iface);
411463

464+
/* Back to preferred state so that later checks work correctly */
465+
ifaddr_ula->addr_state = NET_ADDR_PREFERRED;
466+
412467
/* Then add another address with preferred state and check that we
413-
* still select the deprecated address as it is a better match.
468+
* still do not select the deprecated address even if it is a better match.
414469
*/
415-
ifaddr2 = net_if_ipv6_addr_add(default_iface, &ula3,
416-
NET_ADDR_AUTOCONF, 0);
417-
zassert_not_null(ifaddr2, "IPv6 ula address add failed");
470+
ifaddr_ula3 = net_if_ipv6_addr_add(default_iface, &ula3,
471+
NET_ADDR_AUTOCONF, 0);
472+
zassert_not_null(ifaddr_ula3, "IPv6 ula address add failed");
418473

419-
ifaddr2->addr_state = NET_ADDR_PREFERRED;
474+
ifaddr_ula3->addr_state = NET_ADDR_PREFERRED;
420475

421476
out = net_if_ipv6_select_src_addr(default_iface, &ula2);
422477
zassert_not_null(out, "IPv6 src ula addr selection failed, "
423478
"iface %p\n", default_iface);
424479

425-
DBG("Selected IPv6 address %s, iface %p\n",
426-
net_sprint_ipv6_addr(out), iface);
480+
DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
481+
net_sprint_ipv6_addr(out), get_addr_state(default_iface, out),
482+
iface);
427483

428484
zassert_false(memcmp(out->s6_addr, &ula3.s6_addr, sizeof(struct in6_addr)),
429485
"IPv6 wrong src ula address selected, iface %p\n", iface);
430486

431-
zassert_true(net_if_ipv6_addr_rm(default_iface, &ula),
432-
"IPv6 removing address failed\n");
487+
/* Then change the address to deprecated state and check that we
488+
* do select the deprecated address.
489+
*/
490+
ifaddr_ula3->addr_state = NET_ADDR_DEPRECATED;
433491

434-
prefix = net_if_ipv6_prefix_add(default_iface, &ula4, 96, 3600);
435-
zassert_not_null(prefix, "IPv6 ula prefix add failed");
492+
out = net_if_ipv6_select_src_addr(default_iface, &ula2);
493+
zassert_not_null(out, "IPv6 src ula addr selection failed, "
494+
"iface %p\n", default_iface);
436495

437-
ifaddr1 = net_if_ipv6_addr_add(default_iface, &ula4,
438-
NET_ADDR_AUTOCONF, 0);
439-
zassert_not_null(ifaddr1, "IPv6 ula address add failed");
496+
DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
497+
net_sprint_ipv6_addr(out), get_addr_state(default_iface, out),
498+
iface);
440499

441-
ifaddr2->addr_state = NET_ADDR_DEPRECATED;
500+
zassert_false(memcmp(out->s6_addr, &ula.s6_addr, sizeof(struct in6_addr)),
501+
"IPv6 wrong src ula address selected, iface %p\n", iface);
502+
503+
/* Then have two deprecated addresses */
504+
ifaddr_ula->addr_state = NET_ADDR_DEPRECATED;
505+
506+
out = net_if_ipv6_select_src_addr(default_iface, &ula2);
507+
zassert_not_null(out, "IPv6 src ula addr selection failed, "
508+
"iface %p\n", default_iface);
509+
510+
DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
511+
net_sprint_ipv6_addr(out), get_addr_state(default_iface, out),
512+
iface);
442513

514+
zassert_false(memcmp(out->s6_addr, &ula3.s6_addr, sizeof(struct in6_addr)),
515+
"IPv6 wrong src ula address selected, iface %p\n", iface);
516+
517+
ifaddr_ula4 = net_if_ipv6_addr_add(default_iface, &ula4,
518+
NET_ADDR_AUTOCONF, 0);
519+
zassert_not_null(ifaddr_ula4, "IPv6 ula address add failed");
520+
521+
ifaddr_ula4->addr_state = NET_ADDR_DEPRECATED;
522+
ifaddr_ula3->addr_state = NET_ADDR_PREFERRED;
523+
524+
/* There is now one preferred and two deprecated addresses.
525+
* The preferred address should be selected.
526+
*/
443527
out = net_if_ipv6_select_src_addr(default_iface, &ula5);
444528
zassert_not_null(out, "IPv6 src ula addr selection failed, "
445529
"iface %p\n", default_iface);
446530

447-
DBG("Selected IPv6 address %s, iface %p\n",
448-
net_sprint_ipv6_addr(out), iface);
531+
DBG("%d: Selected IPv6 address %s state %s, iface %p\n", __LINE__,
532+
net_sprint_ipv6_addr(out), get_addr_state(default_iface, out),
533+
iface);
449534

450-
zassert_false(memcmp(out->s6_addr, &ula4.s6_addr, sizeof(struct in6_addr)),
535+
zassert_false(memcmp(out->s6_addr, &ula3.s6_addr, sizeof(struct in6_addr)),
451536
"IPv6 wrong src ula address selected, iface %p\n", iface);
452537

538+
zassert_true(net_if_ipv6_addr_rm(default_iface, &ula),
539+
"IPv6 removing address failed\n");
540+
453541
zassert_true(net_if_ipv6_addr_rm(default_iface, &ula3),
454542
"IPv6 removing address failed\n");
455543

@@ -489,8 +577,8 @@ ZTEST(ip_addr_fn, test_ipv4_ll_address_select_default_first)
489577
zassert_not_null(out, "IPv4 src addr selection failed, iface %p\n",
490578
iface);
491579

492-
DBG("Selected IPv4 address %s, iface %p\n", net_sprint_ipv4_addr(out),
493-
iface);
580+
DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
581+
net_sprint_ipv4_addr(out), iface);
494582

495583
zassert_equal_ptr(iface, default_iface, "Wrong iface selected");
496584
zassert_equal(out->s_addr, lladdr4_1.s_addr,
@@ -528,8 +616,8 @@ ZTEST(ip_addr_fn, test_ipv4_ll_address_select)
528616
zassert_not_null(out, "IPv4 src addr selection failed, iface %p\n",
529617
iface);
530618

531-
DBG("Selected IPv4 address %s, iface %p\n", net_sprint_ipv4_addr(out),
532-
iface);
619+
DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
620+
net_sprint_ipv4_addr(out), iface);
533621

534622
zassert_equal(out->s_addr, lladdr4_1.s_addr,
535623
"IPv4 wrong src address selected, iface %p\n", iface);
@@ -540,8 +628,8 @@ ZTEST(ip_addr_fn, test_ipv4_ll_address_select)
540628
zassert_not_null(out, "IPv4 src addr selection failed, iface %p\n",
541629
iface);
542630

543-
DBG("Selected IPv4 address %s, iface %p\n", net_sprint_ipv4_addr(out),
544-
iface);
631+
DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
632+
net_sprint_ipv4_addr(out), iface);
545633

546634
zassert_equal(out->s_addr, lladdr4_2.s_addr,
547635
"IPv4 wrong src address selected, iface %p\n", iface);
@@ -606,7 +694,7 @@ ZTEST(ip_addr_fn, test_ipv4_addresses)
606694
zassert_not_null(out, "IPv4 src addr selection failed, "
607695
"iface %p\n", iface);
608696

609-
DBG("Selected IPv4 address %s, iface %p\n",
697+
DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
610698
net_sprint_ipv4_addr(out), iface);
611699

612700
zassert_equal(out->s_addr, addr4.s_addr,
@@ -618,7 +706,7 @@ ZTEST(ip_addr_fn, test_ipv4_addresses)
618706
zassert_not_null(out, "IPv4 src ll addr selection failed, "
619707
"iface %p\n", iface);
620708

621-
DBG("Selected IPv4 address %s, iface %p\n",
709+
DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
622710
net_sprint_ipv4_addr(out), iface);
623711

624712
zassert_equal(out->s_addr, lladdr4.s_addr,
@@ -630,7 +718,7 @@ ZTEST(ip_addr_fn, test_ipv4_addresses)
630718
zassert_not_null(out, "IPv4 src any addr selection failed, "
631719
"iface %p\n", iface);
632720

633-
DBG("Selected IPv4 address %s, iface %p\n",
721+
DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
634722
net_sprint_ipv4_addr(out), iface);
635723

636724
zassert_equal(out->s_addr, addr4.s_addr,
@@ -642,7 +730,7 @@ ZTEST(ip_addr_fn, test_ipv4_addresses)
642730
zassert_not_null(out, "IPv4 src any addr selection failed, "
643731
"iface %p\n", iface);
644732

645-
DBG("Selected IPv4 address %s, iface %p\n",
733+
DBG("%d: Selected IPv4 address %s, iface %p\n", __LINE__,
646734
net_sprint_ipv4_addr(out), iface);
647735

648736
zassert_equal(out->s_addr, addr4.s_addr,

0 commit comments

Comments
 (0)