Decomposing Arbitrary IPv4 Ranges Into Exact Firewall CIDR Rules
Third-party vendors and ISPs frequently share allowlists as start–end IPv4 ranges (for example, 192.168.1.10 through 192.168.1.50), whereas AWS Security Groups, Cloudflare WAF, Kubernetes NetworkPolicies, and Nginx allow/deny directives only accept CIDR notation. Rounding up to 192.168.1.0/24 opens 215 unintended IP addresses.
The exact decomposition algorithm repeatedly picks the largest power-of-two block that both aligns on startIP's lowest set bit and does not exceed endIP, advancing startIP until the entire range is covered with zero gaps and zero spillover.
- Decomposition AlgorithmGreedy max-prefix alignment checking trailing zero bits of startIP and remaining span (endIP - startIP + 1).
- Exactness GuaranteeSum of IPs across output CIDRs equals (end - start + 1) exactly—never allows unintended adjacent IPs.
- Export SnippetsOutputs plain CIDR list, Nginx allow directives, and Terraform string list syntax.
Why does a small IP range like .10 to .50 split into multiple CIDR blocks?
CIDR blocks must be power-of-two sizes aligned to power-of-two boundaries. Because .10 is only divisible by 2 (not 32), the exact cover starts with /31 (.10–.11), then /30 (.12–.15), /28 (.16–.31), /28 (.32–.47), /31 (.48–.49), and /32 (.50).
Can I convert a single IP range like 203.0.113.7 - 203.0.113.7?
Yes. An identical start and end IP yields a single /32 CIDR block (203.0.113.7/32).
Does this tool verify that start IP is less than or equal to end IP?
Yes. If the endpoints are reversed, it validates and flags or normalizes the order cleanly.