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.
- 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!