Lo scopo di questo post è di raccogliere una serie di esperimenti fatti con VB per creare una lan interamente virtualizzata.
Se si dispone di un pc abbastanza "carrozzato" (difficile in questi giorni trovare un pc con meno di 4GB di ram e qualche centinaia di GB di spazio disco) è possibile creare una lan composta da 4-5 computer virtuali.
L'idea generale è quella di realizzare una lan secondo lo schema sotto riportato:

Le macchine virtuali da creare sono quindi quattro: un firewall, due nodi per il cluster e una SAN.
Se non è chiaro cosa sia un cluster si può avere una idea leggendo qui, invece per una descrizione sulla SAN si può leggere qui.
Come dicevo in precedenza useremo VirtualBox per questo esperimento. Conviene, per velocizzare le operazioni, costruire un sistema base che poi personalizzeremo a seconda delle esigenze. Questo ci permette di dover installare una sola volta il sistema operativo.
Creiamo una macchina virtuale di base, potete impostare a piacimento i parametri. L'importante è dare un nome al disco fisso che sia facilmente identificabile (una buona idea ptrebbe essere template.vdi :) ).Tale disco sarà utilizzato per produrre tutti i dischi di avvio dei vari sistemi sulla lan.Dopo aver configurato tutti i parametri della macchina virtuale nel riepilogo dovreste avere una cosa del genere:

accendiamo la macchina "template", installiamo debian lenny e quindi spegnamo la macchina virtuale.Siccome il disco di questa macchina ci servirà per costruire i dischi di tutti gli altri sistemi è bene rimanere, nella fase di installazione, il più generico possibile. Quindi imposteremo un hostname generico, tipo "template" ed utilizzeremo una sola scheda di rete, connessa ad internet, che in questa fase ci serve tanto per l'installazione che per l'aggiornamento del sistema.
Importante prima di procedere con i passi successivi bisogna rimuovere, dal gestore supporti virtuali, il disco template (nel mio caso template.vdi):

Selezionare il disco da rilasciare, premere "Rilascia" e confermare nella schermata successiva. Selezionare nuovamente il disco, che ora risulterà Connesso a: Non Connesso e premere "Rimuovi". Confermare con "Rimuovi" nella schermata successiva e "Mantieni" nell'ultima schermata. Questo permette di avere il disco tolto dal controllo del gestore supporti virtuali, ma di non cancellare il disco fisicamente
Diversamente da quanto avviene con vmware non basta copiare il disco e rinominarlo per importarlo su una macchina virtuale. Bisogna utilizzare il seguente comando:
Sistemi HOST windows:
C:\Program Files\Sun\VirtualBox>VBoxManage.exe clonevdi template.vdi fw-boot-disk.vdi
VirtualBox Command Line Management Interface Version 3.0.6
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: f548d710-fcea-4118-8353-e21cf6794de6
Sistemi HOST linux
domenico@cerbero:~$ VBoxManage clonevdi template.vdi /vm/rebeldia-lab.local/fw-boot-disk.vdi
VirtualBox Command Line Management Interface Version 3.0.6
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: 1d8d923e-e42a-4b82-819c-f3ec285ffe5b
C:\Program Files\Sun\VirtualBox>VBoxManage.exe clonevdi template.vdi fw-boot-disk.vdi
VirtualBox Command Line Management Interface Version 3.0.6
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: f548d710-fcea-4118-8353-e21cf6794de6
Sistemi HOST linux
domenico@cerbero:~$ VBoxManage clonevdi template.vdi /vm/rebeldia-lab.local/fw-boot-disk.vdi
VirtualBox Command Line Management Interface Version 3.0.6
(C) 2005-2009 Sun Microsystems, Inc.
All rights reserved.
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: 1d8d923e-e42a-4b82-819c-f3ec285ffe5b
Analogamente vengono clonati i dischi di boot per il cluster (Consiglierei, per motivi di opportunità e di tempo, di non creare entrambi i dischi per i due nodi del cluster. Conviene farne uno solo e poi, dopo i vari aggiustamenti, clonare quello in modo da avere il sistema sui due nodi del cluster assolutamente allineato) e per la SAN.
Importiamo i vari dischi creati all'interno del gestore dei supporti virtuali, premendo il pulsante "Aggiungi" e selezionando, quindi, i dischi da porre sotto il controllo del gestore.
A questo punto creiamo le macchine virtuali che faranno parte della nostra lan, avendo cura di impostare, quando richiesto, i dischi clonati in precedenza. Verifichiamo che tutto funzioni accendendoli (uno alla volta o tutti assieme, in questa fase non importa). Se tutti i sistemi caricano il sistema operativo correttamente si può passare al punto successivo. Spegniamo tutti i sistemi virtuali e iniziamo a riconfigurare, prima però è necessario avere ben chiaro come deve essere la configurazione globale della lan. Abbiamo necessità di una lan di gestione, utilizzando la quale si riesca ad accedere ai sistemi, una lan privata per il cluster e una privata per la SAN. Lo specchietto seguente riporta le varie esigenze:
Lan di gestione: 10.100.100.0/24 (lan-net)
Lan del cluster: 192.168.200.0/24 (cluster-net)
Lan della SAN: 10.120.32.0/24 (san-net)
Lan del cluster: 192.168.200.0/24 (cluster-net)
Lan della SAN: 10.120.32.0/24 (san-net)
1) Il Firewall
Rispetto al template il firewall necessita di una scheda di rete aggiuntiva. La prima scheda sarà attestata sulla rete esterna (WAN) ed avrà l'ip assegnato tramite DHCP. La seconda scheda sarà invece sulla lan di gestione, avrà ip 10.100.100.1
Passiamo a modificare la configurazione della macchina virtuale. Aggiungiamo una scheda di rete. La prima interfaccia (WAN) sarà in bridge su una scheda di rete del sistema host che possa essere raggiunta dal DHCP server. La secondo interfaccia sarà, invece, dichiarata come connessa alla rete interna lan-net:

