1
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
151
152
153
158
159
160
173
174
175
176
181
182
184
185
186
191
192
193
194
195
196
197
198
199
200
201
202
203
205
206
207
208
214
215
216
217
218
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
245
246
248
249
250
251
252
253
254
255
256
261
262
263
264
265
266
269
270
277
278
279
280
281
282
283
284
286
287
288
289
290
294
295
296
297
298
299
302
303
304
306
307
310
311
312
313
314
315
317
318
319
320
325
326
327
328
329
330
332
333
334
335
336
340
341
342
344
345
346
347
348
349
350
351
352
353
360
361
364
373
374
375
376
378
379
382
383
384
385
386
387
388
391
392
393
394
395
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
420
421
422
423
424
425
427
432
433
434
435
436
437
438
439
441
442
451
452
455
456
457
458
460
461
475
478
479
480
481
482
483
484
485
486
487
488
490
491
492
496
497
498
499
500
501
502
510
511
512
513
514
518
519
520
521
522
527
528
529
531
547
548
549
552
553
554
555
558
559
560
561
563
564
565
566
567
568
572
584
585
586
587
588
589
590
591
592
593
594
599
600
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
640
641
642
643
645
652
653
654
659
660
663
664
665
666
667
668
669
670
671
672
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
717
718
719
720
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
746
749
750
751
752
753
767
768
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
836
837
842
843
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
889
890
891
893
899
900
902
903
906
907
908
909
921
922
923
924
925
926
927
928
929
933
934
935
936
937
938
939
940
941
942
951
953
954
955
956
959
960
961
962
963
964
965
969
974
978
979
981
988
989
990
991
992
993
994
995
996
997
998
999
1000
1004
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1038
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1062
1064
1065
1066
1067
1068
1071
1072
1073
1074
1075
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1160
1163
1164
1165
1166
1167
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1229
1230
1231
1232
1235
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1274
1275
1276
1277
1278
1279
1280
1281
1282
1284
1285
1291
1292
1293
1295
1300
1301
1302
1303
1304
1308
1309
1310
1315
1316
1317
1318
1332
1333
1337
1338
1339
1340
1341
1344
1345
1347
1348
1349
1350
1351
1352
1353
1354
1355
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1402
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1457
1458
1459
1460
1463
1464
1465
1466
1467
1468
1469
1470
1475
1476
1477
1478
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1503
1504
1506
1507
1508
1509
1510
1511
1516
1517
1518
1519
1520
1522
1523
1524
1525
1526
1527
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1564
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1598
1605
1606
1607
1608
1609
1610
1621
1622
1623
1624
1625
1626
1627
1628
1636
1637
1638
1643
1644
1645
1646
1647
1648
1649
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1675
1676
1677
1678
1704
1739
1740
1741
1747
1748
1751
1752
1753
1756
1757
1758
1762
1763
1764
1769
1770
1771
1772
1773
1774
1775
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1818
1819
1820
1821
1823
1824
1826
1827
1828
1830
1831
1832
1833
1834
1835
1836
1841
1842
1847
1848
1849
1854
1855
1856
1863
1864
1865
1866
1867
1868
1869
1870
1872
1873
1874
1878
1885
1886
1887
1888
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1928
1932
1933
1934
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1953
1954
1955
1957
1958
1959
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1979
1980
1981
1982
1983
1984
1992
1993
1994
1995
1996
1997
1998
1999
2004
2005
2009
2010
2011
2023
2024
2025
2026
2027
2028
2029
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2055
2056
2060
2073
2074
2080
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2114
2128
2129
2130
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2154
2168
2169
2170
2173
2174
2175
2176
2177
2178
/* ... */
/* ... */
#include "lwip/opt.h"
#if LWIP_TCP
#include "lwip/priv/tcp_priv.h"
#include "lwip/def.h"
#include "lwip/ip_addr.h"
#include "lwip/netif.h"
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/inet_chksum.h"
#include "lwip/stats.h"
#include "lwip/ip6.h"
#include "lwip/ip6_addr.h"
10 includes#if LWIP_ND6_TCP_REACHABILITY_HINTS
#include "lwip/nd6.h"
#endif
#include <string.h>
#ifdef LWIP_HOOK_FILENAME
#include LWIP_HOOK_FILENAME
#endif
#define LWIP_TCP_CALC_INITIAL_CWND(mss) ((tcpwnd_size_t)LWIP_MIN((4U * (mss)), LWIP_MAX((2U * (mss)), 4380U)))
/* ... */
static struct tcp_seg inseg;
static struct tcp_hdr *tcphdr;
static u16_t tcphdr_optlen;
static u16_t tcphdr_opt1len;
static u8_t *tcphdr_opt2;
static u16_t tcp_optidx;
static u32_t seqno, ackno;
static tcpwnd_size_t recv_acked;
static u16_t tcplen;
static u8_t flags;
static u8_t recv_flags;
static struct pbuf *recv_data;
struct tcp_pcb *tcp_input_pcb;
static err_t tcp_process(struct tcp_pcb *pcb);
static void tcp_receive(struct tcp_pcb *pcb);
static void tcp_parseopt(struct tcp_pcb *pcb);
static void tcp_listen_input(struct tcp_pcb_listen *pcb);
static void tcp_timewait_input(struct tcp_pcb *pcb);
static int tcp_input_delayed_close(struct tcp_pcb *pcb);
#if LWIP_TCP_SACK_OUT
static void tcp_add_sack(struct tcp_pcb *pcb, u32_t left, u32_t right);
static void tcp_remove_sacks_lt(struct tcp_pcb *pcb, u32_t seq);
#if defined(TCP_OOSEQ_BYTES_LIMIT) || defined(TCP_OOSEQ_PBUFS_LIMIT)
static void tcp_remove_sacks_gt(struct tcp_pcb *pcb, u32_t seq);
#endif /* ... */
#endif
/* ... */
void
tcp_input(struct pbuf *p, struct netif *inp)
{
struct tcp_pcb *pcb, *prev;
struct tcp_pcb_listen *lpcb;
#if SO_REUSE
struct tcp_pcb *lpcb_prev = NULL;
struct tcp_pcb_listen *lpcb_any = NULL;/* ... */
#endif
u8_t hdrlen_bytes;
err_t err;
LWIP_UNUSED_ARG(inp);
LWIP_ASSERT_CORE_LOCKED();
LWIP_ASSERT("tcp_input: invalid pbuf", p != NULL);
PERF_START;
TCP_STATS_INC(tcp.recv);
MIB2_STATS_INC(mib2.tcpinsegs);
tcphdr = (struct tcp_hdr *)p->payload;
#if TCP_INPUT_DEBUG
tcp_debug_print(tcphdr);
#endif
if (p->len < TCP_HLEN) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: short packet (%"U16_F" bytes) discarded\n", p->tot_len));
TCP_STATS_INC(tcp.lenerr);
goto dropped;
}if (p->len < TCP_HLEN) { ... }
if (ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif()) ||
ip_addr_ismulticast(ip_current_dest_addr())) {
TCP_STATS_INC(tcp.proterr);
goto dropped;
}if (ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif()) || ip_addr_ismulticast(ip_current_dest_addr())) { ... }
#if CHECKSUM_CHECK_TCP
IF__NETIF_CHECKSUM_ENABLED(inp, NETIF_CHECKSUM_CHECK_TCP) {
u16_t chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
ip_current_src_addr(), ip_current_dest_addr());
if (chksum != 0) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packet discarded due to failing checksum 0x%04"X16_F"\n",
chksum));
tcp_debug_print(tcphdr);
TCP_STATS_INC(tcp.chkerr);
goto dropped;
}if (chksum != 0) { ... }
}IF__NETIF_CHECKSUM_ENABLED (inp, NETIF_CHECKSUM_CHECK_TCP) { ... }
/* ... */#endif
hdrlen_bytes = TCPH_HDRLEN_BYTES(tcphdr);
if ((hdrlen_bytes < TCP_HLEN) || (hdrlen_bytes > p->tot_len)) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: invalid header length (%"U16_F")\n", (u16_t)hdrlen_bytes));
TCP_STATS_INC(tcp.lenerr);
goto dropped;
}if ((hdrlen_bytes < TCP_HLEN) || (hdrlen_bytes > p->tot_len)) { ... }
/* ... */
tcphdr_optlen = (u16_t)(hdrlen_bytes - TCP_HLEN);
tcphdr_opt2 = NULL;
if (p->len >= hdrlen_bytes) {
tcphdr_opt1len = tcphdr_optlen;
pbuf_remove_header(p, hdrlen_bytes);
}if (p->len >= hdrlen_bytes) { ... } else {
u16_t opt2len;
LWIP_ASSERT("p->next != NULL", p->next != NULL);
pbuf_remove_header(p, TCP_HLEN);
tcphdr_opt1len = p->len;
opt2len = (u16_t)(tcphdr_optlen - tcphdr_opt1len);
/* ... */
pbuf_remove_header(p, tcphdr_opt1len);
if (opt2len > p->next->len) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: options overflow second pbuf (%"U16_F" bytes)\n", p->next->len));
TCP_STATS_INC(tcp.lenerr);
goto dropped;
}if (opt2len > p->next->len) { ... }
tcphdr_opt2 = (u8_t *)p->next->payload;
/* ... */
pbuf_remove_header(p->next, opt2len);
p->tot_len = (u16_t)(p->tot_len - opt2len);
LWIP_ASSERT("p->len == 0", p->len == 0);
LWIP_ASSERT("p->tot_len == p->next->tot_len", p->tot_len == p->next->tot_len);
}else { ... }
tcphdr->src = lwip_ntohs(tcphdr->src);
tcphdr->dest = lwip_ntohs(tcphdr->dest);
seqno = tcphdr->seqno = lwip_ntohl(tcphdr->seqno);
ackno = tcphdr->ackno = lwip_ntohl(tcphdr->ackno);
tcphdr->wnd = lwip_ntohs(tcphdr->wnd);
flags = TCPH_FLAGS(tcphdr);
tcplen = p->tot_len;
if (flags & (TCP_FIN | TCP_SYN)) {
tcplen++;
if (tcplen < p->tot_len) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: length u16_t overflow, cannot handle this\n"));
TCP_STATS_INC(tcp.lenerr);
goto dropped;
}if (tcplen < p->tot_len) { ... }
}if (flags & (TCP_FIN | TCP_SYN)) { ... }
/* ... */
prev = NULL;
for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
LWIP_ASSERT("tcp_input: active pcb->state != CLOSED", pcb->state != CLOSED);
LWIP_ASSERT("tcp_input: active pcb->state != TIME-WAIT", pcb->state != TIME_WAIT);
LWIP_ASSERT("tcp_input: active pcb->state != LISTEN", pcb->state != LISTEN);
if ((pcb->netif_idx != NETIF_NO_INDEX) &&
(pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) {
prev = pcb;
continue;
}if ((pcb->netif_idx != NETIF_NO_INDEX) && (pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) { ... }
if (pcb->remote_port == tcphdr->src &&
pcb->local_port == tcphdr->dest &&
ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()) &&
ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
/* ... */
LWIP_ASSERT("tcp_input: pcb->next != pcb (before cache)", pcb->next != pcb);
if (prev != NULL) {
prev->next = pcb->next;
pcb->next = tcp_active_pcbs;
tcp_active_pcbs = pcb;
}if (prev != NULL) { ... } else {
TCP_STATS_INC(tcp.cachehit);
}else { ... }
LWIP_ASSERT("tcp_input: pcb->next != pcb (after cache)", pcb->next != pcb);
break;
}if (pcb->remote_port == tcphdr->src && pcb->local_port == tcphdr->dest && ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()) && ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) { ... }
prev = pcb;
}for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) { ... }
if (pcb == NULL) {
/* ... */
for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) {
LWIP_ASSERT("tcp_input: TIME-WAIT pcb->state == TIME-WAIT", pcb->state == TIME_WAIT);
if ((pcb->netif_idx != NETIF_NO_INDEX) &&
(pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) {
continue;
}if ((pcb->netif_idx != NETIF_NO_INDEX) && (pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) { ... }
if (pcb->remote_port == tcphdr->src &&
pcb->local_port == tcphdr->dest &&
ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()) &&
ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
/* ... */
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packed for TIME_WAITing connection.\n"));
#ifdef LWIP_HOOK_TCP_INPACKET_PCB
if (LWIP_HOOK_TCP_INPACKET_PCB(pcb, tcphdr, tcphdr_optlen, tcphdr_opt1len,
tcphdr_opt2, p) == ERR_OK)/* ... */
#endif
{
tcp_timewait_input(pcb);
...}
pbuf_free(p);
return;
}if (pcb->remote_port == tcphdr->src && pcb->local_port == tcphdr->dest && ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()) && ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) { ... }
}for (pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) { ... }
/* ... */
prev = NULL;
for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) {
if ((lpcb->netif_idx != NETIF_NO_INDEX) &&
(lpcb->netif_idx != netif_get_index(ip_data.current_input_netif))) {
prev = (struct tcp_pcb *)lpcb;
continue;
}if ((lpcb->netif_idx != NETIF_NO_INDEX) && (lpcb->netif_idx != netif_get_index(ip_data.current_input_netif))) { ... }
if (lpcb->local_port == tcphdr->dest) {
if (IP_IS_ANY_TYPE_VAL(lpcb->local_ip)) {
#if SO_REUSE
lpcb_any = lpcb;
lpcb_prev = prev;/* ... */
#else
break;
#endif
}if (IP_IS_ANY_TYPE_VAL(lpcb->local_ip)) { ... } else if (IP_ADDR_PCB_VERSION_MATCH_EXACT(lpcb, ip_current_dest_addr())) {
if (ip_addr_cmp(&lpcb->local_ip, ip_current_dest_addr())) {
break;
}if (ip_addr_cmp(&lpcb->local_ip, ip_current_dest_addr())) { ... } else if (ip_addr_isany(&lpcb->local_ip)) {
#if SO_REUSE
lpcb_any = lpcb;
lpcb_prev = prev;/* ... */
#else
break;
#endif
}else if (ip_addr_isany(&lpcb->local_ip)) { ... }
}else if (IP_ADDR_PCB_VERSION_MATCH_EXACT(lpcb, ip_current_dest_addr())) { ... }
}if (lpcb->local_port == tcphdr->dest) { ... }
prev = (struct tcp_pcb *)lpcb;
}for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = lpcb->next) { ... }
#if SO_REUSE
if (lpcb == NULL) {
lpcb = lpcb_any;
prev = lpcb_prev;
}if (lpcb == NULL) { ... }
/* ... */#endif
if (lpcb != NULL) {
/* ... */
if (prev != NULL) {
((struct tcp_pcb_listen *)prev)->next = lpcb->next;
lpcb->next = tcp_listen_pcbs.listen_pcbs;
tcp_listen_pcbs.listen_pcbs = lpcb;
}if (prev != NULL) { ... } else {
TCP_STATS_INC(tcp.cachehit);
}else { ... }
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: packed for LISTENing connection.\n"));
#ifdef LWIP_HOOK_TCP_INPACKET_PCB
if (LWIP_HOOK_TCP_INPACKET_PCB((struct tcp_pcb *)lpcb, tcphdr, tcphdr_optlen,
tcphdr_opt1len, tcphdr_opt2, p) == ERR_OK)/* ... */
#endif
{
tcp_listen_input(lpcb);
...}
pbuf_free(p);
return;
}if (lpcb != NULL) { ... }
}if (pcb == NULL) { ... }
#if TCP_INPUT_DEBUG
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("+-+-+-+-+-+-+-+-+-+-+-+-+-+- tcp_input: flags "));
tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n"));/* ... */
#endif
#ifdef LWIP_HOOK_TCP_INPACKET_PCB
if ((pcb != NULL) && LWIP_HOOK_TCP_INPACKET_PCB(pcb, tcphdr, tcphdr_optlen,
tcphdr_opt1len, tcphdr_opt2, p) != ERR_OK) {
pbuf_free(p);
return;
}if ((pcb != NULL) && LWIP_HOOK_TCP_INPACKET_PCB(pcb, tcphdr, tcphdr_optlen, tcphdr_opt1len, tcphdr_opt2, p) != ERR_OK) { ... }
/* ... */#endif
if (pcb != NULL) {
#if TCP_INPUT_DEBUG
tcp_debug_print_state(pcb->state);
#endif
inseg.next = NULL;
inseg.len = p->tot_len;
inseg.p = p;
inseg.tcphdr = tcphdr;
recv_data = NULL;
recv_flags = 0;
recv_acked = 0;
if (flags & TCP_PSH) {
p->flags |= PBUF_FLAG_PUSH;
}if (flags & TCP_PSH) { ... }
if (pcb->refused_data != NULL) {
if ((tcp_process_refused_data(pcb) == ERR_ABRT) ||
((pcb->refused_data != NULL) && (tcplen > 0))) {
/* ... */
if (pcb->rcv_ann_wnd == 0) {
/* ... */
tcp_send_empty_ack(pcb);
}if (pcb->rcv_ann_wnd == 0) { ... }
TCP_STATS_INC(tcp.drop);
MIB2_STATS_INC(mib2.tcpinerrs);
goto aborted;
}if ((tcp_process_refused_data(pcb) == ERR_ABRT) || ((pcb->refused_data != NULL) && (tcplen > 0))) { ... }
}if (pcb->refused_data != NULL) { ... }
tcp_input_pcb = pcb;
err = tcp_process(pcb);
/* ... */
if (err != ERR_ABRT) {
if (recv_flags & TF_RESET) {
/* ... */
TCP_EVENT_ERR(pcb->state, pcb->errf, pcb->callback_arg, ERR_RST);
tcp_pcb_remove(&tcp_active_pcbs, pcb);
tcp_free(pcb);
}if (recv_flags & TF_RESET) { ... } else {
err = ERR_OK;
/* ... */
if (recv_acked > 0) {
u16_t acked16;
#if LWIP_WND_SCALE
/* ... */
u32_t acked = recv_acked;
while (acked > 0) {
acked16 = (u16_t)LWIP_MIN(acked, 0xffffu);
acked -= acked16;
#else
{
acked16 = recv_acked;
#endif
TCP_EVENT_SENT(pcb, (u16_t)acked16, err);
if (err == ERR_ABRT) {
goto aborted;
}if (err == ERR_ABRT) { ... }
...}
recv_acked = 0;
}while (acked > 0) { ... }
if (tcp_input_delayed_close(pcb)) {
goto aborted;
}if (tcp_input_delayed_close(pcb)) { ... }
#if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
while (recv_data != NULL) {
struct pbuf *rest = NULL;
pbuf_split_64k(recv_data, &rest);
#else
if (recv_data != NULL) {
#endif
LWIP_ASSERT("pcb->refused_data == NULL", pcb->refused_data == NULL);
if (pcb->flags & TF_RXCLOSED) {
/* ... */
pbuf_free(recv_data);
#if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
if (rest != NULL) {
pbuf_free(rest);
}if (rest != NULL) { ... }
/* ... */#endif
tcp_abort(pcb);
goto aborted;
}if (pcb->flags & TF_RXCLOSED) { ... }
TCP_EVENT_RECV(pcb, recv_data, ERR_OK, err);
if (err == ERR_ABRT) {
#if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
if (rest != NULL) {
pbuf_free(rest);
}if (rest != NULL) { ... }
/* ... */#endif
goto aborted;
}if (err == ERR_ABRT) { ... }
if (err != ERR_OK) {
#if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
if (rest != NULL) {
pbuf_cat(recv_data, rest);
}if (rest != NULL) { ... }
/* ... */#endif
pcb->refused_data = recv_data;
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_input: keep incoming packet, because pcb is \"full\"\n"));
#if TCP_QUEUE_OOSEQ && LWIP_WND_SCALE
break;
}if (err != ERR_OK) { ... } else {
recv_data = rest;
#endif
}else { ... }
}if (recv_data != NULL) { ... }
/* ... */
if (recv_flags & TF_GOT_FIN) {
if (pcb->refused_data != NULL) {
pcb->refused_data->flags |= PBUF_FLAG_TCP_FIN;
}if (pcb->refused_data != NULL) { ... } else {
/* ... */
if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) {
pcb->rcv_wnd++;
}if (pcb->rcv_wnd != TCP_WND_MAX(pcb)) { ... }
TCP_EVENT_CLOSED(pcb, err);
if (err == ERR_ABRT) {
goto aborted;
}if (err == ERR_ABRT) { ... }
}else { ... }
}if (recv_flags & TF_GOT_FIN) { ... }
tcp_input_pcb = NULL;
if (tcp_input_delayed_close(pcb)) {
goto aborted;
}if (tcp_input_delayed_close(pcb)) { ... }
tcp_output(pcb);
#if TCP_INPUT_DEBUG
#if TCP_DEBUG
tcp_debug_print_state(pcb->state);
#endif /* ... */
#endif
}while (recv_data != NULL) { ... }
}if (recv_acked > 0) { ... }
/* ... */
aborted:
tcp_input_pcb = NULL;
recv_data = NULL;
if (inseg.p != NULL) {
pbuf_free(inseg.p);
inseg.p = NULL;
}if (inseg.p != NULL) { ... }
}else { ... } else {
/* ... */
LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_input: no PCB match found, resetting.\n"));
if (!(TCPH_FLAGS(tcphdr) & TCP_RST)) {
TCP_STATS_INC(tcp.proterr);
TCP_STATS_INC(tcp.drop);
tcp_rst(NULL, ackno, seqno + tcplen, ip_current_dest_addr(),
ip_current_src_addr(), tcphdr->dest, tcphdr->src);
}if (!(TCPH_FLAGS(tcphdr) & TCP_RST)) { ... }
pbuf_free(p);
}else { ... }
LWIP_ASSERT("tcp_input: tcp_pcbs_sane()", tcp_pcbs_sane());
PERF_STOP("tcp_input");
return;
dropped:
TCP_STATS_INC(tcp.drop);
MIB2_STATS_INC(mib2.tcpinerrs);
pbuf_free(p);
}{ ... }
/* ... */
static int
tcp_input_delayed_close(struct tcp_pcb *pcb)
{
LWIP_ASSERT("tcp_input_delayed_close: invalid pcb", pcb != NULL);
if (recv_flags & TF_CLOSED) {
/* ... */
if (!(pcb->flags & TF_RXCLOSED)) {
/* ... */
TCP_EVENT_ERR(pcb->state, pcb->errf, pcb->callback_arg, ERR_CLSD);
}if (!(pcb->flags & TF_RXCLOSED)) { ... }
tcp_pcb_remove(&tcp_active_pcbs, pcb);
tcp_free(pcb);
return 1;
}if (recv_flags & TF_CLOSED) { ... }
return 0;
}{ ... }
/* ... */
static void
tcp_listen_input(struct tcp_pcb_listen *pcb)
{
struct tcp_pcb *npcb;
u32_t iss;
err_t rc;
if (flags & TCP_RST) {
return;
}if (flags & TCP_RST) { ... }
LWIP_ASSERT("tcp_listen_input: invalid pcb", pcb != NULL);
/* ... */
if (flags & TCP_ACK) {
/* ... */
LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_listen_input: ACK in LISTEN, sending reset\n"));
tcp_rst((const struct tcp_pcb *)pcb, ackno, seqno + tcplen, ip_current_dest_addr(),
ip_current_src_addr(), tcphdr->dest, tcphdr->src);
}if (flags & TCP_ACK) { ... } else if (flags & TCP_SYN) {
LWIP_DEBUGF(TCP_DEBUG, ("TCP connection request %"U16_F" -> %"U16_F".\n", tcphdr->src, tcphdr->dest));
#if TCP_LISTEN_BACKLOG
if (pcb->accepts_pending >= pcb->backlog) {
LWIP_DEBUGF(TCP_DEBUG, ("tcp_listen_input: listen backlog exceeded for port %"U16_F"\n", tcphdr->dest));
return;
}if (pcb->accepts_pending >= pcb->backlog) { ... }
/* ... */#endif
npcb = tcp_alloc(pcb->prio);
/* ... */
if (npcb == NULL) {
err_t err;
LWIP_DEBUGF(TCP_DEBUG, ("tcp_listen_input: could not allocate PCB\n"));
TCP_STATS_INC(tcp.memerr);
TCP_EVENT_ACCEPT(pcb, NULL, pcb->callback_arg, ERR_MEM, err);
LWIP_UNUSED_ARG(err);
return;
}if (npcb == NULL) { ... }
#if TCP_LISTEN_BACKLOG
pcb->accepts_pending++;
tcp_set_flags(npcb, TF_BACKLOGPEND);/* ... */
#endif
ip_addr_copy(npcb->local_ip, *ip_current_dest_addr());
ip_addr_copy(npcb->remote_ip, *ip_current_src_addr());
npcb->local_port = pcb->local_port;
npcb->remote_port = tcphdr->src;
npcb->state = SYN_RCVD;
npcb->rcv_nxt = seqno + 1;
npcb->rcv_ann_right_edge = npcb->rcv_nxt;
iss = tcp_next_iss(npcb);
npcb->snd_wl2 = iss;
npcb->snd_nxt = iss;
npcb->lastack = iss;
npcb->snd_lbb = iss;
npcb->snd_wl1 = seqno - 1;
npcb->callback_arg = pcb->callback_arg;
#if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
npcb->listener = pcb;
#endif
npcb->so_options = pcb->so_options & SOF_INHERITED;
npcb->netif_idx = pcb->netif_idx;
/* ... */
TCP_REG_ACTIVE(npcb);
tcp_parseopt(npcb);
npcb->snd_wnd = tcphdr->wnd;
npcb->snd_wnd_max = npcb->snd_wnd;
#if TCP_CALCULATE_EFF_SEND_MSS
npcb->mss = tcp_eff_send_mss(npcb->mss, &npcb->local_ip, &npcb->remote_ip);
#endif
MIB2_STATS_INC(mib2.tcppassiveopens);
#if LWIP_TCP_PCB_NUM_EXT_ARGS
if (tcp_ext_arg_invoke_callbacks_passive_open(pcb, npcb) != ERR_OK) {
tcp_abandon(npcb, 0);
return;
}if (tcp_ext_arg_invoke_callbacks_passive_open(pcb, npcb) != ERR_OK) { ... }
/* ... */#endif
rc = tcp_enqueue_flags(npcb, TCP_SYN | TCP_ACK);
if (rc != ERR_OK) {
tcp_abandon(npcb, 0);
return;
}if (rc != ERR_OK) { ... }
tcp_output(npcb);
}else if (flags & TCP_SYN) { ... }
return;
}{ ... }
/* ... */
static void
tcp_timewait_input(struct tcp_pcb *pcb)
{
/* ... */
if (flags & TCP_RST) {
return;
}if (flags & TCP_RST) { ... }
LWIP_ASSERT("tcp_timewait_input: invalid pcb", pcb != NULL);
if (flags & TCP_SYN) {
/* ... */
if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd)) {
tcp_rst(pcb, ackno, seqno + tcplen, ip_current_dest_addr(),
ip_current_src_addr(), tcphdr->dest, tcphdr->src);
return;
}if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd)) { ... }
}if (flags & TCP_SYN) { ... } else if (flags & TCP_FIN) {
/* ... */
pcb->tmr = tcp_ticks;
}else if (flags & TCP_FIN) { ... }
if ((tcplen > 0)) {
tcp_ack_now(pcb);
tcp_output(pcb);
}if ((tcplen > 0)) { ... }
return;
}{ ... }
/* ... */
static err_t
tcp_process(struct tcp_pcb *pcb)
{
struct tcp_seg *rseg;
u8_t acceptable = 0;
err_t err;
err = ERR_OK;
LWIP_ASSERT("tcp_process: invalid pcb", pcb != NULL);
if (flags & TCP_RST) {
if (pcb->state == SYN_SENT) {
/* ... */
if (ackno == pcb->snd_nxt) {
acceptable = 1;
}if (ackno == pcb->snd_nxt) { ... }
}if (pcb->state == SYN_SENT) { ... } else {
/* ... */
if (seqno == pcb->rcv_nxt) {
acceptable = 1;
}if (seqno == pcb->rcv_nxt) { ... } else if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt,
pcb->rcv_nxt + pcb->rcv_wnd)) {
/* ... */
tcp_ack_now(pcb);
}else if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd)) { ... }
}else { ... }
if (acceptable) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_process: Connection RESET\n"));
LWIP_ASSERT("tcp_input: pcb->state != CLOSED", pcb->state != CLOSED);
recv_flags |= TF_RESET;
tcp_clear_flags(pcb, TF_ACK_DELAY);
return ERR_RST;
}if (acceptable) { ... } else {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_process: unacceptable reset seqno %"U32_F" rcv_nxt %"U32_F"\n",
seqno, pcb->rcv_nxt));
LWIP_DEBUGF(TCP_DEBUG, ("tcp_process: unacceptable reset seqno %"U32_F" rcv_nxt %"U32_F"\n",
seqno, pcb->rcv_nxt));
return ERR_OK;
}else { ... }
}if (flags & TCP_RST) { ... }
if ((flags & TCP_SYN) && (pcb->state != SYN_SENT && pcb->state != SYN_RCVD)) {
tcp_ack_now(pcb);
return ERR_OK;
}if ((flags & TCP_SYN) && (pcb->state != SYN_SENT && pcb->state != SYN_RCVD)) { ... }
if ((pcb->flags & TF_RXCLOSED) == 0) {
pcb->tmr = tcp_ticks;
}if ((pcb->flags & TF_RXCLOSED) == 0) { ... }
pcb->keep_cnt_sent = 0;
pcb->persist_probe = 0;
tcp_parseopt(pcb);
switch (pcb->state) {
case SYN_SENT:
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("SYN-SENT: ackno %"U32_F" pcb->snd_nxt %"U32_F" unacked %"U32_F"\n", ackno,
pcb->snd_nxt, lwip_ntohl(pcb->unacked->tcphdr->seqno)));
if ((flags & TCP_ACK) && (flags & TCP_SYN)
&& (ackno == pcb->lastack + 1)) {
pcb->rcv_nxt = seqno + 1;
pcb->rcv_ann_right_edge = pcb->rcv_nxt;
pcb->lastack = ackno;
pcb->snd_wnd = tcphdr->wnd;
pcb->snd_wnd_max = pcb->snd_wnd;
pcb->snd_wl1 = seqno - 1;
pcb->state = ESTABLISHED;
#if TCP_CALCULATE_EFF_SEND_MSS
pcb->mss = tcp_eff_send_mss(pcb->mss, &pcb->local_ip, &pcb->remote_ip);
#endif
pcb->cwnd = LWIP_TCP_CALC_INITIAL_CWND(pcb->mss);
LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_process (SENT): cwnd %"TCPWNDSIZE_F
" ssthresh %"TCPWNDSIZE_F"\n",
pcb->cwnd, pcb->ssthresh));
LWIP_ASSERT("pcb->snd_queuelen > 0", (pcb->snd_queuelen > 0));
--pcb->snd_queuelen;
LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_process: SYN-SENT --queuelen %"TCPWNDSIZE_F"\n", (tcpwnd_size_t)pcb->snd_queuelen));
rseg = pcb->unacked;
if (rseg == NULL) {
/* ... */
rseg = pcb->unsent;
LWIP_ASSERT("no segment to free", rseg != NULL);
pcb->unsent = rseg->next;
}if (rseg == NULL) { ... } else {
pcb->unacked = rseg->next;
}else { ... }
tcp_seg_free(rseg);
/* ... */
if (pcb->unacked == NULL) {
pcb->rtime = -1;
}if (pcb->unacked == NULL) { ... } else {
pcb->rtime = 0;
pcb->nrtx = 0;
}else { ... }
/* ... */
TCP_EVENT_CONNECTED(pcb, ERR_OK, err);
if (err == ERR_ABRT) {
return ERR_ABRT;
}if (err == ERR_ABRT) { ... }
tcp_ack_now(pcb);
}if ((flags & TCP_ACK) && (flags & TCP_SYN) && (ackno == pcb->lastack + 1)) { ... }
else if (flags & TCP_ACK) {
tcp_rst(pcb, ackno, seqno + tcplen, ip_current_dest_addr(),
ip_current_src_addr(), tcphdr->dest, tcphdr->src);
/* ... */
if (pcb->nrtx < TCP_SYNMAXRTX) {
pcb->rtime = 0;
tcp_rexmit_rto(pcb);
}if (pcb->nrtx < TCP_SYNMAXRTX) { ... }
}else if (flags & TCP_ACK) { ... }
break;case SYN_SENT:
case SYN_RCVD:
if (flags & TCP_ACK) {
if (TCP_SEQ_BETWEEN(ackno, pcb->lastack + 1, pcb->snd_nxt)) {
pcb->state = ESTABLISHED;
LWIP_DEBUGF(TCP_DEBUG, ("TCP connection established %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
#if LWIP_CALLBACK_API || TCP_LISTEN_BACKLOG
if (pcb->listener == NULL) {
err = ERR_VAL;
}if (pcb->listener == NULL) { ... } else
#endif
{
#if LWIP_CALLBACK_API
LWIP_ASSERT("pcb->listener->accept != NULL", pcb->listener->accept != NULL);
#endif
tcp_backlog_accepted(pcb);
TCP_EVENT_ACCEPT(pcb->listener, pcb, pcb->callback_arg, ERR_OK, err);
}else { ... }
if (err != ERR_OK) {
/* ... */
if (err != ERR_ABRT) {
tcp_abort(pcb);
}if (err != ERR_ABRT) { ... }
return ERR_ABRT;
}if (err != ERR_OK) { ... }
/* ... */
tcp_receive(pcb);
if (recv_acked != 0) {
recv_acked--;
}if (recv_acked != 0) { ... }
pcb->cwnd = LWIP_TCP_CALC_INITIAL_CWND(pcb->mss);
LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_process (SYN_RCVD): cwnd %"TCPWNDSIZE_F
" ssthresh %"TCPWNDSIZE_F"\n",
pcb->cwnd, pcb->ssthresh));
if (recv_flags & TF_GOT_FIN) {
tcp_ack_now(pcb);
pcb->state = CLOSE_WAIT;
}if (recv_flags & TF_GOT_FIN) { ... }
}if (TCP_SEQ_BETWEEN(ackno, pcb->lastack + 1, pcb->snd_nxt)) { ... } else {
tcp_rst(pcb, ackno, seqno + tcplen, ip_current_dest_addr(),
ip_current_src_addr(), tcphdr->dest, tcphdr->src);
}else { ... }
}if (flags & TCP_ACK) { ... } else if ((flags & TCP_SYN) && (seqno == pcb->rcv_nxt - 1)) {
tcp_rexmit(pcb);
}else if ((flags & TCP_SYN) && (seqno == pcb->rcv_nxt - 1)) { ... }
break;case SYN_RCVD:
case CLOSE_WAIT:
case CLOSE_WAIT:
case ESTABLISHED:
tcp_receive(pcb);
if (recv_flags & TF_GOT_FIN) {
tcp_ack_now(pcb);
pcb->state = CLOSE_WAIT;
}if (recv_flags & TF_GOT_FIN) { ... }
break;case ESTABLISHED:
case FIN_WAIT_1:
tcp_receive(pcb);
if (recv_flags & TF_GOT_FIN) {
if ((flags & TCP_ACK) && (ackno == pcb->snd_nxt) &&
pcb->unsent == NULL) {
LWIP_DEBUGF(TCP_DEBUG,
("TCP connection closed: FIN_WAIT_1 %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
tcp_ack_now(pcb);
tcp_pcb_purge(pcb);
TCP_RMV_ACTIVE(pcb);
pcb->state = TIME_WAIT;
TCP_REG(&tcp_tw_pcbs, pcb);
}if ((flags & TCP_ACK) && (ackno == pcb->snd_nxt) && pcb->unsent == NULL) { ... } else {
tcp_ack_now(pcb);
pcb->state = CLOSING;
}else { ... }
}if (recv_flags & TF_GOT_FIN) { ... } else if ((flags & TCP_ACK) && (ackno == pcb->snd_nxt) &&
pcb->unsent == NULL) {
pcb->state = FIN_WAIT_2;
}else if ((flags & TCP_ACK) && (ackno == pcb->snd_nxt) && pcb->unsent == NULL) { ... }
break;case FIN_WAIT_1:
case FIN_WAIT_2:
tcp_receive(pcb);
if (recv_flags & TF_GOT_FIN) {
LWIP_DEBUGF(TCP_DEBUG, ("TCP connection closed: FIN_WAIT_2 %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
tcp_ack_now(pcb);
tcp_pcb_purge(pcb);
TCP_RMV_ACTIVE(pcb);
pcb->state = TIME_WAIT;
TCP_REG(&tcp_tw_pcbs, pcb);
}if (recv_flags & TF_GOT_FIN) { ... }
break;case FIN_WAIT_2:
case CLOSING:
tcp_receive(pcb);
if ((flags & TCP_ACK) && ackno == pcb->snd_nxt && pcb->unsent == NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("TCP connection closed: CLOSING %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
tcp_pcb_purge(pcb);
TCP_RMV_ACTIVE(pcb);
pcb->state = TIME_WAIT;
TCP_REG(&tcp_tw_pcbs, pcb);
}if ((flags & TCP_ACK) && ackno == pcb->snd_nxt && pcb->unsent == NULL) { ... }
break;case CLOSING:
case LAST_ACK:
tcp_receive(pcb);
if ((flags & TCP_ACK) && ackno == pcb->snd_nxt && pcb->unsent == NULL) {
LWIP_DEBUGF(TCP_DEBUG, ("TCP connection closed: LAST_ACK %"U16_F" -> %"U16_F".\n", inseg.tcphdr->src, inseg.tcphdr->dest));
recv_flags |= TF_CLOSED;
}if ((flags & TCP_ACK) && ackno == pcb->snd_nxt && pcb->unsent == NULL) { ... }
break;case LAST_ACK:
default:
break;default
}switch (pcb->state) { ... }
return ERR_OK;
}{ ... }
#if TCP_QUEUE_OOSEQ
/* ... */
static void
tcp_oos_insert_segment(struct tcp_seg *cseg, struct tcp_seg *next)
{
struct tcp_seg *old_seg;
LWIP_ASSERT("tcp_oos_insert_segment: invalid cseg", cseg != NULL);
if (TCPH_FLAGS(cseg->tcphdr) & TCP_FIN) {
tcp_segs_free(next);
next = NULL;
}if (TCPH_FLAGS(cseg->tcphdr) & TCP_FIN) { ... } else {
/* ... */
while (next &&
TCP_SEQ_GEQ((seqno + cseg->len),
(next->tcphdr->seqno + next->len))) {
if (TCPH_FLAGS(next->tcphdr) & TCP_FIN) {
TCPH_SET_FLAG(cseg->tcphdr, TCP_FIN);
}if (TCPH_FLAGS(next->tcphdr) & TCP_FIN) { ... }
old_seg = next;
next = next->next;
tcp_seg_free(old_seg);
}while (next && TCP_SEQ_GEQ((seqno + cseg->len), (next->tcphdr->seqno + next->len))) { ... }
if (next &&
TCP_SEQ_GT(seqno + cseg->len, next->tcphdr->seqno)) {
cseg->len = (u16_t)(next->tcphdr->seqno - seqno);
pbuf_realloc(cseg->p, cseg->len);
}if (next && TCP_SEQ_GT(seqno + cseg->len, next->tcphdr->seqno)) { ... }
}else { ... }
cseg->next = next;
}tcp_oos_insert_segment (struct tcp_seg *cseg, struct tcp_seg *next) { ... }
/* ... */#endif
static struct tcp_seg *
tcp_free_acked_segments(struct tcp_pcb *pcb, struct tcp_seg *seg_list, const char *dbg_list_name,
struct tcp_seg *dbg_other_seg_list)
{
struct tcp_seg *next;
u16_t clen;
LWIP_UNUSED_ARG(dbg_list_name);
LWIP_UNUSED_ARG(dbg_other_seg_list);
while (seg_list != NULL &&
TCP_SEQ_LEQ(lwip_ntohl(seg_list->tcphdr->seqno) +
TCP_TCPLEN(seg_list), ackno)) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: removing %"U32_F":%"U32_F" from pcb->%s\n",
lwip_ntohl(seg_list->tcphdr->seqno),
lwip_ntohl(seg_list->tcphdr->seqno) + TCP_TCPLEN(seg_list),
dbg_list_name));
next = seg_list;
seg_list = seg_list->next;
clen = pbuf_clen(next->p);
LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_receive: queuelen %"TCPWNDSIZE_F" ... ",
(tcpwnd_size_t)pcb->snd_queuelen));
LWIP_ASSERT("pcb->snd_queuelen >= pbuf_clen(next->p)", (pcb->snd_queuelen >= clen));
pcb->snd_queuelen = (u16_t)(pcb->snd_queuelen - clen);
recv_acked = (tcpwnd_size_t)(recv_acked + next->len);
tcp_seg_free(next);
LWIP_DEBUGF(TCP_QLEN_DEBUG, ("%"TCPWNDSIZE_F" (after freeing %s)\n",
(tcpwnd_size_t)pcb->snd_queuelen,
dbg_list_name));
if (pcb->snd_queuelen != 0) {
LWIP_ASSERT("tcp_receive: valid queue length",
seg_list != NULL || dbg_other_seg_list != NULL);
}if (pcb->snd_queuelen != 0) { ... }
}while (seg_list != NULL && TCP_SEQ_LEQ(lwip_ntohl(seg_list->tcphdr->seqno) + TCP_TCPLEN(seg_list), ackno)) { ... }
return seg_list;
}{ ... }
/* ... */
static void
tcp_receive(struct tcp_pcb *pcb)
{
s16_t m;
u32_t right_wnd_edge;
int found_dupack = 0;
LWIP_ASSERT("tcp_receive: invalid pcb", pcb != NULL);
LWIP_ASSERT("tcp_receive: wrong state", pcb->state >= ESTABLISHED);
if (flags & TCP_ACK) {
right_wnd_edge = pcb->snd_wnd + pcb->snd_wl2;
if (TCP_SEQ_LT(pcb->snd_wl1, seqno) ||
(pcb->snd_wl1 == seqno && TCP_SEQ_LT(pcb->snd_wl2, ackno)) ||
(pcb->snd_wl2 == ackno && (u32_t)SND_WND_SCALE(pcb, tcphdr->wnd) > pcb->snd_wnd)) {
pcb->snd_wnd = SND_WND_SCALE(pcb, tcphdr->wnd);
/* ... */
if (pcb->snd_wnd_max < pcb->snd_wnd) {
pcb->snd_wnd_max = pcb->snd_wnd;
}if (pcb->snd_wnd_max < pcb->snd_wnd) { ... }
pcb->snd_wl1 = seqno;
pcb->snd_wl2 = ackno;
LWIP_DEBUGF(TCP_WND_DEBUG, ("tcp_receive: window update %"TCPWNDSIZE_F"\n", pcb->snd_wnd));
#if TCP_WND_DEBUG
}if (TCP_SEQ_LT(pcb->snd_wl1, seqno) || (pcb->snd_wl1 == seqno && TCP_SEQ_LT(pcb->snd_wl2, ackno)) || (pcb->snd_wl2 == ackno && (u32_t)SND_WND_SCALE(pcb, tcphdr->wnd) > pcb->snd_wnd)) { ... } else {
if (pcb->snd_wnd != (tcpwnd_size_t)SND_WND_SCALE(pcb, tcphdr->wnd)) {
LWIP_DEBUGF(TCP_WND_DEBUG,
("tcp_receive: no window update lastack %"U32_F" ackno %"
U32_F" wl1 %"U32_F" seqno %"U32_F" wl2 %"U32_F"\n",
pcb->lastack, ackno, pcb->snd_wl1, seqno, pcb->snd_wl2));
}if (pcb->snd_wnd != (tcpwnd_size_t)SND_WND_SCALE(pcb, tcphdr->wnd)) { ... }
#endif
}else { ... }
/* ... */
if (TCP_SEQ_LEQ(ackno, pcb->lastack)) {
if (tcplen == 0) {
if (pcb->snd_wl2 + pcb->snd_wnd == right_wnd_edge) {
if (pcb->rtime >= 0) {
if (pcb->lastack == ackno) {
found_dupack = 1;
if ((u8_t)(pcb->dupacks + 1) > pcb->dupacks) {
++pcb->dupacks;
}if ((u8_t)(pcb->dupacks + 1) > pcb->dupacks) { ... }
if (pcb->dupacks > 3) {
TCP_WND_INC(pcb->cwnd, pcb->mss);
}if (pcb->dupacks > 3) { ... }
if (pcb->dupacks >= 3) {
tcp_rexmit_fast(pcb);
}if (pcb->dupacks >= 3) { ... }
}if (pcb->lastack == ackno) { ... }
}if (pcb->rtime >= 0) { ... }
}if (pcb->snd_wl2 + pcb->snd_wnd == right_wnd_edge) { ... }
}if (tcplen == 0) { ... }
/* ... */
if (!found_dupack) {
pcb->dupacks = 0;
}if (!found_dupack) { ... }
}if (TCP_SEQ_LEQ(ackno, pcb->lastack)) { ... } else if (TCP_SEQ_BETWEEN(ackno, pcb->lastack + 1, pcb->snd_nxt)) {
tcpwnd_size_t acked;
/* ... */
if (pcb->flags & TF_INFR) {
tcp_clear_flags(pcb, TF_INFR);
pcb->cwnd = pcb->ssthresh;
pcb->bytes_acked = 0;
}if (pcb->flags & TF_INFR) { ... }
pcb->nrtx = 0;
pcb->rto = (s16_t)((pcb->sa >> 3) + pcb->sv);
acked = (tcpwnd_size_t)(ackno - pcb->lastack);
pcb->dupacks = 0;
pcb->lastack = ackno;
/* ... */
if (pcb->state >= ESTABLISHED) {
if (pcb->cwnd < pcb->ssthresh) {
tcpwnd_size_t increase;
u8_t num_seg = (pcb->flags & TF_RTO) ? 1 : 2;
increase = LWIP_MIN(acked, (tcpwnd_size_t)(num_seg * pcb->mss));
TCP_WND_INC(pcb->cwnd, increase);
LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_receive: slow start cwnd %"TCPWNDSIZE_F"\n", pcb->cwnd));
}if (pcb->cwnd < pcb->ssthresh) { ... } else {
TCP_WND_INC(pcb->bytes_acked, acked);
if (pcb->bytes_acked >= pcb->cwnd) {
pcb->bytes_acked = (tcpwnd_size_t)(pcb->bytes_acked - pcb->cwnd);
TCP_WND_INC(pcb->cwnd, pcb->mss);
}if (pcb->bytes_acked >= pcb->cwnd) { ... }
LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_receive: congestion avoidance cwnd %"TCPWNDSIZE_F"\n", pcb->cwnd));
}else { ... }
}if (pcb->state >= ESTABLISHED) { ... }
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: ACK for %"U32_F", unacked->seqno %"U32_F":%"U32_F"\n",
ackno,
pcb->unacked != NULL ?
lwip_ntohl(pcb->unacked->tcphdr->seqno) : 0,
pcb->unacked != NULL ?
lwip_ntohl(pcb->unacked->tcphdr->seqno) + TCP_TCPLEN(pcb->unacked) : 0));
/* ... */
pcb->unacked = tcp_free_acked_segments(pcb, pcb->unacked, "unacked", pcb->unsent);
/* ... */
pcb->unsent = tcp_free_acked_segments(pcb, pcb->unsent, "unsent", pcb->unacked);
/* ... */
if (pcb->unacked == NULL) {
pcb->rtime = -1;
}if (pcb->unacked == NULL) { ... } else {
pcb->rtime = 0;
}else { ... }
pcb->polltmr = 0;
#if TCP_OVERSIZE
if (pcb->unsent == NULL) {
pcb->unsent_oversize = 0;
}if (pcb->unsent == NULL) { ... }
/* ... */#endif
#if LWIP_IPV6 && LWIP_ND6_TCP_REACHABILITY_HINTS
if (ip_current_is_v6()) {
nd6_reachability_hint(ip6_current_src_addr());
}if (ip_current_is_v6()) { ... }
/* ... */#endif
pcb->snd_buf = (tcpwnd_size_t)(pcb->snd_buf + recv_acked);
if (pcb->flags & TF_RTO) {
/* ... */
if (pcb->unacked == NULL) {
if ((pcb->unsent == NULL) ||
(TCP_SEQ_LEQ(pcb->rto_end, lwip_ntohl(pcb->unsent->tcphdr->seqno)))) {
tcp_clear_flags(pcb, TF_RTO);
}if ((pcb->unsent == NULL) || (TCP_SEQ_LEQ(pcb->rto_end, lwip_ntohl(pcb->unsent->tcphdr->seqno)))) { ... }
}if (pcb->unacked == NULL) { ... } else if (TCP_SEQ_LEQ(pcb->rto_end, lwip_ntohl(pcb->unacked->tcphdr->seqno))) {
tcp_clear_flags(pcb, TF_RTO);
}else if (TCP_SEQ_LEQ(pcb->rto_end, lwip_ntohl(pcb->unacked->tcphdr->seqno))) { ... }
}if (pcb->flags & TF_RTO) { ... }
}else if (TCP_SEQ_BETWEEN(ackno, pcb->lastack + 1, pcb->snd_nxt)) { ... } else {
tcp_send_empty_ack(pcb);
}else { ... }
LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: pcb->rttest %"U32_F" rtseq %"U32_F" ackno %"U32_F"\n",
pcb->rttest, pcb->rtseq, ackno));
/* ... */
if (pcb->rttest && TCP_SEQ_LT(pcb->rtseq, ackno)) {
/* ... */
m = (s16_t)(tcp_ticks - pcb->rttest);
LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: experienced rtt %"U16_F" ticks (%"U16_F" msec).\n",
m, (u16_t)(m * TCP_SLOW_INTERVAL)));
m = (s16_t)(m - (pcb->sa >> 3));
pcb->sa = (s16_t)(pcb->sa + m);
if (m < 0) {
m = (s16_t) - m;
}if (m < 0) { ... }
m = (s16_t)(m - (pcb->sv >> 2));
pcb->sv = (s16_t)(pcb->sv + m);
pcb->rto = (s16_t)((pcb->sa >> 3) + pcb->sv);
LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_receive: RTO %"U16_F" (%"U16_F" milliseconds)\n",
pcb->rto, (u16_t)(pcb->rto * TCP_SLOW_INTERVAL)));
pcb->rttest = 0;
}if (pcb->rttest && TCP_SEQ_LT(pcb->rtseq, ackno)) { ... }
}if (flags & TCP_ACK) { ... }
/* ... */
if ((tcplen > 0) && (pcb->state < CLOSE_WAIT)) {
/* ... */
/* ... */
/* ... */
if (TCP_SEQ_BETWEEN(pcb->rcv_nxt, seqno + 1, seqno + tcplen - 1)) {
/* ... */
struct pbuf *p = inseg.p;
u32_t off32 = pcb->rcv_nxt - seqno;
u16_t new_tot_len, off;
LWIP_ASSERT("inseg.p != NULL", inseg.p);
LWIP_ASSERT("insane offset!", (off32 < 0xffff));
off = (u16_t)off32;
LWIP_ASSERT("pbuf too short!", (((s32_t)inseg.p->tot_len) >= off));
inseg.len -= off;
new_tot_len = (u16_t)(inseg.p->tot_len - off);
while (p->len < off) {
off -= p->len;
p->tot_len = new_tot_len;
p->len = 0;
p = p->next;
}while (p->len < off) { ... }
pbuf_remove_header(p, off);
inseg.tcphdr->seqno = seqno = pcb->rcv_nxt;
}if (TCP_SEQ_BETWEEN(pcb->rcv_nxt, seqno + 1, seqno + tcplen - 1)) { ... } else {
if (TCP_SEQ_LT(seqno, pcb->rcv_nxt)) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: duplicate seqno %"U32_F"\n", seqno));
tcp_ack_now(pcb);
}if (TCP_SEQ_LT(seqno, pcb->rcv_nxt)) { ... }
}else { ... }
/* ... */
if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt,
pcb->rcv_nxt + pcb->rcv_wnd - 1)) {
if (pcb->rcv_nxt == seqno) {
/* ... */
tcplen = TCP_TCPLEN(&inseg);
if (tcplen > pcb->rcv_wnd) {
LWIP_DEBUGF(TCP_INPUT_DEBUG,
("tcp_receive: other end overran receive window"
"seqno %"U32_F" len %"U16_F" right edge %"U32_F"\n",
seqno, tcplen, pcb->rcv_nxt + pcb->rcv_wnd));
if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) {
/* ... */
TCPH_FLAGS_SET(inseg.tcphdr, TCPH_FLAGS(inseg.tcphdr) & ~(unsigned int)TCP_FIN);
}if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) { ... }
TCPWND_CHECK16(pcb->rcv_wnd);
inseg.len = (u16_t)pcb->rcv_wnd;
if (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) {
inseg.len -= 1;
}if (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) { ... }
pbuf_realloc(inseg.p, inseg.len);
tcplen = TCP_TCPLEN(&inseg);
LWIP_ASSERT("tcp_receive: segment not trimmed correctly to rcv_wnd\n",
(seqno + tcplen) == (pcb->rcv_nxt + pcb->rcv_wnd));
}if (tcplen > pcb->rcv_wnd) { ... }
#if TCP_QUEUE_OOSEQ
/* ... */
if (pcb->ooseq != NULL) {
if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) {
LWIP_DEBUGF(TCP_INPUT_DEBUG,
("tcp_receive: received in-order FIN, binning ooseq queue\n"));
/* ... */
while (pcb->ooseq != NULL) {
struct tcp_seg *old_ooseq = pcb->ooseq;
pcb->ooseq = pcb->ooseq->next;
tcp_seg_free(old_ooseq);
}while (pcb->ooseq != NULL) { ... }
}if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) { ... } else {
struct tcp_seg *next = pcb->ooseq;
/* ... */
while (next &&
TCP_SEQ_GEQ(seqno + tcplen,
next->tcphdr->seqno + next->len)) {
struct tcp_seg *tmp;
if ((TCPH_FLAGS(next->tcphdr) & TCP_FIN) != 0 &&
(TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) == 0) {
TCPH_SET_FLAG(inseg.tcphdr, TCP_FIN);
tcplen = TCP_TCPLEN(&inseg);
}if ((TCPH_FLAGS(next->tcphdr) & TCP_FIN) != 0 && (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) == 0) { ... }
tmp = next;
next = next->next;
tcp_seg_free(tmp);
}while (next && TCP_SEQ_GEQ(seqno + tcplen, next->tcphdr->seqno + next->len)) { ... }
/* ... */
if (next &&
TCP_SEQ_GT(seqno + tcplen,
next->tcphdr->seqno)) {
inseg.len = (u16_t)(next->tcphdr->seqno - seqno);
if (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) {
inseg.len -= 1;
}if (TCPH_FLAGS(inseg.tcphdr) & TCP_SYN) { ... }
pbuf_realloc(inseg.p, inseg.len);
tcplen = TCP_TCPLEN(&inseg);
LWIP_ASSERT("tcp_receive: segment not trimmed correctly to ooseq queue\n",
(seqno + tcplen) == next->tcphdr->seqno);
}if (next && TCP_SEQ_GT(seqno + tcplen, next->tcphdr->seqno)) { ... }
pcb->ooseq = next;
}else { ... }
}if (pcb->ooseq != NULL) { ... }
/* ... */#endif
pcb->rcv_nxt = seqno + tcplen;
LWIP_ASSERT("tcp_receive: tcplen > rcv_wnd\n", pcb->rcv_wnd >= tcplen);
pcb->rcv_wnd -= tcplen;
tcp_update_rcv_ann_wnd(pcb);
/* ... */
if (inseg.p->tot_len > 0) {
recv_data = inseg.p;
/* ... */
inseg.p = NULL;
}if (inseg.p->tot_len > 0) { ... }
if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: received FIN.\n"));
recv_flags |= TF_GOT_FIN;
}if (TCPH_FLAGS(inseg.tcphdr) & TCP_FIN) { ... }
#if TCP_QUEUE_OOSEQ
/* ... */
while (pcb->ooseq != NULL &&
pcb->ooseq->tcphdr->seqno == pcb->rcv_nxt) {
struct tcp_seg *cseg = pcb->ooseq;
seqno = pcb->ooseq->tcphdr->seqno;
pcb->rcv_nxt += TCP_TCPLEN(cseg);
LWIP_ASSERT("tcp_receive: ooseq tcplen > rcv_wnd\n",
pcb->rcv_wnd >= TCP_TCPLEN(cseg));
pcb->rcv_wnd -= TCP_TCPLEN(cseg);
tcp_update_rcv_ann_wnd(pcb);
if (cseg->p->tot_len > 0) {
/* ... */
/* ... */
if (recv_data) {
pbuf_cat(recv_data, cseg->p);
}if (recv_data) { ... } else {
recv_data = cseg->p;
}else { ... }
cseg->p = NULL;
}if (cseg->p->tot_len > 0) { ... }
if (TCPH_FLAGS(cseg->tcphdr) & TCP_FIN) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_receive: dequeued FIN.\n"));
recv_flags |= TF_GOT_FIN;
if (pcb->state == ESTABLISHED) {
pcb->state = CLOSE_WAIT;
}if (pcb->state == ESTABLISHED) { ... }
}if (TCPH_FLAGS(cseg->tcphdr) & TCP_FIN) { ... }
pcb->ooseq = cseg->next;
tcp_seg_free(cseg);
}while (pcb->ooseq != NULL && pcb->ooseq->tcphdr->seqno == pcb->rcv_nxt) { ... }
#if LWIP_TCP_SACK_OUT
if (pcb->flags & TF_SACK) {
if (pcb->ooseq != NULL) {
/* ... */
tcp_remove_sacks_lt(pcb, pcb->ooseq->tcphdr->seqno);
}if (pcb->ooseq != NULL) { ... } else if (LWIP_TCP_SACK_VALID(pcb, 0)) {
memset(pcb->rcv_sacks, 0, sizeof(pcb->rcv_sacks));
}else if (LWIP_TCP_SACK_VALID(pcb, 0)) { ... }
}if (pcb->flags & TF_SACK) { ... }
/* ... */#endif /* ... */
#endif
tcp_ack(pcb);
#if LWIP_TCP_SACK_OUT
if (LWIP_TCP_SACK_VALID(pcb, 0)) {
/* ... */
tcp_send_empty_ack(pcb);
}if (LWIP_TCP_SACK_VALID(pcb, 0)) { ... }
/* ... */#endif
#if LWIP_IPV6 && LWIP_ND6_TCP_REACHABILITY_HINTS
if (ip_current_is_v6()) {
nd6_reachability_hint(ip6_current_src_addr());
}if (ip_current_is_v6()) { ... }
/* ... */#endif
}if (pcb->rcv_nxt == seqno) { ... } else {
#if TCP_QUEUE_OOSEQ
if (pcb->ooseq == NULL) {
pcb->ooseq = tcp_seg_copy(&inseg);
#if LWIP_TCP_SACK_OUT
if (pcb->flags & TF_SACK) {
pcb->rcv_sacks[0].left = seqno;
pcb->rcv_sacks[0].right = seqno + inseg.len;
}if (pcb->flags & TF_SACK) { ... }
/* ... */#endif
}if (pcb->ooseq == NULL) { ... } else {
/* ... */
#if LWIP_TCP_SACK_OUT
/* ... */
u32_t sackbeg = TCP_SEQ_LT(seqno, pcb->ooseq->tcphdr->seqno) ? seqno : pcb->ooseq->tcphdr->seqno;/* ... */
#endif
struct tcp_seg *next, *prev = NULL;
for (next = pcb->ooseq; next != NULL; next = next->next) {
if (seqno == next->tcphdr->seqno) {
/* ... */
if (inseg.len > next->len) {
/* ... */
struct tcp_seg *cseg = tcp_seg_copy(&inseg);
if (cseg != NULL) {
if (prev != NULL) {
prev->next = cseg;
}if (prev != NULL) { ... } else {
pcb->ooseq = cseg;
}else { ... }
tcp_oos_insert_segment(cseg, next);
}if (cseg != NULL) { ... }
break;
}if (inseg.len > next->len) { ... } else {
/* ... */
break;
}else { ... }
}if (seqno == next->tcphdr->seqno) { ... } else {
if (prev == NULL) {
if (TCP_SEQ_LT(seqno, next->tcphdr->seqno)) {
/* ... */
struct tcp_seg *cseg = tcp_seg_copy(&inseg);
if (cseg != NULL) {
pcb->ooseq = cseg;
tcp_oos_insert_segment(cseg, next);
}if (cseg != NULL) { ... }
break;
}if (TCP_SEQ_LT(seqno, next->tcphdr->seqno)) { ... }
}if (prev == NULL) { ... } else {
/* ... */
if (TCP_SEQ_BETWEEN(seqno, prev->tcphdr->seqno + 1, next->tcphdr->seqno - 1)) {
/* ... */
struct tcp_seg *cseg = tcp_seg_copy(&inseg);
if (cseg != NULL) {
if (TCP_SEQ_GT(prev->tcphdr->seqno + prev->len, seqno)) {
prev->len = (u16_t)(seqno - prev->tcphdr->seqno);
pbuf_realloc(prev->p, prev->len);
}if (TCP_SEQ_GT(prev->tcphdr->seqno + prev->len, seqno)) { ... }
prev->next = cseg;
tcp_oos_insert_segment(cseg, next);
}if (cseg != NULL) { ... }
break;
}if (TCP_SEQ_BETWEEN(seqno, prev->tcphdr->seqno + 1, next->tcphdr->seqno - 1)) { ... }
}else { ... }
#if LWIP_TCP_SACK_OUT
/* ... */
if (prev != NULL && prev->tcphdr->seqno + prev->len != next->tcphdr->seqno) {
sackbeg = next->tcphdr->seqno;
}if (prev != NULL && prev->tcphdr->seqno + prev->len != next->tcphdr->seqno) { ... }
/* ... */#endif
/* ... */
prev = next;
/* ... */
if (next->next == NULL &&
TCP_SEQ_GT(seqno, next->tcphdr->seqno)) {
if (TCPH_FLAGS(next->tcphdr) & TCP_FIN) {
break;
}if (TCPH_FLAGS(next->tcphdr) & TCP_FIN) { ... }
next->next = tcp_seg_copy(&inseg);
if (next->next != NULL) {
if (TCP_SEQ_GT(next->tcphdr->seqno + next->len, seqno)) {
next->len = (u16_t)(seqno - next->tcphdr->seqno);
pbuf_realloc(next->p, next->len);
}if (TCP_SEQ_GT(next->tcphdr->seqno + next->len, seqno)) { ... }
if (TCP_SEQ_GT((u32_t)tcplen + seqno, pcb->rcv_nxt + (u32_t)pcb->rcv_wnd)) {
LWIP_DEBUGF(TCP_INPUT_DEBUG,
("tcp_receive: other end overran receive window"
"seqno %"U32_F" len %"U16_F" right edge %"U32_F"\n",
seqno, tcplen, pcb->rcv_nxt + pcb->rcv_wnd));
if (TCPH_FLAGS(next->next->tcphdr) & TCP_FIN) {
/* ... */
TCPH_FLAGS_SET(next->next->tcphdr, TCPH_FLAGS(next->next->tcphdr) & ~TCP_FIN);
}if (TCPH_FLAGS(next->next->tcphdr) & TCP_FIN) { ... }
next->next->len = (u16_t)(pcb->rcv_nxt + pcb->rcv_wnd - seqno);
pbuf_realloc(next->next->p, next->next->len);
tcplen = TCP_TCPLEN(next->next);
LWIP_ASSERT("tcp_receive: segment not trimmed correctly to rcv_wnd\n",
(seqno + tcplen) == (pcb->rcv_nxt + pcb->rcv_wnd));
}if (TCP_SEQ_GT((u32_t)tcplen + seqno, pcb->rcv_nxt + (u32_t)pcb->rcv_wnd)) { ... }
}if (next->next != NULL) { ... }
break;
}if (next->next == NULL && TCP_SEQ_GT(seqno, next->tcphdr->seqno)) { ... }
}else { ... }
}for (next = pcb->ooseq; next != NULL; next = next->next) { ... }
#if LWIP_TCP_SACK_OUT
if (pcb->flags & TF_SACK) {
if (prev == NULL) {
/* ... */
next = pcb->ooseq;
}if (prev == NULL) { ... } else if (prev->next != NULL) {
/* ... */
next = prev->next;
if (prev->tcphdr->seqno + prev->len != next->tcphdr->seqno) {
sackbeg = next->tcphdr->seqno;
}if (prev->tcphdr->seqno + prev->len != next->tcphdr->seqno) { ... }
}else if (prev->next != NULL) { ... } else {
next = NULL;
}else { ... }
if (next != NULL) {
u32_t sackend = next->tcphdr->seqno;
for ( ; (next != NULL) && (sackend == next->tcphdr->seqno); next = next->next) {
sackend += next->len;
}for (; (next != NULL) && (sackend == next->tcphdr->seqno); next = next->next) { ... }
tcp_add_sack(pcb, sackbeg, sackend);
}if (next != NULL) { ... }
}if (pcb->flags & TF_SACK) { ... }
/* ... */#endif
}else { ... }
#if defined(TCP_OOSEQ_BYTES_LIMIT) || defined(TCP_OOSEQ_PBUFS_LIMIT)
{
/* ... */
#ifdef TCP_OOSEQ_BYTES_LIMIT
const u32_t ooseq_max_blen = TCP_OOSEQ_BYTES_LIMIT(pcb);
u32_t ooseq_blen = 0;/* ... */
#endif
#ifdef TCP_OOSEQ_PBUFS_LIMIT
const u16_t ooseq_max_qlen = TCP_OOSEQ_PBUFS_LIMIT(pcb);
u16_t ooseq_qlen = 0;/* ... */
#endif
struct tcp_seg *next, *prev = NULL;
for (next = pcb->ooseq; next != NULL; prev = next, next = next->next) {
struct pbuf *p = next->p;
int stop_here = 0;
#ifdef TCP_OOSEQ_BYTES_LIMIT
ooseq_blen += p->tot_len;
if (ooseq_blen > ooseq_max_blen) {
stop_here = 1;
}if (ooseq_blen > ooseq_max_blen) { ... }
/* ... */#endif
#ifdef TCP_OOSEQ_PBUFS_LIMIT
ooseq_qlen += pbuf_clen(p);
if (ooseq_qlen > ooseq_max_qlen) {
stop_here = 1;
}if (ooseq_qlen > ooseq_max_qlen) { ... }
/* ... */#endif
if (stop_here) {
#if LWIP_TCP_SACK_OUT
if (pcb->flags & TF_SACK) {
tcp_remove_sacks_gt(pcb, next->tcphdr->seqno);
}if (pcb->flags & TF_SACK) { ... }
/* ... */#endif
tcp_segs_free(next);
if (prev == NULL) {
pcb->ooseq = NULL;
}if (prev == NULL) { ... } else {
prev->next = NULL;
}else { ... }
break;
}if (stop_here) { ... }
}for (next = pcb->ooseq; next != NULL; prev = next, next = next->next) { ... }
}/* ... */
defined (TCP_OOSEQ_PBUFS_LIMIT) { ... }#endif /* ... */
#endif
/* ... */
tcp_send_empty_ack(pcb);
}else { ... }
}if (TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd - 1)) { ... } else {
tcp_send_empty_ack(pcb);
}else { ... }
}if ((tcplen > 0) && (pcb->state < CLOSE_WAIT)) { ... } else {
/* ... */
if (!TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd - 1)) {
tcp_ack_now(pcb);
}if (!TCP_SEQ_BETWEEN(seqno, pcb->rcv_nxt, pcb->rcv_nxt + pcb->rcv_wnd - 1)) { ... }
}else { ... }
}{ ... }
static u8_t
tcp_get_next_optbyte(void)
{
u16_t optidx = tcp_optidx++;
if ((tcphdr_opt2 == NULL) || (optidx < tcphdr_opt1len)) {
u8_t *opts = (u8_t *)tcphdr + TCP_HLEN;
return opts[optidx];
}if ((tcphdr_opt2 == NULL) || (optidx < tcphdr_opt1len)) { ... } else {
u8_t idx = (u8_t)(optidx - tcphdr_opt1len);
return tcphdr_opt2[idx];
}else { ... }
}{ ... }
/* ... */
static void
tcp_parseopt(struct tcp_pcb *pcb)
{
u8_t data;
u16_t mss;
#if LWIP_TCP_TIMESTAMPS
u32_t tsval;
#endif
LWIP_ASSERT("tcp_parseopt: invalid pcb", pcb != NULL);
if (tcphdr_optlen != 0) {
for (tcp_optidx = 0; tcp_optidx < tcphdr_optlen; ) {
u8_t opt = tcp_get_next_optbyte();
switch (opt) {
case LWIP_TCP_OPT_EOL:
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: EOL\n"));
return;case LWIP_TCP_OPT_EOL:
case LWIP_TCP_OPT_NOP:
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: NOP\n"));
break;case LWIP_TCP_OPT_NOP:
case LWIP_TCP_OPT_MSS:
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: MSS\n"));
if (tcp_get_next_optbyte() != LWIP_TCP_OPT_LEN_MSS || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_MSS) > tcphdr_optlen) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
return;
}if (tcp_get_next_optbyte() != LWIP_TCP_OPT_LEN_MSS || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_MSS) > tcphdr_optlen) { ... }
mss = (u16_t)(tcp_get_next_optbyte() << 8);
mss |= tcp_get_next_optbyte();
pcb->mss = ((mss > TCP_MSS) || (mss == 0)) ? TCP_MSS : mss;
break;
#if LWIP_WND_SCALEcase LWIP_TCP_OPT_MSS:
case LWIP_TCP_OPT_WS:
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: WND_SCALE\n"));
if (tcp_get_next_optbyte() != LWIP_TCP_OPT_LEN_WS || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_WS) > tcphdr_optlen) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
return;
}if (tcp_get_next_optbyte() != LWIP_TCP_OPT_LEN_WS || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_WS) > tcphdr_optlen) { ... }
data = tcp_get_next_optbyte();
/* ... */
if ((flags & TCP_SYN) && !(pcb->flags & TF_WND_SCALE)) {
pcb->snd_scale = data;
if (pcb->snd_scale > 14U) {
pcb->snd_scale = 14U;
}if (pcb->snd_scale > 14U) { ... }
pcb->rcv_scale = TCP_RCV_SCALE;
tcp_set_flags(pcb, TF_WND_SCALE);
LWIP_ASSERT("window not at default value", pcb->rcv_wnd == TCPWND_MIN16(TCP_WND));
LWIP_ASSERT("window not at default value", pcb->rcv_ann_wnd == TCPWND_MIN16(TCP_WND));
pcb->rcv_wnd = pcb->rcv_ann_wnd = TCP_WND;
}if ((flags & TCP_SYN) && !(pcb->flags & TF_WND_SCALE)) { ... }
break;/* ... */
#endif
#if LWIP_TCP_TIMESTAMPScase LWIP_TCP_OPT_WS:
case LWIP_TCP_OPT_TS:
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: TS\n"));
if (tcp_get_next_optbyte() != LWIP_TCP_OPT_LEN_TS || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_TS) > tcphdr_optlen) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
return;
}if (tcp_get_next_optbyte() != LWIP_TCP_OPT_LEN_TS || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_TS) > tcphdr_optlen) { ... }
tsval = tcp_get_next_optbyte();
tsval |= (tcp_get_next_optbyte() << 8);
tsval |= (tcp_get_next_optbyte() << 16);
tsval |= (tcp_get_next_optbyte() << 24);
if (flags & TCP_SYN) {
pcb->ts_recent = lwip_ntohl(tsval);
/* ... */
tcp_set_flags(pcb, TF_TIMESTAMP);
}if (flags & TCP_SYN) { ... } else if (TCP_SEQ_BETWEEN(pcb->ts_lastacksent, seqno, seqno + tcplen)) {
pcb->ts_recent = lwip_ntohl(tsval);
}else if (TCP_SEQ_BETWEEN(pcb->ts_lastacksent, seqno, seqno + tcplen)) { ... }
tcp_optidx += LWIP_TCP_OPT_LEN_TS - 6;
break;/* ... */
#endif
#if LWIP_TCP_SACK_OUTcase LWIP_TCP_OPT_TS:
case LWIP_TCP_OPT_SACK_PERM:
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: SACK_PERM\n"));
if (tcp_get_next_optbyte() != LWIP_TCP_OPT_LEN_SACK_PERM || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_SACK_PERM) > tcphdr_optlen) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
return;
}if (tcp_get_next_optbyte() != LWIP_TCP_OPT_LEN_SACK_PERM || (tcp_optidx - 2 + LWIP_TCP_OPT_LEN_SACK_PERM) > tcphdr_optlen) { ... }
if (flags & TCP_SYN) {
tcp_set_flags(pcb, TF_SACK);
}if (flags & TCP_SYN) { ... }
break;/* ... */
#endif case LWIP_TCP_OPT_SACK_PERM:
default:
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: other\n"));
data = tcp_get_next_optbyte();
if (data < 2) {
LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
/* ... */
return;
}if (data < 2) { ... }
/* ... */
tcp_optidx += data - 2;default
}switch (opt) { ... }
}for (tcp_optidx = 0; tcp_optidx < tcphdr_optlen;) { ... }
}if (tcphdr_optlen != 0) { ... }
}{ ... }
void
tcp_trigger_input_pcb_close(void)
{
recv_flags |= TF_CLOSED;
}{ ... }
#if LWIP_TCP_SACK_OUT
/* ... */
static void
tcp_add_sack(struct tcp_pcb *pcb, u32_t left, u32_t right)
{
u8_t i;
u8_t unused_idx;
if ((pcb->flags & TF_SACK) == 0 || !TCP_SEQ_LT(left, right)) {
return;
}if ((pcb->flags & TF_SACK) == 0 || !TCP_SEQ_LT(left, right)) { ... }
/* ... */
for (i = unused_idx = 0; (i < LWIP_TCP_MAX_SACK_NUM) && LWIP_TCP_SACK_VALID(pcb, i); ++i) {
/* ... */
if (TCP_SEQ_LEQ(pcb->rcv_sacks[i].right, left) || TCP_SEQ_LEQ(right, pcb->rcv_sacks[i].left)) {
if (unused_idx != i) {
pcb->rcv_sacks[unused_idx] = pcb->rcv_sacks[i];
}if (unused_idx != i) { ... }
++unused_idx;
}if (TCP_SEQ_LEQ(pcb->rcv_sacks[i].right, left) || TCP_SEQ_LEQ(right, pcb->rcv_sacks[i].left)) { ... }
}for (i = unused_idx = 0; (i < LWIP_TCP_MAX_SACK_NUM) && LWIP_TCP_SACK_VALID(pcb, i); ++i) { ... }
/* ... */
for (i = LWIP_TCP_MAX_SACK_NUM - 1; i > 0; --i) {
/* ... */
if (i - 1 >= unused_idx) {
pcb->rcv_sacks[i].left = pcb->rcv_sacks[i].right = 0;
}if (i - 1 >= unused_idx) { ... } else {
pcb->rcv_sacks[i] = pcb->rcv_sacks[i - 1];
}else { ... }
}for (i = LWIP_TCP_MAX_SACK_NUM - 1; i > 0; --i) { ... }
pcb->rcv_sacks[0].left = left;
pcb->rcv_sacks[0].right = right;
}tcp_add_sack (struct tcp_pcb *pcb, u32_t left, u32_t right) { ... }
/* ... */
static void
tcp_remove_sacks_lt(struct tcp_pcb *pcb, u32_t seq)
{
u8_t i;
u8_t unused_idx;
/* ... */
for (i = unused_idx = 0; (i < LWIP_TCP_MAX_SACK_NUM) && LWIP_TCP_SACK_VALID(pcb, i); ++i) {
if (TCP_SEQ_GT(pcb->rcv_sacks[i].right, seq)) {
if (unused_idx != i) {
pcb->rcv_sacks[unused_idx] = pcb->rcv_sacks[i];
}if (unused_idx != i) { ... }
if (TCP_SEQ_LT(pcb->rcv_sacks[unused_idx].left, seq)) {
pcb->rcv_sacks[unused_idx].left = seq;
}if (TCP_SEQ_LT(pcb->rcv_sacks[unused_idx].left, seq)) { ... }
++unused_idx;
}if (TCP_SEQ_GT(pcb->rcv_sacks[i].right, seq)) { ... }
}for (i = unused_idx = 0; (i < LWIP_TCP_MAX_SACK_NUM) && LWIP_TCP_SACK_VALID(pcb, i); ++i) { ... }
for (i = unused_idx; i < LWIP_TCP_MAX_SACK_NUM; ++i) {
pcb->rcv_sacks[i].left = pcb->rcv_sacks[i].right = 0;
}for (i = unused_idx; i < LWIP_TCP_MAX_SACK_NUM; ++i) { ... }
}tcp_remove_sacks_lt (struct tcp_pcb *pcb, u32_t seq) { ... }
#if defined(TCP_OOSEQ_BYTES_LIMIT) || defined(TCP_OOSEQ_PBUFS_LIMIT)
/* ... */
static void
tcp_remove_sacks_gt(struct tcp_pcb *pcb, u32_t seq)
{
u8_t i;
u8_t unused_idx;
/* ... */
for (i = unused_idx = 0; (i < LWIP_TCP_MAX_SACK_NUM) && LWIP_TCP_SACK_VALID(pcb, i); ++i) {
if (TCP_SEQ_LT(pcb->rcv_sacks[i].left, seq)) {
if (unused_idx != i) {
pcb->rcv_sacks[unused_idx] = pcb->rcv_sacks[i];
}if (unused_idx != i) { ... }
if (TCP_SEQ_GT(pcb->rcv_sacks[unused_idx].right, seq)) {
pcb->rcv_sacks[unused_idx].right = seq;
}if (TCP_SEQ_GT(pcb->rcv_sacks[unused_idx].right, seq)) { ... }
++unused_idx;
}if (TCP_SEQ_LT(pcb->rcv_sacks[i].left, seq)) { ... }
}for (i = unused_idx = 0; (i < LWIP_TCP_MAX_SACK_NUM) && LWIP_TCP_SACK_VALID(pcb, i); ++i) { ... }
for (i = unused_idx; i < LWIP_TCP_MAX_SACK_NUM; ++i) {
pcb->rcv_sacks[i].left = pcb->rcv_sacks[i].right = 0;
}for (i = unused_idx; i < LWIP_TCP_MAX_SACK_NUM; ++i) { ... }
}tcp_remove_sacks_gt (struct tcp_pcb *pcb, u32_t seq) { ... }
/* ... */#endif
/* ... */
#endif
/* ... */
#endif