| 1 |
# Rate limit |
| 2 |
|
| 3 |
|
| 4 |
## global_rate |
| 5 |
|
| 6 |
Defines global rate limit without source or destination IP address check. For |
| 7 |
example `https global_rate "10/second burst 20"` allows https traffic with |
| 8 |
rate: |
| 9 |
|
| 10 |
* First 20 new connections are allowed without limit. |
| 11 |
* After burst is filled, up to 10 new connections per second are allowed. |
| 12 |
* This rate is "global", meaning that one single source IP can use all allowed |
| 13 |
slots, or 20 sources can use one slot each. |
| 14 |
* Here "connection" means new connection, not total number of established, |
| 15 |
active connections (see `ct count` below). |
| 16 |
|
| 17 |
There are three types of rates: |
| 18 |
|
| 19 |
* New connection rates (`x/time burst y`), ignoring if some of |
| 20 |
them have already been closed. |
| 21 |
|
| 22 |
* `"7/second burst 30"` |
| 23 |
* `"3/second"` (no burst specified, so `burst 5` is assumed) |
| 24 |
* `"1/minute burst 1"` |
| 25 |
* `"50/minute burst 200"` |
| 26 |
* `"30/minute"` |
| 27 |
* `"100/hour burst 200"` |
| 28 |
* `"100/hour"` |
| 29 |
* `"over 8/second burst 10"` |
| 30 |
|
| 31 |
* Bandwidth rates (`x bytes/time burst y bytes`): |
| 32 |
|
| 33 |
* `"10 mbytes/second burst 12000 kbytes"` |
| 34 |
* `"10 mbytes/second"` |
| 35 |
* `"over 10 mbytes/second burst 12000 kbytes"` |
| 36 |
* `"over 10 mbytes/second"` |
| 37 |
|
| 38 |
* Conntrack rates (`ct count x`) counting total number of established, active |
| 39 |
connections: |
| 40 |
|
| 41 |
* `"ct count 5"` |
| 42 |
* `"ct count over 6"` |
| 43 |
|
| 44 |
New connection rate limit can be visualized as leaking water bucket. |
| 45 |
`burst 10` specifies the bucket size as 10 units. `3/minute` specifies how |
| 46 |
much it leaks, 3 units per minute, or 1 unit per every 20 seconds. Every new |
| 47 |
connection adds one unit of water to it. If it fits, rule matches (usually: |
| 48 |
connection is accepted). If the bucket overflows, rule doesn't match. |
| 49 |
|
| 50 |
If burst is not specified, Linux kernel assumes value 5 for it. Minimum value |
| 51 |
for burst is 1. |
| 52 |
|
| 53 |
Some examples: |
| 54 |
|
| 55 |
* `5/minute burst 1` allows one new connection (`burst 1`) and new connection |
| 56 |
every 12 second (`5/minute`). |
| 57 |
* `3/minute burst 5` allows up to 5 new connections and new connection every |
| 58 |
20 second after those 5 connections are used. |
| 59 |
|
| 60 |
Bandwidth rate can be used as simple traffic limiter. This can be specified |
| 61 |
per service, or total for all. Example: |
| 62 |
|
| 63 |
``` |
| 64 |
public-localhost { |
| 65 |
# Limit all incoming traffic to 30 MiB/s, total for all traffic |
| 66 |
global_rate "over 30 mbytes/second" drop -conntrack |
| 67 |
|
| 68 |
# Limit incoming SSH traffic to 2 MiB/s, per service traffic |
| 69 |
ssh global_rate "over 2 mbytes/second" drop -conntrack |
| 70 |
|
| 71 |
# Allow incoming SSH connections |
| 72 |
ssh |
| 73 |
... |
| 74 |
} |
| 75 |
``` |
| 76 |
|
| 77 |
Rate `ct count 5` matches if there are up to 5 established connections, |
| 78 |
including current one. So rule `ssh global_rate "ct count 2"` allows two |
| 79 |
SSH connections (one old + current). |
| 80 |
|
| 81 |
Rate `ct count over 6` matches if there are more than 6 established |
| 82 |
connections. This is usually used with `drop` statement: `ssh global_rate |
| 83 |
"ct count over 6" drop`. |
| 84 |
|
| 85 |
|
| 86 |
## saddr_rate, daddr_rate |
| 87 |
|
| 88 |
Source / destination IP address specific rate limit is similar to |
| 89 |
`global_rate`. |
| 90 |
|
| 91 |
`saddr_rate` allows limited amount of connections or bandwidth from single |
| 92 |
source IP address, but there is no total (global) limit. |
| 93 |
|
| 94 |
`daddr_rate` allows limited amount of connections or bandwidth for single |
| 95 |
destination IP address, which is usually some host in `dmz` zone. |
| 96 |
|
| 97 |
Both `saddr_rate` and `daddr_rate` can be specified for single rule. For |
| 98 |
example `https saddr_rate "10/second" daddr_rate "1000/second"` specifies: |
| 99 |
|
| 100 |
* Single IP can open 10 new connections per second. |
| 101 |
* Total of 1000 connection per second from all IPs are allowed. |
| 102 |
|
| 103 |
|
| 104 |
## saddr_rate_mask, daddr_rate_mask |
| 105 |
|
| 106 |
Normally full IPv4 or IPv6 addresses are considered when counting `saddr_rate` |
| 107 |
or `daddr_rate`. This can be changed with netmask: `ping saddr_rate "5/second |
| 108 |
burst 20" saddr_rate_mask 24 56` uses <IPv4-address>/24 and |
| 109 |
<IPv6-address>/56 instead of full IP address when counting limits. |
| 110 |
|
| 111 |
|
| 112 |
## saddr_rate_name, daddr_rate_name |
| 113 |
|
| 114 |
If you want to share same rate limit with two different rules you must |
| 115 |
specify a name for it. For example: |
| 116 |
|
| 117 |
``` |
| 118 |
http saddr_rate "30/second burst 50" saddr_rate_name http_limit |
| 119 |
https saddr_rate "30/second burst 50" saddr_rate_name http_limit |
| 120 |
``` |
| 121 |
|
| 122 |
This counts both http and https traffic as single, allowing total of 30 |
| 123 |
connections per IP per second. Without name it would allow 30 + 30 |
| 124 |
connections per IP per second. |
| 125 |
|
| 126 |
|
| 127 |
## saddr_daddr_rate, saddr_daddr_rate_mask, saddr_daddr_rate_name |
| 128 |
|
| 129 |
This is special form of `saddr_rate + daddr_rate`, usable when you have |
| 130 |
multiple destination IPs per service (DNS round-robin). In this rule |
| 131 |
both source and destination address are counted as check key, instead of only |
| 132 |
source or destination. |