Skip to content

Rate Limiting Configuration

The [dns.rate_limit] section configures per-subnet token-bucket rate limiting and per-IP connection limits for TCP/DoT.


Overview

Ferrous DNS rate limiting protects against DNS query floods and DoS attacks while allowing legitimate traffic through. It works at three levels:

  1. Query rate limiting — token bucket per client subnet, one for UDP and one for the stream transports (TCP, DoT, DoH, DoQ)
  2. NXDOMAIN budget — separate stricter limit that applies only to NXDOMAIN answers
  3. Connection limiting — per-IP caps on concurrent TCP and DoT connections

When disabled (enabled = false), rate limiting adds zero overhead to query processing.


Full Configuration

ferrous-dns.toml
[dns.rate_limit]
enabled                    = true       # master switch
queries_per_second         = 1000       # sustained QPS per subnet
burst_size                 = 500        # token bucket capacity
ipv4_prefix_len            = 24         # /24 = class C network
ipv6_prefix_len            = 48         # /48 = standard home delegation
whitelist                  = [          # bypass rate limiting
    "127.0.0.0/8",
    "::1/128",
    "10.0.0.0/8",
    "172.16.0.0/12",
    "192.168.0.0/16"
]
nxdomain_per_second        = 50         # stricter NXDOMAIN budget
slip_ratio                 = 2          # TC=1 slip frequency
dry_run                    = false      # true = log only
stale_entry_ttl_secs       = 300        # idle bucket eviction
tcp_max_connections_per_ip = 30         # TCP connection limit
dot_max_connections_per_ip = 15         # DoT connection limit
doq_max_connections_per_ip = 15         # DoQ connection limit

Token Bucket Algorithm

Each client subnet gets an independent token bucket for UDP and another for the stream transports (TCP, DoT, DoH and DoQ), both with the same settings:

  • Tokens refill at queries_per_second rate
  • Maximum capacity is burst_size
  • Each query consumes one token
  • When empty, UDP queries are either refused or slipped (TC=1); stream queries are refused
Subnet 10.0.0.0/24:
  [==========------]  burst_size = 500
       ↑ 350 tokens remaining

  Refill: +1000 tokens/sec (capped at 500)
  Consume: -1 token per query

Subnet grouping

All clients within the same subnet share its buckets, one for UDP and one for the stream transports. With ipv4_prefix_len = 24, all devices in 192.168.1.0/24 share them. This prevents a single device from consuming the entire budget.

Why the stream transports have a bucket of their own

A spoofed-source flood can only use UDP: TCP, TLS and QUIC need a handshake from the real address. Keeping the stream budget apart means such a flood never drains it, so the TCP retry of a slipped query is answered, while a stream flood from a real address is still held to its own budget. A subnet that uses both kinds of transport can therefore spend up to twice the configured numbers.


Reference

Query Rate Limiting

The budgets apply per subnet, once for UDP and once for the stream transports (TCP, DoT, DoH, DoQ).

Option Type Default Description
enabled bool false Master switch. When false, all rate limiting is bypassed with zero overhead
queries_per_second u32 1000 Token refill rate per subnet per second
burst_size u32 500 Token bucket capacity. Allows short bursts (e.g. page loads generating 50+ queries)
ipv4_prefix_len u8 24 IPv4 prefix length used to group clients into subnet buckets. 24 groups a /24 subnet
ipv6_prefix_len u8 48 IPv6 prefix length used to group clients into subnet buckets. 48 covers a standard home delegation
whitelist [str] [] List of CIDRs that bypass rate limiting entirely
nxdomain_per_second u32 50 Separate budget for NXDOMAIN answers; only an NXDOMAIN over it is limited. Catches random-subdomain attacks. 0 = no NXDOMAIN budget
slip_ratio u32 0 Every Nth rate-limited UDP response is TC=1 instead of REFUSED; the stream transports are always REFUSED. 0 = disabled
dry_run bool false Log rate-limit events without refusing queries
stale_entry_ttl_secs u64 300 Seconds before an idle subnet bucket is evicted from memory

Connection Limiting

Option Type Default Description
tcp_max_connections_per_ip u32 30 Max concurrent TCP DNS connections per IP address. 0 = unlimited
dot_max_connections_per_ip u32 15 Max concurrent DoT connections per IP address. 0 = unlimited
doq_max_connections_per_ip u32 15 Max concurrent DoQ connections per IP address. 0 = unlimited

TC=1 Slip

When slip_ratio > 0, Ferrous DNS alternates between REFUSED and TC=1 (truncated) responses for rate-limited UDP queries:

