Iptables Tutorial 1.2.2
Total Page:16
File Type:pdf, Size:1020Kb
Iptables Tutorial 1.2.2 Oskar Andreasson [email protected] Iptables Tutorial 1.2.2 by Oskar Andreasson Copyright © 2001-2006 Oskar Andreasson Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1; with the Invariant Sections being "Introduction" and all sub-sections, with the Front-Cover Texts being "Original Author: Oskar Andreasson", and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". All scripts in this tutorial are covered by the GNU General Public License. The scripts are free source; you can redistribute them and/or modify them under the terms of the GNU General Public License as published by the Free Software Foundation, version 2 of the License. These scripts are distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License within this tutorial, under the section entitled "GNU General Public License"; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Dedications I would like to dedicate this document to my wonderful sister, niece and brother-in-law for giving me inspiration and feedback. They are a source of joy and a ray of light when I have need of it. Thank you! A special word should also be extended to Ninel for always encouraging my writing and for taking care of me when I needed it the most. Thank you! Second of all, I would like to dedicate this work to all of the incredibly hard working Linux developers and maintainers. It is people like those who make this wonderful operating system possible. 3 Table of Contents About the author............................................................................... xiv How to read ....................................................................................... xvi Prerequisites................................................................................... xviii Conventions used in this document............................................... xix 1. Introduction...................................................................................... 1 Why this document was written .................................................... 1 How it was written ......................................................................... 1 Terms used in this document ........................................................ 2 What’s next? ................................................................................. 4 2. TCP/IP repetition.............................................................................. 5 TCP/IP Layers............................................................................... 5 IP characteristics .......................................................................... 9 IP headers .................................................................................. 12 TCP characteristics..................................................................... 17 TCP headers............................................................................... 18 UDP characteristics .................................................................... 21 UDP headers .............................................................................. 22 ICMP characteristics................................................................... 23 ICMP headers............................................................................. 24 ICMP Echo Request/Reply ................................................ 25 ICMP Destination Unreachable .......................................... 26 Source Quench .................................................................. 28 Redirect.............................................................................. 29 TTL equals 0 ...................................................................... 30 Parameter problem............................................................. 31 Timestamp request/reply.................................................... 31 Information request/reply ................................................... 32 SCTP Characteristics ................................................................. 33 Initialization and association .............................................. 35 Data sending and control session ...................................... 35 Shutdown and abort ........................................................... 35 SCTP Headers............................................................................ 36 SCTP Generic header format............................................. 36 iv SCTP Common and generic headers ................................ 37 SCTP ABORT chunk.......................................................... 40 SCTP COOKIE ACK chunk................................................ 41 SCTP COOKIE ECHO chunk............................................. 41 SCTP DATA chunk ............................................................. 42 SCTP ERROR chunk ......................................................... 44 SCTP HEARTBEAT chunk ................................................. 45 SCTP HEARTBEAT ACK chunk......................................... 46 SCTP INIT chunk ............................................................... 46 SCTP INIT ACK chunk....................................................... 50 SCTP SACK chunk ............................................................ 52 SCTP SHUTDOWN chunk................................................. 55 SCTP SHUTDOWN ACK chunk......................................... 56 SCTP SHUTDOWN COMPLETE chunk ............................ 56 TCP/IP destination driven routing ............................................... 57 What’s next? ............................................................................... 58 3. IP filtering introduction................................................................. 59 What is an IP filter....................................................................... 59 IP filtering terms and expressions............................................... 61 HowtoplananIPfilter................................................................ 64 What’s next? ............................................................................... 68 4. Network Address Translation Introduction................................. 69 What NAT is used for and basic terms and expressions............. 69 Caveats using NAT ..................................................................... 71 Example NAT machine in theory................................................. 72 What is needed to build a NAT machine ............................ 72 Placement of NAT machines .............................................. 74 How to place proxies .......................................................... 74 The final stage of our NAT machine ................................... 75 What’s next? ............................................................................... 77 5. Preparations................................................................................... 79 Where to get iptables .................................................................. 79 Kernel setup................................................................................ 79 User-land setup .......................................................................... 84 Compiling the user-land applications ................................. 85 Installation on Red Hat 7.1................................................. 87 v What’s next? ............................................................................... 90 6. Traversing of tables and chains ................................................... 92 General ....................................................................................... 92 Mangle table ............................................................................... 99 Nat table.................................................................................... 101 Raw table .................................................................................. 102 Filter table ................................................................................. 102 User specified chains................................................................ 103 What’s next? ............................................................................. 105 7. The state machine ....................................................................... 106 Introduction ............................................................................... 106 The conntrack entries ............................................................... 107 User-land states........................................................................ 109 TCP connections ...................................................................... 112 UDP connections ...................................................................... 117 ICMP connections..................................................................... 119 Default connections .................................................................. 123 Untracked connections and the raw table ................................. 124 Complex protocols and connection tracking ............................. 125 What’s next? ............................................................................