Skip to content

Commit 2709b14

Browse files
rveerama1jhedberg
authored andcommitted
tests: net: Add wrong options length case
Wrong option length in IPv4 header options testcase added. This should cover malformed packet case. Signed-off-by: Ravi kumar Veeramally <[email protected]>
1 parent 22f93b3 commit 2709b14

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

tests/net/icmpv4/src/main.c

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ static const unsigned char icmpv4_echo_rep[] = {
5555
0x16, 0x50, 0x00, 0x00, 0x01, 0xfd, 0x56, 0xa0 };
5656

5757
static const unsigned char icmpv4_echo_req_opt[] = {
58-
5958
/* IPv4 Header */
6059
0x4e, 0x00, 0x00, 0x78, 0xe1, 0x4b, 0x40, 0x00,
6160
0x40, 0x01, 0x9a, 0x83, 0xc0, 0x00, 0x02, 0x02,
@@ -78,6 +77,35 @@ static const unsigned char icmpv4_echo_req_opt[] = {
7877
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
7978
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
8079

80+
static const unsigned char icmpv4_echo_req_opt_bad[] = {
81+
/* IPv4 Header */
82+
0x46, 0x00, 0x00, 0xa0, 0xf8, 0x6c, 0x00, 0x00,
83+
0x64, 0x01, 0x56, 0xa8, 0xc0, 0x00, 0x02, 0x02,
84+
0xc0, 0x00, 0x02, 0x01,
85+
86+
/* IPv4 Header Options (Wrong length) */
87+
0x41, 0x03, 0x41, 0x41,
88+
89+
/* ICMP Header (Echo Request) */
90+
0x08, 0x00, 0x06, 0xb8, 0x30, 0x31, 0x32, 0x07,
91+
/* Payload */
92+
0x80, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
93+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
94+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
95+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
96+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
97+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
98+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
99+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
100+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
101+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
102+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
103+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
104+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
105+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
106+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
107+
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x00 };
108+
81109
static enum net_verdict handle_reply_msg(struct net_pkt *pkt,
82110
struct net_ipv4_hdr *ip_hdr,
83111
struct net_icmp_hdr *icmp_hdr)
@@ -272,6 +300,36 @@ void test_icmpv4(void)
272300
net_icmpv4_unregister_handler(&echo_req_opt_handler);
273301

274302
net_pkt_unref(pkt);
303+
304+
/* ================ Echo Request with Bad Options ================= */
305+
net_icmpv4_register_handler(&echo_req_opt_handler);
306+
307+
pkt = net_pkt_alloc_with_buffer(NULL, sizeof(icmpv4_echo_req_opt_bad),
308+
AF_UNSPEC, 0, K_SECONDS(1));
309+
zassert_not_null(pkt, "Allocation failed");
310+
311+
net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr));
312+
net_pkt_write(pkt, icmpv4_echo_req_opt_bad,
313+
sizeof(icmpv4_echo_req_opt_bad));
314+
315+
net_pkt_cursor_init(pkt);
316+
317+
net_pkt_set_ipv4_opts_len(pkt, 4);
318+
net_pkt_set_overwrite(pkt, true);
319+
320+
net_pkt_skip(pkt, sizeof(struct net_ipv4_hdr)); /* Header*/
321+
net_pkt_skip(pkt, 4); /* Options length */
322+
323+
hdr = (struct net_ipv4_hdr *)pkt->buffer->data;
324+
325+
ret = net_icmpv4_input(pkt, hdr);
326+
327+
/** TESTPOINT: Check input */
328+
zassert_true((ret == NET_DROP), "Packet should drop");
329+
330+
net_icmpv4_unregister_handler(&echo_req_opt_handler);
331+
332+
net_pkt_unref(pkt);
275333
}
276334

277335
/**test case main entry*/

0 commit comments

Comments
 (0)