These days, the hashlimit module has been used in the firewall. Google found that there were very few documents in both English and Chinese, so I will record my tossing experiences. Hashlimit is a matching module of iptables. It can be used with other commands of iptables to implement the speed limit function. (Note: Separate hashlimit modulo
These days, the hashlimit module has been used in the firewall. Google found related documents in a circle whether in English or not
Is rare in Chinese,
So I will record my own tossing experiences.
Hashlimit is a matching module of iptables.CommandSpeed Limit
. (Note: Separate hashlimit Module
).
However, it must be clarified that hashlimit itself is only a "matching" module. We know that the basic principle of iptables
The reason is "match -- process", and hashlimit is
This process can only play a matching role, and it itself cannot process network packets. I see
In some examples of hashlimit on the internet, only
You can use an iptables statement that contains the hashlimit matching rule to limit the speed. This is incorrect.
In fact, two steps are required to use hashlimit to limit the speed.
1. Release the hashlimit matching rule package
2. Discard/reject unreleased packages
The following is a simple example:
Iptables-a input-p tCp-- Dport 22-m hashlimit -- hashlimit-name ssh
-- Hashlimit 5/sec -- hashlimit-burst 10 -- hashlimit-mode srcip
-- Hashlimit-htable-ExPire 90000-j ACCEPT
Iptables-a input-p tcp -- dport 22-j DROP
Next, let's focus on how the hashlimit module works. The matching of hashlimit is based on the token bucket.
(Token bucket) model. The token bucket is a common buffer Working Principle in network communication. It has two important
The token bucket capacity n and the Token Generation Rate s. We can regard the token as a ticket, while the token bucket is the responsibility system.
The administrator who works as and issues tickets, and has a maximum of n tokens. At first, the administrator started to have n tokens. Every time
When a data packet arrives, the Administrator will check whether there is a token available in his hand. If yes, send the token to this number.
Hashlimit tells iptables that the packet is matched. When the administrator handles all the tokens
After the packet is sent, the token cannot be obtained. In this case, the hashlimit module tells iptables that the data
The package cannot be matched.
In addition to issuing tokens, as long as the number of tokens in the token bucket is less than n, it will generate a new token at the rate of s
The number of tokens reaches n.
Through the token bucket mechanism, you can effectively control the number of packets that pass (matching) within a unit time, and allow short
A large number of burst packets pass in time (as long as the number of packets does not exceed the token bucket n ).
The hashlimit module provides two parameters: hashlimit and -- hashlimit-burst, which correspond to the Token Generation Rate respectively.
Rate and token bucket capacity.
In addition to the token bucket model, another important concept of hashlimit matching is matching. In hashlimit
The configuration item has a separate token bucket for independent matching calculation. Use the -- hashlimit-mode parameter of hashlimit
Number, you can specify four matching items and their combinations, namely: srcip (each source IP address is a match), dstip (
Each destination IP address is a matched item), sRcpOrt (each source port is a match), dstport (each object
)
In addition to the preceding three parameters, hashlimit also has a required parameter -- hashlimit-name.
Hashlimit will be in the/proc/net/ipt_hashlimit directory, for each iptables that calls the hashlimit Module
Command to create a file that stores information about each matching item. The -- hashlimit-name parameter is used to specify the file.
File Name.
Now, we have introduced the working principle and corresponding parameters of hashlimit. Let's take a few examples.
The first is the previous example:
Iptables-a input-p tcp -- dport 22-m hashlimit -- hashlimit-name ssh