## IP Addressing and Subnetting

There is considerable confusion with regards to the way that IP addresses, network masks, broadcast addresses and network numbers are related. Even the standard reference on TCP/IP networking does not discuss the topic with reference to the commands which can be issued in UNIX to see the results of configuration changes. This treatise will attempt to eliminate the confusion.

An important basis for the discussion is the representation of numbers in different bases. The binary system uses a base of 2, the octal system uses 8, decimal uses 10, and hexadecimal uses 16. The base of the numbering systems specifies the number of unique symbols which are used to represent numbers. The octal system, for example, uses a base of 8 and the 8 symbols used to represet octal numbers are: 0, 1, 2, 3, 4, 5, 6, and 7.

The digits of a number using any base are multiplied by the the base of the system raised to the power of the digit number, starting at 0 and increasing from right to left. As an example, the number 12310 (base 10) is equal to ( 1 x 102 ) + ( 2 x 101 ) + ( 3 x 100 ), or ( 1 x 100 ) + ( 2 x 10 ) + ( 3 x 1 ), or 123 decimal.

Similarly, the number 12348 (base 8, or octal) is equal to ( 1 x 83 ) + ( 2 x 82 ) + ( 3 x 81 ) + ( 4 x 80 ), or ( 1 x 512 ) + ( 2 x 64 ) + ( 3 x 8 ) + ( 4 x 1 ), or 668 decimal. The number 110102 (base 2, or binary) is equal to ( 1 x 24 ) + ( 1 x 23 ) + ( 0 x 22 ) + ( 1 x 21 ) + ( 0 x 20 ), or ( 1 x 16 ) + ( 1 x 8 ) + ( 0 x 4 ) + ( 1 x 2 ) + ( 0 x 1 ), or 26 decimal. Note that any number raised to the power of 0 is 1 and any number raised to the power of 1 is itself, i.e. 160 = 1, 161 = 16.

The IP address consists of a 32 bit quantity which consists of two parts: the network number and the host number. IP addresses are usually written in the "dotted-decimal" format consisting of four decimal numbers separated by periods. In this format, each decimal number represents 8 bits of the address. Thus, an IP address of 159.249.121.60 would correspond to the binary number 10011111111110010111100100111100.

The netmask is simply a bit-mask which specifies which part of an IP address is used for the network number and which part is used for the node number. When the normal version of the netmask is AND'ed with an address, what remains is the network number. When the inverse of the netmask is AND'ed with the address, what remains is the node number. Here is an example:

```IP address: 159.249.121.60              Netmask: 255.255.255.0

Network #:      10011111111110010111100100000000

Node #:         00000000000000000000000000111100
```

In this example we have used the C-language notation of ~ to indicate the inverse (or logical NOT) function. Converting the network number back to dotted-decimal format gives 159.249.121.0. The most important function of the subnet mask is to determine if an IP address is "local" to a network or "foreign". Local addresses can be reached directly on the local network while packets destined for a foreign addresses will have to be forwarded through an intermediate address.

To demonstrate this mechanism, imagine a host with a network address of 145.78.23.2 and a netmask of 255.255.0.0 (standard for a class B address, as we'll see later.) Now imagine a local application sending two packets; one to 145.78.23.10 and another to 159.249.121.70. We will and the local address and the detination address of the two packets and show the results.

```IP address:     10010001010011100001011100000010        (145.78.23.2)
Network #:      10010001010011100000000000000000        (145.78.0.0)

IP address:     10010001010011100001011100001010        (145.78.23.10)
Network #:      10010001010011100000000000000000        (145.78.0.0)

IP address:     10011111111110010111100101000110        (159.249.121.70)
Network #:      10011111111110010000000000000000        (159.249.0.0)
```

Since the network number of the 159.249.121.70 address does not match the network number of the local address, we know that this is destined for a foreign network. Various routing protocols specify how routing information can be exchanged among hosts on the internet. Open Shortest Path First (OSPF) and Border Gateway Protocol (BGP) are two common examples. Some TCP/IP implementations do not support any routing protocol and only permit the specification of a default address to send foreign packet through.

One of the base concepts of IP addressing is the use of address classes. The original network classes were designed to permit a flexible mechanism for building networks with differing numbers of hosts. Some sites with connection to the internet would have a small number of hosts while others would have a much larger host population. Associated with each class of address is a standard netmask. The following diagram shows the standard IP address classes.

A 0xxxxxxx 255.0.0.0
B 10xxxxxx 255.255.0.0
C 110xxxxx 255.255.255.0

Assignment of IP addresses for organizations wishing to connect to the internet is performed by the Network Information Centre. Organizations can request which address class they require, with consideration being given to the total expected number of hosts expected to exist on the network. While a large organization such as IBM might be assigned a class A address class, mid-sized organizations will typically request and be assigned a class B address.

Internet access providers, such as UUNET, typically specify the addresses which will be used by their customers. An organization administering their own address domain can specify the addresses used by each sub-network connected to the corporate backbone. In order to break an address range into smaller groups, we use a mechanism known as sub-netting.

While we previously mentioned the "typical" values of the network mask based on address class, it is perfectly acceptable to use additional bits of the address to indicate the host and network portions of the IP address. It is important to remember that we are sub-netting the address assigned by the NIC and must use a netmask of at least the value shown in the table.

Since the bits in the netmask which indicate the network portion of an address must be contiguous, we allocate additional bits to be used as the netmask from left to right. The following table shows the number of bits used for the submask, the binary representation, and the dotted-decimal representation.

Number of bits Binary netmask Dotted-decimal netmask
16 11111111111111110000000000000000 255.255.0.0
17 11111111111111111000000000000000 255.255.128.0
18 11111111111111111100000000000000 255.255.192.0
19 11111111111111111110000000000000 255.255.224.0
20 11111111111111111111000000000000 255.255.240.0
21 11111111111111111111100000000000 255.255.248.0
22 11111111111111111111110000000000 255.255.252.0
23 11111111111111111111111000000000 255.255.254.0

The dotted decimal values for other configurations can be determined through use of the mathematical concepts discussed earlier. It is significant that a network can be variably subnetted. We could for example have one network with a network address of 159.249.128.0 and a subnet mask of 255.255.255.128 and two networks, 159.249.64.0 and 159.249.32.0, with a netmask of 255.255.224.0.

```IP address:     10011111111110010000000000000000        (159.249.128.0)
Network #:      10011111111110010000000000000000        (159.249.128.0)

IP address:     10011111111110001000000000000000        (159.249.64.0)
Network #:      10011111111110001000000000000000        (159.249.64.0)

IP address:     10011111111110000100000000000000        (159.249.32.0)
Network #:      10011111111110000100000000000000        (159.249.32.0)
```
Let's take a look at each of these networks and see how they handle packets destined for the other subnets. We’ll start with the 159.249.128.0 network and send packets to 159.249.64.1 and 159.249.32.2.
```IP address:     10011111111110000010000000000001        (159.249.64.1)
Network #:      10011111111110000000000000000000        (159.249.0.0)

IP address:     10011111111110000010000000000010        (159.249.32.2)
Network #:      10011111111110000000000000000000        (159.249.0.0)
```

In both cases we can see that the resulting network number is not the local network number and so the packets would have to be forwarded to another network. Here is the example for the 159.249.64.0 network with netmask 255.255.255.224 and destination addresses of 159.249.128.3 and 159.249.32.2

```IP address:     10011111111110001000000000000011        (159.249.128.3) 