home *** CD-ROM | disk | FTP | other *** search
/ Hackers Toolkit v2.0 / Hackers_Toolkit_v2.0.iso / HTML / archive / Unix / HOWTOs / IPCHAINS < prev    next >
Text File  |  1999-11-04  |  74KB  |  2,311 lines

  1.   Linux IPCHAINS-HOWTO
  2.   Paul Russell Paul.Russell@rustcorp.com.au
  3.   v0.8, 31 July 1998
  4.  
  5.   This document aims to describe how to obtain, install and configure
  6.   the enhanced IP firewalling chains software for Linux, and some ideas
  7.   on how you might use them.
  8.   ______________________________________________________________________
  9.  
  10.   Table of Contents
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.   1. Introduction
  68.  
  69.      1.1 What?
  70.      1.2 Why?
  71.      1.3 How?
  72.      1.4 Where?
  73.  
  74.   2. Packet filtering basics.
  75.  
  76.      2.1 What?
  77.      2.2 Why?
  78.      2.3 How?
  79.         2.3.1 A kernel with packet filtering.
  80.         2.3.2 (TT
  81.  
  82.   3. IP firewalling chains.
  83.  
  84.      3.1 How packets traverse the filters.
  85.         3.1.1 Using
  86.         3.1.2 Operations on a single rule.
  87.         3.1.3 Filtering specifications.
  88.            3.1.3.1 Specifying source and destination IP addresses.
  89.            3.1.3.2 Specifying inversion.
  90.            3.1.3.3 Specifying protocol.
  91.               3.1.3.3.1 Specifying UDP and TCP ports.
  92.               3.1.3.3.2 Specifying ICMP type & code.
  93.            3.1.3.4 Specifying an interface.
  94.            3.1.3.5 Specifying TCP SYN packets only.
  95.            3.1.3.6 Handling fragments.
  96.         3.1.4 Filtering side effects.
  97.            3.1.4.1 Specifying a target.
  98.            3.1.4.2 Logging packets.
  99.            3.1.4.3 Manipulating the Type Of Service
  100.            3.1.4.4 Marking a packet.
  101.            3.1.4.5 Operations on an entire chain.
  102.            3.1.4.6 Creating a new chain.
  103.            3.1.4.7 Deleting a chain.
  104.            3.1.4.8 Flushing a chain.
  105.            3.1.4.9 Listing a chain.
  106.            3.1.4.10 Resetting (zeroing) counters.
  107.            3.1.4.11 Setting policy.
  108.         3.1.5 Operations on masquerading.
  109.         3.1.6 Checking a packet.
  110.         3.1.7 Multiple rules at once and watching what happens.
  111.      3.2 Useful Examples
  112.         3.2.1 Using
  113.         3.2.2 Using
  114.  
  115.   4. Miscellaneous.
  116.  
  117.      4.1 How to organise your firewall rules.
  118.      4.2 What
  119.         4.2.1 ICMP packets.
  120.         4.2.2 DNS replies.
  121.         4.2.3 FTP nightmares.
  122.      4.3 Filtering out Ping of Death.
  123.      4.4 Filtering out Teardrop and Bonk.
  124.      4.5 Filtering out Fragment Bombs.
  125.      4.6 Changing firewall rules.
  126.      4.7 How do I set up IP spoof protection?
  127.      4.8 Advanced projects.
  128.      4.9 Future enhancements.
  129.  
  130.   5. Common problems.
  131.  
  132.      5.1 Masquerading/forwarding doesn't work!
  133.      5.2 Wildcard interfaces don't work!
  134.      5.3 TOS doesn't work!
  135.      5.4 (TT
  136.      5.5 xosview is broken!
  137.      5.6 Segmentation fault with
  138.      5.7 I can't set masquerading timeouts!
  139.      5.8 I want to firewall IPX!
  140.  
  141.   6. Appendix: Differences between
  142.  
  143.      6.1 Quick-Reference table.
  144.      6.2 Examples of translated ipfwadm commands
  145.  
  146.   7. Appendix: Using the
  147.  
  148.   8. Appendix: thanks.
  149.  
  150.  
  151.  
  152.   ______________________________________________________________________
  153.  
  154.   1.  Introduction
  155.  
  156.   This is the Linux IPCHAINS-HOWTO.  You should read the Linux
  157.   NET-3-HOWTO as well.  The IP-Masquerading HOWTO, the PPP-HOWTO and the
  158.   Ethernet-HOWTO might make interesting reading.  (Then again, so might
  159.   the alt.fan.bigfoot FAQ).
  160.  
  161.  
  162.   If packet filtering is passe to you, read Section ``Why?'', Section
  163.   ``How?'', and scan through the titles in Section ``IP Firewalling
  164.   Chains''.
  165.  
  166.  
  167.   If you are converting from ipfwadm, read Section ``Introduction'',
  168.   Section ``How?'', and Appendices in section ``Differences between
  169.   ipchains and ipfwadm'' and section ``Using the `ipfwadm-wrapper'
  170.   script''.
  171.  
  172.  
  173.   1.1.  What?
  174.  
  175.   Linux ipchains is a rewrite of the Linux IPv4 firewalling code (which
  176.   was mainly stolen from BSD) and a rewrite of ipfwadm, which was a
  177.   rewrite of BSD's ipfw, I believe.  It is required to administer the IP
  178.   packet filters in Linux kernel versions 2.1.102 and above.
  179.  
  180.  
  181.   1.2.  Why?
  182.  
  183.   The older Linux firewalling code doesn't deal with fragments, has
  184.   32-bit counters (on Intel at least), doesn't allow specification of
  185.   protocols other than TCP, UDP or ICMP, can't make large changes
  186.   atomically, can't specify inverse rules, has some quirks, and can be
  187.   tough to manage (making it prone to user error).
  188.  
  189.  
  190.   1.3.  How?
  191.  
  192.   Currently the code is in the mainstream kernel from 2.1.102.  For the
  193.   2.0 kernel series, you will need to download a kernel patch from the
  194.   web page.  If your 2.0 kernel is more recent than the supplied patch,
  195.   the older patch should be OK; this part of the 2.0 kernels is fairly
  196.   stable (eg. the 2.0.34 kernel patch works just fine on the 2.0.35
  197.   kernel).
  198.  
  199.   1.4.  Where?
  200.  
  201.   The official page is The Linux IP Firewall Chains Page
  202.   <http://www.adelaide.net.au/~rustcorp/ipfwchains/ipfwchains.html>
  203.  
  204.  
  205.   There is a mailing list for bug reports, discussion, development and
  206.   usage.  Join the mailing list by sending a message containing the word
  207.   ``subscribe'' to ipchains-request at wantree.com.au.  To mail to the
  208.   list use `ipchains' instead of `ipchains-request'.
  209.  
  210.  
  211.   2.  Packet filtering basics.
  212.  
  213.   2.1.  What?
  214.  
  215.   All traffic through a network is sent in the form of packets.  For
  216.   example, downloading this package (say it's 50k long) might cause you
  217.   to receive 36 or so packets of 1460 bytes each, (to pull numbers at
  218.   random).
  219.  
  220.  
  221.   The start of each packet says where it's going, where it came from,
  222.   the type of the packet, and other administrative details.  This start
  223.   of the packet is called the header.  The rest of the packet,
  224.   containing the actual data being transmitted, is usually called the
  225.   body.
  226.  
  227.  
  228.   Some protocols, such TCP, which is used for web traffic, mail, and
  229.   remote logins, use the concept of a `connection' -- before any packets
  230.   with actual data are sent, various setup packets (with special
  231.   headers) are exchanged saying `I want to connect', `OK' and `Thanks'.
  232.   Then normal packets are exchanged.
  233.  
  234.  
  235.   A packet filter is a piece of software which looks at the header of
  236.   packets as they pass through, and decide the fate of the entire
  237.   packet.  It might decide to deny the packet (ie. discard the packet as
  238.   if it had never received it), accept the packet (ie. let the packet go
  239.   through), or reject the packet (like deny, but tell the source of the
  240.   packet that it has done so).
  241.  
  242.  
  243.   Under Linux, packet filtering is built into the kernel, and there are
  244.   a few trickier things we can do with packets, but the general
  245.   principle of looking at the headers and deciding the fate of the
  246.   packet is still there.
  247.  
  248.  
  249.   2.2.  Why?
  250.  
  251.   Control.  Security.  Watchfulness.
  252.  
  253.  
  254.  
  255.      Control:
  256.         when you are using a Linux box to connect your internal network
  257.         to another network (say, the Internet) you have an opportunity
  258.         to allow certain types of traffic, and disallow others.  For
  259.         example, the header of a packet contains the destination address
  260.         of the packet, so you can prevent packets going to a certain
  261.         part of the outside network.  As another example, I use Netscape
  262.         to access the Dilbert archives.  There are advertisements from
  263.         doubleclick.net on the page, and Netscape wastes my time by
  264.         cheerfully downloading them.  Telling the packet filter not to
  265.         allow any packets to or from the addresses owned by
  266.         doubleclick.net solves that problem (there are better ways of
  267.         doing this though).
  268.  
  269.  
  270.      Security:
  271.         when your Linux box is the only thing between the chaos of the
  272.         Internet and your nice, orderly network, it's nice to know you
  273.         can restrict what comes tromping in your door.  For example, you
  274.         might allow anything to go out from your network, but you might
  275.         be worried about the well-known `Ping of Death' coming in from
  276.         malicious outsiders.  As another example, you might not want
  277.         outsiders telnetting to your Linux box, even though all your
  278.         accounts have passwords; maybe you want (like most people) to be
  279.         an observer on the Internet, and not a server (willing or
  280.         otherwise) -- simply don't let anyone connect in, by having the
  281.         packet filter reject incoming packets used to set up
  282.         connections.
  283.  
  284.  
  285.      Watchfulness:
  286.         sometimes a badly configured machine on the local network will
  287.         decide to spew packets to the outside world.  It's nice to tell
  288.         the packet filter to let you know if anything abnormal occurs;
  289.         maybe you can do something about it, or maybe you're just
  290.         curious by nature.
  291.  
  292.  
  293.   2.3.  How?
  294.  
  295.   2.3.1.  A kernel with packet filtering.
  296.  
  297.   You need a kernel which has the new IP firewall chains in it.  You can
  298.   tell if the kernel you are running right now has this installed by
  299.   looking for the file `/proc/net/ip_fwchains'.  If it exists, you're
  300.   in.
  301.  
  302.  
  303.   If not, you need to make a kernel that has IP firewall chains.  First,
  304.   download the source to the kernel you want.  If you have a kernel
  305.   numbered 2.1.102 or higher, you won't need to patch it (it's in the
  306.   mainstream kernel now).  Otherwise, apply the patch from the web page
  307.   listed above, and set the configuration as detailed below.  If you
  308.   don't know how to do this, don't panic -- read the Kernel-HOWTO.
  309.  
  310.  
  311.  
  312.   The configuration options you will need to set for the 2.0-series
  313.   kernel are:
  314.  
  315.  
  316.   ______________________________________________________________________
  317.           CONFIG_EXPERIMENTAL=y
  318.           CONFIG_FIREWALL=y
  319.           CONFIG_IP_FIREWALL=y
  320.           CONFIG_IP_FIREWALL_CHAINS=y
  321.   ______________________________________________________________________
  322.  
  323.  
  324.  
  325.   For the 2.1 or 2.2 series kernels:
  326.  
  327.  
  328.  
  329.  
  330.  
  331.   ______________________________________________________________________
  332.           CONFIG_FIREWALL=y
  333.           CONFIG_IP_FIREWALL=y
  334.   ______________________________________________________________________
  335.  
  336.  
  337.  
  338.  
  339.   The tool ipchains talks to the kernel and tells it what packets to
  340.   filter.  Unless you are a programmer, or overly curious, this is how
  341.   you will control the packet filtering.
  342.  
  343.  
  344.   2.3.2.  ipchains .
  345.  
  346.   This tool replaces ipfwadm used for the old IP Firewall code.  The
  347.   package also contains a shell script called ipfwadm-wrapper which
  348.   allows you to do packet filtering as it was done before.  You should
  349.   not use this script unless you want a quick way of upgrading a system
  350.   which uses ipfwadm (it's slower, and doesn't check arguments, etc).
  351.   In that case, you don't need this HOWTO much either.  See Appendix
  352.   ``Differences between ipchains and ipfwadm'' and Appendix ``Using the
  353.   `ipfwadm-wrapper' script'' for more details on ipfwadm issues.
  354.  
  355.  
  356.   3.  IP firewalling chains.
  357.  
  358.   This section describes all you really need to know to build a packet
  359.   filter that meets your needs.
  360.  
  361.  
  362.   3.1.  How packets traverse the filters.
  363.  
  364.   The kernel starts with three lists of rules; these lists arecalled
  365.   firewall chains or just chains.  The three chains are called input,
  366.   output and forward.  When a packet comes in (say, through the Ethernet
  367.   card) the kernel uses the input chain to decide its fate.  If it
  368.   survives that step, then the kernel decides where to send the packet
  369.   next (this is called routing).  If it is destined for another machine,
  370.   it consults the forward chain.  Finally, just before a packet is to go
  371.   out, the kernel consults the output chain.
  372.  
  373.  
  374.   A chain is a checklist of rules.  Each rule says `if the packet header
  375.   looks like this, then here's what to do with the packet'.  If the rule
  376.   doesn't match the packet, then the next rule in the chain is
  377.   consulted.  Finally, if there are no more rules to consult, then the
  378.   kernel looks at the chain policy to decide what to do.  In a security-
  379.   conscious system, this policy usually tells the kernel to reject or
  380.   deny the packet.
  381.  
  382.  
  383.   For ASCII-art fans, this shown the complete path of a packet coming
  384.   into a machine.
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                        ACCEPT/
  398.                       REDIRECT                                        ACCEPT
  399.   --> C --> S --> ______ --> D --> ~~~~~~~~ --> local ------> _______ -->
  400.       h  -> a    |input |    e    {Routing } |  __|____ -->->|output |
  401.       e  |  n    |Chain |    m    {Decision} | |forward|   | |Chain  |
  402.       c  |  i    |______|    a     ~~~~~~~~  | |Chain  |   | |_______|
  403.       k  |  t       |        s        |      | |_______|   |     |
  404.       s  |  y       |        q        |      |     |       |     |
  405.       u  |  |       v        e        v      |     |       |     v
  406.       m  |  |     DENY/      r  Local Process|     v       |   DENY/
  407.       |  |  v    REJECT      a        |-------   DENY/     |  REJECT
  408.       |  |DENY               d        |         REJECT     |
  409.       v  |                   e -------+---------------------
  410.      DENY|                            |
  411.          ------------------------------
  412.  
  413.  
  414.   Here is a blow-by-blow description of each stage:
  415.  
  416.  
  417.      Checksum:
  418.         This is a test that the packet hasn't been corrupted in some
  419.         way.  If it has, it is denied.
  420.  
  421.  
  422.      Sanity:
  423.         There is actually one of these sanity checks before each
  424.         firewall chain, but the input chain's is the most important.
  425.         Some malformed packets might confuse the rule-checking code, and
  426.         these are denied here (a message is printed to the syslog if
  427.         this happens).
  428.  
  429.  
  430.      input chain:
  431.         This is the first firewall chain against which the packet will
  432.         be tested.  If the verdict of the chain is not DENY or REJECT,
  433.         the packet continues on.
  434.  
  435.  
  436.      Demasquerade:
  437.         If the packet is a reply to a previously masqueraded packet, it
  438.         is demasqueraded, and skips straight to the output chain.  If
  439.         you don't use IP Masquerading, you can mentally erase this from
  440.         the diagram.
  441.  
  442.  
  443.      Routing decision:
  444.         The destination field is examined by the routing code, to decide
  445.         if this packet should go to a local process (see Local process
  446.         below) or forwarded to a remote machine (see forward chain
  447.         below).
  448.  
  449.  
  450.      Local process:
  451.         A process running on the machine can receive packets after the
  452.         Routing Decision step, and can send packets (which go through
  453.         the output then input chains with interface set to `lo' if they
  454.         are destined for a local process, otherwise they only traverse
  455.         the output chain).  This is inter-process chain traversal isn't
  456.         fully shown on the diagram, as it is too icky.
  457.  
  458.  
  459.      local:
  460.         If the packet was not created by a local process, then the
  461.         forward chain is checked, otherwise the packet goes straight to
  462.         the output chain.
  463.      forward chain:
  464.         This chain is traversed for any packets which are attempting to
  465.         pass through this machine to another.
  466.  
  467.  
  468.      output chain:
  469.         This chain is traversed for all packets just before they are
  470.         sent out.
  471.  
  472.  
  473.   3.1.1.  Using ipchains .
  474.  
  475.   First, check that you have the version of ipchains that this document
  476.   refers to:
  477.  
  478.  
  479.  
  480.        $ ipchains --version
  481.        ipchains 1.3.5, 26-June-1998
  482.  
  483.  
  484.  
  485.  
  486.  
  487.   ipchains has a fairly detailed manual page (man ipchains), and if you
  488.   need more detail on particulars, you can check out the programming
  489.   interface (man 4 ipfw), or the file net/ipv4/ip_fw.c in the 2.1.x
  490.   kernel source, which is (obviously) authoritative.
  491.  
  492.  
  493.   There are several different things you can do with ipchains.  First
  494.   the operations to manage whole chains.  You start with three built-in
  495.   chains input, output and forward which you can't delete.
  496.  
  497.  
  498.   1. Create a new chain (-N).
  499.  
  500.   2. Delete an empty chain (-X).
  501.  
  502.   3. Change the policy for a built-in chain. (-P).
  503.  
  504.   4. List the rules in a chain (-L).
  505.  
  506.   5. Flush the rules out of a chain (-F).
  507.  
  508.   6. Zero the packet and byte counters on all rules in a chain (-Z).
  509.  
  510.   There are several ways to manipulate rules inside a chain:
  511.  
  512.  
  513.   1. Append a new rule to a chain (-A).
  514.  
  515.   2. Insert a new rule at some position in a chain (-I).
  516.  
  517.   3. Replace a rule at some position in a chain (-R).
  518.  
  519.   4. Delete a rule at some position in a chain (-D).
  520.  
  521.   5. Delete the first rule that matches in a chain (-D).
  522.  
  523.   There are a few operations for masquerading, which are in ipchains for
  524.   want of a good place to put them:
  525.  
  526.  
  527.   1. List the currently masqueraded connections (-M -L).
  528.  
  529.   2. Set masquerading timeout values (-M -S). (But see ``I can't set
  530.      masquerading timeouts!'').
  531.  
  532.   The final (and perhaps the most useful) function allows you to check
  533.   what would happen to a given packet if it were to traverse a given
  534.   chain.
  535.  
  536.  
  537.   3.1.2.  Operations on a single rule.
  538.  
  539.   This is the bread-and-butter of ipchains; manipulating rules.  Most
  540.   commonly, you will probably use the append (-A) and delete (-D)
  541.   commands.  The others (-I for insert and -R for replace) are simple
  542.   extensions of these concepts.
  543.  
  544.  
  545.   Each rule specifies a set of conditions the packet must meet, and what
  546.   to do if it meets them (a `target').  For example, you might want to
  547.   deny all ICMP packets coming from the IP address 127.0.0.1.  So in
  548.   this case our conditions are that the protocol must be ICMP and that
  549.   the source address must be 127.0.0.1.  Our target is `DENY'.
  550.  
  551.  
  552.   127.0.0.1 is the `loopback' interface, which you will have even if you
  553.   have no real network connection.  You can use the `ping' program to
  554.   generate such packets (it simply sends an ICMP type 8 (echo request)
  555.   which all cooperative hosts should obligingly respond to with an ICMP
  556.   type 0 (echo reply) packet).  This makes it useful for testing.
  557.  
  558.  
  559.  
  560.        # ping -c 1 127.0.0.1
  561.        PING 127.0.0.1 (127.0.0.1): 56 data bytes
  562.        64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.2 ms
  563.  
  564.        --- 127.0.0.1 ping statistics ---
  565.        1 packets transmitted, 1 packets received, 0% packet loss
  566.        round-trip min/avg/max = 0.2/0.2/0.2 ms
  567.        # ipchains -A input -s 127.0.0.1 -p icmp -j DENY
  568.        # ping -c 1 127.0.0.1
  569.        PING 127.0.0.1 (127.0.0.1): 56 data bytes
  570.  
  571.        --- 127.0.0.1 ping statistics ---
  572.        1 packets transmitted, 0 packets received, 100% packet loss
  573.        #
  574.  
  575.  
  576.  
  577.  
  578.   You can see here that the first ping succeeds (the `-c 1' tells ping
  579.   to only send a single packet).
  580.  
  581.  
  582.   Then we append (-A) to the `input' chain, a rule specifying that for
  583.   packets from 127.0.0.1 (`-s 127.0.0.1') with protocol ICMP (`-p ICMP')
  584.   we should jump to DENY (`-j DENY').
  585.  
  586.  
  587.   Then we test our rule, using the second ping.  There will be a pause
  588.   before the program gives up waiting for a response that will never
  589.   come.
  590.  
  591.  
  592.   We can delete the rule in one of two ways.  Firstly, since we know
  593.   that it is the only rule in the input chain, we can use a numbered
  594.   delete, as in:
  595.                # ipchains -D input 1
  596.                #
  597.  
  598.  
  599.  
  600.  
  601.   To delete rule number 1 in the input chain.
  602.  
  603.  
  604.   The second way is to mirror the -A command, but replacing the -A with
  605.   -D.  This is useful when you have a complex chain of rules and you
  606.   don't want to have to count them to figure out that it's rule 37 that
  607.   you want to get rid of.  In this case, we would use:
  608.  
  609.  
  610.                # ipchains -D input -s 127.0.0.1 -p icmp -j DENY
  611.                #
  612.  
  613.  
  614.  
  615.  
  616.   The syntax of -D must have exactly the same options as the -A (or -I
  617.   or -R) command.  If there are multiple identical rules in the same
  618.   chain, only the first will be deleted.
  619.  
  620.  
  621.   3.1.3.  Filtering specifications.
  622.  
  623.   We have seen the use of `-p' to specify protocol, and `-s' to specify
  624.   source address, but there are other options we can use to specify
  625.   packet characteristics.  What follows is an exhaustive compendium.
  626.  
  627.  
  628.   3.1.3.1.  Specifying source and destination IP addresses.
  629.  
  630.   Source (-s) and destination (-d) IP addresses can be specified in four
  631.   ways.  The most common way is to use the full name, such as
  632.   `localhost' or `www.linuxhq.com'.  The second way is to specify the IP
  633.   address such as `127.0.0.1'.
  634.  
  635.  
  636.   The third and fourth ways allow specification of a group of IP
  637.   addresses, such as `199.95.207.0/24' or `199.95.207.0/255.255.255.0'.
  638.   These both specify any IP address from 192.95.207.0 to 192.95.207.255
  639.   inclusive; the digits after the `/' tell which parts of the IP address
  640.   are significant.  `/32' or `/255.255.255.255' is the default (match
  641.   all of the IP address).  To specify any IP address at all `/0' can be
  642.   used, like so:
  643.  
  644.  
  645.                # ipchains -A input -s 0/0 -j DENY
  646.                #
  647.  
  648.  
  649.  
  650.  
  651.   This is rarely used, as the effect above is the same as not specifying
  652.   the `-s' option at all.
  653.  
  654.  
  655.   3.1.3.2.  Specifying inversion.
  656.  
  657.   Many flags, including the `-s' and `-d' flags can have their arguments
  658.   preceded by `!' (pronounced `not') to match addresses NOT equal to the
  659.   ones given.  For example. `-s ! localhost' matches any packet not
  660.   coming from localhost.
  661.   3.1.3.3.  Specifying protocol.
  662.  
  663.   The protocol can be specified with the `-p' flag.  Protocol can be a
  664.   number (if you know the numeric protocol values for IP) or a name for
  665.   the special cases of `TCP', `UDP' or `ICMP'.  Case doesn't matter, so
  666.   `tcp' works as well as `TCP'.
  667.  
  668.  
  669.   The protocol name can be prefixed by a `!', to invert it, such as `-p
  670.   ! TCP'.
  671.  
  672.  
  673.   3.1.3.3.1.  Specifying UDP and TCP ports.
  674.  
  675.   For the special case where a protocol of TCP or UDP is specified,
  676.   there can be an extra argument indicating the TCP or UDP port, or an
  677.   (inclusive) range of ports (but see ``Handling Fragments'' below).  A
  678.   range is represented using a `:' character, such as `6000:6010', which
  679.   covers 11 port numbers, from 6000 to 6010 inclusive.  If the lower
  680.   bound is omitted, it defaults to 0.  If the upper bound is omitted, it
  681.   defaults to 65535.  So to specify TCP connections coming from ports
  682.   under 1024, the syntax would be as `-p TCP -s 0.0.0.0/0 :1024'.  Port
  683.   numbers can be specified by name, eg. `www'.
  684.  
  685.  
  686.   Note that the port specification can be preceded by a `!', which
  687.   inverts it.  So to specify every TCP packet BUT a WWW packet, you
  688.   would specify
  689.  
  690.   -p TCP -d 0.0.0.0/0 ! www
  691.  
  692.  
  693.  
  694.   It is important to realise that the specification
  695.  
  696.  
  697.   -p TCP -d ! 192.168.1.1 www
  698.  
  699.  
  700.  
  701.   is very different from
  702.  
  703.   -p TCP -d 192.168.1.1 ! www
  704.  
  705.  
  706.  
  707.   The first specifies any TCP packet to the WWW port on any machine but
  708.   192.168.1.1.  The second specifies any TCP connection to any port on
  709.   192.168.1.1 but the WWW port.
  710.  
  711.  
  712.   Finally, this case means not the WWW port and not 192.168.1.1:
  713.  
  714.   -p TCP -d ! 192.168.1.1 ! www
  715.  
  716.  
  717.  
  718.  
  719.   3.1.3.3.2.  Specifying ICMP type & code.
  720.  
  721.   ICMP also allows an optional argument, but as ICMP doesn't have ports,
  722.   (ICMP has a type and a code) they have a different meaning.
  723.  
  724.  
  725.   You can specify them as ICMP names (use ipchains -h icmp to list the
  726.   names) after the `-s' option, or as a numeric ICMP type and code,
  727.   where the type follows the `-s' option and the code follows the `-d'
  728.   option.
  729.  
  730.  
  731.   The ICMP names are fairly long: you only need use enough letters to
  732.   make the name distinct from any other.
  733.  
  734.  
  735.   Here is a small table of some of the most common ICMP packets:
  736.  
  737.  
  738.        Number  Name                     Required by
  739.  
  740.        0       echo-reply               ping
  741.        3       destination-unreachable  Any TCP/UDP traffic.
  742.        5       redirect                 routing if not running routing daemon
  743.        8       echo-request             ping
  744.        11      time-exceeded            traceroute
  745.  
  746.  
  747.  
  748.  
  749.   Note that the ICMP names cannot be preceeded by `!' at the moment.
  750.  
  751.  
  752.   DO NOT DO NOT DO NOT block all ICMP type 3 messages!  (See ``ICMP
  753.   Packets'' below).
  754.  
  755.  
  756.   3.1.3.4.  Specifying an interface.
  757.  
  758.   The `-i' option specifies the name of an interface to match.  An
  759.   interface is the physical device the packet came in on, or is going
  760.   out on.  You can use the ifconfig command to list the interfaces which
  761.   are `up' (ie. working at the moment).
  762.  
  763.  
  764.   The interface for incoming packets (ie. packets traversing the input
  765.   chain) is considered to be the interface they came in on.  Logically,
  766.   the interface for outgoing packets (packets traversing the output
  767.   chain) is the interface they will go out on.  The interface for
  768.   packets traversing the forward chain is also the interface they will
  769.   go out on; a fairly arbitrary decision it seems to me.
  770.  
  771.  
  772.   It is perfectly legal to specify an interface that currently does not
  773.   exist; the rule will not match anything until the interface comes up.
  774.   This is extremely useful for dial-up ppp links (usually interface
  775.   ppp0) and the like.
  776.  
  777.  
  778.   As a special case, an interface name ending with a `+' will match all
  779.   interfaces (whether they currently exist or not) which begin with that
  780.   string.  For example, to specify a rule which matches all PPP
  781.   interfaces, the -i ppp+ option would be used.
  782.  
  783.  
  784.   The interface name can be preceded by a `!' to match a packet which
  785.   does NOT match the specified interface(s).
  786.  
  787.  
  788.   3.1.3.5.  Specifying TCP SYN packets only.
  789.  
  790.   It is sometimes useful to allow TCP connections in one direction, but
  791.   not the other.  For example, you might want to allow connections to an
  792.   external WWW server, but not connections from that server.
  793.   The naive approach would be to block TCP packets coming from the
  794.   server.  Unfortunately, TCP connections require packets going in both
  795.   directions to work at all.
  796.  
  797.  
  798.   The solution is to block only the packets used to request a
  799.   connection.  These packets are called SYN packets (ok, technically
  800.   they're packets with the SYN flag set, and the FIN and ACK flags
  801.   cleared, but we call them SYN packets).  By disallowing only these
  802.   packets, we can stop attempted connections in their tracks.
  803.  
  804.  
  805.   The `-y' flag is used for this: it is only valid for rules which
  806.   specify TCP as their protocol.  For example, to specify TCP connection
  807.   attempts from 192.168.1.1:
  808.  
  809.   -p TCP -s 192.168.1.1 -y
  810.  
  811.  
  812.  
  813.  
  814.   Once again, this flag can be inverted by preceding it with a `!',
  815.   which means every packet other than the connection initiation.
  816.  
  817.  
  818.   3.1.3.6.  Handling fragments.
  819.  
  820.   Sometimes a packet is too large to fit down a wire all at once.  When
  821.   this happens, the packet is divided into fragments, and sent as
  822.   multiple packets.  The other end reassembles the fragments to
  823.   reconstruct the whole packet.
  824.  
  825.  
  826.   The problem with fragments is that some of the specifications listed
  827.   above (in particular, source port, destinations port, ICMP type, ICMP
  828.   code, or TCP SYN flag) require the kernel to peek at the start of the
  829.   packet, which is only contained in the first fragment.
  830.  
  831.  
  832.   If your machine is the only connection to an external network, then
  833.   you can tell the Linux kernel to reassemble all fragments which pass
  834.   through it, by compiling the kernel with IP: always defragment set to
  835.   `Y'.  This sidesteps the issue neatly.
  836.  
  837.  
  838.   Otherwise, it is important to understand how fragments get treated by
  839.   the filtering rules.  Any filtering rule that asks for information we
  840.   don't have will not match.  This means that the first fragment is
  841.   treated like any other packet.  Second and further fragments won't be.
  842.   Thus a rule -p TCP -s 192.168.1.1 www (specifying a source port of
  843.   `www') will never match a fragment (other than the first fragment).
  844.   Neither will the opposite rule -p TCP -s 192.168.1.1 ! www.
  845.  
  846.  
  847.   However, you can specify a rule specifically for second and further
  848.   fragments, using the `-f' flag.  Obviously, it is illegal to specify a
  849.   TCP or UDP port, ICMP type, ICMP code or TCP SYN flag in such a
  850.   fragment rule.
  851.  
  852.  
  853.   It is also legal to specify that a rule does not apply to second and
  854.   further fragments, by preceding the `-f' with `!'.
  855.  
  856.  
  857.   Usually it is regarded as safe to let second and further fragments
  858.   through, since filtering will effect the first fragment, and thus
  859.   prevent reassembly on the target host, however, bugs have been known
  860.   to allow crashing of machines simply by sending fragments.  Your call.
  861.  
  862.  
  863.   Note for network-heads: malformed packets (TCP, UDP and ICMP packets
  864.   too short for the firewalling code to read the ports or ICMP code and
  865.   type) are treated as fragments as well.  Only TCP fragments starting
  866.   at position 8 are explicitly dropped by the firewall code (a message
  867.   should appear in the syslog if this occurs).
  868.  
  869.  
  870.   As an example, the following rule will drop any fragments going to
  871.   192.168.1.1:
  872.  
  873.  
  874.  
  875.  
  876.        # ipchains -A output -f -D 192.168.1.1 -j DENY
  877.        #
  878.  
  879.  
  880.  
  881.  
  882.  
  883.   3.1.4.  Filtering side effects.
  884.  
  885.   OK, so now we know all the ways we can match a packet using a rule.
  886.   If a packet matches a rule, the following things happen:
  887.  
  888.  
  889.   1. The byte counter for that rule is increased by the size of the
  890.      packet (header and all).
  891.  
  892.   2. The packet counter for that rule is incremented.
  893.  
  894.   3. If the rule requests it, the packet is logged.
  895.  
  896.   4. If the rule requests it, the packet's Type Of Service field is
  897.      changed.
  898.  
  899.   5. If the rule requests it, the packet is marked (not in 2.0 kernel
  900.      series).
  901.  
  902.   6. The rule target is examined to decide what to do to the packet
  903.      next.
  904.  
  905.  
  906.   For variety, I'll address these in order of importance.
  907.  
  908.  
  909.   3.1.4.1.  Specifying a target.
  910.  
  911.   A target tells the kernel what to do with a packet that matches a
  912.   rule.  ipchains uses `-j' (think `jump-to') for the target
  913.   specification.
  914.  
  915.  
  916.   The simplest case is when there is no target specified.  This type of
  917.   rule (often called an `accounting' rule) is useful for simply counting
  918.   a certain type of packet.  Whether this rule matches or not, the
  919.   kernel simply examines the next rule in the chain.  For example, to
  920.   count the number of packets from 192.168.1.1, we could do this:
  921.  
  922.  
  923.  
  924.  
  925.   # ipchains -A input -s 192.168.1.1
  926.   #
  927.  
  928.  
  929.  
  930.  
  931.  
  932.   (Using `ipchains -L -v' we can see the byte and packet counters
  933.   associated with each rule).
  934.  
  935.  
  936.   There are six special targets.  The first three, ACCEPT, REJECT and
  937.   DENY are fairly simple.  ACCEPT allows the packet through.  DENY drops
  938.   the packet as if it had never been received.  REJECT drops the packet,
  939.   but (if it's not an ICMP packet) generates an ICMP reply to the source
  940.   to tell it that the destination was unreachable.
  941.  
  942.  
  943.   The next one, MASQ tells the kernel to masquerade the packet.  For
  944.   this to work, your kernel needs to be compiled with IP Masquerading
  945.   enabled.  For details on this, see the Masquerading-HOWTO and the
  946.   Appendix ``Differences between ipchains and ipfwadm''.  This target is
  947.   only valid for packets traversing the forward chain.
  948.  
  949.  
  950.   The other major special target is REDIRECT which tells the kernel to
  951.   send a packet to a local port instead of wherever it was heading.
  952.   This can only be specified for rules specifying TCP or UDP as their
  953.   protocol.  Optionally, a port (name or number) can be specified
  954.   following `-j REDIRECT' which will cause the packet to be redirected
  955.   to that particular port, even if it was addressed to another port.
  956.   This target is only valid for packets traversing the input chain.
  957.  
  958.  
  959.   The final special target is RETURN which is identical to falling off
  960.   the end of the chain immediately.  (See ``Setting Policy'' below).
  961.  
  962.  
  963.   Any other target indicates a user-defined chain (as described in
  964.   ``Operations on an Entire Chain'' below).  The packet will begin
  965.   traversing the rules in that chain.  If that chain doesn't decide the
  966.   fate of the packet, then once traversal on that chain has finished,
  967.   traversal resumes on the next rule in the current chain.
  968.  
  969.  
  970.   Time for more ASCII art.  Consider two (silly) chains: input (the
  971.   built-in chain) and Test (a user-defined chain).
  972.  
  973.  
  974.            `input'                         `Test'
  975.           ----------------------------    ----------------------------
  976.           | Rule1: -p ICMP -j REJECT |    | Rule1: -s 192.168.1.1    |
  977.           |--------------------------|    |--------------------------|
  978.           | Rule2: -p TCP -j Test    |    | Rule2: -d 192.168.1.1    |
  979.           |--------------------------|    ----------------------------
  980.           | Rule3: -p UDP -j DENY    |
  981.           ----------------------------
  982.  
  983.  
  984.  
  985.  
  986.   Consider a TCP packet coming from 192.168.1.1, going to 1.2.3.4.  It
  987.   enters the input chain, and gets tested against Rule1 - no match.
  988.   Rule2 matches, and its target is Test, so the next rule examined is
  989.   the start of Test.  Rule1 in Test matches, but doesn't specify a
  990.   target, so the next rule is examined, Rule2.  This doesn't match, so
  991.   we have reached the end of the chain.  We return to the input chain,
  992.   where we had just examined Rule2, so we now examine Rule3, which
  993.   doesn't match either.
  994.  
  995.  
  996.   So the packet path is:
  997.  
  998.                                   v    __________________________
  999.            `input'                |   /    `Test'                v
  1000.           ------------------------|--/    -----------------------|----
  1001.           | Rule1                 | /|    | Rule1                |   |
  1002.           |-----------------------|/-|    |----------------------|---|
  1003.           | Rule2                 /  |    | Rule2                |   |
  1004.           |--------------------------|    -----------------------v----
  1005.           | Rule3                 /--+___________________________/
  1006.           ------------------------|---
  1007.                                   v
  1008.  
  1009.  
  1010.  
  1011.  
  1012.   See the section ``How to Organise Your Firewall Rules'' for ways to
  1013.   use user-defined chains effectively.
  1014.  
  1015.  
  1016.   3.1.4.2.  Logging packets.
  1017.  
  1018.   This is a side effect that matching a rule can have; you can have the
  1019.   matching packet logged using the `-l' flag.  You will usually not want
  1020.   this for routine packets, but it is a useful feature if you want to
  1021.   look for exceptional events (see man klogd or man dmesg).
  1022.  
  1023.  
  1024.   3.1.4.3.  Manipulating the Type Of Service
  1025.  
  1026.   There are four seldom-used bits in the IP header, called the Type of
  1027.   Service (TOS) bits.  The effect the way packets are treated; the four
  1028.   bits are "Minimum Delay", "Maximum Throughput", "Maximum Reliability"
  1029.   and "Minimum Cost".  Only one of these bits is allowed to be set.  Rob
  1030.   van Nieuwkerk, the author of the TOS-mangling code, puts it as
  1031.   follows:
  1032.  
  1033.  
  1034.        Especially the "Minimum Delay" is important for me.  I
  1035.        switch it on for "interactive" packets in my upstream
  1036.        (Linux) router.  I'm behind a 33k6 modem link.  Linux pri¡
  1037.        oritises packets in 3 queues.  This way I get acceptable
  1038.        interactive performance while doing bulk downloads at the
  1039.        same time.  (It could even be better if there wasn't such a
  1040.        big queue in the serial driver, but latency is kept down 1.5
  1041.        seconds now).
  1042.  
  1043.  
  1044.  
  1045.   The most common use is to set telnet & ftp control connections to
  1046.   "Minimum Delay" and FTP data to "Maximum Throughput".  This would be
  1047.   done as follows:
  1048.  
  1049.  
  1050.  
  1051.        ipchains -A output -p tcp -d 0.0.0.0/0 telnet -t 0x01 0x10
  1052.        ipchains -A output -p tcp -d 0.0.0.0/0 ftp -t 0x01 0x10
  1053.        ipchains -A output -p tcp -s 0.0.0.0/0 ftp-data -t 0x01 0x08
  1054.  
  1055.  
  1056.  
  1057.   The `-t' flag takes two extra parameters, both in hexadecimal.  These
  1058.   allow complex twiddling of the TOS bits: the first mask is ANDed with
  1059.   the packet's current TOS, and then the second mask is XORed with it.
  1060.   If this is too confusing, just use the following table:
  1061.  
  1062.  
  1063.  
  1064.        TOS Name                Value           Typical Uses
  1065.  
  1066.        Minimum Delay           0x01 0x10       ftp, telnet
  1067.        Maximum Throughput      0x01 0x08       ftp-data
  1068.        Maximum Reliability     0x01 0x04       snmp
  1069.        Minimum Cost            0x01 0x02       nntp
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.   3.1.4.4.  Marking a packet.
  1076.  
  1077.   This is currently unused, but it is my hope that it will allow complex
  1078.   and powerful interactions with Alexey Kuznetsov's new Quality of
  1079.   Service implementation which replaces the Traffic Shaper in the v2.1
  1080.   kernels.  Thus, it is ignored altogether in the 2.0 kernel series.
  1081.  
  1082.  
  1083.   3.1.4.5.  Operations on an entire chain.
  1084.  
  1085.   A very useful feature of ipchains is the ability to group related
  1086.   rules into chains.  You can call the chains whatever you want, as long
  1087.   as the names don't clash with the built-in chains (input, output and
  1088.   forward) or the targets (MASQ, REDIRECT, ACCEPT, DENY, REJECT or
  1089.   RETURN).  I suggest avoiding upper-case labels entirely, since I may
  1090.   use these for future extensions.  The chain name can be up to 8
  1091.   characters long.
  1092.  
  1093.  
  1094.   3.1.4.6.  Creating a new chain.
  1095.  
  1096.   Let's create a new chain.  Because I am such an imaginative fellow,
  1097.   I'll call it test.
  1098.  
  1099.  
  1100.  
  1101.        # ipchains -N test
  1102.        #
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.   It's that simple.  Now you can put rules in it as detailed above.
  1109.  
  1110.  
  1111.   3.1.4.7.  Deleting a chain.
  1112.  
  1113.   Deleting a chain is simple as well.
  1114.  
  1115.  
  1116.  
  1117.        # ipchains -X test
  1118.        #
  1119.  
  1120.  
  1121.  
  1122.  
  1123.   Why `-X'?  Well, all the good letters were taken.
  1124.  
  1125.  
  1126.   There are a couple of restrictions to deleting chains: they must be
  1127.   empty (see ``Flushing a Chain'' below) and they must not be the target
  1128.   of any rule.  You can't delete any of the three built-in chains.
  1129.  
  1130.  
  1131.   3.1.4.8.  Flushing a chain.
  1132.  
  1133.   There is a simple way of emptying all rules out of a chain, using the
  1134.   `-F' command.
  1135.  
  1136.  
  1137.  
  1138.                # ipchains -F forward
  1139.                #
  1140.  
  1141.  
  1142.  
  1143.  
  1144.  
  1145.   If you don't specify a chain, then all chains will be flushed.
  1146.  
  1147.  
  1148.   3.1.4.9.  Listing a chain.
  1149.  
  1150.   You can list all the rules in a chain by using the `-L' command.
  1151.  
  1152.  
  1153.  
  1154.        # ipchains -L input
  1155.        Chain input (refcnt = 1): (policy ACCEPT)
  1156.        target     prot opt    source                destination           ports
  1157.        ACCEPT     icmp -----  anywhere              anywhere              any
  1158.        # ipchains -L test
  1159.        Chain test (refcnt = 0):
  1160.        target     prot opt    source                destination           ports
  1161.        DENY       icmp -----  localnet/24           anywhere              any
  1162.        #
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.   The `refcnt' listed for test is the number of rules which have test as
  1169.   their target.  This must be zero (and the chain be empty) before this
  1170.   chain can be deleted.
  1171.  
  1172.  
  1173.   If the chain name is omitted, all chains are listed, even empty ones.
  1174.  
  1175.  
  1176.   There are three options which can accompany `-L'.  The `-n' (numeric)
  1177.   option is very useful as it prevents ipchains from trying to lookup
  1178.   the IP addresses, which (if you are using DNS like most people) will
  1179.   cause large delays if your DNS is not set up properly, or you have
  1180.   filtered out DNS requests.  It also causes ports to be printed out as
  1181.   numbers rather than names.
  1182.  
  1183.  
  1184.   The `-v' options shows you all the details of the rules, such as the
  1185.   the packet and byte counters, the TOS masks, the interface, and the
  1186.   packet mark.  Otherwise these values are omitted.  For example:
  1187.  
  1188.  
  1189.        # ipchains -v -L input
  1190.        Chain input (refcnt = 1): (policy ACCEPT)
  1191.         pkts bytes target     prot opt   tosa tosx  ifname    mark        source                destination           ports
  1192.           10   840 ACCEPT     icmp ----- 0xFF 0x00  lo                    anywhere              anywhere              any
  1193.  
  1194.  
  1195.  
  1196.  
  1197.  
  1198.   Note that the packet and byte counters are printed out using the
  1199.   suffixes `K', `M' or `G' for 1000, 1,000,000 and 1,000,000,000
  1200.   respectively.  Using the `-x' (expand numbers) flag as well prints the
  1201.   full numbers, no matter how large they are.
  1202.  
  1203.  
  1204.   3.1.4.10.  Resetting (zeroing) counters.
  1205.  
  1206.   It is useful to be able to reset the counters.  This can be done with
  1207.   the `-Z' (zero counters) option.  For example:
  1208.  
  1209.  
  1210.  
  1211.        # ipchains -v -L input
  1212.        Chain input (refcnt = 1): (policy ACCEPT)
  1213.         pkts bytes target     prot opt   tosa tosx  ifname    mark        source                destination           ports
  1214.           10   840 ACCEPT     icmp ----- 0xFF 0x00  lo                    anywhere              anywhere              any
  1215.        # ipchains -Z input
  1216.        # ipchains -v -L input
  1217.        Chain input (refcnt = 1): (policy ACCEPT)
  1218.         pkts bytes target     prot opt   tosa tosx  ifname    mark        source                destination           ports
  1219.            0     0 ACCEPT     icmp ----- 0xFF 0x00  lo                    anywhere              anywhere              any
  1220.        #
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.   The problem with this approach is that sometimes you need to know the
  1227.   counter values immediately before they are reset.  In the above
  1228.   example, some packets could pass through between the `-L' and `-Z'
  1229.   commands.  For this reason, you can use the `-L' and `-Z' together, to
  1230.   reset the counters while reading them.  Unfortunately, if you do this,
  1231.   you can't operate on a single chain: you have to list and zero all the
  1232.   chains at once.
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.   # ipchains -L -v -Z
  1256.   Chain input (policy ACCEPT):
  1257.    pkts bytes target     prot opt   tosa tosx  ifname    mark        source                destination           ports
  1258.      10   840 ACCEPT     icmp ----- 0xFF 0x00  lo                    anywhere              anywhere              any
  1259.  
  1260.   Chain forward (refcnt = 1): (policy ACCEPT)
  1261.   Chain output (refcnt = 1): (policy ACCEPT)
  1262.   Chain test (refcnt = 0):
  1263.       0     0 DENY       icmp ----- 0xFF 0x00  ppp0                  localnet/24           anywhere              any
  1264.   # ipchains -L -v
  1265.   Chain input (policy ACCEPT):
  1266.    pkts bytes target     prot opt   tosa tosx  ifname    mark        source                destination           ports
  1267.      10   840 ACCEPT     icmp ----- 0xFF 0x00  lo                    anywhere              anywhere              any
  1268.  
  1269.   Chain forward (refcnt = 1): (policy ACCEPT)
  1270.   Chain output (refcnt = 1): (policy ACCEPT)
  1271.   Chain test (refcnt = 0):
  1272.       0     0 DENY       icmp ----- 0xFF 0x00  ppp0                  localnet/24           anywhere              any
  1273.   #
  1274.  
  1275.  
  1276.  
  1277.  
  1278.  
  1279.   3.1.4.11.  Setting policy.
  1280.  
  1281.   We glossed over what happens when a packet hits the end of a built-in
  1282.   chain when we discussed how a packet walks through chains in
  1283.   ``Specifying a Target'' above.  In this case, the policy of the chain
  1284.   determines the fate of the packet.  Only built-in chains (input,
  1285.   output and forward) have policies, because if a packet falls off the
  1286.   end of a user-defined chain, traversal resumes at the previous chain.
  1287.  
  1288.  
  1289.   The policy can be any of the first four special targets: ACCEPT, DENY,
  1290.   REJECT or MASQ.  MASQ is only valid for the `forward' chain.
  1291.  
  1292.  
  1293.   It is also important to note that a RETURN target in a rule in one of
  1294.   the built-in chains is useful to explicitly target the chain policy
  1295.   when a packet matches a rule.
  1296.  
  1297.  
  1298.   3.1.5.  Operations on masquerading.
  1299.  
  1300.   There are several parameters you can tweak for IP Masquerading.  They
  1301.   are bundled with ipchains because it's not worth writing a separate
  1302.   tool for them (although this will change).
  1303.  
  1304.  
  1305.   The IP Masquerading command is `-M', and it can be combined with `-L'
  1306.   to list currently masqueraded connections, or `-S' to set the
  1307.   masquerading parameters.
  1308.  
  1309.  
  1310.   The `-L' command can be accompanied by `-n' (show numbers instead of
  1311.   hostnames and port names) or `-v' (show deltas in sequence numbers for
  1312.   masqueraded connection, just in case you care).
  1313.  
  1314.  
  1315.   The `-S' command should be followed by three timeout values, each in
  1316.   seconds: for TCP sessions, for TCP sessions after a FIN packet, and
  1317.   for UDP packets.  If you don't want to change one of these values,
  1318.   simply give a value of `0'.
  1319.  
  1320.  
  1321.   The default values are listed in `/usr/include/net/ip_masq.h',
  1322.   currently 15 minutes, 2 minutes and 5 minutes respectively.
  1323.  
  1324.  
  1325.   The most common value to change is the first one, for FTP (see ``FTP
  1326.   Nightmares'' below).
  1327.  
  1328.  
  1329.   Note the problems with setting timeouts listed in ``I can't set
  1330.   masquerading timeouts!''.
  1331.  
  1332.  
  1333.   3.1.6.  Checking a packet.
  1334.  
  1335.   Sometimes you want to see what happens when a certain packet enters
  1336.   your machine, such as for debugging your firewall chains.  ipchains
  1337.   has the `-C' command to allow this, using the exact same routines that
  1338.   the kernel uses to diagnose real packets.
  1339.  
  1340.  
  1341.   You specify which chain to test the packet on by following the `-C'
  1342.   argument with its name.  Whereas the kernel always starts traversing
  1343.   on the input, output or forward chains, you are allowed to begin
  1344.   traversing on any chain for testing purposes.
  1345.  
  1346.  
  1347.   The details of the `packet' are specified using the same syntax used
  1348.   to specify firewall rules.  In particular, a protocol (`-p'), source
  1349.   address (`-s'), destination address (`-d') and interface (`-i') are
  1350.   compulsory.  If the protocol is TCP or UDP, then a single source and a
  1351.   single destination port must be specified, and a ICMP type and code
  1352.   must be specified for the ICMP protocol (unless the `-f' flag is
  1353.   specified to indicate a fragment rule, in which case these options are
  1354.   illegal).
  1355.  
  1356.  
  1357.   If the protocol is TCP (and the `-f' flag is not specified), the `-y'
  1358.   flag may be specified, to indicate that the test packet should have
  1359.   the SYN bit set.
  1360.  
  1361.  
  1362.   Here is an example of testing a TCP SYN packet from 192.168.1.1 port
  1363.   60000 to 192.168.1.2 port www, entering the `input' chain.  (This is a
  1364.   classic incoming WWW connection initiation):
  1365.  
  1366.  
  1367.  
  1368.        # ipchains -C input -p tcp -y -s 192.168.1.1 60000 -d 192.168.1.2 www
  1369.        packet accepted
  1370.        #
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.   3.1.7.  Multiple rules at once and watching what happens.
  1377.  
  1378.   Sometimes a single command line can result in multiple rules being
  1379.   effected.  This is done in two ways.  Firstly, if you specify a
  1380.   hostname which resolves (using DNS) to multiple IP addresses, ipchains
  1381.   will act as if you had typed multiple commands with each combination
  1382.   of addresses.
  1383.  
  1384.  
  1385.   So if the hostname `www.foo.com' resolves to three IP addresses, and
  1386.   the hostname `www.bar.com' resolves to two IP addresses, then the
  1387.   command `ipchains -A input -j reject -s www.bar.com -d www.foo.com'
  1388.   would append six rules to the input chain.
  1389.  
  1390.  
  1391.   The other way to have ipchains perform multiple actions is to use the
  1392.   bidirectional flag (`-b').  This flag makes ipchains behave as if you
  1393.   had typed the command twice, the second time with the `-s' and `-d'
  1394.   arguments reversed.  So, to avoid forwarding either to or from
  1395.   192.168.1.1, you could do the following:
  1396.  
  1397.  
  1398.  
  1399.        # ipchains -b -A forward -j reject -s 192.168.1.1
  1400.        #
  1401.  
  1402.  
  1403.  
  1404.  
  1405.  
  1406.   Personally, I don't like the `-b' option much; if you want
  1407.   convenience, see ``Using ipchains-save'' below.
  1408.  
  1409.  
  1410.   The -b option can be used with the insert (`-I'), delete (`-D') (but
  1411.   not the variation which takes a rule number), append (`-A') and check
  1412.   (`-C') commands.
  1413.  
  1414.  
  1415.   Another useful flag is `-v' (verbose) which prints out exactly what
  1416.   ipchains is doing with your commands.  This is useful if you are
  1417.   dealing with commands that may effect multiple rules.  For example,
  1418.   here we check the behaviour of fragments between 192.168.1.1 and
  1419.   192.168.1.2.
  1420.  
  1421.  
  1422.  
  1423.        # ipchains -v -b -C input -p tcp -f -s 192.168.1.1 -d 192.168.1.2 -i lo
  1424.          tcp opt   ---f- tos 0xFF 0x00  via lo    192.168.1.1  -> 192.168.1.2    * ->   *
  1425.        packet accepted
  1426.          tcp opt   ---f- tos 0xFF 0x00  via lo    192.168.1.2  -> 192.168.1.1    * ->   *
  1427.        packet accepted
  1428.        #
  1429.  
  1430.  
  1431.  
  1432.  
  1433.  
  1434.   3.2.  Useful Examples
  1435.  
  1436.   I have a dialup PPP connection (-i ppp0).  I grab news (-p TCP -s
  1437.   news.virtual.net.au nntp) and mail (-p TCP -s mail.virtual.net.au
  1438.   pop-3) every time I dial up.  I use Debian's ftp method to update my
  1439.   machine regularly (-p TCP -y -s ftp.debian.org.au ftp-data).  I surf
  1440.   the web through my ISP's proxy while this is going on (-p TCP -d
  1441.   proxy.virtual.net.au 8080), but hate the ads from doubleclick.net on
  1442.   the Dilbert Archive (-p TCP -y -d 199.95.207.0/24 & -p TCP -y -d
  1443.   199.95.208.0/24).
  1444.  
  1445.  
  1446.   I don't mind people trying to ftp to my machine while I'm online (-p
  1447.   TCP -d $LOCALIP ftp), but don't want anyone outside pretending to have
  1448.   an IP address of my internal network (-s 192.168.1.0/24).  This is
  1449.   commonly called IP spoofing, and there is a better way to protect
  1450.   yourself from it in the 2.1.x kernels and above: see ``How do I set up
  1451.   IP spoof protection?''.
  1452.  
  1453.   This setup is fairly simple, because there are currently no other
  1454.   boxes on my internal network.
  1455.  
  1456.  
  1457.   I don't want any local process (ie. Netscape, lynx etc.) to connect to
  1458.   doubleclick.net:
  1459.  
  1460.  
  1461.  
  1462.        # ipchains -A output -d 199.95.207.0/24 -j REJECT
  1463.        # ipchains -A output -d 199.95.208.0/24 -j REJECT
  1464.        #
  1465.  
  1466.  
  1467.  
  1468.  
  1469.  
  1470.   Now I want to set priorities on various outgoing packets (there isn't
  1471.   much point in doing it on incoming packets).  Since I have a fair
  1472.   number of these rules, it makes sense to put them all in a single
  1473.   chain, called ppp-out.
  1474.  
  1475.  
  1476.  
  1477.        # ipchains -N ppp-out
  1478.        # ipchains -A output -i ppp0 -j ppp-out
  1479.        #
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.   Minimum delay for web traffic & telnet.
  1486.  
  1487.  
  1488.  
  1489.        # ipchains -A ppp-out -p TCP -d proxy.virtual.net.au 8080 -t 0x00 0x10
  1490.        # ipchains -A ppp-out -p TCP -d 0.0.0.0 telnet -t 0x00 0x10
  1491.        #
  1492.  
  1493.  
  1494.  
  1495.  
  1496.  
  1497.   Low priority for ftp data, nntp, pop-3:
  1498.  
  1499.  
  1500.  
  1501.        # ipchains -A ppp-out -p TCP -d 0.0.0.0/0 ftp-data -t 0x00 0x02
  1502.        # ipchains -A ppp-out -p TCP -d 0.0.0.0/0 nntp -t 0x00 0x02
  1503.        # ipchains -A ppp-out -p TCP -d 0.0.0.0/0 pop-3 -t 0x00 0x02
  1504.        #
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510.   There are a few restrictions on packets coming in the ppp0 interface:
  1511.   let's create a chain called `ppp-in':
  1512.  
  1513.  
  1514.  
  1515.        # ipchains -N ppp-in
  1516.        # ipchains -A input -i ppp0 -j ppp-in
  1517.        #
  1518.  
  1519.   Now, no packets coming in ppp0 should be claiming a source address of
  1520.   192.168.1.*, so we log and deny them:
  1521.  
  1522.  
  1523.  
  1524.        # ipchains -A ppp-in -s 192.168.1.0/24 -l -j DENY
  1525.        #
  1526.  
  1527.  
  1528.  
  1529.  
  1530.  
  1531.   I allow connections in for DNS (I forward all requests to 203.29.16.1,
  1532.   so I expect DNS TCP replies from them only), ftp, and return ftp-data
  1533.   only (which should only be going to a port above 1023, and not the X11
  1534.   ports around 6000).
  1535.  
  1536.  
  1537.  
  1538.        # ipchains -A ppp-in -p TCP -s 203.29.16.1 -d $LOCALIP dns -j ACCEPT
  1539.        # ipchains -A ppp-in -p TCP -s 0.0.0.0/0 ftp-data -d $LOCALIP 1024:5999 -j ACCEPT
  1540.        # ipchains -A ppp-in -p TCP -s 0.0.0.0/0 ftp-data -d $LOCALIP 6010: -j ACCEPT
  1541.        # ipchains -A ppp-in -p TCP -d $LOCALIP ftp -j ACCEPT
  1542.        #
  1543.  
  1544.  
  1545.  
  1546.  
  1547.  
  1548.   Finally, local-to-local packets are OK:
  1549.  
  1550.  
  1551.  
  1552.        # ipchains -A input -i lo -j ACCEPT
  1553.        #
  1554.  
  1555.  
  1556.  
  1557.  
  1558.  
  1559.   Now, my default policy on the input chain is DENY, so everything else
  1560.   gets dropped:
  1561.  
  1562.  
  1563.  
  1564.        # ipchains -P input DENY
  1565.        #
  1566.  
  1567.  
  1568.  
  1569.  
  1570.  
  1571.   NOTE: I wouldn't set up my chains in this order, as packets might get
  1572.   through while I'm setting up.  Safest is usually to set the policy to
  1573.   DENY first, then insert the rules.  Of course, if your rules require
  1574.   DNS lookups to resolve hostnames, you could be in trouble.
  1575.  
  1576.  
  1577.   3.2.1.  Using ipchains-save .
  1578.  
  1579.   Setting up firewall chains just the way you want them, and then trying
  1580.   to remember the commands you used so you can do them next time is a
  1581.   pain.
  1582.  
  1583.  
  1584.  
  1585.   So, ipchains-save is a script which reads your current chains setup
  1586.   and saves it to a file.  For the moment I'll keep you in suspense with
  1587.   regards to what ipchains-restore does.
  1588.  
  1589.  
  1590.   ipchains-save can save a single chain, or all chains (if no chain name
  1591.   is specified).  The only option currently permitted is `-v' which
  1592.   prints the rules (to stderr) as they are saved.  The policy of the
  1593.   chain is also saved for input, output and forward chains.
  1594.  
  1595.  
  1596.  
  1597.        $ ipchains-save > my_firewall
  1598.        Saving `input'.
  1599.        Saving `output'.
  1600.        Saving `forward'.
  1601.        Saving `ppp-in'.
  1602.        Saving `ppp-out'.
  1603.        $
  1604.  
  1605.  
  1606.  
  1607.  
  1608.  
  1609.   3.2.2.  Using ipchains-restore .
  1610.  
  1611.   ipchains-restore restores chains as saved with ipchains-save.  It can
  1612.   take two options: `-v' which describes each rule as it is added, and
  1613.   `-f' which forces flushing of user-defined chains if they exist, as
  1614.   described below.
  1615.  
  1616.  
  1617.   If a user-defined chain is found in the input, ipchains-restore checks
  1618.   if that chain already exists.  If it does, then you will be prompted
  1619.   whether the chains should be flushed (cleared of all rules) or whether
  1620.   restoring this chain should be skipped.  If you specified `-f' on the
  1621.   command line, you will not be prompted; the chain will be flushed.
  1622.  
  1623.  
  1624.   You must be root to run this script; it uses ipchains to attempt to
  1625.   restore the rules.
  1626.  
  1627.  
  1628.   For example:
  1629.  
  1630.  
  1631.  
  1632.        # ipchains-restore < my_firewall
  1633.        Restoring `input'.
  1634.        Restoring `output'.
  1635.        Restoring `forward'.
  1636.        Restoring `ppp-in'.
  1637.        Chain `ppp-in' already exists. Skip or flush? [S/f]? s
  1638.        Skipping `ppp-in'.
  1639.        Restoring `ppp-out'.
  1640.        Chain `ppp-out' already exists. Skip or flush? [S/f]? f
  1641.        Flushing `ppp-out'.
  1642.        #
  1643.  
  1644.  
  1645.  
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.   4.  Miscellaneous.
  1652.  
  1653.   This section contains all the information and FAQs that I couldn't fit
  1654.   inside the structure above.
  1655.  
  1656.  
  1657.   4.1.  How to organise your firewall rules.
  1658.  
  1659.   This question requires some thought.  You can try to organise them to
  1660.   optimize speed (minimize the number of rule-checks for the most common
  1661.   packets) or to increase managability.
  1662.  
  1663.  
  1664.   If you have an intermittent link, say a PPP link, you might want to
  1665.   set the first rule in the input chain to be set to `-i ppp0 -j DENY'
  1666.   at boot time, then have something like this in your ip-up script:
  1667.  
  1668.  
  1669.  
  1670.        # Re-create the `ppp-in' chain.
  1671.        ipchains-restore -f < ppp-in.firewall
  1672.  
  1673.        # Replace DENY rule with jump to ppp-handling chain.
  1674.        ipchains -R input 1 -i ppp0 -j ppp-in
  1675.  
  1676.  
  1677.  
  1678.  
  1679.  
  1680.   Your ip-down script would look like:
  1681.  
  1682.  
  1683.  
  1684.        ipchains -R input 1 -i ppp0 -j DENY
  1685.  
  1686.  
  1687.  
  1688.  
  1689.  
  1690.  
  1691.   4.2.  What not  to filter out.
  1692.  
  1693.   There are some things you should be aware of before you start
  1694.   filtering out everything you don't want.
  1695.  
  1696.  
  1697.   4.2.1.  ICMP packets.
  1698.  
  1699.   ICMP packets are used (among other things) to indicate failure for
  1700.   other protocols (such as TCP and UDP).  `destination-unreachable'
  1701.   packets in particular.  Blocking these packets means that you will
  1702.   never get `Host unreachable' or `No route to host' errors; any
  1703.   connections will just wait for a reply that never comes.  This is
  1704.   irritating, but rarely fatal.
  1705.  
  1706.  
  1707.   A worse problem is the role of ICMP packets in MTU discovery.  All
  1708.   good TCP implementations (Linux included) use MTU discovery to try to
  1709.   figure out what the largest packet that can get to a destination
  1710.   without being fragmented (fragmentation slows performance, especially
  1711.   when occasional fragments are lost).  MTU discovery works by sending
  1712.   packets with the "Don't Fragment" bit set, and then sending smaller
  1713.   packets if it gets an ICMP packet indicating "Fragmentation needed but
  1714.   DF set" (`fragmentation-needed').  This is a type of `destination-
  1715.   unreachable' packet, and if it is never received, the local host will
  1716.   not reduce MTU, and performance will be abysmal or non-existant.
  1717.   4.2.2.  DNS replies.
  1718.  
  1719.   You might think that you can block any incoming TCP connections, but
  1720.   there are two cases where this will break things, and the first is
  1721.   DNS.  Your machine uses DNS to turn hostnames into IP addresses.
  1722.   Normally DNS uses UDP, however, if the reply is large, it uses TCP to
  1723.   reply.  Blocking these connections will make DNS unreliable.
  1724.  
  1725.  
  1726.   If your DNS queries are always directed at the same external source
  1727.   (either directly by using the nameserver line in /etc/resolv.conf or
  1728.   by using a caching nameserver in forward mode), then you need only
  1729.   allow TCP connections to port domain from that machine.
  1730.  
  1731.  
  1732.   4.2.3.  FTP nightmares.
  1733.  
  1734.   The other classic problem is FTP.  FTP has two modes; the traditional
  1735.   one is called active mode and the more recent one is called passive
  1736.   mode.  Web browsers usually default to passive mode, but command-line
  1737.   ftp programs usually default to active mode.
  1738.  
  1739.  
  1740.   In active mode, when the remote end wants to send a file (or even the
  1741.   results of an ls or dir command) it tries to open a TCP connection to
  1742.   the local machine.  This means you can't filter out these TCP
  1743.   connections without breaking active FTP.
  1744.  
  1745.  
  1746.   If you have the option of using passive mode, then fine; passive mode
  1747.   makes data connections from client to server, even for incoming data.
  1748.   Otherwise, it is recommended that you only allow TCP connections to
  1749.   ports above 1024 and not between 6000 and 6010 (6000 is used for X-
  1750.   Windows).
  1751.  
  1752.  
  1753.   4.3.  Filtering out Ping of Death.
  1754.  
  1755.   Linux boxes are now immune to the famous Ping of Death, which involves
  1756.   sending an illegally-large ICMP packet which overflows buffers in the
  1757.   TCP stack on the receiver and causes havoc.
  1758.  
  1759.  
  1760.   If you are protecting boxes which might be vulnerable, you could
  1761.   simply block ICMP fragments.  Normal ICMP packets aren't large enough
  1762.   to require fragmentation, so you won't break anything except big
  1763.   pings.  I have heard (unconfirmed) reports that some systems required
  1764.   only the last fragment of an oversize ICMP packet to corrupt them, so
  1765.   blocking only the first fragment is not recommended.
  1766.  
  1767.  
  1768.   While the exploit programs I have seen all use ICMP, there is no
  1769.   reasons that TCP or UDP fragments (or an unknown protocol) could not
  1770.   be used for this attack, so blocking ICMP fragments is only a
  1771.   temporary solution.
  1772.  
  1773.  
  1774.   4.4.  Filtering out Teardrop and Bonk.
  1775.  
  1776.   Teardrop and Bonk are two attacks (mainly against Microsoft Windows NT
  1777.   machines) which rely on overlapping fragments.  Having your Linux
  1778.   router do defragmentation, or disallowing all fragments to your
  1779.   vulnerable machines are the other options.
  1780.  
  1781.  
  1782.  
  1783.   4.5.  Filtering out Fragment Bombs.
  1784.  
  1785.   Some less-reliable TCP stacks are said to have problems dealing with
  1786.   large numbers of fragments of packets when they don't receive all the
  1787.   fragments.  Linux does not have this problem.  You can filter out
  1788.   fragments (which might break legitimate uses) or compile your kernel
  1789.   with `IP: always defragment' set to `Y' (only if your Linux box is the
  1790.   only possible route for these packets).
  1791.  
  1792.  
  1793.   4.6.  Changing firewall rules.
  1794.  
  1795.   There are some timing issues involved in altering firewall rules.  If
  1796.   you are not careful, you can let packets through while you are half-
  1797.   way through your changes.  A simplistic approach is to do the
  1798.   following:
  1799.  
  1800.  
  1801.  
  1802.        # ipchains -I input 1 -j DENY
  1803.        # ipchains -I output 1 -j DENY
  1804.        # ipchains -I forward 1 -j DENY
  1805.  
  1806.        ... make changes ...
  1807.  
  1808.        # ipchains -D input 1
  1809.        # ipchains -D output 1
  1810.        # ipchains -D forward 1
  1811.        #
  1812.  
  1813.  
  1814.  
  1815.  
  1816.   This drops all packets for the duration of the changes.
  1817.  
  1818.  
  1819.   If you changes are restricted to a single chain, you might want to
  1820.   create a new chain with the new rules, and then replace (`-R') the
  1821.   rule that pointed to the old chain with one that points to the new
  1822.   chain: then you can delete the old chain.  This replacement will occur
  1823.   atomically.
  1824.  
  1825.  
  1826.   4.7.  How do I set up IP spoof protection?
  1827.  
  1828.   IP spoofing is a technique where a host sends out packets which claim
  1829.   to be from another host.  Since packet filtering makes decisions based
  1830.   on this source address, IP spoofing is uses to fool packet filters.
  1831.   It is also used to hide the identity of attackers using SYN attacks,
  1832.   Teardrop, Ping of Death and the like (don't worry if you don't know
  1833.   what they are).
  1834.  
  1835.  
  1836.   The best way to protect from IP spoofing is called Source Address
  1837.   Verificaion, and it is done by the routing code, and not firewalling
  1838.   at all.  Look for a file called /proc/sys/net/ipv4/conf/all/rp_filter.
  1839.   If this exists, then turning on Source Address Verification at every
  1840.   boot is the right solution for you.  To do that, insert the following
  1841.   lines somewhere in your init scripts, before any network interfaces
  1842.   are initialised (eg. Debian users would put them in
  1843.   /etc/init.d/netbase if they are not already there):
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.   # This is the best method: turn on Source Address Verification and get
  1850.   # spoof protection on all current and future interfaces.
  1851.   if [ -e /proc/sys/net/ipv4/conf/all/rp_filter ]; then
  1852.     echo -n "Setting up IP spoofing protection..."
  1853.     for f in /proc/sys/net/ipv4/conf/*/rp_filter; do
  1854.         echo 1 > $f
  1855.     done
  1856.     echo "done."
  1857.   else
  1858.     echo PROBLEMS SETTING UP IP SPOOFING PROTECTION.  BE WORRIED.
  1859.     echo "CONTROL-D will exit from this shell and continue system startup."
  1860.     echo
  1861.     # Start a single user shell on the console
  1862.     /sbin/sulogin $CONSOLE
  1863.   fi
  1864.  
  1865.  
  1866.  
  1867.  
  1868.  
  1869.   If you cannot do this, you can manually insert rules to protect every
  1870.   interface.  This requires knowledge of each interface.  The 2.1
  1871.   kernels automatically reject packets claiming to come from the 127.*
  1872.   addresses (reserved for the local loopback interface, lo).
  1873.  
  1874.  
  1875.   For example, say we have three interfaces, eth0, eth1 and ppp0.  We
  1876.   can use ifconfig to tell us the address and netmask of the interfaces.
  1877.   Say eth0 was attached to a network 192.168.1.0 with netmask
  1878.   255.255.255.0, eth1 was attached to a network 10.0.0.0 with netmask
  1879.   255.0.0.0, and ppp0 connected to the internet (where any address
  1880.   except the reserved private IP addresses are allowed), we would insert
  1881.   the following rules:
  1882.  
  1883.  
  1884.  
  1885.        # ipchains -A input -i eth0 -s ! 192.168.1.0/255.255.255.0 -j DENY
  1886.        # ipchains -A input -i ! eth0 -s 192.168.1.0/255.255.255.0 -j DENY
  1887.        # ipchains -A input -i eth1 -s ! 10.0.0.0/255.0.0.0 -j DENY
  1888.        # ipchains -A input -i ! eth1 -s 10.0.0.0/255.0.0.0 -j DENY
  1889.        #
  1890.  
  1891.  
  1892.  
  1893.  
  1894.  
  1895.   This approach is not as good as the Source Address Verification
  1896.   approach, because if your network changes, you have to change your
  1897.   firewalling rules to keep up.
  1898.  
  1899.  
  1900.   If you are running a 2.0 series kernel, you might want to protect the
  1901.   loopback interface as well, using a rule like this:
  1902.  
  1903.  
  1904.  
  1905.        # ipchains -A input -i ! lo -s 127.0.0.0/255.0.0.0 -j DENY
  1906.        #
  1907.  
  1908.  
  1909.  
  1910.  
  1911.  
  1912.  
  1913.  
  1914.  
  1915.   4.8.  Advanced projects.
  1916.  
  1917.   There is a userspace library I have written which is included with the
  1918.   source distribution called `libfw'.  It uses the ability of IP Chains
  1919.   1.3 and above to copy a packet to userspace (using the
  1920.   IP_FIREWALL_NETLINK config option).
  1921.  
  1922.  
  1923.   Things such as stateful inspection (I prefer the term dynamic
  1924.   firewalling) can be implemented in userspace using this library.
  1925.   Other nifty ideas include controlling packets on a per-user basis by
  1926.   doing a lookup in a userspace daemon.  This should be pretty easy.
  1927.  
  1928.  
  1929.   The `mark' capability of the firewalls is underutilised: it could
  1930.   easily be used to represent a priority for the Quality of Service
  1931.   code, which would make it simple to control packet priorities.
  1932.  
  1933.  
  1934.   4.9.  Future enhancements.
  1935.  
  1936.   Firewalling and NAT are being redesigned for 2.3.  Plans and
  1937.   discussions are available on the netdev archive.  These enhancements
  1938.   should clear up many outstanding usability issues (really, firewalling
  1939.   and masquerading shouldn't be this hard), and allow growth for far
  1940.   more flexible firewalling.
  1941.  
  1942.  
  1943.   5.  Common problems.
  1944.  
  1945.  
  1946.   5.1.  Masquerading/forwarding doesn't work!
  1947.  
  1948.   Make sure that packet forwarding is enabled (in recent kernels it is
  1949.   disabled by default, meaning that packets never even try to traverse
  1950.   the `forward' chain).  You can override this (as root) by typing
  1951.  
  1952.  
  1953.  
  1954.        # echo 1 > /proc/sys/net/ipv4/ip_forward
  1955.        #
  1956.  
  1957.  
  1958.  
  1959.  
  1960.  
  1961.   If this works for you, you can put this somewhere in your bootup
  1962.   scripts so it is enabled every time.
  1963.  
  1964.  
  1965.   5.2.  Wildcard interfaces don't work!
  1966.  
  1967.   There was a bug in versions 2.1.102 and 2.1.103 of the kernel (and
  1968.   some old patches I produced) which made ipchains commands which
  1969.   specified a wildcard interface (such as -i ppp+) fail.
  1970.  
  1971.  
  1972.   This is fixed in recent kernels, and in the 2.0.34 patch on the web
  1973.   site.  You can also fix it by hand in the kernel source by changing
  1974.   line 63 or so in include/linux/ip_fw.h:
  1975.  
  1976.  
  1977.  
  1978.        #define IP_FW_F_MASK    0x002F  /* All possible flag bits mask   */
  1979.  
  1980.  
  1981.   This should read ``0x003F''.  Fix this and recompile the kernel.
  1982.  
  1983.  
  1984.   5.3.  TOS doesn't work!
  1985.  
  1986.   This was my mistake: setting the Type of Service field dif not
  1987.   actually set the Type of Service in kernel versions 2.1.102 through
  1988.   2.1.111.  This problem was fixed in 2.1.112.
  1989.  
  1990.  
  1991.   5.4.  ipautofw  and ipportfw  don't work!
  1992.  
  1993.   For 2.0.x, this is true; I haven't time to create and maintain a jumbo
  1994.   patch for ipchains and ipautofw/ipportfw.
  1995.  
  1996.  
  1997.   For 2.1.x, download ipmasqadm from
  1998.  
  1999.   <url url="http://juanjox.home.ml.org/"
  2000.           name="http://juanjox.home.ml.org/">
  2001.  
  2002.  
  2003.   and use it exactly as you would have used ipautofw or ipportfw, except
  2004.   instead of ipportfw you type ipmasqadm portfw, and instead of ipautofw
  2005.   you type ipmasqadm autofw.
  2006.  
  2007.  
  2008.   5.5.  xosview is broken!
  2009.  
  2010.   Upgrade to version 1.6.0 or above, which doesn't require any firewall
  2011.   rules at all for 2.1.x kernels.
  2012.  
  2013.  
  2014.   5.6.  Segmentation fault with -j REDIRECT !
  2015.  
  2016.   This was a bug in ipchains version 1.3.3.  Please upgrade.
  2017.  
  2018.  
  2019.  
  2020.   5.7.  I can't set masquerading timeouts!
  2021.  
  2022.   True (for 2.1.x kernels) up to and including 2.1.112.  This is being
  2023.   persued vigorously at the moment, and by the time you read this it
  2024.   might have been fixed.  My web page will contain a patch when
  2025.   available.
  2026.  
  2027.  
  2028.   5.8.  I want to firewall IPX!
  2029.  
  2030.   So do a number of others, it seems.  My code only covers IP,
  2031.   unfortunately.  On the good side, all the hooks are there to firewall
  2032.   IPX!  You just need to write the code; I will happily help where
  2033.   possible.
  2034.  
  2035.  
  2036.   6.  Appendix: Differences between ipchains  and ipfwadm
  2037.  
  2038.   Some of these changes are a result of kernel changes, and some a
  2039.   result of ipchains being different from ipfwadm.
  2040.  
  2041.  
  2042.  
  2043.   1. Many arguments have been remapped: capitals now indicates a
  2044.      command, and lower case now indicates an option.
  2045.  
  2046.  
  2047.   2. Arbitrary chains are supported, so even built-in chains have full
  2048.      names instead of flags (eg. `input' instead of `-I').
  2049.  
  2050.   3. The `-k' option has vanished: use `! -y'.
  2051.  
  2052.   4. The `-b' option actually inserts/appends/deletes two rules, rather
  2053.      than a single `bidirectional' rule.
  2054.  
  2055.   5. The `-b' option can be passed to `-C' to do two checks (one in each
  2056.      direction).
  2057.  
  2058.   6. The `-x' option to `-l' has been replaced by `-v'.
  2059.  
  2060.   7. Multiple source and destination ports are not supported anymore.
  2061.      Hopefully being able to negate the port range will somewhat make up
  2062.      for that.
  2063.  
  2064.   8. Interfaces can only be specified by name (not address).  The old
  2065.      semantics got silently changed in the 2.1 kernel series anyway.
  2066.  
  2067.   9. Fragments are examined, not automatically allowed through.
  2068.  
  2069.   10.
  2070.      Explicit accounting chains have been done away with.
  2071.  
  2072.   11.
  2073.      Arbitrary protocols over IP can be tested for.
  2074.  
  2075.   12.
  2076.      The old behaviour of SYN and ACK matching (which was previously
  2077.      ignored for non-TCP packets) has changed; the SYN option is not
  2078.      valid for non-TCP-specific rules.
  2079.  
  2080.   13.
  2081.      Counters are now 64-bit on 32-bit machines, not 32-bit.
  2082.  
  2083.   14.
  2084.      Inverse options are now supported.
  2085.  
  2086.   15.
  2087.      ICMP codes are now supported.
  2088.  
  2089.   16.
  2090.      Wildcard interfaces are now supported.
  2091.  
  2092.   17.
  2093.      TOS manipulations are now sanity-checked: the old kernel code would
  2094.      silently stop you from (illegally) manipulating the `Must Be Zero'
  2095.      TOS bit; ipchains now returns an error if you try, as well as for
  2096.      other illegal cases.
  2097.  
  2098.  
  2099.   6.1.  Quick-Reference table.
  2100.  
  2101.   [ Mainly, command arguments are UPPER CASE, and option arguments are
  2102.   lower case ]
  2103.  
  2104.  
  2105.   One thing to note, masquerading is specified by `-j MASQ'; it is
  2106.   completely different from `-j ACCEPT', and not treated as merely a
  2107.   side-effect, unlike ipfwadm does.
  2108.  
  2109.  
  2110.  
  2111.  
  2112.  
  2113.   ================================================================
  2114.   | ipfwadm      | ipchains              | Notes
  2115.   ----------------------------------------------------------------
  2116.   | -A [both]    | -N acct               | Create an `acct' chain
  2117.   |              |& -I 1 input -j acct   | and have output and input
  2118.   |              |& -I 1 output -j acct  | packets traverse it.
  2119.   |              |& acct                 |
  2120.   ----------------------------------------------------------------
  2121.   | -A in        | input                 | A rule with no target
  2122.   ----------------------------------------------------------------
  2123.   | -A out       | output                | A rule with no target
  2124.   ----------------------------------------------------------------
  2125.   | -F           | forward               | Use this as [chain].
  2126.   ----------------------------------------------------------------
  2127.   | -I           | input                 | Use this as [chain].
  2128.   ----------------------------------------------------------------
  2129.   | -O           | output                | Use this as [chain].
  2130.   ----------------------------------------------------------------
  2131.   | -M -l        | -M -L                 |
  2132.   ----------------------------------------------------------------
  2133.   | -M -s        | -M -S                 |
  2134.   ----------------------------------------------------------------
  2135.   | -a policy    | -A [chain] -j POLICY  | (but see -r and -m).
  2136.   ----------------------------------------------------------------
  2137.   | -d policy    | -D [chain] -j POLICY  | (but see -r and -m).
  2138.   ----------------------------------------------------------------
  2139.   | -i policy    | -I 1 [chain] -j POLICY| (but see -r and -m).
  2140.   ----------------------------------------------------------------
  2141.   | -l           | -L                    |
  2142.   ----------------------------------------------------------------
  2143.   | -z           | -Z                    |
  2144.   ----------------------------------------------------------------
  2145.   | -f           | -F                    |
  2146.   ----------------------------------------------------------------
  2147.   | -p           | -P                    |
  2148.   ----------------------------------------------------------------
  2149.   | -c           | -C                    |
  2150.   ----------------------------------------------------------------
  2151.   | -P           | -p                    |
  2152.   ----------------------------------------------------------------
  2153.   | -S           | -s                    | Only takes one port or
  2154.   |              |                       | range, not multiples.
  2155.   ----------------------------------------------------------------
  2156.   | -D           | -d                    | Only takes one port or
  2157.   |              |                       | range, not multiples.
  2158.   ----------------------------------------------------------------
  2159.   | -V           | <none>                | Use -i [name].
  2160.   ----------------------------------------------------------------
  2161.   | -W           | -i                    |
  2162.   ----------------------------------------------------------------
  2163.   | -b           | -b                    | Now actually makes 2 rules.
  2164.   ----------------------------------------------------------------
  2165.   | -e           | -v                    |
  2166.   ----------------------------------------------------------------
  2167.   | -k           | ! -y                  | Doesn't work unless
  2168.   |              |                       | -p tcp also specified.
  2169.   ----------------------------------------------------------------
  2170.   | -m           | -j MASQ               |
  2171.   ----------------------------------------------------------------
  2172.   | -n           | -n                    |
  2173.   ----------------------------------------------------------------
  2174.   | -o           | -l                    |
  2175.   ----------------------------------------------------------------
  2176.   | -r [redirpt] | -j REDIRECT [redirpt] |
  2177.   ----------------------------------------------------------------
  2178.   | -t           | -t                    |
  2179.   ----------------------------------------------------------------
  2180.   | -v           | -v                    |
  2181.   ----------------------------------------------------------------
  2182.   | -x           | -x                    |
  2183.   ----------------------------------------------------------------
  2184.   | -y           | -y                    | Doesn't work unless
  2185.   |              |                       | -p tcp also specified.
  2186.   ----------------------------------------------------------------
  2187.  
  2188.  
  2189.  
  2190.  
  2191.   6.2.  Examples of translated ipfwadm commands
  2192.  
  2193.   Old command: ipfwadm -F  -p deny
  2194.  
  2195.   New command: ipchains -P forward DENY
  2196.  
  2197.  
  2198.   Old command: ipfwadm -F -a m -S 192.168.0.0/24 -D 0.0.0.0/0
  2199.  
  2200.   New command: ipchains -A forward -j MASQ -s 192.168.0.0/24 -d
  2201.   0.0.0.0/0
  2202.  
  2203.  
  2204.   Old command: ipfwadm -I -a accept -V 10.1.2.1 -S 10.0.0.0/8 -D
  2205.   0.0.0.0/0
  2206.  
  2207.   New command: ipchains -A input -j ACCEPT -i eth0 -s 10.0.0.0/8 -d
  2208.   0.0.0.0/0
  2209.  
  2210.   (Note that there is no equivalent for specifying interfaces by
  2211.   address: use the interface name.  On this machine, 10.1.2.1
  2212.   corresponds to eth0).
  2213.  
  2214.  
  2215.   7.  Appendix: Using the ipfwadm-wrapper  script.
  2216.  
  2217.   The ipfwadm-wrapper shell script should be a plug-in replacement of
  2218.   ipfwadm for backwards compadibility with ipfwadm 2.3a.
  2219.  
  2220.  
  2221.   The only feature it can't really handle is the `-V' option.  When this
  2222.   is used, a warning is given.  If the `-W' option is also used, the
  2223.   `-V' option is ignored.  Otherwise, the script tries to find the
  2224.   interface name associated with that address, using ifconfig.  If that
  2225.   fails (such as for an interface which is down) then it will exit with
  2226.   an error message.
  2227.  
  2228.  
  2229.   This warning can be suppressed by either changing the `-V' to a `-W',
  2230.   or directing the standard output of the script to /dev/null.
  2231.  
  2232.  
  2233.   If you should find any mistakes in this script, or any changes between
  2234.   the real ipfwadm and this script, please report a bug to me: send an
  2235.   EMail to ipchains@wantree.com.au with "BUG-REPORT" in the subject.
  2236.   Please list your old version of ipfwadm (ipfwadm -h), your version of
  2237.   ipchains (ipchains --version), the version of the ipfwadm wrapper
  2238.   script (ipfwadm-wrapper --version).  Also send the output of ipchains-
  2239.   save.  Thanks in advance.
  2240.  
  2241.  
  2242.   Mix ipchains with this ipfwadm-wrapper script at your own peril.
  2243.  
  2244.  
  2245.   8.  Appendix: thanks.
  2246.  
  2247.   Many thanks have to go to Michael Neuling, who wrote the first
  2248.   releasable cut of the IP chains code while working for me.  Public
  2249.   apologies for nixing his result-caching idea, which Alan Cox later
  2250.   proposed and I have finally begun implementing, having seen the error
  2251.   of my ways.
  2252.  
  2253.  
  2254.   Thanks to Alan Cox for his 24-hour EMail tech support, and
  2255.   encouragement.
  2256.  
  2257.  
  2258.   Thanks to all the authors of the ipfw and ipfwadm code, especially Jos
  2259.   Vos.  Standing on the shoulders of giants and all that...  This
  2260.   applies to Linus Torvalds and all the kernel and userspace hackers as
  2261.   well.
  2262.  
  2263.  
  2264.   Thanks to the diligent beta testers and bughunters, especially Jordan
  2265.   Mendelson, Shaw Carruthers, Kevin Moule, Dr. Liviu Daia, Helmut Adams,
  2266.   Franck Sicard, Kevin Littlejohn, Matt Kemner, John D. Hardin, Alexey
  2267.   Kuznetsov, Leos Bitto, Jim Kunzman, Gerard Gerritsen, Serge Sivkov.
  2268.  
  2269.  
  2270.  
  2271.  
  2272.  
  2273.  
  2274.  
  2275.  
  2276.  
  2277.  
  2278.  
  2279.  
  2280.  
  2281.  
  2282.  
  2283.  
  2284.  
  2285.  
  2286.  
  2287.  
  2288.  
  2289.  
  2290.  
  2291.  
  2292.  
  2293.  
  2294.  
  2295.  
  2296.  
  2297.  
  2298.  
  2299.  
  2300.  
  2301.  
  2302.  
  2303.  
  2304.  
  2305.  
  2306.  
  2307.  
  2308.  
  2309.  
  2310.  
  2311.