{"canonical_url":"https://engineer.company/glossary/cidr/","content_markdown":"# CIDR\n\n## What it is\n\nCIDR, short for Classless Inter-Domain Routing, is the scheme that describes a\nblock of IP addresses by a starting address and the number of leading bits that\nevery address in the block shares. Written down, it is the familiar slash:\n198.51.100.0/24 means every address whose first 24 bits match 198.51.100.0,\nwhich is 256 addresses from 198.51.100.0 to 198.51.100.255. The number after the\nslash is called the prefix length, and the block itself is called a prefix.\n\nThe word classless points at what came before. The original IPv4 plan from RFC\n791 had three sizes of network, and RFC 4632, the current description of CIDR,\nexplains why that failed: Class C, with 254 usable host addresses, was too small\nfor a mid-sized organisation, and Class B, with up to 65,534, far too large, yet\nClass B was the best fit available. As the network was commercialised in the\nlate 1980s, demand for Class B grew at what looked like an exponential rate.\n\nIn January 1992 the IETF's ROAD group (Routing and Addressing) named three\nproblems: Class B space running out, routing tables outgrowing what routers and\ntheir operators could manage, and the eventual exhaustion of IPv4 itself. The\nfirst two were expected to turn critical between 1993 and 1995. CIDR was the\nanswer to those two, first proposed in RFC 1338 in June 1992 and then in RFC\n1519 in September 1993, which RFC 4632 replaced in August 2006. It was never\nmeant to solve the third, only to buy time.\n\nIn CIDR notation, RFC 4632 writes a prefix as the usual four-part address, a\nslash, and a number from 0 to 32 giving the significant bits. The older netmask\nsays the same with those bits set to one and the rest to zero, so /24 is\n255.255.255.0. The one constraint the RFC keeps is that the mask must be\ncontiguous, and within it a prefix can describe any power-of-two block. The\nRFC's table runs from /32, a host route of one address, through /31 for a\npoint-to-point link with two, /24 with 256 and /16 with 65,536, down to\n0.0.0.0/0, all 4,294,967,296 addresses, used as the default route.\n\nThe notation outlived the address family it was built for: RFC 4291, which sets\nout how IPv6 addresses are structured, writes IPv6 prefixes the same way, so\n2001:db8::/32 reads exactly like its IPv4 cousins.\n\n## Why it matters\n\nCIDR solved its original problems by aggregation. In RFC 4632's account,\nsummarising routes is the only widely understood way for a packet-switched\nnetwork to carry less routing state. A provider that holds one large block and\nnumbers its customers from small pieces of it can announce a single route for\nthe whole block, and the rest of the Internet needs nothing more to reach every\ncustomer inside it. RFC 4632 adds that address assignment itself had to change\nto make that possible, and that the part of the old Class C space which cannot\nbe aggregated, known as the swamp, still held an estimated 15,000 routing\nentries in 2006.\n\nAggregation works because of a single rule that every router follows. In RFC\n4632's words: \"Forwarding in the Internet is done on a longest-match basis.\"\nWhen more than one prefix covers a destination, the most specific one wins. That\nis how an organisation that changes provider without renumbering stays\nreachable: the new provider announces the organisation's own small prefix, which\nthe RFC describes as punching a hole in the old provider's aggregate, and it is\npreferred over the aggregate because it is the longer match. Planned to last\nthree to five years, CIDR had, by the RFC's account, been thoroughly deployed\nby 2005.\n\nFew engineers configure inter-domain routing, but almost all of them write\nprefixes: in a firewall rule that admits an office network, in a route table on\na Linux server, and in WireGuard, where each peer's AllowedIPs list of prefixes\ndecides both which source addresses are accepted from that peer and where\ntraffic for it is sent. In each place, one wrong digit after the slash silently\nadmits or excludes thousands of hosts.\n\n## What it looks like in practice\n\nPython's standard `ipaddress` module understands the notation and is a quick way\nto check arithmetic before it goes into a rule. The session below is this\narticle's illustration, run on Python 3.15 with an address from the\n198.51.100.0/24 documentation range:\n\n```text\n\u003e\u003e\u003e import ipaddress\n\u003e\u003e\u003e net = ipaddress.ip_network(\"198.51.100.0/24\")\n\u003e\u003e\u003e net.netmask, net.num_addresses\n(IPv4Address('255.255.255.0'), 256)\n\u003e\u003e\u003e for sub in net.subnets(new_prefix=26):\n...     print(sub)\n...\n198.51.100.0/26\n198.51.100.64/26\n198.51.100.128/26\n198.51.100.192/26\n\u003e\u003e\u003e ipaddress.ip_network(\"198.51.100.70/26\")\nTraceback (most recent call last):\n  ...\nValueError: 198.51.100.70/26 has host bits set\n\u003e\u003e\u003e ipaddress.ip_network(\"198.51.100.70/26\", strict=False)\nIPv4Network('198.51.100.64/26')\n\u003e\u003e\u003e half = ipaddress.ip_network(\"198.51.100.128/25\")\n\u003e\u003e\u003e ipaddress.ip_address(\"198.51.100.200\") in half\nTrue\n\u003e\u003e\u003e half.subnet_of(net)\nTrue\n```\n\nA /24 splits into four /26 blocks of 64 addresses, each starting on a multiple\nof 64. Asking for 198.51.100.70/26 fails, because the block containing .70\nstarts at .64 and the bits after the 26th are not zero; with `strict=False` the\nmodule zeroes them and returns the block .70 belongs to. The last two lines are\nthe questions a reviewer should ask of any pair of prefixes: does this address\nfall inside, and is one block wholly inside the other.\n\nThat second question matters most in a packet filter, which does not use longest\nmatch. It reads its rules in order and stops at the first that decides. The\nnft(8) manual says that `accept` and `drop` terminate evaluation of the chain,\nand that `add` appends a rule to the end of a chain while `insert` puts it at\nthe beginning. In this second illustration of mine, the aim is to let a /24\nreach SSH while keeping its upper half out:\n\n```text\nadd table inet filter\nadd chain inet filter input { type filter hook input priority filter; }\nadd rule inet filter input ip saddr 198.51.100.0/24 tcp dport 22 accept\nadd rule inet filter input ip saddr 198.51.100.128/25 tcp dport 22 drop\n```\n\nBoth rules look right, and a listing shows both. But every address in the /25 is\nalso in the /24, so the first rule accepts those packets and the drop below it\nis never reached. In routing, the more specific prefix would win wherever it\nsat; in a filter, it wins only if it comes first. Swapping the two lines, or\nadding the narrower rule with `insert`, gives the intended result.\n`nft -a list ruleset` prints every rule with its handle, and both `add` and\n`insert` accept a handle to place a new rule next to an existing one.\n\nThe portfolio entry\n[Verified firewall rules by position](/portfolio/verified-firewall-rules-by-position/),\nfrom August 2026, describes the same trap from the operating side. A host ran\nfor a year with a rate limit that no packet ever reached, because it sat below a\nbroader rule that matched first, and every tool that printed a summary showed\nthe limit correctly. The checks now read the numbered rule list and the live\nfilter chain and assert their order. That page does not say how the broader rule\nwas written or whether address prefixes were involved, so the lines above\nillustrate the principle, not that host's rules.\n\n## Where it goes wrong\n\n**Host bits in the address.** 198.51.100.70/26 is an address with a mask, not a\nblock. Python rejects it by default, but with `strict=False` rounds it down to\n198.51.100.64/26, which may not be the hosts the author meant. Write the network\naddress of the block, and parse strictly so the mistake shows up as an error.\n\n**A bare address where a block was meant.** Python's documentation says a\nnetwork given without a mask is taken as /32, so an office's address typed\nwithout its prefix admits one host. Always write the prefix length, even /32, so\na reader can tell a host route from a forgotten slash.\n\n**A broad prefix above a narrow one.** As the illustration shows, a first-match\nfilter lets the earlier rule shadow any later rule whose prefix it contains. Put\nthe most specific rules first, and when rules are generated, test each pair with\na containment check such as `subnet_of` or `overlaps` and fail the build if a\nlater rule is unreachable.\n\n**A prefix length that is a few bits too short.** Each bit removed doubles the\nblock, so a /16 typed for a /24 admits 256 times as many addresses, and /0\nadmits every address there is. RFC 4632 records that operator error and other\nevents have caused /1 and /2 routes to be observed in some networks in the past.\nTreat any rule shorter than the block you own as a review item, and reserve\n0.0.0.0/0 and ::/0 for rules that are meant to match everything.\n\n**Counting hosts as addresses.** A /24 has 256 addresses, but on an ordinary\nnetwork the first is the network address and the last the broadcast address, so\nPython's `hosts()` returns 254 of them. On point-to-point links that waste adds\nup: RFC 3021 notes that a /30 spends four addresses on two hosts, and lets such\nlinks use a /31 instead, which saves 1,000 addresses across 500 links. Size a\nblock for the hosts it must hold plus those reserved addresses, and use /31 only\non equipment that supports RFC 3021.\n\n**An aggregate with no discard route.** A router that announces a summary block\nbut has lost one of the smaller prefixes inside it can send traffic for the\nmissing piece back along a less specific route, such as the default, and so\ncreate a forwarding loop. RFC 4632 requires the router that generates the\naggregate to discard packets matching it but no more specific route, which in\npractice means installing a discard or null route for every aggregate it\nannounces.\n\n**Allow-lists that cover only IPv4.** In nftables, `ip saddr` reads the IPv4\nheader and `ip6 saddr` the IPv6 one, and in Python, collapsing a mixed list\nraises a `TypeError` because the two versions cannot be combined. A list of IPv4\nprefixes says nothing about IPv6 clients, which then fall through to whatever\ncomes next. Keep IPv4 and IPv6 prefixes side by side for every rule, and check\nboth when you test.\n\n## Further reading\n\n- [RFC 4632: Classless Inter-domain Routing (CIDR): The Internet Address Assignment and Aggregation Plan](https://www.rfc-editor.org/rfc/rfc4632.html)\n  — the current description of CIDR, with its history, the prefix table and the\n  rules for aggregation.\n- [RFC 3021: Using 31-Bit Prefixes on IPv4 Point-to-Point Links](https://www.rfc-editor.org/rfc/rfc3021.html)\n  — why a /30 spends half its addresses on a point-to-point link, and how a /31\n  avoids it.\n- [RFC 4291: IP Version 6 Addressing Architecture](https://www.rfc-editor.org/rfc/rfc4291.html)\n  — the IPv6 addressing rules, including prefixes written in the same slash\n  notation.\n- [ipaddress: IPv4/IPv6 manipulation library](https://docs.python.org/3/library/ipaddress.html)\n  — Python's reference for parsing, splitting, comparing and collapsing\n  prefixes.\n- [nft(8): nftables manual](https://www.netfilter.org/projects/nftables/manpage.html)\n  — the reference for nftables, including how rules are ordered and which\n  verdicts end a chain.\n- [wg(8): set and retrieve configuration of WireGuard interfaces](https://git.zx2c4.com/wireguard-tools/about/src/man/wg.8)\n  — the WireGuard manual, where AllowedIPs is defined as a list of addresses\n  with CIDR masks.\n\n\u003chttps://engineer.company/glossary/cidr/\u003e\n","date_modified":"2026-10-30T02:51:31Z","description":"What CIDR is, how a slash and a prefix length describe a range of IP addresses, and where it goes wrong, from host bits to the order of firewall rules.","formats":{"gemtext":"https://engineer.company/glossary/cidr/index.gmi","gopher":"https://engineer.company/glossary/cidr/gophermap","html":"https://engineer.company/glossary/cidr/","json":"https://engineer.company/glossary/cidr/index.json","markdown":"https://engineer.company/glossary/cidr/index.md","pdf":"https://engineer.company/glossary/cidr/index.pdf"},"kind":"page","language":"en","title":"What is CIDR?","url":"https://engineer.company/glossary/cidr/","version":1}