SkillPilotAI

iptables / ip6tables (3/3)

The last iptables part: the remaining matches (tcp, udp, ttl, tos, tcpmss, time, u32) and all the extension targets. Matches select the packet, the target decides what happens to it: reject, NAT, rewrite MSS, mark, log or queue.

99.1 Matches vs targets

  • Match = which packet: -p tcp --dport 22, -m ttl.
  • Target = what to do: -j REJECT, -j DNAT, -j MARK.
  • Terminating targets (DROP, REJECT, DNAT) stop rule processing; non-terminating ones (LOG, MARK) do their work and continue to the next rule.
  • Linux only (iptables-nft front end of nftables); Windows uses netsh advfirewall, macOS uses pf.

99.2 How DNAT and REJECT work

DNAT + MASQUERADE: port forwarding
  1. 1.cli 192.168.119.20 sends SYN to fw 192.168.119.10:8080

  2. 2.fw DNAT: dst becomes web 192.168.119.30:80

  3. 3.fw MASQUERADE: src becomes 192.168.119.10

  4. 4.web replies SYN+ACK to the fw

  5. 5.fw uses conntrack to restore the addresses and forwards to cli: HTTP 200

DROP vs REJECT
  1. 1.cli sends SYN to web :22, rule -j DROP

  2. 2.Packet silently discarded: nc waits 3.00 s, no answer

  3. 3.cli sends SYN to web :80, rule -j REJECT --reject-with tcp-reset

  4. 4.web sends a TCP RST at once

  5. 5.curl fails with exit 7 in 0.01 s

Internet-facing firewalls usually DROP, because REJECT also answers scanners

99.3 Matches: tcp, udp, ttl, tos

