simple iptables based packet limiter to deflect DoS attacks on our clients
[svn/Prometheus-QoS/.git] / optional-tools / make-iptables-restore
index 449534f53e1deb68c448a48ef6bb6374c26e1012..c4c0ce481ddfd84c77b0c42515c55121a11d6ff6 100755 (executable)
@@ -2,6 +2,10 @@
 # $Id$
 iptables="/sbin/iptables"
 iptablesrestore="/sbin/iptables-restore"
+ifconfig="/sbin/ifconfig"
+grep="/bin/grep"
+cut="/usr/bin/cut"
+ipcalc="/usr/bin/ipcalc"
 
 #pimp files must be generated by optional-tools/make-pimp utility
 pimp_2way_nat="/dev/shm/pimp-2way-nat.tmp"
@@ -21,33 +25,6 @@ pubfirstbitmask="26"
 pubsecondbitmask="29"
 chaintrack="_"
 
-# ===============================================================
-#  ipcalc rewrite
-# ===============================================================
-
-gen_ipt_string() {
-eval `echo $1 | awk -F\/ '{printf "IP=%s; CIDRMASK=%s; \n", $1, $2}'`
-
-MASK_FULL_OCTETS=$(($CIDRMASK/8))
-MASK_PART_OCTETS=$(($CIDRMASK%8))
-for i in `seq 0 3`; do 
-       if [ "$i" -lt "$MASK_FULL_OCTETS" ]; then
-               MASK+="255"
-       elif [ "$i" -eq "$MASK_FULL_OCTETS" ]; then
-               MASK+=$((256 - 2**(8-$MASK_PART_OCTETS)))
-       else
-               MASK+="0"
-       fi
-       [ "$i" -lt "3" ] && MASK+="."
-done
-
-eval `echo $IP | awk -F\. '{printf "IPBYTE1=%s; IPBYTE2=%s; IPBYTE3=%s; IPBYTE4=%s; \n", $1, $2, $3, $4}'`
-eval `echo $MASK | awk -F\. '{printf "MASKBYTE1=%s; MASKBYTE2=%s; MASKBYTE3=%s; MASKBYTE4=%s; \n", $1, $2, $3, $4}'`
-
-IPT_STRING="$(($IPBYTE1 & $MASKBYTE1))_$(($IPBYTE2 & $MASKBYTE2))_$(($IPBYTE3 & $MASKBYTE3))_$(($IPBYTE4 & $MASKBYTE4))_$CIDRMASK"
-echo $IPT_STRING
-}
-
 echo "*nat" > $restoretmp
 echo ":PREROUTING ACCEPT [0:0]" >> $restoretmp
 echo ":POSTROUTING ACCEPT [0:0]" >> $restoretmp
@@ -58,20 +35,20 @@ echo ":OUTPUT ACCEPT [0:0]" >> $restoretmp
 # ===============================================================
 echo -n "Generating new iptables-restore data - two way SNAT/DNAT "
 
-
-while read LINE; do
eval `echo -e $LINE | awk '{printf "czfip=%s; pubip=%s\n", $1, $2}'`
- czffirstindex=priv_`gen_ipt_string $czfip/$czffirstbitmask`
- czfsecondindex=priv_`gen_ipt_string $czfip/$czfsecondbitmask`
- czfthirdindex=priv_`gen_ipt_string $czfip/$czfthirdbitmask`
- czffourthindex=priv_`gen_ipt_string $czfip/$czffourthbitmask`
- pubfirstindex=pub_`gen_ipt_string $pubip/$pubfirstbitmask`
- pubsecondindex=pub_`gen_ipt_string $pubip/$pubsecondbitmask`
+for czfip in `$grep -v ^# $pimp_2way_nat|$cut -f 1 -d " "`
+do
pubip=`$grep "$czfip " $pimp_2way_nat|$cut -f 2 -d " "`
+ czffirstindex=priv_`$ipcalc -n $czfip/$czffirstbitmask|$grep Network|$cut -f 4 -d \ |tr [./] _`
+ czfsecondindex=priv_`$ipcalc -n $czfip/$czfsecondbitmask|$grep Network|$cut -f 4 -d \ |tr [./] _`
+ czfthirdindex=priv_`$ipcalc -n $czfip/$czfthirdbitmask|$grep Network|$cut -f 4 -d \ |tr [./] _`
+ czffourthindex=priv_`$ipcalc -n $czfip/$czffourthbitmask|$grep Network|$cut -f 4 -d \ |tr [./] _`
+ pubfirstindex=pub_`$ipcalc -n $pubip/$pubfirstbitmask|$grep Network|$cut -f 4 -d \ |tr [./] _`
+ pubsecondindex=pub_`$ipcalc -n $pubip/$pubsecondbitmask|$grep Network|$cut -f 4 -d \ |tr [./] _`
 
  if ! [[ "$chaintrack" == *"$czffirstindex"* ]]
  then
   echo :$czffirstindex "- [0:0]" >> $restoretmp
