In the TCP/IP protocol, the TCP protocol provides a reliable connection service with a three-time handshake to establish a connection.
First handshake: When a connection is established, the client sends a SYN packet (SYN=X) to the server and enters the Syn_send state, waiting for the server to confirm;
Second handshake: The server receives the SYN packet, it must confirm the customer's SYN (ACK=X+1), and also send itself a SYN packet (syn=y), that is, the Syn+ack packet, when the server enters the SYN_RECV state;
Third handshake: The client receives the server's Syn+ack packet, sends the acknowledgment packet ack (ACK=Y+1) to the server, the packet is sent, the client and the server enter the established state, and the handshake is completed three times.
After three handshakes, the client and server begin transmitting data, and in the above process there are some important concepts:
Not connected queue: In the three-time handshake protocol, the server maintains an disconnected queue, which is listed as a SYN package (SYN=J) for each client, which indicates that the server has received a SYN packet and has issued a confirmation to the customer that it is waiting for the customer's confirmation package. The connections identified by these entries are in the SYN_RECV state of the server, and when the server receives the customer's confirmation package, the entry is deleted and the server enters the established state.
Backlog parameter: Represents the maximum number of seats that are not connected to a queue.
Syn-ack the number of times the server sends the Syn-ack package, if not received the customer confirmation package, the server for the first retransmission, waiting for a period of time still not received the customer confirmation package, the second retransmission, if the retransmission times than the system specified maximum retransmission times, the system will remove the connection information from the semi-connection queue. Note that the time to wait for each retransmission is not necessarily the same.
Half-Connection survival time: Refers to the maximum time that the entry of a semi-connected queue survives, that is, the maximum time that the service receives a SYN packet to confirm that the message is invalid, which is the maximum waiting time sum of all retransmission request packets. Sometimes we also call the half-connection survival time timeout time, syn_recv survival time.
State explanation
CLOSED: Indicates the initial state.
LISTEN:
Indicates that a socket on the server side is listening and can accept the connection
SYN_RCVD:
To accept the SYN message, under normal circumstances, this state is the server side of the socket in the establishment of a TCP connection during the three handshake session in the process of an intermediate state, very short, basically with netstat you are very difficult to see this state, unless you specifically wrote a client test program, Intentionally the last ACK message in the three-time TCP handshake is not sent. Therefore, when the ACK message is received from the client, it goes into the established state.
Syn_sent:
This state echoes the SYN_RCVD thinking back, when the client socket performs a connect connection, it sends the SYN message first, so it then enters the syn_sent state and waits for the server to send the 2nd message in the three-time handshake. The Syn_sent status indicates that the client has sent a SYN message.
Established: Indicates that the connection has been established
Fin_wait_1:
The true meaning of the fin_wait_1 and fin_wait_2 states is the fin message that waits for each other. The difference between the two states is: The fin_wait_1 state is actually when the socket in the established state, it would like to actively close the connection, send a FIN message to the other side, when the socket is entered into the fin_wait_1 state. And when the other party responds to the ACK message, then into the fin_wait_2 state, of course, under the actual normal circumstances, regardless of the circumstances of each other, should immediately respond to the ACK message, so fin_wait_1 state is generally more difficult to see, and Fin_wait_ 2 states can also sometimes be seen with netstat.
Fin_wait_2:
Fin_wait_2 State of the socket, indicating a semi-connection, also that one side requires close connection, but also tell the other side, I temporarily have some data to send to you, and then close the connection.
Time_wait:
Indicates received the other side's fin message, and sent out an ACK message, and so on 2MSL can return to the closed available state. If the fin_wait_1 state, received the other side with the FIN flag and the ACK flag message, you can directly into the time_wait state, without having to go through the fin_wait_2 state.
CLOSING:
Normally, after sending fin message, it is supposed to receive (or at the same time receive) the other party's ACK message, and then receive the other side's fin message. But closing status indicates that you send fin message, and did not receive the other's ACK message, but also received the other side of the fin message. Under what circumstances will this happen? In fact, it is not difficult to come to a conclusion: that is, if the two sides close a socket at the same time, then there is a situation where both sides send the fin message, there will be a closing state, indicating that both sides are shutting down the socket connection.
Close_wait:
Indicates that the wait is closed. How do you understand it? When the other side close a socket to send fin message to yourself, you will undoubtedly respond to an ACK message to each other, then enter into the close_wait state. Next, the real thing you really need to consider is whether you still have the data sent to the other person, if not, then you can close the socket, send fin messages to each other, that is, close the connection. So what you need to accomplish in the close_wait state is waiting for you to close the connection.
Last_ack:
The passive closing party waits for the other party's ACK message after sending the fin message. When an ACK message is received, it is also possible to enter the closed available state.
Tuning
Turn on SYN Cookies. When there is a SYN wait queue overflow, cookies are enabled to protect against a small number of SYN attacks, the default is 0, which means close;
Net.ipv4.tcp_syncookies = 1
Turn on reuse. Allows time-wait sockets to be re-used for new TCP connections, which defaults to 0, which means shutdown;
Net.ipv4.tcp_tw_reuse = 1
The fast recovery of time-wait sockets in TCP connection is turned on, the default is 0, which means close;
Net.ipv4.tcp_tw_recycle = 1
The system default timeout time.
Net.ipv4.tcp_fin_timeout = 5
When KeepAlive is employed, the frequency at which TCP sends keepalive messages. Default is 2 hours, change to 20 minutes (20*60s)
Net.ipv4.tcp_keepalive_time = 1200
Represents the range of ports used for an outward connection. Small by default: 32768 to 61000, 10000 to 65000
Net.ipv4.ip_local_port_range = 10000 65000
The length of the SYN queue, which defaults to 1024, increases the queue length to 8192, and can accommodate more network connections waiting to be connected
Net.ipv4.tcp_max_syn_backlog = 8192
The system maintains the maximum number of time_wait at the same time, and if this number is exceeded, time_wait is immediately cleared and the warning message is printed. Default is 180000, change to 5000
Net.ipv4.tcp_max_tw_buckets = 5000
Other tuning parameters
Tcp_syn_retries:integer
The default value is 5
For a new connection, the kernel will send a number of SYN connection requests before deciding to discard. should not be greater than 255, the default value is 5, which corresponds to about 180 seconds of time. (This value is high for networks with good physical communication for heavy loads and can be modified to 2. This value is only for external connections, the connection to come in, is determined by tcp_retries1)
Tcp_synack_retries:integer
The default value is 5
For a remote connection request SYN, the kernel sends a SYN + ACK datagram to confirm receipt of the last SYN connection request packet. This is the second step of the so-called three-time handshake (threeway handshake) mechanism. This determines the number of syn+ack that the kernel sends before discarding the connection. should not be greater than 255, the default value is 5, which corresponds to about 180 seconds of time. (This value can be determined according to the tcp_syn_retries above)
Tcp_keepalive_time:integer
The default value is 7200 (2 hours)
The frequency at which TCP sends keepalive messages when KeepAlive is turned on. (due to the current network attacks and other factors, resulting in the use of this attack is very frequent, has also been a friend of Cu mentioned, said if the 2 side to establish a connection, and then do not send any data or rst/fin messages, then the duration of the time is not 2 hours, an empty connection attack? tcp_keepalive_ Time is the prevention of this situation. I am personally in the NAT service when the change value is 1800 seconds)
Tcp_keepalive_probes:integer
The default value is 9
TCP sends a KeepAlive probe to determine the number of times that the connection has been disconnected. (Note: Keeping a connection is only sent if the so_keepalive socket option is turned on.) The number of times does not need to be modified by default, although this value can be shortened appropriately depending on the situation. Set to 5 more appropriate)
Tcp_keepalive_intvl:integer
The default value is 75
The frequency at which the message is sent is multiplied by tcp_keepalive_probes to get the time to kill the connection that has not responded since the start of the probe. The default value is 75 seconds, which means that no active connections will be discarded after approximately 11 minutes. (For normal applications, this value is somewhat larger and can be changed as needed.) in particular, the Web Class Server needs to be smaller, 15 is a more appropriate value)
Tcp_retries1:integer
The default value is 3
How many retries are required before giving up a response to a TCP connection request. The RFC specifies that the lowest value is 3,, which is also the default value, depending on the RTO value around 3 seconds – 8 minutes. (Note: This value also determines the incoming SYN connection)
Tcp_retries2:integer
The default value is 15
How many retries are required before a TCP connection that is activated (established communication state) is discarded. The default value is 15, which is determined by the RTO value, which is equivalent to 13-30 minutes (RFC1122, which must be greater than 100 seconds). (This value is based on the current network settings, can be appropriately changed to small, my network changes in order to 5)
Tcp_orphan_retries:integer
The default value is 7
How many retries to make before the near-end drops the TCP connection. The default value is 7, which is equivalent to 50 seconds – 16 minutes, depending on the RTO. If your system is a heavily loaded Web server, you may need to lower this value, which can be a lot of resource-intensive sockets. In addition, the examination Tcp_max_orphans. (In fact, when doing NAT, reducing this value is also a significant benefit, I myself in the network environment to reduce the value of 3)
Tcp_fin_timeout:integer
The default value is 60
For a socket connection that is disconnected at this end, TCP remains in the Fin-wait-2 state for the time. The other person may be disconnected or have not ended the connection or the unpredictable process has died. The default value is 60 seconds. It used to be 180 seconds in the 2.2 version of the kernel. You can set this value, but be aware that if your machine is a heavily loaded Web server, you may be risking the risk of memory being filled with a large number of invalid datagrams,fin-wait-2 sockets is less dangerous than fin-wait-1, because they eat up to 1.5K of memory, But they exist for a longer period of time. In addition refer to Tcp_max_orphans. (In fact, when doing NAT, reducing this value is also a significant benefit, I myself in the network environment to reduce the value of 30)
Tcp_max_tw_buckets:integer
The default value is 180000
The maximum number of timewait sockets the system is processing at the same time. If this number is exceeded, the,time-wait socket is immediately removed and a warning message is displayed. The reason to set this limit is to protect against simple DoS attacks, but do not artificially reduce this limit, but if the network conditions need to be more than the default value, you can increase it (and perhaps increase the memory). (In fact, it is best to increase the value appropriately when doing NAT)
Tcp_tw_recycle:boolean
The default value is 0
Turn on quick time-wait sockets recycling. Do not modify this value unless you are advised or requested by a technical expert. (It is recommended to open it when doing a NAT)
Tcp_tw_reuse:boolean
The default value is 0
This file indicates whether to allow the re-application of the socket in the time-wait state for a new TCP connection (this is very helpful in the case of a quick restart of some services and prompt port usage after startup)
Tcp_max_orphans:integer
The default value is 8192
The maximum number of TCP sockets that the system can handle that is not part of any process. If this amount is exceeded, then the connection that is not part of any process is immediately reset and a warning message is displayed. The reason to set this limit, purely to protect against those simple DoS attacks, do not rely on this or artificially reduce this limit (this value Redhat as version is set to 32768, but many firewall modifications, it is recommended that the value modified to 2000)
Tcp_abort_on_overflow:boolean
The default value is 0
When the daemon is too busy to accept the new connection, as if the other party sends the reset message, the default value is False. This means that when the cause of the overflow is due to an accidental burst, then the connection will revert to the state. This option is only turned on when you are sure that the daemon is really unable to complete the connection request, which affects the customer's use. (when dealing with services such as a fully loaded Sendmail,apache, this allows the client to terminate the connection quickly, allowing the service program to handle the existing connection buffer, so many firewalls recommend opening it)
Tcp_syncookies:boolean
The default value is 0
This will only occur if Config_syncookies is selected at kernel compile time. When the SYN wait queue appears overflow, the syncookies is sent like the other. The aim is to prevent SYN flood attacks.
Note: This option must not be used for high-load servers that do not receive an attack, if a Synflood message appears in the log, but the investigation finds that there is no synflood attack, but that the legitimate user's connection load is too high, you should adjust other parameters to improve server performance. Reference:
Tcp_max_syn_backlog
Tcp_synack_retries
Tcp_abort_on_overflow
Syncookie serious breaches of the TCP protocol, which does not allow the use of TCP extensions, may cause serious performance impacts (such as SMTP forwarding) on some services. (Note that this implementation, like the TCP proxy used by BSD above, is a violation of the RfC's three-time handshake for TCP connections, but is really useful for defense syn-flood.)
Tcp_stdurg:boolean
The default value is 0
Use the host Request interpretation feature in the TCP Urg pointer field. Most hosts use an old BSD explanation, so if you open it on Linux, you may not be able to communicate with them correctly.
Tcp_max_syn_backlog:integer
For connection requests that still do not have a client acknowledgement, the maximum number that needs to be saved in the queue. For systems that exceed 128Mb of memory, the default value is 1024, below 128Mb for 128. If the server is overloaded frequently, try increasing this number. Warning! If you set this value to greater than 1024, it is best to modify the tcp_synq_hsize, inside the include/net/tcp.h to keep tcp_synq_hsize*16<=tcp_max_syn_backlog, and into the core. (SYN flood attack uses TCP protocol to spread the defect of handshake, fake source IP address to send a large number of Tcp-syn semi-open connection to the target system, eventually causing the target system socket queue resources exhausted and unable to accept the new connection. In order to deal with this attack, modern UNIX systems generally use multi-connection queue processing to buffer (rather than resolve) this attack, is to use a basic queue to handle the normal full-connected application (connect () and accept ()), is to use another queue to separate semi-open connections. This dual-queue approach, combined with some other system kernel measures (such as syn-cookies/caches), can be used to mitigate small-scale SYN flood attacks more effectively (facts prove <1000p/s) Increasing the SYN queue length can accommodate more network connections waiting for the connection, so you might consider increasing the value for the server.
Tcp_window_scaling:integer
The default value is 1
This file indicates whether the sliding window size of the TCP/IP session is set to variable. The value of the parameter is a Boolean value, 1 is variable, and 0 indicates immutable. TCP/IP typically uses a maximum of 65535 bytes of Windows, which may be too small for high-speed networks, which, if enabled, can increase the TCP/IP sliding window size by several orders of magnitude, thereby increasing the ability to transmit data (RFC 1323). (for the average hundred m network, the shutdown will reduce the overhead, so if it is not a high-speed network, you can consider setting it to 0)
Tcp_timestamps:boolean
The default value is 1
Timestamps is used in other things to guard against forged sequence numbers. A 1G broadband line may re-encounter the old sequence number with out-of-line value (if it was due to the last generation). Timestamp will let it know it's an ' old bag '. (This file indicates whether it is enabled to enable calculation of RTT in a more precise way than a time-out, RFC 1323); This option should be enabled for better performance. )
Tcp_sack:boolean
The default value is 1
Using selective ack, It can be used to find a specific missing datagram-so it helps to recover the status quickly. This file indicates whether a selective answer (selective acknowledgment) is enabled, which can improve performance by selectively answering packets received by a random order (which allows the sender to send only the missing segment). (This option should be enabled for WAN traffic, but this increases CPU usage.) )
Tcp_fack:boolean
The default value is 1
Open Fack congestion avoidance and fast retransmission function. (Note that this value, even if set to 1, is not valid when Tcp_sack is set to 0)
Tcp_dsack:boolean
The default value is 1
Allow TCP to send "two identical" sack.
Explanation of TCP parameter tuning