From 53a6e019c5fa3c09b48783d4110faec5748c56d6 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 4 Nov 2022 11:50:42 +0100 Subject: [PATCH] api_msg: fix tcp_abort thread safety (2.1.2-esp) As this tcp_abort could be directly called by application thread without locking the TCP/TP core. Original commit 10197b212a95c49c733fb18ffed56cafb0d196d4 --- src/api/api_msg.c | 20 ++++++++++++++++++++ test/unit/lwipopts.h | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/src/api/api_msg.c b/src/api/api_msg.c index 792255b1..0fa08b57 100644 --- a/src/api/api_msg.c +++ b/src/api/api_msg.c @@ -851,6 +851,20 @@ netconn_free(struct netconn *conn) memp_free(MEMP_NETCONN, conn); } +#if ESP_LWIP +struct tcp_psb_msg { + struct tcpip_api_call_data call; + struct tcp_pcb *pcb; +}; + +static err_t tcp_do_abort(struct tcpip_api_call_data *msg) +{ + struct tcp_psb_msg *pcb_msg = __containerof(msg, struct tcp_psb_msg, call); + tcp_abort(pcb_msg->pcb); + return ERR_OK; +} +#endif /* ESP_LWIP */ + /** * Delete rcvmbox and acceptmbox of a netconn and free the left-over data in * these mboxes @@ -913,7 +927,13 @@ netconn_drain(struct netconn *conn) #endif /* ESP_LWIP */ netconn_drain(newconn); if (newconn->pcb.tcp != NULL) { +#if ESP_LWIP + struct tcp_psb_msg pcb_msg = { 0 }; + pcb_msg.pcb = newconn->pcb.tcp; + tcpip_api_call(tcp_do_abort, &pcb_msg.call); +#else tcp_abort(newconn->pcb.tcp); +#endif /* ESP_LWIP */ newconn->pcb.tcp = NULL; } netconn_free(newconn); diff --git a/test/unit/lwipopts.h b/test/unit/lwipopts.h index d70a9758..e15c3e88 100644 --- a/test/unit/lwipopts.h +++ b/test/unit/lwipopts.h @@ -116,6 +116,10 @@ #define ESP_DHCP_DISABLE_VENDOR_CLASS_IDENTIFIER 0 #define ESP_IP_FORWARD 1 +#if ESP_LWIP && !defined(__containerof) +#define __containerof(ptr, type, member) ((type *)(void *)((char *)ptr - offsetof(type, member))) +#endif + #ifdef IP_NAPT #define IP_NAPT_MAX 16 #undef LWIP_RAND