Bonjour,
Je suis actuellement en train de monter une passerelle Internet pour un réseau d'entreprise de petite taille (une vingtaine de postes).
Le but de la passerelle est de jouer le rôle de firewall et de nater les connexions sur 2 accès Internet ADSL.
Il n'y a aucun serveur sur le réseau interne, uniquement des clients qui se connectent au Net.
Le problème que j'ai est le suivant : les connexions FTP initiés par les clients vers des serveurs sur le web sont sans cesse interrompues, en moyenne toutes les 10 minutes.
Pour ma configuration de test, je n'utilise qu'un seul accès Internet et un poste client qui est en fait une console d'administration Windows XP sur laquelle tourne MRTG. La passerelle est sous Fedora Core 5 avec Netfilter dont je configure les règles avec le script suivant :
#!/bin/sh
# Variables
PRIVATE_NETWORK=192.168.0.0/24 # Private network
PRIVATE_NETWORK_ODD_IPS=192.168.0.1/255.255.255.1 # Odd IPs to be routed via Internet access #1
PRIVATE_NETWORK_EVEN_IPS=192.168.0.2/255.255.255.1 # Even IPs to be routed via Internet access #2
PRIVATE_INTERFACE=eth0 # Interface connected to the private network
PRIVATE_IP=192.168.0.254 # IP associated to the private interface
MANAGEMENT_CONSOLE_IP=192.168.0.250 # IP of the management console
PUBLIC_NETWORK_1=xxx.xxx.xxx.xxx/xx # Public network associated to the Internet link #1
PUBLIC_INTERFACE_1=eth1 # Interface connected to the public network #1
PUBLIC_IP_1=xxx.xxx.xxx.xxx # IP associated to the public interface #1
PUBLIC_ROUTER_1=xxx.xxx.xxx.xxx # IP of the router connected to Internet link #1
MARK_1=4 # Mark associated to packets to forward them to router 1
PUBLIC_NETWORK_2=xxx.xxx.xxx.xxx/xx # Public network associated to the Internet link #2
PUBLIC_INTERFACE_2=eth2 # Interface connected to the public network #2
PUBLIC_IP_2=xxx.xxx.xxx.xxx # IP associated to the public interface #2
PUBLIC_ROUTER_2=xxx.xxx.xxx.xxx # IP of the router connected to Internet link #2
MARK_2=5 # Mark associated to packets to forward them to router 2
MODE=$1 # The mode to use (--load-balance | --router-1 | --router-2 | --off)
# Check the mode provided is correct
case "$MODE" in
--load-balance)
echo
echo "Setting the gateway to split traffic over the 2 routers :"
;;
--router-1)
echo
echo "Setting the gateway to route all traffic to router #1 :"
;;
--router-2)
echo
echo "Setting the gateway to route all traffic to router #2 :"
;;
--off)
echo 0 > /proc/sys/net/ipv4/ip_forward
# Flush all firewall rules
iptables -t filter -F
iptables -t nat -F
iptables -t mangle -F
# Set default policies to DROP all packets
iptables -t filter -P INPUT DROP
iptables -t filter -P OUTPUT DROP
iptables -t filter -P FORWARD DROP
# Accept ICMP traffic
iptables -t filter -A INPUT -p icmp -j ACCEPT # Accept ICMP input (e.g. PING requests)
iptables -t filter -A OUTPUT -p icmp -j ACCEPT # Accept ICMP output (e.g. PING replies)
echo "Gateway disabled"
exit 0
;;
*)
echo "Usage: $0 {--load-balance|--router-1|--router-2|--off}"
exit 1
esac
###################################################################################################
### Setting the firewall rules ###
###################################################################################################
echo
echo " *** Setting up iptables rules ***"
echo
# Allow to forward packets to another interface
echo 1 > /proc/sys/net/ipv4/ip_forward
# Restart the service to make sure it is started
service iptables restart
# Load modules to handle FTP connections
modprobe ip_nat_ftp
modprobe ip_conntrack_ftp
# Flush all firewall rules
iptables -t filter -F
iptables -t nat -F
iptables -t mangle -F
# Set default policies to DROP all packets
iptables -t filter -P INPUT DROP
iptables -t filter -P OUTPUT DROP
iptables -t filter -P FORWARD DROP
# Define LOG_DROP chain to log packets that are dropped
iptables -t filter -N LOG_DROP
iptables -t filter -A LOG_DROP -j LOG --log-prefix '[IPTABLES DROP]:'
iptables -t filter -A LOG_DROP -j DROP
# Source NAT packets from the private network to public IPs of the firewall
iptables -t nat -A POSTROUTING -s $PRIVATE_NETWORK -o $PUBLIC_INTERFACE_1 -j SNAT --to-source $PUBLIC_IP_1
iptables -t nat -A POSTROUTING -s $PRIVATE_NETWORK -o $PUBLIC_INTERFACE_2 -j SNAT --to-source $PUBLIC_IP_2
# Mark packets so as to route them according to the mode given as parameter
case "$MODE" in
--load-balance)
# Route the packets to the 2 routers depending on their source IP address
iptables -t mangle -A PREROUTING -s $PRIVATE_NETWORK_ODD_IPS -i $PRIVATE_INTERFACE -j MARK --set-mark $MARK_1
iptables -t mangle -A PREROUTING -s $PRIVATE_NETWORK_EVEN_IPS -i $PRIVATE_INTERFACE -j MARK --set-mark $MARK_2
;;
--router-1)
# Route all packets to the router #1 (e.g. in case of router #2 failure)
iptables -t mangle -A PREROUTING -s $PRIVATE_NETWORK -i $PRIVATE_INTERFACE -j MARK --set-mark $MARK_1
;;
--router-2)
# Route all packets to the router #2 (e.g. in case of router #1 failure)
iptables -t mangle -A PREROUTING -s $PRIVATE_NETWORK -i $PRIVATE_INTERFACE -j MARK --set-mark $MARK_2
;;
esac
# Accept ICMP traffic
iptables -t filter -A INPUT -p icmp -j ACCEPT # Accept ICMP input (e.g. PING requests)
iptables -t filter -A OUTPUT -p icmp -j ACCEPT # Accept ICMP output (e.g. PING replies)
iptables -t filter -A FORWARD -p icmp -j ACCEPT # Accept to forward ICMP traffic in both ways
# Forward all traffic that has been initiated by / is related to a previous connection
# This way, we will only have to allow new connections from inner network for required protocols
iptables -t filter -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
# Forward HTTP/HTTPS packets from connections established from private network
iptables -t filter -A FORWARD -s $PRIVATE_NETWORK -i $PRIVATE_INTERFACE -p tcp --dport http -j ACCEPT # HTTP
iptables -t filter -A FORWARD -s $PRIVATE_NETWORK -i $PRIVATE_INTERFACE -p tcp --dport https -j ACCEPT # HTTPS
# Forward FTP packets from connections established from private network
iptables -t filter -A FORWARD -s $PRIVATE_NETWORK -i $PRIVATE_INTERFACE -p tcp --dport ftp -j ACCEPT
# Accept DNS requests from private network to be transmitted to public routers
iptables -t filter -A FORWARD -s $PRIVATE_NETWORK -i $PRIVATE_INTERFACE -d $PUBLIC_ROUTER_1 -o $PUBLIC_INTERFACE_1 -p udp --dport domain -j ACCEPT # Accept DNS requests from the gateway to public router 1
iptables -t filter -A FORWARD -s $PRIVATE_NETWORK -i $PRIVATE_INTERFACE -d $PUBLIC_ROUTER_2 -o $PUBLIC_INTERFACE_2 -p udp --dport domain -j ACCEPT # Accept DNS requests from the gateway to public router 2
case "$MODE" in
--load-balance)
# Destination NAT the DNS packets to the 2 routers depending on their source IP address
iptables -t nat -A PREROUTING -s $PRIVATE_NETWORK_ODD_IPS -i $PRIVATE_INTERFACE -d $PRIVATE_IP -p udp --dport domain -j DNAT --to-destination $PUBLIC_ROUTER_1
iptables -t nat -A PREROUTING -s $PRIVATE_NETWORK_EVEN_IPS -i $PRIVATE_INTERFACE -d $PRIVATE_IP -p udp --dport domain -j DNAT --to-destination $PUBLIC_ROUTER_2
;;
--router-1)
# Destination NAT the DNS packets to the router #1 (i.e. in case of router 2 failure)
iptables -t nat -A PREROUTING -s $PRIVATE_NETWORK -i $PRIVATE_INTERFACE -d $PRIVATE_IP -p udp --dport domain -j DNAT --to-destination $PUBLIC_ROUTER_1
;;
--router-2)
# Destination NAT the DNS packets to the router #2 (i.e. in case of router 1 failure)
iptables -t nat -A PREROUTING -s $PRIVATE_NETWORK -i $PRIVATE_INTERFACE -d $PRIVATE_IP -p udp --dport domain -j DNAT --to-destination $PUBLIC_ROUTER_2
;;
esac
# Accept SNMP traffic with the management console
iptables -t filter -A INPUT -s $MANAGEMENT_CONSOLE_IP -i $PRIVATE_INTERFACE -d $PRIVATE_IP -p udp --dport 161 -j ACCEPT # Accept SNMP requests from the management console
iptables -t filter -A OUTPUT -s $PRIVATE_IP -d $MANAGEMENT_CONSOLE_IP -o $PRIVATE_INTERFACE -p udp --sport 161 -j ACCEPT # Accept SNMP replies to the management console
# Set last filter rules to LOG_DROP packets
# No rules should be inserted after these ones !!!
iptables -t filter -A INPUT -j LOG_DROP
iptables -t filter -A OUTPUT -j LOG_DROP
iptables -t filter -A FORWARD -j LOG_DROP
# Show the filter, nat and mangle tables
echo
echo "**********************************************************************"
echo FILTER tables :
echo
iptables -t filter -n -L -v
echo "**********************************************************************"
echo
echo "**********************************************************************"
echo NAT tables :
echo
iptables -t nat -n -L -v
echo "**********************************************************************"
echo
echo "**********************************************************************"
echo MANGLE tables:
echo
iptables -t mangle -n -L -v
echo "**********************************************************************"
###################################################################################################
### Setting up the routing rules ###
###################################################################################################
echo
echo " *** Setting up routing rules ***"
echo
# Flush tables that will be used later
ip rule del fwmark 4
ip rule del fwmark 5
ip route flush table $MARK_1
ip route flush table $MARK_2
# Populate the 2 tables with the main routes
ip route show table main | grep -Ev ^default | while read ROUTE ; do
ip route add table $MARK_1 $ROUTE
ip route add table $MARK_2 $ROUTE
done
# Add the default route for each table
ip route add table $MARK_1 default via $PUBLIC_ROUTER_1
ip route add table $MARK_2 default via $PUBLIC_ROUTER_2
# Associate the firewall marks to the tables
ip rule add fwmark $MARK_1 table $MARK_1
ip rule add fwmark $MARK_2 table $MARK_2
# Diplay the results
echo
echo "**********************************************************************"
echo Routing rules :
echo
ip rule show
echo "**********************************************************************"
echo
echo "**********************************************************************"
echo Table $MARK_1 :
echo
ip route show table $MARK_1
echo "**********************************************************************"
echo
echo "**********************************************************************"
echo Table $MARK_2 :
echo
ip route show table $MARK_2
echo "**********************************************************************"
echo
# Flush the cache
ip route flush cache
Après avoir analysé les logs d'iptables, je me suis rendu compte que certains paquets venant du serveur FTP auquel je me connecte sont rejetés.
J'ai poussé mon analyse plus loin en jetant un oeil à la table conntrack. J'ai trouvé dans ces analyses trois choses bizarres :
- deux autres serveurs du même domaine que le serveur FTP sur lequel je suis connecté envoient des paquets vers le client,
- le serveur FTP sur lequel je suis connecté envoit des paquets sur d'autres ports que celui sur lequel le transfert s'est initié,
- certains paquets dont toutes les caractéristiques matchent la table conntrack (IP source, IP destination, port source, port destination) sont quand même rejetés par le firewall.
Je ne suis pas un expert du FTP, aussi j'aimerai avoir quelques éclaircissements à ce sujet. Est-ce que tout cela est normal ?
Comment puis-je faire pour régler ce problème de déconnexions récurrentes ?
Pour info, je me connecte sur le FTP
www.mirrorservice.org pour télécharger la Fedora Core 6 😉.
Le client FTP se connecte au serveur en mode passif.
Je joins ci-dessous deux extraits des logs d'iptables accompagnés d'extraits de la table conntrack :
Log iptables 1 :
Nov 2 16:46:25 LVS-test kernel: [IPTABLES DROP]:IN=eth2 OUT= MAC=00:17:a4:15:1a:af:00:14:95:f2:0d:29:08:00 SRC=212.219.56.134 DST=xxx.xxx.xxx.xxx LEN=144 TOS=0x00 PREC=0x00 TTL=47 ID=21137 DF PROTO=TCP SPT=21 DPT=4746 WINDOW=5840 RES=0x00 ACK PSH URGP=0
Nov 2 16:46:47 LVS-test kernel: [IPTABLES DROP]:IN=eth2 OUT= MAC=00:17:a4:15:1a:af:00:14:95:f2:0d:29:08:00 SRC=212.219.56.134 DST=xxx.xxx.xxx.xxx LEN=144 TOS=0x00 PREC=0x00 TTL=47 ID=5691 DF PROTO=TCP SPT=21 DPT=4813 WINDOW=5840 RES=0x00 ACK PSH URGP=0
Nov 2 16:47:52 LVS-test kernel: [IPTABLES DROP]:IN=eth2 OUT= MAC=00:17:a4:15:1a:af:00:14:95:f2:0d:29:08:00 SRC=212.219.56.134 DST=xxx.xxx.xxx.xxx LEN=1500 TOS=0x00 PREC=0x00 TTL=47 ID=55200 DF PROTO=TCP SPT=45432 DPT=4873 WINDOW=5840 RES=0x00 ACK URGP=0
Nov 2 16:48:24 LVS-test kernel: [IPTABLES DROP]:IN=eth2 OUT= MAC=00:17:a4:15:1a:af:00:14:95:f2:0d:29:08:00 SRC=212.219.56.134 DST=xxx.xxx.xxx.xxx LEN=144 TOS=0x00 PREC=0x00 TTL=47 ID=24833 DF PROTO=TCP SPT=21 DPT=4801 WINDOW=5840 RES=0x00 ACK PSH URGP=0
Nov 2 16:48:24 LVS-test kernel: [IPTABLES DROP]:IN=eth2 OUT= MAC=00:17:a4:15:1a:af:00:14:95:f2:0d:29:08:00 SRC=212.219.56.134 DST=xxx.xxx.xxx.xxx LEN=1500 TOS=0x00 PREC=0x00 TTL=47 ID=6061 DF PROTO=TCP SPT=46535 DPT=4834 WINDOW=5840 RES=0x00 ACK URGP=0
Nov 2 16:48:25 LVS-test kernel: [IPTABLES DROP]:IN=eth2 OUT= MAC=00:17:a4:15:1a:af:00:14:95:f2:0d:29:08:00 SRC=212.219.56.134 DST=xxx.xxx.xxx.xxx LEN=144 TOS=0x00 PREC=0x00 TTL=47 ID=21138 DF PROTO=TCP SPT=21 DPT=4746 WINDOW=5840 RES=0x00 ACK PSH URGP=0
log ip_conntrack 1 :
tcp 6 431645 ESTABLISHED src=192.168.0.250 dst=212.219.56.135 sport=4917 dport=21 packets=11 bytes=617 src=212.219.56.135 dst=xxx.xxx.xxx.xxx sport=21 dport=4917 packets=11 bytes=1092 [ASSURED] mark=0 use=2
tcp 6 432000 ESTABLISHED src=192.168.0.250 dst=212.219.56.135 sport=4918 dport=46299 packets=26638 bytes=1153444 src=212.219.56.135 dst=xxx.xxx.xxx.xxx sport=46299 dport=4918 packets=40924 bytes=61384548 [ASSURED] mark=0 use=1
log iptables 2 :
Nov 2 16:50:41 LVS-test kernel: [IPTABLES DROP]:IN=eth2 OUT= MAC=00:17:a4:15:1a:af:00:14:95:f2:0d:29:08:00 SRC=212.219.56.134 DST=xxx.xxx.xxx.xxx LEN=144 TOS=0x00 PREC=0x00 TTL=47 ID=15905 DF PROTO=TCP SPT=21 DPT=4833 WINDOW=5840 RES=0x00 ACK PSH URGP=0
Nov 2 16:50:47 LVS-test kernel: [IPTABLES DROP]:IN=eth2 OUT= MAC=00:17:a4:15:1a:af:00:14:95:f2:0d:29:08:00 SRC=212.219.56.134 DST=xxx.xxx.xxx.xxx LEN=144 TOS=0x00 PREC=0x00 TTL=47 ID=5693 DF PROTO=TCP SPT=21 DPT=4813 WINDOW=5840 RES=0x00 ACK PSH URGP=0
Nov 2 16:50:49 LVS-test kernel: [IPTABLES DROP]:IN=eth2 OUT= MAC=00:17:a4:15:1a:af:00:14:95:f2:0d:29:08:00 SRC=212.219.56.135 DST=xxx.xxx.xxx.xxx LEN=1500 TOS=0x00 PREC=0x00 TTL=47 ID=37982 DF PROTO=TCP SPT=46299 DPT=4918 WINDOW=5840 RES=0x00 ACK URGP=0
Nov 2 16:50:52 LVS-test kernel: [IPTABLES DROP]:IN=eth0 OUT=eth2 SRC=192.168.0.250 DST=212.219.56.135 LEN=40 TOS=0x00 PREC=0x00 TTL=127 ID=38492 DF PROTO=TCP SPT=4918 DPT=46299 WINDOW=65535 RES=0x00 ACK FIN URGP=0
Nov 2 16:50:59 LVS-test kernel: [IPTABLES DROP]:IN=eth2 OUT= MAC=00:17:a4:15:1a:af:00:14:95:f2:0d:29:08:00 SRC=212.219.56.134 DST=xxx.xxx.xxx.xxx LEN=144 TOS=0x00 PREC=0x00 TTL=47 ID=15906 DF PROTO=TCP SPT=21 DPT=4833 WINDOW=5840 RES=0x00 ACK PSH URGP=0
Nov 2 16:51:35 LVS-test kernel: [IPTABLES DROP]:IN=eth2 OUT= MAC=00:17:a4:15:1a:af:00:14:95:f2:0d:29:08:00 SRC=212.219.56.134 DST=xxx.xxx.xxx.xxx LEN=144 TOS=0x00 PREC=0x00 TTL=47 ID=15907 DF PROTO=TCP SPT=21 DPT=4833 WINDOW=5840 RES=0x00 ACK PSH URGP=0
Nov 2 16:51:52 LVS-test kernel: [IPTABLES DROP]:IN=eth2 OUT= MAC=00:17:a4:15:1a:af:00:14:95:f2:0d:29:08:00 SRC=212.219.56.134 DST=xxx.xxx.xxx.xxx LEN=1500 TOS=0x00 PREC=0x00 TTL=47 ID=55202 DF PROTO=TCP SPT=45432 DPT=4873 WINDOW=5840 RES=0x00 ACK URGP=0
log ip_conntrack 2 :
tcp 6 431856 ESTABLISHED src=192.168.0.250 dst=212.219.56.135 sport=4962 dport=21 packets=11 bytes=617 src=212.219.56.135 dst=xxx.xxx.xxx.xxx sport=21 dport=4962 packets=11 bytes=1092 [ASSURED] mark=0 use=2
tcp 6 431999 ESTABLISHED src=192.168.0.250 dst=212.219.56.135 sport=4963 dport=60324 packets=10416 bytes=453104 src=212.219.56.135 dst=xxx.xxx.xxx.xxx sport=60324 dport=4963 packets=16011 bytes=24015048 [ASSURED] mark=0 use=1
En espérant que ça aide.
Merci d'avance pour vos lumières.