slip_ratio Behavior
0 All rate-limited UDP responses are REFUSED
1 All rate-limited UDP responses are TC=1
2 Every 2nd rate-limited UDP response is TC=1, rest are REFUSED
N Every Nth rate-limited UDP response is TC=1

TC=1 makes the client retry over TCP, which:

  • Verifies legitimacy — spoofed-source floods can't complete TCP handshakes
  • Allows recovery — the retry spends the subnet's stream budget, which a UDP flood cannot drain, so real clients still get their answers
  • Follows standards — same approach used by NSD and BIND

Only UDP is slipped. A TCP, DoT, DoH or DoQ query over the subnet's stream budget is REFUSED: TC=1 on a stream asks for a retry over a transport the client is already using.


NXDOMAIN Budget

The NXDOMAIN budget works like BIND's response rate limiting (nxdomains-per-second). It is a separate, stricter token bucket per subnet (again one for UDP and one for the stream transports), charged for each NXDOMAIN answer the subnet receives. This catches:

  • Random subdomain attacks — bots generating abc123.example.com queries
  • IoT scanning — devices probing many non-existent subdomains
  • DGA malware — domain generation algorithm traffic

The budget is charged when the NXDOMAIN answer is produced, and it decides that answer only. An NXDOMAIN answer over the budget gets the configured action: REFUSED or, over UDP, TC=1 per slip_ratio, or only a log line under dry_run. The subnet's other queries are never refused because of it, so one device sweeping non-existent names does not take the rest of its /24 offline.

Upstream NXDOMAINs are charged however they are answered: fresh, from the cache, or for a query that joined another's in-flight lookup. NXDOMAINs from local_dns_server and from internal pools are never charged, fresh or cached: they are the LAN's own traffic, such as a router answering reverse lookups for hosts it does not know. Whitelisted clients are never charged either.

The NXDOMAIN burst capacity is nxdomain_per_second * 2 and it refills at nxdomain_per_second. Answers that resolve do not spend it. It is separate from the general query budget, which every query spends once, NXDOMAINs included; only answers the UDP listener serves straight from the cache skip the general budget. While the NXDOMAIN budget is on, a cached upstream NXDOMAIN is not served that way, so that it can be charged. nxdomain_per_second = 0 disables the NXDOMAIN budget.


Dry-Run Mode

With dry_run = true:

  • Rate-limited queries are allowed through (not refused)
  • Events are logged with status RATE_LIMITED in the query log
  • The dashboard shows rate-limited counts in the stats card and timeline
  • Useful for calibrating thresholds before enforcing

Whitelist

CIDRs in the whitelist bypass rate limiting entirely. Both IPv4 and IPv6 CIDRs are supported:

whitelist = [
    "127.0.0.0/8",       # loopback
    "::1/128",           # IPv6 loopback
    "10.0.0.0/8",        # private class A
    "172.16.0.0/12",     # private class B
    "192.168.0.0/16",    # private class C
]

Home network setup

Whitelist your RFC-1918 subnets to avoid rate-limiting internal traffic. Rate limiting is primarily meant for external or untrusted networks.


Tuning Guide

Household (~100 devices)

[dns.rate_limit]
enabled                = true
queries_per_second     = 1000    # ~10 QPS/device, covers heavy browsing + IoT
burst_size             = 500     # absorbs page loads (50+ queries/page)
nxdomain_per_second    = 50      # IoT devices probe many subdomains
slip_ratio             = 2       # 50% TC=1 for rate-limited UDP traffic
whitelist              = ["127.0.0.0/8", "::1/128", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]

Small office (~20 devices)

[dns.rate_limit]
enabled                = true
queries_per_second     = 200
burst_size             = 100
nxdomain_per_second    = 20
slip_ratio             = 2

Public resolver

[dns.rate_limit]
enabled                = true
queries_per_second     = 50      # strict per-subnet limits
burst_size             = 100
nxdomain_per_second    = 10
slip_ratio             = 2
ipv4_prefix_len        = 24
ipv6_prefix_len        = 48
whitelist              = []      # no exemptions

Dashboard Integration

When rate limiting is enabled:

  • The Dashboard shows a "Rate Limited" stat card with the count of throttled queries
  • The Timeline chart includes a third dataset (amber) for rate-limited queries over time
  • The Query Log shows rate-limited queries with an orange badge and supports filtering by "Rate Limited" category
  • The Settings page provides a full UI for configuring all rate limiting options

Performance

The rate limiter is designed for minimal impact on query processing:

  • Sharded, lock-free token operations for high concurrency
  • Per-subnet tracking with minimal memory overhead
  • Background eviction task removes idle buckets every stale_entry_ttl_secs

When enabled = false, the rate limiter is bypassed entirely with zero overhead.