IP Addresses & Routing

Wildcard Masks and Where You Meet Them

Read and write wildcard masks for access lists and routing statements, convert them from subnet masks and avoid classic errors.

Key takeaways

  • In a wildcard mask, zeros mark bits that must match and ones mark bits that are ignored.
  • For an ordinary block, each wildcard octet equals 255 minus the matching subnet mask octet.
  • Unlike subnet masks, wildcard bits do not have to be contiguous, so one entry can match unusual patterns.
  • Check which form a device expects, because typing a subnet mask where a wildcard belongs can match something entirely different.

A wildcard mask tells a device which bits of an address it must compare and which it may ignore. A zero bit means "this bit must match". A one bit means "any value will do". For a whole block, you get the wildcard by subtracting the subnet mask from 255.255.255.255, so a /24 has the wildcard 0.0.0.255.

You meet wildcard masks mainly in access control lists and some routing protocol statements, most famously on Cisco-style command lines. They look like subnet masks turned inside out, which is where both their usefulness and their reputation for causing mistakes come from.

The matching rule

A wildcard entry has two parts: a base address and a wildcard. To test an address, the device compares it with the base, bit by bit, but only where the wildcard holds a zero.

Take base 192.168.1.0 with wildcard 0.0.0.255. The first three octets have wildcard zero, so they must be exactly 192.168.1. The last octet has all ones, so anything is accepted. The entry matches 192.168.1.0 through 192.168.1.255.

Reading the entry 192.168.1.0 0.0.0.255
192.168.1.0 1 0.0.0 2 255 3 Base address The pattern that packets are compared against. 1 Must match Wildcard zeros: these octets must equal 192.168.1. 2 Do not care Wildcard ones: any value from 0 to 255 passes. 3 192.168.1.0 1 0.0.0 2 255 3 Base address The pattern that packets are compared against. 1 Must match Wildcard zeros: these octets must equal 192.168.1. 2 Do not care Wildcard ones: any value from 0 to 255 passes. 3

Converting from a subnet mask

Subtract each octet of the subnet mask from 255.

Prefix Subnet mask Wildcard mask Addresses matched
/32 255.255.255.255 0.0.0.0 1
/30 255.255.255.252 0.0.0.3 4
/28 255.255.255.240 0.0.0.15 16
/27 255.255.255.224 0.0.0.31 32
/26 255.255.255.192 0.0.0.63 64
/24 255.255.255.0 0.0.0.255 256
/22 255.255.252.0 0.0.3.255 1,024
/20 255.255.240.0 0.0.15.255 4,096
/16 255.255.0.0 0.0.255.255 65,536
/0 0.0.0.0 255.255.255.255 All

In the interesting octet, the wildcard value is the block size minus one. A /27 has blocks of 32, so the wildcard ends in 31. If you already know the block size method, you know wildcards.

The base address should be the start of the block. For hosts 172.16.32.0 to 172.16.47.255, write 172.16.32.0 0.0.15.255. Many devices tidy up a base such as 172.16.45.9 by zeroing the ignored bits, which can hide the fact that you matched much more than one host.

Two shorthand words

Cisco-style lists offer two convenient aliases:

  • host 192.0.2.10 means 192.0.2.10 0.0.0.0, exactly one address.
  • any means 0.0.0.0 255.255.255.255, every address.

Testing an address by hand

You can decide whether any address matches an entry in three moves. For each octet where the wildcard is 0, the address must equal the base. For each octet where the wildcard is 255, skip it. For a partial octet, check that the address octet falls between the base value and the base plus the wildcard value, which works whenever the wildcard is contiguous and the base is aligned.

Take the entry 10.20.64.0 0.0.31.255 and the address 10.20.90.14. The first two octets must equal 10.20, and they do. The third has wildcard 31 on base 64, so it accepts 64 through 95, and 90 qualifies. The fourth is ignored. The address matches. 10.20.96.1 would not.

For a non-contiguous wildcard the range shortcut fails, and you need the bits. Write the base octet, the wildcard octet and the address octet in binary, and compare only the columns where the wildcard shows 0.

Access control lists

An access control list, or ACL, is an ordered set of permit and deny lines. A standard numbered list matches only the source address:

access-list 10 permit 192.168.1.0 0.0.0.255
access-list 10 deny any

An extended list can match protocol, source, destination and port:

access-list 110 permit tcp 192.168.1.0 0.0.0.255 host 192.0.2.10 eq 443
access-list 110 deny ip 192.168.1.0 0.0.0.255 host 192.0.2.10
access-list 110 permit ip any any

Read it from the top. Machines on 192.168.1.0/24 may use HTTPS to the server, may not reach it any other way, and all other traffic passes.

Two behaviors matter more than the syntax. Evaluation stops at the first match, so a broad line above a narrow one hides the narrow one. And on these platforms an implicit deny ends every list: anything that matched nothing is dropped. The explicit deny any in the first example changes no behavior, though it can make the intent visible and, depending on the platform, let you count hits.

How a packet moves through an access list
1 Check line one Compare addresses using base and wildcard. 2 Match found? Apply permit or deny and stop reading. 3 No match Move to the next line in order. 4 End of list The implicit deny drops the packet. Check line one Compare addresses using base and wildcard. 1 Match found? Apply permit or deny and stop reading. 2 No match Move to the next line in order. 3 End of list The implicit deny drops the packet. 4

A list does nothing until applied to an interface or service, in a direction. Before applying one on a device you manage remotely, make sure it cannot cut off your own session, and arrange another way in. Only change equipment you are authorized to manage.

