LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 12-06-2007, 12:05 PM   #1
Madone_SL_5.5
Member
 
Registered: Oct 2006
Location: Ogden, Utah
Distribution: Fedora 10
Posts: 66

Rep: Reputation: 15
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.
 
Old 12-06-2007, 03:42 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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
 
Old 12-06-2007, 09:03 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 79
Quote:
Originally Posted by acid_kewpie View Post
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.

Last edited by osor; 12-07-2007 at 11:25 AM. Reason: typo
 
Old 12-06-2007, 10:35 PM   #4
farslayer
LQ Guru
 
Registered: Oct 2005
Location: Northeast Ohio
Distribution: linuxdebian
Posts: 7,249
Blog Entries: 5

Rep: Reputation: 191Reputation: 191
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
 
Old 12-07-2007, 03:11 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by osor View Post
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
 
Old 12-07-2007, 11:26 AM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 79
Quote:
Originally Posted by acid_kewpie View Post
really did think it was just 137.0.0.0
I figured that’s what you were thinking.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Understanding iptables veeruk101 Linux - Networking 4 07-25-2007 03:58 PM
pointer notation vs array notation? pablowablo Programming 5 03-14-2005 01:34 PM
help me understanding iptables logs ddaas Linux - Security 1 02-23-2005 10:08 AM
understanding iptables gr00ve Linux - Networking 1 11-01-2004 01:47 PM
Understanding iptables Buckyjunior Linux - Networking 3 03-07-2004 07:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration