Packet filtering is something I've always hard a hard time getting my head around. Not the basics; that's easy enough. It's just the incredible level of detail, the difficulty of keeping it all in your head at once.
And then, of course, there are all the different flavors: ipfw, ipfilters, ipchains, and now iptables. It gets more than a little confusing, and I've never taken the time for more than a cursory look at any of them.
Well, time to change that. I needed to learn more about iptables because the SME Server firewall/mail server I used to sell uses this. So..
The basic idea of any packet filtering is to look at a network packet and decide what to do with it: accept it as is and let it go on its way, stop it dead, or change it in some way (which usually involves sending it somewhere other than where it was originally headed).
Iptables starts with three built in chains. You can add more chains, (generally for convenience). Let's understand what it comes with first.
It is important to first understand what packets these chains see.
If a packet comes from this machine (is generated by an application running on this machine), it will go to the OUTPUT chain only.
A packet coming TO this machine traverses the INPUT chain only.
A packet going somewhere else uses FORWARD only.
THAT'S NOT HOW IPCHAINS WORKS. A packet going somewhere else never sees INPUT with iptables. Similarly, a forwarded packet never sees the OUTPUT chain with iptables. In some ways this makes iptables easier to understand, but if you have the ipchains flow stuck in your head, it makes it confusing.
Another major difference is that iptables is stateful; that is, it keeps track of each connection. You can look at connections by examining /proc/net/ip_connact. Here's a little bit from a machine:
Note that you can see an ssh and an ftp connection there.
You need the ip_connact module to have iptables understand the relationship between the control and data sides of an ftp connection. If that makes no sense right now, you might want to read the ftp section in /Security/dslsecure.html. This module is also used by the nat translation module.
There are other differences. The http://www.netfilter.org/documentation/HOWTO/packet-filtering-HOWTO-10.html lists most of them (there's a lot of other good iptables help at http://www.netfilter.org/documentation/ too).
But back to chains: why would you want to add your own chains - it looks like the standard three pretty well cover everything? True, but you'd usually do that so that you can apply the rules you make for the new chain to other chains. A quick example:
You create a chain called "mychain" and add a bunch of rules to it. You want both the INPUT and the FORWARD chains to use those rules.
iptables -N mychain
iptables -A mychain -m state --state ESTABLISHED,RELATED -j ACCEPT
# .. more rules ..
iptables -A mychain -j DROP
iptables -A INPUT -j mychain
iptables -A FORWARD -j mychain
That saves the effort of writing out the same rules for both INPUT and FORWARD chains. It's also, unfortunately, why professionally written iptables firewalls are so hard to comprehend: you have to follow them back through chain after chain to figure out what's really going on. One chain will list several other chains as targets for its various rules, and those in turn may list others - it can be hard to follow.
Now for some more confusion. You can have more than one network card on the machine. That's the whole idea of a firewall: one interface to the internet, one or more to the internal lan. A packet coming in on the external interface may be a FORWARD to it, and an INPUT to the lan side. Therefore, you may have to write more than one rule to control the packet.
Tables make this even more confusing. This is straight from the manual page:
TABLES
There are current three independent tables (which tables
are present at any time depends on the kernel configura�
tion options and which modules are present).
-t, --table
This option specifies the packet matching table
which the command should operate on. If the kernel
is configured with automatic module loading, an
attempt will be made to load the appropriate module
for that table if it is not already there.
The tables are as follows:
filter This is the default table. It contains the built-
in chains INPUT (for packets coming into the box
itself), FORWARD (for packets being routed through
the box), and OUTPUT (for locally-generated pack�
ets).
nat This table is consulted when a packet that creates
a new connection is encountered. It consists of
three built-ins: PREROUTING (for altering packets
as soon as they come in), OUTPUT (for altering
locally-generated packets before routing), and
POSTROUTING (for altering packets as they are about
to go out).
mangle This table is used for specialized packet alter�
ation. It has two built-in chains: PREROUTING (for
altering incoming packets before routing) and OUT�
PUT (for altering locally-generated packets before
routing).
Does you head hurt yet? Mine sure did. It gets worse: while those three are probably all you have at most, you could have more. You can find out by "cat /proc/net/ip_tables_names".
This also introduces another complication: if you want to list the rules for the chains, you also need to specify the table. If you just do "iptables -L -n" (don't forget the -n to avoid wasting time asking DNS to resolve your internal addresses), you only get the filter table. To get them all, do something like:
for i in `cat /proc/net/ip_tables_names` do echo "Table $i:" echo "============" iptables -L -n -t $i done
Got that all digested? Good, because now we have to learn about extensions. Look in /lib/iptables or /usr/lib/iptables.
You should find a bunch of libraries, here are just a few:
libipt_ah.so libipt_DNAT.so libipt_DSCP.so ... libipt_state.so libipt_tcp.so libipt_tos.so ...
Each of these are things you can use in iptables rules. We used the "state" module in the user defined chains example above. That's great, but how do you use these things? Well, some of them are documented in the "man iptables" page, but they are also self documenting. Try these:
iptables -p tcp --help iptables -m state --help iptables -j LOG --help
How do you know whether to use -p, -j or -m? Honestly, it can be a little confusing, but if one doesn't work, try another- you'll find it by trial and error if no other way. When you are reading someone else's rules, you may need at least this to understand what their rule is trying to do. You may also find http://www.netfilter.org/documentation/HOWTO/netfilter-extensions-HOWTO.html helpful.
There is no way that I'm even going to attempt to write firewall rules. I will, when necessary, add to or modify someone else's rules to do something needful that they didn't include. The level of knowledge necessary for that is substantially less than that required for actual authoring. Even that can be daunting, however: these things can be very complicated.
There are iptables firewall generators available on the net. Use Google and search for "iptables firewall". Some of these are pretty well documented, so you can learn quite a bit more about iptables by studying them.
More Articles by Tony Lawrence - Find me on Google+
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
I'd like this to be a little more hopeful about getting to learn this!
Tue Aug 30 20:48:08 2011: Paul
The layout of a simple self-created table is relatively easy for a notice to understand. Your explanation dissuades people from attempting to understand the wonders of *basic* IP tables.
Wed Aug 31 14:37:18 2011: BigDumbDInosaur
http://bcstechnology.net
It is important to first understand what packets these chains see.
If a packet comes from this machine (is generated by an application running on this machine), it will go to the OUTPUT chain only.
A packet coming TO this machine traverses the INPUT chain only.
A packet going somewhere else uses FORWARD only.
That last item might be better phrased:
A packet that was not created by this machine nor intended for this machine uses FORWARD only. Such would be the case whene this machine is acting as a router for a local network.
Thu Apr 19 02:52:10 2012: Jim
You really did nothing to explain how to use iptables. All you did was offer a brief explanation of some very basic aspects, and then act like it was rocket science to do anything with it. You should re-title your article, "I get frustrated by IPTables, never took the time to understand how to use iptables, so I will also try to discourage everyone else from using it."
Thu Apr 19 11:19:08 2012: TonyLawrence
Then show me something better, Jim. I've read everything I can find and still find iptables confusing and messy. I tried here to give a BASIC intro - obviously some will be far beyond that, but so what? What value is your comment beyond polishing your ego?
Thu Apr 19 16:05:19 2012: BigDumbDinosaur
http://bcstechnology.net
You really did nothing to explain how to use iptables. All you did was offer a brief explanation of some very basic aspects, and then act like it was rocket science to do anything with it. You should re-title your article, "I get frustrated by IPTables, never took the time to understand how to use iptables, so I will also try to discourage everyone else from using it."
I don't recall seeing anything in Tony's article that discourages anyone from doing anything. If any discouragement is forthcoming it is due to the incredibly poor job the author(s) of iptables did in documenting how it works. Even the O'Reilly tome on the subject (LINUX iptables Pocket Reference) is barely adequate in that regard. What I know about iptables I mostly gleaned from patient experimentation. Had I been totally dependent on the available documentation to learn how to set up packet filtration on a Linux box I would still be completely at sea.
One of the glaring and continuing weaknesses of Linux is the poor quality documentation that exists in all distributions. I know from experience that documenting ones work isn't nearly as much fun as writing the code. However, if users are expected to adopt Linux they should be able to understand it without having to wade through inadequate explanations, such as embodied in the iptables man page.
Tony's article makes the not-so-subtle point that while iptables itself is fine, its documentation is not. I completely agree with him in that regard.
Thu Apr 26 19:25:30 2012: Perk
It is easy for people to come to a forum and complain when they still dont get the basic laymens terms explanation. This aint kindergarten, the man should not be teaching you how to count and get hooked on phonics. Go get another job and then take your complaining with you. Great job getting this discussion started! Keep speaking intelligently!
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar