1 /* main.c - Application main entry point
2 *
3 * This is a self-contained test for exercising the TCP protocol stack. Both
4 * the server and client side run on ths same device using a DUMMY interface
5 * as a loopback of the IP packets.
6 *
7 */
8
9 /*
10 * Copyright (c) 2020 Intel Corporation
11 *
12 * SPDX-License-Identifier: Apache-2.0
13 */
14
15 #include <zephyr/logging/log.h>
16 LOG_MODULE_REGISTER(net_test, CONFIG_NET_TCP_LOG_LEVEL);
17
18 #include <errno.h>
19 #include <zephyr/types.h>
20 #include <stddef.h>
21 #include <string.h>
22 #include <zephyr/sys/printk.h>
23 #include <zephyr/linker/sections.h>
24 #include <zephyr/tc_util.h>
25
26 #include <zephyr/misc/lorem_ipsum.h>
27 #include <zephyr/net/ethernet.h>
28 #include <zephyr/net/dummy.h>
29 #include <zephyr/net/net_pkt.h>
30
31 #include "ipv4.h"
32 #include "ipv6.h"
33 #include "tcp.h"
34 #include "tcp_private.h"
35 #include "net_stats.h"
36
37 #include <zephyr/ztest.h>
38
39 #define MY_PORT 4242
40 #define PEER_PORT 4242
41
42 /* Data (1280 bytes) to be sent */
43 static const char lorem_ipsum[] = LOREM_IPSUM;
44
45 static struct in_addr my_addr = { { { 192, 0, 2, 1 } } };
46 static struct sockaddr_in my_addr_s = {
47 .sin_family = AF_INET,
48 .sin_port = htons(PEER_PORT),
49 .sin_addr = { { { 192, 0, 2, 1 } } },
50 };
51
52 static struct in_addr peer_addr = { { { 192, 0, 2, 2 } } };
53 static struct sockaddr_in peer_addr_s = {
54 .sin_family = AF_INET,
55 .sin_port = htons(PEER_PORT),
56 .sin_addr = { { { 192, 0, 2, 2 } } },
57 };
58
59 static struct in6_addr my_addr_v6 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
60 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
61 static struct sockaddr_in6 my_addr_v6_s = {
62 .sin6_family = AF_INET6,
63 .sin6_port = htons(PEER_PORT),
64 .sin6_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0x1 } } },
66 };
67
68 static struct in6_addr peer_addr_v6 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0x2 } } };
70 static struct sockaddr_in6 peer_addr_v6_s = {
71 .sin6_family = AF_INET6,
72 .sin6_port = htons(PEER_PORT),
73 .sin6_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0x2 } } },
75 };
76
77 static struct net_if *net_iface;
78 static uint32_t seq;
79 static uint32_t device_initial_seq;
80 static uint32_t ack;
81
82 static K_SEM_DEFINE(test_sem, 0, 1);
83 static bool sem;
84
85 enum test_state {
86 T_SYN = 0,
87 T_SYN_ACK,
88 T_DATA,
89 T_DATA_ACK,
90 T_FIN,
91 T_FIN_ACK,
92 T_FIN_1,
93 T_FIN_2,
94 T_CLOSING,
95 T_RST,
96 };
97
98 static enum test_case_no {
99 TEST_CLIENT_IPV4 = 1,
100 TEST_CLIENT_IPV6 = 2,
101 TEST_SERVER_IPV4 = 3,
102 TEST_SERVER_WITH_OPTIONS_IPV4 = 4,
103 TEST_SERVER_IPV6 = 5,
104 TEST_CLIENT_SYN_RESEND = 6,
105 TEST_CLIENT_FIN_WAIT_2_IPV4 = 7,
106 TEST_CLIENT_CLOSING_IPV6 = 8,
107 TEST_SERVER_RECV_OUT_OF_ORDER_DATA = 9,
108 TEST_CLIENT_FIN_WAIT_1_RETRANSMIT_IPV4 = 10,
109 TEST_CLIENT_DATA_DURING_FIN_1_IPV4 = 11,
110 TEST_CLIENT_SYN_RST_ACK = 12,
111 TEST_SERVER_RST_ON_CLOSED_PORT = 13,
112 TEST_SERVER_RST_ON_LISTENING_PORT_NO_ACTIVE_CONNECTION = 14,
113 TEST_CLIENT_RST_ON_UNEXPECTED_ACK_ON_SYN = 15,
114 TEST_CLIENT_CLOSING_FAILURE_IPV6 = 16,
115 TEST_CLIENT_FIN_WAIT_2_IPV4_FAILURE = 17,
116 TEST_CLIENT_FIN_ACK_WITH_DATA = 18,
117 } test_case_no;
118
119 static enum test_state t_state;
120
121 static struct k_work_delayable test_server;
122 static void test_server_timeout(struct k_work *work);
123
124 static int tester_send(const struct device *dev, struct net_pkt *pkt);
125
126 static void handle_client_test(sa_family_t af, struct tcphdr *th);
127 static void handle_server_test(sa_family_t af, struct tcphdr *th);
128 static void handle_syn_resend(void);
129 static void handle_syn_rst_ack(sa_family_t af, struct tcphdr *th);
130 static void handle_client_fin_wait_2_test(sa_family_t af, struct tcphdr *th);
131 static void handle_client_fin_wait_2_failure_test(sa_family_t af, struct tcphdr *th);
132 static void handle_client_closing_test(sa_family_t af, struct tcphdr *th);
133 static void handle_client_closing_failure_test(sa_family_t af, struct tcphdr *th);
134 static void handle_data_fin1_test(sa_family_t af, struct tcphdr *th);
135 static void handle_data_during_fin1_test(sa_family_t af, struct tcphdr *th);
136 static void handle_server_recv_out_of_order(struct net_pkt *pkt);
137 static void handle_server_rst_on_closed_port(sa_family_t af, struct tcphdr *th);
138 static void handle_server_rst_on_listening_port(sa_family_t af, struct tcphdr *th);
139 static void handle_syn_invalid_ack(sa_family_t af, struct tcphdr *th);
140 static void handle_client_fin_ack_with_data_test(sa_family_t af, struct tcphdr *th);
141
verify_flags(struct tcphdr * th,uint8_t flags,const char * fun,int line)142 static void verify_flags(struct tcphdr *th, uint8_t flags,
143 const char *fun, int line)
144 {
145 if (!(th && FL(&th->th_flags, ==, flags))) {
146 zassert_true(false,
147 "%s:%d flags mismatch (0x%04x vs 0x%04x)",
148 fun, line, th->th_flags, flags);
149 }
150 }
151
152 #define test_verify_flags(_th, _flags) \
153 verify_flags(_th, _flags, __func__, __LINE__)
154
155 struct net_tcp_context {
156 uint8_t mac_addr[sizeof(struct net_eth_addr)];
157 struct net_linkaddr ll_addr;
158 };
159
net_tcp_dev_init(const struct device * dev)160 static int net_tcp_dev_init(const struct device *dev)
161 {
162 struct net_tcp_context *net_tcp_context = dev->data;
163
164 net_tcp_context = net_tcp_context;
165
166 return 0;
167 }
168
net_tcp_get_mac(const struct device * dev)169 static uint8_t *net_tcp_get_mac(const struct device *dev)
170 {
171 struct net_tcp_context *context = dev->data;
172
173 if (context->mac_addr[2] == 0x00) {
174 /* 00-00-5E-00-53-xx Documentation RFC 7042 */
175 context->mac_addr[0] = 0x00;
176 context->mac_addr[1] = 0x00;
177 context->mac_addr[2] = 0x5E;
178 context->mac_addr[3] = 0x00;
179 context->mac_addr[4] = 0x53;
180 context->mac_addr[5] = 0x01;
181 }
182
183 return context->mac_addr;
184 }
185
net_tcp_iface_init(struct net_if * iface)186 static void net_tcp_iface_init(struct net_if *iface)
187 {
188 uint8_t *mac = net_tcp_get_mac(net_if_get_device(iface));
189
190 net_if_set_link_addr(iface, mac, 6, NET_LINK_ETHERNET);
191 }
192
193 struct net_tcp_context net_tcp_context_data;
194
195 static struct dummy_api net_tcp_if_api = {
196 .iface_api.init = net_tcp_iface_init,
197 .send = tester_send,
198 };
199
200 NET_DEVICE_INIT(net_tcp_test, "net_tcp_test",
201 net_tcp_dev_init, NULL,
202 &net_tcp_context_data, NULL,
203 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
204 &net_tcp_if_api, DUMMY_L2,
205 NET_L2_GET_CTX_TYPE(DUMMY_L2), 127);
206
test_sem_give(void)207 static void test_sem_give(void)
208 {
209 sem = false;
210 k_sem_give(&test_sem);
211 }
212
test_sem_take(k_timeout_t timeout,int line)213 static void test_sem_take(k_timeout_t timeout, int line)
214 {
215 sem = true;
216
217 if (k_sem_take(&test_sem, timeout) != 0) {
218 zassert_true(false, "semaphore timed out (line %d)", line);
219 }
220 }
221
test_sem_take_failure(k_timeout_t timeout,int line)222 static void test_sem_take_failure(k_timeout_t timeout, int line)
223 {
224 sem = true;
225
226 if (k_sem_take(&test_sem, timeout) == 0) {
227 zassert_true(false, "semaphore succeed out (line %d)", line);
228 }
229 }
230
231 static uint8_t tcp_options[20] = {
232 0x02, 0x04, 0x05, 0xb4, /* Max segment */
233 0x04, 0x02, /* SACK */
234 0x08, 0x0a, 0xc2, 0x7b, 0xef, 0x0f, 0x00, 0x00, 0x00, 0x00, /* Time */
235 0x01, /* NOP */
236 0x03, 0x03, 0x07 /* Win scale*/ };
237
tester_prepare_tcp_pkt(sa_family_t af,uint16_t src_port,uint16_t dst_port,uint8_t flags,const uint8_t * data,size_t len)238 static struct net_pkt *tester_prepare_tcp_pkt(sa_family_t af,
239 uint16_t src_port,
240 uint16_t dst_port,
241 uint8_t flags,
242 const uint8_t *data,
243 size_t len)
244 {
245 NET_PKT_DATA_ACCESS_DEFINE(tcp_access, struct tcphdr);
246 struct net_pkt *pkt;
247 struct tcphdr *th;
248 uint8_t opts_len = 0;
249 int ret = -EINVAL;
250
251 if ((test_case_no == TEST_SERVER_WITH_OPTIONS_IPV4) && (flags & SYN)) {
252 opts_len = sizeof(tcp_options);
253 }
254
255 /* Allocate buffer */
256 pkt = net_pkt_alloc_with_buffer(net_iface,
257 sizeof(struct tcphdr) + len + opts_len,
258 af, IPPROTO_TCP, K_NO_WAIT);
259 if (!pkt) {
260 return NULL;
261 }
262
263 /* Create IP header */
264 if (af == AF_INET) {
265 ret = net_ipv4_create(pkt, &peer_addr, &my_addr);
266 } else if (af == AF_INET6) {
267 ret = net_ipv6_create(pkt, &peer_addr_v6, &my_addr_v6);
268 } else {
269 goto fail;
270 }
271
272 /* Create TCP header */
273 if (ret < 0) {
274 goto fail;
275 }
276
277 th = (struct tcphdr *)net_pkt_get_data(pkt, &tcp_access);
278 if (!th) {
279 goto fail;
280 }
281
282 memset(th, 0U, sizeof(struct tcphdr));
283
284 th->th_sport = src_port;
285 th->th_dport = dst_port;
286
287 if ((test_case_no == TEST_SERVER_WITH_OPTIONS_IPV4) && (flags & SYN)) {
288 th->th_off = 10U;
289 } else {
290 th->th_off = 5U;
291 }
292
293 th->th_flags = flags;
294 th->th_win = NET_IPV6_MTU;
295 th->th_seq = htonl(seq);
296
297 if (ACK & flags) {
298 th->th_ack = htonl(ack);
299 }
300
301 ret = net_pkt_set_data(pkt, &tcp_access);
302 if (ret < 0) {
303 goto fail;
304 }
305
306 if ((test_case_no == TEST_SERVER_WITH_OPTIONS_IPV4) && (flags & SYN)) {
307 /* Add TCP Options */
308 ret = net_pkt_write(pkt, tcp_options, opts_len);
309 if (ret < 0) {
310 goto fail;
311 }
312 }
313
314 if (data && len) {
315 ret = net_pkt_write(pkt, data, len);
316 if (ret < 0) {
317 goto fail;
318 }
319 }
320
321 net_pkt_cursor_init(pkt);
322
323 if (af == AF_INET) {
324 ret = net_ipv4_finalize(pkt, IPPROTO_TCP);
325 } else if (af == AF_INET6) {
326 ret = net_ipv6_finalize(pkt, IPPROTO_TCP);
327 } else {
328 goto fail;
329 }
330
331 if (ret < 0) {
332 goto fail;
333 }
334
335 return pkt;
336 fail:
337 net_pkt_unref(pkt);
338 return NULL;
339 }
340
prepare_syn_packet(sa_family_t af,uint16_t src_port,uint16_t dst_port)341 static struct net_pkt *prepare_syn_packet(sa_family_t af, uint16_t src_port,
342 uint16_t dst_port)
343 {
344 return tester_prepare_tcp_pkt(af, src_port, dst_port, SYN, NULL, 0U);
345 }
346
prepare_syn_ack_packet(sa_family_t af,uint16_t src_port,uint16_t dst_port)347 static struct net_pkt *prepare_syn_ack_packet(sa_family_t af, uint16_t src_port,
348 uint16_t dst_port)
349 {
350 return tester_prepare_tcp_pkt(af, src_port, dst_port, SYN | ACK,
351 NULL, 0U);
352 }
353
prepare_rst_ack_packet(sa_family_t af,uint16_t src_port,uint16_t dst_port)354 static struct net_pkt *prepare_rst_ack_packet(sa_family_t af, uint16_t src_port,
355 uint16_t dst_port)
356 {
357 return tester_prepare_tcp_pkt(af, src_port, dst_port, RST | ACK,
358 NULL, 0U);
359 }
360
prepare_ack_packet(sa_family_t af,uint16_t src_port,uint16_t dst_port)361 static struct net_pkt *prepare_ack_packet(sa_family_t af, uint16_t src_port,
362 uint16_t dst_port)
363 {
364 return tester_prepare_tcp_pkt(af, src_port, dst_port, ACK, NULL, 0U);
365 }
366
prepare_data_packet(sa_family_t af,uint16_t src_port,uint16_t dst_port,const uint8_t * data,size_t len)367 static struct net_pkt *prepare_data_packet(sa_family_t af, uint16_t src_port,
368 uint16_t dst_port,
369 const uint8_t *data,
370 size_t len)
371 {
372 return tester_prepare_tcp_pkt(af, src_port, dst_port, PSH | ACK, data,
373 len);
374 }
375
prepare_fin_ack_packet(sa_family_t af,uint16_t src_port,uint16_t dst_port)376 static struct net_pkt *prepare_fin_ack_packet(sa_family_t af, uint16_t src_port,
377 uint16_t dst_port)
378 {
379 return tester_prepare_tcp_pkt(af, src_port, dst_port, FIN | ACK,
380 NULL, 0U);
381 }
382
prepare_fin_packet(sa_family_t af,uint16_t src_port,uint16_t dst_port)383 static struct net_pkt *prepare_fin_packet(sa_family_t af, uint16_t src_port,
384 uint16_t dst_port)
385 {
386 return tester_prepare_tcp_pkt(af, src_port, dst_port, FIN, NULL, 0U);
387 }
388
prepare_rst_packet(sa_family_t af,uint16_t src_port,uint16_t dst_port)389 static struct net_pkt *prepare_rst_packet(sa_family_t af, uint16_t src_port,
390 uint16_t dst_port)
391 {
392 return tester_prepare_tcp_pkt(af, src_port, dst_port, RST, NULL, 0U);
393 }
394
is_icmp_pkt(struct net_pkt * pkt)395 static bool is_icmp_pkt(struct net_pkt *pkt)
396 {
397 if (net_pkt_family(pkt) == AF_INET) {
398 return NET_IPV4_HDR(pkt)->proto == IPPROTO_ICMP;
399 } else {
400 return NET_IPV6_HDR(pkt)->nexthdr == IPPROTO_ICMPV6;
401 }
402 }
403
read_tcp_header(struct net_pkt * pkt,struct tcphdr * th)404 static int read_tcp_header(struct net_pkt *pkt, struct tcphdr *th)
405 {
406 int ret;
407
408 net_pkt_cursor_init(pkt);
409 net_pkt_set_overwrite(pkt, true);
410
411 ret = net_pkt_skip(pkt, net_pkt_ip_hdr_len(pkt) +
412 net_pkt_ip_opts_len(pkt));
413 if (ret < 0) {
414 goto fail;
415 }
416
417 ret = net_pkt_read(pkt, th, sizeof(struct tcphdr));
418 if (ret < 0) {
419 goto fail;
420 }
421
422 net_pkt_cursor_init(pkt);
423
424 return 0;
425 fail:
426 return -EINVAL;
427 }
428
tester_send(const struct device * dev,struct net_pkt * pkt)429 static int tester_send(const struct device *dev, struct net_pkt *pkt)
430 {
431 struct tcphdr th;
432 int ret;
433
434 /* Ignore ICMP explicitly */
435 if (is_icmp_pkt(pkt)) {
436 return 0;
437 }
438
439 ret = read_tcp_header(pkt, &th);
440 if (ret < 0) {
441 goto fail;
442 }
443
444 switch (test_case_no) {
445 case TEST_CLIENT_IPV4:
446 case TEST_CLIENT_IPV6:
447 handle_client_test(net_pkt_family(pkt), &th);
448 break;
449 case TEST_SERVER_IPV4:
450 case TEST_SERVER_WITH_OPTIONS_IPV4:
451 case TEST_SERVER_IPV6:
452 handle_server_test(net_pkt_family(pkt), &th);
453 break;
454 case TEST_CLIENT_SYN_RESEND:
455 handle_syn_resend();
456 break;
457 case TEST_CLIENT_FIN_WAIT_2_IPV4:
458 handle_client_fin_wait_2_test(net_pkt_family(pkt), &th);
459 break;
460 case TEST_CLIENT_CLOSING_IPV6:
461 handle_client_closing_test(net_pkt_family(pkt), &th);
462 break;
463 case TEST_SERVER_RECV_OUT_OF_ORDER_DATA:
464 handle_server_recv_out_of_order(pkt);
465 break;
466 case TEST_CLIENT_FIN_WAIT_1_RETRANSMIT_IPV4:
467 handle_data_fin1_test(net_pkt_family(pkt), &th);
468 break;
469 case TEST_CLIENT_DATA_DURING_FIN_1_IPV4:
470 handle_data_during_fin1_test(net_pkt_family(pkt), &th);
471 break;
472 case TEST_CLIENT_SYN_RST_ACK:
473 handle_syn_rst_ack(net_pkt_family(pkt), &th);
474 break;
475 case TEST_SERVER_RST_ON_CLOSED_PORT:
476 handle_server_rst_on_closed_port(net_pkt_family(pkt), &th);
477 break;
478 case TEST_SERVER_RST_ON_LISTENING_PORT_NO_ACTIVE_CONNECTION:
479 handle_server_rst_on_listening_port(net_pkt_family(pkt), &th);
480 break;
481 case TEST_CLIENT_RST_ON_UNEXPECTED_ACK_ON_SYN:
482 handle_syn_invalid_ack(net_pkt_family(pkt), &th);
483 break;
484 case TEST_CLIENT_CLOSING_FAILURE_IPV6:
485 handle_client_closing_failure_test(net_pkt_family(pkt), &th);
486 break;
487 case TEST_CLIENT_FIN_WAIT_2_IPV4_FAILURE:
488 handle_client_fin_wait_2_failure_test(net_pkt_family(pkt), &th);
489 break;
490 case TEST_CLIENT_FIN_ACK_WITH_DATA:
491 handle_client_fin_ack_with_data_test(net_pkt_family(pkt), &th);
492 break;
493
494 default:
495 zassert_true(false, "Undefined test case");
496 }
497
498 return 0;
499 fail:
500 zassert_true(false, "%s failed", __func__);
501
502 net_pkt_unref(pkt);
503
504 return -EINVAL;
505 }
506
507 /* Initial setup for the tests */
presetup(void)508 static void *presetup(void)
509 {
510 struct net_if_addr *ifaddr;
511
512 net_iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY));
513 if (!net_iface) {
514 zassert_true(false, "Interface not available");
515 }
516
517 ifaddr = net_if_ipv4_addr_add(net_iface, &my_addr, NET_ADDR_MANUAL, 0);
518 if (!ifaddr) {
519 zassert_true(false, "Failed to add IPv4 address");
520 }
521
522 ifaddr = net_if_ipv6_addr_add(net_iface, &my_addr_v6, NET_ADDR_MANUAL, 0);
523 if (!ifaddr) {
524 zassert_true(false, "Failed to add IPv6 address");
525 }
526
527 k_work_init_delayable(&test_server, test_server_timeout);
528 return NULL;
529 }
530
handle_client_test(sa_family_t af,struct tcphdr * th)531 static void handle_client_test(sa_family_t af, struct tcphdr *th)
532 {
533 struct net_pkt *reply;
534 int ret;
535
536 switch (t_state) {
537 case T_SYN:
538 test_verify_flags(th, SYN);
539 seq = 0U;
540 ack = ntohl(th->th_seq) + 1U;
541 reply = prepare_syn_ack_packet(af, htons(MY_PORT),
542 th->th_sport);
543 t_state = T_SYN_ACK;
544 break;
545 case T_SYN_ACK:
546 test_verify_flags(th, ACK);
547 /* connection is success */
548 t_state = T_DATA;
549 test_sem_give();
550 return;
551 case T_DATA:
552 test_verify_flags(th, PSH | ACK);
553 seq++;
554 ack = ack + 1U;
555 reply = prepare_ack_packet(af, htons(MY_PORT), th->th_sport);
556 t_state = T_FIN;
557 test_sem_give();
558 break;
559 case T_FIN:
560 test_verify_flags(th, FIN | ACK);
561 ack = ack + 1U;
562 t_state = T_FIN_ACK;
563 reply = prepare_fin_ack_packet(af, htons(MY_PORT),
564 th->th_sport);
565 break;
566 case T_FIN_ACK:
567 test_verify_flags(th, ACK);
568 test_sem_give();
569 return;
570 default:
571 zassert_true(false, "%s unexpected state", __func__);
572 return;
573 }
574
575 ret = net_recv_data(net_iface, reply);
576 if (ret < 0) {
577 goto fail;
578 }
579
580 return;
581 fail:
582 zassert_true(false, "%s failed", __func__);
583 }
584
585 /* Test case scenario IPv4
586 * send SYN,
587 * expect SYN ACK,
588 * send ACK,
589 * send Data,
590 * expect ACK,
591 * send FIN,
592 * expect FIN ACK,
593 * send ACK.
594 * any failures cause test case to fail.
595 */
ZTEST(net_tcp,test_client_ipv4)596 ZTEST(net_tcp, test_client_ipv4)
597 {
598 struct net_context *ctx;
599 uint8_t data = 0x41; /* "A" */
600 int ret;
601
602 t_state = T_SYN;
603 test_case_no = TEST_CLIENT_IPV4;
604 seq = ack = 0;
605
606 ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ctx);
607 if (ret < 0) {
608 zassert_true(false, "Failed to get net_context");
609 }
610
611 net_context_ref(ctx);
612
613 ret = net_context_connect(ctx, (struct sockaddr *)&peer_addr_s,
614 sizeof(struct sockaddr_in),
615 NULL,
616 K_MSEC(100), NULL);
617 if (ret < 0) {
618 zassert_true(false, "Failed to connect to peer");
619 }
620
621 /* Peer will release the semaphore after it receives
622 * proper ACK to SYN | ACK
623 */
624 test_sem_take(K_MSEC(100), __LINE__);
625
626 ret = net_context_send(ctx, &data, 1, NULL, K_NO_WAIT, NULL);
627 if (ret < 0) {
628 zassert_true(false, "Failed to send data to peer");
629 }
630
631 /* Peer will release the semaphore after it sends ACK for data */
632 test_sem_take(K_MSEC(100), __LINE__);
633
634 net_context_put(ctx);
635
636 /* Peer will release the semaphore after it receives
637 * proper ACK to FIN | ACK
638 */
639 test_sem_take(K_MSEC(100), __LINE__);
640
641 /* Connection is in TIME_WAIT state, context will be released
642 * after K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY), so wait for it.
643 */
644 k_sleep(K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY));
645 }
646
647 /* Test case scenario IPv6
648 * send SYN,
649 * expect SYN ACK,
650 * send ACK,
651 * send Data,
652 * expect ACK,
653 * send FIN,
654 * expect FIN ACK,
655 * send ACK.
656 * any failures cause test case to fail.
657 */
ZTEST(net_tcp,test_client_ipv6)658 ZTEST(net_tcp, test_client_ipv6)
659 {
660 struct net_context *ctx;
661 uint8_t data = 0x41; /* "A" */
662 int ret;
663
664 t_state = T_SYN;
665 test_case_no = TEST_CLIENT_IPV6;
666 seq = ack = 0;
667
668 ret = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP, &ctx);
669 if (ret < 0) {
670 zassert_true(false, "Failed to get net_context");
671 }
672
673 net_context_ref(ctx);
674
675 ret = net_context_connect(ctx, (struct sockaddr *)&peer_addr_v6_s,
676 sizeof(struct sockaddr_in6),
677 NULL,
678 K_MSEC(100), NULL);
679 if (ret < 0) {
680 zassert_true(false, "Failed to connect to peer");
681 }
682
683 /* Peer will release the semaphore after it receives
684 * proper ACK to SYN | ACK
685 */
686 test_sem_take(K_MSEC(100), __LINE__);
687
688 ret = net_context_send(ctx, &data, 1, NULL, K_NO_WAIT, NULL);
689 if (ret < 0) {
690 zassert_true(false, "Failed to send data to peer");
691 }
692
693 /* Peer will release the semaphore after it sends ACK for data */
694 test_sem_take(K_MSEC(100), __LINE__);
695
696 net_context_put(ctx);
697
698 /* Peer will release the semaphore after it receives
699 * proper ACK to FIN | ACK
700 */
701 test_sem_take(K_MSEC(100), __LINE__);
702
703 /* Connection is in TIME_WAIT state, context will be released
704 * after K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY), so wait for it.
705 */
706 k_sleep(K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY));
707 }
708
handle_server_test(sa_family_t af,struct tcphdr * th)709 static void handle_server_test(sa_family_t af, struct tcphdr *th)
710 {
711 struct net_pkt *reply;
712 int ret;
713
714 switch (t_state) {
715 case T_SYN:
716 reply = prepare_syn_packet(af, htons(MY_PORT),
717 htons(PEER_PORT));
718 t_state = T_SYN_ACK;
719 break;
720 case T_SYN_ACK:
721 test_verify_flags(th, SYN | ACK);
722 seq++;
723 ack = ntohl(th->th_seq) + 1U;
724 reply = prepare_ack_packet(af, htons(MY_PORT),
725 htons(PEER_PORT));
726 t_state = T_DATA;
727 break;
728 case T_DATA:
729 reply = prepare_data_packet(af, htons(MY_PORT),
730 htons(PEER_PORT), "A", 1U);
731 t_state = T_DATA_ACK;
732 break;
733 case T_DATA_ACK:
734 test_verify_flags(th, ACK);
735 seq++;
736 reply = prepare_fin_ack_packet(af, htons(MY_PORT),
737 htons(PEER_PORT));
738 t_state = T_FIN;
739 break;
740 case T_FIN:
741 test_verify_flags(th, FIN | ACK);
742 seq++;
743 ack++;
744 reply = prepare_ack_packet(af, htons(MY_PORT),
745 htons(PEER_PORT));
746 t_state = T_FIN_ACK;
747 break;
748 case T_FIN_ACK:
749 return;
750 default:
751 zassert_true(false, "%s: unexpected state", __func__);
752 return;
753 }
754
755 ret = net_recv_data(net_iface, reply);
756 if (ret < 0) {
757 goto fail;
758 }
759
760 return;
761 fail:
762 zassert_true(false, "%s failed", __func__);
763 }
764
test_server_timeout(struct k_work * work)765 static void test_server_timeout(struct k_work *work)
766 {
767 if (test_case_no == TEST_SERVER_IPV4 ||
768 test_case_no == TEST_SERVER_WITH_OPTIONS_IPV4 ||
769 test_case_no == TEST_SERVER_RST_ON_CLOSED_PORT ||
770 test_case_no == TEST_SERVER_RST_ON_LISTENING_PORT_NO_ACTIVE_CONNECTION) {
771 handle_server_test(AF_INET, NULL);
772 } else if (test_case_no == TEST_SERVER_IPV6) {
773 handle_server_test(AF_INET6, NULL);
774 } else {
775 zassert_true(false, "Invalid test case");
776 }
777 }
778
test_tcp_recv_cb(struct net_context * context,struct net_pkt * pkt,union net_ip_header * ip_hdr,union net_proto_header * proto_hdr,int status,void * user_data)779 static void test_tcp_recv_cb(struct net_context *context,
780 struct net_pkt *pkt,
781 union net_ip_header *ip_hdr,
782 union net_proto_header *proto_hdr,
783 int status,
784 void *user_data)
785 {
786 if (status && status != -ECONNRESET) {
787 zassert_true(false, "failed to recv the data");
788 }
789
790 if (pkt) {
791 net_pkt_unref(pkt);
792 }
793 }
794
795 static struct net_context *accepted_ctx;
796
test_tcp_accept_cb(struct net_context * ctx,struct sockaddr * addr,socklen_t addrlen,int status,void * user_data)797 static void test_tcp_accept_cb(struct net_context *ctx,
798 struct sockaddr *addr,
799 socklen_t addrlen,
800 int status,
801 void *user_data)
802 {
803 if (status) {
804 zassert_true(false, "failed to accept the conn");
805 }
806
807 /* set callback on newly created context */
808 ctx->recv_cb = test_tcp_recv_cb;
809 accepted_ctx = ctx;
810
811 /* Ref the context on the app behalf. */
812 net_context_ref(ctx);
813
814 test_sem_give();
815 }
816
817 /* Test case scenario IPv4
818 * Expect SYN
819 * send SYN ACK,
820 * expect ACK,
821 * expect DATA,
822 * send ACK,
823 * expect FIN,
824 * send FIN ACK,
825 * expect ACK.
826 * any failures cause test case to fail.
827 */
ZTEST(net_tcp,test_server_ipv4)828 ZTEST(net_tcp, test_server_ipv4)
829 {
830 struct net_context *ctx;
831 int ret;
832
833 t_state = T_SYN;
834 test_case_no = TEST_SERVER_IPV4;
835 seq = ack = 0;
836
837 ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ctx);
838 if (ret < 0) {
839 zassert_true(false, "Failed to get net_context");
840 }
841
842 net_context_ref(ctx);
843
844 ret = net_context_bind(ctx, (struct sockaddr *)&my_addr_s,
845 sizeof(struct sockaddr_in));
846 if (ret < 0) {
847 zassert_true(false, "Failed to bind net_context");
848 }
849
850 ret = net_context_listen(ctx, 1);
851 if (ret < 0) {
852 zassert_true(false, "Failed to listen on net_context");
853 }
854
855 /* Trigger the peer to send SYN */
856 k_work_reschedule(&test_server, K_NO_WAIT);
857
858 ret = net_context_accept(ctx, test_tcp_accept_cb, K_FOREVER, NULL);
859 if (ret < 0) {
860 zassert_true(false, "Failed to set accept on net_context");
861 }
862
863 /* test_tcp_accept_cb will release the semaphore after successful
864 * connection.
865 */
866 test_sem_take(K_MSEC(100), __LINE__);
867
868 /* Trigger the peer to send DATA */
869 k_work_reschedule(&test_server, K_NO_WAIT);
870
871 ret = net_context_recv(accepted_ctx, test_tcp_recv_cb, K_MSEC(200), NULL);
872 if (ret < 0) {
873 zassert_true(false, "Failed to recv data from peer");
874 }
875
876 /* Trigger the peer to send FIN after timeout */
877 k_work_reschedule(&test_server, K_NO_WAIT);
878
879 /* Let the receiving thread run */
880 k_msleep(50);
881
882 net_context_put(ctx);
883 net_context_put(accepted_ctx);
884 }
885
886 /* Test case scenario IPv4
887 * Expect SYN with TCP options
888 * send SYN ACK,
889 * expect ACK,
890 * expect DATA,
891 * send ACK,
892 * expect FIN,
893 * send FIN ACK,
894 * expect ACK.
895 * any failures cause test case to fail.
896 */
ZTEST(net_tcp,test_server_with_options_ipv4)897 ZTEST(net_tcp, test_server_with_options_ipv4)
898 {
899 struct net_context *ctx;
900 int ret;
901
902 t_state = T_SYN;
903 test_case_no = TEST_SERVER_WITH_OPTIONS_IPV4;
904 seq = ack = 0;
905
906 ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ctx);
907 if (ret < 0) {
908 zassert_true(false, "Failed to get net_context");
909 }
910
911 net_context_ref(ctx);
912
913 ret = net_context_bind(ctx, (struct sockaddr *)&my_addr_s,
914 sizeof(struct sockaddr_in));
915 if (ret < 0) {
916 zassert_true(false, "Failed to bind net_context");
917 }
918
919 ret = net_context_listen(ctx, 1);
920 if (ret < 0) {
921 zassert_true(false, "Failed to listen on net_context");
922 }
923
924 /* Trigger the peer to send SYN */
925 k_work_reschedule(&test_server, K_NO_WAIT);
926
927 ret = net_context_accept(ctx, test_tcp_accept_cb, K_FOREVER, NULL);
928 if (ret < 0) {
929 zassert_true(false, "Failed to set accept on net_context");
930 }
931
932 /* test_tcp_accept_cb will release the semaphore after successful
933 * connection.
934 */
935 test_sem_take(K_MSEC(100), __LINE__);
936
937 /* Trigger the peer to send DATA */
938 k_work_reschedule(&test_server, K_NO_WAIT);
939
940 ret = net_context_recv(accepted_ctx, test_tcp_recv_cb, K_MSEC(200), NULL);
941 if (ret < 0) {
942 zassert_true(false, "Failed to recv data from peer");
943 }
944
945 /* Trigger the peer to send FIN after timeout */
946 k_work_reschedule(&test_server, K_NO_WAIT);
947
948 /* Let the receiving thread run */
949 k_msleep(50);
950
951 net_context_put(ctx);
952 net_context_put(accepted_ctx);
953 }
954
955 /* Test case scenario IPv6
956 * Expect SYN
957 * send SYN ACK,
958 * expect ACK,
959 * expect DATA,
960 * send ACK,
961 * expect FIN,
962 * send FIN ACK,
963 * expect ACK.
964 * any failures cause test case to fail.
965 */
ZTEST(net_tcp,test_server_ipv6)966 ZTEST(net_tcp, test_server_ipv6)
967 {
968 struct net_context *ctx;
969 int ret;
970
971 t_state = T_SYN;
972 test_case_no = TEST_SERVER_IPV6;
973 seq = ack = 0;
974
975 ret = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP, &ctx);
976 if (ret < 0) {
977 zassert_true(false, "Failed to get net_context");
978 }
979
980 net_context_ref(ctx);
981
982 ret = net_context_bind(ctx, (struct sockaddr *)&my_addr_v6_s,
983 sizeof(struct sockaddr_in6));
984 if (ret < 0) {
985 zassert_true(false, "Failed to bind net_context");
986 }
987
988 ret = net_context_listen(ctx, 1);
989 if (ret < 0) {
990 zassert_true(false, "Failed to listen on net_context");
991 }
992
993 /* Trigger the peer to send SYN */
994 k_work_reschedule(&test_server, K_NO_WAIT);
995
996 ret = net_context_accept(ctx, test_tcp_accept_cb, K_FOREVER, NULL);
997 if (ret < 0) {
998 zassert_true(false, "Failed to set accept on net_context");
999 }
1000
1001 /* test_tcp_accept_cb will release the semaphore after successful
1002 * connection.
1003 */
1004 test_sem_take(K_MSEC(100), __LINE__);
1005
1006 /* Trigger the peer to send DATA */
1007 k_work_reschedule(&test_server, K_NO_WAIT);
1008
1009 ret = net_context_recv(accepted_ctx, test_tcp_recv_cb, K_MSEC(200), NULL);
1010 if (ret < 0) {
1011 zassert_true(false, "Failed to recv data from peer");
1012 }
1013
1014 /* Trigger the peer to send FIN after timeout */
1015 k_work_reschedule(&test_server, K_NO_WAIT);
1016
1017 /* Let the receiving thread run */
1018 k_msleep(50);
1019
1020 net_context_put(ctx);
1021 net_context_put(accepted_ctx);
1022 }
1023
handle_syn_resend(void)1024 static void handle_syn_resend(void)
1025 {
1026 static uint8_t syn_times;
1027
1028 syn_times++;
1029
1030 if (syn_times == 2) {
1031 test_sem_give();
1032 }
1033 }
1034
1035 /* Test case scenario IPv4
1036 * send SYN,
1037 * peer doesn't reply SYN ACK,
1038 * send SYN again,
1039 * any failures cause test case to fail.
1040 */
ZTEST(net_tcp,test_client_syn_resend)1041 ZTEST(net_tcp, test_client_syn_resend)
1042 {
1043 struct net_context *ctx;
1044 int ret;
1045
1046 t_state = T_SYN;
1047 test_case_no = TEST_CLIENT_SYN_RESEND;
1048 seq = ack = 0;
1049
1050 ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ctx);
1051 if (ret < 0) {
1052 zassert_true(false, "Failed to get net_context");
1053 }
1054
1055 net_context_ref(ctx);
1056
1057 ret = net_context_connect(ctx, (struct sockaddr *)&peer_addr_s,
1058 sizeof(struct sockaddr_in),
1059 NULL,
1060 K_MSEC(300 + 50), NULL);
1061
1062 zassert_true(ret < 0, "Connect on no response from peer");
1063
1064 /* test handler will release the sem once it receives SYN again */
1065 test_sem_take(K_MSEC(500), __LINE__);
1066
1067 net_context_put(ctx);
1068 }
1069
handle_syn_rst_ack(sa_family_t af,struct tcphdr * th)1070 static void handle_syn_rst_ack(sa_family_t af, struct tcphdr *th)
1071 {
1072 struct net_pkt *reply;
1073 int ret;
1074
1075 switch (t_state) {
1076 case T_SYN:
1077 test_verify_flags(th, SYN);
1078 seq = 0U;
1079 ack = ntohl(th->th_seq) + 1U;
1080 reply = prepare_rst_ack_packet(af, htons(MY_PORT),
1081 th->th_sport);
1082 t_state = T_CLOSING;
1083 break;
1084 default:
1085 return;
1086 }
1087
1088 ret = net_recv_data(net_iface, reply);
1089 if (ret < 0) {
1090 goto fail;
1091 }
1092
1093 return;
1094 fail:
1095 zassert_true(false, "%s failed", __func__);
1096 }
1097
1098 /* Test case scenario IPv4
1099 * send SYN,
1100 * peer replies RST+ACK,
1101 * net_context_connect should report an error
1102 */
ZTEST(net_tcp,test_client_syn_rst_ack)1103 ZTEST(net_tcp, test_client_syn_rst_ack)
1104 {
1105 struct net_context *ctx;
1106 int ret;
1107
1108 t_state = T_SYN;
1109 test_case_no = TEST_CLIENT_SYN_RST_ACK;
1110 seq = ack = 0;
1111
1112 ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ctx);
1113 if (ret < 0) {
1114 zassert_true(false, "Failed to get net_context");
1115 }
1116
1117 net_context_ref(ctx);
1118
1119 ret = net_context_connect(ctx, (struct sockaddr *)&peer_addr_s,
1120 sizeof(struct sockaddr_in),
1121 NULL,
1122 K_MSEC(1000), NULL);
1123
1124 zassert_true(ret < 0, "Connect successful on RST+ACK");
1125 zassert_not_equal(net_context_get_state(ctx), NET_CONTEXT_CONNECTED,
1126 "Context should not be connected");
1127
1128 net_context_put(ctx);
1129 }
1130
1131
handle_client_fin_wait_2_test(sa_family_t af,struct tcphdr * th)1132 static void handle_client_fin_wait_2_test(sa_family_t af, struct tcphdr *th)
1133 {
1134 struct net_pkt *reply;
1135 int ret;
1136
1137 send_next:
1138 switch (t_state) {
1139 case T_SYN:
1140 test_verify_flags(th, SYN);
1141 seq = 0U;
1142 ack = ntohl(th->th_seq) + 1U;
1143 reply = prepare_syn_ack_packet(af, htons(MY_PORT),
1144 th->th_sport);
1145 t_state = T_SYN_ACK;
1146 break;
1147 case T_SYN_ACK:
1148 test_verify_flags(th, ACK);
1149 /* connection is success */
1150 t_state = T_DATA;
1151 test_sem_give();
1152 return;
1153 case T_DATA:
1154 test_verify_flags(th, PSH | ACK);
1155 seq++;
1156 ack = ack + 1U;
1157 reply = prepare_ack_packet(af, htons(MY_PORT), th->th_sport);
1158 t_state = T_FIN;
1159 test_sem_give();
1160 break;
1161 case T_FIN:
1162 test_verify_flags(th, FIN | ACK);
1163 ack = ack + 1U;
1164 t_state = T_FIN_2;
1165 reply = prepare_ack_packet(af, htons(MY_PORT), th->th_sport);
1166 break;
1167 case T_FIN_2:
1168 t_state = T_FIN_ACK;
1169 reply = prepare_fin_packet(af, htons(MY_PORT), th->th_sport);
1170 break;
1171 case T_FIN_ACK:
1172 test_verify_flags(th, ACK);
1173 test_sem_give();
1174 return;
1175 default:
1176 zassert_true(false, "%s unexpected state", __func__);
1177 return;
1178 }
1179
1180 ret = net_recv_data(net_iface, reply);
1181 if (ret < 0) {
1182 goto fail;
1183 }
1184
1185 if (t_state == T_FIN_2) {
1186 goto send_next;
1187 }
1188
1189 return;
1190 fail:
1191 zassert_true(false, "%s failed", __func__);
1192 }
1193
1194 /* Test case scenario IPv4
1195 * send SYN,
1196 * expect SYN ACK,
1197 * send ACK,
1198 * send Data,
1199 * expect ACK,
1200 * send FIN,
1201 * expect ACK,
1202 * expect FIN,
1203 * send ACK,
1204 * any failures cause test case to fail.
1205 */
ZTEST(net_tcp,test_client_fin_wait_2_ipv4)1206 ZTEST(net_tcp, test_client_fin_wait_2_ipv4)
1207 {
1208 struct net_context *ctx;
1209 uint8_t data = 0x41; /* "A" */
1210 int ret;
1211
1212 t_state = T_SYN;
1213 test_case_no = TEST_CLIENT_FIN_WAIT_2_IPV4;
1214 seq = ack = 0;
1215
1216 ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ctx);
1217 if (ret < 0) {
1218 zassert_true(false, "Failed to get net_context");
1219 }
1220
1221 net_context_ref(ctx);
1222
1223 ret = net_context_connect(ctx, (struct sockaddr *)&peer_addr_s,
1224 sizeof(struct sockaddr_in),
1225 NULL,
1226 K_MSEC(100), NULL);
1227 if (ret < 0) {
1228 zassert_true(false, "Failed to connect to peer");
1229 }
1230
1231 /* Peer will release the semaphore after it receives
1232 * proper ACK to SYN | ACK
1233 */
1234 test_sem_take(K_MSEC(100), __LINE__);
1235
1236 ret = net_context_send(ctx, &data, 1, NULL, K_NO_WAIT, NULL);
1237 if (ret < 0) {
1238 zassert_true(false, "Failed to send data to peer");
1239 }
1240
1241 /* Peer will release the semaphore after it sends ACK for data */
1242 test_sem_take(K_MSEC(100), __LINE__);
1243
1244 net_context_put(ctx);
1245
1246 /* Peer will release the semaphore after it receives
1247 * proper ACK to FIN | ACK
1248 */
1249 test_sem_take(K_MSEC(300), __LINE__);
1250
1251 /* Connection is in TIME_WAIT state, context will be released
1252 * after K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY), so wait for it.
1253 */
1254 k_sleep(K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY));
1255 }
1256
handle_client_fin_wait_2_failure_test(sa_family_t af,struct tcphdr * th)1257 static void handle_client_fin_wait_2_failure_test(sa_family_t af, struct tcphdr *th)
1258 {
1259 struct net_pkt *reply;
1260 int ret;
1261
1262 send_next:
1263 switch (t_state) {
1264 case T_SYN:
1265 test_verify_flags(th, SYN);
1266 seq = 0U;
1267 ack = ntohl(th->th_seq) + 1U;
1268 reply = prepare_syn_ack_packet(af, htons(MY_PORT),
1269 th->th_sport);
1270 t_state = T_SYN_ACK;
1271 break;
1272 case T_SYN_ACK:
1273 test_verify_flags(th, ACK);
1274 /* connection is success */
1275 t_state = T_DATA;
1276 test_sem_give();
1277 return;
1278 case T_DATA:
1279 test_verify_flags(th, PSH | ACK);
1280 seq++;
1281 ack = ack + 1U;
1282 reply = prepare_ack_packet(af, htons(MY_PORT), th->th_sport);
1283 t_state = T_FIN;
1284 test_sem_give();
1285 break;
1286 case T_FIN:
1287 test_verify_flags(th, FIN | ACK);
1288 ack = ack + 1U;
1289 t_state = T_FIN_2;
1290 reply = prepare_ack_packet(af, htons(MY_PORT), th->th_sport);
1291 break;
1292 case T_FIN_2:
1293 t_state = T_FIN_ACK;
1294 /* We do not send last FIN that would move the state to TIME_WAIT.
1295 * Make sure that the stack can recover from this by closing the
1296 * connection.
1297 */
1298 reply = NULL;
1299 break;
1300 case T_FIN_ACK:
1301 reply = NULL;
1302 return;
1303 default:
1304 zassert_true(false, "%s unexpected state", __func__);
1305 return;
1306 }
1307
1308 if (reply != NULL) {
1309 ret = net_recv_data(net_iface, reply);
1310 if (ret < 0) {
1311 goto fail;
1312 }
1313 }
1314
1315 if (t_state == T_FIN_2) {
1316 goto send_next;
1317 }
1318
1319 return;
1320 fail:
1321 zassert_true(false, "%s failed", __func__);
1322 }
1323
1324 static bool closed;
1325 static K_SEM_DEFINE(wait_data, 0, 1);
1326
connection_closed(struct tcp * conn,void * user_data)1327 static void connection_closed(struct tcp *conn, void *user_data)
1328 {
1329 struct k_sem *my_sem = user_data;
1330
1331 k_sem_give(my_sem);
1332 closed = true;
1333 }
1334
1335 /* Test case scenario IPv4
1336 * send SYN,
1337 * expect SYN ACK,
1338 * send ACK,
1339 * send Data,
1340 * expect ACK,
1341 * send FIN,
1342 * expect ACK,
1343 * expect FIN,
1344 * send ACK,
1345 * any failures cause test case to fail.
1346 */
ZTEST(net_tcp,test_client_fin_wait_2_ipv4_failure)1347 ZTEST(net_tcp, test_client_fin_wait_2_ipv4_failure)
1348 {
1349 struct net_context *ctx;
1350 uint8_t data = 0x41; /* "A" */
1351 int ret;
1352
1353 t_state = T_SYN;
1354 test_case_no = TEST_CLIENT_FIN_WAIT_2_IPV4_FAILURE;
1355 seq = ack = 0;
1356 closed = false;
1357
1358 ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ctx);
1359 if (ret < 0) {
1360 zassert_true(false, "Failed to get net_context");
1361 }
1362
1363 net_context_ref(ctx);
1364
1365 ret = net_context_connect(ctx, (struct sockaddr *)&peer_addr_s,
1366 sizeof(struct sockaddr_in),
1367 NULL,
1368 K_MSEC(100), NULL);
1369 if (ret < 0) {
1370 zassert_true(false, "Failed to connect to peer");
1371 }
1372
1373 /* Peer will release the semaphore after it receives
1374 * proper ACK to SYN | ACK
1375 */
1376 test_sem_take(K_MSEC(100), __LINE__);
1377
1378 ret = net_context_send(ctx, &data, 1, NULL, K_NO_WAIT, NULL);
1379 if (ret < 0) {
1380 zassert_true(false, "Failed to send data to peer");
1381 }
1382
1383 /* Peer will release the semaphore after it sends ACK for data */
1384 test_sem_take(K_MSEC(100), __LINE__);
1385
1386 k_sem_reset(&wait_data);
1387 k_sem_take(&wait_data, K_NO_WAIT);
1388
1389 tcp_install_close_cb(ctx, connection_closed, &wait_data);
1390
1391 net_context_put(ctx);
1392
1393 /* The lock is released after we have closed the connection. */
1394 k_sem_take(&wait_data, K_MSEC(10000));
1395 zassert_equal(closed, true, "Connection was not closed!");
1396 }
1397
get_rel_seq(struct tcphdr * th)1398 static uint32_t get_rel_seq(struct tcphdr *th)
1399 {
1400 return ntohl(th->th_seq) - device_initial_seq;
1401 }
1402
handle_data_fin1_test(sa_family_t af,struct tcphdr * th)1403 static void handle_data_fin1_test(sa_family_t af, struct tcphdr *th)
1404 {
1405 struct net_pkt *reply;
1406 int ret;
1407
1408 send_next:
1409 switch (t_state) {
1410 case T_SYN:
1411 test_verify_flags(th, SYN);
1412 device_initial_seq = ntohl(th->th_seq);
1413 seq = 0U;
1414 ack = ntohl(th->th_seq) + 1U;
1415 reply = prepare_syn_ack_packet(af, htons(MY_PORT),
1416 th->th_sport);
1417 seq++;
1418 t_state = T_SYN_ACK;
1419 break;
1420 case T_SYN_ACK:
1421 test_verify_flags(th, ACK);
1422 /* connection is success */
1423 reply = prepare_data_packet(af, htons(MY_PORT),
1424 th->th_sport, "A", 1U);
1425 t_state = T_DATA_ACK;
1426 break;
1427 case T_DATA_ACK:
1428 test_verify_flags(th, ACK);
1429 test_sem_give();
1430 reply = NULL;
1431 t_state = T_FIN;
1432 return;
1433 case T_FIN:
1434 test_verify_flags(th, FIN | ACK);
1435 zassert_true(get_rel_seq(th) == 1,
1436 "%s:%d unexpected sequence number in original FIN, got %d",
1437 __func__, __LINE__, get_rel_seq(th));
1438 zassert_true(ntohl(th->th_ack) == 2,
1439 "%s:%d unexpected acknowlegdement in original FIN, got %d",
1440 __func__, __LINE__, ntohl(th->th_ack));
1441 t_state = T_FIN_1;
1442 /* retransmit the data that we already send*/
1443 reply = prepare_data_packet(af, htons(MY_PORT),
1444 th->th_sport, "A", 1U);
1445 seq++;
1446 break;
1447 case T_FIN_1:
1448 test_verify_flags(th, FIN | ACK);
1449 /* retransmitted FIN should have the same sequence number*/
1450 zassert_true(get_rel_seq(th) == 1,
1451 "%s:%i unexpected sequence number in retransmitted FIN, got %d",
1452 __func__, __LINE__, get_rel_seq(th));
1453 zassert_true(ntohl(th->th_ack) == 2,
1454 "%s:%i unexpected acknowlegdement in retransmitted FIN, got %d",
1455 __func__, __LINE__, ntohl(th->th_ack));
1456 ack = ack + 1U;
1457 t_state = T_FIN_2;
1458 reply = prepare_ack_packet(af, htons(MY_PORT), th->th_sport);
1459 break;
1460 case T_FIN_2:
1461 t_state = T_FIN_ACK;
1462 reply = prepare_fin_packet(af, htons(MY_PORT), th->th_sport);
1463 break;
1464 case T_FIN_ACK:
1465 test_verify_flags(th, ACK);
1466 test_sem_give();
1467 return;
1468 default:
1469 zassert_true(false, "%s unexpected state", __func__);
1470 return;
1471 }
1472
1473 ret = net_recv_data(net_iface, reply);
1474 if (ret < 0) {
1475 goto fail;
1476 }
1477
1478 if (t_state == T_FIN_2) {
1479 goto send_next;
1480 }
1481
1482 return;
1483 fail:
1484 zassert_true(false, "%s failed", __func__);
1485 }
1486
1487 /* Test case scenario IPv4
1488 * expect SYN,
1489 * send SYN ACK,
1490 * expect ACK,
1491 * expect Data,
1492 * send ACK,
1493 * expect FIN,
1494 * resend Data,
1495 * expect FIN,
1496 * send ACK,
1497 * send FIN,
1498 * expect ACK
1499 * any failures cause test case to fail.
1500 */
ZTEST(net_tcp,test_client_fin_wait_1_retransmit_ipv4)1501 ZTEST(net_tcp, test_client_fin_wait_1_retransmit_ipv4)
1502 {
1503 struct net_context *ctx;
1504 int ret;
1505
1506 t_state = T_SYN;
1507 test_case_no = TEST_CLIENT_FIN_WAIT_1_RETRANSMIT_IPV4;
1508 seq = ack = 0;
1509
1510 ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ctx);
1511 if (ret < 0) {
1512 zassert_true(false, "Failed to get net_context");
1513 }
1514
1515 net_context_ref(ctx);
1516
1517 ret = net_context_connect(ctx, (struct sockaddr *)&peer_addr_s,
1518 sizeof(struct sockaddr_in),
1519 NULL,
1520 K_MSEC(100), NULL);
1521 if (ret < 0) {
1522 zassert_true(false, "Failed to connect to peer");
1523 }
1524
1525 /* Do not perform a receive, as I cannot get it to work and it is not required to get the
1526 * test functional, the receive functionality is covered by other tests.
1527 */
1528
1529 /* Peer will release the semaphore after it sends ACK for data */
1530 test_sem_take(K_MSEC(100), __LINE__);
1531
1532 net_context_put(ctx);
1533
1534 /* Peer will release the semaphore after it receives
1535 * proper ACK to FIN | ACK
1536 */
1537 test_sem_take(K_MSEC(300), __LINE__);
1538
1539 /* Connection is in TIME_WAIT state, context will be released
1540 * after K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY), so wait for it.
1541 */
1542 k_sleep(K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY));
1543 }
1544
handle_data_during_fin1_test(sa_family_t af,struct tcphdr * th)1545 static void handle_data_during_fin1_test(sa_family_t af, struct tcphdr *th)
1546 {
1547 struct net_pkt *reply;
1548 int ret;
1549
1550 switch (t_state) {
1551 case T_SYN:
1552 test_verify_flags(th, SYN);
1553 device_initial_seq = ntohl(th->th_seq);
1554 seq = 0U;
1555 ack = ntohl(th->th_seq) + 1U;
1556 reply = prepare_syn_ack_packet(af, htons(MY_PORT),
1557 th->th_sport);
1558 seq++;
1559 t_state = T_SYN_ACK;
1560 break;
1561 case T_SYN_ACK:
1562 test_verify_flags(th, ACK);
1563 test_sem_give();
1564 t_state = T_FIN;
1565 return;
1566 case T_FIN:
1567 test_verify_flags(th, FIN | ACK);
1568 zassert_true(get_rel_seq(th) == 1,
1569 "%s:%d unexpected sequence number in original FIN, got %d",
1570 __func__, __LINE__, get_rel_seq(th));
1571 zassert_true(ntohl(th->th_ack) == 1,
1572 "%s:%d unexpected acknowlegdement in original FIN, got %d",
1573 __func__, __LINE__, ntohl(th->th_ack));
1574
1575 ack = ack + 1U;
1576
1577 /* retransmit the data that we already send / missed */
1578 reply = prepare_data_packet(af, htons(MY_PORT),
1579 th->th_sport, "A", 1U);
1580
1581 t_state = T_FIN_1;
1582 break;
1583 case T_FIN_1:
1584 test_verify_flags(th, RST);
1585 zassert_true(get_rel_seq(th) == 2,
1586 "%s:%d unexpected sequence number in original FIN, got %d",
1587 __func__, __LINE__, get_rel_seq(th));
1588 test_sem_give();
1589 return;
1590 default:
1591 zassert_true(false, "%s unexpected state", __func__);
1592 return;
1593 }
1594
1595 ret = net_recv_data(net_iface, reply);
1596 if (ret < 0) {
1597 goto fail;
1598 }
1599
1600 return;
1601 fail:
1602 zassert_true(false, "%s failed", __func__);
1603 }
1604
1605 /* Test case scenario IPv4
1606 * expect SYN,
1607 * send SYN ACK,
1608 * expect ACK,
1609 * expect Data,
1610 * send ACK,
1611 * expect FIN,
1612 * resend Data,
1613 * expect FIN,
1614 * send ACK,
1615 * send FIN,
1616 * expect ACK
1617 * any failures cause test case to fail.
1618 */
ZTEST(net_tcp,test_client_data_during_fin_1_ipv4)1619 ZTEST(net_tcp, test_client_data_during_fin_1_ipv4)
1620 {
1621 struct net_context *ctx;
1622 int ret;
1623
1624 t_state = T_SYN;
1625 test_case_no = TEST_CLIENT_DATA_DURING_FIN_1_IPV4;
1626 seq = ack = 0;
1627
1628 ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ctx);
1629 if (ret < 0) {
1630 zassert_true(false, "Failed to get net_context");
1631 }
1632
1633 net_context_ref(ctx);
1634
1635 ret = net_context_connect(ctx, (struct sockaddr *)&peer_addr_s,
1636 sizeof(struct sockaddr_in),
1637 NULL,
1638 K_MSEC(100), NULL);
1639 if (ret < 0) {
1640 zassert_true(false, "Failed to connect to peer");
1641 }
1642
1643 /* Peer will release the semaphore after it sends ACK for data */
1644 test_sem_take(K_MSEC(100), __LINE__);
1645
1646 net_context_put(ctx);
1647
1648 /* Peer will release the semaphore after it receives
1649 * proper ACK to FIN | ACK
1650 */
1651 test_sem_take(K_MSEC(300), __LINE__);
1652
1653 /* Connection is in TIME_WAIT state, context will be released
1654 * after K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY), so wait for it.
1655 */
1656 k_sleep(K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY));
1657 }
1658
1659
handle_client_closing_test(sa_family_t af,struct tcphdr * th)1660 static void handle_client_closing_test(sa_family_t af, struct tcphdr *th)
1661 {
1662 struct net_pkt *reply;
1663 int ret;
1664
1665 switch (t_state) {
1666 case T_SYN:
1667 test_verify_flags(th, SYN);
1668 seq = 0U;
1669 ack = ntohl(th->th_seq) + 1U;
1670 reply = prepare_syn_ack_packet(af, htons(MY_PORT),
1671 th->th_sport);
1672 t_state = T_SYN_ACK;
1673 break;
1674 case T_SYN_ACK:
1675 test_verify_flags(th, ACK);
1676 /* connection is success */
1677 t_state = T_DATA;
1678 test_sem_give();
1679 return;
1680 case T_DATA:
1681 test_verify_flags(th, PSH | ACK);
1682 seq++;
1683 ack = ack + 1U;
1684 reply = prepare_ack_packet(af, htons(MY_PORT), th->th_sport);
1685 t_state = T_FIN;
1686 test_sem_give();
1687 break;
1688 case T_FIN:
1689 test_verify_flags(th, FIN | ACK);
1690 ack = ack + 1U;
1691 t_state = T_CLOSING;
1692 reply = prepare_fin_ack_packet(af, htons(MY_PORT), th->th_sport);
1693 break;
1694 case T_CLOSING:
1695 test_verify_flags(th, ACK);
1696 t_state = T_FIN_ACK;
1697 seq++;
1698 reply = prepare_ack_packet(af, htons(MY_PORT), th->th_sport);
1699 break;
1700 default:
1701 zassert_true(false, "%s unexpected state", __func__);
1702 return;
1703 }
1704
1705 ret = net_recv_data(net_iface, reply);
1706 if (ret < 0) {
1707 goto fail;
1708 }
1709
1710 if (t_state == T_FIN_ACK) {
1711 test_sem_give();
1712 }
1713
1714 return;
1715 fail:
1716 zassert_true(false, "%s failed", __func__);
1717 }
1718
1719 /* Test case scenario IPv6
1720 * send SYN,
1721 * expect SYN ACK,
1722 * send ACK,
1723 * send Data,
1724 * expect ACK,
1725 * send FIN,
1726 * expect FIN,
1727 * send ACK,
1728 * expect ACK,
1729 * any failures cause test case to fail.
1730 */
ZTEST(net_tcp,test_client_closing_ipv6)1731 ZTEST(net_tcp, test_client_closing_ipv6)
1732 {
1733 struct net_context *ctx;
1734 uint8_t data = 0x41; /* "A" */
1735 int ret;
1736
1737 t_state = T_SYN;
1738 test_case_no = TEST_CLIENT_CLOSING_IPV6;
1739 seq = ack = 0;
1740
1741 ret = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP, &ctx);
1742 if (ret < 0) {
1743 zassert_true(false, "Failed to get net_context");
1744 }
1745
1746 net_context_ref(ctx);
1747
1748 ret = net_context_connect(ctx, (struct sockaddr *)&peer_addr_v6_s,
1749 sizeof(struct sockaddr_in6),
1750 NULL,
1751 K_MSEC(100), NULL);
1752 if (ret < 0) {
1753 zassert_true(false, "Failed to connect to peer");
1754 }
1755
1756 /* Peer will release the semaphore after it receives
1757 * proper ACK to SYN | ACK
1758 */
1759 test_sem_take(K_MSEC(100), __LINE__);
1760
1761 ret = net_context_send(ctx, &data, 1, NULL, K_NO_WAIT, NULL);
1762 if (ret < 0) {
1763 zassert_true(false, "Failed to send data to peer");
1764 }
1765
1766 /* Peer will release the semaphore after it sends ACK for data */
1767 test_sem_take(K_MSEC(100), __LINE__);
1768
1769 net_context_put(ctx);
1770
1771 /* Peer will release the semaphore after it receives
1772 * proper ACK to FIN | ACK
1773 */
1774 test_sem_take(K_MSEC(300), __LINE__);
1775
1776 /* Connection is in TIME_WAIT state, context will be released
1777 * after K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY), so wait for it.
1778 */
1779 k_sleep(K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY));
1780 }
1781
1782 /* In this test we check that things work properly if we do not receive
1783 * the final ACK that leads to TIME_WAIT state.
1784 */
handle_client_closing_failure_test(sa_family_t af,struct tcphdr * th)1785 static void handle_client_closing_failure_test(sa_family_t af, struct tcphdr *th)
1786 {
1787 struct net_pkt *reply;
1788 int ret;
1789
1790 switch (t_state) {
1791 case T_SYN:
1792 test_verify_flags(th, SYN);
1793 seq = 0U;
1794 ack = ntohl(th->th_seq) + 1U;
1795 reply = prepare_syn_ack_packet(af, htons(MY_PORT),
1796 th->th_sport);
1797 t_state = T_SYN_ACK;
1798 break;
1799 case T_SYN_ACK:
1800 test_verify_flags(th, ACK);
1801 /* connection is success */
1802 t_state = T_DATA;
1803 test_sem_give();
1804 return;
1805 case T_DATA:
1806 test_verify_flags(th, PSH | ACK);
1807 seq++;
1808 ack = ack + 1U;
1809 reply = prepare_ack_packet(af, htons(MY_PORT), th->th_sport);
1810 t_state = T_FIN;
1811 test_sem_give();
1812 break;
1813 case T_FIN:
1814 test_verify_flags(th, FIN | ACK);
1815 t_state = T_FIN_1;
1816 reply = prepare_fin_packet(af, htons(MY_PORT), th->th_sport);
1817 break;
1818 case T_FIN_1:
1819 test_verify_flags(th, FIN | ACK);
1820 t_state = T_CLOSING;
1821 reply = prepare_fin_packet(af, htons(MY_PORT), th->th_sport);
1822 break;
1823 case T_CLOSING:
1824 test_verify_flags(th, FIN | ACK);
1825 /* Simulate the case where we do not receive final ACK */
1826 reply = NULL;
1827 break;
1828 default:
1829 zassert_true(false, "%s unexpected state", __func__);
1830 return;
1831 }
1832
1833 if (reply != NULL) {
1834 ret = net_recv_data(net_iface, reply);
1835 if (ret < 0) {
1836 goto fail;
1837 }
1838 }
1839
1840 return;
1841 fail:
1842 zassert_true(false, "%s failed", __func__);
1843 }
1844
1845 /* Test case scenario IPv6
1846 * send SYN,
1847 * expect SYN ACK,
1848 * send ACK,
1849 * send Data,
1850 * expect ACK,
1851 * send FIN,
1852 * expect FIN,
1853 * send ACK,
1854 * expect ACK but fail to receive it
1855 * any failures cause test case to fail.
1856 */
ZTEST(net_tcp,test_client_closing_failure_ipv6)1857 ZTEST(net_tcp, test_client_closing_failure_ipv6)
1858 {
1859 struct net_context *ctx;
1860 uint8_t data = 0x41; /* "A" */
1861 int ret;
1862
1863 t_state = T_SYN;
1864 test_case_no = TEST_CLIENT_CLOSING_FAILURE_IPV6;
1865 seq = ack = 0;
1866
1867 ret = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP, &ctx);
1868 if (ret < 0) {
1869 zassert_true(false, "Failed to get net_context");
1870 }
1871
1872 net_context_ref(ctx);
1873
1874 ret = net_context_connect(ctx, (struct sockaddr *)&peer_addr_v6_s,
1875 sizeof(struct sockaddr_in6),
1876 NULL,
1877 K_MSEC(100), NULL);
1878 if (ret < 0) {
1879 zassert_true(false, "Failed to connect to peer");
1880 }
1881
1882 /* Peer will release the semaphore after it receives
1883 * proper ACK to SYN | ACK
1884 */
1885 test_sem_take(K_MSEC(100), __LINE__);
1886
1887 ret = net_context_send(ctx, &data, 1, NULL, K_NO_WAIT, NULL);
1888 if (ret < 0) {
1889 zassert_true(false, "Failed to send data to peer");
1890 }
1891
1892 /* Peer will release the semaphore after it sends ACK for data */
1893 test_sem_take(K_MSEC(100), __LINE__);
1894
1895 net_context_put(ctx);
1896
1897 /* The lock is not released as we do not receive final ACK.
1898 */
1899 test_sem_take_failure(K_MSEC(400), __LINE__);
1900 }
1901
create_server_socket(uint32_t my_seq,uint32_t my_ack)1902 static struct net_context *create_server_socket(uint32_t my_seq,
1903 uint32_t my_ack)
1904 {
1905 struct net_context *ctx;
1906 int ret;
1907
1908 t_state = T_SYN;
1909 test_case_no = TEST_SERVER_IPV6;
1910 seq = my_seq;
1911 ack = my_ack;
1912
1913 ret = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP, &ctx);
1914 if (ret < 0) {
1915 zassert_true(false, "Failed to get net_context");
1916 }
1917
1918 net_context_ref(ctx);
1919
1920 ret = net_context_bind(ctx, (struct sockaddr *)&my_addr_v6_s,
1921 sizeof(struct sockaddr_in6));
1922 if (ret < 0) {
1923 zassert_true(false, "Failed to bind net_context");
1924 }
1925
1926 ret = net_context_listen(ctx, 1);
1927 if (ret < 0) {
1928 zassert_true(false, "Failed to listen on net_context");
1929 }
1930
1931 /* Trigger the peer to send SYN */
1932 k_work_reschedule(&test_server, K_NO_WAIT);
1933
1934 ret = net_context_accept(ctx, test_tcp_accept_cb, K_FOREVER, NULL);
1935 if (ret < 0) {
1936 zassert_true(false, "Failed to set accept on net_context");
1937 }
1938
1939 /* test_tcp_accept_cb will release the semaphore after successful
1940 * connection.
1941 */
1942 test_sem_take(K_MSEC(100), __LINE__);
1943
1944 return ctx;
1945 }
1946
check_rst_fail(uint32_t seq_value)1947 static void check_rst_fail(uint32_t seq_value)
1948 {
1949 struct net_pkt *reply;
1950 int rsterr_before, rsterr_after;
1951 int ret;
1952
1953 rsterr_before = GET_STAT(net_iface, tcp.rsterr);
1954
1955 /* Invalid seq in the RST packet */
1956 seq = seq_value;
1957
1958 reply = prepare_rst_packet(AF_INET6, htons(MY_PORT), htons(PEER_PORT));
1959
1960 ret = net_recv_data(net_iface, reply);
1961 zassert_true(ret == 0, "recv data failed (%d)", ret);
1962
1963 /* Let the receiving thread run */
1964 k_msleep(50);
1965
1966 rsterr_after = GET_STAT(net_iface, tcp.rsterr);
1967
1968 zassert_equal(rsterr_before + 1, rsterr_after,
1969 "RST packet not skipped (before %d, after %d)",
1970 rsterr_before, rsterr_after);
1971 }
1972
check_rst_succeed(struct net_context * ctx,uint32_t seq_value)1973 static void check_rst_succeed(struct net_context *ctx,
1974 uint32_t seq_value)
1975 {
1976 struct net_pkt *reply;
1977 int rsterr_before, rsterr_after;
1978 int ret;
1979
1980 /* Make sure that various other corner cases work */
1981 if (ctx == NULL) {
1982 ctx = create_server_socket(0, 0);
1983 }
1984
1985 /* Another valid seq in the RST packet */
1986 seq = ack + seq_value;
1987
1988 reply = prepare_rst_packet(AF_INET6, htons(MY_PORT), htons(PEER_PORT));
1989
1990 rsterr_before = GET_STAT(net_iface, tcp.rsterr);
1991
1992 ret = net_recv_data(net_iface, reply);
1993 zassert_true(ret == 0, "recv data failed (%d)", ret);
1994
1995 /* Let the receiving thread run */
1996 k_msleep(50);
1997
1998 rsterr_after = GET_STAT(net_iface, tcp.rsterr);
1999
2000 zassert_equal(rsterr_before, rsterr_after,
2001 "RST packet skipped (before %d, after %d)",
2002 rsterr_before, rsterr_after);
2003
2004 net_context_put(ctx);
2005 net_context_put(accepted_ctx);
2006
2007 /* Let other threads run (so the TCP context is actually freed) */
2008 k_msleep(10);
2009 }
2010
ZTEST(net_tcp,test_client_invalid_rst)2011 ZTEST(net_tcp, test_client_invalid_rst)
2012 {
2013 struct net_context *ctx;
2014 struct tcp *conn;
2015 uint16_t wnd;
2016
2017 ctx = create_server_socket(0, 0);
2018
2019 conn = ctx->tcp;
2020 wnd = conn->recv_win;
2021
2022 /* Failure cases, the RST packets should be dropped */
2023 check_rst_fail(ack - 1);
2024 check_rst_fail(ack + wnd);
2025
2026 /* Then send a valid seq in the RST packet */
2027 check_rst_succeed(ctx, wnd - 1);
2028 check_rst_succeed(NULL, 0);
2029 check_rst_succeed(NULL, 1);
2030
2031 /* net_context is released within check_rst_succeed() */
2032 }
2033
2034 #define MAX_DATA 100
2035 #define OUT_OF_ORDER_SEQ_INIT -15
2036 static uint32_t expected_ack;
2037 static struct net_context *ooo_ctx;
2038
handle_server_recv_out_of_order(struct net_pkt * pkt)2039 static void handle_server_recv_out_of_order(struct net_pkt *pkt)
2040 {
2041 struct tcphdr th;
2042 int ret;
2043
2044 ret = read_tcp_header(pkt, &th);
2045 if (ret < 0) {
2046 goto fail;
2047 }
2048
2049 /* Verify that we received all the queued data */
2050 zassert_equal(expected_ack, ntohl(th.th_ack),
2051 "Not all pending data received. "
2052 "Expected ACK %u but got %u",
2053 expected_ack, ntohl(th.th_ack));
2054
2055 test_sem_give();
2056
2057 return;
2058
2059 fail:
2060 zassert_true(false, "%s failed", __func__);
2061 net_pkt_unref(pkt);
2062 }
2063
2064 struct out_of_order_check_struct {
2065 int seq_offset;
2066 int length;
2067 int ack_offset;
2068 int delay_ms;
2069 };
2070
2071 static struct out_of_order_check_struct out_of_order_check_list[] = {
2072 { 30, 10, 0, 0}, /* First packet will be out-of-order */
2073 { 20, 12, 0, 0},
2074 { 10, 9, 0, 0}, /* Section with a gap */
2075 { 0, 10, 10, 0},
2076 { 10, 10, 40, 0}, /* First sequence complete */
2077 { 50, 6, 40, 0},
2078 { 50, 3, 40, 0}, /* Discardable packet */
2079 { 55, 5, 40, 0},
2080 { 40, 10, 60, 0}, /* Some bigger data */
2081 { 61, 2, 60, 0},
2082 { 60, 5, 65, 0}, /* Over lapped incoming packet */
2083 { 66, 4, 65, 0},
2084 { 65, 5, 70, 0}, /* Over lapped incoming packet, at boundary */
2085 { 72, 2, 70, 0},
2086 { 71, 4, 70, 0},
2087 { 70, 1, 75, 0}, /* Over lapped in out of order processing */
2088 { 78, 2, 75, 0},
2089 { 77, 3, 75, 0},
2090 { 75, 2, 80, 0}, /* Over lapped in out of order processing, at boundary */
2091 };
2092
checklist_based_out_of_order_test(struct out_of_order_check_struct * check_list,int num_checks,int sequence_base)2093 static void checklist_based_out_of_order_test(struct out_of_order_check_struct *check_list,
2094 int num_checks, int sequence_base)
2095 {
2096 const uint8_t *data = lorem_ipsum + 10;
2097 struct net_pkt *pkt;
2098 int ret, i;
2099 struct out_of_order_check_struct *check_ptr;
2100
2101 for (i = 0; i < num_checks; i++) {
2102 check_ptr = &check_list[i];
2103
2104 seq = sequence_base + check_ptr->seq_offset;
2105 pkt = prepare_data_packet(AF_INET6, htons(MY_PORT), htons(PEER_PORT),
2106 &data[check_ptr->seq_offset], check_ptr->length);
2107 zassert_not_null(pkt, "Cannot create pkt");
2108
2109 /* Initial ack for the last correctly received byte = SYN flag */
2110 expected_ack = sequence_base + check_ptr->ack_offset;
2111
2112 ret = net_recv_data(net_iface, pkt);
2113 zassert_true(ret == 0, "recv data failed (%d)", ret);
2114
2115 /* Let the IP stack to process the packet properly */
2116 k_yield();
2117
2118 /* Peer will release the semaphore after it sends proper ACK to the
2119 * queued data.
2120 */
2121 test_sem_take(K_MSEC(1000), __LINE__);
2122
2123 /* Optionally wait between transfers */
2124 if (check_ptr->delay_ms) {
2125 k_sleep(K_MSEC(check_ptr->delay_ms));
2126 }
2127 }
2128 }
2129
test_server_recv_out_of_order_data(void)2130 static void test_server_recv_out_of_order_data(void)
2131 {
2132 /* Only run the tests if queueing is enabled */
2133 if (CONFIG_NET_TCP_RECV_QUEUE_TIMEOUT == 0) {
2134 return;
2135 }
2136
2137 k_sem_reset(&test_sem);
2138
2139 /* Start the sequence numbering so that we will wrap it (just for
2140 * testing purposes)
2141 */
2142 ooo_ctx = create_server_socket(OUT_OF_ORDER_SEQ_INIT, -15U);
2143
2144 /* This will force the packet to be routed to our checker func
2145 * handle_server_recv_out_of_order()
2146 */
2147 test_case_no = TEST_SERVER_RECV_OUT_OF_ORDER_DATA;
2148
2149 /* Run over the checklist to complete the test */
2150 checklist_based_out_of_order_test(out_of_order_check_list,
2151 ARRAY_SIZE(out_of_order_check_list),
2152 OUT_OF_ORDER_SEQ_INIT + 1);
2153 }
2154
2155 struct out_of_order_check_struct reorder_timeout_list[] = {
2156 /* Wait more then the receive queue timeout */
2157 { 90, 10, 80, CONFIG_NET_TCP_RECV_QUEUE_TIMEOUT * 2},
2158 /* First message has been timeout, so only this is acknowledged */
2159 { 80, 10, 90, 0},
2160 };
2161
2162
2163 /* This test expects that the system is in correct state after a call to
2164 * test_server_recv_out_of_order_data(), so this test must be run after that
2165 * test.
2166 */
test_server_timeout_out_of_order_data(void)2167 static void test_server_timeout_out_of_order_data(void)
2168 {
2169 struct net_pkt *rst;
2170 int ret;
2171
2172 if (CONFIG_NET_TCP_RECV_QUEUE_TIMEOUT == 0) {
2173 return;
2174 }
2175
2176 k_sem_reset(&test_sem);
2177
2178 checklist_based_out_of_order_test(reorder_timeout_list,
2179 ARRAY_SIZE(reorder_timeout_list),
2180 OUT_OF_ORDER_SEQ_INIT + 1);
2181
2182 /* Just send a RST packet to abort the underlying connection, so that
2183 * the testcase does not need to implement full TCP closing handshake.
2184 */
2185 seq = expected_ack + 1;
2186 rst = prepare_rst_packet(AF_INET6, htons(MY_PORT), htons(PEER_PORT));
2187
2188 ret = net_recv_data(net_iface, rst);
2189 zassert_true(ret == 0, "recv data failed (%d)", ret);
2190
2191 /* Let the receiving thread run */
2192 k_msleep(50);
2193
2194 net_context_put(ooo_ctx);
2195 net_context_put(accepted_ctx);
2196 }
2197
ZTEST(net_tcp,test_server_out_of_order_data)2198 ZTEST(net_tcp, test_server_out_of_order_data)
2199 {
2200 test_server_recv_out_of_order_data();
2201 test_server_timeout_out_of_order_data();
2202 }
2203
handle_server_rst_on_closed_port(sa_family_t af,struct tcphdr * th)2204 static void handle_server_rst_on_closed_port(sa_family_t af, struct tcphdr *th)
2205 {
2206 switch (t_state) {
2207 case T_SYN_ACK:
2208 /* Port was closed so expect RST instead of SYN */
2209 test_verify_flags(th, RST | ACK);
2210 zassert_equal(ntohl(th->th_seq), 0, "Invalid SEQ value");
2211 zassert_equal(ntohl(th->th_ack), seq, "Invalid ACK value");
2212 t_state = T_CLOSING;
2213 test_sem_give();
2214 break;
2215 default:
2216 return;
2217 }
2218 }
2219
2220 /* Test case scenario
2221 * Send SYN
2222 * expect RST ACK (closed port)
2223 * any failures cause test case to fail.
2224 */
ZTEST(net_tcp,test_server_rst_on_closed_port)2225 ZTEST(net_tcp, test_server_rst_on_closed_port)
2226 {
2227 t_state = T_SYN;
2228 test_case_no = TEST_SERVER_RST_ON_CLOSED_PORT;
2229 seq = ack = 0;
2230
2231 k_sem_reset(&test_sem);
2232
2233 /* Trigger the peer to send SYN */
2234 k_work_reschedule(&test_server, K_NO_WAIT);
2235
2236 /* Peer will release the semaphore after it receives RST */
2237 test_sem_take(K_MSEC(100), __LINE__);
2238 }
2239
handle_server_rst_on_listening_port(sa_family_t af,struct tcphdr * th)2240 static void handle_server_rst_on_listening_port(sa_family_t af, struct tcphdr *th)
2241 {
2242 switch (t_state) {
2243 case T_DATA_ACK:
2244 /* No active connection so expect RST instead of ACK */
2245 test_verify_flags(th, RST);
2246 zassert_equal(ntohl(th->th_seq), ack, "Invalid SEQ value");
2247 t_state = T_CLOSING;
2248 test_sem_give();
2249 break;
2250 default:
2251 return;
2252 }
2253 }
2254
dummy_accept_cb(struct net_context * ctx,struct sockaddr * addr,socklen_t addrlen,int status,void * user_data)2255 static void dummy_accept_cb(struct net_context *ctx, struct sockaddr *addr,
2256 socklen_t addrlen, int status, void *user_data)
2257 {
2258 /* Should not ever be called. */
2259 zassert_unreachable("Should not have called dummy accept cb");
2260 }
2261
2262 /* Test case scenario
2263 * Open listening port
2264 * Send DATA packet to the listening port
2265 * expect RST (no matching connection)
2266 * any failures cause test case to fail.
2267 */
ZTEST(net_tcp,test_server_rst_on_listening_port_no_active_connection)2268 ZTEST(net_tcp, test_server_rst_on_listening_port_no_active_connection)
2269 {
2270 struct net_context *ctx;
2271 int ret;
2272
2273 t_state = T_DATA;
2274 test_case_no = TEST_SERVER_RST_ON_LISTENING_PORT_NO_ACTIVE_CONNECTION;
2275 seq = ack = 200;
2276
2277 k_sem_reset(&test_sem);
2278
2279 ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ctx);
2280 if (ret < 0) {
2281 zassert_true(false, "Failed to get net_context");
2282 }
2283
2284 net_context_ref(ctx);
2285
2286 ret = net_context_bind(ctx, (struct sockaddr *)&my_addr_s,
2287 sizeof(struct sockaddr_in));
2288 if (ret < 0) {
2289 zassert_true(false, "Failed to bind net_context");
2290 }
2291
2292 ret = net_context_listen(ctx, 1);
2293 if (ret < 0) {
2294 zassert_true(false, "Failed to listen on net_context");
2295 }
2296
2297 ret = net_context_accept(ctx, dummy_accept_cb, K_NO_WAIT, NULL);
2298 if (ret < 0) {
2299 zassert_true(false, "Failed to set accept on net_context");
2300 }
2301
2302 /* Trigger the peer to send data to the listening port w/o active connection */
2303 k_work_reschedule(&test_server, K_NO_WAIT);
2304
2305 /* Peer will release the semaphore after it receives RST */
2306 test_sem_take(K_MSEC(100), __LINE__);
2307
2308 net_context_put(ctx);
2309 }
2310
2311 /* Test case scenario
2312 * Expect SYN
2313 * Send ACK (wrong ACK number),
2314 * expect RST,
2315 * expect SYN (retransmission),
2316 * send SYN ACK,
2317 * expect ACK (handshake success),
2318 * expect FIN,
2319 * send FIN ACK,
2320 * expect ACK.
2321 * any failures cause test case to fail.
2322 */
handle_syn_invalid_ack(sa_family_t af,struct tcphdr * th)2323 static void handle_syn_invalid_ack(sa_family_t af, struct tcphdr *th)
2324 {
2325 static bool invalid_ack = true;
2326 struct net_pkt *reply;
2327 int ret;
2328
2329
2330 switch (t_state) {
2331 case T_SYN:
2332 test_verify_flags(th, SYN);
2333 if (invalid_ack) {
2334 ack = ntohl(th->th_seq) + 1000U;
2335 reply = prepare_ack_packet(af, htons(MY_PORT),
2336 th->th_sport);
2337 /* Expect RST now. */
2338 t_state = T_RST;
2339 invalid_ack = false;
2340 } else {
2341 ack = ntohl(th->th_seq) + 1U;
2342 reply = prepare_syn_ack_packet(af, htons(MY_PORT),
2343 th->th_sport);
2344 /* Proceed with the handshake on second attempt. */
2345 t_state = T_SYN_ACK;
2346 }
2347
2348 break;
2349 case T_SYN_ACK:
2350 test_verify_flags(th, ACK);
2351 /* connection is successful */
2352 t_state = T_FIN;
2353 return;
2354 case T_FIN:
2355 test_verify_flags(th, FIN | ACK);
2356 seq++;
2357 ack++;
2358 t_state = T_FIN_ACK;
2359 reply = prepare_fin_ack_packet(af, htons(MY_PORT),
2360 th->th_sport);
2361 break;
2362 case T_FIN_ACK:
2363 test_verify_flags(th, ACK);
2364 test_sem_give();
2365 return;
2366 case T_RST:
2367 test_verify_flags(th, RST);
2368 zassert_equal(ntohl(th->th_seq), ack, "Invalid SEQ value");
2369
2370 /* Wait for SYN retransmission. */
2371 t_state = T_SYN;
2372 return;
2373 default:
2374 return;
2375 }
2376
2377 ret = net_recv_data(net_iface, reply);
2378 if (ret < 0) {
2379 goto fail;
2380 }
2381
2382 return;
2383 fail:
2384 zassert_true(false, "%s failed", __func__);
2385 }
2386
ZTEST(net_tcp,test_client_rst_on_unexpected_ack_on_syn)2387 ZTEST(net_tcp, test_client_rst_on_unexpected_ack_on_syn)
2388 {
2389 struct net_context *ctx;
2390 int ret;
2391
2392 t_state = T_SYN;
2393 test_case_no = TEST_CLIENT_RST_ON_UNEXPECTED_ACK_ON_SYN;
2394 seq = ack = 0;
2395
2396 ret = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ctx);
2397 if (ret < 0) {
2398 zassert_true(false, "Failed to get net_context");
2399 }
2400
2401 net_context_ref(ctx);
2402
2403 ret = net_context_connect(ctx, (struct sockaddr *)&peer_addr_s,
2404 sizeof(struct sockaddr_in),
2405 NULL, K_MSEC(1000), NULL);
2406
2407 zassert_equal(ret, 0, "Connect failed");
2408 zassert_equal(net_context_get_state(ctx), NET_CONTEXT_CONNECTED,
2409 "Context should be connected");
2410
2411 /* Just wait for the connection teardown. */
2412 net_context_put(ctx);
2413
2414 /* Peer will release the semaphore after it receives final ACK */
2415 test_sem_take(K_MSEC(100), __LINE__);
2416 }
2417
2418 #define TEST_FIN_DATA "test_data"
2419
2420 static enum fin_data_variant {
2421 FIN_DATA_FIN,
2422 FIN_DATA_FIN_ACK,
2423 FIN_DATA_FIN_ACK_PSH,
2424 } test_fin_data_variant;
2425
2426 static struct k_work_delayable test_fin_data_work;
2427
2428 /* In this test we check that FIN packet containing data is handled correctly
2429 * by the TCP stack.
2430 */
handle_client_fin_ack_with_data_test(sa_family_t af,struct tcphdr * th)2431 static void handle_client_fin_ack_with_data_test(sa_family_t af, struct tcphdr *th)
2432 {
2433 static uint16_t peer_port;
2434 struct net_pkt *reply;
2435 uint8_t flags = 0;
2436
2437 switch (t_state) {
2438 case T_SYN:
2439 test_verify_flags(th, SYN);
2440 device_initial_seq = ntohl(th->th_seq);
2441 seq = 0U;
2442 ack = ntohl(th->th_seq) + 1U;
2443 peer_port = th->th_sport;
2444 reply = prepare_syn_ack_packet(af, htons(MY_PORT), peer_port);
2445 seq++;
2446 t_state = T_SYN_ACK;
2447 break;
2448 case T_SYN_ACK:
2449 test_verify_flags(th, ACK);
2450 t_state = T_DATA;
2451
2452 /* FIN packet with DATA needs to be rescheduled for later - if
2453 * we send it here, the net_context_recv() won't have a chance
2454 * to execute, hence no callback will be registered and data
2455 * will be dropped.
2456 */
2457 k_work_reschedule(&test_fin_data_work, K_MSEC(1));
2458 return;
2459 case T_DATA:
2460 switch (test_fin_data_variant) {
2461 case FIN_DATA_FIN:
2462 flags = FIN;
2463 t_state = T_FIN;
2464 break;
2465 case FIN_DATA_FIN_ACK:
2466 flags = FIN | ACK;
2467 t_state = T_FIN_ACK;
2468 break;
2469 case FIN_DATA_FIN_ACK_PSH:
2470 flags = FIN | ACK | PSH;
2471 t_state = T_FIN_ACK;
2472 break;
2473 }
2474
2475 reply = tester_prepare_tcp_pkt(af, htons(MY_PORT), peer_port,
2476 flags, TEST_FIN_DATA,
2477 strlen(TEST_FIN_DATA));
2478 seq += strlen(TEST_FIN_DATA) + 1;
2479
2480 break;
2481 case T_FIN:
2482 test_verify_flags(th, ACK);
2483 zassert_equal(get_rel_seq(th), 1, "Unexpected SEQ number in T_FIN, got %d",
2484 get_rel_seq(th));
2485 zassert_equal(ntohl(th->th_ack), seq, "Unexpected ACK in T_FIN, got %d",
2486 ntohl(th->th_ack));
2487
2488 t_state = T_CLOSING;
2489 return;
2490 case T_FIN_ACK:
2491 test_verify_flags(th, FIN | ACK);
2492 zassert_equal(get_rel_seq(th), 1, "Unexpected SEQ number in T_FIN_ACK, got %d",
2493 get_rel_seq(th));
2494 zassert_equal(ntohl(th->th_ack), seq, "Unexpected ACK in T_FIN_ACK, got %d",
2495 ntohl(th->th_ack));
2496
2497 ack++;
2498 reply = prepare_ack_packet(af, htons(MY_PORT), peer_port);
2499 t_state = T_SYN;
2500 break;
2501
2502 case T_CLOSING:
2503 test_verify_flags(th, FIN);
2504 zassert_equal(get_rel_seq(th), 1, "Unexpected SEQ number in T_CLOSING, got %d",
2505 get_rel_seq(th));
2506
2507 ack++;
2508 reply = prepare_ack_packet(af, htons(MY_PORT), peer_port);
2509 t_state = T_SYN;
2510 break;
2511
2512 default:
2513 zassert_true(false, "%s unexpected state", __func__);
2514 return;
2515 }
2516
2517 zassert_ok(net_recv_data(net_iface, reply), "%s failed", __func__);
2518 }
2519
2520
test_fin_data_handler(struct k_work * work)2521 static void test_fin_data_handler(struct k_work *work)
2522 {
2523 ARG_UNUSED(work);
2524
2525 handle_client_fin_ack_with_data_test(AF_INET, NULL);
2526 }
2527
test_fin_ack_data_recv_cb(struct net_context * context,struct net_pkt * pkt,union net_ip_header * ip_hdr,union net_proto_header * proto_hdr,int status,void * user_data)2528 static void test_fin_ack_data_recv_cb(struct net_context *context,
2529 struct net_pkt *pkt,
2530 union net_ip_header *ip_hdr,
2531 union net_proto_header *proto_hdr,
2532 int status,
2533 void *user_data)
2534 {
2535 if (status) {
2536 zassert_true(false, "failed to recv the data");
2537 }
2538
2539 if (pkt) {
2540 uint8_t buf[sizeof(TEST_FIN_DATA)] = { 0 };
2541 int data_len = net_pkt_remaining_data(pkt);
2542
2543 zassert_equal(data_len, strlen(TEST_FIN_DATA),
2544 "Invalid packet length, %d", data_len);
2545 zassert_ok(net_pkt_read(pkt, buf, data_len));
2546 zassert_mem_equal(buf, TEST_FIN_DATA, data_len);
2547
2548 net_pkt_unref(pkt);
2549 }
2550
2551 test_sem_give();
2552 }
2553
2554 /* Test case scenario IPv4
2555 * expect SYN,
2556 * send SYN ACK,
2557 * expect ACK,
2558 * send FIN/FIN,ACK/FIN,ACK,PSH with Data,
2559 * expect FIN/FIN,ACK/ACK,
2560 * send ACK
2561 * any failures cause test case to fail.
2562 */
ZTEST(net_tcp,test_client_fin_ack_with_data)2563 ZTEST(net_tcp, test_client_fin_ack_with_data)
2564 {
2565 struct net_context *ctx;
2566
2567 test_case_no = TEST_CLIENT_FIN_ACK_WITH_DATA;
2568
2569 k_work_init_delayable(&test_fin_data_work, test_fin_data_handler);
2570
2571 for (enum fin_data_variant variant = FIN_DATA_FIN;
2572 variant <= FIN_DATA_FIN_ACK_PSH; variant++) {
2573 test_fin_data_variant = variant;
2574 t_state = T_SYN;
2575 seq = ack = 0;
2576
2577 zassert_ok(net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &ctx),
2578 "Failed to get net_context");
2579
2580 net_context_ref(ctx);
2581
2582 zassert_ok(net_context_connect(ctx, (struct sockaddr *)&peer_addr_s,
2583 sizeof(struct sockaddr_in), NULL,
2584 K_MSEC(1000), NULL),
2585 "Failed to connect to peer");
2586 zassert_ok(net_context_recv(ctx, test_fin_ack_data_recv_cb,
2587 K_NO_WAIT, NULL),
2588 "Failed to recv data from peer");
2589
2590 /* Take sem twice, one for data packet, second for conn close
2591 * (NULL net_pkt).
2592 */
2593 test_sem_take(K_MSEC(100), __LINE__);
2594 test_sem_take(K_MSEC(100), __LINE__);
2595
2596 net_context_put(ctx);
2597
2598 /* Connection is in TIME_WAIT state, context will be released
2599 * after K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY), so wait for it.
2600 */
2601 k_sleep(K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY));
2602 }
2603 }
2604
2605 ZTEST_SUITE(net_tcp, NULL, presetup, NULL, NULL, NULL);
2606