wildcard mask

term

networking

Kept in this browser · My desk

A wildcard mask is a 32-bit pattern written like an IPv4 address in which each 0 bit means the corresponding address bit must match and each 1 bit means it is ignored; for a contiguous block it is the bitwise inverse of the netmask, with the host bits set to one, and access lists and some routing commands use it to say which bits of an address to ignore.

Python's ipaddress documentation calls it the host mask, "the logical inverse of a net mask", which "is sometimes used (for example in Cisco access control lists) to denote a network mask": 0.0.0.255 for a /24, 0.0.0.63 for a /26. The module returns it as hostmask and with_hostmask, and reads a mask written after the slash as a host mask when its first field is zero. Cisco IOS uses it in ACLs (access control lists), in OSPF (Open Shortest Path First) network statements and elsewhere; other platforms, the Cisco ASA (Adaptive Security Appliance) among them, write ordinary netmasks in the same places, which is the source of a classic misconfiguration.

A wildcard mask, also called an inverse mask, is a 32-bit value written in dotted decimal like an IPv4 address, which tells a matching rule which bits of an address to compare. Cisco's guide to IP access lists states the rule: each 0 bit in the wildcard means the corresponding bit must match exactly, and each 1 bit means that bit can be ignored. An entry such as 10.1.1.0 0.0.0.255 therefore matches every address that begins 10.1.1, because the first three octets must match and the last one may be anything. The netmask works the other way round, with ones marking the network part, which is why, for the contiguous blocks that make up nearly all real use, the wildcard is simply the bitwise inverse of the netmask: the host mask, in the vocabulary of Python's ipaddress module, which defines it as the logical inverse of a net mask, sometimes used, for example in Cisco access control lists, to denote a network mask.

Computing one is subtraction. Cisco's guide gives the method, subtract the subnet mask from 255.255.255.255, so 255.255.255.0 gives 0.0.0.255 and 255.255.248.0 gives 0.0.7.255; in prefix terms, a /n block has 32 minus n ones at the right-hand end, which is why a /30 is 0.0.0.3, a /26 is 0.0.0.63, a /23 is 0.0.1.255, a /20 is 0.0.15.255 and a /16 is 0.0.255.255. Two values have keywords of their own: a wildcard of 0.0.0.0 matches one exact address, which IOS lets an administrator write as host 10.1.1.2, and the pair 0.0.0.0 255.255.255.255 matches everything, written any. In a standard ACL (access control list) an omitted wildcard is taken as all zeros, so a bare address matches only itself. An entry whose address has bits set where the wildcard ignores them, 10.1.1.5 0.0.0.255 for instance, still matches the whole of 10.1.1.0/24, since the ignored bits are not compared.

What the wildcard can do that a netmask cannot is express non-contiguous patterns. Any bit pattern is legal: 0.0.0.254 combined with 10.10.10.2 matches the even addresses 10.10.10.0, 10.10.10.2, 10.10.10.4 and so on, and combined with 10.10.10.1 it matches the odd ones, and with enough care several unrelated /24s can be squeezed into one entry by finding the bits they share. That power is also the trap. A non-contiguous wildcard does not describe one block, cannot be written as a prefix length, and matches every address the free bits allow, including many that were not meant; as Tony Mattke put it in 2010, the more complex such masks get, the more unintentional traffic they let in, and their use in routing protocols is frowned upon if not prohibited. Reviewers, audit tools and the next administrator all read prefixes more easily than bit puzzles.

The same notation appears outside ACLs on Cisco IOS, most visibly in the OSPF (Open Shortest Path First) network statement, network 10.1.16.0 0.0.15.255 area 3, where the wildcard does not describe a subnet at all. As Ivan Pepelnjak explained in 2007, the statement works like a simple IP access list run against the router's interface addresses: any interface whose address matches is placed in the area, the statements are sorted from most to least specific, and the first match wins. The wildcard bits therefore need not be the inverse of the interface's mask; network 192.168.1.2 0.0.0.0 area 0 selects exactly one interface, and network 0.0.0.0 255.255.255.255 area 0 puts every interface into area 0. A wrong wildcard in that statement does not break a subnet, it silently enables or disables OSPF on interfaces nobody intended. EIGRP (Enhanced Interior Gateway Routing Protocol) network statements on IOS take an optional wildcard in the same way.

Platforms disagree about which mask goes where, and that is the most common practical error. Cisco's own ASA (Adaptive Security Appliance) documentation warns that its access lists take a network mask such as 255.255.255.0, unlike the Cisco IOS access-list command, whose mask uses wildcard bits such as 0.0.0.255. Linux iptables writes a network mask or a prefix length after the slash. FortiOS uses prefixes and netmasks in firewall policy, but its routing access lists, used to filter routes, accept either a prefix or a wildcard described as Cisco-style wildcard filter criteria. An administrator who types 255.255.255.0 into an IOS ACL as if it were a netmask gets the opposite of what was meant: the first three octets are ignored and only the last must match, so the entry matches one host number in every /24 on the internet.

Python's ipaddress module handles the conversion and shows the ambiguity it has to resolve. A network can be written as 192.168.1.0/24, 192.168.1.0/255.255.255.0 or 192.168.1.0/0.0.0.255, all equivalent; the module reads a dotted mask after the slash as a netmask when its first field is non-zero and as a host mask when its first field is zero, with one exception, an all-zero mask, which it treats as a netmask. That exception is worth remembering, because the same text 0.0.0.0 means a single host as an IOS wildcard but /0, everything, as a Python mask. Every network object exposes the wildcard as hostmask and the network with it as with_hostmask, and calculators commonly print it next to the netmask for the same reason.

The numbers to remember follow from the inversion: each octet of the wildcard is 255 minus the corresponding octet of the netmask, a /24 is 0.0.0.255, a /32 is 0.0.0.0, and the full wildcard 255.255.255.255 matches any address. Wildcards appear only for IPv4 in this form; IPv6 ACLs and routing filters on the same platforms use prefix lengths.

Also known as: host mask, hostmask, inverse mask, 0.0.0.255

Sources

← Back to W Back to the glossary