netmask

term

networking

Kept in this browser · My desk

A mask with the network bits set to one and the host bits set to zero, written like an address (255.255.255.0) and equivalent to a prefix length (/24).

Request for Comments (RFC) 4632 explains the prefix 172.16.0.0/16 as a mask whose 16 most significant bits are ones and whose 16 least significant bits are zeros, and requires that "the mask must be left contiguous". Its bitwise inverse is the host mask, which access lists call a wildcard. Python's ipaddress accepts all three IPv4 forms after the slash (/24, /255.255.255.0 and /0.0.0.255) and refuses a mask that mixes ones and zeros; for IPv6 it accepts the prefix length only.

What it does. A netmask is a 32-bit value that marks which bits of an IPv4 address name the network and which name the host. A host applies it every time it sends a packet: Request for Comments (RFC) 1122 requires it to extract, with the mask, the same bits from the destination address and from its own, and to deliver directly if they match or hand the packet to a gateway if they do not. A router applies the mask of each route in the same way to decide whether the route covers a destination. The mask itself never travels in the Internet Protocol (IP) header; it is local configuration, which is why two hosts on one wire can hold different opinions about who their neighbours are. It has had several names for the same thing: address mask in RFC 950, subnet mask in most configuration screens, network mask or netmask in routing documents.

Where it came from. In the original classful scheme the mask was implied by the first bits of the address: 172.16.0.0, a Class B network, had an implied mask of 255.255.0.0. Subnetting needed something explicit. Jeffrey Mogul proposed it in RFC 917 (October 1984), and RFC 950, by Mogul and Jon Postel (August 1985), made it the standard, choosing a bit mask over four other schemes for marking the subnet field. Because the bits are named by a mask, RFC 950 noted, they need not be adjacent, but it recommended that they be contiguous and the most significant bits of the local part. It also defined two messages of the Internet Control Message Protocol (ICMP), Address Mask Request (type 17) and Address Mask Reply (type 18), so that a booting host could ask for its mask.

How the rule hardened. RFC 1122 (October 1989) still remarked that its notation did not imply contiguous one bits. Classless Inter-Domain Routing (CIDR) ended the tolerance: RFC 1519 (September 1993), and RFC 4632 which replaced it in August 2006, generalize routing to any network number and mask on the single condition that the mask be left contiguous. RFC 1812 (June 1995) says that discontiguous subnet masks are not permitted, because arbitrary masks can make routing ambiguous, with two equally specific routes matching one destination, and it describes the subnet mask as the historical name for what newer protocols express as a prefix length: a contiguous string of ones at the more significant end, a contiguous string of zeros at the other, and nothing in between. The ICMP messages lasted until RFC 6918 (April 2013) formally deprecated them as superseded by the Dynamic Host Configuration Protocol (DHCP), which carries the mask as option 1, four octets, placed before the router option.

The numbers. With contiguity required there are only 33 masks, one for each prefix length from 0 to 32, and each octet of a mask can take only nine values: 0, 128, 192, 224, 240, 248, 252, 254 and 255. RFC 1878 (December 1995) tabulates them all: /16 is 255.255.0.0, /23 is 255.255.254.0 with 512 addresses, /24 is 255.255.255.0 with 256, /27 is 255.255.255.224 with 32, /30 is 255.255.255.252 with 4, and /32, 255.255.255.255, is a single host route. A block holds two to the power of the number of zero bits, and in ordinary subnets two of those addresses, all zeros and all ones in the host part, are the network and the broadcast. That is why a /30 spends four addresses on a link with two ends, and why RFC 3021 (December 2000) allows the 31-bit mask 255.255.255.254 on point-to-point links. At the other extreme the mask 0.0.0.0 matches everything: 0.0.0.0/0 is the default route.

The inverse. Subtracting a mask from 255.255.255.255 gives its complement, 0.0.0.255 for a /24. Python's documentation calls it the host mask; Cisco access lists call it the wildcard or inverse mask, in which a 0 means that the bit must match and a 1 means that it is ignored. The two are easy to confuse because both look like addresses. Python decides by the first field: text after the slash that begins with a non-zero field is a net mask, text that begins with a zero field is a host mask, and an all-zero mask is taken as a net mask.

Where each form is typed. Linux shows both generations side by side. The ifconfig command takes netmask followed by a dotted mask, defaults to the old class A, B or C mask if none is given, and prints lines such as inet 203.0.113.9 netmask 255.255.255.0; the newer ip address command takes only an address followed by a slash and the prefix length. Windows keeps the mask in its route command, as in route add 10.41.0.0 mask 255.255.0.0 10.27.0.1; a missing mask defaults to 255.255.255.255, a host route, and a destination with bits set where the mask has zeros is refused with an error. FortiGate writes a prefix as a subnet followed by a netmask. F5 BIG-IP writes a self IP address as 10.10.10.24/16, yet gives a network virtual server a mask option in dotted form whose default, 255.255.255.255, makes it a host. In Python, an IPv4Network object exposes netmask, hostmask and with_netmask, which is the quickest way to convert between the three notations.

IPv6 has no netmask to type. RFC 4291 writes a prefix as an address, a slash and a decimal prefix length, the number of leftmost contiguous bits, and nothing else. Python says so explicitly: 2001:db00::0/24 is valid and 2001:db00::0/ffff:ff00:: is not. The mask still exists as arithmetic, and occasionally surfaces, as in the IPv6 default of that BIG-IP virtual server option, which is written out as eight groups of ffff.

What goes wrong. Because the mask drives the local or remote decision, a wrong one fails in a characteristic way. A mask that is too short makes a host believe that distant addresses are on its own segment, so it tries to reach them directly and never uses the gateway; a mask that is too long makes it send its neighbours' traffic to the gateway, or leaves the gateway itself outside the subnet. Old software fills in the classful mask when none is given, as ifconfig still documents, and the first version of the Routing Information Protocol (RIP) carried no mask at all. Software that validates masks rejects the malformed ones: Python raises an error for a mask that mixes ones and zeros, and RFC 1812 tells a router to check that an interface mask is neither all ones nor all zeros and that its one bits are contiguous. The lasting advice is in RFC 1812 itself: both representations are in common use, so an engineer has to read both, and convert without hesitation.

Also known as: subnet mask, network mask, net mask, 255.255.255.0

Sources

← Back to N Back to the glossary