All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: "Kamil Jońca" <kjonca@op.pl>
Cc: netfilter@vger.kernel.org
Subject: Re: how to use meters?
Date: Mon, 19 Sep 2022 10:47:47 +0200	[thread overview]
Message-ID: <Yygss6Pkwuwgmy4o@salvia> (raw)
In-Reply-To: <871qs9neip.fsf@alfa.kjonca>

On Sun, Sep 18, 2022 at 12:49:34PM +0200, Kamil Jońca wrote:
[...]
> For example:
> https://wiki.archlinux.org/title/Nftables#Dynamic_blackhole
> --8<---------------cut here---------------start------------->8---
>  ct state new tcp dport 443 \
>                 meter flood size 128000 { ip saddr timeout 10s limit rate over 10/second } \
>                 add @blackhole { ip saddr timeout 1m }
> --8<---------------cut here---------------end--------------->8---
> 
> I understand " add @blackhole { ip saddr timeout 1m }" - adds address to
> set for 1 min.
> but what is
> "meter flood size 128000 { ip saddr timeout 10s limit rate over 10/second }"
> 
> (I can guess but I cannot see proper doc of this)
> Any hint?

I'd suggest you use a set declaration for this, instead of the meter syntax.

This example shows how to ratelimit new connections to 10 per second:

table inet global {
    set flood {
        type ipv4_addr
        flags dynamic
        timeout 1m
        limit rate over 10/second
        size 65536
    }

    chain input {
        type filter hook prerouting priority filter; policy drop;
        ct state new tcp dport 443 update @flood { ip saddr } drop
        counter accept
    }
}

This declares a dynamic 'flood' set that stores IPv4 addresses. The
limit rate is also done from the set declaration itself. If the client
goes over the threshold, the packet is dropped.


Now, going back to "drop all HTTPS connections for 1 minute from a
source IP that exceeds the limit of 10/second", let's update the
previous example incrementally with an explicit set declaration:

table inet global {
    set flood {
        type ipv4_addr
        flags dynamic
        timeout 1m
        limit rate over 10/second
        size 65536
    }

    set blocklist {
        type ipv4_addr
        flags dynamic
        timeout 1m
        size 65536
    }

    chain input {
        type filter hook prerouting priority filter; policy drop;
        ct state new tcp dport 443 update @flood { ip saddr } add @blocklist { ip saddr }
        ip saddr @blocklist counter drop
        counter accept
    }
}

the 'flood' set keeps track of the specified ratelimit for each IP
address, if the ratelimit threshold is hit, then the IP address is
added to the blocklist. After 1 minute, the IP address in the
blocklist is removed.

  reply	other threads:[~2022-09-19  8:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-18 10:49 how to use meters? Kamil Jońca
2022-09-19  8:47 ` Pablo Neira Ayuso [this message]
2022-09-19 10:00   ` Kamil Jońca

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Yygss6Pkwuwgmy4o@salvia \
    --to=pablo@netfilter.org \
    --cc=kjonca@op.pl \
    --cc=netfilter@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.