Routing protocol statements

In classic OSPF configuration, a network statement uses a wildcard to choose which of the router's interfaces take part:

router ospf 1
 network 10.1.1.0 0.0.0.255 area 0
 network 10.1.2.1 0.0.0.0 area 0

The first line enables OSPF on any interface whose address falls inside 10.1.1.0/24. The second selects only the interface with address 10.1.2.1. The statement is a selector for interfaces. The prefix that gets advertised comes from the interface's own mask. Many platforms also let you enable OSPF directly on an interface, which avoids the wildcard altogether. OSPF and other interior routing protocols explains areas.

EIGRP network statements can take a wildcard in a similar way.

Non-contiguous wildcards

Subnet masks must be an unbroken run of ones. Wildcards have no such restriction, since each bit is judged independently.

Consider base 192.168.0.0 with wildcard 0.0.254.255. In binary, the third octet of the wildcard is 11111110: every bit is ignored except the last, which must match the base value of 0. So this one entry matches every 192.168.x.y where x is even: 192.168.0.0/24, 192.168.2.0/24, and so on up to 192.168.254.0/24.

Change the base to 192.168.1.0 and it matches all the odd ones.

This can be clever when an address plan uses odd and even numbers for different purposes. It is also hard to read, easy to get wrong and unsupported by some platforms and tools. Use it only with a comment explaining it, and consider whether two or three plain entries would be clearer.

Designing a list that stays readable

Access lists grow. A few habits keep them understandable a year later.

  • Give lists names where the platform allows it. A name such as GUEST-IN says more than a number.
  • Add remarks. Most platforms let you store a comment line beside the entries. Say who asked for the rule and why.
  • Order from specific to general. Single hosts first, then small blocks, then broad permits or denies.
  • Group related entries. Keep everything about one server together so a reviewer can see the whole policy for it.
  • Remove what is dead. Rules for retired servers become openings when the address is reused.

Suppose guests on 192.168.40.0/24 should reach the internet and nothing internal, where internal means the private ranges. A sketch of the idea looks like this:

permit udp any eq 68 any eq 67
permit udp 192.168.40.0 0.0.0.255 host 192.168.40.1 eq 53
deny ip 192.168.40.0 0.0.0.255 10.0.0.0 0.255.255.255
deny ip 192.168.40.0 0.0.0.255 172.16.0.0 0.15.255.255
deny ip 192.168.40.0 0.0.0.255 192.168.0.0 0.0.255.255
permit ip 192.168.40.0 0.0.0.255 any

The first two lines keep address assignment and name lookups working, here assuming the router at .1 provides both. The three denies use the wildcards for the three private ranges. The last line allows everything else, which means the public internet. Treat it as an illustration: exact syntax and what else you need to allow depend on the platform.

Matching a range that is not one block

Say you want hosts 192.168.5.32 to 192.168.5.95. That is 64 addresses, but it does not start on a multiple of 64, so no single block covers it. Use two:

192.168.5.32 0.0.0.31
192.168.5.64 0.0.0.31

Writing 192.168.5.32 0.0.0.63 instead would not do what you expect. The base bit with value 32 falls inside the ignored range, so the entry matches .0 through .63. That admits 32 addresses you did not intend and misses 32 you did. The range to CIDR tool can break a range into aligned blocks, each of which converts directly to a wildcard.

Where you will not find them

Many systems use CIDR prefixes for rules: Linux packet filters, most cloud security groups, the majority of consumer routers and many commercial firewalls. Some vendors use ordinary subnet masks in access lists, even in product lines from companies that use wildcards elsewhere. Newer command syntax on several platforms also accepts prefix notation.

The practical rule: never assume. Read the device's help for the field. If the example in the documentation shows 0.0.0.255, it wants a wildcard. If it shows 255.255.255.0, it wants a mask.

The word "wildcard" also appears in unrelated places, such as wildcard DNS records and wildcard certificates, which use an asterisk to match names. They have nothing to do with address masks.

Common mistakes

  1. Typing a subnet mask. permit 192.168.1.0 255.255.255.0 as a wildcard ignores the first three octets and requires only that the last is 0. It matches any address ending in .0 and rejects your ordinary hosts.
  2. Leaving off the wildcard. In some standard lists an address alone means a single host. If you meant a network, you will match nothing useful.
  3. Misaligned base. As in the range example above.
  4. Wrong order. A general permit above a specific deny.
  5. Forgetting the implicit deny. A list containing only deny lines blocks everything.
  6. Wrong direction. A list matching sources, applied to traffic going the opposite way, never matches.

Verify before you trust

After applying a list, look at its hit counters. Generate a test connection that should be permitted and one that should be denied, and check that the expected lines increase. On Cisco-style devices, show access-lists displays the entries and counts. A line that never gets a hit may be shadowed by one above it, or may describe traffic that does not exist.

Practice

What does 10.0.16.0 0.0.7.255 match? The third octet ignores values 0 to 7 above 16, so it covers 10.0.16.0 through 10.0.23.255, which is 10.0.16.0/21.

What wildcard matches 172.16.0.0/12? The mask is 255.240.0.0, so the wildcard is 0.15.255.255.

How would you match exactly the four addresses 203.0.113.8 to 203.0.113.11? The block is aligned, so 203.0.113.8 0.0.0.3 does it.

If any of those felt uncertain, work through subnetting from the ground up and come back. Wildcards are the same boundaries, written with the bits flipped.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in IP Addresses & Routing →

Latest news on this topic