The Linux kernel implements syn-Cookie, which can effectively prevent syn-flood attacks. syn-cookie is theoretically very simple, that is, it does not allocate any memory space when the server receives the SYN packet from the client, instead, it cleverly selects the ISN value of the server and transmits it to the client. The ISN is not saved locally (nothing is saved locally). After the client sends the ACK confirmation package of synack, the confirmation number is extracted from the confirmation package, and then the sequence number of the synack package sent from the original server to the client is obtained by using the last serial number plus 1 feature, and then the same source is used, the destination address/port information is calculated using the same algorithm to compare a number with the obtained original serial number. If the information is the same, the connection is valid. Otherwise, the connection is invalid and the timeout mechanism is used during transmission. The implementation of Linux is basically similar to the theory. It only saves the MSS value in the ISN obtained by using the sha1 algorithm. This is an extra benefit, and it also limits the shortest time limit between two handshakes:
_ U32 cookie_v4_init_sequence (struct sock * SK, struct sk_buff * SKB, _ 2010* mssp)
{
Struct tcp_sock * TP = tcp_sk (SK );
Int mssind;
Const _ 2010mss = * mssp;
TP-> last_synq_overflow = jiffies;
For (mssind = 0; MSS> msstab [mssind + 1]; mssind ++ );
* Mssp = msstab [mssind] + 1;
Net_inc_stats_bh (linux_mib_syncookiessent); // The following calls the function compute abstract and sends it as the server serial number.
Return secure_tcp_syn_cookie (SKB-> NH. IPH-> saddr, SKB-> NH. IPH-> daddr, SKB-> H. th-> source, SKB-> H. th-> DEST,
Ntohl (SKB-> H. Th-> seq), jiffies/(HZ * 60), mssind );
}
Static _ u32 secure_tcp_syn_cookie (_ u32 saddr, _ u32 daddr, _ ports, _ ports dport, _ u32 sseq, _ u32 count, _ u32 data)
{
// Count
Return (cookie_hash (saddr, daddr, sport, dport, 0, 0) + sseq + (count
(Cookie_hash (saddr, daddr, sport, dport, Count, 1) + Data) & cookiemask ));
}
Static inline int cookie_check (struct sk_buff * SKB, _ u32 cookie)
{
_ U32 seq;
_ U32 mssind;
SEQ = ntohl (SKB-> H. Th-> SEQ)-1;
Mssind = check_tcp_syn_cookie (cookie,
SKB-> NH. iph-> saddr, SKB-> NH. iph-> daddr,
SKB-> H. Th-> source, SKB-> H. Th-> DEST,
Seq, jiffies/(HZ * 60), counter_tries );
Return mssind
}
Static _ u32 check_tcp_syn_cookie (_ u32 cookie, _ u32 saddr, _ u32 daddr, _ ports Port, _ u32 sseq, _ u32 count, _ u32 axdiff)
{
_ U32 diff;
Cookie-= cookie_hash (saddr, daddr, sport, dport, 0, 0) + sseq;
// After the calculation above, the cookie is left with a maximum of 8 characters, namely the number of minutes and the Sha digest value calculated by the address, port, and time, and an additional MSS value, the following diff calculates the difference between the current time and the time obtained through the cookie. Cookie> cookiebits obtains the Cookie's 8-bit height, and the last & Operation screen drops an unrelated high position, but there is a problem, see below.
Diff = (count-(cookie> cookiebits) & (_ u32)-1> cookiebits );
If (diff> = maxdiff) // This maxdiff is counter_tries, which is calculated through the TCP timeout mechanism.
Return (_ u32)-1;
// The original MSS value is obtained through the following subtraction. Diff must be smaller than maxdiff, that is, 4. Then, the difference between count and count is the original time, which is measured in minutes.
Return (cookie-cookie_hash (saddr, daddr, sport, dport, Count-diff, 1) & cookiemask;
}
The above algorithms are not perfect. The problem lies in the diff calculation. If the current Count value is very different from the Count value when the cookie was generated in the past, for example, the current count is 11111111111111111111111100000011, the original value is 00000000000000000000000000000001. In this way, the diff formula is used to calculate 2, which is exactly the same. Then, the following cookie_hash is calculated. If Sha hits again, the system will get the wrong result, however, even if an error occurs, there is no major problem. After all, the chance of an error is very small, so the syn-flood attack still fails. Furthermore, the diff check here is not strict, there is a ShA computing behind it, and the second is that TCP itself is a timeout mechanism, so long time-out has passed, so there will be no such package back. In addition, even if there is no collision, the Sha abstract changes due to changes such as count or other ADDR or port, but the changes are very small, the value within the valid range after being subtracted from the cookie will pass verification. However, you do not have to worry about this situation. The error rate is very low, and the probability of close summary is very low, therefore, such verification is credible. Do not lose efficiency when designing a system.
The syn-cookie is not enabled only when a client connection is established, because syn-cookie is time-consuming and violates the TCP principle, see the following code snippet. This is the code snippet executed when the TCP server replies to the SYN Packet during the three-way handshake:
If (inet_csk_reqsk_queue_is_full (SK )&&! ISN ){
# Ifdef config_syn_cookies
If (sysctl_tcp_syncookies ){
Want_cookie = 1;
} Else
# Endif
Goto drop;
}
...
If (want_cookie ){
ISN = cookie_v4_init_sequence (SK, SKB, & req-> MSS );
}
This feature is enabled only when the server's accept_queue is fully loaded. It can be seen that laziness is very effective for efficiency improvement and can effectively prevent syn-flood attacks. You can attack it as you like. This Linux doesn't matter, linux is not afraid of DoS attacks, nor will it take any action in advance. Everything is like nothing. However, once the attack begins to affect the system, the receiving queue is full, linux is about to handle it. Start the emergency plan. Here is syn-Cookie.
In Linux, how does one implement syn-cookie? Actually, it is calculated using the sha1 Digest algorithm. Next, let's take a look at this algorithm:
# Define sha_digest_words 5 // The sha1 initial summary has a total of five 32-bit Space
# Define sha_workspace_words 80 // It takes 80 32-bit buffer space to compute a digest
Static _ u32 syncookie_secret [2] [16-3 + sha_digest_words]; // a total of 2*21*4 bytes of space
Static u32 cookie_hash (u32 saddr, u32 daddr, u32 sport, u32 dport, u32 count, int C)
{
_ U32 TMP [16 + 5 + sha_workspace_words];
Memcpy (TMP + 3, syncookie_secret [c], sizeof (syncookie_secret [c]);
TMP [0] = saddr;
TMP [1] = daddr;
TMP [2] = (sport
TMP [3] = count;
// Fill Sha's word space from the offset of TMP 21. The filled content is the first 16 elements of TMP.
// A total of 16 + 5 + 80 32-bit space, the first 16 are the existing data space, and the next 5 are the Sha output buffer, note that the five spaces have been filled with some syncookie_secret. Because the length of a syncookie_secret element is 18 and it is filled with the third TMP space, it is extended to 21st spaces, from the second space to the Sha computing buffer.
Sha_transform (TMP + 16, (_ u8 *) TMP, TMP + 16 + 5 );
Return TMP [17]; // for tcp syn-cookie, you only need to pick one.
}
The core of the Sha algorithm is that the digest parameter is the digest information pointer. In fact, the first five unsigned 32-bit data are required. In is the data that needs to be computed for the digest, and the last W is the computing space buffer, the parameters digest, In, and dynamic computation are filled in:
Void sha_transform (_ u32 * digest, const char * In, _ u32 * W)
{
_ U32 A, B, C, D, E, T, I;
For (I = 0; I
W [I] = be32_to_cpu (const _ be32 *) in) [I]);
For (I = 0; I
W [I + 16] = rol32 (W [I + 13] ^ W [I + 8] ^ W [I + 2] ^ W [I], 1 );
A = digest [0]; // these operations are value assignment operations that assign data buffers to temporary variables.
B = digest [1]; // a total of five 32-bit Buffers
C = digest [2]; // for syn-Cookie, this digest is used to call the TMP array of the function starting from the 16 subscript and has been overwritten by syncookie_secret.
D = digest [3];
E = digest [4]; // start calculation and segment Calculation
For (I = 0; I
T = F1 (B, c, d) + k1 + rol32 (A, 5) + E + W [I];
E = D; D = C; C = rol32 (B, 30); B = A; A = T;
}
For (; I
T = F2 (B, c, d) + k2 + rol32 (A, 5) + E + W [I];
E = D; D = C; C = rol32 (B, 30); B = A; A = T;
}
For (; I
T = F3 (B, c, d) + K3 + rol32 (A, 5) + E + W [I];
E = D; D = C; C = rol32 (B, 30); B = A; A = T;
}
For (; I
T = F2 (B, c, d) + K4 + rol32 (A, 5) + E + W [I];
E = D; D = C; C = rol32 (B, 30); B = A; A = T;
}
Digest [0] + = A; // output data
Digest [1] + = B;
Digest [2] + = C;
Digest [3] + = D;
Digest [4] + = E;
}