Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FIN vs RST in TCP connections

Tags:

networking

tcp

The way I understand this, there are 2 ways to close TCP connection:

  • send FIN flag
  • send RST flag

RST causes immediate connection termination, while in FIN you get a confirmation.

Do I understand this right, and are there any other distinctions between the two? Can those 2 flags be used together?

like image 914
Arsen Zahray Avatar asked Oct 24 '12 13:10

Arsen Zahray


People also ask

What is the difference between RST and fin?

RST causes immediate connection termination, while in FIN you get a confirmation.

What is RST and FIN packet?

FIN Packet vs RST Packet Comparison – In case of FIN hosts get a confirmation. RST causes immediate connection termination without any confirmation. Condition For Sending. Fin is sent when the application tells TCP that it wants to close so TCP does 4 way handshake and closes the connection gracefully.

What is FIN in TCP?

The FIN flag indicates the end of data transmission to finish a TCP connection. Their purposes are mutually exclusive.

What is RST in TCP connection?

A TCP Reset (RST) packet is used by a TCP sender to indicate that it will neither accept nor receive more data. Out-of-path network management devices may generate and inject TCP Reset packets in order to terminate undesired connections.


2 Answers

  • FIN says: "I finished talking to you, but I'll still listen to everything you have to say until you say that you're done."

  • RST says: "There is no conversation. I won't say anything and I won't listen to anything you say."

    RST is useful if you have long lasting TCP connection with little traffic. If one of the computers is restarted, it forgets about the connection, and the other computer gets RST, as soon as it sends another packet.

like image 199
escitalopram Avatar answered Sep 28 '22 03:09

escitalopram


FIN or RST would be sent in the following case

  • your process close the socket
  • OS is doing the resource cleanup when your process exit without closing socket.

    If your process call close(), FIN would be sent from the closing side by default (note: you can set socket option SO_LINGER to make it send RST instead of FIN)

    If your process exit without closing the socket, kernel would close the tcp connection and do the clean up for your process. FIN or RST can be sent. If there is data in your receive queue, RST would be sent. Otherwise, FIN would be sent.

    You can loop through tcp_close() in tcp.c for more details.(I am using kernel-2.6.32-573.7.1 from redhat branch)

like image 39
Ben Avatar answered Sep 28 '22 02:09

Ben