Until now, the communications considered involved:
- either only addresses of the form 192.168.10.? connected to hub or
switch 9991,
- or only addresses of the form 192.168.20.? connected to hub or
switch 9992.
At no point did an address of the form
192.168.10.? attempt
to reach an address of the form
192.168.20.? (or vice versa).
Before considering these new communications, first set up packet captures
with
tcpdump -eni eth0 on both
C1 and
C2.
From
A1A2, then use
ping -c2 192.168.10.3 to communicate with
C1, followed by
ping -c2 192.168.20.3 to communicate with
C2.
Carefully observe the source IP addresses of the
“ICMP echo request”
packets captured in both cases.
You should find that, depending on the destination address, the
A1A2
machine decides to identify itself using one IP address or the other
(even though it has both).
To understand this choice, use the
ip route command on
A1A2;
this is its
routing table.
It indicates that all addresses of the form
192.168.10.0/24 are
directly reachable via the
eth0 interface, and likewise that
addresses of the form
192.168.20.0/24 are reachable via the
eth1 interface.
Such notations, also shown on the diagram at the beginning of this tutorial,
mean
“all IP addresses that produce 192.168.10.0 (respectively
192.168.20.0) when they are masked with the first 24 bits set
to 1” (and the remaining 8 bits set to 0, since IPv4 addresses are 32 bits
long)
The masking operation consists of applying a bitwise AND operation
between an address and a mask.
Wherever the mask is 1, the corresponding bit of the address retains
its value.
Wherever the mask is 0, the corresponding bit of the address is forced
to 0.
.
When
A1A2 attempts to reach
192.168.10.3, the first entry in the
routing table suggests combining this address with a 24-bit mask to check
whether the result is indeed
192.168.10.0.
Since this is the case, that entry is selected and indicates that the
eth0 interface should be used to reach this destination;
the source address of the transmitted IP packet will therefore be that of
this interface (
192.168.10.1).
When
A1A2 attempts to reach
192.168.20.3, combining this address
with a 24-bit mask does not produce
192.168.10.0;
the first entry in the routing table is therefore not applicable.
However, there is a second entry
The lines are not necessarily tested in the order in which they are
displayed.
The most specific (longest) masks are tested first.
Here, both masks have the same length (/24), so the order in
which the two entries are tested does not matter.
that again suggests combining the destination address with a 24-bit mask,
and this time the result is indeed
192.168.20.0; this entry is
therefore selected and indicates that the
eth1 interface should be
used to reach this destination.
The source address of the transmitted IP packet will therefore be that of
this interface (
192.168.20.1).
These two entries in the routing table were not entered manually but
automatically.
Indeed, the
eth0 interface is configured with
192.168.10.1/24:
an IP address and its associated
subnet mask.
This mask indicates that the interface is connected to a
subnet
in which all IP addresses share the same first 24 bits as this address,
that is, the
192.168.10.0/24 subnet, which is automatically added
to the routing table.
The same applies to the
eth1 interface, configured with
192.168.20.1/24, which belongs to the
192.168.20.0/24
subnet.
It is therefore very important to follow this rule:
all IP addresses within the same subnet must be associated
with the same subnet mask and must be identical for all
the bits covered by that mask.
It is very common to use the notation expressing the
length
of the mask (24 bits in this case); however, some devices require
the mask to be written explicitly as an IP address (
255.255.255.0
is equivalent to the
/24 notation).
It is very common to choose masks that end on a byte boundary (
/16 or
/24, for example), but this is not mandatory at all; it simply makes
them easier to read and the calculations easier to perform
(exercises illustrating this are provided
later on).
A
subnet is generally implemented using a
switch
Or, historically, a hub, as discussed earlier.
, or several interconnected switches to provide more ports:
this is known as a
broadcast domain.
Indeed, all the interfaces that make up the subnet are expected to
participate in the ARP protocol (studied earlier); an ARP
request relies on the
broadcasting of a frame to reach
all the interfaces within such a subnet.
Use the
ip route command on the
B1,
B2,
C1 and
C2
machines; the routing tables in question contain only a single entry,
since only one network interface is configured.
From
C1, then try
ping 192.168.10.4: this address has not been
assigned, but in theory it could belong to
C1's subnet.
You should find that the command attempts to reach this address but receives
no reply.
Still from
C1, now try
ping 192.168.20.1 and observe the immediate
failure, without any attempt being made; indeed,
C1's routing table
can only reach addresses in the
192.168.10.0/24 subnet.
Complete the network configuration of
C1 as follows
For convenience, these lines can be edited in a
SHARED/script.sh file on the host system and then executed
with bash SHARED/script.sh from the virtual machine.
:
cat <<EOF >>/etc/custom/10_network_setup.sh
ip route add 192.168.20.0/24 via 192.168.10.1
EOF
/etc/custom/10_network_setup.sh
Check the updated state of the routing table using
ip route.
You have just completed it by a
route indicating that the
192.168.20.0/24 subnet can now be reached provided that traffic
first passes through the
192.168.10.1 gateway, which is itself
directly reachable.
Try
ping 192.168.20.1 again from
C1 and observe that you now
receive a reply.
While capturing traffic on
A1A2 with
tcpdump -eni eth0, you
should observe that the frames intended for
192.168.20.1 are
addressed to the MAC address corresponding to
192.168.10.1,
rather than to the one corresponding to the destination IP address.
This is indeed a phenomenon that is characteristic of routing.
- IP packets specify the source and destination IP addresses of the end
hosts,
- but they are carried within frames that specify the MAC addresses
hop by hop, across each subnet they traverse.
In theory, with the explicit routing rule,
C1 should also be able
to reach
B2 and
C2.
However,
ping 192.168.20.3 from
C1 does not receive a reply.
On
A1A2, use
tcpdump -eni eth0 to verify that the IP packet does
indeed arrive on this interface.
Then use
tcpdump -eni eth1 and observe that this packet is not
forwarded out through the other interface.
Indeed, by default, when network devices receive an IP packet that is not
addressed to them, they simply ignore it.
A1A2 replied earlier because
192.168.20.1 is one of its
own addresses, but this is not the case for
192.168.20.3.
Complete the network configuration of
A1A2 as follows
For convenience, these lines can be edited in a
SHARED/script.sh file on the host system and then executed
with bash SHARED/script.sh from the virtual machine.
:
cat <<EOF >>/etc/custom/10_network_setup.sh
echo 1 >/proc/sys/net/ipv4/ip_forward
EOF
/etc/custom/10_network_setup.sh
This setting tells the
A1A2 system that it should behave as a
router; from now on, it must
make the effort to forward IP packets
that are not addressed to it directly, applying the same routing
decisions as if it were the sender of the packet.
Observe that the
“ICMP echo request” packet is now correctly retransmitted
via the
eth1 interface towards
C2; however, there is still no
reply.
For
C2 to be able to reply to
C1, it must know a route that allows
it to do so.
Complete the network configuration of
C2 as follows
For convenience, these lines can be edited in a
SHARED/script.sh file on the host system and then executed
with bash SHARED/script.sh from the virtual machine.
:
cat <<EOF >>/etc/custom/10_network_setup.sh
ip route add 192.168.10.0/24 via 192.168.20.1
EOF
/etc/custom/10_network_setup.sh
Check the updated state of
C2's routing table with
ip route.
From now on, the
ping 192.168.20.3 command from
C1 should receive
a reply.
Of course, for the situation to be complete, the same procedure must be
applied to all the remaining machines.
Complete the network configuration of
B1 as follows
For convenience, these lines can be edited in a
SHARED/script.sh file on the host system and then executed
with bash SHARED/script.sh from the virtual machine.
:
cat <<EOF >>/etc/custom/10_network_setup.sh
ip route add 192.168.20.0/24 via 192.168.10.1
EOF
/etc/custom/10_network_setup.sh
Check the updated state of
B1's routing table with
ip route.
Complete the network configuration of
B2 as follows
For convenience, these lines can be edited in a
SHARED/script.sh file on the host system and then executed
with bash SHARED/script.sh from the virtual machine.
:
cat <<EOF >>/etc/custom/10_network_setup.sh
ip route add 192.168.10.0/24 via 192.168.20.1
EOF
/etc/custom/10_network_setup.sh
Check the updated state of
B2's routing table with
ip route.
Check that, from now on, each of your machines is able to reach any other
machine.