Dev.to Security 🔐 Cybersecurity 👁 0 📖 4 min read

HAProxy Rate Limiting per /64 for Services in Your Free /48

Disclosure up front: I run IPYE, a company that leases IP space. Most of this article is practical how-to; IPYE appears where a free /48, LOA, or ROA helps you try the work, with a short call to action at the end. Rate

Disclosure up front: I run IPYE, a company that leases IP space. Most of this article is practical how-to; IPYE appears where a free /48, LOA, or ROA helps you try the work, with a short call to action at the end.

Rate limiting by client address works well for IPv4, where one address usually means one client or one NAT. On IPv6 it breaks down: a single client typically controls a whole /64 and can rotate through its addresses at will, so a per-address limit is trivial to evade. HAProxy can group IPv6 clients by prefix before counting, which restores the meaning of the limit. This guide adds per-/64 rate limiting to a frontend on 2001:db8:100:10::443, an address from a free IPv6 /48 from IPYE.

Why /64

The /64 is the standard size for an IPv6 LAN or a residential subnet, and SLAAC hosts choose their own interface identifiers within it. Temporary addresses change regularly. So the /64 is the smallest unit that reliably corresponds to one site or customer. Some providers delegate a /56 or /48 to each customer, so a determined client may hold more than one /64, but grouping by /64 already removes the trivial evasion.

The configuration

frontend web
    bind [2001:db8:100:10::443]:443 ssl crt /etc/haproxy/certs/
    stick-table type ipv6 size 1m expire 10m store http_req_rate(10s),conn_cur
    tcp-request connection track-sc1 src,ipmask(24,64)
    tcp-request connection reject if { sc_conn_cur(1) gt 50 }
    http-request track-sc0 src,ipmask(24,64)
    http-request deny deny_status 429 if { sc_http_req_rate(0) gt 100 }
    default_backend app

backend app
    server a1 [2001:db8:100:10::11]:8080 check
    server a2 [2001:db8:100:10::12]:8080 check

The key part is src,ipmask(24,64). The ipmask converter takes two arguments: a mask for IPv4 sources and a mask for IPv6 sources. IPv4 clients are grouped by /24 and IPv6 clients by /64, so the table counts per network rather than per address.

The stick table has type ipv6, which also stores IPv4 sources in mapped form, so one table serves both families. It records the HTTP request rate over ten seconds and the number of concurrent connections.

The rules then do two things. At the connection level, a /64 with more than 50 simultaneous connections has new connections rejected. At the HTTP level, a /64 making more than 100 requests in ten seconds receives a 429 status. The connection rule appears before the HTTP rules because HAProxy processes connection-level rules first anyway.

Choosing the IPv4 mask

Grouping IPv4 by /24 is aggressive: unrelated users behind a large carrier NAT or in the same small network share a limit. If your service has many IPv4 users behind shared addresses, use ipmask(32,64) to keep IPv4 per address while grouping IPv6 by /64. The point of this guide is the IPv6 side; the IPv4 mask is a separate choice.

Pick limits from data

Set the numbers by watching real traffic first. Replace the deny rule with a header or log variable that records the current rate, run it for a few days, and look at the distribution:

http-request set-var(txn.rate) sc_http_req_rate(0)
log-format "%ci %[var(txn.rate)] %r"

Choose limits well above what legitimate clients reach. A browser loading a modern page can make dozens of requests in a second, and API clients may burst during normal use.

Inspect the table

echo "show table web" | socat stdio /run/haproxy/admin.sock

The output lists each tracked key, here masked addresses ending in :: for IPv6 /64s, with its counters. Enable the stats socket in the global section if it is not already configured, and restrict its permissions.

Validate and reload

haproxy -c -f /etc/haproxy/haproxy.cfg
systemctl reload haproxy

haproxy -c checks the configuration and reports warnings such as rule ordering. A reload keeps existing connections open while the new process takes over.

Allow lists

Exempt your own monitoring and known partners from the limits with an ACL on source address, placed before the deny rules:

acl trusted src 2001:db8:100:5::/64
http-request allow if trusted

Because your monitoring hosts live in your own /48, a single prefix covers them.

The service behind the limits

The frontend address is reachable from the Internet once your /48 is announced and accepted. The upstream needs the LOA from the IPYE client portal, and the ROA that IPYE creates validates the route.

Next step

Claim your free /48, put HAProxy on a dedicated address, and count IPv6 clients by /64 with ipmask from the start. Questions: [email protected].

About IPYE

IPYE (https://ipye.com) leases IPv4, IPv6 and ASN space with instant setup. Your first IPv6 /48 is free for the first year, a real allocation you can announce on your own ASN, with a LOA from the portal and the ROA created automatically. ASN and region changes apply automatically. IPv4 /24 to /22 leases include LOA, ROA and per-/24 rDNS; WHOIS is editable on IPv6 /44 and larger. Locations: Los Angeles, New York, Ashburn, Salt Lake City, London, Amsterdam, Frankfurt. Claim your free /48: https://my.ipye.com/products/ipv6-rental. Questions: [email protected].

Disclosure: I run IPYE

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.