Premendo il pulsantino indicato dalla freccia si apre una finestra che contiene il MAC per la scheda.
Prendere nota del mac
Questa dichiarazione fa si che la scheda possa essere vista solo dagli altri appartenenti alla rete interna lan-net.
Anche per questa scheda bisogna annotare il mac
I mac per le due schede sono (le lettere in minuscolo):
Scheda 1 = eth0 = 080027786f18 che, nella notazione normale diventa, 08:00:27:78:6f:18
Scheda 2 = eth1 = 0800277cbd05 e cioè 08:00:27:7c:bd:05
Regoliamo, ora, a nostro piacimento la memoria da assegnare alla macchina virtuale. E' bene tenere conto che la somma della memoria virtuale di tutti i sistemi non dovrebbe andare oltre la metà (i più avventurosi dicono non oltre i 2/3) della ram totale del sistema. Impostati tutti i parametri avviamo il firewall
Chiaramente, siccome tutti i dischi sono clonati da un master avremo il medesimo hostname, le medesime regole udev, le medesime chiavi ssh...
Bisogna dunque effettuare le seguenti modifiche:
modificare il file /etc/hostname inserendo il nome del server (nel mio caso fw). La modifica avrà effetto al boot successivo.
Modificare il file /etc/hosts inserendo il nome dell'host e l'eventuale dominio.
fw:~# cat /etc/hosts
127.0.0.1 localhost
10.100.100.1 fw.rebeldia-lab.local fw
Rigenerare le chiavi rsa e dsa:127.0.0.1 localhost
10.100.100.1 fw.rebeldia-lab.local fw
fw:~# rm /etc/ssh/ssh_host_*
fw:~# dpkg-reconfigure openssh-server
Creating SSH2 RSA key; this may take some time ...
Creating SSH2 DSA key; this may take some time ...
Restarting OpenBSD Secure Shell server: sshd.
Assegnare staticamente i nomi delle nic.fw:~# dpkg-reconfigure openssh-server
Creating SSH2 RSA key; this may take some time ...
Creating SSH2 DSA key; this may take some time ...
Restarting OpenBSD Secure Shell server: sshd.
Al boot, oppure quando si connette una periferica al sistema viene creato un nodo nell'alberatura di /dev per ogni periferica. Udev è il tool di sistema che si occupa di questo. Anche per le schede di rete il meccanismo è analogo. Il file /etc/udev/rules.d/70-persistent-net.rules (su debian, in linea di massima il nome del file da modificare contiene persistent-net.rules) contiene le regole statiche per la creazione dei device ethX (ma, come si vede sotto, non è obbligatorio chiamare le schede ethX). Per quanto non dovrebbe variare l'ordine delle interfacce ethX (ovvero ciò che era eth0 all'installazione rimane eth0 per sempre) in alcuni casi può succedere che venga modificato l'ordine delle schede. Questo può essere un problema, specie se si è configurato il sistema per assegnare staticamente un ip ad una determinata scheda.
Ad esempio nel caso del mio fw il file /etc/udev/rules.d/70-persistent-net.rules contiene:
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:78:6f:18", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
# PCI device 0x1022:0x2000 (pcnet32)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:7c:bd:05", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="lan0"
In questo caso, invece di chiamare la scheda di rete sulla lan-net l'ho chiamata lan0.# PCI device 0x1022:0x2000 (pcnet32)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="08:00:27:7c:bd:05", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="lan0"
Basta quindi inserire il mac address corretto per ciascuna interfaccia. Questa operazione è piuttosto importante in quanto tutti sistemi sono cloni di una unica macchina per cui avranno registrate le interfacce del template in posizione eth0, ad esempio. Per cui quando voi create la nuova macchina virtuale avrete delle schede con, ovviamente, mac diversi e quindi sul sistema troverete eth1 dove vi attendevate eth0.
IMPORTANTE: ogni regola (nell'esempio ci sono due regole) deve stare su una linea unica, senza a capo.
Per impostare gli ip statici bisogna editare /etc/network/interfaces inserendo e/o modificandone il contenuto. Sempre nel mio sistema:
auto lo
iface lo inet loopback
# eth0: scheda sulla wan
allow-hotplug eth0
iface eth0 inet dhcp
# lan0: scheda verso la lan
allow-hotplug lan0
auto lan0
iface lan0 inet static
address 10.100.100.1
netmask 255.255.255.0
broadcast 10.100.100.255
network 10.100.100.0
A questo punto un bel reboot per vedere se funziona tutto. Se la macchina torna su :) verifichiamo che le modifiche siano state acquisite.iface lo inet loopback
# eth0: scheda sulla wan
allow-hotplug eth0
iface eth0 inet dhcp
# lan0: scheda verso la lan
allow-hotplug lan0
auto lan0
iface lan0 inet static
address 10.100.100.1
netmask 255.255.255.0
broadcast 10.100.100.255
network 10.100.100.0
fw:~# ifconfig -a
eth0 Link encap:Ethernet HWaddr 08:00:27:78:6f:18
inet addr:10.19.74.73 Bcast:10.19.74.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe78:6f18/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:383 errors:0 dropped:0 overruns:0 frame:0
TX packets:208 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:33317 (32.5 KiB) TX bytes:30148 (29.4 KiB)
Interrupt:11 Base address:0xd020
lan0 Link encap:Ethernet HWaddr 08:00:27:7c:bd:05
inet addr:10.100.100.1 Bcast:10.100.100.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe7c:bd05/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:468 (468.0 B)
Interrupt:10 Base address:0xd240
Tutte le modifiche sono state correttamente implementate.eth0 Link encap:Ethernet HWaddr 08:00:27:78:6f:18
inet addr:10.19.74.73 Bcast:10.19.74.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe78:6f18/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:383 errors:0 dropped:0 overruns:0 frame:0
TX packets:208 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:33317 (32.5 KiB) TX bytes:30148 (29.4 KiB)
Interrupt:11 Base address:0xd020
lan0 Link encap:Ethernet HWaddr 08:00:27:7c:bd:05
inet addr:10.100.100.1 Bcast:10.100.100.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe7c:bd05/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:468 (468.0 B)
Interrupt:10 Base address:0xd240
Adesso il sistema è pronto per diventare il nostro firewall. Data la natura del post io effettuerò una configurazione di base, chi vuole la può comodamente ampliare :)
In questo caso utilizzo FwBuilder per creare lo script di configurazione iptables:
#!/bin/sh
#
# This is automatically generated file. DO NOT MODIFY !
#
# Firewall Builder fwb_ipt v2.1.19-1
#
#
#
#
PATH="/sbin:/usr/sbin:/bin:/usr/bin:${PATH}"
export PATH
LSMOD="/sbin/lsmod"
MODPROBE="/sbin/modprobe"
IPTABLES="/sbin/iptables"
IPTABLES_RESTORE="/sbin/iptables-restore"
IP="/sbin/ip"
LOGGER="/usr/bin/logger"
#
# Prolog script
#
#
# End of prolog script
#
log() {
echo "$1"
test -x "$LOGGER" && $LOGGER -p info "$1"
}
check_file() {
test -r "$2" || {
echo "Can not find file $2 referenced by AddressTable object $1"
exit 1
}
}
va_num=1
add_addr() {
addr=$1
nm=$2
dev=$3
type=""
aadd=""
L=`$IP -4 link ls $dev | head -n1`
if test -n "$L"; then
OIFS=$IFS
IFS=" /:,<" set $L type=$4 IFS=$OIFS if test "$type" = "NO-CARRIER"; then type=$5 fi L=`$IP -4 addr ls $dev to $addr | grep inet | grep -v :` if test -n "$L"; then OIFS=$IFS IFS=" /" set $L aadd=$2 IFS=$OIFS fi fi if test -z "$aadd"; then if test "$type" = "POINTOPOINT"; then $IP -4 addr add $addr dev $dev scope global label $dev:FWB${va_num} va_num=`expr $va_num + 1` fi if test "$type" = "BROADCAST"; then $IP -4 addr add $addr/$nm dev $dev brd + scope global label $dev:FWB${va_num} va_num=`expr $va_num + 1` fi fi } getInterfaceVarName() { echo $1 | sed 's/\./_/' } getaddr() { dev=$1 name=$2 L=`$IP -4 addr show dev $dev | grep inet | grep -v :` test -z "$L" && { eval "$name=''" return } OIFS=$IFS IFS=" /" set $L eval "$name=$2" IFS=$OIFS } getinterfaces() { NAME=$1 $IP link show | grep ": $NAME" | while read L; do OIFS=$IFS IFS=" :" set $L IFS=$OIFS echo $2 done } # increment ip address incaddr() { n1=$4 n2=$3 n3=$2 n4=$1 vn1=`eval "echo \\$$n1"` R=`expr $vn1 \< 255` if test $R = "1"; then eval "$n1=`expr $vn1 + 1`" else eval "$n1=0" incaddr XX $n4 $n3 $n2 fi } if $IP link ls >/dev/null 2>&1; then
echo;
else
echo "iproute not found"
exit 1
fi
MODULES_DIR="/lib/modules/`uname -r`/kernel/net/"
MODULES=`find $MODULES_DIR -name '*conntrack*'|sed -e 's/^.*\///' -e 's/\([^\.]\)\..*/\1/'`
MODULES="$MODULES `find $MODULES_DIR -name '*nat*'|sed -e 's/^.*\///' -e 's/\([^\.]\)\..*/\1/'`"
for module in $MODULES; do
if $LSMOD | grep ${module} >/dev/null; then continue; fi
$MODPROBE ${module} || exit 1
done
# Using 0 address table files
INTERFACES="eth0 lan0 lo "
for i in $INTERFACES ; do
$IP link show "$i" > /dev/null 2>&1 || {
log "Interface $i does not exist"
exit 1
}
done
# Configure interfaces
$IP -4 neigh flush dev lan0 >/dev/null 2>&1
$IP -4 addr flush dev lan0 secondary label "lan0:FWB*" >/dev/null 2>&1
add_addr 10.100.100.1 24 lan0
$IP link set lan0 up
add_addr 127.0.0.1 8 lo
$IP link set lo up
getaddr eth0 i_eth0
# Add virtual addresses for NAT rules
log 'Activating firewall script generated Thu Sep 17 23:04:29 2009 by domenico'
$IPTABLES -P OUTPUT DROP
$IPTABLES -P INPUT DROP
$IPTABLES -P FORWARD DROP
ip6tables -L -n > /dev/null 2>&1 && {
ip6tables -P OUTPUT DROP
ip6tables -P INPUT DROP
ip6tables -P FORWARD DROP
ip6tables -A INPUT -i lo -j ACCEPT
ip6tables -A OUTPUT -o lo -j ACCEPT
}
cat /proc/net/ip_tables_names | while read table; do
$IPTABLES -t $table -L -n | while read c chain rest; do
if test "X$c" = "XChain" ; then
$IPTABLES -t $table -F $chain
fi
done
$IPTABLES -t $table -X
done
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
#
# Rule 0 (NAT)
#
echo "Rule 0 (NAT)"
#
#
$IPTABLES -t nat -A POSTROUTING -o eth0 -s 10.100.100.0/24 -j MASQUERADE
#
# Rule 0 (eth0)
#
echo "Rule 0 (eth0)"
#
# anti spoofing rule
#
$IPTABLES -N In_RULE_0
test -n "$i_eth0" && $IPTABLES -A INPUT -i eth0 -s $i_eth0 -m state --state NEW -j In_RULE_0
$IPTABLES -A INPUT -i eth0 -s 10.100.100.1 -m state --state NEW -j In_RULE_0
$IPTABLES -A INPUT -i eth0 -s 10.100.100.0/24 -m state --state NEW -j In_RULE_0
test -n "$i_eth0" && $IPTABLES -A FORWARD -i eth0 -s $i_eth0 -m state --state NEW -j In_RULE_0
$IPTABLES -A FORWARD -i eth0 -s 10.100.100.1 -m state --state NEW -j In_RULE_0
$IPTABLES -A FORWARD -i eth0 -s 10.100.100.0/24 -m state --state NEW -j In_RULE_0
$IPTABLES -A In_RULE_0 -j LOG --log-level info --log-prefix "RULE 0 -- DENY "
$IPTABLES -A In_RULE_0 -j DROP
#
# Rule 1 (lo)
#
echo "Rule 1 (lo)"
#
#
#
$IPTABLES -A INPUT -i lo -m state --state NEW -j ACCEPT
$IPTABLES -A OUTPUT -o lo -m state --state NEW -j ACCEPT
#
# Rule 2 (global)
#
echo "Rule 2 (global)"
#
# SSH Access to firewall is permitted
# only from internal network
#
$IPTABLES -N Cid4AB2AEFA15114.0
$IPTABLES -A OUTPUT -p tcp -m tcp --dport 22 -m state --state NEW -j Cid4AB2AEFA15114.0
test -n "$i_eth0" && $IPTABLES -A Cid4AB2AEFA15114.0 -d $i_eth0 -j ACCEPT
$IPTABLES -A Cid4AB2AEFA15114.0 -d 10.100.100.1 -j ACCEPT
$IPTABLES -A INPUT -p tcp -m tcp --dport 22 -m state --state NEW -j ACCEPT
#
# Rule 3 (global)
#
echo "Rule 3 (global)"
#
# Firewall uses one of the machines
# on internal network for DNS
#
$IPTABLES -N RULE_3
$IPTABLES -A OUTPUT -p tcp -m tcp -d 10.100.100.0/24 --dport 53 -m state --state NEW -j RULE_3
$IPTABLES -A OUTPUT -p udp -m udp -d 10.100.100.0/24 --dport 53 -m state --state NEW -j RULE_3
$IPTABLES -A RULE_3 -j LOG --log-level info --log-prefix "RULE 3 -- ACCEPT "
$IPTABLES -A RULE_3 -j ACCEPT
#
# Rule 4 (global)
#
echo "Rule 4 (global)"
#
# All other attempts to connect to
# the firewall are denied and logged
#
$IPTABLES -N RULE_4
test -n "$i_eth0" && $IPTABLES -A OUTPUT -d $i_eth0 -m state --state NEW -j RULE_4
$IPTABLES -A OUTPUT -d 10.100.100.1 -m state --state NEW -j RULE_4
$IPTABLES -A INPUT -m state --state NEW -j RULE_4
$IPTABLES -A RULE_4 -j LOG --log-level info --log-prefix "RULE 4 -- DENY "
$IPTABLES -A RULE_4 -j DROP
#
# Rule 5 (global)
#
echo "Rule 5 (global)"
#
#
#
$IPTABLES -A INPUT -s 10.100.100.0/24 -m state --state NEW -j ACCEPT
$IPTABLES -A OUTPUT -s 10.100.100.0/24 -m state --state NEW -j ACCEPT
$IPTABLES -A FORWARD -s 10.100.100.0/24 -m state --state NEW -j ACCEPT
#
# Rule 6 (global)
#
echo "Rule 6 (global)"
#
#
#
$IPTABLES -N RULE_6
$IPTABLES -A OUTPUT -m state --state NEW -j RULE_6
$IPTABLES -A INPUT -m state --state NEW -j RULE_6
$IPTABLES -A FORWARD -m state --state NEW -j RULE_6
$IPTABLES -A RULE_6 -j LOG --log-level info --log-prefix "RULE 6 -- DENY "
$IPTABLES -A RULE_6 -j DROP
#
#
echo 1 > /proc/sys/net/ipv4/ip_forward
#
# Epilog script
#
# End of epilog script
#
Questo script deve poi essere invocato in fase di boot. Per debian si può far richiamare all'interno di /etc/rc.local#
# This is automatically generated file. DO NOT MODIFY !
#
# Firewall Builder fwb_ipt v2.1.19-1
#
#
#
#
PATH="/sbin:/usr/sbin:/bin:/usr/bin:${PATH}"
export PATH
LSMOD="/sbin/lsmod"
MODPROBE="/sbin/modprobe"
IPTABLES="/sbin/iptables"
IPTABLES_RESTORE="/sbin/iptables-restore"
IP="/sbin/ip"
LOGGER="/usr/bin/logger"
#
# Prolog script
#
#
# End of prolog script
#
log() {
echo "$1"
test -x "$LOGGER" && $LOGGER -p info "$1"
}
check_file() {
test -r "$2" || {
echo "Can not find file $2 referenced by AddressTable object $1"
exit 1
}
}
va_num=1
add_addr() {
addr=$1
nm=$2
dev=$3
type=""
aadd=""
L=`$IP -4 link ls $dev | head -n1`
if test -n "$L"; then
OIFS=$IFS
IFS=" /:,<" set $L type=$4 IFS=$OIFS if test "$type" = "NO-CARRIER"; then type=$5 fi L=`$IP -4 addr ls $dev to $addr | grep inet | grep -v :` if test -n "$L"; then OIFS=$IFS IFS=" /" set $L aadd=$2 IFS=$OIFS fi fi if test -z "$aadd"; then if test "$type" = "POINTOPOINT"; then $IP -4 addr add $addr dev $dev scope global label $dev:FWB${va_num} va_num=`expr $va_num + 1` fi if test "$type" = "BROADCAST"; then $IP -4 addr add $addr/$nm dev $dev brd + scope global label $dev:FWB${va_num} va_num=`expr $va_num + 1` fi fi } getInterfaceVarName() { echo $1 | sed 's/\./_/' } getaddr() { dev=$1 name=$2 L=`$IP -4 addr show dev $dev | grep inet | grep -v :` test -z "$L" && { eval "$name=''" return } OIFS=$IFS IFS=" /" set $L eval "$name=$2" IFS=$OIFS } getinterfaces() { NAME=$1 $IP link show | grep ": $NAME" | while read L; do OIFS=$IFS IFS=" :" set $L IFS=$OIFS echo $2 done } # increment ip address incaddr() { n1=$4 n2=$3 n3=$2 n4=$1 vn1=`eval "echo \\$$n1"` R=`expr $vn1 \< 255` if test $R = "1"; then eval "$n1=`expr $vn1 + 1`" else eval "$n1=0" incaddr XX $n4 $n3 $n2 fi } if $IP link ls >/dev/null 2>&1; then
echo;
else
echo "iproute not found"
exit 1
fi
MODULES_DIR="/lib/modules/`uname -r`/kernel/net/"
MODULES=`find $MODULES_DIR -name '*conntrack*'|sed -e 's/^.*\///' -e 's/\([^\.]\)\..*/\1/'`
MODULES="$MODULES `find $MODULES_DIR -name '*nat*'|sed -e 's/^.*\///' -e 's/\([^\.]\)\..*/\1/'`"
for module in $MODULES; do
if $LSMOD | grep ${module} >/dev/null; then continue; fi
$MODPROBE ${module} || exit 1
done
# Using 0 address table files
INTERFACES="eth0 lan0 lo "
for i in $INTERFACES ; do
$IP link show "$i" > /dev/null 2>&1 || {
log "Interface $i does not exist"
exit 1
}
done
# Configure interfaces
$IP -4 neigh flush dev lan0 >/dev/null 2>&1
$IP -4 addr flush dev lan0 secondary label "lan0:FWB*" >/dev/null 2>&1
add_addr 10.100.100.1 24 lan0
$IP link set lan0 up
add_addr 127.0.0.1 8 lo
$IP link set lo up
getaddr eth0 i_eth0
# Add virtual addresses for NAT rules
log 'Activating firewall script generated Thu Sep 17 23:04:29 2009 by domenico'
$IPTABLES -P OUTPUT DROP
$IPTABLES -P INPUT DROP
$IPTABLES -P FORWARD DROP
ip6tables -L -n > /dev/null 2>&1 && {
ip6tables -P OUTPUT DROP
ip6tables -P INPUT DROP
ip6tables -P FORWARD DROP
ip6tables -A INPUT -i lo -j ACCEPT
ip6tables -A OUTPUT -o lo -j ACCEPT
}
cat /proc/net/ip_tables_names | while read table; do
$IPTABLES -t $table -L -n | while read c chain rest; do
if test "X$c" = "XChain" ; then
$IPTABLES -t $table -F $chain
fi
done
$IPTABLES -t $table -X
done
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
#
# Rule 0 (NAT)
#
echo "Rule 0 (NAT)"
#
#
$IPTABLES -t nat -A POSTROUTING -o eth0 -s 10.100.100.0/24 -j MASQUERADE
#
# Rule 0 (eth0)
#
echo "Rule 0 (eth0)"
#
# anti spoofing rule
#
$IPTABLES -N In_RULE_0
test -n "$i_eth0" && $IPTABLES -A INPUT -i eth0 -s $i_eth0 -m state --state NEW -j In_RULE_0
$IPTABLES -A INPUT -i eth0 -s 10.100.100.1 -m state --state NEW -j In_RULE_0
$IPTABLES -A INPUT -i eth0 -s 10.100.100.0/24 -m state --state NEW -j In_RULE_0
test -n "$i_eth0" && $IPTABLES -A FORWARD -i eth0 -s $i_eth0 -m state --state NEW -j In_RULE_0
$IPTABLES -A FORWARD -i eth0 -s 10.100.100.1 -m state --state NEW -j In_RULE_0
$IPTABLES -A FORWARD -i eth0 -s 10.100.100.0/24 -m state --state NEW -j In_RULE_0
$IPTABLES -A In_RULE_0 -j LOG --log-level info --log-prefix "RULE 0 -- DENY "
$IPTABLES -A In_RULE_0 -j DROP
#
# Rule 1 (lo)
#
echo "Rule 1 (lo)"
#
#
#
$IPTABLES -A INPUT -i lo -m state --state NEW -j ACCEPT
$IPTABLES -A OUTPUT -o lo -m state --state NEW -j ACCEPT
#
# Rule 2 (global)
#
echo "Rule 2 (global)"
#
# SSH Access to firewall is permitted
# only from internal network
#
$IPTABLES -N Cid4AB2AEFA15114.0
$IPTABLES -A OUTPUT -p tcp -m tcp --dport 22 -m state --state NEW -j Cid4AB2AEFA15114.0
test -n "$i_eth0" && $IPTABLES -A Cid4AB2AEFA15114.0 -d $i_eth0 -j ACCEPT
$IPTABLES -A Cid4AB2AEFA15114.0 -d 10.100.100.1 -j ACCEPT
$IPTABLES -A INPUT -p tcp -m tcp --dport 22 -m state --state NEW -j ACCEPT
#
# Rule 3 (global)
#
echo "Rule 3 (global)"
#
# Firewall uses one of the machines
# on internal network for DNS
#
$IPTABLES -N RULE_3
$IPTABLES -A OUTPUT -p tcp -m tcp -d 10.100.100.0/24 --dport 53 -m state --state NEW -j RULE_3
$IPTABLES -A OUTPUT -p udp -m udp -d 10.100.100.0/24 --dport 53 -m state --state NEW -j RULE_3
$IPTABLES -A RULE_3 -j LOG --log-level info --log-prefix "RULE 3 -- ACCEPT "
$IPTABLES -A RULE_3 -j ACCEPT
#
# Rule 4 (global)
#
echo "Rule 4 (global)"
#
# All other attempts to connect to
# the firewall are denied and logged
#
$IPTABLES -N RULE_4
test -n "$i_eth0" && $IPTABLES -A OUTPUT -d $i_eth0 -m state --state NEW -j RULE_4
$IPTABLES -A OUTPUT -d 10.100.100.1 -m state --state NEW -j RULE_4
$IPTABLES -A INPUT -m state --state NEW -j RULE_4
$IPTABLES -A RULE_4 -j LOG --log-level info --log-prefix "RULE 4 -- DENY "
$IPTABLES -A RULE_4 -j DROP
#
# Rule 5 (global)
#
echo "Rule 5 (global)"
#
#
#
$IPTABLES -A INPUT -s 10.100.100.0/24 -m state --state NEW -j ACCEPT
$IPTABLES -A OUTPUT -s 10.100.100.0/24 -m state --state NEW -j ACCEPT
$IPTABLES -A FORWARD -s 10.100.100.0/24 -m state --state NEW -j ACCEPT
#
# Rule 6 (global)
#
echo "Rule 6 (global)"
#
#
#
$IPTABLES -N RULE_6
$IPTABLES -A OUTPUT -m state --state NEW -j RULE_6
$IPTABLES -A INPUT -m state --state NEW -j RULE_6
$IPTABLES -A FORWARD -m state --state NEW -j RULE_6
$IPTABLES -A RULE_6 -j LOG --log-level info --log-prefix "RULE 6 -- DENY "
$IPTABLES -A RULE_6 -j DROP
#
#
echo 1 > /proc/sys/net/ipv4/ip_forward
#
# Epilog script
#
# End of epilog script
#
Alternativamente si può usare arno-iptables-firewall, nella modalità che ho già illustrato in un altro post.
Nel prossimo post la configurazione della SAN e del cluster.
Postate nei commenti se avete considerazioni o domande
domenico .-. www.mentularia.info




2 commenti:
Altro che inutile! Lo cercavo da un po'. Una lan di macchine virtuali è molto utile; permette ad esempio di risparmiare soldi nel testing del funzionamento di una applicazione web configurata per una DMZ. Perciò ti ringrazio per l'articolo, che in realtà sa pochissimo di mentulata ;)
Grazie Carmelo :) appena ho un po' di tempo posto anche la terza parte con la realizzazione del cluster.
Posta un commento