Netmask Filtering Deep Dive

Topics · Published 2019-07-20 · Revised 2026-11-23
Reviewed corrections applied locally; original wording preserved separately · Joomla source

6,359 Total views across this article’s versions; includes the historical baseline where available.

What is a Netmask (or Genmask)? And why is understanding it so important when it comes to network routing?

A netmask identifies the network bits of an IPv4 address. Genmask is the label used for that mask in the legacy route(8) display. The two terms refer to the same mask here.

Table of Contents

Routers and Routes

Routes are instructions, but they are easier to fathom if you think of them as filters. Within a route, there are two filtration layers: an IPv4 host address and a netmask. The netmask filter is superimposed over a single destination host address. The netmask is used to amplify the scope of the route. The result defines a range of IPv4 host addresses.

A prefix lets a route cover many addresses. IPv4 has 2^32 possible values, though not all are public unicast addresses; maintaining a separate route for every value would be impractical. An address and netmask together identify an aligned range.

If you’re familiar with photography, you can liken the process to a variable focus camera lens. Imagine a single IPv4 address is your base - a fixed focal lens - and the netmask is a variable zoom lens mounted to the base lens. The variable lens (netmask) allows the breadth of your focus (address range) to be very broad, very narrow, or somewhere in between. Just as a variable focus lens may zoom in to a specific point or zoom out to capture a wide view, the netmask controls the scope of a route’s address range. It allows the router to narrow its focus between one (1) and ALL possible IPv4 addresses; from fine to coarse focusing per se.

4x8=32

Computers distill all communications down to binary signals of 0 and 1. IP addresses are no exception. As humans, we are used to working with IPv4 addresses in a 10-base numeral format, but that is simply for our convenience. In reality those are not the real IP addresses. Each of those 3-digit, 10-base values we’re accustomed to are converted to 8-bit binary numbers that computers understand. Four (4) of those 8-bit numbers are combined, resulting in an aggregate 32-bit string (4x8=32) which represents an individual IPv4 host address on the network.

You may have noticed every number in an IPv4 address falls into a range between 0 and 255.

An octet contains eight bits. Reading from right to left, their place values are 1, 2, 4, 8, 16, 32, 64, and 128. In the illustration below, position 1 is worth 2^0, or 1; position 4 is worth 2^3, or 8. Add the values of the set bits: positions 4 and 5 together give 8 + 16 = 24. All eight set bits total 255.

8-bit binary to 10-base illustration

Figure: 8-bit Illustration (Big-endian format)

An IPv4 address is 32 bits. A dotted-decimal netmask describes another 32-bit value, but it remains separate from the address.

Address and Mask Are Separate

A name entered in a browser can be resolved through DNS to an IP address.1 For IPv4, that address has four octets. Routing additionally uses a network prefix, often written with a separate netmask:

IPv4 address: 192.168.1.1
Netmask:      255.255.255.192

Both values have 32 bits. Their combination describes a prefix; it is not a new 64-bit IPv4 address or a claim about the kernel’s internal storage format. See RFC 4632 and the discussion of routing prefixes.

Netmask Re-Cap

An octet is eight bits. Each of the four dotted-decimal components in an IPv4 address represents one octet; the whole dotted address is not an octet. A contiguous netmask can also be expressed as a prefix length, using slash notation.

Here’s how it all works.

Subnets and Netmasks

Sub-netting is the process of dividing a network into smaller networks or segments. If you need a primer on sub-networks and netmasks, I suggest reading Routers, Routes, Subnets, and Netmasks before proceeding.

Slash Notation

Slash notation is a simplified representation of a route’s netmask bits. It appears as “IPv4 address/xx” where “xx” is a whole number between 0-32. The number after the slash represents the number of bits which are “on” (set to 1) in the 32-bit netmask string. The slash notation may be any number between 0 and 32.

Most people find it easier to use as a shorthand method of assigning netmasks. However, if you are using tools such as the route command and/or certain legacy tools, such as netstat, the output of those tools is virtually always presented in a 10-base number format, thus requiring some ability to translate those figures to binary representations. Many online tools exist to assist with this process, as it is a common task for network administrators. A table of recommended online IPv4 conversion tools is provided at the end of this article.

A shorter prefix length describes a larger address block; a longer one describes a smaller block. /0 covers the entire IPv4 space, /24 covers 256 addresses, and /32 covers one. Address count and usable host count are different, as the examples below explain.

Valid Netmask Values

For CIDR prefixes, a valid mask has consecutive one bits followed by zero bits. /0 has no one bits; /32 has no zero bits. In between, a one bit cannot occur to the right of a zero bit. Slash notation records how many leading bits are set.

This netmask is valid:

11111111.11111111.11111111.10000000

At 25 bits wide, it represents 255.255.255.128 or /25 in slash notation.

