Skip to content

Commit b06f55f

Browse files
rlubosnashif
authored andcommitted
tests: net: icmp: Add test cases for ICMP family mismatch
Add test cases that respective ICMPv4/ICMPv6 handlers are not called for packets where address family doesn't match (i.e. malformed ICMPv4 packet imitating ICMPv6 one). Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no> (cherry picked from commit 486e824)
1 parent 164f735 commit b06f55f

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

tests/net/icmp/src/main.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,126 @@ ZTEST(icmp_tests, test_offload_icmpv6_echo_request)
620620
#endif
621621
#endif /* CONFIG_NET_OFFLOADING_SUPPORT */
622622

623+
/* Need to have both IPv4/IPv6 for those */
624+
#if defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_IPV6)
625+
static K_SEM_DEFINE(test_req_sem, 0, 1);
626+
627+
static int icmp_request_handler(struct net_icmp_ctx *ctx,
628+
struct net_pkt *pkt,
629+
struct net_icmp_ip_hdr *hdr,
630+
struct net_icmp_hdr *icmp_hdr,
631+
void *user_data)
632+
{
633+
k_sem_give(&test_req_sem);
634+
635+
return 0;
636+
}
637+
638+
ZTEST(icmp_tests, test_malformed_icmpv6_echo_request_on_ipv4)
639+
{
640+
struct in_addr dst4 = { 0 };
641+
const struct in_addr *src4;
642+
struct net_icmp_ctx ctx;
643+
struct net_if *iface;
644+
struct net_pkt *pkt;
645+
int ret;
646+
647+
k_sem_reset(&test_req_sem);
648+
649+
ret = net_icmp_init_ctx(&ctx, AF_INET6, NET_ICMPV6_ECHO_REQUEST, 0,
650+
icmp_request_handler);
651+
zassert_equal(ret, 0, "Cannot init ICMP (%d)", ret);
652+
653+
memcpy(&dst4, &recv_addr_4, sizeof(recv_addr_4));
654+
655+
/* Prepare malformed NET_ICMPV6_ECHO_REQUEST on IPv4 packet */
656+
iface = net_if_ipv4_select_src_iface(&dst4);
657+
zassert_not_null(iface, "NULL iface");
658+
659+
src4 = net_if_ipv4_select_src_addr(iface, &dst4);
660+
zassert_not_null(src4, "NULL addr");
661+
662+
pkt = net_pkt_alloc_with_buffer(iface, sizeof(struct net_icmpv4_echo_req),
663+
AF_INET, IPPROTO_ICMP, K_MSEC(100));
664+
zassert_not_null(pkt, "NULL pkt");
665+
666+
if (net_ipv4_create(pkt, src4, &dst4) != 0 ||
667+
net_icmpv4_create(pkt, NET_ICMPV6_ECHO_REQUEST, 0) != 0) {
668+
net_pkt_unref(pkt);
669+
zassert_true(false, "Failed to create ICMP packet");
670+
}
671+
672+
net_pkt_cursor_init(pkt);
673+
net_ipv4_finalize(pkt, IPPROTO_ICMP);
674+
675+
if (net_send_data(pkt) != 0) {
676+
net_pkt_unref(pkt);
677+
zassert_true(false, "Failed to send packet");
678+
}
679+
680+
ret = k_sem_take(&test_req_sem, K_MSEC(100));
681+
if (ret != -EAGAIN) {
682+
(void)net_icmp_cleanup_ctx(&ctx);
683+
zassert_true(false, "ICMP request shouldn't be processed");
684+
}
685+
686+
ret = net_icmp_cleanup_ctx(&ctx);
687+
zassert_equal(ret, 0, "Cannot cleanup ICMP (%d)", ret);
688+
}
689+
690+
ZTEST(icmp_tests, test_malformed_icmpv4_echo_request_on_ipv6)
691+
{
692+
struct in6_addr dst6 = { 0 };
693+
const struct in6_addr *src6;
694+
struct net_icmp_ctx ctx;
695+
struct net_if *iface;
696+
struct net_pkt *pkt;
697+
int ret;
698+
699+
k_sem_reset(&test_req_sem);
700+
701+
ret = net_icmp_init_ctx(&ctx, AF_INET, NET_ICMPV4_ECHO_REQUEST, 0,
702+
icmp_request_handler);
703+
zassert_equal(ret, 0, "Cannot init ICMP (%d)", ret);
704+
705+
memcpy(&dst6, &recv_addr_6, sizeof(recv_addr_6));
706+
707+
/* Prepare malformed NET_ICMPV4_ECHO_REQUEST on IPv6 packet */
708+
iface = net_if_ipv6_select_src_iface(&dst6);
709+
zassert_not_null(iface, "NULL iface");
710+
711+
src6 = net_if_ipv6_select_src_addr(iface, &dst6);
712+
zassert_not_null(src6, "NULL addr");
713+
714+
pkt = net_pkt_alloc_with_buffer(iface, sizeof(struct net_icmpv6_echo_req),
715+
AF_INET6, IPPROTO_ICMPV6, K_MSEC(100));
716+
zassert_not_null(pkt, "NULL pkt");
717+
718+
if (net_ipv6_create(pkt, src6, &dst6) != 0 ||
719+
net_icmpv6_create(pkt, NET_ICMPV4_ECHO_REQUEST, 0) != 0) {
720+
net_pkt_unref(pkt);
721+
zassert_true(false, "Failed to create ICMP packet");
722+
}
723+
724+
net_pkt_cursor_init(pkt);
725+
net_ipv6_finalize(pkt, IPPROTO_ICMPV6);
726+
727+
if (net_send_data(pkt) != 0) {
728+
net_pkt_unref(pkt);
729+
zassert_true(false, "Failed to send packet");
730+
}
731+
732+
ret = k_sem_take(&test_req_sem, K_MSEC(100));
733+
if (ret != -EAGAIN) {
734+
(void)net_icmp_cleanup_ctx(&ctx);
735+
zassert_true(false, "ICMP request shouldn't be processed");
736+
}
737+
738+
ret = net_icmp_cleanup_ctx(&ctx);
739+
zassert_equal(ret, 0, "Cannot cleanup ICMP (%d)", ret);
740+
}
741+
#endif /* defined(CONFIG_NET_IPV4) && defined(CONFIG_NET_IPV6) */
742+
623743
static void *setup(void)
624744
{
625745
if (IS_ENABLED(CONFIG_NET_TC_THREAD_COOPERATIVE)) {

0 commit comments

Comments
 (0)