Skip to content

Commit 5485faa

Browse files
committed
CS-13500 Added hairpin NAT fix for static NAT
1 parent 751972a commit 5485faa

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

‎patches/systemvm/debian/config/root/firewall.sh‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,15 @@ doHairpinNat () {
5858
logger -t cloud "$(basename $0): create HairPin entry : public ip=$publicIp \
5959
instance ip=$guestVmIp proto=$proto portRange=$guestPort op=$op"
6060

61-
(sudo iptables -t nat $op PREROUTING -d $publicIp -i eth0 -p $prot --dport $port -j DNAT --to-destination $guestVmIp:$guestPort &>> $OUTFILE || [ "$op" == "-D" ]) &&
62-
(sudo iptables -t nat $op POSTROUTING -s $vrGuestIPNetwork -p $prot --dport $port -d $guestVmIp -j SNAT -o eth0 --to-source $vrGuestIP &>> $OUTFILE || [ "$op" == "-D" ])
61+
if [ "$prot" == "all" ]
62+
then
63+
logger -t cloud "creating hairpin nat rules for static nat"
64+
(sudo iptables -t nat $op PREROUTING -d $publicIp -i eth0 -j DNAT --to-destination $guestVmIp &>> $OUTFILE || [ "$op" == "-D" ]) &&
65+
(sudo iptables -t nat $op POSTROUTING -s $vrGuestIPNetwork -d $guestVmIp -j SNAT -o eth0 --to-source $vrGuestIP &>> $OUTFILE || [ "$op" == "-D" ])
66+
else
67+
(sudo iptables -t nat $op PREROUTING -d $publicIp -i eth0 -p $prot --dport $port -j DNAT --to-destination $guestVmIp:$guestPort &>> $OUTFILE || [ "$op" == "-D" ]) &&
68+
(sudo iptables -t nat $op POSTROUTING -s $vrGuestIPNetwork -p $prot --dport $port -d $guestVmIp -j SNAT -o eth0 --to-source $vrGuestIP &>> $OUTFILE || [ "$op" == "-D" ])
69+
fi
6370
}
6471

6572
#Port (address translation) forwarding for tcp or udp
@@ -185,6 +192,8 @@ static_nat() {
185192
local op=$3
186193
local op2="-D"
187194
local rulenum=
195+
local proto="all"
196+
188197
logger -t cloud "$(basename $0): static nat: public ip=$publicIp \
189198
instance ip=$instIp op=$op"
190199

@@ -217,7 +226,8 @@ static_nat() {
217226
(sudo iptables $op FORWARD -i $dev -o eth0 -d $instIp -m state \
218227
--state NEW -j ACCEPT &>> $OUTFILE || [ "$op" == "-D" ]) &&
219228
(sudo iptables -t nat $op2 POSTROUTING $rulenum -s $instIp -j SNAT \
220-
-o $dev --to-source $publicIp &>> $OUTFILE || [ "$op" == "-D" ])
229+
-o $dev --to-source $publicIp &>> $OUTFILE || [ "$op" == "-D" ]) &&
230+
(doHairpinNat $publicIp $proto "all" $instIp "0:65535" $op)
221231

222232
result=$?
223233
logger -t cloud "$(basename $0): done static nat entry public ip=$publicIp op=$op result=$result"

0 commit comments

Comments
 (0)