Skip to content

Commit e371137

Browse files
authored
Merge pull request #1289 from AndreasAakesson/dev
tcp: Now sends resets on invalid packets
2 parents 63bb70c + 7888ebe commit e371137

4 files changed

Lines changed: 49 additions & 5 deletions

File tree

api/net/tcp/tcp.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,21 @@ namespace net {
537537
*/
538538
void transmit(tcp::Packet_ptr);
539539

540+
/**
541+
* @brief Creates an outgoing TCP packet.
542+
*
543+
* @return A tcp packet ptr
544+
*/
545+
tcp::Packet_ptr create_outgoing_packet();
546+
547+
/**
548+
* @brief Sends a TCP reset based on the values of the incoming packet.
549+
* Used when packet are addressed to closed ports or already dead connections.
550+
*
551+
* @param[in] incoming The incoming tcp packet "to reset".
552+
*/
553+
void send_reset(const tcp::Packet& incoming);
554+
540555
/**
541556
* @brief Generate a unique initial sequence number (ISS).
542557
*

src/net/tcp/connection.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,9 @@ Connection::~Connection() {
318318
rtx_clear();
319319
}
320320

321-
Packet_ptr Connection::create_outgoing_packet() {
322-
auto packet = static_unique_ptr_cast<net::tcp::Packet>((host_.inet_).create_packet());
323-
//auto packet = std::static_pointer_cast<TCP::Packet>(create_packet());
324-
325-
packet->init();
321+
Packet_ptr Connection::create_outgoing_packet()
322+
{
323+
auto packet = host_.create_outgoing_packet();
326324
// Set Source (local == the current connection)
327325
packet->set_source(local());
328326
// Set Destination (remote)

src/net/tcp/listener.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ void Listener::segment_arrived(Packet_ptr packet) {
6262
// if it's a new attempt (SYN)
6363
else
6464
{
65+
// don't waste time if the packet does not have SYN
66+
if(UNLIKELY(not packet->isset(SYN)))
67+
{
68+
host_.send_reset(*packet);
69+
return;
70+
}
71+
6572
// Stat increment number of connection attempts
6673
host_.connection_attempts_++;
6774

src/net/tcp/tcp.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ void TCP::receive(net::Packet_ptr packet_ptr) {
202202
return;
203203
}
204204

205+
// Send a reset
206+
send_reset(*packet);
207+
205208
drop(*packet);
206209
}
207210

@@ -263,6 +266,27 @@ void TCP::transmit(tcp::Packet_ptr packet) {
263266
_network_layer_out(std::move(packet));
264267
}
265268

269+
tcp::Packet_ptr TCP::create_outgoing_packet()
270+
{
271+
auto packet = static_unique_ptr_cast<net::tcp::Packet>(inet_.create_packet());
272+
packet->init();
273+
return packet;
274+
}
275+
276+
void TCP::send_reset(const tcp::Packet& in)
277+
{
278+
// TODO: maybe worth to just swap the fields in
279+
// the incoming packet and send that one
280+
auto out = create_outgoing_packet();
281+
// increase incoming SEQ and ACK by 1 and set RST + ACK
282+
out->set_seq(in.ack()+1).set_ack(in.seq()+1).set_flags(RST | ACK);
283+
// swap dest and src
284+
out->set_source(in.destination());
285+
out->set_destination(in.source());
286+
287+
transmit(std::move(out));
288+
}
289+
266290
seq_t TCP::generate_iss() {
267291
// Do something to get a iss.
268292
return rand();

0 commit comments

Comments
 (0)