Skip to content

Commit a1796f4

Browse files
authored
lwIP-v2: new patch to randomize tcp source ports (#5906)
ref: d-a-v/esp82xx-nonos-linklayer#31 origin: #5902 me-no-dev/ESPAsyncTCP#108 Following the links above is instructive. To summarize: * currently and from a long time lwIP tcp client connections always uses the same tcp source port number right after boot * this port number is increased everytime a new one is needed (= new tcp client connection) (to be noted, linux has the same increasing behavior) * when connecting to the same server (right after boot), the triplet (esp-ip-address, source port, destination port) are the same, and may hit remote server list of sockets in time-wait-state (previous connection unproperly closed from the same esp). Consequently the new connection fails when it happens. * this is happening only when debugging (esp reboots often, in less time than time-wait expiration), so the nasty effect is amplified especially when bugs are being chased * efforts had been done when espressif's lwIP implementation wasn't open source, with WiFiClient::setLocalPortStart() #632 but it must be explicitely called with a different random number at every reboot. Efficient but not ideal. This PR uses espressif firmware's r_rand() everytime a new local source port is needed. A different source port number is now showed by tcpdump right after boot. Source port range and duplication is verified everytime in lwIP's src/core/tcp.c:tcp_new_port(). It is implemented as a local patch for upstream lwIP so it is valid not only with WiFiClient but also with @me-no-dev's Async libraries (they don't use WiFiClient). WiFiClient::setLocalPortStart() is still usable with the same effects as before.
1 parent 9a2ed27 commit a1796f4

File tree

8 files changed

+18
-2
lines changed

8 files changed

+18
-2
lines changed

tools/sdk/lib/liblwip2-1460-feat.a

9.71 KB
Binary file not shown.

tools/sdk/lib/liblwip2-1460.a

9.69 KB
Binary file not shown.

tools/sdk/lib/liblwip2-536-feat.a

9.71 KB
Binary file not shown.

tools/sdk/lib/liblwip2-536.a

9.69 KB
Binary file not shown.

tools/sdk/lwip2/include/gluedebug.h

+10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
#define ULWIPDEBUG 0 // 0 or 1 (trigger lwip debug)
1919
#define ULWIPASSERT 0 // 0 or 1 (trigger lwip self-check, 0 saves flash)
2020

21+
#if ARDUINO
2122
#define STRING_IN_FLASH 1 // *print("fmt is stored in flash")
23+
#else
24+
#define STRING_IN_FLASH 0 // *print("fmt is stored in flash")
25+
#endif
2226

2327
#define ROTBUFLEN_BIT 11 // (UDEBUGSTORE=1) doprint()'s buffer: 11=2048B
2428

@@ -39,7 +43,9 @@ extern "C"
3943
void (*phy_capture) (int netif_idx, const char* data, size_t len, int out, int success);
4044

4145
/////////////////////////////////////////////////////////////////////////////
46+
#if ARDUINO
4247
#include <sys/pgmspace.h>
48+
#endif
4349

4450
#if UDEBUG && UDEBUGSTORE
4551
#warning use 'doprint_allow=1' right after Serial is enabled
@@ -94,6 +100,7 @@ int doprint_minus (const char* format, ...) __attribute__ ((format (printf, 1, 2
94100
#define uprint(x...) do { (void)0; } while (0)
95101
#endif
96102

103+
#if ARDUINO
97104
#define udoassert(assertion...) \
98105
do { if ((assertion) == 0) { \
99106
static const char assrt[] ICACHE_RODATA_ATTR STORE_ATTR = #assertion " wrong@"; \
@@ -104,6 +111,9 @@ do { if ((assertion) == 0) { \
104111
os_printf_plus(assrt_line, __LINE__); \
105112
uhalt(); \
106113
} } while (0)
114+
#else
115+
#define udoassert(assertion...) do { if ((assertion) == 0) { os_printf("assert fail: " #assertion " @%s:%d\n", __FILE__, __LINE__); uhalt(); } } while (0)
116+
#endif
107117

108118
#if UNDEBUG
109119
#define uassert(assertion...) do { (void)0; } while (0)
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// generated by makefiles/make-lwip2-hash
22
#ifndef LWIP_HASH_H
33
#define LWIP_HASH_H
4-
#define LWIP_HASH_STR "STABLE-2_1_2_RELEASE/glue:1.1-2-ga501b57"
4+
#define LWIP_HASH_STR "STABLE-2_1_2_RELEASE/glue:1.1-5-g25d5e81"
55
#endif // LWIP_HASH_H

tools/sdk/lwip2/include/lwipopts.h

+6
Original file line numberDiff line numberDiff line change
@@ -3534,6 +3534,12 @@
35343534
#error LWIP_FEATURES must be defined
35353535
#endif
35363536

3537+
3538+
/**
3539+
* TCP_RANDOM_PORT: randomize port instead of simply increasing
3540+
*/
3541+
#define TCP_RANDOM_PORT 1
3542+
35373543
/*
35383544
--------------------------------------------------
35393545
------------------ SNTP options ------------------

0 commit comments

Comments
 (0)