Bienvenido! - Willkommen! - Welcome!

Bitácora Técnica de Tux&Cía., Santa Cruz de la Sierra, BO
Bitácora Central: Tux&Cía.
Bitácora de Información Avanzada: Tux&Cía.-Información
May the source be with you!

Wednesday, November 17, 2010

Subnet calculus

CIDR notation is a compact specification of an Internet Protocol address and its associated routing prefix. Classless Inter-Domain Routing (CIDR) is an Internet Protocol (IP) address allocation and route aggregation methodology[1] used within the Internet addressing architecture that replaced the IPv4 classful network organization of the IP address space. It is used also for IPv6 networking, the next generation of the IP addressing architecture.
CIDR notation is constructed from the IP address and the prefix size, the latter being the number of leading 1 bits of the routing prefix. The IP address is expressed according to the standards of IPv4 or IPv6. It is followed by a separator character, the forward slash (/) character, and the prefix size expressed as a decimal number.
The address may denote a single, distinct, interface address or the beginning address of an entire network. In the latter case the CIDR notation specifies the address block allocation of the network. The maximum size of the network is given by the number of addresses that are possible with the remaining, least-significant bits below the prefix. This is often called the host identifier.
For example:
  • the address specification represents the given IPv4 address and its associated routing prefix, or equivalently, its subnet mask
  • the IPv4 block represents the 1024 IPv4 addresses from to
  • the IPv6 block 2001:DB8::/48 represents the IPv6 addresses from 2001:DB8:0:0:0:0:0:0 to 2001:DB8:0:FFFF:FFFF:FFFF:FFFF:FFFF.
  • ::1/128 represents the IPv6 loopback address. Its prefix size is 128, i.e. the size of the address itself, indicating that this facility consists of only this one address.
Before CIDR notation, IPv4 networks usually used the dot-decimal notation, an alternative representation which uses the network address followed by the network's subnet mask. Thus, The CIDR notation would be written as
The number of addresses of a subnet defined by the mask or prefix can be calculated as 2address size - mask, in which the address size for IPv6 is 128 and 32 for IPv4. For example, in IPv4, a mask of /29 gives: 232-29 = 23 = 8 addresses.
However, because at least one of these addresses is typically used for a gateway to other subnets, and because certain addresses are reserved as broadcast addresses, the number of addresses available for hosts is usually smaller.
IPv4 subnetting referenceCALCULUS
Linux comes with various IP subnet calculator that will help you with network settings. Once such program is Sipcalc which is an ip subnet calculator. You can install it as follows under Debian or Ubuntu Linux:
$ sudo apt-get update
$ sudo apt-get install sipcalc

Download sipcalc
How Do I Calculate Subnets?
$ sipcalc
Sample outputs:
-[ipv4 :] - 0
Host address  -
Host address (decimal) - 3232235776
Host address (hex) - C0A80100
Network address  -
Network mask  -
Network mask (bits) - 24
Network mask (hex) - FFFFFF00
Broadcast address -
Cisco wildcard  -
Addresses in network - 256
Network range  - -
Usable range  - -
The above will provide network start & stop range, wildcard, mask and other information. You can calculate subnet as follows
$ sipcalc
Interface Specific Calculation Instead of taking address information from the shell command line arg sipcalc can obtain relevant information by looking at a specified interface on the system. In this example, get information for eth0 interface:
$ sipcalc eth0
Sample outputs:
-[int-ipv4 : eth0] - 0
Host address  -
Host address (decimal) - 3232236542
Host address (hex) - C0A803FE
Network address  -
Network mask  -
Network mask (bits) - 24
Network mask (hex) - FFFFFF00
Broadcast address -
Cisco wildcard  -
Addresses in network - 256
Network range  - -
Usable range  - -
whatmask Program
Whatmask is a small C program that will help you with network settings.
Download whatmask
Download and compile it as follows:
$ cd /tmp
$ wget
$ tar -zxvf whatmask-1.2.tar.gz
$ cd whatmask-1.2/
$ ./configure
$ make
$ sudo make install

You can use it as follows to find out usable ip address with /29:
$ whatmask /29
Sample outputs:
CIDR = .....................: /29
Netmask = ..................:
Netmask (hex) = ............: 0xfffffff8
Wildcard Bits = ............:
Usable IP Addresses = ......: 6
$ whatmask
Sample outputs:
IP Entered = ..................:
CIDR = ........................: /27
Netmask = .....................:
Netmask (hex) = ...............: 0xffffffe0
Wildcard Bits = ...............:
Network Address = .............:
Broadcast Address = ...........:
Usable IP Addresses = .........: 30
First Usable IP Address = .....:
Last Usable IP Address = ......:
  • Whatmask can work in two modes. The first mode is to invoke Whatmask with only a subnet mask as the argument. In this mode Whatmask will echo back the subnet mask in four formats, plus the number of useable addresses in the range.
Netmask Notations supported:
Name                  Example
 CIDR                         /24
 Netmask (hex)         0xffffff00
 Wilcard Bits 
The above notations are all identical. CIDR notation commonly has a "/" in front of the number (representing the number of bits). Whatmask can accept these notations with or without a slash. This notation is used more and more recently. A lot of popular routers and software support this notation.
Netmask notation is pretty much the standard old-school way of doing it. It is supported by most systems (Un*x, Win, Mac, etc.).
Netmask (Hex) is the hexadecimal representation of the netmask. Many implementations of ifconfig use this notation.
Wilcard bits are similar to the netmask, but they are the logical not of the netmask. This notation is used by a number of popular routers (and nobody knows why... ... actually I found a great explanation from 'billf' who removed the nobody knows why line from the FreeBSD port: Netmasks are contiguous bits, wildcard masks don't need to be).
To use Whatmask in the original mode simply type "whatmask " The notation can be in any of the four formats and Whatmask will automagically figure out what it is and display all four notations.
To find out more about subnets and netmasks see the References section below.
To use Whatmask in its second mode execute Whatmask with any ip address within the subnet, followed by a slash ('/'), followed by the subnet mask in any format. (e.g., or
Whatmask will echo back the following:
  • The netmask in the following formats: CIDR, Netmask, Netmask (Hex) Wildcard Bits
  • The Network Address
  • The Broadcast Address
  • The number of Usable IP Addresses
  • The First Usable IP Address
  • The Last Usable IP Address
(Whatnet assumes that the Broadcast address is the highest address in the subnet. This is the most common configuration.)

No comments: