Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The "right" interface is the entry with the most specific netmask whose IP address is on the same subnet as your input.</p> <p>Let's look at what netmasks are, and how they work, in more detail.</p> <h2>Notation</h2> <p>Although netmasks are usually written in dotted-decimal or hex notation, the binary representation of an IPv4 netmask is always 32 bits; that is, it's exactly the same length as an IP address. The netmask always starts with zero or more <code>1</code> bits and is padded with <code>0</code> bits to complete its 32-bit length. When a netmask is applied to an IP address, they're "lined up" bit by bit. The bits in the IP address that correspond to the <code>1</code> bits in the netmask determine the <em>network number</em> of the IP address; those corresponding to the <code>0</code> bits in the netmask determine the <em>device number</em>.</p> <h2>Purpose</h2> <p>Netmasks are used to divide an address space into smaller <em>subnets</em>. Devices on the same subnet can communicate with each other directly using the TCP/IP protocol stack. Devices on different subnets must use one or more <em>routers</em> to forward data between them. Because they isolate subnets from each other, netmasks are a natural way to create logical groupings of devices. For example, each location or department within a company may have its own subnet, or each <em>type</em> of device (printers, PCs, <em>etc</em>.) may have its own subnet.</p> <p><strong>Example netmasks:</strong></p> <p><code>255.255.255.128</code> &rarr; <code>FF FF FF 10</code> &rarr; <code>1111 1111</code> <code>1111 1111</code> <code>1111 1111</code> <code>1000 0000</code><br> This netmask specifies that the first 25 bits of an IP address determine the network number; the final 7 bits determine the device number. This means there can be 2<sup>25</sup> different subnets, each with 2<sup>7</sup> = 128 devices.<sup>*</sup> </p> <p><code>255.255.255.0</code> &nbsp; &nbsp; &rarr; <code>FF FF FF 00</code> &rarr; <code>1111 1111</code> <code>1111 1111</code> <code>1111 1111</code> <code>0000 0000</code><br> This netmask specifies an address space with 2<sup>24</sup> subnets, each with 2<sup>8</sup> = 256 individual addresses. This is a very common configuration&mdash;so common that it's known simply as a "Class C" network.</p> <p><code>255.255.192.0</code> &nbsp; &nbsp; &rarr; <code>FF FF FC 00</code> &rarr; <code>1111 1111</code> <code>1111 1111</code> <code>1111 1100</code> <code>0000 0000</code><br> This netmask specifies 2<sup>22</sup> subnets, each with 2<sup>10</sup> = 1024 addresses. It might be used inside a large corporation, where each department has several hundred devices that should be logically grouped together.</p> <p>An <strong><em>invalid</em></strong> netmask (note the internal zeroes):<br> <code>255.128.255.0</code> &nbsp; &nbsp; &rarr; <code>FF 80 FF 00</code> &rarr; <code>1111 1111</code> <code>1000 0000</code> <code>1111 1111</code> <code>0000 0000</code></p> <h2>Calculations</h2> <p>Here are a few examples that show how a netmask determines the network number and the device number of an IP address. </p> <p>&nbsp; IP Address: <code>192.168.0.1</code> &rarr; <code>C0 A8 00 01</code><br> &nbsp; Netmask: <code>255.255.255.0</code> &rarr; <code>FF FF FF 00</code><br> This device is on the subnet 192.168.0.0. It can communicate directly with other devices whose IP addresses are of the form 192.168.0.<em>x</em></p> <p>&nbsp; IP Address: <code>192.168.0.1</code> &nbsp; &nbsp; &rarr; <code>C0 A8 00 01</code><br> &nbsp; IP Address: <code>192.168.0.130</code> &rarr; <code>C0 A8 00 82</code><br> &nbsp; Netmask: <code>255.255.255.128</code> &rarr; <code>FF FF FF 80</code><br> These two devices are on different subnets and cannot communicate with each other without a router.</p> <p>&nbsp; IP Address: <code>10.10.195.27</code> &rarr; <code>0A 0A C3 1B</code><br> &nbsp; Netmask: <code>255.255.0.0</code> &rarr; <code>FF FF 00 00</code><br> This is an address on a "Class B" network that can communicate with the 2<sup>16</sup> addresses on the 10.10.0.0 network.</p> <p>You can see that the more <code>1</code> bits at the beginning of a netmask, the more specific it is. That is, more <code>1</code> bits create a "smaller" subnet that consists of fewer devices.</p> <h2>Putting it all together</h2> <p>A routing table, like yours, contains triplets of netmasks, IP addresses, and interfaces. (It may also contain a "cost' metric, which indicates which of several interfaces is the "cheapest" to use, if they are both capable of routing data to a particular destination. For example, one may use an expensive dedicated line.)</p> <p>In order to route a packet, the router finds the interface with the most specific match for the packet's destination. An entry with an address <code>addr</code> and a netmask <code>mask</code> matches a destination IP address <code>dest</code> if <code>(addr &amp; netmask) == (dest &amp; netmask)</code>, where <code>&amp;</code> indicates a bitwise <code>AND</code> operation. In English, we want the <em>smallest subnet</em> that is <em>common to both addresses</em>.</p> <p>Why? Suppose you and a colleague are in a hotel that's part of a huge chain with both a corporate wired network and a wireless network. You've also connected to your company's VPN. Your routing table might look something like this:</p> <pre> Destination Netmask Interface Notes ----------- -------- --------- ----- Company email FFFFFF00 VPN Route ALL company traffic thru VPN Wired network FFFF0000 Wired Traffic to other hotel addresses worldwide Default 00000000 Wireless All other traffic </pre> <p>The most specific rule will route your company email safely through the VPN, even if the address happens to match the wired network also. All traffic to other addresses within the hotel chain will be routed through the wired network. And everything else will be sent through the wireless network.</p> <p><br><br></p> <p><sup>*</sup> Actually, in every subnet, 2 of the addresses&mdash;the highest and the lowest&mdash;are reserved. The all-ones address is the <em>broadcast</em> address: this address sends data to <em>every device</em> on the subnet. And the all-zeroes address is used by a device to refer to itself when it doesn't yet have it's own IP address. I've ignored those for simplicity.</p> <hr> <p>So the algorithm would be something like this:</p> <pre><code>initialize: Sort routing table by netmask from most-specific to least specific. Within each netmask, sort by IP address. search: foreach netmask { Search IP addresses for (input &amp; netmask) == (IP address &amp; netmask) Return corresponding interface if found } Return default interface </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload