Search code examples
asp.net-mvcip-addresssubnetwhitelist

IP Address range with subnetmask


I'm quite new to how subnetmask is calculated.

We have a requirement to whitelist the range of IP addresses on our asp.net website.

199.83.128.1 - 199.83.135.254
198.143.32.1 - 198.143.63.254
149.126.72.1 - 149.126.79.254
103.28.248.1 - 103.28.251.254
185.11.124.1 - 185.11.127.254
45.64.64.0 - 45.64.67.255
192.230.64.1 - 192.230.127.254

ASP.NET IPSecurity has an option to block IP Address range, but with subnet mask. e.g.

    <add ipAddress="xx.xx.xx.xx" subnetmask="x.x.x.x" allowed="true"/>
  </ipSecurity>

Could someone please help me how to come up address ranges list above using subnetmask?


Solution

  • Forget about the .1 and .254s, they should be .0 and .255 for simplicity. I can't think that an organization controls a block of 1024 IPs except for the very first and very last one.

    The canonical answer is https://serverfault.com/questions/49765/how-does-ipv4-subnetting-work/226445 but I suppose it takes several hours to digest. Here's a "no understanding beyond basic math" version.

    If you have a.b.x.0 - a.b.y.255 (with x ≤ y), then

    • the number of /24s is n = y+1-x
    • this is one subnet if n is a power of 2 (1, 2, 4, 8, 16, 32, 64, 128, 256) and x is a multiple of n (x = 0*n = 0 is OK), otherwise you have to split it up
    • the 3rd-octet mask is then m = 256-n
    • the resulting netmask is 255.255.m.0

    Your result is (do feel free to check my math, it's your security not mine):

    45.64.64.0   / 255.255.252.0
    103.28.248.0 / 255.255.252.0
    149.126.72.0 / 255.255.248.0
    185.11.124.0 / 255.255.252.0
    192.230.64.0 / 255.255.192.0
    198.143.32.0 / 255.255.224.0
    199.83.128.0 / 255.255.248.0
    

    The math is the same for a.b.c.x - a.b.c.y, a.x.0.0 - a.y.255.255, and x.0.0.0 - y.0.0.0, except that we're not talking about /24s but IPs, /16s, and /8s, and that the netmasks become 255.255.255.m, 255.m.0.0, and m.0.0.0 respectively.

    Hope this helped.