What does a mask on an interface address actually mean?

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

What does a mask on an interface address actually mean?

Post by mariuszp »

I am very confused as to why an interface IP address needs a mask. For example, my IP address right now (internal of course) is 192.168.0.6/24. All devices on my network get addresses from the block 192.168.0.0/24.

But exactly why does my network configuration have to remember the network from which 192.168.0.6 was obtained?? I mean, it clearly only listens to packets from 192.168.0.6, not from 192.168.0.4 for example. So why does interface configuration include that mask?

I understand that the system needs to know the route 192.168.0.0/24, but I do not understand why it needs to know the mask of the network that it got its IP address from.

I hope my question is clear. Thank you.
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: What does a mask on an interface address actually mean?

Post by Nable »

Mask is used while sending packets to determine local subnet, i.e. range of addresses that can be reached directly because they are in the same subnet as you. Those who don't fit in the mask can only be reached via gateway.
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: What does a mask on an interface address actually mean?

Post by mariuszp »

So that mask is only used to determine routes?

My OS should still ignore packets directed to other address form that mask, right?
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

Re: What does a mask on an interface address actually mean?

Post by Kazinsal »

You should be ignoring packets that aren't directed to IPs you're not attached to and that aren't relevant broadcast and multicast addresses. If you're running an OSPF router for example you'd be listening on your interface's IPs, their broadcasts, 224.0.0.5 (OSPF all routers), and possibly 224.0.0.6 (OSPF designated routers).

The mask determines the network address, the number of usable host addresses, and the broadcast address.
jbemmel
Member
Member
Posts: 53
Joined: Fri May 11, 2012 11:54 am

Re: What does a mask on an interface address actually mean?

Post by jbemmel »

Logic when forwarding a packet with destination IP "x":

if ( "x" in locally attached subnet ) {
destIP = x
} else {
destIP = route_lookup( x )
}
destMAC = lookup dest MAC for destIP ( possibly send ARP )
forward packet to destMAC

"x" in locally attached subnet: for each interface with IP i and mask m, check (x&m) == (i&m ). If equal, return true
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: What does a mask on an interface address actually mean?

Post by SpyderTL »

Simply put, when sending IP packets, you can compare your IP address with the destination IP address (after ANDing the mask to both addresses) to see whether you can send packets directly to the destination Ethernet address, or whether you need to send it to the Gateway/Router's Ethernet address instead.

IP addresses by themselves don't give you enough information to get the packet to the destination. You have to know which Ethernet address to send the packet to, as well.

At least, this is my understanding.

In reality, you may be able to send ALL IP packets to the Gateway/Router, regardless of whether the destination is on the same network or not. However, I'm not entirely sure if this a) will work in all cases, b) is terribly inefficient, and/or c) is frowned upon. Feel free to chime in if you happen to know the answer...
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
User avatar
Kazinsal
Member
Member
Posts: 559
Joined: Wed Jul 13, 2011 7:38 pm
Libera.chat IRC: Kazinsal
Location: Vancouver
Contact:

Re: What does a mask on an interface address actually mean?

Post by Kazinsal »

Any competent router will route within a subnet. Sending everything to a gateway regardless of whether or not the destination is in your subnet is however bad practice and inefficient. If you've got two computers and a router plugged into a switch, all in 192.168.0.0/24, you can just send packets between the computers by sending ethernet frames addressed to the other computer's MAC. That's what ARP caching is for. Layer 2 forwarding is incredibly quick -- frame enters switch, switch does a bit of checking in an ASIC, switch sends frame out correct port if it knows the port the MAC is assigned to (or floods it out all ports if it doesn't).

You should always maintain a cache of IP address to MAC address maps built using ARP. That way you don't clog up your network sending out layer 2 broadcasts when addressing systems on your layer 3 subnet.
User avatar
iansjack
Member
Member
Posts: 4685
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: What does a mask on an interface address actually mean?

Post by iansjack »

You should also note that a local area network network doesn't necessarily include a gateway.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: What does a mask on an interface address actually mean?

Post by Combuster »

...or includes multiple for that matter, which is even more troublesome to try and autoconfigure.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply