1 #include "test_tcp_state.h"
2 
3 #include "lwip/priv/tcp_priv.h"
4 #include "lwip/stats.h"
5 #include "tcp_helper.h"
6 #include "lwip/inet_chksum.h"
7 
8 #ifdef _MSC_VER
9 #pragma warning(disable: 4307) /* we explicitly wrap around TCP seqnos */
10 #endif
11 
12 #if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
13 #error "This tests needs TCP- and MEMP-statistics enabled"
14 #endif
15 
16 static struct netif test_netif = {0};
17 static struct test_tcp_txcounters test_txcounters = {0};
18 
19 #define SEQNO1 (0xFFFFFF00 - TCP_MSS)
20 #define ISS    6510
21 static u8_t test_tcp_timer;
22 
23 /* our own version of tcp_tmr so we can reset fast/slow timer state */
24 static void
test_tcp_tmr(void)25 test_tcp_tmr(void)
26 {
27   tcp_fasttmr();
28   if (++test_tcp_timer & 1) {
29     tcp_slowtmr();
30   }
31 }
32 
33 /* Get TCP flags from packets */
34 static u8_t
get_tcp_flags_from_packet(struct pbuf * p,u16_t tcp_hdr_offset)35 get_tcp_flags_from_packet(struct pbuf *p, u16_t tcp_hdr_offset)
36 {
37   struct tcp_hdr tcphdr;
38   u16_t ret;
39   EXPECT_RETX(p != NULL, 0);
40   EXPECT_RETX(p->len >= tcp_hdr_offset + sizeof(struct tcp_hdr), 0);
41   ret = pbuf_copy_partial(p, &tcphdr, sizeof(struct tcp_hdr), tcp_hdr_offset);
42   EXPECT(ret == sizeof(struct tcp_hdr));
43   return TCPH_FLAGS(&tcphdr);
44 }
45 
46 /* Create listening tcp_pcb */
47 static struct tcp_pcb_listen *
create_listening_pcb(u16_t local_port,struct test_tcp_counters * counters)48 create_listening_pcb(u16_t local_port, struct test_tcp_counters *counters)
49 {
50   struct tcp_pcb *pcb;
51   struct tcp_pcb_listen *lpcb=NULL;
52   err_t err;
53   u16_t port = local_port?local_port:1234;
54 
55   if (counters) {
56     pcb = test_tcp_new_counters_pcb(counters);
57   } else {
58     pcb = tcp_new();
59   }
60   EXPECT(pcb != NULL);
61 
62   if (pcb) {
63     err = tcp_bind(pcb, &test_netif.ip_addr, port);
64     EXPECT(err == ERR_OK);
65     lpcb = (struct tcp_pcb_listen *)tcp_listen(pcb);
66   }
67 
68   return lpcb;
69 }
70 
71 /* Setup/teardown functions */
72 static struct netif* old_netif_list;
73 static struct netif* old_netif_default;
74 
75 static void
tcp_state_setup(void)76 tcp_state_setup(void)
77 {
78   struct tcp_pcb dummy_pcb; /* we need this for tcp_next_iss() only */
79 
80   /* reset iss to default (6510) */
81   tcp_ticks = 0;
82   tcp_ticks = 0 - (tcp_next_iss(&dummy_pcb) - 6510);
83   tcp_next_iss(&dummy_pcb);
84   tcp_ticks = 0;
85 
86   test_tcp_timer = 0;
87 
88   old_netif_list = netif_list;
89   old_netif_default = netif_default;
90   netif_list = NULL;
91   netif_default = NULL;
92   tcp_remove_all();
93   test_tcp_init_netif(&test_netif, &test_txcounters, &test_local_ip, &test_netmask);
94   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
95 }
96 
97 static void
tcp_state_teardown(void)98 tcp_state_teardown(void)
99 {
100   netif_list = NULL;
101   netif_default = NULL;
102   tcp_remove_all();
103   /* restore netif_list for next tests (e.g. loopif) */
104   netif_list = old_netif_list;
105   netif_default = old_netif_default;
106   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
107 }
108 
109 /* helper functions */
110 
111 static void
test_rst_generation_with_incoming_packet(struct pbuf * p,struct netif * netif,struct test_tcp_txcounters * tx_counters)112 test_rst_generation_with_incoming_packet(struct pbuf *p,
113   struct netif *netif, struct test_tcp_txcounters *tx_counters)
114 {
115   u16_t tcp_flags;
116   EXPECT_RET(p != NULL);
117   memset(tx_counters, 0, sizeof(struct test_tcp_txcounters));
118   /* pass the segment to tcp_input */
119   tx_counters->copy_tx_packets = 1;
120   test_tcp_input(p, netif);
121   tx_counters->copy_tx_packets = 0;
122   /* check if packets are as expected */
123   EXPECT(tx_counters->tx_packets != NULL);
124   if (tx_counters->tx_packets) {
125     tcp_flags = get_tcp_flags_from_packet(tx_counters->tx_packets, 20);
126     EXPECT(tcp_flags & TCP_RST);
127     pbuf_free(tx_counters->tx_packets);
128     tx_counters->tx_packets = NULL;
129   }
130 }
131 
132 /* Test functions */
133 
134 /* Call tcp_new() and test memp stats (max number) */
START_TEST(test_tcp_new_max_num)135 START_TEST(test_tcp_new_max_num)
136 {
137   struct tcp_pcb* pcb[MEMP_NUM_TCP_PCB + 1];
138   int i;
139   LWIP_UNUSED_ARG(_i);
140 
141   fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
142 
143   for(i = 0;i < MEMP_NUM_TCP_PCB; i++) {
144     pcb[i] = tcp_new();
145     fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == (i + 1));
146   }
147   fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == MEMP_NUM_TCP_PCB);
148   /* Trying to remove the oldest pcb in TIME_WAIT,LAST_ACK,CLOSING state when pcb full */
149   pcb[MEMP_NUM_TCP_PCB] = tcp_new();
150   fail_unless(pcb[MEMP_NUM_TCP_PCB] == NULL);
151   tcp_set_state(pcb[0], TIME_WAIT, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
152   pcb[MEMP_NUM_TCP_PCB] = tcp_new();
153   fail_unless(pcb[MEMP_NUM_TCP_PCB] != NULL);
154   fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == MEMP_NUM_TCP_PCB);
155 
156   for (i = 1; i <= MEMP_NUM_TCP_PCB; i++)
157   {
158     tcp_abort(pcb[i]);
159   }
160   fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
161 }
162 END_TEST
163 
164 
165 /* pcbs in TIME_WAIT state will be deleted when creating new pcb reach the max number */
START_TEST(test_tcp_new_max_num_remove_TIME_WAIT)166 START_TEST(test_tcp_new_max_num_remove_TIME_WAIT)
167 {
168   struct tcp_pcb* pcb;
169   struct tcp_pcb* pcb_list[MEMP_NUM_TCP_PCB + 1];
170   int i;
171   LWIP_UNUSED_ARG(_i);
172 
173   /* create a pcb in TIME_WAIT state */
174   pcb = tcp_new();
175   EXPECT_RET(pcb != NULL);
176   tcp_set_state(pcb, TIME_WAIT, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
177   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
178   EXPECT_RET(pcb->state == TIME_WAIT);
179 
180   /* Create max number pcbs */
181   for(i = 0;i < MEMP_NUM_TCP_PCB-1; i++) {
182     pcb_list[i] = tcp_new();
183     EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == (i + 2));
184   }
185   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == MEMP_NUM_TCP_PCB);
186 
187   /* Create one more pcb, and expect that the pcb in the TIME_WAIT state is deleted */
188   pcb_list[MEMP_NUM_TCP_PCB-1] = tcp_new();
189   EXPECT_RET(pcb_list[MEMP_NUM_TCP_PCB-1] != NULL);
190   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == MEMP_NUM_TCP_PCB);
191 
192   for (i = 0; i <= MEMP_NUM_TCP_PCB-1; i++)
193   {
194     tcp_abort(pcb_list[i]);
195   }
196   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
197 
198 }
199 END_TEST
200 
201 
202 /* Call tcp_connect to check active open */
START_TEST(test_tcp_connect_active_open)203 START_TEST(test_tcp_connect_active_open)
204 {
205   struct test_tcp_counters counters;
206   struct tcp_pcb *pcb;
207   struct pbuf *p;
208   err_t err;
209   u16_t test_port = 1234;
210   u32_t seqno = 0;
211   LWIP_UNUSED_ARG(_i);
212 
213   /* create and initialize the pcb */
214   tcp_ticks = SEQNO1 - ISS;
215   pcb = test_tcp_new_counters_pcb(&counters);
216   EXPECT_RET(pcb != NULL);
217 
218   /* Get seqno from SYN packet */
219   test_txcounters.copy_tx_packets = 1;
220   err = tcp_connect(pcb, &test_remote_ip, test_port, NULL);
221   test_txcounters.copy_tx_packets = 0;
222   EXPECT(err == ERR_OK);
223   EXPECT(pcb->state == SYN_SENT);
224   EXPECT(test_txcounters.num_tx_calls == 1);
225   EXPECT_RET(test_txcounters.tx_packets != NULL);
226   if (test_txcounters.tx_packets != NULL) {
227     struct tcp_hdr tcphdr;
228     u16_t ret;
229     ret = pbuf_copy_partial(test_txcounters.tx_packets, &tcphdr, 20, 20);
230     EXPECT(ret == 20);
231     EXPECT(TCPH_FLAGS(&tcphdr) & TCP_SYN);
232     pbuf_free(test_txcounters.tx_packets);
233     test_txcounters.tx_packets = NULL;
234     seqno = lwip_htonl(tcphdr.seqno);
235     EXPECT(seqno == pcb->lastack);
236   }
237 
238   /* check correct syn packet */
239   p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, test_port,
240     pcb->local_port, NULL, 0, 12345, seqno + 1, TCP_SYN|TCP_ACK);
241   EXPECT_RET(p != NULL);
242   test_tcp_input(p, &test_netif);
243   EXPECT_RET(pcb->state == ESTABLISHED);
244   EXPECT_RET(test_txcounters.num_tx_calls == 2);
245 
246   /* make sure the pcb is freed */
247   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
248   tcp_abort(pcb);
249   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
250 }
251 END_TEST
252 
START_TEST(test_tcp_active_close)253 START_TEST(test_tcp_active_close)
254 {
255   struct tcp_pcb *pcb, *pcbl;
256   struct test_tcp_counters counters;
257   struct pbuf *p;
258   err_t err;
259   u32_t i;
260   LWIP_UNUSED_ARG(_i);
261 
262   /* create TCP in LISTEN state */
263   memset(&counters, 0, sizeof(counters));
264   pcb = test_tcp_new_counters_pcb(&counters);
265   EXPECT_RET(pcb != NULL);
266   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
267   err = tcp_bind(pcb, &test_netif.ip_addr, 1234);
268   EXPECT_RET(err == ERR_OK);
269   pcbl = tcp_listen(pcb);
270   EXPECT_RET(pcbl != NULL);
271   EXPECT_RET(pcbl->state == LISTEN);
272   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
273   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 1);
274 
275   memset(&test_txcounters, 0, sizeof(test_txcounters));
276   err = tcp_close(pcbl);
277   EXPECT_RET(err == ERR_OK);
278   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
279   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 0);
280   EXPECT(test_txcounters.num_tx_calls == 0);
281 
282   /* close TCP in SYN_SENT state */
283   memset(&counters, 0, sizeof(counters));
284   pcb = test_tcp_new_counters_pcb(&counters);
285   EXPECT_RET(pcb != NULL);
286   err = tcp_connect(pcb, &test_netif.gw, 1234, NULL);
287   EXPECT_RET(err == ERR_OK);
288   EXPECT_RET(pcb->state == SYN_SENT);
289   EXPECT(test_txcounters.num_tx_calls == 1);
290   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
291 
292   memset(&test_txcounters, 0, sizeof(test_txcounters));
293   err = tcp_close(pcb);
294   EXPECT_RET(err == ERR_OK);
295   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
296   EXPECT(test_txcounters.num_tx_calls == 0);
297 
298   /* close TCP in ESTABLISHED state */
299   memset(&counters, 0, sizeof(counters));
300   pcb = test_tcp_new_counters_pcb(&counters);
301   EXPECT_RET(pcb != NULL);
302   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
303   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
304 
305   memset(&test_txcounters, 0, sizeof(test_txcounters));
306   err = tcp_close(pcb);
307   EXPECT_RET(err == ERR_OK);
308   EXPECT_RET(pcb->state == FIN_WAIT_1);
309   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
310   /* test_tcp_tmr(); */
311   EXPECT(test_txcounters.num_tx_calls == 1);
312   /* create a segment ACK and pass it to tcp_input */
313   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 1, TCP_ACK);
314   EXPECT_RET(p != NULL);
315   test_tcp_input(p, &test_netif);
316   EXPECT_RET(pcb->state == FIN_WAIT_2);
317   /* create a segment FIN and pass it to tcp_input */
318   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_FIN);
319   EXPECT_RET(p != NULL);
320   test_tcp_input(p, &test_netif);
321   EXPECT_RET(pcb->state == TIME_WAIT);
322   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
323   for (i = 0; i < 2 * TCP_MSL / TCP_TMR_INTERVAL + 1; i++) {
324     test_tcp_tmr();
325   }
326   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
327 }
328 END_TEST
329 
START_TEST(test_tcp_imultaneous_close)330 START_TEST(test_tcp_imultaneous_close)
331 {
332   struct test_tcp_counters counters;
333   struct tcp_pcb* pcb;
334   struct pbuf* p;
335   char data = 0x0f;
336   err_t err;
337   u32_t i;
338   LWIP_UNUSED_ARG(_i);
339 
340   /* initialize counter struct */
341   memset(&counters, 0, sizeof(counters));
342   counters.expected_data_len = 1;
343   counters.expected_data = &data;
344 
345   /* create and initialize the pcb */
346   pcb = test_tcp_new_counters_pcb(&counters);
347   EXPECT_RET(pcb != NULL);
348   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
349   err = tcp_close(pcb);
350   EXPECT_RET(err == ERR_OK);
351   EXPECT_RET(pcb->state == FIN_WAIT_1);
352   /* create a FIN segment */
353   p = tcp_create_rx_segment(pcb, &data, 0, 0, 0, TCP_FIN);
354   EXPECT(p != NULL);
355   if (p != NULL) {
356     test_tcp_input(p, &test_netif);
357   }
358   EXPECT_RET(pcb->state == CLOSING);
359   /* create an ACK segment */
360   p = tcp_create_rx_segment(pcb, &data, 0, 0, 1, TCP_ACK);
361   EXPECT(p != NULL);
362   if (p != NULL) {
363     test_tcp_input(p, &test_netif);
364   }
365   EXPECT_RET(pcb->state == TIME_WAIT);
366   for (i = 0; i < 2 * TCP_MSL / TCP_TMR_INTERVAL + 1; i++) {
367     test_tcp_tmr();
368   }
369   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
370 }
371 END_TEST
372 
373 
374 /* RST was generated when receive any incoming segment in CLOSED state */
START_TEST(test_tcp_gen_rst_in_CLOSED)375 START_TEST(test_tcp_gen_rst_in_CLOSED)
376 {
377   struct pbuf *p;
378   ip_addr_t src_addr = test_remote_ip;
379   ip_addr_t dst_addr = test_local_ip;
380   LWIP_UNUSED_ARG(_i);
381 
382   /* Do not create any pcb */
383 
384   /* create a segment */
385   p = tcp_create_segment(&src_addr, &dst_addr, TEST_REMOTE_PORT,
386     TEST_LOCAL_PORT, NULL, 0, 12345, 54321, TCP_ACK);
387   EXPECT(p != NULL);
388   test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
389   EXPECT(test_txcounters.num_tx_calls == 1);
390 
391 }
392 END_TEST
393 
394 /* RST was generated when receive ACK in LISTEN state */
START_TEST(test_tcp_gen_rst_in_LISTEN)395 START_TEST(test_tcp_gen_rst_in_LISTEN)
396 {
397   struct tcp_pcb_listen *lpcb;
398   struct pbuf *p;
399   ip_addr_t src_addr = test_remote_ip;
400   LWIP_UNUSED_ARG(_i);
401 
402   /* create a pcb in LISTEN state */
403   lpcb = create_listening_pcb(TEST_LOCAL_PORT, NULL);
404   EXPECT_RET(lpcb != NULL);
405 
406   /* create a segment */
407   p = tcp_create_segment(&src_addr,&lpcb->local_ip, TEST_REMOTE_PORT,
408     lpcb->local_port, NULL, 0, 12345, 54321, TCP_ACK);
409   EXPECT(p != NULL);
410   test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
411   EXPECT(test_txcounters.num_tx_calls == 1);
412 
413   /* the PCB still in LISTEN state */
414   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 1);
415   if (MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) != 0) {
416     /* can not use tcp_abort() */
417     tcp_close((struct tcp_pcb *)lpcb);
418   }
419 
420 }
421 END_TEST
422 
423 
424 /* RST was generated when receive an SYN in TIME_WAIT state */
START_TEST(test_tcp_gen_rst_in_TIME_WAIT)425 START_TEST(test_tcp_gen_rst_in_TIME_WAIT)
426 {
427   struct tcp_pcb *pcb;
428   struct pbuf *p;
429   LWIP_UNUSED_ARG(_i);
430 
431   /* create a pcb in LISTEN state */
432   pcb = tcp_new();
433   EXPECT_RET(pcb != NULL);
434   tcp_set_state(pcb, TIME_WAIT, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
435 
436   /* create a segment */
437   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_SYN);
438   EXPECT(p != NULL);
439   test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
440   EXPECT(test_txcounters.num_tx_calls == 1);
441 
442   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
443   EXPECT(pcb->state == TIME_WAIT);
444 }
445 END_TEST
446 
447 /* receive TCP_RST with different seqno */
START_TEST(test_tcp_process_rst_seqno)448 START_TEST(test_tcp_process_rst_seqno)
449 {
450   struct test_tcp_counters counters;
451   struct tcp_pcb *pcb;
452   struct pbuf *p;
453   err_t err;
454   LWIP_UNUSED_ARG(_i);
455 
456   /* create and initialize a pcb in SYN_SENT state */
457   memset(&counters, 0, sizeof(counters));
458   pcb = test_tcp_new_counters_pcb(&counters);
459   EXPECT_RET(pcb != NULL);
460   err = tcp_connect(pcb, &test_remote_ip, TEST_REMOTE_PORT, NULL);
461   EXPECT_RET(err == ERR_OK);
462 
463   /* a RST segment with incorrect seqno will not be accepted */
464   p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, TEST_REMOTE_PORT,
465     pcb->local_port, NULL, 0, 12345,  pcb->snd_nxt-10, TCP_RST);
466   EXPECT(p != NULL);
467   test_tcp_input(p, &test_netif);
468   EXPECT(counters.err_calls == 0);
469 
470   /* a RST segment with correct seqno will be accepted */
471   p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, TEST_REMOTE_PORT,
472     pcb->local_port, NULL, 0, 12345,  pcb->snd_nxt, TCP_RST);
473   EXPECT(p != NULL);
474   test_tcp_input(p, &test_netif);
475   EXPECT(counters.err_calls == 1);
476   counters.err_calls = 0;
477   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
478 
479   /* create another pcb in ESTABLISHED state */
480   memset(&counters, 0, sizeof(counters));
481   pcb = test_tcp_new_counters_pcb(&counters);
482   EXPECT_RET(pcb != NULL);
483   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
484 
485   /* a RST segment with incorrect seqno will not be accepted */
486   p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port,
487     pcb->local_port, NULL, 0, pcb->rcv_nxt-10, 54321, TCP_RST);
488   EXPECT(p != NULL);
489   test_tcp_input(p, &test_netif);
490   EXPECT(counters.err_calls == 0);
491 
492   /* a RST segment with correct seqno will be accepted */
493   p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, TEST_REMOTE_PORT,
494     pcb->local_port, NULL, 0, pcb->rcv_nxt, 54321, TCP_RST);
495   EXPECT(p != NULL);
496   test_tcp_input(p, &test_netif);
497   EXPECT(counters.err_calls == 1);
498   counters.err_calls = 0;
499   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
500 
501 }
502 END_TEST
503 
504 /* RST was generated when receive an SYN+ACK with incorrect ACK number in SYN_SENT state */
START_TEST(test_tcp_gen_rst_in_SYN_SENT_ackseq)505 START_TEST(test_tcp_gen_rst_in_SYN_SENT_ackseq)
506 {
507   struct tcp_pcb *pcb;
508   struct pbuf *p;
509   u16_t test_port = 1234;
510   err_t err;
511   LWIP_UNUSED_ARG(_i);
512 
513   /* create and initialize a pcb in listen state */
514   pcb = tcp_new();
515   EXPECT_RET(pcb != NULL);
516   err = tcp_connect(pcb, &test_remote_ip, test_port, NULL);
517   EXPECT_RET(err == ERR_OK);
518 
519   /* create a SYN+ACK segment with incorrect seqno */
520   p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port,
521     pcb->local_port, NULL, 0, 12345,  pcb->lastack-10, TCP_SYN|TCP_ACK);
522   EXPECT(p != NULL);
523   test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
524 
525   /* LWIP: send RST then re-send SYN immediately */
526   EXPECT(test_txcounters.num_tx_calls == 2);
527 
528 }
529 END_TEST
530 
531 /* RST was generated when receive an ACK without SYN in SYN_SENT state */
START_TEST(test_tcp_gen_rst_in_SYN_SENT_non_syn_ack)532 START_TEST(test_tcp_gen_rst_in_SYN_SENT_non_syn_ack)
533 {
534   struct tcp_pcb *pcb;
535   struct pbuf *p;
536   u16_t test_port = 1234;
537   err_t err;
538   LWIP_UNUSED_ARG(_i);
539 
540   /* create and initialize a pcb in listen state */
541   pcb = tcp_new();
542   EXPECT_RET(pcb != NULL);
543   err = tcp_connect(pcb, &test_remote_ip, test_port, NULL);
544   EXPECT_RET(err == ERR_OK);
545 
546   /* create a SYN+ACK segment with incorrect seqno */
547   p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port,
548     pcb->local_port, NULL, 0, 12345,  pcb->lastack, TCP_ACK);
549   EXPECT(p != NULL);
550   test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
551 
552   /* LWIP: send RST then re-send SYN immediately */
553   EXPECT(test_txcounters.num_tx_calls == 2);
554 
555 }
556 END_TEST
557 
558 /* RST was generated when receive an ACK with incorrect seqno in SYN_RCVD state */
START_TEST(test_tcp_gen_rst_in_SYN_RCVD)559 START_TEST(test_tcp_gen_rst_in_SYN_RCVD)
560 {
561   struct tcp_pcb_listen *lpcb;
562   struct pbuf *p;
563   u32_t ack_seqno = 0;
564   ip_addr_t src_addr = test_remote_ip;
565   LWIP_UNUSED_ARG(_i);
566 
567   /* create and initialize a pcb in listen state */
568   lpcb = create_listening_pcb(TEST_LOCAL_PORT, NULL);
569   EXPECT_RET(lpcb != NULL);
570 
571   /* LISTEN -> SYN_RCVD */
572   p = tcp_create_segment(&src_addr, &lpcb->local_ip, TEST_REMOTE_PORT,
573     lpcb->local_port, NULL, 0, 1000, 54321, TCP_SYN);
574   EXPECT(p != NULL);
575   memset(&test_txcounters, 0, sizeof(struct test_tcp_txcounters));
576   test_tcp_input(p, &test_netif);
577   EXPECT(test_txcounters.num_tx_calls == 1);
578   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
579   if (MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1) {
580     ack_seqno = tcp_active_pcbs[0].lastack;
581   }
582 
583   /* create a ACK segment with incorrect seqno */
584   p = tcp_create_segment(&src_addr, &lpcb->local_ip, TEST_REMOTE_PORT,
585     lpcb->local_port, NULL, 0, 1001, ack_seqno+1111, TCP_ACK);
586   EXPECT(p != NULL);
587   test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
588   EXPECT(test_txcounters.num_tx_calls == 1);
589 
590   /* the active pcb still exists */
591   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
592   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 1);
593   if (MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) != 0) {
594     /* can not use tcp_abort() */
595     tcp_close((struct tcp_pcb *)lpcb);
596   }
597 }
598 END_TEST
599 
600 /* a listen pcb returns to LISTEN from SYN_RCVD when RST received */
START_TEST(test_tcp_receive_rst_SYN_RCVD_to_LISTEN)601 START_TEST(test_tcp_receive_rst_SYN_RCVD_to_LISTEN)
602 {
603   struct tcp_pcb_listen *lpcb;
604   struct pbuf *p;
605   u16_t tcp_flags;
606   ip_addr_t src_addr = test_remote_ip;
607   LWIP_UNUSED_ARG(_i);
608 
609   /* create and initialize a pcb in listen state */
610   lpcb = create_listening_pcb(TEST_LOCAL_PORT, NULL);
611   EXPECT_RET(lpcb != NULL);
612 
613   /* create a SYN segment */
614   p = tcp_create_segment(&src_addr, &lpcb->local_ip, TEST_REMOTE_PORT,
615     lpcb->local_port, NULL, 0, 1000, 54321, TCP_SYN);
616   EXPECT(p != NULL);
617   /* pass the segment to tcp_input */
618   memset(&test_txcounters, 0, sizeof(struct test_tcp_txcounters));
619   test_txcounters.copy_tx_packets = 1;
620   test_tcp_input(p, &test_netif);
621   test_txcounters.copy_tx_packets = 0;
622   /* check if packets are as expected */
623   EXPECT(test_txcounters.num_tx_calls == 1);
624   tcp_flags = get_tcp_flags_from_packet(test_txcounters.tx_packets, 20);
625   pbuf_free(test_txcounters.tx_packets);
626   test_txcounters.tx_packets = NULL;
627   EXPECT((tcp_flags & TCP_SYN) && (tcp_flags & TCP_ACK));
628   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
629 
630   /* create a RST segment */
631   p = tcp_create_segment(&src_addr, &lpcb->local_ip, TEST_REMOTE_PORT,
632     lpcb->local_port, NULL, 0, 1001, 54321, TCP_RST);
633   EXPECT(p != NULL);
634   test_tcp_input(p, &test_netif);
635   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
636   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 1);
637 
638   if (MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) != 0) {
639     /* can not use tcp_abort() */
640     tcp_close((struct tcp_pcb *)lpcb);
641   }
642 }
643 END_TEST
644 
645 /** Create the suite including all tests for this module */
646 Suite *
tcp_state_suite(void)647 tcp_state_suite(void)
648 {
649   testfunc tests[] = {
650     TESTFUNC(test_tcp_new_max_num),
651     TESTFUNC(test_tcp_new_max_num_remove_TIME_WAIT),
652     TESTFUNC(test_tcp_connect_active_open),
653     TESTFUNC(test_tcp_active_close),
654     TESTFUNC(test_tcp_imultaneous_close),
655     TESTFUNC(test_tcp_gen_rst_in_CLOSED),
656     TESTFUNC(test_tcp_gen_rst_in_LISTEN),
657     TESTFUNC(test_tcp_gen_rst_in_TIME_WAIT),
658     TESTFUNC(test_tcp_process_rst_seqno),
659     TESTFUNC(test_tcp_gen_rst_in_SYN_SENT_ackseq),
660     TESTFUNC(test_tcp_gen_rst_in_SYN_SENT_non_syn_ack),
661     TESTFUNC(test_tcp_gen_rst_in_SYN_RCVD),
662     TESTFUNC(test_tcp_receive_rst_SYN_RCVD_to_LISTEN),
663   };
664   return create_suite("TCP_STATE", tests, sizeof(tests) / sizeof(testfunc), tcp_state_setup, tcp_state_teardown);
665 }
666