Skip to content
KMyFirewall
Release 1.1.1 Download

The KMyFirewall Handbook

Filtering and manipulation of packets

Packet filtering and altering is the main task iptables performs, and it does it well. A highly configurable packet filter is not trivial, so read this chapter carefully before experimenting.

This chapter shows how the different filter modules are used. It may look intimidating, but it is not that hard once you understand it, and then it becomes easy to set up complex rulesets for large routers or simple personal firewalls.

Some advanced filtering options, such as connection tracking and MAC matching, need extra modules loaded or compiled into the kernel. See the kernel compilation documentation for details about building a custom kernel.

Source and destination IP

iptables ‑A INPUT ‑‑source 12.168.120.15 ‑j ACCEPT

Source specification. The address can be a network name, a host name, a network IP address with a mask, or a plain IP address. Specifying a name that requires a remote DNS query is a bad idea. The mask can be a network mask or a plain number giving the number of 1 bits at the left side of the mask. A mask of 24 is equivalent to 255.255.255.0. A ! argument before the address inverts the match. The flag ‑‑src is an alias.

iptables ‑A INPUT ‑‑destination ! 32.112.0.31/24 ‑j ACCEPT

Destination specification works in the same way as the source option.

Checking for the protocol

iptables ‑A INPUT ‑‑protocol tcp ‑j ACCEPT
iptables ‑A INPUT ‑‑protocol ! udp ‑j LOG

The protocol can be tcp, udp, icmp or all, or a numeric value, or a name from /etc/protocols. A ! argument before the protocol inverts the test. The number zero is equivalent to all. Protocol all matches every protocol and is the default when the option is omitted.

TCP source and destination ports

iptables ‑A INPUT ‑‑protocol tcp ‑‑source‑port 22:12 ‑j LOG
iptables ‑A POSTROUTING ‑‑protocol tcp ‑‑destination‑port ! 21232 ‑j DROP

A source or destination port or port range can be a service name or a port number. An inclusive range uses the format port:port. If the first port is omitted, 0 is assumed. If the last port is omitted, 65535 is assumed. If the second port is greater than the first they are swapped. The flag ‑‑sport is an alias.

TCP flags

iptables ‑A INPUT ‑‑protocol tcp ‑‑tcp‑flags ! SYN,ACK,FIN SYN,ACK ‑j LOG

The first argument lists the flags to examine, as a comma separated list. The second argument lists the flags that must be set. Flags are SYN, ACK, FIN, RST, URG, PSH, ALL and NONE.

TCP options

iptables ‑A INPUT ‑‑protocol tcp ‑‑tcp‑option 0 ‑j LOG

Matches if the numeric TCP option is set in the TCP header. A ! can be used to invert the match.

UDP source and destination ports

UDP is the second big protocol on the Internet. Unlike TCP it does not provide a stateful connection, so UDP packets are anonymous and easy to fake. It is extremely fast because there is no connection overhead, which is why many protocols use it. Important UDP protocols include DNS and NFS.

iptables ‑A INPUT ‑‑protocol udp ‑‑source‑port 22:12 ‑j LOG
iptables ‑A POSTROUTING ‑‑protocol udp ‑‑destination‑port ! 21232 ‑j DROP

The port options work in the same way as the TCP versions.

ICMP

The Internet Control Message Protocol is the control and message protocol for IP. Common messages include host unreachable, echo request, which is the normal ping, and source quench, which tells the receiver to slow down.

iptables ‑A INPUT ‑‑protocol icmp ‑‑icmp‑type echo‑request ‑j LOG

This specifies the ICMP type as a numeric value or as one of the type names shown by iptables ‑p icmp ‑h.

Multiport match extension

The multiport match extension allows a comma separated list of up to 15 ports in a TCP or UDP rule. It is useful for saving typing when one rule should cover several ports.

iptables ‑A INPUT ‑‑protocol tcp ‑‑match multiport ‑‑source‑ports 22,21,20,80 ‑j LOG
iptables ‑A INPUT ‑‑protocol udp ‑‑match multiport ‑‑destination‑ports 20,80,10 ‑j LOG
iptables ‑A INPUT ‑‑protocol tcp ‑‑match multiport ‑‑ports 20,50,1660 ‑j LOG

Incoming and outgoing interface

iptables ‑A INPUT ‑‑in‑interface eth0 ‑j LOG
iptables ‑A OUTPUT ‑‑out‑interface ! ippp1 ‑j DROP

The in interface option names the interface on which a packet is received, for packets entering INPUT, FORWARD and PREROUTING. The out interface option names the interface on which a packet is sent, for packets entering FORWARD, OUTPUT and POSTROUTING. A ! before the name inverts the match. If the name ends in +, any interface beginning with that name matches. If the option is omitted, any interface matches.

Source MAC

iptables ‑A INPUT ‑‑match mac ‑‑mac‑source 12:E4:86:FA:5C:54 ‑j ACCEPT

The MAC source must be in the form XX:XX:XX:XX:XX:XX. This only makes sense for packets coming from an Ethernet device and entering PREROUTING, FORWARD or INPUT. It is very useful for protection against man in the middle attacks, because you can combine it with the source option to protect against ARP spoofing.

Limit matches

This module matches at a limited rate using a token bucket filter. A rule using this extension matches until the limit is reached, unless the ! flag is used. It can be combined with the LOG target for limited logging.

iptables ‑A INPUT ‑‑match limit ‑‑limit 10/second ‑‑limit‑burst 5 ‑j ACCEPT
iptables ‑A INPUT ‑‑match limit ‑‑limit 2/hour ‑‑limit‑burst 10 ‑j LOG

Stateful packet filtering

iptables ‑A INPUT ‑‑match state ‑‑state RELATED,ESTABLISHED ‑j ACCEPT

The state option is a comma separated list of connection states. INVALID means the packet is associated with no known connection. ESTABLISHED means the packet is associated with a connection that has seen packets in both directions. NEW means the packet starts a new connection, or is associated with a connection that has not seen packets in both directions. RELATED means the packet starts a new connection but is associated with an existing connection, such as an FTP data transfer or an ICMP error.

Type of service

This module matches the 8 bits of the Type of Service field in the IP header, including the precedence bits.

iptables ‑A INPUT ‑‑match tos ‑‑tos Minimize‑Delay ‑j ACCEPT

The argument is a standard name or a numeric value. Run iptables ‑m tos ‑h to see the allowed names.

See also

Continue with Using KMyFirewall. For the full command reference, see the iptables manual page and the IPTables project.

Back to top