Official Red Hat Linux Administrators Guide

Rules that allow packets to be filtered by the kernel are put in place by running the iptables command. When using the iptables command, you must specify the following options:

The options used with a given iptables rule must be grouped logically, based on the purpose and conditions of the overall rule, in order for the rule to be valid.

Tables

A powerful aspect of iptables is that multiple tables can be used to decide the fate of a particular packet, depending upon the type of packet being monitored and what is to be done with the packet. Thanks to the extendable nature of iptables, specialized tables can be created and stored in the /etc/modules/ kernel-version /kernel/net/ipv4/netfilter/ directory to meet specific goals. Think of iptables as being able to run multiple sets of ipchains rules in defined chains, with each set fulfilling a particular role.

The default table, named filter, contains the standard built-in INPUT, OUTPUT, and FORWARD chains. This is somewhat similar to the standard chains in use with ipchains. However, by default, iptables also includes two additional tables that perform specific packet filtering jobs. The nat table can be used to modify the source and destination addresses recorded in packets, and the mangle table allows you to alter packets in specialized ways. Each table contains default chains that perform necessary tasks based on the purpose of the table, but you can easily set up new chains in each of the tables.

Structure

Many iptables commands have the following structure:

iptables [-t table-name ] command chain-name parameter-1 \ option-1 parameter-n option-n

In this example, the table-name option allows the user to select a table other than the default filter table to use with the command. The command option is the center of the command, dictating a specific action to perform, such as appending a rule to or deleting a rule from a particular chain, which is specified by the chain-name option. Following the chain-name are pairs of parameters and options that actually define the way the rule will work and what will happen when a packet matches the rule.

When looking at the structure of an iptables command, it is important to remember that, unlike most other commands, the length and complexity of an iptables command can change based on its purpose. A simple command to remove a rule from a chain can be very short, while a command designed to filter packets from a particular subnet using a variety of specific parameters and options can be rather lengthy. When creating iptables commands, it is helpful to recognize that some parameters and options may create the need for other parameters and options to further specify the previous option’s request. In order to construct a valid rule, this must continue until every parameter and option that requires another set of options is satisfied. Type iptables -h to see a comprehensive list of iptables command structures.

Commands

Commands tell iptables to perform a specific action. Only one command is allowed per iptables command string. With the exception of the help command, all commands are written in uppercase characters. The iptables commands are as follows:

Parameters

Once certain iptables commands are specified, including those used to add, append, delete, insert, or replace rules within a particular chain, parameters are required to begin the construction of the packet filtering rule.

Match Options

Different network protocols provide specialized matching options that may be set in specific ways to match a particular packet using that protocol. Of course, the protocol must first be specified in the iptables command, such as using -p tcp protocol-name, to make the options for that protocol available.

TCP Protocol

These match options are available for the TCP protocol (-p tcp):

UDP Protocol

These match options are available for the UDP protocol (-p udp):

ICMP Protocol

Packets using the Internet Control Message Protocol (ICMP) can be matched using the following option when -p icmp is specified:

Modules with Additional Match Options

Additional match options are also available through modules loaded when the iptables command calls them. To use a match option module, you must load the module by name by including -m n module-name in the iptables command.

A large number of modules are available by default. It is even possible to create your own modules to provide additional match option functionality. Many modules exist, but only the most popular ones are discussed here.

The limit module allows you to place a limit on how many packets will be matched to a particular rule. This is especially beneficial when logging rule matches so that a flood of matching packets will not fill up your logs with repetitive messages or use too many system resources.

The state module, which uses the --state match option, can match a packet with these particular connection states:

These connection states can be used in combination with one another by separating them with commas, such as -m state --state INVALID,NEW. To specifically match a hardware MAC address of an Ethernet device, use the mac module, which accepts --mac-source plus a MAC address as an option. To exclude a MAC address from a rule, place an exclamation point (!) after the --mac-source match option. To view other match options available through modules, see the iptables man page.

Target Options

Once a packet has matched a particular rule, the rule can direct the packet to a number of different targets that decide its fate and, possibly, take additional actions, such as logging the action. Additionally, each chain has a default target, which is used if none of the rules on that chain match a packet or if none of the rules that match the packet specify a target. Only a few standard targets are available to decide what happens with the packet:

In addition to these standard targets, various other targets may be used with extensions called target modules. For more information about match option modules, see the section “Modules with Additional Match Options.” There are many extended target modules, most of which apply only to specific tables or situations. A couple of the most popular target modules included by default in Red Hat Linux are:

Other target extensions, including several that are useful with masquerading using the nat table or with packet alteration using the mangle table, can be found in the iptables man page.

Listing Options

The default list command, iptables -L, provides a very basic overview of the default filter table’s current chains. Additional options provide more information and arrange that information in specific ways:

Категории