Firewall functions: IPTABLES
U
SER
G
UIDE
177
L
OAD BALANCING
The previous example of
Load Balancing
:
iptables
–
t nat
–
A PREROUTING
–
p tcp
–
d 85.34.166.21 --dport 80
–
j DNAT --to 10.10.1.195-10.10.1.198
establishes that servers to which the connections are distributed have contiguous IP addresses. By
using the match
–m nth
it is possible to surpass this limit, as shown in the following example:
iptables
–
t nat
–
A PREROUTING
–
p tcp
–
d 85.34.166.21 --dport 80 -m nth
–
-every 2
–
-packet 0
–
j DNAT --to 10.10.1.1
iptables
–
t nat
–
A PREROUTING
–
p tcp
–
d 85.34.166.21 --dport 80 -m nth
–
-every 2
–
-packet 1
–
j DNAT --to 10.10.1.10
where the connections are distributed alternately to server
10.10.1.1
and to server
10.10.1.10
.
P
ACKETS LOGGING
By using the
target –j LOG
it is possible to log packets which satisfy specific conditions through
syslog. This target is usually used to log dropped packets; in this case it is necessary to insert the
LOG rule before the respective DROP rule. For example:
iptables
–
A INPUT
–
p tcp iptables
–
A INPUT
–
p icmp
–
s 10.10.1.209
–
j LOG --log-prefix permit-
logicmp --log-level notice
23
–
s 10.10.1.1
–
j LOG --log-level notice
iptables
–
A INPUT
–
p tcp -
–
dport 23
–
s 10.10.1.1
–
j DROP
The first rule logs packets with facility notice coming from address
10.10.1.1
towards the Telnet
port. The second rule cancels them. Options which can be used with the target LOG are:
--log-level
defines the message log facility
--log-prefix
defines a prefix for log messages
This rule must be used together with the router command
set log
which allows to define the
syslog server:
set log remote 192.168.7.1
set log level 2
set log on
A usual log message used to trace all ICMP packets received from the address
10.10.1.209
,
derives from the rule:
iptables
–
A INPUT
–
p icmp
–
s 10.10.1.209
–
j LOG --log-prefix permit-logicmp --log-level notice
and it is:
Feb 10 18:25:26 im1 kernel: permit-logicmp:IN=eth0 OUT=
MAC=00:0d:5a:04:6b:3e:00:16:d4:4d:65:a7:08:00
SRC=10.10.1.209 DST=10.10.113.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=0 DF
PROTO=ICMP TYPE=8 CODE=0 ID=8725 SEQ=1
Feb 10 18:25:27 im1 kernel: permit-logicmp:IN=eth0 OUT=
MAC=00:0d:5a:04:6b:3e:00:16:d4:4d:65:a7:08:00
SRC=10.10.1.209 DST=10.10.113.1 LEN=84 TOS=0x00 PREC=0x00 TTL=64 ID=0 DF
PROTO=ICMP TYPE=8 CODE=0 ID=8725 SEQ=2
Feb 10 18:25:29 im1 kernel: permit-logicmp:IN=eth0 OUT=
MAC=00:0d:5a:04:6b:3e:00:16:d4:4d:65:a7:08:00