Skip to content

Commit 3689107

Browse files
Aska WuAnas Nashif
authored andcommitted
net: tcp: First check sequence number
Previously, the connection will be reset easily due to a forged TCP reset with a random sequence number. As described in RFC793 p.69, we should check if the sequence number falls into the receiver window at first. Signed-off-by: Aska Wu <aska.wu@linaro.org>
1 parent ba50292 commit 3689107

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

subsys/net/ip/net_context.c

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,28 @@ NET_CONN_CB(tcp_established)
11161116

11171117
tcp_flags = NET_TCP_FLAGS(tcp_hdr);
11181118

1119+
if (net_tcp_seq_cmp(sys_get_be32(tcp_hdr->seq),
1120+
context->tcp->send_ack) < 0) {
1121+
/* Peer sent us packet we've already seen. Apparently,
1122+
* our ack was lost.
1123+
*/
1124+
1125+
/* RFC793 specifies that "highest" (i.e. current from our PoV)
1126+
* ack # value can/should be sent, so we just force resend.
1127+
*/
1128+
send_ack(context, &conn->remote_addr, true);
1129+
return NET_DROP;
1130+
}
1131+
1132+
if (net_tcp_seq_cmp(sys_get_be32(tcp_hdr->seq),
1133+
context->tcp->send_ack) > 0) {
1134+
/* Don't try to reorder packets. If it doesn't
1135+
* match the next segment exactly, drop and wait for
1136+
* retransmit
1137+
*/
1138+
return NET_DROP;
1139+
}
1140+
11191141
/*
11201142
* If we receive RST here, we close the socket. See RFC 793 chapter
11211143
* called "Reset Processing" for details.
@@ -1183,27 +1205,6 @@ NET_CONN_CB(tcp_established)
11831205
context->tcp->fin_rcvd = 1;
11841206
}
11851207

1186-
if (net_tcp_seq_cmp(sys_get_be32(tcp_hdr->seq),
1187-
context->tcp->send_ack) < 0) {
1188-
/* Peer sent us packet we've already seen. Apparently,
1189-
* our ack was lost.
1190-
*/
1191-
1192-
/* RFC793 specifies that "highest" (i.e. current from our PoV)
1193-
* ack # value can/should be sent, so we just force resend.
1194-
*/
1195-
send_ack(context, &conn->remote_addr, true);
1196-
return NET_DROP;
1197-
}
1198-
1199-
if (sys_get_be32(tcp_hdr->seq) - context->tcp->send_ack) {
1200-
/* Don't try to reorder packets. If it doesn't
1201-
* match the next segment exactly, drop and wait for
1202-
* retransmit
1203-
*/
1204-
return NET_DROP;
1205-
}
1206-
12071208
set_appdata_values(pkt, IPPROTO_TCP);
12081209

12091210
data_len = net_pkt_appdatalen(pkt);

0 commit comments

Comments
 (0)