Netmask
The key to understanding IP routing is the netmask. The netmask tells us whether we can communicate directly with another machine, or if we need to go via a router. If A wants to talk to B, well, they're on the same network, so A addresses the packet directly to B. If A wants to talk to E, it will have to send the packet to the (routing) firewall between those networks, as it cannot send directly to E:
But how does "A"
know when to send a simple packet and when to do the harder work?
If we assume that box "A" is Linux, and box "B" is Windows, we will see the following: (may look strange if your browser window is narrow)
root@A# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:E1:CC:62:34:53 inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2025455 errors:0 dropped:0 overruns:0 frame:0 TX packets:1969320 errors:2 dropped:0 overruns:0 carrier:4 collisions:0 txqueuelen:1000 RX bytes:1863973735 (1.7 GiB) TX bytes:1280459205 (1.1 GiB) Interrupt:185 Base address:0xb800 root@A#
And on the Windows box:
The Windows screenshot shows the purpose of the netmask most clearly, though a bit of binary (and maybe some hexadecimal) understanding is useful for more complex examples. This means that, if A wants to talk to B, it compares its own IP address and netmask with B's IP address:
A | 192 | 168 | 1 | 1 |
11000000 | 10101000 | 00000001 | 00000001 | |
Mask | 255 | 255 | 255 | 0 |
---|---|---|---|---|
11111111 | 11111111 | 11111111 | 00000000 | |
B | 192 | 168 | 1 | 2 |
11000000 | 10101000 | 00000001 | 00000010 | |
Result | Network | Network | Network | Host |
We need to perform a logical AND on the IP addresses and Netmask. We do this by looking down the columns; a "1" in the Netmask means that if both IP addresses are the same in that column, then they are on the same network, a "0" means that these bits can differ between hosts on the same network. Therefore, the 1's are referred to as the network address, and the 0's are referred to as the host address. In this case, 192.168.1.0 is the (common) network address, so .1 (for A) and .2 (for B) is the host address.
Please see Bases for more information about Base 2 (Binary) and Base 16 (Hexadecimal). See /xx notation for how this makes the /xx notation make sense, but in a nutshell, the example above has 24 "1"s in a row, so it is a /24 network.
This means that for A to communicate with B, it can create a simple packet, like this:
Source IP | 192.168.1.1 | (A) |
---|---|---|
Destination IP | 192.168.1.2 | (B) |
Data | Hello B! This is the Data |