[TCPIP] Synchronize LWIP code to 2.2.0

- take code from STABLE-2_2_0_RELEASE commit on https://git.savannah.gnu.org/git/lwip.git
- remove lwip/contrib directory
- do required changes due to upgrade in
  * CMakeLists.txt
  * include/lwip/arch/cc.h
  * include/lwip/arch/sys_arch.h
  * include/lwip/lwipopts.h
  * include/tcpip.h
  * ip/CMakeLists.txt
  * ip/lwip_glue/lwip_glue.h
  * ip/lwip_glue/tcp.c
  * ip/transport/tcp/if.c
  * tcpip/icmp.c

CORE-13098
This commit is contained in:
Hervé Poussineau 2024-04-20 20:26:04 +02:00
parent bc94df7288
commit d6eebaa47a
449 changed files with 132870 additions and 38788 deletions

View file

@ -1,14 +1,20 @@
#include "tcp_helper.h"
#include "lwip/tcp_impl.h"
#include "lwip/priv/tcp_priv.h"
#include "lwip/stats.h"
#include "lwip/pbuf.h"
#include "lwip/inet.h"
#include "lwip/inet_chksum.h"
#include "lwip/ip_addr.h"
#if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
#error "This tests needs TCP- and MEMP-statistics enabled"
#endif
const ip_addr_t test_local_ip = IPADDR4_INIT_BYTES(192, 168, 1, 1);
const ip_addr_t test_remote_ip = IPADDR4_INIT_BYTES(192, 168, 1, 2);
const ip_addr_t test_netmask = IPADDR4_INIT_BYTES(255, 255, 255, 0);
/** Remove all pcbs on the given list. */
static void
tcp_remove(struct tcp_pcb* pcb_list)
@ -19,7 +25,11 @@ tcp_remove(struct tcp_pcb* pcb_list)
while(pcb != NULL) {
pcb2 = pcb;
pcb = pcb->next;
tcp_abort(pcb2);
if (pcb2->state == LISTEN) {
tcp_close(pcb2);
} else {
tcp_abort(pcb2);
}
}
}
@ -28,12 +38,13 @@ void
tcp_remove_all(void)
{
tcp_remove(tcp_listen_pcbs.pcbs);
tcp_remove(tcp_bound_pcbs);
tcp_remove(tcp_active_pcbs);
tcp_remove(tcp_tw_pcbs);
fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
fail_unless(lwip_stats.memp[MEMP_TCP_PCB_LISTEN].used == 0);
fail_unless(lwip_stats.memp[MEMP_TCP_SEG].used == 0);
fail_unless(lwip_stats.memp[MEMP_PBUF_POOL].used == 0);
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 0);
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_SEG) == 0);
fail_unless(MEMP_STATS_GET(used, MEMP_PBUF_POOL) == 0);
}
/** Create a TCP segment usable for passing to tcp_input */
@ -46,6 +57,7 @@ tcp_create_segment_wnd(ip_addr_t* src_ip, ip_addr_t* dst_ip,
struct ip_hdr* iphdr;
struct tcp_hdr* tcphdr;
u16_t pbuf_len = (u16_t)(sizeof(struct ip_hdr) + sizeof(struct tcp_hdr) + data_len);
LWIP_ASSERT("data_len too big", data_len <= 0xFFFF);
p = pbuf_alloc(PBUF_RAW, pbuf_len, PBUF_POOL);
EXPECT_RETNULL(p != NULL);
@ -60,10 +72,10 @@ tcp_create_segment_wnd(ip_addr_t* src_ip, ip_addr_t* dst_ip,
memset(q->payload, 0, q->len);
}
iphdr = p->payload;
iphdr = (struct ip_hdr*)p->payload;
/* fill IP header */
iphdr->dest.addr = dst_ip->addr;
iphdr->src.addr = src_ip->addr;
iphdr->dest.addr = ip_2_ip4(dst_ip)->addr;
iphdr->src.addr = ip_2_ip4(src_ip)->addr;
IPH_VHL_SET(iphdr, 4, IP_HLEN / 4);
IPH_TOS_SET(iphdr, 0);
IPH_LEN_SET(iphdr, htons(p->tot_len));
@ -72,7 +84,7 @@ tcp_create_segment_wnd(ip_addr_t* src_ip, ip_addr_t* dst_ip,
/* let p point to TCP header */
pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
tcphdr = p->payload;
tcphdr = (struct tcp_hdr*)p->payload;
tcphdr->src = htons(src_port);
tcphdr->dest = htons(dst_port);
tcphdr->seqno = htonl(seqno);
@ -85,15 +97,15 @@ tcp_create_segment_wnd(ip_addr_t* src_ip, ip_addr_t* dst_ip,
/* let p point to TCP data */
pbuf_header(p, -(s16_t)sizeof(struct tcp_hdr));
/* copy data */
pbuf_take(p, data, data_len);
pbuf_take(p, data, (u16_t)data_len);
/* let p point to TCP header again */
pbuf_header(p, sizeof(struct tcp_hdr));
}
/* calculate checksum */
tcphdr->chksum = inet_chksum_pseudo(p, src_ip, dst_ip,
IP_PROTO_TCP, p->tot_len);
tcphdr->chksum = ip_chksum_pseudo(p,
IP_PROTO_TCP, p->tot_len, src_ip, dst_ip);
pbuf_header(p, sizeof(struct ip_hdr));
@ -136,27 +148,36 @@ struct pbuf* tcp_create_rx_segment_wnd(struct tcp_pcb* pcb, void* data, size_t d
/** Safely bring a tcp_pcb into the requested state */
void
tcp_set_state(struct tcp_pcb* pcb, enum tcp_state state, ip_addr_t* local_ip,
ip_addr_t* remote_ip, u16_t local_port, u16_t remote_port)
tcp_set_state(struct tcp_pcb* pcb, enum tcp_state state, const ip_addr_t* local_ip,
const ip_addr_t* remote_ip, u16_t local_port, u16_t remote_port)
{
u32_t iss;
/* @todo: are these all states? */
/* @todo: remove from previous list */
pcb->state = state;
iss = tcp_next_iss(pcb);
pcb->snd_wl2 = iss;
pcb->snd_nxt = iss;
pcb->lastack = iss;
pcb->snd_lbb = iss;
if (state == ESTABLISHED) {
TCP_REG(&tcp_active_pcbs, pcb);
pcb->local_ip.addr = local_ip->addr;
ip_addr_copy(pcb->local_ip, *local_ip);
pcb->local_port = local_port;
pcb->remote_ip.addr = remote_ip->addr;
ip_addr_copy(pcb->remote_ip, *remote_ip);
pcb->remote_port = remote_port;
} else if(state == LISTEN) {
TCP_REG(&tcp_listen_pcbs.pcbs, pcb);
pcb->local_ip.addr = local_ip->addr;
ip_addr_copy(pcb->local_ip, *local_ip);
pcb->local_port = local_port;
} else if(state == TIME_WAIT) {
TCP_REG(&tcp_tw_pcbs, pcb);
pcb->local_ip.addr = local_ip->addr;
ip_addr_copy(pcb->local_ip, *local_ip);
pcb->local_port = local_port;
pcb->remote_ip.addr = remote_ip->addr;
ip_addr_copy(pcb->remote_ip, *remote_ip);
pcb->remote_port = remote_port;
} else {
fail();
@ -166,7 +187,7 @@ tcp_set_state(struct tcp_pcb* pcb, enum tcp_state state, ip_addr_t* local_ip,
void
test_tcp_counters_err(void* arg, err_t err)
{
struct test_tcp_counters* counters = arg;
struct test_tcp_counters* counters = (struct test_tcp_counters*)arg;
EXPECT_RET(arg != NULL);
counters->err_calls++;
counters->last_err = err;
@ -184,7 +205,7 @@ test_tcp_counters_check_rxdata(struct test_tcp_counters* counters, struct pbuf*
EXPECT_RET(counters->recved_bytes + p->tot_len <= counters->expected_data_len);
received = counters->recved_bytes;
for(q = p; q != NULL; q = q->next) {
char *data = q->payload;
char *data = (char*)q->payload;
for(i = 0; i < q->len; i++) {
EXPECT_RET(data[i] == counters->expected_data[received]);
received++;
@ -196,7 +217,7 @@ test_tcp_counters_check_rxdata(struct test_tcp_counters* counters, struct pbuf*
err_t
test_tcp_counters_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err)
{
struct test_tcp_counters* counters = arg;
struct test_tcp_counters* counters = (struct test_tcp_counters*)arg;
EXPECT_RETX(arg != NULL, ERR_OK);
EXPECT_RETX(pcb != NULL, ERR_OK);
EXPECT_RETX(err == ERR_OK, ERR_OK);
@ -238,52 +259,62 @@ test_tcp_new_counters_pcb(struct test_tcp_counters* counters)
void test_tcp_input(struct pbuf *p, struct netif *inp)
{
struct ip_hdr *iphdr = (struct ip_hdr*)p->payload;
ip_addr_copy(current_iphdr_dest, iphdr->dest);
ip_addr_copy(current_iphdr_src, iphdr->src);
current_netif = inp;
current_header = iphdr;
/* these lines are a hack, don't use them as an example :-) */
ip_addr_copy_from_ip4(*ip_current_dest_addr(), iphdr->dest);
ip_addr_copy_from_ip4(*ip_current_src_addr(), iphdr->src);
ip_current_netif() = inp;
ip_data.current_ip4_header = iphdr;
ip_data.current_input_netif = inp;
/* since adding IPv6, p->payload must point to tcp header, not ip header */
pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
tcp_input(p, inp);
current_iphdr_dest.addr = 0;
current_iphdr_src.addr = 0;
current_netif = NULL;
current_header = NULL;
ip_addr_set_zero(ip_current_dest_addr());
ip_addr_set_zero(ip_current_src_addr());
ip_current_netif() = NULL;
ip_data.current_ip4_header = NULL;
}
static err_t test_tcp_netif_output(struct netif *netif, struct pbuf *p,
ip_addr_t *ipaddr)
const ip4_addr_t *ipaddr)
{
struct test_tcp_txcounters *txcounters = (struct test_tcp_txcounters*)netif->state;
LWIP_UNUSED_ARG(ipaddr);
txcounters->num_tx_calls++;
txcounters->num_tx_bytes += p->tot_len;
if (txcounters->copy_tx_packets) {
struct pbuf *p_copy = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
err_t err;
EXPECT(p_copy != NULL);
err = pbuf_copy(p_copy, p);
EXPECT(err == ERR_OK);
if (txcounters->tx_packets == NULL) {
txcounters->tx_packets = p_copy;
} else {
pbuf_cat(txcounters->tx_packets, p_copy);
if (txcounters != NULL)
{
txcounters->num_tx_calls++;
txcounters->num_tx_bytes += p->tot_len;
if (txcounters->copy_tx_packets) {
struct pbuf *p_copy = pbuf_alloc(PBUF_LINK, p->tot_len, PBUF_RAM);
err_t err;
EXPECT(p_copy != NULL);
err = pbuf_copy(p_copy, p);
EXPECT(err == ERR_OK);
if (txcounters->tx_packets == NULL) {
txcounters->tx_packets = p_copy;
} else {
pbuf_cat(txcounters->tx_packets, p_copy);
}
}
}
return ERR_OK;
}
void test_tcp_init_netif(struct netif *netif, struct test_tcp_txcounters *txcounters,
ip_addr_t *ip_addr, ip_addr_t *netmask)
const ip_addr_t *ip_addr, const ip_addr_t *netmask)
{
struct netif *n;
memset(netif, 0, sizeof(struct netif));
memset(txcounters, 0, sizeof(struct test_tcp_txcounters));
if (txcounters != NULL) {
memset(txcounters, 0, sizeof(struct test_tcp_txcounters));
netif->state = txcounters;
}
netif->output = test_tcp_netif_output;
netif->state = txcounters;
netif->flags |= NETIF_FLAG_UP;
ip_addr_copy(netif->netmask, *netmask);
ip_addr_copy(netif->ip_addr, *ip_addr);
netif->flags |= NETIF_FLAG_UP | NETIF_FLAG_LINK_UP;
ip_addr_copy_from_ip4(netif->netmask, *ip_2_ip4(netmask));
ip_addr_copy_from_ip4(netif->ip_addr, *ip_2_ip4(ip_addr));
for (n = netif_list; n != NULL; n = n->next) {
if (n == netif) {
return;

View file

@ -1,5 +1,5 @@
#ifndef __TCP_HELPER_H__
#define __TCP_HELPER_H__
#ifndef LWIP_HDR_TCP_HELPER_H
#define LWIP_HDR_TCP_HELPER_H
#include "../lwip_check.h"
#include "lwip/arch.h"
@ -26,6 +26,12 @@ struct test_tcp_txcounters {
struct pbuf *tx_packets;
};
extern const ip_addr_t test_local_ip;
extern const ip_addr_t test_remote_ip;
extern const ip_addr_t test_netmask;
#define TEST_REMOTE_PORT 0x100
#define TEST_LOCAL_PORT 0x101
/* Helper functions */
void tcp_remove_all(void);
@ -36,8 +42,8 @@ struct pbuf* tcp_create_rx_segment(struct tcp_pcb* pcb, void* data, size_t data_
u32_t seqno_offset, u32_t ackno_offset, u8_t headerflags);
struct pbuf* tcp_create_rx_segment_wnd(struct tcp_pcb* pcb, void* data, size_t data_len,
u32_t seqno_offset, u32_t ackno_offset, u8_t headerflags, u16_t wnd);
void tcp_set_state(struct tcp_pcb* pcb, enum tcp_state state, ip_addr_t* local_ip,
ip_addr_t* remote_ip, u16_t local_port, u16_t remote_port);
void tcp_set_state(struct tcp_pcb* pcb, enum tcp_state state, const ip_addr_t* local_ip,
const ip_addr_t* remote_ip, u16_t local_port, u16_t remote_port);
void test_tcp_counters_err(void* arg, err_t err);
err_t test_tcp_counters_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err);
@ -46,7 +52,7 @@ struct tcp_pcb* test_tcp_new_counters_pcb(struct test_tcp_counters* counters);
void test_tcp_input(struct pbuf *p, struct netif *inp);
void test_tcp_init_netif(struct netif *netif, struct test_tcp_txcounters *txcounters,
ip_addr_t *ip_addr, ip_addr_t *netmask);
const ip_addr_t *ip_addr, const ip_addr_t *netmask);
#endif

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
#ifndef __TEST_TCP_H__
#define __TEST_TCP_H__
#ifndef LWIP_HDR_TEST_TCP_H
#define LWIP_HDR_TEST_TCP_H
#include "../lwip_check.h"

View file

@ -1,6 +1,6 @@
#include "test_tcp_oos.h"
#include "lwip/tcp_impl.h"
#include "lwip/priv/tcp_priv.h"
#include "lwip/stats.h"
#include "tcp_helper.h"
@ -36,6 +36,7 @@ static int tcp_oos_count(struct tcp_pcb* pcb)
return num;
}
#if TCP_OOSEQ_MAX_PBUFS && (TCP_OOSEQ_MAX_PBUFS < ((TCP_WND / TCP_MSS) + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN))
/** Get the numbers of pbufs on the ooseq list */
static int tcp_oos_pbuf_count(struct tcp_pcb* pcb)
{
@ -47,6 +48,7 @@ static int tcp_oos_pbuf_count(struct tcp_pcb* pcb)
}
return num;
}
#endif
/** Get the seqno of a segment (by index) on the ooseq list
*
@ -116,17 +118,30 @@ tcp_oos_tcplen(struct tcp_pcb* pcb)
}
/* Setup/teardown functions */
static struct netif *old_netif_list;
static struct netif *old_netif_default;
static void
tcp_oos_setup(void)
{
old_netif_list = netif_list;
old_netif_default = netif_default;
netif_list = NULL;
netif_default = NULL;
tcp_remove_all();
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void
tcp_oos_teardown(void)
{
netif_list = NULL;
netif_default = NULL;
tcp_remove_all();
/* restore netif_list for next tests (e.g. loopif) */
netif_list = old_netif_list;
netif_default = old_netif_default;
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
@ -146,16 +161,12 @@ START_TEST(test_tcp_recv_ooseq_FIN_OOSEQ)
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16};
ip_addr_t remote_ip, local_ip;
u16_t data_len;
u16_t remote_port = 0x100, local_port = 0x101;
struct netif netif;
LWIP_UNUSED_ARG(_i);
/* initialize local vars */
memset(&netif, 0, sizeof(netif));
IP4_ADDR(&local_ip, 192, 168, 1, 1);
IP4_ADDR(&remote_ip, 192, 168, 1, 2);
test_tcp_init_netif(&netif, NULL, &test_local_ip, &test_netmask);
data_len = sizeof(data);
/* initialize counter struct */
memset(&counters, 0, sizeof(counters));
@ -165,7 +176,7 @@ START_TEST(test_tcp_recv_ooseq_FIN_OOSEQ)
/* create and initialize the pcb */
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
/* create segments */
/* pinseq is sent as last segment! */
@ -266,9 +277,9 @@ START_TEST(test_tcp_recv_ooseq_FIN_OOSEQ)
}
/* make sure the pcb is freed */
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
tcp_abort(pcb);
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
@ -286,16 +297,12 @@ START_TEST(test_tcp_recv_ooseq_FIN_INSEQ)
5, 6, 7, 8,
9, 10, 11, 12,
13, 14, 15, 16};
ip_addr_t remote_ip, local_ip;
u16_t data_len;
u16_t remote_port = 0x100, local_port = 0x101;
struct netif netif;
LWIP_UNUSED_ARG(_i);
/* initialize local vars */
memset(&netif, 0, sizeof(netif));
IP4_ADDR(&local_ip, 192, 168, 1, 1);
IP4_ADDR(&remote_ip, 192, 168, 1, 2);
test_tcp_init_netif(&netif, NULL, &test_local_ip, &test_netmask);
data_len = sizeof(data);
/* initialize counter struct */
memset(&counters, 0, sizeof(counters));
@ -305,7 +312,7 @@ START_TEST(test_tcp_recv_ooseq_FIN_INSEQ)
/* create and initialize the pcb */
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
/* create segments */
/* p1: 7 bytes - 2 before FIN */
@ -440,13 +447,13 @@ START_TEST(test_tcp_recv_ooseq_FIN_INSEQ)
}
/* make sure the pcb is freed */
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
tcp_abort(pcb);
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
static char data_full_wnd[TCP_WND];
static char data_full_wnd[TCP_WND + TCP_MSS];
/** create multiple segments and pass them to tcp_input with the first segment missing
* to simulate overruning the rxwin with ooseq queueing enabled */
@ -457,20 +464,16 @@ START_TEST(test_tcp_recv_ooseq_overrun_rxwin)
struct test_tcp_counters counters;
struct tcp_pcb* pcb;
struct pbuf *pinseq, *p_ovr;
ip_addr_t remote_ip, local_ip;
u16_t remote_port = 0x100, local_port = 0x101;
struct netif netif;
int datalen = 0;
int datalen2;
for(i = 0; i < sizeof(data_full_wnd); i++) {
for(i = 0; i < (int)sizeof(data_full_wnd); i++) {
data_full_wnd[i] = (char)i;
}
/* initialize local vars */
memset(&netif, 0, sizeof(netif));
IP4_ADDR(&local_ip, 192, 168, 1, 1);
IP4_ADDR(&remote_ip, 192, 168, 1, 2);
test_tcp_init_netif(&netif, NULL, &test_local_ip, &test_netmask);
/* initialize counter struct */
memset(&counters, 0, sizeof(counters));
counters.expected_data_len = TCP_WND;
@ -479,7 +482,7 @@ START_TEST(test_tcp_recv_ooseq_overrun_rxwin)
/* create and initialize the pcb */
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
pcb->rcv_nxt = 0x8000;
/* create segments */
@ -532,9 +535,96 @@ START_TEST(test_tcp_recv_ooseq_overrun_rxwin)
EXPECT(pcb->ooseq == NULL);
/* make sure the pcb is freed */
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
tcp_abort(pcb);
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
#endif /* !TCP_OOSEQ_MAX_BYTES && !TCP_OOSEQ_MAX_PBUFS */
LWIP_UNUSED_ARG(_i);
}
END_TEST
/** similar to above test, except seqno starts near the max rxwin */
START_TEST(test_tcp_recv_ooseq_overrun_rxwin_edge)
{
#if !TCP_OOSEQ_MAX_BYTES && !TCP_OOSEQ_MAX_PBUFS
int i, k;
struct test_tcp_counters counters;
struct tcp_pcb* pcb;
struct pbuf *pinseq, *p_ovr;
struct netif netif;
int datalen = 0;
int datalen2;
for(i = 0; i < (int)sizeof(data_full_wnd); i++) {
data_full_wnd[i] = (char)i;
}
/* initialize local vars */
test_tcp_init_netif(&netif, NULL, &test_local_ip, &test_netmask);
/* initialize counter struct */
memset(&counters, 0, sizeof(counters));
counters.expected_data_len = TCP_WND;
counters.expected_data = data_full_wnd;
/* create and initialize the pcb */
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
pcb->rcv_nxt = 0xffffffff - (TCP_WND / 2);
/* create segments */
/* pinseq is sent as last segment! */
pinseq = tcp_create_rx_segment(pcb, &data_full_wnd[0], TCP_MSS, 0, 0, TCP_ACK);
for(i = TCP_MSS, k = 0; i < TCP_WND; i += TCP_MSS, k++) {
int count, expected_datalen;
struct pbuf *p = tcp_create_rx_segment(pcb, &data_full_wnd[TCP_MSS*(k+1)],
TCP_MSS, TCP_MSS*(k+1), 0, TCP_ACK);
EXPECT_RET(p != NULL);
/* pass the segment to tcp_input */
test_tcp_input(p, &netif);
/* check if counters are as expected */
EXPECT(counters.close_calls == 0);
EXPECT(counters.recv_calls == 0);
EXPECT(counters.recved_bytes == 0);
EXPECT(counters.err_calls == 0);
/* check ooseq queue */
count = tcp_oos_count(pcb);
EXPECT_OOSEQ(count == k+1);
datalen = tcp_oos_tcplen(pcb);
if (i + TCP_MSS < TCP_WND) {
expected_datalen = (k+1)*TCP_MSS;
} else {
expected_datalen = TCP_WND - TCP_MSS;
}
if (datalen != expected_datalen) {
EXPECT_OOSEQ(datalen == expected_datalen);
}
}
/* pass in one more segment, cleary overrunning the rxwin */
p_ovr = tcp_create_rx_segment(pcb, &data_full_wnd[TCP_MSS*(k+1)], TCP_MSS, TCP_MSS*(k+1), 0, TCP_ACK);
EXPECT_RET(p_ovr != NULL);
/* pass the segment to tcp_input */
test_tcp_input(p_ovr, &netif);
/* check if counters are as expected */
EXPECT(counters.close_calls == 0);
EXPECT(counters.recv_calls == 0);
EXPECT(counters.recved_bytes == 0);
EXPECT(counters.err_calls == 0);
/* check ooseq queue */
EXPECT_OOSEQ(tcp_oos_count(pcb) == k);
datalen2 = tcp_oos_tcplen(pcb);
EXPECT_OOSEQ(datalen == datalen2);
/* now pass inseq */
test_tcp_input(pinseq, &netif);
EXPECT(pcb->ooseq == NULL);
/* make sure the pcb is freed */
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
tcp_abort(pcb);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
#endif /* !TCP_OOSEQ_MAX_BYTES && !TCP_OOSEQ_MAX_PBUFS */
LWIP_UNUSED_ARG(_i);
}
@ -542,13 +632,11 @@ END_TEST
START_TEST(test_tcp_recv_ooseq_max_bytes)
{
#if TCP_OOSEQ_MAX_BYTES && (TCP_OOSEQ_MAX_BYTES < (TCP_WND + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN))
#if TCP_OOSEQ_MAX_BYTES && (TCP_OOSEQ_MAX_BYTES < (TCP_WND + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN))
int i, k;
struct test_tcp_counters counters;
struct tcp_pcb* pcb;
struct pbuf *p_ovr;
ip_addr_t remote_ip, local_ip;
u16_t remote_port = 0x100, local_port = 0x101;
struct netif netif;
int datalen = 0;
int datalen2;
@ -558,9 +646,7 @@ START_TEST(test_tcp_recv_ooseq_max_bytes)
}
/* initialize local vars */
memset(&netif, 0, sizeof(netif));
IP4_ADDR(&local_ip, 192, 168, 1, 1);
IP4_ADDR(&remote_ip, 192, 168, 1, 2);
test_tcp_init_netif(&netif, NULL, &test_local_ip, &test_netmask);
/* initialize counter struct */
memset(&counters, 0, sizeof(counters));
counters.expected_data_len = TCP_WND;
@ -569,7 +655,7 @@ START_TEST(test_tcp_recv_ooseq_max_bytes)
/* create and initialize the pcb */
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
pcb->rcv_nxt = 0x8000;
/* don't 'recv' the first segment (1 byte) so that all other segments will be ooseq */
@ -611,23 +697,21 @@ START_TEST(test_tcp_recv_ooseq_max_bytes)
EXPECT_OOSEQ(datalen2 == ((i-1) * TCP_MSS));
/* make sure the pcb is freed */
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
tcp_abort(pcb);
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
#endif /* TCP_OOSEQ_MAX_BYTES && (TCP_OOSEQ_MAX_BYTES < (TCP_WND + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN)) */
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
#endif /* TCP_OOSEQ_MAX_BYTES && (TCP_OOSEQ_MAX_BYTES < (TCP_WND + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN)) */
LWIP_UNUSED_ARG(_i);
}
END_TEST
START_TEST(test_tcp_recv_ooseq_max_pbufs)
{
#if TCP_OOSEQ_MAX_PBUFS && (TCP_OOSEQ_MAX_PBUFS < ((TCP_WND / TCP_MSS) + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN))
#if TCP_OOSEQ_MAX_PBUFS && (TCP_OOSEQ_MAX_PBUFS < ((TCP_WND / TCP_MSS) + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN))
int i;
struct test_tcp_counters counters;
struct tcp_pcb* pcb;
struct pbuf *p_ovr;
ip_addr_t remote_ip, local_ip;
u16_t remote_port = 0x100, local_port = 0x101;
struct netif netif;
int datalen = 0;
int datalen2;
@ -637,9 +721,7 @@ START_TEST(test_tcp_recv_ooseq_max_pbufs)
}
/* initialize local vars */
memset(&netif, 0, sizeof(netif));
IP4_ADDR(&local_ip, 192, 168, 1, 1);
IP4_ADDR(&remote_ip, 192, 168, 1, 2);
test_tcp_init_netif(&netif, NULL, &test_local_ip, &test_netmask);
/* initialize counter struct */
memset(&counters, 0, sizeof(counters));
counters.expected_data_len = TCP_WND;
@ -648,7 +730,7 @@ START_TEST(test_tcp_recv_ooseq_max_pbufs)
/* create and initialize the pcb */
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
pcb->rcv_nxt = 0x8000;
/* don't 'recv' the first segment (1 byte) so that all other segments will be ooseq */
@ -690,10 +772,10 @@ START_TEST(test_tcp_recv_ooseq_max_pbufs)
EXPECT_OOSEQ(datalen2 == (i-1));
/* make sure the pcb is freed */
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
tcp_abort(pcb);
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
#endif /* TCP_OOSEQ_MAX_PBUFS && (TCP_OOSEQ_MAX_BYTES < (TCP_WND + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN)) */
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
#endif /* TCP_OOSEQ_MAX_PBUFS && (TCP_OOSEQ_MAX_BYTES < (TCP_WND + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN)) */
LWIP_UNUSED_ARG(_i);
}
END_TEST
@ -727,21 +809,16 @@ static void test_tcp_recv_ooseq_double_FINs(int delay_packet)
struct test_tcp_counters counters;
struct tcp_pcb* pcb;
struct pbuf *p_normal_fin, *p_data_after_fin, *p, *p_2nd_fin_ooseq;
ip_addr_t remote_ip, local_ip;
u16_t remote_port = 0x100, local_port = 0x101;
struct netif netif;
u32_t exp_rx_calls = 0, exp_rx_bytes = 0, exp_close_calls = 0, exp_oos_pbufs = 0, exp_oos_tcplen = 0;
int first_dropped = 0xff;
int last_dropped = 0;
for(i = 0; i < sizeof(data_full_wnd); i++) {
for(i = 0; i < (int)sizeof(data_full_wnd); i++) {
data_full_wnd[i] = (char)i;
}
/* initialize local vars */
memset(&netif, 0, sizeof(netif));
IP4_ADDR(&local_ip, 192, 168, 1, 1);
IP4_ADDR(&remote_ip, 192, 168, 1, 2);
test_tcp_init_netif(&netif, NULL, &test_local_ip, &test_netmask);
/* initialize counter struct */
memset(&counters, 0, sizeof(counters));
counters.expected_data_len = TCP_WND;
@ -750,7 +827,7 @@ static void test_tcp_recv_ooseq_double_FINs(int delay_packet)
/* create and initialize the pcb */
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
pcb->rcv_nxt = 0x8000;
/* create segments */
@ -763,7 +840,6 @@ static void test_tcp_recv_ooseq_double_FINs(int delay_packet)
if(delay_packet & 1) {
/* drop normal data */
first_dropped = 1;
last_dropped = 1;
} else {
/* send normal data */
test_tcp_input(p, &netif);
@ -778,7 +854,6 @@ static void test_tcp_recv_ooseq_double_FINs(int delay_packet)
if(first_dropped > 2) {
first_dropped = 2;
}
last_dropped = 2;
} else {
/* send FIN */
test_tcp_input(p_normal_fin, &netif);
@ -799,7 +874,6 @@ static void test_tcp_recv_ooseq_double_FINs(int delay_packet)
if(first_dropped > 3) {
first_dropped = 3;
}
last_dropped = 3;
} else {
/* send data-after-FIN */
test_tcp_input(p_data_after_fin, &netif);
@ -822,7 +896,6 @@ static void test_tcp_recv_ooseq_double_FINs(int delay_packet)
if(first_dropped > 4) {
first_dropped = 4;
}
last_dropped = 4;
} else {
/* send 2nd-FIN */
test_tcp_input(p_2nd_fin_ooseq, &netif);
@ -881,9 +954,9 @@ static void test_tcp_recv_ooseq_double_FINs(int delay_packet)
EXPECT(pcb->ooseq == NULL);
/* make sure the pcb is freed */
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
tcp_abort(pcb);
EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
/** create multiple segments and pass them to tcp_input with the first segment missing
@ -917,28 +990,29 @@ FIN_TEST(test_tcp_recv_ooseq_double_FIN_15, 15)
Suite *
tcp_oos_suite(void)
{
TFun tests[] = {
test_tcp_recv_ooseq_FIN_OOSEQ,
test_tcp_recv_ooseq_FIN_INSEQ,
test_tcp_recv_ooseq_overrun_rxwin,
test_tcp_recv_ooseq_max_bytes,
test_tcp_recv_ooseq_max_pbufs,
test_tcp_recv_ooseq_double_FIN_0,
test_tcp_recv_ooseq_double_FIN_1,
test_tcp_recv_ooseq_double_FIN_2,
test_tcp_recv_ooseq_double_FIN_3,
test_tcp_recv_ooseq_double_FIN_4,
test_tcp_recv_ooseq_double_FIN_5,
test_tcp_recv_ooseq_double_FIN_6,
test_tcp_recv_ooseq_double_FIN_7,
test_tcp_recv_ooseq_double_FIN_8,
test_tcp_recv_ooseq_double_FIN_9,
test_tcp_recv_ooseq_double_FIN_10,
test_tcp_recv_ooseq_double_FIN_11,
test_tcp_recv_ooseq_double_FIN_12,
test_tcp_recv_ooseq_double_FIN_13,
test_tcp_recv_ooseq_double_FIN_14,
test_tcp_recv_ooseq_double_FIN_15
testfunc tests[] = {
TESTFUNC(test_tcp_recv_ooseq_FIN_OOSEQ),
TESTFUNC(test_tcp_recv_ooseq_FIN_INSEQ),
TESTFUNC(test_tcp_recv_ooseq_overrun_rxwin),
TESTFUNC(test_tcp_recv_ooseq_overrun_rxwin_edge),
TESTFUNC(test_tcp_recv_ooseq_max_bytes),
TESTFUNC(test_tcp_recv_ooseq_max_pbufs),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_0),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_1),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_2),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_3),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_4),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_5),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_6),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_7),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_8),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_9),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_10),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_11),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_12),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_13),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_14),
TESTFUNC(test_tcp_recv_ooseq_double_FIN_15)
};
return create_suite("TCP_OOS", tests, sizeof(tests)/sizeof(TFun), tcp_oos_setup, tcp_oos_teardown);
return create_suite("TCP_OOS", tests, sizeof(tests)/sizeof(testfunc), tcp_oos_setup, tcp_oos_teardown);
}

View file

@ -1,5 +1,5 @@
#ifndef __TEST_TCP_OOS_H__
#define __TEST_TCP_OOS_H__
#ifndef LWIP_HDR_TEST_TCP_OOS_H
#define LWIP_HDR_TEST_TCP_OOS_H
#include "../lwip_check.h"

View file

@ -0,0 +1,665 @@
#include "test_tcp_state.h"
#include "lwip/priv/tcp_priv.h"
#include "lwip/stats.h"
#include "tcp_helper.h"
#include "lwip/inet_chksum.h"
#ifdef _MSC_VER
#pragma warning(disable: 4307) /* we explicitly wrap around TCP seqnos */
#endif
#if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
#error "This tests needs TCP- and MEMP-statistics enabled"
#endif
static struct netif test_netif = {0};
static struct test_tcp_txcounters test_txcounters = {0};
#define SEQNO1 (0xFFFFFF00 - TCP_MSS)
#define ISS 6510
static u8_t test_tcp_timer;
/* our own version of tcp_tmr so we can reset fast/slow timer state */
static void
test_tcp_tmr(void)
{
tcp_fasttmr();
if (++test_tcp_timer & 1) {
tcp_slowtmr();
}
}
/* Get TCP flags from packets */
static u8_t
get_tcp_flags_from_packet(struct pbuf *p, u16_t tcp_hdr_offset)
{
struct tcp_hdr tcphdr;
u16_t ret;
EXPECT_RETX(p != NULL, 0);
EXPECT_RETX(p->len >= tcp_hdr_offset + sizeof(struct tcp_hdr), 0);
ret = pbuf_copy_partial(p, &tcphdr, sizeof(struct tcp_hdr), tcp_hdr_offset);
EXPECT(ret == sizeof(struct tcp_hdr));
return TCPH_FLAGS(&tcphdr);
}
/* Create listening tcp_pcb */
static struct tcp_pcb_listen *
create_listening_pcb(u16_t local_port, struct test_tcp_counters *counters)
{
struct tcp_pcb *pcb;
struct tcp_pcb_listen *lpcb=NULL;
err_t err;
u16_t port = local_port?local_port:1234;
if (counters) {
pcb = test_tcp_new_counters_pcb(counters);
} else {
pcb = tcp_new();
}
EXPECT(pcb != NULL);
if (pcb) {
err = tcp_bind(pcb, &test_netif.ip_addr, port);
EXPECT(err == ERR_OK);
lpcb = (struct tcp_pcb_listen *)tcp_listen(pcb);
}
return lpcb;
}
/* Setup/teardown functions */
static struct netif* old_netif_list;
static struct netif* old_netif_default;
static void
tcp_state_setup(void)
{
struct tcp_pcb dummy_pcb; /* we need this for tcp_next_iss() only */
/* reset iss to default (6510) */
tcp_ticks = 0;
tcp_ticks = 0 - (tcp_next_iss(&dummy_pcb) - 6510);
tcp_next_iss(&dummy_pcb);
tcp_ticks = 0;
test_tcp_timer = 0;
old_netif_list = netif_list;
old_netif_default = netif_default;
netif_list = NULL;
netif_default = NULL;
tcp_remove_all();
test_tcp_init_netif(&test_netif, &test_txcounters, &test_local_ip, &test_netmask);
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
static void
tcp_state_teardown(void)
{
netif_list = NULL;
netif_default = NULL;
tcp_remove_all();
/* restore netif_list for next tests (e.g. loopif) */
netif_list = old_netif_list;
netif_default = old_netif_default;
lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
}
/* helper functions */
static void
test_rst_generation_with_incoming_packet(struct pbuf *p,
struct netif *netif, struct test_tcp_txcounters *tx_counters)
{
u16_t tcp_flags;
EXPECT_RET(p != NULL);
memset(tx_counters, 0, sizeof(struct test_tcp_txcounters));
/* pass the segment to tcp_input */
tx_counters->copy_tx_packets = 1;
test_tcp_input(p, netif);
tx_counters->copy_tx_packets = 0;
/* check if packets are as expected */
EXPECT(tx_counters->tx_packets != NULL);
if (tx_counters->tx_packets) {
tcp_flags = get_tcp_flags_from_packet(tx_counters->tx_packets, 20);
EXPECT(tcp_flags & TCP_RST);
pbuf_free(tx_counters->tx_packets);
tx_counters->tx_packets = NULL;
}
}
/* Test functions */
/* Call tcp_new() and test memp stats (max number) */
START_TEST(test_tcp_new_max_num)
{
struct tcp_pcb* pcb[MEMP_NUM_TCP_PCB + 1];
int i;
LWIP_UNUSED_ARG(_i);
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
for(i = 0;i < MEMP_NUM_TCP_PCB; i++) {
pcb[i] = tcp_new();
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == (i + 1));
}
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == MEMP_NUM_TCP_PCB);
/* Trying to remove the oldest pcb in TIME_WAIT,LAST_ACK,CLOSING state when pcb full */
pcb[MEMP_NUM_TCP_PCB] = tcp_new();
fail_unless(pcb[MEMP_NUM_TCP_PCB] == NULL);
tcp_set_state(pcb[0], TIME_WAIT, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
pcb[MEMP_NUM_TCP_PCB] = tcp_new();
fail_unless(pcb[MEMP_NUM_TCP_PCB] != NULL);
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == MEMP_NUM_TCP_PCB);
for (i = 1; i <= MEMP_NUM_TCP_PCB; i++)
{
tcp_abort(pcb[i]);
}
fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
/* 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)
{
struct tcp_pcb* pcb;
struct tcp_pcb* pcb_list[MEMP_NUM_TCP_PCB + 1];
int i;
LWIP_UNUSED_ARG(_i);
/* create a pcb in TIME_WAIT state */
pcb = tcp_new();
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, TIME_WAIT, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
EXPECT_RET(pcb->state == TIME_WAIT);
/* Create max number pcbs */
for(i = 0;i < MEMP_NUM_TCP_PCB-1; i++) {
pcb_list[i] = tcp_new();
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == (i + 2));
}
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == MEMP_NUM_TCP_PCB);
/* Create one more pcb, and expect that the pcb in the TIME_WAIT state is deleted */
pcb_list[MEMP_NUM_TCP_PCB-1] = tcp_new();
EXPECT_RET(pcb_list[MEMP_NUM_TCP_PCB-1] != NULL);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == MEMP_NUM_TCP_PCB);
for (i = 0; i <= MEMP_NUM_TCP_PCB-1; i++)
{
tcp_abort(pcb_list[i]);
}
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
/* Call tcp_connect to check active open */
START_TEST(test_tcp_connect_active_open)
{
struct test_tcp_counters counters;
struct tcp_pcb *pcb;
struct pbuf *p;
err_t err;
u16_t test_port = 1234;
u32_t seqno = 0;
LWIP_UNUSED_ARG(_i);
/* create and initialize the pcb */
tcp_ticks = SEQNO1 - ISS;
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
/* Get seqno from SYN packet */
test_txcounters.copy_tx_packets = 1;
err = tcp_connect(pcb, &test_remote_ip, test_port, NULL);
test_txcounters.copy_tx_packets = 0;
EXPECT(err == ERR_OK);
EXPECT(pcb->state == SYN_SENT);
EXPECT(test_txcounters.num_tx_calls == 1);
EXPECT_RET(test_txcounters.tx_packets != NULL);
if (test_txcounters.tx_packets != NULL) {
struct tcp_hdr tcphdr;
u16_t ret;
ret = pbuf_copy_partial(test_txcounters.tx_packets, &tcphdr, 20, 20);
EXPECT(ret == 20);
EXPECT(TCPH_FLAGS(&tcphdr) & TCP_SYN);
pbuf_free(test_txcounters.tx_packets);
test_txcounters.tx_packets = NULL;
seqno = lwip_htonl(tcphdr.seqno);
EXPECT(seqno == pcb->lastack);
}
/* check correct syn packet */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, test_port,
pcb->local_port, NULL, 0, 12345, seqno + 1, TCP_SYN|TCP_ACK);
EXPECT_RET(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT_RET(pcb->state == ESTABLISHED);
EXPECT_RET(test_txcounters.num_tx_calls == 2);
/* make sure the pcb is freed */
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
tcp_abort(pcb);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
START_TEST(test_tcp_active_close)
{
struct tcp_pcb *pcb, *pcbl;
struct test_tcp_counters counters;
struct pbuf *p;
err_t err;
u32_t i;
LWIP_UNUSED_ARG(_i);
/* create TCP in LISTEN state */
memset(&counters, 0, sizeof(counters));
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
err = tcp_bind(pcb, &test_netif.ip_addr, 1234);
EXPECT_RET(err == ERR_OK);
pcbl = tcp_listen(pcb);
EXPECT_RET(pcbl != NULL);
EXPECT_RET(pcbl->state == LISTEN);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 1);
memset(&test_txcounters, 0, sizeof(test_txcounters));
err = tcp_close(pcbl);
EXPECT_RET(err == ERR_OK);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 0);
EXPECT(test_txcounters.num_tx_calls == 0);
/* close TCP in SYN_SENT state */
memset(&counters, 0, sizeof(counters));
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
err = tcp_connect(pcb, &test_netif.gw, 1234, NULL);
EXPECT_RET(err == ERR_OK);
EXPECT_RET(pcb->state == SYN_SENT);
EXPECT(test_txcounters.num_tx_calls == 1);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
memset(&test_txcounters, 0, sizeof(test_txcounters));
err = tcp_close(pcb);
EXPECT_RET(err == ERR_OK);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
EXPECT(test_txcounters.num_tx_calls == 0);
/* close TCP in ESTABLISHED state */
memset(&counters, 0, sizeof(counters));
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
memset(&test_txcounters, 0, sizeof(test_txcounters));
err = tcp_close(pcb);
EXPECT_RET(err == ERR_OK);
EXPECT_RET(pcb->state == FIN_WAIT_1);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
/* test_tcp_tmr(); */
EXPECT(test_txcounters.num_tx_calls == 1);
/* create a segment ACK and pass it to tcp_input */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 1, TCP_ACK);
EXPECT_RET(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT_RET(pcb->state == FIN_WAIT_2);
/* create a segment FIN and pass it to tcp_input */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_FIN);
EXPECT_RET(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT_RET(pcb->state == TIME_WAIT);
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
for (i = 0; i < 2 * TCP_MSL / TCP_TMR_INTERVAL + 1; i++) {
test_tcp_tmr();
}
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
START_TEST(test_tcp_imultaneous_close)
{
struct test_tcp_counters counters;
struct tcp_pcb* pcb;
struct pbuf* p;
char data = 0x0f;
err_t err;
u32_t i;
LWIP_UNUSED_ARG(_i);
/* initialize counter struct */
memset(&counters, 0, sizeof(counters));
counters.expected_data_len = 1;
counters.expected_data = &data;
/* create and initialize the pcb */
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
err = tcp_close(pcb);
EXPECT_RET(err == ERR_OK);
EXPECT_RET(pcb->state == FIN_WAIT_1);
/* create a FIN segment */
p = tcp_create_rx_segment(pcb, &data, 0, 0, 0, TCP_FIN);
EXPECT(p != NULL);
if (p != NULL) {
test_tcp_input(p, &test_netif);
}
EXPECT_RET(pcb->state == CLOSING);
/* create an ACK segment */
p = tcp_create_rx_segment(pcb, &data, 0, 0, 1, TCP_ACK);
EXPECT(p != NULL);
if (p != NULL) {
test_tcp_input(p, &test_netif);
}
EXPECT_RET(pcb->state == TIME_WAIT);
for (i = 0; i < 2 * TCP_MSL / TCP_TMR_INTERVAL + 1; i++) {
test_tcp_tmr();
}
EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
/* RST was generated when receive any incoming segment in CLOSED state */
START_TEST(test_tcp_gen_rst_in_CLOSED)
{
struct pbuf *p;
ip_addr_t src_addr = test_remote_ip;
ip_addr_t dst_addr = test_local_ip;
LWIP_UNUSED_ARG(_i);
/* Do not create any pcb */
/* create a segment */
p = tcp_create_segment(&src_addr, &dst_addr, TEST_REMOTE_PORT,
TEST_LOCAL_PORT, NULL, 0, 12345, 54321, TCP_ACK);
EXPECT(p != NULL);
test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
EXPECT(test_txcounters.num_tx_calls == 1);
}
END_TEST
/* RST was generated when receive ACK in LISTEN state */
START_TEST(test_tcp_gen_rst_in_LISTEN)
{
struct tcp_pcb_listen *lpcb;
struct pbuf *p;
ip_addr_t src_addr = test_remote_ip;
LWIP_UNUSED_ARG(_i);
/* create a pcb in LISTEN state */
lpcb = create_listening_pcb(TEST_LOCAL_PORT, NULL);
EXPECT_RET(lpcb != NULL);
/* create a segment */
p = tcp_create_segment(&src_addr,&lpcb->local_ip, TEST_REMOTE_PORT,
lpcb->local_port, NULL, 0, 12345, 54321, TCP_ACK);
EXPECT(p != NULL);
test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
EXPECT(test_txcounters.num_tx_calls == 1);
/* the PCB still in LISTEN state */
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 1);
if (MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) != 0) {
/* can not use tcp_abort() */
tcp_close((struct tcp_pcb *)lpcb);
}
}
END_TEST
/* RST was generated when receive an SYN in TIME_WAIT state */
START_TEST(test_tcp_gen_rst_in_TIME_WAIT)
{
struct tcp_pcb *pcb;
struct pbuf *p;
LWIP_UNUSED_ARG(_i);
/* create a pcb in LISTEN state */
pcb = tcp_new();
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, TIME_WAIT, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
/* create a segment */
p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_SYN);
EXPECT(p != NULL);
test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
EXPECT(test_txcounters.num_tx_calls == 1);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
EXPECT(pcb->state == TIME_WAIT);
}
END_TEST
/* receive TCP_RST with different seqno */
START_TEST(test_tcp_process_rst_seqno)
{
struct test_tcp_counters counters;
struct tcp_pcb *pcb;
struct pbuf *p;
err_t err;
LWIP_UNUSED_ARG(_i);
/* create and initialize a pcb in SYN_SENT state */
memset(&counters, 0, sizeof(counters));
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
err = tcp_connect(pcb, &test_remote_ip, TEST_REMOTE_PORT, NULL);
EXPECT_RET(err == ERR_OK);
/* a RST segment with incorrect seqno will not be accepted */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, TEST_REMOTE_PORT,
pcb->local_port, NULL, 0, 12345, pcb->snd_nxt-10, TCP_RST);
EXPECT(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT(counters.err_calls == 0);
/* a RST segment with correct seqno will be accepted */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, TEST_REMOTE_PORT,
pcb->local_port, NULL, 0, 12345, pcb->snd_nxt, TCP_RST);
EXPECT(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT(counters.err_calls == 1);
counters.err_calls = 0;
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
/* create another pcb in ESTABLISHED state */
memset(&counters, 0, sizeof(counters));
pcb = test_tcp_new_counters_pcb(&counters);
EXPECT_RET(pcb != NULL);
tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
/* a RST segment with incorrect seqno will not be accepted */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port,
pcb->local_port, NULL, 0, pcb->rcv_nxt-10, 54321, TCP_RST);
EXPECT(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT(counters.err_calls == 0);
/* a RST segment with correct seqno will be accepted */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, TEST_REMOTE_PORT,
pcb->local_port, NULL, 0, pcb->rcv_nxt, 54321, TCP_RST);
EXPECT(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT(counters.err_calls == 1);
counters.err_calls = 0;
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
}
END_TEST
/* 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)
{
struct tcp_pcb *pcb;
struct pbuf *p;
u16_t test_port = 1234;
err_t err;
LWIP_UNUSED_ARG(_i);
/* create and initialize a pcb in listen state */
pcb = tcp_new();
EXPECT_RET(pcb != NULL);
err = tcp_connect(pcb, &test_remote_ip, test_port, NULL);
EXPECT_RET(err == ERR_OK);
/* create a SYN+ACK segment with incorrect seqno */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port,
pcb->local_port, NULL, 0, 12345, pcb->lastack-10, TCP_SYN|TCP_ACK);
EXPECT(p != NULL);
test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
/* LWIP: send RST then re-send SYN immediately */
EXPECT(test_txcounters.num_tx_calls == 2);
}
END_TEST
/* 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)
{
struct tcp_pcb *pcb;
struct pbuf *p;
u16_t test_port = 1234;
err_t err;
LWIP_UNUSED_ARG(_i);
/* create and initialize a pcb in listen state */
pcb = tcp_new();
EXPECT_RET(pcb != NULL);
err = tcp_connect(pcb, &test_remote_ip, test_port, NULL);
EXPECT_RET(err == ERR_OK);
/* create a SYN+ACK segment with incorrect seqno */
p = tcp_create_segment(&pcb->remote_ip, &pcb->local_ip, pcb->remote_port,
pcb->local_port, NULL, 0, 12345, pcb->lastack, TCP_ACK);
EXPECT(p != NULL);
test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
/* LWIP: send RST then re-send SYN immediately */
EXPECT(test_txcounters.num_tx_calls == 2);
}
END_TEST
/* RST was generated when receive an ACK with incorrect seqno in SYN_RCVD state */
START_TEST(test_tcp_gen_rst_in_SYN_RCVD)
{
struct tcp_pcb_listen *lpcb;
struct pbuf *p;
u32_t ack_seqno = 0;
ip_addr_t src_addr = test_remote_ip;
LWIP_UNUSED_ARG(_i);
/* create and initialize a pcb in listen state */
lpcb = create_listening_pcb(TEST_LOCAL_PORT, NULL);
EXPECT_RET(lpcb != NULL);
/* LISTEN -> SYN_RCVD */
p = tcp_create_segment(&src_addr, &lpcb->local_ip, TEST_REMOTE_PORT,
lpcb->local_port, NULL, 0, 1000, 54321, TCP_SYN);
EXPECT(p != NULL);
memset(&test_txcounters, 0, sizeof(struct test_tcp_txcounters));
test_tcp_input(p, &test_netif);
EXPECT(test_txcounters.num_tx_calls == 1);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
if (MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1) {
ack_seqno = tcp_active_pcbs[0].lastack;
}
/* create a ACK segment with incorrect seqno */
p = tcp_create_segment(&src_addr, &lpcb->local_ip, TEST_REMOTE_PORT,
lpcb->local_port, NULL, 0, 1001, ack_seqno+1111, TCP_ACK);
EXPECT(p != NULL);
test_rst_generation_with_incoming_packet(p, &test_netif, &test_txcounters);
EXPECT(test_txcounters.num_tx_calls == 1);
/* the active pcb still exists */
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 1);
if (MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) != 0) {
/* can not use tcp_abort() */
tcp_close((struct tcp_pcb *)lpcb);
}
}
END_TEST
/* a listen pcb returns to LISTEN from SYN_RCVD when RST received */
START_TEST(test_tcp_receive_rst_SYN_RCVD_to_LISTEN)
{
struct tcp_pcb_listen *lpcb;
struct pbuf *p;
u16_t tcp_flags;
ip_addr_t src_addr = test_remote_ip;
LWIP_UNUSED_ARG(_i);
/* create and initialize a pcb in listen state */
lpcb = create_listening_pcb(TEST_LOCAL_PORT, NULL);
EXPECT_RET(lpcb != NULL);
/* create a SYN segment */
p = tcp_create_segment(&src_addr, &lpcb->local_ip, TEST_REMOTE_PORT,
lpcb->local_port, NULL, 0, 1000, 54321, TCP_SYN);
EXPECT(p != NULL);
/* pass the segment to tcp_input */
memset(&test_txcounters, 0, sizeof(struct test_tcp_txcounters));
test_txcounters.copy_tx_packets = 1;
test_tcp_input(p, &test_netif);
test_txcounters.copy_tx_packets = 0;
/* check if packets are as expected */
EXPECT(test_txcounters.num_tx_calls == 1);
tcp_flags = get_tcp_flags_from_packet(test_txcounters.tx_packets, 20);
pbuf_free(test_txcounters.tx_packets);
test_txcounters.tx_packets = NULL;
EXPECT((tcp_flags & TCP_SYN) && (tcp_flags & TCP_ACK));
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
/* create a RST segment */
p = tcp_create_segment(&src_addr, &lpcb->local_ip, TEST_REMOTE_PORT,
lpcb->local_port, NULL, 0, 1001, 54321, TCP_RST);
EXPECT(p != NULL);
test_tcp_input(p, &test_netif);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) == 1);
if (MEMP_STATS_GET(used, MEMP_TCP_PCB_LISTEN) != 0) {
/* can not use tcp_abort() */
tcp_close((struct tcp_pcb *)lpcb);
}
}
END_TEST
/** Create the suite including all tests for this module */
Suite *
tcp_state_suite(void)
{
testfunc tests[] = {
TESTFUNC(test_tcp_new_max_num),
TESTFUNC(test_tcp_new_max_num_remove_TIME_WAIT),
TESTFUNC(test_tcp_connect_active_open),
TESTFUNC(test_tcp_active_close),
TESTFUNC(test_tcp_imultaneous_close),
TESTFUNC(test_tcp_gen_rst_in_CLOSED),
TESTFUNC(test_tcp_gen_rst_in_LISTEN),
TESTFUNC(test_tcp_gen_rst_in_TIME_WAIT),
TESTFUNC(test_tcp_process_rst_seqno),
TESTFUNC(test_tcp_gen_rst_in_SYN_SENT_ackseq),
TESTFUNC(test_tcp_gen_rst_in_SYN_SENT_non_syn_ack),
TESTFUNC(test_tcp_gen_rst_in_SYN_RCVD),
TESTFUNC(test_tcp_receive_rst_SYN_RCVD_to_LISTEN),
};
return create_suite("TCP_STATE", tests, sizeof(tests) / sizeof(testfunc), tcp_state_setup, tcp_state_teardown);
}

View file

@ -0,0 +1,8 @@
#ifndef LWIP_HDR_TEST_TCP_STATE_H
#define LWIP_HDR_TEST_TCP_STATE_H
#include "../lwip_check.h"
Suite *tcp_state_suite(void);
#endif