-  s=`gen_ipt_string $czfip/$czffirstbitmask| sed 's/_[0-9]*//4; s/_/./g'`
+  s=`$ipcalc -n $czfip/$czffirstbitmask|$grep Network|$cut -f 4 -d \ `
   echo -A POSTROUTING -d ! 10.0.0.0/8 -s $s -o $wan1 -j $czffirstindex >> $restoretmp
   echo -A POSTROUTING -d ! 10.0.0.0/8 -s $s -o $wan2 -j $czffirstindex >> $restoretmp
   echo -A POSTROUTING -d ! 10.0.0.0/8 -s $s -o $wan3 -j $czffirstindex >> $restoretmp
@@ -82,7 +59,7 @@ while read LINE; do
  if ! [[ "$chaintrack" == *"$czfsecondindex"* ]]
  then
   echo :$czfsecondindex "- [0:0]" >> $restoretmp
-  s=`gen_ipt_string $czfip/$czfsecondbitmask| sed 's/_[0-9]*//4; s/_/./g'`
+  s=`$ipcalc -n $czfip/$czfsecondbitmask|$grep Network|$cut -f 4 -d \ `
   echo -A $czffirstindex -s $s -o $wan1 -j $czfsecondindex >> $restoretmp
   echo -A $czffirstindex -s $s -o $wan2 -j $czfsecondindex >> $restoretmp
   echo -A $czffirstindex -s $s -o $wan3 -j $czfsecondindex >> $restoretmp
@@ -93,7 +70,7 @@ while read LINE; do
  if ! [[ "$chaintrack" == *"$czfthirdindex"* ]]
  then
   echo :$czfthirdindex "- [0:0]" >> $restoretmp
-  s=`gen_ipt_string $czfip/$czfthirdbitmask| sed 's/_[0-9]*//4; s/_/./g'`
+  s=`$ipcalc -n $czfip/$czfthirdbitmask|$grep Network|$cut -f 4 -d \ `
   echo -A $czfsecondindex -s $s -o $wan1 -j $czfthirdindex >> $restoretmp
   echo -A $czfsecondindex -s $s -o $wan2 -j $czfthirdindex >> $restoretmp
   echo -A $czfsecondindex -s $s -o $wan3 -j $czfthirdindex >> $restoretmp
@@ -104,7 +81,7 @@ while read LINE; do
  if ! [[ "$chaintrack" == *"$czffourthindex"* ]]
  then
   echo :$czffourthindex "- [0:0]" >> $restoretmp
-  s=`gen_ipt_string $czfip/$czffourthbitmask| sed 's/_[0-9]*//4; s/_/./g'`
+  s=`$ipcalc -n $czfip/$czffourthbitmask|$grep Network|$cut -f 4 -d \ `
   echo -A $czfthirdindex -s $s -o $wan1 -j $czffourthindex >> $restoretmp
   echo -A $czfthirdindex -s $s -o $wan2 -j $czffourthindex >> $restoretmp
   echo -A $czfthirdindex -s $s -o $wan3 -j $czffourthindex >> $restoretmp
@@ -115,7 +92,7 @@ while read LINE; do
  if ! [[ "$chaintrack" == *"$pubfirstindex"* ]]
  then
   echo :$pubfirstindex "- [0:0]" >> $restoretmp
-  s=`gen_ipt_string $pubip/$pubfirstbitmask| sed 's/_[0-9]*//4; s/_/./g'`
+  s=`$ipcalc -n $pubip/$pubfirstbitmask|$grep Network|$cut -f 4 -d \ `
   echo -A PREROUTING -i $wan1 -d $s -j $pubfirstindex >> $restoretmp
   echo -A PREROUTING -i $wan2 -d $s -j $pubfirstindex >> $restoretmp
   echo -A PREROUTING -i $wan3 -d $s -j $pubfirstindex >> $restoretmp
@@ -126,7 +103,7 @@ while read LINE; do
  if ! [[ "$chaintrack" == *"$pubsecondindex"* ]]
  then
   echo :$pubsecondindex "- [0:0]" >> $restoretmp
-  s=`gen_ipt_string $pubip/$pubsecondbitmask| sed 's/_[0-9]*//4; s/_/./g'`
+  s=`$ipcalc -n $pubip/$pubsecondbitmask|$grep Network|$cut -f 4 -d \ `
   echo -A $pubfirstindex -i $wan1 -d $s -j $pubsecondindex >> $restoretmp
   echo -A $pubfirstindex -i $wan2 -d $s -j $pubsecondindex >> $restoretmp
   echo -A $pubfirstindex -i $wan3 -d $s -j $pubsecondindex >> $restoretmp
@@ -146,7 +123,7 @@ while read LINE; do
 
  echo -n .
 
-done < $pimp_2way_nat
+done
 echo " done."
 
 # ===============================================================
@@ -154,17 +131,18 @@ echo " done."
 # ===============================================================
 echo -n "Generating new iptables-restore data - one way SNAT "
 
