1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
83
84
85
86
87
88
89
90
91
92
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* ... */
/* ... */
/* ... */
#include "lwip_default_hooks.h"
#include "lwip/ip_addr.h"
#include "lwip/sys.h"
#include <string.h>
#include "esp_rom_md5.h"5 includes
#ifdef CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
#include "esp_memory_utils.h"
#endif
#ifdef CONFIG_LWIP_HOOK_TCP_ISN_DEFAULT
static u8_t input[64];
static u32_t base_time;
/* ... */
void
lwip_init_tcp_isn(u32_t boot_time, const u8_t *secret_16_bytes)
{
memset(input, 0, sizeof(input));
MEMCPY(&input[36], secret_16_bytes, 16);
base_time = boot_time * 250000;
}{ ... }
/* ... */
u32_t
lwip_hook_tcp_isn(const ip_addr_t *local_ip, u16_t local_port,
const ip_addr_t *remote_ip, u16_t remote_port)
{
u8_t output[16];
u32_t isn;
#if LWIP_IPV4 && LWIP_IPV6
if (IP_IS_V6(local_ip))
#endif
#if LWIP_IPV6
{
const ip6_addr_t *local_ip6, *remote_ip6;
local_ip6 = ip_2_ip6(local_ip);
remote_ip6 = ip_2_ip6(remote_ip);
SMEMCPY(&input[0], &local_ip6->addr, 16);
SMEMCPY(&input[16], &remote_ip6->addr, 16);
}/* ... */
{...}#endif
#if LWIP_IPV4 && LWIP_IPV6
else
#endif
#if LWIP_IPV4
{
const ip4_addr_t *local_ip4, *remote_ip4;
local_ip4 = ip_2_ip4(local_ip);
remote_ip4 = ip_2_ip4(remote_ip);
/* ... */
memset(&input[0], 0, 10);
input[10] = 0xff;
input[11] = 0xff;
SMEMCPY(&input[12], &local_ip4->addr, 4);
memset(&input[16], 0, 10);
input[26] = 0xff;
input[27] = 0xff;
SMEMCPY(&input[28], &remote_ip4->addr, 4);
}/* ... */
{...}#endif
input[32] = (u8_t)(local_port >> 8);
input[33] = (u8_t)(local_port & 0xff);
input[34] = (u8_t)(remote_port >> 8);
input[35] = (u8_t)(remote_port & 0xff);
/* ... */
#if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
assert(!esp_ptr_external_ram(esp_cpu_get_sp()));
#endif
md5_context_t ctx;
esp_rom_md5_init(&ctx);
esp_rom_md5_update(&ctx, input, sizeof(input));
esp_rom_md5_final(output, &ctx);
MEMCPY(&isn, output, sizeof(isn));
return isn + base_time + sys_now() * 250;
}{ ... }
/* ... */#endif