The netmask below is not valid. Do you see why? The one (1) bits to the right of the first zero (0) - when viewed from left to right - are not valid, because they are to the right of the last consecutive one (1) bit. As soon as a zero (0) appears when viewing from left-to-right, you cannot have more ones (1’s) in the string.

11111111.11111111.11111111.10110010

That noncontiguous mask is not a valid CIDR mask. Correct it explicitly; do not depend on a tool silently converting it to /25.

A prefix length is an integer from 0 through 32. For example, /14 produces this mask:

11111111.11111100.00000000.00000000

There are 14 leading one bits, followed by 18 zero bits. The dotted-decimal mask is 255.252.0.0. The interface address 192.168.10.10/14 lies within network prefix 192.168.0.0/14; a route prefix uses that aligned network address.

Remember, it’s all about the bits!

A mask determines the size of an address block and, together with an address, its boundary. A destination matches when its masked value equals the route’s network address. Longest-prefix selection then distinguishes matching routes in the selected table.

Network Masking Deep Dive

We’ve reviewed the netmask concept at a high level, and how routers use it to filter ranges of IPv4 host addresses by interacting with a single IPv4 host address. But, we have not discussed exactly how a route identifies the starting or lower boundary IPv4 address of a range, and how it identifies the ending or upper boundary IPv4 address in a range. This is obviously a critical point to understand if you’re going to create sub-networks and routes. So, how does the IPv4 netmask concept work? How does it segment or partition a network?

Surprise! There is yet another logic layer you should become familiar with: the wildcard mask.

The Wildcard Mask

The wildcard mask or wildcard is a 32-bit inverse of the netmask.

What is the purpose of the wildcard? It’s used to determine the upper boundary address of the range, using a two-step process. First, a logical NOT operation is applied to the netmask. Then, the wildcard value is added to the lower boundary via a logical OR operation, which produces the upper boundary address.

For a contiguous IPv4 mask, the wildcard’s integer value is one less than the number of addresses in the block. Thus a wildcard of 0.0.0.63 represents an offset of 63 and a block of 64 addresses, including both endpoints. It is not itself the usable host count.

How Masks Work

Routers reference host addresses and netmasks in binary form. Masks are a logical method of comparing two values. Routes use masks to make comparisons between host addresses and target values. How do masks work? When masks are applied, logic operands come into play. Usually, AND, OR, and NOT.

The most common masking processes is the AND operand. The classic definition of a mask is logically ANDing a source value and a mask, resulting in only the bits matching the mask passing through. That’s the gist of how a mask works, though exactly which operands are applied with a particular mask varies. The specifics of each masking process are explained in detail throughout this document in the relevant sections, but at a high level, the AND operand is the key to masking.

This table shows the types of masking operands used in each of the masking processes discussed in this article:

Mask Operand Value1 Value2 Purpose
Netmask AND IPv4 Host Address netmask Define a sub-network
Wildcard NOT netmask Inverse of the Netmask
Lower IPv4 Range Boundary AND IPv4 host address netmask 1st IPv4 in sub-net
Upper IPv4 Range Boundary OR lower boundary wildcard Last IPv4 in sub-net

A tutorial on logical bit operators is beyond the scope of this guide. I have to presume you are familiar with logic gates. If not, you should invest some time in learning about them because routers rely on bitwise operators.

Bitwise Functions

As humans, we tend to think of numbers in a 10-base format, but computers view them as binary or 2-base numbers. “Bitwise” is a term that refers to digital processes capable of acting at the level of individual bits or groups of bits. Routers are bitwise, and so are all their functions discussed in this article. Just in case you do need a little background primer, I’ll briefly expound on the AND operator, using the netmask filtering concept to illustrate.

The Mighty AND

First calculate the network address with address AND netmask. To test a destination, apply the same mask to that destination and compare for equality with the network address. For 192.168.1.0/26, both 192.168.1.1 and 192.168.1.63 mask to 192.168.1.0; 192.168.1.64 does not. Comparing the destination numerically with the lower boundary using “less than or equal” is not the membership test.

AND bit logic illustration

Figure: AND Bit Logic Illustration

Linux policy rules select table lookups in priority order. Within a selected table, the longest matching eligible destination prefix wins. A successful earlier lookup is not compared with every route in every other table; see ip-rule(8).

Here’s what an IPv4 address looks like to you versus a router:

IPv4 Address Netmask Network Address in Binary
192.168.10.0 255.255.255.0 11000000.10101000.00001010.00000000

Netmask Examples

One of the best ways to fully digest all this information is to walk through some examples….

Example 1: The Power of Zero (Default Route)