Terminal
# web 192.168.119.30iptables -A INPUT -p tcp --dport 22 -j DROPiptables -A INPUT -p tcp --dport 80 \  -j REJECT --reject-with tcp-resetiptables -I INPUT 1 -p tcp \  --tcp-flags ALL NONE -j DROP# cli 192.168.119.20 (sirf apna lab)time nc -w 3 192.168.119.30 22time curl -s -m 3 http://192.168.119.30/nmap -sN -p 80 -n -Pn --max-retries 0 \  192.168.119.30# webiptables -L INPUT -n -v --line-numbers
Output
$ time nc -w 3 192.168.119.30 22Command exited with non-zero status 1real	0m 3.00s$ time curl -s -m 3 http://192.168.119.30/Command exited with non-zero status 7real	0m 0.01sPORT   STATE         SERVICE80/tcp open|filtered http$ iptables -L INPUT -n -v --line-numbersnum   pkts bytes target     prot opt in     out     source               destination1        1    40 DROP       6    --  *      *       0.0.0.0/0            0.0.0.0/0            tcp flags:0x3F/0x002        3   180 DROP       6    --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:223        1    60 REJECT     6    --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80 reject-with tcp-reset
  • --tcp-flags ALL NONE drops null-scan packets (no flags set); one nmap -sN probe hit it. Defensive rule, scanned only our own lab.
  • -p tcp loads the tcp match automatically: --sport/--dport, --tcp-flags mask comp, --syn, --tcp-option.
  • The udp match has the same --sport / --dport (Docker's DNS rule uses -m udp --dport 53).
Terminal
# webiptables -A INPUT -p icmp -m ttl \  --ttl-lt 5 -j DROPiptables -A INPUT -p icmp -m tos \  --tos Minimize-Delay -j ACCEPT# cliping -c 2 -W 1 -t 3 192.168.119.30ping -c 2 -W 1 -Q 0x10 192.168.119.30
Output
$ ping -c 2 -W 1 -t 3 192.168.119.30PING 192.168.119.30 (192.168.119.30) 56(84) bytes of data.2 packets transmitted, 0 received, 100% packet loss, time 1015ms$ ping -c 2 -W 1 -Q 0x10 192.168.119.3064 bytes from 192.168.119.30: icmp_seq=1 ttl=64 time=0.134 ms64 bytes from 192.168.119.30: icmp_seq=2 ttl=64 time=0.136 ms$ iptables -L INPUT -n -v pkts bytes target     prot opt in     out     source               destination    2   168 DROP       1    --  *      *       0.0.0.0/0            0.0.0.0/0            TTL match TTL < 5    2   168 ACCEPT     1    --  *      *       0.0.0.0/0            0.0.0.0/0            tos match0x10/0x3f
MatchOptionsNote
-m ttl--ttl-eq / --ttl-lt / --ttl-gtLab: TTL 3 pings dropped by --ttl-lt 5
-m tos--tos Minimize-Delay (0x10)Lab: ping -Q 0x10 counted
-m tcpmss--mss 1400:1500SYN and SYN/ACK only
-m time--datestart/stop, --timestart/stop, --weekdays, --monthdays, --kerneltzUTC by default
-m u32--u32 "0&0xFFFF=0x100:0xFFFF"Extract bytes at any offset and compare

Help for any extension: put -h after the match or target name (iptables -p tcp -h, iptables -m ttl -h, iptables -j REJECT -h). Even help can make the kernel load a missing module.

99.4 Target: REJECT (IPv4 and IPv6)

Terminal
# web: IPv4iptables -A INPUT -p icmp -j REJECT \  --reject-with icmp-host-prohibited# cliping -c 2 -W 1 192.168.119.30# web: IPv6 loopbackip6tables -A OUTPUT -o lo -p ipv6-icmp \  --icmpv6-type echo-request -j REJECT \  --reject-with icmp6-adm-prohibitedping -6 -c 2 -W 1 ::1iptables -j REJECT -h
Output
$ ping -c 2 -W 1 192.168.119.30From 192.168.119.30 icmp_seq=1 Destination Host ProhibitedFrom 192.168.119.30 icmp_seq=2 Destination Host Prohibited2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1022ms$ ping -6 -c 2 -W 1 ::1From ::1 icmp_seq=1 Destination unreachable: Administratively prohibitedFrom ::1 icmp_seq=2 Destination unreachable: Administratively prohibited$ iptables -j REJECT -h    icmp-port-unreachable    	ICMP port unreachable (default)    icmp-net-prohibited      	ICMP network prohibited    icmp-host-prohibited     	ICMP host prohibited    tcp-reset                	TCP RST packet    icmp-admin-prohibited    	ICMP administratively prohibited (*)
  • Default reject answer: ICMP port unreachable; --reject-with picks another type.
  • For INVALID packets DROP is better than REJECT.

99.5 Targets: DNAT, MASQUERADE, SNAT

Terminal
# fw 192.168.119.10iptables -t nat -A PREROUTING -p tcp \  --dport 8080 -j DNAT \  --to-destination 192.168.119.30:80iptables -t nat -A POSTROUTING \  -d 192.168.119.30 -p tcp --dport 80 \  -j MASQUERADE# clicurl -s -m 3 -o /dev/null \  -w 'HTTP %{http_code}\n' \  http://192.168.119.10:8080/# fw: MASQUERADE delete, phir SNATiptables -t nat -A POSTROUTING \  -d 192.168.119.30 -p tcp --dport 80 \  -j SNAT --to-source 192.168.119.10
Output
HTTP 000HTTP 200$ tail -n 3 /tmp/http.log192.168.119.20 - - [08/Nov/2026 23:31:29] "GET / HTTP/1.1" 200 -192.168.119.10 - - [08/Nov/2026 23:32:50] "GET / HTTP/1.1" 200 -$ iptables -t nat -S-A PREROUTING -p tcp -m tcp --dport 8080 -j DNAT --to-destination 192.168.119.30:80-A POSTROUTING -d 192.168.119.30/32 -p tcp -m tcp --dport 80 -j SNAT --to-source 192.168.119.10-A DOCKER_OUTPUT -d 127.0.0.11/32 -p udp -m udp --dport 53 -j DNAT --to-destination 127.0.0.11:43230
  • Before the rule: HTTP 000; after DNAT + MASQUERADE: HTTP 200, and the web log shows 192.168.119.10 instead of the client.
  • ip_forward=1 must be on at the router.
  • On an interface with a fixed IP, SNAT --to-source is better than MASQUERADE.
  • Other NAT targets: NETMAP (1:1 subnet map), REDIRECT --to-ports (local port, transparent proxy), DNPT/SNPT (IPv6 prefix translation), TPROXY (local proxy socket without NAT); --random, --random-fully, --persistent control port/address choice.

99.6 Target: TCPMSS

Terminal
# fw: mangle FORWARDiptables -t mangle -A FORWARD -p tcp \  --tcp-flags SYN,RST SYN \  -j TCPMSS --set-mss 1360# web: SYN packets capturetimeout 8 tcpdump -ni eth0 -c 2 -v \  "tcp port 80 and \  tcp[tcpflags] & tcp-syn != 0"
Output
192.168.119.10.52268 > 192.168.119.30.80: Flags [S], cksum 0x6fa8 (incorrect -> 0xa6c6), seq 2343236606, win 64240, options [mss 1360,sackOK,TS val 326948749 ecr 0,nop,wscale 10], length 0192.168.119.30.80 > 192.168.119.10.52268: Flags [S.], cksum 0x6fa8 (incorrect -> 0x7041), seq 2420120338, ack 2343236607, win 65160, options [mss 1460,sackOK,TS val 4042233397 ecr 326948749,nop,wscale 10], length 0$ iptables -t mangle -L FORWARD -n -v    2   120 TCPMSS     6    --  *      *       0.0.0.0/0            0.0.0.0/0            tcp flags:0x06/0x02 TCPMSS set 1360
  • TCPMSS rewrites the MSS in SYN packets: fixes connections that hang on VPN or PPPoE links (smaller MTU).
  • The forwarded SYN carries mss 1360; the web server's own SYN-ACK still announces 1460.
  • --clamp-mss-to-pmtu sets it automatically from the path MTU (path_MTU - 20, per the help); --set-mss and --clamp-mss-to-pmtu are mutually exclusive.

99.7 Other targets

GroupTargetsWhat they do
LoggingLOG, NFLOG, ULOG (deprecated), AUDIT, TRACEKernel log line (non-terminating, --log-prefix max 29 chars), netlink to userspace, audit records, per-rule trace (raw table)
MarkingMARK, CONNMARK, HMARK, SECMARK, CONNSECMARKPacket nfmark, connection mark (--save-mark/--restore-mark), hash-based mark, SELinux context
Header rewrite (mangle)DSCP, TOS, ECN, TTL, HL, TCPOPTSTRIP, CHECKSUM, CLASSIFYQoS bits, remove ECN bits, set/dec/inc TTL or hop limit, strip TCP options, fill checksum, tc class
Connection tracking (raw)CT, NOTRACK, SYNPROXY, NFQUEUE, TEEDisable tracking / set helper or zone, handshake proxy during SYN flood, queue to userspace, clone to another host
MiscSET, RATEEST, IDLETIMER, LED, CLUSTERIP (deprecated)Add/del address in an ipset, feed a rate estimator, idle notification, blink an LED
Output
-j LOG (non-terminating)  --log-prefix "IPT-DROP: "  max 29 chars  --log-level warning  syslog level-j CONNMARK         connection ctmark  --save-mark       nfmark se ctmark  --restore-mark    ctmark se nfmark-j CT (raw table)  --notrack           conntrack off-j SYNPROXY  (UNTRACKED,INVALID SYNs)  --mss 1460  --wscale 9-j NFQUEUE            userspace queue  --queue-num 1  --queue-balance 0:3-j SET --add-set ssh_clients src

99.8 Common mistakes

  • DNAT alone for port forwarding on the same LAN: the web server replies straight to the client, which rejects it; add MASQUERADE/SNAT and ip_forward=1.
  • Using DROP when the client should know at once (e.g. blocking port 25): use REJECT --reject-with tcp-reset.
  • Expecting LOG to stop a packet: it is non-terminating; follow it with a DROP rule.
  • Using MASQUERADE on a fixed-IP link where SNAT is cheaper and clearer.

99.9 Interview tips

  • SNAT vs MASQUERADE: both change the source; SNAT writes a fixed IP, MASQUERADE uses the outgoing interface's current IP for every new connection (DHCP/PPPoE uplinks), and its connections are cleared when the interface goes down.
  • DROP is silent (client waits for timeout), REJECT answers at once (ICMP or TCP RST).
  • DNAT in nat/PREROUTING, SNAT/MASQUERADE in nat/POSTROUTING, TCPMSS in mangle/FORWARD.
  • Terminating vs non-terminating targets: LOG and MARK continue to the next rule.
  • TCPMSS clamping fixes 'site loads partly' problems over VPN/PPPoE.