IP addresses

IP addresses

+------------------------++-----------------------+
|  ip addr1 -> 10.1.1.1  || ip addr2 -> 10.1.1.2  |
+------------------------++-----------------------+
             |                        |
             |                        |
             +------------------------+
             |
             v
  +--------------------+
  |                    |
  |       Device       |
  |                    |
  +--------------------+
  • One device can have one or more than one IP address.

When I do ip address in Linux, it will print out:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2. eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:1a:b1:01 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::784:a34a:587c:970/64 scope link
       valid_lft forever preferred_lft forever

Here, the first lo is loopback. Can see it binds an ipv4 address 127.0.0.1.
And eth0 binds an address 10.0.2.15.

Add an address to a device

If I run ip addr add 10.0.2.16 dev eth0, it will add an ip address 10.0.2.16,
to eth0. Show in a graph:

+------------------------++-----------------------+
| ip addr1 -> 10.0.2.15  || ip addr2 -> 10.0.2.16*|
+------------------------++-----------------------+
             |                        |
             |                        |
             +------------------------+
             |
             v
  +--------------------+
  |                    |
  |        eth0        |
  |                    |
  +--------------------+

The result of the query also will look like:

eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:1a:b1:01 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.0.2.16/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::784:a34a:587c:970/64 scope link
       valid_lft forever preferred_lft forever

The different between inet 10.0.2.15 and inet 10.0.2.16 is that, 10.0.2.16, don’t have a brd option, and the brd value is 10.0.2.255, this brd is called broadcast address. What does it do?

broadcast address

Broadcast addresses are special values in the host-identification part of an IP address. And it used for sending diagrams(aka udp) packets.

How to calculate a broadcast address?

If an IP using a subnet space 172.16.0.0/12, then its subnet mask is:

12 ones -> 11111111 11110000 00000000 00000000
then invert it
           00000000 00001111 11111111 11111111

the binary ip is:

172.16.0.0 -> 10101100 00010000 00000000 00000000

So, the broadcast IP will be:

    10101100 00010000 00000000 00000000
or  00000000 00001111 11111111 11111111
---------------------------------------
    10101100 00011111 11111111 11111111
    172      31       255      255

Actual use cases for the broadcast address

Host configuration protocol, BOOTP and DHCP. From the Microsoft Technet, it
said that:

The bootstrap protocol (BOOTP) is a host configuration protocol developed before DHCP.

So I will only focus on DHCP here.

DHCP -> Dynamic Host Configuration Protocol

What is DHCP for? When a device connects to an internet gateway, it needs an IP address to identify itself in the network. Instead of configured by human manually. DHCP helps the administrator to do that work.

About 255.255.255.255

255.255.255.255 is a special broadcast address, which means “this network”: it lets you send a broadcast packet to the network you’re connected to, without actually caring about its address. In this, is similar to 127.0.0.1, which is a virtual address meaning “localhost”.

When the time I ran the ip addr add command, I didn’t say what network mask it will use. So it has the result like this:

inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
   valid_lft forever preferred_lft forever
inet 10.0.2.16/32 scope global eth0
   valid_lft forever preferred_lft forever

Which means that the broadcast address of ip 10.0.2.16 is 255.255.255.255.
So if I need to change it to make sure that two ips share the same broadcast address. Just simply remove the 16 one and add it back with network mask.

ip address delete 10.0.2.16/32 dev eth0
ip address add 10.0.2.16/24 dev eth0 brd +

If I do ip address add 10.0.2.16/24 dev eth0 brd +, it will be different.
The host bit will be reset.