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 https://www.netfilter.org/documentation/HOWTO/packet-filtering-HOWTO-10.html lists most of them (there's a lot of other good iptables help at https://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 https://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.
Got something to add? Send me email.
More Articles by Tony Lawrence © 2012-11-29 Tony Lawrence
I had a life once. Now I have a computer. (unknown)
I'd like this to be a little more hopeful about getting to learn this!
Tue Aug 30 20:48:08 2011: 9753 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: 9759 BigDumbDInosaur
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: 10871 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: 10872 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: 10873 BigDumbDinosaur
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: 10896 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!
Thu Nov 29 14:13:14 2012: 11445 Michael
Tony, thank you for this article. I completely sympathize with your thoughts on iptables. BigDumbDinosaur, thanks for sharing your assessment of the iptables documentation. Folks, this article and the comments have been a good read! I'm not a network admin but I am a programmer and a professional computer user, and I am used to reading computer docs a lot, and man! do the iptables docs suck! I thought it was me being stupid all the way not even understanding my router's default configuration … It's not like there's no docs on the web, there are many, but mostly it's simply howtos of the basics that do not really explain the rationale of why iptables is like it is and how you're supposed to use it. Why are chains even organized in tables when the way packets pass through chains jumps willy-nilly from one table to another? It probably somehow all makes sense … especially in the minds of the authors.
Why were tables built into ipchains?
(link)
Enjoy the confusing output of just `iptables -L` without also `-v`:
iptables beginner question about ACCEPT
(link)
And as for complex sets of chains and rules:
Have someone ever audited the default OpenWrt firewall rules?
(link)
How to make sense of an iptables chain configuration [this is my own question]
(link)
Thanks for this post, Tony!
Thu Nov 29 15:32:38 2012: 11447 TonyLawrence
Thank you for the links, Michael!
Thu Nov 29 18:50:57 2012: 11448 BigDumbDinosaur
Why were tables built into ipchains?
(link)
Enjoy the confusing output of just `iptables -L` without also `-v`:
iptables beginner question about ACCEPT
(link)
And as for complex sets of chains and rules:
Have someone ever audited the default OpenWrt firewall rules?
(link)
How to make sense of an iptables chain configuration [this is my own question]
(link)
Read more: (link)
All of this illustrates several things. One is the general documentation of iptables is incredibly weak (something I've noted before), which given that it is a critical part of the Linux environment, is inexcusable.
Secondly, I'm of the opinion that the design premise of iptables (actually, the kernel code that implements what is controlled with the iptables command) is fundamentally flawed. In this case, SCO actually had a better way of doing it in OpenServer. OpenServer separates NAT from packet filtration, which to me is logical, given that NAT is really an entirely different process than that of packet filtration. OS's NAT is/was quite easy to configure and was bullet-proof. The best part was that you could readily change NAT rules and apply them without disturbing filtration, and vice versa.
In a sense, iptables violates a core part of the UNIX philosophy, which is to have each program do one thing very well and only do that one thing.
------------------------
Printer Friendly Version
Understanding IPTABLES Copyright © November 2002 Tony Lawrence
Have you tried Searching this site?
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.
Contact us
Printer Friendly Version