5

When assigning an IP address to an interface, why do I need to specify a subnet mask? I understand the purpose of subnet masks in a routing table for example (to specify a range of IP addresses as destination). But why is it necessary on an interface? What uses it?

Why is it specified on the interface if it is already specified in the routing table?

6

The router uses the IP address and mask configured on an interface to determine what network is attached to that interface. That network and mask then become part of the routing table as a directly connected network.

There are three ways a router learns about networks:

  1. Directly connected networks
  2. Statically configured networks
  3. Networks learned through a routing protocol

A host will use the mask to determine if a destination address is on the same network as the host. If it is on a different network, the host must send the traffic, at layer-2, to the configured gateway instead of directly to the destination.

  • I understand how routing works and why netmasks are used in a routing table. What I don't understand is why I need to specify a netmask when giving an address to my interface. – Gradient Sep 4 '16 at 1:07
  • I explained that. It lets the router figure out what the directly connected network is. How else do you think the router, or a host for that matter, will know what the directly connected network is? – Ron Maupin Sep 4 '16 at 1:08
  • I was under the impression that a router doesn't have any info about how I configure my interfaces on my machine. My router sets his own routing table, which routes everything in 192.168.0.0/16 (for example) to the local network. It doesn't know anything about the machines connected to it. Isn't that right? – Gradient Sep 4 '16 at 1:15
  • As I explained, you enter an IP address and mask on a router so that it can figure out the directly attached network, and it enters that in its routing table. A host needs to figure out its directly attached network so that it knows if it needs to send traffic directly to the destination, or if it needs to send the traffic to its configured gateway. The router doesn't know anything about the hosts at layer-3. – Ron Maupin Sep 4 '16 at 1:18
  • I might have been unclear in my question. My personal machine (the host) has an interface as well as a routing table. I do understand that its routing table need to be configured correctly (with subnet masks) to route packets as expected. What I don't get is why the interface (which is unrelated to the routing table) needs to know the subnet mask. – Gradient Sep 4 '16 at 1:25
7

Super simple answer...

It tells the computer what range of addresses it can talk to before sending the traffic to the gateway to decide where it goes next.

The traffic in that range never goes to the gateway. Only traffic not in that range because the computer can't "talk" to anyone but the gateway outside its subnet range.

Little more details...

If your computer is 192.168.1.10 255.255.255.0 it can talk to 192.168.1.1 - 192.168.1.255 without use of the gateway.

If the subnet was 255.255.0.0 the computer could talk to 192.168.1.1 - 192.168.255.255 without a gateway.

Fyi... gateway is a router or layer 3 device like a switch or firewall that will also have a range it can talk to before sending traffic to its own gateway or 'next hop'

I hope the simple nature of this helps.

2

While all three answers are correct, they don't answer the actual question, which is

Why is it [the netmask] specified on the interface if it is already specified in the routing table?

When specifying the subnet mask on the interface, e.g. through ifconfig on linux, an entry is added to the routing table, which states that all hosts which have the same network prefix as this host are reachable without going through a gateway.

However, one could specify a wrong netmask on the interface (just for fun), and afterwards correct the entry in the routing table by hand.

Example:

  • set up IP 10.0.0.1 and netmask 255.255.255.0 with ifconfig
  • delete the route that is created
  • set up a new route, for network 10.0.0.0 with netmask 255.0.0.0
  • lo and behold, my host can now reach the whole 10.0.0.0/8 network on the same link, although I have a /24 IP

While I don't want to say that this approach makes a lot of sense, it clearly shows that the network mask on the interface is rather useless, apart from automatically setting up the on-link routing table entry.

In case there is any information I missed, I would be eager to hear about it!

  • 1
    I'm not sure your purpose of a Linux lesson when host/server configurations are off-topic here. Could you frame your answer in the context of a business-grade router that is on-topic. – Ron Maupin May 16 '18 at 14:40
  • No I can't, as I don't own a business-grade router to test it. – or1on May 17 '18 at 14:56
0

When you're using that interface to connect to another IP address, the subnet mask for an interface defines what addresses are "on this LAN" (or local) vs. what addresses are "out there", via a router.

By way of example, if you're on a computer with an interface with an IP address of 100.100.99.5 , with a subnet mask of "/24" (or 255.255.255.0) , that mask says that "to find hosts with 100.100.99.0-255, ask the ARP table, because they're on my lan" ... for all other addresses (so, outside of the mask), you should send the packets to the host designated in my routing table (the router), or the default route.

0

If a host sends traffic to say 8.8.8.8 (google.com) it must know if 8.8.8.8 is in the same LAN or if it is outside the LAN.

If your computer has a netmask: 255.255.255.0 and IP addr 192.168.0.50, then it knows that the IPs 192.168.0.0 to 192.168.0.255 are in its same network, and thus won't need a gateway to reach an other network.

This is very important. For example: if you want to PING some host on your network (e.g. 192.168.0.100), your computer will first send an ARP request like this one: Who has 192.168.0.100 ? and it will wait for the ARP reply containing the MAC address of 192.168.0.100. After that it will send a PING to 192.168.0.100 with the MAC address of 192.168.0.100 at the link layer.

On the other hand if you want to PING and address outside your LAN (e.g. 8.8.8.8) your computer will not make an ARP request for 8.8.8.8 MAC address but will instead send a PING to 8.8.8.8 with the MAC address of default gateway at the link layer (e.g. 192.168.0.1)

0

I strongly concur with or1on's answer here. I forgot if I have done the same experiment. It looks to me that what subnet mask value the interface has does not matter at all. The kernel only consults with the route table to determine if another IP is reachable via gateway, on-link or neither.

Maybe the reason that you set IP and net mask at the same time on the interface is just a way for you to set up route entry. Afterwards, only routing table matters.

I will come back with more answers.

By the way, I think the routing table in Unix is the equivalent of forwarding table in the context of switches. How interesting!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.