## Gather information using an IP address and subnet mask ##

[Author](authors.html): Cezary Morga cm@therek.net FreeBSD

[Reviewer](technical_reviewers.html): _name_ _[[contact]]_ _[[BSD_flavour]]_

[Reviewer](technical_reviewers.html): Yannick Cadin yannick@diablotin.fr FreeBSD/OpenBSD

----



### Concept ###

Given an IPv4 address and subnet mask, be
able to determine the subnet address, broadcast address and the valid
host addresses available on that subnet address.


### Introduction ###

Configuring BSD to work in network environment requires proficiency
in operating on IP addresses and subnet masks. To understand different
subnet mask's notations used throughout this section please refer to
[[Convert_a_subnet_mask_between_dotted_decimal,_hexadecimal_or_CIDR_notation]].

### Examples ###

Having a host with an IP address of `192.168.1.25/24` we can determine
without any further calculations:

""subnet identifier:               192.168.1.0
""broadcast address:               192.168.1.255
""number of valid host addresses:  254

The subnet mask of `/24` in CIDR notation stands for `255.255.255.0`.
The IPv4 uses a 32-bit IP addressing and network masks, which means
that the above subnet mask have last eight bits set to zero (`32 - 24
= 8`), which also mean that we have `2^8` IP addresses available.
Subtracting two addresses for subnet address (first address) and broadcast
address (last address) we get number of valid hosts:

""2^8 - 2 = 256 - 2 = 254

As for the address of `192.168.1.25/28` the paremeters in question are:

""subnet identifier:               192.168.1.16
""broadcast address:               192.168.1.31
""number of valid host addresses:  14

This is quite more interesting example. Having a CIDR subnet mask of `/28`
(four bits set to zero)--which is `255.255.255.240` in dotted decimal
notation--means that there are 16 addresses available, and only 14 of
them are valid host addresses:

""2^4 - 2 = 16 - 2 = 14

For networks smaller than 256 IP addresses there's a simple way
to determine number of available addresses using a 'neat trick'
as described in Daryl's TCP/IP Primer (link at the end of this
subject). To do so we can simply subtract the last number of the
subnet mask from 256. For aformentioned subnet mask of
`255.255.255.240` we'll have `256 - 240 = 16` addresses. Now, dividing
the result into 256 we can determine the number of subnets (`256
/ 16 = 16`), which gives us a 16 subnets of 16 addresses each.
The scope of the first one is `192.168.1.0 - 192.168.1.15`, the
second one `192.168.1.16 - 192.168.1.31` and so forth. Our IP
address of `192.168.1.25` is located within the second one.

To make it all more confusing let's try to determine subnet's
parameters having a `192.168.1.25/22` IP address:

""subnet identifier:               192.168.0.0
""broadcast address:               192.168.3.255
""number of valid host addresses:  1022

The CIDR subnet mask of `/22` gives us 10 bits defining the hosts
addresses.

""2^10 - 2 = 1024 - 2 = 1022

This gives us networks with a scope of IP addresses: `192.168.0.0 -
192.168.3.255`, `192.168.4.0 - 192.168.7.255`, etc.

In closing, the partial reference table on IPv4 subnets:

"" CIDR |     Netmask     | Addresses
""-----+-----------------+-----------
"" /18  | 255.255.192.0   | 16384
"" /19  | 255.255.224.0   | 8192
"" /20  | 255.255.240.0   | 4096
"" /21  | 255.255.248.0   | 2048
"" /22  | 255.255.252.0   | 1024
"" /23  | 255.255.254.0   | 512
"" /24  | 255.255.255.0   | 256
"" /25  | 255.255.255.128 | 128
"" /26  | 255.255.255.192 | 64
"" /27  | 255.255.255.224 | 32
"" /28  | 255.255.255.240 | 16
"" /29  | 255.255.255.248 | 8
"" /30  | 255.255.255.252 | 4
"" /31  | 255.255.255.254 | 2
"" /32  | 255.255.255.255 | 1

Note that the `/32` subnet mask actually points to only one host,
and `/31` subnet is simply useless as there are no addresses
left for hosts.

### Practice Exercises ###

1. Determine subnet identifier, broadcast address and number of
valid host addresses having: `192.168.86.2/24`, `10.0.9.7/26`,
`192.168.159.8/25`, and `172.16.0.189/18`.

### More information ###

http://www.ipprimer.com/bitbybit.cfm