There a few host addresses, gateways, and masks that are special for one reason or another. The most important is the default route. A “default” route (as its name implies) is where all traffic will be routed unless a more specific route matches a packet. Depending on the tool used and its location in a routing table, the default route may appear as default, *, or 0.0.0.0. For the purpose of this discussion, let’s focus on the 0.0.0.0 implementation.

A destination prefix of 0.0.0.0/0 matches every IPv4 address: masking any destination with zero yields the network value zero. In the same table, any more specific eligible prefix takes precedence over that default.

Start with the host IPv4 address. This will be the “bottom” or beginning of your address range.

Value 10-base Binary Comment
IPv4 Host 0.0.0.0 00000000.00000000.00000000.00000000 Original host address

Now, let’s introduce the netmask. How would you capture every possible IPv4 address?

Value 10-base Binary Comment
IPv4 Host 0.0.0.0 00000000.00000000.00000000.00000000 Original host address
Netmask 0.0.0.0 00000000.00000000.00000000.00000000 Network mask

A /0 mask imposes no fixed destination bits. It is still a mask: the bitwise AND clears all 32 address bits, so every destination produces the same network value, zero.

Netmask /0 = zero fixed destination bits.

Let’s examine the process in more detail to clearly understand what’s going on here.

Value 10-base Binary Comment
IPv4 Host 0.0.0.0 00000000.00000000.00000000.00000000 Original host address
Netmask 0.0.0.0 00000000.00000000.00000000.00000000 Network mask
Lower Boundary 0.0.0.0 00000000.00000000.00000000.00000000 {Host Address} AND {Netmask}

We’ve got the lower boundary address. Now we need the upper boundary. Finding that requires the wildcard, so let’s determine that next.

Value 10-base Binary Comment
IPv4 Host 0.0.0.0 00000000.00000000.00000000.00000000 Original host address
Netmask 0.0.0.0 00000000.00000000.00000000.00000000 Network mask
Lower Boundary 0.0.0.0 00000000.00000000.00000000.00000000 {Host Addr} AND {Netmask}
Wildcard 255.255.255.255 11111111.11111111.11111111.11111111 NOT {Netmask}

Got the wildcard mask. Next up is the upper boundary calculation, which is the last IPv4 address in the range of the filter. The upper boundary is calculated by processing a logical OR between the lower boundary and wildcard mask.

Value 10-base Binary Comment
IPv4 Host 0.0.0.0 00000000.00000000.00000000.00000000 Original host address
Netmask 0.0.0.0 00000000.00000000.00000000.00000000 Network mask
Lower Boundary 0.0.0.0 00000000.00000000.00000000.00000000 {Host Addr} AND {Netmask}
Wildcard 255.255.255.255 11111111.11111111.11111111.11111111 NOT {Netmask}
Upper Boundary 255.255.255.255 11111111.11111111.11111111.11111111 {Lower Boundary} OR {Wildcard}

So, there you have it. This explains how and why a host address of 0.0.0.0 and netmask of 0.0.0.0 yields a default route or “all addresses.”

Example 2: Sub My Net

Let’s use interface address 192.168.1.1/26. Its network is 192.168.1.0/26, covering 64 addresses from .0 through .63. On an ordinary broadcast subnet, .0 identifies the network and .63 is the broadcast address, leaving 62 host addresses. The special /31 point-to-point case is defined in RFC 3021; /32 identifies one address.

Value 10-base Binary Comment
IPv4 Host 192.168.1.1 11000000.10101000.00000001.00000001 Original host address
Netmask 255.255.255.192 11111111.11111111.11111111.11000000 Network mask
Lower Boundary 192.168.1.0 11000000.10101000.00000001.00000000 {Host Addr} AND {Netmask}
Wildcard 0.0.0.63 00000000.00000000.00000000.00111111 NOT {Netmask}
Upper Boundary 192.168.1.63 11000000.10101000.00000001.00111111 {Lower Boundary} OR {Wildcard}
# of addresses 63 + 1 = 64 n/a Wildcard value + 1

Dissecting the chart above, notice the binary netmask has 26 bits flipped to 1 - thus it could alternatively be represented with slash notation as /26. And the remaining number of bits (6) matches the set bits of the wildcard.

Value Slash Notation Binary Comment
netmask /26 11111111.11111111.11111111.11000000 Network mask

Let’s break down the example above and examine how the IPv4 addressable range of the route is determined. We are looking at a destination IPv4 address of 192.168.1.1 with a netmask of 255.255.255.192 or slant notation of /26:

11111111.11111111.11111111.11 000000

The slant notation is often the easiest method of visualizing what’s going on. In this example, using a slant notation of /26, we can see the first 26 bits are represented in the netmask with their bits flipped to 1:

/26 = 11111111.11111111.11111111.11 prefixed netmask

For /26, the AND operation preserves the first 26 address bits and clears the final six. It does not discard the prefix and use the host bits to choose the lower boundary. The cleared result is the network address, 192.168.1.0.

Bear in mind the lower boundary might not be equal to the starting IPv4 host address. Take a look at the current example:

192.168.1.1 = 11000000.10101000.00000001.00000001

Let’s carve out the trailing 6-bits of the host address not protected by the netmask (remember, only the first 26-bits are masked in this example).

192.168.1.1 = 11000000.10101000.00000001.00 000001

Note the last digit of the host address is a 1.

192.168.1.1 = 11000000.10101000.00000001.00 000001

Applying the required AND logic operation to the original host address and netmask, the lower end of the range after the netmask (in this case, after the 26th bit) is forcibly zeroed out by the trailing end of the netmask.

Value 10-base Binary Comment
IPv4 Host 192.168.1.1 11000000.10101000.00000001.00000001 Host address
AND
Netmask 255.255.255.192 11111111.11111111.11111111.11000000 Network mask

Yields:

Value 10-base Binary Comment
Lower Boundary 192.168.1.0 11000000.10101000.00000001.00000000 {Host Addr} AND {Netmask}

As you can see, the starting point of the network segment has been forced lower (to 192.168.1.0) than the original starting position (192.168.1.1). It’s probably not a big deal in this particular case, but it does highlight the fact the original host address will not necessarily be the lower boundary for any given route.

Now let us turn our attention to the next level in the example: the wildcard.

Value 10-base Binary Comment
Wildcard 0.0.0.63 00000000.00000000.00000000.00111111 NOT {Netmask}

The wildcard is a full 32-bit value: 26 leading zeros and six trailing ones in this example. Those six one bits permit offsets from 0 through 63 within the block.

The wildcard mask is the opposite of the netmask. Turning our attention to the current example,….

11111111.11111111.11111111.11 000000 [net-mask]

00000000.00000000.00000000.00 111111 [wildcard]

Here are the two boundary calculations for the same example:

192.168.1.1 AND 255.255.255.192 = 192.168.1.0
192.168.1.0 OR 0.0.0.63       = 192.168.1.63

In binary, the network address is 11000000.10101000.00000001.00000000; the mask itself is 11111111.11111111.11111111.11000000. These are different values.

The wildcard’s final six bits, 111111, equal 63, not 64. Counting both endpoints gives 63 + 1 = 64 addresses. That agrees with 2^(32 − 26) = 64.

Example 3: 255 Is the Opposite of 0 (Zero) [a.k.a. Singling Out Single Hosts]

One of the more common routing tasks is directing a route to a specific host. Thankfully, it’s a simple task to implement in a routing table. In fact, I would say the process represents the most basic route possible in a routing table. Targeting a single IPv4 host address is the exact opposite of creating a default route.

Hopefully, by now you’ve already ascertained the number 255 has special meaning in netmasks. When all bits of an IPv4 address byte are set to one (1), the 8-bit value is 255. In the context of a netmask, this translates to, “Restrict the range to only the number presented in the corresponding octet of the destination address.” How? Each of those octets is a number from 0 to 255. When the mask is set to 255, it allows all the bits to match and pass through. Take a look at this visual representation of a mask that is 255.255.255.255.

Let’s take a common example using some private network addresses. Your goal is to carve out a sub-net of one (1) host address. Presume you are creating a route with IPv4 host 192.168.1.1 and netmask 255.255.255.255 (alternatively expressed using slant notation as 192.168.1.1/32). The routing calculations would look like this:

Value 10-base Binary Comment
IPv4 Host 192.168.1.1 11000000.10101000.00000001.00000001 Original host address
Netmask 255.255.255.255 11111111.11111111.11111111.11111111 Network mask
Lower Boundary 192.168.1.1 11000000.10101000.00000001.00000001 {Host Addr} AND {Netmask}
Wildcard 0.0.0.0 00000000.00000000.00000000.00000000 NOT {Netmask}
Upper Boundary 192.168.1.1 11000000.10101000.00000001.00000001 {Lower Boundary} OR {Wildcard}
# of addresses 0 + 1 = 1 n/a Wildcard value + 1

Notice what happens to the host address when it is masked by the netmask. All the bits of the host address are masked, meaning they are preserved and fall through to the other side. The net result is a route of one (1) IPv4 address.

Suggested IPv4 Netmask Calculation Tools

An informal list of suggested tools for netmask and CIDR calculations.

ipcalc Linux terminal Found in most distros
Netmask Quick Reference Chart Web Steve Friedl's Netmask Quick Reference Guide
CIDR calculator Web CIDR/VLSM supernet calculator
CIDR to IPv4 Conversion Web IPLocationTools

Footnotes

1 The term “DNS” may be used to represent either of two (2) distinct, but related concepts. DNS can refer to a Domain Name System or Domain Name Server. The latter is simply a server operating or part of the operation of the former.