Now let's imagine another IP network 192.168.12.0/24 ("network B"), on which are connected computer E (with IP address 192.168.12.5) and computer F (with address 192.168.12.6). These computers are also connected through an Ethernet network, but a different one from network A. What happens if computer C wants to send an IP packet to computer F? As they are not connected to the same Ethernet network, the ARP protocol cannot work here, and computer C has no way to know how to communicate with computer F. The solution is to use a router. A router is a device with several network interfaces, each one being connected to a different IP network, and able to transport IP packets from one network to another one (this is called routing or IP forwarding). In our example, a router can be used to connect networks A and B, like this:
As you can see, each interface of the router has its own IP address in a given IP network: here the router has an interface in network A, with the address 192.168.1.1, and an interface in network B, with the address 192.168.12.1. These interfaces are called the gateways of the networks, as they allow IP packets to go outside the network. By convention, the address of a gateway is usually the second IP of the address range of the network, for example the gateway of the network 192.168.1.0/24 has the address 192.168.1.1 (the address 192.168.1.0 is not used).
So, when computer C wants to send a packet to computer F, it has to know that computer F is outside its own IP network (it is easy to know, as the IP address 192.168.12.6 is not in the address range of network 192.168.1.0/24), and in that case it sends the packet to the gateway 192.168.1.1. To take again the post office analogy: if you want to send a letter to someone who is in your street (in your IP network), you can bring directly the letter to his mailbox. But if you want to send a letter to someone in another city, you cannot bring the letter directly, so you drop it in the mailbox of the nearest post office (the gateway of the router).
So, when computer C wants to send a packet to computer F, it has to know that computer F is outside its own IP network (it is easy to know, as the IP address 192.168.12.6 is not in the address range of network 192.168.1.0/24), and in that case it sends the packet to the gateway 192.168.1.1. To take again the post office analogy: if you want to send a letter to someone who is in your street (in your IP network), you can bring directly the letter to his mailbox. But if you want to send a letter to someone in another city, you cannot bring the letter directly, so you drop it in the mailbox of the nearest post office (the gateway of the router).
When a router receives an IP packet, it looks at the destination address, and forwards the packet to the interface which is in the corresponding IP network. In this example, the router can send the packet directly to computer F thanks to the Ethernet protocol, as they are both part of the same Ethernet network.
Any IP router, and actually any computer, phone, etc. connected to an IP network, has a routing table. The routing table allows the device to know on which network interface it has to send an IP packet, depending on the destination IP address. On Linux, the routing table can be displayed with the command "route -n" (add the -n option to avoid DNS resolution; if you don't know what it means, just put the option ;-) ). On Windows or Mac OS X, you can display the routing table with "netstat -r" command. Here is an example of routing table on Linux:
When the gateway address has the special value 0.0.0.0 (or "*"), it means that no gateway is needed to send a packet on this network (the packet can be sent directly to its destination), as all the hosts of this network are directly connected to the same Ethernet network as the corresponding interface. In this example, the network interface "eth0" is connected to an Ethernet network in which all the hosts have an IP address in the range 172.16.110.0 -> 172.16.111.255.
The destination address 0.0.0.0 is also special, and is called the default address; the line which has this address in the routing table gives the interface and gateway (called the default gateway) which have to be used when no other line in the routing table matches the destination address of the packet. For instance an IP packet with a destination address of 138.15.7.145 doesn't match any of the networks 172.16.110.0/23, 192.168.1.0/16 or 127.0.0.0/8, so it will be sent on the interface eth0, to the Ethernet device which has the IP address 172.16.110.1, which is the gateway of a router. Note that (on Linux) if your routing table has no default gateway, you will get an error "No route to host" if you try to send an IP packet to an unknown destination.
Now try yourself to display and understand the routing table of your computer!
Any IP router, and actually any computer, phone, etc. connected to an IP network, has a routing table. The routing table allows the device to know on which network interface it has to send an IP packet, depending on the destination IP address. On Linux, the routing table can be displayed with the command "route -n" (add the -n option to avoid DNS resolution; if you don't know what it means, just put the option ;-) ). On Windows or Mac OS X, you can display the routing table with "netstat -r" command. Here is an example of routing table on Linux:
# route -nThe columns "Destination" and "Genmask" give the network and its mask. As explained in the previous article, the network mask is a way to represent the number of significant bits of the network address; for instance 172.16.110.0 with a mask of 255.255.254.0 actually means 172.16.110.0/23. The column "Iface" gives the network interface on which IP packets must be sent for the given network, and the column "Gateway" is the address of the gateway to use for this network.
Kernel IP routing table
Destination Gateway Genmask Iface
172.16.110.0 0.0.0.0 255.255.254.0 eth0
192.168.1.0 0.0.0.0 255.255.0.0 eth1
127.0.0.0 0.0.0.0 255.0.0.0 lo
0.0.0.0 172.16.110.1 0.0.0.0 eth0
When the gateway address has the special value 0.0.0.0 (or "*"), it means that no gateway is needed to send a packet on this network (the packet can be sent directly to its destination), as all the hosts of this network are directly connected to the same Ethernet network as the corresponding interface. In this example, the network interface "eth0" is connected to an Ethernet network in which all the hosts have an IP address in the range 172.16.110.0 -> 172.16.111.255.
The destination address 0.0.0.0 is also special, and is called the default address; the line which has this address in the routing table gives the interface and gateway (called the default gateway) which have to be used when no other line in the routing table matches the destination address of the packet. For instance an IP packet with a destination address of 138.15.7.145 doesn't match any of the networks 172.16.110.0/23, 192.168.1.0/16 or 127.0.0.0/8, so it will be sent on the interface eth0, to the Ethernet device which has the IP address 172.16.110.1, which is the gateway of a router. Note that (on Linux) if your routing table has no default gateway, you will get an error "No route to host" if you try to send an IP packet to an unknown destination.
Now try yourself to display and understand the routing table of your computer!
Aucun commentaire:
Enregistrer un commentaire