-while read LINE; do
- eval `echo -e $LINE | awk '{printf "czfip=%s; pubip=%s\n", $1, $2}'`
- czffirstindex=priv_`gen_ipt_string $czfip/$czffirstbitmask`
- czfsecondindex=priv_`gen_ipt_string $czfip/$czfsecondbitmask`
- czfthirdindex=priv_`gen_ipt_string $czfip/$czfthirdbitmask`
- czffourthindex=priv_`gen_ipt_string $czfip/$czffourthbitmask`
+for czfip in `$grep -v ^# $pimp_snat|$cut -f 1 -d " "`
+do
+ pubip=`$grep "$czfip " $pimp_snat|$cut -f 2 -d " "`
+ czffirstindex=priv_`$ipcalc -n $czfip/$czffirstbitmask|$grep Network|$cut -f 4 -d \ |tr [./] _`
+ czfsecondindex=priv_`$ipcalc -n $czfip/$czfsecondbitmask|$grep Network|$cut -f 4 -d \ |tr [./] _`
+ czfthirdindex=priv_`$ipcalc -n $czfip/$czfthirdbitmask|$grep Network|$cut -f 4 -d \ |tr [./] _`
+ czffourthindex=priv_`$ipcalc -n $czfip/$czffourthbitmask|$grep Network|$cut -f 4 -d \ |tr [./] _`
 
  if ! [[ "$chaintrack" == *"$czffirstindex"* ]]
  then
   echo :$czffirstindex "- [0:0]" >> $restoretmp
-  s=`gen_ipt_string $czfip/$czffirstbitmask| sed 's/_[0-9]*//4; s/_/./g'`
+  s=`$ipcalc -n $czfip/$czffirstbitmask|$grep Network|$cut -f 4 -d \ `
   echo -A POSTROUTING -d ! 10.0.0.0/8 -s $s -o $wan1 -j $czffirstindex >> $restoretmp
   echo -A POSTROUTING -d ! 10.0.0.0/8 -s $s -o $wan2 -j $czffirstindex >> $restoretmp
   echo -A POSTROUTING -d ! 10.0.0.0/8 -s $s -o $wan3 -j $czffirstindex >> $restoretmp
@@ -175,7 +153,7 @@ while read LINE; do
  if ! [[ "$chaintrack" == *"$czfsecondindex"* ]]
  then
   echo :$czfsecondindex "- [0:0]" >> $restoretmp
-  s=`gen_ipt_string $czfip/$czfsecondbitmask| sed 's/_[0-9]*//4; s/_/./g'`
+  s=`$ipcalc -n $czfip/$czfsecondbitmask|$grep Network|$cut -f 4 -d \ `
   echo -A $czffirstindex -s $s -o $wan1 -j $czfsecondindex >> $restoretmp
   echo -A $czffirstindex -s $s -o $wan2 -j $czfsecondindex >> $restoretmp
   echo -A $czffirstindex -s $s -o $wan3 -j $czfsecondindex >> $restoretmp
@@ -186,7 +164,7 @@ while read LINE; do
  if ! [[ "$chaintrack" == *"$czfthirdindex"* ]]
  then
   echo :$czfthirdindex "- [0:0]" >> $restoretmp
-  s=`gen_ipt_string $czfip/$czfthirdbitmask| sed 's/_[0-9]*//4; s/_/./g'`
+  s=`$ipcalc -n $czfip/$czfthirdbitmask|$grep Network|$cut -f 4 -d \ `
   echo -A $czfsecondindex -s $s -o $wan1 -j $czfthirdindex >> $restoretmp
   echo -A $czfsecondindex -s $s -o $wan2 -j $czfthirdindex >> $restoretmp
   echo -A $czfsecondindex -s $s -o $wan3 -j $czfthirdindex >> $restoretmp
@@ -197,7 +175,7 @@ while read LINE; do
  if ! [[ "$chaintrack" == *"$czffourthindex"* ]]
  then
   echo :$czffourthindex "- [0:0]" >> $restoretmp
-  s=`gen_ipt_string $czfip/$czffourthbitmask| sed 's/_[0-9]*//4; s/_/./g'`
+  s=`$ipcalc -n $czfip/$czffourthbitmask|$grep Network|$cut -f 4 -d \ `
   echo -A $czfthirdindex -s $s -o $wan1 -j $czffourthindex >> $restoretmp
   echo -A $czfthirdindex -s $s -o $wan2 -j $czffourthindex >> $restoretmp
   echo -A $czfthirdindex -s $s -o $wan3 -j $czffourthindex >> $restoretmp
@@ -211,7 +189,7 @@ while read LINE; do
  echo -A $czffourthindex -s $czfip/32 -o $wan4 -j SNAT --to-source $pubip >> $restoretmp
 
  echo -n .
-done < $pimp_snat
+done
 echo " done."
 
 echo COMMIT >> $restoretmp
This page took 0.17959 seconds and 4 git commands to generate.