LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Understanding CIDR notation for iptables (https://www.linuxquestions.org/questions/linux-networking-3/understanding-cidr-notation-for-iptables-604966/)

Madone_SL_5.5 12-06-2007 11:05 AM

Understanding CIDR notation for iptables
 
I live in university dorms on a campus and connect through the university network to the internet. My IP address is assigned by DHCP.

My personal web server is located at a remote location, and I use Putty SSH to connect to it frequently. What I'd really like is to make a rule in iptables that allows ssh connection attempts only from my computer; however, since my IP address changes often, I will just make a rule that allows attempts from any possible campus address. That at least limits potential bogus connection attempts to several thousand people instead of several billion.

Now, I am a bit confused about how to do this. From what I can tell, my IP address always begins with 137 or 138. I'd have to pay closer attention for a while to see which other numbers remain fixed in my address. So for now I have settled with the range 137.0.0.1 to 138.255.255.255. That probably covers a lot more than just my campus here, but it's okay for now.

I have used an entry such as:

Quote:

iptables -A INPUT -p tcp -d server.mydomain.com --sport 22 -m iprange --src-range 137.0.0.1-138.255.255.255 -j ACCEPT
which results in the following rule:

Quote:

ACCEPT tcp -- anywhere server.mydomain.com tcp spt:ssh source IP range 137.0.0.1-138.255.255.255
I'd like for the rule to read:

Quote:

ACCEPT tcp -- xxx.xxx.xxx.xxx/xx server.mydomain.com tcp spt:ssh
which would require an entry such as:

Quote:

iptables -A INPUT -p tcp -s xxx.xxx.xxx.xxx/xx -d server.mydomain.com --sport 22 -j ACCEPT
I believe that what I'm referring to is CIDR notation. Is that right?

So I have two questions: Would there be any difference in the performance of the latter rule over the former? What would be the correct CIDR notation for that IP range?

I'd even be willing to learn how to do it myself (heaven forbid) so that I can modify it by myself in the future, but I have found several online tutorials to be a bit vague.

acid_kewpie 12-06-2007 02:42 PM

i wouldn't use iptables myself, but tcpwrappers: http://closedsrc.org/_static/dn-arti...sts_allow.html

much simpler. you'd do well to properly utilize the other parts of ssh though... use a private key instead of a password to authentiate, then it doesn't matter who connects from wherever, if they don't haev your key, they'll never get in...

also as a note in your iptables code, it's the destination port that is 22, not the source port. and the correct CIDR would be /8: 12345678.xxxxxxxx.xxxxxxxx.xxxxxxxx

osor 12-06-2007 08:03 PM

Quote:

Originally Posted by acid_kewpie (Post 2982460)
and the correct CIDR would be /8: 12345678.xxxxxxxx.xxxxxxxx.xxxxxxxx

No, in fact there is no “correct CIDR” notation for the range you supplied (137.0.0.1-138.255.255.255). In CIDR notation, the number following the slash is the number of bits which belong to the “network part” of the address. If you say 137.0.0.0/8, you it means that the first 8 bits are set in the netmask (i.e., it is 255.0.0.0). So to determine the notation to describe a network with a given number of bits and a representative IP address, you just mask it (using a bitwise AND of the IP with the netmask).

For example, suppose I want to know the correct notation for the network of 137.123.123.123/8. Then I would do a bitwise and like so:
Code:

137.123.123.123 = 10001001.01111011.01111011.01111011
&255.000.000.000 = 11111111.00000000.00000000.00000000
 137.000.000.000 = 10001001.00000000.00000000.00000000

Suppose, on the other hand, that you are given a network such as 137.0.0.0/8 and want to figure out the corresponding range. Well you must keep the first 8 bits constant, and can vary the other 24 bits (all numbers like 10001001.xxxxxxxx.xxxxxxxx.xxxxxxxx). Effectively this means that you must keep the same first number, but can change the other three numbers (i.e., all numbers from 137.0.0.0 to 137.255.255.255). Well, this is too small a range for us. What about the next-largest network (one with the first 7 bits set)? Let’s talk about 137.0.0.0/7 (or more correctly 136.0.0.0/7). This means that the first 7 bits will stay constant and the other 25 bits are changable (all numbers like 1000100x.xxxxxxxx.xxxxxxxx.xxxxxxxx). This translates to IPs where the first number is 136 or 137 and the rest of the numbers vary. This is still not the correct range, but it is the correct size. If you want a network containing the range you specified, you’ll have to go one size bigger (to 6 bits). Now, you have any number like 100010xx.xxxxxxxx.xxxxxxxx.xxxxxxxx (and the CIDR notation is 136.0.0.0/6). Unfortunately, the range is now twice the size that you intended, but it does contain all of the desired range. So the range corresponding to 136.0.0.0/6 is 136.0.0.0-139.255.255.255.

As for performance, it is obviously easier for a computer to match a range in CIDR notation than an arbitrary range. However, when your CIDR notation range is unnecessarily twice as large, it is a trade off.

farslayer 12-06-2007 09:35 PM

ipcalc can be your friend while you are learning subnetting and CIDR notation..

Code:

debianetch:~$ ipcalc 137.0.0.1/8
Address:  137.0.0.1            10001001. 00000000.00000000.00000001
Netmask:  255.0.0.0 = 8        11111111. 00000000.00000000.00000000
Wildcard:  0.255.255.255        00000000. 11111111.11111111.11111111
=>
Network:  137.0.0.0/8          10001001. 00000000.00000000.00000000
HostMin:  137.0.0.1            10001001. 00000000.00000000.00000001
HostMax:  137.255.255.254      10001001. 11111111.11111111.11111110
Broadcast: 137.255.255.255      10001001. 11111111.11111111.11111111
Hosts/Net: 16777214              Class B



debianetch:~$ ipcalc 137.0.0.1/7
Address:  137.0.0.1            1000100 1.00000000.00000000.00000001
Netmask:  254.0.0.0 = 7        1111111 0.00000000.00000000.00000000
Wildcard:  1.255.255.255        0000000 1.11111111.11111111.11111111
=>
Network:  136.0.0.0/7          1000100 0.00000000.00000000.00000000
HostMin:  136.0.0.1            1000100 0.00000000.00000000.00000001
HostMax:  137.255.255.254      1000100 1.11111111.11111111.11111110
Broadcast: 137.255.255.255      1000100 1.11111111.11111111.11111111
Hosts/Net: 33554430              Class B


acid_kewpie 12-07-2007 02:11 AM

Quote:

Originally Posted by osor (Post 2982715)
No, in fact there is no “correct CIDR” notation for the range you supplied (137.0.0.1-138.255.255.255).

ok, how much to pretend i didn't misread it? really did think it was just 137.0.0.0 :)

osor 12-07-2007 10:26 AM

Quote:

Originally Posted by acid_kewpie (Post 2982928)
really did think it was just 137.0.0.0 :)

I figured that’s what you were thinking.


All times are GMT -5. The time now is 07:22 AM.